blob: d67345175179cd512e21447e97023b1d6c31cebb [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Eric Dumazet5517d862007-05-08 00:32:57 -070065#include <asm/tlb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/*
Alexey Dobriyanb035b6d2007-02-10 01:45:10 -080068 * Scheduler clock - returns current time in nanosec units.
69 * This is default implementation.
70 * Architectures and sub-architectures can override this.
71 */
72unsigned long long __attribute__((weak)) sched_clock(void)
73{
74 return (unsigned long long)jiffies * (1000000000 / HZ);
75}
76
77/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * Convert user-nice values [ -20 ... 0 ... 19 ]
79 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
80 * and back.
81 */
82#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
83#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
84#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
85
86/*
87 * 'User priority' is the nice value converted to something we
88 * can work with better when scaling various scheduler parameters,
89 * it's a [ 0 ... 39 ] range.
90 */
91#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
92#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
93#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
94
95/*
96 * Some helpers for converting nanosecond timing to jiffy resolution
97 */
98#define NS_TO_JIFFIES(TIME) ((TIME) / (1000000000 / HZ))
99#define JIFFIES_TO_NS(TIME) ((TIME) * (1000000000 / HZ))
100
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200101#define NICE_0_LOAD SCHED_LOAD_SCALE
102#define NICE_0_SHIFT SCHED_LOAD_SHIFT
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/*
105 * These are the 'tuning knobs' of the scheduler:
106 *
107 * Minimum timeslice is 5 msecs (or 1 jiffy, whichever is larger),
108 * default timeslice is 100 msecs, maximum timeslice is 800 msecs.
109 * Timeslices get refilled after they expire.
110 */
111#define MIN_TIMESLICE max(5 * HZ / 1000, 1)
112#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700113
Eric Dumazet5517d862007-05-08 00:32:57 -0700114#ifdef CONFIG_SMP
115/*
116 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
117 * Since cpu_power is a 'constant', we can use a reciprocal divide.
118 */
119static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
120{
121 return reciprocal_divide(load, sg->reciprocal_cpu_power);
122}
123
124/*
125 * Each time a sched group cpu_power is changed,
126 * we must compute its reciprocal value
127 */
128static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
129{
130 sg->__cpu_power += val;
131 sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
132}
133#endif
134
Ingo Molnar634fa8c2007-07-09 18:52:00 +0200135#define SCALE_PRIO(x, prio) \
136 max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO / 2), MIN_TIMESLICE)
Borislav Petkov91fcdd42006-10-19 23:28:29 -0700137
Ingo Molnar634fa8c2007-07-09 18:52:00 +0200138/*
139 * static_prio_timeslice() scales user-nice values [ -20 ... 0 ... 19 ]
140 * to time slice values: [800ms ... 100ms ... 5ms]
141 */
142static unsigned int static_prio_timeslice(int static_prio)
Peter Williams2dd73a42006-06-27 02:54:34 -0700143{
Ingo Molnar634fa8c2007-07-09 18:52:00 +0200144 if (static_prio == NICE_TO_PRIO(19))
145 return 1;
146
147 if (static_prio < NICE_TO_PRIO(0))
148 return SCALE_PRIO(DEF_TIMESLICE * 4, static_prio);
149 else
150 return SCALE_PRIO(DEF_TIMESLICE, static_prio);
Peter Williams2dd73a42006-06-27 02:54:34 -0700151}
152
Ingo Molnare05606d2007-07-09 18:51:59 +0200153static inline int rt_policy(int policy)
154{
155 if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR))
156 return 1;
157 return 0;
158}
159
160static inline int task_has_rt_policy(struct task_struct *p)
161{
162 return rt_policy(p->policy);
163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200166 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200168struct rt_prio_array {
169 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
170 struct list_head queue[MAX_RT_PRIO];
171};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200173struct load_stat {
174 struct load_weight load;
175 u64 load_update_start, load_update_last;
176 unsigned long delta_fair, delta_exec, delta_stat;
177};
178
179/* CFS-related fields in a runqueue */
180struct cfs_rq {
181 struct load_weight load;
182 unsigned long nr_running;
183
184 s64 fair_clock;
185 u64 exec_clock;
186 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;
193#ifdef CONFIG_FAIR_GROUP_SCHED
194 /* 'curr' points to currently running entity on this cfs_rq.
195 * It is set to NULL otherwise (i.e when none are currently running).
196 */
197 struct sched_entity *curr;
198 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;
265 unsigned int clock_unstable_events;
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 atomic_t nr_iowait;
268
269#ifdef CONFIG_SMP
270 struct sched_domain *sd;
271
272 /* For active balancing */
273 int active_balance;
274 int push_cpu;
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700275 int cpu; /* cpu of this runqueue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Ingo Molnar36c8b582006-07-03 00:25:41 -0700277 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct list_head migration_queue;
279#endif
280
281#ifdef CONFIG_SCHEDSTATS
282 /* latency stats */
283 struct sched_info rq_sched_info;
284
285 /* sys_sched_yield() stats */
286 unsigned long yld_exp_empty;
287 unsigned long yld_act_empty;
288 unsigned long yld_both_empty;
289 unsigned long yld_cnt;
290
291 /* schedule() stats */
292 unsigned long sched_switch;
293 unsigned long sched_cnt;
294 unsigned long sched_goidle;
295
296 /* try_to_wake_up() stats */
297 unsigned long ttwu_cnt;
298 unsigned long ttwu_local;
299#endif
Ingo Molnarfcb99372006-07-03 00:25:10 -0700300 struct lock_class_key rq_lock_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301};
302
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700303static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Gautham R Shenoy5be93612007-05-09 02:34:04 -0700304static DEFINE_MUTEX(sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Ingo Molnardd41f592007-07-09 18:51:59 +0200306static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
307{
308 rq->curr->sched_class->check_preempt_curr(rq, p);
309}
310
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700311static inline int cpu_of(struct rq *rq)
312{
313#ifdef CONFIG_SMP
314 return rq->cpu;
315#else
316 return 0;
317#endif
318}
319
Nick Piggin674311d2005-06-25 14:57:27 -0700320/*
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200321 * Update the per-runqueue clock, as finegrained as the platform can give
322 * us, but without assuming monotonicity, etc.:
Ingo Molnar20d315d2007-07-09 18:51:58 +0200323 */
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200324static void __update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200325{
326 u64 prev_raw = rq->prev_clock_raw;
327 u64 now = sched_clock();
328 s64 delta = now - prev_raw;
329 u64 clock = rq->clock;
330
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200331#ifdef CONFIG_SCHED_DEBUG
332 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
333#endif
Ingo Molnar20d315d2007-07-09 18:51:58 +0200334 /*
335 * Protect against sched_clock() occasionally going backwards:
336 */
337 if (unlikely(delta < 0)) {
338 clock++;
339 rq->clock_warps++;
340 } else {
341 /*
342 * Catch too large forward jumps too:
343 */
344 if (unlikely(delta > 2*TICK_NSEC)) {
345 clock++;
346 rq->clock_overflows++;
347 } else {
348 if (unlikely(delta > rq->clock_max_delta))
349 rq->clock_max_delta = delta;
350 clock += delta;
351 }
352 }
353
354 rq->prev_clock_raw = now;
355 rq->clock = clock;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200356}
357
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200358static void update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200359{
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200360 if (likely(smp_processor_id() == cpu_of(rq)))
361 __update_rq_clock(rq);
362}
Ingo Molnar20d315d2007-07-09 18:51:58 +0200363
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200364static u64 __rq_clock(struct rq *rq)
365{
366 __update_rq_clock(rq);
Ingo Molnar20d315d2007-07-09 18:51:58 +0200367
368 return rq->clock;
369}
370
371/*
Nick Piggin674311d2005-06-25 14:57:27 -0700372 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700373 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700374 *
375 * The domain tree of any CPU may only be accessed from within
376 * preempt-disabled sections.
377 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700378#define for_each_domain(cpu, __sd) \
379 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
382#define this_rq() (&__get_cpu_var(runqueues))
383#define task_rq(p) cpu_rq(task_cpu(p))
384#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
385
Ingo Molnare436d802007-07-19 21:28:35 +0200386/*
387 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
388 * clock constructed from sched_clock():
389 */
390unsigned long long cpu_clock(int cpu)
391{
Ingo Molnare436d802007-07-19 21:28:35 +0200392 unsigned long long now;
393 unsigned long flags;
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200394 struct rq *rq;
Ingo Molnare436d802007-07-19 21:28:35 +0200395
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200396 local_irq_save(flags);
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200397 rq = cpu_rq(cpu);
398 update_rq_clock(rq);
399 now = rq->clock;
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200400 local_irq_restore(flags);
Ingo Molnare436d802007-07-19 21:28:35 +0200401
402 return now;
403}
404
Ingo Molnar138a8ae2007-07-09 18:51:58 +0200405#ifdef CONFIG_FAIR_GROUP_SCHED
406/* Change a task's ->cfs_rq if it moves across CPUs */
407static inline void set_task_cfs_rq(struct task_struct *p)
408{
409 p->se.cfs_rq = &task_rq(p)->cfs;
410}
411#else
412static inline void set_task_cfs_rq(struct task_struct *p)
413{
414}
415#endif
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700418# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700420#ifndef finish_arch_switch
421# define finish_arch_switch(prev) do { } while (0)
422#endif
423
424#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700425static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700426{
427 return rq->curr == p;
428}
429
Ingo Molnar70b97a72006-07-03 00:25:42 -0700430static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700431{
432}
433
Ingo Molnar70b97a72006-07-03 00:25:42 -0700434static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700435{
Ingo Molnarda04c032005-09-13 11:17:59 +0200436#ifdef CONFIG_DEBUG_SPINLOCK
437 /* this is a valid case when another task releases the spinlock */
438 rq->lock.owner = current;
439#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700440 /*
441 * If we are tracking spinlock dependencies then we have to
442 * fix up the runqueue lock - which gets 'carried over' from
443 * prev into current:
444 */
445 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
446
Nick Piggin4866cde2005-06-25 14:57:23 -0700447 spin_unlock_irq(&rq->lock);
448}
449
450#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700451static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700452{
453#ifdef CONFIG_SMP
454 return p->oncpu;
455#else
456 return rq->curr == p;
457#endif
458}
459
Ingo Molnar70b97a72006-07-03 00:25:42 -0700460static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700461{
462#ifdef CONFIG_SMP
463 /*
464 * We can optimise this out completely for !SMP, because the
465 * SMP rebalancing from interrupt is the only thing that cares
466 * here.
467 */
468 next->oncpu = 1;
469#endif
470#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
471 spin_unlock_irq(&rq->lock);
472#else
473 spin_unlock(&rq->lock);
474#endif
475}
476
Ingo Molnar70b97a72006-07-03 00:25:42 -0700477static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700478{
479#ifdef CONFIG_SMP
480 /*
481 * After ->oncpu is cleared, the task can be moved to a different CPU.
482 * We must ensure this doesn't happen until the switch is completely
483 * finished.
484 */
485 smp_wmb();
486 prev->oncpu = 0;
487#endif
488#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
489 local_irq_enable();
490#endif
491}
492#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700495 * __task_rq_lock - lock the runqueue a given task resides on.
496 * Must be called interrupts disabled.
497 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700498static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700499 __acquires(rq->lock)
500{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700501 struct rq *rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700502
503repeat_lock_task:
504 rq = task_rq(p);
505 spin_lock(&rq->lock);
506 if (unlikely(rq != task_rq(p))) {
507 spin_unlock(&rq->lock);
508 goto repeat_lock_task;
509 }
510 return rq;
511}
512
513/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 * task_rq_lock - lock the runqueue a given task resides on and disable
515 * interrupts. Note the ordering: we can safely lookup the task_rq without
516 * explicitly disabling preemption.
517 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700518static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 __acquires(rq->lock)
520{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700521 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523repeat_lock_task:
524 local_irq_save(*flags);
525 rq = task_rq(p);
526 spin_lock(&rq->lock);
527 if (unlikely(rq != task_rq(p))) {
528 spin_unlock_irqrestore(&rq->lock, *flags);
529 goto repeat_lock_task;
530 }
531 return rq;
532}
533
Ingo Molnar70b97a72006-07-03 00:25:42 -0700534static inline void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700535 __releases(rq->lock)
536{
537 spin_unlock(&rq->lock);
538}
539
Ingo Molnar70b97a72006-07-03 00:25:42 -0700540static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 __releases(rq->lock)
542{
543 spin_unlock_irqrestore(&rq->lock, *flags);
544}
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800547 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700549static inline struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 __acquires(rq->lock)
551{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700552 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 local_irq_disable();
555 rq = this_rq();
556 spin_lock(&rq->lock);
557
558 return rq;
559}
560
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200561/*
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200562 * CPU frequency is/was unstable - start new by setting prev_clock_raw:
563 */
564void sched_clock_unstable_event(void)
565{
566 unsigned long flags;
567 struct rq *rq;
568
569 rq = task_rq_lock(current, &flags);
570 rq->prev_clock_raw = sched_clock();
571 rq->clock_unstable_events++;
572 task_rq_unlock(rq, &flags);
573}
574
575/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200576 * resched_task - mark a task 'to be rescheduled now'.
577 *
578 * On UP this means the setting of the need_resched flag, on SMP it
579 * might also involve a cross-CPU call to trigger the scheduler on
580 * the target CPU.
581 */
582#ifdef CONFIG_SMP
583
584#ifndef tsk_is_polling
585#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
586#endif
587
588static void resched_task(struct task_struct *p)
589{
590 int cpu;
591
592 assert_spin_locked(&task_rq(p)->lock);
593
594 if (unlikely(test_tsk_thread_flag(p, TIF_NEED_RESCHED)))
595 return;
596
597 set_tsk_thread_flag(p, TIF_NEED_RESCHED);
598
599 cpu = task_cpu(p);
600 if (cpu == smp_processor_id())
601 return;
602
603 /* NEED_RESCHED must be visible before we test polling */
604 smp_mb();
605 if (!tsk_is_polling(p))
606 smp_send_reschedule(cpu);
607}
608
609static void resched_cpu(int cpu)
610{
611 struct rq *rq = cpu_rq(cpu);
612 unsigned long flags;
613
614 if (!spin_trylock_irqsave(&rq->lock, flags))
615 return;
616 resched_task(cpu_curr(cpu));
617 spin_unlock_irqrestore(&rq->lock, flags);
618}
619#else
620static inline void resched_task(struct task_struct *p)
621{
622 assert_spin_locked(&task_rq(p)->lock);
623 set_tsk_need_resched(p);
624}
625#endif
626
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200627static u64 div64_likely32(u64 divident, unsigned long divisor)
628{
629#if BITS_PER_LONG == 32
630 if (likely(divident <= 0xffffffffULL))
631 return (u32)divident / divisor;
632 do_div(divident, divisor);
633
634 return divident;
635#else
636 return divident / divisor;
637#endif
638}
639
640#if BITS_PER_LONG == 32
641# define WMULT_CONST (~0UL)
642#else
643# define WMULT_CONST (1UL << 32)
644#endif
645
646#define WMULT_SHIFT 32
647
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +0200648static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200649calc_delta_mine(unsigned long delta_exec, unsigned long weight,
650 struct load_weight *lw)
651{
652 u64 tmp;
653
654 if (unlikely(!lw->inv_weight))
655 lw->inv_weight = WMULT_CONST / lw->weight;
656
657 tmp = (u64)delta_exec * weight;
658 /*
659 * Check whether we'd overflow the 64-bit multiplication:
660 */
661 if (unlikely(tmp > WMULT_CONST)) {
662 tmp = ((tmp >> WMULT_SHIFT/2) * lw->inv_weight)
663 >> (WMULT_SHIFT/2);
664 } else {
665 tmp = (tmp * lw->inv_weight) >> WMULT_SHIFT;
666 }
667
Ingo Molnarecf691d2007-08-02 17:41:40 +0200668 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200669}
670
671static inline unsigned long
672calc_delta_fair(unsigned long delta_exec, struct load_weight *lw)
673{
674 return calc_delta_mine(delta_exec, NICE_0_LOAD, lw);
675}
676
677static void update_load_add(struct load_weight *lw, unsigned long inc)
678{
679 lw->weight += inc;
680 lw->inv_weight = 0;
681}
682
683static void update_load_sub(struct load_weight *lw, unsigned long dec)
684{
685 lw->weight -= dec;
686 lw->inv_weight = 0;
687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689/*
Peter Williams2dd73a42006-06-27 02:54:34 -0700690 * To aid in avoiding the subversion of "niceness" due to uneven distribution
691 * of tasks with abnormal "nice" values across CPUs the contribution that
692 * each task makes to its run queue's load is weighted according to its
693 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
694 * scaled version of the new time slice allocation that they receive on time
695 * slice expiry etc.
696 */
697
Ingo Molnardd41f592007-07-09 18:51:59 +0200698#define WEIGHT_IDLEPRIO 2
699#define WMULT_IDLEPRIO (1 << 31)
700
701/*
702 * Nice levels are multiplicative, with a gentle 10% change for every
703 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
704 * nice 1, it will get ~10% less CPU time than another CPU-bound task
705 * that remained on nice 0.
706 *
707 * The "10% effect" is relative and cumulative: from _any_ nice level,
708 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee62007-07-16 09:46:30 +0200709 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
710 * If a task goes up by ~10% and another task goes down by ~10% then
711 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +0200712 */
713static const int prio_to_weight[40] = {
714/* -20 */ 88818, 71054, 56843, 45475, 36380, 29104, 23283, 18626, 14901, 11921,
715/* -10 */ 9537, 7629, 6103, 4883, 3906, 3125, 2500, 2000, 1600, 1280,
716/* 0 */ NICE_0_LOAD /* 1024 */,
717/* 1 */ 819, 655, 524, 419, 336, 268, 215, 172, 137,
718/* 10 */ 110, 87, 70, 56, 45, 36, 29, 23, 18, 15,
719};
720
Ingo Molnar5714d2d2007-07-16 09:46:31 +0200721/*
722 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
723 *
724 * In cases where the weight does not change often, we can use the
725 * precalculated inverse to speed up arithmetics by turning divisions
726 * into multiplications:
727 */
Ingo Molnardd41f592007-07-09 18:51:59 +0200728static const u32 prio_to_wmult[40] = {
Ingo Molnare4af30b2007-07-16 09:46:31 +0200729/* -20 */ 48356, 60446, 75558, 94446, 118058,
730/* -15 */ 147573, 184467, 230589, 288233, 360285,
731/* -10 */ 450347, 562979, 703746, 879575, 1099582,
732/* -5 */ 1374389, 1717986, 2147483, 2684354, 3355443,
733/* 0 */ 4194304, 5244160, 6557201, 8196502, 10250518,
734/* 5 */ 12782640, 16025997, 19976592, 24970740, 31350126,
735/* 10 */ 39045157, 49367440, 61356675, 76695844, 95443717,
736/* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +0200737};
Peter Williams2dd73a42006-06-27 02:54:34 -0700738
Ingo Molnardd41f592007-07-09 18:51:59 +0200739static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
740
741/*
742 * runqueue iterator, to support SMP load-balancing between different
743 * scheduling classes, without having to expose their internal data
744 * structures to the load-balancing proper:
745 */
746struct rq_iterator {
747 void *arg;
748 struct task_struct *(*start)(void *);
749 struct task_struct *(*next)(void *);
750};
751
752static int balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
753 unsigned long max_nr_move, unsigned long max_load_move,
754 struct sched_domain *sd, enum cpu_idle_type idle,
755 int *all_pinned, unsigned long *load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +0200756 int *this_best_prio, struct rq_iterator *iterator);
Ingo Molnardd41f592007-07-09 18:51:59 +0200757
758#include "sched_stats.h"
759#include "sched_rt.c"
760#include "sched_fair.c"
761#include "sched_idletask.c"
762#ifdef CONFIG_SCHED_DEBUG
763# include "sched_debug.c"
764#endif
765
766#define sched_class_highest (&rt_sched_class)
767
Ingo Molnar9c217242007-08-02 17:41:40 +0200768static void __update_curr_load(struct rq *rq, struct load_stat *ls)
769{
770 if (rq->curr != rq->idle && ls->load.weight) {
771 ls->delta_exec += ls->delta_stat;
772 ls->delta_fair += calc_delta_fair(ls->delta_stat, &ls->load);
773 ls->delta_stat = 0;
774 }
775}
776
777/*
778 * Update delta_exec, delta_fair fields for rq.
779 *
780 * delta_fair clock advances at a rate inversely proportional to
781 * total load (rq->ls.load.weight) on the runqueue, while
782 * delta_exec advances at the same rate as wall-clock (provided
783 * cpu is not idle).
784 *
785 * delta_exec / delta_fair is a measure of the (smoothened) load on this
786 * runqueue over any given interval. This (smoothened) load is used
787 * during load balance.
788 *
789 * This function is called /before/ updating rq->ls.load
790 * and when switching tasks.
791 */
792static void update_curr_load(struct rq *rq, u64 now)
793{
794 struct load_stat *ls = &rq->ls;
795 u64 start;
796
797 start = ls->load_update_start;
798 ls->load_update_start = now;
799 ls->delta_stat += now - start;
800 /*
801 * Stagger updates to ls->delta_fair. Very frequent updates
802 * can be expensive.
803 */
804 if (ls->delta_stat >= sysctl_sched_stat_granularity)
805 __update_curr_load(rq, ls);
806}
807
808static inline void
809inc_load(struct rq *rq, const struct task_struct *p, u64 now)
810{
811 update_curr_load(rq, now);
812 update_load_add(&rq->ls.load, p->se.load.weight);
813}
814
815static inline void
816dec_load(struct rq *rq, const struct task_struct *p, u64 now)
817{
818 update_curr_load(rq, now);
819 update_load_sub(&rq->ls.load, p->se.load.weight);
820}
821
822static void inc_nr_running(struct task_struct *p, struct rq *rq, u64 now)
823{
824 rq->nr_running++;
825 inc_load(rq, p, now);
826}
827
828static void dec_nr_running(struct task_struct *p, struct rq *rq, u64 now)
829{
830 rq->nr_running--;
831 dec_load(rq, p, now);
832}
833
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200834static void set_load_weight(struct task_struct *p)
835{
Ingo Molnardd41f592007-07-09 18:51:59 +0200836 task_rq(p)->cfs.wait_runtime -= p->se.wait_runtime;
837 p->se.wait_runtime = 0;
838
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200839 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +0200840 p->se.load.weight = prio_to_weight[0] * 2;
841 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
842 return;
843 }
844
845 /*
846 * SCHED_IDLE tasks get minimal weight:
847 */
848 if (p->policy == SCHED_IDLE) {
849 p->se.load.weight = WEIGHT_IDLEPRIO;
850 p->se.load.inv_weight = WMULT_IDLEPRIO;
851 return;
852 }
853
854 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
855 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200856}
857
Ingo Molnardd41f592007-07-09 18:51:59 +0200858static void
859enqueue_task(struct rq *rq, struct task_struct *p, int wakeup, u64 now)
Ingo Molnar71f8bd42007-07-09 18:51:59 +0200860{
861 sched_info_queued(p);
Ingo Molnardd41f592007-07-09 18:51:59 +0200862 p->sched_class->enqueue_task(rq, p, wakeup, now);
863 p->se.on_rq = 1;
864}
865
866static void
867dequeue_task(struct rq *rq, struct task_struct *p, int sleep, u64 now)
868{
869 p->sched_class->dequeue_task(rq, p, sleep, now);
870 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +0200871}
872
873/*
Ingo Molnardd41f592007-07-09 18:51:59 +0200874 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +0200875 */
Ingo Molnar14531182007-07-09 18:51:59 +0200876static inline int __normal_prio(struct task_struct *p)
877{
Ingo Molnardd41f592007-07-09 18:51:59 +0200878 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +0200879}
880
881/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700882 * Calculate the expected normal priority: i.e. priority
883 * without taking RT-inheritance into account. Might be
884 * boosted by interactivity modifiers. Changes upon fork,
885 * setprio syscalls, and whenever the interactivity
886 * estimator recalculates.
887 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700888static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700889{
890 int prio;
891
Ingo Molnare05606d2007-07-09 18:51:59 +0200892 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -0700893 prio = MAX_RT_PRIO-1 - p->rt_priority;
894 else
895 prio = __normal_prio(p);
896 return prio;
897}
898
899/*
900 * Calculate the current priority, i.e. the priority
901 * taken into account by the scheduler. This value might
902 * be boosted by RT tasks, or might be boosted by
903 * interactivity modifiers. Will be RT if the task got
904 * RT-boosted. If not then it returns p->normal_prio.
905 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700906static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700907{
908 p->normal_prio = normal_prio(p);
909 /*
910 * If we are RT tasks or we were boosted to RT priority,
911 * keep the priority unchanged. Otherwise, update priority
912 * to the normal priority:
913 */
914 if (!rt_prio(p->prio))
915 return p->normal_prio;
916 return p->prio;
917}
918
919/*
Ingo Molnardd41f592007-07-09 18:51:59 +0200920 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 */
Ingo Molnardd41f592007-07-09 18:51:59 +0200922static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Ingo Molnara8e504d2007-08-09 11:16:47 +0200924 u64 now;
925
926 update_rq_clock(rq);
927 now = rq->clock;
Con Kolivasd425b272006-03-31 02:31:29 -0800928
Ingo Molnardd41f592007-07-09 18:51:59 +0200929 if (p->state == TASK_UNINTERRUPTIBLE)
930 rq->nr_uninterruptible--;
931
932 enqueue_task(rq, p, wakeup, now);
933 inc_nr_running(p, rq, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
936/*
Ingo Molnardd41f592007-07-09 18:51:59 +0200937 * activate_idle_task - move idle task to the _front_ of runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 */
Ingo Molnardd41f592007-07-09 18:51:59 +0200939static inline void activate_idle_task(struct task_struct *p, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Ingo Molnara8e504d2007-08-09 11:16:47 +0200941 u64 now;
942
943 update_rq_clock(rq);
944 now = rq->clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Ingo Molnardd41f592007-07-09 18:51:59 +0200946 if (p->state == TASK_UNINTERRUPTIBLE)
947 rq->nr_uninterruptible--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Ingo Molnardd41f592007-07-09 18:51:59 +0200949 enqueue_task(rq, p, 0, now);
950 inc_nr_running(p, rq, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
953/*
954 * deactivate_task - remove a task from the runqueue.
955 */
Ingo Molnar8e717b12007-08-09 11:16:46 +0200956static void
957deactivate_task(struct rq *rq, struct task_struct *p, int sleep, u64 now)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Ingo Molnardd41f592007-07-09 18:51:59 +0200959 if (p->state == TASK_UNINTERRUPTIBLE)
960 rq->nr_uninterruptible++;
961
962 dequeue_task(rq, p, sleep, now);
963 dec_nr_running(p, rq, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966/**
967 * task_curr - is this task currently executing on a CPU?
968 * @p: the task in question.
969 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700970inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 return cpu_curr(task_cpu(p)) == p;
973}
974
Peter Williams2dd73a42006-06-27 02:54:34 -0700975/* Used instead of source_load when we know the type == 0 */
976unsigned long weighted_cpuload(const int cpu)
977{
Ingo Molnardd41f592007-07-09 18:51:59 +0200978 return cpu_rq(cpu)->ls.load.weight;
979}
980
981static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
982{
983#ifdef CONFIG_SMP
984 task_thread_info(p)->cpu = cpu;
985 set_task_cfs_rq(p);
986#endif
Peter Williams2dd73a42006-06-27 02:54:34 -0700987}
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +0200990
Ingo Molnardd41f592007-07-09 18:51:59 +0200991void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +0200992{
Ingo Molnardd41f592007-07-09 18:51:59 +0200993 int old_cpu = task_cpu(p);
994 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
995 u64 clock_offset, fair_clock_offset;
996
997 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +0200998 fair_clock_offset = old_rq->cfs.fair_clock - new_rq->cfs.fair_clock;
999
Ingo Molnardd41f592007-07-09 18:51:59 +02001000 if (p->se.wait_start_fair)
1001 p->se.wait_start_fair -= fair_clock_offset;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001002 if (p->se.sleep_start_fair)
1003 p->se.sleep_start_fair -= fair_clock_offset;
1004
1005#ifdef CONFIG_SCHEDSTATS
1006 if (p->se.wait_start)
1007 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +02001008 if (p->se.sleep_start)
1009 p->se.sleep_start -= clock_offset;
1010 if (p->se.block_start)
1011 p->se.block_start -= clock_offset;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001012#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02001013
1014 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001015}
1016
Ingo Molnar70b97a72006-07-03 00:25:42 -07001017struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Ingo Molnar36c8b582006-07-03 00:25:41 -07001020 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 int dest_cpu;
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001024};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026/*
1027 * The task's runqueue lock must be held.
1028 * Returns true if you have to wait for migration thread.
1029 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001030static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07001031migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001033 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 /*
1036 * If the task is not on a runqueue (and not running), then
1037 * it is sufficient to simply update the task's cpu field.
1038 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001039 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 set_task_cpu(p, dest_cpu);
1041 return 0;
1042 }
1043
1044 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 req->task = p;
1046 req->dest_cpu = dest_cpu;
1047 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07001048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return 1;
1050}
1051
1052/*
1053 * wait_task_inactive - wait for a thread to unschedule.
1054 *
1055 * The caller must ensure that the task *will* unschedule sometime soon,
1056 * else this function might spin for a *long* time. This function can't
1057 * be called with interrupts off, or it may introduce deadlock with
1058 * smp_call_function() if an IPI is sent by the same process we are
1059 * waiting to become inactive.
1060 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001061void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
1063 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001064 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001065 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067repeat:
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001068 /*
1069 * We do the initial early heuristics without holding
1070 * any task-queue locks at all. We'll only try to get
1071 * the runqueue lock when things look like they will
1072 * work out!
1073 */
1074 rq = task_rq(p);
1075
1076 /*
1077 * If the task is actively running on another CPU
1078 * still, just relax and busy-wait without holding
1079 * any locks.
1080 *
1081 * NOTE! Since we don't hold any locks, it's not
1082 * even sure that "rq" stays as the right runqueue!
1083 * But we don't care, since "task_running()" will
1084 * return false if the runqueue has changed and p
1085 * is actually now running somewhere else!
1086 */
1087 while (task_running(rq, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001089
1090 /*
1091 * Ok, time to look more closely! We need the rq
1092 * lock now, to be *sure*. If we're wrong, we'll
1093 * just go back and repeat.
1094 */
1095 rq = task_rq_lock(p, &flags);
1096 running = task_running(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02001097 on_rq = p->se.on_rq;
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001098 task_rq_unlock(rq, &flags);
1099
1100 /*
1101 * Was it really running after all now that we
1102 * checked with the proper locks actually held?
1103 *
1104 * Oops. Go back and try again..
1105 */
1106 if (unlikely(running)) {
1107 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 goto repeat;
1109 }
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001110
1111 /*
1112 * It's not enough that it's not actively running,
1113 * it must be off the runqueue _entirely_, and not
1114 * preempted!
1115 *
1116 * So if it wa still runnable (but just not actively
1117 * running right now), it's preempted, and we should
1118 * yield - it could be a while.
1119 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001120 if (unlikely(on_rq)) {
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001121 yield();
1122 goto repeat;
1123 }
1124
1125 /*
1126 * Ahh, all good. It wasn't running, and it wasn't
1127 * runnable, which means that it will never become
1128 * running in the future either. We're all done!
1129 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
1132/***
1133 * kick_process - kick a running thread to enter/exit the kernel
1134 * @p: the to-be-kicked thread
1135 *
1136 * Cause a process which is running on another CPU to enter
1137 * kernel-mode, without any delay. (to get signals handled.)
1138 *
1139 * NOTE: this function doesnt have to take the runqueue lock,
1140 * because all it wants to ensure is that the remote task enters
1141 * the kernel. If the IPI races and the task has been migrated
1142 * to another CPU then no harm is done and the purpose has been
1143 * achieved as well.
1144 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001145void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 int cpu;
1148
1149 preempt_disable();
1150 cpu = task_cpu(p);
1151 if ((cpu != smp_processor_id()) && task_curr(p))
1152 smp_send_reschedule(cpu);
1153 preempt_enable();
1154}
1155
1156/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001157 * Return a low guess at the load of a migration-source cpu weighted
1158 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 *
1160 * We want to under-estimate the load of migration sources, to
1161 * balance conservatively.
1162 */
Con Kolivasb9104722005-11-08 21:38:55 -08001163static inline unsigned long source_load(int cpu, int type)
1164{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001165 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001166 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001167
Peter Williams2dd73a42006-06-27 02:54:34 -07001168 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001169 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001170
Ingo Molnardd41f592007-07-09 18:51:59 +02001171 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
1174/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001175 * Return a high guess at the load of a migration-target cpu weighted
1176 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 */
Con Kolivasb9104722005-11-08 21:38:55 -08001178static inline unsigned long target_load(int cpu, int type)
1179{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001180 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001181 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001182
Peter Williams2dd73a42006-06-27 02:54:34 -07001183 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001184 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001185
Ingo Molnardd41f592007-07-09 18:51:59 +02001186 return max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07001187}
1188
1189/*
1190 * Return the average load per task on the cpu's run queue
1191 */
1192static inline unsigned long cpu_avg_load_per_task(int cpu)
1193{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001194 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001195 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07001196 unsigned long n = rq->nr_running;
1197
Ingo Molnardd41f592007-07-09 18:51:59 +02001198 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
Nick Piggin147cbb42005-06-25 14:57:19 -07001201/*
1202 * find_idlest_group finds and returns the least busy CPU group within the
1203 * domain.
1204 */
1205static struct sched_group *
1206find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
1207{
1208 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
1209 unsigned long min_load = ULONG_MAX, this_load = 0;
1210 int load_idx = sd->forkexec_idx;
1211 int imbalance = 100 + (sd->imbalance_pct-100)/2;
1212
1213 do {
1214 unsigned long load, avg_load;
1215 int local_group;
1216 int i;
1217
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001218 /* Skip over this group if it has no CPUs allowed */
1219 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
1220 goto nextgroup;
1221
Nick Piggin147cbb42005-06-25 14:57:19 -07001222 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07001223
1224 /* Tally up the load of all CPUs in the group */
1225 avg_load = 0;
1226
1227 for_each_cpu_mask(i, group->cpumask) {
1228 /* Bias balancing toward cpus of our domain */
1229 if (local_group)
1230 load = source_load(i, load_idx);
1231 else
1232 load = target_load(i, load_idx);
1233
1234 avg_load += load;
1235 }
1236
1237 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07001238 avg_load = sg_div_cpu_power(group,
1239 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07001240
1241 if (local_group) {
1242 this_load = avg_load;
1243 this = group;
1244 } else if (avg_load < min_load) {
1245 min_load = avg_load;
1246 idlest = group;
1247 }
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001248nextgroup:
Nick Piggin147cbb42005-06-25 14:57:19 -07001249 group = group->next;
1250 } while (group != sd->groups);
1251
1252 if (!idlest || 100*this_load < imbalance*min_load)
1253 return NULL;
1254 return idlest;
1255}
1256
1257/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07001258 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07001259 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07001260static int
1261find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
Nick Piggin147cbb42005-06-25 14:57:19 -07001262{
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001263 cpumask_t tmp;
Nick Piggin147cbb42005-06-25 14:57:19 -07001264 unsigned long load, min_load = ULONG_MAX;
1265 int idlest = -1;
1266 int i;
1267
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001268 /* Traverse only the allowed CPUs */
1269 cpus_and(tmp, group->cpumask, p->cpus_allowed);
1270
1271 for_each_cpu_mask(i, tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07001272 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07001273
1274 if (load < min_load || (load == min_load && i == this_cpu)) {
1275 min_load = load;
1276 idlest = i;
1277 }
1278 }
1279
1280 return idlest;
1281}
1282
Nick Piggin476d1392005-06-25 14:57:29 -07001283/*
1284 * sched_balance_self: balance the current task (running on cpu) in domains
1285 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
1286 * SD_BALANCE_EXEC.
1287 *
1288 * Balance, ie. select the least loaded group.
1289 *
1290 * Returns the target CPU number, or the same CPU if no balancing is needed.
1291 *
1292 * preempt must be disabled.
1293 */
1294static int sched_balance_self(int cpu, int flag)
1295{
1296 struct task_struct *t = current;
1297 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07001298
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001299 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02001300 /*
1301 * If power savings logic is enabled for a domain, stop there.
1302 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07001303 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
1304 break;
Nick Piggin476d1392005-06-25 14:57:29 -07001305 if (tmp->flags & flag)
1306 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001307 }
Nick Piggin476d1392005-06-25 14:57:29 -07001308
1309 while (sd) {
1310 cpumask_t span;
1311 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001312 int new_cpu, weight;
1313
1314 if (!(sd->flags & flag)) {
1315 sd = sd->child;
1316 continue;
1317 }
Nick Piggin476d1392005-06-25 14:57:29 -07001318
1319 span = sd->span;
1320 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001321 if (!group) {
1322 sd = sd->child;
1323 continue;
1324 }
Nick Piggin476d1392005-06-25 14:57:29 -07001325
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001326 new_cpu = find_idlest_cpu(group, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001327 if (new_cpu == -1 || new_cpu == cpu) {
1328 /* Now try balancing at a lower domain level of cpu */
1329 sd = sd->child;
1330 continue;
1331 }
Nick Piggin476d1392005-06-25 14:57:29 -07001332
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001333 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07001334 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07001335 sd = NULL;
1336 weight = cpus_weight(span);
1337 for_each_domain(cpu, tmp) {
1338 if (weight <= cpus_weight(tmp->span))
1339 break;
1340 if (tmp->flags & flag)
1341 sd = tmp;
1342 }
1343 /* while loop will break here if sd == NULL */
1344 }
1345
1346 return cpu;
1347}
1348
1349#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351/*
1352 * wake_idle() will wake a task on an idle cpu if task->cpu is
1353 * not idle and an idle cpu is available. The span of cpus to
1354 * search starts with cpus closest then further out as needed,
1355 * so we always favor a closer, idle cpu.
1356 *
1357 * Returns the CPU we should wake onto.
1358 */
1359#if defined(ARCH_HAS_SCHED_WAKE_IDLE)
Ingo Molnar36c8b582006-07-03 00:25:41 -07001360static int wake_idle(int cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 cpumask_t tmp;
1363 struct sched_domain *sd;
1364 int i;
1365
Siddha, Suresh B49531982007-05-08 00:33:01 -07001366 /*
1367 * If it is idle, then it is the best cpu to run this task.
1368 *
1369 * This cpu is also the best, if it has more than one task already.
1370 * Siblings must be also busy(in most cases) as they didn't already
1371 * pickup the extra load from this cpu and hence we need not check
1372 * sibling runqueue info. This will avoid the checks and cache miss
1373 * penalities associated with that.
1374 */
1375 if (idle_cpu(cpu) || cpu_rq(cpu)->nr_running > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return cpu;
1377
1378 for_each_domain(cpu, sd) {
1379 if (sd->flags & SD_WAKE_IDLE) {
Nick Piggine0f364f2005-06-25 14:57:06 -07001380 cpus_and(tmp, sd->span, p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 for_each_cpu_mask(i, tmp) {
1382 if (idle_cpu(i))
1383 return i;
1384 }
Ingo Molnar9761eea2007-07-09 18:52:00 +02001385 } else {
Nick Piggine0f364f2005-06-25 14:57:06 -07001386 break;
Ingo Molnar9761eea2007-07-09 18:52:00 +02001387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389 return cpu;
1390}
1391#else
Ingo Molnar36c8b582006-07-03 00:25:41 -07001392static inline int wake_idle(int cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
1394 return cpu;
1395}
1396#endif
1397
1398/***
1399 * try_to_wake_up - wake up a thread
1400 * @p: the to-be-woken-up thread
1401 * @state: the mask of task states that can be woken
1402 * @sync: do a synchronous wakeup?
1403 *
1404 * Put it on the run-queue if it's not already there. The "current"
1405 * thread is always on the run-queue (except when the actual
1406 * re-schedule is in progress), and as such you're allowed to do
1407 * the simpler "current->state = TASK_RUNNING" to mark yourself
1408 * runnable without the overhead of this.
1409 *
1410 * returns failure only if the task is already active.
1411 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001412static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
1414 int cpu, this_cpu, success = 0;
1415 unsigned long flags;
1416 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001417 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418#ifdef CONFIG_SMP
Nick Piggin78979862005-06-25 14:57:13 -07001419 struct sched_domain *sd, *this_sd = NULL;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001420 unsigned long load, this_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 int new_cpu;
1422#endif
1423
1424 rq = task_rq_lock(p, &flags);
1425 old_state = p->state;
1426 if (!(old_state & state))
1427 goto out;
1428
Ingo Molnardd41f592007-07-09 18:51:59 +02001429 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 goto out_running;
1431
1432 cpu = task_cpu(p);
1433 this_cpu = smp_processor_id();
1434
1435#ifdef CONFIG_SMP
1436 if (unlikely(task_running(rq, p)))
1437 goto out_activate;
1438
Nick Piggin78979862005-06-25 14:57:13 -07001439 new_cpu = cpu;
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 schedstat_inc(rq, ttwu_cnt);
1442 if (cpu == this_cpu) {
1443 schedstat_inc(rq, ttwu_local);
Nick Piggin78979862005-06-25 14:57:13 -07001444 goto out_set_cpu;
1445 }
1446
1447 for_each_domain(this_cpu, sd) {
1448 if (cpu_isset(cpu, sd->span)) {
1449 schedstat_inc(sd, ttwu_wake_remote);
1450 this_sd = sd;
1451 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Nick Piggin78979862005-06-25 14:57:13 -07001455 if (unlikely(!cpu_isset(this_cpu, p->cpus_allowed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 goto out_set_cpu;
1457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 /*
Nick Piggin78979862005-06-25 14:57:13 -07001459 * Check for affine wakeup and passive balancing possibilities.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 */
Nick Piggin78979862005-06-25 14:57:13 -07001461 if (this_sd) {
1462 int idx = this_sd->wake_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 unsigned int imbalance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Nick Piggina3f21bc2005-06-25 14:57:15 -07001465 imbalance = 100 + (this_sd->imbalance_pct - 100) / 2;
1466
Nick Piggin78979862005-06-25 14:57:13 -07001467 load = source_load(cpu, idx);
1468 this_load = target_load(this_cpu, idx);
1469
Nick Piggin78979862005-06-25 14:57:13 -07001470 new_cpu = this_cpu; /* Wake to this CPU if we can */
1471
Nick Piggina3f21bc2005-06-25 14:57:15 -07001472 if (this_sd->flags & SD_WAKE_AFFINE) {
1473 unsigned long tl = this_load;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08001474 unsigned long tl_per_task;
1475
1476 tl_per_task = cpu_avg_load_per_task(this_cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 /*
Nick Piggina3f21bc2005-06-25 14:57:15 -07001479 * If sync wakeup then subtract the (maximum possible)
1480 * effect of the currently running task from the load
1481 * of the current CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 */
Nick Piggina3f21bc2005-06-25 14:57:15 -07001483 if (sync)
Ingo Molnardd41f592007-07-09 18:51:59 +02001484 tl -= current->se.load.weight;
Nick Piggina3f21bc2005-06-25 14:57:15 -07001485
1486 if ((tl <= load &&
Peter Williams2dd73a42006-06-27 02:54:34 -07001487 tl + target_load(cpu, idx) <= tl_per_task) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02001488 100*(tl + p->se.load.weight) <= imbalance*load) {
Nick Piggina3f21bc2005-06-25 14:57:15 -07001489 /*
1490 * This domain has SD_WAKE_AFFINE and
1491 * p is cache cold in this domain, and
1492 * there is no bad imbalance.
1493 */
1494 schedstat_inc(this_sd, ttwu_move_affine);
1495 goto out_set_cpu;
1496 }
1497 }
1498
1499 /*
1500 * Start passive balancing when half the imbalance_pct
1501 * limit is reached.
1502 */
1503 if (this_sd->flags & SD_WAKE_BALANCE) {
1504 if (imbalance*this_load <= 100*load) {
1505 schedstat_inc(this_sd, ttwu_move_balance);
1506 goto out_set_cpu;
1507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 }
1509 }
1510
1511 new_cpu = cpu; /* Could not wake to this_cpu. Wake to cpu instead */
1512out_set_cpu:
1513 new_cpu = wake_idle(new_cpu, p);
1514 if (new_cpu != cpu) {
1515 set_task_cpu(p, new_cpu);
1516 task_rq_unlock(rq, &flags);
1517 /* might preempt at this point */
1518 rq = task_rq_lock(p, &flags);
1519 old_state = p->state;
1520 if (!(old_state & state))
1521 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02001522 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 goto out_running;
1524
1525 this_cpu = smp_processor_id();
1526 cpu = task_cpu(p);
1527 }
1528
1529out_activate:
1530#endif /* CONFIG_SMP */
Ingo Molnardd41f592007-07-09 18:51:59 +02001531 activate_task(rq, p, 1);
Ingo Molnard79fc0f2005-09-10 00:26:12 -07001532 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 * Sync wakeups (i.e. those types of wakeups where the waker
1534 * has indicated that it will leave the CPU in short order)
1535 * don't trigger a preemption, if the woken up task will run on
1536 * this cpu. (in this case the 'I will reschedule' promise of
1537 * the waker guarantees that the freshly woken up task is going
1538 * to be considered on this CPU.)
1539 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001540 if (!sync || cpu != this_cpu)
1541 check_preempt_curr(rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 success = 1;
1543
1544out_running:
1545 p->state = TASK_RUNNING;
1546out:
1547 task_rq_unlock(rq, &flags);
1548
1549 return success;
1550}
1551
Ingo Molnar36c8b582006-07-03 00:25:41 -07001552int fastcall wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
1554 return try_to_wake_up(p, TASK_STOPPED | TASK_TRACED |
1555 TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE, 0);
1556}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557EXPORT_SYMBOL(wake_up_process);
1558
Ingo Molnar36c8b582006-07-03 00:25:41 -07001559int fastcall wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
1561 return try_to_wake_up(p, state, 0);
1562}
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564/*
1565 * Perform scheduler related setup for a newly forked process p.
1566 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02001567 *
1568 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001570static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
Ingo Molnardd41f592007-07-09 18:51:59 +02001572 p->se.wait_start_fair = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02001573 p->se.exec_start = 0;
1574 p->se.sum_exec_runtime = 0;
1575 p->se.delta_exec = 0;
1576 p->se.delta_fair_run = 0;
1577 p->se.delta_fair_sleep = 0;
1578 p->se.wait_runtime = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001579 p->se.sleep_start_fair = 0;
1580
1581#ifdef CONFIG_SCHEDSTATS
1582 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02001583 p->se.sum_wait_runtime = 0;
1584 p->se.sum_sleep_runtime = 0;
1585 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02001586 p->se.block_start = 0;
1587 p->se.sleep_max = 0;
1588 p->se.block_max = 0;
1589 p->se.exec_max = 0;
1590 p->se.wait_max = 0;
1591 p->se.wait_runtime_overruns = 0;
1592 p->se.wait_runtime_underruns = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001593#endif
Nick Piggin476d1392005-06-25 14:57:29 -07001594
Ingo Molnardd41f592007-07-09 18:51:59 +02001595 INIT_LIST_HEAD(&p->run_list);
1596 p->se.on_rq = 0;
Nick Piggin476d1392005-06-25 14:57:29 -07001597
Avi Kivitye107be32007-07-26 13:40:43 +02001598#ifdef CONFIG_PREEMPT_NOTIFIERS
1599 INIT_HLIST_HEAD(&p->preempt_notifiers);
1600#endif
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 /*
1603 * We mark the process as running here, but have not actually
1604 * inserted it onto the runqueue yet. This guarantees that
1605 * nobody will actually run it, and a signal or other external
1606 * event cannot wake it up and insert it on the runqueue either.
1607 */
1608 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02001609}
1610
1611/*
1612 * fork()/clone()-time setup:
1613 */
1614void sched_fork(struct task_struct *p, int clone_flags)
1615{
1616 int cpu = get_cpu();
1617
1618 __sched_fork(p);
1619
1620#ifdef CONFIG_SMP
1621 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
1622#endif
1623 __set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07001624
1625 /*
1626 * Make sure we do not leak PI boosting priority to the child:
1627 */
1628 p->prio = current->normal_prio;
1629
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07001630#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02001631 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07001632 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08001634#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07001635 p->oncpu = 0;
1636#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07001638 /* Want to start with kernel preemption disabled. */
Al Viroa1261f542005-11-13 16:06:55 -08001639 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640#endif
Nick Piggin476d1392005-06-25 14:57:29 -07001641 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643
1644/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001645 * After fork, child runs first. (default) If set to 0 then
1646 * parent will (try to) run first.
1647 */
1648unsigned int __read_mostly sysctl_sched_child_runs_first = 1;
1649
1650/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 * wake_up_new_task - wake up a newly created task for the first time.
1652 *
1653 * This function will do some initial scheduler statistics housekeeping
1654 * that must be done for every newly created context, then puts the task
1655 * on the runqueue and wakes it.
1656 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001657void fastcall wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
1659 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001660 struct rq *rq;
1661 int this_cpu;
Ingo Molnarcad60d92007-08-02 17:41:40 +02001662 u64 now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnardd41f592007-07-09 18:51:59 +02001666 this_cpu = smp_processor_id(); /* parent's CPU */
Ingo Molnara8e504d2007-08-09 11:16:47 +02001667 update_rq_clock(rq);
1668 now = rq->clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 p->prio = effective_prio(p);
1671
Ingo Molnarcad60d92007-08-02 17:41:40 +02001672 if (!p->sched_class->task_new || !sysctl_sched_child_runs_first ||
1673 (clone_flags & CLONE_VM) || task_cpu(p) != this_cpu ||
1674 !current->se.on_rq) {
1675
Ingo Molnardd41f592007-07-09 18:51:59 +02001676 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02001679 * Let the scheduling class do new task startup
1680 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 */
Ingo Molnarcad60d92007-08-02 17:41:40 +02001682 p->sched_class->task_new(rq, p, now);
1683 inc_nr_running(p, rq, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 }
Ingo Molnardd41f592007-07-09 18:51:59 +02001685 check_preempt_curr(rq, p);
1686 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
Avi Kivitye107be32007-07-26 13:40:43 +02001689#ifdef CONFIG_PREEMPT_NOTIFIERS
1690
1691/**
Randy Dunlap421cee22007-07-31 00:37:50 -07001692 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
1693 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02001694 */
1695void preempt_notifier_register(struct preempt_notifier *notifier)
1696{
1697 hlist_add_head(&notifier->link, &current->preempt_notifiers);
1698}
1699EXPORT_SYMBOL_GPL(preempt_notifier_register);
1700
1701/**
1702 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07001703 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02001704 *
1705 * This is safe to call from within a preemption notifier.
1706 */
1707void preempt_notifier_unregister(struct preempt_notifier *notifier)
1708{
1709 hlist_del(&notifier->link);
1710}
1711EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
1712
1713static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
1714{
1715 struct preempt_notifier *notifier;
1716 struct hlist_node *node;
1717
1718 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
1719 notifier->ops->sched_in(notifier, raw_smp_processor_id());
1720}
1721
1722static void
1723fire_sched_out_preempt_notifiers(struct task_struct *curr,
1724 struct task_struct *next)
1725{
1726 struct preempt_notifier *notifier;
1727 struct hlist_node *node;
1728
1729 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
1730 notifier->ops->sched_out(notifier, next);
1731}
1732
1733#else
1734
1735static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
1736{
1737}
1738
1739static void
1740fire_sched_out_preempt_notifiers(struct task_struct *curr,
1741 struct task_struct *next)
1742{
1743}
1744
1745#endif
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747/**
Nick Piggin4866cde2005-06-25 14:57:23 -07001748 * prepare_task_switch - prepare to switch tasks
1749 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07001750 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07001751 * @next: the task we are going to switch to.
1752 *
1753 * This is called with the rq lock held and interrupts off. It must
1754 * be paired with a subsequent finish_task_switch after the context
1755 * switch.
1756 *
1757 * prepare_task_switch sets up locking and calls architecture specific
1758 * hooks.
1759 */
Avi Kivitye107be32007-07-26 13:40:43 +02001760static inline void
1761prepare_task_switch(struct rq *rq, struct task_struct *prev,
1762 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07001763{
Avi Kivitye107be32007-07-26 13:40:43 +02001764 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07001765 prepare_lock_switch(rq, next);
1766 prepare_arch_switch(next);
1767}
1768
1769/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04001771 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 * @prev: the thread we just switched away from.
1773 *
Nick Piggin4866cde2005-06-25 14:57:23 -07001774 * finish_task_switch must be called after the context switch, paired
1775 * with a prepare_task_switch call before the context switch.
1776 * finish_task_switch will reconcile locking set up by prepare_task_switch,
1777 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 *
1779 * Note that we may have delayed dropping an mm in context_switch(). If
1780 * so, we finish that here outside of the runqueue lock. (Doing it
1781 * with the lock held can cause deadlocks; see schedule() for
1782 * details.)
1783 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07001784static inline void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 __releases(rq->lock)
1786{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07001788 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 rq->prev_mm = NULL;
1791
1792 /*
1793 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07001794 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07001795 * schedule one last time. The schedule call will never return, and
1796 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07001797 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 * still held, otherwise prev could be scheduled on another cpu, die
1799 * there before we look at prev->state, and then the reference would
1800 * be dropped twice.
1801 * Manfred Spraul <manfred@colorfullife.com>
1802 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07001803 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07001804 finish_arch_switch(prev);
1805 finish_lock_switch(rq, prev);
Avi Kivitye107be32007-07-26 13:40:43 +02001806 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (mm)
1808 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07001809 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08001810 /*
1811 * Remove function-return probe instances associated with this
1812 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02001813 */
bibo maoc6fd91f2006-03-26 01:38:20 -08001814 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08001816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
1818
1819/**
1820 * schedule_tail - first thing a freshly forked thread must call.
1821 * @prev: the thread we just switched away from.
1822 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001823asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 __releases(rq->lock)
1825{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001826 struct rq *rq = this_rq();
1827
Nick Piggin4866cde2005-06-25 14:57:23 -07001828 finish_task_switch(rq, prev);
1829#ifdef __ARCH_WANT_UNLOCKED_CTXSW
1830 /* In this case, finish_task_switch does not reenable preemption */
1831 preempt_enable();
1832#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 if (current->set_child_tid)
1834 put_user(current->pid, current->set_child_tid);
1835}
1836
1837/*
1838 * context_switch - switch to the new MM and the new
1839 * thread's register state.
1840 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001841static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07001842context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07001843 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
Ingo Molnardd41f592007-07-09 18:51:59 +02001845 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Avi Kivitye107be32007-07-26 13:40:43 +02001847 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02001848 mm = next->mm;
1849 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01001850 /*
1851 * For paravirt, this is coupled with an exit in switch_to to
1852 * combine the page table reload and the switch backend into
1853 * one hypercall.
1854 */
1855 arch_enter_lazy_cpu_mode();
1856
Ingo Molnardd41f592007-07-09 18:51:59 +02001857 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 next->active_mm = oldmm;
1859 atomic_inc(&oldmm->mm_count);
1860 enter_lazy_tlb(oldmm, next);
1861 } else
1862 switch_mm(oldmm, mm, next);
1863
Ingo Molnardd41f592007-07-09 18:51:59 +02001864 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 rq->prev_mm = oldmm;
1867 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07001868 /*
1869 * Since the runqueue lock will be released by the next
1870 * task (which is an invalid locking op but in the case
1871 * of the scheduler it's an obvious special-case), so we
1872 * do an early lockdep release here:
1873 */
1874#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07001875 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07001876#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
1878 /* Here we just switch the register state and the stack. */
1879 switch_to(prev, next, prev);
1880
Ingo Molnardd41f592007-07-09 18:51:59 +02001881 barrier();
1882 /*
1883 * this_rq must be evaluated again because prev may have moved
1884 * CPUs since it called schedule(), thus the 'rq' on its stack
1885 * frame will be invalid.
1886 */
1887 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888}
1889
1890/*
1891 * nr_running, nr_uninterruptible and nr_context_switches:
1892 *
1893 * externally visible scheduler statistics: current number of runnable
1894 * threads, current number of uninterruptible-sleeping threads, total
1895 * number of context switches performed since bootup.
1896 */
1897unsigned long nr_running(void)
1898{
1899 unsigned long i, sum = 0;
1900
1901 for_each_online_cpu(i)
1902 sum += cpu_rq(i)->nr_running;
1903
1904 return sum;
1905}
1906
1907unsigned long nr_uninterruptible(void)
1908{
1909 unsigned long i, sum = 0;
1910
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001911 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 sum += cpu_rq(i)->nr_uninterruptible;
1913
1914 /*
1915 * Since we read the counters lockless, it might be slightly
1916 * inaccurate. Do not allow it to go below zero though:
1917 */
1918 if (unlikely((long)sum < 0))
1919 sum = 0;
1920
1921 return sum;
1922}
1923
1924unsigned long long nr_context_switches(void)
1925{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07001926 int i;
1927 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001929 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 sum += cpu_rq(i)->nr_switches;
1931
1932 return sum;
1933}
1934
1935unsigned long nr_iowait(void)
1936{
1937 unsigned long i, sum = 0;
1938
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001939 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 sum += atomic_read(&cpu_rq(i)->nr_iowait);
1941
1942 return sum;
1943}
1944
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08001945unsigned long nr_active(void)
1946{
1947 unsigned long i, running = 0, uninterruptible = 0;
1948
1949 for_each_online_cpu(i) {
1950 running += cpu_rq(i)->nr_running;
1951 uninterruptible += cpu_rq(i)->nr_uninterruptible;
1952 }
1953
1954 if (unlikely((long)uninterruptible < 0))
1955 uninterruptible = 0;
1956
1957 return running + uninterruptible;
1958}
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001961 * Update rq->cpu_load[] statistics. This function is usually called every
1962 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07001963 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001964static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07001965{
Ingo Molnardd41f592007-07-09 18:51:59 +02001966 u64 fair_delta64, exec_delta64, idle_delta64, sample_interval64, tmp64;
1967 unsigned long total_load = this_rq->ls.load.weight;
1968 unsigned long this_load = total_load;
1969 struct load_stat *ls = &this_rq->ls;
Ingo Molnarc1b3da32007-08-09 11:16:47 +02001970 u64 now;
Ingo Molnardd41f592007-07-09 18:51:59 +02001971 int i, scale;
1972
Ingo Molnarc1b3da32007-08-09 11:16:47 +02001973 __update_rq_clock(this_rq);
1974 now = this_rq->clock;
1975
Ingo Molnardd41f592007-07-09 18:51:59 +02001976 this_rq->nr_load_updates++;
1977 if (unlikely(!(sysctl_sched_features & SCHED_FEAT_PRECISE_CPU_LOAD)))
1978 goto do_avg;
1979
1980 /* Update delta_fair/delta_exec fields first */
1981 update_curr_load(this_rq, now);
1982
1983 fair_delta64 = ls->delta_fair + 1;
1984 ls->delta_fair = 0;
1985
1986 exec_delta64 = ls->delta_exec + 1;
1987 ls->delta_exec = 0;
1988
1989 sample_interval64 = now - ls->load_update_last;
1990 ls->load_update_last = now;
1991
1992 if ((s64)sample_interval64 < (s64)TICK_NSEC)
1993 sample_interval64 = TICK_NSEC;
1994
1995 if (exec_delta64 > sample_interval64)
1996 exec_delta64 = sample_interval64;
1997
1998 idle_delta64 = sample_interval64 - exec_delta64;
1999
2000 tmp64 = div64_64(SCHED_LOAD_SCALE * exec_delta64, fair_delta64);
2001 tmp64 = div64_64(tmp64 * exec_delta64, sample_interval64);
2002
2003 this_load = (unsigned long)tmp64;
2004
2005do_avg:
2006
2007 /* Update our load: */
2008 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2009 unsigned long old_load, new_load;
2010
2011 /* scale is effectively 1 << i now, and >> i divides by scale */
2012
2013 old_load = this_rq->cpu_load[i];
2014 new_load = this_load;
2015
2016 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2017 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002018}
2019
Ingo Molnardd41f592007-07-09 18:51:59 +02002020#ifdef CONFIG_SMP
2021
Ingo Molnar48f24c42006-07-03 00:25:40 -07002022/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 * double_rq_lock - safely lock two runqueues
2024 *
2025 * Note this does not disable interrupts like task_rq_lock,
2026 * you need to do so manually before calling.
2027 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002028static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 __acquires(rq1->lock)
2030 __acquires(rq2->lock)
2031{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002032 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 if (rq1 == rq2) {
2034 spin_lock(&rq1->lock);
2035 __acquire(rq2->lock); /* Fake it out ;) */
2036 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002037 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 spin_lock(&rq1->lock);
2039 spin_lock(&rq2->lock);
2040 } else {
2041 spin_lock(&rq2->lock);
2042 spin_lock(&rq1->lock);
2043 }
2044 }
2045}
2046
2047/*
2048 * double_rq_unlock - safely unlock two runqueues
2049 *
2050 * Note this does not restore interrupts like task_rq_unlock,
2051 * you need to do so manually after calling.
2052 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002053static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 __releases(rq1->lock)
2055 __releases(rq2->lock)
2056{
2057 spin_unlock(&rq1->lock);
2058 if (rq1 != rq2)
2059 spin_unlock(&rq2->lock);
2060 else
2061 __release(rq2->lock);
2062}
2063
2064/*
2065 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2066 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002067static void double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 __releases(this_rq->lock)
2069 __acquires(busiest->lock)
2070 __acquires(this_rq->lock)
2071{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002072 if (unlikely(!irqs_disabled())) {
2073 /* printk() doesn't work good under rq->lock */
2074 spin_unlock(&this_rq->lock);
2075 BUG_ON(1);
2076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002078 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 spin_unlock(&this_rq->lock);
2080 spin_lock(&busiest->lock);
2081 spin_lock(&this_rq->lock);
2082 } else
2083 spin_lock(&busiest->lock);
2084 }
2085}
2086
2087/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 * If dest_cpu is allowed for this process, migrate the task to it.
2089 * This is accomplished by forcing the cpu_allowed mask to only
2090 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
2091 * the cpu_allowed mask is restored.
2092 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002093static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002095 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002097 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099 rq = task_rq_lock(p, &flags);
2100 if (!cpu_isset(dest_cpu, p->cpus_allowed)
2101 || unlikely(cpu_is_offline(dest_cpu)))
2102 goto out;
2103
2104 /* force the process onto the specified CPU */
2105 if (migrate_task(p, dest_cpu, &req)) {
2106 /* Need to wait for migration thread (might exit: take ref). */
2107 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07002108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 get_task_struct(mt);
2110 task_rq_unlock(rq, &flags);
2111 wake_up_process(mt);
2112 put_task_struct(mt);
2113 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07002114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 return;
2116 }
2117out:
2118 task_rq_unlock(rq, &flags);
2119}
2120
2121/*
Nick Piggin476d1392005-06-25 14:57:29 -07002122 * sched_exec - execve() is a valuable balancing opportunity, because at
2123 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 */
2125void sched_exec(void)
2126{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002128 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002130 if (new_cpu != this_cpu)
2131 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132}
2133
2134/*
2135 * pull_task - move a task from a remote runqueue to the local runqueue.
2136 * Both runqueues must be locked.
2137 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002138static void pull_task(struct rq *src_rq, struct task_struct *p,
2139 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
Ingo Molnara8e504d2007-08-09 11:16:47 +02002141 update_rq_clock(src_rq);
2142 deactivate_task(src_rq, p, 0, src_rq->clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002144 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 /*
2146 * Note that idle threads have a prio of MAX_PRIO, for this test
2147 * to be always true for them.
2148 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002149 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
2152/*
2153 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
2154 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002155static
Ingo Molnar70b97a72006-07-03 00:25:42 -07002156int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002157 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002158 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
2160 /*
2161 * We do not migrate tasks that are:
2162 * 1) running (obviously), or
2163 * 2) cannot be migrated to this CPU due to cpus_allowed, or
2164 * 3) are cache-hot on their current CPU.
2165 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 if (!cpu_isset(this_cpu, p->cpus_allowed))
2167 return 0;
Nick Piggin81026792005-06-25 14:57:07 -07002168 *all_pinned = 0;
2169
2170 if (task_running(rq, p))
2171 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002174 * Aggressive migration if too many balance attempts have failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002176 if (sd->nr_balance_failed > sd->cache_nice_tries)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 return 1;
2178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 return 1;
2180}
2181
Ingo Molnardd41f592007-07-09 18:51:59 +02002182static int balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
2183 unsigned long max_nr_move, unsigned long max_load_move,
2184 struct sched_domain *sd, enum cpu_idle_type idle,
2185 int *all_pinned, unsigned long *load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002186 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02002187{
2188 int pulled = 0, pinned = 0, skip_for_load;
2189 struct task_struct *p;
2190 long rem_load_move = max_load_move;
2191
2192 if (max_nr_move == 0 || max_load_move == 0)
2193 goto out;
2194
2195 pinned = 1;
2196
2197 /*
2198 * Start the load-balancing iterator:
2199 */
2200 p = iterator->start(iterator->arg);
2201next:
2202 if (!p)
2203 goto out;
2204 /*
2205 * To help distribute high priority tasks accross CPUs we don't
2206 * skip a task if it will be the highest priority task (i.e. smallest
2207 * prio value) on its new queue regardless of its load weight
2208 */
2209 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
2210 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002211 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02002212 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002213 p = iterator->next(iterator->arg);
2214 goto next;
2215 }
2216
2217 pull_task(busiest, p, this_rq, this_cpu);
2218 pulled++;
2219 rem_load_move -= p->se.load.weight;
2220
2221 /*
2222 * We only want to steal up to the prescribed number of tasks
2223 * and the prescribed amount of weighted load.
2224 */
2225 if (pulled < max_nr_move && rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002226 if (p->prio < *this_best_prio)
2227 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02002228 p = iterator->next(iterator->arg);
2229 goto next;
2230 }
2231out:
2232 /*
2233 * Right now, this is the only place pull_task() is called,
2234 * so we can safely collect pull_task() stats here rather than
2235 * inside pull_task().
2236 */
2237 schedstat_add(sd, lb_gained[idle], pulled);
2238
2239 if (all_pinned)
2240 *all_pinned = pinned;
2241 *load_moved = max_load_move - rem_load_move;
2242 return pulled;
2243}
Ingo Molnar48f24c42006-07-03 00:25:40 -07002244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245/*
Peter Williams43010652007-08-09 11:16:46 +02002246 * move_tasks tries to move up to max_load_move weighted load from busiest to
2247 * this_rq, as part of a balancing operation within domain "sd".
2248 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 *
2250 * Called with both runqueues locked.
2251 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002252static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02002253 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002254 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07002255 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
Ingo Molnardd41f592007-07-09 18:51:59 +02002257 struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02002258 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002259 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Ingo Molnardd41f592007-07-09 18:51:59 +02002261 do {
Peter Williams43010652007-08-09 11:16:46 +02002262 total_load_moved +=
2263 class->load_balance(this_rq, this_cpu, busiest,
2264 ULONG_MAX, max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002265 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02002266 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02002267 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Peter Williams43010652007-08-09 11:16:46 +02002269 return total_load_moved > 0;
2270}
2271
2272/*
2273 * move_one_task tries to move exactly one task from busiest to this_rq, as
2274 * part of active balancing operations within "domain".
2275 * Returns 1 if successful and 0 otherwise.
2276 *
2277 * Called with both runqueues locked.
2278 */
2279static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2280 struct sched_domain *sd, enum cpu_idle_type idle)
2281{
2282 struct sched_class *class;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002283 int this_best_prio = MAX_PRIO;
Peter Williams43010652007-08-09 11:16:46 +02002284
2285 for (class = sched_class_highest; class; class = class->next)
2286 if (class->load_balance(this_rq, this_cpu, busiest,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002287 1, ULONG_MAX, sd, idle, NULL,
2288 &this_best_prio))
Peter Williams43010652007-08-09 11:16:46 +02002289 return 1;
2290
2291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292}
2293
2294/*
2295 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07002296 * domain. It calculates and returns the amount of weighted load which
2297 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 */
2299static struct sched_group *
2300find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02002301 unsigned long *imbalance, enum cpu_idle_type idle,
2302 int *sd_idle, cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303{
2304 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
2305 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002306 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07002307 unsigned long busiest_load_per_task, busiest_nr_running;
2308 unsigned long this_load_per_task, this_nr_running;
Nick Piggin78979862005-06-25 14:57:13 -07002309 int load_idx;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002310#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2311 int power_savings_balance = 1;
2312 unsigned long leader_nr_running = 0, min_load_per_task = 0;
2313 unsigned long min_nr_running = ULONG_MAX;
2314 struct sched_group *group_min = NULL, *group_leader = NULL;
2315#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002318 busiest_load_per_task = busiest_nr_running = 0;
2319 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002320 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002321 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002322 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002323 load_idx = sd->newidle_idx;
2324 else
2325 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 do {
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002328 unsigned long load, group_capacity;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 int local_group;
2330 int i;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002331 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002332 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
2334 local_group = cpu_isset(this_cpu, group->cpumask);
2335
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002336 if (local_group)
2337 balance_cpu = first_cpu(group->cpumask);
2338
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07002340 sum_weighted_load = sum_nr_running = avg_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
2342 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002343 struct rq *rq;
2344
2345 if (!cpu_isset(i, *cpus))
2346 continue;
2347
2348 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07002349
Suresh Siddha9439aab2007-07-19 21:28:35 +02002350 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07002351 *sd_idle = 0;
2352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002354 if (local_group) {
2355 if (idle_cpu(i) && !first_idle_cpu) {
2356 first_idle_cpu = 1;
2357 balance_cpu = i;
2358 }
2359
Nick Piggina2000572006-02-10 01:51:02 -08002360 load = target_load(i, load_idx);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002361 } else
Nick Piggina2000572006-02-10 01:51:02 -08002362 load = source_load(i, load_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
2364 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07002365 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002366 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
2368
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002369 /*
2370 * First idle cpu or the first cpu(busiest) in this sched group
2371 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02002372 * domains. In the newly idle case, we will allow all the cpu's
2373 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002374 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02002375 if (idle != CPU_NEWLY_IDLE && local_group &&
2376 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002377 *balance = 0;
2378 goto ret;
2379 }
2380
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07002382 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002385 avg_load = sg_div_cpu_power(group,
2386 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Eric Dumazet5517d862007-05-08 00:32:57 -07002388 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002389
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 if (local_group) {
2391 this_load = avg_load;
2392 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002393 this_nr_running = sum_nr_running;
2394 this_load_per_task = sum_weighted_load;
2395 } else if (avg_load > max_load &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002396 sum_nr_running > group_capacity) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 max_load = avg_load;
2398 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002399 busiest_nr_running = sum_nr_running;
2400 busiest_load_per_task = sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002402
2403#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2404 /*
2405 * Busy processors will not participate in power savings
2406 * balance.
2407 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002408 if (idle == CPU_NOT_IDLE ||
2409 !(sd->flags & SD_POWERSAVINGS_BALANCE))
2410 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002411
2412 /*
2413 * If the local group is idle or completely loaded
2414 * no need to do power savings balance at this domain
2415 */
2416 if (local_group && (this_nr_running >= group_capacity ||
2417 !this_nr_running))
2418 power_savings_balance = 0;
2419
Ingo Molnardd41f592007-07-09 18:51:59 +02002420 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002421 * If a group is already running at full capacity or idle,
2422 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02002423 */
2424 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002425 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02002426 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002427
Ingo Molnardd41f592007-07-09 18:51:59 +02002428 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002429 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002430 * This is the group from where we need to pick up the load
2431 * for saving power
2432 */
2433 if ((sum_nr_running < min_nr_running) ||
2434 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002435 first_cpu(group->cpumask) <
2436 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002437 group_min = group;
2438 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002439 min_load_per_task = sum_weighted_load /
2440 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002441 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002442
Ingo Molnardd41f592007-07-09 18:51:59 +02002443 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002444 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02002445 * capacity but still has some space to pick up some load
2446 * from other group and save more power
2447 */
2448 if (sum_nr_running <= group_capacity - 1) {
2449 if (sum_nr_running > leader_nr_running ||
2450 (sum_nr_running == leader_nr_running &&
2451 first_cpu(group->cpumask) >
2452 first_cpu(group_leader->cpumask))) {
2453 group_leader = group;
2454 leader_nr_running = sum_nr_running;
2455 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002456 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002457group_next:
2458#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 group = group->next;
2460 } while (group != sd->groups);
2461
Peter Williams2dd73a42006-06-27 02:54:34 -07002462 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 goto out_balanced;
2464
2465 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
2466
2467 if (this_load >= avg_load ||
2468 100*max_load <= sd->imbalance_pct*this_load)
2469 goto out_balanced;
2470
Peter Williams2dd73a42006-06-27 02:54:34 -07002471 busiest_load_per_task /= busiest_nr_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 /*
2473 * We're trying to get all the cpus to the average_load, so we don't
2474 * want to push ourselves above the average load, nor do we wish to
2475 * reduce the max loaded cpu below the average load, as either of these
2476 * actions would just result in more rebalancing later, and ping-pong
2477 * tasks around. Thus we look for the minimum possible imbalance.
2478 * Negative imbalances (*we* are more loaded than anyone else) will
2479 * be counted as no imbalance for these purposes -- we can't fix that
2480 * by pulling tasks to us. Be careful of negative numbers as they'll
2481 * appear as very large values with unsigned longs.
2482 */
Peter Williams2dd73a42006-06-27 02:54:34 -07002483 if (max_load <= busiest_load_per_task)
2484 goto out_balanced;
2485
2486 /*
2487 * In the presence of smp nice balancing, certain scenarios can have
2488 * max load less than avg load(as we skip the groups at or below
2489 * its cpu_power, while calculating max_load..)
2490 */
2491 if (max_load < avg_load) {
2492 *imbalance = 0;
2493 goto small_imbalance;
2494 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002495
2496 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07002497 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07002500 *imbalance = min(max_pull * busiest->__cpu_power,
2501 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 / SCHED_LOAD_SCALE;
2503
Peter Williams2dd73a42006-06-27 02:54:34 -07002504 /*
2505 * if *imbalance is less than the average load per runnable task
2506 * there is no gaurantee that any tasks will be moved so we'll have
2507 * a think about bumping its value to force at least one task to be
2508 * moved
2509 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002510 if (*imbalance + SCHED_LOAD_SCALE_FUZZ < busiest_load_per_task/2) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07002511 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07002512 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Peter Williams2dd73a42006-06-27 02:54:34 -07002514small_imbalance:
2515 pwr_move = pwr_now = 0;
2516 imbn = 2;
2517 if (this_nr_running) {
2518 this_load_per_task /= this_nr_running;
2519 if (busiest_load_per_task > this_load_per_task)
2520 imbn = 1;
2521 } else
2522 this_load_per_task = SCHED_LOAD_SCALE;
2523
Ingo Molnardd41f592007-07-09 18:51:59 +02002524 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
2525 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002526 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 return busiest;
2528 }
2529
2530 /*
2531 * OK, we don't have enough imbalance to justify moving tasks,
2532 * however we may be able to increase total CPU power used by
2533 * moving them.
2534 */
2535
Eric Dumazet5517d862007-05-08 00:32:57 -07002536 pwr_now += busiest->__cpu_power *
2537 min(busiest_load_per_task, max_load);
2538 pwr_now += this->__cpu_power *
2539 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 pwr_now /= SCHED_LOAD_SCALE;
2541
2542 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07002543 tmp = sg_div_cpu_power(busiest,
2544 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07002546 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07002547 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
2549 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07002550 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08002551 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07002552 tmp = sg_div_cpu_power(this,
2553 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 else
Eric Dumazet5517d862007-05-08 00:32:57 -07002555 tmp = sg_div_cpu_power(this,
2556 busiest_load_per_task * SCHED_LOAD_SCALE);
2557 pwr_move += this->__cpu_power *
2558 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 pwr_move /= SCHED_LOAD_SCALE;
2560
2561 /* Move if we gain throughput */
2562 if (pwr_move <= pwr_now)
2563 goto out_balanced;
2564
Peter Williams2dd73a42006-06-27 02:54:34 -07002565 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 }
2567
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 return busiest;
2569
2570out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002571#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002572 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002573 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002575 if (this == group_leader && group_leader != group_min) {
2576 *imbalance = min_load_per_task;
2577 return group_min;
2578 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002579#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002580ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 *imbalance = 0;
2582 return NULL;
2583}
2584
2585/*
2586 * find_busiest_queue - find the busiest runqueue among the cpus in group.
2587 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002588static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002589find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002590 unsigned long imbalance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002592 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07002593 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 int i;
2595
2596 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002597 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002598
2599 if (!cpu_isset(i, *cpus))
2600 continue;
2601
Ingo Molnar48f24c42006-07-03 00:25:40 -07002602 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02002603 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Ingo Molnardd41f592007-07-09 18:51:59 +02002605 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07002606 continue;
2607
Ingo Molnardd41f592007-07-09 18:51:59 +02002608 if (wl > max_load) {
2609 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07002610 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 }
2612 }
2613
2614 return busiest;
2615}
2616
2617/*
Nick Piggin77391d72005-06-25 14:57:30 -07002618 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
2619 * so long as it is large enough.
2620 */
2621#define MAX_PINNED_INTERVAL 512
2622
2623/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 * Check this_cpu to ensure it is balanced within domain. Attempt to move
2625 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002627static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002628 struct sched_domain *sd, enum cpu_idle_type idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002629 int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
Peter Williams43010652007-08-09 11:16:46 +02002631 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002634 struct rq *busiest;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002635 cpumask_t cpus = CPU_MASK_ALL;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002636 unsigned long flags;
Nick Piggin5969fe02005-09-10 00:26:19 -07002637
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002638 /*
2639 * When power savings policy is enabled for the parent domain, idle
2640 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02002641 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002642 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002643 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002644 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002645 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002646 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 schedstat_inc(sd, lb_cnt[idle]);
2649
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002650redo:
2651 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002652 &cpus, balance);
2653
Chen, Kenneth W06066712006-12-10 02:20:35 -08002654 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002655 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002656
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 if (!group) {
2658 schedstat_inc(sd, lb_nobusyg[idle]);
2659 goto out_balanced;
2660 }
2661
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002662 busiest = find_busiest_queue(group, idle, imbalance, &cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 if (!busiest) {
2664 schedstat_inc(sd, lb_nobusyq[idle]);
2665 goto out_balanced;
2666 }
2667
Nick Piggindb935db2005-06-25 14:57:11 -07002668 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
2670 schedstat_add(sd, lb_imbalance[idle], imbalance);
2671
Peter Williams43010652007-08-09 11:16:46 +02002672 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 if (busiest->nr_running > 1) {
2674 /*
2675 * Attempt to move tasks. If find_busiest_group has found
2676 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02002677 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 * correctly treated as an imbalance.
2679 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002680 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07002681 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02002682 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07002683 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07002684 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002685 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07002686
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002687 /*
2688 * some other cpu did the load balance for us.
2689 */
Peter Williams43010652007-08-09 11:16:46 +02002690 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002691 resched_cpu(this_cpu);
2692
Nick Piggin81026792005-06-25 14:57:07 -07002693 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002694 if (unlikely(all_pinned)) {
2695 cpu_clear(cpu_of(busiest), cpus);
2696 if (!cpus_empty(cpus))
2697 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07002698 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 }
Nick Piggin81026792005-06-25 14:57:07 -07002701
Peter Williams43010652007-08-09 11:16:46 +02002702 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 schedstat_inc(sd, lb_failed[idle]);
2704 sd->nr_balance_failed++;
2705
2706 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002708 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07002709
2710 /* don't kick the migration_thread, if the curr
2711 * task on busiest cpu can't be moved to this_cpu
2712 */
2713 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002714 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07002715 all_pinned = 1;
2716 goto out_one_pinned;
2717 }
2718
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 if (!busiest->active_balance) {
2720 busiest->active_balance = 1;
2721 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07002722 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002724 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07002725 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 wake_up_process(busiest->migration_thread);
2727
2728 /*
2729 * We've kicked active balancing, reset the failure
2730 * counter.
2731 */
Nick Piggin39507452005-06-25 14:57:09 -07002732 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 }
Nick Piggin81026792005-06-25 14:57:07 -07002734 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 sd->nr_balance_failed = 0;
2736
Nick Piggin81026792005-06-25 14:57:07 -07002737 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 /* We were unbalanced, so reset the balancing interval */
2739 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07002740 } else {
2741 /*
2742 * If we've begun active balancing, start to back off. This
2743 * case may not be covered by the all_pinned logic if there
2744 * is only 1 task on the busy runqueue (because we don't call
2745 * move_tasks).
2746 */
2747 if (sd->balance_interval < sd->max_interval)
2748 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 }
2750
Peter Williams43010652007-08-09 11:16:46 +02002751 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002752 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002753 return -1;
Peter Williams43010652007-08-09 11:16:46 +02002754 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
2756out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 schedstat_inc(sd, lb_balanced[idle]);
2758
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002759 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07002760
2761out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07002763 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
2764 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 sd->balance_interval *= 2;
2766
Ingo Molnar48f24c42006-07-03 00:25:40 -07002767 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002768 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002769 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 return 0;
2771}
2772
2773/*
2774 * Check this_cpu to ensure it is balanced within domain. Attempt to move
2775 * tasks if there is an imbalance.
2776 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002777 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 * this_rq is locked.
2779 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07002780static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07002781load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782{
2783 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002784 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02002786 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07002787 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02002788 int all_pinned = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002789 cpumask_t cpus = CPU_MASK_ALL;
Nick Piggin5969fe02005-09-10 00:26:19 -07002790
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002791 /*
2792 * When power savings policy is enabled for the parent domain, idle
2793 * sibling can pick up load irrespective of busy siblings. In this case,
2794 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002795 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002796 */
2797 if (sd->flags & SD_SHARE_CPUPOWER &&
2798 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002799 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002801 schedstat_inc(sd, lb_cnt[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002802redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002803 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002804 &sd_idle, &cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002806 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002807 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 }
2809
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002810 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002811 &cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07002812 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002813 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002814 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 }
2816
Nick Piggindb935db2005-06-25 14:57:11 -07002817 BUG_ON(busiest == this_rq);
2818
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002819 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07002820
Peter Williams43010652007-08-09 11:16:46 +02002821 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07002822 if (busiest->nr_running > 1) {
2823 /* Attempt to move tasks */
2824 double_lock_balance(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02002825 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02002826 imbalance, sd, CPU_NEWLY_IDLE,
2827 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07002828 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002829
Suresh Siddha969bb4e2007-07-19 21:28:35 +02002830 if (unlikely(all_pinned)) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002831 cpu_clear(cpu_of(busiest), cpus);
2832 if (!cpus_empty(cpus))
2833 goto redo;
2834 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07002835 }
2836
Peter Williams43010652007-08-09 11:16:46 +02002837 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002838 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002839 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
2840 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002841 return -1;
2842 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002843 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
Peter Williams43010652007-08-09 11:16:46 +02002845 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002846
2847out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002848 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07002849 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002850 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002851 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002852 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07002853
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002854 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855}
2856
2857/*
2858 * idle_balance is called by schedule() if this_cpu is about to become
2859 * idle. Attempts to pull tasks from other CPUs.
2860 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002861static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862{
2863 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02002864 int pulled_task = -1;
2865 unsigned long next_balance = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
2867 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07002868 unsigned long interval;
2869
2870 if (!(sd->flags & SD_LOAD_BALANCE))
2871 continue;
2872
2873 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002874 /* If we've pulled tasks over stop searching: */
Christoph Lameter1bd77f22006-12-10 02:20:27 -08002875 pulled_task = load_balance_newidle(this_cpu,
Christoph Lameter92c4ca52007-06-23 17:16:33 -07002876 this_rq, sd);
2877
2878 interval = msecs_to_jiffies(sd->balance_interval);
2879 if (time_after(next_balance, sd->last_balance + interval))
2880 next_balance = sd->last_balance + interval;
2881 if (pulled_task)
2882 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002884 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08002885 /*
2886 * We are going idle. next_balance may be set based on
2887 * a busy processor. So reset next_balance.
2888 */
2889 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02002890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891}
2892
2893/*
2894 * active_load_balance is run by migration threads. It pushes running tasks
2895 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
2896 * running on each physical CPU where possible, and avoids physical /
2897 * logical imbalances.
2898 *
2899 * Called with busiest_rq locked.
2900 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002901static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
Nick Piggin39507452005-06-25 14:57:09 -07002903 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002904 struct sched_domain *sd;
2905 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07002906
Ingo Molnar48f24c42006-07-03 00:25:40 -07002907 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07002908 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07002909 return;
2910
2911 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
2913 /*
Nick Piggin39507452005-06-25 14:57:09 -07002914 * This condition is "impossible", if it occurs
2915 * we need to fix it. Originally reported by
2916 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 */
Nick Piggin39507452005-06-25 14:57:09 -07002918 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Nick Piggin39507452005-06-25 14:57:09 -07002920 /* move a task from busiest_rq to target_rq */
2921 double_lock_balance(busiest_rq, target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Nick Piggin39507452005-06-25 14:57:09 -07002923 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002924 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07002925 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07002926 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07002927 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Ingo Molnar48f24c42006-07-03 00:25:40 -07002930 if (likely(sd)) {
2931 schedstat_inc(sd, alb_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Peter Williams43010652007-08-09 11:16:46 +02002933 if (move_one_task(target_rq, target_cpu, busiest_rq,
2934 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07002935 schedstat_inc(sd, alb_pushed);
2936 else
2937 schedstat_inc(sd, alb_failed);
2938 }
Nick Piggin39507452005-06-25 14:57:09 -07002939 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940}
2941
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002942#ifdef CONFIG_NO_HZ
2943static struct {
2944 atomic_t load_balancer;
2945 cpumask_t cpu_mask;
2946} nohz ____cacheline_aligned = {
2947 .load_balancer = ATOMIC_INIT(-1),
2948 .cpu_mask = CPU_MASK_NONE,
2949};
2950
Christoph Lameter7835b982006-12-10 02:20:22 -08002951/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002952 * This routine will try to nominate the ilb (idle load balancing)
2953 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
2954 * load balancing on behalf of all those cpus. If all the cpus in the system
2955 * go into this tickless mode, then there will be no ilb owner (as there is
2956 * no need for one) and all the cpus will sleep till the next wakeup event
2957 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08002958 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002959 * For the ilb owner, tick is not stopped. And this tick will be used
2960 * for idle load balancing. ilb owner will still be part of
2961 * nohz.cpu_mask..
2962 *
2963 * While stopping the tick, this cpu will become the ilb owner if there
2964 * is no other owner. And will be the owner till that cpu becomes busy
2965 * or if all cpus in the system stop their ticks at which point
2966 * there is no need for ilb owner.
2967 *
2968 * When the ilb owner becomes busy, it nominates another owner, during the
2969 * next busy scheduler_tick()
2970 */
2971int select_nohz_load_balancer(int stop_tick)
2972{
2973 int cpu = smp_processor_id();
2974
2975 if (stop_tick) {
2976 cpu_set(cpu, nohz.cpu_mask);
2977 cpu_rq(cpu)->in_nohz_recently = 1;
2978
2979 /*
2980 * If we are going offline and still the leader, give up!
2981 */
2982 if (cpu_is_offline(cpu) &&
2983 atomic_read(&nohz.load_balancer) == cpu) {
2984 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
2985 BUG();
2986 return 0;
2987 }
2988
2989 /* time for ilb owner also to sleep */
2990 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
2991 if (atomic_read(&nohz.load_balancer) == cpu)
2992 atomic_set(&nohz.load_balancer, -1);
2993 return 0;
2994 }
2995
2996 if (atomic_read(&nohz.load_balancer) == -1) {
2997 /* make me the ilb owner */
2998 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
2999 return 1;
3000 } else if (atomic_read(&nohz.load_balancer) == cpu)
3001 return 1;
3002 } else {
3003 if (!cpu_isset(cpu, nohz.cpu_mask))
3004 return 0;
3005
3006 cpu_clear(cpu, nohz.cpu_mask);
3007
3008 if (atomic_read(&nohz.load_balancer) == cpu)
3009 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3010 BUG();
3011 }
3012 return 0;
3013}
3014#endif
3015
3016static DEFINE_SPINLOCK(balancing);
3017
3018/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003019 * It checks each scheduling domain to see if it is due to be balanced,
3020 * and initiates a balancing operation if so.
3021 *
3022 * Balancing parameters are set up in arch_init_sched_domains.
3023 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003024static inline void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08003025{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003026 int balance = 1;
3027 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08003028 unsigned long interval;
3029 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003030 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08003031 unsigned long next_balance = jiffies + 60*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003033 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 if (!(sd->flags & SD_LOAD_BALANCE))
3035 continue;
3036
3037 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003038 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 interval *= sd->busy_factor;
3040
3041 /* scale ms to jiffies */
3042 interval = msecs_to_jiffies(interval);
3043 if (unlikely(!interval))
3044 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003045 if (interval > HZ*NR_CPUS/10)
3046 interval = HZ*NR_CPUS/10;
3047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
Christoph Lameter08c183f2006-12-10 02:20:29 -08003049 if (sd->flags & SD_SERIALIZE) {
3050 if (!spin_trylock(&balancing))
3051 goto out;
3052 }
3053
Christoph Lameterc9819f42006-12-10 02:20:25 -08003054 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003055 if (load_balance(cpu, rq, sd, idle, &balance)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003056 /*
3057 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07003058 * longer idle, or one of our SMT siblings is
3059 * not idle.
3060 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003061 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003063 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08003065 if (sd->flags & SD_SERIALIZE)
3066 spin_unlock(&balancing);
3067out:
Christoph Lameterc9819f42006-12-10 02:20:25 -08003068 if (time_after(next_balance, sd->last_balance + interval))
3069 next_balance = sd->last_balance + interval;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003070
3071 /*
3072 * Stop the load balance at this level. There is another
3073 * CPU in our sched group which is doing load balancing more
3074 * actively.
3075 */
3076 if (!balance)
3077 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 }
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003079 rq->next_balance = next_balance;
3080}
3081
3082/*
3083 * run_rebalance_domains is triggered when needed from the scheduler tick.
3084 * In CONFIG_NO_HZ case, the idle load balance owner will do the
3085 * rebalancing for all the cpus for whom scheduler ticks are stopped.
3086 */
3087static void run_rebalance_domains(struct softirq_action *h)
3088{
Ingo Molnardd41f592007-07-09 18:51:59 +02003089 int this_cpu = smp_processor_id();
3090 struct rq *this_rq = cpu_rq(this_cpu);
3091 enum cpu_idle_type idle = this_rq->idle_at_tick ?
3092 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003093
Ingo Molnardd41f592007-07-09 18:51:59 +02003094 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003095
3096#ifdef CONFIG_NO_HZ
3097 /*
3098 * If this cpu is the owner for idle load balancing, then do the
3099 * balancing on behalf of the other idle cpus whose ticks are
3100 * stopped.
3101 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003102 if (this_rq->idle_at_tick &&
3103 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003104 cpumask_t cpus = nohz.cpu_mask;
3105 struct rq *rq;
3106 int balance_cpu;
3107
Ingo Molnardd41f592007-07-09 18:51:59 +02003108 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003109 for_each_cpu_mask(balance_cpu, cpus) {
3110 /*
3111 * If this cpu gets work to do, stop the load balancing
3112 * work being done for other cpus. Next load
3113 * balancing owner will pick it up.
3114 */
3115 if (need_resched())
3116 break;
3117
Ingo Molnardd41f592007-07-09 18:51:59 +02003118 rebalance_domains(balance_cpu, SCHED_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003119
3120 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003121 if (time_after(this_rq->next_balance, rq->next_balance))
3122 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003123 }
3124 }
3125#endif
3126}
3127
3128/*
3129 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
3130 *
3131 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
3132 * idle load balancing owner or decide to stop the periodic load balancing,
3133 * if the whole system is idle.
3134 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003135static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003136{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003137#ifdef CONFIG_NO_HZ
3138 /*
3139 * If we were in the nohz mode recently and busy at the current
3140 * scheduler tick, then check if we need to nominate new idle
3141 * load balancer.
3142 */
3143 if (rq->in_nohz_recently && !rq->idle_at_tick) {
3144 rq->in_nohz_recently = 0;
3145
3146 if (atomic_read(&nohz.load_balancer) == cpu) {
3147 cpu_clear(cpu, nohz.cpu_mask);
3148 atomic_set(&nohz.load_balancer, -1);
3149 }
3150
3151 if (atomic_read(&nohz.load_balancer) == -1) {
3152 /*
3153 * simple selection for now: Nominate the
3154 * first cpu in the nohz list to be the next
3155 * ilb owner.
3156 *
3157 * TBD: Traverse the sched domains and nominate
3158 * the nearest cpu in the nohz.cpu_mask.
3159 */
3160 int ilb = first_cpu(nohz.cpu_mask);
3161
3162 if (ilb != NR_CPUS)
3163 resched_cpu(ilb);
3164 }
3165 }
3166
3167 /*
3168 * If this cpu is idle and doing idle load balancing for all the
3169 * cpus with ticks stopped, is it time for that to stop?
3170 */
3171 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
3172 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3173 resched_cpu(cpu);
3174 return;
3175 }
3176
3177 /*
3178 * If this cpu is idle and the idle load balancing is done by
3179 * someone else, then no need raise the SCHED_SOFTIRQ
3180 */
3181 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
3182 cpu_isset(cpu, nohz.cpu_mask))
3183 return;
3184#endif
3185 if (time_after_eq(jiffies, rq->next_balance))
3186 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187}
Ingo Molnardd41f592007-07-09 18:51:59 +02003188
3189#else /* CONFIG_SMP */
3190
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191/*
3192 * on UP we do not need to balance between CPUs:
3193 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003194static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195{
3196}
Ingo Molnardd41f592007-07-09 18:51:59 +02003197
3198/* Avoid "used but not defined" warning on UP */
3199static int balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3200 unsigned long max_nr_move, unsigned long max_load_move,
3201 struct sched_domain *sd, enum cpu_idle_type idle,
3202 int *all_pinned, unsigned long *load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003203 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02003204{
3205 *load_moved = 0;
3206
3207 return 0;
3208}
3209
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210#endif
3211
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212DEFINE_PER_CPU(struct kernel_stat, kstat);
3213
3214EXPORT_PER_CPU_SYMBOL(kstat);
3215
3216/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02003217 * Return p->sum_exec_runtime plus any more ns on the sched_clock
3218 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02003220unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003223 u64 ns, delta_exec;
3224 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003225
Ingo Molnar41b86e92007-07-09 18:51:58 +02003226 rq = task_rq_lock(p, &flags);
3227 ns = p->se.sum_exec_runtime;
3228 if (rq->curr == p) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02003229 update_rq_clock(rq);
3230 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003231 if ((s64)delta_exec > 0)
3232 ns += delta_exec;
3233 }
3234 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003235
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 return ns;
3237}
3238
3239/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 * Account user cpu time to a process.
3241 * @p: the process that the cpu time gets accounted to
3242 * @hardirq_offset: the offset to subtract from hardirq_count()
3243 * @cputime: the cpu time spent in user space since the last update
3244 */
3245void account_user_time(struct task_struct *p, cputime_t cputime)
3246{
3247 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3248 cputime64_t tmp;
3249
3250 p->utime = cputime_add(p->utime, cputime);
3251
3252 /* Add user time to cpustat. */
3253 tmp = cputime_to_cputime64(cputime);
3254 if (TASK_NICE(p) > 0)
3255 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3256 else
3257 cpustat->user = cputime64_add(cpustat->user, tmp);
3258}
3259
3260/*
3261 * Account system cpu time to a process.
3262 * @p: the process that the cpu time gets accounted to
3263 * @hardirq_offset: the offset to subtract from hardirq_count()
3264 * @cputime: the cpu time spent in kernel space since the last update
3265 */
3266void account_system_time(struct task_struct *p, int hardirq_offset,
3267 cputime_t cputime)
3268{
3269 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003270 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 cputime64_t tmp;
3272
3273 p->stime = cputime_add(p->stime, cputime);
3274
3275 /* Add system time to cpustat. */
3276 tmp = cputime_to_cputime64(cputime);
3277 if (hardirq_count() - hardirq_offset)
3278 cpustat->irq = cputime64_add(cpustat->irq, tmp);
3279 else if (softirq_count())
3280 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
3281 else if (p != rq->idle)
3282 cpustat->system = cputime64_add(cpustat->system, tmp);
3283 else if (atomic_read(&rq->nr_iowait) > 0)
3284 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3285 else
3286 cpustat->idle = cputime64_add(cpustat->idle, tmp);
3287 /* Account for system time used */
3288 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289}
3290
3291/*
3292 * Account for involuntary wait time.
3293 * @p: the process from which the cpu time has been stolen
3294 * @steal: the cpu time spent in involuntary wait
3295 */
3296void account_steal_time(struct task_struct *p, cputime_t steal)
3297{
3298 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3299 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07003300 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
3302 if (p == rq->idle) {
3303 p->stime = cputime_add(p->stime, steal);
3304 if (atomic_read(&rq->nr_iowait) > 0)
3305 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3306 else
3307 cpustat->idle = cputime64_add(cpustat->idle, tmp);
3308 } else
3309 cpustat->steal = cputime64_add(cpustat->steal, tmp);
3310}
3311
Christoph Lameter7835b982006-12-10 02:20:22 -08003312/*
3313 * This function gets called by the timer code, with HZ frequency.
3314 * We call it with interrupts disabled.
3315 *
3316 * It also gets called by the fork code, when changing the parent's
3317 * timeslices.
3318 */
3319void scheduler_tick(void)
3320{
Christoph Lameter7835b982006-12-10 02:20:22 -08003321 int cpu = smp_processor_id();
3322 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003323 struct task_struct *curr = rq->curr;
Christoph Lameter7835b982006-12-10 02:20:22 -08003324
Ingo Molnardd41f592007-07-09 18:51:59 +02003325 spin_lock(&rq->lock);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02003326 update_cpu_load(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003327 if (curr != rq->idle) /* FIXME: needed? */
3328 curr->sched_class->task_tick(rq, curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02003329 spin_unlock(&rq->lock);
3330
Christoph Lametere418e1c2006-12-10 02:20:23 -08003331#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02003332 rq->idle_at_tick = idle_cpu(cpu);
3333 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08003334#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335}
3336
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
3338
3339void fastcall add_preempt_count(int val)
3340{
3341 /*
3342 * Underflow?
3343 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003344 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
3345 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 preempt_count() += val;
3347 /*
3348 * Spinlock count overflowing soon?
3349 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003350 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
3351 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352}
3353EXPORT_SYMBOL(add_preempt_count);
3354
3355void fastcall sub_preempt_count(int val)
3356{
3357 /*
3358 * Underflow?
3359 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003360 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
3361 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 /*
3363 * Is the spinlock portion underflowing?
3364 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003365 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
3366 !(preempt_count() & PREEMPT_MASK)))
3367 return;
3368
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 preempt_count() -= val;
3370}
3371EXPORT_SYMBOL(sub_preempt_count);
3372
3373#endif
3374
3375/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003376 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003378static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379{
Ingo Molnardd41f592007-07-09 18:51:59 +02003380 printk(KERN_ERR "BUG: scheduling while atomic: %s/0x%08x/%d\n",
3381 prev->comm, preempt_count(), prev->pid);
3382 debug_show_held_locks(prev);
3383 if (irqs_disabled())
3384 print_irqtrace_events(prev);
3385 dump_stack();
3386}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
Ingo Molnardd41f592007-07-09 18:51:59 +02003388/*
3389 * Various schedule()-time debugging checks and statistics:
3390 */
3391static inline void schedule_debug(struct task_struct *prev)
3392{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 /*
3394 * Test if we are atomic. Since do_exit() needs to call into
3395 * schedule() atomically, we ignore that path for now.
3396 * Otherwise, whine if we are scheduling when we should not be.
3397 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003398 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
3399 __schedule_bug(prev);
3400
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
3402
Ingo Molnardd41f592007-07-09 18:51:59 +02003403 schedstat_inc(this_rq(), sched_cnt);
3404}
3405
3406/*
3407 * Pick up the highest-prio task:
3408 */
3409static inline struct task_struct *
3410pick_next_task(struct rq *rq, struct task_struct *prev, u64 now)
3411{
3412 struct sched_class *class;
3413 struct task_struct *p;
3414
3415 /*
3416 * Optimization: we know that if all tasks are in
3417 * the fair class we can call that function directly:
3418 */
3419 if (likely(rq->nr_running == rq->cfs.nr_running)) {
3420 p = fair_sched_class.pick_next_task(rq, now);
3421 if (likely(p))
3422 return p;
3423 }
3424
3425 class = sched_class_highest;
3426 for ( ; ; ) {
3427 p = class->pick_next_task(rq, now);
3428 if (p)
3429 return p;
3430 /*
3431 * Will never be NULL as the idle class always
3432 * returns a non-NULL p:
3433 */
3434 class = class->next;
3435 }
3436}
3437
3438/*
3439 * schedule() is the main scheduler function.
3440 */
3441asmlinkage void __sched schedule(void)
3442{
3443 struct task_struct *prev, *next;
3444 long *switch_count;
3445 struct rq *rq;
3446 u64 now;
3447 int cpu;
3448
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449need_resched:
3450 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02003451 cpu = smp_processor_id();
3452 rq = cpu_rq(cpu);
3453 rcu_qsctr_inc(cpu);
3454 prev = rq->curr;
3455 switch_count = &prev->nivcsw;
3456
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 release_kernel_lock(prev);
3458need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Ingo Molnardd41f592007-07-09 18:51:59 +02003460 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
3462 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 clear_tsk_need_resched(prev);
Ingo Molnarc1b3da32007-08-09 11:16:47 +02003464 __update_rq_clock(rq);
3465 now = rq->clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
Ingo Molnardd41f592007-07-09 18:51:59 +02003467 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
3468 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
3469 unlikely(signal_pending(prev)))) {
3470 prev->state = TASK_RUNNING;
3471 } else {
Ingo Molnar8e717b12007-08-09 11:16:46 +02003472 deactivate_task(rq, prev, 1, now);
Ingo Molnardd41f592007-07-09 18:51:59 +02003473 }
3474 switch_count = &prev->nvcsw;
3475 }
3476
3477 if (unlikely(!rq->nr_running))
3478 idle_balance(cpu, rq);
3479
Ingo Molnardd41f592007-07-09 18:51:59 +02003480 prev->sched_class->put_prev_task(rq, prev, now);
3481 next = pick_next_task(rq, prev, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
3483 sched_info_switch(prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02003484
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 rq->nr_switches++;
3487 rq->curr = next;
3488 ++*switch_count;
3489
Ingo Molnardd41f592007-07-09 18:51:59 +02003490 context_switch(rq, prev, next); /* unlocks the rq */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 } else
3492 spin_unlock_irq(&rq->lock);
3493
Ingo Molnardd41f592007-07-09 18:51:59 +02003494 if (unlikely(reacquire_kernel_lock(current) < 0)) {
3495 cpu = smp_processor_id();
3496 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 goto need_resched_nonpreemptible;
Ingo Molnardd41f592007-07-09 18:51:59 +02003498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 preempt_enable_no_resched();
3500 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
3501 goto need_resched;
3502}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503EXPORT_SYMBOL(schedule);
3504
3505#ifdef CONFIG_PREEMPT
3506/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003507 * this is the entry point to schedule() from in-kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 * off of preempt_enable. Kernel preemptions off return from interrupt
3509 * occur there and call schedule directly.
3510 */
3511asmlinkage void __sched preempt_schedule(void)
3512{
3513 struct thread_info *ti = current_thread_info();
3514#ifdef CONFIG_PREEMPT_BKL
3515 struct task_struct *task = current;
3516 int saved_lock_depth;
3517#endif
3518 /*
3519 * If there is a non-zero preempt_count or interrupts are disabled,
3520 * we do not want to preempt the current task. Just return..
3521 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07003522 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 return;
3524
3525need_resched:
3526 add_preempt_count(PREEMPT_ACTIVE);
3527 /*
3528 * We keep the big kernel semaphore locked, but we
3529 * clear ->lock_depth so that schedule() doesnt
3530 * auto-release the semaphore:
3531 */
3532#ifdef CONFIG_PREEMPT_BKL
3533 saved_lock_depth = task->lock_depth;
3534 task->lock_depth = -1;
3535#endif
3536 schedule();
3537#ifdef CONFIG_PREEMPT_BKL
3538 task->lock_depth = saved_lock_depth;
3539#endif
3540 sub_preempt_count(PREEMPT_ACTIVE);
3541
3542 /* we could miss a preemption opportunity between schedule and now */
3543 barrier();
3544 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
3545 goto need_resched;
3546}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547EXPORT_SYMBOL(preempt_schedule);
3548
3549/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003550 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 * off of irq context.
3552 * Note, that this is called and return with irqs disabled. This will
3553 * protect us against recursive calling from irq.
3554 */
3555asmlinkage void __sched preempt_schedule_irq(void)
3556{
3557 struct thread_info *ti = current_thread_info();
3558#ifdef CONFIG_PREEMPT_BKL
3559 struct task_struct *task = current;
3560 int saved_lock_depth;
3561#endif
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003562 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 BUG_ON(ti->preempt_count || !irqs_disabled());
3564
3565need_resched:
3566 add_preempt_count(PREEMPT_ACTIVE);
3567 /*
3568 * We keep the big kernel semaphore locked, but we
3569 * clear ->lock_depth so that schedule() doesnt
3570 * auto-release the semaphore:
3571 */
3572#ifdef CONFIG_PREEMPT_BKL
3573 saved_lock_depth = task->lock_depth;
3574 task->lock_depth = -1;
3575#endif
3576 local_irq_enable();
3577 schedule();
3578 local_irq_disable();
3579#ifdef CONFIG_PREEMPT_BKL
3580 task->lock_depth = saved_lock_depth;
3581#endif
3582 sub_preempt_count(PREEMPT_ACTIVE);
3583
3584 /* we could miss a preemption opportunity between schedule and now */
3585 barrier();
3586 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
3587 goto need_resched;
3588}
3589
3590#endif /* CONFIG_PREEMPT */
3591
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003592int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
3593 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594{
Ingo Molnar48f24c42006-07-03 00:25:40 -07003595 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597EXPORT_SYMBOL(default_wake_function);
3598
3599/*
3600 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
3601 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
3602 * number) then we wake all the non-exclusive tasks and one exclusive task.
3603 *
3604 * There are circumstances in which we can try to wake a task which has already
3605 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
3606 * zero in this (rare) case, and we handle it by continuing to scan the queue.
3607 */
3608static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
3609 int nr_exclusive, int sync, void *key)
3610{
3611 struct list_head *tmp, *next;
3612
3613 list_for_each_safe(tmp, next, &q->task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003614 wait_queue_t *curr = list_entry(tmp, wait_queue_t, task_list);
3615 unsigned flags = curr->flags;
3616
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003618 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 break;
3620 }
3621}
3622
3623/**
3624 * __wake_up - wake up threads blocked on a waitqueue.
3625 * @q: the waitqueue
3626 * @mode: which threads
3627 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07003628 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 */
3630void fastcall __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003631 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632{
3633 unsigned long flags;
3634
3635 spin_lock_irqsave(&q->lock, flags);
3636 __wake_up_common(q, mode, nr_exclusive, 0, key);
3637 spin_unlock_irqrestore(&q->lock, flags);
3638}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639EXPORT_SYMBOL(__wake_up);
3640
3641/*
3642 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
3643 */
3644void fastcall __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
3645{
3646 __wake_up_common(q, mode, 1, 0, NULL);
3647}
3648
3649/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07003650 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 * @q: the waitqueue
3652 * @mode: which threads
3653 * @nr_exclusive: how many wake-one or wake-many threads to wake up
3654 *
3655 * The sync wakeup differs that the waker knows that it will schedule
3656 * away soon, so while the target thread will be woken up, it will not
3657 * be migrated to another CPU - ie. the two threads are 'synchronized'
3658 * with each other. This can prevent needless bouncing between CPUs.
3659 *
3660 * On UP it can prevent extra preemption.
3661 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003662void fastcall
3663__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664{
3665 unsigned long flags;
3666 int sync = 1;
3667
3668 if (unlikely(!q))
3669 return;
3670
3671 if (unlikely(!nr_exclusive))
3672 sync = 0;
3673
3674 spin_lock_irqsave(&q->lock, flags);
3675 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
3676 spin_unlock_irqrestore(&q->lock, flags);
3677}
3678EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
3679
3680void fastcall complete(struct completion *x)
3681{
3682 unsigned long flags;
3683
3684 spin_lock_irqsave(&x->wait.lock, flags);
3685 x->done++;
3686 __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
3687 1, 0, NULL);
3688 spin_unlock_irqrestore(&x->wait.lock, flags);
3689}
3690EXPORT_SYMBOL(complete);
3691
3692void fastcall complete_all(struct completion *x)
3693{
3694 unsigned long flags;
3695
3696 spin_lock_irqsave(&x->wait.lock, flags);
3697 x->done += UINT_MAX/2;
3698 __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
3699 0, 0, NULL);
3700 spin_unlock_irqrestore(&x->wait.lock, flags);
3701}
3702EXPORT_SYMBOL(complete_all);
3703
3704void fastcall __sched wait_for_completion(struct completion *x)
3705{
3706 might_sleep();
Ingo Molnar48f24c42006-07-03 00:25:40 -07003707
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 spin_lock_irq(&x->wait.lock);
3709 if (!x->done) {
3710 DECLARE_WAITQUEUE(wait, current);
3711
3712 wait.flags |= WQ_FLAG_EXCLUSIVE;
3713 __add_wait_queue_tail(&x->wait, &wait);
3714 do {
3715 __set_current_state(TASK_UNINTERRUPTIBLE);
3716 spin_unlock_irq(&x->wait.lock);
3717 schedule();
3718 spin_lock_irq(&x->wait.lock);
3719 } while (!x->done);
3720 __remove_wait_queue(&x->wait, &wait);
3721 }
3722 x->done--;
3723 spin_unlock_irq(&x->wait.lock);
3724}
3725EXPORT_SYMBOL(wait_for_completion);
3726
3727unsigned long fastcall __sched
3728wait_for_completion_timeout(struct completion *x, unsigned long timeout)
3729{
3730 might_sleep();
3731
3732 spin_lock_irq(&x->wait.lock);
3733 if (!x->done) {
3734 DECLARE_WAITQUEUE(wait, current);
3735
3736 wait.flags |= WQ_FLAG_EXCLUSIVE;
3737 __add_wait_queue_tail(&x->wait, &wait);
3738 do {
3739 __set_current_state(TASK_UNINTERRUPTIBLE);
3740 spin_unlock_irq(&x->wait.lock);
3741 timeout = schedule_timeout(timeout);
3742 spin_lock_irq(&x->wait.lock);
3743 if (!timeout) {
3744 __remove_wait_queue(&x->wait, &wait);
3745 goto out;
3746 }
3747 } while (!x->done);
3748 __remove_wait_queue(&x->wait, &wait);
3749 }
3750 x->done--;
3751out:
3752 spin_unlock_irq(&x->wait.lock);
3753 return timeout;
3754}
3755EXPORT_SYMBOL(wait_for_completion_timeout);
3756
3757int fastcall __sched wait_for_completion_interruptible(struct completion *x)
3758{
3759 int ret = 0;
3760
3761 might_sleep();
3762
3763 spin_lock_irq(&x->wait.lock);
3764 if (!x->done) {
3765 DECLARE_WAITQUEUE(wait, current);
3766
3767 wait.flags |= WQ_FLAG_EXCLUSIVE;
3768 __add_wait_queue_tail(&x->wait, &wait);
3769 do {
3770 if (signal_pending(current)) {
3771 ret = -ERESTARTSYS;
3772 __remove_wait_queue(&x->wait, &wait);
3773 goto out;
3774 }
3775 __set_current_state(TASK_INTERRUPTIBLE);
3776 spin_unlock_irq(&x->wait.lock);
3777 schedule();
3778 spin_lock_irq(&x->wait.lock);
3779 } while (!x->done);
3780 __remove_wait_queue(&x->wait, &wait);
3781 }
3782 x->done--;
3783out:
3784 spin_unlock_irq(&x->wait.lock);
3785
3786 return ret;
3787}
3788EXPORT_SYMBOL(wait_for_completion_interruptible);
3789
3790unsigned long fastcall __sched
3791wait_for_completion_interruptible_timeout(struct completion *x,
3792 unsigned long timeout)
3793{
3794 might_sleep();
3795
3796 spin_lock_irq(&x->wait.lock);
3797 if (!x->done) {
3798 DECLARE_WAITQUEUE(wait, current);
3799
3800 wait.flags |= WQ_FLAG_EXCLUSIVE;
3801 __add_wait_queue_tail(&x->wait, &wait);
3802 do {
3803 if (signal_pending(current)) {
3804 timeout = -ERESTARTSYS;
3805 __remove_wait_queue(&x->wait, &wait);
3806 goto out;
3807 }
3808 __set_current_state(TASK_INTERRUPTIBLE);
3809 spin_unlock_irq(&x->wait.lock);
3810 timeout = schedule_timeout(timeout);
3811 spin_lock_irq(&x->wait.lock);
3812 if (!timeout) {
3813 __remove_wait_queue(&x->wait, &wait);
3814 goto out;
3815 }
3816 } while (!x->done);
3817 __remove_wait_queue(&x->wait, &wait);
3818 }
3819 x->done--;
3820out:
3821 spin_unlock_irq(&x->wait.lock);
3822 return timeout;
3823}
3824EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
3825
Ingo Molnar0fec1712007-07-09 18:52:01 +02003826static inline void
3827sleep_on_head(wait_queue_head_t *q, wait_queue_t *wait, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828{
Ingo Molnar0fec1712007-07-09 18:52:01 +02003829 spin_lock_irqsave(&q->lock, *flags);
3830 __add_wait_queue(q, wait);
3831 spin_unlock(&q->lock);
3832}
3833
3834static inline void
3835sleep_on_tail(wait_queue_head_t *q, wait_queue_t *wait, unsigned long *flags)
3836{
3837 spin_lock_irq(&q->lock);
3838 __remove_wait_queue(q, wait);
3839 spin_unlock_irqrestore(&q->lock, *flags);
3840}
3841
3842void __sched interruptible_sleep_on(wait_queue_head_t *q)
3843{
3844 unsigned long flags;
3845 wait_queue_t wait;
3846
3847 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848
3849 current->state = TASK_INTERRUPTIBLE;
3850
Ingo Molnar0fec1712007-07-09 18:52:01 +02003851 sleep_on_head(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 schedule();
Ingo Molnar0fec1712007-07-09 18:52:01 +02003853 sleep_on_tail(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855EXPORT_SYMBOL(interruptible_sleep_on);
3856
Ingo Molnar0fec1712007-07-09 18:52:01 +02003857long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003858interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859{
Ingo Molnar0fec1712007-07-09 18:52:01 +02003860 unsigned long flags;
3861 wait_queue_t wait;
3862
3863 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864
3865 current->state = TASK_INTERRUPTIBLE;
3866
Ingo Molnar0fec1712007-07-09 18:52:01 +02003867 sleep_on_head(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 timeout = schedule_timeout(timeout);
Ingo Molnar0fec1712007-07-09 18:52:01 +02003869 sleep_on_tail(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870
3871 return timeout;
3872}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873EXPORT_SYMBOL(interruptible_sleep_on_timeout);
3874
Ingo Molnar0fec1712007-07-09 18:52:01 +02003875void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876{
Ingo Molnar0fec1712007-07-09 18:52:01 +02003877 unsigned long flags;
3878 wait_queue_t wait;
3879
3880 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
3882 current->state = TASK_UNINTERRUPTIBLE;
3883
Ingo Molnar0fec1712007-07-09 18:52:01 +02003884 sleep_on_head(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 schedule();
Ingo Molnar0fec1712007-07-09 18:52:01 +02003886 sleep_on_tail(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888EXPORT_SYMBOL(sleep_on);
3889
Ingo Molnar0fec1712007-07-09 18:52:01 +02003890long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891{
Ingo Molnar0fec1712007-07-09 18:52:01 +02003892 unsigned long flags;
3893 wait_queue_t wait;
3894
3895 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
3897 current->state = TASK_UNINTERRUPTIBLE;
3898
Ingo Molnar0fec1712007-07-09 18:52:01 +02003899 sleep_on_head(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 timeout = schedule_timeout(timeout);
Ingo Molnar0fec1712007-07-09 18:52:01 +02003901 sleep_on_tail(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902
3903 return timeout;
3904}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905EXPORT_SYMBOL(sleep_on_timeout);
3906
Ingo Molnarb29739f2006-06-27 02:54:51 -07003907#ifdef CONFIG_RT_MUTEXES
3908
3909/*
3910 * rt_mutex_setprio - set the current priority of a task
3911 * @p: task
3912 * @prio: prio value (kernel-internal form)
3913 *
3914 * This function changes the 'effective' priority of a task. It does
3915 * not touch ->normal_prio like __setscheduler().
3916 *
3917 * Used by the rt_mutex code to implement priority inheritance logic.
3918 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003919void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07003920{
3921 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02003922 int oldprio, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003923 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02003924 u64 now;
Ingo Molnarb29739f2006-06-27 02:54:51 -07003925
3926 BUG_ON(prio < 0 || prio > MAX_PRIO);
3927
3928 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02003929 update_rq_clock(rq);
3930 now = rq->clock;
Ingo Molnarb29739f2006-06-27 02:54:51 -07003931
Andrew Mortond5f9f942007-05-08 20:27:06 -07003932 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02003933 on_rq = p->se.on_rq;
3934 if (on_rq)
3935 dequeue_task(rq, p, 0, now);
3936
3937 if (rt_prio(prio))
3938 p->sched_class = &rt_sched_class;
3939 else
3940 p->sched_class = &fair_sched_class;
3941
Ingo Molnarb29739f2006-06-27 02:54:51 -07003942 p->prio = prio;
3943
Ingo Molnardd41f592007-07-09 18:51:59 +02003944 if (on_rq) {
3945 enqueue_task(rq, p, 0, now);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003946 /*
3947 * Reschedule if we are currently running on this runqueue and
Andrew Mortond5f9f942007-05-08 20:27:06 -07003948 * our priority decreased, or if we are not currently running on
3949 * this runqueue and our priority is higher than the current's
Ingo Molnarb29739f2006-06-27 02:54:51 -07003950 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07003951 if (task_running(rq, p)) {
3952 if (p->prio > oldprio)
3953 resched_task(rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02003954 } else {
3955 check_preempt_curr(rq, p);
3956 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07003957 }
3958 task_rq_unlock(rq, &flags);
3959}
3960
3961#endif
3962
Ingo Molnar36c8b582006-07-03 00:25:41 -07003963void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964{
Ingo Molnardd41f592007-07-09 18:51:59 +02003965 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003967 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02003968 u64 now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969
3970 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
3971 return;
3972 /*
3973 * We have to be careful, if called from sys_setpriority(),
3974 * the task might be in the middle of scheduling on another CPU.
3975 */
3976 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02003977 update_rq_clock(rq);
3978 now = rq->clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 /*
3980 * The RT priorities are set via sched_setscheduler(), but we still
3981 * allow the 'normal' nice value to be set - but as expected
3982 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02003983 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 */
Ingo Molnare05606d2007-07-09 18:51:59 +02003985 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 p->static_prio = NICE_TO_PRIO(nice);
3987 goto out_unlock;
3988 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003989 on_rq = p->se.on_rq;
3990 if (on_rq) {
3991 dequeue_task(rq, p, 0, now);
3992 dec_load(rq, p, now);
Peter Williams2dd73a42006-06-27 02:54:34 -07003993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07003996 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003997 old_prio = p->prio;
3998 p->prio = effective_prio(p);
3999 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
Ingo Molnardd41f592007-07-09 18:51:59 +02004001 if (on_rq) {
4002 enqueue_task(rq, p, 0, now);
4003 inc_load(rq, p, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07004005 * If the task increased its priority or is running and
4006 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004008 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 resched_task(rq->curr);
4010 }
4011out_unlock:
4012 task_rq_unlock(rq, &flags);
4013}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014EXPORT_SYMBOL(set_user_nice);
4015
Matt Mackalle43379f2005-05-01 08:59:00 -07004016/*
4017 * can_nice - check if a task can reduce its nice value
4018 * @p: task
4019 * @nice: nice value
4020 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004021int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07004022{
Matt Mackall024f4742005-08-18 11:24:19 -07004023 /* convert nice value [19,-20] to rlimit style value [1,40] */
4024 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004025
Matt Mackalle43379f2005-05-01 08:59:00 -07004026 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
4027 capable(CAP_SYS_NICE));
4028}
4029
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030#ifdef __ARCH_WANT_SYS_NICE
4031
4032/*
4033 * sys_nice - change the priority of the current process.
4034 * @increment: priority increment
4035 *
4036 * sys_setpriority is a more generic, but much slower function that
4037 * does similar things.
4038 */
4039asmlinkage long sys_nice(int increment)
4040{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004041 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
4043 /*
4044 * Setpriority might change our priority at the same moment.
4045 * We don't have to worry. Conceptually one call occurs first
4046 * and we have a single winner.
4047 */
Matt Mackalle43379f2005-05-01 08:59:00 -07004048 if (increment < -40)
4049 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 if (increment > 40)
4051 increment = 40;
4052
4053 nice = PRIO_TO_NICE(current->static_prio) + increment;
4054 if (nice < -20)
4055 nice = -20;
4056 if (nice > 19)
4057 nice = 19;
4058
Matt Mackalle43379f2005-05-01 08:59:00 -07004059 if (increment < 0 && !can_nice(current, nice))
4060 return -EPERM;
4061
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 retval = security_task_setnice(current, nice);
4063 if (retval)
4064 return retval;
4065
4066 set_user_nice(current, nice);
4067 return 0;
4068}
4069
4070#endif
4071
4072/**
4073 * task_prio - return the priority value of a given task.
4074 * @p: the task in question.
4075 *
4076 * This is the priority value as seen by users in /proc.
4077 * RT tasks are offset by -200. Normal tasks are centered
4078 * around 0, value goes from -16 to +15.
4079 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004080int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081{
4082 return p->prio - MAX_RT_PRIO;
4083}
4084
4085/**
4086 * task_nice - return the nice value of a given task.
4087 * @p: the task in question.
4088 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004089int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090{
4091 return TASK_NICE(p);
4092}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093EXPORT_SYMBOL_GPL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094
4095/**
4096 * idle_cpu - is a given cpu idle currently?
4097 * @cpu: the processor in question.
4098 */
4099int idle_cpu(int cpu)
4100{
4101 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
4102}
4103
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104/**
4105 * idle_task - return the idle task for a given cpu.
4106 * @cpu: the processor in question.
4107 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004108struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109{
4110 return cpu_rq(cpu)->idle;
4111}
4112
4113/**
4114 * find_process_by_pid - find a process with a matching PID value.
4115 * @pid: the pid in question.
4116 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004117static inline struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118{
4119 return pid ? find_task_by_pid(pid) : current;
4120}
4121
4122/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02004123static void
4124__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125{
Ingo Molnardd41f592007-07-09 18:51:59 +02004126 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004127
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02004129 switch (p->policy) {
4130 case SCHED_NORMAL:
4131 case SCHED_BATCH:
4132 case SCHED_IDLE:
4133 p->sched_class = &fair_sched_class;
4134 break;
4135 case SCHED_FIFO:
4136 case SCHED_RR:
4137 p->sched_class = &rt_sched_class;
4138 break;
4139 }
4140
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004142 p->normal_prio = normal_prio(p);
4143 /* we are holding p->pi_lock already */
4144 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07004145 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146}
4147
4148/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004149 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 * @p: the task in question.
4151 * @policy: new policy.
4152 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004153 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004154 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004156int sched_setscheduler(struct task_struct *p, int policy,
4157 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158{
Ingo Molnardd41f592007-07-09 18:51:59 +02004159 int retval, oldprio, oldpolicy = -1, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004161 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162
Steven Rostedt66e53932006-06-27 02:54:44 -07004163 /* may grab non-irq protected spin_locks */
4164 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165recheck:
4166 /* double check policy once rq lock held */
4167 if (policy < 0)
4168 policy = oldpolicy = p->policy;
4169 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02004170 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
4171 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08004172 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 /*
4174 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02004175 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
4176 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 */
4178 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004179 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04004180 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02004182 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 return -EINVAL;
4184
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004185 /*
4186 * Allow unprivileged RT tasks to decrease priority:
4187 */
4188 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02004189 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004190 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004191
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004192 if (!lock_task_sighand(p, &flags))
4193 return -ESRCH;
4194 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
4195 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004196
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004197 /* can't set/change the rt policy */
4198 if (policy != p->policy && !rlim_rtprio)
4199 return -EPERM;
4200
4201 /* can't increase priority */
4202 if (param->sched_priority > p->rt_priority &&
4203 param->sched_priority > rlim_rtprio)
4204 return -EPERM;
4205 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004206 /*
4207 * Like positive nice levels, dont allow tasks to
4208 * move out of SCHED_IDLE either:
4209 */
4210 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
4211 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004212
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004213 /* can't change other user's priorities */
4214 if ((current->euid != p->euid) &&
4215 (current->euid != p->uid))
4216 return -EPERM;
4217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
4219 retval = security_task_setscheduler(p, policy, param);
4220 if (retval)
4221 return retval;
4222 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07004223 * make sure no PI-waiters arrive (or leave) while we are
4224 * changing the priority of the task:
4225 */
4226 spin_lock_irqsave(&p->pi_lock, flags);
4227 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 * To be able to change p->policy safely, the apropriate
4229 * runqueue lock must be held.
4230 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07004231 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 /* recheck policy now with rq lock held */
4233 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
4234 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004235 __task_rq_unlock(rq);
4236 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 goto recheck;
4238 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004239 on_rq = p->se.on_rq;
Ingo Molnara8e504d2007-08-09 11:16:47 +02004240 if (on_rq) {
4241 update_rq_clock(rq);
4242 deactivate_task(rq, p, 0, rq->clock);
4243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004245 __setscheduler(rq, p, policy, param->sched_priority);
4246 if (on_rq) {
4247 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 /*
4249 * Reschedule if we are currently running on this runqueue and
Andrew Mortond5f9f942007-05-08 20:27:06 -07004250 * our priority decreased, or if we are not currently running on
4251 * this runqueue and our priority is higher than the current's
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004253 if (task_running(rq, p)) {
4254 if (p->prio > oldprio)
4255 resched_task(rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02004256 } else {
4257 check_preempt_curr(rq, p);
4258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07004260 __task_rq_unlock(rq);
4261 spin_unlock_irqrestore(&p->pi_lock, flags);
4262
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07004263 rt_mutex_adjust_pi(p);
4264
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 return 0;
4266}
4267EXPORT_SYMBOL_GPL(sched_setscheduler);
4268
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004269static int
4270do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 struct sched_param lparam;
4273 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004274 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
4276 if (!param || pid < 0)
4277 return -EINVAL;
4278 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
4279 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004280
4281 rcu_read_lock();
4282 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004284 if (p != NULL)
4285 retval = sched_setscheduler(p, policy, &lparam);
4286 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07004287
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 return retval;
4289}
4290
4291/**
4292 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4293 * @pid: the pid in question.
4294 * @policy: new policy.
4295 * @param: structure containing the new RT priority.
4296 */
4297asmlinkage long sys_sched_setscheduler(pid_t pid, int policy,
4298 struct sched_param __user *param)
4299{
Jason Baronc21761f2006-01-18 17:43:03 -08004300 /* negative values for policy are not valid */
4301 if (policy < 0)
4302 return -EINVAL;
4303
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 return do_sched_setscheduler(pid, policy, param);
4305}
4306
4307/**
4308 * sys_sched_setparam - set/change the RT priority of a thread
4309 * @pid: the pid in question.
4310 * @param: structure containing the new RT priority.
4311 */
4312asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
4313{
4314 return do_sched_setscheduler(pid, -1, param);
4315}
4316
4317/**
4318 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4319 * @pid: the pid in question.
4320 */
4321asmlinkage long sys_sched_getscheduler(pid_t pid)
4322{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004323 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325
4326 if (pid < 0)
4327 goto out_nounlock;
4328
4329 retval = -ESRCH;
4330 read_lock(&tasklist_lock);
4331 p = find_process_by_pid(pid);
4332 if (p) {
4333 retval = security_task_getscheduler(p);
4334 if (!retval)
4335 retval = p->policy;
4336 }
4337 read_unlock(&tasklist_lock);
4338
4339out_nounlock:
4340 return retval;
4341}
4342
4343/**
4344 * sys_sched_getscheduler - get the RT priority of a thread
4345 * @pid: the pid in question.
4346 * @param: structure containing the RT priority.
4347 */
4348asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
4349{
4350 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004351 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353
4354 if (!param || pid < 0)
4355 goto out_nounlock;
4356
4357 read_lock(&tasklist_lock);
4358 p = find_process_by_pid(pid);
4359 retval = -ESRCH;
4360 if (!p)
4361 goto out_unlock;
4362
4363 retval = security_task_getscheduler(p);
4364 if (retval)
4365 goto out_unlock;
4366
4367 lp.sched_priority = p->rt_priority;
4368 read_unlock(&tasklist_lock);
4369
4370 /*
4371 * This one might sleep, we cannot do it with a spinlock held ...
4372 */
4373 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
4374
4375out_nounlock:
4376 return retval;
4377
4378out_unlock:
4379 read_unlock(&tasklist_lock);
4380 return retval;
4381}
4382
4383long sched_setaffinity(pid_t pid, cpumask_t new_mask)
4384{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 cpumask_t cpus_allowed;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004386 struct task_struct *p;
4387 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
Gautham R Shenoy5be93612007-05-09 02:34:04 -07004389 mutex_lock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 read_lock(&tasklist_lock);
4391
4392 p = find_process_by_pid(pid);
4393 if (!p) {
4394 read_unlock(&tasklist_lock);
Gautham R Shenoy5be93612007-05-09 02:34:04 -07004395 mutex_unlock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 return -ESRCH;
4397 }
4398
4399 /*
4400 * It is not safe to call set_cpus_allowed with the
4401 * tasklist_lock held. We will bump the task_struct's
4402 * usage count and then drop tasklist_lock.
4403 */
4404 get_task_struct(p);
4405 read_unlock(&tasklist_lock);
4406
4407 retval = -EPERM;
4408 if ((current->euid != p->euid) && (current->euid != p->uid) &&
4409 !capable(CAP_SYS_NICE))
4410 goto out_unlock;
4411
David Quigleye7834f82006-06-23 02:03:59 -07004412 retval = security_task_setscheduler(p, 0, NULL);
4413 if (retval)
4414 goto out_unlock;
4415
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 cpus_allowed = cpuset_cpus_allowed(p);
4417 cpus_and(new_mask, new_mask, cpus_allowed);
4418 retval = set_cpus_allowed(p, new_mask);
4419
4420out_unlock:
4421 put_task_struct(p);
Gautham R Shenoy5be93612007-05-09 02:34:04 -07004422 mutex_unlock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 return retval;
4424}
4425
4426static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
4427 cpumask_t *new_mask)
4428{
4429 if (len < sizeof(cpumask_t)) {
4430 memset(new_mask, 0, sizeof(cpumask_t));
4431 } else if (len > sizeof(cpumask_t)) {
4432 len = sizeof(cpumask_t);
4433 }
4434 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
4435}
4436
4437/**
4438 * sys_sched_setaffinity - set the cpu affinity of a process
4439 * @pid: pid of the process
4440 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4441 * @user_mask_ptr: user-space pointer to the new cpu mask
4442 */
4443asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
4444 unsigned long __user *user_mask_ptr)
4445{
4446 cpumask_t new_mask;
4447 int retval;
4448
4449 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
4450 if (retval)
4451 return retval;
4452
4453 return sched_setaffinity(pid, new_mask);
4454}
4455
4456/*
4457 * Represents all cpu's present in the system
4458 * In systems capable of hotplug, this map could dynamically grow
4459 * as new cpu's are detected in the system via any platform specific
4460 * method, such as ACPI for e.g.
4461 */
4462
Andi Kleen4cef0c62006-01-11 22:44:57 +01004463cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464EXPORT_SYMBOL(cpu_present_map);
4465
4466#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01004467cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07004468EXPORT_SYMBOL(cpu_online_map);
4469
Andi Kleen4cef0c62006-01-11 22:44:57 +01004470cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07004471EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472#endif
4473
4474long sched_getaffinity(pid_t pid, cpumask_t *mask)
4475{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004476 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
Gautham R Shenoy5be93612007-05-09 02:34:04 -07004479 mutex_lock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 read_lock(&tasklist_lock);
4481
4482 retval = -ESRCH;
4483 p = find_process_by_pid(pid);
4484 if (!p)
4485 goto out_unlock;
4486
David Quigleye7834f82006-06-23 02:03:59 -07004487 retval = security_task_getscheduler(p);
4488 if (retval)
4489 goto out_unlock;
4490
Jack Steiner2f7016d2006-02-01 03:05:18 -08004491 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492
4493out_unlock:
4494 read_unlock(&tasklist_lock);
Gautham R Shenoy5be93612007-05-09 02:34:04 -07004495 mutex_unlock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
Ulrich Drepper9531b622007-08-09 11:16:46 +02004497 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498}
4499
4500/**
4501 * sys_sched_getaffinity - get the cpu affinity of a process
4502 * @pid: pid of the process
4503 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4504 * @user_mask_ptr: user-space pointer to hold the current cpu mask
4505 */
4506asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
4507 unsigned long __user *user_mask_ptr)
4508{
4509 int ret;
4510 cpumask_t mask;
4511
4512 if (len < sizeof(cpumask_t))
4513 return -EINVAL;
4514
4515 ret = sched_getaffinity(pid, &mask);
4516 if (ret < 0)
4517 return ret;
4518
4519 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
4520 return -EFAULT;
4521
4522 return sizeof(cpumask_t);
4523}
4524
4525/**
4526 * sys_sched_yield - yield the current processor to other threads.
4527 *
Ingo Molnardd41f592007-07-09 18:51:59 +02004528 * This function yields the current CPU to other tasks. If there are no
4529 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 */
4531asmlinkage long sys_sched_yield(void)
4532{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004533 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534
4535 schedstat_inc(rq, yld_cnt);
Ingo Molnardd41f592007-07-09 18:51:59 +02004536 if (unlikely(rq->nr_running == 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 schedstat_inc(rq, yld_act_empty);
Ingo Molnardd41f592007-07-09 18:51:59 +02004538 else
4539 current->sched_class->yield_task(rq, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540
4541 /*
4542 * Since we are going to call schedule() anyway, there's
4543 * no need to preempt or enable interrupts:
4544 */
4545 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07004546 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 _raw_spin_unlock(&rq->lock);
4548 preempt_enable_no_resched();
4549
4550 schedule();
4551
4552 return 0;
4553}
4554
Andrew Mortone7b38402006-06-30 01:56:00 -07004555static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07004557#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
4558 __might_sleep(__FILE__, __LINE__);
4559#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07004560 /*
4561 * The BKS might be reacquired before we have dropped
4562 * PREEMPT_ACTIVE, which could trigger a second
4563 * cond_resched() call.
4564 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 do {
4566 add_preempt_count(PREEMPT_ACTIVE);
4567 schedule();
4568 sub_preempt_count(PREEMPT_ACTIVE);
4569 } while (need_resched());
4570}
4571
4572int __sched cond_resched(void)
4573{
Ingo Molnar94142322006-12-29 16:48:13 -08004574 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
4575 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 __cond_resched();
4577 return 1;
4578 }
4579 return 0;
4580}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581EXPORT_SYMBOL(cond_resched);
4582
4583/*
4584 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
4585 * call schedule, and on return reacquire the lock.
4586 *
4587 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
4588 * operations here to prevent schedule() from being called twice (once via
4589 * spin_unlock(), once by hand).
4590 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004591int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592{
Jan Kara6df3cec2005-06-13 15:52:32 -07004593 int ret = 0;
4594
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 if (need_lockbreak(lock)) {
4596 spin_unlock(lock);
4597 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07004598 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 spin_lock(lock);
4600 }
Ingo Molnar94142322006-12-29 16:48:13 -08004601 if (need_resched() && system_state == SYSTEM_RUNNING) {
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07004602 spin_release(&lock->dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 _raw_spin_unlock(lock);
4604 preempt_enable_no_resched();
4605 __cond_resched();
Jan Kara6df3cec2005-06-13 15:52:32 -07004606 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 }
Jan Kara6df3cec2005-06-13 15:52:32 -07004609 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611EXPORT_SYMBOL(cond_resched_lock);
4612
4613int __sched cond_resched_softirq(void)
4614{
4615 BUG_ON(!in_softirq());
4616
Ingo Molnar94142322006-12-29 16:48:13 -08004617 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07004618 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 __cond_resched();
4620 local_bh_disable();
4621 return 1;
4622 }
4623 return 0;
4624}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625EXPORT_SYMBOL(cond_resched_softirq);
4626
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627/**
4628 * yield - yield the current processor to other threads.
4629 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004630 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 * thread runnable and calls sys_sched_yield().
4632 */
4633void __sched yield(void)
4634{
4635 set_current_state(TASK_RUNNING);
4636 sys_sched_yield();
4637}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638EXPORT_SYMBOL(yield);
4639
4640/*
4641 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
4642 * that process accounting knows that this is a task in IO wait state.
4643 *
4644 * But don't do that if it is a deliberate, throttling IO wait (this task
4645 * has set its backing_dev_info: the queue against which it should throttle)
4646 */
4647void __sched io_schedule(void)
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
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004651 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 atomic_inc(&rq->nr_iowait);
4653 schedule();
4654 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004655 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657EXPORT_SYMBOL(io_schedule);
4658
4659long __sched io_schedule_timeout(long timeout)
4660{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004661 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 long ret;
4663
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004664 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 atomic_inc(&rq->nr_iowait);
4666 ret = schedule_timeout(timeout);
4667 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004668 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 return ret;
4670}
4671
4672/**
4673 * sys_sched_get_priority_max - return maximum RT priority.
4674 * @policy: scheduling class.
4675 *
4676 * this syscall returns the maximum rt_priority that can be used
4677 * by a given scheduling class.
4678 */
4679asmlinkage long sys_sched_get_priority_max(int policy)
4680{
4681 int ret = -EINVAL;
4682
4683 switch (policy) {
4684 case SCHED_FIFO:
4685 case SCHED_RR:
4686 ret = MAX_USER_RT_PRIO-1;
4687 break;
4688 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08004689 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02004690 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 ret = 0;
4692 break;
4693 }
4694 return ret;
4695}
4696
4697/**
4698 * sys_sched_get_priority_min - return minimum RT priority.
4699 * @policy: scheduling class.
4700 *
4701 * this syscall returns the minimum rt_priority that can be used
4702 * by a given scheduling class.
4703 */
4704asmlinkage long sys_sched_get_priority_min(int policy)
4705{
4706 int ret = -EINVAL;
4707
4708 switch (policy) {
4709 case SCHED_FIFO:
4710 case SCHED_RR:
4711 ret = 1;
4712 break;
4713 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08004714 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02004715 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 ret = 0;
4717 }
4718 return ret;
4719}
4720
4721/**
4722 * sys_sched_rr_get_interval - return the default timeslice of a process.
4723 * @pid: pid of the process.
4724 * @interval: userspace pointer to the timeslice value.
4725 *
4726 * this syscall writes the default timeslice value of a given process
4727 * into the user-space timespec buffer. A value of '0' means infinity.
4728 */
4729asmlinkage
4730long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
4731{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004732 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 int retval = -EINVAL;
4734 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735
4736 if (pid < 0)
4737 goto out_nounlock;
4738
4739 retval = -ESRCH;
4740 read_lock(&tasklist_lock);
4741 p = find_process_by_pid(pid);
4742 if (!p)
4743 goto out_unlock;
4744
4745 retval = security_task_getscheduler(p);
4746 if (retval)
4747 goto out_unlock;
4748
Peter Williamsb78709c2006-06-26 16:58:00 +10004749 jiffies_to_timespec(p->policy == SCHED_FIFO ?
Ingo Molnardd41f592007-07-09 18:51:59 +02004750 0 : static_prio_timeslice(p->static_prio), &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 read_unlock(&tasklist_lock);
4752 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
4753out_nounlock:
4754 return retval;
4755out_unlock:
4756 read_unlock(&tasklist_lock);
4757 return retval;
4758}
4759
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004760static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07004761
4762static void show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004765 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 state = p->state ? __ffs(p->state) + 1 : 0;
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004768 printk("%-13.13s %c", p->comm,
4769 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02004770#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 if (state == TASK_RUNNING)
Ingo Molnar4bd77322007-07-11 21:21:47 +02004772 printk(" running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 else
Ingo Molnar4bd77322007-07-11 21:21:47 +02004774 printk(" %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775#else
4776 if (state == TASK_RUNNING)
Ingo Molnar4bd77322007-07-11 21:21:47 +02004777 printk(" running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 else
4779 printk(" %016lx ", thread_saved_pc(p));
4780#endif
4781#ifdef CONFIG_DEBUG_STACK_USAGE
4782 {
Al Viro10ebffd2005-11-13 16:06:56 -08004783 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 while (!*n)
4785 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08004786 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 }
4788#endif
Ingo Molnar4bd77322007-07-11 21:21:47 +02004789 printk("%5lu %5d %6d\n", free, p->pid, p->parent->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790
4791 if (state != TASK_RUNNING)
4792 show_stack(p, NULL);
4793}
4794
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004795void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004797 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
Ingo Molnar4bd77322007-07-11 21:21:47 +02004799#if BITS_PER_LONG == 32
4800 printk(KERN_INFO
4801 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02004803 printk(KERN_INFO
4804 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805#endif
4806 read_lock(&tasklist_lock);
4807 do_each_thread(g, p) {
4808 /*
4809 * reset the NMI-timeout, listing all files on a slow
4810 * console might take alot of time:
4811 */
4812 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07004813 if (!state_filter || (p->state & state_filter))
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004814 show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 } while_each_thread(g, p);
4816
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07004817 touch_all_softlockup_watchdogs();
4818
Ingo Molnardd41f592007-07-09 18:51:59 +02004819#ifdef CONFIG_SCHED_DEBUG
4820 sysrq_sched_debug_show();
4821#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004823 /*
4824 * Only show locks if all tasks are dumped:
4825 */
4826 if (state_filter == -1)
4827 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828}
4829
Ingo Molnar1df21052007-07-09 18:51:58 +02004830void __cpuinit init_idle_bootup_task(struct task_struct *idle)
4831{
Ingo Molnardd41f592007-07-09 18:51:59 +02004832 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02004833}
4834
Ingo Molnarf340c0d2005-06-28 16:40:42 +02004835/**
4836 * init_idle - set up an idle thread for a given CPU
4837 * @idle: task in question
4838 * @cpu: cpu the idle task belongs to
4839 *
4840 * NOTE: this function does not set the idle thread's NEED_RESCHED
4841 * flag, to make booting more robust.
4842 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07004843void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004845 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 unsigned long flags;
4847
Ingo Molnardd41f592007-07-09 18:51:59 +02004848 __sched_fork(idle);
4849 idle->se.exec_start = sched_clock();
4850
Ingo Molnarb29739f2006-06-27 02:54:51 -07004851 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004853 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
4855 spin_lock_irqsave(&rq->lock, flags);
4856 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07004857#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
4858 idle->oncpu = 1;
4859#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 spin_unlock_irqrestore(&rq->lock, flags);
4861
4862 /* Set the preempt count _outside_ the spinlocks! */
4863#if defined(CONFIG_PREEMPT) && !defined(CONFIG_PREEMPT_BKL)
Al Viroa1261f542005-11-13 16:06:55 -08004864 task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865#else
Al Viroa1261f542005-11-13 16:06:55 -08004866 task_thread_info(idle)->preempt_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004868 /*
4869 * The idle tasks have their own, simple scheduling class:
4870 */
4871 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872}
4873
4874/*
4875 * In a system that switches off the HZ timer nohz_cpu_mask
4876 * indicates which cpus entered this state. This is used
4877 * in the rcu update to wait only for active cpus. For system
4878 * which do not switch off the HZ timer nohz_cpu_mask should
4879 * always be CPU_MASK_NONE.
4880 */
4881cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
4882
Ingo Molnardd41f592007-07-09 18:51:59 +02004883/*
4884 * Increase the granularity value when there are more CPUs,
4885 * because with more CPUs the 'effective latency' as visible
4886 * to users decreases. But the relationship is not linear,
4887 * so pick a second-best guess by going with the log2 of the
4888 * number of CPUs.
4889 *
4890 * This idea comes from the SD scheduler of Con Kolivas:
4891 */
4892static inline void sched_init_granularity(void)
4893{
4894 unsigned int factor = 1 + ilog2(num_online_cpus());
Ingo Molnara5968df2007-07-11 21:21:47 +02004895 const unsigned long gran_limit = 100000000;
Ingo Molnardd41f592007-07-09 18:51:59 +02004896
4897 sysctl_sched_granularity *= factor;
4898 if (sysctl_sched_granularity > gran_limit)
4899 sysctl_sched_granularity = gran_limit;
4900
4901 sysctl_sched_runtime_limit = sysctl_sched_granularity * 4;
4902 sysctl_sched_wakeup_granularity = sysctl_sched_granularity / 2;
4903}
4904
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905#ifdef CONFIG_SMP
4906/*
4907 * This is how migration works:
4908 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07004909 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 * runqueue and wake up that CPU's migration thread.
4911 * 2) we down() the locked semaphore => thread blocks.
4912 * 3) migration thread wakes up (implicitly it forces the migrated
4913 * thread off the CPU)
4914 * 4) it gets the migration request and checks whether the migrated
4915 * task is still in the wrong runqueue.
4916 * 5) if it's in the wrong runqueue then the migration thread removes
4917 * it and puts it into the right queue.
4918 * 6) migration thread up()s the semaphore.
4919 * 7) we wake up and the migration is done.
4920 */
4921
4922/*
4923 * Change a given task's CPU affinity. Migrate the thread to a
4924 * proper CPU and schedule it away if the CPU it's executing on
4925 * is removed from the allowed bitmask.
4926 *
4927 * NOTE: the caller must have a valid reference to the task, the
4928 * task must not exit() & deallocate itself prematurely. The
4929 * call is not atomic; no spinlocks may be held.
4930 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004931int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004933 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004935 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004936 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
4938 rq = task_rq_lock(p, &flags);
4939 if (!cpus_intersects(new_mask, cpu_online_map)) {
4940 ret = -EINVAL;
4941 goto out;
4942 }
4943
4944 p->cpus_allowed = new_mask;
4945 /* Can the task run on the task's current CPU? If so, we're done */
4946 if (cpu_isset(task_cpu(p), new_mask))
4947 goto out;
4948
4949 if (migrate_task(p, any_online_cpu(new_mask), &req)) {
4950 /* Need help from migration thread: drop lock and wait. */
4951 task_rq_unlock(rq, &flags);
4952 wake_up_process(rq->migration_thread);
4953 wait_for_completion(&req.done);
4954 tlb_migrate_finish(p->mm);
4955 return 0;
4956 }
4957out:
4958 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004959
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 return ret;
4961}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962EXPORT_SYMBOL_GPL(set_cpus_allowed);
4963
4964/*
4965 * Move (not current) task off this cpu, onto dest cpu. We're doing
4966 * this because either it can't run here any more (set_cpus_allowed()
4967 * away from this CPU, or CPU going down), or because we're
4968 * attempting to rebalance this task on exec (sched_exec).
4969 *
4970 * So we race with normal scheduler movements, but that's OK, as long
4971 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07004972 *
4973 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07004975static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004977 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02004978 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979
4980 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07004981 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982
4983 rq_src = cpu_rq(src_cpu);
4984 rq_dest = cpu_rq(dest_cpu);
4985
4986 double_rq_lock(rq_src, rq_dest);
4987 /* Already moved. */
4988 if (task_cpu(p) != src_cpu)
4989 goto out;
4990 /* Affinity changed (again). */
4991 if (!cpu_isset(dest_cpu, p->cpus_allowed))
4992 goto out;
4993
Ingo Molnardd41f592007-07-09 18:51:59 +02004994 on_rq = p->se.on_rq;
Ingo Molnara8e504d2007-08-09 11:16:47 +02004995 if (on_rq) {
4996 update_rq_clock(rq_src);
4997 deactivate_task(rq_src, p, 0, rq_src->clock);
4998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005000 if (on_rq) {
5001 activate_task(rq_dest, p, 0);
5002 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07005004 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005out:
5006 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07005007 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008}
5009
5010/*
5011 * migration_thread - this is a highprio system thread that performs
5012 * thread migration by bumping thread off CPU then 'pushing' onto
5013 * another runqueue.
5014 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005015static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005018 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019
5020 rq = cpu_rq(cpu);
5021 BUG_ON(rq->migration_thread != current);
5022
5023 set_current_state(TASK_INTERRUPTIBLE);
5024 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005025 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 spin_lock_irq(&rq->lock);
5029
5030 if (cpu_is_offline(cpu)) {
5031 spin_unlock_irq(&rq->lock);
5032 goto wait_to_die;
5033 }
5034
5035 if (rq->active_balance) {
5036 active_load_balance(rq, cpu);
5037 rq->active_balance = 0;
5038 }
5039
5040 head = &rq->migration_queue;
5041
5042 if (list_empty(head)) {
5043 spin_unlock_irq(&rq->lock);
5044 schedule();
5045 set_current_state(TASK_INTERRUPTIBLE);
5046 continue;
5047 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07005048 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 list_del_init(head->next);
5050
Nick Piggin674311d2005-06-25 14:57:27 -07005051 spin_unlock(&rq->lock);
5052 __migrate_task(req->task, cpu, req->dest_cpu);
5053 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054
5055 complete(&req->done);
5056 }
5057 __set_current_state(TASK_RUNNING);
5058 return 0;
5059
5060wait_to_die:
5061 /* Wait for kthread_stop */
5062 set_current_state(TASK_INTERRUPTIBLE);
5063 while (!kthread_should_stop()) {
5064 schedule();
5065 set_current_state(TASK_INTERRUPTIBLE);
5066 }
5067 __set_current_state(TASK_RUNNING);
5068 return 0;
5069}
5070
5071#ifdef CONFIG_HOTPLUG_CPU
Kirill Korotaev054b9102006-12-10 02:20:11 -08005072/*
5073 * Figure out where task on dead CPU should go, use force if neccessary.
5074 * NOTE: interrupts should be disabled by the caller
5075 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005076static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077{
Kirill Korotaevefc30812006-06-27 02:54:32 -07005078 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005080 struct rq *rq;
5081 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082
Kirill Korotaevefc30812006-06-27 02:54:32 -07005083restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 /* On same node? */
5085 mask = node_to_cpumask(cpu_to_node(dead_cpu));
Ingo Molnar48f24c42006-07-03 00:25:40 -07005086 cpus_and(mask, mask, p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 dest_cpu = any_online_cpu(mask);
5088
5089 /* On any allowed CPU? */
5090 if (dest_cpu == NR_CPUS)
Ingo Molnar48f24c42006-07-03 00:25:40 -07005091 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092
5093 /* No more Mr. Nice Guy. */
5094 if (dest_cpu == NR_CPUS) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07005095 rq = task_rq_lock(p, &flags);
5096 cpus_setall(p->cpus_allowed);
5097 dest_cpu = any_online_cpu(p->cpus_allowed);
Kirill Korotaevefc30812006-06-27 02:54:32 -07005098 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099
5100 /*
5101 * Don't tell them about moving exiting tasks or
5102 * kernel threads (both mm NULL), since they never
5103 * leave kernel.
5104 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005105 if (p->mm && printk_ratelimit())
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 printk(KERN_INFO "process %d (%s) no "
5107 "longer affine to cpu%d\n",
Ingo Molnar48f24c42006-07-03 00:25:40 -07005108 p->pid, p->comm, dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07005110 if (!__migrate_task(p, dead_cpu, dest_cpu))
Kirill Korotaevefc30812006-06-27 02:54:32 -07005111 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112}
5113
5114/*
5115 * While a dead CPU has no uninterruptible tasks queued at this point,
5116 * it might still have a nonzero ->nr_uninterruptible counter, because
5117 * for performance reasons the counter is not stricly tracking tasks to
5118 * their home CPUs. So we just add the counter to another CPU's counter,
5119 * to keep the global sum constant after CPU-down:
5120 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07005121static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005123 struct rq *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 unsigned long flags;
5125
5126 local_irq_save(flags);
5127 double_rq_lock(rq_src, rq_dest);
5128 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
5129 rq_src->nr_uninterruptible = 0;
5130 double_rq_unlock(rq_src, rq_dest);
5131 local_irq_restore(flags);
5132}
5133
5134/* Run through task list and migrate tasks from the dead cpu. */
5135static void migrate_live_tasks(int src_cpu)
5136{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005137 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138
5139 write_lock_irq(&tasklist_lock);
5140
Ingo Molnar48f24c42006-07-03 00:25:40 -07005141 do_each_thread(t, p) {
5142 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 continue;
5144
Ingo Molnar48f24c42006-07-03 00:25:40 -07005145 if (task_cpu(p) == src_cpu)
5146 move_task_off_dead_cpu(src_cpu, p);
5147 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
5149 write_unlock_irq(&tasklist_lock);
5150}
5151
Ingo Molnardd41f592007-07-09 18:51:59 +02005152/*
5153 * Schedules idle task to be the next runnable task on current CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 * It does so by boosting its priority to highest possible and adding it to
Ingo Molnar48f24c42006-07-03 00:25:40 -07005155 * the _front_ of the runqueue. Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 */
5157void sched_idle_next(void)
5158{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005159 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07005160 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 struct task_struct *p = rq->idle;
5162 unsigned long flags;
5163
5164 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005165 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166
Ingo Molnar48f24c42006-07-03 00:25:40 -07005167 /*
5168 * Strictly not necessary since rest of the CPUs are stopped by now
5169 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 */
5171 spin_lock_irqsave(&rq->lock, flags);
5172
Ingo Molnardd41f592007-07-09 18:51:59 +02005173 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005174
5175 /* Add idle task to the _front_ of its priority queue: */
Ingo Molnardd41f592007-07-09 18:51:59 +02005176 activate_idle_task(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177
5178 spin_unlock_irqrestore(&rq->lock, flags);
5179}
5180
Ingo Molnar48f24c42006-07-03 00:25:40 -07005181/*
5182 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 * offline.
5184 */
5185void idle_task_exit(void)
5186{
5187 struct mm_struct *mm = current->active_mm;
5188
5189 BUG_ON(cpu_online(smp_processor_id()));
5190
5191 if (mm != &init_mm)
5192 switch_mm(mm, &init_mm, current);
5193 mmdrop(mm);
5194}
5195
Kirill Korotaev054b9102006-12-10 02:20:11 -08005196/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005197static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005199 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200
5201 /* Must be exiting, otherwise would be on tasklist. */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005202 BUG_ON(p->exit_state != EXIT_ZOMBIE && p->exit_state != EXIT_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203
5204 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07005205 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206
Ingo Molnar48f24c42006-07-03 00:25:40 -07005207 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208
5209 /*
5210 * Drop lock around migration; if someone else moves it,
5211 * that's OK. No task can be added to this CPU, so iteration is
5212 * fine.
Kirill Korotaev054b9102006-12-10 02:20:11 -08005213 * NOTE: interrupts should be left disabled --dev@
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 */
Kirill Korotaev054b9102006-12-10 02:20:11 -08005215 spin_unlock(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005216 move_task_off_dead_cpu(dead_cpu, p);
Kirill Korotaev054b9102006-12-10 02:20:11 -08005217 spin_lock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218
Ingo Molnar48f24c42006-07-03 00:25:40 -07005219 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220}
5221
5222/* release_task() removes task from tasklist, so we won't find dead tasks. */
5223static void migrate_dead_tasks(unsigned int dead_cpu)
5224{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005225 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005226 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227
Ingo Molnardd41f592007-07-09 18:51:59 +02005228 for ( ; ; ) {
5229 if (!rq->nr_running)
5230 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02005231 update_rq_clock(rq);
5232 next = pick_next_task(rq, rq->curr, rq->clock);
Ingo Molnardd41f592007-07-09 18:51:59 +02005233 if (!next)
5234 break;
5235 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02005236
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 }
5238}
5239#endif /* CONFIG_HOTPLUG_CPU */
5240
Nick Piggine692ab52007-07-26 13:40:43 +02005241#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5242
5243static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005244 {
5245 .procname = "sched_domain",
5246 .mode = 0755,
5247 },
Nick Piggine692ab52007-07-26 13:40:43 +02005248 {0,},
5249};
5250
5251static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005252 {
5253 .procname = "kernel",
5254 .mode = 0755,
5255 .child = sd_ctl_dir,
5256 },
Nick Piggine692ab52007-07-26 13:40:43 +02005257 {0,},
5258};
5259
5260static struct ctl_table *sd_alloc_ctl_entry(int n)
5261{
5262 struct ctl_table *entry =
5263 kmalloc(n * sizeof(struct ctl_table), GFP_KERNEL);
5264
5265 BUG_ON(!entry);
5266 memset(entry, 0, n * sizeof(struct ctl_table));
5267
5268 return entry;
5269}
5270
5271static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02005272set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02005273 const char *procname, void *data, int maxlen,
5274 mode_t mode, proc_handler *proc_handler)
5275{
Nick Piggine692ab52007-07-26 13:40:43 +02005276 entry->procname = procname;
5277 entry->data = data;
5278 entry->maxlen = maxlen;
5279 entry->mode = mode;
5280 entry->proc_handler = proc_handler;
5281}
5282
5283static struct ctl_table *
5284sd_alloc_ctl_domain_table(struct sched_domain *sd)
5285{
5286 struct ctl_table *table = sd_alloc_ctl_entry(14);
5287
Alexey Dobriyane0361852007-08-09 11:16:46 +02005288 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005289 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005290 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005291 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005292 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005293 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005294 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005295 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005296 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005297 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005298 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005299 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005300 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005301 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005302 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02005303 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005304 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02005305 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005306 set_table_entry(&table[10], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02005307 &sd->cache_nice_tries,
5308 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005309 set_table_entry(&table[12], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02005310 sizeof(int), 0644, proc_dointvec_minmax);
5311
5312 return table;
5313}
5314
5315static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
5316{
5317 struct ctl_table *entry, *table;
5318 struct sched_domain *sd;
5319 int domain_num = 0, i;
5320 char buf[32];
5321
5322 for_each_domain(cpu, sd)
5323 domain_num++;
5324 entry = table = sd_alloc_ctl_entry(domain_num + 1);
5325
5326 i = 0;
5327 for_each_domain(cpu, sd) {
5328 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005329 entry->procname = kstrdup(buf, GFP_KERNEL);
5330 entry->mode = 0755;
5331 entry->child = sd_alloc_ctl_domain_table(sd);
5332 entry++;
5333 i++;
5334 }
5335 return table;
5336}
5337
5338static struct ctl_table_header *sd_sysctl_header;
5339static void init_sched_domain_sysctl(void)
5340{
5341 int i, cpu_num = num_online_cpus();
5342 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
5343 char buf[32];
5344
5345 sd_ctl_dir[0].child = entry;
5346
5347 for (i = 0; i < cpu_num; i++, entry++) {
5348 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005349 entry->procname = kstrdup(buf, GFP_KERNEL);
5350 entry->mode = 0755;
5351 entry->child = sd_alloc_ctl_cpu_table(i);
5352 }
5353 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
5354}
5355#else
5356static void init_sched_domain_sysctl(void)
5357{
5358}
5359#endif
5360
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361/*
5362 * migration_call - callback that gets triggered when a CPU is added.
5363 * Here we can start up the necessary migration thread for the new CPU.
5364 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005365static int __cpuinit
5366migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005369 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005371 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372
5373 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005374 case CPU_LOCK_ACQUIRE:
5375 mutex_lock(&sched_hotcpu_mutex);
5376 break;
5377
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005379 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02005380 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 if (IS_ERR(p))
5382 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 kthread_bind(p, cpu);
5384 /* Must be high prio: stop_machine expects to yield to it. */
5385 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02005386 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 task_rq_unlock(rq, &flags);
5388 cpu_rq(cpu)->migration_thread = p;
5389 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005390
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005392 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 /* Strictly unneccessary, as first user will wake it. */
5394 wake_up_process(cpu_rq(cpu)->migration_thread);
5395 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005396
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397#ifdef CONFIG_HOTPLUG_CPU
5398 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005399 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07005400 if (!cpu_rq(cpu)->migration_thread)
5401 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08005403 kthread_bind(cpu_rq(cpu)->migration_thread,
5404 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 kthread_stop(cpu_rq(cpu)->migration_thread);
5406 cpu_rq(cpu)->migration_thread = NULL;
5407 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005408
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005410 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 migrate_live_tasks(cpu);
5412 rq = cpu_rq(cpu);
5413 kthread_stop(rq->migration_thread);
5414 rq->migration_thread = NULL;
5415 /* Idle task back to normal (off runqueue, low prio) */
5416 rq = task_rq_lock(rq->idle, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005417 update_rq_clock(rq);
5418 deactivate_task(rq, rq->idle, 0, rq->clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02005420 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
5421 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 migrate_dead_tasks(cpu);
5423 task_rq_unlock(rq, &flags);
5424 migrate_nr_uninterruptible(rq);
5425 BUG_ON(rq->nr_running != 0);
5426
5427 /* No need to migrate the tasks: it was best-effort if
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005428 * they didn't take sched_hotcpu_mutex. Just wake up
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 * the requestors. */
5430 spin_lock_irq(&rq->lock);
5431 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005432 struct migration_req *req;
5433
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07005435 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 list_del_init(&req->list);
5437 complete(&req->done);
5438 }
5439 spin_unlock_irq(&rq->lock);
5440 break;
5441#endif
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005442 case CPU_LOCK_RELEASE:
5443 mutex_unlock(&sched_hotcpu_mutex);
5444 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 }
5446 return NOTIFY_OK;
5447}
5448
5449/* Register at highest priority so that task migration (migrate_all_tasks)
5450 * happens before everything else.
5451 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07005452static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 .notifier_call = migration_call,
5454 .priority = 10
5455};
5456
5457int __init migration_init(void)
5458{
5459 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07005460 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005461
5462 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07005463 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
5464 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 migration_call(&migration_notifier, CPU_ONLINE, cpu);
5466 register_cpu_notifier(&migration_notifier);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005467
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 return 0;
5469}
5470#endif
5471
5472#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07005473
5474/* Number of possible processor ids */
5475int nr_cpu_ids __read_mostly = NR_CPUS;
5476EXPORT_SYMBOL(nr_cpu_ids);
5477
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07005478#undef SCHED_DOMAIN_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479#ifdef SCHED_DOMAIN_DEBUG
5480static void sched_domain_debug(struct sched_domain *sd, int cpu)
5481{
5482 int level = 0;
5483
Nick Piggin41c7ce92005-06-25 14:57:24 -07005484 if (!sd) {
5485 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
5486 return;
5487 }
5488
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
5490
5491 do {
5492 int i;
5493 char str[NR_CPUS];
5494 struct sched_group *group = sd->groups;
5495 cpumask_t groupmask;
5496
5497 cpumask_scnprintf(str, NR_CPUS, sd->span);
5498 cpus_clear(groupmask);
5499
5500 printk(KERN_DEBUG);
5501 for (i = 0; i < level + 1; i++)
5502 printk(" ");
5503 printk("domain %d: ", level);
5504
5505 if (!(sd->flags & SD_LOAD_BALANCE)) {
5506 printk("does not load-balance\n");
5507 if (sd->parent)
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005508 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
5509 " has parent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 break;
5511 }
5512
5513 printk("span %s\n", str);
5514
5515 if (!cpu_isset(cpu, sd->span))
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005516 printk(KERN_ERR "ERROR: domain->span does not contain "
5517 "CPU%d\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 if (!cpu_isset(cpu, group->cpumask))
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005519 printk(KERN_ERR "ERROR: domain->groups does not contain"
5520 " CPU%d\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521
5522 printk(KERN_DEBUG);
5523 for (i = 0; i < level + 2; i++)
5524 printk(" ");
5525 printk("groups:");
5526 do {
5527 if (!group) {
5528 printk("\n");
5529 printk(KERN_ERR "ERROR: group is NULL\n");
5530 break;
5531 }
5532
Eric Dumazet5517d862007-05-08 00:32:57 -07005533 if (!group->__cpu_power) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 printk("\n");
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005535 printk(KERN_ERR "ERROR: domain->cpu_power not "
5536 "set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 }
5538
5539 if (!cpus_weight(group->cpumask)) {
5540 printk("\n");
5541 printk(KERN_ERR "ERROR: empty group\n");
5542 }
5543
5544 if (cpus_intersects(groupmask, group->cpumask)) {
5545 printk("\n");
5546 printk(KERN_ERR "ERROR: repeated CPUs\n");
5547 }
5548
5549 cpus_or(groupmask, groupmask, group->cpumask);
5550
5551 cpumask_scnprintf(str, NR_CPUS, group->cpumask);
5552 printk(" %s", str);
5553
5554 group = group->next;
5555 } while (group != sd->groups);
5556 printk("\n");
5557
5558 if (!cpus_equal(sd->span, groupmask))
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005559 printk(KERN_ERR "ERROR: groups don't span "
5560 "domain->span\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561
5562 level++;
5563 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005564 if (!sd)
5565 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005567 if (!cpus_subset(groupmask, sd->span))
5568 printk(KERN_ERR "ERROR: parent span is not a superset "
5569 "of domain->span\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570
5571 } while (sd);
5572}
5573#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07005574# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575#endif
5576
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07005577static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005578{
5579 if (cpus_weight(sd->span) == 1)
5580 return 1;
5581
5582 /* Following flags need at least 2 groups */
5583 if (sd->flags & (SD_LOAD_BALANCE |
5584 SD_BALANCE_NEWIDLE |
5585 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005586 SD_BALANCE_EXEC |
5587 SD_SHARE_CPUPOWER |
5588 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005589 if (sd->groups != sd->groups->next)
5590 return 0;
5591 }
5592
5593 /* Following flags don't use groups */
5594 if (sd->flags & (SD_WAKE_IDLE |
5595 SD_WAKE_AFFINE |
5596 SD_WAKE_BALANCE))
5597 return 0;
5598
5599 return 1;
5600}
5601
Ingo Molnar48f24c42006-07-03 00:25:40 -07005602static int
5603sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005604{
5605 unsigned long cflags = sd->flags, pflags = parent->flags;
5606
5607 if (sd_degenerate(parent))
5608 return 1;
5609
5610 if (!cpus_equal(sd->span, parent->span))
5611 return 0;
5612
5613 /* Does parent contain flags not in child? */
5614 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
5615 if (cflags & SD_WAKE_AFFINE)
5616 pflags &= ~SD_WAKE_BALANCE;
5617 /* Flags needing groups don't count if only 1 group in parent */
5618 if (parent->groups == parent->groups->next) {
5619 pflags &= ~(SD_LOAD_BALANCE |
5620 SD_BALANCE_NEWIDLE |
5621 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005622 SD_BALANCE_EXEC |
5623 SD_SHARE_CPUPOWER |
5624 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07005625 }
5626 if (~cflags & pflags)
5627 return 0;
5628
5629 return 1;
5630}
5631
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632/*
5633 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
5634 * hold the hotplug lock.
5635 */
John Hawkes9c1cfda2005-09-06 15:18:14 -07005636static void cpu_attach_domain(struct sched_domain *sd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005638 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07005639 struct sched_domain *tmp;
5640
5641 /* Remove the sched domains which do not contribute to scheduling. */
5642 for (tmp = sd; tmp; tmp = tmp->parent) {
5643 struct sched_domain *parent = tmp->parent;
5644 if (!parent)
5645 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005646 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005647 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005648 if (parent->parent)
5649 parent->parent->child = tmp;
5650 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07005651 }
5652
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005653 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005654 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005655 if (sd)
5656 sd->child = NULL;
5657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658
5659 sched_domain_debug(sd, cpu);
5660
Nick Piggin674311d2005-06-25 14:57:27 -07005661 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662}
5663
5664/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08005665static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666
5667/* Setup the mask of cpus configured for isolated domains */
5668static int __init isolated_cpu_setup(char *str)
5669{
5670 int ints[NR_CPUS], i;
5671
5672 str = get_options(str, ARRAY_SIZE(ints), ints);
5673 cpus_clear(cpu_isolated_map);
5674 for (i = 1; i <= ints[0]; i++)
5675 if (ints[i] < NR_CPUS)
5676 cpu_set(ints[i], cpu_isolated_map);
5677 return 1;
5678}
5679
5680__setup ("isolcpus=", isolated_cpu_setup);
5681
5682/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005683 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
5684 * to a function which identifies what group(along with sched group) a CPU
5685 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
5686 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 *
5688 * init_sched_build_groups will build a circular linked list of the groups
5689 * covered by the given span, and will set each group's ->cpumask correctly,
5690 * and ->cpu_power to 0.
5691 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005692static void
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005693init_sched_build_groups(cpumask_t span, const cpumask_t *cpu_map,
5694 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
5695 struct sched_group **sg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696{
5697 struct sched_group *first = NULL, *last = NULL;
5698 cpumask_t covered = CPU_MASK_NONE;
5699 int i;
5700
5701 for_each_cpu_mask(i, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005702 struct sched_group *sg;
5703 int group = group_fn(i, cpu_map, &sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 int j;
5705
5706 if (cpu_isset(i, covered))
5707 continue;
5708
5709 sg->cpumask = CPU_MASK_NONE;
Eric Dumazet5517d862007-05-08 00:32:57 -07005710 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711
5712 for_each_cpu_mask(j, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005713 if (group_fn(j, cpu_map, NULL) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 continue;
5715
5716 cpu_set(j, covered);
5717 cpu_set(j, sg->cpumask);
5718 }
5719 if (!first)
5720 first = sg;
5721 if (last)
5722 last->next = sg;
5723 last = sg;
5724 }
5725 last->next = first;
5726}
5727
John Hawkes9c1cfda2005-09-06 15:18:14 -07005728#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729
John Hawkes9c1cfda2005-09-06 15:18:14 -07005730#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08005731
John Hawkes9c1cfda2005-09-06 15:18:14 -07005732/**
5733 * find_next_best_node - find the next node to include in a sched_domain
5734 * @node: node whose sched_domain we're building
5735 * @used_nodes: nodes already in the sched_domain
5736 *
5737 * Find the next node to include in a given scheduling domain. Simply
5738 * finds the closest node not already in the @used_nodes map.
5739 *
5740 * Should use nodemask_t.
5741 */
5742static int find_next_best_node(int node, unsigned long *used_nodes)
5743{
5744 int i, n, val, min_val, best_node = 0;
5745
5746 min_val = INT_MAX;
5747
5748 for (i = 0; i < MAX_NUMNODES; i++) {
5749 /* Start at @node */
5750 n = (node + i) % MAX_NUMNODES;
5751
5752 if (!nr_cpus_node(n))
5753 continue;
5754
5755 /* Skip already used nodes */
5756 if (test_bit(n, used_nodes))
5757 continue;
5758
5759 /* Simple min distance search */
5760 val = node_distance(node, n);
5761
5762 if (val < min_val) {
5763 min_val = val;
5764 best_node = n;
5765 }
5766 }
5767
5768 set_bit(best_node, used_nodes);
5769 return best_node;
5770}
5771
5772/**
5773 * sched_domain_node_span - get a cpumask for a node's sched_domain
5774 * @node: node whose cpumask we're constructing
5775 * @size: number of nodes to include in this span
5776 *
5777 * Given a node, construct a good cpumask for its sched_domain to span. It
5778 * should be one that prevents unnecessary balancing, but also spreads tasks
5779 * out optimally.
5780 */
5781static cpumask_t sched_domain_node_span(int node)
5782{
John Hawkes9c1cfda2005-09-06 15:18:14 -07005783 DECLARE_BITMAP(used_nodes, MAX_NUMNODES);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005784 cpumask_t span, nodemask;
5785 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07005786
5787 cpus_clear(span);
5788 bitmap_zero(used_nodes, MAX_NUMNODES);
5789
5790 nodemask = node_to_cpumask(node);
5791 cpus_or(span, span, nodemask);
5792 set_bit(node, used_nodes);
5793
5794 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
5795 int next_node = find_next_best_node(node, used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005796
John Hawkes9c1cfda2005-09-06 15:18:14 -07005797 nodemask = node_to_cpumask(next_node);
5798 cpus_or(span, span, nodemask);
5799 }
5800
5801 return span;
5802}
5803#endif
5804
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07005805int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005806
John Hawkes9c1cfda2005-09-06 15:18:14 -07005807/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07005808 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07005809 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810#ifdef CONFIG_SCHED_SMT
5811static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005812static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005813
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005814static int cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map,
5815 struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005817 if (sg)
5818 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 return cpu;
5820}
5821#endif
5822
Ingo Molnar48f24c42006-07-03 00:25:40 -07005823/*
5824 * multi-core sched-domains:
5825 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005826#ifdef CONFIG_SCHED_MC
5827static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005828static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005829#endif
5830
5831#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005832static int cpu_to_core_group(int cpu, const cpumask_t *cpu_map,
5833 struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005834{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005835 int group;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005836 cpumask_t mask = cpu_sibling_map[cpu];
5837 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005838 group = first_cpu(mask);
5839 if (sg)
5840 *sg = &per_cpu(sched_group_core, group);
5841 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005842}
5843#elif defined(CONFIG_SCHED_MC)
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005844static int cpu_to_core_group(int cpu, const cpumask_t *cpu_map,
5845 struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005846{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005847 if (sg)
5848 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005849 return cpu;
5850}
5851#endif
5852
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005854static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005855
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005856static int cpu_to_phys_group(int cpu, const cpumask_t *cpu_map,
5857 struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005859 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005860#ifdef CONFIG_SCHED_MC
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005861 cpumask_t mask = cpu_coregroup_map(cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005862 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005863 group = first_cpu(mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005864#elif defined(CONFIG_SCHED_SMT)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005865 cpumask_t mask = cpu_sibling_map[cpu];
5866 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005867 group = first_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005869 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005871 if (sg)
5872 *sg = &per_cpu(sched_group_phys, group);
5873 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874}
5875
5876#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07005877/*
5878 * The init_sched_build_groups can't handle what we want to do with node
5879 * groups, so roll our own. Now each node has its own list of groups which
5880 * gets dynamically allocated.
5881 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882static DEFINE_PER_CPU(struct sched_domain, node_domains);
John Hawkesd1b55132005-09-06 15:18:14 -07005883static struct sched_group **sched_group_nodes_bycpu[NR_CPUS];
John Hawkes9c1cfda2005-09-06 15:18:14 -07005884
5885static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005886static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07005887
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005888static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
5889 struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005891 cpumask_t nodemask = node_to_cpumask(cpu_to_node(cpu));
5892 int group;
5893
5894 cpus_and(nodemask, nodemask, *cpu_map);
5895 group = first_cpu(nodemask);
5896
5897 if (sg)
5898 *sg = &per_cpu(sched_group_allnodes, group);
5899 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005901
Siddha, Suresh B08069032006-03-27 01:15:23 -08005902static void init_numa_sched_groups_power(struct sched_group *group_head)
5903{
5904 struct sched_group *sg = group_head;
5905 int j;
5906
5907 if (!sg)
5908 return;
5909next_sg:
5910 for_each_cpu_mask(j, sg->cpumask) {
5911 struct sched_domain *sd;
5912
5913 sd = &per_cpu(phys_domains, j);
5914 if (j != first_cpu(sd->groups->cpumask)) {
5915 /*
5916 * Only add "power" once for each
5917 * physical package.
5918 */
5919 continue;
5920 }
5921
Eric Dumazet5517d862007-05-08 00:32:57 -07005922 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08005923 }
5924 sg = sg->next;
5925 if (sg != group_head)
5926 goto next_sg;
5927}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928#endif
5929
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005930#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005931/* Free memory allocated for various sched_group structures */
5932static void free_sched_groups(const cpumask_t *cpu_map)
5933{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005934 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005935
5936 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005937 struct sched_group **sched_group_nodes
5938 = sched_group_nodes_bycpu[cpu];
5939
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005940 if (!sched_group_nodes)
5941 continue;
5942
5943 for (i = 0; i < MAX_NUMNODES; i++) {
5944 cpumask_t nodemask = node_to_cpumask(i);
5945 struct sched_group *oldsg, *sg = sched_group_nodes[i];
5946
5947 cpus_and(nodemask, nodemask, *cpu_map);
5948 if (cpus_empty(nodemask))
5949 continue;
5950
5951 if (sg == NULL)
5952 continue;
5953 sg = sg->next;
5954next_sg:
5955 oldsg = sg;
5956 sg = sg->next;
5957 kfree(oldsg);
5958 if (oldsg != sched_group_nodes[i])
5959 goto next_sg;
5960 }
5961 kfree(sched_group_nodes);
5962 sched_group_nodes_bycpu[cpu] = NULL;
5963 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005964}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005965#else
5966static void free_sched_groups(const cpumask_t *cpu_map)
5967{
5968}
5969#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005970
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005972 * Initialize sched groups cpu_power.
5973 *
5974 * cpu_power indicates the capacity of sched group, which is used while
5975 * distributing the load between different sched groups in a sched domain.
5976 * Typically cpu_power for all the groups in a sched domain will be same unless
5977 * there are asymmetries in the topology. If there are asymmetries, group
5978 * having more cpu_power will pickup more load compared to the group having
5979 * less cpu_power.
5980 *
5981 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
5982 * the maximum number of tasks a group can handle in the presence of other idle
5983 * or lightly loaded groups in the same sched domain.
5984 */
5985static void init_sched_groups_power(int cpu, struct sched_domain *sd)
5986{
5987 struct sched_domain *child;
5988 struct sched_group *group;
5989
5990 WARN_ON(!sd || !sd->groups);
5991
5992 if (cpu != first_cpu(sd->groups->cpumask))
5993 return;
5994
5995 child = sd->child;
5996
Eric Dumazet5517d862007-05-08 00:32:57 -07005997 sd->groups->__cpu_power = 0;
5998
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005999 /*
6000 * For perf policy, if the groups in child domain share resources
6001 * (for example cores sharing some portions of the cache hierarchy
6002 * or SMT), then set this domain groups cpu_power such that each group
6003 * can handle only one task, when there are other idle groups in the
6004 * same sched domain.
6005 */
6006 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
6007 (child->flags &
6008 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07006009 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006010 return;
6011 }
6012
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006013 /*
6014 * add cpu_power of each child group to this groups cpu_power
6015 */
6016 group = child->groups;
6017 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07006018 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006019 group = group->next;
6020 } while (group != child->groups);
6021}
6022
6023/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006024 * Build sched domains for a given set of cpus and attach the sched domains
6025 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006027static int build_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028{
6029 int i;
John Hawkesd1b55132005-09-06 15:18:14 -07006030#ifdef CONFIG_NUMA
6031 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006032 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07006033
6034 /*
6035 * Allocate the per-node list of sched groups
6036 */
Ingo Molnardd41f592007-07-09 18:51:59 +02006037 sched_group_nodes = kzalloc(sizeof(struct sched_group *)*MAX_NUMNODES,
Srivatsa Vaddagirid3a5aa92006-06-27 02:54:39 -07006038 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07006039 if (!sched_group_nodes) {
6040 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006041 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07006042 }
6043 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
6044#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045
6046 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006047 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006049 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 struct sched_domain *sd = NULL, *p;
6051 cpumask_t nodemask = node_to_cpumask(cpu_to_node(i));
6052
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006053 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054
6055#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02006056 if (cpus_weight(*cpu_map) >
6057 SD_NODES_PER_DOMAIN*cpus_weight(nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07006058 sd = &per_cpu(allnodes_domains, i);
6059 *sd = SD_ALLNODES_INIT;
6060 sd->span = *cpu_map;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006061 cpu_to_allnodes_group(i, cpu_map, &sd->groups);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006062 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006063 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006064 } else
6065 p = NULL;
6066
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 sd = &per_cpu(node_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 *sd = SD_NODE_INIT;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006069 sd->span = sched_domain_node_span(cpu_to_node(i));
6070 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006071 if (p)
6072 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006073 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074#endif
6075
6076 p = sd;
6077 sd = &per_cpu(phys_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 *sd = SD_CPU_INIT;
6079 sd->span = nodemask;
6080 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006081 if (p)
6082 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006083 cpu_to_phys_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006085#ifdef CONFIG_SCHED_MC
6086 p = sd;
6087 sd = &per_cpu(core_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006088 *sd = SD_MC_INIT;
6089 sd->span = cpu_coregroup_map(i);
6090 cpus_and(sd->span, sd->span, *cpu_map);
6091 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006092 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006093 cpu_to_core_group(i, cpu_map, &sd->groups);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006094#endif
6095
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096#ifdef CONFIG_SCHED_SMT
6097 p = sd;
6098 sd = &per_cpu(cpu_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 *sd = SD_SIBLING_INIT;
6100 sd->span = cpu_sibling_map[i];
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006101 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006103 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006104 cpu_to_cpu_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105#endif
6106 }
6107
6108#ifdef CONFIG_SCHED_SMT
6109 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07006110 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 cpumask_t this_sibling_map = cpu_sibling_map[i];
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006112 cpus_and(this_sibling_map, this_sibling_map, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 if (i != first_cpu(this_sibling_map))
6114 continue;
6115
Ingo Molnardd41f592007-07-09 18:51:59 +02006116 init_sched_build_groups(this_sibling_map, cpu_map,
6117 &cpu_to_cpu_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 }
6119#endif
6120
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006121#ifdef CONFIG_SCHED_MC
6122 /* Set up multi-core groups */
6123 for_each_cpu_mask(i, *cpu_map) {
6124 cpumask_t this_core_map = cpu_coregroup_map(i);
6125 cpus_and(this_core_map, this_core_map, *cpu_map);
6126 if (i != first_cpu(this_core_map))
6127 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02006128 init_sched_build_groups(this_core_map, cpu_map,
6129 &cpu_to_core_group);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006130 }
6131#endif
6132
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 /* Set up physical groups */
6134 for (i = 0; i < MAX_NUMNODES; i++) {
6135 cpumask_t nodemask = node_to_cpumask(i);
6136
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006137 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 if (cpus_empty(nodemask))
6139 continue;
6140
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006141 init_sched_build_groups(nodemask, cpu_map, &cpu_to_phys_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 }
6143
6144#ifdef CONFIG_NUMA
6145 /* Set up node groups */
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006146 if (sd_allnodes)
Ingo Molnardd41f592007-07-09 18:51:59 +02006147 init_sched_build_groups(*cpu_map, cpu_map,
6148 &cpu_to_allnodes_group);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006149
6150 for (i = 0; i < MAX_NUMNODES; i++) {
6151 /* Set up node groups */
6152 struct sched_group *sg, *prev;
6153 cpumask_t nodemask = node_to_cpumask(i);
6154 cpumask_t domainspan;
6155 cpumask_t covered = CPU_MASK_NONE;
6156 int j;
6157
6158 cpus_and(nodemask, nodemask, *cpu_map);
John Hawkesd1b55132005-09-06 15:18:14 -07006159 if (cpus_empty(nodemask)) {
6160 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006161 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07006162 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006163
6164 domainspan = sched_domain_node_span(i);
6165 cpus_and(domainspan, domainspan, *cpu_map);
6166
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006167 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006168 if (!sg) {
6169 printk(KERN_WARNING "Can not alloc domain group for "
6170 "node %d\n", i);
6171 goto error;
6172 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006173 sched_group_nodes[i] = sg;
6174 for_each_cpu_mask(j, nodemask) {
6175 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02006176
John Hawkes9c1cfda2005-09-06 15:18:14 -07006177 sd = &per_cpu(node_domains, j);
6178 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006179 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006180 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006181 sg->cpumask = nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006182 sg->next = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006183 cpus_or(covered, covered, nodemask);
6184 prev = sg;
6185
6186 for (j = 0; j < MAX_NUMNODES; j++) {
6187 cpumask_t tmp, notcovered;
6188 int n = (i + j) % MAX_NUMNODES;
6189
6190 cpus_complement(notcovered, covered);
6191 cpus_and(tmp, notcovered, *cpu_map);
6192 cpus_and(tmp, tmp, domainspan);
6193 if (cpus_empty(tmp))
6194 break;
6195
6196 nodemask = node_to_cpumask(n);
6197 cpus_and(tmp, tmp, nodemask);
6198 if (cpus_empty(tmp))
6199 continue;
6200
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006201 sg = kmalloc_node(sizeof(struct sched_group),
6202 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006203 if (!sg) {
6204 printk(KERN_WARNING
6205 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006206 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006207 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006208 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006209 sg->cpumask = tmp;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006210 sg->next = prev->next;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006211 cpus_or(covered, covered, tmp);
6212 prev->next = sg;
6213 prev = sg;
6214 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216#endif
6217
6218 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006219#ifdef CONFIG_SCHED_SMT
6220 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006221 struct sched_domain *sd = &per_cpu(cpu_domains, i);
6222
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006223 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006224 }
6225#endif
6226#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006227 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006228 struct sched_domain *sd = &per_cpu(core_domains, i);
6229
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006230 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006231 }
6232#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006234 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006235 struct sched_domain *sd = &per_cpu(phys_domains, i);
6236
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006237 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 }
6239
John Hawkes9c1cfda2005-09-06 15:18:14 -07006240#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08006241 for (i = 0; i < MAX_NUMNODES; i++)
6242 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006243
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006244 if (sd_allnodes) {
6245 struct sched_group *sg;
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07006246
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006247 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg);
Siddha, Suresh Bf712c0c72006-07-30 03:02:59 -07006248 init_numa_sched_groups_power(sg);
6249 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006250#endif
6251
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006253 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254 struct sched_domain *sd;
6255#ifdef CONFIG_SCHED_SMT
6256 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006257#elif defined(CONFIG_SCHED_MC)
6258 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259#else
6260 sd = &per_cpu(phys_domains, i);
6261#endif
6262 cpu_attach_domain(sd, i);
6263 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006264
6265 return 0;
6266
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006267#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006268error:
6269 free_sched_groups(cpu_map);
6270 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006271#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272}
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006273/*
6274 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
6275 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006276static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006277{
6278 cpumask_t cpu_default_map;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006279 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006281 /*
6282 * Setup mask for cpus without special case scheduling requirements.
6283 * For now this just excludes isolated cpus, but could be used to
6284 * exclude other special cases in the future.
6285 */
6286 cpus_andnot(cpu_default_map, *cpu_map, cpu_isolated_map);
6287
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006288 err = build_sched_domains(&cpu_default_map);
6289
6290 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006291}
6292
6293static void arch_destroy_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294{
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006295 free_sched_groups(cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006296}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006298/*
6299 * Detach sched domains from a group of cpus specified in cpu_map
6300 * These cpus will now be attached to the NULL domain
6301 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08006302static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006303{
6304 int i;
6305
6306 for_each_cpu_mask(i, *cpu_map)
6307 cpu_attach_domain(NULL, i);
6308 synchronize_sched();
6309 arch_destroy_sched_domains(cpu_map);
6310}
6311
6312/*
6313 * Partition sched domains as specified by the cpumasks below.
6314 * This attaches all cpus from the cpumasks to the NULL domain,
6315 * waits for a RCU quiescent period, recalculates sched
6316 * domain information and then attaches them back to the
6317 * correct sched domains
6318 * Call with hotplug lock held
6319 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006320int partition_sched_domains(cpumask_t *partition1, cpumask_t *partition2)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006321{
6322 cpumask_t change_map;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006323 int err = 0;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006324
6325 cpus_and(*partition1, *partition1, cpu_online_map);
6326 cpus_and(*partition2, *partition2, cpu_online_map);
6327 cpus_or(change_map, *partition1, *partition2);
6328
6329 /* Detach sched domains from all of the affected cpus */
6330 detach_destroy_domains(&change_map);
6331 if (!cpus_empty(*partition1))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006332 err = build_sched_domains(partition1);
6333 if (!err && !cpus_empty(*partition2))
6334 err = build_sched_domains(partition2);
6335
6336 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006337}
6338
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006339#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
6340int arch_reinit_sched_domains(void)
6341{
6342 int err;
6343
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006344 mutex_lock(&sched_hotcpu_mutex);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006345 detach_destroy_domains(&cpu_online_map);
6346 err = arch_init_sched_domains(&cpu_online_map);
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006347 mutex_unlock(&sched_hotcpu_mutex);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006348
6349 return err;
6350}
6351
6352static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
6353{
6354 int ret;
6355
6356 if (buf[0] != '0' && buf[0] != '1')
6357 return -EINVAL;
6358
6359 if (smt)
6360 sched_smt_power_savings = (buf[0] == '1');
6361 else
6362 sched_mc_power_savings = (buf[0] == '1');
6363
6364 ret = arch_reinit_sched_domains();
6365
6366 return ret ? ret : count;
6367}
6368
6369int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
6370{
6371 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006372
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006373#ifdef CONFIG_SCHED_SMT
6374 if (smt_capable())
6375 err = sysfs_create_file(&cls->kset.kobj,
6376 &attr_sched_smt_power_savings.attr);
6377#endif
6378#ifdef CONFIG_SCHED_MC
6379 if (!err && mc_capable())
6380 err = sysfs_create_file(&cls->kset.kobj,
6381 &attr_sched_mc_power_savings.attr);
6382#endif
6383 return err;
6384}
6385#endif
6386
6387#ifdef CONFIG_SCHED_MC
6388static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
6389{
6390 return sprintf(page, "%u\n", sched_mc_power_savings);
6391}
Ingo Molnar48f24c42006-07-03 00:25:40 -07006392static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
6393 const char *buf, size_t count)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006394{
6395 return sched_power_savings_store(buf, count, 0);
6396}
6397SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
6398 sched_mc_power_savings_store);
6399#endif
6400
6401#ifdef CONFIG_SCHED_SMT
6402static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
6403{
6404 return sprintf(page, "%u\n", sched_smt_power_savings);
6405}
Ingo Molnar48f24c42006-07-03 00:25:40 -07006406static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
6407 const char *buf, size_t count)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006408{
6409 return sched_power_savings_store(buf, count, 1);
6410}
6411SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
6412 sched_smt_power_savings_store);
6413#endif
6414
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415/*
6416 * Force a reinitialization of the sched domains hierarchy. The domains
6417 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07006418 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 * which will prevent rebalancing while the sched domains are recalculated.
6420 */
6421static int update_sched_domains(struct notifier_block *nfb,
6422 unsigned long action, void *hcpu)
6423{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424 switch (action) {
6425 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006426 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006428 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006429 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430 return NOTIFY_OK;
6431
6432 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006433 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006435 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006437 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006439 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 /*
6441 * Fall through and re-initialise the domains.
6442 */
6443 break;
6444 default:
6445 return NOTIFY_DONE;
6446 }
6447
6448 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006449 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450
6451 return NOTIFY_OK;
6452}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453
6454void __init sched_init_smp(void)
6455{
Nick Piggin5c1e1762006-10-03 01:14:04 -07006456 cpumask_t non_isolated_cpus;
6457
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006458 mutex_lock(&sched_hotcpu_mutex);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006459 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08006460 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07006461 if (cpus_empty(non_isolated_cpus))
6462 cpu_set(smp_processor_id(), non_isolated_cpus);
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006463 mutex_unlock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464 /* XXX: Theoretical race here - CPU may be hotplugged now */
6465 hotcpu_notifier(update_sched_domains, 0);
Nick Piggin5c1e1762006-10-03 01:14:04 -07006466
Nick Piggine692ab52007-07-26 13:40:43 +02006467 init_sched_domain_sysctl();
6468
Nick Piggin5c1e1762006-10-03 01:14:04 -07006469 /* Move init over to a non-isolated CPU */
6470 if (set_cpus_allowed(current, non_isolated_cpus) < 0)
6471 BUG();
Ingo Molnardd41f592007-07-09 18:51:59 +02006472 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473}
6474#else
6475void __init sched_init_smp(void)
6476{
Ingo Molnardd41f592007-07-09 18:51:59 +02006477 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478}
6479#endif /* CONFIG_SMP */
6480
6481int in_sched_functions(unsigned long addr)
6482{
6483 /* Linker adds these: start and end of __sched functions */
6484 extern char __sched_text_start[], __sched_text_end[];
Ingo Molnar48f24c42006-07-03 00:25:40 -07006485
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486 return in_lock_functions(addr) ||
6487 (addr >= (unsigned long)__sched_text_start
6488 && addr < (unsigned long)__sched_text_end);
6489}
6490
Ingo Molnardd41f592007-07-09 18:51:59 +02006491static inline void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
6492{
6493 cfs_rq->tasks_timeline = RB_ROOT;
6494 cfs_rq->fair_clock = 1;
6495#ifdef CONFIG_FAIR_GROUP_SCHED
6496 cfs_rq->rq = rq;
6497#endif
6498}
6499
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500void __init sched_init(void)
6501{
Ingo Molnardd41f592007-07-09 18:51:59 +02006502 u64 now = sched_clock();
Christoph Lameter476f3532007-05-06 14:48:58 -07006503 int highest_cpu = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02006504 int i, j;
6505
6506 /*
6507 * Link up the scheduling class hierarchy:
6508 */
6509 rt_sched_class.next = &fair_sched_class;
6510 fair_sched_class.next = &idle_sched_class;
6511 idle_sched_class.next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08006513 for_each_possible_cpu(i) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006514 struct rt_prio_array *array;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006515 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516
6517 rq = cpu_rq(i);
6518 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07006519 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07006520 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02006521 rq->clock = 1;
6522 init_cfs_rq(&rq->cfs, rq);
6523#ifdef CONFIG_FAIR_GROUP_SCHED
6524 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
6525 list_add(&rq->cfs.leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
6526#endif
6527 rq->ls.load_update_last = now;
6528 rq->ls.load_update_start = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529
Ingo Molnardd41f592007-07-09 18:51:59 +02006530 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
6531 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07006533 rq->sd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02006535 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07006537 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538 rq->migration_thread = NULL;
6539 INIT_LIST_HEAD(&rq->migration_queue);
6540#endif
6541 atomic_set(&rq->nr_iowait, 0);
6542
Ingo Molnardd41f592007-07-09 18:51:59 +02006543 array = &rq->rt.active;
6544 for (j = 0; j < MAX_RT_PRIO; j++) {
6545 INIT_LIST_HEAD(array->queue + j);
6546 __clear_bit(j, array->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547 }
Christoph Lameter476f3532007-05-06 14:48:58 -07006548 highest_cpu = i;
Ingo Molnardd41f592007-07-09 18:51:59 +02006549 /* delimiter for bitsearch: */
6550 __set_bit(MAX_RT_PRIO, array->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551 }
6552
Peter Williams2dd73a42006-06-27 02:54:34 -07006553 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07006554
Avi Kivitye107be32007-07-26 13:40:43 +02006555#ifdef CONFIG_PREEMPT_NOTIFIERS
6556 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
6557#endif
6558
Christoph Lameterc9819f42006-12-10 02:20:25 -08006559#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006560 nr_cpu_ids = highest_cpu + 1;
Christoph Lameterc9819f42006-12-10 02:20:25 -08006561 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
6562#endif
6563
Heiko Carstensb50f60c2006-07-30 03:03:52 -07006564#ifdef CONFIG_RT_MUTEXES
6565 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
6566#endif
6567
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 /*
6569 * The boot idle thread does lazy MMU switching as well:
6570 */
6571 atomic_inc(&init_mm.mm_count);
6572 enter_lazy_tlb(&init_mm, current);
6573
6574 /*
6575 * Make us the idle thread. Technically, schedule() should not be
6576 * called from this thread, however somewhere below it might be,
6577 * but because we are the idle thread, we just pick up running again
6578 * when this runqueue becomes "idle".
6579 */
6580 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02006581 /*
6582 * During early bootup we pretend to be a normal task:
6583 */
6584 current->sched_class = &fair_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585}
6586
6587#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
6588void __might_sleep(char *file, int line)
6589{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006590#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591 static unsigned long prev_jiffy; /* ratelimiting */
6592
6593 if ((in_atomic() || irqs_disabled()) &&
6594 system_state == SYSTEM_RUNNING && !oops_in_progress) {
6595 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
6596 return;
6597 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08006598 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599 " context at %s:%d\n", file, line);
6600 printk("in_atomic():%d, irqs_disabled():%d\n",
6601 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08006602 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08006603 if (irqs_disabled())
6604 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 dump_stack();
6606 }
6607#endif
6608}
6609EXPORT_SYMBOL(__might_sleep);
6610#endif
6611
6612#ifdef CONFIG_MAGIC_SYSRQ
6613void normalize_rt_tasks(void)
6614{
Ingo Molnara0f98a12007-06-17 18:37:45 +02006615 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006617 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02006618 int on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619
6620 read_lock_irq(&tasklist_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02006621 do_each_thread(g, p) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006622 p->se.fair_key = 0;
6623 p->se.wait_runtime = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02006624 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02006625 p->se.wait_start_fair = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02006626 p->se.sleep_start_fair = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02006627#ifdef CONFIG_SCHEDSTATS
6628 p->se.wait_start = 0;
6629 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02006630 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02006631#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02006632 task_rq(p)->cfs.fair_clock = 0;
6633 task_rq(p)->clock = 0;
6634
6635 if (!rt_task(p)) {
6636 /*
6637 * Renice negative nice level userspace
6638 * tasks back to 0:
6639 */
6640 if (TASK_NICE(p) < 0 && p->mm)
6641 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02006643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644
Ingo Molnarb29739f2006-06-27 02:54:51 -07006645 spin_lock_irqsave(&p->pi_lock, flags);
6646 rq = __task_rq_lock(p);
Ingo Molnardd41f592007-07-09 18:51:59 +02006647#ifdef CONFIG_SMP
6648 /*
6649 * Do not touch the migration thread:
6650 */
6651 if (p == rq->migration_thread)
6652 goto out_unlock;
6653#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654
Ingo Molnardd41f592007-07-09 18:51:59 +02006655 on_rq = p->se.on_rq;
Ingo Molnara8e504d2007-08-09 11:16:47 +02006656 if (on_rq) {
6657 update_rq_clock(task_rq(p));
6658 deactivate_task(task_rq(p), p, 0, task_rq(p)->clock);
6659 }
Ingo Molnardd41f592007-07-09 18:51:59 +02006660 __setscheduler(rq, p, SCHED_NORMAL, 0);
6661 if (on_rq) {
6662 activate_task(task_rq(p), p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663 resched_task(rq->curr);
6664 }
Ingo Molnardd41f592007-07-09 18:51:59 +02006665#ifdef CONFIG_SMP
6666 out_unlock:
6667#endif
Ingo Molnarb29739f2006-06-27 02:54:51 -07006668 __task_rq_unlock(rq);
6669 spin_unlock_irqrestore(&p->pi_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02006670 } while_each_thread(g, p);
6671
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 read_unlock_irq(&tasklist_lock);
6673}
6674
6675#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07006676
6677#ifdef CONFIG_IA64
6678/*
6679 * These functions are only useful for the IA64 MCA handling.
6680 *
6681 * They can only be called when the whole system has been
6682 * stopped - every CPU needs to be quiescent, and no scheduling
6683 * activity can take place. Using them for anything else would
6684 * be a serious bug, and as a result, they aren't even visible
6685 * under any other configuration.
6686 */
6687
6688/**
6689 * curr_task - return the current task for a given cpu.
6690 * @cpu: the processor in question.
6691 *
6692 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
6693 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006694struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07006695{
6696 return cpu_curr(cpu);
6697}
6698
6699/**
6700 * set_curr_task - set the current task for a given cpu.
6701 * @cpu: the processor in question.
6702 * @p: the task pointer to set.
6703 *
6704 * Description: This function must only be used when non-maskable interrupts
6705 * are serviced on a separate stack. It allows the architecture to switch the
6706 * notion of the current task on a cpu in a non-blocking manner. This function
6707 * must be called with all CPU's synchronized, and interrupts disabled, the
6708 * and caller must save the original value of the current task (see
6709 * curr_task() above) and restore that value before reenabling interrupts and
6710 * re-starting the system.
6711 *
6712 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
6713 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006714void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07006715{
6716 cpu_curr(cpu) = p;
6717}
6718
6719#endif