blob: 5594e65166fc771d658b0073107a3977f99ed11a [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
27#include <linux/mm.h>
28#include <linux/module.h>
29#include <linux/nmi.h>
30#include <linux/init.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020031#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/highmem.h>
33#include <linux/smp_lock.h>
34#include <asm/mmu_context.h>
35#include <linux/interrupt.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080036#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/completion.h>
38#include <linux/kernel_stat.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070039#include <linux/debug_locks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/security.h>
41#include <linux/notifier.h>
42#include <linux/profile.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080043#include <linux/freezer.h>
akpm@osdl.org198e2f12006-01-12 01:05:30 -080044#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/blkdev.h>
46#include <linux/delay.h>
47#include <linux/smp.h>
48#include <linux/threads.h>
49#include <linux/timer.h>
50#include <linux/rcupdate.h>
51#include <linux/cpu.h>
52#include <linux/cpuset.h>
53#include <linux/percpu.h>
54#include <linux/kthread.h>
55#include <linux/seq_file.h>
Nick Piggine692ab52007-07-26 13:40:43 +020056#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/syscalls.h>
58#include <linux/times.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070059#include <linux/tsacct_kern.h>
bibo maoc6fd91f2006-03-26 01:38:20 -080060#include <linux/kprobes.h>
Shailabh Nagar0ff92242006-07-14 00:24:37 -070061#include <linux/delayacct.h>
Eric Dumazet5517d862007-05-08 00:32:57 -070062#include <linux/reciprocal_div.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020063#include <linux/unistd.h>
Jens Axboef5ff8422007-09-21 09:19:54 +020064#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Eric Dumazet5517d862007-05-08 00:32:57 -070066#include <asm/tlb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/*
Alexey Dobriyanb035b6d2007-02-10 01:45:10 -080069 * Scheduler clock - returns current time in nanosec units.
70 * This is default implementation.
71 * Architectures and sub-architectures can override this.
72 */
73unsigned long long __attribute__((weak)) sched_clock(void)
74{
75 return (unsigned long long)jiffies * (1000000000 / HZ);
76}
77
78/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * Convert user-nice values [ -20 ... 0 ... 19 ]
80 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
81 * and back.
82 */
83#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
84#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
85#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
86
87/*
88 * 'User priority' is the nice value converted to something we
89 * can work with better when scaling various scheduler parameters,
90 * it's a [ 0 ... 39 ] range.
91 */
92#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
93#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
94#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
95
96/*
97 * Some helpers for converting nanosecond timing to jiffy resolution
98 */
99#define NS_TO_JIFFIES(TIME) ((TIME) / (1000000000 / HZ))
100#define JIFFIES_TO_NS(TIME) ((TIME) * (1000000000 / HZ))
101
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200102#define NICE_0_LOAD SCHED_LOAD_SCALE
103#define NICE_0_SHIFT SCHED_LOAD_SHIFT
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/*
106 * These are the 'tuning knobs' of the scheduler:
107 *
108 * Minimum timeslice is 5 msecs (or 1 jiffy, whichever is larger),
109 * default timeslice is 100 msecs, maximum timeslice is 800 msecs.
110 * Timeslices get refilled after they expire.
111 */
112#define MIN_TIMESLICE max(5 * HZ / 1000, 1)
113#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700114
Eric Dumazet5517d862007-05-08 00:32:57 -0700115#ifdef CONFIG_SMP
116/*
117 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
118 * Since cpu_power is a 'constant', we can use a reciprocal divide.
119 */
120static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
121{
122 return reciprocal_divide(load, sg->reciprocal_cpu_power);
123}
124
125/*
126 * Each time a sched group cpu_power is changed,
127 * we must compute its reciprocal value
128 */
129static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
130{
131 sg->__cpu_power += val;
132 sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
133}
134#endif
135
Ingo Molnar634fa8c2007-07-09 18:52:00 +0200136#define SCALE_PRIO(x, prio) \
137 max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO / 2), MIN_TIMESLICE)
Borislav Petkov91fcdd42006-10-19 23:28:29 -0700138
Ingo Molnar634fa8c2007-07-09 18:52:00 +0200139/*
140 * static_prio_timeslice() scales user-nice values [ -20 ... 0 ... 19 ]
141 * to time slice values: [800ms ... 100ms ... 5ms]
142 */
143static unsigned int static_prio_timeslice(int static_prio)
Peter Williams2dd73a42006-06-27 02:54:34 -0700144{
Ingo Molnar634fa8c2007-07-09 18:52:00 +0200145 if (static_prio == NICE_TO_PRIO(19))
146 return 1;
147
148 if (static_prio < NICE_TO_PRIO(0))
149 return SCALE_PRIO(DEF_TIMESLICE * 4, static_prio);
150 else
151 return SCALE_PRIO(DEF_TIMESLICE, static_prio);
Peter Williams2dd73a42006-06-27 02:54:34 -0700152}
153
Ingo Molnare05606d2007-07-09 18:51:59 +0200154static inline int rt_policy(int policy)
155{
156 if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR))
157 return 1;
158 return 0;
159}
160
161static inline int task_has_rt_policy(struct task_struct *p)
162{
163 return rt_policy(p->policy);
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200167 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200169struct rt_prio_array {
170 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
171 struct list_head queue[MAX_RT_PRIO];
172};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200174struct load_stat {
175 struct load_weight load;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200176};
177
178/* CFS-related fields in a runqueue */
179struct cfs_rq {
180 struct load_weight load;
181 unsigned long nr_running;
182
183 s64 fair_clock;
184 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200185 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200186 s64 wait_runtime;
187 u64 sleeper_bonus;
188 unsigned long wait_runtime_overruns, wait_runtime_underruns;
189
190 struct rb_root tasks_timeline;
191 struct rb_node *rb_leftmost;
192 struct rb_node *rb_load_balance_curr;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200193 /* 'curr' points to currently running entity on this cfs_rq.
194 * It is set to NULL otherwise (i.e when none are currently running).
195 */
196 struct sched_entity *curr;
Ingo Molnar62160e32007-10-15 17:00:03 +0200197#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200198 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
199
200 /* leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
201 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
202 * (like users, containers etc.)
203 *
204 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
205 * list is used during load balance.
206 */
207 struct list_head leaf_cfs_rq_list; /* Better name : task_cfs_rq_list? */
208#endif
209};
210
211/* Real-Time classes' related field in a runqueue: */
212struct rt_rq {
213 struct rt_prio_array active;
214 int rt_load_balance_idx;
215 struct list_head *rt_load_balance_head, *rt_load_balance_curr;
216};
217
218/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * This is the main, per-CPU runqueue data structure.
220 *
221 * Locking rule: those places that want to lock multiple runqueues
222 * (such as the load balancing or the thread migration code), lock
223 * acquire operations must be ordered by ascending &runqueue.
224 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700225struct rq {
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200226 spinlock_t lock; /* runqueue lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 /*
229 * nr_running and cpu_load should be in the same cacheline because
230 * remote CPUs use both these fields when doing load calculation.
231 */
232 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200233 #define CPU_LOAD_IDX_MAX 5
234 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700235 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700236#ifdef CONFIG_NO_HZ
237 unsigned char in_nohz_recently;
238#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200239 struct load_stat ls; /* capture load from *all* tasks on this cpu */
240 unsigned long nr_load_updates;
241 u64 nr_switches;
242
243 struct cfs_rq cfs;
244#ifdef CONFIG_FAIR_GROUP_SCHED
245 struct list_head leaf_cfs_rq_list; /* list of leaf cfs_rq on this cpu */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200247 struct rt_rq rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 /*
250 * This is part of a global counter where only the total sum
251 * over all CPUs matters. A task can increase this counter on
252 * one CPU and if it got migrated afterwards it may decrease
253 * it on another CPU. Always updated under the runqueue lock:
254 */
255 unsigned long nr_uninterruptible;
256
Ingo Molnar36c8b582006-07-03 00:25:41 -0700257 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800258 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200260
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200261 u64 clock, prev_clock_raw;
262 s64 clock_max_delta;
263
264 unsigned int clock_warps, clock_overflows;
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200265 u64 idle_clock;
266 unsigned int clock_deep_idle_events;
Ingo Molnar529c7722007-08-10 23:05:11 +0200267 u64 tick_timestamp;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 atomic_t nr_iowait;
270
271#ifdef CONFIG_SMP
272 struct sched_domain *sd;
273
274 /* For active balancing */
275 int active_balance;
276 int push_cpu;
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700277 int cpu; /* cpu of this runqueue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Ingo Molnar36c8b582006-07-03 00:25:41 -0700279 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 struct list_head migration_queue;
281#endif
282
283#ifdef CONFIG_SCHEDSTATS
284 /* latency stats */
285 struct sched_info rq_sched_info;
286
287 /* sys_sched_yield() stats */
288 unsigned long yld_exp_empty;
289 unsigned long yld_act_empty;
290 unsigned long yld_both_empty;
291 unsigned long yld_cnt;
292
293 /* schedule() stats */
294 unsigned long sched_switch;
295 unsigned long sched_cnt;
296 unsigned long sched_goidle;
297
298 /* try_to_wake_up() stats */
299 unsigned long ttwu_cnt;
300 unsigned long ttwu_local;
301#endif
Ingo Molnarfcb99372006-07-03 00:25:10 -0700302 struct lock_class_key rq_lock_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303};
304
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700305static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Gautham R Shenoy5be93612007-05-09 02:34:04 -0700306static DEFINE_MUTEX(sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Ingo Molnardd41f592007-07-09 18:51:59 +0200308static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
309{
310 rq->curr->sched_class->check_preempt_curr(rq, p);
311}
312
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700313static inline int cpu_of(struct rq *rq)
314{
315#ifdef CONFIG_SMP
316 return rq->cpu;
317#else
318 return 0;
319#endif
320}
321
Nick Piggin674311d2005-06-25 14:57:27 -0700322/*
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200323 * Update the per-runqueue clock, as finegrained as the platform can give
324 * us, but without assuming monotonicity, etc.:
Ingo Molnar20d315d2007-07-09 18:51:58 +0200325 */
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200326static void __update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200327{
328 u64 prev_raw = rq->prev_clock_raw;
329 u64 now = sched_clock();
330 s64 delta = now - prev_raw;
331 u64 clock = rq->clock;
332
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200333#ifdef CONFIG_SCHED_DEBUG
334 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
335#endif
Ingo Molnar20d315d2007-07-09 18:51:58 +0200336 /*
337 * Protect against sched_clock() occasionally going backwards:
338 */
339 if (unlikely(delta < 0)) {
340 clock++;
341 rq->clock_warps++;
342 } else {
343 /*
344 * Catch too large forward jumps too:
345 */
Ingo Molnar529c7722007-08-10 23:05:11 +0200346 if (unlikely(clock + delta > rq->tick_timestamp + TICK_NSEC)) {
347 if (clock < rq->tick_timestamp + TICK_NSEC)
348 clock = rq->tick_timestamp + TICK_NSEC;
349 else
350 clock++;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200351 rq->clock_overflows++;
352 } else {
353 if (unlikely(delta > rq->clock_max_delta))
354 rq->clock_max_delta = delta;
355 clock += delta;
356 }
357 }
358
359 rq->prev_clock_raw = now;
360 rq->clock = clock;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200361}
362
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200363static void update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200364{
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200365 if (likely(smp_processor_id() == cpu_of(rq)))
366 __update_rq_clock(rq);
367}
Ingo Molnar20d315d2007-07-09 18:51:58 +0200368
Ingo Molnar20d315d2007-07-09 18:51:58 +0200369/*
Nick Piggin674311d2005-06-25 14:57:27 -0700370 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700371 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700372 *
373 * The domain tree of any CPU may only be accessed from within
374 * preempt-disabled sections.
375 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700376#define for_each_domain(cpu, __sd) \
377 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
380#define this_rq() (&__get_cpu_var(runqueues))
381#define task_rq(p) cpu_rq(task_cpu(p))
382#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
383
Ingo Molnare436d802007-07-19 21:28:35 +0200384/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200385 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
386 */
387#ifdef CONFIG_SCHED_DEBUG
388# define const_debug __read_mostly
389#else
390# define const_debug static const
391#endif
392
393/*
394 * Debugging: various feature bits
395 */
396enum {
397 SCHED_FEAT_FAIR_SLEEPERS = 1,
398 SCHED_FEAT_NEW_FAIR_SLEEPERS = 2,
399 SCHED_FEAT_SLEEPER_AVG = 4,
400 SCHED_FEAT_SLEEPER_LOAD_AVG = 8,
401 SCHED_FEAT_START_DEBIT = 16,
402 SCHED_FEAT_SKIP_INITIAL = 32,
403};
404
405const_debug unsigned int sysctl_sched_features =
406 SCHED_FEAT_FAIR_SLEEPERS *0 |
407 SCHED_FEAT_NEW_FAIR_SLEEPERS *1 |
408 SCHED_FEAT_SLEEPER_AVG *0 |
409 SCHED_FEAT_SLEEPER_LOAD_AVG *1 |
410 SCHED_FEAT_START_DEBIT *1 |
411 SCHED_FEAT_SKIP_INITIAL *0;
412
413#define sched_feat(x) (sysctl_sched_features & SCHED_FEAT_##x)
414
415/*
Ingo Molnare436d802007-07-19 21:28:35 +0200416 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
417 * clock constructed from sched_clock():
418 */
419unsigned long long cpu_clock(int cpu)
420{
Ingo Molnare436d802007-07-19 21:28:35 +0200421 unsigned long long now;
422 unsigned long flags;
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200423 struct rq *rq;
Ingo Molnare436d802007-07-19 21:28:35 +0200424
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200425 local_irq_save(flags);
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200426 rq = cpu_rq(cpu);
427 update_rq_clock(rq);
428 now = rq->clock;
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200429 local_irq_restore(flags);
Ingo Molnare436d802007-07-19 21:28:35 +0200430
431 return now;
432}
433
Ingo Molnar138a8ae2007-07-09 18:51:58 +0200434#ifdef CONFIG_FAIR_GROUP_SCHED
435/* Change a task's ->cfs_rq if it moves across CPUs */
436static inline void set_task_cfs_rq(struct task_struct *p)
437{
438 p->se.cfs_rq = &task_rq(p)->cfs;
439}
440#else
441static inline void set_task_cfs_rq(struct task_struct *p)
442{
443}
444#endif
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700447# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700449#ifndef finish_arch_switch
450# define finish_arch_switch(prev) do { } while (0)
451#endif
452
453#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700454static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700455{
456 return rq->curr == p;
457}
458
Ingo Molnar70b97a72006-07-03 00:25:42 -0700459static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700460{
461}
462
Ingo Molnar70b97a72006-07-03 00:25:42 -0700463static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700464{
Ingo Molnarda04c032005-09-13 11:17:59 +0200465#ifdef CONFIG_DEBUG_SPINLOCK
466 /* this is a valid case when another task releases the spinlock */
467 rq->lock.owner = current;
468#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700469 /*
470 * If we are tracking spinlock dependencies then we have to
471 * fix up the runqueue lock - which gets 'carried over' from
472 * prev into current:
473 */
474 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
475
Nick Piggin4866cde2005-06-25 14:57:23 -0700476 spin_unlock_irq(&rq->lock);
477}
478
479#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700480static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700481{
482#ifdef CONFIG_SMP
483 return p->oncpu;
484#else
485 return rq->curr == p;
486#endif
487}
488
Ingo Molnar70b97a72006-07-03 00:25:42 -0700489static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700490{
491#ifdef CONFIG_SMP
492 /*
493 * We can optimise this out completely for !SMP, because the
494 * SMP rebalancing from interrupt is the only thing that cares
495 * here.
496 */
497 next->oncpu = 1;
498#endif
499#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
500 spin_unlock_irq(&rq->lock);
501#else
502 spin_unlock(&rq->lock);
503#endif
504}
505
Ingo Molnar70b97a72006-07-03 00:25:42 -0700506static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700507{
508#ifdef CONFIG_SMP
509 /*
510 * After ->oncpu is cleared, the task can be moved to a different CPU.
511 * We must ensure this doesn't happen until the switch is completely
512 * finished.
513 */
514 smp_wmb();
515 prev->oncpu = 0;
516#endif
517#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
518 local_irq_enable();
519#endif
520}
521#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700524 * __task_rq_lock - lock the runqueue a given task resides on.
525 * Must be called interrupts disabled.
526 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700527static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700528 __acquires(rq->lock)
529{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700530 struct rq *rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700531
532repeat_lock_task:
533 rq = task_rq(p);
534 spin_lock(&rq->lock);
535 if (unlikely(rq != task_rq(p))) {
536 spin_unlock(&rq->lock);
537 goto repeat_lock_task;
538 }
539 return rq;
540}
541
542/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 * task_rq_lock - lock the runqueue a given task resides on and disable
544 * interrupts. Note the ordering: we can safely lookup the task_rq without
545 * explicitly disabling preemption.
546 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700547static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 __acquires(rq->lock)
549{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700550 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552repeat_lock_task:
553 local_irq_save(*flags);
554 rq = task_rq(p);
555 spin_lock(&rq->lock);
556 if (unlikely(rq != task_rq(p))) {
557 spin_unlock_irqrestore(&rq->lock, *flags);
558 goto repeat_lock_task;
559 }
560 return rq;
561}
562
Ingo Molnar70b97a72006-07-03 00:25:42 -0700563static inline void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700564 __releases(rq->lock)
565{
566 spin_unlock(&rq->lock);
567}
568
Ingo Molnar70b97a72006-07-03 00:25:42 -0700569static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 __releases(rq->lock)
571{
572 spin_unlock_irqrestore(&rq->lock, *flags);
573}
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800576 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700578static inline struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 __acquires(rq->lock)
580{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700581 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 local_irq_disable();
584 rq = this_rq();
585 spin_lock(&rq->lock);
586
587 return rq;
588}
589
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200590/*
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200591 * We are going deep-idle (irqs are disabled):
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200592 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200593void sched_clock_idle_sleep_event(void)
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200594{
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200595 struct rq *rq = cpu_rq(smp_processor_id());
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200596
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200597 spin_lock(&rq->lock);
598 __update_rq_clock(rq);
599 spin_unlock(&rq->lock);
600 rq->clock_deep_idle_events++;
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200601}
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200602EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
603
604/*
605 * We just idled delta nanoseconds (called with irqs disabled):
606 */
607void sched_clock_idle_wakeup_event(u64 delta_ns)
608{
609 struct rq *rq = cpu_rq(smp_processor_id());
610 u64 now = sched_clock();
611
612 rq->idle_clock += delta_ns;
613 /*
614 * Override the previous timestamp and ignore all
615 * sched_clock() deltas that occured while we idled,
616 * and use the PM-provided delta_ns to advance the
617 * rq clock:
618 */
619 spin_lock(&rq->lock);
620 rq->prev_clock_raw = now;
621 rq->clock += delta_ns;
622 spin_unlock(&rq->lock);
623}
624EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200625
626/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200627 * resched_task - mark a task 'to be rescheduled now'.
628 *
629 * On UP this means the setting of the need_resched flag, on SMP it
630 * might also involve a cross-CPU call to trigger the scheduler on
631 * the target CPU.
632 */
633#ifdef CONFIG_SMP
634
635#ifndef tsk_is_polling
636#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
637#endif
638
639static void resched_task(struct task_struct *p)
640{
641 int cpu;
642
643 assert_spin_locked(&task_rq(p)->lock);
644
645 if (unlikely(test_tsk_thread_flag(p, TIF_NEED_RESCHED)))
646 return;
647
648 set_tsk_thread_flag(p, TIF_NEED_RESCHED);
649
650 cpu = task_cpu(p);
651 if (cpu == smp_processor_id())
652 return;
653
654 /* NEED_RESCHED must be visible before we test polling */
655 smp_mb();
656 if (!tsk_is_polling(p))
657 smp_send_reschedule(cpu);
658}
659
660static void resched_cpu(int cpu)
661{
662 struct rq *rq = cpu_rq(cpu);
663 unsigned long flags;
664
665 if (!spin_trylock_irqsave(&rq->lock, flags))
666 return;
667 resched_task(cpu_curr(cpu));
668 spin_unlock_irqrestore(&rq->lock, flags);
669}
670#else
671static inline void resched_task(struct task_struct *p)
672{
673 assert_spin_locked(&task_rq(p)->lock);
674 set_tsk_need_resched(p);
675}
676#endif
677
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200678static u64 div64_likely32(u64 divident, unsigned long divisor)
679{
680#if BITS_PER_LONG == 32
681 if (likely(divident <= 0xffffffffULL))
682 return (u32)divident / divisor;
683 do_div(divident, divisor);
684
685 return divident;
686#else
687 return divident / divisor;
688#endif
689}
690
691#if BITS_PER_LONG == 32
692# define WMULT_CONST (~0UL)
693#else
694# define WMULT_CONST (1UL << 32)
695#endif
696
697#define WMULT_SHIFT 32
698
Ingo Molnar194081e2007-08-09 11:16:51 +0200699/*
700 * Shift right and round:
701 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +0200702#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +0200703
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +0200704static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200705calc_delta_mine(unsigned long delta_exec, unsigned long weight,
706 struct load_weight *lw)
707{
708 u64 tmp;
709
710 if (unlikely(!lw->inv_weight))
Ingo Molnar194081e2007-08-09 11:16:51 +0200711 lw->inv_weight = (WMULT_CONST - lw->weight/2) / lw->weight + 1;
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200712
713 tmp = (u64)delta_exec * weight;
714 /*
715 * Check whether we'd overflow the 64-bit multiplication:
716 */
Ingo Molnar194081e2007-08-09 11:16:51 +0200717 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +0200718 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +0200719 WMULT_SHIFT/2);
720 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +0200721 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200722
Ingo Molnarecf691d2007-08-02 17:41:40 +0200723 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200724}
725
726static inline unsigned long
727calc_delta_fair(unsigned long delta_exec, struct load_weight *lw)
728{
729 return calc_delta_mine(delta_exec, NICE_0_LOAD, lw);
730}
731
Ingo Molnar10919852007-10-15 17:00:04 +0200732static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200733{
734 lw->weight += inc;
Ingo Molnar6cb58192007-10-15 17:00:04 +0200735 if (sched_feat(FAIR_SLEEPERS))
736 lw->inv_weight = WMULT_CONST / lw->weight;
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200737}
738
Ingo Molnar10919852007-10-15 17:00:04 +0200739static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200740{
741 lw->weight -= dec;
Ingo Molnar6cb58192007-10-15 17:00:04 +0200742 if (sched_feat(FAIR_SLEEPERS) && likely(lw->weight))
Ingo Molnar10919852007-10-15 17:00:04 +0200743 lw->inv_weight = WMULT_CONST / lw->weight;
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200744}
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746/*
Peter Williams2dd73a42006-06-27 02:54:34 -0700747 * To aid in avoiding the subversion of "niceness" due to uneven distribution
748 * of tasks with abnormal "nice" values across CPUs the contribution that
749 * each task makes to its run queue's load is weighted according to its
750 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
751 * scaled version of the new time slice allocation that they receive on time
752 * slice expiry etc.
753 */
754
Ingo Molnardd41f592007-07-09 18:51:59 +0200755#define WEIGHT_IDLEPRIO 2
756#define WMULT_IDLEPRIO (1 << 31)
757
758/*
759 * Nice levels are multiplicative, with a gentle 10% change for every
760 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
761 * nice 1, it will get ~10% less CPU time than another CPU-bound task
762 * that remained on nice 0.
763 *
764 * The "10% effect" is relative and cumulative: from _any_ nice level,
765 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +0200766 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
767 * If a task goes up by ~10% and another task goes down by ~10% then
768 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +0200769 */
770static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +0200771 /* -20 */ 88761, 71755, 56483, 46273, 36291,
772 /* -15 */ 29154, 23254, 18705, 14949, 11916,
773 /* -10 */ 9548, 7620, 6100, 4904, 3906,
774 /* -5 */ 3121, 2501, 1991, 1586, 1277,
775 /* 0 */ 1024, 820, 655, 526, 423,
776 /* 5 */ 335, 272, 215, 172, 137,
777 /* 10 */ 110, 87, 70, 56, 45,
778 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +0200779};
780
Ingo Molnar5714d2d2007-07-16 09:46:31 +0200781/*
782 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
783 *
784 * In cases where the weight does not change often, we can use the
785 * precalculated inverse to speed up arithmetics by turning divisions
786 * into multiplications:
787 */
Ingo Molnardd41f592007-07-09 18:51:59 +0200788static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +0200789 /* -20 */ 48388, 59856, 76040, 92818, 118348,
790 /* -15 */ 147320, 184698, 229616, 287308, 360437,
791 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
792 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
793 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
794 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
795 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
796 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +0200797};
Peter Williams2dd73a42006-06-27 02:54:34 -0700798
Ingo Molnardd41f592007-07-09 18:51:59 +0200799static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
800
801/*
802 * runqueue iterator, to support SMP load-balancing between different
803 * scheduling classes, without having to expose their internal data
804 * structures to the load-balancing proper:
805 */
806struct rq_iterator {
807 void *arg;
808 struct task_struct *(*start)(void *);
809 struct task_struct *(*next)(void *);
810};
811
812static int balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
813 unsigned long max_nr_move, unsigned long max_load_move,
814 struct sched_domain *sd, enum cpu_idle_type idle,
815 int *all_pinned, unsigned long *load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +0200816 int *this_best_prio, struct rq_iterator *iterator);
Ingo Molnardd41f592007-07-09 18:51:59 +0200817
818#include "sched_stats.h"
819#include "sched_rt.c"
820#include "sched_fair.c"
821#include "sched_idletask.c"
822#ifdef CONFIG_SCHED_DEBUG
823# include "sched_debug.c"
824#endif
825
826#define sched_class_highest (&rt_sched_class)
827
Ingo Molnar9c217242007-08-02 17:41:40 +0200828/*
829 * Update delta_exec, delta_fair fields for rq.
830 *
831 * delta_fair clock advances at a rate inversely proportional to
832 * total load (rq->ls.load.weight) on the runqueue, while
833 * delta_exec advances at the same rate as wall-clock (provided
834 * cpu is not idle).
835 *
836 * delta_exec / delta_fair is a measure of the (smoothened) load on this
837 * runqueue over any given interval. This (smoothened) load is used
838 * during load balance.
839 *
840 * This function is called /before/ updating rq->ls.load
841 * and when switching tasks.
842 */
Ingo Molnar29b4b622007-08-09 11:16:49 +0200843static inline void inc_load(struct rq *rq, const struct task_struct *p)
Ingo Molnar9c217242007-08-02 17:41:40 +0200844{
Ingo Molnar9c217242007-08-02 17:41:40 +0200845 update_load_add(&rq->ls.load, p->se.load.weight);
846}
847
Ingo Molnar79b5ddd2007-08-09 11:16:49 +0200848static inline void dec_load(struct rq *rq, const struct task_struct *p)
Ingo Molnar9c217242007-08-02 17:41:40 +0200849{
Ingo Molnar9c217242007-08-02 17:41:40 +0200850 update_load_sub(&rq->ls.load, p->se.load.weight);
851}
852
Ingo Molnare5fa2232007-08-09 11:16:49 +0200853static void inc_nr_running(struct task_struct *p, struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +0200854{
855 rq->nr_running++;
Ingo Molnar29b4b622007-08-09 11:16:49 +0200856 inc_load(rq, p);
Ingo Molnar9c217242007-08-02 17:41:40 +0200857}
858
Ingo Molnardb531812007-08-09 11:16:49 +0200859static void dec_nr_running(struct task_struct *p, struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +0200860{
861 rq->nr_running--;
Ingo Molnar79b5ddd2007-08-09 11:16:49 +0200862 dec_load(rq, p);
Ingo Molnar9c217242007-08-02 17:41:40 +0200863}
864
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200865static void set_load_weight(struct task_struct *p)
866{
Ingo Molnardd41f592007-07-09 18:51:59 +0200867 p->se.wait_runtime = 0;
868
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200869 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +0200870 p->se.load.weight = prio_to_weight[0] * 2;
871 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
872 return;
873 }
874
875 /*
876 * SCHED_IDLE tasks get minimal weight:
877 */
878 if (p->policy == SCHED_IDLE) {
879 p->se.load.weight = WEIGHT_IDLEPRIO;
880 p->se.load.inv_weight = WMULT_IDLEPRIO;
881 return;
882 }
883
884 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
885 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200886}
887
Ingo Molnar8159f872007-08-09 11:16:49 +0200888static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +0200889{
890 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +0200891 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +0200892 p->se.on_rq = 1;
893}
894
Ingo Molnar69be72c2007-08-09 11:16:49 +0200895static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +0200896{
Ingo Molnarf02231e2007-08-09 11:16:48 +0200897 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +0200898 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +0200899}
900
901/*
Ingo Molnardd41f592007-07-09 18:51:59 +0200902 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +0200903 */
Ingo Molnar14531182007-07-09 18:51:59 +0200904static inline int __normal_prio(struct task_struct *p)
905{
Ingo Molnardd41f592007-07-09 18:51:59 +0200906 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +0200907}
908
909/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700910 * Calculate the expected normal priority: i.e. priority
911 * without taking RT-inheritance into account. Might be
912 * boosted by interactivity modifiers. Changes upon fork,
913 * setprio syscalls, and whenever the interactivity
914 * estimator recalculates.
915 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700916static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700917{
918 int prio;
919
Ingo Molnare05606d2007-07-09 18:51:59 +0200920 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -0700921 prio = MAX_RT_PRIO-1 - p->rt_priority;
922 else
923 prio = __normal_prio(p);
924 return prio;
925}
926
927/*
928 * Calculate the current priority, i.e. the priority
929 * taken into account by the scheduler. This value might
930 * be boosted by RT tasks, or might be boosted by
931 * interactivity modifiers. Will be RT if the task got
932 * RT-boosted. If not then it returns p->normal_prio.
933 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700934static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700935{
936 p->normal_prio = normal_prio(p);
937 /*
938 * If we are RT tasks or we were boosted to RT priority,
939 * keep the priority unchanged. Otherwise, update priority
940 * to the normal priority:
941 */
942 if (!rt_prio(p->prio))
943 return p->normal_prio;
944 return p->prio;
945}
946
947/*
Ingo Molnardd41f592007-07-09 18:51:59 +0200948 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 */
Ingo Molnardd41f592007-07-09 18:51:59 +0200950static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Ingo Molnardd41f592007-07-09 18:51:59 +0200952 if (p->state == TASK_UNINTERRUPTIBLE)
953 rq->nr_uninterruptible--;
954
Ingo Molnar8159f872007-08-09 11:16:49 +0200955 enqueue_task(rq, p, wakeup);
Ingo Molnare5fa2232007-08-09 11:16:49 +0200956 inc_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
959/*
Ingo Molnardd41f592007-07-09 18:51:59 +0200960 * activate_idle_task - move idle task to the _front_ of runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 */
Ingo Molnardd41f592007-07-09 18:51:59 +0200962static inline void activate_idle_task(struct task_struct *p, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Ingo Molnara8e504d2007-08-09 11:16:47 +0200964 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Ingo Molnardd41f592007-07-09 18:51:59 +0200966 if (p->state == TASK_UNINTERRUPTIBLE)
967 rq->nr_uninterruptible--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Ingo Molnar8159f872007-08-09 11:16:49 +0200969 enqueue_task(rq, p, 0);
Ingo Molnare5fa2232007-08-09 11:16:49 +0200970 inc_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
973/*
974 * deactivate_task - remove a task from the runqueue.
975 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +0200976static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Ingo Molnardd41f592007-07-09 18:51:59 +0200978 if (p->state == TASK_UNINTERRUPTIBLE)
979 rq->nr_uninterruptible++;
980
Ingo Molnar69be72c2007-08-09 11:16:49 +0200981 dequeue_task(rq, p, sleep);
Ingo Molnardb531812007-08-09 11:16:49 +0200982 dec_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985/**
986 * task_curr - is this task currently executing on a CPU?
987 * @p: the task in question.
988 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700989inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 return cpu_curr(task_cpu(p)) == p;
992}
993
Peter Williams2dd73a42006-06-27 02:54:34 -0700994/* Used instead of source_load when we know the type == 0 */
995unsigned long weighted_cpuload(const int cpu)
996{
Ingo Molnardd41f592007-07-09 18:51:59 +0200997 return cpu_rq(cpu)->ls.load.weight;
998}
999
1000static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1001{
1002#ifdef CONFIG_SMP
1003 task_thread_info(p)->cpu = cpu;
1004 set_task_cfs_rq(p);
1005#endif
Peter Williams2dd73a42006-06-27 02:54:34 -07001006}
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +02001009
Ingo Molnardd41f592007-07-09 18:51:59 +02001010void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001011{
Ingo Molnardd41f592007-07-09 18:51:59 +02001012 int old_cpu = task_cpu(p);
1013 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
1014 u64 clock_offset, fair_clock_offset;
1015
1016 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001017 fair_clock_offset = old_rq->cfs.fair_clock - new_rq->cfs.fair_clock;
1018
Ingo Molnardd41f592007-07-09 18:51:59 +02001019 if (p->se.wait_start_fair)
1020 p->se.wait_start_fair -= fair_clock_offset;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001021 if (p->se.sleep_start_fair)
1022 p->se.sleep_start_fair -= fair_clock_offset;
1023
1024#ifdef CONFIG_SCHEDSTATS
1025 if (p->se.wait_start)
1026 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001027 if (p->se.sleep_start)
1028 p->se.sleep_start -= clock_offset;
1029 if (p->se.block_start)
1030 p->se.block_start -= clock_offset;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001031#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001032
1033 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001034}
1035
Ingo Molnar70b97a72006-07-03 00:25:42 -07001036struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Ingo Molnar36c8b582006-07-03 00:25:41 -07001039 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 int dest_cpu;
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001043};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045/*
1046 * The task's runqueue lock must be held.
1047 * Returns true if you have to wait for migration thread.
1048 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001049static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07001050migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001052 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 /*
1055 * If the task is not on a runqueue (and not running), then
1056 * it is sufficient to simply update the task's cpu field.
1057 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001058 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 set_task_cpu(p, dest_cpu);
1060 return 0;
1061 }
1062
1063 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 req->task = p;
1065 req->dest_cpu = dest_cpu;
1066 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07001067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return 1;
1069}
1070
1071/*
1072 * wait_task_inactive - wait for a thread to unschedule.
1073 *
1074 * The caller must ensure that the task *will* unschedule sometime soon,
1075 * else this function might spin for a *long* time. This function can't
1076 * be called with interrupts off, or it may introduce deadlock with
1077 * smp_call_function() if an IPI is sent by the same process we are
1078 * waiting to become inactive.
1079 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001080void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
1082 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001083 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001084 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086repeat:
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001087 /*
1088 * We do the initial early heuristics without holding
1089 * any task-queue locks at all. We'll only try to get
1090 * the runqueue lock when things look like they will
1091 * work out!
1092 */
1093 rq = task_rq(p);
1094
1095 /*
1096 * If the task is actively running on another CPU
1097 * still, just relax and busy-wait without holding
1098 * any locks.
1099 *
1100 * NOTE! Since we don't hold any locks, it's not
1101 * even sure that "rq" stays as the right runqueue!
1102 * But we don't care, since "task_running()" will
1103 * return false if the runqueue has changed and p
1104 * is actually now running somewhere else!
1105 */
1106 while (task_running(rq, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001108
1109 /*
1110 * Ok, time to look more closely! We need the rq
1111 * lock now, to be *sure*. If we're wrong, we'll
1112 * just go back and repeat.
1113 */
1114 rq = task_rq_lock(p, &flags);
1115 running = task_running(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02001116 on_rq = p->se.on_rq;
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001117 task_rq_unlock(rq, &flags);
1118
1119 /*
1120 * Was it really running after all now that we
1121 * checked with the proper locks actually held?
1122 *
1123 * Oops. Go back and try again..
1124 */
1125 if (unlikely(running)) {
1126 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 goto repeat;
1128 }
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001129
1130 /*
1131 * It's not enough that it's not actively running,
1132 * it must be off the runqueue _entirely_, and not
1133 * preempted!
1134 *
1135 * So if it wa still runnable (but just not actively
1136 * running right now), it's preempted, and we should
1137 * yield - it could be a while.
1138 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001139 if (unlikely(on_rq)) {
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001140 yield();
1141 goto repeat;
1142 }
1143
1144 /*
1145 * Ahh, all good. It wasn't running, and it wasn't
1146 * runnable, which means that it will never become
1147 * running in the future either. We're all done!
1148 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
1151/***
1152 * kick_process - kick a running thread to enter/exit the kernel
1153 * @p: the to-be-kicked thread
1154 *
1155 * Cause a process which is running on another CPU to enter
1156 * kernel-mode, without any delay. (to get signals handled.)
1157 *
1158 * NOTE: this function doesnt have to take the runqueue lock,
1159 * because all it wants to ensure is that the remote task enters
1160 * the kernel. If the IPI races and the task has been migrated
1161 * to another CPU then no harm is done and the purpose has been
1162 * achieved as well.
1163 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001164void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
1166 int cpu;
1167
1168 preempt_disable();
1169 cpu = task_cpu(p);
1170 if ((cpu != smp_processor_id()) && task_curr(p))
1171 smp_send_reschedule(cpu);
1172 preempt_enable();
1173}
1174
1175/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001176 * Return a low guess at the load of a migration-source cpu weighted
1177 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 *
1179 * We want to under-estimate the load of migration sources, to
1180 * balance conservatively.
1181 */
Con Kolivasb9104722005-11-08 21:38:55 -08001182static inline unsigned long source_load(int cpu, int type)
1183{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001184 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001185 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001186
Peter Williams2dd73a42006-06-27 02:54:34 -07001187 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001188 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001189
Ingo Molnardd41f592007-07-09 18:51:59 +02001190 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
1192
1193/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001194 * Return a high guess at the load of a migration-target cpu weighted
1195 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 */
Con Kolivasb9104722005-11-08 21:38:55 -08001197static inline unsigned long target_load(int cpu, int type)
1198{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001199 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001200 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001201
Peter Williams2dd73a42006-06-27 02:54:34 -07001202 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001203 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001204
Ingo Molnardd41f592007-07-09 18:51:59 +02001205 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07001206}
1207
1208/*
1209 * Return the average load per task on the cpu's run queue
1210 */
1211static inline unsigned long cpu_avg_load_per_task(int cpu)
1212{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001213 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001214 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07001215 unsigned long n = rq->nr_running;
1216
Ingo Molnardd41f592007-07-09 18:51:59 +02001217 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
Nick Piggin147cbb42005-06-25 14:57:19 -07001220/*
1221 * find_idlest_group finds and returns the least busy CPU group within the
1222 * domain.
1223 */
1224static struct sched_group *
1225find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
1226{
1227 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
1228 unsigned long min_load = ULONG_MAX, this_load = 0;
1229 int load_idx = sd->forkexec_idx;
1230 int imbalance = 100 + (sd->imbalance_pct-100)/2;
1231
1232 do {
1233 unsigned long load, avg_load;
1234 int local_group;
1235 int i;
1236
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001237 /* Skip over this group if it has no CPUs allowed */
1238 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
1239 goto nextgroup;
1240
Nick Piggin147cbb42005-06-25 14:57:19 -07001241 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07001242
1243 /* Tally up the load of all CPUs in the group */
1244 avg_load = 0;
1245
1246 for_each_cpu_mask(i, group->cpumask) {
1247 /* Bias balancing toward cpus of our domain */
1248 if (local_group)
1249 load = source_load(i, load_idx);
1250 else
1251 load = target_load(i, load_idx);
1252
1253 avg_load += load;
1254 }
1255
1256 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07001257 avg_load = sg_div_cpu_power(group,
1258 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07001259
1260 if (local_group) {
1261 this_load = avg_load;
1262 this = group;
1263 } else if (avg_load < min_load) {
1264 min_load = avg_load;
1265 idlest = group;
1266 }
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001267nextgroup:
Nick Piggin147cbb42005-06-25 14:57:19 -07001268 group = group->next;
1269 } while (group != sd->groups);
1270
1271 if (!idlest || 100*this_load < imbalance*min_load)
1272 return NULL;
1273 return idlest;
1274}
1275
1276/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07001277 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07001278 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07001279static int
1280find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
Nick Piggin147cbb42005-06-25 14:57:19 -07001281{
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001282 cpumask_t tmp;
Nick Piggin147cbb42005-06-25 14:57:19 -07001283 unsigned long load, min_load = ULONG_MAX;
1284 int idlest = -1;
1285 int i;
1286
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001287 /* Traverse only the allowed CPUs */
1288 cpus_and(tmp, group->cpumask, p->cpus_allowed);
1289
1290 for_each_cpu_mask(i, tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07001291 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07001292
1293 if (load < min_load || (load == min_load && i == this_cpu)) {
1294 min_load = load;
1295 idlest = i;
1296 }
1297 }
1298
1299 return idlest;
1300}
1301
Nick Piggin476d1392005-06-25 14:57:29 -07001302/*
1303 * sched_balance_self: balance the current task (running on cpu) in domains
1304 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
1305 * SD_BALANCE_EXEC.
1306 *
1307 * Balance, ie. select the least loaded group.
1308 *
1309 * Returns the target CPU number, or the same CPU if no balancing is needed.
1310 *
1311 * preempt must be disabled.
1312 */
1313static int sched_balance_self(int cpu, int flag)
1314{
1315 struct task_struct *t = current;
1316 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07001317
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001318 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02001319 /*
1320 * If power savings logic is enabled for a domain, stop there.
1321 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07001322 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
1323 break;
Nick Piggin476d1392005-06-25 14:57:29 -07001324 if (tmp->flags & flag)
1325 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001326 }
Nick Piggin476d1392005-06-25 14:57:29 -07001327
1328 while (sd) {
1329 cpumask_t span;
1330 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001331 int new_cpu, weight;
1332
1333 if (!(sd->flags & flag)) {
1334 sd = sd->child;
1335 continue;
1336 }
Nick Piggin476d1392005-06-25 14:57:29 -07001337
1338 span = sd->span;
1339 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001340 if (!group) {
1341 sd = sd->child;
1342 continue;
1343 }
Nick Piggin476d1392005-06-25 14:57:29 -07001344
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001345 new_cpu = find_idlest_cpu(group, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001346 if (new_cpu == -1 || new_cpu == cpu) {
1347 /* Now try balancing at a lower domain level of cpu */
1348 sd = sd->child;
1349 continue;
1350 }
Nick Piggin476d1392005-06-25 14:57:29 -07001351
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001352 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07001353 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07001354 sd = NULL;
1355 weight = cpus_weight(span);
1356 for_each_domain(cpu, tmp) {
1357 if (weight <= cpus_weight(tmp->span))
1358 break;
1359 if (tmp->flags & flag)
1360 sd = tmp;
1361 }
1362 /* while loop will break here if sd == NULL */
1363 }
1364
1365 return cpu;
1366}
1367
1368#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370/*
1371 * wake_idle() will wake a task on an idle cpu if task->cpu is
1372 * not idle and an idle cpu is available. The span of cpus to
1373 * search starts with cpus closest then further out as needed,
1374 * so we always favor a closer, idle cpu.
1375 *
1376 * Returns the CPU we should wake onto.
1377 */
1378#if defined(ARCH_HAS_SCHED_WAKE_IDLE)
Ingo Molnar36c8b582006-07-03 00:25:41 -07001379static int wake_idle(int cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
1381 cpumask_t tmp;
1382 struct sched_domain *sd;
1383 int i;
1384
Siddha, Suresh B49531982007-05-08 00:33:01 -07001385 /*
1386 * If it is idle, then it is the best cpu to run this task.
1387 *
1388 * This cpu is also the best, if it has more than one task already.
1389 * Siblings must be also busy(in most cases) as they didn't already
1390 * pickup the extra load from this cpu and hence we need not check
1391 * sibling runqueue info. This will avoid the checks and cache miss
1392 * penalities associated with that.
1393 */
1394 if (idle_cpu(cpu) || cpu_rq(cpu)->nr_running > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return cpu;
1396
1397 for_each_domain(cpu, sd) {
1398 if (sd->flags & SD_WAKE_IDLE) {
Nick Piggine0f364f2005-06-25 14:57:06 -07001399 cpus_and(tmp, sd->span, p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 for_each_cpu_mask(i, tmp) {
1401 if (idle_cpu(i))
1402 return i;
1403 }
Ingo Molnar9761eea2007-07-09 18:52:00 +02001404 } else {
Nick Piggine0f364f2005-06-25 14:57:06 -07001405 break;
Ingo Molnar9761eea2007-07-09 18:52:00 +02001406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
1408 return cpu;
1409}
1410#else
Ingo Molnar36c8b582006-07-03 00:25:41 -07001411static inline int wake_idle(int cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
1413 return cpu;
1414}
1415#endif
1416
1417/***
1418 * try_to_wake_up - wake up a thread
1419 * @p: the to-be-woken-up thread
1420 * @state: the mask of task states that can be woken
1421 * @sync: do a synchronous wakeup?
1422 *
1423 * Put it on the run-queue if it's not already there. The "current"
1424 * thread is always on the run-queue (except when the actual
1425 * re-schedule is in progress), and as such you're allowed to do
1426 * the simpler "current->state = TASK_RUNNING" to mark yourself
1427 * runnable without the overhead of this.
1428 *
1429 * returns failure only if the task is already active.
1430 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001431static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
1433 int cpu, this_cpu, success = 0;
1434 unsigned long flags;
1435 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001436 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437#ifdef CONFIG_SMP
Nick Piggin78979862005-06-25 14:57:13 -07001438 struct sched_domain *sd, *this_sd = NULL;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001439 unsigned long load, this_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 int new_cpu;
1441#endif
1442
1443 rq = task_rq_lock(p, &flags);
1444 old_state = p->state;
1445 if (!(old_state & state))
1446 goto out;
1447
Ingo Molnardd41f592007-07-09 18:51:59 +02001448 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 goto out_running;
1450
1451 cpu = task_cpu(p);
1452 this_cpu = smp_processor_id();
1453
1454#ifdef CONFIG_SMP
1455 if (unlikely(task_running(rq, p)))
1456 goto out_activate;
1457
Nick Piggin78979862005-06-25 14:57:13 -07001458 new_cpu = cpu;
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 schedstat_inc(rq, ttwu_cnt);
1461 if (cpu == this_cpu) {
1462 schedstat_inc(rq, ttwu_local);
Nick Piggin78979862005-06-25 14:57:13 -07001463 goto out_set_cpu;
1464 }
1465
1466 for_each_domain(this_cpu, sd) {
1467 if (cpu_isset(cpu, sd->span)) {
1468 schedstat_inc(sd, ttwu_wake_remote);
1469 this_sd = sd;
1470 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
1472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Nick Piggin78979862005-06-25 14:57:13 -07001474 if (unlikely(!cpu_isset(this_cpu, p->cpus_allowed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 goto out_set_cpu;
1476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 /*
Nick Piggin78979862005-06-25 14:57:13 -07001478 * Check for affine wakeup and passive balancing possibilities.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 */
Nick Piggin78979862005-06-25 14:57:13 -07001480 if (this_sd) {
1481 int idx = this_sd->wake_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 unsigned int imbalance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Nick Piggina3f21bc2005-06-25 14:57:15 -07001484 imbalance = 100 + (this_sd->imbalance_pct - 100) / 2;
1485
Nick Piggin78979862005-06-25 14:57:13 -07001486 load = source_load(cpu, idx);
1487 this_load = target_load(this_cpu, idx);
1488
Nick Piggin78979862005-06-25 14:57:13 -07001489 new_cpu = this_cpu; /* Wake to this CPU if we can */
1490
Nick Piggina3f21bc2005-06-25 14:57:15 -07001491 if (this_sd->flags & SD_WAKE_AFFINE) {
1492 unsigned long tl = this_load;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08001493 unsigned long tl_per_task;
1494
1495 tl_per_task = cpu_avg_load_per_task(this_cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07001496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 /*
Nick Piggina3f21bc2005-06-25 14:57:15 -07001498 * If sync wakeup then subtract the (maximum possible)
1499 * effect of the currently running task from the load
1500 * of the current CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 */
Nick Piggina3f21bc2005-06-25 14:57:15 -07001502 if (sync)
Ingo Molnardd41f592007-07-09 18:51:59 +02001503 tl -= current->se.load.weight;
Nick Piggina3f21bc2005-06-25 14:57:15 -07001504
1505 if ((tl <= load &&
Peter Williams2dd73a42006-06-27 02:54:34 -07001506 tl + target_load(cpu, idx) <= tl_per_task) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02001507 100*(tl + p->se.load.weight) <= imbalance*load) {
Nick Piggina3f21bc2005-06-25 14:57:15 -07001508 /*
1509 * This domain has SD_WAKE_AFFINE and
1510 * p is cache cold in this domain, and
1511 * there is no bad imbalance.
1512 */
1513 schedstat_inc(this_sd, ttwu_move_affine);
1514 goto out_set_cpu;
1515 }
1516 }
1517
1518 /*
1519 * Start passive balancing when half the imbalance_pct
1520 * limit is reached.
1521 */
1522 if (this_sd->flags & SD_WAKE_BALANCE) {
1523 if (imbalance*this_load <= 100*load) {
1524 schedstat_inc(this_sd, ttwu_move_balance);
1525 goto out_set_cpu;
1526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528 }
1529
1530 new_cpu = cpu; /* Could not wake to this_cpu. Wake to cpu instead */
1531out_set_cpu:
1532 new_cpu = wake_idle(new_cpu, p);
1533 if (new_cpu != cpu) {
1534 set_task_cpu(p, new_cpu);
1535 task_rq_unlock(rq, &flags);
1536 /* might preempt at this point */
1537 rq = task_rq_lock(p, &flags);
1538 old_state = p->state;
1539 if (!(old_state & state))
1540 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02001541 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 goto out_running;
1543
1544 this_cpu = smp_processor_id();
1545 cpu = task_cpu(p);
1546 }
1547
1548out_activate:
1549#endif /* CONFIG_SMP */
Ingo Molnar2daa3572007-08-09 11:16:51 +02001550 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02001551 activate_task(rq, p, 1);
Ingo Molnard79fc0f2005-09-10 00:26:12 -07001552 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 * Sync wakeups (i.e. those types of wakeups where the waker
1554 * has indicated that it will leave the CPU in short order)
1555 * don't trigger a preemption, if the woken up task will run on
1556 * this cpu. (in this case the 'I will reschedule' promise of
1557 * the waker guarantees that the freshly woken up task is going
1558 * to be considered on this CPU.)
1559 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001560 if (!sync || cpu != this_cpu)
1561 check_preempt_curr(rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 success = 1;
1563
1564out_running:
1565 p->state = TASK_RUNNING;
1566out:
1567 task_rq_unlock(rq, &flags);
1568
1569 return success;
1570}
1571
Ingo Molnar36c8b582006-07-03 00:25:41 -07001572int fastcall wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
1574 return try_to_wake_up(p, TASK_STOPPED | TASK_TRACED |
1575 TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE, 0);
1576}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577EXPORT_SYMBOL(wake_up_process);
1578
Ingo Molnar36c8b582006-07-03 00:25:41 -07001579int fastcall wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
1581 return try_to_wake_up(p, state, 0);
1582}
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584/*
1585 * Perform scheduler related setup for a newly forked process p.
1586 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02001587 *
1588 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001590static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Ingo Molnardd41f592007-07-09 18:51:59 +02001592 p->se.wait_start_fair = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02001593 p->se.exec_start = 0;
1594 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02001595 p->se.prev_sum_exec_runtime = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02001596 p->se.wait_runtime = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001597 p->se.sleep_start_fair = 0;
1598
1599#ifdef CONFIG_SCHEDSTATS
1600 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02001601 p->se.sum_wait_runtime = 0;
1602 p->se.sum_sleep_runtime = 0;
1603 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02001604 p->se.block_start = 0;
1605 p->se.sleep_max = 0;
1606 p->se.block_max = 0;
1607 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02001608 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02001609 p->se.wait_max = 0;
1610 p->se.wait_runtime_overruns = 0;
1611 p->se.wait_runtime_underruns = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001612#endif
Nick Piggin476d1392005-06-25 14:57:29 -07001613
Ingo Molnardd41f592007-07-09 18:51:59 +02001614 INIT_LIST_HEAD(&p->run_list);
1615 p->se.on_rq = 0;
Nick Piggin476d1392005-06-25 14:57:29 -07001616
Avi Kivitye107be32007-07-26 13:40:43 +02001617#ifdef CONFIG_PREEMPT_NOTIFIERS
1618 INIT_HLIST_HEAD(&p->preempt_notifiers);
1619#endif
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 /*
1622 * We mark the process as running here, but have not actually
1623 * inserted it onto the runqueue yet. This guarantees that
1624 * nobody will actually run it, and a signal or other external
1625 * event cannot wake it up and insert it on the runqueue either.
1626 */
1627 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02001628}
1629
1630/*
1631 * fork()/clone()-time setup:
1632 */
1633void sched_fork(struct task_struct *p, int clone_flags)
1634{
1635 int cpu = get_cpu();
1636
1637 __sched_fork(p);
1638
1639#ifdef CONFIG_SMP
1640 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
1641#endif
1642 __set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07001643
1644 /*
1645 * Make sure we do not leak PI boosting priority to the child:
1646 */
1647 p->prio = current->normal_prio;
1648
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07001649#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02001650 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07001651 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08001653#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07001654 p->oncpu = 0;
1655#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07001657 /* Want to start with kernel preemption disabled. */
Al Viroa1261f52005-11-13 16:06:55 -08001658 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659#endif
Nick Piggin476d1392005-06-25 14:57:29 -07001660 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
1662
1663/*
1664 * wake_up_new_task - wake up a newly created task for the first time.
1665 *
1666 * This function will do some initial scheduler statistics housekeeping
1667 * that must be done for every newly created context, then puts the task
1668 * on the runqueue and wakes it.
1669 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001670void fastcall wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
1672 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001673 struct rq *rq;
1674 int this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnardd41f592007-07-09 18:51:59 +02001678 this_cpu = smp_processor_id(); /* parent's CPU */
Ingo Molnara8e504d2007-08-09 11:16:47 +02001679 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 p->prio = effective_prio(p);
1682
Hiroshi Shimamoto9c95e732007-09-19 23:34:46 +02001683 if (rt_prio(p->prio))
1684 p->sched_class = &rt_sched_class;
1685 else
1686 p->sched_class = &fair_sched_class;
1687
Ingo Molnar44142fa2007-10-15 17:00:01 +02001688 if (task_cpu(p) != this_cpu || !p->sched_class->task_new ||
1689 !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001690 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02001693 * Let the scheduling class do new task startup
1694 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02001696 p->sched_class->task_new(rq, p);
Ingo Molnare5fa2232007-08-09 11:16:49 +02001697 inc_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
Ingo Molnardd41f592007-07-09 18:51:59 +02001699 check_preempt_curr(rq, p);
1700 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
Avi Kivitye107be32007-07-26 13:40:43 +02001703#ifdef CONFIG_PREEMPT_NOTIFIERS
1704
1705/**
Randy Dunlap421cee22007-07-31 00:37:50 -07001706 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
1707 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02001708 */
1709void preempt_notifier_register(struct preempt_notifier *notifier)
1710{
1711 hlist_add_head(&notifier->link, &current->preempt_notifiers);
1712}
1713EXPORT_SYMBOL_GPL(preempt_notifier_register);
1714
1715/**
1716 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07001717 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02001718 *
1719 * This is safe to call from within a preemption notifier.
1720 */
1721void preempt_notifier_unregister(struct preempt_notifier *notifier)
1722{
1723 hlist_del(&notifier->link);
1724}
1725EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
1726
1727static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
1728{
1729 struct preempt_notifier *notifier;
1730 struct hlist_node *node;
1731
1732 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
1733 notifier->ops->sched_in(notifier, raw_smp_processor_id());
1734}
1735
1736static void
1737fire_sched_out_preempt_notifiers(struct task_struct *curr,
1738 struct task_struct *next)
1739{
1740 struct preempt_notifier *notifier;
1741 struct hlist_node *node;
1742
1743 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
1744 notifier->ops->sched_out(notifier, next);
1745}
1746
1747#else
1748
1749static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
1750{
1751}
1752
1753static void
1754fire_sched_out_preempt_notifiers(struct task_struct *curr,
1755 struct task_struct *next)
1756{
1757}
1758
1759#endif
1760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761/**
Nick Piggin4866cde2005-06-25 14:57:23 -07001762 * prepare_task_switch - prepare to switch tasks
1763 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07001764 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07001765 * @next: the task we are going to switch to.
1766 *
1767 * This is called with the rq lock held and interrupts off. It must
1768 * be paired with a subsequent finish_task_switch after the context
1769 * switch.
1770 *
1771 * prepare_task_switch sets up locking and calls architecture specific
1772 * hooks.
1773 */
Avi Kivitye107be32007-07-26 13:40:43 +02001774static inline void
1775prepare_task_switch(struct rq *rq, struct task_struct *prev,
1776 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07001777{
Avi Kivitye107be32007-07-26 13:40:43 +02001778 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07001779 prepare_lock_switch(rq, next);
1780 prepare_arch_switch(next);
1781}
1782
1783/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04001785 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 * @prev: the thread we just switched away from.
1787 *
Nick Piggin4866cde2005-06-25 14:57:23 -07001788 * finish_task_switch must be called after the context switch, paired
1789 * with a prepare_task_switch call before the context switch.
1790 * finish_task_switch will reconcile locking set up by prepare_task_switch,
1791 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 *
1793 * Note that we may have delayed dropping an mm in context_switch(). If
1794 * so, we finish that here outside of the runqueue lock. (Doing it
1795 * with the lock held can cause deadlocks; see schedule() for
1796 * details.)
1797 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07001798static inline void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 __releases(rq->lock)
1800{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07001802 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804 rq->prev_mm = NULL;
1805
1806 /*
1807 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07001808 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07001809 * schedule one last time. The schedule call will never return, and
1810 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07001811 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 * still held, otherwise prev could be scheduled on another cpu, die
1813 * there before we look at prev->state, and then the reference would
1814 * be dropped twice.
1815 * Manfred Spraul <manfred@colorfullife.com>
1816 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07001817 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07001818 finish_arch_switch(prev);
1819 finish_lock_switch(rq, prev);
Avi Kivitye107be32007-07-26 13:40:43 +02001820 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 if (mm)
1822 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07001823 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08001824 /*
1825 * Remove function-return probe instances associated with this
1826 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02001827 */
bibo maoc6fd91f2006-03-26 01:38:20 -08001828 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08001830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
1832
1833/**
1834 * schedule_tail - first thing a freshly forked thread must call.
1835 * @prev: the thread we just switched away from.
1836 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001837asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 __releases(rq->lock)
1839{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001840 struct rq *rq = this_rq();
1841
Nick Piggin4866cde2005-06-25 14:57:23 -07001842 finish_task_switch(rq, prev);
1843#ifdef __ARCH_WANT_UNLOCKED_CTXSW
1844 /* In this case, finish_task_switch does not reenable preemption */
1845 preempt_enable();
1846#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (current->set_child_tid)
1848 put_user(current->pid, current->set_child_tid);
1849}
1850
1851/*
1852 * context_switch - switch to the new MM and the new
1853 * thread's register state.
1854 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001855static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07001856context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07001857 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Ingo Molnardd41f592007-07-09 18:51:59 +02001859 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Avi Kivitye107be32007-07-26 13:40:43 +02001861 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02001862 mm = next->mm;
1863 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01001864 /*
1865 * For paravirt, this is coupled with an exit in switch_to to
1866 * combine the page table reload and the switch backend into
1867 * one hypercall.
1868 */
1869 arch_enter_lazy_cpu_mode();
1870
Ingo Molnardd41f592007-07-09 18:51:59 +02001871 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 next->active_mm = oldmm;
1873 atomic_inc(&oldmm->mm_count);
1874 enter_lazy_tlb(oldmm, next);
1875 } else
1876 switch_mm(oldmm, mm, next);
1877
Ingo Molnardd41f592007-07-09 18:51:59 +02001878 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 rq->prev_mm = oldmm;
1881 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07001882 /*
1883 * Since the runqueue lock will be released by the next
1884 * task (which is an invalid locking op but in the case
1885 * of the scheduler it's an obvious special-case), so we
1886 * do an early lockdep release here:
1887 */
1888#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07001889 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07001890#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 /* Here we just switch the register state and the stack. */
1893 switch_to(prev, next, prev);
1894
Ingo Molnardd41f592007-07-09 18:51:59 +02001895 barrier();
1896 /*
1897 * this_rq must be evaluated again because prev may have moved
1898 * CPUs since it called schedule(), thus the 'rq' on its stack
1899 * frame will be invalid.
1900 */
1901 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902}
1903
1904/*
1905 * nr_running, nr_uninterruptible and nr_context_switches:
1906 *
1907 * externally visible scheduler statistics: current number of runnable
1908 * threads, current number of uninterruptible-sleeping threads, total
1909 * number of context switches performed since bootup.
1910 */
1911unsigned long nr_running(void)
1912{
1913 unsigned long i, sum = 0;
1914
1915 for_each_online_cpu(i)
1916 sum += cpu_rq(i)->nr_running;
1917
1918 return sum;
1919}
1920
1921unsigned long nr_uninterruptible(void)
1922{
1923 unsigned long i, sum = 0;
1924
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001925 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 sum += cpu_rq(i)->nr_uninterruptible;
1927
1928 /*
1929 * Since we read the counters lockless, it might be slightly
1930 * inaccurate. Do not allow it to go below zero though:
1931 */
1932 if (unlikely((long)sum < 0))
1933 sum = 0;
1934
1935 return sum;
1936}
1937
1938unsigned long long nr_context_switches(void)
1939{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07001940 int i;
1941 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001943 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 sum += cpu_rq(i)->nr_switches;
1945
1946 return sum;
1947}
1948
1949unsigned long nr_iowait(void)
1950{
1951 unsigned long i, sum = 0;
1952
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001953 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 sum += atomic_read(&cpu_rq(i)->nr_iowait);
1955
1956 return sum;
1957}
1958
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08001959unsigned long nr_active(void)
1960{
1961 unsigned long i, running = 0, uninterruptible = 0;
1962
1963 for_each_online_cpu(i) {
1964 running += cpu_rq(i)->nr_running;
1965 uninterruptible += cpu_rq(i)->nr_uninterruptible;
1966 }
1967
1968 if (unlikely((long)uninterruptible < 0))
1969 uninterruptible = 0;
1970
1971 return running + uninterruptible;
1972}
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001975 * Update rq->cpu_load[] statistics. This function is usually called every
1976 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07001977 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001978static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07001979{
Ingo Molnar53df5562007-10-15 17:00:03 +02001980 unsigned long this_load = this_rq->ls.load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02001981 int i, scale;
1982
1983 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02001984
1985 /* Update our load: */
1986 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
1987 unsigned long old_load, new_load;
1988
1989 /* scale is effectively 1 << i now, and >> i divides by scale */
1990
1991 old_load = this_rq->cpu_load[i];
1992 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02001993 /*
1994 * Round up the averaging division if load is increasing. This
1995 * prevents us from getting stuck on 9 if the load is 10, for
1996 * example.
1997 */
1998 if (new_load > old_load)
1999 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002000 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2001 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002002}
2003
Ingo Molnardd41f592007-07-09 18:51:59 +02002004#ifdef CONFIG_SMP
2005
Ingo Molnar48f24c42006-07-03 00:25:40 -07002006/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 * double_rq_lock - safely lock two runqueues
2008 *
2009 * Note this does not disable interrupts like task_rq_lock,
2010 * you need to do so manually before calling.
2011 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002012static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 __acquires(rq1->lock)
2014 __acquires(rq2->lock)
2015{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002016 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 if (rq1 == rq2) {
2018 spin_lock(&rq1->lock);
2019 __acquire(rq2->lock); /* Fake it out ;) */
2020 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002021 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 spin_lock(&rq1->lock);
2023 spin_lock(&rq2->lock);
2024 } else {
2025 spin_lock(&rq2->lock);
2026 spin_lock(&rq1->lock);
2027 }
2028 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002029 update_rq_clock(rq1);
2030 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
2033/*
2034 * double_rq_unlock - safely unlock two runqueues
2035 *
2036 * Note this does not restore interrupts like task_rq_unlock,
2037 * you need to do so manually after calling.
2038 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002039static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 __releases(rq1->lock)
2041 __releases(rq2->lock)
2042{
2043 spin_unlock(&rq1->lock);
2044 if (rq1 != rq2)
2045 spin_unlock(&rq2->lock);
2046 else
2047 __release(rq2->lock);
2048}
2049
2050/*
2051 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2052 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002053static void double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 __releases(this_rq->lock)
2055 __acquires(busiest->lock)
2056 __acquires(this_rq->lock)
2057{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002058 if (unlikely(!irqs_disabled())) {
2059 /* printk() doesn't work good under rq->lock */
2060 spin_unlock(&this_rq->lock);
2061 BUG_ON(1);
2062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002064 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 spin_unlock(&this_rq->lock);
2066 spin_lock(&busiest->lock);
2067 spin_lock(&this_rq->lock);
2068 } else
2069 spin_lock(&busiest->lock);
2070 }
2071}
2072
2073/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 * If dest_cpu is allowed for this process, migrate the task to it.
2075 * This is accomplished by forcing the cpu_allowed mask to only
2076 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
2077 * the cpu_allowed mask is restored.
2078 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002079static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002081 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002083 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 rq = task_rq_lock(p, &flags);
2086 if (!cpu_isset(dest_cpu, p->cpus_allowed)
2087 || unlikely(cpu_is_offline(dest_cpu)))
2088 goto out;
2089
2090 /* force the process onto the specified CPU */
2091 if (migrate_task(p, dest_cpu, &req)) {
2092 /* Need to wait for migration thread (might exit: take ref). */
2093 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07002094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 get_task_struct(mt);
2096 task_rq_unlock(rq, &flags);
2097 wake_up_process(mt);
2098 put_task_struct(mt);
2099 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07002100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 return;
2102 }
2103out:
2104 task_rq_unlock(rq, &flags);
2105}
2106
2107/*
Nick Piggin476d1392005-06-25 14:57:29 -07002108 * sched_exec - execve() is a valuable balancing opportunity, because at
2109 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 */
2111void sched_exec(void)
2112{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002114 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002116 if (new_cpu != this_cpu)
2117 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
2120/*
2121 * pull_task - move a task from a remote runqueue to the local runqueue.
2122 * Both runqueues must be locked.
2123 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002124static void pull_task(struct rq *src_rq, struct task_struct *p,
2125 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02002127 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002129 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 /*
2131 * Note that idle threads have a prio of MAX_PRIO, for this test
2132 * to be always true for them.
2133 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002134 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
2137/*
2138 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
2139 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002140static
Ingo Molnar70b97a72006-07-03 00:25:42 -07002141int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002142 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002143 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
2145 /*
2146 * We do not migrate tasks that are:
2147 * 1) running (obviously), or
2148 * 2) cannot be migrated to this CPU due to cpus_allowed, or
2149 * 3) are cache-hot on their current CPU.
2150 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 if (!cpu_isset(this_cpu, p->cpus_allowed))
2152 return 0;
Nick Piggin81026792005-06-25 14:57:07 -07002153 *all_pinned = 0;
2154
2155 if (task_running(rq, p))
2156 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 return 1;
2159}
2160
Ingo Molnardd41f592007-07-09 18:51:59 +02002161static int balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
2162 unsigned long max_nr_move, unsigned long max_load_move,
2163 struct sched_domain *sd, enum cpu_idle_type idle,
2164 int *all_pinned, unsigned long *load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002165 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02002166{
2167 int pulled = 0, pinned = 0, skip_for_load;
2168 struct task_struct *p;
2169 long rem_load_move = max_load_move;
2170
2171 if (max_nr_move == 0 || max_load_move == 0)
2172 goto out;
2173
2174 pinned = 1;
2175
2176 /*
2177 * Start the load-balancing iterator:
2178 */
2179 p = iterator->start(iterator->arg);
2180next:
2181 if (!p)
2182 goto out;
2183 /*
2184 * To help distribute high priority tasks accross CPUs we don't
2185 * skip a task if it will be the highest priority task (i.e. smallest
2186 * prio value) on its new queue regardless of its load weight
2187 */
2188 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
2189 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002190 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02002191 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002192 p = iterator->next(iterator->arg);
2193 goto next;
2194 }
2195
2196 pull_task(busiest, p, this_rq, this_cpu);
2197 pulled++;
2198 rem_load_move -= p->se.load.weight;
2199
2200 /*
2201 * We only want to steal up to the prescribed number of tasks
2202 * and the prescribed amount of weighted load.
2203 */
2204 if (pulled < max_nr_move && rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002205 if (p->prio < *this_best_prio)
2206 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02002207 p = iterator->next(iterator->arg);
2208 goto next;
2209 }
2210out:
2211 /*
2212 * Right now, this is the only place pull_task() is called,
2213 * so we can safely collect pull_task() stats here rather than
2214 * inside pull_task().
2215 */
2216 schedstat_add(sd, lb_gained[idle], pulled);
2217
2218 if (all_pinned)
2219 *all_pinned = pinned;
2220 *load_moved = max_load_move - rem_load_move;
2221 return pulled;
2222}
Ingo Molnar48f24c42006-07-03 00:25:40 -07002223
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224/*
Peter Williams43010652007-08-09 11:16:46 +02002225 * move_tasks tries to move up to max_load_move weighted load from busiest to
2226 * this_rq, as part of a balancing operation within domain "sd".
2227 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 *
2229 * Called with both runqueues locked.
2230 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002231static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02002232 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002233 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07002234 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
Ingo Molnardd41f592007-07-09 18:51:59 +02002236 struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02002237 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002238 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Ingo Molnardd41f592007-07-09 18:51:59 +02002240 do {
Peter Williams43010652007-08-09 11:16:46 +02002241 total_load_moved +=
2242 class->load_balance(this_rq, this_cpu, busiest,
2243 ULONG_MAX, max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002244 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02002245 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02002246 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Peter Williams43010652007-08-09 11:16:46 +02002248 return total_load_moved > 0;
2249}
2250
2251/*
2252 * move_one_task tries to move exactly one task from busiest to this_rq, as
2253 * part of active balancing operations within "domain".
2254 * Returns 1 if successful and 0 otherwise.
2255 *
2256 * Called with both runqueues locked.
2257 */
2258static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2259 struct sched_domain *sd, enum cpu_idle_type idle)
2260{
2261 struct sched_class *class;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002262 int this_best_prio = MAX_PRIO;
Peter Williams43010652007-08-09 11:16:46 +02002263
2264 for (class = sched_class_highest; class; class = class->next)
2265 if (class->load_balance(this_rq, this_cpu, busiest,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002266 1, ULONG_MAX, sd, idle, NULL,
2267 &this_best_prio))
Peter Williams43010652007-08-09 11:16:46 +02002268 return 1;
2269
2270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271}
2272
2273/*
2274 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07002275 * domain. It calculates and returns the amount of weighted load which
2276 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 */
2278static struct sched_group *
2279find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02002280 unsigned long *imbalance, enum cpu_idle_type idle,
2281 int *sd_idle, cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
2283 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
2284 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002285 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07002286 unsigned long busiest_load_per_task, busiest_nr_running;
2287 unsigned long this_load_per_task, this_nr_running;
Nick Piggin78979862005-06-25 14:57:13 -07002288 int load_idx;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002289#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2290 int power_savings_balance = 1;
2291 unsigned long leader_nr_running = 0, min_load_per_task = 0;
2292 unsigned long min_nr_running = ULONG_MAX;
2293 struct sched_group *group_min = NULL, *group_leader = NULL;
2294#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
2296 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002297 busiest_load_per_task = busiest_nr_running = 0;
2298 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002299 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002300 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002301 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002302 load_idx = sd->newidle_idx;
2303 else
2304 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
2306 do {
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002307 unsigned long load, group_capacity;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 int local_group;
2309 int i;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002310 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002311 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 local_group = cpu_isset(this_cpu, group->cpumask);
2314
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002315 if (local_group)
2316 balance_cpu = first_cpu(group->cpumask);
2317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07002319 sum_weighted_load = sum_nr_running = avg_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002322 struct rq *rq;
2323
2324 if (!cpu_isset(i, *cpus))
2325 continue;
2326
2327 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07002328
Suresh Siddha9439aab2007-07-19 21:28:35 +02002329 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07002330 *sd_idle = 0;
2331
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002333 if (local_group) {
2334 if (idle_cpu(i) && !first_idle_cpu) {
2335 first_idle_cpu = 1;
2336 balance_cpu = i;
2337 }
2338
Nick Piggina2000572006-02-10 01:51:02 -08002339 load = target_load(i, load_idx);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002340 } else
Nick Piggina2000572006-02-10 01:51:02 -08002341 load = source_load(i, load_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
2343 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07002344 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002345 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 }
2347
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002348 /*
2349 * First idle cpu or the first cpu(busiest) in this sched group
2350 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02002351 * domains. In the newly idle case, we will allow all the cpu's
2352 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002353 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02002354 if (idle != CPU_NEWLY_IDLE && local_group &&
2355 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002356 *balance = 0;
2357 goto ret;
2358 }
2359
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07002361 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
2363 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002364 avg_load = sg_div_cpu_power(group,
2365 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Eric Dumazet5517d862007-05-08 00:32:57 -07002367 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (local_group) {
2370 this_load = avg_load;
2371 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002372 this_nr_running = sum_nr_running;
2373 this_load_per_task = sum_weighted_load;
2374 } else if (avg_load > max_load &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002375 sum_nr_running > group_capacity) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 max_load = avg_load;
2377 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002378 busiest_nr_running = sum_nr_running;
2379 busiest_load_per_task = sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002381
2382#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2383 /*
2384 * Busy processors will not participate in power savings
2385 * balance.
2386 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002387 if (idle == CPU_NOT_IDLE ||
2388 !(sd->flags & SD_POWERSAVINGS_BALANCE))
2389 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002390
2391 /*
2392 * If the local group is idle or completely loaded
2393 * no need to do power savings balance at this domain
2394 */
2395 if (local_group && (this_nr_running >= group_capacity ||
2396 !this_nr_running))
2397 power_savings_balance = 0;
2398
Ingo Molnardd41f592007-07-09 18:51:59 +02002399 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002400 * If a group is already running at full capacity or idle,
2401 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02002402 */
2403 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002404 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02002405 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002406
Ingo Molnardd41f592007-07-09 18:51:59 +02002407 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002408 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002409 * This is the group from where we need to pick up the load
2410 * for saving power
2411 */
2412 if ((sum_nr_running < min_nr_running) ||
2413 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002414 first_cpu(group->cpumask) <
2415 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002416 group_min = group;
2417 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002418 min_load_per_task = sum_weighted_load /
2419 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002420 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002421
Ingo Molnardd41f592007-07-09 18:51:59 +02002422 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002423 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02002424 * capacity but still has some space to pick up some load
2425 * from other group and save more power
2426 */
2427 if (sum_nr_running <= group_capacity - 1) {
2428 if (sum_nr_running > leader_nr_running ||
2429 (sum_nr_running == leader_nr_running &&
2430 first_cpu(group->cpumask) >
2431 first_cpu(group_leader->cpumask))) {
2432 group_leader = group;
2433 leader_nr_running = sum_nr_running;
2434 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002435 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002436group_next:
2437#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 group = group->next;
2439 } while (group != sd->groups);
2440
Peter Williams2dd73a42006-06-27 02:54:34 -07002441 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 goto out_balanced;
2443
2444 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
2445
2446 if (this_load >= avg_load ||
2447 100*max_load <= sd->imbalance_pct*this_load)
2448 goto out_balanced;
2449
Peter Williams2dd73a42006-06-27 02:54:34 -07002450 busiest_load_per_task /= busiest_nr_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 /*
2452 * We're trying to get all the cpus to the average_load, so we don't
2453 * want to push ourselves above the average load, nor do we wish to
2454 * reduce the max loaded cpu below the average load, as either of these
2455 * actions would just result in more rebalancing later, and ping-pong
2456 * tasks around. Thus we look for the minimum possible imbalance.
2457 * Negative imbalances (*we* are more loaded than anyone else) will
2458 * be counted as no imbalance for these purposes -- we can't fix that
2459 * by pulling tasks to us. Be careful of negative numbers as they'll
2460 * appear as very large values with unsigned longs.
2461 */
Peter Williams2dd73a42006-06-27 02:54:34 -07002462 if (max_load <= busiest_load_per_task)
2463 goto out_balanced;
2464
2465 /*
2466 * In the presence of smp nice balancing, certain scenarios can have
2467 * max load less than avg load(as we skip the groups at or below
2468 * its cpu_power, while calculating max_load..)
2469 */
2470 if (max_load < avg_load) {
2471 *imbalance = 0;
2472 goto small_imbalance;
2473 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002474
2475 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07002476 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07002479 *imbalance = min(max_pull * busiest->__cpu_power,
2480 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 / SCHED_LOAD_SCALE;
2482
Peter Williams2dd73a42006-06-27 02:54:34 -07002483 /*
2484 * if *imbalance is less than the average load per runnable task
2485 * there is no gaurantee that any tasks will be moved so we'll have
2486 * a think about bumping its value to force at least one task to be
2487 * moved
2488 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02002489 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07002490 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07002491 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Peter Williams2dd73a42006-06-27 02:54:34 -07002493small_imbalance:
2494 pwr_move = pwr_now = 0;
2495 imbn = 2;
2496 if (this_nr_running) {
2497 this_load_per_task /= this_nr_running;
2498 if (busiest_load_per_task > this_load_per_task)
2499 imbn = 1;
2500 } else
2501 this_load_per_task = SCHED_LOAD_SCALE;
2502
Ingo Molnardd41f592007-07-09 18:51:59 +02002503 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
2504 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002505 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 return busiest;
2507 }
2508
2509 /*
2510 * OK, we don't have enough imbalance to justify moving tasks,
2511 * however we may be able to increase total CPU power used by
2512 * moving them.
2513 */
2514
Eric Dumazet5517d862007-05-08 00:32:57 -07002515 pwr_now += busiest->__cpu_power *
2516 min(busiest_load_per_task, max_load);
2517 pwr_now += this->__cpu_power *
2518 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 pwr_now /= SCHED_LOAD_SCALE;
2520
2521 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07002522 tmp = sg_div_cpu_power(busiest,
2523 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07002525 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07002526 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
2528 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07002529 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08002530 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07002531 tmp = sg_div_cpu_power(this,
2532 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 else
Eric Dumazet5517d862007-05-08 00:32:57 -07002534 tmp = sg_div_cpu_power(this,
2535 busiest_load_per_task * SCHED_LOAD_SCALE);
2536 pwr_move += this->__cpu_power *
2537 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 pwr_move /= SCHED_LOAD_SCALE;
2539
2540 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02002541 if (pwr_move > pwr_now)
2542 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 }
2544
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 return busiest;
2546
2547out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002548#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002549 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002550 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002552 if (this == group_leader && group_leader != group_min) {
2553 *imbalance = min_load_per_task;
2554 return group_min;
2555 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002556#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002557ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 *imbalance = 0;
2559 return NULL;
2560}
2561
2562/*
2563 * find_busiest_queue - find the busiest runqueue among the cpus in group.
2564 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002565static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002566find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002567 unsigned long imbalance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002569 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07002570 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 int i;
2572
2573 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002574 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002575
2576 if (!cpu_isset(i, *cpus))
2577 continue;
2578
Ingo Molnar48f24c42006-07-03 00:25:40 -07002579 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02002580 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Ingo Molnardd41f592007-07-09 18:51:59 +02002582 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07002583 continue;
2584
Ingo Molnardd41f592007-07-09 18:51:59 +02002585 if (wl > max_load) {
2586 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07002587 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 }
2589 }
2590
2591 return busiest;
2592}
2593
2594/*
Nick Piggin77391d72005-06-25 14:57:30 -07002595 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
2596 * so long as it is large enough.
2597 */
2598#define MAX_PINNED_INTERVAL 512
2599
2600/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 * Check this_cpu to ensure it is balanced within domain. Attempt to move
2602 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002604static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002605 struct sched_domain *sd, enum cpu_idle_type idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002606 int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607{
Peter Williams43010652007-08-09 11:16:46 +02002608 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002611 struct rq *busiest;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002612 cpumask_t cpus = CPU_MASK_ALL;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002613 unsigned long flags;
Nick Piggin5969fe02005-09-10 00:26:19 -07002614
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002615 /*
2616 * When power savings policy is enabled for the parent domain, idle
2617 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02002618 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002619 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002620 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002621 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002622 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002623 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 schedstat_inc(sd, lb_cnt[idle]);
2626
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002627redo:
2628 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002629 &cpus, balance);
2630
Chen, Kenneth W06066712006-12-10 02:20:35 -08002631 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002632 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002633
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 if (!group) {
2635 schedstat_inc(sd, lb_nobusyg[idle]);
2636 goto out_balanced;
2637 }
2638
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002639 busiest = find_busiest_queue(group, idle, imbalance, &cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 if (!busiest) {
2641 schedstat_inc(sd, lb_nobusyq[idle]);
2642 goto out_balanced;
2643 }
2644
Nick Piggindb935db2005-06-25 14:57:11 -07002645 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
2647 schedstat_add(sd, lb_imbalance[idle], imbalance);
2648
Peter Williams43010652007-08-09 11:16:46 +02002649 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 if (busiest->nr_running > 1) {
2651 /*
2652 * Attempt to move tasks. If find_busiest_group has found
2653 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02002654 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 * correctly treated as an imbalance.
2656 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002657 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07002658 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02002659 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07002660 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07002661 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002662 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07002663
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002664 /*
2665 * some other cpu did the load balance for us.
2666 */
Peter Williams43010652007-08-09 11:16:46 +02002667 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002668 resched_cpu(this_cpu);
2669
Nick Piggin81026792005-06-25 14:57:07 -07002670 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002671 if (unlikely(all_pinned)) {
2672 cpu_clear(cpu_of(busiest), cpus);
2673 if (!cpus_empty(cpus))
2674 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07002675 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 }
Nick Piggin81026792005-06-25 14:57:07 -07002678
Peter Williams43010652007-08-09 11:16:46 +02002679 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 schedstat_inc(sd, lb_failed[idle]);
2681 sd->nr_balance_failed++;
2682
2683 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002685 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07002686
2687 /* don't kick the migration_thread, if the curr
2688 * task on busiest cpu can't be moved to this_cpu
2689 */
2690 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002691 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07002692 all_pinned = 1;
2693 goto out_one_pinned;
2694 }
2695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 if (!busiest->active_balance) {
2697 busiest->active_balance = 1;
2698 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07002699 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002701 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07002702 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 wake_up_process(busiest->migration_thread);
2704
2705 /*
2706 * We've kicked active balancing, reset the failure
2707 * counter.
2708 */
Nick Piggin39507452005-06-25 14:57:09 -07002709 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 }
Nick Piggin81026792005-06-25 14:57:07 -07002711 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 sd->nr_balance_failed = 0;
2713
Nick Piggin81026792005-06-25 14:57:07 -07002714 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 /* We were unbalanced, so reset the balancing interval */
2716 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07002717 } else {
2718 /*
2719 * If we've begun active balancing, start to back off. This
2720 * case may not be covered by the all_pinned logic if there
2721 * is only 1 task on the busy runqueue (because we don't call
2722 * move_tasks).
2723 */
2724 if (sd->balance_interval < sd->max_interval)
2725 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 }
2727
Peter Williams43010652007-08-09 11:16:46 +02002728 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002729 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002730 return -1;
Peter Williams43010652007-08-09 11:16:46 +02002731 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
2733out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 schedstat_inc(sd, lb_balanced[idle]);
2735
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002736 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07002737
2738out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07002740 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
2741 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 sd->balance_interval *= 2;
2743
Ingo Molnar48f24c42006-07-03 00:25:40 -07002744 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002745 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002746 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 return 0;
2748}
2749
2750/*
2751 * Check this_cpu to ensure it is balanced within domain. Attempt to move
2752 * tasks if there is an imbalance.
2753 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002754 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 * this_rq is locked.
2756 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07002757static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07002758load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759{
2760 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002761 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02002763 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07002764 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02002765 int all_pinned = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002766 cpumask_t cpus = CPU_MASK_ALL;
Nick Piggin5969fe02005-09-10 00:26:19 -07002767
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002768 /*
2769 * When power savings policy is enabled for the parent domain, idle
2770 * sibling can pick up load irrespective of busy siblings. In this case,
2771 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002772 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002773 */
2774 if (sd->flags & SD_SHARE_CPUPOWER &&
2775 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002776 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002778 schedstat_inc(sd, lb_cnt[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002779redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002780 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002781 &sd_idle, &cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002783 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002784 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 }
2786
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002787 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002788 &cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07002789 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002790 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002791 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 }
2793
Nick Piggindb935db2005-06-25 14:57:11 -07002794 BUG_ON(busiest == this_rq);
2795
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002796 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07002797
Peter Williams43010652007-08-09 11:16:46 +02002798 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07002799 if (busiest->nr_running > 1) {
2800 /* Attempt to move tasks */
2801 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002802 /* this_rq->clock is already updated */
2803 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02002804 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02002805 imbalance, sd, CPU_NEWLY_IDLE,
2806 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07002807 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002808
Suresh Siddha969bb4e2007-07-19 21:28:35 +02002809 if (unlikely(all_pinned)) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002810 cpu_clear(cpu_of(busiest), cpus);
2811 if (!cpus_empty(cpus))
2812 goto redo;
2813 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07002814 }
2815
Peter Williams43010652007-08-09 11:16:46 +02002816 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002817 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002818 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
2819 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002820 return -1;
2821 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002822 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Peter Williams43010652007-08-09 11:16:46 +02002824 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002825
2826out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002827 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07002828 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002829 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002830 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002831 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07002832
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002833 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834}
2835
2836/*
2837 * idle_balance is called by schedule() if this_cpu is about to become
2838 * idle. Attempts to pull tasks from other CPUs.
2839 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002840static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
2842 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02002843 int pulled_task = -1;
2844 unsigned long next_balance = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
2846 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07002847 unsigned long interval;
2848
2849 if (!(sd->flags & SD_LOAD_BALANCE))
2850 continue;
2851
2852 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002853 /* If we've pulled tasks over stop searching: */
Christoph Lameter1bd77f22006-12-10 02:20:27 -08002854 pulled_task = load_balance_newidle(this_cpu,
Christoph Lameter92c4ca52007-06-23 17:16:33 -07002855 this_rq, sd);
2856
2857 interval = msecs_to_jiffies(sd->balance_interval);
2858 if (time_after(next_balance, sd->last_balance + interval))
2859 next_balance = sd->last_balance + interval;
2860 if (pulled_task)
2861 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002863 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08002864 /*
2865 * We are going idle. next_balance may be set based on
2866 * a busy processor. So reset next_balance.
2867 */
2868 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02002869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870}
2871
2872/*
2873 * active_load_balance is run by migration threads. It pushes running tasks
2874 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
2875 * running on each physical CPU where possible, and avoids physical /
2876 * logical imbalances.
2877 *
2878 * Called with busiest_rq locked.
2879 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002880static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881{
Nick Piggin39507452005-06-25 14:57:09 -07002882 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002883 struct sched_domain *sd;
2884 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07002885
Ingo Molnar48f24c42006-07-03 00:25:40 -07002886 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07002887 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07002888 return;
2889
2890 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
2892 /*
Nick Piggin39507452005-06-25 14:57:09 -07002893 * This condition is "impossible", if it occurs
2894 * we need to fix it. Originally reported by
2895 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 */
Nick Piggin39507452005-06-25 14:57:09 -07002897 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
Nick Piggin39507452005-06-25 14:57:09 -07002899 /* move a task from busiest_rq to target_rq */
2900 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002901 update_rq_clock(busiest_rq);
2902 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Nick Piggin39507452005-06-25 14:57:09 -07002904 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002905 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07002906 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07002907 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07002908 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Ingo Molnar48f24c42006-07-03 00:25:40 -07002911 if (likely(sd)) {
2912 schedstat_inc(sd, alb_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Peter Williams43010652007-08-09 11:16:46 +02002914 if (move_one_task(target_rq, target_cpu, busiest_rq,
2915 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07002916 schedstat_inc(sd, alb_pushed);
2917 else
2918 schedstat_inc(sd, alb_failed);
2919 }
Nick Piggin39507452005-06-25 14:57:09 -07002920 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921}
2922
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002923#ifdef CONFIG_NO_HZ
2924static struct {
2925 atomic_t load_balancer;
2926 cpumask_t cpu_mask;
2927} nohz ____cacheline_aligned = {
2928 .load_balancer = ATOMIC_INIT(-1),
2929 .cpu_mask = CPU_MASK_NONE,
2930};
2931
Christoph Lameter7835b982006-12-10 02:20:22 -08002932/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002933 * This routine will try to nominate the ilb (idle load balancing)
2934 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
2935 * load balancing on behalf of all those cpus. If all the cpus in the system
2936 * go into this tickless mode, then there will be no ilb owner (as there is
2937 * no need for one) and all the cpus will sleep till the next wakeup event
2938 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08002939 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002940 * For the ilb owner, tick is not stopped. And this tick will be used
2941 * for idle load balancing. ilb owner will still be part of
2942 * nohz.cpu_mask..
2943 *
2944 * While stopping the tick, this cpu will become the ilb owner if there
2945 * is no other owner. And will be the owner till that cpu becomes busy
2946 * or if all cpus in the system stop their ticks at which point
2947 * there is no need for ilb owner.
2948 *
2949 * When the ilb owner becomes busy, it nominates another owner, during the
2950 * next busy scheduler_tick()
2951 */
2952int select_nohz_load_balancer(int stop_tick)
2953{
2954 int cpu = smp_processor_id();
2955
2956 if (stop_tick) {
2957 cpu_set(cpu, nohz.cpu_mask);
2958 cpu_rq(cpu)->in_nohz_recently = 1;
2959
2960 /*
2961 * If we are going offline and still the leader, give up!
2962 */
2963 if (cpu_is_offline(cpu) &&
2964 atomic_read(&nohz.load_balancer) == cpu) {
2965 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
2966 BUG();
2967 return 0;
2968 }
2969
2970 /* time for ilb owner also to sleep */
2971 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
2972 if (atomic_read(&nohz.load_balancer) == cpu)
2973 atomic_set(&nohz.load_balancer, -1);
2974 return 0;
2975 }
2976
2977 if (atomic_read(&nohz.load_balancer) == -1) {
2978 /* make me the ilb owner */
2979 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
2980 return 1;
2981 } else if (atomic_read(&nohz.load_balancer) == cpu)
2982 return 1;
2983 } else {
2984 if (!cpu_isset(cpu, nohz.cpu_mask))
2985 return 0;
2986
2987 cpu_clear(cpu, nohz.cpu_mask);
2988
2989 if (atomic_read(&nohz.load_balancer) == cpu)
2990 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
2991 BUG();
2992 }
2993 return 0;
2994}
2995#endif
2996
2997static DEFINE_SPINLOCK(balancing);
2998
2999/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003000 * It checks each scheduling domain to see if it is due to be balanced,
3001 * and initiates a balancing operation if so.
3002 *
3003 * Balancing parameters are set up in arch_init_sched_domains.
3004 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003005static inline void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08003006{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003007 int balance = 1;
3008 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08003009 unsigned long interval;
3010 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003011 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08003012 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003013 int update_next_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003015 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 if (!(sd->flags & SD_LOAD_BALANCE))
3017 continue;
3018
3019 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003020 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 interval *= sd->busy_factor;
3022
3023 /* scale ms to jiffies */
3024 interval = msecs_to_jiffies(interval);
3025 if (unlikely(!interval))
3026 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003027 if (interval > HZ*NR_CPUS/10)
3028 interval = HZ*NR_CPUS/10;
3029
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Christoph Lameter08c183f2006-12-10 02:20:29 -08003031 if (sd->flags & SD_SERIALIZE) {
3032 if (!spin_trylock(&balancing))
3033 goto out;
3034 }
3035
Christoph Lameterc9819f42006-12-10 02:20:25 -08003036 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003037 if (load_balance(cpu, rq, sd, idle, &balance)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003038 /*
3039 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07003040 * longer idle, or one of our SMT siblings is
3041 * not idle.
3042 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003043 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003045 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08003047 if (sd->flags & SD_SERIALIZE)
3048 spin_unlock(&balancing);
3049out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02003050 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08003051 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003052 update_next_balance = 1;
3053 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003054
3055 /*
3056 * Stop the load balance at this level. There is another
3057 * CPU in our sched group which is doing load balancing more
3058 * actively.
3059 */
3060 if (!balance)
3061 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02003063
3064 /*
3065 * next_balance will be updated only when there is a need.
3066 * When the cpu is attached to null domain for ex, it will not be
3067 * updated.
3068 */
3069 if (likely(update_next_balance))
3070 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003071}
3072
3073/*
3074 * run_rebalance_domains is triggered when needed from the scheduler tick.
3075 * In CONFIG_NO_HZ case, the idle load balance owner will do the
3076 * rebalancing for all the cpus for whom scheduler ticks are stopped.
3077 */
3078static void run_rebalance_domains(struct softirq_action *h)
3079{
Ingo Molnardd41f592007-07-09 18:51:59 +02003080 int this_cpu = smp_processor_id();
3081 struct rq *this_rq = cpu_rq(this_cpu);
3082 enum cpu_idle_type idle = this_rq->idle_at_tick ?
3083 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003084
Ingo Molnardd41f592007-07-09 18:51:59 +02003085 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003086
3087#ifdef CONFIG_NO_HZ
3088 /*
3089 * If this cpu is the owner for idle load balancing, then do the
3090 * balancing on behalf of the other idle cpus whose ticks are
3091 * stopped.
3092 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003093 if (this_rq->idle_at_tick &&
3094 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003095 cpumask_t cpus = nohz.cpu_mask;
3096 struct rq *rq;
3097 int balance_cpu;
3098
Ingo Molnardd41f592007-07-09 18:51:59 +02003099 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003100 for_each_cpu_mask(balance_cpu, cpus) {
3101 /*
3102 * If this cpu gets work to do, stop the load balancing
3103 * work being done for other cpus. Next load
3104 * balancing owner will pick it up.
3105 */
3106 if (need_resched())
3107 break;
3108
Oleg Nesterovde0cf892007-08-12 18:08:19 +02003109 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003110
3111 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003112 if (time_after(this_rq->next_balance, rq->next_balance))
3113 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003114 }
3115 }
3116#endif
3117}
3118
3119/*
3120 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
3121 *
3122 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
3123 * idle load balancing owner or decide to stop the periodic load balancing,
3124 * if the whole system is idle.
3125 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003126static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003127{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003128#ifdef CONFIG_NO_HZ
3129 /*
3130 * If we were in the nohz mode recently and busy at the current
3131 * scheduler tick, then check if we need to nominate new idle
3132 * load balancer.
3133 */
3134 if (rq->in_nohz_recently && !rq->idle_at_tick) {
3135 rq->in_nohz_recently = 0;
3136
3137 if (atomic_read(&nohz.load_balancer) == cpu) {
3138 cpu_clear(cpu, nohz.cpu_mask);
3139 atomic_set(&nohz.load_balancer, -1);
3140 }
3141
3142 if (atomic_read(&nohz.load_balancer) == -1) {
3143 /*
3144 * simple selection for now: Nominate the
3145 * first cpu in the nohz list to be the next
3146 * ilb owner.
3147 *
3148 * TBD: Traverse the sched domains and nominate
3149 * the nearest cpu in the nohz.cpu_mask.
3150 */
3151 int ilb = first_cpu(nohz.cpu_mask);
3152
3153 if (ilb != NR_CPUS)
3154 resched_cpu(ilb);
3155 }
3156 }
3157
3158 /*
3159 * If this cpu is idle and doing idle load balancing for all the
3160 * cpus with ticks stopped, is it time for that to stop?
3161 */
3162 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
3163 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3164 resched_cpu(cpu);
3165 return;
3166 }
3167
3168 /*
3169 * If this cpu is idle and the idle load balancing is done by
3170 * someone else, then no need raise the SCHED_SOFTIRQ
3171 */
3172 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
3173 cpu_isset(cpu, nohz.cpu_mask))
3174 return;
3175#endif
3176 if (time_after_eq(jiffies, rq->next_balance))
3177 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178}
Ingo Molnardd41f592007-07-09 18:51:59 +02003179
3180#else /* CONFIG_SMP */
3181
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182/*
3183 * on UP we do not need to balance between CPUs:
3184 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003185static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186{
3187}
Ingo Molnardd41f592007-07-09 18:51:59 +02003188
3189/* Avoid "used but not defined" warning on UP */
3190static int balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3191 unsigned long max_nr_move, unsigned long max_load_move,
3192 struct sched_domain *sd, enum cpu_idle_type idle,
3193 int *all_pinned, unsigned long *load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003194 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02003195{
3196 *load_moved = 0;
3197
3198 return 0;
3199}
3200
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201#endif
3202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203DEFINE_PER_CPU(struct kernel_stat, kstat);
3204
3205EXPORT_PER_CPU_SYMBOL(kstat);
3206
3207/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02003208 * Return p->sum_exec_runtime plus any more ns on the sched_clock
3209 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02003211unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003214 u64 ns, delta_exec;
3215 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003216
Ingo Molnar41b86e92007-07-09 18:51:58 +02003217 rq = task_rq_lock(p, &flags);
3218 ns = p->se.sum_exec_runtime;
3219 if (rq->curr == p) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02003220 update_rq_clock(rq);
3221 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003222 if ((s64)delta_exec > 0)
3223 ns += delta_exec;
3224 }
3225 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003226
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 return ns;
3228}
3229
3230/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 * Account user cpu time to a process.
3232 * @p: the process that the cpu time gets accounted to
3233 * @hardirq_offset: the offset to subtract from hardirq_count()
3234 * @cputime: the cpu time spent in user space since the last update
3235 */
3236void account_user_time(struct task_struct *p, cputime_t cputime)
3237{
3238 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3239 cputime64_t tmp;
3240
3241 p->utime = cputime_add(p->utime, cputime);
3242
3243 /* Add user time to cpustat. */
3244 tmp = cputime_to_cputime64(cputime);
3245 if (TASK_NICE(p) > 0)
3246 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3247 else
3248 cpustat->user = cputime64_add(cpustat->user, tmp);
3249}
3250
3251/*
3252 * Account system cpu time to a process.
3253 * @p: the process that the cpu time gets accounted to
3254 * @hardirq_offset: the offset to subtract from hardirq_count()
3255 * @cputime: the cpu time spent in kernel space since the last update
3256 */
3257void account_system_time(struct task_struct *p, int hardirq_offset,
3258 cputime_t cputime)
3259{
3260 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003261 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 cputime64_t tmp;
3263
3264 p->stime = cputime_add(p->stime, cputime);
3265
3266 /* Add system time to cpustat. */
3267 tmp = cputime_to_cputime64(cputime);
3268 if (hardirq_count() - hardirq_offset)
3269 cpustat->irq = cputime64_add(cpustat->irq, tmp);
3270 else if (softirq_count())
3271 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
3272 else if (p != rq->idle)
3273 cpustat->system = cputime64_add(cpustat->system, tmp);
3274 else if (atomic_read(&rq->nr_iowait) > 0)
3275 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3276 else
3277 cpustat->idle = cputime64_add(cpustat->idle, tmp);
3278 /* Account for system time used */
3279 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280}
3281
3282/*
3283 * Account for involuntary wait time.
3284 * @p: the process from which the cpu time has been stolen
3285 * @steal: the cpu time spent in involuntary wait
3286 */
3287void account_steal_time(struct task_struct *p, cputime_t steal)
3288{
3289 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3290 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07003291 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
3293 if (p == rq->idle) {
3294 p->stime = cputime_add(p->stime, steal);
3295 if (atomic_read(&rq->nr_iowait) > 0)
3296 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3297 else
3298 cpustat->idle = cputime64_add(cpustat->idle, tmp);
3299 } else
3300 cpustat->steal = cputime64_add(cpustat->steal, tmp);
3301}
3302
Christoph Lameter7835b982006-12-10 02:20:22 -08003303/*
3304 * This function gets called by the timer code, with HZ frequency.
3305 * We call it with interrupts disabled.
3306 *
3307 * It also gets called by the fork code, when changing the parent's
3308 * timeslices.
3309 */
3310void scheduler_tick(void)
3311{
Christoph Lameter7835b982006-12-10 02:20:22 -08003312 int cpu = smp_processor_id();
3313 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003314 struct task_struct *curr = rq->curr;
Ingo Molnar529c7722007-08-10 23:05:11 +02003315 u64 next_tick = rq->tick_timestamp + TICK_NSEC;
Christoph Lameter7835b982006-12-10 02:20:22 -08003316
Ingo Molnardd41f592007-07-09 18:51:59 +02003317 spin_lock(&rq->lock);
Ingo Molnar546fe3c2007-08-09 11:16:51 +02003318 __update_rq_clock(rq);
Ingo Molnar529c7722007-08-10 23:05:11 +02003319 /*
3320 * Let rq->clock advance by at least TICK_NSEC:
3321 */
3322 if (unlikely(rq->clock < next_tick))
3323 rq->clock = next_tick;
3324 rq->tick_timestamp = rq->clock;
Ingo Molnarf1a438d2007-08-09 11:16:45 +02003325 update_cpu_load(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003326 if (curr != rq->idle) /* FIXME: needed? */
3327 curr->sched_class->task_tick(rq, curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02003328 spin_unlock(&rq->lock);
3329
Christoph Lametere418e1c2006-12-10 02:20:23 -08003330#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02003331 rq->idle_at_tick = idle_cpu(cpu);
3332 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08003333#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334}
3335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
3337
3338void fastcall add_preempt_count(int val)
3339{
3340 /*
3341 * Underflow?
3342 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003343 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
3344 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 preempt_count() += val;
3346 /*
3347 * Spinlock count overflowing soon?
3348 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003349 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
3350 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351}
3352EXPORT_SYMBOL(add_preempt_count);
3353
3354void fastcall sub_preempt_count(int val)
3355{
3356 /*
3357 * Underflow?
3358 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003359 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
3360 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 /*
3362 * Is the spinlock portion underflowing?
3363 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003364 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
3365 !(preempt_count() & PREEMPT_MASK)))
3366 return;
3367
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 preempt_count() -= val;
3369}
3370EXPORT_SYMBOL(sub_preempt_count);
3371
3372#endif
3373
3374/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003375 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003377static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378{
Ingo Molnardd41f592007-07-09 18:51:59 +02003379 printk(KERN_ERR "BUG: scheduling while atomic: %s/0x%08x/%d\n",
3380 prev->comm, preempt_count(), prev->pid);
3381 debug_show_held_locks(prev);
3382 if (irqs_disabled())
3383 print_irqtrace_events(prev);
3384 dump_stack();
3385}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386
Ingo Molnardd41f592007-07-09 18:51:59 +02003387/*
3388 * Various schedule()-time debugging checks and statistics:
3389 */
3390static inline void schedule_debug(struct task_struct *prev)
3391{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 /*
3393 * Test if we are atomic. Since do_exit() needs to call into
3394 * schedule() atomically, we ignore that path for now.
3395 * Otherwise, whine if we are scheduling when we should not be.
3396 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003397 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
3398 __schedule_bug(prev);
3399
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
3401
Ingo Molnardd41f592007-07-09 18:51:59 +02003402 schedstat_inc(this_rq(), sched_cnt);
3403}
3404
3405/*
3406 * Pick up the highest-prio task:
3407 */
3408static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02003409pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02003410{
3411 struct sched_class *class;
3412 struct task_struct *p;
3413
3414 /*
3415 * Optimization: we know that if all tasks are in
3416 * the fair class we can call that function directly:
3417 */
3418 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003419 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003420 if (likely(p))
3421 return p;
3422 }
3423
3424 class = sched_class_highest;
3425 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003426 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003427 if (p)
3428 return p;
3429 /*
3430 * Will never be NULL as the idle class always
3431 * returns a non-NULL p:
3432 */
3433 class = class->next;
3434 }
3435}
3436
3437/*
3438 * schedule() is the main scheduler function.
3439 */
3440asmlinkage void __sched schedule(void)
3441{
3442 struct task_struct *prev, *next;
3443 long *switch_count;
3444 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02003445 int cpu;
3446
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447need_resched:
3448 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02003449 cpu = smp_processor_id();
3450 rq = cpu_rq(cpu);
3451 rcu_qsctr_inc(cpu);
3452 prev = rq->curr;
3453 switch_count = &prev->nivcsw;
3454
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 release_kernel_lock(prev);
3456need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
Ingo Molnardd41f592007-07-09 18:51:59 +02003458 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
3460 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 clear_tsk_need_resched(prev);
Ingo Molnarc1b3da32007-08-09 11:16:47 +02003462 __update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463
Ingo Molnardd41f592007-07-09 18:51:59 +02003464 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
3465 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
3466 unlikely(signal_pending(prev)))) {
3467 prev->state = TASK_RUNNING;
3468 } else {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02003469 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02003470 }
3471 switch_count = &prev->nvcsw;
3472 }
3473
3474 if (unlikely(!rq->nr_running))
3475 idle_balance(cpu, rq);
3476
Ingo Molnar31ee5292007-08-09 11:16:49 +02003477 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02003478 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
3480 sched_info_switch(prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02003481
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 rq->nr_switches++;
3484 rq->curr = next;
3485 ++*switch_count;
3486
Ingo Molnardd41f592007-07-09 18:51:59 +02003487 context_switch(rq, prev, next); /* unlocks the rq */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 } else
3489 spin_unlock_irq(&rq->lock);
3490
Ingo Molnardd41f592007-07-09 18:51:59 +02003491 if (unlikely(reacquire_kernel_lock(current) < 0)) {
3492 cpu = smp_processor_id();
3493 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 goto need_resched_nonpreemptible;
Ingo Molnardd41f592007-07-09 18:51:59 +02003495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 preempt_enable_no_resched();
3497 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
3498 goto need_resched;
3499}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500EXPORT_SYMBOL(schedule);
3501
3502#ifdef CONFIG_PREEMPT
3503/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003504 * this is the entry point to schedule() from in-kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 * off of preempt_enable. Kernel preemptions off return from interrupt
3506 * occur there and call schedule directly.
3507 */
3508asmlinkage void __sched preempt_schedule(void)
3509{
3510 struct thread_info *ti = current_thread_info();
3511#ifdef CONFIG_PREEMPT_BKL
3512 struct task_struct *task = current;
3513 int saved_lock_depth;
3514#endif
3515 /*
3516 * If there is a non-zero preempt_count or interrupts are disabled,
3517 * we do not want to preempt the current task. Just return..
3518 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07003519 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 return;
3521
3522need_resched:
3523 add_preempt_count(PREEMPT_ACTIVE);
3524 /*
3525 * We keep the big kernel semaphore locked, but we
3526 * clear ->lock_depth so that schedule() doesnt
3527 * auto-release the semaphore:
3528 */
3529#ifdef CONFIG_PREEMPT_BKL
3530 saved_lock_depth = task->lock_depth;
3531 task->lock_depth = -1;
3532#endif
3533 schedule();
3534#ifdef CONFIG_PREEMPT_BKL
3535 task->lock_depth = saved_lock_depth;
3536#endif
3537 sub_preempt_count(PREEMPT_ACTIVE);
3538
3539 /* we could miss a preemption opportunity between schedule and now */
3540 barrier();
3541 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
3542 goto need_resched;
3543}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544EXPORT_SYMBOL(preempt_schedule);
3545
3546/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003547 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 * off of irq context.
3549 * Note, that this is called and return with irqs disabled. This will
3550 * protect us against recursive calling from irq.
3551 */
3552asmlinkage void __sched preempt_schedule_irq(void)
3553{
3554 struct thread_info *ti = current_thread_info();
3555#ifdef CONFIG_PREEMPT_BKL
3556 struct task_struct *task = current;
3557 int saved_lock_depth;
3558#endif
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003559 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 BUG_ON(ti->preempt_count || !irqs_disabled());
3561
3562need_resched:
3563 add_preempt_count(PREEMPT_ACTIVE);
3564 /*
3565 * We keep the big kernel semaphore locked, but we
3566 * clear ->lock_depth so that schedule() doesnt
3567 * auto-release the semaphore:
3568 */
3569#ifdef CONFIG_PREEMPT_BKL
3570 saved_lock_depth = task->lock_depth;
3571 task->lock_depth = -1;
3572#endif
3573 local_irq_enable();
3574 schedule();
3575 local_irq_disable();
3576#ifdef CONFIG_PREEMPT_BKL
3577 task->lock_depth = saved_lock_depth;
3578#endif
3579 sub_preempt_count(PREEMPT_ACTIVE);
3580
3581 /* we could miss a preemption opportunity between schedule and now */
3582 barrier();
3583 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
3584 goto need_resched;
3585}
3586
3587#endif /* CONFIG_PREEMPT */
3588
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003589int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
3590 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591{
Ingo Molnar48f24c42006-07-03 00:25:40 -07003592 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594EXPORT_SYMBOL(default_wake_function);
3595
3596/*
3597 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
3598 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
3599 * number) then we wake all the non-exclusive tasks and one exclusive task.
3600 *
3601 * There are circumstances in which we can try to wake a task which has already
3602 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
3603 * zero in this (rare) case, and we handle it by continuing to scan the queue.
3604 */
3605static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
3606 int nr_exclusive, int sync, void *key)
3607{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02003608 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02003610 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003611 unsigned flags = curr->flags;
3612
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003614 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 break;
3616 }
3617}
3618
3619/**
3620 * __wake_up - wake up threads blocked on a waitqueue.
3621 * @q: the waitqueue
3622 * @mode: which threads
3623 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07003624 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 */
3626void fastcall __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003627 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628{
3629 unsigned long flags;
3630
3631 spin_lock_irqsave(&q->lock, flags);
3632 __wake_up_common(q, mode, nr_exclusive, 0, key);
3633 spin_unlock_irqrestore(&q->lock, flags);
3634}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635EXPORT_SYMBOL(__wake_up);
3636
3637/*
3638 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
3639 */
3640void fastcall __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
3641{
3642 __wake_up_common(q, mode, 1, 0, NULL);
3643}
3644
3645/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07003646 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 * @q: the waitqueue
3648 * @mode: which threads
3649 * @nr_exclusive: how many wake-one or wake-many threads to wake up
3650 *
3651 * The sync wakeup differs that the waker knows that it will schedule
3652 * away soon, so while the target thread will be woken up, it will not
3653 * be migrated to another CPU - ie. the two threads are 'synchronized'
3654 * with each other. This can prevent needless bouncing between CPUs.
3655 *
3656 * On UP it can prevent extra preemption.
3657 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003658void fastcall
3659__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660{
3661 unsigned long flags;
3662 int sync = 1;
3663
3664 if (unlikely(!q))
3665 return;
3666
3667 if (unlikely(!nr_exclusive))
3668 sync = 0;
3669
3670 spin_lock_irqsave(&q->lock, flags);
3671 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
3672 spin_unlock_irqrestore(&q->lock, flags);
3673}
3674EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
3675
3676void fastcall complete(struct completion *x)
3677{
3678 unsigned long flags;
3679
3680 spin_lock_irqsave(&x->wait.lock, flags);
3681 x->done++;
3682 __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
3683 1, 0, NULL);
3684 spin_unlock_irqrestore(&x->wait.lock, flags);
3685}
3686EXPORT_SYMBOL(complete);
3687
3688void fastcall complete_all(struct completion *x)
3689{
3690 unsigned long flags;
3691
3692 spin_lock_irqsave(&x->wait.lock, flags);
3693 x->done += UINT_MAX/2;
3694 __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
3695 0, 0, NULL);
3696 spin_unlock_irqrestore(&x->wait.lock, flags);
3697}
3698EXPORT_SYMBOL(complete_all);
3699
3700void fastcall __sched wait_for_completion(struct completion *x)
3701{
3702 might_sleep();
Ingo Molnar48f24c42006-07-03 00:25:40 -07003703
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 spin_lock_irq(&x->wait.lock);
3705 if (!x->done) {
3706 DECLARE_WAITQUEUE(wait, current);
3707
3708 wait.flags |= WQ_FLAG_EXCLUSIVE;
3709 __add_wait_queue_tail(&x->wait, &wait);
3710 do {
3711 __set_current_state(TASK_UNINTERRUPTIBLE);
3712 spin_unlock_irq(&x->wait.lock);
3713 schedule();
3714 spin_lock_irq(&x->wait.lock);
3715 } while (!x->done);
3716 __remove_wait_queue(&x->wait, &wait);
3717 }
3718 x->done--;
3719 spin_unlock_irq(&x->wait.lock);
3720}
3721EXPORT_SYMBOL(wait_for_completion);
3722
3723unsigned long fastcall __sched
3724wait_for_completion_timeout(struct completion *x, unsigned long timeout)
3725{
3726 might_sleep();
3727
3728 spin_lock_irq(&x->wait.lock);
3729 if (!x->done) {
3730 DECLARE_WAITQUEUE(wait, current);
3731
3732 wait.flags |= WQ_FLAG_EXCLUSIVE;
3733 __add_wait_queue_tail(&x->wait, &wait);
3734 do {
3735 __set_current_state(TASK_UNINTERRUPTIBLE);
3736 spin_unlock_irq(&x->wait.lock);
3737 timeout = schedule_timeout(timeout);
3738 spin_lock_irq(&x->wait.lock);
3739 if (!timeout) {
3740 __remove_wait_queue(&x->wait, &wait);
3741 goto out;
3742 }
3743 } while (!x->done);
3744 __remove_wait_queue(&x->wait, &wait);
3745 }
3746 x->done--;
3747out:
3748 spin_unlock_irq(&x->wait.lock);
3749 return timeout;
3750}
3751EXPORT_SYMBOL(wait_for_completion_timeout);
3752
3753int fastcall __sched wait_for_completion_interruptible(struct completion *x)
3754{
3755 int ret = 0;
3756
3757 might_sleep();
3758
3759 spin_lock_irq(&x->wait.lock);
3760 if (!x->done) {
3761 DECLARE_WAITQUEUE(wait, current);
3762
3763 wait.flags |= WQ_FLAG_EXCLUSIVE;
3764 __add_wait_queue_tail(&x->wait, &wait);
3765 do {
3766 if (signal_pending(current)) {
3767 ret = -ERESTARTSYS;
3768 __remove_wait_queue(&x->wait, &wait);
3769 goto out;
3770 }
3771 __set_current_state(TASK_INTERRUPTIBLE);
3772 spin_unlock_irq(&x->wait.lock);
3773 schedule();
3774 spin_lock_irq(&x->wait.lock);
3775 } while (!x->done);
3776 __remove_wait_queue(&x->wait, &wait);
3777 }
3778 x->done--;
3779out:
3780 spin_unlock_irq(&x->wait.lock);
3781
3782 return ret;
3783}
3784EXPORT_SYMBOL(wait_for_completion_interruptible);
3785
3786unsigned long fastcall __sched
3787wait_for_completion_interruptible_timeout(struct completion *x,
3788 unsigned long timeout)
3789{
3790 might_sleep();
3791
3792 spin_lock_irq(&x->wait.lock);
3793 if (!x->done) {
3794 DECLARE_WAITQUEUE(wait, current);
3795
3796 wait.flags |= WQ_FLAG_EXCLUSIVE;
3797 __add_wait_queue_tail(&x->wait, &wait);
3798 do {
3799 if (signal_pending(current)) {
3800 timeout = -ERESTARTSYS;
3801 __remove_wait_queue(&x->wait, &wait);
3802 goto out;
3803 }
3804 __set_current_state(TASK_INTERRUPTIBLE);
3805 spin_unlock_irq(&x->wait.lock);
3806 timeout = schedule_timeout(timeout);
3807 spin_lock_irq(&x->wait.lock);
3808 if (!timeout) {
3809 __remove_wait_queue(&x->wait, &wait);
3810 goto out;
3811 }
3812 } while (!x->done);
3813 __remove_wait_queue(&x->wait, &wait);
3814 }
3815 x->done--;
3816out:
3817 spin_unlock_irq(&x->wait.lock);
3818 return timeout;
3819}
3820EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
3821
Ingo Molnar0fec1712007-07-09 18:52:01 +02003822static inline void
3823sleep_on_head(wait_queue_head_t *q, wait_queue_t *wait, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824{
Ingo Molnar0fec1712007-07-09 18:52:01 +02003825 spin_lock_irqsave(&q->lock, *flags);
3826 __add_wait_queue(q, wait);
3827 spin_unlock(&q->lock);
3828}
3829
3830static inline void
3831sleep_on_tail(wait_queue_head_t *q, wait_queue_t *wait, unsigned long *flags)
3832{
3833 spin_lock_irq(&q->lock);
3834 __remove_wait_queue(q, wait);
3835 spin_unlock_irqrestore(&q->lock, *flags);
3836}
3837
3838void __sched interruptible_sleep_on(wait_queue_head_t *q)
3839{
3840 unsigned long flags;
3841 wait_queue_t wait;
3842
3843 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844
3845 current->state = TASK_INTERRUPTIBLE;
3846
Ingo Molnar0fec1712007-07-09 18:52:01 +02003847 sleep_on_head(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 schedule();
Ingo Molnar0fec1712007-07-09 18:52:01 +02003849 sleep_on_tail(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851EXPORT_SYMBOL(interruptible_sleep_on);
3852
Ingo Molnar0fec1712007-07-09 18:52:01 +02003853long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003854interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855{
Ingo Molnar0fec1712007-07-09 18:52:01 +02003856 unsigned long flags;
3857 wait_queue_t wait;
3858
3859 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860
3861 current->state = TASK_INTERRUPTIBLE;
3862
Ingo Molnar0fec1712007-07-09 18:52:01 +02003863 sleep_on_head(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 timeout = schedule_timeout(timeout);
Ingo Molnar0fec1712007-07-09 18:52:01 +02003865 sleep_on_tail(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
3867 return timeout;
3868}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869EXPORT_SYMBOL(interruptible_sleep_on_timeout);
3870
Ingo Molnar0fec1712007-07-09 18:52:01 +02003871void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872{
Ingo Molnar0fec1712007-07-09 18:52:01 +02003873 unsigned long flags;
3874 wait_queue_t wait;
3875
3876 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
3878 current->state = TASK_UNINTERRUPTIBLE;
3879
Ingo Molnar0fec1712007-07-09 18:52:01 +02003880 sleep_on_head(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 schedule();
Ingo Molnar0fec1712007-07-09 18:52:01 +02003882 sleep_on_tail(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884EXPORT_SYMBOL(sleep_on);
3885
Ingo Molnar0fec1712007-07-09 18:52:01 +02003886long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887{
Ingo Molnar0fec1712007-07-09 18:52:01 +02003888 unsigned long flags;
3889 wait_queue_t wait;
3890
3891 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892
3893 current->state = TASK_UNINTERRUPTIBLE;
3894
Ingo Molnar0fec1712007-07-09 18:52:01 +02003895 sleep_on_head(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 timeout = schedule_timeout(timeout);
Ingo Molnar0fec1712007-07-09 18:52:01 +02003897 sleep_on_tail(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898
3899 return timeout;
3900}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901EXPORT_SYMBOL(sleep_on_timeout);
3902
Ingo Molnarb29739f2006-06-27 02:54:51 -07003903#ifdef CONFIG_RT_MUTEXES
3904
3905/*
3906 * rt_mutex_setprio - set the current priority of a task
3907 * @p: task
3908 * @prio: prio value (kernel-internal form)
3909 *
3910 * This function changes the 'effective' priority of a task. It does
3911 * not touch ->normal_prio like __setscheduler().
3912 *
3913 * Used by the rt_mutex code to implement priority inheritance logic.
3914 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003915void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07003916{
3917 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02003918 int oldprio, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003919 struct rq *rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -07003920
3921 BUG_ON(prio < 0 || prio > MAX_PRIO);
3922
3923 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02003924 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003925
Andrew Mortond5f9f942007-05-08 20:27:06 -07003926 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02003927 on_rq = p->se.on_rq;
3928 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02003929 dequeue_task(rq, p, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02003930
3931 if (rt_prio(prio))
3932 p->sched_class = &rt_sched_class;
3933 else
3934 p->sched_class = &fair_sched_class;
3935
Ingo Molnarb29739f2006-06-27 02:54:51 -07003936 p->prio = prio;
3937
Ingo Molnardd41f592007-07-09 18:51:59 +02003938 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02003939 enqueue_task(rq, p, 0);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003940 /*
3941 * Reschedule if we are currently running on this runqueue and
Andrew Mortond5f9f942007-05-08 20:27:06 -07003942 * our priority decreased, or if we are not currently running on
3943 * this runqueue and our priority is higher than the current's
Ingo Molnarb29739f2006-06-27 02:54:51 -07003944 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07003945 if (task_running(rq, p)) {
3946 if (p->prio > oldprio)
3947 resched_task(rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02003948 } else {
3949 check_preempt_curr(rq, p);
3950 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07003951 }
3952 task_rq_unlock(rq, &flags);
3953}
3954
3955#endif
3956
Ingo Molnar36c8b582006-07-03 00:25:41 -07003957void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958{
Ingo Molnardd41f592007-07-09 18:51:59 +02003959 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003961 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962
3963 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
3964 return;
3965 /*
3966 * We have to be careful, if called from sys_setpriority(),
3967 * the task might be in the middle of scheduling on another CPU.
3968 */
3969 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02003970 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 /*
3972 * The RT priorities are set via sched_setscheduler(), but we still
3973 * allow the 'normal' nice value to be set - but as expected
3974 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02003975 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 */
Ingo Molnare05606d2007-07-09 18:51:59 +02003977 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 p->static_prio = NICE_TO_PRIO(nice);
3979 goto out_unlock;
3980 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003981 on_rq = p->se.on_rq;
3982 if (on_rq) {
Ingo Molnar69be72c2007-08-09 11:16:49 +02003983 dequeue_task(rq, p, 0);
Ingo Molnar79b5ddd2007-08-09 11:16:49 +02003984 dec_load(rq, p);
Peter Williams2dd73a42006-06-27 02:54:34 -07003985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07003988 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003989 old_prio = p->prio;
3990 p->prio = effective_prio(p);
3991 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
Ingo Molnardd41f592007-07-09 18:51:59 +02003993 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02003994 enqueue_task(rq, p, 0);
Ingo Molnar29b4b622007-08-09 11:16:49 +02003995 inc_load(rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07003997 * If the task increased its priority or is running and
3998 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004000 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 resched_task(rq->curr);
4002 }
4003out_unlock:
4004 task_rq_unlock(rq, &flags);
4005}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006EXPORT_SYMBOL(set_user_nice);
4007
Matt Mackalle43379f2005-05-01 08:59:00 -07004008/*
4009 * can_nice - check if a task can reduce its nice value
4010 * @p: task
4011 * @nice: nice value
4012 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004013int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07004014{
Matt Mackall024f4742005-08-18 11:24:19 -07004015 /* convert nice value [19,-20] to rlimit style value [1,40] */
4016 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004017
Matt Mackalle43379f2005-05-01 08:59:00 -07004018 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
4019 capable(CAP_SYS_NICE));
4020}
4021
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022#ifdef __ARCH_WANT_SYS_NICE
4023
4024/*
4025 * sys_nice - change the priority of the current process.
4026 * @increment: priority increment
4027 *
4028 * sys_setpriority is a more generic, but much slower function that
4029 * does similar things.
4030 */
4031asmlinkage long sys_nice(int increment)
4032{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004033 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
4035 /*
4036 * Setpriority might change our priority at the same moment.
4037 * We don't have to worry. Conceptually one call occurs first
4038 * and we have a single winner.
4039 */
Matt Mackalle43379f2005-05-01 08:59:00 -07004040 if (increment < -40)
4041 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 if (increment > 40)
4043 increment = 40;
4044
4045 nice = PRIO_TO_NICE(current->static_prio) + increment;
4046 if (nice < -20)
4047 nice = -20;
4048 if (nice > 19)
4049 nice = 19;
4050
Matt Mackalle43379f2005-05-01 08:59:00 -07004051 if (increment < 0 && !can_nice(current, nice))
4052 return -EPERM;
4053
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 retval = security_task_setnice(current, nice);
4055 if (retval)
4056 return retval;
4057
4058 set_user_nice(current, nice);
4059 return 0;
4060}
4061
4062#endif
4063
4064/**
4065 * task_prio - return the priority value of a given task.
4066 * @p: the task in question.
4067 *
4068 * This is the priority value as seen by users in /proc.
4069 * RT tasks are offset by -200. Normal tasks are centered
4070 * around 0, value goes from -16 to +15.
4071 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004072int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073{
4074 return p->prio - MAX_RT_PRIO;
4075}
4076
4077/**
4078 * task_nice - return the nice value of a given task.
4079 * @p: the task in question.
4080 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004081int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082{
4083 return TASK_NICE(p);
4084}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085EXPORT_SYMBOL_GPL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086
4087/**
4088 * idle_cpu - is a given cpu idle currently?
4089 * @cpu: the processor in question.
4090 */
4091int idle_cpu(int cpu)
4092{
4093 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
4094}
4095
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096/**
4097 * idle_task - return the idle task for a given cpu.
4098 * @cpu: the processor in question.
4099 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004100struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101{
4102 return cpu_rq(cpu)->idle;
4103}
4104
4105/**
4106 * find_process_by_pid - find a process with a matching PID value.
4107 * @pid: the pid in question.
4108 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004109static inline struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110{
4111 return pid ? find_task_by_pid(pid) : current;
4112}
4113
4114/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02004115static void
4116__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117{
Ingo Molnardd41f592007-07-09 18:51:59 +02004118 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004119
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02004121 switch (p->policy) {
4122 case SCHED_NORMAL:
4123 case SCHED_BATCH:
4124 case SCHED_IDLE:
4125 p->sched_class = &fair_sched_class;
4126 break;
4127 case SCHED_FIFO:
4128 case SCHED_RR:
4129 p->sched_class = &rt_sched_class;
4130 break;
4131 }
4132
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004134 p->normal_prio = normal_prio(p);
4135 /* we are holding p->pi_lock already */
4136 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07004137 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138}
4139
4140/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004141 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 * @p: the task in question.
4143 * @policy: new policy.
4144 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004145 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004146 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004148int sched_setscheduler(struct task_struct *p, int policy,
4149 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150{
Ingo Molnardd41f592007-07-09 18:51:59 +02004151 int retval, oldprio, oldpolicy = -1, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004153 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
Steven Rostedt66e53932006-06-27 02:54:44 -07004155 /* may grab non-irq protected spin_locks */
4156 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157recheck:
4158 /* double check policy once rq lock held */
4159 if (policy < 0)
4160 policy = oldpolicy = p->policy;
4161 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02004162 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
4163 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08004164 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 /*
4166 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02004167 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
4168 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 */
4170 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004171 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04004172 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02004174 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 return -EINVAL;
4176
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004177 /*
4178 * Allow unprivileged RT tasks to decrease priority:
4179 */
4180 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02004181 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004182 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004183
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004184 if (!lock_task_sighand(p, &flags))
4185 return -ESRCH;
4186 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
4187 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004188
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004189 /* can't set/change the rt policy */
4190 if (policy != p->policy && !rlim_rtprio)
4191 return -EPERM;
4192
4193 /* can't increase priority */
4194 if (param->sched_priority > p->rt_priority &&
4195 param->sched_priority > rlim_rtprio)
4196 return -EPERM;
4197 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004198 /*
4199 * Like positive nice levels, dont allow tasks to
4200 * move out of SCHED_IDLE either:
4201 */
4202 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
4203 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004204
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004205 /* can't change other user's priorities */
4206 if ((current->euid != p->euid) &&
4207 (current->euid != p->uid))
4208 return -EPERM;
4209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
4211 retval = security_task_setscheduler(p, policy, param);
4212 if (retval)
4213 return retval;
4214 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07004215 * make sure no PI-waiters arrive (or leave) while we are
4216 * changing the priority of the task:
4217 */
4218 spin_lock_irqsave(&p->pi_lock, flags);
4219 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 * To be able to change p->policy safely, the apropriate
4221 * runqueue lock must be held.
4222 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07004223 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 /* recheck policy now with rq lock held */
4225 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
4226 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004227 __task_rq_unlock(rq);
4228 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 goto recheck;
4230 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02004231 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004232 on_rq = p->se.on_rq;
Ingo Molnar2daa3572007-08-09 11:16:51 +02004233 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004234 deactivate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004236 __setscheduler(rq, p, policy, param->sched_priority);
4237 if (on_rq) {
4238 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 /*
4240 * Reschedule if we are currently running on this runqueue and
Andrew Mortond5f9f942007-05-08 20:27:06 -07004241 * our priority decreased, or if we are not currently running on
4242 * this runqueue and our priority is higher than the current's
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004244 if (task_running(rq, p)) {
4245 if (p->prio > oldprio)
4246 resched_task(rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02004247 } else {
4248 check_preempt_curr(rq, p);
4249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07004251 __task_rq_unlock(rq);
4252 spin_unlock_irqrestore(&p->pi_lock, flags);
4253
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07004254 rt_mutex_adjust_pi(p);
4255
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 return 0;
4257}
4258EXPORT_SYMBOL_GPL(sched_setscheduler);
4259
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004260static int
4261do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 struct sched_param lparam;
4264 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004265 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
4267 if (!param || pid < 0)
4268 return -EINVAL;
4269 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
4270 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004271
4272 rcu_read_lock();
4273 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004275 if (p != NULL)
4276 retval = sched_setscheduler(p, policy, &lparam);
4277 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07004278
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 return retval;
4280}
4281
4282/**
4283 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4284 * @pid: the pid in question.
4285 * @policy: new policy.
4286 * @param: structure containing the new RT priority.
4287 */
4288asmlinkage long sys_sched_setscheduler(pid_t pid, int policy,
4289 struct sched_param __user *param)
4290{
Jason Baronc21761f2006-01-18 17:43:03 -08004291 /* negative values for policy are not valid */
4292 if (policy < 0)
4293 return -EINVAL;
4294
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 return do_sched_setscheduler(pid, policy, param);
4296}
4297
4298/**
4299 * sys_sched_setparam - set/change the RT priority of a thread
4300 * @pid: the pid in question.
4301 * @param: structure containing the new RT priority.
4302 */
4303asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
4304{
4305 return do_sched_setscheduler(pid, -1, param);
4306}
4307
4308/**
4309 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4310 * @pid: the pid in question.
4311 */
4312asmlinkage long sys_sched_getscheduler(pid_t pid)
4313{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004314 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316
4317 if (pid < 0)
4318 goto out_nounlock;
4319
4320 retval = -ESRCH;
4321 read_lock(&tasklist_lock);
4322 p = find_process_by_pid(pid);
4323 if (p) {
4324 retval = security_task_getscheduler(p);
4325 if (!retval)
4326 retval = p->policy;
4327 }
4328 read_unlock(&tasklist_lock);
4329
4330out_nounlock:
4331 return retval;
4332}
4333
4334/**
4335 * sys_sched_getscheduler - get the RT priority of a thread
4336 * @pid: the pid in question.
4337 * @param: structure containing the RT priority.
4338 */
4339asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
4340{
4341 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004342 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
4345 if (!param || pid < 0)
4346 goto out_nounlock;
4347
4348 read_lock(&tasklist_lock);
4349 p = find_process_by_pid(pid);
4350 retval = -ESRCH;
4351 if (!p)
4352 goto out_unlock;
4353
4354 retval = security_task_getscheduler(p);
4355 if (retval)
4356 goto out_unlock;
4357
4358 lp.sched_priority = p->rt_priority;
4359 read_unlock(&tasklist_lock);
4360
4361 /*
4362 * This one might sleep, we cannot do it with a spinlock held ...
4363 */
4364 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
4365
4366out_nounlock:
4367 return retval;
4368
4369out_unlock:
4370 read_unlock(&tasklist_lock);
4371 return retval;
4372}
4373
4374long sched_setaffinity(pid_t pid, cpumask_t new_mask)
4375{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 cpumask_t cpus_allowed;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004377 struct task_struct *p;
4378 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
Gautham R Shenoy5be93612007-05-09 02:34:04 -07004380 mutex_lock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 read_lock(&tasklist_lock);
4382
4383 p = find_process_by_pid(pid);
4384 if (!p) {
4385 read_unlock(&tasklist_lock);
Gautham R Shenoy5be93612007-05-09 02:34:04 -07004386 mutex_unlock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 return -ESRCH;
4388 }
4389
4390 /*
4391 * It is not safe to call set_cpus_allowed with the
4392 * tasklist_lock held. We will bump the task_struct's
4393 * usage count and then drop tasklist_lock.
4394 */
4395 get_task_struct(p);
4396 read_unlock(&tasklist_lock);
4397
4398 retval = -EPERM;
4399 if ((current->euid != p->euid) && (current->euid != p->uid) &&
4400 !capable(CAP_SYS_NICE))
4401 goto out_unlock;
4402
David Quigleye7834f82006-06-23 02:03:59 -07004403 retval = security_task_setscheduler(p, 0, NULL);
4404 if (retval)
4405 goto out_unlock;
4406
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 cpus_allowed = cpuset_cpus_allowed(p);
4408 cpus_and(new_mask, new_mask, cpus_allowed);
4409 retval = set_cpus_allowed(p, new_mask);
4410
4411out_unlock:
4412 put_task_struct(p);
Gautham R Shenoy5be93612007-05-09 02:34:04 -07004413 mutex_unlock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 return retval;
4415}
4416
4417static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
4418 cpumask_t *new_mask)
4419{
4420 if (len < sizeof(cpumask_t)) {
4421 memset(new_mask, 0, sizeof(cpumask_t));
4422 } else if (len > sizeof(cpumask_t)) {
4423 len = sizeof(cpumask_t);
4424 }
4425 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
4426}
4427
4428/**
4429 * sys_sched_setaffinity - set the cpu affinity of a process
4430 * @pid: pid of the process
4431 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4432 * @user_mask_ptr: user-space pointer to the new cpu mask
4433 */
4434asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
4435 unsigned long __user *user_mask_ptr)
4436{
4437 cpumask_t new_mask;
4438 int retval;
4439
4440 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
4441 if (retval)
4442 return retval;
4443
4444 return sched_setaffinity(pid, new_mask);
4445}
4446
4447/*
4448 * Represents all cpu's present in the system
4449 * In systems capable of hotplug, this map could dynamically grow
4450 * as new cpu's are detected in the system via any platform specific
4451 * method, such as ACPI for e.g.
4452 */
4453
Andi Kleen4cef0c62006-01-11 22:44:57 +01004454cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455EXPORT_SYMBOL(cpu_present_map);
4456
4457#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01004458cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07004459EXPORT_SYMBOL(cpu_online_map);
4460
Andi Kleen4cef0c62006-01-11 22:44:57 +01004461cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07004462EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463#endif
4464
4465long sched_getaffinity(pid_t pid, cpumask_t *mask)
4466{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004467 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469
Gautham R Shenoy5be93612007-05-09 02:34:04 -07004470 mutex_lock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 read_lock(&tasklist_lock);
4472
4473 retval = -ESRCH;
4474 p = find_process_by_pid(pid);
4475 if (!p)
4476 goto out_unlock;
4477
David Quigleye7834f82006-06-23 02:03:59 -07004478 retval = security_task_getscheduler(p);
4479 if (retval)
4480 goto out_unlock;
4481
Jack Steiner2f7016d2006-02-01 03:05:18 -08004482 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483
4484out_unlock:
4485 read_unlock(&tasklist_lock);
Gautham R Shenoy5be93612007-05-09 02:34:04 -07004486 mutex_unlock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
Ulrich Drepper9531b622007-08-09 11:16:46 +02004488 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489}
4490
4491/**
4492 * sys_sched_getaffinity - get the cpu affinity of a process
4493 * @pid: pid of the process
4494 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4495 * @user_mask_ptr: user-space pointer to hold the current cpu mask
4496 */
4497asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
4498 unsigned long __user *user_mask_ptr)
4499{
4500 int ret;
4501 cpumask_t mask;
4502
4503 if (len < sizeof(cpumask_t))
4504 return -EINVAL;
4505
4506 ret = sched_getaffinity(pid, &mask);
4507 if (ret < 0)
4508 return ret;
4509
4510 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
4511 return -EFAULT;
4512
4513 return sizeof(cpumask_t);
4514}
4515
4516/**
4517 * sys_sched_yield - yield the current processor to other threads.
4518 *
Ingo Molnardd41f592007-07-09 18:51:59 +02004519 * This function yields the current CPU to other tasks. If there are no
4520 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 */
4522asmlinkage long sys_sched_yield(void)
4523{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004524 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525
4526 schedstat_inc(rq, yld_cnt);
Ingo Molnar1799e352007-09-19 23:34:46 +02004527 current->sched_class->yield_task(rq, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528
4529 /*
4530 * Since we are going to call schedule() anyway, there's
4531 * no need to preempt or enable interrupts:
4532 */
4533 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07004534 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 _raw_spin_unlock(&rq->lock);
4536 preempt_enable_no_resched();
4537
4538 schedule();
4539
4540 return 0;
4541}
4542
Andrew Mortone7b38402006-06-30 01:56:00 -07004543static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07004545#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
4546 __might_sleep(__FILE__, __LINE__);
4547#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07004548 /*
4549 * The BKS might be reacquired before we have dropped
4550 * PREEMPT_ACTIVE, which could trigger a second
4551 * cond_resched() call.
4552 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 do {
4554 add_preempt_count(PREEMPT_ACTIVE);
4555 schedule();
4556 sub_preempt_count(PREEMPT_ACTIVE);
4557 } while (need_resched());
4558}
4559
4560int __sched cond_resched(void)
4561{
Ingo Molnar94142322006-12-29 16:48:13 -08004562 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
4563 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 __cond_resched();
4565 return 1;
4566 }
4567 return 0;
4568}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569EXPORT_SYMBOL(cond_resched);
4570
4571/*
4572 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
4573 * call schedule, and on return reacquire the lock.
4574 *
4575 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
4576 * operations here to prevent schedule() from being called twice (once via
4577 * spin_unlock(), once by hand).
4578 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004579int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580{
Jan Kara6df3cec2005-06-13 15:52:32 -07004581 int ret = 0;
4582
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 if (need_lockbreak(lock)) {
4584 spin_unlock(lock);
4585 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07004586 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 spin_lock(lock);
4588 }
Ingo Molnar94142322006-12-29 16:48:13 -08004589 if (need_resched() && system_state == SYSTEM_RUNNING) {
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07004590 spin_release(&lock->dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 _raw_spin_unlock(lock);
4592 preempt_enable_no_resched();
4593 __cond_resched();
Jan Kara6df3cec2005-06-13 15:52:32 -07004594 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 }
Jan Kara6df3cec2005-06-13 15:52:32 -07004597 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599EXPORT_SYMBOL(cond_resched_lock);
4600
4601int __sched cond_resched_softirq(void)
4602{
4603 BUG_ON(!in_softirq());
4604
Ingo Molnar94142322006-12-29 16:48:13 -08004605 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07004606 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 __cond_resched();
4608 local_bh_disable();
4609 return 1;
4610 }
4611 return 0;
4612}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613EXPORT_SYMBOL(cond_resched_softirq);
4614
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615/**
4616 * yield - yield the current processor to other threads.
4617 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004618 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 * thread runnable and calls sys_sched_yield().
4620 */
4621void __sched yield(void)
4622{
4623 set_current_state(TASK_RUNNING);
4624 sys_sched_yield();
4625}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626EXPORT_SYMBOL(yield);
4627
4628/*
4629 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
4630 * that process accounting knows that this is a task in IO wait state.
4631 *
4632 * But don't do that if it is a deliberate, throttling IO wait (this task
4633 * has set its backing_dev_info: the queue against which it should throttle)
4634 */
4635void __sched io_schedule(void)
4636{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004637 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004639 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 atomic_inc(&rq->nr_iowait);
4641 schedule();
4642 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004643 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645EXPORT_SYMBOL(io_schedule);
4646
4647long __sched io_schedule_timeout(long timeout)
4648{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004649 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 long ret;
4651
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004652 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 atomic_inc(&rq->nr_iowait);
4654 ret = schedule_timeout(timeout);
4655 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004656 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 return ret;
4658}
4659
4660/**
4661 * sys_sched_get_priority_max - return maximum RT priority.
4662 * @policy: scheduling class.
4663 *
4664 * this syscall returns the maximum rt_priority that can be used
4665 * by a given scheduling class.
4666 */
4667asmlinkage long sys_sched_get_priority_max(int policy)
4668{
4669 int ret = -EINVAL;
4670
4671 switch (policy) {
4672 case SCHED_FIFO:
4673 case SCHED_RR:
4674 ret = MAX_USER_RT_PRIO-1;
4675 break;
4676 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08004677 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02004678 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 ret = 0;
4680 break;
4681 }
4682 return ret;
4683}
4684
4685/**
4686 * sys_sched_get_priority_min - return minimum RT priority.
4687 * @policy: scheduling class.
4688 *
4689 * this syscall returns the minimum rt_priority that can be used
4690 * by a given scheduling class.
4691 */
4692asmlinkage long sys_sched_get_priority_min(int policy)
4693{
4694 int ret = -EINVAL;
4695
4696 switch (policy) {
4697 case SCHED_FIFO:
4698 case SCHED_RR:
4699 ret = 1;
4700 break;
4701 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08004702 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02004703 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 ret = 0;
4705 }
4706 return ret;
4707}
4708
4709/**
4710 * sys_sched_rr_get_interval - return the default timeslice of a process.
4711 * @pid: pid of the process.
4712 * @interval: userspace pointer to the timeslice value.
4713 *
4714 * this syscall writes the default timeslice value of a given process
4715 * into the user-space timespec buffer. A value of '0' means infinity.
4716 */
4717asmlinkage
4718long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
4719{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004720 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 int retval = -EINVAL;
4722 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723
4724 if (pid < 0)
4725 goto out_nounlock;
4726
4727 retval = -ESRCH;
4728 read_lock(&tasklist_lock);
4729 p = find_process_by_pid(pid);
4730 if (!p)
4731 goto out_unlock;
4732
4733 retval = security_task_getscheduler(p);
4734 if (retval)
4735 goto out_unlock;
4736
Peter Williamsb78709c2006-06-26 16:58:00 +10004737 jiffies_to_timespec(p->policy == SCHED_FIFO ?
Ingo Molnardd41f592007-07-09 18:51:59 +02004738 0 : static_prio_timeslice(p->static_prio), &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 read_unlock(&tasklist_lock);
4740 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
4741out_nounlock:
4742 return retval;
4743out_unlock:
4744 read_unlock(&tasklist_lock);
4745 return retval;
4746}
4747
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004748static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07004749
4750static void show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004753 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 state = p->state ? __ffs(p->state) + 1 : 0;
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004756 printk("%-13.13s %c", p->comm,
4757 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02004758#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 if (state == TASK_RUNNING)
Ingo Molnar4bd77322007-07-11 21:21:47 +02004760 printk(" running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 else
Ingo Molnar4bd77322007-07-11 21:21:47 +02004762 printk(" %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763#else
4764 if (state == TASK_RUNNING)
Ingo Molnar4bd77322007-07-11 21:21:47 +02004765 printk(" running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 else
4767 printk(" %016lx ", thread_saved_pc(p));
4768#endif
4769#ifdef CONFIG_DEBUG_STACK_USAGE
4770 {
Al Viro10ebffd2005-11-13 16:06:56 -08004771 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 while (!*n)
4773 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08004774 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 }
4776#endif
Ingo Molnar4bd77322007-07-11 21:21:47 +02004777 printk("%5lu %5d %6d\n", free, p->pid, p->parent->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778
4779 if (state != TASK_RUNNING)
4780 show_stack(p, NULL);
4781}
4782
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004783void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004785 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786
Ingo Molnar4bd77322007-07-11 21:21:47 +02004787#if BITS_PER_LONG == 32
4788 printk(KERN_INFO
4789 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02004791 printk(KERN_INFO
4792 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793#endif
4794 read_lock(&tasklist_lock);
4795 do_each_thread(g, p) {
4796 /*
4797 * reset the NMI-timeout, listing all files on a slow
4798 * console might take alot of time:
4799 */
4800 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07004801 if (!state_filter || (p->state & state_filter))
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004802 show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 } while_each_thread(g, p);
4804
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07004805 touch_all_softlockup_watchdogs();
4806
Ingo Molnardd41f592007-07-09 18:51:59 +02004807#ifdef CONFIG_SCHED_DEBUG
4808 sysrq_sched_debug_show();
4809#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004811 /*
4812 * Only show locks if all tasks are dumped:
4813 */
4814 if (state_filter == -1)
4815 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816}
4817
Ingo Molnar1df21052007-07-09 18:51:58 +02004818void __cpuinit init_idle_bootup_task(struct task_struct *idle)
4819{
Ingo Molnardd41f592007-07-09 18:51:59 +02004820 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02004821}
4822
Ingo Molnarf340c0d2005-06-28 16:40:42 +02004823/**
4824 * init_idle - set up an idle thread for a given CPU
4825 * @idle: task in question
4826 * @cpu: cpu the idle task belongs to
4827 *
4828 * NOTE: this function does not set the idle thread's NEED_RESCHED
4829 * flag, to make booting more robust.
4830 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07004831void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004833 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 unsigned long flags;
4835
Ingo Molnardd41f592007-07-09 18:51:59 +02004836 __sched_fork(idle);
4837 idle->se.exec_start = sched_clock();
4838
Ingo Molnarb29739f2006-06-27 02:54:51 -07004839 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004841 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842
4843 spin_lock_irqsave(&rq->lock, flags);
4844 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07004845#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
4846 idle->oncpu = 1;
4847#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 spin_unlock_irqrestore(&rq->lock, flags);
4849
4850 /* Set the preempt count _outside_ the spinlocks! */
4851#if defined(CONFIG_PREEMPT) && !defined(CONFIG_PREEMPT_BKL)
Al Viroa1261f52005-11-13 16:06:55 -08004852 task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853#else
Al Viroa1261f52005-11-13 16:06:55 -08004854 task_thread_info(idle)->preempt_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004856 /*
4857 * The idle tasks have their own, simple scheduling class:
4858 */
4859 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860}
4861
4862/*
4863 * In a system that switches off the HZ timer nohz_cpu_mask
4864 * indicates which cpus entered this state. This is used
4865 * in the rcu update to wait only for active cpus. For system
4866 * which do not switch off the HZ timer nohz_cpu_mask should
4867 * always be CPU_MASK_NONE.
4868 */
4869cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
4870
4871#ifdef CONFIG_SMP
4872/*
4873 * This is how migration works:
4874 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07004875 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 * runqueue and wake up that CPU's migration thread.
4877 * 2) we down() the locked semaphore => thread blocks.
4878 * 3) migration thread wakes up (implicitly it forces the migrated
4879 * thread off the CPU)
4880 * 4) it gets the migration request and checks whether the migrated
4881 * task is still in the wrong runqueue.
4882 * 5) if it's in the wrong runqueue then the migration thread removes
4883 * it and puts it into the right queue.
4884 * 6) migration thread up()s the semaphore.
4885 * 7) we wake up and the migration is done.
4886 */
4887
4888/*
4889 * Change a given task's CPU affinity. Migrate the thread to a
4890 * proper CPU and schedule it away if the CPU it's executing on
4891 * is removed from the allowed bitmask.
4892 *
4893 * NOTE: the caller must have a valid reference to the task, the
4894 * task must not exit() & deallocate itself prematurely. The
4895 * call is not atomic; no spinlocks may be held.
4896 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004897int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004899 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004901 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004902 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903
4904 rq = task_rq_lock(p, &flags);
4905 if (!cpus_intersects(new_mask, cpu_online_map)) {
4906 ret = -EINVAL;
4907 goto out;
4908 }
4909
4910 p->cpus_allowed = new_mask;
4911 /* Can the task run on the task's current CPU? If so, we're done */
4912 if (cpu_isset(task_cpu(p), new_mask))
4913 goto out;
4914
4915 if (migrate_task(p, any_online_cpu(new_mask), &req)) {
4916 /* Need help from migration thread: drop lock and wait. */
4917 task_rq_unlock(rq, &flags);
4918 wake_up_process(rq->migration_thread);
4919 wait_for_completion(&req.done);
4920 tlb_migrate_finish(p->mm);
4921 return 0;
4922 }
4923out:
4924 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004925
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 return ret;
4927}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928EXPORT_SYMBOL_GPL(set_cpus_allowed);
4929
4930/*
4931 * Move (not current) task off this cpu, onto dest cpu. We're doing
4932 * this because either it can't run here any more (set_cpus_allowed()
4933 * away from this CPU, or CPU going down), or because we're
4934 * attempting to rebalance this task on exec (sched_exec).
4935 *
4936 * So we race with normal scheduler movements, but that's OK, as long
4937 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07004938 *
4939 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07004941static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004943 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02004944 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945
4946 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07004947 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
4949 rq_src = cpu_rq(src_cpu);
4950 rq_dest = cpu_rq(dest_cpu);
4951
4952 double_rq_lock(rq_src, rq_dest);
4953 /* Already moved. */
4954 if (task_cpu(p) != src_cpu)
4955 goto out;
4956 /* Affinity changed (again). */
4957 if (!cpu_isset(dest_cpu, p->cpus_allowed))
4958 goto out;
4959
Ingo Molnardd41f592007-07-09 18:51:59 +02004960 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02004961 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004962 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02004963
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004965 if (on_rq) {
4966 activate_task(rq_dest, p, 0);
4967 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07004969 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970out:
4971 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07004972 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973}
4974
4975/*
4976 * migration_thread - this is a highprio system thread that performs
4977 * thread migration by bumping thread off CPU then 'pushing' onto
4978 * another runqueue.
4979 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004980static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004983 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984
4985 rq = cpu_rq(cpu);
4986 BUG_ON(rq->migration_thread != current);
4987
4988 set_current_state(TASK_INTERRUPTIBLE);
4989 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07004990 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 spin_lock_irq(&rq->lock);
4994
4995 if (cpu_is_offline(cpu)) {
4996 spin_unlock_irq(&rq->lock);
4997 goto wait_to_die;
4998 }
4999
5000 if (rq->active_balance) {
5001 active_load_balance(rq, cpu);
5002 rq->active_balance = 0;
5003 }
5004
5005 head = &rq->migration_queue;
5006
5007 if (list_empty(head)) {
5008 spin_unlock_irq(&rq->lock);
5009 schedule();
5010 set_current_state(TASK_INTERRUPTIBLE);
5011 continue;
5012 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07005013 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 list_del_init(head->next);
5015
Nick Piggin674311d2005-06-25 14:57:27 -07005016 spin_unlock(&rq->lock);
5017 __migrate_task(req->task, cpu, req->dest_cpu);
5018 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019
5020 complete(&req->done);
5021 }
5022 __set_current_state(TASK_RUNNING);
5023 return 0;
5024
5025wait_to_die:
5026 /* Wait for kthread_stop */
5027 set_current_state(TASK_INTERRUPTIBLE);
5028 while (!kthread_should_stop()) {
5029 schedule();
5030 set_current_state(TASK_INTERRUPTIBLE);
5031 }
5032 __set_current_state(TASK_RUNNING);
5033 return 0;
5034}
5035
5036#ifdef CONFIG_HOTPLUG_CPU
Kirill Korotaev054b9102006-12-10 02:20:11 -08005037/*
5038 * Figure out where task on dead CPU should go, use force if neccessary.
5039 * NOTE: interrupts should be disabled by the caller
5040 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005041static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042{
Kirill Korotaevefc30812006-06-27 02:54:32 -07005043 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005045 struct rq *rq;
5046 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047
Kirill Korotaevefc30812006-06-27 02:54:32 -07005048restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 /* On same node? */
5050 mask = node_to_cpumask(cpu_to_node(dead_cpu));
Ingo Molnar48f24c42006-07-03 00:25:40 -07005051 cpus_and(mask, mask, p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 dest_cpu = any_online_cpu(mask);
5053
5054 /* On any allowed CPU? */
5055 if (dest_cpu == NR_CPUS)
Ingo Molnar48f24c42006-07-03 00:25:40 -07005056 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057
5058 /* No more Mr. Nice Guy. */
5059 if (dest_cpu == NR_CPUS) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07005060 rq = task_rq_lock(p, &flags);
5061 cpus_setall(p->cpus_allowed);
5062 dest_cpu = any_online_cpu(p->cpus_allowed);
Kirill Korotaevefc30812006-06-27 02:54:32 -07005063 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064
5065 /*
5066 * Don't tell them about moving exiting tasks or
5067 * kernel threads (both mm NULL), since they never
5068 * leave kernel.
5069 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005070 if (p->mm && printk_ratelimit())
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 printk(KERN_INFO "process %d (%s) no "
5072 "longer affine to cpu%d\n",
Ingo Molnar48f24c42006-07-03 00:25:40 -07005073 p->pid, p->comm, dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07005075 if (!__migrate_task(p, dead_cpu, dest_cpu))
Kirill Korotaevefc30812006-06-27 02:54:32 -07005076 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077}
5078
5079/*
5080 * While a dead CPU has no uninterruptible tasks queued at this point,
5081 * it might still have a nonzero ->nr_uninterruptible counter, because
5082 * for performance reasons the counter is not stricly tracking tasks to
5083 * their home CPUs. So we just add the counter to another CPU's counter,
5084 * to keep the global sum constant after CPU-down:
5085 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07005086static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005088 struct rq *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 unsigned long flags;
5090
5091 local_irq_save(flags);
5092 double_rq_lock(rq_src, rq_dest);
5093 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
5094 rq_src->nr_uninterruptible = 0;
5095 double_rq_unlock(rq_src, rq_dest);
5096 local_irq_restore(flags);
5097}
5098
5099/* Run through task list and migrate tasks from the dead cpu. */
5100static void migrate_live_tasks(int src_cpu)
5101{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005102 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103
5104 write_lock_irq(&tasklist_lock);
5105
Ingo Molnar48f24c42006-07-03 00:25:40 -07005106 do_each_thread(t, p) {
5107 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 continue;
5109
Ingo Molnar48f24c42006-07-03 00:25:40 -07005110 if (task_cpu(p) == src_cpu)
5111 move_task_off_dead_cpu(src_cpu, p);
5112 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113
5114 write_unlock_irq(&tasklist_lock);
5115}
5116
Ingo Molnardd41f592007-07-09 18:51:59 +02005117/*
5118 * Schedules idle task to be the next runnable task on current CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 * It does so by boosting its priority to highest possible and adding it to
Ingo Molnar48f24c42006-07-03 00:25:40 -07005120 * the _front_ of the runqueue. Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 */
5122void sched_idle_next(void)
5123{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005124 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07005125 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 struct task_struct *p = rq->idle;
5127 unsigned long flags;
5128
5129 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005130 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131
Ingo Molnar48f24c42006-07-03 00:25:40 -07005132 /*
5133 * Strictly not necessary since rest of the CPUs are stopped by now
5134 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 */
5136 spin_lock_irqsave(&rq->lock, flags);
5137
Ingo Molnardd41f592007-07-09 18:51:59 +02005138 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005139
5140 /* Add idle task to the _front_ of its priority queue: */
Ingo Molnardd41f592007-07-09 18:51:59 +02005141 activate_idle_task(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142
5143 spin_unlock_irqrestore(&rq->lock, flags);
5144}
5145
Ingo Molnar48f24c42006-07-03 00:25:40 -07005146/*
5147 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 * offline.
5149 */
5150void idle_task_exit(void)
5151{
5152 struct mm_struct *mm = current->active_mm;
5153
5154 BUG_ON(cpu_online(smp_processor_id()));
5155
5156 if (mm != &init_mm)
5157 switch_mm(mm, &init_mm, current);
5158 mmdrop(mm);
5159}
5160
Kirill Korotaev054b9102006-12-10 02:20:11 -08005161/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005162static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005164 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
5166 /* Must be exiting, otherwise would be on tasklist. */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005167 BUG_ON(p->exit_state != EXIT_ZOMBIE && p->exit_state != EXIT_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168
5169 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07005170 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171
Ingo Molnar48f24c42006-07-03 00:25:40 -07005172 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173
5174 /*
5175 * Drop lock around migration; if someone else moves it,
5176 * that's OK. No task can be added to this CPU, so iteration is
5177 * fine.
Kirill Korotaev054b9102006-12-10 02:20:11 -08005178 * NOTE: interrupts should be left disabled --dev@
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 */
Kirill Korotaev054b9102006-12-10 02:20:11 -08005180 spin_unlock(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005181 move_task_off_dead_cpu(dead_cpu, p);
Kirill Korotaev054b9102006-12-10 02:20:11 -08005182 spin_lock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183
Ingo Molnar48f24c42006-07-03 00:25:40 -07005184 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185}
5186
5187/* release_task() removes task from tasklist, so we won't find dead tasks. */
5188static void migrate_dead_tasks(unsigned int dead_cpu)
5189{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005190 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005191 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192
Ingo Molnardd41f592007-07-09 18:51:59 +02005193 for ( ; ; ) {
5194 if (!rq->nr_running)
5195 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02005196 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02005197 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02005198 if (!next)
5199 break;
5200 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02005201
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 }
5203}
5204#endif /* CONFIG_HOTPLUG_CPU */
5205
Nick Piggine692ab52007-07-26 13:40:43 +02005206#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5207
5208static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005209 {
5210 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005211 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005212 },
Nick Piggine692ab52007-07-26 13:40:43 +02005213 {0,},
5214};
5215
5216static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005217 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005218 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005219 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005220 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005221 .child = sd_ctl_dir,
5222 },
Nick Piggine692ab52007-07-26 13:40:43 +02005223 {0,},
5224};
5225
5226static struct ctl_table *sd_alloc_ctl_entry(int n)
5227{
5228 struct ctl_table *entry =
5229 kmalloc(n * sizeof(struct ctl_table), GFP_KERNEL);
5230
5231 BUG_ON(!entry);
5232 memset(entry, 0, n * sizeof(struct ctl_table));
5233
5234 return entry;
5235}
5236
5237static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02005238set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02005239 const char *procname, void *data, int maxlen,
5240 mode_t mode, proc_handler *proc_handler)
5241{
Nick Piggine692ab52007-07-26 13:40:43 +02005242 entry->procname = procname;
5243 entry->data = data;
5244 entry->maxlen = maxlen;
5245 entry->mode = mode;
5246 entry->proc_handler = proc_handler;
5247}
5248
5249static struct ctl_table *
5250sd_alloc_ctl_domain_table(struct sched_domain *sd)
5251{
5252 struct ctl_table *table = sd_alloc_ctl_entry(14);
5253
Alexey Dobriyane0361852007-08-09 11:16:46 +02005254 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005255 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005256 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005257 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005258 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005259 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005260 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005261 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005262 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005263 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005264 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005265 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005266 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005267 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005268 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02005269 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005270 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02005271 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005272 set_table_entry(&table[10], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02005273 &sd->cache_nice_tries,
5274 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005275 set_table_entry(&table[12], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02005276 sizeof(int), 0644, proc_dointvec_minmax);
5277
5278 return table;
5279}
5280
5281static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
5282{
5283 struct ctl_table *entry, *table;
5284 struct sched_domain *sd;
5285 int domain_num = 0, i;
5286 char buf[32];
5287
5288 for_each_domain(cpu, sd)
5289 domain_num++;
5290 entry = table = sd_alloc_ctl_entry(domain_num + 1);
5291
5292 i = 0;
5293 for_each_domain(cpu, sd) {
5294 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005295 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005296 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005297 entry->child = sd_alloc_ctl_domain_table(sd);
5298 entry++;
5299 i++;
5300 }
5301 return table;
5302}
5303
5304static struct ctl_table_header *sd_sysctl_header;
5305static void init_sched_domain_sysctl(void)
5306{
5307 int i, cpu_num = num_online_cpus();
5308 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
5309 char buf[32];
5310
5311 sd_ctl_dir[0].child = entry;
5312
5313 for (i = 0; i < cpu_num; i++, entry++) {
5314 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005315 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005316 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005317 entry->child = sd_alloc_ctl_cpu_table(i);
5318 }
5319 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
5320}
5321#else
5322static void init_sched_domain_sysctl(void)
5323{
5324}
5325#endif
5326
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327/*
5328 * migration_call - callback that gets triggered when a CPU is added.
5329 * Here we can start up the necessary migration thread for the new CPU.
5330 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005331static int __cpuinit
5332migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005335 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005337 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338
5339 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005340 case CPU_LOCK_ACQUIRE:
5341 mutex_lock(&sched_hotcpu_mutex);
5342 break;
5343
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005345 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02005346 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 if (IS_ERR(p))
5348 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 kthread_bind(p, cpu);
5350 /* Must be high prio: stop_machine expects to yield to it. */
5351 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02005352 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 task_rq_unlock(rq, &flags);
5354 cpu_rq(cpu)->migration_thread = p;
5355 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005356
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005358 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 /* Strictly unneccessary, as first user will wake it. */
5360 wake_up_process(cpu_rq(cpu)->migration_thread);
5361 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005362
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363#ifdef CONFIG_HOTPLUG_CPU
5364 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005365 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07005366 if (!cpu_rq(cpu)->migration_thread)
5367 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08005369 kthread_bind(cpu_rq(cpu)->migration_thread,
5370 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 kthread_stop(cpu_rq(cpu)->migration_thread);
5372 cpu_rq(cpu)->migration_thread = NULL;
5373 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005374
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005376 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 migrate_live_tasks(cpu);
5378 rq = cpu_rq(cpu);
5379 kthread_stop(rq->migration_thread);
5380 rq->migration_thread = NULL;
5381 /* Idle task back to normal (off runqueue, low prio) */
5382 rq = task_rq_lock(rq->idle, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005383 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005384 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02005386 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
5387 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 migrate_dead_tasks(cpu);
5389 task_rq_unlock(rq, &flags);
5390 migrate_nr_uninterruptible(rq);
5391 BUG_ON(rq->nr_running != 0);
5392
5393 /* No need to migrate the tasks: it was best-effort if
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005394 * they didn't take sched_hotcpu_mutex. Just wake up
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 * the requestors. */
5396 spin_lock_irq(&rq->lock);
5397 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005398 struct migration_req *req;
5399
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07005401 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 list_del_init(&req->list);
5403 complete(&req->done);
5404 }
5405 spin_unlock_irq(&rq->lock);
5406 break;
5407#endif
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005408 case CPU_LOCK_RELEASE:
5409 mutex_unlock(&sched_hotcpu_mutex);
5410 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 }
5412 return NOTIFY_OK;
5413}
5414
5415/* Register at highest priority so that task migration (migrate_all_tasks)
5416 * happens before everything else.
5417 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07005418static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 .notifier_call = migration_call,
5420 .priority = 10
5421};
5422
5423int __init migration_init(void)
5424{
5425 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07005426 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005427
5428 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07005429 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
5430 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 migration_call(&migration_notifier, CPU_ONLINE, cpu);
5432 register_cpu_notifier(&migration_notifier);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005433
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 return 0;
5435}
5436#endif
5437
5438#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07005439
5440/* Number of possible processor ids */
5441int nr_cpu_ids __read_mostly = NR_CPUS;
5442EXPORT_SYMBOL(nr_cpu_ids);
5443
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07005444#undef SCHED_DOMAIN_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445#ifdef SCHED_DOMAIN_DEBUG
5446static void sched_domain_debug(struct sched_domain *sd, int cpu)
5447{
5448 int level = 0;
5449
Nick Piggin41c7ce92005-06-25 14:57:24 -07005450 if (!sd) {
5451 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
5452 return;
5453 }
5454
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
5456
5457 do {
5458 int i;
5459 char str[NR_CPUS];
5460 struct sched_group *group = sd->groups;
5461 cpumask_t groupmask;
5462
5463 cpumask_scnprintf(str, NR_CPUS, sd->span);
5464 cpus_clear(groupmask);
5465
5466 printk(KERN_DEBUG);
5467 for (i = 0; i < level + 1; i++)
5468 printk(" ");
5469 printk("domain %d: ", level);
5470
5471 if (!(sd->flags & SD_LOAD_BALANCE)) {
5472 printk("does not load-balance\n");
5473 if (sd->parent)
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005474 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
5475 " has parent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 break;
5477 }
5478
5479 printk("span %s\n", str);
5480
5481 if (!cpu_isset(cpu, sd->span))
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005482 printk(KERN_ERR "ERROR: domain->span does not contain "
5483 "CPU%d\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 if (!cpu_isset(cpu, group->cpumask))
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005485 printk(KERN_ERR "ERROR: domain->groups does not contain"
5486 " CPU%d\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487
5488 printk(KERN_DEBUG);
5489 for (i = 0; i < level + 2; i++)
5490 printk(" ");
5491 printk("groups:");
5492 do {
5493 if (!group) {
5494 printk("\n");
5495 printk(KERN_ERR "ERROR: group is NULL\n");
5496 break;
5497 }
5498
Eric Dumazet5517d862007-05-08 00:32:57 -07005499 if (!group->__cpu_power) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 printk("\n");
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005501 printk(KERN_ERR "ERROR: domain->cpu_power not "
5502 "set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 }
5504
5505 if (!cpus_weight(group->cpumask)) {
5506 printk("\n");
5507 printk(KERN_ERR "ERROR: empty group\n");
5508 }
5509
5510 if (cpus_intersects(groupmask, group->cpumask)) {
5511 printk("\n");
5512 printk(KERN_ERR "ERROR: repeated CPUs\n");
5513 }
5514
5515 cpus_or(groupmask, groupmask, group->cpumask);
5516
5517 cpumask_scnprintf(str, NR_CPUS, group->cpumask);
5518 printk(" %s", str);
5519
5520 group = group->next;
5521 } while (group != sd->groups);
5522 printk("\n");
5523
5524 if (!cpus_equal(sd->span, groupmask))
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005525 printk(KERN_ERR "ERROR: groups don't span "
5526 "domain->span\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527
5528 level++;
5529 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005530 if (!sd)
5531 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005533 if (!cpus_subset(groupmask, sd->span))
5534 printk(KERN_ERR "ERROR: parent span is not a superset "
5535 "of domain->span\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536
5537 } while (sd);
5538}
5539#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07005540# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541#endif
5542
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07005543static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005544{
5545 if (cpus_weight(sd->span) == 1)
5546 return 1;
5547
5548 /* Following flags need at least 2 groups */
5549 if (sd->flags & (SD_LOAD_BALANCE |
5550 SD_BALANCE_NEWIDLE |
5551 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005552 SD_BALANCE_EXEC |
5553 SD_SHARE_CPUPOWER |
5554 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005555 if (sd->groups != sd->groups->next)
5556 return 0;
5557 }
5558
5559 /* Following flags don't use groups */
5560 if (sd->flags & (SD_WAKE_IDLE |
5561 SD_WAKE_AFFINE |
5562 SD_WAKE_BALANCE))
5563 return 0;
5564
5565 return 1;
5566}
5567
Ingo Molnar48f24c42006-07-03 00:25:40 -07005568static int
5569sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005570{
5571 unsigned long cflags = sd->flags, pflags = parent->flags;
5572
5573 if (sd_degenerate(parent))
5574 return 1;
5575
5576 if (!cpus_equal(sd->span, parent->span))
5577 return 0;
5578
5579 /* Does parent contain flags not in child? */
5580 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
5581 if (cflags & SD_WAKE_AFFINE)
5582 pflags &= ~SD_WAKE_BALANCE;
5583 /* Flags needing groups don't count if only 1 group in parent */
5584 if (parent->groups == parent->groups->next) {
5585 pflags &= ~(SD_LOAD_BALANCE |
5586 SD_BALANCE_NEWIDLE |
5587 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005588 SD_BALANCE_EXEC |
5589 SD_SHARE_CPUPOWER |
5590 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07005591 }
5592 if (~cflags & pflags)
5593 return 0;
5594
5595 return 1;
5596}
5597
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598/*
5599 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
5600 * hold the hotplug lock.
5601 */
John Hawkes9c1cfda2005-09-06 15:18:14 -07005602static void cpu_attach_domain(struct sched_domain *sd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005604 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07005605 struct sched_domain *tmp;
5606
5607 /* Remove the sched domains which do not contribute to scheduling. */
5608 for (tmp = sd; tmp; tmp = tmp->parent) {
5609 struct sched_domain *parent = tmp->parent;
5610 if (!parent)
5611 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005612 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005613 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005614 if (parent->parent)
5615 parent->parent->child = tmp;
5616 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07005617 }
5618
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005619 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005620 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005621 if (sd)
5622 sd->child = NULL;
5623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624
5625 sched_domain_debug(sd, cpu);
5626
Nick Piggin674311d2005-06-25 14:57:27 -07005627 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628}
5629
5630/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08005631static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632
5633/* Setup the mask of cpus configured for isolated domains */
5634static int __init isolated_cpu_setup(char *str)
5635{
5636 int ints[NR_CPUS], i;
5637
5638 str = get_options(str, ARRAY_SIZE(ints), ints);
5639 cpus_clear(cpu_isolated_map);
5640 for (i = 1; i <= ints[0]; i++)
5641 if (ints[i] < NR_CPUS)
5642 cpu_set(ints[i], cpu_isolated_map);
5643 return 1;
5644}
5645
5646__setup ("isolcpus=", isolated_cpu_setup);
5647
5648/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005649 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
5650 * to a function which identifies what group(along with sched group) a CPU
5651 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
5652 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 *
5654 * init_sched_build_groups will build a circular linked list of the groups
5655 * covered by the given span, and will set each group's ->cpumask correctly,
5656 * and ->cpu_power to 0.
5657 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005658static void
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005659init_sched_build_groups(cpumask_t span, const cpumask_t *cpu_map,
5660 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
5661 struct sched_group **sg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662{
5663 struct sched_group *first = NULL, *last = NULL;
5664 cpumask_t covered = CPU_MASK_NONE;
5665 int i;
5666
5667 for_each_cpu_mask(i, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005668 struct sched_group *sg;
5669 int group = group_fn(i, cpu_map, &sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 int j;
5671
5672 if (cpu_isset(i, covered))
5673 continue;
5674
5675 sg->cpumask = CPU_MASK_NONE;
Eric Dumazet5517d862007-05-08 00:32:57 -07005676 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677
5678 for_each_cpu_mask(j, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005679 if (group_fn(j, cpu_map, NULL) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 continue;
5681
5682 cpu_set(j, covered);
5683 cpu_set(j, sg->cpumask);
5684 }
5685 if (!first)
5686 first = sg;
5687 if (last)
5688 last->next = sg;
5689 last = sg;
5690 }
5691 last->next = first;
5692}
5693
John Hawkes9c1cfda2005-09-06 15:18:14 -07005694#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695
John Hawkes9c1cfda2005-09-06 15:18:14 -07005696#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08005697
John Hawkes9c1cfda2005-09-06 15:18:14 -07005698/**
5699 * find_next_best_node - find the next node to include in a sched_domain
5700 * @node: node whose sched_domain we're building
5701 * @used_nodes: nodes already in the sched_domain
5702 *
5703 * Find the next node to include in a given scheduling domain. Simply
5704 * finds the closest node not already in the @used_nodes map.
5705 *
5706 * Should use nodemask_t.
5707 */
5708static int find_next_best_node(int node, unsigned long *used_nodes)
5709{
5710 int i, n, val, min_val, best_node = 0;
5711
5712 min_val = INT_MAX;
5713
5714 for (i = 0; i < MAX_NUMNODES; i++) {
5715 /* Start at @node */
5716 n = (node + i) % MAX_NUMNODES;
5717
5718 if (!nr_cpus_node(n))
5719 continue;
5720
5721 /* Skip already used nodes */
5722 if (test_bit(n, used_nodes))
5723 continue;
5724
5725 /* Simple min distance search */
5726 val = node_distance(node, n);
5727
5728 if (val < min_val) {
5729 min_val = val;
5730 best_node = n;
5731 }
5732 }
5733
5734 set_bit(best_node, used_nodes);
5735 return best_node;
5736}
5737
5738/**
5739 * sched_domain_node_span - get a cpumask for a node's sched_domain
5740 * @node: node whose cpumask we're constructing
5741 * @size: number of nodes to include in this span
5742 *
5743 * Given a node, construct a good cpumask for its sched_domain to span. It
5744 * should be one that prevents unnecessary balancing, but also spreads tasks
5745 * out optimally.
5746 */
5747static cpumask_t sched_domain_node_span(int node)
5748{
John Hawkes9c1cfda2005-09-06 15:18:14 -07005749 DECLARE_BITMAP(used_nodes, MAX_NUMNODES);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005750 cpumask_t span, nodemask;
5751 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07005752
5753 cpus_clear(span);
5754 bitmap_zero(used_nodes, MAX_NUMNODES);
5755
5756 nodemask = node_to_cpumask(node);
5757 cpus_or(span, span, nodemask);
5758 set_bit(node, used_nodes);
5759
5760 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
5761 int next_node = find_next_best_node(node, used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005762
John Hawkes9c1cfda2005-09-06 15:18:14 -07005763 nodemask = node_to_cpumask(next_node);
5764 cpus_or(span, span, nodemask);
5765 }
5766
5767 return span;
5768}
5769#endif
5770
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07005771int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005772
John Hawkes9c1cfda2005-09-06 15:18:14 -07005773/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07005774 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07005775 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776#ifdef CONFIG_SCHED_SMT
5777static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005778static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005779
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005780static int cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map,
5781 struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005783 if (sg)
5784 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 return cpu;
5786}
5787#endif
5788
Ingo Molnar48f24c42006-07-03 00:25:40 -07005789/*
5790 * multi-core sched-domains:
5791 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005792#ifdef CONFIG_SCHED_MC
5793static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005794static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005795#endif
5796
5797#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005798static int cpu_to_core_group(int cpu, const cpumask_t *cpu_map,
5799 struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005800{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005801 int group;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005802 cpumask_t mask = cpu_sibling_map[cpu];
5803 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005804 group = first_cpu(mask);
5805 if (sg)
5806 *sg = &per_cpu(sched_group_core, group);
5807 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005808}
5809#elif defined(CONFIG_SCHED_MC)
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005810static int cpu_to_core_group(int cpu, const cpumask_t *cpu_map,
5811 struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005812{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005813 if (sg)
5814 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005815 return cpu;
5816}
5817#endif
5818
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005820static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005821
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005822static int cpu_to_phys_group(int cpu, const cpumask_t *cpu_map,
5823 struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005825 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005826#ifdef CONFIG_SCHED_MC
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005827 cpumask_t mask = cpu_coregroup_map(cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005828 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005829 group = first_cpu(mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005830#elif defined(CONFIG_SCHED_SMT)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005831 cpumask_t mask = cpu_sibling_map[cpu];
5832 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005833 group = first_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005835 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005837 if (sg)
5838 *sg = &per_cpu(sched_group_phys, group);
5839 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840}
5841
5842#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07005843/*
5844 * The init_sched_build_groups can't handle what we want to do with node
5845 * groups, so roll our own. Now each node has its own list of groups which
5846 * gets dynamically allocated.
5847 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848static DEFINE_PER_CPU(struct sched_domain, node_domains);
John Hawkesd1b55132005-09-06 15:18:14 -07005849static struct sched_group **sched_group_nodes_bycpu[NR_CPUS];
John Hawkes9c1cfda2005-09-06 15:18:14 -07005850
5851static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005852static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07005853
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005854static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
5855 struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005857 cpumask_t nodemask = node_to_cpumask(cpu_to_node(cpu));
5858 int group;
5859
5860 cpus_and(nodemask, nodemask, *cpu_map);
5861 group = first_cpu(nodemask);
5862
5863 if (sg)
5864 *sg = &per_cpu(sched_group_allnodes, group);
5865 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005867
Siddha, Suresh B08069032006-03-27 01:15:23 -08005868static void init_numa_sched_groups_power(struct sched_group *group_head)
5869{
5870 struct sched_group *sg = group_head;
5871 int j;
5872
5873 if (!sg)
5874 return;
5875next_sg:
5876 for_each_cpu_mask(j, sg->cpumask) {
5877 struct sched_domain *sd;
5878
5879 sd = &per_cpu(phys_domains, j);
5880 if (j != first_cpu(sd->groups->cpumask)) {
5881 /*
5882 * Only add "power" once for each
5883 * physical package.
5884 */
5885 continue;
5886 }
5887
Eric Dumazet5517d862007-05-08 00:32:57 -07005888 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08005889 }
5890 sg = sg->next;
5891 if (sg != group_head)
5892 goto next_sg;
5893}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894#endif
5895
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005896#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005897/* Free memory allocated for various sched_group structures */
5898static void free_sched_groups(const cpumask_t *cpu_map)
5899{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005900 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005901
5902 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005903 struct sched_group **sched_group_nodes
5904 = sched_group_nodes_bycpu[cpu];
5905
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005906 if (!sched_group_nodes)
5907 continue;
5908
5909 for (i = 0; i < MAX_NUMNODES; i++) {
5910 cpumask_t nodemask = node_to_cpumask(i);
5911 struct sched_group *oldsg, *sg = sched_group_nodes[i];
5912
5913 cpus_and(nodemask, nodemask, *cpu_map);
5914 if (cpus_empty(nodemask))
5915 continue;
5916
5917 if (sg == NULL)
5918 continue;
5919 sg = sg->next;
5920next_sg:
5921 oldsg = sg;
5922 sg = sg->next;
5923 kfree(oldsg);
5924 if (oldsg != sched_group_nodes[i])
5925 goto next_sg;
5926 }
5927 kfree(sched_group_nodes);
5928 sched_group_nodes_bycpu[cpu] = NULL;
5929 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005930}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005931#else
5932static void free_sched_groups(const cpumask_t *cpu_map)
5933{
5934}
5935#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005936
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005938 * Initialize sched groups cpu_power.
5939 *
5940 * cpu_power indicates the capacity of sched group, which is used while
5941 * distributing the load between different sched groups in a sched domain.
5942 * Typically cpu_power for all the groups in a sched domain will be same unless
5943 * there are asymmetries in the topology. If there are asymmetries, group
5944 * having more cpu_power will pickup more load compared to the group having
5945 * less cpu_power.
5946 *
5947 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
5948 * the maximum number of tasks a group can handle in the presence of other idle
5949 * or lightly loaded groups in the same sched domain.
5950 */
5951static void init_sched_groups_power(int cpu, struct sched_domain *sd)
5952{
5953 struct sched_domain *child;
5954 struct sched_group *group;
5955
5956 WARN_ON(!sd || !sd->groups);
5957
5958 if (cpu != first_cpu(sd->groups->cpumask))
5959 return;
5960
5961 child = sd->child;
5962
Eric Dumazet5517d862007-05-08 00:32:57 -07005963 sd->groups->__cpu_power = 0;
5964
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005965 /*
5966 * For perf policy, if the groups in child domain share resources
5967 * (for example cores sharing some portions of the cache hierarchy
5968 * or SMT), then set this domain groups cpu_power such that each group
5969 * can handle only one task, when there are other idle groups in the
5970 * same sched domain.
5971 */
5972 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
5973 (child->flags &
5974 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07005975 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005976 return;
5977 }
5978
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005979 /*
5980 * add cpu_power of each child group to this groups cpu_power
5981 */
5982 group = child->groups;
5983 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07005984 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005985 group = group->next;
5986 } while (group != child->groups);
5987}
5988
5989/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07005990 * Build sched domains for a given set of cpus and attach the sched domains
5991 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005993static int build_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994{
5995 int i;
John Hawkesd1b55132005-09-06 15:18:14 -07005996#ifdef CONFIG_NUMA
5997 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005998 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07005999
6000 /*
6001 * Allocate the per-node list of sched groups
6002 */
Ingo Molnardd41f592007-07-09 18:51:59 +02006003 sched_group_nodes = kzalloc(sizeof(struct sched_group *)*MAX_NUMNODES,
Srivatsa Vaddagirid3a5aa92006-06-27 02:54:39 -07006004 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07006005 if (!sched_group_nodes) {
6006 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006007 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07006008 }
6009 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
6010#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011
6012 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006013 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006015 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 struct sched_domain *sd = NULL, *p;
6017 cpumask_t nodemask = node_to_cpumask(cpu_to_node(i));
6018
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006019 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020
6021#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02006022 if (cpus_weight(*cpu_map) >
6023 SD_NODES_PER_DOMAIN*cpus_weight(nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07006024 sd = &per_cpu(allnodes_domains, i);
6025 *sd = SD_ALLNODES_INIT;
6026 sd->span = *cpu_map;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006027 cpu_to_allnodes_group(i, cpu_map, &sd->groups);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006028 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006029 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006030 } else
6031 p = NULL;
6032
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 sd = &per_cpu(node_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 *sd = SD_NODE_INIT;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006035 sd->span = sched_domain_node_span(cpu_to_node(i));
6036 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006037 if (p)
6038 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006039 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040#endif
6041
6042 p = sd;
6043 sd = &per_cpu(phys_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 *sd = SD_CPU_INIT;
6045 sd->span = nodemask;
6046 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006047 if (p)
6048 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006049 cpu_to_phys_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006051#ifdef CONFIG_SCHED_MC
6052 p = sd;
6053 sd = &per_cpu(core_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006054 *sd = SD_MC_INIT;
6055 sd->span = cpu_coregroup_map(i);
6056 cpus_and(sd->span, sd->span, *cpu_map);
6057 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006058 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006059 cpu_to_core_group(i, cpu_map, &sd->groups);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006060#endif
6061
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062#ifdef CONFIG_SCHED_SMT
6063 p = sd;
6064 sd = &per_cpu(cpu_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 *sd = SD_SIBLING_INIT;
6066 sd->span = cpu_sibling_map[i];
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006067 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006069 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006070 cpu_to_cpu_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071#endif
6072 }
6073
6074#ifdef CONFIG_SCHED_SMT
6075 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07006076 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077 cpumask_t this_sibling_map = cpu_sibling_map[i];
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006078 cpus_and(this_sibling_map, this_sibling_map, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 if (i != first_cpu(this_sibling_map))
6080 continue;
6081
Ingo Molnardd41f592007-07-09 18:51:59 +02006082 init_sched_build_groups(this_sibling_map, cpu_map,
6083 &cpu_to_cpu_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 }
6085#endif
6086
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006087#ifdef CONFIG_SCHED_MC
6088 /* Set up multi-core groups */
6089 for_each_cpu_mask(i, *cpu_map) {
6090 cpumask_t this_core_map = cpu_coregroup_map(i);
6091 cpus_and(this_core_map, this_core_map, *cpu_map);
6092 if (i != first_cpu(this_core_map))
6093 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02006094 init_sched_build_groups(this_core_map, cpu_map,
6095 &cpu_to_core_group);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006096 }
6097#endif
6098
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 /* Set up physical groups */
6100 for (i = 0; i < MAX_NUMNODES; i++) {
6101 cpumask_t nodemask = node_to_cpumask(i);
6102
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006103 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 if (cpus_empty(nodemask))
6105 continue;
6106
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006107 init_sched_build_groups(nodemask, cpu_map, &cpu_to_phys_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 }
6109
6110#ifdef CONFIG_NUMA
6111 /* Set up node groups */
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006112 if (sd_allnodes)
Ingo Molnardd41f592007-07-09 18:51:59 +02006113 init_sched_build_groups(*cpu_map, cpu_map,
6114 &cpu_to_allnodes_group);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006115
6116 for (i = 0; i < MAX_NUMNODES; i++) {
6117 /* Set up node groups */
6118 struct sched_group *sg, *prev;
6119 cpumask_t nodemask = node_to_cpumask(i);
6120 cpumask_t domainspan;
6121 cpumask_t covered = CPU_MASK_NONE;
6122 int j;
6123
6124 cpus_and(nodemask, nodemask, *cpu_map);
John Hawkesd1b55132005-09-06 15:18:14 -07006125 if (cpus_empty(nodemask)) {
6126 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006127 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07006128 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006129
6130 domainspan = sched_domain_node_span(i);
6131 cpus_and(domainspan, domainspan, *cpu_map);
6132
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006133 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006134 if (!sg) {
6135 printk(KERN_WARNING "Can not alloc domain group for "
6136 "node %d\n", i);
6137 goto error;
6138 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006139 sched_group_nodes[i] = sg;
6140 for_each_cpu_mask(j, nodemask) {
6141 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02006142
John Hawkes9c1cfda2005-09-06 15:18:14 -07006143 sd = &per_cpu(node_domains, j);
6144 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006145 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006146 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006147 sg->cpumask = nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006148 sg->next = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006149 cpus_or(covered, covered, nodemask);
6150 prev = sg;
6151
6152 for (j = 0; j < MAX_NUMNODES; j++) {
6153 cpumask_t tmp, notcovered;
6154 int n = (i + j) % MAX_NUMNODES;
6155
6156 cpus_complement(notcovered, covered);
6157 cpus_and(tmp, notcovered, *cpu_map);
6158 cpus_and(tmp, tmp, domainspan);
6159 if (cpus_empty(tmp))
6160 break;
6161
6162 nodemask = node_to_cpumask(n);
6163 cpus_and(tmp, tmp, nodemask);
6164 if (cpus_empty(tmp))
6165 continue;
6166
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006167 sg = kmalloc_node(sizeof(struct sched_group),
6168 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006169 if (!sg) {
6170 printk(KERN_WARNING
6171 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006172 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006173 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006174 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006175 sg->cpumask = tmp;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006176 sg->next = prev->next;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006177 cpus_or(covered, covered, tmp);
6178 prev->next = sg;
6179 prev = sg;
6180 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182#endif
6183
6184 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006185#ifdef CONFIG_SCHED_SMT
6186 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006187 struct sched_domain *sd = &per_cpu(cpu_domains, i);
6188
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006189 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006190 }
6191#endif
6192#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006193 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006194 struct sched_domain *sd = &per_cpu(core_domains, i);
6195
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006196 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006197 }
6198#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006200 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006201 struct sched_domain *sd = &per_cpu(phys_domains, i);
6202
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006203 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 }
6205
John Hawkes9c1cfda2005-09-06 15:18:14 -07006206#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08006207 for (i = 0; i < MAX_NUMNODES; i++)
6208 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006209
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006210 if (sd_allnodes) {
6211 struct sched_group *sg;
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07006212
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006213 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg);
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07006214 init_numa_sched_groups_power(sg);
6215 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006216#endif
6217
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006219 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 struct sched_domain *sd;
6221#ifdef CONFIG_SCHED_SMT
6222 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006223#elif defined(CONFIG_SCHED_MC)
6224 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225#else
6226 sd = &per_cpu(phys_domains, i);
6227#endif
6228 cpu_attach_domain(sd, i);
6229 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006230
6231 return 0;
6232
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006233#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006234error:
6235 free_sched_groups(cpu_map);
6236 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006237#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238}
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006239/*
6240 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
6241 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006242static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006243{
6244 cpumask_t cpu_default_map;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006245 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006247 /*
6248 * Setup mask for cpus without special case scheduling requirements.
6249 * For now this just excludes isolated cpus, but could be used to
6250 * exclude other special cases in the future.
6251 */
6252 cpus_andnot(cpu_default_map, *cpu_map, cpu_isolated_map);
6253
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006254 err = build_sched_domains(&cpu_default_map);
6255
6256 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006257}
6258
6259static void arch_destroy_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260{
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006261 free_sched_groups(cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006262}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006264/*
6265 * Detach sched domains from a group of cpus specified in cpu_map
6266 * These cpus will now be attached to the NULL domain
6267 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08006268static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006269{
6270 int i;
6271
6272 for_each_cpu_mask(i, *cpu_map)
6273 cpu_attach_domain(NULL, i);
6274 synchronize_sched();
6275 arch_destroy_sched_domains(cpu_map);
6276}
6277
6278/*
6279 * Partition sched domains as specified by the cpumasks below.
6280 * This attaches all cpus from the cpumasks to the NULL domain,
6281 * waits for a RCU quiescent period, recalculates sched
6282 * domain information and then attaches them back to the
6283 * correct sched domains
6284 * Call with hotplug lock held
6285 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006286int partition_sched_domains(cpumask_t *partition1, cpumask_t *partition2)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006287{
6288 cpumask_t change_map;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006289 int err = 0;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006290
6291 cpus_and(*partition1, *partition1, cpu_online_map);
6292 cpus_and(*partition2, *partition2, cpu_online_map);
6293 cpus_or(change_map, *partition1, *partition2);
6294
6295 /* Detach sched domains from all of the affected cpus */
6296 detach_destroy_domains(&change_map);
6297 if (!cpus_empty(*partition1))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006298 err = build_sched_domains(partition1);
6299 if (!err && !cpus_empty(*partition2))
6300 err = build_sched_domains(partition2);
6301
6302 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006303}
6304
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006305#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Adrian Bunk6707de002007-08-12 18:08:19 +02006306static int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006307{
6308 int err;
6309
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006310 mutex_lock(&sched_hotcpu_mutex);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006311 detach_destroy_domains(&cpu_online_map);
6312 err = arch_init_sched_domains(&cpu_online_map);
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006313 mutex_unlock(&sched_hotcpu_mutex);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006314
6315 return err;
6316}
6317
6318static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
6319{
6320 int ret;
6321
6322 if (buf[0] != '0' && buf[0] != '1')
6323 return -EINVAL;
6324
6325 if (smt)
6326 sched_smt_power_savings = (buf[0] == '1');
6327 else
6328 sched_mc_power_savings = (buf[0] == '1');
6329
6330 ret = arch_reinit_sched_domains();
6331
6332 return ret ? ret : count;
6333}
6334
Adrian Bunk6707de002007-08-12 18:08:19 +02006335#ifdef CONFIG_SCHED_MC
6336static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
6337{
6338 return sprintf(page, "%u\n", sched_mc_power_savings);
6339}
6340static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
6341 const char *buf, size_t count)
6342{
6343 return sched_power_savings_store(buf, count, 0);
6344}
6345static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
6346 sched_mc_power_savings_store);
6347#endif
6348
6349#ifdef CONFIG_SCHED_SMT
6350static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
6351{
6352 return sprintf(page, "%u\n", sched_smt_power_savings);
6353}
6354static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
6355 const char *buf, size_t count)
6356{
6357 return sched_power_savings_store(buf, count, 1);
6358}
6359static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
6360 sched_smt_power_savings_store);
6361#endif
6362
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006363int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
6364{
6365 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006366
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006367#ifdef CONFIG_SCHED_SMT
6368 if (smt_capable())
6369 err = sysfs_create_file(&cls->kset.kobj,
6370 &attr_sched_smt_power_savings.attr);
6371#endif
6372#ifdef CONFIG_SCHED_MC
6373 if (!err && mc_capable())
6374 err = sysfs_create_file(&cls->kset.kobj,
6375 &attr_sched_mc_power_savings.attr);
6376#endif
6377 return err;
6378}
6379#endif
6380
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381/*
6382 * Force a reinitialization of the sched domains hierarchy. The domains
6383 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07006384 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 * which will prevent rebalancing while the sched domains are recalculated.
6386 */
6387static int update_sched_domains(struct notifier_block *nfb,
6388 unsigned long action, void *hcpu)
6389{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390 switch (action) {
6391 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006392 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006394 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006395 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396 return NOTIFY_OK;
6397
6398 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006399 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006401 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006403 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006405 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 /*
6407 * Fall through and re-initialise the domains.
6408 */
6409 break;
6410 default:
6411 return NOTIFY_DONE;
6412 }
6413
6414 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006415 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416
6417 return NOTIFY_OK;
6418}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419
6420void __init sched_init_smp(void)
6421{
Nick Piggin5c1e1762006-10-03 01:14:04 -07006422 cpumask_t non_isolated_cpus;
6423
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006424 mutex_lock(&sched_hotcpu_mutex);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006425 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08006426 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07006427 if (cpus_empty(non_isolated_cpus))
6428 cpu_set(smp_processor_id(), non_isolated_cpus);
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006429 mutex_unlock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430 /* XXX: Theoretical race here - CPU may be hotplugged now */
6431 hotcpu_notifier(update_sched_domains, 0);
Nick Piggin5c1e1762006-10-03 01:14:04 -07006432
Nick Piggine692ab52007-07-26 13:40:43 +02006433 init_sched_domain_sysctl();
6434
Nick Piggin5c1e1762006-10-03 01:14:04 -07006435 /* Move init over to a non-isolated CPU */
6436 if (set_cpus_allowed(current, non_isolated_cpus) < 0)
6437 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438}
6439#else
6440void __init sched_init_smp(void)
6441{
6442}
6443#endif /* CONFIG_SMP */
6444
6445int in_sched_functions(unsigned long addr)
6446{
6447 /* Linker adds these: start and end of __sched functions */
6448 extern char __sched_text_start[], __sched_text_end[];
Ingo Molnar48f24c42006-07-03 00:25:40 -07006449
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 return in_lock_functions(addr) ||
6451 (addr >= (unsigned long)__sched_text_start
6452 && addr < (unsigned long)__sched_text_end);
6453}
6454
Ingo Molnardd41f592007-07-09 18:51:59 +02006455static inline void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
6456{
6457 cfs_rq->tasks_timeline = RB_ROOT;
6458 cfs_rq->fair_clock = 1;
6459#ifdef CONFIG_FAIR_GROUP_SCHED
6460 cfs_rq->rq = rq;
6461#endif
6462}
6463
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464void __init sched_init(void)
6465{
Christoph Lameter476f3532007-05-06 14:48:58 -07006466 int highest_cpu = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02006467 int i, j;
6468
6469 /*
6470 * Link up the scheduling class hierarchy:
6471 */
6472 rt_sched_class.next = &fair_sched_class;
6473 fair_sched_class.next = &idle_sched_class;
6474 idle_sched_class.next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08006476 for_each_possible_cpu(i) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006477 struct rt_prio_array *array;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006478 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479
6480 rq = cpu_rq(i);
6481 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07006482 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07006483 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02006484 rq->clock = 1;
6485 init_cfs_rq(&rq->cfs, rq);
6486#ifdef CONFIG_FAIR_GROUP_SCHED
6487 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
6488 list_add(&rq->cfs.leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
6489#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490
Ingo Molnardd41f592007-07-09 18:51:59 +02006491 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
6492 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07006494 rq->sd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02006496 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07006498 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499 rq->migration_thread = NULL;
6500 INIT_LIST_HEAD(&rq->migration_queue);
6501#endif
6502 atomic_set(&rq->nr_iowait, 0);
6503
Ingo Molnardd41f592007-07-09 18:51:59 +02006504 array = &rq->rt.active;
6505 for (j = 0; j < MAX_RT_PRIO; j++) {
6506 INIT_LIST_HEAD(array->queue + j);
6507 __clear_bit(j, array->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508 }
Christoph Lameter476f3532007-05-06 14:48:58 -07006509 highest_cpu = i;
Ingo Molnardd41f592007-07-09 18:51:59 +02006510 /* delimiter for bitsearch: */
6511 __set_bit(MAX_RT_PRIO, array->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512 }
6513
Peter Williams2dd73a42006-06-27 02:54:34 -07006514 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07006515
Avi Kivitye107be32007-07-26 13:40:43 +02006516#ifdef CONFIG_PREEMPT_NOTIFIERS
6517 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
6518#endif
6519
Christoph Lameterc9819f42006-12-10 02:20:25 -08006520#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006521 nr_cpu_ids = highest_cpu + 1;
Christoph Lameterc9819f42006-12-10 02:20:25 -08006522 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
6523#endif
6524
Heiko Carstensb50f60c2006-07-30 03:03:52 -07006525#ifdef CONFIG_RT_MUTEXES
6526 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
6527#endif
6528
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529 /*
6530 * The boot idle thread does lazy MMU switching as well:
6531 */
6532 atomic_inc(&init_mm.mm_count);
6533 enter_lazy_tlb(&init_mm, current);
6534
6535 /*
6536 * Make us the idle thread. Technically, schedule() should not be
6537 * called from this thread, however somewhere below it might be,
6538 * but because we are the idle thread, we just pick up running again
6539 * when this runqueue becomes "idle".
6540 */
6541 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02006542 /*
6543 * During early bootup we pretend to be a normal task:
6544 */
6545 current->sched_class = &fair_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546}
6547
6548#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
6549void __might_sleep(char *file, int line)
6550{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006551#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552 static unsigned long prev_jiffy; /* ratelimiting */
6553
6554 if ((in_atomic() || irqs_disabled()) &&
6555 system_state == SYSTEM_RUNNING && !oops_in_progress) {
6556 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
6557 return;
6558 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08006559 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 " context at %s:%d\n", file, line);
6561 printk("in_atomic():%d, irqs_disabled():%d\n",
6562 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08006563 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08006564 if (irqs_disabled())
6565 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566 dump_stack();
6567 }
6568#endif
6569}
6570EXPORT_SYMBOL(__might_sleep);
6571#endif
6572
6573#ifdef CONFIG_MAGIC_SYSRQ
6574void normalize_rt_tasks(void)
6575{
Ingo Molnara0f98a12007-06-17 18:37:45 +02006576 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006578 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02006579 int on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580
6581 read_lock_irq(&tasklist_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02006582 do_each_thread(g, p) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006583 p->se.fair_key = 0;
6584 p->se.wait_runtime = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02006585 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02006586 p->se.wait_start_fair = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02006587 p->se.sleep_start_fair = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02006588#ifdef CONFIG_SCHEDSTATS
6589 p->se.wait_start = 0;
6590 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02006591 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02006592#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02006593 task_rq(p)->cfs.fair_clock = 0;
6594 task_rq(p)->clock = 0;
6595
6596 if (!rt_task(p)) {
6597 /*
6598 * Renice negative nice level userspace
6599 * tasks back to 0:
6600 */
6601 if (TASK_NICE(p) < 0 && p->mm)
6602 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02006604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605
Ingo Molnarb29739f2006-06-27 02:54:51 -07006606 spin_lock_irqsave(&p->pi_lock, flags);
6607 rq = __task_rq_lock(p);
Ingo Molnardd41f592007-07-09 18:51:59 +02006608#ifdef CONFIG_SMP
6609 /*
6610 * Do not touch the migration thread:
6611 */
6612 if (p == rq->migration_thread)
6613 goto out_unlock;
6614#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615
Ingo Molnar2daa3572007-08-09 11:16:51 +02006616 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02006617 on_rq = p->se.on_rq;
Ingo Molnar2daa3572007-08-09 11:16:51 +02006618 if (on_rq)
6619 deactivate_task(rq, p, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02006620 __setscheduler(rq, p, SCHED_NORMAL, 0);
6621 if (on_rq) {
Ingo Molnar2daa3572007-08-09 11:16:51 +02006622 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623 resched_task(rq->curr);
6624 }
Ingo Molnardd41f592007-07-09 18:51:59 +02006625#ifdef CONFIG_SMP
6626 out_unlock:
6627#endif
Ingo Molnarb29739f2006-06-27 02:54:51 -07006628 __task_rq_unlock(rq);
6629 spin_unlock_irqrestore(&p->pi_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02006630 } while_each_thread(g, p);
6631
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632 read_unlock_irq(&tasklist_lock);
6633}
6634
6635#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07006636
6637#ifdef CONFIG_IA64
6638/*
6639 * These functions are only useful for the IA64 MCA handling.
6640 *
6641 * They can only be called when the whole system has been
6642 * stopped - every CPU needs to be quiescent, and no scheduling
6643 * activity can take place. Using them for anything else would
6644 * be a serious bug, and as a result, they aren't even visible
6645 * under any other configuration.
6646 */
6647
6648/**
6649 * curr_task - return the current task for a given cpu.
6650 * @cpu: the processor in question.
6651 *
6652 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
6653 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006654struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07006655{
6656 return cpu_curr(cpu);
6657}
6658
6659/**
6660 * set_curr_task - set the current task for a given cpu.
6661 * @cpu: the processor in question.
6662 * @p: the task pointer to set.
6663 *
6664 * Description: This function must only be used when non-maskable interrupts
6665 * are serviced on a separate stack. It allows the architecture to switch the
6666 * notion of the current task on a cpu in a non-blocking manner. This function
6667 * must be called with all CPU's synchronized, and interrupts disabled, the
6668 * and caller must save the original value of the current task (see
6669 * curr_task() above) and restore that value before reenabling interrupts and
6670 * re-starting the system.
6671 *
6672 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
6673 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006674void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07006675{
6676 cpu_curr(cpu) = p;
6677}
6678
6679#endif