blob: d613723f324fd3f056483926b933cfcb4a9f968c [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
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200371static u64 rq_clock(struct rq *rq)
372{
373 update_rq_clock(rq);
374 return rq->clock;
375}
376
Ingo Molnar20d315d2007-07-09 18:51:58 +0200377/*
Nick Piggin674311d2005-06-25 14:57:27 -0700378 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700379 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700380 *
381 * The domain tree of any CPU may only be accessed from within
382 * preempt-disabled sections.
383 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700384#define for_each_domain(cpu, __sd) \
385 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
388#define this_rq() (&__get_cpu_var(runqueues))
389#define task_rq(p) cpu_rq(task_cpu(p))
390#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
391
Ingo Molnare436d802007-07-19 21:28:35 +0200392/*
393 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
394 * clock constructed from sched_clock():
395 */
396unsigned long long cpu_clock(int cpu)
397{
Ingo Molnare436d802007-07-19 21:28:35 +0200398 unsigned long long now;
399 unsigned long flags;
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200400 struct rq *rq;
Ingo Molnare436d802007-07-19 21:28:35 +0200401
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200402 local_irq_save(flags);
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200403 rq = cpu_rq(cpu);
404 update_rq_clock(rq);
405 now = rq->clock;
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200406 local_irq_restore(flags);
Ingo Molnare436d802007-07-19 21:28:35 +0200407
408 return now;
409}
410
Ingo Molnar138a8ae2007-07-09 18:51:58 +0200411#ifdef CONFIG_FAIR_GROUP_SCHED
412/* Change a task's ->cfs_rq if it moves across CPUs */
413static inline void set_task_cfs_rq(struct task_struct *p)
414{
415 p->se.cfs_rq = &task_rq(p)->cfs;
416}
417#else
418static inline void set_task_cfs_rq(struct task_struct *p)
419{
420}
421#endif
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700424# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700426#ifndef finish_arch_switch
427# define finish_arch_switch(prev) do { } while (0)
428#endif
429
430#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700431static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700432{
433 return rq->curr == p;
434}
435
Ingo Molnar70b97a72006-07-03 00:25:42 -0700436static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700437{
438}
439
Ingo Molnar70b97a72006-07-03 00:25:42 -0700440static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700441{
Ingo Molnarda04c032005-09-13 11:17:59 +0200442#ifdef CONFIG_DEBUG_SPINLOCK
443 /* this is a valid case when another task releases the spinlock */
444 rq->lock.owner = current;
445#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700446 /*
447 * If we are tracking spinlock dependencies then we have to
448 * fix up the runqueue lock - which gets 'carried over' from
449 * prev into current:
450 */
451 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
452
Nick Piggin4866cde2005-06-25 14:57:23 -0700453 spin_unlock_irq(&rq->lock);
454}
455
456#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700457static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700458{
459#ifdef CONFIG_SMP
460 return p->oncpu;
461#else
462 return rq->curr == p;
463#endif
464}
465
Ingo Molnar70b97a72006-07-03 00:25:42 -0700466static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700467{
468#ifdef CONFIG_SMP
469 /*
470 * We can optimise this out completely for !SMP, because the
471 * SMP rebalancing from interrupt is the only thing that cares
472 * here.
473 */
474 next->oncpu = 1;
475#endif
476#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
477 spin_unlock_irq(&rq->lock);
478#else
479 spin_unlock(&rq->lock);
480#endif
481}
482
Ingo Molnar70b97a72006-07-03 00:25:42 -0700483static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700484{
485#ifdef CONFIG_SMP
486 /*
487 * After ->oncpu is cleared, the task can be moved to a different CPU.
488 * We must ensure this doesn't happen until the switch is completely
489 * finished.
490 */
491 smp_wmb();
492 prev->oncpu = 0;
493#endif
494#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
495 local_irq_enable();
496#endif
497}
498#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700501 * __task_rq_lock - lock the runqueue a given task resides on.
502 * Must be called interrupts disabled.
503 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700504static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700505 __acquires(rq->lock)
506{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700507 struct rq *rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700508
509repeat_lock_task:
510 rq = task_rq(p);
511 spin_lock(&rq->lock);
512 if (unlikely(rq != task_rq(p))) {
513 spin_unlock(&rq->lock);
514 goto repeat_lock_task;
515 }
516 return rq;
517}
518
519/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 * task_rq_lock - lock the runqueue a given task resides on and disable
521 * interrupts. Note the ordering: we can safely lookup the task_rq without
522 * explicitly disabling preemption.
523 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700524static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 __acquires(rq->lock)
526{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700527 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529repeat_lock_task:
530 local_irq_save(*flags);
531 rq = task_rq(p);
532 spin_lock(&rq->lock);
533 if (unlikely(rq != task_rq(p))) {
534 spin_unlock_irqrestore(&rq->lock, *flags);
535 goto repeat_lock_task;
536 }
537 return rq;
538}
539
Ingo Molnar70b97a72006-07-03 00:25:42 -0700540static inline void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700541 __releases(rq->lock)
542{
543 spin_unlock(&rq->lock);
544}
545
Ingo Molnar70b97a72006-07-03 00:25:42 -0700546static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 __releases(rq->lock)
548{
549 spin_unlock_irqrestore(&rq->lock, *flags);
550}
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800553 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700555static inline struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 __acquires(rq->lock)
557{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700558 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 local_irq_disable();
561 rq = this_rq();
562 spin_lock(&rq->lock);
563
564 return rq;
565}
566
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200567/*
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200568 * CPU frequency is/was unstable - start new by setting prev_clock_raw:
569 */
570void sched_clock_unstable_event(void)
571{
572 unsigned long flags;
573 struct rq *rq;
574
575 rq = task_rq_lock(current, &flags);
576 rq->prev_clock_raw = sched_clock();
577 rq->clock_unstable_events++;
578 task_rq_unlock(rq, &flags);
579}
580
581/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200582 * resched_task - mark a task 'to be rescheduled now'.
583 *
584 * On UP this means the setting of the need_resched flag, on SMP it
585 * might also involve a cross-CPU call to trigger the scheduler on
586 * the target CPU.
587 */
588#ifdef CONFIG_SMP
589
590#ifndef tsk_is_polling
591#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
592#endif
593
594static void resched_task(struct task_struct *p)
595{
596 int cpu;
597
598 assert_spin_locked(&task_rq(p)->lock);
599
600 if (unlikely(test_tsk_thread_flag(p, TIF_NEED_RESCHED)))
601 return;
602
603 set_tsk_thread_flag(p, TIF_NEED_RESCHED);
604
605 cpu = task_cpu(p);
606 if (cpu == smp_processor_id())
607 return;
608
609 /* NEED_RESCHED must be visible before we test polling */
610 smp_mb();
611 if (!tsk_is_polling(p))
612 smp_send_reschedule(cpu);
613}
614
615static void resched_cpu(int cpu)
616{
617 struct rq *rq = cpu_rq(cpu);
618 unsigned long flags;
619
620 if (!spin_trylock_irqsave(&rq->lock, flags))
621 return;
622 resched_task(cpu_curr(cpu));
623 spin_unlock_irqrestore(&rq->lock, flags);
624}
625#else
626static inline void resched_task(struct task_struct *p)
627{
628 assert_spin_locked(&task_rq(p)->lock);
629 set_tsk_need_resched(p);
630}
631#endif
632
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200633static u64 div64_likely32(u64 divident, unsigned long divisor)
634{
635#if BITS_PER_LONG == 32
636 if (likely(divident <= 0xffffffffULL))
637 return (u32)divident / divisor;
638 do_div(divident, divisor);
639
640 return divident;
641#else
642 return divident / divisor;
643#endif
644}
645
646#if BITS_PER_LONG == 32
647# define WMULT_CONST (~0UL)
648#else
649# define WMULT_CONST (1UL << 32)
650#endif
651
652#define WMULT_SHIFT 32
653
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +0200654static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200655calc_delta_mine(unsigned long delta_exec, unsigned long weight,
656 struct load_weight *lw)
657{
658 u64 tmp;
659
660 if (unlikely(!lw->inv_weight))
661 lw->inv_weight = WMULT_CONST / lw->weight;
662
663 tmp = (u64)delta_exec * weight;
664 /*
665 * Check whether we'd overflow the 64-bit multiplication:
666 */
667 if (unlikely(tmp > WMULT_CONST)) {
668 tmp = ((tmp >> WMULT_SHIFT/2) * lw->inv_weight)
669 >> (WMULT_SHIFT/2);
670 } else {
671 tmp = (tmp * lw->inv_weight) >> WMULT_SHIFT;
672 }
673
Ingo Molnarecf691d2007-08-02 17:41:40 +0200674 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200675}
676
677static inline unsigned long
678calc_delta_fair(unsigned long delta_exec, struct load_weight *lw)
679{
680 return calc_delta_mine(delta_exec, NICE_0_LOAD, lw);
681}
682
683static void update_load_add(struct load_weight *lw, unsigned long inc)
684{
685 lw->weight += inc;
686 lw->inv_weight = 0;
687}
688
689static void update_load_sub(struct load_weight *lw, unsigned long dec)
690{
691 lw->weight -= dec;
692 lw->inv_weight = 0;
693}
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695/*
Peter Williams2dd73a42006-06-27 02:54:34 -0700696 * To aid in avoiding the subversion of "niceness" due to uneven distribution
697 * of tasks with abnormal "nice" values across CPUs the contribution that
698 * each task makes to its run queue's load is weighted according to its
699 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
700 * scaled version of the new time slice allocation that they receive on time
701 * slice expiry etc.
702 */
703
Ingo Molnardd41f592007-07-09 18:51:59 +0200704#define WEIGHT_IDLEPRIO 2
705#define WMULT_IDLEPRIO (1 << 31)
706
707/*
708 * Nice levels are multiplicative, with a gentle 10% change for every
709 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
710 * nice 1, it will get ~10% less CPU time than another CPU-bound task
711 * that remained on nice 0.
712 *
713 * The "10% effect" is relative and cumulative: from _any_ nice level,
714 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +0200715 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
716 * If a task goes up by ~10% and another task goes down by ~10% then
717 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +0200718 */
719static const int prio_to_weight[40] = {
720/* -20 */ 88818, 71054, 56843, 45475, 36380, 29104, 23283, 18626, 14901, 11921,
721/* -10 */ 9537, 7629, 6103, 4883, 3906, 3125, 2500, 2000, 1600, 1280,
722/* 0 */ NICE_0_LOAD /* 1024 */,
723/* 1 */ 819, 655, 524, 419, 336, 268, 215, 172, 137,
724/* 10 */ 110, 87, 70, 56, 45, 36, 29, 23, 18, 15,
725};
726
Ingo Molnar5714d2d2007-07-16 09:46:31 +0200727/*
728 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
729 *
730 * In cases where the weight does not change often, we can use the
731 * precalculated inverse to speed up arithmetics by turning divisions
732 * into multiplications:
733 */
Ingo Molnardd41f592007-07-09 18:51:59 +0200734static const u32 prio_to_wmult[40] = {
Ingo Molnare4af30b2007-07-16 09:46:31 +0200735/* -20 */ 48356, 60446, 75558, 94446, 118058,
736/* -15 */ 147573, 184467, 230589, 288233, 360285,
737/* -10 */ 450347, 562979, 703746, 879575, 1099582,
738/* -5 */ 1374389, 1717986, 2147483, 2684354, 3355443,
739/* 0 */ 4194304, 5244160, 6557201, 8196502, 10250518,
740/* 5 */ 12782640, 16025997, 19976592, 24970740, 31350126,
741/* 10 */ 39045157, 49367440, 61356675, 76695844, 95443717,
742/* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +0200743};
Peter Williams2dd73a42006-06-27 02:54:34 -0700744
Ingo Molnardd41f592007-07-09 18:51:59 +0200745static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
746
747/*
748 * runqueue iterator, to support SMP load-balancing between different
749 * scheduling classes, without having to expose their internal data
750 * structures to the load-balancing proper:
751 */
752struct rq_iterator {
753 void *arg;
754 struct task_struct *(*start)(void *);
755 struct task_struct *(*next)(void *);
756};
757
758static int balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
759 unsigned long max_nr_move, unsigned long max_load_move,
760 struct sched_domain *sd, enum cpu_idle_type idle,
761 int *all_pinned, unsigned long *load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +0200762 int *this_best_prio, struct rq_iterator *iterator);
Ingo Molnardd41f592007-07-09 18:51:59 +0200763
764#include "sched_stats.h"
765#include "sched_rt.c"
766#include "sched_fair.c"
767#include "sched_idletask.c"
768#ifdef CONFIG_SCHED_DEBUG
769# include "sched_debug.c"
770#endif
771
772#define sched_class_highest (&rt_sched_class)
773
Ingo Molnar9c217242007-08-02 17:41:40 +0200774static void __update_curr_load(struct rq *rq, struct load_stat *ls)
775{
776 if (rq->curr != rq->idle && ls->load.weight) {
777 ls->delta_exec += ls->delta_stat;
778 ls->delta_fair += calc_delta_fair(ls->delta_stat, &ls->load);
779 ls->delta_stat = 0;
780 }
781}
782
783/*
784 * Update delta_exec, delta_fair fields for rq.
785 *
786 * delta_fair clock advances at a rate inversely proportional to
787 * total load (rq->ls.load.weight) on the runqueue, while
788 * delta_exec advances at the same rate as wall-clock (provided
789 * cpu is not idle).
790 *
791 * delta_exec / delta_fair is a measure of the (smoothened) load on this
792 * runqueue over any given interval. This (smoothened) load is used
793 * during load balance.
794 *
795 * This function is called /before/ updating rq->ls.load
796 * and when switching tasks.
797 */
798static void update_curr_load(struct rq *rq, u64 now)
799{
800 struct load_stat *ls = &rq->ls;
801 u64 start;
802
803 start = ls->load_update_start;
804 ls->load_update_start = now;
805 ls->delta_stat += now - start;
806 /*
807 * Stagger updates to ls->delta_fair. Very frequent updates
808 * can be expensive.
809 */
810 if (ls->delta_stat >= sysctl_sched_stat_granularity)
811 __update_curr_load(rq, ls);
812}
813
814static inline void
815inc_load(struct rq *rq, const struct task_struct *p, u64 now)
816{
817 update_curr_load(rq, now);
818 update_load_add(&rq->ls.load, p->se.load.weight);
819}
820
821static inline void
822dec_load(struct rq *rq, const struct task_struct *p, u64 now)
823{
824 update_curr_load(rq, now);
825 update_load_sub(&rq->ls.load, p->se.load.weight);
826}
827
828static void inc_nr_running(struct task_struct *p, struct rq *rq, u64 now)
829{
830 rq->nr_running++;
831 inc_load(rq, p, now);
832}
833
834static void dec_nr_running(struct task_struct *p, struct rq *rq, u64 now)
835{
836 rq->nr_running--;
837 dec_load(rq, p, now);
838}
839
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200840static void set_load_weight(struct task_struct *p)
841{
Ingo Molnardd41f592007-07-09 18:51:59 +0200842 task_rq(p)->cfs.wait_runtime -= p->se.wait_runtime;
843 p->se.wait_runtime = 0;
844
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200845 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +0200846 p->se.load.weight = prio_to_weight[0] * 2;
847 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
848 return;
849 }
850
851 /*
852 * SCHED_IDLE tasks get minimal weight:
853 */
854 if (p->policy == SCHED_IDLE) {
855 p->se.load.weight = WEIGHT_IDLEPRIO;
856 p->se.load.inv_weight = WMULT_IDLEPRIO;
857 return;
858 }
859
860 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
861 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200862}
863
Ingo Molnardd41f592007-07-09 18:51:59 +0200864static void
865enqueue_task(struct rq *rq, struct task_struct *p, int wakeup, u64 now)
Ingo Molnar71f8bd42007-07-09 18:51:59 +0200866{
867 sched_info_queued(p);
Ingo Molnardd41f592007-07-09 18:51:59 +0200868 p->sched_class->enqueue_task(rq, p, wakeup, now);
869 p->se.on_rq = 1;
870}
871
872static void
873dequeue_task(struct rq *rq, struct task_struct *p, int sleep, u64 now)
874{
875 p->sched_class->dequeue_task(rq, p, sleep, now);
876 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +0200877}
878
879/*
Ingo Molnardd41f592007-07-09 18:51:59 +0200880 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +0200881 */
Ingo Molnar14531182007-07-09 18:51:59 +0200882static inline int __normal_prio(struct task_struct *p)
883{
Ingo Molnardd41f592007-07-09 18:51:59 +0200884 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +0200885}
886
887/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700888 * Calculate the expected normal priority: i.e. priority
889 * without taking RT-inheritance into account. Might be
890 * boosted by interactivity modifiers. Changes upon fork,
891 * setprio syscalls, and whenever the interactivity
892 * estimator recalculates.
893 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700894static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700895{
896 int prio;
897
Ingo Molnare05606d2007-07-09 18:51:59 +0200898 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -0700899 prio = MAX_RT_PRIO-1 - p->rt_priority;
900 else
901 prio = __normal_prio(p);
902 return prio;
903}
904
905/*
906 * Calculate the current priority, i.e. the priority
907 * taken into account by the scheduler. This value might
908 * be boosted by RT tasks, or might be boosted by
909 * interactivity modifiers. Will be RT if the task got
910 * RT-boosted. If not then it returns p->normal_prio.
911 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700912static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700913{
914 p->normal_prio = normal_prio(p);
915 /*
916 * If we are RT tasks or we were boosted to RT priority,
917 * keep the priority unchanged. Otherwise, update priority
918 * to the normal priority:
919 */
920 if (!rt_prio(p->prio))
921 return p->normal_prio;
922 return p->prio;
923}
924
925/*
Ingo Molnardd41f592007-07-09 18:51:59 +0200926 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 */
Ingo Molnardd41f592007-07-09 18:51:59 +0200928static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Ingo Molnardd41f592007-07-09 18:51:59 +0200930 u64 now = rq_clock(rq);
Con Kolivasd425b272006-03-31 02:31:29 -0800931
Ingo Molnardd41f592007-07-09 18:51:59 +0200932 if (p->state == TASK_UNINTERRUPTIBLE)
933 rq->nr_uninterruptible--;
934
935 enqueue_task(rq, p, wakeup, now);
936 inc_nr_running(p, rq, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
939/*
Ingo Molnardd41f592007-07-09 18:51:59 +0200940 * activate_idle_task - move idle task to the _front_ of runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 */
Ingo Molnardd41f592007-07-09 18:51:59 +0200942static inline void activate_idle_task(struct task_struct *p, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Ingo Molnardd41f592007-07-09 18:51:59 +0200944 u64 now = rq_clock(rq);
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 Viroa1261f52005-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 Molnarcad60d92007-08-02 17:41:40 +02001667 now = rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 p->prio = effective_prio(p);
1670
Ingo Molnarcad60d92007-08-02 17:41:40 +02001671 if (!p->sched_class->task_new || !sysctl_sched_child_runs_first ||
1672 (clone_flags & CLONE_VM) || task_cpu(p) != this_cpu ||
1673 !current->se.on_rq) {
1674
Ingo Molnardd41f592007-07-09 18:51:59 +02001675 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02001678 * Let the scheduling class do new task startup
1679 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 */
Ingo Molnarcad60d92007-08-02 17:41:40 +02001681 p->sched_class->task_new(rq, p, now);
1682 inc_nr_running(p, rq, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 }
Ingo Molnardd41f592007-07-09 18:51:59 +02001684 check_preempt_curr(rq, p);
1685 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
Avi Kivitye107be32007-07-26 13:40:43 +02001688#ifdef CONFIG_PREEMPT_NOTIFIERS
1689
1690/**
Randy Dunlap421cee22007-07-31 00:37:50 -07001691 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
1692 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02001693 */
1694void preempt_notifier_register(struct preempt_notifier *notifier)
1695{
1696 hlist_add_head(&notifier->link, &current->preempt_notifiers);
1697}
1698EXPORT_SYMBOL_GPL(preempt_notifier_register);
1699
1700/**
1701 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07001702 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02001703 *
1704 * This is safe to call from within a preemption notifier.
1705 */
1706void preempt_notifier_unregister(struct preempt_notifier *notifier)
1707{
1708 hlist_del(&notifier->link);
1709}
1710EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
1711
1712static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
1713{
1714 struct preempt_notifier *notifier;
1715 struct hlist_node *node;
1716
1717 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
1718 notifier->ops->sched_in(notifier, raw_smp_processor_id());
1719}
1720
1721static void
1722fire_sched_out_preempt_notifiers(struct task_struct *curr,
1723 struct task_struct *next)
1724{
1725 struct preempt_notifier *notifier;
1726 struct hlist_node *node;
1727
1728 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
1729 notifier->ops->sched_out(notifier, next);
1730}
1731
1732#else
1733
1734static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
1735{
1736}
1737
1738static void
1739fire_sched_out_preempt_notifiers(struct task_struct *curr,
1740 struct task_struct *next)
1741{
1742}
1743
1744#endif
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746/**
Nick Piggin4866cde2005-06-25 14:57:23 -07001747 * prepare_task_switch - prepare to switch tasks
1748 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07001749 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07001750 * @next: the task we are going to switch to.
1751 *
1752 * This is called with the rq lock held and interrupts off. It must
1753 * be paired with a subsequent finish_task_switch after the context
1754 * switch.
1755 *
1756 * prepare_task_switch sets up locking and calls architecture specific
1757 * hooks.
1758 */
Avi Kivitye107be32007-07-26 13:40:43 +02001759static inline void
1760prepare_task_switch(struct rq *rq, struct task_struct *prev,
1761 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07001762{
Avi Kivitye107be32007-07-26 13:40:43 +02001763 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07001764 prepare_lock_switch(rq, next);
1765 prepare_arch_switch(next);
1766}
1767
1768/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04001770 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 * @prev: the thread we just switched away from.
1772 *
Nick Piggin4866cde2005-06-25 14:57:23 -07001773 * finish_task_switch must be called after the context switch, paired
1774 * with a prepare_task_switch call before the context switch.
1775 * finish_task_switch will reconcile locking set up by prepare_task_switch,
1776 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 *
1778 * Note that we may have delayed dropping an mm in context_switch(). If
1779 * so, we finish that here outside of the runqueue lock. (Doing it
1780 * with the lock held can cause deadlocks; see schedule() for
1781 * details.)
1782 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07001783static inline void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 __releases(rq->lock)
1785{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07001787 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 rq->prev_mm = NULL;
1790
1791 /*
1792 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07001793 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07001794 * schedule one last time. The schedule call will never return, and
1795 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07001796 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 * still held, otherwise prev could be scheduled on another cpu, die
1798 * there before we look at prev->state, and then the reference would
1799 * be dropped twice.
1800 * Manfred Spraul <manfred@colorfullife.com>
1801 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07001802 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07001803 finish_arch_switch(prev);
1804 finish_lock_switch(rq, prev);
Avi Kivitye107be32007-07-26 13:40:43 +02001805 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 if (mm)
1807 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07001808 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08001809 /*
1810 * Remove function-return probe instances associated with this
1811 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02001812 */
bibo maoc6fd91f2006-03-26 01:38:20 -08001813 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08001815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
1818/**
1819 * schedule_tail - first thing a freshly forked thread must call.
1820 * @prev: the thread we just switched away from.
1821 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001822asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 __releases(rq->lock)
1824{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001825 struct rq *rq = this_rq();
1826
Nick Piggin4866cde2005-06-25 14:57:23 -07001827 finish_task_switch(rq, prev);
1828#ifdef __ARCH_WANT_UNLOCKED_CTXSW
1829 /* In this case, finish_task_switch does not reenable preemption */
1830 preempt_enable();
1831#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 if (current->set_child_tid)
1833 put_user(current->pid, current->set_child_tid);
1834}
1835
1836/*
1837 * context_switch - switch to the new MM and the new
1838 * thread's register state.
1839 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001840static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07001841context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07001842 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
Ingo Molnardd41f592007-07-09 18:51:59 +02001844 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Avi Kivitye107be32007-07-26 13:40:43 +02001846 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02001847 mm = next->mm;
1848 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01001849 /*
1850 * For paravirt, this is coupled with an exit in switch_to to
1851 * combine the page table reload and the switch backend into
1852 * one hypercall.
1853 */
1854 arch_enter_lazy_cpu_mode();
1855
Ingo Molnardd41f592007-07-09 18:51:59 +02001856 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 next->active_mm = oldmm;
1858 atomic_inc(&oldmm->mm_count);
1859 enter_lazy_tlb(oldmm, next);
1860 } else
1861 switch_mm(oldmm, mm, next);
1862
Ingo Molnardd41f592007-07-09 18:51:59 +02001863 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 rq->prev_mm = oldmm;
1866 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07001867 /*
1868 * Since the runqueue lock will be released by the next
1869 * task (which is an invalid locking op but in the case
1870 * of the scheduler it's an obvious special-case), so we
1871 * do an early lockdep release here:
1872 */
1873#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07001874 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07001875#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 /* Here we just switch the register state and the stack. */
1878 switch_to(prev, next, prev);
1879
Ingo Molnardd41f592007-07-09 18:51:59 +02001880 barrier();
1881 /*
1882 * this_rq must be evaluated again because prev may have moved
1883 * CPUs since it called schedule(), thus the 'rq' on its stack
1884 * frame will be invalid.
1885 */
1886 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887}
1888
1889/*
1890 * nr_running, nr_uninterruptible and nr_context_switches:
1891 *
1892 * externally visible scheduler statistics: current number of runnable
1893 * threads, current number of uninterruptible-sleeping threads, total
1894 * number of context switches performed since bootup.
1895 */
1896unsigned long nr_running(void)
1897{
1898 unsigned long i, sum = 0;
1899
1900 for_each_online_cpu(i)
1901 sum += cpu_rq(i)->nr_running;
1902
1903 return sum;
1904}
1905
1906unsigned long nr_uninterruptible(void)
1907{
1908 unsigned long i, sum = 0;
1909
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001910 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 sum += cpu_rq(i)->nr_uninterruptible;
1912
1913 /*
1914 * Since we read the counters lockless, it might be slightly
1915 * inaccurate. Do not allow it to go below zero though:
1916 */
1917 if (unlikely((long)sum < 0))
1918 sum = 0;
1919
1920 return sum;
1921}
1922
1923unsigned long long nr_context_switches(void)
1924{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07001925 int i;
1926 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001928 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 sum += cpu_rq(i)->nr_switches;
1930
1931 return sum;
1932}
1933
1934unsigned long nr_iowait(void)
1935{
1936 unsigned long i, sum = 0;
1937
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001938 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 sum += atomic_read(&cpu_rq(i)->nr_iowait);
1940
1941 return sum;
1942}
1943
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08001944unsigned long nr_active(void)
1945{
1946 unsigned long i, running = 0, uninterruptible = 0;
1947
1948 for_each_online_cpu(i) {
1949 running += cpu_rq(i)->nr_running;
1950 uninterruptible += cpu_rq(i)->nr_uninterruptible;
1951 }
1952
1953 if (unlikely((long)uninterruptible < 0))
1954 uninterruptible = 0;
1955
1956 return running + uninterruptible;
1957}
1958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001960 * Update rq->cpu_load[] statistics. This function is usually called every
1961 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07001962 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001963static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07001964{
Ingo Molnardd41f592007-07-09 18:51:59 +02001965 u64 fair_delta64, exec_delta64, idle_delta64, sample_interval64, tmp64;
1966 unsigned long total_load = this_rq->ls.load.weight;
1967 unsigned long this_load = total_load;
1968 struct load_stat *ls = &this_rq->ls;
1969 u64 now = __rq_clock(this_rq);
1970 int i, scale;
1971
1972 this_rq->nr_load_updates++;
1973 if (unlikely(!(sysctl_sched_features & SCHED_FEAT_PRECISE_CPU_LOAD)))
1974 goto do_avg;
1975
1976 /* Update delta_fair/delta_exec fields first */
1977 update_curr_load(this_rq, now);
1978
1979 fair_delta64 = ls->delta_fair + 1;
1980 ls->delta_fair = 0;
1981
1982 exec_delta64 = ls->delta_exec + 1;
1983 ls->delta_exec = 0;
1984
1985 sample_interval64 = now - ls->load_update_last;
1986 ls->load_update_last = now;
1987
1988 if ((s64)sample_interval64 < (s64)TICK_NSEC)
1989 sample_interval64 = TICK_NSEC;
1990
1991 if (exec_delta64 > sample_interval64)
1992 exec_delta64 = sample_interval64;
1993
1994 idle_delta64 = sample_interval64 - exec_delta64;
1995
1996 tmp64 = div64_64(SCHED_LOAD_SCALE * exec_delta64, fair_delta64);
1997 tmp64 = div64_64(tmp64 * exec_delta64, sample_interval64);
1998
1999 this_load = (unsigned long)tmp64;
2000
2001do_avg:
2002
2003 /* Update our load: */
2004 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
2005 unsigned long old_load, new_load;
2006
2007 /* scale is effectively 1 << i now, and >> i divides by scale */
2008
2009 old_load = this_rq->cpu_load[i];
2010 new_load = this_load;
2011
2012 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
2013 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002014}
2015
Ingo Molnardd41f592007-07-09 18:51:59 +02002016#ifdef CONFIG_SMP
2017
Ingo Molnar48f24c42006-07-03 00:25:40 -07002018/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 * double_rq_lock - safely lock two runqueues
2020 *
2021 * Note this does not disable interrupts like task_rq_lock,
2022 * you need to do so manually before calling.
2023 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002024static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 __acquires(rq1->lock)
2026 __acquires(rq2->lock)
2027{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002028 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 if (rq1 == rq2) {
2030 spin_lock(&rq1->lock);
2031 __acquire(rq2->lock); /* Fake it out ;) */
2032 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002033 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 spin_lock(&rq1->lock);
2035 spin_lock(&rq2->lock);
2036 } else {
2037 spin_lock(&rq2->lock);
2038 spin_lock(&rq1->lock);
2039 }
2040 }
2041}
2042
2043/*
2044 * double_rq_unlock - safely unlock two runqueues
2045 *
2046 * Note this does not restore interrupts like task_rq_unlock,
2047 * you need to do so manually after calling.
2048 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002049static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 __releases(rq1->lock)
2051 __releases(rq2->lock)
2052{
2053 spin_unlock(&rq1->lock);
2054 if (rq1 != rq2)
2055 spin_unlock(&rq2->lock);
2056 else
2057 __release(rq2->lock);
2058}
2059
2060/*
2061 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2062 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002063static void double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 __releases(this_rq->lock)
2065 __acquires(busiest->lock)
2066 __acquires(this_rq->lock)
2067{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002068 if (unlikely(!irqs_disabled())) {
2069 /* printk() doesn't work good under rq->lock */
2070 spin_unlock(&this_rq->lock);
2071 BUG_ON(1);
2072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002074 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 spin_unlock(&this_rq->lock);
2076 spin_lock(&busiest->lock);
2077 spin_lock(&this_rq->lock);
2078 } else
2079 spin_lock(&busiest->lock);
2080 }
2081}
2082
2083/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 * If dest_cpu is allowed for this process, migrate the task to it.
2085 * This is accomplished by forcing the cpu_allowed mask to only
2086 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
2087 * the cpu_allowed mask is restored.
2088 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002089static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002091 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002093 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 rq = task_rq_lock(p, &flags);
2096 if (!cpu_isset(dest_cpu, p->cpus_allowed)
2097 || unlikely(cpu_is_offline(dest_cpu)))
2098 goto out;
2099
2100 /* force the process onto the specified CPU */
2101 if (migrate_task(p, dest_cpu, &req)) {
2102 /* Need to wait for migration thread (might exit: take ref). */
2103 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07002104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 get_task_struct(mt);
2106 task_rq_unlock(rq, &flags);
2107 wake_up_process(mt);
2108 put_task_struct(mt);
2109 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07002110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return;
2112 }
2113out:
2114 task_rq_unlock(rq, &flags);
2115}
2116
2117/*
Nick Piggin476d1392005-06-25 14:57:29 -07002118 * sched_exec - execve() is a valuable balancing opportunity, because at
2119 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 */
2121void sched_exec(void)
2122{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002124 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002126 if (new_cpu != this_cpu)
2127 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129
2130/*
2131 * pull_task - move a task from a remote runqueue to the local runqueue.
2132 * Both runqueues must be locked.
2133 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002134static void pull_task(struct rq *src_rq, struct task_struct *p,
2135 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
Ingo Molnar8e717b12007-08-09 11:16:46 +02002137 deactivate_task(src_rq, p, 0, rq_clock(src_rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002139 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 /*
2141 * Note that idle threads have a prio of MAX_PRIO, for this test
2142 * to be always true for them.
2143 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002144 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
2146
2147/*
2148 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
2149 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002150static
Ingo Molnar70b97a72006-07-03 00:25:42 -07002151int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002152 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002153 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
2155 /*
2156 * We do not migrate tasks that are:
2157 * 1) running (obviously), or
2158 * 2) cannot be migrated to this CPU due to cpus_allowed, or
2159 * 3) are cache-hot on their current CPU.
2160 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 if (!cpu_isset(this_cpu, p->cpus_allowed))
2162 return 0;
Nick Piggin81026792005-06-25 14:57:07 -07002163 *all_pinned = 0;
2164
2165 if (task_running(rq, p))
2166 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02002169 * Aggressive migration if too many balance attempts have failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002171 if (sd->nr_balance_failed > sd->cache_nice_tries)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 return 1;
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 return 1;
2175}
2176
Ingo Molnardd41f592007-07-09 18:51:59 +02002177static int balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
2178 unsigned long max_nr_move, unsigned long max_load_move,
2179 struct sched_domain *sd, enum cpu_idle_type idle,
2180 int *all_pinned, unsigned long *load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002181 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02002182{
2183 int pulled = 0, pinned = 0, skip_for_load;
2184 struct task_struct *p;
2185 long rem_load_move = max_load_move;
2186
2187 if (max_nr_move == 0 || max_load_move == 0)
2188 goto out;
2189
2190 pinned = 1;
2191
2192 /*
2193 * Start the load-balancing iterator:
2194 */
2195 p = iterator->start(iterator->arg);
2196next:
2197 if (!p)
2198 goto out;
2199 /*
2200 * To help distribute high priority tasks accross CPUs we don't
2201 * skip a task if it will be the highest priority task (i.e. smallest
2202 * prio value) on its new queue regardless of its load weight
2203 */
2204 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
2205 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002206 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02002207 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002208 p = iterator->next(iterator->arg);
2209 goto next;
2210 }
2211
2212 pull_task(busiest, p, this_rq, this_cpu);
2213 pulled++;
2214 rem_load_move -= p->se.load.weight;
2215
2216 /*
2217 * We only want to steal up to the prescribed number of tasks
2218 * and the prescribed amount of weighted load.
2219 */
2220 if (pulled < max_nr_move && rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002221 if (p->prio < *this_best_prio)
2222 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02002223 p = iterator->next(iterator->arg);
2224 goto next;
2225 }
2226out:
2227 /*
2228 * Right now, this is the only place pull_task() is called,
2229 * so we can safely collect pull_task() stats here rather than
2230 * inside pull_task().
2231 */
2232 schedstat_add(sd, lb_gained[idle], pulled);
2233
2234 if (all_pinned)
2235 *all_pinned = pinned;
2236 *load_moved = max_load_move - rem_load_move;
2237 return pulled;
2238}
Ingo Molnar48f24c42006-07-03 00:25:40 -07002239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240/*
Peter Williams43010652007-08-09 11:16:46 +02002241 * move_tasks tries to move up to max_load_move weighted load from busiest to
2242 * this_rq, as part of a balancing operation within domain "sd".
2243 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 *
2245 * Called with both runqueues locked.
2246 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002247static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02002248 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002249 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07002250 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
Ingo Molnardd41f592007-07-09 18:51:59 +02002252 struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02002253 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002254 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Ingo Molnardd41f592007-07-09 18:51:59 +02002256 do {
Peter Williams43010652007-08-09 11:16:46 +02002257 total_load_moved +=
2258 class->load_balance(this_rq, this_cpu, busiest,
2259 ULONG_MAX, max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002260 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02002261 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02002262 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Peter Williams43010652007-08-09 11:16:46 +02002264 return total_load_moved > 0;
2265}
2266
2267/*
2268 * move_one_task tries to move exactly one task from busiest to this_rq, as
2269 * part of active balancing operations within "domain".
2270 * Returns 1 if successful and 0 otherwise.
2271 *
2272 * Called with both runqueues locked.
2273 */
2274static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2275 struct sched_domain *sd, enum cpu_idle_type idle)
2276{
2277 struct sched_class *class;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002278 int this_best_prio = MAX_PRIO;
Peter Williams43010652007-08-09 11:16:46 +02002279
2280 for (class = sched_class_highest; class; class = class->next)
2281 if (class->load_balance(this_rq, this_cpu, busiest,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002282 1, ULONG_MAX, sd, idle, NULL,
2283 &this_best_prio))
Peter Williams43010652007-08-09 11:16:46 +02002284 return 1;
2285
2286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287}
2288
2289/*
2290 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07002291 * domain. It calculates and returns the amount of weighted load which
2292 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 */
2294static struct sched_group *
2295find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02002296 unsigned long *imbalance, enum cpu_idle_type idle,
2297 int *sd_idle, cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
2299 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
2300 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002301 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07002302 unsigned long busiest_load_per_task, busiest_nr_running;
2303 unsigned long this_load_per_task, this_nr_running;
Nick Piggin78979862005-06-25 14:57:13 -07002304 int load_idx;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002305#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2306 int power_savings_balance = 1;
2307 unsigned long leader_nr_running = 0, min_load_per_task = 0;
2308 unsigned long min_nr_running = ULONG_MAX;
2309 struct sched_group *group_min = NULL, *group_leader = NULL;
2310#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002313 busiest_load_per_task = busiest_nr_running = 0;
2314 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002315 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002316 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002317 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002318 load_idx = sd->newidle_idx;
2319 else
2320 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 do {
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002323 unsigned long load, group_capacity;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 int local_group;
2325 int i;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002326 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002327 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
2329 local_group = cpu_isset(this_cpu, group->cpumask);
2330
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002331 if (local_group)
2332 balance_cpu = first_cpu(group->cpumask);
2333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07002335 sum_weighted_load = sum_nr_running = avg_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002338 struct rq *rq;
2339
2340 if (!cpu_isset(i, *cpus))
2341 continue;
2342
2343 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07002344
Suresh Siddha9439aab2007-07-19 21:28:35 +02002345 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07002346 *sd_idle = 0;
2347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002349 if (local_group) {
2350 if (idle_cpu(i) && !first_idle_cpu) {
2351 first_idle_cpu = 1;
2352 balance_cpu = i;
2353 }
2354
Nick Piggina2000572006-02-10 01:51:02 -08002355 load = target_load(i, load_idx);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002356 } else
Nick Piggina2000572006-02-10 01:51:02 -08002357 load = source_load(i, load_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
2359 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07002360 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002361 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 }
2363
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002364 /*
2365 * First idle cpu or the first cpu(busiest) in this sched group
2366 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02002367 * domains. In the newly idle case, we will allow all the cpu's
2368 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002369 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02002370 if (idle != CPU_NEWLY_IDLE && local_group &&
2371 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002372 *balance = 0;
2373 goto ret;
2374 }
2375
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07002377 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002380 avg_load = sg_div_cpu_power(group,
2381 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Eric Dumazet5517d862007-05-08 00:32:57 -07002383 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 if (local_group) {
2386 this_load = avg_load;
2387 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002388 this_nr_running = sum_nr_running;
2389 this_load_per_task = sum_weighted_load;
2390 } else if (avg_load > max_load &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002391 sum_nr_running > group_capacity) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 max_load = avg_load;
2393 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002394 busiest_nr_running = sum_nr_running;
2395 busiest_load_per_task = sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002397
2398#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2399 /*
2400 * Busy processors will not participate in power savings
2401 * balance.
2402 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002403 if (idle == CPU_NOT_IDLE ||
2404 !(sd->flags & SD_POWERSAVINGS_BALANCE))
2405 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002406
2407 /*
2408 * If the local group is idle or completely loaded
2409 * no need to do power savings balance at this domain
2410 */
2411 if (local_group && (this_nr_running >= group_capacity ||
2412 !this_nr_running))
2413 power_savings_balance = 0;
2414
Ingo Molnardd41f592007-07-09 18:51:59 +02002415 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002416 * If a group is already running at full capacity or idle,
2417 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02002418 */
2419 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002420 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02002421 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002422
Ingo Molnardd41f592007-07-09 18:51:59 +02002423 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002424 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002425 * This is the group from where we need to pick up the load
2426 * for saving power
2427 */
2428 if ((sum_nr_running < min_nr_running) ||
2429 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002430 first_cpu(group->cpumask) <
2431 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002432 group_min = group;
2433 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002434 min_load_per_task = sum_weighted_load /
2435 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002436 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002437
Ingo Molnardd41f592007-07-09 18:51:59 +02002438 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002439 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02002440 * capacity but still has some space to pick up some load
2441 * from other group and save more power
2442 */
2443 if (sum_nr_running <= group_capacity - 1) {
2444 if (sum_nr_running > leader_nr_running ||
2445 (sum_nr_running == leader_nr_running &&
2446 first_cpu(group->cpumask) >
2447 first_cpu(group_leader->cpumask))) {
2448 group_leader = group;
2449 leader_nr_running = sum_nr_running;
2450 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002451 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002452group_next:
2453#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 group = group->next;
2455 } while (group != sd->groups);
2456
Peter Williams2dd73a42006-06-27 02:54:34 -07002457 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 goto out_balanced;
2459
2460 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
2461
2462 if (this_load >= avg_load ||
2463 100*max_load <= sd->imbalance_pct*this_load)
2464 goto out_balanced;
2465
Peter Williams2dd73a42006-06-27 02:54:34 -07002466 busiest_load_per_task /= busiest_nr_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 /*
2468 * We're trying to get all the cpus to the average_load, so we don't
2469 * want to push ourselves above the average load, nor do we wish to
2470 * reduce the max loaded cpu below the average load, as either of these
2471 * actions would just result in more rebalancing later, and ping-pong
2472 * tasks around. Thus we look for the minimum possible imbalance.
2473 * Negative imbalances (*we* are more loaded than anyone else) will
2474 * be counted as no imbalance for these purposes -- we can't fix that
2475 * by pulling tasks to us. Be careful of negative numbers as they'll
2476 * appear as very large values with unsigned longs.
2477 */
Peter Williams2dd73a42006-06-27 02:54:34 -07002478 if (max_load <= busiest_load_per_task)
2479 goto out_balanced;
2480
2481 /*
2482 * In the presence of smp nice balancing, certain scenarios can have
2483 * max load less than avg load(as we skip the groups at or below
2484 * its cpu_power, while calculating max_load..)
2485 */
2486 if (max_load < avg_load) {
2487 *imbalance = 0;
2488 goto small_imbalance;
2489 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002490
2491 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07002492 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07002495 *imbalance = min(max_pull * busiest->__cpu_power,
2496 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 / SCHED_LOAD_SCALE;
2498
Peter Williams2dd73a42006-06-27 02:54:34 -07002499 /*
2500 * if *imbalance is less than the average load per runnable task
2501 * there is no gaurantee that any tasks will be moved so we'll have
2502 * a think about bumping its value to force at least one task to be
2503 * moved
2504 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002505 if (*imbalance + SCHED_LOAD_SCALE_FUZZ < busiest_load_per_task/2) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07002506 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07002507 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Peter Williams2dd73a42006-06-27 02:54:34 -07002509small_imbalance:
2510 pwr_move = pwr_now = 0;
2511 imbn = 2;
2512 if (this_nr_running) {
2513 this_load_per_task /= this_nr_running;
2514 if (busiest_load_per_task > this_load_per_task)
2515 imbn = 1;
2516 } else
2517 this_load_per_task = SCHED_LOAD_SCALE;
2518
Ingo Molnardd41f592007-07-09 18:51:59 +02002519 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
2520 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002521 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 return busiest;
2523 }
2524
2525 /*
2526 * OK, we don't have enough imbalance to justify moving tasks,
2527 * however we may be able to increase total CPU power used by
2528 * moving them.
2529 */
2530
Eric Dumazet5517d862007-05-08 00:32:57 -07002531 pwr_now += busiest->__cpu_power *
2532 min(busiest_load_per_task, max_load);
2533 pwr_now += this->__cpu_power *
2534 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 pwr_now /= SCHED_LOAD_SCALE;
2536
2537 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07002538 tmp = sg_div_cpu_power(busiest,
2539 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07002541 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07002542 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
2544 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07002545 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08002546 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07002547 tmp = sg_div_cpu_power(this,
2548 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 else
Eric Dumazet5517d862007-05-08 00:32:57 -07002550 tmp = sg_div_cpu_power(this,
2551 busiest_load_per_task * SCHED_LOAD_SCALE);
2552 pwr_move += this->__cpu_power *
2553 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 pwr_move /= SCHED_LOAD_SCALE;
2555
2556 /* Move if we gain throughput */
2557 if (pwr_move <= pwr_now)
2558 goto out_balanced;
2559
Peter Williams2dd73a42006-06-27 02:54:34 -07002560 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 }
2562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 return busiest;
2564
2565out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002566#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002567 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002568 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002570 if (this == group_leader && group_leader != group_min) {
2571 *imbalance = min_load_per_task;
2572 return group_min;
2573 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002574#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002575ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 *imbalance = 0;
2577 return NULL;
2578}
2579
2580/*
2581 * find_busiest_queue - find the busiest runqueue among the cpus in group.
2582 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002583static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002584find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002585 unsigned long imbalance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002587 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07002588 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 int i;
2590
2591 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002592 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002593
2594 if (!cpu_isset(i, *cpus))
2595 continue;
2596
Ingo Molnar48f24c42006-07-03 00:25:40 -07002597 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02002598 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Ingo Molnardd41f592007-07-09 18:51:59 +02002600 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07002601 continue;
2602
Ingo Molnardd41f592007-07-09 18:51:59 +02002603 if (wl > max_load) {
2604 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07002605 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 }
2607 }
2608
2609 return busiest;
2610}
2611
2612/*
Nick Piggin77391d72005-06-25 14:57:30 -07002613 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
2614 * so long as it is large enough.
2615 */
2616#define MAX_PINNED_INTERVAL 512
2617
2618/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 * Check this_cpu to ensure it is balanced within domain. Attempt to move
2620 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002622static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002623 struct sched_domain *sd, enum cpu_idle_type idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002624 int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
Peter Williams43010652007-08-09 11:16:46 +02002626 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002629 struct rq *busiest;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002630 cpumask_t cpus = CPU_MASK_ALL;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002631 unsigned long flags;
Nick Piggin5969fe02005-09-10 00:26:19 -07002632
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002633 /*
2634 * When power savings policy is enabled for the parent domain, idle
2635 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02002636 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002637 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002638 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002639 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002640 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002641 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 schedstat_inc(sd, lb_cnt[idle]);
2644
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002645redo:
2646 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002647 &cpus, balance);
2648
Chen, Kenneth W06066712006-12-10 02:20:35 -08002649 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002650 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 if (!group) {
2653 schedstat_inc(sd, lb_nobusyg[idle]);
2654 goto out_balanced;
2655 }
2656
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002657 busiest = find_busiest_queue(group, idle, imbalance, &cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 if (!busiest) {
2659 schedstat_inc(sd, lb_nobusyq[idle]);
2660 goto out_balanced;
2661 }
2662
Nick Piggindb935db2005-06-25 14:57:11 -07002663 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
2665 schedstat_add(sd, lb_imbalance[idle], imbalance);
2666
Peter Williams43010652007-08-09 11:16:46 +02002667 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 if (busiest->nr_running > 1) {
2669 /*
2670 * Attempt to move tasks. If find_busiest_group has found
2671 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02002672 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 * correctly treated as an imbalance.
2674 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002675 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07002676 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02002677 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07002678 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07002679 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002680 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07002681
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002682 /*
2683 * some other cpu did the load balance for us.
2684 */
Peter Williams43010652007-08-09 11:16:46 +02002685 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002686 resched_cpu(this_cpu);
2687
Nick Piggin81026792005-06-25 14:57:07 -07002688 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002689 if (unlikely(all_pinned)) {
2690 cpu_clear(cpu_of(busiest), cpus);
2691 if (!cpus_empty(cpus))
2692 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07002693 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 }
Nick Piggin81026792005-06-25 14:57:07 -07002696
Peter Williams43010652007-08-09 11:16:46 +02002697 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 schedstat_inc(sd, lb_failed[idle]);
2699 sd->nr_balance_failed++;
2700
2701 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002703 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07002704
2705 /* don't kick the migration_thread, if the curr
2706 * task on busiest cpu can't be moved to this_cpu
2707 */
2708 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002709 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07002710 all_pinned = 1;
2711 goto out_one_pinned;
2712 }
2713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 if (!busiest->active_balance) {
2715 busiest->active_balance = 1;
2716 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07002717 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002719 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07002720 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 wake_up_process(busiest->migration_thread);
2722
2723 /*
2724 * We've kicked active balancing, reset the failure
2725 * counter.
2726 */
Nick Piggin39507452005-06-25 14:57:09 -07002727 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 }
Nick Piggin81026792005-06-25 14:57:07 -07002729 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 sd->nr_balance_failed = 0;
2731
Nick Piggin81026792005-06-25 14:57:07 -07002732 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 /* We were unbalanced, so reset the balancing interval */
2734 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07002735 } else {
2736 /*
2737 * If we've begun active balancing, start to back off. This
2738 * case may not be covered by the all_pinned logic if there
2739 * is only 1 task on the busy runqueue (because we don't call
2740 * move_tasks).
2741 */
2742 if (sd->balance_interval < sd->max_interval)
2743 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 }
2745
Peter Williams43010652007-08-09 11:16:46 +02002746 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002747 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002748 return -1;
Peter Williams43010652007-08-09 11:16:46 +02002749 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
2751out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 schedstat_inc(sd, lb_balanced[idle]);
2753
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002754 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07002755
2756out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07002758 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
2759 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 sd->balance_interval *= 2;
2761
Ingo Molnar48f24c42006-07-03 00:25:40 -07002762 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002763 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002764 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 return 0;
2766}
2767
2768/*
2769 * Check this_cpu to ensure it is balanced within domain. Attempt to move
2770 * tasks if there is an imbalance.
2771 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002772 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 * this_rq is locked.
2774 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07002775static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07002776load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
2778 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002779 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02002781 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07002782 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02002783 int all_pinned = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002784 cpumask_t cpus = CPU_MASK_ALL;
Nick Piggin5969fe02005-09-10 00:26:19 -07002785
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002786 /*
2787 * When power savings policy is enabled for the parent domain, idle
2788 * sibling can pick up load irrespective of busy siblings. In this case,
2789 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002790 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002791 */
2792 if (sd->flags & SD_SHARE_CPUPOWER &&
2793 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002794 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002796 schedstat_inc(sd, lb_cnt[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002797redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002798 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002799 &sd_idle, &cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002801 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002802 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 }
2804
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002805 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002806 &cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07002807 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002808 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002809 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 }
2811
Nick Piggindb935db2005-06-25 14:57:11 -07002812 BUG_ON(busiest == this_rq);
2813
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002814 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07002815
Peter Williams43010652007-08-09 11:16:46 +02002816 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07002817 if (busiest->nr_running > 1) {
2818 /* Attempt to move tasks */
2819 double_lock_balance(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02002820 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02002821 imbalance, sd, CPU_NEWLY_IDLE,
2822 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07002823 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002824
Suresh Siddha969bb4e2007-07-19 21:28:35 +02002825 if (unlikely(all_pinned)) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002826 cpu_clear(cpu_of(busiest), cpus);
2827 if (!cpus_empty(cpus))
2828 goto redo;
2829 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07002830 }
2831
Peter Williams43010652007-08-09 11:16:46 +02002832 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002833 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002834 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
2835 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002836 return -1;
2837 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002838 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Peter Williams43010652007-08-09 11:16:46 +02002840 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002841
2842out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002843 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07002844 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002845 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002846 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002847 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07002848
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002849 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850}
2851
2852/*
2853 * idle_balance is called by schedule() if this_cpu is about to become
2854 * idle. Attempts to pull tasks from other CPUs.
2855 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002856static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
2858 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02002859 int pulled_task = -1;
2860 unsigned long next_balance = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
2862 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07002863 unsigned long interval;
2864
2865 if (!(sd->flags & SD_LOAD_BALANCE))
2866 continue;
2867
2868 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002869 /* If we've pulled tasks over stop searching: */
Christoph Lameter1bd77f22006-12-10 02:20:27 -08002870 pulled_task = load_balance_newidle(this_cpu,
Christoph Lameter92c4ca52007-06-23 17:16:33 -07002871 this_rq, sd);
2872
2873 interval = msecs_to_jiffies(sd->balance_interval);
2874 if (time_after(next_balance, sd->last_balance + interval))
2875 next_balance = sd->last_balance + interval;
2876 if (pulled_task)
2877 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002879 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08002880 /*
2881 * We are going idle. next_balance may be set based on
2882 * a busy processor. So reset next_balance.
2883 */
2884 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02002885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886}
2887
2888/*
2889 * active_load_balance is run by migration threads. It pushes running tasks
2890 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
2891 * running on each physical CPU where possible, and avoids physical /
2892 * logical imbalances.
2893 *
2894 * Called with busiest_rq locked.
2895 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002896static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897{
Nick Piggin39507452005-06-25 14:57:09 -07002898 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002899 struct sched_domain *sd;
2900 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07002901
Ingo Molnar48f24c42006-07-03 00:25:40 -07002902 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07002903 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07002904 return;
2905
2906 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
2908 /*
Nick Piggin39507452005-06-25 14:57:09 -07002909 * This condition is "impossible", if it occurs
2910 * we need to fix it. Originally reported by
2911 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 */
Nick Piggin39507452005-06-25 14:57:09 -07002913 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Nick Piggin39507452005-06-25 14:57:09 -07002915 /* move a task from busiest_rq to target_rq */
2916 double_lock_balance(busiest_rq, target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
Nick Piggin39507452005-06-25 14:57:09 -07002918 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002919 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07002920 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07002921 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07002922 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Ingo Molnar48f24c42006-07-03 00:25:40 -07002925 if (likely(sd)) {
2926 schedstat_inc(sd, alb_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Peter Williams43010652007-08-09 11:16:46 +02002928 if (move_one_task(target_rq, target_cpu, busiest_rq,
2929 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07002930 schedstat_inc(sd, alb_pushed);
2931 else
2932 schedstat_inc(sd, alb_failed);
2933 }
Nick Piggin39507452005-06-25 14:57:09 -07002934 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935}
2936
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002937#ifdef CONFIG_NO_HZ
2938static struct {
2939 atomic_t load_balancer;
2940 cpumask_t cpu_mask;
2941} nohz ____cacheline_aligned = {
2942 .load_balancer = ATOMIC_INIT(-1),
2943 .cpu_mask = CPU_MASK_NONE,
2944};
2945
Christoph Lameter7835b982006-12-10 02:20:22 -08002946/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002947 * This routine will try to nominate the ilb (idle load balancing)
2948 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
2949 * load balancing on behalf of all those cpus. If all the cpus in the system
2950 * go into this tickless mode, then there will be no ilb owner (as there is
2951 * no need for one) and all the cpus will sleep till the next wakeup event
2952 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08002953 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002954 * For the ilb owner, tick is not stopped. And this tick will be used
2955 * for idle load balancing. ilb owner will still be part of
2956 * nohz.cpu_mask..
2957 *
2958 * While stopping the tick, this cpu will become the ilb owner if there
2959 * is no other owner. And will be the owner till that cpu becomes busy
2960 * or if all cpus in the system stop their ticks at which point
2961 * there is no need for ilb owner.
2962 *
2963 * When the ilb owner becomes busy, it nominates another owner, during the
2964 * next busy scheduler_tick()
2965 */
2966int select_nohz_load_balancer(int stop_tick)
2967{
2968 int cpu = smp_processor_id();
2969
2970 if (stop_tick) {
2971 cpu_set(cpu, nohz.cpu_mask);
2972 cpu_rq(cpu)->in_nohz_recently = 1;
2973
2974 /*
2975 * If we are going offline and still the leader, give up!
2976 */
2977 if (cpu_is_offline(cpu) &&
2978 atomic_read(&nohz.load_balancer) == cpu) {
2979 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
2980 BUG();
2981 return 0;
2982 }
2983
2984 /* time for ilb owner also to sleep */
2985 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
2986 if (atomic_read(&nohz.load_balancer) == cpu)
2987 atomic_set(&nohz.load_balancer, -1);
2988 return 0;
2989 }
2990
2991 if (atomic_read(&nohz.load_balancer) == -1) {
2992 /* make me the ilb owner */
2993 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
2994 return 1;
2995 } else if (atomic_read(&nohz.load_balancer) == cpu)
2996 return 1;
2997 } else {
2998 if (!cpu_isset(cpu, nohz.cpu_mask))
2999 return 0;
3000
3001 cpu_clear(cpu, nohz.cpu_mask);
3002
3003 if (atomic_read(&nohz.load_balancer) == cpu)
3004 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
3005 BUG();
3006 }
3007 return 0;
3008}
3009#endif
3010
3011static DEFINE_SPINLOCK(balancing);
3012
3013/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003014 * It checks each scheduling domain to see if it is due to be balanced,
3015 * and initiates a balancing operation if so.
3016 *
3017 * Balancing parameters are set up in arch_init_sched_domains.
3018 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003019static inline void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08003020{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003021 int balance = 1;
3022 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08003023 unsigned long interval;
3024 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003025 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08003026 unsigned long next_balance = jiffies + 60*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003028 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 if (!(sd->flags & SD_LOAD_BALANCE))
3030 continue;
3031
3032 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003033 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 interval *= sd->busy_factor;
3035
3036 /* scale ms to jiffies */
3037 interval = msecs_to_jiffies(interval);
3038 if (unlikely(!interval))
3039 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02003040 if (interval > HZ*NR_CPUS/10)
3041 interval = HZ*NR_CPUS/10;
3042
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Christoph Lameter08c183f2006-12-10 02:20:29 -08003044 if (sd->flags & SD_SERIALIZE) {
3045 if (!spin_trylock(&balancing))
3046 goto out;
3047 }
3048
Christoph Lameterc9819f42006-12-10 02:20:25 -08003049 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003050 if (load_balance(cpu, rq, sd, idle, &balance)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07003051 /*
3052 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07003053 * longer idle, or one of our SMT siblings is
3054 * not idle.
3055 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003056 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003058 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08003060 if (sd->flags & SD_SERIALIZE)
3061 spin_unlock(&balancing);
3062out:
Christoph Lameterc9819f42006-12-10 02:20:25 -08003063 if (time_after(next_balance, sd->last_balance + interval))
3064 next_balance = sd->last_balance + interval;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003065
3066 /*
3067 * Stop the load balance at this level. There is another
3068 * CPU in our sched group which is doing load balancing more
3069 * actively.
3070 */
3071 if (!balance)
3072 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 }
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003074 rq->next_balance = next_balance;
3075}
3076
3077/*
3078 * run_rebalance_domains is triggered when needed from the scheduler tick.
3079 * In CONFIG_NO_HZ case, the idle load balance owner will do the
3080 * rebalancing for all the cpus for whom scheduler ticks are stopped.
3081 */
3082static void run_rebalance_domains(struct softirq_action *h)
3083{
Ingo Molnardd41f592007-07-09 18:51:59 +02003084 int this_cpu = smp_processor_id();
3085 struct rq *this_rq = cpu_rq(this_cpu);
3086 enum cpu_idle_type idle = this_rq->idle_at_tick ?
3087 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003088
Ingo Molnardd41f592007-07-09 18:51:59 +02003089 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003090
3091#ifdef CONFIG_NO_HZ
3092 /*
3093 * If this cpu is the owner for idle load balancing, then do the
3094 * balancing on behalf of the other idle cpus whose ticks are
3095 * stopped.
3096 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003097 if (this_rq->idle_at_tick &&
3098 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003099 cpumask_t cpus = nohz.cpu_mask;
3100 struct rq *rq;
3101 int balance_cpu;
3102
Ingo Molnardd41f592007-07-09 18:51:59 +02003103 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003104 for_each_cpu_mask(balance_cpu, cpus) {
3105 /*
3106 * If this cpu gets work to do, stop the load balancing
3107 * work being done for other cpus. Next load
3108 * balancing owner will pick it up.
3109 */
3110 if (need_resched())
3111 break;
3112
Ingo Molnardd41f592007-07-09 18:51:59 +02003113 rebalance_domains(balance_cpu, SCHED_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003114
3115 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003116 if (time_after(this_rq->next_balance, rq->next_balance))
3117 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003118 }
3119 }
3120#endif
3121}
3122
3123/*
3124 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
3125 *
3126 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
3127 * idle load balancing owner or decide to stop the periodic load balancing,
3128 * if the whole system is idle.
3129 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003130static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003131{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003132#ifdef CONFIG_NO_HZ
3133 /*
3134 * If we were in the nohz mode recently and busy at the current
3135 * scheduler tick, then check if we need to nominate new idle
3136 * load balancer.
3137 */
3138 if (rq->in_nohz_recently && !rq->idle_at_tick) {
3139 rq->in_nohz_recently = 0;
3140
3141 if (atomic_read(&nohz.load_balancer) == cpu) {
3142 cpu_clear(cpu, nohz.cpu_mask);
3143 atomic_set(&nohz.load_balancer, -1);
3144 }
3145
3146 if (atomic_read(&nohz.load_balancer) == -1) {
3147 /*
3148 * simple selection for now: Nominate the
3149 * first cpu in the nohz list to be the next
3150 * ilb owner.
3151 *
3152 * TBD: Traverse the sched domains and nominate
3153 * the nearest cpu in the nohz.cpu_mask.
3154 */
3155 int ilb = first_cpu(nohz.cpu_mask);
3156
3157 if (ilb != NR_CPUS)
3158 resched_cpu(ilb);
3159 }
3160 }
3161
3162 /*
3163 * If this cpu is idle and doing idle load balancing for all the
3164 * cpus with ticks stopped, is it time for that to stop?
3165 */
3166 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
3167 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3168 resched_cpu(cpu);
3169 return;
3170 }
3171
3172 /*
3173 * If this cpu is idle and the idle load balancing is done by
3174 * someone else, then no need raise the SCHED_SOFTIRQ
3175 */
3176 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
3177 cpu_isset(cpu, nohz.cpu_mask))
3178 return;
3179#endif
3180 if (time_after_eq(jiffies, rq->next_balance))
3181 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182}
Ingo Molnardd41f592007-07-09 18:51:59 +02003183
3184#else /* CONFIG_SMP */
3185
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186/*
3187 * on UP we do not need to balance between CPUs:
3188 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003189static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
3191}
Ingo Molnardd41f592007-07-09 18:51:59 +02003192
3193/* Avoid "used but not defined" warning on UP */
3194static int balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3195 unsigned long max_nr_move, unsigned long max_load_move,
3196 struct sched_domain *sd, enum cpu_idle_type idle,
3197 int *all_pinned, unsigned long *load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003198 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02003199{
3200 *load_moved = 0;
3201
3202 return 0;
3203}
3204
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205#endif
3206
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207DEFINE_PER_CPU(struct kernel_stat, kstat);
3208
3209EXPORT_PER_CPU_SYMBOL(kstat);
3210
3211/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02003212 * Return p->sum_exec_runtime plus any more ns on the sched_clock
3213 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02003215unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003218 u64 ns, delta_exec;
3219 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003220
Ingo Molnar41b86e92007-07-09 18:51:58 +02003221 rq = task_rq_lock(p, &flags);
3222 ns = p->se.sum_exec_runtime;
3223 if (rq->curr == p) {
3224 delta_exec = rq_clock(rq) - p->se.exec_start;
3225 if ((s64)delta_exec > 0)
3226 ns += delta_exec;
3227 }
3228 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003229
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 return ns;
3231}
3232
3233/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 * Account user cpu time to a process.
3235 * @p: the process that the cpu time gets accounted to
3236 * @hardirq_offset: the offset to subtract from hardirq_count()
3237 * @cputime: the cpu time spent in user space since the last update
3238 */
3239void account_user_time(struct task_struct *p, cputime_t cputime)
3240{
3241 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3242 cputime64_t tmp;
3243
3244 p->utime = cputime_add(p->utime, cputime);
3245
3246 /* Add user time to cpustat. */
3247 tmp = cputime_to_cputime64(cputime);
3248 if (TASK_NICE(p) > 0)
3249 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3250 else
3251 cpustat->user = cputime64_add(cpustat->user, tmp);
3252}
3253
3254/*
3255 * Account system cpu time to a process.
3256 * @p: the process that the cpu time gets accounted to
3257 * @hardirq_offset: the offset to subtract from hardirq_count()
3258 * @cputime: the cpu time spent in kernel space since the last update
3259 */
3260void account_system_time(struct task_struct *p, int hardirq_offset,
3261 cputime_t cputime)
3262{
3263 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003264 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 cputime64_t tmp;
3266
3267 p->stime = cputime_add(p->stime, cputime);
3268
3269 /* Add system time to cpustat. */
3270 tmp = cputime_to_cputime64(cputime);
3271 if (hardirq_count() - hardirq_offset)
3272 cpustat->irq = cputime64_add(cpustat->irq, tmp);
3273 else if (softirq_count())
3274 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
3275 else if (p != rq->idle)
3276 cpustat->system = cputime64_add(cpustat->system, tmp);
3277 else if (atomic_read(&rq->nr_iowait) > 0)
3278 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3279 else
3280 cpustat->idle = cputime64_add(cpustat->idle, tmp);
3281 /* Account for system time used */
3282 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283}
3284
3285/*
3286 * Account for involuntary wait time.
3287 * @p: the process from which the cpu time has been stolen
3288 * @steal: the cpu time spent in involuntary wait
3289 */
3290void account_steal_time(struct task_struct *p, cputime_t steal)
3291{
3292 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3293 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07003294 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
3296 if (p == rq->idle) {
3297 p->stime = cputime_add(p->stime, steal);
3298 if (atomic_read(&rq->nr_iowait) > 0)
3299 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3300 else
3301 cpustat->idle = cputime64_add(cpustat->idle, tmp);
3302 } else
3303 cpustat->steal = cputime64_add(cpustat->steal, tmp);
3304}
3305
Christoph Lameter7835b982006-12-10 02:20:22 -08003306/*
3307 * This function gets called by the timer code, with HZ frequency.
3308 * We call it with interrupts disabled.
3309 *
3310 * It also gets called by the fork code, when changing the parent's
3311 * timeslices.
3312 */
3313void scheduler_tick(void)
3314{
Christoph Lameter7835b982006-12-10 02:20:22 -08003315 int cpu = smp_processor_id();
3316 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003317 struct task_struct *curr = rq->curr;
Christoph Lameter7835b982006-12-10 02:20:22 -08003318
Ingo Molnardd41f592007-07-09 18:51:59 +02003319 spin_lock(&rq->lock);
Ingo Molnarf1a438d2007-08-09 11:16:45 +02003320 update_cpu_load(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003321 if (curr != rq->idle) /* FIXME: needed? */
3322 curr->sched_class->task_tick(rq, curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02003323 spin_unlock(&rq->lock);
3324
Christoph Lametere418e1c2006-12-10 02:20:23 -08003325#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02003326 rq->idle_at_tick = idle_cpu(cpu);
3327 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08003328#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329}
3330
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
3332
3333void fastcall add_preempt_count(int val)
3334{
3335 /*
3336 * Underflow?
3337 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003338 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
3339 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 preempt_count() += val;
3341 /*
3342 * Spinlock count overflowing soon?
3343 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003344 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
3345 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346}
3347EXPORT_SYMBOL(add_preempt_count);
3348
3349void fastcall sub_preempt_count(int val)
3350{
3351 /*
3352 * Underflow?
3353 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003354 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
3355 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 /*
3357 * Is the spinlock portion underflowing?
3358 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003359 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
3360 !(preempt_count() & PREEMPT_MASK)))
3361 return;
3362
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 preempt_count() -= val;
3364}
3365EXPORT_SYMBOL(sub_preempt_count);
3366
3367#endif
3368
3369/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003370 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003372static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373{
Ingo Molnardd41f592007-07-09 18:51:59 +02003374 printk(KERN_ERR "BUG: scheduling while atomic: %s/0x%08x/%d\n",
3375 prev->comm, preempt_count(), prev->pid);
3376 debug_show_held_locks(prev);
3377 if (irqs_disabled())
3378 print_irqtrace_events(prev);
3379 dump_stack();
3380}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
Ingo Molnardd41f592007-07-09 18:51:59 +02003382/*
3383 * Various schedule()-time debugging checks and statistics:
3384 */
3385static inline void schedule_debug(struct task_struct *prev)
3386{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 /*
3388 * Test if we are atomic. Since do_exit() needs to call into
3389 * schedule() atomically, we ignore that path for now.
3390 * Otherwise, whine if we are scheduling when we should not be.
3391 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003392 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
3393 __schedule_bug(prev);
3394
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
3396
Ingo Molnardd41f592007-07-09 18:51:59 +02003397 schedstat_inc(this_rq(), sched_cnt);
3398}
3399
3400/*
3401 * Pick up the highest-prio task:
3402 */
3403static inline struct task_struct *
3404pick_next_task(struct rq *rq, struct task_struct *prev, u64 now)
3405{
3406 struct sched_class *class;
3407 struct task_struct *p;
3408
3409 /*
3410 * Optimization: we know that if all tasks are in
3411 * the fair class we can call that function directly:
3412 */
3413 if (likely(rq->nr_running == rq->cfs.nr_running)) {
3414 p = fair_sched_class.pick_next_task(rq, now);
3415 if (likely(p))
3416 return p;
3417 }
3418
3419 class = sched_class_highest;
3420 for ( ; ; ) {
3421 p = class->pick_next_task(rq, now);
3422 if (p)
3423 return p;
3424 /*
3425 * Will never be NULL as the idle class always
3426 * returns a non-NULL p:
3427 */
3428 class = class->next;
3429 }
3430}
3431
3432/*
3433 * schedule() is the main scheduler function.
3434 */
3435asmlinkage void __sched schedule(void)
3436{
3437 struct task_struct *prev, *next;
3438 long *switch_count;
3439 struct rq *rq;
3440 u64 now;
3441 int cpu;
3442
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443need_resched:
3444 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02003445 cpu = smp_processor_id();
3446 rq = cpu_rq(cpu);
3447 rcu_qsctr_inc(cpu);
3448 prev = rq->curr;
3449 switch_count = &prev->nivcsw;
3450
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 release_kernel_lock(prev);
3452need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
Ingo Molnardd41f592007-07-09 18:51:59 +02003454 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
3456 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 clear_tsk_need_resched(prev);
Ingo Molnar8e717b12007-08-09 11:16:46 +02003458 now = __rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Ingo Molnardd41f592007-07-09 18:51:59 +02003460 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
3461 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
3462 unlikely(signal_pending(prev)))) {
3463 prev->state = TASK_RUNNING;
3464 } else {
Ingo Molnar8e717b12007-08-09 11:16:46 +02003465 deactivate_task(rq, prev, 1, now);
Ingo Molnardd41f592007-07-09 18:51:59 +02003466 }
3467 switch_count = &prev->nvcsw;
3468 }
3469
3470 if (unlikely(!rq->nr_running))
3471 idle_balance(cpu, rq);
3472
Ingo Molnardd41f592007-07-09 18:51:59 +02003473 prev->sched_class->put_prev_task(rq, prev, now);
3474 next = pick_next_task(rq, prev, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
3476 sched_info_switch(prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02003477
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 rq->nr_switches++;
3480 rq->curr = next;
3481 ++*switch_count;
3482
Ingo Molnardd41f592007-07-09 18:51:59 +02003483 context_switch(rq, prev, next); /* unlocks the rq */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 } else
3485 spin_unlock_irq(&rq->lock);
3486
Ingo Molnardd41f592007-07-09 18:51:59 +02003487 if (unlikely(reacquire_kernel_lock(current) < 0)) {
3488 cpu = smp_processor_id();
3489 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 goto need_resched_nonpreemptible;
Ingo Molnardd41f592007-07-09 18:51:59 +02003491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 preempt_enable_no_resched();
3493 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
3494 goto need_resched;
3495}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496EXPORT_SYMBOL(schedule);
3497
3498#ifdef CONFIG_PREEMPT
3499/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003500 * this is the entry point to schedule() from in-kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 * off of preempt_enable. Kernel preemptions off return from interrupt
3502 * occur there and call schedule directly.
3503 */
3504asmlinkage void __sched preempt_schedule(void)
3505{
3506 struct thread_info *ti = current_thread_info();
3507#ifdef CONFIG_PREEMPT_BKL
3508 struct task_struct *task = current;
3509 int saved_lock_depth;
3510#endif
3511 /*
3512 * If there is a non-zero preempt_count or interrupts are disabled,
3513 * we do not want to preempt the current task. Just return..
3514 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07003515 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 return;
3517
3518need_resched:
3519 add_preempt_count(PREEMPT_ACTIVE);
3520 /*
3521 * We keep the big kernel semaphore locked, but we
3522 * clear ->lock_depth so that schedule() doesnt
3523 * auto-release the semaphore:
3524 */
3525#ifdef CONFIG_PREEMPT_BKL
3526 saved_lock_depth = task->lock_depth;
3527 task->lock_depth = -1;
3528#endif
3529 schedule();
3530#ifdef CONFIG_PREEMPT_BKL
3531 task->lock_depth = saved_lock_depth;
3532#endif
3533 sub_preempt_count(PREEMPT_ACTIVE);
3534
3535 /* we could miss a preemption opportunity between schedule and now */
3536 barrier();
3537 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
3538 goto need_resched;
3539}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540EXPORT_SYMBOL(preempt_schedule);
3541
3542/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003543 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 * off of irq context.
3545 * Note, that this is called and return with irqs disabled. This will
3546 * protect us against recursive calling from irq.
3547 */
3548asmlinkage void __sched preempt_schedule_irq(void)
3549{
3550 struct thread_info *ti = current_thread_info();
3551#ifdef CONFIG_PREEMPT_BKL
3552 struct task_struct *task = current;
3553 int saved_lock_depth;
3554#endif
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003555 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 BUG_ON(ti->preempt_count || !irqs_disabled());
3557
3558need_resched:
3559 add_preempt_count(PREEMPT_ACTIVE);
3560 /*
3561 * We keep the big kernel semaphore locked, but we
3562 * clear ->lock_depth so that schedule() doesnt
3563 * auto-release the semaphore:
3564 */
3565#ifdef CONFIG_PREEMPT_BKL
3566 saved_lock_depth = task->lock_depth;
3567 task->lock_depth = -1;
3568#endif
3569 local_irq_enable();
3570 schedule();
3571 local_irq_disable();
3572#ifdef CONFIG_PREEMPT_BKL
3573 task->lock_depth = saved_lock_depth;
3574#endif
3575 sub_preempt_count(PREEMPT_ACTIVE);
3576
3577 /* we could miss a preemption opportunity between schedule and now */
3578 barrier();
3579 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
3580 goto need_resched;
3581}
3582
3583#endif /* CONFIG_PREEMPT */
3584
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003585int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
3586 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587{
Ingo Molnar48f24c42006-07-03 00:25:40 -07003588 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590EXPORT_SYMBOL(default_wake_function);
3591
3592/*
3593 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
3594 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
3595 * number) then we wake all the non-exclusive tasks and one exclusive task.
3596 *
3597 * There are circumstances in which we can try to wake a task which has already
3598 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
3599 * zero in this (rare) case, and we handle it by continuing to scan the queue.
3600 */
3601static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
3602 int nr_exclusive, int sync, void *key)
3603{
3604 struct list_head *tmp, *next;
3605
3606 list_for_each_safe(tmp, next, &q->task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003607 wait_queue_t *curr = list_entry(tmp, wait_queue_t, task_list);
3608 unsigned flags = curr->flags;
3609
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003611 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 break;
3613 }
3614}
3615
3616/**
3617 * __wake_up - wake up threads blocked on a waitqueue.
3618 * @q: the waitqueue
3619 * @mode: which threads
3620 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07003621 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 */
3623void fastcall __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003624 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625{
3626 unsigned long flags;
3627
3628 spin_lock_irqsave(&q->lock, flags);
3629 __wake_up_common(q, mode, nr_exclusive, 0, key);
3630 spin_unlock_irqrestore(&q->lock, flags);
3631}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632EXPORT_SYMBOL(__wake_up);
3633
3634/*
3635 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
3636 */
3637void fastcall __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
3638{
3639 __wake_up_common(q, mode, 1, 0, NULL);
3640}
3641
3642/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07003643 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 * @q: the waitqueue
3645 * @mode: which threads
3646 * @nr_exclusive: how many wake-one or wake-many threads to wake up
3647 *
3648 * The sync wakeup differs that the waker knows that it will schedule
3649 * away soon, so while the target thread will be woken up, it will not
3650 * be migrated to another CPU - ie. the two threads are 'synchronized'
3651 * with each other. This can prevent needless bouncing between CPUs.
3652 *
3653 * On UP it can prevent extra preemption.
3654 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003655void fastcall
3656__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657{
3658 unsigned long flags;
3659 int sync = 1;
3660
3661 if (unlikely(!q))
3662 return;
3663
3664 if (unlikely(!nr_exclusive))
3665 sync = 0;
3666
3667 spin_lock_irqsave(&q->lock, flags);
3668 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
3669 spin_unlock_irqrestore(&q->lock, flags);
3670}
3671EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
3672
3673void fastcall complete(struct completion *x)
3674{
3675 unsigned long flags;
3676
3677 spin_lock_irqsave(&x->wait.lock, flags);
3678 x->done++;
3679 __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
3680 1, 0, NULL);
3681 spin_unlock_irqrestore(&x->wait.lock, flags);
3682}
3683EXPORT_SYMBOL(complete);
3684
3685void fastcall complete_all(struct completion *x)
3686{
3687 unsigned long flags;
3688
3689 spin_lock_irqsave(&x->wait.lock, flags);
3690 x->done += UINT_MAX/2;
3691 __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
3692 0, 0, NULL);
3693 spin_unlock_irqrestore(&x->wait.lock, flags);
3694}
3695EXPORT_SYMBOL(complete_all);
3696
3697void fastcall __sched wait_for_completion(struct completion *x)
3698{
3699 might_sleep();
Ingo Molnar48f24c42006-07-03 00:25:40 -07003700
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 spin_lock_irq(&x->wait.lock);
3702 if (!x->done) {
3703 DECLARE_WAITQUEUE(wait, current);
3704
3705 wait.flags |= WQ_FLAG_EXCLUSIVE;
3706 __add_wait_queue_tail(&x->wait, &wait);
3707 do {
3708 __set_current_state(TASK_UNINTERRUPTIBLE);
3709 spin_unlock_irq(&x->wait.lock);
3710 schedule();
3711 spin_lock_irq(&x->wait.lock);
3712 } while (!x->done);
3713 __remove_wait_queue(&x->wait, &wait);
3714 }
3715 x->done--;
3716 spin_unlock_irq(&x->wait.lock);
3717}
3718EXPORT_SYMBOL(wait_for_completion);
3719
3720unsigned long fastcall __sched
3721wait_for_completion_timeout(struct completion *x, unsigned long timeout)
3722{
3723 might_sleep();
3724
3725 spin_lock_irq(&x->wait.lock);
3726 if (!x->done) {
3727 DECLARE_WAITQUEUE(wait, current);
3728
3729 wait.flags |= WQ_FLAG_EXCLUSIVE;
3730 __add_wait_queue_tail(&x->wait, &wait);
3731 do {
3732 __set_current_state(TASK_UNINTERRUPTIBLE);
3733 spin_unlock_irq(&x->wait.lock);
3734 timeout = schedule_timeout(timeout);
3735 spin_lock_irq(&x->wait.lock);
3736 if (!timeout) {
3737 __remove_wait_queue(&x->wait, &wait);
3738 goto out;
3739 }
3740 } while (!x->done);
3741 __remove_wait_queue(&x->wait, &wait);
3742 }
3743 x->done--;
3744out:
3745 spin_unlock_irq(&x->wait.lock);
3746 return timeout;
3747}
3748EXPORT_SYMBOL(wait_for_completion_timeout);
3749
3750int fastcall __sched wait_for_completion_interruptible(struct completion *x)
3751{
3752 int ret = 0;
3753
3754 might_sleep();
3755
3756 spin_lock_irq(&x->wait.lock);
3757 if (!x->done) {
3758 DECLARE_WAITQUEUE(wait, current);
3759
3760 wait.flags |= WQ_FLAG_EXCLUSIVE;
3761 __add_wait_queue_tail(&x->wait, &wait);
3762 do {
3763 if (signal_pending(current)) {
3764 ret = -ERESTARTSYS;
3765 __remove_wait_queue(&x->wait, &wait);
3766 goto out;
3767 }
3768 __set_current_state(TASK_INTERRUPTIBLE);
3769 spin_unlock_irq(&x->wait.lock);
3770 schedule();
3771 spin_lock_irq(&x->wait.lock);
3772 } while (!x->done);
3773 __remove_wait_queue(&x->wait, &wait);
3774 }
3775 x->done--;
3776out:
3777 spin_unlock_irq(&x->wait.lock);
3778
3779 return ret;
3780}
3781EXPORT_SYMBOL(wait_for_completion_interruptible);
3782
3783unsigned long fastcall __sched
3784wait_for_completion_interruptible_timeout(struct completion *x,
3785 unsigned long timeout)
3786{
3787 might_sleep();
3788
3789 spin_lock_irq(&x->wait.lock);
3790 if (!x->done) {
3791 DECLARE_WAITQUEUE(wait, current);
3792
3793 wait.flags |= WQ_FLAG_EXCLUSIVE;
3794 __add_wait_queue_tail(&x->wait, &wait);
3795 do {
3796 if (signal_pending(current)) {
3797 timeout = -ERESTARTSYS;
3798 __remove_wait_queue(&x->wait, &wait);
3799 goto out;
3800 }
3801 __set_current_state(TASK_INTERRUPTIBLE);
3802 spin_unlock_irq(&x->wait.lock);
3803 timeout = schedule_timeout(timeout);
3804 spin_lock_irq(&x->wait.lock);
3805 if (!timeout) {
3806 __remove_wait_queue(&x->wait, &wait);
3807 goto out;
3808 }
3809 } while (!x->done);
3810 __remove_wait_queue(&x->wait, &wait);
3811 }
3812 x->done--;
3813out:
3814 spin_unlock_irq(&x->wait.lock);
3815 return timeout;
3816}
3817EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
3818
Ingo Molnar0fec1712007-07-09 18:52:01 +02003819static inline void
3820sleep_on_head(wait_queue_head_t *q, wait_queue_t *wait, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821{
Ingo Molnar0fec1712007-07-09 18:52:01 +02003822 spin_lock_irqsave(&q->lock, *flags);
3823 __add_wait_queue(q, wait);
3824 spin_unlock(&q->lock);
3825}
3826
3827static inline void
3828sleep_on_tail(wait_queue_head_t *q, wait_queue_t *wait, unsigned long *flags)
3829{
3830 spin_lock_irq(&q->lock);
3831 __remove_wait_queue(q, wait);
3832 spin_unlock_irqrestore(&q->lock, *flags);
3833}
3834
3835void __sched interruptible_sleep_on(wait_queue_head_t *q)
3836{
3837 unsigned long flags;
3838 wait_queue_t wait;
3839
3840 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841
3842 current->state = TASK_INTERRUPTIBLE;
3843
Ingo Molnar0fec1712007-07-09 18:52:01 +02003844 sleep_on_head(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 schedule();
Ingo Molnar0fec1712007-07-09 18:52:01 +02003846 sleep_on_tail(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848EXPORT_SYMBOL(interruptible_sleep_on);
3849
Ingo Molnar0fec1712007-07-09 18:52:01 +02003850long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003851interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852{
Ingo Molnar0fec1712007-07-09 18:52:01 +02003853 unsigned long flags;
3854 wait_queue_t wait;
3855
3856 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857
3858 current->state = TASK_INTERRUPTIBLE;
3859
Ingo Molnar0fec1712007-07-09 18:52:01 +02003860 sleep_on_head(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 timeout = schedule_timeout(timeout);
Ingo Molnar0fec1712007-07-09 18:52:01 +02003862 sleep_on_tail(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863
3864 return timeout;
3865}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866EXPORT_SYMBOL(interruptible_sleep_on_timeout);
3867
Ingo Molnar0fec1712007-07-09 18:52:01 +02003868void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869{
Ingo Molnar0fec1712007-07-09 18:52:01 +02003870 unsigned long flags;
3871 wait_queue_t wait;
3872
3873 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
3875 current->state = TASK_UNINTERRUPTIBLE;
3876
Ingo Molnar0fec1712007-07-09 18:52:01 +02003877 sleep_on_head(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 schedule();
Ingo Molnar0fec1712007-07-09 18:52:01 +02003879 sleep_on_tail(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881EXPORT_SYMBOL(sleep_on);
3882
Ingo Molnar0fec1712007-07-09 18:52:01 +02003883long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884{
Ingo Molnar0fec1712007-07-09 18:52:01 +02003885 unsigned long flags;
3886 wait_queue_t wait;
3887
3888 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
3890 current->state = TASK_UNINTERRUPTIBLE;
3891
Ingo Molnar0fec1712007-07-09 18:52:01 +02003892 sleep_on_head(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 timeout = schedule_timeout(timeout);
Ingo Molnar0fec1712007-07-09 18:52:01 +02003894 sleep_on_tail(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
3896 return timeout;
3897}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898EXPORT_SYMBOL(sleep_on_timeout);
3899
Ingo Molnarb29739f2006-06-27 02:54:51 -07003900#ifdef CONFIG_RT_MUTEXES
3901
3902/*
3903 * rt_mutex_setprio - set the current priority of a task
3904 * @p: task
3905 * @prio: prio value (kernel-internal form)
3906 *
3907 * This function changes the 'effective' priority of a task. It does
3908 * not touch ->normal_prio like __setscheduler().
3909 *
3910 * Used by the rt_mutex code to implement priority inheritance logic.
3911 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003912void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07003913{
3914 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02003915 int oldprio, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003916 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02003917 u64 now;
Ingo Molnarb29739f2006-06-27 02:54:51 -07003918
3919 BUG_ON(prio < 0 || prio > MAX_PRIO);
3920
3921 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02003922 now = rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003923
Andrew Mortond5f9f942007-05-08 20:27:06 -07003924 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02003925 on_rq = p->se.on_rq;
3926 if (on_rq)
3927 dequeue_task(rq, p, 0, now);
3928
3929 if (rt_prio(prio))
3930 p->sched_class = &rt_sched_class;
3931 else
3932 p->sched_class = &fair_sched_class;
3933
Ingo Molnarb29739f2006-06-27 02:54:51 -07003934 p->prio = prio;
3935
Ingo Molnardd41f592007-07-09 18:51:59 +02003936 if (on_rq) {
3937 enqueue_task(rq, p, 0, now);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003938 /*
3939 * Reschedule if we are currently running on this runqueue and
Andrew Mortond5f9f942007-05-08 20:27:06 -07003940 * our priority decreased, or if we are not currently running on
3941 * this runqueue and our priority is higher than the current's
Ingo Molnarb29739f2006-06-27 02:54:51 -07003942 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07003943 if (task_running(rq, p)) {
3944 if (p->prio > oldprio)
3945 resched_task(rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02003946 } else {
3947 check_preempt_curr(rq, p);
3948 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07003949 }
3950 task_rq_unlock(rq, &flags);
3951}
3952
3953#endif
3954
Ingo Molnar36c8b582006-07-03 00:25:41 -07003955void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956{
Ingo Molnardd41f592007-07-09 18:51:59 +02003957 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003959 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02003960 u64 now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961
3962 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
3963 return;
3964 /*
3965 * We have to be careful, if called from sys_setpriority(),
3966 * the task might be in the middle of scheduling on another CPU.
3967 */
3968 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02003969 now = rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 /*
3971 * The RT priorities are set via sched_setscheduler(), but we still
3972 * allow the 'normal' nice value to be set - but as expected
3973 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02003974 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 */
Ingo Molnare05606d2007-07-09 18:51:59 +02003976 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 p->static_prio = NICE_TO_PRIO(nice);
3978 goto out_unlock;
3979 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003980 on_rq = p->se.on_rq;
3981 if (on_rq) {
3982 dequeue_task(rq, p, 0, now);
3983 dec_load(rq, p, now);
Peter Williams2dd73a42006-06-27 02:54:34 -07003984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07003987 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003988 old_prio = p->prio;
3989 p->prio = effective_prio(p);
3990 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
Ingo Molnardd41f592007-07-09 18:51:59 +02003992 if (on_rq) {
3993 enqueue_task(rq, p, 0, now);
3994 inc_load(rq, p, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07003996 * If the task increased its priority or is running and
3997 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07003999 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 resched_task(rq->curr);
4001 }
4002out_unlock:
4003 task_rq_unlock(rq, &flags);
4004}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005EXPORT_SYMBOL(set_user_nice);
4006
Matt Mackalle43379f2005-05-01 08:59:00 -07004007/*
4008 * can_nice - check if a task can reduce its nice value
4009 * @p: task
4010 * @nice: nice value
4011 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004012int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07004013{
Matt Mackall024f4742005-08-18 11:24:19 -07004014 /* convert nice value [19,-20] to rlimit style value [1,40] */
4015 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004016
Matt Mackalle43379f2005-05-01 08:59:00 -07004017 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
4018 capable(CAP_SYS_NICE));
4019}
4020
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021#ifdef __ARCH_WANT_SYS_NICE
4022
4023/*
4024 * sys_nice - change the priority of the current process.
4025 * @increment: priority increment
4026 *
4027 * sys_setpriority is a more generic, but much slower function that
4028 * does similar things.
4029 */
4030asmlinkage long sys_nice(int increment)
4031{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004032 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033
4034 /*
4035 * Setpriority might change our priority at the same moment.
4036 * We don't have to worry. Conceptually one call occurs first
4037 * and we have a single winner.
4038 */
Matt Mackalle43379f2005-05-01 08:59:00 -07004039 if (increment < -40)
4040 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 if (increment > 40)
4042 increment = 40;
4043
4044 nice = PRIO_TO_NICE(current->static_prio) + increment;
4045 if (nice < -20)
4046 nice = -20;
4047 if (nice > 19)
4048 nice = 19;
4049
Matt Mackalle43379f2005-05-01 08:59:00 -07004050 if (increment < 0 && !can_nice(current, nice))
4051 return -EPERM;
4052
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 retval = security_task_setnice(current, nice);
4054 if (retval)
4055 return retval;
4056
4057 set_user_nice(current, nice);
4058 return 0;
4059}
4060
4061#endif
4062
4063/**
4064 * task_prio - return the priority value of a given task.
4065 * @p: the task in question.
4066 *
4067 * This is the priority value as seen by users in /proc.
4068 * RT tasks are offset by -200. Normal tasks are centered
4069 * around 0, value goes from -16 to +15.
4070 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004071int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072{
4073 return p->prio - MAX_RT_PRIO;
4074}
4075
4076/**
4077 * task_nice - return the nice value of a given task.
4078 * @p: the task in question.
4079 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004080int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081{
4082 return TASK_NICE(p);
4083}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084EXPORT_SYMBOL_GPL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085
4086/**
4087 * idle_cpu - is a given cpu idle currently?
4088 * @cpu: the processor in question.
4089 */
4090int idle_cpu(int cpu)
4091{
4092 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
4093}
4094
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095/**
4096 * idle_task - return the idle task for a given cpu.
4097 * @cpu: the processor in question.
4098 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004099struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100{
4101 return cpu_rq(cpu)->idle;
4102}
4103
4104/**
4105 * find_process_by_pid - find a process with a matching PID value.
4106 * @pid: the pid in question.
4107 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004108static inline struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109{
4110 return pid ? find_task_by_pid(pid) : current;
4111}
4112
4113/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02004114static void
4115__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116{
Ingo Molnardd41f592007-07-09 18:51:59 +02004117 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004118
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02004120 switch (p->policy) {
4121 case SCHED_NORMAL:
4122 case SCHED_BATCH:
4123 case SCHED_IDLE:
4124 p->sched_class = &fair_sched_class;
4125 break;
4126 case SCHED_FIFO:
4127 case SCHED_RR:
4128 p->sched_class = &rt_sched_class;
4129 break;
4130 }
4131
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004133 p->normal_prio = normal_prio(p);
4134 /* we are holding p->pi_lock already */
4135 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07004136 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137}
4138
4139/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004140 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 * @p: the task in question.
4142 * @policy: new policy.
4143 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004144 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004145 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004147int sched_setscheduler(struct task_struct *p, int policy,
4148 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149{
Ingo Molnardd41f592007-07-09 18:51:59 +02004150 int retval, oldprio, oldpolicy = -1, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004152 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
Steven Rostedt66e53932006-06-27 02:54:44 -07004154 /* may grab non-irq protected spin_locks */
4155 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156recheck:
4157 /* double check policy once rq lock held */
4158 if (policy < 0)
4159 policy = oldpolicy = p->policy;
4160 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02004161 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
4162 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08004163 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 /*
4165 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02004166 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
4167 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 */
4169 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004170 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04004171 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02004173 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 return -EINVAL;
4175
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004176 /*
4177 * Allow unprivileged RT tasks to decrease priority:
4178 */
4179 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02004180 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004181 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004182
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004183 if (!lock_task_sighand(p, &flags))
4184 return -ESRCH;
4185 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
4186 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004187
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004188 /* can't set/change the rt policy */
4189 if (policy != p->policy && !rlim_rtprio)
4190 return -EPERM;
4191
4192 /* can't increase priority */
4193 if (param->sched_priority > p->rt_priority &&
4194 param->sched_priority > rlim_rtprio)
4195 return -EPERM;
4196 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004197 /*
4198 * Like positive nice levels, dont allow tasks to
4199 * move out of SCHED_IDLE either:
4200 */
4201 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
4202 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004203
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004204 /* can't change other user's priorities */
4205 if ((current->euid != p->euid) &&
4206 (current->euid != p->uid))
4207 return -EPERM;
4208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209
4210 retval = security_task_setscheduler(p, policy, param);
4211 if (retval)
4212 return retval;
4213 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07004214 * make sure no PI-waiters arrive (or leave) while we are
4215 * changing the priority of the task:
4216 */
4217 spin_lock_irqsave(&p->pi_lock, flags);
4218 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 * To be able to change p->policy safely, the apropriate
4220 * runqueue lock must be held.
4221 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07004222 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 /* recheck policy now with rq lock held */
4224 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
4225 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004226 __task_rq_unlock(rq);
4227 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 goto recheck;
4229 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004230 on_rq = p->se.on_rq;
4231 if (on_rq)
Ingo Molnar8e717b12007-08-09 11:16:46 +02004232 deactivate_task(rq, p, 0, rq_clock(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004234 __setscheduler(rq, p, policy, param->sched_priority);
4235 if (on_rq) {
4236 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 /*
4238 * Reschedule if we are currently running on this runqueue and
Andrew Mortond5f9f942007-05-08 20:27:06 -07004239 * our priority decreased, or if we are not currently running on
4240 * this runqueue and our priority is higher than the current's
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004242 if (task_running(rq, p)) {
4243 if (p->prio > oldprio)
4244 resched_task(rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02004245 } else {
4246 check_preempt_curr(rq, p);
4247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07004249 __task_rq_unlock(rq);
4250 spin_unlock_irqrestore(&p->pi_lock, flags);
4251
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07004252 rt_mutex_adjust_pi(p);
4253
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 return 0;
4255}
4256EXPORT_SYMBOL_GPL(sched_setscheduler);
4257
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004258static int
4259do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 struct sched_param lparam;
4262 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004263 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
4265 if (!param || pid < 0)
4266 return -EINVAL;
4267 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
4268 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004269
4270 rcu_read_lock();
4271 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004273 if (p != NULL)
4274 retval = sched_setscheduler(p, policy, &lparam);
4275 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07004276
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 return retval;
4278}
4279
4280/**
4281 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4282 * @pid: the pid in question.
4283 * @policy: new policy.
4284 * @param: structure containing the new RT priority.
4285 */
4286asmlinkage long sys_sched_setscheduler(pid_t pid, int policy,
4287 struct sched_param __user *param)
4288{
Jason Baronc21761f2006-01-18 17:43:03 -08004289 /* negative values for policy are not valid */
4290 if (policy < 0)
4291 return -EINVAL;
4292
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 return do_sched_setscheduler(pid, policy, param);
4294}
4295
4296/**
4297 * sys_sched_setparam - set/change the RT priority of a thread
4298 * @pid: the pid in question.
4299 * @param: structure containing the new RT priority.
4300 */
4301asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
4302{
4303 return do_sched_setscheduler(pid, -1, param);
4304}
4305
4306/**
4307 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4308 * @pid: the pid in question.
4309 */
4310asmlinkage long sys_sched_getscheduler(pid_t pid)
4311{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004312 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314
4315 if (pid < 0)
4316 goto out_nounlock;
4317
4318 retval = -ESRCH;
4319 read_lock(&tasklist_lock);
4320 p = find_process_by_pid(pid);
4321 if (p) {
4322 retval = security_task_getscheduler(p);
4323 if (!retval)
4324 retval = p->policy;
4325 }
4326 read_unlock(&tasklist_lock);
4327
4328out_nounlock:
4329 return retval;
4330}
4331
4332/**
4333 * sys_sched_getscheduler - get the RT priority of a thread
4334 * @pid: the pid in question.
4335 * @param: structure containing the RT priority.
4336 */
4337asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
4338{
4339 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004340 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
4343 if (!param || pid < 0)
4344 goto out_nounlock;
4345
4346 read_lock(&tasklist_lock);
4347 p = find_process_by_pid(pid);
4348 retval = -ESRCH;
4349 if (!p)
4350 goto out_unlock;
4351
4352 retval = security_task_getscheduler(p);
4353 if (retval)
4354 goto out_unlock;
4355
4356 lp.sched_priority = p->rt_priority;
4357 read_unlock(&tasklist_lock);
4358
4359 /*
4360 * This one might sleep, we cannot do it with a spinlock held ...
4361 */
4362 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
4363
4364out_nounlock:
4365 return retval;
4366
4367out_unlock:
4368 read_unlock(&tasklist_lock);
4369 return retval;
4370}
4371
4372long sched_setaffinity(pid_t pid, cpumask_t new_mask)
4373{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 cpumask_t cpus_allowed;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004375 struct task_struct *p;
4376 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377
Gautham R Shenoy5be93612007-05-09 02:34:04 -07004378 mutex_lock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 read_lock(&tasklist_lock);
4380
4381 p = find_process_by_pid(pid);
4382 if (!p) {
4383 read_unlock(&tasklist_lock);
Gautham R Shenoy5be93612007-05-09 02:34:04 -07004384 mutex_unlock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 return -ESRCH;
4386 }
4387
4388 /*
4389 * It is not safe to call set_cpus_allowed with the
4390 * tasklist_lock held. We will bump the task_struct's
4391 * usage count and then drop tasklist_lock.
4392 */
4393 get_task_struct(p);
4394 read_unlock(&tasklist_lock);
4395
4396 retval = -EPERM;
4397 if ((current->euid != p->euid) && (current->euid != p->uid) &&
4398 !capable(CAP_SYS_NICE))
4399 goto out_unlock;
4400
David Quigleye7834f82006-06-23 02:03:59 -07004401 retval = security_task_setscheduler(p, 0, NULL);
4402 if (retval)
4403 goto out_unlock;
4404
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 cpus_allowed = cpuset_cpus_allowed(p);
4406 cpus_and(new_mask, new_mask, cpus_allowed);
4407 retval = set_cpus_allowed(p, new_mask);
4408
4409out_unlock:
4410 put_task_struct(p);
Gautham R Shenoy5be93612007-05-09 02:34:04 -07004411 mutex_unlock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 return retval;
4413}
4414
4415static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
4416 cpumask_t *new_mask)
4417{
4418 if (len < sizeof(cpumask_t)) {
4419 memset(new_mask, 0, sizeof(cpumask_t));
4420 } else if (len > sizeof(cpumask_t)) {
4421 len = sizeof(cpumask_t);
4422 }
4423 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
4424}
4425
4426/**
4427 * sys_sched_setaffinity - set the cpu affinity of a process
4428 * @pid: pid of the process
4429 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4430 * @user_mask_ptr: user-space pointer to the new cpu mask
4431 */
4432asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
4433 unsigned long __user *user_mask_ptr)
4434{
4435 cpumask_t new_mask;
4436 int retval;
4437
4438 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
4439 if (retval)
4440 return retval;
4441
4442 return sched_setaffinity(pid, new_mask);
4443}
4444
4445/*
4446 * Represents all cpu's present in the system
4447 * In systems capable of hotplug, this map could dynamically grow
4448 * as new cpu's are detected in the system via any platform specific
4449 * method, such as ACPI for e.g.
4450 */
4451
Andi Kleen4cef0c62006-01-11 22:44:57 +01004452cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453EXPORT_SYMBOL(cpu_present_map);
4454
4455#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01004456cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07004457EXPORT_SYMBOL(cpu_online_map);
4458
Andi Kleen4cef0c62006-01-11 22:44:57 +01004459cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07004460EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461#endif
4462
4463long sched_getaffinity(pid_t pid, cpumask_t *mask)
4464{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004465 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467
Gautham R Shenoy5be93612007-05-09 02:34:04 -07004468 mutex_lock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 read_lock(&tasklist_lock);
4470
4471 retval = -ESRCH;
4472 p = find_process_by_pid(pid);
4473 if (!p)
4474 goto out_unlock;
4475
David Quigleye7834f82006-06-23 02:03:59 -07004476 retval = security_task_getscheduler(p);
4477 if (retval)
4478 goto out_unlock;
4479
Jack Steiner2f7016d2006-02-01 03:05:18 -08004480 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
4482out_unlock:
4483 read_unlock(&tasklist_lock);
Gautham R Shenoy5be93612007-05-09 02:34:04 -07004484 mutex_unlock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485
Ulrich Drepper9531b622007-08-09 11:16:46 +02004486 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487}
4488
4489/**
4490 * sys_sched_getaffinity - get the cpu affinity of a process
4491 * @pid: pid of the process
4492 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4493 * @user_mask_ptr: user-space pointer to hold the current cpu mask
4494 */
4495asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
4496 unsigned long __user *user_mask_ptr)
4497{
4498 int ret;
4499 cpumask_t mask;
4500
4501 if (len < sizeof(cpumask_t))
4502 return -EINVAL;
4503
4504 ret = sched_getaffinity(pid, &mask);
4505 if (ret < 0)
4506 return ret;
4507
4508 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
4509 return -EFAULT;
4510
4511 return sizeof(cpumask_t);
4512}
4513
4514/**
4515 * sys_sched_yield - yield the current processor to other threads.
4516 *
Ingo Molnardd41f592007-07-09 18:51:59 +02004517 * This function yields the current CPU to other tasks. If there are no
4518 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 */
4520asmlinkage long sys_sched_yield(void)
4521{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004522 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523
4524 schedstat_inc(rq, yld_cnt);
Ingo Molnardd41f592007-07-09 18:51:59 +02004525 if (unlikely(rq->nr_running == 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 schedstat_inc(rq, yld_act_empty);
Ingo Molnardd41f592007-07-09 18:51:59 +02004527 else
4528 current->sched_class->yield_task(rq, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
4530 /*
4531 * Since we are going to call schedule() anyway, there's
4532 * no need to preempt or enable interrupts:
4533 */
4534 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07004535 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 _raw_spin_unlock(&rq->lock);
4537 preempt_enable_no_resched();
4538
4539 schedule();
4540
4541 return 0;
4542}
4543
Andrew Mortone7b38402006-06-30 01:56:00 -07004544static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07004546#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
4547 __might_sleep(__FILE__, __LINE__);
4548#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07004549 /*
4550 * The BKS might be reacquired before we have dropped
4551 * PREEMPT_ACTIVE, which could trigger a second
4552 * cond_resched() call.
4553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 do {
4555 add_preempt_count(PREEMPT_ACTIVE);
4556 schedule();
4557 sub_preempt_count(PREEMPT_ACTIVE);
4558 } while (need_resched());
4559}
4560
4561int __sched cond_resched(void)
4562{
Ingo Molnar94142322006-12-29 16:48:13 -08004563 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
4564 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 __cond_resched();
4566 return 1;
4567 }
4568 return 0;
4569}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570EXPORT_SYMBOL(cond_resched);
4571
4572/*
4573 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
4574 * call schedule, and on return reacquire the lock.
4575 *
4576 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
4577 * operations here to prevent schedule() from being called twice (once via
4578 * spin_unlock(), once by hand).
4579 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004580int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581{
Jan Kara6df3cec2005-06-13 15:52:32 -07004582 int ret = 0;
4583
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 if (need_lockbreak(lock)) {
4585 spin_unlock(lock);
4586 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07004587 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 spin_lock(lock);
4589 }
Ingo Molnar94142322006-12-29 16:48:13 -08004590 if (need_resched() && system_state == SYSTEM_RUNNING) {
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07004591 spin_release(&lock->dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 _raw_spin_unlock(lock);
4593 preempt_enable_no_resched();
4594 __cond_resched();
Jan Kara6df3cec2005-06-13 15:52:32 -07004595 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 }
Jan Kara6df3cec2005-06-13 15:52:32 -07004598 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600EXPORT_SYMBOL(cond_resched_lock);
4601
4602int __sched cond_resched_softirq(void)
4603{
4604 BUG_ON(!in_softirq());
4605
Ingo Molnar94142322006-12-29 16:48:13 -08004606 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07004607 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 __cond_resched();
4609 local_bh_disable();
4610 return 1;
4611 }
4612 return 0;
4613}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614EXPORT_SYMBOL(cond_resched_softirq);
4615
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616/**
4617 * yield - yield the current processor to other threads.
4618 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004619 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 * thread runnable and calls sys_sched_yield().
4621 */
4622void __sched yield(void)
4623{
4624 set_current_state(TASK_RUNNING);
4625 sys_sched_yield();
4626}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627EXPORT_SYMBOL(yield);
4628
4629/*
4630 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
4631 * that process accounting knows that this is a task in IO wait state.
4632 *
4633 * But don't do that if it is a deliberate, throttling IO wait (this task
4634 * has set its backing_dev_info: the queue against which it should throttle)
4635 */
4636void __sched io_schedule(void)
4637{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004638 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004640 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 atomic_inc(&rq->nr_iowait);
4642 schedule();
4643 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004644 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646EXPORT_SYMBOL(io_schedule);
4647
4648long __sched io_schedule_timeout(long timeout)
4649{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004650 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 long ret;
4652
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004653 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 atomic_inc(&rq->nr_iowait);
4655 ret = schedule_timeout(timeout);
4656 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004657 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 return ret;
4659}
4660
4661/**
4662 * sys_sched_get_priority_max - return maximum RT priority.
4663 * @policy: scheduling class.
4664 *
4665 * this syscall returns the maximum rt_priority that can be used
4666 * by a given scheduling class.
4667 */
4668asmlinkage long sys_sched_get_priority_max(int policy)
4669{
4670 int ret = -EINVAL;
4671
4672 switch (policy) {
4673 case SCHED_FIFO:
4674 case SCHED_RR:
4675 ret = MAX_USER_RT_PRIO-1;
4676 break;
4677 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08004678 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02004679 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 ret = 0;
4681 break;
4682 }
4683 return ret;
4684}
4685
4686/**
4687 * sys_sched_get_priority_min - return minimum RT priority.
4688 * @policy: scheduling class.
4689 *
4690 * this syscall returns the minimum rt_priority that can be used
4691 * by a given scheduling class.
4692 */
4693asmlinkage long sys_sched_get_priority_min(int policy)
4694{
4695 int ret = -EINVAL;
4696
4697 switch (policy) {
4698 case SCHED_FIFO:
4699 case SCHED_RR:
4700 ret = 1;
4701 break;
4702 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08004703 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02004704 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 ret = 0;
4706 }
4707 return ret;
4708}
4709
4710/**
4711 * sys_sched_rr_get_interval - return the default timeslice of a process.
4712 * @pid: pid of the process.
4713 * @interval: userspace pointer to the timeslice value.
4714 *
4715 * this syscall writes the default timeslice value of a given process
4716 * into the user-space timespec buffer. A value of '0' means infinity.
4717 */
4718asmlinkage
4719long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
4720{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004721 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 int retval = -EINVAL;
4723 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724
4725 if (pid < 0)
4726 goto out_nounlock;
4727
4728 retval = -ESRCH;
4729 read_lock(&tasklist_lock);
4730 p = find_process_by_pid(pid);
4731 if (!p)
4732 goto out_unlock;
4733
4734 retval = security_task_getscheduler(p);
4735 if (retval)
4736 goto out_unlock;
4737
Peter Williamsb78709c2006-06-26 16:58:00 +10004738 jiffies_to_timespec(p->policy == SCHED_FIFO ?
Ingo Molnardd41f592007-07-09 18:51:59 +02004739 0 : static_prio_timeslice(p->static_prio), &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 read_unlock(&tasklist_lock);
4741 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
4742out_nounlock:
4743 return retval;
4744out_unlock:
4745 read_unlock(&tasklist_lock);
4746 return retval;
4747}
4748
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004749static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07004750
4751static void show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004754 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 state = p->state ? __ffs(p->state) + 1 : 0;
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004757 printk("%-13.13s %c", p->comm,
4758 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02004759#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 if (state == TASK_RUNNING)
Ingo Molnar4bd77322007-07-11 21:21:47 +02004761 printk(" running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 else
Ingo Molnar4bd77322007-07-11 21:21:47 +02004763 printk(" %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764#else
4765 if (state == TASK_RUNNING)
Ingo Molnar4bd77322007-07-11 21:21:47 +02004766 printk(" running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 else
4768 printk(" %016lx ", thread_saved_pc(p));
4769#endif
4770#ifdef CONFIG_DEBUG_STACK_USAGE
4771 {
Al Viro10ebffd2005-11-13 16:06:56 -08004772 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 while (!*n)
4774 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08004775 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 }
4777#endif
Ingo Molnar4bd77322007-07-11 21:21:47 +02004778 printk("%5lu %5d %6d\n", free, p->pid, p->parent->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779
4780 if (state != TASK_RUNNING)
4781 show_stack(p, NULL);
4782}
4783
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004784void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004786 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
Ingo Molnar4bd77322007-07-11 21:21:47 +02004788#if BITS_PER_LONG == 32
4789 printk(KERN_INFO
4790 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02004792 printk(KERN_INFO
4793 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794#endif
4795 read_lock(&tasklist_lock);
4796 do_each_thread(g, p) {
4797 /*
4798 * reset the NMI-timeout, listing all files on a slow
4799 * console might take alot of time:
4800 */
4801 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07004802 if (!state_filter || (p->state & state_filter))
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004803 show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 } while_each_thread(g, p);
4805
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07004806 touch_all_softlockup_watchdogs();
4807
Ingo Molnardd41f592007-07-09 18:51:59 +02004808#ifdef CONFIG_SCHED_DEBUG
4809 sysrq_sched_debug_show();
4810#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004812 /*
4813 * Only show locks if all tasks are dumped:
4814 */
4815 if (state_filter == -1)
4816 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817}
4818
Ingo Molnar1df21052007-07-09 18:51:58 +02004819void __cpuinit init_idle_bootup_task(struct task_struct *idle)
4820{
Ingo Molnardd41f592007-07-09 18:51:59 +02004821 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02004822}
4823
Ingo Molnarf340c0d2005-06-28 16:40:42 +02004824/**
4825 * init_idle - set up an idle thread for a given CPU
4826 * @idle: task in question
4827 * @cpu: cpu the idle task belongs to
4828 *
4829 * NOTE: this function does not set the idle thread's NEED_RESCHED
4830 * flag, to make booting more robust.
4831 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07004832void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004834 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 unsigned long flags;
4836
Ingo Molnardd41f592007-07-09 18:51:59 +02004837 __sched_fork(idle);
4838 idle->se.exec_start = sched_clock();
4839
Ingo Molnarb29739f2006-06-27 02:54:51 -07004840 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004842 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843
4844 spin_lock_irqsave(&rq->lock, flags);
4845 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07004846#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
4847 idle->oncpu = 1;
4848#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 spin_unlock_irqrestore(&rq->lock, flags);
4850
4851 /* Set the preempt count _outside_ the spinlocks! */
4852#if defined(CONFIG_PREEMPT) && !defined(CONFIG_PREEMPT_BKL)
Al Viroa1261f52005-11-13 16:06:55 -08004853 task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854#else
Al Viroa1261f52005-11-13 16:06:55 -08004855 task_thread_info(idle)->preempt_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004857 /*
4858 * The idle tasks have their own, simple scheduling class:
4859 */
4860 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861}
4862
4863/*
4864 * In a system that switches off the HZ timer nohz_cpu_mask
4865 * indicates which cpus entered this state. This is used
4866 * in the rcu update to wait only for active cpus. For system
4867 * which do not switch off the HZ timer nohz_cpu_mask should
4868 * always be CPU_MASK_NONE.
4869 */
4870cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
4871
Ingo Molnardd41f592007-07-09 18:51:59 +02004872/*
4873 * Increase the granularity value when there are more CPUs,
4874 * because with more CPUs the 'effective latency' as visible
4875 * to users decreases. But the relationship is not linear,
4876 * so pick a second-best guess by going with the log2 of the
4877 * number of CPUs.
4878 *
4879 * This idea comes from the SD scheduler of Con Kolivas:
4880 */
4881static inline void sched_init_granularity(void)
4882{
4883 unsigned int factor = 1 + ilog2(num_online_cpus());
Ingo Molnara5968df2007-07-11 21:21:47 +02004884 const unsigned long gran_limit = 100000000;
Ingo Molnardd41f592007-07-09 18:51:59 +02004885
4886 sysctl_sched_granularity *= factor;
4887 if (sysctl_sched_granularity > gran_limit)
4888 sysctl_sched_granularity = gran_limit;
4889
4890 sysctl_sched_runtime_limit = sysctl_sched_granularity * 4;
4891 sysctl_sched_wakeup_granularity = sysctl_sched_granularity / 2;
4892}
4893
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894#ifdef CONFIG_SMP
4895/*
4896 * This is how migration works:
4897 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07004898 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 * runqueue and wake up that CPU's migration thread.
4900 * 2) we down() the locked semaphore => thread blocks.
4901 * 3) migration thread wakes up (implicitly it forces the migrated
4902 * thread off the CPU)
4903 * 4) it gets the migration request and checks whether the migrated
4904 * task is still in the wrong runqueue.
4905 * 5) if it's in the wrong runqueue then the migration thread removes
4906 * it and puts it into the right queue.
4907 * 6) migration thread up()s the semaphore.
4908 * 7) we wake up and the migration is done.
4909 */
4910
4911/*
4912 * Change a given task's CPU affinity. Migrate the thread to a
4913 * proper CPU and schedule it away if the CPU it's executing on
4914 * is removed from the allowed bitmask.
4915 *
4916 * NOTE: the caller must have a valid reference to the task, the
4917 * task must not exit() & deallocate itself prematurely. The
4918 * call is not atomic; no spinlocks may be held.
4919 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004920int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004922 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004924 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004925 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926
4927 rq = task_rq_lock(p, &flags);
4928 if (!cpus_intersects(new_mask, cpu_online_map)) {
4929 ret = -EINVAL;
4930 goto out;
4931 }
4932
4933 p->cpus_allowed = new_mask;
4934 /* Can the task run on the task's current CPU? If so, we're done */
4935 if (cpu_isset(task_cpu(p), new_mask))
4936 goto out;
4937
4938 if (migrate_task(p, any_online_cpu(new_mask), &req)) {
4939 /* Need help from migration thread: drop lock and wait. */
4940 task_rq_unlock(rq, &flags);
4941 wake_up_process(rq->migration_thread);
4942 wait_for_completion(&req.done);
4943 tlb_migrate_finish(p->mm);
4944 return 0;
4945 }
4946out:
4947 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004948
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 return ret;
4950}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951EXPORT_SYMBOL_GPL(set_cpus_allowed);
4952
4953/*
4954 * Move (not current) task off this cpu, onto dest cpu. We're doing
4955 * this because either it can't run here any more (set_cpus_allowed()
4956 * away from this CPU, or CPU going down), or because we're
4957 * attempting to rebalance this task on exec (sched_exec).
4958 *
4959 * So we race with normal scheduler movements, but that's OK, as long
4960 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07004961 *
4962 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07004964static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004966 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02004967 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968
4969 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07004970 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971
4972 rq_src = cpu_rq(src_cpu);
4973 rq_dest = cpu_rq(dest_cpu);
4974
4975 double_rq_lock(rq_src, rq_dest);
4976 /* Already moved. */
4977 if (task_cpu(p) != src_cpu)
4978 goto out;
4979 /* Affinity changed (again). */
4980 if (!cpu_isset(dest_cpu, p->cpus_allowed))
4981 goto out;
4982
Ingo Molnardd41f592007-07-09 18:51:59 +02004983 on_rq = p->se.on_rq;
4984 if (on_rq)
Ingo Molnar8e717b12007-08-09 11:16:46 +02004985 deactivate_task(rq_src, p, 0, rq_clock(rq_src));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004987 if (on_rq) {
4988 activate_task(rq_dest, p, 0);
4989 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07004991 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992out:
4993 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07004994 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995}
4996
4997/*
4998 * migration_thread - this is a highprio system thread that performs
4999 * thread migration by bumping thread off CPU then 'pushing' onto
5000 * another runqueue.
5001 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005002static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005005 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006
5007 rq = cpu_rq(cpu);
5008 BUG_ON(rq->migration_thread != current);
5009
5010 set_current_state(TASK_INTERRUPTIBLE);
5011 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005012 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 spin_lock_irq(&rq->lock);
5016
5017 if (cpu_is_offline(cpu)) {
5018 spin_unlock_irq(&rq->lock);
5019 goto wait_to_die;
5020 }
5021
5022 if (rq->active_balance) {
5023 active_load_balance(rq, cpu);
5024 rq->active_balance = 0;
5025 }
5026
5027 head = &rq->migration_queue;
5028
5029 if (list_empty(head)) {
5030 spin_unlock_irq(&rq->lock);
5031 schedule();
5032 set_current_state(TASK_INTERRUPTIBLE);
5033 continue;
5034 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07005035 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 list_del_init(head->next);
5037
Nick Piggin674311d2005-06-25 14:57:27 -07005038 spin_unlock(&rq->lock);
5039 __migrate_task(req->task, cpu, req->dest_cpu);
5040 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041
5042 complete(&req->done);
5043 }
5044 __set_current_state(TASK_RUNNING);
5045 return 0;
5046
5047wait_to_die:
5048 /* Wait for kthread_stop */
5049 set_current_state(TASK_INTERRUPTIBLE);
5050 while (!kthread_should_stop()) {
5051 schedule();
5052 set_current_state(TASK_INTERRUPTIBLE);
5053 }
5054 __set_current_state(TASK_RUNNING);
5055 return 0;
5056}
5057
5058#ifdef CONFIG_HOTPLUG_CPU
Kirill Korotaev054b9102006-12-10 02:20:11 -08005059/*
5060 * Figure out where task on dead CPU should go, use force if neccessary.
5061 * NOTE: interrupts should be disabled by the caller
5062 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005063static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064{
Kirill Korotaevefc30812006-06-27 02:54:32 -07005065 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005067 struct rq *rq;
5068 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069
Kirill Korotaevefc30812006-06-27 02:54:32 -07005070restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 /* On same node? */
5072 mask = node_to_cpumask(cpu_to_node(dead_cpu));
Ingo Molnar48f24c42006-07-03 00:25:40 -07005073 cpus_and(mask, mask, p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 dest_cpu = any_online_cpu(mask);
5075
5076 /* On any allowed CPU? */
5077 if (dest_cpu == NR_CPUS)
Ingo Molnar48f24c42006-07-03 00:25:40 -07005078 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079
5080 /* No more Mr. Nice Guy. */
5081 if (dest_cpu == NR_CPUS) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07005082 rq = task_rq_lock(p, &flags);
5083 cpus_setall(p->cpus_allowed);
5084 dest_cpu = any_online_cpu(p->cpus_allowed);
Kirill Korotaevefc30812006-06-27 02:54:32 -07005085 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086
5087 /*
5088 * Don't tell them about moving exiting tasks or
5089 * kernel threads (both mm NULL), since they never
5090 * leave kernel.
5091 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005092 if (p->mm && printk_ratelimit())
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 printk(KERN_INFO "process %d (%s) no "
5094 "longer affine to cpu%d\n",
Ingo Molnar48f24c42006-07-03 00:25:40 -07005095 p->pid, p->comm, dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07005097 if (!__migrate_task(p, dead_cpu, dest_cpu))
Kirill Korotaevefc30812006-06-27 02:54:32 -07005098 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099}
5100
5101/*
5102 * While a dead CPU has no uninterruptible tasks queued at this point,
5103 * it might still have a nonzero ->nr_uninterruptible counter, because
5104 * for performance reasons the counter is not stricly tracking tasks to
5105 * their home CPUs. So we just add the counter to another CPU's counter,
5106 * to keep the global sum constant after CPU-down:
5107 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07005108static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005110 struct rq *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 unsigned long flags;
5112
5113 local_irq_save(flags);
5114 double_rq_lock(rq_src, rq_dest);
5115 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
5116 rq_src->nr_uninterruptible = 0;
5117 double_rq_unlock(rq_src, rq_dest);
5118 local_irq_restore(flags);
5119}
5120
5121/* Run through task list and migrate tasks from the dead cpu. */
5122static void migrate_live_tasks(int src_cpu)
5123{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005124 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
5126 write_lock_irq(&tasklist_lock);
5127
Ingo Molnar48f24c42006-07-03 00:25:40 -07005128 do_each_thread(t, p) {
5129 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 continue;
5131
Ingo Molnar48f24c42006-07-03 00:25:40 -07005132 if (task_cpu(p) == src_cpu)
5133 move_task_off_dead_cpu(src_cpu, p);
5134 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135
5136 write_unlock_irq(&tasklist_lock);
5137}
5138
Ingo Molnardd41f592007-07-09 18:51:59 +02005139/*
5140 * Schedules idle task to be the next runnable task on current CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 * It does so by boosting its priority to highest possible and adding it to
Ingo Molnar48f24c42006-07-03 00:25:40 -07005142 * the _front_ of the runqueue. Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 */
5144void sched_idle_next(void)
5145{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005146 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07005147 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 struct task_struct *p = rq->idle;
5149 unsigned long flags;
5150
5151 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005152 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153
Ingo Molnar48f24c42006-07-03 00:25:40 -07005154 /*
5155 * Strictly not necessary since rest of the CPUs are stopped by now
5156 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 */
5158 spin_lock_irqsave(&rq->lock, flags);
5159
Ingo Molnardd41f592007-07-09 18:51:59 +02005160 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005161
5162 /* Add idle task to the _front_ of its priority queue: */
Ingo Molnardd41f592007-07-09 18:51:59 +02005163 activate_idle_task(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164
5165 spin_unlock_irqrestore(&rq->lock, flags);
5166}
5167
Ingo Molnar48f24c42006-07-03 00:25:40 -07005168/*
5169 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 * offline.
5171 */
5172void idle_task_exit(void)
5173{
5174 struct mm_struct *mm = current->active_mm;
5175
5176 BUG_ON(cpu_online(smp_processor_id()));
5177
5178 if (mm != &init_mm)
5179 switch_mm(mm, &init_mm, current);
5180 mmdrop(mm);
5181}
5182
Kirill Korotaev054b9102006-12-10 02:20:11 -08005183/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005184static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005186 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187
5188 /* Must be exiting, otherwise would be on tasklist. */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005189 BUG_ON(p->exit_state != EXIT_ZOMBIE && p->exit_state != EXIT_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190
5191 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07005192 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193
Ingo Molnar48f24c42006-07-03 00:25:40 -07005194 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195
5196 /*
5197 * Drop lock around migration; if someone else moves it,
5198 * that's OK. No task can be added to this CPU, so iteration is
5199 * fine.
Kirill Korotaev054b9102006-12-10 02:20:11 -08005200 * NOTE: interrupts should be left disabled --dev@
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 */
Kirill Korotaev054b9102006-12-10 02:20:11 -08005202 spin_unlock(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005203 move_task_off_dead_cpu(dead_cpu, p);
Kirill Korotaev054b9102006-12-10 02:20:11 -08005204 spin_lock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205
Ingo Molnar48f24c42006-07-03 00:25:40 -07005206 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207}
5208
5209/* release_task() removes task from tasklist, so we won't find dead tasks. */
5210static void migrate_dead_tasks(unsigned int dead_cpu)
5211{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005212 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005213 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214
Ingo Molnardd41f592007-07-09 18:51:59 +02005215 for ( ; ; ) {
5216 if (!rq->nr_running)
5217 break;
5218 next = pick_next_task(rq, rq->curr, rq_clock(rq));
5219 if (!next)
5220 break;
5221 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02005222
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 }
5224}
5225#endif /* CONFIG_HOTPLUG_CPU */
5226
Nick Piggine692ab52007-07-26 13:40:43 +02005227#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5228
5229static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005230 {
5231 .procname = "sched_domain",
5232 .mode = 0755,
5233 },
Nick Piggine692ab52007-07-26 13:40:43 +02005234 {0,},
5235};
5236
5237static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005238 {
5239 .procname = "kernel",
5240 .mode = 0755,
5241 .child = sd_ctl_dir,
5242 },
Nick Piggine692ab52007-07-26 13:40:43 +02005243 {0,},
5244};
5245
5246static struct ctl_table *sd_alloc_ctl_entry(int n)
5247{
5248 struct ctl_table *entry =
5249 kmalloc(n * sizeof(struct ctl_table), GFP_KERNEL);
5250
5251 BUG_ON(!entry);
5252 memset(entry, 0, n * sizeof(struct ctl_table));
5253
5254 return entry;
5255}
5256
5257static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02005258set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02005259 const char *procname, void *data, int maxlen,
5260 mode_t mode, proc_handler *proc_handler)
5261{
Nick Piggine692ab52007-07-26 13:40:43 +02005262 entry->procname = procname;
5263 entry->data = data;
5264 entry->maxlen = maxlen;
5265 entry->mode = mode;
5266 entry->proc_handler = proc_handler;
5267}
5268
5269static struct ctl_table *
5270sd_alloc_ctl_domain_table(struct sched_domain *sd)
5271{
5272 struct ctl_table *table = sd_alloc_ctl_entry(14);
5273
Alexey Dobriyane0361852007-08-09 11:16:46 +02005274 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005275 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005276 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005277 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005278 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005279 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005280 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005281 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005282 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005283 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005284 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005285 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005286 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005287 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005288 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02005289 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005290 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02005291 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005292 set_table_entry(&table[10], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02005293 &sd->cache_nice_tries,
5294 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005295 set_table_entry(&table[12], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02005296 sizeof(int), 0644, proc_dointvec_minmax);
5297
5298 return table;
5299}
5300
5301static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
5302{
5303 struct ctl_table *entry, *table;
5304 struct sched_domain *sd;
5305 int domain_num = 0, i;
5306 char buf[32];
5307
5308 for_each_domain(cpu, sd)
5309 domain_num++;
5310 entry = table = sd_alloc_ctl_entry(domain_num + 1);
5311
5312 i = 0;
5313 for_each_domain(cpu, sd) {
5314 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005315 entry->procname = kstrdup(buf, GFP_KERNEL);
5316 entry->mode = 0755;
5317 entry->child = sd_alloc_ctl_domain_table(sd);
5318 entry++;
5319 i++;
5320 }
5321 return table;
5322}
5323
5324static struct ctl_table_header *sd_sysctl_header;
5325static void init_sched_domain_sysctl(void)
5326{
5327 int i, cpu_num = num_online_cpus();
5328 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
5329 char buf[32];
5330
5331 sd_ctl_dir[0].child = entry;
5332
5333 for (i = 0; i < cpu_num; i++, entry++) {
5334 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005335 entry->procname = kstrdup(buf, GFP_KERNEL);
5336 entry->mode = 0755;
5337 entry->child = sd_alloc_ctl_cpu_table(i);
5338 }
5339 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
5340}
5341#else
5342static void init_sched_domain_sysctl(void)
5343{
5344}
5345#endif
5346
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347/*
5348 * migration_call - callback that gets triggered when a CPU is added.
5349 * Here we can start up the necessary migration thread for the new CPU.
5350 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005351static int __cpuinit
5352migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005355 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005357 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358
5359 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005360 case CPU_LOCK_ACQUIRE:
5361 mutex_lock(&sched_hotcpu_mutex);
5362 break;
5363
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005365 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02005366 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 if (IS_ERR(p))
5368 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 kthread_bind(p, cpu);
5370 /* Must be high prio: stop_machine expects to yield to it. */
5371 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02005372 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 task_rq_unlock(rq, &flags);
5374 cpu_rq(cpu)->migration_thread = p;
5375 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005376
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005378 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 /* Strictly unneccessary, as first user will wake it. */
5380 wake_up_process(cpu_rq(cpu)->migration_thread);
5381 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005382
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383#ifdef CONFIG_HOTPLUG_CPU
5384 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005385 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07005386 if (!cpu_rq(cpu)->migration_thread)
5387 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08005389 kthread_bind(cpu_rq(cpu)->migration_thread,
5390 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 kthread_stop(cpu_rq(cpu)->migration_thread);
5392 cpu_rq(cpu)->migration_thread = NULL;
5393 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005394
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005396 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 migrate_live_tasks(cpu);
5398 rq = cpu_rq(cpu);
5399 kthread_stop(rq->migration_thread);
5400 rq->migration_thread = NULL;
5401 /* Idle task back to normal (off runqueue, low prio) */
5402 rq = task_rq_lock(rq->idle, &flags);
Ingo Molnar8e717b12007-08-09 11:16:46 +02005403 deactivate_task(rq, rq->idle, 0, rq_clock(rq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02005405 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
5406 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 migrate_dead_tasks(cpu);
5408 task_rq_unlock(rq, &flags);
5409 migrate_nr_uninterruptible(rq);
5410 BUG_ON(rq->nr_running != 0);
5411
5412 /* No need to migrate the tasks: it was best-effort if
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005413 * they didn't take sched_hotcpu_mutex. Just wake up
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 * the requestors. */
5415 spin_lock_irq(&rq->lock);
5416 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005417 struct migration_req *req;
5418
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07005420 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 list_del_init(&req->list);
5422 complete(&req->done);
5423 }
5424 spin_unlock_irq(&rq->lock);
5425 break;
5426#endif
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005427 case CPU_LOCK_RELEASE:
5428 mutex_unlock(&sched_hotcpu_mutex);
5429 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 }
5431 return NOTIFY_OK;
5432}
5433
5434/* Register at highest priority so that task migration (migrate_all_tasks)
5435 * happens before everything else.
5436 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07005437static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 .notifier_call = migration_call,
5439 .priority = 10
5440};
5441
5442int __init migration_init(void)
5443{
5444 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07005445 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005446
5447 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07005448 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
5449 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 migration_call(&migration_notifier, CPU_ONLINE, cpu);
5451 register_cpu_notifier(&migration_notifier);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005452
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 return 0;
5454}
5455#endif
5456
5457#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07005458
5459/* Number of possible processor ids */
5460int nr_cpu_ids __read_mostly = NR_CPUS;
5461EXPORT_SYMBOL(nr_cpu_ids);
5462
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07005463#undef SCHED_DOMAIN_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464#ifdef SCHED_DOMAIN_DEBUG
5465static void sched_domain_debug(struct sched_domain *sd, int cpu)
5466{
5467 int level = 0;
5468
Nick Piggin41c7ce92005-06-25 14:57:24 -07005469 if (!sd) {
5470 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
5471 return;
5472 }
5473
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
5475
5476 do {
5477 int i;
5478 char str[NR_CPUS];
5479 struct sched_group *group = sd->groups;
5480 cpumask_t groupmask;
5481
5482 cpumask_scnprintf(str, NR_CPUS, sd->span);
5483 cpus_clear(groupmask);
5484
5485 printk(KERN_DEBUG);
5486 for (i = 0; i < level + 1; i++)
5487 printk(" ");
5488 printk("domain %d: ", level);
5489
5490 if (!(sd->flags & SD_LOAD_BALANCE)) {
5491 printk("does not load-balance\n");
5492 if (sd->parent)
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005493 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
5494 " has parent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 break;
5496 }
5497
5498 printk("span %s\n", str);
5499
5500 if (!cpu_isset(cpu, sd->span))
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005501 printk(KERN_ERR "ERROR: domain->span does not contain "
5502 "CPU%d\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 if (!cpu_isset(cpu, group->cpumask))
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005504 printk(KERN_ERR "ERROR: domain->groups does not contain"
5505 " CPU%d\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506
5507 printk(KERN_DEBUG);
5508 for (i = 0; i < level + 2; i++)
5509 printk(" ");
5510 printk("groups:");
5511 do {
5512 if (!group) {
5513 printk("\n");
5514 printk(KERN_ERR "ERROR: group is NULL\n");
5515 break;
5516 }
5517
Eric Dumazet5517d862007-05-08 00:32:57 -07005518 if (!group->__cpu_power) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 printk("\n");
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005520 printk(KERN_ERR "ERROR: domain->cpu_power not "
5521 "set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 }
5523
5524 if (!cpus_weight(group->cpumask)) {
5525 printk("\n");
5526 printk(KERN_ERR "ERROR: empty group\n");
5527 }
5528
5529 if (cpus_intersects(groupmask, group->cpumask)) {
5530 printk("\n");
5531 printk(KERN_ERR "ERROR: repeated CPUs\n");
5532 }
5533
5534 cpus_or(groupmask, groupmask, group->cpumask);
5535
5536 cpumask_scnprintf(str, NR_CPUS, group->cpumask);
5537 printk(" %s", str);
5538
5539 group = group->next;
5540 } while (group != sd->groups);
5541 printk("\n");
5542
5543 if (!cpus_equal(sd->span, groupmask))
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005544 printk(KERN_ERR "ERROR: groups don't span "
5545 "domain->span\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546
5547 level++;
5548 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005549 if (!sd)
5550 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005552 if (!cpus_subset(groupmask, sd->span))
5553 printk(KERN_ERR "ERROR: parent span is not a superset "
5554 "of domain->span\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555
5556 } while (sd);
5557}
5558#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07005559# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560#endif
5561
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07005562static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005563{
5564 if (cpus_weight(sd->span) == 1)
5565 return 1;
5566
5567 /* Following flags need at least 2 groups */
5568 if (sd->flags & (SD_LOAD_BALANCE |
5569 SD_BALANCE_NEWIDLE |
5570 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005571 SD_BALANCE_EXEC |
5572 SD_SHARE_CPUPOWER |
5573 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005574 if (sd->groups != sd->groups->next)
5575 return 0;
5576 }
5577
5578 /* Following flags don't use groups */
5579 if (sd->flags & (SD_WAKE_IDLE |
5580 SD_WAKE_AFFINE |
5581 SD_WAKE_BALANCE))
5582 return 0;
5583
5584 return 1;
5585}
5586
Ingo Molnar48f24c42006-07-03 00:25:40 -07005587static int
5588sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005589{
5590 unsigned long cflags = sd->flags, pflags = parent->flags;
5591
5592 if (sd_degenerate(parent))
5593 return 1;
5594
5595 if (!cpus_equal(sd->span, parent->span))
5596 return 0;
5597
5598 /* Does parent contain flags not in child? */
5599 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
5600 if (cflags & SD_WAKE_AFFINE)
5601 pflags &= ~SD_WAKE_BALANCE;
5602 /* Flags needing groups don't count if only 1 group in parent */
5603 if (parent->groups == parent->groups->next) {
5604 pflags &= ~(SD_LOAD_BALANCE |
5605 SD_BALANCE_NEWIDLE |
5606 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005607 SD_BALANCE_EXEC |
5608 SD_SHARE_CPUPOWER |
5609 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07005610 }
5611 if (~cflags & pflags)
5612 return 0;
5613
5614 return 1;
5615}
5616
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617/*
5618 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
5619 * hold the hotplug lock.
5620 */
John Hawkes9c1cfda2005-09-06 15:18:14 -07005621static void cpu_attach_domain(struct sched_domain *sd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005623 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07005624 struct sched_domain *tmp;
5625
5626 /* Remove the sched domains which do not contribute to scheduling. */
5627 for (tmp = sd; tmp; tmp = tmp->parent) {
5628 struct sched_domain *parent = tmp->parent;
5629 if (!parent)
5630 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005631 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005632 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005633 if (parent->parent)
5634 parent->parent->child = tmp;
5635 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07005636 }
5637
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005638 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005639 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005640 if (sd)
5641 sd->child = NULL;
5642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643
5644 sched_domain_debug(sd, cpu);
5645
Nick Piggin674311d2005-06-25 14:57:27 -07005646 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647}
5648
5649/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08005650static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651
5652/* Setup the mask of cpus configured for isolated domains */
5653static int __init isolated_cpu_setup(char *str)
5654{
5655 int ints[NR_CPUS], i;
5656
5657 str = get_options(str, ARRAY_SIZE(ints), ints);
5658 cpus_clear(cpu_isolated_map);
5659 for (i = 1; i <= ints[0]; i++)
5660 if (ints[i] < NR_CPUS)
5661 cpu_set(ints[i], cpu_isolated_map);
5662 return 1;
5663}
5664
5665__setup ("isolcpus=", isolated_cpu_setup);
5666
5667/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005668 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
5669 * to a function which identifies what group(along with sched group) a CPU
5670 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
5671 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 *
5673 * init_sched_build_groups will build a circular linked list of the groups
5674 * covered by the given span, and will set each group's ->cpumask correctly,
5675 * and ->cpu_power to 0.
5676 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005677static void
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005678init_sched_build_groups(cpumask_t span, const cpumask_t *cpu_map,
5679 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
5680 struct sched_group **sg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681{
5682 struct sched_group *first = NULL, *last = NULL;
5683 cpumask_t covered = CPU_MASK_NONE;
5684 int i;
5685
5686 for_each_cpu_mask(i, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005687 struct sched_group *sg;
5688 int group = group_fn(i, cpu_map, &sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 int j;
5690
5691 if (cpu_isset(i, covered))
5692 continue;
5693
5694 sg->cpumask = CPU_MASK_NONE;
Eric Dumazet5517d862007-05-08 00:32:57 -07005695 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
5697 for_each_cpu_mask(j, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005698 if (group_fn(j, cpu_map, NULL) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 continue;
5700
5701 cpu_set(j, covered);
5702 cpu_set(j, sg->cpumask);
5703 }
5704 if (!first)
5705 first = sg;
5706 if (last)
5707 last->next = sg;
5708 last = sg;
5709 }
5710 last->next = first;
5711}
5712
John Hawkes9c1cfda2005-09-06 15:18:14 -07005713#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714
John Hawkes9c1cfda2005-09-06 15:18:14 -07005715#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08005716
John Hawkes9c1cfda2005-09-06 15:18:14 -07005717/**
5718 * find_next_best_node - find the next node to include in a sched_domain
5719 * @node: node whose sched_domain we're building
5720 * @used_nodes: nodes already in the sched_domain
5721 *
5722 * Find the next node to include in a given scheduling domain. Simply
5723 * finds the closest node not already in the @used_nodes map.
5724 *
5725 * Should use nodemask_t.
5726 */
5727static int find_next_best_node(int node, unsigned long *used_nodes)
5728{
5729 int i, n, val, min_val, best_node = 0;
5730
5731 min_val = INT_MAX;
5732
5733 for (i = 0; i < MAX_NUMNODES; i++) {
5734 /* Start at @node */
5735 n = (node + i) % MAX_NUMNODES;
5736
5737 if (!nr_cpus_node(n))
5738 continue;
5739
5740 /* Skip already used nodes */
5741 if (test_bit(n, used_nodes))
5742 continue;
5743
5744 /* Simple min distance search */
5745 val = node_distance(node, n);
5746
5747 if (val < min_val) {
5748 min_val = val;
5749 best_node = n;
5750 }
5751 }
5752
5753 set_bit(best_node, used_nodes);
5754 return best_node;
5755}
5756
5757/**
5758 * sched_domain_node_span - get a cpumask for a node's sched_domain
5759 * @node: node whose cpumask we're constructing
5760 * @size: number of nodes to include in this span
5761 *
5762 * Given a node, construct a good cpumask for its sched_domain to span. It
5763 * should be one that prevents unnecessary balancing, but also spreads tasks
5764 * out optimally.
5765 */
5766static cpumask_t sched_domain_node_span(int node)
5767{
John Hawkes9c1cfda2005-09-06 15:18:14 -07005768 DECLARE_BITMAP(used_nodes, MAX_NUMNODES);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005769 cpumask_t span, nodemask;
5770 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07005771
5772 cpus_clear(span);
5773 bitmap_zero(used_nodes, MAX_NUMNODES);
5774
5775 nodemask = node_to_cpumask(node);
5776 cpus_or(span, span, nodemask);
5777 set_bit(node, used_nodes);
5778
5779 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
5780 int next_node = find_next_best_node(node, used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005781
John Hawkes9c1cfda2005-09-06 15:18:14 -07005782 nodemask = node_to_cpumask(next_node);
5783 cpus_or(span, span, nodemask);
5784 }
5785
5786 return span;
5787}
5788#endif
5789
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07005790int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005791
John Hawkes9c1cfda2005-09-06 15:18:14 -07005792/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07005793 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07005794 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795#ifdef CONFIG_SCHED_SMT
5796static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005797static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005798
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005799static int cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map,
5800 struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005802 if (sg)
5803 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 return cpu;
5805}
5806#endif
5807
Ingo Molnar48f24c42006-07-03 00:25:40 -07005808/*
5809 * multi-core sched-domains:
5810 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005811#ifdef CONFIG_SCHED_MC
5812static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005813static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005814#endif
5815
5816#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005817static int cpu_to_core_group(int cpu, const cpumask_t *cpu_map,
5818 struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005819{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005820 int group;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005821 cpumask_t mask = cpu_sibling_map[cpu];
5822 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005823 group = first_cpu(mask);
5824 if (sg)
5825 *sg = &per_cpu(sched_group_core, group);
5826 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005827}
5828#elif defined(CONFIG_SCHED_MC)
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005829static int cpu_to_core_group(int cpu, const cpumask_t *cpu_map,
5830 struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005831{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005832 if (sg)
5833 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005834 return cpu;
5835}
5836#endif
5837
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005839static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005840
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005841static int cpu_to_phys_group(int cpu, const cpumask_t *cpu_map,
5842 struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005844 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005845#ifdef CONFIG_SCHED_MC
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005846 cpumask_t mask = cpu_coregroup_map(cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005847 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005848 group = first_cpu(mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005849#elif defined(CONFIG_SCHED_SMT)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005850 cpumask_t mask = cpu_sibling_map[cpu];
5851 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005852 group = first_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005854 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005856 if (sg)
5857 *sg = &per_cpu(sched_group_phys, group);
5858 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859}
5860
5861#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07005862/*
5863 * The init_sched_build_groups can't handle what we want to do with node
5864 * groups, so roll our own. Now each node has its own list of groups which
5865 * gets dynamically allocated.
5866 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867static DEFINE_PER_CPU(struct sched_domain, node_domains);
John Hawkesd1b55132005-09-06 15:18:14 -07005868static struct sched_group **sched_group_nodes_bycpu[NR_CPUS];
John Hawkes9c1cfda2005-09-06 15:18:14 -07005869
5870static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005871static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07005872
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005873static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
5874 struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005876 cpumask_t nodemask = node_to_cpumask(cpu_to_node(cpu));
5877 int group;
5878
5879 cpus_and(nodemask, nodemask, *cpu_map);
5880 group = first_cpu(nodemask);
5881
5882 if (sg)
5883 *sg = &per_cpu(sched_group_allnodes, group);
5884 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005886
Siddha, Suresh B08069032006-03-27 01:15:23 -08005887static void init_numa_sched_groups_power(struct sched_group *group_head)
5888{
5889 struct sched_group *sg = group_head;
5890 int j;
5891
5892 if (!sg)
5893 return;
5894next_sg:
5895 for_each_cpu_mask(j, sg->cpumask) {
5896 struct sched_domain *sd;
5897
5898 sd = &per_cpu(phys_domains, j);
5899 if (j != first_cpu(sd->groups->cpumask)) {
5900 /*
5901 * Only add "power" once for each
5902 * physical package.
5903 */
5904 continue;
5905 }
5906
Eric Dumazet5517d862007-05-08 00:32:57 -07005907 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08005908 }
5909 sg = sg->next;
5910 if (sg != group_head)
5911 goto next_sg;
5912}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913#endif
5914
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005915#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005916/* Free memory allocated for various sched_group structures */
5917static void free_sched_groups(const cpumask_t *cpu_map)
5918{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005919 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005920
5921 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005922 struct sched_group **sched_group_nodes
5923 = sched_group_nodes_bycpu[cpu];
5924
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005925 if (!sched_group_nodes)
5926 continue;
5927
5928 for (i = 0; i < MAX_NUMNODES; i++) {
5929 cpumask_t nodemask = node_to_cpumask(i);
5930 struct sched_group *oldsg, *sg = sched_group_nodes[i];
5931
5932 cpus_and(nodemask, nodemask, *cpu_map);
5933 if (cpus_empty(nodemask))
5934 continue;
5935
5936 if (sg == NULL)
5937 continue;
5938 sg = sg->next;
5939next_sg:
5940 oldsg = sg;
5941 sg = sg->next;
5942 kfree(oldsg);
5943 if (oldsg != sched_group_nodes[i])
5944 goto next_sg;
5945 }
5946 kfree(sched_group_nodes);
5947 sched_group_nodes_bycpu[cpu] = NULL;
5948 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005949}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005950#else
5951static void free_sched_groups(const cpumask_t *cpu_map)
5952{
5953}
5954#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005955
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005957 * Initialize sched groups cpu_power.
5958 *
5959 * cpu_power indicates the capacity of sched group, which is used while
5960 * distributing the load between different sched groups in a sched domain.
5961 * Typically cpu_power for all the groups in a sched domain will be same unless
5962 * there are asymmetries in the topology. If there are asymmetries, group
5963 * having more cpu_power will pickup more load compared to the group having
5964 * less cpu_power.
5965 *
5966 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
5967 * the maximum number of tasks a group can handle in the presence of other idle
5968 * or lightly loaded groups in the same sched domain.
5969 */
5970static void init_sched_groups_power(int cpu, struct sched_domain *sd)
5971{
5972 struct sched_domain *child;
5973 struct sched_group *group;
5974
5975 WARN_ON(!sd || !sd->groups);
5976
5977 if (cpu != first_cpu(sd->groups->cpumask))
5978 return;
5979
5980 child = sd->child;
5981
Eric Dumazet5517d862007-05-08 00:32:57 -07005982 sd->groups->__cpu_power = 0;
5983
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005984 /*
5985 * For perf policy, if the groups in child domain share resources
5986 * (for example cores sharing some portions of the cache hierarchy
5987 * or SMT), then set this domain groups cpu_power such that each group
5988 * can handle only one task, when there are other idle groups in the
5989 * same sched domain.
5990 */
5991 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
5992 (child->flags &
5993 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07005994 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005995 return;
5996 }
5997
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005998 /*
5999 * add cpu_power of each child group to this groups cpu_power
6000 */
6001 group = child->groups;
6002 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07006003 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006004 group = group->next;
6005 } while (group != child->groups);
6006}
6007
6008/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006009 * Build sched domains for a given set of cpus and attach the sched domains
6010 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006012static int build_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013{
6014 int i;
John Hawkesd1b55132005-09-06 15:18:14 -07006015#ifdef CONFIG_NUMA
6016 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006017 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07006018
6019 /*
6020 * Allocate the per-node list of sched groups
6021 */
Ingo Molnardd41f592007-07-09 18:51:59 +02006022 sched_group_nodes = kzalloc(sizeof(struct sched_group *)*MAX_NUMNODES,
Srivatsa Vaddagirid3a5aa92006-06-27 02:54:39 -07006023 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07006024 if (!sched_group_nodes) {
6025 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006026 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07006027 }
6028 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
6029#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030
6031 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006032 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006034 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 struct sched_domain *sd = NULL, *p;
6036 cpumask_t nodemask = node_to_cpumask(cpu_to_node(i));
6037
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006038 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039
6040#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02006041 if (cpus_weight(*cpu_map) >
6042 SD_NODES_PER_DOMAIN*cpus_weight(nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07006043 sd = &per_cpu(allnodes_domains, i);
6044 *sd = SD_ALLNODES_INIT;
6045 sd->span = *cpu_map;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006046 cpu_to_allnodes_group(i, cpu_map, &sd->groups);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006047 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006048 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006049 } else
6050 p = NULL;
6051
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 sd = &per_cpu(node_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 *sd = SD_NODE_INIT;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006054 sd->span = sched_domain_node_span(cpu_to_node(i));
6055 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006056 if (p)
6057 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006058 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059#endif
6060
6061 p = sd;
6062 sd = &per_cpu(phys_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 *sd = SD_CPU_INIT;
6064 sd->span = nodemask;
6065 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006066 if (p)
6067 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006068 cpu_to_phys_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006070#ifdef CONFIG_SCHED_MC
6071 p = sd;
6072 sd = &per_cpu(core_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006073 *sd = SD_MC_INIT;
6074 sd->span = cpu_coregroup_map(i);
6075 cpus_and(sd->span, sd->span, *cpu_map);
6076 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006077 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006078 cpu_to_core_group(i, cpu_map, &sd->groups);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006079#endif
6080
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081#ifdef CONFIG_SCHED_SMT
6082 p = sd;
6083 sd = &per_cpu(cpu_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 *sd = SD_SIBLING_INIT;
6085 sd->span = cpu_sibling_map[i];
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006086 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006088 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006089 cpu_to_cpu_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090#endif
6091 }
6092
6093#ifdef CONFIG_SCHED_SMT
6094 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07006095 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 cpumask_t this_sibling_map = cpu_sibling_map[i];
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006097 cpus_and(this_sibling_map, this_sibling_map, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 if (i != first_cpu(this_sibling_map))
6099 continue;
6100
Ingo Molnardd41f592007-07-09 18:51:59 +02006101 init_sched_build_groups(this_sibling_map, cpu_map,
6102 &cpu_to_cpu_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103 }
6104#endif
6105
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006106#ifdef CONFIG_SCHED_MC
6107 /* Set up multi-core groups */
6108 for_each_cpu_mask(i, *cpu_map) {
6109 cpumask_t this_core_map = cpu_coregroup_map(i);
6110 cpus_and(this_core_map, this_core_map, *cpu_map);
6111 if (i != first_cpu(this_core_map))
6112 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02006113 init_sched_build_groups(this_core_map, cpu_map,
6114 &cpu_to_core_group);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006115 }
6116#endif
6117
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 /* Set up physical groups */
6119 for (i = 0; i < MAX_NUMNODES; i++) {
6120 cpumask_t nodemask = node_to_cpumask(i);
6121
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006122 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 if (cpus_empty(nodemask))
6124 continue;
6125
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006126 init_sched_build_groups(nodemask, cpu_map, &cpu_to_phys_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 }
6128
6129#ifdef CONFIG_NUMA
6130 /* Set up node groups */
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006131 if (sd_allnodes)
Ingo Molnardd41f592007-07-09 18:51:59 +02006132 init_sched_build_groups(*cpu_map, cpu_map,
6133 &cpu_to_allnodes_group);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006134
6135 for (i = 0; i < MAX_NUMNODES; i++) {
6136 /* Set up node groups */
6137 struct sched_group *sg, *prev;
6138 cpumask_t nodemask = node_to_cpumask(i);
6139 cpumask_t domainspan;
6140 cpumask_t covered = CPU_MASK_NONE;
6141 int j;
6142
6143 cpus_and(nodemask, nodemask, *cpu_map);
John Hawkesd1b55132005-09-06 15:18:14 -07006144 if (cpus_empty(nodemask)) {
6145 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006146 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07006147 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006148
6149 domainspan = sched_domain_node_span(i);
6150 cpus_and(domainspan, domainspan, *cpu_map);
6151
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006152 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006153 if (!sg) {
6154 printk(KERN_WARNING "Can not alloc domain group for "
6155 "node %d\n", i);
6156 goto error;
6157 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006158 sched_group_nodes[i] = sg;
6159 for_each_cpu_mask(j, nodemask) {
6160 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02006161
John Hawkes9c1cfda2005-09-06 15:18:14 -07006162 sd = &per_cpu(node_domains, j);
6163 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006164 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006165 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006166 sg->cpumask = nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006167 sg->next = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006168 cpus_or(covered, covered, nodemask);
6169 prev = sg;
6170
6171 for (j = 0; j < MAX_NUMNODES; j++) {
6172 cpumask_t tmp, notcovered;
6173 int n = (i + j) % MAX_NUMNODES;
6174
6175 cpus_complement(notcovered, covered);
6176 cpus_and(tmp, notcovered, *cpu_map);
6177 cpus_and(tmp, tmp, domainspan);
6178 if (cpus_empty(tmp))
6179 break;
6180
6181 nodemask = node_to_cpumask(n);
6182 cpus_and(tmp, tmp, nodemask);
6183 if (cpus_empty(tmp))
6184 continue;
6185
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006186 sg = kmalloc_node(sizeof(struct sched_group),
6187 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006188 if (!sg) {
6189 printk(KERN_WARNING
6190 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006191 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006192 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006193 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006194 sg->cpumask = tmp;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006195 sg->next = prev->next;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006196 cpus_or(covered, covered, tmp);
6197 prev->next = sg;
6198 prev = sg;
6199 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201#endif
6202
6203 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006204#ifdef CONFIG_SCHED_SMT
6205 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006206 struct sched_domain *sd = &per_cpu(cpu_domains, i);
6207
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006208 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006209 }
6210#endif
6211#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006212 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006213 struct sched_domain *sd = &per_cpu(core_domains, i);
6214
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006215 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006216 }
6217#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006219 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006220 struct sched_domain *sd = &per_cpu(phys_domains, i);
6221
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006222 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223 }
6224
John Hawkes9c1cfda2005-09-06 15:18:14 -07006225#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08006226 for (i = 0; i < MAX_NUMNODES; i++)
6227 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006228
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006229 if (sd_allnodes) {
6230 struct sched_group *sg;
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07006231
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006232 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg);
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07006233 init_numa_sched_groups_power(sg);
6234 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006235#endif
6236
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006238 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 struct sched_domain *sd;
6240#ifdef CONFIG_SCHED_SMT
6241 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006242#elif defined(CONFIG_SCHED_MC)
6243 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244#else
6245 sd = &per_cpu(phys_domains, i);
6246#endif
6247 cpu_attach_domain(sd, i);
6248 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006249
6250 return 0;
6251
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006252#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006253error:
6254 free_sched_groups(cpu_map);
6255 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006256#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257}
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006258/*
6259 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
6260 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006261static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006262{
6263 cpumask_t cpu_default_map;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006264 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006266 /*
6267 * Setup mask for cpus without special case scheduling requirements.
6268 * For now this just excludes isolated cpus, but could be used to
6269 * exclude other special cases in the future.
6270 */
6271 cpus_andnot(cpu_default_map, *cpu_map, cpu_isolated_map);
6272
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006273 err = build_sched_domains(&cpu_default_map);
6274
6275 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006276}
6277
6278static void arch_destroy_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279{
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006280 free_sched_groups(cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006281}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006283/*
6284 * Detach sched domains from a group of cpus specified in cpu_map
6285 * These cpus will now be attached to the NULL domain
6286 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08006287static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006288{
6289 int i;
6290
6291 for_each_cpu_mask(i, *cpu_map)
6292 cpu_attach_domain(NULL, i);
6293 synchronize_sched();
6294 arch_destroy_sched_domains(cpu_map);
6295}
6296
6297/*
6298 * Partition sched domains as specified by the cpumasks below.
6299 * This attaches all cpus from the cpumasks to the NULL domain,
6300 * waits for a RCU quiescent period, recalculates sched
6301 * domain information and then attaches them back to the
6302 * correct sched domains
6303 * Call with hotplug lock held
6304 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006305int partition_sched_domains(cpumask_t *partition1, cpumask_t *partition2)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006306{
6307 cpumask_t change_map;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006308 int err = 0;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006309
6310 cpus_and(*partition1, *partition1, cpu_online_map);
6311 cpus_and(*partition2, *partition2, cpu_online_map);
6312 cpus_or(change_map, *partition1, *partition2);
6313
6314 /* Detach sched domains from all of the affected cpus */
6315 detach_destroy_domains(&change_map);
6316 if (!cpus_empty(*partition1))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006317 err = build_sched_domains(partition1);
6318 if (!err && !cpus_empty(*partition2))
6319 err = build_sched_domains(partition2);
6320
6321 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006322}
6323
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006324#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
6325int arch_reinit_sched_domains(void)
6326{
6327 int err;
6328
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006329 mutex_lock(&sched_hotcpu_mutex);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006330 detach_destroy_domains(&cpu_online_map);
6331 err = arch_init_sched_domains(&cpu_online_map);
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006332 mutex_unlock(&sched_hotcpu_mutex);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006333
6334 return err;
6335}
6336
6337static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
6338{
6339 int ret;
6340
6341 if (buf[0] != '0' && buf[0] != '1')
6342 return -EINVAL;
6343
6344 if (smt)
6345 sched_smt_power_savings = (buf[0] == '1');
6346 else
6347 sched_mc_power_savings = (buf[0] == '1');
6348
6349 ret = arch_reinit_sched_domains();
6350
6351 return ret ? ret : count;
6352}
6353
6354int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
6355{
6356 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006357
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006358#ifdef CONFIG_SCHED_SMT
6359 if (smt_capable())
6360 err = sysfs_create_file(&cls->kset.kobj,
6361 &attr_sched_smt_power_savings.attr);
6362#endif
6363#ifdef CONFIG_SCHED_MC
6364 if (!err && mc_capable())
6365 err = sysfs_create_file(&cls->kset.kobj,
6366 &attr_sched_mc_power_savings.attr);
6367#endif
6368 return err;
6369}
6370#endif
6371
6372#ifdef CONFIG_SCHED_MC
6373static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
6374{
6375 return sprintf(page, "%u\n", sched_mc_power_savings);
6376}
Ingo Molnar48f24c42006-07-03 00:25:40 -07006377static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
6378 const char *buf, size_t count)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006379{
6380 return sched_power_savings_store(buf, count, 0);
6381}
6382SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
6383 sched_mc_power_savings_store);
6384#endif
6385
6386#ifdef CONFIG_SCHED_SMT
6387static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
6388{
6389 return sprintf(page, "%u\n", sched_smt_power_savings);
6390}
Ingo Molnar48f24c42006-07-03 00:25:40 -07006391static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
6392 const char *buf, size_t count)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006393{
6394 return sched_power_savings_store(buf, count, 1);
6395}
6396SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
6397 sched_smt_power_savings_store);
6398#endif
6399
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400/*
6401 * Force a reinitialization of the sched domains hierarchy. The domains
6402 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07006403 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 * which will prevent rebalancing while the sched domains are recalculated.
6405 */
6406static int update_sched_domains(struct notifier_block *nfb,
6407 unsigned long action, void *hcpu)
6408{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 switch (action) {
6410 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006411 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006413 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006414 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415 return NOTIFY_OK;
6416
6417 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006418 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006420 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006422 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006424 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425 /*
6426 * Fall through and re-initialise the domains.
6427 */
6428 break;
6429 default:
6430 return NOTIFY_DONE;
6431 }
6432
6433 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006434 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435
6436 return NOTIFY_OK;
6437}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438
6439void __init sched_init_smp(void)
6440{
Nick Piggin5c1e1762006-10-03 01:14:04 -07006441 cpumask_t non_isolated_cpus;
6442
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006443 mutex_lock(&sched_hotcpu_mutex);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006444 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08006445 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07006446 if (cpus_empty(non_isolated_cpus))
6447 cpu_set(smp_processor_id(), non_isolated_cpus);
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006448 mutex_unlock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449 /* XXX: Theoretical race here - CPU may be hotplugged now */
6450 hotcpu_notifier(update_sched_domains, 0);
Nick Piggin5c1e1762006-10-03 01:14:04 -07006451
Nick Piggine692ab52007-07-26 13:40:43 +02006452 init_sched_domain_sysctl();
6453
Nick Piggin5c1e1762006-10-03 01:14:04 -07006454 /* Move init over to a non-isolated CPU */
6455 if (set_cpus_allowed(current, non_isolated_cpus) < 0)
6456 BUG();
Ingo Molnardd41f592007-07-09 18:51:59 +02006457 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458}
6459#else
6460void __init sched_init_smp(void)
6461{
Ingo Molnardd41f592007-07-09 18:51:59 +02006462 sched_init_granularity();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463}
6464#endif /* CONFIG_SMP */
6465
6466int in_sched_functions(unsigned long addr)
6467{
6468 /* Linker adds these: start and end of __sched functions */
6469 extern char __sched_text_start[], __sched_text_end[];
Ingo Molnar48f24c42006-07-03 00:25:40 -07006470
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 return in_lock_functions(addr) ||
6472 (addr >= (unsigned long)__sched_text_start
6473 && addr < (unsigned long)__sched_text_end);
6474}
6475
Ingo Molnardd41f592007-07-09 18:51:59 +02006476static inline void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
6477{
6478 cfs_rq->tasks_timeline = RB_ROOT;
6479 cfs_rq->fair_clock = 1;
6480#ifdef CONFIG_FAIR_GROUP_SCHED
6481 cfs_rq->rq = rq;
6482#endif
6483}
6484
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485void __init sched_init(void)
6486{
Ingo Molnardd41f592007-07-09 18:51:59 +02006487 u64 now = sched_clock();
Christoph Lameter476f3532007-05-06 14:48:58 -07006488 int highest_cpu = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02006489 int i, j;
6490
6491 /*
6492 * Link up the scheduling class hierarchy:
6493 */
6494 rt_sched_class.next = &fair_sched_class;
6495 fair_sched_class.next = &idle_sched_class;
6496 idle_sched_class.next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08006498 for_each_possible_cpu(i) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006499 struct rt_prio_array *array;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006500 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501
6502 rq = cpu_rq(i);
6503 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07006504 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07006505 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02006506 rq->clock = 1;
6507 init_cfs_rq(&rq->cfs, rq);
6508#ifdef CONFIG_FAIR_GROUP_SCHED
6509 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
6510 list_add(&rq->cfs.leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
6511#endif
6512 rq->ls.load_update_last = now;
6513 rq->ls.load_update_start = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514
Ingo Molnardd41f592007-07-09 18:51:59 +02006515 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
6516 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07006518 rq->sd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02006520 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07006522 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523 rq->migration_thread = NULL;
6524 INIT_LIST_HEAD(&rq->migration_queue);
6525#endif
6526 atomic_set(&rq->nr_iowait, 0);
6527
Ingo Molnardd41f592007-07-09 18:51:59 +02006528 array = &rq->rt.active;
6529 for (j = 0; j < MAX_RT_PRIO; j++) {
6530 INIT_LIST_HEAD(array->queue + j);
6531 __clear_bit(j, array->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532 }
Christoph Lameter476f3532007-05-06 14:48:58 -07006533 highest_cpu = i;
Ingo Molnardd41f592007-07-09 18:51:59 +02006534 /* delimiter for bitsearch: */
6535 __set_bit(MAX_RT_PRIO, array->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536 }
6537
Peter Williams2dd73a42006-06-27 02:54:34 -07006538 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07006539
Avi Kivitye107be32007-07-26 13:40:43 +02006540#ifdef CONFIG_PREEMPT_NOTIFIERS
6541 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
6542#endif
6543
Christoph Lameterc9819f42006-12-10 02:20:25 -08006544#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006545 nr_cpu_ids = highest_cpu + 1;
Christoph Lameterc9819f42006-12-10 02:20:25 -08006546 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
6547#endif
6548
Heiko Carstensb50f60c2006-07-30 03:03:52 -07006549#ifdef CONFIG_RT_MUTEXES
6550 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
6551#endif
6552
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553 /*
6554 * The boot idle thread does lazy MMU switching as well:
6555 */
6556 atomic_inc(&init_mm.mm_count);
6557 enter_lazy_tlb(&init_mm, current);
6558
6559 /*
6560 * Make us the idle thread. Technically, schedule() should not be
6561 * called from this thread, however somewhere below it might be,
6562 * but because we are the idle thread, we just pick up running again
6563 * when this runqueue becomes "idle".
6564 */
6565 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02006566 /*
6567 * During early bootup we pretend to be a normal task:
6568 */
6569 current->sched_class = &fair_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570}
6571
6572#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
6573void __might_sleep(char *file, int line)
6574{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006575#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576 static unsigned long prev_jiffy; /* ratelimiting */
6577
6578 if ((in_atomic() || irqs_disabled()) &&
6579 system_state == SYSTEM_RUNNING && !oops_in_progress) {
6580 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
6581 return;
6582 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08006583 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584 " context at %s:%d\n", file, line);
6585 printk("in_atomic():%d, irqs_disabled():%d\n",
6586 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08006587 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08006588 if (irqs_disabled())
6589 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590 dump_stack();
6591 }
6592#endif
6593}
6594EXPORT_SYMBOL(__might_sleep);
6595#endif
6596
6597#ifdef CONFIG_MAGIC_SYSRQ
6598void normalize_rt_tasks(void)
6599{
Ingo Molnara0f98a12007-06-17 18:37:45 +02006600 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006602 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02006603 int on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604
6605 read_lock_irq(&tasklist_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02006606 do_each_thread(g, p) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006607 p->se.fair_key = 0;
6608 p->se.wait_runtime = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02006609 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02006610 p->se.wait_start_fair = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02006611 p->se.sleep_start_fair = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02006612#ifdef CONFIG_SCHEDSTATS
6613 p->se.wait_start = 0;
6614 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02006615 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02006616#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02006617 task_rq(p)->cfs.fair_clock = 0;
6618 task_rq(p)->clock = 0;
6619
6620 if (!rt_task(p)) {
6621 /*
6622 * Renice negative nice level userspace
6623 * tasks back to 0:
6624 */
6625 if (TASK_NICE(p) < 0 && p->mm)
6626 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02006628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629
Ingo Molnarb29739f2006-06-27 02:54:51 -07006630 spin_lock_irqsave(&p->pi_lock, flags);
6631 rq = __task_rq_lock(p);
Ingo Molnardd41f592007-07-09 18:51:59 +02006632#ifdef CONFIG_SMP
6633 /*
6634 * Do not touch the migration thread:
6635 */
6636 if (p == rq->migration_thread)
6637 goto out_unlock;
6638#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639
Ingo Molnardd41f592007-07-09 18:51:59 +02006640 on_rq = p->se.on_rq;
6641 if (on_rq)
Ingo Molnar8e717b12007-08-09 11:16:46 +02006642 deactivate_task(task_rq(p), p, 0, rq_clock(task_rq(p)));
Ingo Molnardd41f592007-07-09 18:51:59 +02006643 __setscheduler(rq, p, SCHED_NORMAL, 0);
6644 if (on_rq) {
6645 activate_task(task_rq(p), p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646 resched_task(rq->curr);
6647 }
Ingo Molnardd41f592007-07-09 18:51:59 +02006648#ifdef CONFIG_SMP
6649 out_unlock:
6650#endif
Ingo Molnarb29739f2006-06-27 02:54:51 -07006651 __task_rq_unlock(rq);
6652 spin_unlock_irqrestore(&p->pi_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02006653 } while_each_thread(g, p);
6654
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655 read_unlock_irq(&tasklist_lock);
6656}
6657
6658#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07006659
6660#ifdef CONFIG_IA64
6661/*
6662 * These functions are only useful for the IA64 MCA handling.
6663 *
6664 * They can only be called when the whole system has been
6665 * stopped - every CPU needs to be quiescent, and no scheduling
6666 * activity can take place. Using them for anything else would
6667 * be a serious bug, and as a result, they aren't even visible
6668 * under any other configuration.
6669 */
6670
6671/**
6672 * curr_task - return the current task for a given cpu.
6673 * @cpu: the processor in question.
6674 *
6675 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
6676 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006677struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07006678{
6679 return cpu_curr(cpu);
6680}
6681
6682/**
6683 * set_curr_task - set the current task for a given cpu.
6684 * @cpu: the processor in question.
6685 * @p: the task pointer to set.
6686 *
6687 * Description: This function must only be used when non-maskable interrupts
6688 * are serviced on a separate stack. It allows the architecture to switch the
6689 * notion of the current task on a cpu in a non-blocking manner. This function
6690 * must be called with all CPU's synchronized, and interrupts disabled, the
6691 * and caller must save the original value of the current task (see
6692 * curr_task() above) and restore that value before reenabling interrupts and
6693 * re-starting the system.
6694 *
6695 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
6696 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006697void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07006698{
6699 cpu_curr(cpu) = p;
6700}
6701
6702#endif