blob: b387a8de26a5f9ca8b6e2e9d3ad33f66fbf65882 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Eric Dumazet5517d862007-05-08 00:32:57 -070070#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020071#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*
Alexey Dobriyanb035b6d2007-02-10 01:45:10 -080074 * Scheduler clock - returns current time in nanosec units.
75 * This is default implementation.
76 * Architectures and sub-architectures can override this.
77 */
78unsigned long long __attribute__((weak)) sched_clock(void)
79{
Eric Dumazetd6322fa2007-11-09 22:39:38 +010080 return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ);
Alexey Dobriyanb035b6d2007-02-10 01:45:10 -080081}
82
83/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * Convert user-nice values [ -20 ... 0 ... 19 ]
85 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
86 * and back.
87 */
88#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
89#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
90#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
91
92/*
93 * 'User priority' is the nice value converted to something we
94 * can work with better when scaling various scheduler parameters,
95 * it's a [ 0 ... 39 ] range.
96 */
97#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
98#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
99#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
100
101/*
Ingo Molnard7876a02008-01-25 21:08:19 +0100102 * Helpers for converting nanosecond timing to jiffy resolution
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
Eric Dumazetd6322fa2007-11-09 22:39:38 +0100104#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200106#define NICE_0_LOAD SCHED_LOAD_SCALE
107#define NICE_0_SHIFT SCHED_LOAD_SHIFT
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/*
110 * These are the 'tuning knobs' of the scheduler:
111 *
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200112 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * Timeslices get refilled after they expire.
114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700116
Eric Dumazet5517d862007-05-08 00:32:57 -0700117#ifdef CONFIG_SMP
118/*
119 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
120 * Since cpu_power is a 'constant', we can use a reciprocal divide.
121 */
122static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
123{
124 return reciprocal_divide(load, sg->reciprocal_cpu_power);
125}
126
127/*
128 * Each time a sched group cpu_power is changed,
129 * we must compute its reciprocal value
130 */
131static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
132{
133 sg->__cpu_power += val;
134 sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
135}
136#endif
137
Ingo Molnare05606d2007-07-09 18:51:59 +0200138static inline int rt_policy(int policy)
139{
140 if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR))
141 return 1;
142 return 0;
143}
144
145static inline int task_has_rt_policy(struct task_struct *p)
146{
147 return rt_policy(p->policy);
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200151 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200153struct rt_prio_array {
154 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
155 struct list_head queue[MAX_RT_PRIO];
156};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100158#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200159
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700160#include <linux/cgroup.h>
161
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200162struct cfs_rq;
163
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100164static LIST_HEAD(task_groups);
165
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200166/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200167struct task_group {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100168#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700169 struct cgroup_subsys_state css;
170#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100171
172#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200173 /* schedulable entities of this group on each cpu */
174 struct sched_entity **se;
175 /* runqueue "owned" by this group on each cpu */
176 struct cfs_rq **cfs_rq;
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100177
178 /*
179 * shares assigned to a task group governs how much of cpu bandwidth
180 * is allocated to the group. The more shares a group has, the more is
181 * the cpu bandwidth allocated to it.
182 *
183 * For ex, lets say that there are three task groups, A, B and C which
184 * have been assigned shares 1000, 2000 and 3000 respectively. Then,
185 * cpu bandwidth allocated by the scheduler to task groups A, B and C
186 * should be:
187 *
188 * Bw(A) = 1000/(1000+2000+3000) * 100 = 16.66%
189 * Bw(B) = 2000/(1000+2000+3000) * 100 = 33.33%
Ingo Molnar03319ec2008-01-25 21:08:28 +0100190 * Bw(C) = 3000/(1000+2000+3000) * 100 = 50%
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100191 *
192 * The weight assigned to a task group's schedulable entities on every
193 * cpu (task_group.se[a_cpu]->load.weight) is derived from the task
194 * group's shares. For ex: lets say that task group A has been
195 * assigned shares of 1000 and there are two CPUs in a system. Then,
196 *
197 * tg_A->se[0]->load.weight = tg_A->se[1]->load.weight = 1000;
198 *
199 * Note: It's not necessary that each of a task's group schedulable
Ingo Molnar03319ec2008-01-25 21:08:28 +0100200 * entity have the same weight on all CPUs. If the group
201 * has 2 of its tasks on CPU0 and 1 task on CPU1, then a
202 * better distribution of weight could be:
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100203 *
204 * tg_A->se[0]->load.weight = 2/3 * 2000 = 1333
205 * tg_A->se[1]->load.weight = 1/2 * 2000 = 667
206 *
207 * rebalance_shares() is responsible for distributing the shares of a
208 * task groups like this among the group's schedulable entities across
209 * cpus.
210 *
211 */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200212 unsigned long shares;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100213#endif
214
215#ifdef CONFIG_RT_GROUP_SCHED
216 struct sched_rt_entity **rt_se;
217 struct rt_rq **rt_rq;
218
219 u64 rt_runtime;
220#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100221
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100222 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100223 struct list_head list;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200224};
225
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100226#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200227/* Default task group's sched entity on each cpu */
228static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
229/* Default task group's cfs_rq on each cpu */
230static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
231
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100232static struct sched_entity *init_sched_entity_p[NR_CPUS];
233static struct cfs_rq *init_cfs_rq_p[NR_CPUS];
234#endif
235
236#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100237static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
238static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
239
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100240static struct sched_rt_entity *init_sched_rt_entity_p[NR_CPUS];
241static struct rt_rq *init_rt_rq_p[NR_CPUS];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100242#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100243
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100244/* task_group_lock serializes add/remove of task groups and also changes to
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100245 * a task group's cpu shares.
246 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100247static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100248
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100249/* doms_cur_mutex serializes access to doms_cur[] array */
250static DEFINE_MUTEX(doms_cur_mutex);
251
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100252#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100253#ifdef CONFIG_SMP
254/* kernel thread that runs rebalance_shares() periodically */
255static struct task_struct *lb_monitor_task;
256static int load_balance_monitor(void *unused);
257#endif
258
259static void set_se_shares(struct sched_entity *se, unsigned long shares);
260
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100261#ifdef CONFIG_USER_SCHED
Ingo Molnar0eab9142008-01-25 21:08:19 +0100262# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200263#else
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100264# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200265#endif
266
Ingo Molnar0eab9142008-01-25 21:08:19 +0100267#define MIN_GROUP_SHARES 2
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100268
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100269static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100270#endif
271
272/* Default task group.
273 * Every task in system belong to this group at bootup.
274 */
275struct task_group init_task_group = {
276#ifdef CONFIG_FAIR_GROUP_SCHED
277 .se = init_sched_entity_p,
278 .cfs_rq = init_cfs_rq_p,
279#endif
280
281#ifdef CONFIG_RT_GROUP_SCHED
282 .rt_se = init_sched_rt_entity_p,
283 .rt_rq = init_rt_rq_p,
284#endif
285};
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200286
287/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200288static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200289{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200290 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200291
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100292#ifdef CONFIG_USER_SCHED
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200293 tg = p->user->tg;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100294#elif defined(CONFIG_CGROUP_SCHED)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700295 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
296 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200297#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100298 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200299#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200300 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200301}
302
303/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100304static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200305{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100306#ifdef CONFIG_FAIR_GROUP_SCHED
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100307 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
308 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100309#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100310
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100311#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100312 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
313 p->rt.parent = task_group(p)->rt_se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100314#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200315}
316
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100317static inline void lock_doms_cur(void)
318{
319 mutex_lock(&doms_cur_mutex);
320}
321
322static inline void unlock_doms_cur(void)
323{
324 mutex_unlock(&doms_cur_mutex);
325}
326
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200327#else
328
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100329static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100330static inline void lock_doms_cur(void) { }
331static inline void unlock_doms_cur(void) { }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200332
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100333#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200334
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200335/* CFS-related fields in a runqueue */
336struct cfs_rq {
337 struct load_weight load;
338 unsigned long nr_running;
339
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200340 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200341 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200342
343 struct rb_root tasks_timeline;
344 struct rb_node *rb_leftmost;
345 struct rb_node *rb_load_balance_curr;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200346 /* 'curr' points to currently running entity on this cfs_rq.
347 * It is set to NULL otherwise (i.e when none are currently running).
348 */
349 struct sched_entity *curr;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200350
351 unsigned long nr_spread_over;
352
Ingo Molnar62160e32007-10-15 17:00:03 +0200353#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200354 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
355
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100356 /*
357 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200358 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
359 * (like users, containers etc.)
360 *
361 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
362 * list is used during load balance.
363 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100364 struct list_head leaf_cfs_rq_list;
365 struct task_group *tg; /* group that "owns" this runqueue */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200366#endif
367};
368
369/* Real-Time classes' related field in a runqueue: */
370struct rt_rq {
371 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100372 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100373#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100374 int highest_prio; /* highest queued rt task prio */
375#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100376#ifdef CONFIG_SMP
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100377 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100378 int overloaded;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100379#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100380 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100381 u64 rt_time;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100382
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100383#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100384 unsigned long rt_nr_boosted;
385
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100386 struct rq *rq;
387 struct list_head leaf_rt_rq_list;
388 struct task_group *tg;
389 struct sched_rt_entity *rt_se;
390#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200391};
392
Gregory Haskins57d885f2008-01-25 21:08:18 +0100393#ifdef CONFIG_SMP
394
395/*
396 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100397 * variables. Each exclusive cpuset essentially defines an island domain by
398 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100399 * exclusive cpuset is created, we also create and attach a new root-domain
400 * object.
401 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100402 */
403struct root_domain {
404 atomic_t refcount;
405 cpumask_t span;
406 cpumask_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100407
Ingo Molnar0eab9142008-01-25 21:08:19 +0100408 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100409 * The "RT overload" flag: it gets set if a CPU has more than
410 * one runnable RT task.
411 */
412 cpumask_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100413 atomic_t rto_count;
Gregory Haskins57d885f2008-01-25 21:08:18 +0100414};
415
Gregory Haskinsdc938522008-01-25 21:08:26 +0100416/*
417 * By default the system creates a single root-domain with all cpus as
418 * members (mimicking the global state we have today).
419 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100420static struct root_domain def_root_domain;
421
422#endif
423
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200424/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * This is the main, per-CPU runqueue data structure.
426 *
427 * Locking rule: those places that want to lock multiple runqueues
428 * (such as the load balancing or the thread migration code), lock
429 * acquire operations must be ordered by ascending &runqueue.
430 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700431struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200432 /* runqueue lock: */
433 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /*
436 * nr_running and cpu_load should be in the same cacheline because
437 * remote CPUs use both these fields when doing load calculation.
438 */
439 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200440 #define CPU_LOAD_IDX_MAX 5
441 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700442 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700443#ifdef CONFIG_NO_HZ
444 unsigned char in_nohz_recently;
445#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200446 /* capture load from *all* tasks on this cpu: */
447 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200448 unsigned long nr_load_updates;
449 u64 nr_switches;
450
451 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100452 struct rt_rq rt;
453 u64 rt_period_expire;
Peter Zijlstra48d5e252008-01-25 21:08:31 +0100454 int rt_throttled;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100455
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200456#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200457 /* list of leaf cfs_rq on this cpu: */
458 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100459#endif
460#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100461 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 /*
465 * This is part of a global counter where only the total sum
466 * over all CPUs matters. A task can increase this counter on
467 * one CPU and if it got migrated afterwards it may decrease
468 * it on another CPU. Always updated under the runqueue lock:
469 */
470 unsigned long nr_uninterruptible;
471
Ingo Molnar36c8b582006-07-03 00:25:41 -0700472 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800473 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200475
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200476 u64 clock, prev_clock_raw;
477 s64 clock_max_delta;
478
Guillaume Chazaraincc203d22008-01-25 21:08:34 +0100479 unsigned int clock_warps, clock_overflows, clock_underflows;
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200480 u64 idle_clock;
481 unsigned int clock_deep_idle_events;
Ingo Molnar529c7722007-08-10 23:05:11 +0200482 u64 tick_timestamp;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 atomic_t nr_iowait;
485
486#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100487 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 struct sched_domain *sd;
489
490 /* For active balancing */
491 int active_balance;
492 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200493 /* cpu of this runqueue: */
494 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Ingo Molnar36c8b582006-07-03 00:25:41 -0700496 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 struct list_head migration_queue;
498#endif
499
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100500#ifdef CONFIG_SCHED_HRTICK
501 unsigned long hrtick_flags;
502 ktime_t hrtick_expire;
503 struct hrtimer hrtick_timer;
504#endif
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506#ifdef CONFIG_SCHEDSTATS
507 /* latency stats */
508 struct sched_info rq_sched_info;
509
510 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200511 unsigned int yld_exp_empty;
512 unsigned int yld_act_empty;
513 unsigned int yld_both_empty;
514 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200517 unsigned int sched_switch;
518 unsigned int sched_count;
519 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200522 unsigned int ttwu_count;
523 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200524
525 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200526 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527#endif
Ingo Molnarfcb99372006-07-03 00:25:10 -0700528 struct lock_class_key rq_lock_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529};
530
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700531static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Ingo Molnardd41f592007-07-09 18:51:59 +0200533static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
534{
535 rq->curr->sched_class->check_preempt_curr(rq, p);
536}
537
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700538static inline int cpu_of(struct rq *rq)
539{
540#ifdef CONFIG_SMP
541 return rq->cpu;
542#else
543 return 0;
544#endif
545}
546
Nick Piggin674311d2005-06-25 14:57:27 -0700547/*
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200548 * Update the per-runqueue clock, as finegrained as the platform can give
549 * us, but without assuming monotonicity, etc.:
Ingo Molnar20d315d2007-07-09 18:51:58 +0200550 */
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200551static void __update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200552{
553 u64 prev_raw = rq->prev_clock_raw;
554 u64 now = sched_clock();
555 s64 delta = now - prev_raw;
556 u64 clock = rq->clock;
557
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200558#ifdef CONFIG_SCHED_DEBUG
559 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
560#endif
Ingo Molnar20d315d2007-07-09 18:51:58 +0200561 /*
562 * Protect against sched_clock() occasionally going backwards:
563 */
564 if (unlikely(delta < 0)) {
565 clock++;
566 rq->clock_warps++;
567 } else {
568 /*
569 * Catch too large forward jumps too:
570 */
Ingo Molnar529c7722007-08-10 23:05:11 +0200571 if (unlikely(clock + delta > rq->tick_timestamp + TICK_NSEC)) {
572 if (clock < rq->tick_timestamp + TICK_NSEC)
573 clock = rq->tick_timestamp + TICK_NSEC;
574 else
575 clock++;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200576 rq->clock_overflows++;
577 } else {
578 if (unlikely(delta > rq->clock_max_delta))
579 rq->clock_max_delta = delta;
580 clock += delta;
581 }
582 }
583
584 rq->prev_clock_raw = now;
585 rq->clock = clock;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200586}
587
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200588static void update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200589{
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200590 if (likely(smp_processor_id() == cpu_of(rq)))
591 __update_rq_clock(rq);
592}
Ingo Molnar20d315d2007-07-09 18:51:58 +0200593
Ingo Molnar20d315d2007-07-09 18:51:58 +0200594/*
Nick Piggin674311d2005-06-25 14:57:27 -0700595 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700596 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700597 *
598 * The domain tree of any CPU may only be accessed from within
599 * preempt-disabled sections.
600 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700601#define for_each_domain(cpu, __sd) \
602 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
605#define this_rq() (&__get_cpu_var(runqueues))
606#define task_rq(p) cpu_rq(task_cpu(p))
607#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
608
Peter Zijlstra48d5e252008-01-25 21:08:31 +0100609unsigned long rt_needs_cpu(int cpu)
610{
611 struct rq *rq = cpu_rq(cpu);
612 u64 delta;
613
614 if (!rq->rt_throttled)
615 return 0;
616
617 if (rq->clock > rq->rt_period_expire)
618 return 1;
619
620 delta = rq->rt_period_expire - rq->clock;
621 do_div(delta, NSEC_PER_SEC / HZ);
622
623 return (unsigned long)delta;
624}
625
Ingo Molnare436d802007-07-19 21:28:35 +0200626/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200627 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
628 */
629#ifdef CONFIG_SCHED_DEBUG
630# define const_debug __read_mostly
631#else
632# define const_debug static const
633#endif
634
635/*
636 * Debugging: various feature bits
637 */
638enum {
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200639 SCHED_FEAT_NEW_FAIR_SLEEPERS = 1,
Ingo Molnar96126332007-11-15 20:57:40 +0100640 SCHED_FEAT_WAKEUP_PREEMPT = 2,
641 SCHED_FEAT_START_DEBIT = 4,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100642 SCHED_FEAT_TREE_AVG = 8,
643 SCHED_FEAT_APPROX_AVG = 16,
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100644 SCHED_FEAT_HRTICK = 32,
645 SCHED_FEAT_DOUBLE_TICK = 64,
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200646};
647
648const_debug unsigned int sysctl_sched_features =
Ingo Molnar8401f772007-10-18 21:32:55 +0200649 SCHED_FEAT_NEW_FAIR_SLEEPERS * 1 |
Ingo Molnar96126332007-11-15 20:57:40 +0100650 SCHED_FEAT_WAKEUP_PREEMPT * 1 |
Ingo Molnar8401f772007-10-18 21:32:55 +0200651 SCHED_FEAT_START_DEBIT * 1 |
652 SCHED_FEAT_TREE_AVG * 0 |
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100653 SCHED_FEAT_APPROX_AVG * 0 |
654 SCHED_FEAT_HRTICK * 1 |
655 SCHED_FEAT_DOUBLE_TICK * 0;
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200656
657#define sched_feat(x) (sysctl_sched_features & SCHED_FEAT_##x)
658
659/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100660 * Number of tasks to iterate in a single balance run.
661 * Limited because this is done with IRQs disabled.
662 */
663const_debug unsigned int sysctl_sched_nr_migrate = 32;
664
665/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100666 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100667 * default: 1s
668 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100669unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100670
671/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100672 * part of the period that we allow rt tasks to run in us.
673 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100674 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100675int sysctl_sched_rt_runtime = 950000;
676
677/*
678 * single value that denotes runtime == period, ie unlimited time.
679 */
680#define RUNTIME_INF ((u64)~0ULL)
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100681
682/*
Ingo Molnare436d802007-07-19 21:28:35 +0200683 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
684 * clock constructed from sched_clock():
685 */
686unsigned long long cpu_clock(int cpu)
687{
Ingo Molnare436d802007-07-19 21:28:35 +0200688 unsigned long long now;
689 unsigned long flags;
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200690 struct rq *rq;
Ingo Molnare436d802007-07-19 21:28:35 +0200691
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200692 local_irq_save(flags);
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200693 rq = cpu_rq(cpu);
Ingo Molnar8ced5f62007-12-07 19:02:47 +0100694 /*
695 * Only call sched_clock() if the scheduler has already been
696 * initialized (some code might call cpu_clock() very early):
697 */
698 if (rq->idle)
699 update_rq_clock(rq);
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200700 now = rq->clock;
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200701 local_irq_restore(flags);
Ingo Molnare436d802007-07-19 21:28:35 +0200702
703 return now;
704}
Paul E. McKenneya58f6f22007-10-15 17:00:14 +0200705EXPORT_SYMBOL_GPL(cpu_clock);
Ingo Molnare436d802007-07-19 21:28:35 +0200706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700708# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700710#ifndef finish_arch_switch
711# define finish_arch_switch(prev) do { } while (0)
712#endif
713
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100714static inline int task_current(struct rq *rq, struct task_struct *p)
715{
716 return rq->curr == p;
717}
718
Nick Piggin4866cde2005-06-25 14:57:23 -0700719#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700720static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700721{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100722 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700723}
724
Ingo Molnar70b97a72006-07-03 00:25:42 -0700725static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700726{
727}
728
Ingo Molnar70b97a72006-07-03 00:25:42 -0700729static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700730{
Ingo Molnarda04c032005-09-13 11:17:59 +0200731#ifdef CONFIG_DEBUG_SPINLOCK
732 /* this is a valid case when another task releases the spinlock */
733 rq->lock.owner = current;
734#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700735 /*
736 * If we are tracking spinlock dependencies then we have to
737 * fix up the runqueue lock - which gets 'carried over' from
738 * prev into current:
739 */
740 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
741
Nick Piggin4866cde2005-06-25 14:57:23 -0700742 spin_unlock_irq(&rq->lock);
743}
744
745#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700746static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700747{
748#ifdef CONFIG_SMP
749 return p->oncpu;
750#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100751 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700752#endif
753}
754
Ingo Molnar70b97a72006-07-03 00:25:42 -0700755static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700756{
757#ifdef CONFIG_SMP
758 /*
759 * We can optimise this out completely for !SMP, because the
760 * SMP rebalancing from interrupt is the only thing that cares
761 * here.
762 */
763 next->oncpu = 1;
764#endif
765#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
766 spin_unlock_irq(&rq->lock);
767#else
768 spin_unlock(&rq->lock);
769#endif
770}
771
Ingo Molnar70b97a72006-07-03 00:25:42 -0700772static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700773{
774#ifdef CONFIG_SMP
775 /*
776 * After ->oncpu is cleared, the task can be moved to a different CPU.
777 * We must ensure this doesn't happen until the switch is completely
778 * finished.
779 */
780 smp_wmb();
781 prev->oncpu = 0;
782#endif
783#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
784 local_irq_enable();
785#endif
786}
787#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700790 * __task_rq_lock - lock the runqueue a given task resides on.
791 * Must be called interrupts disabled.
792 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700793static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700794 __acquires(rq->lock)
795{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200796 for (;;) {
797 struct rq *rq = task_rq(p);
798 spin_lock(&rq->lock);
799 if (likely(rq == task_rq(p)))
800 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700801 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700802 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700803}
804
805/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100807 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 * explicitly disabling preemption.
809 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700810static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 __acquires(rq->lock)
812{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700813 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Andi Kleen3a5c3592007-10-15 17:00:14 +0200815 for (;;) {
816 local_irq_save(*flags);
817 rq = task_rq(p);
818 spin_lock(&rq->lock);
819 if (likely(rq == task_rq(p)))
820 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
Alexey Dobriyana9957442007-10-15 17:00:13 +0200825static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700826 __releases(rq->lock)
827{
828 spin_unlock(&rq->lock);
829}
830
Ingo Molnar70b97a72006-07-03 00:25:42 -0700831static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 __releases(rq->lock)
833{
834 spin_unlock_irqrestore(&rq->lock, *flags);
835}
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800838 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200840static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 __acquires(rq->lock)
842{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700843 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 local_irq_disable();
846 rq = this_rq();
847 spin_lock(&rq->lock);
848
849 return rq;
850}
851
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200852/*
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200853 * We are going deep-idle (irqs are disabled):
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200854 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200855void sched_clock_idle_sleep_event(void)
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200856{
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200857 struct rq *rq = cpu_rq(smp_processor_id());
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200858
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200859 spin_lock(&rq->lock);
860 __update_rq_clock(rq);
861 spin_unlock(&rq->lock);
862 rq->clock_deep_idle_events++;
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200863}
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200864EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
865
866/*
867 * We just idled delta nanoseconds (called with irqs disabled):
868 */
869void sched_clock_idle_wakeup_event(u64 delta_ns)
870{
871 struct rq *rq = cpu_rq(smp_processor_id());
872 u64 now = sched_clock();
873
874 rq->idle_clock += delta_ns;
875 /*
876 * Override the previous timestamp and ignore all
877 * sched_clock() deltas that occured while we idled,
878 * and use the PM-provided delta_ns to advance the
879 * rq clock:
880 */
881 spin_lock(&rq->lock);
882 rq->prev_clock_raw = now;
883 rq->clock += delta_ns;
884 spin_unlock(&rq->lock);
Guillaume Chazarain782daee2008-01-25 21:08:33 +0100885 touch_softlockup_watchdog();
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200886}
887EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200888
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100889static void __resched_task(struct task_struct *p, int tif_bit);
890
891static inline void resched_task(struct task_struct *p)
892{
893 __resched_task(p, TIF_NEED_RESCHED);
894}
895
896#ifdef CONFIG_SCHED_HRTICK
897/*
898 * Use HR-timers to deliver accurate preemption points.
899 *
900 * Its all a bit involved since we cannot program an hrt while holding the
901 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
902 * reschedule event.
903 *
904 * When we get rescheduled we reprogram the hrtick_timer outside of the
905 * rq->lock.
906 */
907static inline void resched_hrt(struct task_struct *p)
908{
909 __resched_task(p, TIF_HRTICK_RESCHED);
910}
911
912static inline void resched_rq(struct rq *rq)
913{
914 unsigned long flags;
915
916 spin_lock_irqsave(&rq->lock, flags);
917 resched_task(rq->curr);
918 spin_unlock_irqrestore(&rq->lock, flags);
919}
920
921enum {
922 HRTICK_SET, /* re-programm hrtick_timer */
923 HRTICK_RESET, /* not a new slice */
924};
925
926/*
927 * Use hrtick when:
928 * - enabled by features
929 * - hrtimer is actually high res
930 */
931static inline int hrtick_enabled(struct rq *rq)
932{
933 if (!sched_feat(HRTICK))
934 return 0;
935 return hrtimer_is_hres_active(&rq->hrtick_timer);
936}
937
938/*
939 * Called to set the hrtick timer state.
940 *
941 * called with rq->lock held and irqs disabled
942 */
943static void hrtick_start(struct rq *rq, u64 delay, int reset)
944{
945 assert_spin_locked(&rq->lock);
946
947 /*
948 * preempt at: now + delay
949 */
950 rq->hrtick_expire =
951 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
952 /*
953 * indicate we need to program the timer
954 */
955 __set_bit(HRTICK_SET, &rq->hrtick_flags);
956 if (reset)
957 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
958
959 /*
960 * New slices are called from the schedule path and don't need a
961 * forced reschedule.
962 */
963 if (reset)
964 resched_hrt(rq->curr);
965}
966
967static void hrtick_clear(struct rq *rq)
968{
969 if (hrtimer_active(&rq->hrtick_timer))
970 hrtimer_cancel(&rq->hrtick_timer);
971}
972
973/*
974 * Update the timer from the possible pending state.
975 */
976static void hrtick_set(struct rq *rq)
977{
978 ktime_t time;
979 int set, reset;
980 unsigned long flags;
981
982 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
983
984 spin_lock_irqsave(&rq->lock, flags);
985 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
986 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
987 time = rq->hrtick_expire;
988 clear_thread_flag(TIF_HRTICK_RESCHED);
989 spin_unlock_irqrestore(&rq->lock, flags);
990
991 if (set) {
992 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
993 if (reset && !hrtimer_active(&rq->hrtick_timer))
994 resched_rq(rq);
995 } else
996 hrtick_clear(rq);
997}
998
999/*
1000 * High-resolution timer tick.
1001 * Runs from hardirq context with interrupts disabled.
1002 */
1003static enum hrtimer_restart hrtick(struct hrtimer *timer)
1004{
1005 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1006
1007 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1008
1009 spin_lock(&rq->lock);
1010 __update_rq_clock(rq);
1011 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1012 spin_unlock(&rq->lock);
1013
1014 return HRTIMER_NORESTART;
1015}
1016
1017static inline void init_rq_hrtick(struct rq *rq)
1018{
1019 rq->hrtick_flags = 0;
1020 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1021 rq->hrtick_timer.function = hrtick;
1022 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1023}
1024
1025void hrtick_resched(void)
1026{
1027 struct rq *rq;
1028 unsigned long flags;
1029
1030 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1031 return;
1032
1033 local_irq_save(flags);
1034 rq = cpu_rq(smp_processor_id());
1035 hrtick_set(rq);
1036 local_irq_restore(flags);
1037}
1038#else
1039static inline void hrtick_clear(struct rq *rq)
1040{
1041}
1042
1043static inline void hrtick_set(struct rq *rq)
1044{
1045}
1046
1047static inline void init_rq_hrtick(struct rq *rq)
1048{
1049}
1050
1051void hrtick_resched(void)
1052{
1053}
1054#endif
1055
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001056/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001057 * resched_task - mark a task 'to be rescheduled now'.
1058 *
1059 * On UP this means the setting of the need_resched flag, on SMP it
1060 * might also involve a cross-CPU call to trigger the scheduler on
1061 * the target CPU.
1062 */
1063#ifdef CONFIG_SMP
1064
1065#ifndef tsk_is_polling
1066#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1067#endif
1068
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001069static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001070{
1071 int cpu;
1072
1073 assert_spin_locked(&task_rq(p)->lock);
1074
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001075 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001076 return;
1077
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001078 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001079
1080 cpu = task_cpu(p);
1081 if (cpu == smp_processor_id())
1082 return;
1083
1084 /* NEED_RESCHED must be visible before we test polling */
1085 smp_mb();
1086 if (!tsk_is_polling(p))
1087 smp_send_reschedule(cpu);
1088}
1089
1090static void resched_cpu(int cpu)
1091{
1092 struct rq *rq = cpu_rq(cpu);
1093 unsigned long flags;
1094
1095 if (!spin_trylock_irqsave(&rq->lock, flags))
1096 return;
1097 resched_task(cpu_curr(cpu));
1098 spin_unlock_irqrestore(&rq->lock, flags);
1099}
1100#else
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001101static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001102{
1103 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001104 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001105}
1106#endif
1107
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001108#if BITS_PER_LONG == 32
1109# define WMULT_CONST (~0UL)
1110#else
1111# define WMULT_CONST (1UL << 32)
1112#endif
1113
1114#define WMULT_SHIFT 32
1115
Ingo Molnar194081e2007-08-09 11:16:51 +02001116/*
1117 * Shift right and round:
1118 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001119#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001120
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001121static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001122calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1123 struct load_weight *lw)
1124{
1125 u64 tmp;
1126
1127 if (unlikely(!lw->inv_weight))
Ingo Molnar194081e2007-08-09 11:16:51 +02001128 lw->inv_weight = (WMULT_CONST - lw->weight/2) / lw->weight + 1;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001129
1130 tmp = (u64)delta_exec * weight;
1131 /*
1132 * Check whether we'd overflow the 64-bit multiplication:
1133 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001134 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001135 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001136 WMULT_SHIFT/2);
1137 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001138 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001139
Ingo Molnarecf691d2007-08-02 17:41:40 +02001140 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001141}
1142
1143static inline unsigned long
1144calc_delta_fair(unsigned long delta_exec, struct load_weight *lw)
1145{
1146 return calc_delta_mine(delta_exec, NICE_0_LOAD, lw);
1147}
1148
Ingo Molnar10919852007-10-15 17:00:04 +02001149static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001150{
1151 lw->weight += inc;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001152}
1153
Ingo Molnar10919852007-10-15 17:00:04 +02001154static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001155{
1156 lw->weight -= dec;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001157}
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001160 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1161 * of tasks with abnormal "nice" values across CPUs the contribution that
1162 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001163 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001164 * scaled version of the new time slice allocation that they receive on time
1165 * slice expiry etc.
1166 */
1167
Ingo Molnardd41f592007-07-09 18:51:59 +02001168#define WEIGHT_IDLEPRIO 2
1169#define WMULT_IDLEPRIO (1 << 31)
1170
1171/*
1172 * Nice levels are multiplicative, with a gentle 10% change for every
1173 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1174 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1175 * that remained on nice 0.
1176 *
1177 * The "10% effect" is relative and cumulative: from _any_ nice level,
1178 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +02001179 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1180 * If a task goes up by ~10% and another task goes down by ~10% then
1181 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001182 */
1183static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001184 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1185 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1186 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1187 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1188 /* 0 */ 1024, 820, 655, 526, 423,
1189 /* 5 */ 335, 272, 215, 172, 137,
1190 /* 10 */ 110, 87, 70, 56, 45,
1191 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001192};
1193
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001194/*
1195 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1196 *
1197 * In cases where the weight does not change often, we can use the
1198 * precalculated inverse to speed up arithmetics by turning divisions
1199 * into multiplications:
1200 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001201static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001202 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1203 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1204 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1205 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1206 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1207 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1208 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1209 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001210};
Peter Williams2dd73a42006-06-27 02:54:34 -07001211
Ingo Molnardd41f592007-07-09 18:51:59 +02001212static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1213
1214/*
1215 * runqueue iterator, to support SMP load-balancing between different
1216 * scheduling classes, without having to expose their internal data
1217 * structures to the load-balancing proper:
1218 */
1219struct rq_iterator {
1220 void *arg;
1221 struct task_struct *(*start)(void *);
1222 struct task_struct *(*next)(void *);
1223};
1224
Peter Williamse1d14842007-10-24 18:23:51 +02001225#ifdef CONFIG_SMP
1226static unsigned long
1227balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1228 unsigned long max_load_move, struct sched_domain *sd,
1229 enum cpu_idle_type idle, int *all_pinned,
1230 int *this_best_prio, struct rq_iterator *iterator);
1231
1232static int
1233iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1234 struct sched_domain *sd, enum cpu_idle_type idle,
1235 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001236#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001237
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001238#ifdef CONFIG_CGROUP_CPUACCT
1239static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1240#else
1241static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1242#endif
1243
Srivatsa Vaddagiri58e2d4c2008-01-25 21:08:00 +01001244static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1245{
1246 update_load_add(&rq->load, load);
1247}
1248
1249static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1250{
1251 update_load_sub(&rq->load, load);
1252}
1253
Gregory Haskinse7693a32008-01-25 21:08:09 +01001254#ifdef CONFIG_SMP
1255static unsigned long source_load(int cpu, int type);
1256static unsigned long target_load(int cpu, int type);
1257static unsigned long cpu_avg_load_per_task(int cpu);
1258static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
1259#endif /* CONFIG_SMP */
1260
Ingo Molnardd41f592007-07-09 18:51:59 +02001261#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001262#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001263#include "sched_fair.c"
1264#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001265#ifdef CONFIG_SCHED_DEBUG
1266# include "sched_debug.c"
1267#endif
1268
1269#define sched_class_highest (&rt_sched_class)
1270
Gerald Stralko5aff0532008-01-31 22:45:23 +01001271static void inc_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001272{
1273 rq->nr_running++;
Ingo Molnar9c217242007-08-02 17:41:40 +02001274}
1275
Gerald Stralko5aff0532008-01-31 22:45:23 +01001276static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001277{
1278 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001279}
1280
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001281static void set_load_weight(struct task_struct *p)
1282{
1283 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001284 p->se.load.weight = prio_to_weight[0] * 2;
1285 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1286 return;
1287 }
1288
1289 /*
1290 * SCHED_IDLE tasks get minimal weight:
1291 */
1292 if (p->policy == SCHED_IDLE) {
1293 p->se.load.weight = WEIGHT_IDLEPRIO;
1294 p->se.load.inv_weight = WMULT_IDLEPRIO;
1295 return;
1296 }
1297
1298 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1299 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001300}
1301
Ingo Molnar8159f872007-08-09 11:16:49 +02001302static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001303{
1304 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001305 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001306 p->se.on_rq = 1;
1307}
1308
Ingo Molnar69be72c2007-08-09 11:16:49 +02001309static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001310{
Ingo Molnarf02231e2007-08-09 11:16:48 +02001311 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001312 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001313}
1314
1315/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001316 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001317 */
Ingo Molnar14531182007-07-09 18:51:59 +02001318static inline int __normal_prio(struct task_struct *p)
1319{
Ingo Molnardd41f592007-07-09 18:51:59 +02001320 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001321}
1322
1323/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001324 * Calculate the expected normal priority: i.e. priority
1325 * without taking RT-inheritance into account. Might be
1326 * boosted by interactivity modifiers. Changes upon fork,
1327 * setprio syscalls, and whenever the interactivity
1328 * estimator recalculates.
1329 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001330static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001331{
1332 int prio;
1333
Ingo Molnare05606d2007-07-09 18:51:59 +02001334 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001335 prio = MAX_RT_PRIO-1 - p->rt_priority;
1336 else
1337 prio = __normal_prio(p);
1338 return prio;
1339}
1340
1341/*
1342 * Calculate the current priority, i.e. the priority
1343 * taken into account by the scheduler. This value might
1344 * be boosted by RT tasks, or might be boosted by
1345 * interactivity modifiers. Will be RT if the task got
1346 * RT-boosted. If not then it returns p->normal_prio.
1347 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001348static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001349{
1350 p->normal_prio = normal_prio(p);
1351 /*
1352 * If we are RT tasks or we were boosted to RT priority,
1353 * keep the priority unchanged. Otherwise, update priority
1354 * to the normal priority:
1355 */
1356 if (!rt_prio(p->prio))
1357 return p->normal_prio;
1358 return p->prio;
1359}
1360
1361/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001362 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001364static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001366 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001367 rq->nr_uninterruptible--;
1368
Ingo Molnar8159f872007-08-09 11:16:49 +02001369 enqueue_task(rq, p, wakeup);
Gerald Stralko5aff0532008-01-31 22:45:23 +01001370 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
1373/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 * deactivate_task - remove a task from the runqueue.
1375 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02001376static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001378 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001379 rq->nr_uninterruptible++;
1380
Ingo Molnar69be72c2007-08-09 11:16:49 +02001381 dequeue_task(rq, p, sleep);
Gerald Stralko5aff0532008-01-31 22:45:23 +01001382 dec_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385/**
1386 * task_curr - is this task currently executing on a CPU?
1387 * @p: the task in question.
1388 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001389inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 return cpu_curr(task_cpu(p)) == p;
1392}
1393
Peter Williams2dd73a42006-06-27 02:54:34 -07001394/* Used instead of source_load when we know the type == 0 */
1395unsigned long weighted_cpuload(const int cpu)
1396{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001397 return cpu_rq(cpu)->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02001398}
1399
1400static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1401{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001402 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001403#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01001404 /*
1405 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1406 * successfuly executed on another CPU. We must ensure that updates of
1407 * per-task data have been completed by this moment.
1408 */
1409 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02001410 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02001411#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07001412}
1413
Steven Rostedtcb469842008-01-25 21:08:22 +01001414static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1415 const struct sched_class *prev_class,
1416 int oldprio, int running)
1417{
1418 if (prev_class != p->sched_class) {
1419 if (prev_class->switched_from)
1420 prev_class->switched_from(rq, p, running);
1421 p->sched_class->switched_to(rq, p, running);
1422 } else
1423 p->sched_class->prio_changed(rq, p, oldprio, running);
1424}
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02001427
Ingo Molnarcc367732007-10-15 17:00:18 +02001428/*
1429 * Is this task likely cache-hot:
1430 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001431static int
Ingo Molnarcc367732007-10-15 17:00:18 +02001432task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
1433{
1434 s64 delta;
1435
1436 if (p->sched_class != &fair_sched_class)
1437 return 0;
1438
Ingo Molnar6bc16652007-10-15 17:00:18 +02001439 if (sysctl_sched_migration_cost == -1)
1440 return 1;
1441 if (sysctl_sched_migration_cost == 0)
1442 return 0;
1443
Ingo Molnarcc367732007-10-15 17:00:18 +02001444 delta = now - p->se.exec_start;
1445
1446 return delta < (s64)sysctl_sched_migration_cost;
1447}
1448
1449
Ingo Molnardd41f592007-07-09 18:51:59 +02001450void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001451{
Ingo Molnardd41f592007-07-09 18:51:59 +02001452 int old_cpu = task_cpu(p);
1453 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001454 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
1455 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02001456 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001457
1458 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001459
1460#ifdef CONFIG_SCHEDSTATS
1461 if (p->se.wait_start)
1462 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001463 if (p->se.sleep_start)
1464 p->se.sleep_start -= clock_offset;
1465 if (p->se.block_start)
1466 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02001467 if (old_cpu != new_cpu) {
1468 schedstat_inc(p, se.nr_migrations);
1469 if (task_hot(p, old_rq->clock, NULL))
1470 schedstat_inc(p, se.nr_forced2_migrations);
1471 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001472#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001473 p->se.vruntime -= old_cfsrq->min_vruntime -
1474 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02001475
1476 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001477}
1478
Ingo Molnar70b97a72006-07-03 00:25:42 -07001479struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Ingo Molnar36c8b582006-07-03 00:25:41 -07001482 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 int dest_cpu;
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001486};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488/*
1489 * The task's runqueue lock must be held.
1490 * Returns true if you have to wait for migration thread.
1491 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001492static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07001493migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001495 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 /*
1498 * If the task is not on a runqueue (and not running), then
1499 * it is sufficient to simply update the task's cpu field.
1500 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001501 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 set_task_cpu(p, dest_cpu);
1503 return 0;
1504 }
1505
1506 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 req->task = p;
1508 req->dest_cpu = dest_cpu;
1509 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07001510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return 1;
1512}
1513
1514/*
1515 * wait_task_inactive - wait for a thread to unschedule.
1516 *
1517 * The caller must ensure that the task *will* unschedule sometime soon,
1518 * else this function might spin for a *long* time. This function can't
1519 * be called with interrupts off, or it may introduce deadlock with
1520 * smp_call_function() if an IPI is sent by the same process we are
1521 * waiting to become inactive.
1522 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001523void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
1525 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001526 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001527 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Andi Kleen3a5c3592007-10-15 17:00:14 +02001529 for (;;) {
1530 /*
1531 * We do the initial early heuristics without holding
1532 * any task-queue locks at all. We'll only try to get
1533 * the runqueue lock when things look like they will
1534 * work out!
1535 */
1536 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001537
Andi Kleen3a5c3592007-10-15 17:00:14 +02001538 /*
1539 * If the task is actively running on another CPU
1540 * still, just relax and busy-wait without holding
1541 * any locks.
1542 *
1543 * NOTE! Since we don't hold any locks, it's not
1544 * even sure that "rq" stays as the right runqueue!
1545 * But we don't care, since "task_running()" will
1546 * return false if the runqueue has changed and p
1547 * is actually now running somewhere else!
1548 */
1549 while (task_running(rq, p))
1550 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001551
Andi Kleen3a5c3592007-10-15 17:00:14 +02001552 /*
1553 * Ok, time to look more closely! We need the rq
1554 * lock now, to be *sure*. If we're wrong, we'll
1555 * just go back and repeat.
1556 */
1557 rq = task_rq_lock(p, &flags);
1558 running = task_running(rq, p);
1559 on_rq = p->se.on_rq;
1560 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001561
Andi Kleen3a5c3592007-10-15 17:00:14 +02001562 /*
1563 * Was it really running after all now that we
1564 * checked with the proper locks actually held?
1565 *
1566 * Oops. Go back and try again..
1567 */
1568 if (unlikely(running)) {
1569 cpu_relax();
1570 continue;
1571 }
1572
1573 /*
1574 * It's not enough that it's not actively running,
1575 * it must be off the runqueue _entirely_, and not
1576 * preempted!
1577 *
1578 * So if it wa still runnable (but just not actively
1579 * running right now), it's preempted, and we should
1580 * yield - it could be a while.
1581 */
1582 if (unlikely(on_rq)) {
1583 schedule_timeout_uninterruptible(1);
1584 continue;
1585 }
1586
1587 /*
1588 * Ahh, all good. It wasn't running, and it wasn't
1589 * runnable, which means that it will never become
1590 * running in the future either. We're all done!
1591 */
1592 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595
1596/***
1597 * kick_process - kick a running thread to enter/exit the kernel
1598 * @p: the to-be-kicked thread
1599 *
1600 * Cause a process which is running on another CPU to enter
1601 * kernel-mode, without any delay. (to get signals handled.)
1602 *
1603 * NOTE: this function doesnt have to take the runqueue lock,
1604 * because all it wants to ensure is that the remote task enters
1605 * the kernel. If the IPI races and the task has been migrated
1606 * to another CPU then no harm is done and the purpose has been
1607 * achieved as well.
1608 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001609void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
1611 int cpu;
1612
1613 preempt_disable();
1614 cpu = task_cpu(p);
1615 if ((cpu != smp_processor_id()) && task_curr(p))
1616 smp_send_reschedule(cpu);
1617 preempt_enable();
1618}
1619
1620/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001621 * Return a low guess at the load of a migration-source cpu weighted
1622 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 *
1624 * We want to under-estimate the load of migration sources, to
1625 * balance conservatively.
1626 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001627static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08001628{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001629 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001630 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001631
Peter Williams2dd73a42006-06-27 02:54:34 -07001632 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001633 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001634
Ingo Molnardd41f592007-07-09 18:51:59 +02001635 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
1638/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001639 * Return a high guess at the load of a migration-target cpu weighted
1640 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001642static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08001643{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001644 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001645 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001646
Peter Williams2dd73a42006-06-27 02:54:34 -07001647 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001648 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001649
Ingo Molnardd41f592007-07-09 18:51:59 +02001650 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07001651}
1652
1653/*
1654 * Return the average load per task on the cpu's run queue
1655 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001656static unsigned long cpu_avg_load_per_task(int cpu)
Peter Williams2dd73a42006-06-27 02:54:34 -07001657{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001658 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001659 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07001660 unsigned long n = rq->nr_running;
1661
Ingo Molnardd41f592007-07-09 18:51:59 +02001662 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
Nick Piggin147cbb42005-06-25 14:57:19 -07001665/*
1666 * find_idlest_group finds and returns the least busy CPU group within the
1667 * domain.
1668 */
1669static struct sched_group *
1670find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
1671{
1672 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
1673 unsigned long min_load = ULONG_MAX, this_load = 0;
1674 int load_idx = sd->forkexec_idx;
1675 int imbalance = 100 + (sd->imbalance_pct-100)/2;
1676
1677 do {
1678 unsigned long load, avg_load;
1679 int local_group;
1680 int i;
1681
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001682 /* Skip over this group if it has no CPUs allowed */
1683 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02001684 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001685
Nick Piggin147cbb42005-06-25 14:57:19 -07001686 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07001687
1688 /* Tally up the load of all CPUs in the group */
1689 avg_load = 0;
1690
1691 for_each_cpu_mask(i, group->cpumask) {
1692 /* Bias balancing toward cpus of our domain */
1693 if (local_group)
1694 load = source_load(i, load_idx);
1695 else
1696 load = target_load(i, load_idx);
1697
1698 avg_load += load;
1699 }
1700
1701 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07001702 avg_load = sg_div_cpu_power(group,
1703 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07001704
1705 if (local_group) {
1706 this_load = avg_load;
1707 this = group;
1708 } else if (avg_load < min_load) {
1709 min_load = avg_load;
1710 idlest = group;
1711 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02001712 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07001713
1714 if (!idlest || 100*this_load < imbalance*min_load)
1715 return NULL;
1716 return idlest;
1717}
1718
1719/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07001720 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07001721 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07001722static int
1723find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
Nick Piggin147cbb42005-06-25 14:57:19 -07001724{
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001725 cpumask_t tmp;
Nick Piggin147cbb42005-06-25 14:57:19 -07001726 unsigned long load, min_load = ULONG_MAX;
1727 int idlest = -1;
1728 int i;
1729
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001730 /* Traverse only the allowed CPUs */
1731 cpus_and(tmp, group->cpumask, p->cpus_allowed);
1732
1733 for_each_cpu_mask(i, tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07001734 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07001735
1736 if (load < min_load || (load == min_load && i == this_cpu)) {
1737 min_load = load;
1738 idlest = i;
1739 }
1740 }
1741
1742 return idlest;
1743}
1744
Nick Piggin476d1392005-06-25 14:57:29 -07001745/*
1746 * sched_balance_self: balance the current task (running on cpu) in domains
1747 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
1748 * SD_BALANCE_EXEC.
1749 *
1750 * Balance, ie. select the least loaded group.
1751 *
1752 * Returns the target CPU number, or the same CPU if no balancing is needed.
1753 *
1754 * preempt must be disabled.
1755 */
1756static int sched_balance_self(int cpu, int flag)
1757{
1758 struct task_struct *t = current;
1759 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07001760
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001761 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02001762 /*
1763 * If power savings logic is enabled for a domain, stop there.
1764 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07001765 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
1766 break;
Nick Piggin476d1392005-06-25 14:57:29 -07001767 if (tmp->flags & flag)
1768 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001769 }
Nick Piggin476d1392005-06-25 14:57:29 -07001770
1771 while (sd) {
1772 cpumask_t span;
1773 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001774 int new_cpu, weight;
1775
1776 if (!(sd->flags & flag)) {
1777 sd = sd->child;
1778 continue;
1779 }
Nick Piggin476d1392005-06-25 14:57:29 -07001780
1781 span = sd->span;
1782 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001783 if (!group) {
1784 sd = sd->child;
1785 continue;
1786 }
Nick Piggin476d1392005-06-25 14:57:29 -07001787
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001788 new_cpu = find_idlest_cpu(group, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001789 if (new_cpu == -1 || new_cpu == cpu) {
1790 /* Now try balancing at a lower domain level of cpu */
1791 sd = sd->child;
1792 continue;
1793 }
Nick Piggin476d1392005-06-25 14:57:29 -07001794
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001795 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07001796 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07001797 sd = NULL;
1798 weight = cpus_weight(span);
1799 for_each_domain(cpu, tmp) {
1800 if (weight <= cpus_weight(tmp->span))
1801 break;
1802 if (tmp->flags & flag)
1803 sd = tmp;
1804 }
1805 /* while loop will break here if sd == NULL */
1806 }
1807
1808 return cpu;
1809}
1810
1811#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813/***
1814 * try_to_wake_up - wake up a thread
1815 * @p: the to-be-woken-up thread
1816 * @state: the mask of task states that can be woken
1817 * @sync: do a synchronous wakeup?
1818 *
1819 * Put it on the run-queue if it's not already there. The "current"
1820 * thread is always on the run-queue (except when the actual
1821 * re-schedule is in progress), and as such you're allowed to do
1822 * the simpler "current->state = TASK_RUNNING" to mark yourself
1823 * runnable without the overhead of this.
1824 *
1825 * returns failure only if the task is already active.
1826 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001827static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Ingo Molnarcc367732007-10-15 17:00:18 +02001829 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 unsigned long flags;
1831 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001832 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Linus Torvalds04e2f172008-02-23 18:05:03 -08001834 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 rq = task_rq_lock(p, &flags);
1836 old_state = p->state;
1837 if (!(old_state & state))
1838 goto out;
1839
Ingo Molnardd41f592007-07-09 18:51:59 +02001840 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 goto out_running;
1842
1843 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02001844 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 this_cpu = smp_processor_id();
1846
1847#ifdef CONFIG_SMP
1848 if (unlikely(task_running(rq, p)))
1849 goto out_activate;
1850
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01001851 cpu = p->sched_class->select_task_rq(p, sync);
1852 if (cpu != orig_cpu) {
1853 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 task_rq_unlock(rq, &flags);
1855 /* might preempt at this point */
1856 rq = task_rq_lock(p, &flags);
1857 old_state = p->state;
1858 if (!(old_state & state))
1859 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02001860 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 goto out_running;
1862
1863 this_cpu = smp_processor_id();
1864 cpu = task_cpu(p);
1865 }
1866
Gregory Haskinse7693a32008-01-25 21:08:09 +01001867#ifdef CONFIG_SCHEDSTATS
1868 schedstat_inc(rq, ttwu_count);
1869 if (cpu == this_cpu)
1870 schedstat_inc(rq, ttwu_local);
1871 else {
1872 struct sched_domain *sd;
1873 for_each_domain(this_cpu, sd) {
1874 if (cpu_isset(cpu, sd->span)) {
1875 schedstat_inc(sd, ttwu_wake_remote);
1876 break;
1877 }
1878 }
1879 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01001880#endif
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882out_activate:
1883#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02001884 schedstat_inc(p, se.nr_wakeups);
1885 if (sync)
1886 schedstat_inc(p, se.nr_wakeups_sync);
1887 if (orig_cpu != cpu)
1888 schedstat_inc(p, se.nr_wakeups_migrate);
1889 if (cpu == this_cpu)
1890 schedstat_inc(p, se.nr_wakeups_local);
1891 else
1892 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02001893 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02001894 activate_task(rq, p, 1);
Ingo Molnar9c63d9c2007-10-15 17:00:20 +02001895 check_preempt_curr(rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 success = 1;
1897
1898out_running:
1899 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01001900#ifdef CONFIG_SMP
1901 if (p->sched_class->task_wake_up)
1902 p->sched_class->task_wake_up(rq, p);
1903#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904out:
1905 task_rq_unlock(rq, &flags);
1906
1907 return success;
1908}
1909
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001910int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001912 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914EXPORT_SYMBOL(wake_up_process);
1915
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001916int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
1918 return try_to_wake_up(p, state, 0);
1919}
1920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921/*
1922 * Perform scheduler related setup for a newly forked process p.
1923 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02001924 *
1925 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001927static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
Ingo Molnardd41f592007-07-09 18:51:59 +02001929 p->se.exec_start = 0;
1930 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02001931 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001932
1933#ifdef CONFIG_SCHEDSTATS
1934 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02001935 p->se.sum_sleep_runtime = 0;
1936 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02001937 p->se.block_start = 0;
1938 p->se.sleep_max = 0;
1939 p->se.block_max = 0;
1940 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02001941 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02001942 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001943#endif
Nick Piggin476d1392005-06-25 14:57:29 -07001944
Peter Zijlstrafa717062008-01-25 21:08:27 +01001945 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02001946 p->se.on_rq = 0;
Nick Piggin476d1392005-06-25 14:57:29 -07001947
Avi Kivitye107be32007-07-26 13:40:43 +02001948#ifdef CONFIG_PREEMPT_NOTIFIERS
1949 INIT_HLIST_HEAD(&p->preempt_notifiers);
1950#endif
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 /*
1953 * We mark the process as running here, but have not actually
1954 * inserted it onto the runqueue yet. This guarantees that
1955 * nobody will actually run it, and a signal or other external
1956 * event cannot wake it up and insert it on the runqueue either.
1957 */
1958 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02001959}
1960
1961/*
1962 * fork()/clone()-time setup:
1963 */
1964void sched_fork(struct task_struct *p, int clone_flags)
1965{
1966 int cpu = get_cpu();
1967
1968 __sched_fork(p);
1969
1970#ifdef CONFIG_SMP
1971 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
1972#endif
Ingo Molnar02e4bac2007-10-15 17:00:11 +02001973 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07001974
1975 /*
1976 * Make sure we do not leak PI boosting priority to the child:
1977 */
1978 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02001979 if (!rt_prio(p->prio))
1980 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07001981
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07001982#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02001983 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07001984 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08001986#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07001987 p->oncpu = 0;
1988#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07001990 /* Want to start with kernel preemption disabled. */
Al Viroa1261f52005-11-13 16:06:55 -08001991 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992#endif
Nick Piggin476d1392005-06-25 14:57:29 -07001993 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994}
1995
1996/*
1997 * wake_up_new_task - wake up a newly created task for the first time.
1998 *
1999 * This function will do some initial scheduler statistics housekeeping
2000 * that must be done for every newly created context, then puts the task
2001 * on the runqueue and wakes it.
2002 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002003void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
2005 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002006 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002010 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
2012 p->prio = effective_prio(p);
2013
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002014 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002015 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002018 * Let the scheduling class do new task startup
2019 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002021 p->sched_class->task_new(rq, p);
Gerald Stralko5aff0532008-01-31 22:45:23 +01002022 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002024 check_preempt_curr(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002025#ifdef CONFIG_SMP
2026 if (p->sched_class->task_wake_up)
2027 p->sched_class->task_wake_up(rq, p);
2028#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002029 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Avi Kivitye107be32007-07-26 13:40:43 +02002032#ifdef CONFIG_PREEMPT_NOTIFIERS
2033
2034/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002035 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2036 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002037 */
2038void preempt_notifier_register(struct preempt_notifier *notifier)
2039{
2040 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2041}
2042EXPORT_SYMBOL_GPL(preempt_notifier_register);
2043
2044/**
2045 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002046 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002047 *
2048 * This is safe to call from within a preemption notifier.
2049 */
2050void preempt_notifier_unregister(struct preempt_notifier *notifier)
2051{
2052 hlist_del(&notifier->link);
2053}
2054EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2055
2056static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2057{
2058 struct preempt_notifier *notifier;
2059 struct hlist_node *node;
2060
2061 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2062 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2063}
2064
2065static void
2066fire_sched_out_preempt_notifiers(struct task_struct *curr,
2067 struct task_struct *next)
2068{
2069 struct preempt_notifier *notifier;
2070 struct hlist_node *node;
2071
2072 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2073 notifier->ops->sched_out(notifier, next);
2074}
2075
2076#else
2077
2078static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2079{
2080}
2081
2082static void
2083fire_sched_out_preempt_notifiers(struct task_struct *curr,
2084 struct task_struct *next)
2085{
2086}
2087
2088#endif
2089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002091 * prepare_task_switch - prepare to switch tasks
2092 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002093 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002094 * @next: the task we are going to switch to.
2095 *
2096 * This is called with the rq lock held and interrupts off. It must
2097 * be paired with a subsequent finish_task_switch after the context
2098 * switch.
2099 *
2100 * prepare_task_switch sets up locking and calls architecture specific
2101 * hooks.
2102 */
Avi Kivitye107be32007-07-26 13:40:43 +02002103static inline void
2104prepare_task_switch(struct rq *rq, struct task_struct *prev,
2105 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002106{
Avi Kivitye107be32007-07-26 13:40:43 +02002107 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002108 prepare_lock_switch(rq, next);
2109 prepare_arch_switch(next);
2110}
2111
2112/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002114 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 * @prev: the thread we just switched away from.
2116 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002117 * finish_task_switch must be called after the context switch, paired
2118 * with a prepare_task_switch call before the context switch.
2119 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2120 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 *
2122 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002123 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 * with the lock held can cause deadlocks; see schedule() for
2125 * details.)
2126 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002127static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 __releases(rq->lock)
2129{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002131 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133 rq->prev_mm = NULL;
2134
2135 /*
2136 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002137 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002138 * schedule one last time. The schedule call will never return, and
2139 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002140 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 * still held, otherwise prev could be scheduled on another cpu, die
2142 * there before we look at prev->state, and then the reference would
2143 * be dropped twice.
2144 * Manfred Spraul <manfred@colorfullife.com>
2145 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002146 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002147 finish_arch_switch(prev);
2148 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002149#ifdef CONFIG_SMP
2150 if (current->sched_class->post_schedule)
2151 current->sched_class->post_schedule(rq);
2152#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002153
Avi Kivitye107be32007-07-26 13:40:43 +02002154 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 if (mm)
2156 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002157 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002158 /*
2159 * Remove function-return probe instances associated with this
2160 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002161 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002162 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
2167/**
2168 * schedule_tail - first thing a freshly forked thread must call.
2169 * @prev: the thread we just switched away from.
2170 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002171asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 __releases(rq->lock)
2173{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002174 struct rq *rq = this_rq();
2175
Nick Piggin4866cde2005-06-25 14:57:23 -07002176 finish_task_switch(rq, prev);
2177#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2178 /* In this case, finish_task_switch does not reenable preemption */
2179 preempt_enable();
2180#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002182 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
2185/*
2186 * context_switch - switch to the new MM and the new
2187 * thread's register state.
2188 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002189static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002190context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002191 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
Ingo Molnardd41f592007-07-09 18:51:59 +02002193 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Avi Kivitye107be32007-07-26 13:40:43 +02002195 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002196 mm = next->mm;
2197 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002198 /*
2199 * For paravirt, this is coupled with an exit in switch_to to
2200 * combine the page table reload and the switch backend into
2201 * one hypercall.
2202 */
2203 arch_enter_lazy_cpu_mode();
2204
Ingo Molnardd41f592007-07-09 18:51:59 +02002205 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 next->active_mm = oldmm;
2207 atomic_inc(&oldmm->mm_count);
2208 enter_lazy_tlb(oldmm, next);
2209 } else
2210 switch_mm(oldmm, mm, next);
2211
Ingo Molnardd41f592007-07-09 18:51:59 +02002212 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 rq->prev_mm = oldmm;
2215 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002216 /*
2217 * Since the runqueue lock will be released by the next
2218 * task (which is an invalid locking op but in the case
2219 * of the scheduler it's an obvious special-case), so we
2220 * do an early lockdep release here:
2221 */
2222#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002223 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002224#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 /* Here we just switch the register state and the stack. */
2227 switch_to(prev, next, prev);
2228
Ingo Molnardd41f592007-07-09 18:51:59 +02002229 barrier();
2230 /*
2231 * this_rq must be evaluated again because prev may have moved
2232 * CPUs since it called schedule(), thus the 'rq' on its stack
2233 * frame will be invalid.
2234 */
2235 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236}
2237
2238/*
2239 * nr_running, nr_uninterruptible and nr_context_switches:
2240 *
2241 * externally visible scheduler statistics: current number of runnable
2242 * threads, current number of uninterruptible-sleeping threads, total
2243 * number of context switches performed since bootup.
2244 */
2245unsigned long nr_running(void)
2246{
2247 unsigned long i, sum = 0;
2248
2249 for_each_online_cpu(i)
2250 sum += cpu_rq(i)->nr_running;
2251
2252 return sum;
2253}
2254
2255unsigned long nr_uninterruptible(void)
2256{
2257 unsigned long i, sum = 0;
2258
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002259 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 sum += cpu_rq(i)->nr_uninterruptible;
2261
2262 /*
2263 * Since we read the counters lockless, it might be slightly
2264 * inaccurate. Do not allow it to go below zero though:
2265 */
2266 if (unlikely((long)sum < 0))
2267 sum = 0;
2268
2269 return sum;
2270}
2271
2272unsigned long long nr_context_switches(void)
2273{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002274 int i;
2275 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002277 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 sum += cpu_rq(i)->nr_switches;
2279
2280 return sum;
2281}
2282
2283unsigned long nr_iowait(void)
2284{
2285 unsigned long i, sum = 0;
2286
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002287 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2289
2290 return sum;
2291}
2292
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002293unsigned long nr_active(void)
2294{
2295 unsigned long i, running = 0, uninterruptible = 0;
2296
2297 for_each_online_cpu(i) {
2298 running += cpu_rq(i)->nr_running;
2299 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2300 }
2301
2302 if (unlikely((long)uninterruptible < 0))
2303 uninterruptible = 0;
2304
2305 return running + uninterruptible;
2306}
2307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002309 * Update rq->cpu_load[] statistics. This function is usually called every
2310 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002311 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002312static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002313{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002314 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002315 int i, scale;
2316
2317 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002318
2319 /* Update our load: */
2320 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2321 unsigned long old_load, new_load;
2322
2323 /* scale is effectively 1 << i now, and >> i divides by scale */
2324
2325 old_load = this_rq->cpu_load[i];
2326 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002327 /*
2328 * Round up the averaging division if load is increasing. This
2329 * prevents us from getting stuck on 9 if the load is 10, for
2330 * example.
2331 */
2332 if (new_load > old_load)
2333 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002334 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2335 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002336}
2337
Ingo Molnardd41f592007-07-09 18:51:59 +02002338#ifdef CONFIG_SMP
2339
Ingo Molnar48f24c42006-07-03 00:25:40 -07002340/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 * double_rq_lock - safely lock two runqueues
2342 *
2343 * Note this does not disable interrupts like task_rq_lock,
2344 * you need to do so manually before calling.
2345 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002346static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 __acquires(rq1->lock)
2348 __acquires(rq2->lock)
2349{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002350 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (rq1 == rq2) {
2352 spin_lock(&rq1->lock);
2353 __acquire(rq2->lock); /* Fake it out ;) */
2354 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002355 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 spin_lock(&rq1->lock);
2357 spin_lock(&rq2->lock);
2358 } else {
2359 spin_lock(&rq2->lock);
2360 spin_lock(&rq1->lock);
2361 }
2362 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002363 update_rq_clock(rq1);
2364 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365}
2366
2367/*
2368 * double_rq_unlock - safely unlock two runqueues
2369 *
2370 * Note this does not restore interrupts like task_rq_unlock,
2371 * you need to do so manually after calling.
2372 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002373static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 __releases(rq1->lock)
2375 __releases(rq2->lock)
2376{
2377 spin_unlock(&rq1->lock);
2378 if (rq1 != rq2)
2379 spin_unlock(&rq2->lock);
2380 else
2381 __release(rq2->lock);
2382}
2383
2384/*
2385 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2386 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01002387static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 __releases(this_rq->lock)
2389 __acquires(busiest->lock)
2390 __acquires(this_rq->lock)
2391{
Steven Rostedte8fa1362008-01-25 21:08:05 +01002392 int ret = 0;
2393
Kirill Korotaev054b9102006-12-10 02:20:11 -08002394 if (unlikely(!irqs_disabled())) {
2395 /* printk() doesn't work good under rq->lock */
2396 spin_unlock(&this_rq->lock);
2397 BUG_ON(1);
2398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002400 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 spin_unlock(&this_rq->lock);
2402 spin_lock(&busiest->lock);
2403 spin_lock(&this_rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01002404 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 } else
2406 spin_lock(&busiest->lock);
2407 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01002408 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409}
2410
2411/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 * If dest_cpu is allowed for this process, migrate the task to it.
2413 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002414 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 * the cpu_allowed mask is restored.
2416 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002417static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002419 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002421 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
2423 rq = task_rq_lock(p, &flags);
2424 if (!cpu_isset(dest_cpu, p->cpus_allowed)
2425 || unlikely(cpu_is_offline(dest_cpu)))
2426 goto out;
2427
2428 /* force the process onto the specified CPU */
2429 if (migrate_task(p, dest_cpu, &req)) {
2430 /* Need to wait for migration thread (might exit: take ref). */
2431 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07002432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 get_task_struct(mt);
2434 task_rq_unlock(rq, &flags);
2435 wake_up_process(mt);
2436 put_task_struct(mt);
2437 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07002438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 return;
2440 }
2441out:
2442 task_rq_unlock(rq, &flags);
2443}
2444
2445/*
Nick Piggin476d1392005-06-25 14:57:29 -07002446 * sched_exec - execve() is a valuable balancing opportunity, because at
2447 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 */
2449void sched_exec(void)
2450{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002452 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002454 if (new_cpu != this_cpu)
2455 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
2457
2458/*
2459 * pull_task - move a task from a remote runqueue to the local runqueue.
2460 * Both runqueues must be locked.
2461 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002462static void pull_task(struct rq *src_rq, struct task_struct *p,
2463 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02002465 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002467 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 /*
2469 * Note that idle threads have a prio of MAX_PRIO, for this test
2470 * to be always true for them.
2471 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002472 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
2475/*
2476 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
2477 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002478static
Ingo Molnar70b97a72006-07-03 00:25:42 -07002479int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002480 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002481 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
2483 /*
2484 * We do not migrate tasks that are:
2485 * 1) running (obviously), or
2486 * 2) cannot be migrated to this CPU due to cpus_allowed, or
2487 * 3) are cache-hot on their current CPU.
2488 */
Ingo Molnarcc367732007-10-15 17:00:18 +02002489 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
2490 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002492 }
Nick Piggin81026792005-06-25 14:57:07 -07002493 *all_pinned = 0;
2494
Ingo Molnarcc367732007-10-15 17:00:18 +02002495 if (task_running(rq, p)) {
2496 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07002497 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Ingo Molnarda84d962007-10-15 17:00:18 +02002500 /*
2501 * Aggressive migration if:
2502 * 1) task is cache cold, or
2503 * 2) too many balance attempts have failed.
2504 */
2505
Ingo Molnar6bc16652007-10-15 17:00:18 +02002506 if (!task_hot(p, rq->clock, sd) ||
2507 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002508#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02002509 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002510 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02002511 schedstat_inc(p, se.nr_forced_migrations);
2512 }
Ingo Molnarda84d962007-10-15 17:00:18 +02002513#endif
2514 return 1;
2515 }
2516
Ingo Molnarcc367732007-10-15 17:00:18 +02002517 if (task_hot(p, rq->clock, sd)) {
2518 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02002519 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 return 1;
2522}
2523
Peter Williamse1d14842007-10-24 18:23:51 +02002524static unsigned long
2525balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
2526 unsigned long max_load_move, struct sched_domain *sd,
2527 enum cpu_idle_type idle, int *all_pinned,
2528 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02002529{
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002530 int loops = 0, pulled = 0, pinned = 0, skip_for_load;
Ingo Molnardd41f592007-07-09 18:51:59 +02002531 struct task_struct *p;
2532 long rem_load_move = max_load_move;
2533
Peter Williamse1d14842007-10-24 18:23:51 +02002534 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002535 goto out;
2536
2537 pinned = 1;
2538
2539 /*
2540 * Start the load-balancing iterator:
2541 */
2542 p = iterator->start(iterator->arg);
2543next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002544 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02002545 goto out;
2546 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002547 * To help distribute high priority tasks across CPUs we don't
Ingo Molnardd41f592007-07-09 18:51:59 +02002548 * skip a task if it will be the highest priority task (i.e. smallest
2549 * prio value) on its new queue regardless of its load weight
2550 */
2551 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
2552 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002553 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02002554 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002555 p = iterator->next(iterator->arg);
2556 goto next;
2557 }
2558
2559 pull_task(busiest, p, this_rq, this_cpu);
2560 pulled++;
2561 rem_load_move -= p->se.load.weight;
2562
2563 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002564 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002565 */
Peter Williamse1d14842007-10-24 18:23:51 +02002566 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002567 if (p->prio < *this_best_prio)
2568 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02002569 p = iterator->next(iterator->arg);
2570 goto next;
2571 }
2572out:
2573 /*
Peter Williamse1d14842007-10-24 18:23:51 +02002574 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02002575 * so we can safely collect pull_task() stats here rather than
2576 * inside pull_task().
2577 */
2578 schedstat_add(sd, lb_gained[idle], pulled);
2579
2580 if (all_pinned)
2581 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02002582
2583 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02002584}
Ingo Molnar48f24c42006-07-03 00:25:40 -07002585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586/*
Peter Williams43010652007-08-09 11:16:46 +02002587 * move_tasks tries to move up to max_load_move weighted load from busiest to
2588 * this_rq, as part of a balancing operation within domain "sd".
2589 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 *
2591 * Called with both runqueues locked.
2592 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002593static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02002594 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002595 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07002596 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002598 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02002599 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002600 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Ingo Molnardd41f592007-07-09 18:51:59 +02002602 do {
Peter Williams43010652007-08-09 11:16:46 +02002603 total_load_moved +=
2604 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02002605 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002606 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02002607 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02002608 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Peter Williams43010652007-08-09 11:16:46 +02002610 return total_load_moved > 0;
2611}
2612
Peter Williamse1d14842007-10-24 18:23:51 +02002613static int
2614iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2615 struct sched_domain *sd, enum cpu_idle_type idle,
2616 struct rq_iterator *iterator)
2617{
2618 struct task_struct *p = iterator->start(iterator->arg);
2619 int pinned = 0;
2620
2621 while (p) {
2622 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
2623 pull_task(busiest, p, this_rq, this_cpu);
2624 /*
2625 * Right now, this is only the second place pull_task()
2626 * is called, so we can safely collect pull_task()
2627 * stats here rather than inside pull_task().
2628 */
2629 schedstat_inc(sd, lb_gained[idle]);
2630
2631 return 1;
2632 }
2633 p = iterator->next(iterator->arg);
2634 }
2635
2636 return 0;
2637}
2638
Peter Williams43010652007-08-09 11:16:46 +02002639/*
2640 * move_one_task tries to move exactly one task from busiest to this_rq, as
2641 * part of active balancing operations within "domain".
2642 * Returns 1 if successful and 0 otherwise.
2643 *
2644 * Called with both runqueues locked.
2645 */
2646static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2647 struct sched_domain *sd, enum cpu_idle_type idle)
2648{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002649 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02002650
2651 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02002652 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02002653 return 1;
2654
2655 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}
2657
2658/*
2659 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07002660 * domain. It calculates and returns the amount of weighted load which
2661 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 */
2663static struct sched_group *
2664find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02002665 unsigned long *imbalance, enum cpu_idle_type idle,
2666 int *sd_idle, cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
2668 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
2669 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002670 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07002671 unsigned long busiest_load_per_task, busiest_nr_running;
2672 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02002673 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002674#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2675 int power_savings_balance = 1;
2676 unsigned long leader_nr_running = 0, min_load_per_task = 0;
2677 unsigned long min_nr_running = ULONG_MAX;
2678 struct sched_group *group_min = NULL, *group_leader = NULL;
2679#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
2681 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002682 busiest_load_per_task = busiest_nr_running = 0;
2683 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002684 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002685 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002686 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002687 load_idx = sd->newidle_idx;
2688 else
2689 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
2691 do {
Ken Chen908a7c12007-10-17 16:55:11 +02002692 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 int local_group;
2694 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02002695 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002696 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002697 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
2699 local_group = cpu_isset(this_cpu, group->cpumask);
2700
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002701 if (local_group)
2702 balance_cpu = first_cpu(group->cpumask);
2703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07002705 sum_weighted_load = sum_nr_running = avg_load = 0;
Ken Chen908a7c12007-10-17 16:55:11 +02002706 max_cpu_load = 0;
2707 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
2709 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002710 struct rq *rq;
2711
2712 if (!cpu_isset(i, *cpus))
2713 continue;
2714
2715 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07002716
Suresh Siddha9439aab2007-07-19 21:28:35 +02002717 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07002718 *sd_idle = 0;
2719
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002721 if (local_group) {
2722 if (idle_cpu(i) && !first_idle_cpu) {
2723 first_idle_cpu = 1;
2724 balance_cpu = i;
2725 }
2726
Nick Piggina2000572006-02-10 01:51:02 -08002727 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02002728 } else {
Nick Piggina2000572006-02-10 01:51:02 -08002729 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02002730 if (load > max_cpu_load)
2731 max_cpu_load = load;
2732 if (min_cpu_load > load)
2733 min_cpu_load = load;
2734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
2736 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07002737 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002738 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 }
2740
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002741 /*
2742 * First idle cpu or the first cpu(busiest) in this sched group
2743 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02002744 * domains. In the newly idle case, we will allow all the cpu's
2745 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002746 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02002747 if (idle != CPU_NEWLY_IDLE && local_group &&
2748 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002749 *balance = 0;
2750 goto ret;
2751 }
2752
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07002754 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
2756 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002757 avg_load = sg_div_cpu_power(group,
2758 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Ken Chen908a7c12007-10-17 16:55:11 +02002760 if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
2761 __group_imb = 1;
2762
Eric Dumazet5517d862007-05-08 00:32:57 -07002763 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002764
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 if (local_group) {
2766 this_load = avg_load;
2767 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002768 this_nr_running = sum_nr_running;
2769 this_load_per_task = sum_weighted_load;
2770 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02002771 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 max_load = avg_load;
2773 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002774 busiest_nr_running = sum_nr_running;
2775 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02002776 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002778
2779#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2780 /*
2781 * Busy processors will not participate in power savings
2782 * balance.
2783 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002784 if (idle == CPU_NOT_IDLE ||
2785 !(sd->flags & SD_POWERSAVINGS_BALANCE))
2786 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002787
2788 /*
2789 * If the local group is idle or completely loaded
2790 * no need to do power savings balance at this domain
2791 */
2792 if (local_group && (this_nr_running >= group_capacity ||
2793 !this_nr_running))
2794 power_savings_balance = 0;
2795
Ingo Molnardd41f592007-07-09 18:51:59 +02002796 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002797 * If a group is already running at full capacity or idle,
2798 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02002799 */
2800 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002801 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02002802 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002803
Ingo Molnardd41f592007-07-09 18:51:59 +02002804 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002805 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002806 * This is the group from where we need to pick up the load
2807 * for saving power
2808 */
2809 if ((sum_nr_running < min_nr_running) ||
2810 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002811 first_cpu(group->cpumask) <
2812 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002813 group_min = group;
2814 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002815 min_load_per_task = sum_weighted_load /
2816 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002817 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002818
Ingo Molnardd41f592007-07-09 18:51:59 +02002819 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002820 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02002821 * capacity but still has some space to pick up some load
2822 * from other group and save more power
2823 */
2824 if (sum_nr_running <= group_capacity - 1) {
2825 if (sum_nr_running > leader_nr_running ||
2826 (sum_nr_running == leader_nr_running &&
2827 first_cpu(group->cpumask) >
2828 first_cpu(group_leader->cpumask))) {
2829 group_leader = group;
2830 leader_nr_running = sum_nr_running;
2831 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002832 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002833group_next:
2834#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 group = group->next;
2836 } while (group != sd->groups);
2837
Peter Williams2dd73a42006-06-27 02:54:34 -07002838 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 goto out_balanced;
2840
2841 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
2842
2843 if (this_load >= avg_load ||
2844 100*max_load <= sd->imbalance_pct*this_load)
2845 goto out_balanced;
2846
Peter Williams2dd73a42006-06-27 02:54:34 -07002847 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02002848 if (group_imb)
2849 busiest_load_per_task = min(busiest_load_per_task, avg_load);
2850
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 /*
2852 * We're trying to get all the cpus to the average_load, so we don't
2853 * want to push ourselves above the average load, nor do we wish to
2854 * reduce the max loaded cpu below the average load, as either of these
2855 * actions would just result in more rebalancing later, and ping-pong
2856 * tasks around. Thus we look for the minimum possible imbalance.
2857 * Negative imbalances (*we* are more loaded than anyone else) will
2858 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002859 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 * appear as very large values with unsigned longs.
2861 */
Peter Williams2dd73a42006-06-27 02:54:34 -07002862 if (max_load <= busiest_load_per_task)
2863 goto out_balanced;
2864
2865 /*
2866 * In the presence of smp nice balancing, certain scenarios can have
2867 * max load less than avg load(as we skip the groups at or below
2868 * its cpu_power, while calculating max_load..)
2869 */
2870 if (max_load < avg_load) {
2871 *imbalance = 0;
2872 goto small_imbalance;
2873 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002874
2875 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07002876 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002877
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07002879 *imbalance = min(max_pull * busiest->__cpu_power,
2880 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 / SCHED_LOAD_SCALE;
2882
Peter Williams2dd73a42006-06-27 02:54:34 -07002883 /*
2884 * if *imbalance is less than the average load per runnable task
2885 * there is no gaurantee that any tasks will be moved so we'll have
2886 * a think about bumping its value to force at least one task to be
2887 * moved
2888 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02002889 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07002890 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07002891 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
Peter Williams2dd73a42006-06-27 02:54:34 -07002893small_imbalance:
2894 pwr_move = pwr_now = 0;
2895 imbn = 2;
2896 if (this_nr_running) {
2897 this_load_per_task /= this_nr_running;
2898 if (busiest_load_per_task > this_load_per_task)
2899 imbn = 1;
2900 } else
2901 this_load_per_task = SCHED_LOAD_SCALE;
2902
Ingo Molnardd41f592007-07-09 18:51:59 +02002903 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
2904 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002905 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 return busiest;
2907 }
2908
2909 /*
2910 * OK, we don't have enough imbalance to justify moving tasks,
2911 * however we may be able to increase total CPU power used by
2912 * moving them.
2913 */
2914
Eric Dumazet5517d862007-05-08 00:32:57 -07002915 pwr_now += busiest->__cpu_power *
2916 min(busiest_load_per_task, max_load);
2917 pwr_now += this->__cpu_power *
2918 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 pwr_now /= SCHED_LOAD_SCALE;
2920
2921 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07002922 tmp = sg_div_cpu_power(busiest,
2923 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07002925 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07002926 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
2928 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07002929 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08002930 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07002931 tmp = sg_div_cpu_power(this,
2932 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 else
Eric Dumazet5517d862007-05-08 00:32:57 -07002934 tmp = sg_div_cpu_power(this,
2935 busiest_load_per_task * SCHED_LOAD_SCALE);
2936 pwr_move += this->__cpu_power *
2937 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 pwr_move /= SCHED_LOAD_SCALE;
2939
2940 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02002941 if (pwr_move > pwr_now)
2942 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 }
2944
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 return busiest;
2946
2947out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002948#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002949 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002950 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002952 if (this == group_leader && group_leader != group_min) {
2953 *imbalance = min_load_per_task;
2954 return group_min;
2955 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002956#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002957ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 *imbalance = 0;
2959 return NULL;
2960}
2961
2962/*
2963 * find_busiest_queue - find the busiest runqueue among the cpus in group.
2964 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002965static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002966find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002967 unsigned long imbalance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002969 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07002970 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 int i;
2972
2973 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002974 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002975
2976 if (!cpu_isset(i, *cpus))
2977 continue;
2978
Ingo Molnar48f24c42006-07-03 00:25:40 -07002979 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02002980 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Ingo Molnardd41f592007-07-09 18:51:59 +02002982 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07002983 continue;
2984
Ingo Molnardd41f592007-07-09 18:51:59 +02002985 if (wl > max_load) {
2986 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07002987 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 }
2989 }
2990
2991 return busiest;
2992}
2993
2994/*
Nick Piggin77391d72005-06-25 14:57:30 -07002995 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
2996 * so long as it is large enough.
2997 */
2998#define MAX_PINNED_INTERVAL 512
2999
3000/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3002 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003004static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003005 struct sched_domain *sd, enum cpu_idle_type idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003006 int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007{
Peter Williams43010652007-08-09 11:16:46 +02003008 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003011 struct rq *busiest;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003012 cpumask_t cpus = CPU_MASK_ALL;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003013 unsigned long flags;
Nick Piggin5969fe02005-09-10 00:26:19 -07003014
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003015 /*
3016 * When power savings policy is enabled for the parent domain, idle
3017 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003018 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003019 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003020 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003021 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003022 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003023 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Ingo Molnar2d723762007-10-15 17:00:12 +02003025 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003027redo:
3028 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003029 &cpus, balance);
3030
Chen, Kenneth W06066712006-12-10 02:20:35 -08003031 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003032 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003033
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 if (!group) {
3035 schedstat_inc(sd, lb_nobusyg[idle]);
3036 goto out_balanced;
3037 }
3038
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003039 busiest = find_busiest_queue(group, idle, imbalance, &cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 if (!busiest) {
3041 schedstat_inc(sd, lb_nobusyq[idle]);
3042 goto out_balanced;
3043 }
3044
Nick Piggindb935db2005-06-25 14:57:11 -07003045 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
3047 schedstat_add(sd, lb_imbalance[idle], imbalance);
3048
Peter Williams43010652007-08-09 11:16:46 +02003049 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 if (busiest->nr_running > 1) {
3051 /*
3052 * Attempt to move tasks. If find_busiest_group has found
3053 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003054 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 * correctly treated as an imbalance.
3056 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003057 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003058 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003059 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003060 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003061 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003062 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003063
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003064 /*
3065 * some other cpu did the load balance for us.
3066 */
Peter Williams43010652007-08-09 11:16:46 +02003067 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003068 resched_cpu(this_cpu);
3069
Nick Piggin81026792005-06-25 14:57:07 -07003070 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003071 if (unlikely(all_pinned)) {
3072 cpu_clear(cpu_of(busiest), cpus);
3073 if (!cpus_empty(cpus))
3074 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003075 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 }
Nick Piggin81026792005-06-25 14:57:07 -07003078
Peter Williams43010652007-08-09 11:16:46 +02003079 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 schedstat_inc(sd, lb_failed[idle]);
3081 sd->nr_balance_failed++;
3082
3083 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003085 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003086
3087 /* don't kick the migration_thread, if the curr
3088 * task on busiest cpu can't be moved to this_cpu
3089 */
3090 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003091 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003092 all_pinned = 1;
3093 goto out_one_pinned;
3094 }
3095
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 if (!busiest->active_balance) {
3097 busiest->active_balance = 1;
3098 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003099 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003101 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003102 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 wake_up_process(busiest->migration_thread);
3104
3105 /*
3106 * We've kicked active balancing, reset the failure
3107 * counter.
3108 */
Nick Piggin39507452005-06-25 14:57:09 -07003109 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 }
Nick Piggin81026792005-06-25 14:57:07 -07003111 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 sd->nr_balance_failed = 0;
3113
Nick Piggin81026792005-06-25 14:57:07 -07003114 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 /* We were unbalanced, so reset the balancing interval */
3116 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003117 } else {
3118 /*
3119 * If we've begun active balancing, start to back off. This
3120 * case may not be covered by the all_pinned logic if there
3121 * is only 1 task on the busy runqueue (because we don't call
3122 * move_tasks).
3123 */
3124 if (sd->balance_interval < sd->max_interval)
3125 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 }
3127
Peter Williams43010652007-08-09 11:16:46 +02003128 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003129 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003130 return -1;
Peter Williams43010652007-08-09 11:16:46 +02003131 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
3133out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 schedstat_inc(sd, lb_balanced[idle]);
3135
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003136 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003137
3138out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003140 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3141 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 sd->balance_interval *= 2;
3143
Ingo Molnar48f24c42006-07-03 00:25:40 -07003144 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003145 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003146 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 return 0;
3148}
3149
3150/*
3151 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3152 * tasks if there is an imbalance.
3153 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003154 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 * this_rq is locked.
3156 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003157static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07003158load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159{
3160 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003161 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003163 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003164 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003165 int all_pinned = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003166 cpumask_t cpus = CPU_MASK_ALL;
Nick Piggin5969fe02005-09-10 00:26:19 -07003167
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003168 /*
3169 * When power savings policy is enabled for the parent domain, idle
3170 * sibling can pick up load irrespective of busy siblings. In this case,
3171 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003172 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003173 */
3174 if (sd->flags & SD_SHARE_CPUPOWER &&
3175 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003176 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
Ingo Molnar2d723762007-10-15 17:00:12 +02003178 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003179redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003180 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003181 &sd_idle, &cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003183 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003184 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 }
3186
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003187 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003188 &cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003189 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003190 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003191 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 }
3193
Nick Piggindb935db2005-06-25 14:57:11 -07003194 BUG_ON(busiest == this_rq);
3195
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003196 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003197
Peter Williams43010652007-08-09 11:16:46 +02003198 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003199 if (busiest->nr_running > 1) {
3200 /* Attempt to move tasks */
3201 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003202 /* this_rq->clock is already updated */
3203 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003204 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003205 imbalance, sd, CPU_NEWLY_IDLE,
3206 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003207 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003208
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003209 if (unlikely(all_pinned)) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003210 cpu_clear(cpu_of(busiest), cpus);
3211 if (!cpus_empty(cpus))
3212 goto redo;
3213 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003214 }
3215
Peter Williams43010652007-08-09 11:16:46 +02003216 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003217 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003218 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3219 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003220 return -1;
3221 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003222 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
Peter Williams43010652007-08-09 11:16:46 +02003224 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003225
3226out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003227 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003228 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003229 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003230 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003231 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003232
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003233 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234}
3235
3236/*
3237 * idle_balance is called by schedule() if this_cpu is about to become
3238 * idle. Attempts to pull tasks from other CPUs.
3239 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003240static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241{
3242 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003243 int pulled_task = -1;
3244 unsigned long next_balance = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
3246 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003247 unsigned long interval;
3248
3249 if (!(sd->flags & SD_LOAD_BALANCE))
3250 continue;
3251
3252 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003253 /* If we've pulled tasks over stop searching: */
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003254 pulled_task = load_balance_newidle(this_cpu,
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003255 this_rq, sd);
3256
3257 interval = msecs_to_jiffies(sd->balance_interval);
3258 if (time_after(next_balance, sd->last_balance + interval))
3259 next_balance = sd->last_balance + interval;
3260 if (pulled_task)
3261 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003263 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003264 /*
3265 * We are going idle. next_balance may be set based on
3266 * a busy processor. So reset next_balance.
3267 */
3268 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270}
3271
3272/*
3273 * active_load_balance is run by migration threads. It pushes running tasks
3274 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3275 * running on each physical CPU where possible, and avoids physical /
3276 * logical imbalances.
3277 *
3278 * Called with busiest_rq locked.
3279 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003280static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281{
Nick Piggin39507452005-06-25 14:57:09 -07003282 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003283 struct sched_domain *sd;
3284 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003285
Ingo Molnar48f24c42006-07-03 00:25:40 -07003286 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003287 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003288 return;
3289
3290 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
3292 /*
Nick Piggin39507452005-06-25 14:57:09 -07003293 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003294 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003295 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 */
Nick Piggin39507452005-06-25 14:57:09 -07003297 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298
Nick Piggin39507452005-06-25 14:57:09 -07003299 /* move a task from busiest_rq to target_rq */
3300 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003301 update_rq_clock(busiest_rq);
3302 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
Nick Piggin39507452005-06-25 14:57:09 -07003304 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003305 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07003306 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003307 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07003308 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Ingo Molnar48f24c42006-07-03 00:25:40 -07003311 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003312 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313
Peter Williams43010652007-08-09 11:16:46 +02003314 if (move_one_task(target_rq, target_cpu, busiest_rq,
3315 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07003316 schedstat_inc(sd, alb_pushed);
3317 else
3318 schedstat_inc(sd, alb_failed);
3319 }
Nick Piggin39507452005-06-25 14:57:09 -07003320 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321}
3322
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003323#ifdef CONFIG_NO_HZ
3324static struct {
3325 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003326 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003327} nohz ____cacheline_aligned = {
3328 .load_balancer = ATOMIC_INIT(-1),
3329 .cpu_mask = CPU_MASK_NONE,
3330};
3331
Christoph Lameter7835b982006-12-10 02:20:22 -08003332/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003333 * This routine will try to nominate the ilb (idle load balancing)
3334 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3335 * load balancing on behalf of all those cpus. If all the cpus in the system
3336 * go into this tickless mode, then there will be no ilb owner (as there is
3337 * no need for one) and all the cpus will sleep till the next wakeup event
3338 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08003339 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003340 * For the ilb owner, tick is not stopped. And this tick will be used
3341 * for idle load balancing. ilb owner will still be part of
3342 * nohz.cpu_mask..
3343 *
3344 * While stopping the tick, this cpu will become the ilb owner if there
3345 * is no other owner. And will be the owner till that cpu becomes busy
3346 * or if all cpus in the system stop their ticks at which point
3347 * there is no need for ilb owner.
3348 *
3349 * When the ilb owner becomes busy, it nominates another owner, during the
3350 * next busy scheduler_tick()
3351 */
3352int select_nohz_load_balancer(int stop_tick)
3353{
3354 int cpu = smp_processor_id();
3355
3356 if (stop_tick) {
3357 cpu_set(cpu, nohz.cpu_mask);
3358 cpu_rq(cpu)->in_nohz_recently = 1;
3359
3360 /*
3361 * If we are going offline and still the leader, give up!
3362 */
3363 if (cpu_is_offline(cpu) &&
3364 atomic_read(&nohz.load_balancer) == cpu) {
3365 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3366 BUG();
3367 return 0;
3368 }
3369
3370 /* time for ilb owner also to sleep */
3371 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3372 if (atomic_read(&nohz.load_balancer) == cpu)
3373 atomic_set(&nohz.load_balancer, -1);
3374 return 0;
3375 }
3376
3377 if (atomic_read(&nohz.load_balancer) == -1) {
3378 /* make me the ilb owner */
3379 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
3380 return 1;
3381 } else if (atomic_read(&nohz.load_balancer) == cpu)
3382 return 1;
3383 } else {
3384 if (!cpu_isset(cpu, nohz.cpu_mask))
3385 return 0;
3386
3387 cpu_clear(cpu, nohz.cpu_mask);
3388
3389 if (atomic_read(&nohz.load_balancer) == cpu)
3390 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3391 BUG();
3392 }
3393 return 0;
3394}
3395#endif
3396
3397static DEFINE_SPINLOCK(balancing);
3398
3399/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003400 * It checks each scheduling domain to see if it is due to be balanced,
3401 * and initiates a balancing operation if so.
3402 *
3403 * Balancing parameters are set up in arch_init_sched_domains.
3404 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003405static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08003406{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003407 int balance = 1;
3408 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08003409 unsigned long interval;
3410 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003411 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08003412 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003413 int update_next_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003415 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 if (!(sd->flags & SD_LOAD_BALANCE))
3417 continue;
3418
3419 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003420 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 interval *= sd->busy_factor;
3422
3423 /* scale ms to jiffies */
3424 interval = msecs_to_jiffies(interval);
3425 if (unlikely(!interval))
3426 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003427 if (interval > HZ*NR_CPUS/10)
3428 interval = HZ*NR_CPUS/10;
3429
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Christoph Lameter08c183f2006-12-10 02:20:29 -08003431 if (sd->flags & SD_SERIALIZE) {
3432 if (!spin_trylock(&balancing))
3433 goto out;
3434 }
3435
Christoph Lameterc9819f42006-12-10 02:20:25 -08003436 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003437 if (load_balance(cpu, rq, sd, idle, &balance)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003438 /*
3439 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07003440 * longer idle, or one of our SMT siblings is
3441 * not idle.
3442 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003443 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003445 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08003447 if (sd->flags & SD_SERIALIZE)
3448 spin_unlock(&balancing);
3449out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02003450 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08003451 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003452 update_next_balance = 1;
3453 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003454
3455 /*
3456 * Stop the load balance at this level. There is another
3457 * CPU in our sched group which is doing load balancing more
3458 * actively.
3459 */
3460 if (!balance)
3461 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02003463
3464 /*
3465 * next_balance will be updated only when there is a need.
3466 * When the cpu is attached to null domain for ex, it will not be
3467 * updated.
3468 */
3469 if (likely(update_next_balance))
3470 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003471}
3472
3473/*
3474 * run_rebalance_domains is triggered when needed from the scheduler tick.
3475 * In CONFIG_NO_HZ case, the idle load balance owner will do the
3476 * rebalancing for all the cpus for whom scheduler ticks are stopped.
3477 */
3478static void run_rebalance_domains(struct softirq_action *h)
3479{
Ingo Molnardd41f592007-07-09 18:51:59 +02003480 int this_cpu = smp_processor_id();
3481 struct rq *this_rq = cpu_rq(this_cpu);
3482 enum cpu_idle_type idle = this_rq->idle_at_tick ?
3483 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003484
Ingo Molnardd41f592007-07-09 18:51:59 +02003485 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003486
3487#ifdef CONFIG_NO_HZ
3488 /*
3489 * If this cpu is the owner for idle load balancing, then do the
3490 * balancing on behalf of the other idle cpus whose ticks are
3491 * stopped.
3492 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003493 if (this_rq->idle_at_tick &&
3494 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003495 cpumask_t cpus = nohz.cpu_mask;
3496 struct rq *rq;
3497 int balance_cpu;
3498
Ingo Molnardd41f592007-07-09 18:51:59 +02003499 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003500 for_each_cpu_mask(balance_cpu, cpus) {
3501 /*
3502 * If this cpu gets work to do, stop the load balancing
3503 * work being done for other cpus. Next load
3504 * balancing owner will pick it up.
3505 */
3506 if (need_resched())
3507 break;
3508
Oleg Nesterovde0cf892007-08-12 18:08:19 +02003509 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003510
3511 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003512 if (time_after(this_rq->next_balance, rq->next_balance))
3513 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003514 }
3515 }
3516#endif
3517}
3518
3519/*
3520 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
3521 *
3522 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
3523 * idle load balancing owner or decide to stop the periodic load balancing,
3524 * if the whole system is idle.
3525 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003526static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003527{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003528#ifdef CONFIG_NO_HZ
3529 /*
3530 * If we were in the nohz mode recently and busy at the current
3531 * scheduler tick, then check if we need to nominate new idle
3532 * load balancer.
3533 */
3534 if (rq->in_nohz_recently && !rq->idle_at_tick) {
3535 rq->in_nohz_recently = 0;
3536
3537 if (atomic_read(&nohz.load_balancer) == cpu) {
3538 cpu_clear(cpu, nohz.cpu_mask);
3539 atomic_set(&nohz.load_balancer, -1);
3540 }
3541
3542 if (atomic_read(&nohz.load_balancer) == -1) {
3543 /*
3544 * simple selection for now: Nominate the
3545 * first cpu in the nohz list to be the next
3546 * ilb owner.
3547 *
3548 * TBD: Traverse the sched domains and nominate
3549 * the nearest cpu in the nohz.cpu_mask.
3550 */
3551 int ilb = first_cpu(nohz.cpu_mask);
3552
3553 if (ilb != NR_CPUS)
3554 resched_cpu(ilb);
3555 }
3556 }
3557
3558 /*
3559 * If this cpu is idle and doing idle load balancing for all the
3560 * cpus with ticks stopped, is it time for that to stop?
3561 */
3562 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
3563 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3564 resched_cpu(cpu);
3565 return;
3566 }
3567
3568 /*
3569 * If this cpu is idle and the idle load balancing is done by
3570 * someone else, then no need raise the SCHED_SOFTIRQ
3571 */
3572 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
3573 cpu_isset(cpu, nohz.cpu_mask))
3574 return;
3575#endif
3576 if (time_after_eq(jiffies, rq->next_balance))
3577 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578}
Ingo Molnardd41f592007-07-09 18:51:59 +02003579
3580#else /* CONFIG_SMP */
3581
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582/*
3583 * on UP we do not need to balance between CPUs:
3584 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003585static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586{
3587}
Ingo Molnardd41f592007-07-09 18:51:59 +02003588
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589#endif
3590
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591DEFINE_PER_CPU(struct kernel_stat, kstat);
3592
3593EXPORT_PER_CPU_SYMBOL(kstat);
3594
3595/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02003596 * Return p->sum_exec_runtime plus any more ns on the sched_clock
3597 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02003599unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003602 u64 ns, delta_exec;
3603 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003604
Ingo Molnar41b86e92007-07-09 18:51:58 +02003605 rq = task_rq_lock(p, &flags);
3606 ns = p->se.sum_exec_runtime;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01003607 if (task_current(rq, p)) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02003608 update_rq_clock(rq);
3609 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003610 if ((s64)delta_exec > 0)
3611 ns += delta_exec;
3612 }
3613 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003614
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 return ns;
3616}
3617
3618/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 * Account user cpu time to a process.
3620 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 * @cputime: the cpu time spent in user space since the last update
3622 */
3623void account_user_time(struct task_struct *p, cputime_t cputime)
3624{
3625 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3626 cputime64_t tmp;
3627
3628 p->utime = cputime_add(p->utime, cputime);
3629
3630 /* Add user time to cpustat. */
3631 tmp = cputime_to_cputime64(cputime);
3632 if (TASK_NICE(p) > 0)
3633 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3634 else
3635 cpustat->user = cputime64_add(cpustat->user, tmp);
3636}
3637
3638/*
Laurent Vivier94886b82007-10-15 17:00:19 +02003639 * Account guest cpu time to a process.
3640 * @p: the process that the cpu time gets accounted to
3641 * @cputime: the cpu time spent in virtual machine since the last update
3642 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01003643static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02003644{
3645 cputime64_t tmp;
3646 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3647
3648 tmp = cputime_to_cputime64(cputime);
3649
3650 p->utime = cputime_add(p->utime, cputime);
3651 p->gtime = cputime_add(p->gtime, cputime);
3652
3653 cpustat->user = cputime64_add(cpustat->user, tmp);
3654 cpustat->guest = cputime64_add(cpustat->guest, tmp);
3655}
3656
3657/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07003658 * Account scaled user cpu time to a process.
3659 * @p: the process that the cpu time gets accounted to
3660 * @cputime: the cpu time spent in user space since the last update
3661 */
3662void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
3663{
3664 p->utimescaled = cputime_add(p->utimescaled, cputime);
3665}
3666
3667/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 * Account system cpu time to a process.
3669 * @p: the process that the cpu time gets accounted to
3670 * @hardirq_offset: the offset to subtract from hardirq_count()
3671 * @cputime: the cpu time spent in kernel space since the last update
3672 */
3673void account_system_time(struct task_struct *p, int hardirq_offset,
3674 cputime_t cputime)
3675{
3676 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003677 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 cputime64_t tmp;
3679
Christian Borntraeger97783852007-11-15 20:57:39 +01003680 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0))
3681 return account_guest_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02003682
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 p->stime = cputime_add(p->stime, cputime);
3684
3685 /* Add system time to cpustat. */
3686 tmp = cputime_to_cputime64(cputime);
3687 if (hardirq_count() - hardirq_offset)
3688 cpustat->irq = cputime64_add(cpustat->irq, tmp);
3689 else if (softirq_count())
3690 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003691 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003693 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3695 else
3696 cpustat->idle = cputime64_add(cpustat->idle, tmp);
3697 /* Account for system time used */
3698 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699}
3700
3701/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07003702 * Account scaled system cpu time to a process.
3703 * @p: the process that the cpu time gets accounted to
3704 * @hardirq_offset: the offset to subtract from hardirq_count()
3705 * @cputime: the cpu time spent in kernel space since the last update
3706 */
3707void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
3708{
3709 p->stimescaled = cputime_add(p->stimescaled, cputime);
3710}
3711
3712/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 * Account for involuntary wait time.
3714 * @p: the process from which the cpu time has been stolen
3715 * @steal: the cpu time spent in involuntary wait
3716 */
3717void account_steal_time(struct task_struct *p, cputime_t steal)
3718{
3719 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3720 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07003721 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
3723 if (p == rq->idle) {
3724 p->stime = cputime_add(p->stime, steal);
3725 if (atomic_read(&rq->nr_iowait) > 0)
3726 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3727 else
3728 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003729 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 cpustat->steal = cputime64_add(cpustat->steal, tmp);
3731}
3732
Christoph Lameter7835b982006-12-10 02:20:22 -08003733/*
3734 * This function gets called by the timer code, with HZ frequency.
3735 * We call it with interrupts disabled.
3736 *
3737 * It also gets called by the fork code, when changing the parent's
3738 * timeslices.
3739 */
3740void scheduler_tick(void)
3741{
Christoph Lameter7835b982006-12-10 02:20:22 -08003742 int cpu = smp_processor_id();
3743 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003744 struct task_struct *curr = rq->curr;
Ingo Molnar529c7722007-08-10 23:05:11 +02003745 u64 next_tick = rq->tick_timestamp + TICK_NSEC;
Christoph Lameter7835b982006-12-10 02:20:22 -08003746
Ingo Molnardd41f592007-07-09 18:51:59 +02003747 spin_lock(&rq->lock);
Ingo Molnar546fe3c2007-08-09 11:16:51 +02003748 __update_rq_clock(rq);
Ingo Molnar529c7722007-08-10 23:05:11 +02003749 /*
3750 * Let rq->clock advance by at least TICK_NSEC:
3751 */
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01003752 if (unlikely(rq->clock < next_tick)) {
Ingo Molnar529c7722007-08-10 23:05:11 +02003753 rq->clock = next_tick;
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01003754 rq->clock_underflows++;
3755 }
Ingo Molnar529c7722007-08-10 23:05:11 +02003756 rq->tick_timestamp = rq->clock;
Ingo Molnarf1a438d2007-08-09 11:16:45 +02003757 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01003758 curr->sched_class->task_tick(rq, curr, 0);
3759 update_sched_rt_period(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003760 spin_unlock(&rq->lock);
3761
Christoph Lametere418e1c2006-12-10 02:20:23 -08003762#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02003763 rq->idle_at_tick = idle_cpu(cpu);
3764 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08003765#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766}
3767
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
3769
Srinivasa Ds43627582008-02-23 15:24:04 -08003770void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771{
3772 /*
3773 * Underflow?
3774 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003775 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
3776 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 preempt_count() += val;
3778 /*
3779 * Spinlock count overflowing soon?
3780 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003781 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
3782 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783}
3784EXPORT_SYMBOL(add_preempt_count);
3785
Srinivasa Ds43627582008-02-23 15:24:04 -08003786void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787{
3788 /*
3789 * Underflow?
3790 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003791 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
3792 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 /*
3794 * Is the spinlock portion underflowing?
3795 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003796 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
3797 !(preempt_count() & PREEMPT_MASK)))
3798 return;
3799
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 preempt_count() -= val;
3801}
3802EXPORT_SYMBOL(sub_preempt_count);
3803
3804#endif
3805
3806/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003807 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003809static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810{
Satyam Sharma838225b2007-10-24 18:23:50 +02003811 struct pt_regs *regs = get_irq_regs();
3812
3813 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
3814 prev->comm, prev->pid, preempt_count());
3815
Ingo Molnardd41f592007-07-09 18:51:59 +02003816 debug_show_held_locks(prev);
3817 if (irqs_disabled())
3818 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02003819
3820 if (regs)
3821 show_regs(regs);
3822 else
3823 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02003824}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825
Ingo Molnardd41f592007-07-09 18:51:59 +02003826/*
3827 * Various schedule()-time debugging checks and statistics:
3828 */
3829static inline void schedule_debug(struct task_struct *prev)
3830{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003832 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 * schedule() atomically, we ignore that path for now.
3834 * Otherwise, whine if we are scheduling when we should not be.
3835 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003836 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
3837 __schedule_bug(prev);
3838
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
3840
Ingo Molnar2d723762007-10-15 17:00:12 +02003841 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02003842#ifdef CONFIG_SCHEDSTATS
3843 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003844 schedstat_inc(this_rq(), bkl_count);
3845 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02003846 }
3847#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02003848}
3849
3850/*
3851 * Pick up the highest-prio task:
3852 */
3853static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02003854pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02003855{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003856 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02003857 struct task_struct *p;
3858
3859 /*
3860 * Optimization: we know that if all tasks are in
3861 * the fair class we can call that function directly:
3862 */
3863 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003864 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003865 if (likely(p))
3866 return p;
3867 }
3868
3869 class = sched_class_highest;
3870 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003871 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003872 if (p)
3873 return p;
3874 /*
3875 * Will never be NULL as the idle class always
3876 * returns a non-NULL p:
3877 */
3878 class = class->next;
3879 }
3880}
3881
3882/*
3883 * schedule() is the main scheduler function.
3884 */
3885asmlinkage void __sched schedule(void)
3886{
3887 struct task_struct *prev, *next;
3888 long *switch_count;
3889 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02003890 int cpu;
3891
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892need_resched:
3893 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02003894 cpu = smp_processor_id();
3895 rq = cpu_rq(cpu);
3896 rcu_qsctr_inc(cpu);
3897 prev = rq->curr;
3898 switch_count = &prev->nivcsw;
3899
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 release_kernel_lock(prev);
3901need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902
Ingo Molnardd41f592007-07-09 18:51:59 +02003903 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003905 hrtick_clear(rq);
3906
Ingo Molnar1e819952007-10-15 17:00:13 +02003907 /*
3908 * Do the rq-clock update outside the rq lock:
3909 */
3910 local_irq_disable();
Ingo Molnarc1b3da32007-08-09 11:16:47 +02003911 __update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02003912 spin_lock(&rq->lock);
3913 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914
Ingo Molnardd41f592007-07-09 18:51:59 +02003915 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
3916 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
3917 unlikely(signal_pending(prev)))) {
3918 prev->state = TASK_RUNNING;
3919 } else {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02003920 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02003921 }
3922 switch_count = &prev->nvcsw;
3923 }
3924
Steven Rostedt9a897c52008-01-25 21:08:22 +01003925#ifdef CONFIG_SMP
3926 if (prev->sched_class->pre_schedule)
3927 prev->sched_class->pre_schedule(rq, prev);
3928#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01003929
Ingo Molnardd41f592007-07-09 18:51:59 +02003930 if (unlikely(!rq->nr_running))
3931 idle_balance(cpu, rq);
3932
Ingo Molnar31ee5292007-08-09 11:16:49 +02003933 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02003934 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935
3936 sched_info_switch(prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02003937
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 rq->nr_switches++;
3940 rq->curr = next;
3941 ++*switch_count;
3942
Ingo Molnardd41f592007-07-09 18:51:59 +02003943 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003944 /*
3945 * the context switch might have flipped the stack from under
3946 * us, hence refresh the local variables.
3947 */
3948 cpu = smp_processor_id();
3949 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 } else
3951 spin_unlock_irq(&rq->lock);
3952
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003953 hrtick_set(rq);
3954
3955 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003957
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 preempt_enable_no_resched();
3959 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
3960 goto need_resched;
3961}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962EXPORT_SYMBOL(schedule);
3963
3964#ifdef CONFIG_PREEMPT
3965/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003966 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003967 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 * occur there and call schedule directly.
3969 */
3970asmlinkage void __sched preempt_schedule(void)
3971{
3972 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 struct task_struct *task = current;
3974 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01003975
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 /*
3977 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003978 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07003980 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 return;
3982
Andi Kleen3a5c3592007-10-15 17:00:14 +02003983 do {
3984 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
Andi Kleen3a5c3592007-10-15 17:00:14 +02003986 /*
3987 * We keep the big kernel semaphore locked, but we
3988 * clear ->lock_depth so that schedule() doesnt
3989 * auto-release the semaphore:
3990 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02003991 saved_lock_depth = task->lock_depth;
3992 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02003993 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02003994 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02003995 sub_preempt_count(PREEMPT_ACTIVE);
3996
3997 /*
3998 * Check again in case we missed a preemption opportunity
3999 * between schedule and now.
4000 */
4001 barrier();
4002 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004EXPORT_SYMBOL(preempt_schedule);
4005
4006/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004007 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 * off of irq context.
4009 * Note, that this is called and return with irqs disabled. This will
4010 * protect us against recursive calling from irq.
4011 */
4012asmlinkage void __sched preempt_schedule_irq(void)
4013{
4014 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 struct task_struct *task = current;
4016 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004017
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004018 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 BUG_ON(ti->preempt_count || !irqs_disabled());
4020
Andi Kleen3a5c3592007-10-15 17:00:14 +02004021 do {
4022 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
Andi Kleen3a5c3592007-10-15 17:00:14 +02004024 /*
4025 * We keep the big kernel semaphore locked, but we
4026 * clear ->lock_depth so that schedule() doesnt
4027 * auto-release the semaphore:
4028 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004029 saved_lock_depth = task->lock_depth;
4030 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004031 local_irq_enable();
4032 schedule();
4033 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004034 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004035 sub_preempt_count(PREEMPT_ACTIVE);
4036
4037 /*
4038 * Check again in case we missed a preemption opportunity
4039 * between schedule and now.
4040 */
4041 barrier();
4042 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043}
4044
4045#endif /* CONFIG_PREEMPT */
4046
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004047int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4048 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004050 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052EXPORT_SYMBOL(default_wake_function);
4053
4054/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004055 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4056 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 * number) then we wake all the non-exclusive tasks and one exclusive task.
4058 *
4059 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004060 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4062 */
4063static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4064 int nr_exclusive, int sync, void *key)
4065{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004066 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004068 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004069 unsigned flags = curr->flags;
4070
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004072 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 break;
4074 }
4075}
4076
4077/**
4078 * __wake_up - wake up threads blocked on a waitqueue.
4079 * @q: the waitqueue
4080 * @mode: which threads
4081 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004082 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004084void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004085 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086{
4087 unsigned long flags;
4088
4089 spin_lock_irqsave(&q->lock, flags);
4090 __wake_up_common(q, mode, nr_exclusive, 0, key);
4091 spin_unlock_irqrestore(&q->lock, flags);
4092}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093EXPORT_SYMBOL(__wake_up);
4094
4095/*
4096 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4097 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004098void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099{
4100 __wake_up_common(q, mode, 1, 0, NULL);
4101}
4102
4103/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004104 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 * @q: the waitqueue
4106 * @mode: which threads
4107 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4108 *
4109 * The sync wakeup differs that the waker knows that it will schedule
4110 * away soon, so while the target thread will be woken up, it will not
4111 * be migrated to another CPU - ie. the two threads are 'synchronized'
4112 * with each other. This can prevent needless bouncing between CPUs.
4113 *
4114 * On UP it can prevent extra preemption.
4115 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004116void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004117__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118{
4119 unsigned long flags;
4120 int sync = 1;
4121
4122 if (unlikely(!q))
4123 return;
4124
4125 if (unlikely(!nr_exclusive))
4126 sync = 0;
4127
4128 spin_lock_irqsave(&q->lock, flags);
4129 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4130 spin_unlock_irqrestore(&q->lock, flags);
4131}
4132EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4133
Ingo Molnarb15136e2007-10-24 18:23:48 +02004134void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135{
4136 unsigned long flags;
4137
4138 spin_lock_irqsave(&x->wait.lock, flags);
4139 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004140 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 spin_unlock_irqrestore(&x->wait.lock, flags);
4142}
4143EXPORT_SYMBOL(complete);
4144
Ingo Molnarb15136e2007-10-24 18:23:48 +02004145void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146{
4147 unsigned long flags;
4148
4149 spin_lock_irqsave(&x->wait.lock, flags);
4150 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004151 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 spin_unlock_irqrestore(&x->wait.lock, flags);
4153}
4154EXPORT_SYMBOL(complete_all);
4155
Andi Kleen8cbbe862007-10-15 17:00:14 +02004156static inline long __sched
4157do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 if (!x->done) {
4160 DECLARE_WAITQUEUE(wait, current);
4161
4162 wait.flags |= WQ_FLAG_EXCLUSIVE;
4163 __add_wait_queue_tail(&x->wait, &wait);
4164 do {
Matthew Wilcox009e5772007-12-06 12:29:54 -05004165 if ((state == TASK_INTERRUPTIBLE &&
4166 signal_pending(current)) ||
4167 (state == TASK_KILLABLE &&
4168 fatal_signal_pending(current))) {
Andi Kleen8cbbe862007-10-15 17:00:14 +02004169 __remove_wait_queue(&x->wait, &wait);
4170 return -ERESTARTSYS;
4171 }
4172 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004174 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 spin_lock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004176 if (!timeout) {
4177 __remove_wait_queue(&x->wait, &wait);
4178 return timeout;
4179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 } while (!x->done);
4181 __remove_wait_queue(&x->wait, &wait);
4182 }
4183 x->done--;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004184 return timeout;
4185}
4186
4187static long __sched
4188wait_for_common(struct completion *x, long timeout, int state)
4189{
4190 might_sleep();
4191
4192 spin_lock_irq(&x->wait.lock);
4193 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004195 return timeout;
4196}
4197
Ingo Molnarb15136e2007-10-24 18:23:48 +02004198void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004199{
4200 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201}
4202EXPORT_SYMBOL(wait_for_completion);
4203
Ingo Molnarb15136e2007-10-24 18:23:48 +02004204unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4206{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004207 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208}
4209EXPORT_SYMBOL(wait_for_completion_timeout);
4210
Andi Kleen8cbbe862007-10-15 17:00:14 +02004211int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212{
Andi Kleen51e97992007-10-18 21:32:55 +02004213 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4214 if (t == -ERESTARTSYS)
4215 return t;
4216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217}
4218EXPORT_SYMBOL(wait_for_completion_interruptible);
4219
Ingo Molnarb15136e2007-10-24 18:23:48 +02004220unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221wait_for_completion_interruptible_timeout(struct completion *x,
4222 unsigned long timeout)
4223{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004224 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225}
4226EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4227
Matthew Wilcox009e5772007-12-06 12:29:54 -05004228int __sched wait_for_completion_killable(struct completion *x)
4229{
4230 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4231 if (t == -ERESTARTSYS)
4232 return t;
4233 return 0;
4234}
4235EXPORT_SYMBOL(wait_for_completion_killable);
4236
Andi Kleen8cbbe862007-10-15 17:00:14 +02004237static long __sched
4238sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004239{
4240 unsigned long flags;
4241 wait_queue_t wait;
4242
4243 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244
Andi Kleen8cbbe862007-10-15 17:00:14 +02004245 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
Andi Kleen8cbbe862007-10-15 17:00:14 +02004247 spin_lock_irqsave(&q->lock, flags);
4248 __add_wait_queue(q, &wait);
4249 spin_unlock(&q->lock);
4250 timeout = schedule_timeout(timeout);
4251 spin_lock_irq(&q->lock);
4252 __remove_wait_queue(q, &wait);
4253 spin_unlock_irqrestore(&q->lock, flags);
4254
4255 return timeout;
4256}
4257
4258void __sched interruptible_sleep_on(wait_queue_head_t *q)
4259{
4260 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262EXPORT_SYMBOL(interruptible_sleep_on);
4263
Ingo Molnar0fec1712007-07-09 18:52:01 +02004264long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004265interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004267 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4270
Ingo Molnar0fec1712007-07-09 18:52:01 +02004271void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004273 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275EXPORT_SYMBOL(sleep_on);
4276
Ingo Molnar0fec1712007-07-09 18:52:01 +02004277long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004279 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281EXPORT_SYMBOL(sleep_on_timeout);
4282
Ingo Molnarb29739f2006-06-27 02:54:51 -07004283#ifdef CONFIG_RT_MUTEXES
4284
4285/*
4286 * rt_mutex_setprio - set the current priority of a task
4287 * @p: task
4288 * @prio: prio value (kernel-internal form)
4289 *
4290 * This function changes the 'effective' priority of a task. It does
4291 * not touch ->normal_prio like __setscheduler().
4292 *
4293 * Used by the rt_mutex code to implement priority inheritance logic.
4294 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004295void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004296{
4297 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004298 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004299 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01004300 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004301
4302 BUG_ON(prio < 0 || prio > MAX_PRIO);
4303
4304 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004305 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004306
Andrew Mortond5f9f942007-05-08 20:27:06 -07004307 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004308 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004309 running = task_current(rq, p);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004310 if (on_rq) {
Ingo Molnar69be72c2007-08-09 11:16:49 +02004311 dequeue_task(rq, p, 0);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004312 if (running)
4313 p->sched_class->put_prev_task(rq, p);
4314 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004315
4316 if (rt_prio(prio))
4317 p->sched_class = &rt_sched_class;
4318 else
4319 p->sched_class = &fair_sched_class;
4320
Ingo Molnarb29739f2006-06-27 02:54:51 -07004321 p->prio = prio;
4322
Ingo Molnardd41f592007-07-09 18:51:59 +02004323 if (on_rq) {
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004324 if (running)
4325 p->sched_class->set_curr_task(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01004326
Ingo Molnar8159f872007-08-09 11:16:49 +02004327 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004328
4329 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004330 }
4331 task_rq_unlock(rq, &flags);
4332}
4333
4334#endif
4335
Ingo Molnar36c8b582006-07-03 00:25:41 -07004336void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337{
Ingo Molnardd41f592007-07-09 18:51:59 +02004338 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004340 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341
4342 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
4343 return;
4344 /*
4345 * We have to be careful, if called from sys_setpriority(),
4346 * the task might be in the middle of scheduling on another CPU.
4347 */
4348 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004349 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 /*
4351 * The RT priorities are set via sched_setscheduler(), but we still
4352 * allow the 'normal' nice value to be set - but as expected
4353 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02004354 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 */
Ingo Molnare05606d2007-07-09 18:51:59 +02004356 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 p->static_prio = NICE_TO_PRIO(nice);
4358 goto out_unlock;
4359 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004360 on_rq = p->se.on_rq;
Srivatsa Vaddagiri58e2d4c2008-01-25 21:08:00 +01004361 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004362 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07004365 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004366 old_prio = p->prio;
4367 p->prio = effective_prio(p);
4368 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369
Ingo Molnardd41f592007-07-09 18:51:59 +02004370 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004371 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07004373 * If the task increased its priority or is running and
4374 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004376 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 resched_task(rq->curr);
4378 }
4379out_unlock:
4380 task_rq_unlock(rq, &flags);
4381}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382EXPORT_SYMBOL(set_user_nice);
4383
Matt Mackalle43379f2005-05-01 08:59:00 -07004384/*
4385 * can_nice - check if a task can reduce its nice value
4386 * @p: task
4387 * @nice: nice value
4388 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004389int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07004390{
Matt Mackall024f4742005-08-18 11:24:19 -07004391 /* convert nice value [19,-20] to rlimit style value [1,40] */
4392 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004393
Matt Mackalle43379f2005-05-01 08:59:00 -07004394 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
4395 capable(CAP_SYS_NICE));
4396}
4397
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398#ifdef __ARCH_WANT_SYS_NICE
4399
4400/*
4401 * sys_nice - change the priority of the current process.
4402 * @increment: priority increment
4403 *
4404 * sys_setpriority is a more generic, but much slower function that
4405 * does similar things.
4406 */
4407asmlinkage long sys_nice(int increment)
4408{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004409 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
4411 /*
4412 * Setpriority might change our priority at the same moment.
4413 * We don't have to worry. Conceptually one call occurs first
4414 * and we have a single winner.
4415 */
Matt Mackalle43379f2005-05-01 08:59:00 -07004416 if (increment < -40)
4417 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 if (increment > 40)
4419 increment = 40;
4420
4421 nice = PRIO_TO_NICE(current->static_prio) + increment;
4422 if (nice < -20)
4423 nice = -20;
4424 if (nice > 19)
4425 nice = 19;
4426
Matt Mackalle43379f2005-05-01 08:59:00 -07004427 if (increment < 0 && !can_nice(current, nice))
4428 return -EPERM;
4429
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 retval = security_task_setnice(current, nice);
4431 if (retval)
4432 return retval;
4433
4434 set_user_nice(current, nice);
4435 return 0;
4436}
4437
4438#endif
4439
4440/**
4441 * task_prio - return the priority value of a given task.
4442 * @p: the task in question.
4443 *
4444 * This is the priority value as seen by users in /proc.
4445 * RT tasks are offset by -200. Normal tasks are centered
4446 * around 0, value goes from -16 to +15.
4447 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004448int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449{
4450 return p->prio - MAX_RT_PRIO;
4451}
4452
4453/**
4454 * task_nice - return the nice value of a given task.
4455 * @p: the task in question.
4456 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004457int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458{
4459 return TASK_NICE(p);
4460}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461EXPORT_SYMBOL_GPL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
4463/**
4464 * idle_cpu - is a given cpu idle currently?
4465 * @cpu: the processor in question.
4466 */
4467int idle_cpu(int cpu)
4468{
4469 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
4470}
4471
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472/**
4473 * idle_task - return the idle task for a given cpu.
4474 * @cpu: the processor in question.
4475 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004476struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477{
4478 return cpu_rq(cpu)->idle;
4479}
4480
4481/**
4482 * find_process_by_pid - find a process with a matching PID value.
4483 * @pid: the pid in question.
4484 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004485static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07004487 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488}
4489
4490/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02004491static void
4492__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493{
Ingo Molnardd41f592007-07-09 18:51:59 +02004494 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004495
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02004497 switch (p->policy) {
4498 case SCHED_NORMAL:
4499 case SCHED_BATCH:
4500 case SCHED_IDLE:
4501 p->sched_class = &fair_sched_class;
4502 break;
4503 case SCHED_FIFO:
4504 case SCHED_RR:
4505 p->sched_class = &rt_sched_class;
4506 break;
4507 }
4508
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004510 p->normal_prio = normal_prio(p);
4511 /* we are holding p->pi_lock already */
4512 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07004513 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514}
4515
4516/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004517 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 * @p: the task in question.
4519 * @policy: new policy.
4520 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004521 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004522 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004524int sched_setscheduler(struct task_struct *p, int policy,
4525 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004527 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01004529 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004530 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531
Steven Rostedt66e53932006-06-27 02:54:44 -07004532 /* may grab non-irq protected spin_locks */
4533 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534recheck:
4535 /* double check policy once rq lock held */
4536 if (policy < 0)
4537 policy = oldpolicy = p->policy;
4538 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02004539 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
4540 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08004541 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 /*
4543 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02004544 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
4545 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 */
4547 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004548 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04004549 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02004551 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 return -EINVAL;
4553
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004554 /*
4555 * Allow unprivileged RT tasks to decrease priority:
4556 */
4557 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02004558 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004559 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004560
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004561 if (!lock_task_sighand(p, &flags))
4562 return -ESRCH;
4563 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
4564 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004565
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004566 /* can't set/change the rt policy */
4567 if (policy != p->policy && !rlim_rtprio)
4568 return -EPERM;
4569
4570 /* can't increase priority */
4571 if (param->sched_priority > p->rt_priority &&
4572 param->sched_priority > rlim_rtprio)
4573 return -EPERM;
4574 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004575 /*
4576 * Like positive nice levels, dont allow tasks to
4577 * move out of SCHED_IDLE either:
4578 */
4579 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
4580 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004581
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004582 /* can't change other user's priorities */
4583 if ((current->euid != p->euid) &&
4584 (current->euid != p->uid))
4585 return -EPERM;
4586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
Peter Zijlstrab68aa232008-02-13 15:45:40 +01004588#ifdef CONFIG_RT_GROUP_SCHED
4589 /*
4590 * Do not allow realtime tasks into groups that have no runtime
4591 * assigned.
4592 */
4593 if (rt_policy(policy) && task_group(p)->rt_runtime == 0)
4594 return -EPERM;
4595#endif
4596
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 retval = security_task_setscheduler(p, policy, param);
4598 if (retval)
4599 return retval;
4600 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07004601 * make sure no PI-waiters arrive (or leave) while we are
4602 * changing the priority of the task:
4603 */
4604 spin_lock_irqsave(&p->pi_lock, flags);
4605 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 * To be able to change p->policy safely, the apropriate
4607 * runqueue lock must be held.
4608 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07004609 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 /* recheck policy now with rq lock held */
4611 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
4612 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004613 __task_rq_unlock(rq);
4614 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 goto recheck;
4616 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02004617 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004618 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004619 running = task_current(rq, p);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004620 if (on_rq) {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004621 deactivate_task(rq, p, 0);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004622 if (running)
4623 p->sched_class->put_prev_task(rq, p);
4624 }
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02004625
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004627 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02004628
Ingo Molnardd41f592007-07-09 18:51:59 +02004629 if (on_rq) {
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004630 if (running)
4631 p->sched_class->set_curr_task(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01004632
Ingo Molnardd41f592007-07-09 18:51:59 +02004633 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004634
4635 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07004637 __task_rq_unlock(rq);
4638 spin_unlock_irqrestore(&p->pi_lock, flags);
4639
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07004640 rt_mutex_adjust_pi(p);
4641
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 return 0;
4643}
4644EXPORT_SYMBOL_GPL(sched_setscheduler);
4645
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004646static int
4647do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 struct sched_param lparam;
4650 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004651 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652
4653 if (!param || pid < 0)
4654 return -EINVAL;
4655 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
4656 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004657
4658 rcu_read_lock();
4659 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004661 if (p != NULL)
4662 retval = sched_setscheduler(p, policy, &lparam);
4663 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07004664
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 return retval;
4666}
4667
4668/**
4669 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4670 * @pid: the pid in question.
4671 * @policy: new policy.
4672 * @param: structure containing the new RT priority.
4673 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004674asmlinkage long
4675sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676{
Jason Baronc21761f2006-01-18 17:43:03 -08004677 /* negative values for policy are not valid */
4678 if (policy < 0)
4679 return -EINVAL;
4680
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 return do_sched_setscheduler(pid, policy, param);
4682}
4683
4684/**
4685 * sys_sched_setparam - set/change the RT priority of a thread
4686 * @pid: the pid in question.
4687 * @param: structure containing the new RT priority.
4688 */
4689asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
4690{
4691 return do_sched_setscheduler(pid, -1, param);
4692}
4693
4694/**
4695 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4696 * @pid: the pid in question.
4697 */
4698asmlinkage long sys_sched_getscheduler(pid_t pid)
4699{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004700 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004701 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702
4703 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004704 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
4706 retval = -ESRCH;
4707 read_lock(&tasklist_lock);
4708 p = find_process_by_pid(pid);
4709 if (p) {
4710 retval = security_task_getscheduler(p);
4711 if (!retval)
4712 retval = p->policy;
4713 }
4714 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 return retval;
4716}
4717
4718/**
4719 * sys_sched_getscheduler - get the RT priority of a thread
4720 * @pid: the pid in question.
4721 * @param: structure containing the RT priority.
4722 */
4723asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
4724{
4725 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004726 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004727 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728
4729 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004730 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731
4732 read_lock(&tasklist_lock);
4733 p = find_process_by_pid(pid);
4734 retval = -ESRCH;
4735 if (!p)
4736 goto out_unlock;
4737
4738 retval = security_task_getscheduler(p);
4739 if (retval)
4740 goto out_unlock;
4741
4742 lp.sched_priority = p->rt_priority;
4743 read_unlock(&tasklist_lock);
4744
4745 /*
4746 * This one might sleep, we cannot do it with a spinlock held ...
4747 */
4748 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
4749
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 return retval;
4751
4752out_unlock:
4753 read_unlock(&tasklist_lock);
4754 return retval;
4755}
4756
4757long sched_setaffinity(pid_t pid, cpumask_t new_mask)
4758{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 cpumask_t cpus_allowed;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004760 struct task_struct *p;
4761 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004763 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 read_lock(&tasklist_lock);
4765
4766 p = find_process_by_pid(pid);
4767 if (!p) {
4768 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004769 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 return -ESRCH;
4771 }
4772
4773 /*
4774 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004775 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 * usage count and then drop tasklist_lock.
4777 */
4778 get_task_struct(p);
4779 read_unlock(&tasklist_lock);
4780
4781 retval = -EPERM;
4782 if ((current->euid != p->euid) && (current->euid != p->uid) &&
4783 !capable(CAP_SYS_NICE))
4784 goto out_unlock;
4785
David Quigleye7834f82006-06-23 02:03:59 -07004786 retval = security_task_setscheduler(p, 0, NULL);
4787 if (retval)
4788 goto out_unlock;
4789
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 cpus_allowed = cpuset_cpus_allowed(p);
4791 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07004792 again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 retval = set_cpus_allowed(p, new_mask);
4794
Paul Menage8707d8b2007-10-18 23:40:22 -07004795 if (!retval) {
4796 cpus_allowed = cpuset_cpus_allowed(p);
4797 if (!cpus_subset(new_mask, cpus_allowed)) {
4798 /*
4799 * We must have raced with a concurrent cpuset
4800 * update. Just reset the cpus_allowed to the
4801 * cpuset's cpus_allowed
4802 */
4803 new_mask = cpus_allowed;
4804 goto again;
4805 }
4806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807out_unlock:
4808 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004809 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 return retval;
4811}
4812
4813static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
4814 cpumask_t *new_mask)
4815{
4816 if (len < sizeof(cpumask_t)) {
4817 memset(new_mask, 0, sizeof(cpumask_t));
4818 } else if (len > sizeof(cpumask_t)) {
4819 len = sizeof(cpumask_t);
4820 }
4821 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
4822}
4823
4824/**
4825 * sys_sched_setaffinity - set the cpu affinity of a process
4826 * @pid: pid of the process
4827 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4828 * @user_mask_ptr: user-space pointer to the new cpu mask
4829 */
4830asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
4831 unsigned long __user *user_mask_ptr)
4832{
4833 cpumask_t new_mask;
4834 int retval;
4835
4836 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
4837 if (retval)
4838 return retval;
4839
4840 return sched_setaffinity(pid, new_mask);
4841}
4842
4843/*
4844 * Represents all cpu's present in the system
4845 * In systems capable of hotplug, this map could dynamically grow
4846 * as new cpu's are detected in the system via any platform specific
4847 * method, such as ACPI for e.g.
4848 */
4849
Andi Kleen4cef0c62006-01-11 22:44:57 +01004850cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851EXPORT_SYMBOL(cpu_present_map);
4852
4853#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01004854cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07004855EXPORT_SYMBOL(cpu_online_map);
4856
Andi Kleen4cef0c62006-01-11 22:44:57 +01004857cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07004858EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859#endif
4860
4861long sched_getaffinity(pid_t pid, cpumask_t *mask)
4862{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004863 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004866 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 read_lock(&tasklist_lock);
4868
4869 retval = -ESRCH;
4870 p = find_process_by_pid(pid);
4871 if (!p)
4872 goto out_unlock;
4873
David Quigleye7834f82006-06-23 02:03:59 -07004874 retval = security_task_getscheduler(p);
4875 if (retval)
4876 goto out_unlock;
4877
Jack Steiner2f7016d2006-02-01 03:05:18 -08004878 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
4880out_unlock:
4881 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004882 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883
Ulrich Drepper9531b622007-08-09 11:16:46 +02004884 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885}
4886
4887/**
4888 * sys_sched_getaffinity - get the cpu affinity of a process
4889 * @pid: pid of the process
4890 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4891 * @user_mask_ptr: user-space pointer to hold the current cpu mask
4892 */
4893asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
4894 unsigned long __user *user_mask_ptr)
4895{
4896 int ret;
4897 cpumask_t mask;
4898
4899 if (len < sizeof(cpumask_t))
4900 return -EINVAL;
4901
4902 ret = sched_getaffinity(pid, &mask);
4903 if (ret < 0)
4904 return ret;
4905
4906 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
4907 return -EFAULT;
4908
4909 return sizeof(cpumask_t);
4910}
4911
4912/**
4913 * sys_sched_yield - yield the current processor to other threads.
4914 *
Ingo Molnardd41f592007-07-09 18:51:59 +02004915 * This function yields the current CPU to other tasks. If there are no
4916 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 */
4918asmlinkage long sys_sched_yield(void)
4919{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004920 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921
Ingo Molnar2d723762007-10-15 17:00:12 +02004922 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02004923 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
4925 /*
4926 * Since we are going to call schedule() anyway, there's
4927 * no need to preempt or enable interrupts:
4928 */
4929 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07004930 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 _raw_spin_unlock(&rq->lock);
4932 preempt_enable_no_resched();
4933
4934 schedule();
4935
4936 return 0;
4937}
4938
Andrew Mortone7b38402006-06-30 01:56:00 -07004939static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07004941#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
4942 __might_sleep(__FILE__, __LINE__);
4943#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07004944 /*
4945 * The BKS might be reacquired before we have dropped
4946 * PREEMPT_ACTIVE, which could trigger a second
4947 * cond_resched() call.
4948 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 do {
4950 add_preempt_count(PREEMPT_ACTIVE);
4951 schedule();
4952 sub_preempt_count(PREEMPT_ACTIVE);
4953 } while (need_resched());
4954}
4955
Herbert Xu02b67cc2008-01-25 21:08:28 +01004956#if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY)
4957int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958{
Ingo Molnar94142322006-12-29 16:48:13 -08004959 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
4960 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 __cond_resched();
4962 return 1;
4963 }
4964 return 0;
4965}
Herbert Xu02b67cc2008-01-25 21:08:28 +01004966EXPORT_SYMBOL(_cond_resched);
4967#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968
4969/*
4970 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
4971 * call schedule, and on return reacquire the lock.
4972 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004973 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 * operations here to prevent schedule() from being called twice (once via
4975 * spin_unlock(), once by hand).
4976 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004977int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978{
Nick Piggin95c354f2008-01-30 13:31:20 +01004979 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07004980 int ret = 0;
4981
Nick Piggin95c354f2008-01-30 13:31:20 +01004982 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01004984 if (resched && need_resched())
4985 __cond_resched();
4986 else
4987 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07004988 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 }
Jan Kara6df3cec2005-06-13 15:52:32 -07004991 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993EXPORT_SYMBOL(cond_resched_lock);
4994
4995int __sched cond_resched_softirq(void)
4996{
4997 BUG_ON(!in_softirq());
4998
Ingo Molnar94142322006-12-29 16:48:13 -08004999 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07005000 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 __cond_resched();
5002 local_bh_disable();
5003 return 1;
5004 }
5005 return 0;
5006}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007EXPORT_SYMBOL(cond_resched_softirq);
5008
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009/**
5010 * yield - yield the current processor to other threads.
5011 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005012 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 * thread runnable and calls sys_sched_yield().
5014 */
5015void __sched yield(void)
5016{
5017 set_current_state(TASK_RUNNING);
5018 sys_sched_yield();
5019}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020EXPORT_SYMBOL(yield);
5021
5022/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005023 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 * that process accounting knows that this is a task in IO wait state.
5025 *
5026 * But don't do that if it is a deliberate, throttling IO wait (this task
5027 * has set its backing_dev_info: the queue against which it should throttle)
5028 */
5029void __sched io_schedule(void)
5030{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005031 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005033 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 atomic_inc(&rq->nr_iowait);
5035 schedule();
5036 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005037 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039EXPORT_SYMBOL(io_schedule);
5040
5041long __sched io_schedule_timeout(long timeout)
5042{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005043 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 long ret;
5045
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005046 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 atomic_inc(&rq->nr_iowait);
5048 ret = schedule_timeout(timeout);
5049 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005050 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 return ret;
5052}
5053
5054/**
5055 * sys_sched_get_priority_max - return maximum RT priority.
5056 * @policy: scheduling class.
5057 *
5058 * this syscall returns the maximum rt_priority that can be used
5059 * by a given scheduling class.
5060 */
5061asmlinkage long sys_sched_get_priority_max(int policy)
5062{
5063 int ret = -EINVAL;
5064
5065 switch (policy) {
5066 case SCHED_FIFO:
5067 case SCHED_RR:
5068 ret = MAX_USER_RT_PRIO-1;
5069 break;
5070 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005071 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005072 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 ret = 0;
5074 break;
5075 }
5076 return ret;
5077}
5078
5079/**
5080 * sys_sched_get_priority_min - return minimum RT priority.
5081 * @policy: scheduling class.
5082 *
5083 * this syscall returns the minimum rt_priority that can be used
5084 * by a given scheduling class.
5085 */
5086asmlinkage long sys_sched_get_priority_min(int policy)
5087{
5088 int ret = -EINVAL;
5089
5090 switch (policy) {
5091 case SCHED_FIFO:
5092 case SCHED_RR:
5093 ret = 1;
5094 break;
5095 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005096 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005097 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 ret = 0;
5099 }
5100 return ret;
5101}
5102
5103/**
5104 * sys_sched_rr_get_interval - return the default timeslice of a process.
5105 * @pid: pid of the process.
5106 * @interval: userspace pointer to the timeslice value.
5107 *
5108 * this syscall writes the default timeslice value of a given process
5109 * into the user-space timespec buffer. A value of '0' means infinity.
5110 */
5111asmlinkage
5112long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5113{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005114 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005115 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005116 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118
5119 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005120 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121
5122 retval = -ESRCH;
5123 read_lock(&tasklist_lock);
5124 p = find_process_by_pid(pid);
5125 if (!p)
5126 goto out_unlock;
5127
5128 retval = security_task_getscheduler(p);
5129 if (retval)
5130 goto out_unlock;
5131
Ingo Molnar77034932007-12-04 17:04:39 +01005132 /*
5133 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5134 * tasks that are on an otherwise idle runqueue:
5135 */
5136 time_slice = 0;
5137 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005138 time_slice = DEF_TIMESLICE;
Ingo Molnar77034932007-12-04 17:04:39 +01005139 } else {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005140 struct sched_entity *se = &p->se;
5141 unsigned long flags;
5142 struct rq *rq;
5143
5144 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005145 if (rq->cfs.load.weight)
5146 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005147 task_rq_unlock(rq, &flags);
5148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005150 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005153
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154out_unlock:
5155 read_unlock(&tasklist_lock);
5156 return retval;
5157}
5158
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005159static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07005160
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005161void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005164 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005167 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005168 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005169#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005171 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005173 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174#else
5175 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005176 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005178 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179#endif
5180#ifdef CONFIG_DEBUG_STACK_USAGE
5181 {
Al Viro10ebffd2005-11-13 16:06:56 -08005182 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 while (!*n)
5184 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005185 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 }
5187#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005188 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005189 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005191 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192}
5193
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005194void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005196 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197
Ingo Molnar4bd77322007-07-11 21:21:47 +02005198#if BITS_PER_LONG == 32
5199 printk(KERN_INFO
5200 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005202 printk(KERN_INFO
5203 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204#endif
5205 read_lock(&tasklist_lock);
5206 do_each_thread(g, p) {
5207 /*
5208 * reset the NMI-timeout, listing all files on a slow
5209 * console might take alot of time:
5210 */
5211 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005212 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005213 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 } while_each_thread(g, p);
5215
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005216 touch_all_softlockup_watchdogs();
5217
Ingo Molnardd41f592007-07-09 18:51:59 +02005218#ifdef CONFIG_SCHED_DEBUG
5219 sysrq_sched_debug_show();
5220#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005222 /*
5223 * Only show locks if all tasks are dumped:
5224 */
5225 if (state_filter == -1)
5226 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227}
5228
Ingo Molnar1df21052007-07-09 18:51:58 +02005229void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5230{
Ingo Molnardd41f592007-07-09 18:51:59 +02005231 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005232}
5233
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005234/**
5235 * init_idle - set up an idle thread for a given CPU
5236 * @idle: task in question
5237 * @cpu: cpu the idle task belongs to
5238 *
5239 * NOTE: this function does not set the idle thread's NEED_RESCHED
5240 * flag, to make booting more robust.
5241 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005242void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005244 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 unsigned long flags;
5246
Ingo Molnardd41f592007-07-09 18:51:59 +02005247 __sched_fork(idle);
5248 idle->se.exec_start = sched_clock();
5249
Ingo Molnarb29739f2006-06-27 02:54:51 -07005250 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005252 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253
5254 spin_lock_irqsave(&rq->lock, flags);
5255 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005256#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5257 idle->oncpu = 1;
5258#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 spin_unlock_irqrestore(&rq->lock, flags);
5260
5261 /* Set the preempt count _outside_ the spinlocks! */
Al Viroa1261f52005-11-13 16:06:55 -08005262 task_thread_info(idle)->preempt_count = 0;
Ingo Molnar6478d882008-01-25 21:08:33 +01005263
Ingo Molnardd41f592007-07-09 18:51:59 +02005264 /*
5265 * The idle tasks have their own, simple scheduling class:
5266 */
5267 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268}
5269
5270/*
5271 * In a system that switches off the HZ timer nohz_cpu_mask
5272 * indicates which cpus entered this state. This is used
5273 * in the rcu update to wait only for active cpus. For system
5274 * which do not switch off the HZ timer nohz_cpu_mask should
5275 * always be CPU_MASK_NONE.
5276 */
5277cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
5278
Ingo Molnar19978ca2007-11-09 22:39:38 +01005279/*
5280 * Increase the granularity value when there are more CPUs,
5281 * because with more CPUs the 'effective latency' as visible
5282 * to users decreases. But the relationship is not linear,
5283 * so pick a second-best guess by going with the log2 of the
5284 * number of CPUs.
5285 *
5286 * This idea comes from the SD scheduler of Con Kolivas:
5287 */
5288static inline void sched_init_granularity(void)
5289{
5290 unsigned int factor = 1 + ilog2(num_online_cpus());
5291 const unsigned long limit = 200000000;
5292
5293 sysctl_sched_min_granularity *= factor;
5294 if (sysctl_sched_min_granularity > limit)
5295 sysctl_sched_min_granularity = limit;
5296
5297 sysctl_sched_latency *= factor;
5298 if (sysctl_sched_latency > limit)
5299 sysctl_sched_latency = limit;
5300
5301 sysctl_sched_wakeup_granularity *= factor;
5302 sysctl_sched_batch_wakeup_granularity *= factor;
5303}
5304
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305#ifdef CONFIG_SMP
5306/*
5307 * This is how migration works:
5308 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07005309 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 * runqueue and wake up that CPU's migration thread.
5311 * 2) we down() the locked semaphore => thread blocks.
5312 * 3) migration thread wakes up (implicitly it forces the migrated
5313 * thread off the CPU)
5314 * 4) it gets the migration request and checks whether the migrated
5315 * task is still in the wrong runqueue.
5316 * 5) if it's in the wrong runqueue then the migration thread removes
5317 * it and puts it into the right queue.
5318 * 6) migration thread up()s the semaphore.
5319 * 7) we wake up and the migration is done.
5320 */
5321
5322/*
5323 * Change a given task's CPU affinity. Migrate the thread to a
5324 * proper CPU and schedule it away if the CPU it's executing on
5325 * is removed from the allowed bitmask.
5326 *
5327 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005328 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 * call is not atomic; no spinlocks may be held.
5330 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005331int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005333 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005335 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005336 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337
5338 rq = task_rq_lock(p, &flags);
5339 if (!cpus_intersects(new_mask, cpu_online_map)) {
5340 ret = -EINVAL;
5341 goto out;
5342 }
5343
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005344 if (p->sched_class->set_cpus_allowed)
5345 p->sched_class->set_cpus_allowed(p, &new_mask);
5346 else {
Ingo Molnar0eab9142008-01-25 21:08:19 +01005347 p->cpus_allowed = new_mask;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01005348 p->rt.nr_cpus_allowed = cpus_weight(new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005349 }
5350
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 /* Can the task run on the task's current CPU? If so, we're done */
5352 if (cpu_isset(task_cpu(p), new_mask))
5353 goto out;
5354
5355 if (migrate_task(p, any_online_cpu(new_mask), &req)) {
5356 /* Need help from migration thread: drop lock and wait. */
5357 task_rq_unlock(rq, &flags);
5358 wake_up_process(rq->migration_thread);
5359 wait_for_completion(&req.done);
5360 tlb_migrate_finish(p->mm);
5361 return 0;
5362 }
5363out:
5364 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005365
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 return ret;
5367}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368EXPORT_SYMBOL_GPL(set_cpus_allowed);
5369
5370/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005371 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 * this because either it can't run here any more (set_cpus_allowed()
5373 * away from this CPU, or CPU going down), or because we're
5374 * attempting to rebalance this task on exec (sched_exec).
5375 *
5376 * So we race with normal scheduler movements, but that's OK, as long
5377 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07005378 *
5379 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07005381static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005383 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02005384 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385
5386 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07005387 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388
5389 rq_src = cpu_rq(src_cpu);
5390 rq_dest = cpu_rq(dest_cpu);
5391
5392 double_rq_lock(rq_src, rq_dest);
5393 /* Already moved. */
5394 if (task_cpu(p) != src_cpu)
5395 goto out;
5396 /* Affinity changed (again). */
5397 if (!cpu_isset(dest_cpu, p->cpus_allowed))
5398 goto out;
5399
Ingo Molnardd41f592007-07-09 18:51:59 +02005400 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005401 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005402 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005403
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005405 if (on_rq) {
5406 activate_task(rq_dest, p, 0);
5407 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07005409 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410out:
5411 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07005412 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413}
5414
5415/*
5416 * migration_thread - this is a highprio system thread that performs
5417 * thread migration by bumping thread off CPU then 'pushing' onto
5418 * another runqueue.
5419 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005420static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005423 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424
5425 rq = cpu_rq(cpu);
5426 BUG_ON(rq->migration_thread != current);
5427
5428 set_current_state(TASK_INTERRUPTIBLE);
5429 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005430 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 spin_lock_irq(&rq->lock);
5434
5435 if (cpu_is_offline(cpu)) {
5436 spin_unlock_irq(&rq->lock);
5437 goto wait_to_die;
5438 }
5439
5440 if (rq->active_balance) {
5441 active_load_balance(rq, cpu);
5442 rq->active_balance = 0;
5443 }
5444
5445 head = &rq->migration_queue;
5446
5447 if (list_empty(head)) {
5448 spin_unlock_irq(&rq->lock);
5449 schedule();
5450 set_current_state(TASK_INTERRUPTIBLE);
5451 continue;
5452 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07005453 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 list_del_init(head->next);
5455
Nick Piggin674311d2005-06-25 14:57:27 -07005456 spin_unlock(&rq->lock);
5457 __migrate_task(req->task, cpu, req->dest_cpu);
5458 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459
5460 complete(&req->done);
5461 }
5462 __set_current_state(TASK_RUNNING);
5463 return 0;
5464
5465wait_to_die:
5466 /* Wait for kthread_stop */
5467 set_current_state(TASK_INTERRUPTIBLE);
5468 while (!kthread_should_stop()) {
5469 schedule();
5470 set_current_state(TASK_INTERRUPTIBLE);
5471 }
5472 __set_current_state(TASK_RUNNING);
5473 return 0;
5474}
5475
5476#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005477
5478static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
5479{
5480 int ret;
5481
5482 local_irq_disable();
5483 ret = __migrate_task(p, src_cpu, dest_cpu);
5484 local_irq_enable();
5485 return ret;
5486}
5487
Kirill Korotaev054b9102006-12-10 02:20:11 -08005488/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02005489 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08005490 * NOTE: interrupts should be disabled by the caller
5491 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005492static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493{
Kirill Korotaevefc30812006-06-27 02:54:32 -07005494 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005496 struct rq *rq;
5497 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498
Andi Kleen3a5c3592007-10-15 17:00:14 +02005499 do {
5500 /* On same node? */
5501 mask = node_to_cpumask(cpu_to_node(dead_cpu));
5502 cpus_and(mask, mask, p->cpus_allowed);
5503 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504
Andi Kleen3a5c3592007-10-15 17:00:14 +02005505 /* On any allowed CPU? */
5506 if (dest_cpu == NR_CPUS)
5507 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508
Andi Kleen3a5c3592007-10-15 17:00:14 +02005509 /* No more Mr. Nice Guy. */
5510 if (dest_cpu == NR_CPUS) {
Cliff Wickman470fd642007-10-18 23:40:46 -07005511 cpumask_t cpus_allowed = cpuset_cpus_allowed_locked(p);
5512 /*
5513 * Try to stay on the same cpuset, where the
5514 * current cpuset may be a subset of all cpus.
5515 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005516 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07005517 * called within calls to cpuset_lock/cpuset_unlock.
5518 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02005519 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07005520 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005521 dest_cpu = any_online_cpu(p->cpus_allowed);
5522 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523
Andi Kleen3a5c3592007-10-15 17:00:14 +02005524 /*
5525 * Don't tell them about moving exiting tasks or
5526 * kernel threads (both mm NULL), since they never
5527 * leave kernel.
5528 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005529 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02005530 printk(KERN_INFO "process %d (%s) no "
5531 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005532 task_pid_nr(p), p->comm, dead_cpu);
5533 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02005534 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005535 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536}
5537
5538/*
5539 * While a dead CPU has no uninterruptible tasks queued at this point,
5540 * it might still have a nonzero ->nr_uninterruptible counter, because
5541 * for performance reasons the counter is not stricly tracking tasks to
5542 * their home CPUs. So we just add the counter to another CPU's counter,
5543 * to keep the global sum constant after CPU-down:
5544 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07005545static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005547 struct rq *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 unsigned long flags;
5549
5550 local_irq_save(flags);
5551 double_rq_lock(rq_src, rq_dest);
5552 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
5553 rq_src->nr_uninterruptible = 0;
5554 double_rq_unlock(rq_src, rq_dest);
5555 local_irq_restore(flags);
5556}
5557
5558/* Run through task list and migrate tasks from the dead cpu. */
5559static void migrate_live_tasks(int src_cpu)
5560{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005561 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005563 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564
Ingo Molnar48f24c42006-07-03 00:25:40 -07005565 do_each_thread(t, p) {
5566 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 continue;
5568
Ingo Molnar48f24c42006-07-03 00:25:40 -07005569 if (task_cpu(p) == src_cpu)
5570 move_task_off_dead_cpu(src_cpu, p);
5571 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005573 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574}
5575
Ingo Molnardd41f592007-07-09 18:51:59 +02005576/*
5577 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005578 * It does so by boosting its priority to highest possible.
5579 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 */
5581void sched_idle_next(void)
5582{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005583 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07005584 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 struct task_struct *p = rq->idle;
5586 unsigned long flags;
5587
5588 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005589 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590
Ingo Molnar48f24c42006-07-03 00:25:40 -07005591 /*
5592 * Strictly not necessary since rest of the CPUs are stopped by now
5593 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 */
5595 spin_lock_irqsave(&rq->lock, flags);
5596
Ingo Molnardd41f592007-07-09 18:51:59 +02005597 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005598
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005599 update_rq_clock(rq);
5600 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601
5602 spin_unlock_irqrestore(&rq->lock, flags);
5603}
5604
Ingo Molnar48f24c42006-07-03 00:25:40 -07005605/*
5606 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 * offline.
5608 */
5609void idle_task_exit(void)
5610{
5611 struct mm_struct *mm = current->active_mm;
5612
5613 BUG_ON(cpu_online(smp_processor_id()));
5614
5615 if (mm != &init_mm)
5616 switch_mm(mm, &init_mm, current);
5617 mmdrop(mm);
5618}
5619
Kirill Korotaev054b9102006-12-10 02:20:11 -08005620/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005621static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005623 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624
5625 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07005626 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627
5628 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07005629 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630
Ingo Molnar48f24c42006-07-03 00:25:40 -07005631 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632
5633 /*
5634 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005635 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 * fine.
5637 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005638 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005639 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005640 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641
Ingo Molnar48f24c42006-07-03 00:25:40 -07005642 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643}
5644
5645/* release_task() removes task from tasklist, so we won't find dead tasks. */
5646static void migrate_dead_tasks(unsigned int dead_cpu)
5647{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005648 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005649 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650
Ingo Molnardd41f592007-07-09 18:51:59 +02005651 for ( ; ; ) {
5652 if (!rq->nr_running)
5653 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02005654 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02005655 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02005656 if (!next)
5657 break;
5658 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02005659
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 }
5661}
5662#endif /* CONFIG_HOTPLUG_CPU */
5663
Nick Piggine692ab52007-07-26 13:40:43 +02005664#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5665
5666static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005667 {
5668 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005669 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005670 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01005671 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02005672};
5673
5674static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005675 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005676 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005677 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005678 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005679 .child = sd_ctl_dir,
5680 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01005681 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02005682};
5683
5684static struct ctl_table *sd_alloc_ctl_entry(int n)
5685{
5686 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02005687 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02005688
Nick Piggine692ab52007-07-26 13:40:43 +02005689 return entry;
5690}
5691
Milton Miller6382bc92007-10-15 17:00:19 +02005692static void sd_free_ctl_entry(struct ctl_table **tablep)
5693{
Milton Millercd790072007-10-17 16:55:11 +02005694 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02005695
Milton Millercd790072007-10-17 16:55:11 +02005696 /*
5697 * In the intermediate directories, both the child directory and
5698 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005699 * will always be set. In the lowest directory the names are
Milton Millercd790072007-10-17 16:55:11 +02005700 * static strings and all have proc handlers.
5701 */
5702 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02005703 if (entry->child)
5704 sd_free_ctl_entry(&entry->child);
Milton Millercd790072007-10-17 16:55:11 +02005705 if (entry->proc_handler == NULL)
5706 kfree(entry->procname);
5707 }
Milton Miller6382bc92007-10-15 17:00:19 +02005708
5709 kfree(*tablep);
5710 *tablep = NULL;
5711}
5712
Nick Piggine692ab52007-07-26 13:40:43 +02005713static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02005714set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02005715 const char *procname, void *data, int maxlen,
5716 mode_t mode, proc_handler *proc_handler)
5717{
Nick Piggine692ab52007-07-26 13:40:43 +02005718 entry->procname = procname;
5719 entry->data = data;
5720 entry->maxlen = maxlen;
5721 entry->mode = mode;
5722 entry->proc_handler = proc_handler;
5723}
5724
5725static struct ctl_table *
5726sd_alloc_ctl_domain_table(struct sched_domain *sd)
5727{
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005728 struct ctl_table *table = sd_alloc_ctl_entry(12);
Nick Piggine692ab52007-07-26 13:40:43 +02005729
Milton Millerad1cdc12007-10-15 17:00:19 +02005730 if (table == NULL)
5731 return NULL;
5732
Alexey Dobriyane0361852007-08-09 11:16:46 +02005733 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005734 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005735 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005736 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005737 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005738 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005739 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005740 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005741 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005742 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005743 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005744 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005745 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005746 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005747 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02005748 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005749 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02005750 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005751 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02005752 &sd->cache_nice_tries,
5753 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005754 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02005755 sizeof(int), 0644, proc_dointvec_minmax);
Milton Miller6323469f2007-10-15 17:00:19 +02005756 /* &table[11] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02005757
5758 return table;
5759}
5760
Ingo Molnar9a4e7152007-11-28 15:52:56 +01005761static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02005762{
5763 struct ctl_table *entry, *table;
5764 struct sched_domain *sd;
5765 int domain_num = 0, i;
5766 char buf[32];
5767
5768 for_each_domain(cpu, sd)
5769 domain_num++;
5770 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02005771 if (table == NULL)
5772 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02005773
5774 i = 0;
5775 for_each_domain(cpu, sd) {
5776 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005777 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005778 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005779 entry->child = sd_alloc_ctl_domain_table(sd);
5780 entry++;
5781 i++;
5782 }
5783 return table;
5784}
5785
5786static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02005787static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005788{
5789 int i, cpu_num = num_online_cpus();
5790 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
5791 char buf[32];
5792
Milton Miller73785472007-10-24 18:23:48 +02005793 WARN_ON(sd_ctl_dir[0].child);
5794 sd_ctl_dir[0].child = entry;
5795
Milton Millerad1cdc12007-10-15 17:00:19 +02005796 if (entry == NULL)
5797 return;
5798
Milton Miller97b6ea72007-10-15 17:00:19 +02005799 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02005800 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005801 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005802 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005803 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02005804 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02005805 }
Milton Miller73785472007-10-24 18:23:48 +02005806
5807 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02005808 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
5809}
Milton Miller6382bc92007-10-15 17:00:19 +02005810
Milton Miller73785472007-10-24 18:23:48 +02005811/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02005812static void unregister_sched_domain_sysctl(void)
5813{
Milton Miller73785472007-10-24 18:23:48 +02005814 if (sd_sysctl_header)
5815 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02005816 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02005817 if (sd_ctl_dir[0].child)
5818 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02005819}
Nick Piggine692ab52007-07-26 13:40:43 +02005820#else
Milton Miller6382bc92007-10-15 17:00:19 +02005821static void register_sched_domain_sysctl(void)
5822{
5823}
5824static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005825{
5826}
5827#endif
5828
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829/*
5830 * migration_call - callback that gets triggered when a CPU is added.
5831 * Here we can start up the necessary migration thread for the new CPU.
5832 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005833static int __cpuinit
5834migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005837 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005839 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840
5841 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005842
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005844 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02005845 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 if (IS_ERR(p))
5847 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 kthread_bind(p, cpu);
5849 /* Must be high prio: stop_machine expects to yield to it. */
5850 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02005851 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 task_rq_unlock(rq, &flags);
5853 cpu_rq(cpu)->migration_thread = p;
5854 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005855
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005857 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02005858 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005860
5861 /* Update our root-domain */
5862 rq = cpu_rq(cpu);
5863 spin_lock_irqsave(&rq->lock, flags);
5864 if (rq->rd) {
5865 BUG_ON(!cpu_isset(cpu, rq->rd->span));
5866 cpu_set(cpu, rq->rd->online);
5867 }
5868 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005870
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871#ifdef CONFIG_HOTPLUG_CPU
5872 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005873 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07005874 if (!cpu_rq(cpu)->migration_thread)
5875 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005876 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08005877 kthread_bind(cpu_rq(cpu)->migration_thread,
5878 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 kthread_stop(cpu_rq(cpu)->migration_thread);
5880 cpu_rq(cpu)->migration_thread = NULL;
5881 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005882
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005884 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07005885 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 migrate_live_tasks(cpu);
5887 rq = cpu_rq(cpu);
5888 kthread_stop(rq->migration_thread);
5889 rq->migration_thread = NULL;
5890 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07005891 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005892 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005893 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02005895 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
5896 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07005898 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07005899 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 migrate_nr_uninterruptible(rq);
5901 BUG_ON(rq->nr_running != 0);
5902
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005903 /*
5904 * No need to migrate the tasks: it was best-effort if
5905 * they didn't take sched_hotcpu_mutex. Just wake up
5906 * the requestors.
5907 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 spin_lock_irq(&rq->lock);
5909 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005910 struct migration_req *req;
5911
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07005913 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 list_del_init(&req->list);
5915 complete(&req->done);
5916 }
5917 spin_unlock_irq(&rq->lock);
5918 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005919
5920 case CPU_DOWN_PREPARE:
5921 /* Update our root-domain */
5922 rq = cpu_rq(cpu);
5923 spin_lock_irqsave(&rq->lock, flags);
5924 if (rq->rd) {
5925 BUG_ON(!cpu_isset(cpu, rq->rd->span));
5926 cpu_clear(cpu, rq->rd->online);
5927 }
5928 spin_unlock_irqrestore(&rq->lock, flags);
5929 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930#endif
5931 }
5932 return NOTIFY_OK;
5933}
5934
5935/* Register at highest priority so that task migration (migrate_all_tasks)
5936 * happens before everything else.
5937 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07005938static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 .notifier_call = migration_call,
5940 .priority = 10
5941};
5942
Adrian Bunke6fe6642007-11-09 22:39:39 +01005943void __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944{
5945 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07005946 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005947
5948 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07005949 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
5950 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 migration_call(&migration_notifier, CPU_ONLINE, cpu);
5952 register_cpu_notifier(&migration_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953}
5954#endif
5955
5956#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07005957
5958/* Number of possible processor ids */
5959int nr_cpu_ids __read_mostly = NR_CPUS;
5960EXPORT_SYMBOL(nr_cpu_ids);
5961
Ingo Molnar3e9830d2007-10-15 17:00:13 +02005962#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005963
5964static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level)
5965{
5966 struct sched_group *group = sd->groups;
5967 cpumask_t groupmask;
5968 char str[NR_CPUS];
5969
5970 cpumask_scnprintf(str, NR_CPUS, sd->span);
5971 cpus_clear(groupmask);
5972
5973 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
5974
5975 if (!(sd->flags & SD_LOAD_BALANCE)) {
5976 printk("does not load-balance\n");
5977 if (sd->parent)
5978 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
5979 " has parent");
5980 return -1;
5981 }
5982
5983 printk(KERN_CONT "span %s\n", str);
5984
5985 if (!cpu_isset(cpu, sd->span)) {
5986 printk(KERN_ERR "ERROR: domain->span does not contain "
5987 "CPU%d\n", cpu);
5988 }
5989 if (!cpu_isset(cpu, group->cpumask)) {
5990 printk(KERN_ERR "ERROR: domain->groups does not contain"
5991 " CPU%d\n", cpu);
5992 }
5993
5994 printk(KERN_DEBUG "%*s groups:", level + 1, "");
5995 do {
5996 if (!group) {
5997 printk("\n");
5998 printk(KERN_ERR "ERROR: group is NULL\n");
5999 break;
6000 }
6001
6002 if (!group->__cpu_power) {
6003 printk(KERN_CONT "\n");
6004 printk(KERN_ERR "ERROR: domain->cpu_power not "
6005 "set\n");
6006 break;
6007 }
6008
6009 if (!cpus_weight(group->cpumask)) {
6010 printk(KERN_CONT "\n");
6011 printk(KERN_ERR "ERROR: empty group\n");
6012 break;
6013 }
6014
6015 if (cpus_intersects(groupmask, group->cpumask)) {
6016 printk(KERN_CONT "\n");
6017 printk(KERN_ERR "ERROR: repeated CPUs\n");
6018 break;
6019 }
6020
6021 cpus_or(groupmask, groupmask, group->cpumask);
6022
6023 cpumask_scnprintf(str, NR_CPUS, group->cpumask);
6024 printk(KERN_CONT " %s", str);
6025
6026 group = group->next;
6027 } while (group != sd->groups);
6028 printk(KERN_CONT "\n");
6029
6030 if (!cpus_equal(sd->span, groupmask))
6031 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6032
6033 if (sd->parent && !cpus_subset(groupmask, sd->parent->span))
6034 printk(KERN_ERR "ERROR: parent span is not a superset "
6035 "of domain->span\n");
6036 return 0;
6037}
6038
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039static void sched_domain_debug(struct sched_domain *sd, int cpu)
6040{
6041 int level = 0;
6042
Nick Piggin41c7ce92005-06-25 14:57:24 -07006043 if (!sd) {
6044 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6045 return;
6046 }
6047
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6049
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006050 for (;;) {
6051 if (sched_domain_debug_one(sd, cpu, level))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 level++;
6054 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006055 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006056 break;
6057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058}
6059#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07006060# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061#endif
6062
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006063static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006064{
6065 if (cpus_weight(sd->span) == 1)
6066 return 1;
6067
6068 /* Following flags need at least 2 groups */
6069 if (sd->flags & (SD_LOAD_BALANCE |
6070 SD_BALANCE_NEWIDLE |
6071 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006072 SD_BALANCE_EXEC |
6073 SD_SHARE_CPUPOWER |
6074 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006075 if (sd->groups != sd->groups->next)
6076 return 0;
6077 }
6078
6079 /* Following flags don't use groups */
6080 if (sd->flags & (SD_WAKE_IDLE |
6081 SD_WAKE_AFFINE |
6082 SD_WAKE_BALANCE))
6083 return 0;
6084
6085 return 1;
6086}
6087
Ingo Molnar48f24c42006-07-03 00:25:40 -07006088static int
6089sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006090{
6091 unsigned long cflags = sd->flags, pflags = parent->flags;
6092
6093 if (sd_degenerate(parent))
6094 return 1;
6095
6096 if (!cpus_equal(sd->span, parent->span))
6097 return 0;
6098
6099 /* Does parent contain flags not in child? */
6100 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6101 if (cflags & SD_WAKE_AFFINE)
6102 pflags &= ~SD_WAKE_BALANCE;
6103 /* Flags needing groups don't count if only 1 group in parent */
6104 if (parent->groups == parent->groups->next) {
6105 pflags &= ~(SD_LOAD_BALANCE |
6106 SD_BALANCE_NEWIDLE |
6107 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006108 SD_BALANCE_EXEC |
6109 SD_SHARE_CPUPOWER |
6110 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006111 }
6112 if (~cflags & pflags)
6113 return 0;
6114
6115 return 1;
6116}
6117
Gregory Haskins57d885f2008-01-25 21:08:18 +01006118static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6119{
6120 unsigned long flags;
6121 const struct sched_class *class;
6122
6123 spin_lock_irqsave(&rq->lock, flags);
6124
6125 if (rq->rd) {
6126 struct root_domain *old_rd = rq->rd;
6127
Ingo Molnar0eab9142008-01-25 21:08:19 +01006128 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006129 if (class->leave_domain)
6130 class->leave_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006131 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006132
Gregory Haskinsdc938522008-01-25 21:08:26 +01006133 cpu_clear(rq->cpu, old_rd->span);
6134 cpu_clear(rq->cpu, old_rd->online);
6135
Gregory Haskins57d885f2008-01-25 21:08:18 +01006136 if (atomic_dec_and_test(&old_rd->refcount))
6137 kfree(old_rd);
6138 }
6139
6140 atomic_inc(&rd->refcount);
6141 rq->rd = rd;
6142
Gregory Haskinsdc938522008-01-25 21:08:26 +01006143 cpu_set(rq->cpu, rd->span);
6144 if (cpu_isset(rq->cpu, cpu_online_map))
6145 cpu_set(rq->cpu, rd->online);
6146
Ingo Molnar0eab9142008-01-25 21:08:19 +01006147 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006148 if (class->join_domain)
6149 class->join_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006150 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006151
6152 spin_unlock_irqrestore(&rq->lock, flags);
6153}
6154
Gregory Haskinsdc938522008-01-25 21:08:26 +01006155static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006156{
6157 memset(rd, 0, sizeof(*rd));
6158
Gregory Haskinsdc938522008-01-25 21:08:26 +01006159 cpus_clear(rd->span);
6160 cpus_clear(rd->online);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006161}
6162
6163static void init_defrootdomain(void)
6164{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006165 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006166 atomic_set(&def_root_domain.refcount, 1);
6167}
6168
Gregory Haskinsdc938522008-01-25 21:08:26 +01006169static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006170{
6171 struct root_domain *rd;
6172
6173 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6174 if (!rd)
6175 return NULL;
6176
Gregory Haskinsdc938522008-01-25 21:08:26 +01006177 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006178
6179 return rd;
6180}
6181
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006183 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184 * hold the hotplug lock.
6185 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006186static void
6187cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006189 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006190 struct sched_domain *tmp;
6191
6192 /* Remove the sched domains which do not contribute to scheduling. */
6193 for (tmp = sd; tmp; tmp = tmp->parent) {
6194 struct sched_domain *parent = tmp->parent;
6195 if (!parent)
6196 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006197 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006198 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006199 if (parent->parent)
6200 parent->parent->child = tmp;
6201 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07006202 }
6203
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006204 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006205 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006206 if (sd)
6207 sd->child = NULL;
6208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209
6210 sched_domain_debug(sd, cpu);
6211
Gregory Haskins57d885f2008-01-25 21:08:18 +01006212 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006213 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214}
6215
6216/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006217static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218
6219/* Setup the mask of cpus configured for isolated domains */
6220static int __init isolated_cpu_setup(char *str)
6221{
6222 int ints[NR_CPUS], i;
6223
6224 str = get_options(str, ARRAY_SIZE(ints), ints);
6225 cpus_clear(cpu_isolated_map);
6226 for (i = 1; i <= ints[0]; i++)
6227 if (ints[i] < NR_CPUS)
6228 cpu_set(ints[i], cpu_isolated_map);
6229 return 1;
6230}
6231
Ingo Molnar8927f492007-10-15 17:00:13 +02006232__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233
6234/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006235 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6236 * to a function which identifies what group(along with sched group) a CPU
6237 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6238 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 *
6240 * init_sched_build_groups will build a circular linked list of the groups
6241 * covered by the given span, and will set each group's ->cpumask correctly,
6242 * and ->cpu_power to 0.
6243 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006244static void
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006245init_sched_build_groups(cpumask_t span, const cpumask_t *cpu_map,
6246 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
6247 struct sched_group **sg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248{
6249 struct sched_group *first = NULL, *last = NULL;
6250 cpumask_t covered = CPU_MASK_NONE;
6251 int i;
6252
6253 for_each_cpu_mask(i, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006254 struct sched_group *sg;
6255 int group = group_fn(i, cpu_map, &sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256 int j;
6257
6258 if (cpu_isset(i, covered))
6259 continue;
6260
6261 sg->cpumask = CPU_MASK_NONE;
Eric Dumazet5517d862007-05-08 00:32:57 -07006262 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263
6264 for_each_cpu_mask(j, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006265 if (group_fn(j, cpu_map, NULL) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266 continue;
6267
6268 cpu_set(j, covered);
6269 cpu_set(j, sg->cpumask);
6270 }
6271 if (!first)
6272 first = sg;
6273 if (last)
6274 last->next = sg;
6275 last = sg;
6276 }
6277 last->next = first;
6278}
6279
John Hawkes9c1cfda2005-09-06 15:18:14 -07006280#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281
John Hawkes9c1cfda2005-09-06 15:18:14 -07006282#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006283
John Hawkes9c1cfda2005-09-06 15:18:14 -07006284/**
6285 * find_next_best_node - find the next node to include in a sched_domain
6286 * @node: node whose sched_domain we're building
6287 * @used_nodes: nodes already in the sched_domain
6288 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006289 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006290 * finds the closest node not already in the @used_nodes map.
6291 *
6292 * Should use nodemask_t.
6293 */
6294static int find_next_best_node(int node, unsigned long *used_nodes)
6295{
6296 int i, n, val, min_val, best_node = 0;
6297
6298 min_val = INT_MAX;
6299
6300 for (i = 0; i < MAX_NUMNODES; i++) {
6301 /* Start at @node */
6302 n = (node + i) % MAX_NUMNODES;
6303
6304 if (!nr_cpus_node(n))
6305 continue;
6306
6307 /* Skip already used nodes */
6308 if (test_bit(n, used_nodes))
6309 continue;
6310
6311 /* Simple min distance search */
6312 val = node_distance(node, n);
6313
6314 if (val < min_val) {
6315 min_val = val;
6316 best_node = n;
6317 }
6318 }
6319
6320 set_bit(best_node, used_nodes);
6321 return best_node;
6322}
6323
6324/**
6325 * sched_domain_node_span - get a cpumask for a node's sched_domain
6326 * @node: node whose cpumask we're constructing
6327 * @size: number of nodes to include in this span
6328 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006329 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07006330 * should be one that prevents unnecessary balancing, but also spreads tasks
6331 * out optimally.
6332 */
6333static cpumask_t sched_domain_node_span(int node)
6334{
John Hawkes9c1cfda2005-09-06 15:18:14 -07006335 DECLARE_BITMAP(used_nodes, MAX_NUMNODES);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006336 cpumask_t span, nodemask;
6337 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006338
6339 cpus_clear(span);
6340 bitmap_zero(used_nodes, MAX_NUMNODES);
6341
6342 nodemask = node_to_cpumask(node);
6343 cpus_or(span, span, nodemask);
6344 set_bit(node, used_nodes);
6345
6346 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
6347 int next_node = find_next_best_node(node, used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006348
John Hawkes9c1cfda2005-09-06 15:18:14 -07006349 nodemask = node_to_cpumask(next_node);
6350 cpus_or(span, span, nodemask);
6351 }
6352
6353 return span;
6354}
6355#endif
6356
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006357int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006358
John Hawkes9c1cfda2005-09-06 15:18:14 -07006359/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07006360 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07006361 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362#ifdef CONFIG_SCHED_SMT
6363static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006364static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006365
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006366static int
6367cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006369 if (sg)
6370 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 return cpu;
6372}
6373#endif
6374
Ingo Molnar48f24c42006-07-03 00:25:40 -07006375/*
6376 * multi-core sched-domains:
6377 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006378#ifdef CONFIG_SCHED_MC
6379static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006380static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006381#endif
6382
6383#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006384static int
6385cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006386{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006387 int group;
Mike Travisd5a74302007-10-16 01:24:05 -07006388 cpumask_t mask = per_cpu(cpu_sibling_map, cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006389 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006390 group = first_cpu(mask);
6391 if (sg)
6392 *sg = &per_cpu(sched_group_core, group);
6393 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006394}
6395#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006396static int
6397cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006398{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006399 if (sg)
6400 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006401 return cpu;
6402}
6403#endif
6404
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006406static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006407
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006408static int
6409cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006411 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006412#ifdef CONFIG_SCHED_MC
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006413 cpumask_t mask = cpu_coregroup_map(cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006414 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006415 group = first_cpu(mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006416#elif defined(CONFIG_SCHED_SMT)
Mike Travisd5a74302007-10-16 01:24:05 -07006417 cpumask_t mask = per_cpu(cpu_sibling_map, cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006418 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006419 group = first_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006421 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006423 if (sg)
6424 *sg = &per_cpu(sched_group_phys, group);
6425 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426}
6427
6428#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07006429/*
6430 * The init_sched_build_groups can't handle what we want to do with node
6431 * groups, so roll our own. Now each node has its own list of groups which
6432 * gets dynamically allocated.
6433 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434static DEFINE_PER_CPU(struct sched_domain, node_domains);
John Hawkesd1b55132005-09-06 15:18:14 -07006435static struct sched_group **sched_group_nodes_bycpu[NR_CPUS];
John Hawkes9c1cfda2005-09-06 15:18:14 -07006436
6437static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006438static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006439
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006440static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
6441 struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006443 cpumask_t nodemask = node_to_cpumask(cpu_to_node(cpu));
6444 int group;
6445
6446 cpus_and(nodemask, nodemask, *cpu_map);
6447 group = first_cpu(nodemask);
6448
6449 if (sg)
6450 *sg = &per_cpu(sched_group_allnodes, group);
6451 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006453
Siddha, Suresh B08069032006-03-27 01:15:23 -08006454static void init_numa_sched_groups_power(struct sched_group *group_head)
6455{
6456 struct sched_group *sg = group_head;
6457 int j;
6458
6459 if (!sg)
6460 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006461 do {
6462 for_each_cpu_mask(j, sg->cpumask) {
6463 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08006464
Andi Kleen3a5c3592007-10-15 17:00:14 +02006465 sd = &per_cpu(phys_domains, j);
6466 if (j != first_cpu(sd->groups->cpumask)) {
6467 /*
6468 * Only add "power" once for each
6469 * physical package.
6470 */
6471 continue;
6472 }
6473
6474 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006475 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006476 sg = sg->next;
6477 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006478}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479#endif
6480
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006481#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006482/* Free memory allocated for various sched_group structures */
6483static void free_sched_groups(const cpumask_t *cpu_map)
6484{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006485 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006486
6487 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006488 struct sched_group **sched_group_nodes
6489 = sched_group_nodes_bycpu[cpu];
6490
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006491 if (!sched_group_nodes)
6492 continue;
6493
6494 for (i = 0; i < MAX_NUMNODES; i++) {
6495 cpumask_t nodemask = node_to_cpumask(i);
6496 struct sched_group *oldsg, *sg = sched_group_nodes[i];
6497
6498 cpus_and(nodemask, nodemask, *cpu_map);
6499 if (cpus_empty(nodemask))
6500 continue;
6501
6502 if (sg == NULL)
6503 continue;
6504 sg = sg->next;
6505next_sg:
6506 oldsg = sg;
6507 sg = sg->next;
6508 kfree(oldsg);
6509 if (oldsg != sched_group_nodes[i])
6510 goto next_sg;
6511 }
6512 kfree(sched_group_nodes);
6513 sched_group_nodes_bycpu[cpu] = NULL;
6514 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006515}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006516#else
6517static void free_sched_groups(const cpumask_t *cpu_map)
6518{
6519}
6520#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006521
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006523 * Initialize sched groups cpu_power.
6524 *
6525 * cpu_power indicates the capacity of sched group, which is used while
6526 * distributing the load between different sched groups in a sched domain.
6527 * Typically cpu_power for all the groups in a sched domain will be same unless
6528 * there are asymmetries in the topology. If there are asymmetries, group
6529 * having more cpu_power will pickup more load compared to the group having
6530 * less cpu_power.
6531 *
6532 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
6533 * the maximum number of tasks a group can handle in the presence of other idle
6534 * or lightly loaded groups in the same sched domain.
6535 */
6536static void init_sched_groups_power(int cpu, struct sched_domain *sd)
6537{
6538 struct sched_domain *child;
6539 struct sched_group *group;
6540
6541 WARN_ON(!sd || !sd->groups);
6542
6543 if (cpu != first_cpu(sd->groups->cpumask))
6544 return;
6545
6546 child = sd->child;
6547
Eric Dumazet5517d862007-05-08 00:32:57 -07006548 sd->groups->__cpu_power = 0;
6549
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006550 /*
6551 * For perf policy, if the groups in child domain share resources
6552 * (for example cores sharing some portions of the cache hierarchy
6553 * or SMT), then set this domain groups cpu_power such that each group
6554 * can handle only one task, when there are other idle groups in the
6555 * same sched domain.
6556 */
6557 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
6558 (child->flags &
6559 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07006560 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006561 return;
6562 }
6563
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006564 /*
6565 * add cpu_power of each child group to this groups cpu_power
6566 */
6567 group = child->groups;
6568 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07006569 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006570 group = group->next;
6571 } while (group != child->groups);
6572}
6573
6574/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006575 * Build sched domains for a given set of cpus and attach the sched domains
6576 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006578static int build_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579{
6580 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006581 struct root_domain *rd;
John Hawkesd1b55132005-09-06 15:18:14 -07006582#ifdef CONFIG_NUMA
6583 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006584 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07006585
6586 /*
6587 * Allocate the per-node list of sched groups
6588 */
Milton Miller5cf9f062007-10-15 17:00:19 +02006589 sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006590 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07006591 if (!sched_group_nodes) {
6592 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006593 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07006594 }
6595 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
6596#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597
Gregory Haskinsdc938522008-01-25 21:08:26 +01006598 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01006599 if (!rd) {
6600 printk(KERN_WARNING "Cannot alloc root domain\n");
6601 return -ENOMEM;
6602 }
6603
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006605 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006607 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608 struct sched_domain *sd = NULL, *p;
6609 cpumask_t nodemask = node_to_cpumask(cpu_to_node(i));
6610
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006611 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612
6613#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02006614 if (cpus_weight(*cpu_map) >
6615 SD_NODES_PER_DOMAIN*cpus_weight(nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07006616 sd = &per_cpu(allnodes_domains, i);
6617 *sd = SD_ALLNODES_INIT;
6618 sd->span = *cpu_map;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006619 cpu_to_allnodes_group(i, cpu_map, &sd->groups);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006620 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006621 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006622 } else
6623 p = NULL;
6624
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625 sd = &per_cpu(node_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 *sd = SD_NODE_INIT;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006627 sd->span = sched_domain_node_span(cpu_to_node(i));
6628 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006629 if (p)
6630 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006631 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632#endif
6633
6634 p = sd;
6635 sd = &per_cpu(phys_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 *sd = SD_CPU_INIT;
6637 sd->span = nodemask;
6638 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006639 if (p)
6640 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006641 cpu_to_phys_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006643#ifdef CONFIG_SCHED_MC
6644 p = sd;
6645 sd = &per_cpu(core_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006646 *sd = SD_MC_INIT;
6647 sd->span = cpu_coregroup_map(i);
6648 cpus_and(sd->span, sd->span, *cpu_map);
6649 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006650 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006651 cpu_to_core_group(i, cpu_map, &sd->groups);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006652#endif
6653
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654#ifdef CONFIG_SCHED_SMT
6655 p = sd;
6656 sd = &per_cpu(cpu_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657 *sd = SD_SIBLING_INIT;
Mike Travisd5a74302007-10-16 01:24:05 -07006658 sd->span = per_cpu(cpu_sibling_map, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006659 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006661 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006662 cpu_to_cpu_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663#endif
6664 }
6665
6666#ifdef CONFIG_SCHED_SMT
6667 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07006668 for_each_cpu_mask(i, *cpu_map) {
Mike Travisd5a74302007-10-16 01:24:05 -07006669 cpumask_t this_sibling_map = per_cpu(cpu_sibling_map, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006670 cpus_and(this_sibling_map, this_sibling_map, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671 if (i != first_cpu(this_sibling_map))
6672 continue;
6673
Ingo Molnardd41f592007-07-09 18:51:59 +02006674 init_sched_build_groups(this_sibling_map, cpu_map,
6675 &cpu_to_cpu_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676 }
6677#endif
6678
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006679#ifdef CONFIG_SCHED_MC
6680 /* Set up multi-core groups */
6681 for_each_cpu_mask(i, *cpu_map) {
6682 cpumask_t this_core_map = cpu_coregroup_map(i);
6683 cpus_and(this_core_map, this_core_map, *cpu_map);
6684 if (i != first_cpu(this_core_map))
6685 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02006686 init_sched_build_groups(this_core_map, cpu_map,
6687 &cpu_to_core_group);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006688 }
6689#endif
6690
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691 /* Set up physical groups */
6692 for (i = 0; i < MAX_NUMNODES; i++) {
6693 cpumask_t nodemask = node_to_cpumask(i);
6694
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006695 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696 if (cpus_empty(nodemask))
6697 continue;
6698
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006699 init_sched_build_groups(nodemask, cpu_map, &cpu_to_phys_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700 }
6701
6702#ifdef CONFIG_NUMA
6703 /* Set up node groups */
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006704 if (sd_allnodes)
Ingo Molnardd41f592007-07-09 18:51:59 +02006705 init_sched_build_groups(*cpu_map, cpu_map,
6706 &cpu_to_allnodes_group);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006707
6708 for (i = 0; i < MAX_NUMNODES; i++) {
6709 /* Set up node groups */
6710 struct sched_group *sg, *prev;
6711 cpumask_t nodemask = node_to_cpumask(i);
6712 cpumask_t domainspan;
6713 cpumask_t covered = CPU_MASK_NONE;
6714 int j;
6715
6716 cpus_and(nodemask, nodemask, *cpu_map);
John Hawkesd1b55132005-09-06 15:18:14 -07006717 if (cpus_empty(nodemask)) {
6718 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006719 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07006720 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006721
6722 domainspan = sched_domain_node_span(i);
6723 cpus_and(domainspan, domainspan, *cpu_map);
6724
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006725 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006726 if (!sg) {
6727 printk(KERN_WARNING "Can not alloc domain group for "
6728 "node %d\n", i);
6729 goto error;
6730 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006731 sched_group_nodes[i] = sg;
6732 for_each_cpu_mask(j, nodemask) {
6733 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02006734
John Hawkes9c1cfda2005-09-06 15:18:14 -07006735 sd = &per_cpu(node_domains, j);
6736 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006737 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006738 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006739 sg->cpumask = nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006740 sg->next = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006741 cpus_or(covered, covered, nodemask);
6742 prev = sg;
6743
6744 for (j = 0; j < MAX_NUMNODES; j++) {
6745 cpumask_t tmp, notcovered;
6746 int n = (i + j) % MAX_NUMNODES;
6747
6748 cpus_complement(notcovered, covered);
6749 cpus_and(tmp, notcovered, *cpu_map);
6750 cpus_and(tmp, tmp, domainspan);
6751 if (cpus_empty(tmp))
6752 break;
6753
6754 nodemask = node_to_cpumask(n);
6755 cpus_and(tmp, tmp, nodemask);
6756 if (cpus_empty(tmp))
6757 continue;
6758
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006759 sg = kmalloc_node(sizeof(struct sched_group),
6760 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006761 if (!sg) {
6762 printk(KERN_WARNING
6763 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006764 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006765 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006766 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006767 sg->cpumask = tmp;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006768 sg->next = prev->next;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006769 cpus_or(covered, covered, tmp);
6770 prev->next = sg;
6771 prev = sg;
6772 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774#endif
6775
6776 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006777#ifdef CONFIG_SCHED_SMT
6778 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006779 struct sched_domain *sd = &per_cpu(cpu_domains, i);
6780
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006781 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006782 }
6783#endif
6784#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006785 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006786 struct sched_domain *sd = &per_cpu(core_domains, i);
6787
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006788 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006789 }
6790#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006792 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006793 struct sched_domain *sd = &per_cpu(phys_domains, i);
6794
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006795 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796 }
6797
John Hawkes9c1cfda2005-09-06 15:18:14 -07006798#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08006799 for (i = 0; i < MAX_NUMNODES; i++)
6800 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006801
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006802 if (sd_allnodes) {
6803 struct sched_group *sg;
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07006804
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006805 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg);
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07006806 init_numa_sched_groups_power(sg);
6807 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006808#endif
6809
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006811 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812 struct sched_domain *sd;
6813#ifdef CONFIG_SCHED_SMT
6814 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006815#elif defined(CONFIG_SCHED_MC)
6816 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817#else
6818 sd = &per_cpu(phys_domains, i);
6819#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01006820 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006822
6823 return 0;
6824
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006825#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006826error:
6827 free_sched_groups(cpu_map);
6828 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006829#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830}
Paul Jackson029190c2007-10-18 23:40:20 -07006831
6832static cpumask_t *doms_cur; /* current sched domains */
6833static int ndoms_cur; /* number of sched domains in 'doms_cur' */
6834
6835/*
6836 * Special case: If a kmalloc of a doms_cur partition (array of
6837 * cpumask_t) fails, then fallback to a single sched domain,
6838 * as determined by the single cpumask_t fallback_doms.
6839 */
6840static cpumask_t fallback_doms;
6841
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006842/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006843 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07006844 * For now this just excludes isolated cpus, but could be used to
6845 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006846 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006847static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006848{
Milton Miller73785472007-10-24 18:23:48 +02006849 int err;
6850
Paul Jackson029190c2007-10-18 23:40:20 -07006851 ndoms_cur = 1;
6852 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6853 if (!doms_cur)
6854 doms_cur = &fallback_doms;
6855 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Milton Miller73785472007-10-24 18:23:48 +02006856 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02006857 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02006858
6859 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006860}
6861
6862static void arch_destroy_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863{
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006864 free_sched_groups(cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006865}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006867/*
6868 * Detach sched domains from a group of cpus specified in cpu_map
6869 * These cpus will now be attached to the NULL domain
6870 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08006871static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006872{
6873 int i;
6874
Milton Miller6382bc92007-10-15 17:00:19 +02006875 unregister_sched_domain_sysctl();
6876
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006877 for_each_cpu_mask(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006878 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006879 synchronize_sched();
6880 arch_destroy_sched_domains(cpu_map);
6881}
6882
Paul Jackson029190c2007-10-18 23:40:20 -07006883/*
6884 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006885 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07006886 * doms_new[] to the current sched domain partitioning, doms_cur[].
6887 * It destroys each deleted domain and builds each new domain.
6888 *
6889 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006890 * The masks don't intersect (don't overlap.) We should setup one
6891 * sched domain for each mask. CPUs not in any of the cpumasks will
6892 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07006893 * current 'doms_cur' domains and in the new 'doms_new', we can leave
6894 * it as it is.
6895 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006896 * The passed in 'doms_new' should be kmalloc'd. This routine takes
6897 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07006898 * failed the kmalloc call, then it can pass in doms_new == NULL,
6899 * and partition_sched_domains() will fallback to the single partition
6900 * 'fallback_doms'.
6901 *
6902 * Call with hotplug lock held
6903 */
6904void partition_sched_domains(int ndoms_new, cpumask_t *doms_new)
6905{
6906 int i, j;
6907
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01006908 lock_doms_cur();
6909
Milton Miller73785472007-10-24 18:23:48 +02006910 /* always unregister in case we don't destroy any domains */
6911 unregister_sched_domain_sysctl();
6912
Paul Jackson029190c2007-10-18 23:40:20 -07006913 if (doms_new == NULL) {
6914 ndoms_new = 1;
6915 doms_new = &fallback_doms;
6916 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
6917 }
6918
6919 /* Destroy deleted domains */
6920 for (i = 0; i < ndoms_cur; i++) {
6921 for (j = 0; j < ndoms_new; j++) {
6922 if (cpus_equal(doms_cur[i], doms_new[j]))
6923 goto match1;
6924 }
6925 /* no match - a current sched domain not in new doms_new[] */
6926 detach_destroy_domains(doms_cur + i);
6927match1:
6928 ;
6929 }
6930
6931 /* Build new domains */
6932 for (i = 0; i < ndoms_new; i++) {
6933 for (j = 0; j < ndoms_cur; j++) {
6934 if (cpus_equal(doms_new[i], doms_cur[j]))
6935 goto match2;
6936 }
6937 /* no match - add a new doms_new */
6938 build_sched_domains(doms_new + i);
6939match2:
6940 ;
6941 }
6942
6943 /* Remember the new sched domains */
6944 if (doms_cur != &fallback_doms)
6945 kfree(doms_cur);
6946 doms_cur = doms_new;
6947 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02006948
6949 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01006950
6951 unlock_doms_cur();
Paul Jackson029190c2007-10-18 23:40:20 -07006952}
6953
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006954#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Adrian Bunk6707de002007-08-12 18:08:19 +02006955static int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006956{
6957 int err;
6958
Gautham R Shenoy95402b32008-01-25 21:08:02 +01006959 get_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006960 detach_destroy_domains(&cpu_online_map);
6961 err = arch_init_sched_domains(&cpu_online_map);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01006962 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006963
6964 return err;
6965}
6966
6967static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
6968{
6969 int ret;
6970
6971 if (buf[0] != '0' && buf[0] != '1')
6972 return -EINVAL;
6973
6974 if (smt)
6975 sched_smt_power_savings = (buf[0] == '1');
6976 else
6977 sched_mc_power_savings = (buf[0] == '1');
6978
6979 ret = arch_reinit_sched_domains();
6980
6981 return ret ? ret : count;
6982}
6983
Adrian Bunk6707de002007-08-12 18:08:19 +02006984#ifdef CONFIG_SCHED_MC
6985static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
6986{
6987 return sprintf(page, "%u\n", sched_mc_power_savings);
6988}
6989static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
6990 const char *buf, size_t count)
6991{
6992 return sched_power_savings_store(buf, count, 0);
6993}
6994static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
6995 sched_mc_power_savings_store);
6996#endif
6997
6998#ifdef CONFIG_SCHED_SMT
6999static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
7000{
7001 return sprintf(page, "%u\n", sched_smt_power_savings);
7002}
7003static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
7004 const char *buf, size_t count)
7005{
7006 return sched_power_savings_store(buf, count, 1);
7007}
7008static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
7009 sched_smt_power_savings_store);
7010#endif
7011
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007012int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7013{
7014 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007015
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007016#ifdef CONFIG_SCHED_SMT
7017 if (smt_capable())
7018 err = sysfs_create_file(&cls->kset.kobj,
7019 &attr_sched_smt_power_savings.attr);
7020#endif
7021#ifdef CONFIG_SCHED_MC
7022 if (!err && mc_capable())
7023 err = sysfs_create_file(&cls->kset.kobj,
7024 &attr_sched_mc_power_savings.attr);
7025#endif
7026 return err;
7027}
7028#endif
7029
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007031 * Force a reinitialization of the sched domains hierarchy. The domains
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07007033 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034 * which will prevent rebalancing while the sched domains are recalculated.
7035 */
7036static int update_sched_domains(struct notifier_block *nfb,
7037 unsigned long action, void *hcpu)
7038{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039 switch (action) {
7040 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007041 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007043 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007044 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045 return NOTIFY_OK;
7046
7047 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007048 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007050 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007052 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007054 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055 /*
7056 * Fall through and re-initialise the domains.
7057 */
7058 break;
7059 default:
7060 return NOTIFY_DONE;
7061 }
7062
7063 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007064 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065
7066 return NOTIFY_OK;
7067}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068
7069void __init sched_init_smp(void)
7070{
Nick Piggin5c1e1762006-10-03 01:14:04 -07007071 cpumask_t non_isolated_cpus;
7072
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007073 get_online_cpus();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007074 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08007075 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007076 if (cpus_empty(non_isolated_cpus))
7077 cpu_set(smp_processor_id(), non_isolated_cpus);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007078 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079 /* XXX: Theoretical race here - CPU may be hotplugged now */
7080 hotcpu_notifier(update_sched_domains, 0);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007081
7082 /* Move init over to a non-isolated CPU */
7083 if (set_cpus_allowed(current, non_isolated_cpus) < 0)
7084 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007085 sched_init_granularity();
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007086
7087#ifdef CONFIG_FAIR_GROUP_SCHED
7088 if (nr_cpu_ids == 1)
7089 return;
7090
7091 lb_monitor_task = kthread_create(load_balance_monitor, NULL,
7092 "group_balance");
7093 if (!IS_ERR(lb_monitor_task)) {
7094 lb_monitor_task->flags |= PF_NOFREEZE;
7095 wake_up_process(lb_monitor_task);
7096 } else {
7097 printk(KERN_ERR "Could not create load balance monitor thread"
7098 "(error = %ld) \n", PTR_ERR(lb_monitor_task));
7099 }
7100#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101}
7102#else
7103void __init sched_init_smp(void)
7104{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007105 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106}
7107#endif /* CONFIG_SMP */
7108
7109int in_sched_functions(unsigned long addr)
7110{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111 return in_lock_functions(addr) ||
7112 (addr >= (unsigned long)__sched_text_start
7113 && addr < (unsigned long)__sched_text_end);
7114}
7115
Alexey Dobriyana9957442007-10-15 17:00:13 +02007116static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007117{
7118 cfs_rq->tasks_timeline = RB_ROOT;
Ingo Molnardd41f592007-07-09 18:51:59 +02007119#ifdef CONFIG_FAIR_GROUP_SCHED
7120 cfs_rq->rq = rq;
7121#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007122 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02007123}
7124
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007125static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7126{
7127 struct rt_prio_array *array;
7128 int i;
7129
7130 array = &rt_rq->active;
7131 for (i = 0; i < MAX_RT_PRIO; i++) {
7132 INIT_LIST_HEAD(array->queue + i);
7133 __clear_bit(i, array->bitmap);
7134 }
7135 /* delimiter for bitsearch: */
7136 __set_bit(MAX_RT_PRIO, array->bitmap);
7137
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007138#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007139 rt_rq->highest_prio = MAX_RT_PRIO;
7140#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007141#ifdef CONFIG_SMP
7142 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007143 rt_rq->overloaded = 0;
7144#endif
7145
7146 rt_rq->rt_time = 0;
7147 rt_rq->rt_throttled = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007148
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007149#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01007150 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007151 rt_rq->rq = rq;
7152#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007153}
7154
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007155#ifdef CONFIG_FAIR_GROUP_SCHED
7156static void init_tg_cfs_entry(struct rq *rq, struct task_group *tg,
7157 struct cfs_rq *cfs_rq, struct sched_entity *se,
7158 int cpu, int add)
7159{
7160 tg->cfs_rq[cpu] = cfs_rq;
7161 init_cfs_rq(cfs_rq, rq);
7162 cfs_rq->tg = tg;
7163 if (add)
7164 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7165
7166 tg->se[cpu] = se;
7167 se->cfs_rq = &rq->cfs;
7168 se->my_q = cfs_rq;
7169 se->load.weight = tg->shares;
7170 se->load.inv_weight = div64_64(1ULL<<32, se->load.weight);
7171 se->parent = NULL;
7172}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007173#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007174
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007175#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007176static void init_tg_rt_entry(struct rq *rq, struct task_group *tg,
7177 struct rt_rq *rt_rq, struct sched_rt_entity *rt_se,
7178 int cpu, int add)
7179{
7180 tg->rt_rq[cpu] = rt_rq;
7181 init_rt_rq(rt_rq, rq);
7182 rt_rq->tg = tg;
7183 rt_rq->rt_se = rt_se;
7184 if (add)
7185 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
7186
7187 tg->rt_se[cpu] = rt_se;
7188 rt_se->rt_rq = &rq->rt;
7189 rt_se->my_q = rt_rq;
7190 rt_se->parent = NULL;
7191 INIT_LIST_HEAD(&rt_se->run_list);
7192}
7193#endif
7194
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195void __init sched_init(void)
7196{
Christoph Lameter476f3532007-05-06 14:48:58 -07007197 int highest_cpu = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007198 int i, j;
7199
Gregory Haskins57d885f2008-01-25 21:08:18 +01007200#ifdef CONFIG_SMP
7201 init_defrootdomain();
7202#endif
7203
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007204#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007205 list_add(&init_task_group.list, &task_groups);
7206#endif
7207
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08007208 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07007209 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210
7211 rq = cpu_rq(i);
7212 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07007213 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07007214 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007215 rq->clock = 1;
7216 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007217 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007218#ifdef CONFIG_FAIR_GROUP_SCHED
7219 init_task_group.shares = init_task_group_load;
7220 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
7221 init_tg_cfs_entry(rq, &init_task_group,
7222 &per_cpu(init_cfs_rq, i),
7223 &per_cpu(init_sched_entity, i), i, 1);
7224
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007225#endif
7226#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007227 init_task_group.rt_runtime =
7228 sysctl_sched_rt_runtime * NSEC_PER_USEC;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007229 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
7230 init_tg_rt_entry(rq, &init_task_group,
7231 &per_cpu(init_rt_rq, i),
7232 &per_cpu(init_sched_rt_entity, i), i, 1);
7233#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007234 rq->rt_period_expire = 0;
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007235 rq->rt_throttled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236
Ingo Molnardd41f592007-07-09 18:51:59 +02007237 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
7238 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07007240 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007241 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007243 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07007245 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246 rq->migration_thread = NULL;
7247 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01007248 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007250 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251 atomic_set(&rq->nr_iowait, 0);
Christoph Lameter476f3532007-05-06 14:48:58 -07007252 highest_cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253 }
7254
Peter Williams2dd73a42006-06-27 02:54:34 -07007255 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007256
Avi Kivitye107be32007-07-26 13:40:43 +02007257#ifdef CONFIG_PREEMPT_NOTIFIERS
7258 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
7259#endif
7260
Christoph Lameterc9819f42006-12-10 02:20:25 -08007261#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07007262 nr_cpu_ids = highest_cpu + 1;
Christoph Lameterc9819f42006-12-10 02:20:25 -08007263 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
7264#endif
7265
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007266#ifdef CONFIG_RT_MUTEXES
7267 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
7268#endif
7269
Linus Torvalds1da177e2005-04-16 15:20:36 -07007270 /*
7271 * The boot idle thread does lazy MMU switching as well:
7272 */
7273 atomic_inc(&init_mm.mm_count);
7274 enter_lazy_tlb(&init_mm, current);
7275
7276 /*
7277 * Make us the idle thread. Technically, schedule() should not be
7278 * called from this thread, however somewhere below it might be,
7279 * but because we are the idle thread, we just pick up running again
7280 * when this runqueue becomes "idle".
7281 */
7282 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02007283 /*
7284 * During early bootup we pretend to be a normal task:
7285 */
7286 current->sched_class = &fair_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287}
7288
7289#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
7290void __might_sleep(char *file, int line)
7291{
Ingo Molnar48f24c42006-07-03 00:25:40 -07007292#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07007293 static unsigned long prev_jiffy; /* ratelimiting */
7294
7295 if ((in_atomic() || irqs_disabled()) &&
7296 system_state == SYSTEM_RUNNING && !oops_in_progress) {
7297 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
7298 return;
7299 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08007300 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301 " context at %s:%d\n", file, line);
7302 printk("in_atomic():%d, irqs_disabled():%d\n",
7303 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08007304 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08007305 if (irqs_disabled())
7306 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307 dump_stack();
7308 }
7309#endif
7310}
7311EXPORT_SYMBOL(__might_sleep);
7312#endif
7313
7314#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007315static void normalize_task(struct rq *rq, struct task_struct *p)
7316{
7317 int on_rq;
7318 update_rq_clock(rq);
7319 on_rq = p->se.on_rq;
7320 if (on_rq)
7321 deactivate_task(rq, p, 0);
7322 __setscheduler(rq, p, SCHED_NORMAL, 0);
7323 if (on_rq) {
7324 activate_task(rq, p, 0);
7325 resched_task(rq->curr);
7326 }
7327}
7328
Linus Torvalds1da177e2005-04-16 15:20:36 -07007329void normalize_rt_tasks(void)
7330{
Ingo Molnara0f98a12007-06-17 18:37:45 +02007331 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07007333 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007335 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007336 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02007337 /*
7338 * Only normalize user tasks:
7339 */
7340 if (!p->mm)
7341 continue;
7342
Ingo Molnardd41f592007-07-09 18:51:59 +02007343 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007344#ifdef CONFIG_SCHEDSTATS
7345 p->se.wait_start = 0;
7346 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007347 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007348#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02007349 task_rq(p)->clock = 0;
7350
7351 if (!rt_task(p)) {
7352 /*
7353 * Renice negative nice level userspace
7354 * tasks back to 0:
7355 */
7356 if (TASK_NICE(p) < 0 && p->mm)
7357 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02007359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007361 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07007362 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363
Ingo Molnar178be792007-10-15 17:00:18 +02007364 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007365
Ingo Molnarb29739f2006-06-27 02:54:51 -07007366 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007367 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007368 } while_each_thread(g, p);
7369
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007370 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371}
7372
7373#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07007374
7375#ifdef CONFIG_IA64
7376/*
7377 * These functions are only useful for the IA64 MCA handling.
7378 *
7379 * They can only be called when the whole system has been
7380 * stopped - every CPU needs to be quiescent, and no scheduling
7381 * activity can take place. Using them for anything else would
7382 * be a serious bug, and as a result, they aren't even visible
7383 * under any other configuration.
7384 */
7385
7386/**
7387 * curr_task - return the current task for a given cpu.
7388 * @cpu: the processor in question.
7389 *
7390 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7391 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007392struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007393{
7394 return cpu_curr(cpu);
7395}
7396
7397/**
7398 * set_curr_task - set the current task for a given cpu.
7399 * @cpu: the processor in question.
7400 * @p: the task pointer to set.
7401 *
7402 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007403 * are serviced on a separate stack. It allows the architecture to switch the
7404 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07007405 * must be called with all CPU's synchronized, and interrupts disabled, the
7406 * and caller must save the original value of the current task (see
7407 * curr_task() above) and restore that value before reenabling interrupts and
7408 * re-starting the system.
7409 *
7410 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7411 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007412void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007413{
7414 cpu_curr(cpu) = p;
7415}
7416
7417#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007418
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007419#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007420
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007421#if defined CONFIG_FAIR_GROUP_SCHED && defined CONFIG_SMP
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007422/*
7423 * distribute shares of all task groups among their schedulable entities,
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007424 * to reflect load distribution across cpus.
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007425 */
7426static int rebalance_shares(struct sched_domain *sd, int this_cpu)
7427{
7428 struct cfs_rq *cfs_rq;
7429 struct rq *rq = cpu_rq(this_cpu);
7430 cpumask_t sdspan = sd->span;
7431 int balanced = 1;
7432
7433 /* Walk thr' all the task groups that we have */
7434 for_each_leaf_cfs_rq(rq, cfs_rq) {
7435 int i;
7436 unsigned long total_load = 0, total_shares;
7437 struct task_group *tg = cfs_rq->tg;
7438
7439 /* Gather total task load of this group across cpus */
7440 for_each_cpu_mask(i, sdspan)
7441 total_load += tg->cfs_rq[i]->load.weight;
7442
Ingo Molnar0eab9142008-01-25 21:08:19 +01007443 /* Nothing to do if this group has no load */
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007444 if (!total_load)
7445 continue;
7446
7447 /*
7448 * tg->shares represents the number of cpu shares the task group
7449 * is eligible to hold on a single cpu. On N cpus, it is
7450 * eligible to hold (N * tg->shares) number of cpu shares.
7451 */
7452 total_shares = tg->shares * cpus_weight(sdspan);
7453
7454 /*
7455 * redistribute total_shares across cpus as per the task load
7456 * distribution.
7457 */
7458 for_each_cpu_mask(i, sdspan) {
7459 unsigned long local_load, local_shares;
7460
7461 local_load = tg->cfs_rq[i]->load.weight;
7462 local_shares = (local_load * total_shares) / total_load;
7463 if (!local_shares)
7464 local_shares = MIN_GROUP_SHARES;
7465 if (local_shares == tg->se[i]->load.weight)
7466 continue;
7467
7468 spin_lock_irq(&cpu_rq(i)->lock);
7469 set_se_shares(tg->se[i], local_shares);
7470 spin_unlock_irq(&cpu_rq(i)->lock);
7471 balanced = 0;
7472 }
7473 }
7474
7475 return balanced;
7476}
7477
7478/*
7479 * How frequently should we rebalance_shares() across cpus?
7480 *
7481 * The more frequently we rebalance shares, the more accurate is the fairness
7482 * of cpu bandwidth distribution between task groups. However higher frequency
7483 * also implies increased scheduling overhead.
7484 *
7485 * sysctl_sched_min_bal_int_shares represents the minimum interval between
7486 * consecutive calls to rebalance_shares() in the same sched domain.
7487 *
7488 * sysctl_sched_max_bal_int_shares represents the maximum interval between
7489 * consecutive calls to rebalance_shares() in the same sched domain.
7490 *
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007491 * These settings allows for the appropriate trade-off between accuracy of
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007492 * fairness and the associated overhead.
7493 *
7494 */
7495
7496/* default: 8ms, units: milliseconds */
7497const_debug unsigned int sysctl_sched_min_bal_int_shares = 8;
7498
7499/* default: 128ms, units: milliseconds */
7500const_debug unsigned int sysctl_sched_max_bal_int_shares = 128;
7501
7502/* kernel thread that runs rebalance_shares() periodically */
7503static int load_balance_monitor(void *unused)
7504{
7505 unsigned int timeout = sysctl_sched_min_bal_int_shares;
7506 struct sched_param schedparm;
7507 int ret;
7508
7509 /*
7510 * We don't want this thread's execution to be limited by the shares
7511 * assigned to default group (init_task_group). Hence make it run
7512 * as a SCHED_RR RT task at the lowest priority.
7513 */
7514 schedparm.sched_priority = 1;
7515 ret = sched_setscheduler(current, SCHED_RR, &schedparm);
7516 if (ret)
7517 printk(KERN_ERR "Couldn't set SCHED_RR policy for load balance"
7518 " monitor thread (error = %d) \n", ret);
7519
7520 while (!kthread_should_stop()) {
7521 int i, cpu, balanced = 1;
7522
7523 /* Prevent cpus going down or coming up */
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +01007524 get_online_cpus();
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007525 /* lockout changes to doms_cur[] array */
7526 lock_doms_cur();
7527 /*
7528 * Enter a rcu read-side critical section to safely walk rq->sd
7529 * chain on various cpus and to walk task group list
7530 * (rq->leaf_cfs_rq_list) in rebalance_shares().
7531 */
7532 rcu_read_lock();
7533
7534 for (i = 0; i < ndoms_cur; i++) {
7535 cpumask_t cpumap = doms_cur[i];
7536 struct sched_domain *sd = NULL, *sd_prev = NULL;
7537
7538 cpu = first_cpu(cpumap);
7539
7540 /* Find the highest domain at which to balance shares */
7541 for_each_domain(cpu, sd) {
7542 if (!(sd->flags & SD_LOAD_BALANCE))
7543 continue;
7544 sd_prev = sd;
7545 }
7546
7547 sd = sd_prev;
7548 /* sd == NULL? No load balance reqd in this domain */
7549 if (!sd)
7550 continue;
7551
7552 balanced &= rebalance_shares(sd, cpu);
7553 }
7554
7555 rcu_read_unlock();
7556
7557 unlock_doms_cur();
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +01007558 put_online_cpus();
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007559
7560 if (!balanced)
7561 timeout = sysctl_sched_min_bal_int_shares;
7562 else if (timeout < sysctl_sched_max_bal_int_shares)
7563 timeout *= 2;
7564
7565 msleep_interruptible(timeout);
7566 }
7567
7568 return 0;
7569}
7570#endif /* CONFIG_SMP */
7571
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007572#ifdef CONFIG_FAIR_GROUP_SCHED
7573static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007574{
7575 int i;
7576
7577 for_each_possible_cpu(i) {
7578 if (tg->cfs_rq)
7579 kfree(tg->cfs_rq[i]);
7580 if (tg->se)
7581 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007582 }
7583
7584 kfree(tg->cfs_rq);
7585 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007586}
7587
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007588static int alloc_fair_sched_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007589{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007590 struct cfs_rq *cfs_rq;
7591 struct sched_entity *se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007592 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007593 int i;
7594
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007595 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * NR_CPUS, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007596 if (!tg->cfs_rq)
7597 goto err;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007598 tg->se = kzalloc(sizeof(se) * NR_CPUS, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007599 if (!tg->se)
7600 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007601
7602 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007603
7604 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007605 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007606
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007607 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
7608 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007609 if (!cfs_rq)
7610 goto err;
7611
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007612 se = kmalloc_node(sizeof(struct sched_entity),
7613 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007614 if (!se)
7615 goto err;
7616
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007617 init_tg_cfs_entry(rq, tg, cfs_rq, se, i, 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007618 }
7619
7620 return 1;
7621
7622 err:
7623 return 0;
7624}
7625
7626static inline void register_fair_sched_group(struct task_group *tg, int cpu)
7627{
7628 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
7629 &cpu_rq(cpu)->leaf_cfs_rq_list);
7630}
7631
7632static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
7633{
7634 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
7635}
7636#else
7637static inline void free_fair_sched_group(struct task_group *tg)
7638{
7639}
7640
7641static inline int alloc_fair_sched_group(struct task_group *tg)
7642{
7643 return 1;
7644}
7645
7646static inline void register_fair_sched_group(struct task_group *tg, int cpu)
7647{
7648}
7649
7650static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
7651{
7652}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007653#endif
7654
7655#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007656static void free_rt_sched_group(struct task_group *tg)
7657{
7658 int i;
7659
7660 for_each_possible_cpu(i) {
7661 if (tg->rt_rq)
7662 kfree(tg->rt_rq[i]);
7663 if (tg->rt_se)
7664 kfree(tg->rt_se[i]);
7665 }
7666
7667 kfree(tg->rt_rq);
7668 kfree(tg->rt_se);
7669}
7670
7671static int alloc_rt_sched_group(struct task_group *tg)
7672{
7673 struct rt_rq *rt_rq;
7674 struct sched_rt_entity *rt_se;
7675 struct rq *rq;
7676 int i;
7677
7678 tg->rt_rq = kzalloc(sizeof(rt_rq) * NR_CPUS, GFP_KERNEL);
7679 if (!tg->rt_rq)
7680 goto err;
7681 tg->rt_se = kzalloc(sizeof(rt_se) * NR_CPUS, GFP_KERNEL);
7682 if (!tg->rt_se)
7683 goto err;
7684
7685 tg->rt_runtime = 0;
7686
7687 for_each_possible_cpu(i) {
7688 rq = cpu_rq(i);
7689
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007690 rt_rq = kmalloc_node(sizeof(struct rt_rq),
7691 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
7692 if (!rt_rq)
7693 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007694
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007695 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
7696 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
7697 if (!rt_se)
7698 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007699
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007700 init_tg_rt_entry(rq, tg, rt_rq, rt_se, i, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007701 }
7702
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007703 return 1;
7704
7705 err:
7706 return 0;
7707}
7708
7709static inline void register_rt_sched_group(struct task_group *tg, int cpu)
7710{
7711 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
7712 &cpu_rq(cpu)->leaf_rt_rq_list);
7713}
7714
7715static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
7716{
7717 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
7718}
7719#else
7720static inline void free_rt_sched_group(struct task_group *tg)
7721{
7722}
7723
7724static inline int alloc_rt_sched_group(struct task_group *tg)
7725{
7726 return 1;
7727}
7728
7729static inline void register_rt_sched_group(struct task_group *tg, int cpu)
7730{
7731}
7732
7733static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
7734{
7735}
7736#endif
7737
7738static void free_sched_group(struct task_group *tg)
7739{
7740 free_fair_sched_group(tg);
7741 free_rt_sched_group(tg);
7742 kfree(tg);
7743}
7744
7745/* allocate runqueue etc for a new task group */
7746struct task_group *sched_create_group(void)
7747{
7748 struct task_group *tg;
7749 unsigned long flags;
7750 int i;
7751
7752 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
7753 if (!tg)
7754 return ERR_PTR(-ENOMEM);
7755
7756 if (!alloc_fair_sched_group(tg))
7757 goto err;
7758
7759 if (!alloc_rt_sched_group(tg))
7760 goto err;
7761
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007762 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007763 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007764 register_fair_sched_group(tg, i);
7765 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007766 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007767 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007768 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007769
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007770 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007771
7772err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007773 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007774 return ERR_PTR(-ENOMEM);
7775}
7776
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007777/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007778static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007779{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007780 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007781 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007782}
7783
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007784/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007785void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007786{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007787 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007788 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007789
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007790 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007791 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007792 unregister_fair_sched_group(tg, i);
7793 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007794 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007795 list_del_rcu(&tg->list);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007796 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007797
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007798 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007799 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007800}
7801
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007802/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02007803 * The caller of this function should have put the task in its new group
7804 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
7805 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007806 */
7807void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007808{
7809 int on_rq, running;
7810 unsigned long flags;
7811 struct rq *rq;
7812
7813 rq = task_rq_lock(tsk, &flags);
7814
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007815 update_rq_clock(rq);
7816
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01007817 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007818 on_rq = tsk->se.on_rq;
7819
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02007820 if (on_rq) {
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007821 dequeue_task(rq, tsk, 0);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02007822 if (unlikely(running))
7823 tsk->sched_class->put_prev_task(rq, tsk);
7824 }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007825
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007826 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007827
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02007828 if (on_rq) {
7829 if (unlikely(running))
7830 tsk->sched_class->set_curr_task(rq);
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02007831 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02007832 }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007833
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007834 task_rq_unlock(rq, &flags);
7835}
7836
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007837#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007838/* rq->lock to be locked by caller */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007839static void set_se_shares(struct sched_entity *se, unsigned long shares)
7840{
7841 struct cfs_rq *cfs_rq = se->cfs_rq;
7842 struct rq *rq = cfs_rq->rq;
7843 int on_rq;
7844
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007845 if (!shares)
7846 shares = MIN_GROUP_SHARES;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007847
7848 on_rq = se->on_rq;
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007849 if (on_rq) {
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007850 dequeue_entity(cfs_rq, se, 0);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007851 dec_cpu_load(rq, se->load.weight);
7852 }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007853
7854 se->load.weight = shares;
7855 se->load.inv_weight = div64_64((1ULL<<32), shares);
7856
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007857 if (on_rq) {
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007858 enqueue_entity(cfs_rq, se, 0);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007859 inc_cpu_load(rq, se->load.weight);
7860 }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007861}
7862
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007863static DEFINE_MUTEX(shares_mutex);
7864
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007865int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007866{
7867 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007868 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01007869
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007870 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007871 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007872 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007873
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007874 if (shares < MIN_GROUP_SHARES)
7875 shares = MIN_GROUP_SHARES;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007876
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007877 /*
7878 * Prevent any load balance activity (rebalance_shares,
7879 * load_balance_fair) from referring to this group first,
7880 * by taking it off the rq->leaf_cfs_rq_list on each cpu.
7881 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007882 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007883 for_each_possible_cpu(i)
7884 unregister_fair_sched_group(tg, i);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007885 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007886
7887 /* wait for any ongoing reference to this group to finish */
7888 synchronize_sched();
7889
7890 /*
7891 * Now we are free to modify the group's share on each cpu
7892 * w/o tripping rebalance_share or load_balance_fair.
7893 */
7894 tg->shares = shares;
7895 for_each_possible_cpu(i) {
7896 spin_lock_irq(&cpu_rq(i)->lock);
7897 set_se_shares(tg->se[i], shares);
7898 spin_unlock_irq(&cpu_rq(i)->lock);
7899 }
7900
7901 /*
7902 * Enable load balance activity on this group, by inserting it back on
7903 * each cpu's rq->leaf_cfs_rq_list.
7904 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007905 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007906 for_each_possible_cpu(i)
7907 register_fair_sched_group(tg, i);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007908 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007909done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007910 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007911 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007912}
7913
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007914unsigned long sched_group_shares(struct task_group *tg)
7915{
7916 return tg->shares;
7917}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007918#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007919
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007920#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007921/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007922 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007923 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007924static DEFINE_MUTEX(rt_constraints_mutex);
7925
7926static unsigned long to_ratio(u64 period, u64 runtime)
7927{
7928 if (runtime == RUNTIME_INF)
7929 return 1ULL << 16;
7930
7931 runtime *= (1ULL << 16);
7932 div64_64(runtime, period);
7933 return runtime;
7934}
7935
7936static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007937{
7938 struct task_group *tgi;
7939 unsigned long total = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007940 unsigned long global_ratio =
7941 to_ratio(sysctl_sched_rt_period,
7942 sysctl_sched_rt_runtime < 0 ?
7943 RUNTIME_INF : sysctl_sched_rt_runtime);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007944
7945 rcu_read_lock();
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007946 list_for_each_entry_rcu(tgi, &task_groups, list) {
7947 if (tgi == tg)
7948 continue;
7949
7950 total += to_ratio(period, tgi->rt_runtime);
7951 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007952 rcu_read_unlock();
7953
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007954 return total + to_ratio(period, runtime) < global_ratio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007955}
7956
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007957int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007958{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007959 u64 rt_runtime, rt_period;
7960 int err = 0;
7961
7962 rt_period = sysctl_sched_rt_period * NSEC_PER_USEC;
7963 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
7964 if (rt_runtime_us == -1)
7965 rt_runtime = rt_period;
7966
7967 mutex_lock(&rt_constraints_mutex);
7968 if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
7969 err = -EINVAL;
7970 goto unlock;
7971 }
7972 if (rt_runtime_us == -1)
7973 rt_runtime = RUNTIME_INF;
7974 tg->rt_runtime = rt_runtime;
7975 unlock:
7976 mutex_unlock(&rt_constraints_mutex);
7977
7978 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007979}
7980
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007981long sched_group_rt_runtime(struct task_group *tg)
7982{
7983 u64 rt_runtime_us;
7984
7985 if (tg->rt_runtime == RUNTIME_INF)
7986 return -1;
7987
7988 rt_runtime_us = tg->rt_runtime;
7989 do_div(rt_runtime_us, NSEC_PER_USEC);
7990 return rt_runtime_us;
7991}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007992#endif
7993#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007994
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007995#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007996
7997/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02007998static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007999{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008000 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
8001 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008002}
8003
8004static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02008005cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008006{
8007 struct task_group *tg;
8008
Paul Menage2b01dfe2007-10-24 18:23:50 +02008009 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008010 /* This is early initialization for the top cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008011 init_task_group.css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008012 return &init_task_group.css;
8013 }
8014
8015 /* we support only 1-level deep hierarchical scheduler atm */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008016 if (cgrp->parent->parent)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008017 return ERR_PTR(-EINVAL);
8018
8019 tg = sched_create_group();
8020 if (IS_ERR(tg))
8021 return ERR_PTR(-ENOMEM);
8022
8023 /* Bind the cgroup to task_group object we just created */
Paul Menage2b01dfe2007-10-24 18:23:50 +02008024 tg->css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008025
8026 return &tg->css;
8027}
8028
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008029static void
8030cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008031{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008032 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008033
8034 sched_destroy_group(tg);
8035}
8036
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008037static int
8038cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
8039 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008040{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008041#ifdef CONFIG_RT_GROUP_SCHED
8042 /* Don't accept realtime tasks when there is no way for them to run */
8043 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_runtime == 0)
8044 return -EINVAL;
8045#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008046 /* We don't support RT-tasks being in separate groups */
8047 if (tsk->sched_class != &fair_sched_class)
8048 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01008049#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008050
8051 return 0;
8052}
8053
8054static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02008055cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008056 struct cgroup *old_cont, struct task_struct *tsk)
8057{
8058 sched_move_task(tsk);
8059}
8060
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008061#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menage2b01dfe2007-10-24 18:23:50 +02008062static int cpu_shares_write_uint(struct cgroup *cgrp, struct cftype *cftype,
8063 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008064{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008065 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008066}
8067
Paul Menage2b01dfe2007-10-24 18:23:50 +02008068static u64 cpu_shares_read_uint(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008069{
Paul Menage2b01dfe2007-10-24 18:23:50 +02008070 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008071
8072 return (u64) tg->shares;
8073}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008074#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008075
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008076#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008077static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
8078 struct file *file,
8079 const char __user *userbuf,
8080 size_t nbytes, loff_t *unused_ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008081{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008082 char buffer[64];
8083 int retval = 0;
8084 s64 val;
8085 char *end;
8086
8087 if (!nbytes)
8088 return -EINVAL;
8089 if (nbytes >= sizeof(buffer))
8090 return -E2BIG;
8091 if (copy_from_user(buffer, userbuf, nbytes))
8092 return -EFAULT;
8093
8094 buffer[nbytes] = 0; /* nul-terminate */
8095
8096 /* strip newline if necessary */
8097 if (nbytes && (buffer[nbytes-1] == '\n'))
8098 buffer[nbytes-1] = 0;
8099 val = simple_strtoll(buffer, &end, 0);
8100 if (*end)
8101 return -EINVAL;
8102
8103 /* Pass to subsystem */
8104 retval = sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
8105 if (!retval)
8106 retval = nbytes;
8107 return retval;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008108}
8109
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008110static ssize_t cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft,
8111 struct file *file,
8112 char __user *buf, size_t nbytes,
8113 loff_t *ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008114{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008115 char tmp[64];
8116 long val = sched_group_rt_runtime(cgroup_tg(cgrp));
8117 int len = sprintf(tmp, "%ld\n", val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008118
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008119 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008120}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008121#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008122
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008123static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008124#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008125 {
8126 .name = "shares",
8127 .read_uint = cpu_shares_read_uint,
8128 .write_uint = cpu_shares_write_uint,
8129 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008130#endif
8131#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008132 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008133 .name = "rt_runtime_us",
8134 .read = cpu_rt_runtime_read,
8135 .write = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008136 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008137#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008138};
8139
8140static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
8141{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008142 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008143}
8144
8145struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01008146 .name = "cpu",
8147 .create = cpu_cgroup_create,
8148 .destroy = cpu_cgroup_destroy,
8149 .can_attach = cpu_cgroup_can_attach,
8150 .attach = cpu_cgroup_attach,
8151 .populate = cpu_cgroup_populate,
8152 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008153 .early_init = 1,
8154};
8155
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008156#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008157
8158#ifdef CONFIG_CGROUP_CPUACCT
8159
8160/*
8161 * CPU accounting code for task groups.
8162 *
8163 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
8164 * (balbir@in.ibm.com).
8165 */
8166
8167/* track cpu usage of a group of tasks */
8168struct cpuacct {
8169 struct cgroup_subsys_state css;
8170 /* cpuusage holds pointer to a u64-type object on every cpu */
8171 u64 *cpuusage;
8172};
8173
8174struct cgroup_subsys cpuacct_subsys;
8175
8176/* return cpu accounting group corresponding to this container */
8177static inline struct cpuacct *cgroup_ca(struct cgroup *cont)
8178{
8179 return container_of(cgroup_subsys_state(cont, cpuacct_subsys_id),
8180 struct cpuacct, css);
8181}
8182
8183/* return cpu accounting group to which this task belongs */
8184static inline struct cpuacct *task_ca(struct task_struct *tsk)
8185{
8186 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
8187 struct cpuacct, css);
8188}
8189
8190/* create a new cpu accounting group */
8191static struct cgroup_subsys_state *cpuacct_create(
8192 struct cgroup_subsys *ss, struct cgroup *cont)
8193{
8194 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
8195
8196 if (!ca)
8197 return ERR_PTR(-ENOMEM);
8198
8199 ca->cpuusage = alloc_percpu(u64);
8200 if (!ca->cpuusage) {
8201 kfree(ca);
8202 return ERR_PTR(-ENOMEM);
8203 }
8204
8205 return &ca->css;
8206}
8207
8208/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008209static void
8210cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008211{
8212 struct cpuacct *ca = cgroup_ca(cont);
8213
8214 free_percpu(ca->cpuusage);
8215 kfree(ca);
8216}
8217
8218/* return total cpu usage (in nanoseconds) of a group */
8219static u64 cpuusage_read(struct cgroup *cont, struct cftype *cft)
8220{
8221 struct cpuacct *ca = cgroup_ca(cont);
8222 u64 totalcpuusage = 0;
8223 int i;
8224
8225 for_each_possible_cpu(i) {
8226 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
8227
8228 /*
8229 * Take rq->lock to make 64-bit addition safe on 32-bit
8230 * platforms.
8231 */
8232 spin_lock_irq(&cpu_rq(i)->lock);
8233 totalcpuusage += *cpuusage;
8234 spin_unlock_irq(&cpu_rq(i)->lock);
8235 }
8236
8237 return totalcpuusage;
8238}
8239
8240static struct cftype files[] = {
8241 {
8242 .name = "usage",
8243 .read_uint = cpuusage_read,
8244 },
8245};
8246
8247static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cont)
8248{
8249 return cgroup_add_files(cont, ss, files, ARRAY_SIZE(files));
8250}
8251
8252/*
8253 * charge this task's execution time to its accounting group.
8254 *
8255 * called with rq->lock held.
8256 */
8257static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
8258{
8259 struct cpuacct *ca;
8260
8261 if (!cpuacct_subsys.active)
8262 return;
8263
8264 ca = task_ca(tsk);
8265 if (ca) {
8266 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
8267
8268 *cpuusage += cputime;
8269 }
8270}
8271
8272struct cgroup_subsys cpuacct_subsys = {
8273 .name = "cpuacct",
8274 .create = cpuacct_create,
8275 .destroy = cpuacct_destroy,
8276 .populate = cpuacct_populate,
8277 .subsys_id = cpuacct_subsys_id,
8278};
8279#endif /* CONFIG_CGROUP_CPUACCT */