blob: 716cfc8e099e5db03da19d4e6a54ffce6b636ff9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * kernel/sched.c
3 *
4 * Kernel scheduler and related syscalls
5 *
6 * Copyright (C) 1991-2002 Linus Torvalds
7 *
8 * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and
9 * make semaphores SMP safe
10 * 1998-11-19 Implemented schedule_timeout() and related stuff
11 * by Andrea Arcangeli
12 * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
13 * hybrid priority-list and round-robin design with
14 * an array-switch method of distributing timeslices
15 * and per-CPU runqueues. Cleanups and useful suggestions
16 * by Davide Libenzi, preemptible kernel bits by Robert Love.
17 * 2003-09-03 Interactivity tuning by Con Kolivas.
18 * 2004-04-02 Scheduler domains code by Nick Piggin
Ingo Molnarc31f2e82007-07-09 18:52:01 +020019 * 2007-04-15 Work begun on replacing all interactivity tuning with a
20 * fair scheduling design by Con Kolivas.
21 * 2007-05-05 Load balancing (smp-nice) and other improvements
22 * by Peter Williams
23 * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith
24 * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri
Ingo Molnarb9131762008-01-25 21:08:19 +010025 * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins,
26 * Thomas Gleixner, Mike Kravetz
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
28
29#include <linux/mm.h>
30#include <linux/module.h>
31#include <linux/nmi.h>
32#include <linux/init.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020033#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/highmem.h>
35#include <linux/smp_lock.h>
36#include <asm/mmu_context.h>
37#include <linux/interrupt.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080038#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/completion.h>
40#include <linux/kernel_stat.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070041#include <linux/debug_locks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/security.h>
43#include <linux/notifier.h>
44#include <linux/profile.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080045#include <linux/freezer.h>
akpm@osdl.org198e2f12006-01-12 01:05:30 -080046#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/blkdev.h>
48#include <linux/delay.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070049#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/smp.h>
51#include <linux/threads.h>
52#include <linux/timer.h>
53#include <linux/rcupdate.h>
54#include <linux/cpu.h>
55#include <linux/cpuset.h>
56#include <linux/percpu.h>
57#include <linux/kthread.h>
58#include <linux/seq_file.h>
Nick Piggine692ab52007-07-26 13:40:43 +020059#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/syscalls.h>
61#include <linux/times.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070062#include <linux/tsacct_kern.h>
bibo maoc6fd91f2006-03-26 01:38:20 -080063#include <linux/kprobes.h>
Shailabh Nagar0ff92242006-07-14 00:24:37 -070064#include <linux/delayacct.h>
Eric Dumazet5517d862007-05-08 00:32:57 -070065#include <linux/reciprocal_div.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020066#include <linux/unistd.h>
Jens Axboef5ff8422007-09-21 09:19:54 +020067#include <linux/pagemap.h>
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +010068#include <linux/hrtimer.h>
Reynes Philippe30914a52008-03-17 16:19:05 -070069#include <linux/tick.h>
Mike Travis434d53b2008-04-04 18:11:04 -070070#include <linux/bootmem.h>
Peter Zijlstraf00b45c2008-04-19 19:45:00 +020071#include <linux/debugfs.h>
72#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Eric Dumazet5517d862007-05-08 00:32:57 -070074#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020075#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Gregory Haskins6e0534f2008-05-12 21:21:01 +020077#include "sched_cpupri.h"
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/*
80 * Convert user-nice values [ -20 ... 0 ... 19 ]
81 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
82 * and back.
83 */
84#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
85#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
86#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
87
88/*
89 * 'User priority' is the nice value converted to something we
90 * can work with better when scaling various scheduler parameters,
91 * it's a [ 0 ... 39 ] range.
92 */
93#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
94#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
95#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
96
97/*
Ingo Molnard7876a02008-01-25 21:08:19 +010098 * Helpers for converting nanosecond timing to jiffy resolution
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
Eric Dumazetd6322fa2007-11-09 22:39:38 +0100100#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200102#define NICE_0_LOAD SCHED_LOAD_SCALE
103#define NICE_0_SHIFT SCHED_LOAD_SHIFT
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/*
106 * These are the 'tuning knobs' of the scheduler:
107 *
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200108 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * Timeslices get refilled after they expire.
110 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700112
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200113/*
114 * single value that denotes runtime == period, ie unlimited time.
115 */
116#define RUNTIME_INF ((u64)~0ULL)
117
Eric Dumazet5517d862007-05-08 00:32:57 -0700118#ifdef CONFIG_SMP
119/*
120 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
121 * Since cpu_power is a 'constant', we can use a reciprocal divide.
122 */
123static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
124{
125 return reciprocal_divide(load, sg->reciprocal_cpu_power);
126}
127
128/*
129 * Each time a sched group cpu_power is changed,
130 * we must compute its reciprocal value
131 */
132static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
133{
134 sg->__cpu_power += val;
135 sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
136}
137#endif
138
Ingo Molnare05606d2007-07-09 18:51:59 +0200139static inline int rt_policy(int policy)
140{
Roel Kluin3f33a7c2008-05-13 23:44:11 +0200141 if (unlikely(policy == SCHED_FIFO || policy == SCHED_RR))
Ingo Molnare05606d2007-07-09 18:51:59 +0200142 return 1;
143 return 0;
144}
145
146static inline int task_has_rt_policy(struct task_struct *p)
147{
148 return rt_policy(p->policy);
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200152 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200154struct rt_prio_array {
155 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
Dmitry Adamushko20b63312008-06-11 00:58:30 +0200156 struct list_head queue[MAX_RT_PRIO];
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200157};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200159struct rt_bandwidth {
Ingo Molnarea736ed2008-03-25 13:51:45 +0100160 /* nests inside the rq lock: */
161 spinlock_t rt_runtime_lock;
162 ktime_t rt_period;
163 u64 rt_runtime;
164 struct hrtimer rt_period_timer;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200165};
166
167static struct rt_bandwidth def_rt_bandwidth;
168
169static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
170
171static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
172{
173 struct rt_bandwidth *rt_b =
174 container_of(timer, struct rt_bandwidth, rt_period_timer);
175 ktime_t now;
176 int overrun;
177 int idle = 0;
178
179 for (;;) {
180 now = hrtimer_cb_get_time(timer);
181 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
182
183 if (!overrun)
184 break;
185
186 idle = do_sched_rt_period_timer(rt_b, overrun);
187 }
188
189 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
190}
191
192static
193void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
194{
195 rt_b->rt_period = ns_to_ktime(period);
196 rt_b->rt_runtime = runtime;
197
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200198 spin_lock_init(&rt_b->rt_runtime_lock);
199
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200200 hrtimer_init(&rt_b->rt_period_timer,
201 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
202 rt_b->rt_period_timer.function = sched_rt_period_timer;
203 rt_b->rt_period_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
204}
205
206static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
207{
208 ktime_t now;
209
210 if (rt_b->rt_runtime == RUNTIME_INF)
211 return;
212
213 if (hrtimer_active(&rt_b->rt_period_timer))
214 return;
215
216 spin_lock(&rt_b->rt_runtime_lock);
217 for (;;) {
218 if (hrtimer_active(&rt_b->rt_period_timer))
219 break;
220
221 now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
222 hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
223 hrtimer_start(&rt_b->rt_period_timer,
224 rt_b->rt_period_timer.expires,
225 HRTIMER_MODE_ABS);
226 }
227 spin_unlock(&rt_b->rt_runtime_lock);
228}
229
230#ifdef CONFIG_RT_GROUP_SCHED
231static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
232{
233 hrtimer_cancel(&rt_b->rt_period_timer);
234}
235#endif
236
Heiko Carstens712555e2008-04-28 11:33:07 +0200237/*
238 * sched_domains_mutex serializes calls to arch_init_sched_domains,
239 * detach_destroy_domains and partition_sched_domains.
240 */
241static DEFINE_MUTEX(sched_domains_mutex);
242
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100243#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200244
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700245#include <linux/cgroup.h>
246
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200247struct cfs_rq;
248
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100249static LIST_HEAD(task_groups);
250
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200251/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200252struct task_group {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100253#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700254 struct cgroup_subsys_state css;
255#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100256
257#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200258 /* schedulable entities of this group on each cpu */
259 struct sched_entity **se;
260 /* runqueue "owned" by this group on each cpu */
261 struct cfs_rq **cfs_rq;
262 unsigned long shares;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100263#endif
264
265#ifdef CONFIG_RT_GROUP_SCHED
266 struct sched_rt_entity **rt_se;
267 struct rt_rq **rt_rq;
268
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200269 struct rt_bandwidth rt_bandwidth;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100270#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100271
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100272 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100273 struct list_head list;
Peter Zijlstraf473aa52008-04-19 19:45:00 +0200274
275 struct task_group *parent;
276 struct list_head siblings;
277 struct list_head children;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200278};
279
Dhaval Giani354d60c2008-04-19 19:44:59 +0200280#ifdef CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200281
282/*
283 * Root task group.
284 * Every UID task group (including init_task_group aka UID-0) will
285 * be a child to this group.
286 */
287struct task_group root_task_group;
288
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100289#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200290/* Default task group's sched entity on each cpu */
291static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
292/* Default task group's cfs_rq on each cpu */
293static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200294#endif /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100295
296#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100297static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
298static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200299#endif /* CONFIG_RT_GROUP_SCHED */
300#else /* !CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200301#define root_task_group init_task_group
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200302#endif /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100303
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100304/* task_group_lock serializes add/remove of task groups and also changes to
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100305 * a task group's cpu shares.
306 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100307static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100308
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100309#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100310#ifdef CONFIG_USER_SCHED
Ingo Molnar0eab9142008-01-25 21:08:19 +0100311# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200312#else /* !CONFIG_USER_SCHED */
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100313# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200314#endif /* CONFIG_USER_SCHED */
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200315
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800316/*
Lai Jiangshan2e084782008-06-12 16:42:58 +0800317 * A weight of 0 or 1 can cause arithmetics problems.
318 * A weight of a cfs_rq is the sum of weights of which entities
319 * are queued on this cfs_rq, so a weight of a entity should not be
320 * too large, so as the shares value of a task group.
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800321 * (The default weight is 1024 - so there's no practical
322 * limitation from this.)
323 */
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200324#define MIN_SHARES 2
Lai Jiangshan2e084782008-06-12 16:42:58 +0800325#define MAX_SHARES (1UL << 18)
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200326
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100327static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100328#endif
329
330/* Default task group.
331 * Every task in system belong to this group at bootup.
332 */
Mike Travis434d53b2008-04-04 18:11:04 -0700333struct task_group init_task_group;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200334
335/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200336static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200337{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200338 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200339
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100340#ifdef CONFIG_USER_SCHED
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200341 tg = p->user->tg;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100342#elif defined(CONFIG_CGROUP_SCHED)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700343 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
344 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200345#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100346 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200347#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200348 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200349}
350
351/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100352static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200353{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100354#ifdef CONFIG_FAIR_GROUP_SCHED
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100355 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
356 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100357#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100358
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100359#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100360 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
361 p->rt.parent = task_group(p)->rt_se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100362#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200363}
364
365#else
366
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100367static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200368
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100369#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200370
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200371/* CFS-related fields in a runqueue */
372struct cfs_rq {
373 struct load_weight load;
374 unsigned long nr_running;
375
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200376 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200377 u64 min_vruntime;
Peter Zijlstra103638d92008-06-27 13:41:16 +0200378 u64 pair_start;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200379
380 struct rb_root tasks_timeline;
381 struct rb_node *rb_leftmost;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +0200382
383 struct list_head tasks;
384 struct list_head *balance_iterator;
385
386 /*
387 * 'curr' points to currently running entity on this cfs_rq.
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200388 * It is set to NULL otherwise (i.e when none are currently running).
389 */
Peter Zijlstraaa2ac252008-03-14 21:12:12 +0100390 struct sched_entity *curr, *next;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200391
392 unsigned long nr_spread_over;
393
Ingo Molnar62160e3f2007-10-15 17:00:03 +0200394#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200395 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
396
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100397 /*
398 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200399 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
400 * (like users, containers etc.)
401 *
402 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
403 * list is used during load balance.
404 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100405 struct list_head leaf_cfs_rq_list;
406 struct task_group *tg; /* group that "owns" this runqueue */
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200407
408#ifdef CONFIG_SMP
409 unsigned long task_weight;
410 unsigned long shares;
411 /*
412 * We need space to build a sched_domain wide view of the full task
413 * group tree, in order to avoid depending on dynamic memory allocation
414 * during the load balancing we place this in the per cpu task group
415 * hierarchy. This limits the load balancing to one instance per cpu,
416 * but more should not be needed anyway.
417 */
418 struct aggregate_struct {
419 /*
420 * load = weight(cpus) * f(tg)
421 *
422 * Where f(tg) is the recursive weight fraction assigned to
423 * this group.
424 */
425 unsigned long load;
426
427 /*
428 * part of the group weight distributed to this span.
429 */
430 unsigned long shares;
431
432 /*
433 * The sum of all runqueue weights within this span.
434 */
435 unsigned long rq_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200436 } aggregate;
437#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200438#endif
439};
440
441/* Real-Time classes' related field in a runqueue: */
442struct rt_rq {
443 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100444 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100445#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100446 int highest_prio; /* highest queued rt task prio */
447#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100448#ifdef CONFIG_SMP
Gregory Haskins73fe6aae2008-01-25 21:08:07 +0100449 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc12008-01-25 21:08:12 +0100450 int overloaded;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100451#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100452 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100453 u64 rt_time;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200454 u64 rt_runtime;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100455 /* Nests inside the rq lock: */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200456 spinlock_t rt_runtime_lock;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100457
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100458#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100459 unsigned long rt_nr_boosted;
460
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100461 struct rq *rq;
462 struct list_head leaf_rt_rq_list;
463 struct task_group *tg;
464 struct sched_rt_entity *rt_se;
465#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200466};
467
Gregory Haskins57d885f2008-01-25 21:08:18 +0100468#ifdef CONFIG_SMP
469
470/*
471 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100472 * variables. Each exclusive cpuset essentially defines an island domain by
473 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100474 * exclusive cpuset is created, we also create and attach a new root-domain
475 * object.
476 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100477 */
478struct root_domain {
479 atomic_t refcount;
480 cpumask_t span;
481 cpumask_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100482
Ingo Molnar0eab9142008-01-25 21:08:19 +0100483 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100484 * The "RT overload" flag: it gets set if a CPU has more than
485 * one runnable RT task.
486 */
487 cpumask_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100488 atomic_t rto_count;
Gregory Haskins6e0534f2008-05-12 21:21:01 +0200489#ifdef CONFIG_SMP
490 struct cpupri cpupri;
491#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +0100492};
493
Gregory Haskinsdc938522008-01-25 21:08:26 +0100494/*
495 * By default the system creates a single root-domain with all cpus as
496 * members (mimicking the global state we have today).
497 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100498static struct root_domain def_root_domain;
499
500#endif
501
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200502/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 * This is the main, per-CPU runqueue data structure.
504 *
505 * Locking rule: those places that want to lock multiple runqueues
506 * (such as the load balancing or the thread migration code), lock
507 * acquire operations must be ordered by ascending &runqueue.
508 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700509struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200510 /* runqueue lock: */
511 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 /*
514 * nr_running and cpu_load should be in the same cacheline because
515 * remote CPUs use both these fields when doing load calculation.
516 */
517 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200518 #define CPU_LOAD_IDX_MAX 5
519 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700520 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700521#ifdef CONFIG_NO_HZ
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200522 unsigned long last_tick_seen;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700523 unsigned char in_nohz_recently;
524#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200525 /* capture load from *all* tasks on this cpu: */
526 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200527 unsigned long nr_load_updates;
528 u64 nr_switches;
529
530 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100531 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100532
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200533#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200534 /* list of leaf cfs_rq on this cpu: */
535 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100536#endif
537#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100538 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 /*
542 * This is part of a global counter where only the total sum
543 * over all CPUs matters. A task can increase this counter on
544 * one CPU and if it got migrated afterwards it may decrease
545 * it on another CPU. Always updated under the runqueue lock:
546 */
547 unsigned long nr_uninterruptible;
548
Ingo Molnar36c8b582006-07-03 00:25:41 -0700549 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800550 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200552
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200553 u64 clock;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 atomic_t nr_iowait;
556
557#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100558 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 struct sched_domain *sd;
560
561 /* For active balancing */
562 int active_balance;
563 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200564 /* cpu of this runqueue: */
565 int cpu;
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -0400566 int online;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Ingo Molnar36c8b582006-07-03 00:25:41 -0700568 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 struct list_head migration_queue;
570#endif
571
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100572#ifdef CONFIG_SCHED_HRTICK
573 unsigned long hrtick_flags;
574 ktime_t hrtick_expire;
575 struct hrtimer hrtick_timer;
576#endif
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578#ifdef CONFIG_SCHEDSTATS
579 /* latency stats */
580 struct sched_info rq_sched_info;
581
582 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200583 unsigned int yld_exp_empty;
584 unsigned int yld_act_empty;
585 unsigned int yld_both_empty;
586 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200589 unsigned int sched_switch;
590 unsigned int sched_count;
591 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200594 unsigned int ttwu_count;
595 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200596
597 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200598 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599#endif
Ingo Molnarfcb99372006-07-03 00:25:10 -0700600 struct lock_class_key rq_lock_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601};
602
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700603static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Ingo Molnardd41f592007-07-09 18:51:59 +0200605static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
606{
607 rq->curr->sched_class->check_preempt_curr(rq, p);
608}
609
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700610static inline int cpu_of(struct rq *rq)
611{
612#ifdef CONFIG_SMP
613 return rq->cpu;
614#else
615 return 0;
616#endif
617}
618
Ingo Molnar20d315d2007-07-09 18:51:58 +0200619/*
Nick Piggin674311d2005-06-25 14:57:27 -0700620 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700621 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700622 *
623 * The domain tree of any CPU may only be accessed from within
624 * preempt-disabled sections.
625 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700626#define for_each_domain(cpu, __sd) \
627 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
630#define this_rq() (&__get_cpu_var(runqueues))
631#define task_rq(p) cpu_rq(task_cpu(p))
632#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
633
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200634static inline void update_rq_clock(struct rq *rq)
635{
636 rq->clock = sched_clock_cpu(cpu_of(rq));
637}
638
Ingo Molnare436d802007-07-19 21:28:35 +0200639/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200640 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
641 */
642#ifdef CONFIG_SCHED_DEBUG
643# define const_debug __read_mostly
644#else
645# define const_debug static const
646#endif
647
648/*
649 * Debugging: various feature bits
650 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200651
652#define SCHED_FEAT(name, enabled) \
653 __SCHED_FEAT_##name ,
654
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200655enum {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200656#include "sched_features.h"
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200657};
658
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200659#undef SCHED_FEAT
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200660
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200661#define SCHED_FEAT(name, enabled) \
662 (1UL << __SCHED_FEAT_##name) * enabled |
663
664const_debug unsigned int sysctl_sched_features =
665#include "sched_features.h"
666 0;
667
668#undef SCHED_FEAT
669
670#ifdef CONFIG_SCHED_DEBUG
671#define SCHED_FEAT(name, enabled) \
672 #name ,
673
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700674static __read_mostly char *sched_feat_names[] = {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200675#include "sched_features.h"
676 NULL
677};
678
679#undef SCHED_FEAT
680
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700681static int sched_feat_open(struct inode *inode, struct file *filp)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200682{
683 filp->private_data = inode->i_private;
684 return 0;
685}
686
687static ssize_t
688sched_feat_read(struct file *filp, char __user *ubuf,
689 size_t cnt, loff_t *ppos)
690{
691 char *buf;
692 int r = 0;
693 int len = 0;
694 int i;
695
696 for (i = 0; sched_feat_names[i]; i++) {
697 len += strlen(sched_feat_names[i]);
698 len += 4;
699 }
700
701 buf = kmalloc(len + 2, GFP_KERNEL);
702 if (!buf)
703 return -ENOMEM;
704
705 for (i = 0; sched_feat_names[i]; i++) {
706 if (sysctl_sched_features & (1UL << i))
707 r += sprintf(buf + r, "%s ", sched_feat_names[i]);
708 else
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200709 r += sprintf(buf + r, "NO_%s ", sched_feat_names[i]);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200710 }
711
712 r += sprintf(buf + r, "\n");
713 WARN_ON(r >= len + 2);
714
715 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
716
717 kfree(buf);
718
719 return r;
720}
721
722static ssize_t
723sched_feat_write(struct file *filp, const char __user *ubuf,
724 size_t cnt, loff_t *ppos)
725{
726 char buf[64];
727 char *cmp = buf;
728 int neg = 0;
729 int i;
730
731 if (cnt > 63)
732 cnt = 63;
733
734 if (copy_from_user(&buf, ubuf, cnt))
735 return -EFAULT;
736
737 buf[cnt] = 0;
738
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200739 if (strncmp(buf, "NO_", 3) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200740 neg = 1;
741 cmp += 3;
742 }
743
744 for (i = 0; sched_feat_names[i]; i++) {
745 int len = strlen(sched_feat_names[i]);
746
747 if (strncmp(cmp, sched_feat_names[i], len) == 0) {
748 if (neg)
749 sysctl_sched_features &= ~(1UL << i);
750 else
751 sysctl_sched_features |= (1UL << i);
752 break;
753 }
754 }
755
756 if (!sched_feat_names[i])
757 return -EINVAL;
758
759 filp->f_pos += cnt;
760
761 return cnt;
762}
763
764static struct file_operations sched_feat_fops = {
765 .open = sched_feat_open,
766 .read = sched_feat_read,
767 .write = sched_feat_write,
768};
769
770static __init int sched_init_debug(void)
771{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200772 debugfs_create_file("sched_features", 0644, NULL, NULL,
773 &sched_feat_fops);
774
775 return 0;
776}
777late_initcall(sched_init_debug);
778
779#endif
780
781#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200782
783/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100784 * Number of tasks to iterate in a single balance run.
785 * Limited because this is done with IRQs disabled.
786 */
787const_debug unsigned int sysctl_sched_nr_migrate = 32;
788
789/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100790 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100791 * default: 1s
792 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100793unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100794
Ingo Molnar6892b752008-02-13 14:02:36 +0100795static __read_mostly int scheduler_running;
796
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100797/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100798 * part of the period that we allow rt tasks to run in us.
799 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100800 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100801int sysctl_sched_rt_runtime = 950000;
802
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200803static inline u64 global_rt_period(void)
804{
805 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
806}
807
808static inline u64 global_rt_runtime(void)
809{
810 if (sysctl_sched_rt_period < 0)
811 return RUNTIME_INF;
812
813 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
814}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700817# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700819#ifndef finish_arch_switch
820# define finish_arch_switch(prev) do { } while (0)
821#endif
822
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100823static inline int task_current(struct rq *rq, struct task_struct *p)
824{
825 return rq->curr == p;
826}
827
Nick Piggin4866cde2005-06-25 14:57:23 -0700828#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700829static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700830{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100831 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700832}
833
Ingo Molnar70b97a72006-07-03 00:25:42 -0700834static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700835{
836}
837
Ingo Molnar70b97a72006-07-03 00:25:42 -0700838static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700839{
Ingo Molnarda04c032005-09-13 11:17:59 +0200840#ifdef CONFIG_DEBUG_SPINLOCK
841 /* this is a valid case when another task releases the spinlock */
842 rq->lock.owner = current;
843#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700844 /*
845 * If we are tracking spinlock dependencies then we have to
846 * fix up the runqueue lock - which gets 'carried over' from
847 * prev into current:
848 */
849 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
850
Nick Piggin4866cde2005-06-25 14:57:23 -0700851 spin_unlock_irq(&rq->lock);
852}
853
854#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700855static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700856{
857#ifdef CONFIG_SMP
858 return p->oncpu;
859#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100860 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700861#endif
862}
863
Ingo Molnar70b97a72006-07-03 00:25:42 -0700864static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700865{
866#ifdef CONFIG_SMP
867 /*
868 * We can optimise this out completely for !SMP, because the
869 * SMP rebalancing from interrupt is the only thing that cares
870 * here.
871 */
872 next->oncpu = 1;
873#endif
874#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
875 spin_unlock_irq(&rq->lock);
876#else
877 spin_unlock(&rq->lock);
878#endif
879}
880
Ingo Molnar70b97a72006-07-03 00:25:42 -0700881static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700882{
883#ifdef CONFIG_SMP
884 /*
885 * After ->oncpu is cleared, the task can be moved to a different CPU.
886 * We must ensure this doesn't happen until the switch is completely
887 * finished.
888 */
889 smp_wmb();
890 prev->oncpu = 0;
891#endif
892#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
893 local_irq_enable();
894#endif
895}
896#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700899 * __task_rq_lock - lock the runqueue a given task resides on.
900 * Must be called interrupts disabled.
901 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700902static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700903 __acquires(rq->lock)
904{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200905 for (;;) {
906 struct rq *rq = task_rq(p);
907 spin_lock(&rq->lock);
908 if (likely(rq == task_rq(p)))
909 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700910 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700911 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700912}
913
914/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100916 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 * explicitly disabling preemption.
918 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700919static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 __acquires(rq->lock)
921{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700922 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Andi Kleen3a5c3592007-10-15 17:00:14 +0200924 for (;;) {
925 local_irq_save(*flags);
926 rq = task_rq(p);
927 spin_lock(&rq->lock);
928 if (likely(rq == task_rq(p)))
929 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Alexey Dobriyana9957442007-10-15 17:00:13 +0200934static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700935 __releases(rq->lock)
936{
937 spin_unlock(&rq->lock);
938}
939
Ingo Molnar70b97a72006-07-03 00:25:42 -0700940static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 __releases(rq->lock)
942{
943 spin_unlock_irqrestore(&rq->lock, *flags);
944}
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800947 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200949static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 __acquires(rq->lock)
951{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700952 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 local_irq_disable();
955 rq = this_rq();
956 spin_lock(&rq->lock);
957
958 return rq;
959}
960
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100961static void __resched_task(struct task_struct *p, int tif_bit);
962
963static inline void resched_task(struct task_struct *p)
964{
965 __resched_task(p, TIF_NEED_RESCHED);
966}
967
968#ifdef CONFIG_SCHED_HRTICK
969/*
970 * Use HR-timers to deliver accurate preemption points.
971 *
972 * Its all a bit involved since we cannot program an hrt while holding the
973 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
974 * reschedule event.
975 *
976 * When we get rescheduled we reprogram the hrtick_timer outside of the
977 * rq->lock.
978 */
979static inline void resched_hrt(struct task_struct *p)
980{
981 __resched_task(p, TIF_HRTICK_RESCHED);
982}
983
984static inline void resched_rq(struct rq *rq)
985{
986 unsigned long flags;
987
988 spin_lock_irqsave(&rq->lock, flags);
989 resched_task(rq->curr);
990 spin_unlock_irqrestore(&rq->lock, flags);
991}
992
993enum {
994 HRTICK_SET, /* re-programm hrtick_timer */
995 HRTICK_RESET, /* not a new slice */
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200996 HRTICK_BLOCK, /* stop hrtick operations */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100997};
998
999/*
1000 * Use hrtick when:
1001 * - enabled by features
1002 * - hrtimer is actually high res
1003 */
1004static inline int hrtick_enabled(struct rq *rq)
1005{
1006 if (!sched_feat(HRTICK))
1007 return 0;
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001008 if (unlikely(test_bit(HRTICK_BLOCK, &rq->hrtick_flags)))
1009 return 0;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001010 return hrtimer_is_hres_active(&rq->hrtick_timer);
1011}
1012
1013/*
1014 * Called to set the hrtick timer state.
1015 *
1016 * called with rq->lock held and irqs disabled
1017 */
1018static void hrtick_start(struct rq *rq, u64 delay, int reset)
1019{
1020 assert_spin_locked(&rq->lock);
1021
1022 /*
1023 * preempt at: now + delay
1024 */
1025 rq->hrtick_expire =
1026 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
1027 /*
1028 * indicate we need to program the timer
1029 */
1030 __set_bit(HRTICK_SET, &rq->hrtick_flags);
1031 if (reset)
1032 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
1033
1034 /*
1035 * New slices are called from the schedule path and don't need a
1036 * forced reschedule.
1037 */
1038 if (reset)
1039 resched_hrt(rq->curr);
1040}
1041
1042static void hrtick_clear(struct rq *rq)
1043{
1044 if (hrtimer_active(&rq->hrtick_timer))
1045 hrtimer_cancel(&rq->hrtick_timer);
1046}
1047
1048/*
1049 * Update the timer from the possible pending state.
1050 */
1051static void hrtick_set(struct rq *rq)
1052{
1053 ktime_t time;
1054 int set, reset;
1055 unsigned long flags;
1056
1057 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1058
1059 spin_lock_irqsave(&rq->lock, flags);
1060 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
1061 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
1062 time = rq->hrtick_expire;
1063 clear_thread_flag(TIF_HRTICK_RESCHED);
1064 spin_unlock_irqrestore(&rq->lock, flags);
1065
1066 if (set) {
1067 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
1068 if (reset && !hrtimer_active(&rq->hrtick_timer))
1069 resched_rq(rq);
1070 } else
1071 hrtick_clear(rq);
1072}
1073
1074/*
1075 * High-resolution timer tick.
1076 * Runs from hardirq context with interrupts disabled.
1077 */
1078static enum hrtimer_restart hrtick(struct hrtimer *timer)
1079{
1080 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1081
1082 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1083
1084 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02001085 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001086 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1087 spin_unlock(&rq->lock);
1088
1089 return HRTIMER_NORESTART;
1090}
1091
Rabin Vincent81d41d72008-05-11 05:55:33 +05301092#ifdef CONFIG_SMP
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001093static void hotplug_hrtick_disable(int cpu)
1094{
1095 struct rq *rq = cpu_rq(cpu);
1096 unsigned long flags;
1097
1098 spin_lock_irqsave(&rq->lock, flags);
1099 rq->hrtick_flags = 0;
1100 __set_bit(HRTICK_BLOCK, &rq->hrtick_flags);
1101 spin_unlock_irqrestore(&rq->lock, flags);
1102
1103 hrtick_clear(rq);
1104}
1105
1106static void hotplug_hrtick_enable(int cpu)
1107{
1108 struct rq *rq = cpu_rq(cpu);
1109 unsigned long flags;
1110
1111 spin_lock_irqsave(&rq->lock, flags);
1112 __clear_bit(HRTICK_BLOCK, &rq->hrtick_flags);
1113 spin_unlock_irqrestore(&rq->lock, flags);
1114}
1115
1116static int
1117hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
1118{
1119 int cpu = (int)(long)hcpu;
1120
1121 switch (action) {
1122 case CPU_UP_CANCELED:
1123 case CPU_UP_CANCELED_FROZEN:
1124 case CPU_DOWN_PREPARE:
1125 case CPU_DOWN_PREPARE_FROZEN:
1126 case CPU_DEAD:
1127 case CPU_DEAD_FROZEN:
1128 hotplug_hrtick_disable(cpu);
1129 return NOTIFY_OK;
1130
1131 case CPU_UP_PREPARE:
1132 case CPU_UP_PREPARE_FROZEN:
1133 case CPU_DOWN_FAILED:
1134 case CPU_DOWN_FAILED_FROZEN:
1135 case CPU_ONLINE:
1136 case CPU_ONLINE_FROZEN:
1137 hotplug_hrtick_enable(cpu);
1138 return NOTIFY_OK;
1139 }
1140
1141 return NOTIFY_DONE;
1142}
1143
1144static void init_hrtick(void)
1145{
1146 hotcpu_notifier(hotplug_hrtick, 0);
1147}
Rabin Vincent81d41d72008-05-11 05:55:33 +05301148#endif /* CONFIG_SMP */
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001149
1150static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001151{
1152 rq->hrtick_flags = 0;
1153 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1154 rq->hrtick_timer.function = hrtick;
1155 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1156}
1157
1158void hrtick_resched(void)
1159{
1160 struct rq *rq;
1161 unsigned long flags;
1162
1163 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1164 return;
1165
1166 local_irq_save(flags);
1167 rq = cpu_rq(smp_processor_id());
1168 hrtick_set(rq);
1169 local_irq_restore(flags);
1170}
1171#else
1172static inline void hrtick_clear(struct rq *rq)
1173{
1174}
1175
1176static inline void hrtick_set(struct rq *rq)
1177{
1178}
1179
1180static inline void init_rq_hrtick(struct rq *rq)
1181{
1182}
1183
1184void hrtick_resched(void)
1185{
1186}
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001187
1188static inline void init_hrtick(void)
1189{
1190}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001191#endif
1192
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001193/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001194 * resched_task - mark a task 'to be rescheduled now'.
1195 *
1196 * On UP this means the setting of the need_resched flag, on SMP it
1197 * might also involve a cross-CPU call to trigger the scheduler on
1198 * the target CPU.
1199 */
1200#ifdef CONFIG_SMP
1201
1202#ifndef tsk_is_polling
1203#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1204#endif
1205
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001206static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001207{
1208 int cpu;
1209
1210 assert_spin_locked(&task_rq(p)->lock);
1211
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001212 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001213 return;
1214
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001215 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001216
1217 cpu = task_cpu(p);
1218 if (cpu == smp_processor_id())
1219 return;
1220
1221 /* NEED_RESCHED must be visible before we test polling */
1222 smp_mb();
1223 if (!tsk_is_polling(p))
1224 smp_send_reschedule(cpu);
1225}
1226
1227static void resched_cpu(int cpu)
1228{
1229 struct rq *rq = cpu_rq(cpu);
1230 unsigned long flags;
1231
1232 if (!spin_trylock_irqsave(&rq->lock, flags))
1233 return;
1234 resched_task(cpu_curr(cpu));
1235 spin_unlock_irqrestore(&rq->lock, flags);
1236}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001237
1238#ifdef CONFIG_NO_HZ
1239/*
1240 * When add_timer_on() enqueues a timer into the timer wheel of an
1241 * idle CPU then this timer might expire before the next timer event
1242 * which is scheduled to wake up that CPU. In case of a completely
1243 * idle system the next event might even be infinite time into the
1244 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1245 * leaves the inner idle loop so the newly added timer is taken into
1246 * account when the CPU goes back to idle and evaluates the timer
1247 * wheel for the next timer event.
1248 */
1249void wake_up_idle_cpu(int cpu)
1250{
1251 struct rq *rq = cpu_rq(cpu);
1252
1253 if (cpu == smp_processor_id())
1254 return;
1255
1256 /*
1257 * This is safe, as this function is called with the timer
1258 * wheel base lock of (cpu) held. When the CPU is on the way
1259 * to idle and has not yet set rq->curr to idle then it will
1260 * be serialized on the timer wheel base lock and take the new
1261 * timer into account automatically.
1262 */
1263 if (rq->curr != rq->idle)
1264 return;
1265
1266 /*
1267 * We can set TIF_RESCHED on the idle task of the other CPU
1268 * lockless. The worst case is that the other CPU runs the
1269 * idle task through an additional NOOP schedule()
1270 */
1271 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1272
1273 /* NEED_RESCHED must be visible before we test polling */
1274 smp_mb();
1275 if (!tsk_is_polling(rq->idle))
1276 smp_send_reschedule(cpu);
1277}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001278#endif /* CONFIG_NO_HZ */
Thomas Gleixner06d83082008-03-22 09:20:24 +01001279
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001280#else /* !CONFIG_SMP */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001281static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001282{
1283 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001284 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001285}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001286#endif /* CONFIG_SMP */
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001287
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001288#if BITS_PER_LONG == 32
1289# define WMULT_CONST (~0UL)
1290#else
1291# define WMULT_CONST (1UL << 32)
1292#endif
1293
1294#define WMULT_SHIFT 32
1295
Ingo Molnar194081e2007-08-09 11:16:51 +02001296/*
1297 * Shift right and round:
1298 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001299#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001300
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001301/*
1302 * delta *= weight / lw
1303 */
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001304static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001305calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1306 struct load_weight *lw)
1307{
1308 u64 tmp;
1309
Lai Jiangshan7a232e02008-06-12 16:43:07 +08001310 if (!lw->inv_weight) {
1311 if (BITS_PER_LONG > 32 && unlikely(lw->weight >= WMULT_CONST))
1312 lw->inv_weight = 1;
1313 else
1314 lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)
1315 / (lw->weight+1);
1316 }
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001317
1318 tmp = (u64)delta_exec * weight;
1319 /*
1320 * Check whether we'd overflow the 64-bit multiplication:
1321 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001322 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001323 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001324 WMULT_SHIFT/2);
1325 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001326 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001327
Ingo Molnarecf691d2007-08-02 17:41:40 +02001328 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001329}
1330
Ingo Molnar10919852007-10-15 17:00:04 +02001331static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001332{
1333 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001334 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001335}
1336
Ingo Molnar10919852007-10-15 17:00:04 +02001337static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001338{
1339 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001340 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001341}
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001344 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1345 * of tasks with abnormal "nice" values across CPUs the contribution that
1346 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001347 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001348 * scaled version of the new time slice allocation that they receive on time
1349 * slice expiry etc.
1350 */
1351
Ingo Molnardd41f592007-07-09 18:51:59 +02001352#define WEIGHT_IDLEPRIO 2
1353#define WMULT_IDLEPRIO (1 << 31)
1354
1355/*
1356 * Nice levels are multiplicative, with a gentle 10% change for every
1357 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1358 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1359 * that remained on nice 0.
1360 *
1361 * The "10% effect" is relative and cumulative: from _any_ nice level,
1362 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee62007-07-16 09:46:30 +02001363 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1364 * If a task goes up by ~10% and another task goes down by ~10% then
1365 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001366 */
1367static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001368 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1369 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1370 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1371 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1372 /* 0 */ 1024, 820, 655, 526, 423,
1373 /* 5 */ 335, 272, 215, 172, 137,
1374 /* 10 */ 110, 87, 70, 56, 45,
1375 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001376};
1377
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001378/*
1379 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1380 *
1381 * In cases where the weight does not change often, we can use the
1382 * precalculated inverse to speed up arithmetics by turning divisions
1383 * into multiplications:
1384 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001385static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001386 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1387 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1388 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1389 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1390 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1391 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1392 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1393 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001394};
Peter Williams2dd73a42006-06-27 02:54:34 -07001395
Ingo Molnardd41f592007-07-09 18:51:59 +02001396static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1397
1398/*
1399 * runqueue iterator, to support SMP load-balancing between different
1400 * scheduling classes, without having to expose their internal data
1401 * structures to the load-balancing proper:
1402 */
1403struct rq_iterator {
1404 void *arg;
1405 struct task_struct *(*start)(void *);
1406 struct task_struct *(*next)(void *);
1407};
1408
Peter Williamse1d14842007-10-24 18:23:51 +02001409#ifdef CONFIG_SMP
1410static unsigned long
1411balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1412 unsigned long max_load_move, struct sched_domain *sd,
1413 enum cpu_idle_type idle, int *all_pinned,
1414 int *this_best_prio, struct rq_iterator *iterator);
1415
1416static int
1417iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1418 struct sched_domain *sd, enum cpu_idle_type idle,
1419 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001420#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001421
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001422#ifdef CONFIG_CGROUP_CPUACCT
1423static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1424#else
1425static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1426#endif
1427
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001428static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1429{
1430 update_load_add(&rq->load, load);
1431}
1432
1433static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1434{
1435 update_load_sub(&rq->load, load);
1436}
1437
Gregory Haskinse7693a32008-01-25 21:08:09 +01001438#ifdef CONFIG_SMP
1439static unsigned long source_load(int cpu, int type);
1440static unsigned long target_load(int cpu, int type);
1441static unsigned long cpu_avg_load_per_task(int cpu);
1442static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001443
1444#ifdef CONFIG_FAIR_GROUP_SCHED
1445
1446/*
1447 * Group load balancing.
1448 *
1449 * We calculate a few balance domain wide aggregate numbers; load and weight.
1450 * Given the pictures below, and assuming each item has equal weight:
1451 *
1452 * root 1 - thread
1453 * / | \ A - group
1454 * A 1 B
1455 * /|\ / \
1456 * C 2 D 3 4
1457 * | |
1458 * 5 6
1459 *
1460 * load:
1461 * A and B get 1/3-rd of the total load. C and D get 1/3-rd of A's 1/3-rd,
1462 * which equals 1/9-th of the total load.
1463 *
1464 * shares:
1465 * The weight of this group on the selected cpus.
1466 *
1467 * rq_weight:
1468 * Direct sum of all the cpu's their rq weight, e.g. A would get 3 while
1469 * B would get 2.
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001470 */
1471
1472static inline struct aggregate_struct *
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001473aggregate(struct task_group *tg, int cpu)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001474{
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001475 return &tg->cfs_rq[cpu]->aggregate;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001476}
1477
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001478typedef void (*aggregate_func)(struct task_group *, int, struct sched_domain *);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001479
1480/*
1481 * Iterate the full tree, calling @down when first entering a node and @up when
1482 * leaving it for the final time.
1483 */
1484static
1485void aggregate_walk_tree(aggregate_func down, aggregate_func up,
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001486 int cpu, struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001487{
1488 struct task_group *parent, *child;
1489
1490 rcu_read_lock();
1491 parent = &root_task_group;
1492down:
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001493 (*down)(parent, cpu, sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001494 list_for_each_entry_rcu(child, &parent->children, siblings) {
1495 parent = child;
1496 goto down;
1497
1498up:
1499 continue;
1500 }
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001501 (*up)(parent, cpu, sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001502
1503 child = parent;
1504 parent = parent->parent;
1505 if (parent)
1506 goto up;
1507 rcu_read_unlock();
1508}
1509
1510/*
1511 * Calculate the aggregate runqueue weight.
1512 */
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001513static void
1514aggregate_group_weight(struct task_group *tg, int cpu, struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001515{
1516 unsigned long rq_weight = 0;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001517 int i;
1518
Peter Zijlstraa25b5ac2008-06-27 13:41:22 +02001519 for_each_cpu_mask(i, sd->span)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001520 rq_weight += tg->cfs_rq[i]->load.weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001521
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001522 aggregate(tg, cpu)->rq_weight = rq_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001523}
1524
1525/*
1526 * Compute the weight of this group on the given cpus.
1527 */
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001528static void
1529aggregate_group_shares(struct task_group *tg, int cpu, struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001530{
1531 unsigned long shares = 0;
1532 int i;
1533
1534 for_each_cpu_mask(i, sd->span)
1535 shares += tg->cfs_rq[i]->shares;
1536
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001537 if ((!shares && aggregate(tg, cpu)->rq_weight) || shares > tg->shares)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001538 shares = tg->shares;
1539
Peter Zijlstrad3f40db2008-06-27 13:41:21 +02001540 if (!sd->parent || !(sd->parent->flags & SD_LOAD_BALANCE))
1541 shares = tg->shares;
1542
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001543 aggregate(tg, cpu)->shares = shares;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001544}
1545
1546/*
1547 * Compute the load fraction assigned to this group, relies on the aggregate
1548 * weight and this group's parent's load, i.e. top-down.
1549 */
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001550static void
1551aggregate_group_load(struct task_group *tg, int cpu, struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001552{
1553 unsigned long load;
1554
1555 if (!tg->parent) {
1556 int i;
1557
1558 load = 0;
1559 for_each_cpu_mask(i, sd->span)
1560 load += cpu_rq(i)->load.weight;
1561
1562 } else {
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001563 load = aggregate(tg->parent, cpu)->load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001564
1565 /*
1566 * shares is our weight in the parent's rq so
1567 * shares/parent->rq_weight gives our fraction of the load
1568 */
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001569 load *= aggregate(tg, cpu)->shares;
1570 load /= aggregate(tg->parent, cpu)->rq_weight + 1;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001571 }
1572
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001573 aggregate(tg, cpu)->load = load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001574}
1575
1576static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1577
1578/*
1579 * Calculate and set the cpu's group shares.
1580 */
1581static void
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001582__update_group_shares_cpu(struct task_group *tg, int cpu,
1583 struct sched_domain *sd, int tcpu)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001584{
1585 int boost = 0;
1586 unsigned long shares;
1587 unsigned long rq_weight;
1588
1589 if (!tg->se[tcpu])
1590 return;
1591
1592 rq_weight = tg->cfs_rq[tcpu]->load.weight;
1593
1594 /*
1595 * If there are currently no tasks on the cpu pretend there is one of
1596 * average load so that when a new task gets to run here it will not
1597 * get delayed by group starvation.
1598 */
1599 if (!rq_weight) {
1600 boost = 1;
1601 rq_weight = NICE_0_LOAD;
1602 }
1603
1604 /*
1605 * \Sum shares * rq_weight
1606 * shares = -----------------------
1607 * \Sum rq_weight
1608 *
1609 */
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001610 shares = aggregate(tg, cpu)->shares * rq_weight;
1611 shares /= aggregate(tg, cpu)->rq_weight + 1;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001612
1613 /*
1614 * record the actual number of shares, not the boosted amount.
1615 */
1616 tg->cfs_rq[tcpu]->shares = boost ? 0 : shares;
1617
1618 if (shares < MIN_SHARES)
1619 shares = MIN_SHARES;
1620 else if (shares > MAX_SHARES)
1621 shares = MAX_SHARES;
1622
1623 __set_se_shares(tg->se[tcpu], shares);
1624}
1625
1626/*
1627 * Re-adjust the weights on the cpu the task came from and on the cpu the
1628 * task went to.
1629 */
1630static void
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001631__move_group_shares(struct task_group *tg, int cpu, struct sched_domain *sd,
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001632 int scpu, int dcpu)
1633{
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001634 __update_group_shares_cpu(tg, cpu, sd, scpu);
1635 __update_group_shares_cpu(tg, cpu, sd, dcpu);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001636}
1637
1638/*
1639 * Because changing a group's shares changes the weight of the super-group
1640 * we need to walk up the tree and change all shares until we hit the root.
1641 */
1642static void
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001643move_group_shares(struct task_group *tg, int cpu, struct sched_domain *sd,
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001644 int scpu, int dcpu)
1645{
1646 while (tg) {
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001647 __move_group_shares(tg, cpu, sd, scpu, dcpu);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001648 tg = tg->parent;
1649 }
1650}
1651
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001652static void
1653aggregate_group_set_shares(struct task_group *tg, int cpu, struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001654{
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001655 int i;
1656
1657 for_each_cpu_mask(i, sd->span) {
1658 struct rq *rq = cpu_rq(i);
1659 unsigned long flags;
1660
1661 spin_lock_irqsave(&rq->lock, flags);
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001662 __update_group_shares_cpu(tg, cpu, sd, i);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001663 spin_unlock_irqrestore(&rq->lock, flags);
1664 }
1665
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001666 aggregate_group_shares(tg, cpu, sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001667}
1668
1669/*
1670 * Calculate the accumulative weight and recursive load of each task group
1671 * while walking down the tree.
1672 */
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001673static void
1674aggregate_get_down(struct task_group *tg, int cpu, struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001675{
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001676 aggregate_group_weight(tg, cpu, sd);
1677 aggregate_group_shares(tg, cpu, sd);
1678 aggregate_group_load(tg, cpu, sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001679}
1680
1681/*
1682 * Rebalance the cpu shares while walking back up the tree.
1683 */
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001684static void
1685aggregate_get_up(struct task_group *tg, int cpu, struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001686{
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001687 aggregate_group_set_shares(tg, cpu, sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001688}
1689
Peter Zijlstra4d8d5952008-06-27 13:41:19 +02001690static void
1691aggregate_get_nop(struct task_group *tg, int cpu, struct sched_domain *sd)
1692{
1693}
1694
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001695static DEFINE_PER_CPU(spinlock_t, aggregate_lock);
1696
1697static void __init init_aggregate(void)
1698{
1699 int i;
1700
1701 for_each_possible_cpu(i)
1702 spin_lock_init(&per_cpu(aggregate_lock, i));
1703}
1704
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001705static int get_aggregate(int cpu, struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001706{
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001707 if (!spin_trylock(&per_cpu(aggregate_lock, cpu)))
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001708 return 0;
1709
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001710 aggregate_walk_tree(aggregate_get_down, aggregate_get_up, cpu, sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001711 return 1;
1712}
1713
Peter Zijlstra4d8d5952008-06-27 13:41:19 +02001714static void update_aggregate(int cpu, struct sched_domain *sd)
1715{
1716 aggregate_walk_tree(aggregate_get_down, aggregate_get_nop, cpu, sd);
1717}
1718
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001719static void put_aggregate(int cpu, struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001720{
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001721 spin_unlock(&per_cpu(aggregate_lock, cpu));
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001722}
1723
1724static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1725{
1726 cfs_rq->shares = shares;
1727}
1728
1729#else
1730
1731static inline void init_aggregate(void)
1732{
1733}
1734
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001735static inline int get_aggregate(int cpu, struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001736{
1737 return 0;
1738}
1739
Peter Zijlstra4d8d5952008-06-27 13:41:19 +02001740static inline void update_aggregate(int cpu, struct sched_domain *sd)
1741{
1742}
1743
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02001744static inline void put_aggregate(int cpu, struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001745{
1746}
1747#endif
1748
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001749#endif
1750
Ingo Molnardd41f592007-07-09 18:51:59 +02001751#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001752#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001753#include "sched_fair.c"
1754#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001755#ifdef CONFIG_SCHED_DEBUG
1756# include "sched_debug.c"
1757#endif
1758
1759#define sched_class_highest (&rt_sched_class)
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04001760#define for_each_class(class) \
1761 for (class = sched_class_highest; class; class = class->next)
Ingo Molnardd41f592007-07-09 18:51:59 +02001762
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001763static void inc_nr_running(struct rq *rq)
Ingo Molnar6363ca52008-05-29 11:28:57 +02001764{
1765 rq->nr_running++;
Ingo Molnar6363ca52008-05-29 11:28:57 +02001766}
1767
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001768static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001769{
1770 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001771}
1772
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001773static void set_load_weight(struct task_struct *p)
1774{
1775 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001776 p->se.load.weight = prio_to_weight[0] * 2;
1777 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1778 return;
1779 }
1780
1781 /*
1782 * SCHED_IDLE tasks get minimal weight:
1783 */
1784 if (p->policy == SCHED_IDLE) {
1785 p->se.load.weight = WEIGHT_IDLEPRIO;
1786 p->se.load.inv_weight = WMULT_IDLEPRIO;
1787 return;
1788 }
1789
1790 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1791 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001792}
1793
Ingo Molnar8159f872007-08-09 11:16:49 +02001794static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001795{
1796 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001797 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001798 p->se.on_rq = 1;
1799}
1800
Ingo Molnar69be72c2007-08-09 11:16:49 +02001801static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001802{
Ingo Molnarf02231e2007-08-09 11:16:48 +02001803 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001804 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001805}
1806
1807/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001808 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001809 */
Ingo Molnar14531182007-07-09 18:51:59 +02001810static inline int __normal_prio(struct task_struct *p)
1811{
Ingo Molnardd41f592007-07-09 18:51:59 +02001812 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001813}
1814
1815/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001816 * Calculate the expected normal priority: i.e. priority
1817 * without taking RT-inheritance into account. Might be
1818 * boosted by interactivity modifiers. Changes upon fork,
1819 * setprio syscalls, and whenever the interactivity
1820 * estimator recalculates.
1821 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001822static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001823{
1824 int prio;
1825
Ingo Molnare05606d2007-07-09 18:51:59 +02001826 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001827 prio = MAX_RT_PRIO-1 - p->rt_priority;
1828 else
1829 prio = __normal_prio(p);
1830 return prio;
1831}
1832
1833/*
1834 * Calculate the current priority, i.e. the priority
1835 * taken into account by the scheduler. This value might
1836 * be boosted by RT tasks, or might be boosted by
1837 * interactivity modifiers. Will be RT if the task got
1838 * RT-boosted. If not then it returns p->normal_prio.
1839 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001840static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001841{
1842 p->normal_prio = normal_prio(p);
1843 /*
1844 * If we are RT tasks or we were boosted to RT priority,
1845 * keep the priority unchanged. Otherwise, update priority
1846 * to the normal priority:
1847 */
1848 if (!rt_prio(p->prio))
1849 return p->normal_prio;
1850 return p->prio;
1851}
1852
1853/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001854 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001856static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001858 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001859 rq->nr_uninterruptible--;
1860
Ingo Molnar8159f872007-08-09 11:16:49 +02001861 enqueue_task(rq, p, wakeup);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001862 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864
1865/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 * deactivate_task - remove a task from the runqueue.
1867 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02001868static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001870 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001871 rq->nr_uninterruptible++;
1872
Ingo Molnar69be72c2007-08-09 11:16:49 +02001873 dequeue_task(rq, p, sleep);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001874 dec_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875}
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877/**
1878 * task_curr - is this task currently executing on a CPU?
1879 * @p: the task in question.
1880 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001881inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
1883 return cpu_curr(task_cpu(p)) == p;
1884}
1885
Ingo Molnardd41f592007-07-09 18:51:59 +02001886static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1887{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001888 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001889#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01001890 /*
1891 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1892 * successfuly executed on another CPU. We must ensure that updates of
1893 * per-task data have been completed by this moment.
1894 */
1895 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02001896 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02001897#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07001898}
1899
Steven Rostedtcb469842008-01-25 21:08:22 +01001900static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1901 const struct sched_class *prev_class,
1902 int oldprio, int running)
1903{
1904 if (prev_class != p->sched_class) {
1905 if (prev_class->switched_from)
1906 prev_class->switched_from(rq, p, running);
1907 p->sched_class->switched_to(rq, p, running);
1908 } else
1909 p->sched_class->prio_changed(rq, p, oldprio, running);
1910}
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02001913
Thomas Gleixnere958b362008-06-04 23:22:32 +02001914/* Used instead of source_load when we know the type == 0 */
1915static unsigned long weighted_cpuload(const int cpu)
1916{
1917 return cpu_rq(cpu)->load.weight;
1918}
1919
Ingo Molnarcc367732007-10-15 17:00:18 +02001920/*
1921 * Is this task likely cache-hot:
1922 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001923static int
Ingo Molnarcc367732007-10-15 17:00:18 +02001924task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
1925{
1926 s64 delta;
1927
Ingo Molnarf540a602008-03-15 17:10:34 +01001928 /*
1929 * Buddy candidates are cache hot:
1930 */
Ingo Molnard25ce4c2008-03-17 09:36:53 +01001931 if (sched_feat(CACHE_HOT_BUDDY) && (&p->se == cfs_rq_of(&p->se)->next))
Ingo Molnarf540a602008-03-15 17:10:34 +01001932 return 1;
1933
Ingo Molnarcc367732007-10-15 17:00:18 +02001934 if (p->sched_class != &fair_sched_class)
1935 return 0;
1936
Ingo Molnar6bc16652007-10-15 17:00:18 +02001937 if (sysctl_sched_migration_cost == -1)
1938 return 1;
1939 if (sysctl_sched_migration_cost == 0)
1940 return 0;
1941
Ingo Molnarcc367732007-10-15 17:00:18 +02001942 delta = now - p->se.exec_start;
1943
1944 return delta < (s64)sysctl_sched_migration_cost;
1945}
1946
1947
Ingo Molnardd41f592007-07-09 18:51:59 +02001948void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001949{
Ingo Molnardd41f592007-07-09 18:51:59 +02001950 int old_cpu = task_cpu(p);
1951 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001952 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
1953 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02001954 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001955
1956 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001957
1958#ifdef CONFIG_SCHEDSTATS
1959 if (p->se.wait_start)
1960 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001961 if (p->se.sleep_start)
1962 p->se.sleep_start -= clock_offset;
1963 if (p->se.block_start)
1964 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02001965 if (old_cpu != new_cpu) {
1966 schedstat_inc(p, se.nr_migrations);
1967 if (task_hot(p, old_rq->clock, NULL))
1968 schedstat_inc(p, se.nr_forced2_migrations);
1969 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001970#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001971 p->se.vruntime -= old_cfsrq->min_vruntime -
1972 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02001973
1974 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001975}
1976
Ingo Molnar70b97a72006-07-03 00:25:42 -07001977struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Ingo Molnar36c8b582006-07-03 00:25:41 -07001980 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 int dest_cpu;
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001984};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986/*
1987 * The task's runqueue lock must be held.
1988 * Returns true if you have to wait for migration thread.
1989 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001990static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07001991migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001993 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 /*
1996 * If the task is not on a runqueue (and not running), then
1997 * it is sufficient to simply update the task's cpu field.
1998 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001999 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 set_task_cpu(p, dest_cpu);
2001 return 0;
2002 }
2003
2004 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 req->task = p;
2006 req->dest_cpu = dest_cpu;
2007 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07002008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 return 1;
2010}
2011
2012/*
2013 * wait_task_inactive - wait for a thread to unschedule.
2014 *
2015 * The caller must ensure that the task *will* unschedule sometime soon,
2016 * else this function might spin for a *long* time. This function can't
2017 * be called with interrupts off, or it may introduce deadlock with
2018 * smp_call_function() if an IPI is sent by the same process we are
2019 * waiting to become inactive.
2020 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002021void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
2023 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002024 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002025 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Andi Kleen3a5c3592007-10-15 17:00:14 +02002027 for (;;) {
2028 /*
2029 * We do the initial early heuristics without holding
2030 * any task-queue locks at all. We'll only try to get
2031 * the runqueue lock when things look like they will
2032 * work out!
2033 */
2034 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002035
Andi Kleen3a5c3592007-10-15 17:00:14 +02002036 /*
2037 * If the task is actively running on another CPU
2038 * still, just relax and busy-wait without holding
2039 * any locks.
2040 *
2041 * NOTE! Since we don't hold any locks, it's not
2042 * even sure that "rq" stays as the right runqueue!
2043 * But we don't care, since "task_running()" will
2044 * return false if the runqueue has changed and p
2045 * is actually now running somewhere else!
2046 */
2047 while (task_running(rq, p))
2048 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002049
Andi Kleen3a5c3592007-10-15 17:00:14 +02002050 /*
2051 * Ok, time to look more closely! We need the rq
2052 * lock now, to be *sure*. If we're wrong, we'll
2053 * just go back and repeat.
2054 */
2055 rq = task_rq_lock(p, &flags);
2056 running = task_running(rq, p);
2057 on_rq = p->se.on_rq;
2058 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002059
Andi Kleen3a5c3592007-10-15 17:00:14 +02002060 /*
2061 * Was it really running after all now that we
2062 * checked with the proper locks actually held?
2063 *
2064 * Oops. Go back and try again..
2065 */
2066 if (unlikely(running)) {
2067 cpu_relax();
2068 continue;
2069 }
2070
2071 /*
2072 * It's not enough that it's not actively running,
2073 * it must be off the runqueue _entirely_, and not
2074 * preempted!
2075 *
2076 * So if it wa still runnable (but just not actively
2077 * running right now), it's preempted, and we should
2078 * yield - it could be a while.
2079 */
2080 if (unlikely(on_rq)) {
2081 schedule_timeout_uninterruptible(1);
2082 continue;
2083 }
2084
2085 /*
2086 * Ahh, all good. It wasn't running, and it wasn't
2087 * runnable, which means that it will never become
2088 * running in the future either. We're all done!
2089 */
2090 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093
2094/***
2095 * kick_process - kick a running thread to enter/exit the kernel
2096 * @p: the to-be-kicked thread
2097 *
2098 * Cause a process which is running on another CPU to enter
2099 * kernel-mode, without any delay. (to get signals handled.)
2100 *
2101 * NOTE: this function doesnt have to take the runqueue lock,
2102 * because all it wants to ensure is that the remote task enters
2103 * the kernel. If the IPI races and the task has been migrated
2104 * to another CPU then no harm is done and the purpose has been
2105 * achieved as well.
2106 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002107void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
2109 int cpu;
2110
2111 preempt_disable();
2112 cpu = task_cpu(p);
2113 if ((cpu != smp_processor_id()) && task_curr(p))
2114 smp_send_reschedule(cpu);
2115 preempt_enable();
2116}
2117
2118/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002119 * Return a low guess at the load of a migration-source cpu weighted
2120 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 *
2122 * We want to under-estimate the load of migration sources, to
2123 * balance conservatively.
2124 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002125static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002126{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002127 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002128 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002129
Peter Williams2dd73a42006-06-27 02:54:34 -07002130 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002131 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002132
Ingo Molnardd41f592007-07-09 18:51:59 +02002133 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
2136/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002137 * Return a high guess at the load of a migration-target cpu weighted
2138 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002140static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002141{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002142 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002143 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002144
Peter Williams2dd73a42006-06-27 02:54:34 -07002145 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002146 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002147
Ingo Molnardd41f592007-07-09 18:51:59 +02002148 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07002149}
2150
2151/*
2152 * Return the average load per task on the cpu's run queue
2153 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002154static unsigned long cpu_avg_load_per_task(int cpu)
Peter Williams2dd73a42006-06-27 02:54:34 -07002155{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002156 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002157 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07002158 unsigned long n = rq->nr_running;
2159
Ingo Molnardd41f592007-07-09 18:51:59 +02002160 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161}
2162
Nick Piggin147cbb42005-06-25 14:57:19 -07002163/*
2164 * find_idlest_group finds and returns the least busy CPU group within the
2165 * domain.
2166 */
2167static struct sched_group *
2168find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
2169{
2170 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
2171 unsigned long min_load = ULONG_MAX, this_load = 0;
2172 int load_idx = sd->forkexec_idx;
2173 int imbalance = 100 + (sd->imbalance_pct-100)/2;
2174
Peter Zijlstra4d8d5952008-06-27 13:41:19 +02002175 /*
2176 * now that we have both rqs locked the rq weight won't change
2177 * anymore - so update the stats.
2178 */
2179 update_aggregate(this_cpu, sd);
2180
Nick Piggin147cbb42005-06-25 14:57:19 -07002181 do {
2182 unsigned long load, avg_load;
2183 int local_group;
2184 int i;
2185
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002186 /* Skip over this group if it has no CPUs allowed */
2187 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02002188 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002189
Nick Piggin147cbb42005-06-25 14:57:19 -07002190 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07002191
2192 /* Tally up the load of all CPUs in the group */
2193 avg_load = 0;
2194
2195 for_each_cpu_mask(i, group->cpumask) {
2196 /* Bias balancing toward cpus of our domain */
2197 if (local_group)
2198 load = source_load(i, load_idx);
2199 else
2200 load = target_load(i, load_idx);
2201
2202 avg_load += load;
2203 }
2204
2205 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002206 avg_load = sg_div_cpu_power(group,
2207 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07002208
2209 if (local_group) {
2210 this_load = avg_load;
2211 this = group;
2212 } else if (avg_load < min_load) {
2213 min_load = avg_load;
2214 idlest = group;
2215 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02002216 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07002217
2218 if (!idlest || 100*this_load < imbalance*min_load)
2219 return NULL;
2220 return idlest;
2221}
2222
2223/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07002224 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07002225 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002226static int
Mike Travis7c16ec52008-04-04 18:11:11 -07002227find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu,
2228 cpumask_t *tmp)
Nick Piggin147cbb42005-06-25 14:57:19 -07002229{
2230 unsigned long load, min_load = ULONG_MAX;
2231 int idlest = -1;
2232 int i;
2233
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002234 /* Traverse only the allowed CPUs */
Mike Travis7c16ec52008-04-04 18:11:11 -07002235 cpus_and(*tmp, group->cpumask, p->cpus_allowed);
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002236
Mike Travis7c16ec52008-04-04 18:11:11 -07002237 for_each_cpu_mask(i, *tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002238 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07002239
2240 if (load < min_load || (load == min_load && i == this_cpu)) {
2241 min_load = load;
2242 idlest = i;
2243 }
2244 }
2245
2246 return idlest;
2247}
2248
Nick Piggin476d1392005-06-25 14:57:29 -07002249/*
2250 * sched_balance_self: balance the current task (running on cpu) in domains
2251 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2252 * SD_BALANCE_EXEC.
2253 *
2254 * Balance, ie. select the least loaded group.
2255 *
2256 * Returns the target CPU number, or the same CPU if no balancing is needed.
2257 *
2258 * preempt must be disabled.
2259 */
2260static int sched_balance_self(int cpu, int flag)
2261{
2262 struct task_struct *t = current;
2263 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07002264
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002265 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02002266 /*
2267 * If power savings logic is enabled for a domain, stop there.
2268 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002269 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2270 break;
Nick Piggin476d1392005-06-25 14:57:29 -07002271 if (tmp->flags & flag)
2272 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002273 }
Nick Piggin476d1392005-06-25 14:57:29 -07002274
2275 while (sd) {
Mike Travis7c16ec52008-04-04 18:11:11 -07002276 cpumask_t span, tmpmask;
Nick Piggin476d1392005-06-25 14:57:29 -07002277 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002278 int new_cpu, weight;
2279
2280 if (!(sd->flags & flag)) {
2281 sd = sd->child;
2282 continue;
2283 }
Nick Piggin476d1392005-06-25 14:57:29 -07002284
2285 span = sd->span;
2286 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002287 if (!group) {
2288 sd = sd->child;
2289 continue;
2290 }
Nick Piggin476d1392005-06-25 14:57:29 -07002291
Mike Travis7c16ec52008-04-04 18:11:11 -07002292 new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002293 if (new_cpu == -1 || new_cpu == cpu) {
2294 /* Now try balancing at a lower domain level of cpu */
2295 sd = sd->child;
2296 continue;
2297 }
Nick Piggin476d1392005-06-25 14:57:29 -07002298
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002299 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07002300 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07002301 sd = NULL;
2302 weight = cpus_weight(span);
2303 for_each_domain(cpu, tmp) {
2304 if (weight <= cpus_weight(tmp->span))
2305 break;
2306 if (tmp->flags & flag)
2307 sd = tmp;
2308 }
2309 /* while loop will break here if sd == NULL */
2310 }
2311
2312 return cpu;
2313}
2314
2315#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317/***
2318 * try_to_wake_up - wake up a thread
2319 * @p: the to-be-woken-up thread
2320 * @state: the mask of task states that can be woken
2321 * @sync: do a synchronous wakeup?
2322 *
2323 * Put it on the run-queue if it's not already there. The "current"
2324 * thread is always on the run-queue (except when the actual
2325 * re-schedule is in progress), and as such you're allowed to do
2326 * the simpler "current->state = TASK_RUNNING" to mark yourself
2327 * runnable without the overhead of this.
2328 *
2329 * returns failure only if the task is already active.
2330 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002331static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
Ingo Molnarcc367732007-10-15 17:00:18 +02002333 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 unsigned long flags;
2335 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002336 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Ingo Molnarb85d0662008-03-16 20:03:22 +01002338 if (!sched_feat(SYNC_WAKEUPS))
2339 sync = 0;
2340
Linus Torvalds04e2f172008-02-23 18:05:03 -08002341 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 rq = task_rq_lock(p, &flags);
2343 old_state = p->state;
2344 if (!(old_state & state))
2345 goto out;
2346
Ingo Molnardd41f592007-07-09 18:51:59 +02002347 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 goto out_running;
2349
2350 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002351 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 this_cpu = smp_processor_id();
2353
2354#ifdef CONFIG_SMP
2355 if (unlikely(task_running(rq, p)))
2356 goto out_activate;
2357
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01002358 cpu = p->sched_class->select_task_rq(p, sync);
2359 if (cpu != orig_cpu) {
2360 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 task_rq_unlock(rq, &flags);
2362 /* might preempt at this point */
2363 rq = task_rq_lock(p, &flags);
2364 old_state = p->state;
2365 if (!(old_state & state))
2366 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02002367 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 goto out_running;
2369
2370 this_cpu = smp_processor_id();
2371 cpu = task_cpu(p);
2372 }
2373
Gregory Haskinse7693a32008-01-25 21:08:09 +01002374#ifdef CONFIG_SCHEDSTATS
2375 schedstat_inc(rq, ttwu_count);
2376 if (cpu == this_cpu)
2377 schedstat_inc(rq, ttwu_local);
2378 else {
2379 struct sched_domain *sd;
2380 for_each_domain(this_cpu, sd) {
2381 if (cpu_isset(cpu, sd->span)) {
2382 schedstat_inc(sd, ttwu_wake_remote);
2383 break;
2384 }
2385 }
2386 }
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002387#endif /* CONFIG_SCHEDSTATS */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002388
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389out_activate:
2390#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02002391 schedstat_inc(p, se.nr_wakeups);
2392 if (sync)
2393 schedstat_inc(p, se.nr_wakeups_sync);
2394 if (orig_cpu != cpu)
2395 schedstat_inc(p, se.nr_wakeups_migrate);
2396 if (cpu == this_cpu)
2397 schedstat_inc(p, se.nr_wakeups_local);
2398 else
2399 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02002400 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02002401 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 success = 1;
2403
2404out_running:
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002405 check_preempt_curr(rq, p);
2406
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002408#ifdef CONFIG_SMP
2409 if (p->sched_class->task_wake_up)
2410 p->sched_class->task_wake_up(rq, p);
2411#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412out:
2413 task_rq_unlock(rq, &flags);
2414
2415 return success;
2416}
2417
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002418int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002420 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422EXPORT_SYMBOL(wake_up_process);
2423
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002424int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
2426 return try_to_wake_up(p, state, 0);
2427}
2428
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429/*
2430 * Perform scheduler related setup for a newly forked process p.
2431 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002432 *
2433 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002435static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
Ingo Molnardd41f592007-07-09 18:51:59 +02002437 p->se.exec_start = 0;
2438 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002439 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002440 p->se.last_wakeup = 0;
2441 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002442
2443#ifdef CONFIG_SCHEDSTATS
2444 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002445 p->se.sum_sleep_runtime = 0;
2446 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002447 p->se.block_start = 0;
2448 p->se.sleep_max = 0;
2449 p->se.block_max = 0;
2450 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002451 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002452 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002453#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002454
Peter Zijlstrafa717062008-01-25 21:08:27 +01002455 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002456 p->se.on_rq = 0;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02002457 INIT_LIST_HEAD(&p->se.group_node);
Nick Piggin476d1392005-06-25 14:57:29 -07002458
Avi Kivitye107be32007-07-26 13:40:43 +02002459#ifdef CONFIG_PREEMPT_NOTIFIERS
2460 INIT_HLIST_HEAD(&p->preempt_notifiers);
2461#endif
2462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 /*
2464 * We mark the process as running here, but have not actually
2465 * inserted it onto the runqueue yet. This guarantees that
2466 * nobody will actually run it, and a signal or other external
2467 * event cannot wake it up and insert it on the runqueue either.
2468 */
2469 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002470}
2471
2472/*
2473 * fork()/clone()-time setup:
2474 */
2475void sched_fork(struct task_struct *p, int clone_flags)
2476{
2477 int cpu = get_cpu();
2478
2479 __sched_fork(p);
2480
2481#ifdef CONFIG_SMP
2482 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2483#endif
Ingo Molnar02e4bac22007-10-15 17:00:11 +02002484 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002485
2486 /*
2487 * Make sure we do not leak PI boosting priority to the child:
2488 */
2489 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002490 if (!rt_prio(p->prio))
2491 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002492
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002493#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002494 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002495 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002497#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002498 p->oncpu = 0;
2499#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002501 /* Want to start with kernel preemption disabled. */
Al Viroa1261f542005-11-13 16:06:55 -08002502 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002504 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505}
2506
2507/*
2508 * wake_up_new_task - wake up a newly created task for the first time.
2509 *
2510 * This function will do some initial scheduler statistics housekeeping
2511 * that must be done for every newly created context, then puts the task
2512 * on the runqueue and wakes it.
2513 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002514void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515{
2516 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002517 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
2519 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002521 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523 p->prio = effective_prio(p);
2524
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002525 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002526 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002529 * Let the scheduling class do new task startup
2530 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002532 p->sched_class->task_new(rq, p);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02002533 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002535 check_preempt_curr(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002536#ifdef CONFIG_SMP
2537 if (p->sched_class->task_wake_up)
2538 p->sched_class->task_wake_up(rq, p);
2539#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002540 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541}
2542
Avi Kivitye107be32007-07-26 13:40:43 +02002543#ifdef CONFIG_PREEMPT_NOTIFIERS
2544
2545/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002546 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2547 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002548 */
2549void preempt_notifier_register(struct preempt_notifier *notifier)
2550{
2551 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2552}
2553EXPORT_SYMBOL_GPL(preempt_notifier_register);
2554
2555/**
2556 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002557 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002558 *
2559 * This is safe to call from within a preemption notifier.
2560 */
2561void preempt_notifier_unregister(struct preempt_notifier *notifier)
2562{
2563 hlist_del(&notifier->link);
2564}
2565EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2566
2567static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2568{
2569 struct preempt_notifier *notifier;
2570 struct hlist_node *node;
2571
2572 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2573 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2574}
2575
2576static void
2577fire_sched_out_preempt_notifiers(struct task_struct *curr,
2578 struct task_struct *next)
2579{
2580 struct preempt_notifier *notifier;
2581 struct hlist_node *node;
2582
2583 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2584 notifier->ops->sched_out(notifier, next);
2585}
2586
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002587#else /* !CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002588
2589static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2590{
2591}
2592
2593static void
2594fire_sched_out_preempt_notifiers(struct task_struct *curr,
2595 struct task_struct *next)
2596{
2597}
2598
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002599#endif /* CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002602 * prepare_task_switch - prepare to switch tasks
2603 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002604 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002605 * @next: the task we are going to switch to.
2606 *
2607 * This is called with the rq lock held and interrupts off. It must
2608 * be paired with a subsequent finish_task_switch after the context
2609 * switch.
2610 *
2611 * prepare_task_switch sets up locking and calls architecture specific
2612 * hooks.
2613 */
Avi Kivitye107be32007-07-26 13:40:43 +02002614static inline void
2615prepare_task_switch(struct rq *rq, struct task_struct *prev,
2616 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002617{
Avi Kivitye107be32007-07-26 13:40:43 +02002618 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002619 prepare_lock_switch(rq, next);
2620 prepare_arch_switch(next);
2621}
2622
2623/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002625 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 * @prev: the thread we just switched away from.
2627 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002628 * finish_task_switch must be called after the context switch, paired
2629 * with a prepare_task_switch call before the context switch.
2630 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2631 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 *
2633 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002634 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 * with the lock held can cause deadlocks; see schedule() for
2636 * details.)
2637 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002638static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 __releases(rq->lock)
2640{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002642 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
2644 rq->prev_mm = NULL;
2645
2646 /*
2647 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002648 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002649 * schedule one last time. The schedule call will never return, and
2650 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002651 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 * still held, otherwise prev could be scheduled on another cpu, die
2653 * there before we look at prev->state, and then the reference would
2654 * be dropped twice.
2655 * Manfred Spraul <manfred@colorfullife.com>
2656 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002657 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002658 finish_arch_switch(prev);
2659 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002660#ifdef CONFIG_SMP
2661 if (current->sched_class->post_schedule)
2662 current->sched_class->post_schedule(rq);
2663#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002664
Avi Kivitye107be32007-07-26 13:40:43 +02002665 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 if (mm)
2667 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002668 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002669 /*
2670 * Remove function-return probe instances associated with this
2671 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002672 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002673 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676}
2677
2678/**
2679 * schedule_tail - first thing a freshly forked thread must call.
2680 * @prev: the thread we just switched away from.
2681 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002682asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 __releases(rq->lock)
2684{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002685 struct rq *rq = this_rq();
2686
Nick Piggin4866cde2005-06-25 14:57:23 -07002687 finish_task_switch(rq, prev);
2688#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2689 /* In this case, finish_task_switch does not reenable preemption */
2690 preempt_enable();
2691#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002693 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694}
2695
2696/*
2697 * context_switch - switch to the new MM and the new
2698 * thread's register state.
2699 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002700static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002701context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002702 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703{
Ingo Molnardd41f592007-07-09 18:51:59 +02002704 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Avi Kivitye107be32007-07-26 13:40:43 +02002706 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002707 mm = next->mm;
2708 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002709 /*
2710 * For paravirt, this is coupled with an exit in switch_to to
2711 * combine the page table reload and the switch backend into
2712 * one hypercall.
2713 */
2714 arch_enter_lazy_cpu_mode();
2715
Ingo Molnardd41f592007-07-09 18:51:59 +02002716 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 next->active_mm = oldmm;
2718 atomic_inc(&oldmm->mm_count);
2719 enter_lazy_tlb(oldmm, next);
2720 } else
2721 switch_mm(oldmm, mm, next);
2722
Ingo Molnardd41f592007-07-09 18:51:59 +02002723 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 rq->prev_mm = oldmm;
2726 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002727 /*
2728 * Since the runqueue lock will be released by the next
2729 * task (which is an invalid locking op but in the case
2730 * of the scheduler it's an obvious special-case), so we
2731 * do an early lockdep release here:
2732 */
2733#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002734 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002735#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
2737 /* Here we just switch the register state and the stack. */
2738 switch_to(prev, next, prev);
2739
Ingo Molnardd41f592007-07-09 18:51:59 +02002740 barrier();
2741 /*
2742 * this_rq must be evaluated again because prev may have moved
2743 * CPUs since it called schedule(), thus the 'rq' on its stack
2744 * frame will be invalid.
2745 */
2746 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747}
2748
2749/*
2750 * nr_running, nr_uninterruptible and nr_context_switches:
2751 *
2752 * externally visible scheduler statistics: current number of runnable
2753 * threads, current number of uninterruptible-sleeping threads, total
2754 * number of context switches performed since bootup.
2755 */
2756unsigned long nr_running(void)
2757{
2758 unsigned long i, sum = 0;
2759
2760 for_each_online_cpu(i)
2761 sum += cpu_rq(i)->nr_running;
2762
2763 return sum;
2764}
2765
2766unsigned long nr_uninterruptible(void)
2767{
2768 unsigned long i, sum = 0;
2769
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002770 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 sum += cpu_rq(i)->nr_uninterruptible;
2772
2773 /*
2774 * Since we read the counters lockless, it might be slightly
2775 * inaccurate. Do not allow it to go below zero though:
2776 */
2777 if (unlikely((long)sum < 0))
2778 sum = 0;
2779
2780 return sum;
2781}
2782
2783unsigned long long nr_context_switches(void)
2784{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002785 int i;
2786 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002788 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 sum += cpu_rq(i)->nr_switches;
2790
2791 return sum;
2792}
2793
2794unsigned long nr_iowait(void)
2795{
2796 unsigned long i, sum = 0;
2797
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002798 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2800
2801 return sum;
2802}
2803
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002804unsigned long nr_active(void)
2805{
2806 unsigned long i, running = 0, uninterruptible = 0;
2807
2808 for_each_online_cpu(i) {
2809 running += cpu_rq(i)->nr_running;
2810 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2811 }
2812
2813 if (unlikely((long)uninterruptible < 0))
2814 uninterruptible = 0;
2815
2816 return running + uninterruptible;
2817}
2818
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002820 * Update rq->cpu_load[] statistics. This function is usually called every
2821 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002822 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002823static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002824{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002825 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002826 int i, scale;
2827
2828 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002829
2830 /* Update our load: */
2831 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2832 unsigned long old_load, new_load;
2833
2834 /* scale is effectively 1 << i now, and >> i divides by scale */
2835
2836 old_load = this_rq->cpu_load[i];
2837 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002838 /*
2839 * Round up the averaging division if load is increasing. This
2840 * prevents us from getting stuck on 9 if the load is 10, for
2841 * example.
2842 */
2843 if (new_load > old_load)
2844 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002845 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2846 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002847}
2848
Ingo Molnardd41f592007-07-09 18:51:59 +02002849#ifdef CONFIG_SMP
2850
Ingo Molnar48f24c42006-07-03 00:25:40 -07002851/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 * double_rq_lock - safely lock two runqueues
2853 *
2854 * Note this does not disable interrupts like task_rq_lock,
2855 * you need to do so manually before calling.
2856 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002857static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 __acquires(rq1->lock)
2859 __acquires(rq2->lock)
2860{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002861 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 if (rq1 == rq2) {
2863 spin_lock(&rq1->lock);
2864 __acquire(rq2->lock); /* Fake it out ;) */
2865 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002866 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 spin_lock(&rq1->lock);
2868 spin_lock(&rq2->lock);
2869 } else {
2870 spin_lock(&rq2->lock);
2871 spin_lock(&rq1->lock);
2872 }
2873 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002874 update_rq_clock(rq1);
2875 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876}
2877
2878/*
2879 * double_rq_unlock - safely unlock two runqueues
2880 *
2881 * Note this does not restore interrupts like task_rq_unlock,
2882 * you need to do so manually after calling.
2883 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002884static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 __releases(rq1->lock)
2886 __releases(rq2->lock)
2887{
2888 spin_unlock(&rq1->lock);
2889 if (rq1 != rq2)
2890 spin_unlock(&rq2->lock);
2891 else
2892 __release(rq2->lock);
2893}
2894
2895/*
2896 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2897 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01002898static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 __releases(this_rq->lock)
2900 __acquires(busiest->lock)
2901 __acquires(this_rq->lock)
2902{
Steven Rostedte8fa1362008-01-25 21:08:05 +01002903 int ret = 0;
2904
Kirill Korotaev054b9102006-12-10 02:20:11 -08002905 if (unlikely(!irqs_disabled())) {
2906 /* printk() doesn't work good under rq->lock */
2907 spin_unlock(&this_rq->lock);
2908 BUG_ON(1);
2909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002911 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 spin_unlock(&this_rq->lock);
2913 spin_lock(&busiest->lock);
2914 spin_lock(&this_rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01002915 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 } else
2917 spin_lock(&busiest->lock);
2918 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01002919 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920}
2921
2922/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 * If dest_cpu is allowed for this process, migrate the task to it.
2924 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002925 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 * the cpu_allowed mask is restored.
2927 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002928static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002930 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002932 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
2934 rq = task_rq_lock(p, &flags);
2935 if (!cpu_isset(dest_cpu, p->cpus_allowed)
2936 || unlikely(cpu_is_offline(dest_cpu)))
2937 goto out;
2938
2939 /* force the process onto the specified CPU */
2940 if (migrate_task(p, dest_cpu, &req)) {
2941 /* Need to wait for migration thread (might exit: take ref). */
2942 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07002943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 get_task_struct(mt);
2945 task_rq_unlock(rq, &flags);
2946 wake_up_process(mt);
2947 put_task_struct(mt);
2948 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07002949
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 return;
2951 }
2952out:
2953 task_rq_unlock(rq, &flags);
2954}
2955
2956/*
Nick Piggin476d1392005-06-25 14:57:29 -07002957 * sched_exec - execve() is a valuable balancing opportunity, because at
2958 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 */
2960void sched_exec(void)
2961{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002963 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002965 if (new_cpu != this_cpu)
2966 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967}
2968
2969/*
2970 * pull_task - move a task from a remote runqueue to the local runqueue.
2971 * Both runqueues must be locked.
2972 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002973static void pull_task(struct rq *src_rq, struct task_struct *p,
2974 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02002976 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002978 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 /*
2980 * Note that idle threads have a prio of MAX_PRIO, for this test
2981 * to be always true for them.
2982 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002983 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984}
2985
2986/*
2987 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
2988 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002989static
Ingo Molnar70b97a72006-07-03 00:25:42 -07002990int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002991 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002992 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993{
2994 /*
2995 * We do not migrate tasks that are:
2996 * 1) running (obviously), or
2997 * 2) cannot be migrated to this CPU due to cpus_allowed, or
2998 * 3) are cache-hot on their current CPU.
2999 */
Ingo Molnarcc367732007-10-15 17:00:18 +02003000 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
3001 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003003 }
Nick Piggin81026792005-06-25 14:57:07 -07003004 *all_pinned = 0;
3005
Ingo Molnarcc367732007-10-15 17:00:18 +02003006 if (task_running(rq, p)) {
3007 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07003008 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Ingo Molnarda84d962007-10-15 17:00:18 +02003011 /*
3012 * Aggressive migration if:
3013 * 1) task is cache cold, or
3014 * 2) too many balance attempts have failed.
3015 */
3016
Ingo Molnar6bc16652007-10-15 17:00:18 +02003017 if (!task_hot(p, rq->clock, sd) ||
3018 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003019#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02003020 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02003021 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02003022 schedstat_inc(p, se.nr_forced_migrations);
3023 }
Ingo Molnarda84d962007-10-15 17:00:18 +02003024#endif
3025 return 1;
3026 }
3027
Ingo Molnarcc367732007-10-15 17:00:18 +02003028 if (task_hot(p, rq->clock, sd)) {
3029 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02003030 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 return 1;
3033}
3034
Peter Williamse1d14842007-10-24 18:23:51 +02003035static unsigned long
3036balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3037 unsigned long max_load_move, struct sched_domain *sd,
3038 enum cpu_idle_type idle, int *all_pinned,
3039 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02003040{
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003041 int loops = 0, pulled = 0, pinned = 0, skip_for_load;
Ingo Molnardd41f592007-07-09 18:51:59 +02003042 struct task_struct *p;
3043 long rem_load_move = max_load_move;
3044
Peter Williamse1d14842007-10-24 18:23:51 +02003045 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02003046 goto out;
3047
3048 pinned = 1;
3049
3050 /*
3051 * Start the load-balancing iterator:
3052 */
3053 p = iterator->start(iterator->arg);
3054next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003055 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02003056 goto out;
3057 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003058 * To help distribute high priority tasks across CPUs we don't
Ingo Molnardd41f592007-07-09 18:51:59 +02003059 * skip a task if it will be the highest priority task (i.e. smallest
3060 * prio value) on its new queue regardless of its load weight
3061 */
3062 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
3063 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003064 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02003065 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003066 p = iterator->next(iterator->arg);
3067 goto next;
3068 }
3069
3070 pull_task(busiest, p, this_rq, this_cpu);
3071 pulled++;
3072 rem_load_move -= p->se.load.weight;
3073
3074 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003075 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003076 */
Peter Williamse1d14842007-10-24 18:23:51 +02003077 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003078 if (p->prio < *this_best_prio)
3079 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02003080 p = iterator->next(iterator->arg);
3081 goto next;
3082 }
3083out:
3084 /*
Peter Williamse1d14842007-10-24 18:23:51 +02003085 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02003086 * so we can safely collect pull_task() stats here rather than
3087 * inside pull_task().
3088 */
3089 schedstat_add(sd, lb_gained[idle], pulled);
3090
3091 if (all_pinned)
3092 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02003093
3094 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02003095}
Ingo Molnar48f24c42006-07-03 00:25:40 -07003096
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097/*
Peter Williams43010652007-08-09 11:16:46 +02003098 * move_tasks tries to move up to max_load_move weighted load from busiest to
3099 * this_rq, as part of a balancing operation within domain "sd".
3100 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 *
3102 * Called with both runqueues locked.
3103 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003104static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02003105 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003106 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07003107 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003109 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02003110 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003111 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Ingo Molnardd41f592007-07-09 18:51:59 +02003113 do {
Peter Williams43010652007-08-09 11:16:46 +02003114 total_load_moved +=
3115 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02003116 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003117 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02003118 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02003119 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
Peter Williams43010652007-08-09 11:16:46 +02003121 return total_load_moved > 0;
3122}
3123
Peter Williamse1d14842007-10-24 18:23:51 +02003124static int
3125iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3126 struct sched_domain *sd, enum cpu_idle_type idle,
3127 struct rq_iterator *iterator)
3128{
3129 struct task_struct *p = iterator->start(iterator->arg);
3130 int pinned = 0;
3131
3132 while (p) {
3133 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3134 pull_task(busiest, p, this_rq, this_cpu);
3135 /*
3136 * Right now, this is only the second place pull_task()
3137 * is called, so we can safely collect pull_task()
3138 * stats here rather than inside pull_task().
3139 */
3140 schedstat_inc(sd, lb_gained[idle]);
3141
3142 return 1;
3143 }
3144 p = iterator->next(iterator->arg);
3145 }
3146
3147 return 0;
3148}
3149
Peter Williams43010652007-08-09 11:16:46 +02003150/*
3151 * move_one_task tries to move exactly one task from busiest to this_rq, as
3152 * part of active balancing operations within "domain".
3153 * Returns 1 if successful and 0 otherwise.
3154 *
3155 * Called with both runqueues locked.
3156 */
3157static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3158 struct sched_domain *sd, enum cpu_idle_type idle)
3159{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003160 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02003161
3162 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02003163 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02003164 return 1;
3165
3166 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167}
3168
3169/*
3170 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07003171 * domain. It calculates and returns the amount of weighted load which
3172 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 */
3174static struct sched_group *
3175find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02003176 unsigned long *imbalance, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003177 int *sd_idle, const cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178{
3179 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
3180 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003181 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07003182 unsigned long busiest_load_per_task, busiest_nr_running;
3183 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003184 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003185#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3186 int power_savings_balance = 1;
3187 unsigned long leader_nr_running = 0, min_load_per_task = 0;
3188 unsigned long min_nr_running = ULONG_MAX;
3189 struct sched_group *group_min = NULL, *group_leader = NULL;
3190#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
3192 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003193 busiest_load_per_task = busiest_nr_running = 0;
3194 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003195 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003196 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003197 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003198 load_idx = sd->newidle_idx;
3199 else
3200 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
3202 do {
Ken Chen908a7c12007-10-17 16:55:11 +02003203 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 int local_group;
3205 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02003206 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003207 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003208 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
3210 local_group = cpu_isset(this_cpu, group->cpumask);
3211
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003212 if (local_group)
3213 balance_cpu = first_cpu(group->cpumask);
3214
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07003216 sum_weighted_load = sum_nr_running = avg_load = 0;
Ken Chen908a7c12007-10-17 16:55:11 +02003217 max_cpu_load = 0;
3218 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
3220 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003221 struct rq *rq;
3222
3223 if (!cpu_isset(i, *cpus))
3224 continue;
3225
3226 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07003227
Suresh Siddha9439aab2007-07-19 21:28:35 +02003228 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07003229 *sd_idle = 0;
3230
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003232 if (local_group) {
3233 if (idle_cpu(i) && !first_idle_cpu) {
3234 first_idle_cpu = 1;
3235 balance_cpu = i;
3236 }
3237
Nick Piggina2000572006-02-10 01:51:02 -08003238 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003239 } else {
Nick Piggina2000572006-02-10 01:51:02 -08003240 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003241 if (load > max_cpu_load)
3242 max_cpu_load = load;
3243 if (min_cpu_load > load)
3244 min_cpu_load = load;
3245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
3247 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07003248 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003249 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 }
3251
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003252 /*
3253 * First idle cpu or the first cpu(busiest) in this sched group
3254 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02003255 * domains. In the newly idle case, we will allow all the cpu's
3256 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003257 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02003258 if (idle != CPU_NEWLY_IDLE && local_group &&
3259 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003260 *balance = 0;
3261 goto ret;
3262 }
3263
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07003265 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
3267 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07003268 avg_load = sg_div_cpu_power(group,
3269 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
Ken Chen908a7c12007-10-17 16:55:11 +02003271 if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
3272 __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 &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003322 first_cpu(group->cpumask) <
3323 first_cpu(group_min->cpumask))) {
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 &&
3338 first_cpu(group->cpumask) >
3339 first_cpu(group_leader->cpumask))) {
3340 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
3412 this_load_per_task = SCHED_LOAD_SCALE;
3413
Ingo Molnardd41f592007-07-09 18:51:59 +02003414 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
3415 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;
3465 return group_min;
3466 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003467#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003468ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 *imbalance = 0;
3470 return NULL;
3471}
3472
3473/*
3474 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3475 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003476static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003477find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003478 unsigned long imbalance, const cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003480 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003481 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 int i;
3483
3484 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003485 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003486
3487 if (!cpu_isset(i, *cpus))
3488 continue;
3489
Ingo Molnar48f24c42006-07-03 00:25:40 -07003490 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003491 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
Ingo Molnardd41f592007-07-09 18:51:59 +02003493 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003494 continue;
3495
Ingo Molnardd41f592007-07-09 18:51:59 +02003496 if (wl > max_load) {
3497 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003498 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 }
3500 }
3501
3502 return busiest;
3503}
3504
3505/*
Nick Piggin77391d72005-06-25 14:57:30 -07003506 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3507 * so long as it is large enough.
3508 */
3509#define MAX_PINNED_INTERVAL 512
3510
3511/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3513 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003515static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003516 struct sched_domain *sd, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003517 int *balance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518{
Peter Williams43010652007-08-09 11:16:46 +02003519 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003522 struct rq *busiest;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003523 unsigned long flags;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02003524 int unlock_aggregate;
Nick Piggin5969fe02005-09-10 00:26:19 -07003525
Mike Travis7c16ec52008-04-04 18:11:11 -07003526 cpus_setall(*cpus);
3527
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02003528 unlock_aggregate = get_aggregate(this_cpu, sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02003529
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003530 /*
3531 * When power savings policy is enabled for the parent domain, idle
3532 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003533 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003534 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003535 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003536 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003537 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003538 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
Ingo Molnar2d723762007-10-15 17:00:12 +02003540 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003542redo:
3543 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003544 cpus, balance);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003545
Chen, Kenneth W06066712006-12-10 02:20:35 -08003546 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003547 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003548
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 if (!group) {
3550 schedstat_inc(sd, lb_nobusyg[idle]);
3551 goto out_balanced;
3552 }
3553
Mike Travis7c16ec52008-04-04 18:11:11 -07003554 busiest = find_busiest_queue(group, idle, imbalance, cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 if (!busiest) {
3556 schedstat_inc(sd, lb_nobusyq[idle]);
3557 goto out_balanced;
3558 }
3559
Nick Piggindb935db2005-06-25 14:57:11 -07003560 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
3562 schedstat_add(sd, lb_imbalance[idle], imbalance);
3563
Peter Williams43010652007-08-09 11:16:46 +02003564 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 if (busiest->nr_running > 1) {
3566 /*
3567 * Attempt to move tasks. If find_busiest_group has found
3568 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003569 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 * correctly treated as an imbalance.
3571 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003572 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003573 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003574 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003575 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003576 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003577 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003578
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003579 /*
3580 * some other cpu did the load balance for us.
3581 */
Peter Williams43010652007-08-09 11:16:46 +02003582 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003583 resched_cpu(this_cpu);
3584
Nick Piggin81026792005-06-25 14:57:07 -07003585 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003586 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003587 cpu_clear(cpu_of(busiest), *cpus);
3588 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003589 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003590 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 }
Nick Piggin81026792005-06-25 14:57:07 -07003593
Peter Williams43010652007-08-09 11:16:46 +02003594 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 schedstat_inc(sd, lb_failed[idle]);
3596 sd->nr_balance_failed++;
3597
3598 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003600 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003601
3602 /* don't kick the migration_thread, if the curr
3603 * task on busiest cpu can't be moved to this_cpu
3604 */
3605 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003606 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003607 all_pinned = 1;
3608 goto out_one_pinned;
3609 }
3610
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 if (!busiest->active_balance) {
3612 busiest->active_balance = 1;
3613 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003614 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003616 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003617 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 wake_up_process(busiest->migration_thread);
3619
3620 /*
3621 * We've kicked active balancing, reset the failure
3622 * counter.
3623 */
Nick Piggin39507452005-06-25 14:57:09 -07003624 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 }
Nick Piggin81026792005-06-25 14:57:07 -07003626 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 sd->nr_balance_failed = 0;
3628
Nick Piggin81026792005-06-25 14:57:07 -07003629 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 /* We were unbalanced, so reset the balancing interval */
3631 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003632 } else {
3633 /*
3634 * If we've begun active balancing, start to back off. This
3635 * case may not be covered by the all_pinned logic if there
3636 * is only 1 task on the busy runqueue (because we don't call
3637 * move_tasks).
3638 */
3639 if (sd->balance_interval < sd->max_interval)
3640 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 }
3642
Peter Williams43010652007-08-09 11:16:46 +02003643 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003644 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstrac09595f2008-06-27 13:41:14 +02003645 ld_moved = -1;
3646
3647 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
3649out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 schedstat_inc(sd, lb_balanced[idle]);
3651
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003652 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003653
3654out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003656 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3657 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 sd->balance_interval *= 2;
3659
Ingo Molnar48f24c42006-07-03 00:25:40 -07003660 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003661 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstrac09595f2008-06-27 13:41:14 +02003662 ld_moved = -1;
3663 else
3664 ld_moved = 0;
3665out:
3666 if (unlock_aggregate)
Peter Zijlstrab6a86c72008-06-27 13:41:18 +02003667 put_aggregate(this_cpu, sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02003668 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669}
3670
3671/*
3672 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3673 * tasks if there is an imbalance.
3674 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003675 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 * this_rq is locked.
3677 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003678static int
Mike Travis7c16ec52008-04-04 18:11:11 -07003679load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
3680 cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681{
3682 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003683 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003685 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003686 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003687 int all_pinned = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07003688
3689 cpus_setall(*cpus);
Nick Piggin5969fe02005-09-10 00:26:19 -07003690
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003691 /*
3692 * When power savings policy is enabled for the parent domain, idle
3693 * sibling can pick up load irrespective of busy siblings. In this case,
3694 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003695 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003696 */
3697 if (sd->flags & SD_SHARE_CPUPOWER &&
3698 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003699 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
Ingo Molnar2d723762007-10-15 17:00:12 +02003701 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003702redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003703 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Mike Travis7c16ec52008-04-04 18:11:11 -07003704 &sd_idle, cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003706 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003707 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 }
3709
Mike Travis7c16ec52008-04-04 18:11:11 -07003710 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003711 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003712 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003713 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 }
3715
Nick Piggindb935db2005-06-25 14:57:11 -07003716 BUG_ON(busiest == this_rq);
3717
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003718 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003719
Peter Williams43010652007-08-09 11:16:46 +02003720 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003721 if (busiest->nr_running > 1) {
3722 /* Attempt to move tasks */
3723 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003724 /* this_rq->clock is already updated */
3725 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003726 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003727 imbalance, sd, CPU_NEWLY_IDLE,
3728 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003729 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003730
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003731 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003732 cpu_clear(cpu_of(busiest), *cpus);
3733 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003734 goto redo;
3735 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003736 }
3737
Peter Williams43010652007-08-09 11:16:46 +02003738 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003739 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003740 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3741 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003742 return -1;
3743 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003744 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745
Peter Williams43010652007-08-09 11:16:46 +02003746 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003747
3748out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003749 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003750 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003751 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003752 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003753 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003754
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003755 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756}
3757
3758/*
3759 * idle_balance is called by schedule() if this_cpu is about to become
3760 * idle. Attempts to pull tasks from other CPUs.
3761 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003762static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763{
3764 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003765 int pulled_task = -1;
3766 unsigned long next_balance = jiffies + HZ;
Mike Travis7c16ec52008-04-04 18:11:11 -07003767 cpumask_t tmpmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
3769 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003770 unsigned long interval;
3771
3772 if (!(sd->flags & SD_LOAD_BALANCE))
3773 continue;
3774
3775 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003776 /* If we've pulled tasks over stop searching: */
Mike Travis7c16ec52008-04-04 18:11:11 -07003777 pulled_task = load_balance_newidle(this_cpu, this_rq,
3778 sd, &tmpmask);
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003779
3780 interval = msecs_to_jiffies(sd->balance_interval);
3781 if (time_after(next_balance, sd->last_balance + interval))
3782 next_balance = sd->last_balance + interval;
3783 if (pulled_task)
3784 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003786 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003787 /*
3788 * We are going idle. next_balance may be set based on
3789 * a busy processor. So reset next_balance.
3790 */
3791 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793}
3794
3795/*
3796 * active_load_balance is run by migration threads. It pushes running tasks
3797 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3798 * running on each physical CPU where possible, and avoids physical /
3799 * logical imbalances.
3800 *
3801 * Called with busiest_rq locked.
3802 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003803static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804{
Nick Piggin39507452005-06-25 14:57:09 -07003805 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003806 struct sched_domain *sd;
3807 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003808
Ingo Molnar48f24c42006-07-03 00:25:40 -07003809 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003810 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003811 return;
3812
3813 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
3815 /*
Nick Piggin39507452005-06-25 14:57:09 -07003816 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003817 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003818 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 */
Nick Piggin39507452005-06-25 14:57:09 -07003820 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
Nick Piggin39507452005-06-25 14:57:09 -07003822 /* move a task from busiest_rq to target_rq */
3823 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003824 update_rq_clock(busiest_rq);
3825 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
Nick Piggin39507452005-06-25 14:57:09 -07003827 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003828 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07003829 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003830 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07003831 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833
Ingo Molnar48f24c42006-07-03 00:25:40 -07003834 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003835 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836
Peter Williams43010652007-08-09 11:16:46 +02003837 if (move_one_task(target_rq, target_cpu, busiest_rq,
3838 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07003839 schedstat_inc(sd, alb_pushed);
3840 else
3841 schedstat_inc(sd, alb_failed);
3842 }
Nick Piggin39507452005-06-25 14:57:09 -07003843 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844}
3845
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003846#ifdef CONFIG_NO_HZ
3847static struct {
3848 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003849 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003850} nohz ____cacheline_aligned = {
3851 .load_balancer = ATOMIC_INIT(-1),
3852 .cpu_mask = CPU_MASK_NONE,
3853};
3854
Christoph Lameter7835b982006-12-10 02:20:22 -08003855/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003856 * This routine will try to nominate the ilb (idle load balancing)
3857 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3858 * load balancing on behalf of all those cpus. If all the cpus in the system
3859 * go into this tickless mode, then there will be no ilb owner (as there is
3860 * no need for one) and all the cpus will sleep till the next wakeup event
3861 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08003862 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003863 * For the ilb owner, tick is not stopped. And this tick will be used
3864 * for idle load balancing. ilb owner will still be part of
3865 * nohz.cpu_mask..
3866 *
3867 * While stopping the tick, this cpu will become the ilb owner if there
3868 * is no other owner. And will be the owner till that cpu becomes busy
3869 * or if all cpus in the system stop their ticks at which point
3870 * there is no need for ilb owner.
3871 *
3872 * When the ilb owner becomes busy, it nominates another owner, during the
3873 * next busy scheduler_tick()
3874 */
3875int select_nohz_load_balancer(int stop_tick)
3876{
3877 int cpu = smp_processor_id();
3878
3879 if (stop_tick) {
3880 cpu_set(cpu, nohz.cpu_mask);
3881 cpu_rq(cpu)->in_nohz_recently = 1;
3882
3883 /*
3884 * If we are going offline and still the leader, give up!
3885 */
3886 if (cpu_is_offline(cpu) &&
3887 atomic_read(&nohz.load_balancer) == cpu) {
3888 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3889 BUG();
3890 return 0;
3891 }
3892
3893 /* time for ilb owner also to sleep */
3894 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3895 if (atomic_read(&nohz.load_balancer) == cpu)
3896 atomic_set(&nohz.load_balancer, -1);
3897 return 0;
3898 }
3899
3900 if (atomic_read(&nohz.load_balancer) == -1) {
3901 /* make me the ilb owner */
3902 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
3903 return 1;
3904 } else if (atomic_read(&nohz.load_balancer) == cpu)
3905 return 1;
3906 } else {
3907 if (!cpu_isset(cpu, nohz.cpu_mask))
3908 return 0;
3909
3910 cpu_clear(cpu, nohz.cpu_mask);
3911
3912 if (atomic_read(&nohz.load_balancer) == cpu)
3913 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3914 BUG();
3915 }
3916 return 0;
3917}
3918#endif
3919
3920static DEFINE_SPINLOCK(balancing);
3921
3922/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003923 * It checks each scheduling domain to see if it is due to be balanced,
3924 * and initiates a balancing operation if so.
3925 *
3926 * Balancing parameters are set up in arch_init_sched_domains.
3927 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003928static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08003929{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003930 int balance = 1;
3931 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08003932 unsigned long interval;
3933 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003934 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08003935 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003936 int update_next_balance = 0;
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02003937 int need_serialize;
Mike Travis7c16ec52008-04-04 18:11:11 -07003938 cpumask_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003940 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 if (!(sd->flags & SD_LOAD_BALANCE))
3942 continue;
3943
3944 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003945 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 interval *= sd->busy_factor;
3947
3948 /* scale ms to jiffies */
3949 interval = msecs_to_jiffies(interval);
3950 if (unlikely(!interval))
3951 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003952 if (interval > HZ*NR_CPUS/10)
3953 interval = HZ*NR_CPUS/10;
3954
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02003955 need_serialize = sd->flags & SD_SERIALIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02003957 if (need_serialize) {
Christoph Lameter08c183f2006-12-10 02:20:29 -08003958 if (!spin_trylock(&balancing))
3959 goto out;
3960 }
3961
Christoph Lameterc9819f42006-12-10 02:20:25 -08003962 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003963 if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003964 /*
3965 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07003966 * longer idle, or one of our SMT siblings is
3967 * not idle.
3968 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003969 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003971 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 }
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02003973 if (need_serialize)
Christoph Lameter08c183f2006-12-10 02:20:29 -08003974 spin_unlock(&balancing);
3975out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02003976 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08003977 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003978 update_next_balance = 1;
3979 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003980
3981 /*
3982 * Stop the load balance at this level. There is another
3983 * CPU in our sched group which is doing load balancing more
3984 * actively.
3985 */
3986 if (!balance)
3987 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02003989
3990 /*
3991 * next_balance will be updated only when there is a need.
3992 * When the cpu is attached to null domain for ex, it will not be
3993 * updated.
3994 */
3995 if (likely(update_next_balance))
3996 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003997}
3998
3999/*
4000 * run_rebalance_domains is triggered when needed from the scheduler tick.
4001 * In CONFIG_NO_HZ case, the idle load balance owner will do the
4002 * rebalancing for all the cpus for whom scheduler ticks are stopped.
4003 */
4004static void run_rebalance_domains(struct softirq_action *h)
4005{
Ingo Molnardd41f592007-07-09 18:51:59 +02004006 int this_cpu = smp_processor_id();
4007 struct rq *this_rq = cpu_rq(this_cpu);
4008 enum cpu_idle_type idle = this_rq->idle_at_tick ?
4009 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004010
Ingo Molnardd41f592007-07-09 18:51:59 +02004011 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004012
4013#ifdef CONFIG_NO_HZ
4014 /*
4015 * If this cpu is the owner for idle load balancing, then do the
4016 * balancing on behalf of the other idle cpus whose ticks are
4017 * stopped.
4018 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004019 if (this_rq->idle_at_tick &&
4020 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004021 cpumask_t cpus = nohz.cpu_mask;
4022 struct rq *rq;
4023 int balance_cpu;
4024
Ingo Molnardd41f592007-07-09 18:51:59 +02004025 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004026 for_each_cpu_mask(balance_cpu, cpus) {
4027 /*
4028 * If this cpu gets work to do, stop the load balancing
4029 * work being done for other cpus. Next load
4030 * balancing owner will pick it up.
4031 */
4032 if (need_resched())
4033 break;
4034
Oleg Nesterovde0cf892007-08-12 18:08:19 +02004035 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004036
4037 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004038 if (time_after(this_rq->next_balance, rq->next_balance))
4039 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004040 }
4041 }
4042#endif
4043}
4044
4045/*
4046 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
4047 *
4048 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
4049 * idle load balancing owner or decide to stop the periodic load balancing,
4050 * if the whole system is idle.
4051 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004052static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004053{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004054#ifdef CONFIG_NO_HZ
4055 /*
4056 * If we were in the nohz mode recently and busy at the current
4057 * scheduler tick, then check if we need to nominate new idle
4058 * load balancer.
4059 */
4060 if (rq->in_nohz_recently && !rq->idle_at_tick) {
4061 rq->in_nohz_recently = 0;
4062
4063 if (atomic_read(&nohz.load_balancer) == cpu) {
4064 cpu_clear(cpu, nohz.cpu_mask);
4065 atomic_set(&nohz.load_balancer, -1);
4066 }
4067
4068 if (atomic_read(&nohz.load_balancer) == -1) {
4069 /*
4070 * simple selection for now: Nominate the
4071 * first cpu in the nohz list to be the next
4072 * ilb owner.
4073 *
4074 * TBD: Traverse the sched domains and nominate
4075 * the nearest cpu in the nohz.cpu_mask.
4076 */
4077 int ilb = first_cpu(nohz.cpu_mask);
4078
Mike Travis434d53b2008-04-04 18:11:04 -07004079 if (ilb < nr_cpu_ids)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004080 resched_cpu(ilb);
4081 }
4082 }
4083
4084 /*
4085 * If this cpu is idle and doing idle load balancing for all the
4086 * cpus with ticks stopped, is it time for that to stop?
4087 */
4088 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
4089 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
4090 resched_cpu(cpu);
4091 return;
4092 }
4093
4094 /*
4095 * If this cpu is idle and the idle load balancing is done by
4096 * someone else, then no need raise the SCHED_SOFTIRQ
4097 */
4098 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
4099 cpu_isset(cpu, nohz.cpu_mask))
4100 return;
4101#endif
4102 if (time_after_eq(jiffies, rq->next_balance))
4103 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104}
Ingo Molnardd41f592007-07-09 18:51:59 +02004105
4106#else /* CONFIG_SMP */
4107
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108/*
4109 * on UP we do not need to balance between CPUs:
4110 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004111static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112{
4113}
Ingo Molnardd41f592007-07-09 18:51:59 +02004114
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115#endif
4116
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117DEFINE_PER_CPU(struct kernel_stat, kstat);
4118
4119EXPORT_PER_CPU_SYMBOL(kstat);
4120
4121/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02004122 * Return p->sum_exec_runtime plus any more ns on the sched_clock
4123 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02004125unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004128 u64 ns, delta_exec;
4129 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004130
Ingo Molnar41b86e92007-07-09 18:51:58 +02004131 rq = task_rq_lock(p, &flags);
4132 ns = p->se.sum_exec_runtime;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004133 if (task_current(rq, p)) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02004134 update_rq_clock(rq);
4135 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004136 if ((s64)delta_exec > 0)
4137 ns += delta_exec;
4138 }
4139 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004140
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 return ns;
4142}
4143
4144/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 * Account user cpu time to a process.
4146 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 * @cputime: the cpu time spent in user space since the last update
4148 */
4149void account_user_time(struct task_struct *p, cputime_t cputime)
4150{
4151 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4152 cputime64_t tmp;
4153
4154 p->utime = cputime_add(p->utime, cputime);
4155
4156 /* Add user time to cpustat. */
4157 tmp = cputime_to_cputime64(cputime);
4158 if (TASK_NICE(p) > 0)
4159 cpustat->nice = cputime64_add(cpustat->nice, tmp);
4160 else
4161 cpustat->user = cputime64_add(cpustat->user, tmp);
4162}
4163
4164/*
Laurent Vivier94886b82007-10-15 17:00:19 +02004165 * Account guest cpu time to a process.
4166 * @p: the process that the cpu time gets accounted to
4167 * @cputime: the cpu time spent in virtual machine since the last update
4168 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01004169static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02004170{
4171 cputime64_t tmp;
4172 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4173
4174 tmp = cputime_to_cputime64(cputime);
4175
4176 p->utime = cputime_add(p->utime, cputime);
4177 p->gtime = cputime_add(p->gtime, cputime);
4178
4179 cpustat->user = cputime64_add(cpustat->user, tmp);
4180 cpustat->guest = cputime64_add(cpustat->guest, tmp);
4181}
4182
4183/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004184 * Account scaled user cpu time to a process.
4185 * @p: the process that the cpu time gets accounted to
4186 * @cputime: the cpu time spent in user space since the last update
4187 */
4188void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
4189{
4190 p->utimescaled = cputime_add(p->utimescaled, cputime);
4191}
4192
4193/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 * Account system cpu time to a process.
4195 * @p: the process that the cpu time gets accounted to
4196 * @hardirq_offset: the offset to subtract from hardirq_count()
4197 * @cputime: the cpu time spent in kernel space since the last update
4198 */
4199void account_system_time(struct task_struct *p, int hardirq_offset,
4200 cputime_t cputime)
4201{
4202 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004203 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 cputime64_t tmp;
4205
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004206 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
4207 account_guest_time(p, cputime);
4208 return;
4209 }
Laurent Vivier94886b82007-10-15 17:00:19 +02004210
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 p->stime = cputime_add(p->stime, cputime);
4212
4213 /* Add system time to cpustat. */
4214 tmp = cputime_to_cputime64(cputime);
4215 if (hardirq_count() - hardirq_offset)
4216 cpustat->irq = cputime64_add(cpustat->irq, tmp);
4217 else if (softirq_count())
4218 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004219 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004221 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4223 else
4224 cpustat->idle = cputime64_add(cpustat->idle, tmp);
4225 /* Account for system time used */
4226 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227}
4228
4229/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004230 * Account scaled system cpu time to a process.
4231 * @p: the process that the cpu time gets accounted to
4232 * @hardirq_offset: the offset to subtract from hardirq_count()
4233 * @cputime: the cpu time spent in kernel space since the last update
4234 */
4235void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
4236{
4237 p->stimescaled = cputime_add(p->stimescaled, cputime);
4238}
4239
4240/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 * Account for involuntary wait time.
4242 * @p: the process from which the cpu time has been stolen
4243 * @steal: the cpu time spent in involuntary wait
4244 */
4245void account_steal_time(struct task_struct *p, cputime_t steal)
4246{
4247 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4248 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07004249 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
4251 if (p == rq->idle) {
4252 p->stime = cputime_add(p->stime, steal);
4253 if (atomic_read(&rq->nr_iowait) > 0)
4254 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4255 else
4256 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004257 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 cpustat->steal = cputime64_add(cpustat->steal, tmp);
4259}
4260
Christoph Lameter7835b982006-12-10 02:20:22 -08004261/*
4262 * This function gets called by the timer code, with HZ frequency.
4263 * We call it with interrupts disabled.
4264 *
4265 * It also gets called by the fork code, when changing the parent's
4266 * timeslices.
4267 */
4268void scheduler_tick(void)
4269{
Christoph Lameter7835b982006-12-10 02:20:22 -08004270 int cpu = smp_processor_id();
4271 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004272 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004273
4274 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08004275
Ingo Molnardd41f592007-07-09 18:51:59 +02004276 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004277 update_rq_clock(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02004278 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01004279 curr->sched_class->task_tick(rq, curr, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02004280 spin_unlock(&rq->lock);
4281
Christoph Lametere418e1c2006-12-10 02:20:23 -08004282#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02004283 rq->idle_at_tick = idle_cpu(cpu);
4284 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08004285#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286}
4287
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
4289
Srinivasa Ds43627582008-02-23 15:24:04 -08004290void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291{
4292 /*
4293 * Underflow?
4294 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004295 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4296 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 preempt_count() += val;
4298 /*
4299 * Spinlock count overflowing soon?
4300 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08004301 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
4302 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303}
4304EXPORT_SYMBOL(add_preempt_count);
4305
Srinivasa Ds43627582008-02-23 15:24:04 -08004306void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307{
4308 /*
4309 * Underflow?
4310 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004311 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
4312 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 /*
4314 * Is the spinlock portion underflowing?
4315 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004316 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
4317 !(preempt_count() & PREEMPT_MASK)))
4318 return;
4319
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 preempt_count() -= val;
4321}
4322EXPORT_SYMBOL(sub_preempt_count);
4323
4324#endif
4325
4326/*
Ingo Molnardd41f592007-07-09 18:51:59 +02004327 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004329static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330{
Satyam Sharma838225b2007-10-24 18:23:50 +02004331 struct pt_regs *regs = get_irq_regs();
4332
4333 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4334 prev->comm, prev->pid, preempt_count());
4335
Ingo Molnardd41f592007-07-09 18:51:59 +02004336 debug_show_held_locks(prev);
Arjan van de Vene21f5b12008-05-23 09:05:58 -07004337 print_modules();
Ingo Molnardd41f592007-07-09 18:51:59 +02004338 if (irqs_disabled())
4339 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02004340
4341 if (regs)
4342 show_regs(regs);
4343 else
4344 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02004345}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
Ingo Molnardd41f592007-07-09 18:51:59 +02004347/*
4348 * Various schedule()-time debugging checks and statistics:
4349 */
4350static inline void schedule_debug(struct task_struct *prev)
4351{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004353 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 * schedule() atomically, we ignore that path for now.
4355 * Otherwise, whine if we are scheduling when we should not be.
4356 */
Roel Kluin3f33a7c2008-05-13 23:44:11 +02004357 if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
Ingo Molnardd41f592007-07-09 18:51:59 +02004358 __schedule_bug(prev);
4359
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4361
Ingo Molnar2d723762007-10-15 17:00:12 +02004362 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004363#ifdef CONFIG_SCHEDSTATS
4364 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004365 schedstat_inc(this_rq(), bkl_count);
4366 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004367 }
4368#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004369}
4370
4371/*
4372 * Pick up the highest-prio task:
4373 */
4374static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004375pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02004376{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004377 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004378 struct task_struct *p;
4379
4380 /*
4381 * Optimization: we know that if all tasks are in
4382 * the fair class we can call that function directly:
4383 */
4384 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004385 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004386 if (likely(p))
4387 return p;
4388 }
4389
4390 class = sched_class_highest;
4391 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004392 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004393 if (p)
4394 return p;
4395 /*
4396 * Will never be NULL as the idle class always
4397 * returns a non-NULL p:
4398 */
4399 class = class->next;
4400 }
4401}
4402
4403/*
4404 * schedule() is the main scheduler function.
4405 */
4406asmlinkage void __sched schedule(void)
4407{
4408 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004409 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02004410 struct rq *rq;
Mike Galbraithf333fdc2008-05-12 21:20:55 +02004411 int cpu, hrtick = sched_feat(HRTICK);
Ingo Molnardd41f592007-07-09 18:51:59 +02004412
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413need_resched:
4414 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02004415 cpu = smp_processor_id();
4416 rq = cpu_rq(cpu);
4417 rcu_qsctr_inc(cpu);
4418 prev = rq->curr;
4419 switch_count = &prev->nivcsw;
4420
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 release_kernel_lock(prev);
4422need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
Ingo Molnardd41f592007-07-09 18:51:59 +02004424 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425
Mike Galbraithf333fdc2008-05-12 21:20:55 +02004426 if (hrtick)
4427 hrtick_clear(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004428
Ingo Molnar1e819952007-10-15 17:00:13 +02004429 /*
4430 * Do the rq-clock update outside the rq lock:
4431 */
4432 local_irq_disable();
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004433 update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02004434 spin_lock(&rq->lock);
4435 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
Ingo Molnardd41f592007-07-09 18:51:59 +02004437 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
Oleg Nesterov16882c12008-06-08 21:20:41 +04004438 if (unlikely(signal_pending_state(prev->state, prev)))
Ingo Molnardd41f592007-07-09 18:51:59 +02004439 prev->state = TASK_RUNNING;
Oleg Nesterov16882c12008-06-08 21:20:41 +04004440 else
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004441 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02004442 switch_count = &prev->nvcsw;
4443 }
4444
Steven Rostedt9a897c52008-01-25 21:08:22 +01004445#ifdef CONFIG_SMP
4446 if (prev->sched_class->pre_schedule)
4447 prev->sched_class->pre_schedule(rq, prev);
4448#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01004449
Ingo Molnardd41f592007-07-09 18:51:59 +02004450 if (unlikely(!rq->nr_running))
4451 idle_balance(cpu, rq);
4452
Ingo Molnar31ee5292007-08-09 11:16:49 +02004453 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004454 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 if (likely(prev != next)) {
David Simner673a90a2008-04-29 10:08:59 +01004457 sched_info_switch(prev, next);
4458
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 rq->nr_switches++;
4460 rq->curr = next;
4461 ++*switch_count;
4462
Ingo Molnardd41f592007-07-09 18:51:59 +02004463 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004464 /*
4465 * the context switch might have flipped the stack from under
4466 * us, hence refresh the local variables.
4467 */
4468 cpu = smp_processor_id();
4469 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 } else
4471 spin_unlock_irq(&rq->lock);
4472
Mike Galbraithf333fdc2008-05-12 21:20:55 +02004473 if (hrtick)
4474 hrtick_set(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004475
4476 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004478
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 preempt_enable_no_resched();
4480 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4481 goto need_resched;
4482}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483EXPORT_SYMBOL(schedule);
4484
4485#ifdef CONFIG_PREEMPT
4486/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004487 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004488 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 * occur there and call schedule directly.
4490 */
4491asmlinkage void __sched preempt_schedule(void)
4492{
4493 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004494
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 /*
4496 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004497 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004499 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 return;
4501
Andi Kleen3a5c3592007-10-15 17:00:14 +02004502 do {
4503 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004504 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004505 sub_preempt_count(PREEMPT_ACTIVE);
4506
4507 /*
4508 * Check again in case we missed a preemption opportunity
4509 * between schedule and now.
4510 */
4511 barrier();
4512 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514EXPORT_SYMBOL(preempt_schedule);
4515
4516/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004517 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 * off of irq context.
4519 * Note, that this is called and return with irqs disabled. This will
4520 * protect us against recursive calling from irq.
4521 */
4522asmlinkage void __sched preempt_schedule_irq(void)
4523{
4524 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004525
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004526 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 BUG_ON(ti->preempt_count || !irqs_disabled());
4528
Andi Kleen3a5c3592007-10-15 17:00:14 +02004529 do {
4530 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004531 local_irq_enable();
4532 schedule();
4533 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004534 sub_preempt_count(PREEMPT_ACTIVE);
4535
4536 /*
4537 * Check again in case we missed a preemption opportunity
4538 * between schedule and now.
4539 */
4540 barrier();
4541 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542}
4543
4544#endif /* CONFIG_PREEMPT */
4545
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004546int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4547 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004549 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551EXPORT_SYMBOL(default_wake_function);
4552
4553/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004554 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4555 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 * number) then we wake all the non-exclusive tasks and one exclusive task.
4557 *
4558 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004559 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4561 */
4562static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4563 int nr_exclusive, int sync, void *key)
4564{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004565 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004567 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004568 unsigned flags = curr->flags;
4569
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004571 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 break;
4573 }
4574}
4575
4576/**
4577 * __wake_up - wake up threads blocked on a waitqueue.
4578 * @q: the waitqueue
4579 * @mode: which threads
4580 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004581 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004583void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004584 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585{
4586 unsigned long flags;
4587
4588 spin_lock_irqsave(&q->lock, flags);
4589 __wake_up_common(q, mode, nr_exclusive, 0, key);
4590 spin_unlock_irqrestore(&q->lock, flags);
4591}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592EXPORT_SYMBOL(__wake_up);
4593
4594/*
4595 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4596 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004597void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598{
4599 __wake_up_common(q, mode, 1, 0, NULL);
4600}
4601
4602/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004603 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 * @q: the waitqueue
4605 * @mode: which threads
4606 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4607 *
4608 * The sync wakeup differs that the waker knows that it will schedule
4609 * away soon, so while the target thread will be woken up, it will not
4610 * be migrated to another CPU - ie. the two threads are 'synchronized'
4611 * with each other. This can prevent needless bouncing between CPUs.
4612 *
4613 * On UP it can prevent extra preemption.
4614 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004615void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004616__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617{
4618 unsigned long flags;
4619 int sync = 1;
4620
4621 if (unlikely(!q))
4622 return;
4623
4624 if (unlikely(!nr_exclusive))
4625 sync = 0;
4626
4627 spin_lock_irqsave(&q->lock, flags);
4628 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4629 spin_unlock_irqrestore(&q->lock, flags);
4630}
4631EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4632
Ingo Molnarb15136e2007-10-24 18:23:48 +02004633void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634{
4635 unsigned long flags;
4636
4637 spin_lock_irqsave(&x->wait.lock, flags);
4638 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004639 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 spin_unlock_irqrestore(&x->wait.lock, flags);
4641}
4642EXPORT_SYMBOL(complete);
4643
Ingo Molnarb15136e2007-10-24 18:23:48 +02004644void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645{
4646 unsigned long flags;
4647
4648 spin_lock_irqsave(&x->wait.lock, flags);
4649 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004650 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 spin_unlock_irqrestore(&x->wait.lock, flags);
4652}
4653EXPORT_SYMBOL(complete_all);
4654
Andi Kleen8cbbe862007-10-15 17:00:14 +02004655static inline long __sched
4656do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 if (!x->done) {
4659 DECLARE_WAITQUEUE(wait, current);
4660
4661 wait.flags |= WQ_FLAG_EXCLUSIVE;
4662 __add_wait_queue_tail(&x->wait, &wait);
4663 do {
Matthew Wilcox009e5772007-12-06 12:29:54 -05004664 if ((state == TASK_INTERRUPTIBLE &&
4665 signal_pending(current)) ||
4666 (state == TASK_KILLABLE &&
4667 fatal_signal_pending(current))) {
Oleg Nesterovea71a542008-06-20 18:32:20 +04004668 timeout = -ERESTARTSYS;
4669 break;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004670 }
4671 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004673 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 spin_lock_irq(&x->wait.lock);
Oleg Nesterovea71a542008-06-20 18:32:20 +04004675 } while (!x->done && timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 __remove_wait_queue(&x->wait, &wait);
Oleg Nesterovea71a542008-06-20 18:32:20 +04004677 if (!x->done)
4678 return timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 }
4680 x->done--;
Oleg Nesterovea71a542008-06-20 18:32:20 +04004681 return timeout ?: 1;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004682}
4683
4684static long __sched
4685wait_for_common(struct completion *x, long timeout, int state)
4686{
4687 might_sleep();
4688
4689 spin_lock_irq(&x->wait.lock);
4690 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004692 return timeout;
4693}
4694
Ingo Molnarb15136e2007-10-24 18:23:48 +02004695void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004696{
4697 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698}
4699EXPORT_SYMBOL(wait_for_completion);
4700
Ingo Molnarb15136e2007-10-24 18:23:48 +02004701unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4703{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004704 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705}
4706EXPORT_SYMBOL(wait_for_completion_timeout);
4707
Andi Kleen8cbbe862007-10-15 17:00:14 +02004708int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709{
Andi Kleen51e97992007-10-18 21:32:55 +02004710 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4711 if (t == -ERESTARTSYS)
4712 return t;
4713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714}
4715EXPORT_SYMBOL(wait_for_completion_interruptible);
4716
Ingo Molnarb15136e2007-10-24 18:23:48 +02004717unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718wait_for_completion_interruptible_timeout(struct completion *x,
4719 unsigned long timeout)
4720{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004721 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722}
4723EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4724
Matthew Wilcox009e5772007-12-06 12:29:54 -05004725int __sched wait_for_completion_killable(struct completion *x)
4726{
4727 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4728 if (t == -ERESTARTSYS)
4729 return t;
4730 return 0;
4731}
4732EXPORT_SYMBOL(wait_for_completion_killable);
4733
Andi Kleen8cbbe862007-10-15 17:00:14 +02004734static long __sched
4735sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004736{
4737 unsigned long flags;
4738 wait_queue_t wait;
4739
4740 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741
Andi Kleen8cbbe862007-10-15 17:00:14 +02004742 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743
Andi Kleen8cbbe862007-10-15 17:00:14 +02004744 spin_lock_irqsave(&q->lock, flags);
4745 __add_wait_queue(q, &wait);
4746 spin_unlock(&q->lock);
4747 timeout = schedule_timeout(timeout);
4748 spin_lock_irq(&q->lock);
4749 __remove_wait_queue(q, &wait);
4750 spin_unlock_irqrestore(&q->lock, flags);
4751
4752 return timeout;
4753}
4754
4755void __sched interruptible_sleep_on(wait_queue_head_t *q)
4756{
4757 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759EXPORT_SYMBOL(interruptible_sleep_on);
4760
Ingo Molnar0fec1712007-07-09 18:52:01 +02004761long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004762interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004764 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4767
Ingo Molnar0fec1712007-07-09 18:52:01 +02004768void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004770 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772EXPORT_SYMBOL(sleep_on);
4773
Ingo Molnar0fec1712007-07-09 18:52:01 +02004774long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004776 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778EXPORT_SYMBOL(sleep_on_timeout);
4779
Ingo Molnarb29739f2006-06-27 02:54:51 -07004780#ifdef CONFIG_RT_MUTEXES
4781
4782/*
4783 * rt_mutex_setprio - set the current priority of a task
4784 * @p: task
4785 * @prio: prio value (kernel-internal form)
4786 *
4787 * This function changes the 'effective' priority of a task. It does
4788 * not touch ->normal_prio like __setscheduler().
4789 *
4790 * Used by the rt_mutex code to implement priority inheritance logic.
4791 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004792void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004793{
4794 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004795 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004796 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01004797 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004798
4799 BUG_ON(prio < 0 || prio > MAX_PRIO);
4800
4801 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004802 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004803
Andrew Mortond5f9f942007-05-08 20:27:06 -07004804 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004805 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004806 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004807 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004808 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004809 if (running)
4810 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004811
4812 if (rt_prio(prio))
4813 p->sched_class = &rt_sched_class;
4814 else
4815 p->sched_class = &fair_sched_class;
4816
Ingo Molnarb29739f2006-06-27 02:54:51 -07004817 p->prio = prio;
4818
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004819 if (running)
4820 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004821 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004822 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004823
4824 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004825 }
4826 task_rq_unlock(rq, &flags);
4827}
4828
4829#endif
4830
Ingo Molnar36c8b582006-07-03 00:25:41 -07004831void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832{
Ingo Molnardd41f592007-07-09 18:51:59 +02004833 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004835 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
4837 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
4838 return;
4839 /*
4840 * We have to be careful, if called from sys_setpriority(),
4841 * the task might be in the middle of scheduling on another CPU.
4842 */
4843 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004844 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 /*
4846 * The RT priorities are set via sched_setscheduler(), but we still
4847 * allow the 'normal' nice value to be set - but as expected
4848 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02004849 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 */
Ingo Molnare05606d2007-07-09 18:51:59 +02004851 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 p->static_prio = NICE_TO_PRIO(nice);
4853 goto out_unlock;
4854 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004855 on_rq = p->se.on_rq;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02004856 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004857 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07004860 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004861 old_prio = p->prio;
4862 p->prio = effective_prio(p);
4863 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864
Ingo Molnardd41f592007-07-09 18:51:59 +02004865 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004866 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07004868 * If the task increased its priority or is running and
4869 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004871 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 resched_task(rq->curr);
4873 }
4874out_unlock:
4875 task_rq_unlock(rq, &flags);
4876}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877EXPORT_SYMBOL(set_user_nice);
4878
Matt Mackalle43379f2005-05-01 08:59:00 -07004879/*
4880 * can_nice - check if a task can reduce its nice value
4881 * @p: task
4882 * @nice: nice value
4883 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004884int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07004885{
Matt Mackall024f4742005-08-18 11:24:19 -07004886 /* convert nice value [19,-20] to rlimit style value [1,40] */
4887 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004888
Matt Mackalle43379f2005-05-01 08:59:00 -07004889 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
4890 capable(CAP_SYS_NICE));
4891}
4892
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893#ifdef __ARCH_WANT_SYS_NICE
4894
4895/*
4896 * sys_nice - change the priority of the current process.
4897 * @increment: priority increment
4898 *
4899 * sys_setpriority is a more generic, but much slower function that
4900 * does similar things.
4901 */
4902asmlinkage long sys_nice(int increment)
4903{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004904 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905
4906 /*
4907 * Setpriority might change our priority at the same moment.
4908 * We don't have to worry. Conceptually one call occurs first
4909 * and we have a single winner.
4910 */
Matt Mackalle43379f2005-05-01 08:59:00 -07004911 if (increment < -40)
4912 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 if (increment > 40)
4914 increment = 40;
4915
4916 nice = PRIO_TO_NICE(current->static_prio) + increment;
4917 if (nice < -20)
4918 nice = -20;
4919 if (nice > 19)
4920 nice = 19;
4921
Matt Mackalle43379f2005-05-01 08:59:00 -07004922 if (increment < 0 && !can_nice(current, nice))
4923 return -EPERM;
4924
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 retval = security_task_setnice(current, nice);
4926 if (retval)
4927 return retval;
4928
4929 set_user_nice(current, nice);
4930 return 0;
4931}
4932
4933#endif
4934
4935/**
4936 * task_prio - return the priority value of a given task.
4937 * @p: the task in question.
4938 *
4939 * This is the priority value as seen by users in /proc.
4940 * RT tasks are offset by -200. Normal tasks are centered
4941 * around 0, value goes from -16 to +15.
4942 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004943int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944{
4945 return p->prio - MAX_RT_PRIO;
4946}
4947
4948/**
4949 * task_nice - return the nice value of a given task.
4950 * @p: the task in question.
4951 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004952int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953{
4954 return TASK_NICE(p);
4955}
Pavel Roskin150d8be2008-03-05 16:56:37 -05004956EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
4958/**
4959 * idle_cpu - is a given cpu idle currently?
4960 * @cpu: the processor in question.
4961 */
4962int idle_cpu(int cpu)
4963{
4964 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
4965}
4966
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967/**
4968 * idle_task - return the idle task for a given cpu.
4969 * @cpu: the processor in question.
4970 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004971struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972{
4973 return cpu_rq(cpu)->idle;
4974}
4975
4976/**
4977 * find_process_by_pid - find a process with a matching PID value.
4978 * @pid: the pid in question.
4979 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004980static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07004982 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983}
4984
4985/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02004986static void
4987__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988{
Ingo Molnardd41f592007-07-09 18:51:59 +02004989 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004990
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02004992 switch (p->policy) {
4993 case SCHED_NORMAL:
4994 case SCHED_BATCH:
4995 case SCHED_IDLE:
4996 p->sched_class = &fair_sched_class;
4997 break;
4998 case SCHED_FIFO:
4999 case SCHED_RR:
5000 p->sched_class = &rt_sched_class;
5001 break;
5002 }
5003
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005005 p->normal_prio = normal_prio(p);
5006 /* we are holding p->pi_lock already */
5007 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07005008 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009}
5010
5011/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005012 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 * @p: the task in question.
5014 * @policy: new policy.
5015 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005016 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005017 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005019int sched_setscheduler(struct task_struct *p, int policy,
5020 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005022 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01005024 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005025 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
Steven Rostedt66e53932006-06-27 02:54:44 -07005027 /* may grab non-irq protected spin_locks */
5028 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029recheck:
5030 /* double check policy once rq lock held */
5031 if (policy < 0)
5032 policy = oldpolicy = p->policy;
5033 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02005034 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
5035 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08005036 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 /*
5038 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02005039 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5040 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 */
5042 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005043 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04005044 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02005046 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 return -EINVAL;
5048
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005049 /*
5050 * Allow unprivileged RT tasks to decrease priority:
5051 */
5052 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02005053 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005054 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005055
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005056 if (!lock_task_sighand(p, &flags))
5057 return -ESRCH;
5058 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
5059 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005060
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005061 /* can't set/change the rt policy */
5062 if (policy != p->policy && !rlim_rtprio)
5063 return -EPERM;
5064
5065 /* can't increase priority */
5066 if (param->sched_priority > p->rt_priority &&
5067 param->sched_priority > rlim_rtprio)
5068 return -EPERM;
5069 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005070 /*
5071 * Like positive nice levels, dont allow tasks to
5072 * move out of SCHED_IDLE either:
5073 */
5074 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
5075 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005076
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005077 /* can't change other user's priorities */
5078 if ((current->euid != p->euid) &&
5079 (current->euid != p->uid))
5080 return -EPERM;
5081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005083#ifdef CONFIG_RT_GROUP_SCHED
5084 /*
5085 * Do not allow realtime tasks into groups that have no runtime
5086 * assigned.
5087 */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02005088 if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005089 return -EPERM;
5090#endif
5091
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 retval = security_task_setscheduler(p, policy, param);
5093 if (retval)
5094 return retval;
5095 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07005096 * make sure no PI-waiters arrive (or leave) while we are
5097 * changing the priority of the task:
5098 */
5099 spin_lock_irqsave(&p->pi_lock, flags);
5100 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 * To be able to change p->policy safely, the apropriate
5102 * runqueue lock must be held.
5103 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07005104 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 /* recheck policy now with rq lock held */
5106 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
5107 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005108 __task_rq_unlock(rq);
5109 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 goto recheck;
5111 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02005112 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005113 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005114 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005115 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005116 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005117 if (running)
5118 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005119
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005121 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005122
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005123 if (running)
5124 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005125 if (on_rq) {
5126 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005127
5128 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07005130 __task_rq_unlock(rq);
5131 spin_unlock_irqrestore(&p->pi_lock, flags);
5132
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07005133 rt_mutex_adjust_pi(p);
5134
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 return 0;
5136}
5137EXPORT_SYMBOL_GPL(sched_setscheduler);
5138
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005139static int
5140do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 struct sched_param lparam;
5143 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005144 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145
5146 if (!param || pid < 0)
5147 return -EINVAL;
5148 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
5149 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005150
5151 rcu_read_lock();
5152 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005154 if (p != NULL)
5155 retval = sched_setscheduler(p, policy, &lparam);
5156 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07005157
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 return retval;
5159}
5160
5161/**
5162 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5163 * @pid: the pid in question.
5164 * @policy: new policy.
5165 * @param: structure containing the new RT priority.
5166 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005167asmlinkage long
5168sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169{
Jason Baronc21761f2006-01-18 17:43:03 -08005170 /* negative values for policy are not valid */
5171 if (policy < 0)
5172 return -EINVAL;
5173
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 return do_sched_setscheduler(pid, policy, param);
5175}
5176
5177/**
5178 * sys_sched_setparam - set/change the RT priority of a thread
5179 * @pid: the pid in question.
5180 * @param: structure containing the new RT priority.
5181 */
5182asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
5183{
5184 return do_sched_setscheduler(pid, -1, param);
5185}
5186
5187/**
5188 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5189 * @pid: the pid in question.
5190 */
5191asmlinkage long sys_sched_getscheduler(pid_t pid)
5192{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005193 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005194 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195
5196 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005197 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198
5199 retval = -ESRCH;
5200 read_lock(&tasklist_lock);
5201 p = find_process_by_pid(pid);
5202 if (p) {
5203 retval = security_task_getscheduler(p);
5204 if (!retval)
5205 retval = p->policy;
5206 }
5207 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 return retval;
5209}
5210
5211/**
5212 * sys_sched_getscheduler - get the RT priority of a thread
5213 * @pid: the pid in question.
5214 * @param: structure containing the RT priority.
5215 */
5216asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
5217{
5218 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005219 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005220 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221
5222 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005223 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224
5225 read_lock(&tasklist_lock);
5226 p = find_process_by_pid(pid);
5227 retval = -ESRCH;
5228 if (!p)
5229 goto out_unlock;
5230
5231 retval = security_task_getscheduler(p);
5232 if (retval)
5233 goto out_unlock;
5234
5235 lp.sched_priority = p->rt_priority;
5236 read_unlock(&tasklist_lock);
5237
5238 /*
5239 * This one might sleep, we cannot do it with a spinlock held ...
5240 */
5241 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
5242
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 return retval;
5244
5245out_unlock:
5246 read_unlock(&tasklist_lock);
5247 return retval;
5248}
5249
Mike Travisb53e9212008-04-04 18:11:08 -07005250long sched_setaffinity(pid_t pid, const cpumask_t *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 cpumask_t cpus_allowed;
Mike Travisb53e9212008-04-04 18:11:08 -07005253 cpumask_t new_mask = *in_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005254 struct task_struct *p;
5255 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005257 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 read_lock(&tasklist_lock);
5259
5260 p = find_process_by_pid(pid);
5261 if (!p) {
5262 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005263 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 return -ESRCH;
5265 }
5266
5267 /*
5268 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005269 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 * usage count and then drop tasklist_lock.
5271 */
5272 get_task_struct(p);
5273 read_unlock(&tasklist_lock);
5274
5275 retval = -EPERM;
5276 if ((current->euid != p->euid) && (current->euid != p->uid) &&
5277 !capable(CAP_SYS_NICE))
5278 goto out_unlock;
5279
David Quigleye7834f82006-06-23 02:03:59 -07005280 retval = security_task_setscheduler(p, 0, NULL);
5281 if (retval)
5282 goto out_unlock;
5283
Mike Travisf9a86fc2008-04-04 18:11:07 -07005284 cpuset_cpus_allowed(p, &cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005286 again:
Mike Travis7c16ec52008-04-04 18:11:11 -07005287 retval = set_cpus_allowed_ptr(p, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288
Paul Menage8707d8b2007-10-18 23:40:22 -07005289 if (!retval) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07005290 cpuset_cpus_allowed(p, &cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005291 if (!cpus_subset(new_mask, cpus_allowed)) {
5292 /*
5293 * We must have raced with a concurrent cpuset
5294 * update. Just reset the cpus_allowed to the
5295 * cpuset's cpus_allowed
5296 */
5297 new_mask = cpus_allowed;
5298 goto again;
5299 }
5300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301out_unlock:
5302 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005303 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 return retval;
5305}
5306
5307static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
5308 cpumask_t *new_mask)
5309{
5310 if (len < sizeof(cpumask_t)) {
5311 memset(new_mask, 0, sizeof(cpumask_t));
5312 } else if (len > sizeof(cpumask_t)) {
5313 len = sizeof(cpumask_t);
5314 }
5315 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5316}
5317
5318/**
5319 * sys_sched_setaffinity - set the cpu affinity of a process
5320 * @pid: pid of the process
5321 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5322 * @user_mask_ptr: user-space pointer to the new cpu mask
5323 */
5324asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
5325 unsigned long __user *user_mask_ptr)
5326{
5327 cpumask_t new_mask;
5328 int retval;
5329
5330 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
5331 if (retval)
5332 return retval;
5333
Mike Travisb53e9212008-04-04 18:11:08 -07005334 return sched_setaffinity(pid, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335}
5336
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337long sched_getaffinity(pid_t pid, cpumask_t *mask)
5338{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005339 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005342 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 read_lock(&tasklist_lock);
5344
5345 retval = -ESRCH;
5346 p = find_process_by_pid(pid);
5347 if (!p)
5348 goto out_unlock;
5349
David Quigleye7834f82006-06-23 02:03:59 -07005350 retval = security_task_getscheduler(p);
5351 if (retval)
5352 goto out_unlock;
5353
Jack Steiner2f7016d2006-02-01 03:05:18 -08005354 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355
5356out_unlock:
5357 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005358 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359
Ulrich Drepper9531b622007-08-09 11:16:46 +02005360 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361}
5362
5363/**
5364 * sys_sched_getaffinity - get the cpu affinity of a process
5365 * @pid: pid of the process
5366 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5367 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5368 */
5369asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
5370 unsigned long __user *user_mask_ptr)
5371{
5372 int ret;
5373 cpumask_t mask;
5374
5375 if (len < sizeof(cpumask_t))
5376 return -EINVAL;
5377
5378 ret = sched_getaffinity(pid, &mask);
5379 if (ret < 0)
5380 return ret;
5381
5382 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
5383 return -EFAULT;
5384
5385 return sizeof(cpumask_t);
5386}
5387
5388/**
5389 * sys_sched_yield - yield the current processor to other threads.
5390 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005391 * This function yields the current CPU to other tasks. If there are no
5392 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 */
5394asmlinkage long sys_sched_yield(void)
5395{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005396 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397
Ingo Molnar2d723762007-10-15 17:00:12 +02005398 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005399 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400
5401 /*
5402 * Since we are going to call schedule() anyway, there's
5403 * no need to preempt or enable interrupts:
5404 */
5405 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005406 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 _raw_spin_unlock(&rq->lock);
5408 preempt_enable_no_resched();
5409
5410 schedule();
5411
5412 return 0;
5413}
5414
Andrew Mortone7b38402006-06-30 01:56:00 -07005415static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07005417#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5418 __might_sleep(__FILE__, __LINE__);
5419#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005420 /*
5421 * The BKS might be reacquired before we have dropped
5422 * PREEMPT_ACTIVE, which could trigger a second
5423 * cond_resched() call.
5424 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 do {
5426 add_preempt_count(PREEMPT_ACTIVE);
5427 schedule();
5428 sub_preempt_count(PREEMPT_ACTIVE);
5429 } while (need_resched());
5430}
5431
Herbert Xu02b67cc32008-01-25 21:08:28 +01005432int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433{
Ingo Molnar94142322006-12-29 16:48:13 -08005434 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5435 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 __cond_resched();
5437 return 1;
5438 }
5439 return 0;
5440}
Herbert Xu02b67cc32008-01-25 21:08:28 +01005441EXPORT_SYMBOL(_cond_resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442
5443/*
5444 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5445 * call schedule, and on return reacquire the lock.
5446 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005447 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 * operations here to prevent schedule() from being called twice (once via
5449 * spin_unlock(), once by hand).
5450 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005451int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452{
Nick Piggin95c354f2008-01-30 13:31:20 +01005453 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005454 int ret = 0;
5455
Nick Piggin95c354f2008-01-30 13:31:20 +01005456 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005458 if (resched && need_resched())
5459 __cond_resched();
5460 else
5461 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005462 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005465 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467EXPORT_SYMBOL(cond_resched_lock);
5468
5469int __sched cond_resched_softirq(void)
5470{
5471 BUG_ON(!in_softirq());
5472
Ingo Molnar94142322006-12-29 16:48:13 -08005473 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07005474 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 __cond_resched();
5476 local_bh_disable();
5477 return 1;
5478 }
5479 return 0;
5480}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481EXPORT_SYMBOL(cond_resched_softirq);
5482
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483/**
5484 * yield - yield the current processor to other threads.
5485 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005486 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 * thread runnable and calls sys_sched_yield().
5488 */
5489void __sched yield(void)
5490{
5491 set_current_state(TASK_RUNNING);
5492 sys_sched_yield();
5493}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494EXPORT_SYMBOL(yield);
5495
5496/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005497 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 * that process accounting knows that this is a task in IO wait state.
5499 *
5500 * But don't do that if it is a deliberate, throttling IO wait (this task
5501 * has set its backing_dev_info: the queue against which it should throttle)
5502 */
5503void __sched io_schedule(void)
5504{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005505 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005507 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 atomic_inc(&rq->nr_iowait);
5509 schedule();
5510 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005511 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513EXPORT_SYMBOL(io_schedule);
5514
5515long __sched io_schedule_timeout(long timeout)
5516{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005517 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 long ret;
5519
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005520 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 atomic_inc(&rq->nr_iowait);
5522 ret = schedule_timeout(timeout);
5523 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005524 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 return ret;
5526}
5527
5528/**
5529 * sys_sched_get_priority_max - return maximum RT priority.
5530 * @policy: scheduling class.
5531 *
5532 * this syscall returns the maximum rt_priority that can be used
5533 * by a given scheduling class.
5534 */
5535asmlinkage long sys_sched_get_priority_max(int policy)
5536{
5537 int ret = -EINVAL;
5538
5539 switch (policy) {
5540 case SCHED_FIFO:
5541 case SCHED_RR:
5542 ret = MAX_USER_RT_PRIO-1;
5543 break;
5544 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005545 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005546 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 ret = 0;
5548 break;
5549 }
5550 return ret;
5551}
5552
5553/**
5554 * sys_sched_get_priority_min - return minimum RT priority.
5555 * @policy: scheduling class.
5556 *
5557 * this syscall returns the minimum rt_priority that can be used
5558 * by a given scheduling class.
5559 */
5560asmlinkage long sys_sched_get_priority_min(int policy)
5561{
5562 int ret = -EINVAL;
5563
5564 switch (policy) {
5565 case SCHED_FIFO:
5566 case SCHED_RR:
5567 ret = 1;
5568 break;
5569 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005570 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005571 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 ret = 0;
5573 }
5574 return ret;
5575}
5576
5577/**
5578 * sys_sched_rr_get_interval - return the default timeslice of a process.
5579 * @pid: pid of the process.
5580 * @interval: userspace pointer to the timeslice value.
5581 *
5582 * this syscall writes the default timeslice value of a given process
5583 * into the user-space timespec buffer. A value of '0' means infinity.
5584 */
5585asmlinkage
5586long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5587{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005588 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005589 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005590 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
5593 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005594 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595
5596 retval = -ESRCH;
5597 read_lock(&tasklist_lock);
5598 p = find_process_by_pid(pid);
5599 if (!p)
5600 goto out_unlock;
5601
5602 retval = security_task_getscheduler(p);
5603 if (retval)
5604 goto out_unlock;
5605
Ingo Molnar77034932007-12-04 17:04:39 +01005606 /*
5607 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5608 * tasks that are on an otherwise idle runqueue:
5609 */
5610 time_slice = 0;
5611 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005612 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005613 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005614 struct sched_entity *se = &p->se;
5615 unsigned long flags;
5616 struct rq *rq;
5617
5618 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005619 if (rq->cfs.load.weight)
5620 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005621 task_rq_unlock(rq, &flags);
5622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005624 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005627
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628out_unlock:
5629 read_unlock(&tasklist_lock);
5630 return retval;
5631}
5632
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005633static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07005634
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005635void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005638 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005641 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005642 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005643#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005645 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005647 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648#else
5649 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005650 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005652 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653#endif
5654#ifdef CONFIG_DEBUG_STACK_USAGE
5655 {
Al Viro10ebffd2005-11-13 16:06:56 -08005656 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 while (!*n)
5658 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005659 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 }
5661#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005662 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005663 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005665 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666}
5667
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005668void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005670 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671
Ingo Molnar4bd77322007-07-11 21:21:47 +02005672#if BITS_PER_LONG == 32
5673 printk(KERN_INFO
5674 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005676 printk(KERN_INFO
5677 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678#endif
5679 read_lock(&tasklist_lock);
5680 do_each_thread(g, p) {
5681 /*
5682 * reset the NMI-timeout, listing all files on a slow
5683 * console might take alot of time:
5684 */
5685 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005686 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005687 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 } while_each_thread(g, p);
5689
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005690 touch_all_softlockup_watchdogs();
5691
Ingo Molnardd41f592007-07-09 18:51:59 +02005692#ifdef CONFIG_SCHED_DEBUG
5693 sysrq_sched_debug_show();
5694#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005696 /*
5697 * Only show locks if all tasks are dumped:
5698 */
5699 if (state_filter == -1)
5700 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701}
5702
Ingo Molnar1df21052007-07-09 18:51:58 +02005703void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5704{
Ingo Molnardd41f592007-07-09 18:51:59 +02005705 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005706}
5707
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005708/**
5709 * init_idle - set up an idle thread for a given CPU
5710 * @idle: task in question
5711 * @cpu: cpu the idle task belongs to
5712 *
5713 * NOTE: this function does not set the idle thread's NEED_RESCHED
5714 * flag, to make booting more robust.
5715 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005716void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005718 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 unsigned long flags;
5720
Ingo Molnardd41f592007-07-09 18:51:59 +02005721 __sched_fork(idle);
5722 idle->se.exec_start = sched_clock();
5723
Ingo Molnarb29739f2006-06-27 02:54:51 -07005724 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005726 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727
5728 spin_lock_irqsave(&rq->lock, flags);
5729 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005730#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5731 idle->oncpu = 1;
5732#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 spin_unlock_irqrestore(&rq->lock, flags);
5734
5735 /* Set the preempt count _outside_ the spinlocks! */
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005736#if defined(CONFIG_PREEMPT)
5737 task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
5738#else
Al Viroa1261f542005-11-13 16:06:55 -08005739 task_thread_info(idle)->preempt_count = 0;
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005740#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02005741 /*
5742 * The idle tasks have their own, simple scheduling class:
5743 */
5744 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745}
5746
5747/*
5748 * In a system that switches off the HZ timer nohz_cpu_mask
5749 * indicates which cpus entered this state. This is used
5750 * in the rcu update to wait only for active cpus. For system
5751 * which do not switch off the HZ timer nohz_cpu_mask should
5752 * always be CPU_MASK_NONE.
5753 */
5754cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
5755
Ingo Molnar19978ca2007-11-09 22:39:38 +01005756/*
5757 * Increase the granularity value when there are more CPUs,
5758 * because with more CPUs the 'effective latency' as visible
5759 * to users decreases. But the relationship is not linear,
5760 * so pick a second-best guess by going with the log2 of the
5761 * number of CPUs.
5762 *
5763 * This idea comes from the SD scheduler of Con Kolivas:
5764 */
5765static inline void sched_init_granularity(void)
5766{
5767 unsigned int factor = 1 + ilog2(num_online_cpus());
5768 const unsigned long limit = 200000000;
5769
5770 sysctl_sched_min_granularity *= factor;
5771 if (sysctl_sched_min_granularity > limit)
5772 sysctl_sched_min_granularity = limit;
5773
5774 sysctl_sched_latency *= factor;
5775 if (sysctl_sched_latency > limit)
5776 sysctl_sched_latency = limit;
5777
5778 sysctl_sched_wakeup_granularity *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01005779}
5780
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781#ifdef CONFIG_SMP
5782/*
5783 * This is how migration works:
5784 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07005785 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 * runqueue and wake up that CPU's migration thread.
5787 * 2) we down() the locked semaphore => thread blocks.
5788 * 3) migration thread wakes up (implicitly it forces the migrated
5789 * thread off the CPU)
5790 * 4) it gets the migration request and checks whether the migrated
5791 * task is still in the wrong runqueue.
5792 * 5) if it's in the wrong runqueue then the migration thread removes
5793 * it and puts it into the right queue.
5794 * 6) migration thread up()s the semaphore.
5795 * 7) we wake up and the migration is done.
5796 */
5797
5798/*
5799 * Change a given task's CPU affinity. Migrate the thread to a
5800 * proper CPU and schedule it away if the CPU it's executing on
5801 * is removed from the allowed bitmask.
5802 *
5803 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005804 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 * call is not atomic; no spinlocks may be held.
5806 */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005807int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005809 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005811 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005812 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813
5814 rq = task_rq_lock(p, &flags);
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005815 if (!cpus_intersects(*new_mask, cpu_online_map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 ret = -EINVAL;
5817 goto out;
5818 }
5819
David Rientjes9985b0b2008-06-05 12:57:11 -07005820 if (unlikely((p->flags & PF_THREAD_BOUND) && p != current &&
5821 !cpus_equal(p->cpus_allowed, *new_mask))) {
5822 ret = -EINVAL;
5823 goto out;
5824 }
5825
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01005826 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005827 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01005828 else {
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005829 p->cpus_allowed = *new_mask;
5830 p->rt.nr_cpus_allowed = cpus_weight(*new_mask);
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01005831 }
5832
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 /* Can the task run on the task's current CPU? If so, we're done */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005834 if (cpu_isset(task_cpu(p), *new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 goto out;
5836
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005837 if (migrate_task(p, any_online_cpu(*new_mask), &req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 /* Need help from migration thread: drop lock and wait. */
5839 task_rq_unlock(rq, &flags);
5840 wake_up_process(rq->migration_thread);
5841 wait_for_completion(&req.done);
5842 tlb_migrate_finish(p->mm);
5843 return 0;
5844 }
5845out:
5846 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005847
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 return ret;
5849}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07005850EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851
5852/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005853 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 * this because either it can't run here any more (set_cpus_allowed()
5855 * away from this CPU, or CPU going down), or because we're
5856 * attempting to rebalance this task on exec (sched_exec).
5857 *
5858 * So we race with normal scheduler movements, but that's OK, as long
5859 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07005860 *
5861 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07005863static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005865 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02005866 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
5868 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07005869 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870
5871 rq_src = cpu_rq(src_cpu);
5872 rq_dest = cpu_rq(dest_cpu);
5873
5874 double_rq_lock(rq_src, rq_dest);
5875 /* Already moved. */
5876 if (task_cpu(p) != src_cpu)
5877 goto out;
5878 /* Affinity changed (again). */
5879 if (!cpu_isset(dest_cpu, p->cpus_allowed))
5880 goto out;
5881
Ingo Molnardd41f592007-07-09 18:51:59 +02005882 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005883 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005884 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005885
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005887 if (on_rq) {
5888 activate_task(rq_dest, p, 0);
5889 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07005891 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892out:
5893 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07005894 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895}
5896
5897/*
5898 * migration_thread - this is a highprio system thread that performs
5899 * thread migration by bumping thread off CPU then 'pushing' onto
5900 * another runqueue.
5901 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005902static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005905 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906
5907 rq = cpu_rq(cpu);
5908 BUG_ON(rq->migration_thread != current);
5909
5910 set_current_state(TASK_INTERRUPTIBLE);
5911 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005912 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 spin_lock_irq(&rq->lock);
5916
5917 if (cpu_is_offline(cpu)) {
5918 spin_unlock_irq(&rq->lock);
5919 goto wait_to_die;
5920 }
5921
5922 if (rq->active_balance) {
5923 active_load_balance(rq, cpu);
5924 rq->active_balance = 0;
5925 }
5926
5927 head = &rq->migration_queue;
5928
5929 if (list_empty(head)) {
5930 spin_unlock_irq(&rq->lock);
5931 schedule();
5932 set_current_state(TASK_INTERRUPTIBLE);
5933 continue;
5934 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07005935 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 list_del_init(head->next);
5937
Nick Piggin674311d2005-06-25 14:57:27 -07005938 spin_unlock(&rq->lock);
5939 __migrate_task(req->task, cpu, req->dest_cpu);
5940 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941
5942 complete(&req->done);
5943 }
5944 __set_current_state(TASK_RUNNING);
5945 return 0;
5946
5947wait_to_die:
5948 /* Wait for kthread_stop */
5949 set_current_state(TASK_INTERRUPTIBLE);
5950 while (!kthread_should_stop()) {
5951 schedule();
5952 set_current_state(TASK_INTERRUPTIBLE);
5953 }
5954 __set_current_state(TASK_RUNNING);
5955 return 0;
5956}
5957
5958#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005959
5960static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
5961{
5962 int ret;
5963
5964 local_irq_disable();
5965 ret = __migrate_task(p, src_cpu, dest_cpu);
5966 local_irq_enable();
5967 return ret;
5968}
5969
Kirill Korotaev054b9102006-12-10 02:20:11 -08005970/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02005971 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08005972 * NOTE: interrupts should be disabled by the caller
5973 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005974static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975{
Kirill Korotaevefc30812006-06-27 02:54:32 -07005976 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005978 struct rq *rq;
5979 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980
Andi Kleen3a5c3592007-10-15 17:00:14 +02005981 do {
5982 /* On same node? */
5983 mask = node_to_cpumask(cpu_to_node(dead_cpu));
5984 cpus_and(mask, mask, p->cpus_allowed);
5985 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986
Andi Kleen3a5c3592007-10-15 17:00:14 +02005987 /* On any allowed CPU? */
Mike Travis434d53b2008-04-04 18:11:04 -07005988 if (dest_cpu >= nr_cpu_ids)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005989 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990
Andi Kleen3a5c3592007-10-15 17:00:14 +02005991 /* No more Mr. Nice Guy. */
Mike Travis434d53b2008-04-04 18:11:04 -07005992 if (dest_cpu >= nr_cpu_ids) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07005993 cpumask_t cpus_allowed;
5994
5995 cpuset_cpus_allowed_locked(p, &cpus_allowed);
Cliff Wickman470fd642007-10-18 23:40:46 -07005996 /*
5997 * Try to stay on the same cpuset, where the
5998 * current cpuset may be a subset of all cpus.
5999 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006000 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07006001 * called within calls to cpuset_lock/cpuset_unlock.
6002 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02006003 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07006004 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006005 dest_cpu = any_online_cpu(p->cpus_allowed);
6006 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007
Andi Kleen3a5c3592007-10-15 17:00:14 +02006008 /*
6009 * Don't tell them about moving exiting tasks or
6010 * kernel threads (both mm NULL), since they never
6011 * leave kernel.
6012 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006013 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02006014 printk(KERN_INFO "process %d (%s) no "
6015 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006016 task_pid_nr(p), p->comm, dead_cpu);
6017 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006018 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006019 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020}
6021
6022/*
6023 * While a dead CPU has no uninterruptible tasks queued at this point,
6024 * it might still have a nonzero ->nr_uninterruptible counter, because
6025 * for performance reasons the counter is not stricly tracking tasks to
6026 * their home CPUs. So we just add the counter to another CPU's counter,
6027 * to keep the global sum constant after CPU-down:
6028 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07006029static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030{
Mike Travis7c16ec52008-04-04 18:11:11 -07006031 struct rq *rq_dest = cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 unsigned long flags;
6033
6034 local_irq_save(flags);
6035 double_rq_lock(rq_src, rq_dest);
6036 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
6037 rq_src->nr_uninterruptible = 0;
6038 double_rq_unlock(rq_src, rq_dest);
6039 local_irq_restore(flags);
6040}
6041
6042/* Run through task list and migrate tasks from the dead cpu. */
6043static void migrate_live_tasks(int src_cpu)
6044{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006045 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006047 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048
Ingo Molnar48f24c42006-07-03 00:25:40 -07006049 do_each_thread(t, p) {
6050 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 continue;
6052
Ingo Molnar48f24c42006-07-03 00:25:40 -07006053 if (task_cpu(p) == src_cpu)
6054 move_task_off_dead_cpu(src_cpu, p);
6055 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006057 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058}
6059
Ingo Molnardd41f592007-07-09 18:51:59 +02006060/*
6061 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006062 * It does so by boosting its priority to highest possible.
6063 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 */
6065void sched_idle_next(void)
6066{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006067 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07006068 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 struct task_struct *p = rq->idle;
6070 unsigned long flags;
6071
6072 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006073 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074
Ingo Molnar48f24c42006-07-03 00:25:40 -07006075 /*
6076 * Strictly not necessary since rest of the CPUs are stopped by now
6077 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 */
6079 spin_lock_irqsave(&rq->lock, flags);
6080
Ingo Molnardd41f592007-07-09 18:51:59 +02006081 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006082
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006083 update_rq_clock(rq);
6084 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085
6086 spin_unlock_irqrestore(&rq->lock, flags);
6087}
6088
Ingo Molnar48f24c42006-07-03 00:25:40 -07006089/*
6090 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 * offline.
6092 */
6093void idle_task_exit(void)
6094{
6095 struct mm_struct *mm = current->active_mm;
6096
6097 BUG_ON(cpu_online(smp_processor_id()));
6098
6099 if (mm != &init_mm)
6100 switch_mm(mm, &init_mm, current);
6101 mmdrop(mm);
6102}
6103
Kirill Korotaev054b9102006-12-10 02:20:11 -08006104/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006105static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006107 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108
6109 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07006110 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111
6112 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07006113 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114
Ingo Molnar48f24c42006-07-03 00:25:40 -07006115 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116
6117 /*
6118 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006119 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 * fine.
6121 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006122 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006123 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006124 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125
Ingo Molnar48f24c42006-07-03 00:25:40 -07006126 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127}
6128
6129/* release_task() removes task from tasklist, so we won't find dead tasks. */
6130static void migrate_dead_tasks(unsigned int dead_cpu)
6131{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006132 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006133 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134
Ingo Molnardd41f592007-07-09 18:51:59 +02006135 for ( ; ; ) {
6136 if (!rq->nr_running)
6137 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02006138 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02006139 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02006140 if (!next)
6141 break;
6142 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02006143
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 }
6145}
6146#endif /* CONFIG_HOTPLUG_CPU */
6147
Nick Piggine692ab52007-07-26 13:40:43 +02006148#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
6149
6150static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006151 {
6152 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006153 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006154 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006155 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006156};
6157
6158static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006159 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006160 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006161 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006162 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006163 .child = sd_ctl_dir,
6164 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006165 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006166};
6167
6168static struct ctl_table *sd_alloc_ctl_entry(int n)
6169{
6170 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02006171 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02006172
Nick Piggine692ab52007-07-26 13:40:43 +02006173 return entry;
6174}
6175
Milton Miller6382bc92007-10-15 17:00:19 +02006176static void sd_free_ctl_entry(struct ctl_table **tablep)
6177{
Milton Millercd7900762007-10-17 16:55:11 +02006178 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02006179
Milton Millercd7900762007-10-17 16:55:11 +02006180 /*
6181 * In the intermediate directories, both the child directory and
6182 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006183 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02006184 * static strings and all have proc handlers.
6185 */
6186 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02006187 if (entry->child)
6188 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02006189 if (entry->proc_handler == NULL)
6190 kfree(entry->procname);
6191 }
Milton Miller6382bc92007-10-15 17:00:19 +02006192
6193 kfree(*tablep);
6194 *tablep = NULL;
6195}
6196
Nick Piggine692ab52007-07-26 13:40:43 +02006197static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02006198set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02006199 const char *procname, void *data, int maxlen,
6200 mode_t mode, proc_handler *proc_handler)
6201{
Nick Piggine692ab52007-07-26 13:40:43 +02006202 entry->procname = procname;
6203 entry->data = data;
6204 entry->maxlen = maxlen;
6205 entry->mode = mode;
6206 entry->proc_handler = proc_handler;
6207}
6208
6209static struct ctl_table *
6210sd_alloc_ctl_domain_table(struct sched_domain *sd)
6211{
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006212 struct ctl_table *table = sd_alloc_ctl_entry(12);
Nick Piggine692ab52007-07-26 13:40:43 +02006213
Milton Millerad1cdc12007-10-15 17:00:19 +02006214 if (table == NULL)
6215 return NULL;
6216
Alexey Dobriyane0361852007-08-09 11:16:46 +02006217 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006218 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006219 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006220 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006221 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006222 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006223 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006224 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006225 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006226 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006227 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006228 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006229 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006230 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006231 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02006232 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006233 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02006234 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006235 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02006236 &sd->cache_nice_tries,
6237 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006238 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02006239 sizeof(int), 0644, proc_dointvec_minmax);
Milton Miller6323469f2007-10-15 17:00:19 +02006240 /* &table[11] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02006241
6242 return table;
6243}
6244
Ingo Molnar9a4e7152007-11-28 15:52:56 +01006245static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02006246{
6247 struct ctl_table *entry, *table;
6248 struct sched_domain *sd;
6249 int domain_num = 0, i;
6250 char buf[32];
6251
6252 for_each_domain(cpu, sd)
6253 domain_num++;
6254 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02006255 if (table == NULL)
6256 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02006257
6258 i = 0;
6259 for_each_domain(cpu, sd) {
6260 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006261 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006262 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006263 entry->child = sd_alloc_ctl_domain_table(sd);
6264 entry++;
6265 i++;
6266 }
6267 return table;
6268}
6269
6270static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02006271static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006272{
6273 int i, cpu_num = num_online_cpus();
6274 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
6275 char buf[32];
6276
Milton Miller73785472007-10-24 18:23:48 +02006277 WARN_ON(sd_ctl_dir[0].child);
6278 sd_ctl_dir[0].child = entry;
6279
Milton Millerad1cdc12007-10-15 17:00:19 +02006280 if (entry == NULL)
6281 return;
6282
Milton Miller97b6ea72007-10-15 17:00:19 +02006283 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02006284 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006285 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006286 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006287 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02006288 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02006289 }
Milton Miller73785472007-10-24 18:23:48 +02006290
6291 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02006292 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
6293}
Milton Miller6382bc92007-10-15 17:00:19 +02006294
Milton Miller73785472007-10-24 18:23:48 +02006295/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02006296static void unregister_sched_domain_sysctl(void)
6297{
Milton Miller73785472007-10-24 18:23:48 +02006298 if (sd_sysctl_header)
6299 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02006300 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02006301 if (sd_ctl_dir[0].child)
6302 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02006303}
Nick Piggine692ab52007-07-26 13:40:43 +02006304#else
Milton Miller6382bc92007-10-15 17:00:19 +02006305static void register_sched_domain_sysctl(void)
6306{
6307}
6308static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006309{
6310}
6311#endif
6312
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006313static void set_rq_online(struct rq *rq)
6314{
6315 if (!rq->online) {
6316 const struct sched_class *class;
6317
6318 cpu_set(rq->cpu, rq->rd->online);
6319 rq->online = 1;
6320
6321 for_each_class(class) {
6322 if (class->rq_online)
6323 class->rq_online(rq);
6324 }
6325 }
6326}
6327
6328static void set_rq_offline(struct rq *rq)
6329{
6330 if (rq->online) {
6331 const struct sched_class *class;
6332
6333 for_each_class(class) {
6334 if (class->rq_offline)
6335 class->rq_offline(rq);
6336 }
6337
6338 cpu_clear(rq->cpu, rq->rd->online);
6339 rq->online = 0;
6340 }
6341}
6342
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343/*
6344 * migration_call - callback that gets triggered when a CPU is added.
6345 * Here we can start up the necessary migration thread for the new CPU.
6346 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006347static int __cpuinit
6348migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006351 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006353 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354
6355 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006356
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006358 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02006359 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 if (IS_ERR(p))
6361 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 kthread_bind(p, cpu);
6363 /* Must be high prio: stop_machine expects to yield to it. */
6364 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02006365 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366 task_rq_unlock(rq, &flags);
6367 cpu_rq(cpu)->migration_thread = p;
6368 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006369
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006371 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006372 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006374
6375 /* Update our root-domain */
6376 rq = cpu_rq(cpu);
6377 spin_lock_irqsave(&rq->lock, flags);
6378 if (rq->rd) {
6379 BUG_ON(!cpu_isset(cpu, rq->rd->span));
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006380
6381 set_rq_online(rq);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006382 }
6383 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006385
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386#ifdef CONFIG_HOTPLUG_CPU
6387 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006388 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07006389 if (!cpu_rq(cpu)->migration_thread)
6390 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006391 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08006392 kthread_bind(cpu_rq(cpu)->migration_thread,
6393 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394 kthread_stop(cpu_rq(cpu)->migration_thread);
6395 cpu_rq(cpu)->migration_thread = NULL;
6396 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006397
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006399 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07006400 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401 migrate_live_tasks(cpu);
6402 rq = cpu_rq(cpu);
6403 kthread_stop(rq->migration_thread);
6404 rq->migration_thread = NULL;
6405 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006406 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02006407 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006408 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02006410 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
6411 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006413 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07006414 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415 migrate_nr_uninterruptible(rq);
6416 BUG_ON(rq->nr_running != 0);
6417
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006418 /*
6419 * No need to migrate the tasks: it was best-effort if
6420 * they didn't take sched_hotcpu_mutex. Just wake up
6421 * the requestors.
6422 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423 spin_lock_irq(&rq->lock);
6424 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006425 struct migration_req *req;
6426
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07006428 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429 list_del_init(&req->list);
6430 complete(&req->done);
6431 }
6432 spin_unlock_irq(&rq->lock);
6433 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006434
Gregory Haskins08f503b2008-03-10 17:59:11 -04006435 case CPU_DYING:
6436 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01006437 /* Update our root-domain */
6438 rq = cpu_rq(cpu);
6439 spin_lock_irqsave(&rq->lock, flags);
6440 if (rq->rd) {
6441 BUG_ON(!cpu_isset(cpu, rq->rd->span));
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006442 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006443 }
6444 spin_unlock_irqrestore(&rq->lock, flags);
6445 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446#endif
6447 }
6448 return NOTIFY_OK;
6449}
6450
6451/* Register at highest priority so that task migration (migrate_all_tasks)
6452 * happens before everything else.
6453 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006454static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455 .notifier_call = migration_call,
6456 .priority = 10
6457};
6458
Adrian Bunke6fe6642007-11-09 22:39:39 +01006459void __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460{
6461 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006462 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006463
6464 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006465 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6466 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6468 register_cpu_notifier(&migration_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469}
6470#endif
6471
6472#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006473
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006474#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006475
Gautham R Shenoy099f98c2008-05-29 20:56:32 +05306476static inline const char *sd_level_to_string(enum sched_domain_level lvl)
6477{
6478 switch (lvl) {
6479 case SD_LV_NONE:
6480 return "NONE";
6481 case SD_LV_SIBLING:
6482 return "SIBLING";
6483 case SD_LV_MC:
6484 return "MC";
6485 case SD_LV_CPU:
6486 return "CPU";
6487 case SD_LV_NODE:
6488 return "NODE";
6489 case SD_LV_ALLNODES:
6490 return "ALLNODES";
6491 case SD_LV_MAX:
6492 return "MAX";
6493
6494 }
6495 return "MAX";
6496}
6497
Mike Travis7c16ec52008-04-04 18:11:11 -07006498static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
6499 cpumask_t *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006500{
6501 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07006502 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006503
Mike Travis434d53b2008-04-04 18:11:04 -07006504 cpulist_scnprintf(str, sizeof(str), sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07006505 cpus_clear(*groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006506
6507 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6508
6509 if (!(sd->flags & SD_LOAD_BALANCE)) {
6510 printk("does not load-balance\n");
6511 if (sd->parent)
6512 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6513 " has parent");
6514 return -1;
6515 }
6516
Gautham R Shenoy099f98c2008-05-29 20:56:32 +05306517 printk(KERN_CONT "span %s level %s\n",
6518 str, sd_level_to_string(sd->level));
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006519
6520 if (!cpu_isset(cpu, sd->span)) {
6521 printk(KERN_ERR "ERROR: domain->span does not contain "
6522 "CPU%d\n", cpu);
6523 }
6524 if (!cpu_isset(cpu, group->cpumask)) {
6525 printk(KERN_ERR "ERROR: domain->groups does not contain"
6526 " CPU%d\n", cpu);
6527 }
6528
6529 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6530 do {
6531 if (!group) {
6532 printk("\n");
6533 printk(KERN_ERR "ERROR: group is NULL\n");
6534 break;
6535 }
6536
6537 if (!group->__cpu_power) {
6538 printk(KERN_CONT "\n");
6539 printk(KERN_ERR "ERROR: domain->cpu_power not "
6540 "set\n");
6541 break;
6542 }
6543
6544 if (!cpus_weight(group->cpumask)) {
6545 printk(KERN_CONT "\n");
6546 printk(KERN_ERR "ERROR: empty group\n");
6547 break;
6548 }
6549
Mike Travis7c16ec52008-04-04 18:11:11 -07006550 if (cpus_intersects(*groupmask, group->cpumask)) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006551 printk(KERN_CONT "\n");
6552 printk(KERN_ERR "ERROR: repeated CPUs\n");
6553 break;
6554 }
6555
Mike Travis7c16ec52008-04-04 18:11:11 -07006556 cpus_or(*groupmask, *groupmask, group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006557
Mike Travis434d53b2008-04-04 18:11:04 -07006558 cpulist_scnprintf(str, sizeof(str), group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006559 printk(KERN_CONT " %s", str);
6560
6561 group = group->next;
6562 } while (group != sd->groups);
6563 printk(KERN_CONT "\n");
6564
Mike Travis7c16ec52008-04-04 18:11:11 -07006565 if (!cpus_equal(sd->span, *groupmask))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006566 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6567
Mike Travis7c16ec52008-04-04 18:11:11 -07006568 if (sd->parent && !cpus_subset(*groupmask, sd->parent->span))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006569 printk(KERN_ERR "ERROR: parent span is not a superset "
6570 "of domain->span\n");
6571 return 0;
6572}
6573
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574static void sched_domain_debug(struct sched_domain *sd, int cpu)
6575{
Mike Travis7c16ec52008-04-04 18:11:11 -07006576 cpumask_t *groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577 int level = 0;
6578
Nick Piggin41c7ce92005-06-25 14:57:24 -07006579 if (!sd) {
6580 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6581 return;
6582 }
6583
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6585
Mike Travis7c16ec52008-04-04 18:11:11 -07006586 groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6587 if (!groupmask) {
6588 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
6589 return;
6590 }
6591
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006592 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006593 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595 level++;
6596 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006597 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006598 break;
6599 }
Mike Travis7c16ec52008-04-04 18:11:11 -07006600 kfree(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006602#else /* !CONFIG_SCHED_DEBUG */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006603# define sched_domain_debug(sd, cpu) do { } while (0)
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006604#endif /* CONFIG_SCHED_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006606static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006607{
6608 if (cpus_weight(sd->span) == 1)
6609 return 1;
6610
6611 /* Following flags need at least 2 groups */
6612 if (sd->flags & (SD_LOAD_BALANCE |
6613 SD_BALANCE_NEWIDLE |
6614 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006615 SD_BALANCE_EXEC |
6616 SD_SHARE_CPUPOWER |
6617 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006618 if (sd->groups != sd->groups->next)
6619 return 0;
6620 }
6621
6622 /* Following flags don't use groups */
6623 if (sd->flags & (SD_WAKE_IDLE |
6624 SD_WAKE_AFFINE |
6625 SD_WAKE_BALANCE))
6626 return 0;
6627
6628 return 1;
6629}
6630
Ingo Molnar48f24c42006-07-03 00:25:40 -07006631static int
6632sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006633{
6634 unsigned long cflags = sd->flags, pflags = parent->flags;
6635
6636 if (sd_degenerate(parent))
6637 return 1;
6638
6639 if (!cpus_equal(sd->span, parent->span))
6640 return 0;
6641
6642 /* Does parent contain flags not in child? */
6643 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6644 if (cflags & SD_WAKE_AFFINE)
6645 pflags &= ~SD_WAKE_BALANCE;
6646 /* Flags needing groups don't count if only 1 group in parent */
6647 if (parent->groups == parent->groups->next) {
6648 pflags &= ~(SD_LOAD_BALANCE |
6649 SD_BALANCE_NEWIDLE |
6650 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006651 SD_BALANCE_EXEC |
6652 SD_SHARE_CPUPOWER |
6653 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006654 }
6655 if (~cflags & pflags)
6656 return 0;
6657
6658 return 1;
6659}
6660
Gregory Haskins57d885f2008-01-25 21:08:18 +01006661static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6662{
6663 unsigned long flags;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006664
6665 spin_lock_irqsave(&rq->lock, flags);
6666
6667 if (rq->rd) {
6668 struct root_domain *old_rd = rq->rd;
6669
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006670 if (cpu_isset(rq->cpu, old_rd->online))
6671 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006672
Gregory Haskinsdc938522008-01-25 21:08:26 +01006673 cpu_clear(rq->cpu, old_rd->span);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006674
Gregory Haskins57d885f2008-01-25 21:08:18 +01006675 if (atomic_dec_and_test(&old_rd->refcount))
6676 kfree(old_rd);
6677 }
6678
6679 atomic_inc(&rd->refcount);
6680 rq->rd = rd;
6681
Gregory Haskinsdc938522008-01-25 21:08:26 +01006682 cpu_set(rq->cpu, rd->span);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006683 if (cpu_isset(rq->cpu, cpu_online_map))
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006684 set_rq_online(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006685
6686 spin_unlock_irqrestore(&rq->lock, flags);
6687}
6688
Gregory Haskinsdc938522008-01-25 21:08:26 +01006689static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006690{
6691 memset(rd, 0, sizeof(*rd));
6692
Gregory Haskinsdc938522008-01-25 21:08:26 +01006693 cpus_clear(rd->span);
6694 cpus_clear(rd->online);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02006695
6696 cpupri_init(&rd->cpupri);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006697}
6698
6699static void init_defrootdomain(void)
6700{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006701 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006702 atomic_set(&def_root_domain.refcount, 1);
6703}
6704
Gregory Haskinsdc938522008-01-25 21:08:26 +01006705static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006706{
6707 struct root_domain *rd;
6708
6709 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6710 if (!rd)
6711 return NULL;
6712
Gregory Haskinsdc938522008-01-25 21:08:26 +01006713 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006714
6715 return rd;
6716}
6717
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006719 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720 * hold the hotplug lock.
6721 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006722static void
6723cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006725 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006726 struct sched_domain *tmp;
6727
6728 /* Remove the sched domains which do not contribute to scheduling. */
6729 for (tmp = sd; tmp; tmp = tmp->parent) {
6730 struct sched_domain *parent = tmp->parent;
6731 if (!parent)
6732 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006733 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006734 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006735 if (parent->parent)
6736 parent->parent->child = tmp;
6737 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07006738 }
6739
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006740 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006741 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006742 if (sd)
6743 sd->child = NULL;
6744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745
6746 sched_domain_debug(sd, cpu);
6747
Gregory Haskins57d885f2008-01-25 21:08:18 +01006748 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006749 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750}
6751
6752/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006753static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754
6755/* Setup the mask of cpus configured for isolated domains */
6756static int __init isolated_cpu_setup(char *str)
6757{
6758 int ints[NR_CPUS], i;
6759
6760 str = get_options(str, ARRAY_SIZE(ints), ints);
6761 cpus_clear(cpu_isolated_map);
6762 for (i = 1; i <= ints[0]; i++)
6763 if (ints[i] < NR_CPUS)
6764 cpu_set(ints[i], cpu_isolated_map);
6765 return 1;
6766}
6767
Ingo Molnar8927f492007-10-15 17:00:13 +02006768__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769
6770/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006771 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6772 * to a function which identifies what group(along with sched group) a CPU
6773 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6774 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775 *
6776 * init_sched_build_groups will build a circular linked list of the groups
6777 * covered by the given span, and will set each group's ->cpumask correctly,
6778 * and ->cpu_power to 0.
6779 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006780static void
Mike Travis7c16ec52008-04-04 18:11:11 -07006781init_sched_build_groups(const cpumask_t *span, const cpumask_t *cpu_map,
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006782 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006783 struct sched_group **sg,
6784 cpumask_t *tmpmask),
6785 cpumask_t *covered, cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786{
6787 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788 int i;
6789
Mike Travis7c16ec52008-04-04 18:11:11 -07006790 cpus_clear(*covered);
6791
6792 for_each_cpu_mask(i, *span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006793 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07006794 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795 int j;
6796
Mike Travis7c16ec52008-04-04 18:11:11 -07006797 if (cpu_isset(i, *covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 continue;
6799
Mike Travis7c16ec52008-04-04 18:11:11 -07006800 cpus_clear(sg->cpumask);
Eric Dumazet5517d862007-05-08 00:32:57 -07006801 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802
Mike Travis7c16ec52008-04-04 18:11:11 -07006803 for_each_cpu_mask(j, *span) {
6804 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 continue;
6806
Mike Travis7c16ec52008-04-04 18:11:11 -07006807 cpu_set(j, *covered);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808 cpu_set(j, sg->cpumask);
6809 }
6810 if (!first)
6811 first = sg;
6812 if (last)
6813 last->next = sg;
6814 last = sg;
6815 }
6816 last->next = first;
6817}
6818
John Hawkes9c1cfda2005-09-06 15:18:14 -07006819#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820
John Hawkes9c1cfda2005-09-06 15:18:14 -07006821#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006822
John Hawkes9c1cfda2005-09-06 15:18:14 -07006823/**
6824 * find_next_best_node - find the next node to include in a sched_domain
6825 * @node: node whose sched_domain we're building
6826 * @used_nodes: nodes already in the sched_domain
6827 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006828 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006829 * finds the closest node not already in the @used_nodes map.
6830 *
6831 * Should use nodemask_t.
6832 */
Mike Travisc5f59f02008-04-04 18:11:10 -07006833static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006834{
6835 int i, n, val, min_val, best_node = 0;
6836
6837 min_val = INT_MAX;
6838
6839 for (i = 0; i < MAX_NUMNODES; i++) {
6840 /* Start at @node */
6841 n = (node + i) % MAX_NUMNODES;
6842
6843 if (!nr_cpus_node(n))
6844 continue;
6845
6846 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07006847 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07006848 continue;
6849
6850 /* Simple min distance search */
6851 val = node_distance(node, n);
6852
6853 if (val < min_val) {
6854 min_val = val;
6855 best_node = n;
6856 }
6857 }
6858
Mike Travisc5f59f02008-04-04 18:11:10 -07006859 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006860 return best_node;
6861}
6862
6863/**
6864 * sched_domain_node_span - get a cpumask for a node's sched_domain
6865 * @node: node whose cpumask we're constructing
Randy Dunlap73486722008-04-22 10:07:22 -07006866 * @span: resulting cpumask
John Hawkes9c1cfda2005-09-06 15:18:14 -07006867 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006868 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07006869 * should be one that prevents unnecessary balancing, but also spreads tasks
6870 * out optimally.
6871 */
Mike Travis4bdbaad32008-04-15 16:35:52 -07006872static void sched_domain_node_span(int node, cpumask_t *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07006873{
Mike Travisc5f59f02008-04-04 18:11:10 -07006874 nodemask_t used_nodes;
Mike Travisc5f59f02008-04-04 18:11:10 -07006875 node_to_cpumask_ptr(nodemask, node);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006876 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006877
Mike Travis4bdbaad32008-04-15 16:35:52 -07006878 cpus_clear(*span);
Mike Travisc5f59f02008-04-04 18:11:10 -07006879 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006880
Mike Travis4bdbaad32008-04-15 16:35:52 -07006881 cpus_or(*span, *span, *nodemask);
Mike Travisc5f59f02008-04-04 18:11:10 -07006882 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006883
6884 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07006885 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006886
Mike Travisc5f59f02008-04-04 18:11:10 -07006887 node_to_cpumask_ptr_next(nodemask, next_node);
Mike Travis4bdbaad32008-04-15 16:35:52 -07006888 cpus_or(*span, *span, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006889 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006890}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006891#endif /* CONFIG_NUMA */
John Hawkes9c1cfda2005-09-06 15:18:14 -07006892
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006893int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006894
John Hawkes9c1cfda2005-09-06 15:18:14 -07006895/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07006896 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07006897 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898#ifdef CONFIG_SCHED_SMT
6899static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006900static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006901
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006902static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006903cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6904 cpumask_t *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006906 if (sg)
6907 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908 return cpu;
6909}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006910#endif /* CONFIG_SCHED_SMT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911
Ingo Molnar48f24c42006-07-03 00:25:40 -07006912/*
6913 * multi-core sched-domains:
6914 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006915#ifdef CONFIG_SCHED_MC
6916static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006917static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006918#endif /* CONFIG_SCHED_MC */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006919
6920#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006921static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006922cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6923 cpumask_t *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006924{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006925 int group;
Mike Travis7c16ec52008-04-04 18:11:11 -07006926
6927 *mask = per_cpu(cpu_sibling_map, cpu);
6928 cpus_and(*mask, *mask, *cpu_map);
6929 group = first_cpu(*mask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006930 if (sg)
6931 *sg = &per_cpu(sched_group_core, group);
6932 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006933}
6934#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006935static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006936cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6937 cpumask_t *unused)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006938{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006939 if (sg)
6940 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006941 return cpu;
6942}
6943#endif
6944
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006946static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006947
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006948static int
Mike Travis7c16ec52008-04-04 18:11:11 -07006949cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
6950 cpumask_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006952 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006953#ifdef CONFIG_SCHED_MC
Mike Travis7c16ec52008-04-04 18:11:11 -07006954 *mask = cpu_coregroup_map(cpu);
6955 cpus_and(*mask, *mask, *cpu_map);
6956 group = first_cpu(*mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006957#elif defined(CONFIG_SCHED_SMT)
Mike Travis7c16ec52008-04-04 18:11:11 -07006958 *mask = per_cpu(cpu_sibling_map, cpu);
6959 cpus_and(*mask, *mask, *cpu_map);
6960 group = first_cpu(*mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006962 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006964 if (sg)
6965 *sg = &per_cpu(sched_group_phys, group);
6966 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967}
6968
6969#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07006970/*
6971 * The init_sched_build_groups can't handle what we want to do with node
6972 * groups, so roll our own. Now each node has its own list of groups which
6973 * gets dynamically allocated.
6974 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975static DEFINE_PER_CPU(struct sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07006976static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006977
6978static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006979static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006980
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006981static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07006982 struct sched_group **sg, cpumask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006984 int group;
6985
Mike Travis7c16ec52008-04-04 18:11:11 -07006986 *nodemask = node_to_cpumask(cpu_to_node(cpu));
6987 cpus_and(*nodemask, *nodemask, *cpu_map);
6988 group = first_cpu(*nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006989
6990 if (sg)
6991 *sg = &per_cpu(sched_group_allnodes, group);
6992 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006994
Siddha, Suresh B08069032006-03-27 01:15:23 -08006995static void init_numa_sched_groups_power(struct sched_group *group_head)
6996{
6997 struct sched_group *sg = group_head;
6998 int j;
6999
7000 if (!sg)
7001 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02007002 do {
7003 for_each_cpu_mask(j, sg->cpumask) {
7004 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08007005
Andi Kleen3a5c3592007-10-15 17:00:14 +02007006 sd = &per_cpu(phys_domains, j);
7007 if (j != first_cpu(sd->groups->cpumask)) {
7008 /*
7009 * Only add "power" once for each
7010 * physical package.
7011 */
7012 continue;
7013 }
7014
7015 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007016 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02007017 sg = sg->next;
7018 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007019}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007020#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007022#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007023/* Free memory allocated for various sched_group structures */
Mike Travis7c16ec52008-04-04 18:11:11 -07007024static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007025{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007026 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007027
7028 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007029 struct sched_group **sched_group_nodes
7030 = sched_group_nodes_bycpu[cpu];
7031
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007032 if (!sched_group_nodes)
7033 continue;
7034
7035 for (i = 0; i < MAX_NUMNODES; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007036 struct sched_group *oldsg, *sg = sched_group_nodes[i];
7037
Mike Travis7c16ec52008-04-04 18:11:11 -07007038 *nodemask = node_to_cpumask(i);
7039 cpus_and(*nodemask, *nodemask, *cpu_map);
7040 if (cpus_empty(*nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007041 continue;
7042
7043 if (sg == NULL)
7044 continue;
7045 sg = sg->next;
7046next_sg:
7047 oldsg = sg;
7048 sg = sg->next;
7049 kfree(oldsg);
7050 if (oldsg != sched_group_nodes[i])
7051 goto next_sg;
7052 }
7053 kfree(sched_group_nodes);
7054 sched_group_nodes_bycpu[cpu] = NULL;
7055 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007056}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007057#else /* !CONFIG_NUMA */
Mike Travis7c16ec52008-04-04 18:11:11 -07007058static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007059{
7060}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007061#endif /* CONFIG_NUMA */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007062
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007064 * Initialize sched groups cpu_power.
7065 *
7066 * cpu_power indicates the capacity of sched group, which is used while
7067 * distributing the load between different sched groups in a sched domain.
7068 * Typically cpu_power for all the groups in a sched domain will be same unless
7069 * there are asymmetries in the topology. If there are asymmetries, group
7070 * having more cpu_power will pickup more load compared to the group having
7071 * less cpu_power.
7072 *
7073 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
7074 * the maximum number of tasks a group can handle in the presence of other idle
7075 * or lightly loaded groups in the same sched domain.
7076 */
7077static void init_sched_groups_power(int cpu, struct sched_domain *sd)
7078{
7079 struct sched_domain *child;
7080 struct sched_group *group;
7081
7082 WARN_ON(!sd || !sd->groups);
7083
7084 if (cpu != first_cpu(sd->groups->cpumask))
7085 return;
7086
7087 child = sd->child;
7088
Eric Dumazet5517d862007-05-08 00:32:57 -07007089 sd->groups->__cpu_power = 0;
7090
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007091 /*
7092 * For perf policy, if the groups in child domain share resources
7093 * (for example cores sharing some portions of the cache hierarchy
7094 * or SMT), then set this domain groups cpu_power such that each group
7095 * can handle only one task, when there are other idle groups in the
7096 * same sched domain.
7097 */
7098 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
7099 (child->flags &
7100 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07007101 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007102 return;
7103 }
7104
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007105 /*
7106 * add cpu_power of each child group to this groups cpu_power
7107 */
7108 group = child->groups;
7109 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07007110 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007111 group = group->next;
7112 } while (group != child->groups);
7113}
7114
7115/*
Mike Travis7c16ec52008-04-04 18:11:11 -07007116 * Initializers for schedule domains
7117 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7118 */
7119
7120#define SD_INIT(sd, type) sd_init_##type(sd)
7121#define SD_INIT_FUNC(type) \
7122static noinline void sd_init_##type(struct sched_domain *sd) \
7123{ \
7124 memset(sd, 0, sizeof(*sd)); \
7125 *sd = SD_##type##_INIT; \
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007126 sd->level = SD_LV_##type; \
Mike Travis7c16ec52008-04-04 18:11:11 -07007127}
7128
7129SD_INIT_FUNC(CPU)
7130#ifdef CONFIG_NUMA
7131 SD_INIT_FUNC(ALLNODES)
7132 SD_INIT_FUNC(NODE)
7133#endif
7134#ifdef CONFIG_SCHED_SMT
7135 SD_INIT_FUNC(SIBLING)
7136#endif
7137#ifdef CONFIG_SCHED_MC
7138 SD_INIT_FUNC(MC)
7139#endif
7140
7141/*
7142 * To minimize stack usage kmalloc room for cpumasks and share the
7143 * space as the usage in build_sched_domains() dictates. Used only
7144 * if the amount of space is significant.
7145 */
7146struct allmasks {
7147 cpumask_t tmpmask; /* make this one first */
7148 union {
7149 cpumask_t nodemask;
7150 cpumask_t this_sibling_map;
7151 cpumask_t this_core_map;
7152 };
7153 cpumask_t send_covered;
7154
7155#ifdef CONFIG_NUMA
7156 cpumask_t domainspan;
7157 cpumask_t covered;
7158 cpumask_t notcovered;
7159#endif
7160};
7161
7162#if NR_CPUS > 128
7163#define SCHED_CPUMASK_ALLOC 1
7164#define SCHED_CPUMASK_FREE(v) kfree(v)
7165#define SCHED_CPUMASK_DECLARE(v) struct allmasks *v
7166#else
7167#define SCHED_CPUMASK_ALLOC 0
7168#define SCHED_CPUMASK_FREE(v)
7169#define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v
7170#endif
7171
7172#define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \
7173 ((unsigned long)(a) + offsetof(struct allmasks, v))
7174
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007175static int default_relax_domain_level = -1;
7176
7177static int __init setup_relax_domain_level(char *str)
7178{
Li Zefan30e0e172008-05-13 10:27:17 +08007179 unsigned long val;
7180
7181 val = simple_strtoul(str, NULL, 0);
7182 if (val < SD_LV_MAX)
7183 default_relax_domain_level = val;
7184
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007185 return 1;
7186}
7187__setup("relax_domain_level=", setup_relax_domain_level);
7188
7189static void set_domain_attribute(struct sched_domain *sd,
7190 struct sched_domain_attr *attr)
7191{
7192 int request;
7193
7194 if (!attr || attr->relax_domain_level < 0) {
7195 if (default_relax_domain_level < 0)
7196 return;
7197 else
7198 request = default_relax_domain_level;
7199 } else
7200 request = attr->relax_domain_level;
7201 if (request < sd->level) {
7202 /* turn off idle balance on this domain */
7203 sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
7204 } else {
7205 /* turn on idle balance on this domain */
7206 sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
7207 }
7208}
7209
Mike Travis7c16ec52008-04-04 18:11:11 -07007210/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007211 * Build sched domains for a given set of cpus and attach the sched domains
7212 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007214static int __build_sched_domains(const cpumask_t *cpu_map,
7215 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216{
7217 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007218 struct root_domain *rd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007219 SCHED_CPUMASK_DECLARE(allmasks);
7220 cpumask_t *tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07007221#ifdef CONFIG_NUMA
7222 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007223 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07007224
7225 /*
7226 * Allocate the per-node list of sched groups
7227 */
Milton Miller5cf9f062007-10-15 17:00:19 +02007228 sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007229 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07007230 if (!sched_group_nodes) {
7231 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007232 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07007233 }
John Hawkesd1b55132005-09-06 15:18:14 -07007234#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235
Gregory Haskinsdc938522008-01-25 21:08:26 +01007236 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01007237 if (!rd) {
7238 printk(KERN_WARNING "Cannot alloc root domain\n");
Mike Travis7c16ec52008-04-04 18:11:11 -07007239#ifdef CONFIG_NUMA
7240 kfree(sched_group_nodes);
7241#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007242 return -ENOMEM;
7243 }
7244
Mike Travis7c16ec52008-04-04 18:11:11 -07007245#if SCHED_CPUMASK_ALLOC
7246 /* get space for all scratch cpumask variables */
7247 allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL);
7248 if (!allmasks) {
7249 printk(KERN_WARNING "Cannot alloc cpumask array\n");
7250 kfree(rd);
7251#ifdef CONFIG_NUMA
7252 kfree(sched_group_nodes);
7253#endif
7254 return -ENOMEM;
7255 }
7256#endif
7257 tmpmask = (cpumask_t *)allmasks;
7258
7259
7260#ifdef CONFIG_NUMA
7261 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
7262#endif
7263
Linus Torvalds1da177e2005-04-16 15:20:36 -07007264 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007265 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007266 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007267 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007268 struct sched_domain *sd = NULL, *p;
Mike Travis7c16ec52008-04-04 18:11:11 -07007269 SCHED_CPUMASK_VAR(nodemask, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007270
Mike Travis7c16ec52008-04-04 18:11:11 -07007271 *nodemask = node_to_cpumask(cpu_to_node(i));
7272 cpus_and(*nodemask, *nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273
7274#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02007275 if (cpus_weight(*cpu_map) >
Mike Travis7c16ec52008-04-04 18:11:11 -07007276 SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007277 sd = &per_cpu(allnodes_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007278 SD_INIT(sd, ALLNODES);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007279 set_domain_attribute(sd, attr);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007280 sd->span = *cpu_map;
Mike Travis7c16ec52008-04-04 18:11:11 -07007281 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007282 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007283 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007284 } else
7285 p = NULL;
7286
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287 sd = &per_cpu(node_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007288 SD_INIT(sd, NODE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007289 set_domain_attribute(sd, attr);
Mike Travis4bdbaad32008-04-15 16:35:52 -07007290 sched_domain_node_span(cpu_to_node(i), &sd->span);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007291 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007292 if (p)
7293 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007294 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295#endif
7296
7297 p = sd;
7298 sd = &per_cpu(phys_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007299 SD_INIT(sd, CPU);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007300 set_domain_attribute(sd, attr);
Mike Travis7c16ec52008-04-04 18:11:11 -07007301 sd->span = *nodemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007302 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007303 if (p)
7304 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007305 cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007307#ifdef CONFIG_SCHED_MC
7308 p = sd;
7309 sd = &per_cpu(core_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007310 SD_INIT(sd, MC);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007311 set_domain_attribute(sd, attr);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007312 sd->span = cpu_coregroup_map(i);
7313 cpus_and(sd->span, sd->span, *cpu_map);
7314 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007315 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007316 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007317#endif
7318
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319#ifdef CONFIG_SCHED_SMT
7320 p = sd;
7321 sd = &per_cpu(cpu_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007322 SD_INIT(sd, SIBLING);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007323 set_domain_attribute(sd, attr);
Mike Travisd5a74302007-10-16 01:24:05 -07007324 sd->span = per_cpu(cpu_sibling_map, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007325 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007326 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007327 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007328 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007329#endif
7330 }
7331
7332#ifdef CONFIG_SCHED_SMT
7333 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07007334 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007335 SCHED_CPUMASK_VAR(this_sibling_map, allmasks);
7336 SCHED_CPUMASK_VAR(send_covered, allmasks);
7337
7338 *this_sibling_map = per_cpu(cpu_sibling_map, i);
7339 cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map);
7340 if (i != first_cpu(*this_sibling_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341 continue;
7342
Ingo Molnardd41f592007-07-09 18:51:59 +02007343 init_sched_build_groups(this_sibling_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007344 &cpu_to_cpu_group,
7345 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346 }
7347#endif
7348
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007349#ifdef CONFIG_SCHED_MC
7350 /* Set up multi-core groups */
7351 for_each_cpu_mask(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007352 SCHED_CPUMASK_VAR(this_core_map, allmasks);
7353 SCHED_CPUMASK_VAR(send_covered, allmasks);
7354
7355 *this_core_map = cpu_coregroup_map(i);
7356 cpus_and(*this_core_map, *this_core_map, *cpu_map);
7357 if (i != first_cpu(*this_core_map))
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007358 continue;
Mike Travis7c16ec52008-04-04 18:11:11 -07007359
Ingo Molnardd41f592007-07-09 18:51:59 +02007360 init_sched_build_groups(this_core_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007361 &cpu_to_core_group,
7362 send_covered, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007363 }
7364#endif
7365
Linus Torvalds1da177e2005-04-16 15:20:36 -07007366 /* Set up physical groups */
7367 for (i = 0; i < MAX_NUMNODES; i++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007368 SCHED_CPUMASK_VAR(nodemask, allmasks);
7369 SCHED_CPUMASK_VAR(send_covered, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370
Mike Travis7c16ec52008-04-04 18:11:11 -07007371 *nodemask = node_to_cpumask(i);
7372 cpus_and(*nodemask, *nodemask, *cpu_map);
7373 if (cpus_empty(*nodemask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374 continue;
7375
Mike Travis7c16ec52008-04-04 18:11:11 -07007376 init_sched_build_groups(nodemask, cpu_map,
7377 &cpu_to_phys_group,
7378 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007379 }
7380
7381#ifdef CONFIG_NUMA
7382 /* Set up node groups */
Mike Travis7c16ec52008-04-04 18:11:11 -07007383 if (sd_allnodes) {
7384 SCHED_CPUMASK_VAR(send_covered, allmasks);
7385
7386 init_sched_build_groups(cpu_map, cpu_map,
7387 &cpu_to_allnodes_group,
7388 send_covered, tmpmask);
7389 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007390
7391 for (i = 0; i < MAX_NUMNODES; i++) {
7392 /* Set up node groups */
7393 struct sched_group *sg, *prev;
Mike Travis7c16ec52008-04-04 18:11:11 -07007394 SCHED_CPUMASK_VAR(nodemask, allmasks);
7395 SCHED_CPUMASK_VAR(domainspan, allmasks);
7396 SCHED_CPUMASK_VAR(covered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007397 int j;
7398
Mike Travis7c16ec52008-04-04 18:11:11 -07007399 *nodemask = node_to_cpumask(i);
7400 cpus_clear(*covered);
7401
7402 cpus_and(*nodemask, *nodemask, *cpu_map);
7403 if (cpus_empty(*nodemask)) {
John Hawkesd1b55132005-09-06 15:18:14 -07007404 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007405 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07007406 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007407
Mike Travis4bdbaad32008-04-15 16:35:52 -07007408 sched_domain_node_span(i, domainspan);
Mike Travis7c16ec52008-04-04 18:11:11 -07007409 cpus_and(*domainspan, *domainspan, *cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007410
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007411 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007412 if (!sg) {
7413 printk(KERN_WARNING "Can not alloc domain group for "
7414 "node %d\n", i);
7415 goto error;
7416 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007417 sched_group_nodes[i] = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007418 for_each_cpu_mask(j, *nodemask) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007419 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02007420
John Hawkes9c1cfda2005-09-06 15:18:14 -07007421 sd = &per_cpu(node_domains, j);
7422 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007423 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007424 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007425 sg->cpumask = *nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007426 sg->next = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007427 cpus_or(*covered, *covered, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007428 prev = sg;
7429
7430 for (j = 0; j < MAX_NUMNODES; j++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007431 SCHED_CPUMASK_VAR(notcovered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007432 int n = (i + j) % MAX_NUMNODES;
Mike Travisc5f59f02008-04-04 18:11:10 -07007433 node_to_cpumask_ptr(pnodemask, n);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007434
Mike Travis7c16ec52008-04-04 18:11:11 -07007435 cpus_complement(*notcovered, *covered);
7436 cpus_and(*tmpmask, *notcovered, *cpu_map);
7437 cpus_and(*tmpmask, *tmpmask, *domainspan);
7438 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007439 break;
7440
Mike Travis7c16ec52008-04-04 18:11:11 -07007441 cpus_and(*tmpmask, *tmpmask, *pnodemask);
7442 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007443 continue;
7444
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007445 sg = kmalloc_node(sizeof(struct sched_group),
7446 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007447 if (!sg) {
7448 printk(KERN_WARNING
7449 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007450 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007451 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007452 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007453 sg->cpumask = *tmpmask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007454 sg->next = prev->next;
Mike Travis7c16ec52008-04-04 18:11:11 -07007455 cpus_or(*covered, *covered, *tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007456 prev->next = sg;
7457 prev = sg;
7458 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460#endif
7461
7462 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007463#ifdef CONFIG_SCHED_SMT
7464 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007465 struct sched_domain *sd = &per_cpu(cpu_domains, i);
7466
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007467 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007468 }
7469#endif
7470#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007471 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007472 struct sched_domain *sd = &per_cpu(core_domains, i);
7473
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007474 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007475 }
7476#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007477
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007478 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007479 struct sched_domain *sd = &per_cpu(phys_domains, i);
7480
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007481 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482 }
7483
John Hawkes9c1cfda2005-09-06 15:18:14 -07007484#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08007485 for (i = 0; i < MAX_NUMNODES; i++)
7486 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007487
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007488 if (sd_allnodes) {
7489 struct sched_group *sg;
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007490
Mike Travis7c16ec52008-04-04 18:11:11 -07007491 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg,
7492 tmpmask);
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007493 init_numa_sched_groups_power(sg);
7494 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007495#endif
7496
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007498 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499 struct sched_domain *sd;
7500#ifdef CONFIG_SCHED_SMT
7501 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007502#elif defined(CONFIG_SCHED_MC)
7503 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504#else
7505 sd = &per_cpu(phys_domains, i);
7506#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007507 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007509
Mike Travis7c16ec52008-04-04 18:11:11 -07007510 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007511 return 0;
7512
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007513#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007514error:
Mike Travis7c16ec52008-04-04 18:11:11 -07007515 free_sched_groups(cpu_map, tmpmask);
7516 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007517 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007518#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007519}
Paul Jackson029190c2007-10-18 23:40:20 -07007520
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007521static int build_sched_domains(const cpumask_t *cpu_map)
7522{
7523 return __build_sched_domains(cpu_map, NULL);
7524}
7525
Paul Jackson029190c2007-10-18 23:40:20 -07007526static cpumask_t *doms_cur; /* current sched domains */
7527static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Ingo Molnar4285f5942008-05-16 17:47:14 +02007528static struct sched_domain_attr *dattr_cur;
7529 /* attribues of custom domains in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007530
7531/*
7532 * Special case: If a kmalloc of a doms_cur partition (array of
7533 * cpumask_t) fails, then fallback to a single sched domain,
7534 * as determined by the single cpumask_t fallback_doms.
7535 */
7536static cpumask_t fallback_doms;
7537
Heiko Carstens22e52b02008-03-12 18:31:59 +01007538void __attribute__((weak)) arch_update_cpu_topology(void)
7539{
7540}
7541
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007542/*
Max Krasnyansky5c8e1ed2008-05-29 11:17:01 -07007543 * Free current domain masks.
7544 * Called after all cpus are attached to NULL domain.
7545 */
7546static void free_sched_domains(void)
7547{
7548 ndoms_cur = 0;
7549 if (doms_cur != &fallback_doms)
7550 kfree(doms_cur);
7551 doms_cur = &fallback_doms;
7552}
7553
7554/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007555 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007556 * For now this just excludes isolated cpus, but could be used to
7557 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007558 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007559static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007560{
Milton Miller73785472007-10-24 18:23:48 +02007561 int err;
7562
Heiko Carstens22e52b02008-03-12 18:31:59 +01007563 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007564 ndoms_cur = 1;
7565 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
7566 if (!doms_cur)
7567 doms_cur = &fallback_doms;
7568 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007569 dattr_cur = NULL;
Milton Miller73785472007-10-24 18:23:48 +02007570 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02007571 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007572
7573 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007574}
7575
Mike Travis7c16ec52008-04-04 18:11:11 -07007576static void arch_destroy_sched_domains(const cpumask_t *cpu_map,
7577 cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578{
Mike Travis7c16ec52008-04-04 18:11:11 -07007579 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007580}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007582/*
7583 * Detach sched domains from a group of cpus specified in cpu_map
7584 * These cpus will now be attached to the NULL domain
7585 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08007586static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007587{
Mike Travis7c16ec52008-04-04 18:11:11 -07007588 cpumask_t tmpmask;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007589 int i;
7590
Milton Miller6382bc92007-10-15 17:00:19 +02007591 unregister_sched_domain_sysctl();
7592
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007593 for_each_cpu_mask(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007594 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007595 synchronize_sched();
Mike Travis7c16ec52008-04-04 18:11:11 -07007596 arch_destroy_sched_domains(cpu_map, &tmpmask);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007597}
7598
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007599/* handle null as "default" */
7600static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7601 struct sched_domain_attr *new, int idx_new)
7602{
7603 struct sched_domain_attr tmp;
7604
7605 /* fast path */
7606 if (!new && !cur)
7607 return 1;
7608
7609 tmp = SD_ATTR_INIT;
7610 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7611 new ? (new + idx_new) : &tmp,
7612 sizeof(struct sched_domain_attr));
7613}
7614
Paul Jackson029190c2007-10-18 23:40:20 -07007615/*
7616 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007617 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007618 * doms_new[] to the current sched domain partitioning, doms_cur[].
7619 * It destroys each deleted domain and builds each new domain.
7620 *
7621 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007622 * The masks don't intersect (don't overlap.) We should setup one
7623 * sched domain for each mask. CPUs not in any of the cpumasks will
7624 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007625 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7626 * it as it is.
7627 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007628 * The passed in 'doms_new' should be kmalloc'd. This routine takes
7629 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07007630 * failed the kmalloc call, then it can pass in doms_new == NULL,
7631 * and partition_sched_domains() will fallback to the single partition
7632 * 'fallback_doms'.
7633 *
7634 * Call with hotplug lock held
7635 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007636void partition_sched_domains(int ndoms_new, cpumask_t *doms_new,
7637 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007638{
7639 int i, j;
7640
Heiko Carstens712555e2008-04-28 11:33:07 +02007641 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007642
Milton Miller73785472007-10-24 18:23:48 +02007643 /* always unregister in case we don't destroy any domains */
7644 unregister_sched_domain_sysctl();
7645
Paul Jackson029190c2007-10-18 23:40:20 -07007646 if (doms_new == NULL) {
7647 ndoms_new = 1;
7648 doms_new = &fallback_doms;
7649 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007650 dattr_new = NULL;
Paul Jackson029190c2007-10-18 23:40:20 -07007651 }
7652
7653 /* Destroy deleted domains */
7654 for (i = 0; i < ndoms_cur; i++) {
7655 for (j = 0; j < ndoms_new; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007656 if (cpus_equal(doms_cur[i], doms_new[j])
7657 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007658 goto match1;
7659 }
7660 /* no match - a current sched domain not in new doms_new[] */
7661 detach_destroy_domains(doms_cur + i);
7662match1:
7663 ;
7664 }
7665
7666 /* Build new domains */
7667 for (i = 0; i < ndoms_new; i++) {
7668 for (j = 0; j < ndoms_cur; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007669 if (cpus_equal(doms_new[i], doms_cur[j])
7670 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007671 goto match2;
7672 }
7673 /* no match - add a new doms_new */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007674 __build_sched_domains(doms_new + i,
7675 dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007676match2:
7677 ;
7678 }
7679
7680 /* Remember the new sched domains */
7681 if (doms_cur != &fallback_doms)
7682 kfree(doms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007683 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007684 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007685 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007686 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007687
7688 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007689
Heiko Carstens712555e2008-04-28 11:33:07 +02007690 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07007691}
7692
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007693#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007694int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007695{
7696 int err;
7697
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007698 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02007699 mutex_lock(&sched_domains_mutex);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007700 detach_destroy_domains(&cpu_online_map);
Max Krasnyansky5c8e1ed2008-05-29 11:17:01 -07007701 free_sched_domains();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007702 err = arch_init_sched_domains(&cpu_online_map);
Heiko Carstens712555e2008-04-28 11:33:07 +02007703 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007704 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007705
7706 return err;
7707}
7708
7709static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7710{
7711 int ret;
7712
7713 if (buf[0] != '0' && buf[0] != '1')
7714 return -EINVAL;
7715
7716 if (smt)
7717 sched_smt_power_savings = (buf[0] == '1');
7718 else
7719 sched_mc_power_savings = (buf[0] == '1');
7720
7721 ret = arch_reinit_sched_domains();
7722
7723 return ret ? ret : count;
7724}
7725
Adrian Bunk6707de002007-08-12 18:08:19 +02007726#ifdef CONFIG_SCHED_MC
7727static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
7728{
7729 return sprintf(page, "%u\n", sched_mc_power_savings);
7730}
7731static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
7732 const char *buf, size_t count)
7733{
7734 return sched_power_savings_store(buf, count, 0);
7735}
7736static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
7737 sched_mc_power_savings_store);
7738#endif
7739
7740#ifdef CONFIG_SCHED_SMT
7741static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
7742{
7743 return sprintf(page, "%u\n", sched_smt_power_savings);
7744}
7745static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
7746 const char *buf, size_t count)
7747{
7748 return sched_power_savings_store(buf, count, 1);
7749}
7750static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
7751 sched_smt_power_savings_store);
7752#endif
7753
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007754int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7755{
7756 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007757
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007758#ifdef CONFIG_SCHED_SMT
7759 if (smt_capable())
7760 err = sysfs_create_file(&cls->kset.kobj,
7761 &attr_sched_smt_power_savings.attr);
7762#endif
7763#ifdef CONFIG_SCHED_MC
7764 if (!err && mc_capable())
7765 err = sysfs_create_file(&cls->kset.kobj,
7766 &attr_sched_mc_power_savings.attr);
7767#endif
7768 return err;
7769}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007770#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007771
Linus Torvalds1da177e2005-04-16 15:20:36 -07007772/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007773 * Force a reinitialization of the sched domains hierarchy. The domains
Linus Torvalds1da177e2005-04-16 15:20:36 -07007774 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07007775 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07007776 * which will prevent rebalancing while the sched domains are recalculated.
7777 */
7778static int update_sched_domains(struct notifier_block *nfb,
7779 unsigned long action, void *hcpu)
7780{
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007781 int cpu = (int)(long)hcpu;
7782
Linus Torvalds1da177e2005-04-16 15:20:36 -07007783 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007784 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007785 case CPU_DOWN_PREPARE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007786 disable_runtime(cpu_rq(cpu));
7787 /* fall-through */
7788 case CPU_UP_PREPARE:
7789 case CPU_UP_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007790 detach_destroy_domains(&cpu_online_map);
Max Krasnyansky5c8e1ed2008-05-29 11:17:01 -07007791 free_sched_domains();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007792 return NOTIFY_OK;
7793
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007794
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007796 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007797 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007798 case CPU_ONLINE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02007799 enable_runtime(cpu_rq(cpu));
7800 /* fall-through */
7801 case CPU_UP_CANCELED:
7802 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007804 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007805 /*
7806 * Fall through and re-initialise the domains.
7807 */
7808 break;
7809 default:
7810 return NOTIFY_DONE;
7811 }
7812
Max Krasnyansky5c8e1ed2008-05-29 11:17:01 -07007813#ifndef CONFIG_CPUSETS
7814 /*
7815 * Create default domain partitioning if cpusets are disabled.
7816 * Otherwise we let cpusets rebuild the domains based on the
7817 * current setup.
7818 */
7819
Linus Torvalds1da177e2005-04-16 15:20:36 -07007820 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007821 arch_init_sched_domains(&cpu_online_map);
Max Krasnyansky5c8e1ed2008-05-29 11:17:01 -07007822#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007823
7824 return NOTIFY_OK;
7825}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007826
7827void __init sched_init_smp(void)
7828{
Nick Piggin5c1e1762006-10-03 01:14:04 -07007829 cpumask_t non_isolated_cpus;
7830
Mike Travis434d53b2008-04-04 18:11:04 -07007831#if defined(CONFIG_NUMA)
7832 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
7833 GFP_KERNEL);
7834 BUG_ON(sched_group_nodes_bycpu == NULL);
7835#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007836 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02007837 mutex_lock(&sched_domains_mutex);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007838 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08007839 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007840 if (cpus_empty(non_isolated_cpus))
7841 cpu_set(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02007842 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007843 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007844 /* XXX: Theoretical race here - CPU may be hotplugged now */
7845 hotcpu_notifier(update_sched_domains, 0);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02007846 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07007847
7848 /* Move init over to a non-isolated CPU */
Mike Travis7c16ec52008-04-04 18:11:11 -07007849 if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07007850 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007851 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007852}
7853#else
7854void __init sched_init_smp(void)
7855{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007856 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007857}
7858#endif /* CONFIG_SMP */
7859
7860int in_sched_functions(unsigned long addr)
7861{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007862 return in_lock_functions(addr) ||
7863 (addr >= (unsigned long)__sched_text_start
7864 && addr < (unsigned long)__sched_text_end);
7865}
7866
Alexey Dobriyana9957442007-10-15 17:00:13 +02007867static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007868{
7869 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02007870 INIT_LIST_HEAD(&cfs_rq->tasks);
Ingo Molnardd41f592007-07-09 18:51:59 +02007871#ifdef CONFIG_FAIR_GROUP_SCHED
7872 cfs_rq->rq = rq;
7873#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007874 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02007875}
7876
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007877static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7878{
7879 struct rt_prio_array *array;
7880 int i;
7881
7882 array = &rt_rq->active;
7883 for (i = 0; i < MAX_RT_PRIO; i++) {
Dmitry Adamushko20b63312008-06-11 00:58:30 +02007884 INIT_LIST_HEAD(array->queue + i);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007885 __clear_bit(i, array->bitmap);
7886 }
7887 /* delimiter for bitsearch: */
7888 __set_bit(MAX_RT_PRIO, array->bitmap);
7889
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007890#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007891 rt_rq->highest_prio = MAX_RT_PRIO;
7892#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007893#ifdef CONFIG_SMP
7894 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007895 rt_rq->overloaded = 0;
7896#endif
7897
7898 rt_rq->rt_time = 0;
7899 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007900 rt_rq->rt_runtime = 0;
7901 spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007902
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007903#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01007904 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007905 rt_rq->rq = rq;
7906#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007907}
7908
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007909#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007910static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
7911 struct sched_entity *se, int cpu, int add,
7912 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007913{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007914 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007915 tg->cfs_rq[cpu] = cfs_rq;
7916 init_cfs_rq(cfs_rq, rq);
7917 cfs_rq->tg = tg;
7918 if (add)
7919 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7920
7921 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02007922 /* se could be NULL for init_task_group */
7923 if (!se)
7924 return;
7925
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007926 if (!parent)
7927 se->cfs_rq = &rq->cfs;
7928 else
7929 se->cfs_rq = parent->my_q;
7930
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007931 se->my_q = cfs_rq;
7932 se->load.weight = tg->shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02007933 se->load.inv_weight = 0;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007934 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007935}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007936#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007937
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007938#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007939static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
7940 struct sched_rt_entity *rt_se, int cpu, int add,
7941 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007942{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007943 struct rq *rq = cpu_rq(cpu);
7944
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007945 tg->rt_rq[cpu] = rt_rq;
7946 init_rt_rq(rt_rq, rq);
7947 rt_rq->tg = tg;
7948 rt_rq->rt_se = rt_se;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02007949 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007950 if (add)
7951 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
7952
7953 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02007954 if (!rt_se)
7955 return;
7956
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007957 if (!parent)
7958 rt_se->rt_rq = &rq->rt;
7959 else
7960 rt_se->rt_rq = parent->my_q;
7961
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007962 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007963 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007964 INIT_LIST_HEAD(&rt_se->run_list);
7965}
7966#endif
7967
Linus Torvalds1da177e2005-04-16 15:20:36 -07007968void __init sched_init(void)
7969{
Ingo Molnardd41f592007-07-09 18:51:59 +02007970 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07007971 unsigned long alloc_size = 0, ptr;
7972
7973#ifdef CONFIG_FAIR_GROUP_SCHED
7974 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7975#endif
7976#ifdef CONFIG_RT_GROUP_SCHED
7977 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
7978#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007979#ifdef CONFIG_USER_SCHED
7980 alloc_size *= 2;
7981#endif
Mike Travis434d53b2008-04-04 18:11:04 -07007982 /*
7983 * As sched_init() is called before page_alloc is setup,
7984 * we use alloc_bootmem().
7985 */
7986 if (alloc_size) {
David Miller5a9d3222008-04-24 20:46:20 -07007987 ptr = (unsigned long)alloc_bootmem(alloc_size);
Mike Travis434d53b2008-04-04 18:11:04 -07007988
7989#ifdef CONFIG_FAIR_GROUP_SCHED
7990 init_task_group.se = (struct sched_entity **)ptr;
7991 ptr += nr_cpu_ids * sizeof(void **);
7992
7993 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
7994 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007995
7996#ifdef CONFIG_USER_SCHED
7997 root_task_group.se = (struct sched_entity **)ptr;
7998 ptr += nr_cpu_ids * sizeof(void **);
7999
8000 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
8001 ptr += nr_cpu_ids * sizeof(void **);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008002#endif /* CONFIG_USER_SCHED */
8003#endif /* CONFIG_FAIR_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07008004#ifdef CONFIG_RT_GROUP_SCHED
8005 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
8006 ptr += nr_cpu_ids * sizeof(void **);
8007
8008 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008009 ptr += nr_cpu_ids * sizeof(void **);
8010
8011#ifdef CONFIG_USER_SCHED
8012 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
8013 ptr += nr_cpu_ids * sizeof(void **);
8014
8015 root_task_group.rt_rq = (struct rt_rq **)ptr;
8016 ptr += nr_cpu_ids * sizeof(void **);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008017#endif /* CONFIG_USER_SCHED */
8018#endif /* CONFIG_RT_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07008019 }
Ingo Molnardd41f592007-07-09 18:51:59 +02008020
Gregory Haskins57d885f2008-01-25 21:08:18 +01008021#ifdef CONFIG_SMP
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008022 init_aggregate();
Gregory Haskins57d885f2008-01-25 21:08:18 +01008023 init_defrootdomain();
8024#endif
8025
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008026 init_rt_bandwidth(&def_rt_bandwidth,
8027 global_rt_period(), global_rt_runtime());
8028
8029#ifdef CONFIG_RT_GROUP_SCHED
8030 init_rt_bandwidth(&init_task_group.rt_bandwidth,
8031 global_rt_period(), global_rt_runtime());
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008032#ifdef CONFIG_USER_SCHED
8033 init_rt_bandwidth(&root_task_group.rt_bandwidth,
8034 global_rt_period(), RUNTIME_INF);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008035#endif /* CONFIG_USER_SCHED */
8036#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008037
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008038#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008039 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008040 INIT_LIST_HEAD(&init_task_group.children);
8041
8042#ifdef CONFIG_USER_SCHED
8043 INIT_LIST_HEAD(&root_task_group.children);
8044 init_task_group.parent = &root_task_group;
8045 list_add(&init_task_group.siblings, &root_task_group.children);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008046#endif /* CONFIG_USER_SCHED */
8047#endif /* CONFIG_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008048
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08008049 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07008050 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008051
8052 rq = cpu_rq(i);
8053 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07008054 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07008055 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008056 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008057 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008058#ifdef CONFIG_FAIR_GROUP_SCHED
8059 init_task_group.shares = init_task_group_load;
8060 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008061#ifdef CONFIG_CGROUP_SCHED
8062 /*
8063 * How much cpu bandwidth does init_task_group get?
8064 *
8065 * In case of task-groups formed thr' the cgroup filesystem, it
8066 * gets 100% of the cpu resources in the system. This overall
8067 * system cpu resource is divided among the tasks of
8068 * init_task_group and its child task-groups in a fair manner,
8069 * based on each entity's (task or task-group's) weight
8070 * (se->load.weight).
8071 *
8072 * In other words, if init_task_group has 10 tasks of weight
8073 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8074 * then A0's share of the cpu resource is:
8075 *
8076 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
8077 *
8078 * We achieve this by letting init_task_group's tasks sit
8079 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
8080 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008081 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008082#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008083 root_task_group.shares = NICE_0_LOAD;
8084 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008085 /*
8086 * In case of task-groups formed thr' the user id of tasks,
8087 * init_task_group represents tasks belonging to root user.
8088 * Hence it forms a sibling of all subsequent groups formed.
8089 * In this case, init_task_group gets only a fraction of overall
8090 * system cpu resource, based on the weight assigned to root
8091 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
8092 * by letting tasks of init_task_group sit in a separate cfs_rq
8093 * (init_cfs_rq) and having one entity represent this group of
8094 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
8095 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008096 init_tg_cfs_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008097 &per_cpu(init_cfs_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008098 &per_cpu(init_sched_entity, i), i, 1,
8099 root_task_group.se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008100
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008101#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02008102#endif /* CONFIG_FAIR_GROUP_SCHED */
8103
8104 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008105#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008106 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008107#ifdef CONFIG_CGROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008108 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008109#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008110 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008111 init_tg_rt_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008112 &per_cpu(init_rt_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008113 &per_cpu(init_sched_rt_entity, i), i, 1,
8114 root_task_group.rt_se[i]);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008115#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008116#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008117
Ingo Molnardd41f592007-07-09 18:51:59 +02008118 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
8119 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008120#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07008121 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01008122 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008123 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008124 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008125 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07008126 rq->cpu = i;
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04008127 rq->online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008128 rq->migration_thread = NULL;
8129 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01008130 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008131#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01008132 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008133 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008134 }
8135
Peter Williams2dd73a42006-06-27 02:54:34 -07008136 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008137
Avi Kivitye107be32007-07-26 13:40:43 +02008138#ifdef CONFIG_PREEMPT_NOTIFIERS
8139 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
8140#endif
8141
Christoph Lameterc9819f42006-12-10 02:20:25 -08008142#ifdef CONFIG_SMP
8143 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
8144#endif
8145
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008146#ifdef CONFIG_RT_MUTEXES
8147 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
8148#endif
8149
Linus Torvalds1da177e2005-04-16 15:20:36 -07008150 /*
8151 * The boot idle thread does lazy MMU switching as well:
8152 */
8153 atomic_inc(&init_mm.mm_count);
8154 enter_lazy_tlb(&init_mm, current);
8155
8156 /*
8157 * Make us the idle thread. Technically, schedule() should not be
8158 * called from this thread, however somewhere below it might be,
8159 * but because we are the idle thread, we just pick up running again
8160 * when this runqueue becomes "idle".
8161 */
8162 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02008163 /*
8164 * During early bootup we pretend to be a normal task:
8165 */
8166 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01008167
8168 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008169}
8170
8171#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
8172void __might_sleep(char *file, int line)
8173{
Ingo Molnar48f24c42006-07-03 00:25:40 -07008174#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07008175 static unsigned long prev_jiffy; /* ratelimiting */
8176
8177 if ((in_atomic() || irqs_disabled()) &&
8178 system_state == SYSTEM_RUNNING && !oops_in_progress) {
8179 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
8180 return;
8181 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08008182 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07008183 " context at %s:%d\n", file, line);
8184 printk("in_atomic():%d, irqs_disabled():%d\n",
8185 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08008186 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08008187 if (irqs_disabled())
8188 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008189 dump_stack();
8190 }
8191#endif
8192}
8193EXPORT_SYMBOL(__might_sleep);
8194#endif
8195
8196#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008197static void normalize_task(struct rq *rq, struct task_struct *p)
8198{
8199 int on_rq;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02008200
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008201 update_rq_clock(rq);
8202 on_rq = p->se.on_rq;
8203 if (on_rq)
8204 deactivate_task(rq, p, 0);
8205 __setscheduler(rq, p, SCHED_NORMAL, 0);
8206 if (on_rq) {
8207 activate_task(rq, p, 0);
8208 resched_task(rq->curr);
8209 }
8210}
8211
Linus Torvalds1da177e2005-04-16 15:20:36 -07008212void normalize_rt_tasks(void)
8213{
Ingo Molnara0f98a12007-06-17 18:37:45 +02008214 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008215 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07008216 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008217
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008218 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008219 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02008220 /*
8221 * Only normalize user tasks:
8222 */
8223 if (!p->mm)
8224 continue;
8225
Ingo Molnardd41f592007-07-09 18:51:59 +02008226 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008227#ifdef CONFIG_SCHEDSTATS
8228 p->se.wait_start = 0;
8229 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008230 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008231#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02008232
8233 if (!rt_task(p)) {
8234 /*
8235 * Renice negative nice level userspace
8236 * tasks back to 0:
8237 */
8238 if (TASK_NICE(p) < 0 && p->mm)
8239 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008240 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02008241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008242
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008243 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07008244 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008245
Ingo Molnar178be792007-10-15 17:00:18 +02008246 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008247
Ingo Molnarb29739f2006-06-27 02:54:51 -07008248 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008249 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008250 } while_each_thread(g, p);
8251
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008252 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008253}
8254
8255#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07008256
8257#ifdef CONFIG_IA64
8258/*
8259 * These functions are only useful for the IA64 MCA handling.
8260 *
8261 * They can only be called when the whole system has been
8262 * stopped - every CPU needs to be quiescent, and no scheduling
8263 * activity can take place. Using them for anything else would
8264 * be a serious bug, and as a result, they aren't even visible
8265 * under any other configuration.
8266 */
8267
8268/**
8269 * curr_task - return the current task for a given cpu.
8270 * @cpu: the processor in question.
8271 *
8272 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8273 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008274struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008275{
8276 return cpu_curr(cpu);
8277}
8278
8279/**
8280 * set_curr_task - set the current task for a given cpu.
8281 * @cpu: the processor in question.
8282 * @p: the task pointer to set.
8283 *
8284 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008285 * are serviced on a separate stack. It allows the architecture to switch the
8286 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07008287 * must be called with all CPU's synchronized, and interrupts disabled, the
8288 * and caller must save the original value of the current task (see
8289 * curr_task() above) and restore that value before reenabling interrupts and
8290 * re-starting the system.
8291 *
8292 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8293 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008294void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008295{
8296 cpu_curr(cpu) = p;
8297}
8298
8299#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008300
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008301#ifdef CONFIG_FAIR_GROUP_SCHED
8302static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008303{
8304 int i;
8305
8306 for_each_possible_cpu(i) {
8307 if (tg->cfs_rq)
8308 kfree(tg->cfs_rq[i]);
8309 if (tg->se)
8310 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008311 }
8312
8313 kfree(tg->cfs_rq);
8314 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008315}
8316
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008317static
8318int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008319{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008320 struct cfs_rq *cfs_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008321 struct sched_entity *se, *parent_se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008322 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008323 int i;
8324
Mike Travis434d53b2008-04-04 18:11:04 -07008325 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008326 if (!tg->cfs_rq)
8327 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008328 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008329 if (!tg->se)
8330 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008331
8332 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008333
8334 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008335 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008336
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008337 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
8338 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008339 if (!cfs_rq)
8340 goto err;
8341
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008342 se = kmalloc_node(sizeof(struct sched_entity),
8343 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008344 if (!se)
8345 goto err;
8346
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008347 parent_se = parent ? parent->se[i] : NULL;
8348 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008349 }
8350
8351 return 1;
8352
8353 err:
8354 return 0;
8355}
8356
8357static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8358{
8359 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
8360 &cpu_rq(cpu)->leaf_cfs_rq_list);
8361}
8362
8363static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8364{
8365 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
8366}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008367#else /* !CONFG_FAIR_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008368static inline void free_fair_sched_group(struct task_group *tg)
8369{
8370}
8371
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008372static inline
8373int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008374{
8375 return 1;
8376}
8377
8378static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8379{
8380}
8381
8382static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8383{
8384}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008385#endif /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008386
8387#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008388static void free_rt_sched_group(struct task_group *tg)
8389{
8390 int i;
8391
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008392 destroy_rt_bandwidth(&tg->rt_bandwidth);
8393
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008394 for_each_possible_cpu(i) {
8395 if (tg->rt_rq)
8396 kfree(tg->rt_rq[i]);
8397 if (tg->rt_se)
8398 kfree(tg->rt_se[i]);
8399 }
8400
8401 kfree(tg->rt_rq);
8402 kfree(tg->rt_se);
8403}
8404
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008405static
8406int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008407{
8408 struct rt_rq *rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008409 struct sched_rt_entity *rt_se, *parent_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008410 struct rq *rq;
8411 int i;
8412
Mike Travis434d53b2008-04-04 18:11:04 -07008413 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008414 if (!tg->rt_rq)
8415 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008416 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008417 if (!tg->rt_se)
8418 goto err;
8419
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008420 init_rt_bandwidth(&tg->rt_bandwidth,
8421 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008422
8423 for_each_possible_cpu(i) {
8424 rq = cpu_rq(i);
8425
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008426 rt_rq = kmalloc_node(sizeof(struct rt_rq),
8427 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8428 if (!rt_rq)
8429 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008430
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008431 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
8432 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8433 if (!rt_se)
8434 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008435
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008436 parent_se = parent ? parent->rt_se[i] : NULL;
8437 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent_se);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008438 }
8439
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008440 return 1;
8441
8442 err:
8443 return 0;
8444}
8445
8446static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8447{
8448 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
8449 &cpu_rq(cpu)->leaf_rt_rq_list);
8450}
8451
8452static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8453{
8454 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
8455}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008456#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008457static inline void free_rt_sched_group(struct task_group *tg)
8458{
8459}
8460
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008461static inline
8462int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008463{
8464 return 1;
8465}
8466
8467static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8468{
8469}
8470
8471static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8472{
8473}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008474#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008475
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008476#ifdef CONFIG_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008477static void free_sched_group(struct task_group *tg)
8478{
8479 free_fair_sched_group(tg);
8480 free_rt_sched_group(tg);
8481 kfree(tg);
8482}
8483
8484/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008485struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008486{
8487 struct task_group *tg;
8488 unsigned long flags;
8489 int i;
8490
8491 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8492 if (!tg)
8493 return ERR_PTR(-ENOMEM);
8494
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008495 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008496 goto err;
8497
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008498 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008499 goto err;
8500
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008501 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008502 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008503 register_fair_sched_group(tg, i);
8504 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008505 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008506 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008507
8508 WARN_ON(!parent); /* root should already exist */
8509
8510 tg->parent = parent;
8511 list_add_rcu(&tg->siblings, &parent->children);
8512 INIT_LIST_HEAD(&tg->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008513 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008514
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008515 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008516
8517err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008518 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008519 return ERR_PTR(-ENOMEM);
8520}
8521
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008522/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008523static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008524{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008525 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008526 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008527}
8528
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008529/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008530void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008531{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008532 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008533 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008534
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008535 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008536 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008537 unregister_fair_sched_group(tg, i);
8538 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008539 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008540 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008541 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008542 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008543
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008544 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008545 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008546}
8547
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008548/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008549 * The caller of this function should have put the task in its new group
8550 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8551 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008552 */
8553void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008554{
8555 int on_rq, running;
8556 unsigned long flags;
8557 struct rq *rq;
8558
8559 rq = task_rq_lock(tsk, &flags);
8560
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008561 update_rq_clock(rq);
8562
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008563 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008564 on_rq = tsk->se.on_rq;
8565
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008566 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008567 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008568 if (unlikely(running))
8569 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008570
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008571 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008572
Peter Zijlstra810b3812008-02-29 15:21:01 -05008573#ifdef CONFIG_FAIR_GROUP_SCHED
8574 if (tsk->sched_class->moved_group)
8575 tsk->sched_class->moved_group(tsk);
8576#endif
8577
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008578 if (unlikely(running))
8579 tsk->sched_class->set_curr_task(rq);
8580 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02008581 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008582
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008583 task_rq_unlock(rq, &flags);
8584}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008585#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008586
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008587#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008588static void __set_se_shares(struct sched_entity *se, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008589{
8590 struct cfs_rq *cfs_rq = se->cfs_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008591 int on_rq;
8592
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008593 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008594 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008595 dequeue_entity(cfs_rq, se, 0);
8596
8597 se->load.weight = shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008598 se->load.inv_weight = 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008599
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008600 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008601 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008602}
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008603
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008604static void set_se_shares(struct sched_entity *se, unsigned long shares)
8605{
8606 struct cfs_rq *cfs_rq = se->cfs_rq;
8607 struct rq *rq = cfs_rq->rq;
8608 unsigned long flags;
8609
8610 spin_lock_irqsave(&rq->lock, flags);
8611 __set_se_shares(se, shares);
8612 spin_unlock_irqrestore(&rq->lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008613}
8614
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008615static DEFINE_MUTEX(shares_mutex);
8616
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008617int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008618{
8619 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008620 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01008621
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008622 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008623 * We can't change the weight of the root cgroup.
8624 */
8625 if (!tg->se[0])
8626 return -EINVAL;
8627
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008628 if (shares < MIN_SHARES)
8629 shares = MIN_SHARES;
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008630 else if (shares > MAX_SHARES)
8631 shares = MAX_SHARES;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008632
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008633 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008634 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008635 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008636
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008637 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008638 for_each_possible_cpu(i)
8639 unregister_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008640 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008641 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008642
8643 /* wait for any ongoing reference to this group to finish */
8644 synchronize_sched();
8645
8646 /*
8647 * Now we are free to modify the group's share on each cpu
8648 * w/o tripping rebalance_share or load_balance_fair.
8649 */
8650 tg->shares = shares;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008651 for_each_possible_cpu(i) {
8652 /*
8653 * force a rebalance
8654 */
8655 cfs_rq_set_shares(tg->cfs_rq[i], 0);
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008656 set_se_shares(tg->se[i], shares);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008657 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008658
8659 /*
8660 * Enable load balance activity on this group, by inserting it back on
8661 * each cpu's rq->leaf_cfs_rq_list.
8662 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008663 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008664 for_each_possible_cpu(i)
8665 register_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008666 list_add_rcu(&tg->siblings, &tg->parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008667 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008668done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008669 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008670 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008671}
8672
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008673unsigned long sched_group_shares(struct task_group *tg)
8674{
8675 return tg->shares;
8676}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008677#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008678
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008679#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008680/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008681 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008682 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008683static DEFINE_MUTEX(rt_constraints_mutex);
8684
8685static unsigned long to_ratio(u64 period, u64 runtime)
8686{
8687 if (runtime == RUNTIME_INF)
8688 return 1ULL << 16;
8689
Roman Zippel6f6d6a12008-05-01 04:34:28 -07008690 return div64_u64(runtime << 16, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008691}
8692
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008693#ifdef CONFIG_CGROUP_SCHED
8694static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
8695{
Peter Zijlstra10b612f2008-06-19 14:22:27 +02008696 struct task_group *tgi, *parent = tg->parent;
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008697 unsigned long total = 0;
8698
8699 if (!parent) {
8700 if (global_rt_period() < period)
8701 return 0;
8702
8703 return to_ratio(period, runtime) <
8704 to_ratio(global_rt_period(), global_rt_runtime());
8705 }
8706
8707 if (ktime_to_ns(parent->rt_bandwidth.rt_period) < period)
8708 return 0;
8709
8710 rcu_read_lock();
8711 list_for_each_entry_rcu(tgi, &parent->children, siblings) {
8712 if (tgi == tg)
8713 continue;
8714
8715 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8716 tgi->rt_bandwidth.rt_runtime);
8717 }
8718 rcu_read_unlock();
8719
Peter Zijlstra10b612f2008-06-19 14:22:27 +02008720 return total + to_ratio(period, runtime) <=
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008721 to_ratio(ktime_to_ns(parent->rt_bandwidth.rt_period),
8722 parent->rt_bandwidth.rt_runtime);
8723}
8724#elif defined CONFIG_USER_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008725static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008726{
8727 struct task_group *tgi;
8728 unsigned long total = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008729 unsigned long global_ratio =
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008730 to_ratio(global_rt_period(), global_rt_runtime());
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008731
8732 rcu_read_lock();
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008733 list_for_each_entry_rcu(tgi, &task_groups, list) {
8734 if (tgi == tg)
8735 continue;
8736
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008737 total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
8738 tgi->rt_bandwidth.rt_runtime);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008739 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008740 rcu_read_unlock();
8741
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008742 return total + to_ratio(period, runtime) < global_ratio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008743}
Peter Zijlstrab40b2e82008-04-19 19:45:00 +02008744#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008745
Dhaval Giani521f1a242008-02-28 15:21:56 +05308746/* Must be called with tasklist_lock held */
8747static inline int tg_has_rt_tasks(struct task_group *tg)
8748{
8749 struct task_struct *g, *p;
8750 do_each_thread(g, p) {
8751 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
8752 return 1;
8753 } while_each_thread(g, p);
8754 return 0;
8755}
8756
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008757static int tg_set_bandwidth(struct task_group *tg,
8758 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008759{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008760 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008761
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008762 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05308763 read_lock(&tasklist_lock);
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008764 if (rt_runtime == 0 && tg_has_rt_tasks(tg)) {
Dhaval Giani521f1a242008-02-28 15:21:56 +05308765 err = -EBUSY;
8766 goto unlock;
8767 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008768 if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
8769 err = -EINVAL;
8770 goto unlock;
8771 }
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008772
8773 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008774 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
8775 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008776
8777 for_each_possible_cpu(i) {
8778 struct rt_rq *rt_rq = tg->rt_rq[i];
8779
8780 spin_lock(&rt_rq->rt_runtime_lock);
8781 rt_rq->rt_runtime = rt_runtime;
8782 spin_unlock(&rt_rq->rt_runtime_lock);
8783 }
8784 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008785 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05308786 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008787 mutex_unlock(&rt_constraints_mutex);
8788
8789 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008790}
8791
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008792int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
8793{
8794 u64 rt_runtime, rt_period;
8795
8796 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8797 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
8798 if (rt_runtime_us < 0)
8799 rt_runtime = RUNTIME_INF;
8800
8801 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8802}
8803
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008804long sched_group_rt_runtime(struct task_group *tg)
8805{
8806 u64 rt_runtime_us;
8807
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008808 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008809 return -1;
8810
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008811 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008812 do_div(rt_runtime_us, NSEC_PER_USEC);
8813 return rt_runtime_us;
8814}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008815
8816int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
8817{
8818 u64 rt_runtime, rt_period;
8819
8820 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
8821 rt_runtime = tg->rt_bandwidth.rt_runtime;
8822
8823 return tg_set_bandwidth(tg, rt_period, rt_runtime);
8824}
8825
8826long sched_group_rt_period(struct task_group *tg)
8827{
8828 u64 rt_period_us;
8829
8830 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
8831 do_div(rt_period_us, NSEC_PER_USEC);
8832 return rt_period_us;
8833}
8834
8835static int sched_rt_global_constraints(void)
8836{
Peter Zijlstra10b612f2008-06-19 14:22:27 +02008837 struct task_group *tg = &root_task_group;
8838 u64 rt_runtime, rt_period;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008839 int ret = 0;
8840
Peter Zijlstra10b612f2008-06-19 14:22:27 +02008841 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8842 rt_runtime = tg->rt_bandwidth.rt_runtime;
8843
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008844 mutex_lock(&rt_constraints_mutex);
Peter Zijlstra10b612f2008-06-19 14:22:27 +02008845 if (!__rt_schedulable(tg, rt_period, rt_runtime))
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008846 ret = -EINVAL;
8847 mutex_unlock(&rt_constraints_mutex);
8848
8849 return ret;
8850}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008851#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008852static int sched_rt_global_constraints(void)
8853{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008854 unsigned long flags;
8855 int i;
8856
8857 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
8858 for_each_possible_cpu(i) {
8859 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
8860
8861 spin_lock(&rt_rq->rt_runtime_lock);
8862 rt_rq->rt_runtime = global_rt_runtime();
8863 spin_unlock(&rt_rq->rt_runtime_lock);
8864 }
8865 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
8866
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008867 return 0;
8868}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008869#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008870
8871int sched_rt_handler(struct ctl_table *table, int write,
8872 struct file *filp, void __user *buffer, size_t *lenp,
8873 loff_t *ppos)
8874{
8875 int ret;
8876 int old_period, old_runtime;
8877 static DEFINE_MUTEX(mutex);
8878
8879 mutex_lock(&mutex);
8880 old_period = sysctl_sched_rt_period;
8881 old_runtime = sysctl_sched_rt_runtime;
8882
8883 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
8884
8885 if (!ret && write) {
8886 ret = sched_rt_global_constraints();
8887 if (ret) {
8888 sysctl_sched_rt_period = old_period;
8889 sysctl_sched_rt_runtime = old_runtime;
8890 } else {
8891 def_rt_bandwidth.rt_runtime = global_rt_runtime();
8892 def_rt_bandwidth.rt_period =
8893 ns_to_ktime(global_rt_period());
8894 }
8895 }
8896 mutex_unlock(&mutex);
8897
8898 return ret;
8899}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008900
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008901#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008902
8903/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008904static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008905{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008906 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
8907 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008908}
8909
8910static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02008911cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008912{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008913 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008914
Paul Menage2b01dfe2007-10-24 18:23:50 +02008915 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008916 /* This is early initialization for the top cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008917 init_task_group.css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008918 return &init_task_group.css;
8919 }
8920
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008921 parent = cgroup_tg(cgrp->parent);
8922 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008923 if (IS_ERR(tg))
8924 return ERR_PTR(-ENOMEM);
8925
8926 /* Bind the cgroup to task_group object we just created */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008927 tg->css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008928
8929 return &tg->css;
8930}
8931
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008932static void
8933cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008934{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008935 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008936
8937 sched_destroy_group(tg);
8938}
8939
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008940static int
8941cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
8942 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008943{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008944#ifdef CONFIG_RT_GROUP_SCHED
8945 /* Don't accept realtime tasks when there is no way for them to run */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008946 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008947 return -EINVAL;
8948#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008949 /* We don't support RT-tasks being in separate groups */
8950 if (tsk->sched_class != &fair_sched_class)
8951 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008952#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008953
8954 return 0;
8955}
8956
8957static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02008958cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008959 struct cgroup *old_cont, struct task_struct *tsk)
8960{
8961 sched_move_task(tsk);
8962}
8963
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008964#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagef4c753b2008-04-29 00:59:56 -07008965static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
Paul Menage2b01dfe2007-10-24 18:23:50 +02008966 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008967{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008968 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008969}
8970
Paul Menagef4c753b2008-04-29 00:59:56 -07008971static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008972{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008973 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008974
8975 return (u64) tg->shares;
8976}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008977#endif /* CONFIG_FAIR_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008978
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008979#ifdef CONFIG_RT_GROUP_SCHED
Mirco Tischler0c708142008-05-14 16:05:46 -07008980static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Paul Menage06ecb272008-04-29 01:00:06 -07008981 s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008982{
Paul Menage06ecb272008-04-29 01:00:06 -07008983 return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008984}
8985
Paul Menage06ecb272008-04-29 01:00:06 -07008986static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008987{
Paul Menage06ecb272008-04-29 01:00:06 -07008988 return sched_group_rt_runtime(cgroup_tg(cgrp));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008989}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008990
8991static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
8992 u64 rt_period_us)
8993{
8994 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
8995}
8996
8997static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
8998{
8999 return sched_group_rt_period(cgroup_tg(cgrp));
9000}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009001#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009002
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009003static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009004#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009005 {
9006 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07009007 .read_u64 = cpu_shares_read_u64,
9008 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009009 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009010#endif
9011#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009012 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009013 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07009014 .read_s64 = cpu_rt_runtime_read,
9015 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009016 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009017 {
9018 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07009019 .read_u64 = cpu_rt_period_read_uint,
9020 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009021 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009022#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009023};
9024
9025static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
9026{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009027 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009028}
9029
9030struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01009031 .name = "cpu",
9032 .create = cpu_cgroup_create,
9033 .destroy = cpu_cgroup_destroy,
9034 .can_attach = cpu_cgroup_can_attach,
9035 .attach = cpu_cgroup_attach,
9036 .populate = cpu_cgroup_populate,
9037 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009038 .early_init = 1,
9039};
9040
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009041#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009042
9043#ifdef CONFIG_CGROUP_CPUACCT
9044
9045/*
9046 * CPU accounting code for task groups.
9047 *
9048 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
9049 * (balbir@in.ibm.com).
9050 */
9051
9052/* track cpu usage of a group of tasks */
9053struct cpuacct {
9054 struct cgroup_subsys_state css;
9055 /* cpuusage holds pointer to a u64-type object on every cpu */
9056 u64 *cpuusage;
9057};
9058
9059struct cgroup_subsys cpuacct_subsys;
9060
9061/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309062static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009063{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309064 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009065 struct cpuacct, css);
9066}
9067
9068/* return cpu accounting group to which this task belongs */
9069static inline struct cpuacct *task_ca(struct task_struct *tsk)
9070{
9071 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
9072 struct cpuacct, css);
9073}
9074
9075/* create a new cpu accounting group */
9076static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05309077 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009078{
9079 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
9080
9081 if (!ca)
9082 return ERR_PTR(-ENOMEM);
9083
9084 ca->cpuusage = alloc_percpu(u64);
9085 if (!ca->cpuusage) {
9086 kfree(ca);
9087 return ERR_PTR(-ENOMEM);
9088 }
9089
9090 return &ca->css;
9091}
9092
9093/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009094static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05309095cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009096{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309097 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009098
9099 free_percpu(ca->cpuusage);
9100 kfree(ca);
9101}
9102
9103/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309104static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009105{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309106 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009107 u64 totalcpuusage = 0;
9108 int i;
9109
9110 for_each_possible_cpu(i) {
9111 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9112
9113 /*
9114 * Take rq->lock to make 64-bit addition safe on 32-bit
9115 * platforms.
9116 */
9117 spin_lock_irq(&cpu_rq(i)->lock);
9118 totalcpuusage += *cpuusage;
9119 spin_unlock_irq(&cpu_rq(i)->lock);
9120 }
9121
9122 return totalcpuusage;
9123}
9124
Dhaval Giani0297b802008-02-29 10:02:44 +05309125static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
9126 u64 reset)
9127{
9128 struct cpuacct *ca = cgroup_ca(cgrp);
9129 int err = 0;
9130 int i;
9131
9132 if (reset) {
9133 err = -EINVAL;
9134 goto out;
9135 }
9136
9137 for_each_possible_cpu(i) {
9138 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9139
9140 spin_lock_irq(&cpu_rq(i)->lock);
9141 *cpuusage = 0;
9142 spin_unlock_irq(&cpu_rq(i)->lock);
9143 }
9144out:
9145 return err;
9146}
9147
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009148static struct cftype files[] = {
9149 {
9150 .name = "usage",
Paul Menagef4c753b2008-04-29 00:59:56 -07009151 .read_u64 = cpuusage_read,
9152 .write_u64 = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009153 },
9154};
9155
Dhaval Giani32cd7562008-02-29 10:02:43 +05309156static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009157{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309158 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009159}
9160
9161/*
9162 * charge this task's execution time to its accounting group.
9163 *
9164 * called with rq->lock held.
9165 */
9166static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
9167{
9168 struct cpuacct *ca;
9169
9170 if (!cpuacct_subsys.active)
9171 return;
9172
9173 ca = task_ca(tsk);
9174 if (ca) {
9175 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
9176
9177 *cpuusage += cputime;
9178 }
9179}
9180
9181struct cgroup_subsys cpuacct_subsys = {
9182 .name = "cpuacct",
9183 .create = cpuacct_create,
9184 .destroy = cpuacct_destroy,
9185 .populate = cpuacct_populate,
9186 .subsys_id = cpuacct_subsys_id,
9187};
9188#endif /* CONFIG_CGROUP_CPUACCT */