blob: 7c5efad78c09aa0700b33e23929dbf40f0085040 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * kernel/sched.c
3 *
4 * Kernel scheduler and related syscalls
5 *
6 * Copyright (C) 1991-2002 Linus Torvalds
7 *
8 * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and
9 * make semaphores SMP safe
10 * 1998-11-19 Implemented schedule_timeout() and related stuff
11 * by Andrea Arcangeli
12 * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
13 * hybrid priority-list and round-robin design with
14 * an array-switch method of distributing timeslices
15 * and per-CPU runqueues. Cleanups and useful suggestions
16 * by Davide Libenzi, preemptible kernel bits by Robert Love.
17 * 2003-09-03 Interactivity tuning by Con Kolivas.
18 * 2004-04-02 Scheduler domains code by Nick Piggin
Ingo Molnarc31f2e82007-07-09 18:52:01 +020019 * 2007-04-15 Work begun on replacing all interactivity tuning with a
20 * fair scheduling design by Con Kolivas.
21 * 2007-05-05 Load balancing (smp-nice) and other improvements
22 * by Peter Williams
23 * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith
24 * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri
Ingo Molnarb9131762008-01-25 21:08:19 +010025 * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins,
26 * Thomas Gleixner, Mike Kravetz
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
28
29#include <linux/mm.h>
30#include <linux/module.h>
31#include <linux/nmi.h>
32#include <linux/init.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020033#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/highmem.h>
35#include <linux/smp_lock.h>
36#include <asm/mmu_context.h>
37#include <linux/interrupt.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080038#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/completion.h>
40#include <linux/kernel_stat.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070041#include <linux/debug_locks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/security.h>
43#include <linux/notifier.h>
44#include <linux/profile.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080045#include <linux/freezer.h>
akpm@osdl.org198e2f12006-01-12 01:05:30 -080046#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/blkdev.h>
48#include <linux/delay.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070049#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/smp.h>
51#include <linux/threads.h>
52#include <linux/timer.h>
53#include <linux/rcupdate.h>
54#include <linux/cpu.h>
55#include <linux/cpuset.h>
56#include <linux/percpu.h>
57#include <linux/kthread.h>
58#include <linux/seq_file.h>
Nick Piggine692ab52007-07-26 13:40:43 +020059#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/syscalls.h>
61#include <linux/times.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070062#include <linux/tsacct_kern.h>
bibo maoc6fd91f2006-03-26 01:38:20 -080063#include <linux/kprobes.h>
Shailabh Nagar0ff92242006-07-14 00:24:37 -070064#include <linux/delayacct.h>
Eric Dumazet5517d862007-05-08 00:32:57 -070065#include <linux/reciprocal_div.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020066#include <linux/unistd.h>
Jens Axboef5ff8422007-09-21 09:19:54 +020067#include <linux/pagemap.h>
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +010068#include <linux/hrtimer.h>
Reynes Philippe30914a52008-03-17 16:19:05 -070069#include <linux/tick.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Eric Dumazet5517d862007-05-08 00:32:57 -070071#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020072#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/*
Alexey Dobriyanb035b6d2007-02-10 01:45:10 -080075 * Scheduler clock - returns current time in nanosec units.
76 * This is default implementation.
77 * Architectures and sub-architectures can override this.
78 */
79unsigned long long __attribute__((weak)) sched_clock(void)
80{
Eric Dumazetd6322fa2007-11-09 22:39:38 +010081 return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ);
Alexey Dobriyanb035b6d2007-02-10 01:45:10 -080082}
83
84/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * Convert user-nice values [ -20 ... 0 ... 19 ]
86 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
87 * and back.
88 */
89#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
90#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
91#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
92
93/*
94 * 'User priority' is the nice value converted to something we
95 * can work with better when scaling various scheduler parameters,
96 * it's a [ 0 ... 39 ] range.
97 */
98#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
99#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
100#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
101
102/*
Ingo Molnard7876a02008-01-25 21:08:19 +0100103 * Helpers for converting nanosecond timing to jiffy resolution
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
Eric Dumazetd6322fa2007-11-09 22:39:38 +0100105#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200107#define NICE_0_LOAD SCHED_LOAD_SCALE
108#define NICE_0_SHIFT SCHED_LOAD_SHIFT
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/*
111 * These are the 'tuning knobs' of the scheduler:
112 *
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200113 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * Timeslices get refilled after they expire.
115 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700117
Eric Dumazet5517d862007-05-08 00:32:57 -0700118#ifdef CONFIG_SMP
119/*
120 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
121 * Since cpu_power is a 'constant', we can use a reciprocal divide.
122 */
123static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
124{
125 return reciprocal_divide(load, sg->reciprocal_cpu_power);
126}
127
128/*
129 * Each time a sched group cpu_power is changed,
130 * we must compute its reciprocal value
131 */
132static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
133{
134 sg->__cpu_power += val;
135 sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
136}
137#endif
138
Ingo Molnare05606d2007-07-09 18:51:59 +0200139static inline int rt_policy(int policy)
140{
141 if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR))
142 return 1;
143 return 0;
144}
145
146static inline int task_has_rt_policy(struct task_struct *p)
147{
148 return rt_policy(p->policy);
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200152 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200154struct rt_prio_array {
155 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
156 struct list_head queue[MAX_RT_PRIO];
157};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100159#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200160
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700161#include <linux/cgroup.h>
162
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200163struct cfs_rq;
164
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100165static LIST_HEAD(task_groups);
166
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200167/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200168struct task_group {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100169#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700170 struct cgroup_subsys_state css;
171#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100172
173#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200174 /* schedulable entities of this group on each cpu */
175 struct sched_entity **se;
176 /* runqueue "owned" by this group on each cpu */
177 struct cfs_rq **cfs_rq;
178 unsigned long shares;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100179#endif
180
181#ifdef CONFIG_RT_GROUP_SCHED
182 struct sched_rt_entity **rt_se;
183 struct rt_rq **rt_rq;
184
185 u64 rt_runtime;
186#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100187
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100188 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100189 struct list_head list;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200190};
191
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100192#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200193/* Default task group's sched entity on each cpu */
194static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
195/* Default task group's cfs_rq on each cpu */
196static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
197
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100198static struct sched_entity *init_sched_entity_p[NR_CPUS];
199static struct cfs_rq *init_cfs_rq_p[NR_CPUS];
200#endif
201
202#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100203static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
204static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
205
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100206static struct sched_rt_entity *init_sched_rt_entity_p[NR_CPUS];
207static struct rt_rq *init_rt_rq_p[NR_CPUS];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100208#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100209
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100210/* task_group_lock serializes add/remove of task groups and also changes to
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100211 * a task group's cpu shares.
212 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100213static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100214
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100215/* doms_cur_mutex serializes access to doms_cur[] array */
216static DEFINE_MUTEX(doms_cur_mutex);
217
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100218#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100219#ifdef CONFIG_USER_SCHED
Ingo Molnar0eab9142008-01-25 21:08:19 +0100220# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200221#else
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100222# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200223#endif
224
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100225static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100226#endif
227
228/* Default task group.
229 * Every task in system belong to this group at bootup.
230 */
231struct task_group init_task_group = {
232#ifdef CONFIG_FAIR_GROUP_SCHED
233 .se = init_sched_entity_p,
234 .cfs_rq = init_cfs_rq_p,
235#endif
236
237#ifdef CONFIG_RT_GROUP_SCHED
238 .rt_se = init_sched_rt_entity_p,
239 .rt_rq = init_rt_rq_p,
240#endif
241};
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200242
243/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200244static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200245{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200246 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200247
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100248#ifdef CONFIG_USER_SCHED
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200249 tg = p->user->tg;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100250#elif defined(CONFIG_CGROUP_SCHED)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700251 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
252 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200253#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100254 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200255#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200256 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200257}
258
259/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100260static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200261{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100262#ifdef CONFIG_FAIR_GROUP_SCHED
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100263 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
264 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100265#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100266
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100267#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100268 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
269 p->rt.parent = task_group(p)->rt_se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100270#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200271}
272
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100273static inline void lock_doms_cur(void)
274{
275 mutex_lock(&doms_cur_mutex);
276}
277
278static inline void unlock_doms_cur(void)
279{
280 mutex_unlock(&doms_cur_mutex);
281}
282
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200283#else
284
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100285static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100286static inline void lock_doms_cur(void) { }
287static inline void unlock_doms_cur(void) { }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200288
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100289#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200290
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200291/* CFS-related fields in a runqueue */
292struct cfs_rq {
293 struct load_weight load;
294 unsigned long nr_running;
295
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200296 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200297 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200298
299 struct rb_root tasks_timeline;
300 struct rb_node *rb_leftmost;
301 struct rb_node *rb_load_balance_curr;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200302 /* 'curr' points to currently running entity on this cfs_rq.
303 * It is set to NULL otherwise (i.e when none are currently running).
304 */
Peter Zijlstraaa2ac252008-03-14 21:12:12 +0100305 struct sched_entity *curr, *next;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200306
307 unsigned long nr_spread_over;
308
Ingo Molnar62160e3f2007-10-15 17:00:03 +0200309#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200310 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
311
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100312 /*
313 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200314 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
315 * (like users, containers etc.)
316 *
317 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
318 * list is used during load balance.
319 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100320 struct list_head leaf_cfs_rq_list;
321 struct task_group *tg; /* group that "owns" this runqueue */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200322#endif
323};
324
325/* Real-Time classes' related field in a runqueue: */
326struct rt_rq {
327 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100328 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100329#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100330 int highest_prio; /* highest queued rt task prio */
331#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100332#ifdef CONFIG_SMP
Gregory Haskins73fe6aae2008-01-25 21:08:07 +0100333 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc12008-01-25 21:08:12 +0100334 int overloaded;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100335#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100336 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100337 u64 rt_time;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100338
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100339#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100340 unsigned long rt_nr_boosted;
341
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100342 struct rq *rq;
343 struct list_head leaf_rt_rq_list;
344 struct task_group *tg;
345 struct sched_rt_entity *rt_se;
346#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200347};
348
Gregory Haskins57d885f2008-01-25 21:08:18 +0100349#ifdef CONFIG_SMP
350
351/*
352 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100353 * variables. Each exclusive cpuset essentially defines an island domain by
354 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100355 * exclusive cpuset is created, we also create and attach a new root-domain
356 * object.
357 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100358 */
359struct root_domain {
360 atomic_t refcount;
361 cpumask_t span;
362 cpumask_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100363
Ingo Molnar0eab9142008-01-25 21:08:19 +0100364 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100365 * The "RT overload" flag: it gets set if a CPU has more than
366 * one runnable RT task.
367 */
368 cpumask_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100369 atomic_t rto_count;
Gregory Haskins57d885f2008-01-25 21:08:18 +0100370};
371
Gregory Haskinsdc938522008-01-25 21:08:26 +0100372/*
373 * By default the system creates a single root-domain with all cpus as
374 * members (mimicking the global state we have today).
375 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100376static struct root_domain def_root_domain;
377
378#endif
379
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200380/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 * This is the main, per-CPU runqueue data structure.
382 *
383 * Locking rule: those places that want to lock multiple runqueues
384 * (such as the load balancing or the thread migration code), lock
385 * acquire operations must be ordered by ascending &runqueue.
386 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700387struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200388 /* runqueue lock: */
389 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 /*
392 * nr_running and cpu_load should be in the same cacheline because
393 * remote CPUs use both these fields when doing load calculation.
394 */
395 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200396 #define CPU_LOAD_IDX_MAX 5
397 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700398 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700399#ifdef CONFIG_NO_HZ
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200400 unsigned long last_tick_seen;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700401 unsigned char in_nohz_recently;
402#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200403 /* capture load from *all* tasks on this cpu: */
404 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200405 unsigned long nr_load_updates;
406 u64 nr_switches;
407
408 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100409 struct rt_rq rt;
410 u64 rt_period_expire;
Peter Zijlstra48d5e252008-01-25 21:08:31 +0100411 int rt_throttled;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100412
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200413#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200414 /* list of leaf cfs_rq on this cpu: */
415 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100416#endif
417#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100418 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 /*
422 * This is part of a global counter where only the total sum
423 * over all CPUs matters. A task can increase this counter on
424 * one CPU and if it got migrated afterwards it may decrease
425 * it on another CPU. Always updated under the runqueue lock:
426 */
427 unsigned long nr_uninterruptible;
428
Ingo Molnar36c8b582006-07-03 00:25:41 -0700429 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800430 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200432
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200433 u64 clock, prev_clock_raw;
434 s64 clock_max_delta;
435
Guillaume Chazaraincc203d22008-01-25 21:08:34 +0100436 unsigned int clock_warps, clock_overflows, clock_underflows;
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200437 u64 idle_clock;
438 unsigned int clock_deep_idle_events;
Ingo Molnar529c7722007-08-10 23:05:11 +0200439 u64 tick_timestamp;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 atomic_t nr_iowait;
442
443#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100444 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 struct sched_domain *sd;
446
447 /* For active balancing */
448 int active_balance;
449 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200450 /* cpu of this runqueue: */
451 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Ingo Molnar36c8b582006-07-03 00:25:41 -0700453 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 struct list_head migration_queue;
455#endif
456
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100457#ifdef CONFIG_SCHED_HRTICK
458 unsigned long hrtick_flags;
459 ktime_t hrtick_expire;
460 struct hrtimer hrtick_timer;
461#endif
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463#ifdef CONFIG_SCHEDSTATS
464 /* latency stats */
465 struct sched_info rq_sched_info;
466
467 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200468 unsigned int yld_exp_empty;
469 unsigned int yld_act_empty;
470 unsigned int yld_both_empty;
471 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200474 unsigned int sched_switch;
475 unsigned int sched_count;
476 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200479 unsigned int ttwu_count;
480 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200481
482 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200483 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484#endif
Ingo Molnarfcb99372006-07-03 00:25:10 -0700485 struct lock_class_key rq_lock_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486};
487
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700488static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Ingo Molnardd41f592007-07-09 18:51:59 +0200490static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
491{
492 rq->curr->sched_class->check_preempt_curr(rq, p);
493}
494
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700495static inline int cpu_of(struct rq *rq)
496{
497#ifdef CONFIG_SMP
498 return rq->cpu;
499#else
500 return 0;
501#endif
502}
503
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200504#ifdef CONFIG_NO_HZ
505static inline bool nohz_on(int cpu)
506{
507 return tick_get_tick_sched(cpu)->nohz_mode != NOHZ_MODE_INACTIVE;
508}
509
510static inline u64 max_skipped_ticks(struct rq *rq)
511{
512 return nohz_on(cpu_of(rq)) ? jiffies - rq->last_tick_seen + 2 : 1;
513}
514
515static inline void update_last_tick_seen(struct rq *rq)
516{
517 rq->last_tick_seen = jiffies;
518}
519#else
520static inline u64 max_skipped_ticks(struct rq *rq)
521{
522 return 1;
523}
524
525static inline void update_last_tick_seen(struct rq *rq)
526{
527}
528#endif
529
Nick Piggin674311d2005-06-25 14:57:27 -0700530/*
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200531 * Update the per-runqueue clock, as finegrained as the platform can give
532 * us, but without assuming monotonicity, etc.:
Ingo Molnar20d315d2007-07-09 18:51:58 +0200533 */
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200534static void __update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200535{
536 u64 prev_raw = rq->prev_clock_raw;
537 u64 now = sched_clock();
538 s64 delta = now - prev_raw;
539 u64 clock = rq->clock;
540
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200541#ifdef CONFIG_SCHED_DEBUG
542 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
543#endif
Ingo Molnar20d315d2007-07-09 18:51:58 +0200544 /*
545 * Protect against sched_clock() occasionally going backwards:
546 */
547 if (unlikely(delta < 0)) {
548 clock++;
549 rq->clock_warps++;
550 } else {
551 /*
552 * Catch too large forward jumps too:
553 */
Guillaume Chazarain15934a32008-04-19 19:44:57 +0200554 u64 max_jump = max_skipped_ticks(rq) * TICK_NSEC;
555 u64 max_time = rq->tick_timestamp + max_jump;
556
557 if (unlikely(clock + delta > max_time)) {
558 if (clock < max_time)
559 clock = max_time;
Ingo Molnar529c7722007-08-10 23:05:11 +0200560 else
561 clock++;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200562 rq->clock_overflows++;
563 } else {
564 if (unlikely(delta > rq->clock_max_delta))
565 rq->clock_max_delta = delta;
566 clock += delta;
567 }
568 }
569
570 rq->prev_clock_raw = now;
571 rq->clock = clock;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200572}
573
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200574static void update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200575{
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200576 if (likely(smp_processor_id() == cpu_of(rq)))
577 __update_rq_clock(rq);
578}
Ingo Molnar20d315d2007-07-09 18:51:58 +0200579
Ingo Molnar20d315d2007-07-09 18:51:58 +0200580/*
Nick Piggin674311d2005-06-25 14:57:27 -0700581 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700582 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700583 *
584 * The domain tree of any CPU may only be accessed from within
585 * preempt-disabled sections.
586 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700587#define for_each_domain(cpu, __sd) \
588 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
591#define this_rq() (&__get_cpu_var(runqueues))
592#define task_rq(p) cpu_rq(task_cpu(p))
593#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
594
Peter Zijlstra48d5e252008-01-25 21:08:31 +0100595unsigned long rt_needs_cpu(int cpu)
596{
597 struct rq *rq = cpu_rq(cpu);
598 u64 delta;
599
600 if (!rq->rt_throttled)
601 return 0;
602
603 if (rq->clock > rq->rt_period_expire)
604 return 1;
605
606 delta = rq->rt_period_expire - rq->clock;
607 do_div(delta, NSEC_PER_SEC / HZ);
608
609 return (unsigned long)delta;
610}
611
Ingo Molnare436d802007-07-19 21:28:35 +0200612/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200613 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
614 */
615#ifdef CONFIG_SCHED_DEBUG
616# define const_debug __read_mostly
617#else
618# define const_debug static const
619#endif
620
621/*
622 * Debugging: various feature bits
623 */
624enum {
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200625 SCHED_FEAT_NEW_FAIR_SLEEPERS = 1,
Ingo Molnar96126332007-11-15 20:57:40 +0100626 SCHED_FEAT_WAKEUP_PREEMPT = 2,
627 SCHED_FEAT_START_DEBIT = 4,
Peter Zijlstra2070ee02008-03-21 16:43:47 +0100628 SCHED_FEAT_HRTICK = 8,
629 SCHED_FEAT_DOUBLE_TICK = 16,
Ingo Molnarb85d0662008-03-16 20:03:22 +0100630 SCHED_FEAT_SYNC_WAKEUPS = 32,
Ingo Molnar1fc8afa2008-03-19 01:39:19 +0100631 SCHED_FEAT_AFFINE_WAKEUPS = 64,
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200632};
633
634const_debug unsigned int sysctl_sched_features =
Ingo Molnar8401f772007-10-18 21:32:55 +0200635 SCHED_FEAT_NEW_FAIR_SLEEPERS * 1 |
Ingo Molnar96126332007-11-15 20:57:40 +0100636 SCHED_FEAT_WAKEUP_PREEMPT * 1 |
Ingo Molnar8401f772007-10-18 21:32:55 +0200637 SCHED_FEAT_START_DEBIT * 1 |
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100638 SCHED_FEAT_HRTICK * 1 |
Ingo Molnarb85d0662008-03-16 20:03:22 +0100639 SCHED_FEAT_DOUBLE_TICK * 0 |
Ingo Molnar1fc8afa2008-03-19 01:39:19 +0100640 SCHED_FEAT_SYNC_WAKEUPS * 0 |
641 SCHED_FEAT_AFFINE_WAKEUPS * 1;
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200642
643#define sched_feat(x) (sysctl_sched_features & SCHED_FEAT_##x)
644
645/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100646 * Number of tasks to iterate in a single balance run.
647 * Limited because this is done with IRQs disabled.
648 */
649const_debug unsigned int sysctl_sched_nr_migrate = 32;
650
651/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100652 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100653 * default: 1s
654 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100655unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100656
Ingo Molnar6892b752008-02-13 14:02:36 +0100657static __read_mostly int scheduler_running;
658
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100659/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100660 * part of the period that we allow rt tasks to run in us.
661 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100662 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100663int sysctl_sched_rt_runtime = 950000;
664
665/*
666 * single value that denotes runtime == period, ie unlimited time.
667 */
668#define RUNTIME_INF ((u64)~0ULL)
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100669
Ingo Molnar27ec4402008-02-28 21:00:21 +0100670static const unsigned long long time_sync_thresh = 100000;
671
672static DEFINE_PER_CPU(unsigned long long, time_offset);
673static DEFINE_PER_CPU(unsigned long long, prev_cpu_time);
674
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100675/*
Ingo Molnar27ec4402008-02-28 21:00:21 +0100676 * Global lock which we take every now and then to synchronize
677 * the CPUs time. This method is not warp-safe, but it's good
678 * enough to synchronize slowly diverging time sources and thus
679 * it's good enough for tracing:
Ingo Molnare436d802007-07-19 21:28:35 +0200680 */
Ingo Molnar27ec4402008-02-28 21:00:21 +0100681static DEFINE_SPINLOCK(time_sync_lock);
682static unsigned long long prev_global_time;
683
684static unsigned long long __sync_cpu_clock(cycles_t time, int cpu)
685{
686 unsigned long flags;
687
688 spin_lock_irqsave(&time_sync_lock, flags);
689
690 if (time < prev_global_time) {
691 per_cpu(time_offset, cpu) += prev_global_time - time;
692 time = prev_global_time;
693 } else {
694 prev_global_time = time;
695 }
696
697 spin_unlock_irqrestore(&time_sync_lock, flags);
698
699 return time;
700}
701
702static unsigned long long __cpu_clock(int cpu)
Ingo Molnare436d802007-07-19 21:28:35 +0200703{
Ingo Molnare436d802007-07-19 21:28:35 +0200704 unsigned long long now;
705 unsigned long flags;
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200706 struct rq *rq;
Ingo Molnare436d802007-07-19 21:28:35 +0200707
Ingo Molnar8ced5f62007-12-07 19:02:47 +0100708 /*
709 * Only call sched_clock() if the scheduler has already been
710 * initialized (some code might call cpu_clock() very early):
711 */
Ingo Molnar6892b752008-02-13 14:02:36 +0100712 if (unlikely(!scheduler_running))
713 return 0;
714
715 local_irq_save(flags);
716 rq = cpu_rq(cpu);
717 update_rq_clock(rq);
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200718 now = rq->clock;
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200719 local_irq_restore(flags);
Ingo Molnare436d802007-07-19 21:28:35 +0200720
721 return now;
722}
Ingo Molnar27ec4402008-02-28 21:00:21 +0100723
724/*
725 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
726 * clock constructed from sched_clock():
727 */
728unsigned long long cpu_clock(int cpu)
729{
730 unsigned long long prev_cpu_time, time, delta_time;
731
732 prev_cpu_time = per_cpu(prev_cpu_time, cpu);
733 time = __cpu_clock(cpu) + per_cpu(time_offset, cpu);
734 delta_time = time-prev_cpu_time;
735
736 if (unlikely(delta_time > time_sync_thresh))
737 time = __sync_cpu_clock(time, cpu);
738
739 return time;
740}
Paul E. McKenneya58f6f22007-10-15 17:00:14 +0200741EXPORT_SYMBOL_GPL(cpu_clock);
Ingo Molnare436d802007-07-19 21:28:35 +0200742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700744# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700746#ifndef finish_arch_switch
747# define finish_arch_switch(prev) do { } while (0)
748#endif
749
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100750static inline int task_current(struct rq *rq, struct task_struct *p)
751{
752 return rq->curr == p;
753}
754
Nick Piggin4866cde2005-06-25 14:57:23 -0700755#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700756static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700757{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100758 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700759}
760
Ingo Molnar70b97a72006-07-03 00:25:42 -0700761static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700762{
763}
764
Ingo Molnar70b97a72006-07-03 00:25:42 -0700765static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700766{
Ingo Molnarda04c032005-09-13 11:17:59 +0200767#ifdef CONFIG_DEBUG_SPINLOCK
768 /* this is a valid case when another task releases the spinlock */
769 rq->lock.owner = current;
770#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700771 /*
772 * If we are tracking spinlock dependencies then we have to
773 * fix up the runqueue lock - which gets 'carried over' from
774 * prev into current:
775 */
776 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
777
Nick Piggin4866cde2005-06-25 14:57:23 -0700778 spin_unlock_irq(&rq->lock);
779}
780
781#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700782static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700783{
784#ifdef CONFIG_SMP
785 return p->oncpu;
786#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100787 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700788#endif
789}
790
Ingo Molnar70b97a72006-07-03 00:25:42 -0700791static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700792{
793#ifdef CONFIG_SMP
794 /*
795 * We can optimise this out completely for !SMP, because the
796 * SMP rebalancing from interrupt is the only thing that cares
797 * here.
798 */
799 next->oncpu = 1;
800#endif
801#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
802 spin_unlock_irq(&rq->lock);
803#else
804 spin_unlock(&rq->lock);
805#endif
806}
807
Ingo Molnar70b97a72006-07-03 00:25:42 -0700808static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700809{
810#ifdef CONFIG_SMP
811 /*
812 * After ->oncpu is cleared, the task can be moved to a different CPU.
813 * We must ensure this doesn't happen until the switch is completely
814 * finished.
815 */
816 smp_wmb();
817 prev->oncpu = 0;
818#endif
819#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
820 local_irq_enable();
821#endif
822}
823#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700826 * __task_rq_lock - lock the runqueue a given task resides on.
827 * Must be called interrupts disabled.
828 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700829static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700830 __acquires(rq->lock)
831{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200832 for (;;) {
833 struct rq *rq = task_rq(p);
834 spin_lock(&rq->lock);
835 if (likely(rq == task_rq(p)))
836 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700837 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700838 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700839}
840
841/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100843 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 * explicitly disabling preemption.
845 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700846static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 __acquires(rq->lock)
848{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700849 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Andi Kleen3a5c3592007-10-15 17:00:14 +0200851 for (;;) {
852 local_irq_save(*flags);
853 rq = task_rq(p);
854 spin_lock(&rq->lock);
855 if (likely(rq == task_rq(p)))
856 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
Alexey Dobriyana9957442007-10-15 17:00:13 +0200861static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700862 __releases(rq->lock)
863{
864 spin_unlock(&rq->lock);
865}
866
Ingo Molnar70b97a72006-07-03 00:25:42 -0700867static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 __releases(rq->lock)
869{
870 spin_unlock_irqrestore(&rq->lock, *flags);
871}
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800874 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200876static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 __acquires(rq->lock)
878{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700879 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 local_irq_disable();
882 rq = this_rq();
883 spin_lock(&rq->lock);
884
885 return rq;
886}
887
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200888/*
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200889 * We are going deep-idle (irqs are disabled):
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200890 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200891void sched_clock_idle_sleep_event(void)
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200892{
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200893 struct rq *rq = cpu_rq(smp_processor_id());
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200894
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200895 spin_lock(&rq->lock);
896 __update_rq_clock(rq);
897 spin_unlock(&rq->lock);
898 rq->clock_deep_idle_events++;
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200899}
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200900EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
901
902/*
903 * We just idled delta nanoseconds (called with irqs disabled):
904 */
905void sched_clock_idle_wakeup_event(u64 delta_ns)
906{
907 struct rq *rq = cpu_rq(smp_processor_id());
908 u64 now = sched_clock();
909
910 rq->idle_clock += delta_ns;
911 /*
912 * Override the previous timestamp and ignore all
913 * sched_clock() deltas that occured while we idled,
914 * and use the PM-provided delta_ns to advance the
915 * rq clock:
916 */
917 spin_lock(&rq->lock);
918 rq->prev_clock_raw = now;
919 rq->clock += delta_ns;
920 spin_unlock(&rq->lock);
Guillaume Chazarain782daee2008-01-25 21:08:33 +0100921 touch_softlockup_watchdog();
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200922}
923EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200924
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100925static void __resched_task(struct task_struct *p, int tif_bit);
926
927static inline void resched_task(struct task_struct *p)
928{
929 __resched_task(p, TIF_NEED_RESCHED);
930}
931
932#ifdef CONFIG_SCHED_HRTICK
933/*
934 * Use HR-timers to deliver accurate preemption points.
935 *
936 * Its all a bit involved since we cannot program an hrt while holding the
937 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
938 * reschedule event.
939 *
940 * When we get rescheduled we reprogram the hrtick_timer outside of the
941 * rq->lock.
942 */
943static inline void resched_hrt(struct task_struct *p)
944{
945 __resched_task(p, TIF_HRTICK_RESCHED);
946}
947
948static inline void resched_rq(struct rq *rq)
949{
950 unsigned long flags;
951
952 spin_lock_irqsave(&rq->lock, flags);
953 resched_task(rq->curr);
954 spin_unlock_irqrestore(&rq->lock, flags);
955}
956
957enum {
958 HRTICK_SET, /* re-programm hrtick_timer */
959 HRTICK_RESET, /* not a new slice */
960};
961
962/*
963 * Use hrtick when:
964 * - enabled by features
965 * - hrtimer is actually high res
966 */
967static inline int hrtick_enabled(struct rq *rq)
968{
969 if (!sched_feat(HRTICK))
970 return 0;
971 return hrtimer_is_hres_active(&rq->hrtick_timer);
972}
973
974/*
975 * Called to set the hrtick timer state.
976 *
977 * called with rq->lock held and irqs disabled
978 */
979static void hrtick_start(struct rq *rq, u64 delay, int reset)
980{
981 assert_spin_locked(&rq->lock);
982
983 /*
984 * preempt at: now + delay
985 */
986 rq->hrtick_expire =
987 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
988 /*
989 * indicate we need to program the timer
990 */
991 __set_bit(HRTICK_SET, &rq->hrtick_flags);
992 if (reset)
993 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
994
995 /*
996 * New slices are called from the schedule path and don't need a
997 * forced reschedule.
998 */
999 if (reset)
1000 resched_hrt(rq->curr);
1001}
1002
1003static void hrtick_clear(struct rq *rq)
1004{
1005 if (hrtimer_active(&rq->hrtick_timer))
1006 hrtimer_cancel(&rq->hrtick_timer);
1007}
1008
1009/*
1010 * Update the timer from the possible pending state.
1011 */
1012static void hrtick_set(struct rq *rq)
1013{
1014 ktime_t time;
1015 int set, reset;
1016 unsigned long flags;
1017
1018 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1019
1020 spin_lock_irqsave(&rq->lock, flags);
1021 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
1022 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
1023 time = rq->hrtick_expire;
1024 clear_thread_flag(TIF_HRTICK_RESCHED);
1025 spin_unlock_irqrestore(&rq->lock, flags);
1026
1027 if (set) {
1028 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
1029 if (reset && !hrtimer_active(&rq->hrtick_timer))
1030 resched_rq(rq);
1031 } else
1032 hrtick_clear(rq);
1033}
1034
1035/*
1036 * High-resolution timer tick.
1037 * Runs from hardirq context with interrupts disabled.
1038 */
1039static enum hrtimer_restart hrtick(struct hrtimer *timer)
1040{
1041 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1042
1043 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1044
1045 spin_lock(&rq->lock);
1046 __update_rq_clock(rq);
1047 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1048 spin_unlock(&rq->lock);
1049
1050 return HRTIMER_NORESTART;
1051}
1052
1053static inline void init_rq_hrtick(struct rq *rq)
1054{
1055 rq->hrtick_flags = 0;
1056 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1057 rq->hrtick_timer.function = hrtick;
1058 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1059}
1060
1061void hrtick_resched(void)
1062{
1063 struct rq *rq;
1064 unsigned long flags;
1065
1066 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1067 return;
1068
1069 local_irq_save(flags);
1070 rq = cpu_rq(smp_processor_id());
1071 hrtick_set(rq);
1072 local_irq_restore(flags);
1073}
1074#else
1075static inline void hrtick_clear(struct rq *rq)
1076{
1077}
1078
1079static inline void hrtick_set(struct rq *rq)
1080{
1081}
1082
1083static inline void init_rq_hrtick(struct rq *rq)
1084{
1085}
1086
1087void hrtick_resched(void)
1088{
1089}
1090#endif
1091
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001092/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001093 * resched_task - mark a task 'to be rescheduled now'.
1094 *
1095 * On UP this means the setting of the need_resched flag, on SMP it
1096 * might also involve a cross-CPU call to trigger the scheduler on
1097 * the target CPU.
1098 */
1099#ifdef CONFIG_SMP
1100
1101#ifndef tsk_is_polling
1102#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1103#endif
1104
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001105static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001106{
1107 int cpu;
1108
1109 assert_spin_locked(&task_rq(p)->lock);
1110
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001111 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001112 return;
1113
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001114 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001115
1116 cpu = task_cpu(p);
1117 if (cpu == smp_processor_id())
1118 return;
1119
1120 /* NEED_RESCHED must be visible before we test polling */
1121 smp_mb();
1122 if (!tsk_is_polling(p))
1123 smp_send_reschedule(cpu);
1124}
1125
1126static void resched_cpu(int cpu)
1127{
1128 struct rq *rq = cpu_rq(cpu);
1129 unsigned long flags;
1130
1131 if (!spin_trylock_irqsave(&rq->lock, flags))
1132 return;
1133 resched_task(cpu_curr(cpu));
1134 spin_unlock_irqrestore(&rq->lock, flags);
1135}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001136
1137#ifdef CONFIG_NO_HZ
1138/*
1139 * When add_timer_on() enqueues a timer into the timer wheel of an
1140 * idle CPU then this timer might expire before the next timer event
1141 * which is scheduled to wake up that CPU. In case of a completely
1142 * idle system the next event might even be infinite time into the
1143 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1144 * leaves the inner idle loop so the newly added timer is taken into
1145 * account when the CPU goes back to idle and evaluates the timer
1146 * wheel for the next timer event.
1147 */
1148void wake_up_idle_cpu(int cpu)
1149{
1150 struct rq *rq = cpu_rq(cpu);
1151
1152 if (cpu == smp_processor_id())
1153 return;
1154
1155 /*
1156 * This is safe, as this function is called with the timer
1157 * wheel base lock of (cpu) held. When the CPU is on the way
1158 * to idle and has not yet set rq->curr to idle then it will
1159 * be serialized on the timer wheel base lock and take the new
1160 * timer into account automatically.
1161 */
1162 if (rq->curr != rq->idle)
1163 return;
1164
1165 /*
1166 * We can set TIF_RESCHED on the idle task of the other CPU
1167 * lockless. The worst case is that the other CPU runs the
1168 * idle task through an additional NOOP schedule()
1169 */
1170 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1171
1172 /* NEED_RESCHED must be visible before we test polling */
1173 smp_mb();
1174 if (!tsk_is_polling(rq->idle))
1175 smp_send_reschedule(cpu);
1176}
1177#endif
1178
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001179#else
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001180static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001181{
1182 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001183 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001184}
1185#endif
1186
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001187#if BITS_PER_LONG == 32
1188# define WMULT_CONST (~0UL)
1189#else
1190# define WMULT_CONST (1UL << 32)
1191#endif
1192
1193#define WMULT_SHIFT 32
1194
Ingo Molnar194081e2007-08-09 11:16:51 +02001195/*
1196 * Shift right and round:
1197 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001198#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001199
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001200static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001201calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1202 struct load_weight *lw)
1203{
1204 u64 tmp;
1205
1206 if (unlikely(!lw->inv_weight))
Ingo Molnar27d11722008-03-14 22:20:01 +01001207 lw->inv_weight = (WMULT_CONST-lw->weight/2) / (lw->weight+1);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001208
1209 tmp = (u64)delta_exec * weight;
1210 /*
1211 * Check whether we'd overflow the 64-bit multiplication:
1212 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001213 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001214 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001215 WMULT_SHIFT/2);
1216 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001217 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001218
Ingo Molnarecf691d2007-08-02 17:41:40 +02001219 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001220}
1221
1222static inline unsigned long
1223calc_delta_fair(unsigned long delta_exec, struct load_weight *lw)
1224{
1225 return calc_delta_mine(delta_exec, NICE_0_LOAD, lw);
1226}
1227
Ingo Molnar10919852007-10-15 17:00:04 +02001228static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001229{
1230 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001231 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001232}
1233
Ingo Molnar10919852007-10-15 17:00:04 +02001234static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001235{
1236 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001237 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001238}
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001241 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1242 * of tasks with abnormal "nice" values across CPUs the contribution that
1243 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001244 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001245 * scaled version of the new time slice allocation that they receive on time
1246 * slice expiry etc.
1247 */
1248
Ingo Molnardd41f592007-07-09 18:51:59 +02001249#define WEIGHT_IDLEPRIO 2
1250#define WMULT_IDLEPRIO (1 << 31)
1251
1252/*
1253 * Nice levels are multiplicative, with a gentle 10% change for every
1254 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1255 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1256 * that remained on nice 0.
1257 *
1258 * The "10% effect" is relative and cumulative: from _any_ nice level,
1259 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee62007-07-16 09:46:30 +02001260 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1261 * If a task goes up by ~10% and another task goes down by ~10% then
1262 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001263 */
1264static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001265 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1266 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1267 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1268 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1269 /* 0 */ 1024, 820, 655, 526, 423,
1270 /* 5 */ 335, 272, 215, 172, 137,
1271 /* 10 */ 110, 87, 70, 56, 45,
1272 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001273};
1274
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001275/*
1276 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1277 *
1278 * In cases where the weight does not change often, we can use the
1279 * precalculated inverse to speed up arithmetics by turning divisions
1280 * into multiplications:
1281 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001282static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001283 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1284 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1285 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1286 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1287 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1288 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1289 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1290 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001291};
Peter Williams2dd73a42006-06-27 02:54:34 -07001292
Ingo Molnardd41f592007-07-09 18:51:59 +02001293static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1294
1295/*
1296 * runqueue iterator, to support SMP load-balancing between different
1297 * scheduling classes, without having to expose their internal data
1298 * structures to the load-balancing proper:
1299 */
1300struct rq_iterator {
1301 void *arg;
1302 struct task_struct *(*start)(void *);
1303 struct task_struct *(*next)(void *);
1304};
1305
Peter Williamse1d14842007-10-24 18:23:51 +02001306#ifdef CONFIG_SMP
1307static unsigned long
1308balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1309 unsigned long max_load_move, struct sched_domain *sd,
1310 enum cpu_idle_type idle, int *all_pinned,
1311 int *this_best_prio, struct rq_iterator *iterator);
1312
1313static int
1314iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1315 struct sched_domain *sd, enum cpu_idle_type idle,
1316 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001317#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001318
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001319#ifdef CONFIG_CGROUP_CPUACCT
1320static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1321#else
1322static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1323#endif
1324
Gregory Haskinse7693a32008-01-25 21:08:09 +01001325#ifdef CONFIG_SMP
1326static unsigned long source_load(int cpu, int type);
1327static unsigned long target_load(int cpu, int type);
1328static unsigned long cpu_avg_load_per_task(int cpu);
1329static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
1330#endif /* CONFIG_SMP */
1331
Ingo Molnardd41f592007-07-09 18:51:59 +02001332#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001333#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001334#include "sched_fair.c"
1335#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001336#ifdef CONFIG_SCHED_DEBUG
1337# include "sched_debug.c"
1338#endif
1339
1340#define sched_class_highest (&rt_sched_class)
1341
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001342static inline void inc_load(struct rq *rq, const struct task_struct *p)
Ingo Molnar9c217242007-08-02 17:41:40 +02001343{
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001344 update_load_add(&rq->load, p->se.load.weight);
Ingo Molnar9c217242007-08-02 17:41:40 +02001345}
1346
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001347static inline void dec_load(struct rq *rq, const struct task_struct *p)
1348{
1349 update_load_sub(&rq->load, p->se.load.weight);
1350}
1351
1352static void inc_nr_running(struct task_struct *p, struct rq *rq)
1353{
1354 rq->nr_running++;
1355 inc_load(rq, p);
1356}
1357
1358static void dec_nr_running(struct task_struct *p, struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001359{
1360 rq->nr_running--;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001361 dec_load(rq, p);
Ingo Molnar9c217242007-08-02 17:41:40 +02001362}
1363
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001364static void set_load_weight(struct task_struct *p)
1365{
1366 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001367 p->se.load.weight = prio_to_weight[0] * 2;
1368 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1369 return;
1370 }
1371
1372 /*
1373 * SCHED_IDLE tasks get minimal weight:
1374 */
1375 if (p->policy == SCHED_IDLE) {
1376 p->se.load.weight = WEIGHT_IDLEPRIO;
1377 p->se.load.inv_weight = WMULT_IDLEPRIO;
1378 return;
1379 }
1380
1381 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1382 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001383}
1384
Ingo Molnar8159f872007-08-09 11:16:49 +02001385static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001386{
1387 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001388 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001389 p->se.on_rq = 1;
1390}
1391
Ingo Molnar69be72c2007-08-09 11:16:49 +02001392static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001393{
Ingo Molnarf02231e2007-08-09 11:16:48 +02001394 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001395 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001396}
1397
1398/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001399 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001400 */
Ingo Molnar14531182007-07-09 18:51:59 +02001401static inline int __normal_prio(struct task_struct *p)
1402{
Ingo Molnardd41f592007-07-09 18:51:59 +02001403 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001404}
1405
1406/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001407 * Calculate the expected normal priority: i.e. priority
1408 * without taking RT-inheritance into account. Might be
1409 * boosted by interactivity modifiers. Changes upon fork,
1410 * setprio syscalls, and whenever the interactivity
1411 * estimator recalculates.
1412 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001413static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001414{
1415 int prio;
1416
Ingo Molnare05606d2007-07-09 18:51:59 +02001417 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001418 prio = MAX_RT_PRIO-1 - p->rt_priority;
1419 else
1420 prio = __normal_prio(p);
1421 return prio;
1422}
1423
1424/*
1425 * Calculate the current priority, i.e. the priority
1426 * taken into account by the scheduler. This value might
1427 * be boosted by RT tasks, or might be boosted by
1428 * interactivity modifiers. Will be RT if the task got
1429 * RT-boosted. If not then it returns p->normal_prio.
1430 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001431static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001432{
1433 p->normal_prio = normal_prio(p);
1434 /*
1435 * If we are RT tasks or we were boosted to RT priority,
1436 * keep the priority unchanged. Otherwise, update priority
1437 * to the normal priority:
1438 */
1439 if (!rt_prio(p->prio))
1440 return p->normal_prio;
1441 return p->prio;
1442}
1443
1444/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001445 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001447static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001449 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001450 rq->nr_uninterruptible--;
1451
Ingo Molnar8159f872007-08-09 11:16:49 +02001452 enqueue_task(rq, p, wakeup);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001453 inc_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455
1456/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 * deactivate_task - remove a task from the runqueue.
1458 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02001459static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001461 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001462 rq->nr_uninterruptible++;
1463
Ingo Molnar69be72c2007-08-09 11:16:49 +02001464 dequeue_task(rq, p, sleep);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001465 dec_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468/**
1469 * task_curr - is this task currently executing on a CPU?
1470 * @p: the task in question.
1471 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001472inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
1474 return cpu_curr(task_cpu(p)) == p;
1475}
1476
Peter Williams2dd73a42006-06-27 02:54:34 -07001477/* Used instead of source_load when we know the type == 0 */
1478unsigned long weighted_cpuload(const int cpu)
1479{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001480 return cpu_rq(cpu)->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02001481}
1482
1483static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1484{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001485 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001486#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01001487 /*
1488 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1489 * successfuly executed on another CPU. We must ensure that updates of
1490 * per-task data have been completed by this moment.
1491 */
1492 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02001493 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02001494#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07001495}
1496
Steven Rostedtcb469842008-01-25 21:08:22 +01001497static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1498 const struct sched_class *prev_class,
1499 int oldprio, int running)
1500{
1501 if (prev_class != p->sched_class) {
1502 if (prev_class->switched_from)
1503 prev_class->switched_from(rq, p, running);
1504 p->sched_class->switched_to(rq, p, running);
1505 } else
1506 p->sched_class->prio_changed(rq, p, oldprio, running);
1507}
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02001510
Ingo Molnarcc367732007-10-15 17:00:18 +02001511/*
1512 * Is this task likely cache-hot:
1513 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001514static int
Ingo Molnarcc367732007-10-15 17:00:18 +02001515task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
1516{
1517 s64 delta;
1518
Ingo Molnarf540a602008-03-15 17:10:34 +01001519 /*
1520 * Buddy candidates are cache hot:
1521 */
1522 if (&p->se == cfs_rq_of(&p->se)->next)
1523 return 1;
1524
Ingo Molnarcc367732007-10-15 17:00:18 +02001525 if (p->sched_class != &fair_sched_class)
1526 return 0;
1527
Ingo Molnar6bc16652007-10-15 17:00:18 +02001528 if (sysctl_sched_migration_cost == -1)
1529 return 1;
1530 if (sysctl_sched_migration_cost == 0)
1531 return 0;
1532
Ingo Molnarcc367732007-10-15 17:00:18 +02001533 delta = now - p->se.exec_start;
1534
1535 return delta < (s64)sysctl_sched_migration_cost;
1536}
1537
1538
Ingo Molnardd41f592007-07-09 18:51:59 +02001539void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001540{
Ingo Molnardd41f592007-07-09 18:51:59 +02001541 int old_cpu = task_cpu(p);
1542 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001543 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
1544 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02001545 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001546
1547 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001548
1549#ifdef CONFIG_SCHEDSTATS
1550 if (p->se.wait_start)
1551 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001552 if (p->se.sleep_start)
1553 p->se.sleep_start -= clock_offset;
1554 if (p->se.block_start)
1555 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02001556 if (old_cpu != new_cpu) {
1557 schedstat_inc(p, se.nr_migrations);
1558 if (task_hot(p, old_rq->clock, NULL))
1559 schedstat_inc(p, se.nr_forced2_migrations);
1560 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001561#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001562 p->se.vruntime -= old_cfsrq->min_vruntime -
1563 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02001564
1565 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001566}
1567
Ingo Molnar70b97a72006-07-03 00:25:42 -07001568struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Ingo Molnar36c8b582006-07-03 00:25:41 -07001571 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 int dest_cpu;
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001575};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577/*
1578 * The task's runqueue lock must be held.
1579 * Returns true if you have to wait for migration thread.
1580 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001581static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07001582migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001584 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 /*
1587 * If the task is not on a runqueue (and not running), then
1588 * it is sufficient to simply update the task's cpu field.
1589 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001590 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 set_task_cpu(p, dest_cpu);
1592 return 0;
1593 }
1594
1595 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 req->task = p;
1597 req->dest_cpu = dest_cpu;
1598 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07001599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 return 1;
1601}
1602
1603/*
1604 * wait_task_inactive - wait for a thread to unschedule.
1605 *
1606 * The caller must ensure that the task *will* unschedule sometime soon,
1607 * else this function might spin for a *long* time. This function can't
1608 * be called with interrupts off, or it may introduce deadlock with
1609 * smp_call_function() if an IPI is sent by the same process we are
1610 * waiting to become inactive.
1611 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001612void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
1614 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001615 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001616 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Andi Kleen3a5c3592007-10-15 17:00:14 +02001618 for (;;) {
1619 /*
1620 * We do the initial early heuristics without holding
1621 * any task-queue locks at all. We'll only try to get
1622 * the runqueue lock when things look like they will
1623 * work out!
1624 */
1625 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001626
Andi Kleen3a5c3592007-10-15 17:00:14 +02001627 /*
1628 * If the task is actively running on another CPU
1629 * still, just relax and busy-wait without holding
1630 * any locks.
1631 *
1632 * NOTE! Since we don't hold any locks, it's not
1633 * even sure that "rq" stays as the right runqueue!
1634 * But we don't care, since "task_running()" will
1635 * return false if the runqueue has changed and p
1636 * is actually now running somewhere else!
1637 */
1638 while (task_running(rq, p))
1639 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001640
Andi Kleen3a5c3592007-10-15 17:00:14 +02001641 /*
1642 * Ok, time to look more closely! We need the rq
1643 * lock now, to be *sure*. If we're wrong, we'll
1644 * just go back and repeat.
1645 */
1646 rq = task_rq_lock(p, &flags);
1647 running = task_running(rq, p);
1648 on_rq = p->se.on_rq;
1649 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001650
Andi Kleen3a5c3592007-10-15 17:00:14 +02001651 /*
1652 * Was it really running after all now that we
1653 * checked with the proper locks actually held?
1654 *
1655 * Oops. Go back and try again..
1656 */
1657 if (unlikely(running)) {
1658 cpu_relax();
1659 continue;
1660 }
1661
1662 /*
1663 * It's not enough that it's not actively running,
1664 * it must be off the runqueue _entirely_, and not
1665 * preempted!
1666 *
1667 * So if it wa still runnable (but just not actively
1668 * running right now), it's preempted, and we should
1669 * yield - it could be a while.
1670 */
1671 if (unlikely(on_rq)) {
1672 schedule_timeout_uninterruptible(1);
1673 continue;
1674 }
1675
1676 /*
1677 * Ahh, all good. It wasn't running, and it wasn't
1678 * runnable, which means that it will never become
1679 * running in the future either. We're all done!
1680 */
1681 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683}
1684
1685/***
1686 * kick_process - kick a running thread to enter/exit the kernel
1687 * @p: the to-be-kicked thread
1688 *
1689 * Cause a process which is running on another CPU to enter
1690 * kernel-mode, without any delay. (to get signals handled.)
1691 *
1692 * NOTE: this function doesnt have to take the runqueue lock,
1693 * because all it wants to ensure is that the remote task enters
1694 * the kernel. If the IPI races and the task has been migrated
1695 * to another CPU then no harm is done and the purpose has been
1696 * achieved as well.
1697 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001698void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
1700 int cpu;
1701
1702 preempt_disable();
1703 cpu = task_cpu(p);
1704 if ((cpu != smp_processor_id()) && task_curr(p))
1705 smp_send_reschedule(cpu);
1706 preempt_enable();
1707}
1708
1709/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001710 * Return a low guess at the load of a migration-source cpu weighted
1711 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 *
1713 * We want to under-estimate the load of migration sources, to
1714 * balance conservatively.
1715 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001716static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08001717{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001718 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001719 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001720
Peter Williams2dd73a42006-06-27 02:54:34 -07001721 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001722 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001723
Ingo Molnardd41f592007-07-09 18:51:59 +02001724 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
1727/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001728 * Return a high guess at the load of a migration-target cpu weighted
1729 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001731static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08001732{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001733 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001734 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001735
Peter Williams2dd73a42006-06-27 02:54:34 -07001736 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001737 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001738
Ingo Molnardd41f592007-07-09 18:51:59 +02001739 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07001740}
1741
1742/*
1743 * Return the average load per task on the cpu's run queue
1744 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001745static unsigned long cpu_avg_load_per_task(int cpu)
Peter Williams2dd73a42006-06-27 02:54:34 -07001746{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001747 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001748 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07001749 unsigned long n = rq->nr_running;
1750
Ingo Molnardd41f592007-07-09 18:51:59 +02001751 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
1753
Nick Piggin147cbb42005-06-25 14:57:19 -07001754/*
1755 * find_idlest_group finds and returns the least busy CPU group within the
1756 * domain.
1757 */
1758static struct sched_group *
1759find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
1760{
1761 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
1762 unsigned long min_load = ULONG_MAX, this_load = 0;
1763 int load_idx = sd->forkexec_idx;
1764 int imbalance = 100 + (sd->imbalance_pct-100)/2;
1765
1766 do {
1767 unsigned long load, avg_load;
1768 int local_group;
1769 int i;
1770
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001771 /* Skip over this group if it has no CPUs allowed */
1772 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02001773 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001774
Nick Piggin147cbb42005-06-25 14:57:19 -07001775 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07001776
1777 /* Tally up the load of all CPUs in the group */
1778 avg_load = 0;
1779
1780 for_each_cpu_mask(i, group->cpumask) {
1781 /* Bias balancing toward cpus of our domain */
1782 if (local_group)
1783 load = source_load(i, load_idx);
1784 else
1785 load = target_load(i, load_idx);
1786
1787 avg_load += load;
1788 }
1789
1790 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07001791 avg_load = sg_div_cpu_power(group,
1792 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07001793
1794 if (local_group) {
1795 this_load = avg_load;
1796 this = group;
1797 } else if (avg_load < min_load) {
1798 min_load = avg_load;
1799 idlest = group;
1800 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02001801 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07001802
1803 if (!idlest || 100*this_load < imbalance*min_load)
1804 return NULL;
1805 return idlest;
1806}
1807
1808/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07001809 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07001810 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07001811static int
1812find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
Nick Piggin147cbb42005-06-25 14:57:19 -07001813{
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001814 cpumask_t tmp;
Nick Piggin147cbb42005-06-25 14:57:19 -07001815 unsigned long load, min_load = ULONG_MAX;
1816 int idlest = -1;
1817 int i;
1818
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001819 /* Traverse only the allowed CPUs */
1820 cpus_and(tmp, group->cpumask, p->cpus_allowed);
1821
1822 for_each_cpu_mask(i, tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07001823 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07001824
1825 if (load < min_load || (load == min_load && i == this_cpu)) {
1826 min_load = load;
1827 idlest = i;
1828 }
1829 }
1830
1831 return idlest;
1832}
1833
Nick Piggin476d1392005-06-25 14:57:29 -07001834/*
1835 * sched_balance_self: balance the current task (running on cpu) in domains
1836 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
1837 * SD_BALANCE_EXEC.
1838 *
1839 * Balance, ie. select the least loaded group.
1840 *
1841 * Returns the target CPU number, or the same CPU if no balancing is needed.
1842 *
1843 * preempt must be disabled.
1844 */
1845static int sched_balance_self(int cpu, int flag)
1846{
1847 struct task_struct *t = current;
1848 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07001849
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001850 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02001851 /*
1852 * If power savings logic is enabled for a domain, stop there.
1853 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07001854 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
1855 break;
Nick Piggin476d1392005-06-25 14:57:29 -07001856 if (tmp->flags & flag)
1857 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001858 }
Nick Piggin476d1392005-06-25 14:57:29 -07001859
1860 while (sd) {
1861 cpumask_t span;
1862 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001863 int new_cpu, weight;
1864
1865 if (!(sd->flags & flag)) {
1866 sd = sd->child;
1867 continue;
1868 }
Nick Piggin476d1392005-06-25 14:57:29 -07001869
1870 span = sd->span;
1871 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001872 if (!group) {
1873 sd = sd->child;
1874 continue;
1875 }
Nick Piggin476d1392005-06-25 14:57:29 -07001876
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001877 new_cpu = find_idlest_cpu(group, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001878 if (new_cpu == -1 || new_cpu == cpu) {
1879 /* Now try balancing at a lower domain level of cpu */
1880 sd = sd->child;
1881 continue;
1882 }
Nick Piggin476d1392005-06-25 14:57:29 -07001883
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001884 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07001885 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07001886 sd = NULL;
1887 weight = cpus_weight(span);
1888 for_each_domain(cpu, tmp) {
1889 if (weight <= cpus_weight(tmp->span))
1890 break;
1891 if (tmp->flags & flag)
1892 sd = tmp;
1893 }
1894 /* while loop will break here if sd == NULL */
1895 }
1896
1897 return cpu;
1898}
1899
1900#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902/***
1903 * try_to_wake_up - wake up a thread
1904 * @p: the to-be-woken-up thread
1905 * @state: the mask of task states that can be woken
1906 * @sync: do a synchronous wakeup?
1907 *
1908 * Put it on the run-queue if it's not already there. The "current"
1909 * thread is always on the run-queue (except when the actual
1910 * re-schedule is in progress), and as such you're allowed to do
1911 * the simpler "current->state = TASK_RUNNING" to mark yourself
1912 * runnable without the overhead of this.
1913 *
1914 * returns failure only if the task is already active.
1915 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001916static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
Ingo Molnarcc367732007-10-15 17:00:18 +02001918 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 unsigned long flags;
1920 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001921 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Ingo Molnarb85d0662008-03-16 20:03:22 +01001923 if (!sched_feat(SYNC_WAKEUPS))
1924 sync = 0;
1925
Linus Torvalds04e2f172008-02-23 18:05:03 -08001926 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 rq = task_rq_lock(p, &flags);
1928 old_state = p->state;
1929 if (!(old_state & state))
1930 goto out;
1931
Ingo Molnardd41f592007-07-09 18:51:59 +02001932 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 goto out_running;
1934
1935 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02001936 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 this_cpu = smp_processor_id();
1938
1939#ifdef CONFIG_SMP
1940 if (unlikely(task_running(rq, p)))
1941 goto out_activate;
1942
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01001943 cpu = p->sched_class->select_task_rq(p, sync);
1944 if (cpu != orig_cpu) {
1945 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 task_rq_unlock(rq, &flags);
1947 /* might preempt at this point */
1948 rq = task_rq_lock(p, &flags);
1949 old_state = p->state;
1950 if (!(old_state & state))
1951 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02001952 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 goto out_running;
1954
1955 this_cpu = smp_processor_id();
1956 cpu = task_cpu(p);
1957 }
1958
Gregory Haskinse7693a32008-01-25 21:08:09 +01001959#ifdef CONFIG_SCHEDSTATS
1960 schedstat_inc(rq, ttwu_count);
1961 if (cpu == this_cpu)
1962 schedstat_inc(rq, ttwu_local);
1963 else {
1964 struct sched_domain *sd;
1965 for_each_domain(this_cpu, sd) {
1966 if (cpu_isset(cpu, sd->span)) {
1967 schedstat_inc(sd, ttwu_wake_remote);
1968 break;
1969 }
1970 }
1971 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01001972#endif
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974out_activate:
1975#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02001976 schedstat_inc(p, se.nr_wakeups);
1977 if (sync)
1978 schedstat_inc(p, se.nr_wakeups_sync);
1979 if (orig_cpu != cpu)
1980 schedstat_inc(p, se.nr_wakeups_migrate);
1981 if (cpu == this_cpu)
1982 schedstat_inc(p, se.nr_wakeups_local);
1983 else
1984 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02001985 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02001986 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 success = 1;
1988
1989out_running:
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01001990 check_preempt_curr(rq, p);
1991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01001993#ifdef CONFIG_SMP
1994 if (p->sched_class->task_wake_up)
1995 p->sched_class->task_wake_up(rq, p);
1996#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997out:
1998 task_rq_unlock(rq, &flags);
1999
2000 return success;
2001}
2002
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002003int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002005 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007EXPORT_SYMBOL(wake_up_process);
2008
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002009int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
2011 return try_to_wake_up(p, state, 0);
2012}
2013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014/*
2015 * Perform scheduler related setup for a newly forked process p.
2016 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002017 *
2018 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002020static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
Ingo Molnardd41f592007-07-09 18:51:59 +02002022 p->se.exec_start = 0;
2023 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002024 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002025 p->se.last_wakeup = 0;
2026 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002027
2028#ifdef CONFIG_SCHEDSTATS
2029 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002030 p->se.sum_sleep_runtime = 0;
2031 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002032 p->se.block_start = 0;
2033 p->se.sleep_max = 0;
2034 p->se.block_max = 0;
2035 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002036 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002037 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002038#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002039
Peter Zijlstrafa717062008-01-25 21:08:27 +01002040 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002041 p->se.on_rq = 0;
Nick Piggin476d1392005-06-25 14:57:29 -07002042
Avi Kivitye107be32007-07-26 13:40:43 +02002043#ifdef CONFIG_PREEMPT_NOTIFIERS
2044 INIT_HLIST_HEAD(&p->preempt_notifiers);
2045#endif
2046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 /*
2048 * We mark the process as running here, but have not actually
2049 * inserted it onto the runqueue yet. This guarantees that
2050 * nobody will actually run it, and a signal or other external
2051 * event cannot wake it up and insert it on the runqueue either.
2052 */
2053 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002054}
2055
2056/*
2057 * fork()/clone()-time setup:
2058 */
2059void sched_fork(struct task_struct *p, int clone_flags)
2060{
2061 int cpu = get_cpu();
2062
2063 __sched_fork(p);
2064
2065#ifdef CONFIG_SMP
2066 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2067#endif
Ingo Molnar02e4bac22007-10-15 17:00:11 +02002068 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002069
2070 /*
2071 * Make sure we do not leak PI boosting priority to the child:
2072 */
2073 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002074 if (!rt_prio(p->prio))
2075 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002076
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002077#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002078 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002079 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002081#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002082 p->oncpu = 0;
2083#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002085 /* Want to start with kernel preemption disabled. */
Al Viroa1261f542005-11-13 16:06:55 -08002086 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002088 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089}
2090
2091/*
2092 * wake_up_new_task - wake up a newly created task for the first time.
2093 *
2094 * This function will do some initial scheduler statistics housekeeping
2095 * that must be done for every newly created context, then puts the task
2096 * on the runqueue and wakes it.
2097 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002098void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
2100 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002101 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002105 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107 p->prio = effective_prio(p);
2108
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002109 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002110 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002113 * Let the scheduling class do new task startup
2114 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002116 p->sched_class->task_new(rq, p);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002117 inc_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002119 check_preempt_curr(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002120#ifdef CONFIG_SMP
2121 if (p->sched_class->task_wake_up)
2122 p->sched_class->task_wake_up(rq, p);
2123#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002124 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125}
2126
Avi Kivitye107be32007-07-26 13:40:43 +02002127#ifdef CONFIG_PREEMPT_NOTIFIERS
2128
2129/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002130 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2131 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002132 */
2133void preempt_notifier_register(struct preempt_notifier *notifier)
2134{
2135 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2136}
2137EXPORT_SYMBOL_GPL(preempt_notifier_register);
2138
2139/**
2140 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002141 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002142 *
2143 * This is safe to call from within a preemption notifier.
2144 */
2145void preempt_notifier_unregister(struct preempt_notifier *notifier)
2146{
2147 hlist_del(&notifier->link);
2148}
2149EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2150
2151static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2152{
2153 struct preempt_notifier *notifier;
2154 struct hlist_node *node;
2155
2156 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2157 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2158}
2159
2160static void
2161fire_sched_out_preempt_notifiers(struct task_struct *curr,
2162 struct task_struct *next)
2163{
2164 struct preempt_notifier *notifier;
2165 struct hlist_node *node;
2166
2167 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2168 notifier->ops->sched_out(notifier, next);
2169}
2170
2171#else
2172
2173static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2174{
2175}
2176
2177static void
2178fire_sched_out_preempt_notifiers(struct task_struct *curr,
2179 struct task_struct *next)
2180{
2181}
2182
2183#endif
2184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002186 * prepare_task_switch - prepare to switch tasks
2187 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002188 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002189 * @next: the task we are going to switch to.
2190 *
2191 * This is called with the rq lock held and interrupts off. It must
2192 * be paired with a subsequent finish_task_switch after the context
2193 * switch.
2194 *
2195 * prepare_task_switch sets up locking and calls architecture specific
2196 * hooks.
2197 */
Avi Kivitye107be32007-07-26 13:40:43 +02002198static inline void
2199prepare_task_switch(struct rq *rq, struct task_struct *prev,
2200 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002201{
Avi Kivitye107be32007-07-26 13:40:43 +02002202 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002203 prepare_lock_switch(rq, next);
2204 prepare_arch_switch(next);
2205}
2206
2207/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002209 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 * @prev: the thread we just switched away from.
2211 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002212 * finish_task_switch must be called after the context switch, paired
2213 * with a prepare_task_switch call before the context switch.
2214 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2215 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 *
2217 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002218 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 * with the lock held can cause deadlocks; see schedule() for
2220 * details.)
2221 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002222static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 __releases(rq->lock)
2224{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002226 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
2228 rq->prev_mm = NULL;
2229
2230 /*
2231 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002232 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002233 * schedule one last time. The schedule call will never return, and
2234 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002235 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 * still held, otherwise prev could be scheduled on another cpu, die
2237 * there before we look at prev->state, and then the reference would
2238 * be dropped twice.
2239 * Manfred Spraul <manfred@colorfullife.com>
2240 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002241 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002242 finish_arch_switch(prev);
2243 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002244#ifdef CONFIG_SMP
2245 if (current->sched_class->post_schedule)
2246 current->sched_class->post_schedule(rq);
2247#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002248
Avi Kivitye107be32007-07-26 13:40:43 +02002249 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 if (mm)
2251 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002252 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002253 /*
2254 * Remove function-return probe instances associated with this
2255 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002256 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002257 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260}
2261
2262/**
2263 * schedule_tail - first thing a freshly forked thread must call.
2264 * @prev: the thread we just switched away from.
2265 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002266asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 __releases(rq->lock)
2268{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002269 struct rq *rq = this_rq();
2270
Nick Piggin4866cde2005-06-25 14:57:23 -07002271 finish_task_switch(rq, prev);
2272#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2273 /* In this case, finish_task_switch does not reenable preemption */
2274 preempt_enable();
2275#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002277 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
2279
2280/*
2281 * context_switch - switch to the new MM and the new
2282 * thread's register state.
2283 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002284static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002285context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002286 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
Ingo Molnardd41f592007-07-09 18:51:59 +02002288 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Avi Kivitye107be32007-07-26 13:40:43 +02002290 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002291 mm = next->mm;
2292 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002293 /*
2294 * For paravirt, this is coupled with an exit in switch_to to
2295 * combine the page table reload and the switch backend into
2296 * one hypercall.
2297 */
2298 arch_enter_lazy_cpu_mode();
2299
Ingo Molnardd41f592007-07-09 18:51:59 +02002300 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 next->active_mm = oldmm;
2302 atomic_inc(&oldmm->mm_count);
2303 enter_lazy_tlb(oldmm, next);
2304 } else
2305 switch_mm(oldmm, mm, next);
2306
Ingo Molnardd41f592007-07-09 18:51:59 +02002307 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 rq->prev_mm = oldmm;
2310 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002311 /*
2312 * Since the runqueue lock will be released by the next
2313 * task (which is an invalid locking op but in the case
2314 * of the scheduler it's an obvious special-case), so we
2315 * do an early lockdep release here:
2316 */
2317#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002318 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002319#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 /* Here we just switch the register state and the stack. */
2322 switch_to(prev, next, prev);
2323
Ingo Molnardd41f592007-07-09 18:51:59 +02002324 barrier();
2325 /*
2326 * this_rq must be evaluated again because prev may have moved
2327 * CPUs since it called schedule(), thus the 'rq' on its stack
2328 * frame will be invalid.
2329 */
2330 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331}
2332
2333/*
2334 * nr_running, nr_uninterruptible and nr_context_switches:
2335 *
2336 * externally visible scheduler statistics: current number of runnable
2337 * threads, current number of uninterruptible-sleeping threads, total
2338 * number of context switches performed since bootup.
2339 */
2340unsigned long nr_running(void)
2341{
2342 unsigned long i, sum = 0;
2343
2344 for_each_online_cpu(i)
2345 sum += cpu_rq(i)->nr_running;
2346
2347 return sum;
2348}
2349
2350unsigned long nr_uninterruptible(void)
2351{
2352 unsigned long i, sum = 0;
2353
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002354 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 sum += cpu_rq(i)->nr_uninterruptible;
2356
2357 /*
2358 * Since we read the counters lockless, it might be slightly
2359 * inaccurate. Do not allow it to go below zero though:
2360 */
2361 if (unlikely((long)sum < 0))
2362 sum = 0;
2363
2364 return sum;
2365}
2366
2367unsigned long long nr_context_switches(void)
2368{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002369 int i;
2370 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002372 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 sum += cpu_rq(i)->nr_switches;
2374
2375 return sum;
2376}
2377
2378unsigned long nr_iowait(void)
2379{
2380 unsigned long i, sum = 0;
2381
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002382 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2384
2385 return sum;
2386}
2387
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002388unsigned long nr_active(void)
2389{
2390 unsigned long i, running = 0, uninterruptible = 0;
2391
2392 for_each_online_cpu(i) {
2393 running += cpu_rq(i)->nr_running;
2394 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2395 }
2396
2397 if (unlikely((long)uninterruptible < 0))
2398 uninterruptible = 0;
2399
2400 return running + uninterruptible;
2401}
2402
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002404 * Update rq->cpu_load[] statistics. This function is usually called every
2405 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002406 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002407static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002408{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002409 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002410 int i, scale;
2411
2412 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002413
2414 /* Update our load: */
2415 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2416 unsigned long old_load, new_load;
2417
2418 /* scale is effectively 1 << i now, and >> i divides by scale */
2419
2420 old_load = this_rq->cpu_load[i];
2421 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002422 /*
2423 * Round up the averaging division if load is increasing. This
2424 * prevents us from getting stuck on 9 if the load is 10, for
2425 * example.
2426 */
2427 if (new_load > old_load)
2428 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002429 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2430 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002431}
2432
Ingo Molnardd41f592007-07-09 18:51:59 +02002433#ifdef CONFIG_SMP
2434
Ingo Molnar48f24c42006-07-03 00:25:40 -07002435/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 * double_rq_lock - safely lock two runqueues
2437 *
2438 * Note this does not disable interrupts like task_rq_lock,
2439 * you need to do so manually before calling.
2440 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002441static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 __acquires(rq1->lock)
2443 __acquires(rq2->lock)
2444{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002445 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 if (rq1 == rq2) {
2447 spin_lock(&rq1->lock);
2448 __acquire(rq2->lock); /* Fake it out ;) */
2449 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002450 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 spin_lock(&rq1->lock);
2452 spin_lock(&rq2->lock);
2453 } else {
2454 spin_lock(&rq2->lock);
2455 spin_lock(&rq1->lock);
2456 }
2457 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002458 update_rq_clock(rq1);
2459 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
2461
2462/*
2463 * double_rq_unlock - safely unlock two runqueues
2464 *
2465 * Note this does not restore interrupts like task_rq_unlock,
2466 * you need to do so manually after calling.
2467 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002468static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 __releases(rq1->lock)
2470 __releases(rq2->lock)
2471{
2472 spin_unlock(&rq1->lock);
2473 if (rq1 != rq2)
2474 spin_unlock(&rq2->lock);
2475 else
2476 __release(rq2->lock);
2477}
2478
2479/*
2480 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2481 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01002482static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 __releases(this_rq->lock)
2484 __acquires(busiest->lock)
2485 __acquires(this_rq->lock)
2486{
Steven Rostedte8fa1362008-01-25 21:08:05 +01002487 int ret = 0;
2488
Kirill Korotaev054b9102006-12-10 02:20:11 -08002489 if (unlikely(!irqs_disabled())) {
2490 /* printk() doesn't work good under rq->lock */
2491 spin_unlock(&this_rq->lock);
2492 BUG_ON(1);
2493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002495 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 spin_unlock(&this_rq->lock);
2497 spin_lock(&busiest->lock);
2498 spin_lock(&this_rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01002499 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 } else
2501 spin_lock(&busiest->lock);
2502 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01002503 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
2505
2506/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 * If dest_cpu is allowed for this process, migrate the task to it.
2508 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002509 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 * the cpu_allowed mask is restored.
2511 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002512static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002514 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002516 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518 rq = task_rq_lock(p, &flags);
2519 if (!cpu_isset(dest_cpu, p->cpus_allowed)
2520 || unlikely(cpu_is_offline(dest_cpu)))
2521 goto out;
2522
2523 /* force the process onto the specified CPU */
2524 if (migrate_task(p, dest_cpu, &req)) {
2525 /* Need to wait for migration thread (might exit: take ref). */
2526 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07002527
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 get_task_struct(mt);
2529 task_rq_unlock(rq, &flags);
2530 wake_up_process(mt);
2531 put_task_struct(mt);
2532 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07002533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 return;
2535 }
2536out:
2537 task_rq_unlock(rq, &flags);
2538}
2539
2540/*
Nick Piggin476d1392005-06-25 14:57:29 -07002541 * sched_exec - execve() is a valuable balancing opportunity, because at
2542 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 */
2544void sched_exec(void)
2545{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002547 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002549 if (new_cpu != this_cpu)
2550 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551}
2552
2553/*
2554 * pull_task - move a task from a remote runqueue to the local runqueue.
2555 * Both runqueues must be locked.
2556 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002557static void pull_task(struct rq *src_rq, struct task_struct *p,
2558 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02002560 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002562 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 /*
2564 * Note that idle threads have a prio of MAX_PRIO, for this test
2565 * to be always true for them.
2566 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002567 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568}
2569
2570/*
2571 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
2572 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002573static
Ingo Molnar70b97a72006-07-03 00:25:42 -07002574int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002575 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002576 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577{
2578 /*
2579 * We do not migrate tasks that are:
2580 * 1) running (obviously), or
2581 * 2) cannot be migrated to this CPU due to cpus_allowed, or
2582 * 3) are cache-hot on their current CPU.
2583 */
Ingo Molnarcc367732007-10-15 17:00:18 +02002584 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
2585 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002587 }
Nick Piggin81026792005-06-25 14:57:07 -07002588 *all_pinned = 0;
2589
Ingo Molnarcc367732007-10-15 17:00:18 +02002590 if (task_running(rq, p)) {
2591 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07002592 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Ingo Molnarda84d962007-10-15 17:00:18 +02002595 /*
2596 * Aggressive migration if:
2597 * 1) task is cache cold, or
2598 * 2) too many balance attempts have failed.
2599 */
2600
Ingo Molnar6bc16652007-10-15 17:00:18 +02002601 if (!task_hot(p, rq->clock, sd) ||
2602 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002603#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02002604 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002605 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02002606 schedstat_inc(p, se.nr_forced_migrations);
2607 }
Ingo Molnarda84d962007-10-15 17:00:18 +02002608#endif
2609 return 1;
2610 }
2611
Ingo Molnarcc367732007-10-15 17:00:18 +02002612 if (task_hot(p, rq->clock, sd)) {
2613 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02002614 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 return 1;
2617}
2618
Peter Williamse1d14842007-10-24 18:23:51 +02002619static unsigned long
2620balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
2621 unsigned long max_load_move, struct sched_domain *sd,
2622 enum cpu_idle_type idle, int *all_pinned,
2623 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02002624{
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002625 int loops = 0, pulled = 0, pinned = 0, skip_for_load;
Ingo Molnardd41f592007-07-09 18:51:59 +02002626 struct task_struct *p;
2627 long rem_load_move = max_load_move;
2628
Peter Williamse1d14842007-10-24 18:23:51 +02002629 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002630 goto out;
2631
2632 pinned = 1;
2633
2634 /*
2635 * Start the load-balancing iterator:
2636 */
2637 p = iterator->start(iterator->arg);
2638next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002639 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02002640 goto out;
2641 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002642 * To help distribute high priority tasks across CPUs we don't
Ingo Molnardd41f592007-07-09 18:51:59 +02002643 * skip a task if it will be the highest priority task (i.e. smallest
2644 * prio value) on its new queue regardless of its load weight
2645 */
2646 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
2647 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002648 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02002649 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002650 p = iterator->next(iterator->arg);
2651 goto next;
2652 }
2653
2654 pull_task(busiest, p, this_rq, this_cpu);
2655 pulled++;
2656 rem_load_move -= p->se.load.weight;
2657
2658 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002659 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002660 */
Peter Williamse1d14842007-10-24 18:23:51 +02002661 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002662 if (p->prio < *this_best_prio)
2663 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02002664 p = iterator->next(iterator->arg);
2665 goto next;
2666 }
2667out:
2668 /*
Peter Williamse1d14842007-10-24 18:23:51 +02002669 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02002670 * so we can safely collect pull_task() stats here rather than
2671 * inside pull_task().
2672 */
2673 schedstat_add(sd, lb_gained[idle], pulled);
2674
2675 if (all_pinned)
2676 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02002677
2678 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02002679}
Ingo Molnar48f24c42006-07-03 00:25:40 -07002680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681/*
Peter Williams43010652007-08-09 11:16:46 +02002682 * move_tasks tries to move up to max_load_move weighted load from busiest to
2683 * this_rq, as part of a balancing operation within domain "sd".
2684 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 *
2686 * Called with both runqueues locked.
2687 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002688static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02002689 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002690 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07002691 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002693 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02002694 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002695 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Ingo Molnardd41f592007-07-09 18:51:59 +02002697 do {
Peter Williams43010652007-08-09 11:16:46 +02002698 total_load_moved +=
2699 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02002700 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002701 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02002702 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02002703 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Peter Williams43010652007-08-09 11:16:46 +02002705 return total_load_moved > 0;
2706}
2707
Peter Williamse1d14842007-10-24 18:23:51 +02002708static int
2709iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2710 struct sched_domain *sd, enum cpu_idle_type idle,
2711 struct rq_iterator *iterator)
2712{
2713 struct task_struct *p = iterator->start(iterator->arg);
2714 int pinned = 0;
2715
2716 while (p) {
2717 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
2718 pull_task(busiest, p, this_rq, this_cpu);
2719 /*
2720 * Right now, this is only the second place pull_task()
2721 * is called, so we can safely collect pull_task()
2722 * stats here rather than inside pull_task().
2723 */
2724 schedstat_inc(sd, lb_gained[idle]);
2725
2726 return 1;
2727 }
2728 p = iterator->next(iterator->arg);
2729 }
2730
2731 return 0;
2732}
2733
Peter Williams43010652007-08-09 11:16:46 +02002734/*
2735 * move_one_task tries to move exactly one task from busiest to this_rq, as
2736 * part of active balancing operations within "domain".
2737 * Returns 1 if successful and 0 otherwise.
2738 *
2739 * Called with both runqueues locked.
2740 */
2741static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2742 struct sched_domain *sd, enum cpu_idle_type idle)
2743{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002744 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02002745
2746 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02002747 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02002748 return 1;
2749
2750 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751}
2752
2753/*
2754 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07002755 * domain. It calculates and returns the amount of weighted load which
2756 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 */
2758static struct sched_group *
2759find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02002760 unsigned long *imbalance, enum cpu_idle_type idle,
2761 int *sd_idle, cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762{
2763 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
2764 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002765 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07002766 unsigned long busiest_load_per_task, busiest_nr_running;
2767 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02002768 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002769#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2770 int power_savings_balance = 1;
2771 unsigned long leader_nr_running = 0, min_load_per_task = 0;
2772 unsigned long min_nr_running = ULONG_MAX;
2773 struct sched_group *group_min = NULL, *group_leader = NULL;
2774#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
2776 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002777 busiest_load_per_task = busiest_nr_running = 0;
2778 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002779 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002780 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002781 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002782 load_idx = sd->newidle_idx;
2783 else
2784 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
2786 do {
Ken Chen908a7c12007-10-17 16:55:11 +02002787 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 int local_group;
2789 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02002790 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002791 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002792 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
2794 local_group = cpu_isset(this_cpu, group->cpumask);
2795
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002796 if (local_group)
2797 balance_cpu = first_cpu(group->cpumask);
2798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07002800 sum_weighted_load = sum_nr_running = avg_load = 0;
Ken Chen908a7c12007-10-17 16:55:11 +02002801 max_cpu_load = 0;
2802 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
2804 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002805 struct rq *rq;
2806
2807 if (!cpu_isset(i, *cpus))
2808 continue;
2809
2810 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07002811
Suresh Siddha9439aab2007-07-19 21:28:35 +02002812 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07002813 *sd_idle = 0;
2814
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002816 if (local_group) {
2817 if (idle_cpu(i) && !first_idle_cpu) {
2818 first_idle_cpu = 1;
2819 balance_cpu = i;
2820 }
2821
Nick Piggina2000572006-02-10 01:51:02 -08002822 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02002823 } else {
Nick Piggina2000572006-02-10 01:51:02 -08002824 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02002825 if (load > max_cpu_load)
2826 max_cpu_load = load;
2827 if (min_cpu_load > load)
2828 min_cpu_load = load;
2829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
2831 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07002832 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002833 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 }
2835
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002836 /*
2837 * First idle cpu or the first cpu(busiest) in this sched group
2838 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02002839 * domains. In the newly idle case, we will allow all the cpu's
2840 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002841 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02002842 if (idle != CPU_NEWLY_IDLE && local_group &&
2843 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002844 *balance = 0;
2845 goto ret;
2846 }
2847
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07002849 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
2851 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002852 avg_load = sg_div_cpu_power(group,
2853 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
Ken Chen908a7c12007-10-17 16:55:11 +02002855 if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
2856 __group_imb = 1;
2857
Eric Dumazet5517d862007-05-08 00:32:57 -07002858 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002859
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 if (local_group) {
2861 this_load = avg_load;
2862 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002863 this_nr_running = sum_nr_running;
2864 this_load_per_task = sum_weighted_load;
2865 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02002866 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 max_load = avg_load;
2868 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002869 busiest_nr_running = sum_nr_running;
2870 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02002871 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002873
2874#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2875 /*
2876 * Busy processors will not participate in power savings
2877 * balance.
2878 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002879 if (idle == CPU_NOT_IDLE ||
2880 !(sd->flags & SD_POWERSAVINGS_BALANCE))
2881 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002882
2883 /*
2884 * If the local group is idle or completely loaded
2885 * no need to do power savings balance at this domain
2886 */
2887 if (local_group && (this_nr_running >= group_capacity ||
2888 !this_nr_running))
2889 power_savings_balance = 0;
2890
Ingo Molnardd41f592007-07-09 18:51:59 +02002891 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002892 * If a group is already running at full capacity or idle,
2893 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02002894 */
2895 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002896 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02002897 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002898
Ingo Molnardd41f592007-07-09 18:51:59 +02002899 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002900 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002901 * This is the group from where we need to pick up the load
2902 * for saving power
2903 */
2904 if ((sum_nr_running < min_nr_running) ||
2905 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002906 first_cpu(group->cpumask) <
2907 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002908 group_min = group;
2909 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002910 min_load_per_task = sum_weighted_load /
2911 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002912 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002913
Ingo Molnardd41f592007-07-09 18:51:59 +02002914 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002915 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02002916 * capacity but still has some space to pick up some load
2917 * from other group and save more power
2918 */
2919 if (sum_nr_running <= group_capacity - 1) {
2920 if (sum_nr_running > leader_nr_running ||
2921 (sum_nr_running == leader_nr_running &&
2922 first_cpu(group->cpumask) >
2923 first_cpu(group_leader->cpumask))) {
2924 group_leader = group;
2925 leader_nr_running = sum_nr_running;
2926 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002927 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002928group_next:
2929#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 group = group->next;
2931 } while (group != sd->groups);
2932
Peter Williams2dd73a42006-06-27 02:54:34 -07002933 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 goto out_balanced;
2935
2936 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
2937
2938 if (this_load >= avg_load ||
2939 100*max_load <= sd->imbalance_pct*this_load)
2940 goto out_balanced;
2941
Peter Williams2dd73a42006-06-27 02:54:34 -07002942 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02002943 if (group_imb)
2944 busiest_load_per_task = min(busiest_load_per_task, avg_load);
2945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 /*
2947 * We're trying to get all the cpus to the average_load, so we don't
2948 * want to push ourselves above the average load, nor do we wish to
2949 * reduce the max loaded cpu below the average load, as either of these
2950 * actions would just result in more rebalancing later, and ping-pong
2951 * tasks around. Thus we look for the minimum possible imbalance.
2952 * Negative imbalances (*we* are more loaded than anyone else) will
2953 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002954 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 * appear as very large values with unsigned longs.
2956 */
Peter Williams2dd73a42006-06-27 02:54:34 -07002957 if (max_load <= busiest_load_per_task)
2958 goto out_balanced;
2959
2960 /*
2961 * In the presence of smp nice balancing, certain scenarios can have
2962 * max load less than avg load(as we skip the groups at or below
2963 * its cpu_power, while calculating max_load..)
2964 */
2965 if (max_load < avg_load) {
2966 *imbalance = 0;
2967 goto small_imbalance;
2968 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002969
2970 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07002971 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07002974 *imbalance = min(max_pull * busiest->__cpu_power,
2975 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 / SCHED_LOAD_SCALE;
2977
Peter Williams2dd73a42006-06-27 02:54:34 -07002978 /*
2979 * if *imbalance is less than the average load per runnable task
2980 * there is no gaurantee that any tasks will be moved so we'll have
2981 * a think about bumping its value to force at least one task to be
2982 * moved
2983 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02002984 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07002985 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07002986 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
Peter Williams2dd73a42006-06-27 02:54:34 -07002988small_imbalance:
2989 pwr_move = pwr_now = 0;
2990 imbn = 2;
2991 if (this_nr_running) {
2992 this_load_per_task /= this_nr_running;
2993 if (busiest_load_per_task > this_load_per_task)
2994 imbn = 1;
2995 } else
2996 this_load_per_task = SCHED_LOAD_SCALE;
2997
Ingo Molnardd41f592007-07-09 18:51:59 +02002998 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
2999 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07003000 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 return busiest;
3002 }
3003
3004 /*
3005 * OK, we don't have enough imbalance to justify moving tasks,
3006 * however we may be able to increase total CPU power used by
3007 * moving them.
3008 */
3009
Eric Dumazet5517d862007-05-08 00:32:57 -07003010 pwr_now += busiest->__cpu_power *
3011 min(busiest_load_per_task, max_load);
3012 pwr_now += this->__cpu_power *
3013 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 pwr_now /= SCHED_LOAD_SCALE;
3015
3016 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07003017 tmp = sg_div_cpu_power(busiest,
3018 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07003020 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07003021 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
3023 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07003024 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003025 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07003026 tmp = sg_div_cpu_power(this,
3027 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 else
Eric Dumazet5517d862007-05-08 00:32:57 -07003029 tmp = sg_div_cpu_power(this,
3030 busiest_load_per_task * SCHED_LOAD_SCALE);
3031 pwr_move += this->__cpu_power *
3032 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 pwr_move /= SCHED_LOAD_SCALE;
3034
3035 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003036 if (pwr_move > pwr_now)
3037 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 }
3039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 return busiest;
3041
3042out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003043#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003044 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003045 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003047 if (this == group_leader && group_leader != group_min) {
3048 *imbalance = min_load_per_task;
3049 return group_min;
3050 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003051#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003052ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 *imbalance = 0;
3054 return NULL;
3055}
3056
3057/*
3058 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3059 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003060static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003061find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003062 unsigned long imbalance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003064 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003065 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 int i;
3067
3068 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003069 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003070
3071 if (!cpu_isset(i, *cpus))
3072 continue;
3073
Ingo Molnar48f24c42006-07-03 00:25:40 -07003074 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003075 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Ingo Molnardd41f592007-07-09 18:51:59 +02003077 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003078 continue;
3079
Ingo Molnardd41f592007-07-09 18:51:59 +02003080 if (wl > max_load) {
3081 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003082 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 }
3084 }
3085
3086 return busiest;
3087}
3088
3089/*
Nick Piggin77391d72005-06-25 14:57:30 -07003090 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3091 * so long as it is large enough.
3092 */
3093#define MAX_PINNED_INTERVAL 512
3094
3095/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3097 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003099static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003100 struct sched_domain *sd, enum cpu_idle_type idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003101 int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102{
Peter Williams43010652007-08-09 11:16:46 +02003103 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003106 struct rq *busiest;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003107 cpumask_t cpus = CPU_MASK_ALL;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003108 unsigned long flags;
Nick Piggin5969fe02005-09-10 00:26:19 -07003109
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003110 /*
3111 * When power savings policy is enabled for the parent domain, idle
3112 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003113 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003114 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003115 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003116 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003117 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003118 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
Ingo Molnar2d723762007-10-15 17:00:12 +02003120 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003122redo:
3123 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003124 &cpus, balance);
3125
Chen, Kenneth W06066712006-12-10 02:20:35 -08003126 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003127 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003128
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 if (!group) {
3130 schedstat_inc(sd, lb_nobusyg[idle]);
3131 goto out_balanced;
3132 }
3133
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003134 busiest = find_busiest_queue(group, idle, imbalance, &cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 if (!busiest) {
3136 schedstat_inc(sd, lb_nobusyq[idle]);
3137 goto out_balanced;
3138 }
3139
Nick Piggindb935db2005-06-25 14:57:11 -07003140 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
3142 schedstat_add(sd, lb_imbalance[idle], imbalance);
3143
Peter Williams43010652007-08-09 11:16:46 +02003144 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 if (busiest->nr_running > 1) {
3146 /*
3147 * Attempt to move tasks. If find_busiest_group has found
3148 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003149 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 * correctly treated as an imbalance.
3151 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003152 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003153 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003154 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003155 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003156 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003157 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003158
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003159 /*
3160 * some other cpu did the load balance for us.
3161 */
Peter Williams43010652007-08-09 11:16:46 +02003162 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003163 resched_cpu(this_cpu);
3164
Nick Piggin81026792005-06-25 14:57:07 -07003165 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003166 if (unlikely(all_pinned)) {
3167 cpu_clear(cpu_of(busiest), cpus);
3168 if (!cpus_empty(cpus))
3169 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003170 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 }
Nick Piggin81026792005-06-25 14:57:07 -07003173
Peter Williams43010652007-08-09 11:16:46 +02003174 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 schedstat_inc(sd, lb_failed[idle]);
3176 sd->nr_balance_failed++;
3177
3178 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003180 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003181
3182 /* don't kick the migration_thread, if the curr
3183 * task on busiest cpu can't be moved to this_cpu
3184 */
3185 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003186 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003187 all_pinned = 1;
3188 goto out_one_pinned;
3189 }
3190
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 if (!busiest->active_balance) {
3192 busiest->active_balance = 1;
3193 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003194 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003196 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003197 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 wake_up_process(busiest->migration_thread);
3199
3200 /*
3201 * We've kicked active balancing, reset the failure
3202 * counter.
3203 */
Nick Piggin39507452005-06-25 14:57:09 -07003204 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 }
Nick Piggin81026792005-06-25 14:57:07 -07003206 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 sd->nr_balance_failed = 0;
3208
Nick Piggin81026792005-06-25 14:57:07 -07003209 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 /* We were unbalanced, so reset the balancing interval */
3211 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003212 } else {
3213 /*
3214 * If we've begun active balancing, start to back off. This
3215 * case may not be covered by the all_pinned logic if there
3216 * is only 1 task on the busy runqueue (because we don't call
3217 * move_tasks).
3218 */
3219 if (sd->balance_interval < sd->max_interval)
3220 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 }
3222
Peter Williams43010652007-08-09 11:16:46 +02003223 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003224 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003225 return -1;
Peter Williams43010652007-08-09 11:16:46 +02003226 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
3228out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 schedstat_inc(sd, lb_balanced[idle]);
3230
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003231 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003232
3233out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003235 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3236 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 sd->balance_interval *= 2;
3238
Ingo Molnar48f24c42006-07-03 00:25:40 -07003239 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003240 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003241 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 return 0;
3243}
3244
3245/*
3246 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3247 * tasks if there is an imbalance.
3248 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003249 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 * this_rq is locked.
3251 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003252static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07003253load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254{
3255 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003256 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003258 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003259 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003260 int all_pinned = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003261 cpumask_t cpus = CPU_MASK_ALL;
Nick Piggin5969fe02005-09-10 00:26:19 -07003262
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003263 /*
3264 * When power savings policy is enabled for the parent domain, idle
3265 * sibling can pick up load irrespective of busy siblings. In this case,
3266 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003267 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003268 */
3269 if (sd->flags & SD_SHARE_CPUPOWER &&
3270 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003271 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
Ingo Molnar2d723762007-10-15 17:00:12 +02003273 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003274redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003275 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003276 &sd_idle, &cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003278 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003279 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 }
3281
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003282 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003283 &cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003284 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003285 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003286 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 }
3288
Nick Piggindb935db2005-06-25 14:57:11 -07003289 BUG_ON(busiest == this_rq);
3290
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003291 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003292
Peter Williams43010652007-08-09 11:16:46 +02003293 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003294 if (busiest->nr_running > 1) {
3295 /* Attempt to move tasks */
3296 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003297 /* this_rq->clock is already updated */
3298 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003299 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003300 imbalance, sd, CPU_NEWLY_IDLE,
3301 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003302 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003303
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003304 if (unlikely(all_pinned)) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003305 cpu_clear(cpu_of(busiest), cpus);
3306 if (!cpus_empty(cpus))
3307 goto redo;
3308 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003309 }
3310
Peter Williams43010652007-08-09 11:16:46 +02003311 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003312 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003313 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3314 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003315 return -1;
3316 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003317 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
Peter Williams43010652007-08-09 11:16:46 +02003319 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003320
3321out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003322 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003323 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003324 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003325 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003326 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003327
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329}
3330
3331/*
3332 * idle_balance is called by schedule() if this_cpu is about to become
3333 * idle. Attempts to pull tasks from other CPUs.
3334 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003335static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336{
3337 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003338 int pulled_task = -1;
3339 unsigned long next_balance = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340
3341 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003342 unsigned long interval;
3343
3344 if (!(sd->flags & SD_LOAD_BALANCE))
3345 continue;
3346
3347 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003348 /* If we've pulled tasks over stop searching: */
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003349 pulled_task = load_balance_newidle(this_cpu,
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003350 this_rq, sd);
3351
3352 interval = msecs_to_jiffies(sd->balance_interval);
3353 if (time_after(next_balance, sd->last_balance + interval))
3354 next_balance = sd->last_balance + interval;
3355 if (pulled_task)
3356 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003358 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003359 /*
3360 * We are going idle. next_balance may be set based on
3361 * a busy processor. So reset next_balance.
3362 */
3363 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365}
3366
3367/*
3368 * active_load_balance is run by migration threads. It pushes running tasks
3369 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3370 * running on each physical CPU where possible, and avoids physical /
3371 * logical imbalances.
3372 *
3373 * Called with busiest_rq locked.
3374 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003375static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376{
Nick Piggin39507452005-06-25 14:57:09 -07003377 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003378 struct sched_domain *sd;
3379 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003380
Ingo Molnar48f24c42006-07-03 00:25:40 -07003381 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003382 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003383 return;
3384
3385 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386
3387 /*
Nick Piggin39507452005-06-25 14:57:09 -07003388 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003389 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003390 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 */
Nick Piggin39507452005-06-25 14:57:09 -07003392 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Nick Piggin39507452005-06-25 14:57:09 -07003394 /* move a task from busiest_rq to target_rq */
3395 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003396 update_rq_clock(busiest_rq);
3397 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398
Nick Piggin39507452005-06-25 14:57:09 -07003399 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003400 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07003401 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003402 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07003403 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405
Ingo Molnar48f24c42006-07-03 00:25:40 -07003406 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003407 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
Peter Williams43010652007-08-09 11:16:46 +02003409 if (move_one_task(target_rq, target_cpu, busiest_rq,
3410 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07003411 schedstat_inc(sd, alb_pushed);
3412 else
3413 schedstat_inc(sd, alb_failed);
3414 }
Nick Piggin39507452005-06-25 14:57:09 -07003415 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416}
3417
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003418#ifdef CONFIG_NO_HZ
3419static struct {
3420 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003421 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003422} nohz ____cacheline_aligned = {
3423 .load_balancer = ATOMIC_INIT(-1),
3424 .cpu_mask = CPU_MASK_NONE,
3425};
3426
Christoph Lameter7835b982006-12-10 02:20:22 -08003427/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003428 * This routine will try to nominate the ilb (idle load balancing)
3429 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3430 * load balancing on behalf of all those cpus. If all the cpus in the system
3431 * go into this tickless mode, then there will be no ilb owner (as there is
3432 * no need for one) and all the cpus will sleep till the next wakeup event
3433 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08003434 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003435 * For the ilb owner, tick is not stopped. And this tick will be used
3436 * for idle load balancing. ilb owner will still be part of
3437 * nohz.cpu_mask..
3438 *
3439 * While stopping the tick, this cpu will become the ilb owner if there
3440 * is no other owner. And will be the owner till that cpu becomes busy
3441 * or if all cpus in the system stop their ticks at which point
3442 * there is no need for ilb owner.
3443 *
3444 * When the ilb owner becomes busy, it nominates another owner, during the
3445 * next busy scheduler_tick()
3446 */
3447int select_nohz_load_balancer(int stop_tick)
3448{
3449 int cpu = smp_processor_id();
3450
3451 if (stop_tick) {
3452 cpu_set(cpu, nohz.cpu_mask);
3453 cpu_rq(cpu)->in_nohz_recently = 1;
3454
3455 /*
3456 * If we are going offline and still the leader, give up!
3457 */
3458 if (cpu_is_offline(cpu) &&
3459 atomic_read(&nohz.load_balancer) == cpu) {
3460 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3461 BUG();
3462 return 0;
3463 }
3464
3465 /* time for ilb owner also to sleep */
3466 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3467 if (atomic_read(&nohz.load_balancer) == cpu)
3468 atomic_set(&nohz.load_balancer, -1);
3469 return 0;
3470 }
3471
3472 if (atomic_read(&nohz.load_balancer) == -1) {
3473 /* make me the ilb owner */
3474 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
3475 return 1;
3476 } else if (atomic_read(&nohz.load_balancer) == cpu)
3477 return 1;
3478 } else {
3479 if (!cpu_isset(cpu, nohz.cpu_mask))
3480 return 0;
3481
3482 cpu_clear(cpu, nohz.cpu_mask);
3483
3484 if (atomic_read(&nohz.load_balancer) == cpu)
3485 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3486 BUG();
3487 }
3488 return 0;
3489}
3490#endif
3491
3492static DEFINE_SPINLOCK(balancing);
3493
3494/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003495 * It checks each scheduling domain to see if it is due to be balanced,
3496 * and initiates a balancing operation if so.
3497 *
3498 * Balancing parameters are set up in arch_init_sched_domains.
3499 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003500static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08003501{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003502 int balance = 1;
3503 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08003504 unsigned long interval;
3505 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003506 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08003507 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003508 int update_next_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003510 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 if (!(sd->flags & SD_LOAD_BALANCE))
3512 continue;
3513
3514 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003515 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 interval *= sd->busy_factor;
3517
3518 /* scale ms to jiffies */
3519 interval = msecs_to_jiffies(interval);
3520 if (unlikely(!interval))
3521 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003522 if (interval > HZ*NR_CPUS/10)
3523 interval = HZ*NR_CPUS/10;
3524
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
Christoph Lameter08c183f2006-12-10 02:20:29 -08003526 if (sd->flags & SD_SERIALIZE) {
3527 if (!spin_trylock(&balancing))
3528 goto out;
3529 }
3530
Christoph Lameterc9819f42006-12-10 02:20:25 -08003531 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003532 if (load_balance(cpu, rq, sd, idle, &balance)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003533 /*
3534 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07003535 * longer idle, or one of our SMT siblings is
3536 * not idle.
3537 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003538 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003540 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08003542 if (sd->flags & SD_SERIALIZE)
3543 spin_unlock(&balancing);
3544out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02003545 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08003546 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003547 update_next_balance = 1;
3548 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003549
3550 /*
3551 * Stop the load balance at this level. There is another
3552 * CPU in our sched group which is doing load balancing more
3553 * actively.
3554 */
3555 if (!balance)
3556 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02003558
3559 /*
3560 * next_balance will be updated only when there is a need.
3561 * When the cpu is attached to null domain for ex, it will not be
3562 * updated.
3563 */
3564 if (likely(update_next_balance))
3565 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003566}
3567
3568/*
3569 * run_rebalance_domains is triggered when needed from the scheduler tick.
3570 * In CONFIG_NO_HZ case, the idle load balance owner will do the
3571 * rebalancing for all the cpus for whom scheduler ticks are stopped.
3572 */
3573static void run_rebalance_domains(struct softirq_action *h)
3574{
Ingo Molnardd41f592007-07-09 18:51:59 +02003575 int this_cpu = smp_processor_id();
3576 struct rq *this_rq = cpu_rq(this_cpu);
3577 enum cpu_idle_type idle = this_rq->idle_at_tick ?
3578 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003579
Ingo Molnardd41f592007-07-09 18:51:59 +02003580 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003581
3582#ifdef CONFIG_NO_HZ
3583 /*
3584 * If this cpu is the owner for idle load balancing, then do the
3585 * balancing on behalf of the other idle cpus whose ticks are
3586 * stopped.
3587 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003588 if (this_rq->idle_at_tick &&
3589 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003590 cpumask_t cpus = nohz.cpu_mask;
3591 struct rq *rq;
3592 int balance_cpu;
3593
Ingo Molnardd41f592007-07-09 18:51:59 +02003594 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003595 for_each_cpu_mask(balance_cpu, cpus) {
3596 /*
3597 * If this cpu gets work to do, stop the load balancing
3598 * work being done for other cpus. Next load
3599 * balancing owner will pick it up.
3600 */
3601 if (need_resched())
3602 break;
3603
Oleg Nesterovde0cf892007-08-12 18:08:19 +02003604 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003605
3606 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003607 if (time_after(this_rq->next_balance, rq->next_balance))
3608 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003609 }
3610 }
3611#endif
3612}
3613
3614/*
3615 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
3616 *
3617 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
3618 * idle load balancing owner or decide to stop the periodic load balancing,
3619 * if the whole system is idle.
3620 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003621static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003622{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003623#ifdef CONFIG_NO_HZ
3624 /*
3625 * If we were in the nohz mode recently and busy at the current
3626 * scheduler tick, then check if we need to nominate new idle
3627 * load balancer.
3628 */
3629 if (rq->in_nohz_recently && !rq->idle_at_tick) {
3630 rq->in_nohz_recently = 0;
3631
3632 if (atomic_read(&nohz.load_balancer) == cpu) {
3633 cpu_clear(cpu, nohz.cpu_mask);
3634 atomic_set(&nohz.load_balancer, -1);
3635 }
3636
3637 if (atomic_read(&nohz.load_balancer) == -1) {
3638 /*
3639 * simple selection for now: Nominate the
3640 * first cpu in the nohz list to be the next
3641 * ilb owner.
3642 *
3643 * TBD: Traverse the sched domains and nominate
3644 * the nearest cpu in the nohz.cpu_mask.
3645 */
3646 int ilb = first_cpu(nohz.cpu_mask);
3647
3648 if (ilb != NR_CPUS)
3649 resched_cpu(ilb);
3650 }
3651 }
3652
3653 /*
3654 * If this cpu is idle and doing idle load balancing for all the
3655 * cpus with ticks stopped, is it time for that to stop?
3656 */
3657 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
3658 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3659 resched_cpu(cpu);
3660 return;
3661 }
3662
3663 /*
3664 * If this cpu is idle and the idle load balancing is done by
3665 * someone else, then no need raise the SCHED_SOFTIRQ
3666 */
3667 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
3668 cpu_isset(cpu, nohz.cpu_mask))
3669 return;
3670#endif
3671 if (time_after_eq(jiffies, rq->next_balance))
3672 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673}
Ingo Molnardd41f592007-07-09 18:51:59 +02003674
3675#else /* CONFIG_SMP */
3676
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677/*
3678 * on UP we do not need to balance between CPUs:
3679 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003680static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681{
3682}
Ingo Molnardd41f592007-07-09 18:51:59 +02003683
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684#endif
3685
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686DEFINE_PER_CPU(struct kernel_stat, kstat);
3687
3688EXPORT_PER_CPU_SYMBOL(kstat);
3689
3690/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02003691 * Return p->sum_exec_runtime plus any more ns on the sched_clock
3692 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02003694unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003697 u64 ns, delta_exec;
3698 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003699
Ingo Molnar41b86e92007-07-09 18:51:58 +02003700 rq = task_rq_lock(p, &flags);
3701 ns = p->se.sum_exec_runtime;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01003702 if (task_current(rq, p)) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02003703 update_rq_clock(rq);
3704 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003705 if ((s64)delta_exec > 0)
3706 ns += delta_exec;
3707 }
3708 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003709
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 return ns;
3711}
3712
3713/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 * Account user cpu time to a process.
3715 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 * @cputime: the cpu time spent in user space since the last update
3717 */
3718void account_user_time(struct task_struct *p, cputime_t cputime)
3719{
3720 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3721 cputime64_t tmp;
3722
3723 p->utime = cputime_add(p->utime, cputime);
3724
3725 /* Add user time to cpustat. */
3726 tmp = cputime_to_cputime64(cputime);
3727 if (TASK_NICE(p) > 0)
3728 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3729 else
3730 cpustat->user = cputime64_add(cpustat->user, tmp);
3731}
3732
3733/*
Laurent Vivier94886b82007-10-15 17:00:19 +02003734 * Account guest cpu time to a process.
3735 * @p: the process that the cpu time gets accounted to
3736 * @cputime: the cpu time spent in virtual machine since the last update
3737 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01003738static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02003739{
3740 cputime64_t tmp;
3741 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3742
3743 tmp = cputime_to_cputime64(cputime);
3744
3745 p->utime = cputime_add(p->utime, cputime);
3746 p->gtime = cputime_add(p->gtime, cputime);
3747
3748 cpustat->user = cputime64_add(cpustat->user, tmp);
3749 cpustat->guest = cputime64_add(cpustat->guest, tmp);
3750}
3751
3752/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07003753 * Account scaled user cpu time to a process.
3754 * @p: the process that the cpu time gets accounted to
3755 * @cputime: the cpu time spent in user space since the last update
3756 */
3757void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
3758{
3759 p->utimescaled = cputime_add(p->utimescaled, cputime);
3760}
3761
3762/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 * Account system cpu time to a process.
3764 * @p: the process that the cpu time gets accounted to
3765 * @hardirq_offset: the offset to subtract from hardirq_count()
3766 * @cputime: the cpu time spent in kernel space since the last update
3767 */
3768void account_system_time(struct task_struct *p, int hardirq_offset,
3769 cputime_t cputime)
3770{
3771 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003772 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 cputime64_t tmp;
3774
Christian Borntraeger97783852007-11-15 20:57:39 +01003775 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0))
3776 return account_guest_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02003777
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 p->stime = cputime_add(p->stime, cputime);
3779
3780 /* Add system time to cpustat. */
3781 tmp = cputime_to_cputime64(cputime);
3782 if (hardirq_count() - hardirq_offset)
3783 cpustat->irq = cputime64_add(cpustat->irq, tmp);
3784 else if (softirq_count())
3785 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003786 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003788 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3790 else
3791 cpustat->idle = cputime64_add(cpustat->idle, tmp);
3792 /* Account for system time used */
3793 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794}
3795
3796/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07003797 * Account scaled system cpu time to a process.
3798 * @p: the process that the cpu time gets accounted to
3799 * @hardirq_offset: the offset to subtract from hardirq_count()
3800 * @cputime: the cpu time spent in kernel space since the last update
3801 */
3802void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
3803{
3804 p->stimescaled = cputime_add(p->stimescaled, cputime);
3805}
3806
3807/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 * Account for involuntary wait time.
3809 * @p: the process from which the cpu time has been stolen
3810 * @steal: the cpu time spent in involuntary wait
3811 */
3812void account_steal_time(struct task_struct *p, cputime_t steal)
3813{
3814 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3815 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07003816 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
3818 if (p == rq->idle) {
3819 p->stime = cputime_add(p->stime, steal);
3820 if (atomic_read(&rq->nr_iowait) > 0)
3821 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3822 else
3823 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003824 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 cpustat->steal = cputime64_add(cpustat->steal, tmp);
3826}
3827
Christoph Lameter7835b982006-12-10 02:20:22 -08003828/*
3829 * This function gets called by the timer code, with HZ frequency.
3830 * We call it with interrupts disabled.
3831 *
3832 * It also gets called by the fork code, when changing the parent's
3833 * timeslices.
3834 */
3835void scheduler_tick(void)
3836{
Christoph Lameter7835b982006-12-10 02:20:22 -08003837 int cpu = smp_processor_id();
3838 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003839 struct task_struct *curr = rq->curr;
Ingo Molnar529c7722007-08-10 23:05:11 +02003840 u64 next_tick = rq->tick_timestamp + TICK_NSEC;
Christoph Lameter7835b982006-12-10 02:20:22 -08003841
Ingo Molnardd41f592007-07-09 18:51:59 +02003842 spin_lock(&rq->lock);
Ingo Molnar546fe3c2007-08-09 11:16:51 +02003843 __update_rq_clock(rq);
Ingo Molnar529c7722007-08-10 23:05:11 +02003844 /*
3845 * Let rq->clock advance by at least TICK_NSEC:
3846 */
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01003847 if (unlikely(rq->clock < next_tick)) {
Ingo Molnar529c7722007-08-10 23:05:11 +02003848 rq->clock = next_tick;
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01003849 rq->clock_underflows++;
3850 }
Ingo Molnar529c7722007-08-10 23:05:11 +02003851 rq->tick_timestamp = rq->clock;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02003852 update_last_tick_seen(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02003853 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01003854 curr->sched_class->task_tick(rq, curr, 0);
3855 update_sched_rt_period(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003856 spin_unlock(&rq->lock);
3857
Christoph Lametere418e1c2006-12-10 02:20:23 -08003858#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02003859 rq->idle_at_tick = idle_cpu(cpu);
3860 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08003861#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862}
3863
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
3865
Srinivasa Ds43627582008-02-23 15:24:04 -08003866void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867{
3868 /*
3869 * Underflow?
3870 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003871 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
3872 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 preempt_count() += val;
3874 /*
3875 * Spinlock count overflowing soon?
3876 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003877 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
3878 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879}
3880EXPORT_SYMBOL(add_preempt_count);
3881
Srinivasa Ds43627582008-02-23 15:24:04 -08003882void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883{
3884 /*
3885 * Underflow?
3886 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003887 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
3888 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 /*
3890 * Is the spinlock portion underflowing?
3891 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003892 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
3893 !(preempt_count() & PREEMPT_MASK)))
3894 return;
3895
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 preempt_count() -= val;
3897}
3898EXPORT_SYMBOL(sub_preempt_count);
3899
3900#endif
3901
3902/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003903 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003905static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906{
Satyam Sharma838225b2007-10-24 18:23:50 +02003907 struct pt_regs *regs = get_irq_regs();
3908
3909 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
3910 prev->comm, prev->pid, preempt_count());
3911
Ingo Molnardd41f592007-07-09 18:51:59 +02003912 debug_show_held_locks(prev);
3913 if (irqs_disabled())
3914 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02003915
3916 if (regs)
3917 show_regs(regs);
3918 else
3919 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02003920}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921
Ingo Molnardd41f592007-07-09 18:51:59 +02003922/*
3923 * Various schedule()-time debugging checks and statistics:
3924 */
3925static inline void schedule_debug(struct task_struct *prev)
3926{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003928 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 * schedule() atomically, we ignore that path for now.
3930 * Otherwise, whine if we are scheduling when we should not be.
3931 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003932 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
3933 __schedule_bug(prev);
3934
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
3936
Ingo Molnar2d723762007-10-15 17:00:12 +02003937 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02003938#ifdef CONFIG_SCHEDSTATS
3939 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003940 schedstat_inc(this_rq(), bkl_count);
3941 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02003942 }
3943#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02003944}
3945
3946/*
3947 * Pick up the highest-prio task:
3948 */
3949static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02003950pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02003951{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003952 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02003953 struct task_struct *p;
3954
3955 /*
3956 * Optimization: we know that if all tasks are in
3957 * the fair class we can call that function directly:
3958 */
3959 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003960 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003961 if (likely(p))
3962 return p;
3963 }
3964
3965 class = sched_class_highest;
3966 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003967 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003968 if (p)
3969 return p;
3970 /*
3971 * Will never be NULL as the idle class always
3972 * returns a non-NULL p:
3973 */
3974 class = class->next;
3975 }
3976}
3977
3978/*
3979 * schedule() is the main scheduler function.
3980 */
3981asmlinkage void __sched schedule(void)
3982{
3983 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08003984 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02003985 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02003986 int cpu;
3987
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988need_resched:
3989 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02003990 cpu = smp_processor_id();
3991 rq = cpu_rq(cpu);
3992 rcu_qsctr_inc(cpu);
3993 prev = rq->curr;
3994 switch_count = &prev->nivcsw;
3995
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 release_kernel_lock(prev);
3997need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998
Ingo Molnardd41f592007-07-09 18:51:59 +02003999 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004001 hrtick_clear(rq);
4002
Ingo Molnar1e819952007-10-15 17:00:13 +02004003 /*
4004 * Do the rq-clock update outside the rq lock:
4005 */
4006 local_irq_disable();
Ingo Molnarc1b3da32007-08-09 11:16:47 +02004007 __update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02004008 spin_lock(&rq->lock);
4009 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010
Ingo Molnardd41f592007-07-09 18:51:59 +02004011 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
4012 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
Roel Kluin23e3c3c2008-03-13 17:41:59 +01004013 signal_pending(prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02004014 prev->state = TASK_RUNNING;
4015 } else {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004016 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02004017 }
4018 switch_count = &prev->nvcsw;
4019 }
4020
Steven Rostedt9a897c52008-01-25 21:08:22 +01004021#ifdef CONFIG_SMP
4022 if (prev->sched_class->pre_schedule)
4023 prev->sched_class->pre_schedule(rq, prev);
4024#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01004025
Ingo Molnardd41f592007-07-09 18:51:59 +02004026 if (unlikely(!rq->nr_running))
4027 idle_balance(cpu, rq);
4028
Ingo Molnar31ee5292007-08-09 11:16:49 +02004029 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004030 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
4032 sched_info_switch(prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02004033
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 rq->nr_switches++;
4036 rq->curr = next;
4037 ++*switch_count;
4038
Ingo Molnardd41f592007-07-09 18:51:59 +02004039 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004040 /*
4041 * the context switch might have flipped the stack from under
4042 * us, hence refresh the local variables.
4043 */
4044 cpu = smp_processor_id();
4045 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 } else
4047 spin_unlock_irq(&rq->lock);
4048
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004049 hrtick_set(rq);
4050
4051 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004053
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 preempt_enable_no_resched();
4055 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4056 goto need_resched;
4057}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058EXPORT_SYMBOL(schedule);
4059
4060#ifdef CONFIG_PREEMPT
4061/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004062 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004063 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 * occur there and call schedule directly.
4065 */
4066asmlinkage void __sched preempt_schedule(void)
4067{
4068 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 struct task_struct *task = current;
4070 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004071
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 /*
4073 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004074 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004076 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 return;
4078
Andi Kleen3a5c3592007-10-15 17:00:14 +02004079 do {
4080 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081
Andi Kleen3a5c3592007-10-15 17:00:14 +02004082 /*
4083 * We keep the big kernel semaphore locked, but we
4084 * clear ->lock_depth so that schedule() doesnt
4085 * auto-release the semaphore:
4086 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004087 saved_lock_depth = task->lock_depth;
4088 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004089 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004090 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004091 sub_preempt_count(PREEMPT_ACTIVE);
4092
4093 /*
4094 * Check again in case we missed a preemption opportunity
4095 * between schedule and now.
4096 */
4097 barrier();
4098 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100EXPORT_SYMBOL(preempt_schedule);
4101
4102/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004103 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 * off of irq context.
4105 * Note, that this is called and return with irqs disabled. This will
4106 * protect us against recursive calling from irq.
4107 */
4108asmlinkage void __sched preempt_schedule_irq(void)
4109{
4110 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 struct task_struct *task = current;
4112 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004113
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004114 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 BUG_ON(ti->preempt_count || !irqs_disabled());
4116
Andi Kleen3a5c3592007-10-15 17:00:14 +02004117 do {
4118 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
Andi Kleen3a5c3592007-10-15 17:00:14 +02004120 /*
4121 * We keep the big kernel semaphore locked, but we
4122 * clear ->lock_depth so that schedule() doesnt
4123 * auto-release the semaphore:
4124 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004125 saved_lock_depth = task->lock_depth;
4126 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004127 local_irq_enable();
4128 schedule();
4129 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004130 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004131 sub_preempt_count(PREEMPT_ACTIVE);
4132
4133 /*
4134 * Check again in case we missed a preemption opportunity
4135 * between schedule and now.
4136 */
4137 barrier();
4138 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139}
4140
4141#endif /* CONFIG_PREEMPT */
4142
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004143int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4144 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004146 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148EXPORT_SYMBOL(default_wake_function);
4149
4150/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004151 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4152 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 * number) then we wake all the non-exclusive tasks and one exclusive task.
4154 *
4155 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004156 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4158 */
4159static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4160 int nr_exclusive, int sync, void *key)
4161{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004162 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004164 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004165 unsigned flags = curr->flags;
4166
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004168 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 break;
4170 }
4171}
4172
4173/**
4174 * __wake_up - wake up threads blocked on a waitqueue.
4175 * @q: the waitqueue
4176 * @mode: which threads
4177 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004178 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004180void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004181 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182{
4183 unsigned long flags;
4184
4185 spin_lock_irqsave(&q->lock, flags);
4186 __wake_up_common(q, mode, nr_exclusive, 0, key);
4187 spin_unlock_irqrestore(&q->lock, flags);
4188}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189EXPORT_SYMBOL(__wake_up);
4190
4191/*
4192 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4193 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004194void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195{
4196 __wake_up_common(q, mode, 1, 0, NULL);
4197}
4198
4199/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004200 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 * @q: the waitqueue
4202 * @mode: which threads
4203 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4204 *
4205 * The sync wakeup differs that the waker knows that it will schedule
4206 * away soon, so while the target thread will be woken up, it will not
4207 * be migrated to another CPU - ie. the two threads are 'synchronized'
4208 * with each other. This can prevent needless bouncing between CPUs.
4209 *
4210 * On UP it can prevent extra preemption.
4211 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004212void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004213__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214{
4215 unsigned long flags;
4216 int sync = 1;
4217
4218 if (unlikely(!q))
4219 return;
4220
4221 if (unlikely(!nr_exclusive))
4222 sync = 0;
4223
4224 spin_lock_irqsave(&q->lock, flags);
4225 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4226 spin_unlock_irqrestore(&q->lock, flags);
4227}
4228EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4229
Ingo Molnarb15136e2007-10-24 18:23:48 +02004230void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231{
4232 unsigned long flags;
4233
4234 spin_lock_irqsave(&x->wait.lock, flags);
4235 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004236 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 spin_unlock_irqrestore(&x->wait.lock, flags);
4238}
4239EXPORT_SYMBOL(complete);
4240
Ingo Molnarb15136e2007-10-24 18:23:48 +02004241void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242{
4243 unsigned long flags;
4244
4245 spin_lock_irqsave(&x->wait.lock, flags);
4246 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004247 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 spin_unlock_irqrestore(&x->wait.lock, flags);
4249}
4250EXPORT_SYMBOL(complete_all);
4251
Andi Kleen8cbbe862007-10-15 17:00:14 +02004252static inline long __sched
4253do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 if (!x->done) {
4256 DECLARE_WAITQUEUE(wait, current);
4257
4258 wait.flags |= WQ_FLAG_EXCLUSIVE;
4259 __add_wait_queue_tail(&x->wait, &wait);
4260 do {
Matthew Wilcox009e5772007-12-06 12:29:54 -05004261 if ((state == TASK_INTERRUPTIBLE &&
4262 signal_pending(current)) ||
4263 (state == TASK_KILLABLE &&
4264 fatal_signal_pending(current))) {
Andi Kleen8cbbe862007-10-15 17:00:14 +02004265 __remove_wait_queue(&x->wait, &wait);
4266 return -ERESTARTSYS;
4267 }
4268 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004270 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 spin_lock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004272 if (!timeout) {
4273 __remove_wait_queue(&x->wait, &wait);
4274 return timeout;
4275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 } while (!x->done);
4277 __remove_wait_queue(&x->wait, &wait);
4278 }
4279 x->done--;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004280 return timeout;
4281}
4282
4283static long __sched
4284wait_for_common(struct completion *x, long timeout, int state)
4285{
4286 might_sleep();
4287
4288 spin_lock_irq(&x->wait.lock);
4289 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004291 return timeout;
4292}
4293
Ingo Molnarb15136e2007-10-24 18:23:48 +02004294void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004295{
4296 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297}
4298EXPORT_SYMBOL(wait_for_completion);
4299
Ingo Molnarb15136e2007-10-24 18:23:48 +02004300unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4302{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004303 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304}
4305EXPORT_SYMBOL(wait_for_completion_timeout);
4306
Andi Kleen8cbbe862007-10-15 17:00:14 +02004307int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308{
Andi Kleen51e97992007-10-18 21:32:55 +02004309 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4310 if (t == -ERESTARTSYS)
4311 return t;
4312 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313}
4314EXPORT_SYMBOL(wait_for_completion_interruptible);
4315
Ingo Molnarb15136e2007-10-24 18:23:48 +02004316unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317wait_for_completion_interruptible_timeout(struct completion *x,
4318 unsigned long timeout)
4319{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004320 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321}
4322EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4323
Matthew Wilcox009e5772007-12-06 12:29:54 -05004324int __sched wait_for_completion_killable(struct completion *x)
4325{
4326 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4327 if (t == -ERESTARTSYS)
4328 return t;
4329 return 0;
4330}
4331EXPORT_SYMBOL(wait_for_completion_killable);
4332
Andi Kleen8cbbe862007-10-15 17:00:14 +02004333static long __sched
4334sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004335{
4336 unsigned long flags;
4337 wait_queue_t wait;
4338
4339 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340
Andi Kleen8cbbe862007-10-15 17:00:14 +02004341 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
Andi Kleen8cbbe862007-10-15 17:00:14 +02004343 spin_lock_irqsave(&q->lock, flags);
4344 __add_wait_queue(q, &wait);
4345 spin_unlock(&q->lock);
4346 timeout = schedule_timeout(timeout);
4347 spin_lock_irq(&q->lock);
4348 __remove_wait_queue(q, &wait);
4349 spin_unlock_irqrestore(&q->lock, flags);
4350
4351 return timeout;
4352}
4353
4354void __sched interruptible_sleep_on(wait_queue_head_t *q)
4355{
4356 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358EXPORT_SYMBOL(interruptible_sleep_on);
4359
Ingo Molnar0fec1712007-07-09 18:52:01 +02004360long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004361interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004363 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4366
Ingo Molnar0fec1712007-07-09 18:52:01 +02004367void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004369 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371EXPORT_SYMBOL(sleep_on);
4372
Ingo Molnar0fec1712007-07-09 18:52:01 +02004373long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004375 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377EXPORT_SYMBOL(sleep_on_timeout);
4378
Ingo Molnarb29739f2006-06-27 02:54:51 -07004379#ifdef CONFIG_RT_MUTEXES
4380
4381/*
4382 * rt_mutex_setprio - set the current priority of a task
4383 * @p: task
4384 * @prio: prio value (kernel-internal form)
4385 *
4386 * This function changes the 'effective' priority of a task. It does
4387 * not touch ->normal_prio like __setscheduler().
4388 *
4389 * Used by the rt_mutex code to implement priority inheritance logic.
4390 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004391void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004392{
4393 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004394 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004395 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01004396 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004397
4398 BUG_ON(prio < 0 || prio > MAX_PRIO);
4399
4400 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004401 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004402
Andrew Mortond5f9f942007-05-08 20:27:06 -07004403 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004404 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004405 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004406 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004407 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004408 if (running)
4409 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004410
4411 if (rt_prio(prio))
4412 p->sched_class = &rt_sched_class;
4413 else
4414 p->sched_class = &fair_sched_class;
4415
Ingo Molnarb29739f2006-06-27 02:54:51 -07004416 p->prio = prio;
4417
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004418 if (running)
4419 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004420 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004421 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004422
4423 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004424 }
4425 task_rq_unlock(rq, &flags);
4426}
4427
4428#endif
4429
Ingo Molnar36c8b582006-07-03 00:25:41 -07004430void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431{
Ingo Molnardd41f592007-07-09 18:51:59 +02004432 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004434 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435
4436 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
4437 return;
4438 /*
4439 * We have to be careful, if called from sys_setpriority(),
4440 * the task might be in the middle of scheduling on another CPU.
4441 */
4442 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004443 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 /*
4445 * The RT priorities are set via sched_setscheduler(), but we still
4446 * allow the 'normal' nice value to be set - but as expected
4447 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02004448 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 */
Ingo Molnare05606d2007-07-09 18:51:59 +02004450 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 p->static_prio = NICE_TO_PRIO(nice);
4452 goto out_unlock;
4453 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004454 on_rq = p->se.on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004455 if (on_rq) {
Ingo Molnar69be72c2007-08-09 11:16:49 +02004456 dequeue_task(rq, p, 0);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004457 dec_load(rq, p);
4458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07004461 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004462 old_prio = p->prio;
4463 p->prio = effective_prio(p);
4464 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465
Ingo Molnardd41f592007-07-09 18:51:59 +02004466 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004467 enqueue_task(rq, p, 0);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004468 inc_load(rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07004470 * If the task increased its priority or is running and
4471 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004473 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 resched_task(rq->curr);
4475 }
4476out_unlock:
4477 task_rq_unlock(rq, &flags);
4478}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479EXPORT_SYMBOL(set_user_nice);
4480
Matt Mackalle43379f2005-05-01 08:59:00 -07004481/*
4482 * can_nice - check if a task can reduce its nice value
4483 * @p: task
4484 * @nice: nice value
4485 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004486int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07004487{
Matt Mackall024f4742005-08-18 11:24:19 -07004488 /* convert nice value [19,-20] to rlimit style value [1,40] */
4489 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004490
Matt Mackalle43379f2005-05-01 08:59:00 -07004491 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
4492 capable(CAP_SYS_NICE));
4493}
4494
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495#ifdef __ARCH_WANT_SYS_NICE
4496
4497/*
4498 * sys_nice - change the priority of the current process.
4499 * @increment: priority increment
4500 *
4501 * sys_setpriority is a more generic, but much slower function that
4502 * does similar things.
4503 */
4504asmlinkage long sys_nice(int increment)
4505{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004506 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507
4508 /*
4509 * Setpriority might change our priority at the same moment.
4510 * We don't have to worry. Conceptually one call occurs first
4511 * and we have a single winner.
4512 */
Matt Mackalle43379f2005-05-01 08:59:00 -07004513 if (increment < -40)
4514 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 if (increment > 40)
4516 increment = 40;
4517
4518 nice = PRIO_TO_NICE(current->static_prio) + increment;
4519 if (nice < -20)
4520 nice = -20;
4521 if (nice > 19)
4522 nice = 19;
4523
Matt Mackalle43379f2005-05-01 08:59:00 -07004524 if (increment < 0 && !can_nice(current, nice))
4525 return -EPERM;
4526
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 retval = security_task_setnice(current, nice);
4528 if (retval)
4529 return retval;
4530
4531 set_user_nice(current, nice);
4532 return 0;
4533}
4534
4535#endif
4536
4537/**
4538 * task_prio - return the priority value of a given task.
4539 * @p: the task in question.
4540 *
4541 * This is the priority value as seen by users in /proc.
4542 * RT tasks are offset by -200. Normal tasks are centered
4543 * around 0, value goes from -16 to +15.
4544 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004545int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546{
4547 return p->prio - MAX_RT_PRIO;
4548}
4549
4550/**
4551 * task_nice - return the nice value of a given task.
4552 * @p: the task in question.
4553 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004554int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555{
4556 return TASK_NICE(p);
4557}
Pavel Roskin150d8be2008-03-05 16:56:37 -05004558EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
4560/**
4561 * idle_cpu - is a given cpu idle currently?
4562 * @cpu: the processor in question.
4563 */
4564int idle_cpu(int cpu)
4565{
4566 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
4567}
4568
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569/**
4570 * idle_task - return the idle task for a given cpu.
4571 * @cpu: the processor in question.
4572 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004573struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574{
4575 return cpu_rq(cpu)->idle;
4576}
4577
4578/**
4579 * find_process_by_pid - find a process with a matching PID value.
4580 * @pid: the pid in question.
4581 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004582static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07004584 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585}
4586
4587/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02004588static void
4589__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590{
Ingo Molnardd41f592007-07-09 18:51:59 +02004591 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004592
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02004594 switch (p->policy) {
4595 case SCHED_NORMAL:
4596 case SCHED_BATCH:
4597 case SCHED_IDLE:
4598 p->sched_class = &fair_sched_class;
4599 break;
4600 case SCHED_FIFO:
4601 case SCHED_RR:
4602 p->sched_class = &rt_sched_class;
4603 break;
4604 }
4605
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004607 p->normal_prio = normal_prio(p);
4608 /* we are holding p->pi_lock already */
4609 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07004610 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611}
4612
4613/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004614 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 * @p: the task in question.
4616 * @policy: new policy.
4617 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004618 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004619 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004621int sched_setscheduler(struct task_struct *p, int policy,
4622 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004624 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01004626 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004627 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628
Steven Rostedt66e53932006-06-27 02:54:44 -07004629 /* may grab non-irq protected spin_locks */
4630 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631recheck:
4632 /* double check policy once rq lock held */
4633 if (policy < 0)
4634 policy = oldpolicy = p->policy;
4635 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02004636 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
4637 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08004638 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 /*
4640 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02004641 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
4642 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 */
4644 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004645 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04004646 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02004648 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 return -EINVAL;
4650
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004651 /*
4652 * Allow unprivileged RT tasks to decrease priority:
4653 */
4654 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02004655 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004656 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004657
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004658 if (!lock_task_sighand(p, &flags))
4659 return -ESRCH;
4660 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
4661 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004662
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004663 /* can't set/change the rt policy */
4664 if (policy != p->policy && !rlim_rtprio)
4665 return -EPERM;
4666
4667 /* can't increase priority */
4668 if (param->sched_priority > p->rt_priority &&
4669 param->sched_priority > rlim_rtprio)
4670 return -EPERM;
4671 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004672 /*
4673 * Like positive nice levels, dont allow tasks to
4674 * move out of SCHED_IDLE either:
4675 */
4676 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
4677 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004678
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004679 /* can't change other user's priorities */
4680 if ((current->euid != p->euid) &&
4681 (current->euid != p->uid))
4682 return -EPERM;
4683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684
Peter Zijlstrab68aa232008-02-13 15:45:40 +01004685#ifdef CONFIG_RT_GROUP_SCHED
4686 /*
4687 * Do not allow realtime tasks into groups that have no runtime
4688 * assigned.
4689 */
4690 if (rt_policy(policy) && task_group(p)->rt_runtime == 0)
4691 return -EPERM;
4692#endif
4693
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 retval = security_task_setscheduler(p, policy, param);
4695 if (retval)
4696 return retval;
4697 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07004698 * make sure no PI-waiters arrive (or leave) while we are
4699 * changing the priority of the task:
4700 */
4701 spin_lock_irqsave(&p->pi_lock, flags);
4702 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 * To be able to change p->policy safely, the apropriate
4704 * runqueue lock must be held.
4705 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07004706 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 /* recheck policy now with rq lock held */
4708 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
4709 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004710 __task_rq_unlock(rq);
4711 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 goto recheck;
4713 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02004714 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004715 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004716 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004717 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004718 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004719 if (running)
4720 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02004721
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004723 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02004724
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004725 if (running)
4726 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004727 if (on_rq) {
4728 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004729
4730 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07004732 __task_rq_unlock(rq);
4733 spin_unlock_irqrestore(&p->pi_lock, flags);
4734
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07004735 rt_mutex_adjust_pi(p);
4736
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 return 0;
4738}
4739EXPORT_SYMBOL_GPL(sched_setscheduler);
4740
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004741static int
4742do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 struct sched_param lparam;
4745 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004746 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747
4748 if (!param || pid < 0)
4749 return -EINVAL;
4750 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
4751 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004752
4753 rcu_read_lock();
4754 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004756 if (p != NULL)
4757 retval = sched_setscheduler(p, policy, &lparam);
4758 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07004759
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 return retval;
4761}
4762
4763/**
4764 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4765 * @pid: the pid in question.
4766 * @policy: new policy.
4767 * @param: structure containing the new RT priority.
4768 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004769asmlinkage long
4770sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771{
Jason Baronc21761f2006-01-18 17:43:03 -08004772 /* negative values for policy are not valid */
4773 if (policy < 0)
4774 return -EINVAL;
4775
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 return do_sched_setscheduler(pid, policy, param);
4777}
4778
4779/**
4780 * sys_sched_setparam - set/change the RT priority of a thread
4781 * @pid: the pid in question.
4782 * @param: structure containing the new RT priority.
4783 */
4784asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
4785{
4786 return do_sched_setscheduler(pid, -1, param);
4787}
4788
4789/**
4790 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4791 * @pid: the pid in question.
4792 */
4793asmlinkage long sys_sched_getscheduler(pid_t pid)
4794{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004795 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004796 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797
4798 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004799 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800
4801 retval = -ESRCH;
4802 read_lock(&tasklist_lock);
4803 p = find_process_by_pid(pid);
4804 if (p) {
4805 retval = security_task_getscheduler(p);
4806 if (!retval)
4807 retval = p->policy;
4808 }
4809 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 return retval;
4811}
4812
4813/**
4814 * sys_sched_getscheduler - get the RT priority of a thread
4815 * @pid: the pid in question.
4816 * @param: structure containing the RT priority.
4817 */
4818asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
4819{
4820 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004821 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004822 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823
4824 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004825 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826
4827 read_lock(&tasklist_lock);
4828 p = find_process_by_pid(pid);
4829 retval = -ESRCH;
4830 if (!p)
4831 goto out_unlock;
4832
4833 retval = security_task_getscheduler(p);
4834 if (retval)
4835 goto out_unlock;
4836
4837 lp.sched_priority = p->rt_priority;
4838 read_unlock(&tasklist_lock);
4839
4840 /*
4841 * This one might sleep, we cannot do it with a spinlock held ...
4842 */
4843 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
4844
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 return retval;
4846
4847out_unlock:
4848 read_unlock(&tasklist_lock);
4849 return retval;
4850}
4851
4852long sched_setaffinity(pid_t pid, cpumask_t new_mask)
4853{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 cpumask_t cpus_allowed;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004855 struct task_struct *p;
4856 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004858 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 read_lock(&tasklist_lock);
4860
4861 p = find_process_by_pid(pid);
4862 if (!p) {
4863 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004864 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 return -ESRCH;
4866 }
4867
4868 /*
4869 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004870 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 * usage count and then drop tasklist_lock.
4872 */
4873 get_task_struct(p);
4874 read_unlock(&tasklist_lock);
4875
4876 retval = -EPERM;
4877 if ((current->euid != p->euid) && (current->euid != p->uid) &&
4878 !capable(CAP_SYS_NICE))
4879 goto out_unlock;
4880
David Quigleye7834f82006-06-23 02:03:59 -07004881 retval = security_task_setscheduler(p, 0, NULL);
4882 if (retval)
4883 goto out_unlock;
4884
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 cpus_allowed = cpuset_cpus_allowed(p);
4886 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07004887 again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 retval = set_cpus_allowed(p, new_mask);
4889
Paul Menage8707d8b2007-10-18 23:40:22 -07004890 if (!retval) {
4891 cpus_allowed = cpuset_cpus_allowed(p);
4892 if (!cpus_subset(new_mask, cpus_allowed)) {
4893 /*
4894 * We must have raced with a concurrent cpuset
4895 * update. Just reset the cpus_allowed to the
4896 * cpuset's cpus_allowed
4897 */
4898 new_mask = cpus_allowed;
4899 goto again;
4900 }
4901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902out_unlock:
4903 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004904 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 return retval;
4906}
4907
4908static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
4909 cpumask_t *new_mask)
4910{
4911 if (len < sizeof(cpumask_t)) {
4912 memset(new_mask, 0, sizeof(cpumask_t));
4913 } else if (len > sizeof(cpumask_t)) {
4914 len = sizeof(cpumask_t);
4915 }
4916 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
4917}
4918
4919/**
4920 * sys_sched_setaffinity - set the cpu affinity of a process
4921 * @pid: pid of the process
4922 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4923 * @user_mask_ptr: user-space pointer to the new cpu mask
4924 */
4925asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
4926 unsigned long __user *user_mask_ptr)
4927{
4928 cpumask_t new_mask;
4929 int retval;
4930
4931 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
4932 if (retval)
4933 return retval;
4934
4935 return sched_setaffinity(pid, new_mask);
4936}
4937
4938/*
4939 * Represents all cpu's present in the system
4940 * In systems capable of hotplug, this map could dynamically grow
4941 * as new cpu's are detected in the system via any platform specific
4942 * method, such as ACPI for e.g.
4943 */
4944
Andi Kleen4cef0c62006-01-11 22:44:57 +01004945cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946EXPORT_SYMBOL(cpu_present_map);
4947
4948#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01004949cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07004950EXPORT_SYMBOL(cpu_online_map);
4951
Andi Kleen4cef0c62006-01-11 22:44:57 +01004952cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07004953EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954#endif
4955
4956long sched_getaffinity(pid_t pid, cpumask_t *mask)
4957{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004958 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004961 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 read_lock(&tasklist_lock);
4963
4964 retval = -ESRCH;
4965 p = find_process_by_pid(pid);
4966 if (!p)
4967 goto out_unlock;
4968
David Quigleye7834f82006-06-23 02:03:59 -07004969 retval = security_task_getscheduler(p);
4970 if (retval)
4971 goto out_unlock;
4972
Jack Steiner2f7016d2006-02-01 03:05:18 -08004973 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974
4975out_unlock:
4976 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004977 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
Ulrich Drepper9531b622007-08-09 11:16:46 +02004979 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980}
4981
4982/**
4983 * sys_sched_getaffinity - get the cpu affinity of a process
4984 * @pid: pid of the process
4985 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4986 * @user_mask_ptr: user-space pointer to hold the current cpu mask
4987 */
4988asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
4989 unsigned long __user *user_mask_ptr)
4990{
4991 int ret;
4992 cpumask_t mask;
4993
4994 if (len < sizeof(cpumask_t))
4995 return -EINVAL;
4996
4997 ret = sched_getaffinity(pid, &mask);
4998 if (ret < 0)
4999 return ret;
5000
5001 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
5002 return -EFAULT;
5003
5004 return sizeof(cpumask_t);
5005}
5006
5007/**
5008 * sys_sched_yield - yield the current processor to other threads.
5009 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005010 * This function yields the current CPU to other tasks. If there are no
5011 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 */
5013asmlinkage long sys_sched_yield(void)
5014{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005015 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016
Ingo Molnar2d723762007-10-15 17:00:12 +02005017 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005018 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019
5020 /*
5021 * Since we are going to call schedule() anyway, there's
5022 * no need to preempt or enable interrupts:
5023 */
5024 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005025 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 _raw_spin_unlock(&rq->lock);
5027 preempt_enable_no_resched();
5028
5029 schedule();
5030
5031 return 0;
5032}
5033
Andrew Mortone7b38402006-06-30 01:56:00 -07005034static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07005036#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5037 __might_sleep(__FILE__, __LINE__);
5038#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005039 /*
5040 * The BKS might be reacquired before we have dropped
5041 * PREEMPT_ACTIVE, which could trigger a second
5042 * cond_resched() call.
5043 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 do {
5045 add_preempt_count(PREEMPT_ACTIVE);
5046 schedule();
5047 sub_preempt_count(PREEMPT_ACTIVE);
5048 } while (need_resched());
5049}
5050
Herbert Xu02b67cc32008-01-25 21:08:28 +01005051#if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY)
5052int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053{
Ingo Molnar94142322006-12-29 16:48:13 -08005054 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5055 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 __cond_resched();
5057 return 1;
5058 }
5059 return 0;
5060}
Herbert Xu02b67cc32008-01-25 21:08:28 +01005061EXPORT_SYMBOL(_cond_resched);
5062#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063
5064/*
5065 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5066 * call schedule, and on return reacquire the lock.
5067 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005068 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 * operations here to prevent schedule() from being called twice (once via
5070 * spin_unlock(), once by hand).
5071 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005072int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073{
Nick Piggin95c354f2008-01-30 13:31:20 +01005074 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005075 int ret = 0;
5076
Nick Piggin95c354f2008-01-30 13:31:20 +01005077 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005079 if (resched && need_resched())
5080 __cond_resched();
5081 else
5082 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005083 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005086 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088EXPORT_SYMBOL(cond_resched_lock);
5089
5090int __sched cond_resched_softirq(void)
5091{
5092 BUG_ON(!in_softirq());
5093
Ingo Molnar94142322006-12-29 16:48:13 -08005094 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07005095 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 __cond_resched();
5097 local_bh_disable();
5098 return 1;
5099 }
5100 return 0;
5101}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102EXPORT_SYMBOL(cond_resched_softirq);
5103
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104/**
5105 * yield - yield the current processor to other threads.
5106 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005107 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 * thread runnable and calls sys_sched_yield().
5109 */
5110void __sched yield(void)
5111{
5112 set_current_state(TASK_RUNNING);
5113 sys_sched_yield();
5114}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115EXPORT_SYMBOL(yield);
5116
5117/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005118 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 * that process accounting knows that this is a task in IO wait state.
5120 *
5121 * But don't do that if it is a deliberate, throttling IO wait (this task
5122 * has set its backing_dev_info: the queue against which it should throttle)
5123 */
5124void __sched io_schedule(void)
5125{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005126 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005128 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 atomic_inc(&rq->nr_iowait);
5130 schedule();
5131 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005132 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134EXPORT_SYMBOL(io_schedule);
5135
5136long __sched io_schedule_timeout(long timeout)
5137{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005138 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 long ret;
5140
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005141 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 atomic_inc(&rq->nr_iowait);
5143 ret = schedule_timeout(timeout);
5144 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005145 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 return ret;
5147}
5148
5149/**
5150 * sys_sched_get_priority_max - return maximum RT priority.
5151 * @policy: scheduling class.
5152 *
5153 * this syscall returns the maximum rt_priority that can be used
5154 * by a given scheduling class.
5155 */
5156asmlinkage long sys_sched_get_priority_max(int policy)
5157{
5158 int ret = -EINVAL;
5159
5160 switch (policy) {
5161 case SCHED_FIFO:
5162 case SCHED_RR:
5163 ret = MAX_USER_RT_PRIO-1;
5164 break;
5165 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005166 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005167 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 ret = 0;
5169 break;
5170 }
5171 return ret;
5172}
5173
5174/**
5175 * sys_sched_get_priority_min - return minimum RT priority.
5176 * @policy: scheduling class.
5177 *
5178 * this syscall returns the minimum rt_priority that can be used
5179 * by a given scheduling class.
5180 */
5181asmlinkage long sys_sched_get_priority_min(int policy)
5182{
5183 int ret = -EINVAL;
5184
5185 switch (policy) {
5186 case SCHED_FIFO:
5187 case SCHED_RR:
5188 ret = 1;
5189 break;
5190 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005191 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005192 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 ret = 0;
5194 }
5195 return ret;
5196}
5197
5198/**
5199 * sys_sched_rr_get_interval - return the default timeslice of a process.
5200 * @pid: pid of the process.
5201 * @interval: userspace pointer to the timeslice value.
5202 *
5203 * this syscall writes the default timeslice value of a given process
5204 * into the user-space timespec buffer. A value of '0' means infinity.
5205 */
5206asmlinkage
5207long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5208{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005209 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005210 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005211 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213
5214 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005215 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216
5217 retval = -ESRCH;
5218 read_lock(&tasklist_lock);
5219 p = find_process_by_pid(pid);
5220 if (!p)
5221 goto out_unlock;
5222
5223 retval = security_task_getscheduler(p);
5224 if (retval)
5225 goto out_unlock;
5226
Ingo Molnar77034932007-12-04 17:04:39 +01005227 /*
5228 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5229 * tasks that are on an otherwise idle runqueue:
5230 */
5231 time_slice = 0;
5232 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005233 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005234 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005235 struct sched_entity *se = &p->se;
5236 unsigned long flags;
5237 struct rq *rq;
5238
5239 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005240 if (rq->cfs.load.weight)
5241 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005242 task_rq_unlock(rq, &flags);
5243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005245 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005248
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249out_unlock:
5250 read_unlock(&tasklist_lock);
5251 return retval;
5252}
5253
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005254static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07005255
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005256void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005259 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005262 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005263 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005264#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005266 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005268 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269#else
5270 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005271 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005273 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274#endif
5275#ifdef CONFIG_DEBUG_STACK_USAGE
5276 {
Al Viro10ebffd2005-11-13 16:06:56 -08005277 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 while (!*n)
5279 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005280 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 }
5282#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005283 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005284 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005286 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287}
5288
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005289void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005291 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292
Ingo Molnar4bd77322007-07-11 21:21:47 +02005293#if BITS_PER_LONG == 32
5294 printk(KERN_INFO
5295 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005297 printk(KERN_INFO
5298 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299#endif
5300 read_lock(&tasklist_lock);
5301 do_each_thread(g, p) {
5302 /*
5303 * reset the NMI-timeout, listing all files on a slow
5304 * console might take alot of time:
5305 */
5306 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005307 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005308 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 } while_each_thread(g, p);
5310
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005311 touch_all_softlockup_watchdogs();
5312
Ingo Molnardd41f592007-07-09 18:51:59 +02005313#ifdef CONFIG_SCHED_DEBUG
5314 sysrq_sched_debug_show();
5315#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005317 /*
5318 * Only show locks if all tasks are dumped:
5319 */
5320 if (state_filter == -1)
5321 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322}
5323
Ingo Molnar1df21052007-07-09 18:51:58 +02005324void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5325{
Ingo Molnardd41f592007-07-09 18:51:59 +02005326 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005327}
5328
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005329/**
5330 * init_idle - set up an idle thread for a given CPU
5331 * @idle: task in question
5332 * @cpu: cpu the idle task belongs to
5333 *
5334 * NOTE: this function does not set the idle thread's NEED_RESCHED
5335 * flag, to make booting more robust.
5336 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005337void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005339 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 unsigned long flags;
5341
Ingo Molnardd41f592007-07-09 18:51:59 +02005342 __sched_fork(idle);
5343 idle->se.exec_start = sched_clock();
5344
Ingo Molnarb29739f2006-06-27 02:54:51 -07005345 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005347 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348
5349 spin_lock_irqsave(&rq->lock, flags);
5350 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005351#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5352 idle->oncpu = 1;
5353#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 spin_unlock_irqrestore(&rq->lock, flags);
5355
5356 /* Set the preempt count _outside_ the spinlocks! */
Al Viroa1261f542005-11-13 16:06:55 -08005357 task_thread_info(idle)->preempt_count = 0;
Ingo Molnar6478d882008-01-25 21:08:33 +01005358
Ingo Molnardd41f592007-07-09 18:51:59 +02005359 /*
5360 * The idle tasks have their own, simple scheduling class:
5361 */
5362 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363}
5364
5365/*
5366 * In a system that switches off the HZ timer nohz_cpu_mask
5367 * indicates which cpus entered this state. This is used
5368 * in the rcu update to wait only for active cpus. For system
5369 * which do not switch off the HZ timer nohz_cpu_mask should
5370 * always be CPU_MASK_NONE.
5371 */
5372cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
5373
Ingo Molnar19978ca2007-11-09 22:39:38 +01005374/*
5375 * Increase the granularity value when there are more CPUs,
5376 * because with more CPUs the 'effective latency' as visible
5377 * to users decreases. But the relationship is not linear,
5378 * so pick a second-best guess by going with the log2 of the
5379 * number of CPUs.
5380 *
5381 * This idea comes from the SD scheduler of Con Kolivas:
5382 */
5383static inline void sched_init_granularity(void)
5384{
5385 unsigned int factor = 1 + ilog2(num_online_cpus());
5386 const unsigned long limit = 200000000;
5387
5388 sysctl_sched_min_granularity *= factor;
5389 if (sysctl_sched_min_granularity > limit)
5390 sysctl_sched_min_granularity = limit;
5391
5392 sysctl_sched_latency *= factor;
5393 if (sysctl_sched_latency > limit)
5394 sysctl_sched_latency = limit;
5395
5396 sysctl_sched_wakeup_granularity *= factor;
5397 sysctl_sched_batch_wakeup_granularity *= factor;
5398}
5399
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400#ifdef CONFIG_SMP
5401/*
5402 * This is how migration works:
5403 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07005404 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 * runqueue and wake up that CPU's migration thread.
5406 * 2) we down() the locked semaphore => thread blocks.
5407 * 3) migration thread wakes up (implicitly it forces the migrated
5408 * thread off the CPU)
5409 * 4) it gets the migration request and checks whether the migrated
5410 * task is still in the wrong runqueue.
5411 * 5) if it's in the wrong runqueue then the migration thread removes
5412 * it and puts it into the right queue.
5413 * 6) migration thread up()s the semaphore.
5414 * 7) we wake up and the migration is done.
5415 */
5416
5417/*
5418 * Change a given task's CPU affinity. Migrate the thread to a
5419 * proper CPU and schedule it away if the CPU it's executing on
5420 * is removed from the allowed bitmask.
5421 *
5422 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005423 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 * call is not atomic; no spinlocks may be held.
5425 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005426int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005428 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005430 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005431 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432
5433 rq = task_rq_lock(p, &flags);
5434 if (!cpus_intersects(new_mask, cpu_online_map)) {
5435 ret = -EINVAL;
5436 goto out;
5437 }
5438
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01005439 if (p->sched_class->set_cpus_allowed)
5440 p->sched_class->set_cpus_allowed(p, &new_mask);
5441 else {
Ingo Molnar0eab9142008-01-25 21:08:19 +01005442 p->cpus_allowed = new_mask;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01005443 p->rt.nr_cpus_allowed = cpus_weight(new_mask);
Gregory Haskins73fe6aae2008-01-25 21:08:07 +01005444 }
5445
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 /* Can the task run on the task's current CPU? If so, we're done */
5447 if (cpu_isset(task_cpu(p), new_mask))
5448 goto out;
5449
5450 if (migrate_task(p, any_online_cpu(new_mask), &req)) {
5451 /* Need help from migration thread: drop lock and wait. */
5452 task_rq_unlock(rq, &flags);
5453 wake_up_process(rq->migration_thread);
5454 wait_for_completion(&req.done);
5455 tlb_migrate_finish(p->mm);
5456 return 0;
5457 }
5458out:
5459 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005460
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 return ret;
5462}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463EXPORT_SYMBOL_GPL(set_cpus_allowed);
5464
5465/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005466 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 * this because either it can't run here any more (set_cpus_allowed()
5468 * away from this CPU, or CPU going down), or because we're
5469 * attempting to rebalance this task on exec (sched_exec).
5470 *
5471 * So we race with normal scheduler movements, but that's OK, as long
5472 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07005473 *
5474 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07005476static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005478 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02005479 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480
5481 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07005482 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483
5484 rq_src = cpu_rq(src_cpu);
5485 rq_dest = cpu_rq(dest_cpu);
5486
5487 double_rq_lock(rq_src, rq_dest);
5488 /* Already moved. */
5489 if (task_cpu(p) != src_cpu)
5490 goto out;
5491 /* Affinity changed (again). */
5492 if (!cpu_isset(dest_cpu, p->cpus_allowed))
5493 goto out;
5494
Ingo Molnardd41f592007-07-09 18:51:59 +02005495 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005496 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005497 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005498
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005500 if (on_rq) {
5501 activate_task(rq_dest, p, 0);
5502 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07005504 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505out:
5506 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07005507 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508}
5509
5510/*
5511 * migration_thread - this is a highprio system thread that performs
5512 * thread migration by bumping thread off CPU then 'pushing' onto
5513 * another runqueue.
5514 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005515static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005518 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519
5520 rq = cpu_rq(cpu);
5521 BUG_ON(rq->migration_thread != current);
5522
5523 set_current_state(TASK_INTERRUPTIBLE);
5524 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005525 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 spin_lock_irq(&rq->lock);
5529
5530 if (cpu_is_offline(cpu)) {
5531 spin_unlock_irq(&rq->lock);
5532 goto wait_to_die;
5533 }
5534
5535 if (rq->active_balance) {
5536 active_load_balance(rq, cpu);
5537 rq->active_balance = 0;
5538 }
5539
5540 head = &rq->migration_queue;
5541
5542 if (list_empty(head)) {
5543 spin_unlock_irq(&rq->lock);
5544 schedule();
5545 set_current_state(TASK_INTERRUPTIBLE);
5546 continue;
5547 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07005548 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 list_del_init(head->next);
5550
Nick Piggin674311d2005-06-25 14:57:27 -07005551 spin_unlock(&rq->lock);
5552 __migrate_task(req->task, cpu, req->dest_cpu);
5553 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554
5555 complete(&req->done);
5556 }
5557 __set_current_state(TASK_RUNNING);
5558 return 0;
5559
5560wait_to_die:
5561 /* Wait for kthread_stop */
5562 set_current_state(TASK_INTERRUPTIBLE);
5563 while (!kthread_should_stop()) {
5564 schedule();
5565 set_current_state(TASK_INTERRUPTIBLE);
5566 }
5567 __set_current_state(TASK_RUNNING);
5568 return 0;
5569}
5570
5571#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005572
5573static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
5574{
5575 int ret;
5576
5577 local_irq_disable();
5578 ret = __migrate_task(p, src_cpu, dest_cpu);
5579 local_irq_enable();
5580 return ret;
5581}
5582
Kirill Korotaev054b9102006-12-10 02:20:11 -08005583/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02005584 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08005585 * NOTE: interrupts should be disabled by the caller
5586 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005587static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588{
Kirill Korotaevefc30812006-06-27 02:54:32 -07005589 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005591 struct rq *rq;
5592 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593
Andi Kleen3a5c3592007-10-15 17:00:14 +02005594 do {
5595 /* On same node? */
5596 mask = node_to_cpumask(cpu_to_node(dead_cpu));
5597 cpus_and(mask, mask, p->cpus_allowed);
5598 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599
Andi Kleen3a5c3592007-10-15 17:00:14 +02005600 /* On any allowed CPU? */
5601 if (dest_cpu == NR_CPUS)
5602 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603
Andi Kleen3a5c3592007-10-15 17:00:14 +02005604 /* No more Mr. Nice Guy. */
5605 if (dest_cpu == NR_CPUS) {
Cliff Wickman470fd642007-10-18 23:40:46 -07005606 cpumask_t cpus_allowed = cpuset_cpus_allowed_locked(p);
5607 /*
5608 * Try to stay on the same cpuset, where the
5609 * current cpuset may be a subset of all cpus.
5610 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005611 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07005612 * called within calls to cpuset_lock/cpuset_unlock.
5613 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02005614 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07005615 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005616 dest_cpu = any_online_cpu(p->cpus_allowed);
5617 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618
Andi Kleen3a5c3592007-10-15 17:00:14 +02005619 /*
5620 * Don't tell them about moving exiting tasks or
5621 * kernel threads (both mm NULL), since they never
5622 * leave kernel.
5623 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005624 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02005625 printk(KERN_INFO "process %d (%s) no "
5626 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005627 task_pid_nr(p), p->comm, dead_cpu);
5628 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02005629 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005630 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631}
5632
5633/*
5634 * While a dead CPU has no uninterruptible tasks queued at this point,
5635 * it might still have a nonzero ->nr_uninterruptible counter, because
5636 * for performance reasons the counter is not stricly tracking tasks to
5637 * their home CPUs. So we just add the counter to another CPU's counter,
5638 * to keep the global sum constant after CPU-down:
5639 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07005640static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005642 struct rq *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 unsigned long flags;
5644
5645 local_irq_save(flags);
5646 double_rq_lock(rq_src, rq_dest);
5647 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
5648 rq_src->nr_uninterruptible = 0;
5649 double_rq_unlock(rq_src, rq_dest);
5650 local_irq_restore(flags);
5651}
5652
5653/* Run through task list and migrate tasks from the dead cpu. */
5654static void migrate_live_tasks(int src_cpu)
5655{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005656 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005658 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659
Ingo Molnar48f24c42006-07-03 00:25:40 -07005660 do_each_thread(t, p) {
5661 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 continue;
5663
Ingo Molnar48f24c42006-07-03 00:25:40 -07005664 if (task_cpu(p) == src_cpu)
5665 move_task_off_dead_cpu(src_cpu, p);
5666 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005668 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669}
5670
Ingo Molnardd41f592007-07-09 18:51:59 +02005671/*
5672 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005673 * It does so by boosting its priority to highest possible.
5674 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 */
5676void sched_idle_next(void)
5677{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005678 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07005679 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 struct task_struct *p = rq->idle;
5681 unsigned long flags;
5682
5683 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005684 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685
Ingo Molnar48f24c42006-07-03 00:25:40 -07005686 /*
5687 * Strictly not necessary since rest of the CPUs are stopped by now
5688 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 */
5690 spin_lock_irqsave(&rq->lock, flags);
5691
Ingo Molnardd41f592007-07-09 18:51:59 +02005692 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005693
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005694 update_rq_clock(rq);
5695 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
5697 spin_unlock_irqrestore(&rq->lock, flags);
5698}
5699
Ingo Molnar48f24c42006-07-03 00:25:40 -07005700/*
5701 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 * offline.
5703 */
5704void idle_task_exit(void)
5705{
5706 struct mm_struct *mm = current->active_mm;
5707
5708 BUG_ON(cpu_online(smp_processor_id()));
5709
5710 if (mm != &init_mm)
5711 switch_mm(mm, &init_mm, current);
5712 mmdrop(mm);
5713}
5714
Kirill Korotaev054b9102006-12-10 02:20:11 -08005715/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005716static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005718 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719
5720 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07005721 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722
5723 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07005724 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725
Ingo Molnar48f24c42006-07-03 00:25:40 -07005726 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727
5728 /*
5729 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005730 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 * fine.
5732 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005733 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005734 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005735 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736
Ingo Molnar48f24c42006-07-03 00:25:40 -07005737 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738}
5739
5740/* release_task() removes task from tasklist, so we won't find dead tasks. */
5741static void migrate_dead_tasks(unsigned int dead_cpu)
5742{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005743 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005744 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745
Ingo Molnardd41f592007-07-09 18:51:59 +02005746 for ( ; ; ) {
5747 if (!rq->nr_running)
5748 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02005749 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02005750 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02005751 if (!next)
5752 break;
5753 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02005754
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 }
5756}
5757#endif /* CONFIG_HOTPLUG_CPU */
5758
Nick Piggine692ab52007-07-26 13:40:43 +02005759#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5760
5761static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005762 {
5763 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005764 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005765 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01005766 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02005767};
5768
5769static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005770 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005771 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005772 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005773 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005774 .child = sd_ctl_dir,
5775 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01005776 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02005777};
5778
5779static struct ctl_table *sd_alloc_ctl_entry(int n)
5780{
5781 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02005782 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02005783
Nick Piggine692ab52007-07-26 13:40:43 +02005784 return entry;
5785}
5786
Milton Miller6382bc92007-10-15 17:00:19 +02005787static void sd_free_ctl_entry(struct ctl_table **tablep)
5788{
Milton Millercd7900762007-10-17 16:55:11 +02005789 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02005790
Milton Millercd7900762007-10-17 16:55:11 +02005791 /*
5792 * In the intermediate directories, both the child directory and
5793 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005794 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02005795 * static strings and all have proc handlers.
5796 */
5797 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02005798 if (entry->child)
5799 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02005800 if (entry->proc_handler == NULL)
5801 kfree(entry->procname);
5802 }
Milton Miller6382bc92007-10-15 17:00:19 +02005803
5804 kfree(*tablep);
5805 *tablep = NULL;
5806}
5807
Nick Piggine692ab52007-07-26 13:40:43 +02005808static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02005809set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02005810 const char *procname, void *data, int maxlen,
5811 mode_t mode, proc_handler *proc_handler)
5812{
Nick Piggine692ab52007-07-26 13:40:43 +02005813 entry->procname = procname;
5814 entry->data = data;
5815 entry->maxlen = maxlen;
5816 entry->mode = mode;
5817 entry->proc_handler = proc_handler;
5818}
5819
5820static struct ctl_table *
5821sd_alloc_ctl_domain_table(struct sched_domain *sd)
5822{
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005823 struct ctl_table *table = sd_alloc_ctl_entry(12);
Nick Piggine692ab52007-07-26 13:40:43 +02005824
Milton Millerad1cdc12007-10-15 17:00:19 +02005825 if (table == NULL)
5826 return NULL;
5827
Alexey Dobriyane0361852007-08-09 11:16:46 +02005828 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005829 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005830 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005831 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005832 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005833 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005834 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005835 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005836 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005837 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005838 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005839 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005840 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005841 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005842 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02005843 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005844 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02005845 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005846 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02005847 &sd->cache_nice_tries,
5848 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005849 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02005850 sizeof(int), 0644, proc_dointvec_minmax);
Milton Miller6323469f2007-10-15 17:00:19 +02005851 /* &table[11] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02005852
5853 return table;
5854}
5855
Ingo Molnar9a4e7152007-11-28 15:52:56 +01005856static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02005857{
5858 struct ctl_table *entry, *table;
5859 struct sched_domain *sd;
5860 int domain_num = 0, i;
5861 char buf[32];
5862
5863 for_each_domain(cpu, sd)
5864 domain_num++;
5865 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02005866 if (table == NULL)
5867 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02005868
5869 i = 0;
5870 for_each_domain(cpu, sd) {
5871 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005872 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005873 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005874 entry->child = sd_alloc_ctl_domain_table(sd);
5875 entry++;
5876 i++;
5877 }
5878 return table;
5879}
5880
5881static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02005882static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005883{
5884 int i, cpu_num = num_online_cpus();
5885 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
5886 char buf[32];
5887
Milton Miller73785472007-10-24 18:23:48 +02005888 WARN_ON(sd_ctl_dir[0].child);
5889 sd_ctl_dir[0].child = entry;
5890
Milton Millerad1cdc12007-10-15 17:00:19 +02005891 if (entry == NULL)
5892 return;
5893
Milton Miller97b6ea72007-10-15 17:00:19 +02005894 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02005895 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005896 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005897 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005898 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02005899 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02005900 }
Milton Miller73785472007-10-24 18:23:48 +02005901
5902 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02005903 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
5904}
Milton Miller6382bc92007-10-15 17:00:19 +02005905
Milton Miller73785472007-10-24 18:23:48 +02005906/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02005907static void unregister_sched_domain_sysctl(void)
5908{
Milton Miller73785472007-10-24 18:23:48 +02005909 if (sd_sysctl_header)
5910 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02005911 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02005912 if (sd_ctl_dir[0].child)
5913 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02005914}
Nick Piggine692ab52007-07-26 13:40:43 +02005915#else
Milton Miller6382bc92007-10-15 17:00:19 +02005916static void register_sched_domain_sysctl(void)
5917{
5918}
5919static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005920{
5921}
5922#endif
5923
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924/*
5925 * migration_call - callback that gets triggered when a CPU is added.
5926 * Here we can start up the necessary migration thread for the new CPU.
5927 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005928static int __cpuinit
5929migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005932 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005934 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935
5936 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005937
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005939 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02005940 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 if (IS_ERR(p))
5942 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 kthread_bind(p, cpu);
5944 /* Must be high prio: stop_machine expects to yield to it. */
5945 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02005946 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 task_rq_unlock(rq, &flags);
5948 cpu_rq(cpu)->migration_thread = p;
5949 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005950
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005952 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02005953 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005955
5956 /* Update our root-domain */
5957 rq = cpu_rq(cpu);
5958 spin_lock_irqsave(&rq->lock, flags);
5959 if (rq->rd) {
5960 BUG_ON(!cpu_isset(cpu, rq->rd->span));
5961 cpu_set(cpu, rq->rd->online);
5962 }
5963 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005965
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966#ifdef CONFIG_HOTPLUG_CPU
5967 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005968 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07005969 if (!cpu_rq(cpu)->migration_thread)
5970 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005971 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08005972 kthread_bind(cpu_rq(cpu)->migration_thread,
5973 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 kthread_stop(cpu_rq(cpu)->migration_thread);
5975 cpu_rq(cpu)->migration_thread = NULL;
5976 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005977
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005979 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07005980 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 migrate_live_tasks(cpu);
5982 rq = cpu_rq(cpu);
5983 kthread_stop(rq->migration_thread);
5984 rq->migration_thread = NULL;
5985 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07005986 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005987 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005988 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02005990 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
5991 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07005993 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07005994 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 migrate_nr_uninterruptible(rq);
5996 BUG_ON(rq->nr_running != 0);
5997
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005998 /*
5999 * No need to migrate the tasks: it was best-effort if
6000 * they didn't take sched_hotcpu_mutex. Just wake up
6001 * the requestors.
6002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003 spin_lock_irq(&rq->lock);
6004 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006005 struct migration_req *req;
6006
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07006008 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 list_del_init(&req->list);
6010 complete(&req->done);
6011 }
6012 spin_unlock_irq(&rq->lock);
6013 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006014
Gregory Haskins08f503b2008-03-10 17:59:11 -04006015 case CPU_DYING:
6016 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01006017 /* Update our root-domain */
6018 rq = cpu_rq(cpu);
6019 spin_lock_irqsave(&rq->lock, flags);
6020 if (rq->rd) {
6021 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6022 cpu_clear(cpu, rq->rd->online);
6023 }
6024 spin_unlock_irqrestore(&rq->lock, flags);
6025 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026#endif
6027 }
6028 return NOTIFY_OK;
6029}
6030
6031/* Register at highest priority so that task migration (migrate_all_tasks)
6032 * happens before everything else.
6033 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006034static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 .notifier_call = migration_call,
6036 .priority = 10
6037};
6038
Adrian Bunke6fe6642007-11-09 22:39:39 +01006039void __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040{
6041 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006042 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006043
6044 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006045 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6046 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6048 register_cpu_notifier(&migration_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049}
6050#endif
6051
6052#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006053
6054/* Number of possible processor ids */
6055int nr_cpu_ids __read_mostly = NR_CPUS;
6056EXPORT_SYMBOL(nr_cpu_ids);
6057
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006058#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006059
6060static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level)
6061{
6062 struct sched_group *group = sd->groups;
6063 cpumask_t groupmask;
6064 char str[NR_CPUS];
6065
6066 cpumask_scnprintf(str, NR_CPUS, sd->span);
6067 cpus_clear(groupmask);
6068
6069 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6070
6071 if (!(sd->flags & SD_LOAD_BALANCE)) {
6072 printk("does not load-balance\n");
6073 if (sd->parent)
6074 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6075 " has parent");
6076 return -1;
6077 }
6078
6079 printk(KERN_CONT "span %s\n", str);
6080
6081 if (!cpu_isset(cpu, sd->span)) {
6082 printk(KERN_ERR "ERROR: domain->span does not contain "
6083 "CPU%d\n", cpu);
6084 }
6085 if (!cpu_isset(cpu, group->cpumask)) {
6086 printk(KERN_ERR "ERROR: domain->groups does not contain"
6087 " CPU%d\n", cpu);
6088 }
6089
6090 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6091 do {
6092 if (!group) {
6093 printk("\n");
6094 printk(KERN_ERR "ERROR: group is NULL\n");
6095 break;
6096 }
6097
6098 if (!group->__cpu_power) {
6099 printk(KERN_CONT "\n");
6100 printk(KERN_ERR "ERROR: domain->cpu_power not "
6101 "set\n");
6102 break;
6103 }
6104
6105 if (!cpus_weight(group->cpumask)) {
6106 printk(KERN_CONT "\n");
6107 printk(KERN_ERR "ERROR: empty group\n");
6108 break;
6109 }
6110
6111 if (cpus_intersects(groupmask, group->cpumask)) {
6112 printk(KERN_CONT "\n");
6113 printk(KERN_ERR "ERROR: repeated CPUs\n");
6114 break;
6115 }
6116
6117 cpus_or(groupmask, groupmask, group->cpumask);
6118
6119 cpumask_scnprintf(str, NR_CPUS, group->cpumask);
6120 printk(KERN_CONT " %s", str);
6121
6122 group = group->next;
6123 } while (group != sd->groups);
6124 printk(KERN_CONT "\n");
6125
6126 if (!cpus_equal(sd->span, groupmask))
6127 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6128
6129 if (sd->parent && !cpus_subset(groupmask, sd->parent->span))
6130 printk(KERN_ERR "ERROR: parent span is not a superset "
6131 "of domain->span\n");
6132 return 0;
6133}
6134
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135static void sched_domain_debug(struct sched_domain *sd, int cpu)
6136{
6137 int level = 0;
6138
Nick Piggin41c7ce92005-06-25 14:57:24 -07006139 if (!sd) {
6140 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6141 return;
6142 }
6143
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6145
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006146 for (;;) {
6147 if (sched_domain_debug_one(sd, cpu, level))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 level++;
6150 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006151 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006152 break;
6153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154}
6155#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07006156# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157#endif
6158
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006159static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006160{
6161 if (cpus_weight(sd->span) == 1)
6162 return 1;
6163
6164 /* Following flags need at least 2 groups */
6165 if (sd->flags & (SD_LOAD_BALANCE |
6166 SD_BALANCE_NEWIDLE |
6167 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006168 SD_BALANCE_EXEC |
6169 SD_SHARE_CPUPOWER |
6170 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006171 if (sd->groups != sd->groups->next)
6172 return 0;
6173 }
6174
6175 /* Following flags don't use groups */
6176 if (sd->flags & (SD_WAKE_IDLE |
6177 SD_WAKE_AFFINE |
6178 SD_WAKE_BALANCE))
6179 return 0;
6180
6181 return 1;
6182}
6183
Ingo Molnar48f24c42006-07-03 00:25:40 -07006184static int
6185sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006186{
6187 unsigned long cflags = sd->flags, pflags = parent->flags;
6188
6189 if (sd_degenerate(parent))
6190 return 1;
6191
6192 if (!cpus_equal(sd->span, parent->span))
6193 return 0;
6194
6195 /* Does parent contain flags not in child? */
6196 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6197 if (cflags & SD_WAKE_AFFINE)
6198 pflags &= ~SD_WAKE_BALANCE;
6199 /* Flags needing groups don't count if only 1 group in parent */
6200 if (parent->groups == parent->groups->next) {
6201 pflags &= ~(SD_LOAD_BALANCE |
6202 SD_BALANCE_NEWIDLE |
6203 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006204 SD_BALANCE_EXEC |
6205 SD_SHARE_CPUPOWER |
6206 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006207 }
6208 if (~cflags & pflags)
6209 return 0;
6210
6211 return 1;
6212}
6213
Gregory Haskins57d885f2008-01-25 21:08:18 +01006214static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6215{
6216 unsigned long flags;
6217 const struct sched_class *class;
6218
6219 spin_lock_irqsave(&rq->lock, flags);
6220
6221 if (rq->rd) {
6222 struct root_domain *old_rd = rq->rd;
6223
Ingo Molnar0eab9142008-01-25 21:08:19 +01006224 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006225 if (class->leave_domain)
6226 class->leave_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006227 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006228
Gregory Haskinsdc938522008-01-25 21:08:26 +01006229 cpu_clear(rq->cpu, old_rd->span);
6230 cpu_clear(rq->cpu, old_rd->online);
6231
Gregory Haskins57d885f2008-01-25 21:08:18 +01006232 if (atomic_dec_and_test(&old_rd->refcount))
6233 kfree(old_rd);
6234 }
6235
6236 atomic_inc(&rd->refcount);
6237 rq->rd = rd;
6238
Gregory Haskinsdc938522008-01-25 21:08:26 +01006239 cpu_set(rq->cpu, rd->span);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006240 if (cpu_isset(rq->cpu, cpu_online_map))
6241 cpu_set(rq->cpu, rd->online);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006242
Ingo Molnar0eab9142008-01-25 21:08:19 +01006243 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006244 if (class->join_domain)
6245 class->join_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006246 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006247
6248 spin_unlock_irqrestore(&rq->lock, flags);
6249}
6250
Gregory Haskinsdc938522008-01-25 21:08:26 +01006251static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006252{
6253 memset(rd, 0, sizeof(*rd));
6254
Gregory Haskinsdc938522008-01-25 21:08:26 +01006255 cpus_clear(rd->span);
6256 cpus_clear(rd->online);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006257}
6258
6259static void init_defrootdomain(void)
6260{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006261 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006262 atomic_set(&def_root_domain.refcount, 1);
6263}
6264
Gregory Haskinsdc938522008-01-25 21:08:26 +01006265static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006266{
6267 struct root_domain *rd;
6268
6269 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6270 if (!rd)
6271 return NULL;
6272
Gregory Haskinsdc938522008-01-25 21:08:26 +01006273 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006274
6275 return rd;
6276}
6277
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006279 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 * hold the hotplug lock.
6281 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006282static void
6283cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006285 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006286 struct sched_domain *tmp;
6287
6288 /* Remove the sched domains which do not contribute to scheduling. */
6289 for (tmp = sd; tmp; tmp = tmp->parent) {
6290 struct sched_domain *parent = tmp->parent;
6291 if (!parent)
6292 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006293 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006294 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006295 if (parent->parent)
6296 parent->parent->child = tmp;
6297 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07006298 }
6299
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006300 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006301 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006302 if (sd)
6303 sd->child = NULL;
6304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305
6306 sched_domain_debug(sd, cpu);
6307
Gregory Haskins57d885f2008-01-25 21:08:18 +01006308 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006309 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310}
6311
6312/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006313static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314
6315/* Setup the mask of cpus configured for isolated domains */
6316static int __init isolated_cpu_setup(char *str)
6317{
6318 int ints[NR_CPUS], i;
6319
6320 str = get_options(str, ARRAY_SIZE(ints), ints);
6321 cpus_clear(cpu_isolated_map);
6322 for (i = 1; i <= ints[0]; i++)
6323 if (ints[i] < NR_CPUS)
6324 cpu_set(ints[i], cpu_isolated_map);
6325 return 1;
6326}
6327
Ingo Molnar8927f492007-10-15 17:00:13 +02006328__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329
6330/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006331 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6332 * to a function which identifies what group(along with sched group) a CPU
6333 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6334 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 *
6336 * init_sched_build_groups will build a circular linked list of the groups
6337 * covered by the given span, and will set each group's ->cpumask correctly,
6338 * and ->cpu_power to 0.
6339 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006340static void
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006341init_sched_build_groups(cpumask_t span, const cpumask_t *cpu_map,
6342 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
6343 struct sched_group **sg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344{
6345 struct sched_group *first = NULL, *last = NULL;
6346 cpumask_t covered = CPU_MASK_NONE;
6347 int i;
6348
6349 for_each_cpu_mask(i, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006350 struct sched_group *sg;
6351 int group = group_fn(i, cpu_map, &sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352 int j;
6353
6354 if (cpu_isset(i, covered))
6355 continue;
6356
6357 sg->cpumask = CPU_MASK_NONE;
Eric Dumazet5517d862007-05-08 00:32:57 -07006358 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359
6360 for_each_cpu_mask(j, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006361 if (group_fn(j, cpu_map, NULL) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 continue;
6363
6364 cpu_set(j, covered);
6365 cpu_set(j, sg->cpumask);
6366 }
6367 if (!first)
6368 first = sg;
6369 if (last)
6370 last->next = sg;
6371 last = sg;
6372 }
6373 last->next = first;
6374}
6375
John Hawkes9c1cfda2005-09-06 15:18:14 -07006376#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377
John Hawkes9c1cfda2005-09-06 15:18:14 -07006378#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006379
John Hawkes9c1cfda2005-09-06 15:18:14 -07006380/**
6381 * find_next_best_node - find the next node to include in a sched_domain
6382 * @node: node whose sched_domain we're building
6383 * @used_nodes: nodes already in the sched_domain
6384 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006385 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006386 * finds the closest node not already in the @used_nodes map.
6387 *
6388 * Should use nodemask_t.
6389 */
6390static int find_next_best_node(int node, unsigned long *used_nodes)
6391{
6392 int i, n, val, min_val, best_node = 0;
6393
6394 min_val = INT_MAX;
6395
6396 for (i = 0; i < MAX_NUMNODES; i++) {
6397 /* Start at @node */
6398 n = (node + i) % MAX_NUMNODES;
6399
6400 if (!nr_cpus_node(n))
6401 continue;
6402
6403 /* Skip already used nodes */
6404 if (test_bit(n, used_nodes))
6405 continue;
6406
6407 /* Simple min distance search */
6408 val = node_distance(node, n);
6409
6410 if (val < min_val) {
6411 min_val = val;
6412 best_node = n;
6413 }
6414 }
6415
6416 set_bit(best_node, used_nodes);
6417 return best_node;
6418}
6419
6420/**
6421 * sched_domain_node_span - get a cpumask for a node's sched_domain
6422 * @node: node whose cpumask we're constructing
6423 * @size: number of nodes to include in this span
6424 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006425 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07006426 * should be one that prevents unnecessary balancing, but also spreads tasks
6427 * out optimally.
6428 */
6429static cpumask_t sched_domain_node_span(int node)
6430{
John Hawkes9c1cfda2005-09-06 15:18:14 -07006431 DECLARE_BITMAP(used_nodes, MAX_NUMNODES);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006432 cpumask_t span, nodemask;
6433 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006434
6435 cpus_clear(span);
6436 bitmap_zero(used_nodes, MAX_NUMNODES);
6437
6438 nodemask = node_to_cpumask(node);
6439 cpus_or(span, span, nodemask);
6440 set_bit(node, used_nodes);
6441
6442 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
6443 int next_node = find_next_best_node(node, used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006444
John Hawkes9c1cfda2005-09-06 15:18:14 -07006445 nodemask = node_to_cpumask(next_node);
6446 cpus_or(span, span, nodemask);
6447 }
6448
6449 return span;
6450}
6451#endif
6452
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006453int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006454
John Hawkes9c1cfda2005-09-06 15:18:14 -07006455/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07006456 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07006457 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458#ifdef CONFIG_SCHED_SMT
6459static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006460static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006461
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006462static int
6463cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006465 if (sg)
6466 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467 return cpu;
6468}
6469#endif
6470
Ingo Molnar48f24c42006-07-03 00:25:40 -07006471/*
6472 * multi-core sched-domains:
6473 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006474#ifdef CONFIG_SCHED_MC
6475static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006476static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006477#endif
6478
6479#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006480static int
6481cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006482{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006483 int group;
Mike Travisd5a74302007-10-16 01:24:05 -07006484 cpumask_t mask = per_cpu(cpu_sibling_map, cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006485 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006486 group = first_cpu(mask);
6487 if (sg)
6488 *sg = &per_cpu(sched_group_core, group);
6489 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006490}
6491#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006492static int
6493cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006494{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006495 if (sg)
6496 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006497 return cpu;
6498}
6499#endif
6500
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006502static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006503
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006504static int
6505cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006507 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006508#ifdef CONFIG_SCHED_MC
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006509 cpumask_t mask = cpu_coregroup_map(cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006510 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006511 group = first_cpu(mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006512#elif defined(CONFIG_SCHED_SMT)
Mike Travisd5a74302007-10-16 01:24:05 -07006513 cpumask_t mask = per_cpu(cpu_sibling_map, cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006514 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006515 group = first_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006517 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006519 if (sg)
6520 *sg = &per_cpu(sched_group_phys, group);
6521 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522}
6523
6524#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07006525/*
6526 * The init_sched_build_groups can't handle what we want to do with node
6527 * groups, so roll our own. Now each node has its own list of groups which
6528 * gets dynamically allocated.
6529 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530static DEFINE_PER_CPU(struct sched_domain, node_domains);
John Hawkesd1b55132005-09-06 15:18:14 -07006531static struct sched_group **sched_group_nodes_bycpu[NR_CPUS];
John Hawkes9c1cfda2005-09-06 15:18:14 -07006532
6533static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006534static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006535
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006536static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
6537 struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006539 cpumask_t nodemask = node_to_cpumask(cpu_to_node(cpu));
6540 int group;
6541
6542 cpus_and(nodemask, nodemask, *cpu_map);
6543 group = first_cpu(nodemask);
6544
6545 if (sg)
6546 *sg = &per_cpu(sched_group_allnodes, group);
6547 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006549
Siddha, Suresh B08069032006-03-27 01:15:23 -08006550static void init_numa_sched_groups_power(struct sched_group *group_head)
6551{
6552 struct sched_group *sg = group_head;
6553 int j;
6554
6555 if (!sg)
6556 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006557 do {
6558 for_each_cpu_mask(j, sg->cpumask) {
6559 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08006560
Andi Kleen3a5c3592007-10-15 17:00:14 +02006561 sd = &per_cpu(phys_domains, j);
6562 if (j != first_cpu(sd->groups->cpumask)) {
6563 /*
6564 * Only add "power" once for each
6565 * physical package.
6566 */
6567 continue;
6568 }
6569
6570 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006571 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006572 sg = sg->next;
6573 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006574}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575#endif
6576
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006577#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006578/* Free memory allocated for various sched_group structures */
6579static void free_sched_groups(const cpumask_t *cpu_map)
6580{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006581 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006582
6583 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006584 struct sched_group **sched_group_nodes
6585 = sched_group_nodes_bycpu[cpu];
6586
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006587 if (!sched_group_nodes)
6588 continue;
6589
6590 for (i = 0; i < MAX_NUMNODES; i++) {
6591 cpumask_t nodemask = node_to_cpumask(i);
6592 struct sched_group *oldsg, *sg = sched_group_nodes[i];
6593
6594 cpus_and(nodemask, nodemask, *cpu_map);
6595 if (cpus_empty(nodemask))
6596 continue;
6597
6598 if (sg == NULL)
6599 continue;
6600 sg = sg->next;
6601next_sg:
6602 oldsg = sg;
6603 sg = sg->next;
6604 kfree(oldsg);
6605 if (oldsg != sched_group_nodes[i])
6606 goto next_sg;
6607 }
6608 kfree(sched_group_nodes);
6609 sched_group_nodes_bycpu[cpu] = NULL;
6610 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006611}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006612#else
6613static void free_sched_groups(const cpumask_t *cpu_map)
6614{
6615}
6616#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006617
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006619 * Initialize sched groups cpu_power.
6620 *
6621 * cpu_power indicates the capacity of sched group, which is used while
6622 * distributing the load between different sched groups in a sched domain.
6623 * Typically cpu_power for all the groups in a sched domain will be same unless
6624 * there are asymmetries in the topology. If there are asymmetries, group
6625 * having more cpu_power will pickup more load compared to the group having
6626 * less cpu_power.
6627 *
6628 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
6629 * the maximum number of tasks a group can handle in the presence of other idle
6630 * or lightly loaded groups in the same sched domain.
6631 */
6632static void init_sched_groups_power(int cpu, struct sched_domain *sd)
6633{
6634 struct sched_domain *child;
6635 struct sched_group *group;
6636
6637 WARN_ON(!sd || !sd->groups);
6638
6639 if (cpu != first_cpu(sd->groups->cpumask))
6640 return;
6641
6642 child = sd->child;
6643
Eric Dumazet5517d862007-05-08 00:32:57 -07006644 sd->groups->__cpu_power = 0;
6645
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006646 /*
6647 * For perf policy, if the groups in child domain share resources
6648 * (for example cores sharing some portions of the cache hierarchy
6649 * or SMT), then set this domain groups cpu_power such that each group
6650 * can handle only one task, when there are other idle groups in the
6651 * same sched domain.
6652 */
6653 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
6654 (child->flags &
6655 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07006656 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006657 return;
6658 }
6659
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006660 /*
6661 * add cpu_power of each child group to this groups cpu_power
6662 */
6663 group = child->groups;
6664 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07006665 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006666 group = group->next;
6667 } while (group != child->groups);
6668}
6669
6670/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006671 * Build sched domains for a given set of cpus and attach the sched domains
6672 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006674static int build_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675{
6676 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006677 struct root_domain *rd;
John Hawkesd1b55132005-09-06 15:18:14 -07006678#ifdef CONFIG_NUMA
6679 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006680 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07006681
6682 /*
6683 * Allocate the per-node list of sched groups
6684 */
Milton Miller5cf9f062007-10-15 17:00:19 +02006685 sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006686 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07006687 if (!sched_group_nodes) {
6688 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006689 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07006690 }
6691 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
6692#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693
Gregory Haskinsdc938522008-01-25 21:08:26 +01006694 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01006695 if (!rd) {
6696 printk(KERN_WARNING "Cannot alloc root domain\n");
6697 return -ENOMEM;
6698 }
6699
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006701 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006703 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704 struct sched_domain *sd = NULL, *p;
6705 cpumask_t nodemask = node_to_cpumask(cpu_to_node(i));
6706
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006707 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708
6709#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02006710 if (cpus_weight(*cpu_map) >
6711 SD_NODES_PER_DOMAIN*cpus_weight(nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07006712 sd = &per_cpu(allnodes_domains, i);
6713 *sd = SD_ALLNODES_INIT;
6714 sd->span = *cpu_map;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006715 cpu_to_allnodes_group(i, cpu_map, &sd->groups);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006716 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006717 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006718 } else
6719 p = NULL;
6720
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721 sd = &per_cpu(node_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722 *sd = SD_NODE_INIT;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006723 sd->span = sched_domain_node_span(cpu_to_node(i));
6724 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006725 if (p)
6726 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006727 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728#endif
6729
6730 p = sd;
6731 sd = &per_cpu(phys_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732 *sd = SD_CPU_INIT;
6733 sd->span = nodemask;
6734 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006735 if (p)
6736 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006737 cpu_to_phys_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006739#ifdef CONFIG_SCHED_MC
6740 p = sd;
6741 sd = &per_cpu(core_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006742 *sd = SD_MC_INIT;
6743 sd->span = cpu_coregroup_map(i);
6744 cpus_and(sd->span, sd->span, *cpu_map);
6745 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006746 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006747 cpu_to_core_group(i, cpu_map, &sd->groups);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006748#endif
6749
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750#ifdef CONFIG_SCHED_SMT
6751 p = sd;
6752 sd = &per_cpu(cpu_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753 *sd = SD_SIBLING_INIT;
Mike Travisd5a74302007-10-16 01:24:05 -07006754 sd->span = per_cpu(cpu_sibling_map, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006755 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006757 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006758 cpu_to_cpu_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759#endif
6760 }
6761
6762#ifdef CONFIG_SCHED_SMT
6763 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07006764 for_each_cpu_mask(i, *cpu_map) {
Mike Travisd5a74302007-10-16 01:24:05 -07006765 cpumask_t this_sibling_map = per_cpu(cpu_sibling_map, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006766 cpus_and(this_sibling_map, this_sibling_map, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767 if (i != first_cpu(this_sibling_map))
6768 continue;
6769
Ingo Molnardd41f592007-07-09 18:51:59 +02006770 init_sched_build_groups(this_sibling_map, cpu_map,
6771 &cpu_to_cpu_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772 }
6773#endif
6774
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006775#ifdef CONFIG_SCHED_MC
6776 /* Set up multi-core groups */
6777 for_each_cpu_mask(i, *cpu_map) {
6778 cpumask_t this_core_map = cpu_coregroup_map(i);
6779 cpus_and(this_core_map, this_core_map, *cpu_map);
6780 if (i != first_cpu(this_core_map))
6781 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02006782 init_sched_build_groups(this_core_map, cpu_map,
6783 &cpu_to_core_group);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006784 }
6785#endif
6786
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 /* Set up physical groups */
6788 for (i = 0; i < MAX_NUMNODES; i++) {
6789 cpumask_t nodemask = node_to_cpumask(i);
6790
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006791 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792 if (cpus_empty(nodemask))
6793 continue;
6794
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006795 init_sched_build_groups(nodemask, cpu_map, &cpu_to_phys_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796 }
6797
6798#ifdef CONFIG_NUMA
6799 /* Set up node groups */
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006800 if (sd_allnodes)
Ingo Molnardd41f592007-07-09 18:51:59 +02006801 init_sched_build_groups(*cpu_map, cpu_map,
6802 &cpu_to_allnodes_group);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006803
6804 for (i = 0; i < MAX_NUMNODES; i++) {
6805 /* Set up node groups */
6806 struct sched_group *sg, *prev;
6807 cpumask_t nodemask = node_to_cpumask(i);
6808 cpumask_t domainspan;
6809 cpumask_t covered = CPU_MASK_NONE;
6810 int j;
6811
6812 cpus_and(nodemask, nodemask, *cpu_map);
John Hawkesd1b55132005-09-06 15:18:14 -07006813 if (cpus_empty(nodemask)) {
6814 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006815 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07006816 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006817
6818 domainspan = sched_domain_node_span(i);
6819 cpus_and(domainspan, domainspan, *cpu_map);
6820
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006821 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006822 if (!sg) {
6823 printk(KERN_WARNING "Can not alloc domain group for "
6824 "node %d\n", i);
6825 goto error;
6826 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006827 sched_group_nodes[i] = sg;
6828 for_each_cpu_mask(j, nodemask) {
6829 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02006830
John Hawkes9c1cfda2005-09-06 15:18:14 -07006831 sd = &per_cpu(node_domains, j);
6832 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006833 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006834 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006835 sg->cpumask = nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006836 sg->next = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006837 cpus_or(covered, covered, nodemask);
6838 prev = sg;
6839
6840 for (j = 0; j < MAX_NUMNODES; j++) {
6841 cpumask_t tmp, notcovered;
6842 int n = (i + j) % MAX_NUMNODES;
6843
6844 cpus_complement(notcovered, covered);
6845 cpus_and(tmp, notcovered, *cpu_map);
6846 cpus_and(tmp, tmp, domainspan);
6847 if (cpus_empty(tmp))
6848 break;
6849
6850 nodemask = node_to_cpumask(n);
6851 cpus_and(tmp, tmp, nodemask);
6852 if (cpus_empty(tmp))
6853 continue;
6854
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006855 sg = kmalloc_node(sizeof(struct sched_group),
6856 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006857 if (!sg) {
6858 printk(KERN_WARNING
6859 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006860 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006861 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006862 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006863 sg->cpumask = tmp;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006864 sg->next = prev->next;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006865 cpus_or(covered, covered, tmp);
6866 prev->next = sg;
6867 prev = sg;
6868 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870#endif
6871
6872 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006873#ifdef CONFIG_SCHED_SMT
6874 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006875 struct sched_domain *sd = &per_cpu(cpu_domains, i);
6876
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006877 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006878 }
6879#endif
6880#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006881 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006882 struct sched_domain *sd = &per_cpu(core_domains, i);
6883
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006884 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006885 }
6886#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006888 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006889 struct sched_domain *sd = &per_cpu(phys_domains, i);
6890
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006891 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892 }
6893
John Hawkes9c1cfda2005-09-06 15:18:14 -07006894#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08006895 for (i = 0; i < MAX_NUMNODES; i++)
6896 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006897
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006898 if (sd_allnodes) {
6899 struct sched_group *sg;
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07006900
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006901 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg);
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07006902 init_numa_sched_groups_power(sg);
6903 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006904#endif
6905
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006907 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908 struct sched_domain *sd;
6909#ifdef CONFIG_SCHED_SMT
6910 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006911#elif defined(CONFIG_SCHED_MC)
6912 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913#else
6914 sd = &per_cpu(phys_domains, i);
6915#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01006916 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006918
6919 return 0;
6920
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006921#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006922error:
6923 free_sched_groups(cpu_map);
6924 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006925#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926}
Paul Jackson029190c2007-10-18 23:40:20 -07006927
6928static cpumask_t *doms_cur; /* current sched domains */
6929static int ndoms_cur; /* number of sched domains in 'doms_cur' */
6930
6931/*
6932 * Special case: If a kmalloc of a doms_cur partition (array of
6933 * cpumask_t) fails, then fallback to a single sched domain,
6934 * as determined by the single cpumask_t fallback_doms.
6935 */
6936static cpumask_t fallback_doms;
6937
Heiko Carstens22e52b02008-03-12 18:31:59 +01006938void __attribute__((weak)) arch_update_cpu_topology(void)
6939{
6940}
6941
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006942/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006943 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07006944 * For now this just excludes isolated cpus, but could be used to
6945 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006946 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006947static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006948{
Milton Miller73785472007-10-24 18:23:48 +02006949 int err;
6950
Heiko Carstens22e52b02008-03-12 18:31:59 +01006951 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07006952 ndoms_cur = 1;
6953 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6954 if (!doms_cur)
6955 doms_cur = &fallback_doms;
6956 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Milton Miller73785472007-10-24 18:23:48 +02006957 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02006958 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02006959
6960 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006961}
6962
6963static void arch_destroy_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964{
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006965 free_sched_groups(cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006966}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006968/*
6969 * Detach sched domains from a group of cpus specified in cpu_map
6970 * These cpus will now be attached to the NULL domain
6971 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08006972static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006973{
6974 int i;
6975
Milton Miller6382bc92007-10-15 17:00:19 +02006976 unregister_sched_domain_sysctl();
6977
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006978 for_each_cpu_mask(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006979 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006980 synchronize_sched();
6981 arch_destroy_sched_domains(cpu_map);
6982}
6983
Paul Jackson029190c2007-10-18 23:40:20 -07006984/*
6985 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006986 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07006987 * doms_new[] to the current sched domain partitioning, doms_cur[].
6988 * It destroys each deleted domain and builds each new domain.
6989 *
6990 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006991 * The masks don't intersect (don't overlap.) We should setup one
6992 * sched domain for each mask. CPUs not in any of the cpumasks will
6993 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07006994 * current 'doms_cur' domains and in the new 'doms_new', we can leave
6995 * it as it is.
6996 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006997 * The passed in 'doms_new' should be kmalloc'd. This routine takes
6998 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07006999 * failed the kmalloc call, then it can pass in doms_new == NULL,
7000 * and partition_sched_domains() will fallback to the single partition
7001 * 'fallback_doms'.
7002 *
7003 * Call with hotplug lock held
7004 */
7005void partition_sched_domains(int ndoms_new, cpumask_t *doms_new)
7006{
7007 int i, j;
7008
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007009 lock_doms_cur();
7010
Milton Miller73785472007-10-24 18:23:48 +02007011 /* always unregister in case we don't destroy any domains */
7012 unregister_sched_domain_sysctl();
7013
Paul Jackson029190c2007-10-18 23:40:20 -07007014 if (doms_new == NULL) {
7015 ndoms_new = 1;
7016 doms_new = &fallback_doms;
7017 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
7018 }
7019
7020 /* Destroy deleted domains */
7021 for (i = 0; i < ndoms_cur; i++) {
7022 for (j = 0; j < ndoms_new; j++) {
7023 if (cpus_equal(doms_cur[i], doms_new[j]))
7024 goto match1;
7025 }
7026 /* no match - a current sched domain not in new doms_new[] */
7027 detach_destroy_domains(doms_cur + i);
7028match1:
7029 ;
7030 }
7031
7032 /* Build new domains */
7033 for (i = 0; i < ndoms_new; i++) {
7034 for (j = 0; j < ndoms_cur; j++) {
7035 if (cpus_equal(doms_new[i], doms_cur[j]))
7036 goto match2;
7037 }
7038 /* no match - add a new doms_new */
7039 build_sched_domains(doms_new + i);
7040match2:
7041 ;
7042 }
7043
7044 /* Remember the new sched domains */
7045 if (doms_cur != &fallback_doms)
7046 kfree(doms_cur);
7047 doms_cur = doms_new;
7048 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007049
7050 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007051
7052 unlock_doms_cur();
Paul Jackson029190c2007-10-18 23:40:20 -07007053}
7054
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007055#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007056int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007057{
7058 int err;
7059
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007060 get_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007061 detach_destroy_domains(&cpu_online_map);
7062 err = arch_init_sched_domains(&cpu_online_map);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007063 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007064
7065 return err;
7066}
7067
7068static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7069{
7070 int ret;
7071
7072 if (buf[0] != '0' && buf[0] != '1')
7073 return -EINVAL;
7074
7075 if (smt)
7076 sched_smt_power_savings = (buf[0] == '1');
7077 else
7078 sched_mc_power_savings = (buf[0] == '1');
7079
7080 ret = arch_reinit_sched_domains();
7081
7082 return ret ? ret : count;
7083}
7084
Adrian Bunk6707de002007-08-12 18:08:19 +02007085#ifdef CONFIG_SCHED_MC
7086static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
7087{
7088 return sprintf(page, "%u\n", sched_mc_power_savings);
7089}
7090static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
7091 const char *buf, size_t count)
7092{
7093 return sched_power_savings_store(buf, count, 0);
7094}
7095static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
7096 sched_mc_power_savings_store);
7097#endif
7098
7099#ifdef CONFIG_SCHED_SMT
7100static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
7101{
7102 return sprintf(page, "%u\n", sched_smt_power_savings);
7103}
7104static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
7105 const char *buf, size_t count)
7106{
7107 return sched_power_savings_store(buf, count, 1);
7108}
7109static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
7110 sched_smt_power_savings_store);
7111#endif
7112
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007113int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7114{
7115 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007116
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007117#ifdef CONFIG_SCHED_SMT
7118 if (smt_capable())
7119 err = sysfs_create_file(&cls->kset.kobj,
7120 &attr_sched_smt_power_savings.attr);
7121#endif
7122#ifdef CONFIG_SCHED_MC
7123 if (!err && mc_capable())
7124 err = sysfs_create_file(&cls->kset.kobj,
7125 &attr_sched_mc_power_savings.attr);
7126#endif
7127 return err;
7128}
7129#endif
7130
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007132 * Force a reinitialization of the sched domains hierarchy. The domains
Linus Torvalds1da177e2005-04-16 15:20:36 -07007133 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07007134 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 * which will prevent rebalancing while the sched domains are recalculated.
7136 */
7137static int update_sched_domains(struct notifier_block *nfb,
7138 unsigned long action, void *hcpu)
7139{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140 switch (action) {
7141 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007142 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007144 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007145 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146 return NOTIFY_OK;
7147
7148 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007149 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007151 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007153 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007155 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156 /*
7157 * Fall through and re-initialise the domains.
7158 */
7159 break;
7160 default:
7161 return NOTIFY_DONE;
7162 }
7163
7164 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007165 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166
7167 return NOTIFY_OK;
7168}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169
7170void __init sched_init_smp(void)
7171{
Nick Piggin5c1e1762006-10-03 01:14:04 -07007172 cpumask_t non_isolated_cpus;
7173
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007174 get_online_cpus();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007175 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08007176 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007177 if (cpus_empty(non_isolated_cpus))
7178 cpu_set(smp_processor_id(), non_isolated_cpus);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007179 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180 /* XXX: Theoretical race here - CPU may be hotplugged now */
7181 hotcpu_notifier(update_sched_domains, 0);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007182
7183 /* Move init over to a non-isolated CPU */
7184 if (set_cpus_allowed(current, non_isolated_cpus) < 0)
7185 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007186 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187}
7188#else
7189void __init sched_init_smp(void)
7190{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007191 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192}
7193#endif /* CONFIG_SMP */
7194
7195int in_sched_functions(unsigned long addr)
7196{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197 return in_lock_functions(addr) ||
7198 (addr >= (unsigned long)__sched_text_start
7199 && addr < (unsigned long)__sched_text_end);
7200}
7201
Alexey Dobriyana9957442007-10-15 17:00:13 +02007202static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007203{
7204 cfs_rq->tasks_timeline = RB_ROOT;
Ingo Molnardd41f592007-07-09 18:51:59 +02007205#ifdef CONFIG_FAIR_GROUP_SCHED
7206 cfs_rq->rq = rq;
7207#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007208 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02007209}
7210
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007211static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7212{
7213 struct rt_prio_array *array;
7214 int i;
7215
7216 array = &rt_rq->active;
7217 for (i = 0; i < MAX_RT_PRIO; i++) {
7218 INIT_LIST_HEAD(array->queue + i);
7219 __clear_bit(i, array->bitmap);
7220 }
7221 /* delimiter for bitsearch: */
7222 __set_bit(MAX_RT_PRIO, array->bitmap);
7223
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007224#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007225 rt_rq->highest_prio = MAX_RT_PRIO;
7226#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007227#ifdef CONFIG_SMP
7228 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007229 rt_rq->overloaded = 0;
7230#endif
7231
7232 rt_rq->rt_time = 0;
7233 rt_rq->rt_throttled = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007234
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007235#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01007236 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007237 rt_rq->rq = rq;
7238#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007239}
7240
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007241#ifdef CONFIG_FAIR_GROUP_SCHED
7242static void init_tg_cfs_entry(struct rq *rq, struct task_group *tg,
7243 struct cfs_rq *cfs_rq, struct sched_entity *se,
7244 int cpu, int add)
7245{
7246 tg->cfs_rq[cpu] = cfs_rq;
7247 init_cfs_rq(cfs_rq, rq);
7248 cfs_rq->tg = tg;
7249 if (add)
7250 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7251
7252 tg->se[cpu] = se;
7253 se->cfs_rq = &rq->cfs;
7254 se->my_q = cfs_rq;
7255 se->load.weight = tg->shares;
7256 se->load.inv_weight = div64_64(1ULL<<32, se->load.weight);
7257 se->parent = NULL;
7258}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007259#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007260
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007261#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007262static void init_tg_rt_entry(struct rq *rq, struct task_group *tg,
7263 struct rt_rq *rt_rq, struct sched_rt_entity *rt_se,
7264 int cpu, int add)
7265{
7266 tg->rt_rq[cpu] = rt_rq;
7267 init_rt_rq(rt_rq, rq);
7268 rt_rq->tg = tg;
7269 rt_rq->rt_se = rt_se;
7270 if (add)
7271 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
7272
7273 tg->rt_se[cpu] = rt_se;
7274 rt_se->rt_rq = &rq->rt;
7275 rt_se->my_q = rt_rq;
7276 rt_se->parent = NULL;
7277 INIT_LIST_HEAD(&rt_se->run_list);
7278}
7279#endif
7280
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281void __init sched_init(void)
7282{
Christoph Lameter476f3532007-05-06 14:48:58 -07007283 int highest_cpu = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007284 int i, j;
7285
Gregory Haskins57d885f2008-01-25 21:08:18 +01007286#ifdef CONFIG_SMP
7287 init_defrootdomain();
7288#endif
7289
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007290#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007291 list_add(&init_task_group.list, &task_groups);
7292#endif
7293
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08007294 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07007295 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296
7297 rq = cpu_rq(i);
7298 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07007299 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07007300 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007301 rq->clock = 1;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02007302 update_last_tick_seen(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02007303 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007304 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007305#ifdef CONFIG_FAIR_GROUP_SCHED
7306 init_task_group.shares = init_task_group_load;
7307 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
7308 init_tg_cfs_entry(rq, &init_task_group,
7309 &per_cpu(init_cfs_rq, i),
7310 &per_cpu(init_sched_entity, i), i, 1);
7311
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007312#endif
7313#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007314 init_task_group.rt_runtime =
7315 sysctl_sched_rt_runtime * NSEC_PER_USEC;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007316 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
7317 init_tg_rt_entry(rq, &init_task_group,
7318 &per_cpu(init_rt_rq, i),
7319 &per_cpu(init_sched_rt_entity, i), i, 1);
7320#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007321 rq->rt_period_expire = 0;
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007322 rq->rt_throttled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323
Ingo Molnardd41f592007-07-09 18:51:59 +02007324 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
7325 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007326#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07007327 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007328 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007329 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007330 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07007332 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333 rq->migration_thread = NULL;
7334 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01007335 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007337 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338 atomic_set(&rq->nr_iowait, 0);
Christoph Lameter476f3532007-05-06 14:48:58 -07007339 highest_cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340 }
7341
Peter Williams2dd73a42006-06-27 02:54:34 -07007342 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007343
Avi Kivitye107be32007-07-26 13:40:43 +02007344#ifdef CONFIG_PREEMPT_NOTIFIERS
7345 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
7346#endif
7347
Christoph Lameterc9819f42006-12-10 02:20:25 -08007348#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07007349 nr_cpu_ids = highest_cpu + 1;
Christoph Lameterc9819f42006-12-10 02:20:25 -08007350 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
7351#endif
7352
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007353#ifdef CONFIG_RT_MUTEXES
7354 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
7355#endif
7356
Linus Torvalds1da177e2005-04-16 15:20:36 -07007357 /*
7358 * The boot idle thread does lazy MMU switching as well:
7359 */
7360 atomic_inc(&init_mm.mm_count);
7361 enter_lazy_tlb(&init_mm, current);
7362
7363 /*
7364 * Make us the idle thread. Technically, schedule() should not be
7365 * called from this thread, however somewhere below it might be,
7366 * but because we are the idle thread, we just pick up running again
7367 * when this runqueue becomes "idle".
7368 */
7369 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02007370 /*
7371 * During early bootup we pretend to be a normal task:
7372 */
7373 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01007374
7375 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376}
7377
7378#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
7379void __might_sleep(char *file, int line)
7380{
Ingo Molnar48f24c42006-07-03 00:25:40 -07007381#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382 static unsigned long prev_jiffy; /* ratelimiting */
7383
7384 if ((in_atomic() || irqs_disabled()) &&
7385 system_state == SYSTEM_RUNNING && !oops_in_progress) {
7386 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
7387 return;
7388 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08007389 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007390 " context at %s:%d\n", file, line);
7391 printk("in_atomic():%d, irqs_disabled():%d\n",
7392 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08007393 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08007394 if (irqs_disabled())
7395 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396 dump_stack();
7397 }
7398#endif
7399}
7400EXPORT_SYMBOL(__might_sleep);
7401#endif
7402
7403#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007404static void normalize_task(struct rq *rq, struct task_struct *p)
7405{
7406 int on_rq;
7407 update_rq_clock(rq);
7408 on_rq = p->se.on_rq;
7409 if (on_rq)
7410 deactivate_task(rq, p, 0);
7411 __setscheduler(rq, p, SCHED_NORMAL, 0);
7412 if (on_rq) {
7413 activate_task(rq, p, 0);
7414 resched_task(rq->curr);
7415 }
7416}
7417
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418void normalize_rt_tasks(void)
7419{
Ingo Molnara0f98a12007-06-17 18:37:45 +02007420 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07007422 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007424 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007425 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02007426 /*
7427 * Only normalize user tasks:
7428 */
7429 if (!p->mm)
7430 continue;
7431
Ingo Molnardd41f592007-07-09 18:51:59 +02007432 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007433#ifdef CONFIG_SCHEDSTATS
7434 p->se.wait_start = 0;
7435 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007436 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007437#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02007438 task_rq(p)->clock = 0;
7439
7440 if (!rt_task(p)) {
7441 /*
7442 * Renice negative nice level userspace
7443 * tasks back to 0:
7444 */
7445 if (TASK_NICE(p) < 0 && p->mm)
7446 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007447 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02007448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007449
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007450 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07007451 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452
Ingo Molnar178be792007-10-15 17:00:18 +02007453 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007454
Ingo Molnarb29739f2006-06-27 02:54:51 -07007455 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007456 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007457 } while_each_thread(g, p);
7458
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007459 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460}
7461
7462#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07007463
7464#ifdef CONFIG_IA64
7465/*
7466 * These functions are only useful for the IA64 MCA handling.
7467 *
7468 * They can only be called when the whole system has been
7469 * stopped - every CPU needs to be quiescent, and no scheduling
7470 * activity can take place. Using them for anything else would
7471 * be a serious bug, and as a result, they aren't even visible
7472 * under any other configuration.
7473 */
7474
7475/**
7476 * curr_task - return the current task for a given cpu.
7477 * @cpu: the processor in question.
7478 *
7479 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7480 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007481struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007482{
7483 return cpu_curr(cpu);
7484}
7485
7486/**
7487 * set_curr_task - set the current task for a given cpu.
7488 * @cpu: the processor in question.
7489 * @p: the task pointer to set.
7490 *
7491 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007492 * are serviced on a separate stack. It allows the architecture to switch the
7493 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07007494 * must be called with all CPU's synchronized, and interrupts disabled, the
7495 * and caller must save the original value of the current task (see
7496 * curr_task() above) and restore that value before reenabling interrupts and
7497 * re-starting the system.
7498 *
7499 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7500 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007501void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007502{
7503 cpu_curr(cpu) = p;
7504}
7505
7506#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007507
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007508#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007509
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007510#ifdef CONFIG_FAIR_GROUP_SCHED
7511static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007512{
7513 int i;
7514
7515 for_each_possible_cpu(i) {
7516 if (tg->cfs_rq)
7517 kfree(tg->cfs_rq[i]);
7518 if (tg->se)
7519 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007520 }
7521
7522 kfree(tg->cfs_rq);
7523 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007524}
7525
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007526static int alloc_fair_sched_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007527{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007528 struct cfs_rq *cfs_rq;
7529 struct sched_entity *se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007530 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007531 int i;
7532
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007533 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * NR_CPUS, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007534 if (!tg->cfs_rq)
7535 goto err;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007536 tg->se = kzalloc(sizeof(se) * NR_CPUS, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007537 if (!tg->se)
7538 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007539
7540 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007541
7542 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007543 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007544
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007545 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
7546 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007547 if (!cfs_rq)
7548 goto err;
7549
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007550 se = kmalloc_node(sizeof(struct sched_entity),
7551 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007552 if (!se)
7553 goto err;
7554
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007555 init_tg_cfs_entry(rq, tg, cfs_rq, se, i, 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007556 }
7557
7558 return 1;
7559
7560 err:
7561 return 0;
7562}
7563
7564static inline void register_fair_sched_group(struct task_group *tg, int cpu)
7565{
7566 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
7567 &cpu_rq(cpu)->leaf_cfs_rq_list);
7568}
7569
7570static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
7571{
7572 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
7573}
7574#else
7575static inline void free_fair_sched_group(struct task_group *tg)
7576{
7577}
7578
7579static inline int alloc_fair_sched_group(struct task_group *tg)
7580{
7581 return 1;
7582}
7583
7584static inline void register_fair_sched_group(struct task_group *tg, int cpu)
7585{
7586}
7587
7588static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
7589{
7590}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007591#endif
7592
7593#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007594static void free_rt_sched_group(struct task_group *tg)
7595{
7596 int i;
7597
7598 for_each_possible_cpu(i) {
7599 if (tg->rt_rq)
7600 kfree(tg->rt_rq[i]);
7601 if (tg->rt_se)
7602 kfree(tg->rt_se[i]);
7603 }
7604
7605 kfree(tg->rt_rq);
7606 kfree(tg->rt_se);
7607}
7608
7609static int alloc_rt_sched_group(struct task_group *tg)
7610{
7611 struct rt_rq *rt_rq;
7612 struct sched_rt_entity *rt_se;
7613 struct rq *rq;
7614 int i;
7615
7616 tg->rt_rq = kzalloc(sizeof(rt_rq) * NR_CPUS, GFP_KERNEL);
7617 if (!tg->rt_rq)
7618 goto err;
7619 tg->rt_se = kzalloc(sizeof(rt_se) * NR_CPUS, GFP_KERNEL);
7620 if (!tg->rt_se)
7621 goto err;
7622
7623 tg->rt_runtime = 0;
7624
7625 for_each_possible_cpu(i) {
7626 rq = cpu_rq(i);
7627
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007628 rt_rq = kmalloc_node(sizeof(struct rt_rq),
7629 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
7630 if (!rt_rq)
7631 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007632
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007633 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
7634 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
7635 if (!rt_se)
7636 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007637
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007638 init_tg_rt_entry(rq, tg, rt_rq, rt_se, i, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007639 }
7640
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007641 return 1;
7642
7643 err:
7644 return 0;
7645}
7646
7647static inline void register_rt_sched_group(struct task_group *tg, int cpu)
7648{
7649 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
7650 &cpu_rq(cpu)->leaf_rt_rq_list);
7651}
7652
7653static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
7654{
7655 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
7656}
7657#else
7658static inline void free_rt_sched_group(struct task_group *tg)
7659{
7660}
7661
7662static inline int alloc_rt_sched_group(struct task_group *tg)
7663{
7664 return 1;
7665}
7666
7667static inline void register_rt_sched_group(struct task_group *tg, int cpu)
7668{
7669}
7670
7671static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
7672{
7673}
7674#endif
7675
7676static void free_sched_group(struct task_group *tg)
7677{
7678 free_fair_sched_group(tg);
7679 free_rt_sched_group(tg);
7680 kfree(tg);
7681}
7682
7683/* allocate runqueue etc for a new task group */
7684struct task_group *sched_create_group(void)
7685{
7686 struct task_group *tg;
7687 unsigned long flags;
7688 int i;
7689
7690 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
7691 if (!tg)
7692 return ERR_PTR(-ENOMEM);
7693
7694 if (!alloc_fair_sched_group(tg))
7695 goto err;
7696
7697 if (!alloc_rt_sched_group(tg))
7698 goto err;
7699
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007700 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007701 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007702 register_fair_sched_group(tg, i);
7703 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007704 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007705 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007706 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007707
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007708 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007709
7710err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007711 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007712 return ERR_PTR(-ENOMEM);
7713}
7714
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007715/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007716static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007717{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007718 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007719 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007720}
7721
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007722/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007723void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007724{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007725 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007726 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007727
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007728 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007729 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007730 unregister_fair_sched_group(tg, i);
7731 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007732 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007733 list_del_rcu(&tg->list);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007734 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007735
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007736 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007737 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007738}
7739
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007740/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02007741 * The caller of this function should have put the task in its new group
7742 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
7743 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007744 */
7745void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007746{
7747 int on_rq, running;
7748 unsigned long flags;
7749 struct rq *rq;
7750
7751 rq = task_rq_lock(tsk, &flags);
7752
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007753 update_rq_clock(rq);
7754
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01007755 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007756 on_rq = tsk->se.on_rq;
7757
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007758 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007759 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007760 if (unlikely(running))
7761 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007762
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007763 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007764
Peter Zijlstra810b3812008-02-29 15:21:01 -05007765#ifdef CONFIG_FAIR_GROUP_SCHED
7766 if (tsk->sched_class->moved_group)
7767 tsk->sched_class->moved_group(tsk);
7768#endif
7769
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007770 if (unlikely(running))
7771 tsk->sched_class->set_curr_task(rq);
7772 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02007773 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007774
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007775 task_rq_unlock(rq, &flags);
7776}
7777
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007778#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007779static void set_se_shares(struct sched_entity *se, unsigned long shares)
7780{
7781 struct cfs_rq *cfs_rq = se->cfs_rq;
7782 struct rq *rq = cfs_rq->rq;
7783 int on_rq;
7784
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007785 spin_lock_irq(&rq->lock);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007786
7787 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007788 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007789 dequeue_entity(cfs_rq, se, 0);
7790
7791 se->load.weight = shares;
7792 se->load.inv_weight = div64_64((1ULL<<32), shares);
7793
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007794 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007795 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007796
7797 spin_unlock_irq(&rq->lock);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007798}
7799
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007800static DEFINE_MUTEX(shares_mutex);
7801
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007802int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007803{
7804 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007805 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01007806
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007807 /*
7808 * A weight of 0 or 1 can cause arithmetics problems.
7809 * (The default weight is 1024 - so there's no practical
7810 * limitation from this.)
7811 */
7812 if (shares < 2)
7813 shares = 2;
7814
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007815 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007816 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007817 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007818
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007819 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007820 for_each_possible_cpu(i)
7821 unregister_fair_sched_group(tg, i);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007822 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007823
7824 /* wait for any ongoing reference to this group to finish */
7825 synchronize_sched();
7826
7827 /*
7828 * Now we are free to modify the group's share on each cpu
7829 * w/o tripping rebalance_share or load_balance_fair.
7830 */
7831 tg->shares = shares;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007832 for_each_possible_cpu(i)
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007833 set_se_shares(tg->se[i], shares);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007834
7835 /*
7836 * Enable load balance activity on this group, by inserting it back on
7837 * each cpu's rq->leaf_cfs_rq_list.
7838 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007839 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007840 for_each_possible_cpu(i)
7841 register_fair_sched_group(tg, i);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007842 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007843done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007844 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007845 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007846}
7847
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007848unsigned long sched_group_shares(struct task_group *tg)
7849{
7850 return tg->shares;
7851}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007852#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007853
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007854#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007855/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007856 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007857 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007858static DEFINE_MUTEX(rt_constraints_mutex);
7859
7860static unsigned long to_ratio(u64 period, u64 runtime)
7861{
7862 if (runtime == RUNTIME_INF)
7863 return 1ULL << 16;
7864
Peter Zijlstra2692a242008-02-27 12:00:46 +01007865 return div64_64(runtime << 16, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007866}
7867
7868static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007869{
7870 struct task_group *tgi;
7871 unsigned long total = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007872 unsigned long global_ratio =
7873 to_ratio(sysctl_sched_rt_period,
7874 sysctl_sched_rt_runtime < 0 ?
7875 RUNTIME_INF : sysctl_sched_rt_runtime);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007876
7877 rcu_read_lock();
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007878 list_for_each_entry_rcu(tgi, &task_groups, list) {
7879 if (tgi == tg)
7880 continue;
7881
7882 total += to_ratio(period, tgi->rt_runtime);
7883 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007884 rcu_read_unlock();
7885
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007886 return total + to_ratio(period, runtime) < global_ratio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007887}
7888
Dhaval Giani521f1a242008-02-28 15:21:56 +05307889/* Must be called with tasklist_lock held */
7890static inline int tg_has_rt_tasks(struct task_group *tg)
7891{
7892 struct task_struct *g, *p;
7893 do_each_thread(g, p) {
7894 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
7895 return 1;
7896 } while_each_thread(g, p);
7897 return 0;
7898}
7899
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007900int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007901{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007902 u64 rt_runtime, rt_period;
7903 int err = 0;
7904
Peter Zijlstra2692a242008-02-27 12:00:46 +01007905 rt_period = (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007906 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
7907 if (rt_runtime_us == -1)
Peter Zijlstra2692a242008-02-27 12:00:46 +01007908 rt_runtime = RUNTIME_INF;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007909
7910 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05307911 read_lock(&tasklist_lock);
7912 if (rt_runtime_us == 0 && tg_has_rt_tasks(tg)) {
7913 err = -EBUSY;
7914 goto unlock;
7915 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007916 if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
7917 err = -EINVAL;
7918 goto unlock;
7919 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007920 tg->rt_runtime = rt_runtime;
7921 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05307922 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007923 mutex_unlock(&rt_constraints_mutex);
7924
7925 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007926}
7927
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007928long sched_group_rt_runtime(struct task_group *tg)
7929{
7930 u64 rt_runtime_us;
7931
7932 if (tg->rt_runtime == RUNTIME_INF)
7933 return -1;
7934
7935 rt_runtime_us = tg->rt_runtime;
7936 do_div(rt_runtime_us, NSEC_PER_USEC);
7937 return rt_runtime_us;
7938}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007939#endif
7940#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007941
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007942#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007943
7944/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02007945static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007946{
Paul Menage2b01dfe2007-10-24 18:23:50 +02007947 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
7948 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007949}
7950
7951static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02007952cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007953{
7954 struct task_group *tg;
7955
Paul Menage2b01dfe2007-10-24 18:23:50 +02007956 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007957 /* This is early initialization for the top cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02007958 init_task_group.css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007959 return &init_task_group.css;
7960 }
7961
7962 /* we support only 1-level deep hierarchical scheduler atm */
Paul Menage2b01dfe2007-10-24 18:23:50 +02007963 if (cgrp->parent->parent)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007964 return ERR_PTR(-EINVAL);
7965
7966 tg = sched_create_group();
7967 if (IS_ERR(tg))
7968 return ERR_PTR(-ENOMEM);
7969
7970 /* Bind the cgroup to task_group object we just created */
Paul Menage2b01dfe2007-10-24 18:23:50 +02007971 tg->css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007972
7973 return &tg->css;
7974}
7975
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007976static void
7977cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007978{
Paul Menage2b01dfe2007-10-24 18:23:50 +02007979 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007980
7981 sched_destroy_group(tg);
7982}
7983
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007984static int
7985cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
7986 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007987{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01007988#ifdef CONFIG_RT_GROUP_SCHED
7989 /* Don't accept realtime tasks when there is no way for them to run */
7990 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_runtime == 0)
7991 return -EINVAL;
7992#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007993 /* We don't support RT-tasks being in separate groups */
7994 if (tsk->sched_class != &fair_sched_class)
7995 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01007996#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007997
7998 return 0;
7999}
8000
8001static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02008002cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008003 struct cgroup *old_cont, struct task_struct *tsk)
8004{
8005 sched_move_task(tsk);
8006}
8007
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008008#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menage2b01dfe2007-10-24 18:23:50 +02008009static int cpu_shares_write_uint(struct cgroup *cgrp, struct cftype *cftype,
8010 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008011{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008012 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008013}
8014
Paul Menage2b01dfe2007-10-24 18:23:50 +02008015static u64 cpu_shares_read_uint(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008016{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008017 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008018
8019 return (u64) tg->shares;
8020}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008021#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008022
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008023#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008024static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
8025 struct file *file,
8026 const char __user *userbuf,
8027 size_t nbytes, loff_t *unused_ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008028{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008029 char buffer[64];
8030 int retval = 0;
8031 s64 val;
8032 char *end;
8033
8034 if (!nbytes)
8035 return -EINVAL;
8036 if (nbytes >= sizeof(buffer))
8037 return -E2BIG;
8038 if (copy_from_user(buffer, userbuf, nbytes))
8039 return -EFAULT;
8040
8041 buffer[nbytes] = 0; /* nul-terminate */
8042
8043 /* strip newline if necessary */
8044 if (nbytes && (buffer[nbytes-1] == '\n'))
8045 buffer[nbytes-1] = 0;
8046 val = simple_strtoll(buffer, &end, 0);
8047 if (*end)
8048 return -EINVAL;
8049
8050 /* Pass to subsystem */
8051 retval = sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
8052 if (!retval)
8053 retval = nbytes;
8054 return retval;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008055}
8056
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008057static ssize_t cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft,
8058 struct file *file,
8059 char __user *buf, size_t nbytes,
8060 loff_t *ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008061{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008062 char tmp[64];
8063 long val = sched_group_rt_runtime(cgroup_tg(cgrp));
8064 int len = sprintf(tmp, "%ld\n", val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008065
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008066 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008067}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008068#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008069
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008070static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008071#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008072 {
8073 .name = "shares",
8074 .read_uint = cpu_shares_read_uint,
8075 .write_uint = cpu_shares_write_uint,
8076 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008077#endif
8078#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008079 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008080 .name = "rt_runtime_us",
8081 .read = cpu_rt_runtime_read,
8082 .write = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008083 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008084#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008085};
8086
8087static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
8088{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008089 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008090}
8091
8092struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01008093 .name = "cpu",
8094 .create = cpu_cgroup_create,
8095 .destroy = cpu_cgroup_destroy,
8096 .can_attach = cpu_cgroup_can_attach,
8097 .attach = cpu_cgroup_attach,
8098 .populate = cpu_cgroup_populate,
8099 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008100 .early_init = 1,
8101};
8102
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008103#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008104
8105#ifdef CONFIG_CGROUP_CPUACCT
8106
8107/*
8108 * CPU accounting code for task groups.
8109 *
8110 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
8111 * (balbir@in.ibm.com).
8112 */
8113
8114/* track cpu usage of a group of tasks */
8115struct cpuacct {
8116 struct cgroup_subsys_state css;
8117 /* cpuusage holds pointer to a u64-type object on every cpu */
8118 u64 *cpuusage;
8119};
8120
8121struct cgroup_subsys cpuacct_subsys;
8122
8123/* return cpu accounting group corresponding to this container */
8124static inline struct cpuacct *cgroup_ca(struct cgroup *cont)
8125{
8126 return container_of(cgroup_subsys_state(cont, cpuacct_subsys_id),
8127 struct cpuacct, css);
8128}
8129
8130/* return cpu accounting group to which this task belongs */
8131static inline struct cpuacct *task_ca(struct task_struct *tsk)
8132{
8133 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
8134 struct cpuacct, css);
8135}
8136
8137/* create a new cpu accounting group */
8138static struct cgroup_subsys_state *cpuacct_create(
8139 struct cgroup_subsys *ss, struct cgroup *cont)
8140{
8141 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
8142
8143 if (!ca)
8144 return ERR_PTR(-ENOMEM);
8145
8146 ca->cpuusage = alloc_percpu(u64);
8147 if (!ca->cpuusage) {
8148 kfree(ca);
8149 return ERR_PTR(-ENOMEM);
8150 }
8151
8152 return &ca->css;
8153}
8154
8155/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008156static void
8157cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008158{
8159 struct cpuacct *ca = cgroup_ca(cont);
8160
8161 free_percpu(ca->cpuusage);
8162 kfree(ca);
8163}
8164
8165/* return total cpu usage (in nanoseconds) of a group */
8166static u64 cpuusage_read(struct cgroup *cont, struct cftype *cft)
8167{
8168 struct cpuacct *ca = cgroup_ca(cont);
8169 u64 totalcpuusage = 0;
8170 int i;
8171
8172 for_each_possible_cpu(i) {
8173 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
8174
8175 /*
8176 * Take rq->lock to make 64-bit addition safe on 32-bit
8177 * platforms.
8178 */
8179 spin_lock_irq(&cpu_rq(i)->lock);
8180 totalcpuusage += *cpuusage;
8181 spin_unlock_irq(&cpu_rq(i)->lock);
8182 }
8183
8184 return totalcpuusage;
8185}
8186
8187static struct cftype files[] = {
8188 {
8189 .name = "usage",
8190 .read_uint = cpuusage_read,
8191 },
8192};
8193
8194static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cont)
8195{
8196 return cgroup_add_files(cont, ss, files, ARRAY_SIZE(files));
8197}
8198
8199/*
8200 * charge this task's execution time to its accounting group.
8201 *
8202 * called with rq->lock held.
8203 */
8204static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
8205{
8206 struct cpuacct *ca;
8207
8208 if (!cpuacct_subsys.active)
8209 return;
8210
8211 ca = task_ca(tsk);
8212 if (ca) {
8213 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
8214
8215 *cpuusage += cputime;
8216 }
8217}
8218
8219struct cgroup_subsys cpuacct_subsys = {
8220 .name = "cpuacct",
8221 .create = cpuacct_create,
8222 .destroy = cpuacct_destroy,
8223 .populate = cpuacct_populate,
8224 .subsys_id = cpuacct_subsys_id,
8225};
8226#endif /* CONFIG_CGROUP_CPUACCT */