blob: 4ad789d268fe4959d7a24789f06a0c711af516a1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * kernel/sched.c
3 *
4 * Kernel scheduler and related syscalls
5 *
6 * Copyright (C) 1991-2002 Linus Torvalds
7 *
8 * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and
9 * make semaphores SMP safe
10 * 1998-11-19 Implemented schedule_timeout() and related stuff
11 * by Andrea Arcangeli
12 * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
13 * hybrid priority-list and round-robin design with
14 * an array-switch method of distributing timeslices
15 * and per-CPU runqueues. Cleanups and useful suggestions
16 * by Davide Libenzi, preemptible kernel bits by Robert Love.
17 * 2003-09-03 Interactivity tuning by Con Kolivas.
18 * 2004-04-02 Scheduler domains code by Nick Piggin
Ingo Molnarc31f2e82007-07-09 18:52:01 +020019 * 2007-04-15 Work begun on replacing all interactivity tuning with a
20 * fair scheduling design by Con Kolivas.
21 * 2007-05-05 Load balancing (smp-nice) and other improvements
22 * by Peter Williams
23 * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith
24 * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
27#include <linux/mm.h>
28#include <linux/module.h>
29#include <linux/nmi.h>
30#include <linux/init.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020031#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/highmem.h>
33#include <linux/smp_lock.h>
34#include <asm/mmu_context.h>
35#include <linux/interrupt.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080036#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/completion.h>
38#include <linux/kernel_stat.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070039#include <linux/debug_locks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/security.h>
41#include <linux/notifier.h>
42#include <linux/profile.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080043#include <linux/freezer.h>
akpm@osdl.org198e2f12006-01-12 01:05:30 -080044#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/blkdev.h>
46#include <linux/delay.h>
47#include <linux/smp.h>
48#include <linux/threads.h>
49#include <linux/timer.h>
50#include <linux/rcupdate.h>
51#include <linux/cpu.h>
52#include <linux/cpuset.h>
53#include <linux/percpu.h>
54#include <linux/kthread.h>
55#include <linux/seq_file.h>
Nick Piggine692ab52007-07-26 13:40:43 +020056#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/syscalls.h>
58#include <linux/times.h>
Jay Lan8f0ab512006-09-30 23:28:59 -070059#include <linux/tsacct_kern.h>
bibo maoc6fd91f2006-03-26 01:38:20 -080060#include <linux/kprobes.h>
Shailabh Nagar0ff92242006-07-14 00:24:37 -070061#include <linux/delayacct.h>
Eric Dumazet5517d862007-05-08 00:32:57 -070062#include <linux/reciprocal_div.h>
Ingo Molnardff06c12007-07-09 18:52:00 +020063#include <linux/unistd.h>
Jens Axboef5ff8422007-09-21 09:19:54 +020064#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Eric Dumazet5517d862007-05-08 00:32:57 -070066#include <asm/tlb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/*
Alexey Dobriyanb035b6d2007-02-10 01:45:10 -080069 * Scheduler clock - returns current time in nanosec units.
70 * This is default implementation.
71 * Architectures and sub-architectures can override this.
72 */
73unsigned long long __attribute__((weak)) sched_clock(void)
74{
75 return (unsigned long long)jiffies * (1000000000 / HZ);
76}
77
78/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * Convert user-nice values [ -20 ... 0 ... 19 ]
80 * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
81 * and back.
82 */
83#define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
84#define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
85#define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
86
87/*
88 * 'User priority' is the nice value converted to something we
89 * can work with better when scaling various scheduler parameters,
90 * it's a [ 0 ... 39 ] range.
91 */
92#define USER_PRIO(p) ((p)-MAX_RT_PRIO)
93#define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
94#define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
95
96/*
97 * Some helpers for converting nanosecond timing to jiffy resolution
98 */
99#define NS_TO_JIFFIES(TIME) ((TIME) / (1000000000 / HZ))
100#define JIFFIES_TO_NS(TIME) ((TIME) * (1000000000 / HZ))
101
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200102#define NICE_0_LOAD SCHED_LOAD_SCALE
103#define NICE_0_SHIFT SCHED_LOAD_SHIFT
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105/*
106 * These are the 'tuning knobs' of the scheduler:
107 *
108 * Minimum timeslice is 5 msecs (or 1 jiffy, whichever is larger),
109 * default timeslice is 100 msecs, maximum timeslice is 800 msecs.
110 * Timeslices get refilled after they expire.
111 */
112#define MIN_TIMESLICE max(5 * HZ / 1000, 1)
113#define DEF_TIMESLICE (100 * HZ / 1000)
Peter Williams2dd73a42006-06-27 02:54:34 -0700114
Eric Dumazet5517d862007-05-08 00:32:57 -0700115#ifdef CONFIG_SMP
116/*
117 * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
118 * Since cpu_power is a 'constant', we can use a reciprocal divide.
119 */
120static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
121{
122 return reciprocal_divide(load, sg->reciprocal_cpu_power);
123}
124
125/*
126 * Each time a sched group cpu_power is changed,
127 * we must compute its reciprocal value
128 */
129static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
130{
131 sg->__cpu_power += val;
132 sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
133}
134#endif
135
Ingo Molnar634fa8c2007-07-09 18:52:00 +0200136#define SCALE_PRIO(x, prio) \
137 max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO / 2), MIN_TIMESLICE)
Borislav Petkov91fcdd42006-10-19 23:28:29 -0700138
Ingo Molnar634fa8c2007-07-09 18:52:00 +0200139/*
140 * static_prio_timeslice() scales user-nice values [ -20 ... 0 ... 19 ]
141 * to time slice values: [800ms ... 100ms ... 5ms]
142 */
143static unsigned int static_prio_timeslice(int static_prio)
Peter Williams2dd73a42006-06-27 02:54:34 -0700144{
Ingo Molnar634fa8c2007-07-09 18:52:00 +0200145 if (static_prio == NICE_TO_PRIO(19))
146 return 1;
147
148 if (static_prio < NICE_TO_PRIO(0))
149 return SCALE_PRIO(DEF_TIMESLICE * 4, static_prio);
150 else
151 return SCALE_PRIO(DEF_TIMESLICE, static_prio);
Peter Williams2dd73a42006-06-27 02:54:34 -0700152}
153
Ingo Molnare05606d2007-07-09 18:51:59 +0200154static inline int rt_policy(int policy)
155{
156 if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR))
157 return 1;
158 return 0;
159}
160
161static inline int task_has_rt_policy(struct task_struct *p)
162{
163 return rt_policy(p->policy);
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/*
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200167 * This is the priority-queue data structure of the RT scheduling class:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200169struct rt_prio_array {
170 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
171 struct list_head queue[MAX_RT_PRIO];
172};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200174/* CFS-related fields in a runqueue */
175struct cfs_rq {
176 struct load_weight load;
177 unsigned long nr_running;
178
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200179 u64 exec_clock;
Ingo Molnare9acbff2007-10-15 17:00:04 +0200180 u64 min_vruntime;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200181
182 struct rb_root tasks_timeline;
183 struct rb_node *rb_leftmost;
184 struct rb_node *rb_load_balance_curr;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200185 /* 'curr' points to currently running entity on this cfs_rq.
186 * It is set to NULL otherwise (i.e when none are currently running).
187 */
188 struct sched_entity *curr;
Ingo Molnar62160e32007-10-15 17:00:03 +0200189#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200190 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
191
192 /* leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
193 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
194 * (like users, containers etc.)
195 *
196 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
197 * list is used during load balance.
198 */
199 struct list_head leaf_cfs_rq_list; /* Better name : task_cfs_rq_list? */
200#endif
201};
202
203/* Real-Time classes' related field in a runqueue: */
204struct rt_rq {
205 struct rt_prio_array active;
206 int rt_load_balance_idx;
207 struct list_head *rt_load_balance_head, *rt_load_balance_curr;
208};
209
210/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * This is the main, per-CPU runqueue data structure.
212 *
213 * Locking rule: those places that want to lock multiple runqueues
214 * (such as the load balancing or the thread migration code), lock
215 * acquire operations must be ordered by ascending &runqueue.
216 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700217struct rq {
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200218 spinlock_t lock; /* runqueue lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 /*
221 * nr_running and cpu_load should be in the same cacheline because
222 * remote CPUs use both these fields when doing load calculation.
223 */
224 unsigned long nr_running;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200225 #define CPU_LOAD_IDX_MAX 5
226 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Siddha, Suresh Bbdecea32007-05-08 00:32:48 -0700227 unsigned char idle_at_tick;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700228#ifdef CONFIG_NO_HZ
229 unsigned char in_nohz_recently;
230#endif
Dmitry Adamushko495eca42007-10-15 17:00:06 +0200231 struct load_weight load; /* capture load from *all* tasks on this cpu */
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200232 unsigned long nr_load_updates;
233 u64 nr_switches;
234
235 struct cfs_rq cfs;
236#ifdef CONFIG_FAIR_GROUP_SCHED
237 struct list_head leaf_cfs_rq_list; /* list of leaf cfs_rq on this cpu */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238#endif
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200239 struct rt_rq rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 /*
242 * This is part of a global counter where only the total sum
243 * over all CPUs matters. A task can increase this counter on
244 * one CPU and if it got migrated afterwards it may decrease
245 * it on another CPU. Always updated under the runqueue lock:
246 */
247 unsigned long nr_uninterruptible;
248
Ingo Molnar36c8b582006-07-03 00:25:41 -0700249 struct task_struct *curr, *idle;
Christoph Lameterc9819f42006-12-10 02:20:25 -0800250 unsigned long next_balance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 struct mm_struct *prev_mm;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200252
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200253 u64 clock, prev_clock_raw;
254 s64 clock_max_delta;
255
256 unsigned int clock_warps, clock_overflows;
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200257 u64 idle_clock;
258 unsigned int clock_deep_idle_events;
Ingo Molnar529c7722007-08-10 23:05:11 +0200259 u64 tick_timestamp;
Ingo Molnar6aa645e2007-07-09 18:51:58 +0200260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 atomic_t nr_iowait;
262
263#ifdef CONFIG_SMP
264 struct sched_domain *sd;
265
266 /* For active balancing */
267 int active_balance;
268 int push_cpu;
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700269 int cpu; /* cpu of this runqueue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Ingo Molnar36c8b582006-07-03 00:25:41 -0700271 struct task_struct *migration_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 struct list_head migration_queue;
273#endif
274
275#ifdef CONFIG_SCHEDSTATS
276 /* latency stats */
277 struct sched_info rq_sched_info;
278
279 /* sys_sched_yield() stats */
280 unsigned long yld_exp_empty;
281 unsigned long yld_act_empty;
282 unsigned long yld_both_empty;
283 unsigned long yld_cnt;
284
285 /* schedule() stats */
286 unsigned long sched_switch;
287 unsigned long sched_cnt;
288 unsigned long sched_goidle;
289
290 /* try_to_wake_up() stats */
291 unsigned long ttwu_cnt;
292 unsigned long ttwu_local;
293#endif
Ingo Molnarfcb99372006-07-03 00:25:10 -0700294 struct lock_class_key rq_lock_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295};
296
Fenghua Yuf34e3b62007-07-19 01:48:13 -0700297static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Gautham R Shenoy5be93612007-05-09 02:34:04 -0700298static DEFINE_MUTEX(sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Ingo Molnardd41f592007-07-09 18:51:59 +0200300static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
301{
302 rq->curr->sched_class->check_preempt_curr(rq, p);
303}
304
Christoph Lameter0a2966b2006-09-25 23:30:51 -0700305static inline int cpu_of(struct rq *rq)
306{
307#ifdef CONFIG_SMP
308 return rq->cpu;
309#else
310 return 0;
311#endif
312}
313
Nick Piggin674311d2005-06-25 14:57:27 -0700314/*
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200315 * Update the per-runqueue clock, as finegrained as the platform can give
316 * us, but without assuming monotonicity, etc.:
Ingo Molnar20d315d2007-07-09 18:51:58 +0200317 */
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200318static void __update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200319{
320 u64 prev_raw = rq->prev_clock_raw;
321 u64 now = sched_clock();
322 s64 delta = now - prev_raw;
323 u64 clock = rq->clock;
324
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200325#ifdef CONFIG_SCHED_DEBUG
326 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
327#endif
Ingo Molnar20d315d2007-07-09 18:51:58 +0200328 /*
329 * Protect against sched_clock() occasionally going backwards:
330 */
331 if (unlikely(delta < 0)) {
332 clock++;
333 rq->clock_warps++;
334 } else {
335 /*
336 * Catch too large forward jumps too:
337 */
Ingo Molnar529c7722007-08-10 23:05:11 +0200338 if (unlikely(clock + delta > rq->tick_timestamp + TICK_NSEC)) {
339 if (clock < rq->tick_timestamp + TICK_NSEC)
340 clock = rq->tick_timestamp + TICK_NSEC;
341 else
342 clock++;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200343 rq->clock_overflows++;
344 } else {
345 if (unlikely(delta > rq->clock_max_delta))
346 rq->clock_max_delta = delta;
347 clock += delta;
348 }
349 }
350
351 rq->prev_clock_raw = now;
352 rq->clock = clock;
Ingo Molnar20d315d2007-07-09 18:51:58 +0200353}
354
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200355static void update_rq_clock(struct rq *rq)
Ingo Molnar20d315d2007-07-09 18:51:58 +0200356{
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200357 if (likely(smp_processor_id() == cpu_of(rq)))
358 __update_rq_clock(rq);
359}
Ingo Molnar20d315d2007-07-09 18:51:58 +0200360
Ingo Molnar20d315d2007-07-09 18:51:58 +0200361/*
Nick Piggin674311d2005-06-25 14:57:27 -0700362 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -0700363 * See detach_destroy_domains: synchronize_sched for details.
Nick Piggin674311d2005-06-25 14:57:27 -0700364 *
365 * The domain tree of any CPU may only be accessed from within
366 * preempt-disabled sections.
367 */
Ingo Molnar48f24c42006-07-03 00:25:40 -0700368#define for_each_domain(cpu, __sd) \
369 for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
372#define this_rq() (&__get_cpu_var(runqueues))
373#define task_rq(p) cpu_rq(task_cpu(p))
374#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
375
Ingo Molnare436d802007-07-19 21:28:35 +0200376/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200377 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
378 */
379#ifdef CONFIG_SCHED_DEBUG
380# define const_debug __read_mostly
381#else
382# define const_debug static const
383#endif
384
385/*
386 * Debugging: various feature bits
387 */
388enum {
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200389 SCHED_FEAT_NEW_FAIR_SLEEPERS = 1,
390 SCHED_FEAT_START_DEBIT = 2,
391 SCHED_FEAT_USE_TREE_AVG = 4,
392 SCHED_FEAT_APPROX_AVG = 8,
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200393};
394
395const_debug unsigned int sysctl_sched_features =
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200396 SCHED_FEAT_NEW_FAIR_SLEEPERS *1 |
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +0200397 SCHED_FEAT_START_DEBIT *1 |
398 SCHED_FEAT_USE_TREE_AVG *0 |
399 SCHED_FEAT_APPROX_AVG *0;
Ingo Molnarbf5c91b2007-10-15 17:00:04 +0200400
401#define sched_feat(x) (sysctl_sched_features & SCHED_FEAT_##x)
402
403/*
Ingo Molnare436d802007-07-19 21:28:35 +0200404 * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
405 * clock constructed from sched_clock():
406 */
407unsigned long long cpu_clock(int cpu)
408{
Ingo Molnare436d802007-07-19 21:28:35 +0200409 unsigned long long now;
410 unsigned long flags;
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200411 struct rq *rq;
Ingo Molnare436d802007-07-19 21:28:35 +0200412
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200413 local_irq_save(flags);
Ingo Molnarb04a0f42007-08-09 11:16:46 +0200414 rq = cpu_rq(cpu);
415 update_rq_clock(rq);
416 now = rq->clock;
Ingo Molnar2cd4d0e2007-07-26 13:40:43 +0200417 local_irq_restore(flags);
Ingo Molnare436d802007-07-19 21:28:35 +0200418
419 return now;
420}
421
Ingo Molnar138a8ae2007-07-09 18:51:58 +0200422#ifdef CONFIG_FAIR_GROUP_SCHED
423/* Change a task's ->cfs_rq if it moves across CPUs */
424static inline void set_task_cfs_rq(struct task_struct *p)
425{
426 p->se.cfs_rq = &task_rq(p)->cfs;
427}
428#else
429static inline void set_task_cfs_rq(struct task_struct *p)
430{
431}
432#endif
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434#ifndef prepare_arch_switch
Nick Piggin4866cde2005-06-25 14:57:23 -0700435# define prepare_arch_switch(next) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436#endif
Nick Piggin4866cde2005-06-25 14:57:23 -0700437#ifndef finish_arch_switch
438# define finish_arch_switch(prev) do { } while (0)
439#endif
440
441#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar70b97a72006-07-03 00:25:42 -0700442static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700443{
444 return rq->curr == p;
445}
446
Ingo Molnar70b97a72006-07-03 00:25:42 -0700447static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700448{
449}
450
Ingo Molnar70b97a72006-07-03 00:25:42 -0700451static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700452{
Ingo Molnarda04c032005-09-13 11:17:59 +0200453#ifdef CONFIG_DEBUG_SPINLOCK
454 /* this is a valid case when another task releases the spinlock */
455 rq->lock.owner = current;
456#endif
Ingo Molnar8a25d5d2006-07-03 00:24:54 -0700457 /*
458 * If we are tracking spinlock dependencies then we have to
459 * fix up the runqueue lock - which gets 'carried over' from
460 * prev into current:
461 */
462 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
463
Nick Piggin4866cde2005-06-25 14:57:23 -0700464 spin_unlock_irq(&rq->lock);
465}
466
467#else /* __ARCH_WANT_UNLOCKED_CTXSW */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700468static inline int task_running(struct rq *rq, struct task_struct *p)
Nick Piggin4866cde2005-06-25 14:57:23 -0700469{
470#ifdef CONFIG_SMP
471 return p->oncpu;
472#else
473 return rq->curr == p;
474#endif
475}
476
Ingo Molnar70b97a72006-07-03 00:25:42 -0700477static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -0700478{
479#ifdef CONFIG_SMP
480 /*
481 * We can optimise this out completely for !SMP, because the
482 * SMP rebalancing from interrupt is the only thing that cares
483 * here.
484 */
485 next->oncpu = 1;
486#endif
487#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
488 spin_unlock_irq(&rq->lock);
489#else
490 spin_unlock(&rq->lock);
491#endif
492}
493
Ingo Molnar70b97a72006-07-03 00:25:42 -0700494static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
Nick Piggin4866cde2005-06-25 14:57:23 -0700495{
496#ifdef CONFIG_SMP
497 /*
498 * After ->oncpu is cleared, the task can be moved to a different CPU.
499 * We must ensure this doesn't happen until the switch is completely
500 * finished.
501 */
502 smp_wmb();
503 prev->oncpu = 0;
504#endif
505#ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
506 local_irq_enable();
507#endif
508}
509#endif /* __ARCH_WANT_UNLOCKED_CTXSW */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700512 * __task_rq_lock - lock the runqueue a given task resides on.
513 * Must be called interrupts disabled.
514 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700515static inline struct rq *__task_rq_lock(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700516 __acquires(rq->lock)
517{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700518 struct rq *rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -0700519
520repeat_lock_task:
521 rq = task_rq(p);
522 spin_lock(&rq->lock);
523 if (unlikely(rq != task_rq(p))) {
524 spin_unlock(&rq->lock);
525 goto repeat_lock_task;
526 }
527 return rq;
528}
529
530/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 * task_rq_lock - lock the runqueue a given task resides on and disable
532 * interrupts. Note the ordering: we can safely lookup the task_rq without
533 * explicitly disabling preemption.
534 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700535static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 __acquires(rq->lock)
537{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700538 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540repeat_lock_task:
541 local_irq_save(*flags);
542 rq = task_rq(p);
543 spin_lock(&rq->lock);
544 if (unlikely(rq != task_rq(p))) {
545 spin_unlock_irqrestore(&rq->lock, *flags);
546 goto repeat_lock_task;
547 }
548 return rq;
549}
550
Ingo Molnar70b97a72006-07-03 00:25:42 -0700551static inline void __task_rq_unlock(struct rq *rq)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700552 __releases(rq->lock)
553{
554 spin_unlock(&rq->lock);
555}
556
Ingo Molnar70b97a72006-07-03 00:25:42 -0700557static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 __releases(rq->lock)
559{
560 spin_unlock_irqrestore(&rq->lock, *flags);
561}
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563/*
Robert P. J. Daycc2a73b2006-12-10 02:20:00 -0800564 * this_rq_lock - lock this runqueue and disable interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 */
Ingo Molnar70b97a72006-07-03 00:25:42 -0700566static inline struct rq *this_rq_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 __acquires(rq->lock)
568{
Ingo Molnar70b97a72006-07-03 00:25:42 -0700569 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 local_irq_disable();
572 rq = this_rq();
573 spin_lock(&rq->lock);
574
575 return rq;
576}
577
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200578/*
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200579 * We are going deep-idle (irqs are disabled):
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200580 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200581void sched_clock_idle_sleep_event(void)
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200582{
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200583 struct rq *rq = cpu_rq(smp_processor_id());
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200584
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200585 spin_lock(&rq->lock);
586 __update_rq_clock(rq);
587 spin_unlock(&rq->lock);
588 rq->clock_deep_idle_events++;
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200589}
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200590EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
591
592/*
593 * We just idled delta nanoseconds (called with irqs disabled):
594 */
595void sched_clock_idle_wakeup_event(u64 delta_ns)
596{
597 struct rq *rq = cpu_rq(smp_processor_id());
598 u64 now = sched_clock();
599
600 rq->idle_clock += delta_ns;
601 /*
602 * Override the previous timestamp and ignore all
603 * sched_clock() deltas that occured while we idled,
604 * and use the PM-provided delta_ns to advance the
605 * rq clock:
606 */
607 spin_lock(&rq->lock);
608 rq->prev_clock_raw = now;
609 rq->clock += delta_ns;
610 spin_unlock(&rq->lock);
611}
612EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
Ingo Molnar1b9f19c2007-07-09 18:51:59 +0200613
614/*
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200615 * resched_task - mark a task 'to be rescheduled now'.
616 *
617 * On UP this means the setting of the need_resched flag, on SMP it
618 * might also involve a cross-CPU call to trigger the scheduler on
619 * the target CPU.
620 */
621#ifdef CONFIG_SMP
622
623#ifndef tsk_is_polling
624#define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
625#endif
626
627static void resched_task(struct task_struct *p)
628{
629 int cpu;
630
631 assert_spin_locked(&task_rq(p)->lock);
632
633 if (unlikely(test_tsk_thread_flag(p, TIF_NEED_RESCHED)))
634 return;
635
636 set_tsk_thread_flag(p, TIF_NEED_RESCHED);
637
638 cpu = task_cpu(p);
639 if (cpu == smp_processor_id())
640 return;
641
642 /* NEED_RESCHED must be visible before we test polling */
643 smp_mb();
644 if (!tsk_is_polling(p))
645 smp_send_reschedule(cpu);
646}
647
648static void resched_cpu(int cpu)
649{
650 struct rq *rq = cpu_rq(cpu);
651 unsigned long flags;
652
653 if (!spin_trylock_irqsave(&rq->lock, flags))
654 return;
655 resched_task(cpu_curr(cpu));
656 spin_unlock_irqrestore(&rq->lock, flags);
657}
658#else
659static inline void resched_task(struct task_struct *p)
660{
661 assert_spin_locked(&task_rq(p)->lock);
662 set_tsk_need_resched(p);
663}
664#endif
665
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200666#if BITS_PER_LONG == 32
667# define WMULT_CONST (~0UL)
668#else
669# define WMULT_CONST (1UL << 32)
670#endif
671
672#define WMULT_SHIFT 32
673
Ingo Molnar194081e2007-08-09 11:16:51 +0200674/*
675 * Shift right and round:
676 */
Ingo Molnarcf2ab462007-09-05 14:32:49 +0200677#define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
Ingo Molnar194081e2007-08-09 11:16:51 +0200678
Ingo Molnarcb1c4fc2007-08-02 17:41:40 +0200679static unsigned long
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200680calc_delta_mine(unsigned long delta_exec, unsigned long weight,
681 struct load_weight *lw)
682{
683 u64 tmp;
684
685 if (unlikely(!lw->inv_weight))
Ingo Molnar194081e2007-08-09 11:16:51 +0200686 lw->inv_weight = (WMULT_CONST - lw->weight/2) / lw->weight + 1;
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200687
688 tmp = (u64)delta_exec * weight;
689 /*
690 * Check whether we'd overflow the 64-bit multiplication:
691 */
Ingo Molnar194081e2007-08-09 11:16:51 +0200692 if (unlikely(tmp > WMULT_CONST))
Ingo Molnarcf2ab462007-09-05 14:32:49 +0200693 tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
Ingo Molnar194081e2007-08-09 11:16:51 +0200694 WMULT_SHIFT/2);
695 else
Ingo Molnarcf2ab462007-09-05 14:32:49 +0200696 tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200697
Ingo Molnarecf691d2007-08-02 17:41:40 +0200698 return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200699}
700
701static inline unsigned long
702calc_delta_fair(unsigned long delta_exec, struct load_weight *lw)
703{
704 return calc_delta_mine(delta_exec, NICE_0_LOAD, lw);
705}
706
Ingo Molnar10919852007-10-15 17:00:04 +0200707static inline void update_load_add(struct load_weight *lw, unsigned long inc)
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200708{
709 lw->weight += inc;
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200710}
711
Ingo Molnar10919852007-10-15 17:00:04 +0200712static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200713{
714 lw->weight -= dec;
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200715}
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717/*
Peter Williams2dd73a42006-06-27 02:54:34 -0700718 * To aid in avoiding the subversion of "niceness" due to uneven distribution
719 * of tasks with abnormal "nice" values across CPUs the contribution that
720 * each task makes to its run queue's load is weighted according to its
721 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
722 * scaled version of the new time slice allocation that they receive on time
723 * slice expiry etc.
724 */
725
Ingo Molnardd41f592007-07-09 18:51:59 +0200726#define WEIGHT_IDLEPRIO 2
727#define WMULT_IDLEPRIO (1 << 31)
728
729/*
730 * Nice levels are multiplicative, with a gentle 10% change for every
731 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
732 * nice 1, it will get ~10% less CPU time than another CPU-bound task
733 * that remained on nice 0.
734 *
735 * The "10% effect" is relative and cumulative: from _any_ nice level,
736 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
Ingo Molnarf9153ee2007-07-16 09:46:30 +0200737 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
738 * If a task goes up by ~10% and another task goes down by ~10% then
739 * the relative distance between them is ~25%.)
Ingo Molnardd41f592007-07-09 18:51:59 +0200740 */
741static const int prio_to_weight[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +0200742 /* -20 */ 88761, 71755, 56483, 46273, 36291,
743 /* -15 */ 29154, 23254, 18705, 14949, 11916,
744 /* -10 */ 9548, 7620, 6100, 4904, 3906,
745 /* -5 */ 3121, 2501, 1991, 1586, 1277,
746 /* 0 */ 1024, 820, 655, 526, 423,
747 /* 5 */ 335, 272, 215, 172, 137,
748 /* 10 */ 110, 87, 70, 56, 45,
749 /* 15 */ 36, 29, 23, 18, 15,
Ingo Molnardd41f592007-07-09 18:51:59 +0200750};
751
Ingo Molnar5714d2d2007-07-16 09:46:31 +0200752/*
753 * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
754 *
755 * In cases where the weight does not change often, we can use the
756 * precalculated inverse to speed up arithmetics by turning divisions
757 * into multiplications:
758 */
Ingo Molnardd41f592007-07-09 18:51:59 +0200759static const u32 prio_to_wmult[40] = {
Ingo Molnar254753d2007-08-09 11:16:51 +0200760 /* -20 */ 48388, 59856, 76040, 92818, 118348,
761 /* -15 */ 147320, 184698, 229616, 287308, 360437,
762 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
763 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
764 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
765 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
766 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
767 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
Ingo Molnardd41f592007-07-09 18:51:59 +0200768};
Peter Williams2dd73a42006-06-27 02:54:34 -0700769
Ingo Molnardd41f592007-07-09 18:51:59 +0200770static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
771
772/*
773 * runqueue iterator, to support SMP load-balancing between different
774 * scheduling classes, without having to expose their internal data
775 * structures to the load-balancing proper:
776 */
777struct rq_iterator {
778 void *arg;
779 struct task_struct *(*start)(void *);
780 struct task_struct *(*next)(void *);
781};
782
783static int balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
784 unsigned long max_nr_move, unsigned long max_load_move,
785 struct sched_domain *sd, enum cpu_idle_type idle,
786 int *all_pinned, unsigned long *load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +0200787 int *this_best_prio, struct rq_iterator *iterator);
Ingo Molnardd41f592007-07-09 18:51:59 +0200788
789#include "sched_stats.h"
790#include "sched_rt.c"
791#include "sched_fair.c"
792#include "sched_idletask.c"
793#ifdef CONFIG_SCHED_DEBUG
794# include "sched_debug.c"
795#endif
796
797#define sched_class_highest (&rt_sched_class)
798
Ingo Molnar9c217242007-08-02 17:41:40 +0200799/*
800 * Update delta_exec, delta_fair fields for rq.
801 *
802 * delta_fair clock advances at a rate inversely proportional to
Dmitry Adamushko495eca42007-10-15 17:00:06 +0200803 * total load (rq->load.weight) on the runqueue, while
Ingo Molnar9c217242007-08-02 17:41:40 +0200804 * delta_exec advances at the same rate as wall-clock (provided
805 * cpu is not idle).
806 *
807 * delta_exec / delta_fair is a measure of the (smoothened) load on this
808 * runqueue over any given interval. This (smoothened) load is used
809 * during load balance.
810 *
Dmitry Adamushko495eca42007-10-15 17:00:06 +0200811 * This function is called /before/ updating rq->load
Ingo Molnar9c217242007-08-02 17:41:40 +0200812 * and when switching tasks.
813 */
Ingo Molnar29b4b622007-08-09 11:16:49 +0200814static inline void inc_load(struct rq *rq, const struct task_struct *p)
Ingo Molnar9c217242007-08-02 17:41:40 +0200815{
Dmitry Adamushko495eca42007-10-15 17:00:06 +0200816 update_load_add(&rq->load, p->se.load.weight);
Ingo Molnar9c217242007-08-02 17:41:40 +0200817}
818
Ingo Molnar79b5ddd2007-08-09 11:16:49 +0200819static inline void dec_load(struct rq *rq, const struct task_struct *p)
Ingo Molnar9c217242007-08-02 17:41:40 +0200820{
Dmitry Adamushko495eca42007-10-15 17:00:06 +0200821 update_load_sub(&rq->load, p->se.load.weight);
Ingo Molnar9c217242007-08-02 17:41:40 +0200822}
823
Ingo Molnare5fa2232007-08-09 11:16:49 +0200824static void inc_nr_running(struct task_struct *p, struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +0200825{
826 rq->nr_running++;
Ingo Molnar29b4b622007-08-09 11:16:49 +0200827 inc_load(rq, p);
Ingo Molnar9c217242007-08-02 17:41:40 +0200828}
829
Ingo Molnardb531812007-08-09 11:16:49 +0200830static void dec_nr_running(struct task_struct *p, struct rq *rq)
Ingo Molnar9c217242007-08-02 17:41:40 +0200831{
832 rq->nr_running--;
Ingo Molnar79b5ddd2007-08-09 11:16:49 +0200833 dec_load(rq, p);
Ingo Molnar9c217242007-08-02 17:41:40 +0200834}
835
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200836static void set_load_weight(struct task_struct *p)
837{
838 if (task_has_rt_policy(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +0200839 p->se.load.weight = prio_to_weight[0] * 2;
840 p->se.load.inv_weight = prio_to_wmult[0] >> 1;
841 return;
842 }
843
844 /*
845 * SCHED_IDLE tasks get minimal weight:
846 */
847 if (p->policy == SCHED_IDLE) {
848 p->se.load.weight = WEIGHT_IDLEPRIO;
849 p->se.load.inv_weight = WMULT_IDLEPRIO;
850 return;
851 }
852
853 p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
854 p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200855}
856
Ingo Molnar8159f872007-08-09 11:16:49 +0200857static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnar71f8bd42007-07-09 18:51:59 +0200858{
859 sched_info_queued(p);
Ingo Molnarfd390f62007-08-09 11:16:48 +0200860 p->sched_class->enqueue_task(rq, p, wakeup);
Ingo Molnardd41f592007-07-09 18:51:59 +0200861 p->se.on_rq = 1;
862}
863
Ingo Molnar69be72c2007-08-09 11:16:49 +0200864static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnardd41f592007-07-09 18:51:59 +0200865{
Ingo Molnarf02231e2007-08-09 11:16:48 +0200866 p->sched_class->dequeue_task(rq, p, sleep);
Ingo Molnardd41f592007-07-09 18:51:59 +0200867 p->se.on_rq = 0;
Ingo Molnar71f8bd42007-07-09 18:51:59 +0200868}
869
870/*
Ingo Molnardd41f592007-07-09 18:51:59 +0200871 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +0200872 */
Ingo Molnar14531182007-07-09 18:51:59 +0200873static inline int __normal_prio(struct task_struct *p)
874{
Ingo Molnardd41f592007-07-09 18:51:59 +0200875 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +0200876}
877
878/*
Ingo Molnarb29739f2006-06-27 02:54:51 -0700879 * Calculate the expected normal priority: i.e. priority
880 * without taking RT-inheritance into account. Might be
881 * boosted by interactivity modifiers. Changes upon fork,
882 * setprio syscalls, and whenever the interactivity
883 * estimator recalculates.
884 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700885static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700886{
887 int prio;
888
Ingo Molnare05606d2007-07-09 18:51:59 +0200889 if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -0700890 prio = MAX_RT_PRIO-1 - p->rt_priority;
891 else
892 prio = __normal_prio(p);
893 return prio;
894}
895
896/*
897 * Calculate the current priority, i.e. the priority
898 * taken into account by the scheduler. This value might
899 * be boosted by RT tasks, or might be boosted by
900 * interactivity modifiers. Will be RT if the task got
901 * RT-boosted. If not then it returns p->normal_prio.
902 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700903static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -0700904{
905 p->normal_prio = normal_prio(p);
906 /*
907 * If we are RT tasks or we were boosted to RT priority,
908 * keep the priority unchanged. Otherwise, update priority
909 * to the normal priority:
910 */
911 if (!rt_prio(p->prio))
912 return p->normal_prio;
913 return p->prio;
914}
915
916/*
Ingo Molnardd41f592007-07-09 18:51:59 +0200917 * activate_task - move a task to the runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 */
Ingo Molnardd41f592007-07-09 18:51:59 +0200919static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Ingo Molnardd41f592007-07-09 18:51:59 +0200921 if (p->state == TASK_UNINTERRUPTIBLE)
922 rq->nr_uninterruptible--;
923
Ingo Molnar8159f872007-08-09 11:16:49 +0200924 enqueue_task(rq, p, wakeup);
Ingo Molnare5fa2232007-08-09 11:16:49 +0200925 inc_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
928/*
Ingo Molnardd41f592007-07-09 18:51:59 +0200929 * activate_idle_task - move idle task to the _front_ of runqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 */
Ingo Molnardd41f592007-07-09 18:51:59 +0200931static inline void activate_idle_task(struct task_struct *p, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Ingo Molnara8e504d2007-08-09 11:16:47 +0200933 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Ingo Molnardd41f592007-07-09 18:51:59 +0200935 if (p->state == TASK_UNINTERRUPTIBLE)
936 rq->nr_uninterruptible--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Ingo Molnar8159f872007-08-09 11:16:49 +0200938 enqueue_task(rq, p, 0);
Ingo Molnare5fa2232007-08-09 11:16:49 +0200939 inc_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
942/*
943 * deactivate_task - remove a task from the runqueue.
944 */
Ingo Molnar2e1cb742007-08-09 11:16:49 +0200945static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Ingo Molnardd41f592007-07-09 18:51:59 +0200947 if (p->state == TASK_UNINTERRUPTIBLE)
948 rq->nr_uninterruptible++;
949
Ingo Molnar69be72c2007-08-09 11:16:49 +0200950 dequeue_task(rq, p, sleep);
Ingo Molnardb531812007-08-09 11:16:49 +0200951 dec_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952}
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954/**
955 * task_curr - is this task currently executing on a CPU?
956 * @p: the task in question.
957 */
Ingo Molnar36c8b582006-07-03 00:25:41 -0700958inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 return cpu_curr(task_cpu(p)) == p;
961}
962
Peter Williams2dd73a42006-06-27 02:54:34 -0700963/* Used instead of source_load when we know the type == 0 */
964unsigned long weighted_cpuload(const int cpu)
965{
Dmitry Adamushko495eca42007-10-15 17:00:06 +0200966 return cpu_rq(cpu)->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +0200967}
968
969static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
970{
971#ifdef CONFIG_SMP
972 task_thread_info(p)->cpu = cpu;
973 set_task_cfs_rq(p);
974#endif
Peter Williams2dd73a42006-06-27 02:54:34 -0700975}
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977#ifdef CONFIG_SMP
Ingo Molnarc65cc872007-07-09 18:51:58 +0200978
Ingo Molnardd41f592007-07-09 18:51:59 +0200979void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +0200980{
Ingo Molnardd41f592007-07-09 18:51:59 +0200981 int old_cpu = task_cpu(p);
982 struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200983 u64 clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +0200984
985 clock_offset = old_rq->clock - new_rq->clock;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +0200986
987#ifdef CONFIG_SCHEDSTATS
988 if (p->se.wait_start)
989 p->se.wait_start -= clock_offset;
Ingo Molnardd41f592007-07-09 18:51:59 +0200990 if (p->se.sleep_start)
991 p->se.sleep_start -= clock_offset;
992 if (p->se.block_start)
993 p->se.block_start -= clock_offset;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +0200994#endif
Mike Galbraith119fe5e2007-10-15 17:00:07 +0200995 if (likely(new_rq->cfs.min_vruntime))
996 p->se.vruntime -= old_rq->cfs.min_vruntime -
997 new_rq->cfs.min_vruntime;
Ingo Molnardd41f592007-07-09 18:51:59 +0200998
999 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001000}
1001
Ingo Molnar70b97a72006-07-03 00:25:42 -07001002struct migration_req {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Ingo Molnar36c8b582006-07-03 00:25:41 -07001005 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 int dest_cpu;
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 struct completion done;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001009};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011/*
1012 * The task's runqueue lock must be held.
1013 * Returns true if you have to wait for migration thread.
1014 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001015static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07001016migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001018 struct rq *rq = task_rq(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 /*
1021 * If the task is not on a runqueue (and not running), then
1022 * it is sufficient to simply update the task's cpu field.
1023 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001024 if (!p->se.on_rq && !task_running(rq, p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 set_task_cpu(p, dest_cpu);
1026 return 0;
1027 }
1028
1029 init_completion(&req->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 req->task = p;
1031 req->dest_cpu = dest_cpu;
1032 list_add(&req->list, &rq->migration_queue);
Ingo Molnar48f24c42006-07-03 00:25:40 -07001033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return 1;
1035}
1036
1037/*
1038 * wait_task_inactive - wait for a thread to unschedule.
1039 *
1040 * The caller must ensure that the task *will* unschedule sometime soon,
1041 * else this function might spin for a *long* time. This function can't
1042 * be called with interrupts off, or it may introduce deadlock with
1043 * smp_call_function() if an IPI is sent by the same process we are
1044 * waiting to become inactive.
1045 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001046void wait_task_inactive(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
1048 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001049 int running, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001050 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052repeat:
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001053 /*
1054 * We do the initial early heuristics without holding
1055 * any task-queue locks at all. We'll only try to get
1056 * the runqueue lock when things look like they will
1057 * work out!
1058 */
1059 rq = task_rq(p);
1060
1061 /*
1062 * If the task is actively running on another CPU
1063 * still, just relax and busy-wait without holding
1064 * any locks.
1065 *
1066 * NOTE! Since we don't hold any locks, it's not
1067 * even sure that "rq" stays as the right runqueue!
1068 * But we don't care, since "task_running()" will
1069 * return false if the runqueue has changed and p
1070 * is actually now running somewhere else!
1071 */
1072 while (task_running(rq, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 cpu_relax();
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001074
1075 /*
1076 * Ok, time to look more closely! We need the rq
1077 * lock now, to be *sure*. If we're wrong, we'll
1078 * just go back and repeat.
1079 */
1080 rq = task_rq_lock(p, &flags);
1081 running = task_running(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02001082 on_rq = p->se.on_rq;
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001083 task_rq_unlock(rq, &flags);
1084
1085 /*
1086 * Was it really running after all now that we
1087 * checked with the proper locks actually held?
1088 *
1089 * Oops. Go back and try again..
1090 */
1091 if (unlikely(running)) {
1092 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 goto repeat;
1094 }
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001095
1096 /*
1097 * It's not enough that it's not actively running,
1098 * it must be off the runqueue _entirely_, and not
1099 * preempted!
1100 *
1101 * So if it wa still runnable (but just not actively
1102 * running right now), it's preempted, and we should
1103 * yield - it could be a while.
1104 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001105 if (unlikely(on_rq)) {
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001106 yield();
1107 goto repeat;
1108 }
1109
1110 /*
1111 * Ahh, all good. It wasn't running, and it wasn't
1112 * runnable, which means that it will never become
1113 * running in the future either. We're all done!
1114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
1117/***
1118 * kick_process - kick a running thread to enter/exit the kernel
1119 * @p: the to-be-kicked thread
1120 *
1121 * Cause a process which is running on another CPU to enter
1122 * kernel-mode, without any delay. (to get signals handled.)
1123 *
1124 * NOTE: this function doesnt have to take the runqueue lock,
1125 * because all it wants to ensure is that the remote task enters
1126 * the kernel. If the IPI races and the task has been migrated
1127 * to another CPU then no harm is done and the purpose has been
1128 * achieved as well.
1129 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001130void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 int cpu;
1133
1134 preempt_disable();
1135 cpu = task_cpu(p);
1136 if ((cpu != smp_processor_id()) && task_curr(p))
1137 smp_send_reschedule(cpu);
1138 preempt_enable();
1139}
1140
1141/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001142 * Return a low guess at the load of a migration-source cpu weighted
1143 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 *
1145 * We want to under-estimate the load of migration sources, to
1146 * balance conservatively.
1147 */
Con Kolivasb9104722005-11-08 21:38:55 -08001148static inline unsigned long source_load(int cpu, int type)
1149{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001150 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001151 unsigned long total = weighted_cpuload(cpu);
Nick Piggina2000572006-02-10 01:51:02 -08001152
Peter Williams2dd73a42006-06-27 02:54:34 -07001153 if (type == 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02001154 return total;
Peter Williams2dd73a42006-06-27 02:54:34 -07001155
Ingo Molnardd41f592007-07-09 18:51:59 +02001156 return min(rq->cpu_load[type-1], total);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
1159/*
Peter Williams2dd73a42006-06-27 02:54:34 -07001160 * Return a high guess at the load of a migration-target cpu weighted
1161 * according to the scheduling class and "nice" value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 */
Con Kolivasb9104722005-11-08 21:38:55 -08001163static inline unsigned long target_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 max(rq->cpu_load[type-1], total);
Peter Williams2dd73a42006-06-27 02:54:34 -07001172}
1173
1174/*
1175 * Return the average load per task on the cpu's run queue
1176 */
1177static inline unsigned long cpu_avg_load_per_task(int cpu)
1178{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001179 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02001180 unsigned long total = weighted_cpuload(cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07001181 unsigned long n = rq->nr_running;
1182
Ingo Molnardd41f592007-07-09 18:51:59 +02001183 return n ? total / n : SCHED_LOAD_SCALE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
1185
Nick Piggin147cbb42005-06-25 14:57:19 -07001186/*
1187 * find_idlest_group finds and returns the least busy CPU group within the
1188 * domain.
1189 */
1190static struct sched_group *
1191find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
1192{
1193 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
1194 unsigned long min_load = ULONG_MAX, this_load = 0;
1195 int load_idx = sd->forkexec_idx;
1196 int imbalance = 100 + (sd->imbalance_pct-100)/2;
1197
1198 do {
1199 unsigned long load, avg_load;
1200 int local_group;
1201 int i;
1202
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001203 /* Skip over this group if it has no CPUs allowed */
1204 if (!cpus_intersects(group->cpumask, p->cpus_allowed))
1205 goto nextgroup;
1206
Nick Piggin147cbb42005-06-25 14:57:19 -07001207 local_group = cpu_isset(this_cpu, group->cpumask);
Nick Piggin147cbb42005-06-25 14:57:19 -07001208
1209 /* Tally up the load of all CPUs in the group */
1210 avg_load = 0;
1211
1212 for_each_cpu_mask(i, group->cpumask) {
1213 /* Bias balancing toward cpus of our domain */
1214 if (local_group)
1215 load = source_load(i, load_idx);
1216 else
1217 load = target_load(i, load_idx);
1218
1219 avg_load += load;
1220 }
1221
1222 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07001223 avg_load = sg_div_cpu_power(group,
1224 avg_load * SCHED_LOAD_SCALE);
Nick Piggin147cbb42005-06-25 14:57:19 -07001225
1226 if (local_group) {
1227 this_load = avg_load;
1228 this = group;
1229 } else if (avg_load < min_load) {
1230 min_load = avg_load;
1231 idlest = group;
1232 }
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001233nextgroup:
Nick Piggin147cbb42005-06-25 14:57:19 -07001234 group = group->next;
1235 } while (group != sd->groups);
1236
1237 if (!idlest || 100*this_load < imbalance*min_load)
1238 return NULL;
1239 return idlest;
1240}
1241
1242/*
Satoru Takeuchi0feaece2006-10-03 01:14:10 -07001243 * find_idlest_cpu - find the idlest cpu among the cpus in group.
Nick Piggin147cbb42005-06-25 14:57:19 -07001244 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07001245static int
1246find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
Nick Piggin147cbb42005-06-25 14:57:19 -07001247{
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001248 cpumask_t tmp;
Nick Piggin147cbb42005-06-25 14:57:19 -07001249 unsigned long load, min_load = ULONG_MAX;
1250 int idlest = -1;
1251 int i;
1252
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001253 /* Traverse only the allowed CPUs */
1254 cpus_and(tmp, group->cpumask, p->cpus_allowed);
1255
1256 for_each_cpu_mask(i, tmp) {
Peter Williams2dd73a42006-06-27 02:54:34 -07001257 load = weighted_cpuload(i);
Nick Piggin147cbb42005-06-25 14:57:19 -07001258
1259 if (load < min_load || (load == min_load && i == this_cpu)) {
1260 min_load = load;
1261 idlest = i;
1262 }
1263 }
1264
1265 return idlest;
1266}
1267
Nick Piggin476d1392005-06-25 14:57:29 -07001268/*
1269 * sched_balance_self: balance the current task (running on cpu) in domains
1270 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
1271 * SD_BALANCE_EXEC.
1272 *
1273 * Balance, ie. select the least loaded group.
1274 *
1275 * Returns the target CPU number, or the same CPU if no balancing is needed.
1276 *
1277 * preempt must be disabled.
1278 */
1279static int sched_balance_self(int cpu, int flag)
1280{
1281 struct task_struct *t = current;
1282 struct sched_domain *tmp, *sd = NULL;
Nick Piggin147cbb42005-06-25 14:57:19 -07001283
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001284 for_each_domain(cpu, tmp) {
Ingo Molnar9761eea2007-07-09 18:52:00 +02001285 /*
1286 * If power savings logic is enabled for a domain, stop there.
1287 */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07001288 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
1289 break;
Nick Piggin476d1392005-06-25 14:57:29 -07001290 if (tmp->flags & flag)
1291 sd = tmp;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001292 }
Nick Piggin476d1392005-06-25 14:57:29 -07001293
1294 while (sd) {
1295 cpumask_t span;
1296 struct sched_group *group;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001297 int new_cpu, weight;
1298
1299 if (!(sd->flags & flag)) {
1300 sd = sd->child;
1301 continue;
1302 }
Nick Piggin476d1392005-06-25 14:57:29 -07001303
1304 span = sd->span;
1305 group = find_idlest_group(sd, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001306 if (!group) {
1307 sd = sd->child;
1308 continue;
1309 }
Nick Piggin476d1392005-06-25 14:57:29 -07001310
M.Baris Demirayda5a5522005-09-10 00:26:09 -07001311 new_cpu = find_idlest_cpu(group, t, cpu);
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001312 if (new_cpu == -1 || new_cpu == cpu) {
1313 /* Now try balancing at a lower domain level of cpu */
1314 sd = sd->child;
1315 continue;
1316 }
Nick Piggin476d1392005-06-25 14:57:29 -07001317
Siddha, Suresh B1a848872006-10-03 01:14:08 -07001318 /* Now try balancing at a lower domain level of new_cpu */
Nick Piggin476d1392005-06-25 14:57:29 -07001319 cpu = new_cpu;
Nick Piggin476d1392005-06-25 14:57:29 -07001320 sd = NULL;
1321 weight = cpus_weight(span);
1322 for_each_domain(cpu, tmp) {
1323 if (weight <= cpus_weight(tmp->span))
1324 break;
1325 if (tmp->flags & flag)
1326 sd = tmp;
1327 }
1328 /* while loop will break here if sd == NULL */
1329 }
1330
1331 return cpu;
1332}
1333
1334#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336/*
1337 * wake_idle() will wake a task on an idle cpu if task->cpu is
1338 * not idle and an idle cpu is available. The span of cpus to
1339 * search starts with cpus closest then further out as needed,
1340 * so we always favor a closer, idle cpu.
1341 *
1342 * Returns the CPU we should wake onto.
1343 */
1344#if defined(ARCH_HAS_SCHED_WAKE_IDLE)
Ingo Molnar36c8b582006-07-03 00:25:41 -07001345static int wake_idle(int cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
1347 cpumask_t tmp;
1348 struct sched_domain *sd;
1349 int i;
1350
Siddha, Suresh B49531982007-05-08 00:33:01 -07001351 /*
1352 * If it is idle, then it is the best cpu to run this task.
1353 *
1354 * This cpu is also the best, if it has more than one task already.
1355 * Siblings must be also busy(in most cases) as they didn't already
1356 * pickup the extra load from this cpu and hence we need not check
1357 * sibling runqueue info. This will avoid the checks and cache miss
1358 * penalities associated with that.
1359 */
1360 if (idle_cpu(cpu) || cpu_rq(cpu)->nr_running > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return cpu;
1362
1363 for_each_domain(cpu, sd) {
1364 if (sd->flags & SD_WAKE_IDLE) {
Nick Piggine0f364f2005-06-25 14:57:06 -07001365 cpus_and(tmp, sd->span, p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 for_each_cpu_mask(i, tmp) {
1367 if (idle_cpu(i))
1368 return i;
1369 }
Ingo Molnar9761eea2007-07-09 18:52:00 +02001370 } else {
Nick Piggine0f364f2005-06-25 14:57:06 -07001371 break;
Ingo Molnar9761eea2007-07-09 18:52:00 +02001372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
1374 return cpu;
1375}
1376#else
Ingo Molnar36c8b582006-07-03 00:25:41 -07001377static inline int wake_idle(int cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 return cpu;
1380}
1381#endif
1382
1383/***
1384 * try_to_wake_up - wake up a thread
1385 * @p: the to-be-woken-up thread
1386 * @state: the mask of task states that can be woken
1387 * @sync: do a synchronous wakeup?
1388 *
1389 * Put it on the run-queue if it's not already there. The "current"
1390 * thread is always on the run-queue (except when the actual
1391 * re-schedule is in progress), and as such you're allowed to do
1392 * the simpler "current->state = TASK_RUNNING" to mark yourself
1393 * runnable without the overhead of this.
1394 *
1395 * returns failure only if the task is already active.
1396 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001397static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
1399 int cpu, this_cpu, success = 0;
1400 unsigned long flags;
1401 long old_state;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001402 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403#ifdef CONFIG_SMP
Nick Piggin78979862005-06-25 14:57:13 -07001404 struct sched_domain *sd, *this_sd = NULL;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001405 unsigned long load, this_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 int new_cpu;
1407#endif
1408
1409 rq = task_rq_lock(p, &flags);
1410 old_state = p->state;
1411 if (!(old_state & state))
1412 goto out;
1413
Ingo Molnardd41f592007-07-09 18:51:59 +02001414 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 goto out_running;
1416
1417 cpu = task_cpu(p);
1418 this_cpu = smp_processor_id();
1419
1420#ifdef CONFIG_SMP
1421 if (unlikely(task_running(rq, p)))
1422 goto out_activate;
1423
Nick Piggin78979862005-06-25 14:57:13 -07001424 new_cpu = cpu;
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 schedstat_inc(rq, ttwu_cnt);
1427 if (cpu == this_cpu) {
1428 schedstat_inc(rq, ttwu_local);
Nick Piggin78979862005-06-25 14:57:13 -07001429 goto out_set_cpu;
1430 }
1431
1432 for_each_domain(this_cpu, sd) {
1433 if (cpu_isset(cpu, sd->span)) {
1434 schedstat_inc(sd, ttwu_wake_remote);
1435 this_sd = sd;
1436 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
1438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Nick Piggin78979862005-06-25 14:57:13 -07001440 if (unlikely(!cpu_isset(this_cpu, p->cpus_allowed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 goto out_set_cpu;
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 /*
Nick Piggin78979862005-06-25 14:57:13 -07001444 * Check for affine wakeup and passive balancing possibilities.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 */
Nick Piggin78979862005-06-25 14:57:13 -07001446 if (this_sd) {
1447 int idx = this_sd->wake_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 unsigned int imbalance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Nick Piggina3f21bc2005-06-25 14:57:15 -07001450 imbalance = 100 + (this_sd->imbalance_pct - 100) / 2;
1451
Nick Piggin78979862005-06-25 14:57:13 -07001452 load = source_load(cpu, idx);
1453 this_load = target_load(this_cpu, idx);
1454
Nick Piggin78979862005-06-25 14:57:13 -07001455 new_cpu = this_cpu; /* Wake to this CPU if we can */
1456
Nick Piggina3f21bc2005-06-25 14:57:15 -07001457 if (this_sd->flags & SD_WAKE_AFFINE) {
1458 unsigned long tl = this_load;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08001459 unsigned long tl_per_task;
1460
1461 tl_per_task = cpu_avg_load_per_task(this_cpu);
Peter Williams2dd73a42006-06-27 02:54:34 -07001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 /*
Nick Piggina3f21bc2005-06-25 14:57:15 -07001464 * If sync wakeup then subtract the (maximum possible)
1465 * effect of the currently running task from the load
1466 * of the current CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 */
Nick Piggina3f21bc2005-06-25 14:57:15 -07001468 if (sync)
Ingo Molnardd41f592007-07-09 18:51:59 +02001469 tl -= current->se.load.weight;
Nick Piggina3f21bc2005-06-25 14:57:15 -07001470
1471 if ((tl <= load &&
Peter Williams2dd73a42006-06-27 02:54:34 -07001472 tl + target_load(cpu, idx) <= tl_per_task) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02001473 100*(tl + p->se.load.weight) <= imbalance*load) {
Nick Piggina3f21bc2005-06-25 14:57:15 -07001474 /*
1475 * This domain has SD_WAKE_AFFINE and
1476 * p is cache cold in this domain, and
1477 * there is no bad imbalance.
1478 */
1479 schedstat_inc(this_sd, ttwu_move_affine);
1480 goto out_set_cpu;
1481 }
1482 }
1483
1484 /*
1485 * Start passive balancing when half the imbalance_pct
1486 * limit is reached.
1487 */
1488 if (this_sd->flags & SD_WAKE_BALANCE) {
1489 if (imbalance*this_load <= 100*load) {
1490 schedstat_inc(this_sd, ttwu_move_balance);
1491 goto out_set_cpu;
1492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
1494 }
1495
1496 new_cpu = cpu; /* Could not wake to this_cpu. Wake to cpu instead */
1497out_set_cpu:
1498 new_cpu = wake_idle(new_cpu, p);
1499 if (new_cpu != cpu) {
1500 set_task_cpu(p, new_cpu);
1501 task_rq_unlock(rq, &flags);
1502 /* might preempt at this point */
1503 rq = task_rq_lock(p, &flags);
1504 old_state = p->state;
1505 if (!(old_state & state))
1506 goto out;
Ingo Molnardd41f592007-07-09 18:51:59 +02001507 if (p->se.on_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 goto out_running;
1509
1510 this_cpu = smp_processor_id();
1511 cpu = task_cpu(p);
1512 }
1513
1514out_activate:
1515#endif /* CONFIG_SMP */
Ingo Molnar2daa3572007-08-09 11:16:51 +02001516 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02001517 activate_task(rq, p, 1);
Ingo Molnard79fc0f2005-09-10 00:26:12 -07001518 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 * Sync wakeups (i.e. those types of wakeups where the waker
1520 * has indicated that it will leave the CPU in short order)
1521 * don't trigger a preemption, if the woken up task will run on
1522 * this cpu. (in this case the 'I will reschedule' promise of
1523 * the waker guarantees that the freshly woken up task is going
1524 * to be considered on this CPU.)
1525 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001526 if (!sync || cpu != this_cpu)
1527 check_preempt_curr(rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 success = 1;
1529
1530out_running:
1531 p->state = TASK_RUNNING;
1532out:
1533 task_rq_unlock(rq, &flags);
1534
1535 return success;
1536}
1537
Ingo Molnar36c8b582006-07-03 00:25:41 -07001538int fastcall wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539{
1540 return try_to_wake_up(p, TASK_STOPPED | TASK_TRACED |
1541 TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE, 0);
1542}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543EXPORT_SYMBOL(wake_up_process);
1544
Ingo Molnar36c8b582006-07-03 00:25:41 -07001545int fastcall wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546{
1547 return try_to_wake_up(p, state, 0);
1548}
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550/*
1551 * Perform scheduler related setup for a newly forked process p.
1552 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02001553 *
1554 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001556static void __sched_fork(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Ingo Molnardd41f592007-07-09 18:51:59 +02001558 p->se.exec_start = 0;
1559 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02001560 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001561
1562#ifdef CONFIG_SCHEDSTATS
1563 p->se.wait_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02001564 p->se.sum_sleep_runtime = 0;
1565 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02001566 p->se.block_start = 0;
1567 p->se.sleep_max = 0;
1568 p->se.block_max = 0;
1569 p->se.exec_max = 0;
Ingo Molnareba1ed42007-10-15 17:00:02 +02001570 p->se.slice_max = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02001571 p->se.wait_max = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02001572#endif
Nick Piggin476d1392005-06-25 14:57:29 -07001573
Ingo Molnardd41f592007-07-09 18:51:59 +02001574 INIT_LIST_HEAD(&p->run_list);
1575 p->se.on_rq = 0;
Nick Piggin476d1392005-06-25 14:57:29 -07001576
Avi Kivitye107be32007-07-26 13:40:43 +02001577#ifdef CONFIG_PREEMPT_NOTIFIERS
1578 INIT_HLIST_HEAD(&p->preempt_notifiers);
1579#endif
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 /*
1582 * We mark the process as running here, but have not actually
1583 * inserted it onto the runqueue yet. This guarantees that
1584 * nobody will actually run it, and a signal or other external
1585 * event cannot wake it up and insert it on the runqueue either.
1586 */
1587 p->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02001588}
1589
1590/*
1591 * fork()/clone()-time setup:
1592 */
1593void sched_fork(struct task_struct *p, int clone_flags)
1594{
1595 int cpu = get_cpu();
1596
1597 __sched_fork(p);
1598
1599#ifdef CONFIG_SMP
1600 cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
1601#endif
1602 __set_task_cpu(p, cpu);
Ingo Molnarb29739f2006-06-27 02:54:51 -07001603
1604 /*
1605 * Make sure we do not leak PI boosting priority to the child:
1606 */
1607 p->prio = current->normal_prio;
1608
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07001609#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
Ingo Molnardd41f592007-07-09 18:51:59 +02001610 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07001611 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612#endif
Chen, Kenneth Wd6077cb2006-02-14 13:53:10 -08001613#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
Nick Piggin4866cde2005-06-25 14:57:23 -07001614 p->oncpu = 0;
1615#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616#ifdef CONFIG_PREEMPT
Nick Piggin4866cde2005-06-25 14:57:23 -07001617 /* Want to start with kernel preemption disabled. */
Al Viroa1261f52005-11-13 16:06:55 -08001618 task_thread_info(p)->preempt_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619#endif
Nick Piggin476d1392005-06-25 14:57:29 -07001620 put_cpu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
1623/*
1624 * wake_up_new_task - wake up a newly created task for the first time.
1625 *
1626 * This function will do some initial scheduler statistics housekeeping
1627 * that must be done for every newly created context, then puts the task
1628 * on the runqueue and wakes it.
1629 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001630void fastcall wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
1632 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02001633 struct rq *rq;
1634 int this_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 rq = task_rq_lock(p, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 BUG_ON(p->state != TASK_RUNNING);
Ingo Molnardd41f592007-07-09 18:51:59 +02001638 this_cpu = smp_processor_id(); /* parent's CPU */
Ingo Molnara8e504d2007-08-09 11:16:47 +02001639 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 p->prio = effective_prio(p);
1642
Hiroshi Shimamoto9c95e732007-09-19 23:34:46 +02001643 if (rt_prio(p->prio))
1644 p->sched_class = &rt_sched_class;
1645 else
1646 p->sched_class = &fair_sched_class;
1647
Ingo Molnar44142fa2007-10-15 17:00:01 +02001648 if (task_cpu(p) != this_cpu || !p->sched_class->task_new ||
1649 !current->se.on_rq) {
Ingo Molnardd41f592007-07-09 18:51:59 +02001650 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 /*
Ingo Molnardd41f592007-07-09 18:51:59 +02001653 * Let the scheduling class do new task startup
1654 * management (if any):
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02001656 p->sched_class->task_new(rq, p);
Ingo Molnare5fa2232007-08-09 11:16:49 +02001657 inc_nr_running(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
Ingo Molnardd41f592007-07-09 18:51:59 +02001659 check_preempt_curr(rq, p);
1660 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
1662
Avi Kivitye107be32007-07-26 13:40:43 +02001663#ifdef CONFIG_PREEMPT_NOTIFIERS
1664
1665/**
Randy Dunlap421cee22007-07-31 00:37:50 -07001666 * preempt_notifier_register - tell me when current is being being preempted & rescheduled
1667 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02001668 */
1669void preempt_notifier_register(struct preempt_notifier *notifier)
1670{
1671 hlist_add_head(&notifier->link, &current->preempt_notifiers);
1672}
1673EXPORT_SYMBOL_GPL(preempt_notifier_register);
1674
1675/**
1676 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07001677 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02001678 *
1679 * This is safe to call from within a preemption notifier.
1680 */
1681void preempt_notifier_unregister(struct preempt_notifier *notifier)
1682{
1683 hlist_del(&notifier->link);
1684}
1685EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
1686
1687static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
1688{
1689 struct preempt_notifier *notifier;
1690 struct hlist_node *node;
1691
1692 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
1693 notifier->ops->sched_in(notifier, raw_smp_processor_id());
1694}
1695
1696static void
1697fire_sched_out_preempt_notifiers(struct task_struct *curr,
1698 struct task_struct *next)
1699{
1700 struct preempt_notifier *notifier;
1701 struct hlist_node *node;
1702
1703 hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
1704 notifier->ops->sched_out(notifier, next);
1705}
1706
1707#else
1708
1709static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
1710{
1711}
1712
1713static void
1714fire_sched_out_preempt_notifiers(struct task_struct *curr,
1715 struct task_struct *next)
1716{
1717}
1718
1719#endif
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721/**
Nick Piggin4866cde2005-06-25 14:57:23 -07001722 * prepare_task_switch - prepare to switch tasks
1723 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07001724 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07001725 * @next: the task we are going to switch to.
1726 *
1727 * This is called with the rq lock held and interrupts off. It must
1728 * be paired with a subsequent finish_task_switch after the context
1729 * switch.
1730 *
1731 * prepare_task_switch sets up locking and calls architecture specific
1732 * hooks.
1733 */
Avi Kivitye107be32007-07-26 13:40:43 +02001734static inline void
1735prepare_task_switch(struct rq *rq, struct task_struct *prev,
1736 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07001737{
Avi Kivitye107be32007-07-26 13:40:43 +02001738 fire_sched_out_preempt_notifiers(prev, next);
Nick Piggin4866cde2005-06-25 14:57:23 -07001739 prepare_lock_switch(rq, next);
1740 prepare_arch_switch(next);
1741}
1742
1743/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 * finish_task_switch - clean up after a task-switch
Jeff Garzik344baba2005-09-07 01:15:17 -04001745 * @rq: runqueue associated with task-switch
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 * @prev: the thread we just switched away from.
1747 *
Nick Piggin4866cde2005-06-25 14:57:23 -07001748 * finish_task_switch must be called after the context switch, paired
1749 * with a prepare_task_switch call before the context switch.
1750 * finish_task_switch will reconcile locking set up by prepare_task_switch,
1751 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 *
1753 * Note that we may have delayed dropping an mm in context_switch(). If
1754 * so, we finish that here outside of the runqueue lock. (Doing it
1755 * with the lock held can cause deadlocks; see schedule() for
1756 * details.)
1757 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07001758static inline void finish_task_switch(struct rq *rq, struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 __releases(rq->lock)
1760{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07001762 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 rq->prev_mm = NULL;
1765
1766 /*
1767 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07001768 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07001769 * schedule one last time. The schedule call will never return, and
1770 * the scheduled task must drop that reference.
Oleg Nesterovc394cc92006-09-29 02:01:11 -07001771 * The test for TASK_DEAD must occur while the runqueue locks are
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 * still held, otherwise prev could be scheduled on another cpu, die
1773 * there before we look at prev->state, and then the reference would
1774 * be dropped twice.
1775 * Manfred Spraul <manfred@colorfullife.com>
1776 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07001777 prev_state = prev->state;
Nick Piggin4866cde2005-06-25 14:57:23 -07001778 finish_arch_switch(prev);
1779 finish_lock_switch(rq, prev);
Avi Kivitye107be32007-07-26 13:40:43 +02001780 fire_sched_in_preempt_notifiers(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 if (mm)
1782 mmdrop(mm);
Oleg Nesterovc394cc92006-09-29 02:01:11 -07001783 if (unlikely(prev_state == TASK_DEAD)) {
bibo maoc6fd91f2006-03-26 01:38:20 -08001784 /*
1785 * Remove function-return probe instances associated with this
1786 * task and put them back on the free list.
Ingo Molnar9761eea2007-07-09 18:52:00 +02001787 */
bibo maoc6fd91f2006-03-26 01:38:20 -08001788 kprobe_flush_task(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 put_task_struct(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08001790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791}
1792
1793/**
1794 * schedule_tail - first thing a freshly forked thread must call.
1795 * @prev: the thread we just switched away from.
1796 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001797asmlinkage void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 __releases(rq->lock)
1799{
Ingo Molnar70b97a72006-07-03 00:25:42 -07001800 struct rq *rq = this_rq();
1801
Nick Piggin4866cde2005-06-25 14:57:23 -07001802 finish_task_switch(rq, prev);
1803#ifdef __ARCH_WANT_UNLOCKED_CTXSW
1804 /* In this case, finish_task_switch does not reenable preemption */
1805 preempt_enable();
1806#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (current->set_child_tid)
1808 put_user(current->pid, current->set_child_tid);
1809}
1810
1811/*
1812 * context_switch - switch to the new MM and the new
1813 * thread's register state.
1814 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001815static inline void
Ingo Molnar70b97a72006-07-03 00:25:42 -07001816context_switch(struct rq *rq, struct task_struct *prev,
Ingo Molnar36c8b582006-07-03 00:25:41 -07001817 struct task_struct *next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
Ingo Molnardd41f592007-07-09 18:51:59 +02001819 struct mm_struct *mm, *oldmm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Avi Kivitye107be32007-07-26 13:40:43 +02001821 prepare_task_switch(rq, prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02001822 mm = next->mm;
1823 oldmm = prev->active_mm;
Zachary Amsden9226d122007-02-13 13:26:21 +01001824 /*
1825 * For paravirt, this is coupled with an exit in switch_to to
1826 * combine the page table reload and the switch backend into
1827 * one hypercall.
1828 */
1829 arch_enter_lazy_cpu_mode();
1830
Ingo Molnardd41f592007-07-09 18:51:59 +02001831 if (unlikely(!mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 next->active_mm = oldmm;
1833 atomic_inc(&oldmm->mm_count);
1834 enter_lazy_tlb(oldmm, next);
1835 } else
1836 switch_mm(oldmm, mm, next);
1837
Ingo Molnardd41f592007-07-09 18:51:59 +02001838 if (unlikely(!prev->mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 prev->active_mm = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 rq->prev_mm = oldmm;
1841 }
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07001842 /*
1843 * Since the runqueue lock will be released by the next
1844 * task (which is an invalid locking op but in the case
1845 * of the scheduler it's an obvious special-case), so we
1846 * do an early lockdep release here:
1847 */
1848#ifndef __ARCH_WANT_UNLOCKED_CTXSW
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07001849 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Ingo Molnar3a5f5e42006-07-14 00:24:27 -07001850#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 /* Here we just switch the register state and the stack. */
1853 switch_to(prev, next, prev);
1854
Ingo Molnardd41f592007-07-09 18:51:59 +02001855 barrier();
1856 /*
1857 * this_rq must be evaluated again because prev may have moved
1858 * CPUs since it called schedule(), thus the 'rq' on its stack
1859 * frame will be invalid.
1860 */
1861 finish_task_switch(this_rq(), prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
1864/*
1865 * nr_running, nr_uninterruptible and nr_context_switches:
1866 *
1867 * externally visible scheduler statistics: current number of runnable
1868 * threads, current number of uninterruptible-sleeping threads, total
1869 * number of context switches performed since bootup.
1870 */
1871unsigned long nr_running(void)
1872{
1873 unsigned long i, sum = 0;
1874
1875 for_each_online_cpu(i)
1876 sum += cpu_rq(i)->nr_running;
1877
1878 return sum;
1879}
1880
1881unsigned long nr_uninterruptible(void)
1882{
1883 unsigned long i, sum = 0;
1884
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001885 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 sum += cpu_rq(i)->nr_uninterruptible;
1887
1888 /*
1889 * Since we read the counters lockless, it might be slightly
1890 * inaccurate. Do not allow it to go below zero though:
1891 */
1892 if (unlikely((long)sum < 0))
1893 sum = 0;
1894
1895 return sum;
1896}
1897
1898unsigned long long nr_context_switches(void)
1899{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07001900 int i;
1901 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001903 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 sum += cpu_rq(i)->nr_switches;
1905
1906 return sum;
1907}
1908
1909unsigned long nr_iowait(void)
1910{
1911 unsigned long i, sum = 0;
1912
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08001913 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 sum += atomic_read(&cpu_rq(i)->nr_iowait);
1915
1916 return sum;
1917}
1918
Jack Steinerdb1b1fe2006-03-31 02:31:21 -08001919unsigned long nr_active(void)
1920{
1921 unsigned long i, running = 0, uninterruptible = 0;
1922
1923 for_each_online_cpu(i) {
1924 running += cpu_rq(i)->nr_running;
1925 uninterruptible += cpu_rq(i)->nr_uninterruptible;
1926 }
1927
1928 if (unlikely((long)uninterruptible < 0))
1929 uninterruptible = 0;
1930
1931 return running + uninterruptible;
1932}
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001935 * Update rq->cpu_load[] statistics. This function is usually called every
1936 * scheduler tick (TICK_NSEC).
Ingo Molnar48f24c42006-07-03 00:25:40 -07001937 */
Ingo Molnardd41f592007-07-09 18:51:59 +02001938static void update_cpu_load(struct rq *this_rq)
Ingo Molnar48f24c42006-07-03 00:25:40 -07001939{
Dmitry Adamushko495eca42007-10-15 17:00:06 +02001940 unsigned long this_load = this_rq->load.weight;
Ingo Molnardd41f592007-07-09 18:51:59 +02001941 int i, scale;
1942
1943 this_rq->nr_load_updates++;
Ingo Molnardd41f592007-07-09 18:51:59 +02001944
1945 /* Update our load: */
1946 for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
1947 unsigned long old_load, new_load;
1948
1949 /* scale is effectively 1 << i now, and >> i divides by scale */
1950
1951 old_load = this_rq->cpu_load[i];
1952 new_load = this_load;
Ingo Molnara25707f2007-10-15 17:00:03 +02001953 /*
1954 * Round up the averaging division if load is increasing. This
1955 * prevents us from getting stuck on 9 if the load is 10, for
1956 * example.
1957 */
1958 if (new_load > old_load)
1959 new_load += scale-1;
Ingo Molnardd41f592007-07-09 18:51:59 +02001960 this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
1961 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07001962}
1963
Ingo Molnardd41f592007-07-09 18:51:59 +02001964#ifdef CONFIG_SMP
1965
Ingo Molnar48f24c42006-07-03 00:25:40 -07001966/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 * double_rq_lock - safely lock two runqueues
1968 *
1969 * Note this does not disable interrupts like task_rq_lock,
1970 * you need to do so manually before calling.
1971 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07001972static void double_rq_lock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 __acquires(rq1->lock)
1974 __acquires(rq2->lock)
1975{
Kirill Korotaev054b9102006-12-10 02:20:11 -08001976 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 if (rq1 == rq2) {
1978 spin_lock(&rq1->lock);
1979 __acquire(rq2->lock); /* Fake it out ;) */
1980 } else {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07001981 if (rq1 < rq2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 spin_lock(&rq1->lock);
1983 spin_lock(&rq2->lock);
1984 } else {
1985 spin_lock(&rq2->lock);
1986 spin_lock(&rq1->lock);
1987 }
1988 }
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02001989 update_rq_clock(rq1);
1990 update_rq_clock(rq2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991}
1992
1993/*
1994 * double_rq_unlock - safely unlock two runqueues
1995 *
1996 * Note this does not restore interrupts like task_rq_unlock,
1997 * you need to do so manually after calling.
1998 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07001999static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 __releases(rq1->lock)
2001 __releases(rq2->lock)
2002{
2003 spin_unlock(&rq1->lock);
2004 if (rq1 != rq2)
2005 spin_unlock(&rq2->lock);
2006 else
2007 __release(rq2->lock);
2008}
2009
2010/*
2011 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2012 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002013static void double_lock_balance(struct rq *this_rq, struct rq *busiest)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 __releases(this_rq->lock)
2015 __acquires(busiest->lock)
2016 __acquires(this_rq->lock)
2017{
Kirill Korotaev054b9102006-12-10 02:20:11 -08002018 if (unlikely(!irqs_disabled())) {
2019 /* printk() doesn't work good under rq->lock */
2020 spin_unlock(&this_rq->lock);
2021 BUG_ON(1);
2022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 if (unlikely(!spin_trylock(&busiest->lock))) {
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002024 if (busiest < this_rq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 spin_unlock(&this_rq->lock);
2026 spin_lock(&busiest->lock);
2027 spin_lock(&this_rq->lock);
2028 } else
2029 spin_lock(&busiest->lock);
2030 }
2031}
2032
2033/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 * If dest_cpu is allowed for this process, migrate the task to it.
2035 * This is accomplished by forcing the cpu_allowed mask to only
2036 * allow dest_cpu, which will force the cpu onto dest_cpu. Then
2037 * the cpu_allowed mask is restored.
2038 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002039static void sched_migrate_task(struct task_struct *p, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002041 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002043 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 rq = task_rq_lock(p, &flags);
2046 if (!cpu_isset(dest_cpu, p->cpus_allowed)
2047 || unlikely(cpu_is_offline(dest_cpu)))
2048 goto out;
2049
2050 /* force the process onto the specified CPU */
2051 if (migrate_task(p, dest_cpu, &req)) {
2052 /* Need to wait for migration thread (might exit: take ref). */
2053 struct task_struct *mt = rq->migration_thread;
Ingo Molnar36c8b582006-07-03 00:25:41 -07002054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 get_task_struct(mt);
2056 task_rq_unlock(rq, &flags);
2057 wake_up_process(mt);
2058 put_task_struct(mt);
2059 wait_for_completion(&req.done);
Ingo Molnar36c8b582006-07-03 00:25:41 -07002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 return;
2062 }
2063out:
2064 task_rq_unlock(rq, &flags);
2065}
2066
2067/*
Nick Piggin476d1392005-06-25 14:57:29 -07002068 * sched_exec - execve() is a valuable balancing opportunity, because at
2069 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 */
2071void sched_exec(void)
2072{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 int new_cpu, this_cpu = get_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002074 new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 put_cpu();
Nick Piggin476d1392005-06-25 14:57:29 -07002076 if (new_cpu != this_cpu)
2077 sched_migrate_task(current, new_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
2080/*
2081 * pull_task - move a task from a remote runqueue to the local runqueue.
2082 * Both runqueues must be locked.
2083 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002084static void pull_task(struct rq *src_rq, struct task_struct *p,
2085 struct rq *this_rq, int this_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
Ingo Molnar2e1cb742007-08-09 11:16:49 +02002087 deactivate_task(src_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 set_task_cpu(p, this_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02002089 activate_task(this_rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 /*
2091 * Note that idle threads have a prio of MAX_PRIO, for this test
2092 * to be always true for them.
2093 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002094 check_preempt_curr(this_rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
2096
2097/*
2098 * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
2099 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08002100static
Ingo Molnar70b97a72006-07-03 00:25:42 -07002101int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002102 struct sched_domain *sd, enum cpu_idle_type idle,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07002103 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
2105 /*
2106 * We do not migrate tasks that are:
2107 * 1) running (obviously), or
2108 * 2) cannot be migrated to this CPU due to cpus_allowed, or
2109 * 3) are cache-hot on their current CPU.
2110 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 if (!cpu_isset(this_cpu, p->cpus_allowed))
2112 return 0;
Nick Piggin81026792005-06-25 14:57:07 -07002113 *all_pinned = 0;
2114
2115 if (task_running(rq, p))
2116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 return 1;
2119}
2120
Ingo Molnardd41f592007-07-09 18:51:59 +02002121static int balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
2122 unsigned long max_nr_move, unsigned long max_load_move,
2123 struct sched_domain *sd, enum cpu_idle_type idle,
2124 int *all_pinned, unsigned long *load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002125 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02002126{
2127 int pulled = 0, pinned = 0, skip_for_load;
2128 struct task_struct *p;
2129 long rem_load_move = max_load_move;
2130
2131 if (max_nr_move == 0 || max_load_move == 0)
2132 goto out;
2133
2134 pinned = 1;
2135
2136 /*
2137 * Start the load-balancing iterator:
2138 */
2139 p = iterator->start(iterator->arg);
2140next:
2141 if (!p)
2142 goto out;
2143 /*
2144 * To help distribute high priority tasks accross CPUs we don't
2145 * skip a task if it will be the highest priority task (i.e. smallest
2146 * prio value) on its new queue regardless of its load weight
2147 */
2148 skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
2149 SCHED_LOAD_SCALE_FUZZ;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002150 if ((skip_for_load && p->prio >= *this_best_prio) ||
Ingo Molnardd41f592007-07-09 18:51:59 +02002151 !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002152 p = iterator->next(iterator->arg);
2153 goto next;
2154 }
2155
2156 pull_task(busiest, p, this_rq, this_cpu);
2157 pulled++;
2158 rem_load_move -= p->se.load.weight;
2159
2160 /*
2161 * We only want to steal up to the prescribed number of tasks
2162 * and the prescribed amount of weighted load.
2163 */
2164 if (pulled < max_nr_move && rem_load_move > 0) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002165 if (p->prio < *this_best_prio)
2166 *this_best_prio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02002167 p = iterator->next(iterator->arg);
2168 goto next;
2169 }
2170out:
2171 /*
2172 * Right now, this is the only place pull_task() is called,
2173 * so we can safely collect pull_task() stats here rather than
2174 * inside pull_task().
2175 */
2176 schedstat_add(sd, lb_gained[idle], pulled);
2177
2178 if (all_pinned)
2179 *all_pinned = pinned;
2180 *load_moved = max_load_move - rem_load_move;
2181 return pulled;
2182}
Ingo Molnar48f24c42006-07-03 00:25:40 -07002183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184/*
Peter Williams43010652007-08-09 11:16:46 +02002185 * move_tasks tries to move up to max_load_move weighted load from busiest to
2186 * this_rq, as part of a balancing operation within domain "sd".
2187 * Returns 1 if successful and 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 *
2189 * Called with both runqueues locked.
2190 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002191static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williams43010652007-08-09 11:16:46 +02002192 unsigned long max_load_move,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002193 struct sched_domain *sd, enum cpu_idle_type idle,
Peter Williams2dd73a42006-06-27 02:54:34 -07002194 int *all_pinned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
Ingo Molnardd41f592007-07-09 18:51:59 +02002196 struct sched_class *class = sched_class_highest;
Peter Williams43010652007-08-09 11:16:46 +02002197 unsigned long total_load_moved = 0;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002198 int this_best_prio = this_rq->curr->prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Ingo Molnardd41f592007-07-09 18:51:59 +02002200 do {
Peter Williams43010652007-08-09 11:16:46 +02002201 total_load_moved +=
2202 class->load_balance(this_rq, this_cpu, busiest,
2203 ULONG_MAX, max_load_move - total_load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002204 sd, idle, all_pinned, &this_best_prio);
Ingo Molnardd41f592007-07-09 18:51:59 +02002205 class = class->next;
Peter Williams43010652007-08-09 11:16:46 +02002206 } while (class && max_load_move > total_load_moved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Peter Williams43010652007-08-09 11:16:46 +02002208 return total_load_moved > 0;
2209}
2210
2211/*
2212 * move_one_task tries to move exactly one task from busiest to this_rq, as
2213 * part of active balancing operations within "domain".
2214 * Returns 1 if successful and 0 otherwise.
2215 *
2216 * Called with both runqueues locked.
2217 */
2218static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
2219 struct sched_domain *sd, enum cpu_idle_type idle)
2220{
2221 struct sched_class *class;
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002222 int this_best_prio = MAX_PRIO;
Peter Williams43010652007-08-09 11:16:46 +02002223
2224 for (class = sched_class_highest; class; class = class->next)
2225 if (class->load_balance(this_rq, this_cpu, busiest,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02002226 1, ULONG_MAX, sd, idle, NULL,
2227 &this_best_prio))
Peter Williams43010652007-08-09 11:16:46 +02002228 return 1;
2229
2230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
2233/*
2234 * find_busiest_group finds and returns the busiest CPU group within the
Ingo Molnar48f24c42006-07-03 00:25:40 -07002235 * domain. It calculates and returns the amount of weighted load which
2236 * should be moved to restore balance via the imbalance parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 */
2238static struct sched_group *
2239find_busiest_group(struct sched_domain *sd, int this_cpu,
Ingo Molnardd41f592007-07-09 18:51:59 +02002240 unsigned long *imbalance, enum cpu_idle_type idle,
2241 int *sd_idle, cpumask_t *cpus, int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242{
2243 struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
2244 unsigned long max_load, avg_load, total_load, this_load, total_pwr;
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002245 unsigned long max_pull;
Peter Williams2dd73a42006-06-27 02:54:34 -07002246 unsigned long busiest_load_per_task, busiest_nr_running;
2247 unsigned long this_load_per_task, this_nr_running;
Nick Piggin78979862005-06-25 14:57:13 -07002248 int load_idx;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002249#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2250 int power_savings_balance = 1;
2251 unsigned long leader_nr_running = 0, min_load_per_task = 0;
2252 unsigned long min_nr_running = ULONG_MAX;
2253 struct sched_group *group_min = NULL, *group_leader = NULL;
2254#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 max_load = this_load = total_load = total_pwr = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002257 busiest_load_per_task = busiest_nr_running = 0;
2258 this_load_per_task = this_nr_running = 0;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002259 if (idle == CPU_NOT_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002260 load_idx = sd->busy_idx;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002261 else if (idle == CPU_NEWLY_IDLE)
Nick Piggin78979862005-06-25 14:57:13 -07002262 load_idx = sd->newidle_idx;
2263 else
2264 load_idx = sd->idle_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
2266 do {
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002267 unsigned long load, group_capacity;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 int local_group;
2269 int i;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002270 unsigned int balance_cpu = -1, first_idle_cpu = 0;
Peter Williams2dd73a42006-06-27 02:54:34 -07002271 unsigned long sum_nr_running, sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 local_group = cpu_isset(this_cpu, group->cpumask);
2274
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002275 if (local_group)
2276 balance_cpu = first_cpu(group->cpumask);
2277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 /* Tally up the load of all CPUs in the group */
Peter Williams2dd73a42006-06-27 02:54:34 -07002279 sum_weighted_load = sum_nr_running = avg_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
2281 for_each_cpu_mask(i, group->cpumask) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002282 struct rq *rq;
2283
2284 if (!cpu_isset(i, *cpus))
2285 continue;
2286
2287 rq = cpu_rq(i);
Peter Williams2dd73a42006-06-27 02:54:34 -07002288
Suresh Siddha9439aab2007-07-19 21:28:35 +02002289 if (*sd_idle && rq->nr_running)
Nick Piggin5969fe02005-09-10 00:26:19 -07002290 *sd_idle = 0;
2291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 /* Bias balancing toward cpus of our domain */
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002293 if (local_group) {
2294 if (idle_cpu(i) && !first_idle_cpu) {
2295 first_idle_cpu = 1;
2296 balance_cpu = i;
2297 }
2298
Nick Piggina2000572006-02-10 01:51:02 -08002299 load = target_load(i, load_idx);
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002300 } else
Nick Piggina2000572006-02-10 01:51:02 -08002301 load = source_load(i, load_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 avg_load += load;
Peter Williams2dd73a42006-06-27 02:54:34 -07002304 sum_nr_running += rq->nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002305 sum_weighted_load += weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
2307
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002308 /*
2309 * First idle cpu or the first cpu(busiest) in this sched group
2310 * is eligible for doing load balancing at this and above
Suresh Siddha9439aab2007-07-19 21:28:35 +02002311 * domains. In the newly idle case, we will allow all the cpu's
2312 * to do the newly idle load balance.
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002313 */
Suresh Siddha9439aab2007-07-19 21:28:35 +02002314 if (idle != CPU_NEWLY_IDLE && local_group &&
2315 balance_cpu != this_cpu && balance) {
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002316 *balance = 0;
2317 goto ret;
2318 }
2319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 total_load += avg_load;
Eric Dumazet5517d862007-05-08 00:32:57 -07002321 total_pwr += group->__cpu_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 /* Adjust by relative CPU power of the group */
Eric Dumazet5517d862007-05-08 00:32:57 -07002324 avg_load = sg_div_cpu_power(group,
2325 avg_load * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Eric Dumazet5517d862007-05-08 00:32:57 -07002327 group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 if (local_group) {
2330 this_load = avg_load;
2331 this = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002332 this_nr_running = sum_nr_running;
2333 this_load_per_task = sum_weighted_load;
2334 } else if (avg_load > max_load &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002335 sum_nr_running > group_capacity) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 max_load = avg_load;
2337 busiest = group;
Peter Williams2dd73a42006-06-27 02:54:34 -07002338 busiest_nr_running = sum_nr_running;
2339 busiest_load_per_task = sum_weighted_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002341
2342#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
2343 /*
2344 * Busy processors will not participate in power savings
2345 * balance.
2346 */
Ingo Molnardd41f592007-07-09 18:51:59 +02002347 if (idle == CPU_NOT_IDLE ||
2348 !(sd->flags & SD_POWERSAVINGS_BALANCE))
2349 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002350
2351 /*
2352 * If the local group is idle or completely loaded
2353 * no need to do power savings balance at this domain
2354 */
2355 if (local_group && (this_nr_running >= group_capacity ||
2356 !this_nr_running))
2357 power_savings_balance = 0;
2358
Ingo Molnardd41f592007-07-09 18:51:59 +02002359 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002360 * If a group is already running at full capacity or idle,
2361 * don't include that group in power savings calculations
Ingo Molnardd41f592007-07-09 18:51:59 +02002362 */
2363 if (!power_savings_balance || sum_nr_running >= group_capacity
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002364 || !sum_nr_running)
Ingo Molnardd41f592007-07-09 18:51:59 +02002365 goto group_next;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002366
Ingo Molnardd41f592007-07-09 18:51:59 +02002367 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002368 * Calculate the group which has the least non-idle load.
Ingo Molnardd41f592007-07-09 18:51:59 +02002369 * This is the group from where we need to pick up the load
2370 * for saving power
2371 */
2372 if ((sum_nr_running < min_nr_running) ||
2373 (sum_nr_running == min_nr_running &&
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002374 first_cpu(group->cpumask) <
2375 first_cpu(group_min->cpumask))) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002376 group_min = group;
2377 min_nr_running = sum_nr_running;
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002378 min_load_per_task = sum_weighted_load /
2379 sum_nr_running;
Ingo Molnardd41f592007-07-09 18:51:59 +02002380 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002381
Ingo Molnardd41f592007-07-09 18:51:59 +02002382 /*
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002383 * Calculate the group which is almost near its
Ingo Molnardd41f592007-07-09 18:51:59 +02002384 * capacity but still has some space to pick up some load
2385 * from other group and save more power
2386 */
2387 if (sum_nr_running <= group_capacity - 1) {
2388 if (sum_nr_running > leader_nr_running ||
2389 (sum_nr_running == leader_nr_running &&
2390 first_cpu(group->cpumask) >
2391 first_cpu(group_leader->cpumask))) {
2392 group_leader = group;
2393 leader_nr_running = sum_nr_running;
2394 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07002395 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002396group_next:
2397#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 group = group->next;
2399 } while (group != sd->groups);
2400
Peter Williams2dd73a42006-06-27 02:54:34 -07002401 if (!busiest || this_load >= max_load || busiest_nr_running == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 goto out_balanced;
2403
2404 avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
2405
2406 if (this_load >= avg_load ||
2407 100*max_load <= sd->imbalance_pct*this_load)
2408 goto out_balanced;
2409
Peter Williams2dd73a42006-06-27 02:54:34 -07002410 busiest_load_per_task /= busiest_nr_running;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 /*
2412 * We're trying to get all the cpus to the average_load, so we don't
2413 * want to push ourselves above the average load, nor do we wish to
2414 * reduce the max loaded cpu below the average load, as either of these
2415 * actions would just result in more rebalancing later, and ping-pong
2416 * tasks around. Thus we look for the minimum possible imbalance.
2417 * Negative imbalances (*we* are more loaded than anyone else) will
2418 * be counted as no imbalance for these purposes -- we can't fix that
2419 * by pulling tasks to us. Be careful of negative numbers as they'll
2420 * appear as very large values with unsigned longs.
2421 */
Peter Williams2dd73a42006-06-27 02:54:34 -07002422 if (max_load <= busiest_load_per_task)
2423 goto out_balanced;
2424
2425 /*
2426 * In the presence of smp nice balancing, certain scenarios can have
2427 * max load less than avg load(as we skip the groups at or below
2428 * its cpu_power, while calculating max_load..)
2429 */
2430 if (max_load < avg_load) {
2431 *imbalance = 0;
2432 goto small_imbalance;
2433 }
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002434
2435 /* Don't want to pull so many tasks that a group would go idle */
Peter Williams2dd73a42006-06-27 02:54:34 -07002436 max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
Siddha, Suresh B0c117f12005-09-10 00:26:21 -07002437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 /* How much load to actually move to equalise the imbalance */
Eric Dumazet5517d862007-05-08 00:32:57 -07002439 *imbalance = min(max_pull * busiest->__cpu_power,
2440 (avg_load - this_load) * this->__cpu_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 / SCHED_LOAD_SCALE;
2442
Peter Williams2dd73a42006-06-27 02:54:34 -07002443 /*
2444 * if *imbalance is less than the average load per runnable task
2445 * there is no gaurantee that any tasks will be moved so we'll have
2446 * a think about bumping its value to force at least one task to be
2447 * moved
2448 */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02002449 if (*imbalance < busiest_load_per_task) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07002450 unsigned long tmp, pwr_now, pwr_move;
Peter Williams2dd73a42006-06-27 02:54:34 -07002451 unsigned int imbn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Peter Williams2dd73a42006-06-27 02:54:34 -07002453small_imbalance:
2454 pwr_move = pwr_now = 0;
2455 imbn = 2;
2456 if (this_nr_running) {
2457 this_load_per_task /= this_nr_running;
2458 if (busiest_load_per_task > this_load_per_task)
2459 imbn = 1;
2460 } else
2461 this_load_per_task = SCHED_LOAD_SCALE;
2462
Ingo Molnardd41f592007-07-09 18:51:59 +02002463 if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
2464 busiest_load_per_task * imbn) {
Peter Williams2dd73a42006-06-27 02:54:34 -07002465 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 return busiest;
2467 }
2468
2469 /*
2470 * OK, we don't have enough imbalance to justify moving tasks,
2471 * however we may be able to increase total CPU power used by
2472 * moving them.
2473 */
2474
Eric Dumazet5517d862007-05-08 00:32:57 -07002475 pwr_now += busiest->__cpu_power *
2476 min(busiest_load_per_task, max_load);
2477 pwr_now += this->__cpu_power *
2478 min(this_load_per_task, this_load);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 pwr_now /= SCHED_LOAD_SCALE;
2480
2481 /* Amount of load we'd subtract */
Eric Dumazet5517d862007-05-08 00:32:57 -07002482 tmp = sg_div_cpu_power(busiest,
2483 busiest_load_per_task * SCHED_LOAD_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 if (max_load > tmp)
Eric Dumazet5517d862007-05-08 00:32:57 -07002485 pwr_move += busiest->__cpu_power *
Peter Williams2dd73a42006-06-27 02:54:34 -07002486 min(busiest_load_per_task, max_load - tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
2488 /* Amount of load we'd add */
Eric Dumazet5517d862007-05-08 00:32:57 -07002489 if (max_load * busiest->__cpu_power <
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08002490 busiest_load_per_task * SCHED_LOAD_SCALE)
Eric Dumazet5517d862007-05-08 00:32:57 -07002491 tmp = sg_div_cpu_power(this,
2492 max_load * busiest->__cpu_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 else
Eric Dumazet5517d862007-05-08 00:32:57 -07002494 tmp = sg_div_cpu_power(this,
2495 busiest_load_per_task * SCHED_LOAD_SCALE);
2496 pwr_move += this->__cpu_power *
2497 min(this_load_per_task, this_load + tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 pwr_move /= SCHED_LOAD_SCALE;
2499
2500 /* Move if we gain throughput */
Suresh Siddha7fd0d2d2007-09-05 14:32:48 +02002501 if (pwr_move > pwr_now)
2502 *imbalance = busiest_load_per_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 }
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 return busiest;
2506
2507out_balanced:
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002508#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002509 if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002510 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002512 if (this == group_leader && group_leader != group_min) {
2513 *imbalance = min_load_per_task;
2514 return group_min;
2515 }
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07002516#endif
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002517ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 *imbalance = 0;
2519 return NULL;
2520}
2521
2522/*
2523 * find_busiest_queue - find the busiest runqueue among the cpus in group.
2524 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002525static struct rq *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002526find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002527 unsigned long imbalance, cpumask_t *cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
Ingo Molnar70b97a72006-07-03 00:25:42 -07002529 struct rq *busiest = NULL, *rq;
Peter Williams2dd73a42006-06-27 02:54:34 -07002530 unsigned long max_load = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 int i;
2532
2533 for_each_cpu_mask(i, group->cpumask) {
Ingo Molnardd41f592007-07-09 18:51:59 +02002534 unsigned long wl;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002535
2536 if (!cpu_isset(i, *cpus))
2537 continue;
2538
Ingo Molnar48f24c42006-07-03 00:25:40 -07002539 rq = cpu_rq(i);
Ingo Molnardd41f592007-07-09 18:51:59 +02002540 wl = weighted_cpuload(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Ingo Molnardd41f592007-07-09 18:51:59 +02002542 if (rq->nr_running == 1 && wl > imbalance)
Peter Williams2dd73a42006-06-27 02:54:34 -07002543 continue;
2544
Ingo Molnardd41f592007-07-09 18:51:59 +02002545 if (wl > max_load) {
2546 max_load = wl;
Ingo Molnar48f24c42006-07-03 00:25:40 -07002547 busiest = rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 }
2549 }
2550
2551 return busiest;
2552}
2553
2554/*
Nick Piggin77391d72005-06-25 14:57:30 -07002555 * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
2556 * so long as it is large enough.
2557 */
2558#define MAX_PINNED_INTERVAL 512
2559
2560/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 * Check this_cpu to ensure it is balanced within domain. Attempt to move
2562 * tasks if there is an imbalance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002564static int load_balance(int this_cpu, struct rq *this_rq,
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002565 struct sched_domain *sd, enum cpu_idle_type idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002566 int *balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
Peter Williams43010652007-08-09 11:16:46 +02002568 int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 struct sched_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 unsigned long imbalance;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002571 struct rq *busiest;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002572 cpumask_t cpus = CPU_MASK_ALL;
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002573 unsigned long flags;
Nick Piggin5969fe02005-09-10 00:26:19 -07002574
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002575 /*
2576 * When power savings policy is enabled for the parent domain, idle
2577 * sibling can pick up load irrespective of busy siblings. In this case,
Ingo Molnardd41f592007-07-09 18:51:59 +02002578 * let the state of idle sibling percolate up as CPU_IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002579 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002580 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002581 if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002582 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002583 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 schedstat_inc(sd, lb_cnt[idle]);
2586
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002587redo:
2588 group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002589 &cpus, balance);
2590
Chen, Kenneth W06066712006-12-10 02:20:35 -08002591 if (*balance == 0)
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002592 goto out_balanced;
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002593
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 if (!group) {
2595 schedstat_inc(sd, lb_nobusyg[idle]);
2596 goto out_balanced;
2597 }
2598
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002599 busiest = find_busiest_queue(group, idle, imbalance, &cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 if (!busiest) {
2601 schedstat_inc(sd, lb_nobusyq[idle]);
2602 goto out_balanced;
2603 }
2604
Nick Piggindb935db2005-06-25 14:57:11 -07002605 BUG_ON(busiest == this_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
2607 schedstat_add(sd, lb_imbalance[idle], imbalance);
2608
Peter Williams43010652007-08-09 11:16:46 +02002609 ld_moved = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 if (busiest->nr_running > 1) {
2611 /*
2612 * Attempt to move tasks. If find_busiest_group has found
2613 * an imbalance but busiest->nr_running <= 1, the group is
Peter Williams43010652007-08-09 11:16:46 +02002614 * still unbalanced. ld_moved simply stays zero, so it is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 * correctly treated as an imbalance.
2616 */
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002617 local_irq_save(flags);
Nick Piggine17224b2005-09-10 00:26:18 -07002618 double_rq_lock(this_rq, busiest);
Peter Williams43010652007-08-09 11:16:46 +02002619 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Ingo Molnar48f24c42006-07-03 00:25:40 -07002620 imbalance, sd, idle, &all_pinned);
Nick Piggine17224b2005-09-10 00:26:18 -07002621 double_rq_unlock(this_rq, busiest);
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002622 local_irq_restore(flags);
Nick Piggin81026792005-06-25 14:57:07 -07002623
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002624 /*
2625 * some other cpu did the load balance for us.
2626 */
Peter Williams43010652007-08-09 11:16:46 +02002627 if (ld_moved && this_cpu != smp_processor_id())
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002628 resched_cpu(this_cpu);
2629
Nick Piggin81026792005-06-25 14:57:07 -07002630 /* All tasks on this runqueue were pinned by CPU affinity */
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002631 if (unlikely(all_pinned)) {
2632 cpu_clear(cpu_of(busiest), cpus);
2633 if (!cpus_empty(cpus))
2634 goto redo;
Nick Piggin81026792005-06-25 14:57:07 -07002635 goto out_balanced;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 }
Nick Piggin81026792005-06-25 14:57:07 -07002638
Peter Williams43010652007-08-09 11:16:46 +02002639 if (!ld_moved) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 schedstat_inc(sd, lb_failed[idle]);
2641 sd->nr_balance_failed++;
2642
2643 if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002645 spin_lock_irqsave(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07002646
2647 /* don't kick the migration_thread, if the curr
2648 * task on busiest cpu can't be moved to this_cpu
2649 */
2650 if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002651 spin_unlock_irqrestore(&busiest->lock, flags);
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07002652 all_pinned = 1;
2653 goto out_one_pinned;
2654 }
2655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 if (!busiest->active_balance) {
2657 busiest->active_balance = 1;
2658 busiest->push_cpu = this_cpu;
Nick Piggin81026792005-06-25 14:57:07 -07002659 active_balance = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 }
Christoph Lameterfe2eea32006-12-10 02:20:21 -08002661 spin_unlock_irqrestore(&busiest->lock, flags);
Nick Piggin81026792005-06-25 14:57:07 -07002662 if (active_balance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 wake_up_process(busiest->migration_thread);
2664
2665 /*
2666 * We've kicked active balancing, reset the failure
2667 * counter.
2668 */
Nick Piggin39507452005-06-25 14:57:09 -07002669 sd->nr_balance_failed = sd->cache_nice_tries+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 }
Nick Piggin81026792005-06-25 14:57:07 -07002671 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 sd->nr_balance_failed = 0;
2673
Nick Piggin81026792005-06-25 14:57:07 -07002674 if (likely(!active_balance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 /* We were unbalanced, so reset the balancing interval */
2676 sd->balance_interval = sd->min_interval;
Nick Piggin81026792005-06-25 14:57:07 -07002677 } else {
2678 /*
2679 * If we've begun active balancing, start to back off. This
2680 * case may not be covered by the all_pinned logic if there
2681 * is only 1 task on the busy runqueue (because we don't call
2682 * move_tasks).
2683 */
2684 if (sd->balance_interval < sd->max_interval)
2685 sd->balance_interval *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 }
2687
Peter Williams43010652007-08-09 11:16:46 +02002688 if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002689 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002690 return -1;
Peter Williams43010652007-08-09 11:16:46 +02002691 return ld_moved;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
2693out_balanced:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 schedstat_inc(sd, lb_balanced[idle]);
2695
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002696 sd->nr_balance_failed = 0;
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07002697
2698out_one_pinned:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 /* tune up the balancing interval */
Nick Piggin77391d72005-06-25 14:57:30 -07002700 if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
2701 (sd->balance_interval < sd->max_interval))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 sd->balance_interval *= 2;
2703
Ingo Molnar48f24c42006-07-03 00:25:40 -07002704 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002705 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002706 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 return 0;
2708}
2709
2710/*
2711 * Check this_cpu to ensure it is balanced within domain. Attempt to move
2712 * tasks if there is an imbalance.
2713 *
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002714 * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 * this_rq is locked.
2716 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07002717static int
Ingo Molnar70b97a72006-07-03 00:25:42 -07002718load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
2720 struct sched_group *group;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002721 struct rq *busiest = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 unsigned long imbalance;
Peter Williams43010652007-08-09 11:16:46 +02002723 int ld_moved = 0;
Nick Piggin5969fe02005-09-10 00:26:19 -07002724 int sd_idle = 0;
Suresh Siddha969bb4e2007-07-19 21:28:35 +02002725 int all_pinned = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002726 cpumask_t cpus = CPU_MASK_ALL;
Nick Piggin5969fe02005-09-10 00:26:19 -07002727
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002728 /*
2729 * When power savings policy is enabled for the parent domain, idle
2730 * sibling can pick up load irrespective of busy siblings. In this case,
2731 * let the state of idle sibling percolate up as IDLE, instead of
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002732 * portraying it as CPU_NOT_IDLE.
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002733 */
2734 if (sd->flags & SD_SHARE_CPUPOWER &&
2735 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002736 sd_idle = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002738 schedstat_inc(sd, lb_cnt[CPU_NEWLY_IDLE]);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002739redo:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002740 group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
Siddha, Suresh B783609c2006-12-10 02:20:33 -08002741 &sd_idle, &cpus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 if (!group) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002743 schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002744 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 }
2746
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002747 busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance,
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002748 &cpus);
Nick Piggindb935db2005-06-25 14:57:11 -07002749 if (!busiest) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002750 schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002751 goto out_balanced;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
2753
Nick Piggindb935db2005-06-25 14:57:11 -07002754 BUG_ON(busiest == this_rq);
2755
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002756 schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07002757
Peter Williams43010652007-08-09 11:16:46 +02002758 ld_moved = 0;
Nick Piggind6d5cfa2005-09-10 00:26:16 -07002759 if (busiest->nr_running > 1) {
2760 /* Attempt to move tasks */
2761 double_lock_balance(this_rq, busiest);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002762 /* this_rq->clock is already updated */
2763 update_rq_clock(busiest);
Peter Williams43010652007-08-09 11:16:46 +02002764 ld_moved = move_tasks(this_rq, this_cpu, busiest,
Suresh Siddha969bb4e2007-07-19 21:28:35 +02002765 imbalance, sd, CPU_NEWLY_IDLE,
2766 &all_pinned);
Nick Piggind6d5cfa2005-09-10 00:26:16 -07002767 spin_unlock(&busiest->lock);
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002768
Suresh Siddha969bb4e2007-07-19 21:28:35 +02002769 if (unlikely(all_pinned)) {
Christoph Lameter0a2966b2006-09-25 23:30:51 -07002770 cpu_clear(cpu_of(busiest), cpus);
2771 if (!cpus_empty(cpus))
2772 goto redo;
2773 }
Nick Piggind6d5cfa2005-09-10 00:26:16 -07002774 }
2775
Peter Williams43010652007-08-09 11:16:46 +02002776 if (!ld_moved) {
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002777 schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002778 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
2779 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002780 return -1;
2781 } else
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002782 sd->nr_balance_failed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Peter Williams43010652007-08-09 11:16:46 +02002784 return ld_moved;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002785
2786out_balanced:
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002787 schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
Ingo Molnar48f24c42006-07-03 00:25:40 -07002788 if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
Siddha, Suresh B89c47102006-10-03 01:14:09 -07002789 !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
Nick Piggin5969fe02005-09-10 00:26:19 -07002790 return -1;
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002791 sd->nr_balance_failed = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07002792
Nick Piggin16cfb1c2005-06-25 14:57:08 -07002793 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794}
2795
2796/*
2797 * idle_balance is called by schedule() if this_cpu is about to become
2798 * idle. Attempts to pull tasks from other CPUs.
2799 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002800static void idle_balance(int this_cpu, struct rq *this_rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801{
2802 struct sched_domain *sd;
Ingo Molnardd41f592007-07-09 18:51:59 +02002803 int pulled_task = -1;
2804 unsigned long next_balance = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
2806 for_each_domain(this_cpu, sd) {
Christoph Lameter92c4ca52007-06-23 17:16:33 -07002807 unsigned long interval;
2808
2809 if (!(sd->flags & SD_LOAD_BALANCE))
2810 continue;
2811
2812 if (sd->flags & SD_BALANCE_NEWIDLE)
Ingo Molnar48f24c42006-07-03 00:25:40 -07002813 /* If we've pulled tasks over stop searching: */
Christoph Lameter1bd77f22006-12-10 02:20:27 -08002814 pulled_task = load_balance_newidle(this_cpu,
Christoph Lameter92c4ca52007-06-23 17:16:33 -07002815 this_rq, sd);
2816
2817 interval = msecs_to_jiffies(sd->balance_interval);
2818 if (time_after(next_balance, sd->last_balance + interval))
2819 next_balance = sd->last_balance + interval;
2820 if (pulled_task)
2821 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002823 if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
Christoph Lameter1bd77f22006-12-10 02:20:27 -08002824 /*
2825 * We are going idle. next_balance may be set based on
2826 * a busy processor. So reset next_balance.
2827 */
2828 this_rq->next_balance = next_balance;
Ingo Molnardd41f592007-07-09 18:51:59 +02002829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830}
2831
2832/*
2833 * active_load_balance is run by migration threads. It pushes running tasks
2834 * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
2835 * running on each physical CPU where possible, and avoids physical /
2836 * logical imbalances.
2837 *
2838 * Called with busiest_rq locked.
2839 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07002840static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
Nick Piggin39507452005-06-25 14:57:09 -07002842 int target_cpu = busiest_rq->push_cpu;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002843 struct sched_domain *sd;
2844 struct rq *target_rq;
Nick Piggin39507452005-06-25 14:57:09 -07002845
Ingo Molnar48f24c42006-07-03 00:25:40 -07002846 /* Is there any task to move? */
Nick Piggin39507452005-06-25 14:57:09 -07002847 if (busiest_rq->nr_running <= 1)
Nick Piggin39507452005-06-25 14:57:09 -07002848 return;
2849
2850 target_rq = cpu_rq(target_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
2852 /*
Nick Piggin39507452005-06-25 14:57:09 -07002853 * This condition is "impossible", if it occurs
2854 * we need to fix it. Originally reported by
2855 * Bjorn Helgaas on a 128-cpu setup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 */
Nick Piggin39507452005-06-25 14:57:09 -07002857 BUG_ON(busiest_rq == target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
Nick Piggin39507452005-06-25 14:57:09 -07002859 /* move a task from busiest_rq to target_rq */
2860 double_lock_balance(busiest_rq, target_rq);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02002861 update_rq_clock(busiest_rq);
2862 update_rq_clock(target_rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Nick Piggin39507452005-06-25 14:57:09 -07002864 /* Search for an sd spanning us and the target CPU. */
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002865 for_each_domain(target_cpu, sd) {
Nick Piggin39507452005-06-25 14:57:09 -07002866 if ((sd->flags & SD_LOAD_BALANCE) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07002867 cpu_isset(busiest_cpu, sd->span))
Nick Piggin39507452005-06-25 14:57:09 -07002868 break;
Chen, Kenneth Wc96d1452006-06-27 02:54:28 -07002869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
Ingo Molnar48f24c42006-07-03 00:25:40 -07002871 if (likely(sd)) {
2872 schedstat_inc(sd, alb_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
Peter Williams43010652007-08-09 11:16:46 +02002874 if (move_one_task(target_rq, target_cpu, busiest_rq,
2875 sd, CPU_IDLE))
Ingo Molnar48f24c42006-07-03 00:25:40 -07002876 schedstat_inc(sd, alb_pushed);
2877 else
2878 schedstat_inc(sd, alb_failed);
2879 }
Nick Piggin39507452005-06-25 14:57:09 -07002880 spin_unlock(&target_rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881}
2882
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002883#ifdef CONFIG_NO_HZ
2884static struct {
2885 atomic_t load_balancer;
2886 cpumask_t cpu_mask;
2887} nohz ____cacheline_aligned = {
2888 .load_balancer = ATOMIC_INIT(-1),
2889 .cpu_mask = CPU_MASK_NONE,
2890};
2891
Christoph Lameter7835b982006-12-10 02:20:22 -08002892/*
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002893 * This routine will try to nominate the ilb (idle load balancing)
2894 * owner among the cpus whose ticks are stopped. ilb owner will do the idle
2895 * load balancing on behalf of all those cpus. If all the cpus in the system
2896 * go into this tickless mode, then there will be no ilb owner (as there is
2897 * no need for one) and all the cpus will sleep till the next wakeup event
2898 * arrives...
Christoph Lameter7835b982006-12-10 02:20:22 -08002899 *
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002900 * For the ilb owner, tick is not stopped. And this tick will be used
2901 * for idle load balancing. ilb owner will still be part of
2902 * nohz.cpu_mask..
2903 *
2904 * While stopping the tick, this cpu will become the ilb owner if there
2905 * is no other owner. And will be the owner till that cpu becomes busy
2906 * or if all cpus in the system stop their ticks at which point
2907 * there is no need for ilb owner.
2908 *
2909 * When the ilb owner becomes busy, it nominates another owner, during the
2910 * next busy scheduler_tick()
2911 */
2912int select_nohz_load_balancer(int stop_tick)
2913{
2914 int cpu = smp_processor_id();
2915
2916 if (stop_tick) {
2917 cpu_set(cpu, nohz.cpu_mask);
2918 cpu_rq(cpu)->in_nohz_recently = 1;
2919
2920 /*
2921 * If we are going offline and still the leader, give up!
2922 */
2923 if (cpu_is_offline(cpu) &&
2924 atomic_read(&nohz.load_balancer) == cpu) {
2925 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
2926 BUG();
2927 return 0;
2928 }
2929
2930 /* time for ilb owner also to sleep */
2931 if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
2932 if (atomic_read(&nohz.load_balancer) == cpu)
2933 atomic_set(&nohz.load_balancer, -1);
2934 return 0;
2935 }
2936
2937 if (atomic_read(&nohz.load_balancer) == -1) {
2938 /* make me the ilb owner */
2939 if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
2940 return 1;
2941 } else if (atomic_read(&nohz.load_balancer) == cpu)
2942 return 1;
2943 } else {
2944 if (!cpu_isset(cpu, nohz.cpu_mask))
2945 return 0;
2946
2947 cpu_clear(cpu, nohz.cpu_mask);
2948
2949 if (atomic_read(&nohz.load_balancer) == cpu)
2950 if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
2951 BUG();
2952 }
2953 return 0;
2954}
2955#endif
2956
2957static DEFINE_SPINLOCK(balancing);
2958
2959/*
Christoph Lameter7835b982006-12-10 02:20:22 -08002960 * It checks each scheduling domain to see if it is due to be balanced,
2961 * and initiates a balancing operation if so.
2962 *
2963 * Balancing parameters are set up in arch_init_sched_domains.
2964 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002965static inline void rebalance_domains(int cpu, enum cpu_idle_type idle)
Christoph Lameter7835b982006-12-10 02:20:22 -08002966{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002967 int balance = 1;
2968 struct rq *rq = cpu_rq(cpu);
Christoph Lameter7835b982006-12-10 02:20:22 -08002969 unsigned long interval;
2970 struct sched_domain *sd;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002971 /* Earliest time when we have to do rebalance again */
Christoph Lameterc9819f42006-12-10 02:20:25 -08002972 unsigned long next_balance = jiffies + 60*HZ;
Suresh Siddhaf549da82007-08-23 15:18:02 +02002973 int update_next_balance = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002975 for_each_domain(cpu, sd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 if (!(sd->flags & SD_LOAD_BALANCE))
2977 continue;
2978
2979 interval = sd->balance_interval;
Ingo Molnard15bcfd2007-07-09 18:51:57 +02002980 if (idle != CPU_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 interval *= sd->busy_factor;
2982
2983 /* scale ms to jiffies */
2984 interval = msecs_to_jiffies(interval);
2985 if (unlikely(!interval))
2986 interval = 1;
Ingo Molnardd41f592007-07-09 18:51:59 +02002987 if (interval > HZ*NR_CPUS/10)
2988 interval = HZ*NR_CPUS/10;
2989
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Christoph Lameter08c183f2006-12-10 02:20:29 -08002991 if (sd->flags & SD_SERIALIZE) {
2992 if (!spin_trylock(&balancing))
2993 goto out;
2994 }
2995
Christoph Lameterc9819f42006-12-10 02:20:25 -08002996 if (time_after_eq(jiffies, sd->last_balance + interval)) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07002997 if (load_balance(cpu, rq, sd, idle, &balance)) {
Siddha, Suresh Bfa3b6dd2005-09-10 00:26:21 -07002998 /*
2999 * We've pulled tasks over so either we're no
Nick Piggin5969fe02005-09-10 00:26:19 -07003000 * longer idle, or one of our SMT siblings is
3001 * not idle.
3002 */
Ingo Molnard15bcfd2007-07-09 18:51:57 +02003003 idle = CPU_NOT_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 }
Christoph Lameter1bd77f22006-12-10 02:20:27 -08003005 sd->last_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 }
Christoph Lameter08c183f2006-12-10 02:20:29 -08003007 if (sd->flags & SD_SERIALIZE)
3008 spin_unlock(&balancing);
3009out:
Suresh Siddhaf549da82007-08-23 15:18:02 +02003010 if (time_after(next_balance, sd->last_balance + interval)) {
Christoph Lameterc9819f42006-12-10 02:20:25 -08003011 next_balance = sd->last_balance + interval;
Suresh Siddhaf549da82007-08-23 15:18:02 +02003012 update_next_balance = 1;
3013 }
Siddha, Suresh B783609c2006-12-10 02:20:33 -08003014
3015 /*
3016 * Stop the load balance at this level. There is another
3017 * CPU in our sched group which is doing load balancing more
3018 * actively.
3019 */
3020 if (!balance)
3021 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 }
Suresh Siddhaf549da82007-08-23 15:18:02 +02003023
3024 /*
3025 * next_balance will be updated only when there is a need.
3026 * When the cpu is attached to null domain for ex, it will not be
3027 * updated.
3028 */
3029 if (likely(update_next_balance))
3030 rq->next_balance = next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003031}
3032
3033/*
3034 * run_rebalance_domains is triggered when needed from the scheduler tick.
3035 * In CONFIG_NO_HZ case, the idle load balance owner will do the
3036 * rebalancing for all the cpus for whom scheduler ticks are stopped.
3037 */
3038static void run_rebalance_domains(struct softirq_action *h)
3039{
Ingo Molnardd41f592007-07-09 18:51:59 +02003040 int this_cpu = smp_processor_id();
3041 struct rq *this_rq = cpu_rq(this_cpu);
3042 enum cpu_idle_type idle = this_rq->idle_at_tick ?
3043 CPU_IDLE : CPU_NOT_IDLE;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003044
Ingo Molnardd41f592007-07-09 18:51:59 +02003045 rebalance_domains(this_cpu, idle);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003046
3047#ifdef CONFIG_NO_HZ
3048 /*
3049 * If this cpu is the owner for idle load balancing, then do the
3050 * balancing on behalf of the other idle cpus whose ticks are
3051 * stopped.
3052 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003053 if (this_rq->idle_at_tick &&
3054 atomic_read(&nohz.load_balancer) == this_cpu) {
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003055 cpumask_t cpus = nohz.cpu_mask;
3056 struct rq *rq;
3057 int balance_cpu;
3058
Ingo Molnardd41f592007-07-09 18:51:59 +02003059 cpu_clear(this_cpu, cpus);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003060 for_each_cpu_mask(balance_cpu, cpus) {
3061 /*
3062 * If this cpu gets work to do, stop the load balancing
3063 * work being done for other cpus. Next load
3064 * balancing owner will pick it up.
3065 */
3066 if (need_resched())
3067 break;
3068
Oleg Nesterovde0cf892007-08-12 18:08:19 +02003069 rebalance_domains(balance_cpu, CPU_IDLE);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003070
3071 rq = cpu_rq(balance_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003072 if (time_after(this_rq->next_balance, rq->next_balance))
3073 this_rq->next_balance = rq->next_balance;
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003074 }
3075 }
3076#endif
3077}
3078
3079/*
3080 * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
3081 *
3082 * In case of CONFIG_NO_HZ, this is the place where we nominate a new
3083 * idle load balancing owner or decide to stop the periodic load balancing,
3084 * if the whole system is idle.
3085 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003086static inline void trigger_load_balance(struct rq *rq, int cpu)
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003087{
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -07003088#ifdef CONFIG_NO_HZ
3089 /*
3090 * If we were in the nohz mode recently and busy at the current
3091 * scheduler tick, then check if we need to nominate new idle
3092 * load balancer.
3093 */
3094 if (rq->in_nohz_recently && !rq->idle_at_tick) {
3095 rq->in_nohz_recently = 0;
3096
3097 if (atomic_read(&nohz.load_balancer) == cpu) {
3098 cpu_clear(cpu, nohz.cpu_mask);
3099 atomic_set(&nohz.load_balancer, -1);
3100 }
3101
3102 if (atomic_read(&nohz.load_balancer) == -1) {
3103 /*
3104 * simple selection for now: Nominate the
3105 * first cpu in the nohz list to be the next
3106 * ilb owner.
3107 *
3108 * TBD: Traverse the sched domains and nominate
3109 * the nearest cpu in the nohz.cpu_mask.
3110 */
3111 int ilb = first_cpu(nohz.cpu_mask);
3112
3113 if (ilb != NR_CPUS)
3114 resched_cpu(ilb);
3115 }
3116 }
3117
3118 /*
3119 * If this cpu is idle and doing idle load balancing for all the
3120 * cpus with ticks stopped, is it time for that to stop?
3121 */
3122 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
3123 cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
3124 resched_cpu(cpu);
3125 return;
3126 }
3127
3128 /*
3129 * If this cpu is idle and the idle load balancing is done by
3130 * someone else, then no need raise the SCHED_SOFTIRQ
3131 */
3132 if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
3133 cpu_isset(cpu, nohz.cpu_mask))
3134 return;
3135#endif
3136 if (time_after_eq(jiffies, rq->next_balance))
3137 raise_softirq(SCHED_SOFTIRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138}
Ingo Molnardd41f592007-07-09 18:51:59 +02003139
3140#else /* CONFIG_SMP */
3141
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142/*
3143 * on UP we do not need to balance between CPUs:
3144 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07003145static inline void idle_balance(int cpu, struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146{
3147}
Ingo Molnardd41f592007-07-09 18:51:59 +02003148
3149/* Avoid "used but not defined" warning on UP */
3150static int balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
3151 unsigned long max_nr_move, unsigned long max_load_move,
3152 struct sched_domain *sd, enum cpu_idle_type idle,
3153 int *all_pinned, unsigned long *load_moved,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02003154 int *this_best_prio, struct rq_iterator *iterator)
Ingo Molnardd41f592007-07-09 18:51:59 +02003155{
3156 *load_moved = 0;
3157
3158 return 0;
3159}
3160
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161#endif
3162
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163DEFINE_PER_CPU(struct kernel_stat, kstat);
3164
3165EXPORT_PER_CPU_SYMBOL(kstat);
3166
3167/*
Ingo Molnar41b86e92007-07-09 18:51:58 +02003168 * Return p->sum_exec_runtime plus any more ns on the sched_clock
3169 * that have not yet been banked in case the task is currently running.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 */
Ingo Molnar41b86e92007-07-09 18:51:58 +02003171unsigned long long task_sched_runtime(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 unsigned long flags;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003174 u64 ns, delta_exec;
3175 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003176
Ingo Molnar41b86e92007-07-09 18:51:58 +02003177 rq = task_rq_lock(p, &flags);
3178 ns = p->se.sum_exec_runtime;
3179 if (rq->curr == p) {
Ingo Molnara8e504d2007-08-09 11:16:47 +02003180 update_rq_clock(rq);
3181 delta_exec = rq->clock - p->se.exec_start;
Ingo Molnar41b86e92007-07-09 18:51:58 +02003182 if ((s64)delta_exec > 0)
3183 ns += delta_exec;
3184 }
3185 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07003186
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 return ns;
3188}
3189
3190/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 * Account user cpu time to a process.
3192 * @p: the process that the cpu time gets accounted to
3193 * @hardirq_offset: the offset to subtract from hardirq_count()
3194 * @cputime: the cpu time spent in user space since the last update
3195 */
3196void account_user_time(struct task_struct *p, cputime_t cputime)
3197{
3198 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3199 cputime64_t tmp;
3200
3201 p->utime = cputime_add(p->utime, cputime);
3202
3203 /* Add user time to cpustat. */
3204 tmp = cputime_to_cputime64(cputime);
3205 if (TASK_NICE(p) > 0)
3206 cpustat->nice = cputime64_add(cpustat->nice, tmp);
3207 else
3208 cpustat->user = cputime64_add(cpustat->user, tmp);
3209}
3210
3211/*
3212 * Account system cpu time to a process.
3213 * @p: the process that the cpu time gets accounted to
3214 * @hardirq_offset: the offset to subtract from hardirq_count()
3215 * @cputime: the cpu time spent in kernel space since the last update
3216 */
3217void account_system_time(struct task_struct *p, int hardirq_offset,
3218 cputime_t cputime)
3219{
3220 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003221 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 cputime64_t tmp;
3223
3224 p->stime = cputime_add(p->stime, cputime);
3225
3226 /* Add system time to cpustat. */
3227 tmp = cputime_to_cputime64(cputime);
3228 if (hardirq_count() - hardirq_offset)
3229 cpustat->irq = cputime64_add(cpustat->irq, tmp);
3230 else if (softirq_count())
3231 cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
3232 else if (p != rq->idle)
3233 cpustat->system = cputime64_add(cpustat->system, tmp);
3234 else if (atomic_read(&rq->nr_iowait) > 0)
3235 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3236 else
3237 cpustat->idle = cputime64_add(cpustat->idle, tmp);
3238 /* Account for system time used */
3239 acct_update_integrals(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240}
3241
3242/*
3243 * Account for involuntary wait time.
3244 * @p: the process from which the cpu time has been stolen
3245 * @steal: the cpu time spent in involuntary wait
3246 */
3247void account_steal_time(struct task_struct *p, cputime_t steal)
3248{
3249 struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
3250 cputime64_t tmp = cputime_to_cputime64(steal);
Ingo Molnar70b97a72006-07-03 00:25:42 -07003251 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
3253 if (p == rq->idle) {
3254 p->stime = cputime_add(p->stime, steal);
3255 if (atomic_read(&rq->nr_iowait) > 0)
3256 cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
3257 else
3258 cpustat->idle = cputime64_add(cpustat->idle, tmp);
3259 } else
3260 cpustat->steal = cputime64_add(cpustat->steal, tmp);
3261}
3262
Christoph Lameter7835b982006-12-10 02:20:22 -08003263/*
3264 * This function gets called by the timer code, with HZ frequency.
3265 * We call it with interrupts disabled.
3266 *
3267 * It also gets called by the fork code, when changing the parent's
3268 * timeslices.
3269 */
3270void scheduler_tick(void)
3271{
Christoph Lameter7835b982006-12-10 02:20:22 -08003272 int cpu = smp_processor_id();
3273 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003274 struct task_struct *curr = rq->curr;
Ingo Molnar529c7722007-08-10 23:05:11 +02003275 u64 next_tick = rq->tick_timestamp + TICK_NSEC;
Christoph Lameter7835b982006-12-10 02:20:22 -08003276
Ingo Molnardd41f592007-07-09 18:51:59 +02003277 spin_lock(&rq->lock);
Ingo Molnar546fe3c2007-08-09 11:16:51 +02003278 __update_rq_clock(rq);
Ingo Molnar529c7722007-08-10 23:05:11 +02003279 /*
3280 * Let rq->clock advance by at least TICK_NSEC:
3281 */
3282 if (unlikely(rq->clock < next_tick))
3283 rq->clock = next_tick;
3284 rq->tick_timestamp = rq->clock;
Ingo Molnarf1a438d2007-08-09 11:16:45 +02003285 update_cpu_load(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003286 if (curr != rq->idle) /* FIXME: needed? */
3287 curr->sched_class->task_tick(rq, curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02003288 spin_unlock(&rq->lock);
3289
Christoph Lametere418e1c2006-12-10 02:20:23 -08003290#ifdef CONFIG_SMP
Ingo Molnardd41f592007-07-09 18:51:59 +02003291 rq->idle_at_tick = idle_cpu(cpu);
3292 trigger_load_balance(rq, cpu);
Christoph Lametere418e1c2006-12-10 02:20:23 -08003293#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294}
3295
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296#if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
3297
3298void fastcall add_preempt_count(int val)
3299{
3300 /*
3301 * Underflow?
3302 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003303 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
3304 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 preempt_count() += val;
3306 /*
3307 * Spinlock count overflowing soon?
3308 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003309 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
3310 PREEMPT_MASK - 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311}
3312EXPORT_SYMBOL(add_preempt_count);
3313
3314void fastcall sub_preempt_count(int val)
3315{
3316 /*
3317 * Underflow?
3318 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003319 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
3320 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 /*
3322 * Is the spinlock portion underflowing?
3323 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003324 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
3325 !(preempt_count() & PREEMPT_MASK)))
3326 return;
3327
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 preempt_count() -= val;
3329}
3330EXPORT_SYMBOL(sub_preempt_count);
3331
3332#endif
3333
3334/*
Ingo Molnardd41f592007-07-09 18:51:59 +02003335 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003337static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338{
Ingo Molnardd41f592007-07-09 18:51:59 +02003339 printk(KERN_ERR "BUG: scheduling while atomic: %s/0x%08x/%d\n",
3340 prev->comm, preempt_count(), prev->pid);
3341 debug_show_held_locks(prev);
3342 if (irqs_disabled())
3343 print_irqtrace_events(prev);
3344 dump_stack();
3345}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Ingo Molnardd41f592007-07-09 18:51:59 +02003347/*
3348 * Various schedule()-time debugging checks and statistics:
3349 */
3350static inline void schedule_debug(struct task_struct *prev)
3351{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 /*
3353 * Test if we are atomic. Since do_exit() needs to call into
3354 * schedule() atomically, we ignore that path for now.
3355 * Otherwise, whine if we are scheduling when we should not be.
3356 */
Ingo Molnardd41f592007-07-09 18:51:59 +02003357 if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
3358 __schedule_bug(prev);
3359
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
3361
Ingo Molnardd41f592007-07-09 18:51:59 +02003362 schedstat_inc(this_rq(), sched_cnt);
3363}
3364
3365/*
3366 * Pick up the highest-prio task:
3367 */
3368static inline struct task_struct *
Ingo Molnarff95f3d2007-08-09 11:16:49 +02003369pick_next_task(struct rq *rq, struct task_struct *prev)
Ingo Molnardd41f592007-07-09 18:51:59 +02003370{
3371 struct sched_class *class;
3372 struct task_struct *p;
3373
3374 /*
3375 * Optimization: we know that if all tasks are in
3376 * the fair class we can call that function directly:
3377 */
3378 if (likely(rq->nr_running == rq->cfs.nr_running)) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003379 p = fair_sched_class.pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003380 if (likely(p))
3381 return p;
3382 }
3383
3384 class = sched_class_highest;
3385 for ( ; ; ) {
Ingo Molnarfb8d4722007-08-09 11:16:48 +02003386 p = class->pick_next_task(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02003387 if (p)
3388 return p;
3389 /*
3390 * Will never be NULL as the idle class always
3391 * returns a non-NULL p:
3392 */
3393 class = class->next;
3394 }
3395}
3396
3397/*
3398 * schedule() is the main scheduler function.
3399 */
3400asmlinkage void __sched schedule(void)
3401{
3402 struct task_struct *prev, *next;
3403 long *switch_count;
3404 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02003405 int cpu;
3406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407need_resched:
3408 preempt_disable();
Ingo Molnardd41f592007-07-09 18:51:59 +02003409 cpu = smp_processor_id();
3410 rq = cpu_rq(cpu);
3411 rcu_qsctr_inc(cpu);
3412 prev = rq->curr;
3413 switch_count = &prev->nivcsw;
3414
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 release_kernel_lock(prev);
3416need_resched_nonpreemptible:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417
Ingo Molnardd41f592007-07-09 18:51:59 +02003418 schedule_debug(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
3420 spin_lock_irq(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 clear_tsk_need_resched(prev);
Ingo Molnarc1b3da32007-08-09 11:16:47 +02003422 __update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
Ingo Molnardd41f592007-07-09 18:51:59 +02003424 if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
3425 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
3426 unlikely(signal_pending(prev)))) {
3427 prev->state = TASK_RUNNING;
3428 } else {
Ingo Molnar2e1cb742007-08-09 11:16:49 +02003429 deactivate_task(rq, prev, 1);
Ingo Molnardd41f592007-07-09 18:51:59 +02003430 }
3431 switch_count = &prev->nvcsw;
3432 }
3433
3434 if (unlikely(!rq->nr_running))
3435 idle_balance(cpu, rq);
3436
Ingo Molnar31ee5292007-08-09 11:16:49 +02003437 prev->sched_class->put_prev_task(rq, prev);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02003438 next = pick_next_task(rq, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
3440 sched_info_switch(prev, next);
Ingo Molnardd41f592007-07-09 18:51:59 +02003441
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 rq->nr_switches++;
3444 rq->curr = next;
3445 ++*switch_count;
3446
Ingo Molnardd41f592007-07-09 18:51:59 +02003447 context_switch(rq, prev, next); /* unlocks the rq */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 } else
3449 spin_unlock_irq(&rq->lock);
3450
Ingo Molnardd41f592007-07-09 18:51:59 +02003451 if (unlikely(reacquire_kernel_lock(current) < 0)) {
3452 cpu = smp_processor_id();
3453 rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 goto need_resched_nonpreemptible;
Ingo Molnardd41f592007-07-09 18:51:59 +02003455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 preempt_enable_no_resched();
3457 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
3458 goto need_resched;
3459}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460EXPORT_SYMBOL(schedule);
3461
3462#ifdef CONFIG_PREEMPT
3463/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003464 * this is the entry point to schedule() from in-kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 * off of preempt_enable. Kernel preemptions off return from interrupt
3466 * occur there and call schedule directly.
3467 */
3468asmlinkage void __sched preempt_schedule(void)
3469{
3470 struct thread_info *ti = current_thread_info();
3471#ifdef CONFIG_PREEMPT_BKL
3472 struct task_struct *task = current;
3473 int saved_lock_depth;
3474#endif
3475 /*
3476 * If there is a non-zero preempt_count or interrupts are disabled,
3477 * we do not want to preempt the current task. Just return..
3478 */
Nick Pigginbeed33a2006-10-11 01:21:52 -07003479 if (likely(ti->preempt_count || irqs_disabled()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 return;
3481
3482need_resched:
3483 add_preempt_count(PREEMPT_ACTIVE);
3484 /*
3485 * We keep the big kernel semaphore locked, but we
3486 * clear ->lock_depth so that schedule() doesnt
3487 * auto-release the semaphore:
3488 */
3489#ifdef CONFIG_PREEMPT_BKL
3490 saved_lock_depth = task->lock_depth;
3491 task->lock_depth = -1;
3492#endif
3493 schedule();
3494#ifdef CONFIG_PREEMPT_BKL
3495 task->lock_depth = saved_lock_depth;
3496#endif
3497 sub_preempt_count(PREEMPT_ACTIVE);
3498
3499 /* we could miss a preemption opportunity between schedule and now */
3500 barrier();
3501 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
3502 goto need_resched;
3503}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504EXPORT_SYMBOL(preempt_schedule);
3505
3506/*
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003507 * this is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 * off of irq context.
3509 * Note, that this is called and return with irqs disabled. This will
3510 * protect us against recursive calling from irq.
3511 */
3512asmlinkage void __sched preempt_schedule_irq(void)
3513{
3514 struct thread_info *ti = current_thread_info();
3515#ifdef CONFIG_PREEMPT_BKL
3516 struct task_struct *task = current;
3517 int saved_lock_depth;
3518#endif
Andreas Mohr2ed6e342006-07-10 04:43:52 -07003519 /* Catch callers which need to be fixed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 BUG_ON(ti->preempt_count || !irqs_disabled());
3521
3522need_resched:
3523 add_preempt_count(PREEMPT_ACTIVE);
3524 /*
3525 * We keep the big kernel semaphore locked, but we
3526 * clear ->lock_depth so that schedule() doesnt
3527 * auto-release the semaphore:
3528 */
3529#ifdef CONFIG_PREEMPT_BKL
3530 saved_lock_depth = task->lock_depth;
3531 task->lock_depth = -1;
3532#endif
3533 local_irq_enable();
3534 schedule();
3535 local_irq_disable();
3536#ifdef CONFIG_PREEMPT_BKL
3537 task->lock_depth = saved_lock_depth;
3538#endif
3539 sub_preempt_count(PREEMPT_ACTIVE);
3540
3541 /* we could miss a preemption opportunity between schedule and now */
3542 barrier();
3543 if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
3544 goto need_resched;
3545}
3546
3547#endif /* CONFIG_PREEMPT */
3548
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003549int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
3550 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551{
Ingo Molnar48f24c42006-07-03 00:25:40 -07003552 return try_to_wake_up(curr->private, mode, sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554EXPORT_SYMBOL(default_wake_function);
3555
3556/*
3557 * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
3558 * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
3559 * number) then we wake all the non-exclusive tasks and one exclusive task.
3560 *
3561 * There are circumstances in which we can try to wake a task which has already
3562 * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
3563 * zero in this (rare) case, and we handle it by continuing to scan the queue.
3564 */
3565static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
3566 int nr_exclusive, int sync, void *key)
3567{
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02003568 wait_queue_t *curr, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
Matthias Kaehlcke2e458742007-10-15 17:00:02 +02003570 list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07003571 unsigned flags = curr->flags;
3572
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 if (curr->func(curr, mode, sync, key) &&
Ingo Molnar48f24c42006-07-03 00:25:40 -07003574 (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 break;
3576 }
3577}
3578
3579/**
3580 * __wake_up - wake up threads blocked on a waitqueue.
3581 * @q: the waitqueue
3582 * @mode: which threads
3583 * @nr_exclusive: how many wake-one or wake-many threads to wake up
Martin Waitz67be2dd2005-05-01 08:59:26 -07003584 * @key: is directly passed to the wakeup function
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 */
3586void fastcall __wake_up(wait_queue_head_t *q, unsigned int mode,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003587 int nr_exclusive, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588{
3589 unsigned long flags;
3590
3591 spin_lock_irqsave(&q->lock, flags);
3592 __wake_up_common(q, mode, nr_exclusive, 0, key);
3593 spin_unlock_irqrestore(&q->lock, flags);
3594}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595EXPORT_SYMBOL(__wake_up);
3596
3597/*
3598 * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
3599 */
3600void fastcall __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
3601{
3602 __wake_up_common(q, mode, 1, 0, NULL);
3603}
3604
3605/**
Martin Waitz67be2dd2005-05-01 08:59:26 -07003606 * __wake_up_sync - wake up threads blocked on a waitqueue.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 * @q: the waitqueue
3608 * @mode: which threads
3609 * @nr_exclusive: how many wake-one or wake-many threads to wake up
3610 *
3611 * The sync wakeup differs that the waker knows that it will schedule
3612 * away soon, so while the target thread will be woken up, it will not
3613 * be migrated to another CPU - ie. the two threads are 'synchronized'
3614 * with each other. This can prevent needless bouncing between CPUs.
3615 *
3616 * On UP it can prevent extra preemption.
3617 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003618void fastcall
3619__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620{
3621 unsigned long flags;
3622 int sync = 1;
3623
3624 if (unlikely(!q))
3625 return;
3626
3627 if (unlikely(!nr_exclusive))
3628 sync = 0;
3629
3630 spin_lock_irqsave(&q->lock, flags);
3631 __wake_up_common(q, mode, nr_exclusive, sync, NULL);
3632 spin_unlock_irqrestore(&q->lock, flags);
3633}
3634EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
3635
3636void fastcall complete(struct completion *x)
3637{
3638 unsigned long flags;
3639
3640 spin_lock_irqsave(&x->wait.lock, flags);
3641 x->done++;
3642 __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
3643 1, 0, NULL);
3644 spin_unlock_irqrestore(&x->wait.lock, flags);
3645}
3646EXPORT_SYMBOL(complete);
3647
3648void fastcall complete_all(struct completion *x)
3649{
3650 unsigned long flags;
3651
3652 spin_lock_irqsave(&x->wait.lock, flags);
3653 x->done += UINT_MAX/2;
3654 __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
3655 0, 0, NULL);
3656 spin_unlock_irqrestore(&x->wait.lock, flags);
3657}
3658EXPORT_SYMBOL(complete_all);
3659
3660void fastcall __sched wait_for_completion(struct completion *x)
3661{
3662 might_sleep();
Ingo Molnar48f24c42006-07-03 00:25:40 -07003663
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 spin_lock_irq(&x->wait.lock);
3665 if (!x->done) {
3666 DECLARE_WAITQUEUE(wait, current);
3667
3668 wait.flags |= WQ_FLAG_EXCLUSIVE;
3669 __add_wait_queue_tail(&x->wait, &wait);
3670 do {
3671 __set_current_state(TASK_UNINTERRUPTIBLE);
3672 spin_unlock_irq(&x->wait.lock);
3673 schedule();
3674 spin_lock_irq(&x->wait.lock);
3675 } while (!x->done);
3676 __remove_wait_queue(&x->wait, &wait);
3677 }
3678 x->done--;
3679 spin_unlock_irq(&x->wait.lock);
3680}
3681EXPORT_SYMBOL(wait_for_completion);
3682
3683unsigned long fastcall __sched
3684wait_for_completion_timeout(struct completion *x, unsigned long timeout)
3685{
3686 might_sleep();
3687
3688 spin_lock_irq(&x->wait.lock);
3689 if (!x->done) {
3690 DECLARE_WAITQUEUE(wait, current);
3691
3692 wait.flags |= WQ_FLAG_EXCLUSIVE;
3693 __add_wait_queue_tail(&x->wait, &wait);
3694 do {
3695 __set_current_state(TASK_UNINTERRUPTIBLE);
3696 spin_unlock_irq(&x->wait.lock);
3697 timeout = schedule_timeout(timeout);
3698 spin_lock_irq(&x->wait.lock);
3699 if (!timeout) {
3700 __remove_wait_queue(&x->wait, &wait);
3701 goto out;
3702 }
3703 } while (!x->done);
3704 __remove_wait_queue(&x->wait, &wait);
3705 }
3706 x->done--;
3707out:
3708 spin_unlock_irq(&x->wait.lock);
3709 return timeout;
3710}
3711EXPORT_SYMBOL(wait_for_completion_timeout);
3712
3713int fastcall __sched wait_for_completion_interruptible(struct completion *x)
3714{
3715 int ret = 0;
3716
3717 might_sleep();
3718
3719 spin_lock_irq(&x->wait.lock);
3720 if (!x->done) {
3721 DECLARE_WAITQUEUE(wait, current);
3722
3723 wait.flags |= WQ_FLAG_EXCLUSIVE;
3724 __add_wait_queue_tail(&x->wait, &wait);
3725 do {
3726 if (signal_pending(current)) {
3727 ret = -ERESTARTSYS;
3728 __remove_wait_queue(&x->wait, &wait);
3729 goto out;
3730 }
3731 __set_current_state(TASK_INTERRUPTIBLE);
3732 spin_unlock_irq(&x->wait.lock);
3733 schedule();
3734 spin_lock_irq(&x->wait.lock);
3735 } while (!x->done);
3736 __remove_wait_queue(&x->wait, &wait);
3737 }
3738 x->done--;
3739out:
3740 spin_unlock_irq(&x->wait.lock);
3741
3742 return ret;
3743}
3744EXPORT_SYMBOL(wait_for_completion_interruptible);
3745
3746unsigned long fastcall __sched
3747wait_for_completion_interruptible_timeout(struct completion *x,
3748 unsigned long timeout)
3749{
3750 might_sleep();
3751
3752 spin_lock_irq(&x->wait.lock);
3753 if (!x->done) {
3754 DECLARE_WAITQUEUE(wait, current);
3755
3756 wait.flags |= WQ_FLAG_EXCLUSIVE;
3757 __add_wait_queue_tail(&x->wait, &wait);
3758 do {
3759 if (signal_pending(current)) {
3760 timeout = -ERESTARTSYS;
3761 __remove_wait_queue(&x->wait, &wait);
3762 goto out;
3763 }
3764 __set_current_state(TASK_INTERRUPTIBLE);
3765 spin_unlock_irq(&x->wait.lock);
3766 timeout = schedule_timeout(timeout);
3767 spin_lock_irq(&x->wait.lock);
3768 if (!timeout) {
3769 __remove_wait_queue(&x->wait, &wait);
3770 goto out;
3771 }
3772 } while (!x->done);
3773 __remove_wait_queue(&x->wait, &wait);
3774 }
3775 x->done--;
3776out:
3777 spin_unlock_irq(&x->wait.lock);
3778 return timeout;
3779}
3780EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
3781
Ingo Molnar0fec1712007-07-09 18:52:01 +02003782static inline void
3783sleep_on_head(wait_queue_head_t *q, wait_queue_t *wait, unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784{
Ingo Molnar0fec1712007-07-09 18:52:01 +02003785 spin_lock_irqsave(&q->lock, *flags);
3786 __add_wait_queue(q, wait);
3787 spin_unlock(&q->lock);
3788}
3789
3790static inline void
3791sleep_on_tail(wait_queue_head_t *q, wait_queue_t *wait, unsigned long *flags)
3792{
3793 spin_lock_irq(&q->lock);
3794 __remove_wait_queue(q, wait);
3795 spin_unlock_irqrestore(&q->lock, *flags);
3796}
3797
3798void __sched interruptible_sleep_on(wait_queue_head_t *q)
3799{
3800 unsigned long flags;
3801 wait_queue_t wait;
3802
3803 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
3805 current->state = TASK_INTERRUPTIBLE;
3806
Ingo Molnar0fec1712007-07-09 18:52:01 +02003807 sleep_on_head(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 schedule();
Ingo Molnar0fec1712007-07-09 18:52:01 +02003809 sleep_on_tail(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811EXPORT_SYMBOL(interruptible_sleep_on);
3812
Ingo Molnar0fec1712007-07-09 18:52:01 +02003813long __sched
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07003814interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815{
Ingo Molnar0fec1712007-07-09 18:52:01 +02003816 unsigned long flags;
3817 wait_queue_t wait;
3818
3819 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820
3821 current->state = TASK_INTERRUPTIBLE;
3822
Ingo Molnar0fec1712007-07-09 18:52:01 +02003823 sleep_on_head(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 timeout = schedule_timeout(timeout);
Ingo Molnar0fec1712007-07-09 18:52:01 +02003825 sleep_on_tail(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
3827 return timeout;
3828}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829EXPORT_SYMBOL(interruptible_sleep_on_timeout);
3830
Ingo Molnar0fec1712007-07-09 18:52:01 +02003831void __sched sleep_on(wait_queue_head_t *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832{
Ingo Molnar0fec1712007-07-09 18:52:01 +02003833 unsigned long flags;
3834 wait_queue_t wait;
3835
3836 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837
3838 current->state = TASK_UNINTERRUPTIBLE;
3839
Ingo Molnar0fec1712007-07-09 18:52:01 +02003840 sleep_on_head(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 schedule();
Ingo Molnar0fec1712007-07-09 18:52:01 +02003842 sleep_on_tail(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844EXPORT_SYMBOL(sleep_on);
3845
Ingo Molnar0fec1712007-07-09 18:52:01 +02003846long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847{
Ingo Molnar0fec1712007-07-09 18:52:01 +02003848 unsigned long flags;
3849 wait_queue_t wait;
3850
3851 init_waitqueue_entry(&wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852
3853 current->state = TASK_UNINTERRUPTIBLE;
3854
Ingo Molnar0fec1712007-07-09 18:52:01 +02003855 sleep_on_head(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 timeout = schedule_timeout(timeout);
Ingo Molnar0fec1712007-07-09 18:52:01 +02003857 sleep_on_tail(q, &wait, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858
3859 return timeout;
3860}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861EXPORT_SYMBOL(sleep_on_timeout);
3862
Ingo Molnarb29739f2006-06-27 02:54:51 -07003863#ifdef CONFIG_RT_MUTEXES
3864
3865/*
3866 * rt_mutex_setprio - set the current priority of a task
3867 * @p: task
3868 * @prio: prio value (kernel-internal form)
3869 *
3870 * This function changes the 'effective' priority of a task. It does
3871 * not touch ->normal_prio like __setscheduler().
3872 *
3873 * Used by the rt_mutex code to implement priority inheritance logic.
3874 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003875void rt_mutex_setprio(struct task_struct *p, int prio)
Ingo Molnarb29739f2006-06-27 02:54:51 -07003876{
3877 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02003878 int oldprio, on_rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003879 struct rq *rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -07003880
3881 BUG_ON(prio < 0 || prio > MAX_PRIO);
3882
3883 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02003884 update_rq_clock(rq);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003885
Andrew Mortond5f9f942007-05-08 20:27:06 -07003886 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02003887 on_rq = p->se.on_rq;
3888 if (on_rq)
Ingo Molnar69be72c2007-08-09 11:16:49 +02003889 dequeue_task(rq, p, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02003890
3891 if (rt_prio(prio))
3892 p->sched_class = &rt_sched_class;
3893 else
3894 p->sched_class = &fair_sched_class;
3895
Ingo Molnarb29739f2006-06-27 02:54:51 -07003896 p->prio = prio;
3897
Ingo Molnardd41f592007-07-09 18:51:59 +02003898 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02003899 enqueue_task(rq, p, 0);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003900 /*
3901 * Reschedule if we are currently running on this runqueue and
Andrew Mortond5f9f942007-05-08 20:27:06 -07003902 * our priority decreased, or if we are not currently running on
3903 * this runqueue and our priority is higher than the current's
Ingo Molnarb29739f2006-06-27 02:54:51 -07003904 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07003905 if (task_running(rq, p)) {
3906 if (p->prio > oldprio)
3907 resched_task(rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02003908 } else {
3909 check_preempt_curr(rq, p);
3910 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07003911 }
3912 task_rq_unlock(rq, &flags);
3913}
3914
3915#endif
3916
Ingo Molnar36c8b582006-07-03 00:25:41 -07003917void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918{
Ingo Molnardd41f592007-07-09 18:51:59 +02003919 int old_prio, delta, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003921 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922
3923 if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
3924 return;
3925 /*
3926 * We have to be careful, if called from sys_setpriority(),
3927 * the task might be in the middle of scheduling on another CPU.
3928 */
3929 rq = task_rq_lock(p, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02003930 update_rq_clock(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 /*
3932 * The RT priorities are set via sched_setscheduler(), but we still
3933 * allow the 'normal' nice value to be set - but as expected
3934 * it wont have any effect on scheduling until the task is
Ingo Molnardd41f592007-07-09 18:51:59 +02003935 * SCHED_FIFO/SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 */
Ingo Molnare05606d2007-07-09 18:51:59 +02003937 if (task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 p->static_prio = NICE_TO_PRIO(nice);
3939 goto out_unlock;
3940 }
Ingo Molnardd41f592007-07-09 18:51:59 +02003941 on_rq = p->se.on_rq;
3942 if (on_rq) {
Ingo Molnar69be72c2007-08-09 11:16:49 +02003943 dequeue_task(rq, p, 0);
Ingo Molnar79b5ddd2007-08-09 11:16:49 +02003944 dec_load(rq, p);
Peter Williams2dd73a42006-06-27 02:54:34 -07003945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 p->static_prio = NICE_TO_PRIO(nice);
Peter Williams2dd73a42006-06-27 02:54:34 -07003948 set_load_weight(p);
Ingo Molnarb29739f2006-06-27 02:54:51 -07003949 old_prio = p->prio;
3950 p->prio = effective_prio(p);
3951 delta = p->prio - old_prio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952
Ingo Molnardd41f592007-07-09 18:51:59 +02003953 if (on_rq) {
Ingo Molnar8159f872007-08-09 11:16:49 +02003954 enqueue_task(rq, p, 0);
Ingo Molnar29b4b622007-08-09 11:16:49 +02003955 inc_load(rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 /*
Andrew Mortond5f9f942007-05-08 20:27:06 -07003957 * If the task increased its priority or is running and
3958 * lowered its priority, then reschedule its CPU:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07003960 if (delta < 0 || (delta > 0 && task_running(rq, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 resched_task(rq->curr);
3962 }
3963out_unlock:
3964 task_rq_unlock(rq, &flags);
3965}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966EXPORT_SYMBOL(set_user_nice);
3967
Matt Mackalle43379f2005-05-01 08:59:00 -07003968/*
3969 * can_nice - check if a task can reduce its nice value
3970 * @p: task
3971 * @nice: nice value
3972 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07003973int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07003974{
Matt Mackall024f4742005-08-18 11:24:19 -07003975 /* convert nice value [19,-20] to rlimit style value [1,40] */
3976 int nice_rlim = 20 - nice;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003977
Matt Mackalle43379f2005-05-01 08:59:00 -07003978 return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
3979 capable(CAP_SYS_NICE));
3980}
3981
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982#ifdef __ARCH_WANT_SYS_NICE
3983
3984/*
3985 * sys_nice - change the priority of the current process.
3986 * @increment: priority increment
3987 *
3988 * sys_setpriority is a more generic, but much slower function that
3989 * does similar things.
3990 */
3991asmlinkage long sys_nice(int increment)
3992{
Ingo Molnar48f24c42006-07-03 00:25:40 -07003993 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
3995 /*
3996 * Setpriority might change our priority at the same moment.
3997 * We don't have to worry. Conceptually one call occurs first
3998 * and we have a single winner.
3999 */
Matt Mackalle43379f2005-05-01 08:59:00 -07004000 if (increment < -40)
4001 increment = -40;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 if (increment > 40)
4003 increment = 40;
4004
4005 nice = PRIO_TO_NICE(current->static_prio) + increment;
4006 if (nice < -20)
4007 nice = -20;
4008 if (nice > 19)
4009 nice = 19;
4010
Matt Mackalle43379f2005-05-01 08:59:00 -07004011 if (increment < 0 && !can_nice(current, nice))
4012 return -EPERM;
4013
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 retval = security_task_setnice(current, nice);
4015 if (retval)
4016 return retval;
4017
4018 set_user_nice(current, nice);
4019 return 0;
4020}
4021
4022#endif
4023
4024/**
4025 * task_prio - return the priority value of a given task.
4026 * @p: the task in question.
4027 *
4028 * This is the priority value as seen by users in /proc.
4029 * RT tasks are offset by -200. Normal tasks are centered
4030 * around 0, value goes from -16 to +15.
4031 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004032int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033{
4034 return p->prio - MAX_RT_PRIO;
4035}
4036
4037/**
4038 * task_nice - return the nice value of a given task.
4039 * @p: the task in question.
4040 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004041int task_nice(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042{
4043 return TASK_NICE(p);
4044}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045EXPORT_SYMBOL_GPL(task_nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046
4047/**
4048 * idle_cpu - is a given cpu idle currently?
4049 * @cpu: the processor in question.
4050 */
4051int idle_cpu(int cpu)
4052{
4053 return cpu_curr(cpu) == cpu_rq(cpu)->idle;
4054}
4055
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056/**
4057 * idle_task - return the idle task for a given cpu.
4058 * @cpu: the processor in question.
4059 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004060struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061{
4062 return cpu_rq(cpu)->idle;
4063}
4064
4065/**
4066 * find_process_by_pid - find a process with a matching PID value.
4067 * @pid: the pid in question.
4068 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004069static inline struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070{
4071 return pid ? find_task_by_pid(pid) : current;
4072}
4073
4074/* Actually do priority change: must hold rq lock. */
Ingo Molnardd41f592007-07-09 18:51:59 +02004075static void
4076__setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077{
Ingo Molnardd41f592007-07-09 18:51:59 +02004078 BUG_ON(p->se.on_rq);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004079
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 p->policy = policy;
Ingo Molnardd41f592007-07-09 18:51:59 +02004081 switch (p->policy) {
4082 case SCHED_NORMAL:
4083 case SCHED_BATCH:
4084 case SCHED_IDLE:
4085 p->sched_class = &fair_sched_class;
4086 break;
4087 case SCHED_FIFO:
4088 case SCHED_RR:
4089 p->sched_class = &rt_sched_class;
4090 break;
4091 }
4092
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 p->rt_priority = prio;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004094 p->normal_prio = normal_prio(p);
4095 /* we are holding p->pi_lock already */
4096 p->prio = rt_mutex_getprio(p);
Peter Williams2dd73a42006-06-27 02:54:34 -07004097 set_load_weight(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098}
4099
4100/**
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004101 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 * @p: the task in question.
4103 * @policy: new policy.
4104 * @param: structure containing the new RT priority.
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004105 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004106 * NOTE that the task may be already dead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004108int sched_setscheduler(struct task_struct *p, int policy,
4109 struct sched_param *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110{
Ingo Molnardd41f592007-07-09 18:51:59 +02004111 int retval, oldprio, oldpolicy = -1, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004113 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
Steven Rostedt66e53932006-06-27 02:54:44 -07004115 /* may grab non-irq protected spin_locks */
4116 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117recheck:
4118 /* double check policy once rq lock held */
4119 if (policy < 0)
4120 policy = oldpolicy = p->policy;
4121 else if (policy != SCHED_FIFO && policy != SCHED_RR &&
Ingo Molnardd41f592007-07-09 18:51:59 +02004122 policy != SCHED_NORMAL && policy != SCHED_BATCH &&
4123 policy != SCHED_IDLE)
Ingo Molnarb0a94992006-01-14 13:20:41 -08004124 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 /*
4126 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02004127 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
4128 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 */
4130 if (param->sched_priority < 0 ||
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004131 (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
Steven Rostedtd46523e2005-07-25 16:28:39 -04004132 (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 return -EINVAL;
Ingo Molnare05606d2007-07-09 18:51:59 +02004134 if (rt_policy(policy) != (param->sched_priority != 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 return -EINVAL;
4136
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004137 /*
4138 * Allow unprivileged RT tasks to decrease priority:
4139 */
4140 if (!capable(CAP_SYS_NICE)) {
Ingo Molnare05606d2007-07-09 18:51:59 +02004141 if (rt_policy(policy)) {
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004142 unsigned long rlim_rtprio;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004143
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004144 if (!lock_task_sighand(p, &flags))
4145 return -ESRCH;
4146 rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
4147 unlock_task_sighand(p, &flags);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004148
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004149 /* can't set/change the rt policy */
4150 if (policy != p->policy && !rlim_rtprio)
4151 return -EPERM;
4152
4153 /* can't increase priority */
4154 if (param->sched_priority > p->rt_priority &&
4155 param->sched_priority > rlim_rtprio)
4156 return -EPERM;
4157 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004158 /*
4159 * Like positive nice levels, dont allow tasks to
4160 * move out of SCHED_IDLE either:
4161 */
4162 if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
4163 return -EPERM;
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07004164
Olivier Croquette37e4ab32005-06-25 14:57:32 -07004165 /* can't change other user's priorities */
4166 if ((current->euid != p->euid) &&
4167 (current->euid != p->uid))
4168 return -EPERM;
4169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170
4171 retval = security_task_setscheduler(p, policy, param);
4172 if (retval)
4173 return retval;
4174 /*
Ingo Molnarb29739f2006-06-27 02:54:51 -07004175 * make sure no PI-waiters arrive (or leave) while we are
4176 * changing the priority of the task:
4177 */
4178 spin_lock_irqsave(&p->pi_lock, flags);
4179 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 * To be able to change p->policy safely, the apropriate
4181 * runqueue lock must be held.
4182 */
Ingo Molnarb29739f2006-06-27 02:54:51 -07004183 rq = __task_rq_lock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 /* recheck policy now with rq lock held */
4185 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
4186 policy = oldpolicy = -1;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004187 __task_rq_unlock(rq);
4188 spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 goto recheck;
4190 }
Ingo Molnar2daa3572007-08-09 11:16:51 +02004191 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02004192 on_rq = p->se.on_rq;
Ingo Molnar2daa3572007-08-09 11:16:51 +02004193 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004194 deactivate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 oldprio = p->prio;
Ingo Molnardd41f592007-07-09 18:51:59 +02004196 __setscheduler(rq, p, policy, param->sched_priority);
4197 if (on_rq) {
4198 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 /*
4200 * Reschedule if we are currently running on this runqueue and
Andrew Mortond5f9f942007-05-08 20:27:06 -07004201 * our priority decreased, or if we are not currently running on
4202 * this runqueue and our priority is higher than the current's
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 */
Andrew Mortond5f9f942007-05-08 20:27:06 -07004204 if (task_running(rq, p)) {
4205 if (p->prio > oldprio)
4206 resched_task(rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02004207 } else {
4208 check_preempt_curr(rq, p);
4209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 }
Ingo Molnarb29739f2006-06-27 02:54:51 -07004211 __task_rq_unlock(rq);
4212 spin_unlock_irqrestore(&p->pi_lock, flags);
4213
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07004214 rt_mutex_adjust_pi(p);
4215
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 return 0;
4217}
4218EXPORT_SYMBOL_GPL(sched_setscheduler);
4219
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004220static int
4221do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 struct sched_param lparam;
4224 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004225 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226
4227 if (!param || pid < 0)
4228 return -EINVAL;
4229 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
4230 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004231
4232 rcu_read_lock();
4233 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 p = find_process_by_pid(pid);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07004235 if (p != NULL)
4236 retval = sched_setscheduler(p, policy, &lparam);
4237 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07004238
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 return retval;
4240}
4241
4242/**
4243 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
4244 * @pid: the pid in question.
4245 * @policy: new policy.
4246 * @param: structure containing the new RT priority.
4247 */
4248asmlinkage long sys_sched_setscheduler(pid_t pid, int policy,
4249 struct sched_param __user *param)
4250{
Jason Baronc21761f2006-01-18 17:43:03 -08004251 /* negative values for policy are not valid */
4252 if (policy < 0)
4253 return -EINVAL;
4254
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 return do_sched_setscheduler(pid, policy, param);
4256}
4257
4258/**
4259 * sys_sched_setparam - set/change the RT priority of a thread
4260 * @pid: the pid in question.
4261 * @param: structure containing the new RT priority.
4262 */
4263asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
4264{
4265 return do_sched_setscheduler(pid, -1, param);
4266}
4267
4268/**
4269 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
4270 * @pid: the pid in question.
4271 */
4272asmlinkage long sys_sched_getscheduler(pid_t pid)
4273{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004274 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
4277 if (pid < 0)
4278 goto out_nounlock;
4279
4280 retval = -ESRCH;
4281 read_lock(&tasklist_lock);
4282 p = find_process_by_pid(pid);
4283 if (p) {
4284 retval = security_task_getscheduler(p);
4285 if (!retval)
4286 retval = p->policy;
4287 }
4288 read_unlock(&tasklist_lock);
4289
4290out_nounlock:
4291 return retval;
4292}
4293
4294/**
4295 * sys_sched_getscheduler - get the RT priority of a thread
4296 * @pid: the pid in question.
4297 * @param: structure containing the RT priority.
4298 */
4299asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
4300{
4301 struct sched_param lp;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004302 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
4305 if (!param || pid < 0)
4306 goto out_nounlock;
4307
4308 read_lock(&tasklist_lock);
4309 p = find_process_by_pid(pid);
4310 retval = -ESRCH;
4311 if (!p)
4312 goto out_unlock;
4313
4314 retval = security_task_getscheduler(p);
4315 if (retval)
4316 goto out_unlock;
4317
4318 lp.sched_priority = p->rt_priority;
4319 read_unlock(&tasklist_lock);
4320
4321 /*
4322 * This one might sleep, we cannot do it with a spinlock held ...
4323 */
4324 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
4325
4326out_nounlock:
4327 return retval;
4328
4329out_unlock:
4330 read_unlock(&tasklist_lock);
4331 return retval;
4332}
4333
4334long sched_setaffinity(pid_t pid, cpumask_t new_mask)
4335{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 cpumask_t cpus_allowed;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004337 struct task_struct *p;
4338 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
Gautham R Shenoy5be93612007-05-09 02:34:04 -07004340 mutex_lock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 read_lock(&tasklist_lock);
4342
4343 p = find_process_by_pid(pid);
4344 if (!p) {
4345 read_unlock(&tasklist_lock);
Gautham R Shenoy5be93612007-05-09 02:34:04 -07004346 mutex_unlock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 return -ESRCH;
4348 }
4349
4350 /*
4351 * It is not safe to call set_cpus_allowed with the
4352 * tasklist_lock held. We will bump the task_struct's
4353 * usage count and then drop tasklist_lock.
4354 */
4355 get_task_struct(p);
4356 read_unlock(&tasklist_lock);
4357
4358 retval = -EPERM;
4359 if ((current->euid != p->euid) && (current->euid != p->uid) &&
4360 !capable(CAP_SYS_NICE))
4361 goto out_unlock;
4362
David Quigleye7834f82006-06-23 02:03:59 -07004363 retval = security_task_setscheduler(p, 0, NULL);
4364 if (retval)
4365 goto out_unlock;
4366
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 cpus_allowed = cpuset_cpus_allowed(p);
4368 cpus_and(new_mask, new_mask, cpus_allowed);
4369 retval = set_cpus_allowed(p, new_mask);
4370
4371out_unlock:
4372 put_task_struct(p);
Gautham R Shenoy5be93612007-05-09 02:34:04 -07004373 mutex_unlock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 return retval;
4375}
4376
4377static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
4378 cpumask_t *new_mask)
4379{
4380 if (len < sizeof(cpumask_t)) {
4381 memset(new_mask, 0, sizeof(cpumask_t));
4382 } else if (len > sizeof(cpumask_t)) {
4383 len = sizeof(cpumask_t);
4384 }
4385 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
4386}
4387
4388/**
4389 * sys_sched_setaffinity - set the cpu affinity of a process
4390 * @pid: pid of the process
4391 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4392 * @user_mask_ptr: user-space pointer to the new cpu mask
4393 */
4394asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
4395 unsigned long __user *user_mask_ptr)
4396{
4397 cpumask_t new_mask;
4398 int retval;
4399
4400 retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
4401 if (retval)
4402 return retval;
4403
4404 return sched_setaffinity(pid, new_mask);
4405}
4406
4407/*
4408 * Represents all cpu's present in the system
4409 * In systems capable of hotplug, this map could dynamically grow
4410 * as new cpu's are detected in the system via any platform specific
4411 * method, such as ACPI for e.g.
4412 */
4413
Andi Kleen4cef0c62006-01-11 22:44:57 +01004414cpumask_t cpu_present_map __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415EXPORT_SYMBOL(cpu_present_map);
4416
4417#ifndef CONFIG_SMP
Andi Kleen4cef0c62006-01-11 22:44:57 +01004418cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07004419EXPORT_SYMBOL(cpu_online_map);
4420
Andi Kleen4cef0c62006-01-11 22:44:57 +01004421cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Greg Bankse16b38f2006-10-02 02:17:40 -07004422EXPORT_SYMBOL(cpu_possible_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423#endif
4424
4425long sched_getaffinity(pid_t pid, cpumask_t *mask)
4426{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004427 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
Gautham R Shenoy5be93612007-05-09 02:34:04 -07004430 mutex_lock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 read_lock(&tasklist_lock);
4432
4433 retval = -ESRCH;
4434 p = find_process_by_pid(pid);
4435 if (!p)
4436 goto out_unlock;
4437
David Quigleye7834f82006-06-23 02:03:59 -07004438 retval = security_task_getscheduler(p);
4439 if (retval)
4440 goto out_unlock;
4441
Jack Steiner2f7016d2006-02-01 03:05:18 -08004442 cpus_and(*mask, p->cpus_allowed, cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
4444out_unlock:
4445 read_unlock(&tasklist_lock);
Gautham R Shenoy5be93612007-05-09 02:34:04 -07004446 mutex_unlock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Ulrich Drepper9531b622007-08-09 11:16:46 +02004448 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449}
4450
4451/**
4452 * sys_sched_getaffinity - get the cpu affinity of a process
4453 * @pid: pid of the process
4454 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
4455 * @user_mask_ptr: user-space pointer to hold the current cpu mask
4456 */
4457asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
4458 unsigned long __user *user_mask_ptr)
4459{
4460 int ret;
4461 cpumask_t mask;
4462
4463 if (len < sizeof(cpumask_t))
4464 return -EINVAL;
4465
4466 ret = sched_getaffinity(pid, &mask);
4467 if (ret < 0)
4468 return ret;
4469
4470 if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
4471 return -EFAULT;
4472
4473 return sizeof(cpumask_t);
4474}
4475
4476/**
4477 * sys_sched_yield - yield the current processor to other threads.
4478 *
Ingo Molnardd41f592007-07-09 18:51:59 +02004479 * This function yields the current CPU to other tasks. If there are no
4480 * other threads running on this CPU then this function will return.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 */
4482asmlinkage long sys_sched_yield(void)
4483{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004484 struct rq *rq = this_rq_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485
4486 schedstat_inc(rq, yld_cnt);
Ingo Molnar1799e352007-09-19 23:34:46 +02004487 current->sched_class->yield_task(rq, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
4489 /*
4490 * Since we are going to call schedule() anyway, there's
4491 * no need to preempt or enable interrupts:
4492 */
4493 __release(rq->lock);
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07004494 spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 _raw_spin_unlock(&rq->lock);
4496 preempt_enable_no_resched();
4497
4498 schedule();
4499
4500 return 0;
4501}
4502
Andrew Mortone7b38402006-06-30 01:56:00 -07004503static void __cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504{
Ingo Molnar8e0a43d2006-06-23 02:05:23 -07004505#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
4506 __might_sleep(__FILE__, __LINE__);
4507#endif
Ingo Molnar5bbcfd92005-07-07 17:57:04 -07004508 /*
4509 * The BKS might be reacquired before we have dropped
4510 * PREEMPT_ACTIVE, which could trigger a second
4511 * cond_resched() call.
4512 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 do {
4514 add_preempt_count(PREEMPT_ACTIVE);
4515 schedule();
4516 sub_preempt_count(PREEMPT_ACTIVE);
4517 } while (need_resched());
4518}
4519
4520int __sched cond_resched(void)
4521{
Ingo Molnar94142322006-12-29 16:48:13 -08004522 if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
4523 system_state == SYSTEM_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 __cond_resched();
4525 return 1;
4526 }
4527 return 0;
4528}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529EXPORT_SYMBOL(cond_resched);
4530
4531/*
4532 * cond_resched_lock() - if a reschedule is pending, drop the given lock,
4533 * call schedule, and on return reacquire the lock.
4534 *
4535 * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
4536 * operations here to prevent schedule() from being called twice (once via
4537 * spin_unlock(), once by hand).
4538 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004539int cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540{
Jan Kara6df3cec2005-06-13 15:52:32 -07004541 int ret = 0;
4542
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 if (need_lockbreak(lock)) {
4544 spin_unlock(lock);
4545 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07004546 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 spin_lock(lock);
4548 }
Ingo Molnar94142322006-12-29 16:48:13 -08004549 if (need_resched() && system_state == SYSTEM_RUNNING) {
Ingo Molnar8a25d5d2006-07-03 00:24:54 -07004550 spin_release(&lock->dep_map, 1, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 _raw_spin_unlock(lock);
4552 preempt_enable_no_resched();
4553 __cond_resched();
Jan Kara6df3cec2005-06-13 15:52:32 -07004554 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 }
Jan Kara6df3cec2005-06-13 15:52:32 -07004557 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559EXPORT_SYMBOL(cond_resched_lock);
4560
4561int __sched cond_resched_softirq(void)
4562{
4563 BUG_ON(!in_softirq());
4564
Ingo Molnar94142322006-12-29 16:48:13 -08004565 if (need_resched() && system_state == SYSTEM_RUNNING) {
Thomas Gleixner98d825672007-05-23 13:58:18 -07004566 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 __cond_resched();
4568 local_bh_disable();
4569 return 1;
4570 }
4571 return 0;
4572}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573EXPORT_SYMBOL(cond_resched_softirq);
4574
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575/**
4576 * yield - yield the current processor to other threads.
4577 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08004578 * This is a shortcut for kernel-space yielding - it marks the
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 * thread runnable and calls sys_sched_yield().
4580 */
4581void __sched yield(void)
4582{
4583 set_current_state(TASK_RUNNING);
4584 sys_sched_yield();
4585}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586EXPORT_SYMBOL(yield);
4587
4588/*
4589 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
4590 * that process accounting knows that this is a task in IO wait state.
4591 *
4592 * But don't do that if it is a deliberate, throttling IO wait (this task
4593 * has set its backing_dev_info: the queue against which it should throttle)
4594 */
4595void __sched io_schedule(void)
4596{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004597 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004599 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 atomic_inc(&rq->nr_iowait);
4601 schedule();
4602 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004603 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605EXPORT_SYMBOL(io_schedule);
4606
4607long __sched io_schedule_timeout(long timeout)
4608{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004609 struct rq *rq = &__raw_get_cpu_var(runqueues);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 long ret;
4611
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004612 delayacct_blkio_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 atomic_inc(&rq->nr_iowait);
4614 ret = schedule_timeout(timeout);
4615 atomic_dec(&rq->nr_iowait);
Shailabh Nagar0ff92242006-07-14 00:24:37 -07004616 delayacct_blkio_end();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 return ret;
4618}
4619
4620/**
4621 * sys_sched_get_priority_max - return maximum RT priority.
4622 * @policy: scheduling class.
4623 *
4624 * this syscall returns the maximum rt_priority that can be used
4625 * by a given scheduling class.
4626 */
4627asmlinkage long sys_sched_get_priority_max(int policy)
4628{
4629 int ret = -EINVAL;
4630
4631 switch (policy) {
4632 case SCHED_FIFO:
4633 case SCHED_RR:
4634 ret = MAX_USER_RT_PRIO-1;
4635 break;
4636 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08004637 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02004638 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 ret = 0;
4640 break;
4641 }
4642 return ret;
4643}
4644
4645/**
4646 * sys_sched_get_priority_min - return minimum RT priority.
4647 * @policy: scheduling class.
4648 *
4649 * this syscall returns the minimum rt_priority that can be used
4650 * by a given scheduling class.
4651 */
4652asmlinkage long sys_sched_get_priority_min(int policy)
4653{
4654 int ret = -EINVAL;
4655
4656 switch (policy) {
4657 case SCHED_FIFO:
4658 case SCHED_RR:
4659 ret = 1;
4660 break;
4661 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08004662 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02004663 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 ret = 0;
4665 }
4666 return ret;
4667}
4668
4669/**
4670 * sys_sched_rr_get_interval - return the default timeslice of a process.
4671 * @pid: pid of the process.
4672 * @interval: userspace pointer to the timeslice value.
4673 *
4674 * this syscall writes the default timeslice value of a given process
4675 * into the user-space timespec buffer. A value of '0' means infinity.
4676 */
4677asmlinkage
4678long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
4679{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004680 struct task_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 int retval = -EINVAL;
4682 struct timespec t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683
4684 if (pid < 0)
4685 goto out_nounlock;
4686
4687 retval = -ESRCH;
4688 read_lock(&tasklist_lock);
4689 p = find_process_by_pid(pid);
4690 if (!p)
4691 goto out_unlock;
4692
4693 retval = security_task_getscheduler(p);
4694 if (retval)
4695 goto out_unlock;
4696
Peter Williamsb78709c2006-06-26 16:58:00 +10004697 jiffies_to_timespec(p->policy == SCHED_FIFO ?
Ingo Molnardd41f592007-07-09 18:51:59 +02004698 0 : static_prio_timeslice(p->static_prio), &t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 read_unlock(&tasklist_lock);
4700 retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
4701out_nounlock:
4702 return retval;
4703out_unlock:
4704 read_unlock(&tasklist_lock);
4705 return retval;
4706}
4707
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004708static const char stat_nam[] = "RSDTtZX";
Ingo Molnar36c8b582006-07-03 00:25:41 -07004709
4710static void show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 unsigned long free = 0;
Ingo Molnar36c8b582006-07-03 00:25:41 -07004713 unsigned state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 state = p->state ? __ffs(p->state) + 1 : 0;
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004716 printk("%-13.13s %c", p->comm,
4717 state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
Ingo Molnar4bd77322007-07-11 21:21:47 +02004718#if BITS_PER_LONG == 32
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 if (state == TASK_RUNNING)
Ingo Molnar4bd77322007-07-11 21:21:47 +02004720 printk(" running ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 else
Ingo Molnar4bd77322007-07-11 21:21:47 +02004722 printk(" %08lx ", thread_saved_pc(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723#else
4724 if (state == TASK_RUNNING)
Ingo Molnar4bd77322007-07-11 21:21:47 +02004725 printk(" running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 else
4727 printk(" %016lx ", thread_saved_pc(p));
4728#endif
4729#ifdef CONFIG_DEBUG_STACK_USAGE
4730 {
Al Viro10ebffd2005-11-13 16:06:56 -08004731 unsigned long *n = end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 while (!*n)
4733 n++;
Al Viro10ebffd2005-11-13 16:06:56 -08004734 free = (unsigned long)n - (unsigned long)end_of_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 }
4736#endif
Ingo Molnar4bd77322007-07-11 21:21:47 +02004737 printk("%5lu %5d %6d\n", free, p->pid, p->parent->pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738
4739 if (state != TASK_RUNNING)
4740 show_stack(p, NULL);
4741}
4742
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004743void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744{
Ingo Molnar36c8b582006-07-03 00:25:41 -07004745 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746
Ingo Molnar4bd77322007-07-11 21:21:47 +02004747#if BITS_PER_LONG == 32
4748 printk(KERN_INFO
4749 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750#else
Ingo Molnar4bd77322007-07-11 21:21:47 +02004751 printk(KERN_INFO
4752 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753#endif
4754 read_lock(&tasklist_lock);
4755 do_each_thread(g, p) {
4756 /*
4757 * reset the NMI-timeout, listing all files on a slow
4758 * console might take alot of time:
4759 */
4760 touch_nmi_watchdog();
Ingo Molnar39bc89f2007-04-25 20:50:03 -07004761 if (!state_filter || (p->state & state_filter))
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004762 show_task(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 } while_each_thread(g, p);
4764
Jeremy Fitzhardinge04c91672007-05-08 00:28:05 -07004765 touch_all_softlockup_watchdogs();
4766
Ingo Molnardd41f592007-07-09 18:51:59 +02004767#ifdef CONFIG_SCHED_DEBUG
4768 sysrq_sched_debug_show();
4769#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 read_unlock(&tasklist_lock);
Ingo Molnare59e2ae2006-12-06 20:35:59 -08004771 /*
4772 * Only show locks if all tasks are dumped:
4773 */
4774 if (state_filter == -1)
4775 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776}
4777
Ingo Molnar1df21052007-07-09 18:51:58 +02004778void __cpuinit init_idle_bootup_task(struct task_struct *idle)
4779{
Ingo Molnardd41f592007-07-09 18:51:59 +02004780 idle->sched_class = &idle_sched_class;
Ingo Molnar1df21052007-07-09 18:51:58 +02004781}
4782
Ingo Molnarf340c0d2005-06-28 16:40:42 +02004783/**
4784 * init_idle - set up an idle thread for a given CPU
4785 * @idle: task in question
4786 * @cpu: cpu the idle task belongs to
4787 *
4788 * NOTE: this function does not set the idle thread's NEED_RESCHED
4789 * flag, to make booting more robust.
4790 */
Nick Piggin5c1e1762006-10-03 01:14:04 -07004791void __cpuinit init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004793 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 unsigned long flags;
4795
Ingo Molnardd41f592007-07-09 18:51:59 +02004796 __sched_fork(idle);
4797 idle->se.exec_start = sched_clock();
4798
Ingo Molnarb29739f2006-06-27 02:54:51 -07004799 idle->prio = idle->normal_prio = MAX_PRIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 idle->cpus_allowed = cpumask_of_cpu(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004801 __set_task_cpu(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802
4803 spin_lock_irqsave(&rq->lock, flags);
4804 rq->curr = rq->idle = idle;
Nick Piggin4866cde2005-06-25 14:57:23 -07004805#if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
4806 idle->oncpu = 1;
4807#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 spin_unlock_irqrestore(&rq->lock, flags);
4809
4810 /* Set the preempt count _outside_ the spinlocks! */
4811#if defined(CONFIG_PREEMPT) && !defined(CONFIG_PREEMPT_BKL)
Al Viroa1261f52005-11-13 16:06:55 -08004812 task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813#else
Al Viroa1261f52005-11-13 16:06:55 -08004814 task_thread_info(idle)->preempt_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02004816 /*
4817 * The idle tasks have their own, simple scheduling class:
4818 */
4819 idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820}
4821
4822/*
4823 * In a system that switches off the HZ timer nohz_cpu_mask
4824 * indicates which cpus entered this state. This is used
4825 * in the rcu update to wait only for active cpus. For system
4826 * which do not switch off the HZ timer nohz_cpu_mask should
4827 * always be CPU_MASK_NONE.
4828 */
4829cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
4830
4831#ifdef CONFIG_SMP
4832/*
4833 * This is how migration works:
4834 *
Ingo Molnar70b97a72006-07-03 00:25:42 -07004835 * 1) we queue a struct migration_req structure in the source CPU's
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 * runqueue and wake up that CPU's migration thread.
4837 * 2) we down() the locked semaphore => thread blocks.
4838 * 3) migration thread wakes up (implicitly it forces the migrated
4839 * thread off the CPU)
4840 * 4) it gets the migration request and checks whether the migrated
4841 * task is still in the wrong runqueue.
4842 * 5) if it's in the wrong runqueue then the migration thread removes
4843 * it and puts it into the right queue.
4844 * 6) migration thread up()s the semaphore.
4845 * 7) we wake up and the migration is done.
4846 */
4847
4848/*
4849 * Change a given task's CPU affinity. Migrate the thread to a
4850 * proper CPU and schedule it away if the CPU it's executing on
4851 * is removed from the allowed bitmask.
4852 *
4853 * NOTE: the caller must have a valid reference to the task, the
4854 * task must not exit() & deallocate itself prematurely. The
4855 * call is not atomic; no spinlocks may be held.
4856 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004857int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004859 struct migration_req req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004861 struct rq *rq;
Ingo Molnar48f24c42006-07-03 00:25:40 -07004862 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863
4864 rq = task_rq_lock(p, &flags);
4865 if (!cpus_intersects(new_mask, cpu_online_map)) {
4866 ret = -EINVAL;
4867 goto out;
4868 }
4869
4870 p->cpus_allowed = new_mask;
4871 /* Can the task run on the task's current CPU? If so, we're done */
4872 if (cpu_isset(task_cpu(p), new_mask))
4873 goto out;
4874
4875 if (migrate_task(p, any_online_cpu(new_mask), &req)) {
4876 /* Need help from migration thread: drop lock and wait. */
4877 task_rq_unlock(rq, &flags);
4878 wake_up_process(rq->migration_thread);
4879 wait_for_completion(&req.done);
4880 tlb_migrate_finish(p->mm);
4881 return 0;
4882 }
4883out:
4884 task_rq_unlock(rq, &flags);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004885
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 return ret;
4887}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888EXPORT_SYMBOL_GPL(set_cpus_allowed);
4889
4890/*
4891 * Move (not current) task off this cpu, onto dest cpu. We're doing
4892 * this because either it can't run here any more (set_cpus_allowed()
4893 * away from this CPU, or CPU going down), or because we're
4894 * attempting to rebalance this task on exec (sched_exec).
4895 *
4896 * So we race with normal scheduler movements, but that's OK, as long
4897 * as the task is no longer on this CPU.
Kirill Korotaevefc30812006-06-27 02:54:32 -07004898 *
4899 * Returns non-zero if task was successfully migrated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 */
Kirill Korotaevefc30812006-06-27 02:54:32 -07004901static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902{
Ingo Molnar70b97a72006-07-03 00:25:42 -07004903 struct rq *rq_dest, *rq_src;
Ingo Molnardd41f592007-07-09 18:51:59 +02004904 int ret = 0, on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905
4906 if (unlikely(cpu_is_offline(dest_cpu)))
Kirill Korotaevefc30812006-06-27 02:54:32 -07004907 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908
4909 rq_src = cpu_rq(src_cpu);
4910 rq_dest = cpu_rq(dest_cpu);
4911
4912 double_rq_lock(rq_src, rq_dest);
4913 /* Already moved. */
4914 if (task_cpu(p) != src_cpu)
4915 goto out;
4916 /* Affinity changed (again). */
4917 if (!cpu_isset(dest_cpu, p->cpus_allowed))
4918 goto out;
4919
Ingo Molnardd41f592007-07-09 18:51:59 +02004920 on_rq = p->se.on_rq;
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02004921 if (on_rq)
Ingo Molnar2e1cb742007-08-09 11:16:49 +02004922 deactivate_task(rq_src, p, 0);
Ingo Molnar6e82a3b2007-08-09 11:16:51 +02004923
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 set_task_cpu(p, dest_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004925 if (on_rq) {
4926 activate_task(rq_dest, p, 0);
4927 check_preempt_curr(rq_dest, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 }
Kirill Korotaevefc30812006-06-27 02:54:32 -07004929 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930out:
4931 double_rq_unlock(rq_src, rq_dest);
Kirill Korotaevefc30812006-06-27 02:54:32 -07004932 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933}
4934
4935/*
4936 * migration_thread - this is a highprio system thread that performs
4937 * thread migration by bumping thread off CPU then 'pushing' onto
4938 * another runqueue.
4939 */
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004940static int migration_thread(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 int cpu = (long)data;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004943 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944
4945 rq = cpu_rq(cpu);
4946 BUG_ON(rq->migration_thread != current);
4947
4948 set_current_state(TASK_INTERRUPTIBLE);
4949 while (!kthread_should_stop()) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07004950 struct migration_req *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 spin_lock_irq(&rq->lock);
4954
4955 if (cpu_is_offline(cpu)) {
4956 spin_unlock_irq(&rq->lock);
4957 goto wait_to_die;
4958 }
4959
4960 if (rq->active_balance) {
4961 active_load_balance(rq, cpu);
4962 rq->active_balance = 0;
4963 }
4964
4965 head = &rq->migration_queue;
4966
4967 if (list_empty(head)) {
4968 spin_unlock_irq(&rq->lock);
4969 schedule();
4970 set_current_state(TASK_INTERRUPTIBLE);
4971 continue;
4972 }
Ingo Molnar70b97a72006-07-03 00:25:42 -07004973 req = list_entry(head->next, struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 list_del_init(head->next);
4975
Nick Piggin674311d2005-06-25 14:57:27 -07004976 spin_unlock(&rq->lock);
4977 __migrate_task(req->task, cpu, req->dest_cpu);
4978 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979
4980 complete(&req->done);
4981 }
4982 __set_current_state(TASK_RUNNING);
4983 return 0;
4984
4985wait_to_die:
4986 /* Wait for kthread_stop */
4987 set_current_state(TASK_INTERRUPTIBLE);
4988 while (!kthread_should_stop()) {
4989 schedule();
4990 set_current_state(TASK_INTERRUPTIBLE);
4991 }
4992 __set_current_state(TASK_RUNNING);
4993 return 0;
4994}
4995
4996#ifdef CONFIG_HOTPLUG_CPU
Kirill Korotaev054b9102006-12-10 02:20:11 -08004997/*
4998 * Figure out where task on dead CPU should go, use force if neccessary.
4999 * NOTE: interrupts should be disabled by the caller
5000 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005001static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002{
Kirill Korotaevefc30812006-06-27 02:54:32 -07005003 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 cpumask_t mask;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005005 struct rq *rq;
5006 int dest_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007
Kirill Korotaevefc30812006-06-27 02:54:32 -07005008restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 /* On same node? */
5010 mask = node_to_cpumask(cpu_to_node(dead_cpu));
Ingo Molnar48f24c42006-07-03 00:25:40 -07005011 cpus_and(mask, mask, p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 dest_cpu = any_online_cpu(mask);
5013
5014 /* On any allowed CPU? */
5015 if (dest_cpu == NR_CPUS)
Ingo Molnar48f24c42006-07-03 00:25:40 -07005016 dest_cpu = any_online_cpu(p->cpus_allowed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017
5018 /* No more Mr. Nice Guy. */
5019 if (dest_cpu == NR_CPUS) {
Ingo Molnar48f24c42006-07-03 00:25:40 -07005020 rq = task_rq_lock(p, &flags);
5021 cpus_setall(p->cpus_allowed);
5022 dest_cpu = any_online_cpu(p->cpus_allowed);
Kirill Korotaevefc30812006-06-27 02:54:32 -07005023 task_rq_unlock(rq, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024
5025 /*
5026 * Don't tell them about moving exiting tasks or
5027 * kernel threads (both mm NULL), since they never
5028 * leave kernel.
5029 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005030 if (p->mm && printk_ratelimit())
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 printk(KERN_INFO "process %d (%s) no "
5032 "longer affine to cpu%d\n",
Ingo Molnar48f24c42006-07-03 00:25:40 -07005033 p->pid, p->comm, dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 }
Ingo Molnar48f24c42006-07-03 00:25:40 -07005035 if (!__migrate_task(p, dead_cpu, dest_cpu))
Kirill Korotaevefc30812006-06-27 02:54:32 -07005036 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037}
5038
5039/*
5040 * While a dead CPU has no uninterruptible tasks queued at this point,
5041 * it might still have a nonzero ->nr_uninterruptible counter, because
5042 * for performance reasons the counter is not stricly tracking tasks to
5043 * their home CPUs. So we just add the counter to another CPU's counter,
5044 * to keep the global sum constant after CPU-down:
5045 */
Ingo Molnar70b97a72006-07-03 00:25:42 -07005046static void migrate_nr_uninterruptible(struct rq *rq_src)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005048 struct rq *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 unsigned long flags;
5050
5051 local_irq_save(flags);
5052 double_rq_lock(rq_src, rq_dest);
5053 rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
5054 rq_src->nr_uninterruptible = 0;
5055 double_rq_unlock(rq_src, rq_dest);
5056 local_irq_restore(flags);
5057}
5058
5059/* Run through task list and migrate tasks from the dead cpu. */
5060static void migrate_live_tasks(int src_cpu)
5061{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005062 struct task_struct *p, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063
5064 write_lock_irq(&tasklist_lock);
5065
Ingo Molnar48f24c42006-07-03 00:25:40 -07005066 do_each_thread(t, p) {
5067 if (p == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 continue;
5069
Ingo Molnar48f24c42006-07-03 00:25:40 -07005070 if (task_cpu(p) == src_cpu)
5071 move_task_off_dead_cpu(src_cpu, p);
5072 } while_each_thread(t, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073
5074 write_unlock_irq(&tasklist_lock);
5075}
5076
Ingo Molnardd41f592007-07-09 18:51:59 +02005077/*
5078 * Schedules idle task to be the next runnable task on current CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 * It does so by boosting its priority to highest possible and adding it to
Ingo Molnar48f24c42006-07-03 00:25:40 -07005080 * the _front_ of the runqueue. Used by CPU offline code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 */
5082void sched_idle_next(void)
5083{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005084 int this_cpu = smp_processor_id();
Ingo Molnar70b97a72006-07-03 00:25:42 -07005085 struct rq *rq = cpu_rq(this_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 struct task_struct *p = rq->idle;
5087 unsigned long flags;
5088
5089 /* cpu has to be offline */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005090 BUG_ON(cpu_online(this_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091
Ingo Molnar48f24c42006-07-03 00:25:40 -07005092 /*
5093 * Strictly not necessary since rest of the CPUs are stopped by now
5094 * and interrupts disabled on the current cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 */
5096 spin_lock_irqsave(&rq->lock, flags);
5097
Ingo Molnardd41f592007-07-09 18:51:59 +02005098 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005099
5100 /* Add idle task to the _front_ of its priority queue: */
Ingo Molnardd41f592007-07-09 18:51:59 +02005101 activate_idle_task(p, rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102
5103 spin_unlock_irqrestore(&rq->lock, flags);
5104}
5105
Ingo Molnar48f24c42006-07-03 00:25:40 -07005106/*
5107 * Ensures that the idle task is using init_mm right before its cpu goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 * offline.
5109 */
5110void idle_task_exit(void)
5111{
5112 struct mm_struct *mm = current->active_mm;
5113
5114 BUG_ON(cpu_online(smp_processor_id()));
5115
5116 if (mm != &init_mm)
5117 switch_mm(mm, &init_mm, current);
5118 mmdrop(mm);
5119}
5120
Kirill Korotaev054b9102006-12-10 02:20:11 -08005121/* called under rq->lock with disabled interrupts */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005122static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005124 struct rq *rq = cpu_rq(dead_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
5126 /* Must be exiting, otherwise would be on tasklist. */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005127 BUG_ON(p->exit_state != EXIT_ZOMBIE && p->exit_state != EXIT_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128
5129 /* Cannot have done final schedule yet: would have vanished. */
Oleg Nesterovc394cc92006-09-29 02:01:11 -07005130 BUG_ON(p->state == TASK_DEAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131
Ingo Molnar48f24c42006-07-03 00:25:40 -07005132 get_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133
5134 /*
5135 * Drop lock around migration; if someone else moves it,
5136 * that's OK. No task can be added to this CPU, so iteration is
5137 * fine.
Kirill Korotaev054b9102006-12-10 02:20:11 -08005138 * NOTE: interrupts should be left disabled --dev@
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 */
Kirill Korotaev054b9102006-12-10 02:20:11 -08005140 spin_unlock(&rq->lock);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005141 move_task_off_dead_cpu(dead_cpu, p);
Kirill Korotaev054b9102006-12-10 02:20:11 -08005142 spin_lock(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143
Ingo Molnar48f24c42006-07-03 00:25:40 -07005144 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145}
5146
5147/* release_task() removes task from tasklist, so we won't find dead tasks. */
5148static void migrate_dead_tasks(unsigned int dead_cpu)
5149{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005150 struct rq *rq = cpu_rq(dead_cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02005151 struct task_struct *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152
Ingo Molnardd41f592007-07-09 18:51:59 +02005153 for ( ; ; ) {
5154 if (!rq->nr_running)
5155 break;
Ingo Molnara8e504d2007-08-09 11:16:47 +02005156 update_rq_clock(rq);
Ingo Molnarff95f3d2007-08-09 11:16:49 +02005157 next = pick_next_task(rq, rq->curr);
Ingo Molnardd41f592007-07-09 18:51:59 +02005158 if (!next)
5159 break;
5160 migrate_dead(dead_cpu, next);
Nick Piggine692ab52007-07-26 13:40:43 +02005161
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 }
5163}
5164#endif /* CONFIG_HOTPLUG_CPU */
5165
Nick Piggine692ab52007-07-26 13:40:43 +02005166#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
5167
5168static struct ctl_table sd_ctl_dir[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005169 {
5170 .procname = "sched_domain",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005171 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005172 },
Nick Piggine692ab52007-07-26 13:40:43 +02005173 {0,},
5174};
5175
5176static struct ctl_table sd_ctl_root[] = {
Alexey Dobriyane0361852007-08-09 11:16:46 +02005177 {
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005178 .ctl_name = CTL_KERN,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005179 .procname = "kernel",
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005180 .mode = 0555,
Alexey Dobriyane0361852007-08-09 11:16:46 +02005181 .child = sd_ctl_dir,
5182 },
Nick Piggine692ab52007-07-26 13:40:43 +02005183 {0,},
5184};
5185
5186static struct ctl_table *sd_alloc_ctl_entry(int n)
5187{
5188 struct ctl_table *entry =
5189 kmalloc(n * sizeof(struct ctl_table), GFP_KERNEL);
5190
5191 BUG_ON(!entry);
5192 memset(entry, 0, n * sizeof(struct ctl_table));
5193
5194 return entry;
5195}
5196
5197static void
Alexey Dobriyane0361852007-08-09 11:16:46 +02005198set_table_entry(struct ctl_table *entry,
Nick Piggine692ab52007-07-26 13:40:43 +02005199 const char *procname, void *data, int maxlen,
5200 mode_t mode, proc_handler *proc_handler)
5201{
Nick Piggine692ab52007-07-26 13:40:43 +02005202 entry->procname = procname;
5203 entry->data = data;
5204 entry->maxlen = maxlen;
5205 entry->mode = mode;
5206 entry->proc_handler = proc_handler;
5207}
5208
5209static struct ctl_table *
5210sd_alloc_ctl_domain_table(struct sched_domain *sd)
5211{
5212 struct ctl_table *table = sd_alloc_ctl_entry(14);
5213
Alexey Dobriyane0361852007-08-09 11:16:46 +02005214 set_table_entry(&table[0], "min_interval", &sd->min_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005215 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005216 set_table_entry(&table[1], "max_interval", &sd->max_interval,
Nick Piggine692ab52007-07-26 13:40:43 +02005217 sizeof(long), 0644, proc_doulongvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005218 set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005219 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005220 set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005221 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005222 set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005223 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005224 set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005225 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005226 set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
Nick Piggine692ab52007-07-26 13:40:43 +02005227 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005228 set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
Nick Piggine692ab52007-07-26 13:40:43 +02005229 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005230 set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
Nick Piggine692ab52007-07-26 13:40:43 +02005231 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005232 set_table_entry(&table[10], "cache_nice_tries",
Nick Piggine692ab52007-07-26 13:40:43 +02005233 &sd->cache_nice_tries,
5234 sizeof(int), 0644, proc_dointvec_minmax);
Alexey Dobriyane0361852007-08-09 11:16:46 +02005235 set_table_entry(&table[12], "flags", &sd->flags,
Nick Piggine692ab52007-07-26 13:40:43 +02005236 sizeof(int), 0644, proc_dointvec_minmax);
5237
5238 return table;
5239}
5240
5241static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
5242{
5243 struct ctl_table *entry, *table;
5244 struct sched_domain *sd;
5245 int domain_num = 0, i;
5246 char buf[32];
5247
5248 for_each_domain(cpu, sd)
5249 domain_num++;
5250 entry = table = sd_alloc_ctl_entry(domain_num + 1);
5251
5252 i = 0;
5253 for_each_domain(cpu, sd) {
5254 snprintf(buf, 32, "domain%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005255 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005256 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005257 entry->child = sd_alloc_ctl_domain_table(sd);
5258 entry++;
5259 i++;
5260 }
5261 return table;
5262}
5263
5264static struct ctl_table_header *sd_sysctl_header;
5265static void init_sched_domain_sysctl(void)
5266{
5267 int i, cpu_num = num_online_cpus();
5268 struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
5269 char buf[32];
5270
5271 sd_ctl_dir[0].child = entry;
5272
5273 for (i = 0; i < cpu_num; i++, entry++) {
5274 snprintf(buf, 32, "cpu%d", i);
Nick Piggine692ab52007-07-26 13:40:43 +02005275 entry->procname = kstrdup(buf, GFP_KERNEL);
Eric W. Biedermanc57baf12007-08-23 15:18:02 +02005276 entry->mode = 0555;
Nick Piggine692ab52007-07-26 13:40:43 +02005277 entry->child = sd_alloc_ctl_cpu_table(i);
5278 }
5279 sd_sysctl_header = register_sysctl_table(sd_ctl_root);
5280}
5281#else
5282static void init_sched_domain_sysctl(void)
5283{
5284}
5285#endif
5286
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287/*
5288 * migration_call - callback that gets triggered when a CPU is added.
5289 * Here we can start up the necessary migration thread for the new CPU.
5290 */
Ingo Molnar48f24c42006-07-03 00:25:40 -07005291static int __cpuinit
5292migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 struct task_struct *p;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005295 int cpu = (long)hcpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07005297 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298
5299 switch (action) {
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005300 case CPU_LOCK_ACQUIRE:
5301 mutex_lock(&sched_hotcpu_mutex);
5302 break;
5303
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005305 case CPU_UP_PREPARE_FROZEN:
Ingo Molnardd41f592007-07-09 18:51:59 +02005306 p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 if (IS_ERR(p))
5308 return NOTIFY_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 kthread_bind(p, cpu);
5310 /* Must be high prio: stop_machine expects to yield to it. */
5311 rq = task_rq_lock(p, &flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02005312 __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 task_rq_unlock(rq, &flags);
5314 cpu_rq(cpu)->migration_thread = p;
5315 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005316
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005318 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 /* Strictly unneccessary, as first user will wake it. */
5320 wake_up_process(cpu_rq(cpu)->migration_thread);
5321 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005322
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323#ifdef CONFIG_HOTPLUG_CPU
5324 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005325 case CPU_UP_CANCELED_FROZEN:
Heiko Carstensfc75cdf2006-06-25 05:49:10 -07005326 if (!cpu_rq(cpu)->migration_thread)
5327 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 /* Unbind it from offline cpu so it can run. Fall thru. */
Heiko Carstensa4c4af72005-11-07 00:58:38 -08005329 kthread_bind(cpu_rq(cpu)->migration_thread,
5330 any_online_cpu(cpu_online_map));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 kthread_stop(cpu_rq(cpu)->migration_thread);
5332 cpu_rq(cpu)->migration_thread = NULL;
5333 break;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005334
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07005336 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 migrate_live_tasks(cpu);
5338 rq = cpu_rq(cpu);
5339 kthread_stop(rq->migration_thread);
5340 rq->migration_thread = NULL;
5341 /* Idle task back to normal (off runqueue, low prio) */
5342 rq = task_rq_lock(rq->idle, &flags);
Ingo Molnara8e504d2007-08-09 11:16:47 +02005343 update_rq_clock(rq);
Ingo Molnar2e1cb742007-08-09 11:16:49 +02005344 deactivate_task(rq, rq->idle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 rq->idle->static_prio = MAX_PRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +02005346 __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
5347 rq->idle->sched_class = &idle_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 migrate_dead_tasks(cpu);
5349 task_rq_unlock(rq, &flags);
5350 migrate_nr_uninterruptible(rq);
5351 BUG_ON(rq->nr_running != 0);
5352
5353 /* No need to migrate the tasks: it was best-effort if
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005354 * they didn't take sched_hotcpu_mutex. Just wake up
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 * the requestors. */
5356 spin_lock_irq(&rq->lock);
5357 while (!list_empty(&rq->migration_queue)) {
Ingo Molnar70b97a72006-07-03 00:25:42 -07005358 struct migration_req *req;
5359
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 req = list_entry(rq->migration_queue.next,
Ingo Molnar70b97a72006-07-03 00:25:42 -07005361 struct migration_req, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 list_del_init(&req->list);
5363 complete(&req->done);
5364 }
5365 spin_unlock_irq(&rq->lock);
5366 break;
5367#endif
Gautham R Shenoy5be93612007-05-09 02:34:04 -07005368 case CPU_LOCK_RELEASE:
5369 mutex_unlock(&sched_hotcpu_mutex);
5370 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 }
5372 return NOTIFY_OK;
5373}
5374
5375/* Register at highest priority so that task migration (migrate_all_tasks)
5376 * happens before everything else.
5377 */
Chandra Seetharaman26c21432006-06-27 02:54:10 -07005378static struct notifier_block __cpuinitdata migration_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 .notifier_call = migration_call,
5380 .priority = 10
5381};
5382
5383int __init migration_init(void)
5384{
5385 void *cpu = (void *)(long)smp_processor_id();
Akinobu Mita07dccf32006-09-29 02:00:22 -07005386 int err;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005387
5388 /* Start one for the boot CPU: */
Akinobu Mita07dccf32006-09-29 02:00:22 -07005389 err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
5390 BUG_ON(err == NOTIFY_BAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 migration_call(&migration_notifier, CPU_ONLINE, cpu);
5392 register_cpu_notifier(&migration_notifier);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005393
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 return 0;
5395}
5396#endif
5397
5398#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07005399
5400/* Number of possible processor ids */
5401int nr_cpu_ids __read_mostly = NR_CPUS;
5402EXPORT_SYMBOL(nr_cpu_ids);
5403
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07005404#undef SCHED_DOMAIN_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405#ifdef SCHED_DOMAIN_DEBUG
5406static void sched_domain_debug(struct sched_domain *sd, int cpu)
5407{
5408 int level = 0;
5409
Nick Piggin41c7ce92005-06-25 14:57:24 -07005410 if (!sd) {
5411 printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
5412 return;
5413 }
5414
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
5416
5417 do {
5418 int i;
5419 char str[NR_CPUS];
5420 struct sched_group *group = sd->groups;
5421 cpumask_t groupmask;
5422
5423 cpumask_scnprintf(str, NR_CPUS, sd->span);
5424 cpus_clear(groupmask);
5425
5426 printk(KERN_DEBUG);
5427 for (i = 0; i < level + 1; i++)
5428 printk(" ");
5429 printk("domain %d: ", level);
5430
5431 if (!(sd->flags & SD_LOAD_BALANCE)) {
5432 printk("does not load-balance\n");
5433 if (sd->parent)
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005434 printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
5435 " has parent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 break;
5437 }
5438
5439 printk("span %s\n", str);
5440
5441 if (!cpu_isset(cpu, sd->span))
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005442 printk(KERN_ERR "ERROR: domain->span does not contain "
5443 "CPU%d\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 if (!cpu_isset(cpu, group->cpumask))
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005445 printk(KERN_ERR "ERROR: domain->groups does not contain"
5446 " CPU%d\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
5448 printk(KERN_DEBUG);
5449 for (i = 0; i < level + 2; i++)
5450 printk(" ");
5451 printk("groups:");
5452 do {
5453 if (!group) {
5454 printk("\n");
5455 printk(KERN_ERR "ERROR: group is NULL\n");
5456 break;
5457 }
5458
Eric Dumazet5517d862007-05-08 00:32:57 -07005459 if (!group->__cpu_power) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 printk("\n");
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005461 printk(KERN_ERR "ERROR: domain->cpu_power not "
5462 "set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 }
5464
5465 if (!cpus_weight(group->cpumask)) {
5466 printk("\n");
5467 printk(KERN_ERR "ERROR: empty group\n");
5468 }
5469
5470 if (cpus_intersects(groupmask, group->cpumask)) {
5471 printk("\n");
5472 printk(KERN_ERR "ERROR: repeated CPUs\n");
5473 }
5474
5475 cpus_or(groupmask, groupmask, group->cpumask);
5476
5477 cpumask_scnprintf(str, NR_CPUS, group->cpumask);
5478 printk(" %s", str);
5479
5480 group = group->next;
5481 } while (group != sd->groups);
5482 printk("\n");
5483
5484 if (!cpus_equal(sd->span, groupmask))
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005485 printk(KERN_ERR "ERROR: groups don't span "
5486 "domain->span\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487
5488 level++;
5489 sd = sd->parent;
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005490 if (!sd)
5491 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08005493 if (!cpus_subset(groupmask, sd->span))
5494 printk(KERN_ERR "ERROR: parent span is not a superset "
5495 "of domain->span\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496
5497 } while (sd);
5498}
5499#else
Ingo Molnar48f24c42006-07-03 00:25:40 -07005500# define sched_domain_debug(sd, cpu) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501#endif
5502
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07005503static int sd_degenerate(struct sched_domain *sd)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005504{
5505 if (cpus_weight(sd->span) == 1)
5506 return 1;
5507
5508 /* Following flags need at least 2 groups */
5509 if (sd->flags & (SD_LOAD_BALANCE |
5510 SD_BALANCE_NEWIDLE |
5511 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005512 SD_BALANCE_EXEC |
5513 SD_SHARE_CPUPOWER |
5514 SD_SHARE_PKG_RESOURCES)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005515 if (sd->groups != sd->groups->next)
5516 return 0;
5517 }
5518
5519 /* Following flags don't use groups */
5520 if (sd->flags & (SD_WAKE_IDLE |
5521 SD_WAKE_AFFINE |
5522 SD_WAKE_BALANCE))
5523 return 0;
5524
5525 return 1;
5526}
5527
Ingo Molnar48f24c42006-07-03 00:25:40 -07005528static int
5529sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
Suresh Siddha245af2c2005-06-25 14:57:25 -07005530{
5531 unsigned long cflags = sd->flags, pflags = parent->flags;
5532
5533 if (sd_degenerate(parent))
5534 return 1;
5535
5536 if (!cpus_equal(sd->span, parent->span))
5537 return 0;
5538
5539 /* Does parent contain flags not in child? */
5540 /* WAKE_BALANCE is a subset of WAKE_AFFINE */
5541 if (cflags & SD_WAKE_AFFINE)
5542 pflags &= ~SD_WAKE_BALANCE;
5543 /* Flags needing groups don't count if only 1 group in parent */
5544 if (parent->groups == parent->groups->next) {
5545 pflags &= ~(SD_LOAD_BALANCE |
5546 SD_BALANCE_NEWIDLE |
5547 SD_BALANCE_FORK |
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005548 SD_BALANCE_EXEC |
5549 SD_SHARE_CPUPOWER |
5550 SD_SHARE_PKG_RESOURCES);
Suresh Siddha245af2c2005-06-25 14:57:25 -07005551 }
5552 if (~cflags & pflags)
5553 return 0;
5554
5555 return 1;
5556}
5557
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558/*
5559 * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
5560 * hold the hotplug lock.
5561 */
John Hawkes9c1cfda2005-09-06 15:18:14 -07005562static void cpu_attach_domain(struct sched_domain *sd, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563{
Ingo Molnar70b97a72006-07-03 00:25:42 -07005564 struct rq *rq = cpu_rq(cpu);
Suresh Siddha245af2c2005-06-25 14:57:25 -07005565 struct sched_domain *tmp;
5566
5567 /* Remove the sched domains which do not contribute to scheduling. */
5568 for (tmp = sd; tmp; tmp = tmp->parent) {
5569 struct sched_domain *parent = tmp->parent;
5570 if (!parent)
5571 break;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005572 if (sd_parent_degenerate(tmp, parent)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005573 tmp->parent = parent->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005574 if (parent->parent)
5575 parent->parent->child = tmp;
5576 }
Suresh Siddha245af2c2005-06-25 14:57:25 -07005577 }
5578
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005579 if (sd && sd_degenerate(sd)) {
Suresh Siddha245af2c2005-06-25 14:57:25 -07005580 sd = sd->parent;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005581 if (sd)
5582 sd->child = NULL;
5583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584
5585 sched_domain_debug(sd, cpu);
5586
Nick Piggin674311d2005-06-25 14:57:27 -07005587 rcu_assign_pointer(rq->sd, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588}
5589
5590/* cpus with isolated domains */
Tim Chen67af63a2006-12-22 01:07:50 -08005591static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
5593/* Setup the mask of cpus configured for isolated domains */
5594static int __init isolated_cpu_setup(char *str)
5595{
5596 int ints[NR_CPUS], i;
5597
5598 str = get_options(str, ARRAY_SIZE(ints), ints);
5599 cpus_clear(cpu_isolated_map);
5600 for (i = 1; i <= ints[0]; i++)
5601 if (ints[i] < NR_CPUS)
5602 cpu_set(ints[i], cpu_isolated_map);
5603 return 1;
5604}
5605
5606__setup ("isolcpus=", isolated_cpu_setup);
5607
5608/*
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005609 * init_sched_build_groups takes the cpumask we wish to span, and a pointer
5610 * to a function which identifies what group(along with sched group) a CPU
5611 * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
5612 * (due to the fact that we keep track of groups covered with a cpumask_t).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 *
5614 * init_sched_build_groups will build a circular linked list of the groups
5615 * covered by the given span, and will set each group's ->cpumask correctly,
5616 * and ->cpu_power to 0.
5617 */
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005618static void
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005619init_sched_build_groups(cpumask_t span, const cpumask_t *cpu_map,
5620 int (*group_fn)(int cpu, const cpumask_t *cpu_map,
5621 struct sched_group **sg))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622{
5623 struct sched_group *first = NULL, *last = NULL;
5624 cpumask_t covered = CPU_MASK_NONE;
5625 int i;
5626
5627 for_each_cpu_mask(i, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005628 struct sched_group *sg;
5629 int group = group_fn(i, cpu_map, &sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 int j;
5631
5632 if (cpu_isset(i, covered))
5633 continue;
5634
5635 sg->cpumask = CPU_MASK_NONE;
Eric Dumazet5517d862007-05-08 00:32:57 -07005636 sg->__cpu_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637
5638 for_each_cpu_mask(j, span) {
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005639 if (group_fn(j, cpu_map, NULL) != group)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 continue;
5641
5642 cpu_set(j, covered);
5643 cpu_set(j, sg->cpumask);
5644 }
5645 if (!first)
5646 first = sg;
5647 if (last)
5648 last->next = sg;
5649 last = sg;
5650 }
5651 last->next = first;
5652}
5653
John Hawkes9c1cfda2005-09-06 15:18:14 -07005654#define SD_NODES_PER_DOMAIN 16
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655
John Hawkes9c1cfda2005-09-06 15:18:14 -07005656#ifdef CONFIG_NUMA
akpm@osdl.org198e2f12006-01-12 01:05:30 -08005657
John Hawkes9c1cfda2005-09-06 15:18:14 -07005658/**
5659 * find_next_best_node - find the next node to include in a sched_domain
5660 * @node: node whose sched_domain we're building
5661 * @used_nodes: nodes already in the sched_domain
5662 *
5663 * Find the next node to include in a given scheduling domain. Simply
5664 * finds the closest node not already in the @used_nodes map.
5665 *
5666 * Should use nodemask_t.
5667 */
5668static int find_next_best_node(int node, unsigned long *used_nodes)
5669{
5670 int i, n, val, min_val, best_node = 0;
5671
5672 min_val = INT_MAX;
5673
5674 for (i = 0; i < MAX_NUMNODES; i++) {
5675 /* Start at @node */
5676 n = (node + i) % MAX_NUMNODES;
5677
5678 if (!nr_cpus_node(n))
5679 continue;
5680
5681 /* Skip already used nodes */
5682 if (test_bit(n, used_nodes))
5683 continue;
5684
5685 /* Simple min distance search */
5686 val = node_distance(node, n);
5687
5688 if (val < min_val) {
5689 min_val = val;
5690 best_node = n;
5691 }
5692 }
5693
5694 set_bit(best_node, used_nodes);
5695 return best_node;
5696}
5697
5698/**
5699 * sched_domain_node_span - get a cpumask for a node's sched_domain
5700 * @node: node whose cpumask we're constructing
5701 * @size: number of nodes to include in this span
5702 *
5703 * Given a node, construct a good cpumask for its sched_domain to span. It
5704 * should be one that prevents unnecessary balancing, but also spreads tasks
5705 * out optimally.
5706 */
5707static cpumask_t sched_domain_node_span(int node)
5708{
John Hawkes9c1cfda2005-09-06 15:18:14 -07005709 DECLARE_BITMAP(used_nodes, MAX_NUMNODES);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005710 cpumask_t span, nodemask;
5711 int i;
John Hawkes9c1cfda2005-09-06 15:18:14 -07005712
5713 cpus_clear(span);
5714 bitmap_zero(used_nodes, MAX_NUMNODES);
5715
5716 nodemask = node_to_cpumask(node);
5717 cpus_or(span, span, nodemask);
5718 set_bit(node, used_nodes);
5719
5720 for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
5721 int next_node = find_next_best_node(node, used_nodes);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005722
John Hawkes9c1cfda2005-09-06 15:18:14 -07005723 nodemask = node_to_cpumask(next_node);
5724 cpus_or(span, span, nodemask);
5725 }
5726
5727 return span;
5728}
5729#endif
5730
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07005731int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005732
John Hawkes9c1cfda2005-09-06 15:18:14 -07005733/*
Ingo Molnar48f24c42006-07-03 00:25:40 -07005734 * SMT sched-domains:
John Hawkes9c1cfda2005-09-06 15:18:14 -07005735 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736#ifdef CONFIG_SCHED_SMT
5737static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005738static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005739
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005740static int cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map,
5741 struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005743 if (sg)
5744 *sg = &per_cpu(sched_group_cpus, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 return cpu;
5746}
5747#endif
5748
Ingo Molnar48f24c42006-07-03 00:25:40 -07005749/*
5750 * multi-core sched-domains:
5751 */
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005752#ifdef CONFIG_SCHED_MC
5753static DEFINE_PER_CPU(struct sched_domain, core_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005754static DEFINE_PER_CPU(struct sched_group, sched_group_core);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005755#endif
5756
5757#if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005758static int cpu_to_core_group(int cpu, const cpumask_t *cpu_map,
5759 struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005760{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005761 int group;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005762 cpumask_t mask = cpu_sibling_map[cpu];
5763 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005764 group = first_cpu(mask);
5765 if (sg)
5766 *sg = &per_cpu(sched_group_core, group);
5767 return group;
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005768}
5769#elif defined(CONFIG_SCHED_MC)
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005770static int cpu_to_core_group(int cpu, const cpumask_t *cpu_map,
5771 struct sched_group **sg)
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005772{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005773 if (sg)
5774 *sg = &per_cpu(sched_group_core, cpu);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005775 return cpu;
5776}
5777#endif
5778
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779static DEFINE_PER_CPU(struct sched_domain, phys_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005780static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005781
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005782static int cpu_to_phys_group(int cpu, const cpumask_t *cpu_map,
5783 struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005785 int group;
Ingo Molnar48f24c42006-07-03 00:25:40 -07005786#ifdef CONFIG_SCHED_MC
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005787 cpumask_t mask = cpu_coregroup_map(cpu);
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005788 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005789 group = first_cpu(mask);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08005790#elif defined(CONFIG_SCHED_SMT)
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005791 cpumask_t mask = cpu_sibling_map[cpu];
5792 cpus_and(mask, mask, *cpu_map);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005793 group = first_cpu(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794#else
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005795 group = cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796#endif
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005797 if (sg)
5798 *sg = &per_cpu(sched_group_phys, group);
5799 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800}
5801
5802#ifdef CONFIG_NUMA
John Hawkes9c1cfda2005-09-06 15:18:14 -07005803/*
5804 * The init_sched_build_groups can't handle what we want to do with node
5805 * groups, so roll our own. Now each node has its own list of groups which
5806 * gets dynamically allocated.
5807 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808static DEFINE_PER_CPU(struct sched_domain, node_domains);
John Hawkesd1b55132005-09-06 15:18:14 -07005809static struct sched_group **sched_group_nodes_bycpu[NR_CPUS];
John Hawkes9c1cfda2005-09-06 15:18:14 -07005810
5811static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005812static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
John Hawkes9c1cfda2005-09-06 15:18:14 -07005813
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005814static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
5815 struct sched_group **sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816{
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005817 cpumask_t nodemask = node_to_cpumask(cpu_to_node(cpu));
5818 int group;
5819
5820 cpus_and(nodemask, nodemask, *cpu_map);
5821 group = first_cpu(nodemask);
5822
5823 if (sg)
5824 *sg = &per_cpu(sched_group_allnodes, group);
5825 return group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826}
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005827
Siddha, Suresh B08069032006-03-27 01:15:23 -08005828static void init_numa_sched_groups_power(struct sched_group *group_head)
5829{
5830 struct sched_group *sg = group_head;
5831 int j;
5832
5833 if (!sg)
5834 return;
5835next_sg:
5836 for_each_cpu_mask(j, sg->cpumask) {
5837 struct sched_domain *sd;
5838
5839 sd = &per_cpu(phys_domains, j);
5840 if (j != first_cpu(sd->groups->cpumask)) {
5841 /*
5842 * Only add "power" once for each
5843 * physical package.
5844 */
5845 continue;
5846 }
5847
Eric Dumazet5517d862007-05-08 00:32:57 -07005848 sg_inc_cpu_power(sg, sd->groups->__cpu_power);
Siddha, Suresh B08069032006-03-27 01:15:23 -08005849 }
5850 sg = sg->next;
5851 if (sg != group_head)
5852 goto next_sg;
5853}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854#endif
5855
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005856#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005857/* Free memory allocated for various sched_group structures */
5858static void free_sched_groups(const cpumask_t *cpu_map)
5859{
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005860 int cpu, i;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005861
5862 for_each_cpu_mask(cpu, *cpu_map) {
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005863 struct sched_group **sched_group_nodes
5864 = sched_group_nodes_bycpu[cpu];
5865
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005866 if (!sched_group_nodes)
5867 continue;
5868
5869 for (i = 0; i < MAX_NUMNODES; i++) {
5870 cpumask_t nodemask = node_to_cpumask(i);
5871 struct sched_group *oldsg, *sg = sched_group_nodes[i];
5872
5873 cpus_and(nodemask, nodemask, *cpu_map);
5874 if (cpus_empty(nodemask))
5875 continue;
5876
5877 if (sg == NULL)
5878 continue;
5879 sg = sg->next;
5880next_sg:
5881 oldsg = sg;
5882 sg = sg->next;
5883 kfree(oldsg);
5884 if (oldsg != sched_group_nodes[i])
5885 goto next_sg;
5886 }
5887 kfree(sched_group_nodes);
5888 sched_group_nodes_bycpu[cpu] = NULL;
5889 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005890}
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07005891#else
5892static void free_sched_groups(const cpumask_t *cpu_map)
5893{
5894}
5895#endif
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005896
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897/*
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005898 * Initialize sched groups cpu_power.
5899 *
5900 * cpu_power indicates the capacity of sched group, which is used while
5901 * distributing the load between different sched groups in a sched domain.
5902 * Typically cpu_power for all the groups in a sched domain will be same unless
5903 * there are asymmetries in the topology. If there are asymmetries, group
5904 * having more cpu_power will pickup more load compared to the group having
5905 * less cpu_power.
5906 *
5907 * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
5908 * the maximum number of tasks a group can handle in the presence of other idle
5909 * or lightly loaded groups in the same sched domain.
5910 */
5911static void init_sched_groups_power(int cpu, struct sched_domain *sd)
5912{
5913 struct sched_domain *child;
5914 struct sched_group *group;
5915
5916 WARN_ON(!sd || !sd->groups);
5917
5918 if (cpu != first_cpu(sd->groups->cpumask))
5919 return;
5920
5921 child = sd->child;
5922
Eric Dumazet5517d862007-05-08 00:32:57 -07005923 sd->groups->__cpu_power = 0;
5924
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005925 /*
5926 * For perf policy, if the groups in child domain share resources
5927 * (for example cores sharing some portions of the cache hierarchy
5928 * or SMT), then set this domain groups cpu_power such that each group
5929 * can handle only one task, when there are other idle groups in the
5930 * same sched domain.
5931 */
5932 if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
5933 (child->flags &
5934 (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
Eric Dumazet5517d862007-05-08 00:32:57 -07005935 sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005936 return;
5937 }
5938
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005939 /*
5940 * add cpu_power of each child group to this groups cpu_power
5941 */
5942 group = child->groups;
5943 do {
Eric Dumazet5517d862007-05-08 00:32:57 -07005944 sg_inc_cpu_power(sd->groups, group->__cpu_power);
Siddha, Suresh B89c47102006-10-03 01:14:09 -07005945 group = group->next;
5946 } while (group != child->groups);
5947}
5948
5949/*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07005950 * Build sched domains for a given set of cpus and attach the sched domains
5951 * to the individual cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005953static int build_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954{
5955 int i;
John Hawkesd1b55132005-09-06 15:18:14 -07005956#ifdef CONFIG_NUMA
5957 struct sched_group **sched_group_nodes = NULL;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005958 int sd_allnodes = 0;
John Hawkesd1b55132005-09-06 15:18:14 -07005959
5960 /*
5961 * Allocate the per-node list of sched groups
5962 */
Ingo Molnardd41f592007-07-09 18:51:59 +02005963 sched_group_nodes = kzalloc(sizeof(struct sched_group *)*MAX_NUMNODES,
Srivatsa Vaddagirid3a5aa92006-06-27 02:54:39 -07005964 GFP_KERNEL);
John Hawkesd1b55132005-09-06 15:18:14 -07005965 if (!sched_group_nodes) {
5966 printk(KERN_WARNING "Can not alloc sched group node list\n");
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07005967 return -ENOMEM;
John Hawkesd1b55132005-09-06 15:18:14 -07005968 }
5969 sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
5970#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971
5972 /*
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07005973 * Set up domains for cpus specified by the cpu_map.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07005975 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 struct sched_domain *sd = NULL, *p;
5977 cpumask_t nodemask = node_to_cpumask(cpu_to_node(i));
5978
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07005979 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980
5981#ifdef CONFIG_NUMA
Ingo Molnardd41f592007-07-09 18:51:59 +02005982 if (cpus_weight(*cpu_map) >
5983 SD_NODES_PER_DOMAIN*cpus_weight(nodemask)) {
John Hawkes9c1cfda2005-09-06 15:18:14 -07005984 sd = &per_cpu(allnodes_domains, i);
5985 *sd = SD_ALLNODES_INIT;
5986 sd->span = *cpu_map;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005987 cpu_to_allnodes_group(i, cpu_map, &sd->groups);
John Hawkes9c1cfda2005-09-06 15:18:14 -07005988 p = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08005989 sd_allnodes = 1;
John Hawkes9c1cfda2005-09-06 15:18:14 -07005990 } else
5991 p = NULL;
5992
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 sd = &per_cpu(node_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994 *sd = SD_NODE_INIT;
John Hawkes9c1cfda2005-09-06 15:18:14 -07005995 sd->span = sched_domain_node_span(cpu_to_node(i));
5996 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07005997 if (p)
5998 p->child = sd;
John Hawkes9c1cfda2005-09-06 15:18:14 -07005999 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000#endif
6001
6002 p = sd;
6003 sd = &per_cpu(phys_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 *sd = SD_CPU_INIT;
6005 sd->span = nodemask;
6006 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006007 if (p)
6008 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006009 cpu_to_phys_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006011#ifdef CONFIG_SCHED_MC
6012 p = sd;
6013 sd = &per_cpu(core_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006014 *sd = SD_MC_INIT;
6015 sd->span = cpu_coregroup_map(i);
6016 cpus_and(sd->span, sd->span, *cpu_map);
6017 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006018 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006019 cpu_to_core_group(i, cpu_map, &sd->groups);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006020#endif
6021
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022#ifdef CONFIG_SCHED_SMT
6023 p = sd;
6024 sd = &per_cpu(cpu_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 *sd = SD_SIBLING_INIT;
6026 sd->span = cpu_sibling_map[i];
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006027 cpus_and(sd->span, sd->span, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 sd->parent = p;
Siddha, Suresh B1a848872006-10-03 01:14:08 -07006029 p->child = sd;
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006030 cpu_to_cpu_group(i, cpu_map, &sd->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031#endif
6032 }
6033
6034#ifdef CONFIG_SCHED_SMT
6035 /* Set up CPU (sibling) groups */
John Hawkes9c1cfda2005-09-06 15:18:14 -07006036 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 cpumask_t this_sibling_map = cpu_sibling_map[i];
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006038 cpus_and(this_sibling_map, this_sibling_map, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 if (i != first_cpu(this_sibling_map))
6040 continue;
6041
Ingo Molnardd41f592007-07-09 18:51:59 +02006042 init_sched_build_groups(this_sibling_map, cpu_map,
6043 &cpu_to_cpu_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 }
6045#endif
6046
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006047#ifdef CONFIG_SCHED_MC
6048 /* Set up multi-core groups */
6049 for_each_cpu_mask(i, *cpu_map) {
6050 cpumask_t this_core_map = cpu_coregroup_map(i);
6051 cpus_and(this_core_map, this_core_map, *cpu_map);
6052 if (i != first_cpu(this_core_map))
6053 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02006054 init_sched_build_groups(this_core_map, cpu_map,
6055 &cpu_to_core_group);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006056 }
6057#endif
6058
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 /* Set up physical groups */
6060 for (i = 0; i < MAX_NUMNODES; i++) {
6061 cpumask_t nodemask = node_to_cpumask(i);
6062
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006063 cpus_and(nodemask, nodemask, *cpu_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 if (cpus_empty(nodemask))
6065 continue;
6066
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006067 init_sched_build_groups(nodemask, cpu_map, &cpu_to_phys_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 }
6069
6070#ifdef CONFIG_NUMA
6071 /* Set up node groups */
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006072 if (sd_allnodes)
Ingo Molnardd41f592007-07-09 18:51:59 +02006073 init_sched_build_groups(*cpu_map, cpu_map,
6074 &cpu_to_allnodes_group);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006075
6076 for (i = 0; i < MAX_NUMNODES; i++) {
6077 /* Set up node groups */
6078 struct sched_group *sg, *prev;
6079 cpumask_t nodemask = node_to_cpumask(i);
6080 cpumask_t domainspan;
6081 cpumask_t covered = CPU_MASK_NONE;
6082 int j;
6083
6084 cpus_and(nodemask, nodemask, *cpu_map);
John Hawkesd1b55132005-09-06 15:18:14 -07006085 if (cpus_empty(nodemask)) {
6086 sched_group_nodes[i] = NULL;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006087 continue;
John Hawkesd1b55132005-09-06 15:18:14 -07006088 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006089
6090 domainspan = sched_domain_node_span(i);
6091 cpus_and(domainspan, domainspan, *cpu_map);
6092
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006093 sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006094 if (!sg) {
6095 printk(KERN_WARNING "Can not alloc domain group for "
6096 "node %d\n", i);
6097 goto error;
6098 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006099 sched_group_nodes[i] = sg;
6100 for_each_cpu_mask(j, nodemask) {
6101 struct sched_domain *sd;
Ingo Molnar9761eea2007-07-09 18:52:00 +02006102
John Hawkes9c1cfda2005-09-06 15:18:14 -07006103 sd = &per_cpu(node_domains, j);
6104 sd->groups = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006105 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006106 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006107 sg->cpumask = nodemask;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006108 sg->next = sg;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006109 cpus_or(covered, covered, nodemask);
6110 prev = sg;
6111
6112 for (j = 0; j < MAX_NUMNODES; j++) {
6113 cpumask_t tmp, notcovered;
6114 int n = (i + j) % MAX_NUMNODES;
6115
6116 cpus_complement(notcovered, covered);
6117 cpus_and(tmp, notcovered, *cpu_map);
6118 cpus_and(tmp, tmp, domainspan);
6119 if (cpus_empty(tmp))
6120 break;
6121
6122 nodemask = node_to_cpumask(n);
6123 cpus_and(tmp, tmp, nodemask);
6124 if (cpus_empty(tmp))
6125 continue;
6126
Srivatsa Vaddagiri15f0b672006-06-27 02:54:40 -07006127 sg = kmalloc_node(sizeof(struct sched_group),
6128 GFP_KERNEL, i);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006129 if (!sg) {
6130 printk(KERN_WARNING
6131 "Can not alloc domain group for node %d\n", j);
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006132 goto error;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006133 }
Eric Dumazet5517d862007-05-08 00:32:57 -07006134 sg->__cpu_power = 0;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006135 sg->cpumask = tmp;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006136 sg->next = prev->next;
John Hawkes9c1cfda2005-09-06 15:18:14 -07006137 cpus_or(covered, covered, tmp);
6138 prev->next = sg;
6139 prev = sg;
6140 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142#endif
6143
6144 /* Calculate CPU power for physical packages and nodes */
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006145#ifdef CONFIG_SCHED_SMT
6146 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006147 struct sched_domain *sd = &per_cpu(cpu_domains, i);
6148
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006149 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006150 }
6151#endif
6152#ifdef CONFIG_SCHED_MC
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006153 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006154 struct sched_domain *sd = &per_cpu(core_domains, i);
6155
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006156 init_sched_groups_power(i, sd);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006157 }
6158#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006160 for_each_cpu_mask(i, *cpu_map) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006161 struct sched_domain *sd = &per_cpu(phys_domains, i);
6162
Siddha, Suresh B89c47102006-10-03 01:14:09 -07006163 init_sched_groups_power(i, sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 }
6165
John Hawkes9c1cfda2005-09-06 15:18:14 -07006166#ifdef CONFIG_NUMA
Siddha, Suresh B08069032006-03-27 01:15:23 -08006167 for (i = 0; i < MAX_NUMNODES; i++)
6168 init_numa_sched_groups_power(sched_group_nodes[i]);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006169
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006170 if (sd_allnodes) {
6171 struct sched_group *sg;
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07006172
Siddha, Suresh B6711cab2006-12-10 02:20:07 -08006173 cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg);
Siddha, Suresh Bf712c0c2006-07-30 03:02:59 -07006174 init_numa_sched_groups_power(sg);
6175 }
John Hawkes9c1cfda2005-09-06 15:18:14 -07006176#endif
6177
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178 /* Attach the domains */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006179 for_each_cpu_mask(i, *cpu_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180 struct sched_domain *sd;
6181#ifdef CONFIG_SCHED_SMT
6182 sd = &per_cpu(cpu_domains, i);
Siddha, Suresh B1e9f28f2006-03-27 01:15:22 -08006183#elif defined(CONFIG_SCHED_MC)
6184 sd = &per_cpu(core_domains, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185#else
6186 sd = &per_cpu(phys_domains, i);
6187#endif
6188 cpu_attach_domain(sd, i);
6189 }
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006190
6191 return 0;
6192
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006193#ifdef CONFIG_NUMA
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006194error:
6195 free_sched_groups(cpu_map);
6196 return -ENOMEM;
Siddha, Suresh Ba6160582006-10-03 01:14:06 -07006197#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198}
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006199/*
6200 * Set up scheduler domains and groups. Callers must hold the hotplug lock.
6201 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006202static int arch_init_sched_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006203{
6204 cpumask_t cpu_default_map;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006205 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006207 /*
6208 * Setup mask for cpus without special case scheduling requirements.
6209 * For now this just excludes isolated cpus, but could be used to
6210 * exclude other special cases in the future.
6211 */
6212 cpus_andnot(cpu_default_map, *cpu_map, cpu_isolated_map);
6213
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006214 err = build_sched_domains(&cpu_default_map);
6215
6216 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006217}
6218
6219static void arch_destroy_sched_domains(const cpumask_t *cpu_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220{
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006221 free_sched_groups(cpu_map);
John Hawkes9c1cfda2005-09-06 15:18:14 -07006222}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006224/*
6225 * Detach sched domains from a group of cpus specified in cpu_map
6226 * These cpus will now be attached to the NULL domain
6227 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08006228static void detach_destroy_domains(const cpumask_t *cpu_map)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006229{
6230 int i;
6231
6232 for_each_cpu_mask(i, *cpu_map)
6233 cpu_attach_domain(NULL, i);
6234 synchronize_sched();
6235 arch_destroy_sched_domains(cpu_map);
6236}
6237
6238/*
6239 * Partition sched domains as specified by the cpumasks below.
6240 * This attaches all cpus from the cpumasks to the NULL domain,
6241 * waits for a RCU quiescent period, recalculates sched
6242 * domain information and then attaches them back to the
6243 * correct sched domains
6244 * Call with hotplug lock held
6245 */
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006246int partition_sched_domains(cpumask_t *partition1, cpumask_t *partition2)
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006247{
6248 cpumask_t change_map;
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006249 int err = 0;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006250
6251 cpus_and(*partition1, *partition1, cpu_online_map);
6252 cpus_and(*partition2, *partition2, cpu_online_map);
6253 cpus_or(change_map, *partition1, *partition2);
6254
6255 /* Detach sched domains from all of the affected cpus */
6256 detach_destroy_domains(&change_map);
6257 if (!cpus_empty(*partition1))
Srivatsa Vaddagiri51888ca2006-06-27 02:54:38 -07006258 err = build_sched_domains(partition1);
6259 if (!err && !cpus_empty(*partition2))
6260 err = build_sched_domains(partition2);
6261
6262 return err;
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006263}
6264
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006265#if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
Adrian Bunk6707de002007-08-12 18:08:19 +02006266static int arch_reinit_sched_domains(void)
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006267{
6268 int err;
6269
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006270 mutex_lock(&sched_hotcpu_mutex);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006271 detach_destroy_domains(&cpu_online_map);
6272 err = arch_init_sched_domains(&cpu_online_map);
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006273 mutex_unlock(&sched_hotcpu_mutex);
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006274
6275 return err;
6276}
6277
6278static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
6279{
6280 int ret;
6281
6282 if (buf[0] != '0' && buf[0] != '1')
6283 return -EINVAL;
6284
6285 if (smt)
6286 sched_smt_power_savings = (buf[0] == '1');
6287 else
6288 sched_mc_power_savings = (buf[0] == '1');
6289
6290 ret = arch_reinit_sched_domains();
6291
6292 return ret ? ret : count;
6293}
6294
Adrian Bunk6707de002007-08-12 18:08:19 +02006295#ifdef CONFIG_SCHED_MC
6296static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
6297{
6298 return sprintf(page, "%u\n", sched_mc_power_savings);
6299}
6300static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
6301 const char *buf, size_t count)
6302{
6303 return sched_power_savings_store(buf, count, 0);
6304}
6305static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
6306 sched_mc_power_savings_store);
6307#endif
6308
6309#ifdef CONFIG_SCHED_SMT
6310static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
6311{
6312 return sprintf(page, "%u\n", sched_smt_power_savings);
6313}
6314static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
6315 const char *buf, size_t count)
6316{
6317 return sched_power_savings_store(buf, count, 1);
6318}
6319static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
6320 sched_smt_power_savings_store);
6321#endif
6322
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006323int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
6324{
6325 int err = 0;
Ingo Molnar48f24c42006-07-03 00:25:40 -07006326
Siddha, Suresh B5c45bf22006-06-27 02:54:42 -07006327#ifdef CONFIG_SCHED_SMT
6328 if (smt_capable())
6329 err = sysfs_create_file(&cls->kset.kobj,
6330 &attr_sched_smt_power_savings.attr);
6331#endif
6332#ifdef CONFIG_SCHED_MC
6333 if (!err && mc_capable())
6334 err = sysfs_create_file(&cls->kset.kobj,
6335 &attr_sched_mc_power_savings.attr);
6336#endif
6337 return err;
6338}
6339#endif
6340
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341/*
6342 * Force a reinitialization of the sched domains hierarchy. The domains
6343 * and groups cannot be updated in place without racing with the balancing
Nick Piggin41c7ce92005-06-25 14:57:24 -07006344 * code, so we temporarily attach all running cpus to the NULL domain
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 * which will prevent rebalancing while the sched domains are recalculated.
6346 */
6347static int update_sched_domains(struct notifier_block *nfb,
6348 unsigned long action, void *hcpu)
6349{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 switch (action) {
6351 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006352 case CPU_UP_PREPARE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006354 case CPU_DOWN_PREPARE_FROZEN:
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006355 detach_destroy_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356 return NOTIFY_OK;
6357
6358 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006359 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006361 case CPU_DOWN_FAILED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006363 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07006365 case CPU_DEAD_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366 /*
6367 * Fall through and re-initialise the domains.
6368 */
6369 break;
6370 default:
6371 return NOTIFY_DONE;
6372 }
6373
6374 /* The hotplug lock is already held by cpu_up/cpu_down */
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006375 arch_init_sched_domains(&cpu_online_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376
6377 return NOTIFY_OK;
6378}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379
6380void __init sched_init_smp(void)
6381{
Nick Piggin5c1e1762006-10-03 01:14:04 -07006382 cpumask_t non_isolated_cpus;
6383
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006384 mutex_lock(&sched_hotcpu_mutex);
Dinakar Guniguntala1a20ff22005-06-25 14:57:33 -07006385 arch_init_sched_domains(&cpu_online_map);
Nathan Lynche5e56732007-01-10 23:15:28 -08006386 cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
Nick Piggin5c1e1762006-10-03 01:14:04 -07006387 if (cpus_empty(non_isolated_cpus))
6388 cpu_set(smp_processor_id(), non_isolated_cpus);
Gautham R Shenoy5be93612007-05-09 02:34:04 -07006389 mutex_unlock(&sched_hotcpu_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390 /* XXX: Theoretical race here - CPU may be hotplugged now */
6391 hotcpu_notifier(update_sched_domains, 0);
Nick Piggin5c1e1762006-10-03 01:14:04 -07006392
Nick Piggine692ab52007-07-26 13:40:43 +02006393 init_sched_domain_sysctl();
6394
Nick Piggin5c1e1762006-10-03 01:14:04 -07006395 /* Move init over to a non-isolated CPU */
6396 if (set_cpus_allowed(current, non_isolated_cpus) < 0)
6397 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398}
6399#else
6400void __init sched_init_smp(void)
6401{
6402}
6403#endif /* CONFIG_SMP */
6404
6405int in_sched_functions(unsigned long addr)
6406{
6407 /* Linker adds these: start and end of __sched functions */
6408 extern char __sched_text_start[], __sched_text_end[];
Ingo Molnar48f24c42006-07-03 00:25:40 -07006409
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 return in_lock_functions(addr) ||
6411 (addr >= (unsigned long)__sched_text_start
6412 && addr < (unsigned long)__sched_text_end);
6413}
6414
Ingo Molnardd41f592007-07-09 18:51:59 +02006415static inline void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
6416{
6417 cfs_rq->tasks_timeline = RB_ROOT;
Ingo Molnardd41f592007-07-09 18:51:59 +02006418#ifdef CONFIG_FAIR_GROUP_SCHED
6419 cfs_rq->rq = rq;
6420#endif
6421}
6422
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423void __init sched_init(void)
6424{
Christoph Lameter476f3532007-05-06 14:48:58 -07006425 int highest_cpu = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02006426 int i, j;
6427
6428 /*
6429 * Link up the scheduling class hierarchy:
6430 */
6431 rt_sched_class.next = &fair_sched_class;
6432 fair_sched_class.next = &idle_sched_class;
6433 idle_sched_class.next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08006435 for_each_possible_cpu(i) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006436 struct rt_prio_array *array;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006437 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438
6439 rq = cpu_rq(i);
6440 spin_lock_init(&rq->lock);
Ingo Molnarfcb99372006-07-03 00:25:10 -07006441 lockdep_set_class(&rq->lock, &rq->rq_lock_key);
Nick Piggin78979862005-06-25 14:57:13 -07006442 rq->nr_running = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02006443 rq->clock = 1;
6444 init_cfs_rq(&rq->cfs, rq);
6445#ifdef CONFIG_FAIR_GROUP_SCHED
6446 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
6447 list_add(&rq->cfs.leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
6448#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449
Ingo Molnardd41f592007-07-09 18:51:59 +02006450 for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
6451 rq->cpu_load[j] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452#ifdef CONFIG_SMP
Nick Piggin41c7ce92005-06-25 14:57:24 -07006453 rq->sd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454 rq->active_balance = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02006455 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456 rq->push_cpu = 0;
Christoph Lameter0a2966b2006-09-25 23:30:51 -07006457 rq->cpu = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458 rq->migration_thread = NULL;
6459 INIT_LIST_HEAD(&rq->migration_queue);
6460#endif
6461 atomic_set(&rq->nr_iowait, 0);
6462
Ingo Molnardd41f592007-07-09 18:51:59 +02006463 array = &rq->rt.active;
6464 for (j = 0; j < MAX_RT_PRIO; j++) {
6465 INIT_LIST_HEAD(array->queue + j);
6466 __clear_bit(j, array->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467 }
Christoph Lameter476f3532007-05-06 14:48:58 -07006468 highest_cpu = i;
Ingo Molnardd41f592007-07-09 18:51:59 +02006469 /* delimiter for bitsearch: */
6470 __set_bit(MAX_RT_PRIO, array->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 }
6472
Peter Williams2dd73a42006-06-27 02:54:34 -07006473 set_load_weight(&init_task);
Heiko Carstensb50f60c2006-07-30 03:03:52 -07006474
Avi Kivitye107be32007-07-26 13:40:43 +02006475#ifdef CONFIG_PREEMPT_NOTIFIERS
6476 INIT_HLIST_HEAD(&init_task.preempt_notifiers);
6477#endif
6478
Christoph Lameterc9819f42006-12-10 02:20:25 -08006479#ifdef CONFIG_SMP
Christoph Lameter476f3532007-05-06 14:48:58 -07006480 nr_cpu_ids = highest_cpu + 1;
Christoph Lameterc9819f42006-12-10 02:20:25 -08006481 open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
6482#endif
6483
Heiko Carstensb50f60c2006-07-30 03:03:52 -07006484#ifdef CONFIG_RT_MUTEXES
6485 plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
6486#endif
6487
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488 /*
6489 * The boot idle thread does lazy MMU switching as well:
6490 */
6491 atomic_inc(&init_mm.mm_count);
6492 enter_lazy_tlb(&init_mm, current);
6493
6494 /*
6495 * Make us the idle thread. Technically, schedule() should not be
6496 * called from this thread, however somewhere below it might be,
6497 * but because we are the idle thread, we just pick up running again
6498 * when this runqueue becomes "idle".
6499 */
6500 init_idle(current, smp_processor_id());
Ingo Molnardd41f592007-07-09 18:51:59 +02006501 /*
6502 * During early bootup we pretend to be a normal task:
6503 */
6504 current->sched_class = &fair_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505}
6506
6507#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
6508void __might_sleep(char *file, int line)
6509{
Ingo Molnar48f24c42006-07-03 00:25:40 -07006510#ifdef in_atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511 static unsigned long prev_jiffy; /* ratelimiting */
6512
6513 if ((in_atomic() || irqs_disabled()) &&
6514 system_state == SYSTEM_RUNNING && !oops_in_progress) {
6515 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
6516 return;
6517 prev_jiffy = jiffies;
Ingo Molnar91368d72006-03-23 03:00:54 -08006518 printk(KERN_ERR "BUG: sleeping function called from invalid"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519 " context at %s:%d\n", file, line);
6520 printk("in_atomic():%d, irqs_disabled():%d\n",
6521 in_atomic(), irqs_disabled());
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08006522 debug_show_held_locks(current);
Ingo Molnar3117df02006-12-13 00:34:43 -08006523 if (irqs_disabled())
6524 print_irqtrace_events(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525 dump_stack();
6526 }
6527#endif
6528}
6529EXPORT_SYMBOL(__might_sleep);
6530#endif
6531
6532#ifdef CONFIG_MAGIC_SYSRQ
6533void normalize_rt_tasks(void)
6534{
Ingo Molnara0f98a12007-06-17 18:37:45 +02006535 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536 unsigned long flags;
Ingo Molnar70b97a72006-07-03 00:25:42 -07006537 struct rq *rq;
Ingo Molnardd41f592007-07-09 18:51:59 +02006538 int on_rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539
6540 read_lock_irq(&tasklist_lock);
Ingo Molnara0f98a12007-06-17 18:37:45 +02006541 do_each_thread(g, p) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006542 p->se.fair_key = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02006543 p->se.exec_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02006544#ifdef CONFIG_SCHEDSTATS
6545 p->se.wait_start = 0;
6546 p->se.sleep_start = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02006547 p->se.block_start = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02006548#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02006549 task_rq(p)->clock = 0;
6550
6551 if (!rt_task(p)) {
6552 /*
6553 * Renice negative nice level userspace
6554 * tasks back to 0:
6555 */
6556 if (TASK_NICE(p) < 0 && p->mm)
6557 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02006559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560
Ingo Molnarb29739f2006-06-27 02:54:51 -07006561 spin_lock_irqsave(&p->pi_lock, flags);
6562 rq = __task_rq_lock(p);
Ingo Molnardd41f592007-07-09 18:51:59 +02006563#ifdef CONFIG_SMP
6564 /*
6565 * Do not touch the migration thread:
6566 */
6567 if (p == rq->migration_thread)
6568 goto out_unlock;
6569#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570
Ingo Molnar2daa3572007-08-09 11:16:51 +02006571 update_rq_clock(rq);
Ingo Molnardd41f592007-07-09 18:51:59 +02006572 on_rq = p->se.on_rq;
Ingo Molnar2daa3572007-08-09 11:16:51 +02006573 if (on_rq)
6574 deactivate_task(rq, p, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02006575 __setscheduler(rq, p, SCHED_NORMAL, 0);
6576 if (on_rq) {
Ingo Molnar2daa3572007-08-09 11:16:51 +02006577 activate_task(rq, p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 resched_task(rq->curr);
6579 }
Ingo Molnardd41f592007-07-09 18:51:59 +02006580#ifdef CONFIG_SMP
6581 out_unlock:
6582#endif
Ingo Molnarb29739f2006-06-27 02:54:51 -07006583 __task_rq_unlock(rq);
6584 spin_unlock_irqrestore(&p->pi_lock, flags);
Ingo Molnara0f98a12007-06-17 18:37:45 +02006585 } while_each_thread(g, p);
6586
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587 read_unlock_irq(&tasklist_lock);
6588}
6589
6590#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07006591
6592#ifdef CONFIG_IA64
6593/*
6594 * These functions are only useful for the IA64 MCA handling.
6595 *
6596 * They can only be called when the whole system has been
6597 * stopped - every CPU needs to be quiescent, and no scheduling
6598 * activity can take place. Using them for anything else would
6599 * be a serious bug, and as a result, they aren't even visible
6600 * under any other configuration.
6601 */
6602
6603/**
6604 * curr_task - return the current task for a given cpu.
6605 * @cpu: the processor in question.
6606 *
6607 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
6608 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006609struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07006610{
6611 return cpu_curr(cpu);
6612}
6613
6614/**
6615 * set_curr_task - set the current task for a given cpu.
6616 * @cpu: the processor in question.
6617 * @p: the task pointer to set.
6618 *
6619 * Description: This function must only be used when non-maskable interrupts
6620 * are serviced on a separate stack. It allows the architecture to switch the
6621 * notion of the current task on a cpu in a non-blocking manner. This function
6622 * must be called with all CPU's synchronized, and interrupts disabled, the
6623 * and caller must save the original value of the current task (see
6624 * curr_task() above) and restore that value before reenabling interrupts and
6625 * re-starting the system.
6626 *
6627 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
6628 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07006629void set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07006630{
6631 cpu_curr(cpu) = p;
6632}
6633
6634#endif