blob: e813e845d9cfeb46cc1fd1a1eb4f69e48b61a8d4 [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 Molnar62160e32007-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 Haskins73fe6aa2008-01-25 21:08:07 +0100333 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc2008-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,
Ingo Molnard25ce4c2008-03-17 09:36:53 +0100628 SCHED_FEAT_AFFINE_WAKEUPS = 8,
629 SCHED_FEAT_CACHE_HOT_BUDDY = 16,
Ingo Molnar02e2b832008-03-19 01:37:10 +0100630 SCHED_FEAT_SYNC_WAKEUPS = 32,
631 SCHED_FEAT_HRTICK = 64,
632 SCHED_FEAT_DOUBLE_TICK = 128,
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200633};
634
635const_debug unsigned int sysctl_sched_features =
Ingo Molnar8401f772007-10-18 21:32:55 +0200636 SCHED_FEAT_NEW_FAIR_SLEEPERS * 1 |
Ingo Molnar96126332007-11-15 20:57:40 +0100637 SCHED_FEAT_WAKEUP_PREEMPT * 1 |
Ingo Molnar8401f772007-10-18 21:32:55 +0200638 SCHED_FEAT_START_DEBIT * 1 |
Ingo Molnard25ce4c2008-03-17 09:36:53 +0100639 SCHED_FEAT_AFFINE_WAKEUPS * 1 |
640 SCHED_FEAT_CACHE_HOT_BUDDY * 1 |
Ingo Molnar02e2b832008-03-19 01:37:10 +0100641 SCHED_FEAT_SYNC_WAKEUPS * 1 |
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100642 SCHED_FEAT_HRTICK * 1 |
Ingo Molnar02e2b832008-03-19 01:37:10 +0100643 SCHED_FEAT_DOUBLE_TICK * 0;
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200644
645#define sched_feat(x) (sysctl_sched_features & SCHED_FEAT_##x)
646
647/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100648 * Number of tasks to iterate in a single balance run.
649 * Limited because this is done with IRQs disabled.
650 */
651const_debug unsigned int sysctl_sched_nr_migrate = 32;
652
653/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100654 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100655 * default: 1s
656 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100657unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100658
Ingo Molnar6892b752008-02-13 14:02:36 +0100659static __read_mostly int scheduler_running;
660
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100661/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100662 * part of the period that we allow rt tasks to run in us.
663 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100664 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100665int sysctl_sched_rt_runtime = 950000;
666
667/*
668 * single value that denotes runtime == period, ie unlimited time.
669 */
670#define RUNTIME_INF ((u64)~0ULL)
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100671
Ingo Molnar27ec4402008-02-28 21:00:21 +0100672static const unsigned long long time_sync_thresh = 100000;
673
674static DEFINE_PER_CPU(unsigned long long, time_offset);
675static DEFINE_PER_CPU(unsigned long long, prev_cpu_time);
676
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100677/*
Ingo Molnar27ec4402008-02-28 21:00:21 +0100678 * Global lock which we take every now and then to synchronize
679 * the CPUs time. This method is not warp-safe, but it's good
680 * enough to synchronize slowly diverging time sources and thus
681 * it's good enough for tracing:
Ingo Molnare436d802007-07-19 21:28:35 +0200682 */
Ingo Molnar27ec4402008-02-28 21:00:21 +0100683static DEFINE_SPINLOCK(time_sync_lock);
684static unsigned long long prev_global_time;
685
686static unsigned long long __sync_cpu_clock(cycles_t time, int cpu)
687{
688 unsigned long flags;
689
690 spin_lock_irqsave(&time_sync_lock, flags);
691
692 if (time < prev_global_time) {
693 per_cpu(time_offset, cpu) += prev_global_time - time;
694 time = prev_global_time;
695 } else {
696 prev_global_time = time;
697 }
698
699 spin_unlock_irqrestore(&time_sync_lock, flags);
700
701 return time;
702}
703
704static unsigned long long __cpu_clock(int cpu)
Ingo Molnare436d802007-07-19 21:28:35 +0200705{
Ingo Molnare436d802007-07-19 21:28:35 +0200706 unsigned long long now;
707 unsigned long flags;
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200708 struct rq *rq;
Ingo Molnare436d802007-07-19 21:28:35 +0200709
Ingo Molnar8ced5f62007-12-07 19:02:47 +0100710 /*
711 * Only call sched_clock() if the scheduler has already been
712 * initialized (some code might call cpu_clock() very early):
713 */
Ingo Molnar6892b752008-02-13 14:02:36 +0100714 if (unlikely(!scheduler_running))
715 return 0;
716
717 local_irq_save(flags);
718 rq = cpu_rq(cpu);
719 update_rq_clock(rq);
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200720 now = rq->clock;
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200721 local_irq_restore(flags);
Ingo Molnare436d802007-07-19 21:28:35 +0200722
723 return now;
724}
Ingo Molnar27ec4402008-02-28 21:00:21 +0100725
726/*
727 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
728 * clock constructed from sched_clock():
729 */
730unsigned long long cpu_clock(int cpu)
731{
732 unsigned long long prev_cpu_time, time, delta_time;
733
734 prev_cpu_time = per_cpu(prev_cpu_time, cpu);
735 time = __cpu_clock(cpu) + per_cpu(time_offset, cpu);
736 delta_time = time-prev_cpu_time;
737
738 if (unlikely(delta_time > time_sync_thresh))
739 time = __sync_cpu_clock(time, cpu);
740
741 return time;
742}
Paul E. McKenneya58f6f22007-10-15 17:00:14 +0200743EXPORT_SYMBOL_GPL(cpu_clock);
Ingo Molnare436d802007-07-19 21:28:35 +0200744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700746# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700748#ifndef finish_arch_switch
749# define finish_arch_switch(prev) do { } while (0)
750#endif
751
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100752static inline int task_current(struct rq *rq, struct task_struct *p)
753{
754 return rq->curr == p;
755}
756
Nick Piggin4866cde2005-06-25 14:57:23 -0700757#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700758static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700759{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100760 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700761}
762
Ingo Molnar70b97a72006-07-03 00:25:42 -0700763static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700764{
765}
766
Ingo Molnar70b97a72006-07-03 00:25:42 -0700767static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700768{
Ingo Molnarda04c032005-09-13 11:17:59 +0200769#ifdef CONFIG_DEBUG_SPINLOCK
770 /* this is a valid case when another task releases the spinlock */
771 rq->lock.owner = current;
772#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700773 /*
774 * If we are tracking spinlock dependencies then we have to
775 * fix up the runqueue lock - which gets 'carried over' from
776 * prev into current:
777 */
778 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
779
Nick Piggin4866cde2005-06-25 14:57:23 -0700780 spin_unlock_irq(&rq->lock);
781}
782
783#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700784static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700785{
786#ifdef CONFIG_SMP
787 return p->oncpu;
788#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100789 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700790#endif
791}
792
Ingo Molnar70b97a72006-07-03 00:25:42 -0700793static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700794{
795#ifdef CONFIG_SMP
796 /*
797 * We can optimise this out completely for !SMP, because the
798 * SMP rebalancing from interrupt is the only thing that cares
799 * here.
800 */
801 next->oncpu = 1;
802#endif
803#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
804 spin_unlock_irq(&rq->lock);
805#else
806 spin_unlock(&rq->lock);
807#endif
808}
809
Ingo Molnar70b97a72006-07-03 00:25:42 -0700810static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700811{
812#ifdef CONFIG_SMP
813 /*
814 * After ->oncpu is cleared, the task can be moved to a different CPU.
815 * We must ensure this doesn't happen until the switch is completely
816 * finished.
817 */
818 smp_wmb();
819 prev->oncpu = 0;
820#endif
821#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
822 local_irq_enable();
823#endif
824}
825#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700828 * __task_rq_lock - lock the runqueue a given task resides on.
829 * Must be called interrupts disabled.
830 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700831static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700832 __acquires(rq->lock)
833{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200834 for (;;) {
835 struct rq *rq = task_rq(p);
836 spin_lock(&rq->lock);
837 if (likely(rq == task_rq(p)))
838 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700839 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700840 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700841}
842
843/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100845 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 * explicitly disabling preemption.
847 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700848static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 __acquires(rq->lock)
850{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700851 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Andi Kleen3a5c3592007-10-15 17:00:14 +0200853 for (;;) {
854 local_irq_save(*flags);
855 rq = task_rq(p);
856 spin_lock(&rq->lock);
857 if (likely(rq == task_rq(p)))
858 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
Alexey Dobriyana9957442007-10-15 17:00:13 +0200863static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700864 __releases(rq->lock)
865{
866 spin_unlock(&rq->lock);
867}
868
Ingo Molnar70b97a72006-07-03 00:25:42 -0700869static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 __releases(rq->lock)
871{
872 spin_unlock_irqrestore(&rq->lock, *flags);
873}
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800876 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200878static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 __acquires(rq->lock)
880{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700881 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 local_irq_disable();
884 rq = this_rq();
885 spin_lock(&rq->lock);
886
887 return rq;
888}
889
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200890/*
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200891 * We are going deep-idle (irqs are disabled):
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200892 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200893void sched_clock_idle_sleep_event(void)
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200894{
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200895 struct rq *rq = cpu_rq(smp_processor_id());
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200896
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200897 spin_lock(&rq->lock);
898 __update_rq_clock(rq);
899 spin_unlock(&rq->lock);
900 rq->clock_deep_idle_events++;
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200901}
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200902EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
903
904/*
905 * We just idled delta nanoseconds (called with irqs disabled):
906 */
907void sched_clock_idle_wakeup_event(u64 delta_ns)
908{
909 struct rq *rq = cpu_rq(smp_processor_id());
910 u64 now = sched_clock();
911
912 rq->idle_clock += delta_ns;
913 /*
914 * Override the previous timestamp and ignore all
915 * sched_clock() deltas that occured while we idled,
916 * and use the PM-provided delta_ns to advance the
917 * rq clock:
918 */
919 spin_lock(&rq->lock);
920 rq->prev_clock_raw = now;
921 rq->clock += delta_ns;
922 spin_unlock(&rq->lock);
Guillaume Chazarain782daee2008-01-25 21:08:33 +0100923 touch_softlockup_watchdog();
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200924}
925EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200926
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100927static void __resched_task(struct task_struct *p, int tif_bit);
928
929static inline void resched_task(struct task_struct *p)
930{
931 __resched_task(p, TIF_NEED_RESCHED);
932}
933
934#ifdef CONFIG_SCHED_HRTICK
935/*
936 * Use HR-timers to deliver accurate preemption points.
937 *
938 * Its all a bit involved since we cannot program an hrt while holding the
939 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
940 * reschedule event.
941 *
942 * When we get rescheduled we reprogram the hrtick_timer outside of the
943 * rq->lock.
944 */
945static inline void resched_hrt(struct task_struct *p)
946{
947 __resched_task(p, TIF_HRTICK_RESCHED);
948}
949
950static inline void resched_rq(struct rq *rq)
951{
952 unsigned long flags;
953
954 spin_lock_irqsave(&rq->lock, flags);
955 resched_task(rq->curr);
956 spin_unlock_irqrestore(&rq->lock, flags);
957}
958
959enum {
960 HRTICK_SET, /* re-programm hrtick_timer */
961 HRTICK_RESET, /* not a new slice */
962};
963
964/*
965 * Use hrtick when:
966 * - enabled by features
967 * - hrtimer is actually high res
968 */
969static inline int hrtick_enabled(struct rq *rq)
970{
971 if (!sched_feat(HRTICK))
972 return 0;
973 return hrtimer_is_hres_active(&rq->hrtick_timer);
974}
975
976/*
977 * Called to set the hrtick timer state.
978 *
979 * called with rq->lock held and irqs disabled
980 */
981static void hrtick_start(struct rq *rq, u64 delay, int reset)
982{
983 assert_spin_locked(&rq->lock);
984
985 /*
986 * preempt at: now + delay
987 */
988 rq->hrtick_expire =
989 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
990 /*
991 * indicate we need to program the timer
992 */
993 __set_bit(HRTICK_SET, &rq->hrtick_flags);
994 if (reset)
995 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
996
997 /*
998 * New slices are called from the schedule path and don't need a
999 * forced reschedule.
1000 */
1001 if (reset)
1002 resched_hrt(rq->curr);
1003}
1004
1005static void hrtick_clear(struct rq *rq)
1006{
1007 if (hrtimer_active(&rq->hrtick_timer))
1008 hrtimer_cancel(&rq->hrtick_timer);
1009}
1010
1011/*
1012 * Update the timer from the possible pending state.
1013 */
1014static void hrtick_set(struct rq *rq)
1015{
1016 ktime_t time;
1017 int set, reset;
1018 unsigned long flags;
1019
1020 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1021
1022 spin_lock_irqsave(&rq->lock, flags);
1023 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
1024 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
1025 time = rq->hrtick_expire;
1026 clear_thread_flag(TIF_HRTICK_RESCHED);
1027 spin_unlock_irqrestore(&rq->lock, flags);
1028
1029 if (set) {
1030 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
1031 if (reset && !hrtimer_active(&rq->hrtick_timer))
1032 resched_rq(rq);
1033 } else
1034 hrtick_clear(rq);
1035}
1036
1037/*
1038 * High-resolution timer tick.
1039 * Runs from hardirq context with interrupts disabled.
1040 */
1041static enum hrtimer_restart hrtick(struct hrtimer *timer)
1042{
1043 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1044
1045 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1046
1047 spin_lock(&rq->lock);
1048 __update_rq_clock(rq);
1049 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1050 spin_unlock(&rq->lock);
1051
1052 return HRTIMER_NORESTART;
1053}
1054
1055static inline void init_rq_hrtick(struct rq *rq)
1056{
1057 rq->hrtick_flags = 0;
1058 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1059 rq->hrtick_timer.function = hrtick;
1060 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1061}
1062
1063void hrtick_resched(void)
1064{
1065 struct rq *rq;
1066 unsigned long flags;
1067
1068 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1069 return;
1070
1071 local_irq_save(flags);
1072 rq = cpu_rq(smp_processor_id());
1073 hrtick_set(rq);
1074 local_irq_restore(flags);
1075}
1076#else
1077static inline void hrtick_clear(struct rq *rq)
1078{
1079}
1080
1081static inline void hrtick_set(struct rq *rq)
1082{
1083}
1084
1085static inline void init_rq_hrtick(struct rq *rq)
1086{
1087}
1088
1089void hrtick_resched(void)
1090{
1091}
1092#endif
1093
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001094/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001095 * resched_task - mark a task 'to be rescheduled now'.
1096 *
1097 * On UP this means the setting of the need_resched flag, on SMP it
1098 * might also involve a cross-CPU call to trigger the scheduler on
1099 * the target CPU.
1100 */
1101#ifdef CONFIG_SMP
1102
1103#ifndef tsk_is_polling
1104#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1105#endif
1106
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001107static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001108{
1109 int cpu;
1110
1111 assert_spin_locked(&task_rq(p)->lock);
1112
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001113 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001114 return;
1115
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001116 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001117
1118 cpu = task_cpu(p);
1119 if (cpu == smp_processor_id())
1120 return;
1121
1122 /* NEED_RESCHED must be visible before we test polling */
1123 smp_mb();
1124 if (!tsk_is_polling(p))
1125 smp_send_reschedule(cpu);
1126}
1127
1128static void resched_cpu(int cpu)
1129{
1130 struct rq *rq = cpu_rq(cpu);
1131 unsigned long flags;
1132
1133 if (!spin_trylock_irqsave(&rq->lock, flags))
1134 return;
1135 resched_task(cpu_curr(cpu));
1136 spin_unlock_irqrestore(&rq->lock, flags);
1137}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001138
1139#ifdef CONFIG_NO_HZ
1140/*
1141 * When add_timer_on() enqueues a timer into the timer wheel of an
1142 * idle CPU then this timer might expire before the next timer event
1143 * which is scheduled to wake up that CPU. In case of a completely
1144 * idle system the next event might even be infinite time into the
1145 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1146 * leaves the inner idle loop so the newly added timer is taken into
1147 * account when the CPU goes back to idle and evaluates the timer
1148 * wheel for the next timer event.
1149 */
1150void wake_up_idle_cpu(int cpu)
1151{
1152 struct rq *rq = cpu_rq(cpu);
1153
1154 if (cpu == smp_processor_id())
1155 return;
1156
1157 /*
1158 * This is safe, as this function is called with the timer
1159 * wheel base lock of (cpu) held. When the CPU is on the way
1160 * to idle and has not yet set rq->curr to idle then it will
1161 * be serialized on the timer wheel base lock and take the new
1162 * timer into account automatically.
1163 */
1164 if (rq->curr != rq->idle)
1165 return;
1166
1167 /*
1168 * We can set TIF_RESCHED on the idle task of the other CPU
1169 * lockless. The worst case is that the other CPU runs the
1170 * idle task through an additional NOOP schedule()
1171 */
1172 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1173
1174 /* NEED_RESCHED must be visible before we test polling */
1175 smp_mb();
1176 if (!tsk_is_polling(rq->idle))
1177 smp_send_reschedule(cpu);
1178}
1179#endif
1180
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001181#else
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001182static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001183{
1184 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001185 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001186}
1187#endif
1188
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001189#if BITS_PER_LONG == 32
1190# define WMULT_CONST (~0UL)
1191#else
1192# define WMULT_CONST (1UL << 32)
1193#endif
1194
1195#define WMULT_SHIFT 32
1196
Ingo Molnar194081e2007-08-09 11:16:51 +02001197/*
1198 * Shift right and round:
1199 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001200#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001201
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001202static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001203calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1204 struct load_weight *lw)
1205{
1206 u64 tmp;
1207
1208 if (unlikely(!lw->inv_weight))
Ingo Molnar27d11722008-03-14 22:20:01 +01001209 lw->inv_weight = (WMULT_CONST-lw->weight/2) / (lw->weight+1);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001210
1211 tmp = (u64)delta_exec * weight;
1212 /*
1213 * Check whether we'd overflow the 64-bit multiplication:
1214 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001215 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001216 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001217 WMULT_SHIFT/2);
1218 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001219 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001220
Ingo Molnarecf691d2007-08-02 17:41:40 +02001221 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001222}
1223
1224static inline unsigned long
1225calc_delta_fair(unsigned long delta_exec, struct load_weight *lw)
1226{
1227 return calc_delta_mine(delta_exec, NICE_0_LOAD, lw);
1228}
1229
Ingo Molnar10919852007-10-15 17:00:04 +02001230static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001231{
1232 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001233 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001234}
1235
Ingo Molnar10919852007-10-15 17:00:04 +02001236static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001237{
1238 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001239 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001240}
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001243 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1244 * of tasks with abnormal "nice" values across CPUs the contribution that
1245 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001246 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001247 * scaled version of the new time slice allocation that they receive on time
1248 * slice expiry etc.
1249 */
1250
Ingo Molnardd41f592007-07-09 18:51:59 +02001251#define WEIGHT_IDLEPRIO 2
1252#define WMULT_IDLEPRIO (1 << 31)
1253
1254/*
1255 * Nice levels are multiplicative, with a gentle 10% change for every
1256 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1257 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1258 * that remained on nice 0.
1259 *
1260 * The "10% effect" is relative and cumulative: from _any_ nice level,
1261 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +02001262 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1263 * If a task goes up by ~10% and another task goes down by ~10% then
1264 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001265 */
1266static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001267 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1268 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1269 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1270 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1271 /* 0 */ 1024, 820, 655, 526, 423,
1272 /* 5 */ 335, 272, 215, 172, 137,
1273 /* 10 */ 110, 87, 70, 56, 45,
1274 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001275};
1276
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001277/*
1278 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1279 *
1280 * In cases where the weight does not change often, we can use the
1281 * precalculated inverse to speed up arithmetics by turning divisions
1282 * into multiplications:
1283 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001284static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001285 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1286 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1287 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1288 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1289 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1290 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1291 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1292 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001293};
Peter Williams2dd73a42006-06-27 02:54:34 -07001294
Ingo Molnardd41f592007-07-09 18:51:59 +02001295static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1296
1297/*
1298 * runqueue iterator, to support SMP load-balancing between different
1299 * scheduling classes, without having to expose their internal data
1300 * structures to the load-balancing proper:
1301 */
1302struct rq_iterator {
1303 void *arg;
1304 struct task_struct *(*start)(void *);
1305 struct task_struct *(*next)(void *);
1306};
1307
Peter Williamse1d14842007-10-24 18:23:51 +02001308#ifdef CONFIG_SMP
1309static unsigned long
1310balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1311 unsigned long max_load_move, struct sched_domain *sd,
1312 enum cpu_idle_type idle, int *all_pinned,
1313 int *this_best_prio, struct rq_iterator *iterator);
1314
1315static int
1316iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1317 struct sched_domain *sd, enum cpu_idle_type idle,
1318 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001319#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001320
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001321#ifdef CONFIG_CGROUP_CPUACCT
1322static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1323#else
1324static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1325#endif
1326
Gregory Haskinse7693a32008-01-25 21:08:09 +01001327#ifdef CONFIG_SMP
1328static unsigned long source_load(int cpu, int type);
1329static unsigned long target_load(int cpu, int type);
1330static unsigned long cpu_avg_load_per_task(int cpu);
1331static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
1332#endif /* CONFIG_SMP */
1333
Ingo Molnardd41f592007-07-09 18:51:59 +02001334#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001335#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001336#include "sched_fair.c"
1337#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001338#ifdef CONFIG_SCHED_DEBUG
1339# include "sched_debug.c"
1340#endif
1341
1342#define sched_class_highest (&rt_sched_class)
1343
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001344static inline void inc_load(struct rq *rq, const struct task_struct *p)
Ingo Molnar9c217242007-08-02 17:41:40 +02001345{
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001346 update_load_add(&rq->load, p->se.load.weight);
Ingo Molnar9c217242007-08-02 17:41:40 +02001347}
1348
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001349static inline void dec_load(struct rq *rq, const struct task_struct *p)
1350{
1351 update_load_sub(&rq->load, p->se.load.weight);
1352}
1353
1354static void inc_nr_running(struct task_struct *p, struct rq *rq)
1355{
1356 rq->nr_running++;
1357 inc_load(rq, p);
1358}
1359
1360static void dec_nr_running(struct task_struct *p, struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001361{
1362 rq->nr_running--;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001363 dec_load(rq, p);
Ingo Molnar9c217242007-08-02 17:41:40 +02001364}
1365
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001366static void set_load_weight(struct task_struct *p)
1367{
1368 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001369 p->se.load.weight = prio_to_weight[0] * 2;
1370 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1371 return;
1372 }
1373
1374 /*
1375 * SCHED_IDLE tasks get minimal weight:
1376 */
1377 if (p->policy == SCHED_IDLE) {
1378 p->se.load.weight = WEIGHT_IDLEPRIO;
1379 p->se.load.inv_weight = WMULT_IDLEPRIO;
1380 return;
1381 }
1382
1383 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1384 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001385}
1386
Ingo Molnar8159f872007-08-09 11:16:49 +02001387static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001388{
1389 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001390 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001391 p->se.on_rq = 1;
1392}
1393
Ingo Molnar69be72c2007-08-09 11:16:49 +02001394static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001395{
Ingo Molnarf02231e2007-08-09 11:16:48 +02001396 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001397 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001398}
1399
1400/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001401 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001402 */
Ingo Molnar14531182007-07-09 18:51:59 +02001403static inline int __normal_prio(struct task_struct *p)
1404{
Ingo Molnardd41f592007-07-09 18:51:59 +02001405 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001406}
1407
1408/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001409 * Calculate the expected normal priority: i.e. priority
1410 * without taking RT-inheritance into account. Might be
1411 * boosted by interactivity modifiers. Changes upon fork,
1412 * setprio syscalls, and whenever the interactivity
1413 * estimator recalculates.
1414 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001415static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001416{
1417 int prio;
1418
Ingo Molnare05606d2007-07-09 18:51:59 +02001419 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001420 prio = MAX_RT_PRIO-1 - p->rt_priority;
1421 else
1422 prio = __normal_prio(p);
1423 return prio;
1424}
1425
1426/*
1427 * Calculate the current priority, i.e. the priority
1428 * taken into account by the scheduler. This value might
1429 * be boosted by RT tasks, or might be boosted by
1430 * interactivity modifiers. Will be RT if the task got
1431 * RT-boosted. If not then it returns p->normal_prio.
1432 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001433static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001434{
1435 p->normal_prio = normal_prio(p);
1436 /*
1437 * If we are RT tasks or we were boosted to RT priority,
1438 * keep the priority unchanged. Otherwise, update priority
1439 * to the normal priority:
1440 */
1441 if (!rt_prio(p->prio))
1442 return p->normal_prio;
1443 return p->prio;
1444}
1445
1446/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001447 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001449static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001451 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001452 rq->nr_uninterruptible--;
1453
Ingo Molnar8159f872007-08-09 11:16:49 +02001454 enqueue_task(rq, p, wakeup);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001455 inc_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
1458/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 * deactivate_task - remove a task from the runqueue.
1460 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02001461static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001463 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001464 rq->nr_uninterruptible++;
1465
Ingo Molnar69be72c2007-08-09 11:16:49 +02001466 dequeue_task(rq, p, sleep);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001467 dec_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470/**
1471 * task_curr - is this task currently executing on a CPU?
1472 * @p: the task in question.
1473 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001474inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
1476 return cpu_curr(task_cpu(p)) == p;
1477}
1478
Peter Williams2dd73a42006-06-27 02:54:34 -07001479/* Used instead of source_load when we know the type == 0 */
1480unsigned long weighted_cpuload(const int cpu)
1481{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001482 return cpu_rq(cpu)->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02001483}
1484
1485static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1486{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001487 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001488#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01001489 /*
1490 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1491 * successfuly executed on another CPU. We must ensure that updates of
1492 * per-task data have been completed by this moment.
1493 */
1494 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02001495 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02001496#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07001497}
1498
Steven Rostedtcb469842008-01-25 21:08:22 +01001499static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1500 const struct sched_class *prev_class,
1501 int oldprio, int running)
1502{
1503 if (prev_class != p->sched_class) {
1504 if (prev_class->switched_from)
1505 prev_class->switched_from(rq, p, running);
1506 p->sched_class->switched_to(rq, p, running);
1507 } else
1508 p->sched_class->prio_changed(rq, p, oldprio, running);
1509}
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02001512
Ingo Molnarcc367732007-10-15 17:00:18 +02001513/*
1514 * Is this task likely cache-hot:
1515 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001516static int
Ingo Molnarcc367732007-10-15 17:00:18 +02001517task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
1518{
1519 s64 delta;
1520
Ingo Molnarf540a602008-03-15 17:10:34 +01001521 /*
1522 * Buddy candidates are cache hot:
1523 */
Ingo Molnard25ce4c2008-03-17 09:36:53 +01001524 if (sched_feat(CACHE_HOT_BUDDY) && (&p->se == cfs_rq_of(&p->se)->next))
Ingo Molnarf540a602008-03-15 17:10:34 +01001525 return 1;
1526
Ingo Molnarcc367732007-10-15 17:00:18 +02001527 if (p->sched_class != &fair_sched_class)
1528 return 0;
1529
Ingo Molnar6bc16652007-10-15 17:00:18 +02001530 if (sysctl_sched_migration_cost == -1)
1531 return 1;
1532 if (sysctl_sched_migration_cost == 0)
1533 return 0;
1534
Ingo Molnarcc367732007-10-15 17:00:18 +02001535 delta = now - p->se.exec_start;
1536
1537 return delta < (s64)sysctl_sched_migration_cost;
1538}
1539
1540
Ingo Molnardd41f592007-07-09 18:51:59 +02001541void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001542{
Ingo Molnardd41f592007-07-09 18:51:59 +02001543 int old_cpu = task_cpu(p);
1544 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001545 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
1546 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02001547 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001548
1549 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001550
1551#ifdef CONFIG_SCHEDSTATS
1552 if (p->se.wait_start)
1553 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001554 if (p->se.sleep_start)
1555 p->se.sleep_start -= clock_offset;
1556 if (p->se.block_start)
1557 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02001558 if (old_cpu != new_cpu) {
1559 schedstat_inc(p, se.nr_migrations);
1560 if (task_hot(p, old_rq->clock, NULL))
1561 schedstat_inc(p, se.nr_forced2_migrations);
1562 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001563#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001564 p->se.vruntime -= old_cfsrq->min_vruntime -
1565 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02001566
1567 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001568}
1569
Ingo Molnar70b97a72006-07-03 00:25:42 -07001570struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Ingo Molnar36c8b582006-07-03 00:25:41 -07001573 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 int dest_cpu;
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001577};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579/*
1580 * The task's runqueue lock must be held.
1581 * Returns true if you have to wait for migration thread.
1582 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001583static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07001584migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001586 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 /*
1589 * If the task is not on a runqueue (and not running), then
1590 * it is sufficient to simply update the task's cpu field.
1591 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001592 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 set_task_cpu(p, dest_cpu);
1594 return 0;
1595 }
1596
1597 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 req->task = p;
1599 req->dest_cpu = dest_cpu;
1600 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07001601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return 1;
1603}
1604
1605/*
1606 * wait_task_inactive - wait for a thread to unschedule.
1607 *
1608 * The caller must ensure that the task *will* unschedule sometime soon,
1609 * else this function might spin for a *long* time. This function can't
1610 * be called with interrupts off, or it may introduce deadlock with
1611 * smp_call_function() if an IPI is sent by the same process we are
1612 * waiting to become inactive.
1613 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001614void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
1616 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001617 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001618 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Andi Kleen3a5c3592007-10-15 17:00:14 +02001620 for (;;) {
1621 /*
1622 * We do the initial early heuristics without holding
1623 * any task-queue locks at all. We'll only try to get
1624 * the runqueue lock when things look like they will
1625 * work out!
1626 */
1627 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001628
Andi Kleen3a5c3592007-10-15 17:00:14 +02001629 /*
1630 * If the task is actively running on another CPU
1631 * still, just relax and busy-wait without holding
1632 * any locks.
1633 *
1634 * NOTE! Since we don't hold any locks, it's not
1635 * even sure that "rq" stays as the right runqueue!
1636 * But we don't care, since "task_running()" will
1637 * return false if the runqueue has changed and p
1638 * is actually now running somewhere else!
1639 */
1640 while (task_running(rq, p))
1641 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001642
Andi Kleen3a5c3592007-10-15 17:00:14 +02001643 /*
1644 * Ok, time to look more closely! We need the rq
1645 * lock now, to be *sure*. If we're wrong, we'll
1646 * just go back and repeat.
1647 */
1648 rq = task_rq_lock(p, &flags);
1649 running = task_running(rq, p);
1650 on_rq = p->se.on_rq;
1651 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001652
Andi Kleen3a5c3592007-10-15 17:00:14 +02001653 /*
1654 * Was it really running after all now that we
1655 * checked with the proper locks actually held?
1656 *
1657 * Oops. Go back and try again..
1658 */
1659 if (unlikely(running)) {
1660 cpu_relax();
1661 continue;
1662 }
1663
1664 /*
1665 * It's not enough that it's not actively running,
1666 * it must be off the runqueue _entirely_, and not
1667 * preempted!
1668 *
1669 * So if it wa still runnable (but just not actively
1670 * running right now), it's preempted, and we should
1671 * yield - it could be a while.
1672 */
1673 if (unlikely(on_rq)) {
1674 schedule_timeout_uninterruptible(1);
1675 continue;
1676 }
1677
1678 /*
1679 * Ahh, all good. It wasn't running, and it wasn't
1680 * runnable, which means that it will never become
1681 * running in the future either. We're all done!
1682 */
1683 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
1687/***
1688 * kick_process - kick a running thread to enter/exit the kernel
1689 * @p: the to-be-kicked thread
1690 *
1691 * Cause a process which is running on another CPU to enter
1692 * kernel-mode, without any delay. (to get signals handled.)
1693 *
1694 * NOTE: this function doesnt have to take the runqueue lock,
1695 * because all it wants to ensure is that the remote task enters
1696 * the kernel. If the IPI races and the task has been migrated
1697 * to another CPU then no harm is done and the purpose has been
1698 * achieved as well.
1699 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001700void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
1702 int cpu;
1703
1704 preempt_disable();
1705 cpu = task_cpu(p);
1706 if ((cpu != smp_processor_id()) && task_curr(p))
1707 smp_send_reschedule(cpu);
1708 preempt_enable();
1709}
1710
1711/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001712 * Return a low guess at the load of a migration-source cpu weighted
1713 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 *
1715 * We want to under-estimate the load of migration sources, to
1716 * balance conservatively.
1717 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001718static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08001719{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001720 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001721 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001722
Peter Williams2dd73a42006-06-27 02:54:34 -07001723 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001724 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001725
Ingo Molnardd41f592007-07-09 18:51:59 +02001726 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
1729/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001730 * Return a high guess at the load of a migration-target cpu weighted
1731 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001733static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08001734{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001735 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001736 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001737
Peter Williams2dd73a42006-06-27 02:54:34 -07001738 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001739 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001740
Ingo Molnardd41f592007-07-09 18:51:59 +02001741 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07001742}
1743
1744/*
1745 * Return the average load per task on the cpu's run queue
1746 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001747static unsigned long cpu_avg_load_per_task(int cpu)
Peter Williams2dd73a42006-06-27 02:54:34 -07001748{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001749 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001750 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07001751 unsigned long n = rq->nr_running;
1752
Ingo Molnardd41f592007-07-09 18:51:59 +02001753 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
Nick Piggin147cbb42005-06-25 14:57:19 -07001756/*
1757 * find_idlest_group finds and returns the least busy CPU group within the
1758 * domain.
1759 */
1760static struct sched_group *
1761find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
1762{
1763 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
1764 unsigned long min_load = ULONG_MAX, this_load = 0;
1765 int load_idx = sd->forkexec_idx;
1766 int imbalance = 100 + (sd->imbalance_pct-100)/2;
1767
1768 do {
1769 unsigned long load, avg_load;
1770 int local_group;
1771 int i;
1772
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001773 /* Skip over this group if it has no CPUs allowed */
1774 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02001775 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001776
Nick Piggin147cbb42005-06-25 14:57:19 -07001777 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07001778
1779 /* Tally up the load of all CPUs in the group */
1780 avg_load = 0;
1781
1782 for_each_cpu_mask(i, group->cpumask) {
1783 /* Bias balancing toward cpus of our domain */
1784 if (local_group)
1785 load = source_load(i, load_idx);
1786 else
1787 load = target_load(i, load_idx);
1788
1789 avg_load += load;
1790 }
1791
1792 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07001793 avg_load = sg_div_cpu_power(group,
1794 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07001795
1796 if (local_group) {
1797 this_load = avg_load;
1798 this = group;
1799 } else if (avg_load < min_load) {
1800 min_load = avg_load;
1801 idlest = group;
1802 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02001803 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07001804
1805 if (!idlest || 100*this_load < imbalance*min_load)
1806 return NULL;
1807 return idlest;
1808}
1809
1810/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07001811 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07001812 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07001813static int
1814find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
Nick Piggin147cbb42005-06-25 14:57:19 -07001815{
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001816 cpumask_t tmp;
Nick Piggin147cbb42005-06-25 14:57:19 -07001817 unsigned long load, min_load = ULONG_MAX;
1818 int idlest = -1;
1819 int i;
1820
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001821 /* Traverse only the allowed CPUs */
1822 cpus_and(tmp, group->cpumask, p->cpus_allowed);
1823
1824 for_each_cpu_mask(i, tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07001825 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07001826
1827 if (load < min_load || (load == min_load && i == this_cpu)) {
1828 min_load = load;
1829 idlest = i;
1830 }
1831 }
1832
1833 return idlest;
1834}
1835
Nick Piggin476d1392005-06-25 14:57:29 -07001836/*
1837 * sched_balance_self: balance the current task (running on cpu) in domains
1838 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
1839 * SD_BALANCE_EXEC.
1840 *
1841 * Balance, ie. select the least loaded group.
1842 *
1843 * Returns the target CPU number, or the same CPU if no balancing is needed.
1844 *
1845 * preempt must be disabled.
1846 */
1847static int sched_balance_self(int cpu, int flag)
1848{
1849 struct task_struct *t = current;
1850 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07001851
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001852 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02001853 /*
1854 * If power savings logic is enabled for a domain, stop there.
1855 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07001856 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
1857 break;
Nick Piggin476d1392005-06-25 14:57:29 -07001858 if (tmp->flags & flag)
1859 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001860 }
Nick Piggin476d1392005-06-25 14:57:29 -07001861
1862 while (sd) {
1863 cpumask_t span;
1864 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001865 int new_cpu, weight;
1866
1867 if (!(sd->flags & flag)) {
1868 sd = sd->child;
1869 continue;
1870 }
Nick Piggin476d1392005-06-25 14:57:29 -07001871
1872 span = sd->span;
1873 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001874 if (!group) {
1875 sd = sd->child;
1876 continue;
1877 }
Nick Piggin476d1392005-06-25 14:57:29 -07001878
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001879 new_cpu = find_idlest_cpu(group, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001880 if (new_cpu == -1 || new_cpu == cpu) {
1881 /* Now try balancing at a lower domain level of cpu */
1882 sd = sd->child;
1883 continue;
1884 }
Nick Piggin476d1392005-06-25 14:57:29 -07001885
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001886 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07001887 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07001888 sd = NULL;
1889 weight = cpus_weight(span);
1890 for_each_domain(cpu, tmp) {
1891 if (weight <= cpus_weight(tmp->span))
1892 break;
1893 if (tmp->flags & flag)
1894 sd = tmp;
1895 }
1896 /* while loop will break here if sd == NULL */
1897 }
1898
1899 return cpu;
1900}
1901
1902#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904/***
1905 * try_to_wake_up - wake up a thread
1906 * @p: the to-be-woken-up thread
1907 * @state: the mask of task states that can be woken
1908 * @sync: do a synchronous wakeup?
1909 *
1910 * Put it on the run-queue if it's not already there. The "current"
1911 * thread is always on the run-queue (except when the actual
1912 * re-schedule is in progress), and as such you're allowed to do
1913 * the simpler "current->state = TASK_RUNNING" to mark yourself
1914 * runnable without the overhead of this.
1915 *
1916 * returns failure only if the task is already active.
1917 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001918static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
Ingo Molnarcc367732007-10-15 17:00:18 +02001920 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 unsigned long flags;
1922 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001923 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Ingo Molnarb85d0662008-03-16 20:03:22 +01001925 if (!sched_feat(SYNC_WAKEUPS))
1926 sync = 0;
1927
Linus Torvalds04e2f172008-02-23 18:05:03 -08001928 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 rq = task_rq_lock(p, &flags);
1930 old_state = p->state;
1931 if (!(old_state & state))
1932 goto out;
1933
Ingo Molnardd41f592007-07-09 18:51:59 +02001934 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 goto out_running;
1936
1937 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02001938 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 this_cpu = smp_processor_id();
1940
1941#ifdef CONFIG_SMP
1942 if (unlikely(task_running(rq, p)))
1943 goto out_activate;
1944
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01001945 cpu = p->sched_class->select_task_rq(p, sync);
1946 if (cpu != orig_cpu) {
1947 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 task_rq_unlock(rq, &flags);
1949 /* might preempt at this point */
1950 rq = task_rq_lock(p, &flags);
1951 old_state = p->state;
1952 if (!(old_state & state))
1953 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02001954 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 goto out_running;
1956
1957 this_cpu = smp_processor_id();
1958 cpu = task_cpu(p);
1959 }
1960
Gregory Haskinse7693a32008-01-25 21:08:09 +01001961#ifdef CONFIG_SCHEDSTATS
1962 schedstat_inc(rq, ttwu_count);
1963 if (cpu == this_cpu)
1964 schedstat_inc(rq, ttwu_local);
1965 else {
1966 struct sched_domain *sd;
1967 for_each_domain(this_cpu, sd) {
1968 if (cpu_isset(cpu, sd->span)) {
1969 schedstat_inc(sd, ttwu_wake_remote);
1970 break;
1971 }
1972 }
1973 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01001974#endif
1975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976out_activate:
1977#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02001978 schedstat_inc(p, se.nr_wakeups);
1979 if (sync)
1980 schedstat_inc(p, se.nr_wakeups_sync);
1981 if (orig_cpu != cpu)
1982 schedstat_inc(p, se.nr_wakeups_migrate);
1983 if (cpu == this_cpu)
1984 schedstat_inc(p, se.nr_wakeups_local);
1985 else
1986 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02001987 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02001988 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 success = 1;
1990
1991out_running:
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01001992 check_preempt_curr(rq, p);
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01001995#ifdef CONFIG_SMP
1996 if (p->sched_class->task_wake_up)
1997 p->sched_class->task_wake_up(rq, p);
1998#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999out:
2000 task_rq_unlock(rq, &flags);
2001
2002 return success;
2003}
2004
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002005int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05002007 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009EXPORT_SYMBOL(wake_up_process);
2010
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002011int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
2013 return try_to_wake_up(p, state, 0);
2014}
2015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016/*
2017 * Perform scheduler related setup for a newly forked process p.
2018 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002019 *
2020 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002022static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
Ingo Molnardd41f592007-07-09 18:51:59 +02002024 p->se.exec_start = 0;
2025 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002026 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01002027 p->se.last_wakeup = 0;
2028 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002029
2030#ifdef CONFIG_SCHEDSTATS
2031 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002032 p->se.sum_sleep_runtime = 0;
2033 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002034 p->se.block_start = 0;
2035 p->se.sleep_max = 0;
2036 p->se.block_max = 0;
2037 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02002038 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002039 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002040#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002041
Peter Zijlstrafa717062008-01-25 21:08:27 +01002042 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002043 p->se.on_rq = 0;
Nick Piggin476d1392005-06-25 14:57:29 -07002044
Avi Kivitye107be32007-07-26 13:40:43 +02002045#ifdef CONFIG_PREEMPT_NOTIFIERS
2046 INIT_HLIST_HEAD(&p->preempt_notifiers);
2047#endif
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 /*
2050 * We mark the process as running here, but have not actually
2051 * inserted it onto the runqueue yet. This guarantees that
2052 * nobody will actually run it, and a signal or other external
2053 * event cannot wake it up and insert it on the runqueue either.
2054 */
2055 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002056}
2057
2058/*
2059 * fork()/clone()-time setup:
2060 */
2061void sched_fork(struct task_struct *p, int clone_flags)
2062{
2063 int cpu = get_cpu();
2064
2065 __sched_fork(p);
2066
2067#ifdef CONFIG_SMP
2068 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2069#endif
Ingo Molnar02e4bac2007-10-15 17:00:11 +02002070 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002071
2072 /*
2073 * Make sure we do not leak PI boosting priority to the child:
2074 */
2075 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002076 if (!rt_prio(p->prio))
2077 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002078
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002079#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002080 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002081 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002083#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002084 p->oncpu = 0;
2085#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002087 /* Want to start with kernel preemption disabled. */
Al Viroa1261f52005-11-13 16:06:55 -08002088 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002090 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091}
2092
2093/*
2094 * wake_up_new_task - wake up a newly created task for the first time.
2095 *
2096 * This function will do some initial scheduler statistics housekeeping
2097 * that must be done for every newly created context, then puts the task
2098 * on the runqueue and wakes it.
2099 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002100void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101{
2102 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002103 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002107 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
2109 p->prio = effective_prio(p);
2110
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002111 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002112 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002115 * Let the scheduling class do new task startup
2116 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002118 p->sched_class->task_new(rq, p);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002119 inc_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002121 check_preempt_curr(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002122#ifdef CONFIG_SMP
2123 if (p->sched_class->task_wake_up)
2124 p->sched_class->task_wake_up(rq, p);
2125#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002126 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
2128
Avi Kivitye107be32007-07-26 13:40:43 +02002129#ifdef CONFIG_PREEMPT_NOTIFIERS
2130
2131/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002132 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2133 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002134 */
2135void preempt_notifier_register(struct preempt_notifier *notifier)
2136{
2137 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2138}
2139EXPORT_SYMBOL_GPL(preempt_notifier_register);
2140
2141/**
2142 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002143 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002144 *
2145 * This is safe to call from within a preemption notifier.
2146 */
2147void preempt_notifier_unregister(struct preempt_notifier *notifier)
2148{
2149 hlist_del(&notifier->link);
2150}
2151EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2152
2153static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2154{
2155 struct preempt_notifier *notifier;
2156 struct hlist_node *node;
2157
2158 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2159 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2160}
2161
2162static void
2163fire_sched_out_preempt_notifiers(struct task_struct *curr,
2164 struct task_struct *next)
2165{
2166 struct preempt_notifier *notifier;
2167 struct hlist_node *node;
2168
2169 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2170 notifier->ops->sched_out(notifier, next);
2171}
2172
2173#else
2174
2175static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2176{
2177}
2178
2179static void
2180fire_sched_out_preempt_notifiers(struct task_struct *curr,
2181 struct task_struct *next)
2182{
2183}
2184
2185#endif
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002188 * prepare_task_switch - prepare to switch tasks
2189 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002190 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002191 * @next: the task we are going to switch to.
2192 *
2193 * This is called with the rq lock held and interrupts off. It must
2194 * be paired with a subsequent finish_task_switch after the context
2195 * switch.
2196 *
2197 * prepare_task_switch sets up locking and calls architecture specific
2198 * hooks.
2199 */
Avi Kivitye107be32007-07-26 13:40:43 +02002200static inline void
2201prepare_task_switch(struct rq *rq, struct task_struct *prev,
2202 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002203{
Avi Kivitye107be32007-07-26 13:40:43 +02002204 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002205 prepare_lock_switch(rq, next);
2206 prepare_arch_switch(next);
2207}
2208
2209/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002211 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 * @prev: the thread we just switched away from.
2213 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002214 * finish_task_switch must be called after the context switch, paired
2215 * with a prepare_task_switch call before the context switch.
2216 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2217 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 *
2219 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002220 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 * with the lock held can cause deadlocks; see schedule() for
2222 * details.)
2223 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002224static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 __releases(rq->lock)
2226{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002228 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
2230 rq->prev_mm = NULL;
2231
2232 /*
2233 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002234 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002235 * schedule one last time. The schedule call will never return, and
2236 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002237 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 * still held, otherwise prev could be scheduled on another cpu, die
2239 * there before we look at prev->state, and then the reference would
2240 * be dropped twice.
2241 * Manfred Spraul <manfred@colorfullife.com>
2242 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002243 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002244 finish_arch_switch(prev);
2245 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002246#ifdef CONFIG_SMP
2247 if (current->sched_class->post_schedule)
2248 current->sched_class->post_schedule(rq);
2249#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002250
Avi Kivitye107be32007-07-26 13:40:43 +02002251 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 if (mm)
2253 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002254 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002255 /*
2256 * Remove function-return probe instances associated with this
2257 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002258 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002259 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262}
2263
2264/**
2265 * schedule_tail - first thing a freshly forked thread must call.
2266 * @prev: the thread we just switched away from.
2267 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002268asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 __releases(rq->lock)
2270{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002271 struct rq *rq = this_rq();
2272
Nick Piggin4866cde2005-06-25 14:57:23 -07002273 finish_task_switch(rq, prev);
2274#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2275 /* In this case, finish_task_switch does not reenable preemption */
2276 preempt_enable();
2277#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002279 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280}
2281
2282/*
2283 * context_switch - switch to the new MM and the new
2284 * thread's register state.
2285 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002286static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002287context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002288 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
Ingo Molnardd41f592007-07-09 18:51:59 +02002290 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Avi Kivitye107be32007-07-26 13:40:43 +02002292 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002293 mm = next->mm;
2294 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002295 /*
2296 * For paravirt, this is coupled with an exit in switch_to to
2297 * combine the page table reload and the switch backend into
2298 * one hypercall.
2299 */
2300 arch_enter_lazy_cpu_mode();
2301
Ingo Molnardd41f592007-07-09 18:51:59 +02002302 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 next->active_mm = oldmm;
2304 atomic_inc(&oldmm->mm_count);
2305 enter_lazy_tlb(oldmm, next);
2306 } else
2307 switch_mm(oldmm, mm, next);
2308
Ingo Molnardd41f592007-07-09 18:51:59 +02002309 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 rq->prev_mm = oldmm;
2312 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002313 /*
2314 * Since the runqueue lock will be released by the next
2315 * task (which is an invalid locking op but in the case
2316 * of the scheduler it's an obvious special-case), so we
2317 * do an early lockdep release here:
2318 */
2319#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002320 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002321#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 /* Here we just switch the register state and the stack. */
2324 switch_to(prev, next, prev);
2325
Ingo Molnardd41f592007-07-09 18:51:59 +02002326 barrier();
2327 /*
2328 * this_rq must be evaluated again because prev may have moved
2329 * CPUs since it called schedule(), thus the 'rq' on its stack
2330 * frame will be invalid.
2331 */
2332 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333}
2334
2335/*
2336 * nr_running, nr_uninterruptible and nr_context_switches:
2337 *
2338 * externally visible scheduler statistics: current number of runnable
2339 * threads, current number of uninterruptible-sleeping threads, total
2340 * number of context switches performed since bootup.
2341 */
2342unsigned long nr_running(void)
2343{
2344 unsigned long i, sum = 0;
2345
2346 for_each_online_cpu(i)
2347 sum += cpu_rq(i)->nr_running;
2348
2349 return sum;
2350}
2351
2352unsigned long nr_uninterruptible(void)
2353{
2354 unsigned long i, sum = 0;
2355
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002356 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 sum += cpu_rq(i)->nr_uninterruptible;
2358
2359 /*
2360 * Since we read the counters lockless, it might be slightly
2361 * inaccurate. Do not allow it to go below zero though:
2362 */
2363 if (unlikely((long)sum < 0))
2364 sum = 0;
2365
2366 return sum;
2367}
2368
2369unsigned long long nr_context_switches(void)
2370{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002371 int i;
2372 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002374 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 sum += cpu_rq(i)->nr_switches;
2376
2377 return sum;
2378}
2379
2380unsigned long nr_iowait(void)
2381{
2382 unsigned long i, sum = 0;
2383
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002384 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2386
2387 return sum;
2388}
2389
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002390unsigned long nr_active(void)
2391{
2392 unsigned long i, running = 0, uninterruptible = 0;
2393
2394 for_each_online_cpu(i) {
2395 running += cpu_rq(i)->nr_running;
2396 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2397 }
2398
2399 if (unlikely((long)uninterruptible < 0))
2400 uninterruptible = 0;
2401
2402 return running + uninterruptible;
2403}
2404
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002406 * Update rq->cpu_load[] statistics. This function is usually called every
2407 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002408 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002409static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002410{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002411 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002412 int i, scale;
2413
2414 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002415
2416 /* Update our load: */
2417 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2418 unsigned long old_load, new_load;
2419
2420 /* scale is effectively 1 << i now, and >> i divides by scale */
2421
2422 old_load = this_rq->cpu_load[i];
2423 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002424 /*
2425 * Round up the averaging division if load is increasing. This
2426 * prevents us from getting stuck on 9 if the load is 10, for
2427 * example.
2428 */
2429 if (new_load > old_load)
2430 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002431 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2432 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002433}
2434
Ingo Molnardd41f592007-07-09 18:51:59 +02002435#ifdef CONFIG_SMP
2436
Ingo Molnar48f24c42006-07-03 00:25:40 -07002437/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 * double_rq_lock - safely lock two runqueues
2439 *
2440 * Note this does not disable interrupts like task_rq_lock,
2441 * you need to do so manually before calling.
2442 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002443static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 __acquires(rq1->lock)
2445 __acquires(rq2->lock)
2446{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002447 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if (rq1 == rq2) {
2449 spin_lock(&rq1->lock);
2450 __acquire(rq2->lock); /* Fake it out ;) */
2451 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002452 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 spin_lock(&rq1->lock);
2454 spin_lock(&rq2->lock);
2455 } else {
2456 spin_lock(&rq2->lock);
2457 spin_lock(&rq1->lock);
2458 }
2459 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002460 update_rq_clock(rq1);
2461 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462}
2463
2464/*
2465 * double_rq_unlock - safely unlock two runqueues
2466 *
2467 * Note this does not restore interrupts like task_rq_unlock,
2468 * you need to do so manually after calling.
2469 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002470static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 __releases(rq1->lock)
2472 __releases(rq2->lock)
2473{
2474 spin_unlock(&rq1->lock);
2475 if (rq1 != rq2)
2476 spin_unlock(&rq2->lock);
2477 else
2478 __release(rq2->lock);
2479}
2480
2481/*
2482 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2483 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01002484static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 __releases(this_rq->lock)
2486 __acquires(busiest->lock)
2487 __acquires(this_rq->lock)
2488{
Steven Rostedte8fa1362008-01-25 21:08:05 +01002489 int ret = 0;
2490
Kirill Korotaev054b9102006-12-10 02:20:11 -08002491 if (unlikely(!irqs_disabled())) {
2492 /* printk() doesn't work good under rq->lock */
2493 spin_unlock(&this_rq->lock);
2494 BUG_ON(1);
2495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002497 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 spin_unlock(&this_rq->lock);
2499 spin_lock(&busiest->lock);
2500 spin_lock(&this_rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01002501 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 } else
2503 spin_lock(&busiest->lock);
2504 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01002505 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
2508/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 * If dest_cpu is allowed for this process, migrate the task to it.
2510 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002511 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 * the cpu_allowed mask is restored.
2513 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002514static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002516 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002518 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
2520 rq = task_rq_lock(p, &flags);
2521 if (!cpu_isset(dest_cpu, p->cpus_allowed)
2522 || unlikely(cpu_is_offline(dest_cpu)))
2523 goto out;
2524
2525 /* force the process onto the specified CPU */
2526 if (migrate_task(p, dest_cpu, &req)) {
2527 /* Need to wait for migration thread (might exit: take ref). */
2528 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07002529
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 get_task_struct(mt);
2531 task_rq_unlock(rq, &flags);
2532 wake_up_process(mt);
2533 put_task_struct(mt);
2534 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07002535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 return;
2537 }
2538out:
2539 task_rq_unlock(rq, &flags);
2540}
2541
2542/*
Nick Piggin476d1392005-06-25 14:57:29 -07002543 * sched_exec - execve() is a valuable balancing opportunity, because at
2544 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 */
2546void sched_exec(void)
2547{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002549 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002551 if (new_cpu != this_cpu)
2552 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553}
2554
2555/*
2556 * pull_task - move a task from a remote runqueue to the local runqueue.
2557 * Both runqueues must be locked.
2558 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002559static void pull_task(struct rq *src_rq, struct task_struct *p,
2560 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02002562 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002564 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 /*
2566 * Note that idle threads have a prio of MAX_PRIO, for this test
2567 * to be always true for them.
2568 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002569 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570}
2571
2572/*
2573 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
2574 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002575static
Ingo Molnar70b97a72006-07-03 00:25:42 -07002576int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002577 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002578 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
2580 /*
2581 * We do not migrate tasks that are:
2582 * 1) running (obviously), or
2583 * 2) cannot be migrated to this CPU due to cpus_allowed, or
2584 * 3) are cache-hot on their current CPU.
2585 */
Ingo Molnarcc367732007-10-15 17:00:18 +02002586 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
2587 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002589 }
Nick Piggin81026792005-06-25 14:57:07 -07002590 *all_pinned = 0;
2591
Ingo Molnarcc367732007-10-15 17:00:18 +02002592 if (task_running(rq, p)) {
2593 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07002594 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
Ingo Molnarda84d962007-10-15 17:00:18 +02002597 /*
2598 * Aggressive migration if:
2599 * 1) task is cache cold, or
2600 * 2) too many balance attempts have failed.
2601 */
2602
Ingo Molnar6bc16652007-10-15 17:00:18 +02002603 if (!task_hot(p, rq->clock, sd) ||
2604 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002605#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02002606 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002607 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02002608 schedstat_inc(p, se.nr_forced_migrations);
2609 }
Ingo Molnarda84d962007-10-15 17:00:18 +02002610#endif
2611 return 1;
2612 }
2613
Ingo Molnarcc367732007-10-15 17:00:18 +02002614 if (task_hot(p, rq->clock, sd)) {
2615 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02002616 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 return 1;
2619}
2620
Peter Williamse1d14842007-10-24 18:23:51 +02002621static unsigned long
2622balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
2623 unsigned long max_load_move, struct sched_domain *sd,
2624 enum cpu_idle_type idle, int *all_pinned,
2625 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02002626{
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002627 int loops = 0, pulled = 0, pinned = 0, skip_for_load;
Ingo Molnardd41f592007-07-09 18:51:59 +02002628 struct task_struct *p;
2629 long rem_load_move = max_load_move;
2630
Peter Williamse1d14842007-10-24 18:23:51 +02002631 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002632 goto out;
2633
2634 pinned = 1;
2635
2636 /*
2637 * Start the load-balancing iterator:
2638 */
2639 p = iterator->start(iterator->arg);
2640next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002641 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02002642 goto out;
2643 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002644 * To help distribute high priority tasks across CPUs we don't
Ingo Molnardd41f592007-07-09 18:51:59 +02002645 * skip a task if it will be the highest priority task (i.e. smallest
2646 * prio value) on its new queue regardless of its load weight
2647 */
2648 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
2649 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002650 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02002651 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002652 p = iterator->next(iterator->arg);
2653 goto next;
2654 }
2655
2656 pull_task(busiest, p, this_rq, this_cpu);
2657 pulled++;
2658 rem_load_move -= p->se.load.weight;
2659
2660 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002661 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002662 */
Peter Williamse1d14842007-10-24 18:23:51 +02002663 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002664 if (p->prio < *this_best_prio)
2665 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02002666 p = iterator->next(iterator->arg);
2667 goto next;
2668 }
2669out:
2670 /*
Peter Williamse1d14842007-10-24 18:23:51 +02002671 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02002672 * so we can safely collect pull_task() stats here rather than
2673 * inside pull_task().
2674 */
2675 schedstat_add(sd, lb_gained[idle], pulled);
2676
2677 if (all_pinned)
2678 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02002679
2680 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02002681}
Ingo Molnar48f24c42006-07-03 00:25:40 -07002682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683/*
Peter Williams43010652007-08-09 11:16:46 +02002684 * move_tasks tries to move up to max_load_move weighted load from busiest to
2685 * this_rq, as part of a balancing operation within domain "sd".
2686 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 *
2688 * Called with both runqueues locked.
2689 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002690static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02002691 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002692 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07002693 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002695 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02002696 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002697 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
Ingo Molnardd41f592007-07-09 18:51:59 +02002699 do {
Peter Williams43010652007-08-09 11:16:46 +02002700 total_load_moved +=
2701 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02002702 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002703 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02002704 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02002705 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
Peter Williams43010652007-08-09 11:16:46 +02002707 return total_load_moved > 0;
2708}
2709
Peter Williamse1d14842007-10-24 18:23:51 +02002710static int
2711iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2712 struct sched_domain *sd, enum cpu_idle_type idle,
2713 struct rq_iterator *iterator)
2714{
2715 struct task_struct *p = iterator->start(iterator->arg);
2716 int pinned = 0;
2717
2718 while (p) {
2719 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
2720 pull_task(busiest, p, this_rq, this_cpu);
2721 /*
2722 * Right now, this is only the second place pull_task()
2723 * is called, so we can safely collect pull_task()
2724 * stats here rather than inside pull_task().
2725 */
2726 schedstat_inc(sd, lb_gained[idle]);
2727
2728 return 1;
2729 }
2730 p = iterator->next(iterator->arg);
2731 }
2732
2733 return 0;
2734}
2735
Peter Williams43010652007-08-09 11:16:46 +02002736/*
2737 * move_one_task tries to move exactly one task from busiest to this_rq, as
2738 * part of active balancing operations within "domain".
2739 * Returns 1 if successful and 0 otherwise.
2740 *
2741 * Called with both runqueues locked.
2742 */
2743static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2744 struct sched_domain *sd, enum cpu_idle_type idle)
2745{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002746 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02002747
2748 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02002749 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02002750 return 1;
2751
2752 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753}
2754
2755/*
2756 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07002757 * domain. It calculates and returns the amount of weighted load which
2758 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 */
2760static struct sched_group *
2761find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02002762 unsigned long *imbalance, enum cpu_idle_type idle,
2763 int *sd_idle, cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
2765 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
2766 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002767 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07002768 unsigned long busiest_load_per_task, busiest_nr_running;
2769 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02002770 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002771#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2772 int power_savings_balance = 1;
2773 unsigned long leader_nr_running = 0, min_load_per_task = 0;
2774 unsigned long min_nr_running = ULONG_MAX;
2775 struct sched_group *group_min = NULL, *group_leader = NULL;
2776#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
2778 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002779 busiest_load_per_task = busiest_nr_running = 0;
2780 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002781 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002782 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002783 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002784 load_idx = sd->newidle_idx;
2785 else
2786 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
2788 do {
Ken Chen908a7c12007-10-17 16:55:11 +02002789 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 int local_group;
2791 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02002792 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002793 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002794 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
2796 local_group = cpu_isset(this_cpu, group->cpumask);
2797
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002798 if (local_group)
2799 balance_cpu = first_cpu(group->cpumask);
2800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07002802 sum_weighted_load = sum_nr_running = avg_load = 0;
Ken Chen908a7c12007-10-17 16:55:11 +02002803 max_cpu_load = 0;
2804 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
2806 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002807 struct rq *rq;
2808
2809 if (!cpu_isset(i, *cpus))
2810 continue;
2811
2812 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07002813
Suresh Siddha9439aab2007-07-19 21:28:35 +02002814 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07002815 *sd_idle = 0;
2816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002818 if (local_group) {
2819 if (idle_cpu(i) && !first_idle_cpu) {
2820 first_idle_cpu = 1;
2821 balance_cpu = i;
2822 }
2823
Nick Piggina2000572006-02-10 01:51:02 -08002824 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02002825 } else {
Nick Piggina2000572006-02-10 01:51:02 -08002826 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02002827 if (load > max_cpu_load)
2828 max_cpu_load = load;
2829 if (min_cpu_load > load)
2830 min_cpu_load = load;
2831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
2833 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07002834 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002835 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 }
2837
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002838 /*
2839 * First idle cpu or the first cpu(busiest) in this sched group
2840 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02002841 * domains. In the newly idle case, we will allow all the cpu's
2842 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002843 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02002844 if (idle != CPU_NEWLY_IDLE && local_group &&
2845 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002846 *balance = 0;
2847 goto ret;
2848 }
2849
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07002851 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
2853 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002854 avg_load = sg_div_cpu_power(group,
2855 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Ken Chen908a7c12007-10-17 16:55:11 +02002857 if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
2858 __group_imb = 1;
2859
Eric Dumazet5517d862007-05-08 00:32:57 -07002860 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002861
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 if (local_group) {
2863 this_load = avg_load;
2864 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002865 this_nr_running = sum_nr_running;
2866 this_load_per_task = sum_weighted_load;
2867 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02002868 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 max_load = avg_load;
2870 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002871 busiest_nr_running = sum_nr_running;
2872 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02002873 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002875
2876#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2877 /*
2878 * Busy processors will not participate in power savings
2879 * balance.
2880 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002881 if (idle == CPU_NOT_IDLE ||
2882 !(sd->flags & SD_POWERSAVINGS_BALANCE))
2883 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002884
2885 /*
2886 * If the local group is idle or completely loaded
2887 * no need to do power savings balance at this domain
2888 */
2889 if (local_group && (this_nr_running >= group_capacity ||
2890 !this_nr_running))
2891 power_savings_balance = 0;
2892
Ingo Molnardd41f592007-07-09 18:51:59 +02002893 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002894 * If a group is already running at full capacity or idle,
2895 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02002896 */
2897 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002898 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02002899 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002900
Ingo Molnardd41f592007-07-09 18:51:59 +02002901 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002902 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002903 * This is the group from where we need to pick up the load
2904 * for saving power
2905 */
2906 if ((sum_nr_running < min_nr_running) ||
2907 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002908 first_cpu(group->cpumask) <
2909 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002910 group_min = group;
2911 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002912 min_load_per_task = sum_weighted_load /
2913 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002914 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002915
Ingo Molnardd41f592007-07-09 18:51:59 +02002916 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002917 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02002918 * capacity but still has some space to pick up some load
2919 * from other group and save more power
2920 */
2921 if (sum_nr_running <= group_capacity - 1) {
2922 if (sum_nr_running > leader_nr_running ||
2923 (sum_nr_running == leader_nr_running &&
2924 first_cpu(group->cpumask) >
2925 first_cpu(group_leader->cpumask))) {
2926 group_leader = group;
2927 leader_nr_running = sum_nr_running;
2928 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002929 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002930group_next:
2931#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 group = group->next;
2933 } while (group != sd->groups);
2934
Peter Williams2dd73a42006-06-27 02:54:34 -07002935 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 goto out_balanced;
2937
2938 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
2939
2940 if (this_load >= avg_load ||
2941 100*max_load <= sd->imbalance_pct*this_load)
2942 goto out_balanced;
2943
Peter Williams2dd73a42006-06-27 02:54:34 -07002944 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02002945 if (group_imb)
2946 busiest_load_per_task = min(busiest_load_per_task, avg_load);
2947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 /*
2949 * We're trying to get all the cpus to the average_load, so we don't
2950 * want to push ourselves above the average load, nor do we wish to
2951 * reduce the max loaded cpu below the average load, as either of these
2952 * actions would just result in more rebalancing later, and ping-pong
2953 * tasks around. Thus we look for the minimum possible imbalance.
2954 * Negative imbalances (*we* are more loaded than anyone else) will
2955 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002956 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 * appear as very large values with unsigned longs.
2958 */
Peter Williams2dd73a42006-06-27 02:54:34 -07002959 if (max_load <= busiest_load_per_task)
2960 goto out_balanced;
2961
2962 /*
2963 * In the presence of smp nice balancing, certain scenarios can have
2964 * max load less than avg load(as we skip the groups at or below
2965 * its cpu_power, while calculating max_load..)
2966 */
2967 if (max_load < avg_load) {
2968 *imbalance = 0;
2969 goto small_imbalance;
2970 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002971
2972 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07002973 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002974
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07002976 *imbalance = min(max_pull * busiest->__cpu_power,
2977 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 / SCHED_LOAD_SCALE;
2979
Peter Williams2dd73a42006-06-27 02:54:34 -07002980 /*
2981 * if *imbalance is less than the average load per runnable task
2982 * there is no gaurantee that any tasks will be moved so we'll have
2983 * a think about bumping its value to force at least one task to be
2984 * moved
2985 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02002986 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07002987 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07002988 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Peter Williams2dd73a42006-06-27 02:54:34 -07002990small_imbalance:
2991 pwr_move = pwr_now = 0;
2992 imbn = 2;
2993 if (this_nr_running) {
2994 this_load_per_task /= this_nr_running;
2995 if (busiest_load_per_task > this_load_per_task)
2996 imbn = 1;
2997 } else
2998 this_load_per_task = SCHED_LOAD_SCALE;
2999
Ingo Molnardd41f592007-07-09 18:51:59 +02003000 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
3001 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07003002 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 return busiest;
3004 }
3005
3006 /*
3007 * OK, we don't have enough imbalance to justify moving tasks,
3008 * however we may be able to increase total CPU power used by
3009 * moving them.
3010 */
3011
Eric Dumazet5517d862007-05-08 00:32:57 -07003012 pwr_now += busiest->__cpu_power *
3013 min(busiest_load_per_task, max_load);
3014 pwr_now += this->__cpu_power *
3015 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 pwr_now /= SCHED_LOAD_SCALE;
3017
3018 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07003019 tmp = sg_div_cpu_power(busiest,
3020 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07003022 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07003023 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
3025 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07003026 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003027 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07003028 tmp = sg_div_cpu_power(this,
3029 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 else
Eric Dumazet5517d862007-05-08 00:32:57 -07003031 tmp = sg_div_cpu_power(this,
3032 busiest_load_per_task * SCHED_LOAD_SCALE);
3033 pwr_move += this->__cpu_power *
3034 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 pwr_move /= SCHED_LOAD_SCALE;
3036
3037 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02003038 if (pwr_move > pwr_now)
3039 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 }
3041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 return busiest;
3043
3044out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003045#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003046 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003047 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003049 if (this == group_leader && group_leader != group_min) {
3050 *imbalance = min_load_per_task;
3051 return group_min;
3052 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003053#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003054ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 *imbalance = 0;
3056 return NULL;
3057}
3058
3059/*
3060 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3061 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003062static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003063find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003064 unsigned long imbalance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003066 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003067 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 int i;
3069
3070 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003071 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003072
3073 if (!cpu_isset(i, *cpus))
3074 continue;
3075
Ingo Molnar48f24c42006-07-03 00:25:40 -07003076 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003077 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
Ingo Molnardd41f592007-07-09 18:51:59 +02003079 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003080 continue;
3081
Ingo Molnardd41f592007-07-09 18:51:59 +02003082 if (wl > max_load) {
3083 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003084 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 }
3086 }
3087
3088 return busiest;
3089}
3090
3091/*
Nick Piggin77391d72005-06-25 14:57:30 -07003092 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3093 * so long as it is large enough.
3094 */
3095#define MAX_PINNED_INTERVAL 512
3096
3097/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3099 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003101static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003102 struct sched_domain *sd, enum cpu_idle_type idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003103 int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
Peter Williams43010652007-08-09 11:16:46 +02003105 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003108 struct rq *busiest;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003109 cpumask_t cpus = CPU_MASK_ALL;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003110 unsigned long flags;
Nick Piggin5969fe02005-09-10 00:26:19 -07003111
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003112 /*
3113 * When power savings policy is enabled for the parent domain, idle
3114 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003115 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003116 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003117 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003118 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003119 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003120 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
Ingo Molnar2d723762007-10-15 17:00:12 +02003122 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003124redo:
3125 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003126 &cpus, balance);
3127
Chen, Kenneth W06066712006-12-10 02:20:35 -08003128 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003129 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003130
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 if (!group) {
3132 schedstat_inc(sd, lb_nobusyg[idle]);
3133 goto out_balanced;
3134 }
3135
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003136 busiest = find_busiest_queue(group, idle, imbalance, &cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 if (!busiest) {
3138 schedstat_inc(sd, lb_nobusyq[idle]);
3139 goto out_balanced;
3140 }
3141
Nick Piggindb935db2005-06-25 14:57:11 -07003142 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
3144 schedstat_add(sd, lb_imbalance[idle], imbalance);
3145
Peter Williams43010652007-08-09 11:16:46 +02003146 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 if (busiest->nr_running > 1) {
3148 /*
3149 * Attempt to move tasks. If find_busiest_group has found
3150 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003151 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 * correctly treated as an imbalance.
3153 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003154 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003155 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003156 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003157 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003158 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003159 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003160
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003161 /*
3162 * some other cpu did the load balance for us.
3163 */
Peter Williams43010652007-08-09 11:16:46 +02003164 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003165 resched_cpu(this_cpu);
3166
Nick Piggin81026792005-06-25 14:57:07 -07003167 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003168 if (unlikely(all_pinned)) {
3169 cpu_clear(cpu_of(busiest), cpus);
3170 if (!cpus_empty(cpus))
3171 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003172 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 }
Nick Piggin81026792005-06-25 14:57:07 -07003175
Peter Williams43010652007-08-09 11:16:46 +02003176 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 schedstat_inc(sd, lb_failed[idle]);
3178 sd->nr_balance_failed++;
3179
3180 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003182 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003183
3184 /* don't kick the migration_thread, if the curr
3185 * task on busiest cpu can't be moved to this_cpu
3186 */
3187 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003188 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003189 all_pinned = 1;
3190 goto out_one_pinned;
3191 }
3192
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 if (!busiest->active_balance) {
3194 busiest->active_balance = 1;
3195 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003196 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003198 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003199 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 wake_up_process(busiest->migration_thread);
3201
3202 /*
3203 * We've kicked active balancing, reset the failure
3204 * counter.
3205 */
Nick Piggin39507452005-06-25 14:57:09 -07003206 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 }
Nick Piggin81026792005-06-25 14:57:07 -07003208 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 sd->nr_balance_failed = 0;
3210
Nick Piggin81026792005-06-25 14:57:07 -07003211 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 /* We were unbalanced, so reset the balancing interval */
3213 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003214 } else {
3215 /*
3216 * If we've begun active balancing, start to back off. This
3217 * case may not be covered by the all_pinned logic if there
3218 * is only 1 task on the busy runqueue (because we don't call
3219 * move_tasks).
3220 */
3221 if (sd->balance_interval < sd->max_interval)
3222 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 }
3224
Peter Williams43010652007-08-09 11:16:46 +02003225 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003226 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003227 return -1;
Peter Williams43010652007-08-09 11:16:46 +02003228 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
3230out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 schedstat_inc(sd, lb_balanced[idle]);
3232
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003233 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003234
3235out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003237 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3238 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 sd->balance_interval *= 2;
3240
Ingo Molnar48f24c42006-07-03 00:25:40 -07003241 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003242 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003243 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 return 0;
3245}
3246
3247/*
3248 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3249 * tasks if there is an imbalance.
3250 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003251 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 * this_rq is locked.
3253 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003254static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07003255load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256{
3257 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003258 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003260 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003261 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003262 int all_pinned = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003263 cpumask_t cpus = CPU_MASK_ALL;
Nick Piggin5969fe02005-09-10 00:26:19 -07003264
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003265 /*
3266 * When power savings policy is enabled for the parent domain, idle
3267 * sibling can pick up load irrespective of busy siblings. In this case,
3268 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003269 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003270 */
3271 if (sd->flags & SD_SHARE_CPUPOWER &&
3272 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003273 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
Ingo Molnar2d723762007-10-15 17:00:12 +02003275 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003276redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003277 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003278 &sd_idle, &cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003280 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003281 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 }
3283
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003284 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003285 &cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003286 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003287 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003288 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 }
3290
Nick Piggindb935db2005-06-25 14:57:11 -07003291 BUG_ON(busiest == this_rq);
3292
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003293 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003294
Peter Williams43010652007-08-09 11:16:46 +02003295 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003296 if (busiest->nr_running > 1) {
3297 /* Attempt to move tasks */
3298 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003299 /* this_rq->clock is already updated */
3300 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003301 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003302 imbalance, sd, CPU_NEWLY_IDLE,
3303 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003304 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003305
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003306 if (unlikely(all_pinned)) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003307 cpu_clear(cpu_of(busiest), cpus);
3308 if (!cpus_empty(cpus))
3309 goto redo;
3310 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003311 }
3312
Peter Williams43010652007-08-09 11:16:46 +02003313 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003314 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003315 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3316 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003317 return -1;
3318 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003319 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
Peter Williams43010652007-08-09 11:16:46 +02003321 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003322
3323out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003324 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003325 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003326 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003327 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003328 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003329
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331}
3332
3333/*
3334 * idle_balance is called by schedule() if this_cpu is about to become
3335 * idle. Attempts to pull tasks from other CPUs.
3336 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003337static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338{
3339 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003340 int pulled_task = -1;
3341 unsigned long next_balance = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
3343 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003344 unsigned long interval;
3345
3346 if (!(sd->flags & SD_LOAD_BALANCE))
3347 continue;
3348
3349 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003350 /* If we've pulled tasks over stop searching: */
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003351 pulled_task = load_balance_newidle(this_cpu,
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003352 this_rq, sd);
3353
3354 interval = msecs_to_jiffies(sd->balance_interval);
3355 if (time_after(next_balance, sd->last_balance + interval))
3356 next_balance = sd->last_balance + interval;
3357 if (pulled_task)
3358 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003360 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003361 /*
3362 * We are going idle. next_balance may be set based on
3363 * a busy processor. So reset next_balance.
3364 */
3365 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367}
3368
3369/*
3370 * active_load_balance is run by migration threads. It pushes running tasks
3371 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3372 * running on each physical CPU where possible, and avoids physical /
3373 * logical imbalances.
3374 *
3375 * Called with busiest_rq locked.
3376 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003377static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378{
Nick Piggin39507452005-06-25 14:57:09 -07003379 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003380 struct sched_domain *sd;
3381 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003382
Ingo Molnar48f24c42006-07-03 00:25:40 -07003383 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003384 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003385 return;
3386
3387 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
3389 /*
Nick Piggin39507452005-06-25 14:57:09 -07003390 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003391 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003392 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 */
Nick Piggin39507452005-06-25 14:57:09 -07003394 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395
Nick Piggin39507452005-06-25 14:57:09 -07003396 /* move a task from busiest_rq to target_rq */
3397 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003398 update_rq_clock(busiest_rq);
3399 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
Nick Piggin39507452005-06-25 14:57:09 -07003401 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003402 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07003403 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003404 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07003405 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
Ingo Molnar48f24c42006-07-03 00:25:40 -07003408 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003409 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Peter Williams43010652007-08-09 11:16:46 +02003411 if (move_one_task(target_rq, target_cpu, busiest_rq,
3412 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07003413 schedstat_inc(sd, alb_pushed);
3414 else
3415 schedstat_inc(sd, alb_failed);
3416 }
Nick Piggin39507452005-06-25 14:57:09 -07003417 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418}
3419
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003420#ifdef CONFIG_NO_HZ
3421static struct {
3422 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003423 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003424} nohz ____cacheline_aligned = {
3425 .load_balancer = ATOMIC_INIT(-1),
3426 .cpu_mask = CPU_MASK_NONE,
3427};
3428
Christoph Lameter7835b982006-12-10 02:20:22 -08003429/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003430 * This routine will try to nominate the ilb (idle load balancing)
3431 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3432 * load balancing on behalf of all those cpus. If all the cpus in the system
3433 * go into this tickless mode, then there will be no ilb owner (as there is
3434 * no need for one) and all the cpus will sleep till the next wakeup event
3435 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08003436 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003437 * For the ilb owner, tick is not stopped. And this tick will be used
3438 * for idle load balancing. ilb owner will still be part of
3439 * nohz.cpu_mask..
3440 *
3441 * While stopping the tick, this cpu will become the ilb owner if there
3442 * is no other owner. And will be the owner till that cpu becomes busy
3443 * or if all cpus in the system stop their ticks at which point
3444 * there is no need for ilb owner.
3445 *
3446 * When the ilb owner becomes busy, it nominates another owner, during the
3447 * next busy scheduler_tick()
3448 */
3449int select_nohz_load_balancer(int stop_tick)
3450{
3451 int cpu = smp_processor_id();
3452
3453 if (stop_tick) {
3454 cpu_set(cpu, nohz.cpu_mask);
3455 cpu_rq(cpu)->in_nohz_recently = 1;
3456
3457 /*
3458 * If we are going offline and still the leader, give up!
3459 */
3460 if (cpu_is_offline(cpu) &&
3461 atomic_read(&nohz.load_balancer) == cpu) {
3462 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3463 BUG();
3464 return 0;
3465 }
3466
3467 /* time for ilb owner also to sleep */
3468 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3469 if (atomic_read(&nohz.load_balancer) == cpu)
3470 atomic_set(&nohz.load_balancer, -1);
3471 return 0;
3472 }
3473
3474 if (atomic_read(&nohz.load_balancer) == -1) {
3475 /* make me the ilb owner */
3476 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
3477 return 1;
3478 } else if (atomic_read(&nohz.load_balancer) == cpu)
3479 return 1;
3480 } else {
3481 if (!cpu_isset(cpu, nohz.cpu_mask))
3482 return 0;
3483
3484 cpu_clear(cpu, nohz.cpu_mask);
3485
3486 if (atomic_read(&nohz.load_balancer) == cpu)
3487 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3488 BUG();
3489 }
3490 return 0;
3491}
3492#endif
3493
3494static DEFINE_SPINLOCK(balancing);
3495
3496/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003497 * It checks each scheduling domain to see if it is due to be balanced,
3498 * and initiates a balancing operation if so.
3499 *
3500 * Balancing parameters are set up in arch_init_sched_domains.
3501 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003502static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08003503{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003504 int balance = 1;
3505 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08003506 unsigned long interval;
3507 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003508 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08003509 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003510 int update_next_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003512 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 if (!(sd->flags & SD_LOAD_BALANCE))
3514 continue;
3515
3516 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003517 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 interval *= sd->busy_factor;
3519
3520 /* scale ms to jiffies */
3521 interval = msecs_to_jiffies(interval);
3522 if (unlikely(!interval))
3523 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003524 if (interval > HZ*NR_CPUS/10)
3525 interval = HZ*NR_CPUS/10;
3526
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527
Christoph Lameter08c183f2006-12-10 02:20:29 -08003528 if (sd->flags & SD_SERIALIZE) {
3529 if (!spin_trylock(&balancing))
3530 goto out;
3531 }
3532
Christoph Lameterc9819f42006-12-10 02:20:25 -08003533 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003534 if (load_balance(cpu, rq, sd, idle, &balance)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003535 /*
3536 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07003537 * longer idle, or one of our SMT siblings is
3538 * not idle.
3539 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003540 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003542 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08003544 if (sd->flags & SD_SERIALIZE)
3545 spin_unlock(&balancing);
3546out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02003547 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08003548 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003549 update_next_balance = 1;
3550 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003551
3552 /*
3553 * Stop the load balance at this level. There is another
3554 * CPU in our sched group which is doing load balancing more
3555 * actively.
3556 */
3557 if (!balance)
3558 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02003560
3561 /*
3562 * next_balance will be updated only when there is a need.
3563 * When the cpu is attached to null domain for ex, it will not be
3564 * updated.
3565 */
3566 if (likely(update_next_balance))
3567 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003568}
3569
3570/*
3571 * run_rebalance_domains is triggered when needed from the scheduler tick.
3572 * In CONFIG_NO_HZ case, the idle load balance owner will do the
3573 * rebalancing for all the cpus for whom scheduler ticks are stopped.
3574 */
3575static void run_rebalance_domains(struct softirq_action *h)
3576{
Ingo Molnardd41f592007-07-09 18:51:59 +02003577 int this_cpu = smp_processor_id();
3578 struct rq *this_rq = cpu_rq(this_cpu);
3579 enum cpu_idle_type idle = this_rq->idle_at_tick ?
3580 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003581
Ingo Molnardd41f592007-07-09 18:51:59 +02003582 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003583
3584#ifdef CONFIG_NO_HZ
3585 /*
3586 * If this cpu is the owner for idle load balancing, then do the
3587 * balancing on behalf of the other idle cpus whose ticks are
3588 * stopped.
3589 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003590 if (this_rq->idle_at_tick &&
3591 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003592 cpumask_t cpus = nohz.cpu_mask;
3593 struct rq *rq;
3594 int balance_cpu;
3595
Ingo Molnardd41f592007-07-09 18:51:59 +02003596 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003597 for_each_cpu_mask(balance_cpu, cpus) {
3598 /*
3599 * If this cpu gets work to do, stop the load balancing
3600 * work being done for other cpus. Next load
3601 * balancing owner will pick it up.
3602 */
3603 if (need_resched())
3604 break;
3605
Oleg Nesterovde0cf892007-08-12 18:08:19 +02003606 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003607
3608 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003609 if (time_after(this_rq->next_balance, rq->next_balance))
3610 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003611 }
3612 }
3613#endif
3614}
3615
3616/*
3617 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
3618 *
3619 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
3620 * idle load balancing owner or decide to stop the periodic load balancing,
3621 * if the whole system is idle.
3622 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003623static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003624{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003625#ifdef CONFIG_NO_HZ
3626 /*
3627 * If we were in the nohz mode recently and busy at the current
3628 * scheduler tick, then check if we need to nominate new idle
3629 * load balancer.
3630 */
3631 if (rq->in_nohz_recently && !rq->idle_at_tick) {
3632 rq->in_nohz_recently = 0;
3633
3634 if (atomic_read(&nohz.load_balancer) == cpu) {
3635 cpu_clear(cpu, nohz.cpu_mask);
3636 atomic_set(&nohz.load_balancer, -1);
3637 }
3638
3639 if (atomic_read(&nohz.load_balancer) == -1) {
3640 /*
3641 * simple selection for now: Nominate the
3642 * first cpu in the nohz list to be the next
3643 * ilb owner.
3644 *
3645 * TBD: Traverse the sched domains and nominate
3646 * the nearest cpu in the nohz.cpu_mask.
3647 */
3648 int ilb = first_cpu(nohz.cpu_mask);
3649
3650 if (ilb != NR_CPUS)
3651 resched_cpu(ilb);
3652 }
3653 }
3654
3655 /*
3656 * If this cpu is idle and doing idle load balancing for all the
3657 * cpus with ticks stopped, is it time for that to stop?
3658 */
3659 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
3660 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3661 resched_cpu(cpu);
3662 return;
3663 }
3664
3665 /*
3666 * If this cpu is idle and the idle load balancing is done by
3667 * someone else, then no need raise the SCHED_SOFTIRQ
3668 */
3669 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
3670 cpu_isset(cpu, nohz.cpu_mask))
3671 return;
3672#endif
3673 if (time_after_eq(jiffies, rq->next_balance))
3674 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675}
Ingo Molnardd41f592007-07-09 18:51:59 +02003676
3677#else /* CONFIG_SMP */
3678
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679/*
3680 * on UP we do not need to balance between CPUs:
3681 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003682static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683{
3684}
Ingo Molnardd41f592007-07-09 18:51:59 +02003685
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686#endif
3687
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688DEFINE_PER_CPU(struct kernel_stat, kstat);
3689
3690EXPORT_PER_CPU_SYMBOL(kstat);
3691
3692/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02003693 * Return p->sum_exec_runtime plus any more ns on the sched_clock
3694 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02003696unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003699 u64 ns, delta_exec;
3700 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003701
Ingo Molnar41b86e92007-07-09 18:51:58 +02003702 rq = task_rq_lock(p, &flags);
3703 ns = p->se.sum_exec_runtime;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01003704 if (task_current(rq, p)) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02003705 update_rq_clock(rq);
3706 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003707 if ((s64)delta_exec > 0)
3708 ns += delta_exec;
3709 }
3710 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003711
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 return ns;
3713}
3714
3715/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 * Account user cpu time to a process.
3717 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 * @cputime: the cpu time spent in user space since the last update
3719 */
3720void account_user_time(struct task_struct *p, cputime_t cputime)
3721{
3722 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3723 cputime64_t tmp;
3724
3725 p->utime = cputime_add(p->utime, cputime);
3726
3727 /* Add user time to cpustat. */
3728 tmp = cputime_to_cputime64(cputime);
3729 if (TASK_NICE(p) > 0)
3730 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3731 else
3732 cpustat->user = cputime64_add(cpustat->user, tmp);
3733}
3734
3735/*
Laurent Vivier94886b82007-10-15 17:00:19 +02003736 * Account guest cpu time to a process.
3737 * @p: the process that the cpu time gets accounted to
3738 * @cputime: the cpu time spent in virtual machine since the last update
3739 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01003740static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02003741{
3742 cputime64_t tmp;
3743 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3744
3745 tmp = cputime_to_cputime64(cputime);
3746
3747 p->utime = cputime_add(p->utime, cputime);
3748 p->gtime = cputime_add(p->gtime, cputime);
3749
3750 cpustat->user = cputime64_add(cpustat->user, tmp);
3751 cpustat->guest = cputime64_add(cpustat->guest, tmp);
3752}
3753
3754/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07003755 * Account scaled user cpu time to a process.
3756 * @p: the process that the cpu time gets accounted to
3757 * @cputime: the cpu time spent in user space since the last update
3758 */
3759void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
3760{
3761 p->utimescaled = cputime_add(p->utimescaled, cputime);
3762}
3763
3764/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 * Account system cpu time to a process.
3766 * @p: the process that the cpu time gets accounted to
3767 * @hardirq_offset: the offset to subtract from hardirq_count()
3768 * @cputime: the cpu time spent in kernel space since the last update
3769 */
3770void account_system_time(struct task_struct *p, int hardirq_offset,
3771 cputime_t cputime)
3772{
3773 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003774 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 cputime64_t tmp;
3776
Christian Borntraeger97783852007-11-15 20:57:39 +01003777 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0))
3778 return account_guest_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02003779
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 p->stime = cputime_add(p->stime, cputime);
3781
3782 /* Add system time to cpustat. */
3783 tmp = cputime_to_cputime64(cputime);
3784 if (hardirq_count() - hardirq_offset)
3785 cpustat->irq = cputime64_add(cpustat->irq, tmp);
3786 else if (softirq_count())
3787 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003788 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003790 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3792 else
3793 cpustat->idle = cputime64_add(cpustat->idle, tmp);
3794 /* Account for system time used */
3795 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796}
3797
3798/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07003799 * Account scaled system cpu time to a process.
3800 * @p: the process that the cpu time gets accounted to
3801 * @hardirq_offset: the offset to subtract from hardirq_count()
3802 * @cputime: the cpu time spent in kernel space since the last update
3803 */
3804void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
3805{
3806 p->stimescaled = cputime_add(p->stimescaled, cputime);
3807}
3808
3809/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 * Account for involuntary wait time.
3811 * @p: the process from which the cpu time has been stolen
3812 * @steal: the cpu time spent in involuntary wait
3813 */
3814void account_steal_time(struct task_struct *p, cputime_t steal)
3815{
3816 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3817 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07003818 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819
3820 if (p == rq->idle) {
3821 p->stime = cputime_add(p->stime, steal);
3822 if (atomic_read(&rq->nr_iowait) > 0)
3823 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3824 else
3825 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003826 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 cpustat->steal = cputime64_add(cpustat->steal, tmp);
3828}
3829
Christoph Lameter7835b982006-12-10 02:20:22 -08003830/*
3831 * This function gets called by the timer code, with HZ frequency.
3832 * We call it with interrupts disabled.
3833 *
3834 * It also gets called by the fork code, when changing the parent's
3835 * timeslices.
3836 */
3837void scheduler_tick(void)
3838{
Christoph Lameter7835b982006-12-10 02:20:22 -08003839 int cpu = smp_processor_id();
3840 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003841 struct task_struct *curr = rq->curr;
Ingo Molnar529c7722007-08-10 23:05:11 +02003842 u64 next_tick = rq->tick_timestamp + TICK_NSEC;
Christoph Lameter7835b982006-12-10 02:20:22 -08003843
Ingo Molnardd41f592007-07-09 18:51:59 +02003844 spin_lock(&rq->lock);
Ingo Molnar546fe3c2007-08-09 11:16:51 +02003845 __update_rq_clock(rq);
Ingo Molnar529c7722007-08-10 23:05:11 +02003846 /*
3847 * Let rq->clock advance by at least TICK_NSEC:
3848 */
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01003849 if (unlikely(rq->clock < next_tick)) {
Ingo Molnar529c7722007-08-10 23:05:11 +02003850 rq->clock = next_tick;
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01003851 rq->clock_underflows++;
3852 }
Ingo Molnar529c7722007-08-10 23:05:11 +02003853 rq->tick_timestamp = rq->clock;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02003854 update_last_tick_seen(rq);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02003855 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01003856 curr->sched_class->task_tick(rq, curr, 0);
3857 update_sched_rt_period(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003858 spin_unlock(&rq->lock);
3859
Christoph Lametere418e1c2006-12-10 02:20:23 -08003860#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02003861 rq->idle_at_tick = idle_cpu(cpu);
3862 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08003863#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864}
3865
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
3867
Srinivasa Ds43627582008-02-23 15:24:04 -08003868void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869{
3870 /*
3871 * Underflow?
3872 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003873 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
3874 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 preempt_count() += val;
3876 /*
3877 * Spinlock count overflowing soon?
3878 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003879 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
3880 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881}
3882EXPORT_SYMBOL(add_preempt_count);
3883
Srinivasa Ds43627582008-02-23 15:24:04 -08003884void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885{
3886 /*
3887 * Underflow?
3888 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003889 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
3890 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 /*
3892 * Is the spinlock portion underflowing?
3893 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003894 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
3895 !(preempt_count() & PREEMPT_MASK)))
3896 return;
3897
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 preempt_count() -= val;
3899}
3900EXPORT_SYMBOL(sub_preempt_count);
3901
3902#endif
3903
3904/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003905 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003907static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908{
Satyam Sharma838225b2007-10-24 18:23:50 +02003909 struct pt_regs *regs = get_irq_regs();
3910
3911 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
3912 prev->comm, prev->pid, preempt_count());
3913
Ingo Molnardd41f592007-07-09 18:51:59 +02003914 debug_show_held_locks(prev);
3915 if (irqs_disabled())
3916 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02003917
3918 if (regs)
3919 show_regs(regs);
3920 else
3921 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02003922}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923
Ingo Molnardd41f592007-07-09 18:51:59 +02003924/*
3925 * Various schedule()-time debugging checks and statistics:
3926 */
3927static inline void schedule_debug(struct task_struct *prev)
3928{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003930 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 * schedule() atomically, we ignore that path for now.
3932 * Otherwise, whine if we are scheduling when we should not be.
3933 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003934 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
3935 __schedule_bug(prev);
3936
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
3938
Ingo Molnar2d723762007-10-15 17:00:12 +02003939 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02003940#ifdef CONFIG_SCHEDSTATS
3941 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003942 schedstat_inc(this_rq(), bkl_count);
3943 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02003944 }
3945#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02003946}
3947
3948/*
3949 * Pick up the highest-prio task:
3950 */
3951static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02003952pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02003953{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003954 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02003955 struct task_struct *p;
3956
3957 /*
3958 * Optimization: we know that if all tasks are in
3959 * the fair class we can call that function directly:
3960 */
3961 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003962 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003963 if (likely(p))
3964 return p;
3965 }
3966
3967 class = sched_class_highest;
3968 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003969 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003970 if (p)
3971 return p;
3972 /*
3973 * Will never be NULL as the idle class always
3974 * returns a non-NULL p:
3975 */
3976 class = class->next;
3977 }
3978}
3979
3980/*
3981 * schedule() is the main scheduler function.
3982 */
3983asmlinkage void __sched schedule(void)
3984{
3985 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08003986 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02003987 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02003988 int cpu;
3989
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990need_resched:
3991 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02003992 cpu = smp_processor_id();
3993 rq = cpu_rq(cpu);
3994 rcu_qsctr_inc(cpu);
3995 prev = rq->curr;
3996 switch_count = &prev->nivcsw;
3997
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 release_kernel_lock(prev);
3999need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
Ingo Molnardd41f592007-07-09 18:51:59 +02004001 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004003 hrtick_clear(rq);
4004
Ingo Molnar1e819952007-10-15 17:00:13 +02004005 /*
4006 * Do the rq-clock update outside the rq lock:
4007 */
4008 local_irq_disable();
Ingo Molnarc1b3da32007-08-09 11:16:47 +02004009 __update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02004010 spin_lock(&rq->lock);
4011 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
Ingo Molnardd41f592007-07-09 18:51:59 +02004013 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
4014 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
Roel Kluin23e3c3c2008-03-13 17:41:59 +01004015 signal_pending(prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02004016 prev->state = TASK_RUNNING;
4017 } else {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004018 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02004019 }
4020 switch_count = &prev->nvcsw;
4021 }
4022
Steven Rostedt9a897c52008-01-25 21:08:22 +01004023#ifdef CONFIG_SMP
4024 if (prev->sched_class->pre_schedule)
4025 prev->sched_class->pre_schedule(rq, prev);
4026#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01004027
Ingo Molnardd41f592007-07-09 18:51:59 +02004028 if (unlikely(!rq->nr_running))
4029 idle_balance(cpu, rq);
4030
Ingo Molnar31ee5292007-08-09 11:16:49 +02004031 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02004032 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033
4034 sched_info_switch(prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02004035
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 rq->nr_switches++;
4038 rq->curr = next;
4039 ++*switch_count;
4040
Ingo Molnardd41f592007-07-09 18:51:59 +02004041 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004042 /*
4043 * the context switch might have flipped the stack from under
4044 * us, hence refresh the local variables.
4045 */
4046 cpu = smp_processor_id();
4047 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 } else
4049 spin_unlock_irq(&rq->lock);
4050
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004051 hrtick_set(rq);
4052
4053 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004055
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 preempt_enable_no_resched();
4057 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4058 goto need_resched;
4059}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060EXPORT_SYMBOL(schedule);
4061
4062#ifdef CONFIG_PREEMPT
4063/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004064 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004065 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 * occur there and call schedule directly.
4067 */
4068asmlinkage void __sched preempt_schedule(void)
4069{
4070 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 struct task_struct *task = current;
4072 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004073
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 /*
4075 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004076 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004078 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 return;
4080
Andi Kleen3a5c3592007-10-15 17:00:14 +02004081 do {
4082 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083
Andi Kleen3a5c3592007-10-15 17:00:14 +02004084 /*
4085 * We keep the big kernel semaphore locked, but we
4086 * clear ->lock_depth so that schedule() doesnt
4087 * auto-release the semaphore:
4088 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004089 saved_lock_depth = task->lock_depth;
4090 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004091 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004092 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004093 sub_preempt_count(PREEMPT_ACTIVE);
4094
4095 /*
4096 * Check again in case we missed a preemption opportunity
4097 * between schedule and now.
4098 */
4099 barrier();
4100 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102EXPORT_SYMBOL(preempt_schedule);
4103
4104/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004105 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 * off of irq context.
4107 * Note, that this is called and return with irqs disabled. This will
4108 * protect us against recursive calling from irq.
4109 */
4110asmlinkage void __sched preempt_schedule_irq(void)
4111{
4112 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 struct task_struct *task = current;
4114 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004115
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004116 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 BUG_ON(ti->preempt_count || !irqs_disabled());
4118
Andi Kleen3a5c3592007-10-15 17:00:14 +02004119 do {
4120 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
Andi Kleen3a5c3592007-10-15 17:00:14 +02004122 /*
4123 * We keep the big kernel semaphore locked, but we
4124 * clear ->lock_depth so that schedule() doesnt
4125 * auto-release the semaphore:
4126 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004127 saved_lock_depth = task->lock_depth;
4128 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004129 local_irq_enable();
4130 schedule();
4131 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004132 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004133 sub_preempt_count(PREEMPT_ACTIVE);
4134
4135 /*
4136 * Check again in case we missed a preemption opportunity
4137 * between schedule and now.
4138 */
4139 barrier();
4140 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141}
4142
4143#endif /* CONFIG_PREEMPT */
4144
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004145int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4146 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004148 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150EXPORT_SYMBOL(default_wake_function);
4151
4152/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004153 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4154 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 * number) then we wake all the non-exclusive tasks and one exclusive task.
4156 *
4157 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004158 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4160 */
4161static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4162 int nr_exclusive, int sync, void *key)
4163{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004164 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004166 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004167 unsigned flags = curr->flags;
4168
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004170 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 break;
4172 }
4173}
4174
4175/**
4176 * __wake_up - wake up threads blocked on a waitqueue.
4177 * @q: the waitqueue
4178 * @mode: which threads
4179 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004180 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004182void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004183 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184{
4185 unsigned long flags;
4186
4187 spin_lock_irqsave(&q->lock, flags);
4188 __wake_up_common(q, mode, nr_exclusive, 0, key);
4189 spin_unlock_irqrestore(&q->lock, flags);
4190}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191EXPORT_SYMBOL(__wake_up);
4192
4193/*
4194 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4195 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004196void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197{
4198 __wake_up_common(q, mode, 1, 0, NULL);
4199}
4200
4201/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004202 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 * @q: the waitqueue
4204 * @mode: which threads
4205 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4206 *
4207 * The sync wakeup differs that the waker knows that it will schedule
4208 * away soon, so while the target thread will be woken up, it will not
4209 * be migrated to another CPU - ie. the two threads are 'synchronized'
4210 * with each other. This can prevent needless bouncing between CPUs.
4211 *
4212 * On UP it can prevent extra preemption.
4213 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004214void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004215__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216{
4217 unsigned long flags;
4218 int sync = 1;
4219
4220 if (unlikely(!q))
4221 return;
4222
4223 if (unlikely(!nr_exclusive))
4224 sync = 0;
4225
4226 spin_lock_irqsave(&q->lock, flags);
4227 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4228 spin_unlock_irqrestore(&q->lock, flags);
4229}
4230EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4231
Ingo Molnarb15136e2007-10-24 18:23:48 +02004232void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233{
4234 unsigned long flags;
4235
4236 spin_lock_irqsave(&x->wait.lock, flags);
4237 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004238 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 spin_unlock_irqrestore(&x->wait.lock, flags);
4240}
4241EXPORT_SYMBOL(complete);
4242
Ingo Molnarb15136e2007-10-24 18:23:48 +02004243void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244{
4245 unsigned long flags;
4246
4247 spin_lock_irqsave(&x->wait.lock, flags);
4248 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004249 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 spin_unlock_irqrestore(&x->wait.lock, flags);
4251}
4252EXPORT_SYMBOL(complete_all);
4253
Andi Kleen8cbbe862007-10-15 17:00:14 +02004254static inline long __sched
4255do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 if (!x->done) {
4258 DECLARE_WAITQUEUE(wait, current);
4259
4260 wait.flags |= WQ_FLAG_EXCLUSIVE;
4261 __add_wait_queue_tail(&x->wait, &wait);
4262 do {
Matthew Wilcox009e5772007-12-06 12:29:54 -05004263 if ((state == TASK_INTERRUPTIBLE &&
4264 signal_pending(current)) ||
4265 (state == TASK_KILLABLE &&
4266 fatal_signal_pending(current))) {
Andi Kleen8cbbe862007-10-15 17:00:14 +02004267 __remove_wait_queue(&x->wait, &wait);
4268 return -ERESTARTSYS;
4269 }
4270 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004272 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 spin_lock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004274 if (!timeout) {
4275 __remove_wait_queue(&x->wait, &wait);
4276 return timeout;
4277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 } while (!x->done);
4279 __remove_wait_queue(&x->wait, &wait);
4280 }
4281 x->done--;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004282 return timeout;
4283}
4284
4285static long __sched
4286wait_for_common(struct completion *x, long timeout, int state)
4287{
4288 might_sleep();
4289
4290 spin_lock_irq(&x->wait.lock);
4291 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004293 return timeout;
4294}
4295
Ingo Molnarb15136e2007-10-24 18:23:48 +02004296void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004297{
4298 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299}
4300EXPORT_SYMBOL(wait_for_completion);
4301
Ingo Molnarb15136e2007-10-24 18:23:48 +02004302unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4304{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004305 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306}
4307EXPORT_SYMBOL(wait_for_completion_timeout);
4308
Andi Kleen8cbbe862007-10-15 17:00:14 +02004309int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310{
Andi Kleen51e97992007-10-18 21:32:55 +02004311 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4312 if (t == -ERESTARTSYS)
4313 return t;
4314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315}
4316EXPORT_SYMBOL(wait_for_completion_interruptible);
4317
Ingo Molnarb15136e2007-10-24 18:23:48 +02004318unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319wait_for_completion_interruptible_timeout(struct completion *x,
4320 unsigned long timeout)
4321{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004322 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323}
4324EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4325
Matthew Wilcox009e5772007-12-06 12:29:54 -05004326int __sched wait_for_completion_killable(struct completion *x)
4327{
4328 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4329 if (t == -ERESTARTSYS)
4330 return t;
4331 return 0;
4332}
4333EXPORT_SYMBOL(wait_for_completion_killable);
4334
Andi Kleen8cbbe862007-10-15 17:00:14 +02004335static long __sched
4336sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004337{
4338 unsigned long flags;
4339 wait_queue_t wait;
4340
4341 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
Andi Kleen8cbbe862007-10-15 17:00:14 +02004343 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
Andi Kleen8cbbe862007-10-15 17:00:14 +02004345 spin_lock_irqsave(&q->lock, flags);
4346 __add_wait_queue(q, &wait);
4347 spin_unlock(&q->lock);
4348 timeout = schedule_timeout(timeout);
4349 spin_lock_irq(&q->lock);
4350 __remove_wait_queue(q, &wait);
4351 spin_unlock_irqrestore(&q->lock, flags);
4352
4353 return timeout;
4354}
4355
4356void __sched interruptible_sleep_on(wait_queue_head_t *q)
4357{
4358 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360EXPORT_SYMBOL(interruptible_sleep_on);
4361
Ingo Molnar0fec1712007-07-09 18:52:01 +02004362long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004363interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004365 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4368
Ingo Molnar0fec1712007-07-09 18:52:01 +02004369void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004371 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373EXPORT_SYMBOL(sleep_on);
4374
Ingo Molnar0fec1712007-07-09 18:52:01 +02004375long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004377 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379EXPORT_SYMBOL(sleep_on_timeout);
4380
Ingo Molnarb29739f2006-06-27 02:54:51 -07004381#ifdef CONFIG_RT_MUTEXES
4382
4383/*
4384 * rt_mutex_setprio - set the current priority of a task
4385 * @p: task
4386 * @prio: prio value (kernel-internal form)
4387 *
4388 * This function changes the 'effective' priority of a task. It does
4389 * not touch ->normal_prio like __setscheduler().
4390 *
4391 * Used by the rt_mutex code to implement priority inheritance logic.
4392 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004393void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004394{
4395 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004396 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004397 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01004398 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004399
4400 BUG_ON(prio < 0 || prio > MAX_PRIO);
4401
4402 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004403 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004404
Andrew Mortond5f9f942007-05-08 20:27:06 -07004405 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004406 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004407 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004408 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004409 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004410 if (running)
4411 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004412
4413 if (rt_prio(prio))
4414 p->sched_class = &rt_sched_class;
4415 else
4416 p->sched_class = &fair_sched_class;
4417
Ingo Molnarb29739f2006-06-27 02:54:51 -07004418 p->prio = prio;
4419
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004420 if (running)
4421 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004422 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004423 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004424
4425 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004426 }
4427 task_rq_unlock(rq, &flags);
4428}
4429
4430#endif
4431
Ingo Molnar36c8b582006-07-03 00:25:41 -07004432void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433{
Ingo Molnardd41f592007-07-09 18:51:59 +02004434 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004436 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
4438 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
4439 return;
4440 /*
4441 * We have to be careful, if called from sys_setpriority(),
4442 * the task might be in the middle of scheduling on another CPU.
4443 */
4444 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004445 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 /*
4447 * The RT priorities are set via sched_setscheduler(), but we still
4448 * allow the 'normal' nice value to be set - but as expected
4449 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02004450 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 */
Ingo Molnare05606d2007-07-09 18:51:59 +02004452 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 p->static_prio = NICE_TO_PRIO(nice);
4454 goto out_unlock;
4455 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004456 on_rq = p->se.on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004457 if (on_rq) {
Ingo Molnar69be72c2007-08-09 11:16:49 +02004458 dequeue_task(rq, p, 0);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004459 dec_load(rq, p);
4460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07004463 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004464 old_prio = p->prio;
4465 p->prio = effective_prio(p);
4466 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467
Ingo Molnardd41f592007-07-09 18:51:59 +02004468 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004469 enqueue_task(rq, p, 0);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004470 inc_load(rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07004472 * If the task increased its priority or is running and
4473 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004475 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 resched_task(rq->curr);
4477 }
4478out_unlock:
4479 task_rq_unlock(rq, &flags);
4480}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481EXPORT_SYMBOL(set_user_nice);
4482
Matt Mackalle43379f2005-05-01 08:59:00 -07004483/*
4484 * can_nice - check if a task can reduce its nice value
4485 * @p: task
4486 * @nice: nice value
4487 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004488int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07004489{
Matt Mackall024f4742005-08-18 11:24:19 -07004490 /* convert nice value [19,-20] to rlimit style value [1,40] */
4491 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004492
Matt Mackalle43379f2005-05-01 08:59:00 -07004493 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
4494 capable(CAP_SYS_NICE));
4495}
4496
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497#ifdef __ARCH_WANT_SYS_NICE
4498
4499/*
4500 * sys_nice - change the priority of the current process.
4501 * @increment: priority increment
4502 *
4503 * sys_setpriority is a more generic, but much slower function that
4504 * does similar things.
4505 */
4506asmlinkage long sys_nice(int increment)
4507{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004508 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
4510 /*
4511 * Setpriority might change our priority at the same moment.
4512 * We don't have to worry. Conceptually one call occurs first
4513 * and we have a single winner.
4514 */
Matt Mackalle43379f2005-05-01 08:59:00 -07004515 if (increment < -40)
4516 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 if (increment > 40)
4518 increment = 40;
4519
4520 nice = PRIO_TO_NICE(current->static_prio) + increment;
4521 if (nice < -20)
4522 nice = -20;
4523 if (nice > 19)
4524 nice = 19;
4525
Matt Mackalle43379f2005-05-01 08:59:00 -07004526 if (increment < 0 && !can_nice(current, nice))
4527 return -EPERM;
4528
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 retval = security_task_setnice(current, nice);
4530 if (retval)
4531 return retval;
4532
4533 set_user_nice(current, nice);
4534 return 0;
4535}
4536
4537#endif
4538
4539/**
4540 * task_prio - return the priority value of a given task.
4541 * @p: the task in question.
4542 *
4543 * This is the priority value as seen by users in /proc.
4544 * RT tasks are offset by -200. Normal tasks are centered
4545 * around 0, value goes from -16 to +15.
4546 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004547int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548{
4549 return p->prio - MAX_RT_PRIO;
4550}
4551
4552/**
4553 * task_nice - return the nice value of a given task.
4554 * @p: the task in question.
4555 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004556int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557{
4558 return TASK_NICE(p);
4559}
Pavel Roskin150d8be2008-03-05 16:56:37 -05004560EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
4562/**
4563 * idle_cpu - is a given cpu idle currently?
4564 * @cpu: the processor in question.
4565 */
4566int idle_cpu(int cpu)
4567{
4568 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
4569}
4570
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571/**
4572 * idle_task - return the idle task for a given cpu.
4573 * @cpu: the processor in question.
4574 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004575struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576{
4577 return cpu_rq(cpu)->idle;
4578}
4579
4580/**
4581 * find_process_by_pid - find a process with a matching PID value.
4582 * @pid: the pid in question.
4583 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004584static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07004586 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587}
4588
4589/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02004590static void
4591__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592{
Ingo Molnardd41f592007-07-09 18:51:59 +02004593 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004594
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02004596 switch (p->policy) {
4597 case SCHED_NORMAL:
4598 case SCHED_BATCH:
4599 case SCHED_IDLE:
4600 p->sched_class = &fair_sched_class;
4601 break;
4602 case SCHED_FIFO:
4603 case SCHED_RR:
4604 p->sched_class = &rt_sched_class;
4605 break;
4606 }
4607
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004609 p->normal_prio = normal_prio(p);
4610 /* we are holding p->pi_lock already */
4611 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07004612 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613}
4614
4615/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004616 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 * @p: the task in question.
4618 * @policy: new policy.
4619 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004620 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004621 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004623int sched_setscheduler(struct task_struct *p, int policy,
4624 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004626 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01004628 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004629 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630
Steven Rostedt66e53932006-06-27 02:54:44 -07004631 /* may grab non-irq protected spin_locks */
4632 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633recheck:
4634 /* double check policy once rq lock held */
4635 if (policy < 0)
4636 policy = oldpolicy = p->policy;
4637 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02004638 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
4639 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08004640 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 /*
4642 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02004643 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
4644 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 */
4646 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004647 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04004648 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02004650 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 return -EINVAL;
4652
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004653 /*
4654 * Allow unprivileged RT tasks to decrease priority:
4655 */
4656 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02004657 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004658 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004659
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004660 if (!lock_task_sighand(p, &flags))
4661 return -ESRCH;
4662 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
4663 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004664
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004665 /* can't set/change the rt policy */
4666 if (policy != p->policy && !rlim_rtprio)
4667 return -EPERM;
4668
4669 /* can't increase priority */
4670 if (param->sched_priority > p->rt_priority &&
4671 param->sched_priority > rlim_rtprio)
4672 return -EPERM;
4673 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004674 /*
4675 * Like positive nice levels, dont allow tasks to
4676 * move out of SCHED_IDLE either:
4677 */
4678 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
4679 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004680
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004681 /* can't change other user's priorities */
4682 if ((current->euid != p->euid) &&
4683 (current->euid != p->uid))
4684 return -EPERM;
4685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686
Peter Zijlstrab68aa232008-02-13 15:45:40 +01004687#ifdef CONFIG_RT_GROUP_SCHED
4688 /*
4689 * Do not allow realtime tasks into groups that have no runtime
4690 * assigned.
4691 */
4692 if (rt_policy(policy) && task_group(p)->rt_runtime == 0)
4693 return -EPERM;
4694#endif
4695
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 retval = security_task_setscheduler(p, policy, param);
4697 if (retval)
4698 return retval;
4699 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07004700 * make sure no PI-waiters arrive (or leave) while we are
4701 * changing the priority of the task:
4702 */
4703 spin_lock_irqsave(&p->pi_lock, flags);
4704 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 * To be able to change p->policy safely, the apropriate
4706 * runqueue lock must be held.
4707 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07004708 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 /* recheck policy now with rq lock held */
4710 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
4711 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004712 __task_rq_unlock(rq);
4713 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 goto recheck;
4715 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02004716 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004717 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004718 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004719 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004720 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004721 if (running)
4722 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02004723
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004725 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02004726
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004727 if (running)
4728 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004729 if (on_rq) {
4730 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004731
4732 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07004734 __task_rq_unlock(rq);
4735 spin_unlock_irqrestore(&p->pi_lock, flags);
4736
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07004737 rt_mutex_adjust_pi(p);
4738
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 return 0;
4740}
4741EXPORT_SYMBOL_GPL(sched_setscheduler);
4742
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004743static int
4744do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 struct sched_param lparam;
4747 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004748 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749
4750 if (!param || pid < 0)
4751 return -EINVAL;
4752 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
4753 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004754
4755 rcu_read_lock();
4756 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004758 if (p != NULL)
4759 retval = sched_setscheduler(p, policy, &lparam);
4760 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07004761
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 return retval;
4763}
4764
4765/**
4766 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4767 * @pid: the pid in question.
4768 * @policy: new policy.
4769 * @param: structure containing the new RT priority.
4770 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004771asmlinkage long
4772sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773{
Jason Baronc21761f2006-01-18 17:43:03 -08004774 /* negative values for policy are not valid */
4775 if (policy < 0)
4776 return -EINVAL;
4777
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 return do_sched_setscheduler(pid, policy, param);
4779}
4780
4781/**
4782 * sys_sched_setparam - set/change the RT priority of a thread
4783 * @pid: the pid in question.
4784 * @param: structure containing the new RT priority.
4785 */
4786asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
4787{
4788 return do_sched_setscheduler(pid, -1, param);
4789}
4790
4791/**
4792 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4793 * @pid: the pid in question.
4794 */
4795asmlinkage long sys_sched_getscheduler(pid_t pid)
4796{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004797 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004798 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799
4800 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004801 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802
4803 retval = -ESRCH;
4804 read_lock(&tasklist_lock);
4805 p = find_process_by_pid(pid);
4806 if (p) {
4807 retval = security_task_getscheduler(p);
4808 if (!retval)
4809 retval = p->policy;
4810 }
4811 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 return retval;
4813}
4814
4815/**
4816 * sys_sched_getscheduler - get the RT priority of a thread
4817 * @pid: the pid in question.
4818 * @param: structure containing the RT priority.
4819 */
4820asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
4821{
4822 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004823 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004824 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
4826 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004827 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
4829 read_lock(&tasklist_lock);
4830 p = find_process_by_pid(pid);
4831 retval = -ESRCH;
4832 if (!p)
4833 goto out_unlock;
4834
4835 retval = security_task_getscheduler(p);
4836 if (retval)
4837 goto out_unlock;
4838
4839 lp.sched_priority = p->rt_priority;
4840 read_unlock(&tasklist_lock);
4841
4842 /*
4843 * This one might sleep, we cannot do it with a spinlock held ...
4844 */
4845 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
4846
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 return retval;
4848
4849out_unlock:
4850 read_unlock(&tasklist_lock);
4851 return retval;
4852}
4853
4854long sched_setaffinity(pid_t pid, cpumask_t new_mask)
4855{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 cpumask_t cpus_allowed;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004857 struct task_struct *p;
4858 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004860 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 read_lock(&tasklist_lock);
4862
4863 p = find_process_by_pid(pid);
4864 if (!p) {
4865 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004866 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 return -ESRCH;
4868 }
4869
4870 /*
4871 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004872 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 * usage count and then drop tasklist_lock.
4874 */
4875 get_task_struct(p);
4876 read_unlock(&tasklist_lock);
4877
4878 retval = -EPERM;
4879 if ((current->euid != p->euid) && (current->euid != p->uid) &&
4880 !capable(CAP_SYS_NICE))
4881 goto out_unlock;
4882
David Quigleye7834f82006-06-23 02:03:59 -07004883 retval = security_task_setscheduler(p, 0, NULL);
4884 if (retval)
4885 goto out_unlock;
4886
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 cpus_allowed = cpuset_cpus_allowed(p);
4888 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07004889 again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 retval = set_cpus_allowed(p, new_mask);
4891
Paul Menage8707d8b2007-10-18 23:40:22 -07004892 if (!retval) {
4893 cpus_allowed = cpuset_cpus_allowed(p);
4894 if (!cpus_subset(new_mask, cpus_allowed)) {
4895 /*
4896 * We must have raced with a concurrent cpuset
4897 * update. Just reset the cpus_allowed to the
4898 * cpuset's cpus_allowed
4899 */
4900 new_mask = cpus_allowed;
4901 goto again;
4902 }
4903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904out_unlock:
4905 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004906 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 return retval;
4908}
4909
4910static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
4911 cpumask_t *new_mask)
4912{
4913 if (len < sizeof(cpumask_t)) {
4914 memset(new_mask, 0, sizeof(cpumask_t));
4915 } else if (len > sizeof(cpumask_t)) {
4916 len = sizeof(cpumask_t);
4917 }
4918 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
4919}
4920
4921/**
4922 * sys_sched_setaffinity - set the cpu affinity of a process
4923 * @pid: pid of the process
4924 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4925 * @user_mask_ptr: user-space pointer to the new cpu mask
4926 */
4927asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
4928 unsigned long __user *user_mask_ptr)
4929{
4930 cpumask_t new_mask;
4931 int retval;
4932
4933 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
4934 if (retval)
4935 return retval;
4936
4937 return sched_setaffinity(pid, new_mask);
4938}
4939
4940/*
4941 * Represents all cpu's present in the system
4942 * In systems capable of hotplug, this map could dynamically grow
4943 * as new cpu's are detected in the system via any platform specific
4944 * method, such as ACPI for e.g.
4945 */
4946
Andi Kleen4cef0c62006-01-11 22:44:57 +01004947cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948EXPORT_SYMBOL(cpu_present_map);
4949
4950#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01004951cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07004952EXPORT_SYMBOL(cpu_online_map);
4953
Andi Kleen4cef0c62006-01-11 22:44:57 +01004954cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07004955EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956#endif
4957
4958long sched_getaffinity(pid_t pid, cpumask_t *mask)
4959{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004960 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004963 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 read_lock(&tasklist_lock);
4965
4966 retval = -ESRCH;
4967 p = find_process_by_pid(pid);
4968 if (!p)
4969 goto out_unlock;
4970
David Quigleye7834f82006-06-23 02:03:59 -07004971 retval = security_task_getscheduler(p);
4972 if (retval)
4973 goto out_unlock;
4974
Jack Steiner2f7016d2006-02-01 03:05:18 -08004975 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976
4977out_unlock:
4978 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004979 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980
Ulrich Drepper9531b622007-08-09 11:16:46 +02004981 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982}
4983
4984/**
4985 * sys_sched_getaffinity - get the cpu affinity of a process
4986 * @pid: pid of the process
4987 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4988 * @user_mask_ptr: user-space pointer to hold the current cpu mask
4989 */
4990asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
4991 unsigned long __user *user_mask_ptr)
4992{
4993 int ret;
4994 cpumask_t mask;
4995
4996 if (len < sizeof(cpumask_t))
4997 return -EINVAL;
4998
4999 ret = sched_getaffinity(pid, &mask);
5000 if (ret < 0)
5001 return ret;
5002
5003 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
5004 return -EFAULT;
5005
5006 return sizeof(cpumask_t);
5007}
5008
5009/**
5010 * sys_sched_yield - yield the current processor to other threads.
5011 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005012 * This function yields the current CPU to other tasks. If there are no
5013 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 */
5015asmlinkage long sys_sched_yield(void)
5016{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005017 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018
Ingo Molnar2d723762007-10-15 17:00:12 +02005019 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005020 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021
5022 /*
5023 * Since we are going to call schedule() anyway, there's
5024 * no need to preempt or enable interrupts:
5025 */
5026 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07005027 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 _raw_spin_unlock(&rq->lock);
5029 preempt_enable_no_resched();
5030
5031 schedule();
5032
5033 return 0;
5034}
5035
Andrew Mortone7b38402006-06-30 01:56:00 -07005036static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07005038#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
5039 __might_sleep(__FILE__, __LINE__);
5040#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005041 /*
5042 * The BKS might be reacquired before we have dropped
5043 * PREEMPT_ACTIVE, which could trigger a second
5044 * cond_resched() call.
5045 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 do {
5047 add_preempt_count(PREEMPT_ACTIVE);
5048 schedule();
5049 sub_preempt_count(PREEMPT_ACTIVE);
5050 } while (need_resched());
5051}
5052
Herbert Xu02b67cc32008-01-25 21:08:28 +01005053#if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY)
5054int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055{
Ingo Molnar94142322006-12-29 16:48:13 -08005056 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5057 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 __cond_resched();
5059 return 1;
5060 }
5061 return 0;
5062}
Herbert Xu02b67cc32008-01-25 21:08:28 +01005063EXPORT_SYMBOL(_cond_resched);
5064#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065
5066/*
5067 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5068 * call schedule, and on return reacquire the lock.
5069 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005070 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 * operations here to prevent schedule() from being called twice (once via
5072 * spin_unlock(), once by hand).
5073 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005074int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075{
Nick Piggin95c354f2008-01-30 13:31:20 +01005076 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005077 int ret = 0;
5078
Nick Piggin95c354f2008-01-30 13:31:20 +01005079 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005081 if (resched && need_resched())
5082 __cond_resched();
5083 else
5084 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005085 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005088 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090EXPORT_SYMBOL(cond_resched_lock);
5091
5092int __sched cond_resched_softirq(void)
5093{
5094 BUG_ON(!in_softirq());
5095
Ingo Molnar94142322006-12-29 16:48:13 -08005096 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07005097 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 __cond_resched();
5099 local_bh_disable();
5100 return 1;
5101 }
5102 return 0;
5103}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104EXPORT_SYMBOL(cond_resched_softirq);
5105
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106/**
5107 * yield - yield the current processor to other threads.
5108 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005109 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 * thread runnable and calls sys_sched_yield().
5111 */
5112void __sched yield(void)
5113{
5114 set_current_state(TASK_RUNNING);
5115 sys_sched_yield();
5116}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117EXPORT_SYMBOL(yield);
5118
5119/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005120 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 * that process accounting knows that this is a task in IO wait state.
5122 *
5123 * But don't do that if it is a deliberate, throttling IO wait (this task
5124 * has set its backing_dev_info: the queue against which it should throttle)
5125 */
5126void __sched io_schedule(void)
5127{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005128 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005130 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 atomic_inc(&rq->nr_iowait);
5132 schedule();
5133 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005134 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136EXPORT_SYMBOL(io_schedule);
5137
5138long __sched io_schedule_timeout(long timeout)
5139{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005140 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 long ret;
5142
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005143 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 atomic_inc(&rq->nr_iowait);
5145 ret = schedule_timeout(timeout);
5146 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005147 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 return ret;
5149}
5150
5151/**
5152 * sys_sched_get_priority_max - return maximum RT priority.
5153 * @policy: scheduling class.
5154 *
5155 * this syscall returns the maximum rt_priority that can be used
5156 * by a given scheduling class.
5157 */
5158asmlinkage long sys_sched_get_priority_max(int policy)
5159{
5160 int ret = -EINVAL;
5161
5162 switch (policy) {
5163 case SCHED_FIFO:
5164 case SCHED_RR:
5165 ret = MAX_USER_RT_PRIO-1;
5166 break;
5167 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005168 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005169 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 ret = 0;
5171 break;
5172 }
5173 return ret;
5174}
5175
5176/**
5177 * sys_sched_get_priority_min - return minimum RT priority.
5178 * @policy: scheduling class.
5179 *
5180 * this syscall returns the minimum rt_priority that can be used
5181 * by a given scheduling class.
5182 */
5183asmlinkage long sys_sched_get_priority_min(int policy)
5184{
5185 int ret = -EINVAL;
5186
5187 switch (policy) {
5188 case SCHED_FIFO:
5189 case SCHED_RR:
5190 ret = 1;
5191 break;
5192 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005193 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005194 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 ret = 0;
5196 }
5197 return ret;
5198}
5199
5200/**
5201 * sys_sched_rr_get_interval - return the default timeslice of a process.
5202 * @pid: pid of the process.
5203 * @interval: userspace pointer to the timeslice value.
5204 *
5205 * this syscall writes the default timeslice value of a given process
5206 * into the user-space timespec buffer. A value of '0' means infinity.
5207 */
5208asmlinkage
5209long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5210{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005211 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005212 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005213 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215
5216 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005217 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218
5219 retval = -ESRCH;
5220 read_lock(&tasklist_lock);
5221 p = find_process_by_pid(pid);
5222 if (!p)
5223 goto out_unlock;
5224
5225 retval = security_task_getscheduler(p);
5226 if (retval)
5227 goto out_unlock;
5228
Ingo Molnar77034932007-12-04 17:04:39 +01005229 /*
5230 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5231 * tasks that are on an otherwise idle runqueue:
5232 */
5233 time_slice = 0;
5234 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005235 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005236 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005237 struct sched_entity *se = &p->se;
5238 unsigned long flags;
5239 struct rq *rq;
5240
5241 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005242 if (rq->cfs.load.weight)
5243 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005244 task_rq_unlock(rq, &flags);
5245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005247 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005250
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251out_unlock:
5252 read_unlock(&tasklist_lock);
5253 return retval;
5254}
5255
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005256static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07005257
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005258void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005261 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005264 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005265 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005266#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005268 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005270 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271#else
5272 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005273 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005275 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276#endif
5277#ifdef CONFIG_DEBUG_STACK_USAGE
5278 {
Al Viro10ebffd2005-11-13 16:06:56 -08005279 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 while (!*n)
5281 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005282 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 }
5284#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005285 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005286 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005288 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289}
5290
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005291void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005293 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294
Ingo Molnar4bd77322007-07-11 21:21:47 +02005295#if BITS_PER_LONG == 32
5296 printk(KERN_INFO
5297 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005299 printk(KERN_INFO
5300 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301#endif
5302 read_lock(&tasklist_lock);
5303 do_each_thread(g, p) {
5304 /*
5305 * reset the NMI-timeout, listing all files on a slow
5306 * console might take alot of time:
5307 */
5308 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005309 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005310 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 } while_each_thread(g, p);
5312
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005313 touch_all_softlockup_watchdogs();
5314
Ingo Molnardd41f592007-07-09 18:51:59 +02005315#ifdef CONFIG_SCHED_DEBUG
5316 sysrq_sched_debug_show();
5317#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005319 /*
5320 * Only show locks if all tasks are dumped:
5321 */
5322 if (state_filter == -1)
5323 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324}
5325
Ingo Molnar1df21052007-07-09 18:51:58 +02005326void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5327{
Ingo Molnardd41f592007-07-09 18:51:59 +02005328 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005329}
5330
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005331/**
5332 * init_idle - set up an idle thread for a given CPU
5333 * @idle: task in question
5334 * @cpu: cpu the idle task belongs to
5335 *
5336 * NOTE: this function does not set the idle thread's NEED_RESCHED
5337 * flag, to make booting more robust.
5338 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005339void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005341 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 unsigned long flags;
5343
Ingo Molnardd41f592007-07-09 18:51:59 +02005344 __sched_fork(idle);
5345 idle->se.exec_start = sched_clock();
5346
Ingo Molnarb29739f2006-06-27 02:54:51 -07005347 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005349 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
5351 spin_lock_irqsave(&rq->lock, flags);
5352 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005353#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5354 idle->oncpu = 1;
5355#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 spin_unlock_irqrestore(&rq->lock, flags);
5357
5358 /* Set the preempt count _outside_ the spinlocks! */
Al Viroa1261f52005-11-13 16:06:55 -08005359 task_thread_info(idle)->preempt_count = 0;
Ingo Molnar6478d882008-01-25 21:08:33 +01005360
Ingo Molnardd41f592007-07-09 18:51:59 +02005361 /*
5362 * The idle tasks have their own, simple scheduling class:
5363 */
5364 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365}
5366
5367/*
5368 * In a system that switches off the HZ timer nohz_cpu_mask
5369 * indicates which cpus entered this state. This is used
5370 * in the rcu update to wait only for active cpus. For system
5371 * which do not switch off the HZ timer nohz_cpu_mask should
5372 * always be CPU_MASK_NONE.
5373 */
5374cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
5375
Ingo Molnar19978ca2007-11-09 22:39:38 +01005376/*
5377 * Increase the granularity value when there are more CPUs,
5378 * because with more CPUs the 'effective latency' as visible
5379 * to users decreases. But the relationship is not linear,
5380 * so pick a second-best guess by going with the log2 of the
5381 * number of CPUs.
5382 *
5383 * This idea comes from the SD scheduler of Con Kolivas:
5384 */
5385static inline void sched_init_granularity(void)
5386{
5387 unsigned int factor = 1 + ilog2(num_online_cpus());
5388 const unsigned long limit = 200000000;
5389
5390 sysctl_sched_min_granularity *= factor;
5391 if (sysctl_sched_min_granularity > limit)
5392 sysctl_sched_min_granularity = limit;
5393
5394 sysctl_sched_latency *= factor;
5395 if (sysctl_sched_latency > limit)
5396 sysctl_sched_latency = limit;
5397
5398 sysctl_sched_wakeup_granularity *= factor;
Ingo Molnar19978ca2007-11-09 22:39:38 +01005399}
5400
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401#ifdef CONFIG_SMP
5402/*
5403 * This is how migration works:
5404 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07005405 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 * runqueue and wake up that CPU's migration thread.
5407 * 2) we down() the locked semaphore => thread blocks.
5408 * 3) migration thread wakes up (implicitly it forces the migrated
5409 * thread off the CPU)
5410 * 4) it gets the migration request and checks whether the migrated
5411 * task is still in the wrong runqueue.
5412 * 5) if it's in the wrong runqueue then the migration thread removes
5413 * it and puts it into the right queue.
5414 * 6) migration thread up()s the semaphore.
5415 * 7) we wake up and the migration is done.
5416 */
5417
5418/*
5419 * Change a given task's CPU affinity. Migrate the thread to a
5420 * proper CPU and schedule it away if the CPU it's executing on
5421 * is removed from the allowed bitmask.
5422 *
5423 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005424 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 * call is not atomic; no spinlocks may be held.
5426 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005427int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005429 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005431 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005432 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433
5434 rq = task_rq_lock(p, &flags);
5435 if (!cpus_intersects(new_mask, cpu_online_map)) {
5436 ret = -EINVAL;
5437 goto out;
5438 }
5439
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005440 if (p->sched_class->set_cpus_allowed)
5441 p->sched_class->set_cpus_allowed(p, &new_mask);
5442 else {
Ingo Molnar0eab9142008-01-25 21:08:19 +01005443 p->cpus_allowed = new_mask;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01005444 p->rt.nr_cpus_allowed = cpus_weight(new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005445 }
5446
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 /* Can the task run on the task's current CPU? If so, we're done */
5448 if (cpu_isset(task_cpu(p), new_mask))
5449 goto out;
5450
5451 if (migrate_task(p, any_online_cpu(new_mask), &req)) {
5452 /* Need help from migration thread: drop lock and wait. */
5453 task_rq_unlock(rq, &flags);
5454 wake_up_process(rq->migration_thread);
5455 wait_for_completion(&req.done);
5456 tlb_migrate_finish(p->mm);
5457 return 0;
5458 }
5459out:
5460 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005461
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 return ret;
5463}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464EXPORT_SYMBOL_GPL(set_cpus_allowed);
5465
5466/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005467 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 * this because either it can't run here any more (set_cpus_allowed()
5469 * away from this CPU, or CPU going down), or because we're
5470 * attempting to rebalance this task on exec (sched_exec).
5471 *
5472 * So we race with normal scheduler movements, but that's OK, as long
5473 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07005474 *
5475 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07005477static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005479 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02005480 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481
5482 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07005483 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484
5485 rq_src = cpu_rq(src_cpu);
5486 rq_dest = cpu_rq(dest_cpu);
5487
5488 double_rq_lock(rq_src, rq_dest);
5489 /* Already moved. */
5490 if (task_cpu(p) != src_cpu)
5491 goto out;
5492 /* Affinity changed (again). */
5493 if (!cpu_isset(dest_cpu, p->cpus_allowed))
5494 goto out;
5495
Ingo Molnardd41f592007-07-09 18:51:59 +02005496 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005497 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005498 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005499
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005501 if (on_rq) {
5502 activate_task(rq_dest, p, 0);
5503 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07005505 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506out:
5507 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07005508 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509}
5510
5511/*
5512 * migration_thread - this is a highprio system thread that performs
5513 * thread migration by bumping thread off CPU then 'pushing' onto
5514 * another runqueue.
5515 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005516static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005519 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520
5521 rq = cpu_rq(cpu);
5522 BUG_ON(rq->migration_thread != current);
5523
5524 set_current_state(TASK_INTERRUPTIBLE);
5525 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005526 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 spin_lock_irq(&rq->lock);
5530
5531 if (cpu_is_offline(cpu)) {
5532 spin_unlock_irq(&rq->lock);
5533 goto wait_to_die;
5534 }
5535
5536 if (rq->active_balance) {
5537 active_load_balance(rq, cpu);
5538 rq->active_balance = 0;
5539 }
5540
5541 head = &rq->migration_queue;
5542
5543 if (list_empty(head)) {
5544 spin_unlock_irq(&rq->lock);
5545 schedule();
5546 set_current_state(TASK_INTERRUPTIBLE);
5547 continue;
5548 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07005549 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 list_del_init(head->next);
5551
Nick Piggin674311d2005-06-25 14:57:27 -07005552 spin_unlock(&rq->lock);
5553 __migrate_task(req->task, cpu, req->dest_cpu);
5554 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555
5556 complete(&req->done);
5557 }
5558 __set_current_state(TASK_RUNNING);
5559 return 0;
5560
5561wait_to_die:
5562 /* Wait for kthread_stop */
5563 set_current_state(TASK_INTERRUPTIBLE);
5564 while (!kthread_should_stop()) {
5565 schedule();
5566 set_current_state(TASK_INTERRUPTIBLE);
5567 }
5568 __set_current_state(TASK_RUNNING);
5569 return 0;
5570}
5571
5572#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005573
5574static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
5575{
5576 int ret;
5577
5578 local_irq_disable();
5579 ret = __migrate_task(p, src_cpu, dest_cpu);
5580 local_irq_enable();
5581 return ret;
5582}
5583
Kirill Korotaev054b9102006-12-10 02:20:11 -08005584/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02005585 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08005586 * NOTE: interrupts should be disabled by the caller
5587 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005588static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589{
Kirill Korotaevefc30812006-06-27 02:54:32 -07005590 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005592 struct rq *rq;
5593 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594
Andi Kleen3a5c3592007-10-15 17:00:14 +02005595 do {
5596 /* On same node? */
5597 mask = node_to_cpumask(cpu_to_node(dead_cpu));
5598 cpus_and(mask, mask, p->cpus_allowed);
5599 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600
Andi Kleen3a5c3592007-10-15 17:00:14 +02005601 /* On any allowed CPU? */
5602 if (dest_cpu == NR_CPUS)
5603 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604
Andi Kleen3a5c3592007-10-15 17:00:14 +02005605 /* No more Mr. Nice Guy. */
5606 if (dest_cpu == NR_CPUS) {
Cliff Wickman470fd642007-10-18 23:40:46 -07005607 cpumask_t cpus_allowed = cpuset_cpus_allowed_locked(p);
5608 /*
5609 * Try to stay on the same cpuset, where the
5610 * current cpuset may be a subset of all cpus.
5611 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005612 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07005613 * called within calls to cpuset_lock/cpuset_unlock.
5614 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02005615 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07005616 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005617 dest_cpu = any_online_cpu(p->cpus_allowed);
5618 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619
Andi Kleen3a5c3592007-10-15 17:00:14 +02005620 /*
5621 * Don't tell them about moving exiting tasks or
5622 * kernel threads (both mm NULL), since they never
5623 * leave kernel.
5624 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005625 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02005626 printk(KERN_INFO "process %d (%s) no "
5627 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005628 task_pid_nr(p), p->comm, dead_cpu);
5629 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02005630 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005631 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632}
5633
5634/*
5635 * While a dead CPU has no uninterruptible tasks queued at this point,
5636 * it might still have a nonzero ->nr_uninterruptible counter, because
5637 * for performance reasons the counter is not stricly tracking tasks to
5638 * their home CPUs. So we just add the counter to another CPU's counter,
5639 * to keep the global sum constant after CPU-down:
5640 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07005641static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005643 struct rq *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 unsigned long flags;
5645
5646 local_irq_save(flags);
5647 double_rq_lock(rq_src, rq_dest);
5648 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
5649 rq_src->nr_uninterruptible = 0;
5650 double_rq_unlock(rq_src, rq_dest);
5651 local_irq_restore(flags);
5652}
5653
5654/* Run through task list and migrate tasks from the dead cpu. */
5655static void migrate_live_tasks(int src_cpu)
5656{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005657 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005659 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660
Ingo Molnar48f24c42006-07-03 00:25:40 -07005661 do_each_thread(t, p) {
5662 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 continue;
5664
Ingo Molnar48f24c42006-07-03 00:25:40 -07005665 if (task_cpu(p) == src_cpu)
5666 move_task_off_dead_cpu(src_cpu, p);
5667 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005669 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670}
5671
Ingo Molnardd41f592007-07-09 18:51:59 +02005672/*
5673 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005674 * It does so by boosting its priority to highest possible.
5675 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 */
5677void sched_idle_next(void)
5678{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005679 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07005680 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 struct task_struct *p = rq->idle;
5682 unsigned long flags;
5683
5684 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005685 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
Ingo Molnar48f24c42006-07-03 00:25:40 -07005687 /*
5688 * Strictly not necessary since rest of the CPUs are stopped by now
5689 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 */
5691 spin_lock_irqsave(&rq->lock, flags);
5692
Ingo Molnardd41f592007-07-09 18:51:59 +02005693 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005694
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005695 update_rq_clock(rq);
5696 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
5698 spin_unlock_irqrestore(&rq->lock, flags);
5699}
5700
Ingo Molnar48f24c42006-07-03 00:25:40 -07005701/*
5702 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 * offline.
5704 */
5705void idle_task_exit(void)
5706{
5707 struct mm_struct *mm = current->active_mm;
5708
5709 BUG_ON(cpu_online(smp_processor_id()));
5710
5711 if (mm != &init_mm)
5712 switch_mm(mm, &init_mm, current);
5713 mmdrop(mm);
5714}
5715
Kirill Korotaev054b9102006-12-10 02:20:11 -08005716/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005717static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005719 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720
5721 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07005722 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723
5724 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07005725 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726
Ingo Molnar48f24c42006-07-03 00:25:40 -07005727 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728
5729 /*
5730 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005731 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 * fine.
5733 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005734 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005735 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005736 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737
Ingo Molnar48f24c42006-07-03 00:25:40 -07005738 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739}
5740
5741/* release_task() removes task from tasklist, so we won't find dead tasks. */
5742static void migrate_dead_tasks(unsigned int dead_cpu)
5743{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005744 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005745 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746
Ingo Molnardd41f592007-07-09 18:51:59 +02005747 for ( ; ; ) {
5748 if (!rq->nr_running)
5749 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02005750 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02005751 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02005752 if (!next)
5753 break;
5754 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02005755
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 }
5757}
5758#endif /* CONFIG_HOTPLUG_CPU */
5759
Nick Piggine692ab52007-07-26 13:40:43 +02005760#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5761
5762static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005763 {
5764 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005765 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005766 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01005767 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02005768};
5769
5770static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005771 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005772 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005773 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005774 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005775 .child = sd_ctl_dir,
5776 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01005777 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02005778};
5779
5780static struct ctl_table *sd_alloc_ctl_entry(int n)
5781{
5782 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02005783 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02005784
Nick Piggine692ab52007-07-26 13:40:43 +02005785 return entry;
5786}
5787
Milton Miller6382bc92007-10-15 17:00:19 +02005788static void sd_free_ctl_entry(struct ctl_table **tablep)
5789{
Milton Millercd7900762007-10-17 16:55:11 +02005790 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02005791
Milton Millercd7900762007-10-17 16:55:11 +02005792 /*
5793 * In the intermediate directories, both the child directory and
5794 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005795 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02005796 * static strings and all have proc handlers.
5797 */
5798 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02005799 if (entry->child)
5800 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02005801 if (entry->proc_handler == NULL)
5802 kfree(entry->procname);
5803 }
Milton Miller6382bc92007-10-15 17:00:19 +02005804
5805 kfree(*tablep);
5806 *tablep = NULL;
5807}
5808
Nick Piggine692ab52007-07-26 13:40:43 +02005809static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02005810set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02005811 const char *procname, void *data, int maxlen,
5812 mode_t mode, proc_handler *proc_handler)
5813{
Nick Piggine692ab52007-07-26 13:40:43 +02005814 entry->procname = procname;
5815 entry->data = data;
5816 entry->maxlen = maxlen;
5817 entry->mode = mode;
5818 entry->proc_handler = proc_handler;
5819}
5820
5821static struct ctl_table *
5822sd_alloc_ctl_domain_table(struct sched_domain *sd)
5823{
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005824 struct ctl_table *table = sd_alloc_ctl_entry(12);
Nick Piggine692ab52007-07-26 13:40:43 +02005825
Milton Millerad1cdc12007-10-15 17:00:19 +02005826 if (table == NULL)
5827 return NULL;
5828
Alexey Dobriyane0361852007-08-09 11:16:46 +02005829 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005830 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005831 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005832 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005833 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005834 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005835 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005836 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005837 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005838 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005839 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005840 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005841 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005842 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005843 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02005844 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005845 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02005846 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005847 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02005848 &sd->cache_nice_tries,
5849 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005850 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02005851 sizeof(int), 0644, proc_dointvec_minmax);
Milton Miller6323469f2007-10-15 17:00:19 +02005852 /* &table[11] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02005853
5854 return table;
5855}
5856
Ingo Molnar9a4e7152007-11-28 15:52:56 +01005857static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02005858{
5859 struct ctl_table *entry, *table;
5860 struct sched_domain *sd;
5861 int domain_num = 0, i;
5862 char buf[32];
5863
5864 for_each_domain(cpu, sd)
5865 domain_num++;
5866 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02005867 if (table == NULL)
5868 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02005869
5870 i = 0;
5871 for_each_domain(cpu, sd) {
5872 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005873 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005874 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005875 entry->child = sd_alloc_ctl_domain_table(sd);
5876 entry++;
5877 i++;
5878 }
5879 return table;
5880}
5881
5882static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02005883static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005884{
5885 int i, cpu_num = num_online_cpus();
5886 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
5887 char buf[32];
5888
Milton Miller73785472007-10-24 18:23:48 +02005889 WARN_ON(sd_ctl_dir[0].child);
5890 sd_ctl_dir[0].child = entry;
5891
Milton Millerad1cdc12007-10-15 17:00:19 +02005892 if (entry == NULL)
5893 return;
5894
Milton Miller97b6ea72007-10-15 17:00:19 +02005895 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02005896 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005897 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005898 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005899 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02005900 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02005901 }
Milton Miller73785472007-10-24 18:23:48 +02005902
5903 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02005904 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
5905}
Milton Miller6382bc92007-10-15 17:00:19 +02005906
Milton Miller73785472007-10-24 18:23:48 +02005907/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02005908static void unregister_sched_domain_sysctl(void)
5909{
Milton Miller73785472007-10-24 18:23:48 +02005910 if (sd_sysctl_header)
5911 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02005912 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02005913 if (sd_ctl_dir[0].child)
5914 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02005915}
Nick Piggine692ab52007-07-26 13:40:43 +02005916#else
Milton Miller6382bc92007-10-15 17:00:19 +02005917static void register_sched_domain_sysctl(void)
5918{
5919}
5920static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005921{
5922}
5923#endif
5924
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925/*
5926 * migration_call - callback that gets triggered when a CPU is added.
5927 * Here we can start up the necessary migration thread for the new CPU.
5928 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005929static int __cpuinit
5930migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005933 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005935 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936
5937 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005938
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005940 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02005941 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 if (IS_ERR(p))
5943 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 kthread_bind(p, cpu);
5945 /* Must be high prio: stop_machine expects to yield to it. */
5946 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02005947 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 task_rq_unlock(rq, &flags);
5949 cpu_rq(cpu)->migration_thread = p;
5950 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005951
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005953 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02005954 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005956
5957 /* Update our root-domain */
5958 rq = cpu_rq(cpu);
5959 spin_lock_irqsave(&rq->lock, flags);
5960 if (rq->rd) {
5961 BUG_ON(!cpu_isset(cpu, rq->rd->span));
5962 cpu_set(cpu, rq->rd->online);
5963 }
5964 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005966
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967#ifdef CONFIG_HOTPLUG_CPU
5968 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005969 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07005970 if (!cpu_rq(cpu)->migration_thread)
5971 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005972 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08005973 kthread_bind(cpu_rq(cpu)->migration_thread,
5974 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 kthread_stop(cpu_rq(cpu)->migration_thread);
5976 cpu_rq(cpu)->migration_thread = NULL;
5977 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005978
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005980 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07005981 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 migrate_live_tasks(cpu);
5983 rq = cpu_rq(cpu);
5984 kthread_stop(rq->migration_thread);
5985 rq->migration_thread = NULL;
5986 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07005987 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005988 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005989 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02005991 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
5992 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07005994 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07005995 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 migrate_nr_uninterruptible(rq);
5997 BUG_ON(rq->nr_running != 0);
5998
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005999 /*
6000 * No need to migrate the tasks: it was best-effort if
6001 * they didn't take sched_hotcpu_mutex. Just wake up
6002 * the requestors.
6003 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 spin_lock_irq(&rq->lock);
6005 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07006006 struct migration_req *req;
6007
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07006009 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 list_del_init(&req->list);
6011 complete(&req->done);
6012 }
6013 spin_unlock_irq(&rq->lock);
6014 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006015
Gregory Haskins08f503b2008-03-10 17:59:11 -04006016 case CPU_DYING:
6017 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01006018 /* Update our root-domain */
6019 rq = cpu_rq(cpu);
6020 spin_lock_irqsave(&rq->lock, flags);
6021 if (rq->rd) {
6022 BUG_ON(!cpu_isset(cpu, rq->rd->span));
6023 cpu_clear(cpu, rq->rd->online);
6024 }
6025 spin_unlock_irqrestore(&rq->lock, flags);
6026 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027#endif
6028 }
6029 return NOTIFY_OK;
6030}
6031
6032/* Register at highest priority so that task migration (migrate_all_tasks)
6033 * happens before everything else.
6034 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07006035static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 .notifier_call = migration_call,
6037 .priority = 10
6038};
6039
Adrian Bunke6fe6642007-11-09 22:39:39 +01006040void __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041{
6042 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006043 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006044
6045 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006046 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6047 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6049 register_cpu_notifier(&migration_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050}
6051#endif
6052
6053#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006054
6055/* Number of possible processor ids */
6056int nr_cpu_ids __read_mostly = NR_CPUS;
6057EXPORT_SYMBOL(nr_cpu_ids);
6058
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006059#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006060
6061static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level)
6062{
6063 struct sched_group *group = sd->groups;
6064 cpumask_t groupmask;
6065 char str[NR_CPUS];
6066
6067 cpumask_scnprintf(str, NR_CPUS, sd->span);
6068 cpus_clear(groupmask);
6069
6070 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6071
6072 if (!(sd->flags & SD_LOAD_BALANCE)) {
6073 printk("does not load-balance\n");
6074 if (sd->parent)
6075 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6076 " has parent");
6077 return -1;
6078 }
6079
6080 printk(KERN_CONT "span %s\n", str);
6081
6082 if (!cpu_isset(cpu, sd->span)) {
6083 printk(KERN_ERR "ERROR: domain->span does not contain "
6084 "CPU%d\n", cpu);
6085 }
6086 if (!cpu_isset(cpu, group->cpumask)) {
6087 printk(KERN_ERR "ERROR: domain->groups does not contain"
6088 " CPU%d\n", cpu);
6089 }
6090
6091 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6092 do {
6093 if (!group) {
6094 printk("\n");
6095 printk(KERN_ERR "ERROR: group is NULL\n");
6096 break;
6097 }
6098
6099 if (!group->__cpu_power) {
6100 printk(KERN_CONT "\n");
6101 printk(KERN_ERR "ERROR: domain->cpu_power not "
6102 "set\n");
6103 break;
6104 }
6105
6106 if (!cpus_weight(group->cpumask)) {
6107 printk(KERN_CONT "\n");
6108 printk(KERN_ERR "ERROR: empty group\n");
6109 break;
6110 }
6111
6112 if (cpus_intersects(groupmask, group->cpumask)) {
6113 printk(KERN_CONT "\n");
6114 printk(KERN_ERR "ERROR: repeated CPUs\n");
6115 break;
6116 }
6117
6118 cpus_or(groupmask, groupmask, group->cpumask);
6119
6120 cpumask_scnprintf(str, NR_CPUS, group->cpumask);
6121 printk(KERN_CONT " %s", str);
6122
6123 group = group->next;
6124 } while (group != sd->groups);
6125 printk(KERN_CONT "\n");
6126
6127 if (!cpus_equal(sd->span, groupmask))
6128 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6129
6130 if (sd->parent && !cpus_subset(groupmask, sd->parent->span))
6131 printk(KERN_ERR "ERROR: parent span is not a superset "
6132 "of domain->span\n");
6133 return 0;
6134}
6135
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136static void sched_domain_debug(struct sched_domain *sd, int cpu)
6137{
6138 int level = 0;
6139
Nick Piggin41c7ce92005-06-25 14:57:24 -07006140 if (!sd) {
6141 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6142 return;
6143 }
6144
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6146
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006147 for (;;) {
6148 if (sched_domain_debug_one(sd, cpu, level))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 level++;
6151 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006152 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006153 break;
6154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155}
6156#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07006157# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158#endif
6159
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006160static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006161{
6162 if (cpus_weight(sd->span) == 1)
6163 return 1;
6164
6165 /* Following flags need at least 2 groups */
6166 if (sd->flags & (SD_LOAD_BALANCE |
6167 SD_BALANCE_NEWIDLE |
6168 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006169 SD_BALANCE_EXEC |
6170 SD_SHARE_CPUPOWER |
6171 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006172 if (sd->groups != sd->groups->next)
6173 return 0;
6174 }
6175
6176 /* Following flags don't use groups */
6177 if (sd->flags & (SD_WAKE_IDLE |
6178 SD_WAKE_AFFINE |
6179 SD_WAKE_BALANCE))
6180 return 0;
6181
6182 return 1;
6183}
6184
Ingo Molnar48f24c42006-07-03 00:25:40 -07006185static int
6186sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006187{
6188 unsigned long cflags = sd->flags, pflags = parent->flags;
6189
6190 if (sd_degenerate(parent))
6191 return 1;
6192
6193 if (!cpus_equal(sd->span, parent->span))
6194 return 0;
6195
6196 /* Does parent contain flags not in child? */
6197 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6198 if (cflags & SD_WAKE_AFFINE)
6199 pflags &= ~SD_WAKE_BALANCE;
6200 /* Flags needing groups don't count if only 1 group in parent */
6201 if (parent->groups == parent->groups->next) {
6202 pflags &= ~(SD_LOAD_BALANCE |
6203 SD_BALANCE_NEWIDLE |
6204 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006205 SD_BALANCE_EXEC |
6206 SD_SHARE_CPUPOWER |
6207 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006208 }
6209 if (~cflags & pflags)
6210 return 0;
6211
6212 return 1;
6213}
6214
Gregory Haskins57d885f2008-01-25 21:08:18 +01006215static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6216{
6217 unsigned long flags;
6218 const struct sched_class *class;
6219
6220 spin_lock_irqsave(&rq->lock, flags);
6221
6222 if (rq->rd) {
6223 struct root_domain *old_rd = rq->rd;
6224
Ingo Molnar0eab9142008-01-25 21:08:19 +01006225 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006226 if (class->leave_domain)
6227 class->leave_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006228 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006229
Gregory Haskinsdc938522008-01-25 21:08:26 +01006230 cpu_clear(rq->cpu, old_rd->span);
6231 cpu_clear(rq->cpu, old_rd->online);
6232
Gregory Haskins57d885f2008-01-25 21:08:18 +01006233 if (atomic_dec_and_test(&old_rd->refcount))
6234 kfree(old_rd);
6235 }
6236
6237 atomic_inc(&rd->refcount);
6238 rq->rd = rd;
6239
Gregory Haskinsdc938522008-01-25 21:08:26 +01006240 cpu_set(rq->cpu, rd->span);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006241 if (cpu_isset(rq->cpu, cpu_online_map))
6242 cpu_set(rq->cpu, rd->online);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006243
Ingo Molnar0eab9142008-01-25 21:08:19 +01006244 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006245 if (class->join_domain)
6246 class->join_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006247 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006248
6249 spin_unlock_irqrestore(&rq->lock, flags);
6250}
6251
Gregory Haskinsdc938522008-01-25 21:08:26 +01006252static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006253{
6254 memset(rd, 0, sizeof(*rd));
6255
Gregory Haskinsdc938522008-01-25 21:08:26 +01006256 cpus_clear(rd->span);
6257 cpus_clear(rd->online);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006258}
6259
6260static void init_defrootdomain(void)
6261{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006262 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006263 atomic_set(&def_root_domain.refcount, 1);
6264}
6265
Gregory Haskinsdc938522008-01-25 21:08:26 +01006266static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006267{
6268 struct root_domain *rd;
6269
6270 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6271 if (!rd)
6272 return NULL;
6273
Gregory Haskinsdc938522008-01-25 21:08:26 +01006274 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006275
6276 return rd;
6277}
6278
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006280 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 * hold the hotplug lock.
6282 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006283static void
6284cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006286 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006287 struct sched_domain *tmp;
6288
6289 /* Remove the sched domains which do not contribute to scheduling. */
6290 for (tmp = sd; tmp; tmp = tmp->parent) {
6291 struct sched_domain *parent = tmp->parent;
6292 if (!parent)
6293 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006294 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006295 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006296 if (parent->parent)
6297 parent->parent->child = tmp;
6298 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07006299 }
6300
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006301 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006302 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006303 if (sd)
6304 sd->child = NULL;
6305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306
6307 sched_domain_debug(sd, cpu);
6308
Gregory Haskins57d885f2008-01-25 21:08:18 +01006309 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006310 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311}
6312
6313/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006314static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315
6316/* Setup the mask of cpus configured for isolated domains */
6317static int __init isolated_cpu_setup(char *str)
6318{
6319 int ints[NR_CPUS], i;
6320
6321 str = get_options(str, ARRAY_SIZE(ints), ints);
6322 cpus_clear(cpu_isolated_map);
6323 for (i = 1; i <= ints[0]; i++)
6324 if (ints[i] < NR_CPUS)
6325 cpu_set(ints[i], cpu_isolated_map);
6326 return 1;
6327}
6328
Ingo Molnar8927f492007-10-15 17:00:13 +02006329__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330
6331/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006332 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6333 * to a function which identifies what group(along with sched group) a CPU
6334 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6335 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336 *
6337 * init_sched_build_groups will build a circular linked list of the groups
6338 * covered by the given span, and will set each group's ->cpumask correctly,
6339 * and ->cpu_power to 0.
6340 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006341static void
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006342init_sched_build_groups(cpumask_t span, const cpumask_t *cpu_map,
6343 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
6344 struct sched_group **sg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345{
6346 struct sched_group *first = NULL, *last = NULL;
6347 cpumask_t covered = CPU_MASK_NONE;
6348 int i;
6349
6350 for_each_cpu_mask(i, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006351 struct sched_group *sg;
6352 int group = group_fn(i, cpu_map, &sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 int j;
6354
6355 if (cpu_isset(i, covered))
6356 continue;
6357
6358 sg->cpumask = CPU_MASK_NONE;
Eric Dumazet5517d862007-05-08 00:32:57 -07006359 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360
6361 for_each_cpu_mask(j, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006362 if (group_fn(j, cpu_map, NULL) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363 continue;
6364
6365 cpu_set(j, covered);
6366 cpu_set(j, sg->cpumask);
6367 }
6368 if (!first)
6369 first = sg;
6370 if (last)
6371 last->next = sg;
6372 last = sg;
6373 }
6374 last->next = first;
6375}
6376
John Hawkes9c1cfda2005-09-06 15:18:14 -07006377#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378
John Hawkes9c1cfda2005-09-06 15:18:14 -07006379#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006380
John Hawkes9c1cfda2005-09-06 15:18:14 -07006381/**
6382 * find_next_best_node - find the next node to include in a sched_domain
6383 * @node: node whose sched_domain we're building
6384 * @used_nodes: nodes already in the sched_domain
6385 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006386 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006387 * finds the closest node not already in the @used_nodes map.
6388 *
6389 * Should use nodemask_t.
6390 */
6391static int find_next_best_node(int node, unsigned long *used_nodes)
6392{
6393 int i, n, val, min_val, best_node = 0;
6394
6395 min_val = INT_MAX;
6396
6397 for (i = 0; i < MAX_NUMNODES; i++) {
6398 /* Start at @node */
6399 n = (node + i) % MAX_NUMNODES;
6400
6401 if (!nr_cpus_node(n))
6402 continue;
6403
6404 /* Skip already used nodes */
6405 if (test_bit(n, used_nodes))
6406 continue;
6407
6408 /* Simple min distance search */
6409 val = node_distance(node, n);
6410
6411 if (val < min_val) {
6412 min_val = val;
6413 best_node = n;
6414 }
6415 }
6416
6417 set_bit(best_node, used_nodes);
6418 return best_node;
6419}
6420
6421/**
6422 * sched_domain_node_span - get a cpumask for a node's sched_domain
6423 * @node: node whose cpumask we're constructing
6424 * @size: number of nodes to include in this span
6425 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006426 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07006427 * should be one that prevents unnecessary balancing, but also spreads tasks
6428 * out optimally.
6429 */
6430static cpumask_t sched_domain_node_span(int node)
6431{
John Hawkes9c1cfda2005-09-06 15:18:14 -07006432 DECLARE_BITMAP(used_nodes, MAX_NUMNODES);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006433 cpumask_t span, nodemask;
6434 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006435
6436 cpus_clear(span);
6437 bitmap_zero(used_nodes, MAX_NUMNODES);
6438
6439 nodemask = node_to_cpumask(node);
6440 cpus_or(span, span, nodemask);
6441 set_bit(node, used_nodes);
6442
6443 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
6444 int next_node = find_next_best_node(node, used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006445
John Hawkes9c1cfda2005-09-06 15:18:14 -07006446 nodemask = node_to_cpumask(next_node);
6447 cpus_or(span, span, nodemask);
6448 }
6449
6450 return span;
6451}
6452#endif
6453
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006454int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006455
John Hawkes9c1cfda2005-09-06 15:18:14 -07006456/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07006457 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07006458 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459#ifdef CONFIG_SCHED_SMT
6460static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006461static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006462
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006463static int
6464cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006466 if (sg)
6467 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468 return cpu;
6469}
6470#endif
6471
Ingo Molnar48f24c42006-07-03 00:25:40 -07006472/*
6473 * multi-core sched-domains:
6474 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006475#ifdef CONFIG_SCHED_MC
6476static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006477static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006478#endif
6479
6480#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006481static int
6482cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006483{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006484 int group;
Mike Travisd5a74302007-10-16 01:24:05 -07006485 cpumask_t mask = per_cpu(cpu_sibling_map, cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006486 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006487 group = first_cpu(mask);
6488 if (sg)
6489 *sg = &per_cpu(sched_group_core, group);
6490 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006491}
6492#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006493static int
6494cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006495{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006496 if (sg)
6497 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006498 return cpu;
6499}
6500#endif
6501
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006503static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006504
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006505static int
6506cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006508 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006509#ifdef CONFIG_SCHED_MC
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006510 cpumask_t mask = cpu_coregroup_map(cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006511 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006512 group = first_cpu(mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006513#elif defined(CONFIG_SCHED_SMT)
Mike Travisd5a74302007-10-16 01:24:05 -07006514 cpumask_t mask = per_cpu(cpu_sibling_map, cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006515 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006516 group = first_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006518 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006520 if (sg)
6521 *sg = &per_cpu(sched_group_phys, group);
6522 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523}
6524
6525#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07006526/*
6527 * The init_sched_build_groups can't handle what we want to do with node
6528 * groups, so roll our own. Now each node has its own list of groups which
6529 * gets dynamically allocated.
6530 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531static DEFINE_PER_CPU(struct sched_domain, node_domains);
John Hawkesd1b55132005-09-06 15:18:14 -07006532static struct sched_group **sched_group_nodes_bycpu[NR_CPUS];
John Hawkes9c1cfda2005-09-06 15:18:14 -07006533
6534static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006535static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006536
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006537static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
6538 struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006540 cpumask_t nodemask = node_to_cpumask(cpu_to_node(cpu));
6541 int group;
6542
6543 cpus_and(nodemask, nodemask, *cpu_map);
6544 group = first_cpu(nodemask);
6545
6546 if (sg)
6547 *sg = &per_cpu(sched_group_allnodes, group);
6548 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006550
Siddha, Suresh B08069032006-03-27 01:15:23 -08006551static void init_numa_sched_groups_power(struct sched_group *group_head)
6552{
6553 struct sched_group *sg = group_head;
6554 int j;
6555
6556 if (!sg)
6557 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006558 do {
6559 for_each_cpu_mask(j, sg->cpumask) {
6560 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08006561
Andi Kleen3a5c3592007-10-15 17:00:14 +02006562 sd = &per_cpu(phys_domains, j);
6563 if (j != first_cpu(sd->groups->cpumask)) {
6564 /*
6565 * Only add "power" once for each
6566 * physical package.
6567 */
6568 continue;
6569 }
6570
6571 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006572 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006573 sg = sg->next;
6574 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006575}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576#endif
6577
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006578#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006579/* Free memory allocated for various sched_group structures */
6580static void free_sched_groups(const cpumask_t *cpu_map)
6581{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006582 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006583
6584 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006585 struct sched_group **sched_group_nodes
6586 = sched_group_nodes_bycpu[cpu];
6587
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006588 if (!sched_group_nodes)
6589 continue;
6590
6591 for (i = 0; i < MAX_NUMNODES; i++) {
6592 cpumask_t nodemask = node_to_cpumask(i);
6593 struct sched_group *oldsg, *sg = sched_group_nodes[i];
6594
6595 cpus_and(nodemask, nodemask, *cpu_map);
6596 if (cpus_empty(nodemask))
6597 continue;
6598
6599 if (sg == NULL)
6600 continue;
6601 sg = sg->next;
6602next_sg:
6603 oldsg = sg;
6604 sg = sg->next;
6605 kfree(oldsg);
6606 if (oldsg != sched_group_nodes[i])
6607 goto next_sg;
6608 }
6609 kfree(sched_group_nodes);
6610 sched_group_nodes_bycpu[cpu] = NULL;
6611 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006612}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006613#else
6614static void free_sched_groups(const cpumask_t *cpu_map)
6615{
6616}
6617#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006618
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006620 * Initialize sched groups cpu_power.
6621 *
6622 * cpu_power indicates the capacity of sched group, which is used while
6623 * distributing the load between different sched groups in a sched domain.
6624 * Typically cpu_power for all the groups in a sched domain will be same unless
6625 * there are asymmetries in the topology. If there are asymmetries, group
6626 * having more cpu_power will pickup more load compared to the group having
6627 * less cpu_power.
6628 *
6629 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
6630 * the maximum number of tasks a group can handle in the presence of other idle
6631 * or lightly loaded groups in the same sched domain.
6632 */
6633static void init_sched_groups_power(int cpu, struct sched_domain *sd)
6634{
6635 struct sched_domain *child;
6636 struct sched_group *group;
6637
6638 WARN_ON(!sd || !sd->groups);
6639
6640 if (cpu != first_cpu(sd->groups->cpumask))
6641 return;
6642
6643 child = sd->child;
6644
Eric Dumazet5517d862007-05-08 00:32:57 -07006645 sd->groups->__cpu_power = 0;
6646
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006647 /*
6648 * For perf policy, if the groups in child domain share resources
6649 * (for example cores sharing some portions of the cache hierarchy
6650 * or SMT), then set this domain groups cpu_power such that each group
6651 * can handle only one task, when there are other idle groups in the
6652 * same sched domain.
6653 */
6654 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
6655 (child->flags &
6656 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07006657 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006658 return;
6659 }
6660
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006661 /*
6662 * add cpu_power of each child group to this groups cpu_power
6663 */
6664 group = child->groups;
6665 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07006666 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006667 group = group->next;
6668 } while (group != child->groups);
6669}
6670
6671/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006672 * Build sched domains for a given set of cpus and attach the sched domains
6673 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006675static int build_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676{
6677 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006678 struct root_domain *rd;
John Hawkesd1b55132005-09-06 15:18:14 -07006679#ifdef CONFIG_NUMA
6680 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006681 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07006682
6683 /*
6684 * Allocate the per-node list of sched groups
6685 */
Milton Miller5cf9f062007-10-15 17:00:19 +02006686 sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006687 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07006688 if (!sched_group_nodes) {
6689 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006690 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07006691 }
6692 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
6693#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694
Gregory Haskinsdc938522008-01-25 21:08:26 +01006695 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01006696 if (!rd) {
6697 printk(KERN_WARNING "Cannot alloc root domain\n");
6698 return -ENOMEM;
6699 }
6700
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006702 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006704 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705 struct sched_domain *sd = NULL, *p;
6706 cpumask_t nodemask = node_to_cpumask(cpu_to_node(i));
6707
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006708 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709
6710#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02006711 if (cpus_weight(*cpu_map) >
6712 SD_NODES_PER_DOMAIN*cpus_weight(nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07006713 sd = &per_cpu(allnodes_domains, i);
6714 *sd = SD_ALLNODES_INIT;
6715 sd->span = *cpu_map;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006716 cpu_to_allnodes_group(i, cpu_map, &sd->groups);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006717 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006718 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006719 } else
6720 p = NULL;
6721
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722 sd = &per_cpu(node_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723 *sd = SD_NODE_INIT;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006724 sd->span = sched_domain_node_span(cpu_to_node(i));
6725 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006726 if (p)
6727 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006728 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729#endif
6730
6731 p = sd;
6732 sd = &per_cpu(phys_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733 *sd = SD_CPU_INIT;
6734 sd->span = nodemask;
6735 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006736 if (p)
6737 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006738 cpu_to_phys_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006740#ifdef CONFIG_SCHED_MC
6741 p = sd;
6742 sd = &per_cpu(core_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006743 *sd = SD_MC_INIT;
6744 sd->span = cpu_coregroup_map(i);
6745 cpus_and(sd->span, sd->span, *cpu_map);
6746 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006747 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006748 cpu_to_core_group(i, cpu_map, &sd->groups);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006749#endif
6750
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751#ifdef CONFIG_SCHED_SMT
6752 p = sd;
6753 sd = &per_cpu(cpu_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 *sd = SD_SIBLING_INIT;
Mike Travisd5a74302007-10-16 01:24:05 -07006755 sd->span = per_cpu(cpu_sibling_map, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006756 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006758 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006759 cpu_to_cpu_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760#endif
6761 }
6762
6763#ifdef CONFIG_SCHED_SMT
6764 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07006765 for_each_cpu_mask(i, *cpu_map) {
Mike Travisd5a74302007-10-16 01:24:05 -07006766 cpumask_t this_sibling_map = per_cpu(cpu_sibling_map, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006767 cpus_and(this_sibling_map, this_sibling_map, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 if (i != first_cpu(this_sibling_map))
6769 continue;
6770
Ingo Molnardd41f592007-07-09 18:51:59 +02006771 init_sched_build_groups(this_sibling_map, cpu_map,
6772 &cpu_to_cpu_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773 }
6774#endif
6775
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006776#ifdef CONFIG_SCHED_MC
6777 /* Set up multi-core groups */
6778 for_each_cpu_mask(i, *cpu_map) {
6779 cpumask_t this_core_map = cpu_coregroup_map(i);
6780 cpus_and(this_core_map, this_core_map, *cpu_map);
6781 if (i != first_cpu(this_core_map))
6782 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02006783 init_sched_build_groups(this_core_map, cpu_map,
6784 &cpu_to_core_group);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006785 }
6786#endif
6787
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788 /* Set up physical groups */
6789 for (i = 0; i < MAX_NUMNODES; i++) {
6790 cpumask_t nodemask = node_to_cpumask(i);
6791
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006792 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793 if (cpus_empty(nodemask))
6794 continue;
6795
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006796 init_sched_build_groups(nodemask, cpu_map, &cpu_to_phys_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797 }
6798
6799#ifdef CONFIG_NUMA
6800 /* Set up node groups */
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006801 if (sd_allnodes)
Ingo Molnardd41f592007-07-09 18:51:59 +02006802 init_sched_build_groups(*cpu_map, cpu_map,
6803 &cpu_to_allnodes_group);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006804
6805 for (i = 0; i < MAX_NUMNODES; i++) {
6806 /* Set up node groups */
6807 struct sched_group *sg, *prev;
6808 cpumask_t nodemask = node_to_cpumask(i);
6809 cpumask_t domainspan;
6810 cpumask_t covered = CPU_MASK_NONE;
6811 int j;
6812
6813 cpus_and(nodemask, nodemask, *cpu_map);
John Hawkesd1b55132005-09-06 15:18:14 -07006814 if (cpus_empty(nodemask)) {
6815 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006816 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07006817 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006818
6819 domainspan = sched_domain_node_span(i);
6820 cpus_and(domainspan, domainspan, *cpu_map);
6821
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006822 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006823 if (!sg) {
6824 printk(KERN_WARNING "Can not alloc domain group for "
6825 "node %d\n", i);
6826 goto error;
6827 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006828 sched_group_nodes[i] = sg;
6829 for_each_cpu_mask(j, nodemask) {
6830 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02006831
John Hawkes9c1cfda2005-09-06 15:18:14 -07006832 sd = &per_cpu(node_domains, j);
6833 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006834 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006835 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006836 sg->cpumask = nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006837 sg->next = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006838 cpus_or(covered, covered, nodemask);
6839 prev = sg;
6840
6841 for (j = 0; j < MAX_NUMNODES; j++) {
6842 cpumask_t tmp, notcovered;
6843 int n = (i + j) % MAX_NUMNODES;
6844
6845 cpus_complement(notcovered, covered);
6846 cpus_and(tmp, notcovered, *cpu_map);
6847 cpus_and(tmp, tmp, domainspan);
6848 if (cpus_empty(tmp))
6849 break;
6850
6851 nodemask = node_to_cpumask(n);
6852 cpus_and(tmp, tmp, nodemask);
6853 if (cpus_empty(tmp))
6854 continue;
6855
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006856 sg = kmalloc_node(sizeof(struct sched_group),
6857 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006858 if (!sg) {
6859 printk(KERN_WARNING
6860 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006861 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006862 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006863 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006864 sg->cpumask = tmp;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006865 sg->next = prev->next;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006866 cpus_or(covered, covered, tmp);
6867 prev->next = sg;
6868 prev = sg;
6869 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871#endif
6872
6873 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006874#ifdef CONFIG_SCHED_SMT
6875 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006876 struct sched_domain *sd = &per_cpu(cpu_domains, i);
6877
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006878 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006879 }
6880#endif
6881#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006882 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006883 struct sched_domain *sd = &per_cpu(core_domains, i);
6884
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006885 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006886 }
6887#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006889 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006890 struct sched_domain *sd = &per_cpu(phys_domains, i);
6891
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006892 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893 }
6894
John Hawkes9c1cfda2005-09-06 15:18:14 -07006895#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08006896 for (i = 0; i < MAX_NUMNODES; i++)
6897 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006898
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006899 if (sd_allnodes) {
6900 struct sched_group *sg;
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07006901
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006902 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg);
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07006903 init_numa_sched_groups_power(sg);
6904 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006905#endif
6906
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006908 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909 struct sched_domain *sd;
6910#ifdef CONFIG_SCHED_SMT
6911 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006912#elif defined(CONFIG_SCHED_MC)
6913 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914#else
6915 sd = &per_cpu(phys_domains, i);
6916#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01006917 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006919
6920 return 0;
6921
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006922#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006923error:
6924 free_sched_groups(cpu_map);
6925 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006926#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927}
Paul Jackson029190c2007-10-18 23:40:20 -07006928
6929static cpumask_t *doms_cur; /* current sched domains */
6930static int ndoms_cur; /* number of sched domains in 'doms_cur' */
6931
6932/*
6933 * Special case: If a kmalloc of a doms_cur partition (array of
6934 * cpumask_t) fails, then fallback to a single sched domain,
6935 * as determined by the single cpumask_t fallback_doms.
6936 */
6937static cpumask_t fallback_doms;
6938
Heiko Carstens22e52b02008-03-12 18:31:59 +01006939void __attribute__((weak)) arch_update_cpu_topology(void)
6940{
6941}
6942
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006943/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006944 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07006945 * For now this just excludes isolated cpus, but could be used to
6946 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006947 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006948static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006949{
Milton Miller73785472007-10-24 18:23:48 +02006950 int err;
6951
Heiko Carstens22e52b02008-03-12 18:31:59 +01006952 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07006953 ndoms_cur = 1;
6954 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6955 if (!doms_cur)
6956 doms_cur = &fallback_doms;
6957 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Milton Miller73785472007-10-24 18:23:48 +02006958 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02006959 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02006960
6961 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006962}
6963
6964static void arch_destroy_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965{
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006966 free_sched_groups(cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006967}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006969/*
6970 * Detach sched domains from a group of cpus specified in cpu_map
6971 * These cpus will now be attached to the NULL domain
6972 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08006973static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006974{
6975 int i;
6976
Milton Miller6382bc92007-10-15 17:00:19 +02006977 unregister_sched_domain_sysctl();
6978
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006979 for_each_cpu_mask(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006980 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006981 synchronize_sched();
6982 arch_destroy_sched_domains(cpu_map);
6983}
6984
Paul Jackson029190c2007-10-18 23:40:20 -07006985/*
6986 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006987 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07006988 * doms_new[] to the current sched domain partitioning, doms_cur[].
6989 * It destroys each deleted domain and builds each new domain.
6990 *
6991 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006992 * The masks don't intersect (don't overlap.) We should setup one
6993 * sched domain for each mask. CPUs not in any of the cpumasks will
6994 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07006995 * current 'doms_cur' domains and in the new 'doms_new', we can leave
6996 * it as it is.
6997 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006998 * The passed in 'doms_new' should be kmalloc'd. This routine takes
6999 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07007000 * failed the kmalloc call, then it can pass in doms_new == NULL,
7001 * and partition_sched_domains() will fallback to the single partition
7002 * 'fallback_doms'.
7003 *
7004 * Call with hotplug lock held
7005 */
7006void partition_sched_domains(int ndoms_new, cpumask_t *doms_new)
7007{
7008 int i, j;
7009
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007010 lock_doms_cur();
7011
Milton Miller73785472007-10-24 18:23:48 +02007012 /* always unregister in case we don't destroy any domains */
7013 unregister_sched_domain_sysctl();
7014
Paul Jackson029190c2007-10-18 23:40:20 -07007015 if (doms_new == NULL) {
7016 ndoms_new = 1;
7017 doms_new = &fallback_doms;
7018 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
7019 }
7020
7021 /* Destroy deleted domains */
7022 for (i = 0; i < ndoms_cur; i++) {
7023 for (j = 0; j < ndoms_new; j++) {
7024 if (cpus_equal(doms_cur[i], doms_new[j]))
7025 goto match1;
7026 }
7027 /* no match - a current sched domain not in new doms_new[] */
7028 detach_destroy_domains(doms_cur + i);
7029match1:
7030 ;
7031 }
7032
7033 /* Build new domains */
7034 for (i = 0; i < ndoms_new; i++) {
7035 for (j = 0; j < ndoms_cur; j++) {
7036 if (cpus_equal(doms_new[i], doms_cur[j]))
7037 goto match2;
7038 }
7039 /* no match - add a new doms_new */
7040 build_sched_domains(doms_new + i);
7041match2:
7042 ;
7043 }
7044
7045 /* Remember the new sched domains */
7046 if (doms_cur != &fallback_doms)
7047 kfree(doms_cur);
7048 doms_cur = doms_new;
7049 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007050
7051 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007052
7053 unlock_doms_cur();
Paul Jackson029190c2007-10-18 23:40:20 -07007054}
7055
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007056#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007057int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007058{
7059 int err;
7060
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007061 get_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007062 detach_destroy_domains(&cpu_online_map);
7063 err = arch_init_sched_domains(&cpu_online_map);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007064 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007065
7066 return err;
7067}
7068
7069static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7070{
7071 int ret;
7072
7073 if (buf[0] != '0' && buf[0] != '1')
7074 return -EINVAL;
7075
7076 if (smt)
7077 sched_smt_power_savings = (buf[0] == '1');
7078 else
7079 sched_mc_power_savings = (buf[0] == '1');
7080
7081 ret = arch_reinit_sched_domains();
7082
7083 return ret ? ret : count;
7084}
7085
Adrian Bunk6707de002007-08-12 18:08:19 +02007086#ifdef CONFIG_SCHED_MC
7087static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
7088{
7089 return sprintf(page, "%u\n", sched_mc_power_savings);
7090}
7091static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
7092 const char *buf, size_t count)
7093{
7094 return sched_power_savings_store(buf, count, 0);
7095}
7096static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
7097 sched_mc_power_savings_store);
7098#endif
7099
7100#ifdef CONFIG_SCHED_SMT
7101static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
7102{
7103 return sprintf(page, "%u\n", sched_smt_power_savings);
7104}
7105static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
7106 const char *buf, size_t count)
7107{
7108 return sched_power_savings_store(buf, count, 1);
7109}
7110static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
7111 sched_smt_power_savings_store);
7112#endif
7113
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007114int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7115{
7116 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007117
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007118#ifdef CONFIG_SCHED_SMT
7119 if (smt_capable())
7120 err = sysfs_create_file(&cls->kset.kobj,
7121 &attr_sched_smt_power_savings.attr);
7122#endif
7123#ifdef CONFIG_SCHED_MC
7124 if (!err && mc_capable())
7125 err = sysfs_create_file(&cls->kset.kobj,
7126 &attr_sched_mc_power_savings.attr);
7127#endif
7128 return err;
7129}
7130#endif
7131
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007133 * Force a reinitialization of the sched domains hierarchy. The domains
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07007135 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 * which will prevent rebalancing while the sched domains are recalculated.
7137 */
7138static int update_sched_domains(struct notifier_block *nfb,
7139 unsigned long action, void *hcpu)
7140{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 switch (action) {
7142 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007143 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007145 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007146 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 return NOTIFY_OK;
7148
7149 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007150 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007152 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007154 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007156 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157 /*
7158 * Fall through and re-initialise the domains.
7159 */
7160 break;
7161 default:
7162 return NOTIFY_DONE;
7163 }
7164
7165 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007166 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167
7168 return NOTIFY_OK;
7169}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170
7171void __init sched_init_smp(void)
7172{
Nick Piggin5c1e1762006-10-03 01:14:04 -07007173 cpumask_t non_isolated_cpus;
7174
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007175 get_online_cpus();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007176 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08007177 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007178 if (cpus_empty(non_isolated_cpus))
7179 cpu_set(smp_processor_id(), non_isolated_cpus);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007180 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181 /* XXX: Theoretical race here - CPU may be hotplugged now */
7182 hotcpu_notifier(update_sched_domains, 0);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007183
7184 /* Move init over to a non-isolated CPU */
7185 if (set_cpus_allowed(current, non_isolated_cpus) < 0)
7186 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007187 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188}
7189#else
7190void __init sched_init_smp(void)
7191{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007192 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193}
7194#endif /* CONFIG_SMP */
7195
7196int in_sched_functions(unsigned long addr)
7197{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198 return in_lock_functions(addr) ||
7199 (addr >= (unsigned long)__sched_text_start
7200 && addr < (unsigned long)__sched_text_end);
7201}
7202
Alexey Dobriyana9957442007-10-15 17:00:13 +02007203static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007204{
7205 cfs_rq->tasks_timeline = RB_ROOT;
Ingo Molnardd41f592007-07-09 18:51:59 +02007206#ifdef CONFIG_FAIR_GROUP_SCHED
7207 cfs_rq->rq = rq;
7208#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007209 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02007210}
7211
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007212static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7213{
7214 struct rt_prio_array *array;
7215 int i;
7216
7217 array = &rt_rq->active;
7218 for (i = 0; i < MAX_RT_PRIO; i++) {
7219 INIT_LIST_HEAD(array->queue + i);
7220 __clear_bit(i, array->bitmap);
7221 }
7222 /* delimiter for bitsearch: */
7223 __set_bit(MAX_RT_PRIO, array->bitmap);
7224
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007225#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007226 rt_rq->highest_prio = MAX_RT_PRIO;
7227#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007228#ifdef CONFIG_SMP
7229 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007230 rt_rq->overloaded = 0;
7231#endif
7232
7233 rt_rq->rt_time = 0;
7234 rt_rq->rt_throttled = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007235
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007236#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01007237 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007238 rt_rq->rq = rq;
7239#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007240}
7241
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007242#ifdef CONFIG_FAIR_GROUP_SCHED
7243static void init_tg_cfs_entry(struct rq *rq, struct task_group *tg,
7244 struct cfs_rq *cfs_rq, struct sched_entity *se,
7245 int cpu, int add)
7246{
7247 tg->cfs_rq[cpu] = cfs_rq;
7248 init_cfs_rq(cfs_rq, rq);
7249 cfs_rq->tg = tg;
7250 if (add)
7251 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7252
7253 tg->se[cpu] = se;
7254 se->cfs_rq = &rq->cfs;
7255 se->my_q = cfs_rq;
7256 se->load.weight = tg->shares;
7257 se->load.inv_weight = div64_64(1ULL<<32, se->load.weight);
7258 se->parent = NULL;
7259}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007260#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007261
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007262#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007263static void init_tg_rt_entry(struct rq *rq, struct task_group *tg,
7264 struct rt_rq *rt_rq, struct sched_rt_entity *rt_se,
7265 int cpu, int add)
7266{
7267 tg->rt_rq[cpu] = rt_rq;
7268 init_rt_rq(rt_rq, rq);
7269 rt_rq->tg = tg;
7270 rt_rq->rt_se = rt_se;
7271 if (add)
7272 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
7273
7274 tg->rt_se[cpu] = rt_se;
7275 rt_se->rt_rq = &rq->rt;
7276 rt_se->my_q = rt_rq;
7277 rt_se->parent = NULL;
7278 INIT_LIST_HEAD(&rt_se->run_list);
7279}
7280#endif
7281
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282void __init sched_init(void)
7283{
Christoph Lameter476f3532007-05-06 14:48:58 -07007284 int highest_cpu = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007285 int i, j;
7286
Gregory Haskins57d885f2008-01-25 21:08:18 +01007287#ifdef CONFIG_SMP
7288 init_defrootdomain();
7289#endif
7290
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007291#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007292 list_add(&init_task_group.list, &task_groups);
7293#endif
7294
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08007295 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07007296 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007297
7298 rq = cpu_rq(i);
7299 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07007300 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07007301 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007302 rq->clock = 1;
Guillaume Chazarain15934a32008-04-19 19:44:57 +02007303 update_last_tick_seen(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02007304 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007305 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007306#ifdef CONFIG_FAIR_GROUP_SCHED
7307 init_task_group.shares = init_task_group_load;
7308 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
7309 init_tg_cfs_entry(rq, &init_task_group,
7310 &per_cpu(init_cfs_rq, i),
7311 &per_cpu(init_sched_entity, i), i, 1);
7312
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007313#endif
7314#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007315 init_task_group.rt_runtime =
7316 sysctl_sched_rt_runtime * NSEC_PER_USEC;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007317 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
7318 init_tg_rt_entry(rq, &init_task_group,
7319 &per_cpu(init_rt_rq, i),
7320 &per_cpu(init_sched_rt_entity, i), i, 1);
7321#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007322 rq->rt_period_expire = 0;
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007323 rq->rt_throttled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324
Ingo Molnardd41f592007-07-09 18:51:59 +02007325 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
7326 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07007328 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007329 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007330 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007331 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07007333 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334 rq->migration_thread = NULL;
7335 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01007336 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007337#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007338 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007339 atomic_set(&rq->nr_iowait, 0);
Christoph Lameter476f3532007-05-06 14:48:58 -07007340 highest_cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341 }
7342
Peter Williams2dd73a42006-06-27 02:54:34 -07007343 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007344
Avi Kivitye107be32007-07-26 13:40:43 +02007345#ifdef CONFIG_PREEMPT_NOTIFIERS
7346 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
7347#endif
7348
Christoph Lameterc9819f42006-12-10 02:20:25 -08007349#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07007350 nr_cpu_ids = highest_cpu + 1;
Christoph Lameterc9819f42006-12-10 02:20:25 -08007351 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
7352#endif
7353
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007354#ifdef CONFIG_RT_MUTEXES
7355 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
7356#endif
7357
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358 /*
7359 * The boot idle thread does lazy MMU switching as well:
7360 */
7361 atomic_inc(&init_mm.mm_count);
7362 enter_lazy_tlb(&init_mm, current);
7363
7364 /*
7365 * Make us the idle thread. Technically, schedule() should not be
7366 * called from this thread, however somewhere below it might be,
7367 * but because we are the idle thread, we just pick up running again
7368 * when this runqueue becomes "idle".
7369 */
7370 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02007371 /*
7372 * During early bootup we pretend to be a normal task:
7373 */
7374 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01007375
7376 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377}
7378
7379#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
7380void __might_sleep(char *file, int line)
7381{
Ingo Molnar48f24c42006-07-03 00:25:40 -07007382#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383 static unsigned long prev_jiffy; /* ratelimiting */
7384
7385 if ((in_atomic() || irqs_disabled()) &&
7386 system_state == SYSTEM_RUNNING && !oops_in_progress) {
7387 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
7388 return;
7389 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08007390 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391 " context at %s:%d\n", file, line);
7392 printk("in_atomic():%d, irqs_disabled():%d\n",
7393 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08007394 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08007395 if (irqs_disabled())
7396 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397 dump_stack();
7398 }
7399#endif
7400}
7401EXPORT_SYMBOL(__might_sleep);
7402#endif
7403
7404#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007405static void normalize_task(struct rq *rq, struct task_struct *p)
7406{
7407 int on_rq;
7408 update_rq_clock(rq);
7409 on_rq = p->se.on_rq;
7410 if (on_rq)
7411 deactivate_task(rq, p, 0);
7412 __setscheduler(rq, p, SCHED_NORMAL, 0);
7413 if (on_rq) {
7414 activate_task(rq, p, 0);
7415 resched_task(rq->curr);
7416 }
7417}
7418
Linus Torvalds1da177e2005-04-16 15:20:36 -07007419void normalize_rt_tasks(void)
7420{
Ingo Molnara0f98a12007-06-17 18:37:45 +02007421 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007422 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07007423 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007424
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007425 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007426 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02007427 /*
7428 * Only normalize user tasks:
7429 */
7430 if (!p->mm)
7431 continue;
7432
Ingo Molnardd41f592007-07-09 18:51:59 +02007433 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007434#ifdef CONFIG_SCHEDSTATS
7435 p->se.wait_start = 0;
7436 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007437 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007438#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02007439 task_rq(p)->clock = 0;
7440
7441 if (!rt_task(p)) {
7442 /*
7443 * Renice negative nice level userspace
7444 * tasks back to 0:
7445 */
7446 if (TASK_NICE(p) < 0 && p->mm)
7447 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02007449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007450
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007451 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07007452 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453
Ingo Molnar178be792007-10-15 17:00:18 +02007454 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007455
Ingo Molnarb29739f2006-06-27 02:54:51 -07007456 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007457 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007458 } while_each_thread(g, p);
7459
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007460 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461}
7462
7463#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07007464
7465#ifdef CONFIG_IA64
7466/*
7467 * These functions are only useful for the IA64 MCA handling.
7468 *
7469 * They can only be called when the whole system has been
7470 * stopped - every CPU needs to be quiescent, and no scheduling
7471 * activity can take place. Using them for anything else would
7472 * be a serious bug, and as a result, they aren't even visible
7473 * under any other configuration.
7474 */
7475
7476/**
7477 * curr_task - return the current task for a given cpu.
7478 * @cpu: the processor in question.
7479 *
7480 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7481 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007482struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007483{
7484 return cpu_curr(cpu);
7485}
7486
7487/**
7488 * set_curr_task - set the current task for a given cpu.
7489 * @cpu: the processor in question.
7490 * @p: the task pointer to set.
7491 *
7492 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007493 * are serviced on a separate stack. It allows the architecture to switch the
7494 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07007495 * must be called with all CPU's synchronized, and interrupts disabled, the
7496 * and caller must save the original value of the current task (see
7497 * curr_task() above) and restore that value before reenabling interrupts and
7498 * re-starting the system.
7499 *
7500 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7501 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007502void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007503{
7504 cpu_curr(cpu) = p;
7505}
7506
7507#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007508
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007509#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007510
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007511#ifdef CONFIG_FAIR_GROUP_SCHED
7512static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007513{
7514 int i;
7515
7516 for_each_possible_cpu(i) {
7517 if (tg->cfs_rq)
7518 kfree(tg->cfs_rq[i]);
7519 if (tg->se)
7520 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007521 }
7522
7523 kfree(tg->cfs_rq);
7524 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007525}
7526
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007527static int alloc_fair_sched_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007528{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007529 struct cfs_rq *cfs_rq;
7530 struct sched_entity *se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007531 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007532 int i;
7533
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007534 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * NR_CPUS, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007535 if (!tg->cfs_rq)
7536 goto err;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007537 tg->se = kzalloc(sizeof(se) * NR_CPUS, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007538 if (!tg->se)
7539 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007540
7541 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007542
7543 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007544 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007545
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007546 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
7547 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007548 if (!cfs_rq)
7549 goto err;
7550
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007551 se = kmalloc_node(sizeof(struct sched_entity),
7552 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007553 if (!se)
7554 goto err;
7555
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007556 init_tg_cfs_entry(rq, tg, cfs_rq, se, i, 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007557 }
7558
7559 return 1;
7560
7561 err:
7562 return 0;
7563}
7564
7565static inline void register_fair_sched_group(struct task_group *tg, int cpu)
7566{
7567 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
7568 &cpu_rq(cpu)->leaf_cfs_rq_list);
7569}
7570
7571static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
7572{
7573 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
7574}
7575#else
7576static inline void free_fair_sched_group(struct task_group *tg)
7577{
7578}
7579
7580static inline int alloc_fair_sched_group(struct task_group *tg)
7581{
7582 return 1;
7583}
7584
7585static inline void register_fair_sched_group(struct task_group *tg, int cpu)
7586{
7587}
7588
7589static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
7590{
7591}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007592#endif
7593
7594#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007595static void free_rt_sched_group(struct task_group *tg)
7596{
7597 int i;
7598
7599 for_each_possible_cpu(i) {
7600 if (tg->rt_rq)
7601 kfree(tg->rt_rq[i]);
7602 if (tg->rt_se)
7603 kfree(tg->rt_se[i]);
7604 }
7605
7606 kfree(tg->rt_rq);
7607 kfree(tg->rt_se);
7608}
7609
7610static int alloc_rt_sched_group(struct task_group *tg)
7611{
7612 struct rt_rq *rt_rq;
7613 struct sched_rt_entity *rt_se;
7614 struct rq *rq;
7615 int i;
7616
7617 tg->rt_rq = kzalloc(sizeof(rt_rq) * NR_CPUS, GFP_KERNEL);
7618 if (!tg->rt_rq)
7619 goto err;
7620 tg->rt_se = kzalloc(sizeof(rt_se) * NR_CPUS, GFP_KERNEL);
7621 if (!tg->rt_se)
7622 goto err;
7623
7624 tg->rt_runtime = 0;
7625
7626 for_each_possible_cpu(i) {
7627 rq = cpu_rq(i);
7628
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007629 rt_rq = kmalloc_node(sizeof(struct rt_rq),
7630 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
7631 if (!rt_rq)
7632 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007633
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007634 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
7635 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
7636 if (!rt_se)
7637 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007638
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007639 init_tg_rt_entry(rq, tg, rt_rq, rt_se, i, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007640 }
7641
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007642 return 1;
7643
7644 err:
7645 return 0;
7646}
7647
7648static inline void register_rt_sched_group(struct task_group *tg, int cpu)
7649{
7650 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
7651 &cpu_rq(cpu)->leaf_rt_rq_list);
7652}
7653
7654static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
7655{
7656 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
7657}
7658#else
7659static inline void free_rt_sched_group(struct task_group *tg)
7660{
7661}
7662
7663static inline int alloc_rt_sched_group(struct task_group *tg)
7664{
7665 return 1;
7666}
7667
7668static inline void register_rt_sched_group(struct task_group *tg, int cpu)
7669{
7670}
7671
7672static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
7673{
7674}
7675#endif
7676
7677static void free_sched_group(struct task_group *tg)
7678{
7679 free_fair_sched_group(tg);
7680 free_rt_sched_group(tg);
7681 kfree(tg);
7682}
7683
7684/* allocate runqueue etc for a new task group */
7685struct task_group *sched_create_group(void)
7686{
7687 struct task_group *tg;
7688 unsigned long flags;
7689 int i;
7690
7691 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
7692 if (!tg)
7693 return ERR_PTR(-ENOMEM);
7694
7695 if (!alloc_fair_sched_group(tg))
7696 goto err;
7697
7698 if (!alloc_rt_sched_group(tg))
7699 goto err;
7700
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007701 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007702 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007703 register_fair_sched_group(tg, i);
7704 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007705 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007706 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007707 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007708
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007709 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007710
7711err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007712 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007713 return ERR_PTR(-ENOMEM);
7714}
7715
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007716/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007717static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007718{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007719 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007720 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007721}
7722
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007723/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007724void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007725{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007726 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007727 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007728
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007729 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007730 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007731 unregister_fair_sched_group(tg, i);
7732 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007733 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007734 list_del_rcu(&tg->list);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007735 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007736
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007737 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007738 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007739}
7740
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007741/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02007742 * The caller of this function should have put the task in its new group
7743 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
7744 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007745 */
7746void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007747{
7748 int on_rq, running;
7749 unsigned long flags;
7750 struct rq *rq;
7751
7752 rq = task_rq_lock(tsk, &flags);
7753
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007754 update_rq_clock(rq);
7755
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01007756 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007757 on_rq = tsk->se.on_rq;
7758
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007759 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007760 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007761 if (unlikely(running))
7762 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007763
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007764 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007765
Peter Zijlstra810b3812008-02-29 15:21:01 -05007766#ifdef CONFIG_FAIR_GROUP_SCHED
7767 if (tsk->sched_class->moved_group)
7768 tsk->sched_class->moved_group(tsk);
7769#endif
7770
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007771 if (unlikely(running))
7772 tsk->sched_class->set_curr_task(rq);
7773 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02007774 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007775
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007776 task_rq_unlock(rq, &flags);
7777}
7778
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007779#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007780static void set_se_shares(struct sched_entity *se, unsigned long shares)
7781{
7782 struct cfs_rq *cfs_rq = se->cfs_rq;
7783 struct rq *rq = cfs_rq->rq;
7784 int on_rq;
7785
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007786 spin_lock_irq(&rq->lock);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007787
7788 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007789 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007790 dequeue_entity(cfs_rq, se, 0);
7791
7792 se->load.weight = shares;
7793 se->load.inv_weight = div64_64((1ULL<<32), shares);
7794
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007795 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007796 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007797
7798 spin_unlock_irq(&rq->lock);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007799}
7800
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007801static DEFINE_MUTEX(shares_mutex);
7802
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007803int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007804{
7805 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007806 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01007807
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007808 /*
7809 * A weight of 0 or 1 can cause arithmetics problems.
7810 * (The default weight is 1024 - so there's no practical
7811 * limitation from this.)
7812 */
7813 if (shares < 2)
7814 shares = 2;
7815
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007816 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007817 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007818 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007819
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007820 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007821 for_each_possible_cpu(i)
7822 unregister_fair_sched_group(tg, i);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007823 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007824
7825 /* wait for any ongoing reference to this group to finish */
7826 synchronize_sched();
7827
7828 /*
7829 * Now we are free to modify the group's share on each cpu
7830 * w/o tripping rebalance_share or load_balance_fair.
7831 */
7832 tg->shares = shares;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007833 for_each_possible_cpu(i)
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007834 set_se_shares(tg->se[i], shares);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007835
7836 /*
7837 * Enable load balance activity on this group, by inserting it back on
7838 * each cpu's rq->leaf_cfs_rq_list.
7839 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007840 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007841 for_each_possible_cpu(i)
7842 register_fair_sched_group(tg, i);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007843 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007844done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007845 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007846 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007847}
7848
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007849unsigned long sched_group_shares(struct task_group *tg)
7850{
7851 return tg->shares;
7852}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007853#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007854
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007855#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007856/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007857 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007858 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007859static DEFINE_MUTEX(rt_constraints_mutex);
7860
7861static unsigned long to_ratio(u64 period, u64 runtime)
7862{
7863 if (runtime == RUNTIME_INF)
7864 return 1ULL << 16;
7865
Peter Zijlstra2692a242008-02-27 12:00:46 +01007866 return div64_64(runtime << 16, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007867}
7868
7869static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007870{
7871 struct task_group *tgi;
7872 unsigned long total = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007873 unsigned long global_ratio =
7874 to_ratio(sysctl_sched_rt_period,
7875 sysctl_sched_rt_runtime < 0 ?
7876 RUNTIME_INF : sysctl_sched_rt_runtime);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007877
7878 rcu_read_lock();
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007879 list_for_each_entry_rcu(tgi, &task_groups, list) {
7880 if (tgi == tg)
7881 continue;
7882
7883 total += to_ratio(period, tgi->rt_runtime);
7884 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007885 rcu_read_unlock();
7886
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007887 return total + to_ratio(period, runtime) < global_ratio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007888}
7889
Dhaval Giani521f1a242008-02-28 15:21:56 +05307890/* Must be called with tasklist_lock held */
7891static inline int tg_has_rt_tasks(struct task_group *tg)
7892{
7893 struct task_struct *g, *p;
7894 do_each_thread(g, p) {
7895 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
7896 return 1;
7897 } while_each_thread(g, p);
7898 return 0;
7899}
7900
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007901int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007902{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007903 u64 rt_runtime, rt_period;
7904 int err = 0;
7905
Peter Zijlstra2692a242008-02-27 12:00:46 +01007906 rt_period = (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007907 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
7908 if (rt_runtime_us == -1)
Peter Zijlstra2692a242008-02-27 12:00:46 +01007909 rt_runtime = RUNTIME_INF;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007910
7911 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05307912 read_lock(&tasklist_lock);
7913 if (rt_runtime_us == 0 && tg_has_rt_tasks(tg)) {
7914 err = -EBUSY;
7915 goto unlock;
7916 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007917 if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
7918 err = -EINVAL;
7919 goto unlock;
7920 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007921 tg->rt_runtime = rt_runtime;
7922 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05307923 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007924 mutex_unlock(&rt_constraints_mutex);
7925
7926 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007927}
7928
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007929long sched_group_rt_runtime(struct task_group *tg)
7930{
7931 u64 rt_runtime_us;
7932
7933 if (tg->rt_runtime == RUNTIME_INF)
7934 return -1;
7935
7936 rt_runtime_us = tg->rt_runtime;
7937 do_div(rt_runtime_us, NSEC_PER_USEC);
7938 return rt_runtime_us;
7939}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007940#endif
7941#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007942
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007943#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007944
7945/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02007946static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007947{
Paul Menage2b01dfe2007-10-24 18:23:50 +02007948 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
7949 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007950}
7951
7952static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02007953cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007954{
7955 struct task_group *tg;
7956
Paul Menage2b01dfe2007-10-24 18:23:50 +02007957 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007958 /* This is early initialization for the top cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02007959 init_task_group.css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007960 return &init_task_group.css;
7961 }
7962
7963 /* we support only 1-level deep hierarchical scheduler atm */
Paul Menage2b01dfe2007-10-24 18:23:50 +02007964 if (cgrp->parent->parent)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007965 return ERR_PTR(-EINVAL);
7966
7967 tg = sched_create_group();
7968 if (IS_ERR(tg))
7969 return ERR_PTR(-ENOMEM);
7970
7971 /* Bind the cgroup to task_group object we just created */
Paul Menage2b01dfe2007-10-24 18:23:50 +02007972 tg->css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007973
7974 return &tg->css;
7975}
7976
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007977static void
7978cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007979{
Paul Menage2b01dfe2007-10-24 18:23:50 +02007980 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007981
7982 sched_destroy_group(tg);
7983}
7984
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007985static int
7986cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
7987 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007988{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01007989#ifdef CONFIG_RT_GROUP_SCHED
7990 /* Don't accept realtime tasks when there is no way for them to run */
7991 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_runtime == 0)
7992 return -EINVAL;
7993#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007994 /* We don't support RT-tasks being in separate groups */
7995 if (tsk->sched_class != &fair_sched_class)
7996 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01007997#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007998
7999 return 0;
8000}
8001
8002static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02008003cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008004 struct cgroup *old_cont, struct task_struct *tsk)
8005{
8006 sched_move_task(tsk);
8007}
8008
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008009#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menage2b01dfe2007-10-24 18:23:50 +02008010static int cpu_shares_write_uint(struct cgroup *cgrp, struct cftype *cftype,
8011 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008012{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008013 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008014}
8015
Paul Menage2b01dfe2007-10-24 18:23:50 +02008016static u64 cpu_shares_read_uint(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008017{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008018 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008019
8020 return (u64) tg->shares;
8021}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008022#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008023
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008024#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008025static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
8026 struct file *file,
8027 const char __user *userbuf,
8028 size_t nbytes, loff_t *unused_ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008029{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008030 char buffer[64];
8031 int retval = 0;
8032 s64 val;
8033 char *end;
8034
8035 if (!nbytes)
8036 return -EINVAL;
8037 if (nbytes >= sizeof(buffer))
8038 return -E2BIG;
8039 if (copy_from_user(buffer, userbuf, nbytes))
8040 return -EFAULT;
8041
8042 buffer[nbytes] = 0; /* nul-terminate */
8043
8044 /* strip newline if necessary */
8045 if (nbytes && (buffer[nbytes-1] == '\n'))
8046 buffer[nbytes-1] = 0;
8047 val = simple_strtoll(buffer, &end, 0);
8048 if (*end)
8049 return -EINVAL;
8050
8051 /* Pass to subsystem */
8052 retval = sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
8053 if (!retval)
8054 retval = nbytes;
8055 return retval;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008056}
8057
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008058static ssize_t cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft,
8059 struct file *file,
8060 char __user *buf, size_t nbytes,
8061 loff_t *ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008062{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008063 char tmp[64];
8064 long val = sched_group_rt_runtime(cgroup_tg(cgrp));
8065 int len = sprintf(tmp, "%ld\n", val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008066
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008067 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008068}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008069#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008070
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008071static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008072#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008073 {
8074 .name = "shares",
8075 .read_uint = cpu_shares_read_uint,
8076 .write_uint = cpu_shares_write_uint,
8077 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008078#endif
8079#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008080 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008081 .name = "rt_runtime_us",
8082 .read = cpu_rt_runtime_read,
8083 .write = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008084 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008085#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008086};
8087
8088static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
8089{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008090 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008091}
8092
8093struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01008094 .name = "cpu",
8095 .create = cpu_cgroup_create,
8096 .destroy = cpu_cgroup_destroy,
8097 .can_attach = cpu_cgroup_can_attach,
8098 .attach = cpu_cgroup_attach,
8099 .populate = cpu_cgroup_populate,
8100 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008101 .early_init = 1,
8102};
8103
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008104#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008105
8106#ifdef CONFIG_CGROUP_CPUACCT
8107
8108/*
8109 * CPU accounting code for task groups.
8110 *
8111 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
8112 * (balbir@in.ibm.com).
8113 */
8114
8115/* track cpu usage of a group of tasks */
8116struct cpuacct {
8117 struct cgroup_subsys_state css;
8118 /* cpuusage holds pointer to a u64-type object on every cpu */
8119 u64 *cpuusage;
8120};
8121
8122struct cgroup_subsys cpuacct_subsys;
8123
8124/* return cpu accounting group corresponding to this container */
8125static inline struct cpuacct *cgroup_ca(struct cgroup *cont)
8126{
8127 return container_of(cgroup_subsys_state(cont, cpuacct_subsys_id),
8128 struct cpuacct, css);
8129}
8130
8131/* return cpu accounting group to which this task belongs */
8132static inline struct cpuacct *task_ca(struct task_struct *tsk)
8133{
8134 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
8135 struct cpuacct, css);
8136}
8137
8138/* create a new cpu accounting group */
8139static struct cgroup_subsys_state *cpuacct_create(
8140 struct cgroup_subsys *ss, struct cgroup *cont)
8141{
8142 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
8143
8144 if (!ca)
8145 return ERR_PTR(-ENOMEM);
8146
8147 ca->cpuusage = alloc_percpu(u64);
8148 if (!ca->cpuusage) {
8149 kfree(ca);
8150 return ERR_PTR(-ENOMEM);
8151 }
8152
8153 return &ca->css;
8154}
8155
8156/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008157static void
8158cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008159{
8160 struct cpuacct *ca = cgroup_ca(cont);
8161
8162 free_percpu(ca->cpuusage);
8163 kfree(ca);
8164}
8165
8166/* return total cpu usage (in nanoseconds) of a group */
8167static u64 cpuusage_read(struct cgroup *cont, struct cftype *cft)
8168{
8169 struct cpuacct *ca = cgroup_ca(cont);
8170 u64 totalcpuusage = 0;
8171 int i;
8172
8173 for_each_possible_cpu(i) {
8174 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
8175
8176 /*
8177 * Take rq->lock to make 64-bit addition safe on 32-bit
8178 * platforms.
8179 */
8180 spin_lock_irq(&cpu_rq(i)->lock);
8181 totalcpuusage += *cpuusage;
8182 spin_unlock_irq(&cpu_rq(i)->lock);
8183 }
8184
8185 return totalcpuusage;
8186}
8187
8188static struct cftype files[] = {
8189 {
8190 .name = "usage",
8191 .read_uint = cpuusage_read,
8192 },
8193};
8194
8195static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cont)
8196{
8197 return cgroup_add_files(cont, ss, files, ARRAY_SIZE(files));
8198}
8199
8200/*
8201 * charge this task's execution time to its accounting group.
8202 *
8203 * called with rq->lock held.
8204 */
8205static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
8206{
8207 struct cpuacct *ca;
8208
8209 if (!cpuacct_subsys.active)
8210 return;
8211
8212 ca = task_ca(tsk);
8213 if (ca) {
8214 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
8215
8216 *cpuusage += cputime;
8217 }
8218}
8219
8220struct cgroup_subsys cpuacct_subsys = {
8221 .name = "cpuacct",
8222 .create = cpuacct_create,
8223 .destroy = cpuacct_destroy,
8224 .populate = cpuacct_populate,
8225 .subsys_id = cpuacct_subsys_id,
8226};
8227#endif /* CONFIG_CGROUP_CPUACCT */