blob: 4d84ff4c8774b95851ba2ee01e03ca7f20ea4672 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * kernel/sched.c
3 *
4 * Kernel scheduler and related syscalls
5 *
6 * Copyright (C) 1991-2002 Linus Torvalds
7 *
8 * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and
9 * make semaphores SMP safe
10 * 1998-11-19 Implemented schedule_timeout() and related stuff
11 * by Andrea Arcangeli
12 * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
13 * hybrid priority-list and round-robin design with
14 * an array-switch method of distributing timeslices
15 * and per-CPU runqueues. Cleanups and useful suggestions
16 * by Davide Libenzi, preemptible kernel bits by Robert Love.
17 * 2003-09-03 Interactivity tuning by Con Kolivas.
18 * 2004-04-02 Scheduler domains code by Nick Piggin
Ingo Molnarc31f2e82007-07-09 18:52:01 +020019 * 2007-04-15 Work begun on replacing all interactivity tuning with a
20 * fair scheduling design by Con Kolivas.
21 * 2007-05-05 Load balancing (smp-nice) and other improvements
22 * by Peter Williams
23 * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith
24 * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri
Ingo Molnarb9131762008-01-25 21:08:19 +010025 * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins,
26 * Thomas Gleixner, Mike Kravetz
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
28
29#include <linux/mm.h>
30#include <linux/module.h>
31#include <linux/nmi.h>
32#include <linux/init.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020033#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/highmem.h>
35#include <linux/smp_lock.h>
36#include <asm/mmu_context.h>
37#include <linux/interrupt.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080038#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/completion.h>
40#include <linux/kernel_stat.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070041#include <linux/debug_locks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/security.h>
43#include <linux/notifier.h>
44#include <linux/profile.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080045#include <linux/freezer.h>
akpm@osdl.org198e2f12006-01-12 01:05:30 -080046#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/blkdev.h>
48#include <linux/delay.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070049#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/smp.h>
51#include <linux/threads.h>
52#include <linux/timer.h>
53#include <linux/rcupdate.h>
54#include <linux/cpu.h>
55#include <linux/cpuset.h>
56#include <linux/percpu.h>
57#include <linux/kthread.h>
Alexey Dobriyanb5aadf72008-10-06 13:23:43 +040058#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/seq_file.h>
Nick Piggine692ab52007-07-26 13:40:43 +020060#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/syscalls.h>
62#include <linux/times.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070063#include <linux/tsacct_kern.h>
bibo maoc6fd91f2006-03-26 01:38:20 -080064#include <linux/kprobes.h>
Shailabh Nagar0ff92242006-07-14 00:24:37 -070065#include <linux/delayacct.h>
Eric Dumazet5517d862007-05-08 00:32:57 -070066#include <linux/reciprocal_div.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020067#include <linux/unistd.h>
Jens Axboef5ff8422007-09-21 09:19:54 +020068#include <linux/pagemap.h>
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +010069#include <linux/hrtimer.h>
Reynes Philippe30914a52008-03-17 16:19:05 -070070#include <linux/tick.h>
Mike Travis434d53b2008-04-04 18:11:04 -070071#include <linux/bootmem.h>
Peter Zijlstraf00b45c2008-04-19 19:45:00 +020072#include <linux/debugfs.h>
73#include <linux/ctype.h>
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +020074#include <linux/ftrace.h>
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -040075#include <trace/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Eric Dumazet5517d862007-05-08 00:32:57 -070077#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020078#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Gregory Haskins6e0534f2008-05-12 21:21:01 +020080#include "sched_cpupri.h"
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/*
83 * Convert user-nice values [ -20 ... 0 ... 19 ]
84 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
85 * and back.
86 */
87#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
88#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
89#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
90
91/*
92 * 'User priority' is the nice value converted to something we
93 * can work with better when scaling various scheduler parameters,
94 * it's a [ 0 ... 39 ] range.
95 */
96#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
97#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
98#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
99
100/*
Ingo Molnard7876a02008-01-25 21:08:19 +0100101 * Helpers for converting nanosecond timing to jiffy resolution
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
Eric Dumazetd6322fa2007-11-09 22:39:38 +0100103#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200105#define NICE_0_LOAD SCHED_LOAD_SCALE
106#define NICE_0_SHIFT SCHED_LOAD_SHIFT
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/*
109 * These are the 'tuning knobs' of the scheduler:
110 *
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200111 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * Timeslices get refilled after they expire.
113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700115
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200116/*
117 * single value that denotes runtime == period, ie unlimited time.
118 */
119#define RUNTIME_INF ((u64)~0ULL)
120
Eric Dumazet5517d862007-05-08 00:32:57 -0700121#ifdef CONFIG_SMP
122/*
123 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
124 * Since cpu_power is a 'constant', we can use a reciprocal divide.
125 */
126static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
127{
128 return reciprocal_divide(load, sg->reciprocal_cpu_power);
129}
130
131/*
132 * Each time a sched group cpu_power is changed,
133 * we must compute its reciprocal value
134 */
135static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
136{
137 sg->__cpu_power += val;
138 sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
139}
140#endif
141
Ingo Molnare05606d2007-07-09 18:51:59 +0200142static inline int rt_policy(int policy)
143{
Roel Kluin3f33a7c2008-05-13 23:44:11 +0200144 if (unlikely(policy == SCHED_FIFO || policy == SCHED_RR))
Ingo Molnare05606d2007-07-09 18:51:59 +0200145 return 1;
146 return 0;
147}
148
149static inline int task_has_rt_policy(struct task_struct *p)
150{
151 return rt_policy(p->policy);
152}
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200155 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200157struct rt_prio_array {
158 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
159 struct list_head queue[MAX_RT_PRIO];
160};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200162struct rt_bandwidth {
Ingo Molnarea736ed2008-03-25 13:51:45 +0100163 /* nests inside the rq lock: */
164 spinlock_t rt_runtime_lock;
165 ktime_t rt_period;
166 u64 rt_runtime;
167 struct hrtimer rt_period_timer;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200168};
169
170static struct rt_bandwidth def_rt_bandwidth;
171
172static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
173
174static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
175{
176 struct rt_bandwidth *rt_b =
177 container_of(timer, struct rt_bandwidth, rt_period_timer);
178 ktime_t now;
179 int overrun;
180 int idle = 0;
181
182 for (;;) {
183 now = hrtimer_cb_get_time(timer);
184 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
185
186 if (!overrun)
187 break;
188
189 idle = do_sched_rt_period_timer(rt_b, overrun);
190 }
191
192 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
193}
194
195static
196void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
197{
198 rt_b->rt_period = ns_to_ktime(period);
199 rt_b->rt_runtime = runtime;
200
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200201 spin_lock_init(&rt_b->rt_runtime_lock);
202
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200203 hrtimer_init(&rt_b->rt_period_timer,
204 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
205 rt_b->rt_period_timer.function = sched_rt_period_timer;
Thomas Gleixnerccc7dad2008-09-29 15:47:42 +0200206 rt_b->rt_period_timer.cb_mode = HRTIMER_CB_IRQSAFE_UNLOCKED;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200207}
208
Krzysztof Heltc8bfff62008-09-05 23:46:19 +0200209static inline int rt_bandwidth_enabled(void)
210{
211 return sysctl_sched_rt_runtime >= 0;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200212}
213
214static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
215{
216 ktime_t now;
217
Peter Zijlstra0b148fa2008-08-19 12:33:04 +0200218 if (rt_bandwidth_enabled() && rt_b->rt_runtime == RUNTIME_INF)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200219 return;
220
221 if (hrtimer_active(&rt_b->rt_period_timer))
222 return;
223
224 spin_lock(&rt_b->rt_runtime_lock);
225 for (;;) {
226 if (hrtimer_active(&rt_b->rt_period_timer))
227 break;
228
229 now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
230 hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700231 hrtimer_start_expires(&rt_b->rt_period_timer,
232 HRTIMER_MODE_ABS);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200233 }
234 spin_unlock(&rt_b->rt_runtime_lock);
235}
236
237#ifdef CONFIG_RT_GROUP_SCHED
238static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
239{
240 hrtimer_cancel(&rt_b->rt_period_timer);
241}
242#endif
243
Heiko Carstens712555e2008-04-28 11:33:07 +0200244/*
245 * sched_domains_mutex serializes calls to arch_init_sched_domains,
246 * detach_destroy_domains and partition_sched_domains.
247 */
248static DEFINE_MUTEX(sched_domains_mutex);
249
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100250#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200251
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700252#include <linux/cgroup.h>
253
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200254struct cfs_rq;
255
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100256static LIST_HEAD(task_groups);
257
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200258/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200259struct task_group {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100260#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700261 struct cgroup_subsys_state css;
262#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100263
264#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200265 /* schedulable entities of this group on each cpu */
266 struct sched_entity **se;
267 /* runqueue "owned" by this group on each cpu */
268 struct cfs_rq **cfs_rq;
269 unsigned long shares;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100270#endif
271
272#ifdef CONFIG_RT_GROUP_SCHED
273 struct sched_rt_entity **rt_se;
274 struct rt_rq **rt_rq;
275
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200276 struct rt_bandwidth rt_bandwidth;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100277#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100278
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100279 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100280 struct list_head list;
Peter Zijlstraf473aa52008-04-19 19:45:00 +0200281
282 struct task_group *parent;
283 struct list_head siblings;
284 struct list_head children;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200285};
286
Dhaval Giani354d60c2008-04-19 19:44:59 +0200287#ifdef CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200288
289/*
290 * Root task group.
291 * Every UID task group (including init_task_group aka UID-0) will
292 * be a child to this group.
293 */
294struct task_group root_task_group;
295
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100296#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200297/* Default task group's sched entity on each cpu */
298static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
299/* Default task group's cfs_rq on each cpu */
300static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200301#endif /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100302
303#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100304static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
305static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200306#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +0200307#else /* !CONFIG_USER_SCHED */
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200308#define root_task_group init_task_group
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +0200309#endif /* CONFIG_USER_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100310
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100311/* task_group_lock serializes add/remove of task groups and also changes to
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100312 * a task group's cpu shares.
313 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100314static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100315
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100316#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100317#ifdef CONFIG_USER_SCHED
Ingo Molnar0eab9142008-01-25 21:08:19 +0100318# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200319#else /* !CONFIG_USER_SCHED */
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100320# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200321#endif /* CONFIG_USER_SCHED */
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200322
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800323/*
Lai Jiangshan2e084782008-06-12 16:42:58 +0800324 * A weight of 0 or 1 can cause arithmetics problems.
325 * A weight of a cfs_rq is the sum of weights of which entities
326 * are queued on this cfs_rq, so a weight of a entity should not be
327 * too large, so as the shares value of a task group.
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800328 * (The default weight is 1024 - so there's no practical
329 * limitation from this.)
330 */
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200331#define MIN_SHARES 2
Lai Jiangshan2e084782008-06-12 16:42:58 +0800332#define MAX_SHARES (1UL << 18)
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200333
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100334static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100335#endif
336
337/* Default task group.
338 * Every task in system belong to this group at bootup.
339 */
Mike Travis434d53b2008-04-04 18:11:04 -0700340struct task_group init_task_group;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200341
342/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200343static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200344{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200345 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200346
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100347#ifdef CONFIG_USER_SCHED
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200348 tg = p->user->tg;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100349#elif defined(CONFIG_CGROUP_SCHED)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700350 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
351 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200352#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100353 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200354#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200355 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200356}
357
358/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100359static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200360{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100361#ifdef CONFIG_FAIR_GROUP_SCHED
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100362 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
363 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100364#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100365
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100366#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100367 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
368 p->rt.parent = task_group(p)->rt_se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100369#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200370}
371
372#else
373
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100374static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Peter Zijlstra83378262008-06-27 13:41:37 +0200375static inline struct task_group *task_group(struct task_struct *p)
376{
377 return NULL;
378}
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200379
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100380#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200381
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200382/* CFS-related fields in a runqueue */
383struct cfs_rq {
384 struct load_weight load;
385 unsigned long nr_running;
386
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200387 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200388 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200389
390 struct rb_root tasks_timeline;
391 struct rb_node *rb_leftmost;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +0200392
393 struct list_head tasks;
394 struct list_head *balance_iterator;
395
396 /*
397 * 'curr' points to currently running entity on this cfs_rq.
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200398 * It is set to NULL otherwise (i.e when none are currently running).
399 */
Peter Zijlstra47932412008-11-04 21:25:09 +0100400 struct sched_entity *curr, *next, *last;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200401
Peter Zijlstra5ac5c4d2008-11-10 10:46:32 +0100402 unsigned int nr_spread_over;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200403
Ingo Molnar62160e3f2007-10-15 17:00:03 +0200404#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200405 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
406
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100407 /*
408 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200409 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
410 * (like users, containers etc.)
411 *
412 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
413 * list is used during load balance.
414 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100415 struct list_head leaf_cfs_rq_list;
416 struct task_group *tg; /* group that "owns" this runqueue */
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200417
418#ifdef CONFIG_SMP
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200419 /*
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200420 * the part of load.weight contributed by tasks
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200421 */
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200422 unsigned long task_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200423
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200424 /*
425 * h_load = weight * f(tg)
426 *
427 * Where f(tg) is the recursive weight fraction assigned to
428 * this group.
429 */
430 unsigned long h_load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200431
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200432 /*
433 * this cpu's part of tg->shares
434 */
435 unsigned long shares;
Peter Zijlstraf1d239f2008-06-27 13:41:38 +0200436
437 /*
438 * load.weight at the time we set shares
439 */
440 unsigned long rq_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200441#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200442#endif
443};
444
445/* Real-Time classes' related field in a runqueue: */
446struct rt_rq {
447 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100448 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100449#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100450 int highest_prio; /* highest queued rt task prio */
451#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100452#ifdef CONFIG_SMP
Gregory Haskins73fe6aae2008-01-25 21:08:07 +0100453 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc12008-01-25 21:08:12 +0100454 int overloaded;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100455#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100456 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100457 u64 rt_time;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200458 u64 rt_runtime;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100459 /* Nests inside the rq lock: */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200460 spinlock_t rt_runtime_lock;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100461
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100462#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100463 unsigned long rt_nr_boosted;
464
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100465 struct rq *rq;
466 struct list_head leaf_rt_rq_list;
467 struct task_group *tg;
468 struct sched_rt_entity *rt_se;
469#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200470};
471
Gregory Haskins57d885f2008-01-25 21:08:18 +0100472#ifdef CONFIG_SMP
473
474/*
475 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100476 * variables. Each exclusive cpuset essentially defines an island domain by
477 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100478 * exclusive cpuset is created, we also create and attach a new root-domain
479 * object.
480 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100481 */
482struct root_domain {
483 atomic_t refcount;
484 cpumask_t span;
485 cpumask_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100486
Ingo Molnar0eab9142008-01-25 21:08:19 +0100487 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100488 * The "RT overload" flag: it gets set if a CPU has more than
489 * one runnable RT task.
490 */
491 cpumask_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100492 atomic_t rto_count;
Gregory Haskins6e0534f2008-05-12 21:21:01 +0200493#ifdef CONFIG_SMP
494 struct cpupri cpupri;
495#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +0100496};
497
Gregory Haskinsdc938522008-01-25 21:08:26 +0100498/*
499 * By default the system creates a single root-domain with all cpus as
500 * members (mimicking the global state we have today).
501 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100502static struct root_domain def_root_domain;
503
504#endif
505
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200506/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 * This is the main, per-CPU runqueue data structure.
508 *
509 * Locking rule: those places that want to lock multiple runqueues
510 * (such as the load balancing or the thread migration code), lock
511 * acquire operations must be ordered by ascending &runqueue.
512 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700513struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200514 /* runqueue lock: */
515 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 /*
518 * nr_running and cpu_load should be in the same cacheline because
519 * remote CPUs use both these fields when doing load calculation.
520 */
521 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200522 #define CPU_LOAD_IDX_MAX 5
523 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700524 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700525#ifdef CONFIG_NO_HZ
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200526 unsigned long last_tick_seen;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700527 unsigned char in_nohz_recently;
528#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200529 /* capture load from *all* tasks on this cpu: */
530 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200531 unsigned long nr_load_updates;
532 u64 nr_switches;
533
534 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100535 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100536
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200537#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200538 /* list of leaf cfs_rq on this cpu: */
539 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100540#endif
541#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100542 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 /*
546 * This is part of a global counter where only the total sum
547 * over all CPUs matters. A task can increase this counter on
548 * one CPU and if it got migrated afterwards it may decrease
549 * it on another CPU. Always updated under the runqueue lock:
550 */
551 unsigned long nr_uninterruptible;
552
Ingo Molnar36c8b582006-07-03 00:25:41 -0700553 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800554 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200556
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200557 u64 clock;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 atomic_t nr_iowait;
560
561#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100562 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 struct sched_domain *sd;
564
565 /* For active balancing */
566 int active_balance;
567 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200568 /* cpu of this runqueue: */
569 int cpu;
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -0400570 int online;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Peter Zijlstraa8a51d52008-06-27 13:41:26 +0200572 unsigned long avg_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Ingo Molnar36c8b582006-07-03 00:25:41 -0700574 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct list_head migration_queue;
576#endif
577
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100578#ifdef CONFIG_SCHED_HRTICK
Peter Zijlstra31656512008-07-18 18:01:23 +0200579#ifdef CONFIG_SMP
580 int hrtick_csd_pending;
581 struct call_single_data hrtick_csd;
582#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100583 struct hrtimer hrtick_timer;
584#endif
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586#ifdef CONFIG_SCHEDSTATS
587 /* latency stats */
588 struct sched_info rq_sched_info;
589
590 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200591 unsigned int yld_exp_empty;
592 unsigned int yld_act_empty;
593 unsigned int yld_both_empty;
594 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200597 unsigned int sched_switch;
598 unsigned int sched_count;
599 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200602 unsigned int ttwu_count;
603 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200604
605 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200606 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607#endif
608};
609
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700610static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Peter Zijlstra15afe092008-09-20 23:38:02 +0200612static inline void check_preempt_curr(struct rq *rq, struct task_struct *p, int sync)
Ingo Molnardd41f592007-07-09 18:51:59 +0200613{
Peter Zijlstra15afe092008-09-20 23:38:02 +0200614 rq->curr->sched_class->check_preempt_curr(rq, p, sync);
Ingo Molnardd41f592007-07-09 18:51:59 +0200615}
616
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700617static inline int cpu_of(struct rq *rq)
618{
619#ifdef CONFIG_SMP
620 return rq->cpu;
621#else
622 return 0;
623#endif
624}
625
Ingo Molnar20d315d2007-07-09 18:51:58 +0200626/*
Nick Piggin674311d2005-06-25 14:57:27 -0700627 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700628 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700629 *
630 * The domain tree of any CPU may only be accessed from within
631 * preempt-disabled sections.
632 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700633#define for_each_domain(cpu, __sd) \
634 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
637#define this_rq() (&__get_cpu_var(runqueues))
638#define task_rq(p) cpu_rq(task_cpu(p))
639#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
640
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100641inline void update_rq_clock(struct rq *rq)
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200642{
643 rq->clock = sched_clock_cpu(cpu_of(rq));
644}
645
Ingo Molnare436d802007-07-19 21:28:35 +0200646/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200647 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
648 */
649#ifdef CONFIG_SCHED_DEBUG
650# define const_debug __read_mostly
651#else
652# define const_debug static const
653#endif
654
Ingo Molnar017730c2008-05-12 21:20:52 +0200655/**
656 * runqueue_is_locked
657 *
658 * Returns true if the current cpu runqueue is locked.
659 * This interface allows printk to be called with the runqueue lock
660 * held and know whether or not it is OK to wake up the klogd.
661 */
662int runqueue_is_locked(void)
663{
664 int cpu = get_cpu();
665 struct rq *rq = cpu_rq(cpu);
666 int ret;
667
668 ret = spin_is_locked(&rq->lock);
669 put_cpu();
670 return ret;
671}
672
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200673/*
674 * Debugging: various feature bits
675 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200676
677#define SCHED_FEAT(name, enabled) \
678 __SCHED_FEAT_##name ,
679
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200680enum {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200681#include "sched_features.h"
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200682};
683
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200684#undef SCHED_FEAT
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200685
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200686#define SCHED_FEAT(name, enabled) \
687 (1UL << __SCHED_FEAT_##name) * enabled |
688
689const_debug unsigned int sysctl_sched_features =
690#include "sched_features.h"
691 0;
692
693#undef SCHED_FEAT
694
695#ifdef CONFIG_SCHED_DEBUG
696#define SCHED_FEAT(name, enabled) \
697 #name ,
698
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700699static __read_mostly char *sched_feat_names[] = {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200700#include "sched_features.h"
701 NULL
702};
703
704#undef SCHED_FEAT
705
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700706static int sched_feat_open(struct inode *inode, struct file *filp)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200707{
708 filp->private_data = inode->i_private;
709 return 0;
710}
711
712static ssize_t
713sched_feat_read(struct file *filp, char __user *ubuf,
714 size_t cnt, loff_t *ppos)
715{
716 char *buf;
717 int r = 0;
718 int len = 0;
719 int i;
720
721 for (i = 0; sched_feat_names[i]; i++) {
722 len += strlen(sched_feat_names[i]);
723 len += 4;
724 }
725
726 buf = kmalloc(len + 2, GFP_KERNEL);
727 if (!buf)
728 return -ENOMEM;
729
730 for (i = 0; sched_feat_names[i]; i++) {
731 if (sysctl_sched_features & (1UL << i))
732 r += sprintf(buf + r, "%s ", sched_feat_names[i]);
733 else
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200734 r += sprintf(buf + r, "NO_%s ", sched_feat_names[i]);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200735 }
736
737 r += sprintf(buf + r, "\n");
738 WARN_ON(r >= len + 2);
739
740 r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
741
742 kfree(buf);
743
744 return r;
745}
746
747static ssize_t
748sched_feat_write(struct file *filp, const char __user *ubuf,
749 size_t cnt, loff_t *ppos)
750{
751 char buf[64];
752 char *cmp = buf;
753 int neg = 0;
754 int i;
755
756 if (cnt > 63)
757 cnt = 63;
758
759 if (copy_from_user(&buf, ubuf, cnt))
760 return -EFAULT;
761
762 buf[cnt] = 0;
763
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200764 if (strncmp(buf, "NO_", 3) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200765 neg = 1;
766 cmp += 3;
767 }
768
769 for (i = 0; sched_feat_names[i]; i++) {
770 int len = strlen(sched_feat_names[i]);
771
772 if (strncmp(cmp, sched_feat_names[i], len) == 0) {
773 if (neg)
774 sysctl_sched_features &= ~(1UL << i);
775 else
776 sysctl_sched_features |= (1UL << i);
777 break;
778 }
779 }
780
781 if (!sched_feat_names[i])
782 return -EINVAL;
783
784 filp->f_pos += cnt;
785
786 return cnt;
787}
788
789static struct file_operations sched_feat_fops = {
790 .open = sched_feat_open,
791 .read = sched_feat_read,
792 .write = sched_feat_write,
793};
794
795static __init int sched_init_debug(void)
796{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200797 debugfs_create_file("sched_features", 0644, NULL, NULL,
798 &sched_feat_fops);
799
800 return 0;
801}
802late_initcall(sched_init_debug);
803
804#endif
805
806#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200807
808/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100809 * Number of tasks to iterate in a single balance run.
810 * Limited because this is done with IRQs disabled.
811 */
812const_debug unsigned int sysctl_sched_nr_migrate = 32;
813
814/*
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200815 * ratelimit for updating the group shares.
Peter Zijlstra55cd5342008-08-04 08:54:26 +0200816 * default: 0.25ms
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200817 */
Peter Zijlstra55cd5342008-08-04 08:54:26 +0200818unsigned int sysctl_sched_shares_ratelimit = 250000;
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200819
820/*
Peter Zijlstraffda12a2008-10-17 19:27:02 +0200821 * Inject some fuzzyness into changing the per-cpu group shares
822 * this avoids remote rq-locks at the expense of fairness.
823 * default: 4
824 */
825unsigned int sysctl_sched_shares_thresh = 4;
826
827/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100828 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100829 * default: 1s
830 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100831unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100832
Ingo Molnar6892b752008-02-13 14:02:36 +0100833static __read_mostly int scheduler_running;
834
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100835/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100836 * part of the period that we allow rt tasks to run in us.
837 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100838 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100839int sysctl_sched_rt_runtime = 950000;
840
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200841static inline u64 global_rt_period(void)
842{
843 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
844}
845
846static inline u64 global_rt_runtime(void)
847{
roel kluine26873b2008-07-22 16:51:15 -0400848 if (sysctl_sched_rt_runtime < 0)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200849 return RUNTIME_INF;
850
851 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
852}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700855# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700857#ifndef finish_arch_switch
858# define finish_arch_switch(prev) do { } while (0)
859#endif
860
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100861static inline int task_current(struct rq *rq, struct task_struct *p)
862{
863 return rq->curr == p;
864}
865
Nick Piggin4866cde2005-06-25 14:57:23 -0700866#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700867static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700868{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100869 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700870}
871
Ingo Molnar70b97a72006-07-03 00:25:42 -0700872static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700873{
874}
875
Ingo Molnar70b97a72006-07-03 00:25:42 -0700876static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700877{
Ingo Molnarda04c032005-09-13 11:17:59 +0200878#ifdef CONFIG_DEBUG_SPINLOCK
879 /* this is a valid case when another task releases the spinlock */
880 rq->lock.owner = current;
881#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700882 /*
883 * If we are tracking spinlock dependencies then we have to
884 * fix up the runqueue lock - which gets 'carried over' from
885 * prev into current:
886 */
887 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
888
Nick Piggin4866cde2005-06-25 14:57:23 -0700889 spin_unlock_irq(&rq->lock);
890}
891
892#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700893static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700894{
895#ifdef CONFIG_SMP
896 return p->oncpu;
897#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100898 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700899#endif
900}
901
Ingo Molnar70b97a72006-07-03 00:25:42 -0700902static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700903{
904#ifdef CONFIG_SMP
905 /*
906 * We can optimise this out completely for !SMP, because the
907 * SMP rebalancing from interrupt is the only thing that cares
908 * here.
909 */
910 next->oncpu = 1;
911#endif
912#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
913 spin_unlock_irq(&rq->lock);
914#else
915 spin_unlock(&rq->lock);
916#endif
917}
918
Ingo Molnar70b97a72006-07-03 00:25:42 -0700919static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700920{
921#ifdef CONFIG_SMP
922 /*
923 * After ->oncpu is cleared, the task can be moved to a different CPU.
924 * We must ensure this doesn't happen until the switch is completely
925 * finished.
926 */
927 smp_wmb();
928 prev->oncpu = 0;
929#endif
930#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
931 local_irq_enable();
932#endif
933}
934#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700937 * __task_rq_lock - lock the runqueue a given task resides on.
938 * Must be called interrupts disabled.
939 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700940static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700941 __acquires(rq->lock)
942{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200943 for (;;) {
944 struct rq *rq = task_rq(p);
945 spin_lock(&rq->lock);
946 if (likely(rq == task_rq(p)))
947 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700948 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700949 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700950}
951
952/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100954 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 * explicitly disabling preemption.
956 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700957static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 __acquires(rq->lock)
959{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700960 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Andi Kleen3a5c3592007-10-15 17:00:14 +0200962 for (;;) {
963 local_irq_save(*flags);
964 rq = task_rq(p);
965 spin_lock(&rq->lock);
966 if (likely(rq == task_rq(p)))
967 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
Ingo Molnaraa9c4c02008-12-17 14:10:57 +0100972void curr_rq_lock_irq_save(unsigned long *flags)
973 __acquires(rq->lock)
974{
975 struct rq *rq;
976
977 local_irq_save(*flags);
978 rq = cpu_rq(smp_processor_id());
979 spin_lock(&rq->lock);
980}
981
982void curr_rq_unlock_irq_restore(unsigned long *flags)
983 __releases(rq->lock)
984{
985 struct rq *rq;
986
987 rq = cpu_rq(smp_processor_id());
988 spin_unlock(&rq->lock);
989 local_irq_restore(*flags);
990}
991
Oleg Nesterovad474ca2008-11-10 15:39:30 +0100992void task_rq_unlock_wait(struct task_struct *p)
993{
994 struct rq *rq = task_rq(p);
995
996 smp_mb(); /* spin-unlock-wait is not a full memory barrier */
997 spin_unlock_wait(&rq->lock);
998}
999
Alexey Dobriyana9957442007-10-15 17:00:13 +02001000static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001001 __releases(rq->lock)
1002{
1003 spin_unlock(&rq->lock);
1004}
1005
Ingo Molnar70b97a72006-07-03 00:25:42 -07001006static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 __releases(rq->lock)
1008{
1009 spin_unlock_irqrestore(&rq->lock, *flags);
1010}
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -08001013 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001015static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 __acquires(rq->lock)
1017{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001018 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 local_irq_disable();
1021 rq = this_rq();
1022 spin_lock(&rq->lock);
1023
1024 return rq;
1025}
1026
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001027#ifdef CONFIG_SCHED_HRTICK
1028/*
1029 * Use HR-timers to deliver accurate preemption points.
1030 *
1031 * Its all a bit involved since we cannot program an hrt while holding the
1032 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1033 * reschedule event.
1034 *
1035 * When we get rescheduled we reprogram the hrtick_timer outside of the
1036 * rq->lock.
1037 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001038
1039/*
1040 * Use hrtick when:
1041 * - enabled by features
1042 * - hrtimer is actually high res
1043 */
1044static inline int hrtick_enabled(struct rq *rq)
1045{
1046 if (!sched_feat(HRTICK))
1047 return 0;
Ingo Molnarba420592008-07-20 11:02:06 +02001048 if (!cpu_active(cpu_of(rq)))
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001049 return 0;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001050 return hrtimer_is_hres_active(&rq->hrtick_timer);
1051}
1052
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001053static void hrtick_clear(struct rq *rq)
1054{
1055 if (hrtimer_active(&rq->hrtick_timer))
1056 hrtimer_cancel(&rq->hrtick_timer);
1057}
1058
1059/*
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001060 * High-resolution timer tick.
1061 * Runs from hardirq context with interrupts disabled.
1062 */
1063static enum hrtimer_restart hrtick(struct hrtimer *timer)
1064{
1065 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1066
1067 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1068
1069 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02001070 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001071 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1072 spin_unlock(&rq->lock);
1073
1074 return HRTIMER_NORESTART;
1075}
1076
Rabin Vincent95e904c2008-05-11 05:55:33 +05301077#ifdef CONFIG_SMP
Peter Zijlstra31656512008-07-18 18:01:23 +02001078/*
1079 * called from hardirq (IPI) context
1080 */
1081static void __hrtick_start(void *arg)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001082{
Peter Zijlstra31656512008-07-18 18:01:23 +02001083 struct rq *rq = arg;
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001084
Peter Zijlstra31656512008-07-18 18:01:23 +02001085 spin_lock(&rq->lock);
1086 hrtimer_restart(&rq->hrtick_timer);
1087 rq->hrtick_csd_pending = 0;
1088 spin_unlock(&rq->lock);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001089}
1090
Peter Zijlstra31656512008-07-18 18:01:23 +02001091/*
1092 * Called to set the hrtick timer state.
1093 *
1094 * called with rq->lock held and irqs disabled
1095 */
1096static void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001097{
Peter Zijlstra31656512008-07-18 18:01:23 +02001098 struct hrtimer *timer = &rq->hrtick_timer;
1099 ktime_t time = ktime_add_ns(timer->base->get_time(), delay);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001100
Arjan van de Vencc584b22008-09-01 15:02:30 -07001101 hrtimer_set_expires(timer, time);
Peter Zijlstra31656512008-07-18 18:01:23 +02001102
1103 if (rq == this_rq()) {
1104 hrtimer_restart(timer);
1105 } else if (!rq->hrtick_csd_pending) {
1106 __smp_call_function_single(cpu_of(rq), &rq->hrtick_csd);
1107 rq->hrtick_csd_pending = 1;
1108 }
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001109}
1110
1111static int
1112hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
1113{
1114 int cpu = (int)(long)hcpu;
1115
1116 switch (action) {
1117 case CPU_UP_CANCELED:
1118 case CPU_UP_CANCELED_FROZEN:
1119 case CPU_DOWN_PREPARE:
1120 case CPU_DOWN_PREPARE_FROZEN:
1121 case CPU_DEAD:
1122 case CPU_DEAD_FROZEN:
Peter Zijlstra31656512008-07-18 18:01:23 +02001123 hrtick_clear(cpu_rq(cpu));
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001124 return NOTIFY_OK;
1125 }
1126
1127 return NOTIFY_DONE;
1128}
1129
Rakib Mullickfa748202008-09-22 14:55:45 -07001130static __init void init_hrtick(void)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001131{
1132 hotcpu_notifier(hotplug_hrtick, 0);
1133}
Peter Zijlstra31656512008-07-18 18:01:23 +02001134#else
1135/*
1136 * Called to set the hrtick timer state.
1137 *
1138 * called with rq->lock held and irqs disabled
1139 */
1140static void hrtick_start(struct rq *rq, u64 delay)
1141{
1142 hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay), HRTIMER_MODE_REL);
1143}
1144
Andrew Morton006c75f2008-09-22 14:55:46 -07001145static inline void init_hrtick(void)
Peter Zijlstra31656512008-07-18 18:01:23 +02001146{
1147}
Rabin Vincent95e904c2008-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{
Peter Zijlstra31656512008-07-18 18:01:23 +02001152#ifdef CONFIG_SMP
1153 rq->hrtick_csd_pending = 0;
1154
1155 rq->hrtick_csd.flags = 0;
1156 rq->hrtick_csd.func = __hrtick_start;
1157 rq->hrtick_csd.info = rq;
1158#endif
1159
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001160 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1161 rq->hrtick_timer.function = hrtick;
Thomas Gleixnerccc7dad2008-09-29 15:47:42 +02001162 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_PERCPU;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001163}
Andrew Morton006c75f2008-09-22 14:55:46 -07001164#else /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001165static inline void hrtick_clear(struct rq *rq)
1166{
1167}
1168
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001169static inline void init_rq_hrtick(struct rq *rq)
1170{
1171}
1172
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001173static inline void init_hrtick(void)
1174{
1175}
Andrew Morton006c75f2008-09-22 14:55:46 -07001176#endif /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001177
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001178/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001179 * resched_task - mark a task 'to be rescheduled now'.
1180 *
1181 * On UP this means the setting of the need_resched flag, on SMP it
1182 * might also involve a cross-CPU call to trigger the scheduler on
1183 * the target CPU.
1184 */
1185#ifdef CONFIG_SMP
1186
1187#ifndef tsk_is_polling
1188#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1189#endif
1190
Peter Zijlstra31656512008-07-18 18:01:23 +02001191static void resched_task(struct task_struct *p)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001192{
1193 int cpu;
1194
1195 assert_spin_locked(&task_rq(p)->lock);
1196
Peter Zijlstra31656512008-07-18 18:01:23 +02001197 if (unlikely(test_tsk_thread_flag(p, TIF_NEED_RESCHED)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001198 return;
1199
Peter Zijlstra31656512008-07-18 18:01:23 +02001200 set_tsk_thread_flag(p, TIF_NEED_RESCHED);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001201
1202 cpu = task_cpu(p);
1203 if (cpu == smp_processor_id())
1204 return;
1205
1206 /* NEED_RESCHED must be visible before we test polling */
1207 smp_mb();
1208 if (!tsk_is_polling(p))
1209 smp_send_reschedule(cpu);
1210}
1211
1212static void resched_cpu(int cpu)
1213{
1214 struct rq *rq = cpu_rq(cpu);
1215 unsigned long flags;
1216
1217 if (!spin_trylock_irqsave(&rq->lock, flags))
1218 return;
1219 resched_task(cpu_curr(cpu));
1220 spin_unlock_irqrestore(&rq->lock, flags);
1221}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001222
1223#ifdef CONFIG_NO_HZ
1224/*
1225 * When add_timer_on() enqueues a timer into the timer wheel of an
1226 * idle CPU then this timer might expire before the next timer event
1227 * which is scheduled to wake up that CPU. In case of a completely
1228 * idle system the next event might even be infinite time into the
1229 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1230 * leaves the inner idle loop so the newly added timer is taken into
1231 * account when the CPU goes back to idle and evaluates the timer
1232 * wheel for the next timer event.
1233 */
1234void wake_up_idle_cpu(int cpu)
1235{
1236 struct rq *rq = cpu_rq(cpu);
1237
1238 if (cpu == smp_processor_id())
1239 return;
1240
1241 /*
1242 * This is safe, as this function is called with the timer
1243 * wheel base lock of (cpu) held. When the CPU is on the way
1244 * to idle and has not yet set rq->curr to idle then it will
1245 * be serialized on the timer wheel base lock and take the new
1246 * timer into account automatically.
1247 */
1248 if (rq->curr != rq->idle)
1249 return;
1250
1251 /*
1252 * We can set TIF_RESCHED on the idle task of the other CPU
1253 * lockless. The worst case is that the other CPU runs the
1254 * idle task through an additional NOOP schedule()
1255 */
1256 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1257
1258 /* NEED_RESCHED must be visible before we test polling */
1259 smp_mb();
1260 if (!tsk_is_polling(rq->idle))
1261 smp_send_reschedule(cpu);
1262}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001263#endif /* CONFIG_NO_HZ */
Thomas Gleixner06d83082008-03-22 09:20:24 +01001264
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001265#else /* !CONFIG_SMP */
Peter Zijlstra31656512008-07-18 18:01:23 +02001266static void resched_task(struct task_struct *p)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001267{
1268 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra31656512008-07-18 18:01:23 +02001269 set_tsk_need_resched(p);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001270}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001271#endif /* CONFIG_SMP */
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001272
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001273#if BITS_PER_LONG == 32
1274# define WMULT_CONST (~0UL)
1275#else
1276# define WMULT_CONST (1UL << 32)
1277#endif
1278
1279#define WMULT_SHIFT 32
1280
Ingo Molnar194081e2007-08-09 11:16:51 +02001281/*
1282 * Shift right and round:
1283 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001284#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001285
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001286/*
1287 * delta *= weight / lw
1288 */
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001289static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001290calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1291 struct load_weight *lw)
1292{
1293 u64 tmp;
1294
Lai Jiangshan7a232e02008-06-12 16:43:07 +08001295 if (!lw->inv_weight) {
1296 if (BITS_PER_LONG > 32 && unlikely(lw->weight >= WMULT_CONST))
1297 lw->inv_weight = 1;
1298 else
1299 lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)
1300 / (lw->weight+1);
1301 }
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001302
1303 tmp = (u64)delta_exec * weight;
1304 /*
1305 * Check whether we'd overflow the 64-bit multiplication:
1306 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001307 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001308 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001309 WMULT_SHIFT/2);
1310 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001311 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001312
Ingo Molnarecf691d2007-08-02 17:41:40 +02001313 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001314}
1315
Ingo Molnar10919852007-10-15 17:00:04 +02001316static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001317{
1318 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001319 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001320}
1321
Ingo Molnar10919852007-10-15 17:00:04 +02001322static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001323{
1324 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001325 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001326}
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001329 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1330 * of tasks with abnormal "nice" values across CPUs the contribution that
1331 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001332 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001333 * scaled version of the new time slice allocation that they receive on time
1334 * slice expiry etc.
1335 */
1336
Ingo Molnardd41f592007-07-09 18:51:59 +02001337#define WEIGHT_IDLEPRIO 2
1338#define WMULT_IDLEPRIO (1 << 31)
1339
1340/*
1341 * Nice levels are multiplicative, with a gentle 10% change for every
1342 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1343 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1344 * that remained on nice 0.
1345 *
1346 * The "10% effect" is relative and cumulative: from _any_ nice level,
1347 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee62007-07-16 09:46:30 +02001348 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1349 * If a task goes up by ~10% and another task goes down by ~10% then
1350 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001351 */
1352static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001353 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1354 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1355 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1356 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1357 /* 0 */ 1024, 820, 655, 526, 423,
1358 /* 5 */ 335, 272, 215, 172, 137,
1359 /* 10 */ 110, 87, 70, 56, 45,
1360 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001361};
1362
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001363/*
1364 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1365 *
1366 * In cases where the weight does not change often, we can use the
1367 * precalculated inverse to speed up arithmetics by turning divisions
1368 * into multiplications:
1369 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001370static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001371 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1372 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1373 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1374 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1375 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1376 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1377 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1378 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001379};
Peter Williams2dd73a42006-06-27 02:54:34 -07001380
Ingo Molnardd41f592007-07-09 18:51:59 +02001381static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1382
1383/*
1384 * runqueue iterator, to support SMP load-balancing between different
1385 * scheduling classes, without having to expose their internal data
1386 * structures to the load-balancing proper:
1387 */
1388struct rq_iterator {
1389 void *arg;
1390 struct task_struct *(*start)(void *);
1391 struct task_struct *(*next)(void *);
1392};
1393
Peter Williamse1d14842007-10-24 18:23:51 +02001394#ifdef CONFIG_SMP
1395static unsigned long
1396balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1397 unsigned long max_load_move, struct sched_domain *sd,
1398 enum cpu_idle_type idle, int *all_pinned,
1399 int *this_best_prio, struct rq_iterator *iterator);
1400
1401static int
1402iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1403 struct sched_domain *sd, enum cpu_idle_type idle,
1404 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001405#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001406
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001407#ifdef CONFIG_CGROUP_CPUACCT
1408static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1409#else
1410static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1411#endif
1412
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001413static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1414{
1415 update_load_add(&rq->load, load);
1416}
1417
1418static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1419{
1420 update_load_sub(&rq->load, load);
1421}
1422
Ingo Molnar7940ca32008-08-19 13:40:47 +02001423#if (defined(CONFIG_SMP) && defined(CONFIG_FAIR_GROUP_SCHED)) || defined(CONFIG_RT_GROUP_SCHED)
Peter Zijlstraeb755802008-08-19 12:33:05 +02001424typedef int (*tg_visitor)(struct task_group *, void *);
1425
1426/*
1427 * Iterate the full tree, calling @down when first entering a node and @up when
1428 * leaving it for the final time.
1429 */
1430static int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
1431{
1432 struct task_group *parent, *child;
1433 int ret;
1434
1435 rcu_read_lock();
1436 parent = &root_task_group;
1437down:
1438 ret = (*down)(parent, data);
1439 if (ret)
1440 goto out_unlock;
1441 list_for_each_entry_rcu(child, &parent->children, siblings) {
1442 parent = child;
1443 goto down;
1444
1445up:
1446 continue;
1447 }
1448 ret = (*up)(parent, data);
1449 if (ret)
1450 goto out_unlock;
1451
1452 child = parent;
1453 parent = parent->parent;
1454 if (parent)
1455 goto up;
1456out_unlock:
1457 rcu_read_unlock();
1458
1459 return ret;
1460}
1461
1462static int tg_nop(struct task_group *tg, void *data)
1463{
1464 return 0;
1465}
1466#endif
1467
Gregory Haskinse7693a32008-01-25 21:08:09 +01001468#ifdef CONFIG_SMP
1469static unsigned long source_load(int cpu, int type);
1470static unsigned long target_load(int cpu, int type);
Gregory Haskinse7693a32008-01-25 21:08:09 +01001471static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001472
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001473static unsigned long cpu_avg_load_per_task(int cpu)
1474{
1475 struct rq *rq = cpu_rq(cpu);
Ingo Molnaraf6d5962008-11-29 20:45:15 +01001476 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001477
Steven Rostedt4cd42622008-11-26 21:04:24 -05001478 if (nr_running)
1479 rq->avg_load_per_task = rq->load.weight / nr_running;
Balbir Singha2d47772008-11-12 16:19:00 +05301480 else
1481 rq->avg_load_per_task = 0;
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001482
1483 return rq->avg_load_per_task;
1484}
1485
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001486#ifdef CONFIG_FAIR_GROUP_SCHED
1487
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001488static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1489
1490/*
1491 * Calculate and set the cpu's group shares.
1492 */
1493static void
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001494update_group_shares_cpu(struct task_group *tg, int cpu,
1495 unsigned long sd_shares, unsigned long sd_rq_weight)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001496{
1497 int boost = 0;
1498 unsigned long shares;
1499 unsigned long rq_weight;
1500
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001501 if (!tg->se[cpu])
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001502 return;
1503
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001504 rq_weight = tg->cfs_rq[cpu]->load.weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001505
1506 /*
1507 * If there are currently no tasks on the cpu pretend there is one of
1508 * average load so that when a new task gets to run here it will not
1509 * get delayed by group starvation.
1510 */
1511 if (!rq_weight) {
1512 boost = 1;
1513 rq_weight = NICE_0_LOAD;
1514 }
1515
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001516 if (unlikely(rq_weight > sd_rq_weight))
1517 rq_weight = sd_rq_weight;
1518
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001519 /*
1520 * \Sum shares * rq_weight
1521 * shares = -----------------------
1522 * \Sum rq_weight
1523 *
1524 */
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001525 shares = (sd_shares * rq_weight) / (sd_rq_weight + 1);
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001526 shares = clamp_t(unsigned long, shares, MIN_SHARES, MAX_SHARES);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001527
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001528 if (abs(shares - tg->se[cpu]->load.weight) >
1529 sysctl_sched_shares_thresh) {
1530 struct rq *rq = cpu_rq(cpu);
1531 unsigned long flags;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001532
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001533 spin_lock_irqsave(&rq->lock, flags);
1534 /*
1535 * record the actual number of shares, not the boosted amount.
1536 */
1537 tg->cfs_rq[cpu]->shares = boost ? 0 : shares;
1538 tg->cfs_rq[cpu]->rq_weight = rq_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001539
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001540 __set_se_shares(tg->se[cpu], shares);
1541 spin_unlock_irqrestore(&rq->lock, flags);
1542 }
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001543}
1544
1545/*
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001546 * Re-compute the task group their per cpu shares over the given domain.
1547 * This needs to be done in a bottom-up fashion because the rq weight of a
1548 * parent group depends on the shares of its child groups.
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001549 */
Peter Zijlstraeb755802008-08-19 12:33:05 +02001550static int tg_shares_up(struct task_group *tg, void *data)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001551{
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001552 unsigned long rq_weight = 0;
1553 unsigned long shares = 0;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001554 struct sched_domain *sd = data;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001555 int i;
1556
1557 for_each_cpu_mask(i, sd->span) {
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001558 rq_weight += tg->cfs_rq[i]->load.weight;
1559 shares += tg->cfs_rq[i]->shares;
1560 }
1561
1562 if ((!shares && rq_weight) || shares > tg->shares)
1563 shares = tg->shares;
1564
1565 if (!sd->parent || !(sd->parent->flags & SD_LOAD_BALANCE))
1566 shares = tg->shares;
1567
Peter Zijlstracd809172008-06-27 13:41:34 +02001568 if (!rq_weight)
1569 rq_weight = cpus_weight(sd->span) * NICE_0_LOAD;
1570
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001571 for_each_cpu_mask(i, sd->span)
1572 update_group_shares_cpu(tg, i, shares, rq_weight);
Peter Zijlstraeb755802008-08-19 12:33:05 +02001573
1574 return 0;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001575}
1576
1577/*
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001578 * Compute the cpu's hierarchical load factor for each task group.
1579 * This needs to be done in a top-down fashion because the load of a child
1580 * group is a fraction of its parents load.
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001581 */
Peter Zijlstraeb755802008-08-19 12:33:05 +02001582static int tg_load_down(struct task_group *tg, void *data)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001583{
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001584 unsigned long load;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001585 long cpu = (long)data;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001586
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001587 if (!tg->parent) {
1588 load = cpu_rq(cpu)->load.weight;
1589 } else {
1590 load = tg->parent->cfs_rq[cpu]->h_load;
1591 load *= tg->cfs_rq[cpu]->shares;
1592 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
1593 }
1594
1595 tg->cfs_rq[cpu]->h_load = load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001596
Peter Zijlstraeb755802008-08-19 12:33:05 +02001597 return 0;
Peter Zijlstra4d8d5952008-06-27 13:41:19 +02001598}
1599
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001600static void update_shares(struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001601{
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02001602 u64 now = cpu_clock(raw_smp_processor_id());
1603 s64 elapsed = now - sd->last_update;
1604
1605 if (elapsed >= (s64)(u64)sysctl_sched_shares_ratelimit) {
1606 sd->last_update = now;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001607 walk_tg_tree(tg_nop, tg_shares_up, sd);
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02001608 }
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001609}
1610
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02001611static void update_shares_locked(struct rq *rq, struct sched_domain *sd)
1612{
1613 spin_unlock(&rq->lock);
1614 update_shares(sd);
1615 spin_lock(&rq->lock);
1616}
1617
Peter Zijlstraeb755802008-08-19 12:33:05 +02001618static void update_h_load(long cpu)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001619{
Peter Zijlstraeb755802008-08-19 12:33:05 +02001620 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001621}
1622
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001623#else
1624
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001625static inline void update_shares(struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001626{
1627}
1628
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02001629static inline void update_shares_locked(struct rq *rq, struct sched_domain *sd)
1630{
1631}
1632
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001633#endif
1634
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001635#endif
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001636
1637#ifdef CONFIG_FAIR_GROUP_SCHED
1638static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1639{
Vegard Nossum30432092008-06-27 21:35:50 +02001640#ifdef CONFIG_SMP
Ingo Molnar34e83e82008-06-27 15:42:36 +02001641 cfs_rq->shares = shares;
1642#endif
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001643}
1644#endif
1645
Ingo Molnardd41f592007-07-09 18:51:59 +02001646#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001647#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001648#include "sched_fair.c"
1649#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001650#ifdef CONFIG_SCHED_DEBUG
1651# include "sched_debug.c"
1652#endif
1653
1654#define sched_class_highest (&rt_sched_class)
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04001655#define for_each_class(class) \
1656 for (class = sched_class_highest; class; class = class->next)
Ingo Molnardd41f592007-07-09 18:51:59 +02001657
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001658static void inc_nr_running(struct rq *rq)
Ingo Molnar6363ca52008-05-29 11:28:57 +02001659{
1660 rq->nr_running++;
Ingo Molnar6363ca52008-05-29 11:28:57 +02001661}
1662
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001663static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001664{
1665 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001666}
1667
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001668static void set_load_weight(struct task_struct *p)
1669{
1670 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001671 p->se.load.weight = prio_to_weight[0] * 2;
1672 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1673 return;
1674 }
1675
1676 /*
1677 * SCHED_IDLE tasks get minimal weight:
1678 */
1679 if (p->policy == SCHED_IDLE) {
1680 p->se.load.weight = WEIGHT_IDLEPRIO;
1681 p->se.load.inv_weight = WMULT_IDLEPRIO;
1682 return;
1683 }
1684
1685 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1686 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001687}
1688
Gregory Haskins2087a1a2008-06-27 14:30:00 -06001689static void update_avg(u64 *avg, u64 sample)
1690{
1691 s64 diff = sample - *avg;
1692 *avg += diff >> 3;
1693}
1694
Ingo Molnar8159f872007-08-09 11:16:49 +02001695static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001696{
1697 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001698 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001699 p->se.on_rq = 1;
1700}
1701
Ingo Molnar69be72c2007-08-09 11:16:49 +02001702static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001703{
Gregory Haskins2087a1a2008-06-27 14:30:00 -06001704 if (sleep && p->se.last_wakeup) {
1705 update_avg(&p->se.avg_overlap,
1706 p->se.sum_exec_runtime - p->se.last_wakeup);
1707 p->se.last_wakeup = 0;
1708 }
1709
Ankita Garg46ac22b2008-07-01 14:30:06 +05301710 sched_info_dequeued(p);
Ingo Molnarf02231e2007-08-09 11:16:48 +02001711 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001712 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001713}
1714
1715/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001716 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001717 */
Ingo Molnar14531182007-07-09 18:51:59 +02001718static inline int __normal_prio(struct task_struct *p)
1719{
Ingo Molnardd41f592007-07-09 18:51:59 +02001720 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001721}
1722
1723/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001724 * Calculate the expected normal priority: i.e. priority
1725 * without taking RT-inheritance into account. Might be
1726 * boosted by interactivity modifiers. Changes upon fork,
1727 * setprio syscalls, and whenever the interactivity
1728 * estimator recalculates.
1729 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001730static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001731{
1732 int prio;
1733
Ingo Molnare05606d2007-07-09 18:51:59 +02001734 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001735 prio = MAX_RT_PRIO-1 - p->rt_priority;
1736 else
1737 prio = __normal_prio(p);
1738 return prio;
1739}
1740
1741/*
1742 * Calculate the current priority, i.e. the priority
1743 * taken into account by the scheduler. This value might
1744 * be boosted by RT tasks, or might be boosted by
1745 * interactivity modifiers. Will be RT if the task got
1746 * RT-boosted. If not then it returns p->normal_prio.
1747 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001748static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001749{
1750 p->normal_prio = normal_prio(p);
1751 /*
1752 * If we are RT tasks or we were boosted to RT priority,
1753 * keep the priority unchanged. Otherwise, update priority
1754 * to the normal priority:
1755 */
1756 if (!rt_prio(p->prio))
1757 return p->normal_prio;
1758 return p->prio;
1759}
1760
1761/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001762 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001764static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001766 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001767 rq->nr_uninterruptible--;
1768
Ingo Molnar8159f872007-08-09 11:16:49 +02001769 enqueue_task(rq, p, wakeup);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001770 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
1773/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 * deactivate_task - remove a task from the runqueue.
1775 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02001776static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001778 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001779 rq->nr_uninterruptible++;
1780
Ingo Molnar69be72c2007-08-09 11:16:49 +02001781 dequeue_task(rq, p, sleep);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001782 dec_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785/**
1786 * task_curr - is this task currently executing on a CPU?
1787 * @p: the task in question.
1788 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001789inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
1791 return cpu_curr(task_cpu(p)) == p;
1792}
1793
Ingo Molnardd41f592007-07-09 18:51:59 +02001794static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1795{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001796 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001797#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01001798 /*
1799 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1800 * successfuly executed on another CPU. We must ensure that updates of
1801 * per-task data have been completed by this moment.
1802 */
1803 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02001804 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02001805#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07001806}
1807
Steven Rostedtcb469842008-01-25 21:08:22 +01001808static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1809 const struct sched_class *prev_class,
1810 int oldprio, int running)
1811{
1812 if (prev_class != p->sched_class) {
1813 if (prev_class->switched_from)
1814 prev_class->switched_from(rq, p, running);
1815 p->sched_class->switched_to(rq, p, running);
1816 } else
1817 p->sched_class->prio_changed(rq, p, oldprio, running);
1818}
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02001821
Thomas Gleixnere958b362008-06-04 23:22:32 +02001822/* Used instead of source_load when we know the type == 0 */
1823static unsigned long weighted_cpuload(const int cpu)
1824{
1825 return cpu_rq(cpu)->load.weight;
1826}
1827
Ingo Molnarcc367732007-10-15 17:00:18 +02001828/*
1829 * Is this task likely cache-hot:
1830 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001831static int
Ingo Molnarcc367732007-10-15 17:00:18 +02001832task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
1833{
1834 s64 delta;
1835
Ingo Molnarf540a602008-03-15 17:10:34 +01001836 /*
1837 * Buddy candidates are cache hot:
1838 */
Peter Zijlstra47932412008-11-04 21:25:09 +01001839 if (sched_feat(CACHE_HOT_BUDDY) &&
1840 (&p->se == cfs_rq_of(&p->se)->next ||
1841 &p->se == cfs_rq_of(&p->se)->last))
Ingo Molnarf540a602008-03-15 17:10:34 +01001842 return 1;
1843
Ingo Molnarcc367732007-10-15 17:00:18 +02001844 if (p->sched_class != &fair_sched_class)
1845 return 0;
1846
Ingo Molnar6bc16652007-10-15 17:00:18 +02001847 if (sysctl_sched_migration_cost == -1)
1848 return 1;
1849 if (sysctl_sched_migration_cost == 0)
1850 return 0;
1851
Ingo Molnarcc367732007-10-15 17:00:18 +02001852 delta = now - p->se.exec_start;
1853
1854 return delta < (s64)sysctl_sched_migration_cost;
1855}
1856
1857
Ingo Molnardd41f592007-07-09 18:51:59 +02001858void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001859{
Ingo Molnardd41f592007-07-09 18:51:59 +02001860 int old_cpu = task_cpu(p);
1861 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001862 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
1863 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02001864 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001865
1866 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001867
1868#ifdef CONFIG_SCHEDSTATS
1869 if (p->se.wait_start)
1870 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001871 if (p->se.sleep_start)
1872 p->se.sleep_start -= clock_offset;
1873 if (p->se.block_start)
1874 p->se.block_start -= clock_offset;
Ingo Molnar6c594c22008-12-14 12:34:15 +01001875#endif
Ingo Molnarcc367732007-10-15 17:00:18 +02001876 if (old_cpu != new_cpu) {
Ingo Molnar6c594c22008-12-14 12:34:15 +01001877 p->se.nr_migrations++;
1878#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02001879 if (task_hot(p, old_rq->clock, NULL))
1880 schedstat_inc(p, se.nr_forced2_migrations);
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001881#endif
Ingo Molnar6c594c22008-12-14 12:34:15 +01001882 }
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001883 p->se.vruntime -= old_cfsrq->min_vruntime -
1884 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02001885
1886 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001887}
1888
Ingo Molnar70b97a72006-07-03 00:25:42 -07001889struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Ingo Molnar36c8b582006-07-03 00:25:41 -07001892 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 int dest_cpu;
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001896};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898/*
1899 * The task's runqueue lock must be held.
1900 * Returns true if you have to wait for migration thread.
1901 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001902static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07001903migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001905 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907 /*
1908 * If the task is not on a runqueue (and not running), then
1909 * it is sufficient to simply update the task's cpu field.
1910 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001911 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 set_task_cpu(p, dest_cpu);
1913 return 0;
1914 }
1915
1916 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 req->task = p;
1918 req->dest_cpu = dest_cpu;
1919 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07001920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 return 1;
1922}
1923
1924/*
1925 * wait_task_inactive - wait for a thread to unschedule.
1926 *
Roland McGrath85ba2d82008-07-25 19:45:58 -07001927 * If @match_state is nonzero, it's the @p->state value just checked and
1928 * not expected to change. If it changes, i.e. @p might have woken up,
1929 * then return zero. When we succeed in waiting for @p to be off its CPU,
1930 * we return a positive number (its total switch count). If a second call
1931 * a short while later returns the same number, the caller can be sure that
1932 * @p has remained unscheduled the whole time.
1933 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 * The caller must ensure that the task *will* unschedule sometime soon,
1935 * else this function might spin for a *long* time. This function can't
1936 * be called with interrupts off, or it may introduce deadlock with
1937 * smp_call_function() if an IPI is sent by the same process we are
1938 * waiting to become inactive.
1939 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07001940unsigned long wait_task_inactive(struct task_struct *p, long match_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
1942 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001943 int running, on_rq;
Roland McGrath85ba2d82008-07-25 19:45:58 -07001944 unsigned long ncsw;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001945 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Andi Kleen3a5c3592007-10-15 17:00:14 +02001947 for (;;) {
1948 /*
1949 * We do the initial early heuristics without holding
1950 * any task-queue locks at all. We'll only try to get
1951 * the runqueue lock when things look like they will
1952 * work out!
1953 */
1954 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001955
Andi Kleen3a5c3592007-10-15 17:00:14 +02001956 /*
1957 * If the task is actively running on another CPU
1958 * still, just relax and busy-wait without holding
1959 * any locks.
1960 *
1961 * NOTE! Since we don't hold any locks, it's not
1962 * even sure that "rq" stays as the right runqueue!
1963 * But we don't care, since "task_running()" will
1964 * return false if the runqueue has changed and p
1965 * is actually now running somewhere else!
1966 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07001967 while (task_running(rq, p)) {
1968 if (match_state && unlikely(p->state != match_state))
1969 return 0;
Andi Kleen3a5c3592007-10-15 17:00:14 +02001970 cpu_relax();
Roland McGrath85ba2d82008-07-25 19:45:58 -07001971 }
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001972
Andi Kleen3a5c3592007-10-15 17:00:14 +02001973 /*
1974 * Ok, time to look more closely! We need the rq
1975 * lock now, to be *sure*. If we're wrong, we'll
1976 * just go back and repeat.
1977 */
1978 rq = task_rq_lock(p, &flags);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04001979 trace_sched_wait_task(rq, p);
Andi Kleen3a5c3592007-10-15 17:00:14 +02001980 running = task_running(rq, p);
1981 on_rq = p->se.on_rq;
Roland McGrath85ba2d82008-07-25 19:45:58 -07001982 ncsw = 0;
Oleg Nesterovf31e11d2008-08-20 16:54:44 -07001983 if (!match_state || p->state == match_state)
Oleg Nesterov93dcf552008-08-20 16:54:44 -07001984 ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
Andi Kleen3a5c3592007-10-15 17:00:14 +02001985 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001986
Andi Kleen3a5c3592007-10-15 17:00:14 +02001987 /*
Roland McGrath85ba2d82008-07-25 19:45:58 -07001988 * If it changed from the expected state, bail out now.
1989 */
1990 if (unlikely(!ncsw))
1991 break;
1992
1993 /*
Andi Kleen3a5c3592007-10-15 17:00:14 +02001994 * Was it really running after all now that we
1995 * checked with the proper locks actually held?
1996 *
1997 * Oops. Go back and try again..
1998 */
1999 if (unlikely(running)) {
2000 cpu_relax();
2001 continue;
2002 }
2003
2004 /*
2005 * It's not enough that it's not actively running,
2006 * it must be off the runqueue _entirely_, and not
2007 * preempted!
2008 *
2009 * So if it wa still runnable (but just not actively
2010 * running right now), it's preempted, and we should
2011 * yield - it could be a while.
2012 */
2013 if (unlikely(on_rq)) {
2014 schedule_timeout_uninterruptible(1);
2015 continue;
2016 }
2017
2018 /*
2019 * Ahh, all good. It wasn't running, and it wasn't
2020 * runnable, which means that it will never become
2021 * running in the future either. We're all done!
2022 */
2023 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
Roland McGrath85ba2d82008-07-25 19:45:58 -07002025
2026 return ncsw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027}
2028
2029/***
2030 * kick_process - kick a running thread to enter/exit the kernel
2031 * @p: the to-be-kicked thread
2032 *
2033 * Cause a process which is running on another CPU to enter
2034 * kernel-mode, without any delay. (to get signals handled.)
2035 *
2036 * NOTE: this function doesnt have to take the runqueue lock,
2037 * because all it wants to ensure is that the remote task enters
2038 * the kernel. If the IPI races and the task has been migrated
2039 * to another CPU then no harm is done and the purpose has been
2040 * achieved as well.
2041 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002042void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
2044 int cpu;
2045
2046 preempt_disable();
2047 cpu = task_cpu(p);
2048 if ((cpu != smp_processor_id()) && task_curr(p))
2049 smp_send_reschedule(cpu);
2050 preempt_enable();
2051}
2052
2053/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002054 * Return a low guess at the load of a migration-source cpu weighted
2055 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 *
2057 * We want to under-estimate the load of migration sources, to
2058 * balance conservatively.
2059 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002060static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002061{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002062 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002063 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002064
Peter Zijlstra93b75212008-06-27 13:41:33 +02002065 if (type == 0 || !sched_feat(LB_BIAS))
Ingo Molnardd41f592007-07-09 18:51:59 +02002066 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002067
Ingo Molnardd41f592007-07-09 18:51:59 +02002068 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
2071/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002072 * Return a high guess at the load of a migration-target cpu weighted
2073 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002075static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002076{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002077 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002078 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002079
Peter Zijlstra93b75212008-06-27 13:41:33 +02002080 if (type == 0 || !sched_feat(LB_BIAS))
Ingo Molnardd41f592007-07-09 18:51:59 +02002081 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002082
Ingo Molnardd41f592007-07-09 18:51:59 +02002083 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07002084}
2085
2086/*
Nick Piggin147cbb42005-06-25 14:57:19 -07002087 * find_idlest_group finds and returns the least busy CPU group within the
2088 * domain.
2089 */
2090static struct sched_group *
2091find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
2092{
2093 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
2094 unsigned long min_load = ULONG_MAX, this_load = 0;
2095 int load_idx = sd->forkexec_idx;
2096 int imbalance = 100 + (sd->imbalance_pct-100)/2;
2097
2098 do {
2099 unsigned long load, avg_load;
2100 int local_group;
2101 int i;
2102
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002103 /* Skip over this group if it has no CPUs allowed */
2104 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02002105 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002106
Nick Piggin147cbb42005-06-25 14:57:19 -07002107 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07002108
2109 /* Tally up the load of all CPUs in the group */
2110 avg_load = 0;
2111
Mike Travis363ab6f2008-05-12 21:21:13 +02002112 for_each_cpu_mask_nr(i, group->cpumask) {
Nick Piggin147cbb42005-06-25 14:57:19 -07002113 /* Bias balancing toward cpus of our domain */
2114 if (local_group)
2115 load = source_load(i, load_idx);
2116 else
2117 load = target_load(i, load_idx);
2118
2119 avg_load += load;
2120 }
2121
2122 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002123 avg_load = sg_div_cpu_power(group,
2124 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07002125
2126 if (local_group) {
2127 this_load = avg_load;
2128 this = group;
2129 } else if (avg_load < min_load) {
2130 min_load = avg_load;
2131 idlest = group;
2132 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02002133 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07002134
2135 if (!idlest || 100*this_load < imbalance*min_load)
2136 return NULL;
2137 return idlest;
2138}
2139
2140/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07002141 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07002142 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002143static int
Mike Travis7c16ec52008-04-04 18:11:11 -07002144find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu,
2145 cpumask_t *tmp)
Nick Piggin147cbb42005-06-25 14:57:19 -07002146{
2147 unsigned long load, min_load = ULONG_MAX;
2148 int idlest = -1;
2149 int i;
2150
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002151 /* Traverse only the allowed CPUs */
Mike Travis7c16ec52008-04-04 18:11:11 -07002152 cpus_and(*tmp, group->cpumask, p->cpus_allowed);
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002153
Mike Travis363ab6f2008-05-12 21:21:13 +02002154 for_each_cpu_mask_nr(i, *tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002155 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07002156
2157 if (load < min_load || (load == min_load && i == this_cpu)) {
2158 min_load = load;
2159 idlest = i;
2160 }
2161 }
2162
2163 return idlest;
2164}
2165
Nick Piggin476d1392005-06-25 14:57:29 -07002166/*
2167 * sched_balance_self: balance the current task (running on cpu) in domains
2168 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2169 * SD_BALANCE_EXEC.
2170 *
2171 * Balance, ie. select the least loaded group.
2172 *
2173 * Returns the target CPU number, or the same CPU if no balancing is needed.
2174 *
2175 * preempt must be disabled.
2176 */
2177static int sched_balance_self(int cpu, int flag)
2178{
2179 struct task_struct *t = current;
2180 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07002181
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002182 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02002183 /*
2184 * If power savings logic is enabled for a domain, stop there.
2185 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002186 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2187 break;
Nick Piggin476d1392005-06-25 14:57:29 -07002188 if (tmp->flags & flag)
2189 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002190 }
Nick Piggin476d1392005-06-25 14:57:29 -07002191
Peter Zijlstra039a1c412008-06-27 13:41:25 +02002192 if (sd)
2193 update_shares(sd);
2194
Nick Piggin476d1392005-06-25 14:57:29 -07002195 while (sd) {
Mike Travis7c16ec52008-04-04 18:11:11 -07002196 cpumask_t span, tmpmask;
Nick Piggin476d1392005-06-25 14:57:29 -07002197 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002198 int new_cpu, weight;
2199
2200 if (!(sd->flags & flag)) {
2201 sd = sd->child;
2202 continue;
2203 }
Nick Piggin476d1392005-06-25 14:57:29 -07002204
2205 span = sd->span;
2206 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002207 if (!group) {
2208 sd = sd->child;
2209 continue;
2210 }
Nick Piggin476d1392005-06-25 14:57:29 -07002211
Mike Travis7c16ec52008-04-04 18:11:11 -07002212 new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002213 if (new_cpu == -1 || new_cpu == cpu) {
2214 /* Now try balancing at a lower domain level of cpu */
2215 sd = sd->child;
2216 continue;
2217 }
Nick Piggin476d1392005-06-25 14:57:29 -07002218
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002219 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07002220 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07002221 sd = NULL;
2222 weight = cpus_weight(span);
2223 for_each_domain(cpu, tmp) {
2224 if (weight <= cpus_weight(tmp->span))
2225 break;
2226 if (tmp->flags & flag)
2227 sd = tmp;
2228 }
2229 /* while loop will break here if sd == NULL */
2230 }
2231
2232 return cpu;
2233}
2234
2235#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Thomas Gleixner0793a612008-12-04 20:12:29 +01002237/**
2238 * task_oncpu_function_call - call a function on the cpu on which a task runs
2239 * @p: the task to evaluate
2240 * @func: the function to be called
2241 * @info: the function call argument
2242 *
2243 * Calls the function @func when the task is currently running. This might
2244 * be on the current CPU, which just calls the function directly
2245 */
2246void task_oncpu_function_call(struct task_struct *p,
2247 void (*func) (void *info), void *info)
2248{
2249 int cpu;
2250
2251 preempt_disable();
2252 cpu = task_cpu(p);
2253 if (task_curr(p))
2254 smp_call_function_single(cpu, func, info, 1);
2255 preempt_enable();
2256}
2257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258/***
2259 * try_to_wake_up - wake up a thread
2260 * @p: the to-be-woken-up thread
2261 * @state: the mask of task states that can be woken
2262 * @sync: do a synchronous wakeup?
2263 *
2264 * Put it on the run-queue if it's not already there. The "current"
2265 * thread is always on the run-queue (except when the actual
2266 * re-schedule is in progress), and as such you're allowed to do
2267 * the simpler "current->state = TASK_RUNNING" to mark yourself
2268 * runnable without the overhead of this.
2269 *
2270 * returns failure only if the task is already active.
2271 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002272static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
Ingo Molnarcc367732007-10-15 17:00:18 +02002274 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 unsigned long flags;
2276 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002277 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Ingo Molnarb85d0662008-03-16 20:03:22 +01002279 if (!sched_feat(SYNC_WAKEUPS))
2280 sync = 0;
2281
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02002282#ifdef CONFIG_SMP
2283 if (sched_feat(LB_WAKEUP_UPDATE)) {
2284 struct sched_domain *sd;
2285
2286 this_cpu = raw_smp_processor_id();
2287 cpu = task_cpu(p);
2288
2289 for_each_domain(this_cpu, sd) {
2290 if (cpu_isset(cpu, sd->span)) {
2291 update_shares(sd);
2292 break;
2293 }
2294 }
2295 }
2296#endif
2297
Linus Torvalds04e2f172008-02-23 18:05:03 -08002298 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 rq = task_rq_lock(p, &flags);
2300 old_state = p->state;
2301 if (!(old_state & state))
2302 goto out;
2303
Ingo Molnardd41f592007-07-09 18:51:59 +02002304 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 goto out_running;
2306
2307 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002308 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 this_cpu = smp_processor_id();
2310
2311#ifdef CONFIG_SMP
2312 if (unlikely(task_running(rq, p)))
2313 goto out_activate;
2314
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01002315 cpu = p->sched_class->select_task_rq(p, sync);
2316 if (cpu != orig_cpu) {
2317 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 task_rq_unlock(rq, &flags);
2319 /* might preempt at this point */
2320 rq = task_rq_lock(p, &flags);
2321 old_state = p->state;
2322 if (!(old_state & state))
2323 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02002324 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 goto out_running;
2326
2327 this_cpu = smp_processor_id();
2328 cpu = task_cpu(p);
2329 }
2330
Gregory Haskinse7693a32008-01-25 21:08:09 +01002331#ifdef CONFIG_SCHEDSTATS
2332 schedstat_inc(rq, ttwu_count);
2333 if (cpu == this_cpu)
2334 schedstat_inc(rq, ttwu_local);
2335 else {
2336 struct sched_domain *sd;
2337 for_each_domain(this_cpu, sd) {
2338 if (cpu_isset(cpu, sd->span)) {
2339 schedstat_inc(sd, ttwu_wake_remote);
2340 break;
2341 }
2342 }
2343 }
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002344#endif /* CONFIG_SCHEDSTATS */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346out_activate:
2347#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02002348 schedstat_inc(p, se.nr_wakeups);
2349 if (sync)
2350 schedstat_inc(p, se.nr_wakeups_sync);
2351 if (orig_cpu != cpu)
2352 schedstat_inc(p, se.nr_wakeups_migrate);
2353 if (cpu == this_cpu)
2354 schedstat_inc(p, se.nr_wakeups_local);
2355 else
2356 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02002357 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02002358 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 success = 1;
2360
2361out_running:
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04002362 trace_sched_wakeup(rq, p);
Peter Zijlstra15afe092008-09-20 23:38:02 +02002363 check_preempt_curr(rq, p, sync);
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002366#ifdef CONFIG_SMP
2367 if (p->sched_class->task_wake_up)
2368 p->sched_class->task_wake_up(rq, p);
2369#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370out:
Gregory Haskins2087a1a2008-06-27 14:30:00 -06002371 current->se.last_wakeup = current->se.sum_exec_runtime;
2372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 task_rq_unlock(rq, &flags);
2374
2375 return success;
2376}
2377
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002378int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002380 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382EXPORT_SYMBOL(wake_up_process);
2383
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002384int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385{
2386 return try_to_wake_up(p, state, 0);
2387}
2388
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389/*
2390 * Perform scheduler related setup for a newly forked process p.
2391 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002392 *
2393 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002395static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
Ingo Molnardd41f592007-07-09 18:51:59 +02002397 p->se.exec_start = 0;
2398 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002399 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002400 p->se.nr_migrations = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002401 p->se.last_wakeup = 0;
2402 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002403
2404#ifdef CONFIG_SCHEDSTATS
2405 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002406 p->se.sum_sleep_runtime = 0;
2407 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002408 p->se.block_start = 0;
2409 p->se.sleep_max = 0;
2410 p->se.block_max = 0;
2411 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002412 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002413 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002414#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002415
Peter Zijlstrafa717062008-01-25 21:08:27 +01002416 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002417 p->se.on_rq = 0;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02002418 INIT_LIST_HEAD(&p->se.group_node);
Nick Piggin476d1392005-06-25 14:57:29 -07002419
Avi Kivitye107be32007-07-26 13:40:43 +02002420#ifdef CONFIG_PREEMPT_NOTIFIERS
2421 INIT_HLIST_HEAD(&p->preempt_notifiers);
2422#endif
2423
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 /*
2425 * We mark the process as running here, but have not actually
2426 * inserted it onto the runqueue yet. This guarantees that
2427 * nobody will actually run it, and a signal or other external
2428 * event cannot wake it up and insert it on the runqueue either.
2429 */
2430 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002431}
2432
2433/*
2434 * fork()/clone()-time setup:
2435 */
2436void sched_fork(struct task_struct *p, int clone_flags)
2437{
2438 int cpu = get_cpu();
2439
2440 __sched_fork(p);
2441
2442#ifdef CONFIG_SMP
2443 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2444#endif
Ingo Molnar02e4bac22007-10-15 17:00:11 +02002445 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002446
2447 /*
2448 * Make sure we do not leak PI boosting priority to the child:
2449 */
2450 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002451 if (!rt_prio(p->prio))
2452 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002453
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002454#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002455 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002456 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002458#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002459 p->oncpu = 0;
2460#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002462 /* Want to start with kernel preemption disabled. */
Al Viroa1261f542005-11-13 16:06:55 -08002463 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002465 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466}
2467
2468/*
2469 * wake_up_new_task - wake up a newly created task for the first time.
2470 *
2471 * This function will do some initial scheduler statistics housekeeping
2472 * that must be done for every newly created context, then puts the task
2473 * on the runqueue and wakes it.
2474 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002475void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
2477 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002478 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
2480 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002482 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
2484 p->prio = effective_prio(p);
2485
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002486 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002487 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002490 * Let the scheduling class do new task startup
2491 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002493 p->sched_class->task_new(rq, p);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02002494 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 }
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04002496 trace_sched_wakeup_new(rq, p);
Peter Zijlstra15afe092008-09-20 23:38:02 +02002497 check_preempt_curr(rq, p, 0);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002498#ifdef CONFIG_SMP
2499 if (p->sched_class->task_wake_up)
2500 p->sched_class->task_wake_up(rq, p);
2501#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002502 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
Avi Kivitye107be32007-07-26 13:40:43 +02002505#ifdef CONFIG_PREEMPT_NOTIFIERS
2506
2507/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002508 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2509 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002510 */
2511void preempt_notifier_register(struct preempt_notifier *notifier)
2512{
2513 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2514}
2515EXPORT_SYMBOL_GPL(preempt_notifier_register);
2516
2517/**
2518 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002519 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002520 *
2521 * This is safe to call from within a preemption notifier.
2522 */
2523void preempt_notifier_unregister(struct preempt_notifier *notifier)
2524{
2525 hlist_del(&notifier->link);
2526}
2527EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2528
2529static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2530{
2531 struct preempt_notifier *notifier;
2532 struct hlist_node *node;
2533
2534 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2535 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2536}
2537
2538static void
2539fire_sched_out_preempt_notifiers(struct task_struct *curr,
2540 struct task_struct *next)
2541{
2542 struct preempt_notifier *notifier;
2543 struct hlist_node *node;
2544
2545 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2546 notifier->ops->sched_out(notifier, next);
2547}
2548
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002549#else /* !CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002550
2551static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2552{
2553}
2554
2555static void
2556fire_sched_out_preempt_notifiers(struct task_struct *curr,
2557 struct task_struct *next)
2558{
2559}
2560
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002561#endif /* CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002564 * prepare_task_switch - prepare to switch tasks
2565 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002566 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002567 * @next: the task we are going to switch to.
2568 *
2569 * This is called with the rq lock held and interrupts off. It must
2570 * be paired with a subsequent finish_task_switch after the context
2571 * switch.
2572 *
2573 * prepare_task_switch sets up locking and calls architecture specific
2574 * hooks.
2575 */
Avi Kivitye107be32007-07-26 13:40:43 +02002576static inline void
2577prepare_task_switch(struct rq *rq, struct task_struct *prev,
2578 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002579{
Avi Kivitye107be32007-07-26 13:40:43 +02002580 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002581 prepare_lock_switch(rq, next);
2582 prepare_arch_switch(next);
2583}
2584
2585/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002587 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 * @prev: the thread we just switched away from.
2589 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002590 * finish_task_switch must be called after the context switch, paired
2591 * with a prepare_task_switch call before the context switch.
2592 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2593 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 *
2595 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002596 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 * with the lock held can cause deadlocks; see schedule() for
2598 * details.)
2599 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002600static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 __releases(rq->lock)
2602{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002604 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
2606 rq->prev_mm = NULL;
2607
2608 /*
2609 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002610 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002611 * schedule one last time. The schedule call will never return, and
2612 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002613 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 * still held, otherwise prev could be scheduled on another cpu, die
2615 * there before we look at prev->state, and then the reference would
2616 * be dropped twice.
2617 * Manfred Spraul <manfred@colorfullife.com>
2618 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002619 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002620 finish_arch_switch(prev);
Thomas Gleixner0793a612008-12-04 20:12:29 +01002621 perf_counter_task_sched_in(current, cpu_of(rq));
Nick Piggin4866cde2005-06-25 14:57:23 -07002622 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002623#ifdef CONFIG_SMP
2624 if (current->sched_class->post_schedule)
2625 current->sched_class->post_schedule(rq);
2626#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002627
Avi Kivitye107be32007-07-26 13:40:43 +02002628 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 if (mm)
2630 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002631 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002632 /*
2633 * Remove function-return probe instances associated with this
2634 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002635 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002636 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639}
2640
2641/**
2642 * schedule_tail - first thing a freshly forked thread must call.
2643 * @prev: the thread we just switched away from.
2644 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002645asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 __releases(rq->lock)
2647{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002648 struct rq *rq = this_rq();
2649
Nick Piggin4866cde2005-06-25 14:57:23 -07002650 finish_task_switch(rq, prev);
2651#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2652 /* In this case, finish_task_switch does not reenable preemption */
2653 preempt_enable();
2654#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002656 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657}
2658
2659/*
2660 * context_switch - switch to the new MM and the new
2661 * thread's register state.
2662 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002663static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002664context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002665 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666{
Ingo Molnardd41f592007-07-09 18:51:59 +02002667 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Avi Kivitye107be32007-07-26 13:40:43 +02002669 prepare_task_switch(rq, prev, next);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04002670 trace_sched_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002671 mm = next->mm;
2672 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002673 /*
2674 * For paravirt, this is coupled with an exit in switch_to to
2675 * combine the page table reload and the switch backend into
2676 * one hypercall.
2677 */
2678 arch_enter_lazy_cpu_mode();
2679
Ingo Molnardd41f592007-07-09 18:51:59 +02002680 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 next->active_mm = oldmm;
2682 atomic_inc(&oldmm->mm_count);
2683 enter_lazy_tlb(oldmm, next);
2684 } else
2685 switch_mm(oldmm, mm, next);
2686
Ingo Molnardd41f592007-07-09 18:51:59 +02002687 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 rq->prev_mm = oldmm;
2690 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002691 /*
2692 * Since the runqueue lock will be released by the next
2693 * task (which is an invalid locking op but in the case
2694 * of the scheduler it's an obvious special-case), so we
2695 * do an early lockdep release here:
2696 */
2697#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002698 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002699#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
2701 /* Here we just switch the register state and the stack. */
2702 switch_to(prev, next, prev);
2703
Ingo Molnardd41f592007-07-09 18:51:59 +02002704 barrier();
2705 /*
2706 * this_rq must be evaluated again because prev may have moved
2707 * CPUs since it called schedule(), thus the 'rq' on its stack
2708 * frame will be invalid.
2709 */
2710 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711}
2712
2713/*
2714 * nr_running, nr_uninterruptible and nr_context_switches:
2715 *
2716 * externally visible scheduler statistics: current number of runnable
2717 * threads, current number of uninterruptible-sleeping threads, total
2718 * number of context switches performed since bootup.
2719 */
2720unsigned long nr_running(void)
2721{
2722 unsigned long i, sum = 0;
2723
2724 for_each_online_cpu(i)
2725 sum += cpu_rq(i)->nr_running;
2726
2727 return sum;
2728}
2729
2730unsigned long nr_uninterruptible(void)
2731{
2732 unsigned long i, sum = 0;
2733
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002734 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 sum += cpu_rq(i)->nr_uninterruptible;
2736
2737 /*
2738 * Since we read the counters lockless, it might be slightly
2739 * inaccurate. Do not allow it to go below zero though:
2740 */
2741 if (unlikely((long)sum < 0))
2742 sum = 0;
2743
2744 return sum;
2745}
2746
2747unsigned long long nr_context_switches(void)
2748{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002749 int i;
2750 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002752 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 sum += cpu_rq(i)->nr_switches;
2754
2755 return sum;
2756}
2757
2758unsigned long nr_iowait(void)
2759{
2760 unsigned long i, sum = 0;
2761
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002762 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2764
2765 return sum;
2766}
2767
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002768unsigned long nr_active(void)
2769{
2770 unsigned long i, running = 0, uninterruptible = 0;
2771
2772 for_each_online_cpu(i) {
2773 running += cpu_rq(i)->nr_running;
2774 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2775 }
2776
2777 if (unlikely((long)uninterruptible < 0))
2778 uninterruptible = 0;
2779
2780 return running + uninterruptible;
2781}
2782
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002784 * Update rq->cpu_load[] statistics. This function is usually called every
2785 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002786 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002787static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002788{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002789 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002790 int i, scale;
2791
2792 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002793
2794 /* Update our load: */
2795 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2796 unsigned long old_load, new_load;
2797
2798 /* scale is effectively 1 << i now, and >> i divides by scale */
2799
2800 old_load = this_rq->cpu_load[i];
2801 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002802 /*
2803 * Round up the averaging division if load is increasing. This
2804 * prevents us from getting stuck on 9 if the load is 10, for
2805 * example.
2806 */
2807 if (new_load > old_load)
2808 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002809 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2810 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002811}
2812
Ingo Molnardd41f592007-07-09 18:51:59 +02002813#ifdef CONFIG_SMP
2814
Ingo Molnar48f24c42006-07-03 00:25:40 -07002815/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 * double_rq_lock - safely lock two runqueues
2817 *
2818 * Note this does not disable interrupts like task_rq_lock,
2819 * you need to do so manually before calling.
2820 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002821static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 __acquires(rq1->lock)
2823 __acquires(rq2->lock)
2824{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002825 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 if (rq1 == rq2) {
2827 spin_lock(&rq1->lock);
2828 __acquire(rq2->lock); /* Fake it out ;) */
2829 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002830 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 spin_lock(&rq1->lock);
Peter Zijlstra5e710e32008-07-30 13:26:57 +02002832 spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 } else {
2834 spin_lock(&rq2->lock);
Peter Zijlstra5e710e32008-07-30 13:26:57 +02002835 spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 }
2837 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002838 update_rq_clock(rq1);
2839 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840}
2841
2842/*
2843 * double_rq_unlock - safely unlock two runqueues
2844 *
2845 * Note this does not restore interrupts like task_rq_unlock,
2846 * you need to do so manually after calling.
2847 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002848static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 __releases(rq1->lock)
2850 __releases(rq2->lock)
2851{
2852 spin_unlock(&rq1->lock);
2853 if (rq1 != rq2)
2854 spin_unlock(&rq2->lock);
2855 else
2856 __release(rq2->lock);
2857}
2858
2859/*
2860 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2861 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01002862static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 __releases(this_rq->lock)
2864 __acquires(busiest->lock)
2865 __acquires(this_rq->lock)
2866{
Steven Rostedte8fa1362008-01-25 21:08:05 +01002867 int ret = 0;
2868
Kirill Korotaev054b9102006-12-10 02:20:11 -08002869 if (unlikely(!irqs_disabled())) {
2870 /* printk() doesn't work good under rq->lock */
2871 spin_unlock(&this_rq->lock);
2872 BUG_ON(1);
2873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002875 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 spin_unlock(&this_rq->lock);
2877 spin_lock(&busiest->lock);
Peter Zijlstra5e710e32008-07-30 13:26:57 +02002878 spin_lock_nested(&this_rq->lock, SINGLE_DEPTH_NESTING);
Steven Rostedte8fa1362008-01-25 21:08:05 +01002879 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 } else
Peter Zijlstra5e710e32008-07-30 13:26:57 +02002881 spin_lock_nested(&busiest->lock, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01002883 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884}
2885
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02002886static void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
2887 __releases(busiest->lock)
2888{
2889 spin_unlock(&busiest->lock);
2890 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
2891}
2892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 * If dest_cpu is allowed for this process, migrate the task to it.
2895 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002896 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 * the cpu_allowed mask is restored.
2898 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002899static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002901 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002903 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
2905 rq = task_rq_lock(p, &flags);
2906 if (!cpu_isset(dest_cpu, p->cpus_allowed)
Max Krasnyanskye761b772008-07-15 04:43:49 -07002907 || unlikely(!cpu_active(dest_cpu)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 goto out;
2909
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04002910 trace_sched_migrate_task(rq, p, dest_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 /* force the process onto the specified CPU */
2912 if (migrate_task(p, dest_cpu, &req)) {
2913 /* Need to wait for migration thread (might exit: take ref). */
2914 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07002915
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 get_task_struct(mt);
2917 task_rq_unlock(rq, &flags);
2918 wake_up_process(mt);
2919 put_task_struct(mt);
2920 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07002921
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 return;
2923 }
2924out:
2925 task_rq_unlock(rq, &flags);
2926}
2927
2928/*
Nick Piggin476d1392005-06-25 14:57:29 -07002929 * sched_exec - execve() is a valuable balancing opportunity, because at
2930 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 */
2932void sched_exec(void)
2933{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002935 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002937 if (new_cpu != this_cpu)
2938 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939}
2940
2941/*
2942 * pull_task - move a task from a remote runqueue to the local runqueue.
2943 * Both runqueues must be locked.
2944 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002945static void pull_task(struct rq *src_rq, struct task_struct *p,
2946 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02002948 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002950 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 /*
2952 * Note that idle threads have a prio of MAX_PRIO, for this test
2953 * to be always true for them.
2954 */
Peter Zijlstra15afe092008-09-20 23:38:02 +02002955 check_preempt_curr(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956}
2957
2958/*
2959 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
2960 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002961static
Ingo Molnar70b97a72006-07-03 00:25:42 -07002962int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002963 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002964 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965{
2966 /*
2967 * We do not migrate tasks that are:
2968 * 1) running (obviously), or
2969 * 2) cannot be migrated to this CPU due to cpus_allowed, or
2970 * 3) are cache-hot on their current CPU.
2971 */
Ingo Molnarcc367732007-10-15 17:00:18 +02002972 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
2973 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002975 }
Nick Piggin81026792005-06-25 14:57:07 -07002976 *all_pinned = 0;
2977
Ingo Molnarcc367732007-10-15 17:00:18 +02002978 if (task_running(rq, p)) {
2979 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07002980 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
Ingo Molnarda84d962007-10-15 17:00:18 +02002983 /*
2984 * Aggressive migration if:
2985 * 1) task is cache cold, or
2986 * 2) too many balance attempts have failed.
2987 */
2988
Ingo Molnar6bc16652007-10-15 17:00:18 +02002989 if (!task_hot(p, rq->clock, sd) ||
2990 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002991#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02002992 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002993 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02002994 schedstat_inc(p, se.nr_forced_migrations);
2995 }
Ingo Molnarda84d962007-10-15 17:00:18 +02002996#endif
2997 return 1;
2998 }
2999
Ingo Molnarcc367732007-10-15 17:00:18 +02003000 if (task_hot(p, rq->clock, sd)) {
3001 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02003002 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02003003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 return 1;
3005}
3006
Peter Williamse1d14842007-10-24 18:23:51 +02003007static unsigned long
3008balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3009 unsigned long max_load_move, struct sched_domain *sd,
3010 enum cpu_idle_type idle, int *all_pinned,
3011 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02003012{
Peter Zijlstra051c6762008-06-27 13:41:31 +02003013 int loops = 0, pulled = 0, pinned = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02003014 struct task_struct *p;
3015 long rem_load_move = max_load_move;
3016
Peter Williamse1d14842007-10-24 18:23:51 +02003017 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02003018 goto out;
3019
3020 pinned = 1;
3021
3022 /*
3023 * Start the load-balancing iterator:
3024 */
3025 p = iterator->start(iterator->arg);
3026next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003027 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02003028 goto out;
Peter Zijlstra051c6762008-06-27 13:41:31 +02003029
3030 if ((p->se.load.weight >> 1) > rem_load_move ||
Ingo Molnardd41f592007-07-09 18:51:59 +02003031 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003032 p = iterator->next(iterator->arg);
3033 goto next;
3034 }
3035
3036 pull_task(busiest, p, this_rq, this_cpu);
3037 pulled++;
3038 rem_load_move -= p->se.load.weight;
3039
3040 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01003041 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003042 */
Peter Williamse1d14842007-10-24 18:23:51 +02003043 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003044 if (p->prio < *this_best_prio)
3045 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02003046 p = iterator->next(iterator->arg);
3047 goto next;
3048 }
3049out:
3050 /*
Peter Williamse1d14842007-10-24 18:23:51 +02003051 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02003052 * so we can safely collect pull_task() stats here rather than
3053 * inside pull_task().
3054 */
3055 schedstat_add(sd, lb_gained[idle], pulled);
3056
3057 if (all_pinned)
3058 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02003059
3060 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02003061}
Ingo Molnar48f24c42006-07-03 00:25:40 -07003062
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063/*
Peter Williams43010652007-08-09 11:16:46 +02003064 * move_tasks tries to move up to max_load_move weighted load from busiest to
3065 * this_rq, as part of a balancing operation within domain "sd".
3066 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 *
3068 * Called with both runqueues locked.
3069 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003070static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02003071 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003072 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07003073 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003075 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02003076 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003077 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
Ingo Molnardd41f592007-07-09 18:51:59 +02003079 do {
Peter Williams43010652007-08-09 11:16:46 +02003080 total_load_moved +=
3081 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02003082 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003083 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02003084 class = class->next;
Gregory Haskinsc4acb2c2008-06-27 14:29:55 -06003085
3086 if (idle == CPU_NEWLY_IDLE && this_rq->nr_running)
3087 break;
3088
Peter Williams43010652007-08-09 11:16:46 +02003089 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Peter Williams43010652007-08-09 11:16:46 +02003091 return total_load_moved > 0;
3092}
3093
Peter Williamse1d14842007-10-24 18:23:51 +02003094static int
3095iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3096 struct sched_domain *sd, enum cpu_idle_type idle,
3097 struct rq_iterator *iterator)
3098{
3099 struct task_struct *p = iterator->start(iterator->arg);
3100 int pinned = 0;
3101
3102 while (p) {
3103 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3104 pull_task(busiest, p, this_rq, this_cpu);
3105 /*
3106 * Right now, this is only the second place pull_task()
3107 * is called, so we can safely collect pull_task()
3108 * stats here rather than inside pull_task().
3109 */
3110 schedstat_inc(sd, lb_gained[idle]);
3111
3112 return 1;
3113 }
3114 p = iterator->next(iterator->arg);
3115 }
3116
3117 return 0;
3118}
3119
Peter Williams43010652007-08-09 11:16:46 +02003120/*
3121 * move_one_task tries to move exactly one task from busiest to this_rq, as
3122 * part of active balancing operations within "domain".
3123 * Returns 1 if successful and 0 otherwise.
3124 *
3125 * Called with both runqueues locked.
3126 */
3127static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3128 struct sched_domain *sd, enum cpu_idle_type idle)
3129{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003130 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02003131
3132 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02003133 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02003134 return 1;
3135
3136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137}
3138
3139/*
3140 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07003141 * domain. It calculates and returns the amount of weighted load which
3142 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 */
3144static struct sched_group *
3145find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02003146 unsigned long *imbalance, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003147 int *sd_idle, const cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148{
3149 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
3150 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003151 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07003152 unsigned long busiest_load_per_task, busiest_nr_running;
3153 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003154 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003155#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3156 int power_savings_balance = 1;
3157 unsigned long leader_nr_running = 0, min_load_per_task = 0;
3158 unsigned long min_nr_running = ULONG_MAX;
3159 struct sched_group *group_min = NULL, *group_leader = NULL;
3160#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
3162 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003163 busiest_load_per_task = busiest_nr_running = 0;
3164 this_load_per_task = this_nr_running = 0;
Peter Zijlstra408ed062008-06-27 13:41:28 +02003165
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003166 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003167 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003168 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003169 load_idx = sd->newidle_idx;
3170 else
3171 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
3173 do {
Ken Chen908a7c12007-10-17 16:55:11 +02003174 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 int local_group;
3176 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02003177 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003178 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003179 unsigned long sum_nr_running, sum_weighted_load;
Peter Zijlstra408ed062008-06-27 13:41:28 +02003180 unsigned long sum_avg_load_per_task;
3181 unsigned long avg_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
3183 local_group = cpu_isset(this_cpu, group->cpumask);
3184
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003185 if (local_group)
3186 balance_cpu = first_cpu(group->cpumask);
3187
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07003189 sum_weighted_load = sum_nr_running = avg_load = 0;
Peter Zijlstra408ed062008-06-27 13:41:28 +02003190 sum_avg_load_per_task = avg_load_per_task = 0;
3191
Ken Chen908a7c12007-10-17 16:55:11 +02003192 max_cpu_load = 0;
3193 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Mike Travis363ab6f2008-05-12 21:21:13 +02003195 for_each_cpu_mask_nr(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003196 struct rq *rq;
3197
3198 if (!cpu_isset(i, *cpus))
3199 continue;
3200
3201 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07003202
Suresh Siddha9439aab2007-07-19 21:28:35 +02003203 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07003204 *sd_idle = 0;
3205
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003207 if (local_group) {
3208 if (idle_cpu(i) && !first_idle_cpu) {
3209 first_idle_cpu = 1;
3210 balance_cpu = i;
3211 }
3212
Nick Piggina2000572006-02-10 01:51:02 -08003213 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003214 } else {
Nick Piggina2000572006-02-10 01:51:02 -08003215 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003216 if (load > max_cpu_load)
3217 max_cpu_load = load;
3218 if (min_cpu_load > load)
3219 min_cpu_load = load;
3220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
3222 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07003223 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003224 sum_weighted_load += weighted_cpuload(i);
Peter Zijlstra408ed062008-06-27 13:41:28 +02003225
3226 sum_avg_load_per_task += cpu_avg_load_per_task(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 }
3228
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003229 /*
3230 * First idle cpu or the first cpu(busiest) in this sched group
3231 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02003232 * domains. In the newly idle case, we will allow all the cpu's
3233 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003234 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02003235 if (idle != CPU_NEWLY_IDLE && local_group &&
3236 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003237 *balance = 0;
3238 goto ret;
3239 }
3240
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07003242 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243
3244 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07003245 avg_load = sg_div_cpu_power(group,
3246 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
Peter Zijlstra408ed062008-06-27 13:41:28 +02003248
3249 /*
3250 * Consider the group unbalanced when the imbalance is larger
3251 * than the average weight of two tasks.
3252 *
3253 * APZ: with cgroup the avg task weight can vary wildly and
3254 * might not be a suitable number - should we keep a
3255 * normalized nr_running number somewhere that negates
3256 * the hierarchy?
3257 */
3258 avg_load_per_task = sg_div_cpu_power(group,
3259 sum_avg_load_per_task * SCHED_LOAD_SCALE);
3260
3261 if ((max_cpu_load - min_cpu_load) > 2*avg_load_per_task)
Ken Chen908a7c12007-10-17 16:55:11 +02003262 __group_imb = 1;
3263
Eric Dumazet5517d862007-05-08 00:32:57 -07003264 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003265
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 if (local_group) {
3267 this_load = avg_load;
3268 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003269 this_nr_running = sum_nr_running;
3270 this_load_per_task = sum_weighted_load;
3271 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02003272 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 max_load = avg_load;
3274 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003275 busiest_nr_running = sum_nr_running;
3276 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02003277 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003279
3280#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3281 /*
3282 * Busy processors will not participate in power savings
3283 * balance.
3284 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003285 if (idle == CPU_NOT_IDLE ||
3286 !(sd->flags & SD_POWERSAVINGS_BALANCE))
3287 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003288
3289 /*
3290 * If the local group is idle or completely loaded
3291 * no need to do power savings balance at this domain
3292 */
3293 if (local_group && (this_nr_running >= group_capacity ||
3294 !this_nr_running))
3295 power_savings_balance = 0;
3296
Ingo Molnardd41f592007-07-09 18:51:59 +02003297 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003298 * If a group is already running at full capacity or idle,
3299 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02003300 */
3301 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003302 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02003303 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003304
Ingo Molnardd41f592007-07-09 18:51:59 +02003305 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003306 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003307 * This is the group from where we need to pick up the load
3308 * for saving power
3309 */
3310 if ((sum_nr_running < min_nr_running) ||
3311 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003312 first_cpu(group->cpumask) <
3313 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003314 group_min = group;
3315 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003316 min_load_per_task = sum_weighted_load /
3317 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003318 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003319
Ingo Molnardd41f592007-07-09 18:51:59 +02003320 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003321 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02003322 * capacity but still has some space to pick up some load
3323 * from other group and save more power
3324 */
3325 if (sum_nr_running <= group_capacity - 1) {
3326 if (sum_nr_running > leader_nr_running ||
3327 (sum_nr_running == leader_nr_running &&
3328 first_cpu(group->cpumask) >
3329 first_cpu(group_leader->cpumask))) {
3330 group_leader = group;
3331 leader_nr_running = sum_nr_running;
3332 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003333 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003334group_next:
3335#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 group = group->next;
3337 } while (group != sd->groups);
3338
Peter Williams2dd73a42006-06-27 02:54:34 -07003339 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 goto out_balanced;
3341
3342 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
3343
3344 if (this_load >= avg_load ||
3345 100*max_load <= sd->imbalance_pct*this_load)
3346 goto out_balanced;
3347
Peter Williams2dd73a42006-06-27 02:54:34 -07003348 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003349 if (group_imb)
3350 busiest_load_per_task = min(busiest_load_per_task, avg_load);
3351
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 /*
3353 * We're trying to get all the cpus to the average_load, so we don't
3354 * want to push ourselves above the average load, nor do we wish to
3355 * reduce the max loaded cpu below the average load, as either of these
3356 * actions would just result in more rebalancing later, and ping-pong
3357 * tasks around. Thus we look for the minimum possible imbalance.
3358 * Negative imbalances (*we* are more loaded than anyone else) will
3359 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003360 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 * appear as very large values with unsigned longs.
3362 */
Peter Williams2dd73a42006-06-27 02:54:34 -07003363 if (max_load <= busiest_load_per_task)
3364 goto out_balanced;
3365
3366 /*
3367 * In the presence of smp nice balancing, certain scenarios can have
3368 * max load less than avg load(as we skip the groups at or below
3369 * its cpu_power, while calculating max_load..)
3370 */
3371 if (max_load < avg_load) {
3372 *imbalance = 0;
3373 goto small_imbalance;
3374 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003375
3376 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07003377 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003378
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07003380 *imbalance = min(max_pull * busiest->__cpu_power,
3381 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 / SCHED_LOAD_SCALE;
3383
Peter Williams2dd73a42006-06-27 02:54:34 -07003384 /*
3385 * if *imbalance is less than the average load per runnable task
3386 * there is no gaurantee that any tasks will be moved so we'll have
3387 * a think about bumping its value to force at least one task to be
3388 * moved
3389 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003390 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003391 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07003392 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Peter Williams2dd73a42006-06-27 02:54:34 -07003394small_imbalance:
3395 pwr_move = pwr_now = 0;
3396 imbn = 2;
3397 if (this_nr_running) {
3398 this_load_per_task /= this_nr_running;
3399 if (busiest_load_per_task > this_load_per_task)
3400 imbn = 1;
3401 } else
Peter Zijlstra408ed062008-06-27 13:41:28 +02003402 this_load_per_task = cpu_avg_load_per_task(this_cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07003403
Peter Zijlstra01c8c572008-10-24 11:06:12 +02003404 if (max_load - this_load + busiest_load_per_task >=
Ingo Molnardd41f592007-07-09 18:51:59 +02003405 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07003406 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 return busiest;
3408 }
3409
3410 /*
3411 * OK, we don't have enough imbalance to justify moving tasks,
3412 * however we may be able to increase total CPU power used by
3413 * moving them.
3414 */
3415
Eric Dumazet5517d862007-05-08 00:32:57 -07003416 pwr_now += busiest->__cpu_power *
3417 min(busiest_load_per_task, max_load);
3418 pwr_now += this->__cpu_power *
3419 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 pwr_now /= SCHED_LOAD_SCALE;
3421
3422 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07003423 tmp = sg_div_cpu_power(busiest,
3424 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07003426 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07003427 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
3429 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07003430 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003431 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07003432 tmp = sg_div_cpu_power(this,
3433 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 else
Eric Dumazet5517d862007-05-08 00:32:57 -07003435 tmp = sg_div_cpu_power(this,
3436 busiest_load_per_task * SCHED_LOAD_SCALE);
3437 pwr_move += this->__cpu_power *
3438 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 pwr_move /= SCHED_LOAD_SCALE;
3440
3441 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003442 if (pwr_move > pwr_now)
3443 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 }
3445
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 return busiest;
3447
3448out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003449#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003450 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003451 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003453 if (this == group_leader && group_leader != group_min) {
3454 *imbalance = min_load_per_task;
3455 return group_min;
3456 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003457#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003458ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 *imbalance = 0;
3460 return NULL;
3461}
3462
3463/*
3464 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3465 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003466static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003467find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003468 unsigned long imbalance, const cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003470 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003471 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 int i;
3473
Mike Travis363ab6f2008-05-12 21:21:13 +02003474 for_each_cpu_mask_nr(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003475 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003476
3477 if (!cpu_isset(i, *cpus))
3478 continue;
3479
Ingo Molnar48f24c42006-07-03 00:25:40 -07003480 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003481 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
Ingo Molnardd41f592007-07-09 18:51:59 +02003483 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003484 continue;
3485
Ingo Molnardd41f592007-07-09 18:51:59 +02003486 if (wl > max_load) {
3487 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003488 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 }
3490 }
3491
3492 return busiest;
3493}
3494
3495/*
Nick Piggin77391d72005-06-25 14:57:30 -07003496 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3497 * so long as it is large enough.
3498 */
3499#define MAX_PINNED_INTERVAL 512
3500
3501/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3503 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003505static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003506 struct sched_domain *sd, enum cpu_idle_type idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003507 int *balance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508{
Peter Williams43010652007-08-09 11:16:46 +02003509 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003512 struct rq *busiest;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003513 unsigned long flags;
Nick Piggin5969fe02005-09-10 00:26:19 -07003514
Mike Travis7c16ec52008-04-04 18:11:11 -07003515 cpus_setall(*cpus);
3516
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003517 /*
3518 * When power savings policy is enabled for the parent domain, idle
3519 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003520 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003521 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003522 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003523 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003524 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003525 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
Ingo Molnar2d723762007-10-15 17:00:12 +02003527 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003529redo:
Peter Zijlstrac8cba852008-06-27 13:41:23 +02003530 update_shares(sd);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003531 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003532 cpus, balance);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003533
Chen, Kenneth W06066712006-12-10 02:20:35 -08003534 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003535 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003536
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 if (!group) {
3538 schedstat_inc(sd, lb_nobusyg[idle]);
3539 goto out_balanced;
3540 }
3541
Mike Travis7c16ec52008-04-04 18:11:11 -07003542 busiest = find_busiest_queue(group, idle, imbalance, cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 if (!busiest) {
3544 schedstat_inc(sd, lb_nobusyq[idle]);
3545 goto out_balanced;
3546 }
3547
Nick Piggindb935db2005-06-25 14:57:11 -07003548 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
3550 schedstat_add(sd, lb_imbalance[idle], imbalance);
3551
Peter Williams43010652007-08-09 11:16:46 +02003552 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 if (busiest->nr_running > 1) {
3554 /*
3555 * Attempt to move tasks. If find_busiest_group has found
3556 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003557 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 * correctly treated as an imbalance.
3559 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003560 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003561 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003562 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003563 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003564 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003565 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003566
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003567 /*
3568 * some other cpu did the load balance for us.
3569 */
Peter Williams43010652007-08-09 11:16:46 +02003570 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003571 resched_cpu(this_cpu);
3572
Nick Piggin81026792005-06-25 14:57:07 -07003573 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003574 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003575 cpu_clear(cpu_of(busiest), *cpus);
3576 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003577 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003578 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 }
Nick Piggin81026792005-06-25 14:57:07 -07003581
Peter Williams43010652007-08-09 11:16:46 +02003582 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 schedstat_inc(sd, lb_failed[idle]);
3584 sd->nr_balance_failed++;
3585
3586 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003588 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003589
3590 /* don't kick the migration_thread, if the curr
3591 * task on busiest cpu can't be moved to this_cpu
3592 */
3593 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003594 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003595 all_pinned = 1;
3596 goto out_one_pinned;
3597 }
3598
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 if (!busiest->active_balance) {
3600 busiest->active_balance = 1;
3601 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003602 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003604 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003605 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 wake_up_process(busiest->migration_thread);
3607
3608 /*
3609 * We've kicked active balancing, reset the failure
3610 * counter.
3611 */
Nick Piggin39507452005-06-25 14:57:09 -07003612 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 }
Nick Piggin81026792005-06-25 14:57:07 -07003614 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 sd->nr_balance_failed = 0;
3616
Nick Piggin81026792005-06-25 14:57:07 -07003617 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 /* We were unbalanced, so reset the balancing interval */
3619 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003620 } else {
3621 /*
3622 * If we've begun active balancing, start to back off. This
3623 * case may not be covered by the all_pinned logic if there
3624 * is only 1 task on the busy runqueue (because we don't call
3625 * move_tasks).
3626 */
3627 if (sd->balance_interval < sd->max_interval)
3628 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 }
3630
Peter Williams43010652007-08-09 11:16:46 +02003631 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003632 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstrac09595f2008-06-27 13:41:14 +02003633 ld_moved = -1;
3634
3635 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
3637out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 schedstat_inc(sd, lb_balanced[idle]);
3639
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003640 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003641
3642out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003644 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3645 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 sd->balance_interval *= 2;
3647
Ingo Molnar48f24c42006-07-03 00:25:40 -07003648 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003649 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstrac09595f2008-06-27 13:41:14 +02003650 ld_moved = -1;
3651 else
3652 ld_moved = 0;
3653out:
Peter Zijlstrac8cba852008-06-27 13:41:23 +02003654 if (ld_moved)
3655 update_shares(sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02003656 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657}
3658
3659/*
3660 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3661 * tasks if there is an imbalance.
3662 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003663 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 * this_rq is locked.
3665 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003666static int
Mike Travis7c16ec52008-04-04 18:11:11 -07003667load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
3668 cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669{
3670 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003671 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003673 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003674 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003675 int all_pinned = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07003676
3677 cpus_setall(*cpus);
Nick Piggin5969fe02005-09-10 00:26:19 -07003678
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003679 /*
3680 * When power savings policy is enabled for the parent domain, idle
3681 * sibling can pick up load irrespective of busy siblings. In this case,
3682 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003683 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003684 */
3685 if (sd->flags & SD_SHARE_CPUPOWER &&
3686 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003687 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
Ingo Molnar2d723762007-10-15 17:00:12 +02003689 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003690redo:
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02003691 update_shares_locked(this_rq, sd);
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003692 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Mike Travis7c16ec52008-04-04 18:11:11 -07003693 &sd_idle, cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003695 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003696 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 }
3698
Mike Travis7c16ec52008-04-04 18:11:11 -07003699 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003700 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003701 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003702 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 }
3704
Nick Piggindb935db2005-06-25 14:57:11 -07003705 BUG_ON(busiest == this_rq);
3706
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003707 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003708
Peter Williams43010652007-08-09 11:16:46 +02003709 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003710 if (busiest->nr_running > 1) {
3711 /* Attempt to move tasks */
3712 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003713 /* this_rq->clock is already updated */
3714 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003715 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003716 imbalance, sd, CPU_NEWLY_IDLE,
3717 &all_pinned);
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02003718 double_unlock_balance(this_rq, busiest);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003719
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003720 if (unlikely(all_pinned)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003721 cpu_clear(cpu_of(busiest), *cpus);
3722 if (!cpus_empty(*cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003723 goto redo;
3724 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003725 }
3726
Peter Williams43010652007-08-09 11:16:46 +02003727 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003728 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003729 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3730 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003731 return -1;
3732 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003733 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02003735 update_shares_locked(this_rq, sd);
Peter Williams43010652007-08-09 11:16:46 +02003736 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003737
3738out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003739 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003740 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003741 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003742 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003743 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003744
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003745 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746}
3747
3748/*
3749 * idle_balance is called by schedule() if this_cpu is about to become
3750 * idle. Attempts to pull tasks from other CPUs.
3751 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003752static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753{
3754 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003755 int pulled_task = -1;
3756 unsigned long next_balance = jiffies + HZ;
Mike Travis7c16ec52008-04-04 18:11:11 -07003757 cpumask_t tmpmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
3759 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003760 unsigned long interval;
3761
3762 if (!(sd->flags & SD_LOAD_BALANCE))
3763 continue;
3764
3765 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003766 /* If we've pulled tasks over stop searching: */
Mike Travis7c16ec52008-04-04 18:11:11 -07003767 pulled_task = load_balance_newidle(this_cpu, this_rq,
3768 sd, &tmpmask);
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003769
3770 interval = msecs_to_jiffies(sd->balance_interval);
3771 if (time_after(next_balance, sd->last_balance + interval))
3772 next_balance = sd->last_balance + interval;
3773 if (pulled_task)
3774 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003776 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003777 /*
3778 * We are going idle. next_balance may be set based on
3779 * a busy processor. So reset next_balance.
3780 */
3781 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783}
3784
3785/*
3786 * active_load_balance is run by migration threads. It pushes running tasks
3787 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3788 * running on each physical CPU where possible, and avoids physical /
3789 * logical imbalances.
3790 *
3791 * Called with busiest_rq locked.
3792 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003793static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794{
Nick Piggin39507452005-06-25 14:57:09 -07003795 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003796 struct sched_domain *sd;
3797 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003798
Ingo Molnar48f24c42006-07-03 00:25:40 -07003799 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003800 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003801 return;
3802
3803 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
3805 /*
Nick Piggin39507452005-06-25 14:57:09 -07003806 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003807 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003808 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 */
Nick Piggin39507452005-06-25 14:57:09 -07003810 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811
Nick Piggin39507452005-06-25 14:57:09 -07003812 /* move a task from busiest_rq to target_rq */
3813 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003814 update_rq_clock(busiest_rq);
3815 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816
Nick Piggin39507452005-06-25 14:57:09 -07003817 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003818 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07003819 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003820 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07003821 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
Ingo Molnar48f24c42006-07-03 00:25:40 -07003824 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003825 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
Peter Williams43010652007-08-09 11:16:46 +02003827 if (move_one_task(target_rq, target_cpu, busiest_rq,
3828 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07003829 schedstat_inc(sd, alb_pushed);
3830 else
3831 schedstat_inc(sd, alb_failed);
3832 }
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02003833 double_unlock_balance(busiest_rq, target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834}
3835
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003836#ifdef CONFIG_NO_HZ
3837static struct {
3838 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003839 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003840} nohz ____cacheline_aligned = {
3841 .load_balancer = ATOMIC_INIT(-1),
3842 .cpu_mask = CPU_MASK_NONE,
3843};
3844
Christoph Lameter7835b982006-12-10 02:20:22 -08003845/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003846 * This routine will try to nominate the ilb (idle load balancing)
3847 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3848 * load balancing on behalf of all those cpus. If all the cpus in the system
3849 * go into this tickless mode, then there will be no ilb owner (as there is
3850 * no need for one) and all the cpus will sleep till the next wakeup event
3851 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08003852 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003853 * For the ilb owner, tick is not stopped. And this tick will be used
3854 * for idle load balancing. ilb owner will still be part of
3855 * nohz.cpu_mask..
3856 *
3857 * While stopping the tick, this cpu will become the ilb owner if there
3858 * is no other owner. And will be the owner till that cpu becomes busy
3859 * or if all cpus in the system stop their ticks at which point
3860 * there is no need for ilb owner.
3861 *
3862 * When the ilb owner becomes busy, it nominates another owner, during the
3863 * next busy scheduler_tick()
3864 */
3865int select_nohz_load_balancer(int stop_tick)
3866{
3867 int cpu = smp_processor_id();
3868
3869 if (stop_tick) {
3870 cpu_set(cpu, nohz.cpu_mask);
3871 cpu_rq(cpu)->in_nohz_recently = 1;
3872
3873 /*
3874 * If we are going offline and still the leader, give up!
3875 */
Max Krasnyanskye761b772008-07-15 04:43:49 -07003876 if (!cpu_active(cpu) &&
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003877 atomic_read(&nohz.load_balancer) == cpu) {
3878 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3879 BUG();
3880 return 0;
3881 }
3882
3883 /* time for ilb owner also to sleep */
3884 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3885 if (atomic_read(&nohz.load_balancer) == cpu)
3886 atomic_set(&nohz.load_balancer, -1);
3887 return 0;
3888 }
3889
3890 if (atomic_read(&nohz.load_balancer) == -1) {
3891 /* make me the ilb owner */
3892 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
3893 return 1;
3894 } else if (atomic_read(&nohz.load_balancer) == cpu)
3895 return 1;
3896 } else {
3897 if (!cpu_isset(cpu, nohz.cpu_mask))
3898 return 0;
3899
3900 cpu_clear(cpu, nohz.cpu_mask);
3901
3902 if (atomic_read(&nohz.load_balancer) == cpu)
3903 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3904 BUG();
3905 }
3906 return 0;
3907}
3908#endif
3909
3910static DEFINE_SPINLOCK(balancing);
3911
3912/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003913 * It checks each scheduling domain to see if it is due to be balanced,
3914 * and initiates a balancing operation if so.
3915 *
3916 * Balancing parameters are set up in arch_init_sched_domains.
3917 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003918static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08003919{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003920 int balance = 1;
3921 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08003922 unsigned long interval;
3923 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003924 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08003925 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003926 int update_next_balance = 0;
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02003927 int need_serialize;
Mike Travis7c16ec52008-04-04 18:11:11 -07003928 cpumask_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003930 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 if (!(sd->flags & SD_LOAD_BALANCE))
3932 continue;
3933
3934 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003935 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 interval *= sd->busy_factor;
3937
3938 /* scale ms to jiffies */
3939 interval = msecs_to_jiffies(interval);
3940 if (unlikely(!interval))
3941 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003942 if (interval > HZ*NR_CPUS/10)
3943 interval = HZ*NR_CPUS/10;
3944
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02003945 need_serialize = sd->flags & SD_SERIALIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02003947 if (need_serialize) {
Christoph Lameter08c183f2006-12-10 02:20:29 -08003948 if (!spin_trylock(&balancing))
3949 goto out;
3950 }
3951
Christoph Lameterc9819f42006-12-10 02:20:25 -08003952 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07003953 if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003954 /*
3955 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07003956 * longer idle, or one of our SMT siblings is
3957 * not idle.
3958 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003959 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003961 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 }
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02003963 if (need_serialize)
Christoph Lameter08c183f2006-12-10 02:20:29 -08003964 spin_unlock(&balancing);
3965out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02003966 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08003967 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003968 update_next_balance = 1;
3969 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003970
3971 /*
3972 * Stop the load balance at this level. There is another
3973 * CPU in our sched group which is doing load balancing more
3974 * actively.
3975 */
3976 if (!balance)
3977 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02003979
3980 /*
3981 * next_balance will be updated only when there is a need.
3982 * When the cpu is attached to null domain for ex, it will not be
3983 * updated.
3984 */
3985 if (likely(update_next_balance))
3986 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003987}
3988
3989/*
3990 * run_rebalance_domains is triggered when needed from the scheduler tick.
3991 * In CONFIG_NO_HZ case, the idle load balance owner will do the
3992 * rebalancing for all the cpus for whom scheduler ticks are stopped.
3993 */
3994static void run_rebalance_domains(struct softirq_action *h)
3995{
Ingo Molnardd41f592007-07-09 18:51:59 +02003996 int this_cpu = smp_processor_id();
3997 struct rq *this_rq = cpu_rq(this_cpu);
3998 enum cpu_idle_type idle = this_rq->idle_at_tick ?
3999 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004000
Ingo Molnardd41f592007-07-09 18:51:59 +02004001 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004002
4003#ifdef CONFIG_NO_HZ
4004 /*
4005 * If this cpu is the owner for idle load balancing, then do the
4006 * balancing on behalf of the other idle cpus whose ticks are
4007 * stopped.
4008 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004009 if (this_rq->idle_at_tick &&
4010 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004011 cpumask_t cpus = nohz.cpu_mask;
4012 struct rq *rq;
4013 int balance_cpu;
4014
Ingo Molnardd41f592007-07-09 18:51:59 +02004015 cpu_clear(this_cpu, cpus);
Mike Travis363ab6f2008-05-12 21:21:13 +02004016 for_each_cpu_mask_nr(balance_cpu, cpus) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004017 /*
4018 * If this cpu gets work to do, stop the load balancing
4019 * work being done for other cpus. Next load
4020 * balancing owner will pick it up.
4021 */
4022 if (need_resched())
4023 break;
4024
Oleg Nesterovde0cf892007-08-12 18:08:19 +02004025 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004026
4027 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004028 if (time_after(this_rq->next_balance, rq->next_balance))
4029 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004030 }
4031 }
4032#endif
4033}
4034
4035/*
4036 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
4037 *
4038 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
4039 * idle load balancing owner or decide to stop the periodic load balancing,
4040 * if the whole system is idle.
4041 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004042static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004043{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004044#ifdef CONFIG_NO_HZ
4045 /*
4046 * If we were in the nohz mode recently and busy at the current
4047 * scheduler tick, then check if we need to nominate new idle
4048 * load balancer.
4049 */
4050 if (rq->in_nohz_recently && !rq->idle_at_tick) {
4051 rq->in_nohz_recently = 0;
4052
4053 if (atomic_read(&nohz.load_balancer) == cpu) {
4054 cpu_clear(cpu, nohz.cpu_mask);
4055 atomic_set(&nohz.load_balancer, -1);
4056 }
4057
4058 if (atomic_read(&nohz.load_balancer) == -1) {
4059 /*
4060 * simple selection for now: Nominate the
4061 * first cpu in the nohz list to be the next
4062 * ilb owner.
4063 *
4064 * TBD: Traverse the sched domains and nominate
4065 * the nearest cpu in the nohz.cpu_mask.
4066 */
4067 int ilb = first_cpu(nohz.cpu_mask);
4068
Mike Travis434d53b2008-04-04 18:11:04 -07004069 if (ilb < nr_cpu_ids)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004070 resched_cpu(ilb);
4071 }
4072 }
4073
4074 /*
4075 * If this cpu is idle and doing idle load balancing for all the
4076 * cpus with ticks stopped, is it time for that to stop?
4077 */
4078 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
4079 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
4080 resched_cpu(cpu);
4081 return;
4082 }
4083
4084 /*
4085 * If this cpu is idle and the idle load balancing is done by
4086 * someone else, then no need raise the SCHED_SOFTIRQ
4087 */
4088 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
4089 cpu_isset(cpu, nohz.cpu_mask))
4090 return;
4091#endif
4092 if (time_after_eq(jiffies, rq->next_balance))
4093 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094}
Ingo Molnardd41f592007-07-09 18:51:59 +02004095
4096#else /* CONFIG_SMP */
4097
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098/*
4099 * on UP we do not need to balance between CPUs:
4100 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004101static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102{
4103}
Ingo Molnardd41f592007-07-09 18:51:59 +02004104
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105#endif
4106
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107DEFINE_PER_CPU(struct kernel_stat, kstat);
4108
4109EXPORT_PER_CPU_SYMBOL(kstat);
4110
4111/*
Frank Mayharf06febc2008-09-12 09:54:39 -07004112 * Return any ns on the sched_clock that have not yet been banked in
4113 * @p in case that task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 */
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01004115unsigned long long __task_delta_exec(struct task_struct *p, int update)
4116{
4117 s64 delta_exec;
4118 struct rq *rq;
4119
4120 rq = task_rq(p);
4121 WARN_ON_ONCE(!runqueue_is_locked());
4122 WARN_ON_ONCE(!task_current(rq, p));
4123
4124 if (update)
4125 update_rq_clock(rq);
4126
4127 delta_exec = rq->clock - p->se.exec_start;
4128
4129 WARN_ON_ONCE(delta_exec < 0);
4130
4131 return delta_exec;
4132}
4133
4134/*
4135 * Return any ns on the sched_clock that have not yet been banked in
4136 * @p in case that task is currently running.
4137 */
Frank Mayharbb34d922008-09-12 09:54:39 -07004138unsigned long long task_delta_exec(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004141 struct rq *rq;
Frank Mayharbb34d922008-09-12 09:54:39 -07004142 u64 ns = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004143
Ingo Molnar41b86e92007-07-09 18:51:58 +02004144 rq = task_rq_lock(p, &flags);
Ingo Molnar15084872008-09-30 08:28:17 +02004145
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004146 if (task_current(rq, p)) {
Frank Mayharf06febc2008-09-12 09:54:39 -07004147 u64 delta_exec;
4148
Ingo Molnara8e504d2007-08-09 11:16:47 +02004149 update_rq_clock(rq);
4150 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004151 if ((s64)delta_exec > 0)
Frank Mayharbb34d922008-09-12 09:54:39 -07004152 ns = delta_exec;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004153 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07004154
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 task_rq_unlock(rq, &flags);
4156
4157 return ns;
4158}
4159
4160/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 * Account user cpu time to a process.
4162 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 * @cputime: the cpu time spent in user space since the last update
4164 */
4165void account_user_time(struct task_struct *p, cputime_t cputime)
4166{
4167 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4168 cputime64_t tmp;
4169
4170 p->utime = cputime_add(p->utime, cputime);
Frank Mayharf06febc2008-09-12 09:54:39 -07004171 account_group_user_time(p, cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
4173 /* Add user time to cpustat. */
4174 tmp = cputime_to_cputime64(cputime);
4175 if (TASK_NICE(p) > 0)
4176 cpustat->nice = cputime64_add(cpustat->nice, tmp);
4177 else
4178 cpustat->user = cputime64_add(cpustat->user, tmp);
Jonathan Lim49b5cf32008-07-25 01:48:40 -07004179 /* Account for user time used */
4180 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181}
4182
4183/*
Laurent Vivier94886b82007-10-15 17:00:19 +02004184 * Account guest cpu time to a process.
4185 * @p: the process that the cpu time gets accounted to
4186 * @cputime: the cpu time spent in virtual machine since the last update
4187 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01004188static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02004189{
4190 cputime64_t tmp;
4191 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4192
4193 tmp = cputime_to_cputime64(cputime);
4194
4195 p->utime = cputime_add(p->utime, cputime);
Frank Mayharf06febc2008-09-12 09:54:39 -07004196 account_group_user_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02004197 p->gtime = cputime_add(p->gtime, cputime);
4198
4199 cpustat->user = cputime64_add(cpustat->user, tmp);
4200 cpustat->guest = cputime64_add(cpustat->guest, tmp);
4201}
4202
4203/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004204 * Account scaled user cpu time to a process.
4205 * @p: the process that the cpu time gets accounted to
4206 * @cputime: the cpu time spent in user space since the last update
4207 */
4208void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
4209{
4210 p->utimescaled = cputime_add(p->utimescaled, cputime);
4211}
4212
4213/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 * Account system cpu time to a process.
4215 * @p: the process that the cpu time gets accounted to
4216 * @hardirq_offset: the offset to subtract from hardirq_count()
4217 * @cputime: the cpu time spent in kernel space since the last update
4218 */
4219void account_system_time(struct task_struct *p, int hardirq_offset,
4220 cputime_t cputime)
4221{
4222 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004223 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 cputime64_t tmp;
4225
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004226 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
4227 account_guest_time(p, cputime);
4228 return;
4229 }
Laurent Vivier94886b82007-10-15 17:00:19 +02004230
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 p->stime = cputime_add(p->stime, cputime);
Frank Mayharf06febc2008-09-12 09:54:39 -07004232 account_group_system_time(p, cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233
4234 /* Add system time to cpustat. */
4235 tmp = cputime_to_cputime64(cputime);
4236 if (hardirq_count() - hardirq_offset)
4237 cpustat->irq = cputime64_add(cpustat->irq, tmp);
4238 else if (softirq_count())
4239 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004240 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004242 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4244 else
4245 cpustat->idle = cputime64_add(cpustat->idle, tmp);
4246 /* Account for system time used */
4247 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248}
4249
4250/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004251 * Account scaled system cpu time to a process.
4252 * @p: the process that the cpu time gets accounted to
4253 * @hardirq_offset: the offset to subtract from hardirq_count()
4254 * @cputime: the cpu time spent in kernel space since the last update
4255 */
4256void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
4257{
4258 p->stimescaled = cputime_add(p->stimescaled, cputime);
4259}
4260
4261/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 * Account for involuntary wait time.
4263 * @p: the process from which the cpu time has been stolen
4264 * @steal: the cpu time spent in involuntary wait
4265 */
4266void account_steal_time(struct task_struct *p, cputime_t steal)
4267{
4268 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4269 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07004270 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
4272 if (p == rq->idle) {
4273 p->stime = cputime_add(p->stime, steal);
Frank Mayharf06febc2008-09-12 09:54:39 -07004274 account_group_system_time(p, steal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 if (atomic_read(&rq->nr_iowait) > 0)
4276 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4277 else
4278 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004279 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 cpustat->steal = cputime64_add(cpustat->steal, tmp);
4281}
4282
Christoph Lameter7835b982006-12-10 02:20:22 -08004283/*
Balbir Singh49048622008-09-05 18:12:23 +02004284 * Use precise platform statistics if available:
4285 */
4286#ifdef CONFIG_VIRT_CPU_ACCOUNTING
4287cputime_t task_utime(struct task_struct *p)
4288{
4289 return p->utime;
4290}
4291
4292cputime_t task_stime(struct task_struct *p)
4293{
4294 return p->stime;
4295}
4296#else
4297cputime_t task_utime(struct task_struct *p)
4298{
4299 clock_t utime = cputime_to_clock_t(p->utime),
4300 total = utime + cputime_to_clock_t(p->stime);
4301 u64 temp;
4302
4303 /*
4304 * Use CFS's precise accounting:
4305 */
4306 temp = (u64)nsec_to_clock_t(p->se.sum_exec_runtime);
4307
4308 if (total) {
4309 temp *= utime;
4310 do_div(temp, total);
4311 }
4312 utime = (clock_t)temp;
4313
4314 p->prev_utime = max(p->prev_utime, clock_t_to_cputime(utime));
4315 return p->prev_utime;
4316}
4317
4318cputime_t task_stime(struct task_struct *p)
4319{
4320 clock_t stime;
4321
4322 /*
4323 * Use CFS's precise accounting. (we subtract utime from
4324 * the total, to make sure the total observed by userspace
4325 * grows monotonically - apps rely on that):
4326 */
4327 stime = nsec_to_clock_t(p->se.sum_exec_runtime) -
4328 cputime_to_clock_t(task_utime(p));
4329
4330 if (stime >= 0)
4331 p->prev_stime = max(p->prev_stime, clock_t_to_cputime(stime));
4332
4333 return p->prev_stime;
4334}
4335#endif
4336
4337inline cputime_t task_gtime(struct task_struct *p)
4338{
4339 return p->gtime;
4340}
4341
4342/*
Christoph Lameter7835b982006-12-10 02:20:22 -08004343 * This function gets called by the timer code, with HZ frequency.
4344 * We call it with interrupts disabled.
4345 *
4346 * It also gets called by the fork code, when changing the parent's
4347 * timeslices.
4348 */
4349void scheduler_tick(void)
4350{
Christoph Lameter7835b982006-12-10 02:20:22 -08004351 int cpu = smp_processor_id();
4352 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004353 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004354
4355 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08004356
Ingo Molnardd41f592007-07-09 18:51:59 +02004357 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004358 update_rq_clock(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02004359 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01004360 curr->sched_class->task_tick(rq, curr, 0);
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01004361 perf_counter_task_tick(curr, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004362 spin_unlock(&rq->lock);
4363
Christoph Lametere418e1c2006-12-10 02:20:23 -08004364#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02004365 rq->idle_at_tick = idle_cpu(cpu);
4366 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08004367#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368}
4369
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004370#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
4371 defined(CONFIG_PREEMPT_TRACER))
4372
4373static inline unsigned long get_parent_ip(unsigned long addr)
4374{
4375 if (in_lock_functions(addr)) {
4376 addr = CALLER_ADDR2;
4377 if (in_lock_functions(addr))
4378 addr = CALLER_ADDR3;
4379 }
4380 return addr;
4381}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382
Srinivasa Ds43627582008-02-23 15:24:04 -08004383void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004385#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 /*
4387 * Underflow?
4388 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004389 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4390 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004391#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 preempt_count() += val;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004393#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 /*
4395 * Spinlock count overflowing soon?
4396 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08004397 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
4398 PREEMPT_MASK - 10);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004399#endif
4400 if (preempt_count() == val)
4401 trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402}
4403EXPORT_SYMBOL(add_preempt_count);
4404
Srinivasa Ds43627582008-02-23 15:24:04 -08004405void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004407#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 /*
4409 * Underflow?
4410 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004411 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
4412 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 /*
4414 * Is the spinlock portion underflowing?
4415 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004416 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
4417 !(preempt_count() & PREEMPT_MASK)))
4418 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004419#endif
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004420
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004421 if (preempt_count() == val)
4422 trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 preempt_count() -= val;
4424}
4425EXPORT_SYMBOL(sub_preempt_count);
4426
4427#endif
4428
4429/*
Ingo Molnardd41f592007-07-09 18:51:59 +02004430 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004432static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433{
Satyam Sharma838225b2007-10-24 18:23:50 +02004434 struct pt_regs *regs = get_irq_regs();
4435
4436 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4437 prev->comm, prev->pid, preempt_count());
4438
Ingo Molnardd41f592007-07-09 18:51:59 +02004439 debug_show_held_locks(prev);
Arjan van de Vene21f5b12008-05-23 09:05:58 -07004440 print_modules();
Ingo Molnardd41f592007-07-09 18:51:59 +02004441 if (irqs_disabled())
4442 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02004443
4444 if (regs)
4445 show_regs(regs);
4446 else
4447 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02004448}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449
Ingo Molnardd41f592007-07-09 18:51:59 +02004450/*
4451 * Various schedule()-time debugging checks and statistics:
4452 */
4453static inline void schedule_debug(struct task_struct *prev)
4454{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004456 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 * schedule() atomically, we ignore that path for now.
4458 * Otherwise, whine if we are scheduling when we should not be.
4459 */
Roel Kluin3f33a7c2008-05-13 23:44:11 +02004460 if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
Ingo Molnardd41f592007-07-09 18:51:59 +02004461 __schedule_bug(prev);
4462
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4464
Ingo Molnar2d723762007-10-15 17:00:12 +02004465 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004466#ifdef CONFIG_SCHEDSTATS
4467 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004468 schedstat_inc(this_rq(), bkl_count);
4469 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004470 }
4471#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004472}
4473
4474/*
4475 * Pick up the highest-prio task:
4476 */
4477static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004478pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02004479{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004480 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004481 struct task_struct *p;
4482
4483 /*
4484 * Optimization: we know that if all tasks are in
4485 * the fair class we can call that function directly:
4486 */
4487 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004488 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004489 if (likely(p))
4490 return p;
4491 }
4492
4493 class = sched_class_highest;
4494 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004495 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004496 if (p)
4497 return p;
4498 /*
4499 * Will never be NULL as the idle class always
4500 * returns a non-NULL p:
4501 */
4502 class = class->next;
4503 }
4504}
4505
4506/*
4507 * schedule() is the main scheduler function.
4508 */
4509asmlinkage void __sched schedule(void)
4510{
4511 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004512 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02004513 struct rq *rq;
Peter Zijlstra31656512008-07-18 18:01:23 +02004514 int cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02004515
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516need_resched:
4517 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02004518 cpu = smp_processor_id();
4519 rq = cpu_rq(cpu);
4520 rcu_qsctr_inc(cpu);
4521 prev = rq->curr;
4522 switch_count = &prev->nivcsw;
4523
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 release_kernel_lock(prev);
4525need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526
Ingo Molnardd41f592007-07-09 18:51:59 +02004527 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528
Peter Zijlstra31656512008-07-18 18:01:23 +02004529 if (sched_feat(HRTICK))
Mike Galbraithf333fdc2008-05-12 21:20:55 +02004530 hrtick_clear(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004531
Peter Zijlstra8cd162c2008-10-15 20:37:23 +02004532 spin_lock_irq(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004533 update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02004534 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535
Ingo Molnardd41f592007-07-09 18:51:59 +02004536 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
Oleg Nesterov16882c12008-06-08 21:20:41 +04004537 if (unlikely(signal_pending_state(prev->state, prev)))
Ingo Molnardd41f592007-07-09 18:51:59 +02004538 prev->state = TASK_RUNNING;
Oleg Nesterov16882c12008-06-08 21:20:41 +04004539 else
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004540 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02004541 switch_count = &prev->nvcsw;
4542 }
4543
Steven Rostedt9a897c52008-01-25 21:08:22 +01004544#ifdef CONFIG_SMP
4545 if (prev->sched_class->pre_schedule)
4546 prev->sched_class->pre_schedule(rq, prev);
4547#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01004548
Ingo Molnardd41f592007-07-09 18:51:59 +02004549 if (unlikely(!rq->nr_running))
4550 idle_balance(cpu, rq);
4551
Ingo Molnar31ee5292007-08-09 11:16:49 +02004552 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004553 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 if (likely(prev != next)) {
David Simner673a90a2008-04-29 10:08:59 +01004556 sched_info_switch(prev, next);
Ingo Molnaraa9c4c02008-12-17 14:10:57 +01004557 perf_counter_task_sched_out(prev, cpu);
David Simner673a90a2008-04-29 10:08:59 +01004558
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 rq->nr_switches++;
4560 rq->curr = next;
4561 ++*switch_count;
4562
Ingo Molnardd41f592007-07-09 18:51:59 +02004563 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004564 /*
4565 * the context switch might have flipped the stack from under
4566 * us, hence refresh the local variables.
4567 */
4568 cpu = smp_processor_id();
4569 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 } else
4571 spin_unlock_irq(&rq->lock);
4572
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004573 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004575
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 preempt_enable_no_resched();
4577 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4578 goto need_resched;
4579}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580EXPORT_SYMBOL(schedule);
4581
4582#ifdef CONFIG_PREEMPT
4583/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004584 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004585 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 * occur there and call schedule directly.
4587 */
4588asmlinkage void __sched preempt_schedule(void)
4589{
4590 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004591
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 /*
4593 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004594 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004596 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 return;
4598
Andi Kleen3a5c3592007-10-15 17:00:14 +02004599 do {
4600 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004601 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004602 sub_preempt_count(PREEMPT_ACTIVE);
4603
4604 /*
4605 * Check again in case we missed a preemption opportunity
4606 * between schedule and now.
4607 */
4608 barrier();
4609 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611EXPORT_SYMBOL(preempt_schedule);
4612
4613/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004614 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 * off of irq context.
4616 * Note, that this is called and return with irqs disabled. This will
4617 * protect us against recursive calling from irq.
4618 */
4619asmlinkage void __sched preempt_schedule_irq(void)
4620{
4621 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004622
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004623 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 BUG_ON(ti->preempt_count || !irqs_disabled());
4625
Andi Kleen3a5c3592007-10-15 17:00:14 +02004626 do {
4627 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004628 local_irq_enable();
4629 schedule();
4630 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004631 sub_preempt_count(PREEMPT_ACTIVE);
4632
4633 /*
4634 * Check again in case we missed a preemption opportunity
4635 * between schedule and now.
4636 */
4637 barrier();
4638 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639}
4640
4641#endif /* CONFIG_PREEMPT */
4642
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004643int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4644 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004646 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648EXPORT_SYMBOL(default_wake_function);
4649
4650/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004651 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4652 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 * number) then we wake all the non-exclusive tasks and one exclusive task.
4654 *
4655 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004656 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4658 */
4659static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4660 int nr_exclusive, int sync, void *key)
4661{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004662 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004664 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004665 unsigned flags = curr->flags;
4666
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004668 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 break;
4670 }
4671}
4672
4673/**
4674 * __wake_up - wake up threads blocked on a waitqueue.
4675 * @q: the waitqueue
4676 * @mode: which threads
4677 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004678 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004680void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004681 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682{
4683 unsigned long flags;
4684
4685 spin_lock_irqsave(&q->lock, flags);
4686 __wake_up_common(q, mode, nr_exclusive, 0, key);
4687 spin_unlock_irqrestore(&q->lock, flags);
4688}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689EXPORT_SYMBOL(__wake_up);
4690
4691/*
4692 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4693 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004694void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695{
4696 __wake_up_common(q, mode, 1, 0, NULL);
4697}
4698
4699/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004700 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 * @q: the waitqueue
4702 * @mode: which threads
4703 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4704 *
4705 * The sync wakeup differs that the waker knows that it will schedule
4706 * away soon, so while the target thread will be woken up, it will not
4707 * be migrated to another CPU - ie. the two threads are 'synchronized'
4708 * with each other. This can prevent needless bouncing between CPUs.
4709 *
4710 * On UP it can prevent extra preemption.
4711 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004712void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004713__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714{
4715 unsigned long flags;
4716 int sync = 1;
4717
4718 if (unlikely(!q))
4719 return;
4720
4721 if (unlikely(!nr_exclusive))
4722 sync = 0;
4723
4724 spin_lock_irqsave(&q->lock, flags);
4725 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4726 spin_unlock_irqrestore(&q->lock, flags);
4727}
4728EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4729
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004730/**
4731 * complete: - signals a single thread waiting on this completion
4732 * @x: holds the state of this particular completion
4733 *
4734 * This will wake up a single thread waiting on this completion. Threads will be
4735 * awakened in the same order in which they were queued.
4736 *
4737 * See also complete_all(), wait_for_completion() and related routines.
4738 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004739void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740{
4741 unsigned long flags;
4742
4743 spin_lock_irqsave(&x->wait.lock, flags);
4744 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004745 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 spin_unlock_irqrestore(&x->wait.lock, flags);
4747}
4748EXPORT_SYMBOL(complete);
4749
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004750/**
4751 * complete_all: - signals all threads waiting on this completion
4752 * @x: holds the state of this particular completion
4753 *
4754 * This will wake up all threads waiting on this particular completion event.
4755 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004756void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757{
4758 unsigned long flags;
4759
4760 spin_lock_irqsave(&x->wait.lock, flags);
4761 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004762 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 spin_unlock_irqrestore(&x->wait.lock, flags);
4764}
4765EXPORT_SYMBOL(complete_all);
4766
Andi Kleen8cbbe862007-10-15 17:00:14 +02004767static inline long __sched
4768do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 if (!x->done) {
4771 DECLARE_WAITQUEUE(wait, current);
4772
4773 wait.flags |= WQ_FLAG_EXCLUSIVE;
4774 __add_wait_queue_tail(&x->wait, &wait);
4775 do {
Oleg Nesterov94d3d822008-08-20 16:54:41 -07004776 if (signal_pending_state(state, current)) {
Oleg Nesterovea71a542008-06-20 18:32:20 +04004777 timeout = -ERESTARTSYS;
4778 break;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004779 }
4780 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004782 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 spin_lock_irq(&x->wait.lock);
Oleg Nesterovea71a542008-06-20 18:32:20 +04004784 } while (!x->done && timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 __remove_wait_queue(&x->wait, &wait);
Oleg Nesterovea71a542008-06-20 18:32:20 +04004786 if (!x->done)
4787 return timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 }
4789 x->done--;
Oleg Nesterovea71a542008-06-20 18:32:20 +04004790 return timeout ?: 1;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004791}
4792
4793static long __sched
4794wait_for_common(struct completion *x, long timeout, int state)
4795{
4796 might_sleep();
4797
4798 spin_lock_irq(&x->wait.lock);
4799 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004801 return timeout;
4802}
4803
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004804/**
4805 * wait_for_completion: - waits for completion of a task
4806 * @x: holds the state of this particular completion
4807 *
4808 * This waits to be signaled for completion of a specific task. It is NOT
4809 * interruptible and there is no timeout.
4810 *
4811 * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
4812 * and interrupt capability. Also see complete().
4813 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004814void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004815{
4816 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817}
4818EXPORT_SYMBOL(wait_for_completion);
4819
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004820/**
4821 * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
4822 * @x: holds the state of this particular completion
4823 * @timeout: timeout value in jiffies
4824 *
4825 * This waits for either a completion of a specific task to be signaled or for a
4826 * specified timeout to expire. The timeout is in jiffies. It is not
4827 * interruptible.
4828 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004829unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4831{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004832 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833}
4834EXPORT_SYMBOL(wait_for_completion_timeout);
4835
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004836/**
4837 * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
4838 * @x: holds the state of this particular completion
4839 *
4840 * This waits for completion of a specific task to be signaled. It is
4841 * interruptible.
4842 */
Andi Kleen8cbbe862007-10-15 17:00:14 +02004843int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844{
Andi Kleen51e97992007-10-18 21:32:55 +02004845 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4846 if (t == -ERESTARTSYS)
4847 return t;
4848 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849}
4850EXPORT_SYMBOL(wait_for_completion_interruptible);
4851
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004852/**
4853 * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
4854 * @x: holds the state of this particular completion
4855 * @timeout: timeout value in jiffies
4856 *
4857 * This waits for either a completion of a specific task to be signaled or for a
4858 * specified timeout to expire. It is interruptible. The timeout is in jiffies.
4859 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004860unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861wait_for_completion_interruptible_timeout(struct completion *x,
4862 unsigned long timeout)
4863{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004864 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865}
4866EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4867
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004868/**
4869 * wait_for_completion_killable: - waits for completion of a task (killable)
4870 * @x: holds the state of this particular completion
4871 *
4872 * This waits to be signaled for completion of a specific task. It can be
4873 * interrupted by a kill signal.
4874 */
Matthew Wilcox009e5772007-12-06 12:29:54 -05004875int __sched wait_for_completion_killable(struct completion *x)
4876{
4877 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4878 if (t == -ERESTARTSYS)
4879 return t;
4880 return 0;
4881}
4882EXPORT_SYMBOL(wait_for_completion_killable);
4883
Dave Chinnerbe4de352008-08-15 00:40:44 -07004884/**
4885 * try_wait_for_completion - try to decrement a completion without blocking
4886 * @x: completion structure
4887 *
4888 * Returns: 0 if a decrement cannot be done without blocking
4889 * 1 if a decrement succeeded.
4890 *
4891 * If a completion is being used as a counting completion,
4892 * attempt to decrement the counter without blocking. This
4893 * enables us to avoid waiting if the resource the completion
4894 * is protecting is not available.
4895 */
4896bool try_wait_for_completion(struct completion *x)
4897{
4898 int ret = 1;
4899
4900 spin_lock_irq(&x->wait.lock);
4901 if (!x->done)
4902 ret = 0;
4903 else
4904 x->done--;
4905 spin_unlock_irq(&x->wait.lock);
4906 return ret;
4907}
4908EXPORT_SYMBOL(try_wait_for_completion);
4909
4910/**
4911 * completion_done - Test to see if a completion has any waiters
4912 * @x: completion structure
4913 *
4914 * Returns: 0 if there are waiters (wait_for_completion() in progress)
4915 * 1 if there are no waiters.
4916 *
4917 */
4918bool completion_done(struct completion *x)
4919{
4920 int ret = 1;
4921
4922 spin_lock_irq(&x->wait.lock);
4923 if (!x->done)
4924 ret = 0;
4925 spin_unlock_irq(&x->wait.lock);
4926 return ret;
4927}
4928EXPORT_SYMBOL(completion_done);
4929
Andi Kleen8cbbe862007-10-15 17:00:14 +02004930static long __sched
4931sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004932{
4933 unsigned long flags;
4934 wait_queue_t wait;
4935
4936 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
Andi Kleen8cbbe862007-10-15 17:00:14 +02004938 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939
Andi Kleen8cbbe862007-10-15 17:00:14 +02004940 spin_lock_irqsave(&q->lock, flags);
4941 __add_wait_queue(q, &wait);
4942 spin_unlock(&q->lock);
4943 timeout = schedule_timeout(timeout);
4944 spin_lock_irq(&q->lock);
4945 __remove_wait_queue(q, &wait);
4946 spin_unlock_irqrestore(&q->lock, flags);
4947
4948 return timeout;
4949}
4950
4951void __sched interruptible_sleep_on(wait_queue_head_t *q)
4952{
4953 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955EXPORT_SYMBOL(interruptible_sleep_on);
4956
Ingo Molnar0fec1712007-07-09 18:52:01 +02004957long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004958interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004960 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4963
Ingo Molnar0fec1712007-07-09 18:52:01 +02004964void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004966 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968EXPORT_SYMBOL(sleep_on);
4969
Ingo Molnar0fec1712007-07-09 18:52:01 +02004970long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004972 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974EXPORT_SYMBOL(sleep_on_timeout);
4975
Ingo Molnarb29739f2006-06-27 02:54:51 -07004976#ifdef CONFIG_RT_MUTEXES
4977
4978/*
4979 * rt_mutex_setprio - set the current priority of a task
4980 * @p: task
4981 * @prio: prio value (kernel-internal form)
4982 *
4983 * This function changes the 'effective' priority of a task. It does
4984 * not touch ->normal_prio like __setscheduler().
4985 *
4986 * Used by the rt_mutex code to implement priority inheritance logic.
4987 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004988void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004989{
4990 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004991 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004992 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01004993 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004994
4995 BUG_ON(prio < 0 || prio > MAX_PRIO);
4996
4997 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004998 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004999
Andrew Mortond5f9f942007-05-08 20:27:06 -07005000 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005001 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005002 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005003 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02005004 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005005 if (running)
5006 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02005007
5008 if (rt_prio(prio))
5009 p->sched_class = &rt_sched_class;
5010 else
5011 p->sched_class = &fair_sched_class;
5012
Ingo Molnarb29739f2006-06-27 02:54:51 -07005013 p->prio = prio;
5014
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005015 if (running)
5016 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005017 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02005018 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005019
5020 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005021 }
5022 task_rq_unlock(rq, &flags);
5023}
5024
5025#endif
5026
Ingo Molnar36c8b582006-07-03 00:25:41 -07005027void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028{
Ingo Molnardd41f592007-07-09 18:51:59 +02005029 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005031 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032
5033 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
5034 return;
5035 /*
5036 * We have to be careful, if called from sys_setpriority(),
5037 * the task might be in the middle of scheduling on another CPU.
5038 */
5039 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005040 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 /*
5042 * The RT priorities are set via sched_setscheduler(), but we still
5043 * allow the 'normal' nice value to be set - but as expected
5044 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02005045 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 */
Ingo Molnare05606d2007-07-09 18:51:59 +02005047 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 p->static_prio = NICE_TO_PRIO(nice);
5049 goto out_unlock;
5050 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005051 on_rq = p->se.on_rq;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02005052 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02005053 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07005056 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005057 old_prio = p->prio;
5058 p->prio = effective_prio(p);
5059 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060
Ingo Molnardd41f592007-07-09 18:51:59 +02005061 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02005062 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07005064 * If the task increased its priority or is running and
5065 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07005067 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 resched_task(rq->curr);
5069 }
5070out_unlock:
5071 task_rq_unlock(rq, &flags);
5072}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073EXPORT_SYMBOL(set_user_nice);
5074
Matt Mackalle43379f2005-05-01 08:59:00 -07005075/*
5076 * can_nice - check if a task can reduce its nice value
5077 * @p: task
5078 * @nice: nice value
5079 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005080int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07005081{
Matt Mackall024f4742005-08-18 11:24:19 -07005082 /* convert nice value [19,-20] to rlimit style value [1,40] */
5083 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005084
Matt Mackalle43379f2005-05-01 08:59:00 -07005085 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
5086 capable(CAP_SYS_NICE));
5087}
5088
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089#ifdef __ARCH_WANT_SYS_NICE
5090
5091/*
5092 * sys_nice - change the priority of the current process.
5093 * @increment: priority increment
5094 *
5095 * sys_setpriority is a more generic, but much slower function that
5096 * does similar things.
5097 */
5098asmlinkage long sys_nice(int increment)
5099{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005100 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101
5102 /*
5103 * Setpriority might change our priority at the same moment.
5104 * We don't have to worry. Conceptually one call occurs first
5105 * and we have a single winner.
5106 */
Matt Mackalle43379f2005-05-01 08:59:00 -07005107 if (increment < -40)
5108 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 if (increment > 40)
5110 increment = 40;
5111
5112 nice = PRIO_TO_NICE(current->static_prio) + increment;
5113 if (nice < -20)
5114 nice = -20;
5115 if (nice > 19)
5116 nice = 19;
5117
Matt Mackalle43379f2005-05-01 08:59:00 -07005118 if (increment < 0 && !can_nice(current, nice))
5119 return -EPERM;
5120
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 retval = security_task_setnice(current, nice);
5122 if (retval)
5123 return retval;
5124
5125 set_user_nice(current, nice);
5126 return 0;
5127}
5128
5129#endif
5130
5131/**
5132 * task_prio - return the priority value of a given task.
5133 * @p: the task in question.
5134 *
5135 * This is the priority value as seen by users in /proc.
5136 * RT tasks are offset by -200. Normal tasks are centered
5137 * around 0, value goes from -16 to +15.
5138 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005139int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140{
5141 return p->prio - MAX_RT_PRIO;
5142}
5143
5144/**
5145 * task_nice - return the nice value of a given task.
5146 * @p: the task in question.
5147 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005148int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149{
5150 return TASK_NICE(p);
5151}
Pavel Roskin150d8be2008-03-05 16:56:37 -05005152EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153
5154/**
5155 * idle_cpu - is a given cpu idle currently?
5156 * @cpu: the processor in question.
5157 */
5158int idle_cpu(int cpu)
5159{
5160 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
5161}
5162
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163/**
5164 * idle_task - return the idle task for a given cpu.
5165 * @cpu: the processor in question.
5166 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005167struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168{
5169 return cpu_rq(cpu)->idle;
5170}
5171
5172/**
5173 * find_process_by_pid - find a process with a matching PID value.
5174 * @pid: the pid in question.
5175 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02005176static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07005178 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179}
5180
5181/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02005182static void
5183__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184{
Ingo Molnardd41f592007-07-09 18:51:59 +02005185 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005186
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02005188 switch (p->policy) {
5189 case SCHED_NORMAL:
5190 case SCHED_BATCH:
5191 case SCHED_IDLE:
5192 p->sched_class = &fair_sched_class;
5193 break;
5194 case SCHED_FIFO:
5195 case SCHED_RR:
5196 p->sched_class = &rt_sched_class;
5197 break;
5198 }
5199
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005201 p->normal_prio = normal_prio(p);
5202 /* we are holding p->pi_lock already */
5203 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07005204 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205}
5206
Rusty Russell961ccdd2008-06-23 13:55:38 +10005207static int __sched_setscheduler(struct task_struct *p, int policy,
5208 struct sched_param *param, bool user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005210 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01005212 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005213 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214
Steven Rostedt66e53932006-06-27 02:54:44 -07005215 /* may grab non-irq protected spin_locks */
5216 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217recheck:
5218 /* double check policy once rq lock held */
5219 if (policy < 0)
5220 policy = oldpolicy = p->policy;
5221 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02005222 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
5223 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08005224 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 /*
5226 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02005227 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5228 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 */
5230 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005231 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04005232 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02005234 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 return -EINVAL;
5236
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005237 /*
5238 * Allow unprivileged RT tasks to decrease priority:
5239 */
Rusty Russell961ccdd2008-06-23 13:55:38 +10005240 if (user && !capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02005241 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005242 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005243
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005244 if (!lock_task_sighand(p, &flags))
5245 return -ESRCH;
5246 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
5247 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005248
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005249 /* can't set/change the rt policy */
5250 if (policy != p->policy && !rlim_rtprio)
5251 return -EPERM;
5252
5253 /* can't increase priority */
5254 if (param->sched_priority > p->rt_priority &&
5255 param->sched_priority > rlim_rtprio)
5256 return -EPERM;
5257 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005258 /*
5259 * Like positive nice levels, dont allow tasks to
5260 * move out of SCHED_IDLE either:
5261 */
5262 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
5263 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005264
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005265 /* can't change other user's priorities */
5266 if ((current->euid != p->euid) &&
5267 (current->euid != p->uid))
5268 return -EPERM;
5269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005271 if (user) {
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005272#ifdef CONFIG_RT_GROUP_SCHED
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005273 /*
5274 * Do not allow realtime tasks into groups that have no runtime
5275 * assigned.
5276 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02005277 if (rt_bandwidth_enabled() && rt_policy(policy) &&
5278 task_group(p)->rt_bandwidth.rt_runtime == 0)
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005279 return -EPERM;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005280#endif
5281
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005282 retval = security_task_setscheduler(p, policy, param);
5283 if (retval)
5284 return retval;
5285 }
5286
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07005288 * make sure no PI-waiters arrive (or leave) while we are
5289 * changing the priority of the task:
5290 */
5291 spin_lock_irqsave(&p->pi_lock, flags);
5292 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 * To be able to change p->policy safely, the apropriate
5294 * runqueue lock must be held.
5295 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07005296 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 /* recheck policy now with rq lock held */
5298 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
5299 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005300 __task_rq_unlock(rq);
5301 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 goto recheck;
5303 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02005304 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005305 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005306 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005307 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005308 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005309 if (running)
5310 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005311
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005313 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005314
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005315 if (running)
5316 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005317 if (on_rq) {
5318 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005319
5320 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07005322 __task_rq_unlock(rq);
5323 spin_unlock_irqrestore(&p->pi_lock, flags);
5324
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07005325 rt_mutex_adjust_pi(p);
5326
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 return 0;
5328}
Rusty Russell961ccdd2008-06-23 13:55:38 +10005329
5330/**
5331 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
5332 * @p: the task in question.
5333 * @policy: new policy.
5334 * @param: structure containing the new RT priority.
5335 *
5336 * NOTE that the task may be already dead.
5337 */
5338int sched_setscheduler(struct task_struct *p, int policy,
5339 struct sched_param *param)
5340{
5341 return __sched_setscheduler(p, policy, param, true);
5342}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343EXPORT_SYMBOL_GPL(sched_setscheduler);
5344
Rusty Russell961ccdd2008-06-23 13:55:38 +10005345/**
5346 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
5347 * @p: the task in question.
5348 * @policy: new policy.
5349 * @param: structure containing the new RT priority.
5350 *
5351 * Just like sched_setscheduler, only don't bother checking if the
5352 * current context has permission. For example, this is needed in
5353 * stop_machine(): we create temporary high priority worker threads,
5354 * but our caller might not have that capability.
5355 */
5356int sched_setscheduler_nocheck(struct task_struct *p, int policy,
5357 struct sched_param *param)
5358{
5359 return __sched_setscheduler(p, policy, param, false);
5360}
5361
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005362static int
5363do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 struct sched_param lparam;
5366 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005367 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368
5369 if (!param || pid < 0)
5370 return -EINVAL;
5371 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
5372 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005373
5374 rcu_read_lock();
5375 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005377 if (p != NULL)
5378 retval = sched_setscheduler(p, policy, &lparam);
5379 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07005380
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 return retval;
5382}
5383
5384/**
5385 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5386 * @pid: the pid in question.
5387 * @policy: new policy.
5388 * @param: structure containing the new RT priority.
5389 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005390asmlinkage long
5391sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392{
Jason Baronc21761f2006-01-18 17:43:03 -08005393 /* negative values for policy are not valid */
5394 if (policy < 0)
5395 return -EINVAL;
5396
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 return do_sched_setscheduler(pid, policy, param);
5398}
5399
5400/**
5401 * sys_sched_setparam - set/change the RT priority of a thread
5402 * @pid: the pid in question.
5403 * @param: structure containing the new RT priority.
5404 */
5405asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
5406{
5407 return do_sched_setscheduler(pid, -1, param);
5408}
5409
5410/**
5411 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5412 * @pid: the pid in question.
5413 */
5414asmlinkage long sys_sched_getscheduler(pid_t pid)
5415{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005416 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005417 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418
5419 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005420 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421
5422 retval = -ESRCH;
5423 read_lock(&tasklist_lock);
5424 p = find_process_by_pid(pid);
5425 if (p) {
5426 retval = security_task_getscheduler(p);
5427 if (!retval)
5428 retval = p->policy;
5429 }
5430 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 return retval;
5432}
5433
5434/**
5435 * sys_sched_getscheduler - get the RT priority of a thread
5436 * @pid: the pid in question.
5437 * @param: structure containing the RT priority.
5438 */
5439asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
5440{
5441 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005442 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005443 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444
5445 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005446 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
5448 read_lock(&tasklist_lock);
5449 p = find_process_by_pid(pid);
5450 retval = -ESRCH;
5451 if (!p)
5452 goto out_unlock;
5453
5454 retval = security_task_getscheduler(p);
5455 if (retval)
5456 goto out_unlock;
5457
5458 lp.sched_priority = p->rt_priority;
5459 read_unlock(&tasklist_lock);
5460
5461 /*
5462 * This one might sleep, we cannot do it with a spinlock held ...
5463 */
5464 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
5465
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 return retval;
5467
5468out_unlock:
5469 read_unlock(&tasklist_lock);
5470 return retval;
5471}
5472
Mike Travisb53e9212008-04-04 18:11:08 -07005473long sched_setaffinity(pid_t pid, const cpumask_t *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 cpumask_t cpus_allowed;
Mike Travisb53e9212008-04-04 18:11:08 -07005476 cpumask_t new_mask = *in_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005477 struct task_struct *p;
5478 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005480 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 read_lock(&tasklist_lock);
5482
5483 p = find_process_by_pid(pid);
5484 if (!p) {
5485 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005486 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 return -ESRCH;
5488 }
5489
5490 /*
5491 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005492 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 * usage count and then drop tasklist_lock.
5494 */
5495 get_task_struct(p);
5496 read_unlock(&tasklist_lock);
5497
5498 retval = -EPERM;
5499 if ((current->euid != p->euid) && (current->euid != p->uid) &&
5500 !capable(CAP_SYS_NICE))
5501 goto out_unlock;
5502
David Quigleye7834f82006-06-23 02:03:59 -07005503 retval = security_task_setscheduler(p, 0, NULL);
5504 if (retval)
5505 goto out_unlock;
5506
Mike Travisf9a86fc2008-04-04 18:11:07 -07005507 cpuset_cpus_allowed(p, &cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005509 again:
Mike Travis7c16ec52008-04-04 18:11:11 -07005510 retval = set_cpus_allowed_ptr(p, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511
Paul Menage8707d8b2007-10-18 23:40:22 -07005512 if (!retval) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07005513 cpuset_cpus_allowed(p, &cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005514 if (!cpus_subset(new_mask, cpus_allowed)) {
5515 /*
5516 * We must have raced with a concurrent cpuset
5517 * update. Just reset the cpus_allowed to the
5518 * cpuset's cpus_allowed
5519 */
5520 new_mask = cpus_allowed;
5521 goto again;
5522 }
5523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524out_unlock:
5525 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005526 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 return retval;
5528}
5529
5530static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
5531 cpumask_t *new_mask)
5532{
5533 if (len < sizeof(cpumask_t)) {
5534 memset(new_mask, 0, sizeof(cpumask_t));
5535 } else if (len > sizeof(cpumask_t)) {
5536 len = sizeof(cpumask_t);
5537 }
5538 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5539}
5540
5541/**
5542 * sys_sched_setaffinity - set the cpu affinity of a process
5543 * @pid: pid of the process
5544 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5545 * @user_mask_ptr: user-space pointer to the new cpu mask
5546 */
5547asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
5548 unsigned long __user *user_mask_ptr)
5549{
5550 cpumask_t new_mask;
5551 int retval;
5552
5553 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
5554 if (retval)
5555 return retval;
5556
Mike Travisb53e9212008-04-04 18:11:08 -07005557 return sched_setaffinity(pid, &new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558}
5559
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560long sched_getaffinity(pid_t pid, cpumask_t *mask)
5561{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005562 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005565 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 read_lock(&tasklist_lock);
5567
5568 retval = -ESRCH;
5569 p = find_process_by_pid(pid);
5570 if (!p)
5571 goto out_unlock;
5572
David Quigleye7834f82006-06-23 02:03:59 -07005573 retval = security_task_getscheduler(p);
5574 if (retval)
5575 goto out_unlock;
5576
Jack Steiner2f7016d2006-02-01 03:05:18 -08005577 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578
5579out_unlock:
5580 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005581 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582
Ulrich Drepper9531b622007-08-09 11:16:46 +02005583 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584}
5585
5586/**
5587 * sys_sched_getaffinity - get the cpu affinity of a process
5588 * @pid: pid of the process
5589 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5590 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5591 */
5592asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
5593 unsigned long __user *user_mask_ptr)
5594{
5595 int ret;
5596 cpumask_t mask;
5597
5598 if (len < sizeof(cpumask_t))
5599 return -EINVAL;
5600
5601 ret = sched_getaffinity(pid, &mask);
5602 if (ret < 0)
5603 return ret;
5604
5605 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
5606 return -EFAULT;
5607
5608 return sizeof(cpumask_t);
5609}
5610
5611/**
5612 * sys_sched_yield - yield the current processor to other threads.
5613 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005614 * This function yields the current CPU to other tasks. If there are no
5615 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 */
5617asmlinkage long sys_sched_yield(void)
5618{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005619 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620
Ingo Molnar2d723762007-10-15 17:00:12 +02005621 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005622 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623
5624 /*
5625 * Since we are going to call schedule() anyway, there's
5626 * no need to preempt or enable interrupts:
5627 */
5628 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005629 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 _raw_spin_unlock(&rq->lock);
5631 preempt_enable_no_resched();
5632
5633 schedule();
5634
5635 return 0;
5636}
5637
Andrew Mortone7b38402006-06-30 01:56:00 -07005638static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07005640#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5641 __might_sleep(__FILE__, __LINE__);
5642#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005643 /*
5644 * The BKS might be reacquired before we have dropped
5645 * PREEMPT_ACTIVE, which could trigger a second
5646 * cond_resched() call.
5647 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 do {
5649 add_preempt_count(PREEMPT_ACTIVE);
5650 schedule();
5651 sub_preempt_count(PREEMPT_ACTIVE);
5652 } while (need_resched());
5653}
5654
Herbert Xu02b67cc32008-01-25 21:08:28 +01005655int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656{
Ingo Molnar94142322006-12-29 16:48:13 -08005657 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5658 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 __cond_resched();
5660 return 1;
5661 }
5662 return 0;
5663}
Herbert Xu02b67cc32008-01-25 21:08:28 +01005664EXPORT_SYMBOL(_cond_resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665
5666/*
5667 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5668 * call schedule, and on return reacquire the lock.
5669 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005670 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 * operations here to prevent schedule() from being called twice (once via
5672 * spin_unlock(), once by hand).
5673 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005674int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675{
Nick Piggin95c354f2008-01-30 13:31:20 +01005676 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005677 int ret = 0;
5678
Nick Piggin95c354f2008-01-30 13:31:20 +01005679 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005681 if (resched && need_resched())
5682 __cond_resched();
5683 else
5684 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005685 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005688 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690EXPORT_SYMBOL(cond_resched_lock);
5691
5692int __sched cond_resched_softirq(void)
5693{
5694 BUG_ON(!in_softirq());
5695
Ingo Molnar94142322006-12-29 16:48:13 -08005696 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07005697 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 __cond_resched();
5699 local_bh_disable();
5700 return 1;
5701 }
5702 return 0;
5703}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704EXPORT_SYMBOL(cond_resched_softirq);
5705
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706/**
5707 * yield - yield the current processor to other threads.
5708 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005709 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 * thread runnable and calls sys_sched_yield().
5711 */
5712void __sched yield(void)
5713{
5714 set_current_state(TASK_RUNNING);
5715 sys_sched_yield();
5716}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717EXPORT_SYMBOL(yield);
5718
5719/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005720 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 * that process accounting knows that this is a task in IO wait state.
5722 *
5723 * But don't do that if it is a deliberate, throttling IO wait (this task
5724 * has set its backing_dev_info: the queue against which it should throttle)
5725 */
5726void __sched io_schedule(void)
5727{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005728 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005730 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 atomic_inc(&rq->nr_iowait);
5732 schedule();
5733 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005734 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736EXPORT_SYMBOL(io_schedule);
5737
5738long __sched io_schedule_timeout(long timeout)
5739{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005740 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 long ret;
5742
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005743 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 atomic_inc(&rq->nr_iowait);
5745 ret = schedule_timeout(timeout);
5746 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005747 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 return ret;
5749}
5750
5751/**
5752 * sys_sched_get_priority_max - return maximum RT priority.
5753 * @policy: scheduling class.
5754 *
5755 * this syscall returns the maximum rt_priority that can be used
5756 * by a given scheduling class.
5757 */
5758asmlinkage long sys_sched_get_priority_max(int policy)
5759{
5760 int ret = -EINVAL;
5761
5762 switch (policy) {
5763 case SCHED_FIFO:
5764 case SCHED_RR:
5765 ret = MAX_USER_RT_PRIO-1;
5766 break;
5767 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005768 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005769 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 ret = 0;
5771 break;
5772 }
5773 return ret;
5774}
5775
5776/**
5777 * sys_sched_get_priority_min - return minimum RT priority.
5778 * @policy: scheduling class.
5779 *
5780 * this syscall returns the minimum rt_priority that can be used
5781 * by a given scheduling class.
5782 */
5783asmlinkage long sys_sched_get_priority_min(int policy)
5784{
5785 int ret = -EINVAL;
5786
5787 switch (policy) {
5788 case SCHED_FIFO:
5789 case SCHED_RR:
5790 ret = 1;
5791 break;
5792 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005793 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005794 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 ret = 0;
5796 }
5797 return ret;
5798}
5799
5800/**
5801 * sys_sched_rr_get_interval - return the default timeslice of a process.
5802 * @pid: pid of the process.
5803 * @interval: userspace pointer to the timeslice value.
5804 *
5805 * this syscall writes the default timeslice value of a given process
5806 * into the user-space timespec buffer. A value of '0' means infinity.
5807 */
5808asmlinkage
5809long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5810{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005811 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005812 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005813 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815
5816 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005817 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818
5819 retval = -ESRCH;
5820 read_lock(&tasklist_lock);
5821 p = find_process_by_pid(pid);
5822 if (!p)
5823 goto out_unlock;
5824
5825 retval = security_task_getscheduler(p);
5826 if (retval)
5827 goto out_unlock;
5828
Ingo Molnar77034932007-12-04 17:04:39 +01005829 /*
5830 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5831 * tasks that are on an otherwise idle runqueue:
5832 */
5833 time_slice = 0;
5834 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005835 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005836 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005837 struct sched_entity *se = &p->se;
5838 unsigned long flags;
5839 struct rq *rq;
5840
5841 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005842 if (rq->cfs.load.weight)
5843 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005844 task_rq_unlock(rq, &flags);
5845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005847 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005850
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851out_unlock:
5852 read_unlock(&tasklist_lock);
5853 return retval;
5854}
5855
Steven Rostedt7c731e02008-05-12 21:20:41 +02005856static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005857
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005858void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005861 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005864 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005865 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005866#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005868 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005870 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871#else
5872 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005873 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005875 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876#endif
5877#ifdef CONFIG_DEBUG_STACK_USAGE
5878 {
Al Viro10ebffd2005-11-13 16:06:56 -08005879 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 while (!*n)
5881 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005882 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 }
5884#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005885 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005886 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005888 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889}
5890
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005891void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005893 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894
Ingo Molnar4bd77322007-07-11 21:21:47 +02005895#if BITS_PER_LONG == 32
5896 printk(KERN_INFO
5897 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005899 printk(KERN_INFO
5900 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901#endif
5902 read_lock(&tasklist_lock);
5903 do_each_thread(g, p) {
5904 /*
5905 * reset the NMI-timeout, listing all files on a slow
5906 * console might take alot of time:
5907 */
5908 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005909 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005910 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 } while_each_thread(g, p);
5912
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005913 touch_all_softlockup_watchdogs();
5914
Ingo Molnardd41f592007-07-09 18:51:59 +02005915#ifdef CONFIG_SCHED_DEBUG
5916 sysrq_sched_debug_show();
5917#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005919 /*
5920 * Only show locks if all tasks are dumped:
5921 */
5922 if (state_filter == -1)
5923 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924}
5925
Ingo Molnar1df21052007-07-09 18:51:58 +02005926void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5927{
Ingo Molnardd41f592007-07-09 18:51:59 +02005928 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005929}
5930
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005931/**
5932 * init_idle - set up an idle thread for a given CPU
5933 * @idle: task in question
5934 * @cpu: cpu the idle task belongs to
5935 *
5936 * NOTE: this function does not set the idle thread's NEED_RESCHED
5937 * flag, to make booting more robust.
5938 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005939void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005941 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 unsigned long flags;
5943
Ingo Molnar5cbd54e2008-11-12 20:05:50 +01005944 spin_lock_irqsave(&rq->lock, flags);
5945
Ingo Molnardd41f592007-07-09 18:51:59 +02005946 __sched_fork(idle);
5947 idle->se.exec_start = sched_clock();
5948
Ingo Molnarb29739f2006-06-27 02:54:51 -07005949 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005951 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005954#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5955 idle->oncpu = 1;
5956#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 spin_unlock_irqrestore(&rq->lock, flags);
5958
5959 /* Set the preempt count _outside_ the spinlocks! */
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005960#if defined(CONFIG_PREEMPT)
5961 task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
5962#else
Al Viroa1261f542005-11-13 16:06:55 -08005963 task_thread_info(idle)->preempt_count = 0;
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005964#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02005965 /*
5966 * The idle tasks have their own, simple scheduling class:
5967 */
5968 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969}
5970
5971/*
5972 * In a system that switches off the HZ timer nohz_cpu_mask
5973 * indicates which cpus entered this state. This is used
5974 * in the rcu update to wait only for active cpus. For system
5975 * which do not switch off the HZ timer nohz_cpu_mask should
5976 * always be CPU_MASK_NONE.
5977 */
5978cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
5979
Ingo Molnar19978ca2007-11-09 22:39:38 +01005980/*
5981 * Increase the granularity value when there are more CPUs,
5982 * because with more CPUs the 'effective latency' as visible
5983 * to users decreases. But the relationship is not linear,
5984 * so pick a second-best guess by going with the log2 of the
5985 * number of CPUs.
5986 *
5987 * This idea comes from the SD scheduler of Con Kolivas:
5988 */
5989static inline void sched_init_granularity(void)
5990{
5991 unsigned int factor = 1 + ilog2(num_online_cpus());
5992 const unsigned long limit = 200000000;
5993
5994 sysctl_sched_min_granularity *= factor;
5995 if (sysctl_sched_min_granularity > limit)
5996 sysctl_sched_min_granularity = limit;
5997
5998 sysctl_sched_latency *= factor;
5999 if (sysctl_sched_latency > limit)
6000 sysctl_sched_latency = limit;
6001
6002 sysctl_sched_wakeup_granularity *= factor;
Peter Zijlstra55cd5342008-08-04 08:54:26 +02006003
6004 sysctl_sched_shares_ratelimit *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01006005}
6006
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007#ifdef CONFIG_SMP
6008/*
6009 * This is how migration works:
6010 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07006011 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 * runqueue and wake up that CPU's migration thread.
6013 * 2) we down() the locked semaphore => thread blocks.
6014 * 3) migration thread wakes up (implicitly it forces the migrated
6015 * thread off the CPU)
6016 * 4) it gets the migration request and checks whether the migrated
6017 * task is still in the wrong runqueue.
6018 * 5) if it's in the wrong runqueue then the migration thread removes
6019 * it and puts it into the right queue.
6020 * 6) migration thread up()s the semaphore.
6021 * 7) we wake up and the migration is done.
6022 */
6023
6024/*
6025 * Change a given task's CPU affinity. Migrate the thread to a
6026 * proper CPU and schedule it away if the CPU it's executing on
6027 * is removed from the allowed bitmask.
6028 *
6029 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006030 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 * call is not atomic; no spinlocks may be held.
6032 */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006033int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006035 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006037 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006038 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039
6040 rq = task_rq_lock(p, &flags);
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006041 if (!cpus_intersects(*new_mask, cpu_online_map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 ret = -EINVAL;
6043 goto out;
6044 }
6045
David Rientjes9985b0b2008-06-05 12:57:11 -07006046 if (unlikely((p->flags & PF_THREAD_BOUND) && p != current &&
6047 !cpus_equal(p->cpus_allowed, *new_mask))) {
6048 ret = -EINVAL;
6049 goto out;
6050 }
6051
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01006052 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006053 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01006054 else {
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006055 p->cpus_allowed = *new_mask;
6056 p->rt.nr_cpus_allowed = cpus_weight(*new_mask);
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01006057 }
6058
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 /* Can the task run on the task's current CPU? If so, we're done */
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006060 if (cpu_isset(task_cpu(p), *new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 goto out;
6062
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006063 if (migrate_task(p, any_online_cpu(*new_mask), &req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 /* Need help from migration thread: drop lock and wait. */
6065 task_rq_unlock(rq, &flags);
6066 wake_up_process(rq->migration_thread);
6067 wait_for_completion(&req.done);
6068 tlb_migrate_finish(p->mm);
6069 return 0;
6070 }
6071out:
6072 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006073
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 return ret;
6075}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006076EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077
6078/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006079 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 * this because either it can't run here any more (set_cpus_allowed()
6081 * away from this CPU, or CPU going down), or because we're
6082 * attempting to rebalance this task on exec (sched_exec).
6083 *
6084 * So we race with normal scheduler movements, but that's OK, as long
6085 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07006086 *
6087 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07006089static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006091 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02006092 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093
Max Krasnyanskye761b772008-07-15 04:43:49 -07006094 if (unlikely(!cpu_active(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07006095 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096
6097 rq_src = cpu_rq(src_cpu);
6098 rq_dest = cpu_rq(dest_cpu);
6099
6100 double_rq_lock(rq_src, rq_dest);
6101 /* Already moved. */
6102 if (task_cpu(p) != src_cpu)
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006103 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 /* Affinity changed (again). */
6105 if (!cpu_isset(dest_cpu, p->cpus_allowed))
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006106 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107
Ingo Molnardd41f592007-07-09 18:51:59 +02006108 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006109 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006110 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006111
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006113 if (on_rq) {
6114 activate_task(rq_dest, p, 0);
Peter Zijlstra15afe092008-09-20 23:38:02 +02006115 check_preempt_curr(rq_dest, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 }
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006117done:
Kirill Korotaevefc30812006-06-27 02:54:32 -07006118 ret = 1;
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006119fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07006121 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122}
6123
6124/*
6125 * migration_thread - this is a highprio system thread that performs
6126 * thread migration by bumping thread off CPU then 'pushing' onto
6127 * another runqueue.
6128 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07006129static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006132 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133
6134 rq = cpu_rq(cpu);
6135 BUG_ON(rq->migration_thread != current);
6136
6137 set_current_state(TASK_INTERRUPTIBLE);
6138 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006139 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 spin_lock_irq(&rq->lock);
6143
6144 if (cpu_is_offline(cpu)) {
6145 spin_unlock_irq(&rq->lock);
6146 goto wait_to_die;
6147 }
6148
6149 if (rq->active_balance) {
6150 active_load_balance(rq, cpu);
6151 rq->active_balance = 0;
6152 }
6153
6154 head = &rq->migration_queue;
6155
6156 if (list_empty(head)) {
6157 spin_unlock_irq(&rq->lock);
6158 schedule();
6159 set_current_state(TASK_INTERRUPTIBLE);
6160 continue;
6161 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07006162 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 list_del_init(head->next);
6164
Nick Piggin674311d2005-06-25 14:57:27 -07006165 spin_unlock(&rq->lock);
6166 __migrate_task(req->task, cpu, req->dest_cpu);
6167 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168
6169 complete(&req->done);
6170 }
6171 __set_current_state(TASK_RUNNING);
6172 return 0;
6173
6174wait_to_die:
6175 /* Wait for kthread_stop */
6176 set_current_state(TASK_INTERRUPTIBLE);
6177 while (!kthread_should_stop()) {
6178 schedule();
6179 set_current_state(TASK_INTERRUPTIBLE);
6180 }
6181 __set_current_state(TASK_RUNNING);
6182 return 0;
6183}
6184
6185#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006186
6187static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
6188{
6189 int ret;
6190
6191 local_irq_disable();
6192 ret = __migrate_task(p, src_cpu, dest_cpu);
6193 local_irq_enable();
6194 return ret;
6195}
6196
Kirill Korotaev054b9102006-12-10 02:20:11 -08006197/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006198 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08006199 * NOTE: interrupts should be disabled by the caller
6200 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006201static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202{
Kirill Korotaevefc30812006-06-27 02:54:32 -07006203 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006205 struct rq *rq;
6206 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207
Andi Kleen3a5c3592007-10-15 17:00:14 +02006208 do {
6209 /* On same node? */
6210 mask = node_to_cpumask(cpu_to_node(dead_cpu));
6211 cpus_and(mask, mask, p->cpus_allowed);
6212 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213
Andi Kleen3a5c3592007-10-15 17:00:14 +02006214 /* On any allowed CPU? */
Mike Travis434d53b2008-04-04 18:11:04 -07006215 if (dest_cpu >= nr_cpu_ids)
Andi Kleen3a5c3592007-10-15 17:00:14 +02006216 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217
Andi Kleen3a5c3592007-10-15 17:00:14 +02006218 /* No more Mr. Nice Guy. */
Mike Travis434d53b2008-04-04 18:11:04 -07006219 if (dest_cpu >= nr_cpu_ids) {
Mike Travisf9a86fc2008-04-04 18:11:07 -07006220 cpumask_t cpus_allowed;
6221
6222 cpuset_cpus_allowed_locked(p, &cpus_allowed);
Cliff Wickman470fd642007-10-18 23:40:46 -07006223 /*
6224 * Try to stay on the same cpuset, where the
6225 * current cpuset may be a subset of all cpus.
6226 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006227 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07006228 * called within calls to cpuset_lock/cpuset_unlock.
6229 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02006230 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07006231 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006232 dest_cpu = any_online_cpu(p->cpus_allowed);
6233 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234
Andi Kleen3a5c3592007-10-15 17:00:14 +02006235 /*
6236 * Don't tell them about moving exiting tasks or
6237 * kernel threads (both mm NULL), since they never
6238 * leave kernel.
6239 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006240 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02006241 printk(KERN_INFO "process %d (%s) no "
6242 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006243 task_pid_nr(p), p->comm, dead_cpu);
6244 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006245 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006246 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247}
6248
6249/*
6250 * While a dead CPU has no uninterruptible tasks queued at this point,
6251 * it might still have a nonzero ->nr_uninterruptible counter, because
6252 * for performance reasons the counter is not stricly tracking tasks to
6253 * their home CPUs. So we just add the counter to another CPU's counter,
6254 * to keep the global sum constant after CPU-down:
6255 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07006256static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257{
Mike Travis7c16ec52008-04-04 18:11:11 -07006258 struct rq *rq_dest = cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 unsigned long flags;
6260
6261 local_irq_save(flags);
6262 double_rq_lock(rq_src, rq_dest);
6263 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
6264 rq_src->nr_uninterruptible = 0;
6265 double_rq_unlock(rq_src, rq_dest);
6266 local_irq_restore(flags);
6267}
6268
6269/* Run through task list and migrate tasks from the dead cpu. */
6270static void migrate_live_tasks(int src_cpu)
6271{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006272 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006274 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275
Ingo Molnar48f24c42006-07-03 00:25:40 -07006276 do_each_thread(t, p) {
6277 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278 continue;
6279
Ingo Molnar48f24c42006-07-03 00:25:40 -07006280 if (task_cpu(p) == src_cpu)
6281 move_task_off_dead_cpu(src_cpu, p);
6282 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006284 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285}
6286
Ingo Molnardd41f592007-07-09 18:51:59 +02006287/*
6288 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006289 * It does so by boosting its priority to highest possible.
6290 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 */
6292void sched_idle_next(void)
6293{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006294 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07006295 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 struct task_struct *p = rq->idle;
6297 unsigned long flags;
6298
6299 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006300 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301
Ingo Molnar48f24c42006-07-03 00:25:40 -07006302 /*
6303 * Strictly not necessary since rest of the CPUs are stopped by now
6304 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 */
6306 spin_lock_irqsave(&rq->lock, flags);
6307
Ingo Molnardd41f592007-07-09 18:51:59 +02006308 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006309
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006310 update_rq_clock(rq);
6311 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312
6313 spin_unlock_irqrestore(&rq->lock, flags);
6314}
6315
Ingo Molnar48f24c42006-07-03 00:25:40 -07006316/*
6317 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318 * offline.
6319 */
6320void idle_task_exit(void)
6321{
6322 struct mm_struct *mm = current->active_mm;
6323
6324 BUG_ON(cpu_online(smp_processor_id()));
6325
6326 if (mm != &init_mm)
6327 switch_mm(mm, &init_mm, current);
6328 mmdrop(mm);
6329}
6330
Kirill Korotaev054b9102006-12-10 02:20:11 -08006331/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006332static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006334 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335
6336 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07006337 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338
6339 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07006340 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341
Ingo Molnar48f24c42006-07-03 00:25:40 -07006342 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343
6344 /*
6345 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006346 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 * fine.
6348 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006349 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006350 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006351 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352
Ingo Molnar48f24c42006-07-03 00:25:40 -07006353 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354}
6355
6356/* release_task() removes task from tasklist, so we won't find dead tasks. */
6357static void migrate_dead_tasks(unsigned int dead_cpu)
6358{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006359 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006360 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361
Ingo Molnardd41f592007-07-09 18:51:59 +02006362 for ( ; ; ) {
6363 if (!rq->nr_running)
6364 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02006365 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02006366 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02006367 if (!next)
6368 break;
Dmitry Adamushko79c53792008-06-29 00:16:56 +02006369 next->sched_class->put_prev_task(rq, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02006370 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02006371
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372 }
6373}
6374#endif /* CONFIG_HOTPLUG_CPU */
6375
Nick Piggine692ab52007-07-26 13:40:43 +02006376#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
6377
6378static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006379 {
6380 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006381 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006382 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006383 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006384};
6385
6386static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006387 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006388 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006389 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006390 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006391 .child = sd_ctl_dir,
6392 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006393 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006394};
6395
6396static struct ctl_table *sd_alloc_ctl_entry(int n)
6397{
6398 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02006399 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02006400
Nick Piggine692ab52007-07-26 13:40:43 +02006401 return entry;
6402}
6403
Milton Miller6382bc92007-10-15 17:00:19 +02006404static void sd_free_ctl_entry(struct ctl_table **tablep)
6405{
Milton Millercd7900762007-10-17 16:55:11 +02006406 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02006407
Milton Millercd7900762007-10-17 16:55:11 +02006408 /*
6409 * In the intermediate directories, both the child directory and
6410 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006411 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02006412 * static strings and all have proc handlers.
6413 */
6414 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02006415 if (entry->child)
6416 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02006417 if (entry->proc_handler == NULL)
6418 kfree(entry->procname);
6419 }
Milton Miller6382bc92007-10-15 17:00:19 +02006420
6421 kfree(*tablep);
6422 *tablep = NULL;
6423}
6424
Nick Piggine692ab52007-07-26 13:40:43 +02006425static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02006426set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02006427 const char *procname, void *data, int maxlen,
6428 mode_t mode, proc_handler *proc_handler)
6429{
Nick Piggine692ab52007-07-26 13:40:43 +02006430 entry->procname = procname;
6431 entry->data = data;
6432 entry->maxlen = maxlen;
6433 entry->mode = mode;
6434 entry->proc_handler = proc_handler;
6435}
6436
6437static struct ctl_table *
6438sd_alloc_ctl_domain_table(struct sched_domain *sd)
6439{
Ingo Molnara5d8c342008-10-09 11:35:51 +02006440 struct ctl_table *table = sd_alloc_ctl_entry(13);
Nick Piggine692ab52007-07-26 13:40:43 +02006441
Milton Millerad1cdc12007-10-15 17:00:19 +02006442 if (table == NULL)
6443 return NULL;
6444
Alexey Dobriyane0361852007-08-09 11:16:46 +02006445 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006446 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006447 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006448 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006449 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006450 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006451 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006452 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006453 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006454 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006455 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006456 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006457 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006458 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006459 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02006460 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006461 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02006462 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006463 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02006464 &sd->cache_nice_tries,
6465 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006466 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02006467 sizeof(int), 0644, proc_dointvec_minmax);
Ingo Molnara5d8c342008-10-09 11:35:51 +02006468 set_table_entry(&table[11], "name", sd->name,
6469 CORENAME_MAX_SIZE, 0444, proc_dostring);
6470 /* &table[12] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02006471
6472 return table;
6473}
6474
Ingo Molnar9a4e7152007-11-28 15:52:56 +01006475static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02006476{
6477 struct ctl_table *entry, *table;
6478 struct sched_domain *sd;
6479 int domain_num = 0, i;
6480 char buf[32];
6481
6482 for_each_domain(cpu, sd)
6483 domain_num++;
6484 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02006485 if (table == NULL)
6486 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02006487
6488 i = 0;
6489 for_each_domain(cpu, sd) {
6490 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006491 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006492 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006493 entry->child = sd_alloc_ctl_domain_table(sd);
6494 entry++;
6495 i++;
6496 }
6497 return table;
6498}
6499
6500static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02006501static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006502{
6503 int i, cpu_num = num_online_cpus();
6504 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
6505 char buf[32];
6506
Milton Miller73785472007-10-24 18:23:48 +02006507 WARN_ON(sd_ctl_dir[0].child);
6508 sd_ctl_dir[0].child = entry;
6509
Milton Millerad1cdc12007-10-15 17:00:19 +02006510 if (entry == NULL)
6511 return;
6512
Milton Miller97b6ea72007-10-15 17:00:19 +02006513 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02006514 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006515 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006516 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006517 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02006518 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02006519 }
Milton Miller73785472007-10-24 18:23:48 +02006520
6521 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02006522 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
6523}
Milton Miller6382bc92007-10-15 17:00:19 +02006524
Milton Miller73785472007-10-24 18:23:48 +02006525/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02006526static void unregister_sched_domain_sysctl(void)
6527{
Milton Miller73785472007-10-24 18:23:48 +02006528 if (sd_sysctl_header)
6529 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02006530 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02006531 if (sd_ctl_dir[0].child)
6532 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02006533}
Nick Piggine692ab52007-07-26 13:40:43 +02006534#else
Milton Miller6382bc92007-10-15 17:00:19 +02006535static void register_sched_domain_sysctl(void)
6536{
6537}
6538static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006539{
6540}
6541#endif
6542
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006543static void set_rq_online(struct rq *rq)
6544{
6545 if (!rq->online) {
6546 const struct sched_class *class;
6547
6548 cpu_set(rq->cpu, rq->rd->online);
6549 rq->online = 1;
6550
6551 for_each_class(class) {
6552 if (class->rq_online)
6553 class->rq_online(rq);
6554 }
6555 }
6556}
6557
6558static void set_rq_offline(struct rq *rq)
6559{
6560 if (rq->online) {
6561 const struct sched_class *class;
6562
6563 for_each_class(class) {
6564 if (class->rq_offline)
6565 class->rq_offline(rq);
6566 }
6567
6568 cpu_clear(rq->cpu, rq->rd->online);
6569 rq->online = 0;
6570 }
6571}
6572
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573/*
6574 * migration_call - callback that gets triggered when a CPU is added.
6575 * Here we can start up the necessary migration thread for the new CPU.
6576 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006577static int __cpuinit
6578migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006581 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006583 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584
6585 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006586
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006588 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02006589 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590 if (IS_ERR(p))
6591 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592 kthread_bind(p, cpu);
6593 /* Must be high prio: stop_machine expects to yield to it. */
6594 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02006595 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596 task_rq_unlock(rq, &flags);
6597 cpu_rq(cpu)->migration_thread = p;
6598 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006599
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006601 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006602 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006604
6605 /* Update our root-domain */
6606 rq = cpu_rq(cpu);
6607 spin_lock_irqsave(&rq->lock, flags);
6608 if (rq->rd) {
6609 BUG_ON(!cpu_isset(cpu, rq->rd->span));
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006610
6611 set_rq_online(rq);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006612 }
6613 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006615
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616#ifdef CONFIG_HOTPLUG_CPU
6617 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006618 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07006619 if (!cpu_rq(cpu)->migration_thread)
6620 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006621 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08006622 kthread_bind(cpu_rq(cpu)->migration_thread,
6623 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624 kthread_stop(cpu_rq(cpu)->migration_thread);
6625 cpu_rq(cpu)->migration_thread = NULL;
6626 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006627
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006629 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07006630 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631 migrate_live_tasks(cpu);
6632 rq = cpu_rq(cpu);
6633 kthread_stop(rq->migration_thread);
6634 rq->migration_thread = NULL;
6635 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006636 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02006637 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006638 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02006640 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
6641 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006643 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07006644 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645 migrate_nr_uninterruptible(rq);
6646 BUG_ON(rq->nr_running != 0);
6647
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006648 /*
6649 * No need to migrate the tasks: it was best-effort if
6650 * they didn't take sched_hotcpu_mutex. Just wake up
6651 * the requestors.
6652 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653 spin_lock_irq(&rq->lock);
6654 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006655 struct migration_req *req;
6656
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07006658 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 list_del_init(&req->list);
Brian King9a2bd242008-12-09 08:47:00 -06006660 spin_unlock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006661 complete(&req->done);
Brian King9a2bd242008-12-09 08:47:00 -06006662 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663 }
6664 spin_unlock_irq(&rq->lock);
6665 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006666
Gregory Haskins08f503b2008-03-10 17:59:11 -04006667 case CPU_DYING:
6668 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01006669 /* Update our root-domain */
6670 rq = cpu_rq(cpu);
6671 spin_lock_irqsave(&rq->lock, flags);
6672 if (rq->rd) {
6673 BUG_ON(!cpu_isset(cpu, rq->rd->span));
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006674 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006675 }
6676 spin_unlock_irqrestore(&rq->lock, flags);
6677 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678#endif
6679 }
6680 return NOTIFY_OK;
6681}
6682
6683/* Register at highest priority so that task migration (migrate_all_tasks)
6684 * happens before everything else.
6685 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006686static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687 .notifier_call = migration_call,
6688 .priority = 10
6689};
6690
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07006691static int __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692{
6693 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006694 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006695
6696 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006697 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6698 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006699 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6700 register_cpu_notifier(&migration_notifier);
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07006701
6702 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703}
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07006704early_initcall(migration_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705#endif
6706
6707#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006708
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006709#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006710
Gautham R Shenoy099f98c2008-05-29 20:56:32 +05306711static inline const char *sd_level_to_string(enum sched_domain_level lvl)
6712{
6713 switch (lvl) {
6714 case SD_LV_NONE:
6715 return "NONE";
6716 case SD_LV_SIBLING:
6717 return "SIBLING";
6718 case SD_LV_MC:
6719 return "MC";
6720 case SD_LV_CPU:
6721 return "CPU";
6722 case SD_LV_NODE:
6723 return "NODE";
6724 case SD_LV_ALLNODES:
6725 return "ALLNODES";
6726 case SD_LV_MAX:
6727 return "MAX";
6728
6729 }
6730 return "MAX";
6731}
6732
Mike Travis7c16ec52008-04-04 18:11:11 -07006733static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
6734 cpumask_t *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006735{
6736 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07006737 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006738
Mike Travis434d53b2008-04-04 18:11:04 -07006739 cpulist_scnprintf(str, sizeof(str), sd->span);
Mike Travis7c16ec52008-04-04 18:11:11 -07006740 cpus_clear(*groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006741
6742 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6743
6744 if (!(sd->flags & SD_LOAD_BALANCE)) {
6745 printk("does not load-balance\n");
6746 if (sd->parent)
6747 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6748 " has parent");
6749 return -1;
6750 }
6751
Gautham R Shenoy099f98c2008-05-29 20:56:32 +05306752 printk(KERN_CONT "span %s level %s\n",
6753 str, sd_level_to_string(sd->level));
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006754
6755 if (!cpu_isset(cpu, sd->span)) {
6756 printk(KERN_ERR "ERROR: domain->span does not contain "
6757 "CPU%d\n", cpu);
6758 }
6759 if (!cpu_isset(cpu, group->cpumask)) {
6760 printk(KERN_ERR "ERROR: domain->groups does not contain"
6761 " CPU%d\n", cpu);
6762 }
6763
6764 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6765 do {
6766 if (!group) {
6767 printk("\n");
6768 printk(KERN_ERR "ERROR: group is NULL\n");
6769 break;
6770 }
6771
6772 if (!group->__cpu_power) {
6773 printk(KERN_CONT "\n");
6774 printk(KERN_ERR "ERROR: domain->cpu_power not "
6775 "set\n");
6776 break;
6777 }
6778
6779 if (!cpus_weight(group->cpumask)) {
6780 printk(KERN_CONT "\n");
6781 printk(KERN_ERR "ERROR: empty group\n");
6782 break;
6783 }
6784
Mike Travis7c16ec52008-04-04 18:11:11 -07006785 if (cpus_intersects(*groupmask, group->cpumask)) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006786 printk(KERN_CONT "\n");
6787 printk(KERN_ERR "ERROR: repeated CPUs\n");
6788 break;
6789 }
6790
Mike Travis7c16ec52008-04-04 18:11:11 -07006791 cpus_or(*groupmask, *groupmask, group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006792
Mike Travis434d53b2008-04-04 18:11:04 -07006793 cpulist_scnprintf(str, sizeof(str), group->cpumask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006794 printk(KERN_CONT " %s", str);
6795
6796 group = group->next;
6797 } while (group != sd->groups);
6798 printk(KERN_CONT "\n");
6799
Mike Travis7c16ec52008-04-04 18:11:11 -07006800 if (!cpus_equal(sd->span, *groupmask))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006801 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6802
Mike Travis7c16ec52008-04-04 18:11:11 -07006803 if (sd->parent && !cpus_subset(*groupmask, sd->parent->span))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006804 printk(KERN_ERR "ERROR: parent span is not a superset "
6805 "of domain->span\n");
6806 return 0;
6807}
6808
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809static void sched_domain_debug(struct sched_domain *sd, int cpu)
6810{
Mike Travis7c16ec52008-04-04 18:11:11 -07006811 cpumask_t *groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812 int level = 0;
6813
Nick Piggin41c7ce92005-06-25 14:57:24 -07006814 if (!sd) {
6815 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6816 return;
6817 }
6818
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6820
Mike Travis7c16ec52008-04-04 18:11:11 -07006821 groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6822 if (!groupmask) {
6823 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
6824 return;
6825 }
6826
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006827 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006828 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830 level++;
6831 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006832 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006833 break;
6834 }
Mike Travis7c16ec52008-04-04 18:11:11 -07006835 kfree(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006837#else /* !CONFIG_SCHED_DEBUG */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006838# define sched_domain_debug(sd, cpu) do { } while (0)
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006839#endif /* CONFIG_SCHED_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006841static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006842{
6843 if (cpus_weight(sd->span) == 1)
6844 return 1;
6845
6846 /* Following flags need at least 2 groups */
6847 if (sd->flags & (SD_LOAD_BALANCE |
6848 SD_BALANCE_NEWIDLE |
6849 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006850 SD_BALANCE_EXEC |
6851 SD_SHARE_CPUPOWER |
6852 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006853 if (sd->groups != sd->groups->next)
6854 return 0;
6855 }
6856
6857 /* Following flags don't use groups */
6858 if (sd->flags & (SD_WAKE_IDLE |
6859 SD_WAKE_AFFINE |
6860 SD_WAKE_BALANCE))
6861 return 0;
6862
6863 return 1;
6864}
6865
Ingo Molnar48f24c42006-07-03 00:25:40 -07006866static int
6867sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006868{
6869 unsigned long cflags = sd->flags, pflags = parent->flags;
6870
6871 if (sd_degenerate(parent))
6872 return 1;
6873
6874 if (!cpus_equal(sd->span, parent->span))
6875 return 0;
6876
6877 /* Does parent contain flags not in child? */
6878 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6879 if (cflags & SD_WAKE_AFFINE)
6880 pflags &= ~SD_WAKE_BALANCE;
6881 /* Flags needing groups don't count if only 1 group in parent */
6882 if (parent->groups == parent->groups->next) {
6883 pflags &= ~(SD_LOAD_BALANCE |
6884 SD_BALANCE_NEWIDLE |
6885 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006886 SD_BALANCE_EXEC |
6887 SD_SHARE_CPUPOWER |
6888 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006889 }
6890 if (~cflags & pflags)
6891 return 0;
6892
6893 return 1;
6894}
6895
Gregory Haskins57d885f2008-01-25 21:08:18 +01006896static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6897{
6898 unsigned long flags;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006899
6900 spin_lock_irqsave(&rq->lock, flags);
6901
6902 if (rq->rd) {
6903 struct root_domain *old_rd = rq->rd;
6904
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006905 if (cpu_isset(rq->cpu, old_rd->online))
6906 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006907
Gregory Haskinsdc938522008-01-25 21:08:26 +01006908 cpu_clear(rq->cpu, old_rd->span);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006909
Gregory Haskins57d885f2008-01-25 21:08:18 +01006910 if (atomic_dec_and_test(&old_rd->refcount))
6911 kfree(old_rd);
6912 }
6913
6914 atomic_inc(&rd->refcount);
6915 rq->rd = rd;
6916
Gregory Haskinsdc938522008-01-25 21:08:26 +01006917 cpu_set(rq->cpu, rd->span);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006918 if (cpu_isset(rq->cpu, cpu_online_map))
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006919 set_rq_online(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006920
6921 spin_unlock_irqrestore(&rq->lock, flags);
6922}
6923
Gregory Haskinsdc938522008-01-25 21:08:26 +01006924static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006925{
6926 memset(rd, 0, sizeof(*rd));
6927
Gregory Haskinsdc938522008-01-25 21:08:26 +01006928 cpus_clear(rd->span);
6929 cpus_clear(rd->online);
Gregory Haskins6e0534f2008-05-12 21:21:01 +02006930
6931 cpupri_init(&rd->cpupri);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006932}
6933
6934static void init_defrootdomain(void)
6935{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006936 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006937 atomic_set(&def_root_domain.refcount, 1);
6938}
6939
Gregory Haskinsdc938522008-01-25 21:08:26 +01006940static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006941{
6942 struct root_domain *rd;
6943
6944 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6945 if (!rd)
6946 return NULL;
6947
Gregory Haskinsdc938522008-01-25 21:08:26 +01006948 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006949
6950 return rd;
6951}
6952
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006954 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955 * hold the hotplug lock.
6956 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006957static void
6958cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006960 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006961 struct sched_domain *tmp;
6962
6963 /* Remove the sched domains which do not contribute to scheduling. */
Li Zefanf29c9b12008-11-06 09:45:16 +08006964 for (tmp = sd; tmp; ) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006965 struct sched_domain *parent = tmp->parent;
6966 if (!parent)
6967 break;
Li Zefanf29c9b12008-11-06 09:45:16 +08006968
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006969 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006970 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006971 if (parent->parent)
6972 parent->parent->child = tmp;
Li Zefanf29c9b12008-11-06 09:45:16 +08006973 } else
6974 tmp = tmp->parent;
Suresh Siddha245af2c2005-06-25 14:57:25 -07006975 }
6976
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006977 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006978 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006979 if (sd)
6980 sd->child = NULL;
6981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982
6983 sched_domain_debug(sd, cpu);
6984
Gregory Haskins57d885f2008-01-25 21:08:18 +01006985 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006986 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987}
6988
6989/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006990static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991
6992/* Setup the mask of cpus configured for isolated domains */
6993static int __init isolated_cpu_setup(char *str)
6994{
Mike Travis13b40c12008-07-01 10:32:50 -07006995 static int __initdata ints[NR_CPUS];
6996 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997
6998 str = get_options(str, ARRAY_SIZE(ints), ints);
6999 cpus_clear(cpu_isolated_map);
7000 for (i = 1; i <= ints[0]; i++)
7001 if (ints[i] < NR_CPUS)
7002 cpu_set(ints[i], cpu_isolated_map);
7003 return 1;
7004}
7005
Ingo Molnar8927f492007-10-15 17:00:13 +02007006__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007
7008/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007009 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
7010 * to a function which identifies what group(along with sched group) a CPU
7011 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
7012 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013 *
7014 * init_sched_build_groups will build a circular linked list of the groups
7015 * covered by the given span, and will set each group's ->cpumask correctly,
7016 * and ->cpu_power to 0.
7017 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007018static void
Mike Travis7c16ec52008-04-04 18:11:11 -07007019init_sched_build_groups(const cpumask_t *span, const cpumask_t *cpu_map,
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007020 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007021 struct sched_group **sg,
7022 cpumask_t *tmpmask),
7023 cpumask_t *covered, cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024{
7025 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026 int i;
7027
Mike Travis7c16ec52008-04-04 18:11:11 -07007028 cpus_clear(*covered);
7029
Mike Travis363ab6f2008-05-12 21:21:13 +02007030 for_each_cpu_mask_nr(i, *span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007031 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007032 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033 int j;
7034
Mike Travis7c16ec52008-04-04 18:11:11 -07007035 if (cpu_isset(i, *covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036 continue;
7037
Mike Travis7c16ec52008-04-04 18:11:11 -07007038 cpus_clear(sg->cpumask);
Eric Dumazet5517d862007-05-08 00:32:57 -07007039 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040
Mike Travis363ab6f2008-05-12 21:21:13 +02007041 for_each_cpu_mask_nr(j, *span) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007042 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043 continue;
7044
Mike Travis7c16ec52008-04-04 18:11:11 -07007045 cpu_set(j, *covered);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046 cpu_set(j, sg->cpumask);
7047 }
7048 if (!first)
7049 first = sg;
7050 if (last)
7051 last->next = sg;
7052 last = sg;
7053 }
7054 last->next = first;
7055}
7056
John Hawkes9c1cfda2005-09-06 15:18:14 -07007057#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058
John Hawkes9c1cfda2005-09-06 15:18:14 -07007059#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08007060
John Hawkes9c1cfda2005-09-06 15:18:14 -07007061/**
7062 * find_next_best_node - find the next node to include in a sched_domain
7063 * @node: node whose sched_domain we're building
7064 * @used_nodes: nodes already in the sched_domain
7065 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007066 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07007067 * finds the closest node not already in the @used_nodes map.
7068 *
7069 * Should use nodemask_t.
7070 */
Mike Travisc5f59f02008-04-04 18:11:10 -07007071static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007072{
7073 int i, n, val, min_val, best_node = 0;
7074
7075 min_val = INT_MAX;
7076
Mike Travis076ac2a2008-05-12 21:21:12 +02007077 for (i = 0; i < nr_node_ids; i++) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007078 /* Start at @node */
Mike Travis076ac2a2008-05-12 21:21:12 +02007079 n = (node + i) % nr_node_ids;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007080
7081 if (!nr_cpus_node(n))
7082 continue;
7083
7084 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07007085 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007086 continue;
7087
7088 /* Simple min distance search */
7089 val = node_distance(node, n);
7090
7091 if (val < min_val) {
7092 min_val = val;
7093 best_node = n;
7094 }
7095 }
7096
Mike Travisc5f59f02008-04-04 18:11:10 -07007097 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007098 return best_node;
7099}
7100
7101/**
7102 * sched_domain_node_span - get a cpumask for a node's sched_domain
7103 * @node: node whose cpumask we're constructing
Randy Dunlap73486722008-04-22 10:07:22 -07007104 * @span: resulting cpumask
John Hawkes9c1cfda2005-09-06 15:18:14 -07007105 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007106 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07007107 * should be one that prevents unnecessary balancing, but also spreads tasks
7108 * out optimally.
7109 */
Mike Travis4bdbaad32008-04-15 16:35:52 -07007110static void sched_domain_node_span(int node, cpumask_t *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007111{
Mike Travisc5f59f02008-04-04 18:11:10 -07007112 nodemask_t used_nodes;
Mike Travisc5f59f02008-04-04 18:11:10 -07007113 node_to_cpumask_ptr(nodemask, node);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007114 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007115
Mike Travis4bdbaad32008-04-15 16:35:52 -07007116 cpus_clear(*span);
Mike Travisc5f59f02008-04-04 18:11:10 -07007117 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007118
Mike Travis4bdbaad32008-04-15 16:35:52 -07007119 cpus_or(*span, *span, *nodemask);
Mike Travisc5f59f02008-04-04 18:11:10 -07007120 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007121
7122 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07007123 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007124
Mike Travisc5f59f02008-04-04 18:11:10 -07007125 node_to_cpumask_ptr_next(nodemask, next_node);
Mike Travis4bdbaad32008-04-15 16:35:52 -07007126 cpus_or(*span, *span, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007127 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007128}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007129#endif /* CONFIG_NUMA */
John Hawkes9c1cfda2005-09-06 15:18:14 -07007130
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007131int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007132
John Hawkes9c1cfda2005-09-06 15:18:14 -07007133/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07007134 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07007135 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136#ifdef CONFIG_SCHED_SMT
7137static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007138static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007139
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007140static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007141cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7142 cpumask_t *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007144 if (sg)
7145 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146 return cpu;
7147}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007148#endif /* CONFIG_SCHED_SMT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149
Ingo Molnar48f24c42006-07-03 00:25:40 -07007150/*
7151 * multi-core sched-domains:
7152 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007153#ifdef CONFIG_SCHED_MC
7154static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007155static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007156#endif /* CONFIG_SCHED_MC */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007157
7158#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007159static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007160cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7161 cpumask_t *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007162{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007163 int group;
Mike Travis7c16ec52008-04-04 18:11:11 -07007164
7165 *mask = per_cpu(cpu_sibling_map, cpu);
7166 cpus_and(*mask, *mask, *cpu_map);
7167 group = first_cpu(*mask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007168 if (sg)
7169 *sg = &per_cpu(sched_group_core, group);
7170 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007171}
7172#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007173static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007174cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7175 cpumask_t *unused)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007176{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007177 if (sg)
7178 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007179 return cpu;
7180}
7181#endif
7182
Linus Torvalds1da177e2005-04-16 15:20:36 -07007183static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007184static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007185
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007186static int
Mike Travis7c16ec52008-04-04 18:11:11 -07007187cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
7188 cpumask_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007190 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007191#ifdef CONFIG_SCHED_MC
Mike Travis7c16ec52008-04-04 18:11:11 -07007192 *mask = cpu_coregroup_map(cpu);
7193 cpus_and(*mask, *mask, *cpu_map);
7194 group = first_cpu(*mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007195#elif defined(CONFIG_SCHED_SMT)
Mike Travis7c16ec52008-04-04 18:11:11 -07007196 *mask = per_cpu(cpu_sibling_map, cpu);
7197 cpus_and(*mask, *mask, *cpu_map);
7198 group = first_cpu(*mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007200 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007202 if (sg)
7203 *sg = &per_cpu(sched_group_phys, group);
7204 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007205}
7206
7207#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07007208/*
7209 * The init_sched_build_groups can't handle what we want to do with node
7210 * groups, so roll our own. Now each node has its own list of groups which
7211 * gets dynamically allocated.
7212 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213static DEFINE_PER_CPU(struct sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07007214static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007215
7216static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007217static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007218
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007219static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007220 struct sched_group **sg, cpumask_t *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007222 int group;
7223
Mike Travis7c16ec52008-04-04 18:11:11 -07007224 *nodemask = node_to_cpumask(cpu_to_node(cpu));
7225 cpus_and(*nodemask, *nodemask, *cpu_map);
7226 group = first_cpu(*nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007227
7228 if (sg)
7229 *sg = &per_cpu(sched_group_allnodes, group);
7230 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007232
Siddha, Suresh B08069032006-03-27 01:15:23 -08007233static void init_numa_sched_groups_power(struct sched_group *group_head)
7234{
7235 struct sched_group *sg = group_head;
7236 int j;
7237
7238 if (!sg)
7239 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02007240 do {
Mike Travis363ab6f2008-05-12 21:21:13 +02007241 for_each_cpu_mask_nr(j, sg->cpumask) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02007242 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08007243
Andi Kleen3a5c3592007-10-15 17:00:14 +02007244 sd = &per_cpu(phys_domains, j);
7245 if (j != first_cpu(sd->groups->cpumask)) {
7246 /*
7247 * Only add "power" once for each
7248 * physical package.
7249 */
7250 continue;
7251 }
7252
7253 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007254 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02007255 sg = sg->next;
7256 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007257}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007258#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007260#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007261/* Free memory allocated for various sched_group structures */
Mike Travis7c16ec52008-04-04 18:11:11 -07007262static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007263{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007264 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007265
Mike Travis363ab6f2008-05-12 21:21:13 +02007266 for_each_cpu_mask_nr(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007267 struct sched_group **sched_group_nodes
7268 = sched_group_nodes_bycpu[cpu];
7269
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007270 if (!sched_group_nodes)
7271 continue;
7272
Mike Travis076ac2a2008-05-12 21:21:12 +02007273 for (i = 0; i < nr_node_ids; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007274 struct sched_group *oldsg, *sg = sched_group_nodes[i];
7275
Mike Travis7c16ec52008-04-04 18:11:11 -07007276 *nodemask = node_to_cpumask(i);
7277 cpus_and(*nodemask, *nodemask, *cpu_map);
7278 if (cpus_empty(*nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007279 continue;
7280
7281 if (sg == NULL)
7282 continue;
7283 sg = sg->next;
7284next_sg:
7285 oldsg = sg;
7286 sg = sg->next;
7287 kfree(oldsg);
7288 if (oldsg != sched_group_nodes[i])
7289 goto next_sg;
7290 }
7291 kfree(sched_group_nodes);
7292 sched_group_nodes_bycpu[cpu] = NULL;
7293 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007294}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007295#else /* !CONFIG_NUMA */
Mike Travis7c16ec52008-04-04 18:11:11 -07007296static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007297{
7298}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007299#endif /* CONFIG_NUMA */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007300
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007302 * Initialize sched groups cpu_power.
7303 *
7304 * cpu_power indicates the capacity of sched group, which is used while
7305 * distributing the load between different sched groups in a sched domain.
7306 * Typically cpu_power for all the groups in a sched domain will be same unless
7307 * there are asymmetries in the topology. If there are asymmetries, group
7308 * having more cpu_power will pickup more load compared to the group having
7309 * less cpu_power.
7310 *
7311 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
7312 * the maximum number of tasks a group can handle in the presence of other idle
7313 * or lightly loaded groups in the same sched domain.
7314 */
7315static void init_sched_groups_power(int cpu, struct sched_domain *sd)
7316{
7317 struct sched_domain *child;
7318 struct sched_group *group;
7319
7320 WARN_ON(!sd || !sd->groups);
7321
7322 if (cpu != first_cpu(sd->groups->cpumask))
7323 return;
7324
7325 child = sd->child;
7326
Eric Dumazet5517d862007-05-08 00:32:57 -07007327 sd->groups->__cpu_power = 0;
7328
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007329 /*
7330 * For perf policy, if the groups in child domain share resources
7331 * (for example cores sharing some portions of the cache hierarchy
7332 * or SMT), then set this domain groups cpu_power such that each group
7333 * can handle only one task, when there are other idle groups in the
7334 * same sched domain.
7335 */
7336 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
7337 (child->flags &
7338 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07007339 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007340 return;
7341 }
7342
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007343 /*
7344 * add cpu_power of each child group to this groups cpu_power
7345 */
7346 group = child->groups;
7347 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07007348 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007349 group = group->next;
7350 } while (group != child->groups);
7351}
7352
7353/*
Mike Travis7c16ec52008-04-04 18:11:11 -07007354 * Initializers for schedule domains
7355 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7356 */
7357
Ingo Molnara5d8c342008-10-09 11:35:51 +02007358#ifdef CONFIG_SCHED_DEBUG
7359# define SD_INIT_NAME(sd, type) sd->name = #type
7360#else
7361# define SD_INIT_NAME(sd, type) do { } while (0)
7362#endif
7363
Mike Travis7c16ec52008-04-04 18:11:11 -07007364#define SD_INIT(sd, type) sd_init_##type(sd)
Ingo Molnara5d8c342008-10-09 11:35:51 +02007365
Mike Travis7c16ec52008-04-04 18:11:11 -07007366#define SD_INIT_FUNC(type) \
7367static noinline void sd_init_##type(struct sched_domain *sd) \
7368{ \
7369 memset(sd, 0, sizeof(*sd)); \
7370 *sd = SD_##type##_INIT; \
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007371 sd->level = SD_LV_##type; \
Ingo Molnara5d8c342008-10-09 11:35:51 +02007372 SD_INIT_NAME(sd, type); \
Mike Travis7c16ec52008-04-04 18:11:11 -07007373}
7374
7375SD_INIT_FUNC(CPU)
7376#ifdef CONFIG_NUMA
7377 SD_INIT_FUNC(ALLNODES)
7378 SD_INIT_FUNC(NODE)
7379#endif
7380#ifdef CONFIG_SCHED_SMT
7381 SD_INIT_FUNC(SIBLING)
7382#endif
7383#ifdef CONFIG_SCHED_MC
7384 SD_INIT_FUNC(MC)
7385#endif
7386
7387/*
7388 * To minimize stack usage kmalloc room for cpumasks and share the
7389 * space as the usage in build_sched_domains() dictates. Used only
7390 * if the amount of space is significant.
7391 */
7392struct allmasks {
7393 cpumask_t tmpmask; /* make this one first */
7394 union {
7395 cpumask_t nodemask;
7396 cpumask_t this_sibling_map;
7397 cpumask_t this_core_map;
7398 };
7399 cpumask_t send_covered;
7400
7401#ifdef CONFIG_NUMA
7402 cpumask_t domainspan;
7403 cpumask_t covered;
7404 cpumask_t notcovered;
7405#endif
7406};
7407
7408#if NR_CPUS > 128
7409#define SCHED_CPUMASK_ALLOC 1
7410#define SCHED_CPUMASK_FREE(v) kfree(v)
7411#define SCHED_CPUMASK_DECLARE(v) struct allmasks *v
7412#else
7413#define SCHED_CPUMASK_ALLOC 0
7414#define SCHED_CPUMASK_FREE(v)
7415#define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v
7416#endif
7417
7418#define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \
7419 ((unsigned long)(a) + offsetof(struct allmasks, v))
7420
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007421static int default_relax_domain_level = -1;
7422
7423static int __init setup_relax_domain_level(char *str)
7424{
Li Zefan30e0e172008-05-13 10:27:17 +08007425 unsigned long val;
7426
7427 val = simple_strtoul(str, NULL, 0);
7428 if (val < SD_LV_MAX)
7429 default_relax_domain_level = val;
7430
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007431 return 1;
7432}
7433__setup("relax_domain_level=", setup_relax_domain_level);
7434
7435static void set_domain_attribute(struct sched_domain *sd,
7436 struct sched_domain_attr *attr)
7437{
7438 int request;
7439
7440 if (!attr || attr->relax_domain_level < 0) {
7441 if (default_relax_domain_level < 0)
7442 return;
7443 else
7444 request = default_relax_domain_level;
7445 } else
7446 request = attr->relax_domain_level;
7447 if (request < sd->level) {
7448 /* turn off idle balance on this domain */
7449 sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
7450 } else {
7451 /* turn on idle balance on this domain */
7452 sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
7453 }
7454}
7455
Mike Travis7c16ec52008-04-04 18:11:11 -07007456/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007457 * Build sched domains for a given set of cpus and attach the sched domains
7458 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007460static int __build_sched_domains(const cpumask_t *cpu_map,
7461 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462{
7463 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007464 struct root_domain *rd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007465 SCHED_CPUMASK_DECLARE(allmasks);
7466 cpumask_t *tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07007467#ifdef CONFIG_NUMA
7468 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007469 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07007470
7471 /*
7472 * Allocate the per-node list of sched groups
7473 */
Mike Travis076ac2a2008-05-12 21:21:12 +02007474 sched_group_nodes = kcalloc(nr_node_ids, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007475 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07007476 if (!sched_group_nodes) {
7477 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007478 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07007479 }
John Hawkesd1b55132005-09-06 15:18:14 -07007480#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481
Gregory Haskinsdc938522008-01-25 21:08:26 +01007482 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01007483 if (!rd) {
7484 printk(KERN_WARNING "Cannot alloc root domain\n");
Mike Travis7c16ec52008-04-04 18:11:11 -07007485#ifdef CONFIG_NUMA
7486 kfree(sched_group_nodes);
7487#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007488 return -ENOMEM;
7489 }
7490
Mike Travis7c16ec52008-04-04 18:11:11 -07007491#if SCHED_CPUMASK_ALLOC
7492 /* get space for all scratch cpumask variables */
7493 allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL);
7494 if (!allmasks) {
7495 printk(KERN_WARNING "Cannot alloc cpumask array\n");
7496 kfree(rd);
7497#ifdef CONFIG_NUMA
7498 kfree(sched_group_nodes);
7499#endif
7500 return -ENOMEM;
7501 }
7502#endif
7503 tmpmask = (cpumask_t *)allmasks;
7504
7505
7506#ifdef CONFIG_NUMA
7507 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
7508#endif
7509
Linus Torvalds1da177e2005-04-16 15:20:36 -07007510 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007511 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512 */
Mike Travis363ab6f2008-05-12 21:21:13 +02007513 for_each_cpu_mask_nr(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514 struct sched_domain *sd = NULL, *p;
Mike Travis7c16ec52008-04-04 18:11:11 -07007515 SCHED_CPUMASK_VAR(nodemask, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007516
Mike Travis7c16ec52008-04-04 18:11:11 -07007517 *nodemask = node_to_cpumask(cpu_to_node(i));
7518 cpus_and(*nodemask, *nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007519
7520#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02007521 if (cpus_weight(*cpu_map) >
Mike Travis7c16ec52008-04-04 18:11:11 -07007522 SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007523 sd = &per_cpu(allnodes_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007524 SD_INIT(sd, ALLNODES);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007525 set_domain_attribute(sd, attr);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007526 sd->span = *cpu_map;
Mike Travis7c16ec52008-04-04 18:11:11 -07007527 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007528 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007529 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007530 } else
7531 p = NULL;
7532
Linus Torvalds1da177e2005-04-16 15:20:36 -07007533 sd = &per_cpu(node_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007534 SD_INIT(sd, NODE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007535 set_domain_attribute(sd, attr);
Mike Travis4bdbaad32008-04-15 16:35:52 -07007536 sched_domain_node_span(cpu_to_node(i), &sd->span);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007537 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007538 if (p)
7539 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007540 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007541#endif
7542
7543 p = sd;
7544 sd = &per_cpu(phys_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007545 SD_INIT(sd, CPU);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007546 set_domain_attribute(sd, attr);
Mike Travis7c16ec52008-04-04 18:11:11 -07007547 sd->span = *nodemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007549 if (p)
7550 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007551 cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007552
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007553#ifdef CONFIG_SCHED_MC
7554 p = sd;
7555 sd = &per_cpu(core_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007556 SD_INIT(sd, MC);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007557 set_domain_attribute(sd, attr);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007558 sd->span = cpu_coregroup_map(i);
7559 cpus_and(sd->span, sd->span, *cpu_map);
7560 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007561 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007562 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007563#endif
7564
Linus Torvalds1da177e2005-04-16 15:20:36 -07007565#ifdef CONFIG_SCHED_SMT
7566 p = sd;
7567 sd = &per_cpu(cpu_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007568 SD_INIT(sd, SIBLING);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007569 set_domain_attribute(sd, attr);
Mike Travisd5a74302007-10-16 01:24:05 -07007570 sd->span = per_cpu(cpu_sibling_map, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007571 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007573 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007574 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007575#endif
7576 }
7577
7578#ifdef CONFIG_SCHED_SMT
7579 /* Set up CPU (sibling) groups */
Mike Travis363ab6f2008-05-12 21:21:13 +02007580 for_each_cpu_mask_nr(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007581 SCHED_CPUMASK_VAR(this_sibling_map, allmasks);
7582 SCHED_CPUMASK_VAR(send_covered, allmasks);
7583
7584 *this_sibling_map = per_cpu(cpu_sibling_map, i);
7585 cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map);
7586 if (i != first_cpu(*this_sibling_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587 continue;
7588
Ingo Molnardd41f592007-07-09 18:51:59 +02007589 init_sched_build_groups(this_sibling_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007590 &cpu_to_cpu_group,
7591 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592 }
7593#endif
7594
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007595#ifdef CONFIG_SCHED_MC
7596 /* Set up multi-core groups */
Mike Travis363ab6f2008-05-12 21:21:13 +02007597 for_each_cpu_mask_nr(i, *cpu_map) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007598 SCHED_CPUMASK_VAR(this_core_map, allmasks);
7599 SCHED_CPUMASK_VAR(send_covered, allmasks);
7600
7601 *this_core_map = cpu_coregroup_map(i);
7602 cpus_and(*this_core_map, *this_core_map, *cpu_map);
7603 if (i != first_cpu(*this_core_map))
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007604 continue;
Mike Travis7c16ec52008-04-04 18:11:11 -07007605
Ingo Molnardd41f592007-07-09 18:51:59 +02007606 init_sched_build_groups(this_core_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007607 &cpu_to_core_group,
7608 send_covered, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007609 }
7610#endif
7611
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612 /* Set up physical groups */
Mike Travis076ac2a2008-05-12 21:21:12 +02007613 for (i = 0; i < nr_node_ids; i++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007614 SCHED_CPUMASK_VAR(nodemask, allmasks);
7615 SCHED_CPUMASK_VAR(send_covered, allmasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007616
Mike Travis7c16ec52008-04-04 18:11:11 -07007617 *nodemask = node_to_cpumask(i);
7618 cpus_and(*nodemask, *nodemask, *cpu_map);
7619 if (cpus_empty(*nodemask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620 continue;
7621
Mike Travis7c16ec52008-04-04 18:11:11 -07007622 init_sched_build_groups(nodemask, cpu_map,
7623 &cpu_to_phys_group,
7624 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625 }
7626
7627#ifdef CONFIG_NUMA
7628 /* Set up node groups */
Mike Travis7c16ec52008-04-04 18:11:11 -07007629 if (sd_allnodes) {
7630 SCHED_CPUMASK_VAR(send_covered, allmasks);
7631
7632 init_sched_build_groups(cpu_map, cpu_map,
7633 &cpu_to_allnodes_group,
7634 send_covered, tmpmask);
7635 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007636
Mike Travis076ac2a2008-05-12 21:21:12 +02007637 for (i = 0; i < nr_node_ids; i++) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007638 /* Set up node groups */
7639 struct sched_group *sg, *prev;
Mike Travis7c16ec52008-04-04 18:11:11 -07007640 SCHED_CPUMASK_VAR(nodemask, allmasks);
7641 SCHED_CPUMASK_VAR(domainspan, allmasks);
7642 SCHED_CPUMASK_VAR(covered, allmasks);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007643 int j;
7644
Mike Travis7c16ec52008-04-04 18:11:11 -07007645 *nodemask = node_to_cpumask(i);
7646 cpus_clear(*covered);
7647
7648 cpus_and(*nodemask, *nodemask, *cpu_map);
7649 if (cpus_empty(*nodemask)) {
John Hawkesd1b55132005-09-06 15:18:14 -07007650 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007651 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07007652 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007653
Mike Travis4bdbaad32008-04-15 16:35:52 -07007654 sched_domain_node_span(i, domainspan);
Mike Travis7c16ec52008-04-04 18:11:11 -07007655 cpus_and(*domainspan, *domainspan, *cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007656
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007657 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007658 if (!sg) {
7659 printk(KERN_WARNING "Can not alloc domain group for "
7660 "node %d\n", i);
7661 goto error;
7662 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007663 sched_group_nodes[i] = sg;
Mike Travis363ab6f2008-05-12 21:21:13 +02007664 for_each_cpu_mask_nr(j, *nodemask) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007665 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02007666
John Hawkes9c1cfda2005-09-06 15:18:14 -07007667 sd = &per_cpu(node_domains, j);
7668 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007669 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007670 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007671 sg->cpumask = *nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007672 sg->next = sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007673 cpus_or(*covered, *covered, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007674 prev = sg;
7675
Mike Travis076ac2a2008-05-12 21:21:12 +02007676 for (j = 0; j < nr_node_ids; j++) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007677 SCHED_CPUMASK_VAR(notcovered, allmasks);
Mike Travis076ac2a2008-05-12 21:21:12 +02007678 int n = (i + j) % nr_node_ids;
Mike Travisc5f59f02008-04-04 18:11:10 -07007679 node_to_cpumask_ptr(pnodemask, n);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007680
Mike Travis7c16ec52008-04-04 18:11:11 -07007681 cpus_complement(*notcovered, *covered);
7682 cpus_and(*tmpmask, *notcovered, *cpu_map);
7683 cpus_and(*tmpmask, *tmpmask, *domainspan);
7684 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007685 break;
7686
Mike Travis7c16ec52008-04-04 18:11:11 -07007687 cpus_and(*tmpmask, *tmpmask, *pnodemask);
7688 if (cpus_empty(*tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007689 continue;
7690
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007691 sg = kmalloc_node(sizeof(struct sched_group),
7692 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007693 if (!sg) {
7694 printk(KERN_WARNING
7695 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007696 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007697 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007698 sg->__cpu_power = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07007699 sg->cpumask = *tmpmask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007700 sg->next = prev->next;
Mike Travis7c16ec52008-04-04 18:11:11 -07007701 cpus_or(*covered, *covered, *tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007702 prev->next = sg;
7703 prev = sg;
7704 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007706#endif
7707
7708 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007709#ifdef CONFIG_SCHED_SMT
Mike Travis363ab6f2008-05-12 21:21:13 +02007710 for_each_cpu_mask_nr(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007711 struct sched_domain *sd = &per_cpu(cpu_domains, i);
7712
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007713 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007714 }
7715#endif
7716#ifdef CONFIG_SCHED_MC
Mike Travis363ab6f2008-05-12 21:21:13 +02007717 for_each_cpu_mask_nr(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007718 struct sched_domain *sd = &per_cpu(core_domains, i);
7719
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007720 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007721 }
7722#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007723
Mike Travis363ab6f2008-05-12 21:21:13 +02007724 for_each_cpu_mask_nr(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007725 struct sched_domain *sd = &per_cpu(phys_domains, i);
7726
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007727 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007728 }
7729
John Hawkes9c1cfda2005-09-06 15:18:14 -07007730#ifdef CONFIG_NUMA
Mike Travis076ac2a2008-05-12 21:21:12 +02007731 for (i = 0; i < nr_node_ids; i++)
Siddha, Suresh B08069032006-03-27 01:15:23 -08007732 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007733
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007734 if (sd_allnodes) {
7735 struct sched_group *sg;
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007736
Mike Travis7c16ec52008-04-04 18:11:11 -07007737 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg,
7738 tmpmask);
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007739 init_numa_sched_groups_power(sg);
7740 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007741#endif
7742
Linus Torvalds1da177e2005-04-16 15:20:36 -07007743 /* Attach the domains */
Mike Travis363ab6f2008-05-12 21:21:13 +02007744 for_each_cpu_mask_nr(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007745 struct sched_domain *sd;
7746#ifdef CONFIG_SCHED_SMT
7747 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007748#elif defined(CONFIG_SCHED_MC)
7749 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007750#else
7751 sd = &per_cpu(phys_domains, i);
7752#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007753 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007754 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007755
Mike Travis7c16ec52008-04-04 18:11:11 -07007756 SCHED_CPUMASK_FREE((void *)allmasks);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007757 return 0;
7758
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007759#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007760error:
Mike Travis7c16ec52008-04-04 18:11:11 -07007761 free_sched_groups(cpu_map, tmpmask);
7762 SCHED_CPUMASK_FREE((void *)allmasks);
Li Zefanca3273f92008-11-07 14:47:21 +08007763 kfree(rd);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007764 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007765#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007766}
Paul Jackson029190c2007-10-18 23:40:20 -07007767
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007768static int build_sched_domains(const cpumask_t *cpu_map)
7769{
7770 return __build_sched_domains(cpu_map, NULL);
7771}
7772
Paul Jackson029190c2007-10-18 23:40:20 -07007773static cpumask_t *doms_cur; /* current sched domains */
7774static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Ingo Molnar4285f5942008-05-16 17:47:14 +02007775static struct sched_domain_attr *dattr_cur;
7776 /* attribues of custom domains in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007777
7778/*
7779 * Special case: If a kmalloc of a doms_cur partition (array of
7780 * cpumask_t) fails, then fallback to a single sched domain,
7781 * as determined by the single cpumask_t fallback_doms.
7782 */
7783static cpumask_t fallback_doms;
7784
Heiko Carstens22e52b02008-03-12 18:31:59 +01007785void __attribute__((weak)) arch_update_cpu_topology(void)
7786{
7787}
7788
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007789/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007790 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007791 * For now this just excludes isolated cpus, but could be used to
7792 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007793 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007794static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007795{
Milton Miller73785472007-10-24 18:23:48 +02007796 int err;
7797
Heiko Carstens22e52b02008-03-12 18:31:59 +01007798 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007799 ndoms_cur = 1;
7800 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
7801 if (!doms_cur)
7802 doms_cur = &fallback_doms;
7803 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007804 dattr_cur = NULL;
Milton Miller73785472007-10-24 18:23:48 +02007805 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02007806 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007807
7808 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007809}
7810
Mike Travis7c16ec52008-04-04 18:11:11 -07007811static void arch_destroy_sched_domains(const cpumask_t *cpu_map,
7812 cpumask_t *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007813{
Mike Travis7c16ec52008-04-04 18:11:11 -07007814 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007815}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007816
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007817/*
7818 * Detach sched domains from a group of cpus specified in cpu_map
7819 * These cpus will now be attached to the NULL domain
7820 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08007821static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007822{
Mike Travis7c16ec52008-04-04 18:11:11 -07007823 cpumask_t tmpmask;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007824 int i;
7825
Milton Miller6382bc92007-10-15 17:00:19 +02007826 unregister_sched_domain_sysctl();
7827
Mike Travis363ab6f2008-05-12 21:21:13 +02007828 for_each_cpu_mask_nr(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007829 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007830 synchronize_sched();
Mike Travis7c16ec52008-04-04 18:11:11 -07007831 arch_destroy_sched_domains(cpu_map, &tmpmask);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007832}
7833
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007834/* handle null as "default" */
7835static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7836 struct sched_domain_attr *new, int idx_new)
7837{
7838 struct sched_domain_attr tmp;
7839
7840 /* fast path */
7841 if (!new && !cur)
7842 return 1;
7843
7844 tmp = SD_ATTR_INIT;
7845 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7846 new ? (new + idx_new) : &tmp,
7847 sizeof(struct sched_domain_attr));
7848}
7849
Paul Jackson029190c2007-10-18 23:40:20 -07007850/*
7851 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007852 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007853 * doms_new[] to the current sched domain partitioning, doms_cur[].
7854 * It destroys each deleted domain and builds each new domain.
7855 *
7856 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007857 * The masks don't intersect (don't overlap.) We should setup one
7858 * sched domain for each mask. CPUs not in any of the cpumasks will
7859 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007860 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7861 * it as it is.
7862 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007863 * The passed in 'doms_new' should be kmalloc'd. This routine takes
7864 * ownership of it and will kfree it when done with it. If the caller
Li Zefan700018e2008-11-18 14:02:03 +08007865 * failed the kmalloc call, then it can pass in doms_new == NULL &&
7866 * ndoms_new == 1, and partition_sched_domains() will fallback to
7867 * the single partition 'fallback_doms', it also forces the domains
7868 * to be rebuilt.
Paul Jackson029190c2007-10-18 23:40:20 -07007869 *
Li Zefan700018e2008-11-18 14:02:03 +08007870 * If doms_new == NULL it will be replaced with cpu_online_map.
7871 * ndoms_new == 0 is a special case for destroying existing domains,
7872 * and it will not create the default domain.
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007873 *
Paul Jackson029190c2007-10-18 23:40:20 -07007874 * Call with hotplug lock held
7875 */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007876void partition_sched_domains(int ndoms_new, cpumask_t *doms_new,
7877 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007878{
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007879 int i, j, n;
Paul Jackson029190c2007-10-18 23:40:20 -07007880
Heiko Carstens712555e2008-04-28 11:33:07 +02007881 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007882
Milton Miller73785472007-10-24 18:23:48 +02007883 /* always unregister in case we don't destroy any domains */
7884 unregister_sched_domain_sysctl();
7885
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007886 n = doms_new ? ndoms_new : 0;
Paul Jackson029190c2007-10-18 23:40:20 -07007887
7888 /* Destroy deleted domains */
7889 for (i = 0; i < ndoms_cur; i++) {
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007890 for (j = 0; j < n; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007891 if (cpus_equal(doms_cur[i], doms_new[j])
7892 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007893 goto match1;
7894 }
7895 /* no match - a current sched domain not in new doms_new[] */
7896 detach_destroy_domains(doms_cur + i);
7897match1:
7898 ;
7899 }
7900
Max Krasnyanskye761b772008-07-15 04:43:49 -07007901 if (doms_new == NULL) {
7902 ndoms_cur = 0;
Max Krasnyanskye761b772008-07-15 04:43:49 -07007903 doms_new = &fallback_doms;
7904 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
7905 dattr_new = NULL;
7906 }
7907
Paul Jackson029190c2007-10-18 23:40:20 -07007908 /* Build new domains */
7909 for (i = 0; i < ndoms_new; i++) {
7910 for (j = 0; j < ndoms_cur; j++) {
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007911 if (cpus_equal(doms_new[i], doms_cur[j])
7912 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007913 goto match2;
7914 }
7915 /* no match - add a new doms_new */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007916 __build_sched_domains(doms_new + i,
7917 dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007918match2:
7919 ;
7920 }
7921
7922 /* Remember the new sched domains */
7923 if (doms_cur != &fallback_doms)
7924 kfree(doms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007925 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007926 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007927 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007928 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007929
7930 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007931
Heiko Carstens712555e2008-04-28 11:33:07 +02007932 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07007933}
7934
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007935#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007936int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007937{
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007938 get_online_cpus();
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007939
7940 /* Destroy domains first to force the rebuild */
7941 partition_sched_domains(0, NULL, NULL);
7942
Max Krasnyanskye761b772008-07-15 04:43:49 -07007943 rebuild_sched_domains();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007944 put_online_cpus();
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007945
Max Krasnyanskye761b772008-07-15 04:43:49 -07007946 return 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007947}
7948
7949static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7950{
7951 int ret;
7952
7953 if (buf[0] != '0' && buf[0] != '1')
7954 return -EINVAL;
7955
7956 if (smt)
7957 sched_smt_power_savings = (buf[0] == '1');
7958 else
7959 sched_mc_power_savings = (buf[0] == '1');
7960
7961 ret = arch_reinit_sched_domains();
7962
7963 return ret ? ret : count;
7964}
7965
Adrian Bunk6707de002007-08-12 18:08:19 +02007966#ifdef CONFIG_SCHED_MC
Andi Kleenf718cd42008-07-29 22:33:52 -07007967static ssize_t sched_mc_power_savings_show(struct sysdev_class *class,
7968 char *page)
Adrian Bunk6707de002007-08-12 18:08:19 +02007969{
7970 return sprintf(page, "%u\n", sched_mc_power_savings);
7971}
Andi Kleenf718cd42008-07-29 22:33:52 -07007972static ssize_t sched_mc_power_savings_store(struct sysdev_class *class,
Adrian Bunk6707de002007-08-12 18:08:19 +02007973 const char *buf, size_t count)
7974{
7975 return sched_power_savings_store(buf, count, 0);
7976}
Andi Kleenf718cd42008-07-29 22:33:52 -07007977static SYSDEV_CLASS_ATTR(sched_mc_power_savings, 0644,
7978 sched_mc_power_savings_show,
7979 sched_mc_power_savings_store);
Adrian Bunk6707de002007-08-12 18:08:19 +02007980#endif
7981
7982#ifdef CONFIG_SCHED_SMT
Andi Kleenf718cd42008-07-29 22:33:52 -07007983static ssize_t sched_smt_power_savings_show(struct sysdev_class *dev,
7984 char *page)
Adrian Bunk6707de002007-08-12 18:08:19 +02007985{
7986 return sprintf(page, "%u\n", sched_smt_power_savings);
7987}
Andi Kleenf718cd42008-07-29 22:33:52 -07007988static ssize_t sched_smt_power_savings_store(struct sysdev_class *dev,
Adrian Bunk6707de002007-08-12 18:08:19 +02007989 const char *buf, size_t count)
7990{
7991 return sched_power_savings_store(buf, count, 1);
7992}
Andi Kleenf718cd42008-07-29 22:33:52 -07007993static SYSDEV_CLASS_ATTR(sched_smt_power_savings, 0644,
7994 sched_smt_power_savings_show,
Adrian Bunk6707de002007-08-12 18:08:19 +02007995 sched_smt_power_savings_store);
7996#endif
7997
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007998int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7999{
8000 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07008001
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008002#ifdef CONFIG_SCHED_SMT
8003 if (smt_capable())
8004 err = sysfs_create_file(&cls->kset.kobj,
8005 &attr_sched_smt_power_savings.attr);
8006#endif
8007#ifdef CONFIG_SCHED_MC
8008 if (!err && mc_capable())
8009 err = sysfs_create_file(&cls->kset.kobj,
8010 &attr_sched_mc_power_savings.attr);
8011#endif
8012 return err;
8013}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008014#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008015
Max Krasnyanskye761b772008-07-15 04:43:49 -07008016#ifndef CONFIG_CPUSETS
Linus Torvalds1da177e2005-04-16 15:20:36 -07008017/*
Max Krasnyanskye761b772008-07-15 04:43:49 -07008018 * Add online and remove offline CPUs from the scheduler domains.
8019 * When cpusets are enabled they take over this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008020 */
8021static int update_sched_domains(struct notifier_block *nfb,
8022 unsigned long action, void *hcpu)
8023{
Max Krasnyanskye761b772008-07-15 04:43:49 -07008024 switch (action) {
8025 case CPU_ONLINE:
8026 case CPU_ONLINE_FROZEN:
8027 case CPU_DEAD:
8028 case CPU_DEAD_FROZEN:
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07008029 partition_sched_domains(1, NULL, NULL);
Max Krasnyanskye761b772008-07-15 04:43:49 -07008030 return NOTIFY_OK;
8031
8032 default:
8033 return NOTIFY_DONE;
8034 }
8035}
8036#endif
8037
8038static int update_runtime(struct notifier_block *nfb,
8039 unsigned long action, void *hcpu)
8040{
Peter Zijlstra7def2be2008-06-05 14:49:58 +02008041 int cpu = (int)(long)hcpu;
8042
Linus Torvalds1da177e2005-04-16 15:20:36 -07008043 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008044 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07008045 case CPU_DOWN_PREPARE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02008046 disable_runtime(cpu_rq(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008047 return NOTIFY_OK;
8048
Linus Torvalds1da177e2005-04-16 15:20:36 -07008049 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07008050 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008051 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07008052 case CPU_ONLINE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02008053 enable_runtime(cpu_rq(cpu));
Max Krasnyanskye761b772008-07-15 04:43:49 -07008054 return NOTIFY_OK;
8055
Linus Torvalds1da177e2005-04-16 15:20:36 -07008056 default:
8057 return NOTIFY_DONE;
8058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008059}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008060
8061void __init sched_init_smp(void)
8062{
Nick Piggin5c1e1762006-10-03 01:14:04 -07008063 cpumask_t non_isolated_cpus;
8064
Mike Travis434d53b2008-04-04 18:11:04 -07008065#if defined(CONFIG_NUMA)
8066 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
8067 GFP_KERNEL);
8068 BUG_ON(sched_group_nodes_bycpu == NULL);
8069#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01008070 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02008071 mutex_lock(&sched_domains_mutex);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07008072 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08008073 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07008074 if (cpus_empty(non_isolated_cpus))
8075 cpu_set(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02008076 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01008077 put_online_cpus();
Max Krasnyanskye761b772008-07-15 04:43:49 -07008078
8079#ifndef CONFIG_CPUSETS
Linus Torvalds1da177e2005-04-16 15:20:36 -07008080 /* XXX: Theoretical race here - CPU may be hotplugged now */
8081 hotcpu_notifier(update_sched_domains, 0);
Max Krasnyanskye761b772008-07-15 04:43:49 -07008082#endif
8083
8084 /* RT runtime code needs to handle some hotplug events */
8085 hotcpu_notifier(update_runtime, 0);
8086
Peter Zijlstrab328ca12008-04-29 10:02:46 +02008087 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07008088
8089 /* Move init over to a non-isolated CPU */
Mike Travis7c16ec52008-04-04 18:11:11 -07008090 if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07008091 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01008092 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008093}
8094#else
8095void __init sched_init_smp(void)
8096{
Ingo Molnar19978ca2007-11-09 22:39:38 +01008097 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008098}
8099#endif /* CONFIG_SMP */
8100
8101int in_sched_functions(unsigned long addr)
8102{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008103 return in_lock_functions(addr) ||
8104 (addr >= (unsigned long)__sched_text_start
8105 && addr < (unsigned long)__sched_text_end);
8106}
8107
Alexey Dobriyana9957442007-10-15 17:00:13 +02008108static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02008109{
8110 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02008111 INIT_LIST_HEAD(&cfs_rq->tasks);
Ingo Molnardd41f592007-07-09 18:51:59 +02008112#ifdef CONFIG_FAIR_GROUP_SCHED
8113 cfs_rq->rq = rq;
8114#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02008115 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02008116}
8117
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008118static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
8119{
8120 struct rt_prio_array *array;
8121 int i;
8122
8123 array = &rt_rq->active;
8124 for (i = 0; i < MAX_RT_PRIO; i++) {
8125 INIT_LIST_HEAD(array->queue + i);
8126 __clear_bit(i, array->bitmap);
8127 }
8128 /* delimiter for bitsearch: */
8129 __set_bit(MAX_RT_PRIO, array->bitmap);
8130
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008131#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01008132 rt_rq->highest_prio = MAX_RT_PRIO;
8133#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008134#ifdef CONFIG_SMP
8135 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008136 rt_rq->overloaded = 0;
8137#endif
8138
8139 rt_rq->rt_time = 0;
8140 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008141 rt_rq->rt_runtime = 0;
8142 spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008143
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008144#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01008145 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008146 rt_rq->rq = rq;
8147#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008148}
8149
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008150#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008151static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
8152 struct sched_entity *se, int cpu, int add,
8153 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008154{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008155 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008156 tg->cfs_rq[cpu] = cfs_rq;
8157 init_cfs_rq(cfs_rq, rq);
8158 cfs_rq->tg = tg;
8159 if (add)
8160 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
8161
8162 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008163 /* se could be NULL for init_task_group */
8164 if (!se)
8165 return;
8166
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008167 if (!parent)
8168 se->cfs_rq = &rq->cfs;
8169 else
8170 se->cfs_rq = parent->my_q;
8171
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008172 se->my_q = cfs_rq;
8173 se->load.weight = tg->shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008174 se->load.inv_weight = 0;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008175 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008176}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008177#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008178
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008179#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008180static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
8181 struct sched_rt_entity *rt_se, int cpu, int add,
8182 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008183{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008184 struct rq *rq = cpu_rq(cpu);
8185
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008186 tg->rt_rq[cpu] = rt_rq;
8187 init_rt_rq(rt_rq, rq);
8188 rt_rq->tg = tg;
8189 rt_rq->rt_se = rt_se;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008190 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008191 if (add)
8192 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
8193
8194 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008195 if (!rt_se)
8196 return;
8197
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008198 if (!parent)
8199 rt_se->rt_rq = &rq->rt;
8200 else
8201 rt_se->rt_rq = parent->my_q;
8202
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008203 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008204 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008205 INIT_LIST_HEAD(&rt_se->run_list);
8206}
8207#endif
8208
Linus Torvalds1da177e2005-04-16 15:20:36 -07008209void __init sched_init(void)
8210{
Ingo Molnardd41f592007-07-09 18:51:59 +02008211 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07008212 unsigned long alloc_size = 0, ptr;
8213
8214#ifdef CONFIG_FAIR_GROUP_SCHED
8215 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8216#endif
8217#ifdef CONFIG_RT_GROUP_SCHED
8218 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8219#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008220#ifdef CONFIG_USER_SCHED
8221 alloc_size *= 2;
8222#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008223 /*
8224 * As sched_init() is called before page_alloc is setup,
8225 * we use alloc_bootmem().
8226 */
8227 if (alloc_size) {
David Miller5a9d3222008-04-24 20:46:20 -07008228 ptr = (unsigned long)alloc_bootmem(alloc_size);
Mike Travis434d53b2008-04-04 18:11:04 -07008229
8230#ifdef CONFIG_FAIR_GROUP_SCHED
8231 init_task_group.se = (struct sched_entity **)ptr;
8232 ptr += nr_cpu_ids * sizeof(void **);
8233
8234 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
8235 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008236
8237#ifdef CONFIG_USER_SCHED
8238 root_task_group.se = (struct sched_entity **)ptr;
8239 ptr += nr_cpu_ids * sizeof(void **);
8240
8241 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
8242 ptr += nr_cpu_ids * sizeof(void **);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008243#endif /* CONFIG_USER_SCHED */
8244#endif /* CONFIG_FAIR_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07008245#ifdef CONFIG_RT_GROUP_SCHED
8246 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
8247 ptr += nr_cpu_ids * sizeof(void **);
8248
8249 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008250 ptr += nr_cpu_ids * sizeof(void **);
8251
8252#ifdef CONFIG_USER_SCHED
8253 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
8254 ptr += nr_cpu_ids * sizeof(void **);
8255
8256 root_task_group.rt_rq = (struct rt_rq **)ptr;
8257 ptr += nr_cpu_ids * sizeof(void **);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008258#endif /* CONFIG_USER_SCHED */
8259#endif /* CONFIG_RT_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07008260 }
Ingo Molnardd41f592007-07-09 18:51:59 +02008261
Gregory Haskins57d885f2008-01-25 21:08:18 +01008262#ifdef CONFIG_SMP
8263 init_defrootdomain();
8264#endif
8265
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008266 init_rt_bandwidth(&def_rt_bandwidth,
8267 global_rt_period(), global_rt_runtime());
8268
8269#ifdef CONFIG_RT_GROUP_SCHED
8270 init_rt_bandwidth(&init_task_group.rt_bandwidth,
8271 global_rt_period(), global_rt_runtime());
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008272#ifdef CONFIG_USER_SCHED
8273 init_rt_bandwidth(&root_task_group.rt_bandwidth,
8274 global_rt_period(), RUNTIME_INF);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008275#endif /* CONFIG_USER_SCHED */
8276#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008277
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008278#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008279 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008280 INIT_LIST_HEAD(&init_task_group.children);
8281
8282#ifdef CONFIG_USER_SCHED
8283 INIT_LIST_HEAD(&root_task_group.children);
8284 init_task_group.parent = &root_task_group;
8285 list_add(&init_task_group.siblings, &root_task_group.children);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008286#endif /* CONFIG_USER_SCHED */
8287#endif /* CONFIG_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008288
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08008289 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07008290 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008291
8292 rq = cpu_rq(i);
8293 spin_lock_init(&rq->lock);
Nick Piggin78979862005-06-25 14:57:13 -07008294 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008295 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008296 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008297#ifdef CONFIG_FAIR_GROUP_SCHED
8298 init_task_group.shares = init_task_group_load;
8299 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008300#ifdef CONFIG_CGROUP_SCHED
8301 /*
8302 * How much cpu bandwidth does init_task_group get?
8303 *
8304 * In case of task-groups formed thr' the cgroup filesystem, it
8305 * gets 100% of the cpu resources in the system. This overall
8306 * system cpu resource is divided among the tasks of
8307 * init_task_group and its child task-groups in a fair manner,
8308 * based on each entity's (task or task-group's) weight
8309 * (se->load.weight).
8310 *
8311 * In other words, if init_task_group has 10 tasks of weight
8312 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8313 * then A0's share of the cpu resource is:
8314 *
8315 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
8316 *
8317 * We achieve this by letting init_task_group's tasks sit
8318 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
8319 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008320 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008321#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008322 root_task_group.shares = NICE_0_LOAD;
8323 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008324 /*
8325 * In case of task-groups formed thr' the user id of tasks,
8326 * init_task_group represents tasks belonging to root user.
8327 * Hence it forms a sibling of all subsequent groups formed.
8328 * In this case, init_task_group gets only a fraction of overall
8329 * system cpu resource, based on the weight assigned to root
8330 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
8331 * by letting tasks of init_task_group sit in a separate cfs_rq
8332 * (init_cfs_rq) and having one entity represent this group of
8333 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
8334 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008335 init_tg_cfs_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008336 &per_cpu(init_cfs_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008337 &per_cpu(init_sched_entity, i), i, 1,
8338 root_task_group.se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008339
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008340#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02008341#endif /* CONFIG_FAIR_GROUP_SCHED */
8342
8343 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008344#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008345 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008346#ifdef CONFIG_CGROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008347 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008348#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008349 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008350 init_tg_rt_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008351 &per_cpu(init_rt_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008352 &per_cpu(init_sched_rt_entity, i), i, 1,
8353 root_task_group.rt_se[i]);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008354#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008355#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008356
Ingo Molnardd41f592007-07-09 18:51:59 +02008357 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
8358 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008359#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07008360 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01008361 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008362 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008363 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008364 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07008365 rq->cpu = i;
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04008366 rq->online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008367 rq->migration_thread = NULL;
8368 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01008369 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008370#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01008371 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008372 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008373 }
8374
Peter Williams2dd73a42006-06-27 02:54:34 -07008375 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008376
Avi Kivitye107be32007-07-26 13:40:43 +02008377#ifdef CONFIG_PREEMPT_NOTIFIERS
8378 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
8379#endif
8380
Christoph Lameterc9819f42006-12-10 02:20:25 -08008381#ifdef CONFIG_SMP
Carlos R. Mafra962cf362008-05-15 11:15:37 -03008382 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
Christoph Lameterc9819f42006-12-10 02:20:25 -08008383#endif
8384
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008385#ifdef CONFIG_RT_MUTEXES
8386 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
8387#endif
8388
Linus Torvalds1da177e2005-04-16 15:20:36 -07008389 /*
8390 * The boot idle thread does lazy MMU switching as well:
8391 */
8392 atomic_inc(&init_mm.mm_count);
8393 enter_lazy_tlb(&init_mm, current);
8394
8395 /*
8396 * Make us the idle thread. Technically, schedule() should not be
8397 * called from this thread, however somewhere below it might be,
8398 * but because we are the idle thread, we just pick up running again
8399 * when this runqueue becomes "idle".
8400 */
8401 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02008402 /*
8403 * During early bootup we pretend to be a normal task:
8404 */
8405 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01008406
8407 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008408}
8409
8410#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
8411void __might_sleep(char *file, int line)
8412{
Ingo Molnar48f24c42006-07-03 00:25:40 -07008413#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07008414 static unsigned long prev_jiffy; /* ratelimiting */
8415
Ingo Molnaraef745f2008-08-28 11:34:43 +02008416 if ((!in_atomic() && !irqs_disabled()) ||
8417 system_state != SYSTEM_RUNNING || oops_in_progress)
8418 return;
8419 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
8420 return;
8421 prev_jiffy = jiffies;
8422
8423 printk(KERN_ERR
8424 "BUG: sleeping function called from invalid context at %s:%d\n",
8425 file, line);
8426 printk(KERN_ERR
8427 "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
8428 in_atomic(), irqs_disabled(),
8429 current->pid, current->comm);
8430
8431 debug_show_held_locks(current);
8432 if (irqs_disabled())
8433 print_irqtrace_events(current);
8434 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008435#endif
8436}
8437EXPORT_SYMBOL(__might_sleep);
8438#endif
8439
8440#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008441static void normalize_task(struct rq *rq, struct task_struct *p)
8442{
8443 int on_rq;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02008444
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008445 update_rq_clock(rq);
8446 on_rq = p->se.on_rq;
8447 if (on_rq)
8448 deactivate_task(rq, p, 0);
8449 __setscheduler(rq, p, SCHED_NORMAL, 0);
8450 if (on_rq) {
8451 activate_task(rq, p, 0);
8452 resched_task(rq->curr);
8453 }
8454}
8455
Linus Torvalds1da177e2005-04-16 15:20:36 -07008456void normalize_rt_tasks(void)
8457{
Ingo Molnara0f98a12007-06-17 18:37:45 +02008458 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008459 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07008460 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008461
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008462 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008463 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02008464 /*
8465 * Only normalize user tasks:
8466 */
8467 if (!p->mm)
8468 continue;
8469
Ingo Molnardd41f592007-07-09 18:51:59 +02008470 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008471#ifdef CONFIG_SCHEDSTATS
8472 p->se.wait_start = 0;
8473 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008474 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008475#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02008476
8477 if (!rt_task(p)) {
8478 /*
8479 * Renice negative nice level userspace
8480 * tasks back to 0:
8481 */
8482 if (TASK_NICE(p) < 0 && p->mm)
8483 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008484 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02008485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008486
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008487 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07008488 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008489
Ingo Molnar178be792007-10-15 17:00:18 +02008490 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008491
Ingo Molnarb29739f2006-06-27 02:54:51 -07008492 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008493 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008494 } while_each_thread(g, p);
8495
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008496 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008497}
8498
8499#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07008500
8501#ifdef CONFIG_IA64
8502/*
8503 * These functions are only useful for the IA64 MCA handling.
8504 *
8505 * They can only be called when the whole system has been
8506 * stopped - every CPU needs to be quiescent, and no scheduling
8507 * activity can take place. Using them for anything else would
8508 * be a serious bug, and as a result, they aren't even visible
8509 * under any other configuration.
8510 */
8511
8512/**
8513 * curr_task - return the current task for a given cpu.
8514 * @cpu: the processor in question.
8515 *
8516 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8517 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008518struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008519{
8520 return cpu_curr(cpu);
8521}
8522
8523/**
8524 * set_curr_task - set the current task for a given cpu.
8525 * @cpu: the processor in question.
8526 * @p: the task pointer to set.
8527 *
8528 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008529 * are serviced on a separate stack. It allows the architecture to switch the
8530 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07008531 * must be called with all CPU's synchronized, and interrupts disabled, the
8532 * and caller must save the original value of the current task (see
8533 * curr_task() above) and restore that value before reenabling interrupts and
8534 * re-starting the system.
8535 *
8536 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8537 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008538void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008539{
8540 cpu_curr(cpu) = p;
8541}
8542
8543#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008544
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008545#ifdef CONFIG_FAIR_GROUP_SCHED
8546static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008547{
8548 int i;
8549
8550 for_each_possible_cpu(i) {
8551 if (tg->cfs_rq)
8552 kfree(tg->cfs_rq[i]);
8553 if (tg->se)
8554 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008555 }
8556
8557 kfree(tg->cfs_rq);
8558 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008559}
8560
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008561static
8562int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008563{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008564 struct cfs_rq *cfs_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008565 struct sched_entity *se, *parent_se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008566 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008567 int i;
8568
Mike Travis434d53b2008-04-04 18:11:04 -07008569 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008570 if (!tg->cfs_rq)
8571 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008572 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008573 if (!tg->se)
8574 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008575
8576 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008577
8578 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008579 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008580
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008581 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
8582 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008583 if (!cfs_rq)
8584 goto err;
8585
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008586 se = kmalloc_node(sizeof(struct sched_entity),
8587 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008588 if (!se)
8589 goto err;
8590
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008591 parent_se = parent ? parent->se[i] : NULL;
8592 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008593 }
8594
8595 return 1;
8596
8597 err:
8598 return 0;
8599}
8600
8601static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8602{
8603 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
8604 &cpu_rq(cpu)->leaf_cfs_rq_list);
8605}
8606
8607static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8608{
8609 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
8610}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008611#else /* !CONFG_FAIR_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008612static inline void free_fair_sched_group(struct task_group *tg)
8613{
8614}
8615
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008616static inline
8617int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008618{
8619 return 1;
8620}
8621
8622static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8623{
8624}
8625
8626static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8627{
8628}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008629#endif /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008630
8631#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008632static void free_rt_sched_group(struct task_group *tg)
8633{
8634 int i;
8635
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008636 destroy_rt_bandwidth(&tg->rt_bandwidth);
8637
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008638 for_each_possible_cpu(i) {
8639 if (tg->rt_rq)
8640 kfree(tg->rt_rq[i]);
8641 if (tg->rt_se)
8642 kfree(tg->rt_se[i]);
8643 }
8644
8645 kfree(tg->rt_rq);
8646 kfree(tg->rt_se);
8647}
8648
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008649static
8650int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008651{
8652 struct rt_rq *rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008653 struct sched_rt_entity *rt_se, *parent_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008654 struct rq *rq;
8655 int i;
8656
Mike Travis434d53b2008-04-04 18:11:04 -07008657 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008658 if (!tg->rt_rq)
8659 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008660 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008661 if (!tg->rt_se)
8662 goto err;
8663
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008664 init_rt_bandwidth(&tg->rt_bandwidth,
8665 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008666
8667 for_each_possible_cpu(i) {
8668 rq = cpu_rq(i);
8669
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008670 rt_rq = kmalloc_node(sizeof(struct rt_rq),
8671 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8672 if (!rt_rq)
8673 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008674
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008675 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
8676 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
8677 if (!rt_se)
8678 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008679
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008680 parent_se = parent ? parent->rt_se[i] : NULL;
8681 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent_se);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008682 }
8683
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008684 return 1;
8685
8686 err:
8687 return 0;
8688}
8689
8690static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8691{
8692 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
8693 &cpu_rq(cpu)->leaf_rt_rq_list);
8694}
8695
8696static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8697{
8698 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
8699}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008700#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008701static inline void free_rt_sched_group(struct task_group *tg)
8702{
8703}
8704
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008705static inline
8706int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008707{
8708 return 1;
8709}
8710
8711static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8712{
8713}
8714
8715static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8716{
8717}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008718#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008719
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008720#ifdef CONFIG_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008721static void free_sched_group(struct task_group *tg)
8722{
8723 free_fair_sched_group(tg);
8724 free_rt_sched_group(tg);
8725 kfree(tg);
8726}
8727
8728/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008729struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008730{
8731 struct task_group *tg;
8732 unsigned long flags;
8733 int i;
8734
8735 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8736 if (!tg)
8737 return ERR_PTR(-ENOMEM);
8738
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008739 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008740 goto err;
8741
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008742 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008743 goto err;
8744
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008745 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008746 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008747 register_fair_sched_group(tg, i);
8748 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008749 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008750 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008751
8752 WARN_ON(!parent); /* root should already exist */
8753
8754 tg->parent = parent;
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008755 INIT_LIST_HEAD(&tg->children);
Zhang, Yanmin09f27242030-08-14 15:56:40 +08008756 list_add_rcu(&tg->siblings, &parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008757 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008758
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008759 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008760
8761err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008762 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008763 return ERR_PTR(-ENOMEM);
8764}
8765
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008766/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008767static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008768{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008769 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008770 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008771}
8772
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008773/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008774void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008775{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008776 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008777 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008778
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008779 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008780 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008781 unregister_fair_sched_group(tg, i);
8782 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008783 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008784 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008785 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008786 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008787
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008788 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008789 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008790}
8791
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008792/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008793 * The caller of this function should have put the task in its new group
8794 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8795 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008796 */
8797void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008798{
8799 int on_rq, running;
8800 unsigned long flags;
8801 struct rq *rq;
8802
8803 rq = task_rq_lock(tsk, &flags);
8804
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008805 update_rq_clock(rq);
8806
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008807 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008808 on_rq = tsk->se.on_rq;
8809
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008810 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008811 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008812 if (unlikely(running))
8813 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008814
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008815 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008816
Peter Zijlstra810b3812008-02-29 15:21:01 -05008817#ifdef CONFIG_FAIR_GROUP_SCHED
8818 if (tsk->sched_class->moved_group)
8819 tsk->sched_class->moved_group(tsk);
8820#endif
8821
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008822 if (unlikely(running))
8823 tsk->sched_class->set_curr_task(rq);
8824 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02008825 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008826
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008827 task_rq_unlock(rq, &flags);
8828}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008829#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008830
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008831#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008832static void __set_se_shares(struct sched_entity *se, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008833{
8834 struct cfs_rq *cfs_rq = se->cfs_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008835 int on_rq;
8836
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008837 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008838 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008839 dequeue_entity(cfs_rq, se, 0);
8840
8841 se->load.weight = shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008842 se->load.inv_weight = 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008843
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008844 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008845 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008846}
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008847
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008848static void set_se_shares(struct sched_entity *se, unsigned long shares)
8849{
8850 struct cfs_rq *cfs_rq = se->cfs_rq;
8851 struct rq *rq = cfs_rq->rq;
8852 unsigned long flags;
8853
8854 spin_lock_irqsave(&rq->lock, flags);
8855 __set_se_shares(se, shares);
8856 spin_unlock_irqrestore(&rq->lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008857}
8858
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008859static DEFINE_MUTEX(shares_mutex);
8860
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008861int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008862{
8863 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008864 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01008865
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008866 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008867 * We can't change the weight of the root cgroup.
8868 */
8869 if (!tg->se[0])
8870 return -EINVAL;
8871
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008872 if (shares < MIN_SHARES)
8873 shares = MIN_SHARES;
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008874 else if (shares > MAX_SHARES)
8875 shares = MAX_SHARES;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008876
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008877 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008878 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008879 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008880
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008881 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008882 for_each_possible_cpu(i)
8883 unregister_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008884 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008885 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008886
8887 /* wait for any ongoing reference to this group to finish */
8888 synchronize_sched();
8889
8890 /*
8891 * Now we are free to modify the group's share on each cpu
8892 * w/o tripping rebalance_share or load_balance_fair.
8893 */
8894 tg->shares = shares;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008895 for_each_possible_cpu(i) {
8896 /*
8897 * force a rebalance
8898 */
8899 cfs_rq_set_shares(tg->cfs_rq[i], 0);
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008900 set_se_shares(tg->se[i], shares);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008901 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008902
8903 /*
8904 * Enable load balance activity on this group, by inserting it back on
8905 * each cpu's rq->leaf_cfs_rq_list.
8906 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008907 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008908 for_each_possible_cpu(i)
8909 register_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008910 list_add_rcu(&tg->siblings, &tg->parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008911 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008912done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008913 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008914 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008915}
8916
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008917unsigned long sched_group_shares(struct task_group *tg)
8918{
8919 return tg->shares;
8920}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008921#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008922
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008923#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008924/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008925 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008926 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008927static DEFINE_MUTEX(rt_constraints_mutex);
8928
8929static unsigned long to_ratio(u64 period, u64 runtime)
8930{
8931 if (runtime == RUNTIME_INF)
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008932 return 1ULL << 20;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008933
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008934 return div64_u64(runtime << 20, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008935}
8936
Dhaval Giani521f1a242008-02-28 15:21:56 +05308937/* Must be called with tasklist_lock held */
8938static inline int tg_has_rt_tasks(struct task_group *tg)
8939{
8940 struct task_struct *g, *p;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008941
Dhaval Giani521f1a242008-02-28 15:21:56 +05308942 do_each_thread(g, p) {
8943 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
8944 return 1;
8945 } while_each_thread(g, p);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008946
Dhaval Giani521f1a242008-02-28 15:21:56 +05308947 return 0;
8948}
8949
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008950struct rt_schedulable_data {
8951 struct task_group *tg;
8952 u64 rt_period;
8953 u64 rt_runtime;
8954};
8955
8956static int tg_schedulable(struct task_group *tg, void *data)
8957{
8958 struct rt_schedulable_data *d = data;
8959 struct task_group *child;
8960 unsigned long total, sum = 0;
8961 u64 period, runtime;
8962
8963 period = ktime_to_ns(tg->rt_bandwidth.rt_period);
8964 runtime = tg->rt_bandwidth.rt_runtime;
8965
8966 if (tg == d->tg) {
8967 period = d->rt_period;
8968 runtime = d->rt_runtime;
8969 }
8970
Peter Zijlstra4653f802008-09-23 15:33:44 +02008971 /*
8972 * Cannot have more runtime than the period.
8973 */
8974 if (runtime > period && runtime != RUNTIME_INF)
8975 return -EINVAL;
8976
8977 /*
8978 * Ensure we don't starve existing RT tasks.
8979 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008980 if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
8981 return -EBUSY;
8982
8983 total = to_ratio(period, runtime);
8984
Peter Zijlstra4653f802008-09-23 15:33:44 +02008985 /*
8986 * Nobody can have more than the global setting allows.
8987 */
8988 if (total > to_ratio(global_rt_period(), global_rt_runtime()))
8989 return -EINVAL;
8990
8991 /*
8992 * The sum of our children's runtime should not exceed our own.
8993 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008994 list_for_each_entry_rcu(child, &tg->children, siblings) {
8995 period = ktime_to_ns(child->rt_bandwidth.rt_period);
8996 runtime = child->rt_bandwidth.rt_runtime;
8997
8998 if (child == d->tg) {
8999 period = d->rt_period;
9000 runtime = d->rt_runtime;
9001 }
9002
9003 sum += to_ratio(period, runtime);
9004 }
9005
9006 if (sum > total)
9007 return -EINVAL;
9008
9009 return 0;
9010}
9011
9012static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
9013{
9014 struct rt_schedulable_data data = {
9015 .tg = tg,
9016 .rt_period = period,
9017 .rt_runtime = runtime,
9018 };
9019
9020 return walk_tg_tree(tg_schedulable, tg_nop, &data);
9021}
9022
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009023static int tg_set_bandwidth(struct task_group *tg,
9024 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009025{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009026 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009027
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009028 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05309029 read_lock(&tasklist_lock);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009030 err = __rt_schedulable(tg, rt_period, rt_runtime);
9031 if (err)
Dhaval Giani521f1a242008-02-28 15:21:56 +05309032 goto unlock;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009033
9034 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009035 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
9036 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009037
9038 for_each_possible_cpu(i) {
9039 struct rt_rq *rt_rq = tg->rt_rq[i];
9040
9041 spin_lock(&rt_rq->rt_runtime_lock);
9042 rt_rq->rt_runtime = rt_runtime;
9043 spin_unlock(&rt_rq->rt_runtime_lock);
9044 }
9045 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009046 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05309047 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009048 mutex_unlock(&rt_constraints_mutex);
9049
9050 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009051}
9052
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009053int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
9054{
9055 u64 rt_runtime, rt_period;
9056
9057 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
9058 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
9059 if (rt_runtime_us < 0)
9060 rt_runtime = RUNTIME_INF;
9061
9062 return tg_set_bandwidth(tg, rt_period, rt_runtime);
9063}
9064
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009065long sched_group_rt_runtime(struct task_group *tg)
9066{
9067 u64 rt_runtime_us;
9068
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009069 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009070 return -1;
9071
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009072 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009073 do_div(rt_runtime_us, NSEC_PER_USEC);
9074 return rt_runtime_us;
9075}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009076
9077int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
9078{
9079 u64 rt_runtime, rt_period;
9080
9081 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
9082 rt_runtime = tg->rt_bandwidth.rt_runtime;
9083
Raistlin619b0482008-06-26 18:54:09 +02009084 if (rt_period == 0)
9085 return -EINVAL;
9086
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009087 return tg_set_bandwidth(tg, rt_period, rt_runtime);
9088}
9089
9090long sched_group_rt_period(struct task_group *tg)
9091{
9092 u64 rt_period_us;
9093
9094 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
9095 do_div(rt_period_us, NSEC_PER_USEC);
9096 return rt_period_us;
9097}
9098
9099static int sched_rt_global_constraints(void)
9100{
Peter Zijlstra4653f802008-09-23 15:33:44 +02009101 u64 runtime, period;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009102 int ret = 0;
9103
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07009104 if (sysctl_sched_rt_period <= 0)
9105 return -EINVAL;
9106
Peter Zijlstra4653f802008-09-23 15:33:44 +02009107 runtime = global_rt_runtime();
9108 period = global_rt_period();
9109
9110 /*
9111 * Sanity check on the sysctl variables.
9112 */
9113 if (runtime > period && runtime != RUNTIME_INF)
9114 return -EINVAL;
Peter Zijlstra10b612f2008-06-19 14:22:27 +02009115
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009116 mutex_lock(&rt_constraints_mutex);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009117 read_lock(&tasklist_lock);
Peter Zijlstra4653f802008-09-23 15:33:44 +02009118 ret = __rt_schedulable(NULL, 0, 0);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009119 read_unlock(&tasklist_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009120 mutex_unlock(&rt_constraints_mutex);
9121
9122 return ret;
9123}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009124#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009125static int sched_rt_global_constraints(void)
9126{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009127 unsigned long flags;
9128 int i;
9129
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07009130 if (sysctl_sched_rt_period <= 0)
9131 return -EINVAL;
9132
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009133 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
9134 for_each_possible_cpu(i) {
9135 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
9136
9137 spin_lock(&rt_rq->rt_runtime_lock);
9138 rt_rq->rt_runtime = global_rt_runtime();
9139 spin_unlock(&rt_rq->rt_runtime_lock);
9140 }
9141 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
9142
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009143 return 0;
9144}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009145#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009146
9147int sched_rt_handler(struct ctl_table *table, int write,
9148 struct file *filp, void __user *buffer, size_t *lenp,
9149 loff_t *ppos)
9150{
9151 int ret;
9152 int old_period, old_runtime;
9153 static DEFINE_MUTEX(mutex);
9154
9155 mutex_lock(&mutex);
9156 old_period = sysctl_sched_rt_period;
9157 old_runtime = sysctl_sched_rt_runtime;
9158
9159 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
9160
9161 if (!ret && write) {
9162 ret = sched_rt_global_constraints();
9163 if (ret) {
9164 sysctl_sched_rt_period = old_period;
9165 sysctl_sched_rt_runtime = old_runtime;
9166 } else {
9167 def_rt_bandwidth.rt_runtime = global_rt_runtime();
9168 def_rt_bandwidth.rt_period =
9169 ns_to_ktime(global_rt_period());
9170 }
9171 }
9172 mutex_unlock(&mutex);
9173
9174 return ret;
9175}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009176
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009177#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009178
9179/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009180static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009181{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009182 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
9183 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009184}
9185
9186static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02009187cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009188{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009189 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009190
Paul Menage2b01dfe2007-10-24 18:23:50 +02009191 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009192 /* This is early initialization for the top cgroup */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009193 return &init_task_group.css;
9194 }
9195
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009196 parent = cgroup_tg(cgrp->parent);
9197 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009198 if (IS_ERR(tg))
9199 return ERR_PTR(-ENOMEM);
9200
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009201 return &tg->css;
9202}
9203
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009204static void
9205cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009206{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009207 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009208
9209 sched_destroy_group(tg);
9210}
9211
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009212static int
9213cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
9214 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009215{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009216#ifdef CONFIG_RT_GROUP_SCHED
9217 /* Don't accept realtime tasks when there is no way for them to run */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009218 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009219 return -EINVAL;
9220#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009221 /* We don't support RT-tasks being in separate groups */
9222 if (tsk->sched_class != &fair_sched_class)
9223 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009224#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009225
9226 return 0;
9227}
9228
9229static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02009230cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009231 struct cgroup *old_cont, struct task_struct *tsk)
9232{
9233 sched_move_task(tsk);
9234}
9235
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009236#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagef4c753b2008-04-29 00:59:56 -07009237static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
Paul Menage2b01dfe2007-10-24 18:23:50 +02009238 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009239{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009240 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009241}
9242
Paul Menagef4c753b2008-04-29 00:59:56 -07009243static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009244{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009245 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009246
9247 return (u64) tg->shares;
9248}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009249#endif /* CONFIG_FAIR_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009250
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009251#ifdef CONFIG_RT_GROUP_SCHED
Mirco Tischler0c708142008-05-14 16:05:46 -07009252static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Paul Menage06ecb272008-04-29 01:00:06 -07009253 s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009254{
Paul Menage06ecb272008-04-29 01:00:06 -07009255 return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009256}
9257
Paul Menage06ecb272008-04-29 01:00:06 -07009258static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009259{
Paul Menage06ecb272008-04-29 01:00:06 -07009260 return sched_group_rt_runtime(cgroup_tg(cgrp));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009261}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009262
9263static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
9264 u64 rt_period_us)
9265{
9266 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
9267}
9268
9269static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
9270{
9271 return sched_group_rt_period(cgroup_tg(cgrp));
9272}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009273#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009274
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009275static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009276#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009277 {
9278 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07009279 .read_u64 = cpu_shares_read_u64,
9280 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009281 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009282#endif
9283#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009284 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009285 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07009286 .read_s64 = cpu_rt_runtime_read,
9287 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009288 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009289 {
9290 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07009291 .read_u64 = cpu_rt_period_read_uint,
9292 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009293 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009294#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009295};
9296
9297static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
9298{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009299 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009300}
9301
9302struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01009303 .name = "cpu",
9304 .create = cpu_cgroup_create,
9305 .destroy = cpu_cgroup_destroy,
9306 .can_attach = cpu_cgroup_can_attach,
9307 .attach = cpu_cgroup_attach,
9308 .populate = cpu_cgroup_populate,
9309 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009310 .early_init = 1,
9311};
9312
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009313#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009314
9315#ifdef CONFIG_CGROUP_CPUACCT
9316
9317/*
9318 * CPU accounting code for task groups.
9319 *
9320 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
9321 * (balbir@in.ibm.com).
9322 */
9323
9324/* track cpu usage of a group of tasks */
9325struct cpuacct {
9326 struct cgroup_subsys_state css;
9327 /* cpuusage holds pointer to a u64-type object on every cpu */
9328 u64 *cpuusage;
9329};
9330
9331struct cgroup_subsys cpuacct_subsys;
9332
9333/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309334static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009335{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309336 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009337 struct cpuacct, css);
9338}
9339
9340/* return cpu accounting group to which this task belongs */
9341static inline struct cpuacct *task_ca(struct task_struct *tsk)
9342{
9343 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
9344 struct cpuacct, css);
9345}
9346
9347/* create a new cpu accounting group */
9348static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05309349 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009350{
9351 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
9352
9353 if (!ca)
9354 return ERR_PTR(-ENOMEM);
9355
9356 ca->cpuusage = alloc_percpu(u64);
9357 if (!ca->cpuusage) {
9358 kfree(ca);
9359 return ERR_PTR(-ENOMEM);
9360 }
9361
9362 return &ca->css;
9363}
9364
9365/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009366static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05309367cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009368{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309369 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009370
9371 free_percpu(ca->cpuusage);
9372 kfree(ca);
9373}
9374
9375/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309376static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009377{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309378 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009379 u64 totalcpuusage = 0;
9380 int i;
9381
9382 for_each_possible_cpu(i) {
9383 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9384
9385 /*
9386 * Take rq->lock to make 64-bit addition safe on 32-bit
9387 * platforms.
9388 */
9389 spin_lock_irq(&cpu_rq(i)->lock);
9390 totalcpuusage += *cpuusage;
9391 spin_unlock_irq(&cpu_rq(i)->lock);
9392 }
9393
9394 return totalcpuusage;
9395}
9396
Dhaval Giani0297b802008-02-29 10:02:44 +05309397static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
9398 u64 reset)
9399{
9400 struct cpuacct *ca = cgroup_ca(cgrp);
9401 int err = 0;
9402 int i;
9403
9404 if (reset) {
9405 err = -EINVAL;
9406 goto out;
9407 }
9408
9409 for_each_possible_cpu(i) {
9410 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9411
9412 spin_lock_irq(&cpu_rq(i)->lock);
9413 *cpuusage = 0;
9414 spin_unlock_irq(&cpu_rq(i)->lock);
9415 }
9416out:
9417 return err;
9418}
9419
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009420static struct cftype files[] = {
9421 {
9422 .name = "usage",
Paul Menagef4c753b2008-04-29 00:59:56 -07009423 .read_u64 = cpuusage_read,
9424 .write_u64 = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009425 },
9426};
9427
Dhaval Giani32cd7562008-02-29 10:02:43 +05309428static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009429{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309430 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009431}
9432
9433/*
9434 * charge this task's execution time to its accounting group.
9435 *
9436 * called with rq->lock held.
9437 */
9438static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
9439{
9440 struct cpuacct *ca;
9441
9442 if (!cpuacct_subsys.active)
9443 return;
9444
9445 ca = task_ca(tsk);
9446 if (ca) {
9447 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
9448
9449 *cpuusage += cputime;
9450 }
9451}
9452
9453struct cgroup_subsys cpuacct_subsys = {
9454 .name = "cpuacct",
9455 .create = cpuacct_create,
9456 .destroy = cpuacct_destroy,
9457 .populate = cpuacct_populate,
9458 .subsys_id = cpuacct_subsys_id,
9459};
9460#endif /* CONFIG_CGROUP_CPUACCT */