blob: 7377222ab42fe3da53715f719684cedef2cfdf42 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * kernel/sched.c
3 *
4 * Kernel scheduler and related syscalls
5 *
6 * Copyright (C) 1991-2002 Linus Torvalds
7 *
8 * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and
9 * make semaphores SMP safe
10 * 1998-11-19 Implemented schedule_timeout() and related stuff
11 * by Andrea Arcangeli
12 * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
13 * hybrid priority-list and round-robin design with
14 * an array-switch method of distributing timeslices
15 * and per-CPU runqueues. Cleanups and useful suggestions
16 * by Davide Libenzi, preemptible kernel bits by Robert Love.
17 * 2003-09-03 Interactivity tuning by Con Kolivas.
18 * 2004-04-02 Scheduler domains code by Nick Piggin
Ingo Molnarc31f2e82007-07-09 18:52:01 +020019 * 2007-04-15 Work begun on replacing all interactivity tuning with a
20 * fair scheduling design by Con Kolivas.
21 * 2007-05-05 Load balancing (smp-nice) and other improvements
22 * by Peter Williams
23 * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith
24 * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri
Ingo Molnarb9131762008-01-25 21:08:19 +010025 * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins,
26 * Thomas Gleixner, Mike Kravetz
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
28
29#include <linux/mm.h>
30#include <linux/module.h>
31#include <linux/nmi.h>
32#include <linux/init.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020033#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/highmem.h>
35#include <linux/smp_lock.h>
36#include <asm/mmu_context.h>
37#include <linux/interrupt.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080038#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/completion.h>
40#include <linux/kernel_stat.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070041#include <linux/debug_locks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/security.h>
43#include <linux/notifier.h>
44#include <linux/profile.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080045#include <linux/freezer.h>
akpm@osdl.org198e2f12006-01-12 01:05:30 -080046#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/blkdev.h>
48#include <linux/delay.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070049#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/smp.h>
51#include <linux/threads.h>
52#include <linux/timer.h>
53#include <linux/rcupdate.h>
54#include <linux/cpu.h>
55#include <linux/cpuset.h>
56#include <linux/percpu.h>
57#include <linux/kthread.h>
58#include <linux/seq_file.h>
Nick Piggine692ab52007-07-26 13:40:43 +020059#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/syscalls.h>
61#include <linux/times.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070062#include <linux/tsacct_kern.h>
bibo maoc6fd91f2006-03-26 01:38:20 -080063#include <linux/kprobes.h>
Shailabh Nagar0ff92242006-07-14 00:24:37 -070064#include <linux/delayacct.h>
Eric Dumazet5517d862007-05-08 00:32:57 -070065#include <linux/reciprocal_div.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020066#include <linux/unistd.h>
Jens Axboef5ff8422007-09-21 09:19:54 +020067#include <linux/pagemap.h>
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +010068#include <linux/hrtimer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Eric Dumazet5517d862007-05-08 00:32:57 -070070#include <asm/tlb.h>
Satyam Sharma838225b2007-10-24 18:23:50 +020071#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*
Alexey Dobriyanb035b6d2007-02-10 01:45:10 -080074 * Scheduler clock - returns current time in nanosec units.
75 * This is default implementation.
76 * Architectures and sub-architectures can override this.
77 */
78unsigned long long __attribute__((weak)) sched_clock(void)
79{
Eric Dumazetd6322fa2007-11-09 22:39:38 +010080 return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ);
Alexey Dobriyanb035b6d2007-02-10 01:45:10 -080081}
82
83/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * Convert user-nice values [ -20 ... 0 ... 19 ]
85 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
86 * and back.
87 */
88#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
89#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
90#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
91
92/*
93 * 'User priority' is the nice value converted to something we
94 * can work with better when scaling various scheduler parameters,
95 * it's a [ 0 ... 39 ] range.
96 */
97#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
98#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
99#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
100
101/*
Ingo Molnard7876a02008-01-25 21:08:19 +0100102 * Helpers for converting nanosecond timing to jiffy resolution
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 */
Eric Dumazetd6322fa2007-11-09 22:39:38 +0100104#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200106#define NICE_0_LOAD SCHED_LOAD_SCALE
107#define NICE_0_SHIFT SCHED_LOAD_SHIFT
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/*
110 * These are the 'tuning knobs' of the scheduler:
111 *
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +0200112 * default timeslice is 100 msecs (used only for SCHED_RR tasks).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * Timeslices get refilled after they expire.
114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700116
Eric Dumazet5517d862007-05-08 00:32:57 -0700117#ifdef CONFIG_SMP
118/*
119 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
120 * Since cpu_power is a 'constant', we can use a reciprocal divide.
121 */
122static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
123{
124 return reciprocal_divide(load, sg->reciprocal_cpu_power);
125}
126
127/*
128 * Each time a sched group cpu_power is changed,
129 * we must compute its reciprocal value
130 */
131static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
132{
133 sg->__cpu_power += val;
134 sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
135}
136#endif
137
Ingo Molnare05606d2007-07-09 18:51:59 +0200138static inline int rt_policy(int policy)
139{
140 if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR))
141 return 1;
142 return 0;
143}
144
145static inline int task_has_rt_policy(struct task_struct *p)
146{
147 return rt_policy(p->policy);
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200151 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200153struct rt_prio_array {
154 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
155 struct list_head queue[MAX_RT_PRIO];
156};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100158#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200159
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700160#include <linux/cgroup.h>
161
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200162struct cfs_rq;
163
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100164static LIST_HEAD(task_groups);
165
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200166/* task group related information */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200167struct task_group {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100168#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700169 struct cgroup_subsys_state css;
170#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100171
172#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200173 /* schedulable entities of this group on each cpu */
174 struct sched_entity **se;
175 /* runqueue "owned" by this group on each cpu */
176 struct cfs_rq **cfs_rq;
177 unsigned long shares;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100178#endif
179
180#ifdef CONFIG_RT_GROUP_SCHED
181 struct sched_rt_entity **rt_se;
182 struct rt_rq **rt_rq;
183
184 u64 rt_runtime;
185#endif
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +0100186
Srivatsa Vaddagiriae8393e2007-10-29 21:18:11 +0100187 struct rcu_head rcu;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100188 struct list_head list;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200189};
190
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100191#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200192/* Default task group's sched entity on each cpu */
193static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
194/* Default task group's cfs_rq on each cpu */
195static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
196
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100197static struct sched_entity *init_sched_entity_p[NR_CPUS];
198static struct cfs_rq *init_cfs_rq_p[NR_CPUS];
199#endif
200
201#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100202static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
203static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
204
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100205static struct sched_rt_entity *init_sched_rt_entity_p[NR_CPUS];
206static struct rt_rq *init_rt_rq_p[NR_CPUS];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100207#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100208
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100209/* task_group_lock serializes add/remove of task groups and also changes to
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100210 * a task group's cpu shares.
211 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +0100212static DEFINE_SPINLOCK(task_group_lock);
Srivatsa Vaddagiriec2c5072008-01-25 21:07:59 +0100213
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100214/* doms_cur_mutex serializes access to doms_cur[] array */
215static DEFINE_MUTEX(doms_cur_mutex);
216
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100217#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100218#ifdef CONFIG_USER_SCHED
Ingo Molnar0eab9142008-01-25 21:08:19 +0100219# define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200220#else
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100221# define INIT_TASK_GROUP_LOAD NICE_0_LOAD
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200222#endif
223
Srivatsa Vaddagiri93f992c2008-01-25 21:07:59 +0100224static int init_task_group_load = INIT_TASK_GROUP_LOAD;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100225#endif
226
227/* Default task group.
228 * Every task in system belong to this group at bootup.
229 */
230struct task_group init_task_group = {
231#ifdef CONFIG_FAIR_GROUP_SCHED
232 .se = init_sched_entity_p,
233 .cfs_rq = init_cfs_rq_p,
234#endif
235
236#ifdef CONFIG_RT_GROUP_SCHED
237 .rt_se = init_sched_rt_entity_p,
238 .rt_rq = init_rt_rq_p,
239#endif
240};
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200241
242/* return group to which a task belongs */
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200243static inline struct task_group *task_group(struct task_struct *p)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200244{
Ingo Molnar4cf86d72007-10-15 17:00:14 +0200245 struct task_group *tg;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200246
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100247#ifdef CONFIG_USER_SCHED
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200248 tg = p->user->tg;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100249#elif defined(CONFIG_CGROUP_SCHED)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -0700250 tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
251 struct task_group, css);
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200252#else
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100253 tg = &init_task_group;
Srivatsa Vaddagiri24e377a2007-10-15 17:00:09 +0200254#endif
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +0200255 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200256}
257
258/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100259static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200260{
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100261#ifdef CONFIG_FAIR_GROUP_SCHED
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +0100262 p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
263 p->se.parent = task_group(p)->se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100264#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100265
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100266#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100267 p->rt.rt_rq = task_group(p)->rt_rq[cpu];
268 p->rt.parent = task_group(p)->rt_se[cpu];
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100269#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200270}
271
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100272static inline void lock_doms_cur(void)
273{
274 mutex_lock(&doms_cur_mutex);
275}
276
277static inline void unlock_doms_cur(void)
278{
279 mutex_unlock(&doms_cur_mutex);
280}
281
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200282#else
283
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100284static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +0100285static inline void lock_doms_cur(void) { }
286static inline void unlock_doms_cur(void) { }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200287
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100288#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +0200289
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200290/* CFS-related fields in a runqueue */
291struct cfs_rq {
292 struct load_weight load;
293 unsigned long nr_running;
294
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200295 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200296 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200297
298 struct rb_root tasks_timeline;
299 struct rb_node *rb_leftmost;
300 struct rb_node *rb_load_balance_curr;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200301 /* 'curr' points to currently running entity on this cfs_rq.
302 * It is set to NULL otherwise (i.e when none are currently running).
303 */
Peter Zijlstraaa2ac252008-03-14 21:12:12 +0100304 struct sched_entity *curr, *next;
Peter Zijlstraddc97292007-10-15 17:00:10 +0200305
306 unsigned long nr_spread_over;
307
Ingo Molnar62160e32007-10-15 17:00:03 +0200308#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200309 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
310
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100311 /*
312 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200313 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
314 * (like users, containers etc.)
315 *
316 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
317 * list is used during load balance.
318 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100319 struct list_head leaf_cfs_rq_list;
320 struct task_group *tg; /* group that "owns" this runqueue */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200321#endif
322};
323
324/* Real-Time classes' related field in a runqueue: */
325struct rt_rq {
326 struct rt_prio_array active;
Steven Rostedt63489e42008-01-25 21:08:03 +0100327 unsigned long rt_nr_running;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100328#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100329 int highest_prio; /* highest queued rt task prio */
330#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100331#ifdef CONFIG_SMP
Gregory Haskins73fe6aa2008-01-25 21:08:07 +0100332 unsigned long rt_nr_migratory;
Gregory Haskinsa22d7fc2008-01-25 21:08:12 +0100333 int overloaded;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100334#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100335 int rt_throttled;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100336 u64 rt_time;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100337
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100338#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +0100339 unsigned long rt_nr_boosted;
340
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100341 struct rq *rq;
342 struct list_head leaf_rt_rq_list;
343 struct task_group *tg;
344 struct sched_rt_entity *rt_se;
345#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200346};
347
Gregory Haskins57d885f2008-01-25 21:08:18 +0100348#ifdef CONFIG_SMP
349
350/*
351 * We add the notion of a root-domain which will be used to define per-domain
Ingo Molnar0eab9142008-01-25 21:08:19 +0100352 * variables. Each exclusive cpuset essentially defines an island domain by
353 * fully partitioning the member cpus from any other cpuset. Whenever a new
Gregory Haskins57d885f2008-01-25 21:08:18 +0100354 * exclusive cpuset is created, we also create and attach a new root-domain
355 * object.
356 *
Gregory Haskins57d885f2008-01-25 21:08:18 +0100357 */
358struct root_domain {
359 atomic_t refcount;
360 cpumask_t span;
361 cpumask_t online;
Gregory Haskins637f5082008-01-25 21:08:18 +0100362
Ingo Molnar0eab9142008-01-25 21:08:19 +0100363 /*
Gregory Haskins637f5082008-01-25 21:08:18 +0100364 * The "RT overload" flag: it gets set if a CPU has more than
365 * one runnable RT task.
366 */
367 cpumask_t rto_mask;
Ingo Molnar0eab9142008-01-25 21:08:19 +0100368 atomic_t rto_count;
Gregory Haskins57d885f2008-01-25 21:08:18 +0100369};
370
Gregory Haskinsdc938522008-01-25 21:08:26 +0100371/*
372 * By default the system creates a single root-domain with all cpus as
373 * members (mimicking the global state we have today).
374 */
Gregory Haskins57d885f2008-01-25 21:08:18 +0100375static struct root_domain def_root_domain;
376
377#endif
378
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200379/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 * This is the main, per-CPU runqueue data structure.
381 *
382 * Locking rule: those places that want to lock multiple runqueues
383 * (such as the load balancing or the thread migration code), lock
384 * acquire operations must be ordered by ascending &runqueue.
385 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700386struct rq {
Ingo Molnard8016492007-10-18 21:32:55 +0200387 /* runqueue lock: */
388 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 /*
391 * nr_running and cpu_load should be in the same cacheline because
392 * remote CPUs use both these fields when doing load calculation.
393 */
394 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200395 #define CPU_LOAD_IDX_MAX 5
396 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700397 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700398#ifdef CONFIG_NO_HZ
399 unsigned char in_nohz_recently;
400#endif
Ingo Molnard8016492007-10-18 21:32:55 +0200401 /* capture load from *all* tasks on this cpu: */
402 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200403 unsigned long nr_load_updates;
404 u64 nr_switches;
405
406 struct cfs_rq cfs;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100407 struct rt_rq rt;
408 u64 rt_period_expire;
Peter Zijlstra48d5e252008-01-25 21:08:31 +0100409 int rt_throttled;
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100410
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200411#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnard8016492007-10-18 21:32:55 +0200412 /* list of leaf cfs_rq on this cpu: */
413 struct list_head leaf_cfs_rq_list;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +0100414#endif
415#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +0100416 struct list_head leaf_rt_rq_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 /*
420 * This is part of a global counter where only the total sum
421 * over all CPUs matters. A task can increase this counter on
422 * one CPU and if it got migrated afterwards it may decrease
423 * it on another CPU. Always updated under the runqueue lock:
424 */
425 unsigned long nr_uninterruptible;
426
Ingo Molnar36c8b582006-07-03 00:25:41 -0700427 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800428 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200430
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200431 u64 clock, prev_clock_raw;
432 s64 clock_max_delta;
433
Guillaume Chazaraincc203d22008-01-25 21:08:34 +0100434 unsigned int clock_warps, clock_overflows, clock_underflows;
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200435 u64 idle_clock;
436 unsigned int clock_deep_idle_events;
Ingo Molnar529c7722007-08-10 23:05:11 +0200437 u64 tick_timestamp;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 atomic_t nr_iowait;
440
441#ifdef CONFIG_SMP
Ingo Molnar0eab9142008-01-25 21:08:19 +0100442 struct root_domain *rd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 struct sched_domain *sd;
444
445 /* For active balancing */
446 int active_balance;
447 int push_cpu;
Ingo Molnard8016492007-10-18 21:32:55 +0200448 /* cpu of this runqueue: */
449 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Ingo Molnar36c8b582006-07-03 00:25:41 -0700451 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 struct list_head migration_queue;
453#endif
454
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100455#ifdef CONFIG_SCHED_HRTICK
456 unsigned long hrtick_flags;
457 ktime_t hrtick_expire;
458 struct hrtimer hrtick_timer;
459#endif
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461#ifdef CONFIG_SCHEDSTATS
462 /* latency stats */
463 struct sched_info rq_sched_info;
464
465 /* sys_sched_yield() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200466 unsigned int yld_exp_empty;
467 unsigned int yld_act_empty;
468 unsigned int yld_both_empty;
469 unsigned int yld_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 /* schedule() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200472 unsigned int sched_switch;
473 unsigned int sched_count;
474 unsigned int sched_goidle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 /* try_to_wake_up() stats */
Ken Chen480b9432007-10-18 21:32:56 +0200477 unsigned int ttwu_count;
478 unsigned int ttwu_local;
Ingo Molnarb8efb562007-10-15 17:00:10 +0200479
480 /* BKL stats */
Ken Chen480b9432007-10-18 21:32:56 +0200481 unsigned int bkl_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482#endif
Ingo Molnarfcb99372006-07-03 00:25:10 -0700483 struct lock_class_key rq_lock_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484};
485
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700486static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Ingo Molnardd41f592007-07-09 18:51:59 +0200488static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
489{
490 rq->curr->sched_class->check_preempt_curr(rq, p);
491}
492
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700493static inline int cpu_of(struct rq *rq)
494{
495#ifdef CONFIG_SMP
496 return rq->cpu;
497#else
498 return 0;
499#endif
500}
501
Nick Piggin674311d2005-06-25 14:57:27 -0700502/*
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200503 * Update the per-runqueue clock, as finegrained as the platform can give
504 * us, but without assuming monotonicity, etc.:
Ingo Molnar20d315d2007-07-09 18:51:58 +0200505 */
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200506static void __update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200507{
508 u64 prev_raw = rq->prev_clock_raw;
509 u64 now = sched_clock();
510 s64 delta = now - prev_raw;
511 u64 clock = rq->clock;
512
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200513#ifdef CONFIG_SCHED_DEBUG
514 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
515#endif
Ingo Molnar20d315d2007-07-09 18:51:58 +0200516 /*
517 * Protect against sched_clock() occasionally going backwards:
518 */
519 if (unlikely(delta < 0)) {
520 clock++;
521 rq->clock_warps++;
522 } else {
523 /*
524 * Catch too large forward jumps too:
525 */
Ingo Molnar529c7722007-08-10 23:05:11 +0200526 if (unlikely(clock + delta > rq->tick_timestamp + TICK_NSEC)) {
527 if (clock < rq->tick_timestamp + TICK_NSEC)
528 clock = rq->tick_timestamp + TICK_NSEC;
529 else
530 clock++;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200531 rq->clock_overflows++;
532 } else {
533 if (unlikely(delta > rq->clock_max_delta))
534 rq->clock_max_delta = delta;
535 clock += delta;
536 }
537 }
538
539 rq->prev_clock_raw = now;
540 rq->clock = clock;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200541}
542
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200543static void update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200544{
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200545 if (likely(smp_processor_id() == cpu_of(rq)))
546 __update_rq_clock(rq);
547}
Ingo Molnar20d315d2007-07-09 18:51:58 +0200548
Ingo Molnar20d315d2007-07-09 18:51:58 +0200549/*
Nick Piggin674311d2005-06-25 14:57:27 -0700550 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700551 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700552 *
553 * The domain tree of any CPU may only be accessed from within
554 * preempt-disabled sections.
555 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700556#define for_each_domain(cpu, __sd) \
557 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
560#define this_rq() (&__get_cpu_var(runqueues))
561#define task_rq(p) cpu_rq(task_cpu(p))
562#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
563
Peter Zijlstra48d5e252008-01-25 21:08:31 +0100564unsigned long rt_needs_cpu(int cpu)
565{
566 struct rq *rq = cpu_rq(cpu);
567 u64 delta;
568
569 if (!rq->rt_throttled)
570 return 0;
571
572 if (rq->clock > rq->rt_period_expire)
573 return 1;
574
575 delta = rq->rt_period_expire - rq->clock;
576 do_div(delta, NSEC_PER_SEC / HZ);
577
578 return (unsigned long)delta;
579}
580
Ingo Molnare436d802007-07-19 21:28:35 +0200581/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200582 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
583 */
584#ifdef CONFIG_SCHED_DEBUG
585# define const_debug __read_mostly
586#else
587# define const_debug static const
588#endif
589
590/*
591 * Debugging: various feature bits
592 */
593enum {
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200594 SCHED_FEAT_NEW_FAIR_SLEEPERS = 1,
Ingo Molnar96126332007-11-15 20:57:40 +0100595 SCHED_FEAT_WAKEUP_PREEMPT = 2,
596 SCHED_FEAT_START_DEBIT = 4,
Peter Zijlstra2070ee02008-03-21 16:43:47 +0100597 SCHED_FEAT_HRTICK = 8,
598 SCHED_FEAT_DOUBLE_TICK = 16,
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200599};
600
601const_debug unsigned int sysctl_sched_features =
Ingo Molnar8401f772007-10-18 21:32:55 +0200602 SCHED_FEAT_NEW_FAIR_SLEEPERS * 1 |
Ingo Molnar96126332007-11-15 20:57:40 +0100603 SCHED_FEAT_WAKEUP_PREEMPT * 1 |
Ingo Molnar8401f772007-10-18 21:32:55 +0200604 SCHED_FEAT_START_DEBIT * 1 |
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100605 SCHED_FEAT_HRTICK * 1 |
606 SCHED_FEAT_DOUBLE_TICK * 0;
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200607
608#define sched_feat(x) (sysctl_sched_features & SCHED_FEAT_##x)
609
610/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +0100611 * Number of tasks to iterate in a single balance run.
612 * Limited because this is done with IRQs disabled.
613 */
614const_debug unsigned int sysctl_sched_nr_migrate = 32;
615
616/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100617 * period over which we measure -rt task cpu usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100618 * default: 1s
619 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100620unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100621
Ingo Molnar6892b752008-02-13 14:02:36 +0100622static __read_mostly int scheduler_running;
623
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100624/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100625 * part of the period that we allow rt tasks to run in us.
626 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100627 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +0100628int sysctl_sched_rt_runtime = 950000;
629
630/*
631 * single value that denotes runtime == period, ie unlimited time.
632 */
633#define RUNTIME_INF ((u64)~0ULL)
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100634
Ingo Molnar27ec4402008-02-28 21:00:21 +0100635static const unsigned long long time_sync_thresh = 100000;
636
637static DEFINE_PER_CPU(unsigned long long, time_offset);
638static DEFINE_PER_CPU(unsigned long long, prev_cpu_time);
639
Peter Zijlstrafa85ae22008-01-25 21:08:29 +0100640/*
Ingo Molnar27ec4402008-02-28 21:00:21 +0100641 * Global lock which we take every now and then to synchronize
642 * the CPUs time. This method is not warp-safe, but it's good
643 * enough to synchronize slowly diverging time sources and thus
644 * it's good enough for tracing:
Ingo Molnare436d802007-07-19 21:28:35 +0200645 */
Ingo Molnar27ec4402008-02-28 21:00:21 +0100646static DEFINE_SPINLOCK(time_sync_lock);
647static unsigned long long prev_global_time;
648
649static unsigned long long __sync_cpu_clock(cycles_t time, int cpu)
650{
651 unsigned long flags;
652
653 spin_lock_irqsave(&time_sync_lock, flags);
654
655 if (time < prev_global_time) {
656 per_cpu(time_offset, cpu) += prev_global_time - time;
657 time = prev_global_time;
658 } else {
659 prev_global_time = time;
660 }
661
662 spin_unlock_irqrestore(&time_sync_lock, flags);
663
664 return time;
665}
666
667static unsigned long long __cpu_clock(int cpu)
Ingo Molnare436d802007-07-19 21:28:35 +0200668{
Ingo Molnare436d802007-07-19 21:28:35 +0200669 unsigned long long now;
670 unsigned long flags;
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200671 struct rq *rq;
Ingo Molnare436d802007-07-19 21:28:35 +0200672
Ingo Molnar8ced5f62007-12-07 19:02:47 +0100673 /*
674 * Only call sched_clock() if the scheduler has already been
675 * initialized (some code might call cpu_clock() very early):
676 */
Ingo Molnar6892b752008-02-13 14:02:36 +0100677 if (unlikely(!scheduler_running))
678 return 0;
679
680 local_irq_save(flags);
681 rq = cpu_rq(cpu);
682 update_rq_clock(rq);
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200683 now = rq->clock;
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200684 local_irq_restore(flags);
Ingo Molnare436d802007-07-19 21:28:35 +0200685
686 return now;
687}
Ingo Molnar27ec4402008-02-28 21:00:21 +0100688
689/*
690 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
691 * clock constructed from sched_clock():
692 */
693unsigned long long cpu_clock(int cpu)
694{
695 unsigned long long prev_cpu_time, time, delta_time;
696
697 prev_cpu_time = per_cpu(prev_cpu_time, cpu);
698 time = __cpu_clock(cpu) + per_cpu(time_offset, cpu);
699 delta_time = time-prev_cpu_time;
700
701 if (unlikely(delta_time > time_sync_thresh))
702 time = __sync_cpu_clock(time, cpu);
703
704 return time;
705}
Paul E. McKenneya58f6f22007-10-15 17:00:14 +0200706EXPORT_SYMBOL_GPL(cpu_clock);
Ingo Molnare436d802007-07-19 21:28:35 +0200707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700709# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700711#ifndef finish_arch_switch
712# define finish_arch_switch(prev) do { } while (0)
713#endif
714
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100715static inline int task_current(struct rq *rq, struct task_struct *p)
716{
717 return rq->curr == p;
718}
719
Nick Piggin4866cde2005-06-25 14:57:23 -0700720#ifndef __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{
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100723 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700724}
725
Ingo Molnar70b97a72006-07-03 00:25:42 -0700726static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700727{
728}
729
Ingo Molnar70b97a72006-07-03 00:25:42 -0700730static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700731{
Ingo Molnarda04c032005-09-13 11:17:59 +0200732#ifdef CONFIG_DEBUG_SPINLOCK
733 /* this is a valid case when another task releases the spinlock */
734 rq->lock.owner = current;
735#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700736 /*
737 * If we are tracking spinlock dependencies then we have to
738 * fix up the runqueue lock - which gets 'carried over' from
739 * prev into current:
740 */
741 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
742
Nick Piggin4866cde2005-06-25 14:57:23 -0700743 spin_unlock_irq(&rq->lock);
744}
745
746#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700747static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700748{
749#ifdef CONFIG_SMP
750 return p->oncpu;
751#else
Dmitry Adamushko051a1d12007-12-18 15:21:13 +0100752 return task_current(rq, p);
Nick Piggin4866cde2005-06-25 14:57:23 -0700753#endif
754}
755
Ingo Molnar70b97a72006-07-03 00:25:42 -0700756static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700757{
758#ifdef CONFIG_SMP
759 /*
760 * We can optimise this out completely for !SMP, because the
761 * SMP rebalancing from interrupt is the only thing that cares
762 * here.
763 */
764 next->oncpu = 1;
765#endif
766#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
767 spin_unlock_irq(&rq->lock);
768#else
769 spin_unlock(&rq->lock);
770#endif
771}
772
Ingo Molnar70b97a72006-07-03 00:25:42 -0700773static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700774{
775#ifdef CONFIG_SMP
776 /*
777 * After ->oncpu is cleared, the task can be moved to a different CPU.
778 * We must ensure this doesn't happen until the switch is completely
779 * finished.
780 */
781 smp_wmb();
782 prev->oncpu = 0;
783#endif
784#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
785 local_irq_enable();
786#endif
787}
788#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700791 * __task_rq_lock - lock the runqueue a given task resides on.
792 * Must be called interrupts disabled.
793 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700794static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700795 __acquires(rq->lock)
796{
Andi Kleen3a5c3592007-10-15 17:00:14 +0200797 for (;;) {
798 struct rq *rq = task_rq(p);
799 spin_lock(&rq->lock);
800 if (likely(rq == task_rq(p)))
801 return rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700802 spin_unlock(&rq->lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -0700803 }
Ingo Molnarb29739f2006-06-27 02:54:51 -0700804}
805
806/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 * task_rq_lock - lock the runqueue a given task resides on and disable
Ingo Molnar41a2d6c2007-12-05 15:46:09 +0100808 * interrupts. Note the ordering: we can safely lookup the task_rq without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 * explicitly disabling preemption.
810 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700811static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 __acquires(rq->lock)
813{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700814 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Andi Kleen3a5c3592007-10-15 17:00:14 +0200816 for (;;) {
817 local_irq_save(*flags);
818 rq = task_rq(p);
819 spin_lock(&rq->lock);
820 if (likely(rq == task_rq(p)))
821 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 spin_unlock_irqrestore(&rq->lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Alexey Dobriyana9957442007-10-15 17:00:13 +0200826static void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700827 __releases(rq->lock)
828{
829 spin_unlock(&rq->lock);
830}
831
Ingo Molnar70b97a72006-07-03 00:25:42 -0700832static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 __releases(rq->lock)
834{
835 spin_unlock_irqrestore(&rq->lock, *flags);
836}
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800839 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 */
Alexey Dobriyana9957442007-10-15 17:00:13 +0200841static struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 __acquires(rq->lock)
843{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700844 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 local_irq_disable();
847 rq = this_rq();
848 spin_lock(&rq->lock);
849
850 return rq;
851}
852
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200853/*
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200854 * We are going deep-idle (irqs are disabled):
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200855 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200856void sched_clock_idle_sleep_event(void)
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200857{
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200858 struct rq *rq = cpu_rq(smp_processor_id());
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200859
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200860 spin_lock(&rq->lock);
861 __update_rq_clock(rq);
862 spin_unlock(&rq->lock);
863 rq->clock_deep_idle_events++;
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200864}
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200865EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
866
867/*
868 * We just idled delta nanoseconds (called with irqs disabled):
869 */
870void sched_clock_idle_wakeup_event(u64 delta_ns)
871{
872 struct rq *rq = cpu_rq(smp_processor_id());
873 u64 now = sched_clock();
874
875 rq->idle_clock += delta_ns;
876 /*
877 * Override the previous timestamp and ignore all
878 * sched_clock() deltas that occured while we idled,
879 * and use the PM-provided delta_ns to advance the
880 * rq clock:
881 */
882 spin_lock(&rq->lock);
883 rq->prev_clock_raw = now;
884 rq->clock += delta_ns;
885 spin_unlock(&rq->lock);
Guillaume Chazarain782daee2008-01-25 21:08:33 +0100886 touch_softlockup_watchdog();
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200887}
888EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200889
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100890static void __resched_task(struct task_struct *p, int tif_bit);
891
892static inline void resched_task(struct task_struct *p)
893{
894 __resched_task(p, TIF_NEED_RESCHED);
895}
896
897#ifdef CONFIG_SCHED_HRTICK
898/*
899 * Use HR-timers to deliver accurate preemption points.
900 *
901 * Its all a bit involved since we cannot program an hrt while holding the
902 * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
903 * reschedule event.
904 *
905 * When we get rescheduled we reprogram the hrtick_timer outside of the
906 * rq->lock.
907 */
908static inline void resched_hrt(struct task_struct *p)
909{
910 __resched_task(p, TIF_HRTICK_RESCHED);
911}
912
913static inline void resched_rq(struct rq *rq)
914{
915 unsigned long flags;
916
917 spin_lock_irqsave(&rq->lock, flags);
918 resched_task(rq->curr);
919 spin_unlock_irqrestore(&rq->lock, flags);
920}
921
922enum {
923 HRTICK_SET, /* re-programm hrtick_timer */
924 HRTICK_RESET, /* not a new slice */
925};
926
927/*
928 * Use hrtick when:
929 * - enabled by features
930 * - hrtimer is actually high res
931 */
932static inline int hrtick_enabled(struct rq *rq)
933{
934 if (!sched_feat(HRTICK))
935 return 0;
936 return hrtimer_is_hres_active(&rq->hrtick_timer);
937}
938
939/*
940 * Called to set the hrtick timer state.
941 *
942 * called with rq->lock held and irqs disabled
943 */
944static void hrtick_start(struct rq *rq, u64 delay, int reset)
945{
946 assert_spin_locked(&rq->lock);
947
948 /*
949 * preempt at: now + delay
950 */
951 rq->hrtick_expire =
952 ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
953 /*
954 * indicate we need to program the timer
955 */
956 __set_bit(HRTICK_SET, &rq->hrtick_flags);
957 if (reset)
958 __set_bit(HRTICK_RESET, &rq->hrtick_flags);
959
960 /*
961 * New slices are called from the schedule path and don't need a
962 * forced reschedule.
963 */
964 if (reset)
965 resched_hrt(rq->curr);
966}
967
968static void hrtick_clear(struct rq *rq)
969{
970 if (hrtimer_active(&rq->hrtick_timer))
971 hrtimer_cancel(&rq->hrtick_timer);
972}
973
974/*
975 * Update the timer from the possible pending state.
976 */
977static void hrtick_set(struct rq *rq)
978{
979 ktime_t time;
980 int set, reset;
981 unsigned long flags;
982
983 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
984
985 spin_lock_irqsave(&rq->lock, flags);
986 set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
987 reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
988 time = rq->hrtick_expire;
989 clear_thread_flag(TIF_HRTICK_RESCHED);
990 spin_unlock_irqrestore(&rq->lock, flags);
991
992 if (set) {
993 hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
994 if (reset && !hrtimer_active(&rq->hrtick_timer))
995 resched_rq(rq);
996 } else
997 hrtick_clear(rq);
998}
999
1000/*
1001 * High-resolution timer tick.
1002 * Runs from hardirq context with interrupts disabled.
1003 */
1004static enum hrtimer_restart hrtick(struct hrtimer *timer)
1005{
1006 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
1007
1008 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
1009
1010 spin_lock(&rq->lock);
1011 __update_rq_clock(rq);
1012 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
1013 spin_unlock(&rq->lock);
1014
1015 return HRTIMER_NORESTART;
1016}
1017
1018static inline void init_rq_hrtick(struct rq *rq)
1019{
1020 rq->hrtick_flags = 0;
1021 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1022 rq->hrtick_timer.function = hrtick;
1023 rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
1024}
1025
1026void hrtick_resched(void)
1027{
1028 struct rq *rq;
1029 unsigned long flags;
1030
1031 if (!test_thread_flag(TIF_HRTICK_RESCHED))
1032 return;
1033
1034 local_irq_save(flags);
1035 rq = cpu_rq(smp_processor_id());
1036 hrtick_set(rq);
1037 local_irq_restore(flags);
1038}
1039#else
1040static inline void hrtick_clear(struct rq *rq)
1041{
1042}
1043
1044static inline void hrtick_set(struct rq *rq)
1045{
1046}
1047
1048static inline void init_rq_hrtick(struct rq *rq)
1049{
1050}
1051
1052void hrtick_resched(void)
1053{
1054}
1055#endif
1056
Ingo Molnar1b9f19c2007-07-09 18:51:59 +02001057/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001058 * resched_task - mark a task 'to be rescheduled now'.
1059 *
1060 * On UP this means the setting of the need_resched flag, on SMP it
1061 * might also involve a cross-CPU call to trigger the scheduler on
1062 * the target CPU.
1063 */
1064#ifdef CONFIG_SMP
1065
1066#ifndef tsk_is_polling
1067#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
1068#endif
1069
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001070static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001071{
1072 int cpu;
1073
1074 assert_spin_locked(&task_rq(p)->lock);
1075
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001076 if (unlikely(test_tsk_thread_flag(p, tif_bit)))
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001077 return;
1078
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001079 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001080
1081 cpu = task_cpu(p);
1082 if (cpu == smp_processor_id())
1083 return;
1084
1085 /* NEED_RESCHED must be visible before we test polling */
1086 smp_mb();
1087 if (!tsk_is_polling(p))
1088 smp_send_reschedule(cpu);
1089}
1090
1091static void resched_cpu(int cpu)
1092{
1093 struct rq *rq = cpu_rq(cpu);
1094 unsigned long flags;
1095
1096 if (!spin_trylock_irqsave(&rq->lock, flags))
1097 return;
1098 resched_task(cpu_curr(cpu));
1099 spin_unlock_irqrestore(&rq->lock, flags);
1100}
Thomas Gleixner06d83082008-03-22 09:20:24 +01001101
1102#ifdef CONFIG_NO_HZ
1103/*
1104 * When add_timer_on() enqueues a timer into the timer wheel of an
1105 * idle CPU then this timer might expire before the next timer event
1106 * which is scheduled to wake up that CPU. In case of a completely
1107 * idle system the next event might even be infinite time into the
1108 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1109 * leaves the inner idle loop so the newly added timer is taken into
1110 * account when the CPU goes back to idle and evaluates the timer
1111 * wheel for the next timer event.
1112 */
1113void wake_up_idle_cpu(int cpu)
1114{
1115 struct rq *rq = cpu_rq(cpu);
1116
1117 if (cpu == smp_processor_id())
1118 return;
1119
1120 /*
1121 * This is safe, as this function is called with the timer
1122 * wheel base lock of (cpu) held. When the CPU is on the way
1123 * to idle and has not yet set rq->curr to idle then it will
1124 * be serialized on the timer wheel base lock and take the new
1125 * timer into account automatically.
1126 */
1127 if (rq->curr != rq->idle)
1128 return;
1129
1130 /*
1131 * We can set TIF_RESCHED on the idle task of the other CPU
1132 * lockless. The worst case is that the other CPU runs the
1133 * idle task through an additional NOOP schedule()
1134 */
1135 set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
1136
1137 /* NEED_RESCHED must be visible before we test polling */
1138 smp_mb();
1139 if (!tsk_is_polling(rq->idle))
1140 smp_send_reschedule(cpu);
1141}
1142#endif
1143
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001144#else
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001145static void __resched_task(struct task_struct *p, int tif_bit)
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001146{
1147 assert_spin_locked(&task_rq(p)->lock);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001148 set_tsk_thread_flag(p, tif_bit);
Ingo Molnarc24d20d2007-07-09 18:51:59 +02001149}
1150#endif
1151
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001152#if BITS_PER_LONG == 32
1153# define WMULT_CONST (~0UL)
1154#else
1155# define WMULT_CONST (1UL << 32)
1156#endif
1157
1158#define WMULT_SHIFT 32
1159
Ingo Molnar194081e2007-08-09 11:16:51 +02001160/*
1161 * Shift right and round:
1162 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001163#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +02001164
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +02001165static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001166calc_delta_mine(unsigned long delta_exec, unsigned long weight,
1167 struct load_weight *lw)
1168{
1169 u64 tmp;
1170
1171 if (unlikely(!lw->inv_weight))
Ingo Molnar27d11722008-03-14 22:20:01 +01001172 lw->inv_weight = (WMULT_CONST-lw->weight/2) / (lw->weight+1);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001173
1174 tmp = (u64)delta_exec * weight;
1175 /*
1176 * Check whether we'd overflow the 64-bit multiplication:
1177 */
Ingo Molnar194081e2007-08-09 11:16:51 +02001178 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001179 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +02001180 WMULT_SHIFT/2);
1181 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +02001182 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001183
Ingo Molnarecf691d2007-08-02 17:41:40 +02001184 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001185}
1186
1187static inline unsigned long
1188calc_delta_fair(unsigned long delta_exec, struct load_weight *lw)
1189{
1190 return calc_delta_mine(delta_exec, NICE_0_LOAD, lw);
1191}
1192
Ingo Molnar10919852007-10-15 17:00:04 +02001193static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001194{
1195 lw->weight += inc;
Ingo Molnare89996a2008-03-14 23:48:28 +01001196 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001197}
1198
Ingo Molnar10919852007-10-15 17:00:04 +02001199static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001200{
1201 lw->weight -= dec;
Ingo Molnare89996a2008-03-14 23:48:28 +01001202 lw->inv_weight = 0;
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001203}
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001206 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1207 * of tasks with abnormal "nice" values across CPUs the contribution that
1208 * each task makes to its run queue's load is weighted according to its
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01001209 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
Peter Williams2dd73a42006-06-27 02:54:34 -07001210 * scaled version of the new time slice allocation that they receive on time
1211 * slice expiry etc.
1212 */
1213
Ingo Molnardd41f592007-07-09 18:51:59 +02001214#define WEIGHT_IDLEPRIO 2
1215#define WMULT_IDLEPRIO (1 << 31)
1216
1217/*
1218 * Nice levels are multiplicative, with a gentle 10% change for every
1219 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
1220 * nice 1, it will get ~10% less CPU time than another CPU-bound task
1221 * that remained on nice 0.
1222 *
1223 * The "10% effect" is relative and cumulative: from _any_ nice level,
1224 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +02001225 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
1226 * If a task goes up by ~10% and another task goes down by ~10% then
1227 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +02001228 */
1229static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001230 /* -20 */ 88761, 71755, 56483, 46273, 36291,
1231 /* -15 */ 29154, 23254, 18705, 14949, 11916,
1232 /* -10 */ 9548, 7620, 6100, 4904, 3906,
1233 /* -5 */ 3121, 2501, 1991, 1586, 1277,
1234 /* 0 */ 1024, 820, 655, 526, 423,
1235 /* 5 */ 335, 272, 215, 172, 137,
1236 /* 10 */ 110, 87, 70, 56, 45,
1237 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +02001238};
1239
Ingo Molnar5714d2d2007-07-16 09:46:31 +02001240/*
1241 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
1242 *
1243 * In cases where the weight does not change often, we can use the
1244 * precalculated inverse to speed up arithmetics by turning divisions
1245 * into multiplications:
1246 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001247static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +02001248 /* -20 */ 48388, 59856, 76040, 92818, 118348,
1249 /* -15 */ 147320, 184698, 229616, 287308, 360437,
1250 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
1251 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
1252 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
1253 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
1254 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
1255 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +02001256};
Peter Williams2dd73a42006-06-27 02:54:34 -07001257
Ingo Molnardd41f592007-07-09 18:51:59 +02001258static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
1259
1260/*
1261 * runqueue iterator, to support SMP load-balancing between different
1262 * scheduling classes, without having to expose their internal data
1263 * structures to the load-balancing proper:
1264 */
1265struct rq_iterator {
1266 void *arg;
1267 struct task_struct *(*start)(void *);
1268 struct task_struct *(*next)(void *);
1269};
1270
Peter Williamse1d14842007-10-24 18:23:51 +02001271#ifdef CONFIG_SMP
1272static unsigned long
1273balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
1274 unsigned long max_load_move, struct sched_domain *sd,
1275 enum cpu_idle_type idle, int *all_pinned,
1276 int *this_best_prio, struct rq_iterator *iterator);
1277
1278static int
1279iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
1280 struct sched_domain *sd, enum cpu_idle_type idle,
1281 struct rq_iterator *iterator);
Peter Williamse1d14842007-10-24 18:23:51 +02001282#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001283
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01001284#ifdef CONFIG_CGROUP_CPUACCT
1285static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
1286#else
1287static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
1288#endif
1289
Gregory Haskinse7693a32008-01-25 21:08:09 +01001290#ifdef CONFIG_SMP
1291static unsigned long source_load(int cpu, int type);
1292static unsigned long target_load(int cpu, int type);
1293static unsigned long cpu_avg_load_per_task(int cpu);
1294static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
1295#endif /* CONFIG_SMP */
1296
Ingo Molnardd41f592007-07-09 18:51:59 +02001297#include "sched_stats.h"
Ingo Molnardd41f592007-07-09 18:51:59 +02001298#include "sched_idletask.c"
Ingo Molnar5522d5d2007-10-15 17:00:12 +02001299#include "sched_fair.c"
1300#include "sched_rt.c"
Ingo Molnardd41f592007-07-09 18:51:59 +02001301#ifdef CONFIG_SCHED_DEBUG
1302# include "sched_debug.c"
1303#endif
1304
1305#define sched_class_highest (&rt_sched_class)
1306
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001307static inline void inc_load(struct rq *rq, const struct task_struct *p)
Ingo Molnar9c217242007-08-02 17:41:40 +02001308{
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001309 update_load_add(&rq->load, p->se.load.weight);
Ingo Molnar9c217242007-08-02 17:41:40 +02001310}
1311
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001312static inline void dec_load(struct rq *rq, const struct task_struct *p)
1313{
1314 update_load_sub(&rq->load, p->se.load.weight);
1315}
1316
1317static void inc_nr_running(struct task_struct *p, struct rq *rq)
1318{
1319 rq->nr_running++;
1320 inc_load(rq, p);
1321}
1322
1323static void dec_nr_running(struct task_struct *p, struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +02001324{
1325 rq->nr_running--;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001326 dec_load(rq, p);
Ingo Molnar9c217242007-08-02 17:41:40 +02001327}
1328
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001329static void set_load_weight(struct task_struct *p)
1330{
1331 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001332 p->se.load.weight = prio_to_weight[0] * 2;
1333 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
1334 return;
1335 }
1336
1337 /*
1338 * SCHED_IDLE tasks get minimal weight:
1339 */
1340 if (p->policy == SCHED_IDLE) {
1341 p->se.load.weight = WEIGHT_IDLEPRIO;
1342 p->se.load.inv_weight = WMULT_IDLEPRIO;
1343 return;
1344 }
1345
1346 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
1347 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +02001348}
1349
Ingo Molnar8159f872007-08-09 11:16:49 +02001350static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001351{
1352 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +02001353 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +02001354 p->se.on_rq = 1;
1355}
1356
Ingo Molnar69be72c2007-08-09 11:16:49 +02001357static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +02001358{
Ingo Molnarf02231e2007-08-09 11:16:48 +02001359 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +02001360 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001361}
1362
1363/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001364 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001365 */
Ingo Molnar14531182007-07-09 18:51:59 +02001366static inline int __normal_prio(struct task_struct *p)
1367{
Ingo Molnardd41f592007-07-09 18:51:59 +02001368 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001369}
1370
1371/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001372 * Calculate the expected normal priority: i.e. priority
1373 * without taking RT-inheritance into account. Might be
1374 * boosted by interactivity modifiers. Changes upon fork,
1375 * setprio syscalls, and whenever the interactivity
1376 * estimator recalculates.
1377 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001378static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001379{
1380 int prio;
1381
Ingo Molnare05606d2007-07-09 18:51:59 +02001382 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001383 prio = MAX_RT_PRIO-1 - p->rt_priority;
1384 else
1385 prio = __normal_prio(p);
1386 return prio;
1387}
1388
1389/*
1390 * Calculate the current priority, i.e. the priority
1391 * taken into account by the scheduler. This value might
1392 * be boosted by RT tasks, or might be boosted by
1393 * interactivity modifiers. Will be RT if the task got
1394 * RT-boosted. If not then it returns p->normal_prio.
1395 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001396static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001397{
1398 p->normal_prio = normal_prio(p);
1399 /*
1400 * If we are RT tasks or we were boosted to RT priority,
1401 * keep the priority unchanged. Otherwise, update priority
1402 * to the normal priority:
1403 */
1404 if (!rt_prio(p->prio))
1405 return p->normal_prio;
1406 return p->prio;
1407}
1408
1409/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001410 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001412static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001414 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001415 rq->nr_uninterruptible--;
1416
Ingo Molnar8159f872007-08-09 11:16:49 +02001417 enqueue_task(rq, p, wakeup);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001418 inc_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419}
1420
1421/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 * deactivate_task - remove a task from the runqueue.
1423 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +02001424static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001426 if (task_contributes_to_load(p))
Ingo Molnardd41f592007-07-09 18:51:59 +02001427 rq->nr_uninterruptible++;
1428
Ingo Molnar69be72c2007-08-09 11:16:49 +02001429 dequeue_task(rq, p, sleep);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001430 dec_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433/**
1434 * task_curr - is this task currently executing on a CPU?
1435 * @p: the task in question.
1436 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001437inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
1439 return cpu_curr(task_cpu(p)) == p;
1440}
1441
Peter Williams2dd73a42006-06-27 02:54:34 -07001442/* Used instead of source_load when we know the type == 0 */
1443unsigned long weighted_cpuload(const int cpu)
1444{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001445 return cpu_rq(cpu)->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02001446}
1447
1448static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1449{
Peter Zijlstra6f505b12008-01-25 21:08:30 +01001450 set_task_rq(p, cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001451#ifdef CONFIG_SMP
Dmitry Adamushkoce96b5a2007-11-15 20:57:40 +01001452 /*
1453 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1454 * successfuly executed on another CPU. We must ensure that updates of
1455 * per-task data have been completed by this moment.
1456 */
1457 smp_wmb();
Ingo Molnardd41f592007-07-09 18:51:59 +02001458 task_thread_info(p)->cpu = cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02001459#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07001460}
1461
Steven Rostedtcb469842008-01-25 21:08:22 +01001462static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1463 const struct sched_class *prev_class,
1464 int oldprio, int running)
1465{
1466 if (prev_class != p->sched_class) {
1467 if (prev_class->switched_from)
1468 prev_class->switched_from(rq, p, running);
1469 p->sched_class->switched_to(rq, p, running);
1470 } else
1471 p->sched_class->prio_changed(rq, p, oldprio, running);
1472}
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02001475
Ingo Molnarcc367732007-10-15 17:00:18 +02001476/*
1477 * Is this task likely cache-hot:
1478 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001479static int
Ingo Molnarcc367732007-10-15 17:00:18 +02001480task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
1481{
1482 s64 delta;
1483
Ingo Molnarf540a602008-03-15 17:10:34 +01001484 /*
1485 * Buddy candidates are cache hot:
1486 */
1487 if (&p->se == cfs_rq_of(&p->se)->next)
1488 return 1;
1489
Ingo Molnarcc367732007-10-15 17:00:18 +02001490 if (p->sched_class != &fair_sched_class)
1491 return 0;
1492
Ingo Molnar6bc16652007-10-15 17:00:18 +02001493 if (sysctl_sched_migration_cost == -1)
1494 return 1;
1495 if (sysctl_sched_migration_cost == 0)
1496 return 0;
1497
Ingo Molnarcc367732007-10-15 17:00:18 +02001498 delta = now - p->se.exec_start;
1499
1500 return delta < (s64)sysctl_sched_migration_cost;
1501}
1502
1503
Ingo Molnardd41f592007-07-09 18:51:59 +02001504void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001505{
Ingo Molnardd41f592007-07-09 18:51:59 +02001506 int old_cpu = task_cpu(p);
1507 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001508 struct cfs_rq *old_cfsrq = task_cfs_rq(p),
1509 *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +02001510 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001511
1512 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001513
1514#ifdef CONFIG_SCHEDSTATS
1515 if (p->se.wait_start)
1516 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001517 if (p->se.sleep_start)
1518 p->se.sleep_start -= clock_offset;
1519 if (p->se.block_start)
1520 p->se.block_start -= clock_offset;
Ingo Molnarcc367732007-10-15 17:00:18 +02001521 if (old_cpu != new_cpu) {
1522 schedstat_inc(p, se.nr_migrations);
1523 if (task_hot(p, old_rq->clock, NULL))
1524 schedstat_inc(p, se.nr_forced2_migrations);
1525 }
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001526#endif
Srivatsa Vaddagiri2830cf82007-10-15 17:00:12 +02001527 p->se.vruntime -= old_cfsrq->min_vruntime -
1528 new_cfsrq->min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +02001529
1530 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001531}
1532
Ingo Molnar70b97a72006-07-03 00:25:42 -07001533struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Ingo Molnar36c8b582006-07-03 00:25:41 -07001536 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 int dest_cpu;
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001540};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542/*
1543 * The task's runqueue lock must be held.
1544 * Returns true if you have to wait for migration thread.
1545 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001546static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07001547migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001549 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 /*
1552 * If the task is not on a runqueue (and not running), then
1553 * it is sufficient to simply update the task's cpu field.
1554 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001555 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 set_task_cpu(p, dest_cpu);
1557 return 0;
1558 }
1559
1560 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 req->task = p;
1562 req->dest_cpu = dest_cpu;
1563 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07001564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return 1;
1566}
1567
1568/*
1569 * wait_task_inactive - wait for a thread to unschedule.
1570 *
1571 * The caller must ensure that the task *will* unschedule sometime soon,
1572 * else this function might spin for a *long* time. This function can't
1573 * be called with interrupts off, or it may introduce deadlock with
1574 * smp_call_function() if an IPI is sent by the same process we are
1575 * waiting to become inactive.
1576 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001577void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
1579 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001580 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001581 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Andi Kleen3a5c3592007-10-15 17:00:14 +02001583 for (;;) {
1584 /*
1585 * We do the initial early heuristics without holding
1586 * any task-queue locks at all. We'll only try to get
1587 * the runqueue lock when things look like they will
1588 * work out!
1589 */
1590 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001591
Andi Kleen3a5c3592007-10-15 17:00:14 +02001592 /*
1593 * If the task is actively running on another CPU
1594 * still, just relax and busy-wait without holding
1595 * any locks.
1596 *
1597 * NOTE! Since we don't hold any locks, it's not
1598 * even sure that "rq" stays as the right runqueue!
1599 * But we don't care, since "task_running()" will
1600 * return false if the runqueue has changed and p
1601 * is actually now running somewhere else!
1602 */
1603 while (task_running(rq, p))
1604 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001605
Andi Kleen3a5c3592007-10-15 17:00:14 +02001606 /*
1607 * Ok, time to look more closely! We need the rq
1608 * lock now, to be *sure*. If we're wrong, we'll
1609 * just go back and repeat.
1610 */
1611 rq = task_rq_lock(p, &flags);
1612 running = task_running(rq, p);
1613 on_rq = p->se.on_rq;
1614 task_rq_unlock(rq, &flags);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001615
Andi Kleen3a5c3592007-10-15 17:00:14 +02001616 /*
1617 * Was it really running after all now that we
1618 * checked with the proper locks actually held?
1619 *
1620 * Oops. Go back and try again..
1621 */
1622 if (unlikely(running)) {
1623 cpu_relax();
1624 continue;
1625 }
1626
1627 /*
1628 * It's not enough that it's not actively running,
1629 * it must be off the runqueue _entirely_, and not
1630 * preempted!
1631 *
1632 * So if it wa still runnable (but just not actively
1633 * running right now), it's preempted, and we should
1634 * yield - it could be a while.
1635 */
1636 if (unlikely(on_rq)) {
1637 schedule_timeout_uninterruptible(1);
1638 continue;
1639 }
1640
1641 /*
1642 * Ahh, all good. It wasn't running, and it wasn't
1643 * runnable, which means that it will never become
1644 * running in the future either. We're all done!
1645 */
1646 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
1649
1650/***
1651 * kick_process - kick a running thread to enter/exit the kernel
1652 * @p: the to-be-kicked thread
1653 *
1654 * Cause a process which is running on another CPU to enter
1655 * kernel-mode, without any delay. (to get signals handled.)
1656 *
1657 * NOTE: this function doesnt have to take the runqueue lock,
1658 * because all it wants to ensure is that the remote task enters
1659 * the kernel. If the IPI races and the task has been migrated
1660 * to another CPU then no harm is done and the purpose has been
1661 * achieved as well.
1662 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001663void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
1665 int cpu;
1666
1667 preempt_disable();
1668 cpu = task_cpu(p);
1669 if ((cpu != smp_processor_id()) && task_curr(p))
1670 smp_send_reschedule(cpu);
1671 preempt_enable();
1672}
1673
1674/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001675 * Return a low guess at the load of a migration-source cpu weighted
1676 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 *
1678 * We want to under-estimate the load of migration sources, to
1679 * balance conservatively.
1680 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001681static unsigned long source_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08001682{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001683 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001684 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001685
Peter Williams2dd73a42006-06-27 02:54:34 -07001686 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001687 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001688
Ingo Molnardd41f592007-07-09 18:51:59 +02001689 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690}
1691
1692/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001693 * Return a high guess at the load of a migration-target cpu weighted
1694 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001696static unsigned long target_load(int cpu, int type)
Con Kolivasb9104722005-11-08 21:38:55 -08001697{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001698 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001699 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001700
Peter Williams2dd73a42006-06-27 02:54:34 -07001701 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001702 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001703
Ingo Molnardd41f592007-07-09 18:51:59 +02001704 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07001705}
1706
1707/*
1708 * Return the average load per task on the cpu's run queue
1709 */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001710static unsigned long cpu_avg_load_per_task(int cpu)
Peter Williams2dd73a42006-06-27 02:54:34 -07001711{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001712 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001713 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07001714 unsigned long n = rq->nr_running;
1715
Ingo Molnardd41f592007-07-09 18:51:59 +02001716 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
Nick Piggin147cbb42005-06-25 14:57:19 -07001719/*
1720 * find_idlest_group finds and returns the least busy CPU group within the
1721 * domain.
1722 */
1723static struct sched_group *
1724find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
1725{
1726 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
1727 unsigned long min_load = ULONG_MAX, this_load = 0;
1728 int load_idx = sd->forkexec_idx;
1729 int imbalance = 100 + (sd->imbalance_pct-100)/2;
1730
1731 do {
1732 unsigned long load, avg_load;
1733 int local_group;
1734 int i;
1735
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001736 /* Skip over this group if it has no CPUs allowed */
1737 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
Andi Kleen3a5c3592007-10-15 17:00:14 +02001738 continue;
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001739
Nick Piggin147cbb42005-06-25 14:57:19 -07001740 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07001741
1742 /* Tally up the load of all CPUs in the group */
1743 avg_load = 0;
1744
1745 for_each_cpu_mask(i, group->cpumask) {
1746 /* Bias balancing toward cpus of our domain */
1747 if (local_group)
1748 load = source_load(i, load_idx);
1749 else
1750 load = target_load(i, load_idx);
1751
1752 avg_load += load;
1753 }
1754
1755 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07001756 avg_load = sg_div_cpu_power(group,
1757 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07001758
1759 if (local_group) {
1760 this_load = avg_load;
1761 this = group;
1762 } else if (avg_load < min_load) {
1763 min_load = avg_load;
1764 idlest = group;
1765 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02001766 } while (group = group->next, group != sd->groups);
Nick Piggin147cbb42005-06-25 14:57:19 -07001767
1768 if (!idlest || 100*this_load < imbalance*min_load)
1769 return NULL;
1770 return idlest;
1771}
1772
1773/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07001774 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07001775 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07001776static int
1777find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
Nick Piggin147cbb42005-06-25 14:57:19 -07001778{
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001779 cpumask_t tmp;
Nick Piggin147cbb42005-06-25 14:57:19 -07001780 unsigned long load, min_load = ULONG_MAX;
1781 int idlest = -1;
1782 int i;
1783
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001784 /* Traverse only the allowed CPUs */
1785 cpus_and(tmp, group->cpumask, p->cpus_allowed);
1786
1787 for_each_cpu_mask(i, tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07001788 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07001789
1790 if (load < min_load || (load == min_load && i == this_cpu)) {
1791 min_load = load;
1792 idlest = i;
1793 }
1794 }
1795
1796 return idlest;
1797}
1798
Nick Piggin476d1392005-06-25 14:57:29 -07001799/*
1800 * sched_balance_self: balance the current task (running on cpu) in domains
1801 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
1802 * SD_BALANCE_EXEC.
1803 *
1804 * Balance, ie. select the least loaded group.
1805 *
1806 * Returns the target CPU number, or the same CPU if no balancing is needed.
1807 *
1808 * preempt must be disabled.
1809 */
1810static int sched_balance_self(int cpu, int flag)
1811{
1812 struct task_struct *t = current;
1813 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07001814
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001815 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02001816 /*
1817 * If power savings logic is enabled for a domain, stop there.
1818 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07001819 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
1820 break;
Nick Piggin476d1392005-06-25 14:57:29 -07001821 if (tmp->flags & flag)
1822 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001823 }
Nick Piggin476d1392005-06-25 14:57:29 -07001824
1825 while (sd) {
1826 cpumask_t span;
1827 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001828 int new_cpu, weight;
1829
1830 if (!(sd->flags & flag)) {
1831 sd = sd->child;
1832 continue;
1833 }
Nick Piggin476d1392005-06-25 14:57:29 -07001834
1835 span = sd->span;
1836 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001837 if (!group) {
1838 sd = sd->child;
1839 continue;
1840 }
Nick Piggin476d1392005-06-25 14:57:29 -07001841
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001842 new_cpu = find_idlest_cpu(group, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001843 if (new_cpu == -1 || new_cpu == cpu) {
1844 /* Now try balancing at a lower domain level of cpu */
1845 sd = sd->child;
1846 continue;
1847 }
Nick Piggin476d1392005-06-25 14:57:29 -07001848
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001849 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07001850 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07001851 sd = NULL;
1852 weight = cpus_weight(span);
1853 for_each_domain(cpu, tmp) {
1854 if (weight <= cpus_weight(tmp->span))
1855 break;
1856 if (tmp->flags & flag)
1857 sd = tmp;
1858 }
1859 /* while loop will break here if sd == NULL */
1860 }
1861
1862 return cpu;
1863}
1864
1865#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867/***
1868 * try_to_wake_up - wake up a thread
1869 * @p: the to-be-woken-up thread
1870 * @state: the mask of task states that can be woken
1871 * @sync: do a synchronous wakeup?
1872 *
1873 * Put it on the run-queue if it's not already there. The "current"
1874 * thread is always on the run-queue (except when the actual
1875 * re-schedule is in progress), and as such you're allowed to do
1876 * the simpler "current->state = TASK_RUNNING" to mark yourself
1877 * runnable without the overhead of this.
1878 *
1879 * returns failure only if the task is already active.
1880 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001881static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
Ingo Molnarcc367732007-10-15 17:00:18 +02001883 int cpu, orig_cpu, this_cpu, success = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 unsigned long flags;
1885 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001886 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Linus Torvalds04e2f172008-02-23 18:05:03 -08001888 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 rq = task_rq_lock(p, &flags);
1890 old_state = p->state;
1891 if (!(old_state & state))
1892 goto out;
1893
Ingo Molnardd41f592007-07-09 18:51:59 +02001894 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 goto out_running;
1896
1897 cpu = task_cpu(p);
Ingo Molnarcc367732007-10-15 17:00:18 +02001898 orig_cpu = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 this_cpu = smp_processor_id();
1900
1901#ifdef CONFIG_SMP
1902 if (unlikely(task_running(rq, p)))
1903 goto out_activate;
1904
Dmitry Adamushko5d2f5a62008-01-25 21:08:21 +01001905 cpu = p->sched_class->select_task_rq(p, sync);
1906 if (cpu != orig_cpu) {
1907 set_task_cpu(p, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 task_rq_unlock(rq, &flags);
1909 /* might preempt at this point */
1910 rq = task_rq_lock(p, &flags);
1911 old_state = p->state;
1912 if (!(old_state & state))
1913 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02001914 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 goto out_running;
1916
1917 this_cpu = smp_processor_id();
1918 cpu = task_cpu(p);
1919 }
1920
Gregory Haskinse7693a32008-01-25 21:08:09 +01001921#ifdef CONFIG_SCHEDSTATS
1922 schedstat_inc(rq, ttwu_count);
1923 if (cpu == this_cpu)
1924 schedstat_inc(rq, ttwu_local);
1925 else {
1926 struct sched_domain *sd;
1927 for_each_domain(this_cpu, sd) {
1928 if (cpu_isset(cpu, sd->span)) {
1929 schedstat_inc(sd, ttwu_wake_remote);
1930 break;
1931 }
1932 }
1933 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01001934#endif
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936out_activate:
1937#endif /* CONFIG_SMP */
Ingo Molnarcc367732007-10-15 17:00:18 +02001938 schedstat_inc(p, se.nr_wakeups);
1939 if (sync)
1940 schedstat_inc(p, se.nr_wakeups_sync);
1941 if (orig_cpu != cpu)
1942 schedstat_inc(p, se.nr_wakeups_migrate);
1943 if (cpu == this_cpu)
1944 schedstat_inc(p, se.nr_wakeups_local);
1945 else
1946 schedstat_inc(p, se.nr_wakeups_remote);
Ingo Molnar2daa3572007-08-09 11:16:51 +02001947 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02001948 activate_task(rq, p, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 success = 1;
1950
1951out_running:
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01001952 check_preempt_curr(rq, p);
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 p->state = TASK_RUNNING;
Steven Rostedt9a897c52008-01-25 21:08:22 +01001955#ifdef CONFIG_SMP
1956 if (p->sched_class->task_wake_up)
1957 p->sched_class->task_wake_up(rq, p);
1958#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959out:
1960 task_rq_unlock(rq, &flags);
1961
1962 return success;
1963}
1964
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001965int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05001967 return try_to_wake_up(p, TASK_ALL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969EXPORT_SYMBOL(wake_up_process);
1970
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08001971int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
1973 return try_to_wake_up(p, state, 0);
1974}
1975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976/*
1977 * Perform scheduler related setup for a newly forked process p.
1978 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02001979 *
1980 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001982static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
Ingo Molnardd41f592007-07-09 18:51:59 +02001984 p->se.exec_start = 0;
1985 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02001986 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01001987 p->se.last_wakeup = 0;
1988 p->se.avg_overlap = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001989
1990#ifdef CONFIG_SCHEDSTATS
1991 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02001992 p->se.sum_sleep_runtime = 0;
1993 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02001994 p->se.block_start = 0;
1995 p->se.sleep_max = 0;
1996 p->se.block_max = 0;
1997 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02001998 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02001999 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002000#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002001
Peter Zijlstrafa717062008-01-25 21:08:27 +01002002 INIT_LIST_HEAD(&p->rt.run_list);
Ingo Molnardd41f592007-07-09 18:51:59 +02002003 p->se.on_rq = 0;
Nick Piggin476d1392005-06-25 14:57:29 -07002004
Avi Kivitye107be32007-07-26 13:40:43 +02002005#ifdef CONFIG_PREEMPT_NOTIFIERS
2006 INIT_HLIST_HEAD(&p->preempt_notifiers);
2007#endif
2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 /*
2010 * We mark the process as running here, but have not actually
2011 * inserted it onto the runqueue yet. This guarantees that
2012 * nobody will actually run it, and a signal or other external
2013 * event cannot wake it up and insert it on the runqueue either.
2014 */
2015 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02002016}
2017
2018/*
2019 * fork()/clone()-time setup:
2020 */
2021void sched_fork(struct task_struct *p, int clone_flags)
2022{
2023 int cpu = get_cpu();
2024
2025 __sched_fork(p);
2026
2027#ifdef CONFIG_SMP
2028 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
2029#endif
Ingo Molnar02e4bac2007-10-15 17:00:11 +02002030 set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07002031
2032 /*
2033 * Make sure we do not leak PI boosting priority to the child:
2034 */
2035 p->prio = current->normal_prio;
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02002036 if (!rt_prio(p->prio))
2037 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07002038
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002039#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02002040 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07002041 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08002043#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07002044 p->oncpu = 0;
2045#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07002047 /* Want to start with kernel preemption disabled. */
Al Viroa1261f52005-11-13 16:06:55 -08002048 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002050 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
2052
2053/*
2054 * wake_up_new_task - wake up a newly created task for the first time.
2055 *
2056 * This function will do some initial scheduler statistics housekeeping
2057 * that must be done for every newly created context, then puts the task
2058 * on the runqueue and wakes it.
2059 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002060void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
2062 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002063 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnara8e504d2007-08-09 11:16:47 +02002067 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
2069 p->prio = effective_prio(p);
2070
Srivatsa Vaddagirib9dca1e2007-10-17 16:55:11 +02002071 if (!p->sched_class->task_new || !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002072 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002075 * Let the scheduling class do new task startup
2076 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02002078 p->sched_class->task_new(rq, p);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01002079 inc_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002081 check_preempt_curr(rq, p);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002082#ifdef CONFIG_SMP
2083 if (p->sched_class->task_wake_up)
2084 p->sched_class->task_wake_up(rq, p);
2085#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002086 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087}
2088
Avi Kivitye107be32007-07-26 13:40:43 +02002089#ifdef CONFIG_PREEMPT_NOTIFIERS
2090
2091/**
Randy Dunlap421cee22007-07-31 00:37:50 -07002092 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
2093 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02002094 */
2095void preempt_notifier_register(struct preempt_notifier *notifier)
2096{
2097 hlist_add_head(&notifier->link, &current->preempt_notifiers);
2098}
2099EXPORT_SYMBOL_GPL(preempt_notifier_register);
2100
2101/**
2102 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07002103 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02002104 *
2105 * This is safe to call from within a preemption notifier.
2106 */
2107void preempt_notifier_unregister(struct preempt_notifier *notifier)
2108{
2109 hlist_del(&notifier->link);
2110}
2111EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
2112
2113static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2114{
2115 struct preempt_notifier *notifier;
2116 struct hlist_node *node;
2117
2118 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2119 notifier->ops->sched_in(notifier, raw_smp_processor_id());
2120}
2121
2122static void
2123fire_sched_out_preempt_notifiers(struct task_struct *curr,
2124 struct task_struct *next)
2125{
2126 struct preempt_notifier *notifier;
2127 struct hlist_node *node;
2128
2129 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
2130 notifier->ops->sched_out(notifier, next);
2131}
2132
2133#else
2134
2135static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
2136{
2137}
2138
2139static void
2140fire_sched_out_preempt_notifiers(struct task_struct *curr,
2141 struct task_struct *next)
2142{
2143}
2144
2145#endif
2146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147/**
Nick Piggin4866cde2005-06-25 14:57:23 -07002148 * prepare_task_switch - prepare to switch tasks
2149 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07002150 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07002151 * @next: the task we are going to switch to.
2152 *
2153 * This is called with the rq lock held and interrupts off. It must
2154 * be paired with a subsequent finish_task_switch after the context
2155 * switch.
2156 *
2157 * prepare_task_switch sets up locking and calls architecture specific
2158 * hooks.
2159 */
Avi Kivitye107be32007-07-26 13:40:43 +02002160static inline void
2161prepare_task_switch(struct rq *rq, struct task_struct *prev,
2162 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07002163{
Avi Kivitye107be32007-07-26 13:40:43 +02002164 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07002165 prepare_lock_switch(rq, next);
2166 prepare_arch_switch(next);
2167}
2168
2169/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04002171 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 * @prev: the thread we just switched away from.
2173 *
Nick Piggin4866cde2005-06-25 14:57:23 -07002174 * finish_task_switch must be called after the context switch, paired
2175 * with a prepare_task_switch call before the context switch.
2176 * finish_task_switch will reconcile locking set up by prepare_task_switch,
2177 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 *
2179 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002180 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 * with the lock held can cause deadlocks; see schedule() for
2182 * details.)
2183 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02002184static void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 __releases(rq->lock)
2186{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002188 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 rq->prev_mm = NULL;
2191
2192 /*
2193 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002194 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002195 * schedule one last time. The schedule call will never return, and
2196 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002197 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 * still held, otherwise prev could be scheduled on another cpu, die
2199 * there before we look at prev->state, and then the reference would
2200 * be dropped twice.
2201 * Manfred Spraul <manfred@colorfullife.com>
2202 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07002203 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07002204 finish_arch_switch(prev);
2205 finish_lock_switch(rq, prev);
Steven Rostedt9a897c52008-01-25 21:08:22 +01002206#ifdef CONFIG_SMP
2207 if (current->sched_class->post_schedule)
2208 current->sched_class->post_schedule(rq);
2209#endif
Steven Rostedte8fa1362008-01-25 21:08:05 +01002210
Avi Kivitye107be32007-07-26 13:40:43 +02002211 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 if (mm)
2213 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07002214 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08002215 /*
2216 * Remove function-return probe instances associated with this
2217 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02002218 */
bibo maoc6fd91f2006-03-26 01:38:20 -08002219 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08002221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222}
2223
2224/**
2225 * schedule_tail - first thing a freshly forked thread must call.
2226 * @prev: the thread we just switched away from.
2227 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002228asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 __releases(rq->lock)
2230{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002231 struct rq *rq = this_rq();
2232
Nick Piggin4866cde2005-06-25 14:57:23 -07002233 finish_task_switch(rq, prev);
2234#ifdef __ARCH_WANT_UNLOCKED_CTXSW
2235 /* In this case, finish_task_switch does not reenable preemption */
2236 preempt_enable();
2237#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002239 put_user(task_pid_vnr(current), current->set_child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
2241
2242/*
2243 * context_switch - switch to the new MM and the new
2244 * thread's register state.
2245 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002246static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07002247context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07002248 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
Ingo Molnardd41f592007-07-09 18:51:59 +02002250 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Avi Kivitye107be32007-07-26 13:40:43 +02002252 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02002253 mm = next->mm;
2254 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01002255 /*
2256 * For paravirt, this is coupled with an exit in switch_to to
2257 * combine the page table reload and the switch backend into
2258 * one hypercall.
2259 */
2260 arch_enter_lazy_cpu_mode();
2261
Ingo Molnardd41f592007-07-09 18:51:59 +02002262 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 next->active_mm = oldmm;
2264 atomic_inc(&oldmm->mm_count);
2265 enter_lazy_tlb(oldmm, next);
2266 } else
2267 switch_mm(oldmm, mm, next);
2268
Ingo Molnardd41f592007-07-09 18:51:59 +02002269 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 rq->prev_mm = oldmm;
2272 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002273 /*
2274 * Since the runqueue lock will be released by the next
2275 * task (which is an invalid locking op but in the case
2276 * of the scheduler it's an obvious special-case), so we
2277 * do an early lockdep release here:
2278 */
2279#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07002280 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07002281#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
2283 /* Here we just switch the register state and the stack. */
2284 switch_to(prev, next, prev);
2285
Ingo Molnardd41f592007-07-09 18:51:59 +02002286 barrier();
2287 /*
2288 * this_rq must be evaluated again because prev may have moved
2289 * CPUs since it called schedule(), thus the 'rq' on its stack
2290 * frame will be invalid.
2291 */
2292 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293}
2294
2295/*
2296 * nr_running, nr_uninterruptible and nr_context_switches:
2297 *
2298 * externally visible scheduler statistics: current number of runnable
2299 * threads, current number of uninterruptible-sleeping threads, total
2300 * number of context switches performed since bootup.
2301 */
2302unsigned long nr_running(void)
2303{
2304 unsigned long i, sum = 0;
2305
2306 for_each_online_cpu(i)
2307 sum += cpu_rq(i)->nr_running;
2308
2309 return sum;
2310}
2311
2312unsigned long nr_uninterruptible(void)
2313{
2314 unsigned long i, sum = 0;
2315
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002316 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 sum += cpu_rq(i)->nr_uninterruptible;
2318
2319 /*
2320 * Since we read the counters lockless, it might be slightly
2321 * inaccurate. Do not allow it to go below zero though:
2322 */
2323 if (unlikely((long)sum < 0))
2324 sum = 0;
2325
2326 return sum;
2327}
2328
2329unsigned long long nr_context_switches(void)
2330{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07002331 int i;
2332 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002334 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 sum += cpu_rq(i)->nr_switches;
2336
2337 return sum;
2338}
2339
2340unsigned long nr_iowait(void)
2341{
2342 unsigned long i, sum = 0;
2343
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08002344 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 sum += atomic_read(&cpu_rq(i)->nr_iowait);
2346
2347 return sum;
2348}
2349
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08002350unsigned long nr_active(void)
2351{
2352 unsigned long i, running = 0, uninterruptible = 0;
2353
2354 for_each_online_cpu(i) {
2355 running += cpu_rq(i)->nr_running;
2356 uninterruptible += cpu_rq(i)->nr_uninterruptible;
2357 }
2358
2359 if (unlikely((long)uninterruptible < 0))
2360 uninterruptible = 0;
2361
2362 return running + uninterruptible;
2363}
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365/*
Ingo Molnardd41f592007-07-09 18:51:59 +02002366 * Update rq->cpu_load[] statistics. This function is usually called every
2367 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07002368 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002369static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002370{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02002371 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02002372 int i, scale;
2373
2374 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02002375
2376 /* Update our load: */
2377 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2378 unsigned long old_load, new_load;
2379
2380 /* scale is effectively 1 << i now, and >> i divides by scale */
2381
2382 old_load = this_rq->cpu_load[i];
2383 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02002384 /*
2385 * Round up the averaging division if load is increasing. This
2386 * prevents us from getting stuck on 9 if the load is 10, for
2387 * example.
2388 */
2389 if (new_load > old_load)
2390 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002391 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2392 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002393}
2394
Ingo Molnardd41f592007-07-09 18:51:59 +02002395#ifdef CONFIG_SMP
2396
Ingo Molnar48f24c42006-07-03 00:25:40 -07002397/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 * double_rq_lock - safely lock two runqueues
2399 *
2400 * Note this does not disable interrupts like task_rq_lock,
2401 * you need to do so manually before calling.
2402 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002403static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 __acquires(rq1->lock)
2405 __acquires(rq2->lock)
2406{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002407 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 if (rq1 == rq2) {
2409 spin_lock(&rq1->lock);
2410 __acquire(rq2->lock); /* Fake it out ;) */
2411 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002412 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 spin_lock(&rq1->lock);
2414 spin_lock(&rq2->lock);
2415 } else {
2416 spin_lock(&rq2->lock);
2417 spin_lock(&rq1->lock);
2418 }
2419 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002420 update_rq_clock(rq1);
2421 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422}
2423
2424/*
2425 * double_rq_unlock - safely unlock two runqueues
2426 *
2427 * Note this does not restore interrupts like task_rq_unlock,
2428 * you need to do so manually after calling.
2429 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002430static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 __releases(rq1->lock)
2432 __releases(rq2->lock)
2433{
2434 spin_unlock(&rq1->lock);
2435 if (rq1 != rq2)
2436 spin_unlock(&rq2->lock);
2437 else
2438 __release(rq2->lock);
2439}
2440
2441/*
2442 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2443 */
Steven Rostedte8fa1362008-01-25 21:08:05 +01002444static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 __releases(this_rq->lock)
2446 __acquires(busiest->lock)
2447 __acquires(this_rq->lock)
2448{
Steven Rostedte8fa1362008-01-25 21:08:05 +01002449 int ret = 0;
2450
Kirill Korotaev054b9102006-12-10 02:20:11 -08002451 if (unlikely(!irqs_disabled())) {
2452 /* printk() doesn't work good under rq->lock */
2453 spin_unlock(&this_rq->lock);
2454 BUG_ON(1);
2455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002457 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 spin_unlock(&this_rq->lock);
2459 spin_lock(&busiest->lock);
2460 spin_lock(&this_rq->lock);
Steven Rostedte8fa1362008-01-25 21:08:05 +01002461 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 } else
2463 spin_lock(&busiest->lock);
2464 }
Steven Rostedte8fa1362008-01-25 21:08:05 +01002465 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466}
2467
2468/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 * If dest_cpu is allowed for this process, migrate the task to it.
2470 * This is accomplished by forcing the cpu_allowed mask to only
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002471 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 * the cpu_allowed mask is restored.
2473 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002474static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002476 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002478 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
2480 rq = task_rq_lock(p, &flags);
2481 if (!cpu_isset(dest_cpu, p->cpus_allowed)
2482 || unlikely(cpu_is_offline(dest_cpu)))
2483 goto out;
2484
2485 /* force the process onto the specified CPU */
2486 if (migrate_task(p, dest_cpu, &req)) {
2487 /* Need to wait for migration thread (might exit: take ref). */
2488 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07002489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 get_task_struct(mt);
2491 task_rq_unlock(rq, &flags);
2492 wake_up_process(mt);
2493 put_task_struct(mt);
2494 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07002495
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 return;
2497 }
2498out:
2499 task_rq_unlock(rq, &flags);
2500}
2501
2502/*
Nick Piggin476d1392005-06-25 14:57:29 -07002503 * sched_exec - execve() is a valuable balancing opportunity, because at
2504 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 */
2506void sched_exec(void)
2507{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002509 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002511 if (new_cpu != this_cpu)
2512 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514
2515/*
2516 * pull_task - move a task from a remote runqueue to the local runqueue.
2517 * Both runqueues must be locked.
2518 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002519static void pull_task(struct rq *src_rq, struct task_struct *p,
2520 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02002522 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002524 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 /*
2526 * Note that idle threads have a prio of MAX_PRIO, for this test
2527 * to be always true for them.
2528 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002529 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530}
2531
2532/*
2533 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
2534 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002535static
Ingo Molnar70b97a72006-07-03 00:25:42 -07002536int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002537 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002538 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539{
2540 /*
2541 * We do not migrate tasks that are:
2542 * 1) running (obviously), or
2543 * 2) cannot be migrated to this CPU due to cpus_allowed, or
2544 * 3) are cache-hot on their current CPU.
2545 */
Ingo Molnarcc367732007-10-15 17:00:18 +02002546 if (!cpu_isset(this_cpu, p->cpus_allowed)) {
2547 schedstat_inc(p, se.nr_failed_migrations_affine);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002549 }
Nick Piggin81026792005-06-25 14:57:07 -07002550 *all_pinned = 0;
2551
Ingo Molnarcc367732007-10-15 17:00:18 +02002552 if (task_running(rq, p)) {
2553 schedstat_inc(p, se.nr_failed_migrations_running);
Nick Piggin81026792005-06-25 14:57:07 -07002554 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Ingo Molnarda84d962007-10-15 17:00:18 +02002557 /*
2558 * Aggressive migration if:
2559 * 1) task is cache cold, or
2560 * 2) too many balance attempts have failed.
2561 */
2562
Ingo Molnar6bc16652007-10-15 17:00:18 +02002563 if (!task_hot(p, rq->clock, sd) ||
2564 sd->nr_balance_failed > sd->cache_nice_tries) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002565#ifdef CONFIG_SCHEDSTATS
Ingo Molnarcc367732007-10-15 17:00:18 +02002566 if (task_hot(p, rq->clock, sd)) {
Ingo Molnarda84d962007-10-15 17:00:18 +02002567 schedstat_inc(sd, lb_hot_gained[idle]);
Ingo Molnarcc367732007-10-15 17:00:18 +02002568 schedstat_inc(p, se.nr_forced_migrations);
2569 }
Ingo Molnarda84d962007-10-15 17:00:18 +02002570#endif
2571 return 1;
2572 }
2573
Ingo Molnarcc367732007-10-15 17:00:18 +02002574 if (task_hot(p, rq->clock, sd)) {
2575 schedstat_inc(p, se.nr_failed_migrations_hot);
Ingo Molnarda84d962007-10-15 17:00:18 +02002576 return 0;
Ingo Molnarcc367732007-10-15 17:00:18 +02002577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 return 1;
2579}
2580
Peter Williamse1d14842007-10-24 18:23:51 +02002581static unsigned long
2582balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
2583 unsigned long max_load_move, struct sched_domain *sd,
2584 enum cpu_idle_type idle, int *all_pinned,
2585 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02002586{
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002587 int loops = 0, pulled = 0, pinned = 0, skip_for_load;
Ingo Molnardd41f592007-07-09 18:51:59 +02002588 struct task_struct *p;
2589 long rem_load_move = max_load_move;
2590
Peter Williamse1d14842007-10-24 18:23:51 +02002591 if (max_load_move == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02002592 goto out;
2593
2594 pinned = 1;
2595
2596 /*
2597 * Start the load-balancing iterator:
2598 */
2599 p = iterator->start(iterator->arg);
2600next:
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002601 if (!p || loops++ > sysctl_sched_nr_migrate)
Ingo Molnardd41f592007-07-09 18:51:59 +02002602 goto out;
2603 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002604 * To help distribute high priority tasks across CPUs we don't
Ingo Molnardd41f592007-07-09 18:51:59 +02002605 * skip a task if it will be the highest priority task (i.e. smallest
2606 * prio value) on its new queue regardless of its load weight
2607 */
2608 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
2609 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002610 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02002611 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002612 p = iterator->next(iterator->arg);
2613 goto next;
2614 }
2615
2616 pull_task(busiest, p, this_rq, this_cpu);
2617 pulled++;
2618 rem_load_move -= p->se.load.weight;
2619
2620 /*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +01002621 * We only want to steal up to the prescribed amount of weighted load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002622 */
Peter Williamse1d14842007-10-24 18:23:51 +02002623 if (rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002624 if (p->prio < *this_best_prio)
2625 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02002626 p = iterator->next(iterator->arg);
2627 goto next;
2628 }
2629out:
2630 /*
Peter Williamse1d14842007-10-24 18:23:51 +02002631 * Right now, this is one of only two places pull_task() is called,
Ingo Molnardd41f592007-07-09 18:51:59 +02002632 * so we can safely collect pull_task() stats here rather than
2633 * inside pull_task().
2634 */
2635 schedstat_add(sd, lb_gained[idle], pulled);
2636
2637 if (all_pinned)
2638 *all_pinned = pinned;
Peter Williamse1d14842007-10-24 18:23:51 +02002639
2640 return max_load_move - rem_load_move;
Ingo Molnardd41f592007-07-09 18:51:59 +02002641}
Ingo Molnar48f24c42006-07-03 00:25:40 -07002642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643/*
Peter Williams43010652007-08-09 11:16:46 +02002644 * move_tasks tries to move up to max_load_move weighted load from busiest to
2645 * this_rq, as part of a balancing operation within domain "sd".
2646 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 *
2648 * Called with both runqueues locked.
2649 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002650static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02002651 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002652 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07002653 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002655 const struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02002656 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002657 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Ingo Molnardd41f592007-07-09 18:51:59 +02002659 do {
Peter Williams43010652007-08-09 11:16:46 +02002660 total_load_moved +=
2661 class->load_balance(this_rq, this_cpu, busiest,
Peter Williamse1d14842007-10-24 18:23:51 +02002662 max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002663 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02002664 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02002665 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Peter Williams43010652007-08-09 11:16:46 +02002667 return total_load_moved > 0;
2668}
2669
Peter Williamse1d14842007-10-24 18:23:51 +02002670static int
2671iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2672 struct sched_domain *sd, enum cpu_idle_type idle,
2673 struct rq_iterator *iterator)
2674{
2675 struct task_struct *p = iterator->start(iterator->arg);
2676 int pinned = 0;
2677
2678 while (p) {
2679 if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
2680 pull_task(busiest, p, this_rq, this_cpu);
2681 /*
2682 * Right now, this is only the second place pull_task()
2683 * is called, so we can safely collect pull_task()
2684 * stats here rather than inside pull_task().
2685 */
2686 schedstat_inc(sd, lb_gained[idle]);
2687
2688 return 1;
2689 }
2690 p = iterator->next(iterator->arg);
2691 }
2692
2693 return 0;
2694}
2695
Peter Williams43010652007-08-09 11:16:46 +02002696/*
2697 * move_one_task tries to move exactly one task from busiest to this_rq, as
2698 * part of active balancing operations within "domain".
2699 * Returns 1 if successful and 0 otherwise.
2700 *
2701 * Called with both runqueues locked.
2702 */
2703static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2704 struct sched_domain *sd, enum cpu_idle_type idle)
2705{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002706 const struct sched_class *class;
Peter Williams43010652007-08-09 11:16:46 +02002707
2708 for (class = sched_class_highest; class; class = class->next)
Peter Williamse1d14842007-10-24 18:23:51 +02002709 if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
Peter Williams43010652007-08-09 11:16:46 +02002710 return 1;
2711
2712 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713}
2714
2715/*
2716 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07002717 * domain. It calculates and returns the amount of weighted load which
2718 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 */
2720static struct sched_group *
2721find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02002722 unsigned long *imbalance, enum cpu_idle_type idle,
2723 int *sd_idle, cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724{
2725 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
2726 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002727 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07002728 unsigned long busiest_load_per_task, busiest_nr_running;
2729 unsigned long this_load_per_task, this_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02002730 int load_idx, group_imb = 0;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002731#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2732 int power_savings_balance = 1;
2733 unsigned long leader_nr_running = 0, min_load_per_task = 0;
2734 unsigned long min_nr_running = ULONG_MAX;
2735 struct sched_group *group_min = NULL, *group_leader = NULL;
2736#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002739 busiest_load_per_task = busiest_nr_running = 0;
2740 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002741 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002742 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002743 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002744 load_idx = sd->newidle_idx;
2745 else
2746 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
2748 do {
Ken Chen908a7c12007-10-17 16:55:11 +02002749 unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 int local_group;
2751 int i;
Ken Chen908a7c12007-10-17 16:55:11 +02002752 int __group_imb = 0;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002753 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002754 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
2756 local_group = cpu_isset(this_cpu, group->cpumask);
2757
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002758 if (local_group)
2759 balance_cpu = first_cpu(group->cpumask);
2760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07002762 sum_weighted_load = sum_nr_running = avg_load = 0;
Ken Chen908a7c12007-10-17 16:55:11 +02002763 max_cpu_load = 0;
2764 min_cpu_load = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
2766 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002767 struct rq *rq;
2768
2769 if (!cpu_isset(i, *cpus))
2770 continue;
2771
2772 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07002773
Suresh Siddha9439aab2007-07-19 21:28:35 +02002774 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07002775 *sd_idle = 0;
2776
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002778 if (local_group) {
2779 if (idle_cpu(i) && !first_idle_cpu) {
2780 first_idle_cpu = 1;
2781 balance_cpu = i;
2782 }
2783
Nick Piggina2000572006-02-10 01:51:02 -08002784 load = target_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02002785 } else {
Nick Piggina2000572006-02-10 01:51:02 -08002786 load = source_load(i, load_idx);
Ken Chen908a7c12007-10-17 16:55:11 +02002787 if (load > max_cpu_load)
2788 max_cpu_load = load;
2789 if (min_cpu_load > load)
2790 min_cpu_load = load;
2791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
2793 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07002794 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002795 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 }
2797
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002798 /*
2799 * First idle cpu or the first cpu(busiest) in this sched group
2800 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02002801 * domains. In the newly idle case, we will allow all the cpu's
2802 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002803 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02002804 if (idle != CPU_NEWLY_IDLE && local_group &&
2805 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002806 *balance = 0;
2807 goto ret;
2808 }
2809
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07002811 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
2813 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002814 avg_load = sg_div_cpu_power(group,
2815 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
Ken Chen908a7c12007-10-17 16:55:11 +02002817 if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
2818 __group_imb = 1;
2819
Eric Dumazet5517d862007-05-08 00:32:57 -07002820 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002821
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 if (local_group) {
2823 this_load = avg_load;
2824 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002825 this_nr_running = sum_nr_running;
2826 this_load_per_task = sum_weighted_load;
2827 } else if (avg_load > max_load &&
Ken Chen908a7c12007-10-17 16:55:11 +02002828 (sum_nr_running > group_capacity || __group_imb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 max_load = avg_load;
2830 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002831 busiest_nr_running = sum_nr_running;
2832 busiest_load_per_task = sum_weighted_load;
Ken Chen908a7c12007-10-17 16:55:11 +02002833 group_imb = __group_imb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002835
2836#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2837 /*
2838 * Busy processors will not participate in power savings
2839 * balance.
2840 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002841 if (idle == CPU_NOT_IDLE ||
2842 !(sd->flags & SD_POWERSAVINGS_BALANCE))
2843 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002844
2845 /*
2846 * If the local group is idle or completely loaded
2847 * no need to do power savings balance at this domain
2848 */
2849 if (local_group && (this_nr_running >= group_capacity ||
2850 !this_nr_running))
2851 power_savings_balance = 0;
2852
Ingo Molnardd41f592007-07-09 18:51:59 +02002853 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002854 * If a group is already running at full capacity or idle,
2855 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02002856 */
2857 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002858 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02002859 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002860
Ingo Molnardd41f592007-07-09 18:51:59 +02002861 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002862 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002863 * This is the group from where we need to pick up the load
2864 * for saving power
2865 */
2866 if ((sum_nr_running < min_nr_running) ||
2867 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002868 first_cpu(group->cpumask) <
2869 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002870 group_min = group;
2871 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002872 min_load_per_task = sum_weighted_load /
2873 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002874 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002875
Ingo Molnardd41f592007-07-09 18:51:59 +02002876 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002877 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02002878 * capacity but still has some space to pick up some load
2879 * from other group and save more power
2880 */
2881 if (sum_nr_running <= group_capacity - 1) {
2882 if (sum_nr_running > leader_nr_running ||
2883 (sum_nr_running == leader_nr_running &&
2884 first_cpu(group->cpumask) >
2885 first_cpu(group_leader->cpumask))) {
2886 group_leader = group;
2887 leader_nr_running = sum_nr_running;
2888 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002889 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002890group_next:
2891#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 group = group->next;
2893 } while (group != sd->groups);
2894
Peter Williams2dd73a42006-06-27 02:54:34 -07002895 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 goto out_balanced;
2897
2898 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
2899
2900 if (this_load >= avg_load ||
2901 100*max_load <= sd->imbalance_pct*this_load)
2902 goto out_balanced;
2903
Peter Williams2dd73a42006-06-27 02:54:34 -07002904 busiest_load_per_task /= busiest_nr_running;
Ken Chen908a7c12007-10-17 16:55:11 +02002905 if (group_imb)
2906 busiest_load_per_task = min(busiest_load_per_task, avg_load);
2907
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 /*
2909 * We're trying to get all the cpus to the average_load, so we don't
2910 * want to push ourselves above the average load, nor do we wish to
2911 * reduce the max loaded cpu below the average load, as either of these
2912 * actions would just result in more rebalancing later, and ping-pong
2913 * tasks around. Thus we look for the minimum possible imbalance.
2914 * Negative imbalances (*we* are more loaded than anyone else) will
2915 * be counted as no imbalance for these purposes -- we can't fix that
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01002916 * by pulling tasks to us. Be careful of negative numbers as they'll
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 * appear as very large values with unsigned longs.
2918 */
Peter Williams2dd73a42006-06-27 02:54:34 -07002919 if (max_load <= busiest_load_per_task)
2920 goto out_balanced;
2921
2922 /*
2923 * In the presence of smp nice balancing, certain scenarios can have
2924 * max load less than avg load(as we skip the groups at or below
2925 * its cpu_power, while calculating max_load..)
2926 */
2927 if (max_load < avg_load) {
2928 *imbalance = 0;
2929 goto small_imbalance;
2930 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002931
2932 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07002933 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002934
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07002936 *imbalance = min(max_pull * busiest->__cpu_power,
2937 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 / SCHED_LOAD_SCALE;
2939
Peter Williams2dd73a42006-06-27 02:54:34 -07002940 /*
2941 * if *imbalance is less than the average load per runnable task
2942 * there is no gaurantee that any tasks will be moved so we'll have
2943 * a think about bumping its value to force at least one task to be
2944 * moved
2945 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02002946 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07002947 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07002948 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
Peter Williams2dd73a42006-06-27 02:54:34 -07002950small_imbalance:
2951 pwr_move = pwr_now = 0;
2952 imbn = 2;
2953 if (this_nr_running) {
2954 this_load_per_task /= this_nr_running;
2955 if (busiest_load_per_task > this_load_per_task)
2956 imbn = 1;
2957 } else
2958 this_load_per_task = SCHED_LOAD_SCALE;
2959
Ingo Molnardd41f592007-07-09 18:51:59 +02002960 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
2961 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002962 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 return busiest;
2964 }
2965
2966 /*
2967 * OK, we don't have enough imbalance to justify moving tasks,
2968 * however we may be able to increase total CPU power used by
2969 * moving them.
2970 */
2971
Eric Dumazet5517d862007-05-08 00:32:57 -07002972 pwr_now += busiest->__cpu_power *
2973 min(busiest_load_per_task, max_load);
2974 pwr_now += this->__cpu_power *
2975 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 pwr_now /= SCHED_LOAD_SCALE;
2977
2978 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07002979 tmp = sg_div_cpu_power(busiest,
2980 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07002982 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07002983 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
2985 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07002986 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08002987 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07002988 tmp = sg_div_cpu_power(this,
2989 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 else
Eric Dumazet5517d862007-05-08 00:32:57 -07002991 tmp = sg_div_cpu_power(this,
2992 busiest_load_per_task * SCHED_LOAD_SCALE);
2993 pwr_move += this->__cpu_power *
2994 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 pwr_move /= SCHED_LOAD_SCALE;
2996
2997 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02002998 if (pwr_move > pwr_now)
2999 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 }
3001
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 return busiest;
3003
3004out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003005#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003006 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003007 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003009 if (this == group_leader && group_leader != group_min) {
3010 *imbalance = min_load_per_task;
3011 return group_min;
3012 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07003013#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003014ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 *imbalance = 0;
3016 return NULL;
3017}
3018
3019/*
3020 * find_busiest_queue - find the busiest runqueue among the cpus in group.
3021 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003022static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003023find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003024 unsigned long imbalance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025{
Ingo Molnar70b97a72006-07-03 00:25:42 -07003026 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07003027 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 int i;
3029
3030 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003031 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003032
3033 if (!cpu_isset(i, *cpus))
3034 continue;
3035
Ingo Molnar48f24c42006-07-03 00:25:40 -07003036 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02003037 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Ingo Molnardd41f592007-07-09 18:51:59 +02003039 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07003040 continue;
3041
Ingo Molnardd41f592007-07-09 18:51:59 +02003042 if (wl > max_load) {
3043 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003044 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 }
3046 }
3047
3048 return busiest;
3049}
3050
3051/*
Nick Piggin77391d72005-06-25 14:57:30 -07003052 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
3053 * so long as it is large enough.
3054 */
3055#define MAX_PINNED_INTERVAL 512
3056
3057/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3059 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003061static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003062 struct sched_domain *sd, enum cpu_idle_type idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003063 int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
Peter Williams43010652007-08-09 11:16:46 +02003065 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003068 struct rq *busiest;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003069 cpumask_t cpus = CPU_MASK_ALL;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003070 unsigned long flags;
Nick Piggin5969fe02005-09-10 00:26:19 -07003071
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003072 /*
3073 * When power savings policy is enabled for the parent domain, idle
3074 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02003075 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003076 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003077 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003078 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003079 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003080 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
Ingo Molnar2d723762007-10-15 17:00:12 +02003082 schedstat_inc(sd, lb_count[idle]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003084redo:
3085 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003086 &cpus, balance);
3087
Chen, Kenneth W06066712006-12-10 02:20:35 -08003088 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003089 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 if (!group) {
3092 schedstat_inc(sd, lb_nobusyg[idle]);
3093 goto out_balanced;
3094 }
3095
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003096 busiest = find_busiest_queue(group, idle, imbalance, &cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 if (!busiest) {
3098 schedstat_inc(sd, lb_nobusyq[idle]);
3099 goto out_balanced;
3100 }
3101
Nick Piggindb935db2005-06-25 14:57:11 -07003102 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
3104 schedstat_add(sd, lb_imbalance[idle], imbalance);
3105
Peter Williams43010652007-08-09 11:16:46 +02003106 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 if (busiest->nr_running > 1) {
3108 /*
3109 * Attempt to move tasks. If find_busiest_group has found
3110 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02003111 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 * correctly treated as an imbalance.
3113 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003114 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07003115 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02003116 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07003117 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07003118 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003119 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07003120
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003121 /*
3122 * some other cpu did the load balance for us.
3123 */
Peter Williams43010652007-08-09 11:16:46 +02003124 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003125 resched_cpu(this_cpu);
3126
Nick Piggin81026792005-06-25 14:57:07 -07003127 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003128 if (unlikely(all_pinned)) {
3129 cpu_clear(cpu_of(busiest), cpus);
3130 if (!cpus_empty(cpus))
3131 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07003132 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 }
Nick Piggin81026792005-06-25 14:57:07 -07003135
Peter Williams43010652007-08-09 11:16:46 +02003136 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 schedstat_inc(sd, lb_failed[idle]);
3138 sd->nr_balance_failed++;
3139
3140 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003142 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003143
3144 /* don't kick the migration_thread, if the curr
3145 * task on busiest cpu can't be moved to this_cpu
3146 */
3147 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003148 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003149 all_pinned = 1;
3150 goto out_one_pinned;
3151 }
3152
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 if (!busiest->active_balance) {
3154 busiest->active_balance = 1;
3155 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07003156 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08003158 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07003159 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 wake_up_process(busiest->migration_thread);
3161
3162 /*
3163 * We've kicked active balancing, reset the failure
3164 * counter.
3165 */
Nick Piggin39507452005-06-25 14:57:09 -07003166 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 }
Nick Piggin81026792005-06-25 14:57:07 -07003168 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 sd->nr_balance_failed = 0;
3170
Nick Piggin81026792005-06-25 14:57:07 -07003171 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 /* We were unbalanced, so reset the balancing interval */
3173 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07003174 } else {
3175 /*
3176 * If we've begun active balancing, start to back off. This
3177 * case may not be covered by the all_pinned logic if there
3178 * is only 1 task on the busy runqueue (because we don't call
3179 * move_tasks).
3180 */
3181 if (sd->balance_interval < sd->max_interval)
3182 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 }
3184
Peter Williams43010652007-08-09 11:16:46 +02003185 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003186 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003187 return -1;
Peter Williams43010652007-08-09 11:16:46 +02003188 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
3190out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 schedstat_inc(sd, lb_balanced[idle]);
3192
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003193 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003194
3195out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07003197 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
3198 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 sd->balance_interval *= 2;
3200
Ingo Molnar48f24c42006-07-03 00:25:40 -07003201 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003202 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003203 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 return 0;
3205}
3206
3207/*
3208 * Check this_cpu to ensure it is balanced within domain. Attempt to move
3209 * tasks if there is an imbalance.
3210 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003211 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 * this_rq is locked.
3213 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07003214static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07003215load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216{
3217 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003218 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02003220 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07003221 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003222 int all_pinned = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003223 cpumask_t cpus = CPU_MASK_ALL;
Nick Piggin5969fe02005-09-10 00:26:19 -07003224
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003225 /*
3226 * When power savings policy is enabled for the parent domain, idle
3227 * sibling can pick up load irrespective of busy siblings. In this case,
3228 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003229 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003230 */
3231 if (sd->flags & SD_SHARE_CPUPOWER &&
3232 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003233 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Ingo Molnar2d723762007-10-15 17:00:12 +02003235 schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003236redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003237 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003238 &sd_idle, &cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003240 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003241 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 }
3243
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003244 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003245 &cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07003246 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003247 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003248 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 }
3250
Nick Piggindb935db2005-06-25 14:57:11 -07003251 BUG_ON(busiest == this_rq);
3252
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003253 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003254
Peter Williams43010652007-08-09 11:16:46 +02003255 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003256 if (busiest->nr_running > 1) {
3257 /* Attempt to move tasks */
3258 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003259 /* this_rq->clock is already updated */
3260 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02003261 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003262 imbalance, sd, CPU_NEWLY_IDLE,
3263 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003264 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003265
Suresh Siddha969bb4e2007-07-19 21:28:35 +02003266 if (unlikely(all_pinned)) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07003267 cpu_clear(cpu_of(busiest), cpus);
3268 if (!cpus_empty(cpus))
3269 goto redo;
3270 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07003271 }
3272
Peter Williams43010652007-08-09 11:16:46 +02003273 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003274 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003275 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
3276 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003277 return -1;
3278 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003279 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
Peter Williams43010652007-08-09 11:16:46 +02003281 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003282
3283out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003284 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003285 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07003286 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07003287 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003288 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003289
Nick Piggin16cfb1c2005-06-25 14:57:08 -07003290 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291}
3292
3293/*
3294 * idle_balance is called by schedule() if this_cpu is about to become
3295 * idle. Attempts to pull tasks from other CPUs.
3296 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003297static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298{
3299 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02003300 int pulled_task = -1;
3301 unsigned long next_balance = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
3303 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003304 unsigned long interval;
3305
3306 if (!(sd->flags & SD_LOAD_BALANCE))
3307 continue;
3308
3309 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07003310 /* If we've pulled tasks over stop searching: */
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003311 pulled_task = load_balance_newidle(this_cpu,
Christoph Lameter92c4ca52007-06-23 17:16:33 -07003312 this_rq, sd);
3313
3314 interval = msecs_to_jiffies(sd->balance_interval);
3315 if (time_after(next_balance, sd->last_balance + interval))
3316 next_balance = sd->last_balance + interval;
3317 if (pulled_task)
3318 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003320 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003321 /*
3322 * We are going idle. next_balance may be set based on
3323 * a busy processor. So reset next_balance.
3324 */
3325 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02003326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327}
3328
3329/*
3330 * active_load_balance is run by migration threads. It pushes running tasks
3331 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
3332 * running on each physical CPU where possible, and avoids physical /
3333 * logical imbalances.
3334 *
3335 * Called with busiest_rq locked.
3336 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003337static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338{
Nick Piggin39507452005-06-25 14:57:09 -07003339 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003340 struct sched_domain *sd;
3341 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07003342
Ingo Molnar48f24c42006-07-03 00:25:40 -07003343 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07003344 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07003345 return;
3346
3347 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
3349 /*
Nick Piggin39507452005-06-25 14:57:09 -07003350 * This condition is "impossible", if it occurs
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003351 * we need to fix it. Originally reported by
Nick Piggin39507452005-06-25 14:57:09 -07003352 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 */
Nick Piggin39507452005-06-25 14:57:09 -07003354 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
Nick Piggin39507452005-06-25 14:57:09 -07003356 /* move a task from busiest_rq to target_rq */
3357 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02003358 update_rq_clock(busiest_rq);
3359 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360
Nick Piggin39507452005-06-25 14:57:09 -07003361 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003362 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07003363 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003364 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07003365 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07003366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367
Ingo Molnar48f24c42006-07-03 00:25:40 -07003368 if (likely(sd)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003369 schedstat_inc(sd, alb_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370
Peter Williams43010652007-08-09 11:16:46 +02003371 if (move_one_task(target_rq, target_cpu, busiest_rq,
3372 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07003373 schedstat_inc(sd, alb_pushed);
3374 else
3375 schedstat_inc(sd, alb_failed);
3376 }
Nick Piggin39507452005-06-25 14:57:09 -07003377 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378}
3379
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003380#ifdef CONFIG_NO_HZ
3381static struct {
3382 atomic_t load_balancer;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003383 cpumask_t cpu_mask;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003384} nohz ____cacheline_aligned = {
3385 .load_balancer = ATOMIC_INIT(-1),
3386 .cpu_mask = CPU_MASK_NONE,
3387};
3388
Christoph Lameter7835b982006-12-10 02:20:22 -08003389/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003390 * This routine will try to nominate the ilb (idle load balancing)
3391 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
3392 * load balancing on behalf of all those cpus. If all the cpus in the system
3393 * go into this tickless mode, then there will be no ilb owner (as there is
3394 * no need for one) and all the cpus will sleep till the next wakeup event
3395 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08003396 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003397 * For the ilb owner, tick is not stopped. And this tick will be used
3398 * for idle load balancing. ilb owner will still be part of
3399 * nohz.cpu_mask..
3400 *
3401 * While stopping the tick, this cpu will become the ilb owner if there
3402 * is no other owner. And will be the owner till that cpu becomes busy
3403 * or if all cpus in the system stop their ticks at which point
3404 * there is no need for ilb owner.
3405 *
3406 * When the ilb owner becomes busy, it nominates another owner, during the
3407 * next busy scheduler_tick()
3408 */
3409int select_nohz_load_balancer(int stop_tick)
3410{
3411 int cpu = smp_processor_id();
3412
3413 if (stop_tick) {
3414 cpu_set(cpu, nohz.cpu_mask);
3415 cpu_rq(cpu)->in_nohz_recently = 1;
3416
3417 /*
3418 * If we are going offline and still the leader, give up!
3419 */
3420 if (cpu_is_offline(cpu) &&
3421 atomic_read(&nohz.load_balancer) == cpu) {
3422 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3423 BUG();
3424 return 0;
3425 }
3426
3427 /* time for ilb owner also to sleep */
3428 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3429 if (atomic_read(&nohz.load_balancer) == cpu)
3430 atomic_set(&nohz.load_balancer, -1);
3431 return 0;
3432 }
3433
3434 if (atomic_read(&nohz.load_balancer) == -1) {
3435 /* make me the ilb owner */
3436 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
3437 return 1;
3438 } else if (atomic_read(&nohz.load_balancer) == cpu)
3439 return 1;
3440 } else {
3441 if (!cpu_isset(cpu, nohz.cpu_mask))
3442 return 0;
3443
3444 cpu_clear(cpu, nohz.cpu_mask);
3445
3446 if (atomic_read(&nohz.load_balancer) == cpu)
3447 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3448 BUG();
3449 }
3450 return 0;
3451}
3452#endif
3453
3454static DEFINE_SPINLOCK(balancing);
3455
3456/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003457 * It checks each scheduling domain to see if it is due to be balanced,
3458 * and initiates a balancing operation if so.
3459 *
3460 * Balancing parameters are set up in arch_init_sched_domains.
3461 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02003462static void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08003463{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003464 int balance = 1;
3465 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08003466 unsigned long interval;
3467 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003468 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08003469 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003470 int update_next_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003472 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 if (!(sd->flags & SD_LOAD_BALANCE))
3474 continue;
3475
3476 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003477 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 interval *= sd->busy_factor;
3479
3480 /* scale ms to jiffies */
3481 interval = msecs_to_jiffies(interval);
3482 if (unlikely(!interval))
3483 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003484 if (interval > HZ*NR_CPUS/10)
3485 interval = HZ*NR_CPUS/10;
3486
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487
Christoph Lameter08c183f2006-12-10 02:20:29 -08003488 if (sd->flags & SD_SERIALIZE) {
3489 if (!spin_trylock(&balancing))
3490 goto out;
3491 }
3492
Christoph Lameterc9819f42006-12-10 02:20:25 -08003493 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003494 if (load_balance(cpu, rq, sd, idle, &balance)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003495 /*
3496 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07003497 * longer idle, or one of our SMT siblings is
3498 * not idle.
3499 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003500 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003502 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08003504 if (sd->flags & SD_SERIALIZE)
3505 spin_unlock(&balancing);
3506out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02003507 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08003508 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003509 update_next_balance = 1;
3510 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003511
3512 /*
3513 * Stop the load balance at this level. There is another
3514 * CPU in our sched group which is doing load balancing more
3515 * actively.
3516 */
3517 if (!balance)
3518 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02003520
3521 /*
3522 * next_balance will be updated only when there is a need.
3523 * When the cpu is attached to null domain for ex, it will not be
3524 * updated.
3525 */
3526 if (likely(update_next_balance))
3527 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003528}
3529
3530/*
3531 * run_rebalance_domains is triggered when needed from the scheduler tick.
3532 * In CONFIG_NO_HZ case, the idle load balance owner will do the
3533 * rebalancing for all the cpus for whom scheduler ticks are stopped.
3534 */
3535static void run_rebalance_domains(struct softirq_action *h)
3536{
Ingo Molnardd41f592007-07-09 18:51:59 +02003537 int this_cpu = smp_processor_id();
3538 struct rq *this_rq = cpu_rq(this_cpu);
3539 enum cpu_idle_type idle = this_rq->idle_at_tick ?
3540 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003541
Ingo Molnardd41f592007-07-09 18:51:59 +02003542 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003543
3544#ifdef CONFIG_NO_HZ
3545 /*
3546 * If this cpu is the owner for idle load balancing, then do the
3547 * balancing on behalf of the other idle cpus whose ticks are
3548 * stopped.
3549 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003550 if (this_rq->idle_at_tick &&
3551 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003552 cpumask_t cpus = nohz.cpu_mask;
3553 struct rq *rq;
3554 int balance_cpu;
3555
Ingo Molnardd41f592007-07-09 18:51:59 +02003556 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003557 for_each_cpu_mask(balance_cpu, cpus) {
3558 /*
3559 * If this cpu gets work to do, stop the load balancing
3560 * work being done for other cpus. Next load
3561 * balancing owner will pick it up.
3562 */
3563 if (need_resched())
3564 break;
3565
Oleg Nesterovde0cf892007-08-12 18:08:19 +02003566 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003567
3568 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003569 if (time_after(this_rq->next_balance, rq->next_balance))
3570 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003571 }
3572 }
3573#endif
3574}
3575
3576/*
3577 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
3578 *
3579 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
3580 * idle load balancing owner or decide to stop the periodic load balancing,
3581 * if the whole system is idle.
3582 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003583static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003584{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003585#ifdef CONFIG_NO_HZ
3586 /*
3587 * If we were in the nohz mode recently and busy at the current
3588 * scheduler tick, then check if we need to nominate new idle
3589 * load balancer.
3590 */
3591 if (rq->in_nohz_recently && !rq->idle_at_tick) {
3592 rq->in_nohz_recently = 0;
3593
3594 if (atomic_read(&nohz.load_balancer) == cpu) {
3595 cpu_clear(cpu, nohz.cpu_mask);
3596 atomic_set(&nohz.load_balancer, -1);
3597 }
3598
3599 if (atomic_read(&nohz.load_balancer) == -1) {
3600 /*
3601 * simple selection for now: Nominate the
3602 * first cpu in the nohz list to be the next
3603 * ilb owner.
3604 *
3605 * TBD: Traverse the sched domains and nominate
3606 * the nearest cpu in the nohz.cpu_mask.
3607 */
3608 int ilb = first_cpu(nohz.cpu_mask);
3609
3610 if (ilb != NR_CPUS)
3611 resched_cpu(ilb);
3612 }
3613 }
3614
3615 /*
3616 * If this cpu is idle and doing idle load balancing for all the
3617 * cpus with ticks stopped, is it time for that to stop?
3618 */
3619 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
3620 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3621 resched_cpu(cpu);
3622 return;
3623 }
3624
3625 /*
3626 * If this cpu is idle and the idle load balancing is done by
3627 * someone else, then no need raise the SCHED_SOFTIRQ
3628 */
3629 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
3630 cpu_isset(cpu, nohz.cpu_mask))
3631 return;
3632#endif
3633 if (time_after_eq(jiffies, rq->next_balance))
3634 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635}
Ingo Molnardd41f592007-07-09 18:51:59 +02003636
3637#else /* CONFIG_SMP */
3638
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639/*
3640 * on UP we do not need to balance between CPUs:
3641 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003642static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643{
3644}
Ingo Molnardd41f592007-07-09 18:51:59 +02003645
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646#endif
3647
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648DEFINE_PER_CPU(struct kernel_stat, kstat);
3649
3650EXPORT_PER_CPU_SYMBOL(kstat);
3651
3652/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02003653 * Return p->sum_exec_runtime plus any more ns on the sched_clock
3654 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02003656unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003659 u64 ns, delta_exec;
3660 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003661
Ingo Molnar41b86e92007-07-09 18:51:58 +02003662 rq = task_rq_lock(p, &flags);
3663 ns = p->se.sum_exec_runtime;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01003664 if (task_current(rq, p)) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02003665 update_rq_clock(rq);
3666 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003667 if ((s64)delta_exec > 0)
3668 ns += delta_exec;
3669 }
3670 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003671
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 return ns;
3673}
3674
3675/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 * Account user cpu time to a process.
3677 * @p: the process that the cpu time gets accounted to
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 * @cputime: the cpu time spent in user space since the last update
3679 */
3680void account_user_time(struct task_struct *p, cputime_t cputime)
3681{
3682 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3683 cputime64_t tmp;
3684
3685 p->utime = cputime_add(p->utime, cputime);
3686
3687 /* Add user time to cpustat. */
3688 tmp = cputime_to_cputime64(cputime);
3689 if (TASK_NICE(p) > 0)
3690 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3691 else
3692 cpustat->user = cputime64_add(cpustat->user, tmp);
3693}
3694
3695/*
Laurent Vivier94886b82007-10-15 17:00:19 +02003696 * Account guest cpu time to a process.
3697 * @p: the process that the cpu time gets accounted to
3698 * @cputime: the cpu time spent in virtual machine since the last update
3699 */
Adrian Bunkf7402e02007-10-29 21:18:10 +01003700static void account_guest_time(struct task_struct *p, cputime_t cputime)
Laurent Vivier94886b82007-10-15 17:00:19 +02003701{
3702 cputime64_t tmp;
3703 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3704
3705 tmp = cputime_to_cputime64(cputime);
3706
3707 p->utime = cputime_add(p->utime, cputime);
3708 p->gtime = cputime_add(p->gtime, cputime);
3709
3710 cpustat->user = cputime64_add(cpustat->user, tmp);
3711 cpustat->guest = cputime64_add(cpustat->guest, tmp);
3712}
3713
3714/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07003715 * Account scaled user cpu time to a process.
3716 * @p: the process that the cpu time gets accounted to
3717 * @cputime: the cpu time spent in user space since the last update
3718 */
3719void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
3720{
3721 p->utimescaled = cputime_add(p->utimescaled, cputime);
3722}
3723
3724/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 * Account system cpu time to a process.
3726 * @p: the process that the cpu time gets accounted to
3727 * @hardirq_offset: the offset to subtract from hardirq_count()
3728 * @cputime: the cpu time spent in kernel space since the last update
3729 */
3730void account_system_time(struct task_struct *p, int hardirq_offset,
3731 cputime_t cputime)
3732{
3733 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003734 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 cputime64_t tmp;
3736
Christian Borntraeger97783852007-11-15 20:57:39 +01003737 if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0))
3738 return account_guest_time(p, cputime);
Laurent Vivier94886b82007-10-15 17:00:19 +02003739
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 p->stime = cputime_add(p->stime, cputime);
3741
3742 /* Add system time to cpustat. */
3743 tmp = cputime_to_cputime64(cputime);
3744 if (hardirq_count() - hardirq_offset)
3745 cpustat->irq = cputime64_add(cpustat->irq, tmp);
3746 else if (softirq_count())
3747 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003748 else if (p != rq->idle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 cpustat->system = cputime64_add(cpustat->system, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003750 else if (atomic_read(&rq->nr_iowait) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3752 else
3753 cpustat->idle = cputime64_add(cpustat->idle, tmp);
3754 /* Account for system time used */
3755 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756}
3757
3758/*
Michael Neulingc66f08b2007-10-18 03:06:34 -07003759 * Account scaled system cpu time to a process.
3760 * @p: the process that the cpu time gets accounted to
3761 * @hardirq_offset: the offset to subtract from hardirq_count()
3762 * @cputime: the cpu time spent in kernel space since the last update
3763 */
3764void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
3765{
3766 p->stimescaled = cputime_add(p->stimescaled, cputime);
3767}
3768
3769/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 * Account for involuntary wait time.
3771 * @p: the process from which the cpu time has been stolen
3772 * @steal: the cpu time spent in involuntary wait
3773 */
3774void account_steal_time(struct task_struct *p, cputime_t steal)
3775{
3776 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3777 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07003778 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
3780 if (p == rq->idle) {
3781 p->stime = cputime_add(p->stime, steal);
3782 if (atomic_read(&rq->nr_iowait) > 0)
3783 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3784 else
3785 cpustat->idle = cputime64_add(cpustat->idle, tmp);
Andrew Mortoncfb52852007-11-14 16:59:45 -08003786 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 cpustat->steal = cputime64_add(cpustat->steal, tmp);
3788}
3789
Christoph Lameter7835b982006-12-10 02:20:22 -08003790/*
3791 * This function gets called by the timer code, with HZ frequency.
3792 * We call it with interrupts disabled.
3793 *
3794 * It also gets called by the fork code, when changing the parent's
3795 * timeslices.
3796 */
3797void scheduler_tick(void)
3798{
Christoph Lameter7835b982006-12-10 02:20:22 -08003799 int cpu = smp_processor_id();
3800 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003801 struct task_struct *curr = rq->curr;
Ingo Molnar529c7722007-08-10 23:05:11 +02003802 u64 next_tick = rq->tick_timestamp + TICK_NSEC;
Christoph Lameter7835b982006-12-10 02:20:22 -08003803
Ingo Molnardd41f592007-07-09 18:51:59 +02003804 spin_lock(&rq->lock);
Ingo Molnar546fe3c2007-08-09 11:16:51 +02003805 __update_rq_clock(rq);
Ingo Molnar529c7722007-08-10 23:05:11 +02003806 /*
3807 * Let rq->clock advance by at least TICK_NSEC:
3808 */
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01003809 if (unlikely(rq->clock < next_tick)) {
Ingo Molnar529c7722007-08-10 23:05:11 +02003810 rq->clock = next_tick;
Guillaume Chazaraincc203d22008-01-25 21:08:34 +01003811 rq->clock_underflows++;
3812 }
Ingo Molnar529c7722007-08-10 23:05:11 +02003813 rq->tick_timestamp = rq->clock;
Ingo Molnarf1a438d2007-08-09 11:16:45 +02003814 update_cpu_load(rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01003815 curr->sched_class->task_tick(rq, curr, 0);
3816 update_sched_rt_period(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003817 spin_unlock(&rq->lock);
3818
Christoph Lametere418e1c2006-12-10 02:20:23 -08003819#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02003820 rq->idle_at_tick = idle_cpu(cpu);
3821 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08003822#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823}
3824
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
3826
Srinivasa Ds43627582008-02-23 15:24:04 -08003827void __kprobes add_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828{
3829 /*
3830 * Underflow?
3831 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003832 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
3833 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 preempt_count() += val;
3835 /*
3836 * Spinlock count overflowing soon?
3837 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003838 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
3839 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840}
3841EXPORT_SYMBOL(add_preempt_count);
3842
Srinivasa Ds43627582008-02-23 15:24:04 -08003843void __kprobes sub_preempt_count(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844{
3845 /*
3846 * Underflow?
3847 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003848 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
3849 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 /*
3851 * Is the spinlock portion underflowing?
3852 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003853 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
3854 !(preempt_count() & PREEMPT_MASK)))
3855 return;
3856
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 preempt_count() -= val;
3858}
3859EXPORT_SYMBOL(sub_preempt_count);
3860
3861#endif
3862
3863/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003864 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003866static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867{
Satyam Sharma838225b2007-10-24 18:23:50 +02003868 struct pt_regs *regs = get_irq_regs();
3869
3870 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
3871 prev->comm, prev->pid, preempt_count());
3872
Ingo Molnardd41f592007-07-09 18:51:59 +02003873 debug_show_held_locks(prev);
3874 if (irqs_disabled())
3875 print_irqtrace_events(prev);
Satyam Sharma838225b2007-10-24 18:23:50 +02003876
3877 if (regs)
3878 show_regs(regs);
3879 else
3880 dump_stack();
Ingo Molnardd41f592007-07-09 18:51:59 +02003881}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
Ingo Molnardd41f592007-07-09 18:51:59 +02003883/*
3884 * Various schedule()-time debugging checks and statistics:
3885 */
3886static inline void schedule_debug(struct task_struct *prev)
3887{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 /*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003889 * Test if we are atomic. Since do_exit() needs to call into
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 * schedule() atomically, we ignore that path for now.
3891 * Otherwise, whine if we are scheduling when we should not be.
3892 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003893 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
3894 __schedule_bug(prev);
3895
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
3897
Ingo Molnar2d723762007-10-15 17:00:12 +02003898 schedstat_inc(this_rq(), sched_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02003899#ifdef CONFIG_SCHEDSTATS
3900 if (unlikely(prev->lock_depth >= 0)) {
Ingo Molnar2d723762007-10-15 17:00:12 +02003901 schedstat_inc(this_rq(), bkl_count);
3902 schedstat_inc(prev, sched_info.bkl_count);
Ingo Molnarb8efb562007-10-15 17:00:10 +02003903 }
3904#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02003905}
3906
3907/*
3908 * Pick up the highest-prio task:
3909 */
3910static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02003911pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02003912{
Ingo Molnar5522d5d2007-10-15 17:00:12 +02003913 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02003914 struct task_struct *p;
3915
3916 /*
3917 * Optimization: we know that if all tasks are in
3918 * the fair class we can call that function directly:
3919 */
3920 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003921 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003922 if (likely(p))
3923 return p;
3924 }
3925
3926 class = sched_class_highest;
3927 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003928 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003929 if (p)
3930 return p;
3931 /*
3932 * Will never be NULL as the idle class always
3933 * returns a non-NULL p:
3934 */
3935 class = class->next;
3936 }
3937}
3938
3939/*
3940 * schedule() is the main scheduler function.
3941 */
3942asmlinkage void __sched schedule(void)
3943{
3944 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08003945 unsigned long *switch_count;
Ingo Molnardd41f592007-07-09 18:51:59 +02003946 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02003947 int cpu;
3948
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949need_resched:
3950 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02003951 cpu = smp_processor_id();
3952 rq = cpu_rq(cpu);
3953 rcu_qsctr_inc(cpu);
3954 prev = rq->curr;
3955 switch_count = &prev->nivcsw;
3956
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 release_kernel_lock(prev);
3958need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
Ingo Molnardd41f592007-07-09 18:51:59 +02003960 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01003962 hrtick_clear(rq);
3963
Ingo Molnar1e819952007-10-15 17:00:13 +02003964 /*
3965 * Do the rq-clock update outside the rq lock:
3966 */
3967 local_irq_disable();
Ingo Molnarc1b3da32007-08-09 11:16:47 +02003968 __update_rq_clock(rq);
Ingo Molnar1e819952007-10-15 17:00:13 +02003969 spin_lock(&rq->lock);
3970 clear_tsk_need_resched(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971
Ingo Molnardd41f592007-07-09 18:51:59 +02003972 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
3973 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
Roel Kluin23e3c3c2008-03-13 17:41:59 +01003974 signal_pending(prev))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02003975 prev->state = TASK_RUNNING;
3976 } else {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02003977 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02003978 }
3979 switch_count = &prev->nvcsw;
3980 }
3981
Steven Rostedt9a897c52008-01-25 21:08:22 +01003982#ifdef CONFIG_SMP
3983 if (prev->sched_class->pre_schedule)
3984 prev->sched_class->pre_schedule(rq, prev);
3985#endif
Steven Rostedtf65eda42008-01-25 21:08:07 +01003986
Ingo Molnardd41f592007-07-09 18:51:59 +02003987 if (unlikely(!rq->nr_running))
3988 idle_balance(cpu, rq);
3989
Ingo Molnar31ee5292007-08-09 11:16:49 +02003990 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02003991 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
3993 sched_info_switch(prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02003994
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 rq->nr_switches++;
3997 rq->curr = next;
3998 ++*switch_count;
3999
Ingo Molnardd41f592007-07-09 18:51:59 +02004000 context_switch(rq, prev, next); /* unlocks the rq */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004001 /*
4002 * the context switch might have flipped the stack from under
4003 * us, hence refresh the local variables.
4004 */
4005 cpu = smp_processor_id();
4006 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 } else
4008 spin_unlock_irq(&rq->lock);
4009
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004010 hrtick_set(rq);
4011
4012 if (unlikely(reacquire_kernel_lock(current) < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 goto need_resched_nonpreemptible;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004014
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 preempt_enable_no_resched();
4016 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
4017 goto need_resched;
4018}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019EXPORT_SYMBOL(schedule);
4020
4021#ifdef CONFIG_PREEMPT
4022/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004023 * this is the entry point to schedule() from in-kernel preemption
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004024 * off of preempt_enable. Kernel preemptions off return from interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 * occur there and call schedule directly.
4026 */
4027asmlinkage void __sched preempt_schedule(void)
4028{
4029 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 struct task_struct *task = current;
4031 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004032
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 /*
4034 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004035 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07004037 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 return;
4039
Andi Kleen3a5c3592007-10-15 17:00:14 +02004040 do {
4041 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
Andi Kleen3a5c3592007-10-15 17:00:14 +02004043 /*
4044 * We keep the big kernel semaphore locked, but we
4045 * clear ->lock_depth so that schedule() doesnt
4046 * auto-release the semaphore:
4047 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004048 saved_lock_depth = task->lock_depth;
4049 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004050 schedule();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004051 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004052 sub_preempt_count(PREEMPT_ACTIVE);
4053
4054 /*
4055 * Check again in case we missed a preemption opportunity
4056 * between schedule and now.
4057 */
4058 barrier();
4059 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061EXPORT_SYMBOL(preempt_schedule);
4062
4063/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004064 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 * off of irq context.
4066 * Note, that this is called and return with irqs disabled. This will
4067 * protect us against recursive calling from irq.
4068 */
4069asmlinkage void __sched preempt_schedule_irq(void)
4070{
4071 struct thread_info *ti = current_thread_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 struct task_struct *task = current;
4073 int saved_lock_depth;
Ingo Molnar6478d882008-01-25 21:08:33 +01004074
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004075 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 BUG_ON(ti->preempt_count || !irqs_disabled());
4077
Andi Kleen3a5c3592007-10-15 17:00:14 +02004078 do {
4079 add_preempt_count(PREEMPT_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080
Andi Kleen3a5c3592007-10-15 17:00:14 +02004081 /*
4082 * We keep the big kernel semaphore locked, but we
4083 * clear ->lock_depth so that schedule() doesnt
4084 * auto-release the semaphore:
4085 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02004086 saved_lock_depth = task->lock_depth;
4087 task->lock_depth = -1;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004088 local_irq_enable();
4089 schedule();
4090 local_irq_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004091 task->lock_depth = saved_lock_depth;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004092 sub_preempt_count(PREEMPT_ACTIVE);
4093
4094 /*
4095 * Check again in case we missed a preemption opportunity
4096 * between schedule and now.
4097 */
4098 barrier();
4099 } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100}
4101
4102#endif /* CONFIG_PREEMPT */
4103
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004104int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
4105 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004107 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109EXPORT_SYMBOL(default_wake_function);
4110
4111/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004112 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
4113 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 * number) then we wake all the non-exclusive tasks and one exclusive task.
4115 *
4116 * There are circumstances in which we can try to wake a task which has already
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004117 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 * zero in this (rare) case, and we handle it by continuing to scan the queue.
4119 */
4120static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
4121 int nr_exclusive, int sync, void *key)
4122{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004123 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02004125 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07004126 unsigned flags = curr->flags;
4127
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07004129 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 break;
4131 }
4132}
4133
4134/**
4135 * __wake_up - wake up threads blocked on a waitqueue.
4136 * @q: the waitqueue
4137 * @mode: which threads
4138 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07004139 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004141void __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004142 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143{
4144 unsigned long flags;
4145
4146 spin_lock_irqsave(&q->lock, flags);
4147 __wake_up_common(q, mode, nr_exclusive, 0, key);
4148 spin_unlock_irqrestore(&q->lock, flags);
4149}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150EXPORT_SYMBOL(__wake_up);
4151
4152/*
4153 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
4154 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004155void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156{
4157 __wake_up_common(q, mode, 1, 0, NULL);
4158}
4159
4160/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07004161 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 * @q: the waitqueue
4163 * @mode: which threads
4164 * @nr_exclusive: how many wake-one or wake-many threads to wake up
4165 *
4166 * The sync wakeup differs that the waker knows that it will schedule
4167 * away soon, so while the target thread will be woken up, it will not
4168 * be migrated to another CPU - ie. the two threads are 'synchronized'
4169 * with each other. This can prevent needless bouncing between CPUs.
4170 *
4171 * On UP it can prevent extra preemption.
4172 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08004173void
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004174__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175{
4176 unsigned long flags;
4177 int sync = 1;
4178
4179 if (unlikely(!q))
4180 return;
4181
4182 if (unlikely(!nr_exclusive))
4183 sync = 0;
4184
4185 spin_lock_irqsave(&q->lock, flags);
4186 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
4187 spin_unlock_irqrestore(&q->lock, flags);
4188}
4189EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
4190
Ingo Molnarb15136e2007-10-24 18:23:48 +02004191void complete(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192{
4193 unsigned long flags;
4194
4195 spin_lock_irqsave(&x->wait.lock, flags);
4196 x->done++;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004197 __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 spin_unlock_irqrestore(&x->wait.lock, flags);
4199}
4200EXPORT_SYMBOL(complete);
4201
Ingo Molnarb15136e2007-10-24 18:23:48 +02004202void complete_all(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203{
4204 unsigned long flags;
4205
4206 spin_lock_irqsave(&x->wait.lock, flags);
4207 x->done += UINT_MAX/2;
Matthew Wilcoxd9514f62007-12-06 11:07:07 -05004208 __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 spin_unlock_irqrestore(&x->wait.lock, flags);
4210}
4211EXPORT_SYMBOL(complete_all);
4212
Andi Kleen8cbbe862007-10-15 17:00:14 +02004213static inline long __sched
4214do_wait_for_common(struct completion *x, long timeout, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 if (!x->done) {
4217 DECLARE_WAITQUEUE(wait, current);
4218
4219 wait.flags |= WQ_FLAG_EXCLUSIVE;
4220 __add_wait_queue_tail(&x->wait, &wait);
4221 do {
Matthew Wilcox009e5772007-12-06 12:29:54 -05004222 if ((state == TASK_INTERRUPTIBLE &&
4223 signal_pending(current)) ||
4224 (state == TASK_KILLABLE &&
4225 fatal_signal_pending(current))) {
Andi Kleen8cbbe862007-10-15 17:00:14 +02004226 __remove_wait_queue(&x->wait, &wait);
4227 return -ERESTARTSYS;
4228 }
4229 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004231 timeout = schedule_timeout(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 spin_lock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004233 if (!timeout) {
4234 __remove_wait_queue(&x->wait, &wait);
4235 return timeout;
4236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 } while (!x->done);
4238 __remove_wait_queue(&x->wait, &wait);
4239 }
4240 x->done--;
Andi Kleen8cbbe862007-10-15 17:00:14 +02004241 return timeout;
4242}
4243
4244static long __sched
4245wait_for_common(struct completion *x, long timeout, int state)
4246{
4247 might_sleep();
4248
4249 spin_lock_irq(&x->wait.lock);
4250 timeout = do_wait_for_common(x, timeout, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 spin_unlock_irq(&x->wait.lock);
Andi Kleen8cbbe862007-10-15 17:00:14 +02004252 return timeout;
4253}
4254
Ingo Molnarb15136e2007-10-24 18:23:48 +02004255void __sched wait_for_completion(struct completion *x)
Andi Kleen8cbbe862007-10-15 17:00:14 +02004256{
4257 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258}
4259EXPORT_SYMBOL(wait_for_completion);
4260
Ingo Molnarb15136e2007-10-24 18:23:48 +02004261unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262wait_for_completion_timeout(struct completion *x, unsigned long timeout)
4263{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004264 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265}
4266EXPORT_SYMBOL(wait_for_completion_timeout);
4267
Andi Kleen8cbbe862007-10-15 17:00:14 +02004268int __sched wait_for_completion_interruptible(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269{
Andi Kleen51e97992007-10-18 21:32:55 +02004270 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
4271 if (t == -ERESTARTSYS)
4272 return t;
4273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274}
4275EXPORT_SYMBOL(wait_for_completion_interruptible);
4276
Ingo Molnarb15136e2007-10-24 18:23:48 +02004277unsigned long __sched
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278wait_for_completion_interruptible_timeout(struct completion *x,
4279 unsigned long timeout)
4280{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004281 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282}
4283EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
4284
Matthew Wilcox009e5772007-12-06 12:29:54 -05004285int __sched wait_for_completion_killable(struct completion *x)
4286{
4287 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
4288 if (t == -ERESTARTSYS)
4289 return t;
4290 return 0;
4291}
4292EXPORT_SYMBOL(wait_for_completion_killable);
4293
Andi Kleen8cbbe862007-10-15 17:00:14 +02004294static long __sched
4295sleep_on_common(wait_queue_head_t *q, int state, long timeout)
Ingo Molnar0fec1712007-07-09 18:52:01 +02004296{
4297 unsigned long flags;
4298 wait_queue_t wait;
4299
4300 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301
Andi Kleen8cbbe862007-10-15 17:00:14 +02004302 __set_current_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303
Andi Kleen8cbbe862007-10-15 17:00:14 +02004304 spin_lock_irqsave(&q->lock, flags);
4305 __add_wait_queue(q, &wait);
4306 spin_unlock(&q->lock);
4307 timeout = schedule_timeout(timeout);
4308 spin_lock_irq(&q->lock);
4309 __remove_wait_queue(q, &wait);
4310 spin_unlock_irqrestore(&q->lock, flags);
4311
4312 return timeout;
4313}
4314
4315void __sched interruptible_sleep_on(wait_queue_head_t *q)
4316{
4317 sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319EXPORT_SYMBOL(interruptible_sleep_on);
4320
Ingo Molnar0fec1712007-07-09 18:52:01 +02004321long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004322interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004324 return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326EXPORT_SYMBOL(interruptible_sleep_on_timeout);
4327
Ingo Molnar0fec1712007-07-09 18:52:01 +02004328void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004330 sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332EXPORT_SYMBOL(sleep_on);
4333
Ingo Molnar0fec1712007-07-09 18:52:01 +02004334long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335{
Andi Kleen8cbbe862007-10-15 17:00:14 +02004336 return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338EXPORT_SYMBOL(sleep_on_timeout);
4339
Ingo Molnarb29739f2006-06-27 02:54:51 -07004340#ifdef CONFIG_RT_MUTEXES
4341
4342/*
4343 * rt_mutex_setprio - set the current priority of a task
4344 * @p: task
4345 * @prio: prio value (kernel-internal form)
4346 *
4347 * This function changes the 'effective' priority of a task. It does
4348 * not touch ->normal_prio like __setscheduler().
4349 *
4350 * Used by the rt_mutex code to implement priority inheritance logic.
4351 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004352void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004353{
4354 unsigned long flags;
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004355 int oldprio, on_rq, running;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004356 struct rq *rq;
Steven Rostedtcb469842008-01-25 21:08:22 +01004357 const struct sched_class *prev_class = p->sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004358
4359 BUG_ON(prio < 0 || prio > MAX_PRIO);
4360
4361 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004362 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004363
Andrew Mortond5f9f942007-05-08 20:27:06 -07004364 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004365 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004366 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004367 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02004368 dequeue_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004369 if (running)
4370 p->sched_class->put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004371
4372 if (rt_prio(prio))
4373 p->sched_class = &rt_sched_class;
4374 else
4375 p->sched_class = &fair_sched_class;
4376
Ingo Molnarb29739f2006-06-27 02:54:51 -07004377 p->prio = prio;
4378
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004379 if (running)
4380 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004381 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004382 enqueue_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004383
4384 check_class_changed(rq, p, prev_class, oldprio, running);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004385 }
4386 task_rq_unlock(rq, &flags);
4387}
4388
4389#endif
4390
Ingo Molnar36c8b582006-07-03 00:25:41 -07004391void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392{
Ingo Molnardd41f592007-07-09 18:51:59 +02004393 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004395 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396
4397 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
4398 return;
4399 /*
4400 * We have to be careful, if called from sys_setpriority(),
4401 * the task might be in the middle of scheduling on another CPU.
4402 */
4403 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02004404 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 /*
4406 * The RT priorities are set via sched_setscheduler(), but we still
4407 * allow the 'normal' nice value to be set - but as expected
4408 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02004409 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 */
Ingo Molnare05606d2007-07-09 18:51:59 +02004411 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 p->static_prio = NICE_TO_PRIO(nice);
4413 goto out_unlock;
4414 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004415 on_rq = p->se.on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004416 if (on_rq) {
Ingo Molnar69be72c2007-08-09 11:16:49 +02004417 dequeue_task(rq, p, 0);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004418 dec_load(rq, p);
4419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07004422 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004423 old_prio = p->prio;
4424 p->prio = effective_prio(p);
4425 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
Ingo Molnardd41f592007-07-09 18:51:59 +02004427 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02004428 enqueue_task(rq, p, 0);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01004429 inc_load(rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07004431 * If the task increased its priority or is running and
4432 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004434 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 resched_task(rq->curr);
4436 }
4437out_unlock:
4438 task_rq_unlock(rq, &flags);
4439}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440EXPORT_SYMBOL(set_user_nice);
4441
Matt Mackalle43379f2005-05-01 08:59:00 -07004442/*
4443 * can_nice - check if a task can reduce its nice value
4444 * @p: task
4445 * @nice: nice value
4446 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004447int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07004448{
Matt Mackall024f4742005-08-18 11:24:19 -07004449 /* convert nice value [19,-20] to rlimit style value [1,40] */
4450 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004451
Matt Mackalle43379f2005-05-01 08:59:00 -07004452 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
4453 capable(CAP_SYS_NICE));
4454}
4455
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456#ifdef __ARCH_WANT_SYS_NICE
4457
4458/*
4459 * sys_nice - change the priority of the current process.
4460 * @increment: priority increment
4461 *
4462 * sys_setpriority is a more generic, but much slower function that
4463 * does similar things.
4464 */
4465asmlinkage long sys_nice(int increment)
4466{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004467 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468
4469 /*
4470 * Setpriority might change our priority at the same moment.
4471 * We don't have to worry. Conceptually one call occurs first
4472 * and we have a single winner.
4473 */
Matt Mackalle43379f2005-05-01 08:59:00 -07004474 if (increment < -40)
4475 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 if (increment > 40)
4477 increment = 40;
4478
4479 nice = PRIO_TO_NICE(current->static_prio) + increment;
4480 if (nice < -20)
4481 nice = -20;
4482 if (nice > 19)
4483 nice = 19;
4484
Matt Mackalle43379f2005-05-01 08:59:00 -07004485 if (increment < 0 && !can_nice(current, nice))
4486 return -EPERM;
4487
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 retval = security_task_setnice(current, nice);
4489 if (retval)
4490 return retval;
4491
4492 set_user_nice(current, nice);
4493 return 0;
4494}
4495
4496#endif
4497
4498/**
4499 * task_prio - return the priority value of a given task.
4500 * @p: the task in question.
4501 *
4502 * This is the priority value as seen by users in /proc.
4503 * RT tasks are offset by -200. Normal tasks are centered
4504 * around 0, value goes from -16 to +15.
4505 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004506int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507{
4508 return p->prio - MAX_RT_PRIO;
4509}
4510
4511/**
4512 * task_nice - return the nice value of a given task.
4513 * @p: the task in question.
4514 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004515int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516{
4517 return TASK_NICE(p);
4518}
Pavel Roskin150d8be2008-03-05 16:56:37 -05004519EXPORT_SYMBOL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520
4521/**
4522 * idle_cpu - is a given cpu idle currently?
4523 * @cpu: the processor in question.
4524 */
4525int idle_cpu(int cpu)
4526{
4527 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
4528}
4529
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530/**
4531 * idle_task - return the idle task for a given cpu.
4532 * @cpu: the processor in question.
4533 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004534struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535{
4536 return cpu_rq(cpu)->idle;
4537}
4538
4539/**
4540 * find_process_by_pid - find a process with a matching PID value.
4541 * @pid: the pid in question.
4542 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004543static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07004545 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546}
4547
4548/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02004549static void
4550__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551{
Ingo Molnardd41f592007-07-09 18:51:59 +02004552 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004553
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02004555 switch (p->policy) {
4556 case SCHED_NORMAL:
4557 case SCHED_BATCH:
4558 case SCHED_IDLE:
4559 p->sched_class = &fair_sched_class;
4560 break;
4561 case SCHED_FIFO:
4562 case SCHED_RR:
4563 p->sched_class = &rt_sched_class;
4564 break;
4565 }
4566
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004568 p->normal_prio = normal_prio(p);
4569 /* we are holding p->pi_lock already */
4570 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07004571 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572}
4573
4574/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004575 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 * @p: the task in question.
4577 * @policy: new policy.
4578 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004579 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004580 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004582int sched_setscheduler(struct task_struct *p, int policy,
4583 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02004585 int retval, oldprio, oldpolicy = -1, on_rq, running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 unsigned long flags;
Steven Rostedtcb469842008-01-25 21:08:22 +01004587 const struct sched_class *prev_class = p->sched_class;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004588 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589
Steven Rostedt66e53932006-06-27 02:54:44 -07004590 /* may grab non-irq protected spin_locks */
4591 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592recheck:
4593 /* double check policy once rq lock held */
4594 if (policy < 0)
4595 policy = oldpolicy = p->policy;
4596 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02004597 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
4598 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08004599 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 /*
4601 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02004602 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
4603 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 */
4605 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004606 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04004607 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02004609 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 return -EINVAL;
4611
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004612 /*
4613 * Allow unprivileged RT tasks to decrease priority:
4614 */
4615 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02004616 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004617 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004618
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004619 if (!lock_task_sighand(p, &flags))
4620 return -ESRCH;
4621 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
4622 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004623
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004624 /* can't set/change the rt policy */
4625 if (policy != p->policy && !rlim_rtprio)
4626 return -EPERM;
4627
4628 /* can't increase priority */
4629 if (param->sched_priority > p->rt_priority &&
4630 param->sched_priority > rlim_rtprio)
4631 return -EPERM;
4632 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004633 /*
4634 * Like positive nice levels, dont allow tasks to
4635 * move out of SCHED_IDLE either:
4636 */
4637 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
4638 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004639
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004640 /* can't change other user's priorities */
4641 if ((current->euid != p->euid) &&
4642 (current->euid != p->uid))
4643 return -EPERM;
4644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645
Peter Zijlstrab68aa232008-02-13 15:45:40 +01004646#ifdef CONFIG_RT_GROUP_SCHED
4647 /*
4648 * Do not allow realtime tasks into groups that have no runtime
4649 * assigned.
4650 */
4651 if (rt_policy(policy) && task_group(p)->rt_runtime == 0)
4652 return -EPERM;
4653#endif
4654
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 retval = security_task_setscheduler(p, policy, param);
4656 if (retval)
4657 return retval;
4658 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07004659 * make sure no PI-waiters arrive (or leave) while we are
4660 * changing the priority of the task:
4661 */
4662 spin_lock_irqsave(&p->pi_lock, flags);
4663 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 * To be able to change p->policy safely, the apropriate
4665 * runqueue lock must be held.
4666 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07004667 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 /* recheck policy now with rq lock held */
4669 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
4670 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004671 __task_rq_unlock(rq);
4672 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 goto recheck;
4674 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02004675 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004676 on_rq = p->se.on_rq;
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004677 running = task_current(rq, p);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004678 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004679 deactivate_task(rq, p, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004680 if (running)
4681 p->sched_class->put_prev_task(rq, p);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02004682
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004684 __setscheduler(rq, p, policy, param->sched_priority);
Dmitry Adamushkof6b53202007-10-15 17:00:08 +02004685
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004686 if (running)
4687 p->sched_class->set_curr_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004688 if (on_rq) {
4689 activate_task(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01004690
4691 check_class_changed(rq, p, prev_class, oldprio, running);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07004693 __task_rq_unlock(rq);
4694 spin_unlock_irqrestore(&p->pi_lock, flags);
4695
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07004696 rt_mutex_adjust_pi(p);
4697
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 return 0;
4699}
4700EXPORT_SYMBOL_GPL(sched_setscheduler);
4701
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004702static int
4703do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 struct sched_param lparam;
4706 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004707 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
4709 if (!param || pid < 0)
4710 return -EINVAL;
4711 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
4712 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004713
4714 rcu_read_lock();
4715 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004717 if (p != NULL)
4718 retval = sched_setscheduler(p, policy, &lparam);
4719 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07004720
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 return retval;
4722}
4723
4724/**
4725 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4726 * @pid: the pid in question.
4727 * @policy: new policy.
4728 * @param: structure containing the new RT priority.
4729 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004730asmlinkage long
4731sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732{
Jason Baronc21761f2006-01-18 17:43:03 -08004733 /* negative values for policy are not valid */
4734 if (policy < 0)
4735 return -EINVAL;
4736
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 return do_sched_setscheduler(pid, policy, param);
4738}
4739
4740/**
4741 * sys_sched_setparam - set/change the RT priority of a thread
4742 * @pid: the pid in question.
4743 * @param: structure containing the new RT priority.
4744 */
4745asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
4746{
4747 return do_sched_setscheduler(pid, -1, param);
4748}
4749
4750/**
4751 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4752 * @pid: the pid in question.
4753 */
4754asmlinkage long sys_sched_getscheduler(pid_t pid)
4755{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004756 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004757 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758
4759 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004760 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
4762 retval = -ESRCH;
4763 read_lock(&tasklist_lock);
4764 p = find_process_by_pid(pid);
4765 if (p) {
4766 retval = security_task_getscheduler(p);
4767 if (!retval)
4768 retval = p->policy;
4769 }
4770 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 return retval;
4772}
4773
4774/**
4775 * sys_sched_getscheduler - get the RT priority of a thread
4776 * @pid: the pid in question.
4777 * @param: structure containing the RT priority.
4778 */
4779asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
4780{
4781 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004782 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02004783 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784
4785 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02004786 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
4788 read_lock(&tasklist_lock);
4789 p = find_process_by_pid(pid);
4790 retval = -ESRCH;
4791 if (!p)
4792 goto out_unlock;
4793
4794 retval = security_task_getscheduler(p);
4795 if (retval)
4796 goto out_unlock;
4797
4798 lp.sched_priority = p->rt_priority;
4799 read_unlock(&tasklist_lock);
4800
4801 /*
4802 * This one might sleep, we cannot do it with a spinlock held ...
4803 */
4804 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
4805
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 return retval;
4807
4808out_unlock:
4809 read_unlock(&tasklist_lock);
4810 return retval;
4811}
4812
4813long sched_setaffinity(pid_t pid, cpumask_t new_mask)
4814{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 cpumask_t cpus_allowed;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004816 struct task_struct *p;
4817 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004819 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 read_lock(&tasklist_lock);
4821
4822 p = find_process_by_pid(pid);
4823 if (!p) {
4824 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004825 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 return -ESRCH;
4827 }
4828
4829 /*
4830 * It is not safe to call set_cpus_allowed with the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004831 * tasklist_lock held. We will bump the task_struct's
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 * usage count and then drop tasklist_lock.
4833 */
4834 get_task_struct(p);
4835 read_unlock(&tasklist_lock);
4836
4837 retval = -EPERM;
4838 if ((current->euid != p->euid) && (current->euid != p->uid) &&
4839 !capable(CAP_SYS_NICE))
4840 goto out_unlock;
4841
David Quigleye7834f82006-06-23 02:03:59 -07004842 retval = security_task_setscheduler(p, 0, NULL);
4843 if (retval)
4844 goto out_unlock;
4845
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 cpus_allowed = cpuset_cpus_allowed(p);
4847 cpus_and(new_mask, new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07004848 again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 retval = set_cpus_allowed(p, new_mask);
4850
Paul Menage8707d8b2007-10-18 23:40:22 -07004851 if (!retval) {
4852 cpus_allowed = cpuset_cpus_allowed(p);
4853 if (!cpus_subset(new_mask, cpus_allowed)) {
4854 /*
4855 * We must have raced with a concurrent cpuset
4856 * update. Just reset the cpus_allowed to the
4857 * cpuset's cpus_allowed
4858 */
4859 new_mask = cpus_allowed;
4860 goto again;
4861 }
4862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863out_unlock:
4864 put_task_struct(p);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004865 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 return retval;
4867}
4868
4869static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
4870 cpumask_t *new_mask)
4871{
4872 if (len < sizeof(cpumask_t)) {
4873 memset(new_mask, 0, sizeof(cpumask_t));
4874 } else if (len > sizeof(cpumask_t)) {
4875 len = sizeof(cpumask_t);
4876 }
4877 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
4878}
4879
4880/**
4881 * sys_sched_setaffinity - set the cpu affinity of a process
4882 * @pid: pid of the process
4883 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4884 * @user_mask_ptr: user-space pointer to the new cpu mask
4885 */
4886asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
4887 unsigned long __user *user_mask_ptr)
4888{
4889 cpumask_t new_mask;
4890 int retval;
4891
4892 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
4893 if (retval)
4894 return retval;
4895
4896 return sched_setaffinity(pid, new_mask);
4897}
4898
4899/*
4900 * Represents all cpu's present in the system
4901 * In systems capable of hotplug, this map could dynamically grow
4902 * as new cpu's are detected in the system via any platform specific
4903 * method, such as ACPI for e.g.
4904 */
4905
Andi Kleen4cef0c62006-01-11 22:44:57 +01004906cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907EXPORT_SYMBOL(cpu_present_map);
4908
4909#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01004910cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07004911EXPORT_SYMBOL(cpu_online_map);
4912
Andi Kleen4cef0c62006-01-11 22:44:57 +01004913cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07004914EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915#endif
4916
4917long sched_getaffinity(pid_t pid, cpumask_t *mask)
4918{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004919 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004922 get_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 read_lock(&tasklist_lock);
4924
4925 retval = -ESRCH;
4926 p = find_process_by_pid(pid);
4927 if (!p)
4928 goto out_unlock;
4929
David Quigleye7834f82006-06-23 02:03:59 -07004930 retval = security_task_getscheduler(p);
4931 if (retval)
4932 goto out_unlock;
4933
Jack Steiner2f7016d2006-02-01 03:05:18 -08004934 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935
4936out_unlock:
4937 read_unlock(&tasklist_lock);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01004938 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939
Ulrich Drepper9531b622007-08-09 11:16:46 +02004940 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941}
4942
4943/**
4944 * sys_sched_getaffinity - get the cpu affinity of a process
4945 * @pid: pid of the process
4946 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4947 * @user_mask_ptr: user-space pointer to hold the current cpu mask
4948 */
4949asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
4950 unsigned long __user *user_mask_ptr)
4951{
4952 int ret;
4953 cpumask_t mask;
4954
4955 if (len < sizeof(cpumask_t))
4956 return -EINVAL;
4957
4958 ret = sched_getaffinity(pid, &mask);
4959 if (ret < 0)
4960 return ret;
4961
4962 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
4963 return -EFAULT;
4964
4965 return sizeof(cpumask_t);
4966}
4967
4968/**
4969 * sys_sched_yield - yield the current processor to other threads.
4970 *
Ingo Molnardd41f592007-07-09 18:51:59 +02004971 * This function yields the current CPU to other tasks. If there are no
4972 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 */
4974asmlinkage long sys_sched_yield(void)
4975{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004976 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
Ingo Molnar2d723762007-10-15 17:00:12 +02004978 schedstat_inc(rq, yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02004979 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980
4981 /*
4982 * Since we are going to call schedule() anyway, there's
4983 * no need to preempt or enable interrupts:
4984 */
4985 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07004986 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 _raw_spin_unlock(&rq->lock);
4988 preempt_enable_no_resched();
4989
4990 schedule();
4991
4992 return 0;
4993}
4994
Andrew Mortone7b38402006-06-30 01:56:00 -07004995static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07004997#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
4998 __might_sleep(__FILE__, __LINE__);
4999#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07005000 /*
5001 * The BKS might be reacquired before we have dropped
5002 * PREEMPT_ACTIVE, which could trigger a second
5003 * cond_resched() call.
5004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 do {
5006 add_preempt_count(PREEMPT_ACTIVE);
5007 schedule();
5008 sub_preempt_count(PREEMPT_ACTIVE);
5009 } while (need_resched());
5010}
5011
Herbert Xu02b67cc2008-01-25 21:08:28 +01005012#if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY)
5013int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014{
Ingo Molnar94142322006-12-29 16:48:13 -08005015 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
5016 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 __cond_resched();
5018 return 1;
5019 }
5020 return 0;
5021}
Herbert Xu02b67cc2008-01-25 21:08:28 +01005022EXPORT_SYMBOL(_cond_resched);
5023#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024
5025/*
5026 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
5027 * call schedule, and on return reacquire the lock.
5028 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005029 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 * operations here to prevent schedule() from being called twice (once via
5031 * spin_unlock(), once by hand).
5032 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005033int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034{
Nick Piggin95c354f2008-01-30 13:31:20 +01005035 int resched = need_resched() && system_state == SYSTEM_RUNNING;
Jan Kara6df3cec2005-06-13 15:52:32 -07005036 int ret = 0;
5037
Nick Piggin95c354f2008-01-30 13:31:20 +01005038 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 spin_unlock(lock);
Nick Piggin95c354f2008-01-30 13:31:20 +01005040 if (resched && need_resched())
5041 __cond_resched();
5042 else
5043 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005044 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005047 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049EXPORT_SYMBOL(cond_resched_lock);
5050
5051int __sched cond_resched_softirq(void)
5052{
5053 BUG_ON(!in_softirq());
5054
Ingo Molnar94142322006-12-29 16:48:13 -08005055 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07005056 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 __cond_resched();
5058 local_bh_disable();
5059 return 1;
5060 }
5061 return 0;
5062}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063EXPORT_SYMBOL(cond_resched_softirq);
5064
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065/**
5066 * yield - yield the current processor to other threads.
5067 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08005068 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 * thread runnable and calls sys_sched_yield().
5070 */
5071void __sched yield(void)
5072{
5073 set_current_state(TASK_RUNNING);
5074 sys_sched_yield();
5075}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076EXPORT_SYMBOL(yield);
5077
5078/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005079 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 * that process accounting knows that this is a task in IO wait state.
5081 *
5082 * But don't do that if it is a deliberate, throttling IO wait (this task
5083 * has set its backing_dev_info: the queue against which it should throttle)
5084 */
5085void __sched io_schedule(void)
5086{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005087 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005089 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 atomic_inc(&rq->nr_iowait);
5091 schedule();
5092 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005093 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095EXPORT_SYMBOL(io_schedule);
5096
5097long __sched io_schedule_timeout(long timeout)
5098{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005099 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 long ret;
5101
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005102 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 atomic_inc(&rq->nr_iowait);
5104 ret = schedule_timeout(timeout);
5105 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07005106 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 return ret;
5108}
5109
5110/**
5111 * sys_sched_get_priority_max - return maximum RT priority.
5112 * @policy: scheduling class.
5113 *
5114 * this syscall returns the maximum rt_priority that can be used
5115 * by a given scheduling class.
5116 */
5117asmlinkage long sys_sched_get_priority_max(int policy)
5118{
5119 int ret = -EINVAL;
5120
5121 switch (policy) {
5122 case SCHED_FIFO:
5123 case SCHED_RR:
5124 ret = MAX_USER_RT_PRIO-1;
5125 break;
5126 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005127 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005128 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 ret = 0;
5130 break;
5131 }
5132 return ret;
5133}
5134
5135/**
5136 * sys_sched_get_priority_min - return minimum RT priority.
5137 * @policy: scheduling class.
5138 *
5139 * this syscall returns the minimum rt_priority that can be used
5140 * by a given scheduling class.
5141 */
5142asmlinkage long sys_sched_get_priority_min(int policy)
5143{
5144 int ret = -EINVAL;
5145
5146 switch (policy) {
5147 case SCHED_FIFO:
5148 case SCHED_RR:
5149 ret = 1;
5150 break;
5151 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08005152 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02005153 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 ret = 0;
5155 }
5156 return ret;
5157}
5158
5159/**
5160 * sys_sched_rr_get_interval - return the default timeslice of a process.
5161 * @pid: pid of the process.
5162 * @interval: userspace pointer to the timeslice value.
5163 *
5164 * this syscall writes the default timeslice value of a given process
5165 * into the user-space timespec buffer. A value of '0' means infinity.
5166 */
5167asmlinkage
5168long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
5169{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005170 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005171 unsigned int time_slice;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005172 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174
5175 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005176 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177
5178 retval = -ESRCH;
5179 read_lock(&tasklist_lock);
5180 p = find_process_by_pid(pid);
5181 if (!p)
5182 goto out_unlock;
5183
5184 retval = security_task_getscheduler(p);
5185 if (retval)
5186 goto out_unlock;
5187
Ingo Molnar77034932007-12-04 17:04:39 +01005188 /*
5189 * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
5190 * tasks that are on an otherwise idle runqueue:
5191 */
5192 time_slice = 0;
5193 if (p->policy == SCHED_RR) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005194 time_slice = DEF_TIMESLICE;
Miao Xie1868f952008-03-07 09:35:06 +08005195 } else if (p->policy != SCHED_FIFO) {
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005196 struct sched_entity *se = &p->se;
5197 unsigned long flags;
5198 struct rq *rq;
5199
5200 rq = task_rq_lock(p, &flags);
Ingo Molnar77034932007-12-04 17:04:39 +01005201 if (rq->cfs.load.weight)
5202 time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005203 task_rq_unlock(rq, &flags);
5204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 read_unlock(&tasklist_lock);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02005206 jiffies_to_timespec(time_slice, &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 return retval;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005209
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210out_unlock:
5211 read_unlock(&tasklist_lock);
5212 return retval;
5213}
5214
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005215static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07005216
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005217void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005220 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 state = p->state ? __ffs(p->state) + 1 : 0;
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005223 printk(KERN_INFO "%-13.13s %c", p->comm,
Andreas Mohr2ed6e342006-07-10 04:43:52 -07005224 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02005225#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005227 printk(KERN_CONT " running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005229 printk(KERN_CONT " %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230#else
5231 if (state == TASK_RUNNING)
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005232 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 else
Ingo Molnarcc4ea792007-10-18 21:32:56 +02005234 printk(KERN_CONT " %016lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235#endif
5236#ifdef CONFIG_DEBUG_STACK_USAGE
5237 {
Al Viro10ebffd2005-11-13 16:06:56 -08005238 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 while (!*n)
5240 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08005241 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 }
5243#endif
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005244 printk(KERN_CONT "%5lu %5d %6d\n", free,
Roland McGrathfcfd50a2008-01-09 00:03:23 -08005245 task_pid_nr(p), task_pid_nr(p->real_parent));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246
Nick Piggin5fb5e6d2008-01-25 21:08:34 +01005247 show_stack(p, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248}
5249
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005250void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005252 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253
Ingo Molnar4bd77322007-07-11 21:21:47 +02005254#if BITS_PER_LONG == 32
5255 printk(KERN_INFO
5256 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02005258 printk(KERN_INFO
5259 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260#endif
5261 read_lock(&tasklist_lock);
5262 do_each_thread(g, p) {
5263 /*
5264 * reset the NMI-timeout, listing all files on a slow
5265 * console might take alot of time:
5266 */
5267 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07005268 if (!state_filter || (p->state & state_filter))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01005269 sched_show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 } while_each_thread(g, p);
5271
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07005272 touch_all_softlockup_watchdogs();
5273
Ingo Molnardd41f592007-07-09 18:51:59 +02005274#ifdef CONFIG_SCHED_DEBUG
5275 sysrq_sched_debug_show();
5276#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08005278 /*
5279 * Only show locks if all tasks are dumped:
5280 */
5281 if (state_filter == -1)
5282 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283}
5284
Ingo Molnar1df21052007-07-09 18:51:58 +02005285void __cpuinit init_idle_bootup_task(struct task_struct *idle)
5286{
Ingo Molnardd41f592007-07-09 18:51:59 +02005287 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02005288}
5289
Ingo Molnarf340c0d2005-06-28 16:40:42 +02005290/**
5291 * init_idle - set up an idle thread for a given CPU
5292 * @idle: task in question
5293 * @cpu: cpu the idle task belongs to
5294 *
5295 * NOTE: this function does not set the idle thread's NEED_RESCHED
5296 * flag, to make booting more robust.
5297 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07005298void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005300 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 unsigned long flags;
5302
Ingo Molnardd41f592007-07-09 18:51:59 +02005303 __sched_fork(idle);
5304 idle->se.exec_start = sched_clock();
5305
Ingo Molnarb29739f2006-06-27 02:54:51 -07005306 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005308 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309
5310 spin_lock_irqsave(&rq->lock, flags);
5311 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07005312#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
5313 idle->oncpu = 1;
5314#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 spin_unlock_irqrestore(&rq->lock, flags);
5316
5317 /* Set the preempt count _outside_ the spinlocks! */
Al Viroa1261f52005-11-13 16:06:55 -08005318 task_thread_info(idle)->preempt_count = 0;
Ingo Molnar6478d882008-01-25 21:08:33 +01005319
Ingo Molnardd41f592007-07-09 18:51:59 +02005320 /*
5321 * The idle tasks have their own, simple scheduling class:
5322 */
5323 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324}
5325
5326/*
5327 * In a system that switches off the HZ timer nohz_cpu_mask
5328 * indicates which cpus entered this state. This is used
5329 * in the rcu update to wait only for active cpus. For system
5330 * which do not switch off the HZ timer nohz_cpu_mask should
5331 * always be CPU_MASK_NONE.
5332 */
5333cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
5334
Ingo Molnar19978ca2007-11-09 22:39:38 +01005335/*
5336 * Increase the granularity value when there are more CPUs,
5337 * because with more CPUs the 'effective latency' as visible
5338 * to users decreases. But the relationship is not linear,
5339 * so pick a second-best guess by going with the log2 of the
5340 * number of CPUs.
5341 *
5342 * This idea comes from the SD scheduler of Con Kolivas:
5343 */
5344static inline void sched_init_granularity(void)
5345{
5346 unsigned int factor = 1 + ilog2(num_online_cpus());
5347 const unsigned long limit = 200000000;
5348
5349 sysctl_sched_min_granularity *= factor;
5350 if (sysctl_sched_min_granularity > limit)
5351 sysctl_sched_min_granularity = limit;
5352
5353 sysctl_sched_latency *= factor;
5354 if (sysctl_sched_latency > limit)
5355 sysctl_sched_latency = limit;
5356
5357 sysctl_sched_wakeup_granularity *= factor;
5358 sysctl_sched_batch_wakeup_granularity *= factor;
5359}
5360
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361#ifdef CONFIG_SMP
5362/*
5363 * This is how migration works:
5364 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07005365 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 * runqueue and wake up that CPU's migration thread.
5367 * 2) we down() the locked semaphore => thread blocks.
5368 * 3) migration thread wakes up (implicitly it forces the migrated
5369 * thread off the CPU)
5370 * 4) it gets the migration request and checks whether the migrated
5371 * task is still in the wrong runqueue.
5372 * 5) if it's in the wrong runqueue then the migration thread removes
5373 * it and puts it into the right queue.
5374 * 6) migration thread up()s the semaphore.
5375 * 7) we wake up and the migration is done.
5376 */
5377
5378/*
5379 * Change a given task's CPU affinity. Migrate the thread to a
5380 * proper CPU and schedule it away if the CPU it's executing on
5381 * is removed from the allowed bitmask.
5382 *
5383 * NOTE: the caller must have a valid reference to the task, the
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005384 * task must not exit() & deallocate itself prematurely. The
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 * call is not atomic; no spinlocks may be held.
5386 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005387int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005389 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005391 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005392 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393
5394 rq = task_rq_lock(p, &flags);
5395 if (!cpus_intersects(new_mask, cpu_online_map)) {
5396 ret = -EINVAL;
5397 goto out;
5398 }
5399
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005400 if (p->sched_class->set_cpus_allowed)
5401 p->sched_class->set_cpus_allowed(p, &new_mask);
5402 else {
Ingo Molnar0eab9142008-01-25 21:08:19 +01005403 p->cpus_allowed = new_mask;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01005404 p->rt.nr_cpus_allowed = cpus_weight(new_mask);
Gregory Haskins73fe6aa2008-01-25 21:08:07 +01005405 }
5406
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 /* Can the task run on the task's current CPU? If so, we're done */
5408 if (cpu_isset(task_cpu(p), new_mask))
5409 goto out;
5410
5411 if (migrate_task(p, any_online_cpu(new_mask), &req)) {
5412 /* Need help from migration thread: drop lock and wait. */
5413 task_rq_unlock(rq, &flags);
5414 wake_up_process(rq->migration_thread);
5415 wait_for_completion(&req.done);
5416 tlb_migrate_finish(p->mm);
5417 return 0;
5418 }
5419out:
5420 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005421
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 return ret;
5423}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424EXPORT_SYMBOL_GPL(set_cpus_allowed);
5425
5426/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005427 * Move (not current) task off this cpu, onto dest cpu. We're doing
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 * this because either it can't run here any more (set_cpus_allowed()
5429 * away from this CPU, or CPU going down), or because we're
5430 * attempting to rebalance this task on exec (sched_exec).
5431 *
5432 * So we race with normal scheduler movements, but that's OK, as long
5433 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07005434 *
5435 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07005437static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005439 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02005440 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441
5442 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07005443 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444
5445 rq_src = cpu_rq(src_cpu);
5446 rq_dest = cpu_rq(dest_cpu);
5447
5448 double_rq_lock(rq_src, rq_dest);
5449 /* Already moved. */
5450 if (task_cpu(p) != src_cpu)
5451 goto out;
5452 /* Affinity changed (again). */
5453 if (!cpu_isset(dest_cpu, p->cpus_allowed))
5454 goto out;
5455
Ingo Molnardd41f592007-07-09 18:51:59 +02005456 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005457 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005458 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02005459
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005461 if (on_rq) {
5462 activate_task(rq_dest, p, 0);
5463 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07005465 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466out:
5467 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07005468 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469}
5470
5471/*
5472 * migration_thread - this is a highprio system thread that performs
5473 * thread migration by bumping thread off CPU then 'pushing' onto
5474 * another runqueue.
5475 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005476static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005479 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480
5481 rq = cpu_rq(cpu);
5482 BUG_ON(rq->migration_thread != current);
5483
5484 set_current_state(TASK_INTERRUPTIBLE);
5485 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005486 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 spin_lock_irq(&rq->lock);
5490
5491 if (cpu_is_offline(cpu)) {
5492 spin_unlock_irq(&rq->lock);
5493 goto wait_to_die;
5494 }
5495
5496 if (rq->active_balance) {
5497 active_load_balance(rq, cpu);
5498 rq->active_balance = 0;
5499 }
5500
5501 head = &rq->migration_queue;
5502
5503 if (list_empty(head)) {
5504 spin_unlock_irq(&rq->lock);
5505 schedule();
5506 set_current_state(TASK_INTERRUPTIBLE);
5507 continue;
5508 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07005509 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 list_del_init(head->next);
5511
Nick Piggin674311d2005-06-25 14:57:27 -07005512 spin_unlock(&rq->lock);
5513 __migrate_task(req->task, cpu, req->dest_cpu);
5514 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515
5516 complete(&req->done);
5517 }
5518 __set_current_state(TASK_RUNNING);
5519 return 0;
5520
5521wait_to_die:
5522 /* Wait for kthread_stop */
5523 set_current_state(TASK_INTERRUPTIBLE);
5524 while (!kthread_should_stop()) {
5525 schedule();
5526 set_current_state(TASK_INTERRUPTIBLE);
5527 }
5528 __set_current_state(TASK_RUNNING);
5529 return 0;
5530}
5531
5532#ifdef CONFIG_HOTPLUG_CPU
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005533
5534static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
5535{
5536 int ret;
5537
5538 local_irq_disable();
5539 ret = __migrate_task(p, src_cpu, dest_cpu);
5540 local_irq_enable();
5541 return ret;
5542}
5543
Kirill Korotaev054b9102006-12-10 02:20:11 -08005544/*
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02005545 * Figure out where task on dead CPU should go, use force if necessary.
Kirill Korotaev054b9102006-12-10 02:20:11 -08005546 * NOTE: interrupts should be disabled by the caller
5547 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005548static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549{
Kirill Korotaevefc30812006-06-27 02:54:32 -07005550 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005552 struct rq *rq;
5553 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554
Andi Kleen3a5c3592007-10-15 17:00:14 +02005555 do {
5556 /* On same node? */
5557 mask = node_to_cpumask(cpu_to_node(dead_cpu));
5558 cpus_and(mask, mask, p->cpus_allowed);
5559 dest_cpu = any_online_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560
Andi Kleen3a5c3592007-10-15 17:00:14 +02005561 /* On any allowed CPU? */
5562 if (dest_cpu == NR_CPUS)
5563 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564
Andi Kleen3a5c3592007-10-15 17:00:14 +02005565 /* No more Mr. Nice Guy. */
5566 if (dest_cpu == NR_CPUS) {
Cliff Wickman470fd642007-10-18 23:40:46 -07005567 cpumask_t cpus_allowed = cpuset_cpus_allowed_locked(p);
5568 /*
5569 * Try to stay on the same cpuset, where the
5570 * current cpuset may be a subset of all cpus.
5571 * The cpuset_cpus_allowed_locked() variant of
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005572 * cpuset_cpus_allowed() will not block. It must be
Cliff Wickman470fd642007-10-18 23:40:46 -07005573 * called within calls to cpuset_lock/cpuset_unlock.
5574 */
Andi Kleen3a5c3592007-10-15 17:00:14 +02005575 rq = task_rq_lock(p, &flags);
Cliff Wickman470fd642007-10-18 23:40:46 -07005576 p->cpus_allowed = cpus_allowed;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005577 dest_cpu = any_online_cpu(p->cpus_allowed);
5578 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579
Andi Kleen3a5c3592007-10-15 17:00:14 +02005580 /*
5581 * Don't tell them about moving exiting tasks or
5582 * kernel threads (both mm NULL), since they never
5583 * leave kernel.
5584 */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005585 if (p->mm && printk_ratelimit()) {
Andi Kleen3a5c3592007-10-15 17:00:14 +02005586 printk(KERN_INFO "process %d (%s) no "
5587 "longer affine to cpu%d\n",
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005588 task_pid_nr(p), p->comm, dead_cpu);
5589 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02005590 }
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005591 } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592}
5593
5594/*
5595 * While a dead CPU has no uninterruptible tasks queued at this point,
5596 * it might still have a nonzero ->nr_uninterruptible counter, because
5597 * for performance reasons the counter is not stricly tracking tasks to
5598 * their home CPUs. So we just add the counter to another CPU's counter,
5599 * to keep the global sum constant after CPU-down:
5600 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07005601static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005603 struct rq *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 unsigned long flags;
5605
5606 local_irq_save(flags);
5607 double_rq_lock(rq_src, rq_dest);
5608 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
5609 rq_src->nr_uninterruptible = 0;
5610 double_rq_unlock(rq_src, rq_dest);
5611 local_irq_restore(flags);
5612}
5613
5614/* Run through task list and migrate tasks from the dead cpu. */
5615static void migrate_live_tasks(int src_cpu)
5616{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005617 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005619 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620
Ingo Molnar48f24c42006-07-03 00:25:40 -07005621 do_each_thread(t, p) {
5622 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 continue;
5624
Ingo Molnar48f24c42006-07-03 00:25:40 -07005625 if (task_cpu(p) == src_cpu)
5626 move_task_off_dead_cpu(src_cpu, p);
5627 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005629 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630}
5631
Ingo Molnardd41f592007-07-09 18:51:59 +02005632/*
5633 * Schedules idle task to be the next runnable task on current CPU.
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005634 * It does so by boosting its priority to highest possible.
5635 * Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 */
5637void sched_idle_next(void)
5638{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005639 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07005640 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 struct task_struct *p = rq->idle;
5642 unsigned long flags;
5643
5644 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005645 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646
Ingo Molnar48f24c42006-07-03 00:25:40 -07005647 /*
5648 * Strictly not necessary since rest of the CPUs are stopped by now
5649 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 */
5651 spin_lock_irqsave(&rq->lock, flags);
5652
Ingo Molnardd41f592007-07-09 18:51:59 +02005653 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005654
Dmitry Adamushko94bc9a72007-11-15 20:57:40 +01005655 update_rq_clock(rq);
5656 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657
5658 spin_unlock_irqrestore(&rq->lock, flags);
5659}
5660
Ingo Molnar48f24c42006-07-03 00:25:40 -07005661/*
5662 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 * offline.
5664 */
5665void idle_task_exit(void)
5666{
5667 struct mm_struct *mm = current->active_mm;
5668
5669 BUG_ON(cpu_online(smp_processor_id()));
5670
5671 if (mm != &init_mm)
5672 switch_mm(mm, &init_mm, current);
5673 mmdrop(mm);
5674}
5675
Kirill Korotaev054b9102006-12-10 02:20:11 -08005676/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005677static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005679 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680
5681 /* Must be exiting, otherwise would be on tasklist. */
Eugene Teo270f7222007-10-18 23:40:38 -07005682 BUG_ON(!p->exit_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683
5684 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07005685 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
Ingo Molnar48f24c42006-07-03 00:25:40 -07005687 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688
5689 /*
5690 * Drop lock around migration; if someone else moves it,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005691 * that's OK. No task can be added to this CPU, so iteration is
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 * fine.
5693 */
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005694 spin_unlock_irq(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005695 move_task_off_dead_cpu(dead_cpu, p);
Oleg Nesterovf7b4cdd2007-10-16 23:30:56 -07005696 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
Ingo Molnar48f24c42006-07-03 00:25:40 -07005698 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699}
5700
5701/* release_task() removes task from tasklist, so we won't find dead tasks. */
5702static void migrate_dead_tasks(unsigned int dead_cpu)
5703{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005704 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005705 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706
Ingo Molnardd41f592007-07-09 18:51:59 +02005707 for ( ; ; ) {
5708 if (!rq->nr_running)
5709 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02005710 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02005711 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02005712 if (!next)
5713 break;
5714 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02005715
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 }
5717}
5718#endif /* CONFIG_HOTPLUG_CPU */
5719
Nick Piggine692ab52007-07-26 13:40:43 +02005720#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5721
5722static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005723 {
5724 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005725 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005726 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01005727 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02005728};
5729
5730static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005731 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005732 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005733 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005734 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005735 .child = sd_ctl_dir,
5736 },
Ingo Molnar38605ca2007-10-29 21:18:11 +01005737 {0, },
Nick Piggine692ab52007-07-26 13:40:43 +02005738};
5739
5740static struct ctl_table *sd_alloc_ctl_entry(int n)
5741{
5742 struct ctl_table *entry =
Milton Miller5cf9f062007-10-15 17:00:19 +02005743 kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
Nick Piggine692ab52007-07-26 13:40:43 +02005744
Nick Piggine692ab52007-07-26 13:40:43 +02005745 return entry;
5746}
5747
Milton Miller6382bc92007-10-15 17:00:19 +02005748static void sd_free_ctl_entry(struct ctl_table **tablep)
5749{
Milton Millercd790072007-10-17 16:55:11 +02005750 struct ctl_table *entry;
Milton Miller6382bc92007-10-15 17:00:19 +02005751
Milton Millercd790072007-10-17 16:55:11 +02005752 /*
5753 * In the intermediate directories, both the child directory and
5754 * procname are dynamically allocated and could fail but the mode
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005755 * will always be set. In the lowest directory the names are
Milton Millercd790072007-10-17 16:55:11 +02005756 * static strings and all have proc handlers.
5757 */
5758 for (entry = *tablep; entry->mode; entry++) {
Milton Miller6382bc92007-10-15 17:00:19 +02005759 if (entry->child)
5760 sd_free_ctl_entry(&entry->child);
Milton Millercd790072007-10-17 16:55:11 +02005761 if (entry->proc_handler == NULL)
5762 kfree(entry->procname);
5763 }
Milton Miller6382bc92007-10-15 17:00:19 +02005764
5765 kfree(*tablep);
5766 *tablep = NULL;
5767}
5768
Nick Piggine692ab52007-07-26 13:40:43 +02005769static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02005770set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02005771 const char *procname, void *data, int maxlen,
5772 mode_t mode, proc_handler *proc_handler)
5773{
Nick Piggine692ab52007-07-26 13:40:43 +02005774 entry->procname = procname;
5775 entry->data = data;
5776 entry->maxlen = maxlen;
5777 entry->mode = mode;
5778 entry->proc_handler = proc_handler;
5779}
5780
5781static struct ctl_table *
5782sd_alloc_ctl_domain_table(struct sched_domain *sd)
5783{
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005784 struct ctl_table *table = sd_alloc_ctl_entry(12);
Nick Piggine692ab52007-07-26 13:40:43 +02005785
Milton Millerad1cdc12007-10-15 17:00:19 +02005786 if (table == NULL)
5787 return NULL;
5788
Alexey Dobriyane0361852007-08-09 11:16:46 +02005789 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005790 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005791 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005792 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005793 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005794 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005795 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005796 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005797 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005798 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005799 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005800 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005801 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005802 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005803 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02005804 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005805 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02005806 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005807 set_table_entry(&table[9], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02005808 &sd->cache_nice_tries,
5809 sizeof(int), 0644, proc_dointvec_minmax);
Zou Nan haiace8b3d2007-10-15 17:00:14 +02005810 set_table_entry(&table[10], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02005811 sizeof(int), 0644, proc_dointvec_minmax);
Milton Miller6323469f2007-10-15 17:00:19 +02005812 /* &table[11] is terminator */
Nick Piggine692ab52007-07-26 13:40:43 +02005813
5814 return table;
5815}
5816
Ingo Molnar9a4e7152007-11-28 15:52:56 +01005817static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
Nick Piggine692ab52007-07-26 13:40:43 +02005818{
5819 struct ctl_table *entry, *table;
5820 struct sched_domain *sd;
5821 int domain_num = 0, i;
5822 char buf[32];
5823
5824 for_each_domain(cpu, sd)
5825 domain_num++;
5826 entry = table = sd_alloc_ctl_entry(domain_num + 1);
Milton Millerad1cdc12007-10-15 17:00:19 +02005827 if (table == NULL)
5828 return NULL;
Nick Piggine692ab52007-07-26 13:40:43 +02005829
5830 i = 0;
5831 for_each_domain(cpu, sd) {
5832 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005833 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005834 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005835 entry->child = sd_alloc_ctl_domain_table(sd);
5836 entry++;
5837 i++;
5838 }
5839 return table;
5840}
5841
5842static struct ctl_table_header *sd_sysctl_header;
Milton Miller6382bc92007-10-15 17:00:19 +02005843static void register_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005844{
5845 int i, cpu_num = num_online_cpus();
5846 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
5847 char buf[32];
5848
Milton Miller73785472007-10-24 18:23:48 +02005849 WARN_ON(sd_ctl_dir[0].child);
5850 sd_ctl_dir[0].child = entry;
5851
Milton Millerad1cdc12007-10-15 17:00:19 +02005852 if (entry == NULL)
5853 return;
5854
Milton Miller97b6ea72007-10-15 17:00:19 +02005855 for_each_online_cpu(i) {
Nick Piggine692ab52007-07-26 13:40:43 +02005856 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005857 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005858 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005859 entry->child = sd_alloc_ctl_cpu_table(i);
Milton Miller97b6ea72007-10-15 17:00:19 +02005860 entry++;
Nick Piggine692ab52007-07-26 13:40:43 +02005861 }
Milton Miller73785472007-10-24 18:23:48 +02005862
5863 WARN_ON(sd_sysctl_header);
Nick Piggine692ab52007-07-26 13:40:43 +02005864 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
5865}
Milton Miller6382bc92007-10-15 17:00:19 +02005866
Milton Miller73785472007-10-24 18:23:48 +02005867/* may be called multiple times per register */
Milton Miller6382bc92007-10-15 17:00:19 +02005868static void unregister_sched_domain_sysctl(void)
5869{
Milton Miller73785472007-10-24 18:23:48 +02005870 if (sd_sysctl_header)
5871 unregister_sysctl_table(sd_sysctl_header);
Milton Miller6382bc92007-10-15 17:00:19 +02005872 sd_sysctl_header = NULL;
Milton Miller73785472007-10-24 18:23:48 +02005873 if (sd_ctl_dir[0].child)
5874 sd_free_ctl_entry(&sd_ctl_dir[0].child);
Milton Miller6382bc92007-10-15 17:00:19 +02005875}
Nick Piggine692ab52007-07-26 13:40:43 +02005876#else
Milton Miller6382bc92007-10-15 17:00:19 +02005877static void register_sched_domain_sysctl(void)
5878{
5879}
5880static void unregister_sched_domain_sysctl(void)
Nick Piggine692ab52007-07-26 13:40:43 +02005881{
5882}
5883#endif
5884
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885/*
5886 * migration_call - callback that gets triggered when a CPU is added.
5887 * Here we can start up the necessary migration thread for the new CPU.
5888 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005889static int __cpuinit
5890migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005893 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005895 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896
5897 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005898
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005900 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02005901 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902 if (IS_ERR(p))
5903 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 kthread_bind(p, cpu);
5905 /* Must be high prio: stop_machine expects to yield to it. */
5906 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02005907 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 task_rq_unlock(rq, &flags);
5909 cpu_rq(cpu)->migration_thread = p;
5910 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005911
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005913 case CPU_ONLINE_FROZEN:
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02005914 /* Strictly unnecessary, as first user will wake it. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 wake_up_process(cpu_rq(cpu)->migration_thread);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04005916
5917 /* Update our root-domain */
5918 rq = cpu_rq(cpu);
5919 spin_lock_irqsave(&rq->lock, flags);
5920 if (rq->rd) {
5921 BUG_ON(!cpu_isset(cpu, rq->rd->span));
5922 cpu_set(cpu, rq->rd->online);
5923 }
5924 spin_unlock_irqrestore(&rq->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005926
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927#ifdef CONFIG_HOTPLUG_CPU
5928 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005929 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07005930 if (!cpu_rq(cpu)->migration_thread)
5931 break;
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005932 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08005933 kthread_bind(cpu_rq(cpu)->migration_thread,
5934 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 kthread_stop(cpu_rq(cpu)->migration_thread);
5936 cpu_rq(cpu)->migration_thread = NULL;
5937 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005938
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005940 case CPU_DEAD_FROZEN:
Cliff Wickman470fd642007-10-18 23:40:46 -07005941 cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 migrate_live_tasks(cpu);
5943 rq = cpu_rq(cpu);
5944 kthread_stop(rq->migration_thread);
5945 rq->migration_thread = NULL;
5946 /* Idle task back to normal (off runqueue, low prio) */
Oleg Nesterovd2da2722007-10-16 23:30:56 -07005947 spin_lock_irq(&rq->lock);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005948 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005949 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02005951 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
5952 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 migrate_dead_tasks(cpu);
Oleg Nesterovd2da2722007-10-16 23:30:56 -07005954 spin_unlock_irq(&rq->lock);
Cliff Wickman470fd642007-10-18 23:40:46 -07005955 cpuset_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 migrate_nr_uninterruptible(rq);
5957 BUG_ON(rq->nr_running != 0);
5958
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005959 /*
5960 * No need to migrate the tasks: it was best-effort if
5961 * they didn't take sched_hotcpu_mutex. Just wake up
5962 * the requestors.
5963 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 spin_lock_irq(&rq->lock);
5965 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005966 struct migration_req *req;
5967
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07005969 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 list_del_init(&req->list);
5971 complete(&req->done);
5972 }
5973 spin_unlock_irq(&rq->lock);
5974 break;
Gregory Haskins57d885f2008-01-25 21:08:18 +01005975
Gregory Haskins08f503b2008-03-10 17:59:11 -04005976 case CPU_DYING:
5977 case CPU_DYING_FROZEN:
Gregory Haskins57d885f2008-01-25 21:08:18 +01005978 /* Update our root-domain */
5979 rq = cpu_rq(cpu);
5980 spin_lock_irqsave(&rq->lock, flags);
5981 if (rq->rd) {
5982 BUG_ON(!cpu_isset(cpu, rq->rd->span));
5983 cpu_clear(cpu, rq->rd->online);
5984 }
5985 spin_unlock_irqrestore(&rq->lock, flags);
5986 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987#endif
5988 }
5989 return NOTIFY_OK;
5990}
5991
5992/* Register at highest priority so that task migration (migrate_all_tasks)
5993 * happens before everything else.
5994 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07005995static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 .notifier_call = migration_call,
5997 .priority = 10
5998};
5999
Adrian Bunke6fe6642007-11-09 22:39:39 +01006000void __init migration_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001{
6002 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07006003 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006004
6005 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07006006 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
6007 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 migration_call(&migration_notifier, CPU_ONLINE, cpu);
6009 register_cpu_notifier(&migration_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010}
6011#endif
6012
6013#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006014
6015/* Number of possible processor ids */
6016int nr_cpu_ids __read_mostly = NR_CPUS;
6017EXPORT_SYMBOL(nr_cpu_ids);
6018
Ingo Molnar3e9830d2007-10-15 17:00:13 +02006019#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006020
6021static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level)
6022{
6023 struct sched_group *group = sd->groups;
6024 cpumask_t groupmask;
6025 char str[NR_CPUS];
6026
6027 cpumask_scnprintf(str, NR_CPUS, sd->span);
6028 cpus_clear(groupmask);
6029
6030 printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
6031
6032 if (!(sd->flags & SD_LOAD_BALANCE)) {
6033 printk("does not load-balance\n");
6034 if (sd->parent)
6035 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
6036 " has parent");
6037 return -1;
6038 }
6039
6040 printk(KERN_CONT "span %s\n", str);
6041
6042 if (!cpu_isset(cpu, sd->span)) {
6043 printk(KERN_ERR "ERROR: domain->span does not contain "
6044 "CPU%d\n", cpu);
6045 }
6046 if (!cpu_isset(cpu, group->cpumask)) {
6047 printk(KERN_ERR "ERROR: domain->groups does not contain"
6048 " CPU%d\n", cpu);
6049 }
6050
6051 printk(KERN_DEBUG "%*s groups:", level + 1, "");
6052 do {
6053 if (!group) {
6054 printk("\n");
6055 printk(KERN_ERR "ERROR: group is NULL\n");
6056 break;
6057 }
6058
6059 if (!group->__cpu_power) {
6060 printk(KERN_CONT "\n");
6061 printk(KERN_ERR "ERROR: domain->cpu_power not "
6062 "set\n");
6063 break;
6064 }
6065
6066 if (!cpus_weight(group->cpumask)) {
6067 printk(KERN_CONT "\n");
6068 printk(KERN_ERR "ERROR: empty group\n");
6069 break;
6070 }
6071
6072 if (cpus_intersects(groupmask, group->cpumask)) {
6073 printk(KERN_CONT "\n");
6074 printk(KERN_ERR "ERROR: repeated CPUs\n");
6075 break;
6076 }
6077
6078 cpus_or(groupmask, groupmask, group->cpumask);
6079
6080 cpumask_scnprintf(str, NR_CPUS, group->cpumask);
6081 printk(KERN_CONT " %s", str);
6082
6083 group = group->next;
6084 } while (group != sd->groups);
6085 printk(KERN_CONT "\n");
6086
6087 if (!cpus_equal(sd->span, groupmask))
6088 printk(KERN_ERR "ERROR: groups don't span domain->span\n");
6089
6090 if (sd->parent && !cpus_subset(groupmask, sd->parent->span))
6091 printk(KERN_ERR "ERROR: parent span is not a superset "
6092 "of domain->span\n");
6093 return 0;
6094}
6095
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096static void sched_domain_debug(struct sched_domain *sd, int cpu)
6097{
6098 int level = 0;
6099
Nick Piggin41c7ce92005-06-25 14:57:24 -07006100 if (!sd) {
6101 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
6102 return;
6103 }
6104
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
6106
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006107 for (;;) {
6108 if (sched_domain_debug_one(sd, cpu, level))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110 level++;
6111 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08006112 if (!sd)
Ingo Molnar4dcf6af2007-10-24 18:23:48 +02006113 break;
6114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115}
6116#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07006117# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118#endif
6119
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006120static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006121{
6122 if (cpus_weight(sd->span) == 1)
6123 return 1;
6124
6125 /* Following flags need at least 2 groups */
6126 if (sd->flags & (SD_LOAD_BALANCE |
6127 SD_BALANCE_NEWIDLE |
6128 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006129 SD_BALANCE_EXEC |
6130 SD_SHARE_CPUPOWER |
6131 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006132 if (sd->groups != sd->groups->next)
6133 return 0;
6134 }
6135
6136 /* Following flags don't use groups */
6137 if (sd->flags & (SD_WAKE_IDLE |
6138 SD_WAKE_AFFINE |
6139 SD_WAKE_BALANCE))
6140 return 0;
6141
6142 return 1;
6143}
6144
Ingo Molnar48f24c42006-07-03 00:25:40 -07006145static int
6146sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07006147{
6148 unsigned long cflags = sd->flags, pflags = parent->flags;
6149
6150 if (sd_degenerate(parent))
6151 return 1;
6152
6153 if (!cpus_equal(sd->span, parent->span))
6154 return 0;
6155
6156 /* Does parent contain flags not in child? */
6157 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
6158 if (cflags & SD_WAKE_AFFINE)
6159 pflags &= ~SD_WAKE_BALANCE;
6160 /* Flags needing groups don't count if only 1 group in parent */
6161 if (parent->groups == parent->groups->next) {
6162 pflags &= ~(SD_LOAD_BALANCE |
6163 SD_BALANCE_NEWIDLE |
6164 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006165 SD_BALANCE_EXEC |
6166 SD_SHARE_CPUPOWER |
6167 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006168 }
6169 if (~cflags & pflags)
6170 return 0;
6171
6172 return 1;
6173}
6174
Gregory Haskins57d885f2008-01-25 21:08:18 +01006175static void rq_attach_root(struct rq *rq, struct root_domain *rd)
6176{
6177 unsigned long flags;
6178 const struct sched_class *class;
6179
6180 spin_lock_irqsave(&rq->lock, flags);
6181
6182 if (rq->rd) {
6183 struct root_domain *old_rd = rq->rd;
6184
Ingo Molnar0eab9142008-01-25 21:08:19 +01006185 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006186 if (class->leave_domain)
6187 class->leave_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006188 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006189
Gregory Haskinsdc938522008-01-25 21:08:26 +01006190 cpu_clear(rq->cpu, old_rd->span);
6191 cpu_clear(rq->cpu, old_rd->online);
6192
Gregory Haskins57d885f2008-01-25 21:08:18 +01006193 if (atomic_dec_and_test(&old_rd->refcount))
6194 kfree(old_rd);
6195 }
6196
6197 atomic_inc(&rd->refcount);
6198 rq->rd = rd;
6199
Gregory Haskinsdc938522008-01-25 21:08:26 +01006200 cpu_set(rq->cpu, rd->span);
Gregory Haskins1f94ef52008-03-10 16:52:41 -04006201 if (cpu_isset(rq->cpu, cpu_online_map))
6202 cpu_set(rq->cpu, rd->online);
Gregory Haskinsdc938522008-01-25 21:08:26 +01006203
Ingo Molnar0eab9142008-01-25 21:08:19 +01006204 for (class = sched_class_highest; class; class = class->next) {
Gregory Haskins57d885f2008-01-25 21:08:18 +01006205 if (class->join_domain)
6206 class->join_domain(rq);
Ingo Molnar0eab9142008-01-25 21:08:19 +01006207 }
Gregory Haskins57d885f2008-01-25 21:08:18 +01006208
6209 spin_unlock_irqrestore(&rq->lock, flags);
6210}
6211
Gregory Haskinsdc938522008-01-25 21:08:26 +01006212static void init_rootdomain(struct root_domain *rd)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006213{
6214 memset(rd, 0, sizeof(*rd));
6215
Gregory Haskinsdc938522008-01-25 21:08:26 +01006216 cpus_clear(rd->span);
6217 cpus_clear(rd->online);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006218}
6219
6220static void init_defrootdomain(void)
6221{
Gregory Haskinsdc938522008-01-25 21:08:26 +01006222 init_rootdomain(&def_root_domain);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006223 atomic_set(&def_root_domain.refcount, 1);
6224}
6225
Gregory Haskinsdc938522008-01-25 21:08:26 +01006226static struct root_domain *alloc_rootdomain(void)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006227{
6228 struct root_domain *rd;
6229
6230 rd = kmalloc(sizeof(*rd), GFP_KERNEL);
6231 if (!rd)
6232 return NULL;
6233
Gregory Haskinsdc938522008-01-25 21:08:26 +01006234 init_rootdomain(rd);
Gregory Haskins57d885f2008-01-25 21:08:18 +01006235
6236 return rd;
6237}
6238
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239/*
Ingo Molnar0eab9142008-01-25 21:08:19 +01006240 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 * hold the hotplug lock.
6242 */
Ingo Molnar0eab9142008-01-25 21:08:19 +01006243static void
6244cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006246 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07006247 struct sched_domain *tmp;
6248
6249 /* Remove the sched domains which do not contribute to scheduling. */
6250 for (tmp = sd; tmp; tmp = tmp->parent) {
6251 struct sched_domain *parent = tmp->parent;
6252 if (!parent)
6253 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006254 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006255 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006256 if (parent->parent)
6257 parent->parent->child = tmp;
6258 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07006259 }
6260
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006261 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07006262 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006263 if (sd)
6264 sd->child = NULL;
6265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266
6267 sched_domain_debug(sd, cpu);
6268
Gregory Haskins57d885f2008-01-25 21:08:18 +01006269 rq_attach_root(rq, rd);
Nick Piggin674311d2005-06-25 14:57:27 -07006270 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271}
6272
6273/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08006274static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275
6276/* Setup the mask of cpus configured for isolated domains */
6277static int __init isolated_cpu_setup(char *str)
6278{
6279 int ints[NR_CPUS], i;
6280
6281 str = get_options(str, ARRAY_SIZE(ints), ints);
6282 cpus_clear(cpu_isolated_map);
6283 for (i = 1; i <= ints[0]; i++)
6284 if (ints[i] < NR_CPUS)
6285 cpu_set(ints[i], cpu_isolated_map);
6286 return 1;
6287}
6288
Ingo Molnar8927f492007-10-15 17:00:13 +02006289__setup("isolcpus=", isolated_cpu_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290
6291/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006292 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
6293 * to a function which identifies what group(along with sched group) a CPU
6294 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
6295 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 *
6297 * init_sched_build_groups will build a circular linked list of the groups
6298 * covered by the given span, and will set each group's ->cpumask correctly,
6299 * and ->cpu_power to 0.
6300 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006301static void
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006302init_sched_build_groups(cpumask_t span, const cpumask_t *cpu_map,
6303 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
6304 struct sched_group **sg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305{
6306 struct sched_group *first = NULL, *last = NULL;
6307 cpumask_t covered = CPU_MASK_NONE;
6308 int i;
6309
6310 for_each_cpu_mask(i, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006311 struct sched_group *sg;
6312 int group = group_fn(i, cpu_map, &sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 int j;
6314
6315 if (cpu_isset(i, covered))
6316 continue;
6317
6318 sg->cpumask = CPU_MASK_NONE;
Eric Dumazet5517d862007-05-08 00:32:57 -07006319 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320
6321 for_each_cpu_mask(j, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006322 if (group_fn(j, cpu_map, NULL) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323 continue;
6324
6325 cpu_set(j, covered);
6326 cpu_set(j, sg->cpumask);
6327 }
6328 if (!first)
6329 first = sg;
6330 if (last)
6331 last->next = sg;
6332 last = sg;
6333 }
6334 last->next = first;
6335}
6336
John Hawkes9c1cfda2005-09-06 15:18:14 -07006337#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338
John Hawkes9c1cfda2005-09-06 15:18:14 -07006339#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08006340
John Hawkes9c1cfda2005-09-06 15:18:14 -07006341/**
6342 * find_next_best_node - find the next node to include in a sched_domain
6343 * @node: node whose sched_domain we're building
6344 * @used_nodes: nodes already in the sched_domain
6345 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006346 * Find the next node to include in a given scheduling domain. Simply
John Hawkes9c1cfda2005-09-06 15:18:14 -07006347 * finds the closest node not already in the @used_nodes map.
6348 *
6349 * Should use nodemask_t.
6350 */
6351static int find_next_best_node(int node, unsigned long *used_nodes)
6352{
6353 int i, n, val, min_val, best_node = 0;
6354
6355 min_val = INT_MAX;
6356
6357 for (i = 0; i < MAX_NUMNODES; i++) {
6358 /* Start at @node */
6359 n = (node + i) % MAX_NUMNODES;
6360
6361 if (!nr_cpus_node(n))
6362 continue;
6363
6364 /* Skip already used nodes */
6365 if (test_bit(n, used_nodes))
6366 continue;
6367
6368 /* Simple min distance search */
6369 val = node_distance(node, n);
6370
6371 if (val < min_val) {
6372 min_val = val;
6373 best_node = n;
6374 }
6375 }
6376
6377 set_bit(best_node, used_nodes);
6378 return best_node;
6379}
6380
6381/**
6382 * sched_domain_node_span - get a cpumask for a node's sched_domain
6383 * @node: node whose cpumask we're constructing
6384 * @size: number of nodes to include in this span
6385 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006386 * Given a node, construct a good cpumask for its sched_domain to span. It
John Hawkes9c1cfda2005-09-06 15:18:14 -07006387 * should be one that prevents unnecessary balancing, but also spreads tasks
6388 * out optimally.
6389 */
6390static cpumask_t sched_domain_node_span(int node)
6391{
John Hawkes9c1cfda2005-09-06 15:18:14 -07006392 DECLARE_BITMAP(used_nodes, MAX_NUMNODES);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006393 cpumask_t span, nodemask;
6394 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006395
6396 cpus_clear(span);
6397 bitmap_zero(used_nodes, MAX_NUMNODES);
6398
6399 nodemask = node_to_cpumask(node);
6400 cpus_or(span, span, nodemask);
6401 set_bit(node, used_nodes);
6402
6403 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
6404 int next_node = find_next_best_node(node, used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006405
John Hawkes9c1cfda2005-09-06 15:18:14 -07006406 nodemask = node_to_cpumask(next_node);
6407 cpus_or(span, span, nodemask);
6408 }
6409
6410 return span;
6411}
6412#endif
6413
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006414int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006415
John Hawkes9c1cfda2005-09-06 15:18:14 -07006416/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07006417 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07006418 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419#ifdef CONFIG_SCHED_SMT
6420static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006421static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006422
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006423static int
6424cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006426 if (sg)
6427 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 return cpu;
6429}
6430#endif
6431
Ingo Molnar48f24c42006-07-03 00:25:40 -07006432/*
6433 * multi-core sched-domains:
6434 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006435#ifdef CONFIG_SCHED_MC
6436static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006437static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006438#endif
6439
6440#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006441static int
6442cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006443{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006444 int group;
Mike Travisd5a74302007-10-16 01:24:05 -07006445 cpumask_t mask = per_cpu(cpu_sibling_map, cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006446 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006447 group = first_cpu(mask);
6448 if (sg)
6449 *sg = &per_cpu(sched_group_core, group);
6450 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006451}
6452#elif defined(CONFIG_SCHED_MC)
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006453static int
6454cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006455{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006456 if (sg)
6457 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006458 return cpu;
6459}
6460#endif
6461
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006463static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07006464
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006465static int
6466cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006468 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006469#ifdef CONFIG_SCHED_MC
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006470 cpumask_t mask = cpu_coregroup_map(cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006471 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006472 group = first_cpu(mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006473#elif defined(CONFIG_SCHED_SMT)
Mike Travisd5a74302007-10-16 01:24:05 -07006474 cpumask_t mask = per_cpu(cpu_sibling_map, cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006475 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006476 group = first_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006478 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006480 if (sg)
6481 *sg = &per_cpu(sched_group_phys, group);
6482 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483}
6484
6485#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07006486/*
6487 * The init_sched_build_groups can't handle what we want to do with node
6488 * groups, so roll our own. Now each node has its own list of groups which
6489 * gets dynamically allocated.
6490 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491static DEFINE_PER_CPU(struct sched_domain, node_domains);
John Hawkesd1b55132005-09-06 15:18:14 -07006492static struct sched_group **sched_group_nodes_bycpu[NR_CPUS];
John Hawkes9c1cfda2005-09-06 15:18:14 -07006493
6494static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006495static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006496
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006497static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
6498 struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006500 cpumask_t nodemask = node_to_cpumask(cpu_to_node(cpu));
6501 int group;
6502
6503 cpus_and(nodemask, nodemask, *cpu_map);
6504 group = first_cpu(nodemask);
6505
6506 if (sg)
6507 *sg = &per_cpu(sched_group_allnodes, group);
6508 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006510
Siddha, Suresh B08069032006-03-27 01:15:23 -08006511static void init_numa_sched_groups_power(struct sched_group *group_head)
6512{
6513 struct sched_group *sg = group_head;
6514 int j;
6515
6516 if (!sg)
6517 return;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006518 do {
6519 for_each_cpu_mask(j, sg->cpumask) {
6520 struct sched_domain *sd;
Siddha, Suresh B08069032006-03-27 01:15:23 -08006521
Andi Kleen3a5c3592007-10-15 17:00:14 +02006522 sd = &per_cpu(phys_domains, j);
6523 if (j != first_cpu(sd->groups->cpumask)) {
6524 /*
6525 * Only add "power" once for each
6526 * physical package.
6527 */
6528 continue;
6529 }
6530
6531 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006532 }
Andi Kleen3a5c3592007-10-15 17:00:14 +02006533 sg = sg->next;
6534 } while (sg != group_head);
Siddha, Suresh B08069032006-03-27 01:15:23 -08006535}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536#endif
6537
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006538#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006539/* Free memory allocated for various sched_group structures */
6540static void free_sched_groups(const cpumask_t *cpu_map)
6541{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006542 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006543
6544 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006545 struct sched_group **sched_group_nodes
6546 = sched_group_nodes_bycpu[cpu];
6547
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006548 if (!sched_group_nodes)
6549 continue;
6550
6551 for (i = 0; i < MAX_NUMNODES; i++) {
6552 cpumask_t nodemask = node_to_cpumask(i);
6553 struct sched_group *oldsg, *sg = sched_group_nodes[i];
6554
6555 cpus_and(nodemask, nodemask, *cpu_map);
6556 if (cpus_empty(nodemask))
6557 continue;
6558
6559 if (sg == NULL)
6560 continue;
6561 sg = sg->next;
6562next_sg:
6563 oldsg = sg;
6564 sg = sg->next;
6565 kfree(oldsg);
6566 if (oldsg != sched_group_nodes[i])
6567 goto next_sg;
6568 }
6569 kfree(sched_group_nodes);
6570 sched_group_nodes_bycpu[cpu] = NULL;
6571 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006572}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006573#else
6574static void free_sched_groups(const cpumask_t *cpu_map)
6575{
6576}
6577#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006578
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006580 * Initialize sched groups cpu_power.
6581 *
6582 * cpu_power indicates the capacity of sched group, which is used while
6583 * distributing the load between different sched groups in a sched domain.
6584 * Typically cpu_power for all the groups in a sched domain will be same unless
6585 * there are asymmetries in the topology. If there are asymmetries, group
6586 * having more cpu_power will pickup more load compared to the group having
6587 * less cpu_power.
6588 *
6589 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
6590 * the maximum number of tasks a group can handle in the presence of other idle
6591 * or lightly loaded groups in the same sched domain.
6592 */
6593static void init_sched_groups_power(int cpu, struct sched_domain *sd)
6594{
6595 struct sched_domain *child;
6596 struct sched_group *group;
6597
6598 WARN_ON(!sd || !sd->groups);
6599
6600 if (cpu != first_cpu(sd->groups->cpumask))
6601 return;
6602
6603 child = sd->child;
6604
Eric Dumazet5517d862007-05-08 00:32:57 -07006605 sd->groups->__cpu_power = 0;
6606
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006607 /*
6608 * For perf policy, if the groups in child domain share resources
6609 * (for example cores sharing some portions of the cache hierarchy
6610 * or SMT), then set this domain groups cpu_power such that each group
6611 * can handle only one task, when there are other idle groups in the
6612 * same sched domain.
6613 */
6614 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
6615 (child->flags &
6616 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07006617 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006618 return;
6619 }
6620
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006621 /*
6622 * add cpu_power of each child group to this groups cpu_power
6623 */
6624 group = child->groups;
6625 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07006626 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006627 group = group->next;
6628 } while (group != child->groups);
6629}
6630
6631/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006632 * Build sched domains for a given set of cpus and attach the sched domains
6633 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006635static int build_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636{
6637 int i;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006638 struct root_domain *rd;
John Hawkesd1b55132005-09-06 15:18:14 -07006639#ifdef CONFIG_NUMA
6640 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006641 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07006642
6643 /*
6644 * Allocate the per-node list of sched groups
6645 */
Milton Miller5cf9f062007-10-15 17:00:19 +02006646 sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006647 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07006648 if (!sched_group_nodes) {
6649 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006650 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07006651 }
6652 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
6653#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654
Gregory Haskinsdc938522008-01-25 21:08:26 +01006655 rd = alloc_rootdomain();
Gregory Haskins57d885f2008-01-25 21:08:18 +01006656 if (!rd) {
6657 printk(KERN_WARNING "Cannot alloc root domain\n");
6658 return -ENOMEM;
6659 }
6660
Linus Torvalds1da177e2005-04-16 15:20:36 -07006661 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006662 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006664 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665 struct sched_domain *sd = NULL, *p;
6666 cpumask_t nodemask = node_to_cpumask(cpu_to_node(i));
6667
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006668 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669
6670#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02006671 if (cpus_weight(*cpu_map) >
6672 SD_NODES_PER_DOMAIN*cpus_weight(nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07006673 sd = &per_cpu(allnodes_domains, i);
6674 *sd = SD_ALLNODES_INIT;
6675 sd->span = *cpu_map;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006676 cpu_to_allnodes_group(i, cpu_map, &sd->groups);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006677 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006678 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006679 } else
6680 p = NULL;
6681
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682 sd = &per_cpu(node_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683 *sd = SD_NODE_INIT;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006684 sd->span = sched_domain_node_span(cpu_to_node(i));
6685 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006686 if (p)
6687 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006688 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006689#endif
6690
6691 p = sd;
6692 sd = &per_cpu(phys_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693 *sd = SD_CPU_INIT;
6694 sd->span = nodemask;
6695 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006696 if (p)
6697 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006698 cpu_to_phys_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006699
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006700#ifdef CONFIG_SCHED_MC
6701 p = sd;
6702 sd = &per_cpu(core_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006703 *sd = SD_MC_INIT;
6704 sd->span = cpu_coregroup_map(i);
6705 cpus_and(sd->span, sd->span, *cpu_map);
6706 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006707 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006708 cpu_to_core_group(i, cpu_map, &sd->groups);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006709#endif
6710
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711#ifdef CONFIG_SCHED_SMT
6712 p = sd;
6713 sd = &per_cpu(cpu_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714 *sd = SD_SIBLING_INIT;
Mike Travisd5a74302007-10-16 01:24:05 -07006715 sd->span = per_cpu(cpu_sibling_map, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006716 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006718 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006719 cpu_to_cpu_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720#endif
6721 }
6722
6723#ifdef CONFIG_SCHED_SMT
6724 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07006725 for_each_cpu_mask(i, *cpu_map) {
Mike Travisd5a74302007-10-16 01:24:05 -07006726 cpumask_t this_sibling_map = per_cpu(cpu_sibling_map, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006727 cpus_and(this_sibling_map, this_sibling_map, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728 if (i != first_cpu(this_sibling_map))
6729 continue;
6730
Ingo Molnardd41f592007-07-09 18:51:59 +02006731 init_sched_build_groups(this_sibling_map, cpu_map,
6732 &cpu_to_cpu_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733 }
6734#endif
6735
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006736#ifdef CONFIG_SCHED_MC
6737 /* Set up multi-core groups */
6738 for_each_cpu_mask(i, *cpu_map) {
6739 cpumask_t this_core_map = cpu_coregroup_map(i);
6740 cpus_and(this_core_map, this_core_map, *cpu_map);
6741 if (i != first_cpu(this_core_map))
6742 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02006743 init_sched_build_groups(this_core_map, cpu_map,
6744 &cpu_to_core_group);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006745 }
6746#endif
6747
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748 /* Set up physical groups */
6749 for (i = 0; i < MAX_NUMNODES; i++) {
6750 cpumask_t nodemask = node_to_cpumask(i);
6751
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006752 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753 if (cpus_empty(nodemask))
6754 continue;
6755
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006756 init_sched_build_groups(nodemask, cpu_map, &cpu_to_phys_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 }
6758
6759#ifdef CONFIG_NUMA
6760 /* Set up node groups */
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006761 if (sd_allnodes)
Ingo Molnardd41f592007-07-09 18:51:59 +02006762 init_sched_build_groups(*cpu_map, cpu_map,
6763 &cpu_to_allnodes_group);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006764
6765 for (i = 0; i < MAX_NUMNODES; i++) {
6766 /* Set up node groups */
6767 struct sched_group *sg, *prev;
6768 cpumask_t nodemask = node_to_cpumask(i);
6769 cpumask_t domainspan;
6770 cpumask_t covered = CPU_MASK_NONE;
6771 int j;
6772
6773 cpus_and(nodemask, nodemask, *cpu_map);
John Hawkesd1b55132005-09-06 15:18:14 -07006774 if (cpus_empty(nodemask)) {
6775 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006776 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07006777 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006778
6779 domainspan = sched_domain_node_span(i);
6780 cpus_and(domainspan, domainspan, *cpu_map);
6781
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006782 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006783 if (!sg) {
6784 printk(KERN_WARNING "Can not alloc domain group for "
6785 "node %d\n", i);
6786 goto error;
6787 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006788 sched_group_nodes[i] = sg;
6789 for_each_cpu_mask(j, nodemask) {
6790 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02006791
John Hawkes9c1cfda2005-09-06 15:18:14 -07006792 sd = &per_cpu(node_domains, j);
6793 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006794 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006795 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006796 sg->cpumask = nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006797 sg->next = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006798 cpus_or(covered, covered, nodemask);
6799 prev = sg;
6800
6801 for (j = 0; j < MAX_NUMNODES; j++) {
6802 cpumask_t tmp, notcovered;
6803 int n = (i + j) % MAX_NUMNODES;
6804
6805 cpus_complement(notcovered, covered);
6806 cpus_and(tmp, notcovered, *cpu_map);
6807 cpus_and(tmp, tmp, domainspan);
6808 if (cpus_empty(tmp))
6809 break;
6810
6811 nodemask = node_to_cpumask(n);
6812 cpus_and(tmp, tmp, nodemask);
6813 if (cpus_empty(tmp))
6814 continue;
6815
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006816 sg = kmalloc_node(sizeof(struct sched_group),
6817 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006818 if (!sg) {
6819 printk(KERN_WARNING
6820 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006821 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006822 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006823 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006824 sg->cpumask = tmp;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006825 sg->next = prev->next;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006826 cpus_or(covered, covered, tmp);
6827 prev->next = sg;
6828 prev = sg;
6829 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831#endif
6832
6833 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006834#ifdef CONFIG_SCHED_SMT
6835 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006836 struct sched_domain *sd = &per_cpu(cpu_domains, i);
6837
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006838 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006839 }
6840#endif
6841#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006842 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006843 struct sched_domain *sd = &per_cpu(core_domains, i);
6844
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006845 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006846 }
6847#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006849 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006850 struct sched_domain *sd = &per_cpu(phys_domains, i);
6851
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006852 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 }
6854
John Hawkes9c1cfda2005-09-06 15:18:14 -07006855#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08006856 for (i = 0; i < MAX_NUMNODES; i++)
6857 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006858
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006859 if (sd_allnodes) {
6860 struct sched_group *sg;
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07006861
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006862 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg);
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07006863 init_numa_sched_groups_power(sg);
6864 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006865#endif
6866
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006868 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869 struct sched_domain *sd;
6870#ifdef CONFIG_SCHED_SMT
6871 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006872#elif defined(CONFIG_SCHED_MC)
6873 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874#else
6875 sd = &per_cpu(phys_domains, i);
6876#endif
Gregory Haskins57d885f2008-01-25 21:08:18 +01006877 cpu_attach_domain(sd, rd, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006879
6880 return 0;
6881
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006882#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006883error:
6884 free_sched_groups(cpu_map);
6885 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006886#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887}
Paul Jackson029190c2007-10-18 23:40:20 -07006888
6889static cpumask_t *doms_cur; /* current sched domains */
6890static int ndoms_cur; /* number of sched domains in 'doms_cur' */
6891
6892/*
6893 * Special case: If a kmalloc of a doms_cur partition (array of
6894 * cpumask_t) fails, then fallback to a single sched domain,
6895 * as determined by the single cpumask_t fallback_doms.
6896 */
6897static cpumask_t fallback_doms;
6898
Heiko Carstens22e52b02008-03-12 18:31:59 +01006899void __attribute__((weak)) arch_update_cpu_topology(void)
6900{
6901}
6902
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006903/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006904 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
Paul Jackson029190c2007-10-18 23:40:20 -07006905 * For now this just excludes isolated cpus, but could be used to
6906 * exclude other special cases in the future.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006907 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006908static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006909{
Milton Miller73785472007-10-24 18:23:48 +02006910 int err;
6911
Heiko Carstens22e52b02008-03-12 18:31:59 +01006912 arch_update_cpu_topology();
Paul Jackson029190c2007-10-18 23:40:20 -07006913 ndoms_cur = 1;
6914 doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
6915 if (!doms_cur)
6916 doms_cur = &fallback_doms;
6917 cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
Milton Miller73785472007-10-24 18:23:48 +02006918 err = build_sched_domains(doms_cur);
Milton Miller6382bc92007-10-15 17:00:19 +02006919 register_sched_domain_sysctl();
Milton Miller73785472007-10-24 18:23:48 +02006920
6921 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006922}
6923
6924static void arch_destroy_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925{
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006926 free_sched_groups(cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006927}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006929/*
6930 * Detach sched domains from a group of cpus specified in cpu_map
6931 * These cpus will now be attached to the NULL domain
6932 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08006933static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006934{
6935 int i;
6936
Milton Miller6382bc92007-10-15 17:00:19 +02006937 unregister_sched_domain_sysctl();
6938
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006939 for_each_cpu_mask(i, *cpu_map)
Gregory Haskins57d885f2008-01-25 21:08:18 +01006940 cpu_attach_domain(NULL, &def_root_domain, i);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006941 synchronize_sched();
6942 arch_destroy_sched_domains(cpu_map);
6943}
6944
Paul Jackson029190c2007-10-18 23:40:20 -07006945/*
6946 * Partition sched domains as specified by the 'ndoms_new'
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006947 * cpumasks in the array doms_new[] of cpumasks. This compares
Paul Jackson029190c2007-10-18 23:40:20 -07006948 * doms_new[] to the current sched domain partitioning, doms_cur[].
6949 * It destroys each deleted domain and builds each new domain.
6950 *
6951 * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006952 * The masks don't intersect (don't overlap.) We should setup one
6953 * sched domain for each mask. CPUs not in any of the cpumasks will
6954 * not be load balanced. If the same cpumask appears both in the
Paul Jackson029190c2007-10-18 23:40:20 -07006955 * current 'doms_cur' domains and in the new 'doms_new', we can leave
6956 * it as it is.
6957 *
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006958 * The passed in 'doms_new' should be kmalloc'd. This routine takes
6959 * ownership of it and will kfree it when done with it. If the caller
Paul Jackson029190c2007-10-18 23:40:20 -07006960 * failed the kmalloc call, then it can pass in doms_new == NULL,
6961 * and partition_sched_domains() will fallback to the single partition
6962 * 'fallback_doms'.
6963 *
6964 * Call with hotplug lock held
6965 */
6966void partition_sched_domains(int ndoms_new, cpumask_t *doms_new)
6967{
6968 int i, j;
6969
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01006970 lock_doms_cur();
6971
Milton Miller73785472007-10-24 18:23:48 +02006972 /* always unregister in case we don't destroy any domains */
6973 unregister_sched_domain_sysctl();
6974
Paul Jackson029190c2007-10-18 23:40:20 -07006975 if (doms_new == NULL) {
6976 ndoms_new = 1;
6977 doms_new = &fallback_doms;
6978 cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
6979 }
6980
6981 /* Destroy deleted domains */
6982 for (i = 0; i < ndoms_cur; i++) {
6983 for (j = 0; j < ndoms_new; j++) {
6984 if (cpus_equal(doms_cur[i], doms_new[j]))
6985 goto match1;
6986 }
6987 /* no match - a current sched domain not in new doms_new[] */
6988 detach_destroy_domains(doms_cur + i);
6989match1:
6990 ;
6991 }
6992
6993 /* Build new domains */
6994 for (i = 0; i < ndoms_new; i++) {
6995 for (j = 0; j < ndoms_cur; j++) {
6996 if (cpus_equal(doms_new[i], doms_cur[j]))
6997 goto match2;
6998 }
6999 /* no match - add a new doms_new */
7000 build_sched_domains(doms_new + i);
7001match2:
7002 ;
7003 }
7004
7005 /* Remember the new sched domains */
7006 if (doms_cur != &fallback_doms)
7007 kfree(doms_cur);
7008 doms_cur = doms_new;
7009 ndoms_cur = ndoms_new;
Milton Miller73785472007-10-24 18:23:48 +02007010
7011 register_sched_domain_sysctl();
Srivatsa Vaddagiria1835612008-01-25 21:08:00 +01007012
7013 unlock_doms_cur();
Paul Jackson029190c2007-10-18 23:40:20 -07007014}
7015
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007016#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Heiko Carstens9aefd0a2008-03-12 18:31:58 +01007017int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007018{
7019 int err;
7020
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007021 get_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007022 detach_destroy_domains(&cpu_online_map);
7023 err = arch_init_sched_domains(&cpu_online_map);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007024 put_online_cpus();
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007025
7026 return err;
7027}
7028
7029static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
7030{
7031 int ret;
7032
7033 if (buf[0] != '0' && buf[0] != '1')
7034 return -EINVAL;
7035
7036 if (smt)
7037 sched_smt_power_savings = (buf[0] == '1');
7038 else
7039 sched_mc_power_savings = (buf[0] == '1');
7040
7041 ret = arch_reinit_sched_domains();
7042
7043 return ret ? ret : count;
7044}
7045
Adrian Bunk6707de002007-08-12 18:08:19 +02007046#ifdef CONFIG_SCHED_MC
7047static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
7048{
7049 return sprintf(page, "%u\n", sched_mc_power_savings);
7050}
7051static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
7052 const char *buf, size_t count)
7053{
7054 return sched_power_savings_store(buf, count, 0);
7055}
7056static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
7057 sched_mc_power_savings_store);
7058#endif
7059
7060#ifdef CONFIG_SCHED_SMT
7061static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
7062{
7063 return sprintf(page, "%u\n", sched_smt_power_savings);
7064}
7065static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
7066 const char *buf, size_t count)
7067{
7068 return sched_power_savings_store(buf, count, 1);
7069}
7070static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
7071 sched_smt_power_savings_store);
7072#endif
7073
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007074int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
7075{
7076 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07007077
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07007078#ifdef CONFIG_SCHED_SMT
7079 if (smt_capable())
7080 err = sysfs_create_file(&cls->kset.kobj,
7081 &attr_sched_smt_power_savings.attr);
7082#endif
7083#ifdef CONFIG_SCHED_MC
7084 if (!err && mc_capable())
7085 err = sysfs_create_file(&cls->kset.kobj,
7086 &attr_sched_mc_power_savings.attr);
7087#endif
7088 return err;
7089}
7090#endif
7091
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007093 * Force a reinitialization of the sched domains hierarchy. The domains
Linus Torvalds1da177e2005-04-16 15:20:36 -07007094 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07007095 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096 * which will prevent rebalancing while the sched domains are recalculated.
7097 */
7098static int update_sched_domains(struct notifier_block *nfb,
7099 unsigned long action, void *hcpu)
7100{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101 switch (action) {
7102 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007103 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007105 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007106 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107 return NOTIFY_OK;
7108
7109 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007110 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007112 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007114 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07007116 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117 /*
7118 * Fall through and re-initialise the domains.
7119 */
7120 break;
7121 default:
7122 return NOTIFY_DONE;
7123 }
7124
7125 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007126 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127
7128 return NOTIFY_OK;
7129}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130
7131void __init sched_init_smp(void)
7132{
Nick Piggin5c1e1762006-10-03 01:14:04 -07007133 cpumask_t non_isolated_cpus;
7134
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007135 get_online_cpus();
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07007136 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08007137 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007138 if (cpus_empty(non_isolated_cpus))
7139 cpu_set(smp_processor_id(), non_isolated_cpus);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01007140 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 /* XXX: Theoretical race here - CPU may be hotplugged now */
7142 hotcpu_notifier(update_sched_domains, 0);
Nick Piggin5c1e1762006-10-03 01:14:04 -07007143
7144 /* Move init over to a non-isolated CPU */
7145 if (set_cpus_allowed(current, non_isolated_cpus) < 0)
7146 BUG();
Ingo Molnar19978ca2007-11-09 22:39:38 +01007147 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148}
7149#else
7150void __init sched_init_smp(void)
7151{
Ingo Molnar19978ca2007-11-09 22:39:38 +01007152 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153}
7154#endif /* CONFIG_SMP */
7155
7156int in_sched_functions(unsigned long addr)
7157{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 return in_lock_functions(addr) ||
7159 (addr >= (unsigned long)__sched_text_start
7160 && addr < (unsigned long)__sched_text_end);
7161}
7162
Alexey Dobriyana9957442007-10-15 17:00:13 +02007163static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
Ingo Molnardd41f592007-07-09 18:51:59 +02007164{
7165 cfs_rq->tasks_timeline = RB_ROOT;
Ingo Molnardd41f592007-07-09 18:51:59 +02007166#ifdef CONFIG_FAIR_GROUP_SCHED
7167 cfs_rq->rq = rq;
7168#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +02007169 cfs_rq->min_vruntime = (u64)(-(1LL << 20));
Ingo Molnardd41f592007-07-09 18:51:59 +02007170}
7171
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007172static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
7173{
7174 struct rt_prio_array *array;
7175 int i;
7176
7177 array = &rt_rq->active;
7178 for (i = 0; i < MAX_RT_PRIO; i++) {
7179 INIT_LIST_HEAD(array->queue + i);
7180 __clear_bit(i, array->bitmap);
7181 }
7182 /* delimiter for bitsearch: */
7183 __set_bit(MAX_RT_PRIO, array->bitmap);
7184
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007185#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007186 rt_rq->highest_prio = MAX_RT_PRIO;
7187#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007188#ifdef CONFIG_SMP
7189 rt_rq->rt_nr_migratory = 0;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007190 rt_rq->overloaded = 0;
7191#endif
7192
7193 rt_rq->rt_time = 0;
7194 rt_rq->rt_throttled = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007195
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007196#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra23b0fdf2008-02-13 15:45:39 +01007197 rt_rq->rt_nr_boosted = 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007198 rt_rq->rq = rq;
7199#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007200}
7201
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007202#ifdef CONFIG_FAIR_GROUP_SCHED
7203static void init_tg_cfs_entry(struct rq *rq, struct task_group *tg,
7204 struct cfs_rq *cfs_rq, struct sched_entity *se,
7205 int cpu, int add)
7206{
7207 tg->cfs_rq[cpu] = cfs_rq;
7208 init_cfs_rq(cfs_rq, rq);
7209 cfs_rq->tg = tg;
7210 if (add)
7211 list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
7212
7213 tg->se[cpu] = se;
7214 se->cfs_rq = &rq->cfs;
7215 se->my_q = cfs_rq;
7216 se->load.weight = tg->shares;
7217 se->load.inv_weight = div64_64(1ULL<<32, se->load.weight);
7218 se->parent = NULL;
7219}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007220#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007221
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007222#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007223static void init_tg_rt_entry(struct rq *rq, struct task_group *tg,
7224 struct rt_rq *rt_rq, struct sched_rt_entity *rt_se,
7225 int cpu, int add)
7226{
7227 tg->rt_rq[cpu] = rt_rq;
7228 init_rt_rq(rt_rq, rq);
7229 rt_rq->tg = tg;
7230 rt_rq->rt_se = rt_se;
7231 if (add)
7232 list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
7233
7234 tg->rt_se[cpu] = rt_se;
7235 rt_se->rt_rq = &rq->rt;
7236 rt_se->my_q = rt_rq;
7237 rt_se->parent = NULL;
7238 INIT_LIST_HEAD(&rt_se->run_list);
7239}
7240#endif
7241
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242void __init sched_init(void)
7243{
Christoph Lameter476f3532007-05-06 14:48:58 -07007244 int highest_cpu = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007245 int i, j;
7246
Gregory Haskins57d885f2008-01-25 21:08:18 +01007247#ifdef CONFIG_SMP
7248 init_defrootdomain();
7249#endif
7250
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007251#ifdef CONFIG_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007252 list_add(&init_task_group.list, &task_groups);
7253#endif
7254
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08007255 for_each_possible_cpu(i) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07007256 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257
7258 rq = cpu_rq(i);
7259 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07007260 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07007261 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007262 rq->clock = 1;
7263 init_cfs_rq(&rq->cfs, rq);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007264 init_rt_rq(&rq->rt, rq);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007265#ifdef CONFIG_FAIR_GROUP_SCHED
7266 init_task_group.shares = init_task_group_load;
7267 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
7268 init_tg_cfs_entry(rq, &init_task_group,
7269 &per_cpu(init_cfs_rq, i),
7270 &per_cpu(init_sched_entity, i), i, 1);
7271
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007272#endif
7273#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007274 init_task_group.rt_runtime =
7275 sysctl_sched_rt_runtime * NSEC_PER_USEC;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007276 INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
7277 init_tg_rt_entry(rq, &init_task_group,
7278 &per_cpu(init_rt_rq, i),
7279 &per_cpu(init_sched_rt_entity, i), i, 1);
7280#endif
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01007281 rq->rt_period_expire = 0;
Peter Zijlstra48d5e252008-01-25 21:08:31 +01007282 rq->rt_throttled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283
Ingo Molnardd41f592007-07-09 18:51:59 +02007284 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
7285 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07007287 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007288 rq->rd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007289 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007290 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07007292 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007293 rq->migration_thread = NULL;
7294 INIT_LIST_HEAD(&rq->migration_queue);
Gregory Haskinsdc938522008-01-25 21:08:26 +01007295 rq_attach_root(rq, &def_root_domain);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296#endif
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01007297 init_rq_hrtick(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007298 atomic_set(&rq->nr_iowait, 0);
Christoph Lameter476f3532007-05-06 14:48:58 -07007299 highest_cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007300 }
7301
Peter Williams2dd73a42006-06-27 02:54:34 -07007302 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007303
Avi Kivitye107be32007-07-26 13:40:43 +02007304#ifdef CONFIG_PREEMPT_NOTIFIERS
7305 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
7306#endif
7307
Christoph Lameterc9819f42006-12-10 02:20:25 -08007308#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07007309 nr_cpu_ids = highest_cpu + 1;
Christoph Lameterc9819f42006-12-10 02:20:25 -08007310 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
7311#endif
7312
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007313#ifdef CONFIG_RT_MUTEXES
7314 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
7315#endif
7316
Linus Torvalds1da177e2005-04-16 15:20:36 -07007317 /*
7318 * The boot idle thread does lazy MMU switching as well:
7319 */
7320 atomic_inc(&init_mm.mm_count);
7321 enter_lazy_tlb(&init_mm, current);
7322
7323 /*
7324 * Make us the idle thread. Technically, schedule() should not be
7325 * called from this thread, however somewhere below it might be,
7326 * but because we are the idle thread, we just pick up running again
7327 * when this runqueue becomes "idle".
7328 */
7329 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02007330 /*
7331 * During early bootup we pretend to be a normal task:
7332 */
7333 current->sched_class = &fair_sched_class;
Ingo Molnar6892b752008-02-13 14:02:36 +01007334
7335 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336}
7337
7338#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
7339void __might_sleep(char *file, int line)
7340{
Ingo Molnar48f24c42006-07-03 00:25:40 -07007341#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342 static unsigned long prev_jiffy; /* ratelimiting */
7343
7344 if ((in_atomic() || irqs_disabled()) &&
7345 system_state == SYSTEM_RUNNING && !oops_in_progress) {
7346 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
7347 return;
7348 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08007349 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350 " context at %s:%d\n", file, line);
7351 printk("in_atomic():%d, irqs_disabled():%d\n",
7352 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08007353 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08007354 if (irqs_disabled())
7355 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356 dump_stack();
7357 }
7358#endif
7359}
7360EXPORT_SYMBOL(__might_sleep);
7361#endif
7362
7363#ifdef CONFIG_MAGIC_SYSRQ
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007364static void normalize_task(struct rq *rq, struct task_struct *p)
7365{
7366 int on_rq;
7367 update_rq_clock(rq);
7368 on_rq = p->se.on_rq;
7369 if (on_rq)
7370 deactivate_task(rq, p, 0);
7371 __setscheduler(rq, p, SCHED_NORMAL, 0);
7372 if (on_rq) {
7373 activate_task(rq, p, 0);
7374 resched_task(rq->curr);
7375 }
7376}
7377
Linus Torvalds1da177e2005-04-16 15:20:36 -07007378void normalize_rt_tasks(void)
7379{
Ingo Molnara0f98a12007-06-17 18:37:45 +02007380 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07007382 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007384 read_lock_irqsave(&tasklist_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007385 do_each_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02007386 /*
7387 * Only normalize user tasks:
7388 */
7389 if (!p->mm)
7390 continue;
7391
Ingo Molnardd41f592007-07-09 18:51:59 +02007392 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007393#ifdef CONFIG_SCHEDSTATS
7394 p->se.wait_start = 0;
7395 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02007396 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02007397#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02007398 task_rq(p)->clock = 0;
7399
7400 if (!rt_task(p)) {
7401 /*
7402 * Renice negative nice level userspace
7403 * tasks back to 0:
7404 */
7405 if (TASK_NICE(p) < 0 && p->mm)
7406 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02007408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007410 spin_lock(&p->pi_lock);
Ingo Molnarb29739f2006-06-27 02:54:51 -07007411 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412
Ingo Molnar178be792007-10-15 17:00:18 +02007413 normalize_task(rq, p);
Andi Kleen3a5e4dc2007-10-15 17:00:15 +02007414
Ingo Molnarb29739f2006-06-27 02:54:51 -07007415 __task_rq_unlock(rq);
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007416 spin_unlock(&p->pi_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02007417 } while_each_thread(g, p);
7418
Peter Zijlstra4cf5d772008-02-13 15:45:39 +01007419 read_unlock_irqrestore(&tasklist_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007420}
7421
7422#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07007423
7424#ifdef CONFIG_IA64
7425/*
7426 * These functions are only useful for the IA64 MCA handling.
7427 *
7428 * They can only be called when the whole system has been
7429 * stopped - every CPU needs to be quiescent, and no scheduling
7430 * activity can take place. Using them for anything else would
7431 * be a serious bug, and as a result, they aren't even visible
7432 * under any other configuration.
7433 */
7434
7435/**
7436 * curr_task - return the current task for a given cpu.
7437 * @cpu: the processor in question.
7438 *
7439 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7440 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007441struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007442{
7443 return cpu_curr(cpu);
7444}
7445
7446/**
7447 * set_curr_task - set the current task for a given cpu.
7448 * @cpu: the processor in question.
7449 * @p: the task pointer to set.
7450 *
7451 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007452 * are serviced on a separate stack. It allows the architecture to switch the
7453 * notion of the current task on a cpu in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07007454 * must be called with all CPU's synchronized, and interrupts disabled, the
7455 * and caller must save the original value of the current task (see
7456 * curr_task() above) and restore that value before reenabling interrupts and
7457 * re-starting the system.
7458 *
7459 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7460 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007461void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007462{
7463 cpu_curr(cpu) = p;
7464}
7465
7466#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007467
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007468#ifdef CONFIG_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007469
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007470#ifdef CONFIG_FAIR_GROUP_SCHED
7471static void free_fair_sched_group(struct task_group *tg)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007472{
7473 int i;
7474
7475 for_each_possible_cpu(i) {
7476 if (tg->cfs_rq)
7477 kfree(tg->cfs_rq[i]);
7478 if (tg->se)
7479 kfree(tg->se[i]);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007480 }
7481
7482 kfree(tg->cfs_rq);
7483 kfree(tg->se);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007484}
7485
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007486static int alloc_fair_sched_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007487{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007488 struct cfs_rq *cfs_rq;
7489 struct sched_entity *se;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007490 struct rq *rq;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007491 int i;
7492
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007493 tg->cfs_rq = kzalloc(sizeof(cfs_rq) * NR_CPUS, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007494 if (!tg->cfs_rq)
7495 goto err;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007496 tg->se = kzalloc(sizeof(se) * NR_CPUS, GFP_KERNEL);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007497 if (!tg->se)
7498 goto err;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007499
7500 tg->shares = NICE_0_LOAD;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007501
7502 for_each_possible_cpu(i) {
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007503 rq = cpu_rq(i);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007504
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007505 cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
7506 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007507 if (!cfs_rq)
7508 goto err;
7509
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007510 se = kmalloc_node(sizeof(struct sched_entity),
7511 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007512 if (!se)
7513 goto err;
7514
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007515 init_tg_cfs_entry(rq, tg, cfs_rq, se, i, 0);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007516 }
7517
7518 return 1;
7519
7520 err:
7521 return 0;
7522}
7523
7524static inline void register_fair_sched_group(struct task_group *tg, int cpu)
7525{
7526 list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
7527 &cpu_rq(cpu)->leaf_cfs_rq_list);
7528}
7529
7530static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
7531{
7532 list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
7533}
7534#else
7535static inline void free_fair_sched_group(struct task_group *tg)
7536{
7537}
7538
7539static inline int alloc_fair_sched_group(struct task_group *tg)
7540{
7541 return 1;
7542}
7543
7544static inline void register_fair_sched_group(struct task_group *tg, int cpu)
7545{
7546}
7547
7548static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
7549{
7550}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007551#endif
7552
7553#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007554static void free_rt_sched_group(struct task_group *tg)
7555{
7556 int i;
7557
7558 for_each_possible_cpu(i) {
7559 if (tg->rt_rq)
7560 kfree(tg->rt_rq[i]);
7561 if (tg->rt_se)
7562 kfree(tg->rt_se[i]);
7563 }
7564
7565 kfree(tg->rt_rq);
7566 kfree(tg->rt_se);
7567}
7568
7569static int alloc_rt_sched_group(struct task_group *tg)
7570{
7571 struct rt_rq *rt_rq;
7572 struct sched_rt_entity *rt_se;
7573 struct rq *rq;
7574 int i;
7575
7576 tg->rt_rq = kzalloc(sizeof(rt_rq) * NR_CPUS, GFP_KERNEL);
7577 if (!tg->rt_rq)
7578 goto err;
7579 tg->rt_se = kzalloc(sizeof(rt_se) * NR_CPUS, GFP_KERNEL);
7580 if (!tg->rt_se)
7581 goto err;
7582
7583 tg->rt_runtime = 0;
7584
7585 for_each_possible_cpu(i) {
7586 rq = cpu_rq(i);
7587
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007588 rt_rq = kmalloc_node(sizeof(struct rt_rq),
7589 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
7590 if (!rt_rq)
7591 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007592
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007593 rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
7594 GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
7595 if (!rt_se)
7596 goto err;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007597
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007598 init_tg_rt_entry(rq, tg, rt_rq, rt_se, i, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007599 }
7600
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007601 return 1;
7602
7603 err:
7604 return 0;
7605}
7606
7607static inline void register_rt_sched_group(struct task_group *tg, int cpu)
7608{
7609 list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
7610 &cpu_rq(cpu)->leaf_rt_rq_list);
7611}
7612
7613static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
7614{
7615 list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
7616}
7617#else
7618static inline void free_rt_sched_group(struct task_group *tg)
7619{
7620}
7621
7622static inline int alloc_rt_sched_group(struct task_group *tg)
7623{
7624 return 1;
7625}
7626
7627static inline void register_rt_sched_group(struct task_group *tg, int cpu)
7628{
7629}
7630
7631static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
7632{
7633}
7634#endif
7635
7636static void free_sched_group(struct task_group *tg)
7637{
7638 free_fair_sched_group(tg);
7639 free_rt_sched_group(tg);
7640 kfree(tg);
7641}
7642
7643/* allocate runqueue etc for a new task group */
7644struct task_group *sched_create_group(void)
7645{
7646 struct task_group *tg;
7647 unsigned long flags;
7648 int i;
7649
7650 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
7651 if (!tg)
7652 return ERR_PTR(-ENOMEM);
7653
7654 if (!alloc_fair_sched_group(tg))
7655 goto err;
7656
7657 if (!alloc_rt_sched_group(tg))
7658 goto err;
7659
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007660 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007661 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007662 register_fair_sched_group(tg, i);
7663 register_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007664 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007665 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007666 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007667
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007668 return tg;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007669
7670err:
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007671 free_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007672 return ERR_PTR(-ENOMEM);
7673}
7674
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007675/* rcu callback to free various structures associated with a task group */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007676static void free_sched_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007677{
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007678 /* now it should be safe to free those cfs_rqs */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007679 free_sched_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007680}
7681
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007682/* Destroy runqueue etc associated with a task group */
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007683void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007684{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007685 unsigned long flags;
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007686 int i;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007687
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007688 spin_lock_irqsave(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007689 for_each_possible_cpu(i) {
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007690 unregister_fair_sched_group(tg, i);
7691 unregister_rt_sched_group(tg, i);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007692 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007693 list_del_rcu(&tg->list);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007694 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007695
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007696 /* wait for possible concurrent references to cfs_rqs complete */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007697 call_rcu(&tg->rcu, free_sched_group_rcu);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007698}
7699
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007700/* change task's runqueue when it moves between groups.
Ingo Molnar3a252012007-10-15 17:00:12 +02007701 * The caller of this function should have put the task in its new group
7702 * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
7703 * reflect its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007704 */
7705void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007706{
7707 int on_rq, running;
7708 unsigned long flags;
7709 struct rq *rq;
7710
7711 rq = task_rq_lock(tsk, &flags);
7712
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007713 update_rq_clock(rq);
7714
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01007715 running = task_current(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007716 on_rq = tsk->se.on_rq;
7717
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007718 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007719 dequeue_task(rq, tsk, 0);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007720 if (unlikely(running))
7721 tsk->sched_class->put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007722
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007723 set_task_rq(tsk, task_cpu(tsk));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007724
Peter Zijlstra810b3812008-02-29 15:21:01 -05007725#ifdef CONFIG_FAIR_GROUP_SCHED
7726 if (tsk->sched_class->moved_group)
7727 tsk->sched_class->moved_group(tsk);
7728#endif
7729
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07007730 if (unlikely(running))
7731 tsk->sched_class->set_curr_task(rq);
7732 if (on_rq)
Dmitry Adamushko7074bad2007-10-15 17:00:07 +02007733 enqueue_task(rq, tsk, 0);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007734
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007735 task_rq_unlock(rq, &flags);
7736}
7737
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007738#ifdef CONFIG_FAIR_GROUP_SCHED
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007739static void set_se_shares(struct sched_entity *se, unsigned long shares)
7740{
7741 struct cfs_rq *cfs_rq = se->cfs_rq;
7742 struct rq *rq = cfs_rq->rq;
7743 int on_rq;
7744
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007745 spin_lock_irq(&rq->lock);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007746
7747 on_rq = se->on_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007748 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007749 dequeue_entity(cfs_rq, se, 0);
7750
7751 se->load.weight = shares;
7752 se->load.inv_weight = div64_64((1ULL<<32), shares);
7753
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007754 if (on_rq)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007755 enqueue_entity(cfs_rq, se, 0);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007756
7757 spin_unlock_irq(&rq->lock);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007758}
7759
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007760static DEFINE_MUTEX(shares_mutex);
7761
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007762int sched_group_set_shares(struct task_group *tg, unsigned long shares)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007763{
7764 int i;
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007765 unsigned long flags;
Ingo Molnarc61935f2008-01-22 11:24:58 +01007766
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007767 /*
7768 * A weight of 0 or 1 can cause arithmetics problems.
7769 * (The default weight is 1024 - so there's no practical
7770 * limitation from this.)
7771 */
7772 if (shares < 2)
7773 shares = 2;
7774
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007775 mutex_lock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007776 if (tg->shares == shares)
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007777 goto done;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007778
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007779 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007780 for_each_possible_cpu(i)
7781 unregister_fair_sched_group(tg, i);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007782 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007783
7784 /* wait for any ongoing reference to this group to finish */
7785 synchronize_sched();
7786
7787 /*
7788 * Now we are free to modify the group's share on each cpu
7789 * w/o tripping rebalance_share or load_balance_fair.
7790 */
7791 tg->shares = shares;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01007792 for_each_possible_cpu(i)
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007793 set_se_shares(tg->se[i], shares);
Srivatsa Vaddagiri6b2d7702008-01-25 21:08:00 +01007794
7795 /*
7796 * Enable load balance activity on this group, by inserting it back on
7797 * each cpu's rq->leaf_cfs_rq_list.
7798 */
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007799 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007800 for_each_possible_cpu(i)
7801 register_fair_sched_group(tg, i);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007802 spin_unlock_irqrestore(&task_group_lock, flags);
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007803done:
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007804 mutex_unlock(&shares_mutex);
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007805 return 0;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007806}
7807
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007808unsigned long sched_group_shares(struct task_group *tg)
7809{
7810 return tg->shares;
7811}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007812#endif
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007813
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007814#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007815/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007816 * Ensure that the real time constraints are schedulable.
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007817 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007818static DEFINE_MUTEX(rt_constraints_mutex);
7819
7820static unsigned long to_ratio(u64 period, u64 runtime)
7821{
7822 if (runtime == RUNTIME_INF)
7823 return 1ULL << 16;
7824
Peter Zijlstra2692a242008-02-27 12:00:46 +01007825 return div64_64(runtime << 16, period);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007826}
7827
7828static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007829{
7830 struct task_group *tgi;
7831 unsigned long total = 0;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007832 unsigned long global_ratio =
7833 to_ratio(sysctl_sched_rt_period,
7834 sysctl_sched_rt_runtime < 0 ?
7835 RUNTIME_INF : sysctl_sched_rt_runtime);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007836
7837 rcu_read_lock();
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007838 list_for_each_entry_rcu(tgi, &task_groups, list) {
7839 if (tgi == tg)
7840 continue;
7841
7842 total += to_ratio(period, tgi->rt_runtime);
7843 }
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007844 rcu_read_unlock();
7845
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007846 return total + to_ratio(period, runtime) < global_ratio;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007847}
7848
Dhaval Giani521f1a242008-02-28 15:21:56 +05307849/* Must be called with tasklist_lock held */
7850static inline int tg_has_rt_tasks(struct task_group *tg)
7851{
7852 struct task_struct *g, *p;
7853 do_each_thread(g, p) {
7854 if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
7855 return 1;
7856 } while_each_thread(g, p);
7857 return 0;
7858}
7859
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007860int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007861{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007862 u64 rt_runtime, rt_period;
7863 int err = 0;
7864
Peter Zijlstra2692a242008-02-27 12:00:46 +01007865 rt_period = (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007866 rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
7867 if (rt_runtime_us == -1)
Peter Zijlstra2692a242008-02-27 12:00:46 +01007868 rt_runtime = RUNTIME_INF;
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007869
7870 mutex_lock(&rt_constraints_mutex);
Dhaval Giani521f1a242008-02-28 15:21:56 +05307871 read_lock(&tasklist_lock);
7872 if (rt_runtime_us == 0 && tg_has_rt_tasks(tg)) {
7873 err = -EBUSY;
7874 goto unlock;
7875 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007876 if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
7877 err = -EINVAL;
7878 goto unlock;
7879 }
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007880 tg->rt_runtime = rt_runtime;
7881 unlock:
Dhaval Giani521f1a242008-02-28 15:21:56 +05307882 read_unlock(&tasklist_lock);
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007883 mutex_unlock(&rt_constraints_mutex);
7884
7885 return err;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007886}
7887
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007888long sched_group_rt_runtime(struct task_group *tg)
7889{
7890 u64 rt_runtime_us;
7891
7892 if (tg->rt_runtime == RUNTIME_INF)
7893 return -1;
7894
7895 rt_runtime_us = tg->rt_runtime;
7896 do_div(rt_runtime_us, NSEC_PER_USEC);
7897 return rt_runtime_us;
7898}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007899#endif
7900#endif /* CONFIG_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007901
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007902#ifdef CONFIG_CGROUP_SCHED
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007903
7904/* return corresponding task_group object of a cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02007905static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007906{
Paul Menage2b01dfe2007-10-24 18:23:50 +02007907 return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
7908 struct task_group, css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007909}
7910
7911static struct cgroup_subsys_state *
Paul Menage2b01dfe2007-10-24 18:23:50 +02007912cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007913{
7914 struct task_group *tg;
7915
Paul Menage2b01dfe2007-10-24 18:23:50 +02007916 if (!cgrp->parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007917 /* This is early initialization for the top cgroup */
Paul Menage2b01dfe2007-10-24 18:23:50 +02007918 init_task_group.css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007919 return &init_task_group.css;
7920 }
7921
7922 /* we support only 1-level deep hierarchical scheduler atm */
Paul Menage2b01dfe2007-10-24 18:23:50 +02007923 if (cgrp->parent->parent)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007924 return ERR_PTR(-EINVAL);
7925
7926 tg = sched_create_group();
7927 if (IS_ERR(tg))
7928 return ERR_PTR(-ENOMEM);
7929
7930 /* Bind the cgroup to task_group object we just created */
Paul Menage2b01dfe2007-10-24 18:23:50 +02007931 tg->css.cgroup = cgrp;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007932
7933 return &tg->css;
7934}
7935
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007936static void
7937cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007938{
Paul Menage2b01dfe2007-10-24 18:23:50 +02007939 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007940
7941 sched_destroy_group(tg);
7942}
7943
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007944static int
7945cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
7946 struct task_struct *tsk)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007947{
Peter Zijlstrab68aa232008-02-13 15:45:40 +01007948#ifdef CONFIG_RT_GROUP_SCHED
7949 /* Don't accept realtime tasks when there is no way for them to run */
7950 if (rt_task(tsk) && cgroup_tg(cgrp)->rt_runtime == 0)
7951 return -EINVAL;
7952#else
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007953 /* We don't support RT-tasks being in separate groups */
7954 if (tsk->sched_class != &fair_sched_class)
7955 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01007956#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007957
7958 return 0;
7959}
7960
7961static void
Paul Menage2b01dfe2007-10-24 18:23:50 +02007962cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007963 struct cgroup *old_cont, struct task_struct *tsk)
7964{
7965 sched_move_task(tsk);
7966}
7967
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007968#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menage2b01dfe2007-10-24 18:23:50 +02007969static int cpu_shares_write_uint(struct cgroup *cgrp, struct cftype *cftype,
7970 u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007971{
Paul Menage2b01dfe2007-10-24 18:23:50 +02007972 return sched_group_set_shares(cgroup_tg(cgrp), shareval);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007973}
7974
Paul Menage2b01dfe2007-10-24 18:23:50 +02007975static u64 cpu_shares_read_uint(struct cgroup *cgrp, struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007976{
Paul Menage2b01dfe2007-10-24 18:23:50 +02007977 struct task_group *tg = cgroup_tg(cgrp);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007978
7979 return (u64) tg->shares;
7980}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007981#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007982
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007983#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007984static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
7985 struct file *file,
7986 const char __user *userbuf,
7987 size_t nbytes, loff_t *unused_ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007988{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007989 char buffer[64];
7990 int retval = 0;
7991 s64 val;
7992 char *end;
7993
7994 if (!nbytes)
7995 return -EINVAL;
7996 if (nbytes >= sizeof(buffer))
7997 return -E2BIG;
7998 if (copy_from_user(buffer, userbuf, nbytes))
7999 return -EFAULT;
8000
8001 buffer[nbytes] = 0; /* nul-terminate */
8002
8003 /* strip newline if necessary */
8004 if (nbytes && (buffer[nbytes-1] == '\n'))
8005 buffer[nbytes-1] = 0;
8006 val = simple_strtoll(buffer, &end, 0);
8007 if (*end)
8008 return -EINVAL;
8009
8010 /* Pass to subsystem */
8011 retval = sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
8012 if (!retval)
8013 retval = nbytes;
8014 return retval;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008015}
8016
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008017static ssize_t cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft,
8018 struct file *file,
8019 char __user *buf, size_t nbytes,
8020 loff_t *ppos)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008021{
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008022 char tmp[64];
8023 long val = sched_group_rt_runtime(cgroup_tg(cgrp));
8024 int len = sprintf(tmp, "%ld\n", val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008025
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008026 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008027}
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008028#endif
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008029
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008030static struct cftype cpu_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008031#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008032 {
8033 .name = "shares",
8034 .read_uint = cpu_shares_read_uint,
8035 .write_uint = cpu_shares_write_uint,
8036 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008037#endif
8038#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008039 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008040 .name = "rt_runtime_us",
8041 .read = cpu_rt_runtime_read,
8042 .write = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008043 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008044#endif
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008045};
8046
8047static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
8048{
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008049 return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008050}
8051
8052struct cgroup_subsys cpu_cgroup_subsys = {
Ingo Molnar38605ca2007-10-29 21:18:11 +01008053 .name = "cpu",
8054 .create = cpu_cgroup_create,
8055 .destroy = cpu_cgroup_destroy,
8056 .can_attach = cpu_cgroup_can_attach,
8057 .attach = cpu_cgroup_attach,
8058 .populate = cpu_cgroup_populate,
8059 .subsys_id = cpu_cgroup_subsys_id,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008060 .early_init = 1,
8061};
8062
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008063#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008064
8065#ifdef CONFIG_CGROUP_CPUACCT
8066
8067/*
8068 * CPU accounting code for task groups.
8069 *
8070 * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
8071 * (balbir@in.ibm.com).
8072 */
8073
8074/* track cpu usage of a group of tasks */
8075struct cpuacct {
8076 struct cgroup_subsys_state css;
8077 /* cpuusage holds pointer to a u64-type object on every cpu */
8078 u64 *cpuusage;
8079};
8080
8081struct cgroup_subsys cpuacct_subsys;
8082
8083/* return cpu accounting group corresponding to this container */
8084static inline struct cpuacct *cgroup_ca(struct cgroup *cont)
8085{
8086 return container_of(cgroup_subsys_state(cont, cpuacct_subsys_id),
8087 struct cpuacct, css);
8088}
8089
8090/* return cpu accounting group to which this task belongs */
8091static inline struct cpuacct *task_ca(struct task_struct *tsk)
8092{
8093 return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
8094 struct cpuacct, css);
8095}
8096
8097/* create a new cpu accounting group */
8098static struct cgroup_subsys_state *cpuacct_create(
8099 struct cgroup_subsys *ss, struct cgroup *cont)
8100{
8101 struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
8102
8103 if (!ca)
8104 return ERR_PTR(-ENOMEM);
8105
8106 ca->cpuusage = alloc_percpu(u64);
8107 if (!ca->cpuusage) {
8108 kfree(ca);
8109 return ERR_PTR(-ENOMEM);
8110 }
8111
8112 return &ca->css;
8113}
8114
8115/* destroy an existing cpu accounting group */
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01008116static void
8117cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cont)
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008118{
8119 struct cpuacct *ca = cgroup_ca(cont);
8120
8121 free_percpu(ca->cpuusage);
8122 kfree(ca);
8123}
8124
8125/* return total cpu usage (in nanoseconds) of a group */
8126static u64 cpuusage_read(struct cgroup *cont, struct cftype *cft)
8127{
8128 struct cpuacct *ca = cgroup_ca(cont);
8129 u64 totalcpuusage = 0;
8130 int i;
8131
8132 for_each_possible_cpu(i) {
8133 u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
8134
8135 /*
8136 * Take rq->lock to make 64-bit addition safe on 32-bit
8137 * platforms.
8138 */
8139 spin_lock_irq(&cpu_rq(i)->lock);
8140 totalcpuusage += *cpuusage;
8141 spin_unlock_irq(&cpu_rq(i)->lock);
8142 }
8143
8144 return totalcpuusage;
8145}
8146
8147static struct cftype files[] = {
8148 {
8149 .name = "usage",
8150 .read_uint = cpuusage_read,
8151 },
8152};
8153
8154static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cont)
8155{
8156 return cgroup_add_files(cont, ss, files, ARRAY_SIZE(files));
8157}
8158
8159/*
8160 * charge this task's execution time to its accounting group.
8161 *
8162 * called with rq->lock held.
8163 */
8164static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
8165{
8166 struct cpuacct *ca;
8167
8168 if (!cpuacct_subsys.active)
8169 return;
8170
8171 ca = task_ca(tsk);
8172 if (ca) {
8173 u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
8174
8175 *cpuusage += cputime;
8176 }
8177}
8178
8179struct cgroup_subsys cpuacct_subsys = {
8180 .name = "cpuacct",
8181 .create = cpuacct_create,
8182 .destroy = cpuacct_destroy,
8183 .populate = cpuacct_populate,
8184 .subsys_id = cpuacct_subsys_id,
8185};
8186#endif /* CONFIG_CGROUP_CPUACCT */