blob: 2b703f1fac3a1de8cefd93509cf87ae3813c22c5 [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;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200212}
213
Krzysztof Heltc8bfff62008-09-05 23:46:19 +0200214static inline int rt_bandwidth_enabled(void)
215{
216 return sysctl_sched_rt_runtime >= 0;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200217}
218
219static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
220{
221 ktime_t now;
222
Peter Zijlstra0b148fa2008-08-19 12:33:04 +0200223 if (rt_bandwidth_enabled() && rt_b->rt_runtime == RUNTIME_INF)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200224 return;
225
226 if (hrtimer_active(&rt_b->rt_period_timer))
227 return;
228
229 spin_lock(&rt_b->rt_runtime_lock);
230 for (;;) {
231 if (hrtimer_active(&rt_b->rt_period_timer))
232 break;
233
234 now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
235 hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700236 hrtimer_start_expires(&rt_b->rt_period_timer,
237 HRTIMER_MODE_ABS);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200238 }
239 spin_unlock(&rt_b->rt_runtime_lock);
240}
241
242#ifdef CONFIG_RT_GROUP_SCHED
243static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
244{
245 hrtimer_cancel(&rt_b->rt_period_timer);
246}
247#endif
248
Heiko Carstens712555e2008-04-28 11:33:07 +0200249/*
250 * sched_domains_mutex serializes calls to arch_init_sched_domains,
251 * detach_destroy_domains and partition_sched_domains.
252 */
253static DEFINE_MUTEX(sched_domains_mutex);
254
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100255#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200256
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700257#include <linux/cgroup.h>
258
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200259struct cfs_rq;
260
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100261static LIST_HEAD(task_groups);
262
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200263/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200264struct task_group {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100265#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700266 struct cgroup_subsys_state css;
267#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100268
Arun R Bharadwaj6c415b92008-12-01 20:49:05 +0530269#ifdef CONFIG_USER_SCHED
270 uid_t uid;
271#endif
272
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100273#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200274 /* schedulable entities of this group on each cpu */
275 struct sched_entity **se;
276 /* runqueue "owned" by this group on each cpu */
277 struct cfs_rq **cfs_rq;
278 unsigned long shares;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100279#endif
280
281#ifdef CONFIG_RT_GROUP_SCHED
282 struct sched_rt_entity **rt_se;
283 struct rt_rq **rt_rq;
284
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200285 struct rt_bandwidth rt_bandwidth;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100286#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100287
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100288 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100289 struct list_head list;
Peter Zijlstraf473aa52008-04-19 19:45:00 +0200290
291 struct task_group *parent;
292 struct list_head siblings;
293 struct list_head children;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200294};
295
Dhaval Giani354d60c2008-04-19 19:44:59 +0200296#ifdef CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200297
Arun R Bharadwaj6c415b92008-12-01 20:49:05 +0530298/* Helper function to pass uid information to create_sched_user() */
299void set_tg_uid(struct user_struct *user)
300{
301 user->tg->uid = user->uid;
302}
303
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200304/*
305 * Root task group.
306 * Every UID task group (including init_task_group aka UID-0) will
307 * be a child to this group.
308 */
309struct task_group root_task_group;
310
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100311#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200312/* Default task group's sched entity on each cpu */
313static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
314/* Default task group's cfs_rq on each cpu */
315static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200316#endif /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100317
318#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100319static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
320static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200321#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +0200322#else /* !CONFIG_USER_SCHED */
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200323#define root_task_group init_task_group
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +0200324#endif /* CONFIG_USER_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100325
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100326/* task_group_lock serializes add/remove of task groups and also changes to
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100327 * a task group's cpu shares.
328 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100329static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100330
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100331#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100332#ifdef CONFIG_USER_SCHED
Ingo Molnar0eab9142008-01-25 21:08:19 +0100333# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200334#else /* !CONFIG_USER_SCHED */
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100335# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200336#endif /* CONFIG_USER_SCHED */
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200337
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800338/*
Lai Jiangshan2e084782008-06-12 16:42:58 +0800339 * A weight of 0 or 1 can cause arithmetics problems.
340 * A weight of a cfs_rq is the sum of weights of which entities
341 * are queued on this cfs_rq, so a weight of a entity should not be
342 * too large, so as the shares value of a task group.
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800343 * (The default weight is 1024 - so there's no practical
344 * limitation from this.)
345 */
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200346#define MIN_SHARES 2
Lai Jiangshan2e084782008-06-12 16:42:58 +0800347#define MAX_SHARES (1UL << 18)
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200348
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100349static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100350#endif
351
352/* Default task group.
353 * Every task in system belong to this group at bootup.
354 */
Mike Travis434d53b2008-04-04 18:11:04 -0700355struct task_group init_task_group;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200356
357/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200358static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200359{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200360 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200361
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100362#ifdef CONFIG_USER_SCHED
David Howellsc69e8d92008-11-14 10:39:19 +1100363 rcu_read_lock();
364 tg = __task_cred(p)->user->tg;
365 rcu_read_unlock();
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100366#elif defined(CONFIG_CGROUP_SCHED)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700367 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
368 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200369#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100370 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200371#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200372 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200373}
374
375/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100376static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200377{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100378#ifdef CONFIG_FAIR_GROUP_SCHED
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100379 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
380 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100381#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100382
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100383#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100384 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
385 p->rt.parent = task_group(p)->rt_se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100386#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200387}
388
389#else
390
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100391static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Peter Zijlstra83378262008-06-27 13:41:37 +0200392static inline struct task_group *task_group(struct task_struct *p)
393{
394 return NULL;
395}
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200396
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100397#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200398
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200399/* CFS-related fields in a runqueue */
400struct cfs_rq {
401 struct load_weight load;
402 unsigned long nr_running;
403
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200404 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200405 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200406
407 struct rb_root tasks_timeline;
408 struct rb_node *rb_leftmost;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +0200409
410 struct list_head tasks;
411 struct list_head *balance_iterator;
412
413 /*
414 * 'curr' points to currently running entity on this cfs_rq.
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200415 * It is set to NULL otherwise (i.e when none are currently running).
416 */
Peter Zijlstra47932412008-11-04 21:25:09 +0100417 struct sched_entity *curr, *next, *last;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200418
Peter Zijlstra5ac5c4d2008-11-10 10:46:32 +0100419 unsigned int nr_spread_over;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200420
Ingo Molnar62160e3f2007-10-15 17:00:03 +0200421#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200422 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
423
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100424 /*
425 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200426 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
427 * (like users, containers etc.)
428 *
429 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
430 * list is used during load balance.
431 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100432 struct list_head leaf_cfs_rq_list;
433 struct task_group *tg; /* group that "owns" this runqueue */
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200434
435#ifdef CONFIG_SMP
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200436 /*
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200437 * the part of load.weight contributed by tasks
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200438 */
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200439 unsigned long task_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200440
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200441 /*
442 * h_load = weight * f(tg)
443 *
444 * Where f(tg) is the recursive weight fraction assigned to
445 * this group.
446 */
447 unsigned long h_load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200448
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200449 /*
450 * this cpu's part of tg->shares
451 */
452 unsigned long shares;
Peter Zijlstraf1d239f2008-06-27 13:41:38 +0200453
454 /*
455 * load.weight at the time we set shares
456 */
457 unsigned long rq_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200458#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200459#endif
460};
461
462/* Real-Time classes' related field in a runqueue: */
463struct rt_rq {
464 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100465 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100466#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Gregory Haskinse864c492008-12-29 09:39:49 -0500467 struct {
468 int curr; /* highest queued rt task prio */
Gregory Haskins398a1532009-01-14 09:10:04 -0500469#ifdef CONFIG_SMP
Gregory Haskinse864c492008-12-29 09:39:49 -0500470 int next; /* next highest */
Gregory Haskins398a1532009-01-14 09:10:04 -0500471#endif
Gregory Haskinse864c492008-12-29 09:39:49 -0500472 } highest_prio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100473#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100474#ifdef CONFIG_SMP
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100475 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100476 int overloaded;
Gregory Haskins917b6272008-12-29 09:39:53 -0500477 struct plist_head pushable_tasks;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100478#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100479 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100480 u64 rt_time;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200481 u64 rt_runtime;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100482 /* Nests inside the rq lock: */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200483 spinlock_t rt_runtime_lock;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100484
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100485#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100486 unsigned long rt_nr_boosted;
487
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100488 struct rq *rq;
489 struct list_head leaf_rt_rq_list;
490 struct task_group *tg;
491 struct sched_rt_entity *rt_se;
492#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200493};
494
Gregory Haskins57d885f2008-01-25 21:08:18 +0100495#ifdef CONFIG_SMP
496
497/*
498 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100499 * variables. Each exclusive cpuset essentially defines an island domain by
500 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100501 * exclusive cpuset is created, we also create and attach a new root-domain
502 * object.
503 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100504 */
505struct root_domain {
506 atomic_t refcount;
Rusty Russellc6c49272008-11-25 02:35:05 +1030507 cpumask_var_t span;
508 cpumask_var_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100509
Ingo Molnar0eab9142008-01-25 21:08:19 +0100510 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100511 * The "RT overload" flag: it gets set if a CPU has more than
512 * one runnable RT task.
513 */
Rusty Russellc6c49272008-11-25 02:35:05 +1030514 cpumask_var_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100515 atomic_t rto_count;
Gregory Haskins6e0534f2008-05-12 21:21:01 +0200516#ifdef CONFIG_SMP
517 struct cpupri cpupri;
518#endif
Vaidyanathan Srinivasan7a09b1a2008-12-18 23:26:22 +0530519#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
520 /*
521 * Preferred wake up cpu nominated by sched_mc balance that will be
522 * used when most cpus are idle in the system indicating overall very
523 * low system utilisation. Triggered at POWERSAVINGS_BALANCE_WAKEUP(2)
524 */
525 unsigned int sched_mc_preferred_wakeup_cpu;
526#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +0100527};
528
Gregory Haskinsdc938522008-01-25 21:08:26 +0100529/*
530 * By default the system creates a single root-domain with all cpus as
531 * members (mimicking the global state we have today).
532 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100533static struct root_domain def_root_domain;
534
535#endif
536
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200537/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * This is the main, per-CPU runqueue data structure.
539 *
540 * Locking rule: those places that want to lock multiple runqueues
541 * (such as the load balancing or the thread migration code), lock
542 * acquire operations must be ordered by ascending &runqueue.
543 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700544struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200545 /* runqueue lock: */
546 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 /*
549 * nr_running and cpu_load should be in the same cacheline because
550 * remote CPUs use both these fields when doing load calculation.
551 */
552 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200553 #define CPU_LOAD_IDX_MAX 5
554 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700555 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700556#ifdef CONFIG_NO_HZ
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200557 unsigned long last_tick_seen;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700558 unsigned char in_nohz_recently;
559#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200560 /* capture load from *all* tasks on this cpu: */
561 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200562 unsigned long nr_load_updates;
563 u64 nr_switches;
564
565 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100566 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100567
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200568#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200569 /* list of leaf cfs_rq on this cpu: */
570 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100571#endif
572#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100573 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 /*
577 * This is part of a global counter where only the total sum
578 * over all CPUs matters. A task can increase this counter on
579 * one CPU and if it got migrated afterwards it may decrease
580 * it on another CPU. Always updated under the runqueue lock:
581 */
582 unsigned long nr_uninterruptible;
583
Ingo Molnar36c8b582006-07-03 00:25:41 -0700584 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800585 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200587
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200588 u64 clock;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 atomic_t nr_iowait;
591
592#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100593 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 struct sched_domain *sd;
595
596 /* For active balancing */
597 int active_balance;
598 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200599 /* cpu of this runqueue: */
600 int cpu;
Gregory Haskins1f11eb62008-06-04 15:04:05 -0400601 int online;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Peter Zijlstraa8a51d52008-06-27 13:41:26 +0200603 unsigned long avg_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Ingo Molnar36c8b582006-07-03 00:25:41 -0700605 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 struct list_head migration_queue;
607#endif
608
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100609#ifdef CONFIG_SCHED_HRTICK
Peter Zijlstra31656512008-07-18 18:01:23 +0200610#ifdef CONFIG_SMP
611 int hrtick_csd_pending;
612 struct call_single_data hrtick_csd;
613#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100614 struct hrtimer hrtick_timer;
615#endif
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617#ifdef CONFIG_SCHEDSTATS
618 /* latency stats */
619 struct sched_info rq_sched_info;
Ken Chen9c2c4802008-12-16 23:41:22 -0800620 unsigned long long rq_cpu_time;
621 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200624 unsigned int yld_exp_empty;
625 unsigned int yld_act_empty;
626 unsigned int yld_both_empty;
627 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200630 unsigned int sched_switch;
631 unsigned int sched_count;
632 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200635 unsigned int ttwu_count;
636 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200637
638 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200639 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640#endif
641};
642
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700643static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Peter Zijlstra15afe092008-09-20 23:38:02 +0200645static inline void check_preempt_curr(struct rq *rq, struct task_struct *p, int sync)
Ingo Molnardd41f592007-07-09 18:51:59 +0200646{
Peter Zijlstra15afe092008-09-20 23:38:02 +0200647 rq->curr->sched_class->check_preempt_curr(rq, p, sync);
Ingo Molnardd41f592007-07-09 18:51:59 +0200648}
649
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700650static inline int cpu_of(struct rq *rq)
651{
652#ifdef CONFIG_SMP
653 return rq->cpu;
654#else
655 return 0;
656#endif
657}
658
Ingo Molnar20d315d2007-07-09 18:51:58 +0200659/*
Nick Piggin674311d2005-06-25 14:57:27 -0700660 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700661 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700662 *
663 * The domain tree of any CPU may only be accessed from within
664 * preempt-disabled sections.
665 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700666#define for_each_domain(cpu, __sd) \
667 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
670#define this_rq() (&__get_cpu_var(runqueues))
671#define task_rq(p) cpu_rq(task_cpu(p))
672#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
673
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200674static inline void update_rq_clock(struct rq *rq)
675{
676 rq->clock = sched_clock_cpu(cpu_of(rq));
677}
678
Ingo Molnare436d802007-07-19 21:28:35 +0200679/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200680 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
681 */
682#ifdef CONFIG_SCHED_DEBUG
683# define const_debug __read_mostly
684#else
685# define const_debug static const
686#endif
687
Ingo Molnar017730c2008-05-12 21:20:52 +0200688/**
689 * runqueue_is_locked
690 *
691 * Returns true if the current cpu runqueue is locked.
692 * This interface allows printk to be called with the runqueue lock
693 * held and know whether or not it is OK to wake up the klogd.
694 */
695int runqueue_is_locked(void)
696{
697 int cpu = get_cpu();
698 struct rq *rq = cpu_rq(cpu);
699 int ret;
700
701 ret = spin_is_locked(&rq->lock);
702 put_cpu();
703 return ret;
704}
705
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200706/*
707 * Debugging: various feature bits
708 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200709
710#define SCHED_FEAT(name, enabled) \
711 __SCHED_FEAT_##name ,
712
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200713enum {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200714#include "sched_features.h"
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200715};
716
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200717#undef SCHED_FEAT
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200718
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200719#define SCHED_FEAT(name, enabled) \
720 (1UL << __SCHED_FEAT_##name) * enabled |
721
722const_debug unsigned int sysctl_sched_features =
723#include "sched_features.h"
724 0;
725
726#undef SCHED_FEAT
727
728#ifdef CONFIG_SCHED_DEBUG
729#define SCHED_FEAT(name, enabled) \
730 #name ,
731
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700732static __read_mostly char *sched_feat_names[] = {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200733#include "sched_features.h"
734 NULL
735};
736
737#undef SCHED_FEAT
738
Li Zefan34f3a812008-10-30 15:23:32 +0800739static int sched_feat_show(struct seq_file *m, void *v)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200740{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200741 int i;
742
743 for (i = 0; sched_feat_names[i]; i++) {
Li Zefan34f3a812008-10-30 15:23:32 +0800744 if (!(sysctl_sched_features & (1UL << i)))
745 seq_puts(m, "NO_");
746 seq_printf(m, "%s ", sched_feat_names[i]);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200747 }
Li Zefan34f3a812008-10-30 15:23:32 +0800748 seq_puts(m, "\n");
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200749
Li Zefan34f3a812008-10-30 15:23:32 +0800750 return 0;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200751}
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
Li Zefan34f3a812008-10-30 15:23:32 +0800795static int sched_feat_open(struct inode *inode, struct file *filp)
796{
797 return single_open(filp, sched_feat_show, NULL);
798}
799
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200800static struct file_operations sched_feat_fops = {
Li Zefan34f3a812008-10-30 15:23:32 +0800801 .open = sched_feat_open,
802 .write = sched_feat_write,
803 .read = seq_read,
804 .llseek = seq_lseek,
805 .release = single_release,
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200806};
807
808static __init int sched_init_debug(void)
809{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200810 debugfs_create_file("sched_features", 0644, NULL, NULL,
811 &sched_feat_fops);
812
813 return 0;
814}
815late_initcall(sched_init_debug);
816
817#endif
818
819#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200820
821/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100822 * Number of tasks to iterate in a single balance run.
823 * Limited because this is done with IRQs disabled.
824 */
825const_debug unsigned int sysctl_sched_nr_migrate = 32;
826
827/*
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200828 * ratelimit for updating the group shares.
Peter Zijlstra55cd5342008-08-04 08:54:26 +0200829 * default: 0.25ms
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200830 */
Peter Zijlstra55cd5342008-08-04 08:54:26 +0200831unsigned int sysctl_sched_shares_ratelimit = 250000;
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200832
833/*
Peter Zijlstraffda12a2008-10-17 19:27:02 +0200834 * Inject some fuzzyness into changing the per-cpu group shares
835 * this avoids remote rq-locks at the expense of fairness.
836 * default: 4
837 */
838unsigned int sysctl_sched_shares_thresh = 4;
839
840/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100841 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100842 * default: 1s
843 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100844unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100845
Ingo Molnar6892b752008-02-13 14:02:36 +0100846static __read_mostly int scheduler_running;
847
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100848/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100849 * part of the period that we allow rt tasks to run in us.
850 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100851 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100852int sysctl_sched_rt_runtime = 950000;
853
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200854static inline u64 global_rt_period(void)
855{
856 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
857}
858
859static inline u64 global_rt_runtime(void)
860{
roel kluine26873b2008-07-22 16:51:15 -0400861 if (sysctl_sched_rt_runtime < 0)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200862 return RUNTIME_INF;
863
864 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
865}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700868# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700870#ifndef finish_arch_switch
871# define finish_arch_switch(prev) do { } while (0)
872#endif
873
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100874static inline int task_current(struct rq *rq, struct task_struct *p)
875{
876 return rq->curr == p;
877}
878
Nick Piggin4866cde2005-06-25 14:57:23 -0700879#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700880static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700881{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100882 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700883}
884
Ingo Molnar70b97a72006-07-03 00:25:42 -0700885static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700886{
887}
888
Ingo Molnar70b97a72006-07-03 00:25:42 -0700889static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700890{
Ingo Molnarda04c032005-09-13 11:17:59 +0200891#ifdef CONFIG_DEBUG_SPINLOCK
892 /* this is a valid case when another task releases the spinlock */
893 rq->lock.owner = current;
894#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700895 /*
896 * If we are tracking spinlock dependencies then we have to
897 * fix up the runqueue lock - which gets 'carried over' from
898 * prev into current:
899 */
900 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
901
Nick Piggin4866cde2005-06-25 14:57:23 -0700902 spin_unlock_irq(&rq->lock);
903}
904
905#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700906static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700907{
908#ifdef CONFIG_SMP
909 return p->oncpu;
910#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100911 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700912#endif
913}
914
Ingo Molnar70b97a72006-07-03 00:25:42 -0700915static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700916{
917#ifdef CONFIG_SMP
918 /*
919 * We can optimise this out completely for !SMP, because the
920 * SMP rebalancing from interrupt is the only thing that cares
921 * here.
922 */
923 next->oncpu = 1;
924#endif
925#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
926 spin_unlock_irq(&rq->lock);
927#else
928 spin_unlock(&rq->lock);
929#endif
930}
931
Ingo Molnar70b97a72006-07-03 00:25:42 -0700932static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700933{
934#ifdef CONFIG_SMP
935 /*
936 * After ->oncpu is cleared, the task can be moved to a different CPU.
937 * We must ensure this doesn't happen until the switch is completely
938 * finished.
939 */
940 smp_wmb();
941 prev->oncpu = 0;
942#endif
943#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
944 local_irq_enable();
945#endif
946}
947#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700950 * __task_rq_lock - lock the runqueue a given task resides on.
951 * Must be called interrupts disabled.
952 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700953static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700954 __acquires(rq->lock)
955{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200956 for (;;) {
957 struct rq *rq = task_rq(p);
958 spin_lock(&rq->lock);
959 if (likely(rq == task_rq(p)))
960 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700961 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700962 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700963}
964
965/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100967 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 * explicitly disabling preemption.
969 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700970static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 __acquires(rq->lock)
972{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700973 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Andi Kleen3a5c3592007-10-15 17:00:14 +0200975 for (;;) {
976 local_irq_save(*flags);
977 rq = task_rq(p);
978 spin_lock(&rq->lock);
979 if (likely(rq == task_rq(p)))
980 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
Oleg Nesterovad474ca2008-11-10 15:39:30 +0100985void task_rq_unlock_wait(struct task_struct *p)
986{
987 struct rq *rq = task_rq(p);
988
989 smp_mb(); /* spin-unlock-wait is not a full memory barrier */
990 spin_unlock_wait(&rq->lock);
991}
992
Alexey Dobriyana9957442007-10-15 17:00:13 +0200993static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700994 __releases(rq->lock)
995{
996 spin_unlock(&rq->lock);
997}
998
Ingo Molnar70b97a72006-07-03 00:25:42 -0700999static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 __releases(rq->lock)
1001{
1002 spin_unlock_irqrestore(&rq->lock, *flags);
1003}
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -08001006 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001008static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 __acquires(rq->lock)
1010{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001011 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 local_irq_disable();
1014 rq = this_rq();
1015 spin_lock(&rq->lock);
1016
1017 return rq;
1018}
1019
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001020#ifdef CONFIG_SCHED_HRTICK
1021/*
1022 * Use HR-timers to deliver accurate preemption points.
1023 *
1024 * Its all a bit involved since we cannot program an hrt while holding the
1025 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1026 * reschedule event.
1027 *
1028 * When we get rescheduled we reprogram the hrtick_timer outside of the
1029 * rq->lock.
1030 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001031
1032/*
1033 * Use hrtick when:
1034 * - enabled by features
1035 * - hrtimer is actually high res
1036 */
1037static inline int hrtick_enabled(struct rq *rq)
1038{
1039 if (!sched_feat(HRTICK))
1040 return 0;
Ingo Molnarba420592008-07-20 11:02:06 +02001041 if (!cpu_active(cpu_of(rq)))
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001042 return 0;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001043 return hrtimer_is_hres_active(&rq->hrtick_timer);
1044}
1045
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001046static void hrtick_clear(struct rq *rq)
1047{
1048 if (hrtimer_active(&rq->hrtick_timer))
1049 hrtimer_cancel(&rq->hrtick_timer);
1050}
1051
1052/*
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001053 * High-resolution timer tick.
1054 * Runs from hardirq context with interrupts disabled.
1055 */
1056static enum hrtimer_restart hrtick(struct hrtimer *timer)
1057{
1058 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1059
1060 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1061
1062 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02001063 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001064 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1065 spin_unlock(&rq->lock);
1066
1067 return HRTIMER_NORESTART;
1068}
1069
Rabin Vincent95e904c2008-05-11 05:55:33 +05301070#ifdef CONFIG_SMP
Peter Zijlstra31656512008-07-18 18:01:23 +02001071/*
1072 * called from hardirq (IPI) context
1073 */
1074static void __hrtick_start(void *arg)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001075{
Peter Zijlstra31656512008-07-18 18:01:23 +02001076 struct rq *rq = arg;
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001077
Peter Zijlstra31656512008-07-18 18:01:23 +02001078 spin_lock(&rq->lock);
1079 hrtimer_restart(&rq->hrtick_timer);
1080 rq->hrtick_csd_pending = 0;
1081 spin_unlock(&rq->lock);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001082}
1083
Peter Zijlstra31656512008-07-18 18:01:23 +02001084/*
1085 * Called to set the hrtick timer state.
1086 *
1087 * called with rq->lock held and irqs disabled
1088 */
1089static void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001090{
Peter Zijlstra31656512008-07-18 18:01:23 +02001091 struct hrtimer *timer = &rq->hrtick_timer;
1092 ktime_t time = ktime_add_ns(timer->base->get_time(), delay);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001093
Arjan van de Vencc584b22008-09-01 15:02:30 -07001094 hrtimer_set_expires(timer, time);
Peter Zijlstra31656512008-07-18 18:01:23 +02001095
1096 if (rq == this_rq()) {
1097 hrtimer_restart(timer);
1098 } else if (!rq->hrtick_csd_pending) {
1099 __smp_call_function_single(cpu_of(rq), &rq->hrtick_csd);
1100 rq->hrtick_csd_pending = 1;
1101 }
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001102}
1103
1104static int
1105hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
1106{
1107 int cpu = (int)(long)hcpu;
1108
1109 switch (action) {
1110 case CPU_UP_CANCELED:
1111 case CPU_UP_CANCELED_FROZEN:
1112 case CPU_DOWN_PREPARE:
1113 case CPU_DOWN_PREPARE_FROZEN:
1114 case CPU_DEAD:
1115 case CPU_DEAD_FROZEN:
Peter Zijlstra31656512008-07-18 18:01:23 +02001116 hrtick_clear(cpu_rq(cpu));
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001117 return NOTIFY_OK;
1118 }
1119
1120 return NOTIFY_DONE;
1121}
1122
Rakib Mullickfa748202008-09-22 14:55:45 -07001123static __init void init_hrtick(void)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001124{
1125 hotcpu_notifier(hotplug_hrtick, 0);
1126}
Peter Zijlstra31656512008-07-18 18:01:23 +02001127#else
1128/*
1129 * Called to set the hrtick timer state.
1130 *
1131 * called with rq->lock held and irqs disabled
1132 */
1133static void hrtick_start(struct rq *rq, u64 delay)
1134{
1135 hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay), HRTIMER_MODE_REL);
1136}
1137
Andrew Morton006c75f2008-09-22 14:55:46 -07001138static inline void init_hrtick(void)
Peter Zijlstra31656512008-07-18 18:01:23 +02001139{
1140}
Rabin Vincent95e904c2008-05-11 05:55:33 +05301141#endif /* CONFIG_SMP */
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001142
1143static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001144{
Peter Zijlstra31656512008-07-18 18:01:23 +02001145#ifdef CONFIG_SMP
1146 rq->hrtick_csd_pending = 0;
1147
1148 rq->hrtick_csd.flags = 0;
1149 rq->hrtick_csd.func = __hrtick_start;
1150 rq->hrtick_csd.info = rq;
1151#endif
1152
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001153 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1154 rq->hrtick_timer.function = hrtick;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001155}
Andrew Morton006c75f2008-09-22 14:55:46 -07001156#else /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001157static inline void hrtick_clear(struct rq *rq)
1158{
1159}
1160
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001161static inline void init_rq_hrtick(struct rq *rq)
1162{
1163}
1164
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001165static inline void init_hrtick(void)
1166{
1167}
Andrew Morton006c75f2008-09-22 14:55:46 -07001168#endif /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001169
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001170/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001171 * resched_task - mark a task 'to be rescheduled now'.
1172 *
1173 * On UP this means the setting of the need_resched flag, on SMP it
1174 * might also involve a cross-CPU call to trigger the scheduler on
1175 * the target CPU.
1176 */
1177#ifdef CONFIG_SMP
1178
1179#ifndef tsk_is_polling
1180#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1181#endif
1182
Peter Zijlstra31656512008-07-18 18:01:23 +02001183static void resched_task(struct task_struct *p)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001184{
1185 int cpu;
1186
1187 assert_spin_locked(&task_rq(p)->lock);
1188
Peter Zijlstra31656512008-07-18 18:01:23 +02001189 if (unlikely(test_tsk_thread_flag(p, TIF_NEED_RESCHED)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001190 return;
1191
Peter Zijlstra31656512008-07-18 18:01:23 +02001192 set_tsk_thread_flag(p, TIF_NEED_RESCHED);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001193
1194 cpu = task_cpu(p);
1195 if (cpu == smp_processor_id())
1196 return;
1197
1198 /* NEED_RESCHED must be visible before we test polling */
1199 smp_mb();
1200 if (!tsk_is_polling(p))
1201 smp_send_reschedule(cpu);
1202}
1203
1204static void resched_cpu(int cpu)
1205{
1206 struct rq *rq = cpu_rq(cpu);
1207 unsigned long flags;
1208
1209 if (!spin_trylock_irqsave(&rq->lock, flags))
1210 return;
1211 resched_task(cpu_curr(cpu));
1212 spin_unlock_irqrestore(&rq->lock, flags);
1213}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001214
1215#ifdef CONFIG_NO_HZ
1216/*
1217 * When add_timer_on() enqueues a timer into the timer wheel of an
1218 * idle CPU then this timer might expire before the next timer event
1219 * which is scheduled to wake up that CPU. In case of a completely
1220 * idle system the next event might even be infinite time into the
1221 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1222 * leaves the inner idle loop so the newly added timer is taken into
1223 * account when the CPU goes back to idle and evaluates the timer
1224 * wheel for the next timer event.
1225 */
1226void wake_up_idle_cpu(int cpu)
1227{
1228 struct rq *rq = cpu_rq(cpu);
1229
1230 if (cpu == smp_processor_id())
1231 return;
1232
1233 /*
1234 * This is safe, as this function is called with the timer
1235 * wheel base lock of (cpu) held. When the CPU is on the way
1236 * to idle and has not yet set rq->curr to idle then it will
1237 * be serialized on the timer wheel base lock and take the new
1238 * timer into account automatically.
1239 */
1240 if (rq->curr != rq->idle)
1241 return;
1242
1243 /*
1244 * We can set TIF_RESCHED on the idle task of the other CPU
1245 * lockless. The worst case is that the other CPU runs the
1246 * idle task through an additional NOOP schedule()
1247 */
1248 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1249
1250 /* NEED_RESCHED must be visible before we test polling */
1251 smp_mb();
1252 if (!tsk_is_polling(rq->idle))
1253 smp_send_reschedule(cpu);
1254}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001255#endif /* CONFIG_NO_HZ */
Thomas Gleixner06d83082008-03-22 09:20:24 +01001256
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001257#else /* !CONFIG_SMP */
Peter Zijlstra31656512008-07-18 18:01:23 +02001258static void resched_task(struct task_struct *p)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001259{
1260 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra31656512008-07-18 18:01:23 +02001261 set_tsk_need_resched(p);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001262}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001263#endif /* CONFIG_SMP */
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001264
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001265#if BITS_PER_LONG == 32
1266# define WMULT_CONST (~0UL)
1267#else
1268# define WMULT_CONST (1UL << 32)
1269#endif
1270
1271#define WMULT_SHIFT 32
1272
Ingo Molnar194081e2007-08-09 11:16:51 +02001273/*
1274 * Shift right and round:
1275 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001276#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001277
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001278/*
1279 * delta *= weight / lw
1280 */
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001281static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001282calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1283 struct load_weight *lw)
1284{
1285 u64 tmp;
1286
Lai Jiangshan7a232e02008-06-12 16:43:07 +08001287 if (!lw->inv_weight) {
1288 if (BITS_PER_LONG > 32 && unlikely(lw->weight >= WMULT_CONST))
1289 lw->inv_weight = 1;
1290 else
1291 lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)
1292 / (lw->weight+1);
1293 }
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001294
1295 tmp = (u64)delta_exec * weight;
1296 /*
1297 * Check whether we'd overflow the 64-bit multiplication:
1298 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001299 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001300 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001301 WMULT_SHIFT/2);
1302 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001303 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001304
Ingo Molnarecf691d2007-08-02 17:41:40 +02001305 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001306}
1307
Ingo Molnar10919852007-10-15 17:00:04 +02001308static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001309{
1310 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001311 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001312}
1313
Ingo Molnar10919852007-10-15 17:00:04 +02001314static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001315{
1316 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001317 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001318}
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001321 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1322 * of tasks with abnormal "nice" values across CPUs the contribution that
1323 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001324 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001325 * scaled version of the new time slice allocation that they receive on time
1326 * slice expiry etc.
1327 */
1328
Ingo Molnardd41f592007-07-09 18:51:59 +02001329#define WEIGHT_IDLEPRIO 2
1330#define WMULT_IDLEPRIO (1 << 31)
1331
1332/*
1333 * Nice levels are multiplicative, with a gentle 10% change for every
1334 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1335 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1336 * that remained on nice 0.
1337 *
1338 * The "10% effect" is relative and cumulative: from _any_ nice level,
1339 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +02001340 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1341 * If a task goes up by ~10% and another task goes down by ~10% then
1342 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001343 */
1344static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001345 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1346 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1347 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1348 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1349 /* 0 */ 1024, 820, 655, 526, 423,
1350 /* 5 */ 335, 272, 215, 172, 137,
1351 /* 10 */ 110, 87, 70, 56, 45,
1352 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001353};
1354
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001355/*
1356 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1357 *
1358 * In cases where the weight does not change often, we can use the
1359 * precalculated inverse to speed up arithmetics by turning divisions
1360 * into multiplications:
1361 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001362static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001363 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1364 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1365 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1366 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1367 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1368 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1369 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1370 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001371};
Peter Williams2dd73a42006-06-27 02:54:34 -07001372
Ingo Molnardd41f592007-07-09 18:51:59 +02001373static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1374
1375/*
1376 * runqueue iterator, to support SMP load-balancing between different
1377 * scheduling classes, without having to expose their internal data
1378 * structures to the load-balancing proper:
1379 */
1380struct rq_iterator {
1381 void *arg;
1382 struct task_struct *(*start)(void *);
1383 struct task_struct *(*next)(void *);
1384};
1385
Peter Williamse1d14842007-10-24 18:23:51 +02001386#ifdef CONFIG_SMP
1387static unsigned long
1388balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1389 unsigned long max_load_move, struct sched_domain *sd,
1390 enum cpu_idle_type idle, int *all_pinned,
1391 int *this_best_prio, struct rq_iterator *iterator);
1392
1393static int
1394iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1395 struct sched_domain *sd, enum cpu_idle_type idle,
1396 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001397#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001398
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001399#ifdef CONFIG_CGROUP_CPUACCT
1400static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1401#else
1402static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1403#endif
1404
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001405static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1406{
1407 update_load_add(&rq->load, load);
1408}
1409
1410static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1411{
1412 update_load_sub(&rq->load, load);
1413}
1414
Ingo Molnar7940ca32008-08-19 13:40:47 +02001415#if (defined(CONFIG_SMP) && defined(CONFIG_FAIR_GROUP_SCHED)) || defined(CONFIG_RT_GROUP_SCHED)
Peter Zijlstraeb755802008-08-19 12:33:05 +02001416typedef int (*tg_visitor)(struct task_group *, void *);
1417
1418/*
1419 * Iterate the full tree, calling @down when first entering a node and @up when
1420 * leaving it for the final time.
1421 */
1422static int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
1423{
1424 struct task_group *parent, *child;
1425 int ret;
1426
1427 rcu_read_lock();
1428 parent = &root_task_group;
1429down:
1430 ret = (*down)(parent, data);
1431 if (ret)
1432 goto out_unlock;
1433 list_for_each_entry_rcu(child, &parent->children, siblings) {
1434 parent = child;
1435 goto down;
1436
1437up:
1438 continue;
1439 }
1440 ret = (*up)(parent, data);
1441 if (ret)
1442 goto out_unlock;
1443
1444 child = parent;
1445 parent = parent->parent;
1446 if (parent)
1447 goto up;
1448out_unlock:
1449 rcu_read_unlock();
1450
1451 return ret;
1452}
1453
1454static int tg_nop(struct task_group *tg, void *data)
1455{
1456 return 0;
1457}
1458#endif
1459
Gregory Haskinse7693a32008-01-25 21:08:09 +01001460#ifdef CONFIG_SMP
1461static unsigned long source_load(int cpu, int type);
1462static unsigned long target_load(int cpu, int type);
Gregory Haskinse7693a32008-01-25 21:08:09 +01001463static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001464
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001465static unsigned long cpu_avg_load_per_task(int cpu)
1466{
1467 struct rq *rq = cpu_rq(cpu);
Ingo Molnaraf6d5962008-11-29 20:45:15 +01001468 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001469
Steven Rostedt4cd42622008-11-26 21:04:24 -05001470 if (nr_running)
1471 rq->avg_load_per_task = rq->load.weight / nr_running;
Balbir Singha2d47772008-11-12 16:19:00 +05301472 else
1473 rq->avg_load_per_task = 0;
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001474
1475 return rq->avg_load_per_task;
1476}
1477
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001478#ifdef CONFIG_FAIR_GROUP_SCHED
1479
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001480static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1481
1482/*
1483 * Calculate and set the cpu's group shares.
1484 */
1485static void
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001486update_group_shares_cpu(struct task_group *tg, int cpu,
1487 unsigned long sd_shares, unsigned long sd_rq_weight)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001488{
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001489 unsigned long shares;
1490 unsigned long rq_weight;
1491
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001492 if (!tg->se[cpu])
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001493 return;
1494
Ken Chenec4e0e22008-11-18 22:41:57 -08001495 rq_weight = tg->cfs_rq[cpu]->rq_weight;
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001496
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001497 /*
1498 * \Sum shares * rq_weight
1499 * shares = -----------------------
1500 * \Sum rq_weight
1501 *
1502 */
Ken Chenec4e0e22008-11-18 22:41:57 -08001503 shares = (sd_shares * rq_weight) / sd_rq_weight;
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001504 shares = clamp_t(unsigned long, shares, MIN_SHARES, MAX_SHARES);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001505
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001506 if (abs(shares - tg->se[cpu]->load.weight) >
1507 sysctl_sched_shares_thresh) {
1508 struct rq *rq = cpu_rq(cpu);
1509 unsigned long flags;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001510
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001511 spin_lock_irqsave(&rq->lock, flags);
Ken Chenec4e0e22008-11-18 22:41:57 -08001512 tg->cfs_rq[cpu]->shares = shares;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001513
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001514 __set_se_shares(tg->se[cpu], shares);
1515 spin_unlock_irqrestore(&rq->lock, flags);
1516 }
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001517}
1518
1519/*
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001520 * Re-compute the task group their per cpu shares over the given domain.
1521 * This needs to be done in a bottom-up fashion because the rq weight of a
1522 * parent group depends on the shares of its child groups.
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001523 */
Peter Zijlstraeb755802008-08-19 12:33:05 +02001524static int tg_shares_up(struct task_group *tg, void *data)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001525{
Ken Chenec4e0e22008-11-18 22:41:57 -08001526 unsigned long weight, rq_weight = 0;
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001527 unsigned long shares = 0;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001528 struct sched_domain *sd = data;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001529 int i;
1530
Rusty Russell758b2cd2008-11-25 02:35:04 +10301531 for_each_cpu(i, sched_domain_span(sd)) {
Ken Chenec4e0e22008-11-18 22:41:57 -08001532 /*
1533 * If there are currently no tasks on the cpu pretend there
1534 * is one of average load so that when a new task gets to
1535 * run here it will not get delayed by group starvation.
1536 */
1537 weight = tg->cfs_rq[i]->load.weight;
1538 if (!weight)
1539 weight = NICE_0_LOAD;
1540
1541 tg->cfs_rq[i]->rq_weight = weight;
1542 rq_weight += weight;
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001543 shares += tg->cfs_rq[i]->shares;
1544 }
1545
1546 if ((!shares && rq_weight) || shares > tg->shares)
1547 shares = tg->shares;
1548
1549 if (!sd->parent || !(sd->parent->flags & SD_LOAD_BALANCE))
1550 shares = tg->shares;
1551
Rusty Russell758b2cd2008-11-25 02:35:04 +10301552 for_each_cpu(i, sched_domain_span(sd))
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001553 update_group_shares_cpu(tg, i, shares, rq_weight);
Peter Zijlstraeb755802008-08-19 12:33:05 +02001554
1555 return 0;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001556}
1557
1558/*
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001559 * Compute the cpu's hierarchical load factor for each task group.
1560 * This needs to be done in a top-down fashion because the load of a child
1561 * group is a fraction of its parents load.
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001562 */
Peter Zijlstraeb755802008-08-19 12:33:05 +02001563static int tg_load_down(struct task_group *tg, void *data)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001564{
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001565 unsigned long load;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001566 long cpu = (long)data;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001567
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001568 if (!tg->parent) {
1569 load = cpu_rq(cpu)->load.weight;
1570 } else {
1571 load = tg->parent->cfs_rq[cpu]->h_load;
1572 load *= tg->cfs_rq[cpu]->shares;
1573 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
1574 }
1575
1576 tg->cfs_rq[cpu]->h_load = load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001577
Peter Zijlstraeb755802008-08-19 12:33:05 +02001578 return 0;
Peter Zijlstra4d8d5952008-06-27 13:41:19 +02001579}
1580
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001581static void update_shares(struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001582{
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02001583 u64 now = cpu_clock(raw_smp_processor_id());
1584 s64 elapsed = now - sd->last_update;
1585
1586 if (elapsed >= (s64)(u64)sysctl_sched_shares_ratelimit) {
1587 sd->last_update = now;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001588 walk_tg_tree(tg_nop, tg_shares_up, sd);
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02001589 }
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001590}
1591
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02001592static void update_shares_locked(struct rq *rq, struct sched_domain *sd)
1593{
1594 spin_unlock(&rq->lock);
1595 update_shares(sd);
1596 spin_lock(&rq->lock);
1597}
1598
Peter Zijlstraeb755802008-08-19 12:33:05 +02001599static void update_h_load(long cpu)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001600{
Peter Zijlstraeb755802008-08-19 12:33:05 +02001601 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001602}
1603
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001604#else
1605
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001606static inline void update_shares(struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001607{
1608}
1609
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02001610static inline void update_shares_locked(struct rq *rq, struct sched_domain *sd)
1611{
1612}
1613
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001614#endif
1615
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001616#ifdef CONFIG_PREEMPT
1617
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001618/*
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001619 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1620 * way at the expense of forcing extra atomic operations in all
1621 * invocations. This assures that the double_lock is acquired using the
1622 * same underlying policy as the spinlock_t on this architecture, which
1623 * reduces latency compared to the unfair variant below. However, it
1624 * also adds more overhead and therefore may reduce throughput.
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001625 */
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001626static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1627 __releases(this_rq->lock)
1628 __acquires(busiest->lock)
1629 __acquires(this_rq->lock)
1630{
1631 spin_unlock(&this_rq->lock);
1632 double_rq_lock(this_rq, busiest);
1633
1634 return 1;
1635}
1636
1637#else
1638/*
1639 * Unfair double_lock_balance: Optimizes throughput at the expense of
1640 * latency by eliminating extra atomic operations when the locks are
1641 * already in proper order on entry. This favors lower cpu-ids and will
1642 * grant the double lock to lower cpus over higher ids under contention,
1643 * regardless of entry order into the function.
1644 */
1645static int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001646 __releases(this_rq->lock)
1647 __acquires(busiest->lock)
1648 __acquires(this_rq->lock)
1649{
1650 int ret = 0;
1651
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001652 if (unlikely(!spin_trylock(&busiest->lock))) {
1653 if (busiest < this_rq) {
1654 spin_unlock(&this_rq->lock);
1655 spin_lock(&busiest->lock);
1656 spin_lock_nested(&this_rq->lock, SINGLE_DEPTH_NESTING);
1657 ret = 1;
1658 } else
1659 spin_lock_nested(&busiest->lock, SINGLE_DEPTH_NESTING);
1660 }
1661 return ret;
1662}
1663
Gregory Haskins8f45e2b2008-12-29 09:39:51 -05001664#endif /* CONFIG_PREEMPT */
1665
1666/*
1667 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1668 */
1669static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1670{
1671 if (unlikely(!irqs_disabled())) {
1672 /* printk() doesn't work good under rq->lock */
1673 spin_unlock(&this_rq->lock);
1674 BUG_ON(1);
1675 }
1676
1677 return _double_lock_balance(this_rq, busiest);
1678}
1679
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001680static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1681 __releases(busiest->lock)
1682{
1683 spin_unlock(&busiest->lock);
1684 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1685}
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001686#endif
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001687
1688#ifdef CONFIG_FAIR_GROUP_SCHED
1689static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1690{
Vegard Nossum30432092008-06-27 21:35:50 +02001691#ifdef CONFIG_SMP
Ingo Molnar34e83e82008-06-27 15:42:36 +02001692 cfs_rq->shares = shares;
1693#endif
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001694}
1695#endif
1696
Ingo Molnardd41f592007-07-09 18:51:59 +02001697#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001698#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001699#include "sched_fair.c"
1700#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001701#ifdef CONFIG_SCHED_DEBUG
1702# include "sched_debug.c"
1703#endif
1704
1705#define sched_class_highest (&rt_sched_class)
Gregory Haskins1f11eb62008-06-04 15:04:05 -04001706#define for_each_class(class) \
1707 for (class = sched_class_highest; class; class = class->next)
Ingo Molnardd41f592007-07-09 18:51:59 +02001708
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001709static void inc_nr_running(struct rq *rq)
Ingo Molnar6363ca52008-05-29 11:28:57 +02001710{
1711 rq->nr_running++;
Ingo Molnar6363ca52008-05-29 11:28:57 +02001712}
1713
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001714static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001715{
1716 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001717}
1718
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001719static void set_load_weight(struct task_struct *p)
1720{
1721 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001722 p->se.load.weight = prio_to_weight[0] * 2;
1723 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1724 return;
1725 }
1726
1727 /*
1728 * SCHED_IDLE tasks get minimal weight:
1729 */
1730 if (p->policy == SCHED_IDLE) {
1731 p->se.load.weight = WEIGHT_IDLEPRIO;
1732 p->se.load.inv_weight = WMULT_IDLEPRIO;
1733 return;
1734 }
1735
1736 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1737 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001738}
1739
Gregory Haskins2087a1a2008-06-27 14:30:00 -06001740static void update_avg(u64 *avg, u64 sample)
1741{
1742 s64 diff = sample - *avg;
1743 *avg += diff >> 3;
1744}
1745
Ingo Molnar8159f872007-08-09 11:16:49 +02001746static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001747{
1748 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001749 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001750 p->se.on_rq = 1;
1751}
1752
Ingo Molnar69be72c2007-08-09 11:16:49 +02001753static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001754{
Gregory Haskins2087a1a2008-06-27 14:30:00 -06001755 if (sleep && p->se.last_wakeup) {
1756 update_avg(&p->se.avg_overlap,
1757 p->se.sum_exec_runtime - p->se.last_wakeup);
1758 p->se.last_wakeup = 0;
1759 }
1760
Ankita Garg46ac22b2008-07-01 14:30:06 +05301761 sched_info_dequeued(p);
Ingo Molnarf02231e2007-08-09 11:16:48 +02001762 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001763 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001764}
1765
1766/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001767 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001768 */
Ingo Molnar14531182007-07-09 18:51:59 +02001769static inline int __normal_prio(struct task_struct *p)
1770{
Ingo Molnardd41f592007-07-09 18:51:59 +02001771 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001772}
1773
1774/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001775 * Calculate the expected normal priority: i.e. priority
1776 * without taking RT-inheritance into account. Might be
1777 * boosted by interactivity modifiers. Changes upon fork,
1778 * setprio syscalls, and whenever the interactivity
1779 * estimator recalculates.
1780 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001781static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001782{
1783 int prio;
1784
Ingo Molnare05606d2007-07-09 18:51:59 +02001785 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001786 prio = MAX_RT_PRIO-1 - p->rt_priority;
1787 else
1788 prio = __normal_prio(p);
1789 return prio;
1790}
1791
1792/*
1793 * Calculate the current priority, i.e. the priority
1794 * taken into account by the scheduler. This value might
1795 * be boosted by RT tasks, or might be boosted by
1796 * interactivity modifiers. Will be RT if the task got
1797 * RT-boosted. If not then it returns p->normal_prio.
1798 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001799static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001800{
1801 p->normal_prio = normal_prio(p);
1802 /*
1803 * If we are RT tasks or we were boosted to RT priority,
1804 * keep the priority unchanged. Otherwise, update priority
1805 * to the normal priority:
1806 */
1807 if (!rt_prio(p->prio))
1808 return p->normal_prio;
1809 return p->prio;
1810}
1811
1812/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001813 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001815static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001817 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001818 rq->nr_uninterruptible--;
1819
Ingo Molnar8159f872007-08-09 11:16:49 +02001820 enqueue_task(rq, p, wakeup);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001821 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
1823
1824/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 * deactivate_task - remove a task from the runqueue.
1826 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02001827static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001829 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001830 rq->nr_uninterruptible++;
1831
Ingo Molnar69be72c2007-08-09 11:16:49 +02001832 dequeue_task(rq, p, sleep);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001833 dec_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836/**
1837 * task_curr - is this task currently executing on a CPU?
1838 * @p: the task in question.
1839 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001840inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
1842 return cpu_curr(task_cpu(p)) == p;
1843}
1844
Ingo Molnardd41f592007-07-09 18:51:59 +02001845static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1846{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001847 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001848#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01001849 /*
1850 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1851 * successfuly executed on another CPU. We must ensure that updates of
1852 * per-task data have been completed by this moment.
1853 */
1854 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02001855 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02001856#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07001857}
1858
Steven Rostedtcb469842008-01-25 21:08:22 +01001859static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1860 const struct sched_class *prev_class,
1861 int oldprio, int running)
1862{
1863 if (prev_class != p->sched_class) {
1864 if (prev_class->switched_from)
1865 prev_class->switched_from(rq, p, running);
1866 p->sched_class->switched_to(rq, p, running);
1867 } else
1868 p->sched_class->prio_changed(rq, p, oldprio, running);
1869}
1870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02001872
Thomas Gleixnere958b362008-06-04 23:22:32 +02001873/* Used instead of source_load when we know the type == 0 */
1874static unsigned long weighted_cpuload(const int cpu)
1875{
1876 return cpu_rq(cpu)->load.weight;
1877}
1878
Ingo Molnarcc367732007-10-15 17:00:18 +02001879/*
1880 * Is this task likely cache-hot:
1881 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001882static int
Ingo Molnarcc367732007-10-15 17:00:18 +02001883task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
1884{
1885 s64 delta;
1886
Ingo Molnarf540a602008-03-15 17:10:34 +01001887 /*
1888 * Buddy candidates are cache hot:
1889 */
Peter Zijlstra47932412008-11-04 21:25:09 +01001890 if (sched_feat(CACHE_HOT_BUDDY) &&
1891 (&p->se == cfs_rq_of(&p->se)->next ||
1892 &p->se == cfs_rq_of(&p->se)->last))
Ingo Molnarf540a602008-03-15 17:10:34 +01001893 return 1;
1894
Ingo Molnarcc367732007-10-15 17:00:18 +02001895 if (p->sched_class != &fair_sched_class)
1896 return 0;
1897
Ingo Molnar6bc16652007-10-15 17:00:18 +02001898 if (sysctl_sched_migration_cost == -1)
1899 return 1;
1900 if (sysctl_sched_migration_cost == 0)
1901 return 0;
1902
Ingo Molnarcc367732007-10-15 17:00:18 +02001903 delta = now - p->se.exec_start;
1904
1905 return delta < (s64)sysctl_sched_migration_cost;
1906}
1907
1908
Ingo Molnardd41f592007-07-09 18:51:59 +02001909void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001910{
Ingo Molnardd41f592007-07-09 18:51:59 +02001911 int old_cpu = task_cpu(p);
1912 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001913 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
1914 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02001915 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001916
1917 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001918
Peter Zijlstracbc34ed2008-12-10 08:08:22 +01001919 trace_sched_migrate_task(p, task_cpu(p), new_cpu);
1920
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001921#ifdef CONFIG_SCHEDSTATS
1922 if (p->se.wait_start)
1923 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001924 if (p->se.sleep_start)
1925 p->se.sleep_start -= clock_offset;
1926 if (p->se.block_start)
1927 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02001928 if (old_cpu != new_cpu) {
1929 schedstat_inc(p, se.nr_migrations);
1930 if (task_hot(p, old_rq->clock, NULL))
1931 schedstat_inc(p, se.nr_forced2_migrations);
1932 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001933#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001934 p->se.vruntime -= old_cfsrq->min_vruntime -
1935 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02001936
1937 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001938}
1939
Ingo Molnar70b97a72006-07-03 00:25:42 -07001940struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Ingo Molnar36c8b582006-07-03 00:25:41 -07001943 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 int dest_cpu;
1945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001947};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949/*
1950 * The task's runqueue lock must be held.
1951 * Returns true if you have to wait for migration thread.
1952 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001953static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07001954migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001956 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 /*
1959 * If the task is not on a runqueue (and not running), then
1960 * it is sufficient to simply update the task's cpu field.
1961 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001962 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 set_task_cpu(p, dest_cpu);
1964 return 0;
1965 }
1966
1967 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 req->task = p;
1969 req->dest_cpu = dest_cpu;
1970 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07001971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 return 1;
1973}
1974
1975/*
1976 * wait_task_inactive - wait for a thread to unschedule.
1977 *
Roland McGrath85ba2d82008-07-25 19:45:58 -07001978 * If @match_state is nonzero, it's the @p->state value just checked and
1979 * not expected to change. If it changes, i.e. @p might have woken up,
1980 * then return zero. When we succeed in waiting for @p to be off its CPU,
1981 * we return a positive number (its total switch count). If a second call
1982 * a short while later returns the same number, the caller can be sure that
1983 * @p has remained unscheduled the whole time.
1984 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 * The caller must ensure that the task *will* unschedule sometime soon,
1986 * else this function might spin for a *long* time. This function can't
1987 * be called with interrupts off, or it may introduce deadlock with
1988 * smp_call_function() if an IPI is sent by the same process we are
1989 * waiting to become inactive.
1990 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07001991unsigned long wait_task_inactive(struct task_struct *p, long match_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
1993 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001994 int running, on_rq;
Roland McGrath85ba2d82008-07-25 19:45:58 -07001995 unsigned long ncsw;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001996 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Andi Kleen3a5c3592007-10-15 17:00:14 +02001998 for (;;) {
1999 /*
2000 * We do the initial early heuristics without holding
2001 * any task-queue locks at all. We'll only try to get
2002 * the runqueue lock when things look like they will
2003 * work out!
2004 */
2005 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002006
Andi Kleen3a5c3592007-10-15 17:00:14 +02002007 /*
2008 * If the task is actively running on another CPU
2009 * still, just relax and busy-wait without holding
2010 * any locks.
2011 *
2012 * NOTE! Since we don't hold any locks, it's not
2013 * even sure that "rq" stays as the right runqueue!
2014 * But we don't care, since "task_running()" will
2015 * return false if the runqueue has changed and p
2016 * is actually now running somewhere else!
2017 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07002018 while (task_running(rq, p)) {
2019 if (match_state && unlikely(p->state != match_state))
2020 return 0;
Andi Kleen3a5c3592007-10-15 17:00:14 +02002021 cpu_relax();
Roland McGrath85ba2d82008-07-25 19:45:58 -07002022 }
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002023
Andi Kleen3a5c3592007-10-15 17:00:14 +02002024 /*
2025 * Ok, time to look more closely! We need the rq
2026 * lock now, to be *sure*. If we're wrong, we'll
2027 * just go back and repeat.
2028 */
2029 rq = task_rq_lock(p, &flags);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04002030 trace_sched_wait_task(rq, p);
Andi Kleen3a5c3592007-10-15 17:00:14 +02002031 running = task_running(rq, p);
2032 on_rq = p->se.on_rq;
Roland McGrath85ba2d82008-07-25 19:45:58 -07002033 ncsw = 0;
Oleg Nesterovf31e11d2008-08-20 16:54:44 -07002034 if (!match_state || p->state == match_state)
Oleg Nesterov93dcf552008-08-20 16:54:44 -07002035 ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
Andi Kleen3a5c3592007-10-15 17:00:14 +02002036 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002037
Andi Kleen3a5c3592007-10-15 17:00:14 +02002038 /*
Roland McGrath85ba2d82008-07-25 19:45:58 -07002039 * If it changed from the expected state, bail out now.
2040 */
2041 if (unlikely(!ncsw))
2042 break;
2043
2044 /*
Andi Kleen3a5c3592007-10-15 17:00:14 +02002045 * Was it really running after all now that we
2046 * checked with the proper locks actually held?
2047 *
2048 * Oops. Go back and try again..
2049 */
2050 if (unlikely(running)) {
2051 cpu_relax();
2052 continue;
2053 }
2054
2055 /*
2056 * It's not enough that it's not actively running,
2057 * it must be off the runqueue _entirely_, and not
2058 * preempted!
2059 *
2060 * So if it wa still runnable (but just not actively
2061 * running right now), it's preempted, and we should
2062 * yield - it could be a while.
2063 */
2064 if (unlikely(on_rq)) {
2065 schedule_timeout_uninterruptible(1);
2066 continue;
2067 }
2068
2069 /*
2070 * Ahh, all good. It wasn't running, and it wasn't
2071 * runnable, which means that it will never become
2072 * running in the future either. We're all done!
2073 */
2074 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 }
Roland McGrath85ba2d82008-07-25 19:45:58 -07002076
2077 return ncsw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
2080/***
2081 * kick_process - kick a running thread to enter/exit the kernel
2082 * @p: the to-be-kicked thread
2083 *
2084 * Cause a process which is running on another CPU to enter
2085 * kernel-mode, without any delay. (to get signals handled.)
2086 *
2087 * NOTE: this function doesnt have to take the runqueue lock,
2088 * because all it wants to ensure is that the remote task enters
2089 * the kernel. If the IPI races and the task has been migrated
2090 * to another CPU then no harm is done and the purpose has been
2091 * achieved as well.
2092 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002093void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
2095 int cpu;
2096
2097 preempt_disable();
2098 cpu = task_cpu(p);
2099 if ((cpu != smp_processor_id()) && task_curr(p))
2100 smp_send_reschedule(cpu);
2101 preempt_enable();
2102}
2103
2104/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002105 * Return a low guess at the load of a migration-source cpu weighted
2106 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 *
2108 * We want to under-estimate the load of migration sources, to
2109 * balance conservatively.
2110 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002111static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002112{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002113 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002114 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002115
Peter Zijlstra93b75212008-06-27 13:41:33 +02002116 if (type == 0 || !sched_feat(LB_BIAS))
Ingo Molnardd41f592007-07-09 18:51:59 +02002117 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002118
Ingo Molnardd41f592007-07-09 18:51:59 +02002119 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
2122/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002123 * Return a high guess at the load of a migration-target cpu weighted
2124 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002126static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002127{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002128 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002129 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002130
Peter Zijlstra93b75212008-06-27 13:41:33 +02002131 if (type == 0 || !sched_feat(LB_BIAS))
Ingo Molnardd41f592007-07-09 18:51:59 +02002132 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002133
Ingo Molnardd41f592007-07-09 18:51:59 +02002134 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07002135}
2136
2137/*
Nick Piggin147cbb42005-06-25 14:57:19 -07002138 * find_idlest_group finds and returns the least busy CPU group within the
2139 * domain.
2140 */
2141static struct sched_group *
2142find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
2143{
2144 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
2145 unsigned long min_load = ULONG_MAX, this_load = 0;
2146 int load_idx = sd->forkexec_idx;
2147 int imbalance = 100 + (sd->imbalance_pct-100)/2;
2148
2149 do {
2150 unsigned long load, avg_load;
2151 int local_group;
2152 int i;
2153
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002154 /* Skip over this group if it has no CPUs allowed */
Rusty Russell758b2cd2008-11-25 02:35:04 +10302155 if (!cpumask_intersects(sched_group_cpus(group),
2156 &p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02002157 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002158
Rusty Russell758b2cd2008-11-25 02:35:04 +10302159 local_group = cpumask_test_cpu(this_cpu,
2160 sched_group_cpus(group));
Nick Piggin147cbb42005-06-25 14:57:19 -07002161
2162 /* Tally up the load of all CPUs in the group */
2163 avg_load = 0;
2164
Rusty Russell758b2cd2008-11-25 02:35:04 +10302165 for_each_cpu(i, sched_group_cpus(group)) {
Nick Piggin147cbb42005-06-25 14:57:19 -07002166 /* Bias balancing toward cpus of our domain */
2167 if (local_group)
2168 load = source_load(i, load_idx);
2169 else
2170 load = target_load(i, load_idx);
2171
2172 avg_load += load;
2173 }
2174
2175 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002176 avg_load = sg_div_cpu_power(group,
2177 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07002178
2179 if (local_group) {
2180 this_load = avg_load;
2181 this = group;
2182 } else if (avg_load < min_load) {
2183 min_load = avg_load;
2184 idlest = group;
2185 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02002186 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07002187
2188 if (!idlest || 100*this_load < imbalance*min_load)
2189 return NULL;
2190 return idlest;
2191}
2192
2193/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07002194 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07002195 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002196static int
Rusty Russell758b2cd2008-11-25 02:35:04 +10302197find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
Nick Piggin147cbb42005-06-25 14:57:19 -07002198{
2199 unsigned long load, min_load = ULONG_MAX;
2200 int idlest = -1;
2201 int i;
2202
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002203 /* Traverse only the allowed CPUs */
Rusty Russell758b2cd2008-11-25 02:35:04 +10302204 for_each_cpu_and(i, sched_group_cpus(group), &p->cpus_allowed) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002205 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07002206
2207 if (load < min_load || (load == min_load && i == this_cpu)) {
2208 min_load = load;
2209 idlest = i;
2210 }
2211 }
2212
2213 return idlest;
2214}
2215
Nick Piggin476d1392005-06-25 14:57:29 -07002216/*
2217 * sched_balance_self: balance the current task (running on cpu) in domains
2218 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2219 * SD_BALANCE_EXEC.
2220 *
2221 * Balance, ie. select the least loaded group.
2222 *
2223 * Returns the target CPU number, or the same CPU if no balancing is needed.
2224 *
2225 * preempt must be disabled.
2226 */
2227static int sched_balance_self(int cpu, int flag)
2228{
2229 struct task_struct *t = current;
2230 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07002231
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002232 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02002233 /*
2234 * If power savings logic is enabled for a domain, stop there.
2235 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002236 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2237 break;
Nick Piggin476d1392005-06-25 14:57:29 -07002238 if (tmp->flags & flag)
2239 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002240 }
Nick Piggin476d1392005-06-25 14:57:29 -07002241
Peter Zijlstra039a1c42008-06-27 13:41:25 +02002242 if (sd)
2243 update_shares(sd);
2244
Nick Piggin476d1392005-06-25 14:57:29 -07002245 while (sd) {
Nick Piggin476d1392005-06-25 14:57:29 -07002246 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002247 int new_cpu, weight;
2248
2249 if (!(sd->flags & flag)) {
2250 sd = sd->child;
2251 continue;
2252 }
Nick Piggin476d1392005-06-25 14:57:29 -07002253
Nick Piggin476d1392005-06-25 14:57:29 -07002254 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002255 if (!group) {
2256 sd = sd->child;
2257 continue;
2258 }
Nick Piggin476d1392005-06-25 14:57:29 -07002259
Rusty Russell758b2cd2008-11-25 02:35:04 +10302260 new_cpu = find_idlest_cpu(group, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002261 if (new_cpu == -1 || new_cpu == cpu) {
2262 /* Now try balancing at a lower domain level of cpu */
2263 sd = sd->child;
2264 continue;
2265 }
Nick Piggin476d1392005-06-25 14:57:29 -07002266
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002267 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07002268 cpu = new_cpu;
Rusty Russell758b2cd2008-11-25 02:35:04 +10302269 weight = cpumask_weight(sched_domain_span(sd));
Nick Piggin476d1392005-06-25 14:57:29 -07002270 sd = NULL;
Nick Piggin476d1392005-06-25 14:57:29 -07002271 for_each_domain(cpu, tmp) {
Rusty Russell758b2cd2008-11-25 02:35:04 +10302272 if (weight <= cpumask_weight(sched_domain_span(tmp)))
Nick Piggin476d1392005-06-25 14:57:29 -07002273 break;
2274 if (tmp->flags & flag)
2275 sd = tmp;
2276 }
2277 /* while loop will break here if sd == NULL */
2278 }
2279
2280 return cpu;
2281}
2282
2283#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285/***
2286 * try_to_wake_up - wake up a thread
2287 * @p: the to-be-woken-up thread
2288 * @state: the mask of task states that can be woken
2289 * @sync: do a synchronous wakeup?
2290 *
2291 * Put it on the run-queue if it's not already there. The "current"
2292 * thread is always on the run-queue (except when the actual
2293 * re-schedule is in progress), and as such you're allowed to do
2294 * the simpler "current->state = TASK_RUNNING" to mark yourself
2295 * runnable without the overhead of this.
2296 *
2297 * returns failure only if the task is already active.
2298 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002299static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
Ingo Molnarcc367732007-10-15 17:00:18 +02002301 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 unsigned long flags;
2303 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002304 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Ingo Molnarb85d0662008-03-16 20:03:22 +01002306 if (!sched_feat(SYNC_WAKEUPS))
2307 sync = 0;
2308
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02002309#ifdef CONFIG_SMP
2310 if (sched_feat(LB_WAKEUP_UPDATE)) {
2311 struct sched_domain *sd;
2312
2313 this_cpu = raw_smp_processor_id();
2314 cpu = task_cpu(p);
2315
2316 for_each_domain(this_cpu, sd) {
Rusty Russell758b2cd2008-11-25 02:35:04 +10302317 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02002318 update_shares(sd);
2319 break;
2320 }
2321 }
2322 }
2323#endif
2324
Linus Torvalds04e2f172008-02-23 18:05:03 -08002325 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 rq = task_rq_lock(p, &flags);
Mike Galbraith03e89e42008-12-16 08:45:30 +01002327 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 old_state = p->state;
2329 if (!(old_state & state))
2330 goto out;
2331
Ingo Molnardd41f592007-07-09 18:51:59 +02002332 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 goto out_running;
2334
2335 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002336 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 this_cpu = smp_processor_id();
2338
2339#ifdef CONFIG_SMP
2340 if (unlikely(task_running(rq, p)))
2341 goto out_activate;
2342
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01002343 cpu = p->sched_class->select_task_rq(p, sync);
2344 if (cpu != orig_cpu) {
2345 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 task_rq_unlock(rq, &flags);
2347 /* might preempt at this point */
2348 rq = task_rq_lock(p, &flags);
2349 old_state = p->state;
2350 if (!(old_state & state))
2351 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02002352 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 goto out_running;
2354
2355 this_cpu = smp_processor_id();
2356 cpu = task_cpu(p);
2357 }
2358
Gregory Haskinse7693a32008-01-25 21:08:09 +01002359#ifdef CONFIG_SCHEDSTATS
2360 schedstat_inc(rq, ttwu_count);
2361 if (cpu == this_cpu)
2362 schedstat_inc(rq, ttwu_local);
2363 else {
2364 struct sched_domain *sd;
2365 for_each_domain(this_cpu, sd) {
Rusty Russell758b2cd2008-11-25 02:35:04 +10302366 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Gregory Haskinse7693a32008-01-25 21:08:09 +01002367 schedstat_inc(sd, ttwu_wake_remote);
2368 break;
2369 }
2370 }
2371 }
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002372#endif /* CONFIG_SCHEDSTATS */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002373
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374out_activate:
2375#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02002376 schedstat_inc(p, se.nr_wakeups);
2377 if (sync)
2378 schedstat_inc(p, se.nr_wakeups_sync);
2379 if (orig_cpu != cpu)
2380 schedstat_inc(p, se.nr_wakeups_migrate);
2381 if (cpu == this_cpu)
2382 schedstat_inc(p, se.nr_wakeups_local);
2383 else
2384 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnardd41f592007-07-09 18:51:59 +02002385 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 success = 1;
2387
2388out_running:
Peter Zijlstra468a15b2008-12-16 08:07:03 +01002389 trace_sched_wakeup(rq, p, success);
Peter Zijlstra15afe092008-09-20 23:38:02 +02002390 check_preempt_curr(rq, p, sync);
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002393#ifdef CONFIG_SMP
2394 if (p->sched_class->task_wake_up)
2395 p->sched_class->task_wake_up(rq, p);
2396#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397out:
Gregory Haskins2087a1a2008-06-27 14:30:00 -06002398 current->se.last_wakeup = current->se.sum_exec_runtime;
2399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 task_rq_unlock(rq, &flags);
2401
2402 return success;
2403}
2404
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002405int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002407 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409EXPORT_SYMBOL(wake_up_process);
2410
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002411int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412{
2413 return try_to_wake_up(p, state, 0);
2414}
2415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416/*
2417 * Perform scheduler related setup for a newly forked process p.
2418 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002419 *
2420 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002422static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
Ingo Molnardd41f592007-07-09 18:51:59 +02002424 p->se.exec_start = 0;
2425 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002426 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002427 p->se.last_wakeup = 0;
2428 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002429
2430#ifdef CONFIG_SCHEDSTATS
2431 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002432 p->se.sum_sleep_runtime = 0;
2433 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002434 p->se.block_start = 0;
2435 p->se.sleep_max = 0;
2436 p->se.block_max = 0;
2437 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002438 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002439 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002440#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002441
Peter Zijlstrafa717062008-01-25 21:08:27 +01002442 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002443 p->se.on_rq = 0;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02002444 INIT_LIST_HEAD(&p->se.group_node);
Nick Piggin476d1392005-06-25 14:57:29 -07002445
Avi Kivitye107be32007-07-26 13:40:43 +02002446#ifdef CONFIG_PREEMPT_NOTIFIERS
2447 INIT_HLIST_HEAD(&p->preempt_notifiers);
2448#endif
2449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 /*
2451 * We mark the process as running here, but have not actually
2452 * inserted it onto the runqueue yet. This guarantees that
2453 * nobody will actually run it, and a signal or other external
2454 * event cannot wake it up and insert it on the runqueue either.
2455 */
2456 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002457}
2458
2459/*
2460 * fork()/clone()-time setup:
2461 */
2462void sched_fork(struct task_struct *p, int clone_flags)
2463{
2464 int cpu = get_cpu();
2465
2466 __sched_fork(p);
2467
2468#ifdef CONFIG_SMP
2469 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2470#endif
Ingo Molnar02e4bac2007-10-15 17:00:11 +02002471 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002472
2473 /*
2474 * Make sure we do not leak PI boosting priority to the child:
2475 */
2476 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002477 if (!rt_prio(p->prio))
2478 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002479
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002480#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002481 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002482 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002484#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002485 p->oncpu = 0;
2486#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002488 /* Want to start with kernel preemption disabled. */
Al Viroa1261f52005-11-13 16:06:55 -08002489 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490#endif
Gregory Haskins917b6272008-12-29 09:39:53 -05002491 plist_node_init(&p->pushable_tasks, MAX_PRIO);
2492
Nick Piggin476d1392005-06-25 14:57:29 -07002493 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}
2495
2496/*
2497 * wake_up_new_task - wake up a newly created task for the first time.
2498 *
2499 * This function will do some initial scheduler statistics housekeeping
2500 * that must be done for every newly created context, then puts the task
2501 * on the runqueue and wakes it.
2502 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002503void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
2505 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002506 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
2508 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002510 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
2512 p->prio = effective_prio(p);
2513
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002514 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002515 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002518 * Let the scheduling class do new task startup
2519 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002521 p->sched_class->task_new(rq, p);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02002522 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 }
Ingo Molnarc71dd422008-12-19 01:09:51 +01002524 trace_sched_wakeup_new(rq, p, 1);
Peter Zijlstra15afe092008-09-20 23:38:02 +02002525 check_preempt_curr(rq, p, 0);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002526#ifdef CONFIG_SMP
2527 if (p->sched_class->task_wake_up)
2528 p->sched_class->task_wake_up(rq, p);
2529#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002530 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531}
2532
Avi Kivitye107be32007-07-26 13:40:43 +02002533#ifdef CONFIG_PREEMPT_NOTIFIERS
2534
2535/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002536 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2537 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002538 */
2539void preempt_notifier_register(struct preempt_notifier *notifier)
2540{
2541 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2542}
2543EXPORT_SYMBOL_GPL(preempt_notifier_register);
2544
2545/**
2546 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002547 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002548 *
2549 * This is safe to call from within a preemption notifier.
2550 */
2551void preempt_notifier_unregister(struct preempt_notifier *notifier)
2552{
2553 hlist_del(&notifier->link);
2554}
2555EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2556
2557static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2558{
2559 struct preempt_notifier *notifier;
2560 struct hlist_node *node;
2561
2562 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2563 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2564}
2565
2566static void
2567fire_sched_out_preempt_notifiers(struct task_struct *curr,
2568 struct task_struct *next)
2569{
2570 struct preempt_notifier *notifier;
2571 struct hlist_node *node;
2572
2573 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2574 notifier->ops->sched_out(notifier, next);
2575}
2576
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002577#else /* !CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002578
2579static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2580{
2581}
2582
2583static void
2584fire_sched_out_preempt_notifiers(struct task_struct *curr,
2585 struct task_struct *next)
2586{
2587}
2588
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002589#endif /* CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002592 * prepare_task_switch - prepare to switch tasks
2593 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002594 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002595 * @next: the task we are going to switch to.
2596 *
2597 * This is called with the rq lock held and interrupts off. It must
2598 * be paired with a subsequent finish_task_switch after the context
2599 * switch.
2600 *
2601 * prepare_task_switch sets up locking and calls architecture specific
2602 * hooks.
2603 */
Avi Kivitye107be32007-07-26 13:40:43 +02002604static inline void
2605prepare_task_switch(struct rq *rq, struct task_struct *prev,
2606 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002607{
Avi Kivitye107be32007-07-26 13:40:43 +02002608 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002609 prepare_lock_switch(rq, next);
2610 prepare_arch_switch(next);
2611}
2612
2613/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002615 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 * @prev: the thread we just switched away from.
2617 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002618 * finish_task_switch must be called after the context switch, paired
2619 * with a prepare_task_switch call before the context switch.
2620 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2621 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 *
2623 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002624 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 * with the lock held can cause deadlocks; see schedule() for
2626 * details.)
2627 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002628static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 __releases(rq->lock)
2630{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002632 long prev_state;
Gregory Haskins967fc042008-12-29 09:39:52 -05002633#ifdef CONFIG_SMP
2634 int post_schedule = 0;
2635
2636 if (current->sched_class->needs_post_schedule)
2637 post_schedule = current->sched_class->needs_post_schedule(rq);
2638#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
2640 rq->prev_mm = NULL;
2641
2642 /*
2643 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002644 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002645 * schedule one last time. The schedule call will never return, and
2646 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002647 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 * still held, otherwise prev could be scheduled on another cpu, die
2649 * there before we look at prev->state, and then the reference would
2650 * be dropped twice.
2651 * Manfred Spraul <manfred@colorfullife.com>
2652 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002653 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002654 finish_arch_switch(prev);
2655 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002656#ifdef CONFIG_SMP
Gregory Haskins967fc042008-12-29 09:39:52 -05002657 if (post_schedule)
Steven Rostedt9a897c52008-01-25 21:08:22 +01002658 current->sched_class->post_schedule(rq);
2659#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002660
Avi Kivitye107be32007-07-26 13:40:43 +02002661 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 if (mm)
2663 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002664 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002665 /*
2666 * Remove function-return probe instances associated with this
2667 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002668 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002669 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672}
2673
2674/**
2675 * schedule_tail - first thing a freshly forked thread must call.
2676 * @prev: the thread we just switched away from.
2677 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002678asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 __releases(rq->lock)
2680{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002681 struct rq *rq = this_rq();
2682
Nick Piggin4866cde2005-06-25 14:57:23 -07002683 finish_task_switch(rq, prev);
2684#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2685 /* In this case, finish_task_switch does not reenable preemption */
2686 preempt_enable();
2687#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002689 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690}
2691
2692/*
2693 * context_switch - switch to the new MM and the new
2694 * thread's register state.
2695 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002696static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002697context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002698 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
Ingo Molnardd41f592007-07-09 18:51:59 +02002700 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
Avi Kivitye107be32007-07-26 13:40:43 +02002702 prepare_task_switch(rq, prev, next);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04002703 trace_sched_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002704 mm = next->mm;
2705 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002706 /*
2707 * For paravirt, this is coupled with an exit in switch_to to
2708 * combine the page table reload and the switch backend into
2709 * one hypercall.
2710 */
2711 arch_enter_lazy_cpu_mode();
2712
Ingo Molnardd41f592007-07-09 18:51:59 +02002713 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 next->active_mm = oldmm;
2715 atomic_inc(&oldmm->mm_count);
2716 enter_lazy_tlb(oldmm, next);
2717 } else
2718 switch_mm(oldmm, mm, next);
2719
Ingo Molnardd41f592007-07-09 18:51:59 +02002720 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 rq->prev_mm = oldmm;
2723 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002724 /*
2725 * Since the runqueue lock will be released by the next
2726 * task (which is an invalid locking op but in the case
2727 * of the scheduler it's an obvious special-case), so we
2728 * do an early lockdep release here:
2729 */
2730#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002731 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002732#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
2734 /* Here we just switch the register state and the stack. */
2735 switch_to(prev, next, prev);
2736
Ingo Molnardd41f592007-07-09 18:51:59 +02002737 barrier();
2738 /*
2739 * this_rq must be evaluated again because prev may have moved
2740 * CPUs since it called schedule(), thus the 'rq' on its stack
2741 * frame will be invalid.
2742 */
2743 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744}
2745
2746/*
2747 * nr_running, nr_uninterruptible and nr_context_switches:
2748 *
2749 * externally visible scheduler statistics: current number of runnable
2750 * threads, current number of uninterruptible-sleeping threads, total
2751 * number of context switches performed since bootup.
2752 */
2753unsigned long nr_running(void)
2754{
2755 unsigned long i, sum = 0;
2756
2757 for_each_online_cpu(i)
2758 sum += cpu_rq(i)->nr_running;
2759
2760 return sum;
2761}
2762
2763unsigned long nr_uninterruptible(void)
2764{
2765 unsigned long i, sum = 0;
2766
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002767 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 sum += cpu_rq(i)->nr_uninterruptible;
2769
2770 /*
2771 * Since we read the counters lockless, it might be slightly
2772 * inaccurate. Do not allow it to go below zero though:
2773 */
2774 if (unlikely((long)sum < 0))
2775 sum = 0;
2776
2777 return sum;
2778}
2779
2780unsigned long long nr_context_switches(void)
2781{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002782 int i;
2783 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002785 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 sum += cpu_rq(i)->nr_switches;
2787
2788 return sum;
2789}
2790
2791unsigned long nr_iowait(void)
2792{
2793 unsigned long i, sum = 0;
2794
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002795 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2797
2798 return sum;
2799}
2800
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002801unsigned long nr_active(void)
2802{
2803 unsigned long i, running = 0, uninterruptible = 0;
2804
2805 for_each_online_cpu(i) {
2806 running += cpu_rq(i)->nr_running;
2807 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2808 }
2809
2810 if (unlikely((long)uninterruptible < 0))
2811 uninterruptible = 0;
2812
2813 return running + uninterruptible;
2814}
2815
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002817 * Update rq->cpu_load[] statistics. This function is usually called every
2818 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002819 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002820static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002821{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002822 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002823 int i, scale;
2824
2825 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002826
2827 /* Update our load: */
2828 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2829 unsigned long old_load, new_load;
2830
2831 /* scale is effectively 1 << i now, and >> i divides by scale */
2832
2833 old_load = this_rq->cpu_load[i];
2834 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002835 /*
2836 * Round up the averaging division if load is increasing. This
2837 * prevents us from getting stuck on 9 if the load is 10, for
2838 * example.
2839 */
2840 if (new_load > old_load)
2841 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002842 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2843 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002844}
2845
Ingo Molnardd41f592007-07-09 18:51:59 +02002846#ifdef CONFIG_SMP
2847
Ingo Molnar48f24c42006-07-03 00:25:40 -07002848/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 * double_rq_lock - safely lock two runqueues
2850 *
2851 * Note this does not disable interrupts like task_rq_lock,
2852 * you need to do so manually before calling.
2853 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002854static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 __acquires(rq1->lock)
2856 __acquires(rq2->lock)
2857{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002858 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 if (rq1 == rq2) {
2860 spin_lock(&rq1->lock);
2861 __acquire(rq2->lock); /* Fake it out ;) */
2862 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002863 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 spin_lock(&rq1->lock);
Peter Zijlstra5e710e32008-07-30 13:26:57 +02002865 spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 } else {
2867 spin_lock(&rq2->lock);
Peter Zijlstra5e710e32008-07-30 13:26:57 +02002868 spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 }
2870 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002871 update_rq_clock(rq1);
2872 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873}
2874
2875/*
2876 * double_rq_unlock - safely unlock two runqueues
2877 *
2878 * Note this does not restore interrupts like task_rq_unlock,
2879 * you need to do so manually after calling.
2880 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002881static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 __releases(rq1->lock)
2883 __releases(rq2->lock)
2884{
2885 spin_unlock(&rq1->lock);
2886 if (rq1 != rq2)
2887 spin_unlock(&rq2->lock);
2888 else
2889 __release(rq2->lock);
2890}
2891
2892/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 * If dest_cpu is allowed for this process, migrate the task to it.
2894 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002895 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 * the cpu_allowed mask is restored.
2897 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002898static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002900 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002902 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
2904 rq = task_rq_lock(p, &flags);
Rusty Russell96f874e2008-11-25 02:35:14 +10302905 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed)
Max Krasnyanskye761b772008-07-15 04:43:49 -07002906 || unlikely(!cpu_active(dest_cpu)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 goto out;
2908
2909 /* force the process onto the specified CPU */
2910 if (migrate_task(p, dest_cpu, &req)) {
2911 /* Need to wait for migration thread (might exit: take ref). */
2912 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07002913
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 get_task_struct(mt);
2915 task_rq_unlock(rq, &flags);
2916 wake_up_process(mt);
2917 put_task_struct(mt);
2918 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07002919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 return;
2921 }
2922out:
2923 task_rq_unlock(rq, &flags);
2924}
2925
2926/*
Nick Piggin476d1392005-06-25 14:57:29 -07002927 * sched_exec - execve() is a valuable balancing opportunity, because at
2928 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 */
2930void sched_exec(void)
2931{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002933 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002935 if (new_cpu != this_cpu)
2936 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937}
2938
2939/*
2940 * pull_task - move a task from a remote runqueue to the local runqueue.
2941 * Both runqueues must be locked.
2942 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002943static void pull_task(struct rq *src_rq, struct task_struct *p,
2944 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02002946 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002948 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 /*
2950 * Note that idle threads have a prio of MAX_PRIO, for this test
2951 * to be always true for them.
2952 */
Peter Zijlstra15afe092008-09-20 23:38:02 +02002953 check_preempt_curr(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954}
2955
2956/*
2957 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
2958 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002959static
Ingo Molnar70b97a72006-07-03 00:25:42 -07002960int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002961 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002962 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963{
2964 /*
2965 * We do not migrate tasks that are:
2966 * 1) running (obviously), or
2967 * 2) cannot be migrated to this CPU due to cpus_allowed, or
2968 * 3) are cache-hot on their current CPU.
2969 */
Rusty Russell96f874e2008-11-25 02:35:14 +10302970 if (!cpumask_test_cpu(this_cpu, &p->cpus_allowed)) {
Ingo Molnarcc367732007-10-15 17:00:18 +02002971 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002973 }
Nick Piggin81026792005-06-25 14:57:07 -07002974 *all_pinned = 0;
2975
Ingo Molnarcc367732007-10-15 17:00:18 +02002976 if (task_running(rq, p)) {
2977 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07002978 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Ingo Molnarda84d962007-10-15 17:00:18 +02002981 /*
2982 * Aggressive migration if:
2983 * 1) task is cache cold, or
2984 * 2) too many balance attempts have failed.
2985 */
2986
Ingo Molnar6bc16652007-10-15 17:00:18 +02002987 if (!task_hot(p, rq->clock, sd) ||
2988 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002989#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02002990 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002991 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02002992 schedstat_inc(p, se.nr_forced_migrations);
2993 }
Ingo Molnarda84d962007-10-15 17:00:18 +02002994#endif
2995 return 1;
2996 }
2997
Ingo Molnarcc367732007-10-15 17:00:18 +02002998 if (task_hot(p, rq->clock, sd)) {
2999 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02003000 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 return 1;
3003}
3004
Peter Williamse1d14842007-10-24 18:23:51 +02003005static unsigned long
3006balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3007 unsigned long max_load_move, struct sched_domain *sd,
3008 enum cpu_idle_type idle, int *all_pinned,
3009 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02003010{
Peter Zijlstra051c6762008-06-27 13:41:31 +02003011 int loops = 0, pulled = 0, pinned = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02003012 struct task_struct *p;
3013 long rem_load_move = max_load_move;
3014
Peter Williamse1d14842007-10-24 18:23:51 +02003015 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02003016 goto out;
3017
3018 pinned = 1;
3019
3020 /*
3021 * Start the load-balancing iterator:
3022 */
3023 p = iterator->start(iterator->arg);
3024next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003025 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02003026 goto out;
Peter Zijlstra051c6762008-06-27 13:41:31 +02003027
3028 if ((p->se.load.weight >> 1) > rem_load_move ||
Ingo Molnardd41f592007-07-09 18:51:59 +02003029 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003030 p = iterator->next(iterator->arg);
3031 goto next;
3032 }
3033
3034 pull_task(busiest, p, this_rq, this_cpu);
3035 pulled++;
3036 rem_load_move -= p->se.load.weight;
3037
Gregory Haskins7e96fa52008-12-29 09:39:50 -05003038#ifdef CONFIG_PREEMPT
3039 /*
3040 * NEWIDLE balancing is a source of latency, so preemptible kernels
3041 * will stop after the first task is pulled to minimize the critical
3042 * section.
3043 */
3044 if (idle == CPU_NEWLY_IDLE)
3045 goto out;
3046#endif
3047
Ingo Molnardd41f592007-07-09 18:51:59 +02003048 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003049 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003050 */
Peter Williamse1d14842007-10-24 18:23:51 +02003051 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003052 if (p->prio < *this_best_prio)
3053 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02003054 p = iterator->next(iterator->arg);
3055 goto next;
3056 }
3057out:
3058 /*
Peter Williamse1d14842007-10-24 18:23:51 +02003059 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02003060 * so we can safely collect pull_task() stats here rather than
3061 * inside pull_task().
3062 */
3063 schedstat_add(sd, lb_gained[idle], pulled);
3064
3065 if (all_pinned)
3066 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02003067
3068 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02003069}
Ingo Molnar48f24c42006-07-03 00:25:40 -07003070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071/*
Peter Williams43010652007-08-09 11:16:46 +02003072 * move_tasks tries to move up to max_load_move weighted load from busiest to
3073 * this_rq, as part of a balancing operation within domain "sd".
3074 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 *
3076 * Called with both runqueues locked.
3077 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003078static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02003079 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003080 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07003081 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003083 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02003084 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003085 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Ingo Molnardd41f592007-07-09 18:51:59 +02003087 do {
Peter Williams43010652007-08-09 11:16:46 +02003088 total_load_moved +=
3089 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02003090 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003091 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02003092 class = class->next;
Gregory Haskinsc4acb2c2008-06-27 14:29:55 -06003093
Gregory Haskins7e96fa52008-12-29 09:39:50 -05003094#ifdef CONFIG_PREEMPT
3095 /*
3096 * NEWIDLE balancing is a source of latency, so preemptible
3097 * kernels will stop after the first task is pulled to minimize
3098 * the critical section.
3099 */
Gregory Haskinsc4acb2c2008-06-27 14:29:55 -06003100 if (idle == CPU_NEWLY_IDLE && this_rq->nr_running)
3101 break;
Gregory Haskins7e96fa52008-12-29 09:39:50 -05003102#endif
Peter Williams43010652007-08-09 11:16:46 +02003103 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Peter Williams43010652007-08-09 11:16:46 +02003105 return total_load_moved > 0;
3106}
3107
Peter Williamse1d14842007-10-24 18:23:51 +02003108static int
3109iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3110 struct sched_domain *sd, enum cpu_idle_type idle,
3111 struct rq_iterator *iterator)
3112{
3113 struct task_struct *p = iterator->start(iterator->arg);
3114 int pinned = 0;
3115
3116 while (p) {
3117 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3118 pull_task(busiest, p, this_rq, this_cpu);
3119 /*
3120 * Right now, this is only the second place pull_task()
3121 * is called, so we can safely collect pull_task()
3122 * stats here rather than inside pull_task().
3123 */
3124 schedstat_inc(sd, lb_gained[idle]);
3125
3126 return 1;
3127 }
3128 p = iterator->next(iterator->arg);
3129 }
3130
3131 return 0;
3132}
3133
Peter Williams43010652007-08-09 11:16:46 +02003134/*
3135 * move_one_task tries to move exactly one task from busiest to this_rq, as
3136 * part of active balancing operations within "domain".
3137 * Returns 1 if successful and 0 otherwise.
3138 *
3139 * Called with both runqueues locked.
3140 */
3141static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3142 struct sched_domain *sd, enum cpu_idle_type idle)
3143{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003144 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02003145
3146 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02003147 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02003148 return 1;
3149
3150 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151}
3152
3153/*
3154 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07003155 * domain. It calculates and returns the amount of weighted load which
3156 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 */
3158static struct sched_group *
3159find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02003160 unsigned long *imbalance, enum cpu_idle_type idle,
Rusty Russell96f874e2008-11-25 02:35:14 +10303161 int *sd_idle, const struct cpumask *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162{
3163 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
3164 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003165 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07003166 unsigned long busiest_load_per_task, busiest_nr_running;
3167 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003168 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003169#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3170 int power_savings_balance = 1;
3171 unsigned long leader_nr_running = 0, min_load_per_task = 0;
3172 unsigned long min_nr_running = ULONG_MAX;
3173 struct sched_group *group_min = NULL, *group_leader = NULL;
3174#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
3176 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003177 busiest_load_per_task = busiest_nr_running = 0;
3178 this_load_per_task = this_nr_running = 0;
Peter Zijlstra408ed062008-06-27 13:41:28 +02003179
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003180 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003181 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003182 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003183 load_idx = sd->newidle_idx;
3184 else
3185 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
3187 do {
Ken Chen908a7c12007-10-17 16:55:11 +02003188 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 int local_group;
3190 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02003191 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003192 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003193 unsigned long sum_nr_running, sum_weighted_load;
Peter Zijlstra408ed062008-06-27 13:41:28 +02003194 unsigned long sum_avg_load_per_task;
3195 unsigned long avg_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
Rusty Russell758b2cd2008-11-25 02:35:04 +10303197 local_group = cpumask_test_cpu(this_cpu,
3198 sched_group_cpus(group));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003200 if (local_group)
Rusty Russell758b2cd2008-11-25 02:35:04 +10303201 balance_cpu = cpumask_first(sched_group_cpus(group));
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07003204 sum_weighted_load = sum_nr_running = avg_load = 0;
Peter Zijlstra408ed062008-06-27 13:41:28 +02003205 sum_avg_load_per_task = avg_load_per_task = 0;
3206
Ken Chen908a7c12007-10-17 16:55:11 +02003207 max_cpu_load = 0;
3208 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
Rusty Russell758b2cd2008-11-25 02:35:04 +10303210 for_each_cpu_and(i, sched_group_cpus(group), cpus) {
3211 struct rq *rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07003212
Suresh Siddha9439aab2007-07-19 21:28:35 +02003213 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07003214 *sd_idle = 0;
3215
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003217 if (local_group) {
3218 if (idle_cpu(i) && !first_idle_cpu) {
3219 first_idle_cpu = 1;
3220 balance_cpu = i;
3221 }
3222
Nick Piggina2000572006-02-10 01:51:02 -08003223 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003224 } else {
Nick Piggina2000572006-02-10 01:51:02 -08003225 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003226 if (load > max_cpu_load)
3227 max_cpu_load = load;
3228 if (min_cpu_load > load)
3229 min_cpu_load = load;
3230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
3232 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07003233 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003234 sum_weighted_load += weighted_cpuload(i);
Peter Zijlstra408ed062008-06-27 13:41:28 +02003235
3236 sum_avg_load_per_task += cpu_avg_load_per_task(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 }
3238
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003239 /*
3240 * First idle cpu or the first cpu(busiest) in this sched group
3241 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02003242 * domains. In the newly idle case, we will allow all the cpu's
3243 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003244 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02003245 if (idle != CPU_NEWLY_IDLE && local_group &&
3246 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003247 *balance = 0;
3248 goto ret;
3249 }
3250
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07003252 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
3254 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07003255 avg_load = sg_div_cpu_power(group,
3256 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
Peter Zijlstra408ed062008-06-27 13:41:28 +02003258
3259 /*
3260 * Consider the group unbalanced when the imbalance is larger
3261 * than the average weight of two tasks.
3262 *
3263 * APZ: with cgroup the avg task weight can vary wildly and
3264 * might not be a suitable number - should we keep a
3265 * normalized nr_running number somewhere that negates
3266 * the hierarchy?
3267 */
3268 avg_load_per_task = sg_div_cpu_power(group,
3269 sum_avg_load_per_task * SCHED_LOAD_SCALE);
3270
3271 if ((max_cpu_load - min_cpu_load) > 2*avg_load_per_task)
Ken Chen908a7c12007-10-17 16:55:11 +02003272 __group_imb = 1;
3273
Eric Dumazet5517d862007-05-08 00:32:57 -07003274 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 if (local_group) {
3277 this_load = avg_load;
3278 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003279 this_nr_running = sum_nr_running;
3280 this_load_per_task = sum_weighted_load;
3281 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02003282 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 max_load = avg_load;
3284 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003285 busiest_nr_running = sum_nr_running;
3286 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02003287 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003289
3290#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3291 /*
3292 * Busy processors will not participate in power savings
3293 * balance.
3294 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003295 if (idle == CPU_NOT_IDLE ||
3296 !(sd->flags & SD_POWERSAVINGS_BALANCE))
3297 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003298
3299 /*
3300 * If the local group is idle or completely loaded
3301 * no need to do power savings balance at this domain
3302 */
3303 if (local_group && (this_nr_running >= group_capacity ||
3304 !this_nr_running))
3305 power_savings_balance = 0;
3306
Ingo Molnardd41f592007-07-09 18:51:59 +02003307 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003308 * If a group is already running at full capacity or idle,
3309 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02003310 */
3311 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003312 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02003313 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003314
Ingo Molnardd41f592007-07-09 18:51:59 +02003315 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003316 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003317 * This is the group from where we need to pick up the load
3318 * for saving power
3319 */
3320 if ((sum_nr_running < min_nr_running) ||
3321 (sum_nr_running == min_nr_running &&
Vaidyanathan Srinivasand5679bd2008-12-18 23:26:16 +05303322 cpumask_first(sched_group_cpus(group)) >
Rusty Russell758b2cd2008-11-25 02:35:04 +10303323 cpumask_first(sched_group_cpus(group_min)))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003324 group_min = group;
3325 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003326 min_load_per_task = sum_weighted_load /
3327 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003328 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003329
Ingo Molnardd41f592007-07-09 18:51:59 +02003330 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003331 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02003332 * capacity but still has some space to pick up some load
3333 * from other group and save more power
3334 */
3335 if (sum_nr_running <= group_capacity - 1) {
3336 if (sum_nr_running > leader_nr_running ||
3337 (sum_nr_running == leader_nr_running &&
Vaidyanathan Srinivasand5679bd2008-12-18 23:26:16 +05303338 cpumask_first(sched_group_cpus(group)) <
Rusty Russell758b2cd2008-11-25 02:35:04 +10303339 cpumask_first(sched_group_cpus(group_leader)))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003340 group_leader = group;
3341 leader_nr_running = sum_nr_running;
3342 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003343 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003344group_next:
3345#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 group = group->next;
3347 } while (group != sd->groups);
3348
Peter Williams2dd73a42006-06-27 02:54:34 -07003349 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 goto out_balanced;
3351
3352 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
3353
3354 if (this_load >= avg_load ||
3355 100*max_load <= sd->imbalance_pct*this_load)
3356 goto out_balanced;
3357
Peter Williams2dd73a42006-06-27 02:54:34 -07003358 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003359 if (group_imb)
3360 busiest_load_per_task = min(busiest_load_per_task, avg_load);
3361
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 /*
3363 * We're trying to get all the cpus to the average_load, so we don't
3364 * want to push ourselves above the average load, nor do we wish to
3365 * reduce the max loaded cpu below the average load, as either of these
3366 * actions would just result in more rebalancing later, and ping-pong
3367 * tasks around. Thus we look for the minimum possible imbalance.
3368 * Negative imbalances (*we* are more loaded than anyone else) will
3369 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003370 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 * appear as very large values with unsigned longs.
3372 */
Peter Williams2dd73a42006-06-27 02:54:34 -07003373 if (max_load <= busiest_load_per_task)
3374 goto out_balanced;
3375
3376 /*
3377 * In the presence of smp nice balancing, certain scenarios can have
3378 * max load less than avg load(as we skip the groups at or below
3379 * its cpu_power, while calculating max_load..)
3380 */
3381 if (max_load < avg_load) {
3382 *imbalance = 0;
3383 goto small_imbalance;
3384 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003385
3386 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07003387 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003388
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07003390 *imbalance = min(max_pull * busiest->__cpu_power,
3391 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 / SCHED_LOAD_SCALE;
3393
Peter Williams2dd73a42006-06-27 02:54:34 -07003394 /*
3395 * if *imbalance is less than the average load per runnable task
3396 * there is no gaurantee that any tasks will be moved so we'll have
3397 * a think about bumping its value to force at least one task to be
3398 * moved
3399 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003400 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003401 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07003402 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
Peter Williams2dd73a42006-06-27 02:54:34 -07003404small_imbalance:
3405 pwr_move = pwr_now = 0;
3406 imbn = 2;
3407 if (this_nr_running) {
3408 this_load_per_task /= this_nr_running;
3409 if (busiest_load_per_task > this_load_per_task)
3410 imbn = 1;
3411 } else
Peter Zijlstra408ed062008-06-27 13:41:28 +02003412 this_load_per_task = cpu_avg_load_per_task(this_cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07003413
Peter Zijlstra01c8c572008-10-24 11:06:12 +02003414 if (max_load - this_load + busiest_load_per_task >=
Ingo Molnardd41f592007-07-09 18:51:59 +02003415 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07003416 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 return busiest;
3418 }
3419
3420 /*
3421 * OK, we don't have enough imbalance to justify moving tasks,
3422 * however we may be able to increase total CPU power used by
3423 * moving them.
3424 */
3425
Eric Dumazet5517d862007-05-08 00:32:57 -07003426 pwr_now += busiest->__cpu_power *
3427 min(busiest_load_per_task, max_load);
3428 pwr_now += this->__cpu_power *
3429 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 pwr_now /= SCHED_LOAD_SCALE;
3431
3432 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07003433 tmp = sg_div_cpu_power(busiest,
3434 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07003436 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07003437 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
3439 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07003440 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003441 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07003442 tmp = sg_div_cpu_power(this,
3443 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 else
Eric Dumazet5517d862007-05-08 00:32:57 -07003445 tmp = sg_div_cpu_power(this,
3446 busiest_load_per_task * SCHED_LOAD_SCALE);
3447 pwr_move += this->__cpu_power *
3448 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 pwr_move /= SCHED_LOAD_SCALE;
3450
3451 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003452 if (pwr_move > pwr_now)
3453 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 }
3455
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 return busiest;
3457
3458out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003459#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003460 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003461 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003463 if (this == group_leader && group_leader != group_min) {
3464 *imbalance = min_load_per_task;
Vaidyanathan Srinivasan7a09b1a2008-12-18 23:26:22 +05303465 if (sched_mc_power_savings >= POWERSAVINGS_BALANCE_WAKEUP) {
3466 cpu_rq(this_cpu)->rd->sched_mc_preferred_wakeup_cpu =
Ingo Molnar9924da42008-12-19 00:53:40 +01003467 cpumask_first(sched_group_cpus(group_leader));
Vaidyanathan Srinivasan7a09b1a2008-12-18 23:26:22 +05303468 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003469 return group_min;
3470 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003471#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003472ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 *imbalance = 0;
3474 return NULL;
3475}
3476
3477/*
3478 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3479 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003480static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003481find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Rusty Russell96f874e2008-11-25 02:35:14 +10303482 unsigned long imbalance, const struct cpumask *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003484 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003485 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 int i;
3487
Rusty Russell758b2cd2008-11-25 02:35:04 +10303488 for_each_cpu(i, sched_group_cpus(group)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003489 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003490
Rusty Russell96f874e2008-11-25 02:35:14 +10303491 if (!cpumask_test_cpu(i, cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003492 continue;
3493
Ingo Molnar48f24c42006-07-03 00:25:40 -07003494 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003495 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
Ingo Molnardd41f592007-07-09 18:51:59 +02003497 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003498 continue;
3499
Ingo Molnardd41f592007-07-09 18:51:59 +02003500 if (wl > max_load) {
3501 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003502 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 }
3504 }
3505
3506 return busiest;
3507}
3508
3509/*
Nick Piggin77391d72005-06-25 14:57:30 -07003510 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3511 * so long as it is large enough.
3512 */
3513#define MAX_PINNED_INTERVAL 512
3514
3515/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3517 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003519static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003520 struct sched_domain *sd, enum cpu_idle_type idle,
Rusty Russell96f874e2008-11-25 02:35:14 +10303521 int *balance, struct cpumask *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522{
Peter Williams43010652007-08-09 11:16:46 +02003523 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003526 struct rq *busiest;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003527 unsigned long flags;
Nick Piggin5969fe02005-09-10 00:26:19 -07003528
Rusty Russell96f874e2008-11-25 02:35:14 +10303529 cpumask_setall(cpus);
Mike Travis7c16ec52008-04-04 18:11:11 -07003530
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003531 /*
3532 * When power savings policy is enabled for the parent domain, idle
3533 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003534 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003535 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003536 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003537 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003538 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003539 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
Ingo Molnar2d723762007-10-15 17:00:12 +02003541 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003543redo:
Peter Zijlstrac8cba852008-06-27 13:41:23 +02003544 update_shares(sd);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003545 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003546 cpus, balance);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003547
Chen, Kenneth W06066712006-12-10 02:20:35 -08003548 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003549 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003550
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 if (!group) {
3552 schedstat_inc(sd, lb_nobusyg[idle]);
3553 goto out_balanced;
3554 }
3555
Mike Travis7c16ec52008-04-04 18:11:11 -07003556 busiest = find_busiest_queue(group, idle, imbalance, cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 if (!busiest) {
3558 schedstat_inc(sd, lb_nobusyq[idle]);
3559 goto out_balanced;
3560 }
3561
Nick Piggindb935db2005-06-25 14:57:11 -07003562 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
3564 schedstat_add(sd, lb_imbalance[idle], imbalance);
3565
Peter Williams43010652007-08-09 11:16:46 +02003566 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 if (busiest->nr_running > 1) {
3568 /*
3569 * Attempt to move tasks. If find_busiest_group has found
3570 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003571 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 * correctly treated as an imbalance.
3573 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003574 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003575 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003576 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003577 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003578 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003579 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003580
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003581 /*
3582 * some other cpu did the load balance for us.
3583 */
Peter Williams43010652007-08-09 11:16:46 +02003584 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003585 resched_cpu(this_cpu);
3586
Nick Piggin81026792005-06-25 14:57:07 -07003587 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003588 if (unlikely(all_pinned)) {
Rusty Russell96f874e2008-11-25 02:35:14 +10303589 cpumask_clear_cpu(cpu_of(busiest), cpus);
3590 if (!cpumask_empty(cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003591 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003592 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 }
Nick Piggin81026792005-06-25 14:57:07 -07003595
Peter Williams43010652007-08-09 11:16:46 +02003596 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 schedstat_inc(sd, lb_failed[idle]);
3598 sd->nr_balance_failed++;
3599
3600 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003602 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003603
3604 /* don't kick the migration_thread, if the curr
3605 * task on busiest cpu can't be moved to this_cpu
3606 */
Rusty Russell96f874e2008-11-25 02:35:14 +10303607 if (!cpumask_test_cpu(this_cpu,
3608 &busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003609 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003610 all_pinned = 1;
3611 goto out_one_pinned;
3612 }
3613
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 if (!busiest->active_balance) {
3615 busiest->active_balance = 1;
3616 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003617 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003619 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003620 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 wake_up_process(busiest->migration_thread);
3622
3623 /*
3624 * We've kicked active balancing, reset the failure
3625 * counter.
3626 */
Nick Piggin39507452005-06-25 14:57:09 -07003627 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 }
Nick Piggin81026792005-06-25 14:57:07 -07003629 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 sd->nr_balance_failed = 0;
3631
Nick Piggin81026792005-06-25 14:57:07 -07003632 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 /* We were unbalanced, so reset the balancing interval */
3634 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003635 } else {
3636 /*
3637 * If we've begun active balancing, start to back off. This
3638 * case may not be covered by the all_pinned logic if there
3639 * is only 1 task on the busy runqueue (because we don't call
3640 * move_tasks).
3641 */
3642 if (sd->balance_interval < sd->max_interval)
3643 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 }
3645
Peter Williams43010652007-08-09 11:16:46 +02003646 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003647 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstrac09595f2008-06-27 13:41:14 +02003648 ld_moved = -1;
3649
3650 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651
3652out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 schedstat_inc(sd, lb_balanced[idle]);
3654
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003655 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003656
3657out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003659 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3660 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 sd->balance_interval *= 2;
3662
Ingo Molnar48f24c42006-07-03 00:25:40 -07003663 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003664 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstrac09595f2008-06-27 13:41:14 +02003665 ld_moved = -1;
3666 else
3667 ld_moved = 0;
3668out:
Peter Zijlstrac8cba852008-06-27 13:41:23 +02003669 if (ld_moved)
3670 update_shares(sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02003671 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672}
3673
3674/*
3675 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3676 * tasks if there is an imbalance.
3677 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003678 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 * this_rq is locked.
3680 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003681static int
Mike Travis7c16ec52008-04-04 18:11:11 -07003682load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
Rusty Russell96f874e2008-11-25 02:35:14 +10303683 struct cpumask *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684{
3685 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003686 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003688 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003689 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003690 int all_pinned = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07003691
Rusty Russell96f874e2008-11-25 02:35:14 +10303692 cpumask_setall(cpus);
Nick Piggin5969fe02005-09-10 00:26:19 -07003693
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003694 /*
3695 * When power savings policy is enabled for the parent domain, idle
3696 * sibling can pick up load irrespective of busy siblings. In this case,
3697 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003698 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003699 */
3700 if (sd->flags & SD_SHARE_CPUPOWER &&
3701 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003702 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703
Ingo Molnar2d723762007-10-15 17:00:12 +02003704 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003705redo:
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02003706 update_shares_locked(this_rq, sd);
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003707 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Mike Travis7c16ec52008-04-04 18:11:11 -07003708 &sd_idle, cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003710 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003711 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 }
3713
Mike Travis7c16ec52008-04-04 18:11:11 -07003714 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003715 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003716 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003717 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 }
3719
Nick Piggindb935db2005-06-25 14:57:11 -07003720 BUG_ON(busiest == this_rq);
3721
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003722 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003723
Peter Williams43010652007-08-09 11:16:46 +02003724 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003725 if (busiest->nr_running > 1) {
3726 /* Attempt to move tasks */
3727 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003728 /* this_rq->clock is already updated */
3729 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003730 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003731 imbalance, sd, CPU_NEWLY_IDLE,
3732 &all_pinned);
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02003733 double_unlock_balance(this_rq, busiest);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003734
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003735 if (unlikely(all_pinned)) {
Rusty Russell96f874e2008-11-25 02:35:14 +10303736 cpumask_clear_cpu(cpu_of(busiest), cpus);
3737 if (!cpumask_empty(cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003738 goto redo;
3739 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003740 }
3741
Peter Williams43010652007-08-09 11:16:46 +02003742 if (!ld_moved) {
Vaidyanathan Srinivasan36dffab2008-12-20 10:06:38 +05303743 int active_balance = 0;
Vaidyanathan Srinivasanad273b32008-12-18 23:26:36 +05303744
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003745 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003746 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3747 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003748 return -1;
Vaidyanathan Srinivasanad273b32008-12-18 23:26:36 +05303749
3750 if (sched_mc_power_savings < POWERSAVINGS_BALANCE_WAKEUP)
3751 return -1;
3752
3753 if (sd->nr_balance_failed++ < 2)
3754 return -1;
3755
3756 /*
3757 * The only task running in a non-idle cpu can be moved to this
3758 * cpu in an attempt to completely freeup the other CPU
3759 * package. The same method used to move task in load_balance()
3760 * have been extended for load_balance_newidle() to speedup
3761 * consolidation at sched_mc=POWERSAVINGS_BALANCE_WAKEUP (2)
3762 *
3763 * The package power saving logic comes from
3764 * find_busiest_group(). If there are no imbalance, then
3765 * f_b_g() will return NULL. However when sched_mc={1,2} then
3766 * f_b_g() will select a group from which a running task may be
3767 * pulled to this cpu in order to make the other package idle.
3768 * If there is no opportunity to make a package idle and if
3769 * there are no imbalance, then f_b_g() will return NULL and no
3770 * action will be taken in load_balance_newidle().
3771 *
3772 * Under normal task pull operation due to imbalance, there
3773 * will be more than one task in the source run queue and
3774 * move_tasks() will succeed. ld_moved will be true and this
3775 * active balance code will not be triggered.
3776 */
3777
3778 /* Lock busiest in correct order while this_rq is held */
3779 double_lock_balance(this_rq, busiest);
3780
3781 /*
3782 * don't kick the migration_thread, if the curr
3783 * task on busiest cpu can't be moved to this_cpu
3784 */
Mike Travis6ca09df2008-12-31 18:08:45 -08003785 if (!cpumask_test_cpu(this_cpu, &busiest->curr->cpus_allowed)) {
Vaidyanathan Srinivasanad273b32008-12-18 23:26:36 +05303786 double_unlock_balance(this_rq, busiest);
3787 all_pinned = 1;
3788 return ld_moved;
3789 }
3790
3791 if (!busiest->active_balance) {
3792 busiest->active_balance = 1;
3793 busiest->push_cpu = this_cpu;
3794 active_balance = 1;
3795 }
3796
3797 double_unlock_balance(this_rq, busiest);
Peter Zijlstrada8d5082009-01-07 15:28:57 +01003798 /*
3799 * Should not call ttwu while holding a rq->lock
3800 */
3801 spin_unlock(&this_rq->lock);
Vaidyanathan Srinivasanad273b32008-12-18 23:26:36 +05303802 if (active_balance)
3803 wake_up_process(busiest->migration_thread);
Peter Zijlstrada8d5082009-01-07 15:28:57 +01003804 spin_lock(&this_rq->lock);
Vaidyanathan Srinivasanad273b32008-12-18 23:26:36 +05303805
Nick Piggin5969fe02005-09-10 00:26:19 -07003806 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003807 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02003809 update_shares_locked(this_rq, sd);
Peter Williams43010652007-08-09 11:16:46 +02003810 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003811
3812out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003813 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003814 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003815 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003816 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003817 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003818
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003819 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820}
3821
3822/*
3823 * idle_balance is called by schedule() if this_cpu is about to become
3824 * idle. Attempts to pull tasks from other CPUs.
3825 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003826static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827{
3828 struct sched_domain *sd;
Vaidyanathan Srinivasanefbe0272008-12-08 20:52:49 +05303829 int pulled_task = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02003830 unsigned long next_balance = jiffies + HZ;
Rusty Russell4d2732c2008-11-25 02:35:10 +10303831 cpumask_var_t tmpmask;
3832
3833 if (!alloc_cpumask_var(&tmpmask, GFP_ATOMIC))
3834 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
3836 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003837 unsigned long interval;
3838
3839 if (!(sd->flags & SD_LOAD_BALANCE))
3840 continue;
3841
3842 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003843 /* If we've pulled tasks over stop searching: */
Mike Travis7c16ec52008-04-04 18:11:11 -07003844 pulled_task = load_balance_newidle(this_cpu, this_rq,
Rusty Russell4d2732c2008-11-25 02:35:10 +10303845 sd, tmpmask);
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003846
3847 interval = msecs_to_jiffies(sd->balance_interval);
3848 if (time_after(next_balance, sd->last_balance + interval))
3849 next_balance = sd->last_balance + interval;
3850 if (pulled_task)
3851 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003853 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003854 /*
3855 * We are going idle. next_balance may be set based on
3856 * a busy processor. So reset next_balance.
3857 */
3858 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003859 }
Rusty Russell4d2732c2008-11-25 02:35:10 +10303860 free_cpumask_var(tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861}
3862
3863/*
3864 * active_load_balance is run by migration threads. It pushes running tasks
3865 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3866 * running on each physical CPU where possible, and avoids physical /
3867 * logical imbalances.
3868 *
3869 * Called with busiest_rq locked.
3870 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003871static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872{
Nick Piggin39507452005-06-25 14:57:09 -07003873 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003874 struct sched_domain *sd;
3875 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003876
Ingo Molnar48f24c42006-07-03 00:25:40 -07003877 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003878 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003879 return;
3880
3881 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
3883 /*
Nick Piggin39507452005-06-25 14:57:09 -07003884 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003885 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003886 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 */
Nick Piggin39507452005-06-25 14:57:09 -07003888 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
Nick Piggin39507452005-06-25 14:57:09 -07003890 /* move a task from busiest_rq to target_rq */
3891 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003892 update_rq_clock(busiest_rq);
3893 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894
Nick Piggin39507452005-06-25 14:57:09 -07003895 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003896 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07003897 if ((sd->flags & SD_LOAD_BALANCE) &&
Rusty Russell758b2cd2008-11-25 02:35:04 +10303898 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
Nick Piggin39507452005-06-25 14:57:09 -07003899 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901
Ingo Molnar48f24c42006-07-03 00:25:40 -07003902 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003903 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
Peter Williams43010652007-08-09 11:16:46 +02003905 if (move_one_task(target_rq, target_cpu, busiest_rq,
3906 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07003907 schedstat_inc(sd, alb_pushed);
3908 else
3909 schedstat_inc(sd, alb_failed);
3910 }
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02003911 double_unlock_balance(busiest_rq, target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912}
3913
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003914#ifdef CONFIG_NO_HZ
3915static struct {
3916 atomic_t load_balancer;
Rusty Russell7d1e6a92008-11-25 02:35:09 +10303917 cpumask_var_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003918} nohz ____cacheline_aligned = {
3919 .load_balancer = ATOMIC_INIT(-1),
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003920};
3921
Christoph Lameter7835b982006-12-10 02:20:22 -08003922/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003923 * This routine will try to nominate the ilb (idle load balancing)
3924 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3925 * load balancing on behalf of all those cpus. If all the cpus in the system
3926 * go into this tickless mode, then there will be no ilb owner (as there is
3927 * no need for one) and all the cpus will sleep till the next wakeup event
3928 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08003929 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003930 * For the ilb owner, tick is not stopped. And this tick will be used
3931 * for idle load balancing. ilb owner will still be part of
3932 * nohz.cpu_mask..
3933 *
3934 * While stopping the tick, this cpu will become the ilb owner if there
3935 * is no other owner. And will be the owner till that cpu becomes busy
3936 * or if all cpus in the system stop their ticks at which point
3937 * there is no need for ilb owner.
3938 *
3939 * When the ilb owner becomes busy, it nominates another owner, during the
3940 * next busy scheduler_tick()
3941 */
3942int select_nohz_load_balancer(int stop_tick)
3943{
3944 int cpu = smp_processor_id();
3945
3946 if (stop_tick) {
Rusty Russell7d1e6a92008-11-25 02:35:09 +10303947 cpumask_set_cpu(cpu, nohz.cpu_mask);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003948 cpu_rq(cpu)->in_nohz_recently = 1;
3949
3950 /*
3951 * If we are going offline and still the leader, give up!
3952 */
Max Krasnyanskye761b772008-07-15 04:43:49 -07003953 if (!cpu_active(cpu) &&
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003954 atomic_read(&nohz.load_balancer) == cpu) {
3955 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3956 BUG();
3957 return 0;
3958 }
3959
3960 /* time for ilb owner also to sleep */
Rusty Russell7d1e6a92008-11-25 02:35:09 +10303961 if (cpumask_weight(nohz.cpu_mask) == num_online_cpus()) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003962 if (atomic_read(&nohz.load_balancer) == cpu)
3963 atomic_set(&nohz.load_balancer, -1);
3964 return 0;
3965 }
3966
3967 if (atomic_read(&nohz.load_balancer) == -1) {
3968 /* make me the ilb owner */
3969 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
3970 return 1;
3971 } else if (atomic_read(&nohz.load_balancer) == cpu)
3972 return 1;
3973 } else {
Rusty Russell7d1e6a92008-11-25 02:35:09 +10303974 if (!cpumask_test_cpu(cpu, nohz.cpu_mask))
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003975 return 0;
3976
Rusty Russell7d1e6a92008-11-25 02:35:09 +10303977 cpumask_clear_cpu(cpu, nohz.cpu_mask);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003978
3979 if (atomic_read(&nohz.load_balancer) == cpu)
3980 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3981 BUG();
3982 }
3983 return 0;
3984}
3985#endif
3986
3987static DEFINE_SPINLOCK(balancing);
3988
3989/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003990 * It checks each scheduling domain to see if it is due to be balanced,
3991 * and initiates a balancing operation if so.
3992 *
3993 * Balancing parameters are set up in arch_init_sched_domains.
3994 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003995static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08003996{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003997 int balance = 1;
3998 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08003999 unsigned long interval;
4000 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004001 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08004002 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004003 int update_next_balance = 0;
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02004004 int need_serialize;
Rusty Russella0e90242008-11-25 02:35:11 +10304005 cpumask_var_t tmp;
4006
4007 /* Fails alloc? Rebalancing probably not a priority right now. */
4008 if (!alloc_cpumask_var(&tmp, GFP_ATOMIC))
4009 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004011 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 if (!(sd->flags & SD_LOAD_BALANCE))
4013 continue;
4014
4015 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004016 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 interval *= sd->busy_factor;
4018
4019 /* scale ms to jiffies */
4020 interval = msecs_to_jiffies(interval);
4021 if (unlikely(!interval))
4022 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02004023 if (interval > HZ*NR_CPUS/10)
4024 interval = HZ*NR_CPUS/10;
4025
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02004026 need_serialize = sd->flags & SD_SERIALIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02004028 if (need_serialize) {
Christoph Lameter08c183f2006-12-10 02:20:29 -08004029 if (!spin_trylock(&balancing))
4030 goto out;
4031 }
4032
Christoph Lameterc9819f42006-12-10 02:20:25 -08004033 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Rusty Russella0e90242008-11-25 02:35:11 +10304034 if (load_balance(cpu, rq, sd, idle, &balance, tmp)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07004035 /*
4036 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07004037 * longer idle, or one of our SMT siblings is
4038 * not idle.
4039 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02004040 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08004042 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 }
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02004044 if (need_serialize)
Christoph Lameter08c183f2006-12-10 02:20:29 -08004045 spin_unlock(&balancing);
4046out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02004047 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08004048 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02004049 update_next_balance = 1;
4050 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08004051
4052 /*
4053 * Stop the load balance at this level. There is another
4054 * CPU in our sched group which is doing load balancing more
4055 * actively.
4056 */
4057 if (!balance)
4058 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02004060
4061 /*
4062 * next_balance will be updated only when there is a need.
4063 * When the cpu is attached to null domain for ex, it will not be
4064 * updated.
4065 */
4066 if (likely(update_next_balance))
4067 rq->next_balance = next_balance;
Rusty Russella0e90242008-11-25 02:35:11 +10304068
4069 free_cpumask_var(tmp);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004070}
4071
4072/*
4073 * run_rebalance_domains is triggered when needed from the scheduler tick.
4074 * In CONFIG_NO_HZ case, the idle load balance owner will do the
4075 * rebalancing for all the cpus for whom scheduler ticks are stopped.
4076 */
4077static void run_rebalance_domains(struct softirq_action *h)
4078{
Ingo Molnardd41f592007-07-09 18:51:59 +02004079 int this_cpu = smp_processor_id();
4080 struct rq *this_rq = cpu_rq(this_cpu);
4081 enum cpu_idle_type idle = this_rq->idle_at_tick ?
4082 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004083
Ingo Molnardd41f592007-07-09 18:51:59 +02004084 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004085
4086#ifdef CONFIG_NO_HZ
4087 /*
4088 * If this cpu is the owner for idle load balancing, then do the
4089 * balancing on behalf of the other idle cpus whose ticks are
4090 * stopped.
4091 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004092 if (this_rq->idle_at_tick &&
4093 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004094 struct rq *rq;
4095 int balance_cpu;
4096
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304097 for_each_cpu(balance_cpu, nohz.cpu_mask) {
4098 if (balance_cpu == this_cpu)
4099 continue;
4100
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004101 /*
4102 * If this cpu gets work to do, stop the load balancing
4103 * work being done for other cpus. Next load
4104 * balancing owner will pick it up.
4105 */
4106 if (need_resched())
4107 break;
4108
Oleg Nesterovde0cf892007-08-12 18:08:19 +02004109 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004110
4111 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004112 if (time_after(this_rq->next_balance, rq->next_balance))
4113 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004114 }
4115 }
4116#endif
4117}
4118
4119/*
4120 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
4121 *
4122 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
4123 * idle load balancing owner or decide to stop the periodic load balancing,
4124 * if the whole system is idle.
4125 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004126static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004127{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004128#ifdef CONFIG_NO_HZ
4129 /*
4130 * If we were in the nohz mode recently and busy at the current
4131 * scheduler tick, then check if we need to nominate new idle
4132 * load balancer.
4133 */
4134 if (rq->in_nohz_recently && !rq->idle_at_tick) {
4135 rq->in_nohz_recently = 0;
4136
4137 if (atomic_read(&nohz.load_balancer) == cpu) {
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304138 cpumask_clear_cpu(cpu, nohz.cpu_mask);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004139 atomic_set(&nohz.load_balancer, -1);
4140 }
4141
4142 if (atomic_read(&nohz.load_balancer) == -1) {
4143 /*
4144 * simple selection for now: Nominate the
4145 * first cpu in the nohz list to be the next
4146 * ilb owner.
4147 *
4148 * TBD: Traverse the sched domains and nominate
4149 * the nearest cpu in the nohz.cpu_mask.
4150 */
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304151 int ilb = cpumask_first(nohz.cpu_mask);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004152
Mike Travis434d53b2008-04-04 18:11:04 -07004153 if (ilb < nr_cpu_ids)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004154 resched_cpu(ilb);
4155 }
4156 }
4157
4158 /*
4159 * If this cpu is idle and doing idle load balancing for all the
4160 * cpus with ticks stopped, is it time for that to stop?
4161 */
4162 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304163 cpumask_weight(nohz.cpu_mask) == num_online_cpus()) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004164 resched_cpu(cpu);
4165 return;
4166 }
4167
4168 /*
4169 * If this cpu is idle and the idle load balancing is done by
4170 * someone else, then no need raise the SCHED_SOFTIRQ
4171 */
4172 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304173 cpumask_test_cpu(cpu, nohz.cpu_mask))
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004174 return;
4175#endif
4176 if (time_after_eq(jiffies, rq->next_balance))
4177 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178}
Ingo Molnardd41f592007-07-09 18:51:59 +02004179
4180#else /* CONFIG_SMP */
4181
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182/*
4183 * on UP we do not need to balance between CPUs:
4184 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004185static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186{
4187}
Ingo Molnardd41f592007-07-09 18:51:59 +02004188
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189#endif
4190
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191DEFINE_PER_CPU(struct kernel_stat, kstat);
4192
4193EXPORT_PER_CPU_SYMBOL(kstat);
4194
4195/*
Frank Mayharf06febc2008-09-12 09:54:39 -07004196 * Return any ns on the sched_clock that have not yet been banked in
4197 * @p in case that task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 */
Frank Mayharbb34d922008-09-12 09:54:39 -07004199unsigned long long task_delta_exec(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004202 struct rq *rq;
Frank Mayharbb34d922008-09-12 09:54:39 -07004203 u64 ns = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004204
Ingo Molnar41b86e92007-07-09 18:51:58 +02004205 rq = task_rq_lock(p, &flags);
Ingo Molnar15084872008-09-30 08:28:17 +02004206
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004207 if (task_current(rq, p)) {
Frank Mayharf06febc2008-09-12 09:54:39 -07004208 u64 delta_exec;
4209
Ingo Molnara8e504d2007-08-09 11:16:47 +02004210 update_rq_clock(rq);
4211 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004212 if ((s64)delta_exec > 0)
Frank Mayharbb34d922008-09-12 09:54:39 -07004213 ns = delta_exec;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004214 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07004215
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 task_rq_unlock(rq, &flags);
4217
4218 return ns;
4219}
4220
4221/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 * Account user cpu time to a process.
4223 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 * @cputime: the cpu time spent in user space since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004225 * @cputime_scaled: cputime scaled by cpu frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 */
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004227void account_user_time(struct task_struct *p, cputime_t cputime,
4228 cputime_t cputime_scaled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229{
4230 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4231 cputime64_t tmp;
4232
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004233 /* Add user time to process. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 p->utime = cputime_add(p->utime, cputime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004235 p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
Frank Mayharf06febc2008-09-12 09:54:39 -07004236 account_group_user_time(p, cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
4238 /* Add user time to cpustat. */
4239 tmp = cputime_to_cputime64(cputime);
4240 if (TASK_NICE(p) > 0)
4241 cpustat->nice = cputime64_add(cpustat->nice, tmp);
4242 else
4243 cpustat->user = cputime64_add(cpustat->user, tmp);
Jonathan Lim49b5cf32008-07-25 01:48:40 -07004244 /* Account for user time used */
4245 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246}
4247
4248/*
Laurent Vivier94886b82007-10-15 17:00:19 +02004249 * Account guest cpu time to a process.
4250 * @p: the process that the cpu time gets accounted to
4251 * @cputime: the cpu time spent in virtual machine since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004252 * @cputime_scaled: cputime scaled by cpu frequency
Laurent Vivier94886b82007-10-15 17:00:19 +02004253 */
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004254static void account_guest_time(struct task_struct *p, cputime_t cputime,
4255 cputime_t cputime_scaled)
Laurent Vivier94886b82007-10-15 17:00:19 +02004256{
4257 cputime64_t tmp;
4258 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4259
4260 tmp = cputime_to_cputime64(cputime);
4261
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004262 /* Add guest time to process. */
Laurent Vivier94886b82007-10-15 17:00:19 +02004263 p->utime = cputime_add(p->utime, cputime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004264 p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
Frank Mayharf06febc2008-09-12 09:54:39 -07004265 account_group_user_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02004266 p->gtime = cputime_add(p->gtime, cputime);
4267
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004268 /* Add guest time to cpustat. */
Laurent Vivier94886b82007-10-15 17:00:19 +02004269 cpustat->user = cputime64_add(cpustat->user, tmp);
4270 cpustat->guest = cputime64_add(cpustat->guest, tmp);
4271}
4272
4273/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 * Account system cpu time to a process.
4275 * @p: the process that the cpu time gets accounted to
4276 * @hardirq_offset: the offset to subtract from hardirq_count()
4277 * @cputime: the cpu time spent in kernel space since the last update
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004278 * @cputime_scaled: cputime scaled by cpu frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 */
4280void account_system_time(struct task_struct *p, int hardirq_offset,
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004281 cputime_t cputime, cputime_t cputime_scaled)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282{
4283 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 cputime64_t tmp;
4285
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004286 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004287 account_guest_time(p, cputime, cputime_scaled);
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004288 return;
4289 }
Laurent Vivier94886b82007-10-15 17:00:19 +02004290
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004291 /* Add system time to process. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 p->stime = cputime_add(p->stime, cputime);
Martin Schwidefsky457533a2008-12-31 15:11:37 +01004293 p->stimescaled = cputime_add(p->stimescaled, cputime_scaled);
Frank Mayharf06febc2008-09-12 09:54:39 -07004294 account_group_system_time(p, cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295
4296 /* Add system time to cpustat. */
4297 tmp = cputime_to_cputime64(cputime);
4298 if (hardirq_count() - hardirq_offset)
4299 cpustat->irq = cputime64_add(cpustat->irq, tmp);
4300 else if (softirq_count())
4301 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 else
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004303 cpustat->system = cputime64_add(cpustat->system, tmp);
4304
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 /* Account for system time used */
4306 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307}
4308
4309/*
4310 * Account for involuntary wait time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 * @steal: the cpu time spent in involuntary wait
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 */
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004313void account_steal_time(cputime_t cputime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004316 cputime64_t cputime64 = cputime_to_cputime64(cputime);
4317
4318 cpustat->steal = cputime64_add(cpustat->steal, cputime64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319}
4320
Christoph Lameter7835b982006-12-10 02:20:22 -08004321/*
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004322 * Account for idle time.
4323 * @cputime: the cpu time spent in idle wait
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 */
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004325void account_idle_time(cputime_t cputime)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326{
4327 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004328 cputime64_t cputime64 = cputime_to_cputime64(cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 struct rq *rq = this_rq();
4330
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004331 if (atomic_read(&rq->nr_iowait) > 0)
4332 cpustat->iowait = cputime64_add(cpustat->iowait, cputime64);
4333 else
4334 cpustat->idle = cputime64_add(cpustat->idle, cputime64);
Christoph Lameter7835b982006-12-10 02:20:22 -08004335}
4336
Martin Schwidefsky79741dd2008-12-31 15:11:38 +01004337#ifndef CONFIG_VIRT_CPU_ACCOUNTING
4338
4339/*
4340 * Account a single tick of cpu time.
4341 * @p: the process that the cpu time gets accounted to
4342 * @user_tick: indicates if the tick is a user or a system tick
4343 */
4344void account_process_tick(struct task_struct *p, int user_tick)
4345{
4346 cputime_t one_jiffy = jiffies_to_cputime(1);
4347 cputime_t one_jiffy_scaled = cputime_to_scaled(one_jiffy);
4348 struct rq *rq = this_rq();
4349
4350 if (user_tick)
4351 account_user_time(p, one_jiffy, one_jiffy_scaled);
4352 else if (p != rq->idle)
4353 account_system_time(p, HARDIRQ_OFFSET, one_jiffy,
4354 one_jiffy_scaled);
4355 else
4356 account_idle_time(one_jiffy);
4357}
4358
4359/*
4360 * Account multiple ticks of steal time.
4361 * @p: the process from which the cpu time has been stolen
4362 * @ticks: number of stolen ticks
4363 */
4364void account_steal_ticks(unsigned long ticks)
4365{
4366 account_steal_time(jiffies_to_cputime(ticks));
4367}
4368
4369/*
4370 * Account multiple ticks of idle time.
4371 * @ticks: number of stolen ticks
4372 */
4373void account_idle_ticks(unsigned long ticks)
4374{
4375 account_idle_time(jiffies_to_cputime(ticks));
4376}
4377
4378#endif
4379
Christoph Lameter7835b982006-12-10 02:20:22 -08004380/*
Balbir Singh49048622008-09-05 18:12:23 +02004381 * Use precise platform statistics if available:
4382 */
4383#ifdef CONFIG_VIRT_CPU_ACCOUNTING
4384cputime_t task_utime(struct task_struct *p)
4385{
4386 return p->utime;
4387}
4388
4389cputime_t task_stime(struct task_struct *p)
4390{
4391 return p->stime;
4392}
4393#else
4394cputime_t task_utime(struct task_struct *p)
4395{
4396 clock_t utime = cputime_to_clock_t(p->utime),
4397 total = utime + cputime_to_clock_t(p->stime);
4398 u64 temp;
4399
4400 /*
4401 * Use CFS's precise accounting:
4402 */
4403 temp = (u64)nsec_to_clock_t(p->se.sum_exec_runtime);
4404
4405 if (total) {
4406 temp *= utime;
4407 do_div(temp, total);
4408 }
4409 utime = (clock_t)temp;
4410
4411 p->prev_utime = max(p->prev_utime, clock_t_to_cputime(utime));
4412 return p->prev_utime;
4413}
4414
4415cputime_t task_stime(struct task_struct *p)
4416{
4417 clock_t stime;
4418
4419 /*
4420 * Use CFS's precise accounting. (we subtract utime from
4421 * the total, to make sure the total observed by userspace
4422 * grows monotonically - apps rely on that):
4423 */
4424 stime = nsec_to_clock_t(p->se.sum_exec_runtime) -
4425 cputime_to_clock_t(task_utime(p));
4426
4427 if (stime >= 0)
4428 p->prev_stime = max(p->prev_stime, clock_t_to_cputime(stime));
4429
4430 return p->prev_stime;
4431}
4432#endif
4433
4434inline cputime_t task_gtime(struct task_struct *p)
4435{
4436 return p->gtime;
4437}
4438
4439/*
Christoph Lameter7835b982006-12-10 02:20:22 -08004440 * This function gets called by the timer code, with HZ frequency.
4441 * We call it with interrupts disabled.
4442 *
4443 * It also gets called by the fork code, when changing the parent's
4444 * timeslices.
4445 */
4446void scheduler_tick(void)
4447{
Christoph Lameter7835b982006-12-10 02:20:22 -08004448 int cpu = smp_processor_id();
4449 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004450 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004451
4452 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08004453
Ingo Molnardd41f592007-07-09 18:51:59 +02004454 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004455 update_rq_clock(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02004456 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01004457 curr->sched_class->task_tick(rq, curr, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02004458 spin_unlock(&rq->lock);
4459
Christoph Lametere418e1c2006-12-10 02:20:23 -08004460#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02004461 rq->idle_at_tick = idle_cpu(cpu);
4462 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08004463#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464}
4465
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004466#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
4467 defined(CONFIG_PREEMPT_TRACER))
4468
4469static inline unsigned long get_parent_ip(unsigned long addr)
4470{
4471 if (in_lock_functions(addr)) {
4472 addr = CALLER_ADDR2;
4473 if (in_lock_functions(addr))
4474 addr = CALLER_ADDR3;
4475 }
4476 return addr;
4477}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
Srinivasa Ds43627582008-02-23 15:24:04 -08004479void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004481#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 /*
4483 * Underflow?
4484 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004485 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4486 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004487#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 preempt_count() += val;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004489#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 /*
4491 * Spinlock count overflowing soon?
4492 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08004493 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
4494 PREEMPT_MASK - 10);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004495#endif
4496 if (preempt_count() == val)
4497 trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498}
4499EXPORT_SYMBOL(add_preempt_count);
4500
Srinivasa Ds43627582008-02-23 15:24:04 -08004501void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004503#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 /*
4505 * Underflow?
4506 */
Nick Piggin7317d7b2008-09-30 20:50:27 +10004507 if (DEBUG_LOCKS_WARN_ON(val > preempt_count() - (!!kernel_locked())))
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004508 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 /*
4510 * Is the spinlock portion underflowing?
4511 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004512 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
4513 !(preempt_count() & PREEMPT_MASK)))
4514 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004515#endif
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004516
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004517 if (preempt_count() == val)
4518 trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 preempt_count() -= val;
4520}
4521EXPORT_SYMBOL(sub_preempt_count);
4522
4523#endif
4524
4525/*
Ingo Molnardd41f592007-07-09 18:51:59 +02004526 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004528static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529{
Satyam Sharma838225b2007-10-24 18:23:50 +02004530 struct pt_regs *regs = get_irq_regs();
4531
4532 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4533 prev->comm, prev->pid, preempt_count());
4534
Ingo Molnardd41f592007-07-09 18:51:59 +02004535 debug_show_held_locks(prev);
Arjan van de Vene21f5b12008-05-23 09:05:58 -07004536 print_modules();
Ingo Molnardd41f592007-07-09 18:51:59 +02004537 if (irqs_disabled())
4538 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02004539
4540 if (regs)
4541 show_regs(regs);
4542 else
4543 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02004544}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545
Ingo Molnardd41f592007-07-09 18:51:59 +02004546/*
4547 * Various schedule()-time debugging checks and statistics:
4548 */
4549static inline void schedule_debug(struct task_struct *prev)
4550{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004552 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 * schedule() atomically, we ignore that path for now.
4554 * Otherwise, whine if we are scheduling when we should not be.
4555 */
Roel Kluin3f33a7c2008-05-13 23:44:11 +02004556 if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
Ingo Molnardd41f592007-07-09 18:51:59 +02004557 __schedule_bug(prev);
4558
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4560
Ingo Molnar2d723762007-10-15 17:00:12 +02004561 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004562#ifdef CONFIG_SCHEDSTATS
4563 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004564 schedstat_inc(this_rq(), bkl_count);
4565 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004566 }
4567#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004568}
4569
4570/*
4571 * Pick up the highest-prio task:
4572 */
4573static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004574pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02004575{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004576 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004577 struct task_struct *p;
4578
4579 /*
4580 * Optimization: we know that if all tasks are in
4581 * the fair class we can call that function directly:
4582 */
4583 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004584 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004585 if (likely(p))
4586 return p;
4587 }
4588
4589 class = sched_class_highest;
4590 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004591 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004592 if (p)
4593 return p;
4594 /*
4595 * Will never be NULL as the idle class always
4596 * returns a non-NULL p:
4597 */
4598 class = class->next;
4599 }
4600}
4601
4602/*
4603 * schedule() is the main scheduler function.
4604 */
4605asmlinkage void __sched schedule(void)
4606{
4607 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004608 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02004609 struct rq *rq;
Peter Zijlstra31656512008-07-18 18:01:23 +02004610 int cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02004611
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612need_resched:
4613 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02004614 cpu = smp_processor_id();
4615 rq = cpu_rq(cpu);
4616 rcu_qsctr_inc(cpu);
4617 prev = rq->curr;
4618 switch_count = &prev->nivcsw;
4619
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 release_kernel_lock(prev);
4621need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622
Ingo Molnardd41f592007-07-09 18:51:59 +02004623 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624
Peter Zijlstra31656512008-07-18 18:01:23 +02004625 if (sched_feat(HRTICK))
Mike Galbraithf333fdc2008-05-12 21:20:55 +02004626 hrtick_clear(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004627
Peter Zijlstra8cd162c2008-10-15 20:37:23 +02004628 spin_lock_irq(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004629 update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02004630 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631
Ingo Molnardd41f592007-07-09 18:51:59 +02004632 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
Oleg Nesterov16882c12008-06-08 21:20:41 +04004633 if (unlikely(signal_pending_state(prev->state, prev)))
Ingo Molnardd41f592007-07-09 18:51:59 +02004634 prev->state = TASK_RUNNING;
Oleg Nesterov16882c12008-06-08 21:20:41 +04004635 else
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004636 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02004637 switch_count = &prev->nvcsw;
4638 }
4639
Steven Rostedt9a897c52008-01-25 21:08:22 +01004640#ifdef CONFIG_SMP
4641 if (prev->sched_class->pre_schedule)
4642 prev->sched_class->pre_schedule(rq, prev);
4643#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01004644
Ingo Molnardd41f592007-07-09 18:51:59 +02004645 if (unlikely(!rq->nr_running))
4646 idle_balance(cpu, rq);
4647
Ingo Molnar31ee5292007-08-09 11:16:49 +02004648 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004649 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 if (likely(prev != next)) {
David Simner673a90a2008-04-29 10:08:59 +01004652 sched_info_switch(prev, next);
4653
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 rq->nr_switches++;
4655 rq->curr = next;
4656 ++*switch_count;
4657
Ingo Molnardd41f592007-07-09 18:51:59 +02004658 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004659 /*
4660 * the context switch might have flipped the stack from under
4661 * us, hence refresh the local variables.
4662 */
4663 cpu = smp_processor_id();
4664 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 } else
4666 spin_unlock_irq(&rq->lock);
4667
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004668 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004670
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 preempt_enable_no_resched();
4672 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4673 goto need_resched;
4674}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675EXPORT_SYMBOL(schedule);
4676
4677#ifdef CONFIG_PREEMPT
4678/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004679 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004680 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 * occur there and call schedule directly.
4682 */
4683asmlinkage void __sched preempt_schedule(void)
4684{
4685 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004686
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 /*
4688 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004689 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004691 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 return;
4693
Andi Kleen3a5c3592007-10-15 17:00:14 +02004694 do {
4695 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004696 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004697 sub_preempt_count(PREEMPT_ACTIVE);
4698
4699 /*
4700 * Check again in case we missed a preemption opportunity
4701 * between schedule and now.
4702 */
4703 barrier();
4704 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706EXPORT_SYMBOL(preempt_schedule);
4707
4708/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004709 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 * off of irq context.
4711 * Note, that this is called and return with irqs disabled. This will
4712 * protect us against recursive calling from irq.
4713 */
4714asmlinkage void __sched preempt_schedule_irq(void)
4715{
4716 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004717
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004718 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 BUG_ON(ti->preempt_count || !irqs_disabled());
4720
Andi Kleen3a5c3592007-10-15 17:00:14 +02004721 do {
4722 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004723 local_irq_enable();
4724 schedule();
4725 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004726 sub_preempt_count(PREEMPT_ACTIVE);
4727
4728 /*
4729 * Check again in case we missed a preemption opportunity
4730 * between schedule and now.
4731 */
4732 barrier();
4733 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734}
4735
4736#endif /* CONFIG_PREEMPT */
4737
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004738int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4739 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004741 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743EXPORT_SYMBOL(default_wake_function);
4744
4745/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004746 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4747 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 * number) then we wake all the non-exclusive tasks and one exclusive task.
4749 *
4750 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004751 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4753 */
4754static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4755 int nr_exclusive, int sync, void *key)
4756{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004757 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004759 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004760 unsigned flags = curr->flags;
4761
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004763 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 break;
4765 }
4766}
4767
4768/**
4769 * __wake_up - wake up threads blocked on a waitqueue.
4770 * @q: the waitqueue
4771 * @mode: which threads
4772 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004773 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004775void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004776 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777{
4778 unsigned long flags;
4779
4780 spin_lock_irqsave(&q->lock, flags);
4781 __wake_up_common(q, mode, nr_exclusive, 0, key);
4782 spin_unlock_irqrestore(&q->lock, flags);
4783}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784EXPORT_SYMBOL(__wake_up);
4785
4786/*
4787 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4788 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004789void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790{
4791 __wake_up_common(q, mode, 1, 0, NULL);
4792}
4793
4794/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004795 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 * @q: the waitqueue
4797 * @mode: which threads
4798 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4799 *
4800 * The sync wakeup differs that the waker knows that it will schedule
4801 * away soon, so while the target thread will be woken up, it will not
4802 * be migrated to another CPU - ie. the two threads are 'synchronized'
4803 * with each other. This can prevent needless bouncing between CPUs.
4804 *
4805 * On UP it can prevent extra preemption.
4806 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004807void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004808__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809{
4810 unsigned long flags;
4811 int sync = 1;
4812
4813 if (unlikely(!q))
4814 return;
4815
4816 if (unlikely(!nr_exclusive))
4817 sync = 0;
4818
4819 spin_lock_irqsave(&q->lock, flags);
4820 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4821 spin_unlock_irqrestore(&q->lock, flags);
4822}
4823EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4824
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004825/**
4826 * complete: - signals a single thread waiting on this completion
4827 * @x: holds the state of this particular completion
4828 *
4829 * This will wake up a single thread waiting on this completion. Threads will be
4830 * awakened in the same order in which they were queued.
4831 *
4832 * See also complete_all(), wait_for_completion() and related routines.
4833 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004834void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835{
4836 unsigned long flags;
4837
4838 spin_lock_irqsave(&x->wait.lock, flags);
4839 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004840 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 spin_unlock_irqrestore(&x->wait.lock, flags);
4842}
4843EXPORT_SYMBOL(complete);
4844
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004845/**
4846 * complete_all: - signals all threads waiting on this completion
4847 * @x: holds the state of this particular completion
4848 *
4849 * This will wake up all threads waiting on this particular completion event.
4850 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004851void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852{
4853 unsigned long flags;
4854
4855 spin_lock_irqsave(&x->wait.lock, flags);
4856 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004857 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 spin_unlock_irqrestore(&x->wait.lock, flags);
4859}
4860EXPORT_SYMBOL(complete_all);
4861
Andi Kleen8cbbe862007-10-15 17:00:14 +02004862static inline long __sched
4863do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 if (!x->done) {
4866 DECLARE_WAITQUEUE(wait, current);
4867
4868 wait.flags |= WQ_FLAG_EXCLUSIVE;
4869 __add_wait_queue_tail(&x->wait, &wait);
4870 do {
Oleg Nesterov94d3d822008-08-20 16:54:41 -07004871 if (signal_pending_state(state, current)) {
Oleg Nesterovea71a542008-06-20 18:32:20 +04004872 timeout = -ERESTARTSYS;
4873 break;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004874 }
4875 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004877 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 spin_lock_irq(&x->wait.lock);
Oleg Nesterovea71a542008-06-20 18:32:20 +04004879 } while (!x->done && timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 __remove_wait_queue(&x->wait, &wait);
Oleg Nesterovea71a542008-06-20 18:32:20 +04004881 if (!x->done)
4882 return timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 }
4884 x->done--;
Oleg Nesterovea71a542008-06-20 18:32:20 +04004885 return timeout ?: 1;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004886}
4887
4888static long __sched
4889wait_for_common(struct completion *x, long timeout, int state)
4890{
4891 might_sleep();
4892
4893 spin_lock_irq(&x->wait.lock);
4894 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004896 return timeout;
4897}
4898
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004899/**
4900 * wait_for_completion: - waits for completion of a task
4901 * @x: holds the state of this particular completion
4902 *
4903 * This waits to be signaled for completion of a specific task. It is NOT
4904 * interruptible and there is no timeout.
4905 *
4906 * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
4907 * and interrupt capability. Also see complete().
4908 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004909void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004910{
4911 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912}
4913EXPORT_SYMBOL(wait_for_completion);
4914
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004915/**
4916 * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
4917 * @x: holds the state of this particular completion
4918 * @timeout: timeout value in jiffies
4919 *
4920 * This waits for either a completion of a specific task to be signaled or for a
4921 * specified timeout to expire. The timeout is in jiffies. It is not
4922 * interruptible.
4923 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004924unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4926{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004927 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928}
4929EXPORT_SYMBOL(wait_for_completion_timeout);
4930
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004931/**
4932 * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
4933 * @x: holds the state of this particular completion
4934 *
4935 * This waits for completion of a specific task to be signaled. It is
4936 * interruptible.
4937 */
Andi Kleen8cbbe862007-10-15 17:00:14 +02004938int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939{
Andi Kleen51e97992007-10-18 21:32:55 +02004940 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4941 if (t == -ERESTARTSYS)
4942 return t;
4943 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944}
4945EXPORT_SYMBOL(wait_for_completion_interruptible);
4946
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004947/**
4948 * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
4949 * @x: holds the state of this particular completion
4950 * @timeout: timeout value in jiffies
4951 *
4952 * This waits for either a completion of a specific task to be signaled or for a
4953 * specified timeout to expire. It is interruptible. The timeout is in jiffies.
4954 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004955unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956wait_for_completion_interruptible_timeout(struct completion *x,
4957 unsigned long timeout)
4958{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004959 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960}
4961EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4962
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004963/**
4964 * wait_for_completion_killable: - waits for completion of a task (killable)
4965 * @x: holds the state of this particular completion
4966 *
4967 * This waits to be signaled for completion of a specific task. It can be
4968 * interrupted by a kill signal.
4969 */
Matthew Wilcox009e5772007-12-06 12:29:54 -05004970int __sched wait_for_completion_killable(struct completion *x)
4971{
4972 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4973 if (t == -ERESTARTSYS)
4974 return t;
4975 return 0;
4976}
4977EXPORT_SYMBOL(wait_for_completion_killable);
4978
Dave Chinnerbe4de352008-08-15 00:40:44 -07004979/**
4980 * try_wait_for_completion - try to decrement a completion without blocking
4981 * @x: completion structure
4982 *
4983 * Returns: 0 if a decrement cannot be done without blocking
4984 * 1 if a decrement succeeded.
4985 *
4986 * If a completion is being used as a counting completion,
4987 * attempt to decrement the counter without blocking. This
4988 * enables us to avoid waiting if the resource the completion
4989 * is protecting is not available.
4990 */
4991bool try_wait_for_completion(struct completion *x)
4992{
4993 int ret = 1;
4994
4995 spin_lock_irq(&x->wait.lock);
4996 if (!x->done)
4997 ret = 0;
4998 else
4999 x->done--;
5000 spin_unlock_irq(&x->wait.lock);
5001 return ret;
5002}
5003EXPORT_SYMBOL(try_wait_for_completion);
5004
5005/**
5006 * completion_done - Test to see if a completion has any waiters
5007 * @x: completion structure
5008 *
5009 * Returns: 0 if there are waiters (wait_for_completion() in progress)
5010 * 1 if there are no waiters.
5011 *
5012 */
5013bool completion_done(struct completion *x)
5014{
5015 int ret = 1;
5016
5017 spin_lock_irq(&x->wait.lock);
5018 if (!x->done)
5019 ret = 0;
5020 spin_unlock_irq(&x->wait.lock);
5021 return ret;
5022}
5023EXPORT_SYMBOL(completion_done);
5024
Andi Kleen8cbbe862007-10-15 17:00:14 +02005025static long __sched
5026sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02005027{
5028 unsigned long flags;
5029 wait_queue_t wait;
5030
5031 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032
Andi Kleen8cbbe862007-10-15 17:00:14 +02005033 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034
Andi Kleen8cbbe862007-10-15 17:00:14 +02005035 spin_lock_irqsave(&q->lock, flags);
5036 __add_wait_queue(q, &wait);
5037 spin_unlock(&q->lock);
5038 timeout = schedule_timeout(timeout);
5039 spin_lock_irq(&q->lock);
5040 __remove_wait_queue(q, &wait);
5041 spin_unlock_irqrestore(&q->lock, flags);
5042
5043 return timeout;
5044}
5045
5046void __sched interruptible_sleep_on(wait_queue_head_t *q)
5047{
5048 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050EXPORT_SYMBOL(interruptible_sleep_on);
5051
Ingo Molnar0fec1712007-07-09 18:52:01 +02005052long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005053interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054{
Andi Kleen8cbbe862007-10-15 17:00:14 +02005055 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057EXPORT_SYMBOL(interruptible_sleep_on_timeout);
5058
Ingo Molnar0fec1712007-07-09 18:52:01 +02005059void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060{
Andi Kleen8cbbe862007-10-15 17:00:14 +02005061 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063EXPORT_SYMBOL(sleep_on);
5064
Ingo Molnar0fec1712007-07-09 18:52:01 +02005065long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066{
Andi Kleen8cbbe862007-10-15 17:00:14 +02005067 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069EXPORT_SYMBOL(sleep_on_timeout);
5070
Ingo Molnarb29739f2006-06-27 02:54:51 -07005071#ifdef CONFIG_RT_MUTEXES
5072
5073/*
5074 * rt_mutex_setprio - set the current priority of a task
5075 * @p: task
5076 * @prio: prio value (kernel-internal form)
5077 *
5078 * This function changes the 'effective' priority of a task. It does
5079 * not touch ->normal_prio like __setscheduler().
5080 *
5081 * Used by the rt_mutex code to implement priority inheritance logic.
5082 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005083void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07005084{
5085 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005086 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005087 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01005088 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005089
5090 BUG_ON(prio < 0 || prio > MAX_PRIO);
5091
5092 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005093 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005094
Andrew Mortond5f9f942007-05-08 20:27:06 -07005095 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005096 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005097 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005098 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02005099 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005100 if (running)
5101 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02005102
5103 if (rt_prio(prio))
5104 p->sched_class = &rt_sched_class;
5105 else
5106 p->sched_class = &fair_sched_class;
5107
Ingo Molnarb29739f2006-06-27 02:54:51 -07005108 p->prio = prio;
5109
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005110 if (running)
5111 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005112 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02005113 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005114
5115 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005116 }
5117 task_rq_unlock(rq, &flags);
5118}
5119
5120#endif
5121
Ingo Molnar36c8b582006-07-03 00:25:41 -07005122void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123{
Ingo Molnardd41f592007-07-09 18:51:59 +02005124 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005126 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127
5128 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
5129 return;
5130 /*
5131 * We have to be careful, if called from sys_setpriority(),
5132 * the task might be in the middle of scheduling on another CPU.
5133 */
5134 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005135 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 /*
5137 * The RT priorities are set via sched_setscheduler(), but we still
5138 * allow the 'normal' nice value to be set - but as expected
5139 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02005140 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 */
Ingo Molnare05606d2007-07-09 18:51:59 +02005142 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 p->static_prio = NICE_TO_PRIO(nice);
5144 goto out_unlock;
5145 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005146 on_rq = p->se.on_rq;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02005147 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02005148 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07005151 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005152 old_prio = p->prio;
5153 p->prio = effective_prio(p);
5154 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155
Ingo Molnardd41f592007-07-09 18:51:59 +02005156 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02005157 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07005159 * If the task increased its priority or is running and
5160 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07005162 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 resched_task(rq->curr);
5164 }
5165out_unlock:
5166 task_rq_unlock(rq, &flags);
5167}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168EXPORT_SYMBOL(set_user_nice);
5169
Matt Mackalle43379f2005-05-01 08:59:00 -07005170/*
5171 * can_nice - check if a task can reduce its nice value
5172 * @p: task
5173 * @nice: nice value
5174 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005175int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07005176{
Matt Mackall024f4742005-08-18 11:24:19 -07005177 /* convert nice value [19,-20] to rlimit style value [1,40] */
5178 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005179
Matt Mackalle43379f2005-05-01 08:59:00 -07005180 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
5181 capable(CAP_SYS_NICE));
5182}
5183
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184#ifdef __ARCH_WANT_SYS_NICE
5185
5186/*
5187 * sys_nice - change the priority of the current process.
5188 * @increment: priority increment
5189 *
5190 * sys_setpriority is a more generic, but much slower function that
5191 * does similar things.
5192 */
5193asmlinkage long sys_nice(int increment)
5194{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005195 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196
5197 /*
5198 * Setpriority might change our priority at the same moment.
5199 * We don't have to worry. Conceptually one call occurs first
5200 * and we have a single winner.
5201 */
Matt Mackalle43379f2005-05-01 08:59:00 -07005202 if (increment < -40)
5203 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 if (increment > 40)
5205 increment = 40;
5206
5207 nice = PRIO_TO_NICE(current->static_prio) + increment;
5208 if (nice < -20)
5209 nice = -20;
5210 if (nice > 19)
5211 nice = 19;
5212
Matt Mackalle43379f2005-05-01 08:59:00 -07005213 if (increment < 0 && !can_nice(current, nice))
5214 return -EPERM;
5215
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 retval = security_task_setnice(current, nice);
5217 if (retval)
5218 return retval;
5219
5220 set_user_nice(current, nice);
5221 return 0;
5222}
5223
5224#endif
5225
5226/**
5227 * task_prio - return the priority value of a given task.
5228 * @p: the task in question.
5229 *
5230 * This is the priority value as seen by users in /proc.
5231 * RT tasks are offset by -200. Normal tasks are centered
5232 * around 0, value goes from -16 to +15.
5233 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005234int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235{
5236 return p->prio - MAX_RT_PRIO;
5237}
5238
5239/**
5240 * task_nice - return the nice value of a given task.
5241 * @p: the task in question.
5242 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005243int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244{
5245 return TASK_NICE(p);
5246}
Pavel Roskin150d8be2008-03-05 16:56:37 -05005247EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248
5249/**
5250 * idle_cpu - is a given cpu idle currently?
5251 * @cpu: the processor in question.
5252 */
5253int idle_cpu(int cpu)
5254{
5255 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
5256}
5257
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258/**
5259 * idle_task - return the idle task for a given cpu.
5260 * @cpu: the processor in question.
5261 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005262struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263{
5264 return cpu_rq(cpu)->idle;
5265}
5266
5267/**
5268 * find_process_by_pid - find a process with a matching PID value.
5269 * @pid: the pid in question.
5270 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02005271static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07005273 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274}
5275
5276/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02005277static void
5278__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279{
Ingo Molnardd41f592007-07-09 18:51:59 +02005280 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005281
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02005283 switch (p->policy) {
5284 case SCHED_NORMAL:
5285 case SCHED_BATCH:
5286 case SCHED_IDLE:
5287 p->sched_class = &fair_sched_class;
5288 break;
5289 case SCHED_FIFO:
5290 case SCHED_RR:
5291 p->sched_class = &rt_sched_class;
5292 break;
5293 }
5294
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005296 p->normal_prio = normal_prio(p);
5297 /* we are holding p->pi_lock already */
5298 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07005299 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300}
5301
David Howellsc69e8d92008-11-14 10:39:19 +11005302/*
5303 * check the target process has a UID that matches the current process's
5304 */
5305static bool check_same_owner(struct task_struct *p)
5306{
5307 const struct cred *cred = current_cred(), *pcred;
5308 bool match;
5309
5310 rcu_read_lock();
5311 pcred = __task_cred(p);
5312 match = (cred->euid == pcred->euid ||
5313 cred->euid == pcred->uid);
5314 rcu_read_unlock();
5315 return match;
5316}
5317
Rusty Russell961ccdd2008-06-23 13:55:38 +10005318static int __sched_setscheduler(struct task_struct *p, int policy,
5319 struct sched_param *param, bool user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005321 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01005323 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005324 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325
Steven Rostedt66e53932006-06-27 02:54:44 -07005326 /* may grab non-irq protected spin_locks */
5327 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328recheck:
5329 /* double check policy once rq lock held */
5330 if (policy < 0)
5331 policy = oldpolicy = p->policy;
5332 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02005333 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
5334 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08005335 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 /*
5337 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02005338 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5339 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 */
5341 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005342 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04005343 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02005345 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 return -EINVAL;
5347
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005348 /*
5349 * Allow unprivileged RT tasks to decrease priority:
5350 */
Rusty Russell961ccdd2008-06-23 13:55:38 +10005351 if (user && !capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02005352 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005353 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005354
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005355 if (!lock_task_sighand(p, &flags))
5356 return -ESRCH;
5357 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
5358 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005359
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005360 /* can't set/change the rt policy */
5361 if (policy != p->policy && !rlim_rtprio)
5362 return -EPERM;
5363
5364 /* can't increase priority */
5365 if (param->sched_priority > p->rt_priority &&
5366 param->sched_priority > rlim_rtprio)
5367 return -EPERM;
5368 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005369 /*
5370 * Like positive nice levels, dont allow tasks to
5371 * move out of SCHED_IDLE either:
5372 */
5373 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
5374 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005375
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005376 /* can't change other user's priorities */
David Howellsc69e8d92008-11-14 10:39:19 +11005377 if (!check_same_owner(p))
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005378 return -EPERM;
5379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005381 if (user) {
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005382#ifdef CONFIG_RT_GROUP_SCHED
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005383 /*
5384 * Do not allow realtime tasks into groups that have no runtime
5385 * assigned.
5386 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02005387 if (rt_bandwidth_enabled() && rt_policy(policy) &&
5388 task_group(p)->rt_bandwidth.rt_runtime == 0)
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005389 return -EPERM;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005390#endif
5391
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005392 retval = security_task_setscheduler(p, policy, param);
5393 if (retval)
5394 return retval;
5395 }
5396
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07005398 * make sure no PI-waiters arrive (or leave) while we are
5399 * changing the priority of the task:
5400 */
5401 spin_lock_irqsave(&p->pi_lock, flags);
5402 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 * To be able to change p->policy safely, the apropriate
5404 * runqueue lock must be held.
5405 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07005406 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 /* recheck policy now with rq lock held */
5408 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
5409 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005410 __task_rq_unlock(rq);
5411 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 goto recheck;
5413 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02005414 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005415 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005416 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005417 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005418 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005419 if (running)
5420 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02005421
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005423 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02005424
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005425 if (running)
5426 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005427 if (on_rq) {
5428 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005429
5430 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07005432 __task_rq_unlock(rq);
5433 spin_unlock_irqrestore(&p->pi_lock, flags);
5434
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07005435 rt_mutex_adjust_pi(p);
5436
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 return 0;
5438}
Rusty Russell961ccdd2008-06-23 13:55:38 +10005439
5440/**
5441 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
5442 * @p: the task in question.
5443 * @policy: new policy.
5444 * @param: structure containing the new RT priority.
5445 *
5446 * NOTE that the task may be already dead.
5447 */
5448int sched_setscheduler(struct task_struct *p, int policy,
5449 struct sched_param *param)
5450{
5451 return __sched_setscheduler(p, policy, param, true);
5452}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453EXPORT_SYMBOL_GPL(sched_setscheduler);
5454
Rusty Russell961ccdd2008-06-23 13:55:38 +10005455/**
5456 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
5457 * @p: the task in question.
5458 * @policy: new policy.
5459 * @param: structure containing the new RT priority.
5460 *
5461 * Just like sched_setscheduler, only don't bother checking if the
5462 * current context has permission. For example, this is needed in
5463 * stop_machine(): we create temporary high priority worker threads,
5464 * but our caller might not have that capability.
5465 */
5466int sched_setscheduler_nocheck(struct task_struct *p, int policy,
5467 struct sched_param *param)
5468{
5469 return __sched_setscheduler(p, policy, param, false);
5470}
5471
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005472static int
5473do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 struct sched_param lparam;
5476 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005477 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478
5479 if (!param || pid < 0)
5480 return -EINVAL;
5481 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
5482 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005483
5484 rcu_read_lock();
5485 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005487 if (p != NULL)
5488 retval = sched_setscheduler(p, policy, &lparam);
5489 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07005490
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 return retval;
5492}
5493
5494/**
5495 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5496 * @pid: the pid in question.
5497 * @policy: new policy.
5498 * @param: structure containing the new RT priority.
5499 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005500asmlinkage long
5501sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502{
Jason Baronc21761f2006-01-18 17:43:03 -08005503 /* negative values for policy are not valid */
5504 if (policy < 0)
5505 return -EINVAL;
5506
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 return do_sched_setscheduler(pid, policy, param);
5508}
5509
5510/**
5511 * sys_sched_setparam - set/change the RT priority of a thread
5512 * @pid: the pid in question.
5513 * @param: structure containing the new RT priority.
5514 */
5515asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
5516{
5517 return do_sched_setscheduler(pid, -1, param);
5518}
5519
5520/**
5521 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5522 * @pid: the pid in question.
5523 */
5524asmlinkage long sys_sched_getscheduler(pid_t pid)
5525{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005526 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005527 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528
5529 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005530 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531
5532 retval = -ESRCH;
5533 read_lock(&tasklist_lock);
5534 p = find_process_by_pid(pid);
5535 if (p) {
5536 retval = security_task_getscheduler(p);
5537 if (!retval)
5538 retval = p->policy;
5539 }
5540 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 return retval;
5542}
5543
5544/**
5545 * sys_sched_getscheduler - get the RT priority of a thread
5546 * @pid: the pid in question.
5547 * @param: structure containing the RT priority.
5548 */
5549asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
5550{
5551 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005552 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005553 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554
5555 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005556 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557
5558 read_lock(&tasklist_lock);
5559 p = find_process_by_pid(pid);
5560 retval = -ESRCH;
5561 if (!p)
5562 goto out_unlock;
5563
5564 retval = security_task_getscheduler(p);
5565 if (retval)
5566 goto out_unlock;
5567
5568 lp.sched_priority = p->rt_priority;
5569 read_unlock(&tasklist_lock);
5570
5571 /*
5572 * This one might sleep, we cannot do it with a spinlock held ...
5573 */
5574 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
5575
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 return retval;
5577
5578out_unlock:
5579 read_unlock(&tasklist_lock);
5580 return retval;
5581}
5582
Rusty Russell96f874e2008-11-25 02:35:14 +10305583long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305585 cpumask_var_t cpus_allowed, new_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005586 struct task_struct *p;
5587 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005589 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 read_lock(&tasklist_lock);
5591
5592 p = find_process_by_pid(pid);
5593 if (!p) {
5594 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005595 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 return -ESRCH;
5597 }
5598
5599 /*
5600 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005601 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 * usage count and then drop tasklist_lock.
5603 */
5604 get_task_struct(p);
5605 read_unlock(&tasklist_lock);
5606
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305607 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
5608 retval = -ENOMEM;
5609 goto out_put_task;
5610 }
5611 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
5612 retval = -ENOMEM;
5613 goto out_free_cpus_allowed;
5614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 retval = -EPERM;
David Howellsc69e8d92008-11-14 10:39:19 +11005616 if (!check_same_owner(p) && !capable(CAP_SYS_NICE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 goto out_unlock;
5618
David Quigleye7834f82006-06-23 02:03:59 -07005619 retval = security_task_setscheduler(p, 0, NULL);
5620 if (retval)
5621 goto out_unlock;
5622
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305623 cpuset_cpus_allowed(p, cpus_allowed);
5624 cpumask_and(new_mask, in_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005625 again:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305626 retval = set_cpus_allowed_ptr(p, new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627
Paul Menage8707d8b2007-10-18 23:40:22 -07005628 if (!retval) {
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305629 cpuset_cpus_allowed(p, cpus_allowed);
5630 if (!cpumask_subset(new_mask, cpus_allowed)) {
Paul Menage8707d8b2007-10-18 23:40:22 -07005631 /*
5632 * We must have raced with a concurrent cpuset
5633 * update. Just reset the cpus_allowed to the
5634 * cpuset's cpus_allowed
5635 */
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305636 cpumask_copy(new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005637 goto again;
5638 }
5639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640out_unlock:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305641 free_cpumask_var(new_mask);
5642out_free_cpus_allowed:
5643 free_cpumask_var(cpus_allowed);
5644out_put_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005646 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 return retval;
5648}
5649
5650static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
Rusty Russell96f874e2008-11-25 02:35:14 +10305651 struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652{
Rusty Russell96f874e2008-11-25 02:35:14 +10305653 if (len < cpumask_size())
5654 cpumask_clear(new_mask);
5655 else if (len > cpumask_size())
5656 len = cpumask_size();
5657
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5659}
5660
5661/**
5662 * sys_sched_setaffinity - set the cpu affinity of a process
5663 * @pid: pid of the process
5664 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5665 * @user_mask_ptr: user-space pointer to the new cpu mask
5666 */
5667asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
5668 unsigned long __user *user_mask_ptr)
5669{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305670 cpumask_var_t new_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 int retval;
5672
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305673 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
5674 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305676 retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
5677 if (retval == 0)
5678 retval = sched_setaffinity(pid, new_mask);
5679 free_cpumask_var(new_mask);
5680 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681}
5682
Rusty Russell96f874e2008-11-25 02:35:14 +10305683long sched_getaffinity(pid_t pid, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005685 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005688 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 read_lock(&tasklist_lock);
5690
5691 retval = -ESRCH;
5692 p = find_process_by_pid(pid);
5693 if (!p)
5694 goto out_unlock;
5695
David Quigleye7834f82006-06-23 02:03:59 -07005696 retval = security_task_getscheduler(p);
5697 if (retval)
5698 goto out_unlock;
5699
Rusty Russell96f874e2008-11-25 02:35:14 +10305700 cpumask_and(mask, &p->cpus_allowed, cpu_online_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701
5702out_unlock:
5703 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005704 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705
Ulrich Drepper9531b622007-08-09 11:16:46 +02005706 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707}
5708
5709/**
5710 * sys_sched_getaffinity - get the cpu affinity of a process
5711 * @pid: pid of the process
5712 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5713 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5714 */
5715asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
5716 unsigned long __user *user_mask_ptr)
5717{
5718 int ret;
Rusty Russellf17c8602008-11-25 02:35:11 +10305719 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720
Rusty Russellf17c8602008-11-25 02:35:11 +10305721 if (len < cpumask_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 return -EINVAL;
5723
Rusty Russellf17c8602008-11-25 02:35:11 +10305724 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
5725 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726
Rusty Russellf17c8602008-11-25 02:35:11 +10305727 ret = sched_getaffinity(pid, mask);
5728 if (ret == 0) {
5729 if (copy_to_user(user_mask_ptr, mask, cpumask_size()))
5730 ret = -EFAULT;
5731 else
5732 ret = cpumask_size();
5733 }
5734 free_cpumask_var(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735
Rusty Russellf17c8602008-11-25 02:35:11 +10305736 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737}
5738
5739/**
5740 * sys_sched_yield - yield the current processor to other threads.
5741 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005742 * This function yields the current CPU to other tasks. If there are no
5743 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 */
5745asmlinkage long sys_sched_yield(void)
5746{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005747 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748
Ingo Molnar2d723762007-10-15 17:00:12 +02005749 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005750 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751
5752 /*
5753 * Since we are going to call schedule() anyway, there's
5754 * no need to preempt or enable interrupts:
5755 */
5756 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005757 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 _raw_spin_unlock(&rq->lock);
5759 preempt_enable_no_resched();
5760
5761 schedule();
5762
5763 return 0;
5764}
5765
Andrew Mortone7b38402006-06-30 01:56:00 -07005766static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07005768#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5769 __might_sleep(__FILE__, __LINE__);
5770#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005771 /*
5772 * The BKS might be reacquired before we have dropped
5773 * PREEMPT_ACTIVE, which could trigger a second
5774 * cond_resched() call.
5775 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 do {
5777 add_preempt_count(PREEMPT_ACTIVE);
5778 schedule();
5779 sub_preempt_count(PREEMPT_ACTIVE);
5780 } while (need_resched());
5781}
5782
Herbert Xu02b67cc32008-01-25 21:08:28 +01005783int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784{
Ingo Molnar94142322006-12-29 16:48:13 -08005785 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5786 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 __cond_resched();
5788 return 1;
5789 }
5790 return 0;
5791}
Herbert Xu02b67cc32008-01-25 21:08:28 +01005792EXPORT_SYMBOL(_cond_resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793
5794/*
5795 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5796 * call schedule, and on return reacquire the lock.
5797 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005798 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 * operations here to prevent schedule() from being called twice (once via
5800 * spin_unlock(), once by hand).
5801 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005802int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803{
Nick Piggin95c354f2008-01-30 13:31:20 +01005804 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005805 int ret = 0;
5806
Nick Piggin95c354f2008-01-30 13:31:20 +01005807 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005809 if (resched && need_resched())
5810 __cond_resched();
5811 else
5812 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005813 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005816 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818EXPORT_SYMBOL(cond_resched_lock);
5819
5820int __sched cond_resched_softirq(void)
5821{
5822 BUG_ON(!in_softirq());
5823
Ingo Molnar94142322006-12-29 16:48:13 -08005824 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07005825 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 __cond_resched();
5827 local_bh_disable();
5828 return 1;
5829 }
5830 return 0;
5831}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832EXPORT_SYMBOL(cond_resched_softirq);
5833
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834/**
5835 * yield - yield the current processor to other threads.
5836 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005837 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 * thread runnable and calls sys_sched_yield().
5839 */
5840void __sched yield(void)
5841{
5842 set_current_state(TASK_RUNNING);
5843 sys_sched_yield();
5844}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845EXPORT_SYMBOL(yield);
5846
5847/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005848 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 * that process accounting knows that this is a task in IO wait state.
5850 *
5851 * But don't do that if it is a deliberate, throttling IO wait (this task
5852 * has set its backing_dev_info: the queue against which it should throttle)
5853 */
5854void __sched io_schedule(void)
5855{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005856 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005858 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 atomic_inc(&rq->nr_iowait);
5860 schedule();
5861 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005862 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864EXPORT_SYMBOL(io_schedule);
5865
5866long __sched io_schedule_timeout(long timeout)
5867{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005868 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 long ret;
5870
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005871 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 atomic_inc(&rq->nr_iowait);
5873 ret = schedule_timeout(timeout);
5874 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005875 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 return ret;
5877}
5878
5879/**
5880 * sys_sched_get_priority_max - return maximum RT priority.
5881 * @policy: scheduling class.
5882 *
5883 * this syscall returns the maximum rt_priority that can be used
5884 * by a given scheduling class.
5885 */
5886asmlinkage long sys_sched_get_priority_max(int policy)
5887{
5888 int ret = -EINVAL;
5889
5890 switch (policy) {
5891 case SCHED_FIFO:
5892 case SCHED_RR:
5893 ret = MAX_USER_RT_PRIO-1;
5894 break;
5895 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005896 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005897 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 ret = 0;
5899 break;
5900 }
5901 return ret;
5902}
5903
5904/**
5905 * sys_sched_get_priority_min - return minimum RT priority.
5906 * @policy: scheduling class.
5907 *
5908 * this syscall returns the minimum rt_priority that can be used
5909 * by a given scheduling class.
5910 */
5911asmlinkage long sys_sched_get_priority_min(int policy)
5912{
5913 int ret = -EINVAL;
5914
5915 switch (policy) {
5916 case SCHED_FIFO:
5917 case SCHED_RR:
5918 ret = 1;
5919 break;
5920 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005921 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005922 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 ret = 0;
5924 }
5925 return ret;
5926}
5927
5928/**
5929 * sys_sched_rr_get_interval - return the default timeslice of a process.
5930 * @pid: pid of the process.
5931 * @interval: userspace pointer to the timeslice value.
5932 *
5933 * this syscall writes the default timeslice value of a given process
5934 * into the user-space timespec buffer. A value of '0' means infinity.
5935 */
5936asmlinkage
5937long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5938{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005939 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005940 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005941 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943
5944 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005945 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946
5947 retval = -ESRCH;
5948 read_lock(&tasklist_lock);
5949 p = find_process_by_pid(pid);
5950 if (!p)
5951 goto out_unlock;
5952
5953 retval = security_task_getscheduler(p);
5954 if (retval)
5955 goto out_unlock;
5956
Ingo Molnar77034932007-12-04 17:04:39 +01005957 /*
5958 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5959 * tasks that are on an otherwise idle runqueue:
5960 */
5961 time_slice = 0;
5962 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005963 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005964 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005965 struct sched_entity *se = &p->se;
5966 unsigned long flags;
5967 struct rq *rq;
5968
5969 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005970 if (rq->cfs.load.weight)
5971 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005972 task_rq_unlock(rq, &flags);
5973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005975 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005978
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979out_unlock:
5980 read_unlock(&tasklist_lock);
5981 return retval;
5982}
5983
Steven Rostedt7c731e02008-05-12 21:20:41 +02005984static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005985
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005986void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005989 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005992 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005993 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005994#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005996 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005998 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999#else
6000 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02006001 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02006003 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004#endif
6005#ifdef CONFIG_DEBUG_STACK_USAGE
6006 {
Al Viro10ebffd2005-11-13 16:06:56 -08006007 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 while (!*n)
6009 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08006010 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 }
6012#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07006013 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08006014 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01006016 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017}
6018
Ingo Molnare59e2ae2006-12-06 20:35:59 -08006019void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020{
Ingo Molnar36c8b582006-07-03 00:25:41 -07006021 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022
Ingo Molnar4bd77322007-07-11 21:21:47 +02006023#if BITS_PER_LONG == 32
6024 printk(KERN_INFO
6025 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02006027 printk(KERN_INFO
6028 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029#endif
6030 read_lock(&tasklist_lock);
6031 do_each_thread(g, p) {
6032 /*
6033 * reset the NMI-timeout, listing all files on a slow
6034 * console might take alot of time:
6035 */
6036 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07006037 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01006038 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 } while_each_thread(g, p);
6040
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07006041 touch_all_softlockup_watchdogs();
6042
Ingo Molnardd41f592007-07-09 18:51:59 +02006043#ifdef CONFIG_SCHED_DEBUG
6044 sysrq_sched_debug_show();
6045#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08006047 /*
6048 * Only show locks if all tasks are dumped:
6049 */
6050 if (state_filter == -1)
6051 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052}
6053
Ingo Molnar1df21052007-07-09 18:51:58 +02006054void __cpuinit init_idle_bootup_task(struct task_struct *idle)
6055{
Ingo Molnardd41f592007-07-09 18:51:59 +02006056 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02006057}
6058
Ingo Molnarf340c0d2005-06-28 16:40:42 +02006059/**
6060 * init_idle - set up an idle thread for a given CPU
6061 * @idle: task in question
6062 * @cpu: cpu the idle task belongs to
6063 *
6064 * NOTE: this function does not set the idle thread's NEED_RESCHED
6065 * flag, to make booting more robust.
6066 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07006067void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006069 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 unsigned long flags;
6071
Ingo Molnar5cbd54e2008-11-12 20:05:50 +01006072 spin_lock_irqsave(&rq->lock, flags);
6073
Ingo Molnardd41f592007-07-09 18:51:59 +02006074 __sched_fork(idle);
6075 idle->se.exec_start = sched_clock();
6076
Ingo Molnarb29739f2006-06-27 02:54:51 -07006077 idle->prio = idle->normal_prio = MAX_PRIO;
Rusty Russell96f874e2008-11-25 02:35:14 +10306078 cpumask_copy(&idle->cpus_allowed, cpumask_of(cpu));
Ingo Molnardd41f592007-07-09 18:51:59 +02006079 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07006082#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
6083 idle->oncpu = 1;
6084#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 spin_unlock_irqrestore(&rq->lock, flags);
6086
6087 /* Set the preempt count _outside_ the spinlocks! */
Linus Torvalds8e3e0762008-05-10 20:58:02 -07006088#if defined(CONFIG_PREEMPT)
6089 task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
6090#else
Al Viroa1261f52005-11-13 16:06:55 -08006091 task_thread_info(idle)->preempt_count = 0;
Linus Torvalds8e3e0762008-05-10 20:58:02 -07006092#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02006093 /*
6094 * The idle tasks have their own, simple scheduling class:
6095 */
6096 idle->sched_class = &idle_sched_class;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006097 ftrace_graph_init_task(idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098}
6099
6100/*
6101 * In a system that switches off the HZ timer nohz_cpu_mask
6102 * indicates which cpus entered this state. This is used
6103 * in the rcu update to wait only for active cpus. For system
6104 * which do not switch off the HZ timer nohz_cpu_mask should
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10306105 * always be CPU_BITS_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106 */
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10306107cpumask_var_t nohz_cpu_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108
Ingo Molnar19978ca2007-11-09 22:39:38 +01006109/*
6110 * Increase the granularity value when there are more CPUs,
6111 * because with more CPUs the 'effective latency' as visible
6112 * to users decreases. But the relationship is not linear,
6113 * so pick a second-best guess by going with the log2 of the
6114 * number of CPUs.
6115 *
6116 * This idea comes from the SD scheduler of Con Kolivas:
6117 */
6118static inline void sched_init_granularity(void)
6119{
6120 unsigned int factor = 1 + ilog2(num_online_cpus());
6121 const unsigned long limit = 200000000;
6122
6123 sysctl_sched_min_granularity *= factor;
6124 if (sysctl_sched_min_granularity > limit)
6125 sysctl_sched_min_granularity = limit;
6126
6127 sysctl_sched_latency *= factor;
6128 if (sysctl_sched_latency > limit)
6129 sysctl_sched_latency = limit;
6130
6131 sysctl_sched_wakeup_granularity *= factor;
Peter Zijlstra55cd5342008-08-04 08:54:26 +02006132
6133 sysctl_sched_shares_ratelimit *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01006134}
6135
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136#ifdef CONFIG_SMP
6137/*
6138 * This is how migration works:
6139 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07006140 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141 * runqueue and wake up that CPU's migration thread.
6142 * 2) we down() the locked semaphore => thread blocks.
6143 * 3) migration thread wakes up (implicitly it forces the migrated
6144 * thread off the CPU)
6145 * 4) it gets the migration request and checks whether the migrated
6146 * task is still in the wrong runqueue.
6147 * 5) if it's in the wrong runqueue then the migration thread removes
6148 * it and puts it into the right queue.
6149 * 6) migration thread up()s the semaphore.
6150 * 7) we wake up and the migration is done.
6151 */
6152
6153/*
6154 * Change a given task's CPU affinity. Migrate the thread to a
6155 * proper CPU and schedule it away if the CPU it's executing on
6156 * is removed from the allowed bitmask.
6157 *
6158 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006159 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160 * call is not atomic; no spinlocks may be held.
6161 */
Rusty Russell96f874e2008-11-25 02:35:14 +10306162int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006164 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006166 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006167 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168
6169 rq = task_rq_lock(p, &flags);
Rusty Russell96f874e2008-11-25 02:35:14 +10306170 if (!cpumask_intersects(new_mask, cpu_online_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171 ret = -EINVAL;
6172 goto out;
6173 }
6174
David Rientjes9985b0b2008-06-05 12:57:11 -07006175 if (unlikely((p->flags & PF_THREAD_BOUND) && p != current &&
Rusty Russell96f874e2008-11-25 02:35:14 +10306176 !cpumask_equal(&p->cpus_allowed, new_mask))) {
David Rientjes9985b0b2008-06-05 12:57:11 -07006177 ret = -EINVAL;
6178 goto out;
6179 }
6180
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006181 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006182 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006183 else {
Rusty Russell96f874e2008-11-25 02:35:14 +10306184 cpumask_copy(&p->cpus_allowed, new_mask);
6185 p->rt.nr_cpus_allowed = cpumask_weight(new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006186 }
6187
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 /* Can the task run on the task's current CPU? If so, we're done */
Rusty Russell96f874e2008-11-25 02:35:14 +10306189 if (cpumask_test_cpu(task_cpu(p), new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 goto out;
6191
Rusty Russell1e5ce4f2008-11-25 02:35:03 +10306192 if (migrate_task(p, cpumask_any_and(cpu_online_mask, new_mask), &req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193 /* Need help from migration thread: drop lock and wait. */
6194 task_rq_unlock(rq, &flags);
6195 wake_up_process(rq->migration_thread);
6196 wait_for_completion(&req.done);
6197 tlb_migrate_finish(p->mm);
6198 return 0;
6199 }
6200out:
6201 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006202
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 return ret;
6204}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006205EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206
6207/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006208 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209 * this because either it can't run here any more (set_cpus_allowed()
6210 * away from this CPU, or CPU going down), or because we're
6211 * attempting to rebalance this task on exec (sched_exec).
6212 *
6213 * So we race with normal scheduler movements, but that's OK, as long
6214 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07006215 *
6216 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07006218static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006220 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02006221 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222
Max Krasnyanskye761b772008-07-15 04:43:49 -07006223 if (unlikely(!cpu_active(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07006224 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225
6226 rq_src = cpu_rq(src_cpu);
6227 rq_dest = cpu_rq(dest_cpu);
6228
6229 double_rq_lock(rq_src, rq_dest);
6230 /* Already moved. */
6231 if (task_cpu(p) != src_cpu)
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006232 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233 /* Affinity changed (again). */
Rusty Russell96f874e2008-11-25 02:35:14 +10306234 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006235 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236
Ingo Molnardd41f592007-07-09 18:51:59 +02006237 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006238 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006239 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006240
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006242 if (on_rq) {
6243 activate_task(rq_dest, p, 0);
Peter Zijlstra15afe092008-09-20 23:38:02 +02006244 check_preempt_curr(rq_dest, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 }
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006246done:
Kirill Korotaevefc30812006-06-27 02:54:32 -07006247 ret = 1;
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006248fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07006250 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251}
6252
6253/*
6254 * migration_thread - this is a highprio system thread that performs
6255 * thread migration by bumping thread off CPU then 'pushing' onto
6256 * another runqueue.
6257 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07006258static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006261 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262
6263 rq = cpu_rq(cpu);
6264 BUG_ON(rq->migration_thread != current);
6265
6266 set_current_state(TASK_INTERRUPTIBLE);
6267 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006268 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 spin_lock_irq(&rq->lock);
6272
6273 if (cpu_is_offline(cpu)) {
6274 spin_unlock_irq(&rq->lock);
6275 goto wait_to_die;
6276 }
6277
6278 if (rq->active_balance) {
6279 active_load_balance(rq, cpu);
6280 rq->active_balance = 0;
6281 }
6282
6283 head = &rq->migration_queue;
6284
6285 if (list_empty(head)) {
6286 spin_unlock_irq(&rq->lock);
6287 schedule();
6288 set_current_state(TASK_INTERRUPTIBLE);
6289 continue;
6290 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07006291 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292 list_del_init(head->next);
6293
Nick Piggin674311d2005-06-25 14:57:27 -07006294 spin_unlock(&rq->lock);
6295 __migrate_task(req->task, cpu, req->dest_cpu);
6296 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297
6298 complete(&req->done);
6299 }
6300 __set_current_state(TASK_RUNNING);
6301 return 0;
6302
6303wait_to_die:
6304 /* Wait for kthread_stop */
6305 set_current_state(TASK_INTERRUPTIBLE);
6306 while (!kthread_should_stop()) {
6307 schedule();
6308 set_current_state(TASK_INTERRUPTIBLE);
6309 }
6310 __set_current_state(TASK_RUNNING);
6311 return 0;
6312}
6313
6314#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006315
6316static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
6317{
6318 int ret;
6319
6320 local_irq_disable();
6321 ret = __migrate_task(p, src_cpu, dest_cpu);
6322 local_irq_enable();
6323 return ret;
6324}
6325
Kirill Korotaev054b9102006-12-10 02:20:11 -08006326/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006327 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08006328 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006329static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006331 int dest_cpu;
Mike Travis6ca09df2008-12-31 18:08:45 -08006332 const struct cpumask *nodemask = cpumask_of_node(cpu_to_node(dead_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333
Rusty Russelle76bd8d2008-11-25 02:35:11 +10306334again:
6335 /* Look for allowed, online CPU in same node. */
6336 for_each_cpu_and(dest_cpu, nodemask, cpu_online_mask)
6337 if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
6338 goto move;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339
Rusty Russelle76bd8d2008-11-25 02:35:11 +10306340 /* Any allowed, online CPU? */
6341 dest_cpu = cpumask_any_and(&p->cpus_allowed, cpu_online_mask);
6342 if (dest_cpu < nr_cpu_ids)
6343 goto move;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344
Rusty Russelle76bd8d2008-11-25 02:35:11 +10306345 /* No more Mr. Nice Guy. */
6346 if (dest_cpu >= nr_cpu_ids) {
Rusty Russelle76bd8d2008-11-25 02:35:11 +10306347 cpuset_cpus_allowed_locked(p, &p->cpus_allowed);
6348 dest_cpu = cpumask_any_and(cpu_online_mask, &p->cpus_allowed);
Mike Travisf9a86fc2008-04-04 18:11:07 -07006349
Rusty Russelle76bd8d2008-11-25 02:35:11 +10306350 /*
6351 * Don't tell them about moving exiting tasks or
6352 * kernel threads (both mm NULL), since they never
6353 * leave kernel.
6354 */
6355 if (p->mm && printk_ratelimit()) {
6356 printk(KERN_INFO "process %d (%s) no "
6357 "longer affine to cpu%d\n",
6358 task_pid_nr(p), p->comm, dead_cpu);
Andi Kleen3a5c3592007-10-15 17:00:14 +02006359 }
Rusty Russelle76bd8d2008-11-25 02:35:11 +10306360 }
6361
6362move:
6363 /* It can have affinity changed while we were choosing. */
6364 if (unlikely(!__migrate_task_irq(p, dead_cpu, dest_cpu)))
6365 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366}
6367
6368/*
6369 * While a dead CPU has no uninterruptible tasks queued at this point,
6370 * it might still have a nonzero ->nr_uninterruptible counter, because
6371 * for performance reasons the counter is not stricly tracking tasks to
6372 * their home CPUs. So we just add the counter to another CPU's counter,
6373 * to keep the global sum constant after CPU-down:
6374 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07006375static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376{
Rusty Russell1e5ce4f2008-11-25 02:35:03 +10306377 struct rq *rq_dest = cpu_rq(cpumask_any(cpu_online_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378 unsigned long flags;
6379
6380 local_irq_save(flags);
6381 double_rq_lock(rq_src, rq_dest);
6382 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
6383 rq_src->nr_uninterruptible = 0;
6384 double_rq_unlock(rq_src, rq_dest);
6385 local_irq_restore(flags);
6386}
6387
6388/* Run through task list and migrate tasks from the dead cpu. */
6389static void migrate_live_tasks(int src_cpu)
6390{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006391 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006393 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394
Ingo Molnar48f24c42006-07-03 00:25:40 -07006395 do_each_thread(t, p) {
6396 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397 continue;
6398
Ingo Molnar48f24c42006-07-03 00:25:40 -07006399 if (task_cpu(p) == src_cpu)
6400 move_task_off_dead_cpu(src_cpu, p);
6401 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006403 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404}
6405
Ingo Molnardd41f592007-07-09 18:51:59 +02006406/*
6407 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006408 * It does so by boosting its priority to highest possible.
6409 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 */
6411void sched_idle_next(void)
6412{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006413 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07006414 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415 struct task_struct *p = rq->idle;
6416 unsigned long flags;
6417
6418 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006419 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420
Ingo Molnar48f24c42006-07-03 00:25:40 -07006421 /*
6422 * Strictly not necessary since rest of the CPUs are stopped by now
6423 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424 */
6425 spin_lock_irqsave(&rq->lock, flags);
6426
Ingo Molnardd41f592007-07-09 18:51:59 +02006427 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006428
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006429 update_rq_clock(rq);
6430 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431
6432 spin_unlock_irqrestore(&rq->lock, flags);
6433}
6434
Ingo Molnar48f24c42006-07-03 00:25:40 -07006435/*
6436 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437 * offline.
6438 */
6439void idle_task_exit(void)
6440{
6441 struct mm_struct *mm = current->active_mm;
6442
6443 BUG_ON(cpu_online(smp_processor_id()));
6444
6445 if (mm != &init_mm)
6446 switch_mm(mm, &init_mm, current);
6447 mmdrop(mm);
6448}
6449
Kirill Korotaev054b9102006-12-10 02:20:11 -08006450/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006451static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006453 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454
6455 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07006456 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457
6458 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07006459 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460
Ingo Molnar48f24c42006-07-03 00:25:40 -07006461 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462
6463 /*
6464 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006465 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 * fine.
6467 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006468 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006469 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006470 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471
Ingo Molnar48f24c42006-07-03 00:25:40 -07006472 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473}
6474
6475/* release_task() removes task from tasklist, so we won't find dead tasks. */
6476static void migrate_dead_tasks(unsigned int dead_cpu)
6477{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006478 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006479 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480
Ingo Molnardd41f592007-07-09 18:51:59 +02006481 for ( ; ; ) {
6482 if (!rq->nr_running)
6483 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02006484 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02006485 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02006486 if (!next)
6487 break;
Dmitry Adamushko79c53792008-06-29 00:16:56 +02006488 next->sched_class->put_prev_task(rq, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02006489 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02006490
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491 }
6492}
6493#endif /* CONFIG_HOTPLUG_CPU */
6494
Nick Piggine692ab52007-07-26 13:40:43 +02006495#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
6496
6497static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006498 {
6499 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006500 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006501 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006502 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006503};
6504
6505static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006506 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006507 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006508 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006509 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006510 .child = sd_ctl_dir,
6511 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006512 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006513};
6514
6515static struct ctl_table *sd_alloc_ctl_entry(int n)
6516{
6517 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02006518 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02006519
Nick Piggine692ab52007-07-26 13:40:43 +02006520 return entry;
6521}
6522
Milton Miller6382bc92007-10-15 17:00:19 +02006523static void sd_free_ctl_entry(struct ctl_table **tablep)
6524{
Milton Millercd7900762007-10-17 16:55:11 +02006525 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02006526
Milton Millercd7900762007-10-17 16:55:11 +02006527 /*
6528 * In the intermediate directories, both the child directory and
6529 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006530 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02006531 * static strings and all have proc handlers.
6532 */
6533 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02006534 if (entry->child)
6535 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02006536 if (entry->proc_handler == NULL)
6537 kfree(entry->procname);
6538 }
Milton Miller6382bc92007-10-15 17:00:19 +02006539
6540 kfree(*tablep);
6541 *tablep = NULL;
6542}
6543
Nick Piggine692ab52007-07-26 13:40:43 +02006544static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02006545set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02006546 const char *procname, void *data, int maxlen,
6547 mode_t mode, proc_handler *proc_handler)
6548{
Nick Piggine692ab52007-07-26 13:40:43 +02006549 entry->procname = procname;
6550 entry->data = data;
6551 entry->maxlen = maxlen;
6552 entry->mode = mode;
6553 entry->proc_handler = proc_handler;
6554}
6555
6556static struct ctl_table *
6557sd_alloc_ctl_domain_table(struct sched_domain *sd)
6558{
Ingo Molnara5d8c342008-10-09 11:35:51 +02006559 struct ctl_table *table = sd_alloc_ctl_entry(13);
Nick Piggine692ab52007-07-26 13:40:43 +02006560
Milton Millerad1cdc12007-10-15 17:00:19 +02006561 if (table == NULL)
6562 return NULL;
6563
Alexey Dobriyane0361852007-08-09 11:16:46 +02006564 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006565 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006566 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006567 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006568 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006569 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006570 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006571 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006572 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006573 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006574 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006575 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006576 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006577 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006578 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02006579 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006580 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02006581 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006582 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02006583 &sd->cache_nice_tries,
6584 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006585 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02006586 sizeof(int), 0644, proc_dointvec_minmax);
Ingo Molnara5d8c342008-10-09 11:35:51 +02006587 set_table_entry(&table[11], "name", sd->name,
6588 CORENAME_MAX_SIZE, 0444, proc_dostring);
6589 /* &table[12] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02006590
6591 return table;
6592}
6593
Ingo Molnar9a4e7152007-11-28 15:52:56 +01006594static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02006595{
6596 struct ctl_table *entry, *table;
6597 struct sched_domain *sd;
6598 int domain_num = 0, i;
6599 char buf[32];
6600
6601 for_each_domain(cpu, sd)
6602 domain_num++;
6603 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02006604 if (table == NULL)
6605 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02006606
6607 i = 0;
6608 for_each_domain(cpu, sd) {
6609 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006610 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006611 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006612 entry->child = sd_alloc_ctl_domain_table(sd);
6613 entry++;
6614 i++;
6615 }
6616 return table;
6617}
6618
6619static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02006620static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006621{
6622 int i, cpu_num = num_online_cpus();
6623 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
6624 char buf[32];
6625
Milton Miller73785472007-10-24 18:23:48 +02006626 WARN_ON(sd_ctl_dir[0].child);
6627 sd_ctl_dir[0].child = entry;
6628
Milton Millerad1cdc12007-10-15 17:00:19 +02006629 if (entry == NULL)
6630 return;
6631
Milton Miller97b6ea72007-10-15 17:00:19 +02006632 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02006633 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006634 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006635 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006636 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02006637 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02006638 }
Milton Miller73785472007-10-24 18:23:48 +02006639
6640 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02006641 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
6642}
Milton Miller6382bc92007-10-15 17:00:19 +02006643
Milton Miller73785472007-10-24 18:23:48 +02006644/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02006645static void unregister_sched_domain_sysctl(void)
6646{
Milton Miller73785472007-10-24 18:23:48 +02006647 if (sd_sysctl_header)
6648 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02006649 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02006650 if (sd_ctl_dir[0].child)
6651 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02006652}
Nick Piggine692ab52007-07-26 13:40:43 +02006653#else
Milton Miller6382bc92007-10-15 17:00:19 +02006654static void register_sched_domain_sysctl(void)
6655{
6656}
6657static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006658{
6659}
6660#endif
6661
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006662static void set_rq_online(struct rq *rq)
6663{
6664 if (!rq->online) {
6665 const struct sched_class *class;
6666
Rusty Russellc6c49272008-11-25 02:35:05 +10306667 cpumask_set_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006668 rq->online = 1;
6669
6670 for_each_class(class) {
6671 if (class->rq_online)
6672 class->rq_online(rq);
6673 }
6674 }
6675}
6676
6677static void set_rq_offline(struct rq *rq)
6678{
6679 if (rq->online) {
6680 const struct sched_class *class;
6681
6682 for_each_class(class) {
6683 if (class->rq_offline)
6684 class->rq_offline(rq);
6685 }
6686
Rusty Russellc6c49272008-11-25 02:35:05 +10306687 cpumask_clear_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006688 rq->online = 0;
6689 }
6690}
6691
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692/*
6693 * migration_call - callback that gets triggered when a CPU is added.
6694 * Here we can start up the necessary migration thread for the new CPU.
6695 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006696static int __cpuinit
6697migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006699 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006700 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006702 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703
6704 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006705
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006707 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02006708 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 if (IS_ERR(p))
6710 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711 kthread_bind(p, cpu);
6712 /* Must be high prio: stop_machine expects to yield to it. */
6713 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02006714 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 task_rq_unlock(rq, &flags);
6716 cpu_rq(cpu)->migration_thread = p;
6717 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006718
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006720 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006721 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006723
6724 /* Update our root-domain */
6725 rq = cpu_rq(cpu);
6726 spin_lock_irqsave(&rq->lock, flags);
6727 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10306728 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006729
6730 set_rq_online(rq);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006731 }
6732 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006734
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735#ifdef CONFIG_HOTPLUG_CPU
6736 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006737 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07006738 if (!cpu_rq(cpu)->migration_thread)
6739 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006740 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08006741 kthread_bind(cpu_rq(cpu)->migration_thread,
Rusty Russell1e5ce4f2008-11-25 02:35:03 +10306742 cpumask_any(cpu_online_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743 kthread_stop(cpu_rq(cpu)->migration_thread);
6744 cpu_rq(cpu)->migration_thread = NULL;
6745 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006746
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006748 case CPU_DEAD_FROZEN:
Cliff Wickman470fd6462007-10-18 23:40:46 -07006749 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 migrate_live_tasks(cpu);
6751 rq = cpu_rq(cpu);
6752 kthread_stop(rq->migration_thread);
6753 rq->migration_thread = NULL;
6754 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006755 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02006756 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006757 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02006759 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
6760 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006762 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd6462007-10-18 23:40:46 -07006763 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764 migrate_nr_uninterruptible(rq);
6765 BUG_ON(rq->nr_running != 0);
6766
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006767 /*
6768 * No need to migrate the tasks: it was best-effort if
6769 * they didn't take sched_hotcpu_mutex. Just wake up
6770 * the requestors.
6771 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772 spin_lock_irq(&rq->lock);
6773 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006774 struct migration_req *req;
6775
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07006777 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778 list_del_init(&req->list);
Brian King9a2bd242008-12-09 08:47:00 -06006779 spin_unlock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006780 complete(&req->done);
Brian King9a2bd242008-12-09 08:47:00 -06006781 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 }
6783 spin_unlock_irq(&rq->lock);
6784 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006785
Gregory Haskins08f503b2008-03-10 17:59:11 -04006786 case CPU_DYING:
6787 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01006788 /* Update our root-domain */
6789 rq = cpu_rq(cpu);
6790 spin_lock_irqsave(&rq->lock, flags);
6791 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10306792 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb62008-06-04 15:04:05 -04006793 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006794 }
6795 spin_unlock_irqrestore(&rq->lock, flags);
6796 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797#endif
6798 }
6799 return NOTIFY_OK;
6800}
6801
6802/* Register at highest priority so that task migration (migrate_all_tasks)
6803 * happens before everything else.
6804 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006805static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806 .notifier_call = migration_call,
6807 .priority = 10
6808};
6809
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07006810static int __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811{
6812 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006813 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006814
6815 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006816 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6817 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6819 register_cpu_notifier(&migration_notifier);
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07006820
6821 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822}
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07006823early_initcall(migration_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824#endif
6825
6826#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006827
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006828#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006829
Mike Travis7c16ec52008-04-04 18:11:11 -07006830static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
Rusty Russell96f874e2008-11-25 02:35:14 +10306831 struct cpumask *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006832{
6833 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07006834 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006835
Rusty Russell968ea6d2008-12-13 21:55:51 +10306836 cpulist_scnprintf(str, sizeof(str), sched_domain_span(sd));
Rusty Russell96f874e2008-11-25 02:35:14 +10306837 cpumask_clear(groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006838
6839 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6840
6841 if (!(sd->flags & SD_LOAD_BALANCE)) {
6842 printk("does not load-balance\n");
6843 if (sd->parent)
6844 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6845 " has parent");
6846 return -1;
6847 }
6848
Li Zefaneefd7962008-11-04 16:15:37 +08006849 printk(KERN_CONT "span %s level %s\n", str, sd->name);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006850
Rusty Russell758b2cd2008-11-25 02:35:04 +10306851 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006852 printk(KERN_ERR "ERROR: domain->span does not contain "
6853 "CPU%d\n", cpu);
6854 }
Rusty Russell758b2cd2008-11-25 02:35:04 +10306855 if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006856 printk(KERN_ERR "ERROR: domain->groups does not contain"
6857 " CPU%d\n", cpu);
6858 }
6859
6860 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6861 do {
6862 if (!group) {
6863 printk("\n");
6864 printk(KERN_ERR "ERROR: group is NULL\n");
6865 break;
6866 }
6867
6868 if (!group->__cpu_power) {
6869 printk(KERN_CONT "\n");
6870 printk(KERN_ERR "ERROR: domain->cpu_power not "
6871 "set\n");
6872 break;
6873 }
6874
Rusty Russell758b2cd2008-11-25 02:35:04 +10306875 if (!cpumask_weight(sched_group_cpus(group))) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006876 printk(KERN_CONT "\n");
6877 printk(KERN_ERR "ERROR: empty group\n");
6878 break;
6879 }
6880
Rusty Russell758b2cd2008-11-25 02:35:04 +10306881 if (cpumask_intersects(groupmask, sched_group_cpus(group))) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006882 printk(KERN_CONT "\n");
6883 printk(KERN_ERR "ERROR: repeated CPUs\n");
6884 break;
6885 }
6886
Rusty Russell758b2cd2008-11-25 02:35:04 +10306887 cpumask_or(groupmask, groupmask, sched_group_cpus(group));
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006888
Rusty Russell968ea6d2008-12-13 21:55:51 +10306889 cpulist_scnprintf(str, sizeof(str), sched_group_cpus(group));
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006890 printk(KERN_CONT " %s", str);
6891
6892 group = group->next;
6893 } while (group != sd->groups);
6894 printk(KERN_CONT "\n");
6895
Rusty Russell758b2cd2008-11-25 02:35:04 +10306896 if (!cpumask_equal(sched_domain_span(sd), groupmask))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006897 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6898
Rusty Russell758b2cd2008-11-25 02:35:04 +10306899 if (sd->parent &&
6900 !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006901 printk(KERN_ERR "ERROR: parent span is not a superset "
6902 "of domain->span\n");
6903 return 0;
6904}
6905
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906static void sched_domain_debug(struct sched_domain *sd, int cpu)
6907{
Rusty Russelld5dd3db2008-11-25 02:35:12 +10306908 cpumask_var_t groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909 int level = 0;
6910
Nick Piggin41c7ce92005-06-25 14:57:24 -07006911 if (!sd) {
6912 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6913 return;
6914 }
6915
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6917
Rusty Russelld5dd3db2008-11-25 02:35:12 +10306918 if (!alloc_cpumask_var(&groupmask, GFP_KERNEL)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006919 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
6920 return;
6921 }
6922
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006923 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006924 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 level++;
6927 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006928 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006929 break;
6930 }
Rusty Russelld5dd3db2008-11-25 02:35:12 +10306931 free_cpumask_var(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006933#else /* !CONFIG_SCHED_DEBUG */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006934# define sched_domain_debug(sd, cpu) do { } while (0)
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006935#endif /* CONFIG_SCHED_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006937static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006938{
Rusty Russell758b2cd2008-11-25 02:35:04 +10306939 if (cpumask_weight(sched_domain_span(sd)) == 1)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006940 return 1;
6941
6942 /* Following flags need at least 2 groups */
6943 if (sd->flags & (SD_LOAD_BALANCE |
6944 SD_BALANCE_NEWIDLE |
6945 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006946 SD_BALANCE_EXEC |
6947 SD_SHARE_CPUPOWER |
6948 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006949 if (sd->groups != sd->groups->next)
6950 return 0;
6951 }
6952
6953 /* Following flags don't use groups */
6954 if (sd->flags & (SD_WAKE_IDLE |
6955 SD_WAKE_AFFINE |
6956 SD_WAKE_BALANCE))
6957 return 0;
6958
6959 return 1;
6960}
6961
Ingo Molnar48f24c42006-07-03 00:25:40 -07006962static int
6963sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006964{
6965 unsigned long cflags = sd->flags, pflags = parent->flags;
6966
6967 if (sd_degenerate(parent))
6968 return 1;
6969
Rusty Russell758b2cd2008-11-25 02:35:04 +10306970 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
Suresh Siddha245af2c2005-06-25 14:57:25 -07006971 return 0;
6972
6973 /* Does parent contain flags not in child? */
6974 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6975 if (cflags & SD_WAKE_AFFINE)
6976 pflags &= ~SD_WAKE_BALANCE;
6977 /* Flags needing groups don't count if only 1 group in parent */
6978 if (parent->groups == parent->groups->next) {
6979 pflags &= ~(SD_LOAD_BALANCE |
6980 SD_BALANCE_NEWIDLE |
6981 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006982 SD_BALANCE_EXEC |
6983 SD_SHARE_CPUPOWER |
6984 SD_SHARE_PKG_RESOURCES);
Ken Chen54364992008-12-07 18:47:37 -08006985 if (nr_node_ids == 1)
6986 pflags &= ~SD_SERIALIZE;
Suresh Siddha245af2c2005-06-25 14:57:25 -07006987 }
6988 if (~cflags & pflags)
6989 return 0;
6990
6991 return 1;
6992}
6993
Rusty Russellc6c49272008-11-25 02:35:05 +10306994static void free_rootdomain(struct root_domain *rd)
6995{
Rusty Russell68e74562008-11-25 02:35:13 +10306996 cpupri_cleanup(&rd->cpupri);
6997
Rusty Russellc6c49272008-11-25 02:35:05 +10306998 free_cpumask_var(rd->rto_mask);
6999 free_cpumask_var(rd->online);
7000 free_cpumask_var(rd->span);
7001 kfree(rd);
7002}
7003
Gregory Haskins57d885f2008-01-25 21:08:18 +01007004static void rq_attach_root(struct rq *rq, struct root_domain *rd)
7005{
7006 unsigned long flags;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007007
7008 spin_lock_irqsave(&rq->lock, flags);
7009
7010 if (rq->rd) {
7011 struct root_domain *old_rd = rq->rd;
7012
Rusty Russellc6c49272008-11-25 02:35:05 +10307013 if (cpumask_test_cpu(rq->cpu, old_rd->online))
Gregory Haskins1f11eb62008-06-04 15:04:05 -04007014 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01007015
Rusty Russellc6c49272008-11-25 02:35:05 +10307016 cpumask_clear_cpu(rq->cpu, old_rd->span);
Gregory Haskinsdc938522008-01-25 21:08:26 +01007017
Gregory Haskins57d885f2008-01-25 21:08:18 +01007018 if (atomic_dec_and_test(&old_rd->refcount))
Rusty Russellc6c49272008-11-25 02:35:05 +10307019 free_rootdomain(old_rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01007020 }
7021
7022 atomic_inc(&rd->refcount);
7023 rq->rd = rd;
7024
Rusty Russellc6c49272008-11-25 02:35:05 +10307025 cpumask_set_cpu(rq->cpu, rd->span);
7026 if (cpumask_test_cpu(rq->cpu, cpu_online_mask))
Gregory Haskins1f11eb62008-06-04 15:04:05 -04007027 set_rq_online(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01007028
7029 spin_unlock_irqrestore(&rq->lock, flags);
7030}
7031
Li Zefandb2f59c2009-01-06 17:40:36 +08007032static int __init_refok init_rootdomain(struct root_domain *rd, bool bootmem)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007033{
7034 memset(rd, 0, sizeof(*rd));
7035
Rusty Russellc6c49272008-11-25 02:35:05 +10307036 if (bootmem) {
7037 alloc_bootmem_cpumask_var(&def_root_domain.span);
7038 alloc_bootmem_cpumask_var(&def_root_domain.online);
7039 alloc_bootmem_cpumask_var(&def_root_domain.rto_mask);
Rusty Russell68e74562008-11-25 02:35:13 +10307040 cpupri_init(&rd->cpupri, true);
Rusty Russellc6c49272008-11-25 02:35:05 +10307041 return 0;
7042 }
Gregory Haskins6e0534f2008-05-12 21:21:01 +02007043
Rusty Russellc6c49272008-11-25 02:35:05 +10307044 if (!alloc_cpumask_var(&rd->span, GFP_KERNEL))
Li Zefan0c910d22009-01-06 17:39:06 +08007045 goto out;
Rusty Russellc6c49272008-11-25 02:35:05 +10307046 if (!alloc_cpumask_var(&rd->online, GFP_KERNEL))
7047 goto free_span;
7048 if (!alloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
7049 goto free_online;
Gregory Haskins6e0534f2008-05-12 21:21:01 +02007050
Rusty Russell68e74562008-11-25 02:35:13 +10307051 if (cpupri_init(&rd->cpupri, false) != 0)
7052 goto free_rto_mask;
Rusty Russellc6c49272008-11-25 02:35:05 +10307053 return 0;
7054
Rusty Russell68e74562008-11-25 02:35:13 +10307055free_rto_mask:
7056 free_cpumask_var(rd->rto_mask);
Rusty Russellc6c49272008-11-25 02:35:05 +10307057free_online:
7058 free_cpumask_var(rd->online);
7059free_span:
7060 free_cpumask_var(rd->span);
Li Zefan0c910d22009-01-06 17:39:06 +08007061out:
Rusty Russellc6c49272008-11-25 02:35:05 +10307062 return -ENOMEM;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007063}
7064
7065static void init_defrootdomain(void)
7066{
Rusty Russellc6c49272008-11-25 02:35:05 +10307067 init_rootdomain(&def_root_domain, true);
7068
Gregory Haskins57d885f2008-01-25 21:08:18 +01007069 atomic_set(&def_root_domain.refcount, 1);
7070}
7071
Gregory Haskinsdc938522008-01-25 21:08:26 +01007072static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007073{
7074 struct root_domain *rd;
7075
7076 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
7077 if (!rd)
7078 return NULL;
7079
Rusty Russellc6c49272008-11-25 02:35:05 +10307080 if (init_rootdomain(rd, false) != 0) {
7081 kfree(rd);
7082 return NULL;
7083 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01007084
7085 return rd;
7086}
7087
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01007089 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090 * hold the hotplug lock.
7091 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01007092static void
7093cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007094{
Ingo Molnar70b97a72006-07-03 00:25:42 -07007095 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07007096 struct sched_domain *tmp;
7097
7098 /* Remove the sched domains which do not contribute to scheduling. */
Li Zefanf29c9b12008-11-06 09:45:16 +08007099 for (tmp = sd; tmp; ) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07007100 struct sched_domain *parent = tmp->parent;
7101 if (!parent)
7102 break;
Li Zefanf29c9b12008-11-06 09:45:16 +08007103
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007104 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07007105 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007106 if (parent->parent)
7107 parent->parent->child = tmp;
Li Zefanf29c9b12008-11-06 09:45:16 +08007108 } else
7109 tmp = tmp->parent;
Suresh Siddha245af2c2005-06-25 14:57:25 -07007110 }
7111
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007112 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07007113 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007114 if (sd)
7115 sd->child = NULL;
7116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117
7118 sched_domain_debug(sd, cpu);
7119
Gregory Haskins57d885f2008-01-25 21:08:18 +01007120 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07007121 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122}
7123
7124/* cpus with isolated domains */
Rusty Russelldcc30a32008-11-25 02:35:12 +10307125static cpumask_var_t cpu_isolated_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126
7127/* Setup the mask of cpus configured for isolated domains */
7128static int __init isolated_cpu_setup(char *str)
7129{
Rusty Russell968ea6d2008-12-13 21:55:51 +10307130 cpulist_parse(str, cpu_isolated_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131 return 1;
7132}
7133
Ingo Molnar8927f492007-10-15 17:00:13 +02007134__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135
7136/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007137 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
7138 * to a function which identifies what group(along with sched group) a CPU
Rusty Russell96f874e2008-11-25 02:35:14 +10307139 * belongs to. The return value of group_fn must be a >= 0 and < nr_cpu_ids
7140 * (due to the fact that we keep track of groups covered with a struct cpumask).
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 *
7142 * init_sched_build_groups will build a circular linked list of the groups
7143 * covered by the given span, and will set each group's ->cpumask correctly,
7144 * and ->cpu_power to 0.
7145 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007146static void
Rusty Russell96f874e2008-11-25 02:35:14 +10307147init_sched_build_groups(const struct cpumask *span,
7148 const struct cpumask *cpu_map,
7149 int (*group_fn)(int cpu, const struct cpumask *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007150 struct sched_group **sg,
Rusty Russell96f874e2008-11-25 02:35:14 +10307151 struct cpumask *tmpmask),
7152 struct cpumask *covered, struct cpumask *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153{
7154 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155 int i;
7156
Rusty Russell96f874e2008-11-25 02:35:14 +10307157 cpumask_clear(covered);
Mike Travis7c16ec52008-04-04 18:11:11 -07007158
Rusty Russellabcd0832008-11-25 02:35:02 +10307159 for_each_cpu(i, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007160 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007161 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162 int j;
7163
Rusty Russell758b2cd2008-11-25 02:35:04 +10307164 if (cpumask_test_cpu(i, covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165 continue;
7166
Rusty Russell758b2cd2008-11-25 02:35:04 +10307167 cpumask_clear(sched_group_cpus(sg));
Eric Dumazet5517d862007-05-08 00:32:57 -07007168 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169
Rusty Russellabcd0832008-11-25 02:35:02 +10307170 for_each_cpu(j, span) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007171 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172 continue;
7173
Rusty Russell96f874e2008-11-25 02:35:14 +10307174 cpumask_set_cpu(j, covered);
Rusty Russell758b2cd2008-11-25 02:35:04 +10307175 cpumask_set_cpu(j, sched_group_cpus(sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176 }
7177 if (!first)
7178 first = sg;
7179 if (last)
7180 last->next = sg;
7181 last = sg;
7182 }
7183 last->next = first;
7184}
7185
John Hawkes9c1cfda2005-09-06 15:18:14 -07007186#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187
John Hawkes9c1cfda2005-09-06 15:18:14 -07007188#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08007189
John Hawkes9c1cfda2005-09-06 15:18:14 -07007190/**
7191 * find_next_best_node - find the next node to include in a sched_domain
7192 * @node: node whose sched_domain we're building
7193 * @used_nodes: nodes already in the sched_domain
7194 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007195 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07007196 * finds the closest node not already in the @used_nodes map.
7197 *
7198 * Should use nodemask_t.
7199 */
Mike Travisc5f59f02008-04-04 18:11:10 -07007200static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007201{
7202 int i, n, val, min_val, best_node = 0;
7203
7204 min_val = INT_MAX;
7205
Mike Travis076ac2a2008-05-12 21:21:12 +02007206 for (i = 0; i < nr_node_ids; i++) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007207 /* Start at @node */
Mike Travis076ac2a2008-05-12 21:21:12 +02007208 n = (node + i) % nr_node_ids;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007209
7210 if (!nr_cpus_node(n))
7211 continue;
7212
7213 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07007214 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007215 continue;
7216
7217 /* Simple min distance search */
7218 val = node_distance(node, n);
7219
7220 if (val < min_val) {
7221 min_val = val;
7222 best_node = n;
7223 }
7224 }
7225
Mike Travisc5f59f02008-04-04 18:11:10 -07007226 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007227 return best_node;
7228}
7229
7230/**
7231 * sched_domain_node_span - get a cpumask for a node's sched_domain
7232 * @node: node whose cpumask we're constructing
Randy Dunlap73486722008-04-22 10:07:22 -07007233 * @span: resulting cpumask
John Hawkes9c1cfda2005-09-06 15:18:14 -07007234 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007235 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07007236 * should be one that prevents unnecessary balancing, but also spreads tasks
7237 * out optimally.
7238 */
Rusty Russell96f874e2008-11-25 02:35:14 +10307239static void sched_domain_node_span(int node, struct cpumask *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007240{
Mike Travisc5f59f02008-04-04 18:11:10 -07007241 nodemask_t used_nodes;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007242 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007243
Mike Travis6ca09df2008-12-31 18:08:45 -08007244 cpumask_clear(span);
Mike Travisc5f59f02008-04-04 18:11:10 -07007245 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007246
Mike Travis6ca09df2008-12-31 18:08:45 -08007247 cpumask_or(span, span, cpumask_of_node(node));
Mike Travisc5f59f02008-04-04 18:11:10 -07007248 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007249
7250 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07007251 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007252
Mike Travis6ca09df2008-12-31 18:08:45 -08007253 cpumask_or(span, span, cpumask_of_node(next_node));
John Hawkes9c1cfda2005-09-06 15:18:14 -07007254 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007255}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007256#endif /* CONFIG_NUMA */
John Hawkes9c1cfda2005-09-06 15:18:14 -07007257
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007258int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007259
John Hawkes9c1cfda2005-09-06 15:18:14 -07007260/*
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307261 * The cpus mask in sched_group and sched_domain hangs off the end.
7262 * FIXME: use cpumask_var_t or dynamic percpu alloc to avoid wasting space
7263 * for nr_cpu_ids < CONFIG_NR_CPUS.
7264 */
7265struct static_sched_group {
7266 struct sched_group sg;
7267 DECLARE_BITMAP(cpus, CONFIG_NR_CPUS);
7268};
7269
7270struct static_sched_domain {
7271 struct sched_domain sd;
7272 DECLARE_BITMAP(span, CONFIG_NR_CPUS);
7273};
7274
7275/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07007276 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07007277 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278#ifdef CONFIG_SCHED_SMT
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307279static DEFINE_PER_CPU(struct static_sched_domain, cpu_domains);
7280static DEFINE_PER_CPU(struct static_sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007281
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007282static int
Rusty Russell96f874e2008-11-25 02:35:14 +10307283cpu_to_cpu_group(int cpu, const struct cpumask *cpu_map,
7284 struct sched_group **sg, struct cpumask *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007286 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307287 *sg = &per_cpu(sched_group_cpus, cpu).sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288 return cpu;
7289}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007290#endif /* CONFIG_SCHED_SMT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291
Ingo Molnar48f24c42006-07-03 00:25:40 -07007292/*
7293 * multi-core sched-domains:
7294 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007295#ifdef CONFIG_SCHED_MC
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307296static DEFINE_PER_CPU(struct static_sched_domain, core_domains);
7297static DEFINE_PER_CPU(struct static_sched_group, sched_group_core);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007298#endif /* CONFIG_SCHED_MC */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007299
7300#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007301static int
Rusty Russell96f874e2008-11-25 02:35:14 +10307302cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
7303 struct sched_group **sg, struct cpumask *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007304{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007305 int group;
Mike Travis7c16ec52008-04-04 18:11:11 -07007306
Rusty Russell96f874e2008-11-25 02:35:14 +10307307 cpumask_and(mask, &per_cpu(cpu_sibling_map, cpu), cpu_map);
7308 group = cpumask_first(mask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007309 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307310 *sg = &per_cpu(sched_group_core, group).sg;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007311 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007312}
7313#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007314static int
Rusty Russell96f874e2008-11-25 02:35:14 +10307315cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
7316 struct sched_group **sg, struct cpumask *unused)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007317{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007318 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307319 *sg = &per_cpu(sched_group_core, cpu).sg;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007320 return cpu;
7321}
7322#endif
7323
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307324static DEFINE_PER_CPU(struct static_sched_domain, phys_domains);
7325static DEFINE_PER_CPU(struct static_sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007326
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007327static int
Rusty Russell96f874e2008-11-25 02:35:14 +10307328cpu_to_phys_group(int cpu, const struct cpumask *cpu_map,
7329 struct sched_group **sg, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007330{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007331 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007332#ifdef CONFIG_SCHED_MC
Mike Travis6ca09df2008-12-31 18:08:45 -08007333 cpumask_and(mask, cpu_coregroup_mask(cpu), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10307334 group = cpumask_first(mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007335#elif defined(CONFIG_SCHED_SMT)
Rusty Russell96f874e2008-11-25 02:35:14 +10307336 cpumask_and(mask, &per_cpu(cpu_sibling_map, cpu), cpu_map);
7337 group = cpumask_first(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007339 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007341 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307342 *sg = &per_cpu(sched_group_phys, group).sg;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007343 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344}
7345
7346#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07007347/*
7348 * The init_sched_build_groups can't handle what we want to do with node
7349 * groups, so roll our own. Now each node has its own list of groups which
7350 * gets dynamically allocated.
7351 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352static DEFINE_PER_CPU(struct sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07007353static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007354
7355static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307356static DEFINE_PER_CPU(struct static_sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007357
Rusty Russell96f874e2008-11-25 02:35:14 +10307358static int cpu_to_allnodes_group(int cpu, const struct cpumask *cpu_map,
7359 struct sched_group **sg,
7360 struct cpumask *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007362 int group;
7363
Mike Travis6ca09df2008-12-31 18:08:45 -08007364 cpumask_and(nodemask, cpumask_of_node(cpu_to_node(cpu)), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10307365 group = cpumask_first(nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007366
7367 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307368 *sg = &per_cpu(sched_group_allnodes, group).sg;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007369 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007371
Siddha, Suresh B08069032006-03-27 01:15:23 -08007372static void init_numa_sched_groups_power(struct sched_group *group_head)
7373{
7374 struct sched_group *sg = group_head;
7375 int j;
7376
7377 if (!sg)
7378 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02007379 do {
Rusty Russell758b2cd2008-11-25 02:35:04 +10307380 for_each_cpu(j, sched_group_cpus(sg)) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02007381 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08007382
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307383 sd = &per_cpu(phys_domains, j).sd;
Rusty Russell758b2cd2008-11-25 02:35:04 +10307384 if (j != cpumask_first(sched_group_cpus(sd->groups))) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02007385 /*
7386 * Only add "power" once for each
7387 * physical package.
7388 */
7389 continue;
7390 }
7391
7392 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007393 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02007394 sg = sg->next;
7395 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007396}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007397#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007399#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007400/* Free memory allocated for various sched_group structures */
Rusty Russell96f874e2008-11-25 02:35:14 +10307401static void free_sched_groups(const struct cpumask *cpu_map,
7402 struct cpumask *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007403{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007404 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007405
Rusty Russellabcd0832008-11-25 02:35:02 +10307406 for_each_cpu(cpu, cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007407 struct sched_group **sched_group_nodes
7408 = sched_group_nodes_bycpu[cpu];
7409
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007410 if (!sched_group_nodes)
7411 continue;
7412
Mike Travis076ac2a2008-05-12 21:21:12 +02007413 for (i = 0; i < nr_node_ids; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007414 struct sched_group *oldsg, *sg = sched_group_nodes[i];
7415
Mike Travis6ca09df2008-12-31 18:08:45 -08007416 cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10307417 if (cpumask_empty(nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007418 continue;
7419
7420 if (sg == NULL)
7421 continue;
7422 sg = sg->next;
7423next_sg:
7424 oldsg = sg;
7425 sg = sg->next;
7426 kfree(oldsg);
7427 if (oldsg != sched_group_nodes[i])
7428 goto next_sg;
7429 }
7430 kfree(sched_group_nodes);
7431 sched_group_nodes_bycpu[cpu] = NULL;
7432 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007433}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007434#else /* !CONFIG_NUMA */
Rusty Russell96f874e2008-11-25 02:35:14 +10307435static void free_sched_groups(const struct cpumask *cpu_map,
7436 struct cpumask *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007437{
7438}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007439#endif /* CONFIG_NUMA */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007440
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007442 * Initialize sched groups cpu_power.
7443 *
7444 * cpu_power indicates the capacity of sched group, which is used while
7445 * distributing the load between different sched groups in a sched domain.
7446 * Typically cpu_power for all the groups in a sched domain will be same unless
7447 * there are asymmetries in the topology. If there are asymmetries, group
7448 * having more cpu_power will pickup more load compared to the group having
7449 * less cpu_power.
7450 *
7451 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
7452 * the maximum number of tasks a group can handle in the presence of other idle
7453 * or lightly loaded groups in the same sched domain.
7454 */
7455static void init_sched_groups_power(int cpu, struct sched_domain *sd)
7456{
7457 struct sched_domain *child;
7458 struct sched_group *group;
7459
7460 WARN_ON(!sd || !sd->groups);
7461
Rusty Russell758b2cd2008-11-25 02:35:04 +10307462 if (cpu != cpumask_first(sched_group_cpus(sd->groups)))
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007463 return;
7464
7465 child = sd->child;
7466
Eric Dumazet5517d862007-05-08 00:32:57 -07007467 sd->groups->__cpu_power = 0;
7468
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007469 /*
7470 * For perf policy, if the groups in child domain share resources
7471 * (for example cores sharing some portions of the cache hierarchy
7472 * or SMT), then set this domain groups cpu_power such that each group
7473 * can handle only one task, when there are other idle groups in the
7474 * same sched domain.
7475 */
7476 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
7477 (child->flags &
7478 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07007479 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007480 return;
7481 }
7482
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007483 /*
7484 * add cpu_power of each child group to this groups cpu_power
7485 */
7486 group = child->groups;
7487 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07007488 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007489 group = group->next;
7490 } while (group != child->groups);
7491}
7492
7493/*
Mike Travis7c16ec52008-04-04 18:11:11 -07007494 * Initializers for schedule domains
7495 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7496 */
7497
Ingo Molnara5d8c342008-10-09 11:35:51 +02007498#ifdef CONFIG_SCHED_DEBUG
7499# define SD_INIT_NAME(sd, type) sd->name = #type
7500#else
7501# define SD_INIT_NAME(sd, type) do { } while (0)
7502#endif
7503
Mike Travis7c16ec52008-04-04 18:11:11 -07007504#define SD_INIT(sd, type) sd_init_##type(sd)
Ingo Molnara5d8c342008-10-09 11:35:51 +02007505
Mike Travis7c16ec52008-04-04 18:11:11 -07007506#define SD_INIT_FUNC(type) \
7507static noinline void sd_init_##type(struct sched_domain *sd) \
7508{ \
7509 memset(sd, 0, sizeof(*sd)); \
7510 *sd = SD_##type##_INIT; \
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007511 sd->level = SD_LV_##type; \
Ingo Molnara5d8c342008-10-09 11:35:51 +02007512 SD_INIT_NAME(sd, type); \
Mike Travis7c16ec52008-04-04 18:11:11 -07007513}
7514
7515SD_INIT_FUNC(CPU)
7516#ifdef CONFIG_NUMA
7517 SD_INIT_FUNC(ALLNODES)
7518 SD_INIT_FUNC(NODE)
7519#endif
7520#ifdef CONFIG_SCHED_SMT
7521 SD_INIT_FUNC(SIBLING)
7522#endif
7523#ifdef CONFIG_SCHED_MC
7524 SD_INIT_FUNC(MC)
7525#endif
7526
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007527static int default_relax_domain_level = -1;
7528
7529static int __init setup_relax_domain_level(char *str)
7530{
Li Zefan30e0e172008-05-13 10:27:17 +08007531 unsigned long val;
7532
7533 val = simple_strtoul(str, NULL, 0);
7534 if (val < SD_LV_MAX)
7535 default_relax_domain_level = val;
7536
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007537 return 1;
7538}
7539__setup("relax_domain_level=", setup_relax_domain_level);
7540
7541static void set_domain_attribute(struct sched_domain *sd,
7542 struct sched_domain_attr *attr)
7543{
7544 int request;
7545
7546 if (!attr || attr->relax_domain_level < 0) {
7547 if (default_relax_domain_level < 0)
7548 return;
7549 else
7550 request = default_relax_domain_level;
7551 } else
7552 request = attr->relax_domain_level;
7553 if (request < sd->level) {
7554 /* turn off idle balance on this domain */
7555 sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
7556 } else {
7557 /* turn on idle balance on this domain */
7558 sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
7559 }
7560}
7561
Mike Travis7c16ec52008-04-04 18:11:11 -07007562/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007563 * Build sched domains for a given set of cpus and attach the sched domains
7564 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07007565 */
Rusty Russell96f874e2008-11-25 02:35:14 +10307566static int __build_sched_domains(const struct cpumask *cpu_map,
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007567 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568{
Rusty Russell3404c8d2008-11-25 02:35:03 +10307569 int i, err = -ENOMEM;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007570 struct root_domain *rd;
Rusty Russell3404c8d2008-11-25 02:35:03 +10307571 cpumask_var_t nodemask, this_sibling_map, this_core_map, send_covered,
7572 tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07007573#ifdef CONFIG_NUMA
Rusty Russell3404c8d2008-11-25 02:35:03 +10307574 cpumask_var_t domainspan, covered, notcovered;
John Hawkesd1b55132005-09-06 15:18:14 -07007575 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007576 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07007577
Rusty Russell3404c8d2008-11-25 02:35:03 +10307578 if (!alloc_cpumask_var(&domainspan, GFP_KERNEL))
7579 goto out;
7580 if (!alloc_cpumask_var(&covered, GFP_KERNEL))
7581 goto free_domainspan;
7582 if (!alloc_cpumask_var(&notcovered, GFP_KERNEL))
7583 goto free_covered;
7584#endif
7585
7586 if (!alloc_cpumask_var(&nodemask, GFP_KERNEL))
7587 goto free_notcovered;
7588 if (!alloc_cpumask_var(&this_sibling_map, GFP_KERNEL))
7589 goto free_nodemask;
7590 if (!alloc_cpumask_var(&this_core_map, GFP_KERNEL))
7591 goto free_this_sibling_map;
7592 if (!alloc_cpumask_var(&send_covered, GFP_KERNEL))
7593 goto free_this_core_map;
7594 if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
7595 goto free_send_covered;
7596
7597#ifdef CONFIG_NUMA
John Hawkesd1b55132005-09-06 15:18:14 -07007598 /*
7599 * Allocate the per-node list of sched groups
7600 */
Mike Travis076ac2a2008-05-12 21:21:12 +02007601 sched_group_nodes = kcalloc(nr_node_ids, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007602 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07007603 if (!sched_group_nodes) {
7604 printk(KERN_WARNING "Can not alloc sched group node list\n");
Rusty Russell3404c8d2008-11-25 02:35:03 +10307605 goto free_tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07007606 }
John Hawkesd1b55132005-09-06 15:18:14 -07007607#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007608
Gregory Haskinsdc938522008-01-25 21:08:26 +01007609 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01007610 if (!rd) {
7611 printk(KERN_WARNING "Cannot alloc root domain\n");
Rusty Russell3404c8d2008-11-25 02:35:03 +10307612 goto free_sched_groups;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007613 }
7614
Mike Travis7c16ec52008-04-04 18:11:11 -07007615#ifdef CONFIG_NUMA
Rusty Russell96f874e2008-11-25 02:35:14 +10307616 sched_group_nodes_bycpu[cpumask_first(cpu_map)] = sched_group_nodes;
Mike Travis7c16ec52008-04-04 18:11:11 -07007617#endif
7618
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007620 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007621 */
Rusty Russellabcd0832008-11-25 02:35:02 +10307622 for_each_cpu(i, cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623 struct sched_domain *sd = NULL, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007624
Mike Travis6ca09df2008-12-31 18:08:45 -08007625 cpumask_and(nodemask, cpumask_of_node(cpu_to_node(i)), cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007626
7627#ifdef CONFIG_NUMA
Rusty Russell96f874e2008-11-25 02:35:14 +10307628 if (cpumask_weight(cpu_map) >
7629 SD_NODES_PER_DOMAIN*cpumask_weight(nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007630 sd = &per_cpu(allnodes_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007631 SD_INIT(sd, ALLNODES);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007632 set_domain_attribute(sd, attr);
Rusty Russell758b2cd2008-11-25 02:35:04 +10307633 cpumask_copy(sched_domain_span(sd), cpu_map);
Mike Travis7c16ec52008-04-04 18:11:11 -07007634 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007635 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007636 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007637 } else
7638 p = NULL;
7639
Linus Torvalds1da177e2005-04-16 15:20:36 -07007640 sd = &per_cpu(node_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007641 SD_INIT(sd, NODE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007642 set_domain_attribute(sd, attr);
Rusty Russell758b2cd2008-11-25 02:35:04 +10307643 sched_domain_node_span(cpu_to_node(i), sched_domain_span(sd));
John Hawkes9c1cfda2005-09-06 15:18:14 -07007644 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007645 if (p)
7646 p->child = sd;
Rusty Russell758b2cd2008-11-25 02:35:04 +10307647 cpumask_and(sched_domain_span(sd),
7648 sched_domain_span(sd), cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007649#endif
7650
7651 p = sd;
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307652 sd = &per_cpu(phys_domains, i).sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007653 SD_INIT(sd, CPU);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007654 set_domain_attribute(sd, attr);
Rusty Russell758b2cd2008-11-25 02:35:04 +10307655 cpumask_copy(sched_domain_span(sd), nodemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007657 if (p)
7658 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007659 cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007661#ifdef CONFIG_SCHED_MC
7662 p = sd;
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307663 sd = &per_cpu(core_domains, i).sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007664 SD_INIT(sd, MC);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007665 set_domain_attribute(sd, attr);
Mike Travis6ca09df2008-12-31 18:08:45 -08007666 cpumask_and(sched_domain_span(sd), cpu_map,
7667 cpu_coregroup_mask(i));
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007668 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007669 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007670 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007671#endif
7672
Linus Torvalds1da177e2005-04-16 15:20:36 -07007673#ifdef CONFIG_SCHED_SMT
7674 p = sd;
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307675 sd = &per_cpu(cpu_domains, i).sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007676 SD_INIT(sd, SIBLING);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007677 set_domain_attribute(sd, attr);
Rusty Russell758b2cd2008-11-25 02:35:04 +10307678 cpumask_and(sched_domain_span(sd),
7679 &per_cpu(cpu_sibling_map, i), cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007680 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007681 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007682 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007683#endif
7684 }
7685
7686#ifdef CONFIG_SCHED_SMT
7687 /* Set up CPU (sibling) groups */
Rusty Russellabcd0832008-11-25 02:35:02 +10307688 for_each_cpu(i, cpu_map) {
Rusty Russell96f874e2008-11-25 02:35:14 +10307689 cpumask_and(this_sibling_map,
7690 &per_cpu(cpu_sibling_map, i), cpu_map);
7691 if (i != cpumask_first(this_sibling_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007692 continue;
7693
Ingo Molnardd41f592007-07-09 18:51:59 +02007694 init_sched_build_groups(this_sibling_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007695 &cpu_to_cpu_group,
7696 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697 }
7698#endif
7699
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007700#ifdef CONFIG_SCHED_MC
7701 /* Set up multi-core groups */
Rusty Russellabcd0832008-11-25 02:35:02 +10307702 for_each_cpu(i, cpu_map) {
Mike Travis6ca09df2008-12-31 18:08:45 -08007703 cpumask_and(this_core_map, cpu_coregroup_mask(i), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10307704 if (i != cpumask_first(this_core_map))
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007705 continue;
Mike Travis7c16ec52008-04-04 18:11:11 -07007706
Ingo Molnardd41f592007-07-09 18:51:59 +02007707 init_sched_build_groups(this_core_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007708 &cpu_to_core_group,
7709 send_covered, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007710 }
7711#endif
7712
Linus Torvalds1da177e2005-04-16 15:20:36 -07007713 /* Set up physical groups */
Mike Travis076ac2a2008-05-12 21:21:12 +02007714 for (i = 0; i < nr_node_ids; i++) {
Mike Travis6ca09df2008-12-31 18:08:45 -08007715 cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10307716 if (cpumask_empty(nodemask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717 continue;
7718
Mike Travis7c16ec52008-04-04 18:11:11 -07007719 init_sched_build_groups(nodemask, cpu_map,
7720 &cpu_to_phys_group,
7721 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007722 }
7723
7724#ifdef CONFIG_NUMA
7725 /* Set up node groups */
Mike Travis7c16ec52008-04-04 18:11:11 -07007726 if (sd_allnodes) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007727 init_sched_build_groups(cpu_map, cpu_map,
7728 &cpu_to_allnodes_group,
7729 send_covered, tmpmask);
7730 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007731
Mike Travis076ac2a2008-05-12 21:21:12 +02007732 for (i = 0; i < nr_node_ids; i++) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007733 /* Set up node groups */
7734 struct sched_group *sg, *prev;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007735 int j;
7736
Rusty Russell96f874e2008-11-25 02:35:14 +10307737 cpumask_clear(covered);
Mike Travis6ca09df2008-12-31 18:08:45 -08007738 cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
Rusty Russell96f874e2008-11-25 02:35:14 +10307739 if (cpumask_empty(nodemask)) {
John Hawkesd1b55132005-09-06 15:18:14 -07007740 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007741 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07007742 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007743
Mike Travis4bdbaad32008-04-15 16:35:52 -07007744 sched_domain_node_span(i, domainspan);
Rusty Russell96f874e2008-11-25 02:35:14 +10307745 cpumask_and(domainspan, domainspan, cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007746
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307747 sg = kmalloc_node(sizeof(struct sched_group) + cpumask_size(),
7748 GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007749 if (!sg) {
7750 printk(KERN_WARNING "Can not alloc domain group for "
7751 "node %d\n", i);
7752 goto error;
7753 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007754 sched_group_nodes[i] = sg;
Rusty Russellabcd0832008-11-25 02:35:02 +10307755 for_each_cpu(j, nodemask) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007756 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02007757
John Hawkes9c1cfda2005-09-06 15:18:14 -07007758 sd = &per_cpu(node_domains, j);
7759 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007760 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007761 sg->__cpu_power = 0;
Rusty Russell758b2cd2008-11-25 02:35:04 +10307762 cpumask_copy(sched_group_cpus(sg), nodemask);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007763 sg->next = sg;
Rusty Russell96f874e2008-11-25 02:35:14 +10307764 cpumask_or(covered, covered, nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007765 prev = sg;
7766
Mike Travis076ac2a2008-05-12 21:21:12 +02007767 for (j = 0; j < nr_node_ids; j++) {
Mike Travis076ac2a2008-05-12 21:21:12 +02007768 int n = (i + j) % nr_node_ids;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007769
Rusty Russell96f874e2008-11-25 02:35:14 +10307770 cpumask_complement(notcovered, covered);
7771 cpumask_and(tmpmask, notcovered, cpu_map);
7772 cpumask_and(tmpmask, tmpmask, domainspan);
7773 if (cpumask_empty(tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007774 break;
7775
Mike Travis6ca09df2008-12-31 18:08:45 -08007776 cpumask_and(tmpmask, tmpmask, cpumask_of_node(n));
Rusty Russell96f874e2008-11-25 02:35:14 +10307777 if (cpumask_empty(tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007778 continue;
7779
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307780 sg = kmalloc_node(sizeof(struct sched_group) +
7781 cpumask_size(),
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007782 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007783 if (!sg) {
7784 printk(KERN_WARNING
7785 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007786 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007787 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007788 sg->__cpu_power = 0;
Rusty Russell758b2cd2008-11-25 02:35:04 +10307789 cpumask_copy(sched_group_cpus(sg), tmpmask);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007790 sg->next = prev->next;
Rusty Russell96f874e2008-11-25 02:35:14 +10307791 cpumask_or(covered, covered, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007792 prev->next = sg;
7793 prev = sg;
7794 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007796#endif
7797
7798 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007799#ifdef CONFIG_SCHED_SMT
Rusty Russellabcd0832008-11-25 02:35:02 +10307800 for_each_cpu(i, cpu_map) {
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307801 struct sched_domain *sd = &per_cpu(cpu_domains, i).sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02007802
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007803 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007804 }
7805#endif
7806#ifdef CONFIG_SCHED_MC
Rusty Russellabcd0832008-11-25 02:35:02 +10307807 for_each_cpu(i, cpu_map) {
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307808 struct sched_domain *sd = &per_cpu(core_domains, i).sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02007809
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007810 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007811 }
7812#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007813
Rusty Russellabcd0832008-11-25 02:35:02 +10307814 for_each_cpu(i, cpu_map) {
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307815 struct sched_domain *sd = &per_cpu(phys_domains, i).sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02007816
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007817 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007818 }
7819
John Hawkes9c1cfda2005-09-06 15:18:14 -07007820#ifdef CONFIG_NUMA
Mike Travis076ac2a2008-05-12 21:21:12 +02007821 for (i = 0; i < nr_node_ids; i++)
Siddha, Suresh B08069032006-03-27 01:15:23 -08007822 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007823
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007824 if (sd_allnodes) {
7825 struct sched_group *sg;
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07007826
Rusty Russell96f874e2008-11-25 02:35:14 +10307827 cpu_to_allnodes_group(cpumask_first(cpu_map), cpu_map, &sg,
Mike Travis7c16ec52008-04-04 18:11:11 -07007828 tmpmask);
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07007829 init_numa_sched_groups_power(sg);
7830 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007831#endif
7832
Linus Torvalds1da177e2005-04-16 15:20:36 -07007833 /* Attach the domains */
Rusty Russellabcd0832008-11-25 02:35:02 +10307834 for_each_cpu(i, cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835 struct sched_domain *sd;
7836#ifdef CONFIG_SCHED_SMT
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307837 sd = &per_cpu(cpu_domains, i).sd;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007838#elif defined(CONFIG_SCHED_MC)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307839 sd = &per_cpu(core_domains, i).sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007840#else
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307841 sd = &per_cpu(phys_domains, i).sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007842#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007843 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007844 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007845
Rusty Russell3404c8d2008-11-25 02:35:03 +10307846 err = 0;
7847
7848free_tmpmask:
7849 free_cpumask_var(tmpmask);
7850free_send_covered:
7851 free_cpumask_var(send_covered);
7852free_this_core_map:
7853 free_cpumask_var(this_core_map);
7854free_this_sibling_map:
7855 free_cpumask_var(this_sibling_map);
7856free_nodemask:
7857 free_cpumask_var(nodemask);
7858free_notcovered:
7859#ifdef CONFIG_NUMA
7860 free_cpumask_var(notcovered);
7861free_covered:
7862 free_cpumask_var(covered);
7863free_domainspan:
7864 free_cpumask_var(domainspan);
7865out:
7866#endif
7867 return err;
7868
7869free_sched_groups:
7870#ifdef CONFIG_NUMA
7871 kfree(sched_group_nodes);
7872#endif
7873 goto free_tmpmask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007874
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007875#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007876error:
Mike Travis7c16ec52008-04-04 18:11:11 -07007877 free_sched_groups(cpu_map, tmpmask);
Rusty Russellc6c49272008-11-25 02:35:05 +10307878 free_rootdomain(rd);
Rusty Russell3404c8d2008-11-25 02:35:03 +10307879 goto free_tmpmask;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007880#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007881}
Paul Jackson029190c2007-10-18 23:40:20 -07007882
Rusty Russell96f874e2008-11-25 02:35:14 +10307883static int build_sched_domains(const struct cpumask *cpu_map)
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007884{
7885 return __build_sched_domains(cpu_map, NULL);
7886}
7887
Rusty Russell96f874e2008-11-25 02:35:14 +10307888static struct cpumask *doms_cur; /* current sched domains */
Paul Jackson029190c2007-10-18 23:40:20 -07007889static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Ingo Molnar4285f5942008-05-16 17:47:14 +02007890static struct sched_domain_attr *dattr_cur;
7891 /* attribues of custom domains in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007892
7893/*
7894 * Special case: If a kmalloc of a doms_cur partition (array of
Rusty Russell42128232008-11-25 02:35:12 +10307895 * cpumask) fails, then fallback to a single sched domain,
7896 * as determined by the single cpumask fallback_doms.
Paul Jackson029190c2007-10-18 23:40:20 -07007897 */
Rusty Russell42128232008-11-25 02:35:12 +10307898static cpumask_var_t fallback_doms;
Paul Jackson029190c2007-10-18 23:40:20 -07007899
Heiko Carstensee79d1b2008-12-09 18:49:50 +01007900/*
7901 * arch_update_cpu_topology lets virtualized architectures update the
7902 * cpu core maps. It is supposed to return 1 if the topology changed
7903 * or 0 if it stayed the same.
7904 */
7905int __attribute__((weak)) arch_update_cpu_topology(void)
Heiko Carstens22e52b02008-03-12 18:31:59 +01007906{
Heiko Carstensee79d1b2008-12-09 18:49:50 +01007907 return 0;
Heiko Carstens22e52b02008-03-12 18:31:59 +01007908}
7909
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007910/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007911 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007912 * For now this just excludes isolated cpus, but could be used to
7913 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007914 */
Rusty Russell96f874e2008-11-25 02:35:14 +10307915static int arch_init_sched_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007916{
Milton Miller73785472007-10-24 18:23:48 +02007917 int err;
7918
Heiko Carstens22e52b02008-03-12 18:31:59 +01007919 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007920 ndoms_cur = 1;
Rusty Russell96f874e2008-11-25 02:35:14 +10307921 doms_cur = kmalloc(cpumask_size(), GFP_KERNEL);
Paul Jackson029190c2007-10-18 23:40:20 -07007922 if (!doms_cur)
Rusty Russell42128232008-11-25 02:35:12 +10307923 doms_cur = fallback_doms;
Rusty Russelldcc30a32008-11-25 02:35:12 +10307924 cpumask_andnot(doms_cur, cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007925 dattr_cur = NULL;
Milton Miller73785472007-10-24 18:23:48 +02007926 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02007927 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007928
7929 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007930}
7931
Rusty Russell96f874e2008-11-25 02:35:14 +10307932static void arch_destroy_sched_domains(const struct cpumask *cpu_map,
7933 struct cpumask *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007934{
Mike Travis7c16ec52008-04-04 18:11:11 -07007935 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007936}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007937
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007938/*
7939 * Detach sched domains from a group of cpus specified in cpu_map
7940 * These cpus will now be attached to the NULL domain
7941 */
Rusty Russell96f874e2008-11-25 02:35:14 +10307942static void detach_destroy_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007943{
Rusty Russell96f874e2008-11-25 02:35:14 +10307944 /* Save because hotplug lock held. */
7945 static DECLARE_BITMAP(tmpmask, CONFIG_NR_CPUS);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007946 int i;
7947
Rusty Russellabcd0832008-11-25 02:35:02 +10307948 for_each_cpu(i, cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007949 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007950 synchronize_sched();
Rusty Russell96f874e2008-11-25 02:35:14 +10307951 arch_destroy_sched_domains(cpu_map, to_cpumask(tmpmask));
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007952}
7953
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007954/* handle null as "default" */
7955static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7956 struct sched_domain_attr *new, int idx_new)
7957{
7958 struct sched_domain_attr tmp;
7959
7960 /* fast path */
7961 if (!new && !cur)
7962 return 1;
7963
7964 tmp = SD_ATTR_INIT;
7965 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7966 new ? (new + idx_new) : &tmp,
7967 sizeof(struct sched_domain_attr));
7968}
7969
Paul Jackson029190c2007-10-18 23:40:20 -07007970/*
7971 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007972 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007973 * doms_new[] to the current sched domain partitioning, doms_cur[].
7974 * It destroys each deleted domain and builds each new domain.
7975 *
Rusty Russell96f874e2008-11-25 02:35:14 +10307976 * 'doms_new' is an array of cpumask's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007977 * The masks don't intersect (don't overlap.) We should setup one
7978 * sched domain for each mask. CPUs not in any of the cpumasks will
7979 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007980 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7981 * it as it is.
7982 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007983 * The passed in 'doms_new' should be kmalloc'd. This routine takes
7984 * ownership of it and will kfree it when done with it. If the caller
Li Zefan700018e2008-11-18 14:02:03 +08007985 * failed the kmalloc call, then it can pass in doms_new == NULL &&
7986 * ndoms_new == 1, and partition_sched_domains() will fallback to
7987 * the single partition 'fallback_doms', it also forces the domains
7988 * to be rebuilt.
Paul Jackson029190c2007-10-18 23:40:20 -07007989 *
Rusty Russell96f874e2008-11-25 02:35:14 +10307990 * If doms_new == NULL it will be replaced with cpu_online_mask.
Li Zefan700018e2008-11-18 14:02:03 +08007991 * ndoms_new == 0 is a special case for destroying existing domains,
7992 * and it will not create the default domain.
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007993 *
Paul Jackson029190c2007-10-18 23:40:20 -07007994 * Call with hotplug lock held
7995 */
Rusty Russell96f874e2008-11-25 02:35:14 +10307996/* FIXME: Change to struct cpumask *doms_new[] */
7997void partition_sched_domains(int ndoms_new, struct cpumask *doms_new,
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007998 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007999{
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07008000 int i, j, n;
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01008001 int new_topology;
Paul Jackson029190c2007-10-18 23:40:20 -07008002
Heiko Carstens712555e2008-04-28 11:33:07 +02008003 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01008004
Milton Miller73785472007-10-24 18:23:48 +02008005 /* always unregister in case we don't destroy any domains */
8006 unregister_sched_domain_sysctl();
8007
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01008008 /* Let architecture update cpu core mappings. */
8009 new_topology = arch_update_cpu_topology();
8010
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07008011 n = doms_new ? ndoms_new : 0;
Paul Jackson029190c2007-10-18 23:40:20 -07008012
8013 /* Destroy deleted domains */
8014 for (i = 0; i < ndoms_cur; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01008015 for (j = 0; j < n && !new_topology; j++) {
Rusty Russell96f874e2008-11-25 02:35:14 +10308016 if (cpumask_equal(&doms_cur[i], &doms_new[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008017 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07008018 goto match1;
8019 }
8020 /* no match - a current sched domain not in new doms_new[] */
8021 detach_destroy_domains(doms_cur + i);
8022match1:
8023 ;
8024 }
8025
Max Krasnyanskye761b772008-07-15 04:43:49 -07008026 if (doms_new == NULL) {
8027 ndoms_cur = 0;
Rusty Russell42128232008-11-25 02:35:12 +10308028 doms_new = fallback_doms;
Rusty Russelldcc30a32008-11-25 02:35:12 +10308029 cpumask_andnot(&doms_new[0], cpu_online_mask, cpu_isolated_map);
Li Zefanfaa2f982008-11-04 16:20:23 +08008030 WARN_ON_ONCE(dattr_new);
Max Krasnyanskye761b772008-07-15 04:43:49 -07008031 }
8032
Paul Jackson029190c2007-10-18 23:40:20 -07008033 /* Build new domains */
8034 for (i = 0; i < ndoms_new; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01008035 for (j = 0; j < ndoms_cur && !new_topology; j++) {
Rusty Russell96f874e2008-11-25 02:35:14 +10308036 if (cpumask_equal(&doms_new[i], &doms_cur[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008037 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07008038 goto match2;
8039 }
8040 /* no match - add a new doms_new */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008041 __build_sched_domains(doms_new + i,
8042 dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07008043match2:
8044 ;
8045 }
8046
8047 /* Remember the new sched domains */
Rusty Russell42128232008-11-25 02:35:12 +10308048 if (doms_cur != fallback_doms)
Paul Jackson029190c2007-10-18 23:40:20 -07008049 kfree(doms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008050 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07008051 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09008052 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07008053 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02008054
8055 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01008056
Heiko Carstens712555e2008-04-28 11:33:07 +02008057 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07008058}
8059
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008060#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Li Zefanc70f22d2009-01-05 19:07:50 +08008061static void arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008062{
Gautham R Shenoy95402b32008-01-25 21:08:02 +01008063 get_online_cpus();
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07008064
8065 /* Destroy domains first to force the rebuild */
8066 partition_sched_domains(0, NULL, NULL);
8067
Max Krasnyanskye761b772008-07-15 04:43:49 -07008068 rebuild_sched_domains();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01008069 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008070}
8071
8072static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
8073{
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05308074 unsigned int level = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008075
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05308076 if (sscanf(buf, "%u", &level) != 1)
8077 return -EINVAL;
8078
8079 /*
8080 * level is always be positive so don't check for
8081 * level < POWERSAVINGS_BALANCE_NONE which is 0
8082 * What happens on 0 or 1 byte write,
8083 * need to check for count as well?
8084 */
8085
8086 if (level >= MAX_POWERSAVINGS_BALANCE_LEVELS)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008087 return -EINVAL;
8088
8089 if (smt)
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05308090 sched_smt_power_savings = level;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008091 else
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05308092 sched_mc_power_savings = level;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008093
Li Zefanc70f22d2009-01-05 19:07:50 +08008094 arch_reinit_sched_domains();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008095
Li Zefanc70f22d2009-01-05 19:07:50 +08008096 return count;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008097}
8098
Adrian Bunk6707de002007-08-12 18:08:19 +02008099#ifdef CONFIG_SCHED_MC
Andi Kleenf718cd42008-07-29 22:33:52 -07008100static ssize_t sched_mc_power_savings_show(struct sysdev_class *class,
8101 char *page)
Adrian Bunk6707de002007-08-12 18:08:19 +02008102{
8103 return sprintf(page, "%u\n", sched_mc_power_savings);
8104}
Andi Kleenf718cd42008-07-29 22:33:52 -07008105static ssize_t sched_mc_power_savings_store(struct sysdev_class *class,
Adrian Bunk6707de002007-08-12 18:08:19 +02008106 const char *buf, size_t count)
8107{
8108 return sched_power_savings_store(buf, count, 0);
8109}
Andi Kleenf718cd42008-07-29 22:33:52 -07008110static SYSDEV_CLASS_ATTR(sched_mc_power_savings, 0644,
8111 sched_mc_power_savings_show,
8112 sched_mc_power_savings_store);
Adrian Bunk6707de002007-08-12 18:08:19 +02008113#endif
8114
8115#ifdef CONFIG_SCHED_SMT
Andi Kleenf718cd42008-07-29 22:33:52 -07008116static ssize_t sched_smt_power_savings_show(struct sysdev_class *dev,
8117 char *page)
Adrian Bunk6707de002007-08-12 18:08:19 +02008118{
8119 return sprintf(page, "%u\n", sched_smt_power_savings);
8120}
Andi Kleenf718cd42008-07-29 22:33:52 -07008121static ssize_t sched_smt_power_savings_store(struct sysdev_class *dev,
Adrian Bunk6707de002007-08-12 18:08:19 +02008122 const char *buf, size_t count)
8123{
8124 return sched_power_savings_store(buf, count, 1);
8125}
Andi Kleenf718cd42008-07-29 22:33:52 -07008126static SYSDEV_CLASS_ATTR(sched_smt_power_savings, 0644,
8127 sched_smt_power_savings_show,
Adrian Bunk6707de002007-08-12 18:08:19 +02008128 sched_smt_power_savings_store);
8129#endif
8130
Li Zefan39aac642009-01-05 19:18:02 +08008131int __init sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008132{
8133 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07008134
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008135#ifdef CONFIG_SCHED_SMT
8136 if (smt_capable())
8137 err = sysfs_create_file(&cls->kset.kobj,
8138 &attr_sched_smt_power_savings.attr);
8139#endif
8140#ifdef CONFIG_SCHED_MC
8141 if (!err && mc_capable())
8142 err = sysfs_create_file(&cls->kset.kobj,
8143 &attr_sched_mc_power_savings.attr);
8144#endif
8145 return err;
8146}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008147#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008148
Max Krasnyanskye761b772008-07-15 04:43:49 -07008149#ifndef CONFIG_CPUSETS
Linus Torvalds1da177e2005-04-16 15:20:36 -07008150/*
Max Krasnyanskye761b772008-07-15 04:43:49 -07008151 * Add online and remove offline CPUs from the scheduler domains.
8152 * When cpusets are enabled they take over this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008153 */
8154static int update_sched_domains(struct notifier_block *nfb,
8155 unsigned long action, void *hcpu)
8156{
Max Krasnyanskye761b772008-07-15 04:43:49 -07008157 switch (action) {
8158 case CPU_ONLINE:
8159 case CPU_ONLINE_FROZEN:
8160 case CPU_DEAD:
8161 case CPU_DEAD_FROZEN:
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07008162 partition_sched_domains(1, NULL, NULL);
Max Krasnyanskye761b772008-07-15 04:43:49 -07008163 return NOTIFY_OK;
8164
8165 default:
8166 return NOTIFY_DONE;
8167 }
8168}
8169#endif
8170
8171static int update_runtime(struct notifier_block *nfb,
8172 unsigned long action, void *hcpu)
8173{
Peter Zijlstra7def2be2008-06-05 14:49:58 +02008174 int cpu = (int)(long)hcpu;
8175
Linus Torvalds1da177e2005-04-16 15:20:36 -07008176 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008177 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07008178 case CPU_DOWN_PREPARE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02008179 disable_runtime(cpu_rq(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008180 return NOTIFY_OK;
8181
Linus Torvalds1da177e2005-04-16 15:20:36 -07008182 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07008183 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008184 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07008185 case CPU_ONLINE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02008186 enable_runtime(cpu_rq(cpu));
Max Krasnyanskye761b772008-07-15 04:43:49 -07008187 return NOTIFY_OK;
8188
Linus Torvalds1da177e2005-04-16 15:20:36 -07008189 default:
8190 return NOTIFY_DONE;
8191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008192}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008193
8194void __init sched_init_smp(void)
8195{
Rusty Russelldcc30a32008-11-25 02:35:12 +10308196 cpumask_var_t non_isolated_cpus;
8197
8198 alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
Nick Piggin5c1e1762006-10-03 01:14:04 -07008199
Mike Travis434d53b2008-04-04 18:11:04 -07008200#if defined(CONFIG_NUMA)
8201 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
8202 GFP_KERNEL);
8203 BUG_ON(sched_group_nodes_bycpu == NULL);
8204#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01008205 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02008206 mutex_lock(&sched_domains_mutex);
Rusty Russelldcc30a32008-11-25 02:35:12 +10308207 arch_init_sched_domains(cpu_online_mask);
8208 cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
8209 if (cpumask_empty(non_isolated_cpus))
8210 cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02008211 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01008212 put_online_cpus();
Max Krasnyanskye761b772008-07-15 04:43:49 -07008213
8214#ifndef CONFIG_CPUSETS
Linus Torvalds1da177e2005-04-16 15:20:36 -07008215 /* XXX: Theoretical race here - CPU may be hotplugged now */
8216 hotcpu_notifier(update_sched_domains, 0);
Max Krasnyanskye761b772008-07-15 04:43:49 -07008217#endif
8218
8219 /* RT runtime code needs to handle some hotplug events */
8220 hotcpu_notifier(update_runtime, 0);
8221
Peter Zijlstrab328ca12008-04-29 10:02:46 +02008222 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07008223
8224 /* Move init over to a non-isolated CPU */
Rusty Russelldcc30a32008-11-25 02:35:12 +10308225 if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07008226 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01008227 sched_init_granularity();
Rusty Russelldcc30a32008-11-25 02:35:12 +10308228 free_cpumask_var(non_isolated_cpus);
Rusty Russell42128232008-11-25 02:35:12 +10308229
8230 alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
Rusty Russell0e3900e2008-11-25 02:35:13 +10308231 init_sched_rt_class();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008232}
8233#else
8234void __init sched_init_smp(void)
8235{
Ingo Molnar19978ca2007-11-09 22:39:38 +01008236 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008237}
8238#endif /* CONFIG_SMP */
8239
8240int in_sched_functions(unsigned long addr)
8241{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008242 return in_lock_functions(addr) ||
8243 (addr >= (unsigned long)__sched_text_start
8244 && addr < (unsigned long)__sched_text_end);
8245}
8246
Alexey Dobriyana9957442007-10-15 17:00:13 +02008247static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02008248{
8249 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02008250 INIT_LIST_HEAD(&cfs_rq->tasks);
Ingo Molnardd41f592007-07-09 18:51:59 +02008251#ifdef CONFIG_FAIR_GROUP_SCHED
8252 cfs_rq->rq = rq;
8253#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02008254 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02008255}
8256
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008257static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
8258{
8259 struct rt_prio_array *array;
8260 int i;
8261
8262 array = &rt_rq->active;
8263 for (i = 0; i < MAX_RT_PRIO; i++) {
8264 INIT_LIST_HEAD(array->queue + i);
8265 __clear_bit(i, array->bitmap);
8266 }
8267 /* delimiter for bitsearch: */
8268 __set_bit(MAX_RT_PRIO, array->bitmap);
8269
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008270#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Gregory Haskinse864c492008-12-29 09:39:49 -05008271 rt_rq->highest_prio.curr = MAX_RT_PRIO;
Gregory Haskins398a1532009-01-14 09:10:04 -05008272#ifdef CONFIG_SMP
Gregory Haskinse864c492008-12-29 09:39:49 -05008273 rt_rq->highest_prio.next = MAX_RT_PRIO;
Peter Zijlstra48d5e252008-01-25 21:08:31 +01008274#endif
Gregory Haskins398a1532009-01-14 09:10:04 -05008275#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008276#ifdef CONFIG_SMP
8277 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008278 rt_rq->overloaded = 0;
Gregory Haskins917b6272008-12-29 09:39:53 -05008279 plist_head_init(&rq->rt.pushable_tasks, &rq->lock);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008280#endif
8281
8282 rt_rq->rt_time = 0;
8283 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008284 rt_rq->rt_runtime = 0;
8285 spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008286
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008287#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01008288 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008289 rt_rq->rq = rq;
8290#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008291}
8292
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008293#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008294static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
8295 struct sched_entity *se, int cpu, int add,
8296 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008297{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008298 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008299 tg->cfs_rq[cpu] = cfs_rq;
8300 init_cfs_rq(cfs_rq, rq);
8301 cfs_rq->tg = tg;
8302 if (add)
8303 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
8304
8305 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008306 /* se could be NULL for init_task_group */
8307 if (!se)
8308 return;
8309
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008310 if (!parent)
8311 se->cfs_rq = &rq->cfs;
8312 else
8313 se->cfs_rq = parent->my_q;
8314
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008315 se->my_q = cfs_rq;
8316 se->load.weight = tg->shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008317 se->load.inv_weight = 0;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008318 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008319}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008320#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008321
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008322#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008323static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
8324 struct sched_rt_entity *rt_se, int cpu, int add,
8325 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008326{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008327 struct rq *rq = cpu_rq(cpu);
8328
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008329 tg->rt_rq[cpu] = rt_rq;
8330 init_rt_rq(rt_rq, rq);
8331 rt_rq->tg = tg;
8332 rt_rq->rt_se = rt_se;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008333 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008334 if (add)
8335 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
8336
8337 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008338 if (!rt_se)
8339 return;
8340
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008341 if (!parent)
8342 rt_se->rt_rq = &rq->rt;
8343 else
8344 rt_se->rt_rq = parent->my_q;
8345
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008346 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008347 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008348 INIT_LIST_HEAD(&rt_se->run_list);
8349}
8350#endif
8351
Linus Torvalds1da177e2005-04-16 15:20:36 -07008352void __init sched_init(void)
8353{
Ingo Molnardd41f592007-07-09 18:51:59 +02008354 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07008355 unsigned long alloc_size = 0, ptr;
8356
8357#ifdef CONFIG_FAIR_GROUP_SCHED
8358 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8359#endif
8360#ifdef CONFIG_RT_GROUP_SCHED
8361 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8362#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008363#ifdef CONFIG_USER_SCHED
8364 alloc_size *= 2;
8365#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008366 /*
8367 * As sched_init() is called before page_alloc is setup,
8368 * we use alloc_bootmem().
8369 */
8370 if (alloc_size) {
David Miller5a9d3222008-04-24 20:46:20 -07008371 ptr = (unsigned long)alloc_bootmem(alloc_size);
Mike Travis434d53b2008-04-04 18:11:04 -07008372
8373#ifdef CONFIG_FAIR_GROUP_SCHED
8374 init_task_group.se = (struct sched_entity **)ptr;
8375 ptr += nr_cpu_ids * sizeof(void **);
8376
8377 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
8378 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008379
8380#ifdef CONFIG_USER_SCHED
8381 root_task_group.se = (struct sched_entity **)ptr;
8382 ptr += nr_cpu_ids * sizeof(void **);
8383
8384 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
8385 ptr += nr_cpu_ids * sizeof(void **);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008386#endif /* CONFIG_USER_SCHED */
8387#endif /* CONFIG_FAIR_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07008388#ifdef CONFIG_RT_GROUP_SCHED
8389 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
8390 ptr += nr_cpu_ids * sizeof(void **);
8391
8392 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008393 ptr += nr_cpu_ids * sizeof(void **);
8394
8395#ifdef CONFIG_USER_SCHED
8396 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
8397 ptr += nr_cpu_ids * sizeof(void **);
8398
8399 root_task_group.rt_rq = (struct rt_rq **)ptr;
8400 ptr += nr_cpu_ids * sizeof(void **);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008401#endif /* CONFIG_USER_SCHED */
8402#endif /* CONFIG_RT_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07008403 }
Ingo Molnardd41f592007-07-09 18:51:59 +02008404
Gregory Haskins57d885f2008-01-25 21:08:18 +01008405#ifdef CONFIG_SMP
8406 init_defrootdomain();
8407#endif
8408
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008409 init_rt_bandwidth(&def_rt_bandwidth,
8410 global_rt_period(), global_rt_runtime());
8411
8412#ifdef CONFIG_RT_GROUP_SCHED
8413 init_rt_bandwidth(&init_task_group.rt_bandwidth,
8414 global_rt_period(), global_rt_runtime());
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008415#ifdef CONFIG_USER_SCHED
8416 init_rt_bandwidth(&root_task_group.rt_bandwidth,
8417 global_rt_period(), RUNTIME_INF);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008418#endif /* CONFIG_USER_SCHED */
8419#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008420
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008421#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008422 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008423 INIT_LIST_HEAD(&init_task_group.children);
8424
8425#ifdef CONFIG_USER_SCHED
8426 INIT_LIST_HEAD(&root_task_group.children);
8427 init_task_group.parent = &root_task_group;
8428 list_add(&init_task_group.siblings, &root_task_group.children);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008429#endif /* CONFIG_USER_SCHED */
8430#endif /* CONFIG_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008431
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08008432 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07008433 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008434
8435 rq = cpu_rq(i);
8436 spin_lock_init(&rq->lock);
Nick Piggin78979862005-06-25 14:57:13 -07008437 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008438 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008439 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008440#ifdef CONFIG_FAIR_GROUP_SCHED
8441 init_task_group.shares = init_task_group_load;
8442 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008443#ifdef CONFIG_CGROUP_SCHED
8444 /*
8445 * How much cpu bandwidth does init_task_group get?
8446 *
8447 * In case of task-groups formed thr' the cgroup filesystem, it
8448 * gets 100% of the cpu resources in the system. This overall
8449 * system cpu resource is divided among the tasks of
8450 * init_task_group and its child task-groups in a fair manner,
8451 * based on each entity's (task or task-group's) weight
8452 * (se->load.weight).
8453 *
8454 * In other words, if init_task_group has 10 tasks of weight
8455 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8456 * then A0's share of the cpu resource is:
8457 *
8458 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
8459 *
8460 * We achieve this by letting init_task_group's tasks sit
8461 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
8462 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008463 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008464#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008465 root_task_group.shares = NICE_0_LOAD;
8466 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008467 /*
8468 * In case of task-groups formed thr' the user id of tasks,
8469 * init_task_group represents tasks belonging to root user.
8470 * Hence it forms a sibling of all subsequent groups formed.
8471 * In this case, init_task_group gets only a fraction of overall
8472 * system cpu resource, based on the weight assigned to root
8473 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
8474 * by letting tasks of init_task_group sit in a separate cfs_rq
8475 * (init_cfs_rq) and having one entity represent this group of
8476 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
8477 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008478 init_tg_cfs_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008479 &per_cpu(init_cfs_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008480 &per_cpu(init_sched_entity, i), i, 1,
8481 root_task_group.se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008482
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008483#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02008484#endif /* CONFIG_FAIR_GROUP_SCHED */
8485
8486 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008487#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008488 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008489#ifdef CONFIG_CGROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008490 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008491#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008492 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008493 init_tg_rt_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008494 &per_cpu(init_rt_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008495 &per_cpu(init_sched_rt_entity, i), i, 1,
8496 root_task_group.rt_se[i]);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008497#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008498#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008499
Ingo Molnardd41f592007-07-09 18:51:59 +02008500 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
8501 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008502#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07008503 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01008504 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008505 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008506 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008507 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07008508 rq->cpu = i;
Gregory Haskins1f11eb62008-06-04 15:04:05 -04008509 rq->online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008510 rq->migration_thread = NULL;
8511 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01008512 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008513#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01008514 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008515 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008516 }
8517
Peter Williams2dd73a42006-06-27 02:54:34 -07008518 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008519
Avi Kivitye107be32007-07-26 13:40:43 +02008520#ifdef CONFIG_PREEMPT_NOTIFIERS
8521 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
8522#endif
8523
Christoph Lameterc9819f42006-12-10 02:20:25 -08008524#ifdef CONFIG_SMP
Carlos R. Mafra962cf362008-05-15 11:15:37 -03008525 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
Christoph Lameterc9819f42006-12-10 02:20:25 -08008526#endif
8527
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008528#ifdef CONFIG_RT_MUTEXES
8529 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
8530#endif
8531
Linus Torvalds1da177e2005-04-16 15:20:36 -07008532 /*
8533 * The boot idle thread does lazy MMU switching as well:
8534 */
8535 atomic_inc(&init_mm.mm_count);
8536 enter_lazy_tlb(&init_mm, current);
8537
8538 /*
8539 * Make us the idle thread. Technically, schedule() should not be
8540 * called from this thread, however somewhere below it might be,
8541 * but because we are the idle thread, we just pick up running again
8542 * when this runqueue becomes "idle".
8543 */
8544 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02008545 /*
8546 * During early bootup we pretend to be a normal task:
8547 */
8548 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01008549
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10308550 /* Allocate the nohz_cpu_mask if CONFIG_CPUMASK_OFFSTACK */
8551 alloc_bootmem_cpumask_var(&nohz_cpu_mask);
Rusty Russellbf4d83f2008-11-25 09:57:51 +10308552#ifdef CONFIG_SMP
Rusty Russell7d1e6a92008-11-25 02:35:09 +10308553#ifdef CONFIG_NO_HZ
8554 alloc_bootmem_cpumask_var(&nohz.cpu_mask);
8555#endif
Rusty Russelldcc30a32008-11-25 02:35:12 +10308556 alloc_bootmem_cpumask_var(&cpu_isolated_map);
Rusty Russellbf4d83f2008-11-25 09:57:51 +10308557#endif /* SMP */
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10308558
Ingo Molnar6892b752008-02-13 14:02:36 +01008559 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008560}
8561
8562#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
8563void __might_sleep(char *file, int line)
8564{
Ingo Molnar48f24c42006-07-03 00:25:40 -07008565#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07008566 static unsigned long prev_jiffy; /* ratelimiting */
8567
Ingo Molnaraef745f2008-08-28 11:34:43 +02008568 if ((!in_atomic() && !irqs_disabled()) ||
8569 system_state != SYSTEM_RUNNING || oops_in_progress)
8570 return;
8571 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
8572 return;
8573 prev_jiffy = jiffies;
8574
8575 printk(KERN_ERR
8576 "BUG: sleeping function called from invalid context at %s:%d\n",
8577 file, line);
8578 printk(KERN_ERR
8579 "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
8580 in_atomic(), irqs_disabled(),
8581 current->pid, current->comm);
8582
8583 debug_show_held_locks(current);
8584 if (irqs_disabled())
8585 print_irqtrace_events(current);
8586 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008587#endif
8588}
8589EXPORT_SYMBOL(__might_sleep);
8590#endif
8591
8592#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008593static void normalize_task(struct rq *rq, struct task_struct *p)
8594{
8595 int on_rq;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02008596
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008597 update_rq_clock(rq);
8598 on_rq = p->se.on_rq;
8599 if (on_rq)
8600 deactivate_task(rq, p, 0);
8601 __setscheduler(rq, p, SCHED_NORMAL, 0);
8602 if (on_rq) {
8603 activate_task(rq, p, 0);
8604 resched_task(rq->curr);
8605 }
8606}
8607
Linus Torvalds1da177e2005-04-16 15:20:36 -07008608void normalize_rt_tasks(void)
8609{
Ingo Molnara0f98a12007-06-17 18:37:45 +02008610 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008611 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07008612 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008613
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008614 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008615 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02008616 /*
8617 * Only normalize user tasks:
8618 */
8619 if (!p->mm)
8620 continue;
8621
Ingo Molnardd41f592007-07-09 18:51:59 +02008622 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008623#ifdef CONFIG_SCHEDSTATS
8624 p->se.wait_start = 0;
8625 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008626 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008627#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02008628
8629 if (!rt_task(p)) {
8630 /*
8631 * Renice negative nice level userspace
8632 * tasks back to 0:
8633 */
8634 if (TASK_NICE(p) < 0 && p->mm)
8635 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008636 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02008637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008638
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008639 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07008640 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008641
Ingo Molnar178be792007-10-15 17:00:18 +02008642 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008643
Ingo Molnarb29739f2006-06-27 02:54:51 -07008644 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008645 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008646 } while_each_thread(g, p);
8647
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008648 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008649}
8650
8651#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07008652
8653#ifdef CONFIG_IA64
8654/*
8655 * These functions are only useful for the IA64 MCA handling.
8656 *
8657 * They can only be called when the whole system has been
8658 * stopped - every CPU needs to be quiescent, and no scheduling
8659 * activity can take place. Using them for anything else would
8660 * be a serious bug, and as a result, they aren't even visible
8661 * under any other configuration.
8662 */
8663
8664/**
8665 * curr_task - return the current task for a given cpu.
8666 * @cpu: the processor in question.
8667 *
8668 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8669 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008670struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008671{
8672 return cpu_curr(cpu);
8673}
8674
8675/**
8676 * set_curr_task - set the current task for a given cpu.
8677 * @cpu: the processor in question.
8678 * @p: the task pointer to set.
8679 *
8680 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008681 * are serviced on a separate stack. It allows the architecture to switch the
8682 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07008683 * must be called with all CPU's synchronized, and interrupts disabled, the
8684 * and caller must save the original value of the current task (see
8685 * curr_task() above) and restore that value before reenabling interrupts and
8686 * re-starting the system.
8687 *
8688 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8689 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008690void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008691{
8692 cpu_curr(cpu) = p;
8693}
8694
8695#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008696
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008697#ifdef CONFIG_FAIR_GROUP_SCHED
8698static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008699{
8700 int i;
8701
8702 for_each_possible_cpu(i) {
8703 if (tg->cfs_rq)
8704 kfree(tg->cfs_rq[i]);
8705 if (tg->se)
8706 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008707 }
8708
8709 kfree(tg->cfs_rq);
8710 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008711}
8712
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008713static
8714int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008715{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008716 struct cfs_rq *cfs_rq;
Li Zefaneab17222008-10-29 17:03:22 +08008717 struct sched_entity *se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008718 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008719 int i;
8720
Mike Travis434d53b2008-04-04 18:11:04 -07008721 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008722 if (!tg->cfs_rq)
8723 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008724 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008725 if (!tg->se)
8726 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008727
8728 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008729
8730 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008731 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008732
Li Zefaneab17222008-10-29 17:03:22 +08008733 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
8734 GFP_KERNEL, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008735 if (!cfs_rq)
8736 goto err;
8737
Li Zefaneab17222008-10-29 17:03:22 +08008738 se = kzalloc_node(sizeof(struct sched_entity),
8739 GFP_KERNEL, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008740 if (!se)
8741 goto err;
8742
Li Zefaneab17222008-10-29 17:03:22 +08008743 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent->se[i]);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008744 }
8745
8746 return 1;
8747
8748 err:
8749 return 0;
8750}
8751
8752static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8753{
8754 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
8755 &cpu_rq(cpu)->leaf_cfs_rq_list);
8756}
8757
8758static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8759{
8760 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
8761}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008762#else /* !CONFG_FAIR_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008763static inline void free_fair_sched_group(struct task_group *tg)
8764{
8765}
8766
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008767static inline
8768int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008769{
8770 return 1;
8771}
8772
8773static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8774{
8775}
8776
8777static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8778{
8779}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008780#endif /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008781
8782#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008783static void free_rt_sched_group(struct task_group *tg)
8784{
8785 int i;
8786
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008787 destroy_rt_bandwidth(&tg->rt_bandwidth);
8788
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008789 for_each_possible_cpu(i) {
8790 if (tg->rt_rq)
8791 kfree(tg->rt_rq[i]);
8792 if (tg->rt_se)
8793 kfree(tg->rt_se[i]);
8794 }
8795
8796 kfree(tg->rt_rq);
8797 kfree(tg->rt_se);
8798}
8799
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008800static
8801int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008802{
8803 struct rt_rq *rt_rq;
Li Zefaneab17222008-10-29 17:03:22 +08008804 struct sched_rt_entity *rt_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008805 struct rq *rq;
8806 int i;
8807
Mike Travis434d53b2008-04-04 18:11:04 -07008808 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008809 if (!tg->rt_rq)
8810 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008811 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008812 if (!tg->rt_se)
8813 goto err;
8814
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008815 init_rt_bandwidth(&tg->rt_bandwidth,
8816 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008817
8818 for_each_possible_cpu(i) {
8819 rq = cpu_rq(i);
8820
Li Zefaneab17222008-10-29 17:03:22 +08008821 rt_rq = kzalloc_node(sizeof(struct rt_rq),
8822 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008823 if (!rt_rq)
8824 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008825
Li Zefaneab17222008-10-29 17:03:22 +08008826 rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
8827 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008828 if (!rt_se)
8829 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008830
Li Zefaneab17222008-10-29 17:03:22 +08008831 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent->rt_se[i]);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008832 }
8833
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008834 return 1;
8835
8836 err:
8837 return 0;
8838}
8839
8840static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8841{
8842 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
8843 &cpu_rq(cpu)->leaf_rt_rq_list);
8844}
8845
8846static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8847{
8848 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
8849}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008850#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008851static inline void free_rt_sched_group(struct task_group *tg)
8852{
8853}
8854
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008855static inline
8856int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008857{
8858 return 1;
8859}
8860
8861static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8862{
8863}
8864
8865static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8866{
8867}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008868#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008869
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008870#ifdef CONFIG_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008871static void free_sched_group(struct task_group *tg)
8872{
8873 free_fair_sched_group(tg);
8874 free_rt_sched_group(tg);
8875 kfree(tg);
8876}
8877
8878/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008879struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008880{
8881 struct task_group *tg;
8882 unsigned long flags;
8883 int i;
8884
8885 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8886 if (!tg)
8887 return ERR_PTR(-ENOMEM);
8888
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008889 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008890 goto err;
8891
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008892 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008893 goto err;
8894
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008895 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008896 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008897 register_fair_sched_group(tg, i);
8898 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008899 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008900 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008901
8902 WARN_ON(!parent); /* root should already exist */
8903
8904 tg->parent = parent;
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008905 INIT_LIST_HEAD(&tg->children);
Zhang, Yanmin09f27242030-08-14 15:56:40 +08008906 list_add_rcu(&tg->siblings, &parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008907 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008908
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008909 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008910
8911err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008912 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008913 return ERR_PTR(-ENOMEM);
8914}
8915
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008916/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008917static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008918{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008919 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008920 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008921}
8922
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008923/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008924void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008925{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008926 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008927 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008928
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008929 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008930 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008931 unregister_fair_sched_group(tg, i);
8932 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008933 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008934 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008935 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008936 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008937
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008938 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008939 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008940}
8941
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008942/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008943 * The caller of this function should have put the task in its new group
8944 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8945 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008946 */
8947void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008948{
8949 int on_rq, running;
8950 unsigned long flags;
8951 struct rq *rq;
8952
8953 rq = task_rq_lock(tsk, &flags);
8954
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008955 update_rq_clock(rq);
8956
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008957 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008958 on_rq = tsk->se.on_rq;
8959
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008960 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008961 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008962 if (unlikely(running))
8963 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008964
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008965 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008966
Peter Zijlstra810b3812008-02-29 15:21:01 -05008967#ifdef CONFIG_FAIR_GROUP_SCHED
8968 if (tsk->sched_class->moved_group)
8969 tsk->sched_class->moved_group(tsk);
8970#endif
8971
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008972 if (unlikely(running))
8973 tsk->sched_class->set_curr_task(rq);
8974 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02008975 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008976
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008977 task_rq_unlock(rq, &flags);
8978}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008979#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008980
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008981#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008982static void __set_se_shares(struct sched_entity *se, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008983{
8984 struct cfs_rq *cfs_rq = se->cfs_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008985 int on_rq;
8986
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008987 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008988 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008989 dequeue_entity(cfs_rq, se, 0);
8990
8991 se->load.weight = shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008992 se->load.inv_weight = 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008993
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008994 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008995 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008996}
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008997
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008998static void set_se_shares(struct sched_entity *se, unsigned long shares)
8999{
9000 struct cfs_rq *cfs_rq = se->cfs_rq;
9001 struct rq *rq = cfs_rq->rq;
9002 unsigned long flags;
9003
9004 spin_lock_irqsave(&rq->lock, flags);
9005 __set_se_shares(se, shares);
9006 spin_unlock_irqrestore(&rq->lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009007}
9008
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009009static DEFINE_MUTEX(shares_mutex);
9010
Ingo Molnar4cf86d72007-10-15 17:00:14 +02009011int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009012{
9013 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009014 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01009015
Peter Zijlstra62fb1852008-02-25 17:34:02 +01009016 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009017 * We can't change the weight of the root cgroup.
9018 */
9019 if (!tg->se[0])
9020 return -EINVAL;
9021
Peter Zijlstra18d95a22008-04-19 19:45:00 +02009022 if (shares < MIN_SHARES)
9023 shares = MIN_SHARES;
Miao Xiecb4ad1f2008-04-28 12:54:56 +08009024 else if (shares > MAX_SHARES)
9025 shares = MAX_SHARES;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01009026
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009027 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009028 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02009029 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009030
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009031 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009032 for_each_possible_cpu(i)
9033 unregister_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02009034 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009035 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01009036
9037 /* wait for any ongoing reference to this group to finish */
9038 synchronize_sched();
9039
9040 /*
9041 * Now we are free to modify the group's share on each cpu
9042 * w/o tripping rebalance_share or load_balance_fair.
9043 */
9044 tg->shares = shares;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02009045 for_each_possible_cpu(i) {
9046 /*
9047 * force a rebalance
9048 */
9049 cfs_rq_set_shares(tg->cfs_rq[i], 0);
Miao Xiecb4ad1f2008-04-28 12:54:56 +08009050 set_se_shares(tg->se[i], shares);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02009051 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01009052
9053 /*
9054 * Enable load balance activity on this group, by inserting it back on
9055 * each cpu's rq->leaf_cfs_rq_list.
9056 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009057 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01009058 for_each_possible_cpu(i)
9059 register_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02009060 list_add_rcu(&tg->siblings, &tg->parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009061 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02009062done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01009063 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02009064 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02009065}
9066
Dhaval Giani5cb350b2007-10-15 17:00:14 +02009067unsigned long sched_group_shares(struct task_group *tg)
9068{
9069 return tg->shares;
9070}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009071#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02009072
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009073#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009074/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009075 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009076 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009077static DEFINE_MUTEX(rt_constraints_mutex);
9078
9079static unsigned long to_ratio(u64 period, u64 runtime)
9080{
9081 if (runtime == RUNTIME_INF)
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009082 return 1ULL << 20;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009083
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009084 return div64_u64(runtime << 20, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009085}
9086
Dhaval Giani521f1a242008-02-28 15:21:56 +05309087/* Must be called with tasklist_lock held */
9088static inline int tg_has_rt_tasks(struct task_group *tg)
9089{
9090 struct task_struct *g, *p;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009091
Dhaval Giani521f1a242008-02-28 15:21:56 +05309092 do_each_thread(g, p) {
9093 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
9094 return 1;
9095 } while_each_thread(g, p);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009096
Dhaval Giani521f1a242008-02-28 15:21:56 +05309097 return 0;
9098}
9099
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009100struct rt_schedulable_data {
9101 struct task_group *tg;
9102 u64 rt_period;
9103 u64 rt_runtime;
9104};
9105
9106static int tg_schedulable(struct task_group *tg, void *data)
9107{
9108 struct rt_schedulable_data *d = data;
9109 struct task_group *child;
9110 unsigned long total, sum = 0;
9111 u64 period, runtime;
9112
9113 period = ktime_to_ns(tg->rt_bandwidth.rt_period);
9114 runtime = tg->rt_bandwidth.rt_runtime;
9115
9116 if (tg == d->tg) {
9117 period = d->rt_period;
9118 runtime = d->rt_runtime;
9119 }
9120
Peter Zijlstra4653f802008-09-23 15:33:44 +02009121 /*
9122 * Cannot have more runtime than the period.
9123 */
9124 if (runtime > period && runtime != RUNTIME_INF)
9125 return -EINVAL;
9126
9127 /*
9128 * Ensure we don't starve existing RT tasks.
9129 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009130 if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
9131 return -EBUSY;
9132
9133 total = to_ratio(period, runtime);
9134
Peter Zijlstra4653f802008-09-23 15:33:44 +02009135 /*
9136 * Nobody can have more than the global setting allows.
9137 */
9138 if (total > to_ratio(global_rt_period(), global_rt_runtime()))
9139 return -EINVAL;
9140
9141 /*
9142 * The sum of our children's runtime should not exceed our own.
9143 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009144 list_for_each_entry_rcu(child, &tg->children, siblings) {
9145 period = ktime_to_ns(child->rt_bandwidth.rt_period);
9146 runtime = child->rt_bandwidth.rt_runtime;
9147
9148 if (child == d->tg) {
9149 period = d->rt_period;
9150 runtime = d->rt_runtime;
9151 }
9152
9153 sum += to_ratio(period, runtime);
9154 }
9155
9156 if (sum > total)
9157 return -EINVAL;
9158
9159 return 0;
9160}
9161
9162static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
9163{
9164 struct rt_schedulable_data data = {
9165 .tg = tg,
9166 .rt_period = period,
9167 .rt_runtime = runtime,
9168 };
9169
9170 return walk_tg_tree(tg_schedulable, tg_nop, &data);
9171}
9172
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009173static int tg_set_bandwidth(struct task_group *tg,
9174 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009175{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009176 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009177
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009178 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05309179 read_lock(&tasklist_lock);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009180 err = __rt_schedulable(tg, rt_period, rt_runtime);
9181 if (err)
Dhaval Giani521f1a242008-02-28 15:21:56 +05309182 goto unlock;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009183
9184 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009185 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
9186 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009187
9188 for_each_possible_cpu(i) {
9189 struct rt_rq *rt_rq = tg->rt_rq[i];
9190
9191 spin_lock(&rt_rq->rt_runtime_lock);
9192 rt_rq->rt_runtime = rt_runtime;
9193 spin_unlock(&rt_rq->rt_runtime_lock);
9194 }
9195 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009196 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05309197 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009198 mutex_unlock(&rt_constraints_mutex);
9199
9200 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009201}
9202
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009203int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
9204{
9205 u64 rt_runtime, rt_period;
9206
9207 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
9208 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
9209 if (rt_runtime_us < 0)
9210 rt_runtime = RUNTIME_INF;
9211
9212 return tg_set_bandwidth(tg, rt_period, rt_runtime);
9213}
9214
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009215long sched_group_rt_runtime(struct task_group *tg)
9216{
9217 u64 rt_runtime_us;
9218
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009219 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009220 return -1;
9221
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009222 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009223 do_div(rt_runtime_us, NSEC_PER_USEC);
9224 return rt_runtime_us;
9225}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009226
9227int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
9228{
9229 u64 rt_runtime, rt_period;
9230
9231 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
9232 rt_runtime = tg->rt_bandwidth.rt_runtime;
9233
Raistlin619b0482008-06-26 18:54:09 +02009234 if (rt_period == 0)
9235 return -EINVAL;
9236
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009237 return tg_set_bandwidth(tg, rt_period, rt_runtime);
9238}
9239
9240long sched_group_rt_period(struct task_group *tg)
9241{
9242 u64 rt_period_us;
9243
9244 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
9245 do_div(rt_period_us, NSEC_PER_USEC);
9246 return rt_period_us;
9247}
9248
9249static int sched_rt_global_constraints(void)
9250{
Peter Zijlstra4653f802008-09-23 15:33:44 +02009251 u64 runtime, period;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009252 int ret = 0;
9253
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07009254 if (sysctl_sched_rt_period <= 0)
9255 return -EINVAL;
9256
Peter Zijlstra4653f802008-09-23 15:33:44 +02009257 runtime = global_rt_runtime();
9258 period = global_rt_period();
9259
9260 /*
9261 * Sanity check on the sysctl variables.
9262 */
9263 if (runtime > period && runtime != RUNTIME_INF)
9264 return -EINVAL;
Peter Zijlstra10b612f2008-06-19 14:22:27 +02009265
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009266 mutex_lock(&rt_constraints_mutex);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009267 read_lock(&tasklist_lock);
Peter Zijlstra4653f802008-09-23 15:33:44 +02009268 ret = __rt_schedulable(NULL, 0, 0);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009269 read_unlock(&tasklist_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009270 mutex_unlock(&rt_constraints_mutex);
9271
9272 return ret;
9273}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009274#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009275static int sched_rt_global_constraints(void)
9276{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009277 unsigned long flags;
9278 int i;
9279
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07009280 if (sysctl_sched_rt_period <= 0)
9281 return -EINVAL;
9282
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009283 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
9284 for_each_possible_cpu(i) {
9285 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
9286
9287 spin_lock(&rt_rq->rt_runtime_lock);
9288 rt_rq->rt_runtime = global_rt_runtime();
9289 spin_unlock(&rt_rq->rt_runtime_lock);
9290 }
9291 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
9292
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009293 return 0;
9294}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009295#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009296
9297int sched_rt_handler(struct ctl_table *table, int write,
9298 struct file *filp, void __user *buffer, size_t *lenp,
9299 loff_t *ppos)
9300{
9301 int ret;
9302 int old_period, old_runtime;
9303 static DEFINE_MUTEX(mutex);
9304
9305 mutex_lock(&mutex);
9306 old_period = sysctl_sched_rt_period;
9307 old_runtime = sysctl_sched_rt_runtime;
9308
9309 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
9310
9311 if (!ret && write) {
9312 ret = sched_rt_global_constraints();
9313 if (ret) {
9314 sysctl_sched_rt_period = old_period;
9315 sysctl_sched_rt_runtime = old_runtime;
9316 } else {
9317 def_rt_bandwidth.rt_runtime = global_rt_runtime();
9318 def_rt_bandwidth.rt_period =
9319 ns_to_ktime(global_rt_period());
9320 }
9321 }
9322 mutex_unlock(&mutex);
9323
9324 return ret;
9325}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009326
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009327#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009328
9329/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009330static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009331{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009332 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
9333 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009334}
9335
9336static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02009337cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009338{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009339 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009340
Paul Menage2b01dfe2007-10-24 18:23:50 +02009341 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009342 /* This is early initialization for the top cgroup */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009343 return &init_task_group.css;
9344 }
9345
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009346 parent = cgroup_tg(cgrp->parent);
9347 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009348 if (IS_ERR(tg))
9349 return ERR_PTR(-ENOMEM);
9350
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009351 return &tg->css;
9352}
9353
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009354static void
9355cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009356{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009357 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009358
9359 sched_destroy_group(tg);
9360}
9361
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009362static int
9363cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
9364 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009365{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009366#ifdef CONFIG_RT_GROUP_SCHED
9367 /* Don't accept realtime tasks when there is no way for them to run */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009368 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009369 return -EINVAL;
9370#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009371 /* We don't support RT-tasks being in separate groups */
9372 if (tsk->sched_class != &fair_sched_class)
9373 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009374#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009375
9376 return 0;
9377}
9378
9379static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02009380cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009381 struct cgroup *old_cont, struct task_struct *tsk)
9382{
9383 sched_move_task(tsk);
9384}
9385
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009386#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagef4c753b2008-04-29 00:59:56 -07009387static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
Paul Menage2b01dfe2007-10-24 18:23:50 +02009388 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009389{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009390 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009391}
9392
Paul Menagef4c753b2008-04-29 00:59:56 -07009393static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009394{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009395 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009396
9397 return (u64) tg->shares;
9398}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009399#endif /* CONFIG_FAIR_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009400
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009401#ifdef CONFIG_RT_GROUP_SCHED
Mirco Tischler0c708142008-05-14 16:05:46 -07009402static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Paul Menage06ecb272008-04-29 01:00:06 -07009403 s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009404{
Paul Menage06ecb272008-04-29 01:00:06 -07009405 return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009406}
9407
Paul Menage06ecb272008-04-29 01:00:06 -07009408static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009409{
Paul Menage06ecb272008-04-29 01:00:06 -07009410 return sched_group_rt_runtime(cgroup_tg(cgrp));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009411}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009412
9413static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
9414 u64 rt_period_us)
9415{
9416 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
9417}
9418
9419static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
9420{
9421 return sched_group_rt_period(cgroup_tg(cgrp));
9422}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009423#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009424
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009425static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009426#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009427 {
9428 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07009429 .read_u64 = cpu_shares_read_u64,
9430 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009431 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009432#endif
9433#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009434 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009435 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07009436 .read_s64 = cpu_rt_runtime_read,
9437 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009438 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009439 {
9440 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07009441 .read_u64 = cpu_rt_period_read_uint,
9442 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009443 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009444#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009445};
9446
9447static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
9448{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009449 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009450}
9451
9452struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01009453 .name = "cpu",
9454 .create = cpu_cgroup_create,
9455 .destroy = cpu_cgroup_destroy,
9456 .can_attach = cpu_cgroup_can_attach,
9457 .attach = cpu_cgroup_attach,
9458 .populate = cpu_cgroup_populate,
9459 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009460 .early_init = 1,
9461};
9462
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009463#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009464
9465#ifdef CONFIG_CGROUP_CPUACCT
9466
9467/*
9468 * CPU accounting code for task groups.
9469 *
9470 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
9471 * (balbir@in.ibm.com).
9472 */
9473
Bharata B Rao934352f2008-11-10 20:41:13 +05309474/* track cpu usage of a group of tasks and its child groups */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009475struct cpuacct {
9476 struct cgroup_subsys_state css;
9477 /* cpuusage holds pointer to a u64-type object on every cpu */
9478 u64 *cpuusage;
Bharata B Rao934352f2008-11-10 20:41:13 +05309479 struct cpuacct *parent;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009480};
9481
9482struct cgroup_subsys cpuacct_subsys;
9483
9484/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309485static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009486{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309487 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009488 struct cpuacct, css);
9489}
9490
9491/* return cpu accounting group to which this task belongs */
9492static inline struct cpuacct *task_ca(struct task_struct *tsk)
9493{
9494 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
9495 struct cpuacct, css);
9496}
9497
9498/* create a new cpu accounting group */
9499static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05309500 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009501{
9502 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
9503
9504 if (!ca)
9505 return ERR_PTR(-ENOMEM);
9506
9507 ca->cpuusage = alloc_percpu(u64);
9508 if (!ca->cpuusage) {
9509 kfree(ca);
9510 return ERR_PTR(-ENOMEM);
9511 }
9512
Bharata B Rao934352f2008-11-10 20:41:13 +05309513 if (cgrp->parent)
9514 ca->parent = cgroup_ca(cgrp->parent);
9515
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009516 return &ca->css;
9517}
9518
9519/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009520static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05309521cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009522{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309523 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009524
9525 free_percpu(ca->cpuusage);
9526 kfree(ca);
9527}
9528
Ken Chen720f5492008-12-15 22:02:01 -08009529static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu)
9530{
9531 u64 *cpuusage = percpu_ptr(ca->cpuusage, cpu);
9532 u64 data;
9533
9534#ifndef CONFIG_64BIT
9535 /*
9536 * Take rq->lock to make 64-bit read safe on 32-bit platforms.
9537 */
9538 spin_lock_irq(&cpu_rq(cpu)->lock);
9539 data = *cpuusage;
9540 spin_unlock_irq(&cpu_rq(cpu)->lock);
9541#else
9542 data = *cpuusage;
9543#endif
9544
9545 return data;
9546}
9547
9548static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val)
9549{
9550 u64 *cpuusage = percpu_ptr(ca->cpuusage, cpu);
9551
9552#ifndef CONFIG_64BIT
9553 /*
9554 * Take rq->lock to make 64-bit write safe on 32-bit platforms.
9555 */
9556 spin_lock_irq(&cpu_rq(cpu)->lock);
9557 *cpuusage = val;
9558 spin_unlock_irq(&cpu_rq(cpu)->lock);
9559#else
9560 *cpuusage = val;
9561#endif
9562}
9563
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009564/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309565static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009566{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309567 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009568 u64 totalcpuusage = 0;
9569 int i;
9570
Ken Chen720f5492008-12-15 22:02:01 -08009571 for_each_present_cpu(i)
9572 totalcpuusage += cpuacct_cpuusage_read(ca, i);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009573
9574 return totalcpuusage;
9575}
9576
Dhaval Giani0297b802008-02-29 10:02:44 +05309577static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
9578 u64 reset)
9579{
9580 struct cpuacct *ca = cgroup_ca(cgrp);
9581 int err = 0;
9582 int i;
9583
9584 if (reset) {
9585 err = -EINVAL;
9586 goto out;
9587 }
9588
Ken Chen720f5492008-12-15 22:02:01 -08009589 for_each_present_cpu(i)
9590 cpuacct_cpuusage_write(ca, i, 0);
Dhaval Giani0297b802008-02-29 10:02:44 +05309591
Dhaval Giani0297b802008-02-29 10:02:44 +05309592out:
9593 return err;
9594}
9595
Ken Chene9515c32008-12-15 22:04:15 -08009596static int cpuacct_percpu_seq_read(struct cgroup *cgroup, struct cftype *cft,
9597 struct seq_file *m)
9598{
9599 struct cpuacct *ca = cgroup_ca(cgroup);
9600 u64 percpu;
9601 int i;
9602
9603 for_each_present_cpu(i) {
9604 percpu = cpuacct_cpuusage_read(ca, i);
9605 seq_printf(m, "%llu ", (unsigned long long) percpu);
9606 }
9607 seq_printf(m, "\n");
9608 return 0;
9609}
9610
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009611static struct cftype files[] = {
9612 {
9613 .name = "usage",
Paul Menagef4c753b2008-04-29 00:59:56 -07009614 .read_u64 = cpuusage_read,
9615 .write_u64 = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009616 },
Ken Chene9515c32008-12-15 22:04:15 -08009617 {
9618 .name = "usage_percpu",
9619 .read_seq_string = cpuacct_percpu_seq_read,
9620 },
9621
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009622};
9623
Dhaval Giani32cd7562008-02-29 10:02:43 +05309624static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009625{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309626 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009627}
9628
9629/*
9630 * charge this task's execution time to its accounting group.
9631 *
9632 * called with rq->lock held.
9633 */
9634static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
9635{
9636 struct cpuacct *ca;
Bharata B Rao934352f2008-11-10 20:41:13 +05309637 int cpu;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009638
9639 if (!cpuacct_subsys.active)
9640 return;
9641
Bharata B Rao934352f2008-11-10 20:41:13 +05309642 cpu = task_cpu(tsk);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009643 ca = task_ca(tsk);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009644
Bharata B Rao934352f2008-11-10 20:41:13 +05309645 for (; ca; ca = ca->parent) {
9646 u64 *cpuusage = percpu_ptr(ca->cpuusage, cpu);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009647 *cpuusage += cputime;
9648 }
9649}
9650
9651struct cgroup_subsys cpuacct_subsys = {
9652 .name = "cpuacct",
9653 .create = cpuacct_create,
9654 .destroy = cpuacct_destroy,
9655 .populate = cpuacct_populate,
9656 .subsys_id = cpuacct_subsys_id,
9657};
9658#endif /* CONFIG_CGROUP_CPUACCT */