blob: 8fc0d5aa43b1c6127c9d882c5bfa214f10f3c5d6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * kernel/sched.c
3 *
4 * Kernel scheduler and related syscalls
5 *
6 * Copyright (C) 1991-2002 Linus Torvalds
7 *
8 * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and
9 * make semaphores SMP safe
10 * 1998-11-19 Implemented schedule_timeout() and related stuff
11 * by Andrea Arcangeli
12 * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
13 * hybrid priority-list and round-robin design with
14 * an array-switch method of distributing timeslices
15 * and per-CPU runqueues. Cleanups and useful suggestions
16 * by Davide Libenzi, preemptible kernel bits by Robert Love.
17 * 2003-09-03 Interactivity tuning by Con Kolivas.
18 * 2004-04-02 Scheduler domains code by Nick Piggin
Ingo Molnarc31f2e82007-07-09 18:52:01 +020019 * 2007-04-15 Work begun on replacing all interactivity tuning with a
20 * fair scheduling design by Con Kolivas.
21 * 2007-05-05 Load balancing (smp-nice) and other improvements
22 * by Peter Williams
23 * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith
24 * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri
Ingo Molnarb9131762008-01-25 21:08:19 +010025 * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins,
26 * Thomas Gleixner, Mike Kravetz
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
28
29#include <linux/mm.h>
30#include <linux/module.h>
31#include <linux/nmi.h>
32#include <linux/init.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020033#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/highmem.h>
35#include <linux/smp_lock.h>
36#include <asm/mmu_context.h>
37#include <linux/interrupt.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080038#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/completion.h>
40#include <linux/kernel_stat.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070041#include <linux/debug_locks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/security.h>
43#include <linux/notifier.h>
44#include <linux/profile.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080045#include <linux/freezer.h>
akpm@osdl.org198e2f12006-01-12 01:05:30 -080046#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/blkdev.h>
48#include <linux/delay.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070049#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/smp.h>
51#include <linux/threads.h>
52#include <linux/timer.h>
53#include <linux/rcupdate.h>
54#include <linux/cpu.h>
55#include <linux/cpuset.h>
56#include <linux/percpu.h>
57#include <linux/kthread.h>
Alexey Dobriyanb5aadf72008-10-06 13:23:43 +040058#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/seq_file.h>
Nick Piggine692ab52007-07-26 13:40:43 +020060#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/syscalls.h>
62#include <linux/times.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070063#include <linux/tsacct_kern.h>
bibo maoc6fd91f2006-03-26 01:38:20 -080064#include <linux/kprobes.h>
Shailabh Nagar0ff92242006-07-14 00:24:37 -070065#include <linux/delayacct.h>
Eric Dumazet5517d862007-05-08 00:32:57 -070066#include <linux/reciprocal_div.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020067#include <linux/unistd.h>
Jens Axboef5ff8422007-09-21 09:19:54 +020068#include <linux/pagemap.h>
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +010069#include <linux/hrtimer.h>
Reynes Philippe30914a52008-03-17 16:19:05 -070070#include <linux/tick.h>
Mike Travis434d53b2008-04-04 18:11:04 -070071#include <linux/bootmem.h>
Peter Zijlstraf00b45c2008-04-19 19:45:00 +020072#include <linux/debugfs.h>
73#include <linux/ctype.h>
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +020074#include <linux/ftrace.h>
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -040075#include <trace/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Eric Dumazet5517d862007-05-08 00:32:57 -070077#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020078#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Gregory Haskins6e0534f2008-05-12 21:21:01 +020080#include "sched_cpupri.h"
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/*
83 * Convert user-nice values [ -20 ... 0 ... 19 ]
84 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
85 * and back.
86 */
87#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
88#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
89#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
90
91/*
92 * 'User priority' is the nice value converted to something we
93 * can work with better when scaling various scheduler parameters,
94 * it's a [ 0 ... 39 ] range.
95 */
96#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
97#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
98#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
99
100/*
Ingo Molnard7876a02008-01-25 21:08:19 +0100101 * Helpers for converting nanosecond timing to jiffy resolution
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
Eric Dumazetd6322fa2007-11-09 22:39:38 +0100103#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200105#define NICE_0_LOAD SCHED_LOAD_SCALE
106#define NICE_0_SHIFT SCHED_LOAD_SHIFT
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/*
109 * These are the 'tuning knobs' of the scheduler:
110 *
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200111 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * Timeslices get refilled after they expire.
113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700115
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200116/*
117 * single value that denotes runtime == period, ie unlimited time.
118 */
119#define RUNTIME_INF ((u64)~0ULL)
120
Mathieu Desnoyers7e066fb2008-11-14 17:47:47 -0500121DEFINE_TRACE(sched_wait_task);
122DEFINE_TRACE(sched_wakeup);
123DEFINE_TRACE(sched_wakeup_new);
124DEFINE_TRACE(sched_switch);
125DEFINE_TRACE(sched_migrate_task);
126
Eric Dumazet5517d862007-05-08 00:32:57 -0700127#ifdef CONFIG_SMP
128/*
129 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
130 * Since cpu_power is a 'constant', we can use a reciprocal divide.
131 */
132static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
133{
134 return reciprocal_divide(load, sg->reciprocal_cpu_power);
135}
136
137/*
138 * Each time a sched group cpu_power is changed,
139 * we must compute its reciprocal value
140 */
141static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
142{
143 sg->__cpu_power += val;
144 sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
145}
146#endif
147
Ingo Molnare05606d2007-07-09 18:51:59 +0200148static inline int rt_policy(int policy)
149{
Roel Kluin3f33a7c2008-05-13 23:44:11 +0200150 if (unlikely(policy == SCHED_FIFO || policy == SCHED_RR))
Ingo Molnare05606d2007-07-09 18:51:59 +0200151 return 1;
152 return 0;
153}
154
155static inline int task_has_rt_policy(struct task_struct *p)
156{
157 return rt_policy(p->policy);
158}
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200161 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200163struct rt_prio_array {
164 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
165 struct list_head queue[MAX_RT_PRIO];
166};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200168struct rt_bandwidth {
Ingo Molnarea736ed2008-03-25 13:51:45 +0100169 /* nests inside the rq lock: */
170 spinlock_t rt_runtime_lock;
171 ktime_t rt_period;
172 u64 rt_runtime;
173 struct hrtimer rt_period_timer;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200174};
175
176static struct rt_bandwidth def_rt_bandwidth;
177
178static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
179
180static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
181{
182 struct rt_bandwidth *rt_b =
183 container_of(timer, struct rt_bandwidth, rt_period_timer);
184 ktime_t now;
185 int overrun;
186 int idle = 0;
187
188 for (;;) {
189 now = hrtimer_cb_get_time(timer);
190 overrun = hrtimer_forward(timer, now, rt_b->rt_period);
191
192 if (!overrun)
193 break;
194
195 idle = do_sched_rt_period_timer(rt_b, overrun);
196 }
197
198 return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
199}
200
201static
202void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
203{
204 rt_b->rt_period = ns_to_ktime(period);
205 rt_b->rt_runtime = runtime;
206
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200207 spin_lock_init(&rt_b->rt_runtime_lock);
208
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200209 hrtimer_init(&rt_b->rt_period_timer,
210 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
211 rt_b->rt_period_timer.function = sched_rt_period_timer;
Thomas Gleixnerccc7dad2008-09-29 15:47:42 +0200212 rt_b->rt_period_timer.cb_mode = HRTIMER_CB_IRQSAFE_UNLOCKED;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200213}
214
Krzysztof Heltc8bfff62008-09-05 23:46:19 +0200215static inline int rt_bandwidth_enabled(void)
216{
217 return sysctl_sched_rt_runtime >= 0;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200218}
219
220static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
221{
222 ktime_t now;
223
Peter Zijlstra0b148fa2008-08-19 12:33:04 +0200224 if (rt_bandwidth_enabled() && rt_b->rt_runtime == RUNTIME_INF)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200225 return;
226
227 if (hrtimer_active(&rt_b->rt_period_timer))
228 return;
229
230 spin_lock(&rt_b->rt_runtime_lock);
231 for (;;) {
232 if (hrtimer_active(&rt_b->rt_period_timer))
233 break;
234
235 now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
236 hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700237 hrtimer_start_expires(&rt_b->rt_period_timer,
238 HRTIMER_MODE_ABS);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200239 }
240 spin_unlock(&rt_b->rt_runtime_lock);
241}
242
243#ifdef CONFIG_RT_GROUP_SCHED
244static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
245{
246 hrtimer_cancel(&rt_b->rt_period_timer);
247}
248#endif
249
Heiko Carstens712555e2008-04-28 11:33:07 +0200250/*
251 * sched_domains_mutex serializes calls to arch_init_sched_domains,
252 * detach_destroy_domains and partition_sched_domains.
253 */
254static DEFINE_MUTEX(sched_domains_mutex);
255
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100256#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200257
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700258#include <linux/cgroup.h>
259
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200260struct cfs_rq;
261
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100262static LIST_HEAD(task_groups);
263
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200264/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200265struct task_group {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100266#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700267 struct cgroup_subsys_state css;
268#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100269
Arun R Bharadwaj6c415b92008-12-01 20:49:05 +0530270#ifdef CONFIG_USER_SCHED
271 uid_t uid;
272#endif
273
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100274#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200275 /* schedulable entities of this group on each cpu */
276 struct sched_entity **se;
277 /* runqueue "owned" by this group on each cpu */
278 struct cfs_rq **cfs_rq;
279 unsigned long shares;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100280#endif
281
282#ifdef CONFIG_RT_GROUP_SCHED
283 struct sched_rt_entity **rt_se;
284 struct rt_rq **rt_rq;
285
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200286 struct rt_bandwidth rt_bandwidth;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100287#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100288
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100289 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100290 struct list_head list;
Peter Zijlstraf473aa52008-04-19 19:45:00 +0200291
292 struct task_group *parent;
293 struct list_head siblings;
294 struct list_head children;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200295};
296
Dhaval Giani354d60c2008-04-19 19:44:59 +0200297#ifdef CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200298
Arun R Bharadwaj6c415b92008-12-01 20:49:05 +0530299/* Helper function to pass uid information to create_sched_user() */
300void set_tg_uid(struct user_struct *user)
301{
302 user->tg->uid = user->uid;
303}
304
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200305/*
306 * Root task group.
307 * Every UID task group (including init_task_group aka UID-0) will
308 * be a child to this group.
309 */
310struct task_group root_task_group;
311
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100312#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200313/* Default task group's sched entity on each cpu */
314static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
315/* Default task group's cfs_rq on each cpu */
316static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200317#endif /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100318
319#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100320static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
321static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200322#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +0200323#else /* !CONFIG_USER_SCHED */
Peter Zijlstraeff766a2008-04-19 19:45:00 +0200324#define root_task_group init_task_group
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +0200325#endif /* CONFIG_USER_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100326
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100327/* task_group_lock serializes add/remove of task groups and also changes to
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100328 * a task group's cpu shares.
329 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100330static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100331
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100332#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100333#ifdef CONFIG_USER_SCHED
Ingo Molnar0eab9142008-01-25 21:08:19 +0100334# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200335#else /* !CONFIG_USER_SCHED */
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100336# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200337#endif /* CONFIG_USER_SCHED */
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200338
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800339/*
Lai Jiangshan2e084782008-06-12 16:42:58 +0800340 * A weight of 0 or 1 can cause arithmetics problems.
341 * A weight of a cfs_rq is the sum of weights of which entities
342 * are queued on this cfs_rq, so a weight of a entity should not be
343 * too large, so as the shares value of a task group.
Miao Xiecb4ad1f2008-04-28 12:54:56 +0800344 * (The default weight is 1024 - so there's no practical
345 * limitation from this.)
346 */
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200347#define MIN_SHARES 2
Lai Jiangshan2e084782008-06-12 16:42:58 +0800348#define MAX_SHARES (1UL << 18)
Peter Zijlstra18d95a22008-04-19 19:45:00 +0200349
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100350static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100351#endif
352
353/* Default task group.
354 * Every task in system belong to this group at bootup.
355 */
Mike Travis434d53b2008-04-04 18:11:04 -0700356struct task_group init_task_group;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200357
358/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200359static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200360{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200361 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200362
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100363#ifdef CONFIG_USER_SCHED
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200364 tg = p->user->tg;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100365#elif defined(CONFIG_CGROUP_SCHED)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700366 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
367 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200368#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100369 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200370#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200371 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200372}
373
374/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100375static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200376{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100377#ifdef CONFIG_FAIR_GROUP_SCHED
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100378 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
379 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100380#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100381
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100382#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100383 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
384 p->rt.parent = task_group(p)->rt_se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100385#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200386}
387
388#else
389
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100390static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Peter Zijlstra83378262008-06-27 13:41:37 +0200391static inline struct task_group *task_group(struct task_struct *p)
392{
393 return NULL;
394}
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200395
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100396#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200397
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200398/* CFS-related fields in a runqueue */
399struct cfs_rq {
400 struct load_weight load;
401 unsigned long nr_running;
402
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200403 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200404 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200405
406 struct rb_root tasks_timeline;
407 struct rb_node *rb_leftmost;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +0200408
409 struct list_head tasks;
410 struct list_head *balance_iterator;
411
412 /*
413 * 'curr' points to currently running entity on this cfs_rq.
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200414 * It is set to NULL otherwise (i.e when none are currently running).
415 */
Peter Zijlstra47932412008-11-04 21:25:09 +0100416 struct sched_entity *curr, *next, *last;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200417
Peter Zijlstra5ac5c4d2008-11-10 10:46:32 +0100418 unsigned int nr_spread_over;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200419
Ingo Molnar62160e32007-10-15 17:00:03 +0200420#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200421 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
422
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100423 /*
424 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200425 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
426 * (like users, containers etc.)
427 *
428 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
429 * list is used during load balance.
430 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100431 struct list_head leaf_cfs_rq_list;
432 struct task_group *tg; /* group that "owns" this runqueue */
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200433
434#ifdef CONFIG_SMP
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200435 /*
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200436 * the part of load.weight contributed by tasks
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200437 */
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200438 unsigned long task_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200439
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200440 /*
441 * h_load = weight * f(tg)
442 *
443 * Where f(tg) is the recursive weight fraction assigned to
444 * this group.
445 */
446 unsigned long h_load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200447
Peter Zijlstrac8cba852008-06-27 13:41:23 +0200448 /*
449 * this cpu's part of tg->shares
450 */
451 unsigned long shares;
Peter Zijlstraf1d239f2008-06-27 13:41:38 +0200452
453 /*
454 * load.weight at the time we set shares
455 */
456 unsigned long rq_weight;
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200457#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200458#endif
459};
460
461/* Real-Time classes' related field in a runqueue: */
462struct rt_rq {
463 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100464 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100465#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100466 int highest_prio; /* highest queued rt task prio */
467#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100468#ifdef CONFIG_SMP
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100469 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100470 int overloaded;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100471#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100472 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100473 u64 rt_time;
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200474 u64 rt_runtime;
Ingo Molnarea736ed2008-03-25 13:51:45 +0100475 /* Nests inside the rq lock: */
Peter Zijlstraac086bc2008-04-19 19:44:58 +0200476 spinlock_t rt_runtime_lock;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100477
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100478#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100479 unsigned long rt_nr_boosted;
480
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100481 struct rq *rq;
482 struct list_head leaf_rt_rq_list;
483 struct task_group *tg;
484 struct sched_rt_entity *rt_se;
485#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200486};
487
Gregory Haskins57d885f2008-01-25 21:08:18 +0100488#ifdef CONFIG_SMP
489
490/*
491 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100492 * variables. Each exclusive cpuset essentially defines an island domain by
493 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100494 * exclusive cpuset is created, we also create and attach a new root-domain
495 * object.
496 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100497 */
498struct root_domain {
499 atomic_t refcount;
Rusty Russellc6c49272008-11-25 02:35:05 +1030500 cpumask_var_t span;
501 cpumask_var_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100502
Ingo Molnar0eab9142008-01-25 21:08:19 +0100503 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100504 * The "RT overload" flag: it gets set if a CPU has more than
505 * one runnable RT task.
506 */
Rusty Russellc6c49272008-11-25 02:35:05 +1030507 cpumask_var_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100508 atomic_t rto_count;
Gregory Haskins6e0534f2008-05-12 21:21:01 +0200509#ifdef CONFIG_SMP
510 struct cpupri cpupri;
511#endif
Vaidyanathan Srinivasan7a09b1a2008-12-18 23:26:22 +0530512#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
513 /*
514 * Preferred wake up cpu nominated by sched_mc balance that will be
515 * used when most cpus are idle in the system indicating overall very
516 * low system utilisation. Triggered at POWERSAVINGS_BALANCE_WAKEUP(2)
517 */
518 unsigned int sched_mc_preferred_wakeup_cpu;
519#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +0100520};
521
Gregory Haskinsdc938522008-01-25 21:08:26 +0100522/*
523 * By default the system creates a single root-domain with all cpus as
524 * members (mimicking the global state we have today).
525 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100526static struct root_domain def_root_domain;
527
528#endif
529
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200530/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 * This is the main, per-CPU runqueue data structure.
532 *
533 * Locking rule: those places that want to lock multiple runqueues
534 * (such as the load balancing or the thread migration code), lock
535 * acquire operations must be ordered by ascending &runqueue.
536 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700537struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200538 /* runqueue lock: */
539 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 /*
542 * nr_running and cpu_load should be in the same cacheline because
543 * remote CPUs use both these fields when doing load calculation.
544 */
545 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200546 #define CPU_LOAD_IDX_MAX 5
547 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700548 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700549#ifdef CONFIG_NO_HZ
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200550 unsigned long last_tick_seen;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700551 unsigned char in_nohz_recently;
552#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200553 /* capture load from *all* tasks on this cpu: */
554 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200555 unsigned long nr_load_updates;
556 u64 nr_switches;
557
558 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100559 struct rt_rq rt;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100560
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200561#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200562 /* list of leaf cfs_rq on this cpu: */
563 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100564#endif
565#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100566 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /*
570 * This is part of a global counter where only the total sum
571 * over all CPUs matters. A task can increase this counter on
572 * one CPU and if it got migrated afterwards it may decrease
573 * it on another CPU. Always updated under the runqueue lock:
574 */
575 unsigned long nr_uninterruptible;
576
Ingo Molnar36c8b582006-07-03 00:25:41 -0700577 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800578 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200580
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200581 u64 clock;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 atomic_t nr_iowait;
584
585#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100586 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 struct sched_domain *sd;
588
589 /* For active balancing */
590 int active_balance;
591 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200592 /* cpu of this runqueue: */
593 int cpu;
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -0400594 int online;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Peter Zijlstraa8a51d52008-06-27 13:41:26 +0200596 unsigned long avg_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Ingo Molnar36c8b582006-07-03 00:25:41 -0700598 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 struct list_head migration_queue;
600#endif
601
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100602#ifdef CONFIG_SCHED_HRTICK
Peter Zijlstra31656512008-07-18 18:01:23 +0200603#ifdef CONFIG_SMP
604 int hrtick_csd_pending;
605 struct call_single_data hrtick_csd;
606#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100607 struct hrtimer hrtick_timer;
608#endif
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610#ifdef CONFIG_SCHEDSTATS
611 /* latency stats */
612 struct sched_info rq_sched_info;
613
614 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200615 unsigned int yld_exp_empty;
616 unsigned int yld_act_empty;
617 unsigned int yld_both_empty;
618 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200621 unsigned int sched_switch;
622 unsigned int sched_count;
623 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200626 unsigned int ttwu_count;
627 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200628
629 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200630 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631#endif
632};
633
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700634static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Peter Zijlstra15afe092008-09-20 23:38:02 +0200636static inline void check_preempt_curr(struct rq *rq, struct task_struct *p, int sync)
Ingo Molnardd41f592007-07-09 18:51:59 +0200637{
Peter Zijlstra15afe092008-09-20 23:38:02 +0200638 rq->curr->sched_class->check_preempt_curr(rq, p, sync);
Ingo Molnardd41f592007-07-09 18:51:59 +0200639}
640
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700641static inline int cpu_of(struct rq *rq)
642{
643#ifdef CONFIG_SMP
644 return rq->cpu;
645#else
646 return 0;
647#endif
648}
649
Ingo Molnar20d315d2007-07-09 18:51:58 +0200650/*
Nick Piggin674311d2005-06-25 14:57:27 -0700651 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700652 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700653 *
654 * The domain tree of any CPU may only be accessed from within
655 * preempt-disabled sections.
656 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700657#define for_each_domain(cpu, __sd) \
658 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
661#define this_rq() (&__get_cpu_var(runqueues))
662#define task_rq(p) cpu_rq(task_cpu(p))
663#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
664
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200665static inline void update_rq_clock(struct rq *rq)
666{
667 rq->clock = sched_clock_cpu(cpu_of(rq));
668}
669
Ingo Molnare436d802007-07-19 21:28:35 +0200670/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200671 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
672 */
673#ifdef CONFIG_SCHED_DEBUG
674# define const_debug __read_mostly
675#else
676# define const_debug static const
677#endif
678
Ingo Molnar017730c2008-05-12 21:20:52 +0200679/**
680 * runqueue_is_locked
681 *
682 * Returns true if the current cpu runqueue is locked.
683 * This interface allows printk to be called with the runqueue lock
684 * held and know whether or not it is OK to wake up the klogd.
685 */
686int runqueue_is_locked(void)
687{
688 int cpu = get_cpu();
689 struct rq *rq = cpu_rq(cpu);
690 int ret;
691
692 ret = spin_is_locked(&rq->lock);
693 put_cpu();
694 return ret;
695}
696
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200697/*
698 * Debugging: various feature bits
699 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200700
701#define SCHED_FEAT(name, enabled) \
702 __SCHED_FEAT_##name ,
703
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200704enum {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200705#include "sched_features.h"
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200706};
707
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200708#undef SCHED_FEAT
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200709
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200710#define SCHED_FEAT(name, enabled) \
711 (1UL << __SCHED_FEAT_##name) * enabled |
712
713const_debug unsigned int sysctl_sched_features =
714#include "sched_features.h"
715 0;
716
717#undef SCHED_FEAT
718
719#ifdef CONFIG_SCHED_DEBUG
720#define SCHED_FEAT(name, enabled) \
721 #name ,
722
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700723static __read_mostly char *sched_feat_names[] = {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200724#include "sched_features.h"
725 NULL
726};
727
728#undef SCHED_FEAT
729
Li Zefan34f3a812008-10-30 15:23:32 +0800730static int sched_feat_show(struct seq_file *m, void *v)
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200731{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200732 int i;
733
734 for (i = 0; sched_feat_names[i]; i++) {
Li Zefan34f3a812008-10-30 15:23:32 +0800735 if (!(sysctl_sched_features & (1UL << i)))
736 seq_puts(m, "NO_");
737 seq_printf(m, "%s ", sched_feat_names[i]);
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200738 }
Li Zefan34f3a812008-10-30 15:23:32 +0800739 seq_puts(m, "\n");
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200740
Li Zefan34f3a812008-10-30 15:23:32 +0800741 return 0;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200742}
743
744static ssize_t
745sched_feat_write(struct file *filp, const char __user *ubuf,
746 size_t cnt, loff_t *ppos)
747{
748 char buf[64];
749 char *cmp = buf;
750 int neg = 0;
751 int i;
752
753 if (cnt > 63)
754 cnt = 63;
755
756 if (copy_from_user(&buf, ubuf, cnt))
757 return -EFAULT;
758
759 buf[cnt] = 0;
760
Ingo Molnarc24b7c52008-04-18 10:55:34 +0200761 if (strncmp(buf, "NO_", 3) == 0) {
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200762 neg = 1;
763 cmp += 3;
764 }
765
766 for (i = 0; sched_feat_names[i]; i++) {
767 int len = strlen(sched_feat_names[i]);
768
769 if (strncmp(cmp, sched_feat_names[i], len) == 0) {
770 if (neg)
771 sysctl_sched_features &= ~(1UL << i);
772 else
773 sysctl_sched_features |= (1UL << i);
774 break;
775 }
776 }
777
778 if (!sched_feat_names[i])
779 return -EINVAL;
780
781 filp->f_pos += cnt;
782
783 return cnt;
784}
785
Li Zefan34f3a812008-10-30 15:23:32 +0800786static int sched_feat_open(struct inode *inode, struct file *filp)
787{
788 return single_open(filp, sched_feat_show, NULL);
789}
790
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200791static struct file_operations sched_feat_fops = {
Li Zefan34f3a812008-10-30 15:23:32 +0800792 .open = sched_feat_open,
793 .write = sched_feat_write,
794 .read = seq_read,
795 .llseek = seq_lseek,
796 .release = single_release,
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200797};
798
799static __init int sched_init_debug(void)
800{
Peter Zijlstraf00b45c2008-04-19 19:45:00 +0200801 debugfs_create_file("sched_features", 0644, NULL, NULL,
802 &sched_feat_fops);
803
804 return 0;
805}
806late_initcall(sched_init_debug);
807
808#endif
809
810#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200811
812/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100813 * Number of tasks to iterate in a single balance run.
814 * Limited because this is done with IRQs disabled.
815 */
816const_debug unsigned int sysctl_sched_nr_migrate = 32;
817
818/*
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200819 * ratelimit for updating the group shares.
Peter Zijlstra55cd5342008-08-04 08:54:26 +0200820 * default: 0.25ms
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200821 */
Peter Zijlstra55cd5342008-08-04 08:54:26 +0200822unsigned int sysctl_sched_shares_ratelimit = 250000;
Peter Zijlstra2398f2c2008-06-27 13:41:35 +0200823
824/*
Peter Zijlstraffda12a2008-10-17 19:27:02 +0200825 * Inject some fuzzyness into changing the per-cpu group shares
826 * this avoids remote rq-locks at the expense of fairness.
827 * default: 4
828 */
829unsigned int sysctl_sched_shares_thresh = 4;
830
831/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100832 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100833 * default: 1s
834 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100835unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100836
Ingo Molnar6892b752008-02-13 14:02:36 +0100837static __read_mostly int scheduler_running;
838
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100839/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100840 * part of the period that we allow rt tasks to run in us.
841 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100842 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100843int sysctl_sched_rt_runtime = 950000;
844
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200845static inline u64 global_rt_period(void)
846{
847 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
848}
849
850static inline u64 global_rt_runtime(void)
851{
roel kluine26873b2008-07-22 16:51:15 -0400852 if (sysctl_sched_rt_runtime < 0)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +0200853 return RUNTIME_INF;
854
855 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
856}
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700859# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700861#ifndef finish_arch_switch
862# define finish_arch_switch(prev) do { } while (0)
863#endif
864
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100865static inline int task_current(struct rq *rq, struct task_struct *p)
866{
867 return rq->curr == p;
868}
869
Nick Piggin4866cde2005-06-25 14:57:23 -0700870#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700871static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700872{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100873 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700874}
875
Ingo Molnar70b97a72006-07-03 00:25:42 -0700876static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700877{
878}
879
Ingo Molnar70b97a72006-07-03 00:25:42 -0700880static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700881{
Ingo Molnarda04c032005-09-13 11:17:59 +0200882#ifdef CONFIG_DEBUG_SPINLOCK
883 /* this is a valid case when another task releases the spinlock */
884 rq->lock.owner = current;
885#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700886 /*
887 * If we are tracking spinlock dependencies then we have to
888 * fix up the runqueue lock - which gets 'carried over' from
889 * prev into current:
890 */
891 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
892
Nick Piggin4866cde2005-06-25 14:57:23 -0700893 spin_unlock_irq(&rq->lock);
894}
895
896#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700897static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700898{
899#ifdef CONFIG_SMP
900 return p->oncpu;
901#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100902 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700903#endif
904}
905
Ingo Molnar70b97a72006-07-03 00:25:42 -0700906static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700907{
908#ifdef CONFIG_SMP
909 /*
910 * We can optimise this out completely for !SMP, because the
911 * SMP rebalancing from interrupt is the only thing that cares
912 * here.
913 */
914 next->oncpu = 1;
915#endif
916#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
917 spin_unlock_irq(&rq->lock);
918#else
919 spin_unlock(&rq->lock);
920#endif
921}
922
Ingo Molnar70b97a72006-07-03 00:25:42 -0700923static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700924{
925#ifdef CONFIG_SMP
926 /*
927 * After ->oncpu is cleared, the task can be moved to a different CPU.
928 * We must ensure this doesn't happen until the switch is completely
929 * finished.
930 */
931 smp_wmb();
932 prev->oncpu = 0;
933#endif
934#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
935 local_irq_enable();
936#endif
937}
938#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700941 * __task_rq_lock - lock the runqueue a given task resides on.
942 * Must be called interrupts disabled.
943 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700944static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700945 __acquires(rq->lock)
946{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200947 for (;;) {
948 struct rq *rq = task_rq(p);
949 spin_lock(&rq->lock);
950 if (likely(rq == task_rq(p)))
951 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700952 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700953 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700954}
955
956/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100958 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 * explicitly disabling preemption.
960 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700961static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 __acquires(rq->lock)
963{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700964 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Andi Kleen3a5c3592007-10-15 17:00:14 +0200966 for (;;) {
967 local_irq_save(*flags);
968 rq = task_rq(p);
969 spin_lock(&rq->lock);
970 if (likely(rq == task_rq(p)))
971 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
Oleg Nesterovad474ca2008-11-10 15:39:30 +0100976void task_rq_unlock_wait(struct task_struct *p)
977{
978 struct rq *rq = task_rq(p);
979
980 smp_mb(); /* spin-unlock-wait is not a full memory barrier */
981 spin_unlock_wait(&rq->lock);
982}
983
Alexey Dobriyana9957442007-10-15 17:00:13 +0200984static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700985 __releases(rq->lock)
986{
987 spin_unlock(&rq->lock);
988}
989
Ingo Molnar70b97a72006-07-03 00:25:42 -0700990static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 __releases(rq->lock)
992{
993 spin_unlock_irqrestore(&rq->lock, *flags);
994}
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800997 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200999static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 __acquires(rq->lock)
1001{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001002 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 local_irq_disable();
1005 rq = this_rq();
1006 spin_lock(&rq->lock);
1007
1008 return rq;
1009}
1010
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001011#ifdef CONFIG_SCHED_HRTICK
1012/*
1013 * Use HR-timers to deliver accurate preemption points.
1014 *
1015 * Its all a bit involved since we cannot program an hrt while holding the
1016 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
1017 * reschedule event.
1018 *
1019 * When we get rescheduled we reprogram the hrtick_timer outside of the
1020 * rq->lock.
1021 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001022
1023/*
1024 * Use hrtick when:
1025 * - enabled by features
1026 * - hrtimer is actually high res
1027 */
1028static inline int hrtick_enabled(struct rq *rq)
1029{
1030 if (!sched_feat(HRTICK))
1031 return 0;
Ingo Molnarba420592008-07-20 11:02:06 +02001032 if (!cpu_active(cpu_of(rq)))
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001033 return 0;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001034 return hrtimer_is_hres_active(&rq->hrtick_timer);
1035}
1036
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001037static void hrtick_clear(struct rq *rq)
1038{
1039 if (hrtimer_active(&rq->hrtick_timer))
1040 hrtimer_cancel(&rq->hrtick_timer);
1041}
1042
1043/*
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001044 * High-resolution timer tick.
1045 * Runs from hardirq context with interrupts disabled.
1046 */
1047static enum hrtimer_restart hrtick(struct hrtimer *timer)
1048{
1049 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1050
1051 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1052
1053 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02001054 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001055 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1056 spin_unlock(&rq->lock);
1057
1058 return HRTIMER_NORESTART;
1059}
1060
Rabin Vincent95e904c2008-05-11 05:55:33 +05301061#ifdef CONFIG_SMP
Peter Zijlstra31656512008-07-18 18:01:23 +02001062/*
1063 * called from hardirq (IPI) context
1064 */
1065static void __hrtick_start(void *arg)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001066{
Peter Zijlstra31656512008-07-18 18:01:23 +02001067 struct rq *rq = arg;
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001068
Peter Zijlstra31656512008-07-18 18:01:23 +02001069 spin_lock(&rq->lock);
1070 hrtimer_restart(&rq->hrtick_timer);
1071 rq->hrtick_csd_pending = 0;
1072 spin_unlock(&rq->lock);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001073}
1074
Peter Zijlstra31656512008-07-18 18:01:23 +02001075/*
1076 * Called to set the hrtick timer state.
1077 *
1078 * called with rq->lock held and irqs disabled
1079 */
1080static void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001081{
Peter Zijlstra31656512008-07-18 18:01:23 +02001082 struct hrtimer *timer = &rq->hrtick_timer;
1083 ktime_t time = ktime_add_ns(timer->base->get_time(), delay);
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001084
Arjan van de Vencc584b22008-09-01 15:02:30 -07001085 hrtimer_set_expires(timer, time);
Peter Zijlstra31656512008-07-18 18:01:23 +02001086
1087 if (rq == this_rq()) {
1088 hrtimer_restart(timer);
1089 } else if (!rq->hrtick_csd_pending) {
1090 __smp_call_function_single(cpu_of(rq), &rq->hrtick_csd);
1091 rq->hrtick_csd_pending = 1;
1092 }
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001093}
1094
1095static int
1096hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
1097{
1098 int cpu = (int)(long)hcpu;
1099
1100 switch (action) {
1101 case CPU_UP_CANCELED:
1102 case CPU_UP_CANCELED_FROZEN:
1103 case CPU_DOWN_PREPARE:
1104 case CPU_DOWN_PREPARE_FROZEN:
1105 case CPU_DEAD:
1106 case CPU_DEAD_FROZEN:
Peter Zijlstra31656512008-07-18 18:01:23 +02001107 hrtick_clear(cpu_rq(cpu));
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001108 return NOTIFY_OK;
1109 }
1110
1111 return NOTIFY_DONE;
1112}
1113
Rakib Mullickfa748202008-09-22 14:55:45 -07001114static __init void init_hrtick(void)
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001115{
1116 hotcpu_notifier(hotplug_hrtick, 0);
1117}
Peter Zijlstra31656512008-07-18 18:01:23 +02001118#else
1119/*
1120 * Called to set the hrtick timer state.
1121 *
1122 * called with rq->lock held and irqs disabled
1123 */
1124static void hrtick_start(struct rq *rq, u64 delay)
1125{
1126 hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay), HRTIMER_MODE_REL);
1127}
1128
Andrew Morton006c75f2008-09-22 14:55:46 -07001129static inline void init_hrtick(void)
Peter Zijlstra31656512008-07-18 18:01:23 +02001130{
1131}
Rabin Vincent95e904c2008-05-11 05:55:33 +05301132#endif /* CONFIG_SMP */
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001133
1134static void init_rq_hrtick(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001135{
Peter Zijlstra31656512008-07-18 18:01:23 +02001136#ifdef CONFIG_SMP
1137 rq->hrtick_csd_pending = 0;
1138
1139 rq->hrtick_csd.flags = 0;
1140 rq->hrtick_csd.func = __hrtick_start;
1141 rq->hrtick_csd.info = rq;
1142#endif
1143
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001144 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1145 rq->hrtick_timer.function = hrtick;
Thomas Gleixnerccc7dad2008-09-29 15:47:42 +02001146 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_PERCPU;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001147}
Andrew Morton006c75f2008-09-22 14:55:46 -07001148#else /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001149static inline void hrtick_clear(struct rq *rq)
1150{
1151}
1152
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001153static inline void init_rq_hrtick(struct rq *rq)
1154{
1155}
1156
Peter Zijlstrab328ca12008-04-29 10:02:46 +02001157static inline void init_hrtick(void)
1158{
1159}
Andrew Morton006c75f2008-09-22 14:55:46 -07001160#endif /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001161
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001162/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001163 * resched_task - mark a task 'to be rescheduled now'.
1164 *
1165 * On UP this means the setting of the need_resched flag, on SMP it
1166 * might also involve a cross-CPU call to trigger the scheduler on
1167 * the target CPU.
1168 */
1169#ifdef CONFIG_SMP
1170
1171#ifndef tsk_is_polling
1172#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1173#endif
1174
Peter Zijlstra31656512008-07-18 18:01:23 +02001175static void resched_task(struct task_struct *p)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001176{
1177 int cpu;
1178
1179 assert_spin_locked(&task_rq(p)->lock);
1180
Peter Zijlstra31656512008-07-18 18:01:23 +02001181 if (unlikely(test_tsk_thread_flag(p, TIF_NEED_RESCHED)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001182 return;
1183
Peter Zijlstra31656512008-07-18 18:01:23 +02001184 set_tsk_thread_flag(p, TIF_NEED_RESCHED);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001185
1186 cpu = task_cpu(p);
1187 if (cpu == smp_processor_id())
1188 return;
1189
1190 /* NEED_RESCHED must be visible before we test polling */
1191 smp_mb();
1192 if (!tsk_is_polling(p))
1193 smp_send_reschedule(cpu);
1194}
1195
1196static void resched_cpu(int cpu)
1197{
1198 struct rq *rq = cpu_rq(cpu);
1199 unsigned long flags;
1200
1201 if (!spin_trylock_irqsave(&rq->lock, flags))
1202 return;
1203 resched_task(cpu_curr(cpu));
1204 spin_unlock_irqrestore(&rq->lock, flags);
1205}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001206
1207#ifdef CONFIG_NO_HZ
1208/*
1209 * When add_timer_on() enqueues a timer into the timer wheel of an
1210 * idle CPU then this timer might expire before the next timer event
1211 * which is scheduled to wake up that CPU. In case of a completely
1212 * idle system the next event might even be infinite time into the
1213 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1214 * leaves the inner idle loop so the newly added timer is taken into
1215 * account when the CPU goes back to idle and evaluates the timer
1216 * wheel for the next timer event.
1217 */
1218void wake_up_idle_cpu(int cpu)
1219{
1220 struct rq *rq = cpu_rq(cpu);
1221
1222 if (cpu == smp_processor_id())
1223 return;
1224
1225 /*
1226 * This is safe, as this function is called with the timer
1227 * wheel base lock of (cpu) held. When the CPU is on the way
1228 * to idle and has not yet set rq->curr to idle then it will
1229 * be serialized on the timer wheel base lock and take the new
1230 * timer into account automatically.
1231 */
1232 if (rq->curr != rq->idle)
1233 return;
1234
1235 /*
1236 * We can set TIF_RESCHED on the idle task of the other CPU
1237 * lockless. The worst case is that the other CPU runs the
1238 * idle task through an additional NOOP schedule()
1239 */
1240 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1241
1242 /* NEED_RESCHED must be visible before we test polling */
1243 smp_mb();
1244 if (!tsk_is_polling(rq->idle))
1245 smp_send_reschedule(cpu);
1246}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001247#endif /* CONFIG_NO_HZ */
Thomas Gleixner06d83082008-03-22 09:20:24 +01001248
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001249#else /* !CONFIG_SMP */
Peter Zijlstra31656512008-07-18 18:01:23 +02001250static void resched_task(struct task_struct *p)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001251{
1252 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra31656512008-07-18 18:01:23 +02001253 set_tsk_need_resched(p);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001254}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02001255#endif /* CONFIG_SMP */
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001256
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001257#if BITS_PER_LONG == 32
1258# define WMULT_CONST (~0UL)
1259#else
1260# define WMULT_CONST (1UL << 32)
1261#endif
1262
1263#define WMULT_SHIFT 32
1264
Ingo Molnar194081e2007-08-09 11:16:51 +02001265/*
1266 * Shift right and round:
1267 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001268#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001269
Peter Zijlstraa7be37a2008-06-27 13:41:11 +02001270/*
1271 * delta *= weight / lw
1272 */
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001273static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001274calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1275 struct load_weight *lw)
1276{
1277 u64 tmp;
1278
Lai Jiangshan7a232e02008-06-12 16:43:07 +08001279 if (!lw->inv_weight) {
1280 if (BITS_PER_LONG > 32 && unlikely(lw->weight >= WMULT_CONST))
1281 lw->inv_weight = 1;
1282 else
1283 lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)
1284 / (lw->weight+1);
1285 }
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001286
1287 tmp = (u64)delta_exec * weight;
1288 /*
1289 * Check whether we'd overflow the 64-bit multiplication:
1290 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001291 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001292 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001293 WMULT_SHIFT/2);
1294 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001295 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001296
Ingo Molnarecf691d2007-08-02 17:41:40 +02001297 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001298}
1299
Ingo Molnar10919852007-10-15 17:00:04 +02001300static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001301{
1302 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001303 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001304}
1305
Ingo Molnar10919852007-10-15 17:00:04 +02001306static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001307{
1308 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001309 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001310}
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001313 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1314 * of tasks with abnormal "nice" values across CPUs the contribution that
1315 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001316 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001317 * scaled version of the new time slice allocation that they receive on time
1318 * slice expiry etc.
1319 */
1320
Ingo Molnardd41f592007-07-09 18:51:59 +02001321#define WEIGHT_IDLEPRIO 2
1322#define WMULT_IDLEPRIO (1 << 31)
1323
1324/*
1325 * Nice levels are multiplicative, with a gentle 10% change for every
1326 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1327 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1328 * that remained on nice 0.
1329 *
1330 * The "10% effect" is relative and cumulative: from _any_ nice level,
1331 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +02001332 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1333 * If a task goes up by ~10% and another task goes down by ~10% then
1334 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001335 */
1336static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001337 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1338 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1339 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1340 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1341 /* 0 */ 1024, 820, 655, 526, 423,
1342 /* 5 */ 335, 272, 215, 172, 137,
1343 /* 10 */ 110, 87, 70, 56, 45,
1344 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001345};
1346
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001347/*
1348 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1349 *
1350 * In cases where the weight does not change often, we can use the
1351 * precalculated inverse to speed up arithmetics by turning divisions
1352 * into multiplications:
1353 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001354static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001355 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1356 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1357 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1358 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1359 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1360 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1361 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1362 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001363};
Peter Williams2dd73a42006-06-27 02:54:34 -07001364
Ingo Molnardd41f592007-07-09 18:51:59 +02001365static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1366
1367/*
1368 * runqueue iterator, to support SMP load-balancing between different
1369 * scheduling classes, without having to expose their internal data
1370 * structures to the load-balancing proper:
1371 */
1372struct rq_iterator {
1373 void *arg;
1374 struct task_struct *(*start)(void *);
1375 struct task_struct *(*next)(void *);
1376};
1377
Peter Williamse1d14842007-10-24 18:23:51 +02001378#ifdef CONFIG_SMP
1379static unsigned long
1380balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1381 unsigned long max_load_move, struct sched_domain *sd,
1382 enum cpu_idle_type idle, int *all_pinned,
1383 int *this_best_prio, struct rq_iterator *iterator);
1384
1385static int
1386iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1387 struct sched_domain *sd, enum cpu_idle_type idle,
1388 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001389#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001390
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001391#ifdef CONFIG_CGROUP_CPUACCT
1392static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1393#else
1394static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1395#endif
1396
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001397static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1398{
1399 update_load_add(&rq->load, load);
1400}
1401
1402static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1403{
1404 update_load_sub(&rq->load, load);
1405}
1406
Ingo Molnar7940ca32008-08-19 13:40:47 +02001407#if (defined(CONFIG_SMP) && defined(CONFIG_FAIR_GROUP_SCHED)) || defined(CONFIG_RT_GROUP_SCHED)
Peter Zijlstraeb755802008-08-19 12:33:05 +02001408typedef int (*tg_visitor)(struct task_group *, void *);
1409
1410/*
1411 * Iterate the full tree, calling @down when first entering a node and @up when
1412 * leaving it for the final time.
1413 */
1414static int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
1415{
1416 struct task_group *parent, *child;
1417 int ret;
1418
1419 rcu_read_lock();
1420 parent = &root_task_group;
1421down:
1422 ret = (*down)(parent, data);
1423 if (ret)
1424 goto out_unlock;
1425 list_for_each_entry_rcu(child, &parent->children, siblings) {
1426 parent = child;
1427 goto down;
1428
1429up:
1430 continue;
1431 }
1432 ret = (*up)(parent, data);
1433 if (ret)
1434 goto out_unlock;
1435
1436 child = parent;
1437 parent = parent->parent;
1438 if (parent)
1439 goto up;
1440out_unlock:
1441 rcu_read_unlock();
1442
1443 return ret;
1444}
1445
1446static int tg_nop(struct task_group *tg, void *data)
1447{
1448 return 0;
1449}
1450#endif
1451
Gregory Haskinse7693a32008-01-25 21:08:09 +01001452#ifdef CONFIG_SMP
1453static unsigned long source_load(int cpu, int type);
1454static unsigned long target_load(int cpu, int type);
Gregory Haskinse7693a32008-01-25 21:08:09 +01001455static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001456
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001457static unsigned long cpu_avg_load_per_task(int cpu)
1458{
1459 struct rq *rq = cpu_rq(cpu);
Ingo Molnaraf6d5962008-11-29 20:45:15 +01001460 unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001461
Steven Rostedt4cd42622008-11-26 21:04:24 -05001462 if (nr_running)
1463 rq->avg_load_per_task = rq->load.weight / nr_running;
Balbir Singha2d47772008-11-12 16:19:00 +05301464 else
1465 rq->avg_load_per_task = 0;
Peter Zijlstraa8a51d52008-06-27 13:41:26 +02001466
1467 return rq->avg_load_per_task;
1468}
1469
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001470#ifdef CONFIG_FAIR_GROUP_SCHED
1471
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001472static void __set_se_shares(struct sched_entity *se, unsigned long shares);
1473
1474/*
1475 * Calculate and set the cpu's group shares.
1476 */
1477static void
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001478update_group_shares_cpu(struct task_group *tg, int cpu,
1479 unsigned long sd_shares, unsigned long sd_rq_weight)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001480{
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001481 unsigned long shares;
1482 unsigned long rq_weight;
1483
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001484 if (!tg->se[cpu])
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001485 return;
1486
Ken Chenec4e0e22008-11-18 22:41:57 -08001487 rq_weight = tg->cfs_rq[cpu]->rq_weight;
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001488
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001489 /*
1490 * \Sum shares * rq_weight
1491 * shares = -----------------------
1492 * \Sum rq_weight
1493 *
1494 */
Ken Chenec4e0e22008-11-18 22:41:57 -08001495 shares = (sd_shares * rq_weight) / sd_rq_weight;
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001496 shares = clamp_t(unsigned long, shares, MIN_SHARES, MAX_SHARES);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001497
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001498 if (abs(shares - tg->se[cpu]->load.weight) >
1499 sysctl_sched_shares_thresh) {
1500 struct rq *rq = cpu_rq(cpu);
1501 unsigned long flags;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001502
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001503 spin_lock_irqsave(&rq->lock, flags);
Ken Chenec4e0e22008-11-18 22:41:57 -08001504 tg->cfs_rq[cpu]->shares = shares;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001505
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001506 __set_se_shares(tg->se[cpu], shares);
1507 spin_unlock_irqrestore(&rq->lock, flags);
1508 }
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001509}
1510
1511/*
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001512 * Re-compute the task group their per cpu shares over the given domain.
1513 * This needs to be done in a bottom-up fashion because the rq weight of a
1514 * parent group depends on the shares of its child groups.
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001515 */
Peter Zijlstraeb755802008-08-19 12:33:05 +02001516static int tg_shares_up(struct task_group *tg, void *data)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001517{
Ken Chenec4e0e22008-11-18 22:41:57 -08001518 unsigned long weight, rq_weight = 0;
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001519 unsigned long shares = 0;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001520 struct sched_domain *sd = data;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001521 int i;
1522
Rusty Russell758b2cd2008-11-25 02:35:04 +10301523 for_each_cpu(i, sched_domain_span(sd)) {
Ken Chenec4e0e22008-11-18 22:41:57 -08001524 /*
1525 * If there are currently no tasks on the cpu pretend there
1526 * is one of average load so that when a new task gets to
1527 * run here it will not get delayed by group starvation.
1528 */
1529 weight = tg->cfs_rq[i]->load.weight;
1530 if (!weight)
1531 weight = NICE_0_LOAD;
1532
1533 tg->cfs_rq[i]->rq_weight = weight;
1534 rq_weight += weight;
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001535 shares += tg->cfs_rq[i]->shares;
1536 }
1537
1538 if ((!shares && rq_weight) || shares > tg->shares)
1539 shares = tg->shares;
1540
1541 if (!sd->parent || !(sd->parent->flags & SD_LOAD_BALANCE))
1542 shares = tg->shares;
1543
Rusty Russell758b2cd2008-11-25 02:35:04 +10301544 for_each_cpu(i, sched_domain_span(sd))
Peter Zijlstraffda12a2008-10-17 19:27:02 +02001545 update_group_shares_cpu(tg, i, shares, rq_weight);
Peter Zijlstraeb755802008-08-19 12:33:05 +02001546
1547 return 0;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001548}
1549
1550/*
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001551 * Compute the cpu's hierarchical load factor for each task group.
1552 * This needs to be done in a top-down fashion because the load of a child
1553 * group is a fraction of its parents load.
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001554 */
Peter Zijlstraeb755802008-08-19 12:33:05 +02001555static int tg_load_down(struct task_group *tg, void *data)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001556{
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001557 unsigned long load;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001558 long cpu = (long)data;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001559
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001560 if (!tg->parent) {
1561 load = cpu_rq(cpu)->load.weight;
1562 } else {
1563 load = tg->parent->cfs_rq[cpu]->h_load;
1564 load *= tg->cfs_rq[cpu]->shares;
1565 load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
1566 }
1567
1568 tg->cfs_rq[cpu]->h_load = load;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001569
Peter Zijlstraeb755802008-08-19 12:33:05 +02001570 return 0;
Peter Zijlstra4d8d5952008-06-27 13:41:19 +02001571}
1572
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001573static void update_shares(struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001574{
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02001575 u64 now = cpu_clock(raw_smp_processor_id());
1576 s64 elapsed = now - sd->last_update;
1577
1578 if (elapsed >= (s64)(u64)sysctl_sched_shares_ratelimit) {
1579 sd->last_update = now;
Peter Zijlstraeb755802008-08-19 12:33:05 +02001580 walk_tg_tree(tg_nop, tg_shares_up, sd);
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02001581 }
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001582}
1583
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02001584static void update_shares_locked(struct rq *rq, struct sched_domain *sd)
1585{
1586 spin_unlock(&rq->lock);
1587 update_shares(sd);
1588 spin_lock(&rq->lock);
1589}
1590
Peter Zijlstraeb755802008-08-19 12:33:05 +02001591static void update_h_load(long cpu)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001592{
Peter Zijlstraeb755802008-08-19 12:33:05 +02001593 walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001594}
1595
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001596#else
1597
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001598static inline void update_shares(struct sched_domain *sd)
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001599{
1600}
1601
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02001602static inline void update_shares_locked(struct rq *rq, struct sched_domain *sd)
1603{
1604}
1605
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001606#endif
1607
Alexey Dobriyan70574a92008-11-28 22:08:00 +03001608/*
1609 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1610 */
1611static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1612 __releases(this_rq->lock)
1613 __acquires(busiest->lock)
1614 __acquires(this_rq->lock)
1615{
1616 int ret = 0;
1617
1618 if (unlikely(!irqs_disabled())) {
1619 /* printk() doesn't work good under rq->lock */
1620 spin_unlock(&this_rq->lock);
1621 BUG_ON(1);
1622 }
1623 if (unlikely(!spin_trylock(&busiest->lock))) {
1624 if (busiest < this_rq) {
1625 spin_unlock(&this_rq->lock);
1626 spin_lock(&busiest->lock);
1627 spin_lock_nested(&this_rq->lock, SINGLE_DEPTH_NESTING);
1628 ret = 1;
1629 } else
1630 spin_lock_nested(&busiest->lock, SINGLE_DEPTH_NESTING);
1631 }
1632 return ret;
1633}
1634
1635static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1636 __releases(busiest->lock)
1637{
1638 spin_unlock(&busiest->lock);
1639 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1640}
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001641#endif
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001642
1643#ifdef CONFIG_FAIR_GROUP_SCHED
1644static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
1645{
Vegard Nossum30432092008-06-27 21:35:50 +02001646#ifdef CONFIG_SMP
Ingo Molnar34e83e82008-06-27 15:42:36 +02001647 cfs_rq->shares = shares;
1648#endif
Peter Zijlstra18d95a22008-04-19 19:45:00 +02001649}
1650#endif
1651
Ingo Molnardd41f592007-07-09 18:51:59 +02001652#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001653#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001654#include "sched_fair.c"
1655#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001656#ifdef CONFIG_SCHED_DEBUG
1657# include "sched_debug.c"
1658#endif
1659
1660#define sched_class_highest (&rt_sched_class)
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04001661#define for_each_class(class) \
1662 for (class = sched_class_highest; class; class = class->next)
Ingo Molnardd41f592007-07-09 18:51:59 +02001663
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001664static void inc_nr_running(struct rq *rq)
Ingo Molnar6363ca52008-05-29 11:28:57 +02001665{
1666 rq->nr_running++;
Ingo Molnar6363ca52008-05-29 11:28:57 +02001667}
1668
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001669static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001670{
1671 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001672}
1673
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001674static void set_load_weight(struct task_struct *p)
1675{
1676 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001677 p->se.load.weight = prio_to_weight[0] * 2;
1678 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1679 return;
1680 }
1681
1682 /*
1683 * SCHED_IDLE tasks get minimal weight:
1684 */
1685 if (p->policy == SCHED_IDLE) {
1686 p->se.load.weight = WEIGHT_IDLEPRIO;
1687 p->se.load.inv_weight = WMULT_IDLEPRIO;
1688 return;
1689 }
1690
1691 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1692 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001693}
1694
Gregory Haskins2087a1a2008-06-27 14:30:00 -06001695static void update_avg(u64 *avg, u64 sample)
1696{
1697 s64 diff = sample - *avg;
1698 *avg += diff >> 3;
1699}
1700
Ingo Molnar8159f872007-08-09 11:16:49 +02001701static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001702{
1703 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001704 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001705 p->se.on_rq = 1;
1706}
1707
Ingo Molnar69be72c2007-08-09 11:16:49 +02001708static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001709{
Gregory Haskins2087a1a2008-06-27 14:30:00 -06001710 if (sleep && p->se.last_wakeup) {
1711 update_avg(&p->se.avg_overlap,
1712 p->se.sum_exec_runtime - p->se.last_wakeup);
1713 p->se.last_wakeup = 0;
1714 }
1715
Ankita Garg46ac22b2008-07-01 14:30:06 +05301716 sched_info_dequeued(p);
Ingo Molnarf02231e2007-08-09 11:16:48 +02001717 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001718 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001719}
1720
1721/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001722 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001723 */
Ingo Molnar14531182007-07-09 18:51:59 +02001724static inline int __normal_prio(struct task_struct *p)
1725{
Ingo Molnardd41f592007-07-09 18:51:59 +02001726 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001727}
1728
1729/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001730 * Calculate the expected normal priority: i.e. priority
1731 * without taking RT-inheritance into account. Might be
1732 * boosted by interactivity modifiers. Changes upon fork,
1733 * setprio syscalls, and whenever the interactivity
1734 * estimator recalculates.
1735 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001736static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001737{
1738 int prio;
1739
Ingo Molnare05606d2007-07-09 18:51:59 +02001740 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001741 prio = MAX_RT_PRIO-1 - p->rt_priority;
1742 else
1743 prio = __normal_prio(p);
1744 return prio;
1745}
1746
1747/*
1748 * Calculate the current priority, i.e. the priority
1749 * taken into account by the scheduler. This value might
1750 * be boosted by RT tasks, or might be boosted by
1751 * interactivity modifiers. Will be RT if the task got
1752 * RT-boosted. If not then it returns p->normal_prio.
1753 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001754static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001755{
1756 p->normal_prio = normal_prio(p);
1757 /*
1758 * If we are RT tasks or we were boosted to RT priority,
1759 * keep the priority unchanged. Otherwise, update priority
1760 * to the normal priority:
1761 */
1762 if (!rt_prio(p->prio))
1763 return p->normal_prio;
1764 return p->prio;
1765}
1766
1767/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001768 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001770static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001772 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001773 rq->nr_uninterruptible--;
1774
Ingo Molnar8159f872007-08-09 11:16:49 +02001775 enqueue_task(rq, p, wakeup);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001776 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
1779/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 * deactivate_task - remove a task from the runqueue.
1781 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02001782static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001784 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001785 rq->nr_uninterruptible++;
1786
Ingo Molnar69be72c2007-08-09 11:16:49 +02001787 dequeue_task(rq, p, sleep);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001788 dec_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791/**
1792 * task_curr - is this task currently executing on a CPU?
1793 * @p: the task in question.
1794 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001795inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
1797 return cpu_curr(task_cpu(p)) == p;
1798}
1799
Ingo Molnardd41f592007-07-09 18:51:59 +02001800static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1801{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001802 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001803#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01001804 /*
1805 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1806 * successfuly executed on another CPU. We must ensure that updates of
1807 * per-task data have been completed by this moment.
1808 */
1809 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02001810 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02001811#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07001812}
1813
Steven Rostedtcb469842008-01-25 21:08:22 +01001814static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1815 const struct sched_class *prev_class,
1816 int oldprio, int running)
1817{
1818 if (prev_class != p->sched_class) {
1819 if (prev_class->switched_from)
1820 prev_class->switched_from(rq, p, running);
1821 p->sched_class->switched_to(rq, p, running);
1822 } else
1823 p->sched_class->prio_changed(rq, p, oldprio, running);
1824}
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02001827
Thomas Gleixnere958b362008-06-04 23:22:32 +02001828/* Used instead of source_load when we know the type == 0 */
1829static unsigned long weighted_cpuload(const int cpu)
1830{
1831 return cpu_rq(cpu)->load.weight;
1832}
1833
Ingo Molnarcc367732007-10-15 17:00:18 +02001834/*
1835 * Is this task likely cache-hot:
1836 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001837static int
Ingo Molnarcc367732007-10-15 17:00:18 +02001838task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
1839{
1840 s64 delta;
1841
Ingo Molnarf540a602008-03-15 17:10:34 +01001842 /*
1843 * Buddy candidates are cache hot:
1844 */
Peter Zijlstra47932412008-11-04 21:25:09 +01001845 if (sched_feat(CACHE_HOT_BUDDY) &&
1846 (&p->se == cfs_rq_of(&p->se)->next ||
1847 &p->se == cfs_rq_of(&p->se)->last))
Ingo Molnarf540a602008-03-15 17:10:34 +01001848 return 1;
1849
Ingo Molnarcc367732007-10-15 17:00:18 +02001850 if (p->sched_class != &fair_sched_class)
1851 return 0;
1852
Ingo Molnar6bc16652007-10-15 17:00:18 +02001853 if (sysctl_sched_migration_cost == -1)
1854 return 1;
1855 if (sysctl_sched_migration_cost == 0)
1856 return 0;
1857
Ingo Molnarcc367732007-10-15 17:00:18 +02001858 delta = now - p->se.exec_start;
1859
1860 return delta < (s64)sysctl_sched_migration_cost;
1861}
1862
1863
Ingo Molnardd41f592007-07-09 18:51:59 +02001864void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001865{
Ingo Molnardd41f592007-07-09 18:51:59 +02001866 int old_cpu = task_cpu(p);
1867 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001868 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
1869 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02001870 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001871
1872 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001873
1874#ifdef CONFIG_SCHEDSTATS
1875 if (p->se.wait_start)
1876 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001877 if (p->se.sleep_start)
1878 p->se.sleep_start -= clock_offset;
1879 if (p->se.block_start)
1880 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02001881 if (old_cpu != new_cpu) {
1882 schedstat_inc(p, se.nr_migrations);
1883 if (task_hot(p, old_rq->clock, NULL))
1884 schedstat_inc(p, se.nr_forced2_migrations);
1885 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001886#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001887 p->se.vruntime -= old_cfsrq->min_vruntime -
1888 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02001889
1890 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001891}
1892
Ingo Molnar70b97a72006-07-03 00:25:42 -07001893struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Ingo Molnar36c8b582006-07-03 00:25:41 -07001896 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 int dest_cpu;
1898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001900};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
1902/*
1903 * The task's runqueue lock must be held.
1904 * Returns true if you have to wait for migration thread.
1905 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001906static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07001907migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001909 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 /*
1912 * If the task is not on a runqueue (and not running), then
1913 * it is sufficient to simply update the task's cpu field.
1914 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001915 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 set_task_cpu(p, dest_cpu);
1917 return 0;
1918 }
1919
1920 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 req->task = p;
1922 req->dest_cpu = dest_cpu;
1923 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07001924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 return 1;
1926}
1927
1928/*
1929 * wait_task_inactive - wait for a thread to unschedule.
1930 *
Roland McGrath85ba2d82008-07-25 19:45:58 -07001931 * If @match_state is nonzero, it's the @p->state value just checked and
1932 * not expected to change. If it changes, i.e. @p might have woken up,
1933 * then return zero. When we succeed in waiting for @p to be off its CPU,
1934 * we return a positive number (its total switch count). If a second call
1935 * a short while later returns the same number, the caller can be sure that
1936 * @p has remained unscheduled the whole time.
1937 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 * The caller must ensure that the task *will* unschedule sometime soon,
1939 * else this function might spin for a *long* time. This function can't
1940 * be called with interrupts off, or it may introduce deadlock with
1941 * smp_call_function() if an IPI is sent by the same process we are
1942 * waiting to become inactive.
1943 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07001944unsigned long wait_task_inactive(struct task_struct *p, long match_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
1946 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001947 int running, on_rq;
Roland McGrath85ba2d82008-07-25 19:45:58 -07001948 unsigned long ncsw;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001949 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Andi Kleen3a5c3592007-10-15 17:00:14 +02001951 for (;;) {
1952 /*
1953 * We do the initial early heuristics without holding
1954 * any task-queue locks at all. We'll only try to get
1955 * the runqueue lock when things look like they will
1956 * work out!
1957 */
1958 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001959
Andi Kleen3a5c3592007-10-15 17:00:14 +02001960 /*
1961 * If the task is actively running on another CPU
1962 * still, just relax and busy-wait without holding
1963 * any locks.
1964 *
1965 * NOTE! Since we don't hold any locks, it's not
1966 * even sure that "rq" stays as the right runqueue!
1967 * But we don't care, since "task_running()" will
1968 * return false if the runqueue has changed and p
1969 * is actually now running somewhere else!
1970 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07001971 while (task_running(rq, p)) {
1972 if (match_state && unlikely(p->state != match_state))
1973 return 0;
Andi Kleen3a5c3592007-10-15 17:00:14 +02001974 cpu_relax();
Roland McGrath85ba2d82008-07-25 19:45:58 -07001975 }
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001976
Andi Kleen3a5c3592007-10-15 17:00:14 +02001977 /*
1978 * Ok, time to look more closely! We need the rq
1979 * lock now, to be *sure*. If we're wrong, we'll
1980 * just go back and repeat.
1981 */
1982 rq = task_rq_lock(p, &flags);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04001983 trace_sched_wait_task(rq, p);
Andi Kleen3a5c3592007-10-15 17:00:14 +02001984 running = task_running(rq, p);
1985 on_rq = p->se.on_rq;
Roland McGrath85ba2d82008-07-25 19:45:58 -07001986 ncsw = 0;
Oleg Nesterovf31e11d2008-08-20 16:54:44 -07001987 if (!match_state || p->state == match_state)
Oleg Nesterov93dcf552008-08-20 16:54:44 -07001988 ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
Andi Kleen3a5c3592007-10-15 17:00:14 +02001989 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001990
Andi Kleen3a5c3592007-10-15 17:00:14 +02001991 /*
Roland McGrath85ba2d82008-07-25 19:45:58 -07001992 * If it changed from the expected state, bail out now.
1993 */
1994 if (unlikely(!ncsw))
1995 break;
1996
1997 /*
Andi Kleen3a5c3592007-10-15 17:00:14 +02001998 * Was it really running after all now that we
1999 * checked with the proper locks actually held?
2000 *
2001 * Oops. Go back and try again..
2002 */
2003 if (unlikely(running)) {
2004 cpu_relax();
2005 continue;
2006 }
2007
2008 /*
2009 * It's not enough that it's not actively running,
2010 * it must be off the runqueue _entirely_, and not
2011 * preempted!
2012 *
2013 * So if it wa still runnable (but just not actively
2014 * running right now), it's preempted, and we should
2015 * yield - it could be a while.
2016 */
2017 if (unlikely(on_rq)) {
2018 schedule_timeout_uninterruptible(1);
2019 continue;
2020 }
2021
2022 /*
2023 * Ahh, all good. It wasn't running, and it wasn't
2024 * runnable, which means that it will never become
2025 * running in the future either. We're all done!
2026 */
2027 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 }
Roland McGrath85ba2d82008-07-25 19:45:58 -07002029
2030 return ncsw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
2033/***
2034 * kick_process - kick a running thread to enter/exit the kernel
2035 * @p: the to-be-kicked thread
2036 *
2037 * Cause a process which is running on another CPU to enter
2038 * kernel-mode, without any delay. (to get signals handled.)
2039 *
2040 * NOTE: this function doesnt have to take the runqueue lock,
2041 * because all it wants to ensure is that the remote task enters
2042 * the kernel. If the IPI races and the task has been migrated
2043 * to another CPU then no harm is done and the purpose has been
2044 * achieved as well.
2045 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002046void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
2048 int cpu;
2049
2050 preempt_disable();
2051 cpu = task_cpu(p);
2052 if ((cpu != smp_processor_id()) && task_curr(p))
2053 smp_send_reschedule(cpu);
2054 preempt_enable();
2055}
2056
2057/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002058 * Return a low guess at the load of a migration-source cpu weighted
2059 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 *
2061 * We want to under-estimate the load of migration sources, to
2062 * balance conservatively.
2063 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002064static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002065{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002066 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002067 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002068
Peter Zijlstra93b75212008-06-27 13:41:33 +02002069 if (type == 0 || !sched_feat(LB_BIAS))
Ingo Molnardd41f592007-07-09 18:51:59 +02002070 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002071
Ingo Molnardd41f592007-07-09 18:51:59 +02002072 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073}
2074
2075/*
Peter Williams2dd73a42006-06-27 02:54:34 -07002076 * Return a high guess at the load of a migration-target cpu weighted
2077 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002079static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08002080{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002081 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002082 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08002083
Peter Zijlstra93b75212008-06-27 13:41:33 +02002084 if (type == 0 || !sched_feat(LB_BIAS))
Ingo Molnardd41f592007-07-09 18:51:59 +02002085 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07002086
Ingo Molnardd41f592007-07-09 18:51:59 +02002087 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07002088}
2089
2090/*
Nick Piggin147cbb42005-06-25 14:57:19 -07002091 * find_idlest_group finds and returns the least busy CPU group within the
2092 * domain.
2093 */
2094static struct sched_group *
2095find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
2096{
2097 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
2098 unsigned long min_load = ULONG_MAX, this_load = 0;
2099 int load_idx = sd->forkexec_idx;
2100 int imbalance = 100 + (sd->imbalance_pct-100)/2;
2101
2102 do {
2103 unsigned long load, avg_load;
2104 int local_group;
2105 int i;
2106
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002107 /* Skip over this group if it has no CPUs allowed */
Rusty Russell758b2cd2008-11-25 02:35:04 +10302108 if (!cpumask_intersects(sched_group_cpus(group),
2109 &p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02002110 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002111
Rusty Russell758b2cd2008-11-25 02:35:04 +10302112 local_group = cpumask_test_cpu(this_cpu,
2113 sched_group_cpus(group));
Nick Piggin147cbb42005-06-25 14:57:19 -07002114
2115 /* Tally up the load of all CPUs in the group */
2116 avg_load = 0;
2117
Rusty Russell758b2cd2008-11-25 02:35:04 +10302118 for_each_cpu(i, sched_group_cpus(group)) {
Nick Piggin147cbb42005-06-25 14:57:19 -07002119 /* Bias balancing toward cpus of our domain */
2120 if (local_group)
2121 load = source_load(i, load_idx);
2122 else
2123 load = target_load(i, load_idx);
2124
2125 avg_load += load;
2126 }
2127
2128 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002129 avg_load = sg_div_cpu_power(group,
2130 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07002131
2132 if (local_group) {
2133 this_load = avg_load;
2134 this = group;
2135 } else if (avg_load < min_load) {
2136 min_load = avg_load;
2137 idlest = group;
2138 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02002139 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07002140
2141 if (!idlest || 100*this_load < imbalance*min_load)
2142 return NULL;
2143 return idlest;
2144}
2145
2146/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07002147 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07002148 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002149static int
Rusty Russell758b2cd2008-11-25 02:35:04 +10302150find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
Nick Piggin147cbb42005-06-25 14:57:19 -07002151{
2152 unsigned long load, min_load = ULONG_MAX;
2153 int idlest = -1;
2154 int i;
2155
M.Baris Demirayda5a5522005-09-10 00:26:09 -07002156 /* Traverse only the allowed CPUs */
Rusty Russell758b2cd2008-11-25 02:35:04 +10302157 for_each_cpu_and(i, sched_group_cpus(group), &p->cpus_allowed) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002158 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07002159
2160 if (load < min_load || (load == min_load && i == this_cpu)) {
2161 min_load = load;
2162 idlest = i;
2163 }
2164 }
2165
2166 return idlest;
2167}
2168
Nick Piggin476d1392005-06-25 14:57:29 -07002169/*
2170 * sched_balance_self: balance the current task (running on cpu) in domains
2171 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
2172 * SD_BALANCE_EXEC.
2173 *
2174 * Balance, ie. select the least loaded group.
2175 *
2176 * Returns the target CPU number, or the same CPU if no balancing is needed.
2177 *
2178 * preempt must be disabled.
2179 */
2180static int sched_balance_self(int cpu, int flag)
2181{
2182 struct task_struct *t = current;
2183 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07002184
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002185 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02002186 /*
2187 * If power savings logic is enabled for a domain, stop there.
2188 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002189 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
2190 break;
Nick Piggin476d1392005-06-25 14:57:29 -07002191 if (tmp->flags & flag)
2192 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002193 }
Nick Piggin476d1392005-06-25 14:57:29 -07002194
Peter Zijlstra039a1c412008-06-27 13:41:25 +02002195 if (sd)
2196 update_shares(sd);
2197
Nick Piggin476d1392005-06-25 14:57:29 -07002198 while (sd) {
Nick Piggin476d1392005-06-25 14:57:29 -07002199 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002200 int new_cpu, weight;
2201
2202 if (!(sd->flags & flag)) {
2203 sd = sd->child;
2204 continue;
2205 }
Nick Piggin476d1392005-06-25 14:57:29 -07002206
Nick Piggin476d1392005-06-25 14:57:29 -07002207 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002208 if (!group) {
2209 sd = sd->child;
2210 continue;
2211 }
Nick Piggin476d1392005-06-25 14:57:29 -07002212
Rusty Russell758b2cd2008-11-25 02:35:04 +10302213 new_cpu = find_idlest_cpu(group, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002214 if (new_cpu == -1 || new_cpu == cpu) {
2215 /* Now try balancing at a lower domain level of cpu */
2216 sd = sd->child;
2217 continue;
2218 }
Nick Piggin476d1392005-06-25 14:57:29 -07002219
Siddha, Suresh B1a848872006-10-03 01:14:08 -07002220 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07002221 cpu = new_cpu;
Rusty Russell758b2cd2008-11-25 02:35:04 +10302222 weight = cpumask_weight(sched_domain_span(sd));
Nick Piggin476d1392005-06-25 14:57:29 -07002223 sd = NULL;
Nick Piggin476d1392005-06-25 14:57:29 -07002224 for_each_domain(cpu, tmp) {
Rusty Russell758b2cd2008-11-25 02:35:04 +10302225 if (weight <= cpumask_weight(sched_domain_span(tmp)))
Nick Piggin476d1392005-06-25 14:57:29 -07002226 break;
2227 if (tmp->flags & flag)
2228 sd = tmp;
2229 }
2230 /* while loop will break here if sd == NULL */
2231 }
2232
2233 return cpu;
2234}
2235
2236#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238/***
2239 * try_to_wake_up - wake up a thread
2240 * @p: the to-be-woken-up thread
2241 * @state: the mask of task states that can be woken
2242 * @sync: do a synchronous wakeup?
2243 *
2244 * Put it on the run-queue if it's not already there. The "current"
2245 * thread is always on the run-queue (except when the actual
2246 * re-schedule is in progress), and as such you're allowed to do
2247 * the simpler "current->state = TASK_RUNNING" to mark yourself
2248 * runnable without the overhead of this.
2249 *
2250 * returns failure only if the task is already active.
2251 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002252static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
Ingo Molnarcc367732007-10-15 17:00:18 +02002254 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 unsigned long flags;
2256 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002257 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Ingo Molnarb85d0662008-03-16 20:03:22 +01002259 if (!sched_feat(SYNC_WAKEUPS))
2260 sync = 0;
2261
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02002262#ifdef CONFIG_SMP
2263 if (sched_feat(LB_WAKEUP_UPDATE)) {
2264 struct sched_domain *sd;
2265
2266 this_cpu = raw_smp_processor_id();
2267 cpu = task_cpu(p);
2268
2269 for_each_domain(this_cpu, sd) {
Rusty Russell758b2cd2008-11-25 02:35:04 +10302270 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02002271 update_shares(sd);
2272 break;
2273 }
2274 }
2275 }
2276#endif
2277
Linus Torvalds04e2f172008-02-23 18:05:03 -08002278 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 rq = task_rq_lock(p, &flags);
2280 old_state = p->state;
2281 if (!(old_state & state))
2282 goto out;
2283
Ingo Molnardd41f592007-07-09 18:51:59 +02002284 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 goto out_running;
2286
2287 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02002288 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 this_cpu = smp_processor_id();
2290
2291#ifdef CONFIG_SMP
2292 if (unlikely(task_running(rq, p)))
2293 goto out_activate;
2294
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01002295 cpu = p->sched_class->select_task_rq(p, sync);
2296 if (cpu != orig_cpu) {
2297 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 task_rq_unlock(rq, &flags);
2299 /* might preempt at this point */
2300 rq = task_rq_lock(p, &flags);
2301 old_state = p->state;
2302 if (!(old_state & state))
2303 goto out;
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 this_cpu = smp_processor_id();
2308 cpu = task_cpu(p);
2309 }
2310
Gregory Haskinse7693a32008-01-25 21:08:09 +01002311#ifdef CONFIG_SCHEDSTATS
2312 schedstat_inc(rq, ttwu_count);
2313 if (cpu == this_cpu)
2314 schedstat_inc(rq, ttwu_local);
2315 else {
2316 struct sched_domain *sd;
2317 for_each_domain(this_cpu, sd) {
Rusty Russell758b2cd2008-11-25 02:35:04 +10302318 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Gregory Haskinse7693a32008-01-25 21:08:09 +01002319 schedstat_inc(sd, ttwu_wake_remote);
2320 break;
2321 }
2322 }
2323 }
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002324#endif /* CONFIG_SCHEDSTATS */
Gregory Haskinse7693a32008-01-25 21:08:09 +01002325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326out_activate:
2327#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02002328 schedstat_inc(p, se.nr_wakeups);
2329 if (sync)
2330 schedstat_inc(p, se.nr_wakeups_sync);
2331 if (orig_cpu != cpu)
2332 schedstat_inc(p, se.nr_wakeups_migrate);
2333 if (cpu == this_cpu)
2334 schedstat_inc(p, se.nr_wakeups_local);
2335 else
2336 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02002337 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02002338 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 success = 1;
2340
2341out_running:
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04002342 trace_sched_wakeup(rq, p);
Peter Zijlstra15afe092008-09-20 23:38:02 +02002343 check_preempt_curr(rq, p, sync);
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01002346#ifdef CONFIG_SMP
2347 if (p->sched_class->task_wake_up)
2348 p->sched_class->task_wake_up(rq, p);
2349#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350out:
Gregory Haskins2087a1a2008-06-27 14:30:00 -06002351 current->se.last_wakeup = current->se.sum_exec_runtime;
2352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 task_rq_unlock(rq, &flags);
2354
2355 return success;
2356}
2357
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002358int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002360 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362EXPORT_SYMBOL(wake_up_process);
2363
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002364int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
2366 return try_to_wake_up(p, state, 0);
2367}
2368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369/*
2370 * Perform scheduler related setup for a newly forked process p.
2371 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002372 *
2373 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002375static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
Ingo Molnardd41f592007-07-09 18:51:59 +02002377 p->se.exec_start = 0;
2378 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002379 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002380 p->se.last_wakeup = 0;
2381 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002382
2383#ifdef CONFIG_SCHEDSTATS
2384 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002385 p->se.sum_sleep_runtime = 0;
2386 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002387 p->se.block_start = 0;
2388 p->se.sleep_max = 0;
2389 p->se.block_max = 0;
2390 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002391 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002392 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002393#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002394
Peter Zijlstrafa717062008-01-25 21:08:27 +01002395 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002396 p->se.on_rq = 0;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02002397 INIT_LIST_HEAD(&p->se.group_node);
Nick Piggin476d1392005-06-25 14:57:29 -07002398
Avi Kivitye107be32007-07-26 13:40:43 +02002399#ifdef CONFIG_PREEMPT_NOTIFIERS
2400 INIT_HLIST_HEAD(&p->preempt_notifiers);
2401#endif
2402
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 /*
2404 * We mark the process as running here, but have not actually
2405 * inserted it onto the runqueue yet. This guarantees that
2406 * nobody will actually run it, and a signal or other external
2407 * event cannot wake it up and insert it on the runqueue either.
2408 */
2409 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002410}
2411
2412/*
2413 * fork()/clone()-time setup:
2414 */
2415void sched_fork(struct task_struct *p, int clone_flags)
2416{
2417 int cpu = get_cpu();
2418
2419 __sched_fork(p);
2420
2421#ifdef CONFIG_SMP
2422 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2423#endif
Ingo Molnar02e4bac22007-10-15 17:00:11 +02002424 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002425
2426 /*
2427 * Make sure we do not leak PI boosting priority to the child:
2428 */
2429 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002430 if (!rt_prio(p->prio))
2431 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002432
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002433#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002434 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002435 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002437#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002438 p->oncpu = 0;
2439#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002441 /* Want to start with kernel preemption disabled. */
Al Viroa1261f542005-11-13 16:06:55 -08002442 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002444 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445}
2446
2447/*
2448 * wake_up_new_task - wake up a newly created task for the first time.
2449 *
2450 * This function will do some initial scheduler statistics housekeeping
2451 * that must be done for every newly created context, then puts the task
2452 * on the runqueue and wakes it.
2453 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002454void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
2456 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002457 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
2459 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002461 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
2463 p->prio = effective_prio(p);
2464
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002465 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002466 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002469 * Let the scheduling class do new task startup
2470 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002472 p->sched_class->task_new(rq, p);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02002473 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 }
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04002475 trace_sched_wakeup_new(rq, p);
Peter Zijlstra15afe092008-09-20 23:38:02 +02002476 check_preempt_curr(rq, p, 0);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002477#ifdef CONFIG_SMP
2478 if (p->sched_class->task_wake_up)
2479 p->sched_class->task_wake_up(rq, p);
2480#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002481 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
2483
Avi Kivitye107be32007-07-26 13:40:43 +02002484#ifdef CONFIG_PREEMPT_NOTIFIERS
2485
2486/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002487 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2488 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002489 */
2490void preempt_notifier_register(struct preempt_notifier *notifier)
2491{
2492 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2493}
2494EXPORT_SYMBOL_GPL(preempt_notifier_register);
2495
2496/**
2497 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002498 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002499 *
2500 * This is safe to call from within a preemption notifier.
2501 */
2502void preempt_notifier_unregister(struct preempt_notifier *notifier)
2503{
2504 hlist_del(&notifier->link);
2505}
2506EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2507
2508static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2509{
2510 struct preempt_notifier *notifier;
2511 struct hlist_node *node;
2512
2513 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2514 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2515}
2516
2517static void
2518fire_sched_out_preempt_notifiers(struct task_struct *curr,
2519 struct task_struct *next)
2520{
2521 struct preempt_notifier *notifier;
2522 struct hlist_node *node;
2523
2524 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2525 notifier->ops->sched_out(notifier, next);
2526}
2527
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002528#else /* !CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002529
2530static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2531{
2532}
2533
2534static void
2535fire_sched_out_preempt_notifiers(struct task_struct *curr,
2536 struct task_struct *next)
2537{
2538}
2539
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02002540#endif /* CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02002541
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002543 * prepare_task_switch - prepare to switch tasks
2544 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002545 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002546 * @next: the task we are going to switch to.
2547 *
2548 * This is called with the rq lock held and interrupts off. It must
2549 * be paired with a subsequent finish_task_switch after the context
2550 * switch.
2551 *
2552 * prepare_task_switch sets up locking and calls architecture specific
2553 * hooks.
2554 */
Avi Kivitye107be32007-07-26 13:40:43 +02002555static inline void
2556prepare_task_switch(struct rq *rq, struct task_struct *prev,
2557 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002558{
Avi Kivitye107be32007-07-26 13:40:43 +02002559 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002560 prepare_lock_switch(rq, next);
2561 prepare_arch_switch(next);
2562}
2563
2564/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002566 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 * @prev: the thread we just switched away from.
2568 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002569 * finish_task_switch must be called after the context switch, paired
2570 * with a prepare_task_switch call before the context switch.
2571 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2572 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 *
2574 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002575 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 * with the lock held can cause deadlocks; see schedule() for
2577 * details.)
2578 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002579static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 __releases(rq->lock)
2581{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002583 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
2585 rq->prev_mm = NULL;
2586
2587 /*
2588 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002589 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002590 * schedule one last time. The schedule call will never return, and
2591 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002592 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 * still held, otherwise prev could be scheduled on another cpu, die
2594 * there before we look at prev->state, and then the reference would
2595 * be dropped twice.
2596 * Manfred Spraul <manfred@colorfullife.com>
2597 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002598 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002599 finish_arch_switch(prev);
2600 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002601#ifdef CONFIG_SMP
2602 if (current->sched_class->post_schedule)
2603 current->sched_class->post_schedule(rq);
2604#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002605
Avi Kivitye107be32007-07-26 13:40:43 +02002606 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 if (mm)
2608 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002609 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002610 /*
2611 * Remove function-return probe instances associated with this
2612 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002613 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002614 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618
2619/**
2620 * schedule_tail - first thing a freshly forked thread must call.
2621 * @prev: the thread we just switched away from.
2622 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002623asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 __releases(rq->lock)
2625{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002626 struct rq *rq = this_rq();
2627
Nick Piggin4866cde2005-06-25 14:57:23 -07002628 finish_task_switch(rq, prev);
2629#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2630 /* In this case, finish_task_switch does not reenable preemption */
2631 preempt_enable();
2632#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002634 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635}
2636
2637/*
2638 * context_switch - switch to the new MM and the new
2639 * thread's register state.
2640 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002641static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002642context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002643 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
Ingo Molnardd41f592007-07-09 18:51:59 +02002645 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Avi Kivitye107be32007-07-26 13:40:43 +02002647 prepare_task_switch(rq, prev, next);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04002648 trace_sched_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002649 mm = next->mm;
2650 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002651 /*
2652 * For paravirt, this is coupled with an exit in switch_to to
2653 * combine the page table reload and the switch backend into
2654 * one hypercall.
2655 */
2656 arch_enter_lazy_cpu_mode();
2657
Ingo Molnardd41f592007-07-09 18:51:59 +02002658 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 next->active_mm = oldmm;
2660 atomic_inc(&oldmm->mm_count);
2661 enter_lazy_tlb(oldmm, next);
2662 } else
2663 switch_mm(oldmm, mm, next);
2664
Ingo Molnardd41f592007-07-09 18:51:59 +02002665 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 rq->prev_mm = oldmm;
2668 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002669 /*
2670 * Since the runqueue lock will be released by the next
2671 * task (which is an invalid locking op but in the case
2672 * of the scheduler it's an obvious special-case), so we
2673 * do an early lockdep release here:
2674 */
2675#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002676 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002677#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
2679 /* Here we just switch the register state and the stack. */
2680 switch_to(prev, next, prev);
2681
Ingo Molnardd41f592007-07-09 18:51:59 +02002682 barrier();
2683 /*
2684 * this_rq must be evaluated again because prev may have moved
2685 * CPUs since it called schedule(), thus the 'rq' on its stack
2686 * frame will be invalid.
2687 */
2688 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689}
2690
2691/*
2692 * nr_running, nr_uninterruptible and nr_context_switches:
2693 *
2694 * externally visible scheduler statistics: current number of runnable
2695 * threads, current number of uninterruptible-sleeping threads, total
2696 * number of context switches performed since bootup.
2697 */
2698unsigned long nr_running(void)
2699{
2700 unsigned long i, sum = 0;
2701
2702 for_each_online_cpu(i)
2703 sum += cpu_rq(i)->nr_running;
2704
2705 return sum;
2706}
2707
2708unsigned long nr_uninterruptible(void)
2709{
2710 unsigned long i, sum = 0;
2711
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002712 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 sum += cpu_rq(i)->nr_uninterruptible;
2714
2715 /*
2716 * Since we read the counters lockless, it might be slightly
2717 * inaccurate. Do not allow it to go below zero though:
2718 */
2719 if (unlikely((long)sum < 0))
2720 sum = 0;
2721
2722 return sum;
2723}
2724
2725unsigned long long nr_context_switches(void)
2726{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002727 int i;
2728 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002730 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 sum += cpu_rq(i)->nr_switches;
2732
2733 return sum;
2734}
2735
2736unsigned long nr_iowait(void)
2737{
2738 unsigned long i, sum = 0;
2739
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002740 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2742
2743 return sum;
2744}
2745
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002746unsigned long nr_active(void)
2747{
2748 unsigned long i, running = 0, uninterruptible = 0;
2749
2750 for_each_online_cpu(i) {
2751 running += cpu_rq(i)->nr_running;
2752 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2753 }
2754
2755 if (unlikely((long)uninterruptible < 0))
2756 uninterruptible = 0;
2757
2758 return running + uninterruptible;
2759}
2760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002762 * Update rq->cpu_load[] statistics. This function is usually called every
2763 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002764 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002765static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002766{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002767 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002768 int i, scale;
2769
2770 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002771
2772 /* Update our load: */
2773 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2774 unsigned long old_load, new_load;
2775
2776 /* scale is effectively 1 << i now, and >> i divides by scale */
2777
2778 old_load = this_rq->cpu_load[i];
2779 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002780 /*
2781 * Round up the averaging division if load is increasing. This
2782 * prevents us from getting stuck on 9 if the load is 10, for
2783 * example.
2784 */
2785 if (new_load > old_load)
2786 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002787 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2788 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002789}
2790
Ingo Molnardd41f592007-07-09 18:51:59 +02002791#ifdef CONFIG_SMP
2792
Ingo Molnar48f24c42006-07-03 00:25:40 -07002793/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 * double_rq_lock - safely lock two runqueues
2795 *
2796 * Note this does not disable interrupts like task_rq_lock,
2797 * you need to do so manually before calling.
2798 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002799static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 __acquires(rq1->lock)
2801 __acquires(rq2->lock)
2802{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002803 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 if (rq1 == rq2) {
2805 spin_lock(&rq1->lock);
2806 __acquire(rq2->lock); /* Fake it out ;) */
2807 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002808 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 spin_lock(&rq1->lock);
Peter Zijlstra5e710e32008-07-30 13:26:57 +02002810 spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 } else {
2812 spin_lock(&rq2->lock);
Peter Zijlstra5e710e32008-07-30 13:26:57 +02002813 spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 }
2815 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002816 update_rq_clock(rq1);
2817 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818}
2819
2820/*
2821 * double_rq_unlock - safely unlock two runqueues
2822 *
2823 * Note this does not restore interrupts like task_rq_unlock,
2824 * you need to do so manually after calling.
2825 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002826static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 __releases(rq1->lock)
2828 __releases(rq2->lock)
2829{
2830 spin_unlock(&rq1->lock);
2831 if (rq1 != rq2)
2832 spin_unlock(&rq2->lock);
2833 else
2834 __release(rq2->lock);
2835}
2836
2837/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 * If dest_cpu is allowed for this process, migrate the task to it.
2839 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002840 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 * the cpu_allowed mask is restored.
2842 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002843static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002845 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002847 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849 rq = task_rq_lock(p, &flags);
Rusty Russell96f874e22008-11-25 02:35:14 +10302850 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed)
Max Krasnyanskye761b772008-07-15 04:43:49 -07002851 || unlikely(!cpu_active(dest_cpu)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 goto out;
2853
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04002854 trace_sched_migrate_task(rq, p, dest_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 /* force the process onto the specified CPU */
2856 if (migrate_task(p, dest_cpu, &req)) {
2857 /* Need to wait for migration thread (might exit: take ref). */
2858 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07002859
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 get_task_struct(mt);
2861 task_rq_unlock(rq, &flags);
2862 wake_up_process(mt);
2863 put_task_struct(mt);
2864 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07002865
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 return;
2867 }
2868out:
2869 task_rq_unlock(rq, &flags);
2870}
2871
2872/*
Nick Piggin476d1392005-06-25 14:57:29 -07002873 * sched_exec - execve() is a valuable balancing opportunity, because at
2874 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 */
2876void sched_exec(void)
2877{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002879 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002881 if (new_cpu != this_cpu)
2882 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883}
2884
2885/*
2886 * pull_task - move a task from a remote runqueue to the local runqueue.
2887 * Both runqueues must be locked.
2888 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002889static void pull_task(struct rq *src_rq, struct task_struct *p,
2890 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02002892 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002894 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 /*
2896 * Note that idle threads have a prio of MAX_PRIO, for this test
2897 * to be always true for them.
2898 */
Peter Zijlstra15afe092008-09-20 23:38:02 +02002899 check_preempt_curr(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900}
2901
2902/*
2903 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
2904 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002905static
Ingo Molnar70b97a72006-07-03 00:25:42 -07002906int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002907 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002908 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909{
2910 /*
2911 * We do not migrate tasks that are:
2912 * 1) running (obviously), or
2913 * 2) cannot be migrated to this CPU due to cpus_allowed, or
2914 * 3) are cache-hot on their current CPU.
2915 */
Rusty Russell96f874e22008-11-25 02:35:14 +10302916 if (!cpumask_test_cpu(this_cpu, &p->cpus_allowed)) {
Ingo Molnarcc367732007-10-15 17:00:18 +02002917 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002919 }
Nick Piggin81026792005-06-25 14:57:07 -07002920 *all_pinned = 0;
2921
Ingo Molnarcc367732007-10-15 17:00:18 +02002922 if (task_running(rq, p)) {
2923 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07002924 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Ingo Molnarda84d962007-10-15 17:00:18 +02002927 /*
2928 * Aggressive migration if:
2929 * 1) task is cache cold, or
2930 * 2) too many balance attempts have failed.
2931 */
2932
Ingo Molnar6bc16652007-10-15 17:00:18 +02002933 if (!task_hot(p, rq->clock, sd) ||
2934 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002935#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02002936 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002937 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02002938 schedstat_inc(p, se.nr_forced_migrations);
2939 }
Ingo Molnarda84d962007-10-15 17:00:18 +02002940#endif
2941 return 1;
2942 }
2943
Ingo Molnarcc367732007-10-15 17:00:18 +02002944 if (task_hot(p, rq->clock, sd)) {
2945 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02002946 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 return 1;
2949}
2950
Peter Williamse1d14842007-10-24 18:23:51 +02002951static unsigned long
2952balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
2953 unsigned long max_load_move, struct sched_domain *sd,
2954 enum cpu_idle_type idle, int *all_pinned,
2955 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02002956{
Peter Zijlstra051c6762008-06-27 13:41:31 +02002957 int loops = 0, pulled = 0, pinned = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002958 struct task_struct *p;
2959 long rem_load_move = max_load_move;
2960
Peter Williamse1d14842007-10-24 18:23:51 +02002961 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002962 goto out;
2963
2964 pinned = 1;
2965
2966 /*
2967 * Start the load-balancing iterator:
2968 */
2969 p = iterator->start(iterator->arg);
2970next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002971 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02002972 goto out;
Peter Zijlstra051c6762008-06-27 13:41:31 +02002973
2974 if ((p->se.load.weight >> 1) > rem_load_move ||
Ingo Molnardd41f592007-07-09 18:51:59 +02002975 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002976 p = iterator->next(iterator->arg);
2977 goto next;
2978 }
2979
2980 pull_task(busiest, p, this_rq, this_cpu);
2981 pulled++;
2982 rem_load_move -= p->se.load.weight;
2983
2984 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002985 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002986 */
Peter Williamse1d14842007-10-24 18:23:51 +02002987 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002988 if (p->prio < *this_best_prio)
2989 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02002990 p = iterator->next(iterator->arg);
2991 goto next;
2992 }
2993out:
2994 /*
Peter Williamse1d14842007-10-24 18:23:51 +02002995 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02002996 * so we can safely collect pull_task() stats here rather than
2997 * inside pull_task().
2998 */
2999 schedstat_add(sd, lb_gained[idle], pulled);
3000
3001 if (all_pinned)
3002 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02003003
3004 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02003005}
Ingo Molnar48f24c42006-07-03 00:25:40 -07003006
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007/*
Peter Williams43010652007-08-09 11:16:46 +02003008 * move_tasks tries to move up to max_load_move weighted load from busiest to
3009 * this_rq, as part of a balancing operation within domain "sd".
3010 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 *
3012 * Called with both runqueues locked.
3013 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003014static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02003015 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003016 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07003017 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003019 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02003020 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003021 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
Ingo Molnardd41f592007-07-09 18:51:59 +02003023 do {
Peter Williams43010652007-08-09 11:16:46 +02003024 total_load_moved +=
3025 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02003026 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003027 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02003028 class = class->next;
Gregory Haskinsc4acb2c2008-06-27 14:29:55 -06003029
3030 if (idle == CPU_NEWLY_IDLE && this_rq->nr_running)
3031 break;
3032
Peter Williams43010652007-08-09 11:16:46 +02003033 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
Peter Williams43010652007-08-09 11:16:46 +02003035 return total_load_moved > 0;
3036}
3037
Peter Williamse1d14842007-10-24 18:23:51 +02003038static int
3039iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3040 struct sched_domain *sd, enum cpu_idle_type idle,
3041 struct rq_iterator *iterator)
3042{
3043 struct task_struct *p = iterator->start(iterator->arg);
3044 int pinned = 0;
3045
3046 while (p) {
3047 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
3048 pull_task(busiest, p, this_rq, this_cpu);
3049 /*
3050 * Right now, this is only the second place pull_task()
3051 * is called, so we can safely collect pull_task()
3052 * stats here rather than inside pull_task().
3053 */
3054 schedstat_inc(sd, lb_gained[idle]);
3055
3056 return 1;
3057 }
3058 p = iterator->next(iterator->arg);
3059 }
3060
3061 return 0;
3062}
3063
Peter Williams43010652007-08-09 11:16:46 +02003064/*
3065 * move_one_task tries to move exactly one task from busiest to this_rq, as
3066 * part of active balancing operations within "domain".
3067 * Returns 1 if successful and 0 otherwise.
3068 *
3069 * Called with both runqueues locked.
3070 */
3071static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
3072 struct sched_domain *sd, enum cpu_idle_type idle)
3073{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003074 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02003075
3076 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02003077 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02003078 return 1;
3079
3080 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081}
3082
3083/*
3084 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07003085 * domain. It calculates and returns the amount of weighted load which
3086 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 */
3088static struct sched_group *
3089find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02003090 unsigned long *imbalance, enum cpu_idle_type idle,
Rusty Russell96f874e22008-11-25 02:35:14 +10303091 int *sd_idle, const struct cpumask *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092{
3093 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
3094 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003095 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07003096 unsigned long busiest_load_per_task, busiest_nr_running;
3097 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003098 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003099#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3100 int power_savings_balance = 1;
3101 unsigned long leader_nr_running = 0, min_load_per_task = 0;
3102 unsigned long min_nr_running = ULONG_MAX;
3103 struct sched_group *group_min = NULL, *group_leader = NULL;
3104#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
3106 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003107 busiest_load_per_task = busiest_nr_running = 0;
3108 this_load_per_task = this_nr_running = 0;
Peter Zijlstra408ed062008-06-27 13:41:28 +02003109
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003110 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003111 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003112 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07003113 load_idx = sd->newidle_idx;
3114 else
3115 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
3117 do {
Ken Chen908a7c12007-10-17 16:55:11 +02003118 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 int local_group;
3120 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02003121 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003122 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07003123 unsigned long sum_nr_running, sum_weighted_load;
Peter Zijlstra408ed062008-06-27 13:41:28 +02003124 unsigned long sum_avg_load_per_task;
3125 unsigned long avg_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Rusty Russell758b2cd2008-11-25 02:35:04 +10303127 local_group = cpumask_test_cpu(this_cpu,
3128 sched_group_cpus(group));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003130 if (local_group)
Rusty Russell758b2cd2008-11-25 02:35:04 +10303131 balance_cpu = cpumask_first(sched_group_cpus(group));
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07003134 sum_weighted_load = sum_nr_running = avg_load = 0;
Peter Zijlstra408ed062008-06-27 13:41:28 +02003135 sum_avg_load_per_task = avg_load_per_task = 0;
3136
Ken Chen908a7c12007-10-17 16:55:11 +02003137 max_cpu_load = 0;
3138 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
Rusty Russell758b2cd2008-11-25 02:35:04 +10303140 for_each_cpu_and(i, sched_group_cpus(group), cpus) {
3141 struct rq *rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07003142
Suresh Siddha9439aab2007-07-19 21:28:35 +02003143 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07003144 *sd_idle = 0;
3145
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003147 if (local_group) {
3148 if (idle_cpu(i) && !first_idle_cpu) {
3149 first_idle_cpu = 1;
3150 balance_cpu = i;
3151 }
3152
Nick Piggina2000572006-02-10 01:51:02 -08003153 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003154 } else {
Nick Piggina2000572006-02-10 01:51:02 -08003155 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02003156 if (load > max_cpu_load)
3157 max_cpu_load = load;
3158 if (min_cpu_load > load)
3159 min_cpu_load = load;
3160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
3162 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07003163 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003164 sum_weighted_load += weighted_cpuload(i);
Peter Zijlstra408ed062008-06-27 13:41:28 +02003165
3166 sum_avg_load_per_task += cpu_avg_load_per_task(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 }
3168
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003169 /*
3170 * First idle cpu or the first cpu(busiest) in this sched group
3171 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02003172 * domains. In the newly idle case, we will allow all the cpu's
3173 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003174 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02003175 if (idle != CPU_NEWLY_IDLE && local_group &&
3176 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003177 *balance = 0;
3178 goto ret;
3179 }
3180
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07003182 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
3184 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07003185 avg_load = sg_div_cpu_power(group,
3186 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
Peter Zijlstra408ed062008-06-27 13:41:28 +02003188
3189 /*
3190 * Consider the group unbalanced when the imbalance is larger
3191 * than the average weight of two tasks.
3192 *
3193 * APZ: with cgroup the avg task weight can vary wildly and
3194 * might not be a suitable number - should we keep a
3195 * normalized nr_running number somewhere that negates
3196 * the hierarchy?
3197 */
3198 avg_load_per_task = sg_div_cpu_power(group,
3199 sum_avg_load_per_task * SCHED_LOAD_SCALE);
3200
3201 if ((max_cpu_load - min_cpu_load) > 2*avg_load_per_task)
Ken Chen908a7c12007-10-17 16:55:11 +02003202 __group_imb = 1;
3203
Eric Dumazet5517d862007-05-08 00:32:57 -07003204 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003205
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 if (local_group) {
3207 this_load = avg_load;
3208 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003209 this_nr_running = sum_nr_running;
3210 this_load_per_task = sum_weighted_load;
3211 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02003212 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 max_load = avg_load;
3214 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07003215 busiest_nr_running = sum_nr_running;
3216 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02003217 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003219
3220#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
3221 /*
3222 * Busy processors will not participate in power savings
3223 * balance.
3224 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003225 if (idle == CPU_NOT_IDLE ||
3226 !(sd->flags & SD_POWERSAVINGS_BALANCE))
3227 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003228
3229 /*
3230 * If the local group is idle or completely loaded
3231 * no need to do power savings balance at this domain
3232 */
3233 if (local_group && (this_nr_running >= group_capacity ||
3234 !this_nr_running))
3235 power_savings_balance = 0;
3236
Ingo Molnardd41f592007-07-09 18:51:59 +02003237 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003238 * If a group is already running at full capacity or idle,
3239 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02003240 */
3241 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003242 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02003243 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003244
Ingo Molnardd41f592007-07-09 18:51:59 +02003245 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003246 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02003247 * This is the group from where we need to pick up the load
3248 * for saving power
3249 */
3250 if ((sum_nr_running < min_nr_running) ||
3251 (sum_nr_running == min_nr_running &&
Vaidyanathan Srinivasand5679bd2008-12-18 23:26:16 +05303252 cpumask_first(sched_group_cpus(group)) >
Rusty Russell758b2cd2008-11-25 02:35:04 +10303253 cpumask_first(sched_group_cpus(group_min)))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003254 group_min = group;
3255 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003256 min_load_per_task = sum_weighted_load /
3257 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02003258 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003259
Ingo Molnardd41f592007-07-09 18:51:59 +02003260 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003261 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02003262 * capacity but still has some space to pick up some load
3263 * from other group and save more power
3264 */
3265 if (sum_nr_running <= group_capacity - 1) {
3266 if (sum_nr_running > leader_nr_running ||
3267 (sum_nr_running == leader_nr_running &&
Vaidyanathan Srinivasand5679bd2008-12-18 23:26:16 +05303268 cpumask_first(sched_group_cpus(group)) <
Rusty Russell758b2cd2008-11-25 02:35:04 +10303269 cpumask_first(sched_group_cpus(group_leader)))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003270 group_leader = group;
3271 leader_nr_running = sum_nr_running;
3272 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07003273 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003274group_next:
3275#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 group = group->next;
3277 } while (group != sd->groups);
3278
Peter Williams2dd73a42006-06-27 02:54:34 -07003279 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 goto out_balanced;
3281
3282 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
3283
3284 if (this_load >= avg_load ||
3285 100*max_load <= sd->imbalance_pct*this_load)
3286 goto out_balanced;
3287
Peter Williams2dd73a42006-06-27 02:54:34 -07003288 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02003289 if (group_imb)
3290 busiest_load_per_task = min(busiest_load_per_task, avg_load);
3291
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 /*
3293 * We're trying to get all the cpus to the average_load, so we don't
3294 * want to push ourselves above the average load, nor do we wish to
3295 * reduce the max loaded cpu below the average load, as either of these
3296 * actions would just result in more rebalancing later, and ping-pong
3297 * tasks around. Thus we look for the minimum possible imbalance.
3298 * Negative imbalances (*we* are more loaded than anyone else) will
3299 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003300 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 * appear as very large values with unsigned longs.
3302 */
Peter Williams2dd73a42006-06-27 02:54:34 -07003303 if (max_load <= busiest_load_per_task)
3304 goto out_balanced;
3305
3306 /*
3307 * In the presence of smp nice balancing, certain scenarios can have
3308 * max load less than avg load(as we skip the groups at or below
3309 * its cpu_power, while calculating max_load..)
3310 */
3311 if (max_load < avg_load) {
3312 *imbalance = 0;
3313 goto small_imbalance;
3314 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003315
3316 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07003317 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07003318
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07003320 *imbalance = min(max_pull * busiest->__cpu_power,
3321 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 / SCHED_LOAD_SCALE;
3323
Peter Williams2dd73a42006-06-27 02:54:34 -07003324 /*
3325 * if *imbalance is less than the average load per runnable task
3326 * there is no gaurantee that any tasks will be moved so we'll have
3327 * a think about bumping its value to force at least one task to be
3328 * moved
3329 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003330 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003331 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07003332 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
Peter Williams2dd73a42006-06-27 02:54:34 -07003334small_imbalance:
3335 pwr_move = pwr_now = 0;
3336 imbn = 2;
3337 if (this_nr_running) {
3338 this_load_per_task /= this_nr_running;
3339 if (busiest_load_per_task > this_load_per_task)
3340 imbn = 1;
3341 } else
Peter Zijlstra408ed062008-06-27 13:41:28 +02003342 this_load_per_task = cpu_avg_load_per_task(this_cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07003343
Peter Zijlstra01c8c572008-10-24 11:06:12 +02003344 if (max_load - this_load + busiest_load_per_task >=
Ingo Molnardd41f592007-07-09 18:51:59 +02003345 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07003346 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 return busiest;
3348 }
3349
3350 /*
3351 * OK, we don't have enough imbalance to justify moving tasks,
3352 * however we may be able to increase total CPU power used by
3353 * moving them.
3354 */
3355
Eric Dumazet5517d862007-05-08 00:32:57 -07003356 pwr_now += busiest->__cpu_power *
3357 min(busiest_load_per_task, max_load);
3358 pwr_now += this->__cpu_power *
3359 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 pwr_now /= SCHED_LOAD_SCALE;
3361
3362 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07003363 tmp = sg_div_cpu_power(busiest,
3364 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07003366 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07003367 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
3369 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07003370 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003371 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07003372 tmp = sg_div_cpu_power(this,
3373 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 else
Eric Dumazet5517d862007-05-08 00:32:57 -07003375 tmp = sg_div_cpu_power(this,
3376 busiest_load_per_task * SCHED_LOAD_SCALE);
3377 pwr_move += this->__cpu_power *
3378 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 pwr_move /= SCHED_LOAD_SCALE;
3380
3381 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003382 if (pwr_move > pwr_now)
3383 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 }
3385
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 return busiest;
3387
3388out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003389#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003390 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003391 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003393 if (this == group_leader && group_leader != group_min) {
3394 *imbalance = min_load_per_task;
Vaidyanathan Srinivasan7a09b1a2008-12-18 23:26:22 +05303395 if (sched_mc_power_savings >= POWERSAVINGS_BALANCE_WAKEUP) {
3396 cpu_rq(this_cpu)->rd->sched_mc_preferred_wakeup_cpu =
3397 first_cpu(group_leader->cpumask);
3398 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003399 return group_min;
3400 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003401#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003402ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 *imbalance = 0;
3404 return NULL;
3405}
3406
3407/*
3408 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3409 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003410static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003411find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Rusty Russell96f874e22008-11-25 02:35:14 +10303412 unsigned long imbalance, const struct cpumask *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003414 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003415 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 int i;
3417
Rusty Russell758b2cd2008-11-25 02:35:04 +10303418 for_each_cpu(i, sched_group_cpus(group)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003419 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003420
Rusty Russell96f874e22008-11-25 02:35:14 +10303421 if (!cpumask_test_cpu(i, cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003422 continue;
3423
Ingo Molnar48f24c42006-07-03 00:25:40 -07003424 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003425 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
Ingo Molnardd41f592007-07-09 18:51:59 +02003427 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003428 continue;
3429
Ingo Molnardd41f592007-07-09 18:51:59 +02003430 if (wl > max_load) {
3431 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003432 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 }
3434 }
3435
3436 return busiest;
3437}
3438
3439/*
Nick Piggin77391d72005-06-25 14:57:30 -07003440 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3441 * so long as it is large enough.
3442 */
3443#define MAX_PINNED_INTERVAL 512
3444
3445/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3447 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003449static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003450 struct sched_domain *sd, enum cpu_idle_type idle,
Rusty Russell96f874e22008-11-25 02:35:14 +10303451 int *balance, struct cpumask *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452{
Peter Williams43010652007-08-09 11:16:46 +02003453 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003456 struct rq *busiest;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003457 unsigned long flags;
Nick Piggin5969fe02005-09-10 00:26:19 -07003458
Rusty Russell96f874e22008-11-25 02:35:14 +10303459 cpumask_setall(cpus);
Mike Travis7c16ec52008-04-04 18:11:11 -07003460
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003461 /*
3462 * When power savings policy is enabled for the parent domain, idle
3463 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003464 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003465 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003466 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003467 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003468 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003469 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
Ingo Molnar2d723762007-10-15 17:00:12 +02003471 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003473redo:
Peter Zijlstrac8cba852008-06-27 13:41:23 +02003474 update_shares(sd);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003475 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Mike Travis7c16ec52008-04-04 18:11:11 -07003476 cpus, balance);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003477
Chen, Kenneth W06066712006-12-10 02:20:35 -08003478 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003479 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003480
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 if (!group) {
3482 schedstat_inc(sd, lb_nobusyg[idle]);
3483 goto out_balanced;
3484 }
3485
Mike Travis7c16ec52008-04-04 18:11:11 -07003486 busiest = find_busiest_queue(group, idle, imbalance, cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 if (!busiest) {
3488 schedstat_inc(sd, lb_nobusyq[idle]);
3489 goto out_balanced;
3490 }
3491
Nick Piggindb935db2005-06-25 14:57:11 -07003492 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
3494 schedstat_add(sd, lb_imbalance[idle], imbalance);
3495
Peter Williams43010652007-08-09 11:16:46 +02003496 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 if (busiest->nr_running > 1) {
3498 /*
3499 * Attempt to move tasks. If find_busiest_group has found
3500 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003501 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 * correctly treated as an imbalance.
3503 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003504 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003505 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003506 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003507 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003508 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003509 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003510
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003511 /*
3512 * some other cpu did the load balance for us.
3513 */
Peter Williams43010652007-08-09 11:16:46 +02003514 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003515 resched_cpu(this_cpu);
3516
Nick Piggin81026792005-06-25 14:57:07 -07003517 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003518 if (unlikely(all_pinned)) {
Rusty Russell96f874e22008-11-25 02:35:14 +10303519 cpumask_clear_cpu(cpu_of(busiest), cpus);
3520 if (!cpumask_empty(cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003521 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003522 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 }
Nick Piggin81026792005-06-25 14:57:07 -07003525
Peter Williams43010652007-08-09 11:16:46 +02003526 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 schedstat_inc(sd, lb_failed[idle]);
3528 sd->nr_balance_failed++;
3529
3530 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003532 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003533
3534 /* don't kick the migration_thread, if the curr
3535 * task on busiest cpu can't be moved to this_cpu
3536 */
Rusty Russell96f874e22008-11-25 02:35:14 +10303537 if (!cpumask_test_cpu(this_cpu,
3538 &busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003539 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003540 all_pinned = 1;
3541 goto out_one_pinned;
3542 }
3543
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 if (!busiest->active_balance) {
3545 busiest->active_balance = 1;
3546 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003547 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003549 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003550 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 wake_up_process(busiest->migration_thread);
3552
3553 /*
3554 * We've kicked active balancing, reset the failure
3555 * counter.
3556 */
Nick Piggin39507452005-06-25 14:57:09 -07003557 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 }
Nick Piggin81026792005-06-25 14:57:07 -07003559 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 sd->nr_balance_failed = 0;
3561
Nick Piggin81026792005-06-25 14:57:07 -07003562 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 /* We were unbalanced, so reset the balancing interval */
3564 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003565 } else {
3566 /*
3567 * If we've begun active balancing, start to back off. This
3568 * case may not be covered by the all_pinned logic if there
3569 * is only 1 task on the busy runqueue (because we don't call
3570 * move_tasks).
3571 */
3572 if (sd->balance_interval < sd->max_interval)
3573 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 }
3575
Peter Williams43010652007-08-09 11:16:46 +02003576 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003577 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstrac09595f2008-06-27 13:41:14 +02003578 ld_moved = -1;
3579
3580 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581
3582out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 schedstat_inc(sd, lb_balanced[idle]);
3584
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003585 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003586
3587out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003589 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3590 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 sd->balance_interval *= 2;
3592
Ingo Molnar48f24c42006-07-03 00:25:40 -07003593 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003594 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Peter Zijlstrac09595f2008-06-27 13:41:14 +02003595 ld_moved = -1;
3596 else
3597 ld_moved = 0;
3598out:
Peter Zijlstrac8cba852008-06-27 13:41:23 +02003599 if (ld_moved)
3600 update_shares(sd);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02003601 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602}
3603
3604/*
3605 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3606 * tasks if there is an imbalance.
3607 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003608 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 * this_rq is locked.
3610 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003611static int
Mike Travis7c16ec52008-04-04 18:11:11 -07003612load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
Rusty Russell96f874e22008-11-25 02:35:14 +10303613 struct cpumask *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614{
3615 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003616 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003618 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003619 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003620 int all_pinned = 0;
Mike Travis7c16ec52008-04-04 18:11:11 -07003621
Rusty Russell96f874e22008-11-25 02:35:14 +10303622 cpumask_setall(cpus);
Nick Piggin5969fe02005-09-10 00:26:19 -07003623
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003624 /*
3625 * When power savings policy is enabled for the parent domain, idle
3626 * sibling can pick up load irrespective of busy siblings. In this case,
3627 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003628 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003629 */
3630 if (sd->flags & SD_SHARE_CPUPOWER &&
3631 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003632 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
Ingo Molnar2d723762007-10-15 17:00:12 +02003634 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003635redo:
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02003636 update_shares_locked(this_rq, sd);
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003637 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Mike Travis7c16ec52008-04-04 18:11:11 -07003638 &sd_idle, cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003640 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003641 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 }
3643
Mike Travis7c16ec52008-04-04 18:11:11 -07003644 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003645 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003646 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003647 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 }
3649
Nick Piggindb935db2005-06-25 14:57:11 -07003650 BUG_ON(busiest == this_rq);
3651
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003652 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003653
Peter Williams43010652007-08-09 11:16:46 +02003654 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003655 if (busiest->nr_running > 1) {
3656 /* Attempt to move tasks */
3657 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003658 /* this_rq->clock is already updated */
3659 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003660 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003661 imbalance, sd, CPU_NEWLY_IDLE,
3662 &all_pinned);
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02003663 double_unlock_balance(this_rq, busiest);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003664
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003665 if (unlikely(all_pinned)) {
Rusty Russell96f874e22008-11-25 02:35:14 +10303666 cpumask_clear_cpu(cpu_of(busiest), cpus);
3667 if (!cpumask_empty(cpus))
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003668 goto redo;
3669 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003670 }
3671
Peter Williams43010652007-08-09 11:16:46 +02003672 if (!ld_moved) {
Vaidyanathan Srinivasanad273b32008-12-18 23:26:36 +05303673 int active_balance;
3674
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003675 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003676 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3677 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003678 return -1;
Vaidyanathan Srinivasanad273b32008-12-18 23:26:36 +05303679
3680 if (sched_mc_power_savings < POWERSAVINGS_BALANCE_WAKEUP)
3681 return -1;
3682
3683 if (sd->nr_balance_failed++ < 2)
3684 return -1;
3685
3686 /*
3687 * The only task running in a non-idle cpu can be moved to this
3688 * cpu in an attempt to completely freeup the other CPU
3689 * package. The same method used to move task in load_balance()
3690 * have been extended for load_balance_newidle() to speedup
3691 * consolidation at sched_mc=POWERSAVINGS_BALANCE_WAKEUP (2)
3692 *
3693 * The package power saving logic comes from
3694 * find_busiest_group(). If there are no imbalance, then
3695 * f_b_g() will return NULL. However when sched_mc={1,2} then
3696 * f_b_g() will select a group from which a running task may be
3697 * pulled to this cpu in order to make the other package idle.
3698 * If there is no opportunity to make a package idle and if
3699 * there are no imbalance, then f_b_g() will return NULL and no
3700 * action will be taken in load_balance_newidle().
3701 *
3702 * Under normal task pull operation due to imbalance, there
3703 * will be more than one task in the source run queue and
3704 * move_tasks() will succeed. ld_moved will be true and this
3705 * active balance code will not be triggered.
3706 */
3707
3708 /* Lock busiest in correct order while this_rq is held */
3709 double_lock_balance(this_rq, busiest);
3710
3711 /*
3712 * don't kick the migration_thread, if the curr
3713 * task on busiest cpu can't be moved to this_cpu
3714 */
3715 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
3716 double_unlock_balance(this_rq, busiest);
3717 all_pinned = 1;
3718 return ld_moved;
3719 }
3720
3721 if (!busiest->active_balance) {
3722 busiest->active_balance = 1;
3723 busiest->push_cpu = this_cpu;
3724 active_balance = 1;
3725 }
3726
3727 double_unlock_balance(this_rq, busiest);
3728 if (active_balance)
3729 wake_up_process(busiest->migration_thread);
3730
Nick Piggin5969fe02005-09-10 00:26:19 -07003731 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003732 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
Peter Zijlstra3e5459b2008-06-27 13:41:24 +02003734 update_shares_locked(this_rq, sd);
Peter Williams43010652007-08-09 11:16:46 +02003735 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003736
3737out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003738 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003739 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003740 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003741 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003742 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003743
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003744 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745}
3746
3747/*
3748 * idle_balance is called by schedule() if this_cpu is about to become
3749 * idle. Attempts to pull tasks from other CPUs.
3750 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003751static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752{
3753 struct sched_domain *sd;
Vaidyanathan Srinivasanefbe0272008-12-08 20:52:49 +05303754 int pulled_task = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02003755 unsigned long next_balance = jiffies + HZ;
Rusty Russell4d2732c2008-11-25 02:35:10 +10303756 cpumask_var_t tmpmask;
3757
3758 if (!alloc_cpumask_var(&tmpmask, GFP_ATOMIC))
3759 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760
3761 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003762 unsigned long interval;
3763
3764 if (!(sd->flags & SD_LOAD_BALANCE))
3765 continue;
3766
3767 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003768 /* If we've pulled tasks over stop searching: */
Mike Travis7c16ec52008-04-04 18:11:11 -07003769 pulled_task = load_balance_newidle(this_cpu, this_rq,
Rusty Russell4d2732c2008-11-25 02:35:10 +10303770 sd, tmpmask);
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003771
3772 interval = msecs_to_jiffies(sd->balance_interval);
3773 if (time_after(next_balance, sd->last_balance + interval))
3774 next_balance = sd->last_balance + interval;
3775 if (pulled_task)
3776 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003778 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003779 /*
3780 * We are going idle. next_balance may be set based on
3781 * a busy processor. So reset next_balance.
3782 */
3783 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003784 }
Rusty Russell4d2732c2008-11-25 02:35:10 +10303785 free_cpumask_var(tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786}
3787
3788/*
3789 * active_load_balance is run by migration threads. It pushes running tasks
3790 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3791 * running on each physical CPU where possible, and avoids physical /
3792 * logical imbalances.
3793 *
3794 * Called with busiest_rq locked.
3795 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003796static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797{
Nick Piggin39507452005-06-25 14:57:09 -07003798 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003799 struct sched_domain *sd;
3800 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003801
Ingo Molnar48f24c42006-07-03 00:25:40 -07003802 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003803 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003804 return;
3805
3806 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807
3808 /*
Nick Piggin39507452005-06-25 14:57:09 -07003809 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003810 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003811 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 */
Nick Piggin39507452005-06-25 14:57:09 -07003813 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
Nick Piggin39507452005-06-25 14:57:09 -07003815 /* move a task from busiest_rq to target_rq */
3816 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003817 update_rq_clock(busiest_rq);
3818 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819
Nick Piggin39507452005-06-25 14:57:09 -07003820 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003821 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07003822 if ((sd->flags & SD_LOAD_BALANCE) &&
Rusty Russell758b2cd2008-11-25 02:35:04 +10303823 cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
Nick Piggin39507452005-06-25 14:57:09 -07003824 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
Ingo Molnar48f24c42006-07-03 00:25:40 -07003827 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003828 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
Peter Williams43010652007-08-09 11:16:46 +02003830 if (move_one_task(target_rq, target_cpu, busiest_rq,
3831 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07003832 schedstat_inc(sd, alb_pushed);
3833 else
3834 schedstat_inc(sd, alb_failed);
3835 }
Peter Zijlstra1b12bbc2008-08-11 09:30:22 +02003836 double_unlock_balance(busiest_rq, target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837}
3838
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003839#ifdef CONFIG_NO_HZ
3840static struct {
3841 atomic_t load_balancer;
Rusty Russell7d1e6a92008-11-25 02:35:09 +10303842 cpumask_var_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003843} nohz ____cacheline_aligned = {
3844 .load_balancer = ATOMIC_INIT(-1),
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003845};
3846
Christoph Lameter7835b982006-12-10 02:20:22 -08003847/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003848 * This routine will try to nominate the ilb (idle load balancing)
3849 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3850 * load balancing on behalf of all those cpus. If all the cpus in the system
3851 * go into this tickless mode, then there will be no ilb owner (as there is
3852 * no need for one) and all the cpus will sleep till the next wakeup event
3853 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08003854 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003855 * For the ilb owner, tick is not stopped. And this tick will be used
3856 * for idle load balancing. ilb owner will still be part of
3857 * nohz.cpu_mask..
3858 *
3859 * While stopping the tick, this cpu will become the ilb owner if there
3860 * is no other owner. And will be the owner till that cpu becomes busy
3861 * or if all cpus in the system stop their ticks at which point
3862 * there is no need for ilb owner.
3863 *
3864 * When the ilb owner becomes busy, it nominates another owner, during the
3865 * next busy scheduler_tick()
3866 */
3867int select_nohz_load_balancer(int stop_tick)
3868{
3869 int cpu = smp_processor_id();
3870
3871 if (stop_tick) {
Rusty Russell7d1e6a92008-11-25 02:35:09 +10303872 cpumask_set_cpu(cpu, nohz.cpu_mask);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003873 cpu_rq(cpu)->in_nohz_recently = 1;
3874
3875 /*
3876 * If we are going offline and still the leader, give up!
3877 */
Max Krasnyanskye761b772008-07-15 04:43:49 -07003878 if (!cpu_active(cpu) &&
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003879 atomic_read(&nohz.load_balancer) == cpu) {
3880 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3881 BUG();
3882 return 0;
3883 }
3884
3885 /* time for ilb owner also to sleep */
Rusty Russell7d1e6a92008-11-25 02:35:09 +10303886 if (cpumask_weight(nohz.cpu_mask) == num_online_cpus()) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003887 if (atomic_read(&nohz.load_balancer) == cpu)
3888 atomic_set(&nohz.load_balancer, -1);
3889 return 0;
3890 }
3891
3892 if (atomic_read(&nohz.load_balancer) == -1) {
3893 /* make me the ilb owner */
3894 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
3895 return 1;
3896 } else if (atomic_read(&nohz.load_balancer) == cpu)
3897 return 1;
3898 } else {
Rusty Russell7d1e6a92008-11-25 02:35:09 +10303899 if (!cpumask_test_cpu(cpu, nohz.cpu_mask))
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003900 return 0;
3901
Rusty Russell7d1e6a92008-11-25 02:35:09 +10303902 cpumask_clear_cpu(cpu, nohz.cpu_mask);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003903
3904 if (atomic_read(&nohz.load_balancer) == cpu)
3905 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3906 BUG();
3907 }
3908 return 0;
3909}
3910#endif
3911
3912static DEFINE_SPINLOCK(balancing);
3913
3914/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003915 * It checks each scheduling domain to see if it is due to be balanced,
3916 * and initiates a balancing operation if so.
3917 *
3918 * Balancing parameters are set up in arch_init_sched_domains.
3919 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003920static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08003921{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003922 int balance = 1;
3923 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08003924 unsigned long interval;
3925 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003926 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08003927 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003928 int update_next_balance = 0;
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02003929 int need_serialize;
Rusty Russella0e90242008-11-25 02:35:11 +10303930 cpumask_var_t tmp;
3931
3932 /* Fails alloc? Rebalancing probably not a priority right now. */
3933 if (!alloc_cpumask_var(&tmp, GFP_ATOMIC))
3934 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003936 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 if (!(sd->flags & SD_LOAD_BALANCE))
3938 continue;
3939
3940 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003941 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 interval *= sd->busy_factor;
3943
3944 /* scale ms to jiffies */
3945 interval = msecs_to_jiffies(interval);
3946 if (unlikely(!interval))
3947 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003948 if (interval > HZ*NR_CPUS/10)
3949 interval = HZ*NR_CPUS/10;
3950
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02003951 need_serialize = sd->flags & SD_SERIALIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02003953 if (need_serialize) {
Christoph Lameter08c183f2006-12-10 02:20:29 -08003954 if (!spin_trylock(&balancing))
3955 goto out;
3956 }
3957
Christoph Lameterc9819f42006-12-10 02:20:25 -08003958 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Rusty Russella0e90242008-11-25 02:35:11 +10303959 if (load_balance(cpu, rq, sd, idle, &balance, tmp)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003960 /*
3961 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07003962 * longer idle, or one of our SMT siblings is
3963 * not idle.
3964 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003965 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003967 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 }
Dmitry Adamushkod07355f2008-05-12 21:21:15 +02003969 if (need_serialize)
Christoph Lameter08c183f2006-12-10 02:20:29 -08003970 spin_unlock(&balancing);
3971out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02003972 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08003973 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003974 update_next_balance = 1;
3975 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003976
3977 /*
3978 * Stop the load balance at this level. There is another
3979 * CPU in our sched group which is doing load balancing more
3980 * actively.
3981 */
3982 if (!balance)
3983 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02003985
3986 /*
3987 * next_balance will be updated only when there is a need.
3988 * When the cpu is attached to null domain for ex, it will not be
3989 * updated.
3990 */
3991 if (likely(update_next_balance))
3992 rq->next_balance = next_balance;
Rusty Russella0e90242008-11-25 02:35:11 +10303993
3994 free_cpumask_var(tmp);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003995}
3996
3997/*
3998 * run_rebalance_domains is triggered when needed from the scheduler tick.
3999 * In CONFIG_NO_HZ case, the idle load balance owner will do the
4000 * rebalancing for all the cpus for whom scheduler ticks are stopped.
4001 */
4002static void run_rebalance_domains(struct softirq_action *h)
4003{
Ingo Molnardd41f592007-07-09 18:51:59 +02004004 int this_cpu = smp_processor_id();
4005 struct rq *this_rq = cpu_rq(this_cpu);
4006 enum cpu_idle_type idle = this_rq->idle_at_tick ?
4007 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004008
Ingo Molnardd41f592007-07-09 18:51:59 +02004009 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004010
4011#ifdef CONFIG_NO_HZ
4012 /*
4013 * If this cpu is the owner for idle load balancing, then do the
4014 * balancing on behalf of the other idle cpus whose ticks are
4015 * stopped.
4016 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004017 if (this_rq->idle_at_tick &&
4018 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004019 struct rq *rq;
4020 int balance_cpu;
4021
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304022 for_each_cpu(balance_cpu, nohz.cpu_mask) {
4023 if (balance_cpu == this_cpu)
4024 continue;
4025
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004026 /*
4027 * If this cpu gets work to do, stop the load balancing
4028 * work being done for other cpus. Next load
4029 * balancing owner will pick it up.
4030 */
4031 if (need_resched())
4032 break;
4033
Oleg Nesterovde0cf892007-08-12 18:08:19 +02004034 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004035
4036 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004037 if (time_after(this_rq->next_balance, rq->next_balance))
4038 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004039 }
4040 }
4041#endif
4042}
4043
4044/*
4045 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
4046 *
4047 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
4048 * idle load balancing owner or decide to stop the periodic load balancing,
4049 * if the whole system is idle.
4050 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004051static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004052{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004053#ifdef CONFIG_NO_HZ
4054 /*
4055 * If we were in the nohz mode recently and busy at the current
4056 * scheduler tick, then check if we need to nominate new idle
4057 * load balancer.
4058 */
4059 if (rq->in_nohz_recently && !rq->idle_at_tick) {
4060 rq->in_nohz_recently = 0;
4061
4062 if (atomic_read(&nohz.load_balancer) == cpu) {
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304063 cpumask_clear_cpu(cpu, nohz.cpu_mask);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004064 atomic_set(&nohz.load_balancer, -1);
4065 }
4066
4067 if (atomic_read(&nohz.load_balancer) == -1) {
4068 /*
4069 * simple selection for now: Nominate the
4070 * first cpu in the nohz list to be the next
4071 * ilb owner.
4072 *
4073 * TBD: Traverse the sched domains and nominate
4074 * the nearest cpu in the nohz.cpu_mask.
4075 */
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304076 int ilb = cpumask_first(nohz.cpu_mask);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004077
Mike Travis434d53b2008-04-04 18:11:04 -07004078 if (ilb < nr_cpu_ids)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004079 resched_cpu(ilb);
4080 }
4081 }
4082
4083 /*
4084 * If this cpu is idle and doing idle load balancing for all the
4085 * cpus with ticks stopped, is it time for that to stop?
4086 */
4087 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304088 cpumask_weight(nohz.cpu_mask) == num_online_cpus()) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004089 resched_cpu(cpu);
4090 return;
4091 }
4092
4093 /*
4094 * If this cpu is idle and the idle load balancing is done by
4095 * someone else, then no need raise the SCHED_SOFTIRQ
4096 */
4097 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
Rusty Russell7d1e6a92008-11-25 02:35:09 +10304098 cpumask_test_cpu(cpu, nohz.cpu_mask))
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07004099 return;
4100#endif
4101 if (time_after_eq(jiffies, rq->next_balance))
4102 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103}
Ingo Molnardd41f592007-07-09 18:51:59 +02004104
4105#else /* CONFIG_SMP */
4106
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107/*
4108 * on UP we do not need to balance between CPUs:
4109 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07004110static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111{
4112}
Ingo Molnardd41f592007-07-09 18:51:59 +02004113
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114#endif
4115
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116DEFINE_PER_CPU(struct kernel_stat, kstat);
4117
4118EXPORT_PER_CPU_SYMBOL(kstat);
4119
4120/*
Frank Mayharf06febc2008-09-12 09:54:39 -07004121 * Return any ns on the sched_clock that have not yet been banked in
4122 * @p in case that task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 */
Frank Mayharbb34d922008-09-12 09:54:39 -07004124unsigned long long task_delta_exec(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004127 struct rq *rq;
Frank Mayharbb34d922008-09-12 09:54:39 -07004128 u64 ns = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004129
Ingo Molnar41b86e92007-07-09 18:51:58 +02004130 rq = task_rq_lock(p, &flags);
Ingo Molnar15084872008-09-30 08:28:17 +02004131
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004132 if (task_current(rq, p)) {
Frank Mayharf06febc2008-09-12 09:54:39 -07004133 u64 delta_exec;
4134
Ingo Molnara8e504d2007-08-09 11:16:47 +02004135 update_rq_clock(rq);
4136 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004137 if ((s64)delta_exec > 0)
Frank Mayharbb34d922008-09-12 09:54:39 -07004138 ns = delta_exec;
Ingo Molnar41b86e92007-07-09 18:51:58 +02004139 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07004140
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 task_rq_unlock(rq, &flags);
4142
4143 return ns;
4144}
4145
4146/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 * Account user cpu time to a process.
4148 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 * @cputime: the cpu time spent in user space since the last update
4150 */
4151void account_user_time(struct task_struct *p, cputime_t cputime)
4152{
4153 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4154 cputime64_t tmp;
4155
4156 p->utime = cputime_add(p->utime, cputime);
Frank Mayharf06febc2008-09-12 09:54:39 -07004157 account_group_user_time(p, cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158
4159 /* Add user time to cpustat. */
4160 tmp = cputime_to_cputime64(cputime);
4161 if (TASK_NICE(p) > 0)
4162 cpustat->nice = cputime64_add(cpustat->nice, tmp);
4163 else
4164 cpustat->user = cputime64_add(cpustat->user, tmp);
Jonathan Lim49b5cf32008-07-25 01:48:40 -07004165 /* Account for user time used */
4166 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167}
4168
4169/*
Laurent Vivier94886b82007-10-15 17:00:19 +02004170 * Account guest cpu time to a process.
4171 * @p: the process that the cpu time gets accounted to
4172 * @cputime: the cpu time spent in virtual machine since the last update
4173 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01004174static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02004175{
4176 cputime64_t tmp;
4177 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4178
4179 tmp = cputime_to_cputime64(cputime);
4180
4181 p->utime = cputime_add(p->utime, cputime);
Frank Mayharf06febc2008-09-12 09:54:39 -07004182 account_group_user_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02004183 p->gtime = cputime_add(p->gtime, cputime);
4184
4185 cpustat->user = cputime64_add(cpustat->user, tmp);
4186 cpustat->guest = cputime64_add(cpustat->guest, tmp);
4187}
4188
4189/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004190 * Account scaled user cpu time to a process.
4191 * @p: the process that the cpu time gets accounted to
4192 * @cputime: the cpu time spent in user space since the last update
4193 */
4194void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
4195{
4196 p->utimescaled = cputime_add(p->utimescaled, cputime);
4197}
4198
4199/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 * Account system cpu time to a process.
4201 * @p: the process that the cpu time gets accounted to
4202 * @hardirq_offset: the offset to subtract from hardirq_count()
4203 * @cputime: the cpu time spent in kernel space since the last update
4204 */
4205void account_system_time(struct task_struct *p, int hardirq_offset,
4206 cputime_t cputime)
4207{
4208 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004209 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 cputime64_t tmp;
4211
Harvey Harrison983ed7a2008-04-24 18:17:55 -07004212 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
4213 account_guest_time(p, cputime);
4214 return;
4215 }
Laurent Vivier94886b82007-10-15 17:00:19 +02004216
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 p->stime = cputime_add(p->stime, cputime);
Frank Mayharf06febc2008-09-12 09:54:39 -07004218 account_group_system_time(p, cputime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219
4220 /* Add system time to cpustat. */
4221 tmp = cputime_to_cputime64(cputime);
4222 if (hardirq_count() - hardirq_offset)
4223 cpustat->irq = cputime64_add(cpustat->irq, tmp);
4224 else if (softirq_count())
4225 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004226 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004228 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4230 else
4231 cpustat->idle = cputime64_add(cpustat->idle, tmp);
4232 /* Account for system time used */
4233 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234}
4235
4236/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07004237 * Account scaled system cpu time to a process.
4238 * @p: the process that the cpu time gets accounted to
4239 * @hardirq_offset: the offset to subtract from hardirq_count()
4240 * @cputime: the cpu time spent in kernel space since the last update
4241 */
4242void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
4243{
4244 p->stimescaled = cputime_add(p->stimescaled, cputime);
4245}
4246
4247/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 * Account for involuntary wait time.
4249 * @p: the process from which the cpu time has been stolen
4250 * @steal: the cpu time spent in involuntary wait
4251 */
4252void account_steal_time(struct task_struct *p, cputime_t steal)
4253{
4254 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
4255 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07004256 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257
4258 if (p == rq->idle) {
4259 p->stime = cputime_add(p->stime, steal);
4260 if (atomic_read(&rq->nr_iowait) > 0)
4261 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
4262 else
4263 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08004264 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 cpustat->steal = cputime64_add(cpustat->steal, tmp);
4266}
4267
Christoph Lameter7835b982006-12-10 02:20:22 -08004268/*
Balbir Singh49048622008-09-05 18:12:23 +02004269 * Use precise platform statistics if available:
4270 */
4271#ifdef CONFIG_VIRT_CPU_ACCOUNTING
4272cputime_t task_utime(struct task_struct *p)
4273{
4274 return p->utime;
4275}
4276
4277cputime_t task_stime(struct task_struct *p)
4278{
4279 return p->stime;
4280}
4281#else
4282cputime_t task_utime(struct task_struct *p)
4283{
4284 clock_t utime = cputime_to_clock_t(p->utime),
4285 total = utime + cputime_to_clock_t(p->stime);
4286 u64 temp;
4287
4288 /*
4289 * Use CFS's precise accounting:
4290 */
4291 temp = (u64)nsec_to_clock_t(p->se.sum_exec_runtime);
4292
4293 if (total) {
4294 temp *= utime;
4295 do_div(temp, total);
4296 }
4297 utime = (clock_t)temp;
4298
4299 p->prev_utime = max(p->prev_utime, clock_t_to_cputime(utime));
4300 return p->prev_utime;
4301}
4302
4303cputime_t task_stime(struct task_struct *p)
4304{
4305 clock_t stime;
4306
4307 /*
4308 * Use CFS's precise accounting. (we subtract utime from
4309 * the total, to make sure the total observed by userspace
4310 * grows monotonically - apps rely on that):
4311 */
4312 stime = nsec_to_clock_t(p->se.sum_exec_runtime) -
4313 cputime_to_clock_t(task_utime(p));
4314
4315 if (stime >= 0)
4316 p->prev_stime = max(p->prev_stime, clock_t_to_cputime(stime));
4317
4318 return p->prev_stime;
4319}
4320#endif
4321
4322inline cputime_t task_gtime(struct task_struct *p)
4323{
4324 return p->gtime;
4325}
4326
4327/*
Christoph Lameter7835b982006-12-10 02:20:22 -08004328 * This function gets called by the timer code, with HZ frequency.
4329 * We call it with interrupts disabled.
4330 *
4331 * It also gets called by the fork code, when changing the parent's
4332 * timeslices.
4333 */
4334void scheduler_tick(void)
4335{
Christoph Lameter7835b982006-12-10 02:20:22 -08004336 int cpu = smp_processor_id();
4337 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004338 struct task_struct *curr = rq->curr;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004339
4340 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08004341
Ingo Molnardd41f592007-07-09 18:51:59 +02004342 spin_lock(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004343 update_rq_clock(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02004344 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01004345 curr->sched_class->task_tick(rq, curr, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02004346 spin_unlock(&rq->lock);
4347
Christoph Lametere418e1c2006-12-10 02:20:23 -08004348#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02004349 rq->idle_at_tick = idle_cpu(cpu);
4350 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08004351#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352}
4353
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004354#if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
4355 defined(CONFIG_PREEMPT_TRACER))
4356
4357static inline unsigned long get_parent_ip(unsigned long addr)
4358{
4359 if (in_lock_functions(addr)) {
4360 addr = CALLER_ADDR2;
4361 if (in_lock_functions(addr))
4362 addr = CALLER_ADDR3;
4363 }
4364 return addr;
4365}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366
Srinivasa Ds43627582008-02-23 15:24:04 -08004367void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004369#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 /*
4371 * Underflow?
4372 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004373 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4374 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004375#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 preempt_count() += val;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004377#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 /*
4379 * Spinlock count overflowing soon?
4380 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08004381 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
4382 PREEMPT_MASK - 10);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004383#endif
4384 if (preempt_count() == val)
4385 trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386}
4387EXPORT_SYMBOL(add_preempt_count);
4388
Srinivasa Ds43627582008-02-23 15:24:04 -08004389void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004391#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 /*
4393 * Underflow?
4394 */
Nick Piggin7317d7b2008-09-30 20:50:27 +10004395 if (DEBUG_LOCKS_WARN_ON(val > preempt_count() - (!!kernel_locked())))
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004396 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 /*
4398 * Is the spinlock portion underflowing?
4399 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004400 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
4401 !(preempt_count() & PREEMPT_MASK)))
4402 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004403#endif
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004404
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004405 if (preempt_count() == val)
4406 trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 preempt_count() -= val;
4408}
4409EXPORT_SYMBOL(sub_preempt_count);
4410
4411#endif
4412
4413/*
Ingo Molnardd41f592007-07-09 18:51:59 +02004414 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004416static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417{
Satyam Sharma838225b2007-10-24 18:23:50 +02004418 struct pt_regs *regs = get_irq_regs();
4419
4420 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4421 prev->comm, prev->pid, preempt_count());
4422
Ingo Molnardd41f592007-07-09 18:51:59 +02004423 debug_show_held_locks(prev);
Arjan van de Vene21f5b12008-05-23 09:05:58 -07004424 print_modules();
Ingo Molnardd41f592007-07-09 18:51:59 +02004425 if (irqs_disabled())
4426 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02004427
4428 if (regs)
4429 show_regs(regs);
4430 else
4431 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02004432}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
Ingo Molnardd41f592007-07-09 18:51:59 +02004434/*
4435 * Various schedule()-time debugging checks and statistics:
4436 */
4437static inline void schedule_debug(struct task_struct *prev)
4438{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004440 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 * schedule() atomically, we ignore that path for now.
4442 * Otherwise, whine if we are scheduling when we should not be.
4443 */
Roel Kluin3f33a7c2008-05-13 23:44:11 +02004444 if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
Ingo Molnardd41f592007-07-09 18:51:59 +02004445 __schedule_bug(prev);
4446
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4448
Ingo Molnar2d723762007-10-15 17:00:12 +02004449 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004450#ifdef CONFIG_SCHEDSTATS
4451 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02004452 schedstat_inc(this_rq(), bkl_count);
4453 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02004454 }
4455#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004456}
4457
4458/*
4459 * Pick up the highest-prio task:
4460 */
4461static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004462pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02004463{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02004464 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004465 struct task_struct *p;
4466
4467 /*
4468 * Optimization: we know that if all tasks are in
4469 * the fair class we can call that function directly:
4470 */
4471 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004472 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004473 if (likely(p))
4474 return p;
4475 }
4476
4477 class = sched_class_highest;
4478 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02004479 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004480 if (p)
4481 return p;
4482 /*
4483 * Will never be NULL as the idle class always
4484 * returns a non-NULL p:
4485 */
4486 class = class->next;
4487 }
4488}
4489
4490/*
4491 * schedule() is the main scheduler function.
4492 */
4493asmlinkage void __sched schedule(void)
4494{
4495 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004496 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02004497 struct rq *rq;
Peter Zijlstra31656512008-07-18 18:01:23 +02004498 int cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02004499
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500need_resched:
4501 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02004502 cpu = smp_processor_id();
4503 rq = cpu_rq(cpu);
4504 rcu_qsctr_inc(cpu);
4505 prev = rq->curr;
4506 switch_count = &prev->nivcsw;
4507
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 release_kernel_lock(prev);
4509need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510
Ingo Molnardd41f592007-07-09 18:51:59 +02004511 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
Peter Zijlstra31656512008-07-18 18:01:23 +02004513 if (sched_feat(HRTICK))
Mike Galbraithf333fdc2008-05-12 21:20:55 +02004514 hrtick_clear(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004515
Peter Zijlstra8cd162c2008-10-15 20:37:23 +02004516 spin_lock_irq(&rq->lock);
Peter Zijlstra3e51f332008-05-03 18:29:28 +02004517 update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02004518 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
Ingo Molnardd41f592007-07-09 18:51:59 +02004520 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
Oleg Nesterov16882c12008-06-08 21:20:41 +04004521 if (unlikely(signal_pending_state(prev->state, prev)))
Ingo Molnardd41f592007-07-09 18:51:59 +02004522 prev->state = TASK_RUNNING;
Oleg Nesterov16882c12008-06-08 21:20:41 +04004523 else
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004524 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02004525 switch_count = &prev->nvcsw;
4526 }
4527
Steven Rostedt9a897c52008-01-25 21:08:22 +01004528#ifdef CONFIG_SMP
4529 if (prev->sched_class->pre_schedule)
4530 prev->sched_class->pre_schedule(rq, prev);
4531#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01004532
Ingo Molnardd41f592007-07-09 18:51:59 +02004533 if (unlikely(!rq->nr_running))
4534 idle_balance(cpu, rq);
4535
Ingo Molnar31ee5292007-08-09 11:16:49 +02004536 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004537 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 if (likely(prev != next)) {
David Simner673a90a2008-04-29 10:08:59 +01004540 sched_info_switch(prev, next);
4541
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 rq->nr_switches++;
4543 rq->curr = next;
4544 ++*switch_count;
4545
Ingo Molnardd41f592007-07-09 18:51:59 +02004546 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004547 /*
4548 * the context switch might have flipped the stack from under
4549 * us, hence refresh the local variables.
4550 */
4551 cpu = smp_processor_id();
4552 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 } else
4554 spin_unlock_irq(&rq->lock);
4555
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004556 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004558
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 preempt_enable_no_resched();
4560 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4561 goto need_resched;
4562}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563EXPORT_SYMBOL(schedule);
4564
4565#ifdef CONFIG_PREEMPT
4566/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004567 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004568 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 * occur there and call schedule directly.
4570 */
4571asmlinkage void __sched preempt_schedule(void)
4572{
4573 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004574
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 /*
4576 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004577 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004579 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 return;
4581
Andi Kleen3a5c3592007-10-15 17:00:14 +02004582 do {
4583 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004584 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004585 sub_preempt_count(PREEMPT_ACTIVE);
4586
4587 /*
4588 * Check again in case we missed a preemption opportunity
4589 * between schedule and now.
4590 */
4591 barrier();
4592 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594EXPORT_SYMBOL(preempt_schedule);
4595
4596/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004597 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 * off of irq context.
4599 * Note, that this is called and return with irqs disabled. This will
4600 * protect us against recursive calling from irq.
4601 */
4602asmlinkage void __sched preempt_schedule_irq(void)
4603{
4604 struct thread_info *ti = current_thread_info();
Ingo Molnar6478d882008-01-25 21:08:33 +01004605
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004606 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 BUG_ON(ti->preempt_count || !irqs_disabled());
4608
Andi Kleen3a5c3592007-10-15 17:00:14 +02004609 do {
4610 add_preempt_count(PREEMPT_ACTIVE);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004611 local_irq_enable();
4612 schedule();
4613 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004614 sub_preempt_count(PREEMPT_ACTIVE);
4615
4616 /*
4617 * Check again in case we missed a preemption opportunity
4618 * between schedule and now.
4619 */
4620 barrier();
4621 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622}
4623
4624#endif /* CONFIG_PREEMPT */
4625
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004626int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4627 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004629 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631EXPORT_SYMBOL(default_wake_function);
4632
4633/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004634 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4635 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 * number) then we wake all the non-exclusive tasks and one exclusive task.
4637 *
4638 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004639 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4641 */
4642static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4643 int nr_exclusive, int sync, void *key)
4644{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004645 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004647 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004648 unsigned flags = curr->flags;
4649
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004651 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 break;
4653 }
4654}
4655
4656/**
4657 * __wake_up - wake up threads blocked on a waitqueue.
4658 * @q: the waitqueue
4659 * @mode: which threads
4660 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004661 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004663void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004664 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665{
4666 unsigned long flags;
4667
4668 spin_lock_irqsave(&q->lock, flags);
4669 __wake_up_common(q, mode, nr_exclusive, 0, key);
4670 spin_unlock_irqrestore(&q->lock, flags);
4671}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672EXPORT_SYMBOL(__wake_up);
4673
4674/*
4675 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4676 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004677void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678{
4679 __wake_up_common(q, mode, 1, 0, NULL);
4680}
4681
4682/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004683 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 * @q: the waitqueue
4685 * @mode: which threads
4686 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4687 *
4688 * The sync wakeup differs that the waker knows that it will schedule
4689 * away soon, so while the target thread will be woken up, it will not
4690 * be migrated to another CPU - ie. the two threads are 'synchronized'
4691 * with each other. This can prevent needless bouncing between CPUs.
4692 *
4693 * On UP it can prevent extra preemption.
4694 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004695void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004696__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697{
4698 unsigned long flags;
4699 int sync = 1;
4700
4701 if (unlikely(!q))
4702 return;
4703
4704 if (unlikely(!nr_exclusive))
4705 sync = 0;
4706
4707 spin_lock_irqsave(&q->lock, flags);
4708 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4709 spin_unlock_irqrestore(&q->lock, flags);
4710}
4711EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4712
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004713/**
4714 * complete: - signals a single thread waiting on this completion
4715 * @x: holds the state of this particular completion
4716 *
4717 * This will wake up a single thread waiting on this completion. Threads will be
4718 * awakened in the same order in which they were queued.
4719 *
4720 * See also complete_all(), wait_for_completion() and related routines.
4721 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004722void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723{
4724 unsigned long flags;
4725
4726 spin_lock_irqsave(&x->wait.lock, flags);
4727 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004728 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 spin_unlock_irqrestore(&x->wait.lock, flags);
4730}
4731EXPORT_SYMBOL(complete);
4732
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004733/**
4734 * complete_all: - signals all threads waiting on this completion
4735 * @x: holds the state of this particular completion
4736 *
4737 * This will wake up all threads waiting on this particular completion event.
4738 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004739void complete_all(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 += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004745 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 spin_unlock_irqrestore(&x->wait.lock, flags);
4747}
4748EXPORT_SYMBOL(complete_all);
4749
Andi Kleen8cbbe862007-10-15 17:00:14 +02004750static inline long __sched
4751do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 if (!x->done) {
4754 DECLARE_WAITQUEUE(wait, current);
4755
4756 wait.flags |= WQ_FLAG_EXCLUSIVE;
4757 __add_wait_queue_tail(&x->wait, &wait);
4758 do {
Oleg Nesterov94d3d822008-08-20 16:54:41 -07004759 if (signal_pending_state(state, current)) {
Oleg Nesterovea71a542008-06-20 18:32:20 +04004760 timeout = -ERESTARTSYS;
4761 break;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004762 }
4763 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004765 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 spin_lock_irq(&x->wait.lock);
Oleg Nesterovea71a542008-06-20 18:32:20 +04004767 } while (!x->done && timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 __remove_wait_queue(&x->wait, &wait);
Oleg Nesterovea71a542008-06-20 18:32:20 +04004769 if (!x->done)
4770 return timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 }
4772 x->done--;
Oleg Nesterovea71a542008-06-20 18:32:20 +04004773 return timeout ?: 1;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004774}
4775
4776static long __sched
4777wait_for_common(struct completion *x, long timeout, int state)
4778{
4779 might_sleep();
4780
4781 spin_lock_irq(&x->wait.lock);
4782 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004784 return timeout;
4785}
4786
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004787/**
4788 * wait_for_completion: - waits for completion of a task
4789 * @x: holds the state of this particular completion
4790 *
4791 * This waits to be signaled for completion of a specific task. It is NOT
4792 * interruptible and there is no timeout.
4793 *
4794 * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
4795 * and interrupt capability. Also see complete().
4796 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004797void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004798{
4799 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800}
4801EXPORT_SYMBOL(wait_for_completion);
4802
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004803/**
4804 * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
4805 * @x: holds the state of this particular completion
4806 * @timeout: timeout value in jiffies
4807 *
4808 * This waits for either a completion of a specific task to be signaled or for a
4809 * specified timeout to expire. The timeout is in jiffies. It is not
4810 * interruptible.
4811 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004812unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4814{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004815 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816}
4817EXPORT_SYMBOL(wait_for_completion_timeout);
4818
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004819/**
4820 * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
4821 * @x: holds the state of this particular completion
4822 *
4823 * This waits for completion of a specific task to be signaled. It is
4824 * interruptible.
4825 */
Andi Kleen8cbbe862007-10-15 17:00:14 +02004826int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827{
Andi Kleen51e97992007-10-18 21:32:55 +02004828 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4829 if (t == -ERESTARTSYS)
4830 return t;
4831 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832}
4833EXPORT_SYMBOL(wait_for_completion_interruptible);
4834
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004835/**
4836 * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
4837 * @x: holds the state of this particular completion
4838 * @timeout: timeout value in jiffies
4839 *
4840 * This waits for either a completion of a specific task to be signaled or for a
4841 * specified timeout to expire. It is interruptible. The timeout is in jiffies.
4842 */
Ingo Molnarb15136e2007-10-24 18:23:48 +02004843unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844wait_for_completion_interruptible_timeout(struct completion *x,
4845 unsigned long timeout)
4846{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004847 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848}
4849EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4850
Kevin Diggs65eb3dc2008-08-26 10:26:54 +02004851/**
4852 * wait_for_completion_killable: - waits for completion of a task (killable)
4853 * @x: holds the state of this particular completion
4854 *
4855 * This waits to be signaled for completion of a specific task. It can be
4856 * interrupted by a kill signal.
4857 */
Matthew Wilcox009e5772007-12-06 12:29:54 -05004858int __sched wait_for_completion_killable(struct completion *x)
4859{
4860 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4861 if (t == -ERESTARTSYS)
4862 return t;
4863 return 0;
4864}
4865EXPORT_SYMBOL(wait_for_completion_killable);
4866
Dave Chinnerbe4de352008-08-15 00:40:44 -07004867/**
4868 * try_wait_for_completion - try to decrement a completion without blocking
4869 * @x: completion structure
4870 *
4871 * Returns: 0 if a decrement cannot be done without blocking
4872 * 1 if a decrement succeeded.
4873 *
4874 * If a completion is being used as a counting completion,
4875 * attempt to decrement the counter without blocking. This
4876 * enables us to avoid waiting if the resource the completion
4877 * is protecting is not available.
4878 */
4879bool try_wait_for_completion(struct completion *x)
4880{
4881 int ret = 1;
4882
4883 spin_lock_irq(&x->wait.lock);
4884 if (!x->done)
4885 ret = 0;
4886 else
4887 x->done--;
4888 spin_unlock_irq(&x->wait.lock);
4889 return ret;
4890}
4891EXPORT_SYMBOL(try_wait_for_completion);
4892
4893/**
4894 * completion_done - Test to see if a completion has any waiters
4895 * @x: completion structure
4896 *
4897 * Returns: 0 if there are waiters (wait_for_completion() in progress)
4898 * 1 if there are no waiters.
4899 *
4900 */
4901bool completion_done(struct completion *x)
4902{
4903 int ret = 1;
4904
4905 spin_lock_irq(&x->wait.lock);
4906 if (!x->done)
4907 ret = 0;
4908 spin_unlock_irq(&x->wait.lock);
4909 return ret;
4910}
4911EXPORT_SYMBOL(completion_done);
4912
Andi Kleen8cbbe862007-10-15 17:00:14 +02004913static long __sched
4914sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004915{
4916 unsigned long flags;
4917 wait_queue_t wait;
4918
4919 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920
Andi Kleen8cbbe862007-10-15 17:00:14 +02004921 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
Andi Kleen8cbbe862007-10-15 17:00:14 +02004923 spin_lock_irqsave(&q->lock, flags);
4924 __add_wait_queue(q, &wait);
4925 spin_unlock(&q->lock);
4926 timeout = schedule_timeout(timeout);
4927 spin_lock_irq(&q->lock);
4928 __remove_wait_queue(q, &wait);
4929 spin_unlock_irqrestore(&q->lock, flags);
4930
4931 return timeout;
4932}
4933
4934void __sched interruptible_sleep_on(wait_queue_head_t *q)
4935{
4936 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938EXPORT_SYMBOL(interruptible_sleep_on);
4939
Ingo Molnar0fec1712007-07-09 18:52:01 +02004940long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004941interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004943 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4946
Ingo Molnar0fec1712007-07-09 18:52:01 +02004947void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004949 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951EXPORT_SYMBOL(sleep_on);
4952
Ingo Molnar0fec1712007-07-09 18:52:01 +02004953long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004955 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957EXPORT_SYMBOL(sleep_on_timeout);
4958
Ingo Molnarb29739f2006-06-27 02:54:51 -07004959#ifdef CONFIG_RT_MUTEXES
4960
4961/*
4962 * rt_mutex_setprio - set the current priority of a task
4963 * @p: task
4964 * @prio: prio value (kernel-internal form)
4965 *
4966 * This function changes the 'effective' priority of a task. It does
4967 * not touch ->normal_prio like __setscheduler().
4968 *
4969 * Used by the rt_mutex code to implement priority inheritance logic.
4970 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004971void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004972{
4973 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004974 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004975 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01004976 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004977
4978 BUG_ON(prio < 0 || prio > MAX_PRIO);
4979
4980 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004981 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004982
Andrew Mortond5f9f942007-05-08 20:27:06 -07004983 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004984 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004985 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004986 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004987 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004988 if (running)
4989 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004990
4991 if (rt_prio(prio))
4992 p->sched_class = &rt_sched_class;
4993 else
4994 p->sched_class = &fair_sched_class;
4995
Ingo Molnarb29739f2006-06-27 02:54:51 -07004996 p->prio = prio;
4997
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004998 if (running)
4999 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005000 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02005001 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005002
5003 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005004 }
5005 task_rq_unlock(rq, &flags);
5006}
5007
5008#endif
5009
Ingo Molnar36c8b582006-07-03 00:25:41 -07005010void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011{
Ingo Molnardd41f592007-07-09 18:51:59 +02005012 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005014 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015
5016 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
5017 return;
5018 /*
5019 * We have to be careful, if called from sys_setpriority(),
5020 * the task might be in the middle of scheduling on another CPU.
5021 */
5022 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005023 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 /*
5025 * The RT priorities are set via sched_setscheduler(), but we still
5026 * allow the 'normal' nice value to be set - but as expected
5027 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02005028 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 */
Ingo Molnare05606d2007-07-09 18:51:59 +02005030 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 p->static_prio = NICE_TO_PRIO(nice);
5032 goto out_unlock;
5033 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005034 on_rq = p->se.on_rq;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02005035 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02005036 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07005039 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005040 old_prio = p->prio;
5041 p->prio = effective_prio(p);
5042 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043
Ingo Molnardd41f592007-07-09 18:51:59 +02005044 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02005045 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07005047 * If the task increased its priority or is running and
5048 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07005050 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 resched_task(rq->curr);
5052 }
5053out_unlock:
5054 task_rq_unlock(rq, &flags);
5055}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056EXPORT_SYMBOL(set_user_nice);
5057
Matt Mackalle43379f2005-05-01 08:59:00 -07005058/*
5059 * can_nice - check if a task can reduce its nice value
5060 * @p: task
5061 * @nice: nice value
5062 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005063int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07005064{
Matt Mackall024f4742005-08-18 11:24:19 -07005065 /* convert nice value [19,-20] to rlimit style value [1,40] */
5066 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005067
Matt Mackalle43379f2005-05-01 08:59:00 -07005068 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
5069 capable(CAP_SYS_NICE));
5070}
5071
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072#ifdef __ARCH_WANT_SYS_NICE
5073
5074/*
5075 * sys_nice - change the priority of the current process.
5076 * @increment: priority increment
5077 *
5078 * sys_setpriority is a more generic, but much slower function that
5079 * does similar things.
5080 */
5081asmlinkage long sys_nice(int increment)
5082{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005083 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084
5085 /*
5086 * Setpriority might change our priority at the same moment.
5087 * We don't have to worry. Conceptually one call occurs first
5088 * and we have a single winner.
5089 */
Matt Mackalle43379f2005-05-01 08:59:00 -07005090 if (increment < -40)
5091 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 if (increment > 40)
5093 increment = 40;
5094
5095 nice = PRIO_TO_NICE(current->static_prio) + increment;
5096 if (nice < -20)
5097 nice = -20;
5098 if (nice > 19)
5099 nice = 19;
5100
Matt Mackalle43379f2005-05-01 08:59:00 -07005101 if (increment < 0 && !can_nice(current, nice))
5102 return -EPERM;
5103
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 retval = security_task_setnice(current, nice);
5105 if (retval)
5106 return retval;
5107
5108 set_user_nice(current, nice);
5109 return 0;
5110}
5111
5112#endif
5113
5114/**
5115 * task_prio - return the priority value of a given task.
5116 * @p: the task in question.
5117 *
5118 * This is the priority value as seen by users in /proc.
5119 * RT tasks are offset by -200. Normal tasks are centered
5120 * around 0, value goes from -16 to +15.
5121 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005122int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123{
5124 return p->prio - MAX_RT_PRIO;
5125}
5126
5127/**
5128 * task_nice - return the nice value of a given task.
5129 * @p: the task in question.
5130 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005131int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132{
5133 return TASK_NICE(p);
5134}
Pavel Roskin150d8be2008-03-05 16:56:37 -05005135EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
5137/**
5138 * idle_cpu - is a given cpu idle currently?
5139 * @cpu: the processor in question.
5140 */
5141int idle_cpu(int cpu)
5142{
5143 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
5144}
5145
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146/**
5147 * idle_task - return the idle task for a given cpu.
5148 * @cpu: the processor in question.
5149 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005150struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151{
5152 return cpu_rq(cpu)->idle;
5153}
5154
5155/**
5156 * find_process_by_pid - find a process with a matching PID value.
5157 * @pid: the pid in question.
5158 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02005159static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07005161 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162}
5163
5164/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02005165static void
5166__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167{
Ingo Molnardd41f592007-07-09 18:51:59 +02005168 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005169
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02005171 switch (p->policy) {
5172 case SCHED_NORMAL:
5173 case SCHED_BATCH:
5174 case SCHED_IDLE:
5175 p->sched_class = &fair_sched_class;
5176 break;
5177 case SCHED_FIFO:
5178 case SCHED_RR:
5179 p->sched_class = &rt_sched_class;
5180 break;
5181 }
5182
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005184 p->normal_prio = normal_prio(p);
5185 /* we are holding p->pi_lock already */
5186 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07005187 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188}
5189
Rusty Russell961ccdd2008-06-23 13:55:38 +10005190static int __sched_setscheduler(struct task_struct *p, int policy,
5191 struct sched_param *param, bool user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02005193 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01005195 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005196 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197
Steven Rostedt66e53932006-06-27 02:54:44 -07005198 /* may grab non-irq protected spin_locks */
5199 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200recheck:
5201 /* double check policy once rq lock held */
5202 if (policy < 0)
5203 policy = oldpolicy = p->policy;
5204 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02005205 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
5206 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08005207 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 /*
5209 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02005210 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5211 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 */
5213 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005214 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04005215 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02005217 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 return -EINVAL;
5219
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005220 /*
5221 * Allow unprivileged RT tasks to decrease priority:
5222 */
Rusty Russell961ccdd2008-06-23 13:55:38 +10005223 if (user && !capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02005224 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005225 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005226
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005227 if (!lock_task_sighand(p, &flags))
5228 return -ESRCH;
5229 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
5230 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005231
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005232 /* can't set/change the rt policy */
5233 if (policy != p->policy && !rlim_rtprio)
5234 return -EPERM;
5235
5236 /* can't increase priority */
5237 if (param->sched_priority > p->rt_priority &&
5238 param->sched_priority > rlim_rtprio)
5239 return -EPERM;
5240 }
Ingo Molnardd41f592007-07-09 18:51:59 +02005241 /*
5242 * Like positive nice levels, dont allow tasks to
5243 * move out of SCHED_IDLE either:
5244 */
5245 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
5246 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005247
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005248 /* can't change other user's priorities */
5249 if ((current->euid != p->euid) &&
5250 (current->euid != p->uid))
5251 return -EPERM;
5252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005254 if (user) {
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005255#ifdef CONFIG_RT_GROUP_SCHED
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005256 /*
5257 * Do not allow realtime tasks into groups that have no runtime
5258 * assigned.
5259 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02005260 if (rt_bandwidth_enabled() && rt_policy(policy) &&
5261 task_group(p)->rt_bandwidth.rt_runtime == 0)
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005262 return -EPERM;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01005263#endif
5264
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005265 retval = security_task_setscheduler(p, policy, param);
5266 if (retval)
5267 return retval;
5268 }
5269
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07005271 * make sure no PI-waiters arrive (or leave) while we are
5272 * changing the priority of the task:
5273 */
5274 spin_lock_irqsave(&p->pi_lock, flags);
5275 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 * To be able to change p->policy safely, the apropriate
5277 * runqueue lock must be held.
5278 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07005279 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 /* recheck policy now with rq lock held */
5281 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
5282 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07005283 __task_rq_unlock(rq);
5284 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 goto recheck;
5286 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02005287 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005288 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005289 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005290 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005291 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005292 if (running)
5293 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005294
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02005296 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005297
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005298 if (running)
5299 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02005300 if (on_rq) {
5301 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01005302
5303 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07005305 __task_rq_unlock(rq);
5306 spin_unlock_irqrestore(&p->pi_lock, flags);
5307
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07005308 rt_mutex_adjust_pi(p);
5309
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 return 0;
5311}
Rusty Russell961ccdd2008-06-23 13:55:38 +10005312
5313/**
5314 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
5315 * @p: the task in question.
5316 * @policy: new policy.
5317 * @param: structure containing the new RT priority.
5318 *
5319 * NOTE that the task may be already dead.
5320 */
5321int sched_setscheduler(struct task_struct *p, int policy,
5322 struct sched_param *param)
5323{
5324 return __sched_setscheduler(p, policy, param, true);
5325}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326EXPORT_SYMBOL_GPL(sched_setscheduler);
5327
Rusty Russell961ccdd2008-06-23 13:55:38 +10005328/**
5329 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
5330 * @p: the task in question.
5331 * @policy: new policy.
5332 * @param: structure containing the new RT priority.
5333 *
5334 * Just like sched_setscheduler, only don't bother checking if the
5335 * current context has permission. For example, this is needed in
5336 * stop_machine(): we create temporary high priority worker threads,
5337 * but our caller might not have that capability.
5338 */
5339int sched_setscheduler_nocheck(struct task_struct *p, int policy,
5340 struct sched_param *param)
5341{
5342 return __sched_setscheduler(p, policy, param, false);
5343}
5344
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005345static int
5346do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 struct sched_param lparam;
5349 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005350 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351
5352 if (!param || pid < 0)
5353 return -EINVAL;
5354 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
5355 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005356
5357 rcu_read_lock();
5358 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005360 if (p != NULL)
5361 retval = sched_setscheduler(p, policy, &lparam);
5362 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07005363
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 return retval;
5365}
5366
5367/**
5368 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5369 * @pid: the pid in question.
5370 * @policy: new policy.
5371 * @param: structure containing the new RT priority.
5372 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005373asmlinkage long
5374sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375{
Jason Baronc21761f2006-01-18 17:43:03 -08005376 /* negative values for policy are not valid */
5377 if (policy < 0)
5378 return -EINVAL;
5379
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 return do_sched_setscheduler(pid, policy, param);
5381}
5382
5383/**
5384 * sys_sched_setparam - set/change the RT priority of a thread
5385 * @pid: the pid in question.
5386 * @param: structure containing the new RT priority.
5387 */
5388asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
5389{
5390 return do_sched_setscheduler(pid, -1, param);
5391}
5392
5393/**
5394 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5395 * @pid: the pid in question.
5396 */
5397asmlinkage long sys_sched_getscheduler(pid_t pid)
5398{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005399 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005400 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401
5402 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005403 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404
5405 retval = -ESRCH;
5406 read_lock(&tasklist_lock);
5407 p = find_process_by_pid(pid);
5408 if (p) {
5409 retval = security_task_getscheduler(p);
5410 if (!retval)
5411 retval = p->policy;
5412 }
5413 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 return retval;
5415}
5416
5417/**
5418 * sys_sched_getscheduler - get the RT priority of a thread
5419 * @pid: the pid in question.
5420 * @param: structure containing the RT priority.
5421 */
5422asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
5423{
5424 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005425 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005426 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427
5428 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005429 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430
5431 read_lock(&tasklist_lock);
5432 p = find_process_by_pid(pid);
5433 retval = -ESRCH;
5434 if (!p)
5435 goto out_unlock;
5436
5437 retval = security_task_getscheduler(p);
5438 if (retval)
5439 goto out_unlock;
5440
5441 lp.sched_priority = p->rt_priority;
5442 read_unlock(&tasklist_lock);
5443
5444 /*
5445 * This one might sleep, we cannot do it with a spinlock held ...
5446 */
5447 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
5448
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 return retval;
5450
5451out_unlock:
5452 read_unlock(&tasklist_lock);
5453 return retval;
5454}
5455
Rusty Russell96f874e22008-11-25 02:35:14 +10305456long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305458 cpumask_var_t cpus_allowed, new_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005459 struct task_struct *p;
5460 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005462 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 read_lock(&tasklist_lock);
5464
5465 p = find_process_by_pid(pid);
5466 if (!p) {
5467 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005468 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 return -ESRCH;
5470 }
5471
5472 /*
5473 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005474 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 * usage count and then drop tasklist_lock.
5476 */
5477 get_task_struct(p);
5478 read_unlock(&tasklist_lock);
5479
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305480 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
5481 retval = -ENOMEM;
5482 goto out_put_task;
5483 }
5484 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
5485 retval = -ENOMEM;
5486 goto out_free_cpus_allowed;
5487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 retval = -EPERM;
5489 if ((current->euid != p->euid) && (current->euid != p->uid) &&
5490 !capable(CAP_SYS_NICE))
5491 goto out_unlock;
5492
David Quigleye7834f82006-06-23 02:03:59 -07005493 retval = security_task_setscheduler(p, 0, NULL);
5494 if (retval)
5495 goto out_unlock;
5496
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305497 cpuset_cpus_allowed(p, cpus_allowed);
5498 cpumask_and(new_mask, in_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005499 again:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305500 retval = set_cpus_allowed_ptr(p, new_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501
Paul Menage8707d8b2007-10-18 23:40:22 -07005502 if (!retval) {
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305503 cpuset_cpus_allowed(p, cpus_allowed);
5504 if (!cpumask_subset(new_mask, cpus_allowed)) {
Paul Menage8707d8b2007-10-18 23:40:22 -07005505 /*
5506 * We must have raced with a concurrent cpuset
5507 * update. Just reset the cpus_allowed to the
5508 * cpuset's cpus_allowed
5509 */
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305510 cpumask_copy(new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005511 goto again;
5512 }
5513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514out_unlock:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305515 free_cpumask_var(new_mask);
5516out_free_cpus_allowed:
5517 free_cpumask_var(cpus_allowed);
5518out_put_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005520 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 return retval;
5522}
5523
5524static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
Rusty Russell96f874e22008-11-25 02:35:14 +10305525 struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526{
Rusty Russell96f874e22008-11-25 02:35:14 +10305527 if (len < cpumask_size())
5528 cpumask_clear(new_mask);
5529 else if (len > cpumask_size())
5530 len = cpumask_size();
5531
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5533}
5534
5535/**
5536 * sys_sched_setaffinity - set the cpu affinity of a process
5537 * @pid: pid of the process
5538 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5539 * @user_mask_ptr: user-space pointer to the new cpu mask
5540 */
5541asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
5542 unsigned long __user *user_mask_ptr)
5543{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305544 cpumask_var_t new_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 int retval;
5546
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305547 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
5548 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305550 retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
5551 if (retval == 0)
5552 retval = sched_setaffinity(pid, new_mask);
5553 free_cpumask_var(new_mask);
5554 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555}
5556
Rusty Russell96f874e22008-11-25 02:35:14 +10305557long sched_getaffinity(pid_t pid, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005559 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005562 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 read_lock(&tasklist_lock);
5564
5565 retval = -ESRCH;
5566 p = find_process_by_pid(pid);
5567 if (!p)
5568 goto out_unlock;
5569
David Quigleye7834f82006-06-23 02:03:59 -07005570 retval = security_task_getscheduler(p);
5571 if (retval)
5572 goto out_unlock;
5573
Rusty Russell96f874e22008-11-25 02:35:14 +10305574 cpumask_and(mask, &p->cpus_allowed, cpu_online_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575
5576out_unlock:
5577 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01005578 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579
Ulrich Drepper9531b622007-08-09 11:16:46 +02005580 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581}
5582
5583/**
5584 * sys_sched_getaffinity - get the cpu affinity of a process
5585 * @pid: pid of the process
5586 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
5587 * @user_mask_ptr: user-space pointer to hold the current cpu mask
5588 */
5589asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
5590 unsigned long __user *user_mask_ptr)
5591{
5592 int ret;
Rusty Russellf17c8602008-11-25 02:35:11 +10305593 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594
Rusty Russellf17c8602008-11-25 02:35:11 +10305595 if (len < cpumask_size())
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 return -EINVAL;
5597
Rusty Russellf17c8602008-11-25 02:35:11 +10305598 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
5599 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600
Rusty Russellf17c8602008-11-25 02:35:11 +10305601 ret = sched_getaffinity(pid, mask);
5602 if (ret == 0) {
5603 if (copy_to_user(user_mask_ptr, mask, cpumask_size()))
5604 ret = -EFAULT;
5605 else
5606 ret = cpumask_size();
5607 }
5608 free_cpumask_var(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609
Rusty Russellf17c8602008-11-25 02:35:11 +10305610 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611}
5612
5613/**
5614 * sys_sched_yield - yield the current processor to other threads.
5615 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005616 * This function yields the current CPU to other tasks. If there are no
5617 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 */
5619asmlinkage long sys_sched_yield(void)
5620{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005621 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622
Ingo Molnar2d723762007-10-15 17:00:12 +02005623 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005624 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625
5626 /*
5627 * Since we are going to call schedule() anyway, there's
5628 * no need to preempt or enable interrupts:
5629 */
5630 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005631 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 _raw_spin_unlock(&rq->lock);
5633 preempt_enable_no_resched();
5634
5635 schedule();
5636
5637 return 0;
5638}
5639
Andrew Mortone7b38402006-06-30 01:56:00 -07005640static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07005642#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5643 __might_sleep(__FILE__, __LINE__);
5644#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005645 /*
5646 * The BKS might be reacquired before we have dropped
5647 * PREEMPT_ACTIVE, which could trigger a second
5648 * cond_resched() call.
5649 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 do {
5651 add_preempt_count(PREEMPT_ACTIVE);
5652 schedule();
5653 sub_preempt_count(PREEMPT_ACTIVE);
5654 } while (need_resched());
5655}
5656
Herbert Xu02b67cc32008-01-25 21:08:28 +01005657int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658{
Ingo Molnar94142322006-12-29 16:48:13 -08005659 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5660 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 __cond_resched();
5662 return 1;
5663 }
5664 return 0;
5665}
Herbert Xu02b67cc32008-01-25 21:08:28 +01005666EXPORT_SYMBOL(_cond_resched);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667
5668/*
5669 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5670 * call schedule, and on return reacquire the lock.
5671 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005672 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 * operations here to prevent schedule() from being called twice (once via
5674 * spin_unlock(), once by hand).
5675 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005676int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677{
Nick Piggin95c354f2008-01-30 13:31:20 +01005678 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005679 int ret = 0;
5680
Nick Piggin95c354f2008-01-30 13:31:20 +01005681 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005683 if (resched && need_resched())
5684 __cond_resched();
5685 else
5686 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005687 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005690 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692EXPORT_SYMBOL(cond_resched_lock);
5693
5694int __sched cond_resched_softirq(void)
5695{
5696 BUG_ON(!in_softirq());
5697
Ingo Molnar94142322006-12-29 16:48:13 -08005698 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07005699 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 __cond_resched();
5701 local_bh_disable();
5702 return 1;
5703 }
5704 return 0;
5705}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706EXPORT_SYMBOL(cond_resched_softirq);
5707
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708/**
5709 * yield - yield the current processor to other threads.
5710 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005711 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 * thread runnable and calls sys_sched_yield().
5713 */
5714void __sched yield(void)
5715{
5716 set_current_state(TASK_RUNNING);
5717 sys_sched_yield();
5718}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719EXPORT_SYMBOL(yield);
5720
5721/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005722 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 * that process accounting knows that this is a task in IO wait state.
5724 *
5725 * But don't do that if it is a deliberate, throttling IO wait (this task
5726 * has set its backing_dev_info: the queue against which it should throttle)
5727 */
5728void __sched io_schedule(void)
5729{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005730 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005732 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 atomic_inc(&rq->nr_iowait);
5734 schedule();
5735 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005736 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738EXPORT_SYMBOL(io_schedule);
5739
5740long __sched io_schedule_timeout(long timeout)
5741{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005742 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 long ret;
5744
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005745 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 atomic_inc(&rq->nr_iowait);
5747 ret = schedule_timeout(timeout);
5748 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005749 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 return ret;
5751}
5752
5753/**
5754 * sys_sched_get_priority_max - return maximum RT priority.
5755 * @policy: scheduling class.
5756 *
5757 * this syscall returns the maximum rt_priority that can be used
5758 * by a given scheduling class.
5759 */
5760asmlinkage long sys_sched_get_priority_max(int policy)
5761{
5762 int ret = -EINVAL;
5763
5764 switch (policy) {
5765 case SCHED_FIFO:
5766 case SCHED_RR:
5767 ret = MAX_USER_RT_PRIO-1;
5768 break;
5769 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005770 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005771 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 ret = 0;
5773 break;
5774 }
5775 return ret;
5776}
5777
5778/**
5779 * sys_sched_get_priority_min - return minimum RT priority.
5780 * @policy: scheduling class.
5781 *
5782 * this syscall returns the minimum rt_priority that can be used
5783 * by a given scheduling class.
5784 */
5785asmlinkage long sys_sched_get_priority_min(int policy)
5786{
5787 int ret = -EINVAL;
5788
5789 switch (policy) {
5790 case SCHED_FIFO:
5791 case SCHED_RR:
5792 ret = 1;
5793 break;
5794 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005795 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005796 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 ret = 0;
5798 }
5799 return ret;
5800}
5801
5802/**
5803 * sys_sched_rr_get_interval - return the default timeslice of a process.
5804 * @pid: pid of the process.
5805 * @interval: userspace pointer to the timeslice value.
5806 *
5807 * this syscall writes the default timeslice value of a given process
5808 * into the user-space timespec buffer. A value of '0' means infinity.
5809 */
5810asmlinkage
5811long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5812{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005813 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005814 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005815 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817
5818 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005819 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820
5821 retval = -ESRCH;
5822 read_lock(&tasklist_lock);
5823 p = find_process_by_pid(pid);
5824 if (!p)
5825 goto out_unlock;
5826
5827 retval = security_task_getscheduler(p);
5828 if (retval)
5829 goto out_unlock;
5830
Ingo Molnar77034932007-12-04 17:04:39 +01005831 /*
5832 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5833 * tasks that are on an otherwise idle runqueue:
5834 */
5835 time_slice = 0;
5836 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005837 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005838 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005839 struct sched_entity *se = &p->se;
5840 unsigned long flags;
5841 struct rq *rq;
5842
5843 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005844 if (rq->cfs.load.weight)
5845 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005846 task_rq_unlock(rq, &flags);
5847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005849 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005852
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853out_unlock:
5854 read_unlock(&tasklist_lock);
5855 return retval;
5856}
5857
Steven Rostedt7c731e02008-05-12 21:20:41 +02005858static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005859
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005860void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005863 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005866 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005867 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005868#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005870 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005872 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873#else
5874 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005875 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005877 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878#endif
5879#ifdef CONFIG_DEBUG_STACK_USAGE
5880 {
Al Viro10ebffd2005-11-13 16:06:56 -08005881 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 while (!*n)
5883 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005884 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 }
5886#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005887 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005888 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005890 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891}
5892
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005893void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005895 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896
Ingo Molnar4bd77322007-07-11 21:21:47 +02005897#if BITS_PER_LONG == 32
5898 printk(KERN_INFO
5899 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005901 printk(KERN_INFO
5902 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903#endif
5904 read_lock(&tasklist_lock);
5905 do_each_thread(g, p) {
5906 /*
5907 * reset the NMI-timeout, listing all files on a slow
5908 * console might take alot of time:
5909 */
5910 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005911 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005912 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 } while_each_thread(g, p);
5914
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005915 touch_all_softlockup_watchdogs();
5916
Ingo Molnardd41f592007-07-09 18:51:59 +02005917#ifdef CONFIG_SCHED_DEBUG
5918 sysrq_sched_debug_show();
5919#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005921 /*
5922 * Only show locks if all tasks are dumped:
5923 */
5924 if (state_filter == -1)
5925 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926}
5927
Ingo Molnar1df21052007-07-09 18:51:58 +02005928void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5929{
Ingo Molnardd41f592007-07-09 18:51:59 +02005930 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005931}
5932
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005933/**
5934 * init_idle - set up an idle thread for a given CPU
5935 * @idle: task in question
5936 * @cpu: cpu the idle task belongs to
5937 *
5938 * NOTE: this function does not set the idle thread's NEED_RESCHED
5939 * flag, to make booting more robust.
5940 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005941void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005943 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 unsigned long flags;
5945
Ingo Molnar5cbd54e2008-11-12 20:05:50 +01005946 spin_lock_irqsave(&rq->lock, flags);
5947
Ingo Molnardd41f592007-07-09 18:51:59 +02005948 __sched_fork(idle);
5949 idle->se.exec_start = sched_clock();
5950
Ingo Molnarb29739f2006-06-27 02:54:51 -07005951 idle->prio = idle->normal_prio = MAX_PRIO;
Rusty Russell96f874e22008-11-25 02:35:14 +10305952 cpumask_copy(&idle->cpus_allowed, cpumask_of(cpu));
Ingo Molnardd41f592007-07-09 18:51:59 +02005953 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005956#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5957 idle->oncpu = 1;
5958#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 spin_unlock_irqrestore(&rq->lock, flags);
5960
5961 /* Set the preempt count _outside_ the spinlocks! */
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005962#if defined(CONFIG_PREEMPT)
5963 task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
5964#else
Al Viroa1261f542005-11-13 16:06:55 -08005965 task_thread_info(idle)->preempt_count = 0;
Linus Torvalds8e3e0762008-05-10 20:58:02 -07005966#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02005967 /*
5968 * The idle tasks have their own, simple scheduling class:
5969 */
5970 idle->sched_class = &idle_sched_class;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005971 ftrace_graph_init_task(idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972}
5973
5974/*
5975 * In a system that switches off the HZ timer nohz_cpu_mask
5976 * indicates which cpus entered this state. This is used
5977 * in the rcu update to wait only for active cpus. For system
5978 * which do not switch off the HZ timer nohz_cpu_mask should
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10305979 * always be CPU_BITS_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 */
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10305981cpumask_var_t nohz_cpu_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982
Ingo Molnar19978ca2007-11-09 22:39:38 +01005983/*
5984 * Increase the granularity value when there are more CPUs,
5985 * because with more CPUs the 'effective latency' as visible
5986 * to users decreases. But the relationship is not linear,
5987 * so pick a second-best guess by going with the log2 of the
5988 * number of CPUs.
5989 *
5990 * This idea comes from the SD scheduler of Con Kolivas:
5991 */
5992static inline void sched_init_granularity(void)
5993{
5994 unsigned int factor = 1 + ilog2(num_online_cpus());
5995 const unsigned long limit = 200000000;
5996
5997 sysctl_sched_min_granularity *= factor;
5998 if (sysctl_sched_min_granularity > limit)
5999 sysctl_sched_min_granularity = limit;
6000
6001 sysctl_sched_latency *= factor;
6002 if (sysctl_sched_latency > limit)
6003 sysctl_sched_latency = limit;
6004
6005 sysctl_sched_wakeup_granularity *= factor;
Peter Zijlstra55cd5342008-08-04 08:54:26 +02006006
6007 sysctl_sched_shares_ratelimit *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01006008}
6009
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010#ifdef CONFIG_SMP
6011/*
6012 * This is how migration works:
6013 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07006014 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 * runqueue and wake up that CPU's migration thread.
6016 * 2) we down() the locked semaphore => thread blocks.
6017 * 3) migration thread wakes up (implicitly it forces the migrated
6018 * thread off the CPU)
6019 * 4) it gets the migration request and checks whether the migrated
6020 * task is still in the wrong runqueue.
6021 * 5) if it's in the wrong runqueue then the migration thread removes
6022 * it and puts it into the right queue.
6023 * 6) migration thread up()s the semaphore.
6024 * 7) we wake up and the migration is done.
6025 */
6026
6027/*
6028 * Change a given task's CPU affinity. Migrate the thread to a
6029 * proper CPU and schedule it away if the CPU it's executing on
6030 * is removed from the allowed bitmask.
6031 *
6032 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006033 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 * call is not atomic; no spinlocks may be held.
6035 */
Rusty Russell96f874e22008-11-25 02:35:14 +10306036int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006038 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006040 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006041 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042
6043 rq = task_rq_lock(p, &flags);
Rusty Russell96f874e22008-11-25 02:35:14 +10306044 if (!cpumask_intersects(new_mask, cpu_online_mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 ret = -EINVAL;
6046 goto out;
6047 }
6048
David Rientjes9985b0b2008-06-05 12:57:11 -07006049 if (unlikely((p->flags & PF_THREAD_BOUND) && p != current &&
Rusty Russell96f874e22008-11-25 02:35:14 +10306050 !cpumask_equal(&p->cpus_allowed, new_mask))) {
David Rientjes9985b0b2008-06-05 12:57:11 -07006051 ret = -EINVAL;
6052 goto out;
6053 }
6054
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006055 if (p->sched_class->set_cpus_allowed)
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006056 p->sched_class->set_cpus_allowed(p, new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006057 else {
Rusty Russell96f874e22008-11-25 02:35:14 +10306058 cpumask_copy(&p->cpus_allowed, new_mask);
6059 p->rt.nr_cpus_allowed = cpumask_weight(new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01006060 }
6061
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 /* Can the task run on the task's current CPU? If so, we're done */
Rusty Russell96f874e22008-11-25 02:35:14 +10306063 if (cpumask_test_cpu(task_cpu(p), new_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 goto out;
6065
Rusty Russell1e5ce4f2008-11-25 02:35:03 +10306066 if (migrate_task(p, cpumask_any_and(cpu_online_mask, new_mask), &req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 /* Need help from migration thread: drop lock and wait. */
6068 task_rq_unlock(rq, &flags);
6069 wake_up_process(rq->migration_thread);
6070 wait_for_completion(&req.done);
6071 tlb_migrate_finish(p->mm);
6072 return 0;
6073 }
6074out:
6075 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006076
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077 return ret;
6078}
Mike Traviscd8ba7c2008-03-26 14:23:49 -07006079EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080
6081/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006082 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 * this because either it can't run here any more (set_cpus_allowed()
6084 * away from this CPU, or CPU going down), or because we're
6085 * attempting to rebalance this task on exec (sched_exec).
6086 *
6087 * So we race with normal scheduler movements, but that's OK, as long
6088 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07006089 *
6090 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07006092static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006094 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02006095 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096
Max Krasnyanskye761b772008-07-15 04:43:49 -07006097 if (unlikely(!cpu_active(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07006098 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099
6100 rq_src = cpu_rq(src_cpu);
6101 rq_dest = cpu_rq(dest_cpu);
6102
6103 double_rq_lock(rq_src, rq_dest);
6104 /* Already moved. */
6105 if (task_cpu(p) != src_cpu)
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006106 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 /* Affinity changed (again). */
Rusty Russell96f874e22008-11-25 02:35:14 +10306108 if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006109 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110
Ingo Molnardd41f592007-07-09 18:51:59 +02006111 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006112 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006113 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02006114
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006116 if (on_rq) {
6117 activate_task(rq_dest, p, 0);
Peter Zijlstra15afe092008-09-20 23:38:02 +02006118 check_preempt_curr(rq_dest, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 }
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006120done:
Kirill Korotaevefc30812006-06-27 02:54:32 -07006121 ret = 1;
Linus Torvaldsb1e38732008-07-10 11:25:03 -07006122fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07006124 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125}
6126
6127/*
6128 * migration_thread - this is a highprio system thread that performs
6129 * thread migration by bumping thread off CPU then 'pushing' onto
6130 * another runqueue.
6131 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07006132static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006135 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136
6137 rq = cpu_rq(cpu);
6138 BUG_ON(rq->migration_thread != current);
6139
6140 set_current_state(TASK_INTERRUPTIBLE);
6141 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006142 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 spin_lock_irq(&rq->lock);
6146
6147 if (cpu_is_offline(cpu)) {
6148 spin_unlock_irq(&rq->lock);
6149 goto wait_to_die;
6150 }
6151
6152 if (rq->active_balance) {
6153 active_load_balance(rq, cpu);
6154 rq->active_balance = 0;
6155 }
6156
6157 head = &rq->migration_queue;
6158
6159 if (list_empty(head)) {
6160 spin_unlock_irq(&rq->lock);
6161 schedule();
6162 set_current_state(TASK_INTERRUPTIBLE);
6163 continue;
6164 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07006165 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 list_del_init(head->next);
6167
Nick Piggin674311d2005-06-25 14:57:27 -07006168 spin_unlock(&rq->lock);
6169 __migrate_task(req->task, cpu, req->dest_cpu);
6170 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171
6172 complete(&req->done);
6173 }
6174 __set_current_state(TASK_RUNNING);
6175 return 0;
6176
6177wait_to_die:
6178 /* Wait for kthread_stop */
6179 set_current_state(TASK_INTERRUPTIBLE);
6180 while (!kthread_should_stop()) {
6181 schedule();
6182 set_current_state(TASK_INTERRUPTIBLE);
6183 }
6184 __set_current_state(TASK_RUNNING);
6185 return 0;
6186}
6187
6188#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006189
6190static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
6191{
6192 int ret;
6193
6194 local_irq_disable();
6195 ret = __migrate_task(p, src_cpu, dest_cpu);
6196 local_irq_enable();
6197 return ret;
6198}
6199
Kirill Korotaev054b9102006-12-10 02:20:11 -08006200/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006201 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08006202 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006203static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006205 int dest_cpu;
Rusty Russelle76bd8d2008-11-25 02:35:11 +10306206 /* FIXME: Use cpumask_of_node here. */
6207 cpumask_t _nodemask = node_to_cpumask(cpu_to_node(dead_cpu));
6208 const struct cpumask *nodemask = &_nodemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209
Rusty Russelle76bd8d2008-11-25 02:35:11 +10306210again:
6211 /* Look for allowed, online CPU in same node. */
6212 for_each_cpu_and(dest_cpu, nodemask, cpu_online_mask)
6213 if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
6214 goto move;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215
Rusty Russelle76bd8d2008-11-25 02:35:11 +10306216 /* Any allowed, online CPU? */
6217 dest_cpu = cpumask_any_and(&p->cpus_allowed, cpu_online_mask);
6218 if (dest_cpu < nr_cpu_ids)
6219 goto move;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220
Rusty Russelle76bd8d2008-11-25 02:35:11 +10306221 /* No more Mr. Nice Guy. */
6222 if (dest_cpu >= nr_cpu_ids) {
Rusty Russelle76bd8d2008-11-25 02:35:11 +10306223 cpuset_cpus_allowed_locked(p, &p->cpus_allowed);
6224 dest_cpu = cpumask_any_and(cpu_online_mask, &p->cpus_allowed);
Mike Travisf9a86fc2008-04-04 18:11:07 -07006225
Rusty Russelle76bd8d2008-11-25 02:35:11 +10306226 /*
6227 * Don't tell them about moving exiting tasks or
6228 * kernel threads (both mm NULL), since they never
6229 * leave kernel.
6230 */
6231 if (p->mm && printk_ratelimit()) {
6232 printk(KERN_INFO "process %d (%s) no "
6233 "longer affine to cpu%d\n",
6234 task_pid_nr(p), p->comm, dead_cpu);
Andi Kleen3a5c3592007-10-15 17:00:14 +02006235 }
Rusty Russelle76bd8d2008-11-25 02:35:11 +10306236 }
6237
6238move:
6239 /* It can have affinity changed while we were choosing. */
6240 if (unlikely(!__migrate_task_irq(p, dead_cpu, dest_cpu)))
6241 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242}
6243
6244/*
6245 * While a dead CPU has no uninterruptible tasks queued at this point,
6246 * it might still have a nonzero ->nr_uninterruptible counter, because
6247 * for performance reasons the counter is not stricly tracking tasks to
6248 * their home CPUs. So we just add the counter to another CPU's counter,
6249 * to keep the global sum constant after CPU-down:
6250 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07006251static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252{
Rusty Russell1e5ce4f2008-11-25 02:35:03 +10306253 struct rq *rq_dest = cpu_rq(cpumask_any(cpu_online_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254 unsigned long flags;
6255
6256 local_irq_save(flags);
6257 double_rq_lock(rq_src, rq_dest);
6258 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
6259 rq_src->nr_uninterruptible = 0;
6260 double_rq_unlock(rq_src, rq_dest);
6261 local_irq_restore(flags);
6262}
6263
6264/* Run through task list and migrate tasks from the dead cpu. */
6265static void migrate_live_tasks(int src_cpu)
6266{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006267 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006269 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270
Ingo Molnar48f24c42006-07-03 00:25:40 -07006271 do_each_thread(t, p) {
6272 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 continue;
6274
Ingo Molnar48f24c42006-07-03 00:25:40 -07006275 if (task_cpu(p) == src_cpu)
6276 move_task_off_dead_cpu(src_cpu, p);
6277 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006279 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280}
6281
Ingo Molnardd41f592007-07-09 18:51:59 +02006282/*
6283 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006284 * It does so by boosting its priority to highest possible.
6285 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286 */
6287void sched_idle_next(void)
6288{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006289 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07006290 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 struct task_struct *p = rq->idle;
6292 unsigned long flags;
6293
6294 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006295 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296
Ingo Molnar48f24c42006-07-03 00:25:40 -07006297 /*
6298 * Strictly not necessary since rest of the CPUs are stopped by now
6299 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300 */
6301 spin_lock_irqsave(&rq->lock, flags);
6302
Ingo Molnardd41f592007-07-09 18:51:59 +02006303 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006304
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01006305 update_rq_clock(rq);
6306 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307
6308 spin_unlock_irqrestore(&rq->lock, flags);
6309}
6310
Ingo Molnar48f24c42006-07-03 00:25:40 -07006311/*
6312 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 * offline.
6314 */
6315void idle_task_exit(void)
6316{
6317 struct mm_struct *mm = current->active_mm;
6318
6319 BUG_ON(cpu_online(smp_processor_id()));
6320
6321 if (mm != &init_mm)
6322 switch_mm(mm, &init_mm, current);
6323 mmdrop(mm);
6324}
6325
Kirill Korotaev054b9102006-12-10 02:20:11 -08006326/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006327static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006329 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330
6331 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07006332 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333
6334 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07006335 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336
Ingo Molnar48f24c42006-07-03 00:25:40 -07006337 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338
6339 /*
6340 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006341 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342 * fine.
6343 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006344 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006345 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07006346 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347
Ingo Molnar48f24c42006-07-03 00:25:40 -07006348 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349}
6350
6351/* release_task() removes task from tasklist, so we won't find dead tasks. */
6352static void migrate_dead_tasks(unsigned int dead_cpu)
6353{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006354 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02006355 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356
Ingo Molnardd41f592007-07-09 18:51:59 +02006357 for ( ; ; ) {
6358 if (!rq->nr_running)
6359 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02006360 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02006361 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02006362 if (!next)
6363 break;
Dmitry Adamushko79c53792008-06-29 00:16:56 +02006364 next->sched_class->put_prev_task(rq, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02006365 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02006366
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 }
6368}
6369#endif /* CONFIG_HOTPLUG_CPU */
6370
Nick Piggine692ab52007-07-26 13:40:43 +02006371#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
6372
6373static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006374 {
6375 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006376 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006377 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006378 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006379};
6380
6381static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02006382 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006383 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006384 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006385 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02006386 .child = sd_ctl_dir,
6387 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01006388 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02006389};
6390
6391static struct ctl_table *sd_alloc_ctl_entry(int n)
6392{
6393 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02006394 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02006395
Nick Piggine692ab52007-07-26 13:40:43 +02006396 return entry;
6397}
6398
Milton Miller6382bc92007-10-15 17:00:19 +02006399static void sd_free_ctl_entry(struct ctl_table **tablep)
6400{
Milton Millercd7900762007-10-17 16:55:11 +02006401 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02006402
Milton Millercd7900762007-10-17 16:55:11 +02006403 /*
6404 * In the intermediate directories, both the child directory and
6405 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006406 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02006407 * static strings and all have proc handlers.
6408 */
6409 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02006410 if (entry->child)
6411 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02006412 if (entry->proc_handler == NULL)
6413 kfree(entry->procname);
6414 }
Milton Miller6382bc92007-10-15 17:00:19 +02006415
6416 kfree(*tablep);
6417 *tablep = NULL;
6418}
6419
Nick Piggine692ab52007-07-26 13:40:43 +02006420static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02006421set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02006422 const char *procname, void *data, int maxlen,
6423 mode_t mode, proc_handler *proc_handler)
6424{
Nick Piggine692ab52007-07-26 13:40:43 +02006425 entry->procname = procname;
6426 entry->data = data;
6427 entry->maxlen = maxlen;
6428 entry->mode = mode;
6429 entry->proc_handler = proc_handler;
6430}
6431
6432static struct ctl_table *
6433sd_alloc_ctl_domain_table(struct sched_domain *sd)
6434{
Ingo Molnara5d8c342008-10-09 11:35:51 +02006435 struct ctl_table *table = sd_alloc_ctl_entry(13);
Nick Piggine692ab52007-07-26 13:40:43 +02006436
Milton Millerad1cdc12007-10-15 17:00:19 +02006437 if (table == NULL)
6438 return NULL;
6439
Alexey Dobriyane0361852007-08-09 11:16:46 +02006440 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006441 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006442 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02006443 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006444 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006445 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006446 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006447 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006448 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006449 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006450 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006451 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006452 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02006453 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006454 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02006455 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02006456 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02006457 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006458 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02006459 &sd->cache_nice_tries,
6460 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02006461 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02006462 sizeof(int), 0644, proc_dointvec_minmax);
Ingo Molnara5d8c342008-10-09 11:35:51 +02006463 set_table_entry(&table[11], "name", sd->name,
6464 CORENAME_MAX_SIZE, 0444, proc_dostring);
6465 /* &table[12] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02006466
6467 return table;
6468}
6469
Ingo Molnar9a4e7152007-11-28 15:52:56 +01006470static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02006471{
6472 struct ctl_table *entry, *table;
6473 struct sched_domain *sd;
6474 int domain_num = 0, i;
6475 char buf[32];
6476
6477 for_each_domain(cpu, sd)
6478 domain_num++;
6479 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02006480 if (table == NULL)
6481 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02006482
6483 i = 0;
6484 for_each_domain(cpu, sd) {
6485 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006486 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006487 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006488 entry->child = sd_alloc_ctl_domain_table(sd);
6489 entry++;
6490 i++;
6491 }
6492 return table;
6493}
6494
6495static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02006496static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006497{
6498 int i, cpu_num = num_online_cpus();
6499 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
6500 char buf[32];
6501
Milton Miller73785472007-10-24 18:23:48 +02006502 WARN_ON(sd_ctl_dir[0].child);
6503 sd_ctl_dir[0].child = entry;
6504
Milton Millerad1cdc12007-10-15 17:00:19 +02006505 if (entry == NULL)
6506 return;
6507
Milton Miller97b6ea72007-10-15 17:00:19 +02006508 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02006509 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02006510 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02006511 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02006512 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02006513 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02006514 }
Milton Miller73785472007-10-24 18:23:48 +02006515
6516 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02006517 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
6518}
Milton Miller6382bc92007-10-15 17:00:19 +02006519
Milton Miller73785472007-10-24 18:23:48 +02006520/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02006521static void unregister_sched_domain_sysctl(void)
6522{
Milton Miller73785472007-10-24 18:23:48 +02006523 if (sd_sysctl_header)
6524 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02006525 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02006526 if (sd_ctl_dir[0].child)
6527 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02006528}
Nick Piggine692ab52007-07-26 13:40:43 +02006529#else
Milton Miller6382bc92007-10-15 17:00:19 +02006530static void register_sched_domain_sysctl(void)
6531{
6532}
6533static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02006534{
6535}
6536#endif
6537
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006538static void set_rq_online(struct rq *rq)
6539{
6540 if (!rq->online) {
6541 const struct sched_class *class;
6542
Rusty Russellc6c49272008-11-25 02:35:05 +10306543 cpumask_set_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006544 rq->online = 1;
6545
6546 for_each_class(class) {
6547 if (class->rq_online)
6548 class->rq_online(rq);
6549 }
6550 }
6551}
6552
6553static void set_rq_offline(struct rq *rq)
6554{
6555 if (rq->online) {
6556 const struct sched_class *class;
6557
6558 for_each_class(class) {
6559 if (class->rq_offline)
6560 class->rq_offline(rq);
6561 }
6562
Rusty Russellc6c49272008-11-25 02:35:05 +10306563 cpumask_clear_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006564 rq->online = 0;
6565 }
6566}
6567
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568/*
6569 * migration_call - callback that gets triggered when a CPU is added.
6570 * Here we can start up the necessary migration thread for the new CPU.
6571 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006572static int __cpuinit
6573migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006576 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006578 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579
6580 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006581
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006583 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02006584 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585 if (IS_ERR(p))
6586 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587 kthread_bind(p, cpu);
6588 /* Must be high prio: stop_machine expects to yield to it. */
6589 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02006590 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591 task_rq_unlock(rq, &flags);
6592 cpu_rq(cpu)->migration_thread = p;
6593 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006594
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006596 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02006597 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006599
6600 /* Update our root-domain */
6601 rq = cpu_rq(cpu);
6602 spin_lock_irqsave(&rq->lock, flags);
6603 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10306604 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006605
6606 set_rq_online(rq);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006607 }
6608 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006610
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611#ifdef CONFIG_HOTPLUG_CPU
6612 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006613 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07006614 if (!cpu_rq(cpu)->migration_thread)
6615 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006616 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08006617 kthread_bind(cpu_rq(cpu)->migration_thread,
Rusty Russell1e5ce4f2008-11-25 02:35:03 +10306618 cpumask_any(cpu_online_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619 kthread_stop(cpu_rq(cpu)->migration_thread);
6620 cpu_rq(cpu)->migration_thread = NULL;
6621 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006622
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006624 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07006625 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 migrate_live_tasks(cpu);
6627 rq = cpu_rq(cpu);
6628 kthread_stop(rq->migration_thread);
6629 rq->migration_thread = NULL;
6630 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006631 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02006632 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02006633 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02006635 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
6636 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07006638 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07006639 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640 migrate_nr_uninterruptible(rq);
6641 BUG_ON(rq->nr_running != 0);
6642
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006643 /*
6644 * No need to migrate the tasks: it was best-effort if
6645 * they didn't take sched_hotcpu_mutex. Just wake up
6646 * the requestors.
6647 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648 spin_lock_irq(&rq->lock);
6649 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006650 struct migration_req *req;
6651
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07006653 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 list_del_init(&req->list);
Brian King9a2bd242008-12-09 08:47:00 -06006655 spin_unlock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656 complete(&req->done);
Brian King9a2bd242008-12-09 08:47:00 -06006657 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658 }
6659 spin_unlock_irq(&rq->lock);
6660 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006661
Gregory Haskins08f503b2008-03-10 17:59:11 -04006662 case CPU_DYING:
6663 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01006664 /* Update our root-domain */
6665 rq = cpu_rq(cpu);
6666 spin_lock_irqsave(&rq->lock, flags);
6667 if (rq->rd) {
Rusty Russellc6c49272008-11-25 02:35:05 +10306668 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006669 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006670 }
6671 spin_unlock_irqrestore(&rq->lock, flags);
6672 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673#endif
6674 }
6675 return NOTIFY_OK;
6676}
6677
6678/* Register at highest priority so that task migration (migrate_all_tasks)
6679 * happens before everything else.
6680 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006681static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682 .notifier_call = migration_call,
6683 .priority = 10
6684};
6685
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07006686static int __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687{
6688 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006689 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006690
6691 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006692 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6693 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6695 register_cpu_notifier(&migration_notifier);
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07006696
6697 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698}
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -07006699early_initcall(migration_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700#endif
6701
6702#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006703
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006704#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006705
Mike Travis7c16ec52008-04-04 18:11:11 -07006706static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
Rusty Russell96f874e22008-11-25 02:35:14 +10306707 struct cpumask *groupmask)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006708{
6709 struct sched_group *group = sd->groups;
Mike Travis434d53b2008-04-04 18:11:04 -07006710 char str[256];
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006711
Rusty Russell968ea6d2008-12-13 21:55:51 +10306712 cpulist_scnprintf(str, sizeof(str), sched_domain_span(sd));
Rusty Russell96f874e22008-11-25 02:35:14 +10306713 cpumask_clear(groupmask);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006714
6715 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6716
6717 if (!(sd->flags & SD_LOAD_BALANCE)) {
6718 printk("does not load-balance\n");
6719 if (sd->parent)
6720 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6721 " has parent");
6722 return -1;
6723 }
6724
Li Zefaneefd7962008-11-04 16:15:37 +08006725 printk(KERN_CONT "span %s level %s\n", str, sd->name);
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006726
Rusty Russell758b2cd2008-11-25 02:35:04 +10306727 if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006728 printk(KERN_ERR "ERROR: domain->span does not contain "
6729 "CPU%d\n", cpu);
6730 }
Rusty Russell758b2cd2008-11-25 02:35:04 +10306731 if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006732 printk(KERN_ERR "ERROR: domain->groups does not contain"
6733 " CPU%d\n", cpu);
6734 }
6735
6736 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6737 do {
6738 if (!group) {
6739 printk("\n");
6740 printk(KERN_ERR "ERROR: group is NULL\n");
6741 break;
6742 }
6743
6744 if (!group->__cpu_power) {
6745 printk(KERN_CONT "\n");
6746 printk(KERN_ERR "ERROR: domain->cpu_power not "
6747 "set\n");
6748 break;
6749 }
6750
Rusty Russell758b2cd2008-11-25 02:35:04 +10306751 if (!cpumask_weight(sched_group_cpus(group))) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006752 printk(KERN_CONT "\n");
6753 printk(KERN_ERR "ERROR: empty group\n");
6754 break;
6755 }
6756
Rusty Russell758b2cd2008-11-25 02:35:04 +10306757 if (cpumask_intersects(groupmask, sched_group_cpus(group))) {
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006758 printk(KERN_CONT "\n");
6759 printk(KERN_ERR "ERROR: repeated CPUs\n");
6760 break;
6761 }
6762
Rusty Russell758b2cd2008-11-25 02:35:04 +10306763 cpumask_or(groupmask, groupmask, sched_group_cpus(group));
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006764
Rusty Russell968ea6d2008-12-13 21:55:51 +10306765 cpulist_scnprintf(str, sizeof(str), sched_group_cpus(group));
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006766 printk(KERN_CONT " %s", str);
6767
6768 group = group->next;
6769 } while (group != sd->groups);
6770 printk(KERN_CONT "\n");
6771
Rusty Russell758b2cd2008-11-25 02:35:04 +10306772 if (!cpumask_equal(sched_domain_span(sd), groupmask))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006773 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6774
Rusty Russell758b2cd2008-11-25 02:35:04 +10306775 if (sd->parent &&
6776 !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006777 printk(KERN_ERR "ERROR: parent span is not a superset "
6778 "of domain->span\n");
6779 return 0;
6780}
6781
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782static void sched_domain_debug(struct sched_domain *sd, int cpu)
6783{
Rusty Russelld5dd3db2008-11-25 02:35:12 +10306784 cpumask_var_t groupmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785 int level = 0;
6786
Nick Piggin41c7ce92005-06-25 14:57:24 -07006787 if (!sd) {
6788 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6789 return;
6790 }
6791
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6793
Rusty Russelld5dd3db2008-11-25 02:35:12 +10306794 if (!alloc_cpumask_var(&groupmask, GFP_KERNEL)) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006795 printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
6796 return;
6797 }
6798
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006799 for (;;) {
Mike Travis7c16ec52008-04-04 18:11:11 -07006800 if (sched_domain_debug_one(sd, cpu, level, groupmask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802 level++;
6803 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006804 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006805 break;
6806 }
Rusty Russelld5dd3db2008-11-25 02:35:12 +10306807 free_cpumask_var(groupmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006809#else /* !CONFIG_SCHED_DEBUG */
Ingo Molnar48f24c42006-07-03 00:25:40 -07006810# define sched_domain_debug(sd, cpu) do { } while (0)
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006811#endif /* CONFIG_SCHED_DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006813static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006814{
Rusty Russell758b2cd2008-11-25 02:35:04 +10306815 if (cpumask_weight(sched_domain_span(sd)) == 1)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006816 return 1;
6817
6818 /* Following flags need at least 2 groups */
6819 if (sd->flags & (SD_LOAD_BALANCE |
6820 SD_BALANCE_NEWIDLE |
6821 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006822 SD_BALANCE_EXEC |
6823 SD_SHARE_CPUPOWER |
6824 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006825 if (sd->groups != sd->groups->next)
6826 return 0;
6827 }
6828
6829 /* Following flags don't use groups */
6830 if (sd->flags & (SD_WAKE_IDLE |
6831 SD_WAKE_AFFINE |
6832 SD_WAKE_BALANCE))
6833 return 0;
6834
6835 return 1;
6836}
6837
Ingo Molnar48f24c42006-07-03 00:25:40 -07006838static int
6839sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006840{
6841 unsigned long cflags = sd->flags, pflags = parent->flags;
6842
6843 if (sd_degenerate(parent))
6844 return 1;
6845
Rusty Russell758b2cd2008-11-25 02:35:04 +10306846 if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
Suresh Siddha245af2c2005-06-25 14:57:25 -07006847 return 0;
6848
6849 /* Does parent contain flags not in child? */
6850 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6851 if (cflags & SD_WAKE_AFFINE)
6852 pflags &= ~SD_WAKE_BALANCE;
6853 /* Flags needing groups don't count if only 1 group in parent */
6854 if (parent->groups == parent->groups->next) {
6855 pflags &= ~(SD_LOAD_BALANCE |
6856 SD_BALANCE_NEWIDLE |
6857 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006858 SD_BALANCE_EXEC |
6859 SD_SHARE_CPUPOWER |
6860 SD_SHARE_PKG_RESOURCES);
Ken Chen54364992008-12-07 18:47:37 -08006861 if (nr_node_ids == 1)
6862 pflags &= ~SD_SERIALIZE;
Suresh Siddha245af2c2005-06-25 14:57:25 -07006863 }
6864 if (~cflags & pflags)
6865 return 0;
6866
6867 return 1;
6868}
6869
Rusty Russellc6c49272008-11-25 02:35:05 +10306870static void free_rootdomain(struct root_domain *rd)
6871{
Rusty Russell68e74562008-11-25 02:35:13 +10306872 cpupri_cleanup(&rd->cpupri);
6873
Rusty Russellc6c49272008-11-25 02:35:05 +10306874 free_cpumask_var(rd->rto_mask);
6875 free_cpumask_var(rd->online);
6876 free_cpumask_var(rd->span);
6877 kfree(rd);
6878}
6879
Gregory Haskins57d885f2008-01-25 21:08:18 +01006880static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6881{
6882 unsigned long flags;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006883
6884 spin_lock_irqsave(&rq->lock, flags);
6885
6886 if (rq->rd) {
6887 struct root_domain *old_rd = rq->rd;
6888
Rusty Russellc6c49272008-11-25 02:35:05 +10306889 if (cpumask_test_cpu(rq->cpu, old_rd->online))
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006890 set_rq_offline(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006891
Rusty Russellc6c49272008-11-25 02:35:05 +10306892 cpumask_clear_cpu(rq->cpu, old_rd->span);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006893
Gregory Haskins57d885f2008-01-25 21:08:18 +01006894 if (atomic_dec_and_test(&old_rd->refcount))
Rusty Russellc6c49272008-11-25 02:35:05 +10306895 free_rootdomain(old_rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006896 }
6897
6898 atomic_inc(&rd->refcount);
6899 rq->rd = rd;
6900
Rusty Russellc6c49272008-11-25 02:35:05 +10306901 cpumask_set_cpu(rq->cpu, rd->span);
6902 if (cpumask_test_cpu(rq->cpu, cpu_online_mask))
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006903 set_rq_online(rq);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006904
6905 spin_unlock_irqrestore(&rq->lock, flags);
6906}
6907
Rusty Russellc6c49272008-11-25 02:35:05 +10306908static int init_rootdomain(struct root_domain *rd, bool bootmem)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006909{
6910 memset(rd, 0, sizeof(*rd));
6911
Rusty Russellc6c49272008-11-25 02:35:05 +10306912 if (bootmem) {
6913 alloc_bootmem_cpumask_var(&def_root_domain.span);
6914 alloc_bootmem_cpumask_var(&def_root_domain.online);
6915 alloc_bootmem_cpumask_var(&def_root_domain.rto_mask);
Rusty Russell68e74562008-11-25 02:35:13 +10306916 cpupri_init(&rd->cpupri, true);
Rusty Russellc6c49272008-11-25 02:35:05 +10306917 return 0;
6918 }
Gregory Haskins6e0534f2008-05-12 21:21:01 +02006919
Rusty Russellc6c49272008-11-25 02:35:05 +10306920 if (!alloc_cpumask_var(&rd->span, GFP_KERNEL))
6921 goto free_rd;
6922 if (!alloc_cpumask_var(&rd->online, GFP_KERNEL))
6923 goto free_span;
6924 if (!alloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
6925 goto free_online;
Gregory Haskins6e0534f2008-05-12 21:21:01 +02006926
Rusty Russell68e74562008-11-25 02:35:13 +10306927 if (cpupri_init(&rd->cpupri, false) != 0)
6928 goto free_rto_mask;
Rusty Russellc6c49272008-11-25 02:35:05 +10306929 return 0;
6930
Rusty Russell68e74562008-11-25 02:35:13 +10306931free_rto_mask:
6932 free_cpumask_var(rd->rto_mask);
Rusty Russellc6c49272008-11-25 02:35:05 +10306933free_online:
6934 free_cpumask_var(rd->online);
6935free_span:
6936 free_cpumask_var(rd->span);
6937free_rd:
6938 kfree(rd);
6939 return -ENOMEM;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006940}
6941
6942static void init_defrootdomain(void)
6943{
Rusty Russellc6c49272008-11-25 02:35:05 +10306944 init_rootdomain(&def_root_domain, true);
6945
Gregory Haskins57d885f2008-01-25 21:08:18 +01006946 atomic_set(&def_root_domain.refcount, 1);
6947}
6948
Gregory Haskinsdc938522008-01-25 21:08:26 +01006949static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006950{
6951 struct root_domain *rd;
6952
6953 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6954 if (!rd)
6955 return NULL;
6956
Rusty Russellc6c49272008-11-25 02:35:05 +10306957 if (init_rootdomain(rd, false) != 0) {
6958 kfree(rd);
6959 return NULL;
6960 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006961
6962 return rd;
6963}
6964
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006966 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967 * hold the hotplug lock.
6968 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006969static void
6970cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006972 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006973 struct sched_domain *tmp;
6974
6975 /* Remove the sched domains which do not contribute to scheduling. */
Li Zefanf29c9b12008-11-06 09:45:16 +08006976 for (tmp = sd; tmp; ) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006977 struct sched_domain *parent = tmp->parent;
6978 if (!parent)
6979 break;
Li Zefanf29c9b12008-11-06 09:45:16 +08006980
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006981 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006982 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006983 if (parent->parent)
6984 parent->parent->child = tmp;
Li Zefanf29c9b12008-11-06 09:45:16 +08006985 } else
6986 tmp = tmp->parent;
Suresh Siddha245af2c2005-06-25 14:57:25 -07006987 }
6988
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006989 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006990 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006991 if (sd)
6992 sd->child = NULL;
6993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994
6995 sched_domain_debug(sd, cpu);
6996
Gregory Haskins57d885f2008-01-25 21:08:18 +01006997 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006998 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999}
7000
7001/* cpus with isolated domains */
Rusty Russelldcc30a32008-11-25 02:35:12 +10307002static cpumask_var_t cpu_isolated_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003
7004/* Setup the mask of cpus configured for isolated domains */
7005static int __init isolated_cpu_setup(char *str)
7006{
Rusty Russell968ea6d2008-12-13 21:55:51 +10307007 cpulist_parse(str, cpu_isolated_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008 return 1;
7009}
7010
Ingo Molnar8927f492007-10-15 17:00:13 +02007011__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012
7013/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007014 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
7015 * to a function which identifies what group(along with sched group) a CPU
Rusty Russell96f874e22008-11-25 02:35:14 +10307016 * belongs to. The return value of group_fn must be a >= 0 and < nr_cpu_ids
7017 * (due to the fact that we keep track of groups covered with a struct cpumask).
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018 *
7019 * init_sched_build_groups will build a circular linked list of the groups
7020 * covered by the given span, and will set each group's ->cpumask correctly,
7021 * and ->cpu_power to 0.
7022 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007023static void
Rusty Russell96f874e22008-11-25 02:35:14 +10307024init_sched_build_groups(const struct cpumask *span,
7025 const struct cpumask *cpu_map,
7026 int (*group_fn)(int cpu, const struct cpumask *cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007027 struct sched_group **sg,
Rusty Russell96f874e22008-11-25 02:35:14 +10307028 struct cpumask *tmpmask),
7029 struct cpumask *covered, struct cpumask *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030{
7031 struct sched_group *first = NULL, *last = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032 int i;
7033
Rusty Russell96f874e22008-11-25 02:35:14 +10307034 cpumask_clear(covered);
Mike Travis7c16ec52008-04-04 18:11:11 -07007035
Rusty Russellabcd0832008-11-25 02:35:02 +10307036 for_each_cpu(i, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007037 struct sched_group *sg;
Mike Travis7c16ec52008-04-04 18:11:11 -07007038 int group = group_fn(i, cpu_map, &sg, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039 int j;
7040
Rusty Russell758b2cd2008-11-25 02:35:04 +10307041 if (cpumask_test_cpu(i, covered))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042 continue;
7043
Rusty Russell758b2cd2008-11-25 02:35:04 +10307044 cpumask_clear(sched_group_cpus(sg));
Eric Dumazet5517d862007-05-08 00:32:57 -07007045 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046
Rusty Russellabcd0832008-11-25 02:35:02 +10307047 for_each_cpu(j, span) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007048 if (group_fn(j, cpu_map, NULL, tmpmask) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 continue;
7050
Rusty Russell96f874e22008-11-25 02:35:14 +10307051 cpumask_set_cpu(j, covered);
Rusty Russell758b2cd2008-11-25 02:35:04 +10307052 cpumask_set_cpu(j, sched_group_cpus(sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053 }
7054 if (!first)
7055 first = sg;
7056 if (last)
7057 last->next = sg;
7058 last = sg;
7059 }
7060 last->next = first;
7061}
7062
John Hawkes9c1cfda2005-09-06 15:18:14 -07007063#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064
John Hawkes9c1cfda2005-09-06 15:18:14 -07007065#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08007066
John Hawkes9c1cfda2005-09-06 15:18:14 -07007067/**
7068 * find_next_best_node - find the next node to include in a sched_domain
7069 * @node: node whose sched_domain we're building
7070 * @used_nodes: nodes already in the sched_domain
7071 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007072 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07007073 * finds the closest node not already in the @used_nodes map.
7074 *
7075 * Should use nodemask_t.
7076 */
Mike Travisc5f59f02008-04-04 18:11:10 -07007077static int find_next_best_node(int node, nodemask_t *used_nodes)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007078{
7079 int i, n, val, min_val, best_node = 0;
7080
7081 min_val = INT_MAX;
7082
Mike Travis076ac2a2008-05-12 21:21:12 +02007083 for (i = 0; i < nr_node_ids; i++) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007084 /* Start at @node */
Mike Travis076ac2a2008-05-12 21:21:12 +02007085 n = (node + i) % nr_node_ids;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007086
7087 if (!nr_cpus_node(n))
7088 continue;
7089
7090 /* Skip already used nodes */
Mike Travisc5f59f02008-04-04 18:11:10 -07007091 if (node_isset(n, *used_nodes))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007092 continue;
7093
7094 /* Simple min distance search */
7095 val = node_distance(node, n);
7096
7097 if (val < min_val) {
7098 min_val = val;
7099 best_node = n;
7100 }
7101 }
7102
Mike Travisc5f59f02008-04-04 18:11:10 -07007103 node_set(best_node, *used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007104 return best_node;
7105}
7106
7107/**
7108 * sched_domain_node_span - get a cpumask for a node's sched_domain
7109 * @node: node whose cpumask we're constructing
Randy Dunlap73486722008-04-22 10:07:22 -07007110 * @span: resulting cpumask
John Hawkes9c1cfda2005-09-06 15:18:14 -07007111 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007112 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07007113 * should be one that prevents unnecessary balancing, but also spreads tasks
7114 * out optimally.
7115 */
Rusty Russell96f874e22008-11-25 02:35:14 +10307116static void sched_domain_node_span(int node, struct cpumask *span)
John Hawkes9c1cfda2005-09-06 15:18:14 -07007117{
Mike Travisc5f59f02008-04-04 18:11:10 -07007118 nodemask_t used_nodes;
Rusty Russell96f874e22008-11-25 02:35:14 +10307119 /* FIXME: use cpumask_of_node() */
Mike Travisc5f59f02008-04-04 18:11:10 -07007120 node_to_cpumask_ptr(nodemask, node);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007121 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007122
Mike Travis4bdbaad32008-04-15 16:35:52 -07007123 cpus_clear(*span);
Mike Travisc5f59f02008-04-04 18:11:10 -07007124 nodes_clear(used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007125
Mike Travis4bdbaad32008-04-15 16:35:52 -07007126 cpus_or(*span, *span, *nodemask);
Mike Travisc5f59f02008-04-04 18:11:10 -07007127 node_set(node, used_nodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007128
7129 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
Mike Travisc5f59f02008-04-04 18:11:10 -07007130 int next_node = find_next_best_node(node, &used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007131
Mike Travisc5f59f02008-04-04 18:11:10 -07007132 node_to_cpumask_ptr_next(nodemask, next_node);
Mike Travis4bdbaad32008-04-15 16:35:52 -07007133 cpus_or(*span, *span, *nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007134 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007135}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007136#endif /* CONFIG_NUMA */
John Hawkes9c1cfda2005-09-06 15:18:14 -07007137
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007138int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007139
John Hawkes9c1cfda2005-09-06 15:18:14 -07007140/*
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307141 * The cpus mask in sched_group and sched_domain hangs off the end.
7142 * FIXME: use cpumask_var_t or dynamic percpu alloc to avoid wasting space
7143 * for nr_cpu_ids < CONFIG_NR_CPUS.
7144 */
7145struct static_sched_group {
7146 struct sched_group sg;
7147 DECLARE_BITMAP(cpus, CONFIG_NR_CPUS);
7148};
7149
7150struct static_sched_domain {
7151 struct sched_domain sd;
7152 DECLARE_BITMAP(span, CONFIG_NR_CPUS);
7153};
7154
7155/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07007156 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07007157 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158#ifdef CONFIG_SCHED_SMT
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307159static DEFINE_PER_CPU(struct static_sched_domain, cpu_domains);
7160static DEFINE_PER_CPU(struct static_sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007161
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007162static int
Rusty Russell96f874e22008-11-25 02:35:14 +10307163cpu_to_cpu_group(int cpu, const struct cpumask *cpu_map,
7164 struct sched_group **sg, struct cpumask *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007166 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307167 *sg = &per_cpu(sched_group_cpus, cpu).sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168 return cpu;
7169}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007170#endif /* CONFIG_SCHED_SMT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171
Ingo Molnar48f24c42006-07-03 00:25:40 -07007172/*
7173 * multi-core sched-domains:
7174 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007175#ifdef CONFIG_SCHED_MC
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307176static DEFINE_PER_CPU(struct static_sched_domain, core_domains);
7177static DEFINE_PER_CPU(struct static_sched_group, sched_group_core);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007178#endif /* CONFIG_SCHED_MC */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007179
7180#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007181static int
Rusty Russell96f874e22008-11-25 02:35:14 +10307182cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
7183 struct sched_group **sg, struct cpumask *mask)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007184{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007185 int group;
Mike Travis7c16ec52008-04-04 18:11:11 -07007186
Rusty Russell96f874e22008-11-25 02:35:14 +10307187 cpumask_and(mask, &per_cpu(cpu_sibling_map, cpu), cpu_map);
7188 group = cpumask_first(mask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007189 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307190 *sg = &per_cpu(sched_group_core, group).sg;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007191 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007192}
7193#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007194static int
Rusty Russell96f874e22008-11-25 02:35:14 +10307195cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
7196 struct sched_group **sg, struct cpumask *unused)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007197{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007198 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307199 *sg = &per_cpu(sched_group_core, cpu).sg;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007200 return cpu;
7201}
7202#endif
7203
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307204static DEFINE_PER_CPU(struct static_sched_domain, phys_domains);
7205static DEFINE_PER_CPU(struct static_sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07007206
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007207static int
Rusty Russell96f874e22008-11-25 02:35:14 +10307208cpu_to_phys_group(int cpu, const struct cpumask *cpu_map,
7209 struct sched_group **sg, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007211 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007212#ifdef CONFIG_SCHED_MC
Rusty Russell96f874e22008-11-25 02:35:14 +10307213 /* FIXME: Use cpu_coregroup_mask. */
Mike Travis7c16ec52008-04-04 18:11:11 -07007214 *mask = cpu_coregroup_map(cpu);
7215 cpus_and(*mask, *mask, *cpu_map);
Rusty Russell96f874e22008-11-25 02:35:14 +10307216 group = cpumask_first(mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007217#elif defined(CONFIG_SCHED_SMT)
Rusty Russell96f874e22008-11-25 02:35:14 +10307218 cpumask_and(mask, &per_cpu(cpu_sibling_map, cpu), cpu_map);
7219 group = cpumask_first(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007221 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007222#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007223 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307224 *sg = &per_cpu(sched_group_phys, group).sg;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007225 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226}
7227
7228#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07007229/*
7230 * The init_sched_build_groups can't handle what we want to do with node
7231 * groups, so roll our own. Now each node has its own list of groups which
7232 * gets dynamically allocated.
7233 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234static DEFINE_PER_CPU(struct sched_domain, node_domains);
Mike Travis434d53b2008-04-04 18:11:04 -07007235static struct sched_group ***sched_group_nodes_bycpu;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007236
7237static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307238static DEFINE_PER_CPU(struct static_sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007239
Rusty Russell96f874e22008-11-25 02:35:14 +10307240static int cpu_to_allnodes_group(int cpu, const struct cpumask *cpu_map,
7241 struct sched_group **sg,
7242 struct cpumask *nodemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007244 int group;
Rusty Russell96f874e22008-11-25 02:35:14 +10307245 /* FIXME: use cpumask_of_node */
Mike Travisea6f18e2008-11-25 02:35:02 +10307246 node_to_cpumask_ptr(pnodemask, cpu_to_node(cpu));
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007247
Rusty Russell96f874e22008-11-25 02:35:14 +10307248 cpumask_and(nodemask, pnodemask, cpu_map);
7249 group = cpumask_first(nodemask);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007250
7251 if (sg)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307252 *sg = &per_cpu(sched_group_allnodes, group).sg;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007253 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007254}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007255
Siddha, Suresh B08069032006-03-27 01:15:23 -08007256static void init_numa_sched_groups_power(struct sched_group *group_head)
7257{
7258 struct sched_group *sg = group_head;
7259 int j;
7260
7261 if (!sg)
7262 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02007263 do {
Rusty Russell758b2cd2008-11-25 02:35:04 +10307264 for_each_cpu(j, sched_group_cpus(sg)) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02007265 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08007266
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307267 sd = &per_cpu(phys_domains, j).sd;
Rusty Russell758b2cd2008-11-25 02:35:04 +10307268 if (j != cpumask_first(sched_group_cpus(sd->groups))) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02007269 /*
7270 * Only add "power" once for each
7271 * physical package.
7272 */
7273 continue;
7274 }
7275
7276 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007277 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02007278 sg = sg->next;
7279 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08007280}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007281#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007283#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007284/* Free memory allocated for various sched_group structures */
Rusty Russell96f874e22008-11-25 02:35:14 +10307285static void free_sched_groups(const struct cpumask *cpu_map,
7286 struct cpumask *nodemask)
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007287{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007288 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007289
Rusty Russellabcd0832008-11-25 02:35:02 +10307290 for_each_cpu(cpu, cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007291 struct sched_group **sched_group_nodes
7292 = sched_group_nodes_bycpu[cpu];
7293
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007294 if (!sched_group_nodes)
7295 continue;
7296
Mike Travis076ac2a2008-05-12 21:21:12 +02007297 for (i = 0; i < nr_node_ids; i++) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007298 struct sched_group *oldsg, *sg = sched_group_nodes[i];
Rusty Russell96f874e22008-11-25 02:35:14 +10307299 /* FIXME: Use cpumask_of_node */
Mike Travisea6f18e2008-11-25 02:35:02 +10307300 node_to_cpumask_ptr(pnodemask, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007301
Mike Travisea6f18e2008-11-25 02:35:02 +10307302 cpus_and(*nodemask, *pnodemask, *cpu_map);
Rusty Russell96f874e22008-11-25 02:35:14 +10307303 if (cpumask_empty(nodemask))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007304 continue;
7305
7306 if (sg == NULL)
7307 continue;
7308 sg = sg->next;
7309next_sg:
7310 oldsg = sg;
7311 sg = sg->next;
7312 kfree(oldsg);
7313 if (oldsg != sched_group_nodes[i])
7314 goto next_sg;
7315 }
7316 kfree(sched_group_nodes);
7317 sched_group_nodes_bycpu[cpu] = NULL;
7318 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007319}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007320#else /* !CONFIG_NUMA */
Rusty Russell96f874e22008-11-25 02:35:14 +10307321static void free_sched_groups(const struct cpumask *cpu_map,
7322 struct cpumask *nodemask)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007323{
7324}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007325#endif /* CONFIG_NUMA */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007326
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007328 * Initialize sched groups cpu_power.
7329 *
7330 * cpu_power indicates the capacity of sched group, which is used while
7331 * distributing the load between different sched groups in a sched domain.
7332 * Typically cpu_power for all the groups in a sched domain will be same unless
7333 * there are asymmetries in the topology. If there are asymmetries, group
7334 * having more cpu_power will pickup more load compared to the group having
7335 * less cpu_power.
7336 *
7337 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
7338 * the maximum number of tasks a group can handle in the presence of other idle
7339 * or lightly loaded groups in the same sched domain.
7340 */
7341static void init_sched_groups_power(int cpu, struct sched_domain *sd)
7342{
7343 struct sched_domain *child;
7344 struct sched_group *group;
7345
7346 WARN_ON(!sd || !sd->groups);
7347
Rusty Russell758b2cd2008-11-25 02:35:04 +10307348 if (cpu != cpumask_first(sched_group_cpus(sd->groups)))
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007349 return;
7350
7351 child = sd->child;
7352
Eric Dumazet5517d862007-05-08 00:32:57 -07007353 sd->groups->__cpu_power = 0;
7354
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007355 /*
7356 * For perf policy, if the groups in child domain share resources
7357 * (for example cores sharing some portions of the cache hierarchy
7358 * or SMT), then set this domain groups cpu_power such that each group
7359 * can handle only one task, when there are other idle groups in the
7360 * same sched domain.
7361 */
7362 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
7363 (child->flags &
7364 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07007365 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007366 return;
7367 }
7368
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007369 /*
7370 * add cpu_power of each child group to this groups cpu_power
7371 */
7372 group = child->groups;
7373 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07007374 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007375 group = group->next;
7376 } while (group != child->groups);
7377}
7378
7379/*
Mike Travis7c16ec52008-04-04 18:11:11 -07007380 * Initializers for schedule domains
7381 * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
7382 */
7383
Ingo Molnara5d8c342008-10-09 11:35:51 +02007384#ifdef CONFIG_SCHED_DEBUG
7385# define SD_INIT_NAME(sd, type) sd->name = #type
7386#else
7387# define SD_INIT_NAME(sd, type) do { } while (0)
7388#endif
7389
Mike Travis7c16ec52008-04-04 18:11:11 -07007390#define SD_INIT(sd, type) sd_init_##type(sd)
Ingo Molnara5d8c342008-10-09 11:35:51 +02007391
Mike Travis7c16ec52008-04-04 18:11:11 -07007392#define SD_INIT_FUNC(type) \
7393static noinline void sd_init_##type(struct sched_domain *sd) \
7394{ \
7395 memset(sd, 0, sizeof(*sd)); \
7396 *sd = SD_##type##_INIT; \
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007397 sd->level = SD_LV_##type; \
Ingo Molnara5d8c342008-10-09 11:35:51 +02007398 SD_INIT_NAME(sd, type); \
Mike Travis7c16ec52008-04-04 18:11:11 -07007399}
7400
7401SD_INIT_FUNC(CPU)
7402#ifdef CONFIG_NUMA
7403 SD_INIT_FUNC(ALLNODES)
7404 SD_INIT_FUNC(NODE)
7405#endif
7406#ifdef CONFIG_SCHED_SMT
7407 SD_INIT_FUNC(SIBLING)
7408#endif
7409#ifdef CONFIG_SCHED_MC
7410 SD_INIT_FUNC(MC)
7411#endif
7412
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007413static int default_relax_domain_level = -1;
7414
7415static int __init setup_relax_domain_level(char *str)
7416{
Li Zefan30e0e172008-05-13 10:27:17 +08007417 unsigned long val;
7418
7419 val = simple_strtoul(str, NULL, 0);
7420 if (val < SD_LV_MAX)
7421 default_relax_domain_level = val;
7422
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007423 return 1;
7424}
7425__setup("relax_domain_level=", setup_relax_domain_level);
7426
7427static void set_domain_attribute(struct sched_domain *sd,
7428 struct sched_domain_attr *attr)
7429{
7430 int request;
7431
7432 if (!attr || attr->relax_domain_level < 0) {
7433 if (default_relax_domain_level < 0)
7434 return;
7435 else
7436 request = default_relax_domain_level;
7437 } else
7438 request = attr->relax_domain_level;
7439 if (request < sd->level) {
7440 /* turn off idle balance on this domain */
7441 sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
7442 } else {
7443 /* turn on idle balance on this domain */
7444 sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
7445 }
7446}
7447
Mike Travis7c16ec52008-04-04 18:11:11 -07007448/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007449 * Build sched domains for a given set of cpus and attach the sched domains
7450 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07007451 */
Rusty Russell96f874e22008-11-25 02:35:14 +10307452static int __build_sched_domains(const struct cpumask *cpu_map,
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007453 struct sched_domain_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454{
Rusty Russell3404c8d2008-11-25 02:35:03 +10307455 int i, err = -ENOMEM;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007456 struct root_domain *rd;
Rusty Russell3404c8d2008-11-25 02:35:03 +10307457 cpumask_var_t nodemask, this_sibling_map, this_core_map, send_covered,
7458 tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07007459#ifdef CONFIG_NUMA
Rusty Russell3404c8d2008-11-25 02:35:03 +10307460 cpumask_var_t domainspan, covered, notcovered;
John Hawkesd1b55132005-09-06 15:18:14 -07007461 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007462 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07007463
Rusty Russell3404c8d2008-11-25 02:35:03 +10307464 if (!alloc_cpumask_var(&domainspan, GFP_KERNEL))
7465 goto out;
7466 if (!alloc_cpumask_var(&covered, GFP_KERNEL))
7467 goto free_domainspan;
7468 if (!alloc_cpumask_var(&notcovered, GFP_KERNEL))
7469 goto free_covered;
7470#endif
7471
7472 if (!alloc_cpumask_var(&nodemask, GFP_KERNEL))
7473 goto free_notcovered;
7474 if (!alloc_cpumask_var(&this_sibling_map, GFP_KERNEL))
7475 goto free_nodemask;
7476 if (!alloc_cpumask_var(&this_core_map, GFP_KERNEL))
7477 goto free_this_sibling_map;
7478 if (!alloc_cpumask_var(&send_covered, GFP_KERNEL))
7479 goto free_this_core_map;
7480 if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
7481 goto free_send_covered;
7482
7483#ifdef CONFIG_NUMA
John Hawkesd1b55132005-09-06 15:18:14 -07007484 /*
7485 * Allocate the per-node list of sched groups
7486 */
Mike Travis076ac2a2008-05-12 21:21:12 +02007487 sched_group_nodes = kcalloc(nr_node_ids, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007488 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07007489 if (!sched_group_nodes) {
7490 printk(KERN_WARNING "Can not alloc sched group node list\n");
Rusty Russell3404c8d2008-11-25 02:35:03 +10307491 goto free_tmpmask;
John Hawkesd1b55132005-09-06 15:18:14 -07007492 }
John Hawkesd1b55132005-09-06 15:18:14 -07007493#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494
Gregory Haskinsdc938522008-01-25 21:08:26 +01007495 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01007496 if (!rd) {
7497 printk(KERN_WARNING "Cannot alloc root domain\n");
Rusty Russell3404c8d2008-11-25 02:35:03 +10307498 goto free_sched_groups;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007499 }
7500
Mike Travis7c16ec52008-04-04 18:11:11 -07007501#ifdef CONFIG_NUMA
Rusty Russell96f874e22008-11-25 02:35:14 +10307502 sched_group_nodes_bycpu[cpumask_first(cpu_map)] = sched_group_nodes;
Mike Travis7c16ec52008-04-04 18:11:11 -07007503#endif
7504
Linus Torvalds1da177e2005-04-16 15:20:36 -07007505 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007506 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007507 */
Rusty Russellabcd0832008-11-25 02:35:02 +10307508 for_each_cpu(i, cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509 struct sched_domain *sd = NULL, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007510
Rusty Russell96f874e22008-11-25 02:35:14 +10307511 /* FIXME: use cpumask_of_node */
Mike Travis7c16ec52008-04-04 18:11:11 -07007512 *nodemask = node_to_cpumask(cpu_to_node(i));
7513 cpus_and(*nodemask, *nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514
7515#ifdef CONFIG_NUMA
Rusty Russell96f874e22008-11-25 02:35:14 +10307516 if (cpumask_weight(cpu_map) >
7517 SD_NODES_PER_DOMAIN*cpumask_weight(nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007518 sd = &per_cpu(allnodes_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007519 SD_INIT(sd, ALLNODES);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007520 set_domain_attribute(sd, attr);
Rusty Russell758b2cd2008-11-25 02:35:04 +10307521 cpumask_copy(sched_domain_span(sd), cpu_map);
Mike Travis7c16ec52008-04-04 18:11:11 -07007522 cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007523 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007524 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007525 } else
7526 p = NULL;
7527
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528 sd = &per_cpu(node_domains, i);
Mike Travis7c16ec52008-04-04 18:11:11 -07007529 SD_INIT(sd, NODE);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007530 set_domain_attribute(sd, attr);
Rusty Russell758b2cd2008-11-25 02:35:04 +10307531 sched_domain_node_span(cpu_to_node(i), sched_domain_span(sd));
John Hawkes9c1cfda2005-09-06 15:18:14 -07007532 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007533 if (p)
7534 p->child = sd;
Rusty Russell758b2cd2008-11-25 02:35:04 +10307535 cpumask_and(sched_domain_span(sd),
7536 sched_domain_span(sd), cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537#endif
7538
7539 p = sd;
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307540 sd = &per_cpu(phys_domains, i).sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007541 SD_INIT(sd, CPU);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007542 set_domain_attribute(sd, attr);
Rusty Russell758b2cd2008-11-25 02:35:04 +10307543 cpumask_copy(sched_domain_span(sd), nodemask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007545 if (p)
7546 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007547 cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007549#ifdef CONFIG_SCHED_MC
7550 p = sd;
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307551 sd = &per_cpu(core_domains, i).sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007552 SD_INIT(sd, MC);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007553 set_domain_attribute(sd, attr);
Rusty Russell758b2cd2008-11-25 02:35:04 +10307554 *sched_domain_span(sd) = cpu_coregroup_map(i);
7555 cpumask_and(sched_domain_span(sd),
7556 sched_domain_span(sd), cpu_map);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007557 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007558 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007559 cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007560#endif
7561
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562#ifdef CONFIG_SCHED_SMT
7563 p = sd;
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307564 sd = &per_cpu(cpu_domains, i).sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007565 SD_INIT(sd, SIBLING);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007566 set_domain_attribute(sd, attr);
Rusty Russell758b2cd2008-11-25 02:35:04 +10307567 cpumask_and(sched_domain_span(sd),
7568 &per_cpu(cpu_sibling_map, i), cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07007570 p->child = sd;
Mike Travis7c16ec52008-04-04 18:11:11 -07007571 cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572#endif
7573 }
7574
7575#ifdef CONFIG_SCHED_SMT
7576 /* Set up CPU (sibling) groups */
Rusty Russellabcd0832008-11-25 02:35:02 +10307577 for_each_cpu(i, cpu_map) {
Rusty Russell96f874e22008-11-25 02:35:14 +10307578 cpumask_and(this_sibling_map,
7579 &per_cpu(cpu_sibling_map, i), cpu_map);
7580 if (i != cpumask_first(this_sibling_map))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581 continue;
7582
Ingo Molnardd41f592007-07-09 18:51:59 +02007583 init_sched_build_groups(this_sibling_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007584 &cpu_to_cpu_group,
7585 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586 }
7587#endif
7588
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007589#ifdef CONFIG_SCHED_MC
7590 /* Set up multi-core groups */
Rusty Russellabcd0832008-11-25 02:35:02 +10307591 for_each_cpu(i, cpu_map) {
Rusty Russell96f874e22008-11-25 02:35:14 +10307592 /* FIXME: Use cpu_coregroup_mask */
Mike Travis7c16ec52008-04-04 18:11:11 -07007593 *this_core_map = cpu_coregroup_map(i);
7594 cpus_and(*this_core_map, *this_core_map, *cpu_map);
Rusty Russell96f874e22008-11-25 02:35:14 +10307595 if (i != cpumask_first(this_core_map))
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007596 continue;
Mike Travis7c16ec52008-04-04 18:11:11 -07007597
Ingo Molnardd41f592007-07-09 18:51:59 +02007598 init_sched_build_groups(this_core_map, cpu_map,
Mike Travis7c16ec52008-04-04 18:11:11 -07007599 &cpu_to_core_group,
7600 send_covered, tmpmask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007601 }
7602#endif
7603
Linus Torvalds1da177e2005-04-16 15:20:36 -07007604 /* Set up physical groups */
Mike Travis076ac2a2008-05-12 21:21:12 +02007605 for (i = 0; i < nr_node_ids; i++) {
Rusty Russell96f874e22008-11-25 02:35:14 +10307606 /* FIXME: Use cpumask_of_node */
Mike Travis7c16ec52008-04-04 18:11:11 -07007607 *nodemask = node_to_cpumask(i);
7608 cpus_and(*nodemask, *nodemask, *cpu_map);
Rusty Russell96f874e22008-11-25 02:35:14 +10307609 if (cpumask_empty(nodemask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610 continue;
7611
Mike Travis7c16ec52008-04-04 18:11:11 -07007612 init_sched_build_groups(nodemask, cpu_map,
7613 &cpu_to_phys_group,
7614 send_covered, tmpmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615 }
7616
7617#ifdef CONFIG_NUMA
7618 /* Set up node groups */
Mike Travis7c16ec52008-04-04 18:11:11 -07007619 if (sd_allnodes) {
Mike Travis7c16ec52008-04-04 18:11:11 -07007620 init_sched_build_groups(cpu_map, cpu_map,
7621 &cpu_to_allnodes_group,
7622 send_covered, tmpmask);
7623 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007624
Mike Travis076ac2a2008-05-12 21:21:12 +02007625 for (i = 0; i < nr_node_ids; i++) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007626 /* Set up node groups */
7627 struct sched_group *sg, *prev;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007628 int j;
7629
Rusty Russell96f874e22008-11-25 02:35:14 +10307630 /* FIXME: Use cpumask_of_node */
Mike Travis7c16ec52008-04-04 18:11:11 -07007631 *nodemask = node_to_cpumask(i);
Rusty Russell96f874e22008-11-25 02:35:14 +10307632 cpumask_clear(covered);
Mike Travis7c16ec52008-04-04 18:11:11 -07007633
7634 cpus_and(*nodemask, *nodemask, *cpu_map);
Rusty Russell96f874e22008-11-25 02:35:14 +10307635 if (cpumask_empty(nodemask)) {
John Hawkesd1b55132005-09-06 15:18:14 -07007636 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007637 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07007638 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007639
Mike Travis4bdbaad32008-04-15 16:35:52 -07007640 sched_domain_node_span(i, domainspan);
Rusty Russell96f874e22008-11-25 02:35:14 +10307641 cpumask_and(domainspan, domainspan, cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007642
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307643 sg = kmalloc_node(sizeof(struct sched_group) + cpumask_size(),
7644 GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007645 if (!sg) {
7646 printk(KERN_WARNING "Can not alloc domain group for "
7647 "node %d\n", i);
7648 goto error;
7649 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007650 sched_group_nodes[i] = sg;
Rusty Russellabcd0832008-11-25 02:35:02 +10307651 for_each_cpu(j, nodemask) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07007652 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02007653
John Hawkes9c1cfda2005-09-06 15:18:14 -07007654 sd = &per_cpu(node_domains, j);
7655 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007656 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007657 sg->__cpu_power = 0;
Rusty Russell758b2cd2008-11-25 02:35:04 +10307658 cpumask_copy(sched_group_cpus(sg), nodemask);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007659 sg->next = sg;
Rusty Russell96f874e22008-11-25 02:35:14 +10307660 cpumask_or(covered, covered, nodemask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007661 prev = sg;
7662
Mike Travis076ac2a2008-05-12 21:21:12 +02007663 for (j = 0; j < nr_node_ids; j++) {
Mike Travis076ac2a2008-05-12 21:21:12 +02007664 int n = (i + j) % nr_node_ids;
Rusty Russell96f874e22008-11-25 02:35:14 +10307665 /* FIXME: Use cpumask_of_node */
Mike Travisc5f59f02008-04-04 18:11:10 -07007666 node_to_cpumask_ptr(pnodemask, n);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007667
Rusty Russell96f874e22008-11-25 02:35:14 +10307668 cpumask_complement(notcovered, covered);
7669 cpumask_and(tmpmask, notcovered, cpu_map);
7670 cpumask_and(tmpmask, tmpmask, domainspan);
7671 if (cpumask_empty(tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007672 break;
7673
Rusty Russell96f874e22008-11-25 02:35:14 +10307674 cpumask_and(tmpmask, tmpmask, pnodemask);
7675 if (cpumask_empty(tmpmask))
John Hawkes9c1cfda2005-09-06 15:18:14 -07007676 continue;
7677
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307678 sg = kmalloc_node(sizeof(struct sched_group) +
7679 cpumask_size(),
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07007680 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007681 if (!sg) {
7682 printk(KERN_WARNING
7683 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007684 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07007685 }
Eric Dumazet5517d862007-05-08 00:32:57 -07007686 sg->__cpu_power = 0;
Rusty Russell758b2cd2008-11-25 02:35:04 +10307687 cpumask_copy(sched_group_cpus(sg), tmpmask);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007688 sg->next = prev->next;
Rusty Russell96f874e22008-11-25 02:35:14 +10307689 cpumask_or(covered, covered, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007690 prev->next = sg;
7691 prev = sg;
7692 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007694#endif
7695
7696 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007697#ifdef CONFIG_SCHED_SMT
Rusty Russellabcd0832008-11-25 02:35:02 +10307698 for_each_cpu(i, cpu_map) {
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307699 struct sched_domain *sd = &per_cpu(cpu_domains, i).sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02007700
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007701 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007702 }
7703#endif
7704#ifdef CONFIG_SCHED_MC
Rusty Russellabcd0832008-11-25 02:35:02 +10307705 for_each_cpu(i, cpu_map) {
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307706 struct sched_domain *sd = &per_cpu(core_domains, i).sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02007707
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007708 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007709 }
7710#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007711
Rusty Russellabcd0832008-11-25 02:35:02 +10307712 for_each_cpu(i, cpu_map) {
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307713 struct sched_domain *sd = &per_cpu(phys_domains, i).sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02007714
Siddha, Suresh B89c47102006-10-03 01:14:09 -07007715 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007716 }
7717
John Hawkes9c1cfda2005-09-06 15:18:14 -07007718#ifdef CONFIG_NUMA
Mike Travis076ac2a2008-05-12 21:21:12 +02007719 for (i = 0; i < nr_node_ids; i++)
Siddha, Suresh B08069032006-03-27 01:15:23 -08007720 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007721
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08007722 if (sd_allnodes) {
7723 struct sched_group *sg;
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007724
Rusty Russell96f874e22008-11-25 02:35:14 +10307725 cpu_to_allnodes_group(cpumask_first(cpu_map), cpu_map, &sg,
Mike Travis7c16ec52008-04-04 18:11:11 -07007726 tmpmask);
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07007727 init_numa_sched_groups_power(sg);
7728 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07007729#endif
7730
Linus Torvalds1da177e2005-04-16 15:20:36 -07007731 /* Attach the domains */
Rusty Russellabcd0832008-11-25 02:35:02 +10307732 for_each_cpu(i, cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007733 struct sched_domain *sd;
7734#ifdef CONFIG_SCHED_SMT
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307735 sd = &per_cpu(cpu_domains, i).sd;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08007736#elif defined(CONFIG_SCHED_MC)
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307737 sd = &per_cpu(core_domains, i).sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007738#else
Rusty Russell6c99e9a2008-11-25 02:35:04 +10307739 sd = &per_cpu(phys_domains, i).sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007740#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01007741 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007742 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007743
Rusty Russell3404c8d2008-11-25 02:35:03 +10307744 err = 0;
7745
7746free_tmpmask:
7747 free_cpumask_var(tmpmask);
7748free_send_covered:
7749 free_cpumask_var(send_covered);
7750free_this_core_map:
7751 free_cpumask_var(this_core_map);
7752free_this_sibling_map:
7753 free_cpumask_var(this_sibling_map);
7754free_nodemask:
7755 free_cpumask_var(nodemask);
7756free_notcovered:
7757#ifdef CONFIG_NUMA
7758 free_cpumask_var(notcovered);
7759free_covered:
7760 free_cpumask_var(covered);
7761free_domainspan:
7762 free_cpumask_var(domainspan);
7763out:
7764#endif
7765 return err;
7766
7767free_sched_groups:
7768#ifdef CONFIG_NUMA
7769 kfree(sched_group_nodes);
7770#endif
7771 goto free_tmpmask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007772
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007773#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07007774error:
Mike Travis7c16ec52008-04-04 18:11:11 -07007775 free_sched_groups(cpu_map, tmpmask);
Rusty Russellc6c49272008-11-25 02:35:05 +10307776 free_rootdomain(rd);
Rusty Russell3404c8d2008-11-25 02:35:03 +10307777 goto free_tmpmask;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07007778#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007779}
Paul Jackson029190c2007-10-18 23:40:20 -07007780
Rusty Russell96f874e22008-11-25 02:35:14 +10307781static int build_sched_domains(const struct cpumask *cpu_map)
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007782{
7783 return __build_sched_domains(cpu_map, NULL);
7784}
7785
Rusty Russell96f874e22008-11-25 02:35:14 +10307786static struct cpumask *doms_cur; /* current sched domains */
Paul Jackson029190c2007-10-18 23:40:20 -07007787static int ndoms_cur; /* number of sched domains in 'doms_cur' */
Ingo Molnar4285f5942008-05-16 17:47:14 +02007788static struct sched_domain_attr *dattr_cur;
7789 /* attribues of custom domains in 'doms_cur' */
Paul Jackson029190c2007-10-18 23:40:20 -07007790
7791/*
7792 * Special case: If a kmalloc of a doms_cur partition (array of
Rusty Russell42128232008-11-25 02:35:12 +10307793 * cpumask) fails, then fallback to a single sched domain,
7794 * as determined by the single cpumask fallback_doms.
Paul Jackson029190c2007-10-18 23:40:20 -07007795 */
Rusty Russell42128232008-11-25 02:35:12 +10307796static cpumask_var_t fallback_doms;
Paul Jackson029190c2007-10-18 23:40:20 -07007797
Heiko Carstensee79d1b2008-12-09 18:49:50 +01007798/*
7799 * arch_update_cpu_topology lets virtualized architectures update the
7800 * cpu core maps. It is supposed to return 1 if the topology changed
7801 * or 0 if it stayed the same.
7802 */
7803int __attribute__((weak)) arch_update_cpu_topology(void)
Heiko Carstens22e52b02008-03-12 18:31:59 +01007804{
Heiko Carstensee79d1b2008-12-09 18:49:50 +01007805 return 0;
Heiko Carstens22e52b02008-03-12 18:31:59 +01007806}
7807
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007808/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007809 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07007810 * For now this just excludes isolated cpus, but could be used to
7811 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007812 */
Rusty Russell96f874e22008-11-25 02:35:14 +10307813static int arch_init_sched_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007814{
Milton Miller73785472007-10-24 18:23:48 +02007815 int err;
7816
Heiko Carstens22e52b02008-03-12 18:31:59 +01007817 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07007818 ndoms_cur = 1;
Rusty Russell96f874e22008-11-25 02:35:14 +10307819 doms_cur = kmalloc(cpumask_size(), GFP_KERNEL);
Paul Jackson029190c2007-10-18 23:40:20 -07007820 if (!doms_cur)
Rusty Russell42128232008-11-25 02:35:12 +10307821 doms_cur = fallback_doms;
Rusty Russelldcc30a32008-11-25 02:35:12 +10307822 cpumask_andnot(doms_cur, cpu_map, cpu_isolated_map);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007823 dattr_cur = NULL;
Milton Miller73785472007-10-24 18:23:48 +02007824 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02007825 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02007826
7827 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007828}
7829
Rusty Russell96f874e22008-11-25 02:35:14 +10307830static void arch_destroy_sched_domains(const struct cpumask *cpu_map,
7831 struct cpumask *tmpmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832{
Mike Travis7c16ec52008-04-04 18:11:11 -07007833 free_sched_groups(cpu_map, tmpmask);
John Hawkes9c1cfda2005-09-06 15:18:14 -07007834}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007836/*
7837 * Detach sched domains from a group of cpus specified in cpu_map
7838 * These cpus will now be attached to the NULL domain
7839 */
Rusty Russell96f874e22008-11-25 02:35:14 +10307840static void detach_destroy_domains(const struct cpumask *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007841{
Rusty Russell96f874e22008-11-25 02:35:14 +10307842 /* Save because hotplug lock held. */
7843 static DECLARE_BITMAP(tmpmask, CONFIG_NR_CPUS);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007844 int i;
7845
Rusty Russellabcd0832008-11-25 02:35:02 +10307846 for_each_cpu(i, cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01007847 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007848 synchronize_sched();
Rusty Russell96f874e22008-11-25 02:35:14 +10307849 arch_destroy_sched_domains(cpu_map, to_cpumask(tmpmask));
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007850}
7851
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007852/* handle null as "default" */
7853static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
7854 struct sched_domain_attr *new, int idx_new)
7855{
7856 struct sched_domain_attr tmp;
7857
7858 /* fast path */
7859 if (!new && !cur)
7860 return 1;
7861
7862 tmp = SD_ATTR_INIT;
7863 return !memcmp(cur ? (cur + idx_cur) : &tmp,
7864 new ? (new + idx_new) : &tmp,
7865 sizeof(struct sched_domain_attr));
7866}
7867
Paul Jackson029190c2007-10-18 23:40:20 -07007868/*
7869 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007870 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07007871 * doms_new[] to the current sched domain partitioning, doms_cur[].
7872 * It destroys each deleted domain and builds each new domain.
7873 *
Rusty Russell96f874e22008-11-25 02:35:14 +10307874 * 'doms_new' is an array of cpumask's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007875 * The masks don't intersect (don't overlap.) We should setup one
7876 * sched domain for each mask. CPUs not in any of the cpumasks will
7877 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07007878 * current 'doms_cur' domains and in the new 'doms_new', we can leave
7879 * it as it is.
7880 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007881 * The passed in 'doms_new' should be kmalloc'd. This routine takes
7882 * ownership of it and will kfree it when done with it. If the caller
Li Zefan700018e2008-11-18 14:02:03 +08007883 * failed the kmalloc call, then it can pass in doms_new == NULL &&
7884 * ndoms_new == 1, and partition_sched_domains() will fallback to
7885 * the single partition 'fallback_doms', it also forces the domains
7886 * to be rebuilt.
Paul Jackson029190c2007-10-18 23:40:20 -07007887 *
Rusty Russell96f874e22008-11-25 02:35:14 +10307888 * If doms_new == NULL it will be replaced with cpu_online_mask.
Li Zefan700018e2008-11-18 14:02:03 +08007889 * ndoms_new == 0 is a special case for destroying existing domains,
7890 * and it will not create the default domain.
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007891 *
Paul Jackson029190c2007-10-18 23:40:20 -07007892 * Call with hotplug lock held
7893 */
Rusty Russell96f874e22008-11-25 02:35:14 +10307894/* FIXME: Change to struct cpumask *doms_new[] */
7895void partition_sched_domains(int ndoms_new, struct cpumask *doms_new,
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007896 struct sched_domain_attr *dattr_new)
Paul Jackson029190c2007-10-18 23:40:20 -07007897{
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007898 int i, j, n;
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007899 int new_topology;
Paul Jackson029190c2007-10-18 23:40:20 -07007900
Heiko Carstens712555e2008-04-28 11:33:07 +02007901 mutex_lock(&sched_domains_mutex);
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007902
Milton Miller73785472007-10-24 18:23:48 +02007903 /* always unregister in case we don't destroy any domains */
7904 unregister_sched_domain_sysctl();
7905
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007906 /* Let architecture update cpu core mappings. */
7907 new_topology = arch_update_cpu_topology();
7908
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007909 n = doms_new ? ndoms_new : 0;
Paul Jackson029190c2007-10-18 23:40:20 -07007910
7911 /* Destroy deleted domains */
7912 for (i = 0; i < ndoms_cur; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007913 for (j = 0; j < n && !new_topology; j++) {
Rusty Russell96f874e22008-11-25 02:35:14 +10307914 if (cpumask_equal(&doms_cur[i], &doms_new[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007915 && dattrs_equal(dattr_cur, i, dattr_new, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007916 goto match1;
7917 }
7918 /* no match - a current sched domain not in new doms_new[] */
7919 detach_destroy_domains(doms_cur + i);
7920match1:
7921 ;
7922 }
7923
Max Krasnyanskye761b772008-07-15 04:43:49 -07007924 if (doms_new == NULL) {
7925 ndoms_cur = 0;
Rusty Russell42128232008-11-25 02:35:12 +10307926 doms_new = fallback_doms;
Rusty Russelldcc30a32008-11-25 02:35:12 +10307927 cpumask_andnot(&doms_new[0], cpu_online_mask, cpu_isolated_map);
Li Zefanfaa2f982008-11-04 16:20:23 +08007928 WARN_ON_ONCE(dattr_new);
Max Krasnyanskye761b772008-07-15 04:43:49 -07007929 }
7930
Paul Jackson029190c2007-10-18 23:40:20 -07007931 /* Build new domains */
7932 for (i = 0; i < ndoms_new; i++) {
Heiko Carstensd65bd5e2008-12-09 18:49:51 +01007933 for (j = 0; j < ndoms_cur && !new_topology; j++) {
Rusty Russell96f874e22008-11-25 02:35:14 +10307934 if (cpumask_equal(&doms_new[i], &doms_cur[j])
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007935 && dattrs_equal(dattr_new, i, dattr_cur, j))
Paul Jackson029190c2007-10-18 23:40:20 -07007936 goto match2;
7937 }
7938 /* no match - add a new doms_new */
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007939 __build_sched_domains(doms_new + i,
7940 dattr_new ? dattr_new + i : NULL);
Paul Jackson029190c2007-10-18 23:40:20 -07007941match2:
7942 ;
7943 }
7944
7945 /* Remember the new sched domains */
Rusty Russell42128232008-11-25 02:35:12 +10307946 if (doms_cur != fallback_doms)
Paul Jackson029190c2007-10-18 23:40:20 -07007947 kfree(doms_cur);
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007948 kfree(dattr_cur); /* kfree(NULL) is safe */
Paul Jackson029190c2007-10-18 23:40:20 -07007949 doms_cur = doms_new;
Hidetoshi Seto1d3504f2008-04-15 14:04:23 +09007950 dattr_cur = dattr_new;
Paul Jackson029190c2007-10-18 23:40:20 -07007951 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007952
7953 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007954
Heiko Carstens712555e2008-04-28 11:33:07 +02007955 mutex_unlock(&sched_domains_mutex);
Paul Jackson029190c2007-10-18 23:40:20 -07007956}
7957
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007958#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007959int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007960{
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007961 get_online_cpus();
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007962
7963 /* Destroy domains first to force the rebuild */
7964 partition_sched_domains(0, NULL, NULL);
7965
Max Krasnyanskye761b772008-07-15 04:43:49 -07007966 rebuild_sched_domains();
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007967 put_online_cpus();
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07007968
Max Krasnyanskye761b772008-07-15 04:43:49 -07007969 return 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007970}
7971
7972static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7973{
7974 int ret;
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307975 unsigned int level = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007976
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307977 if (sscanf(buf, "%u", &level) != 1)
7978 return -EINVAL;
7979
7980 /*
7981 * level is always be positive so don't check for
7982 * level < POWERSAVINGS_BALANCE_NONE which is 0
7983 * What happens on 0 or 1 byte write,
7984 * need to check for count as well?
7985 */
7986
7987 if (level >= MAX_POWERSAVINGS_BALANCE_LEVELS)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007988 return -EINVAL;
7989
7990 if (smt)
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307991 sched_smt_power_savings = level;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007992 else
Gautham R Shenoyafb8a9b2008-12-18 23:26:09 +05307993 sched_mc_power_savings = level;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007994
7995 ret = arch_reinit_sched_domains();
7996
7997 return ret ? ret : count;
7998}
7999
Adrian Bunk6707de002007-08-12 18:08:19 +02008000#ifdef CONFIG_SCHED_MC
Andi Kleenf718cd42008-07-29 22:33:52 -07008001static ssize_t sched_mc_power_savings_show(struct sysdev_class *class,
8002 char *page)
Adrian Bunk6707de002007-08-12 18:08:19 +02008003{
8004 return sprintf(page, "%u\n", sched_mc_power_savings);
8005}
Andi Kleenf718cd42008-07-29 22:33:52 -07008006static ssize_t sched_mc_power_savings_store(struct sysdev_class *class,
Adrian Bunk6707de002007-08-12 18:08:19 +02008007 const char *buf, size_t count)
8008{
8009 return sched_power_savings_store(buf, count, 0);
8010}
Andi Kleenf718cd42008-07-29 22:33:52 -07008011static SYSDEV_CLASS_ATTR(sched_mc_power_savings, 0644,
8012 sched_mc_power_savings_show,
8013 sched_mc_power_savings_store);
Adrian Bunk6707de002007-08-12 18:08:19 +02008014#endif
8015
8016#ifdef CONFIG_SCHED_SMT
Andi Kleenf718cd42008-07-29 22:33:52 -07008017static ssize_t sched_smt_power_savings_show(struct sysdev_class *dev,
8018 char *page)
Adrian Bunk6707de002007-08-12 18:08:19 +02008019{
8020 return sprintf(page, "%u\n", sched_smt_power_savings);
8021}
Andi Kleenf718cd42008-07-29 22:33:52 -07008022static ssize_t sched_smt_power_savings_store(struct sysdev_class *dev,
Adrian Bunk6707de002007-08-12 18:08:19 +02008023 const char *buf, size_t count)
8024{
8025 return sched_power_savings_store(buf, count, 1);
8026}
Andi Kleenf718cd42008-07-29 22:33:52 -07008027static SYSDEV_CLASS_ATTR(sched_smt_power_savings, 0644,
8028 sched_smt_power_savings_show,
Adrian Bunk6707de002007-08-12 18:08:19 +02008029 sched_smt_power_savings_store);
8030#endif
8031
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008032int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
8033{
8034 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07008035
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008036#ifdef CONFIG_SCHED_SMT
8037 if (smt_capable())
8038 err = sysfs_create_file(&cls->kset.kobj,
8039 &attr_sched_smt_power_savings.attr);
8040#endif
8041#ifdef CONFIG_SCHED_MC
8042 if (!err && mc_capable())
8043 err = sysfs_create_file(&cls->kset.kobj,
8044 &attr_sched_mc_power_savings.attr);
8045#endif
8046 return err;
8047}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008048#endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07008049
Max Krasnyanskye761b772008-07-15 04:43:49 -07008050#ifndef CONFIG_CPUSETS
Linus Torvalds1da177e2005-04-16 15:20:36 -07008051/*
Max Krasnyanskye761b772008-07-15 04:43:49 -07008052 * Add online and remove offline CPUs from the scheduler domains.
8053 * When cpusets are enabled they take over this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008054 */
8055static int update_sched_domains(struct notifier_block *nfb,
8056 unsigned long action, void *hcpu)
8057{
Max Krasnyanskye761b772008-07-15 04:43:49 -07008058 switch (action) {
8059 case CPU_ONLINE:
8060 case CPU_ONLINE_FROZEN:
8061 case CPU_DEAD:
8062 case CPU_DEAD_FROZEN:
Max Krasnyanskydfb512e2008-08-29 13:11:41 -07008063 partition_sched_domains(1, NULL, NULL);
Max Krasnyanskye761b772008-07-15 04:43:49 -07008064 return NOTIFY_OK;
8065
8066 default:
8067 return NOTIFY_DONE;
8068 }
8069}
8070#endif
8071
8072static int update_runtime(struct notifier_block *nfb,
8073 unsigned long action, void *hcpu)
8074{
Peter Zijlstra7def2be2008-06-05 14:49:58 +02008075 int cpu = (int)(long)hcpu;
8076
Linus Torvalds1da177e2005-04-16 15:20:36 -07008077 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008078 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07008079 case CPU_DOWN_PREPARE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02008080 disable_runtime(cpu_rq(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008081 return NOTIFY_OK;
8082
Linus Torvalds1da177e2005-04-16 15:20:36 -07008083 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07008084 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008085 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07008086 case CPU_ONLINE_FROZEN:
Peter Zijlstra7def2be2008-06-05 14:49:58 +02008087 enable_runtime(cpu_rq(cpu));
Max Krasnyanskye761b772008-07-15 04:43:49 -07008088 return NOTIFY_OK;
8089
Linus Torvalds1da177e2005-04-16 15:20:36 -07008090 default:
8091 return NOTIFY_DONE;
8092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008093}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008094
8095void __init sched_init_smp(void)
8096{
Rusty Russelldcc30a32008-11-25 02:35:12 +10308097 cpumask_var_t non_isolated_cpus;
8098
8099 alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
Nick Piggin5c1e1762006-10-03 01:14:04 -07008100
Mike Travis434d53b2008-04-04 18:11:04 -07008101#if defined(CONFIG_NUMA)
8102 sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
8103 GFP_KERNEL);
8104 BUG_ON(sched_group_nodes_bycpu == NULL);
8105#endif
Gautham R Shenoy95402b32008-01-25 21:08:02 +01008106 get_online_cpus();
Heiko Carstens712555e2008-04-28 11:33:07 +02008107 mutex_lock(&sched_domains_mutex);
Rusty Russelldcc30a32008-11-25 02:35:12 +10308108 arch_init_sched_domains(cpu_online_mask);
8109 cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
8110 if (cpumask_empty(non_isolated_cpus))
8111 cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
Heiko Carstens712555e2008-04-28 11:33:07 +02008112 mutex_unlock(&sched_domains_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01008113 put_online_cpus();
Max Krasnyanskye761b772008-07-15 04:43:49 -07008114
8115#ifndef CONFIG_CPUSETS
Linus Torvalds1da177e2005-04-16 15:20:36 -07008116 /* XXX: Theoretical race here - CPU may be hotplugged now */
8117 hotcpu_notifier(update_sched_domains, 0);
Max Krasnyanskye761b772008-07-15 04:43:49 -07008118#endif
8119
8120 /* RT runtime code needs to handle some hotplug events */
8121 hotcpu_notifier(update_runtime, 0);
8122
Peter Zijlstrab328ca12008-04-29 10:02:46 +02008123 init_hrtick();
Nick Piggin5c1e1762006-10-03 01:14:04 -07008124
8125 /* Move init over to a non-isolated CPU */
Rusty Russelldcc30a32008-11-25 02:35:12 +10308126 if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
Nick Piggin5c1e1762006-10-03 01:14:04 -07008127 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01008128 sched_init_granularity();
Rusty Russelldcc30a32008-11-25 02:35:12 +10308129 free_cpumask_var(non_isolated_cpus);
Rusty Russell42128232008-11-25 02:35:12 +10308130
8131 alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
Rusty Russell0e3900e2008-11-25 02:35:13 +10308132 init_sched_rt_class();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008133}
8134#else
8135void __init sched_init_smp(void)
8136{
Ingo Molnar19978ca2007-11-09 22:39:38 +01008137 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008138}
8139#endif /* CONFIG_SMP */
8140
8141int in_sched_functions(unsigned long addr)
8142{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008143 return in_lock_functions(addr) ||
8144 (addr >= (unsigned long)__sched_text_start
8145 && addr < (unsigned long)__sched_text_end);
8146}
8147
Alexey Dobriyana9957442007-10-15 17:00:13 +02008148static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02008149{
8150 cfs_rq->tasks_timeline = RB_ROOT;
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02008151 INIT_LIST_HEAD(&cfs_rq->tasks);
Ingo Molnardd41f592007-07-09 18:51:59 +02008152#ifdef CONFIG_FAIR_GROUP_SCHED
8153 cfs_rq->rq = rq;
8154#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02008155 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02008156}
8157
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008158static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
8159{
8160 struct rt_prio_array *array;
8161 int i;
8162
8163 array = &rt_rq->active;
8164 for (i = 0; i < MAX_RT_PRIO; i++) {
8165 INIT_LIST_HEAD(array->queue + i);
8166 __clear_bit(i, array->bitmap);
8167 }
8168 /* delimiter for bitsearch: */
8169 __set_bit(MAX_RT_PRIO, array->bitmap);
8170
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008171#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01008172 rt_rq->highest_prio = MAX_RT_PRIO;
8173#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008174#ifdef CONFIG_SMP
8175 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008176 rt_rq->overloaded = 0;
8177#endif
8178
8179 rt_rq->rt_time = 0;
8180 rt_rq->rt_throttled = 0;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008181 rt_rq->rt_runtime = 0;
8182 spin_lock_init(&rt_rq->rt_runtime_lock);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008183
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008184#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01008185 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008186 rt_rq->rq = rq;
8187#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008188}
8189
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008190#ifdef CONFIG_FAIR_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008191static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
8192 struct sched_entity *se, int cpu, int add,
8193 struct sched_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008194{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008195 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008196 tg->cfs_rq[cpu] = cfs_rq;
8197 init_cfs_rq(cfs_rq, rq);
8198 cfs_rq->tg = tg;
8199 if (add)
8200 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
8201
8202 tg->se[cpu] = se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008203 /* se could be NULL for init_task_group */
8204 if (!se)
8205 return;
8206
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008207 if (!parent)
8208 se->cfs_rq = &rq->cfs;
8209 else
8210 se->cfs_rq = parent->my_q;
8211
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008212 se->my_q = cfs_rq;
8213 se->load.weight = tg->shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008214 se->load.inv_weight = 0;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008215 se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008216}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008217#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008218
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008219#ifdef CONFIG_RT_GROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008220static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
8221 struct sched_rt_entity *rt_se, int cpu, int add,
8222 struct sched_rt_entity *parent)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008223{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008224 struct rq *rq = cpu_rq(cpu);
8225
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008226 tg->rt_rq[cpu] = rt_rq;
8227 init_rt_rq(rt_rq, rq);
8228 rt_rq->tg = tg;
8229 rt_rq->rt_se = rt_se;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02008230 rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008231 if (add)
8232 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
8233
8234 tg->rt_se[cpu] = rt_se;
Dhaval Giani354d60c2008-04-19 19:44:59 +02008235 if (!rt_se)
8236 return;
8237
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008238 if (!parent)
8239 rt_se->rt_rq = &rq->rt;
8240 else
8241 rt_se->rt_rq = parent->my_q;
8242
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008243 rt_se->my_q = rt_rq;
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008244 rt_se->parent = parent;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008245 INIT_LIST_HEAD(&rt_se->run_list);
8246}
8247#endif
8248
Linus Torvalds1da177e2005-04-16 15:20:36 -07008249void __init sched_init(void)
8250{
Ingo Molnardd41f592007-07-09 18:51:59 +02008251 int i, j;
Mike Travis434d53b2008-04-04 18:11:04 -07008252 unsigned long alloc_size = 0, ptr;
8253
8254#ifdef CONFIG_FAIR_GROUP_SCHED
8255 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8256#endif
8257#ifdef CONFIG_RT_GROUP_SCHED
8258 alloc_size += 2 * nr_cpu_ids * sizeof(void **);
8259#endif
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008260#ifdef CONFIG_USER_SCHED
8261 alloc_size *= 2;
8262#endif
Mike Travis434d53b2008-04-04 18:11:04 -07008263 /*
8264 * As sched_init() is called before page_alloc is setup,
8265 * we use alloc_bootmem().
8266 */
8267 if (alloc_size) {
David Miller5a9d3222008-04-24 20:46:20 -07008268 ptr = (unsigned long)alloc_bootmem(alloc_size);
Mike Travis434d53b2008-04-04 18:11:04 -07008269
8270#ifdef CONFIG_FAIR_GROUP_SCHED
8271 init_task_group.se = (struct sched_entity **)ptr;
8272 ptr += nr_cpu_ids * sizeof(void **);
8273
8274 init_task_group.cfs_rq = (struct cfs_rq **)ptr;
8275 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008276
8277#ifdef CONFIG_USER_SCHED
8278 root_task_group.se = (struct sched_entity **)ptr;
8279 ptr += nr_cpu_ids * sizeof(void **);
8280
8281 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
8282 ptr += nr_cpu_ids * sizeof(void **);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008283#endif /* CONFIG_USER_SCHED */
8284#endif /* CONFIG_FAIR_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07008285#ifdef CONFIG_RT_GROUP_SCHED
8286 init_task_group.rt_se = (struct sched_rt_entity **)ptr;
8287 ptr += nr_cpu_ids * sizeof(void **);
8288
8289 init_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008290 ptr += nr_cpu_ids * sizeof(void **);
8291
8292#ifdef CONFIG_USER_SCHED
8293 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
8294 ptr += nr_cpu_ids * sizeof(void **);
8295
8296 root_task_group.rt_rq = (struct rt_rq **)ptr;
8297 ptr += nr_cpu_ids * sizeof(void **);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008298#endif /* CONFIG_USER_SCHED */
8299#endif /* CONFIG_RT_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07008300 }
Ingo Molnardd41f592007-07-09 18:51:59 +02008301
Gregory Haskins57d885f2008-01-25 21:08:18 +01008302#ifdef CONFIG_SMP
8303 init_defrootdomain();
8304#endif
8305
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008306 init_rt_bandwidth(&def_rt_bandwidth,
8307 global_rt_period(), global_rt_runtime());
8308
8309#ifdef CONFIG_RT_GROUP_SCHED
8310 init_rt_bandwidth(&init_task_group.rt_bandwidth,
8311 global_rt_period(), global_rt_runtime());
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008312#ifdef CONFIG_USER_SCHED
8313 init_rt_bandwidth(&root_task_group.rt_bandwidth,
8314 global_rt_period(), RUNTIME_INF);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008315#endif /* CONFIG_USER_SCHED */
8316#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008317
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008318#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008319 list_add(&init_task_group.list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008320 INIT_LIST_HEAD(&init_task_group.children);
8321
8322#ifdef CONFIG_USER_SCHED
8323 INIT_LIST_HEAD(&root_task_group.children);
8324 init_task_group.parent = &root_task_group;
8325 list_add(&init_task_group.siblings, &root_task_group.children);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008326#endif /* CONFIG_USER_SCHED */
8327#endif /* CONFIG_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008328
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08008329 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07008330 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008331
8332 rq = cpu_rq(i);
8333 spin_lock_init(&rq->lock);
Nick Piggin78979862005-06-25 14:57:13 -07008334 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008335 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01008336 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008337#ifdef CONFIG_FAIR_GROUP_SCHED
8338 init_task_group.shares = init_task_group_load;
8339 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008340#ifdef CONFIG_CGROUP_SCHED
8341 /*
8342 * How much cpu bandwidth does init_task_group get?
8343 *
8344 * In case of task-groups formed thr' the cgroup filesystem, it
8345 * gets 100% of the cpu resources in the system. This overall
8346 * system cpu resource is divided among the tasks of
8347 * init_task_group and its child task-groups in a fair manner,
8348 * based on each entity's (task or task-group's) weight
8349 * (se->load.weight).
8350 *
8351 * In other words, if init_task_group has 10 tasks of weight
8352 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8353 * then A0's share of the cpu resource is:
8354 *
8355 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
8356 *
8357 * We achieve this by letting init_task_group's tasks sit
8358 * directly in rq->cfs (i.e init_task_group->se[] = NULL).
8359 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008360 init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008361#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008362 root_task_group.shares = NICE_0_LOAD;
8363 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008364 /*
8365 * In case of task-groups formed thr' the user id of tasks,
8366 * init_task_group represents tasks belonging to root user.
8367 * Hence it forms a sibling of all subsequent groups formed.
8368 * In this case, init_task_group gets only a fraction of overall
8369 * system cpu resource, based on the weight assigned to root
8370 * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
8371 * by letting tasks of init_task_group sit in a separate cfs_rq
8372 * (init_cfs_rq) and having one entity represent this group of
8373 * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
8374 */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008375 init_tg_cfs_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008376 &per_cpu(init_cfs_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008377 &per_cpu(init_sched_entity, i), i, 1,
8378 root_task_group.se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008379
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008380#endif
Dhaval Giani354d60c2008-04-19 19:44:59 +02008381#endif /* CONFIG_FAIR_GROUP_SCHED */
8382
8383 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008384#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008385 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008386#ifdef CONFIG_CGROUP_SCHED
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008387 init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008388#elif defined CONFIG_USER_SCHED
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008389 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008390 init_tg_rt_entry(&init_task_group,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008391 &per_cpu(init_rt_rq, i),
Peter Zijlstraeff766a2008-04-19 19:45:00 +02008392 &per_cpu(init_sched_rt_entity, i), i, 1,
8393 root_task_group.rt_se[i]);
Dhaval Giani354d60c2008-04-19 19:44:59 +02008394#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008395#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07008396
Ingo Molnardd41f592007-07-09 18:51:59 +02008397 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
8398 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008399#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07008400 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01008401 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008402 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008403 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008404 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07008405 rq->cpu = i;
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04008406 rq->online = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008407 rq->migration_thread = NULL;
8408 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01008409 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008410#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01008411 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008412 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008413 }
8414
Peter Williams2dd73a42006-06-27 02:54:34 -07008415 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008416
Avi Kivitye107be32007-07-26 13:40:43 +02008417#ifdef CONFIG_PREEMPT_NOTIFIERS
8418 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
8419#endif
8420
Christoph Lameterc9819f42006-12-10 02:20:25 -08008421#ifdef CONFIG_SMP
Carlos R. Mafra962cf362008-05-15 11:15:37 -03008422 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
Christoph Lameterc9819f42006-12-10 02:20:25 -08008423#endif
8424
Heiko Carstensb50f60c2006-07-30 03:03:52 -07008425#ifdef CONFIG_RT_MUTEXES
8426 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
8427#endif
8428
Linus Torvalds1da177e2005-04-16 15:20:36 -07008429 /*
8430 * The boot idle thread does lazy MMU switching as well:
8431 */
8432 atomic_inc(&init_mm.mm_count);
8433 enter_lazy_tlb(&init_mm, current);
8434
8435 /*
8436 * Make us the idle thread. Technically, schedule() should not be
8437 * called from this thread, however somewhere below it might be,
8438 * but because we are the idle thread, we just pick up running again
8439 * when this runqueue becomes "idle".
8440 */
8441 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02008442 /*
8443 * During early bootup we pretend to be a normal task:
8444 */
8445 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01008446
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10308447 /* Allocate the nohz_cpu_mask if CONFIG_CPUMASK_OFFSTACK */
8448 alloc_bootmem_cpumask_var(&nohz_cpu_mask);
Rusty Russellbf4d83f2008-11-25 09:57:51 +10308449#ifdef CONFIG_SMP
Rusty Russell7d1e6a92008-11-25 02:35:09 +10308450#ifdef CONFIG_NO_HZ
8451 alloc_bootmem_cpumask_var(&nohz.cpu_mask);
8452#endif
Rusty Russelldcc30a32008-11-25 02:35:12 +10308453 alloc_bootmem_cpumask_var(&cpu_isolated_map);
Rusty Russellbf4d83f2008-11-25 09:57:51 +10308454#endif /* SMP */
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10308455
Ingo Molnar6892b752008-02-13 14:02:36 +01008456 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008457}
8458
8459#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
8460void __might_sleep(char *file, int line)
8461{
Ingo Molnar48f24c42006-07-03 00:25:40 -07008462#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07008463 static unsigned long prev_jiffy; /* ratelimiting */
8464
Ingo Molnaraef745f2008-08-28 11:34:43 +02008465 if ((!in_atomic() && !irqs_disabled()) ||
8466 system_state != SYSTEM_RUNNING || oops_in_progress)
8467 return;
8468 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
8469 return;
8470 prev_jiffy = jiffies;
8471
8472 printk(KERN_ERR
8473 "BUG: sleeping function called from invalid context at %s:%d\n",
8474 file, line);
8475 printk(KERN_ERR
8476 "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
8477 in_atomic(), irqs_disabled(),
8478 current->pid, current->comm);
8479
8480 debug_show_held_locks(current);
8481 if (irqs_disabled())
8482 print_irqtrace_events(current);
8483 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07008484#endif
8485}
8486EXPORT_SYMBOL(__might_sleep);
8487#endif
8488
8489#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008490static void normalize_task(struct rq *rq, struct task_struct *p)
8491{
8492 int on_rq;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02008493
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008494 update_rq_clock(rq);
8495 on_rq = p->se.on_rq;
8496 if (on_rq)
8497 deactivate_task(rq, p, 0);
8498 __setscheduler(rq, p, SCHED_NORMAL, 0);
8499 if (on_rq) {
8500 activate_task(rq, p, 0);
8501 resched_task(rq->curr);
8502 }
8503}
8504
Linus Torvalds1da177e2005-04-16 15:20:36 -07008505void normalize_rt_tasks(void)
8506{
Ingo Molnara0f98a12007-06-17 18:37:45 +02008507 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008508 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07008509 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008510
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008511 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008512 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02008513 /*
8514 * Only normalize user tasks:
8515 */
8516 if (!p->mm)
8517 continue;
8518
Ingo Molnardd41f592007-07-09 18:51:59 +02008519 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008520#ifdef CONFIG_SCHEDSTATS
8521 p->se.wait_start = 0;
8522 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02008523 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02008524#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02008525
8526 if (!rt_task(p)) {
8527 /*
8528 * Renice negative nice level userspace
8529 * tasks back to 0:
8530 */
8531 if (TASK_NICE(p) < 0 && p->mm)
8532 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008533 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02008534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008535
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008536 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07008537 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008538
Ingo Molnar178be792007-10-15 17:00:18 +02008539 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02008540
Ingo Molnarb29739f2006-06-27 02:54:51 -07008541 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008542 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02008543 } while_each_thread(g, p);
8544
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01008545 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008546}
8547
8548#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07008549
8550#ifdef CONFIG_IA64
8551/*
8552 * These functions are only useful for the IA64 MCA handling.
8553 *
8554 * They can only be called when the whole system has been
8555 * stopped - every CPU needs to be quiescent, and no scheduling
8556 * activity can take place. Using them for anything else would
8557 * be a serious bug, and as a result, they aren't even visible
8558 * under any other configuration.
8559 */
8560
8561/**
8562 * curr_task - return the current task for a given cpu.
8563 * @cpu: the processor in question.
8564 *
8565 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8566 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008567struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008568{
8569 return cpu_curr(cpu);
8570}
8571
8572/**
8573 * set_curr_task - set the current task for a given cpu.
8574 * @cpu: the processor in question.
8575 * @p: the task pointer to set.
8576 *
8577 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008578 * are serviced on a separate stack. It allows the architecture to switch the
8579 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07008580 * must be called with all CPU's synchronized, and interrupts disabled, the
8581 * and caller must save the original value of the current task (see
8582 * curr_task() above) and restore that value before reenabling interrupts and
8583 * re-starting the system.
8584 *
8585 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8586 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07008587void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07008588{
8589 cpu_curr(cpu) = p;
8590}
8591
8592#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008593
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008594#ifdef CONFIG_FAIR_GROUP_SCHED
8595static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008596{
8597 int i;
8598
8599 for_each_possible_cpu(i) {
8600 if (tg->cfs_rq)
8601 kfree(tg->cfs_rq[i]);
8602 if (tg->se)
8603 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008604 }
8605
8606 kfree(tg->cfs_rq);
8607 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008608}
8609
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008610static
8611int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008612{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008613 struct cfs_rq *cfs_rq;
Li Zefaneab17222008-10-29 17:03:22 +08008614 struct sched_entity *se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008615 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008616 int i;
8617
Mike Travis434d53b2008-04-04 18:11:04 -07008618 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008619 if (!tg->cfs_rq)
8620 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008621 tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008622 if (!tg->se)
8623 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008624
8625 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008626
8627 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008628 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008629
Li Zefaneab17222008-10-29 17:03:22 +08008630 cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
8631 GFP_KERNEL, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008632 if (!cfs_rq)
8633 goto err;
8634
Li Zefaneab17222008-10-29 17:03:22 +08008635 se = kzalloc_node(sizeof(struct sched_entity),
8636 GFP_KERNEL, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008637 if (!se)
8638 goto err;
8639
Li Zefaneab17222008-10-29 17:03:22 +08008640 init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent->se[i]);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008641 }
8642
8643 return 1;
8644
8645 err:
8646 return 0;
8647}
8648
8649static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8650{
8651 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
8652 &cpu_rq(cpu)->leaf_cfs_rq_list);
8653}
8654
8655static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8656{
8657 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
8658}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008659#else /* !CONFG_FAIR_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008660static inline void free_fair_sched_group(struct task_group *tg)
8661{
8662}
8663
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008664static inline
8665int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008666{
8667 return 1;
8668}
8669
8670static inline void register_fair_sched_group(struct task_group *tg, int cpu)
8671{
8672}
8673
8674static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
8675{
8676}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008677#endif /* CONFIG_FAIR_GROUP_SCHED */
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008678
8679#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008680static void free_rt_sched_group(struct task_group *tg)
8681{
8682 int i;
8683
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008684 destroy_rt_bandwidth(&tg->rt_bandwidth);
8685
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008686 for_each_possible_cpu(i) {
8687 if (tg->rt_rq)
8688 kfree(tg->rt_rq[i]);
8689 if (tg->rt_se)
8690 kfree(tg->rt_se[i]);
8691 }
8692
8693 kfree(tg->rt_rq);
8694 kfree(tg->rt_se);
8695}
8696
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008697static
8698int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008699{
8700 struct rt_rq *rt_rq;
Li Zefaneab17222008-10-29 17:03:22 +08008701 struct sched_rt_entity *rt_se;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008702 struct rq *rq;
8703 int i;
8704
Mike Travis434d53b2008-04-04 18:11:04 -07008705 tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008706 if (!tg->rt_rq)
8707 goto err;
Mike Travis434d53b2008-04-04 18:11:04 -07008708 tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008709 if (!tg->rt_se)
8710 goto err;
8711
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008712 init_rt_bandwidth(&tg->rt_bandwidth,
8713 ktime_to_ns(def_rt_bandwidth.rt_period), 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008714
8715 for_each_possible_cpu(i) {
8716 rq = cpu_rq(i);
8717
Li Zefaneab17222008-10-29 17:03:22 +08008718 rt_rq = kzalloc_node(sizeof(struct rt_rq),
8719 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008720 if (!rt_rq)
8721 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008722
Li Zefaneab17222008-10-29 17:03:22 +08008723 rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
8724 GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008725 if (!rt_se)
8726 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008727
Li Zefaneab17222008-10-29 17:03:22 +08008728 init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent->rt_se[i]);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008729 }
8730
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008731 return 1;
8732
8733 err:
8734 return 0;
8735}
8736
8737static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8738{
8739 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
8740 &cpu_rq(cpu)->leaf_rt_rq_list);
8741}
8742
8743static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8744{
8745 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
8746}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008747#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008748static inline void free_rt_sched_group(struct task_group *tg)
8749{
8750}
8751
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008752static inline
8753int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008754{
8755 return 1;
8756}
8757
8758static inline void register_rt_sched_group(struct task_group *tg, int cpu)
8759{
8760}
8761
8762static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
8763{
8764}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008765#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008766
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008767#ifdef CONFIG_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008768static void free_sched_group(struct task_group *tg)
8769{
8770 free_fair_sched_group(tg);
8771 free_rt_sched_group(tg);
8772 kfree(tg);
8773}
8774
8775/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008776struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008777{
8778 struct task_group *tg;
8779 unsigned long flags;
8780 int i;
8781
8782 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
8783 if (!tg)
8784 return ERR_PTR(-ENOMEM);
8785
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008786 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008787 goto err;
8788
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008789 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008790 goto err;
8791
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008792 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008793 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008794 register_fair_sched_group(tg, i);
8795 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008796 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008797 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008798
8799 WARN_ON(!parent); /* root should already exist */
8800
8801 tg->parent = parent;
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008802 INIT_LIST_HEAD(&tg->children);
Zhang, Yanmin09f27242030-08-14 15:56:40 +08008803 list_add_rcu(&tg->siblings, &parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008804 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008805
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008806 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008807
8808err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008809 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008810 return ERR_PTR(-ENOMEM);
8811}
8812
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008813/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008814static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008815{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008816 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008817 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008818}
8819
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008820/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008821void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008822{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008823 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008824 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008825
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008826 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008827 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008828 unregister_fair_sched_group(tg, i);
8829 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008830 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008831 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008832 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008833 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008834
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008835 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008836 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008837}
8838
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008839/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02008840 * The caller of this function should have put the task in its new group
8841 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
8842 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008843 */
8844void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008845{
8846 int on_rq, running;
8847 unsigned long flags;
8848 struct rq *rq;
8849
8850 rq = task_rq_lock(tsk, &flags);
8851
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008852 update_rq_clock(rq);
8853
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01008854 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008855 on_rq = tsk->se.on_rq;
8856
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008857 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008858 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008859 if (unlikely(running))
8860 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008861
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008862 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008863
Peter Zijlstra810b3812008-02-29 15:21:01 -05008864#ifdef CONFIG_FAIR_GROUP_SCHED
8865 if (tsk->sched_class->moved_group)
8866 tsk->sched_class->moved_group(tsk);
8867#endif
8868
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07008869 if (unlikely(running))
8870 tsk->sched_class->set_curr_task(rq);
8871 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02008872 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008873
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008874 task_rq_unlock(rq, &flags);
8875}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008876#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008877
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008878#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008879static void __set_se_shares(struct sched_entity *se, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008880{
8881 struct cfs_rq *cfs_rq = se->cfs_rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008882 int on_rq;
8883
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008884 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008885 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008886 dequeue_entity(cfs_rq, se, 0);
8887
8888 se->load.weight = shares;
Peter Zijlstrae05510d2008-05-05 23:56:17 +02008889 se->load.inv_weight = 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008890
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008891 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008892 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008893}
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008894
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008895static void set_se_shares(struct sched_entity *se, unsigned long shares)
8896{
8897 struct cfs_rq *cfs_rq = se->cfs_rq;
8898 struct rq *rq = cfs_rq->rq;
8899 unsigned long flags;
8900
8901 spin_lock_irqsave(&rq->lock, flags);
8902 __set_se_shares(se, shares);
8903 spin_unlock_irqrestore(&rq->lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008904}
8905
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008906static DEFINE_MUTEX(shares_mutex);
8907
Ingo Molnar4cf86d72007-10-15 17:00:14 +02008908int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008909{
8910 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008911 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01008912
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008913 /*
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02008914 * We can't change the weight of the root cgroup.
8915 */
8916 if (!tg->se[0])
8917 return -EINVAL;
8918
Peter Zijlstra18d95a22008-04-19 19:45:00 +02008919 if (shares < MIN_SHARES)
8920 shares = MIN_SHARES;
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008921 else if (shares > MAX_SHARES)
8922 shares = MAX_SHARES;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01008923
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008924 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008925 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008926 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008927
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008928 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008929 for_each_possible_cpu(i)
8930 unregister_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008931 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008932 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008933
8934 /* wait for any ongoing reference to this group to finish */
8935 synchronize_sched();
8936
8937 /*
8938 * Now we are free to modify the group's share on each cpu
8939 * w/o tripping rebalance_share or load_balance_fair.
8940 */
8941 tg->shares = shares;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008942 for_each_possible_cpu(i) {
8943 /*
8944 * force a rebalance
8945 */
8946 cfs_rq_set_shares(tg->cfs_rq[i], 0);
Miao Xiecb4ad1f2008-04-28 12:54:56 +08008947 set_se_shares(tg->se[i], shares);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02008948 }
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01008949
8950 /*
8951 * Enable load balance activity on this group, by inserting it back on
8952 * each cpu's rq->leaf_cfs_rq_list.
8953 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008954 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01008955 for_each_possible_cpu(i)
8956 register_fair_sched_group(tg, i);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02008957 list_add_rcu(&tg->siblings, &tg->parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008958 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008959done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01008960 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02008961 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02008962}
8963
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008964unsigned long sched_group_shares(struct task_group *tg)
8965{
8966 return tg->shares;
8967}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008968#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02008969
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008970#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008971/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008972 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008973 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008974static DEFINE_MUTEX(rt_constraints_mutex);
8975
8976static unsigned long to_ratio(u64 period, u64 runtime)
8977{
8978 if (runtime == RUNTIME_INF)
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008979 return 1ULL << 20;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008980
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008981 return div64_u64(runtime << 20, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008982}
8983
Dhaval Giani521f1a242008-02-28 15:21:56 +05308984/* Must be called with tasklist_lock held */
8985static inline int tg_has_rt_tasks(struct task_group *tg)
8986{
8987 struct task_struct *g, *p;
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008988
Dhaval Giani521f1a242008-02-28 15:21:56 +05308989 do_each_thread(g, p) {
8990 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
8991 return 1;
8992 } while_each_thread(g, p);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008993
Dhaval Giani521f1a242008-02-28 15:21:56 +05308994 return 0;
8995}
8996
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02008997struct rt_schedulable_data {
8998 struct task_group *tg;
8999 u64 rt_period;
9000 u64 rt_runtime;
9001};
9002
9003static int tg_schedulable(struct task_group *tg, void *data)
9004{
9005 struct rt_schedulable_data *d = data;
9006 struct task_group *child;
9007 unsigned long total, sum = 0;
9008 u64 period, runtime;
9009
9010 period = ktime_to_ns(tg->rt_bandwidth.rt_period);
9011 runtime = tg->rt_bandwidth.rt_runtime;
9012
9013 if (tg == d->tg) {
9014 period = d->rt_period;
9015 runtime = d->rt_runtime;
9016 }
9017
Peter Zijlstra4653f802008-09-23 15:33:44 +02009018 /*
9019 * Cannot have more runtime than the period.
9020 */
9021 if (runtime > period && runtime != RUNTIME_INF)
9022 return -EINVAL;
9023
9024 /*
9025 * Ensure we don't starve existing RT tasks.
9026 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009027 if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
9028 return -EBUSY;
9029
9030 total = to_ratio(period, runtime);
9031
Peter Zijlstra4653f802008-09-23 15:33:44 +02009032 /*
9033 * Nobody can have more than the global setting allows.
9034 */
9035 if (total > to_ratio(global_rt_period(), global_rt_runtime()))
9036 return -EINVAL;
9037
9038 /*
9039 * The sum of our children's runtime should not exceed our own.
9040 */
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009041 list_for_each_entry_rcu(child, &tg->children, siblings) {
9042 period = ktime_to_ns(child->rt_bandwidth.rt_period);
9043 runtime = child->rt_bandwidth.rt_runtime;
9044
9045 if (child == d->tg) {
9046 period = d->rt_period;
9047 runtime = d->rt_runtime;
9048 }
9049
9050 sum += to_ratio(period, runtime);
9051 }
9052
9053 if (sum > total)
9054 return -EINVAL;
9055
9056 return 0;
9057}
9058
9059static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
9060{
9061 struct rt_schedulable_data data = {
9062 .tg = tg,
9063 .rt_period = period,
9064 .rt_runtime = runtime,
9065 };
9066
9067 return walk_tg_tree(tg_schedulable, tg_nop, &data);
9068}
9069
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009070static int tg_set_bandwidth(struct task_group *tg,
9071 u64 rt_period, u64 rt_runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009072{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009073 int i, err = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009074
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009075 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05309076 read_lock(&tasklist_lock);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009077 err = __rt_schedulable(tg, rt_period, rt_runtime);
9078 if (err)
Dhaval Giani521f1a242008-02-28 15:21:56 +05309079 goto unlock;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009080
9081 spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009082 tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
9083 tg->rt_bandwidth.rt_runtime = rt_runtime;
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009084
9085 for_each_possible_cpu(i) {
9086 struct rt_rq *rt_rq = tg->rt_rq[i];
9087
9088 spin_lock(&rt_rq->rt_runtime_lock);
9089 rt_rq->rt_runtime = rt_runtime;
9090 spin_unlock(&rt_rq->rt_runtime_lock);
9091 }
9092 spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009093 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05309094 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009095 mutex_unlock(&rt_constraints_mutex);
9096
9097 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009098}
9099
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009100int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
9101{
9102 u64 rt_runtime, rt_period;
9103
9104 rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
9105 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
9106 if (rt_runtime_us < 0)
9107 rt_runtime = RUNTIME_INF;
9108
9109 return tg_set_bandwidth(tg, rt_period, rt_runtime);
9110}
9111
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009112long sched_group_rt_runtime(struct task_group *tg)
9113{
9114 u64 rt_runtime_us;
9115
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009116 if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009117 return -1;
9118
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009119 rt_runtime_us = tg->rt_bandwidth.rt_runtime;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009120 do_div(rt_runtime_us, NSEC_PER_USEC);
9121 return rt_runtime_us;
9122}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009123
9124int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
9125{
9126 u64 rt_runtime, rt_period;
9127
9128 rt_period = (u64)rt_period_us * NSEC_PER_USEC;
9129 rt_runtime = tg->rt_bandwidth.rt_runtime;
9130
Raistlin619b0482008-06-26 18:54:09 +02009131 if (rt_period == 0)
9132 return -EINVAL;
9133
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009134 return tg_set_bandwidth(tg, rt_period, rt_runtime);
9135}
9136
9137long sched_group_rt_period(struct task_group *tg)
9138{
9139 u64 rt_period_us;
9140
9141 rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
9142 do_div(rt_period_us, NSEC_PER_USEC);
9143 return rt_period_us;
9144}
9145
9146static int sched_rt_global_constraints(void)
9147{
Peter Zijlstra4653f802008-09-23 15:33:44 +02009148 u64 runtime, period;
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009149 int ret = 0;
9150
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07009151 if (sysctl_sched_rt_period <= 0)
9152 return -EINVAL;
9153
Peter Zijlstra4653f802008-09-23 15:33:44 +02009154 runtime = global_rt_runtime();
9155 period = global_rt_period();
9156
9157 /*
9158 * Sanity check on the sysctl variables.
9159 */
9160 if (runtime > period && runtime != RUNTIME_INF)
9161 return -EINVAL;
Peter Zijlstra10b612f2008-06-19 14:22:27 +02009162
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009163 mutex_lock(&rt_constraints_mutex);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009164 read_lock(&tasklist_lock);
Peter Zijlstra4653f802008-09-23 15:33:44 +02009165 ret = __rt_schedulable(NULL, 0, 0);
Peter Zijlstra9a7e0b12008-08-19 12:33:06 +02009166 read_unlock(&tasklist_lock);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009167 mutex_unlock(&rt_constraints_mutex);
9168
9169 return ret;
9170}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009171#else /* !CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009172static int sched_rt_global_constraints(void)
9173{
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009174 unsigned long flags;
9175 int i;
9176
Hiroshi Shimamotoec5d4982008-09-10 17:00:19 -07009177 if (sysctl_sched_rt_period <= 0)
9178 return -EINVAL;
9179
Peter Zijlstraac086bc2008-04-19 19:44:58 +02009180 spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
9181 for_each_possible_cpu(i) {
9182 struct rt_rq *rt_rq = &cpu_rq(i)->rt;
9183
9184 spin_lock(&rt_rq->rt_runtime_lock);
9185 rt_rq->rt_runtime = global_rt_runtime();
9186 spin_unlock(&rt_rq->rt_runtime_lock);
9187 }
9188 spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
9189
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009190 return 0;
9191}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009192#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009193
9194int sched_rt_handler(struct ctl_table *table, int write,
9195 struct file *filp, void __user *buffer, size_t *lenp,
9196 loff_t *ppos)
9197{
9198 int ret;
9199 int old_period, old_runtime;
9200 static DEFINE_MUTEX(mutex);
9201
9202 mutex_lock(&mutex);
9203 old_period = sysctl_sched_rt_period;
9204 old_runtime = sysctl_sched_rt_runtime;
9205
9206 ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
9207
9208 if (!ret && write) {
9209 ret = sched_rt_global_constraints();
9210 if (ret) {
9211 sysctl_sched_rt_period = old_period;
9212 sysctl_sched_rt_runtime = old_runtime;
9213 } else {
9214 def_rt_bandwidth.rt_runtime = global_rt_runtime();
9215 def_rt_bandwidth.rt_period =
9216 ns_to_ktime(global_rt_period());
9217 }
9218 }
9219 mutex_unlock(&mutex);
9220
9221 return ret;
9222}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009223
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009224#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009225
9226/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02009227static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009228{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009229 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
9230 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009231}
9232
9233static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02009234cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009235{
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009236 struct task_group *tg, *parent;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009237
Paul Menage2b01dfe2007-10-24 18:23:50 +02009238 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009239 /* This is early initialization for the top cgroup */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009240 return &init_task_group.css;
9241 }
9242
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02009243 parent = cgroup_tg(cgrp->parent);
9244 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009245 if (IS_ERR(tg))
9246 return ERR_PTR(-ENOMEM);
9247
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009248 return &tg->css;
9249}
9250
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009251static void
9252cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009253{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009254 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009255
9256 sched_destroy_group(tg);
9257}
9258
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009259static int
9260cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
9261 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009262{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009263#ifdef CONFIG_RT_GROUP_SCHED
9264 /* Don't accept realtime tasks when there is no way for them to run */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009265 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009266 return -EINVAL;
9267#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009268 /* We don't support RT-tasks being in separate groups */
9269 if (tsk->sched_class != &fair_sched_class)
9270 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01009271#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009272
9273 return 0;
9274}
9275
9276static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02009277cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009278 struct cgroup *old_cont, struct task_struct *tsk)
9279{
9280 sched_move_task(tsk);
9281}
9282
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009283#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagef4c753b2008-04-29 00:59:56 -07009284static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
Paul Menage2b01dfe2007-10-24 18:23:50 +02009285 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009286{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009287 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009288}
9289
Paul Menagef4c753b2008-04-29 00:59:56 -07009290static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009291{
Paul Menage2b01dfe2007-10-24 18:23:50 +02009292 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009293
9294 return (u64) tg->shares;
9295}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009296#endif /* CONFIG_FAIR_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009297
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009298#ifdef CONFIG_RT_GROUP_SCHED
Mirco Tischler0c708142008-05-14 16:05:46 -07009299static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
Paul Menage06ecb272008-04-29 01:00:06 -07009300 s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009301{
Paul Menage06ecb272008-04-29 01:00:06 -07009302 return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009303}
9304
Paul Menage06ecb272008-04-29 01:00:06 -07009305static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009306{
Paul Menage06ecb272008-04-29 01:00:06 -07009307 return sched_group_rt_runtime(cgroup_tg(cgrp));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009308}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009309
9310static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
9311 u64 rt_period_us)
9312{
9313 return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
9314}
9315
9316static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
9317{
9318 return sched_group_rt_period(cgroup_tg(cgrp));
9319}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02009320#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009321
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009322static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009323#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009324 {
9325 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07009326 .read_u64 = cpu_shares_read_u64,
9327 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009328 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009329#endif
9330#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009331 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01009332 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07009333 .read_s64 = cpu_rt_runtime_read,
9334 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01009335 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009336 {
9337 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07009338 .read_u64 = cpu_rt_period_read_uint,
9339 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02009340 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009341#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009342};
9343
9344static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
9345{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01009346 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009347}
9348
9349struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01009350 .name = "cpu",
9351 .create = cpu_cgroup_create,
9352 .destroy = cpu_cgroup_destroy,
9353 .can_attach = cpu_cgroup_can_attach,
9354 .attach = cpu_cgroup_attach,
9355 .populate = cpu_cgroup_populate,
9356 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07009357 .early_init = 1,
9358};
9359
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01009360#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009361
9362#ifdef CONFIG_CGROUP_CPUACCT
9363
9364/*
9365 * CPU accounting code for task groups.
9366 *
9367 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
9368 * (balbir@in.ibm.com).
9369 */
9370
Bharata B Rao934352f2008-11-10 20:41:13 +05309371/* track cpu usage of a group of tasks and its child groups */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009372struct cpuacct {
9373 struct cgroup_subsys_state css;
9374 /* cpuusage holds pointer to a u64-type object on every cpu */
9375 u64 *cpuusage;
Bharata B Rao934352f2008-11-10 20:41:13 +05309376 struct cpuacct *parent;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009377};
9378
9379struct cgroup_subsys cpuacct_subsys;
9380
9381/* return cpu accounting group corresponding to this container */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309382static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009383{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309384 return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009385 struct cpuacct, css);
9386}
9387
9388/* return cpu accounting group to which this task belongs */
9389static inline struct cpuacct *task_ca(struct task_struct *tsk)
9390{
9391 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
9392 struct cpuacct, css);
9393}
9394
9395/* create a new cpu accounting group */
9396static struct cgroup_subsys_state *cpuacct_create(
Dhaval Giani32cd7562008-02-29 10:02:43 +05309397 struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009398{
9399 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
9400
9401 if (!ca)
9402 return ERR_PTR(-ENOMEM);
9403
9404 ca->cpuusage = alloc_percpu(u64);
9405 if (!ca->cpuusage) {
9406 kfree(ca);
9407 return ERR_PTR(-ENOMEM);
9408 }
9409
Bharata B Rao934352f2008-11-10 20:41:13 +05309410 if (cgrp->parent)
9411 ca->parent = cgroup_ca(cgrp->parent);
9412
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009413 return &ca->css;
9414}
9415
9416/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01009417static void
Dhaval Giani32cd7562008-02-29 10:02:43 +05309418cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009419{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309420 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009421
9422 free_percpu(ca->cpuusage);
9423 kfree(ca);
9424}
9425
9426/* return total cpu usage (in nanoseconds) of a group */
Dhaval Giani32cd7562008-02-29 10:02:43 +05309427static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009428{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309429 struct cpuacct *ca = cgroup_ca(cgrp);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009430 u64 totalcpuusage = 0;
9431 int i;
9432
9433 for_each_possible_cpu(i) {
9434 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9435
9436 /*
9437 * Take rq->lock to make 64-bit addition safe on 32-bit
9438 * platforms.
9439 */
9440 spin_lock_irq(&cpu_rq(i)->lock);
9441 totalcpuusage += *cpuusage;
9442 spin_unlock_irq(&cpu_rq(i)->lock);
9443 }
9444
9445 return totalcpuusage;
9446}
9447
Dhaval Giani0297b802008-02-29 10:02:44 +05309448static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
9449 u64 reset)
9450{
9451 struct cpuacct *ca = cgroup_ca(cgrp);
9452 int err = 0;
9453 int i;
9454
9455 if (reset) {
9456 err = -EINVAL;
9457 goto out;
9458 }
9459
9460 for_each_possible_cpu(i) {
9461 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
9462
9463 spin_lock_irq(&cpu_rq(i)->lock);
9464 *cpuusage = 0;
9465 spin_unlock_irq(&cpu_rq(i)->lock);
9466 }
9467out:
9468 return err;
9469}
9470
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009471static struct cftype files[] = {
9472 {
9473 .name = "usage",
Paul Menagef4c753b2008-04-29 00:59:56 -07009474 .read_u64 = cpuusage_read,
9475 .write_u64 = cpuusage_write,
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009476 },
9477};
9478
Dhaval Giani32cd7562008-02-29 10:02:43 +05309479static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009480{
Dhaval Giani32cd7562008-02-29 10:02:43 +05309481 return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009482}
9483
9484/*
9485 * charge this task's execution time to its accounting group.
9486 *
9487 * called with rq->lock held.
9488 */
9489static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
9490{
9491 struct cpuacct *ca;
Bharata B Rao934352f2008-11-10 20:41:13 +05309492 int cpu;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009493
9494 if (!cpuacct_subsys.active)
9495 return;
9496
Bharata B Rao934352f2008-11-10 20:41:13 +05309497 cpu = task_cpu(tsk);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009498 ca = task_ca(tsk);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009499
Bharata B Rao934352f2008-11-10 20:41:13 +05309500 for (; ca; ca = ca->parent) {
9501 u64 *cpuusage = percpu_ptr(ca->cpuusage, cpu);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01009502 *cpuusage += cputime;
9503 }
9504}
9505
9506struct cgroup_subsys cpuacct_subsys = {
9507 .name = "cpuacct",
9508 .create = cpuacct_create,
9509 .destroy = cpuacct_destroy,
9510 .populate = cpuacct_populate,
9511 .subsys_id = cpuacct_subsys_id,
9512};
9513#endif /* CONFIG_CGROUP_CPUACCT */