blob: 88a17c7128c3b4639a6bf69703a01a5ca4ffd85a [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
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200158#ifdef CONFIG_FAIR_GROUP_SCHED
159
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 {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700168#ifdef CONFIG_FAIR_CGROUP_SCHED
169 struct cgroup_subsys_state css;
170#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200171 /* schedulable entities of this group on each cpu */
172 struct sched_entity **se;
173 /* runqueue "owned" by this group on each cpu */
174 struct cfs_rq **cfs_rq;
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100175
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100176 struct sched_rt_entity **rt_se;
177 struct rt_rq **rt_rq;
178
179 unsigned int rt_ratio;
180
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100181 /*
182 * shares assigned to a task group governs how much of cpu bandwidth
183 * is allocated to the group. The more shares a group has, the more is
184 * the cpu bandwidth allocated to it.
185 *
186 * For ex, lets say that there are three task groups, A, B and C which
187 * have been assigned shares 1000, 2000 and 3000 respectively. Then,
188 * cpu bandwidth allocated by the scheduler to task groups A, B and C
189 * should be:
190 *
191 * Bw(A) = 1000/(1000+2000+3000) * 100 = 16.66%
192 * Bw(B) = 2000/(1000+2000+3000) * 100 = 33.33%
Ingo Molnar03319ec2008-01-25 21:08:28 +0100193 * Bw(C) = 3000/(1000+2000+3000) * 100 = 50%
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100194 *
195 * The weight assigned to a task group's schedulable entities on every
196 * cpu (task_group.se[a_cpu]->load.weight) is derived from the task
197 * group's shares. For ex: lets say that task group A has been
198 * assigned shares of 1000 and there are two CPUs in a system. Then,
199 *
200 * tg_A->se[0]->load.weight = tg_A->se[1]->load.weight = 1000;
201 *
202 * Note: It's not necessary that each of a task's group schedulable
Ingo Molnar03319ec2008-01-25 21:08:28 +0100203 * entity have the same weight on all CPUs. If the group
204 * has 2 of its tasks on CPU0 and 1 task on CPU1, then a
205 * better distribution of weight could be:
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100206 *
207 * tg_A->se[0]->load.weight = 2/3 * 2000 = 1333
208 * tg_A->se[1]->load.weight = 1/2 * 2000 = 667
209 *
210 * rebalance_shares() is responsible for distributing the shares of a
211 * task groups like this among the group's schedulable entities across
212 * cpus.
213 *
214 */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200215 unsigned long shares;
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100216
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100217 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100218 struct list_head list;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200219};
220
221/* Default task group's sched entity on each cpu */
222static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
223/* Default task group's cfs_rq on each cpu */
224static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
225
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100226static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
227static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
228
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200229static struct sched_entity *init_sched_entity_p[NR_CPUS];
230static struct cfs_rq *init_cfs_rq_p[NR_CPUS];
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200231
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100232static struct sched_rt_entity *init_sched_rt_entity_p[NR_CPUS];
233static struct rt_rq *init_rt_rq_p[NR_CPUS];
234
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100235/* task_group_lock serializes add/remove of task groups and also changes to
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100236 * a task group's cpu shares.
237 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100238static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100239
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100240/* doms_cur_mutex serializes access to doms_cur[] array */
241static DEFINE_MUTEX(doms_cur_mutex);
242
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100243#ifdef CONFIG_SMP
244/* kernel thread that runs rebalance_shares() periodically */
245static struct task_struct *lb_monitor_task;
246static int load_balance_monitor(void *unused);
247#endif
248
249static void set_se_shares(struct sched_entity *se, unsigned long shares);
250
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200251/* Default task group.
Ingo Molnar3a252012007-10-15 17:00:12 +0200252 * Every task in system belong to this group at bootup.
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200253 */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200254struct task_group init_task_group = {
Ingo Molnar0eab9142008-01-25 21:08:19 +0100255 .se = init_sched_entity_p,
Ingo Molnar3a252012007-10-15 17:00:12 +0200256 .cfs_rq = init_cfs_rq_p,
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100257
258 .rt_se = init_sched_rt_entity_p,
259 .rt_rq = init_rt_rq_p,
Ingo Molnar3a252012007-10-15 17:00:12 +0200260};
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200261
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200262#ifdef CONFIG_FAIR_USER_SCHED
Ingo Molnar0eab9142008-01-25 21:08:19 +0100263# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200264#else
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100265# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200266#endif
267
Ingo Molnar0eab9142008-01-25 21:08:19 +0100268#define MIN_GROUP_SHARES 2
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100269
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100270static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200271
272/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200273static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200274{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200275 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200276
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200277#ifdef CONFIG_FAIR_USER_SCHED
278 tg = p->user->tg;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700279#elif defined(CONFIG_FAIR_CGROUP_SCHED)
280 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
281 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200282#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100283 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200284#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200285 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200286}
287
288/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100289static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200290{
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100291 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
292 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100293
294 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
295 p->rt.parent = task_group(p)->rt_se[cpu];
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200296}
297
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100298static inline void lock_doms_cur(void)
299{
300 mutex_lock(&doms_cur_mutex);
301}
302
303static inline void unlock_doms_cur(void)
304{
305 mutex_unlock(&doms_cur_mutex);
306}
307
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200308#else
309
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100310static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100311static inline void lock_doms_cur(void) { }
312static inline void unlock_doms_cur(void) { }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200313
314#endif /* CONFIG_FAIR_GROUP_SCHED */
315
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200316/* CFS-related fields in a runqueue */
317struct cfs_rq {
318 struct load_weight load;
319 unsigned long nr_running;
320
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200321 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200322 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200323
324 struct rb_root tasks_timeline;
325 struct rb_node *rb_leftmost;
326 struct rb_node *rb_load_balance_curr;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200327 /* 'curr' points to currently running entity on this cfs_rq.
328 * It is set to NULL otherwise (i.e when none are currently running).
329 */
330 struct sched_entity *curr;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200331
332 unsigned long nr_spread_over;
333
Ingo Molnar62160e32007-10-15 17:00:03 +0200334#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200335 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
336
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100337 /*
338 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200339 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
340 * (like users, containers etc.)
341 *
342 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
343 * list is used during load balance.
344 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100345 struct list_head leaf_cfs_rq_list;
346 struct task_group *tg; /* group that "owns" this runqueue */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200347#endif
348};
349
350/* Real-Time classes' related field in a runqueue: */
351struct rt_rq {
352 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100353 unsigned long rt_nr_running;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100354#if defined CONFIG_SMP || defined CONFIG_FAIR_GROUP_SCHED
355 int highest_prio; /* highest queued rt task prio */
356#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100357#ifdef CONFIG_SMP
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100358 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100359 int overloaded;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100360#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100361 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100362 u64 rt_time;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100363
364#ifdef CONFIG_FAIR_GROUP_SCHED
365 struct rq *rq;
366 struct list_head leaf_rt_rq_list;
367 struct task_group *tg;
368 struct sched_rt_entity *rt_se;
369#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200370};
371
Gregory Haskins57d885f2008-01-25 21:08:18 +0100372#ifdef CONFIG_SMP
373
374/*
375 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100376 * variables. Each exclusive cpuset essentially defines an island domain by
377 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100378 * exclusive cpuset is created, we also create and attach a new root-domain
379 * object.
380 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100381 */
382struct root_domain {
383 atomic_t refcount;
384 cpumask_t span;
385 cpumask_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100386
Ingo Molnar0eab9142008-01-25 21:08:19 +0100387 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100388 * The "RT overload" flag: it gets set if a CPU has more than
389 * one runnable RT task.
390 */
391 cpumask_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100392 atomic_t rto_count;
Gregory Haskins57d885f2008-01-25 21:08:18 +0100393};
394
Gregory Haskinsdc938522008-01-25 21:08:26 +0100395/*
396 * By default the system creates a single root-domain with all cpus as
397 * members (mimicking the global state we have today).
398 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100399static struct root_domain def_root_domain;
400
401#endif
402
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200403/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 * This is the main, per-CPU runqueue data structure.
405 *
406 * Locking rule: those places that want to lock multiple runqueues
407 * (such as the load balancing or the thread migration code), lock
408 * acquire operations must be ordered by ascending &runqueue.
409 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700410struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200411 /* runqueue lock: */
412 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 /*
415 * nr_running and cpu_load should be in the same cacheline because
416 * remote CPUs use both these fields when doing load calculation.
417 */
418 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200419 #define CPU_LOAD_IDX_MAX 5
420 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700421 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700422#ifdef CONFIG_NO_HZ
423 unsigned char in_nohz_recently;
424#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200425 /* capture load from *all* tasks on this cpu: */
426 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200427 unsigned long nr_load_updates;
428 u64 nr_switches;
429
430 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100431 struct rt_rq rt;
432 u64 rt_period_expire;
Peter Zijlstra48d5e252008-01-25 21:08:31 +0100433 int rt_throttled;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100434
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200435#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200436 /* list of leaf cfs_rq on this cpu: */
437 struct list_head leaf_cfs_rq_list;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100438 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 /*
442 * This is part of a global counter where only the total sum
443 * over all CPUs matters. A task can increase this counter on
444 * one CPU and if it got migrated afterwards it may decrease
445 * it on another CPU. Always updated under the runqueue lock:
446 */
447 unsigned long nr_uninterruptible;
448
Ingo Molnar36c8b582006-07-03 00:25:41 -0700449 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800450 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200452
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200453 u64 clock, prev_clock_raw;
454 s64 clock_max_delta;
455
Guillaume Chazaraincc203d22008-01-25 21:08:34 +0100456 unsigned int clock_warps, clock_overflows, clock_underflows;
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200457 u64 idle_clock;
458 unsigned int clock_deep_idle_events;
Ingo Molnar529c7722007-08-10 23:05:11 +0200459 u64 tick_timestamp;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 atomic_t nr_iowait;
462
463#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100464 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 struct sched_domain *sd;
466
467 /* For active balancing */
468 int active_balance;
469 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200470 /* cpu of this runqueue: */
471 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Ingo Molnar36c8b582006-07-03 00:25:41 -0700473 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct list_head migration_queue;
475#endif
476
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100477#ifdef CONFIG_SCHED_HRTICK
478 unsigned long hrtick_flags;
479 ktime_t hrtick_expire;
480 struct hrtimer hrtick_timer;
481#endif
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483#ifdef CONFIG_SCHEDSTATS
484 /* latency stats */
485 struct sched_info rq_sched_info;
486
487 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200488 unsigned int yld_exp_empty;
489 unsigned int yld_act_empty;
490 unsigned int yld_both_empty;
491 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200494 unsigned int sched_switch;
495 unsigned int sched_count;
496 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200499 unsigned int ttwu_count;
500 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200501
502 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200503 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504#endif
Ingo Molnarfcb99372006-07-03 00:25:10 -0700505 struct lock_class_key rq_lock_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506};
507
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700508static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Ingo Molnardd41f592007-07-09 18:51:59 +0200510static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
511{
512 rq->curr->sched_class->check_preempt_curr(rq, p);
513}
514
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700515static inline int cpu_of(struct rq *rq)
516{
517#ifdef CONFIG_SMP
518 return rq->cpu;
519#else
520 return 0;
521#endif
522}
523
Nick Piggin674311d2005-06-25 14:57:27 -0700524/*
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200525 * Update the per-runqueue clock, as finegrained as the platform can give
526 * us, but without assuming monotonicity, etc.:
Ingo Molnar20d315d2007-07-09 18:51:58 +0200527 */
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200528static void __update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200529{
530 u64 prev_raw = rq->prev_clock_raw;
531 u64 now = sched_clock();
532 s64 delta = now - prev_raw;
533 u64 clock = rq->clock;
534
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200535#ifdef CONFIG_SCHED_DEBUG
536 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
537#endif
Ingo Molnar20d315d2007-07-09 18:51:58 +0200538 /*
539 * Protect against sched_clock() occasionally going backwards:
540 */
541 if (unlikely(delta < 0)) {
542 clock++;
543 rq->clock_warps++;
544 } else {
545 /*
546 * Catch too large forward jumps too:
547 */
Ingo Molnar529c7722007-08-10 23:05:11 +0200548 if (unlikely(clock + delta > rq->tick_timestamp + TICK_NSEC)) {
549 if (clock < rq->tick_timestamp + TICK_NSEC)
550 clock = rq->tick_timestamp + TICK_NSEC;
551 else
552 clock++;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200553 rq->clock_overflows++;
554 } else {
555 if (unlikely(delta > rq->clock_max_delta))
556 rq->clock_max_delta = delta;
557 clock += delta;
558 }
559 }
560
561 rq->prev_clock_raw = now;
562 rq->clock = clock;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200563}
564
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200565static void update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200566{
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200567 if (likely(smp_processor_id() == cpu_of(rq)))
568 __update_rq_clock(rq);
569}
Ingo Molnar20d315d2007-07-09 18:51:58 +0200570
Ingo Molnar20d315d2007-07-09 18:51:58 +0200571/*
Nick Piggin674311d2005-06-25 14:57:27 -0700572 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700573 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700574 *
575 * The domain tree of any CPU may only be accessed from within
576 * preempt-disabled sections.
577 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700578#define for_each_domain(cpu, __sd) \
579 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
582#define this_rq() (&__get_cpu_var(runqueues))
583#define task_rq(p) cpu_rq(task_cpu(p))
584#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
585
Peter Zijlstra48d5e252008-01-25 21:08:31 +0100586unsigned long rt_needs_cpu(int cpu)
587{
588 struct rq *rq = cpu_rq(cpu);
589 u64 delta;
590
591 if (!rq->rt_throttled)
592 return 0;
593
594 if (rq->clock > rq->rt_period_expire)
595 return 1;
596
597 delta = rq->rt_period_expire - rq->clock;
598 do_div(delta, NSEC_PER_SEC / HZ);
599
600 return (unsigned long)delta;
601}
602
Ingo Molnare436d802007-07-19 21:28:35 +0200603/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200604 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
605 */
606#ifdef CONFIG_SCHED_DEBUG
607# define const_debug __read_mostly
608#else
609# define const_debug static const
610#endif
611
612/*
613 * Debugging: various feature bits
614 */
615enum {
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200616 SCHED_FEAT_NEW_FAIR_SLEEPERS = 1,
Ingo Molnar96126332007-11-15 20:57:40 +0100617 SCHED_FEAT_WAKEUP_PREEMPT = 2,
618 SCHED_FEAT_START_DEBIT = 4,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100619 SCHED_FEAT_TREE_AVG = 8,
620 SCHED_FEAT_APPROX_AVG = 16,
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100621 SCHED_FEAT_HRTICK = 32,
622 SCHED_FEAT_DOUBLE_TICK = 64,
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200623};
624
625const_debug unsigned int sysctl_sched_features =
Ingo Molnar8401f772007-10-18 21:32:55 +0200626 SCHED_FEAT_NEW_FAIR_SLEEPERS * 1 |
Ingo Molnar96126332007-11-15 20:57:40 +0100627 SCHED_FEAT_WAKEUP_PREEMPT * 1 |
Ingo Molnar8401f772007-10-18 21:32:55 +0200628 SCHED_FEAT_START_DEBIT * 1 |
629 SCHED_FEAT_TREE_AVG * 0 |
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100630 SCHED_FEAT_APPROX_AVG * 0 |
631 SCHED_FEAT_HRTICK * 1 |
632 SCHED_FEAT_DOUBLE_TICK * 0;
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200633
634#define sched_feat(x) (sysctl_sched_features & SCHED_FEAT_##x)
635
636/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100637 * Number of tasks to iterate in a single balance run.
638 * Limited because this is done with IRQs disabled.
639 */
640const_debug unsigned int sysctl_sched_nr_migrate = 32;
641
642/*
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100643 * period over which we measure -rt task cpu usage in ms.
644 * default: 1s
645 */
646const_debug unsigned int sysctl_sched_rt_period = 1000;
647
648#define SCHED_RT_FRAC_SHIFT 16
649#define SCHED_RT_FRAC (1UL << SCHED_RT_FRAC_SHIFT)
650
651/*
652 * ratio of time -rt tasks may consume.
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100653 * default: 95%
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100654 */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100655const_debug unsigned int sysctl_sched_rt_ratio = 62259;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100656
657/*
Ingo Molnare436d802007-07-19 21:28:35 +0200658 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
659 * clock constructed from sched_clock():
660 */
661unsigned long long cpu_clock(int cpu)
662{
Ingo Molnare436d802007-07-19 21:28:35 +0200663 unsigned long long now;
664 unsigned long flags;
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200665 struct rq *rq;
Ingo Molnare436d802007-07-19 21:28:35 +0200666
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200667 local_irq_save(flags);
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200668 rq = cpu_rq(cpu);
Ingo Molnar8ced5f62007-12-07 19:02:47 +0100669 /*
670 * Only call sched_clock() if the scheduler has already been
671 * initialized (some code might call cpu_clock() very early):
672 */
673 if (rq->idle)
674 update_rq_clock(rq);
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200675 now = rq->clock;
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200676 local_irq_restore(flags);
Ingo Molnare436d802007-07-19 21:28:35 +0200677
678 return now;
679}
Paul E. McKenneya58f6f22007-10-15 17:00:14 +0200680EXPORT_SYMBOL_GPL(cpu_clock);
Ingo Molnare436d802007-07-19 21:28:35 +0200681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700683# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700685#ifndef finish_arch_switch
686# define finish_arch_switch(prev) do { } while (0)
687#endif
688
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100689static inline int task_current(struct rq *rq, struct task_struct *p)
690{
691 return rq->curr == p;
692}
693
Nick Piggin4866cde2005-06-25 14:57:23 -0700694#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700695static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700696{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100697 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700698}
699
Ingo Molnar70b97a72006-07-03 00:25:42 -0700700static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700701{
702}
703
Ingo Molnar70b97a72006-07-03 00:25:42 -0700704static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700705{
Ingo Molnarda04c032005-09-13 11:17:59 +0200706#ifdef CONFIG_DEBUG_SPINLOCK
707 /* this is a valid case when another task releases the spinlock */
708 rq->lock.owner = current;
709#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700710 /*
711 * If we are tracking spinlock dependencies then we have to
712 * fix up the runqueue lock - which gets 'carried over' from
713 * prev into current:
714 */
715 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
716
Nick Piggin4866cde2005-06-25 14:57:23 -0700717 spin_unlock_irq(&rq->lock);
718}
719
720#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700721static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700722{
723#ifdef CONFIG_SMP
724 return p->oncpu;
725#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100726 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700727#endif
728}
729
Ingo Molnar70b97a72006-07-03 00:25:42 -0700730static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700731{
732#ifdef CONFIG_SMP
733 /*
734 * We can optimise this out completely for !SMP, because the
735 * SMP rebalancing from interrupt is the only thing that cares
736 * here.
737 */
738 next->oncpu = 1;
739#endif
740#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
741 spin_unlock_irq(&rq->lock);
742#else
743 spin_unlock(&rq->lock);
744#endif
745}
746
Ingo Molnar70b97a72006-07-03 00:25:42 -0700747static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700748{
749#ifdef CONFIG_SMP
750 /*
751 * After ->oncpu is cleared, the task can be moved to a different CPU.
752 * We must ensure this doesn't happen until the switch is completely
753 * finished.
754 */
755 smp_wmb();
756 prev->oncpu = 0;
757#endif
758#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
759 local_irq_enable();
760#endif
761}
762#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700765 * __task_rq_lock - lock the runqueue a given task resides on.
766 * Must be called interrupts disabled.
767 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700768static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700769 __acquires(rq->lock)
770{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200771 for (;;) {
772 struct rq *rq = task_rq(p);
773 spin_lock(&rq->lock);
774 if (likely(rq == task_rq(p)))
775 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700776 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700777 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700778}
779
780/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100782 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 * explicitly disabling preemption.
784 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700785static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 __acquires(rq->lock)
787{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700788 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Andi Kleen3a5c3592007-10-15 17:00:14 +0200790 for (;;) {
791 local_irq_save(*flags);
792 rq = task_rq(p);
793 spin_lock(&rq->lock);
794 if (likely(rq == task_rq(p)))
795 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
Alexey Dobriyana9957442007-10-15 17:00:13 +0200800static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700801 __releases(rq->lock)
802{
803 spin_unlock(&rq->lock);
804}
805
Ingo Molnar70b97a72006-07-03 00:25:42 -0700806static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 __releases(rq->lock)
808{
809 spin_unlock_irqrestore(&rq->lock, *flags);
810}
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800813 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200815static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 __acquires(rq->lock)
817{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700818 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 local_irq_disable();
821 rq = this_rq();
822 spin_lock(&rq->lock);
823
824 return rq;
825}
826
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200827/*
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200828 * We are going deep-idle (irqs are disabled):
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200829 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200830void sched_clock_idle_sleep_event(void)
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200831{
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200832 struct rq *rq = cpu_rq(smp_processor_id());
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200833
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200834 spin_lock(&rq->lock);
835 __update_rq_clock(rq);
836 spin_unlock(&rq->lock);
837 rq->clock_deep_idle_events++;
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200838}
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200839EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
840
841/*
842 * We just idled delta nanoseconds (called with irqs disabled):
843 */
844void sched_clock_idle_wakeup_event(u64 delta_ns)
845{
846 struct rq *rq = cpu_rq(smp_processor_id());
847 u64 now = sched_clock();
848
849 rq->idle_clock += delta_ns;
850 /*
851 * Override the previous timestamp and ignore all
852 * sched_clock() deltas that occured while we idled,
853 * and use the PM-provided delta_ns to advance the
854 * rq clock:
855 */
856 spin_lock(&rq->lock);
857 rq->prev_clock_raw = now;
858 rq->clock += delta_ns;
859 spin_unlock(&rq->lock);
Guillaume Chazarain782daee2008-01-25 21:08:33 +0100860 touch_softlockup_watchdog();
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200861}
862EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200863
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100864static void __resched_task(struct task_struct *p, int tif_bit);
865
866static inline void resched_task(struct task_struct *p)
867{
868 __resched_task(p, TIF_NEED_RESCHED);
869}
870
871#ifdef CONFIG_SCHED_HRTICK
872/*
873 * Use HR-timers to deliver accurate preemption points.
874 *
875 * Its all a bit involved since we cannot program an hrt while holding the
876 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
877 * reschedule event.
878 *
879 * When we get rescheduled we reprogram the hrtick_timer outside of the
880 * rq->lock.
881 */
882static inline void resched_hrt(struct task_struct *p)
883{
884 __resched_task(p, TIF_HRTICK_RESCHED);
885}
886
887static inline void resched_rq(struct rq *rq)
888{
889 unsigned long flags;
890
891 spin_lock_irqsave(&rq->lock, flags);
892 resched_task(rq->curr);
893 spin_unlock_irqrestore(&rq->lock, flags);
894}
895
896enum {
897 HRTICK_SET, /* re-programm hrtick_timer */
898 HRTICK_RESET, /* not a new slice */
899};
900
901/*
902 * Use hrtick when:
903 * - enabled by features
904 * - hrtimer is actually high res
905 */
906static inline int hrtick_enabled(struct rq *rq)
907{
908 if (!sched_feat(HRTICK))
909 return 0;
910 return hrtimer_is_hres_active(&rq->hrtick_timer);
911}
912
913/*
914 * Called to set the hrtick timer state.
915 *
916 * called with rq->lock held and irqs disabled
917 */
918static void hrtick_start(struct rq *rq, u64 delay, int reset)
919{
920 assert_spin_locked(&rq->lock);
921
922 /*
923 * preempt at: now + delay
924 */
925 rq->hrtick_expire =
926 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
927 /*
928 * indicate we need to program the timer
929 */
930 __set_bit(HRTICK_SET, &rq->hrtick_flags);
931 if (reset)
932 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
933
934 /*
935 * New slices are called from the schedule path and don't need a
936 * forced reschedule.
937 */
938 if (reset)
939 resched_hrt(rq->curr);
940}
941
942static void hrtick_clear(struct rq *rq)
943{
944 if (hrtimer_active(&rq->hrtick_timer))
945 hrtimer_cancel(&rq->hrtick_timer);
946}
947
948/*
949 * Update the timer from the possible pending state.
950 */
951static void hrtick_set(struct rq *rq)
952{
953 ktime_t time;
954 int set, reset;
955 unsigned long flags;
956
957 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
958
959 spin_lock_irqsave(&rq->lock, flags);
960 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
961 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
962 time = rq->hrtick_expire;
963 clear_thread_flag(TIF_HRTICK_RESCHED);
964 spin_unlock_irqrestore(&rq->lock, flags);
965
966 if (set) {
967 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
968 if (reset && !hrtimer_active(&rq->hrtick_timer))
969 resched_rq(rq);
970 } else
971 hrtick_clear(rq);
972}
973
974/*
975 * High-resolution timer tick.
976 * Runs from hardirq context with interrupts disabled.
977 */
978static enum hrtimer_restart hrtick(struct hrtimer *timer)
979{
980 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
981
982 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
983
984 spin_lock(&rq->lock);
985 __update_rq_clock(rq);
986 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
987 spin_unlock(&rq->lock);
988
989 return HRTIMER_NORESTART;
990}
991
992static inline void init_rq_hrtick(struct rq *rq)
993{
994 rq->hrtick_flags = 0;
995 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
996 rq->hrtick_timer.function = hrtick;
997 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
998}
999
1000void hrtick_resched(void)
1001{
1002 struct rq *rq;
1003 unsigned long flags;
1004
1005 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1006 return;
1007
1008 local_irq_save(flags);
1009 rq = cpu_rq(smp_processor_id());
1010 hrtick_set(rq);
1011 local_irq_restore(flags);
1012}
1013#else
1014static inline void hrtick_clear(struct rq *rq)
1015{
1016}
1017
1018static inline void hrtick_set(struct rq *rq)
1019{
1020}
1021
1022static inline void init_rq_hrtick(struct rq *rq)
1023{
1024}
1025
1026void hrtick_resched(void)
1027{
1028}
1029#endif
1030
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001031/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001032 * resched_task - mark a task 'to be rescheduled now'.
1033 *
1034 * On UP this means the setting of the need_resched flag, on SMP it
1035 * might also involve a cross-CPU call to trigger the scheduler on
1036 * the target CPU.
1037 */
1038#ifdef CONFIG_SMP
1039
1040#ifndef tsk_is_polling
1041#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1042#endif
1043
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001044static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001045{
1046 int cpu;
1047
1048 assert_spin_locked(&task_rq(p)->lock);
1049
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001050 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001051 return;
1052
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001053 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001054
1055 cpu = task_cpu(p);
1056 if (cpu == smp_processor_id())
1057 return;
1058
1059 /* NEED_RESCHED must be visible before we test polling */
1060 smp_mb();
1061 if (!tsk_is_polling(p))
1062 smp_send_reschedule(cpu);
1063}
1064
1065static void resched_cpu(int cpu)
1066{
1067 struct rq *rq = cpu_rq(cpu);
1068 unsigned long flags;
1069
1070 if (!spin_trylock_irqsave(&rq->lock, flags))
1071 return;
1072 resched_task(cpu_curr(cpu));
1073 spin_unlock_irqrestore(&rq->lock, flags);
1074}
1075#else
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001076static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001077{
1078 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001079 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001080}
1081#endif
1082
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001083#if BITS_PER_LONG == 32
1084# define WMULT_CONST (~0UL)
1085#else
1086# define WMULT_CONST (1UL << 32)
1087#endif
1088
1089#define WMULT_SHIFT 32
1090
Ingo Molnar194081e2007-08-09 11:16:51 +02001091/*
1092 * Shift right and round:
1093 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001094#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001095
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001096static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001097calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1098 struct load_weight *lw)
1099{
1100 u64 tmp;
1101
1102 if (unlikely(!lw->inv_weight))
Ingo Molnar194081e2007-08-09 11:16:51 +02001103 lw->inv_weight = (WMULT_CONST - lw->weight/2) / lw->weight + 1;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001104
1105 tmp = (u64)delta_exec * weight;
1106 /*
1107 * Check whether we'd overflow the 64-bit multiplication:
1108 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001109 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001110 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001111 WMULT_SHIFT/2);
1112 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001113 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001114
Ingo Molnarecf691d2007-08-02 17:41:40 +02001115 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001116}
1117
1118static inline unsigned long
1119calc_delta_fair(unsigned long delta_exec, struct load_weight *lw)
1120{
1121 return calc_delta_mine(delta_exec, NICE_0_LOAD, lw);
1122}
1123
Ingo Molnar10919852007-10-15 17:00:04 +02001124static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001125{
1126 lw->weight += inc;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001127}
1128
Ingo Molnar10919852007-10-15 17:00:04 +02001129static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001130{
1131 lw->weight -= dec;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001132}
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001135 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1136 * of tasks with abnormal "nice" values across CPUs the contribution that
1137 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001138 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001139 * scaled version of the new time slice allocation that they receive on time
1140 * slice expiry etc.
1141 */
1142
Ingo Molnardd41f592007-07-09 18:51:59 +02001143#define WEIGHT_IDLEPRIO 2
1144#define WMULT_IDLEPRIO (1 << 31)
1145
1146/*
1147 * Nice levels are multiplicative, with a gentle 10% change for every
1148 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1149 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1150 * that remained on nice 0.
1151 *
1152 * The "10% effect" is relative and cumulative: from _any_ nice level,
1153 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +02001154 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1155 * If a task goes up by ~10% and another task goes down by ~10% then
1156 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001157 */
1158static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001159 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1160 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1161 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1162 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1163 /* 0 */ 1024, 820, 655, 526, 423,
1164 /* 5 */ 335, 272, 215, 172, 137,
1165 /* 10 */ 110, 87, 70, 56, 45,
1166 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001167};
1168
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001169/*
1170 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1171 *
1172 * In cases where the weight does not change often, we can use the
1173 * precalculated inverse to speed up arithmetics by turning divisions
1174 * into multiplications:
1175 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001176static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001177 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1178 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1179 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1180 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1181 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1182 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1183 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1184 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001185};
Peter Williams2dd73a42006-06-27 02:54:34 -07001186
Ingo Molnardd41f592007-07-09 18:51:59 +02001187static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1188
1189/*
1190 * runqueue iterator, to support SMP load-balancing between different
1191 * scheduling classes, without having to expose their internal data
1192 * structures to the load-balancing proper:
1193 */
1194struct rq_iterator {
1195 void *arg;
1196 struct task_struct *(*start)(void *);
1197 struct task_struct *(*next)(void *);
1198};
1199
Peter Williamse1d14842007-10-24 18:23:51 +02001200#ifdef CONFIG_SMP
1201static unsigned long
1202balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1203 unsigned long max_load_move, struct sched_domain *sd,
1204 enum cpu_idle_type idle, int *all_pinned,
1205 int *this_best_prio, struct rq_iterator *iterator);
1206
1207static int
1208iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1209 struct sched_domain *sd, enum cpu_idle_type idle,
1210 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001211#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001212
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001213#ifdef CONFIG_CGROUP_CPUACCT
1214static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1215#else
1216static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1217#endif
1218
Srivatsa Vaddagiri58e2d4c2008-01-25 21:08:00 +01001219static inline void inc_cpu_load(struct rq *rq, unsigned long load)
1220{
1221 update_load_add(&rq->load, load);
1222}
1223
1224static inline void dec_cpu_load(struct rq *rq, unsigned long load)
1225{
1226 update_load_sub(&rq->load, load);
1227}
1228
Gregory Haskinse7693a32008-01-25 21:08:09 +01001229#ifdef CONFIG_SMP
1230static unsigned long source_load(int cpu, int type);
1231static unsigned long target_load(int cpu, int type);
1232static unsigned long cpu_avg_load_per_task(int cpu);
1233static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
1234#endif /* CONFIG_SMP */
1235
Ingo Molnardd41f592007-07-09 18:51:59 +02001236#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001237#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001238#include "sched_fair.c"
1239#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001240#ifdef CONFIG_SCHED_DEBUG
1241# include "sched_debug.c"
1242#endif
1243
1244#define sched_class_highest (&rt_sched_class)
1245
Gerald Stralko5aff0532008-01-31 22:45:23 +01001246static void inc_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001247{
1248 rq->nr_running++;
Ingo Molnar9c217242007-08-02 17:41:40 +02001249}
1250
Gerald Stralko5aff0532008-01-31 22:45:23 +01001251static void dec_nr_running(struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001252{
1253 rq->nr_running--;
Ingo Molnar9c217242007-08-02 17:41:40 +02001254}
1255
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001256static void set_load_weight(struct task_struct *p)
1257{
1258 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001259 p->se.load.weight = prio_to_weight[0] * 2;
1260 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1261 return;
1262 }
1263
1264 /*
1265 * SCHED_IDLE tasks get minimal weight:
1266 */
1267 if (p->policy == SCHED_IDLE) {
1268 p->se.load.weight = WEIGHT_IDLEPRIO;
1269 p->se.load.inv_weight = WMULT_IDLEPRIO;
1270 return;
1271 }
1272
1273 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1274 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001275}
1276
Ingo Molnar8159f872007-08-09 11:16:49 +02001277static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001278{
1279 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001280 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001281 p->se.on_rq = 1;
1282}
1283
Ingo Molnar69be72c2007-08-09 11:16:49 +02001284static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001285{
Ingo Molnarf02231e2007-08-09 11:16:48 +02001286 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001287 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001288}
1289
1290/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001291 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001292 */
Ingo Molnar14531182007-07-09 18:51:59 +02001293static inline int __normal_prio(struct task_struct *p)
1294{
Ingo Molnardd41f592007-07-09 18:51:59 +02001295 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001296}
1297
1298/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001299 * Calculate the expected normal priority: i.e. priority
1300 * without taking RT-inheritance into account. Might be
1301 * boosted by interactivity modifiers. Changes upon fork,
1302 * setprio syscalls, and whenever the interactivity
1303 * estimator recalculates.
1304 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001305static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001306{
1307 int prio;
1308
Ingo Molnare05606d2007-07-09 18:51:59 +02001309 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001310 prio = MAX_RT_PRIO-1 - p->rt_priority;
1311 else
1312 prio = __normal_prio(p);
1313 return prio;
1314}
1315
1316/*
1317 * Calculate the current priority, i.e. the priority
1318 * taken into account by the scheduler. This value might
1319 * be boosted by RT tasks, or might be boosted by
1320 * interactivity modifiers. Will be RT if the task got
1321 * RT-boosted. If not then it returns p->normal_prio.
1322 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001323static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001324{
1325 p->normal_prio = normal_prio(p);
1326 /*
1327 * If we are RT tasks or we were boosted to RT priority,
1328 * keep the priority unchanged. Otherwise, update priority
1329 * to the normal priority:
1330 */
1331 if (!rt_prio(p->prio))
1332 return p->normal_prio;
1333 return p->prio;
1334}
1335
1336/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001337 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001339static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001341 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001342 rq->nr_uninterruptible--;
1343
Ingo Molnar8159f872007-08-09 11:16:49 +02001344 enqueue_task(rq, p, wakeup);
Gerald Stralko5aff0532008-01-31 22:45:23 +01001345 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
1348/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 * deactivate_task - remove a task from the runqueue.
1350 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02001351static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001353 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001354 rq->nr_uninterruptible++;
1355
Ingo Molnar69be72c2007-08-09 11:16:49 +02001356 dequeue_task(rq, p, sleep);
Gerald Stralko5aff0532008-01-31 22:45:23 +01001357 dec_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360/**
1361 * task_curr - is this task currently executing on a CPU?
1362 * @p: the task in question.
1363 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001364inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
1366 return cpu_curr(task_cpu(p)) == p;
1367}
1368
Peter Williams2dd73a42006-06-27 02:54:34 -07001369/* Used instead of source_load when we know the type == 0 */
1370unsigned long weighted_cpuload(const int cpu)
1371{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001372 return cpu_rq(cpu)->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02001373}
1374
1375static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1376{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001377 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001378#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01001379 /*
1380 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1381 * successfuly executed on another CPU. We must ensure that updates of
1382 * per-task data have been completed by this moment.
1383 */
1384 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02001385 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02001386#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07001387}
1388
Steven Rostedtcb469842008-01-25 21:08:22 +01001389static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1390 const struct sched_class *prev_class,
1391 int oldprio, int running)
1392{
1393 if (prev_class != p->sched_class) {
1394 if (prev_class->switched_from)
1395 prev_class->switched_from(rq, p, running);
1396 p->sched_class->switched_to(rq, p, running);
1397 } else
1398 p->sched_class->prio_changed(rq, p, oldprio, running);
1399}
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02001402
Ingo Molnarcc367732007-10-15 17:00:18 +02001403/*
1404 * Is this task likely cache-hot:
1405 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001406static int
Ingo Molnarcc367732007-10-15 17:00:18 +02001407task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
1408{
1409 s64 delta;
1410
1411 if (p->sched_class != &fair_sched_class)
1412 return 0;
1413
Ingo Molnar6bc16652007-10-15 17:00:18 +02001414 if (sysctl_sched_migration_cost == -1)
1415 return 1;
1416 if (sysctl_sched_migration_cost == 0)
1417 return 0;
1418
Ingo Molnarcc367732007-10-15 17:00:18 +02001419 delta = now - p->se.exec_start;
1420
1421 return delta < (s64)sysctl_sched_migration_cost;
1422}
1423
1424
Ingo Molnardd41f592007-07-09 18:51:59 +02001425void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001426{
Ingo Molnardd41f592007-07-09 18:51:59 +02001427 int old_cpu = task_cpu(p);
1428 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001429 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
1430 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02001431 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001432
1433 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001434
1435#ifdef CONFIG_SCHEDSTATS
1436 if (p->se.wait_start)
1437 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001438 if (p->se.sleep_start)
1439 p->se.sleep_start -= clock_offset;
1440 if (p->se.block_start)
1441 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02001442 if (old_cpu != new_cpu) {
1443 schedstat_inc(p, se.nr_migrations);
1444 if (task_hot(p, old_rq->clock, NULL))
1445 schedstat_inc(p, se.nr_forced2_migrations);
1446 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001447#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001448 p->se.vruntime -= old_cfsrq->min_vruntime -
1449 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02001450
1451 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001452}
1453
Ingo Molnar70b97a72006-07-03 00:25:42 -07001454struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Ingo Molnar36c8b582006-07-03 00:25:41 -07001457 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 int dest_cpu;
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001461};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463/*
1464 * The task's runqueue lock must be held.
1465 * Returns true if you have to wait for migration thread.
1466 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001467static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07001468migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001470 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 /*
1473 * If the task is not on a runqueue (and not running), then
1474 * it is sufficient to simply update the task's cpu field.
1475 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001476 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 set_task_cpu(p, dest_cpu);
1478 return 0;
1479 }
1480
1481 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 req->task = p;
1483 req->dest_cpu = dest_cpu;
1484 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07001485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return 1;
1487}
1488
1489/*
1490 * wait_task_inactive - wait for a thread to unschedule.
1491 *
1492 * The caller must ensure that the task *will* unschedule sometime soon,
1493 * else this function might spin for a *long* time. This function can't
1494 * be called with interrupts off, or it may introduce deadlock with
1495 * smp_call_function() if an IPI is sent by the same process we are
1496 * waiting to become inactive.
1497 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001498void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
1500 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001501 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001502 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Andi Kleen3a5c3592007-10-15 17:00:14 +02001504 for (;;) {
1505 /*
1506 * We do the initial early heuristics without holding
1507 * any task-queue locks at all. We'll only try to get
1508 * the runqueue lock when things look like they will
1509 * work out!
1510 */
1511 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001512
Andi Kleen3a5c3592007-10-15 17:00:14 +02001513 /*
1514 * If the task is actively running on another CPU
1515 * still, just relax and busy-wait without holding
1516 * any locks.
1517 *
1518 * NOTE! Since we don't hold any locks, it's not
1519 * even sure that "rq" stays as the right runqueue!
1520 * But we don't care, since "task_running()" will
1521 * return false if the runqueue has changed and p
1522 * is actually now running somewhere else!
1523 */
1524 while (task_running(rq, p))
1525 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001526
Andi Kleen3a5c3592007-10-15 17:00:14 +02001527 /*
1528 * Ok, time to look more closely! We need the rq
1529 * lock now, to be *sure*. If we're wrong, we'll
1530 * just go back and repeat.
1531 */
1532 rq = task_rq_lock(p, &flags);
1533 running = task_running(rq, p);
1534 on_rq = p->se.on_rq;
1535 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001536
Andi Kleen3a5c3592007-10-15 17:00:14 +02001537 /*
1538 * Was it really running after all now that we
1539 * checked with the proper locks actually held?
1540 *
1541 * Oops. Go back and try again..
1542 */
1543 if (unlikely(running)) {
1544 cpu_relax();
1545 continue;
1546 }
1547
1548 /*
1549 * It's not enough that it's not actively running,
1550 * it must be off the runqueue _entirely_, and not
1551 * preempted!
1552 *
1553 * So if it wa still runnable (but just not actively
1554 * running right now), it's preempted, and we should
1555 * yield - it could be a while.
1556 */
1557 if (unlikely(on_rq)) {
1558 schedule_timeout_uninterruptible(1);
1559 continue;
1560 }
1561
1562 /*
1563 * Ahh, all good. It wasn't running, and it wasn't
1564 * runnable, which means that it will never become
1565 * running in the future either. We're all done!
1566 */
1567 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
1571/***
1572 * kick_process - kick a running thread to enter/exit the kernel
1573 * @p: the to-be-kicked thread
1574 *
1575 * Cause a process which is running on another CPU to enter
1576 * kernel-mode, without any delay. (to get signals handled.)
1577 *
1578 * NOTE: this function doesnt have to take the runqueue lock,
1579 * because all it wants to ensure is that the remote task enters
1580 * the kernel. If the IPI races and the task has been migrated
1581 * to another CPU then no harm is done and the purpose has been
1582 * achieved as well.
1583 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001584void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
1586 int cpu;
1587
1588 preempt_disable();
1589 cpu = task_cpu(p);
1590 if ((cpu != smp_processor_id()) && task_curr(p))
1591 smp_send_reschedule(cpu);
1592 preempt_enable();
1593}
1594
1595/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001596 * Return a low guess at the load of a migration-source cpu weighted
1597 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 *
1599 * We want to under-estimate the load of migration sources, to
1600 * balance conservatively.
1601 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001602static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08001603{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001604 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001605 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001606
Peter Williams2dd73a42006-06-27 02:54:34 -07001607 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001608 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001609
Ingo Molnardd41f592007-07-09 18:51:59 +02001610 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
1612
1613/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001614 * Return a high guess at the load of a migration-target cpu weighted
1615 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001617static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08001618{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001619 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001620 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001621
Peter Williams2dd73a42006-06-27 02:54:34 -07001622 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001623 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001624
Ingo Molnardd41f592007-07-09 18:51:59 +02001625 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07001626}
1627
1628/*
1629 * Return the average load per task on the cpu's run queue
1630 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001631static unsigned long cpu_avg_load_per_task(int cpu)
Peter Williams2dd73a42006-06-27 02:54:34 -07001632{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001633 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001634 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07001635 unsigned long n = rq->nr_running;
1636
Ingo Molnardd41f592007-07-09 18:51:59 +02001637 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
Nick Piggin147cbb42005-06-25 14:57:19 -07001640/*
1641 * find_idlest_group finds and returns the least busy CPU group within the
1642 * domain.
1643 */
1644static struct sched_group *
1645find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
1646{
1647 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
1648 unsigned long min_load = ULONG_MAX, this_load = 0;
1649 int load_idx = sd->forkexec_idx;
1650 int imbalance = 100 + (sd->imbalance_pct-100)/2;
1651
1652 do {
1653 unsigned long load, avg_load;
1654 int local_group;
1655 int i;
1656
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001657 /* Skip over this group if it has no CPUs allowed */
1658 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02001659 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001660
Nick Piggin147cbb42005-06-25 14:57:19 -07001661 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07001662
1663 /* Tally up the load of all CPUs in the group */
1664 avg_load = 0;
1665
1666 for_each_cpu_mask(i, group->cpumask) {
1667 /* Bias balancing toward cpus of our domain */
1668 if (local_group)
1669 load = source_load(i, load_idx);
1670 else
1671 load = target_load(i, load_idx);
1672
1673 avg_load += load;
1674 }
1675
1676 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07001677 avg_load = sg_div_cpu_power(group,
1678 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07001679
1680 if (local_group) {
1681 this_load = avg_load;
1682 this = group;
1683 } else if (avg_load < min_load) {
1684 min_load = avg_load;
1685 idlest = group;
1686 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02001687 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07001688
1689 if (!idlest || 100*this_load < imbalance*min_load)
1690 return NULL;
1691 return idlest;
1692}
1693
1694/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07001695 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07001696 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07001697static int
1698find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
Nick Piggin147cbb42005-06-25 14:57:19 -07001699{
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001700 cpumask_t tmp;
Nick Piggin147cbb42005-06-25 14:57:19 -07001701 unsigned long load, min_load = ULONG_MAX;
1702 int idlest = -1;
1703 int i;
1704
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001705 /* Traverse only the allowed CPUs */
1706 cpus_and(tmp, group->cpumask, p->cpus_allowed);
1707
1708 for_each_cpu_mask(i, tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07001709 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07001710
1711 if (load < min_load || (load == min_load && i == this_cpu)) {
1712 min_load = load;
1713 idlest = i;
1714 }
1715 }
1716
1717 return idlest;
1718}
1719
Nick Piggin476d1392005-06-25 14:57:29 -07001720/*
1721 * sched_balance_self: balance the current task (running on cpu) in domains
1722 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
1723 * SD_BALANCE_EXEC.
1724 *
1725 * Balance, ie. select the least loaded group.
1726 *
1727 * Returns the target CPU number, or the same CPU if no balancing is needed.
1728 *
1729 * preempt must be disabled.
1730 */
1731static int sched_balance_self(int cpu, int flag)
1732{
1733 struct task_struct *t = current;
1734 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07001735
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001736 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02001737 /*
1738 * If power savings logic is enabled for a domain, stop there.
1739 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07001740 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
1741 break;
Nick Piggin476d1392005-06-25 14:57:29 -07001742 if (tmp->flags & flag)
1743 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001744 }
Nick Piggin476d1392005-06-25 14:57:29 -07001745
1746 while (sd) {
1747 cpumask_t span;
1748 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001749 int new_cpu, weight;
1750
1751 if (!(sd->flags & flag)) {
1752 sd = sd->child;
1753 continue;
1754 }
Nick Piggin476d1392005-06-25 14:57:29 -07001755
1756 span = sd->span;
1757 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001758 if (!group) {
1759 sd = sd->child;
1760 continue;
1761 }
Nick Piggin476d1392005-06-25 14:57:29 -07001762
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001763 new_cpu = find_idlest_cpu(group, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001764 if (new_cpu == -1 || new_cpu == cpu) {
1765 /* Now try balancing at a lower domain level of cpu */
1766 sd = sd->child;
1767 continue;
1768 }
Nick Piggin476d1392005-06-25 14:57:29 -07001769
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001770 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07001771 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07001772 sd = NULL;
1773 weight = cpus_weight(span);
1774 for_each_domain(cpu, tmp) {
1775 if (weight <= cpus_weight(tmp->span))
1776 break;
1777 if (tmp->flags & flag)
1778 sd = tmp;
1779 }
1780 /* while loop will break here if sd == NULL */
1781 }
1782
1783 return cpu;
1784}
1785
1786#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788/***
1789 * try_to_wake_up - wake up a thread
1790 * @p: the to-be-woken-up thread
1791 * @state: the mask of task states that can be woken
1792 * @sync: do a synchronous wakeup?
1793 *
1794 * Put it on the run-queue if it's not already there. The "current"
1795 * thread is always on the run-queue (except when the actual
1796 * re-schedule is in progress), and as such you're allowed to do
1797 * the simpler "current->state = TASK_RUNNING" to mark yourself
1798 * runnable without the overhead of this.
1799 *
1800 * returns failure only if the task is already active.
1801 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001802static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
Ingo Molnarcc367732007-10-15 17:00:18 +02001804 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 unsigned long flags;
1806 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001807 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 rq = task_rq_lock(p, &flags);
1810 old_state = p->state;
1811 if (!(old_state & state))
1812 goto out;
1813
Ingo Molnardd41f592007-07-09 18:51:59 +02001814 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 goto out_running;
1816
1817 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02001818 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 this_cpu = smp_processor_id();
1820
1821#ifdef CONFIG_SMP
1822 if (unlikely(task_running(rq, p)))
1823 goto out_activate;
1824
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01001825 cpu = p->sched_class->select_task_rq(p, sync);
1826 if (cpu != orig_cpu) {
1827 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 task_rq_unlock(rq, &flags);
1829 /* might preempt at this point */
1830 rq = task_rq_lock(p, &flags);
1831 old_state = p->state;
1832 if (!(old_state & state))
1833 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02001834 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 goto out_running;
1836
1837 this_cpu = smp_processor_id();
1838 cpu = task_cpu(p);
1839 }
1840
Gregory Haskinse7693a32008-01-25 21:08:09 +01001841#ifdef CONFIG_SCHEDSTATS
1842 schedstat_inc(rq, ttwu_count);
1843 if (cpu == this_cpu)
1844 schedstat_inc(rq, ttwu_local);
1845 else {
1846 struct sched_domain *sd;
1847 for_each_domain(this_cpu, sd) {
1848 if (cpu_isset(cpu, sd->span)) {
1849 schedstat_inc(sd, ttwu_wake_remote);
1850 break;
1851 }
1852 }
1853 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01001854#endif
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856out_activate:
1857#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02001858 schedstat_inc(p, se.nr_wakeups);
1859 if (sync)
1860 schedstat_inc(p, se.nr_wakeups_sync);
1861 if (orig_cpu != cpu)
1862 schedstat_inc(p, se.nr_wakeups_migrate);
1863 if (cpu == this_cpu)
1864 schedstat_inc(p, se.nr_wakeups_local);
1865 else
1866 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02001867 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02001868 activate_task(rq, p, 1);
Ingo Molnar9c63d9c2007-10-15 17:00:20 +02001869 check_preempt_curr(rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 success = 1;
1871
1872out_running:
1873 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01001874#ifdef CONFIG_SMP
1875 if (p->sched_class->task_wake_up)
1876 p->sched_class->task_wake_up(rq, p);
1877#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878out:
1879 task_rq_unlock(rq, &flags);
1880
1881 return success;
1882}
1883
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001884int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001886 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888EXPORT_SYMBOL(wake_up_process);
1889
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001890int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
1892 return try_to_wake_up(p, state, 0);
1893}
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895/*
1896 * Perform scheduler related setup for a newly forked process p.
1897 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02001898 *
1899 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001901static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
Ingo Molnardd41f592007-07-09 18:51:59 +02001903 p->se.exec_start = 0;
1904 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02001905 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001906
1907#ifdef CONFIG_SCHEDSTATS
1908 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02001909 p->se.sum_sleep_runtime = 0;
1910 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02001911 p->se.block_start = 0;
1912 p->se.sleep_max = 0;
1913 p->se.block_max = 0;
1914 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02001915 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02001916 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001917#endif
Nick Piggin476d1392005-06-25 14:57:29 -07001918
Peter Zijlstrafa717062008-01-25 21:08:27 +01001919 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02001920 p->se.on_rq = 0;
Nick Piggin476d1392005-06-25 14:57:29 -07001921
Avi Kivitye107be32007-07-26 13:40:43 +02001922#ifdef CONFIG_PREEMPT_NOTIFIERS
1923 INIT_HLIST_HEAD(&p->preempt_notifiers);
1924#endif
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 /*
1927 * We mark the process as running here, but have not actually
1928 * inserted it onto the runqueue yet. This guarantees that
1929 * nobody will actually run it, and a signal or other external
1930 * event cannot wake it up and insert it on the runqueue either.
1931 */
1932 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02001933}
1934
1935/*
1936 * fork()/clone()-time setup:
1937 */
1938void sched_fork(struct task_struct *p, int clone_flags)
1939{
1940 int cpu = get_cpu();
1941
1942 __sched_fork(p);
1943
1944#ifdef CONFIG_SMP
1945 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
1946#endif
Ingo Molnar02e4bac2007-10-15 17:00:11 +02001947 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07001948
1949 /*
1950 * Make sure we do not leak PI boosting priority to the child:
1951 */
1952 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02001953 if (!rt_prio(p->prio))
1954 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07001955
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07001956#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02001957 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07001958 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08001960#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07001961 p->oncpu = 0;
1962#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07001964 /* Want to start with kernel preemption disabled. */
Al Viroa1261f52005-11-13 16:06:55 -08001965 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966#endif
Nick Piggin476d1392005-06-25 14:57:29 -07001967 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968}
1969
1970/*
1971 * wake_up_new_task - wake up a newly created task for the first time.
1972 *
1973 * This function will do some initial scheduler statistics housekeeping
1974 * that must be done for every newly created context, then puts the task
1975 * on the runqueue and wakes it.
1976 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001977void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
1979 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001980 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02001984 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 p->prio = effective_prio(p);
1987
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02001988 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001989 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02001992 * Let the scheduling class do new task startup
1993 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02001995 p->sched_class->task_new(rq, p);
Gerald Stralko5aff0532008-01-31 22:45:23 +01001996 inc_nr_running(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
Ingo Molnardd41f592007-07-09 18:51:59 +02001998 check_preempt_curr(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01001999#ifdef CONFIG_SMP
2000 if (p->sched_class->task_wake_up)
2001 p->sched_class->task_wake_up(rq, p);
2002#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002003 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004}
2005
Avi Kivitye107be32007-07-26 13:40:43 +02002006#ifdef CONFIG_PREEMPT_NOTIFIERS
2007
2008/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002009 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2010 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002011 */
2012void preempt_notifier_register(struct preempt_notifier *notifier)
2013{
2014 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2015}
2016EXPORT_SYMBOL_GPL(preempt_notifier_register);
2017
2018/**
2019 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002020 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002021 *
2022 * This is safe to call from within a preemption notifier.
2023 */
2024void preempt_notifier_unregister(struct preempt_notifier *notifier)
2025{
2026 hlist_del(&notifier->link);
2027}
2028EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2029
2030static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2031{
2032 struct preempt_notifier *notifier;
2033 struct hlist_node *node;
2034
2035 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2036 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2037}
2038
2039static void
2040fire_sched_out_preempt_notifiers(struct task_struct *curr,
2041 struct task_struct *next)
2042{
2043 struct preempt_notifier *notifier;
2044 struct hlist_node *node;
2045
2046 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2047 notifier->ops->sched_out(notifier, next);
2048}
2049
2050#else
2051
2052static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2053{
2054}
2055
2056static void
2057fire_sched_out_preempt_notifiers(struct task_struct *curr,
2058 struct task_struct *next)
2059{
2060}
2061
2062#endif
2063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002065 * prepare_task_switch - prepare to switch tasks
2066 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002067 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002068 * @next: the task we are going to switch to.
2069 *
2070 * This is called with the rq lock held and interrupts off. It must
2071 * be paired with a subsequent finish_task_switch after the context
2072 * switch.
2073 *
2074 * prepare_task_switch sets up locking and calls architecture specific
2075 * hooks.
2076 */
Avi Kivitye107be32007-07-26 13:40:43 +02002077static inline void
2078prepare_task_switch(struct rq *rq, struct task_struct *prev,
2079 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002080{
Avi Kivitye107be32007-07-26 13:40:43 +02002081 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002082 prepare_lock_switch(rq, next);
2083 prepare_arch_switch(next);
2084}
2085
2086/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002088 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 * @prev: the thread we just switched away from.
2090 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002091 * finish_task_switch must be called after the context switch, paired
2092 * with a prepare_task_switch call before the context switch.
2093 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2094 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 *
2096 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002097 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 * with the lock held can cause deadlocks; see schedule() for
2099 * details.)
2100 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002101static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 __releases(rq->lock)
2103{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002105 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107 rq->prev_mm = NULL;
2108
2109 /*
2110 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002111 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002112 * schedule one last time. The schedule call will never return, and
2113 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002114 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 * still held, otherwise prev could be scheduled on another cpu, die
2116 * there before we look at prev->state, and then the reference would
2117 * be dropped twice.
2118 * Manfred Spraul <manfred@colorfullife.com>
2119 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002120 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002121 finish_arch_switch(prev);
2122 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002123#ifdef CONFIG_SMP
2124 if (current->sched_class->post_schedule)
2125 current->sched_class->post_schedule(rq);
2126#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002127
Avi Kivitye107be32007-07-26 13:40:43 +02002128 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 if (mm)
2130 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002131 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002132 /*
2133 * Remove function-return probe instances associated with this
2134 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002135 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002136 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
2141/**
2142 * schedule_tail - first thing a freshly forked thread must call.
2143 * @prev: the thread we just switched away from.
2144 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002145asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 __releases(rq->lock)
2147{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002148 struct rq *rq = this_rq();
2149
Nick Piggin4866cde2005-06-25 14:57:23 -07002150 finish_task_switch(rq, prev);
2151#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2152 /* In this case, finish_task_switch does not reenable preemption */
2153 preempt_enable();
2154#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002156 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157}
2158
2159/*
2160 * context_switch - switch to the new MM and the new
2161 * thread's register state.
2162 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002163static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002164context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002165 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
Ingo Molnardd41f592007-07-09 18:51:59 +02002167 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Avi Kivitye107be32007-07-26 13:40:43 +02002169 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002170 mm = next->mm;
2171 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002172 /*
2173 * For paravirt, this is coupled with an exit in switch_to to
2174 * combine the page table reload and the switch backend into
2175 * one hypercall.
2176 */
2177 arch_enter_lazy_cpu_mode();
2178
Ingo Molnardd41f592007-07-09 18:51:59 +02002179 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 next->active_mm = oldmm;
2181 atomic_inc(&oldmm->mm_count);
2182 enter_lazy_tlb(oldmm, next);
2183 } else
2184 switch_mm(oldmm, mm, next);
2185
Ingo Molnardd41f592007-07-09 18:51:59 +02002186 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 rq->prev_mm = oldmm;
2189 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002190 /*
2191 * Since the runqueue lock will be released by the next
2192 * task (which is an invalid locking op but in the case
2193 * of the scheduler it's an obvious special-case), so we
2194 * do an early lockdep release here:
2195 */
2196#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002197 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002198#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
2200 /* Here we just switch the register state and the stack. */
2201 switch_to(prev, next, prev);
2202
Ingo Molnardd41f592007-07-09 18:51:59 +02002203 barrier();
2204 /*
2205 * this_rq must be evaluated again because prev may have moved
2206 * CPUs since it called schedule(), thus the 'rq' on its stack
2207 * frame will be invalid.
2208 */
2209 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
2211
2212/*
2213 * nr_running, nr_uninterruptible and nr_context_switches:
2214 *
2215 * externally visible scheduler statistics: current number of runnable
2216 * threads, current number of uninterruptible-sleeping threads, total
2217 * number of context switches performed since bootup.
2218 */
2219unsigned long nr_running(void)
2220{
2221 unsigned long i, sum = 0;
2222
2223 for_each_online_cpu(i)
2224 sum += cpu_rq(i)->nr_running;
2225
2226 return sum;
2227}
2228
2229unsigned long nr_uninterruptible(void)
2230{
2231 unsigned long i, sum = 0;
2232
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002233 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 sum += cpu_rq(i)->nr_uninterruptible;
2235
2236 /*
2237 * Since we read the counters lockless, it might be slightly
2238 * inaccurate. Do not allow it to go below zero though:
2239 */
2240 if (unlikely((long)sum < 0))
2241 sum = 0;
2242
2243 return sum;
2244}
2245
2246unsigned long long nr_context_switches(void)
2247{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002248 int i;
2249 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002251 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 sum += cpu_rq(i)->nr_switches;
2253
2254 return sum;
2255}
2256
2257unsigned long nr_iowait(void)
2258{
2259 unsigned long i, sum = 0;
2260
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002261 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2263
2264 return sum;
2265}
2266
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002267unsigned long nr_active(void)
2268{
2269 unsigned long i, running = 0, uninterruptible = 0;
2270
2271 for_each_online_cpu(i) {
2272 running += cpu_rq(i)->nr_running;
2273 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2274 }
2275
2276 if (unlikely((long)uninterruptible < 0))
2277 uninterruptible = 0;
2278
2279 return running + uninterruptible;
2280}
2281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002283 * Update rq->cpu_load[] statistics. This function is usually called every
2284 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002285 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002286static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002287{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002288 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002289 int i, scale;
2290
2291 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002292
2293 /* Update our load: */
2294 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2295 unsigned long old_load, new_load;
2296
2297 /* scale is effectively 1 << i now, and >> i divides by scale */
2298
2299 old_load = this_rq->cpu_load[i];
2300 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002301 /*
2302 * Round up the averaging division if load is increasing. This
2303 * prevents us from getting stuck on 9 if the load is 10, for
2304 * example.
2305 */
2306 if (new_load > old_load)
2307 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002308 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2309 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002310}
2311
Ingo Molnardd41f592007-07-09 18:51:59 +02002312#ifdef CONFIG_SMP
2313
Ingo Molnar48f24c42006-07-03 00:25:40 -07002314/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 * double_rq_lock - safely lock two runqueues
2316 *
2317 * Note this does not disable interrupts like task_rq_lock,
2318 * you need to do so manually before calling.
2319 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002320static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 __acquires(rq1->lock)
2322 __acquires(rq2->lock)
2323{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002324 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 if (rq1 == rq2) {
2326 spin_lock(&rq1->lock);
2327 __acquire(rq2->lock); /* Fake it out ;) */
2328 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002329 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 spin_lock(&rq1->lock);
2331 spin_lock(&rq2->lock);
2332 } else {
2333 spin_lock(&rq2->lock);
2334 spin_lock(&rq1->lock);
2335 }
2336 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002337 update_rq_clock(rq1);
2338 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339}
2340
2341/*
2342 * double_rq_unlock - safely unlock two runqueues
2343 *
2344 * Note this does not restore interrupts like task_rq_unlock,
2345 * you need to do so manually after calling.
2346 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002347static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 __releases(rq1->lock)
2349 __releases(rq2->lock)
2350{
2351 spin_unlock(&rq1->lock);
2352 if (rq1 != rq2)
2353 spin_unlock(&rq2->lock);
2354 else
2355 __release(rq2->lock);
2356}
2357
2358/*
2359 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2360 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01002361static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 __releases(this_rq->lock)
2363 __acquires(busiest->lock)
2364 __acquires(this_rq->lock)
2365{
Steven Rostedte8fa1362008-01-25 21:08:05 +01002366 int ret = 0;
2367
Kirill Korotaev054b9102006-12-10 02:20:11 -08002368 if (unlikely(!irqs_disabled())) {
2369 /* printk() doesn't work good under rq->lock */
2370 spin_unlock(&this_rq->lock);
2371 BUG_ON(1);
2372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002374 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 spin_unlock(&this_rq->lock);
2376 spin_lock(&busiest->lock);
2377 spin_lock(&this_rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01002378 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 } else
2380 spin_lock(&busiest->lock);
2381 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01002382 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
2384
2385/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 * If dest_cpu is allowed for this process, migrate the task to it.
2387 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002388 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 * the cpu_allowed mask is restored.
2390 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002391static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002393 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002395 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
2397 rq = task_rq_lock(p, &flags);
2398 if (!cpu_isset(dest_cpu, p->cpus_allowed)
2399 || unlikely(cpu_is_offline(dest_cpu)))
2400 goto out;
2401
2402 /* force the process onto the specified CPU */
2403 if (migrate_task(p, dest_cpu, &req)) {
2404 /* Need to wait for migration thread (might exit: take ref). */
2405 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07002406
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 get_task_struct(mt);
2408 task_rq_unlock(rq, &flags);
2409 wake_up_process(mt);
2410 put_task_struct(mt);
2411 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07002412
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 return;
2414 }
2415out:
2416 task_rq_unlock(rq, &flags);
2417}
2418
2419/*
Nick Piggin476d1392005-06-25 14:57:29 -07002420 * sched_exec - execve() is a valuable balancing opportunity, because at
2421 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 */
2423void sched_exec(void)
2424{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002426 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002428 if (new_cpu != this_cpu)
2429 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430}
2431
2432/*
2433 * pull_task - move a task from a remote runqueue to the local runqueue.
2434 * Both runqueues must be locked.
2435 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002436static void pull_task(struct rq *src_rq, struct task_struct *p,
2437 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02002439 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002441 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 /*
2443 * Note that idle threads have a prio of MAX_PRIO, for this test
2444 * to be always true for them.
2445 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002446 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447}
2448
2449/*
2450 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
2451 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002452static
Ingo Molnar70b97a72006-07-03 00:25:42 -07002453int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002454 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002455 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
2457 /*
2458 * We do not migrate tasks that are:
2459 * 1) running (obviously), or
2460 * 2) cannot be migrated to this CPU due to cpus_allowed, or
2461 * 3) are cache-hot on their current CPU.
2462 */
Ingo Molnarcc367732007-10-15 17:00:18 +02002463 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
2464 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002466 }
Nick Piggin81026792005-06-25 14:57:07 -07002467 *all_pinned = 0;
2468
Ingo Molnarcc367732007-10-15 17:00:18 +02002469 if (task_running(rq, p)) {
2470 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07002471 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Ingo Molnarda84d962007-10-15 17:00:18 +02002474 /*
2475 * Aggressive migration if:
2476 * 1) task is cache cold, or
2477 * 2) too many balance attempts have failed.
2478 */
2479
Ingo Molnar6bc16652007-10-15 17:00:18 +02002480 if (!task_hot(p, rq->clock, sd) ||
2481 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002482#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02002483 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002484 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02002485 schedstat_inc(p, se.nr_forced_migrations);
2486 }
Ingo Molnarda84d962007-10-15 17:00:18 +02002487#endif
2488 return 1;
2489 }
2490
Ingo Molnarcc367732007-10-15 17:00:18 +02002491 if (task_hot(p, rq->clock, sd)) {
2492 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02002493 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 return 1;
2496}
2497
Peter Williamse1d14842007-10-24 18:23:51 +02002498static unsigned long
2499balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
2500 unsigned long max_load_move, struct sched_domain *sd,
2501 enum cpu_idle_type idle, int *all_pinned,
2502 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02002503{
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002504 int loops = 0, pulled = 0, pinned = 0, skip_for_load;
Ingo Molnardd41f592007-07-09 18:51:59 +02002505 struct task_struct *p;
2506 long rem_load_move = max_load_move;
2507
Peter Williamse1d14842007-10-24 18:23:51 +02002508 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002509 goto out;
2510
2511 pinned = 1;
2512
2513 /*
2514 * Start the load-balancing iterator:
2515 */
2516 p = iterator->start(iterator->arg);
2517next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002518 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02002519 goto out;
2520 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002521 * To help distribute high priority tasks across CPUs we don't
Ingo Molnardd41f592007-07-09 18:51:59 +02002522 * skip a task if it will be the highest priority task (i.e. smallest
2523 * prio value) on its new queue regardless of its load weight
2524 */
2525 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
2526 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002527 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02002528 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002529 p = iterator->next(iterator->arg);
2530 goto next;
2531 }
2532
2533 pull_task(busiest, p, this_rq, this_cpu);
2534 pulled++;
2535 rem_load_move -= p->se.load.weight;
2536
2537 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002538 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002539 */
Peter Williamse1d14842007-10-24 18:23:51 +02002540 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002541 if (p->prio < *this_best_prio)
2542 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02002543 p = iterator->next(iterator->arg);
2544 goto next;
2545 }
2546out:
2547 /*
Peter Williamse1d14842007-10-24 18:23:51 +02002548 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02002549 * so we can safely collect pull_task() stats here rather than
2550 * inside pull_task().
2551 */
2552 schedstat_add(sd, lb_gained[idle], pulled);
2553
2554 if (all_pinned)
2555 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02002556
2557 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02002558}
Ingo Molnar48f24c42006-07-03 00:25:40 -07002559
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560/*
Peter Williams43010652007-08-09 11:16:46 +02002561 * move_tasks tries to move up to max_load_move weighted load from busiest to
2562 * this_rq, as part of a balancing operation within domain "sd".
2563 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 *
2565 * Called with both runqueues locked.
2566 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002567static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02002568 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002569 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07002570 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002572 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02002573 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002574 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Ingo Molnardd41f592007-07-09 18:51:59 +02002576 do {
Peter Williams43010652007-08-09 11:16:46 +02002577 total_load_moved +=
2578 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02002579 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002580 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02002581 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02002582 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Peter Williams43010652007-08-09 11:16:46 +02002584 return total_load_moved > 0;
2585}
2586
Peter Williamse1d14842007-10-24 18:23:51 +02002587static int
2588iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2589 struct sched_domain *sd, enum cpu_idle_type idle,
2590 struct rq_iterator *iterator)
2591{
2592 struct task_struct *p = iterator->start(iterator->arg);
2593 int pinned = 0;
2594
2595 while (p) {
2596 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
2597 pull_task(busiest, p, this_rq, this_cpu);
2598 /*
2599 * Right now, this is only the second place pull_task()
2600 * is called, so we can safely collect pull_task()
2601 * stats here rather than inside pull_task().
2602 */
2603 schedstat_inc(sd, lb_gained[idle]);
2604
2605 return 1;
2606 }
2607 p = iterator->next(iterator->arg);
2608 }
2609
2610 return 0;
2611}
2612
Peter Williams43010652007-08-09 11:16:46 +02002613/*
2614 * move_one_task tries to move exactly one task from busiest to this_rq, as
2615 * part of active balancing operations within "domain".
2616 * Returns 1 if successful and 0 otherwise.
2617 *
2618 * Called with both runqueues locked.
2619 */
2620static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2621 struct sched_domain *sd, enum cpu_idle_type idle)
2622{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002623 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02002624
2625 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02002626 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02002627 return 1;
2628
2629 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630}
2631
2632/*
2633 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07002634 * domain. It calculates and returns the amount of weighted load which
2635 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 */
2637static struct sched_group *
2638find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02002639 unsigned long *imbalance, enum cpu_idle_type idle,
2640 int *sd_idle, cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641{
2642 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
2643 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002644 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07002645 unsigned long busiest_load_per_task, busiest_nr_running;
2646 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02002647 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002648#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2649 int power_savings_balance = 1;
2650 unsigned long leader_nr_running = 0, min_load_per_task = 0;
2651 unsigned long min_nr_running = ULONG_MAX;
2652 struct sched_group *group_min = NULL, *group_leader = NULL;
2653#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
2655 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002656 busiest_load_per_task = busiest_nr_running = 0;
2657 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002658 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002659 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002660 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002661 load_idx = sd->newidle_idx;
2662 else
2663 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
2665 do {
Ken Chen908a7c12007-10-17 16:55:11 +02002666 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 int local_group;
2668 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02002669 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002670 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002671 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
2673 local_group = cpu_isset(this_cpu, group->cpumask);
2674
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002675 if (local_group)
2676 balance_cpu = first_cpu(group->cpumask);
2677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07002679 sum_weighted_load = sum_nr_running = avg_load = 0;
Ken Chen908a7c12007-10-17 16:55:11 +02002680 max_cpu_load = 0;
2681 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
2683 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002684 struct rq *rq;
2685
2686 if (!cpu_isset(i, *cpus))
2687 continue;
2688
2689 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07002690
Suresh Siddha9439aab2007-07-19 21:28:35 +02002691 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07002692 *sd_idle = 0;
2693
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002695 if (local_group) {
2696 if (idle_cpu(i) && !first_idle_cpu) {
2697 first_idle_cpu = 1;
2698 balance_cpu = i;
2699 }
2700
Nick Piggina2000572006-02-10 01:51:02 -08002701 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02002702 } else {
Nick Piggina2000572006-02-10 01:51:02 -08002703 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02002704 if (load > max_cpu_load)
2705 max_cpu_load = load;
2706 if (min_cpu_load > load)
2707 min_cpu_load = load;
2708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
2710 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07002711 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002712 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 }
2714
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002715 /*
2716 * First idle cpu or the first cpu(busiest) in this sched group
2717 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02002718 * domains. In the newly idle case, we will allow all the cpu's
2719 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002720 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02002721 if (idle != CPU_NEWLY_IDLE && local_group &&
2722 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002723 *balance = 0;
2724 goto ret;
2725 }
2726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07002728 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
2730 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002731 avg_load = sg_div_cpu_power(group,
2732 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Ken Chen908a7c12007-10-17 16:55:11 +02002734 if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
2735 __group_imb = 1;
2736
Eric Dumazet5517d862007-05-08 00:32:57 -07002737 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 if (local_group) {
2740 this_load = avg_load;
2741 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002742 this_nr_running = sum_nr_running;
2743 this_load_per_task = sum_weighted_load;
2744 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02002745 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 max_load = avg_load;
2747 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002748 busiest_nr_running = sum_nr_running;
2749 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02002750 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002752
2753#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2754 /*
2755 * Busy processors will not participate in power savings
2756 * balance.
2757 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002758 if (idle == CPU_NOT_IDLE ||
2759 !(sd->flags & SD_POWERSAVINGS_BALANCE))
2760 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002761
2762 /*
2763 * If the local group is idle or completely loaded
2764 * no need to do power savings balance at this domain
2765 */
2766 if (local_group && (this_nr_running >= group_capacity ||
2767 !this_nr_running))
2768 power_savings_balance = 0;
2769
Ingo Molnardd41f592007-07-09 18:51:59 +02002770 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002771 * If a group is already running at full capacity or idle,
2772 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02002773 */
2774 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002775 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02002776 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002777
Ingo Molnardd41f592007-07-09 18:51:59 +02002778 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002779 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002780 * This is the group from where we need to pick up the load
2781 * for saving power
2782 */
2783 if ((sum_nr_running < min_nr_running) ||
2784 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002785 first_cpu(group->cpumask) <
2786 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002787 group_min = group;
2788 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002789 min_load_per_task = sum_weighted_load /
2790 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002791 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002792
Ingo Molnardd41f592007-07-09 18:51:59 +02002793 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002794 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02002795 * capacity but still has some space to pick up some load
2796 * from other group and save more power
2797 */
2798 if (sum_nr_running <= group_capacity - 1) {
2799 if (sum_nr_running > leader_nr_running ||
2800 (sum_nr_running == leader_nr_running &&
2801 first_cpu(group->cpumask) >
2802 first_cpu(group_leader->cpumask))) {
2803 group_leader = group;
2804 leader_nr_running = sum_nr_running;
2805 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002806 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002807group_next:
2808#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 group = group->next;
2810 } while (group != sd->groups);
2811
Peter Williams2dd73a42006-06-27 02:54:34 -07002812 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 goto out_balanced;
2814
2815 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
2816
2817 if (this_load >= avg_load ||
2818 100*max_load <= sd->imbalance_pct*this_load)
2819 goto out_balanced;
2820
Peter Williams2dd73a42006-06-27 02:54:34 -07002821 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02002822 if (group_imb)
2823 busiest_load_per_task = min(busiest_load_per_task, avg_load);
2824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 /*
2826 * We're trying to get all the cpus to the average_load, so we don't
2827 * want to push ourselves above the average load, nor do we wish to
2828 * reduce the max loaded cpu below the average load, as either of these
2829 * actions would just result in more rebalancing later, and ping-pong
2830 * tasks around. Thus we look for the minimum possible imbalance.
2831 * Negative imbalances (*we* are more loaded than anyone else) will
2832 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002833 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 * appear as very large values with unsigned longs.
2835 */
Peter Williams2dd73a42006-06-27 02:54:34 -07002836 if (max_load <= busiest_load_per_task)
2837 goto out_balanced;
2838
2839 /*
2840 * In the presence of smp nice balancing, certain scenarios can have
2841 * max load less than avg load(as we skip the groups at or below
2842 * its cpu_power, while calculating max_load..)
2843 */
2844 if (max_load < avg_load) {
2845 *imbalance = 0;
2846 goto small_imbalance;
2847 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002848
2849 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07002850 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07002853 *imbalance = min(max_pull * busiest->__cpu_power,
2854 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 / SCHED_LOAD_SCALE;
2856
Peter Williams2dd73a42006-06-27 02:54:34 -07002857 /*
2858 * if *imbalance is less than the average load per runnable task
2859 * there is no gaurantee that any tasks will be moved so we'll have
2860 * a think about bumping its value to force at least one task to be
2861 * moved
2862 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02002863 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07002864 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07002865 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Peter Williams2dd73a42006-06-27 02:54:34 -07002867small_imbalance:
2868 pwr_move = pwr_now = 0;
2869 imbn = 2;
2870 if (this_nr_running) {
2871 this_load_per_task /= this_nr_running;
2872 if (busiest_load_per_task > this_load_per_task)
2873 imbn = 1;
2874 } else
2875 this_load_per_task = SCHED_LOAD_SCALE;
2876
Ingo Molnardd41f592007-07-09 18:51:59 +02002877 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
2878 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002879 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 return busiest;
2881 }
2882
2883 /*
2884 * OK, we don't have enough imbalance to justify moving tasks,
2885 * however we may be able to increase total CPU power used by
2886 * moving them.
2887 */
2888
Eric Dumazet5517d862007-05-08 00:32:57 -07002889 pwr_now += busiest->__cpu_power *
2890 min(busiest_load_per_task, max_load);
2891 pwr_now += this->__cpu_power *
2892 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 pwr_now /= SCHED_LOAD_SCALE;
2894
2895 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07002896 tmp = sg_div_cpu_power(busiest,
2897 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07002899 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07002900 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
2902 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07002903 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08002904 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07002905 tmp = sg_div_cpu_power(this,
2906 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 else
Eric Dumazet5517d862007-05-08 00:32:57 -07002908 tmp = sg_div_cpu_power(this,
2909 busiest_load_per_task * SCHED_LOAD_SCALE);
2910 pwr_move += this->__cpu_power *
2911 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 pwr_move /= SCHED_LOAD_SCALE;
2913
2914 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02002915 if (pwr_move > pwr_now)
2916 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 }
2918
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 return busiest;
2920
2921out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002922#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002923 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002924 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002926 if (this == group_leader && group_leader != group_min) {
2927 *imbalance = min_load_per_task;
2928 return group_min;
2929 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002930#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002931ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 *imbalance = 0;
2933 return NULL;
2934}
2935
2936/*
2937 * find_busiest_queue - find the busiest runqueue among the cpus in group.
2938 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002939static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002940find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002941 unsigned long imbalance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002943 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07002944 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 int i;
2946
2947 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002948 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002949
2950 if (!cpu_isset(i, *cpus))
2951 continue;
2952
Ingo Molnar48f24c42006-07-03 00:25:40 -07002953 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02002954 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
Ingo Molnardd41f592007-07-09 18:51:59 +02002956 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07002957 continue;
2958
Ingo Molnardd41f592007-07-09 18:51:59 +02002959 if (wl > max_load) {
2960 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07002961 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 }
2963 }
2964
2965 return busiest;
2966}
2967
2968/*
Nick Piggin77391d72005-06-25 14:57:30 -07002969 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
2970 * so long as it is large enough.
2971 */
2972#define MAX_PINNED_INTERVAL 512
2973
2974/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 * Check this_cpu to ensure it is balanced within domain. Attempt to move
2976 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002978static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002979 struct sched_domain *sd, enum cpu_idle_type idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002980 int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981{
Peter Williams43010652007-08-09 11:16:46 +02002982 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002985 struct rq *busiest;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002986 cpumask_t cpus = CPU_MASK_ALL;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002987 unsigned long flags;
Nick Piggin5969fe02005-09-10 00:26:19 -07002988
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002989 /*
2990 * When power savings policy is enabled for the parent domain, idle
2991 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02002992 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002993 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002994 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002995 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002996 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002997 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Ingo Molnar2d723762007-10-15 17:00:12 +02002999 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003001redo:
3002 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003003 &cpus, balance);
3004
Chen, Kenneth W06066712006-12-10 02:20:35 -08003005 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003006 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003007
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 if (!group) {
3009 schedstat_inc(sd, lb_nobusyg[idle]);
3010 goto out_balanced;
3011 }
3012
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003013 busiest = find_busiest_queue(group, idle, imbalance, &cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 if (!busiest) {
3015 schedstat_inc(sd, lb_nobusyq[idle]);
3016 goto out_balanced;
3017 }
3018
Nick Piggindb935db2005-06-25 14:57:11 -07003019 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
3021 schedstat_add(sd, lb_imbalance[idle], imbalance);
3022
Peter Williams43010652007-08-09 11:16:46 +02003023 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 if (busiest->nr_running > 1) {
3025 /*
3026 * Attempt to move tasks. If find_busiest_group has found
3027 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003028 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 * correctly treated as an imbalance.
3030 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003031 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003032 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003033 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003034 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003035 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003036 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003037
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003038 /*
3039 * some other cpu did the load balance for us.
3040 */
Peter Williams43010652007-08-09 11:16:46 +02003041 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003042 resched_cpu(this_cpu);
3043
Nick Piggin81026792005-06-25 14:57:07 -07003044 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003045 if (unlikely(all_pinned)) {
3046 cpu_clear(cpu_of(busiest), cpus);
3047 if (!cpus_empty(cpus))
3048 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003049 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 }
Nick Piggin81026792005-06-25 14:57:07 -07003052
Peter Williams43010652007-08-09 11:16:46 +02003053 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 schedstat_inc(sd, lb_failed[idle]);
3055 sd->nr_balance_failed++;
3056
3057 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003059 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003060
3061 /* don't kick the migration_thread, if the curr
3062 * task on busiest cpu can't be moved to this_cpu
3063 */
3064 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003065 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003066 all_pinned = 1;
3067 goto out_one_pinned;
3068 }
3069
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 if (!busiest->active_balance) {
3071 busiest->active_balance = 1;
3072 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003073 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003075 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003076 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 wake_up_process(busiest->migration_thread);
3078
3079 /*
3080 * We've kicked active balancing, reset the failure
3081 * counter.
3082 */
Nick Piggin39507452005-06-25 14:57:09 -07003083 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 }
Nick Piggin81026792005-06-25 14:57:07 -07003085 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 sd->nr_balance_failed = 0;
3087
Nick Piggin81026792005-06-25 14:57:07 -07003088 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 /* We were unbalanced, so reset the balancing interval */
3090 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003091 } else {
3092 /*
3093 * If we've begun active balancing, start to back off. This
3094 * case may not be covered by the all_pinned logic if there
3095 * is only 1 task on the busy runqueue (because we don't call
3096 * move_tasks).
3097 */
3098 if (sd->balance_interval < sd->max_interval)
3099 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 }
3101
Peter Williams43010652007-08-09 11:16:46 +02003102 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003103 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003104 return -1;
Peter Williams43010652007-08-09 11:16:46 +02003105 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
3107out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 schedstat_inc(sd, lb_balanced[idle]);
3109
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003110 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003111
3112out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003114 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3115 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 sd->balance_interval *= 2;
3117
Ingo Molnar48f24c42006-07-03 00:25:40 -07003118 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003119 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003120 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 return 0;
3122}
3123
3124/*
3125 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3126 * tasks if there is an imbalance.
3127 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003128 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 * this_rq is locked.
3130 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003131static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07003132load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133{
3134 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003135 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003137 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003138 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003139 int all_pinned = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003140 cpumask_t cpus = CPU_MASK_ALL;
Nick Piggin5969fe02005-09-10 00:26:19 -07003141
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003142 /*
3143 * When power savings policy is enabled for the parent domain, idle
3144 * sibling can pick up load irrespective of busy siblings. In this case,
3145 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003146 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003147 */
3148 if (sd->flags & SD_SHARE_CPUPOWER &&
3149 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003150 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
Ingo Molnar2d723762007-10-15 17:00:12 +02003152 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003153redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003154 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003155 &sd_idle, &cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003157 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003158 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 }
3160
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003161 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003162 &cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003163 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003164 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003165 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 }
3167
Nick Piggindb935db2005-06-25 14:57:11 -07003168 BUG_ON(busiest == this_rq);
3169
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003170 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003171
Peter Williams43010652007-08-09 11:16:46 +02003172 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003173 if (busiest->nr_running > 1) {
3174 /* Attempt to move tasks */
3175 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003176 /* this_rq->clock is already updated */
3177 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003178 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003179 imbalance, sd, CPU_NEWLY_IDLE,
3180 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003181 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003182
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003183 if (unlikely(all_pinned)) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003184 cpu_clear(cpu_of(busiest), cpus);
3185 if (!cpus_empty(cpus))
3186 goto redo;
3187 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003188 }
3189
Peter Williams43010652007-08-09 11:16:46 +02003190 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003191 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003192 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3193 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003194 return -1;
3195 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003196 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
Peter Williams43010652007-08-09 11:16:46 +02003198 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003199
3200out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003201 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003202 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003203 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003204 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003205 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003206
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208}
3209
3210/*
3211 * idle_balance is called by schedule() if this_cpu is about to become
3212 * idle. Attempts to pull tasks from other CPUs.
3213 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003214static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215{
3216 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003217 int pulled_task = -1;
3218 unsigned long next_balance = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
3220 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003221 unsigned long interval;
3222
3223 if (!(sd->flags & SD_LOAD_BALANCE))
3224 continue;
3225
3226 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003227 /* If we've pulled tasks over stop searching: */
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003228 pulled_task = load_balance_newidle(this_cpu,
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003229 this_rq, sd);
3230
3231 interval = msecs_to_jiffies(sd->balance_interval);
3232 if (time_after(next_balance, sd->last_balance + interval))
3233 next_balance = sd->last_balance + interval;
3234 if (pulled_task)
3235 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003237 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003238 /*
3239 * We are going idle. next_balance may be set based on
3240 * a busy processor. So reset next_balance.
3241 */
3242 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244}
3245
3246/*
3247 * active_load_balance is run by migration threads. It pushes running tasks
3248 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3249 * running on each physical CPU where possible, and avoids physical /
3250 * logical imbalances.
3251 *
3252 * Called with busiest_rq locked.
3253 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003254static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255{
Nick Piggin39507452005-06-25 14:57:09 -07003256 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003257 struct sched_domain *sd;
3258 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003259
Ingo Molnar48f24c42006-07-03 00:25:40 -07003260 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003261 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003262 return;
3263
3264 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
3266 /*
Nick Piggin39507452005-06-25 14:57:09 -07003267 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003268 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003269 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 */
Nick Piggin39507452005-06-25 14:57:09 -07003271 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
Nick Piggin39507452005-06-25 14:57:09 -07003273 /* move a task from busiest_rq to target_rq */
3274 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003275 update_rq_clock(busiest_rq);
3276 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
Nick Piggin39507452005-06-25 14:57:09 -07003278 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003279 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07003280 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003281 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07003282 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
Ingo Molnar48f24c42006-07-03 00:25:40 -07003285 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003286 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
Peter Williams43010652007-08-09 11:16:46 +02003288 if (move_one_task(target_rq, target_cpu, busiest_rq,
3289 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07003290 schedstat_inc(sd, alb_pushed);
3291 else
3292 schedstat_inc(sd, alb_failed);
3293 }
Nick Piggin39507452005-06-25 14:57:09 -07003294 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295}
3296
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003297#ifdef CONFIG_NO_HZ
3298static struct {
3299 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003300 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003301} nohz ____cacheline_aligned = {
3302 .load_balancer = ATOMIC_INIT(-1),
3303 .cpu_mask = CPU_MASK_NONE,
3304};
3305
Christoph Lameter7835b982006-12-10 02:20:22 -08003306/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003307 * This routine will try to nominate the ilb (idle load balancing)
3308 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3309 * load balancing on behalf of all those cpus. If all the cpus in the system
3310 * go into this tickless mode, then there will be no ilb owner (as there is
3311 * no need for one) and all the cpus will sleep till the next wakeup event
3312 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08003313 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003314 * For the ilb owner, tick is not stopped. And this tick will be used
3315 * for idle load balancing. ilb owner will still be part of
3316 * nohz.cpu_mask..
3317 *
3318 * While stopping the tick, this cpu will become the ilb owner if there
3319 * is no other owner. And will be the owner till that cpu becomes busy
3320 * or if all cpus in the system stop their ticks at which point
3321 * there is no need for ilb owner.
3322 *
3323 * When the ilb owner becomes busy, it nominates another owner, during the
3324 * next busy scheduler_tick()
3325 */
3326int select_nohz_load_balancer(int stop_tick)
3327{
3328 int cpu = smp_processor_id();
3329
3330 if (stop_tick) {
3331 cpu_set(cpu, nohz.cpu_mask);
3332 cpu_rq(cpu)->in_nohz_recently = 1;
3333
3334 /*
3335 * If we are going offline and still the leader, give up!
3336 */
3337 if (cpu_is_offline(cpu) &&
3338 atomic_read(&nohz.load_balancer) == cpu) {
3339 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3340 BUG();
3341 return 0;
3342 }
3343
3344 /* time for ilb owner also to sleep */
3345 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3346 if (atomic_read(&nohz.load_balancer) == cpu)
3347 atomic_set(&nohz.load_balancer, -1);
3348 return 0;
3349 }
3350
3351 if (atomic_read(&nohz.load_balancer) == -1) {
3352 /* make me the ilb owner */
3353 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
3354 return 1;
3355 } else if (atomic_read(&nohz.load_balancer) == cpu)
3356 return 1;
3357 } else {
3358 if (!cpu_isset(cpu, nohz.cpu_mask))
3359 return 0;
3360
3361 cpu_clear(cpu, nohz.cpu_mask);
3362
3363 if (atomic_read(&nohz.load_balancer) == cpu)
3364 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3365 BUG();
3366 }
3367 return 0;
3368}
3369#endif
3370
3371static DEFINE_SPINLOCK(balancing);
3372
3373/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003374 * It checks each scheduling domain to see if it is due to be balanced,
3375 * and initiates a balancing operation if so.
3376 *
3377 * Balancing parameters are set up in arch_init_sched_domains.
3378 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003379static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08003380{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003381 int balance = 1;
3382 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08003383 unsigned long interval;
3384 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003385 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08003386 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003387 int update_next_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003389 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 if (!(sd->flags & SD_LOAD_BALANCE))
3391 continue;
3392
3393 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003394 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 interval *= sd->busy_factor;
3396
3397 /* scale ms to jiffies */
3398 interval = msecs_to_jiffies(interval);
3399 if (unlikely(!interval))
3400 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003401 if (interval > HZ*NR_CPUS/10)
3402 interval = HZ*NR_CPUS/10;
3403
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Christoph Lameter08c183f2006-12-10 02:20:29 -08003405 if (sd->flags & SD_SERIALIZE) {
3406 if (!spin_trylock(&balancing))
3407 goto out;
3408 }
3409
Christoph Lameterc9819f42006-12-10 02:20:25 -08003410 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003411 if (load_balance(cpu, rq, sd, idle, &balance)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003412 /*
3413 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07003414 * longer idle, or one of our SMT siblings is
3415 * not idle.
3416 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003417 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003419 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08003421 if (sd->flags & SD_SERIALIZE)
3422 spin_unlock(&balancing);
3423out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02003424 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08003425 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003426 update_next_balance = 1;
3427 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003428
3429 /*
3430 * Stop the load balance at this level. There is another
3431 * CPU in our sched group which is doing load balancing more
3432 * actively.
3433 */
3434 if (!balance)
3435 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02003437
3438 /*
3439 * next_balance will be updated only when there is a need.
3440 * When the cpu is attached to null domain for ex, it will not be
3441 * updated.
3442 */
3443 if (likely(update_next_balance))
3444 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003445}
3446
3447/*
3448 * run_rebalance_domains is triggered when needed from the scheduler tick.
3449 * In CONFIG_NO_HZ case, the idle load balance owner will do the
3450 * rebalancing for all the cpus for whom scheduler ticks are stopped.
3451 */
3452static void run_rebalance_domains(struct softirq_action *h)
3453{
Ingo Molnardd41f592007-07-09 18:51:59 +02003454 int this_cpu = smp_processor_id();
3455 struct rq *this_rq = cpu_rq(this_cpu);
3456 enum cpu_idle_type idle = this_rq->idle_at_tick ?
3457 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003458
Ingo Molnardd41f592007-07-09 18:51:59 +02003459 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003460
3461#ifdef CONFIG_NO_HZ
3462 /*
3463 * If this cpu is the owner for idle load balancing, then do the
3464 * balancing on behalf of the other idle cpus whose ticks are
3465 * stopped.
3466 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003467 if (this_rq->idle_at_tick &&
3468 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003469 cpumask_t cpus = nohz.cpu_mask;
3470 struct rq *rq;
3471 int balance_cpu;
3472
Ingo Molnardd41f592007-07-09 18:51:59 +02003473 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003474 for_each_cpu_mask(balance_cpu, cpus) {
3475 /*
3476 * If this cpu gets work to do, stop the load balancing
3477 * work being done for other cpus. Next load
3478 * balancing owner will pick it up.
3479 */
3480 if (need_resched())
3481 break;
3482
Oleg Nesterovde0cf892007-08-12 18:08:19 +02003483 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003484
3485 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003486 if (time_after(this_rq->next_balance, rq->next_balance))
3487 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003488 }
3489 }
3490#endif
3491}
3492
3493/*
3494 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
3495 *
3496 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
3497 * idle load balancing owner or decide to stop the periodic load balancing,
3498 * if the whole system is idle.
3499 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003500static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003501{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003502#ifdef CONFIG_NO_HZ
3503 /*
3504 * If we were in the nohz mode recently and busy at the current
3505 * scheduler tick, then check if we need to nominate new idle
3506 * load balancer.
3507 */
3508 if (rq->in_nohz_recently && !rq->idle_at_tick) {
3509 rq->in_nohz_recently = 0;
3510
3511 if (atomic_read(&nohz.load_balancer) == cpu) {
3512 cpu_clear(cpu, nohz.cpu_mask);
3513 atomic_set(&nohz.load_balancer, -1);
3514 }
3515
3516 if (atomic_read(&nohz.load_balancer) == -1) {
3517 /*
3518 * simple selection for now: Nominate the
3519 * first cpu in the nohz list to be the next
3520 * ilb owner.
3521 *
3522 * TBD: Traverse the sched domains and nominate
3523 * the nearest cpu in the nohz.cpu_mask.
3524 */
3525 int ilb = first_cpu(nohz.cpu_mask);
3526
3527 if (ilb != NR_CPUS)
3528 resched_cpu(ilb);
3529 }
3530 }
3531
3532 /*
3533 * If this cpu is idle and doing idle load balancing for all the
3534 * cpus with ticks stopped, is it time for that to stop?
3535 */
3536 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
3537 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3538 resched_cpu(cpu);
3539 return;
3540 }
3541
3542 /*
3543 * If this cpu is idle and the idle load balancing is done by
3544 * someone else, then no need raise the SCHED_SOFTIRQ
3545 */
3546 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
3547 cpu_isset(cpu, nohz.cpu_mask))
3548 return;
3549#endif
3550 if (time_after_eq(jiffies, rq->next_balance))
3551 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552}
Ingo Molnardd41f592007-07-09 18:51:59 +02003553
3554#else /* CONFIG_SMP */
3555
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556/*
3557 * on UP we do not need to balance between CPUs:
3558 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003559static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560{
3561}
Ingo Molnardd41f592007-07-09 18:51:59 +02003562
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563#endif
3564
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565DEFINE_PER_CPU(struct kernel_stat, kstat);
3566
3567EXPORT_PER_CPU_SYMBOL(kstat);
3568
3569/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02003570 * Return p->sum_exec_runtime plus any more ns on the sched_clock
3571 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02003573unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003576 u64 ns, delta_exec;
3577 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003578
Ingo Molnar41b86e92007-07-09 18:51:58 +02003579 rq = task_rq_lock(p, &flags);
3580 ns = p->se.sum_exec_runtime;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01003581 if (task_current(rq, p)) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02003582 update_rq_clock(rq);
3583 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003584 if ((s64)delta_exec > 0)
3585 ns += delta_exec;
3586 }
3587 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003588
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 return ns;
3590}
3591
3592/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 * Account user cpu time to a process.
3594 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 * @cputime: the cpu time spent in user space since the last update
3596 */
3597void account_user_time(struct task_struct *p, cputime_t cputime)
3598{
3599 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3600 cputime64_t tmp;
3601
3602 p->utime = cputime_add(p->utime, cputime);
3603
3604 /* Add user time to cpustat. */
3605 tmp = cputime_to_cputime64(cputime);
3606 if (TASK_NICE(p) > 0)
3607 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3608 else
3609 cpustat->user = cputime64_add(cpustat->user, tmp);
3610}
3611
3612/*
Laurent Vivier94886b82007-10-15 17:00:19 +02003613 * Account guest cpu time to a process.
3614 * @p: the process that the cpu time gets accounted to
3615 * @cputime: the cpu time spent in virtual machine since the last update
3616 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01003617static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02003618{
3619 cputime64_t tmp;
3620 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3621
3622 tmp = cputime_to_cputime64(cputime);
3623
3624 p->utime = cputime_add(p->utime, cputime);
3625 p->gtime = cputime_add(p->gtime, cputime);
3626
3627 cpustat->user = cputime64_add(cpustat->user, tmp);
3628 cpustat->guest = cputime64_add(cpustat->guest, tmp);
3629}
3630
3631/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07003632 * Account scaled user cpu time to a process.
3633 * @p: the process that the cpu time gets accounted to
3634 * @cputime: the cpu time spent in user space since the last update
3635 */
3636void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
3637{
3638 p->utimescaled = cputime_add(p->utimescaled, cputime);
3639}
3640
3641/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 * Account system cpu time to a process.
3643 * @p: the process that the cpu time gets accounted to
3644 * @hardirq_offset: the offset to subtract from hardirq_count()
3645 * @cputime: the cpu time spent in kernel space since the last update
3646 */
3647void account_system_time(struct task_struct *p, int hardirq_offset,
3648 cputime_t cputime)
3649{
3650 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003651 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 cputime64_t tmp;
3653
Christian Borntraeger97783852007-11-15 20:57:39 +01003654 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0))
3655 return account_guest_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02003656
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 p->stime = cputime_add(p->stime, cputime);
3658
3659 /* Add system time to cpustat. */
3660 tmp = cputime_to_cputime64(cputime);
3661 if (hardirq_count() - hardirq_offset)
3662 cpustat->irq = cputime64_add(cpustat->irq, tmp);
3663 else if (softirq_count())
3664 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003665 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003667 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3669 else
3670 cpustat->idle = cputime64_add(cpustat->idle, tmp);
3671 /* Account for system time used */
3672 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673}
3674
3675/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07003676 * Account scaled system cpu time to a process.
3677 * @p: the process that the cpu time gets accounted to
3678 * @hardirq_offset: the offset to subtract from hardirq_count()
3679 * @cputime: the cpu time spent in kernel space since the last update
3680 */
3681void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
3682{
3683 p->stimescaled = cputime_add(p->stimescaled, cputime);
3684}
3685
3686/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 * Account for involuntary wait time.
3688 * @p: the process from which the cpu time has been stolen
3689 * @steal: the cpu time spent in involuntary wait
3690 */
3691void account_steal_time(struct task_struct *p, cputime_t steal)
3692{
3693 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3694 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07003695 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696
3697 if (p == rq->idle) {
3698 p->stime = cputime_add(p->stime, steal);
3699 if (atomic_read(&rq->nr_iowait) > 0)
3700 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3701 else
3702 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003703 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 cpustat->steal = cputime64_add(cpustat->steal, tmp);
3705}
3706
Christoph Lameter7835b982006-12-10 02:20:22 -08003707/*
3708 * This function gets called by the timer code, with HZ frequency.
3709 * We call it with interrupts disabled.
3710 *
3711 * It also gets called by the fork code, when changing the parent's
3712 * timeslices.
3713 */
3714void scheduler_tick(void)
3715{
Christoph Lameter7835b982006-12-10 02:20:22 -08003716 int cpu = smp_processor_id();
3717 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003718 struct task_struct *curr = rq->curr;
Ingo Molnar529c7722007-08-10 23:05:11 +02003719 u64 next_tick = rq->tick_timestamp + TICK_NSEC;
Christoph Lameter7835b982006-12-10 02:20:22 -08003720
Ingo Molnardd41f592007-07-09 18:51:59 +02003721 spin_lock(&rq->lock);
Ingo Molnar546fe3c2007-08-09 11:16:51 +02003722 __update_rq_clock(rq);
Ingo Molnar529c7722007-08-10 23:05:11 +02003723 /*
3724 * Let rq->clock advance by at least TICK_NSEC:
3725 */
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01003726 if (unlikely(rq->clock < next_tick)) {
Ingo Molnar529c7722007-08-10 23:05:11 +02003727 rq->clock = next_tick;
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01003728 rq->clock_underflows++;
3729 }
Ingo Molnar529c7722007-08-10 23:05:11 +02003730 rq->tick_timestamp = rq->clock;
Ingo Molnarf1a438d2007-08-09 11:16:45 +02003731 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01003732 curr->sched_class->task_tick(rq, curr, 0);
3733 update_sched_rt_period(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003734 spin_unlock(&rq->lock);
3735
Christoph Lametere418e1c2006-12-10 02:20:23 -08003736#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02003737 rq->idle_at_tick = idle_cpu(cpu);
3738 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08003739#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740}
3741
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
3743
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08003744void add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745{
3746 /*
3747 * Underflow?
3748 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003749 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
3750 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 preempt_count() += val;
3752 /*
3753 * Spinlock count overflowing soon?
3754 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003755 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
3756 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757}
3758EXPORT_SYMBOL(add_preempt_count);
3759
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08003760void sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761{
3762 /*
3763 * Underflow?
3764 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003765 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
3766 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 /*
3768 * Is the spinlock portion underflowing?
3769 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003770 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
3771 !(preempt_count() & PREEMPT_MASK)))
3772 return;
3773
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 preempt_count() -= val;
3775}
3776EXPORT_SYMBOL(sub_preempt_count);
3777
3778#endif
3779
3780/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003781 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003783static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784{
Satyam Sharma838225b2007-10-24 18:23:50 +02003785 struct pt_regs *regs = get_irq_regs();
3786
3787 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
3788 prev->comm, prev->pid, preempt_count());
3789
Ingo Molnardd41f592007-07-09 18:51:59 +02003790 debug_show_held_locks(prev);
3791 if (irqs_disabled())
3792 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02003793
3794 if (regs)
3795 show_regs(regs);
3796 else
3797 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02003798}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799
Ingo Molnardd41f592007-07-09 18:51:59 +02003800/*
3801 * Various schedule()-time debugging checks and statistics:
3802 */
3803static inline void schedule_debug(struct task_struct *prev)
3804{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003806 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 * schedule() atomically, we ignore that path for now.
3808 * Otherwise, whine if we are scheduling when we should not be.
3809 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003810 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
3811 __schedule_bug(prev);
3812
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
3814
Ingo Molnar2d723762007-10-15 17:00:12 +02003815 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02003816#ifdef CONFIG_SCHEDSTATS
3817 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003818 schedstat_inc(this_rq(), bkl_count);
3819 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02003820 }
3821#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02003822}
3823
3824/*
3825 * Pick up the highest-prio task:
3826 */
3827static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02003828pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02003829{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003830 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02003831 struct task_struct *p;
3832
3833 /*
3834 * Optimization: we know that if all tasks are in
3835 * the fair class we can call that function directly:
3836 */
3837 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003838 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003839 if (likely(p))
3840 return p;
3841 }
3842
3843 class = sched_class_highest;
3844 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003845 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003846 if (p)
3847 return p;
3848 /*
3849 * Will never be NULL as the idle class always
3850 * returns a non-NULL p:
3851 */
3852 class = class->next;
3853 }
3854}
3855
3856/*
3857 * schedule() is the main scheduler function.
3858 */
3859asmlinkage void __sched schedule(void)
3860{
3861 struct task_struct *prev, *next;
3862 long *switch_count;
3863 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02003864 int cpu;
3865
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866need_resched:
3867 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02003868 cpu = smp_processor_id();
3869 rq = cpu_rq(cpu);
3870 rcu_qsctr_inc(cpu);
3871 prev = rq->curr;
3872 switch_count = &prev->nivcsw;
3873
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 release_kernel_lock(prev);
3875need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876
Ingo Molnardd41f592007-07-09 18:51:59 +02003877 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003879 hrtick_clear(rq);
3880
Ingo Molnar1e819952007-10-15 17:00:13 +02003881 /*
3882 * Do the rq-clock update outside the rq lock:
3883 */
3884 local_irq_disable();
Ingo Molnarc1b3da32007-08-09 11:16:47 +02003885 __update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02003886 spin_lock(&rq->lock);
3887 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888
Ingo Molnardd41f592007-07-09 18:51:59 +02003889 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
3890 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
3891 unlikely(signal_pending(prev)))) {
3892 prev->state = TASK_RUNNING;
3893 } else {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02003894 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02003895 }
3896 switch_count = &prev->nvcsw;
3897 }
3898
Steven Rostedt9a897c52008-01-25 21:08:22 +01003899#ifdef CONFIG_SMP
3900 if (prev->sched_class->pre_schedule)
3901 prev->sched_class->pre_schedule(rq, prev);
3902#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01003903
Ingo Molnardd41f592007-07-09 18:51:59 +02003904 if (unlikely(!rq->nr_running))
3905 idle_balance(cpu, rq);
3906
Ingo Molnar31ee5292007-08-09 11:16:49 +02003907 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02003908 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909
3910 sched_info_switch(prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02003911
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 rq->nr_switches++;
3914 rq->curr = next;
3915 ++*switch_count;
3916
Ingo Molnardd41f592007-07-09 18:51:59 +02003917 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003918 /*
3919 * the context switch might have flipped the stack from under
3920 * us, hence refresh the local variables.
3921 */
3922 cpu = smp_processor_id();
3923 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 } else
3925 spin_unlock_irq(&rq->lock);
3926
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003927 hrtick_set(rq);
3928
3929 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003931
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 preempt_enable_no_resched();
3933 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
3934 goto need_resched;
3935}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936EXPORT_SYMBOL(schedule);
3937
3938#ifdef CONFIG_PREEMPT
3939/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003940 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003941 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 * occur there and call schedule directly.
3943 */
3944asmlinkage void __sched preempt_schedule(void)
3945{
3946 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 struct task_struct *task = current;
3948 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01003949
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 /*
3951 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003952 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07003954 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 return;
3956
Andi Kleen3a5c3592007-10-15 17:00:14 +02003957 do {
3958 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
Andi Kleen3a5c3592007-10-15 17:00:14 +02003960 /*
3961 * We keep the big kernel semaphore locked, but we
3962 * clear ->lock_depth so that schedule() doesnt
3963 * auto-release the semaphore:
3964 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02003965 saved_lock_depth = task->lock_depth;
3966 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02003967 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02003968 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02003969 sub_preempt_count(PREEMPT_ACTIVE);
3970
3971 /*
3972 * Check again in case we missed a preemption opportunity
3973 * between schedule and now.
3974 */
3975 barrier();
3976 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978EXPORT_SYMBOL(preempt_schedule);
3979
3980/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003981 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 * off of irq context.
3983 * Note, that this is called and return with irqs disabled. This will
3984 * protect us against recursive calling from irq.
3985 */
3986asmlinkage void __sched preempt_schedule_irq(void)
3987{
3988 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 struct task_struct *task = current;
3990 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01003991
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003992 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 BUG_ON(ti->preempt_count || !irqs_disabled());
3994
Andi Kleen3a5c3592007-10-15 17:00:14 +02003995 do {
3996 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997
Andi Kleen3a5c3592007-10-15 17:00:14 +02003998 /*
3999 * We keep the big kernel semaphore locked, but we
4000 * clear ->lock_depth so that schedule() doesnt
4001 * auto-release the semaphore:
4002 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004003 saved_lock_depth = task->lock_depth;
4004 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004005 local_irq_enable();
4006 schedule();
4007 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004008 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004009 sub_preempt_count(PREEMPT_ACTIVE);
4010
4011 /*
4012 * Check again in case we missed a preemption opportunity
4013 * between schedule and now.
4014 */
4015 barrier();
4016 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017}
4018
4019#endif /* CONFIG_PREEMPT */
4020
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004021int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4022 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004024 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026EXPORT_SYMBOL(default_wake_function);
4027
4028/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004029 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4030 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 * number) then we wake all the non-exclusive tasks and one exclusive task.
4032 *
4033 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004034 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4036 */
4037static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4038 int nr_exclusive, int sync, void *key)
4039{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004040 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004042 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004043 unsigned flags = curr->flags;
4044
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004046 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 break;
4048 }
4049}
4050
4051/**
4052 * __wake_up - wake up threads blocked on a waitqueue.
4053 * @q: the waitqueue
4054 * @mode: which threads
4055 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004056 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004058void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004059 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060{
4061 unsigned long flags;
4062
4063 spin_lock_irqsave(&q->lock, flags);
4064 __wake_up_common(q, mode, nr_exclusive, 0, key);
4065 spin_unlock_irqrestore(&q->lock, flags);
4066}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067EXPORT_SYMBOL(__wake_up);
4068
4069/*
4070 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4071 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004072void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073{
4074 __wake_up_common(q, mode, 1, 0, NULL);
4075}
4076
4077/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004078 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 * @q: the waitqueue
4080 * @mode: which threads
4081 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4082 *
4083 * The sync wakeup differs that the waker knows that it will schedule
4084 * away soon, so while the target thread will be woken up, it will not
4085 * be migrated to another CPU - ie. the two threads are 'synchronized'
4086 * with each other. This can prevent needless bouncing between CPUs.
4087 *
4088 * On UP it can prevent extra preemption.
4089 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004090void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004091__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092{
4093 unsigned long flags;
4094 int sync = 1;
4095
4096 if (unlikely(!q))
4097 return;
4098
4099 if (unlikely(!nr_exclusive))
4100 sync = 0;
4101
4102 spin_lock_irqsave(&q->lock, flags);
4103 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4104 spin_unlock_irqrestore(&q->lock, flags);
4105}
4106EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4107
Ingo Molnarb15136e2007-10-24 18:23:48 +02004108void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109{
4110 unsigned long flags;
4111
4112 spin_lock_irqsave(&x->wait.lock, flags);
4113 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004114 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 spin_unlock_irqrestore(&x->wait.lock, flags);
4116}
4117EXPORT_SYMBOL(complete);
4118
Ingo Molnarb15136e2007-10-24 18:23:48 +02004119void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120{
4121 unsigned long flags;
4122
4123 spin_lock_irqsave(&x->wait.lock, flags);
4124 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004125 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 spin_unlock_irqrestore(&x->wait.lock, flags);
4127}
4128EXPORT_SYMBOL(complete_all);
4129
Andi Kleen8cbbe862007-10-15 17:00:14 +02004130static inline long __sched
4131do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 if (!x->done) {
4134 DECLARE_WAITQUEUE(wait, current);
4135
4136 wait.flags |= WQ_FLAG_EXCLUSIVE;
4137 __add_wait_queue_tail(&x->wait, &wait);
4138 do {
Matthew Wilcox009e5772007-12-06 12:29:54 -05004139 if ((state == TASK_INTERRUPTIBLE &&
4140 signal_pending(current)) ||
4141 (state == TASK_KILLABLE &&
4142 fatal_signal_pending(current))) {
Andi Kleen8cbbe862007-10-15 17:00:14 +02004143 __remove_wait_queue(&x->wait, &wait);
4144 return -ERESTARTSYS;
4145 }
4146 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004148 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 spin_lock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004150 if (!timeout) {
4151 __remove_wait_queue(&x->wait, &wait);
4152 return timeout;
4153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 } while (!x->done);
4155 __remove_wait_queue(&x->wait, &wait);
4156 }
4157 x->done--;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004158 return timeout;
4159}
4160
4161static long __sched
4162wait_for_common(struct completion *x, long timeout, int state)
4163{
4164 might_sleep();
4165
4166 spin_lock_irq(&x->wait.lock);
4167 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004169 return timeout;
4170}
4171
Ingo Molnarb15136e2007-10-24 18:23:48 +02004172void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004173{
4174 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175}
4176EXPORT_SYMBOL(wait_for_completion);
4177
Ingo Molnarb15136e2007-10-24 18:23:48 +02004178unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4180{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004181 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182}
4183EXPORT_SYMBOL(wait_for_completion_timeout);
4184
Andi Kleen8cbbe862007-10-15 17:00:14 +02004185int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186{
Andi Kleen51e97992007-10-18 21:32:55 +02004187 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4188 if (t == -ERESTARTSYS)
4189 return t;
4190 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191}
4192EXPORT_SYMBOL(wait_for_completion_interruptible);
4193
Ingo Molnarb15136e2007-10-24 18:23:48 +02004194unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195wait_for_completion_interruptible_timeout(struct completion *x,
4196 unsigned long timeout)
4197{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004198 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199}
4200EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4201
Matthew Wilcox009e5772007-12-06 12:29:54 -05004202int __sched wait_for_completion_killable(struct completion *x)
4203{
4204 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4205 if (t == -ERESTARTSYS)
4206 return t;
4207 return 0;
4208}
4209EXPORT_SYMBOL(wait_for_completion_killable);
4210
Andi Kleen8cbbe862007-10-15 17:00:14 +02004211static long __sched
4212sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004213{
4214 unsigned long flags;
4215 wait_queue_t wait;
4216
4217 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Andi Kleen8cbbe862007-10-15 17:00:14 +02004219 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220
Andi Kleen8cbbe862007-10-15 17:00:14 +02004221 spin_lock_irqsave(&q->lock, flags);
4222 __add_wait_queue(q, &wait);
4223 spin_unlock(&q->lock);
4224 timeout = schedule_timeout(timeout);
4225 spin_lock_irq(&q->lock);
4226 __remove_wait_queue(q, &wait);
4227 spin_unlock_irqrestore(&q->lock, flags);
4228
4229 return timeout;
4230}
4231
4232void __sched interruptible_sleep_on(wait_queue_head_t *q)
4233{
4234 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236EXPORT_SYMBOL(interruptible_sleep_on);
4237
Ingo Molnar0fec1712007-07-09 18:52:01 +02004238long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004239interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004241 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4244
Ingo Molnar0fec1712007-07-09 18:52:01 +02004245void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004247 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249EXPORT_SYMBOL(sleep_on);
4250
Ingo Molnar0fec1712007-07-09 18:52:01 +02004251long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004253 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255EXPORT_SYMBOL(sleep_on_timeout);
4256
Ingo Molnarb29739f2006-06-27 02:54:51 -07004257#ifdef CONFIG_RT_MUTEXES
4258
4259/*
4260 * rt_mutex_setprio - set the current priority of a task
4261 * @p: task
4262 * @prio: prio value (kernel-internal form)
4263 *
4264 * This function changes the 'effective' priority of a task. It does
4265 * not touch ->normal_prio like __setscheduler().
4266 *
4267 * Used by the rt_mutex code to implement priority inheritance logic.
4268 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004269void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004270{
4271 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004272 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004273 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01004274 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004275
4276 BUG_ON(prio < 0 || prio > MAX_PRIO);
4277
4278 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004279 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004280
Andrew Mortond5f9f942007-05-08 20:27:06 -07004281 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004282 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004283 running = task_current(rq, p);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004284 if (on_rq) {
Ingo Molnar69be72c2007-08-09 11:16:49 +02004285 dequeue_task(rq, p, 0);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004286 if (running)
4287 p->sched_class->put_prev_task(rq, p);
4288 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004289
4290 if (rt_prio(prio))
4291 p->sched_class = &rt_sched_class;
4292 else
4293 p->sched_class = &fair_sched_class;
4294
Ingo Molnarb29739f2006-06-27 02:54:51 -07004295 p->prio = prio;
4296
Ingo Molnardd41f592007-07-09 18:51:59 +02004297 if (on_rq) {
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004298 if (running)
4299 p->sched_class->set_curr_task(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01004300
Ingo Molnar8159f872007-08-09 11:16:49 +02004301 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004302
4303 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004304 }
4305 task_rq_unlock(rq, &flags);
4306}
4307
4308#endif
4309
Ingo Molnar36c8b582006-07-03 00:25:41 -07004310void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311{
Ingo Molnardd41f592007-07-09 18:51:59 +02004312 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004314 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
4316 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
4317 return;
4318 /*
4319 * We have to be careful, if called from sys_setpriority(),
4320 * the task might be in the middle of scheduling on another CPU.
4321 */
4322 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004323 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 /*
4325 * The RT priorities are set via sched_setscheduler(), but we still
4326 * allow the 'normal' nice value to be set - but as expected
4327 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02004328 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 */
Ingo Molnare05606d2007-07-09 18:51:59 +02004330 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 p->static_prio = NICE_TO_PRIO(nice);
4332 goto out_unlock;
4333 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004334 on_rq = p->se.on_rq;
Srivatsa Vaddagiri58e2d4c2008-01-25 21:08:00 +01004335 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004336 dequeue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07004339 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004340 old_prio = p->prio;
4341 p->prio = effective_prio(p);
4342 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
Ingo Molnardd41f592007-07-09 18:51:59 +02004344 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004345 enqueue_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07004347 * If the task increased its priority or is running and
4348 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004350 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 resched_task(rq->curr);
4352 }
4353out_unlock:
4354 task_rq_unlock(rq, &flags);
4355}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356EXPORT_SYMBOL(set_user_nice);
4357
Matt Mackalle43379f2005-05-01 08:59:00 -07004358/*
4359 * can_nice - check if a task can reduce its nice value
4360 * @p: task
4361 * @nice: nice value
4362 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004363int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07004364{
Matt Mackall024f4742005-08-18 11:24:19 -07004365 /* convert nice value [19,-20] to rlimit style value [1,40] */
4366 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004367
Matt Mackalle43379f2005-05-01 08:59:00 -07004368 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
4369 capable(CAP_SYS_NICE));
4370}
4371
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372#ifdef __ARCH_WANT_SYS_NICE
4373
4374/*
4375 * sys_nice - change the priority of the current process.
4376 * @increment: priority increment
4377 *
4378 * sys_setpriority is a more generic, but much slower function that
4379 * does similar things.
4380 */
4381asmlinkage long sys_nice(int increment)
4382{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004383 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
4385 /*
4386 * Setpriority might change our priority at the same moment.
4387 * We don't have to worry. Conceptually one call occurs first
4388 * and we have a single winner.
4389 */
Matt Mackalle43379f2005-05-01 08:59:00 -07004390 if (increment < -40)
4391 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 if (increment > 40)
4393 increment = 40;
4394
4395 nice = PRIO_TO_NICE(current->static_prio) + increment;
4396 if (nice < -20)
4397 nice = -20;
4398 if (nice > 19)
4399 nice = 19;
4400
Matt Mackalle43379f2005-05-01 08:59:00 -07004401 if (increment < 0 && !can_nice(current, nice))
4402 return -EPERM;
4403
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 retval = security_task_setnice(current, nice);
4405 if (retval)
4406 return retval;
4407
4408 set_user_nice(current, nice);
4409 return 0;
4410}
4411
4412#endif
4413
4414/**
4415 * task_prio - return the priority value of a given task.
4416 * @p: the task in question.
4417 *
4418 * This is the priority value as seen by users in /proc.
4419 * RT tasks are offset by -200. Normal tasks are centered
4420 * around 0, value goes from -16 to +15.
4421 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004422int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423{
4424 return p->prio - MAX_RT_PRIO;
4425}
4426
4427/**
4428 * task_nice - return the nice value of a given task.
4429 * @p: the task in question.
4430 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004431int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432{
4433 return TASK_NICE(p);
4434}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435EXPORT_SYMBOL_GPL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
4437/**
4438 * idle_cpu - is a given cpu idle currently?
4439 * @cpu: the processor in question.
4440 */
4441int idle_cpu(int cpu)
4442{
4443 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
4444}
4445
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446/**
4447 * idle_task - return the idle task for a given cpu.
4448 * @cpu: the processor in question.
4449 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004450struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451{
4452 return cpu_rq(cpu)->idle;
4453}
4454
4455/**
4456 * find_process_by_pid - find a process with a matching PID value.
4457 * @pid: the pid in question.
4458 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004459static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07004461 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462}
4463
4464/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02004465static void
4466__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467{
Ingo Molnardd41f592007-07-09 18:51:59 +02004468 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004469
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02004471 switch (p->policy) {
4472 case SCHED_NORMAL:
4473 case SCHED_BATCH:
4474 case SCHED_IDLE:
4475 p->sched_class = &fair_sched_class;
4476 break;
4477 case SCHED_FIFO:
4478 case SCHED_RR:
4479 p->sched_class = &rt_sched_class;
4480 break;
4481 }
4482
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004484 p->normal_prio = normal_prio(p);
4485 /* we are holding p->pi_lock already */
4486 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07004487 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488}
4489
4490/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004491 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 * @p: the task in question.
4493 * @policy: new policy.
4494 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004495 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004496 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004498int sched_setscheduler(struct task_struct *p, int policy,
4499 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004501 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01004503 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004504 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505
Steven Rostedt66e53932006-06-27 02:54:44 -07004506 /* may grab non-irq protected spin_locks */
4507 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508recheck:
4509 /* double check policy once rq lock held */
4510 if (policy < 0)
4511 policy = oldpolicy = p->policy;
4512 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02004513 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
4514 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08004515 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 /*
4517 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02004518 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
4519 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 */
4521 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004522 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04004523 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02004525 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 return -EINVAL;
4527
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004528 /*
4529 * Allow unprivileged RT tasks to decrease priority:
4530 */
4531 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02004532 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004533 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004534
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004535 if (!lock_task_sighand(p, &flags))
4536 return -ESRCH;
4537 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
4538 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004539
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004540 /* can't set/change the rt policy */
4541 if (policy != p->policy && !rlim_rtprio)
4542 return -EPERM;
4543
4544 /* can't increase priority */
4545 if (param->sched_priority > p->rt_priority &&
4546 param->sched_priority > rlim_rtprio)
4547 return -EPERM;
4548 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004549 /*
4550 * Like positive nice levels, dont allow tasks to
4551 * move out of SCHED_IDLE either:
4552 */
4553 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
4554 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004555
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004556 /* can't change other user's priorities */
4557 if ((current->euid != p->euid) &&
4558 (current->euid != p->uid))
4559 return -EPERM;
4560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
4562 retval = security_task_setscheduler(p, policy, param);
4563 if (retval)
4564 return retval;
4565 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07004566 * make sure no PI-waiters arrive (or leave) while we are
4567 * changing the priority of the task:
4568 */
4569 spin_lock_irqsave(&p->pi_lock, flags);
4570 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 * To be able to change p->policy safely, the apropriate
4572 * runqueue lock must be held.
4573 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07004574 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 /* recheck policy now with rq lock held */
4576 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
4577 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004578 __task_rq_unlock(rq);
4579 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 goto recheck;
4581 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02004582 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004583 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004584 running = task_current(rq, p);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004585 if (on_rq) {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004586 deactivate_task(rq, p, 0);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004587 if (running)
4588 p->sched_class->put_prev_task(rq, p);
4589 }
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02004590
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004592 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02004593
Ingo Molnardd41f592007-07-09 18:51:59 +02004594 if (on_rq) {
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004595 if (running)
4596 p->sched_class->set_curr_task(rq);
Steven Rostedtcb469842008-01-25 21:08:22 +01004597
Ingo Molnardd41f592007-07-09 18:51:59 +02004598 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004599
4600 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07004602 __task_rq_unlock(rq);
4603 spin_unlock_irqrestore(&p->pi_lock, flags);
4604
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07004605 rt_mutex_adjust_pi(p);
4606
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 return 0;
4608}
4609EXPORT_SYMBOL_GPL(sched_setscheduler);
4610
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004611static int
4612do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 struct sched_param lparam;
4615 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004616 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617
4618 if (!param || pid < 0)
4619 return -EINVAL;
4620 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
4621 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004622
4623 rcu_read_lock();
4624 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004626 if (p != NULL)
4627 retval = sched_setscheduler(p, policy, &lparam);
4628 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07004629
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 return retval;
4631}
4632
4633/**
4634 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4635 * @pid: the pid in question.
4636 * @policy: new policy.
4637 * @param: structure containing the new RT priority.
4638 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004639asmlinkage long
4640sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641{
Jason Baronc21761f2006-01-18 17:43:03 -08004642 /* negative values for policy are not valid */
4643 if (policy < 0)
4644 return -EINVAL;
4645
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 return do_sched_setscheduler(pid, policy, param);
4647}
4648
4649/**
4650 * sys_sched_setparam - set/change the RT priority of a thread
4651 * @pid: the pid in question.
4652 * @param: structure containing the new RT priority.
4653 */
4654asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
4655{
4656 return do_sched_setscheduler(pid, -1, param);
4657}
4658
4659/**
4660 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4661 * @pid: the pid in question.
4662 */
4663asmlinkage long sys_sched_getscheduler(pid_t pid)
4664{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004665 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004666 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
4668 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004669 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
4671 retval = -ESRCH;
4672 read_lock(&tasklist_lock);
4673 p = find_process_by_pid(pid);
4674 if (p) {
4675 retval = security_task_getscheduler(p);
4676 if (!retval)
4677 retval = p->policy;
4678 }
4679 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 return retval;
4681}
4682
4683/**
4684 * sys_sched_getscheduler - get the RT priority of a thread
4685 * @pid: the pid in question.
4686 * @param: structure containing the RT priority.
4687 */
4688asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
4689{
4690 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004691 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004692 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693
4694 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004695 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
4697 read_lock(&tasklist_lock);
4698 p = find_process_by_pid(pid);
4699 retval = -ESRCH;
4700 if (!p)
4701 goto out_unlock;
4702
4703 retval = security_task_getscheduler(p);
4704 if (retval)
4705 goto out_unlock;
4706
4707 lp.sched_priority = p->rt_priority;
4708 read_unlock(&tasklist_lock);
4709
4710 /*
4711 * This one might sleep, we cannot do it with a spinlock held ...
4712 */
4713 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
4714
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 return retval;
4716
4717out_unlock:
4718 read_unlock(&tasklist_lock);
4719 return retval;
4720}
4721
4722long sched_setaffinity(pid_t pid, cpumask_t new_mask)
4723{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 cpumask_t cpus_allowed;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004725 struct task_struct *p;
4726 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004728 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 read_lock(&tasklist_lock);
4730
4731 p = find_process_by_pid(pid);
4732 if (!p) {
4733 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004734 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 return -ESRCH;
4736 }
4737
4738 /*
4739 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004740 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 * usage count and then drop tasklist_lock.
4742 */
4743 get_task_struct(p);
4744 read_unlock(&tasklist_lock);
4745
4746 retval = -EPERM;
4747 if ((current->euid != p->euid) && (current->euid != p->uid) &&
4748 !capable(CAP_SYS_NICE))
4749 goto out_unlock;
4750
David Quigleye7834f82006-06-23 02:03:59 -07004751 retval = security_task_setscheduler(p, 0, NULL);
4752 if (retval)
4753 goto out_unlock;
4754
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 cpus_allowed = cpuset_cpus_allowed(p);
4756 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07004757 again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 retval = set_cpus_allowed(p, new_mask);
4759
Paul Menage8707d8b2007-10-18 23:40:22 -07004760 if (!retval) {
4761 cpus_allowed = cpuset_cpus_allowed(p);
4762 if (!cpus_subset(new_mask, cpus_allowed)) {
4763 /*
4764 * We must have raced with a concurrent cpuset
4765 * update. Just reset the cpus_allowed to the
4766 * cpuset's cpus_allowed
4767 */
4768 new_mask = cpus_allowed;
4769 goto again;
4770 }
4771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772out_unlock:
4773 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004774 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 return retval;
4776}
4777
4778static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
4779 cpumask_t *new_mask)
4780{
4781 if (len < sizeof(cpumask_t)) {
4782 memset(new_mask, 0, sizeof(cpumask_t));
4783 } else if (len > sizeof(cpumask_t)) {
4784 len = sizeof(cpumask_t);
4785 }
4786 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
4787}
4788
4789/**
4790 * sys_sched_setaffinity - set the cpu affinity of a process
4791 * @pid: pid of the process
4792 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4793 * @user_mask_ptr: user-space pointer to the new cpu mask
4794 */
4795asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
4796 unsigned long __user *user_mask_ptr)
4797{
4798 cpumask_t new_mask;
4799 int retval;
4800
4801 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
4802 if (retval)
4803 return retval;
4804
4805 return sched_setaffinity(pid, new_mask);
4806}
4807
4808/*
4809 * Represents all cpu's present in the system
4810 * In systems capable of hotplug, this map could dynamically grow
4811 * as new cpu's are detected in the system via any platform specific
4812 * method, such as ACPI for e.g.
4813 */
4814
Andi Kleen4cef0c62006-01-11 22:44:57 +01004815cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816EXPORT_SYMBOL(cpu_present_map);
4817
4818#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01004819cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07004820EXPORT_SYMBOL(cpu_online_map);
4821
Andi Kleen4cef0c62006-01-11 22:44:57 +01004822cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07004823EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824#endif
4825
4826long sched_getaffinity(pid_t pid, cpumask_t *mask)
4827{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004828 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004831 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 read_lock(&tasklist_lock);
4833
4834 retval = -ESRCH;
4835 p = find_process_by_pid(pid);
4836 if (!p)
4837 goto out_unlock;
4838
David Quigleye7834f82006-06-23 02:03:59 -07004839 retval = security_task_getscheduler(p);
4840 if (retval)
4841 goto out_unlock;
4842
Jack Steiner2f7016d2006-02-01 03:05:18 -08004843 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844
4845out_unlock:
4846 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004847 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848
Ulrich Drepper9531b622007-08-09 11:16:46 +02004849 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850}
4851
4852/**
4853 * sys_sched_getaffinity - get the cpu affinity of a process
4854 * @pid: pid of the process
4855 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4856 * @user_mask_ptr: user-space pointer to hold the current cpu mask
4857 */
4858asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
4859 unsigned long __user *user_mask_ptr)
4860{
4861 int ret;
4862 cpumask_t mask;
4863
4864 if (len < sizeof(cpumask_t))
4865 return -EINVAL;
4866
4867 ret = sched_getaffinity(pid, &mask);
4868 if (ret < 0)
4869 return ret;
4870
4871 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
4872 return -EFAULT;
4873
4874 return sizeof(cpumask_t);
4875}
4876
4877/**
4878 * sys_sched_yield - yield the current processor to other threads.
4879 *
Ingo Molnardd41f592007-07-09 18:51:59 +02004880 * This function yields the current CPU to other tasks. If there are no
4881 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 */
4883asmlinkage long sys_sched_yield(void)
4884{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004885 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886
Ingo Molnar2d723762007-10-15 17:00:12 +02004887 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02004888 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889
4890 /*
4891 * Since we are going to call schedule() anyway, there's
4892 * no need to preempt or enable interrupts:
4893 */
4894 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07004895 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 _raw_spin_unlock(&rq->lock);
4897 preempt_enable_no_resched();
4898
4899 schedule();
4900
4901 return 0;
4902}
4903
Andrew Mortone7b38402006-06-30 01:56:00 -07004904static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07004906#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
4907 __might_sleep(__FILE__, __LINE__);
4908#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07004909 /*
4910 * The BKS might be reacquired before we have dropped
4911 * PREEMPT_ACTIVE, which could trigger a second
4912 * cond_resched() call.
4913 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 do {
4915 add_preempt_count(PREEMPT_ACTIVE);
4916 schedule();
4917 sub_preempt_count(PREEMPT_ACTIVE);
4918 } while (need_resched());
4919}
4920
Herbert Xu02b67cc32008-01-25 21:08:28 +01004921#if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY)
4922int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923{
Ingo Molnar94142322006-12-29 16:48:13 -08004924 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
4925 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 __cond_resched();
4927 return 1;
4928 }
4929 return 0;
4930}
Herbert Xu02b67cc32008-01-25 21:08:28 +01004931EXPORT_SYMBOL(_cond_resched);
4932#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
4934/*
4935 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
4936 * call schedule, and on return reacquire the lock.
4937 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004938 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 * operations here to prevent schedule() from being called twice (once via
4940 * spin_unlock(), once by hand).
4941 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004942int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943{
Nick Piggin95c354f2008-01-30 13:31:20 +01004944 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07004945 int ret = 0;
4946
Nick Piggin95c354f2008-01-30 13:31:20 +01004947 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01004949 if (resched && need_resched())
4950 __cond_resched();
4951 else
4952 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07004953 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 }
Jan Kara6df3cec2005-06-13 15:52:32 -07004956 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958EXPORT_SYMBOL(cond_resched_lock);
4959
4960int __sched cond_resched_softirq(void)
4961{
4962 BUG_ON(!in_softirq());
4963
Ingo Molnar94142322006-12-29 16:48:13 -08004964 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07004965 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 __cond_resched();
4967 local_bh_disable();
4968 return 1;
4969 }
4970 return 0;
4971}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972EXPORT_SYMBOL(cond_resched_softirq);
4973
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974/**
4975 * yield - yield the current processor to other threads.
4976 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004977 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 * thread runnable and calls sys_sched_yield().
4979 */
4980void __sched yield(void)
4981{
4982 set_current_state(TASK_RUNNING);
4983 sys_sched_yield();
4984}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985EXPORT_SYMBOL(yield);
4986
4987/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004988 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 * that process accounting knows that this is a task in IO wait state.
4990 *
4991 * But don't do that if it is a deliberate, throttling IO wait (this task
4992 * has set its backing_dev_info: the queue against which it should throttle)
4993 */
4994void __sched io_schedule(void)
4995{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004996 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004998 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 atomic_inc(&rq->nr_iowait);
5000 schedule();
5001 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005002 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004EXPORT_SYMBOL(io_schedule);
5005
5006long __sched io_schedule_timeout(long timeout)
5007{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005008 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 long ret;
5010
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005011 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 atomic_inc(&rq->nr_iowait);
5013 ret = schedule_timeout(timeout);
5014 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005015 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 return ret;
5017}
5018
5019/**
5020 * sys_sched_get_priority_max - return maximum RT priority.
5021 * @policy: scheduling class.
5022 *
5023 * this syscall returns the maximum rt_priority that can be used
5024 * by a given scheduling class.
5025 */
5026asmlinkage long sys_sched_get_priority_max(int policy)
5027{
5028 int ret = -EINVAL;
5029
5030 switch (policy) {
5031 case SCHED_FIFO:
5032 case SCHED_RR:
5033 ret = MAX_USER_RT_PRIO-1;
5034 break;
5035 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005036 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005037 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 ret = 0;
5039 break;
5040 }
5041 return ret;
5042}
5043
5044/**
5045 * sys_sched_get_priority_min - return minimum RT priority.
5046 * @policy: scheduling class.
5047 *
5048 * this syscall returns the minimum rt_priority that can be used
5049 * by a given scheduling class.
5050 */
5051asmlinkage long sys_sched_get_priority_min(int policy)
5052{
5053 int ret = -EINVAL;
5054
5055 switch (policy) {
5056 case SCHED_FIFO:
5057 case SCHED_RR:
5058 ret = 1;
5059 break;
5060 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005061 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005062 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 ret = 0;
5064 }
5065 return ret;
5066}
5067
5068/**
5069 * sys_sched_rr_get_interval - return the default timeslice of a process.
5070 * @pid: pid of the process.
5071 * @interval: userspace pointer to the timeslice value.
5072 *
5073 * this syscall writes the default timeslice value of a given process
5074 * into the user-space timespec buffer. A value of '0' means infinity.
5075 */
5076asmlinkage
5077long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5078{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005079 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005080 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005081 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083
5084 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005085 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086
5087 retval = -ESRCH;
5088 read_lock(&tasklist_lock);
5089 p = find_process_by_pid(pid);
5090 if (!p)
5091 goto out_unlock;
5092
5093 retval = security_task_getscheduler(p);
5094 if (retval)
5095 goto out_unlock;
5096
Ingo Molnar77034932007-12-04 17:04:39 +01005097 /*
5098 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5099 * tasks that are on an otherwise idle runqueue:
5100 */
5101 time_slice = 0;
5102 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005103 time_slice = DEF_TIMESLICE;
Ingo Molnar77034932007-12-04 17:04:39 +01005104 } else {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005105 struct sched_entity *se = &p->se;
5106 unsigned long flags;
5107 struct rq *rq;
5108
5109 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005110 if (rq->cfs.load.weight)
5111 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005112 task_rq_unlock(rq, &flags);
5113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005115 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005118
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119out_unlock:
5120 read_unlock(&tasklist_lock);
5121 return retval;
5122}
5123
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005124static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07005125
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005126void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005129 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005132 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005133 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005134#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005136 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005138 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139#else
5140 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005141 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005143 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144#endif
5145#ifdef CONFIG_DEBUG_STACK_USAGE
5146 {
Al Viro10ebffd2005-11-13 16:06:56 -08005147 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 while (!*n)
5149 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005150 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 }
5152#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005153 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005154 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005156 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157}
5158
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005159void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005161 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162
Ingo Molnar4bd77322007-07-11 21:21:47 +02005163#if BITS_PER_LONG == 32
5164 printk(KERN_INFO
5165 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005167 printk(KERN_INFO
5168 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169#endif
5170 read_lock(&tasklist_lock);
5171 do_each_thread(g, p) {
5172 /*
5173 * reset the NMI-timeout, listing all files on a slow
5174 * console might take alot of time:
5175 */
5176 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005177 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005178 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 } while_each_thread(g, p);
5180
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005181 touch_all_softlockup_watchdogs();
5182
Ingo Molnardd41f592007-07-09 18:51:59 +02005183#ifdef CONFIG_SCHED_DEBUG
5184 sysrq_sched_debug_show();
5185#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005187 /*
5188 * Only show locks if all tasks are dumped:
5189 */
5190 if (state_filter == -1)
5191 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192}
5193
Ingo Molnar1df21052007-07-09 18:51:58 +02005194void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5195{
Ingo Molnardd41f592007-07-09 18:51:59 +02005196 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005197}
5198
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005199/**
5200 * init_idle - set up an idle thread for a given CPU
5201 * @idle: task in question
5202 * @cpu: cpu the idle task belongs to
5203 *
5204 * NOTE: this function does not set the idle thread's NEED_RESCHED
5205 * flag, to make booting more robust.
5206 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005207void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005209 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 unsigned long flags;
5211
Ingo Molnardd41f592007-07-09 18:51:59 +02005212 __sched_fork(idle);
5213 idle->se.exec_start = sched_clock();
5214
Ingo Molnarb29739f2006-06-27 02:54:51 -07005215 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005217 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218
5219 spin_lock_irqsave(&rq->lock, flags);
5220 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005221#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5222 idle->oncpu = 1;
5223#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 spin_unlock_irqrestore(&rq->lock, flags);
5225
5226 /* Set the preempt count _outside_ the spinlocks! */
Al Viroa1261f52005-11-13 16:06:55 -08005227 task_thread_info(idle)->preempt_count = 0;
Ingo Molnar6478d882008-01-25 21:08:33 +01005228
Ingo Molnardd41f592007-07-09 18:51:59 +02005229 /*
5230 * The idle tasks have their own, simple scheduling class:
5231 */
5232 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233}
5234
5235/*
5236 * In a system that switches off the HZ timer nohz_cpu_mask
5237 * indicates which cpus entered this state. This is used
5238 * in the rcu update to wait only for active cpus. For system
5239 * which do not switch off the HZ timer nohz_cpu_mask should
5240 * always be CPU_MASK_NONE.
5241 */
5242cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
5243
Ingo Molnar19978ca2007-11-09 22:39:38 +01005244/*
5245 * Increase the granularity value when there are more CPUs,
5246 * because with more CPUs the 'effective latency' as visible
5247 * to users decreases. But the relationship is not linear,
5248 * so pick a second-best guess by going with the log2 of the
5249 * number of CPUs.
5250 *
5251 * This idea comes from the SD scheduler of Con Kolivas:
5252 */
5253static inline void sched_init_granularity(void)
5254{
5255 unsigned int factor = 1 + ilog2(num_online_cpus());
5256 const unsigned long limit = 200000000;
5257
5258 sysctl_sched_min_granularity *= factor;
5259 if (sysctl_sched_min_granularity > limit)
5260 sysctl_sched_min_granularity = limit;
5261
5262 sysctl_sched_latency *= factor;
5263 if (sysctl_sched_latency > limit)
5264 sysctl_sched_latency = limit;
5265
5266 sysctl_sched_wakeup_granularity *= factor;
5267 sysctl_sched_batch_wakeup_granularity *= factor;
5268}
5269
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270#ifdef CONFIG_SMP
5271/*
5272 * This is how migration works:
5273 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07005274 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 * runqueue and wake up that CPU's migration thread.
5276 * 2) we down() the locked semaphore => thread blocks.
5277 * 3) migration thread wakes up (implicitly it forces the migrated
5278 * thread off the CPU)
5279 * 4) it gets the migration request and checks whether the migrated
5280 * task is still in the wrong runqueue.
5281 * 5) if it's in the wrong runqueue then the migration thread removes
5282 * it and puts it into the right queue.
5283 * 6) migration thread up()s the semaphore.
5284 * 7) we wake up and the migration is done.
5285 */
5286
5287/*
5288 * Change a given task's CPU affinity. Migrate the thread to a
5289 * proper CPU and schedule it away if the CPU it's executing on
5290 * is removed from the allowed bitmask.
5291 *
5292 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005293 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 * call is not atomic; no spinlocks may be held.
5295 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005296int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005298 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005300 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005301 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302
5303 rq = task_rq_lock(p, &flags);
5304 if (!cpus_intersects(new_mask, cpu_online_map)) {
5305 ret = -EINVAL;
5306 goto out;
5307 }
5308
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005309 if (p->sched_class->set_cpus_allowed)
5310 p->sched_class->set_cpus_allowed(p, &new_mask);
5311 else {
Ingo Molnar0eab9142008-01-25 21:08:19 +01005312 p->cpus_allowed = new_mask;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01005313 p->rt.nr_cpus_allowed = cpus_weight(new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005314 }
5315
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 /* Can the task run on the task's current CPU? If so, we're done */
5317 if (cpu_isset(task_cpu(p), new_mask))
5318 goto out;
5319
5320 if (migrate_task(p, any_online_cpu(new_mask), &req)) {
5321 /* Need help from migration thread: drop lock and wait. */
5322 task_rq_unlock(rq, &flags);
5323 wake_up_process(rq->migration_thread);
5324 wait_for_completion(&req.done);
5325 tlb_migrate_finish(p->mm);
5326 return 0;
5327 }
5328out:
5329 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005330
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 return ret;
5332}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333EXPORT_SYMBOL_GPL(set_cpus_allowed);
5334
5335/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005336 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 * this because either it can't run here any more (set_cpus_allowed()
5338 * away from this CPU, or CPU going down), or because we're
5339 * attempting to rebalance this task on exec (sched_exec).
5340 *
5341 * So we race with normal scheduler movements, but that's OK, as long
5342 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07005343 *
5344 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07005346static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005348 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02005349 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
5351 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07005352 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353
5354 rq_src = cpu_rq(src_cpu);
5355 rq_dest = cpu_rq(dest_cpu);
5356
5357 double_rq_lock(rq_src, rq_dest);
5358 /* Already moved. */
5359 if (task_cpu(p) != src_cpu)
5360 goto out;
5361 /* Affinity changed (again). */
5362 if (!cpu_isset(dest_cpu, p->cpus_allowed))
5363 goto out;
5364
Ingo Molnardd41f592007-07-09 18:51:59 +02005365 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005366 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005367 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005368
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005370 if (on_rq) {
5371 activate_task(rq_dest, p, 0);
5372 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07005374 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375out:
5376 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07005377 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378}
5379
5380/*
5381 * migration_thread - this is a highprio system thread that performs
5382 * thread migration by bumping thread off CPU then 'pushing' onto
5383 * another runqueue.
5384 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005385static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005388 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389
5390 rq = cpu_rq(cpu);
5391 BUG_ON(rq->migration_thread != current);
5392
5393 set_current_state(TASK_INTERRUPTIBLE);
5394 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005395 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 spin_lock_irq(&rq->lock);
5399
5400 if (cpu_is_offline(cpu)) {
5401 spin_unlock_irq(&rq->lock);
5402 goto wait_to_die;
5403 }
5404
5405 if (rq->active_balance) {
5406 active_load_balance(rq, cpu);
5407 rq->active_balance = 0;
5408 }
5409
5410 head = &rq->migration_queue;
5411
5412 if (list_empty(head)) {
5413 spin_unlock_irq(&rq->lock);
5414 schedule();
5415 set_current_state(TASK_INTERRUPTIBLE);
5416 continue;
5417 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07005418 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 list_del_init(head->next);
5420
Nick Piggin674311d2005-06-25 14:57:27 -07005421 spin_unlock(&rq->lock);
5422 __migrate_task(req->task, cpu, req->dest_cpu);
5423 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424
5425 complete(&req->done);
5426 }
5427 __set_current_state(TASK_RUNNING);
5428 return 0;
5429
5430wait_to_die:
5431 /* Wait for kthread_stop */
5432 set_current_state(TASK_INTERRUPTIBLE);
5433 while (!kthread_should_stop()) {
5434 schedule();
5435 set_current_state(TASK_INTERRUPTIBLE);
5436 }
5437 __set_current_state(TASK_RUNNING);
5438 return 0;
5439}
5440
5441#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005442
5443static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
5444{
5445 int ret;
5446
5447 local_irq_disable();
5448 ret = __migrate_task(p, src_cpu, dest_cpu);
5449 local_irq_enable();
5450 return ret;
5451}
5452
Kirill Korotaev054b9102006-12-10 02:20:11 -08005453/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02005454 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08005455 * NOTE: interrupts should be disabled by the caller
5456 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005457static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458{
Kirill Korotaevefc30812006-06-27 02:54:32 -07005459 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005461 struct rq *rq;
5462 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463
Andi Kleen3a5c3592007-10-15 17:00:14 +02005464 do {
5465 /* On same node? */
5466 mask = node_to_cpumask(cpu_to_node(dead_cpu));
5467 cpus_and(mask, mask, p->cpus_allowed);
5468 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469
Andi Kleen3a5c3592007-10-15 17:00:14 +02005470 /* On any allowed CPU? */
5471 if (dest_cpu == NR_CPUS)
5472 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473
Andi Kleen3a5c3592007-10-15 17:00:14 +02005474 /* No more Mr. Nice Guy. */
5475 if (dest_cpu == NR_CPUS) {
Cliff Wickman470fd642007-10-18 23:40:46 -07005476 cpumask_t cpus_allowed = cpuset_cpus_allowed_locked(p);
5477 /*
5478 * Try to stay on the same cpuset, where the
5479 * current cpuset may be a subset of all cpus.
5480 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005481 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07005482 * called within calls to cpuset_lock/cpuset_unlock.
5483 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02005484 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07005485 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005486 dest_cpu = any_online_cpu(p->cpus_allowed);
5487 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488
Andi Kleen3a5c3592007-10-15 17:00:14 +02005489 /*
5490 * Don't tell them about moving exiting tasks or
5491 * kernel threads (both mm NULL), since they never
5492 * leave kernel.
5493 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005494 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02005495 printk(KERN_INFO "process %d (%s) no "
5496 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005497 task_pid_nr(p), p->comm, dead_cpu);
5498 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02005499 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005500 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501}
5502
5503/*
5504 * While a dead CPU has no uninterruptible tasks queued at this point,
5505 * it might still have a nonzero ->nr_uninterruptible counter, because
5506 * for performance reasons the counter is not stricly tracking tasks to
5507 * their home CPUs. So we just add the counter to another CPU's counter,
5508 * to keep the global sum constant after CPU-down:
5509 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07005510static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005512 struct rq *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 unsigned long flags;
5514
5515 local_irq_save(flags);
5516 double_rq_lock(rq_src, rq_dest);
5517 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
5518 rq_src->nr_uninterruptible = 0;
5519 double_rq_unlock(rq_src, rq_dest);
5520 local_irq_restore(flags);
5521}
5522
5523/* Run through task list and migrate tasks from the dead cpu. */
5524static void migrate_live_tasks(int src_cpu)
5525{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005526 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005528 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
Ingo Molnar48f24c42006-07-03 00:25:40 -07005530 do_each_thread(t, p) {
5531 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 continue;
5533
Ingo Molnar48f24c42006-07-03 00:25:40 -07005534 if (task_cpu(p) == src_cpu)
5535 move_task_off_dead_cpu(src_cpu, p);
5536 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005538 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539}
5540
Ingo Molnardd41f592007-07-09 18:51:59 +02005541/*
5542 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005543 * It does so by boosting its priority to highest possible.
5544 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 */
5546void sched_idle_next(void)
5547{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005548 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07005549 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 struct task_struct *p = rq->idle;
5551 unsigned long flags;
5552
5553 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005554 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555
Ingo Molnar48f24c42006-07-03 00:25:40 -07005556 /*
5557 * Strictly not necessary since rest of the CPUs are stopped by now
5558 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 */
5560 spin_lock_irqsave(&rq->lock, flags);
5561
Ingo Molnardd41f592007-07-09 18:51:59 +02005562 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005563
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005564 update_rq_clock(rq);
5565 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
5567 spin_unlock_irqrestore(&rq->lock, flags);
5568}
5569
Ingo Molnar48f24c42006-07-03 00:25:40 -07005570/*
5571 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 * offline.
5573 */
5574void idle_task_exit(void)
5575{
5576 struct mm_struct *mm = current->active_mm;
5577
5578 BUG_ON(cpu_online(smp_processor_id()));
5579
5580 if (mm != &init_mm)
5581 switch_mm(mm, &init_mm, current);
5582 mmdrop(mm);
5583}
5584
Kirill Korotaev054b9102006-12-10 02:20:11 -08005585/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005586static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005588 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589
5590 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07005591 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
5593 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07005594 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595
Ingo Molnar48f24c42006-07-03 00:25:40 -07005596 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597
5598 /*
5599 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005600 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 * fine.
5602 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005603 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005604 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005605 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606
Ingo Molnar48f24c42006-07-03 00:25:40 -07005607 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608}
5609
5610/* release_task() removes task from tasklist, so we won't find dead tasks. */
5611static void migrate_dead_tasks(unsigned int dead_cpu)
5612{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005613 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005614 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615
Ingo Molnardd41f592007-07-09 18:51:59 +02005616 for ( ; ; ) {
5617 if (!rq->nr_running)
5618 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02005619 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02005620 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02005621 if (!next)
5622 break;
5623 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02005624
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 }
5626}
5627#endif /* CONFIG_HOTPLUG_CPU */
5628
Nick Piggine692ab52007-07-26 13:40:43 +02005629#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5630
5631static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005632 {
5633 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005634 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005635 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01005636 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02005637};
5638
5639static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005640 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005641 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005642 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005643 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005644 .child = sd_ctl_dir,
5645 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01005646 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02005647};
5648
5649static struct ctl_table *sd_alloc_ctl_entry(int n)
5650{
5651 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02005652 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02005653
Nick Piggine692ab52007-07-26 13:40:43 +02005654 return entry;
5655}
5656
Milton Miller6382bc92007-10-15 17:00:19 +02005657static void sd_free_ctl_entry(struct ctl_table **tablep)
5658{
Milton Millercd7900762007-10-17 16:55:11 +02005659 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02005660
Milton Millercd7900762007-10-17 16:55:11 +02005661 /*
5662 * In the intermediate directories, both the child directory and
5663 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005664 * will always be set. In the lowest directory the names are
Milton Millercd7900762007-10-17 16:55:11 +02005665 * static strings and all have proc handlers.
5666 */
5667 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02005668 if (entry->child)
5669 sd_free_ctl_entry(&entry->child);
Milton Millercd7900762007-10-17 16:55:11 +02005670 if (entry->proc_handler == NULL)
5671 kfree(entry->procname);
5672 }
Milton Miller6382bc92007-10-15 17:00:19 +02005673
5674 kfree(*tablep);
5675 *tablep = NULL;
5676}
5677
Nick Piggine692ab52007-07-26 13:40:43 +02005678static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02005679set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02005680 const char *procname, void *data, int maxlen,
5681 mode_t mode, proc_handler *proc_handler)
5682{
Nick Piggine692ab52007-07-26 13:40:43 +02005683 entry->procname = procname;
5684 entry->data = data;
5685 entry->maxlen = maxlen;
5686 entry->mode = mode;
5687 entry->proc_handler = proc_handler;
5688}
5689
5690static struct ctl_table *
5691sd_alloc_ctl_domain_table(struct sched_domain *sd)
5692{
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005693 struct ctl_table *table = sd_alloc_ctl_entry(12);
Nick Piggine692ab52007-07-26 13:40:43 +02005694
Milton Millerad1cdc12007-10-15 17:00:19 +02005695 if (table == NULL)
5696 return NULL;
5697
Alexey Dobriyane0361852007-08-09 11:16:46 +02005698 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005699 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005700 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005701 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005702 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005703 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005704 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005705 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005706 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005707 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005708 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005709 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005710 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005711 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005712 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02005713 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005714 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02005715 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005716 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02005717 &sd->cache_nice_tries,
5718 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005719 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02005720 sizeof(int), 0644, proc_dointvec_minmax);
Milton Miller6323469f2007-10-15 17:00:19 +02005721 /* &table[11] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02005722
5723 return table;
5724}
5725
Ingo Molnar9a4e7152007-11-28 15:52:56 +01005726static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02005727{
5728 struct ctl_table *entry, *table;
5729 struct sched_domain *sd;
5730 int domain_num = 0, i;
5731 char buf[32];
5732
5733 for_each_domain(cpu, sd)
5734 domain_num++;
5735 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02005736 if (table == NULL)
5737 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02005738
5739 i = 0;
5740 for_each_domain(cpu, sd) {
5741 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005742 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005743 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005744 entry->child = sd_alloc_ctl_domain_table(sd);
5745 entry++;
5746 i++;
5747 }
5748 return table;
5749}
5750
5751static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02005752static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005753{
5754 int i, cpu_num = num_online_cpus();
5755 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
5756 char buf[32];
5757
Milton Miller73785472007-10-24 18:23:48 +02005758 WARN_ON(sd_ctl_dir[0].child);
5759 sd_ctl_dir[0].child = entry;
5760
Milton Millerad1cdc12007-10-15 17:00:19 +02005761 if (entry == NULL)
5762 return;
5763
Milton Miller97b6ea72007-10-15 17:00:19 +02005764 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02005765 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005766 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005767 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005768 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02005769 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02005770 }
Milton Miller73785472007-10-24 18:23:48 +02005771
5772 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02005773 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
5774}
Milton Miller6382bc92007-10-15 17:00:19 +02005775
Milton Miller73785472007-10-24 18:23:48 +02005776/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02005777static void unregister_sched_domain_sysctl(void)
5778{
Milton Miller73785472007-10-24 18:23:48 +02005779 if (sd_sysctl_header)
5780 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02005781 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02005782 if (sd_ctl_dir[0].child)
5783 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02005784}
Nick Piggine692ab52007-07-26 13:40:43 +02005785#else
Milton Miller6382bc92007-10-15 17:00:19 +02005786static void register_sched_domain_sysctl(void)
5787{
5788}
5789static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005790{
5791}
5792#endif
5793
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794/*
5795 * migration_call - callback that gets triggered when a CPU is added.
5796 * Here we can start up the necessary migration thread for the new CPU.
5797 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005798static int __cpuinit
5799migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005802 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005804 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805
5806 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005807
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005809 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02005810 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 if (IS_ERR(p))
5812 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 kthread_bind(p, cpu);
5814 /* Must be high prio: stop_machine expects to yield to it. */
5815 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02005816 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 task_rq_unlock(rq, &flags);
5818 cpu_rq(cpu)->migration_thread = p;
5819 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005820
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005822 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02005823 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins57d885f2008-01-25 21:08:18 +01005825
5826 /* Update our root-domain */
5827 rq = cpu_rq(cpu);
5828 spin_lock_irqsave(&rq->lock, flags);
5829 if (rq->rd) {
5830 BUG_ON(!cpu_isset(cpu, rq->rd->span));
5831 cpu_set(cpu, rq->rd->online);
5832 }
5833 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005835
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836#ifdef CONFIG_HOTPLUG_CPU
5837 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005838 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07005839 if (!cpu_rq(cpu)->migration_thread)
5840 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005841 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08005842 kthread_bind(cpu_rq(cpu)->migration_thread,
5843 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 kthread_stop(cpu_rq(cpu)->migration_thread);
5845 cpu_rq(cpu)->migration_thread = NULL;
5846 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005847
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005849 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07005850 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 migrate_live_tasks(cpu);
5852 rq = cpu_rq(cpu);
5853 kthread_stop(rq->migration_thread);
5854 rq->migration_thread = NULL;
5855 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07005856 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005857 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005858 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02005860 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
5861 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07005863 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07005864 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 migrate_nr_uninterruptible(rq);
5866 BUG_ON(rq->nr_running != 0);
5867
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005868 /*
5869 * No need to migrate the tasks: it was best-effort if
5870 * they didn't take sched_hotcpu_mutex. Just wake up
5871 * the requestors.
5872 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 spin_lock_irq(&rq->lock);
5874 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005875 struct migration_req *req;
5876
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07005878 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 list_del_init(&req->list);
5880 complete(&req->done);
5881 }
5882 spin_unlock_irq(&rq->lock);
5883 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005884
5885 case CPU_DOWN_PREPARE:
5886 /* Update our root-domain */
5887 rq = cpu_rq(cpu);
5888 spin_lock_irqsave(&rq->lock, flags);
5889 if (rq->rd) {
5890 BUG_ON(!cpu_isset(cpu, rq->rd->span));
5891 cpu_clear(cpu, rq->rd->online);
5892 }
5893 spin_unlock_irqrestore(&rq->lock, flags);
5894 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895#endif
5896 }
5897 return NOTIFY_OK;
5898}
5899
5900/* Register at highest priority so that task migration (migrate_all_tasks)
5901 * happens before everything else.
5902 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07005903static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 .notifier_call = migration_call,
5905 .priority = 10
5906};
5907
Adrian Bunke6fe6642007-11-09 22:39:39 +01005908void __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909{
5910 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07005911 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005912
5913 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07005914 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
5915 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 migration_call(&migration_notifier, CPU_ONLINE, cpu);
5917 register_cpu_notifier(&migration_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918}
5919#endif
5920
5921#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07005922
5923/* Number of possible processor ids */
5924int nr_cpu_ids __read_mostly = NR_CPUS;
5925EXPORT_SYMBOL(nr_cpu_ids);
5926
Ingo Molnar3e9830d2007-10-15 17:00:13 +02005927#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02005928
5929static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level)
5930{
5931 struct sched_group *group = sd->groups;
5932 cpumask_t groupmask;
5933 char str[NR_CPUS];
5934
5935 cpumask_scnprintf(str, NR_CPUS, sd->span);
5936 cpus_clear(groupmask);
5937
5938 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
5939
5940 if (!(sd->flags & SD_LOAD_BALANCE)) {
5941 printk("does not load-balance\n");
5942 if (sd->parent)
5943 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
5944 " has parent");
5945 return -1;
5946 }
5947
5948 printk(KERN_CONT "span %s\n", str);
5949
5950 if (!cpu_isset(cpu, sd->span)) {
5951 printk(KERN_ERR "ERROR: domain->span does not contain "
5952 "CPU%d\n", cpu);
5953 }
5954 if (!cpu_isset(cpu, group->cpumask)) {
5955 printk(KERN_ERR "ERROR: domain->groups does not contain"
5956 " CPU%d\n", cpu);
5957 }
5958
5959 printk(KERN_DEBUG "%*s groups:", level + 1, "");
5960 do {
5961 if (!group) {
5962 printk("\n");
5963 printk(KERN_ERR "ERROR: group is NULL\n");
5964 break;
5965 }
5966
5967 if (!group->__cpu_power) {
5968 printk(KERN_CONT "\n");
5969 printk(KERN_ERR "ERROR: domain->cpu_power not "
5970 "set\n");
5971 break;
5972 }
5973
5974 if (!cpus_weight(group->cpumask)) {
5975 printk(KERN_CONT "\n");
5976 printk(KERN_ERR "ERROR: empty group\n");
5977 break;
5978 }
5979
5980 if (cpus_intersects(groupmask, group->cpumask)) {
5981 printk(KERN_CONT "\n");
5982 printk(KERN_ERR "ERROR: repeated CPUs\n");
5983 break;
5984 }
5985
5986 cpus_or(groupmask, groupmask, group->cpumask);
5987
5988 cpumask_scnprintf(str, NR_CPUS, group->cpumask);
5989 printk(KERN_CONT " %s", str);
5990
5991 group = group->next;
5992 } while (group != sd->groups);
5993 printk(KERN_CONT "\n");
5994
5995 if (!cpus_equal(sd->span, groupmask))
5996 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
5997
5998 if (sd->parent && !cpus_subset(groupmask, sd->parent->span))
5999 printk(KERN_ERR "ERROR: parent span is not a superset "
6000 "of domain->span\n");
6001 return 0;
6002}
6003
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004static void sched_domain_debug(struct sched_domain *sd, int cpu)
6005{
6006 int level = 0;
6007
Nick Piggin41c7ce92005-06-25 14:57:24 -07006008 if (!sd) {
6009 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6010 return;
6011 }
6012
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6014
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006015 for (;;) {
6016 if (sched_domain_debug_one(sd, cpu, level))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 level++;
6019 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006020 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006021 break;
6022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023}
6024#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07006025# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026#endif
6027
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006028static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006029{
6030 if (cpus_weight(sd->span) == 1)
6031 return 1;
6032
6033 /* Following flags need at least 2 groups */
6034 if (sd->flags & (SD_LOAD_BALANCE |
6035 SD_BALANCE_NEWIDLE |
6036 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006037 SD_BALANCE_EXEC |
6038 SD_SHARE_CPUPOWER |
6039 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006040 if (sd->groups != sd->groups->next)
6041 return 0;
6042 }
6043
6044 /* Following flags don't use groups */
6045 if (sd->flags & (SD_WAKE_IDLE |
6046 SD_WAKE_AFFINE |
6047 SD_WAKE_BALANCE))
6048 return 0;
6049
6050 return 1;
6051}
6052
Ingo Molnar48f24c42006-07-03 00:25:40 -07006053static int
6054sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006055{
6056 unsigned long cflags = sd->flags, pflags = parent->flags;
6057
6058 if (sd_degenerate(parent))
6059 return 1;
6060
6061 if (!cpus_equal(sd->span, parent->span))
6062 return 0;
6063
6064 /* Does parent contain flags not in child? */
6065 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6066 if (cflags & SD_WAKE_AFFINE)
6067 pflags &= ~SD_WAKE_BALANCE;
6068 /* Flags needing groups don't count if only 1 group in parent */
6069 if (parent->groups == parent->groups->next) {
6070 pflags &= ~(SD_LOAD_BALANCE |
6071 SD_BALANCE_NEWIDLE |
6072 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006073 SD_BALANCE_EXEC |
6074 SD_SHARE_CPUPOWER |
6075 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006076 }
6077 if (~cflags & pflags)
6078 return 0;
6079
6080 return 1;
6081}
6082
Gregory Haskins57d885f2008-01-25 21:08:18 +01006083static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6084{
6085 unsigned long flags;
6086 const struct sched_class *class;
6087
6088 spin_lock_irqsave(&rq->lock, flags);
6089
6090 if (rq->rd) {
6091 struct root_domain *old_rd = rq->rd;
6092
Ingo Molnar0eab9142008-01-25 21:08:19 +01006093 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006094 if (class->leave_domain)
6095 class->leave_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006096 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006097
Gregory Haskinsdc938522008-01-25 21:08:26 +01006098 cpu_clear(rq->cpu, old_rd->span);
6099 cpu_clear(rq->cpu, old_rd->online);
6100
Gregory Haskins57d885f2008-01-25 21:08:18 +01006101 if (atomic_dec_and_test(&old_rd->refcount))
6102 kfree(old_rd);
6103 }
6104
6105 atomic_inc(&rd->refcount);
6106 rq->rd = rd;
6107
Gregory Haskinsdc938522008-01-25 21:08:26 +01006108 cpu_set(rq->cpu, rd->span);
6109 if (cpu_isset(rq->cpu, cpu_online_map))
6110 cpu_set(rq->cpu, rd->online);
6111
Ingo Molnar0eab9142008-01-25 21:08:19 +01006112 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006113 if (class->join_domain)
6114 class->join_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006115 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006116
6117 spin_unlock_irqrestore(&rq->lock, flags);
6118}
6119
Gregory Haskinsdc938522008-01-25 21:08:26 +01006120static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006121{
6122 memset(rd, 0, sizeof(*rd));
6123
Gregory Haskinsdc938522008-01-25 21:08:26 +01006124 cpus_clear(rd->span);
6125 cpus_clear(rd->online);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006126}
6127
6128static void init_defrootdomain(void)
6129{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006130 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006131 atomic_set(&def_root_domain.refcount, 1);
6132}
6133
Gregory Haskinsdc938522008-01-25 21:08:26 +01006134static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006135{
6136 struct root_domain *rd;
6137
6138 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6139 if (!rd)
6140 return NULL;
6141
Gregory Haskinsdc938522008-01-25 21:08:26 +01006142 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006143
6144 return rd;
6145}
6146
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006148 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 * hold the hotplug lock.
6150 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006151static void
6152cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006154 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006155 struct sched_domain *tmp;
6156
6157 /* Remove the sched domains which do not contribute to scheduling. */
6158 for (tmp = sd; tmp; tmp = tmp->parent) {
6159 struct sched_domain *parent = tmp->parent;
6160 if (!parent)
6161 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006162 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006163 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006164 if (parent->parent)
6165 parent->parent->child = tmp;
6166 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07006167 }
6168
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006169 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006170 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006171 if (sd)
6172 sd->child = NULL;
6173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174
6175 sched_domain_debug(sd, cpu);
6176
Gregory Haskins57d885f2008-01-25 21:08:18 +01006177 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006178 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179}
6180
6181/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006182static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183
6184/* Setup the mask of cpus configured for isolated domains */
6185static int __init isolated_cpu_setup(char *str)
6186{
6187 int ints[NR_CPUS], i;
6188
6189 str = get_options(str, ARRAY_SIZE(ints), ints);
6190 cpus_clear(cpu_isolated_map);
6191 for (i = 1; i <= ints[0]; i++)
6192 if (ints[i] < NR_CPUS)
6193 cpu_set(ints[i], cpu_isolated_map);
6194 return 1;
6195}
6196
Ingo Molnar8927f492007-10-15 17:00:13 +02006197__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198
6199/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006200 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6201 * to a function which identifies what group(along with sched group) a CPU
6202 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6203 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 *
6205 * init_sched_build_groups will build a circular linked list of the groups
6206 * covered by the given span, and will set each group's ->cpumask correctly,
6207 * and ->cpu_power to 0.
6208 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006209static void
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006210init_sched_build_groups(cpumask_t span, const cpumask_t *cpu_map,
6211 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
6212 struct sched_group **sg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213{
6214 struct sched_group *first = NULL, *last = NULL;
6215 cpumask_t covered = CPU_MASK_NONE;
6216 int i;
6217
6218 for_each_cpu_mask(i, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006219 struct sched_group *sg;
6220 int group = group_fn(i, cpu_map, &sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221 int j;
6222
6223 if (cpu_isset(i, covered))
6224 continue;
6225
6226 sg->cpumask = CPU_MASK_NONE;
Eric Dumazet5517d862007-05-08 00:32:57 -07006227 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228
6229 for_each_cpu_mask(j, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006230 if (group_fn(j, cpu_map, NULL) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 continue;
6232
6233 cpu_set(j, covered);
6234 cpu_set(j, sg->cpumask);
6235 }
6236 if (!first)
6237 first = sg;
6238 if (last)
6239 last->next = sg;
6240 last = sg;
6241 }
6242 last->next = first;
6243}
6244
John Hawkes9c1cfda2005-09-06 15:18:14 -07006245#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246
John Hawkes9c1cfda2005-09-06 15:18:14 -07006247#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006248
John Hawkes9c1cfda2005-09-06 15:18:14 -07006249/**
6250 * find_next_best_node - find the next node to include in a sched_domain
6251 * @node: node whose sched_domain we're building
6252 * @used_nodes: nodes already in the sched_domain
6253 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006254 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006255 * finds the closest node not already in the @used_nodes map.
6256 *
6257 * Should use nodemask_t.
6258 */
6259static int find_next_best_node(int node, unsigned long *used_nodes)
6260{
6261 int i, n, val, min_val, best_node = 0;
6262
6263 min_val = INT_MAX;
6264
6265 for (i = 0; i < MAX_NUMNODES; i++) {
6266 /* Start at @node */
6267 n = (node + i) % MAX_NUMNODES;
6268
6269 if (!nr_cpus_node(n))
6270 continue;
6271
6272 /* Skip already used nodes */
6273 if (test_bit(n, used_nodes))
6274 continue;
6275
6276 /* Simple min distance search */
6277 val = node_distance(node, n);
6278
6279 if (val < min_val) {
6280 min_val = val;
6281 best_node = n;
6282 }
6283 }
6284
6285 set_bit(best_node, used_nodes);
6286 return best_node;
6287}
6288
6289/**
6290 * sched_domain_node_span - get a cpumask for a node's sched_domain
6291 * @node: node whose cpumask we're constructing
6292 * @size: number of nodes to include in this span
6293 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006294 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07006295 * should be one that prevents unnecessary balancing, but also spreads tasks
6296 * out optimally.
6297 */
6298static cpumask_t sched_domain_node_span(int node)
6299{
John Hawkes9c1cfda2005-09-06 15:18:14 -07006300 DECLARE_BITMAP(used_nodes, MAX_NUMNODES);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006301 cpumask_t span, nodemask;
6302 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006303
6304 cpus_clear(span);
6305 bitmap_zero(used_nodes, MAX_NUMNODES);
6306
6307 nodemask = node_to_cpumask(node);
6308 cpus_or(span, span, nodemask);
6309 set_bit(node, used_nodes);
6310
6311 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
6312 int next_node = find_next_best_node(node, used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006313
John Hawkes9c1cfda2005-09-06 15:18:14 -07006314 nodemask = node_to_cpumask(next_node);
6315 cpus_or(span, span, nodemask);
6316 }
6317
6318 return span;
6319}
6320#endif
6321
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006322int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006323
John Hawkes9c1cfda2005-09-06 15:18:14 -07006324/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07006325 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07006326 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327#ifdef CONFIG_SCHED_SMT
6328static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006329static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006330
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006331static int
6332cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006334 if (sg)
6335 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336 return cpu;
6337}
6338#endif
6339
Ingo Molnar48f24c42006-07-03 00:25:40 -07006340/*
6341 * multi-core sched-domains:
6342 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006343#ifdef CONFIG_SCHED_MC
6344static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006345static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006346#endif
6347
6348#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006349static int
6350cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006351{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006352 int group;
Mike Travisd5a74302007-10-16 01:24:05 -07006353 cpumask_t mask = per_cpu(cpu_sibling_map, cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006354 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006355 group = first_cpu(mask);
6356 if (sg)
6357 *sg = &per_cpu(sched_group_core, group);
6358 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006359}
6360#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006361static int
6362cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006363{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006364 if (sg)
6365 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006366 return cpu;
6367}
6368#endif
6369
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006371static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006372
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006373static int
6374cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006376 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006377#ifdef CONFIG_SCHED_MC
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006378 cpumask_t mask = cpu_coregroup_map(cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006379 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006380 group = first_cpu(mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006381#elif defined(CONFIG_SCHED_SMT)
Mike Travisd5a74302007-10-16 01:24:05 -07006382 cpumask_t mask = per_cpu(cpu_sibling_map, cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006383 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006384 group = first_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006386 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006388 if (sg)
6389 *sg = &per_cpu(sched_group_phys, group);
6390 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391}
6392
6393#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07006394/*
6395 * The init_sched_build_groups can't handle what we want to do with node
6396 * groups, so roll our own. Now each node has its own list of groups which
6397 * gets dynamically allocated.
6398 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399static DEFINE_PER_CPU(struct sched_domain, node_domains);
John Hawkesd1b55132005-09-06 15:18:14 -07006400static struct sched_group **sched_group_nodes_bycpu[NR_CPUS];
John Hawkes9c1cfda2005-09-06 15:18:14 -07006401
6402static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006403static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006404
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006405static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
6406 struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006408 cpumask_t nodemask = node_to_cpumask(cpu_to_node(cpu));
6409 int group;
6410
6411 cpus_and(nodemask, nodemask, *cpu_map);
6412 group = first_cpu(nodemask);
6413
6414 if (sg)
6415 *sg = &per_cpu(sched_group_allnodes, group);
6416 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006418
Siddha, Suresh B08069032006-03-27 01:15:23 -08006419static void init_numa_sched_groups_power(struct sched_group *group_head)
6420{
6421 struct sched_group *sg = group_head;
6422 int j;
6423
6424 if (!sg)
6425 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006426 do {
6427 for_each_cpu_mask(j, sg->cpumask) {
6428 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08006429
Andi Kleen3a5c3592007-10-15 17:00:14 +02006430 sd = &per_cpu(phys_domains, j);
6431 if (j != first_cpu(sd->groups->cpumask)) {
6432 /*
6433 * Only add "power" once for each
6434 * physical package.
6435 */
6436 continue;
6437 }
6438
6439 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006440 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006441 sg = sg->next;
6442 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006443}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444#endif
6445
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006446#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006447/* Free memory allocated for various sched_group structures */
6448static void free_sched_groups(const cpumask_t *cpu_map)
6449{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006450 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006451
6452 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006453 struct sched_group **sched_group_nodes
6454 = sched_group_nodes_bycpu[cpu];
6455
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006456 if (!sched_group_nodes)
6457 continue;
6458
6459 for (i = 0; i < MAX_NUMNODES; i++) {
6460 cpumask_t nodemask = node_to_cpumask(i);
6461 struct sched_group *oldsg, *sg = sched_group_nodes[i];
6462
6463 cpus_and(nodemask, nodemask, *cpu_map);
6464 if (cpus_empty(nodemask))
6465 continue;
6466
6467 if (sg == NULL)
6468 continue;
6469 sg = sg->next;
6470next_sg:
6471 oldsg = sg;
6472 sg = sg->next;
6473 kfree(oldsg);
6474 if (oldsg != sched_group_nodes[i])
6475 goto next_sg;
6476 }
6477 kfree(sched_group_nodes);
6478 sched_group_nodes_bycpu[cpu] = NULL;
6479 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006480}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006481#else
6482static void free_sched_groups(const cpumask_t *cpu_map)
6483{
6484}
6485#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006486
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006488 * Initialize sched groups cpu_power.
6489 *
6490 * cpu_power indicates the capacity of sched group, which is used while
6491 * distributing the load between different sched groups in a sched domain.
6492 * Typically cpu_power for all the groups in a sched domain will be same unless
6493 * there are asymmetries in the topology. If there are asymmetries, group
6494 * having more cpu_power will pickup more load compared to the group having
6495 * less cpu_power.
6496 *
6497 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
6498 * the maximum number of tasks a group can handle in the presence of other idle
6499 * or lightly loaded groups in the same sched domain.
6500 */
6501static void init_sched_groups_power(int cpu, struct sched_domain *sd)
6502{
6503 struct sched_domain *child;
6504 struct sched_group *group;
6505
6506 WARN_ON(!sd || !sd->groups);
6507
6508 if (cpu != first_cpu(sd->groups->cpumask))
6509 return;
6510
6511 child = sd->child;
6512
Eric Dumazet5517d862007-05-08 00:32:57 -07006513 sd->groups->__cpu_power = 0;
6514
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006515 /*
6516 * For perf policy, if the groups in child domain share resources
6517 * (for example cores sharing some portions of the cache hierarchy
6518 * or SMT), then set this domain groups cpu_power such that each group
6519 * can handle only one task, when there are other idle groups in the
6520 * same sched domain.
6521 */
6522 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
6523 (child->flags &
6524 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07006525 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006526 return;
6527 }
6528
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006529 /*
6530 * add cpu_power of each child group to this groups cpu_power
6531 */
6532 group = child->groups;
6533 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07006534 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006535 group = group->next;
6536 } while (group != child->groups);
6537}
6538
6539/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006540 * Build sched domains for a given set of cpus and attach the sched domains
6541 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006543static int build_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544{
6545 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006546 struct root_domain *rd;
John Hawkesd1b55132005-09-06 15:18:14 -07006547#ifdef CONFIG_NUMA
6548 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006549 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07006550
6551 /*
6552 * Allocate the per-node list of sched groups
6553 */
Milton Miller5cf9f062007-10-15 17:00:19 +02006554 sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006555 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07006556 if (!sched_group_nodes) {
6557 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006558 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07006559 }
6560 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
6561#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562
Gregory Haskinsdc938522008-01-25 21:08:26 +01006563 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01006564 if (!rd) {
6565 printk(KERN_WARNING "Cannot alloc root domain\n");
6566 return -ENOMEM;
6567 }
6568
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006570 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006572 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 struct sched_domain *sd = NULL, *p;
6574 cpumask_t nodemask = node_to_cpumask(cpu_to_node(i));
6575
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006576 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577
6578#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02006579 if (cpus_weight(*cpu_map) >
6580 SD_NODES_PER_DOMAIN*cpus_weight(nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07006581 sd = &per_cpu(allnodes_domains, i);
6582 *sd = SD_ALLNODES_INIT;
6583 sd->span = *cpu_map;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006584 cpu_to_allnodes_group(i, cpu_map, &sd->groups);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006585 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006586 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006587 } else
6588 p = NULL;
6589
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590 sd = &per_cpu(node_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591 *sd = SD_NODE_INIT;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006592 sd->span = sched_domain_node_span(cpu_to_node(i));
6593 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006594 if (p)
6595 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006596 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597#endif
6598
6599 p = sd;
6600 sd = &per_cpu(phys_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601 *sd = SD_CPU_INIT;
6602 sd->span = nodemask;
6603 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006604 if (p)
6605 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006606 cpu_to_phys_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006608#ifdef CONFIG_SCHED_MC
6609 p = sd;
6610 sd = &per_cpu(core_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006611 *sd = SD_MC_INIT;
6612 sd->span = cpu_coregroup_map(i);
6613 cpus_and(sd->span, sd->span, *cpu_map);
6614 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006615 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006616 cpu_to_core_group(i, cpu_map, &sd->groups);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006617#endif
6618
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619#ifdef CONFIG_SCHED_SMT
6620 p = sd;
6621 sd = &per_cpu(cpu_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622 *sd = SD_SIBLING_INIT;
Mike Travisd5a74302007-10-16 01:24:05 -07006623 sd->span = per_cpu(cpu_sibling_map, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006624 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006626 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006627 cpu_to_cpu_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628#endif
6629 }
6630
6631#ifdef CONFIG_SCHED_SMT
6632 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07006633 for_each_cpu_mask(i, *cpu_map) {
Mike Travisd5a74302007-10-16 01:24:05 -07006634 cpumask_t this_sibling_map = per_cpu(cpu_sibling_map, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006635 cpus_and(this_sibling_map, this_sibling_map, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 if (i != first_cpu(this_sibling_map))
6637 continue;
6638
Ingo Molnardd41f592007-07-09 18:51:59 +02006639 init_sched_build_groups(this_sibling_map, cpu_map,
6640 &cpu_to_cpu_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641 }
6642#endif
6643
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006644#ifdef CONFIG_SCHED_MC
6645 /* Set up multi-core groups */
6646 for_each_cpu_mask(i, *cpu_map) {
6647 cpumask_t this_core_map = cpu_coregroup_map(i);
6648 cpus_and(this_core_map, this_core_map, *cpu_map);
6649 if (i != first_cpu(this_core_map))
6650 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02006651 init_sched_build_groups(this_core_map, cpu_map,
6652 &cpu_to_core_group);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006653 }
6654#endif
6655
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656 /* Set up physical groups */
6657 for (i = 0; i < MAX_NUMNODES; i++) {
6658 cpumask_t nodemask = node_to_cpumask(i);
6659
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006660 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006661 if (cpus_empty(nodemask))
6662 continue;
6663
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006664 init_sched_build_groups(nodemask, cpu_map, &cpu_to_phys_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665 }
6666
6667#ifdef CONFIG_NUMA
6668 /* Set up node groups */
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006669 if (sd_allnodes)
Ingo Molnardd41f592007-07-09 18:51:59 +02006670 init_sched_build_groups(*cpu_map, cpu_map,
6671 &cpu_to_allnodes_group);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006672
6673 for (i = 0; i < MAX_NUMNODES; i++) {
6674 /* Set up node groups */
6675 struct sched_group *sg, *prev;
6676 cpumask_t nodemask = node_to_cpumask(i);
6677 cpumask_t domainspan;
6678 cpumask_t covered = CPU_MASK_NONE;
6679 int j;
6680
6681 cpus_and(nodemask, nodemask, *cpu_map);
John Hawkesd1b55132005-09-06 15:18:14 -07006682 if (cpus_empty(nodemask)) {
6683 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006684 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07006685 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006686
6687 domainspan = sched_domain_node_span(i);
6688 cpus_and(domainspan, domainspan, *cpu_map);
6689
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006690 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006691 if (!sg) {
6692 printk(KERN_WARNING "Can not alloc domain group for "
6693 "node %d\n", i);
6694 goto error;
6695 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006696 sched_group_nodes[i] = sg;
6697 for_each_cpu_mask(j, nodemask) {
6698 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02006699
John Hawkes9c1cfda2005-09-06 15:18:14 -07006700 sd = &per_cpu(node_domains, j);
6701 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006702 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006703 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006704 sg->cpumask = nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006705 sg->next = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006706 cpus_or(covered, covered, nodemask);
6707 prev = sg;
6708
6709 for (j = 0; j < MAX_NUMNODES; j++) {
6710 cpumask_t tmp, notcovered;
6711 int n = (i + j) % MAX_NUMNODES;
6712
6713 cpus_complement(notcovered, covered);
6714 cpus_and(tmp, notcovered, *cpu_map);
6715 cpus_and(tmp, tmp, domainspan);
6716 if (cpus_empty(tmp))
6717 break;
6718
6719 nodemask = node_to_cpumask(n);
6720 cpus_and(tmp, tmp, nodemask);
6721 if (cpus_empty(tmp))
6722 continue;
6723
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006724 sg = kmalloc_node(sizeof(struct sched_group),
6725 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006726 if (!sg) {
6727 printk(KERN_WARNING
6728 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006729 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006730 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006731 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006732 sg->cpumask = tmp;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006733 sg->next = prev->next;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006734 cpus_or(covered, covered, tmp);
6735 prev->next = sg;
6736 prev = sg;
6737 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739#endif
6740
6741 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006742#ifdef CONFIG_SCHED_SMT
6743 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006744 struct sched_domain *sd = &per_cpu(cpu_domains, i);
6745
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006746 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006747 }
6748#endif
6749#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006750 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006751 struct sched_domain *sd = &per_cpu(core_domains, i);
6752
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006753 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006754 }
6755#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006757 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006758 struct sched_domain *sd = &per_cpu(phys_domains, i);
6759
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006760 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761 }
6762
John Hawkes9c1cfda2005-09-06 15:18:14 -07006763#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08006764 for (i = 0; i < MAX_NUMNODES; i++)
6765 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006766
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006767 if (sd_allnodes) {
6768 struct sched_group *sg;
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07006769
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006770 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg);
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07006771 init_numa_sched_groups_power(sg);
6772 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006773#endif
6774
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006776 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777 struct sched_domain *sd;
6778#ifdef CONFIG_SCHED_SMT
6779 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006780#elif defined(CONFIG_SCHED_MC)
6781 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782#else
6783 sd = &per_cpu(phys_domains, i);
6784#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01006785 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006787
6788 return 0;
6789
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006790#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006791error:
6792 free_sched_groups(cpu_map);
6793 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006794#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795}
Paul Jackson029190c2007-10-18 23:40:20 -07006796
6797static cpumask_t *doms_cur; /* current sched domains */
6798static int ndoms_cur; /* number of sched domains in 'doms_cur' */
6799
6800/*
6801 * Special case: If a kmalloc of a doms_cur partition (array of
6802 * cpumask_t) fails, then fallback to a single sched domain,
6803 * as determined by the single cpumask_t fallback_doms.
6804 */
6805static cpumask_t fallback_doms;
6806
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006807/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006808 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07006809 * For now this just excludes isolated cpus, but could be used to
6810 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006811 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006812static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006813{
Milton Miller73785472007-10-24 18:23:48 +02006814 int err;
6815
Paul Jackson029190c2007-10-18 23:40:20 -07006816 ndoms_cur = 1;
6817 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6818 if (!doms_cur)
6819 doms_cur = &fallback_doms;
6820 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Milton Miller73785472007-10-24 18:23:48 +02006821 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02006822 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02006823
6824 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006825}
6826
6827static void arch_destroy_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828{
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006829 free_sched_groups(cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006830}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006832/*
6833 * Detach sched domains from a group of cpus specified in cpu_map
6834 * These cpus will now be attached to the NULL domain
6835 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08006836static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006837{
6838 int i;
6839
Milton Miller6382bc92007-10-15 17:00:19 +02006840 unregister_sched_domain_sysctl();
6841
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006842 for_each_cpu_mask(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006843 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006844 synchronize_sched();
6845 arch_destroy_sched_domains(cpu_map);
6846}
6847
Paul Jackson029190c2007-10-18 23:40:20 -07006848/*
6849 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006850 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07006851 * doms_new[] to the current sched domain partitioning, doms_cur[].
6852 * It destroys each deleted domain and builds each new domain.
6853 *
6854 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006855 * The masks don't intersect (don't overlap.) We should setup one
6856 * sched domain for each mask. CPUs not in any of the cpumasks will
6857 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07006858 * current 'doms_cur' domains and in the new 'doms_new', we can leave
6859 * it as it is.
6860 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006861 * The passed in 'doms_new' should be kmalloc'd. This routine takes
6862 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07006863 * failed the kmalloc call, then it can pass in doms_new == NULL,
6864 * and partition_sched_domains() will fallback to the single partition
6865 * 'fallback_doms'.
6866 *
6867 * Call with hotplug lock held
6868 */
6869void partition_sched_domains(int ndoms_new, cpumask_t *doms_new)
6870{
6871 int i, j;
6872
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01006873 lock_doms_cur();
6874
Milton Miller73785472007-10-24 18:23:48 +02006875 /* always unregister in case we don't destroy any domains */
6876 unregister_sched_domain_sysctl();
6877
Paul Jackson029190c2007-10-18 23:40:20 -07006878 if (doms_new == NULL) {
6879 ndoms_new = 1;
6880 doms_new = &fallback_doms;
6881 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
6882 }
6883
6884 /* Destroy deleted domains */
6885 for (i = 0; i < ndoms_cur; i++) {
6886 for (j = 0; j < ndoms_new; j++) {
6887 if (cpus_equal(doms_cur[i], doms_new[j]))
6888 goto match1;
6889 }
6890 /* no match - a current sched domain not in new doms_new[] */
6891 detach_destroy_domains(doms_cur + i);
6892match1:
6893 ;
6894 }
6895
6896 /* Build new domains */
6897 for (i = 0; i < ndoms_new; i++) {
6898 for (j = 0; j < ndoms_cur; j++) {
6899 if (cpus_equal(doms_new[i], doms_cur[j]))
6900 goto match2;
6901 }
6902 /* no match - add a new doms_new */
6903 build_sched_domains(doms_new + i);
6904match2:
6905 ;
6906 }
6907
6908 /* Remember the new sched domains */
6909 if (doms_cur != &fallback_doms)
6910 kfree(doms_cur);
6911 doms_cur = doms_new;
6912 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02006913
6914 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01006915
6916 unlock_doms_cur();
Paul Jackson029190c2007-10-18 23:40:20 -07006917}
6918
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006919#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Adrian Bunk6707de002007-08-12 18:08:19 +02006920static int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006921{
6922 int err;
6923
Gautham R Shenoy95402b32008-01-25 21:08:02 +01006924 get_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006925 detach_destroy_domains(&cpu_online_map);
6926 err = arch_init_sched_domains(&cpu_online_map);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01006927 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006928
6929 return err;
6930}
6931
6932static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
6933{
6934 int ret;
6935
6936 if (buf[0] != '0' && buf[0] != '1')
6937 return -EINVAL;
6938
6939 if (smt)
6940 sched_smt_power_savings = (buf[0] == '1');
6941 else
6942 sched_mc_power_savings = (buf[0] == '1');
6943
6944 ret = arch_reinit_sched_domains();
6945
6946 return ret ? ret : count;
6947}
6948
Adrian Bunk6707de002007-08-12 18:08:19 +02006949#ifdef CONFIG_SCHED_MC
6950static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
6951{
6952 return sprintf(page, "%u\n", sched_mc_power_savings);
6953}
6954static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
6955 const char *buf, size_t count)
6956{
6957 return sched_power_savings_store(buf, count, 0);
6958}
6959static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
6960 sched_mc_power_savings_store);
6961#endif
6962
6963#ifdef CONFIG_SCHED_SMT
6964static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
6965{
6966 return sprintf(page, "%u\n", sched_smt_power_savings);
6967}
6968static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
6969 const char *buf, size_t count)
6970{
6971 return sched_power_savings_store(buf, count, 1);
6972}
6973static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
6974 sched_smt_power_savings_store);
6975#endif
6976
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006977int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
6978{
6979 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006980
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006981#ifdef CONFIG_SCHED_SMT
6982 if (smt_capable())
6983 err = sysfs_create_file(&cls->kset.kobj,
6984 &attr_sched_smt_power_savings.attr);
6985#endif
6986#ifdef CONFIG_SCHED_MC
6987 if (!err && mc_capable())
6988 err = sysfs_create_file(&cls->kset.kobj,
6989 &attr_sched_mc_power_savings.attr);
6990#endif
6991 return err;
6992}
6993#endif
6994
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006996 * Force a reinitialization of the sched domains hierarchy. The domains
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07006998 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999 * which will prevent rebalancing while the sched domains are recalculated.
7000 */
7001static int update_sched_domains(struct notifier_block *nfb,
7002 unsigned long action, void *hcpu)
7003{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004 switch (action) {
7005 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007006 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007008 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007009 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010 return NOTIFY_OK;
7011
7012 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007013 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007015 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007017 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007019 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007020 /*
7021 * Fall through and re-initialise the domains.
7022 */
7023 break;
7024 default:
7025 return NOTIFY_DONE;
7026 }
7027
7028 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007029 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030
7031 return NOTIFY_OK;
7032}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033
7034void __init sched_init_smp(void)
7035{
Nick Piggin5c1e1762006-10-03 01:14:04 -07007036 cpumask_t non_isolated_cpus;
7037
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007038 get_online_cpus();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007039 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08007040 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007041 if (cpus_empty(non_isolated_cpus))
7042 cpu_set(smp_processor_id(), non_isolated_cpus);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007043 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044 /* XXX: Theoretical race here - CPU may be hotplugged now */
7045 hotcpu_notifier(update_sched_domains, 0);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007046
7047 /* Move init over to a non-isolated CPU */
7048 if (set_cpus_allowed(current, non_isolated_cpus) < 0)
7049 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007050 sched_init_granularity();
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007051
7052#ifdef CONFIG_FAIR_GROUP_SCHED
7053 if (nr_cpu_ids == 1)
7054 return;
7055
7056 lb_monitor_task = kthread_create(load_balance_monitor, NULL,
7057 "group_balance");
7058 if (!IS_ERR(lb_monitor_task)) {
7059 lb_monitor_task->flags |= PF_NOFREEZE;
7060 wake_up_process(lb_monitor_task);
7061 } else {
7062 printk(KERN_ERR "Could not create load balance monitor thread"
7063 "(error = %ld) \n", PTR_ERR(lb_monitor_task));
7064 }
7065#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066}
7067#else
7068void __init sched_init_smp(void)
7069{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007070 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071}
7072#endif /* CONFIG_SMP */
7073
7074int in_sched_functions(unsigned long addr)
7075{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076 return in_lock_functions(addr) ||
7077 (addr >= (unsigned long)__sched_text_start
7078 && addr < (unsigned long)__sched_text_end);
7079}
7080
Alexey Dobriyana9957442007-10-15 17:00:13 +02007081static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007082{
7083 cfs_rq->tasks_timeline = RB_ROOT;
Ingo Molnardd41f592007-07-09 18:51:59 +02007084#ifdef CONFIG_FAIR_GROUP_SCHED
7085 cfs_rq->rq = rq;
7086#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007087 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02007088}
7089
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007090static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7091{
7092 struct rt_prio_array *array;
7093 int i;
7094
7095 array = &rt_rq->active;
7096 for (i = 0; i < MAX_RT_PRIO; i++) {
7097 INIT_LIST_HEAD(array->queue + i);
7098 __clear_bit(i, array->bitmap);
7099 }
7100 /* delimiter for bitsearch: */
7101 __set_bit(MAX_RT_PRIO, array->bitmap);
7102
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007103#if defined CONFIG_SMP || defined CONFIG_FAIR_GROUP_SCHED
7104 rt_rq->highest_prio = MAX_RT_PRIO;
7105#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007106#ifdef CONFIG_SMP
7107 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007108 rt_rq->overloaded = 0;
7109#endif
7110
7111 rt_rq->rt_time = 0;
7112 rt_rq->rt_throttled = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007113
7114#ifdef CONFIG_FAIR_GROUP_SCHED
7115 rt_rq->rq = rq;
7116#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007117}
7118
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007119#ifdef CONFIG_FAIR_GROUP_SCHED
7120static void init_tg_cfs_entry(struct rq *rq, struct task_group *tg,
7121 struct cfs_rq *cfs_rq, struct sched_entity *se,
7122 int cpu, int add)
7123{
7124 tg->cfs_rq[cpu] = cfs_rq;
7125 init_cfs_rq(cfs_rq, rq);
7126 cfs_rq->tg = tg;
7127 if (add)
7128 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7129
7130 tg->se[cpu] = se;
7131 se->cfs_rq = &rq->cfs;
7132 se->my_q = cfs_rq;
7133 se->load.weight = tg->shares;
7134 se->load.inv_weight = div64_64(1ULL<<32, se->load.weight);
7135 se->parent = NULL;
7136}
7137
7138static void init_tg_rt_entry(struct rq *rq, struct task_group *tg,
7139 struct rt_rq *rt_rq, struct sched_rt_entity *rt_se,
7140 int cpu, int add)
7141{
7142 tg->rt_rq[cpu] = rt_rq;
7143 init_rt_rq(rt_rq, rq);
7144 rt_rq->tg = tg;
7145 rt_rq->rt_se = rt_se;
7146 if (add)
7147 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
7148
7149 tg->rt_se[cpu] = rt_se;
7150 rt_se->rt_rq = &rq->rt;
7151 rt_se->my_q = rt_rq;
7152 rt_se->parent = NULL;
7153 INIT_LIST_HEAD(&rt_se->run_list);
7154}
7155#endif
7156
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157void __init sched_init(void)
7158{
Christoph Lameter476f3532007-05-06 14:48:58 -07007159 int highest_cpu = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007160 int i, j;
7161
Gregory Haskins57d885f2008-01-25 21:08:18 +01007162#ifdef CONFIG_SMP
7163 init_defrootdomain();
7164#endif
7165
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007166#ifdef CONFIG_FAIR_GROUP_SCHED
7167 list_add(&init_task_group.list, &task_groups);
7168#endif
7169
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08007170 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07007171 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172
7173 rq = cpu_rq(i);
7174 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07007175 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07007176 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007177 rq->clock = 1;
7178 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007179 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007180#ifdef CONFIG_FAIR_GROUP_SCHED
7181 init_task_group.shares = init_task_group_load;
7182 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
7183 init_tg_cfs_entry(rq, &init_task_group,
7184 &per_cpu(init_cfs_rq, i),
7185 &per_cpu(init_sched_entity, i), i, 1);
7186
7187 init_task_group.rt_ratio = sysctl_sched_rt_ratio; /* XXX */
7188 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
7189 init_tg_rt_entry(rq, &init_task_group,
7190 &per_cpu(init_rt_rq, i),
7191 &per_cpu(init_sched_rt_entity, i), i, 1);
7192#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007193 rq->rt_period_expire = 0;
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007194 rq->rt_throttled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195
Ingo Molnardd41f592007-07-09 18:51:59 +02007196 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
7197 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07007199 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007200 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007202 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07007204 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007205 rq->migration_thread = NULL;
7206 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01007207 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007209 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210 atomic_set(&rq->nr_iowait, 0);
Christoph Lameter476f3532007-05-06 14:48:58 -07007211 highest_cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212 }
7213
Peter Williams2dd73a42006-06-27 02:54:34 -07007214 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007215
Avi Kivitye107be32007-07-26 13:40:43 +02007216#ifdef CONFIG_PREEMPT_NOTIFIERS
7217 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
7218#endif
7219
Christoph Lameterc9819f42006-12-10 02:20:25 -08007220#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07007221 nr_cpu_ids = highest_cpu + 1;
Christoph Lameterc9819f42006-12-10 02:20:25 -08007222 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
7223#endif
7224
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007225#ifdef CONFIG_RT_MUTEXES
7226 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
7227#endif
7228
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229 /*
7230 * The boot idle thread does lazy MMU switching as well:
7231 */
7232 atomic_inc(&init_mm.mm_count);
7233 enter_lazy_tlb(&init_mm, current);
7234
7235 /*
7236 * Make us the idle thread. Technically, schedule() should not be
7237 * called from this thread, however somewhere below it might be,
7238 * but because we are the idle thread, we just pick up running again
7239 * when this runqueue becomes "idle".
7240 */
7241 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02007242 /*
7243 * During early bootup we pretend to be a normal task:
7244 */
7245 current->sched_class = &fair_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246}
7247
7248#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
7249void __might_sleep(char *file, int line)
7250{
Ingo Molnar48f24c42006-07-03 00:25:40 -07007251#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252 static unsigned long prev_jiffy; /* ratelimiting */
7253
7254 if ((in_atomic() || irqs_disabled()) &&
7255 system_state == SYSTEM_RUNNING && !oops_in_progress) {
7256 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
7257 return;
7258 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08007259 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260 " context at %s:%d\n", file, line);
7261 printk("in_atomic():%d, irqs_disabled():%d\n",
7262 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08007263 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08007264 if (irqs_disabled())
7265 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007266 dump_stack();
7267 }
7268#endif
7269}
7270EXPORT_SYMBOL(__might_sleep);
7271#endif
7272
7273#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007274static void normalize_task(struct rq *rq, struct task_struct *p)
7275{
7276 int on_rq;
7277 update_rq_clock(rq);
7278 on_rq = p->se.on_rq;
7279 if (on_rq)
7280 deactivate_task(rq, p, 0);
7281 __setscheduler(rq, p, SCHED_NORMAL, 0);
7282 if (on_rq) {
7283 activate_task(rq, p, 0);
7284 resched_task(rq->curr);
7285 }
7286}
7287
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288void normalize_rt_tasks(void)
7289{
Ingo Molnara0f98a12007-06-17 18:37:45 +02007290 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07007292 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007293
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007294 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007295 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02007296 /*
7297 * Only normalize user tasks:
7298 */
7299 if (!p->mm)
7300 continue;
7301
Ingo Molnardd41f592007-07-09 18:51:59 +02007302 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007303#ifdef CONFIG_SCHEDSTATS
7304 p->se.wait_start = 0;
7305 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007306 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007307#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02007308 task_rq(p)->clock = 0;
7309
7310 if (!rt_task(p)) {
7311 /*
7312 * Renice negative nice level userspace
7313 * tasks back to 0:
7314 */
7315 if (TASK_NICE(p) < 0 && p->mm)
7316 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007317 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02007318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007320 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07007321 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007322
Ingo Molnar178be792007-10-15 17:00:18 +02007323 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007324
Ingo Molnarb29739f2006-06-27 02:54:51 -07007325 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007326 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007327 } while_each_thread(g, p);
7328
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007329 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007330}
7331
7332#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07007333
7334#ifdef CONFIG_IA64
7335/*
7336 * These functions are only useful for the IA64 MCA handling.
7337 *
7338 * They can only be called when the whole system has been
7339 * stopped - every CPU needs to be quiescent, and no scheduling
7340 * activity can take place. Using them for anything else would
7341 * be a serious bug, and as a result, they aren't even visible
7342 * under any other configuration.
7343 */
7344
7345/**
7346 * curr_task - return the current task for a given cpu.
7347 * @cpu: the processor in question.
7348 *
7349 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7350 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007351struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007352{
7353 return cpu_curr(cpu);
7354}
7355
7356/**
7357 * set_curr_task - set the current task for a given cpu.
7358 * @cpu: the processor in question.
7359 * @p: the task pointer to set.
7360 *
7361 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007362 * are serviced on a separate stack. It allows the architecture to switch the
7363 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07007364 * must be called with all CPU's synchronized, and interrupts disabled, the
7365 * and caller must save the original value of the current task (see
7366 * curr_task() above) and restore that value before reenabling interrupts and
7367 * re-starting the system.
7368 *
7369 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7370 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007371void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007372{
7373 cpu_curr(cpu) = p;
7374}
7375
7376#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007377
7378#ifdef CONFIG_FAIR_GROUP_SCHED
7379
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007380#ifdef CONFIG_SMP
7381/*
7382 * distribute shares of all task groups among their schedulable entities,
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007383 * to reflect load distribution across cpus.
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007384 */
7385static int rebalance_shares(struct sched_domain *sd, int this_cpu)
7386{
7387 struct cfs_rq *cfs_rq;
7388 struct rq *rq = cpu_rq(this_cpu);
7389 cpumask_t sdspan = sd->span;
7390 int balanced = 1;
7391
7392 /* Walk thr' all the task groups that we have */
7393 for_each_leaf_cfs_rq(rq, cfs_rq) {
7394 int i;
7395 unsigned long total_load = 0, total_shares;
7396 struct task_group *tg = cfs_rq->tg;
7397
7398 /* Gather total task load of this group across cpus */
7399 for_each_cpu_mask(i, sdspan)
7400 total_load += tg->cfs_rq[i]->load.weight;
7401
Ingo Molnar0eab9142008-01-25 21:08:19 +01007402 /* Nothing to do if this group has no load */
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007403 if (!total_load)
7404 continue;
7405
7406 /*
7407 * tg->shares represents the number of cpu shares the task group
7408 * is eligible to hold on a single cpu. On N cpus, it is
7409 * eligible to hold (N * tg->shares) number of cpu shares.
7410 */
7411 total_shares = tg->shares * cpus_weight(sdspan);
7412
7413 /*
7414 * redistribute total_shares across cpus as per the task load
7415 * distribution.
7416 */
7417 for_each_cpu_mask(i, sdspan) {
7418 unsigned long local_load, local_shares;
7419
7420 local_load = tg->cfs_rq[i]->load.weight;
7421 local_shares = (local_load * total_shares) / total_load;
7422 if (!local_shares)
7423 local_shares = MIN_GROUP_SHARES;
7424 if (local_shares == tg->se[i]->load.weight)
7425 continue;
7426
7427 spin_lock_irq(&cpu_rq(i)->lock);
7428 set_se_shares(tg->se[i], local_shares);
7429 spin_unlock_irq(&cpu_rq(i)->lock);
7430 balanced = 0;
7431 }
7432 }
7433
7434 return balanced;
7435}
7436
7437/*
7438 * How frequently should we rebalance_shares() across cpus?
7439 *
7440 * The more frequently we rebalance shares, the more accurate is the fairness
7441 * of cpu bandwidth distribution between task groups. However higher frequency
7442 * also implies increased scheduling overhead.
7443 *
7444 * sysctl_sched_min_bal_int_shares represents the minimum interval between
7445 * consecutive calls to rebalance_shares() in the same sched domain.
7446 *
7447 * sysctl_sched_max_bal_int_shares represents the maximum interval between
7448 * consecutive calls to rebalance_shares() in the same sched domain.
7449 *
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007450 * These settings allows for the appropriate trade-off between accuracy of
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007451 * fairness and the associated overhead.
7452 *
7453 */
7454
7455/* default: 8ms, units: milliseconds */
7456const_debug unsigned int sysctl_sched_min_bal_int_shares = 8;
7457
7458/* default: 128ms, units: milliseconds */
7459const_debug unsigned int sysctl_sched_max_bal_int_shares = 128;
7460
7461/* kernel thread that runs rebalance_shares() periodically */
7462static int load_balance_monitor(void *unused)
7463{
7464 unsigned int timeout = sysctl_sched_min_bal_int_shares;
7465 struct sched_param schedparm;
7466 int ret;
7467
7468 /*
7469 * We don't want this thread's execution to be limited by the shares
7470 * assigned to default group (init_task_group). Hence make it run
7471 * as a SCHED_RR RT task at the lowest priority.
7472 */
7473 schedparm.sched_priority = 1;
7474 ret = sched_setscheduler(current, SCHED_RR, &schedparm);
7475 if (ret)
7476 printk(KERN_ERR "Couldn't set SCHED_RR policy for load balance"
7477 " monitor thread (error = %d) \n", ret);
7478
7479 while (!kthread_should_stop()) {
7480 int i, cpu, balanced = 1;
7481
7482 /* Prevent cpus going down or coming up */
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +01007483 get_online_cpus();
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007484 /* lockout changes to doms_cur[] array */
7485 lock_doms_cur();
7486 /*
7487 * Enter a rcu read-side critical section to safely walk rq->sd
7488 * chain on various cpus and to walk task group list
7489 * (rq->leaf_cfs_rq_list) in rebalance_shares().
7490 */
7491 rcu_read_lock();
7492
7493 for (i = 0; i < ndoms_cur; i++) {
7494 cpumask_t cpumap = doms_cur[i];
7495 struct sched_domain *sd = NULL, *sd_prev = NULL;
7496
7497 cpu = first_cpu(cpumap);
7498
7499 /* Find the highest domain at which to balance shares */
7500 for_each_domain(cpu, sd) {
7501 if (!(sd->flags & SD_LOAD_BALANCE))
7502 continue;
7503 sd_prev = sd;
7504 }
7505
7506 sd = sd_prev;
7507 /* sd == NULL? No load balance reqd in this domain */
7508 if (!sd)
7509 continue;
7510
7511 balanced &= rebalance_shares(sd, cpu);
7512 }
7513
7514 rcu_read_unlock();
7515
7516 unlock_doms_cur();
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +01007517 put_online_cpus();
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007518
7519 if (!balanced)
7520 timeout = sysctl_sched_min_bal_int_shares;
7521 else if (timeout < sysctl_sched_max_bal_int_shares)
7522 timeout *= 2;
7523
7524 msleep_interruptible(timeout);
7525 }
7526
7527 return 0;
7528}
7529#endif /* CONFIG_SMP */
7530
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007531static void free_sched_group(struct task_group *tg)
7532{
7533 int i;
7534
7535 for_each_possible_cpu(i) {
7536 if (tg->cfs_rq)
7537 kfree(tg->cfs_rq[i]);
7538 if (tg->se)
7539 kfree(tg->se[i]);
7540 if (tg->rt_rq)
7541 kfree(tg->rt_rq[i]);
7542 if (tg->rt_se)
7543 kfree(tg->rt_se[i]);
7544 }
7545
7546 kfree(tg->cfs_rq);
7547 kfree(tg->se);
7548 kfree(tg->rt_rq);
7549 kfree(tg->rt_se);
7550 kfree(tg);
7551}
7552
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007553/* allocate runqueue etc for a new task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007554struct task_group *sched_create_group(void)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007555{
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007556 struct task_group *tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007557 struct cfs_rq *cfs_rq;
7558 struct sched_entity *se;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007559 struct rt_rq *rt_rq;
7560 struct sched_rt_entity *rt_se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007561 struct rq *rq;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007562 unsigned long flags;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007563 int i;
7564
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007565 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
7566 if (!tg)
7567 return ERR_PTR(-ENOMEM);
7568
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007569 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * NR_CPUS, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007570 if (!tg->cfs_rq)
7571 goto err;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007572 tg->se = kzalloc(sizeof(se) * NR_CPUS, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007573 if (!tg->se)
7574 goto err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007575 tg->rt_rq = kzalloc(sizeof(rt_rq) * NR_CPUS, GFP_KERNEL);
7576 if (!tg->rt_rq)
7577 goto err;
7578 tg->rt_se = kzalloc(sizeof(rt_se) * NR_CPUS, GFP_KERNEL);
7579 if (!tg->rt_se)
7580 goto err;
7581
7582 tg->shares = NICE_0_LOAD;
7583 tg->rt_ratio = 0; /* XXX */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007584
7585 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007586 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007587
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007588 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
7589 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007590 if (!cfs_rq)
7591 goto err;
7592
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007593 se = kmalloc_node(sizeof(struct sched_entity),
7594 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007595 if (!se)
7596 goto err;
7597
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007598 rt_rq = kmalloc_node(sizeof(struct rt_rq),
7599 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
7600 if (!rt_rq)
7601 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007602
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007603 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
7604 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
7605 if (!rt_se)
7606 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007607
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007608 init_tg_cfs_entry(rq, tg, cfs_rq, se, i, 0);
7609 init_tg_rt_entry(rq, tg, rt_rq, rt_se, i, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007610 }
7611
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007612 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007613 for_each_possible_cpu(i) {
7614 rq = cpu_rq(i);
7615 cfs_rq = tg->cfs_rq[i];
7616 list_add_rcu(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007617 rt_rq = tg->rt_rq[i];
7618 list_add_rcu(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007619 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007620 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007621 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007622
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007623 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007624
7625err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007626 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007627 return ERR_PTR(-ENOMEM);
7628}
7629
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007630/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007631static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007632{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007633 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007634 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007635}
7636
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007637/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007638void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007639{
James Bottomley7bae49d2007-10-29 21:18:11 +01007640 struct cfs_rq *cfs_rq = NULL;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007641 struct rt_rq *rt_rq = NULL;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007642 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007643 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007644
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007645 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007646 for_each_possible_cpu(i) {
7647 cfs_rq = tg->cfs_rq[i];
7648 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007649 rt_rq = tg->rt_rq[i];
7650 list_del_rcu(&rt_rq->leaf_rt_rq_list);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007651 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007652 list_del_rcu(&tg->list);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007653 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007654
James Bottomley7bae49d2007-10-29 21:18:11 +01007655 BUG_ON(!cfs_rq);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007656
7657 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007658 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007659}
7660
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007661/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02007662 * The caller of this function should have put the task in its new group
7663 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
7664 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007665 */
7666void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007667{
7668 int on_rq, running;
7669 unsigned long flags;
7670 struct rq *rq;
7671
7672 rq = task_rq_lock(tsk, &flags);
7673
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007674 update_rq_clock(rq);
7675
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01007676 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007677 on_rq = tsk->se.on_rq;
7678
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02007679 if (on_rq) {
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007680 dequeue_task(rq, tsk, 0);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02007681 if (unlikely(running))
7682 tsk->sched_class->put_prev_task(rq, tsk);
7683 }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007684
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007685 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007686
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02007687 if (on_rq) {
7688 if (unlikely(running))
7689 tsk->sched_class->set_curr_task(rq);
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02007690 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02007691 }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007692
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007693 task_rq_unlock(rq, &flags);
7694}
7695
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007696/* rq->lock to be locked by caller */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007697static void set_se_shares(struct sched_entity *se, unsigned long shares)
7698{
7699 struct cfs_rq *cfs_rq = se->cfs_rq;
7700 struct rq *rq = cfs_rq->rq;
7701 int on_rq;
7702
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007703 if (!shares)
7704 shares = MIN_GROUP_SHARES;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007705
7706 on_rq = se->on_rq;
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007707 if (on_rq) {
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007708 dequeue_entity(cfs_rq, se, 0);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007709 dec_cpu_load(rq, se->load.weight);
7710 }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007711
7712 se->load.weight = shares;
7713 se->load.inv_weight = div64_64((1ULL<<32), shares);
7714
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007715 if (on_rq) {
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007716 enqueue_entity(cfs_rq, se, 0);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007717 inc_cpu_load(rq, se->load.weight);
7718 }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007719}
7720
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007721static DEFINE_MUTEX(shares_mutex);
7722
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007723int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007724{
7725 int i;
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007726 struct cfs_rq *cfs_rq;
7727 struct rq *rq;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007728 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01007729
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007730 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007731 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007732 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007733
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007734 if (shares < MIN_GROUP_SHARES)
7735 shares = MIN_GROUP_SHARES;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007736
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007737 /*
7738 * Prevent any load balance activity (rebalance_shares,
7739 * load_balance_fair) from referring to this group first,
7740 * by taking it off the rq->leaf_cfs_rq_list on each cpu.
7741 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007742 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007743 for_each_possible_cpu(i) {
7744 cfs_rq = tg->cfs_rq[i];
7745 list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
7746 }
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007747 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007748
7749 /* wait for any ongoing reference to this group to finish */
7750 synchronize_sched();
7751
7752 /*
7753 * Now we are free to modify the group's share on each cpu
7754 * w/o tripping rebalance_share or load_balance_fair.
7755 */
7756 tg->shares = shares;
7757 for_each_possible_cpu(i) {
7758 spin_lock_irq(&cpu_rq(i)->lock);
7759 set_se_shares(tg->se[i], shares);
7760 spin_unlock_irq(&cpu_rq(i)->lock);
7761 }
7762
7763 /*
7764 * Enable load balance activity on this group, by inserting it back on
7765 * each cpu's rq->leaf_cfs_rq_list.
7766 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007767 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007768 for_each_possible_cpu(i) {
7769 rq = cpu_rq(i);
7770 cfs_rq = tg->cfs_rq[i];
7771 list_add_rcu(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7772 }
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007773 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007774done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007775 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007776 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007777}
7778
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007779unsigned long sched_group_shares(struct task_group *tg)
7780{
7781 return tg->shares;
7782}
7783
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007784/*
7785 * Ensure the total rt_ratio <= sysctl_sched_rt_ratio
7786 */
7787int sched_group_set_rt_ratio(struct task_group *tg, unsigned long rt_ratio)
7788{
7789 struct task_group *tgi;
7790 unsigned long total = 0;
7791
7792 rcu_read_lock();
7793 list_for_each_entry_rcu(tgi, &task_groups, list)
7794 total += tgi->rt_ratio;
7795 rcu_read_unlock();
7796
7797 if (total + rt_ratio - tg->rt_ratio > sysctl_sched_rt_ratio)
7798 return -EINVAL;
7799
7800 tg->rt_ratio = rt_ratio;
7801 return 0;
7802}
7803
7804unsigned long sched_group_rt_ratio(struct task_group *tg)
7805{
7806 return tg->rt_ratio;
7807}
7808
Ingo Molnar3a252012007-10-15 17:00:12 +02007809#endif /* CONFIG_FAIR_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007810
7811#ifdef CONFIG_FAIR_CGROUP_SCHED
7812
7813/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02007814static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007815{
Paul Menage2b01dfe2007-10-24 18:23:50 +02007816 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
7817 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007818}
7819
7820static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02007821cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007822{
7823 struct task_group *tg;
7824
Paul Menage2b01dfe2007-10-24 18:23:50 +02007825 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007826 /* This is early initialization for the top cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02007827 init_task_group.css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007828 return &init_task_group.css;
7829 }
7830
7831 /* we support only 1-level deep hierarchical scheduler atm */
Paul Menage2b01dfe2007-10-24 18:23:50 +02007832 if (cgrp->parent->parent)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007833 return ERR_PTR(-EINVAL);
7834
7835 tg = sched_create_group();
7836 if (IS_ERR(tg))
7837 return ERR_PTR(-ENOMEM);
7838
7839 /* Bind the cgroup to task_group object we just created */
Paul Menage2b01dfe2007-10-24 18:23:50 +02007840 tg->css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007841
7842 return &tg->css;
7843}
7844
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007845static void
7846cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007847{
Paul Menage2b01dfe2007-10-24 18:23:50 +02007848 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007849
7850 sched_destroy_group(tg);
7851}
7852
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007853static int
7854cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
7855 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007856{
7857 /* We don't support RT-tasks being in separate groups */
7858 if (tsk->sched_class != &fair_sched_class)
7859 return -EINVAL;
7860
7861 return 0;
7862}
7863
7864static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02007865cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007866 struct cgroup *old_cont, struct task_struct *tsk)
7867{
7868 sched_move_task(tsk);
7869}
7870
Paul Menage2b01dfe2007-10-24 18:23:50 +02007871static int cpu_shares_write_uint(struct cgroup *cgrp, struct cftype *cftype,
7872 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007873{
Paul Menage2b01dfe2007-10-24 18:23:50 +02007874 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007875}
7876
Paul Menage2b01dfe2007-10-24 18:23:50 +02007877static u64 cpu_shares_read_uint(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007878{
Paul Menage2b01dfe2007-10-24 18:23:50 +02007879 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007880
7881 return (u64) tg->shares;
7882}
7883
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007884static int cpu_rt_ratio_write_uint(struct cgroup *cgrp, struct cftype *cftype,
7885 u64 rt_ratio_val)
7886{
7887 return sched_group_set_rt_ratio(cgroup_tg(cgrp), rt_ratio_val);
7888}
7889
7890static u64 cpu_rt_ratio_read_uint(struct cgroup *cgrp, struct cftype *cft)
7891{
7892 struct task_group *tg = cgroup_tg(cgrp);
7893
7894 return (u64) tg->rt_ratio;
7895}
7896
Paul Menagefe5c7cc2007-10-29 21:18:11 +01007897static struct cftype cpu_files[] = {
7898 {
7899 .name = "shares",
7900 .read_uint = cpu_shares_read_uint,
7901 .write_uint = cpu_shares_write_uint,
7902 },
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007903 {
7904 .name = "rt_ratio",
7905 .read_uint = cpu_rt_ratio_read_uint,
7906 .write_uint = cpu_rt_ratio_write_uint,
7907 },
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007908};
7909
7910static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
7911{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01007912 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007913}
7914
7915struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01007916 .name = "cpu",
7917 .create = cpu_cgroup_create,
7918 .destroy = cpu_cgroup_destroy,
7919 .can_attach = cpu_cgroup_can_attach,
7920 .attach = cpu_cgroup_attach,
7921 .populate = cpu_cgroup_populate,
7922 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007923 .early_init = 1,
7924};
7925
7926#endif /* CONFIG_FAIR_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01007927
7928#ifdef CONFIG_CGROUP_CPUACCT
7929
7930/*
7931 * CPU accounting code for task groups.
7932 *
7933 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
7934 * (balbir@in.ibm.com).
7935 */
7936
7937/* track cpu usage of a group of tasks */
7938struct cpuacct {
7939 struct cgroup_subsys_state css;
7940 /* cpuusage holds pointer to a u64-type object on every cpu */
7941 u64 *cpuusage;
7942};
7943
7944struct cgroup_subsys cpuacct_subsys;
7945
7946/* return cpu accounting group corresponding to this container */
7947static inline struct cpuacct *cgroup_ca(struct cgroup *cont)
7948{
7949 return container_of(cgroup_subsys_state(cont, cpuacct_subsys_id),
7950 struct cpuacct, css);
7951}
7952
7953/* return cpu accounting group to which this task belongs */
7954static inline struct cpuacct *task_ca(struct task_struct *tsk)
7955{
7956 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
7957 struct cpuacct, css);
7958}
7959
7960/* create a new cpu accounting group */
7961static struct cgroup_subsys_state *cpuacct_create(
7962 struct cgroup_subsys *ss, struct cgroup *cont)
7963{
7964 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
7965
7966 if (!ca)
7967 return ERR_PTR(-ENOMEM);
7968
7969 ca->cpuusage = alloc_percpu(u64);
7970 if (!ca->cpuusage) {
7971 kfree(ca);
7972 return ERR_PTR(-ENOMEM);
7973 }
7974
7975 return &ca->css;
7976}
7977
7978/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007979static void
7980cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01007981{
7982 struct cpuacct *ca = cgroup_ca(cont);
7983
7984 free_percpu(ca->cpuusage);
7985 kfree(ca);
7986}
7987
7988/* return total cpu usage (in nanoseconds) of a group */
7989static u64 cpuusage_read(struct cgroup *cont, struct cftype *cft)
7990{
7991 struct cpuacct *ca = cgroup_ca(cont);
7992 u64 totalcpuusage = 0;
7993 int i;
7994
7995 for_each_possible_cpu(i) {
7996 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
7997
7998 /*
7999 * Take rq->lock to make 64-bit addition safe on 32-bit
8000 * platforms.
8001 */
8002 spin_lock_irq(&cpu_rq(i)->lock);
8003 totalcpuusage += *cpuusage;
8004 spin_unlock_irq(&cpu_rq(i)->lock);
8005 }
8006
8007 return totalcpuusage;
8008}
8009
8010static struct cftype files[] = {
8011 {
8012 .name = "usage",
8013 .read_uint = cpuusage_read,
8014 },
8015};
8016
8017static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cont)
8018{
8019 return cgroup_add_files(cont, ss, files, ARRAY_SIZE(files));
8020}
8021
8022/*
8023 * charge this task's execution time to its accounting group.
8024 *
8025 * called with rq->lock held.
8026 */
8027static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
8028{
8029 struct cpuacct *ca;
8030
8031 if (!cpuacct_subsys.active)
8032 return;
8033
8034 ca = task_ca(tsk);
8035 if (ca) {
8036 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
8037
8038 *cpuusage += cputime;
8039 }
8040}
8041
8042struct cgroup_subsys cpuacct_subsys = {
8043 .name = "cpuacct",
8044 .create = cpuacct_create,
8045 .destroy = cpuacct_destroy,
8046 .populate = cpuacct_populate,
8047 .subsys_id = cpuacct_subsys_id,
8048};
8049#endif /* CONFIG_CGROUP_CPUACCT */