]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | |
c31f2e8a IM |
19 | * 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 | |
1da177e4 LT |
25 | */ |
26 | ||
27 | #include <linux/mm.h> | |
28 | #include <linux/module.h> | |
29 | #include <linux/nmi.h> | |
30 | #include <linux/init.h> | |
dff06c15 | 31 | #include <linux/uaccess.h> |
1da177e4 LT |
32 | #include <linux/highmem.h> |
33 | #include <linux/smp_lock.h> | |
34 | #include <asm/mmu_context.h> | |
35 | #include <linux/interrupt.h> | |
c59ede7b | 36 | #include <linux/capability.h> |
1da177e4 LT |
37 | #include <linux/completion.h> |
38 | #include <linux/kernel_stat.h> | |
9a11b49a | 39 | #include <linux/debug_locks.h> |
1da177e4 LT |
40 | #include <linux/security.h> |
41 | #include <linux/notifier.h> | |
42 | #include <linux/profile.h> | |
7dfb7103 | 43 | #include <linux/freezer.h> |
198e2f18 | 44 | #include <linux/vmalloc.h> |
1da177e4 LT |
45 | #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> | |
e692ab53 | 56 | #include <linux/sysctl.h> |
1da177e4 LT |
57 | #include <linux/syscalls.h> |
58 | #include <linux/times.h> | |
8f0ab514 | 59 | #include <linux/tsacct_kern.h> |
c6fd91f0 | 60 | #include <linux/kprobes.h> |
0ff92245 | 61 | #include <linux/delayacct.h> |
5517d86b | 62 | #include <linux/reciprocal_div.h> |
dff06c15 | 63 | #include <linux/unistd.h> |
1da177e4 | 64 | |
5517d86b | 65 | #include <asm/tlb.h> |
1da177e4 | 66 | |
b035b6de AD |
67 | /* |
68 | * Scheduler clock - returns current time in nanosec units. | |
69 | * This is default implementation. | |
70 | * Architectures and sub-architectures can override this. | |
71 | */ | |
72 | unsigned long long __attribute__((weak)) sched_clock(void) | |
73 | { | |
74 | return (unsigned long long)jiffies * (1000000000 / HZ); | |
75 | } | |
76 | ||
1da177e4 LT |
77 | /* |
78 | * Convert user-nice values [ -20 ... 0 ... 19 ] | |
79 | * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ], | |
80 | * and back. | |
81 | */ | |
82 | #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20) | |
83 | #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20) | |
84 | #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio) | |
85 | ||
86 | /* | |
87 | * 'User priority' is the nice value converted to something we | |
88 | * can work with better when scaling various scheduler parameters, | |
89 | * it's a [ 0 ... 39 ] range. | |
90 | */ | |
91 | #define USER_PRIO(p) ((p)-MAX_RT_PRIO) | |
92 | #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio) | |
93 | #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO)) | |
94 | ||
95 | /* | |
96 | * Some helpers for converting nanosecond timing to jiffy resolution | |
97 | */ | |
98 | #define NS_TO_JIFFIES(TIME) ((TIME) / (1000000000 / HZ)) | |
99 | #define JIFFIES_TO_NS(TIME) ((TIME) * (1000000000 / HZ)) | |
100 | ||
6aa645ea IM |
101 | #define NICE_0_LOAD SCHED_LOAD_SCALE |
102 | #define NICE_0_SHIFT SCHED_LOAD_SHIFT | |
103 | ||
1da177e4 LT |
104 | /* |
105 | * These are the 'tuning knobs' of the scheduler: | |
106 | * | |
107 | * Minimum timeslice is 5 msecs (or 1 jiffy, whichever is larger), | |
108 | * default timeslice is 100 msecs, maximum timeslice is 800 msecs. | |
109 | * Timeslices get refilled after they expire. | |
110 | */ | |
111 | #define MIN_TIMESLICE max(5 * HZ / 1000, 1) | |
112 | #define DEF_TIMESLICE (100 * HZ / 1000) | |
2dd73a4f | 113 | |
5517d86b ED |
114 | #ifdef CONFIG_SMP |
115 | /* | |
116 | * Divide a load by a sched group cpu_power : (load / sg->__cpu_power) | |
117 | * Since cpu_power is a 'constant', we can use a reciprocal divide. | |
118 | */ | |
119 | static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load) | |
120 | { | |
121 | return reciprocal_divide(load, sg->reciprocal_cpu_power); | |
122 | } | |
123 | ||
124 | /* | |
125 | * Each time a sched group cpu_power is changed, | |
126 | * we must compute its reciprocal value | |
127 | */ | |
128 | static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val) | |
129 | { | |
130 | sg->__cpu_power += val; | |
131 | sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power); | |
132 | } | |
133 | #endif | |
134 | ||
634fa8c9 IM |
135 | #define SCALE_PRIO(x, prio) \ |
136 | max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO / 2), MIN_TIMESLICE) | |
137 | ||
91fcdd4e | 138 | /* |
634fa8c9 | 139 | * static_prio_timeslice() scales user-nice values [ -20 ... 0 ... 19 ] |
91fcdd4e | 140 | * to time slice values: [800ms ... 100ms ... 5ms] |
91fcdd4e | 141 | */ |
634fa8c9 | 142 | static unsigned int static_prio_timeslice(int static_prio) |
2dd73a4f | 143 | { |
634fa8c9 IM |
144 | if (static_prio == NICE_TO_PRIO(19)) |
145 | return 1; | |
146 | ||
147 | if (static_prio < NICE_TO_PRIO(0)) | |
148 | return SCALE_PRIO(DEF_TIMESLICE * 4, static_prio); | |
149 | else | |
150 | return SCALE_PRIO(DEF_TIMESLICE, static_prio); | |
2dd73a4f PW |
151 | } |
152 | ||
e05606d3 IM |
153 | static inline int rt_policy(int policy) |
154 | { | |
155 | if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR)) | |
156 | return 1; | |
157 | return 0; | |
158 | } | |
159 | ||
160 | static inline int task_has_rt_policy(struct task_struct *p) | |
161 | { | |
162 | return rt_policy(p->policy); | |
163 | } | |
164 | ||
1da177e4 | 165 | /* |
6aa645ea | 166 | * This is the priority-queue data structure of the RT scheduling class: |
1da177e4 | 167 | */ |
6aa645ea IM |
168 | struct rt_prio_array { |
169 | DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */ | |
170 | struct list_head queue[MAX_RT_PRIO]; | |
171 | }; | |
172 | ||
173 | struct load_stat { | |
174 | struct load_weight load; | |
175 | u64 load_update_start, load_update_last; | |
176 | unsigned long delta_fair, delta_exec, delta_stat; | |
177 | }; | |
178 | ||
179 | /* CFS-related fields in a runqueue */ | |
180 | struct cfs_rq { | |
181 | struct load_weight load; | |
182 | unsigned long nr_running; | |
183 | ||
184 | s64 fair_clock; | |
185 | u64 exec_clock; | |
186 | s64 wait_runtime; | |
187 | u64 sleeper_bonus; | |
188 | unsigned long wait_runtime_overruns, wait_runtime_underruns; | |
189 | ||
190 | struct rb_root tasks_timeline; | |
191 | struct rb_node *rb_leftmost; | |
192 | struct rb_node *rb_load_balance_curr; | |
193 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
194 | /* 'curr' points to currently running entity on this cfs_rq. | |
195 | * It is set to NULL otherwise (i.e when none are currently running). | |
196 | */ | |
197 | struct sched_entity *curr; | |
198 | struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */ | |
199 | ||
200 | /* leaf cfs_rqs are those that hold tasks (lowest schedulable entity in | |
201 | * a hierarchy). Non-leaf lrqs hold other higher schedulable entities | |
202 | * (like users, containers etc.) | |
203 | * | |
204 | * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This | |
205 | * list is used during load balance. | |
206 | */ | |
207 | struct list_head leaf_cfs_rq_list; /* Better name : task_cfs_rq_list? */ | |
208 | #endif | |
209 | }; | |
1da177e4 | 210 | |
6aa645ea IM |
211 | /* Real-Time classes' related field in a runqueue: */ |
212 | struct rt_rq { | |
213 | struct rt_prio_array active; | |
214 | int rt_load_balance_idx; | |
215 | struct list_head *rt_load_balance_head, *rt_load_balance_curr; | |
216 | }; | |
217 | ||
1da177e4 LT |
218 | /* |
219 | * This is the main, per-CPU runqueue data structure. | |
220 | * | |
221 | * Locking rule: those places that want to lock multiple runqueues | |
222 | * (such as the load balancing or the thread migration code), lock | |
223 | * acquire operations must be ordered by ascending &runqueue. | |
224 | */ | |
70b97a7f | 225 | struct rq { |
6aa645ea | 226 | spinlock_t lock; /* runqueue lock */ |
1da177e4 LT |
227 | |
228 | /* | |
229 | * nr_running and cpu_load should be in the same cacheline because | |
230 | * remote CPUs use both these fields when doing load calculation. | |
231 | */ | |
232 | unsigned long nr_running; | |
6aa645ea IM |
233 | #define CPU_LOAD_IDX_MAX 5 |
234 | unsigned long cpu_load[CPU_LOAD_IDX_MAX]; | |
bdecea3a | 235 | unsigned char idle_at_tick; |
46cb4b7c SS |
236 | #ifdef CONFIG_NO_HZ |
237 | unsigned char in_nohz_recently; | |
238 | #endif | |
6aa645ea IM |
239 | struct load_stat ls; /* capture load from *all* tasks on this cpu */ |
240 | unsigned long nr_load_updates; | |
241 | u64 nr_switches; | |
242 | ||
243 | struct cfs_rq cfs; | |
244 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
245 | struct list_head leaf_cfs_rq_list; /* list of leaf cfs_rq on this cpu */ | |
1da177e4 | 246 | #endif |
6aa645ea | 247 | struct rt_rq rt; |
1da177e4 LT |
248 | |
249 | /* | |
250 | * This is part of a global counter where only the total sum | |
251 | * over all CPUs matters. A task can increase this counter on | |
252 | * one CPU and if it got migrated afterwards it may decrease | |
253 | * it on another CPU. Always updated under the runqueue lock: | |
254 | */ | |
255 | unsigned long nr_uninterruptible; | |
256 | ||
36c8b586 | 257 | struct task_struct *curr, *idle; |
c9819f45 | 258 | unsigned long next_balance; |
1da177e4 | 259 | struct mm_struct *prev_mm; |
6aa645ea | 260 | |
6aa645ea IM |
261 | u64 clock, prev_clock_raw; |
262 | s64 clock_max_delta; | |
263 | ||
264 | unsigned int clock_warps, clock_overflows; | |
265 | unsigned int clock_unstable_events; | |
266 | ||
1da177e4 LT |
267 | atomic_t nr_iowait; |
268 | ||
269 | #ifdef CONFIG_SMP | |
270 | struct sched_domain *sd; | |
271 | ||
272 | /* For active balancing */ | |
273 | int active_balance; | |
274 | int push_cpu; | |
0a2966b4 | 275 | int cpu; /* cpu of this runqueue */ |
1da177e4 | 276 | |
36c8b586 | 277 | struct task_struct *migration_thread; |
1da177e4 LT |
278 | struct list_head migration_queue; |
279 | #endif | |
280 | ||
281 | #ifdef CONFIG_SCHEDSTATS | |
282 | /* latency stats */ | |
283 | struct sched_info rq_sched_info; | |
284 | ||
285 | /* sys_sched_yield() stats */ | |
286 | unsigned long yld_exp_empty; | |
287 | unsigned long yld_act_empty; | |
288 | unsigned long yld_both_empty; | |
289 | unsigned long yld_cnt; | |
290 | ||
291 | /* schedule() stats */ | |
292 | unsigned long sched_switch; | |
293 | unsigned long sched_cnt; | |
294 | unsigned long sched_goidle; | |
295 | ||
296 | /* try_to_wake_up() stats */ | |
297 | unsigned long ttwu_cnt; | |
298 | unsigned long ttwu_local; | |
299 | #endif | |
fcb99371 | 300 | struct lock_class_key rq_lock_key; |
1da177e4 LT |
301 | }; |
302 | ||
f34e3b61 | 303 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); |
5be9361c | 304 | static DEFINE_MUTEX(sched_hotcpu_mutex); |
1da177e4 | 305 | |
dd41f596 IM |
306 | static inline void check_preempt_curr(struct rq *rq, struct task_struct *p) |
307 | { | |
308 | rq->curr->sched_class->check_preempt_curr(rq, p); | |
309 | } | |
310 | ||
0a2966b4 CL |
311 | static inline int cpu_of(struct rq *rq) |
312 | { | |
313 | #ifdef CONFIG_SMP | |
314 | return rq->cpu; | |
315 | #else | |
316 | return 0; | |
317 | #endif | |
318 | } | |
319 | ||
20d315d4 | 320 | /* |
b04a0f4c IM |
321 | * Update the per-runqueue clock, as finegrained as the platform can give |
322 | * us, but without assuming monotonicity, etc.: | |
20d315d4 | 323 | */ |
b04a0f4c | 324 | static void __update_rq_clock(struct rq *rq) |
20d315d4 IM |
325 | { |
326 | u64 prev_raw = rq->prev_clock_raw; | |
327 | u64 now = sched_clock(); | |
328 | s64 delta = now - prev_raw; | |
329 | u64 clock = rq->clock; | |
330 | ||
b04a0f4c IM |
331 | #ifdef CONFIG_SCHED_DEBUG |
332 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); | |
333 | #endif | |
20d315d4 IM |
334 | /* |
335 | * Protect against sched_clock() occasionally going backwards: | |
336 | */ | |
337 | if (unlikely(delta < 0)) { | |
338 | clock++; | |
339 | rq->clock_warps++; | |
340 | } else { | |
341 | /* | |
342 | * Catch too large forward jumps too: | |
343 | */ | |
344 | if (unlikely(delta > 2*TICK_NSEC)) { | |
345 | clock++; | |
346 | rq->clock_overflows++; | |
347 | } else { | |
348 | if (unlikely(delta > rq->clock_max_delta)) | |
349 | rq->clock_max_delta = delta; | |
350 | clock += delta; | |
351 | } | |
352 | } | |
353 | ||
354 | rq->prev_clock_raw = now; | |
355 | rq->clock = clock; | |
b04a0f4c | 356 | } |
20d315d4 | 357 | |
b04a0f4c IM |
358 | static void update_rq_clock(struct rq *rq) |
359 | { | |
360 | if (likely(smp_processor_id() == cpu_of(rq))) | |
361 | __update_rq_clock(rq); | |
20d315d4 IM |
362 | } |
363 | ||
674311d5 NP |
364 | /* |
365 | * The domain tree (rq->sd) is protected by RCU's quiescent state transition. | |
1a20ff27 | 366 | * See detach_destroy_domains: synchronize_sched for details. |
674311d5 NP |
367 | * |
368 | * The domain tree of any CPU may only be accessed from within | |
369 | * preempt-disabled sections. | |
370 | */ | |
48f24c4d IM |
371 | #define for_each_domain(cpu, __sd) \ |
372 | for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent) | |
1da177e4 LT |
373 | |
374 | #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu))) | |
375 | #define this_rq() (&__get_cpu_var(runqueues)) | |
376 | #define task_rq(p) cpu_rq(task_cpu(p)) | |
377 | #define cpu_curr(cpu) (cpu_rq(cpu)->curr) | |
378 | ||
e436d800 IM |
379 | /* |
380 | * For kernel-internal use: high-speed (but slightly incorrect) per-cpu | |
381 | * clock constructed from sched_clock(): | |
382 | */ | |
383 | unsigned long long cpu_clock(int cpu) | |
384 | { | |
e436d800 IM |
385 | unsigned long long now; |
386 | unsigned long flags; | |
b04a0f4c | 387 | struct rq *rq; |
e436d800 | 388 | |
2cd4d0ea | 389 | local_irq_save(flags); |
b04a0f4c IM |
390 | rq = cpu_rq(cpu); |
391 | update_rq_clock(rq); | |
392 | now = rq->clock; | |
2cd4d0ea | 393 | local_irq_restore(flags); |
e436d800 IM |
394 | |
395 | return now; | |
396 | } | |
397 | ||
138a8aeb IM |
398 | #ifdef CONFIG_FAIR_GROUP_SCHED |
399 | /* Change a task's ->cfs_rq if it moves across CPUs */ | |
400 | static inline void set_task_cfs_rq(struct task_struct *p) | |
401 | { | |
402 | p->se.cfs_rq = &task_rq(p)->cfs; | |
403 | } | |
404 | #else | |
405 | static inline void set_task_cfs_rq(struct task_struct *p) | |
406 | { | |
407 | } | |
408 | #endif | |
409 | ||
1da177e4 | 410 | #ifndef prepare_arch_switch |
4866cde0 NP |
411 | # define prepare_arch_switch(next) do { } while (0) |
412 | #endif | |
413 | #ifndef finish_arch_switch | |
414 | # define finish_arch_switch(prev) do { } while (0) | |
415 | #endif | |
416 | ||
417 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW | |
70b97a7f | 418 | static inline int task_running(struct rq *rq, struct task_struct *p) |
4866cde0 NP |
419 | { |
420 | return rq->curr == p; | |
421 | } | |
422 | ||
70b97a7f | 423 | static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next) |
4866cde0 NP |
424 | { |
425 | } | |
426 | ||
70b97a7f | 427 | static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev) |
4866cde0 | 428 | { |
da04c035 IM |
429 | #ifdef CONFIG_DEBUG_SPINLOCK |
430 | /* this is a valid case when another task releases the spinlock */ | |
431 | rq->lock.owner = current; | |
432 | #endif | |
8a25d5de IM |
433 | /* |
434 | * If we are tracking spinlock dependencies then we have to | |
435 | * fix up the runqueue lock - which gets 'carried over' from | |
436 | * prev into current: | |
437 | */ | |
438 | spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_); | |
439 | ||
4866cde0 NP |
440 | spin_unlock_irq(&rq->lock); |
441 | } | |
442 | ||
443 | #else /* __ARCH_WANT_UNLOCKED_CTXSW */ | |
70b97a7f | 444 | static inline int task_running(struct rq *rq, struct task_struct *p) |
4866cde0 NP |
445 | { |
446 | #ifdef CONFIG_SMP | |
447 | return p->oncpu; | |
448 | #else | |
449 | return rq->curr == p; | |
450 | #endif | |
451 | } | |
452 | ||
70b97a7f | 453 | static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next) |
4866cde0 NP |
454 | { |
455 | #ifdef CONFIG_SMP | |
456 | /* | |
457 | * We can optimise this out completely for !SMP, because the | |
458 | * SMP rebalancing from interrupt is the only thing that cares | |
459 | * here. | |
460 | */ | |
461 | next->oncpu = 1; | |
462 | #endif | |
463 | #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW | |
464 | spin_unlock_irq(&rq->lock); | |
465 | #else | |
466 | spin_unlock(&rq->lock); | |
467 | #endif | |
468 | } | |
469 | ||
70b97a7f | 470 | static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev) |
4866cde0 NP |
471 | { |
472 | #ifdef CONFIG_SMP | |
473 | /* | |
474 | * After ->oncpu is cleared, the task can be moved to a different CPU. | |
475 | * We must ensure this doesn't happen until the switch is completely | |
476 | * finished. | |
477 | */ | |
478 | smp_wmb(); | |
479 | prev->oncpu = 0; | |
480 | #endif | |
481 | #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW | |
482 | local_irq_enable(); | |
1da177e4 | 483 | #endif |
4866cde0 NP |
484 | } |
485 | #endif /* __ARCH_WANT_UNLOCKED_CTXSW */ | |
1da177e4 | 486 | |
b29739f9 IM |
487 | /* |
488 | * __task_rq_lock - lock the runqueue a given task resides on. | |
489 | * Must be called interrupts disabled. | |
490 | */ | |
70b97a7f | 491 | static inline struct rq *__task_rq_lock(struct task_struct *p) |
b29739f9 IM |
492 | __acquires(rq->lock) |
493 | { | |
70b97a7f | 494 | struct rq *rq; |
b29739f9 IM |
495 | |
496 | repeat_lock_task: | |
497 | rq = task_rq(p); | |
498 | spin_lock(&rq->lock); | |
499 | if (unlikely(rq != task_rq(p))) { | |
500 | spin_unlock(&rq->lock); | |
501 | goto repeat_lock_task; | |
502 | } | |
503 | return rq; | |
504 | } | |
505 | ||
1da177e4 LT |
506 | /* |
507 | * task_rq_lock - lock the runqueue a given task resides on and disable | |
508 | * interrupts. Note the ordering: we can safely lookup the task_rq without | |
509 | * explicitly disabling preemption. | |
510 | */ | |
70b97a7f | 511 | static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags) |
1da177e4 LT |
512 | __acquires(rq->lock) |
513 | { | |
70b97a7f | 514 | struct rq *rq; |
1da177e4 LT |
515 | |
516 | repeat_lock_task: | |
517 | local_irq_save(*flags); | |
518 | rq = task_rq(p); | |
519 | spin_lock(&rq->lock); | |
520 | if (unlikely(rq != task_rq(p))) { | |
521 | spin_unlock_irqrestore(&rq->lock, *flags); | |
522 | goto repeat_lock_task; | |
523 | } | |
524 | return rq; | |
525 | } | |
526 | ||
70b97a7f | 527 | static inline void __task_rq_unlock(struct rq *rq) |
b29739f9 IM |
528 | __releases(rq->lock) |
529 | { | |
530 | spin_unlock(&rq->lock); | |
531 | } | |
532 | ||
70b97a7f | 533 | static inline void task_rq_unlock(struct rq *rq, unsigned long *flags) |
1da177e4 LT |
534 | __releases(rq->lock) |
535 | { | |
536 | spin_unlock_irqrestore(&rq->lock, *flags); | |
537 | } | |
538 | ||
1da177e4 | 539 | /* |
cc2a73b5 | 540 | * this_rq_lock - lock this runqueue and disable interrupts. |
1da177e4 | 541 | */ |
70b97a7f | 542 | static inline struct rq *this_rq_lock(void) |
1da177e4 LT |
543 | __acquires(rq->lock) |
544 | { | |
70b97a7f | 545 | struct rq *rq; |
1da177e4 LT |
546 | |
547 | local_irq_disable(); | |
548 | rq = this_rq(); | |
549 | spin_lock(&rq->lock); | |
550 | ||
551 | return rq; | |
552 | } | |
553 | ||
1b9f19c2 IM |
554 | /* |
555 | * CPU frequency is/was unstable - start new by setting prev_clock_raw: | |
556 | */ | |
557 | void sched_clock_unstable_event(void) | |
558 | { | |
559 | unsigned long flags; | |
560 | struct rq *rq; | |
561 | ||
562 | rq = task_rq_lock(current, &flags); | |
563 | rq->prev_clock_raw = sched_clock(); | |
564 | rq->clock_unstable_events++; | |
565 | task_rq_unlock(rq, &flags); | |
566 | } | |
567 | ||
c24d20db IM |
568 | /* |
569 | * resched_task - mark a task 'to be rescheduled now'. | |
570 | * | |
571 | * On UP this means the setting of the need_resched flag, on SMP it | |
572 | * might also involve a cross-CPU call to trigger the scheduler on | |
573 | * the target CPU. | |
574 | */ | |
575 | #ifdef CONFIG_SMP | |
576 | ||
577 | #ifndef tsk_is_polling | |
578 | #define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG) | |
579 | #endif | |
580 | ||
581 | static void resched_task(struct task_struct *p) | |
582 | { | |
583 | int cpu; | |
584 | ||
585 | assert_spin_locked(&task_rq(p)->lock); | |
586 | ||
587 | if (unlikely(test_tsk_thread_flag(p, TIF_NEED_RESCHED))) | |
588 | return; | |
589 | ||
590 | set_tsk_thread_flag(p, TIF_NEED_RESCHED); | |
591 | ||
592 | cpu = task_cpu(p); | |
593 | if (cpu == smp_processor_id()) | |
594 | return; | |
595 | ||
596 | /* NEED_RESCHED must be visible before we test polling */ | |
597 | smp_mb(); | |
598 | if (!tsk_is_polling(p)) | |
599 | smp_send_reschedule(cpu); | |
600 | } | |
601 | ||
602 | static void resched_cpu(int cpu) | |
603 | { | |
604 | struct rq *rq = cpu_rq(cpu); | |
605 | unsigned long flags; | |
606 | ||
607 | if (!spin_trylock_irqsave(&rq->lock, flags)) | |
608 | return; | |
609 | resched_task(cpu_curr(cpu)); | |
610 | spin_unlock_irqrestore(&rq->lock, flags); | |
611 | } | |
612 | #else | |
613 | static inline void resched_task(struct task_struct *p) | |
614 | { | |
615 | assert_spin_locked(&task_rq(p)->lock); | |
616 | set_tsk_need_resched(p); | |
617 | } | |
618 | #endif | |
619 | ||
45bf76df IM |
620 | static u64 div64_likely32(u64 divident, unsigned long divisor) |
621 | { | |
622 | #if BITS_PER_LONG == 32 | |
623 | if (likely(divident <= 0xffffffffULL)) | |
624 | return (u32)divident / divisor; | |
625 | do_div(divident, divisor); | |
626 | ||
627 | return divident; | |
628 | #else | |
629 | return divident / divisor; | |
630 | #endif | |
631 | } | |
632 | ||
633 | #if BITS_PER_LONG == 32 | |
634 | # define WMULT_CONST (~0UL) | |
635 | #else | |
636 | # define WMULT_CONST (1UL << 32) | |
637 | #endif | |
638 | ||
639 | #define WMULT_SHIFT 32 | |
640 | ||
cb1c4fc9 | 641 | static unsigned long |
45bf76df IM |
642 | calc_delta_mine(unsigned long delta_exec, unsigned long weight, |
643 | struct load_weight *lw) | |
644 | { | |
645 | u64 tmp; | |
646 | ||
647 | if (unlikely(!lw->inv_weight)) | |
648 | lw->inv_weight = WMULT_CONST / lw->weight; | |
649 | ||
650 | tmp = (u64)delta_exec * weight; | |
651 | /* | |
652 | * Check whether we'd overflow the 64-bit multiplication: | |
653 | */ | |
654 | if (unlikely(tmp > WMULT_CONST)) { | |
655 | tmp = ((tmp >> WMULT_SHIFT/2) * lw->inv_weight) | |
656 | >> (WMULT_SHIFT/2); | |
657 | } else { | |
658 | tmp = (tmp * lw->inv_weight) >> WMULT_SHIFT; | |
659 | } | |
660 | ||
ecf691da | 661 | return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX); |
45bf76df IM |
662 | } |
663 | ||
664 | static inline unsigned long | |
665 | calc_delta_fair(unsigned long delta_exec, struct load_weight *lw) | |
666 | { | |
667 | return calc_delta_mine(delta_exec, NICE_0_LOAD, lw); | |
668 | } | |
669 | ||
670 | static void update_load_add(struct load_weight *lw, unsigned long inc) | |
671 | { | |
672 | lw->weight += inc; | |
673 | lw->inv_weight = 0; | |
674 | } | |
675 | ||
676 | static void update_load_sub(struct load_weight *lw, unsigned long dec) | |
677 | { | |
678 | lw->weight -= dec; | |
679 | lw->inv_weight = 0; | |
680 | } | |
681 | ||
2dd73a4f PW |
682 | /* |
683 | * To aid in avoiding the subversion of "niceness" due to uneven distribution | |
684 | * of tasks with abnormal "nice" values across CPUs the contribution that | |
685 | * each task makes to its run queue's load is weighted according to its | |
686 | * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a | |
687 | * scaled version of the new time slice allocation that they receive on time | |
688 | * slice expiry etc. | |
689 | */ | |
690 | ||
dd41f596 IM |
691 | #define WEIGHT_IDLEPRIO 2 |
692 | #define WMULT_IDLEPRIO (1 << 31) | |
693 | ||
694 | /* | |
695 | * Nice levels are multiplicative, with a gentle 10% change for every | |
696 | * nice level changed. I.e. when a CPU-bound task goes from nice 0 to | |
697 | * nice 1, it will get ~10% less CPU time than another CPU-bound task | |
698 | * that remained on nice 0. | |
699 | * | |
700 | * The "10% effect" is relative and cumulative: from _any_ nice level, | |
701 | * if you go up 1 level, it's -10% CPU usage, if you go down 1 level | |
f9153ee6 IM |
702 | * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25. |
703 | * If a task goes up by ~10% and another task goes down by ~10% then | |
704 | * the relative distance between them is ~25%.) | |
dd41f596 IM |
705 | */ |
706 | static const int prio_to_weight[40] = { | |
707 | /* -20 */ 88818, 71054, 56843, 45475, 36380, 29104, 23283, 18626, 14901, 11921, | |
708 | /* -10 */ 9537, 7629, 6103, 4883, 3906, 3125, 2500, 2000, 1600, 1280, | |
709 | /* 0 */ NICE_0_LOAD /* 1024 */, | |
710 | /* 1 */ 819, 655, 524, 419, 336, 268, 215, 172, 137, | |
711 | /* 10 */ 110, 87, 70, 56, 45, 36, 29, 23, 18, 15, | |
712 | }; | |
713 | ||
5714d2de IM |
714 | /* |
715 | * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated. | |
716 | * | |
717 | * In cases where the weight does not change often, we can use the | |
718 | * precalculated inverse to speed up arithmetics by turning divisions | |
719 | * into multiplications: | |
720 | */ | |
dd41f596 | 721 | static const u32 prio_to_wmult[40] = { |
e4af30be IM |
722 | /* -20 */ 48356, 60446, 75558, 94446, 118058, |
723 | /* -15 */ 147573, 184467, 230589, 288233, 360285, | |
724 | /* -10 */ 450347, 562979, 703746, 879575, 1099582, | |
725 | /* -5 */ 1374389, 1717986, 2147483, 2684354, 3355443, | |
726 | /* 0 */ 4194304, 5244160, 6557201, 8196502, 10250518, | |
727 | /* 5 */ 12782640, 16025997, 19976592, 24970740, 31350126, | |
728 | /* 10 */ 39045157, 49367440, 61356675, 76695844, 95443717, | |
729 | /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153, | |
dd41f596 | 730 | }; |
2dd73a4f | 731 | |
dd41f596 IM |
732 | static void activate_task(struct rq *rq, struct task_struct *p, int wakeup); |
733 | ||
734 | /* | |
735 | * runqueue iterator, to support SMP load-balancing between different | |
736 | * scheduling classes, without having to expose their internal data | |
737 | * structures to the load-balancing proper: | |
738 | */ | |
739 | struct rq_iterator { | |
740 | void *arg; | |
741 | struct task_struct *(*start)(void *); | |
742 | struct task_struct *(*next)(void *); | |
743 | }; | |
744 | ||
745 | static int balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, | |
746 | unsigned long max_nr_move, unsigned long max_load_move, | |
747 | struct sched_domain *sd, enum cpu_idle_type idle, | |
748 | int *all_pinned, unsigned long *load_moved, | |
a4ac01c3 | 749 | int *this_best_prio, struct rq_iterator *iterator); |
dd41f596 IM |
750 | |
751 | #include "sched_stats.h" | |
752 | #include "sched_rt.c" | |
753 | #include "sched_fair.c" | |
754 | #include "sched_idletask.c" | |
755 | #ifdef CONFIG_SCHED_DEBUG | |
756 | # include "sched_debug.c" | |
757 | #endif | |
758 | ||
759 | #define sched_class_highest (&rt_sched_class) | |
760 | ||
9c217245 IM |
761 | static void __update_curr_load(struct rq *rq, struct load_stat *ls) |
762 | { | |
763 | if (rq->curr != rq->idle && ls->load.weight) { | |
764 | ls->delta_exec += ls->delta_stat; | |
765 | ls->delta_fair += calc_delta_fair(ls->delta_stat, &ls->load); | |
766 | ls->delta_stat = 0; | |
767 | } | |
768 | } | |
769 | ||
770 | /* | |
771 | * Update delta_exec, delta_fair fields for rq. | |
772 | * | |
773 | * delta_fair clock advances at a rate inversely proportional to | |
774 | * total load (rq->ls.load.weight) on the runqueue, while | |
775 | * delta_exec advances at the same rate as wall-clock (provided | |
776 | * cpu is not idle). | |
777 | * | |
778 | * delta_exec / delta_fair is a measure of the (smoothened) load on this | |
779 | * runqueue over any given interval. This (smoothened) load is used | |
780 | * during load balance. | |
781 | * | |
782 | * This function is called /before/ updating rq->ls.load | |
783 | * and when switching tasks. | |
784 | */ | |
84a1d7a2 | 785 | static void update_curr_load(struct rq *rq) |
9c217245 IM |
786 | { |
787 | struct load_stat *ls = &rq->ls; | |
788 | u64 start; | |
789 | ||
790 | start = ls->load_update_start; | |
d281918d IM |
791 | ls->load_update_start = rq->clock; |
792 | ls->delta_stat += rq->clock - start; | |
9c217245 IM |
793 | /* |
794 | * Stagger updates to ls->delta_fair. Very frequent updates | |
795 | * can be expensive. | |
796 | */ | |
797 | if (ls->delta_stat >= sysctl_sched_stat_granularity) | |
798 | __update_curr_load(rq, ls); | |
799 | } | |
800 | ||
29b4b623 | 801 | static inline void inc_load(struct rq *rq, const struct task_struct *p) |
9c217245 | 802 | { |
84a1d7a2 | 803 | update_curr_load(rq); |
9c217245 IM |
804 | update_load_add(&rq->ls.load, p->se.load.weight); |
805 | } | |
806 | ||
79b5dddf | 807 | static inline void dec_load(struct rq *rq, const struct task_struct *p) |
9c217245 | 808 | { |
84a1d7a2 | 809 | update_curr_load(rq); |
9c217245 IM |
810 | update_load_sub(&rq->ls.load, p->se.load.weight); |
811 | } | |
812 | ||
e5fa2237 | 813 | static void inc_nr_running(struct task_struct *p, struct rq *rq) |
9c217245 IM |
814 | { |
815 | rq->nr_running++; | |
29b4b623 | 816 | inc_load(rq, p); |
9c217245 IM |
817 | } |
818 | ||
db53181e | 819 | static void dec_nr_running(struct task_struct *p, struct rq *rq) |
9c217245 IM |
820 | { |
821 | rq->nr_running--; | |
79b5dddf | 822 | dec_load(rq, p); |
9c217245 IM |
823 | } |
824 | ||
45bf76df IM |
825 | static void set_load_weight(struct task_struct *p) |
826 | { | |
dd41f596 IM |
827 | task_rq(p)->cfs.wait_runtime -= p->se.wait_runtime; |
828 | p->se.wait_runtime = 0; | |
829 | ||
45bf76df | 830 | if (task_has_rt_policy(p)) { |
dd41f596 IM |
831 | p->se.load.weight = prio_to_weight[0] * 2; |
832 | p->se.load.inv_weight = prio_to_wmult[0] >> 1; | |
833 | return; | |
834 | } | |
45bf76df | 835 | |
dd41f596 IM |
836 | /* |
837 | * SCHED_IDLE tasks get minimal weight: | |
838 | */ | |
839 | if (p->policy == SCHED_IDLE) { | |
840 | p->se.load.weight = WEIGHT_IDLEPRIO; | |
841 | p->se.load.inv_weight = WMULT_IDLEPRIO; | |
842 | return; | |
843 | } | |
71f8bd46 | 844 | |
dd41f596 IM |
845 | p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO]; |
846 | p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO]; | |
71f8bd46 IM |
847 | } |
848 | ||
8159f87e | 849 | static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup) |
71f8bd46 | 850 | { |
dd41f596 | 851 | sched_info_queued(p); |
fd390f6a | 852 | p->sched_class->enqueue_task(rq, p, wakeup); |
dd41f596 | 853 | p->se.on_rq = 1; |
71f8bd46 IM |
854 | } |
855 | ||
69be72c1 | 856 | static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep) |
71f8bd46 | 857 | { |
f02231e5 | 858 | p->sched_class->dequeue_task(rq, p, sleep); |
dd41f596 | 859 | p->se.on_rq = 0; |
71f8bd46 IM |
860 | } |
861 | ||
14531189 | 862 | /* |
dd41f596 | 863 | * __normal_prio - return the priority that is based on the static prio |
14531189 | 864 | */ |
14531189 IM |
865 | static inline int __normal_prio(struct task_struct *p) |
866 | { | |
dd41f596 | 867 | return p->static_prio; |
14531189 IM |
868 | } |
869 | ||
b29739f9 IM |
870 | /* |
871 | * Calculate the expected normal priority: i.e. priority | |
872 | * without taking RT-inheritance into account. Might be | |
873 | * boosted by interactivity modifiers. Changes upon fork, | |
874 | * setprio syscalls, and whenever the interactivity | |
875 | * estimator recalculates. | |
876 | */ | |
36c8b586 | 877 | static inline int normal_prio(struct task_struct *p) |
b29739f9 IM |
878 | { |
879 | int prio; | |
880 | ||
e05606d3 | 881 | if (task_has_rt_policy(p)) |
b29739f9 IM |
882 | prio = MAX_RT_PRIO-1 - p->rt_priority; |
883 | else | |
884 | prio = __normal_prio(p); | |
885 | return prio; | |
886 | } | |
887 | ||
888 | /* | |
889 | * Calculate the current priority, i.e. the priority | |
890 | * taken into account by the scheduler. This value might | |
891 | * be boosted by RT tasks, or might be boosted by | |
892 | * interactivity modifiers. Will be RT if the task got | |
893 | * RT-boosted. If not then it returns p->normal_prio. | |
894 | */ | |
36c8b586 | 895 | static int effective_prio(struct task_struct *p) |
b29739f9 IM |
896 | { |
897 | p->normal_prio = normal_prio(p); | |
898 | /* | |
899 | * If we are RT tasks or we were boosted to RT priority, | |
900 | * keep the priority unchanged. Otherwise, update priority | |
901 | * to the normal priority: | |
902 | */ | |
903 | if (!rt_prio(p->prio)) | |
904 | return p->normal_prio; | |
905 | return p->prio; | |
906 | } | |
907 | ||
1da177e4 | 908 | /* |
dd41f596 | 909 | * activate_task - move a task to the runqueue. |
1da177e4 | 910 | */ |
dd41f596 | 911 | static void activate_task(struct rq *rq, struct task_struct *p, int wakeup) |
1da177e4 | 912 | { |
a8e504d2 IM |
913 | u64 now; |
914 | ||
915 | update_rq_clock(rq); | |
916 | now = rq->clock; | |
d425b274 | 917 | |
dd41f596 IM |
918 | if (p->state == TASK_UNINTERRUPTIBLE) |
919 | rq->nr_uninterruptible--; | |
1da177e4 | 920 | |
8159f87e | 921 | enqueue_task(rq, p, wakeup); |
e5fa2237 | 922 | inc_nr_running(p, rq); |
1da177e4 LT |
923 | } |
924 | ||
925 | /* | |
dd41f596 | 926 | * activate_idle_task - move idle task to the _front_ of runqueue. |
1da177e4 | 927 | */ |
dd41f596 | 928 | static inline void activate_idle_task(struct task_struct *p, struct rq *rq) |
1da177e4 | 929 | { |
a8e504d2 IM |
930 | u64 now; |
931 | ||
932 | update_rq_clock(rq); | |
933 | now = rq->clock; | |
1da177e4 | 934 | |
dd41f596 IM |
935 | if (p->state == TASK_UNINTERRUPTIBLE) |
936 | rq->nr_uninterruptible--; | |
ece8a684 | 937 | |
8159f87e | 938 | enqueue_task(rq, p, 0); |
e5fa2237 | 939 | inc_nr_running(p, rq); |
1da177e4 LT |
940 | } |
941 | ||
942 | /* | |
943 | * deactivate_task - remove a task from the runqueue. | |
944 | */ | |
8e717b19 IM |
945 | static void |
946 | deactivate_task(struct rq *rq, struct task_struct *p, int sleep, u64 now) | |
1da177e4 | 947 | { |
dd41f596 IM |
948 | if (p->state == TASK_UNINTERRUPTIBLE) |
949 | rq->nr_uninterruptible++; | |
950 | ||
69be72c1 | 951 | dequeue_task(rq, p, sleep); |
db53181e | 952 | dec_nr_running(p, rq); |
1da177e4 LT |
953 | } |
954 | ||
1da177e4 LT |
955 | /** |
956 | * task_curr - is this task currently executing on a CPU? | |
957 | * @p: the task in question. | |
958 | */ | |
36c8b586 | 959 | inline int task_curr(const struct task_struct *p) |
1da177e4 LT |
960 | { |
961 | return cpu_curr(task_cpu(p)) == p; | |
962 | } | |
963 | ||
2dd73a4f PW |
964 | /* Used instead of source_load when we know the type == 0 */ |
965 | unsigned long weighted_cpuload(const int cpu) | |
966 | { | |
dd41f596 IM |
967 | return cpu_rq(cpu)->ls.load.weight; |
968 | } | |
969 | ||
970 | static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu) | |
971 | { | |
972 | #ifdef CONFIG_SMP | |
973 | task_thread_info(p)->cpu = cpu; | |
974 | set_task_cfs_rq(p); | |
975 | #endif | |
2dd73a4f PW |
976 | } |
977 | ||
1da177e4 | 978 | #ifdef CONFIG_SMP |
c65cc870 | 979 | |
dd41f596 | 980 | void set_task_cpu(struct task_struct *p, unsigned int new_cpu) |
c65cc870 | 981 | { |
dd41f596 IM |
982 | int old_cpu = task_cpu(p); |
983 | struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu); | |
984 | u64 clock_offset, fair_clock_offset; | |
985 | ||
986 | clock_offset = old_rq->clock - new_rq->clock; | |
6cfb0d5d IM |
987 | fair_clock_offset = old_rq->cfs.fair_clock - new_rq->cfs.fair_clock; |
988 | ||
dd41f596 IM |
989 | if (p->se.wait_start_fair) |
990 | p->se.wait_start_fair -= fair_clock_offset; | |
6cfb0d5d IM |
991 | if (p->se.sleep_start_fair) |
992 | p->se.sleep_start_fair -= fair_clock_offset; | |
993 | ||
994 | #ifdef CONFIG_SCHEDSTATS | |
995 | if (p->se.wait_start) | |
996 | p->se.wait_start -= clock_offset; | |
dd41f596 IM |
997 | if (p->se.sleep_start) |
998 | p->se.sleep_start -= clock_offset; | |
999 | if (p->se.block_start) | |
1000 | p->se.block_start -= clock_offset; | |
6cfb0d5d | 1001 | #endif |
dd41f596 IM |
1002 | |
1003 | __set_task_cpu(p, new_cpu); | |
c65cc870 IM |
1004 | } |
1005 | ||
70b97a7f | 1006 | struct migration_req { |
1da177e4 | 1007 | struct list_head list; |
1da177e4 | 1008 | |
36c8b586 | 1009 | struct task_struct *task; |
1da177e4 LT |
1010 | int dest_cpu; |
1011 | ||
1da177e4 | 1012 | struct completion done; |
70b97a7f | 1013 | }; |
1da177e4 LT |
1014 | |
1015 | /* | |
1016 | * The task's runqueue lock must be held. | |
1017 | * Returns true if you have to wait for migration thread. | |
1018 | */ | |
36c8b586 | 1019 | static int |
70b97a7f | 1020 | migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req) |
1da177e4 | 1021 | { |
70b97a7f | 1022 | struct rq *rq = task_rq(p); |
1da177e4 LT |
1023 | |
1024 | /* | |
1025 | * If the task is not on a runqueue (and not running), then | |
1026 | * it is sufficient to simply update the task's cpu field. | |
1027 | */ | |
dd41f596 | 1028 | if (!p->se.on_rq && !task_running(rq, p)) { |
1da177e4 LT |
1029 | set_task_cpu(p, dest_cpu); |
1030 | return 0; | |
1031 | } | |
1032 | ||
1033 | init_completion(&req->done); | |
1da177e4 LT |
1034 | req->task = p; |
1035 | req->dest_cpu = dest_cpu; | |
1036 | list_add(&req->list, &rq->migration_queue); | |
48f24c4d | 1037 | |
1da177e4 LT |
1038 | return 1; |
1039 | } | |
1040 | ||
1041 | /* | |
1042 | * wait_task_inactive - wait for a thread to unschedule. | |
1043 | * | |
1044 | * The caller must ensure that the task *will* unschedule sometime soon, | |
1045 | * else this function might spin for a *long* time. This function can't | |
1046 | * be called with interrupts off, or it may introduce deadlock with | |
1047 | * smp_call_function() if an IPI is sent by the same process we are | |
1048 | * waiting to become inactive. | |
1049 | */ | |
36c8b586 | 1050 | void wait_task_inactive(struct task_struct *p) |
1da177e4 LT |
1051 | { |
1052 | unsigned long flags; | |
dd41f596 | 1053 | int running, on_rq; |
70b97a7f | 1054 | struct rq *rq; |
1da177e4 LT |
1055 | |
1056 | repeat: | |
fa490cfd LT |
1057 | /* |
1058 | * We do the initial early heuristics without holding | |
1059 | * any task-queue locks at all. We'll only try to get | |
1060 | * the runqueue lock when things look like they will | |
1061 | * work out! | |
1062 | */ | |
1063 | rq = task_rq(p); | |
1064 | ||
1065 | /* | |
1066 | * If the task is actively running on another CPU | |
1067 | * still, just relax and busy-wait without holding | |
1068 | * any locks. | |
1069 | * | |
1070 | * NOTE! Since we don't hold any locks, it's not | |
1071 | * even sure that "rq" stays as the right runqueue! | |
1072 | * But we don't care, since "task_running()" will | |
1073 | * return false if the runqueue has changed and p | |
1074 | * is actually now running somewhere else! | |
1075 | */ | |
1076 | while (task_running(rq, p)) | |
1077 | cpu_relax(); | |
1078 | ||
1079 | /* | |
1080 | * Ok, time to look more closely! We need the rq | |
1081 | * lock now, to be *sure*. If we're wrong, we'll | |
1082 | * just go back and repeat. | |
1083 | */ | |
1da177e4 | 1084 | rq = task_rq_lock(p, &flags); |
fa490cfd | 1085 | running = task_running(rq, p); |
dd41f596 | 1086 | on_rq = p->se.on_rq; |
fa490cfd LT |
1087 | task_rq_unlock(rq, &flags); |
1088 | ||
1089 | /* | |
1090 | * Was it really running after all now that we | |
1091 | * checked with the proper locks actually held? | |
1092 | * | |
1093 | * Oops. Go back and try again.. | |
1094 | */ | |
1095 | if (unlikely(running)) { | |
1da177e4 | 1096 | cpu_relax(); |
1da177e4 LT |
1097 | goto repeat; |
1098 | } | |
fa490cfd LT |
1099 | |
1100 | /* | |
1101 | * It's not enough that it's not actively running, | |
1102 | * it must be off the runqueue _entirely_, and not | |
1103 | * preempted! | |
1104 | * | |
1105 | * So if it wa still runnable (but just not actively | |
1106 | * running right now), it's preempted, and we should | |
1107 | * yield - it could be a while. | |
1108 | */ | |
dd41f596 | 1109 | if (unlikely(on_rq)) { |
fa490cfd LT |
1110 | yield(); |
1111 | goto repeat; | |
1112 | } | |
1113 | ||
1114 | /* | |
1115 | * Ahh, all good. It wasn't running, and it wasn't | |
1116 | * runnable, which means that it will never become | |
1117 | * running in the future either. We're all done! | |
1118 | */ | |
1da177e4 LT |
1119 | } |
1120 | ||
1121 | /*** | |
1122 | * kick_process - kick a running thread to enter/exit the kernel | |
1123 | * @p: the to-be-kicked thread | |
1124 | * | |
1125 | * Cause a process which is running on another CPU to enter | |
1126 | * kernel-mode, without any delay. (to get signals handled.) | |
1127 | * | |
1128 | * NOTE: this function doesnt have to take the runqueue lock, | |
1129 | * because all it wants to ensure is that the remote task enters | |
1130 | * the kernel. If the IPI races and the task has been migrated | |
1131 | * to another CPU then no harm is done and the purpose has been | |
1132 | * achieved as well. | |
1133 | */ | |
36c8b586 | 1134 | void kick_process(struct task_struct *p) |
1da177e4 LT |
1135 | { |
1136 | int cpu; | |
1137 | ||
1138 | preempt_disable(); | |
1139 | cpu = task_cpu(p); | |
1140 | if ((cpu != smp_processor_id()) && task_curr(p)) | |
1141 | smp_send_reschedule(cpu); | |
1142 | preempt_enable(); | |
1143 | } | |
1144 | ||
1145 | /* | |
2dd73a4f PW |
1146 | * Return a low guess at the load of a migration-source cpu weighted |
1147 | * according to the scheduling class and "nice" value. | |
1da177e4 LT |
1148 | * |
1149 | * We want to under-estimate the load of migration sources, to | |
1150 | * balance conservatively. | |
1151 | */ | |
a2000572 | 1152 | static inline unsigned long source_load(int cpu, int type) |
1da177e4 | 1153 | { |
70b97a7f | 1154 | struct rq *rq = cpu_rq(cpu); |
dd41f596 | 1155 | unsigned long total = weighted_cpuload(cpu); |
2dd73a4f | 1156 | |
3b0bd9bc | 1157 | if (type == 0) |
dd41f596 | 1158 | return total; |
b910472d | 1159 | |
dd41f596 | 1160 | return min(rq->cpu_load[type-1], total); |
1da177e4 LT |
1161 | } |
1162 | ||
1163 | /* | |
2dd73a4f PW |
1164 | * Return a high guess at the load of a migration-target cpu weighted |
1165 | * according to the scheduling class and "nice" value. | |
1da177e4 | 1166 | */ |
a2000572 | 1167 | static inline unsigned long target_load(int cpu, int type) |
1da177e4 | 1168 | { |
70b97a7f | 1169 | struct rq *rq = cpu_rq(cpu); |
dd41f596 | 1170 | unsigned long total = weighted_cpuload(cpu); |
2dd73a4f | 1171 | |
7897986b | 1172 | if (type == 0) |
dd41f596 | 1173 | return total; |
3b0bd9bc | 1174 | |
dd41f596 | 1175 | return max(rq->cpu_load[type-1], total); |
2dd73a4f PW |
1176 | } |
1177 | ||
1178 | /* | |
1179 | * Return the average load per task on the cpu's run queue | |
1180 | */ | |
1181 | static inline unsigned long cpu_avg_load_per_task(int cpu) | |
1182 | { | |
70b97a7f | 1183 | struct rq *rq = cpu_rq(cpu); |
dd41f596 | 1184 | unsigned long total = weighted_cpuload(cpu); |
2dd73a4f PW |
1185 | unsigned long n = rq->nr_running; |
1186 | ||
dd41f596 | 1187 | return n ? total / n : SCHED_LOAD_SCALE; |
1da177e4 LT |
1188 | } |
1189 | ||
147cbb4b NP |
1190 | /* |
1191 | * find_idlest_group finds and returns the least busy CPU group within the | |
1192 | * domain. | |
1193 | */ | |
1194 | static struct sched_group * | |
1195 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu) | |
1196 | { | |
1197 | struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups; | |
1198 | unsigned long min_load = ULONG_MAX, this_load = 0; | |
1199 | int load_idx = sd->forkexec_idx; | |
1200 | int imbalance = 100 + (sd->imbalance_pct-100)/2; | |
1201 | ||
1202 | do { | |
1203 | unsigned long load, avg_load; | |
1204 | int local_group; | |
1205 | int i; | |
1206 | ||
da5a5522 BD |
1207 | /* Skip over this group if it has no CPUs allowed */ |
1208 | if (!cpus_intersects(group->cpumask, p->cpus_allowed)) | |
1209 | goto nextgroup; | |
1210 | ||
147cbb4b | 1211 | local_group = cpu_isset(this_cpu, group->cpumask); |
147cbb4b NP |
1212 | |
1213 | /* Tally up the load of all CPUs in the group */ | |
1214 | avg_load = 0; | |
1215 | ||
1216 | for_each_cpu_mask(i, group->cpumask) { | |
1217 | /* Bias balancing toward cpus of our domain */ | |
1218 | if (local_group) | |
1219 | load = source_load(i, load_idx); | |
1220 | else | |
1221 | load = target_load(i, load_idx); | |
1222 | ||
1223 | avg_load += load; | |
1224 | } | |
1225 | ||
1226 | /* Adjust by relative CPU power of the group */ | |
5517d86b ED |
1227 | avg_load = sg_div_cpu_power(group, |
1228 | avg_load * SCHED_LOAD_SCALE); | |
147cbb4b NP |
1229 | |
1230 | if (local_group) { | |
1231 | this_load = avg_load; | |
1232 | this = group; | |
1233 | } else if (avg_load < min_load) { | |
1234 | min_load = avg_load; | |
1235 | idlest = group; | |
1236 | } | |
da5a5522 | 1237 | nextgroup: |
147cbb4b NP |
1238 | group = group->next; |
1239 | } while (group != sd->groups); | |
1240 | ||
1241 | if (!idlest || 100*this_load < imbalance*min_load) | |
1242 | return NULL; | |
1243 | return idlest; | |
1244 | } | |
1245 | ||
1246 | /* | |
0feaece9 | 1247 | * find_idlest_cpu - find the idlest cpu among the cpus in group. |
147cbb4b | 1248 | */ |
95cdf3b7 IM |
1249 | static int |
1250 | find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu) | |
147cbb4b | 1251 | { |
da5a5522 | 1252 | cpumask_t tmp; |
147cbb4b NP |
1253 | unsigned long load, min_load = ULONG_MAX; |
1254 | int idlest = -1; | |
1255 | int i; | |
1256 | ||
da5a5522 BD |
1257 | /* Traverse only the allowed CPUs */ |
1258 | cpus_and(tmp, group->cpumask, p->cpus_allowed); | |
1259 | ||
1260 | for_each_cpu_mask(i, tmp) { | |
2dd73a4f | 1261 | load = weighted_cpuload(i); |
147cbb4b NP |
1262 | |
1263 | if (load < min_load || (load == min_load && i == this_cpu)) { | |
1264 | min_load = load; | |
1265 | idlest = i; | |
1266 | } | |
1267 | } | |
1268 | ||
1269 | return idlest; | |
1270 | } | |
1271 | ||
476d139c NP |
1272 | /* |
1273 | * sched_balance_self: balance the current task (running on cpu) in domains | |
1274 | * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and | |
1275 | * SD_BALANCE_EXEC. | |
1276 | * | |
1277 | * Balance, ie. select the least loaded group. | |
1278 | * | |
1279 | * Returns the target CPU number, or the same CPU if no balancing is needed. | |
1280 | * | |
1281 | * preempt must be disabled. | |
1282 | */ | |
1283 | static int sched_balance_self(int cpu, int flag) | |
1284 | { | |
1285 | struct task_struct *t = current; | |
1286 | struct sched_domain *tmp, *sd = NULL; | |
147cbb4b | 1287 | |
c96d145e | 1288 | for_each_domain(cpu, tmp) { |
9761eea8 IM |
1289 | /* |
1290 | * If power savings logic is enabled for a domain, stop there. | |
1291 | */ | |
5c45bf27 SS |
1292 | if (tmp->flags & SD_POWERSAVINGS_BALANCE) |
1293 | break; | |
476d139c NP |
1294 | if (tmp->flags & flag) |
1295 | sd = tmp; | |
c96d145e | 1296 | } |
476d139c NP |
1297 | |
1298 | while (sd) { | |
1299 | cpumask_t span; | |
1300 | struct sched_group *group; | |
1a848870 SS |
1301 | int new_cpu, weight; |
1302 | ||
1303 | if (!(sd->flags & flag)) { | |
1304 | sd = sd->child; | |
1305 | continue; | |
1306 | } | |
476d139c NP |
1307 | |
1308 | span = sd->span; | |
1309 | group = find_idlest_group(sd, t, cpu); | |
1a848870 SS |
1310 | if (!group) { |
1311 | sd = sd->child; | |
1312 | continue; | |
1313 | } | |
476d139c | 1314 | |
da5a5522 | 1315 | new_cpu = find_idlest_cpu(group, t, cpu); |
1a848870 SS |
1316 | if (new_cpu == -1 || new_cpu == cpu) { |
1317 | /* Now try balancing at a lower domain level of cpu */ | |
1318 | sd = sd->child; | |
1319 | continue; | |
1320 | } | |
476d139c | 1321 | |
1a848870 | 1322 | /* Now try balancing at a lower domain level of new_cpu */ |
476d139c | 1323 | cpu = new_cpu; |
476d139c NP |
1324 | sd = NULL; |
1325 | weight = cpus_weight(span); | |
1326 | for_each_domain(cpu, tmp) { | |
1327 | if (weight <= cpus_weight(tmp->span)) | |
1328 | break; | |
1329 | if (tmp->flags & flag) | |
1330 | sd = tmp; | |
1331 | } | |
1332 | /* while loop will break here if sd == NULL */ | |
1333 | } | |
1334 | ||
1335 | return cpu; | |
1336 | } | |
1337 | ||
1338 | #endif /* CONFIG_SMP */ | |
1da177e4 LT |
1339 | |
1340 | /* | |
1341 | * wake_idle() will wake a task on an idle cpu if task->cpu is | |
1342 | * not idle and an idle cpu is available. The span of cpus to | |
1343 | * search starts with cpus closest then further out as needed, | |
1344 | * so we always favor a closer, idle cpu. | |
1345 | * | |
1346 | * Returns the CPU we should wake onto. | |
1347 | */ | |
1348 | #if defined(ARCH_HAS_SCHED_WAKE_IDLE) | |
36c8b586 | 1349 | static int wake_idle(int cpu, struct task_struct *p) |
1da177e4 LT |
1350 | { |
1351 | cpumask_t tmp; | |
1352 | struct sched_domain *sd; | |
1353 | int i; | |
1354 | ||
4953198b SS |
1355 | /* |
1356 | * If it is idle, then it is the best cpu to run this task. | |
1357 | * | |
1358 | * This cpu is also the best, if it has more than one task already. | |
1359 | * Siblings must be also busy(in most cases) as they didn't already | |
1360 | * pickup the extra load from this cpu and hence we need not check | |
1361 | * sibling runqueue info. This will avoid the checks and cache miss | |
1362 | * penalities associated with that. | |
1363 | */ | |
1364 | if (idle_cpu(cpu) || cpu_rq(cpu)->nr_running > 1) | |
1da177e4 LT |
1365 | return cpu; |
1366 | ||
1367 | for_each_domain(cpu, sd) { | |
1368 | if (sd->flags & SD_WAKE_IDLE) { | |
e0f364f4 | 1369 | cpus_and(tmp, sd->span, p->cpus_allowed); |
1da177e4 LT |
1370 | for_each_cpu_mask(i, tmp) { |
1371 | if (idle_cpu(i)) | |
1372 | return i; | |
1373 | } | |
9761eea8 | 1374 | } else { |
e0f364f4 | 1375 | break; |
9761eea8 | 1376 | } |
1da177e4 LT |
1377 | } |
1378 | return cpu; | |
1379 | } | |
1380 | #else | |
36c8b586 | 1381 | static inline int wake_idle(int cpu, struct task_struct *p) |
1da177e4 LT |
1382 | { |
1383 | return cpu; | |
1384 | } | |
1385 | #endif | |
1386 | ||
1387 | /*** | |
1388 | * try_to_wake_up - wake up a thread | |
1389 | * @p: the to-be-woken-up thread | |
1390 | * @state: the mask of task states that can be woken | |
1391 | * @sync: do a synchronous wakeup? | |
1392 | * | |
1393 | * Put it on the run-queue if it's not already there. The "current" | |
1394 | * thread is always on the run-queue (except when the actual | |
1395 | * re-schedule is in progress), and as such you're allowed to do | |
1396 | * the simpler "current->state = TASK_RUNNING" to mark yourself | |
1397 | * runnable without the overhead of this. | |
1398 | * | |
1399 | * returns failure only if the task is already active. | |
1400 | */ | |
36c8b586 | 1401 | static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync) |
1da177e4 LT |
1402 | { |
1403 | int cpu, this_cpu, success = 0; | |
1404 | unsigned long flags; | |
1405 | long old_state; | |
70b97a7f | 1406 | struct rq *rq; |
1da177e4 | 1407 | #ifdef CONFIG_SMP |
7897986b | 1408 | struct sched_domain *sd, *this_sd = NULL; |
70b97a7f | 1409 | unsigned long load, this_load; |
1da177e4 LT |
1410 | int new_cpu; |
1411 | #endif | |
1412 | ||
1413 | rq = task_rq_lock(p, &flags); | |
1414 | old_state = p->state; | |
1415 | if (!(old_state & state)) | |
1416 | goto out; | |
1417 | ||
dd41f596 | 1418 | if (p->se.on_rq) |
1da177e4 LT |
1419 | goto out_running; |
1420 | ||
1421 | cpu = task_cpu(p); | |
1422 | this_cpu = smp_processor_id(); | |
1423 | ||
1424 | #ifdef CONFIG_SMP | |
1425 | if (unlikely(task_running(rq, p))) | |
1426 | goto out_activate; | |
1427 | ||
7897986b NP |
1428 | new_cpu = cpu; |
1429 | ||
1da177e4 LT |
1430 | schedstat_inc(rq, ttwu_cnt); |
1431 | if (cpu == this_cpu) { | |
1432 | schedstat_inc(rq, ttwu_local); | |
7897986b NP |
1433 | goto out_set_cpu; |
1434 | } | |
1435 | ||
1436 | for_each_domain(this_cpu, sd) { | |
1437 | if (cpu_isset(cpu, sd->span)) { | |
1438 | schedstat_inc(sd, ttwu_wake_remote); | |
1439 | this_sd = sd; | |
1440 | break; | |
1da177e4 LT |
1441 | } |
1442 | } | |
1da177e4 | 1443 | |
7897986b | 1444 | if (unlikely(!cpu_isset(this_cpu, p->cpus_allowed))) |
1da177e4 LT |
1445 | goto out_set_cpu; |
1446 | ||
1da177e4 | 1447 | /* |
7897986b | 1448 | * Check for affine wakeup and passive balancing possibilities. |
1da177e4 | 1449 | */ |
7897986b NP |
1450 | if (this_sd) { |
1451 | int idx = this_sd->wake_idx; | |
1452 | unsigned int imbalance; | |
1da177e4 | 1453 | |
a3f21bce NP |
1454 | imbalance = 100 + (this_sd->imbalance_pct - 100) / 2; |
1455 | ||
7897986b NP |
1456 | load = source_load(cpu, idx); |
1457 | this_load = target_load(this_cpu, idx); | |
1da177e4 | 1458 | |
7897986b NP |
1459 | new_cpu = this_cpu; /* Wake to this CPU if we can */ |
1460 | ||
a3f21bce NP |
1461 | if (this_sd->flags & SD_WAKE_AFFINE) { |
1462 | unsigned long tl = this_load; | |
33859f7f MOS |
1463 | unsigned long tl_per_task; |
1464 | ||
1465 | tl_per_task = cpu_avg_load_per_task(this_cpu); | |
2dd73a4f | 1466 | |
1da177e4 | 1467 | /* |
a3f21bce NP |
1468 | * If sync wakeup then subtract the (maximum possible) |
1469 | * effect of the currently running task from the load | |
1470 | * of the current CPU: | |
1da177e4 | 1471 | */ |
a3f21bce | 1472 | if (sync) |
dd41f596 | 1473 | tl -= current->se.load.weight; |
a3f21bce NP |
1474 | |
1475 | if ((tl <= load && | |
2dd73a4f | 1476 | tl + target_load(cpu, idx) <= tl_per_task) || |
dd41f596 | 1477 | 100*(tl + p->se.load.weight) <= imbalance*load) { |
a3f21bce NP |
1478 | /* |
1479 | * This domain has SD_WAKE_AFFINE and | |
1480 | * p is cache cold in this domain, and | |
1481 | * there is no bad imbalance. | |
1482 | */ | |
1483 | schedstat_inc(this_sd, ttwu_move_affine); | |
1484 | goto out_set_cpu; | |
1485 | } | |
1486 | } | |
1487 | ||
1488 | /* | |
1489 | * Start passive balancing when half the imbalance_pct | |
1490 | * limit is reached. | |
1491 | */ | |
1492 | if (this_sd->flags & SD_WAKE_BALANCE) { | |
1493 | if (imbalance*this_load <= 100*load) { | |
1494 | schedstat_inc(this_sd, ttwu_move_balance); | |
1495 | goto out_set_cpu; | |
1496 | } | |
1da177e4 LT |
1497 | } |
1498 | } | |
1499 | ||
1500 | new_cpu = cpu; /* Could not wake to this_cpu. Wake to cpu instead */ | |
1501 | out_set_cpu: | |
1502 | new_cpu = wake_idle(new_cpu, p); | |
1503 | if (new_cpu != cpu) { | |
1504 | set_task_cpu(p, new_cpu); | |
1505 | task_rq_unlock(rq, &flags); | |
1506 | /* might preempt at this point */ | |
1507 | rq = task_rq_lock(p, &flags); | |
1508 | old_state = p->state; | |
1509 | if (!(old_state & state)) | |
1510 | goto out; | |
dd41f596 | 1511 | if (p->se.on_rq) |
1da177e4 LT |
1512 | goto out_running; |
1513 | ||
1514 | this_cpu = smp_processor_id(); | |
1515 | cpu = task_cpu(p); | |
1516 | } | |
1517 | ||
1518 | out_activate: | |
1519 | #endif /* CONFIG_SMP */ | |
dd41f596 | 1520 | activate_task(rq, p, 1); |
1da177e4 LT |
1521 | /* |
1522 | * Sync wakeups (i.e. those types of wakeups where the waker | |
1523 | * has indicated that it will leave the CPU in short order) | |
1524 | * don't trigger a preemption, if the woken up task will run on | |
1525 | * this cpu. (in this case the 'I will reschedule' promise of | |
1526 | * the waker guarantees that the freshly woken up task is going | |
1527 | * to be considered on this CPU.) | |
1528 | */ | |
dd41f596 IM |
1529 | if (!sync || cpu != this_cpu) |
1530 | check_preempt_curr(rq, p); | |
1da177e4 LT |
1531 | success = 1; |
1532 | ||
1533 | out_running: | |
1534 | p->state = TASK_RUNNING; | |
1535 | out: | |
1536 | task_rq_unlock(rq, &flags); | |
1537 | ||
1538 | return success; | |
1539 | } | |
1540 | ||
36c8b586 | 1541 | int fastcall wake_up_process(struct task_struct *p) |
1da177e4 LT |
1542 | { |
1543 | return try_to_wake_up(p, TASK_STOPPED | TASK_TRACED | | |
1544 | TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE, 0); | |
1545 | } | |
1da177e4 LT |
1546 | EXPORT_SYMBOL(wake_up_process); |
1547 | ||
36c8b586 | 1548 | int fastcall wake_up_state(struct task_struct *p, unsigned int state) |
1da177e4 LT |
1549 | { |
1550 | return try_to_wake_up(p, state, 0); | |
1551 | } | |
1552 | ||
1da177e4 LT |
1553 | /* |
1554 | * Perform scheduler related setup for a newly forked process p. | |
1555 | * p is forked by current. | |
dd41f596 IM |
1556 | * |
1557 | * __sched_fork() is basic setup used by init_idle() too: | |
1558 | */ | |
1559 | static void __sched_fork(struct task_struct *p) | |
1560 | { | |
1561 | p->se.wait_start_fair = 0; | |
dd41f596 IM |
1562 | p->se.exec_start = 0; |
1563 | p->se.sum_exec_runtime = 0; | |
1564 | p->se.delta_exec = 0; | |
1565 | p->se.delta_fair_run = 0; | |
1566 | p->se.delta_fair_sleep = 0; | |
1567 | p->se.wait_runtime = 0; | |
6cfb0d5d IM |
1568 | p->se.sleep_start_fair = 0; |
1569 | ||
1570 | #ifdef CONFIG_SCHEDSTATS | |
1571 | p->se.wait_start = 0; | |
dd41f596 IM |
1572 | p->se.sum_wait_runtime = 0; |
1573 | p->se.sum_sleep_runtime = 0; | |
1574 | p->se.sleep_start = 0; | |
dd41f596 IM |
1575 | p->se.block_start = 0; |
1576 | p->se.sleep_max = 0; | |
1577 | p->se.block_max = 0; | |
1578 | p->se.exec_max = 0; | |
1579 | p->se.wait_max = 0; | |
1580 | p->se.wait_runtime_overruns = 0; | |
1581 | p->se.wait_runtime_underruns = 0; | |
6cfb0d5d | 1582 | #endif |
476d139c | 1583 | |
dd41f596 IM |
1584 | INIT_LIST_HEAD(&p->run_list); |
1585 | p->se.on_rq = 0; | |
476d139c | 1586 | |
e107be36 AK |
1587 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
1588 | INIT_HLIST_HEAD(&p->preempt_notifiers); | |
1589 | #endif | |
1590 | ||
1da177e4 LT |
1591 | /* |
1592 | * We mark the process as running here, but have not actually | |
1593 | * inserted it onto the runqueue yet. This guarantees that | |
1594 | * nobody will actually run it, and a signal or other external | |
1595 | * event cannot wake it up and insert it on the runqueue either. | |
1596 | */ | |
1597 | p->state = TASK_RUNNING; | |
dd41f596 IM |
1598 | } |
1599 | ||
1600 | /* | |
1601 | * fork()/clone()-time setup: | |
1602 | */ | |
1603 | void sched_fork(struct task_struct *p, int clone_flags) | |
1604 | { | |
1605 | int cpu = get_cpu(); | |
1606 | ||
1607 | __sched_fork(p); | |
1608 | ||
1609 | #ifdef CONFIG_SMP | |
1610 | cpu = sched_balance_self(cpu, SD_BALANCE_FORK); | |
1611 | #endif | |
1612 | __set_task_cpu(p, cpu); | |
b29739f9 IM |
1613 | |
1614 | /* | |
1615 | * Make sure we do not leak PI boosting priority to the child: | |
1616 | */ | |
1617 | p->prio = current->normal_prio; | |
1618 | ||
52f17b6c | 1619 | #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT) |
dd41f596 | 1620 | if (likely(sched_info_on())) |
52f17b6c | 1621 | memset(&p->sched_info, 0, sizeof(p->sched_info)); |
1da177e4 | 1622 | #endif |
d6077cb8 | 1623 | #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW) |
4866cde0 NP |
1624 | p->oncpu = 0; |
1625 | #endif | |
1da177e4 | 1626 | #ifdef CONFIG_PREEMPT |
4866cde0 | 1627 | /* Want to start with kernel preemption disabled. */ |
a1261f54 | 1628 | task_thread_info(p)->preempt_count = 1; |
1da177e4 | 1629 | #endif |
476d139c | 1630 | put_cpu(); |
1da177e4 LT |
1631 | } |
1632 | ||
dd41f596 IM |
1633 | /* |
1634 | * After fork, child runs first. (default) If set to 0 then | |
1635 | * parent will (try to) run first. | |
1636 | */ | |
1637 | unsigned int __read_mostly sysctl_sched_child_runs_first = 1; | |
1638 | ||
1da177e4 LT |
1639 | /* |
1640 | * wake_up_new_task - wake up a newly created task for the first time. | |
1641 | * | |
1642 | * This function will do some initial scheduler statistics housekeeping | |
1643 | * that must be done for every newly created context, then puts the task | |
1644 | * on the runqueue and wakes it. | |
1645 | */ | |
36c8b586 | 1646 | void fastcall wake_up_new_task(struct task_struct *p, unsigned long clone_flags) |
1da177e4 LT |
1647 | { |
1648 | unsigned long flags; | |
dd41f596 IM |
1649 | struct rq *rq; |
1650 | int this_cpu; | |
cad60d93 | 1651 | u64 now; |
1da177e4 LT |
1652 | |
1653 | rq = task_rq_lock(p, &flags); | |
147cbb4b | 1654 | BUG_ON(p->state != TASK_RUNNING); |
dd41f596 | 1655 | this_cpu = smp_processor_id(); /* parent's CPU */ |
a8e504d2 IM |
1656 | update_rq_clock(rq); |
1657 | now = rq->clock; | |
1da177e4 LT |
1658 | |
1659 | p->prio = effective_prio(p); | |
1660 | ||
cad60d93 IM |
1661 | if (!p->sched_class->task_new || !sysctl_sched_child_runs_first || |
1662 | (clone_flags & CLONE_VM) || task_cpu(p) != this_cpu || | |
1663 | !current->se.on_rq) { | |
1664 | ||
dd41f596 | 1665 | activate_task(rq, p, 0); |
1da177e4 | 1666 | } else { |
1da177e4 | 1667 | /* |
dd41f596 IM |
1668 | * Let the scheduling class do new task startup |
1669 | * management (if any): | |
1da177e4 | 1670 | */ |
ee0827d8 | 1671 | p->sched_class->task_new(rq, p); |
e5fa2237 | 1672 | inc_nr_running(p, rq); |
1da177e4 | 1673 | } |
dd41f596 IM |
1674 | check_preempt_curr(rq, p); |
1675 | task_rq_unlock(rq, &flags); | |
1da177e4 LT |
1676 | } |
1677 | ||
e107be36 AK |
1678 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
1679 | ||
1680 | /** | |
421cee29 RD |
1681 | * preempt_notifier_register - tell me when current is being being preempted & rescheduled |
1682 | * @notifier: notifier struct to register | |
e107be36 AK |
1683 | */ |
1684 | void preempt_notifier_register(struct preempt_notifier *notifier) | |
1685 | { | |
1686 | hlist_add_head(¬ifier->link, ¤t->preempt_notifiers); | |
1687 | } | |
1688 | EXPORT_SYMBOL_GPL(preempt_notifier_register); | |
1689 | ||
1690 | /** | |
1691 | * preempt_notifier_unregister - no longer interested in preemption notifications | |
421cee29 | 1692 | * @notifier: notifier struct to unregister |
e107be36 AK |
1693 | * |
1694 | * This is safe to call from within a preemption notifier. | |
1695 | */ | |
1696 | void preempt_notifier_unregister(struct preempt_notifier *notifier) | |
1697 | { | |
1698 | hlist_del(¬ifier->link); | |
1699 | } | |
1700 | EXPORT_SYMBOL_GPL(preempt_notifier_unregister); | |
1701 | ||
1702 | static void fire_sched_in_preempt_notifiers(struct task_struct *curr) | |
1703 | { | |
1704 | struct preempt_notifier *notifier; | |
1705 | struct hlist_node *node; | |
1706 | ||
1707 | hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link) | |
1708 | notifier->ops->sched_in(notifier, raw_smp_processor_id()); | |
1709 | } | |
1710 | ||
1711 | static void | |
1712 | fire_sched_out_preempt_notifiers(struct task_struct *curr, | |
1713 | struct task_struct *next) | |
1714 | { | |
1715 | struct preempt_notifier *notifier; | |
1716 | struct hlist_node *node; | |
1717 | ||
1718 | hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link) | |
1719 | notifier->ops->sched_out(notifier, next); | |
1720 | } | |
1721 | ||
1722 | #else | |
1723 | ||
1724 | static void fire_sched_in_preempt_notifiers(struct task_struct *curr) | |
1725 | { | |
1726 | } | |
1727 | ||
1728 | static void | |
1729 | fire_sched_out_preempt_notifiers(struct task_struct *curr, | |
1730 | struct task_struct *next) | |
1731 | { | |
1732 | } | |
1733 | ||
1734 | #endif | |
1735 | ||
4866cde0 NP |
1736 | /** |
1737 | * prepare_task_switch - prepare to switch tasks | |
1738 | * @rq: the runqueue preparing to switch | |
421cee29 | 1739 | * @prev: the current task that is being switched out |
4866cde0 NP |
1740 | * @next: the task we are going to switch to. |
1741 | * | |
1742 | * This is called with the rq lock held and interrupts off. It must | |
1743 | * be paired with a subsequent finish_task_switch after the context | |
1744 | * switch. | |
1745 | * | |
1746 | * prepare_task_switch sets up locking and calls architecture specific | |
1747 | * hooks. | |
1748 | */ | |
e107be36 AK |
1749 | static inline void |
1750 | prepare_task_switch(struct rq *rq, struct task_struct *prev, | |
1751 | struct task_struct *next) | |
4866cde0 | 1752 | { |
e107be36 | 1753 | fire_sched_out_preempt_notifiers(prev, next); |
4866cde0 NP |
1754 | prepare_lock_switch(rq, next); |
1755 | prepare_arch_switch(next); | |
1756 | } | |
1757 | ||
1da177e4 LT |
1758 | /** |
1759 | * finish_task_switch - clean up after a task-switch | |
344babaa | 1760 | * @rq: runqueue associated with task-switch |
1da177e4 LT |
1761 | * @prev: the thread we just switched away from. |
1762 | * | |
4866cde0 NP |
1763 | * finish_task_switch must be called after the context switch, paired |
1764 | * with a prepare_task_switch call before the context switch. | |
1765 | * finish_task_switch will reconcile locking set up by prepare_task_switch, | |
1766 | * and do any other architecture-specific cleanup actions. | |
1da177e4 LT |
1767 | * |
1768 | * Note that we may have delayed dropping an mm in context_switch(). If | |
1769 | * so, we finish that here outside of the runqueue lock. (Doing it | |
1770 | * with the lock held can cause deadlocks; see schedule() for | |
1771 | * details.) | |
1772 | */ | |
70b97a7f | 1773 | static inline void finish_task_switch(struct rq *rq, struct task_struct *prev) |
1da177e4 LT |
1774 | __releases(rq->lock) |
1775 | { | |
1da177e4 | 1776 | struct mm_struct *mm = rq->prev_mm; |
55a101f8 | 1777 | long prev_state; |
1da177e4 LT |
1778 | |
1779 | rq->prev_mm = NULL; | |
1780 | ||
1781 | /* | |
1782 | * A task struct has one reference for the use as "current". | |
c394cc9f | 1783 | * If a task dies, then it sets TASK_DEAD in tsk->state and calls |
55a101f8 ON |
1784 | * schedule one last time. The schedule call will never return, and |
1785 | * the scheduled task must drop that reference. | |
c394cc9f | 1786 | * The test for TASK_DEAD must occur while the runqueue locks are |
1da177e4 LT |
1787 | * still held, otherwise prev could be scheduled on another cpu, die |
1788 | * there before we look at prev->state, and then the reference would | |
1789 | * be dropped twice. | |
1790 | * Manfred Spraul <manfred@colorfullife.com> | |
1791 | */ | |
55a101f8 | 1792 | prev_state = prev->state; |
4866cde0 NP |
1793 | finish_arch_switch(prev); |
1794 | finish_lock_switch(rq, prev); | |
e107be36 | 1795 | fire_sched_in_preempt_notifiers(current); |
1da177e4 LT |
1796 | if (mm) |
1797 | mmdrop(mm); | |
c394cc9f | 1798 | if (unlikely(prev_state == TASK_DEAD)) { |
c6fd91f0 | 1799 | /* |
1800 | * Remove function-return probe instances associated with this | |
1801 | * task and put them back on the free list. | |
9761eea8 | 1802 | */ |
c6fd91f0 | 1803 | kprobe_flush_task(prev); |
1da177e4 | 1804 | put_task_struct(prev); |
c6fd91f0 | 1805 | } |
1da177e4 LT |
1806 | } |
1807 | ||
1808 | /** | |
1809 | * schedule_tail - first thing a freshly forked thread must call. | |
1810 | * @prev: the thread we just switched away from. | |
1811 | */ | |
36c8b586 | 1812 | asmlinkage void schedule_tail(struct task_struct *prev) |
1da177e4 LT |
1813 | __releases(rq->lock) |
1814 | { | |
70b97a7f IM |
1815 | struct rq *rq = this_rq(); |
1816 | ||
4866cde0 NP |
1817 | finish_task_switch(rq, prev); |
1818 | #ifdef __ARCH_WANT_UNLOCKED_CTXSW | |
1819 | /* In this case, finish_task_switch does not reenable preemption */ | |
1820 | preempt_enable(); | |
1821 | #endif | |
1da177e4 LT |
1822 | if (current->set_child_tid) |
1823 | put_user(current->pid, current->set_child_tid); | |
1824 | } | |
1825 | ||
1826 | /* | |
1827 | * context_switch - switch to the new MM and the new | |
1828 | * thread's register state. | |
1829 | */ | |
dd41f596 | 1830 | static inline void |
70b97a7f | 1831 | context_switch(struct rq *rq, struct task_struct *prev, |
36c8b586 | 1832 | struct task_struct *next) |
1da177e4 | 1833 | { |
dd41f596 | 1834 | struct mm_struct *mm, *oldmm; |
1da177e4 | 1835 | |
e107be36 | 1836 | prepare_task_switch(rq, prev, next); |
dd41f596 IM |
1837 | mm = next->mm; |
1838 | oldmm = prev->active_mm; | |
9226d125 ZA |
1839 | /* |
1840 | * For paravirt, this is coupled with an exit in switch_to to | |
1841 | * combine the page table reload and the switch backend into | |
1842 | * one hypercall. | |
1843 | */ | |
1844 | arch_enter_lazy_cpu_mode(); | |
1845 | ||
dd41f596 | 1846 | if (unlikely(!mm)) { |
1da177e4 LT |
1847 | next->active_mm = oldmm; |
1848 | atomic_inc(&oldmm->mm_count); | |
1849 | enter_lazy_tlb(oldmm, next); | |
1850 | } else | |
1851 | switch_mm(oldmm, mm, next); | |
1852 | ||
dd41f596 | 1853 | if (unlikely(!prev->mm)) { |
1da177e4 | 1854 | prev->active_mm = NULL; |
1da177e4 LT |
1855 | rq->prev_mm = oldmm; |
1856 | } | |
3a5f5e48 IM |
1857 | /* |
1858 | * Since the runqueue lock will be released by the next | |
1859 | * task (which is an invalid locking op but in the case | |
1860 | * of the scheduler it's an obvious special-case), so we | |
1861 | * do an early lockdep release here: | |
1862 | */ | |
1863 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW | |
8a25d5de | 1864 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); |
3a5f5e48 | 1865 | #endif |
1da177e4 LT |
1866 | |
1867 | /* Here we just switch the register state and the stack. */ | |
1868 | switch_to(prev, next, prev); | |
1869 | ||
dd41f596 IM |
1870 | barrier(); |
1871 | /* | |
1872 | * this_rq must be evaluated again because prev may have moved | |
1873 | * CPUs since it called schedule(), thus the 'rq' on its stack | |
1874 | * frame will be invalid. | |
1875 | */ | |
1876 | finish_task_switch(this_rq(), prev); | |
1da177e4 LT |
1877 | } |
1878 | ||
1879 | /* | |
1880 | * nr_running, nr_uninterruptible and nr_context_switches: | |
1881 | * | |
1882 | * externally visible scheduler statistics: current number of runnable | |
1883 | * threads, current number of uninterruptible-sleeping threads, total | |
1884 | * number of context switches performed since bootup. | |
1885 | */ | |
1886 | unsigned long nr_running(void) | |
1887 | { | |
1888 | unsigned long i, sum = 0; | |
1889 | ||
1890 | for_each_online_cpu(i) | |
1891 | sum += cpu_rq(i)->nr_running; | |
1892 | ||
1893 | return sum; | |
1894 | } | |
1895 | ||
1896 | unsigned long nr_uninterruptible(void) | |
1897 | { | |
1898 | unsigned long i, sum = 0; | |
1899 | ||
0a945022 | 1900 | for_each_possible_cpu(i) |
1da177e4 LT |
1901 | sum += cpu_rq(i)->nr_uninterruptible; |
1902 | ||
1903 | /* | |
1904 | * Since we read the counters lockless, it might be slightly | |
1905 | * inaccurate. Do not allow it to go below zero though: | |
1906 | */ | |
1907 | if (unlikely((long)sum < 0)) | |
1908 | sum = 0; | |
1909 | ||
1910 | return sum; | |
1911 | } | |
1912 | ||
1913 | unsigned long long nr_context_switches(void) | |
1914 | { | |
cc94abfc SR |
1915 | int i; |
1916 | unsigned long long sum = 0; | |
1da177e4 | 1917 | |
0a945022 | 1918 | for_each_possible_cpu(i) |
1da177e4 LT |
1919 | sum += cpu_rq(i)->nr_switches; |
1920 | ||
1921 | return sum; | |
1922 | } | |
1923 | ||
1924 | unsigned long nr_iowait(void) | |
1925 | { | |
1926 | unsigned long i, sum = 0; | |
1927 | ||
0a945022 | 1928 | for_each_possible_cpu(i) |
1da177e4 LT |
1929 | sum += atomic_read(&cpu_rq(i)->nr_iowait); |
1930 | ||
1931 | return sum; | |
1932 | } | |
1933 | ||
db1b1fef JS |
1934 | unsigned long nr_active(void) |
1935 | { | |
1936 | unsigned long i, running = 0, uninterruptible = 0; | |
1937 | ||
1938 | for_each_online_cpu(i) { | |
1939 | running += cpu_rq(i)->nr_running; | |
1940 | uninterruptible += cpu_rq(i)->nr_uninterruptible; | |
1941 | } | |
1942 | ||
1943 | if (unlikely((long)uninterruptible < 0)) | |
1944 | uninterruptible = 0; | |
1945 | ||
1946 | return running + uninterruptible; | |
1947 | } | |
1948 | ||
48f24c4d | 1949 | /* |
dd41f596 IM |
1950 | * Update rq->cpu_load[] statistics. This function is usually called every |
1951 | * scheduler tick (TICK_NSEC). | |
48f24c4d | 1952 | */ |
dd41f596 | 1953 | static void update_cpu_load(struct rq *this_rq) |
48f24c4d | 1954 | { |
dd41f596 IM |
1955 | u64 fair_delta64, exec_delta64, idle_delta64, sample_interval64, tmp64; |
1956 | unsigned long total_load = this_rq->ls.load.weight; | |
1957 | unsigned long this_load = total_load; | |
1958 | struct load_stat *ls = &this_rq->ls; | |
c1b3da3e | 1959 | u64 now; |
dd41f596 IM |
1960 | int i, scale; |
1961 | ||
c1b3da3e IM |
1962 | __update_rq_clock(this_rq); |
1963 | now = this_rq->clock; | |
1964 | ||
dd41f596 IM |
1965 | this_rq->nr_load_updates++; |
1966 | if (unlikely(!(sysctl_sched_features & SCHED_FEAT_PRECISE_CPU_LOAD))) | |
1967 | goto do_avg; | |
1968 | ||
1969 | /* Update delta_fair/delta_exec fields first */ | |
84a1d7a2 | 1970 | update_curr_load(this_rq); |
dd41f596 IM |
1971 | |
1972 | fair_delta64 = ls->delta_fair + 1; | |
1973 | ls->delta_fair = 0; | |
1974 | ||
1975 | exec_delta64 = ls->delta_exec + 1; | |
1976 | ls->delta_exec = 0; | |
1977 | ||
d281918d IM |
1978 | sample_interval64 = this_rq->clock - ls->load_update_last; |
1979 | ls->load_update_last = this_rq->clock; | |
dd41f596 IM |
1980 | |
1981 | if ((s64)sample_interval64 < (s64)TICK_NSEC) | |
1982 | sample_interval64 = TICK_NSEC; | |
1983 | ||
1984 | if (exec_delta64 > sample_interval64) | |
1985 | exec_delta64 = sample_interval64; | |
1986 | ||
1987 | idle_delta64 = sample_interval64 - exec_delta64; | |
1988 | ||
1989 | tmp64 = div64_64(SCHED_LOAD_SCALE * exec_delta64, fair_delta64); | |
1990 | tmp64 = div64_64(tmp64 * exec_delta64, sample_interval64); | |
1991 | ||
1992 | this_load = (unsigned long)tmp64; | |
1993 | ||
1994 | do_avg: | |
1995 | ||
1996 | /* Update our load: */ | |
1997 | for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) { | |
1998 | unsigned long old_load, new_load; | |
1999 | ||
2000 | /* scale is effectively 1 << i now, and >> i divides by scale */ | |
2001 | ||
2002 | old_load = this_rq->cpu_load[i]; | |
2003 | new_load = this_load; | |
2004 | ||
2005 | this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i; | |
2006 | } | |
48f24c4d IM |
2007 | } |
2008 | ||
dd41f596 IM |
2009 | #ifdef CONFIG_SMP |
2010 | ||
1da177e4 LT |
2011 | /* |
2012 | * double_rq_lock - safely lock two runqueues | |
2013 | * | |
2014 | * Note this does not disable interrupts like task_rq_lock, | |
2015 | * you need to do so manually before calling. | |
2016 | */ | |
70b97a7f | 2017 | static void double_rq_lock(struct rq *rq1, struct rq *rq2) |
1da177e4 LT |
2018 | __acquires(rq1->lock) |
2019 | __acquires(rq2->lock) | |
2020 | { | |
054b9108 | 2021 | BUG_ON(!irqs_disabled()); |
1da177e4 LT |
2022 | if (rq1 == rq2) { |
2023 | spin_lock(&rq1->lock); | |
2024 | __acquire(rq2->lock); /* Fake it out ;) */ | |
2025 | } else { | |
c96d145e | 2026 | if (rq1 < rq2) { |
1da177e4 LT |
2027 | spin_lock(&rq1->lock); |
2028 | spin_lock(&rq2->lock); | |
2029 | } else { | |
2030 | spin_lock(&rq2->lock); | |
2031 | spin_lock(&rq1->lock); | |
2032 | } | |
2033 | } | |
2034 | } | |
2035 | ||
2036 | /* | |
2037 | * double_rq_unlock - safely unlock two runqueues | |
2038 | * | |
2039 | * Note this does not restore interrupts like task_rq_unlock, | |
2040 | * you need to do so manually after calling. | |
2041 | */ | |
70b97a7f | 2042 | static void double_rq_unlock(struct rq *rq1, struct rq *rq2) |
1da177e4 LT |
2043 | __releases(rq1->lock) |
2044 | __releases(rq2->lock) | |
2045 | { | |
2046 | spin_unlock(&rq1->lock); | |
2047 | if (rq1 != rq2) | |
2048 | spin_unlock(&rq2->lock); | |
2049 | else | |
2050 | __release(rq2->lock); | |
2051 | } | |
2052 | ||
2053 | /* | |
2054 | * double_lock_balance - lock the busiest runqueue, this_rq is locked already. | |
2055 | */ | |
70b97a7f | 2056 | static void double_lock_balance(struct rq *this_rq, struct rq *busiest) |
1da177e4 LT |
2057 | __releases(this_rq->lock) |
2058 | __acquires(busiest->lock) | |
2059 | __acquires(this_rq->lock) | |
2060 | { | |
054b9108 KK |
2061 | if (unlikely(!irqs_disabled())) { |
2062 | /* printk() doesn't work good under rq->lock */ | |
2063 | spin_unlock(&this_rq->lock); | |
2064 | BUG_ON(1); | |
2065 | } | |
1da177e4 | 2066 | if (unlikely(!spin_trylock(&busiest->lock))) { |
c96d145e | 2067 | if (busiest < this_rq) { |
1da177e4 LT |
2068 | spin_unlock(&this_rq->lock); |
2069 | spin_lock(&busiest->lock); | |
2070 | spin_lock(&this_rq->lock); | |
2071 | } else | |
2072 | spin_lock(&busiest->lock); | |
2073 | } | |
2074 | } | |
2075 | ||
1da177e4 LT |
2076 | /* |
2077 | * If dest_cpu is allowed for this process, migrate the task to it. | |
2078 | * This is accomplished by forcing the cpu_allowed mask to only | |
2079 | * allow dest_cpu, which will force the cpu onto dest_cpu. Then | |
2080 | * the cpu_allowed mask is restored. | |
2081 | */ | |
36c8b586 | 2082 | static void sched_migrate_task(struct task_struct *p, int dest_cpu) |
1da177e4 | 2083 | { |
70b97a7f | 2084 | struct migration_req req; |
1da177e4 | 2085 | unsigned long flags; |
70b97a7f | 2086 | struct rq *rq; |
1da177e4 LT |
2087 | |
2088 | rq = task_rq_lock(p, &flags); | |
2089 | if (!cpu_isset(dest_cpu, p->cpus_allowed) | |
2090 | || unlikely(cpu_is_offline(dest_cpu))) | |
2091 | goto out; | |
2092 | ||
2093 | /* force the process onto the specified CPU */ | |
2094 | if (migrate_task(p, dest_cpu, &req)) { | |
2095 | /* Need to wait for migration thread (might exit: take ref). */ | |
2096 | struct task_struct *mt = rq->migration_thread; | |
36c8b586 | 2097 | |
1da177e4 LT |
2098 | get_task_struct(mt); |
2099 | task_rq_unlock(rq, &flags); | |
2100 | wake_up_process(mt); | |
2101 | put_task_struct(mt); | |
2102 | wait_for_completion(&req.done); | |
36c8b586 | 2103 | |
1da177e4 LT |
2104 | return; |
2105 | } | |
2106 | out: | |
2107 | task_rq_unlock(rq, &flags); | |
2108 | } | |
2109 | ||
2110 | /* | |
476d139c NP |
2111 | * sched_exec - execve() is a valuable balancing opportunity, because at |
2112 | * this point the task has the smallest effective memory and cache footprint. | |
1da177e4 LT |
2113 | */ |
2114 | void sched_exec(void) | |
2115 | { | |
1da177e4 | 2116 | int new_cpu, this_cpu = get_cpu(); |
476d139c | 2117 | new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC); |
1da177e4 | 2118 | put_cpu(); |
476d139c NP |
2119 | if (new_cpu != this_cpu) |
2120 | sched_migrate_task(current, new_cpu); | |
1da177e4 LT |
2121 | } |
2122 | ||
2123 | /* | |
2124 | * pull_task - move a task from a remote runqueue to the local runqueue. | |
2125 | * Both runqueues must be locked. | |
2126 | */ | |
dd41f596 IM |
2127 | static void pull_task(struct rq *src_rq, struct task_struct *p, |
2128 | struct rq *this_rq, int this_cpu) | |
1da177e4 | 2129 | { |
a8e504d2 IM |
2130 | update_rq_clock(src_rq); |
2131 | deactivate_task(src_rq, p, 0, src_rq->clock); | |
1da177e4 | 2132 | set_task_cpu(p, this_cpu); |
dd41f596 | 2133 | activate_task(this_rq, p, 0); |
1da177e4 LT |
2134 | /* |
2135 | * Note that idle threads have a prio of MAX_PRIO, for this test | |
2136 | * to be always true for them. | |
2137 | */ | |
dd41f596 | 2138 | check_preempt_curr(this_rq, p); |
1da177e4 LT |
2139 | } |
2140 | ||
2141 | /* | |
2142 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? | |
2143 | */ | |
858119e1 | 2144 | static |
70b97a7f | 2145 | int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu, |
d15bcfdb | 2146 | struct sched_domain *sd, enum cpu_idle_type idle, |
95cdf3b7 | 2147 | int *all_pinned) |
1da177e4 LT |
2148 | { |
2149 | /* | |
2150 | * We do not migrate tasks that are: | |
2151 | * 1) running (obviously), or | |
2152 | * 2) cannot be migrated to this CPU due to cpus_allowed, or | |
2153 | * 3) are cache-hot on their current CPU. | |
2154 | */ | |
1da177e4 LT |
2155 | if (!cpu_isset(this_cpu, p->cpus_allowed)) |
2156 | return 0; | |
81026794 NP |
2157 | *all_pinned = 0; |
2158 | ||
2159 | if (task_running(rq, p)) | |
2160 | return 0; | |
1da177e4 LT |
2161 | |
2162 | /* | |
dd41f596 | 2163 | * Aggressive migration if too many balance attempts have failed: |
1da177e4 | 2164 | */ |
dd41f596 | 2165 | if (sd->nr_balance_failed > sd->cache_nice_tries) |
1da177e4 LT |
2166 | return 1; |
2167 | ||
1da177e4 LT |
2168 | return 1; |
2169 | } | |
2170 | ||
dd41f596 | 2171 | static int balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, |
2dd73a4f | 2172 | unsigned long max_nr_move, unsigned long max_load_move, |
d15bcfdb | 2173 | struct sched_domain *sd, enum cpu_idle_type idle, |
dd41f596 | 2174 | int *all_pinned, unsigned long *load_moved, |
a4ac01c3 | 2175 | int *this_best_prio, struct rq_iterator *iterator) |
1da177e4 | 2176 | { |
dd41f596 IM |
2177 | int pulled = 0, pinned = 0, skip_for_load; |
2178 | struct task_struct *p; | |
2179 | long rem_load_move = max_load_move; | |
1da177e4 | 2180 | |
2dd73a4f | 2181 | if (max_nr_move == 0 || max_load_move == 0) |
1da177e4 LT |
2182 | goto out; |
2183 | ||
81026794 NP |
2184 | pinned = 1; |
2185 | ||
1da177e4 | 2186 | /* |
dd41f596 | 2187 | * Start the load-balancing iterator: |
1da177e4 | 2188 | */ |
dd41f596 IM |
2189 | p = iterator->start(iterator->arg); |
2190 | next: | |
2191 | if (!p) | |
1da177e4 | 2192 | goto out; |
50ddd969 PW |
2193 | /* |
2194 | * To help distribute high priority tasks accross CPUs we don't | |
2195 | * skip a task if it will be the highest priority task (i.e. smallest | |
2196 | * prio value) on its new queue regardless of its load weight | |
2197 | */ | |
dd41f596 IM |
2198 | skip_for_load = (p->se.load.weight >> 1) > rem_load_move + |
2199 | SCHED_LOAD_SCALE_FUZZ; | |
a4ac01c3 | 2200 | if ((skip_for_load && p->prio >= *this_best_prio) || |
dd41f596 | 2201 | !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) { |
dd41f596 IM |
2202 | p = iterator->next(iterator->arg); |
2203 | goto next; | |
1da177e4 LT |
2204 | } |
2205 | ||
dd41f596 | 2206 | pull_task(busiest, p, this_rq, this_cpu); |
1da177e4 | 2207 | pulled++; |
dd41f596 | 2208 | rem_load_move -= p->se.load.weight; |
1da177e4 | 2209 | |
2dd73a4f PW |
2210 | /* |
2211 | * We only want to steal up to the prescribed number of tasks | |
2212 | * and the prescribed amount of weighted load. | |
2213 | */ | |
2214 | if (pulled < max_nr_move && rem_load_move > 0) { | |
a4ac01c3 PW |
2215 | if (p->prio < *this_best_prio) |
2216 | *this_best_prio = p->prio; | |
dd41f596 IM |
2217 | p = iterator->next(iterator->arg); |
2218 | goto next; | |
1da177e4 LT |
2219 | } |
2220 | out: | |
2221 | /* | |
2222 | * Right now, this is the only place pull_task() is called, | |
2223 | * so we can safely collect pull_task() stats here rather than | |
2224 | * inside pull_task(). | |
2225 | */ | |
2226 | schedstat_add(sd, lb_gained[idle], pulled); | |
81026794 NP |
2227 | |
2228 | if (all_pinned) | |
2229 | *all_pinned = pinned; | |
dd41f596 | 2230 | *load_moved = max_load_move - rem_load_move; |
1da177e4 LT |
2231 | return pulled; |
2232 | } | |
2233 | ||
dd41f596 | 2234 | /* |
43010659 PW |
2235 | * move_tasks tries to move up to max_load_move weighted load from busiest to |
2236 | * this_rq, as part of a balancing operation within domain "sd". | |
2237 | * Returns 1 if successful and 0 otherwise. | |
dd41f596 IM |
2238 | * |
2239 | * Called with both runqueues locked. | |
2240 | */ | |
2241 | static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, | |
43010659 | 2242 | unsigned long max_load_move, |
dd41f596 IM |
2243 | struct sched_domain *sd, enum cpu_idle_type idle, |
2244 | int *all_pinned) | |
2245 | { | |
2246 | struct sched_class *class = sched_class_highest; | |
43010659 | 2247 | unsigned long total_load_moved = 0; |
a4ac01c3 | 2248 | int this_best_prio = this_rq->curr->prio; |
dd41f596 IM |
2249 | |
2250 | do { | |
43010659 PW |
2251 | total_load_moved += |
2252 | class->load_balance(this_rq, this_cpu, busiest, | |
2253 | ULONG_MAX, max_load_move - total_load_moved, | |
a4ac01c3 | 2254 | sd, idle, all_pinned, &this_best_prio); |
dd41f596 | 2255 | class = class->next; |
43010659 | 2256 | } while (class && max_load_move > total_load_moved); |
dd41f596 | 2257 | |
43010659 PW |
2258 | return total_load_moved > 0; |
2259 | } | |
2260 | ||
2261 | /* | |
2262 | * move_one_task tries to move exactly one task from busiest to this_rq, as | |
2263 | * part of active balancing operations within "domain". | |
2264 | * Returns 1 if successful and 0 otherwise. | |
2265 | * | |
2266 | * Called with both runqueues locked. | |
2267 | */ | |
2268 | static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest, | |
2269 | struct sched_domain *sd, enum cpu_idle_type idle) | |
2270 | { | |
2271 | struct sched_class *class; | |
a4ac01c3 | 2272 | int this_best_prio = MAX_PRIO; |
43010659 PW |
2273 | |
2274 | for (class = sched_class_highest; class; class = class->next) | |
2275 | if (class->load_balance(this_rq, this_cpu, busiest, | |
a4ac01c3 PW |
2276 | 1, ULONG_MAX, sd, idle, NULL, |
2277 | &this_best_prio)) | |
43010659 PW |
2278 | return 1; |
2279 | ||
2280 | return 0; | |
dd41f596 IM |
2281 | } |
2282 | ||
1da177e4 LT |
2283 | /* |
2284 | * find_busiest_group finds and returns the busiest CPU group within the | |
48f24c4d IM |
2285 | * domain. It calculates and returns the amount of weighted load which |
2286 | * should be moved to restore balance via the imbalance parameter. | |
1da177e4 LT |
2287 | */ |
2288 | static struct sched_group * | |
2289 | find_busiest_group(struct sched_domain *sd, int this_cpu, | |
dd41f596 IM |
2290 | unsigned long *imbalance, enum cpu_idle_type idle, |
2291 | int *sd_idle, cpumask_t *cpus, int *balance) | |
1da177e4 LT |
2292 | { |
2293 | struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups; | |
2294 | unsigned long max_load, avg_load, total_load, this_load, total_pwr; | |
0c117f1b | 2295 | unsigned long max_pull; |
2dd73a4f PW |
2296 | unsigned long busiest_load_per_task, busiest_nr_running; |
2297 | unsigned long this_load_per_task, this_nr_running; | |
7897986b | 2298 | int load_idx; |
5c45bf27 SS |
2299 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
2300 | int power_savings_balance = 1; | |
2301 | unsigned long leader_nr_running = 0, min_load_per_task = 0; | |
2302 | unsigned long min_nr_running = ULONG_MAX; | |
2303 | struct sched_group *group_min = NULL, *group_leader = NULL; | |
2304 | #endif | |
1da177e4 LT |
2305 | |
2306 | max_load = this_load = total_load = total_pwr = 0; | |
2dd73a4f PW |
2307 | busiest_load_per_task = busiest_nr_running = 0; |
2308 | this_load_per_task = this_nr_running = 0; | |
d15bcfdb | 2309 | if (idle == CPU_NOT_IDLE) |
7897986b | 2310 | load_idx = sd->busy_idx; |
d15bcfdb | 2311 | else if (idle == CPU_NEWLY_IDLE) |
7897986b NP |
2312 | load_idx = sd->newidle_idx; |
2313 | else | |
2314 | load_idx = sd->idle_idx; | |
1da177e4 LT |
2315 | |
2316 | do { | |
5c45bf27 | 2317 | unsigned long load, group_capacity; |
1da177e4 LT |
2318 | int local_group; |
2319 | int i; | |
783609c6 | 2320 | unsigned int balance_cpu = -1, first_idle_cpu = 0; |
2dd73a4f | 2321 | unsigned long sum_nr_running, sum_weighted_load; |
1da177e4 LT |
2322 | |
2323 | local_group = cpu_isset(this_cpu, group->cpumask); | |
2324 | ||
783609c6 SS |
2325 | if (local_group) |
2326 | balance_cpu = first_cpu(group->cpumask); | |
2327 | ||
1da177e4 | 2328 | /* Tally up the load of all CPUs in the group */ |
2dd73a4f | 2329 | sum_weighted_load = sum_nr_running = avg_load = 0; |
1da177e4 LT |
2330 | |
2331 | for_each_cpu_mask(i, group->cpumask) { | |
0a2966b4 CL |
2332 | struct rq *rq; |
2333 | ||
2334 | if (!cpu_isset(i, *cpus)) | |
2335 | continue; | |
2336 | ||
2337 | rq = cpu_rq(i); | |
2dd73a4f | 2338 | |
9439aab8 | 2339 | if (*sd_idle && rq->nr_running) |
5969fe06 NP |
2340 | *sd_idle = 0; |
2341 | ||
1da177e4 | 2342 | /* Bias balancing toward cpus of our domain */ |
783609c6 SS |
2343 | if (local_group) { |
2344 | if (idle_cpu(i) && !first_idle_cpu) { | |
2345 | first_idle_cpu = 1; | |
2346 | balance_cpu = i; | |
2347 | } | |
2348 | ||
a2000572 | 2349 | load = target_load(i, load_idx); |
783609c6 | 2350 | } else |
a2000572 | 2351 | load = source_load(i, load_idx); |
1da177e4 LT |
2352 | |
2353 | avg_load += load; | |
2dd73a4f | 2354 | sum_nr_running += rq->nr_running; |
dd41f596 | 2355 | sum_weighted_load += weighted_cpuload(i); |
1da177e4 LT |
2356 | } |
2357 | ||
783609c6 SS |
2358 | /* |
2359 | * First idle cpu or the first cpu(busiest) in this sched group | |
2360 | * is eligible for doing load balancing at this and above | |
9439aab8 SS |
2361 | * domains. In the newly idle case, we will allow all the cpu's |
2362 | * to do the newly idle load balance. | |
783609c6 | 2363 | */ |
9439aab8 SS |
2364 | if (idle != CPU_NEWLY_IDLE && local_group && |
2365 | balance_cpu != this_cpu && balance) { | |
783609c6 SS |
2366 | *balance = 0; |
2367 | goto ret; | |
2368 | } | |
2369 | ||
1da177e4 | 2370 | total_load += avg_load; |
5517d86b | 2371 | total_pwr += group->__cpu_power; |
1da177e4 LT |
2372 | |
2373 | /* Adjust by relative CPU power of the group */ | |
5517d86b ED |
2374 | avg_load = sg_div_cpu_power(group, |
2375 | avg_load * SCHED_LOAD_SCALE); | |
1da177e4 | 2376 | |
5517d86b | 2377 | group_capacity = group->__cpu_power / SCHED_LOAD_SCALE; |
5c45bf27 | 2378 | |
1da177e4 LT |
2379 | if (local_group) { |
2380 | this_load = avg_load; | |
2381 | this = group; | |
2dd73a4f PW |
2382 | this_nr_running = sum_nr_running; |
2383 | this_load_per_task = sum_weighted_load; | |
2384 | } else if (avg_load > max_load && | |
5c45bf27 | 2385 | sum_nr_running > group_capacity) { |
1da177e4 LT |
2386 | max_load = avg_load; |
2387 | busiest = group; | |
2dd73a4f PW |
2388 | busiest_nr_running = sum_nr_running; |
2389 | busiest_load_per_task = sum_weighted_load; | |
1da177e4 | 2390 | } |
5c45bf27 SS |
2391 | |
2392 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) | |
2393 | /* | |
2394 | * Busy processors will not participate in power savings | |
2395 | * balance. | |
2396 | */ | |
dd41f596 IM |
2397 | if (idle == CPU_NOT_IDLE || |
2398 | !(sd->flags & SD_POWERSAVINGS_BALANCE)) | |
2399 | goto group_next; | |
5c45bf27 SS |
2400 | |
2401 | /* | |
2402 | * If the local group is idle or completely loaded | |
2403 | * no need to do power savings balance at this domain | |
2404 | */ | |
2405 | if (local_group && (this_nr_running >= group_capacity || | |
2406 | !this_nr_running)) | |
2407 | power_savings_balance = 0; | |
2408 | ||
dd41f596 | 2409 | /* |
5c45bf27 SS |
2410 | * If a group is already running at full capacity or idle, |
2411 | * don't include that group in power savings calculations | |
dd41f596 IM |
2412 | */ |
2413 | if (!power_savings_balance || sum_nr_running >= group_capacity | |
5c45bf27 | 2414 | || !sum_nr_running) |
dd41f596 | 2415 | goto group_next; |
5c45bf27 | 2416 | |
dd41f596 | 2417 | /* |
5c45bf27 | 2418 | * Calculate the group which has the least non-idle load. |
dd41f596 IM |
2419 | * This is the group from where we need to pick up the load |
2420 | * for saving power | |
2421 | */ | |
2422 | if ((sum_nr_running < min_nr_running) || | |
2423 | (sum_nr_running == min_nr_running && | |
5c45bf27 SS |
2424 | first_cpu(group->cpumask) < |
2425 | first_cpu(group_min->cpumask))) { | |
dd41f596 IM |
2426 | group_min = group; |
2427 | min_nr_running = sum_nr_running; | |
5c45bf27 SS |
2428 | min_load_per_task = sum_weighted_load / |
2429 | sum_nr_running; | |
dd41f596 | 2430 | } |
5c45bf27 | 2431 | |
dd41f596 | 2432 | /* |
5c45bf27 | 2433 | * Calculate the group which is almost near its |
dd41f596 IM |
2434 | * capacity but still has some space to pick up some load |
2435 | * from other group and save more power | |
2436 | */ | |
2437 | if (sum_nr_running <= group_capacity - 1) { | |
2438 | if (sum_nr_running > leader_nr_running || | |
2439 | (sum_nr_running == leader_nr_running && | |
2440 | first_cpu(group->cpumask) > | |
2441 | first_cpu(group_leader->cpumask))) { | |
2442 | group_leader = group; | |
2443 | leader_nr_running = sum_nr_running; | |
2444 | } | |
48f24c4d | 2445 | } |
5c45bf27 SS |
2446 | group_next: |
2447 | #endif | |
1da177e4 LT |
2448 | group = group->next; |
2449 | } while (group != sd->groups); | |
2450 | ||
2dd73a4f | 2451 | if (!busiest || this_load >= max_load || busiest_nr_running == 0) |
1da177e4 LT |
2452 | goto out_balanced; |
2453 | ||
2454 | avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr; | |
2455 | ||
2456 | if (this_load >= avg_load || | |
2457 | 100*max_load <= sd->imbalance_pct*this_load) | |
2458 | goto out_balanced; | |
2459 | ||
2dd73a4f | 2460 | busiest_load_per_task /= busiest_nr_running; |
1da177e4 LT |
2461 | /* |
2462 | * We're trying to get all the cpus to the average_load, so we don't | |
2463 | * want to push ourselves above the average load, nor do we wish to | |
2464 | * reduce the max loaded cpu below the average load, as either of these | |
2465 | * actions would just result in more rebalancing later, and ping-pong | |
2466 | * tasks around. Thus we look for the minimum possible imbalance. | |
2467 | * Negative imbalances (*we* are more loaded than anyone else) will | |
2468 | * be counted as no imbalance for these purposes -- we can't fix that | |
2469 | * by pulling tasks to us. Be careful of negative numbers as they'll | |
2470 | * appear as very large values with unsigned longs. | |
2471 | */ | |
2dd73a4f PW |
2472 | if (max_load <= busiest_load_per_task) |
2473 | goto out_balanced; | |
2474 | ||
2475 | /* | |
2476 | * In the presence of smp nice balancing, certain scenarios can have | |
2477 | * max load less than avg load(as we skip the groups at or below | |
2478 | * its cpu_power, while calculating max_load..) | |
2479 | */ | |
2480 | if (max_load < avg_load) { | |
2481 | *imbalance = 0; | |
2482 | goto small_imbalance; | |
2483 | } | |
0c117f1b SS |
2484 | |
2485 | /* Don't want to pull so many tasks that a group would go idle */ | |
2dd73a4f | 2486 | max_pull = min(max_load - avg_load, max_load - busiest_load_per_task); |
0c117f1b | 2487 | |
1da177e4 | 2488 | /* How much load to actually move to equalise the imbalance */ |
5517d86b ED |
2489 | *imbalance = min(max_pull * busiest->__cpu_power, |
2490 | (avg_load - this_load) * this->__cpu_power) | |
1da177e4 LT |
2491 | / SCHED_LOAD_SCALE; |
2492 | ||
2dd73a4f PW |
2493 | /* |
2494 | * if *imbalance is less than the average load per runnable task | |
2495 | * there is no gaurantee that any tasks will be moved so we'll have | |
2496 | * a think about bumping its value to force at least one task to be | |
2497 | * moved | |
2498 | */ | |
dd41f596 | 2499 | if (*imbalance + SCHED_LOAD_SCALE_FUZZ < busiest_load_per_task/2) { |
48f24c4d | 2500 | unsigned long tmp, pwr_now, pwr_move; |
2dd73a4f PW |
2501 | unsigned int imbn; |
2502 | ||
2503 | small_imbalance: | |
2504 | pwr_move = pwr_now = 0; | |
2505 | imbn = 2; | |
2506 | if (this_nr_running) { | |
2507 | this_load_per_task /= this_nr_running; | |
2508 | if (busiest_load_per_task > this_load_per_task) | |
2509 | imbn = 1; | |
2510 | } else | |
2511 | this_load_per_task = SCHED_LOAD_SCALE; | |
1da177e4 | 2512 | |
dd41f596 IM |
2513 | if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >= |
2514 | busiest_load_per_task * imbn) { | |
2dd73a4f | 2515 | *imbalance = busiest_load_per_task; |
1da177e4 LT |
2516 | return busiest; |
2517 | } | |
2518 | ||
2519 | /* | |
2520 | * OK, we don't have enough imbalance to justify moving tasks, | |
2521 | * however we may be able to increase total CPU power used by | |
2522 | * moving them. | |
2523 | */ | |
2524 | ||
5517d86b ED |
2525 | pwr_now += busiest->__cpu_power * |
2526 | min(busiest_load_per_task, max_load); | |
2527 | pwr_now += this->__cpu_power * | |
2528 | min(this_load_per_task, this_load); | |
1da177e4 LT |
2529 | pwr_now /= SCHED_LOAD_SCALE; |
2530 | ||
2531 | /* Amount of load we'd subtract */ | |
5517d86b ED |
2532 | tmp = sg_div_cpu_power(busiest, |
2533 | busiest_load_per_task * SCHED_LOAD_SCALE); | |
1da177e4 | 2534 | if (max_load > tmp) |
5517d86b | 2535 | pwr_move += busiest->__cpu_power * |
2dd73a4f | 2536 | min(busiest_load_per_task, max_load - tmp); |
1da177e4 LT |
2537 | |
2538 | /* Amount of load we'd add */ | |
5517d86b | 2539 | if (max_load * busiest->__cpu_power < |
33859f7f | 2540 | busiest_load_per_task * SCHED_LOAD_SCALE) |
5517d86b ED |
2541 | tmp = sg_div_cpu_power(this, |
2542 | max_load * busiest->__cpu_power); | |
1da177e4 | 2543 | else |
5517d86b ED |
2544 | tmp = sg_div_cpu_power(this, |
2545 | busiest_load_per_task * SCHED_LOAD_SCALE); | |
2546 | pwr_move += this->__cpu_power * | |
2547 | min(this_load_per_task, this_load + tmp); | |
1da177e4 LT |
2548 | pwr_move /= SCHED_LOAD_SCALE; |
2549 | ||
2550 | /* Move if we gain throughput */ | |
2551 | if (pwr_move <= pwr_now) | |
2552 | goto out_balanced; | |
2553 | ||
2dd73a4f | 2554 | *imbalance = busiest_load_per_task; |
1da177e4 LT |
2555 | } |
2556 | ||
1da177e4 LT |
2557 | return busiest; |
2558 | ||
2559 | out_balanced: | |
5c45bf27 | 2560 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
d15bcfdb | 2561 | if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE)) |
5c45bf27 | 2562 | goto ret; |
1da177e4 | 2563 | |
5c45bf27 SS |
2564 | if (this == group_leader && group_leader != group_min) { |
2565 | *imbalance = min_load_per_task; | |
2566 | return group_min; | |
2567 | } | |
5c45bf27 | 2568 | #endif |
783609c6 | 2569 | ret: |
1da177e4 LT |
2570 | *imbalance = 0; |
2571 | return NULL; | |
2572 | } | |
2573 | ||
2574 | /* | |
2575 | * find_busiest_queue - find the busiest runqueue among the cpus in group. | |
2576 | */ | |
70b97a7f | 2577 | static struct rq * |
d15bcfdb | 2578 | find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle, |
0a2966b4 | 2579 | unsigned long imbalance, cpumask_t *cpus) |
1da177e4 | 2580 | { |
70b97a7f | 2581 | struct rq *busiest = NULL, *rq; |
2dd73a4f | 2582 | unsigned long max_load = 0; |
1da177e4 LT |
2583 | int i; |
2584 | ||
2585 | for_each_cpu_mask(i, group->cpumask) { | |
dd41f596 | 2586 | unsigned long wl; |
0a2966b4 CL |
2587 | |
2588 | if (!cpu_isset(i, *cpus)) | |
2589 | continue; | |
2590 | ||
48f24c4d | 2591 | rq = cpu_rq(i); |
dd41f596 | 2592 | wl = weighted_cpuload(i); |
2dd73a4f | 2593 | |
dd41f596 | 2594 | if (rq->nr_running == 1 && wl > imbalance) |
2dd73a4f | 2595 | continue; |
1da177e4 | 2596 | |
dd41f596 IM |
2597 | if (wl > max_load) { |
2598 | max_load = wl; | |
48f24c4d | 2599 | busiest = rq; |
1da177e4 LT |
2600 | } |
2601 | } | |
2602 | ||
2603 | return busiest; | |
2604 | } | |
2605 | ||
77391d71 NP |
2606 | /* |
2607 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but | |
2608 | * so long as it is large enough. | |
2609 | */ | |
2610 | #define MAX_PINNED_INTERVAL 512 | |
2611 | ||
1da177e4 LT |
2612 | /* |
2613 | * Check this_cpu to ensure it is balanced within domain. Attempt to move | |
2614 | * tasks if there is an imbalance. | |
1da177e4 | 2615 | */ |
70b97a7f | 2616 | static int load_balance(int this_cpu, struct rq *this_rq, |
d15bcfdb | 2617 | struct sched_domain *sd, enum cpu_idle_type idle, |
783609c6 | 2618 | int *balance) |
1da177e4 | 2619 | { |
43010659 | 2620 | int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0; |
1da177e4 | 2621 | struct sched_group *group; |
1da177e4 | 2622 | unsigned long imbalance; |
70b97a7f | 2623 | struct rq *busiest; |
0a2966b4 | 2624 | cpumask_t cpus = CPU_MASK_ALL; |
fe2eea3f | 2625 | unsigned long flags; |
5969fe06 | 2626 | |
89c4710e SS |
2627 | /* |
2628 | * When power savings policy is enabled for the parent domain, idle | |
2629 | * sibling can pick up load irrespective of busy siblings. In this case, | |
dd41f596 | 2630 | * let the state of idle sibling percolate up as CPU_IDLE, instead of |
d15bcfdb | 2631 | * portraying it as CPU_NOT_IDLE. |
89c4710e | 2632 | */ |
d15bcfdb | 2633 | if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER && |
89c4710e | 2634 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
5969fe06 | 2635 | sd_idle = 1; |
1da177e4 | 2636 | |
1da177e4 LT |
2637 | schedstat_inc(sd, lb_cnt[idle]); |
2638 | ||
0a2966b4 CL |
2639 | redo: |
2640 | group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle, | |
783609c6 SS |
2641 | &cpus, balance); |
2642 | ||
06066714 | 2643 | if (*balance == 0) |
783609c6 | 2644 | goto out_balanced; |
783609c6 | 2645 | |
1da177e4 LT |
2646 | if (!group) { |
2647 | schedstat_inc(sd, lb_nobusyg[idle]); | |
2648 | goto out_balanced; | |
2649 | } | |
2650 | ||
0a2966b4 | 2651 | busiest = find_busiest_queue(group, idle, imbalance, &cpus); |
1da177e4 LT |
2652 | if (!busiest) { |
2653 | schedstat_inc(sd, lb_nobusyq[idle]); | |
2654 | goto out_balanced; | |
2655 | } | |
2656 | ||
db935dbd | 2657 | BUG_ON(busiest == this_rq); |
1da177e4 LT |
2658 | |
2659 | schedstat_add(sd, lb_imbalance[idle], imbalance); | |
2660 | ||
43010659 | 2661 | ld_moved = 0; |
1da177e4 LT |
2662 | if (busiest->nr_running > 1) { |
2663 | /* | |
2664 | * Attempt to move tasks. If find_busiest_group has found | |
2665 | * an imbalance but busiest->nr_running <= 1, the group is | |
43010659 | 2666 | * still unbalanced. ld_moved simply stays zero, so it is |
1da177e4 LT |
2667 | * correctly treated as an imbalance. |
2668 | */ | |
fe2eea3f | 2669 | local_irq_save(flags); |
e17224bf | 2670 | double_rq_lock(this_rq, busiest); |
43010659 | 2671 | ld_moved = move_tasks(this_rq, this_cpu, busiest, |
48f24c4d | 2672 | imbalance, sd, idle, &all_pinned); |
e17224bf | 2673 | double_rq_unlock(this_rq, busiest); |
fe2eea3f | 2674 | local_irq_restore(flags); |
81026794 | 2675 | |
46cb4b7c SS |
2676 | /* |
2677 | * some other cpu did the load balance for us. | |
2678 | */ | |
43010659 | 2679 | if (ld_moved && this_cpu != smp_processor_id()) |
46cb4b7c SS |
2680 | resched_cpu(this_cpu); |
2681 | ||
81026794 | 2682 | /* All tasks on this runqueue were pinned by CPU affinity */ |
0a2966b4 CL |
2683 | if (unlikely(all_pinned)) { |
2684 | cpu_clear(cpu_of(busiest), cpus); | |
2685 | if (!cpus_empty(cpus)) | |
2686 | goto redo; | |
81026794 | 2687 | goto out_balanced; |
0a2966b4 | 2688 | } |
1da177e4 | 2689 | } |
81026794 | 2690 | |
43010659 | 2691 | if (!ld_moved) { |
1da177e4 LT |
2692 | schedstat_inc(sd, lb_failed[idle]); |
2693 | sd->nr_balance_failed++; | |
2694 | ||
2695 | if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) { | |
1da177e4 | 2696 | |
fe2eea3f | 2697 | spin_lock_irqsave(&busiest->lock, flags); |
fa3b6ddc SS |
2698 | |
2699 | /* don't kick the migration_thread, if the curr | |
2700 | * task on busiest cpu can't be moved to this_cpu | |
2701 | */ | |
2702 | if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) { | |
fe2eea3f | 2703 | spin_unlock_irqrestore(&busiest->lock, flags); |
fa3b6ddc SS |
2704 | all_pinned = 1; |
2705 | goto out_one_pinned; | |
2706 | } | |
2707 | ||
1da177e4 LT |
2708 | if (!busiest->active_balance) { |
2709 | busiest->active_balance = 1; | |
2710 | busiest->push_cpu = this_cpu; | |
81026794 | 2711 | active_balance = 1; |
1da177e4 | 2712 | } |
fe2eea3f | 2713 | spin_unlock_irqrestore(&busiest->lock, flags); |
81026794 | 2714 | if (active_balance) |
1da177e4 LT |
2715 | wake_up_process(busiest->migration_thread); |
2716 | ||
2717 | /* | |
2718 | * We've kicked active balancing, reset the failure | |
2719 | * counter. | |
2720 | */ | |
39507451 | 2721 | sd->nr_balance_failed = sd->cache_nice_tries+1; |
1da177e4 | 2722 | } |
81026794 | 2723 | } else |
1da177e4 LT |
2724 | sd->nr_balance_failed = 0; |
2725 | ||
81026794 | 2726 | if (likely(!active_balance)) { |
1da177e4 LT |
2727 | /* We were unbalanced, so reset the balancing interval */ |
2728 | sd->balance_interval = sd->min_interval; | |
81026794 NP |
2729 | } else { |
2730 | /* | |
2731 | * If we've begun active balancing, start to back off. This | |
2732 | * case may not be covered by the all_pinned logic if there | |
2733 | * is only 1 task on the busy runqueue (because we don't call | |
2734 | * move_tasks). | |
2735 | */ | |
2736 | if (sd->balance_interval < sd->max_interval) | |
2737 | sd->balance_interval *= 2; | |
1da177e4 LT |
2738 | } |
2739 | ||
43010659 | 2740 | if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
89c4710e | 2741 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
5969fe06 | 2742 | return -1; |
43010659 | 2743 | return ld_moved; |
1da177e4 LT |
2744 | |
2745 | out_balanced: | |
1da177e4 LT |
2746 | schedstat_inc(sd, lb_balanced[idle]); |
2747 | ||
16cfb1c0 | 2748 | sd->nr_balance_failed = 0; |
fa3b6ddc SS |
2749 | |
2750 | out_one_pinned: | |
1da177e4 | 2751 | /* tune up the balancing interval */ |
77391d71 NP |
2752 | if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) || |
2753 | (sd->balance_interval < sd->max_interval)) | |
1da177e4 LT |
2754 | sd->balance_interval *= 2; |
2755 | ||
48f24c4d | 2756 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
89c4710e | 2757 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
5969fe06 | 2758 | return -1; |
1da177e4 LT |
2759 | return 0; |
2760 | } | |
2761 | ||
2762 | /* | |
2763 | * Check this_cpu to ensure it is balanced within domain. Attempt to move | |
2764 | * tasks if there is an imbalance. | |
2765 | * | |
d15bcfdb | 2766 | * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE). |
1da177e4 LT |
2767 | * this_rq is locked. |
2768 | */ | |
48f24c4d | 2769 | static int |
70b97a7f | 2770 | load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd) |
1da177e4 LT |
2771 | { |
2772 | struct sched_group *group; | |
70b97a7f | 2773 | struct rq *busiest = NULL; |
1da177e4 | 2774 | unsigned long imbalance; |
43010659 | 2775 | int ld_moved = 0; |
5969fe06 | 2776 | int sd_idle = 0; |
969bb4e4 | 2777 | int all_pinned = 0; |
0a2966b4 | 2778 | cpumask_t cpus = CPU_MASK_ALL; |
5969fe06 | 2779 | |
89c4710e SS |
2780 | /* |
2781 | * When power savings policy is enabled for the parent domain, idle | |
2782 | * sibling can pick up load irrespective of busy siblings. In this case, | |
2783 | * let the state of idle sibling percolate up as IDLE, instead of | |
d15bcfdb | 2784 | * portraying it as CPU_NOT_IDLE. |
89c4710e SS |
2785 | */ |
2786 | if (sd->flags & SD_SHARE_CPUPOWER && | |
2787 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) | |
5969fe06 | 2788 | sd_idle = 1; |
1da177e4 | 2789 | |
d15bcfdb | 2790 | schedstat_inc(sd, lb_cnt[CPU_NEWLY_IDLE]); |
0a2966b4 | 2791 | redo: |
d15bcfdb | 2792 | group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE, |
783609c6 | 2793 | &sd_idle, &cpus, NULL); |
1da177e4 | 2794 | if (!group) { |
d15bcfdb | 2795 | schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]); |
16cfb1c0 | 2796 | goto out_balanced; |
1da177e4 LT |
2797 | } |
2798 | ||
d15bcfdb | 2799 | busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, |
0a2966b4 | 2800 | &cpus); |
db935dbd | 2801 | if (!busiest) { |
d15bcfdb | 2802 | schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]); |
16cfb1c0 | 2803 | goto out_balanced; |
1da177e4 LT |
2804 | } |
2805 | ||
db935dbd NP |
2806 | BUG_ON(busiest == this_rq); |
2807 | ||
d15bcfdb | 2808 | schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance); |
d6d5cfaf | 2809 | |
43010659 | 2810 | ld_moved = 0; |
d6d5cfaf NP |
2811 | if (busiest->nr_running > 1) { |
2812 | /* Attempt to move tasks */ | |
2813 | double_lock_balance(this_rq, busiest); | |
43010659 | 2814 | ld_moved = move_tasks(this_rq, this_cpu, busiest, |
969bb4e4 SS |
2815 | imbalance, sd, CPU_NEWLY_IDLE, |
2816 | &all_pinned); | |
d6d5cfaf | 2817 | spin_unlock(&busiest->lock); |
0a2966b4 | 2818 | |
969bb4e4 | 2819 | if (unlikely(all_pinned)) { |
0a2966b4 CL |
2820 | cpu_clear(cpu_of(busiest), cpus); |
2821 | if (!cpus_empty(cpus)) | |
2822 | goto redo; | |
2823 | } | |
d6d5cfaf NP |
2824 | } |
2825 | ||
43010659 | 2826 | if (!ld_moved) { |
d15bcfdb | 2827 | schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]); |
89c4710e SS |
2828 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
2829 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) | |
5969fe06 NP |
2830 | return -1; |
2831 | } else | |
16cfb1c0 | 2832 | sd->nr_balance_failed = 0; |
1da177e4 | 2833 | |
43010659 | 2834 | return ld_moved; |
16cfb1c0 NP |
2835 | |
2836 | out_balanced: | |
d15bcfdb | 2837 | schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]); |
48f24c4d | 2838 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
89c4710e | 2839 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
5969fe06 | 2840 | return -1; |
16cfb1c0 | 2841 | sd->nr_balance_failed = 0; |
48f24c4d | 2842 | |
16cfb1c0 | 2843 | return 0; |
1da177e4 LT |
2844 | } |
2845 | ||
2846 | /* | |
2847 | * idle_balance is called by schedule() if this_cpu is about to become | |
2848 | * idle. Attempts to pull tasks from other CPUs. | |
2849 | */ | |
70b97a7f | 2850 | static void idle_balance(int this_cpu, struct rq *this_rq) |
1da177e4 LT |
2851 | { |
2852 | struct sched_domain *sd; | |
dd41f596 IM |
2853 | int pulled_task = -1; |
2854 | unsigned long next_balance = jiffies + HZ; | |
1da177e4 LT |
2855 | |
2856 | for_each_domain(this_cpu, sd) { | |
92c4ca5c CL |
2857 | unsigned long interval; |
2858 | ||
2859 | if (!(sd->flags & SD_LOAD_BALANCE)) | |
2860 | continue; | |
2861 | ||
2862 | if (sd->flags & SD_BALANCE_NEWIDLE) | |
48f24c4d | 2863 | /* If we've pulled tasks over stop searching: */ |
1bd77f2d | 2864 | pulled_task = load_balance_newidle(this_cpu, |
92c4ca5c CL |
2865 | this_rq, sd); |
2866 | ||
2867 | interval = msecs_to_jiffies(sd->balance_interval); | |
2868 | if (time_after(next_balance, sd->last_balance + interval)) | |
2869 | next_balance = sd->last_balance + interval; | |
2870 | if (pulled_task) | |
2871 | break; | |
1da177e4 | 2872 | } |
dd41f596 | 2873 | if (pulled_task || time_after(jiffies, this_rq->next_balance)) { |
1bd77f2d CL |
2874 | /* |
2875 | * We are going idle. next_balance may be set based on | |
2876 | * a busy processor. So reset next_balance. | |
2877 | */ | |
2878 | this_rq->next_balance = next_balance; | |
dd41f596 | 2879 | } |
1da177e4 LT |
2880 | } |
2881 | ||
2882 | /* | |
2883 | * active_load_balance is run by migration threads. It pushes running tasks | |
2884 | * off the busiest CPU onto idle CPUs. It requires at least 1 task to be | |
2885 | * running on each physical CPU where possible, and avoids physical / | |
2886 | * logical imbalances. | |
2887 | * | |
2888 | * Called with busiest_rq locked. | |
2889 | */ | |
70b97a7f | 2890 | static void active_load_balance(struct rq *busiest_rq, int busiest_cpu) |
1da177e4 | 2891 | { |
39507451 | 2892 | int target_cpu = busiest_rq->push_cpu; |
70b97a7f IM |
2893 | struct sched_domain *sd; |
2894 | struct rq *target_rq; | |
39507451 | 2895 | |
48f24c4d | 2896 | /* Is there any task to move? */ |
39507451 | 2897 | if (busiest_rq->nr_running <= 1) |
39507451 NP |
2898 | return; |
2899 | ||
2900 | target_rq = cpu_rq(target_cpu); | |
1da177e4 LT |
2901 | |
2902 | /* | |
39507451 NP |
2903 | * This condition is "impossible", if it occurs |
2904 | * we need to fix it. Originally reported by | |
2905 | * Bjorn Helgaas on a 128-cpu setup. | |
1da177e4 | 2906 | */ |
39507451 | 2907 | BUG_ON(busiest_rq == target_rq); |
1da177e4 | 2908 | |
39507451 NP |
2909 | /* move a task from busiest_rq to target_rq */ |
2910 | double_lock_balance(busiest_rq, target_rq); | |
2911 | ||
2912 | /* Search for an sd spanning us and the target CPU. */ | |
c96d145e | 2913 | for_each_domain(target_cpu, sd) { |
39507451 | 2914 | if ((sd->flags & SD_LOAD_BALANCE) && |
48f24c4d | 2915 | cpu_isset(busiest_cpu, sd->span)) |
39507451 | 2916 | break; |
c96d145e | 2917 | } |
39507451 | 2918 | |
48f24c4d IM |
2919 | if (likely(sd)) { |
2920 | schedstat_inc(sd, alb_cnt); | |
39507451 | 2921 | |
43010659 PW |
2922 | if (move_one_task(target_rq, target_cpu, busiest_rq, |
2923 | sd, CPU_IDLE)) | |
48f24c4d IM |
2924 | schedstat_inc(sd, alb_pushed); |
2925 | else | |
2926 | schedstat_inc(sd, alb_failed); | |
2927 | } | |
39507451 | 2928 | spin_unlock(&target_rq->lock); |
1da177e4 LT |
2929 | } |
2930 | ||
46cb4b7c SS |
2931 | #ifdef CONFIG_NO_HZ |
2932 | static struct { | |
2933 | atomic_t load_balancer; | |
2934 | cpumask_t cpu_mask; | |
2935 | } nohz ____cacheline_aligned = { | |
2936 | .load_balancer = ATOMIC_INIT(-1), | |
2937 | .cpu_mask = CPU_MASK_NONE, | |
2938 | }; | |
2939 | ||
7835b98b | 2940 | /* |
46cb4b7c SS |
2941 | * This routine will try to nominate the ilb (idle load balancing) |
2942 | * owner among the cpus whose ticks are stopped. ilb owner will do the idle | |
2943 | * load balancing on behalf of all those cpus. If all the cpus in the system | |
2944 | * go into this tickless mode, then there will be no ilb owner (as there is | |
2945 | * no need for one) and all the cpus will sleep till the next wakeup event | |
2946 | * arrives... | |
2947 | * | |
2948 | * For the ilb owner, tick is not stopped. And this tick will be used | |
2949 | * for idle load balancing. ilb owner will still be part of | |
2950 | * nohz.cpu_mask.. | |
7835b98b | 2951 | * |
46cb4b7c SS |
2952 | * While stopping the tick, this cpu will become the ilb owner if there |
2953 | * is no other owner. And will be the owner till that cpu becomes busy | |
2954 | * or if all cpus in the system stop their ticks at which point | |
2955 | * there is no need for ilb owner. | |
2956 | * | |
2957 | * When the ilb owner becomes busy, it nominates another owner, during the | |
2958 | * next busy scheduler_tick() | |
2959 | */ | |
2960 | int select_nohz_load_balancer(int stop_tick) | |
2961 | { | |
2962 | int cpu = smp_processor_id(); | |
2963 | ||
2964 | if (stop_tick) { | |
2965 | cpu_set(cpu, nohz.cpu_mask); | |
2966 | cpu_rq(cpu)->in_nohz_recently = 1; | |
2967 | ||
2968 | /* | |
2969 | * If we are going offline and still the leader, give up! | |
2970 | */ | |
2971 | if (cpu_is_offline(cpu) && | |
2972 | atomic_read(&nohz.load_balancer) == cpu) { | |
2973 | if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu) | |
2974 | BUG(); | |
2975 | return 0; | |
2976 | } | |
2977 | ||
2978 | /* time for ilb owner also to sleep */ | |
2979 | if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) { | |
2980 | if (atomic_read(&nohz.load_balancer) == cpu) | |
2981 | atomic_set(&nohz.load_balancer, -1); | |
2982 | return 0; | |
2983 | } | |
2984 | ||
2985 | if (atomic_read(&nohz.load_balancer) == -1) { | |
2986 | /* make me the ilb owner */ | |
2987 | if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1) | |
2988 | return 1; | |
2989 | } else if (atomic_read(&nohz.load_balancer) == cpu) | |
2990 | return 1; | |
2991 | } else { | |
2992 | if (!cpu_isset(cpu, nohz.cpu_mask)) | |
2993 | return 0; | |
2994 | ||
2995 | cpu_clear(cpu, nohz.cpu_mask); | |
2996 | ||
2997 | if (atomic_read(&nohz.load_balancer) == cpu) | |
2998 | if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu) | |
2999 | BUG(); | |
3000 | } | |
3001 | return 0; | |
3002 | } | |
3003 | #endif | |
3004 | ||
3005 | static DEFINE_SPINLOCK(balancing); | |
3006 | ||
3007 | /* | |
7835b98b CL |
3008 | * It checks each scheduling domain to see if it is due to be balanced, |
3009 | * and initiates a balancing operation if so. | |
3010 | * | |
3011 | * Balancing parameters are set up in arch_init_sched_domains. | |
3012 | */ | |
d15bcfdb | 3013 | static inline void rebalance_domains(int cpu, enum cpu_idle_type idle) |
7835b98b | 3014 | { |
46cb4b7c SS |
3015 | int balance = 1; |
3016 | struct rq *rq = cpu_rq(cpu); | |
7835b98b CL |
3017 | unsigned long interval; |
3018 | struct sched_domain *sd; | |
46cb4b7c | 3019 | /* Earliest time when we have to do rebalance again */ |
c9819f45 | 3020 | unsigned long next_balance = jiffies + 60*HZ; |
1da177e4 | 3021 | |
46cb4b7c | 3022 | for_each_domain(cpu, sd) { |
1da177e4 LT |
3023 | if (!(sd->flags & SD_LOAD_BALANCE)) |
3024 | continue; | |
3025 | ||
3026 | interval = sd->balance_interval; | |
d15bcfdb | 3027 | if (idle != CPU_IDLE) |
1da177e4 LT |
3028 | interval *= sd->busy_factor; |
3029 | ||
3030 | /* scale ms to jiffies */ | |
3031 | interval = msecs_to_jiffies(interval); | |
3032 | if (unlikely(!interval)) | |
3033 | interval = 1; | |
dd41f596 IM |
3034 | if (interval > HZ*NR_CPUS/10) |
3035 | interval = HZ*NR_CPUS/10; | |
3036 | ||
1da177e4 | 3037 | |
08c183f3 CL |
3038 | if (sd->flags & SD_SERIALIZE) { |
3039 | if (!spin_trylock(&balancing)) | |
3040 | goto out; | |
3041 | } | |
3042 | ||
c9819f45 | 3043 | if (time_after_eq(jiffies, sd->last_balance + interval)) { |
46cb4b7c | 3044 | if (load_balance(cpu, rq, sd, idle, &balance)) { |
fa3b6ddc SS |
3045 | /* |
3046 | * We've pulled tasks over so either we're no | |
5969fe06 NP |
3047 | * longer idle, or one of our SMT siblings is |
3048 | * not idle. | |
3049 | */ | |
d15bcfdb | 3050 | idle = CPU_NOT_IDLE; |
1da177e4 | 3051 | } |
1bd77f2d | 3052 | sd->last_balance = jiffies; |
1da177e4 | 3053 | } |
08c183f3 CL |
3054 | if (sd->flags & SD_SERIALIZE) |
3055 | spin_unlock(&balancing); | |
3056 | out: | |
c9819f45 CL |
3057 | if (time_after(next_balance, sd->last_balance + interval)) |
3058 | next_balance = sd->last_balance + interval; | |
783609c6 SS |
3059 | |
3060 | /* | |
3061 | * Stop the load balance at this level. There is another | |
3062 | * CPU in our sched group which is doing load balancing more | |
3063 | * actively. | |
3064 | */ | |
3065 | if (!balance) | |
3066 | break; | |
1da177e4 | 3067 | } |
46cb4b7c SS |
3068 | rq->next_balance = next_balance; |
3069 | } | |
3070 | ||
3071 | /* | |
3072 | * run_rebalance_domains is triggered when needed from the scheduler tick. | |
3073 | * In CONFIG_NO_HZ case, the idle load balance owner will do the | |
3074 | * rebalancing for all the cpus for whom scheduler ticks are stopped. | |
3075 | */ | |
3076 | static void run_rebalance_domains(struct softirq_action *h) | |
3077 | { | |
dd41f596 IM |
3078 | int this_cpu = smp_processor_id(); |
3079 | struct rq *this_rq = cpu_rq(this_cpu); | |
3080 | enum cpu_idle_type idle = this_rq->idle_at_tick ? | |
3081 | CPU_IDLE : CPU_NOT_IDLE; | |
46cb4b7c | 3082 | |
dd41f596 | 3083 | rebalance_domains(this_cpu, idle); |
46cb4b7c SS |
3084 | |
3085 | #ifdef CONFIG_NO_HZ | |
3086 | /* | |
3087 | * If this cpu is the owner for idle load balancing, then do the | |
3088 | * balancing on behalf of the other idle cpus whose ticks are | |
3089 | * stopped. | |
3090 | */ | |
dd41f596 IM |
3091 | if (this_rq->idle_at_tick && |
3092 | atomic_read(&nohz.load_balancer) == this_cpu) { | |
46cb4b7c SS |
3093 | cpumask_t cpus = nohz.cpu_mask; |
3094 | struct rq *rq; | |
3095 | int balance_cpu; | |
3096 | ||
dd41f596 | 3097 | cpu_clear(this_cpu, cpus); |
46cb4b7c SS |
3098 | for_each_cpu_mask(balance_cpu, cpus) { |
3099 | /* | |
3100 | * If this cpu gets work to do, stop the load balancing | |
3101 | * work being done for other cpus. Next load | |
3102 | * balancing owner will pick it up. | |
3103 | */ | |
3104 | if (need_resched()) | |
3105 | break; | |
3106 | ||
dd41f596 | 3107 | rebalance_domains(balance_cpu, SCHED_IDLE); |
46cb4b7c SS |
3108 | |
3109 | rq = cpu_rq(balance_cpu); | |
dd41f596 IM |
3110 | if (time_after(this_rq->next_balance, rq->next_balance)) |
3111 | this_rq->next_balance = rq->next_balance; | |
46cb4b7c SS |
3112 | } |
3113 | } | |
3114 | #endif | |
3115 | } | |
3116 | ||
3117 | /* | |
3118 | * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing. | |
3119 | * | |
3120 | * In case of CONFIG_NO_HZ, this is the place where we nominate a new | |
3121 | * idle load balancing owner or decide to stop the periodic load balancing, | |
3122 | * if the whole system is idle. | |
3123 | */ | |
dd41f596 | 3124 | static inline void trigger_load_balance(struct rq *rq, int cpu) |
46cb4b7c | 3125 | { |
46cb4b7c SS |
3126 | #ifdef CONFIG_NO_HZ |
3127 | /* | |
3128 | * If we were in the nohz mode recently and busy at the current | |
3129 | * scheduler tick, then check if we need to nominate new idle | |
3130 | * load balancer. | |
3131 | */ | |
3132 | if (rq->in_nohz_recently && !rq->idle_at_tick) { | |
3133 | rq->in_nohz_recently = 0; | |
3134 | ||
3135 | if (atomic_read(&nohz.load_balancer) == cpu) { | |
3136 | cpu_clear(cpu, nohz.cpu_mask); | |
3137 | atomic_set(&nohz.load_balancer, -1); | |
3138 | } | |
3139 | ||
3140 | if (atomic_read(&nohz.load_balancer) == -1) { | |
3141 | /* | |
3142 | * simple selection for now: Nominate the | |
3143 | * first cpu in the nohz list to be the next | |
3144 | * ilb owner. | |
3145 | * | |
3146 | * TBD: Traverse the sched domains and nominate | |
3147 | * the nearest cpu in the nohz.cpu_mask. | |
3148 | */ | |
3149 | int ilb = first_cpu(nohz.cpu_mask); | |
3150 | ||
3151 | if (ilb != NR_CPUS) | |
3152 | resched_cpu(ilb); | |
3153 | } | |
3154 | } | |
3155 | ||
3156 | /* | |
3157 | * If this cpu is idle and doing idle load balancing for all the | |
3158 | * cpus with ticks stopped, is it time for that to stop? | |
3159 | */ | |
3160 | if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu && | |
3161 | cpus_weight(nohz.cpu_mask) == num_online_cpus()) { | |
3162 | resched_cpu(cpu); | |
3163 | return; | |
3164 | } | |
3165 | ||
3166 | /* | |
3167 | * If this cpu is idle and the idle load balancing is done by | |
3168 | * someone else, then no need raise the SCHED_SOFTIRQ | |
3169 | */ | |
3170 | if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu && | |
3171 | cpu_isset(cpu, nohz.cpu_mask)) | |
3172 | return; | |
3173 | #endif | |
3174 | if (time_after_eq(jiffies, rq->next_balance)) | |
3175 | raise_softirq(SCHED_SOFTIRQ); | |
1da177e4 | 3176 | } |
dd41f596 IM |
3177 | |
3178 | #else /* CONFIG_SMP */ | |
3179 | ||
1da177e4 LT |
3180 | /* |
3181 | * on UP we do not need to balance between CPUs: | |
3182 | */ | |
70b97a7f | 3183 | static inline void idle_balance(int cpu, struct rq *rq) |
1da177e4 LT |
3184 | { |
3185 | } | |
dd41f596 IM |
3186 | |
3187 | /* Avoid "used but not defined" warning on UP */ | |
3188 | static int balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, | |
3189 | unsigned long max_nr_move, unsigned long max_load_move, | |
3190 | struct sched_domain *sd, enum cpu_idle_type idle, | |
3191 | int *all_pinned, unsigned long *load_moved, | |
a4ac01c3 | 3192 | int *this_best_prio, struct rq_iterator *iterator) |
dd41f596 IM |
3193 | { |
3194 | *load_moved = 0; | |
3195 | ||
3196 | return 0; | |
3197 | } | |
3198 | ||
1da177e4 LT |
3199 | #endif |
3200 | ||
1da177e4 LT |
3201 | DEFINE_PER_CPU(struct kernel_stat, kstat); |
3202 | ||
3203 | EXPORT_PER_CPU_SYMBOL(kstat); | |
3204 | ||
3205 | /* | |
41b86e9c IM |
3206 | * Return p->sum_exec_runtime plus any more ns on the sched_clock |
3207 | * that have not yet been banked in case the task is currently running. | |
1da177e4 | 3208 | */ |
41b86e9c | 3209 | unsigned long long task_sched_runtime(struct task_struct *p) |
1da177e4 | 3210 | { |
1da177e4 | 3211 | unsigned long flags; |
41b86e9c IM |
3212 | u64 ns, delta_exec; |
3213 | struct rq *rq; | |
48f24c4d | 3214 | |
41b86e9c IM |
3215 | rq = task_rq_lock(p, &flags); |
3216 | ns = p->se.sum_exec_runtime; | |
3217 | if (rq->curr == p) { | |
a8e504d2 IM |
3218 | update_rq_clock(rq); |
3219 | delta_exec = rq->clock - p->se.exec_start; | |
41b86e9c IM |
3220 | if ((s64)delta_exec > 0) |
3221 | ns += delta_exec; | |
3222 | } | |
3223 | task_rq_unlock(rq, &flags); | |
48f24c4d | 3224 | |
1da177e4 LT |
3225 | return ns; |
3226 | } | |
3227 | ||
1da177e4 LT |
3228 | /* |
3229 | * Account user cpu time to a process. | |
3230 | * @p: the process that the cpu time gets accounted to | |
3231 | * @hardirq_offset: the offset to subtract from hardirq_count() | |
3232 | * @cputime: the cpu time spent in user space since the last update | |
3233 | */ | |
3234 | void account_user_time(struct task_struct *p, cputime_t cputime) | |
3235 | { | |
3236 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | |
3237 | cputime64_t tmp; | |
3238 | ||
3239 | p->utime = cputime_add(p->utime, cputime); | |
3240 | ||
3241 | /* Add user time to cpustat. */ | |
3242 | tmp = cputime_to_cputime64(cputime); | |
3243 | if (TASK_NICE(p) > 0) | |
3244 | cpustat->nice = cputime64_add(cpustat->nice, tmp); | |
3245 | else | |
3246 | cpustat->user = cputime64_add(cpustat->user, tmp); | |
3247 | } | |
3248 | ||
3249 | /* | |
3250 | * Account system cpu time to a process. | |
3251 | * @p: the process that the cpu time gets accounted to | |
3252 | * @hardirq_offset: the offset to subtract from hardirq_count() | |
3253 | * @cputime: the cpu time spent in kernel space since the last update | |
3254 | */ | |
3255 | void account_system_time(struct task_struct *p, int hardirq_offset, | |
3256 | cputime_t cputime) | |
3257 | { | |
3258 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | |
70b97a7f | 3259 | struct rq *rq = this_rq(); |
1da177e4 LT |
3260 | cputime64_t tmp; |
3261 | ||
3262 | p->stime = cputime_add(p->stime, cputime); | |
3263 | ||
3264 | /* Add system time to cpustat. */ | |
3265 | tmp = cputime_to_cputime64(cputime); | |
3266 | if (hardirq_count() - hardirq_offset) | |
3267 | cpustat->irq = cputime64_add(cpustat->irq, tmp); | |
3268 | else if (softirq_count()) | |
3269 | cpustat->softirq = cputime64_add(cpustat->softirq, tmp); | |
3270 | else if (p != rq->idle) | |
3271 | cpustat->system = cputime64_add(cpustat->system, tmp); | |
3272 | else if (atomic_read(&rq->nr_iowait) > 0) | |
3273 | cpustat->iowait = cputime64_add(cpustat->iowait, tmp); | |
3274 | else | |
3275 | cpustat->idle = cputime64_add(cpustat->idle, tmp); | |
3276 | /* Account for system time used */ | |
3277 | acct_update_integrals(p); | |
1da177e4 LT |
3278 | } |
3279 | ||
3280 | /* | |
3281 | * Account for involuntary wait time. | |
3282 | * @p: the process from which the cpu time has been stolen | |
3283 | * @steal: the cpu time spent in involuntary wait | |
3284 | */ | |
3285 | void account_steal_time(struct task_struct *p, cputime_t steal) | |
3286 | { | |
3287 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | |
3288 | cputime64_t tmp = cputime_to_cputime64(steal); | |
70b97a7f | 3289 | struct rq *rq = this_rq(); |
1da177e4 LT |
3290 | |
3291 | if (p == rq->idle) { | |
3292 | p->stime = cputime_add(p->stime, steal); | |
3293 | if (atomic_read(&rq->nr_iowait) > 0) | |
3294 | cpustat->iowait = cputime64_add(cpustat->iowait, tmp); | |
3295 | else | |
3296 | cpustat->idle = cputime64_add(cpustat->idle, tmp); | |
3297 | } else | |
3298 | cpustat->steal = cputime64_add(cpustat->steal, tmp); | |
3299 | } | |
3300 | ||
7835b98b CL |
3301 | /* |
3302 | * This function gets called by the timer code, with HZ frequency. | |
3303 | * We call it with interrupts disabled. | |
3304 | * | |
3305 | * It also gets called by the fork code, when changing the parent's | |
3306 | * timeslices. | |
3307 | */ | |
3308 | void scheduler_tick(void) | |
3309 | { | |
7835b98b CL |
3310 | int cpu = smp_processor_id(); |
3311 | struct rq *rq = cpu_rq(cpu); | |
dd41f596 IM |
3312 | struct task_struct *curr = rq->curr; |
3313 | ||
3314 | spin_lock(&rq->lock); | |
f1a438d8 | 3315 | update_cpu_load(rq); |
dd41f596 IM |
3316 | if (curr != rq->idle) /* FIXME: needed? */ |
3317 | curr->sched_class->task_tick(rq, curr); | |
dd41f596 | 3318 | spin_unlock(&rq->lock); |
7835b98b | 3319 | |
e418e1c2 | 3320 | #ifdef CONFIG_SMP |
dd41f596 IM |
3321 | rq->idle_at_tick = idle_cpu(cpu); |
3322 | trigger_load_balance(rq, cpu); | |
e418e1c2 | 3323 | #endif |
1da177e4 LT |
3324 | } |
3325 | ||
1da177e4 LT |
3326 | #if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT) |
3327 | ||
3328 | void fastcall add_preempt_count(int val) | |
3329 | { | |
3330 | /* | |
3331 | * Underflow? | |
3332 | */ | |
9a11b49a IM |
3333 | if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0))) |
3334 | return; | |
1da177e4 LT |
3335 | preempt_count() += val; |
3336 | /* | |
3337 | * Spinlock count overflowing soon? | |
3338 | */ | |
33859f7f MOS |
3339 | DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >= |
3340 | PREEMPT_MASK - 10); | |
1da177e4 LT |
3341 | } |
3342 | EXPORT_SYMBOL(add_preempt_count); | |
3343 | ||
3344 | void fastcall sub_preempt_count(int val) | |
3345 | { | |
3346 | /* | |
3347 | * Underflow? | |
3348 | */ | |
9a11b49a IM |
3349 | if (DEBUG_LOCKS_WARN_ON(val > preempt_count())) |
3350 | return; | |
1da177e4 LT |
3351 | /* |
3352 | * Is the spinlock portion underflowing? | |
3353 | */ | |
9a11b49a IM |
3354 | if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) && |
3355 | !(preempt_count() & PREEMPT_MASK))) | |
3356 | return; | |
3357 | ||
1da177e4 LT |
3358 | preempt_count() -= val; |
3359 | } | |
3360 | EXPORT_SYMBOL(sub_preempt_count); | |
3361 | ||
3362 | #endif | |
3363 | ||
3364 | /* | |
dd41f596 | 3365 | * Print scheduling while atomic bug: |
1da177e4 | 3366 | */ |
dd41f596 | 3367 | static noinline void __schedule_bug(struct task_struct *prev) |
1da177e4 | 3368 | { |
dd41f596 IM |
3369 | printk(KERN_ERR "BUG: scheduling while atomic: %s/0x%08x/%d\n", |
3370 | prev->comm, preempt_count(), prev->pid); | |
3371 | debug_show_held_locks(prev); | |
3372 | if (irqs_disabled()) | |
3373 | print_irqtrace_events(prev); | |
3374 | dump_stack(); | |
3375 | } | |
1da177e4 | 3376 | |
dd41f596 IM |
3377 | /* |
3378 | * Various schedule()-time debugging checks and statistics: | |
3379 | */ | |
3380 | static inline void schedule_debug(struct task_struct *prev) | |
3381 | { | |
1da177e4 LT |
3382 | /* |
3383 | * Test if we are atomic. Since do_exit() needs to call into | |
3384 | * schedule() atomically, we ignore that path for now. | |
3385 | * Otherwise, whine if we are scheduling when we should not be. | |
3386 | */ | |
dd41f596 IM |
3387 | if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state)) |
3388 | __schedule_bug(prev); | |
3389 | ||
1da177e4 LT |
3390 | profile_hit(SCHED_PROFILING, __builtin_return_address(0)); |
3391 | ||
dd41f596 IM |
3392 | schedstat_inc(this_rq(), sched_cnt); |
3393 | } | |
3394 | ||
3395 | /* | |
3396 | * Pick up the highest-prio task: | |
3397 | */ | |
3398 | static inline struct task_struct * | |
ff95f3df | 3399 | pick_next_task(struct rq *rq, struct task_struct *prev) |
dd41f596 IM |
3400 | { |
3401 | struct sched_class *class; | |
3402 | struct task_struct *p; | |
1da177e4 LT |
3403 | |
3404 | /* | |
dd41f596 IM |
3405 | * Optimization: we know that if all tasks are in |
3406 | * the fair class we can call that function directly: | |
1da177e4 | 3407 | */ |
dd41f596 | 3408 | if (likely(rq->nr_running == rq->cfs.nr_running)) { |
fb8d4724 | 3409 | p = fair_sched_class.pick_next_task(rq); |
dd41f596 IM |
3410 | if (likely(p)) |
3411 | return p; | |
1da177e4 LT |
3412 | } |
3413 | ||
dd41f596 IM |
3414 | class = sched_class_highest; |
3415 | for ( ; ; ) { | |
fb8d4724 | 3416 | p = class->pick_next_task(rq); |
dd41f596 IM |
3417 | if (p) |
3418 | return p; | |
3419 | /* | |
3420 | * Will never be NULL as the idle class always | |
3421 | * returns a non-NULL p: | |
3422 | */ | |
3423 | class = class->next; | |
3424 | } | |
3425 | } | |
1da177e4 | 3426 | |
dd41f596 IM |
3427 | /* |
3428 | * schedule() is the main scheduler function. | |
3429 | */ | |
3430 | asmlinkage void __sched schedule(void) | |
3431 | { | |
3432 | struct task_struct *prev, *next; | |
3433 | long *switch_count; | |
3434 | struct rq *rq; | |
3435 | u64 now; | |
3436 | int cpu; | |
3437 | ||
3438 | need_resched: | |
3439 | preempt_disable(); | |
3440 | cpu = smp_processor_id(); | |
3441 | rq = cpu_rq(cpu); | |
3442 | rcu_qsctr_inc(cpu); | |
3443 | prev = rq->curr; | |
3444 | switch_count = &prev->nivcsw; | |
3445 | ||
3446 | release_kernel_lock(prev); | |
3447 | need_resched_nonpreemptible: | |
3448 | ||
3449 | schedule_debug(prev); | |
1da177e4 LT |
3450 | |
3451 | spin_lock_irq(&rq->lock); | |
dd41f596 | 3452 | clear_tsk_need_resched(prev); |
c1b3da3e IM |
3453 | __update_rq_clock(rq); |
3454 | now = rq->clock; | |
1da177e4 | 3455 | |
1da177e4 | 3456 | if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) { |
1da177e4 | 3457 | if (unlikely((prev->state & TASK_INTERRUPTIBLE) && |
dd41f596 | 3458 | unlikely(signal_pending(prev)))) { |
1da177e4 | 3459 | prev->state = TASK_RUNNING; |
dd41f596 | 3460 | } else { |
8e717b19 | 3461 | deactivate_task(rq, prev, 1, now); |
1da177e4 | 3462 | } |
dd41f596 | 3463 | switch_count = &prev->nvcsw; |
1da177e4 LT |
3464 | } |
3465 | ||
dd41f596 | 3466 | if (unlikely(!rq->nr_running)) |
1da177e4 | 3467 | idle_balance(cpu, rq); |
1da177e4 | 3468 | |
31ee529c | 3469 | prev->sched_class->put_prev_task(rq, prev); |
ff95f3df | 3470 | next = pick_next_task(rq, prev); |
1da177e4 LT |
3471 | |
3472 | sched_info_switch(prev, next); | |
dd41f596 | 3473 | |
1da177e4 | 3474 | if (likely(prev != next)) { |
1da177e4 LT |
3475 | rq->nr_switches++; |
3476 | rq->curr = next; | |
3477 | ++*switch_count; | |
3478 | ||
dd41f596 | 3479 | context_switch(rq, prev, next); /* unlocks the rq */ |
1da177e4 LT |
3480 | } else |
3481 | spin_unlock_irq(&rq->lock); | |
3482 | ||
dd41f596 IM |
3483 | if (unlikely(reacquire_kernel_lock(current) < 0)) { |
3484 | cpu = smp_processor_id(); | |
3485 | rq = cpu_rq(cpu); | |
1da177e4 | 3486 | goto need_resched_nonpreemptible; |
dd41f596 | 3487 | } |
1da177e4 LT |
3488 | preempt_enable_no_resched(); |
3489 | if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) | |
3490 | goto need_resched; | |
3491 | } | |
1da177e4 LT |
3492 | EXPORT_SYMBOL(schedule); |
3493 | ||
3494 | #ifdef CONFIG_PREEMPT | |
3495 | /* | |
2ed6e34f | 3496 | * this is the entry point to schedule() from in-kernel preemption |
1da177e4 LT |
3497 | * off of preempt_enable. Kernel preemptions off return from interrupt |
3498 | * occur there and call schedule directly. | |
3499 | */ | |
3500 | asmlinkage void __sched preempt_schedule(void) | |
3501 | { | |
3502 | struct thread_info *ti = current_thread_info(); | |
3503 | #ifdef CONFIG_PREEMPT_BKL | |
3504 | struct task_struct *task = current; | |
3505 | int saved_lock_depth; | |
3506 | #endif | |
3507 | /* | |
3508 | * If there is a non-zero preempt_count or interrupts are disabled, | |
3509 | * we do not want to preempt the current task. Just return.. | |
3510 | */ | |
beed33a8 | 3511 | if (likely(ti->preempt_count || irqs_disabled())) |
1da177e4 LT |
3512 | return; |
3513 | ||
3514 | need_resched: | |
3515 | add_preempt_count(PREEMPT_ACTIVE); | |
3516 | /* | |
3517 | * We keep the big kernel semaphore locked, but we | |
3518 | * clear ->lock_depth so that schedule() doesnt | |
3519 | * auto-release the semaphore: | |
3520 | */ | |
3521 | #ifdef CONFIG_PREEMPT_BKL | |
3522 | saved_lock_depth = task->lock_depth; | |
3523 | task->lock_depth = -1; | |
3524 | #endif | |
3525 | schedule(); | |
3526 | #ifdef CONFIG_PREEMPT_BKL | |
3527 | task->lock_depth = saved_lock_depth; | |
3528 | #endif | |
3529 | sub_preempt_count(PREEMPT_ACTIVE); | |
3530 | ||
3531 | /* we could miss a preemption opportunity between schedule and now */ | |
3532 | barrier(); | |
3533 | if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) | |
3534 | goto need_resched; | |
3535 | } | |
1da177e4 LT |
3536 | EXPORT_SYMBOL(preempt_schedule); |
3537 | ||
3538 | /* | |
2ed6e34f | 3539 | * this is the entry point to schedule() from kernel preemption |
1da177e4 LT |
3540 | * off of irq context. |
3541 | * Note, that this is called and return with irqs disabled. This will | |
3542 | * protect us against recursive calling from irq. | |
3543 | */ | |
3544 | asmlinkage void __sched preempt_schedule_irq(void) | |
3545 | { | |
3546 | struct thread_info *ti = current_thread_info(); | |
3547 | #ifdef CONFIG_PREEMPT_BKL | |
3548 | struct task_struct *task = current; | |
3549 | int saved_lock_depth; | |
3550 | #endif | |
2ed6e34f | 3551 | /* Catch callers which need to be fixed */ |
1da177e4 LT |
3552 | BUG_ON(ti->preempt_count || !irqs_disabled()); |
3553 | ||
3554 | need_resched: | |
3555 | add_preempt_count(PREEMPT_ACTIVE); | |
3556 | /* | |
3557 | * We keep the big kernel semaphore locked, but we | |
3558 | * clear ->lock_depth so that schedule() doesnt | |
3559 | * auto-release the semaphore: | |
3560 | */ | |
3561 | #ifdef CONFIG_PREEMPT_BKL | |
3562 | saved_lock_depth = task->lock_depth; | |
3563 | task->lock_depth = -1; | |
3564 | #endif | |
3565 | local_irq_enable(); | |
3566 | schedule(); | |
3567 | local_irq_disable(); | |
3568 | #ifdef CONFIG_PREEMPT_BKL | |
3569 | task->lock_depth = saved_lock_depth; | |
3570 | #endif | |
3571 | sub_preempt_count(PREEMPT_ACTIVE); | |
3572 | ||
3573 | /* we could miss a preemption opportunity between schedule and now */ | |
3574 | barrier(); | |
3575 | if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) | |
3576 | goto need_resched; | |
3577 | } | |
3578 | ||
3579 | #endif /* CONFIG_PREEMPT */ | |
3580 | ||
95cdf3b7 IM |
3581 | int default_wake_function(wait_queue_t *curr, unsigned mode, int sync, |
3582 | void *key) | |
1da177e4 | 3583 | { |
48f24c4d | 3584 | return try_to_wake_up(curr->private, mode, sync); |
1da177e4 | 3585 | } |
1da177e4 LT |
3586 | EXPORT_SYMBOL(default_wake_function); |
3587 | ||
3588 | /* | |
3589 | * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just | |
3590 | * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve | |
3591 | * number) then we wake all the non-exclusive tasks and one exclusive task. | |
3592 | * | |
3593 | * There are circumstances in which we can try to wake a task which has already | |
3594 | * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns | |
3595 | * zero in this (rare) case, and we handle it by continuing to scan the queue. | |
3596 | */ | |
3597 | static void __wake_up_common(wait_queue_head_t *q, unsigned int mode, | |
3598 | int nr_exclusive, int sync, void *key) | |
3599 | { | |
3600 | struct list_head *tmp, *next; | |
3601 | ||
3602 | list_for_each_safe(tmp, next, &q->task_list) { | |
48f24c4d IM |
3603 | wait_queue_t *curr = list_entry(tmp, wait_queue_t, task_list); |
3604 | unsigned flags = curr->flags; | |
3605 | ||
1da177e4 | 3606 | if (curr->func(curr, mode, sync, key) && |
48f24c4d | 3607 | (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive) |
1da177e4 LT |
3608 | break; |
3609 | } | |
3610 | } | |
3611 | ||
3612 | /** | |
3613 | * __wake_up - wake up threads blocked on a waitqueue. | |
3614 | * @q: the waitqueue | |
3615 | * @mode: which threads | |
3616 | * @nr_exclusive: how many wake-one or wake-many threads to wake up | |
67be2dd1 | 3617 | * @key: is directly passed to the wakeup function |
1da177e4 LT |
3618 | */ |
3619 | void fastcall __wake_up(wait_queue_head_t *q, unsigned int mode, | |
95cdf3b7 | 3620 | int nr_exclusive, void *key) |
1da177e4 LT |
3621 | { |
3622 | unsigned long flags; | |
3623 | ||
3624 | spin_lock_irqsave(&q->lock, flags); | |
3625 | __wake_up_common(q, mode, nr_exclusive, 0, key); | |
3626 | spin_unlock_irqrestore(&q->lock, flags); | |
3627 | } | |
1da177e4 LT |
3628 | EXPORT_SYMBOL(__wake_up); |
3629 | ||
3630 | /* | |
3631 | * Same as __wake_up but called with the spinlock in wait_queue_head_t held. | |
3632 | */ | |
3633 | void fastcall __wake_up_locked(wait_queue_head_t *q, unsigned int mode) | |
3634 | { | |
3635 | __wake_up_common(q, mode, 1, 0, NULL); | |
3636 | } | |
3637 | ||
3638 | /** | |
67be2dd1 | 3639 | * __wake_up_sync - wake up threads blocked on a waitqueue. |
1da177e4 LT |
3640 | * @q: the waitqueue |
3641 | * @mode: which threads | |
3642 | * @nr_exclusive: how many wake-one or wake-many threads to wake up | |
3643 | * | |
3644 | * The sync wakeup differs that the waker knows that it will schedule | |
3645 | * away soon, so while the target thread will be woken up, it will not | |
3646 | * be migrated to another CPU - ie. the two threads are 'synchronized' | |
3647 | * with each other. This can prevent needless bouncing between CPUs. | |
3648 | * | |
3649 | * On UP it can prevent extra preemption. | |
3650 | */ | |
95cdf3b7 IM |
3651 | void fastcall |
3652 | __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive) | |
1da177e4 LT |
3653 | { |
3654 | unsigned long flags; | |
3655 | int sync = 1; | |
3656 | ||
3657 | if (unlikely(!q)) | |
3658 | return; | |
3659 | ||
3660 | if (unlikely(!nr_exclusive)) | |
3661 | sync = 0; | |
3662 | ||
3663 | spin_lock_irqsave(&q->lock, flags); | |
3664 | __wake_up_common(q, mode, nr_exclusive, sync, NULL); | |
3665 | spin_unlock_irqrestore(&q->lock, flags); | |
3666 | } | |
3667 | EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */ | |
3668 | ||
3669 | void fastcall complete(struct completion *x) | |
3670 | { | |
3671 | unsigned long flags; | |
3672 | ||
3673 | spin_lock_irqsave(&x->wait.lock, flags); | |
3674 | x->done++; | |
3675 | __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, | |
3676 | 1, 0, NULL); | |
3677 | spin_unlock_irqrestore(&x->wait.lock, flags); | |
3678 | } | |
3679 | EXPORT_SYMBOL(complete); | |
3680 | ||
3681 | void fastcall complete_all(struct completion *x) | |
3682 | { | |
3683 | unsigned long flags; | |
3684 | ||
3685 | spin_lock_irqsave(&x->wait.lock, flags); | |
3686 | x->done += UINT_MAX/2; | |
3687 | __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, | |
3688 | 0, 0, NULL); | |
3689 | spin_unlock_irqrestore(&x->wait.lock, flags); | |
3690 | } | |
3691 | EXPORT_SYMBOL(complete_all); | |
3692 | ||
3693 | void fastcall __sched wait_for_completion(struct completion *x) | |
3694 | { | |
3695 | might_sleep(); | |
48f24c4d | 3696 | |
1da177e4 LT |
3697 | spin_lock_irq(&x->wait.lock); |
3698 | if (!x->done) { | |
3699 | DECLARE_WAITQUEUE(wait, current); | |
3700 | ||
3701 | wait.flags |= WQ_FLAG_EXCLUSIVE; | |
3702 | __add_wait_queue_tail(&x->wait, &wait); | |
3703 | do { | |
3704 | __set_current_state(TASK_UNINTERRUPTIBLE); | |
3705 | spin_unlock_irq(&x->wait.lock); | |
3706 | schedule(); | |
3707 | spin_lock_irq(&x->wait.lock); | |
3708 | } while (!x->done); | |
3709 | __remove_wait_queue(&x->wait, &wait); | |
3710 | } | |
3711 | x->done--; | |
3712 | spin_unlock_irq(&x->wait.lock); | |
3713 | } | |
3714 | EXPORT_SYMBOL(wait_for_completion); | |
3715 | ||
3716 | unsigned long fastcall __sched | |
3717 | wait_for_completion_timeout(struct completion *x, unsigned long timeout) | |
3718 | { | |
3719 | might_sleep(); | |
3720 | ||
3721 | spin_lock_irq(&x->wait.lock); | |
3722 | if (!x->done) { | |
3723 | DECLARE_WAITQUEUE(wait, current); | |
3724 | ||
3725 | wait.flags |= WQ_FLAG_EXCLUSIVE; | |
3726 | __add_wait_queue_tail(&x->wait, &wait); | |
3727 | do { | |
3728 | __set_current_state(TASK_UNINTERRUPTIBLE); | |
3729 | spin_unlock_irq(&x->wait.lock); | |
3730 | timeout = schedule_timeout(timeout); | |
3731 | spin_lock_irq(&x->wait.lock); | |
3732 | if (!timeout) { | |
3733 | __remove_wait_queue(&x->wait, &wait); | |
3734 | goto out; | |
3735 | } | |
3736 | } while (!x->done); | |
3737 | __remove_wait_queue(&x->wait, &wait); | |
3738 | } | |
3739 | x->done--; | |
3740 | out: | |
3741 | spin_unlock_irq(&x->wait.lock); | |
3742 | return timeout; | |
3743 | } | |
3744 | EXPORT_SYMBOL(wait_for_completion_timeout); | |
3745 | ||
3746 | int fastcall __sched wait_for_completion_interruptible(struct completion *x) | |
3747 | { | |
3748 | int ret = 0; | |
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 | ret = -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 | schedule(); | |
3767 | spin_lock_irq(&x->wait.lock); | |
3768 | } while (!x->done); | |
3769 | __remove_wait_queue(&x->wait, &wait); | |
3770 | } | |
3771 | x->done--; | |
3772 | out: | |
3773 | spin_unlock_irq(&x->wait.lock); | |
3774 | ||
3775 | return ret; | |
3776 | } | |
3777 | EXPORT_SYMBOL(wait_for_completion_interruptible); | |
3778 | ||
3779 | unsigned long fastcall __sched | |
3780 | wait_for_completion_interruptible_timeout(struct completion *x, | |
3781 | unsigned long timeout) | |
3782 | { | |
3783 | might_sleep(); | |
3784 | ||
3785 | spin_lock_irq(&x->wait.lock); | |
3786 | if (!x->done) { | |
3787 | DECLARE_WAITQUEUE(wait, current); | |
3788 | ||
3789 | wait.flags |= WQ_FLAG_EXCLUSIVE; | |
3790 | __add_wait_queue_tail(&x->wait, &wait); | |
3791 | do { | |
3792 | if (signal_pending(current)) { | |
3793 | timeout = -ERESTARTSYS; | |
3794 | __remove_wait_queue(&x->wait, &wait); | |
3795 | goto out; | |
3796 | } | |
3797 | __set_current_state(TASK_INTERRUPTIBLE); | |
3798 | spin_unlock_irq(&x->wait.lock); | |
3799 | timeout = schedule_timeout(timeout); | |
3800 | spin_lock_irq(&x->wait.lock); | |
3801 | if (!timeout) { | |
3802 | __remove_wait_queue(&x->wait, &wait); | |
3803 | goto out; | |
3804 | } | |
3805 | } while (!x->done); | |
3806 | __remove_wait_queue(&x->wait, &wait); | |
3807 | } | |
3808 | x->done--; | |
3809 | out: | |
3810 | spin_unlock_irq(&x->wait.lock); | |
3811 | return timeout; | |
3812 | } | |
3813 | EXPORT_SYMBOL(wait_for_completion_interruptible_timeout); | |
3814 | ||
0fec171c IM |
3815 | static inline void |
3816 | sleep_on_head(wait_queue_head_t *q, wait_queue_t *wait, unsigned long *flags) | |
3817 | { | |
3818 | spin_lock_irqsave(&q->lock, *flags); | |
3819 | __add_wait_queue(q, wait); | |
1da177e4 | 3820 | spin_unlock(&q->lock); |
0fec171c | 3821 | } |
1da177e4 | 3822 | |
0fec171c IM |
3823 | static inline void |
3824 | sleep_on_tail(wait_queue_head_t *q, wait_queue_t *wait, unsigned long *flags) | |
3825 | { | |
3826 | spin_lock_irq(&q->lock); | |
3827 | __remove_wait_queue(q, wait); | |
3828 | spin_unlock_irqrestore(&q->lock, *flags); | |
3829 | } | |
1da177e4 | 3830 | |
0fec171c | 3831 | void __sched interruptible_sleep_on(wait_queue_head_t *q) |
1da177e4 | 3832 | { |
0fec171c IM |
3833 | unsigned long flags; |
3834 | wait_queue_t wait; | |
3835 | ||
3836 | init_waitqueue_entry(&wait, current); | |
1da177e4 LT |
3837 | |
3838 | current->state = TASK_INTERRUPTIBLE; | |
3839 | ||
0fec171c | 3840 | sleep_on_head(q, &wait, &flags); |
1da177e4 | 3841 | schedule(); |
0fec171c | 3842 | sleep_on_tail(q, &wait, &flags); |
1da177e4 | 3843 | } |
1da177e4 LT |
3844 | EXPORT_SYMBOL(interruptible_sleep_on); |
3845 | ||
0fec171c | 3846 | long __sched |
95cdf3b7 | 3847 | interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout) |
1da177e4 | 3848 | { |
0fec171c IM |
3849 | unsigned long flags; |
3850 | wait_queue_t wait; | |
3851 | ||
3852 | init_waitqueue_entry(&wait, current); | |
1da177e4 LT |
3853 | |
3854 | current->state = TASK_INTERRUPTIBLE; | |
3855 | ||
0fec171c | 3856 | sleep_on_head(q, &wait, &flags); |
1da177e4 | 3857 | timeout = schedule_timeout(timeout); |
0fec171c | 3858 | sleep_on_tail(q, &wait, &flags); |
1da177e4 LT |
3859 | |
3860 | return timeout; | |
3861 | } | |
1da177e4 LT |
3862 | EXPORT_SYMBOL(interruptible_sleep_on_timeout); |
3863 | ||
0fec171c | 3864 | void __sched sleep_on(wait_queue_head_t *q) |
1da177e4 | 3865 | { |
0fec171c IM |
3866 | unsigned long flags; |
3867 | wait_queue_t wait; | |
3868 | ||
3869 | init_waitqueue_entry(&wait, current); | |
1da177e4 LT |
3870 | |
3871 | current->state = TASK_UNINTERRUPTIBLE; | |
3872 | ||
0fec171c | 3873 | sleep_on_head(q, &wait, &flags); |
1da177e4 | 3874 | schedule(); |
0fec171c | 3875 | sleep_on_tail(q, &wait, &flags); |
1da177e4 | 3876 | } |
1da177e4 LT |
3877 | EXPORT_SYMBOL(sleep_on); |
3878 | ||
0fec171c | 3879 | long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout) |
1da177e4 | 3880 | { |
0fec171c IM |
3881 | unsigned long flags; |
3882 | wait_queue_t wait; | |
3883 | ||
3884 | init_waitqueue_entry(&wait, current); | |
1da177e4 LT |
3885 | |
3886 | current->state = TASK_UNINTERRUPTIBLE; | |
3887 | ||
0fec171c | 3888 | sleep_on_head(q, &wait, &flags); |
1da177e4 | 3889 | timeout = schedule_timeout(timeout); |
0fec171c | 3890 | sleep_on_tail(q, &wait, &flags); |
1da177e4 LT |
3891 | |
3892 | return timeout; | |
3893 | } | |
1da177e4 LT |
3894 | EXPORT_SYMBOL(sleep_on_timeout); |
3895 | ||
b29739f9 IM |
3896 | #ifdef CONFIG_RT_MUTEXES |
3897 | ||
3898 | /* | |
3899 | * rt_mutex_setprio - set the current priority of a task | |
3900 | * @p: task | |
3901 | * @prio: prio value (kernel-internal form) | |
3902 | * | |
3903 | * This function changes the 'effective' priority of a task. It does | |
3904 | * not touch ->normal_prio like __setscheduler(). | |
3905 | * | |
3906 | * Used by the rt_mutex code to implement priority inheritance logic. | |
3907 | */ | |
36c8b586 | 3908 | void rt_mutex_setprio(struct task_struct *p, int prio) |
b29739f9 IM |
3909 | { |
3910 | unsigned long flags; | |
dd41f596 | 3911 | int oldprio, on_rq; |
70b97a7f | 3912 | struct rq *rq; |
dd41f596 | 3913 | u64 now; |
b29739f9 IM |
3914 | |
3915 | BUG_ON(prio < 0 || prio > MAX_PRIO); | |
3916 | ||
3917 | rq = task_rq_lock(p, &flags); | |
a8e504d2 IM |
3918 | update_rq_clock(rq); |
3919 | now = rq->clock; | |
b29739f9 | 3920 | |
d5f9f942 | 3921 | oldprio = p->prio; |
dd41f596 IM |
3922 | on_rq = p->se.on_rq; |
3923 | if (on_rq) | |
69be72c1 | 3924 | dequeue_task(rq, p, 0); |
dd41f596 IM |
3925 | |
3926 | if (rt_prio(prio)) | |
3927 | p->sched_class = &rt_sched_class; | |
3928 | else | |
3929 | p->sched_class = &fair_sched_class; | |
3930 | ||
b29739f9 IM |
3931 | p->prio = prio; |
3932 | ||
dd41f596 | 3933 | if (on_rq) { |
8159f87e | 3934 | enqueue_task(rq, p, 0); |
b29739f9 IM |
3935 | /* |
3936 | * Reschedule if we are currently running on this runqueue and | |
d5f9f942 AM |
3937 | * our priority decreased, or if we are not currently running on |
3938 | * this runqueue and our priority is higher than the current's | |
b29739f9 | 3939 | */ |
d5f9f942 AM |
3940 | if (task_running(rq, p)) { |
3941 | if (p->prio > oldprio) | |
3942 | resched_task(rq->curr); | |
dd41f596 IM |
3943 | } else { |
3944 | check_preempt_curr(rq, p); | |
3945 | } | |
b29739f9 IM |
3946 | } |
3947 | task_rq_unlock(rq, &flags); | |
3948 | } | |
3949 | ||
3950 | #endif | |
3951 | ||
36c8b586 | 3952 | void set_user_nice(struct task_struct *p, long nice) |
1da177e4 | 3953 | { |
dd41f596 | 3954 | int old_prio, delta, on_rq; |
1da177e4 | 3955 | unsigned long flags; |
70b97a7f | 3956 | struct rq *rq; |
dd41f596 | 3957 | u64 now; |
1da177e4 LT |
3958 | |
3959 | if (TASK_NICE(p) == nice || nice < -20 || nice > 19) | |
3960 | return; | |
3961 | /* | |
3962 | * We have to be careful, if called from sys_setpriority(), | |
3963 | * the task might be in the middle of scheduling on another CPU. | |
3964 | */ | |
3965 | rq = task_rq_lock(p, &flags); | |
a8e504d2 IM |
3966 | update_rq_clock(rq); |
3967 | now = rq->clock; | |
1da177e4 LT |
3968 | /* |
3969 | * The RT priorities are set via sched_setscheduler(), but we still | |
3970 | * allow the 'normal' nice value to be set - but as expected | |
3971 | * it wont have any effect on scheduling until the task is | |
dd41f596 | 3972 | * SCHED_FIFO/SCHED_RR: |
1da177e4 | 3973 | */ |
e05606d3 | 3974 | if (task_has_rt_policy(p)) { |
1da177e4 LT |
3975 | p->static_prio = NICE_TO_PRIO(nice); |
3976 | goto out_unlock; | |
3977 | } | |
dd41f596 IM |
3978 | on_rq = p->se.on_rq; |
3979 | if (on_rq) { | |
69be72c1 | 3980 | dequeue_task(rq, p, 0); |
79b5dddf | 3981 | dec_load(rq, p); |
2dd73a4f | 3982 | } |
1da177e4 | 3983 | |
1da177e4 | 3984 | p->static_prio = NICE_TO_PRIO(nice); |
2dd73a4f | 3985 | set_load_weight(p); |
b29739f9 IM |
3986 | old_prio = p->prio; |
3987 | p->prio = effective_prio(p); | |
3988 | delta = p->prio - old_prio; | |
1da177e4 | 3989 | |
dd41f596 | 3990 | if (on_rq) { |
8159f87e | 3991 | enqueue_task(rq, p, 0); |
29b4b623 | 3992 | inc_load(rq, p); |
1da177e4 | 3993 | /* |
d5f9f942 AM |
3994 | * If the task increased its priority or is running and |
3995 | * lowered its priority, then reschedule its CPU: | |
1da177e4 | 3996 | */ |
d5f9f942 | 3997 | if (delta < 0 || (delta > 0 && task_running(rq, p))) |
1da177e4 LT |
3998 | resched_task(rq->curr); |
3999 | } | |
4000 | out_unlock: | |
4001 | task_rq_unlock(rq, &flags); | |
4002 | } | |
1da177e4 LT |
4003 | EXPORT_SYMBOL(set_user_nice); |
4004 | ||
e43379f1 MM |
4005 | /* |
4006 | * can_nice - check if a task can reduce its nice value | |
4007 | * @p: task | |
4008 | * @nice: nice value | |
4009 | */ | |
36c8b586 | 4010 | int can_nice(const struct task_struct *p, const int nice) |
e43379f1 | 4011 | { |
024f4747 MM |
4012 | /* convert nice value [19,-20] to rlimit style value [1,40] */ |
4013 | int nice_rlim = 20 - nice; | |
48f24c4d | 4014 | |
e43379f1 MM |
4015 | return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur || |
4016 | capable(CAP_SYS_NICE)); | |
4017 | } | |
4018 | ||
1da177e4 LT |
4019 | #ifdef __ARCH_WANT_SYS_NICE |
4020 | ||
4021 | /* | |
4022 | * sys_nice - change the priority of the current process. | |
4023 | * @increment: priority increment | |
4024 | * | |
4025 | * sys_setpriority is a more generic, but much slower function that | |
4026 | * does similar things. | |
4027 | */ | |
4028 | asmlinkage long sys_nice(int increment) | |
4029 | { | |
48f24c4d | 4030 | long nice, retval; |
1da177e4 LT |
4031 | |
4032 | /* | |
4033 | * Setpriority might change our priority at the same moment. | |
4034 | * We don't have to worry. Conceptually one call occurs first | |
4035 | * and we have a single winner. | |
4036 | */ | |
e43379f1 MM |
4037 | if (increment < -40) |
4038 | increment = -40; | |
1da177e4 LT |
4039 | if (increment > 40) |
4040 | increment = 40; | |
4041 | ||
4042 | nice = PRIO_TO_NICE(current->static_prio) + increment; | |
4043 | if (nice < -20) | |
4044 | nice = -20; | |
4045 | if (nice > 19) | |
4046 | nice = 19; | |
4047 | ||
e43379f1 MM |
4048 | if (increment < 0 && !can_nice(current, nice)) |
4049 | return -EPERM; | |
4050 | ||
1da177e4 LT |
4051 | retval = security_task_setnice(current, nice); |
4052 | if (retval) | |
4053 | return retval; | |
4054 | ||
4055 | set_user_nice(current, nice); | |
4056 | return 0; | |
4057 | } | |
4058 | ||
4059 | #endif | |
4060 | ||
4061 | /** | |
4062 | * task_prio - return the priority value of a given task. | |
4063 | * @p: the task in question. | |
4064 | * | |
4065 | * This is the priority value as seen by users in /proc. | |
4066 | * RT tasks are offset by -200. Normal tasks are centered | |
4067 | * around 0, value goes from -16 to +15. | |
4068 | */ | |
36c8b586 | 4069 | int task_prio(const struct task_struct *p) |
1da177e4 LT |
4070 | { |
4071 | return p->prio - MAX_RT_PRIO; | |
4072 | } | |
4073 | ||
4074 | /** | |
4075 | * task_nice - return the nice value of a given task. | |
4076 | * @p: the task in question. | |
4077 | */ | |
36c8b586 | 4078 | int task_nice(const struct task_struct *p) |
1da177e4 LT |
4079 | { |
4080 | return TASK_NICE(p); | |
4081 | } | |
1da177e4 | 4082 | EXPORT_SYMBOL_GPL(task_nice); |
1da177e4 LT |
4083 | |
4084 | /** | |
4085 | * idle_cpu - is a given cpu idle currently? | |
4086 | * @cpu: the processor in question. | |
4087 | */ | |
4088 | int idle_cpu(int cpu) | |
4089 | { | |
4090 | return cpu_curr(cpu) == cpu_rq(cpu)->idle; | |
4091 | } | |
4092 | ||
1da177e4 LT |
4093 | /** |
4094 | * idle_task - return the idle task for a given cpu. | |
4095 | * @cpu: the processor in question. | |
4096 | */ | |
36c8b586 | 4097 | struct task_struct *idle_task(int cpu) |
1da177e4 LT |
4098 | { |
4099 | return cpu_rq(cpu)->idle; | |
4100 | } | |
4101 | ||
4102 | /** | |
4103 | * find_process_by_pid - find a process with a matching PID value. | |
4104 | * @pid: the pid in question. | |
4105 | */ | |
36c8b586 | 4106 | static inline struct task_struct *find_process_by_pid(pid_t pid) |
1da177e4 LT |
4107 | { |
4108 | return pid ? find_task_by_pid(pid) : current; | |
4109 | } | |
4110 | ||
4111 | /* Actually do priority change: must hold rq lock. */ | |
dd41f596 IM |
4112 | static void |
4113 | __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio) | |
1da177e4 | 4114 | { |
dd41f596 | 4115 | BUG_ON(p->se.on_rq); |
48f24c4d | 4116 | |
1da177e4 | 4117 | p->policy = policy; |
dd41f596 IM |
4118 | switch (p->policy) { |
4119 | case SCHED_NORMAL: | |
4120 | case SCHED_BATCH: | |
4121 | case SCHED_IDLE: | |
4122 | p->sched_class = &fair_sched_class; | |
4123 | break; | |
4124 | case SCHED_FIFO: | |
4125 | case SCHED_RR: | |
4126 | p->sched_class = &rt_sched_class; | |
4127 | break; | |
4128 | } | |
4129 | ||
1da177e4 | 4130 | p->rt_priority = prio; |
b29739f9 IM |
4131 | p->normal_prio = normal_prio(p); |
4132 | /* we are holding p->pi_lock already */ | |
4133 | p->prio = rt_mutex_getprio(p); | |
2dd73a4f | 4134 | set_load_weight(p); |
1da177e4 LT |
4135 | } |
4136 | ||
4137 | /** | |
72fd4a35 | 4138 | * sched_setscheduler - change the scheduling policy and/or RT priority of a thread. |
1da177e4 LT |
4139 | * @p: the task in question. |
4140 | * @policy: new policy. | |
4141 | * @param: structure containing the new RT priority. | |
5fe1d75f | 4142 | * |
72fd4a35 | 4143 | * NOTE that the task may be already dead. |
1da177e4 | 4144 | */ |
95cdf3b7 IM |
4145 | int sched_setscheduler(struct task_struct *p, int policy, |
4146 | struct sched_param *param) | |
1da177e4 | 4147 | { |
dd41f596 | 4148 | int retval, oldprio, oldpolicy = -1, on_rq; |
1da177e4 | 4149 | unsigned long flags; |
70b97a7f | 4150 | struct rq *rq; |
1da177e4 | 4151 | |
66e5393a SR |
4152 | /* may grab non-irq protected spin_locks */ |
4153 | BUG_ON(in_interrupt()); | |
1da177e4 LT |
4154 | recheck: |
4155 | /* double check policy once rq lock held */ | |
4156 | if (policy < 0) | |
4157 | policy = oldpolicy = p->policy; | |
4158 | else if (policy != SCHED_FIFO && policy != SCHED_RR && | |
dd41f596 IM |
4159 | policy != SCHED_NORMAL && policy != SCHED_BATCH && |
4160 | policy != SCHED_IDLE) | |
b0a9499c | 4161 | return -EINVAL; |
1da177e4 LT |
4162 | /* |
4163 | * Valid priorities for SCHED_FIFO and SCHED_RR are | |
dd41f596 IM |
4164 | * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL, |
4165 | * SCHED_BATCH and SCHED_IDLE is 0. | |
1da177e4 LT |
4166 | */ |
4167 | if (param->sched_priority < 0 || | |
95cdf3b7 | 4168 | (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) || |
d46523ea | 4169 | (!p->mm && param->sched_priority > MAX_RT_PRIO-1)) |
1da177e4 | 4170 | return -EINVAL; |
e05606d3 | 4171 | if (rt_policy(policy) != (param->sched_priority != 0)) |
1da177e4 LT |
4172 | return -EINVAL; |
4173 | ||
37e4ab3f OC |
4174 | /* |
4175 | * Allow unprivileged RT tasks to decrease priority: | |
4176 | */ | |
4177 | if (!capable(CAP_SYS_NICE)) { | |
e05606d3 | 4178 | if (rt_policy(policy)) { |
8dc3e909 | 4179 | unsigned long rlim_rtprio; |
8dc3e909 ON |
4180 | |
4181 | if (!lock_task_sighand(p, &flags)) | |
4182 | return -ESRCH; | |
4183 | rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur; | |
4184 | unlock_task_sighand(p, &flags); | |
4185 | ||
4186 | /* can't set/change the rt policy */ | |
4187 | if (policy != p->policy && !rlim_rtprio) | |
4188 | return -EPERM; | |
4189 | ||
4190 | /* can't increase priority */ | |
4191 | if (param->sched_priority > p->rt_priority && | |
4192 | param->sched_priority > rlim_rtprio) | |
4193 | return -EPERM; | |
4194 | } | |
dd41f596 IM |
4195 | /* |
4196 | * Like positive nice levels, dont allow tasks to | |
4197 | * move out of SCHED_IDLE either: | |
4198 | */ | |
4199 | if (p->policy == SCHED_IDLE && policy != SCHED_IDLE) | |
4200 | return -EPERM; | |
5fe1d75f | 4201 | |
37e4ab3f OC |
4202 | /* can't change other user's priorities */ |
4203 | if ((current->euid != p->euid) && | |
4204 | (current->euid != p->uid)) | |
4205 | return -EPERM; | |
4206 | } | |
1da177e4 LT |
4207 | |
4208 | retval = security_task_setscheduler(p, policy, param); | |
4209 | if (retval) | |
4210 | return retval; | |
b29739f9 IM |
4211 | /* |
4212 | * make sure no PI-waiters arrive (or leave) while we are | |
4213 | * changing the priority of the task: | |
4214 | */ | |
4215 | spin_lock_irqsave(&p->pi_lock, flags); | |
1da177e4 LT |
4216 | /* |
4217 | * To be able to change p->policy safely, the apropriate | |
4218 | * runqueue lock must be held. | |
4219 | */ | |
b29739f9 | 4220 | rq = __task_rq_lock(p); |
1da177e4 LT |
4221 | /* recheck policy now with rq lock held */ |
4222 | if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { | |
4223 | policy = oldpolicy = -1; | |
b29739f9 IM |
4224 | __task_rq_unlock(rq); |
4225 | spin_unlock_irqrestore(&p->pi_lock, flags); | |
1da177e4 LT |
4226 | goto recheck; |
4227 | } | |
dd41f596 | 4228 | on_rq = p->se.on_rq; |
a8e504d2 IM |
4229 | if (on_rq) { |
4230 | update_rq_clock(rq); | |
4231 | deactivate_task(rq, p, 0, rq->clock); | |
4232 | } | |
1da177e4 | 4233 | oldprio = p->prio; |
dd41f596 IM |
4234 | __setscheduler(rq, p, policy, param->sched_priority); |
4235 | if (on_rq) { | |
4236 | activate_task(rq, p, 0); | |
1da177e4 LT |
4237 | /* |
4238 | * Reschedule if we are currently running on this runqueue and | |
d5f9f942 AM |
4239 | * our priority decreased, or if we are not currently running on |
4240 | * this runqueue and our priority is higher than the current's | |
1da177e4 | 4241 | */ |
d5f9f942 AM |
4242 | if (task_running(rq, p)) { |
4243 | if (p->prio > oldprio) | |
4244 | resched_task(rq->curr); | |
dd41f596 IM |
4245 | } else { |
4246 | check_preempt_curr(rq, p); | |
4247 | } | |
1da177e4 | 4248 | } |
b29739f9 IM |
4249 | __task_rq_unlock(rq); |
4250 | spin_unlock_irqrestore(&p->pi_lock, flags); | |
4251 | ||
95e02ca9 TG |
4252 | rt_mutex_adjust_pi(p); |
4253 | ||
1da177e4 LT |
4254 | return 0; |
4255 | } | |
4256 | EXPORT_SYMBOL_GPL(sched_setscheduler); | |
4257 | ||
95cdf3b7 IM |
4258 | static int |
4259 | do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param) | |
1da177e4 | 4260 | { |
1da177e4 LT |
4261 | struct sched_param lparam; |
4262 | struct task_struct *p; | |
36c8b586 | 4263 | int retval; |
1da177e4 LT |
4264 | |
4265 | if (!param || pid < 0) | |
4266 | return -EINVAL; | |
4267 | if (copy_from_user(&lparam, param, sizeof(struct sched_param))) | |
4268 | return -EFAULT; | |
5fe1d75f ON |
4269 | |
4270 | rcu_read_lock(); | |
4271 | retval = -ESRCH; | |
1da177e4 | 4272 | p = find_process_by_pid(pid); |
5fe1d75f ON |
4273 | if (p != NULL) |
4274 | retval = sched_setscheduler(p, policy, &lparam); | |
4275 | rcu_read_unlock(); | |
36c8b586 | 4276 | |
1da177e4 LT |
4277 | return retval; |
4278 | } | |
4279 | ||
4280 | /** | |
4281 | * sys_sched_setscheduler - set/change the scheduler policy and RT priority | |
4282 | * @pid: the pid in question. | |
4283 | * @policy: new policy. | |
4284 | * @param: structure containing the new RT priority. | |
4285 | */ | |
4286 | asmlinkage long sys_sched_setscheduler(pid_t pid, int policy, | |
4287 | struct sched_param __user *param) | |
4288 | { | |
c21761f1 JB |
4289 | /* negative values for policy are not valid */ |
4290 | if (policy < 0) | |
4291 | return -EINVAL; | |
4292 | ||
1da177e4 LT |
4293 | return do_sched_setscheduler(pid, policy, param); |
4294 | } | |
4295 | ||
4296 | /** | |
4297 | * sys_sched_setparam - set/change the RT priority of a thread | |
4298 | * @pid: the pid in question. | |
4299 | * @param: structure containing the new RT priority. | |
4300 | */ | |
4301 | asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param) | |
4302 | { | |
4303 | return do_sched_setscheduler(pid, -1, param); | |
4304 | } | |
4305 | ||
4306 | /** | |
4307 | * sys_sched_getscheduler - get the policy (scheduling class) of a thread | |
4308 | * @pid: the pid in question. | |
4309 | */ | |
4310 | asmlinkage long sys_sched_getscheduler(pid_t pid) | |
4311 | { | |
36c8b586 | 4312 | struct task_struct *p; |
1da177e4 | 4313 | int retval = -EINVAL; |
1da177e4 LT |
4314 | |
4315 | if (pid < 0) | |
4316 | goto out_nounlock; | |
4317 | ||
4318 | retval = -ESRCH; | |
4319 | read_lock(&tasklist_lock); | |
4320 | p = find_process_by_pid(pid); | |
4321 | if (p) { | |
4322 | retval = security_task_getscheduler(p); | |
4323 | if (!retval) | |
4324 | retval = p->policy; | |
4325 | } | |
4326 | read_unlock(&tasklist_lock); | |
4327 | ||
4328 | out_nounlock: | |
4329 | return retval; | |
4330 | } | |
4331 | ||
4332 | /** | |
4333 | * sys_sched_getscheduler - get the RT priority of a thread | |
4334 | * @pid: the pid in question. | |
4335 | * @param: structure containing the RT priority. | |
4336 | */ | |
4337 | asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param) | |
4338 | { | |
4339 | struct sched_param lp; | |
36c8b586 | 4340 | struct task_struct *p; |
1da177e4 | 4341 | int retval = -EINVAL; |
1da177e4 LT |
4342 | |
4343 | if (!param || pid < 0) | |
4344 | goto out_nounlock; | |
4345 | ||
4346 | read_lock(&tasklist_lock); | |
4347 | p = find_process_by_pid(pid); | |
4348 | retval = -ESRCH; | |
4349 | if (!p) | |
4350 | goto out_unlock; | |
4351 | ||
4352 | retval = security_task_getscheduler(p); | |
4353 | if (retval) | |
4354 | goto out_unlock; | |
4355 | ||
4356 | lp.sched_priority = p->rt_priority; | |
4357 | read_unlock(&tasklist_lock); | |
4358 | ||
4359 | /* | |
4360 | * This one might sleep, we cannot do it with a spinlock held ... | |
4361 | */ | |
4362 | retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0; | |
4363 | ||
4364 | out_nounlock: | |
4365 | return retval; | |
4366 | ||
4367 | out_unlock: | |
4368 | read_unlock(&tasklist_lock); | |
4369 | return retval; | |
4370 | } | |
4371 | ||
4372 | long sched_setaffinity(pid_t pid, cpumask_t new_mask) | |
4373 | { | |
1da177e4 | 4374 | cpumask_t cpus_allowed; |
36c8b586 IM |
4375 | struct task_struct *p; |
4376 | int retval; | |
1da177e4 | 4377 | |
5be9361c | 4378 | mutex_lock(&sched_hotcpu_mutex); |
1da177e4 LT |
4379 | read_lock(&tasklist_lock); |
4380 | ||
4381 | p = find_process_by_pid(pid); | |
4382 | if (!p) { | |
4383 | read_unlock(&tasklist_lock); | |
5be9361c | 4384 | mutex_unlock(&sched_hotcpu_mutex); |
1da177e4 LT |
4385 | return -ESRCH; |
4386 | } | |
4387 | ||
4388 | /* | |
4389 | * It is not safe to call set_cpus_allowed with the | |
4390 | * tasklist_lock held. We will bump the task_struct's | |
4391 | * usage count and then drop tasklist_lock. | |
4392 | */ | |
4393 | get_task_struct(p); | |
4394 | read_unlock(&tasklist_lock); | |
4395 | ||
4396 | retval = -EPERM; | |
4397 | if ((current->euid != p->euid) && (current->euid != p->uid) && | |
4398 | !capable(CAP_SYS_NICE)) | |
4399 | goto out_unlock; | |
4400 | ||
e7834f8f DQ |
4401 | retval = security_task_setscheduler(p, 0, NULL); |
4402 | if (retval) | |
4403 | goto out_unlock; | |
4404 | ||
1da177e4 LT |
4405 | cpus_allowed = cpuset_cpus_allowed(p); |
4406 | cpus_and(new_mask, new_mask, cpus_allowed); | |
4407 | retval = set_cpus_allowed(p, new_mask); | |
4408 | ||
4409 | out_unlock: | |
4410 | put_task_struct(p); | |
5be9361c | 4411 | mutex_unlock(&sched_hotcpu_mutex); |
1da177e4 LT |
4412 | return retval; |
4413 | } | |
4414 | ||
4415 | static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len, | |
4416 | cpumask_t *new_mask) | |
4417 | { | |
4418 | if (len < sizeof(cpumask_t)) { | |
4419 | memset(new_mask, 0, sizeof(cpumask_t)); | |
4420 | } else if (len > sizeof(cpumask_t)) { | |
4421 | len = sizeof(cpumask_t); | |
4422 | } | |
4423 | return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0; | |
4424 | } | |
4425 | ||
4426 | /** | |
4427 | * sys_sched_setaffinity - set the cpu affinity of a process | |
4428 | * @pid: pid of the process | |
4429 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr | |
4430 | * @user_mask_ptr: user-space pointer to the new cpu mask | |
4431 | */ | |
4432 | asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len, | |
4433 | unsigned long __user *user_mask_ptr) | |
4434 | { | |
4435 | cpumask_t new_mask; | |
4436 | int retval; | |
4437 | ||
4438 | retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask); | |
4439 | if (retval) | |
4440 | return retval; | |
4441 | ||
4442 | return sched_setaffinity(pid, new_mask); | |
4443 | } | |
4444 | ||
4445 | /* | |
4446 | * Represents all cpu's present in the system | |
4447 | * In systems capable of hotplug, this map could dynamically grow | |
4448 | * as new cpu's are detected in the system via any platform specific | |
4449 | * method, such as ACPI for e.g. | |
4450 | */ | |
4451 | ||
4cef0c61 | 4452 | cpumask_t cpu_present_map __read_mostly; |
1da177e4 LT |
4453 | EXPORT_SYMBOL(cpu_present_map); |
4454 | ||
4455 | #ifndef CONFIG_SMP | |
4cef0c61 | 4456 | cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL; |
e16b38f7 GB |
4457 | EXPORT_SYMBOL(cpu_online_map); |
4458 | ||
4cef0c61 | 4459 | cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL; |
e16b38f7 | 4460 | EXPORT_SYMBOL(cpu_possible_map); |
1da177e4 LT |
4461 | #endif |
4462 | ||
4463 | long sched_getaffinity(pid_t pid, cpumask_t *mask) | |
4464 | { | |
36c8b586 | 4465 | struct task_struct *p; |
1da177e4 | 4466 | int retval; |
1da177e4 | 4467 | |
5be9361c | 4468 | mutex_lock(&sched_hotcpu_mutex); |
1da177e4 LT |
4469 | read_lock(&tasklist_lock); |
4470 | ||
4471 | retval = -ESRCH; | |
4472 | p = find_process_by_pid(pid); | |
4473 | if (!p) | |
4474 | goto out_unlock; | |
4475 | ||
e7834f8f DQ |
4476 | retval = security_task_getscheduler(p); |
4477 | if (retval) | |
4478 | goto out_unlock; | |
4479 | ||
2f7016d9 | 4480 | cpus_and(*mask, p->cpus_allowed, cpu_online_map); |
1da177e4 LT |
4481 | |
4482 | out_unlock: | |
4483 | read_unlock(&tasklist_lock); | |
5be9361c | 4484 | mutex_unlock(&sched_hotcpu_mutex); |
1da177e4 | 4485 | |
9531b62f | 4486 | return retval; |
1da177e4 LT |
4487 | } |
4488 | ||
4489 | /** | |
4490 | * sys_sched_getaffinity - get the cpu affinity of a process | |
4491 | * @pid: pid of the process | |
4492 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr | |
4493 | * @user_mask_ptr: user-space pointer to hold the current cpu mask | |
4494 | */ | |
4495 | asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len, | |
4496 | unsigned long __user *user_mask_ptr) | |
4497 | { | |
4498 | int ret; | |
4499 | cpumask_t mask; | |
4500 | ||
4501 | if (len < sizeof(cpumask_t)) | |
4502 | return -EINVAL; | |
4503 | ||
4504 | ret = sched_getaffinity(pid, &mask); | |
4505 | if (ret < 0) | |
4506 | return ret; | |
4507 | ||
4508 | if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t))) | |
4509 | return -EFAULT; | |
4510 | ||
4511 | return sizeof(cpumask_t); | |
4512 | } | |
4513 | ||
4514 | /** | |
4515 | * sys_sched_yield - yield the current processor to other threads. | |
4516 | * | |
dd41f596 IM |
4517 | * This function yields the current CPU to other tasks. If there are no |
4518 | * other threads running on this CPU then this function will return. | |
1da177e4 LT |
4519 | */ |
4520 | asmlinkage long sys_sched_yield(void) | |
4521 | { | |
70b97a7f | 4522 | struct rq *rq = this_rq_lock(); |
1da177e4 LT |
4523 | |
4524 | schedstat_inc(rq, yld_cnt); | |
dd41f596 | 4525 | if (unlikely(rq->nr_running == 1)) |
1da177e4 | 4526 | schedstat_inc(rq, yld_act_empty); |
dd41f596 IM |
4527 | else |
4528 | current->sched_class->yield_task(rq, current); | |
1da177e4 LT |
4529 | |
4530 | /* | |
4531 | * Since we are going to call schedule() anyway, there's | |
4532 | * no need to preempt or enable interrupts: | |
4533 | */ | |
4534 | __release(rq->lock); | |
8a25d5de | 4535 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); |
1da177e4 LT |
4536 | _raw_spin_unlock(&rq->lock); |
4537 | preempt_enable_no_resched(); | |
4538 | ||
4539 | schedule(); | |
4540 | ||
4541 | return 0; | |
4542 | } | |
4543 | ||
e7b38404 | 4544 | static void __cond_resched(void) |
1da177e4 | 4545 | { |
8e0a43d8 IM |
4546 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP |
4547 | __might_sleep(__FILE__, __LINE__); | |
4548 | #endif | |
5bbcfd90 IM |
4549 | /* |
4550 | * The BKS might be reacquired before we have dropped | |
4551 | * PREEMPT_ACTIVE, which could trigger a second | |
4552 | * cond_resched() call. | |
4553 | */ | |
1da177e4 LT |
4554 | do { |
4555 | add_preempt_count(PREEMPT_ACTIVE); | |
4556 | schedule(); | |
4557 | sub_preempt_count(PREEMPT_ACTIVE); | |
4558 | } while (need_resched()); | |
4559 | } | |
4560 | ||
4561 | int __sched cond_resched(void) | |
4562 | { | |
9414232f IM |
4563 | if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) && |
4564 | system_state == SYSTEM_RUNNING) { | |
1da177e4 LT |
4565 | __cond_resched(); |
4566 | return 1; | |
4567 | } | |
4568 | return 0; | |
4569 | } | |
1da177e4 LT |
4570 | EXPORT_SYMBOL(cond_resched); |
4571 | ||
4572 | /* | |
4573 | * cond_resched_lock() - if a reschedule is pending, drop the given lock, | |
4574 | * call schedule, and on return reacquire the lock. | |
4575 | * | |
4576 | * This works OK both with and without CONFIG_PREEMPT. We do strange low-level | |
4577 | * operations here to prevent schedule() from being called twice (once via | |
4578 | * spin_unlock(), once by hand). | |
4579 | */ | |
95cdf3b7 | 4580 | int cond_resched_lock(spinlock_t *lock) |
1da177e4 | 4581 | { |
6df3cecb JK |
4582 | int ret = 0; |
4583 | ||
1da177e4 LT |
4584 | if (need_lockbreak(lock)) { |
4585 | spin_unlock(lock); | |
4586 | cpu_relax(); | |
6df3cecb | 4587 | ret = 1; |
1da177e4 LT |
4588 | spin_lock(lock); |
4589 | } | |
9414232f | 4590 | if (need_resched() && system_state == SYSTEM_RUNNING) { |
8a25d5de | 4591 | spin_release(&lock->dep_map, 1, _THIS_IP_); |
1da177e4 LT |
4592 | _raw_spin_unlock(lock); |
4593 | preempt_enable_no_resched(); | |
4594 | __cond_resched(); | |
6df3cecb | 4595 | ret = 1; |
1da177e4 | 4596 | spin_lock(lock); |
1da177e4 | 4597 | } |
6df3cecb | 4598 | return ret; |
1da177e4 | 4599 | } |
1da177e4 LT |
4600 | EXPORT_SYMBOL(cond_resched_lock); |
4601 | ||
4602 | int __sched cond_resched_softirq(void) | |
4603 | { | |
4604 | BUG_ON(!in_softirq()); | |
4605 | ||
9414232f | 4606 | if (need_resched() && system_state == SYSTEM_RUNNING) { |
98d82567 | 4607 | local_bh_enable(); |
1da177e4 LT |
4608 | __cond_resched(); |
4609 | local_bh_disable(); | |
4610 | return 1; | |
4611 | } | |
4612 | return 0; | |
4613 | } | |
1da177e4 LT |
4614 | EXPORT_SYMBOL(cond_resched_softirq); |
4615 | ||
1da177e4 LT |
4616 | /** |
4617 | * yield - yield the current processor to other threads. | |
4618 | * | |
72fd4a35 | 4619 | * This is a shortcut for kernel-space yielding - it marks the |
1da177e4 LT |
4620 | * thread runnable and calls sys_sched_yield(). |
4621 | */ | |
4622 | void __sched yield(void) | |
4623 | { | |
4624 | set_current_state(TASK_RUNNING); | |
4625 | sys_sched_yield(); | |
4626 | } | |
1da177e4 LT |
4627 | EXPORT_SYMBOL(yield); |
4628 | ||
4629 | /* | |
4630 | * This task is about to go to sleep on IO. Increment rq->nr_iowait so | |
4631 | * that process accounting knows that this is a task in IO wait state. | |
4632 | * | |
4633 | * But don't do that if it is a deliberate, throttling IO wait (this task | |
4634 | * has set its backing_dev_info: the queue against which it should throttle) | |
4635 | */ | |
4636 | void __sched io_schedule(void) | |
4637 | { | |
70b97a7f | 4638 | struct rq *rq = &__raw_get_cpu_var(runqueues); |
1da177e4 | 4639 | |
0ff92245 | 4640 | delayacct_blkio_start(); |
1da177e4 LT |
4641 | atomic_inc(&rq->nr_iowait); |
4642 | schedule(); | |
4643 | atomic_dec(&rq->nr_iowait); | |
0ff92245 | 4644 | delayacct_blkio_end(); |
1da177e4 | 4645 | } |
1da177e4 LT |
4646 | EXPORT_SYMBOL(io_schedule); |
4647 | ||
4648 | long __sched io_schedule_timeout(long timeout) | |
4649 | { | |
70b97a7f | 4650 | struct rq *rq = &__raw_get_cpu_var(runqueues); |
1da177e4 LT |
4651 | long ret; |
4652 | ||
0ff92245 | 4653 | delayacct_blkio_start(); |
1da177e4 LT |
4654 | atomic_inc(&rq->nr_iowait); |
4655 | ret = schedule_timeout(timeout); | |
4656 | atomic_dec(&rq->nr_iowait); | |
0ff92245 | 4657 | delayacct_blkio_end(); |
1da177e4 LT |
4658 | return ret; |
4659 | } | |
4660 | ||
4661 | /** | |
4662 | * sys_sched_get_priority_max - return maximum RT priority. | |
4663 | * @policy: scheduling class. | |
4664 | * | |
4665 | * this syscall returns the maximum rt_priority that can be used | |
4666 | * by a given scheduling class. | |
4667 | */ | |
4668 | asmlinkage long sys_sched_get_priority_max(int policy) | |
4669 | { | |
4670 | int ret = -EINVAL; | |
4671 | ||
4672 | switch (policy) { | |
4673 | case SCHED_FIFO: | |
4674 | case SCHED_RR: | |
4675 | ret = MAX_USER_RT_PRIO-1; | |
4676 | break; | |
4677 | case SCHED_NORMAL: | |
b0a9499c | 4678 | case SCHED_BATCH: |
dd41f596 | 4679 | case SCHED_IDLE: |
1da177e4 LT |
4680 | ret = 0; |
4681 | break; | |
4682 | } | |
4683 | return ret; | |
4684 | } | |
4685 | ||
4686 | /** | |
4687 | * sys_sched_get_priority_min - return minimum RT priority. | |
4688 | * @policy: scheduling class. | |
4689 | * | |
4690 | * this syscall returns the minimum rt_priority that can be used | |
4691 | * by a given scheduling class. | |
4692 | */ | |
4693 | asmlinkage long sys_sched_get_priority_min(int policy) | |
4694 | { | |
4695 | int ret = -EINVAL; | |
4696 | ||
4697 | switch (policy) { | |
4698 | case SCHED_FIFO: | |
4699 | case SCHED_RR: | |
4700 | ret = 1; | |
4701 | break; | |
4702 | case SCHED_NORMAL: | |
b0a9499c | 4703 | case SCHED_BATCH: |
dd41f596 | 4704 | case SCHED_IDLE: |
1da177e4 LT |
4705 | ret = 0; |
4706 | } | |
4707 | return ret; | |
4708 | } | |
4709 | ||
4710 | /** | |
4711 | * sys_sched_rr_get_interval - return the default timeslice of a process. | |
4712 | * @pid: pid of the process. | |
4713 | * @interval: userspace pointer to the timeslice value. | |
4714 | * | |
4715 | * this syscall writes the default timeslice value of a given process | |
4716 | * into the user-space timespec buffer. A value of '0' means infinity. | |
4717 | */ | |
4718 | asmlinkage | |
4719 | long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval) | |
4720 | { | |
36c8b586 | 4721 | struct task_struct *p; |
1da177e4 LT |
4722 | int retval = -EINVAL; |
4723 | struct timespec t; | |
1da177e4 LT |
4724 | |
4725 | if (pid < 0) | |
4726 | goto out_nounlock; | |
4727 | ||
4728 | retval = -ESRCH; | |
4729 | read_lock(&tasklist_lock); | |
4730 | p = find_process_by_pid(pid); | |
4731 | if (!p) | |
4732 | goto out_unlock; | |
4733 | ||
4734 | retval = security_task_getscheduler(p); | |
4735 | if (retval) | |
4736 | goto out_unlock; | |
4737 | ||
b78709cf | 4738 | jiffies_to_timespec(p->policy == SCHED_FIFO ? |
dd41f596 | 4739 | 0 : static_prio_timeslice(p->static_prio), &t); |
1da177e4 LT |
4740 | read_unlock(&tasklist_lock); |
4741 | retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0; | |
4742 | out_nounlock: | |
4743 | return retval; | |
4744 | out_unlock: | |
4745 | read_unlock(&tasklist_lock); | |
4746 | return retval; | |
4747 | } | |
4748 | ||
2ed6e34f | 4749 | static const char stat_nam[] = "RSDTtZX"; |
36c8b586 IM |
4750 | |
4751 | static void show_task(struct task_struct *p) | |
1da177e4 | 4752 | { |
1da177e4 | 4753 | unsigned long free = 0; |
36c8b586 | 4754 | unsigned state; |
1da177e4 | 4755 | |
1da177e4 | 4756 | state = p->state ? __ffs(p->state) + 1 : 0; |
2ed6e34f AM |
4757 | printk("%-13.13s %c", p->comm, |
4758 | state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?'); | |
4bd77321 | 4759 | #if BITS_PER_LONG == 32 |
1da177e4 | 4760 | if (state == TASK_RUNNING) |
4bd77321 | 4761 | printk(" running "); |
1da177e4 | 4762 | else |
4bd77321 | 4763 | printk(" %08lx ", thread_saved_pc(p)); |
1da177e4 LT |
4764 | #else |
4765 | if (state == TASK_RUNNING) | |
4bd77321 | 4766 | printk(" running task "); |
1da177e4 LT |
4767 | else |
4768 | printk(" %016lx ", thread_saved_pc(p)); | |
4769 | #endif | |
4770 | #ifdef CONFIG_DEBUG_STACK_USAGE | |
4771 | { | |
10ebffde | 4772 | unsigned long *n = end_of_stack(p); |
1da177e4 LT |
4773 | while (!*n) |
4774 | n++; | |
10ebffde | 4775 | free = (unsigned long)n - (unsigned long)end_of_stack(p); |
1da177e4 LT |
4776 | } |
4777 | #endif | |
4bd77321 | 4778 | printk("%5lu %5d %6d\n", free, p->pid, p->parent->pid); |
1da177e4 LT |
4779 | |
4780 | if (state != TASK_RUNNING) | |
4781 | show_stack(p, NULL); | |
4782 | } | |
4783 | ||
e59e2ae2 | 4784 | void show_state_filter(unsigned long state_filter) |
1da177e4 | 4785 | { |
36c8b586 | 4786 | struct task_struct *g, *p; |
1da177e4 | 4787 | |
4bd77321 IM |
4788 | #if BITS_PER_LONG == 32 |
4789 | printk(KERN_INFO | |
4790 | " task PC stack pid father\n"); | |
1da177e4 | 4791 | #else |
4bd77321 IM |
4792 | printk(KERN_INFO |
4793 | " task PC stack pid father\n"); | |
1da177e4 LT |
4794 | #endif |
4795 | read_lock(&tasklist_lock); | |
4796 | do_each_thread(g, p) { | |
4797 | /* | |
4798 | * reset the NMI-timeout, listing all files on a slow | |
4799 | * console might take alot of time: | |
4800 | */ | |
4801 | touch_nmi_watchdog(); | |
39bc89fd | 4802 | if (!state_filter || (p->state & state_filter)) |
e59e2ae2 | 4803 | show_task(p); |
1da177e4 LT |
4804 | } while_each_thread(g, p); |
4805 | ||
04c9167f JF |
4806 | touch_all_softlockup_watchdogs(); |
4807 | ||
dd41f596 IM |
4808 | #ifdef CONFIG_SCHED_DEBUG |
4809 | sysrq_sched_debug_show(); | |
4810 | #endif | |
1da177e4 | 4811 | read_unlock(&tasklist_lock); |
e59e2ae2 IM |
4812 | /* |
4813 | * Only show locks if all tasks are dumped: | |
4814 | */ | |
4815 | if (state_filter == -1) | |
4816 | debug_show_all_locks(); | |
1da177e4 LT |
4817 | } |
4818 | ||
1df21055 IM |
4819 | void __cpuinit init_idle_bootup_task(struct task_struct *idle) |
4820 | { | |
dd41f596 | 4821 | idle->sched_class = &idle_sched_class; |
1df21055 IM |
4822 | } |
4823 | ||
f340c0d1 IM |
4824 | /** |
4825 | * init_idle - set up an idle thread for a given CPU | |
4826 | * @idle: task in question | |
4827 | * @cpu: cpu the idle task belongs to | |
4828 | * | |
4829 | * NOTE: this function does not set the idle thread's NEED_RESCHED | |
4830 | * flag, to make booting more robust. | |
4831 | */ | |
5c1e1767 | 4832 | void __cpuinit init_idle(struct task_struct *idle, int cpu) |
1da177e4 | 4833 | { |
70b97a7f | 4834 | struct rq *rq = cpu_rq(cpu); |
1da177e4 LT |
4835 | unsigned long flags; |
4836 | ||
dd41f596 IM |
4837 | __sched_fork(idle); |
4838 | idle->se.exec_start = sched_clock(); | |
4839 | ||
b29739f9 | 4840 | idle->prio = idle->normal_prio = MAX_PRIO; |
1da177e4 | 4841 | idle->cpus_allowed = cpumask_of_cpu(cpu); |
dd41f596 | 4842 | __set_task_cpu(idle, cpu); |
1da177e4 LT |
4843 | |
4844 | spin_lock_irqsave(&rq->lock, flags); | |
4845 | rq->curr = rq->idle = idle; | |
4866cde0 NP |
4846 | #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW) |
4847 | idle->oncpu = 1; | |
4848 | #endif | |
1da177e4 LT |
4849 | spin_unlock_irqrestore(&rq->lock, flags); |
4850 | ||
4851 | /* Set the preempt count _outside_ the spinlocks! */ | |
4852 | #if defined(CONFIG_PREEMPT) && !defined(CONFIG_PREEMPT_BKL) | |
a1261f54 | 4853 | task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0); |
1da177e4 | 4854 | #else |
a1261f54 | 4855 | task_thread_info(idle)->preempt_count = 0; |
1da177e4 | 4856 | #endif |
dd41f596 IM |
4857 | /* |
4858 | * The idle tasks have their own, simple scheduling class: | |
4859 | */ | |
4860 | idle->sched_class = &idle_sched_class; | |
1da177e4 LT |
4861 | } |
4862 | ||
4863 | /* | |
4864 | * In a system that switches off the HZ timer nohz_cpu_mask | |
4865 | * indicates which cpus entered this state. This is used | |
4866 | * in the rcu update to wait only for active cpus. For system | |
4867 | * which do not switch off the HZ timer nohz_cpu_mask should | |
4868 | * always be CPU_MASK_NONE. | |
4869 | */ | |
4870 | cpumask_t nohz_cpu_mask = CPU_MASK_NONE; | |
4871 | ||
dd41f596 IM |
4872 | /* |
4873 | * Increase the granularity value when there are more CPUs, | |
4874 | * because with more CPUs the 'effective latency' as visible | |
4875 | * to users decreases. But the relationship is not linear, | |
4876 | * so pick a second-best guess by going with the log2 of the | |
4877 | * number of CPUs. | |
4878 | * | |
4879 | * This idea comes from the SD scheduler of Con Kolivas: | |
4880 | */ | |
4881 | static inline void sched_init_granularity(void) | |
4882 | { | |
4883 | unsigned int factor = 1 + ilog2(num_online_cpus()); | |
a5968df8 | 4884 | const unsigned long gran_limit = 100000000; |
dd41f596 IM |
4885 | |
4886 | sysctl_sched_granularity *= factor; | |
4887 | if (sysctl_sched_granularity > gran_limit) | |
4888 | sysctl_sched_granularity = gran_limit; | |
4889 | ||
4890 | sysctl_sched_runtime_limit = sysctl_sched_granularity * 4; | |
4891 | sysctl_sched_wakeup_granularity = sysctl_sched_granularity / 2; | |
4892 | } | |
4893 | ||
1da177e4 LT |
4894 | #ifdef CONFIG_SMP |
4895 | /* | |
4896 | * This is how migration works: | |
4897 | * | |
70b97a7f | 4898 | * 1) we queue a struct migration_req structure in the source CPU's |
1da177e4 LT |
4899 | * runqueue and wake up that CPU's migration thread. |
4900 | * 2) we down() the locked semaphore => thread blocks. | |
4901 | * 3) migration thread wakes up (implicitly it forces the migrated | |
4902 | * thread off the CPU) | |
4903 | * 4) it gets the migration request and checks whether the migrated | |
4904 | * task is still in the wrong runqueue. | |
4905 | * 5) if it's in the wrong runqueue then the migration thread removes | |
4906 | * it and puts it into the right queue. | |
4907 | * 6) migration thread up()s the semaphore. | |
4908 | * 7) we wake up and the migration is done. | |
4909 | */ | |
4910 | ||
4911 | /* | |
4912 | * Change a given task's CPU affinity. Migrate the thread to a | |
4913 | * proper CPU and schedule it away if the CPU it's executing on | |
4914 | * is removed from the allowed bitmask. | |
4915 | * | |
4916 | * NOTE: the caller must have a valid reference to the task, the | |
4917 | * task must not exit() & deallocate itself prematurely. The | |
4918 | * call is not atomic; no spinlocks may be held. | |
4919 | */ | |
36c8b586 | 4920 | int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask) |
1da177e4 | 4921 | { |
70b97a7f | 4922 | struct migration_req req; |
1da177e4 | 4923 | unsigned long flags; |
70b97a7f | 4924 | struct rq *rq; |
48f24c4d | 4925 | int ret = 0; |
1da177e4 LT |
4926 | |
4927 | rq = task_rq_lock(p, &flags); | |
4928 | if (!cpus_intersects(new_mask, cpu_online_map)) { | |
4929 | ret = -EINVAL; | |
4930 | goto out; | |
4931 | } | |
4932 | ||
4933 | p->cpus_allowed = new_mask; | |
4934 | /* Can the task run on the task's current CPU? If so, we're done */ | |
4935 | if (cpu_isset(task_cpu(p), new_mask)) | |
4936 | goto out; | |
4937 | ||
4938 | if (migrate_task(p, any_online_cpu(new_mask), &req)) { | |
4939 | /* Need help from migration thread: drop lock and wait. */ | |
4940 | task_rq_unlock(rq, &flags); | |
4941 | wake_up_process(rq->migration_thread); | |
4942 | wait_for_completion(&req.done); | |
4943 | tlb_migrate_finish(p->mm); | |
4944 | return 0; | |
4945 | } | |
4946 | out: | |
4947 | task_rq_unlock(rq, &flags); | |
48f24c4d | 4948 | |
1da177e4 LT |
4949 | return ret; |
4950 | } | |
1da177e4 LT |
4951 | EXPORT_SYMBOL_GPL(set_cpus_allowed); |
4952 | ||
4953 | /* | |
4954 | * Move (not current) task off this cpu, onto dest cpu. We're doing | |
4955 | * this because either it can't run here any more (set_cpus_allowed() | |
4956 | * away from this CPU, or CPU going down), or because we're | |
4957 | * attempting to rebalance this task on exec (sched_exec). | |
4958 | * | |
4959 | * So we race with normal scheduler movements, but that's OK, as long | |
4960 | * as the task is no longer on this CPU. | |
efc30814 KK |
4961 | * |
4962 | * Returns non-zero if task was successfully migrated. | |
1da177e4 | 4963 | */ |
efc30814 | 4964 | static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu) |
1da177e4 | 4965 | { |
70b97a7f | 4966 | struct rq *rq_dest, *rq_src; |
dd41f596 | 4967 | int ret = 0, on_rq; |
1da177e4 LT |
4968 | |
4969 | if (unlikely(cpu_is_offline(dest_cpu))) | |
efc30814 | 4970 | return ret; |
1da177e4 LT |
4971 | |
4972 | rq_src = cpu_rq(src_cpu); | |
4973 | rq_dest = cpu_rq(dest_cpu); | |
4974 | ||
4975 | double_rq_lock(rq_src, rq_dest); | |
4976 | /* Already moved. */ | |
4977 | if (task_cpu(p) != src_cpu) | |
4978 | goto out; | |
4979 | /* Affinity changed (again). */ | |
4980 | if (!cpu_isset(dest_cpu, p->cpus_allowed)) | |
4981 | goto out; | |
4982 | ||
dd41f596 | 4983 | on_rq = p->se.on_rq; |
a8e504d2 IM |
4984 | if (on_rq) { |
4985 | update_rq_clock(rq_src); | |
4986 | deactivate_task(rq_src, p, 0, rq_src->clock); | |
4987 | } | |
1da177e4 | 4988 | set_task_cpu(p, dest_cpu); |
dd41f596 IM |
4989 | if (on_rq) { |
4990 | activate_task(rq_dest, p, 0); | |
4991 | check_preempt_curr(rq_dest, p); | |
1da177e4 | 4992 | } |
efc30814 | 4993 | ret = 1; |
1da177e4 LT |
4994 | out: |
4995 | double_rq_unlock(rq_src, rq_dest); | |
efc30814 | 4996 | return ret; |
1da177e4 LT |
4997 | } |
4998 | ||
4999 | /* | |
5000 | * migration_thread - this is a highprio system thread that performs | |
5001 | * thread migration by bumping thread off CPU then 'pushing' onto | |
5002 | * another runqueue. | |
5003 | */ | |
95cdf3b7 | 5004 | static int migration_thread(void *data) |
1da177e4 | 5005 | { |
1da177e4 | 5006 | int cpu = (long)data; |
70b97a7f | 5007 | struct rq *rq; |
1da177e4 LT |
5008 | |
5009 | rq = cpu_rq(cpu); | |
5010 | BUG_ON(rq->migration_thread != current); | |
5011 | ||
5012 | set_current_state(TASK_INTERRUPTIBLE); | |
5013 | while (!kthread_should_stop()) { | |
70b97a7f | 5014 | struct migration_req *req; |
1da177e4 | 5015 | struct list_head *head; |
1da177e4 | 5016 | |
1da177e4 LT |
5017 | spin_lock_irq(&rq->lock); |
5018 | ||
5019 | if (cpu_is_offline(cpu)) { | |
5020 | spin_unlock_irq(&rq->lock); | |
5021 | goto wait_to_die; | |
5022 | } | |
5023 | ||
5024 | if (rq->active_balance) { | |
5025 | active_load_balance(rq, cpu); | |
5026 | rq->active_balance = 0; | |
5027 | } | |
5028 | ||
5029 | head = &rq->migration_queue; | |
5030 | ||
5031 | if (list_empty(head)) { | |
5032 | spin_unlock_irq(&rq->lock); | |
5033 | schedule(); | |
5034 | set_current_state(TASK_INTERRUPTIBLE); | |
5035 | continue; | |
5036 | } | |
70b97a7f | 5037 | req = list_entry(head->next, struct migration_req, list); |
1da177e4 LT |
5038 | list_del_init(head->next); |
5039 | ||
674311d5 NP |
5040 | spin_unlock(&rq->lock); |
5041 | __migrate_task(req->task, cpu, req->dest_cpu); | |
5042 | local_irq_enable(); | |
1da177e4 LT |
5043 | |
5044 | complete(&req->done); | |
5045 | } | |
5046 | __set_current_state(TASK_RUNNING); | |
5047 | return 0; | |
5048 | ||
5049 | wait_to_die: | |
5050 | /* Wait for kthread_stop */ | |
5051 | set_current_state(TASK_INTERRUPTIBLE); | |
5052 | while (!kthread_should_stop()) { | |
5053 | schedule(); | |
5054 | set_current_state(TASK_INTERRUPTIBLE); | |
5055 | } | |
5056 | __set_current_state(TASK_RUNNING); | |
5057 | return 0; | |
5058 | } | |
5059 | ||
5060 | #ifdef CONFIG_HOTPLUG_CPU | |
054b9108 KK |
5061 | /* |
5062 | * Figure out where task on dead CPU should go, use force if neccessary. | |
5063 | * NOTE: interrupts should be disabled by the caller | |
5064 | */ | |
48f24c4d | 5065 | static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p) |
1da177e4 | 5066 | { |
efc30814 | 5067 | unsigned long flags; |
1da177e4 | 5068 | cpumask_t mask; |
70b97a7f IM |
5069 | struct rq *rq; |
5070 | int dest_cpu; | |
1da177e4 | 5071 | |
efc30814 | 5072 | restart: |
1da177e4 LT |
5073 | /* On same node? */ |
5074 | mask = node_to_cpumask(cpu_to_node(dead_cpu)); | |
48f24c4d | 5075 | cpus_and(mask, mask, p->cpus_allowed); |
1da177e4 LT |
5076 | dest_cpu = any_online_cpu(mask); |
5077 | ||
5078 | /* On any allowed CPU? */ | |
5079 | if (dest_cpu == NR_CPUS) | |
48f24c4d | 5080 | dest_cpu = any_online_cpu(p->cpus_allowed); |
1da177e4 LT |
5081 | |
5082 | /* No more Mr. Nice Guy. */ | |
5083 | if (dest_cpu == NR_CPUS) { | |
48f24c4d IM |
5084 | rq = task_rq_lock(p, &flags); |
5085 | cpus_setall(p->cpus_allowed); | |
5086 | dest_cpu = any_online_cpu(p->cpus_allowed); | |
efc30814 | 5087 | task_rq_unlock(rq, &flags); |
1da177e4 LT |
5088 | |
5089 | /* | |
5090 | * Don't tell them about moving exiting tasks or | |
5091 | * kernel threads (both mm NULL), since they never | |
5092 | * leave kernel. | |
5093 | */ | |
48f24c4d | 5094 | if (p->mm && printk_ratelimit()) |
1da177e4 LT |
5095 | printk(KERN_INFO "process %d (%s) no " |
5096 | "longer affine to cpu%d\n", | |
48f24c4d | 5097 | p->pid, p->comm, dead_cpu); |
1da177e4 | 5098 | } |
48f24c4d | 5099 | if (!__migrate_task(p, dead_cpu, dest_cpu)) |
efc30814 | 5100 | goto restart; |
1da177e4 LT |
5101 | } |
5102 | ||
5103 | /* | |
5104 | * While a dead CPU has no uninterruptible tasks queued at this point, | |
5105 | * it might still have a nonzero ->nr_uninterruptible counter, because | |
5106 | * for performance reasons the counter is not stricly tracking tasks to | |
5107 | * their home CPUs. So we just add the counter to another CPU's counter, | |
5108 | * to keep the global sum constant after CPU-down: | |
5109 | */ | |
70b97a7f | 5110 | static void migrate_nr_uninterruptible(struct rq *rq_src) |
1da177e4 | 5111 | { |
70b97a7f | 5112 | struct rq *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL)); |
1da177e4 LT |
5113 | unsigned long flags; |
5114 | ||
5115 | local_irq_save(flags); | |
5116 | double_rq_lock(rq_src, rq_dest); | |
5117 | rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible; | |
5118 | rq_src->nr_uninterruptible = 0; | |
5119 | double_rq_unlock(rq_src, rq_dest); | |
5120 | local_irq_restore(flags); | |
5121 | } | |
5122 | ||
5123 | /* Run through task list and migrate tasks from the dead cpu. */ | |
5124 | static void migrate_live_tasks(int src_cpu) | |
5125 | { | |
48f24c4d | 5126 | struct task_struct *p, *t; |
1da177e4 LT |
5127 | |
5128 | write_lock_irq(&tasklist_lock); | |
5129 | ||
48f24c4d IM |
5130 | do_each_thread(t, p) { |
5131 | if (p == current) | |
1da177e4 LT |
5132 | continue; |
5133 | ||
48f24c4d IM |
5134 | if (task_cpu(p) == src_cpu) |
5135 | move_task_off_dead_cpu(src_cpu, p); | |
5136 | } while_each_thread(t, p); | |
1da177e4 LT |
5137 | |
5138 | write_unlock_irq(&tasklist_lock); | |
5139 | } | |
5140 | ||
dd41f596 IM |
5141 | /* |
5142 | * Schedules idle task to be the next runnable task on current CPU. | |
1da177e4 | 5143 | * It does so by boosting its priority to highest possible and adding it to |
48f24c4d | 5144 | * the _front_ of the runqueue. Used by CPU offline code. |
1da177e4 LT |
5145 | */ |
5146 | void sched_idle_next(void) | |
5147 | { | |
48f24c4d | 5148 | int this_cpu = smp_processor_id(); |
70b97a7f | 5149 | struct rq *rq = cpu_rq(this_cpu); |
1da177e4 LT |
5150 | struct task_struct *p = rq->idle; |
5151 | unsigned long flags; | |
5152 | ||
5153 | /* cpu has to be offline */ | |
48f24c4d | 5154 | BUG_ON(cpu_online(this_cpu)); |
1da177e4 | 5155 | |
48f24c4d IM |
5156 | /* |
5157 | * Strictly not necessary since rest of the CPUs are stopped by now | |
5158 | * and interrupts disabled on the current cpu. | |
1da177e4 LT |
5159 | */ |
5160 | spin_lock_irqsave(&rq->lock, flags); | |
5161 | ||
dd41f596 | 5162 | __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1); |
48f24c4d IM |
5163 | |
5164 | /* Add idle task to the _front_ of its priority queue: */ | |
dd41f596 | 5165 | activate_idle_task(p, rq); |
1da177e4 LT |
5166 | |
5167 | spin_unlock_irqrestore(&rq->lock, flags); | |
5168 | } | |
5169 | ||
48f24c4d IM |
5170 | /* |
5171 | * Ensures that the idle task is using init_mm right before its cpu goes | |
1da177e4 LT |
5172 | * offline. |
5173 | */ | |
5174 | void idle_task_exit(void) | |
5175 | { | |
5176 | struct mm_struct *mm = current->active_mm; | |
5177 | ||
5178 | BUG_ON(cpu_online(smp_processor_id())); | |
5179 | ||
5180 | if (mm != &init_mm) | |
5181 | switch_mm(mm, &init_mm, current); | |
5182 | mmdrop(mm); | |
5183 | } | |
5184 | ||
054b9108 | 5185 | /* called under rq->lock with disabled interrupts */ |
36c8b586 | 5186 | static void migrate_dead(unsigned int dead_cpu, struct task_struct *p) |
1da177e4 | 5187 | { |
70b97a7f | 5188 | struct rq *rq = cpu_rq(dead_cpu); |
1da177e4 LT |
5189 | |
5190 | /* Must be exiting, otherwise would be on tasklist. */ | |
48f24c4d | 5191 | BUG_ON(p->exit_state != EXIT_ZOMBIE && p->exit_state != EXIT_DEAD); |
1da177e4 LT |
5192 | |
5193 | /* Cannot have done final schedule yet: would have vanished. */ | |
c394cc9f | 5194 | BUG_ON(p->state == TASK_DEAD); |
1da177e4 | 5195 | |
48f24c4d | 5196 | get_task_struct(p); |
1da177e4 LT |
5197 | |
5198 | /* | |
5199 | * Drop lock around migration; if someone else moves it, | |
5200 | * that's OK. No task can be added to this CPU, so iteration is | |
5201 | * fine. | |
054b9108 | 5202 | * NOTE: interrupts should be left disabled --dev@ |
1da177e4 | 5203 | */ |
054b9108 | 5204 | spin_unlock(&rq->lock); |
48f24c4d | 5205 | move_task_off_dead_cpu(dead_cpu, p); |
054b9108 | 5206 | spin_lock(&rq->lock); |
1da177e4 | 5207 | |
48f24c4d | 5208 | put_task_struct(p); |
1da177e4 LT |
5209 | } |
5210 | ||
5211 | /* release_task() removes task from tasklist, so we won't find dead tasks. */ | |
5212 | static void migrate_dead_tasks(unsigned int dead_cpu) | |
5213 | { | |
70b97a7f | 5214 | struct rq *rq = cpu_rq(dead_cpu); |
dd41f596 | 5215 | struct task_struct *next; |
48f24c4d | 5216 | |
dd41f596 IM |
5217 | for ( ; ; ) { |
5218 | if (!rq->nr_running) | |
5219 | break; | |
a8e504d2 | 5220 | update_rq_clock(rq); |
ff95f3df | 5221 | next = pick_next_task(rq, rq->curr); |
dd41f596 IM |
5222 | if (!next) |
5223 | break; | |
5224 | migrate_dead(dead_cpu, next); | |
e692ab53 | 5225 | |
1da177e4 LT |
5226 | } |
5227 | } | |
5228 | #endif /* CONFIG_HOTPLUG_CPU */ | |
5229 | ||
e692ab53 NP |
5230 | #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL) |
5231 | ||
5232 | static struct ctl_table sd_ctl_dir[] = { | |
e0361851 AD |
5233 | { |
5234 | .procname = "sched_domain", | |
5235 | .mode = 0755, | |
5236 | }, | |
e692ab53 NP |
5237 | {0,}, |
5238 | }; | |
5239 | ||
5240 | static struct ctl_table sd_ctl_root[] = { | |
e0361851 AD |
5241 | { |
5242 | .procname = "kernel", | |
5243 | .mode = 0755, | |
5244 | .child = sd_ctl_dir, | |
5245 | }, | |
e692ab53 NP |
5246 | {0,}, |
5247 | }; | |
5248 | ||
5249 | static struct ctl_table *sd_alloc_ctl_entry(int n) | |
5250 | { | |
5251 | struct ctl_table *entry = | |
5252 | kmalloc(n * sizeof(struct ctl_table), GFP_KERNEL); | |
5253 | ||
5254 | BUG_ON(!entry); | |
5255 | memset(entry, 0, n * sizeof(struct ctl_table)); | |
5256 | ||
5257 | return entry; | |
5258 | } | |
5259 | ||
5260 | static void | |
e0361851 | 5261 | set_table_entry(struct ctl_table *entry, |
e692ab53 NP |
5262 | const char *procname, void *data, int maxlen, |
5263 | mode_t mode, proc_handler *proc_handler) | |
5264 | { | |
e692ab53 NP |
5265 | entry->procname = procname; |
5266 | entry->data = data; | |
5267 | entry->maxlen = maxlen; | |
5268 | entry->mode = mode; | |
5269 | entry->proc_handler = proc_handler; | |
5270 | } | |
5271 | ||
5272 | static struct ctl_table * | |
5273 | sd_alloc_ctl_domain_table(struct sched_domain *sd) | |
5274 | { | |
5275 | struct ctl_table *table = sd_alloc_ctl_entry(14); | |
5276 | ||
e0361851 | 5277 | set_table_entry(&table[0], "min_interval", &sd->min_interval, |
e692ab53 | 5278 | sizeof(long), 0644, proc_doulongvec_minmax); |
e0361851 | 5279 | set_table_entry(&table[1], "max_interval", &sd->max_interval, |
e692ab53 | 5280 | sizeof(long), 0644, proc_doulongvec_minmax); |
e0361851 | 5281 | set_table_entry(&table[2], "busy_idx", &sd->busy_idx, |
e692ab53 | 5282 | sizeof(int), 0644, proc_dointvec_minmax); |
e0361851 | 5283 | set_table_entry(&table[3], "idle_idx", &sd->idle_idx, |
e692ab53 | 5284 | sizeof(int), 0644, proc_dointvec_minmax); |
e0361851 | 5285 | set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx, |
e692ab53 | 5286 | sizeof(int), 0644, proc_dointvec_minmax); |
e0361851 | 5287 | set_table_entry(&table[5], "wake_idx", &sd->wake_idx, |
e692ab53 | 5288 | sizeof(int), 0644, proc_dointvec_minmax); |
e0361851 | 5289 | set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx, |
e692ab53 | 5290 | sizeof(int), 0644, proc_dointvec_minmax); |
e0361851 | 5291 | set_table_entry(&table[7], "busy_factor", &sd->busy_factor, |
e692ab53 | 5292 | sizeof(int), 0644, proc_dointvec_minmax); |
e0361851 | 5293 | set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct, |
e692ab53 | 5294 | sizeof(int), 0644, proc_dointvec_minmax); |
e0361851 | 5295 | set_table_entry(&table[10], "cache_nice_tries", |
e692ab53 NP |
5296 | &sd->cache_nice_tries, |
5297 | sizeof(int), 0644, proc_dointvec_minmax); | |
e0361851 | 5298 | set_table_entry(&table[12], "flags", &sd->flags, |
e692ab53 NP |
5299 | sizeof(int), 0644, proc_dointvec_minmax); |
5300 | ||
5301 | return table; | |
5302 | } | |
5303 | ||
5304 | static ctl_table *sd_alloc_ctl_cpu_table(int cpu) | |
5305 | { | |
5306 | struct ctl_table *entry, *table; | |
5307 | struct sched_domain *sd; | |
5308 | int domain_num = 0, i; | |
5309 | char buf[32]; | |
5310 | ||
5311 | for_each_domain(cpu, sd) | |
5312 | domain_num++; | |
5313 | entry = table = sd_alloc_ctl_entry(domain_num + 1); | |
5314 | ||
5315 | i = 0; | |
5316 | for_each_domain(cpu, sd) { | |
5317 | snprintf(buf, 32, "domain%d", i); | |
e692ab53 NP |
5318 | entry->procname = kstrdup(buf, GFP_KERNEL); |
5319 | entry->mode = 0755; | |
5320 | entry->child = sd_alloc_ctl_domain_table(sd); | |
5321 | entry++; | |
5322 | i++; | |
5323 | } | |
5324 | return table; | |
5325 | } | |
5326 | ||
5327 | static struct ctl_table_header *sd_sysctl_header; | |
5328 | static void init_sched_domain_sysctl(void) | |
5329 | { | |
5330 | int i, cpu_num = num_online_cpus(); | |
5331 | struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1); | |
5332 | char buf[32]; | |
5333 | ||
5334 | sd_ctl_dir[0].child = entry; | |
5335 | ||
5336 | for (i = 0; i < cpu_num; i++, entry++) { | |
5337 | snprintf(buf, 32, "cpu%d", i); | |
e692ab53 NP |
5338 | entry->procname = kstrdup(buf, GFP_KERNEL); |
5339 | entry->mode = 0755; | |
5340 | entry->child = sd_alloc_ctl_cpu_table(i); | |
5341 | } | |
5342 | sd_sysctl_header = register_sysctl_table(sd_ctl_root); | |
5343 | } | |
5344 | #else | |
5345 | static void init_sched_domain_sysctl(void) | |
5346 | { | |
5347 | } | |
5348 | #endif | |
5349 | ||
1da177e4 LT |
5350 | /* |
5351 | * migration_call - callback that gets triggered when a CPU is added. | |
5352 | * Here we can start up the necessary migration thread for the new CPU. | |
5353 | */ | |
48f24c4d IM |
5354 | static int __cpuinit |
5355 | migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu) | |
1da177e4 | 5356 | { |
1da177e4 | 5357 | struct task_struct *p; |
48f24c4d | 5358 | int cpu = (long)hcpu; |
1da177e4 | 5359 | unsigned long flags; |
70b97a7f | 5360 | struct rq *rq; |
1da177e4 LT |
5361 | |
5362 | switch (action) { | |
5be9361c GS |
5363 | case CPU_LOCK_ACQUIRE: |
5364 | mutex_lock(&sched_hotcpu_mutex); | |
5365 | break; | |
5366 | ||
1da177e4 | 5367 | case CPU_UP_PREPARE: |
8bb78442 | 5368 | case CPU_UP_PREPARE_FROZEN: |
dd41f596 | 5369 | p = kthread_create(migration_thread, hcpu, "migration/%d", cpu); |
1da177e4 LT |
5370 | if (IS_ERR(p)) |
5371 | return NOTIFY_BAD; | |
1da177e4 LT |
5372 | kthread_bind(p, cpu); |
5373 | /* Must be high prio: stop_machine expects to yield to it. */ | |
5374 | rq = task_rq_lock(p, &flags); | |
dd41f596 | 5375 | __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1); |
1da177e4 LT |
5376 | task_rq_unlock(rq, &flags); |
5377 | cpu_rq(cpu)->migration_thread = p; | |
5378 | break; | |
48f24c4d | 5379 | |
1da177e4 | 5380 | case CPU_ONLINE: |
8bb78442 | 5381 | case CPU_ONLINE_FROZEN: |
1da177e4 LT |
5382 | /* Strictly unneccessary, as first user will wake it. */ |
5383 | wake_up_process(cpu_rq(cpu)->migration_thread); | |
5384 | break; | |
48f24c4d | 5385 | |
1da177e4 LT |
5386 | #ifdef CONFIG_HOTPLUG_CPU |
5387 | case CPU_UP_CANCELED: | |
8bb78442 | 5388 | case CPU_UP_CANCELED_FROZEN: |
fc75cdfa HC |
5389 | if (!cpu_rq(cpu)->migration_thread) |
5390 | break; | |
1da177e4 | 5391 | /* Unbind it from offline cpu so it can run. Fall thru. */ |
a4c4af7c HC |
5392 | kthread_bind(cpu_rq(cpu)->migration_thread, |
5393 | any_online_cpu(cpu_online_map)); | |
1da177e4 LT |
5394 | kthread_stop(cpu_rq(cpu)->migration_thread); |
5395 | cpu_rq(cpu)->migration_thread = NULL; | |
5396 | break; | |
48f24c4d | 5397 | |
1da177e4 | 5398 | case CPU_DEAD: |
8bb78442 | 5399 | case CPU_DEAD_FROZEN: |
1da177e4 LT |
5400 | migrate_live_tasks(cpu); |
5401 | rq = cpu_rq(cpu); | |
5402 | kthread_stop(rq->migration_thread); | |
5403 | rq->migration_thread = NULL; | |
5404 | /* Idle task back to normal (off runqueue, low prio) */ | |
5405 | rq = task_rq_lock(rq->idle, &flags); | |
a8e504d2 IM |
5406 | update_rq_clock(rq); |
5407 | deactivate_task(rq, rq->idle, 0, rq->clock); | |
1da177e4 | 5408 | rq->idle->static_prio = MAX_PRIO; |
dd41f596 IM |
5409 | __setscheduler(rq, rq->idle, SCHED_NORMAL, 0); |
5410 | rq->idle->sched_class = &idle_sched_class; | |
1da177e4 LT |
5411 | migrate_dead_tasks(cpu); |
5412 | task_rq_unlock(rq, &flags); | |
5413 | migrate_nr_uninterruptible(rq); | |
5414 | BUG_ON(rq->nr_running != 0); | |
5415 | ||
5416 | /* No need to migrate the tasks: it was best-effort if | |
5be9361c | 5417 | * they didn't take sched_hotcpu_mutex. Just wake up |
1da177e4 LT |
5418 | * the requestors. */ |
5419 | spin_lock_irq(&rq->lock); | |
5420 | while (!list_empty(&rq->migration_queue)) { | |
70b97a7f IM |
5421 | struct migration_req *req; |
5422 | ||
1da177e4 | 5423 | req = list_entry(rq->migration_queue.next, |
70b97a7f | 5424 | struct migration_req, list); |
1da177e4 LT |
5425 | list_del_init(&req->list); |
5426 | complete(&req->done); | |
5427 | } | |
5428 | spin_unlock_irq(&rq->lock); | |
5429 | break; | |
5430 | #endif | |
5be9361c GS |
5431 | case CPU_LOCK_RELEASE: |
5432 | mutex_unlock(&sched_hotcpu_mutex); | |
5433 | break; | |
1da177e4 LT |
5434 | } |
5435 | return NOTIFY_OK; | |
5436 | } | |
5437 | ||
5438 | /* Register at highest priority so that task migration (migrate_all_tasks) | |
5439 | * happens before everything else. | |
5440 | */ | |
26c2143b | 5441 | static struct notifier_block __cpuinitdata migration_notifier = { |
1da177e4 LT |
5442 | .notifier_call = migration_call, |
5443 | .priority = 10 | |
5444 | }; | |
5445 | ||
5446 | int __init migration_init(void) | |
5447 | { | |
5448 | void *cpu = (void *)(long)smp_processor_id(); | |
07dccf33 | 5449 | int err; |
48f24c4d IM |
5450 | |
5451 | /* Start one for the boot CPU: */ | |
07dccf33 AM |
5452 | err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu); |
5453 | BUG_ON(err == NOTIFY_BAD); | |
1da177e4 LT |
5454 | migration_call(&migration_notifier, CPU_ONLINE, cpu); |
5455 | register_cpu_notifier(&migration_notifier); | |
48f24c4d | 5456 | |
1da177e4 LT |
5457 | return 0; |
5458 | } | |
5459 | #endif | |
5460 | ||
5461 | #ifdef CONFIG_SMP | |
476f3534 CL |
5462 | |
5463 | /* Number of possible processor ids */ | |
5464 | int nr_cpu_ids __read_mostly = NR_CPUS; | |
5465 | EXPORT_SYMBOL(nr_cpu_ids); | |
5466 | ||
1a20ff27 | 5467 | #undef SCHED_DOMAIN_DEBUG |
1da177e4 LT |
5468 | #ifdef SCHED_DOMAIN_DEBUG |
5469 | static void sched_domain_debug(struct sched_domain *sd, int cpu) | |
5470 | { | |
5471 | int level = 0; | |
5472 | ||
41c7ce9a NP |
5473 | if (!sd) { |
5474 | printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu); | |
5475 | return; | |
5476 | } | |
5477 | ||
1da177e4 LT |
5478 | printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu); |
5479 | ||
5480 | do { | |
5481 | int i; | |
5482 | char str[NR_CPUS]; | |
5483 | struct sched_group *group = sd->groups; | |
5484 | cpumask_t groupmask; | |
5485 | ||
5486 | cpumask_scnprintf(str, NR_CPUS, sd->span); | |
5487 | cpus_clear(groupmask); | |
5488 | ||
5489 | printk(KERN_DEBUG); | |
5490 | for (i = 0; i < level + 1; i++) | |
5491 | printk(" "); | |
5492 | printk("domain %d: ", level); | |
5493 | ||
5494 | if (!(sd->flags & SD_LOAD_BALANCE)) { | |
5495 | printk("does not load-balance\n"); | |
5496 | if (sd->parent) | |
33859f7f MOS |
5497 | printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain" |
5498 | " has parent"); | |
1da177e4 LT |
5499 | break; |
5500 | } | |
5501 | ||
5502 | printk("span %s\n", str); | |
5503 | ||
5504 | if (!cpu_isset(cpu, sd->span)) | |
33859f7f MOS |
5505 | printk(KERN_ERR "ERROR: domain->span does not contain " |
5506 | "CPU%d\n", cpu); | |
1da177e4 | 5507 | if (!cpu_isset(cpu, group->cpumask)) |
33859f7f MOS |
5508 | printk(KERN_ERR "ERROR: domain->groups does not contain" |
5509 | " CPU%d\n", cpu); | |
1da177e4 LT |
5510 | |
5511 | printk(KERN_DEBUG); | |
5512 | for (i = 0; i < level + 2; i++) | |
5513 | printk(" "); | |
5514 | printk("groups:"); | |
5515 | do { | |
5516 | if (!group) { | |
5517 | printk("\n"); | |
5518 | printk(KERN_ERR "ERROR: group is NULL\n"); | |
5519 | break; | |
5520 | } | |
5521 | ||
5517d86b | 5522 | if (!group->__cpu_power) { |
1da177e4 | 5523 | printk("\n"); |
33859f7f MOS |
5524 | printk(KERN_ERR "ERROR: domain->cpu_power not " |
5525 | "set\n"); | |
1da177e4 LT |
5526 | } |
5527 | ||
5528 | if (!cpus_weight(group->cpumask)) { | |
5529 | printk("\n"); | |
5530 | printk(KERN_ERR "ERROR: empty group\n"); | |
5531 | } | |
5532 | ||
5533 | if (cpus_intersects(groupmask, group->cpumask)) { | |
5534 | printk("\n"); | |
5535 | printk(KERN_ERR "ERROR: repeated CPUs\n"); | |
5536 | } | |
5537 | ||
5538 | cpus_or(groupmask, groupmask, group->cpumask); | |
5539 | ||
5540 | cpumask_scnprintf(str, NR_CPUS, group->cpumask); | |
5541 | printk(" %s", str); | |
5542 | ||
5543 | group = group->next; | |
5544 | } while (group != sd->groups); | |
5545 | printk("\n"); | |
5546 | ||
5547 | if (!cpus_equal(sd->span, groupmask)) | |
33859f7f MOS |
5548 | printk(KERN_ERR "ERROR: groups don't span " |
5549 | "domain->span\n"); | |
1da177e4 LT |
5550 | |
5551 | level++; | |
5552 | sd = sd->parent; | |
33859f7f MOS |
5553 | if (!sd) |
5554 | continue; | |
1da177e4 | 5555 | |
33859f7f MOS |
5556 | if (!cpus_subset(groupmask, sd->span)) |
5557 | printk(KERN_ERR "ERROR: parent span is not a superset " | |
5558 | "of domain->span\n"); | |
1da177e4 LT |
5559 | |
5560 | } while (sd); | |
5561 | } | |
5562 | #else | |
48f24c4d | 5563 | # define sched_domain_debug(sd, cpu) do { } while (0) |
1da177e4 LT |
5564 | #endif |
5565 | ||
1a20ff27 | 5566 | static int sd_degenerate(struct sched_domain *sd) |
245af2c7 SS |
5567 | { |
5568 | if (cpus_weight(sd->span) == 1) | |
5569 | return 1; | |
5570 | ||
5571 | /* Following flags need at least 2 groups */ | |
5572 | if (sd->flags & (SD_LOAD_BALANCE | | |
5573 | SD_BALANCE_NEWIDLE | | |
5574 | SD_BALANCE_FORK | | |
89c4710e SS |
5575 | SD_BALANCE_EXEC | |
5576 | SD_SHARE_CPUPOWER | | |
5577 | SD_SHARE_PKG_RESOURCES)) { | |
245af2c7 SS |
5578 | if (sd->groups != sd->groups->next) |
5579 | return 0; | |
5580 | } | |
5581 | ||
5582 | /* Following flags don't use groups */ | |
5583 | if (sd->flags & (SD_WAKE_IDLE | | |
5584 | SD_WAKE_AFFINE | | |
5585 | SD_WAKE_BALANCE)) | |
5586 | return 0; | |
5587 | ||
5588 | return 1; | |
5589 | } | |
5590 | ||
48f24c4d IM |
5591 | static int |
5592 | sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent) | |
245af2c7 SS |
5593 | { |
5594 | unsigned long cflags = sd->flags, pflags = parent->flags; | |
5595 | ||
5596 | if (sd_degenerate(parent)) | |
5597 | return 1; | |
5598 | ||
5599 | if (!cpus_equal(sd->span, parent->span)) | |
5600 | return 0; | |
5601 | ||
5602 | /* Does parent contain flags not in child? */ | |
5603 | /* WAKE_BALANCE is a subset of WAKE_AFFINE */ | |
5604 | if (cflags & SD_WAKE_AFFINE) | |
5605 | pflags &= ~SD_WAKE_BALANCE; | |
5606 | /* Flags needing groups don't count if only 1 group in parent */ | |
5607 | if (parent->groups == parent->groups->next) { | |
5608 | pflags &= ~(SD_LOAD_BALANCE | | |
5609 | SD_BALANCE_NEWIDLE | | |
5610 | SD_BALANCE_FORK | | |
89c4710e SS |
5611 | SD_BALANCE_EXEC | |
5612 | SD_SHARE_CPUPOWER | | |
5613 | SD_SHARE_PKG_RESOURCES); | |
245af2c7 SS |
5614 | } |
5615 | if (~cflags & pflags) | |
5616 | return 0; | |
5617 | ||
5618 | return 1; | |
5619 | } | |
5620 | ||
1da177e4 LT |
5621 | /* |
5622 | * Attach the domain 'sd' to 'cpu' as its base domain. Callers must | |
5623 | * hold the hotplug lock. | |
5624 | */ | |
9c1cfda2 | 5625 | static void cpu_attach_domain(struct sched_domain *sd, int cpu) |
1da177e4 | 5626 | { |
70b97a7f | 5627 | struct rq *rq = cpu_rq(cpu); |
245af2c7 SS |
5628 | struct sched_domain *tmp; |
5629 | ||
5630 | /* Remove the sched domains which do not contribute to scheduling. */ | |
5631 | for (tmp = sd; tmp; tmp = tmp->parent) { | |
5632 | struct sched_domain *parent = tmp->parent; | |
5633 | if (!parent) | |
5634 | break; | |
1a848870 | 5635 | if (sd_parent_degenerate(tmp, parent)) { |
245af2c7 | 5636 | tmp->parent = parent->parent; |
1a848870 SS |
5637 | if (parent->parent) |
5638 | parent->parent->child = tmp; | |
5639 | } | |
245af2c7 SS |
5640 | } |
5641 | ||
1a848870 | 5642 | if (sd && sd_degenerate(sd)) { |
245af2c7 | 5643 | sd = sd->parent; |
1a848870 SS |
5644 | if (sd) |
5645 | sd->child = NULL; | |
5646 | } | |
1da177e4 LT |
5647 | |
5648 | sched_domain_debug(sd, cpu); | |
5649 | ||
674311d5 | 5650 | rcu_assign_pointer(rq->sd, sd); |
1da177e4 LT |
5651 | } |
5652 | ||
5653 | /* cpus with isolated domains */ | |
67af63a6 | 5654 | static cpumask_t cpu_isolated_map = CPU_MASK_NONE; |
1da177e4 LT |
5655 | |
5656 | /* Setup the mask of cpus configured for isolated domains */ | |
5657 | static int __init isolated_cpu_setup(char *str) | |
5658 | { | |
5659 | int ints[NR_CPUS], i; | |
5660 | ||
5661 | str = get_options(str, ARRAY_SIZE(ints), ints); | |
5662 | cpus_clear(cpu_isolated_map); | |
5663 | for (i = 1; i <= ints[0]; i++) | |
5664 | if (ints[i] < NR_CPUS) | |
5665 | cpu_set(ints[i], cpu_isolated_map); | |
5666 | return 1; | |
5667 | } | |
5668 | ||
5669 | __setup ("isolcpus=", isolated_cpu_setup); | |
5670 | ||
5671 | /* | |
6711cab4 SS |
5672 | * init_sched_build_groups takes the cpumask we wish to span, and a pointer |
5673 | * to a function which identifies what group(along with sched group) a CPU | |
5674 | * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS | |
5675 | * (due to the fact that we keep track of groups covered with a cpumask_t). | |
1da177e4 LT |
5676 | * |
5677 | * init_sched_build_groups will build a circular linked list of the groups | |
5678 | * covered by the given span, and will set each group's ->cpumask correctly, | |
5679 | * and ->cpu_power to 0. | |
5680 | */ | |
a616058b | 5681 | static void |
6711cab4 SS |
5682 | init_sched_build_groups(cpumask_t span, const cpumask_t *cpu_map, |
5683 | int (*group_fn)(int cpu, const cpumask_t *cpu_map, | |
5684 | struct sched_group **sg)) | |
1da177e4 LT |
5685 | { |
5686 | struct sched_group *first = NULL, *last = NULL; | |
5687 | cpumask_t covered = CPU_MASK_NONE; | |
5688 | int i; | |
5689 | ||
5690 | for_each_cpu_mask(i, span) { | |
6711cab4 SS |
5691 | struct sched_group *sg; |
5692 | int group = group_fn(i, cpu_map, &sg); | |
1da177e4 LT |
5693 | int j; |
5694 | ||
5695 | if (cpu_isset(i, covered)) | |
5696 | continue; | |
5697 | ||
5698 | sg->cpumask = CPU_MASK_NONE; | |
5517d86b | 5699 | sg->__cpu_power = 0; |
1da177e4 LT |
5700 | |
5701 | for_each_cpu_mask(j, span) { | |
6711cab4 | 5702 | if (group_fn(j, cpu_map, NULL) != group) |
1da177e4 LT |
5703 | continue; |
5704 | ||
5705 | cpu_set(j, covered); | |
5706 | cpu_set(j, sg->cpumask); | |
5707 | } | |
5708 | if (!first) | |
5709 | first = sg; | |
5710 | if (last) | |
5711 | last->next = sg; | |
5712 | last = sg; | |
5713 | } | |
5714 | last->next = first; | |
5715 | } | |
5716 | ||
9c1cfda2 | 5717 | #define SD_NODES_PER_DOMAIN 16 |
1da177e4 | 5718 | |
9c1cfda2 | 5719 | #ifdef CONFIG_NUMA |
198e2f18 | 5720 | |
9c1cfda2 JH |
5721 | /** |
5722 | * find_next_best_node - find the next node to include in a sched_domain | |
5723 | * @node: node whose sched_domain we're building | |
5724 | * @used_nodes: nodes already in the sched_domain | |
5725 | * | |
5726 | * Find the next node to include in a given scheduling domain. Simply | |
5727 | * finds the closest node not already in the @used_nodes map. | |
5728 | * | |
5729 | * Should use nodemask_t. | |
5730 | */ | |
5731 | static int find_next_best_node(int node, unsigned long *used_nodes) | |
5732 | { | |
5733 | int i, n, val, min_val, best_node = 0; | |
5734 | ||
5735 | min_val = INT_MAX; | |
5736 | ||
5737 | for (i = 0; i < MAX_NUMNODES; i++) { | |
5738 | /* Start at @node */ | |
5739 | n = (node + i) % MAX_NUMNODES; | |
5740 | ||
5741 | if (!nr_cpus_node(n)) | |
5742 | continue; | |
5743 | ||
5744 | /* Skip already used nodes */ | |
5745 | if (test_bit(n, used_nodes)) | |
5746 | continue; | |
5747 | ||
5748 | /* Simple min distance search */ | |
5749 | val = node_distance(node, n); | |
5750 | ||
5751 | if (val < min_val) { | |
5752 | min_val = val; | |
5753 | best_node = n; | |
5754 | } | |
5755 | } | |
5756 | ||
5757 | set_bit(best_node, used_nodes); | |
5758 | return best_node; | |
5759 | } | |
5760 | ||
5761 | /** | |
5762 | * sched_domain_node_span - get a cpumask for a node's sched_domain | |
5763 | * @node: node whose cpumask we're constructing | |
5764 | * @size: number of nodes to include in this span | |
5765 | * | |
5766 | * Given a node, construct a good cpumask for its sched_domain to span. It | |
5767 | * should be one that prevents unnecessary balancing, but also spreads tasks | |
5768 | * out optimally. | |
5769 | */ | |
5770 | static cpumask_t sched_domain_node_span(int node) | |
5771 | { | |
9c1cfda2 | 5772 | DECLARE_BITMAP(used_nodes, MAX_NUMNODES); |
48f24c4d IM |
5773 | cpumask_t span, nodemask; |
5774 | int i; | |
9c1cfda2 JH |
5775 | |
5776 | cpus_clear(span); | |
5777 | bitmap_zero(used_nodes, MAX_NUMNODES); | |
5778 | ||
5779 | nodemask = node_to_cpumask(node); | |
5780 | cpus_or(span, span, nodemask); | |
5781 | set_bit(node, used_nodes); | |
5782 | ||
5783 | for (i = 1; i < SD_NODES_PER_DOMAIN; i++) { | |
5784 | int next_node = find_next_best_node(node, used_nodes); | |
48f24c4d | 5785 | |
9c1cfda2 JH |
5786 | nodemask = node_to_cpumask(next_node); |
5787 | cpus_or(span, span, nodemask); | |
5788 | } | |
5789 | ||
5790 | return span; | |
5791 | } | |
5792 | #endif | |
5793 | ||
5c45bf27 | 5794 | int sched_smt_power_savings = 0, sched_mc_power_savings = 0; |
48f24c4d | 5795 | |
9c1cfda2 | 5796 | /* |
48f24c4d | 5797 | * SMT sched-domains: |
9c1cfda2 | 5798 | */ |
1da177e4 LT |
5799 | #ifdef CONFIG_SCHED_SMT |
5800 | static DEFINE_PER_CPU(struct sched_domain, cpu_domains); | |
6711cab4 | 5801 | static DEFINE_PER_CPU(struct sched_group, sched_group_cpus); |
48f24c4d | 5802 | |
6711cab4 SS |
5803 | static int cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, |
5804 | struct sched_group **sg) | |
1da177e4 | 5805 | { |
6711cab4 SS |
5806 | if (sg) |
5807 | *sg = &per_cpu(sched_group_cpus, cpu); | |
1da177e4 LT |
5808 | return cpu; |
5809 | } | |
5810 | #endif | |
5811 | ||
48f24c4d IM |
5812 | /* |
5813 | * multi-core sched-domains: | |
5814 | */ | |
1e9f28fa SS |
5815 | #ifdef CONFIG_SCHED_MC |
5816 | static DEFINE_PER_CPU(struct sched_domain, core_domains); | |
6711cab4 | 5817 | static DEFINE_PER_CPU(struct sched_group, sched_group_core); |
1e9f28fa SS |
5818 | #endif |
5819 | ||
5820 | #if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT) | |
6711cab4 SS |
5821 | static int cpu_to_core_group(int cpu, const cpumask_t *cpu_map, |
5822 | struct sched_group **sg) | |
1e9f28fa | 5823 | { |
6711cab4 | 5824 | int group; |
a616058b SS |
5825 | cpumask_t mask = cpu_sibling_map[cpu]; |
5826 | cpus_and(mask, mask, *cpu_map); | |
6711cab4 SS |
5827 | group = first_cpu(mask); |
5828 | if (sg) | |
5829 | *sg = &per_cpu(sched_group_core, group); | |
5830 | return group; | |
1e9f28fa SS |
5831 | } |
5832 | #elif defined(CONFIG_SCHED_MC) | |
6711cab4 SS |
5833 | static int cpu_to_core_group(int cpu, const cpumask_t *cpu_map, |
5834 | struct sched_group **sg) | |
1e9f28fa | 5835 | { |
6711cab4 SS |
5836 | if (sg) |
5837 | *sg = &per_cpu(sched_group_core, cpu); | |
1e9f28fa SS |
5838 | return cpu; |
5839 | } | |
5840 | #endif | |
5841 | ||
1da177e4 | 5842 | static DEFINE_PER_CPU(struct sched_domain, phys_domains); |
6711cab4 | 5843 | static DEFINE_PER_CPU(struct sched_group, sched_group_phys); |
48f24c4d | 5844 | |
6711cab4 SS |
5845 | static int cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, |
5846 | struct sched_group **sg) | |
1da177e4 | 5847 | { |
6711cab4 | 5848 | int group; |
48f24c4d | 5849 | #ifdef CONFIG_SCHED_MC |
1e9f28fa | 5850 | cpumask_t mask = cpu_coregroup_map(cpu); |
a616058b | 5851 | cpus_and(mask, mask, *cpu_map); |
6711cab4 | 5852 | group = first_cpu(mask); |
1e9f28fa | 5853 | #elif defined(CONFIG_SCHED_SMT) |
a616058b SS |
5854 | cpumask_t mask = cpu_sibling_map[cpu]; |
5855 | cpus_and(mask, mask, *cpu_map); | |
6711cab4 | 5856 | group = first_cpu(mask); |
1da177e4 | 5857 | #else |
6711cab4 | 5858 | group = cpu; |
1da177e4 | 5859 | #endif |
6711cab4 SS |
5860 | if (sg) |
5861 | *sg = &per_cpu(sched_group_phys, group); | |
5862 | return group; | |
1da177e4 LT |
5863 | } |
5864 | ||
5865 | #ifdef CONFIG_NUMA | |
1da177e4 | 5866 | /* |
9c1cfda2 JH |
5867 | * The init_sched_build_groups can't handle what we want to do with node |
5868 | * groups, so roll our own. Now each node has its own list of groups which | |
5869 | * gets dynamically allocated. | |
1da177e4 | 5870 | */ |
9c1cfda2 | 5871 | static DEFINE_PER_CPU(struct sched_domain, node_domains); |
d1b55138 | 5872 | static struct sched_group **sched_group_nodes_bycpu[NR_CPUS]; |
1da177e4 | 5873 | |
9c1cfda2 | 5874 | static DEFINE_PER_CPU(struct sched_domain, allnodes_domains); |
6711cab4 | 5875 | static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes); |
9c1cfda2 | 5876 | |
6711cab4 SS |
5877 | static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map, |
5878 | struct sched_group **sg) | |
9c1cfda2 | 5879 | { |
6711cab4 SS |
5880 | cpumask_t nodemask = node_to_cpumask(cpu_to_node(cpu)); |
5881 | int group; | |
5882 | ||
5883 | cpus_and(nodemask, nodemask, *cpu_map); | |
5884 | group = first_cpu(nodemask); | |
5885 | ||
5886 | if (sg) | |
5887 | *sg = &per_cpu(sched_group_allnodes, group); | |
5888 | return group; | |
1da177e4 | 5889 | } |
6711cab4 | 5890 | |
08069033 SS |
5891 | static void init_numa_sched_groups_power(struct sched_group *group_head) |
5892 | { | |
5893 | struct sched_group *sg = group_head; | |
5894 | int j; | |
5895 | ||
5896 | if (!sg) | |
5897 | return; | |
5898 | next_sg: | |
5899 | for_each_cpu_mask(j, sg->cpumask) { | |
5900 | struct sched_domain *sd; | |
5901 | ||
5902 | sd = &per_cpu(phys_domains, j); | |
5903 | if (j != first_cpu(sd->groups->cpumask)) { | |
5904 | /* | |
5905 | * Only add "power" once for each | |
5906 | * physical package. | |
5907 | */ | |
5908 | continue; | |
5909 | } | |
5910 | ||
5517d86b | 5911 | sg_inc_cpu_power(sg, sd->groups->__cpu_power); |
08069033 SS |
5912 | } |
5913 | sg = sg->next; | |
5914 | if (sg != group_head) | |
5915 | goto next_sg; | |
5916 | } | |
1da177e4 LT |
5917 | #endif |
5918 | ||
a616058b | 5919 | #ifdef CONFIG_NUMA |
51888ca2 SV |
5920 | /* Free memory allocated for various sched_group structures */ |
5921 | static void free_sched_groups(const cpumask_t *cpu_map) | |
5922 | { | |
a616058b | 5923 | int cpu, i; |
51888ca2 SV |
5924 | |
5925 | for_each_cpu_mask(cpu, *cpu_map) { | |
51888ca2 SV |
5926 | struct sched_group **sched_group_nodes |
5927 | = sched_group_nodes_bycpu[cpu]; | |
5928 | ||
51888ca2 SV |
5929 | if (!sched_group_nodes) |
5930 | continue; | |
5931 | ||
5932 | for (i = 0; i < MAX_NUMNODES; i++) { | |
5933 | cpumask_t nodemask = node_to_cpumask(i); | |
5934 | struct sched_group *oldsg, *sg = sched_group_nodes[i]; | |
5935 | ||
5936 | cpus_and(nodemask, nodemask, *cpu_map); | |
5937 | if (cpus_empty(nodemask)) | |
5938 | continue; | |
5939 | ||
5940 | if (sg == NULL) | |
5941 | continue; | |
5942 | sg = sg->next; | |
5943 | next_sg: | |
5944 | oldsg = sg; | |
5945 | sg = sg->next; | |
5946 | kfree(oldsg); | |
5947 | if (oldsg != sched_group_nodes[i]) | |
5948 | goto next_sg; | |
5949 | } | |
5950 | kfree(sched_group_nodes); | |
5951 | sched_group_nodes_bycpu[cpu] = NULL; | |
5952 | } | |
51888ca2 | 5953 | } |
a616058b SS |
5954 | #else |
5955 | static void free_sched_groups(const cpumask_t *cpu_map) | |
5956 | { | |
5957 | } | |
5958 | #endif | |
51888ca2 | 5959 | |
89c4710e SS |
5960 | /* |
5961 | * Initialize sched groups cpu_power. | |
5962 | * | |
5963 | * cpu_power indicates the capacity of sched group, which is used while | |
5964 | * distributing the load between different sched groups in a sched domain. | |
5965 | * Typically cpu_power for all the groups in a sched domain will be same unless | |
5966 | * there are asymmetries in the topology. If there are asymmetries, group | |
5967 | * having more cpu_power will pickup more load compared to the group having | |
5968 | * less cpu_power. | |
5969 | * | |
5970 | * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents | |
5971 | * the maximum number of tasks a group can handle in the presence of other idle | |
5972 | * or lightly loaded groups in the same sched domain. | |
5973 | */ | |
5974 | static void init_sched_groups_power(int cpu, struct sched_domain *sd) | |
5975 | { | |
5976 | struct sched_domain *child; | |
5977 | struct sched_group *group; | |
5978 | ||
5979 | WARN_ON(!sd || !sd->groups); | |
5980 | ||
5981 | if (cpu != first_cpu(sd->groups->cpumask)) | |
5982 | return; | |
5983 | ||
5984 | child = sd->child; | |
5985 | ||
5517d86b ED |
5986 | sd->groups->__cpu_power = 0; |
5987 | ||
89c4710e SS |
5988 | /* |
5989 | * For perf policy, if the groups in child domain share resources | |
5990 | * (for example cores sharing some portions of the cache hierarchy | |
5991 | * or SMT), then set this domain groups cpu_power such that each group | |
5992 | * can handle only one task, when there are other idle groups in the | |
5993 | * same sched domain. | |
5994 | */ | |
5995 | if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) && | |
5996 | (child->flags & | |
5997 | (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) { | |
5517d86b | 5998 | sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE); |
89c4710e SS |
5999 | return; |
6000 | } | |
6001 | ||
89c4710e SS |
6002 | /* |
6003 | * add cpu_power of each child group to this groups cpu_power | |
6004 | */ | |
6005 | group = child->groups; | |
6006 | do { | |
5517d86b | 6007 | sg_inc_cpu_power(sd->groups, group->__cpu_power); |
89c4710e SS |
6008 | group = group->next; |
6009 | } while (group != child->groups); | |
6010 | } | |
6011 | ||
1da177e4 | 6012 | /* |
1a20ff27 DG |
6013 | * Build sched domains for a given set of cpus and attach the sched domains |
6014 | * to the individual cpus | |
1da177e4 | 6015 | */ |
51888ca2 | 6016 | static int build_sched_domains(const cpumask_t *cpu_map) |
1da177e4 LT |
6017 | { |
6018 | int i; | |
d1b55138 JH |
6019 | #ifdef CONFIG_NUMA |
6020 | struct sched_group **sched_group_nodes = NULL; | |
6711cab4 | 6021 | int sd_allnodes = 0; |
d1b55138 JH |
6022 | |
6023 | /* | |
6024 | * Allocate the per-node list of sched groups | |
6025 | */ | |
dd41f596 | 6026 | sched_group_nodes = kzalloc(sizeof(struct sched_group *)*MAX_NUMNODES, |
d3a5aa98 | 6027 | GFP_KERNEL); |
d1b55138 JH |
6028 | if (!sched_group_nodes) { |
6029 | printk(KERN_WARNING "Can not alloc sched group node list\n"); | |
51888ca2 | 6030 | return -ENOMEM; |
d1b55138 JH |
6031 | } |
6032 | sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes; | |
6033 | #endif | |
1da177e4 LT |
6034 | |
6035 | /* | |
1a20ff27 | 6036 | * Set up domains for cpus specified by the cpu_map. |
1da177e4 | 6037 | */ |
1a20ff27 | 6038 | for_each_cpu_mask(i, *cpu_map) { |
1da177e4 LT |
6039 | struct sched_domain *sd = NULL, *p; |
6040 | cpumask_t nodemask = node_to_cpumask(cpu_to_node(i)); | |
6041 | ||
1a20ff27 | 6042 | cpus_and(nodemask, nodemask, *cpu_map); |
1da177e4 LT |
6043 | |
6044 | #ifdef CONFIG_NUMA | |
dd41f596 IM |
6045 | if (cpus_weight(*cpu_map) > |
6046 | SD_NODES_PER_DOMAIN*cpus_weight(nodemask)) { | |
9c1cfda2 JH |
6047 | sd = &per_cpu(allnodes_domains, i); |
6048 | *sd = SD_ALLNODES_INIT; | |
6049 | sd->span = *cpu_map; | |
6711cab4 | 6050 | cpu_to_allnodes_group(i, cpu_map, &sd->groups); |
9c1cfda2 | 6051 | p = sd; |
6711cab4 | 6052 | sd_allnodes = 1; |
9c1cfda2 JH |
6053 | } else |
6054 | p = NULL; | |
6055 | ||
1da177e4 | 6056 | sd = &per_cpu(node_domains, i); |
1da177e4 | 6057 | *sd = SD_NODE_INIT; |
9c1cfda2 JH |
6058 | sd->span = sched_domain_node_span(cpu_to_node(i)); |
6059 | sd->parent = p; | |
1a848870 SS |
6060 | if (p) |
6061 | p->child = sd; | |
9c1cfda2 | 6062 | cpus_and(sd->span, sd->span, *cpu_map); |
1da177e4 LT |
6063 | #endif |
6064 | ||
6065 | p = sd; | |
6066 | sd = &per_cpu(phys_domains, i); | |
1da177e4 LT |
6067 | *sd = SD_CPU_INIT; |
6068 | sd->span = nodemask; | |
6069 | sd->parent = p; | |
1a848870 SS |
6070 | if (p) |
6071 | p->child = sd; | |
6711cab4 | 6072 | cpu_to_phys_group(i, cpu_map, &sd->groups); |
1da177e4 | 6073 | |
1e9f28fa SS |
6074 | #ifdef CONFIG_SCHED_MC |
6075 | p = sd; | |
6076 | sd = &per_cpu(core_domains, i); | |
1e9f28fa SS |
6077 | *sd = SD_MC_INIT; |
6078 | sd->span = cpu_coregroup_map(i); | |
6079 | cpus_and(sd->span, sd->span, *cpu_map); | |
6080 | sd->parent = p; | |
1a848870 | 6081 | p->child = sd; |
6711cab4 | 6082 | cpu_to_core_group(i, cpu_map, &sd->groups); |
1e9f28fa SS |
6083 | #endif |
6084 | ||
1da177e4 LT |
6085 | #ifdef CONFIG_SCHED_SMT |
6086 | p = sd; | |
6087 | sd = &per_cpu(cpu_domains, i); | |
1da177e4 LT |
6088 | *sd = SD_SIBLING_INIT; |
6089 | sd->span = cpu_sibling_map[i]; | |
1a20ff27 | 6090 | cpus_and(sd->span, sd->span, *cpu_map); |
1da177e4 | 6091 | sd->parent = p; |
1a848870 | 6092 | p->child = sd; |
6711cab4 | 6093 | cpu_to_cpu_group(i, cpu_map, &sd->groups); |
1da177e4 LT |
6094 | #endif |
6095 | } | |
6096 | ||
6097 | #ifdef CONFIG_SCHED_SMT | |
6098 | /* Set up CPU (sibling) groups */ | |
9c1cfda2 | 6099 | for_each_cpu_mask(i, *cpu_map) { |
1da177e4 | 6100 | cpumask_t this_sibling_map = cpu_sibling_map[i]; |
1a20ff27 | 6101 | cpus_and(this_sibling_map, this_sibling_map, *cpu_map); |
1da177e4 LT |
6102 | if (i != first_cpu(this_sibling_map)) |
6103 | continue; | |
6104 | ||
dd41f596 IM |
6105 | init_sched_build_groups(this_sibling_map, cpu_map, |
6106 | &cpu_to_cpu_group); | |
1da177e4 LT |
6107 | } |
6108 | #endif | |
6109 | ||
1e9f28fa SS |
6110 | #ifdef CONFIG_SCHED_MC |
6111 | /* Set up multi-core groups */ | |
6112 | for_each_cpu_mask(i, *cpu_map) { | |
6113 | cpumask_t this_core_map = cpu_coregroup_map(i); | |
6114 | cpus_and(this_core_map, this_core_map, *cpu_map); | |
6115 | if (i != first_cpu(this_core_map)) | |
6116 | continue; | |
dd41f596 IM |
6117 | init_sched_build_groups(this_core_map, cpu_map, |
6118 | &cpu_to_core_group); | |
1e9f28fa SS |
6119 | } |
6120 | #endif | |
6121 | ||
1da177e4 LT |
6122 | /* Set up physical groups */ |
6123 | for (i = 0; i < MAX_NUMNODES; i++) { | |
6124 | cpumask_t nodemask = node_to_cpumask(i); | |
6125 | ||
1a20ff27 | 6126 | cpus_and(nodemask, nodemask, *cpu_map); |
1da177e4 LT |
6127 | if (cpus_empty(nodemask)) |
6128 | continue; | |
6129 | ||
6711cab4 | 6130 | init_sched_build_groups(nodemask, cpu_map, &cpu_to_phys_group); |
1da177e4 LT |
6131 | } |
6132 | ||
6133 | #ifdef CONFIG_NUMA | |
6134 | /* Set up node groups */ | |
6711cab4 | 6135 | if (sd_allnodes) |
dd41f596 IM |
6136 | init_sched_build_groups(*cpu_map, cpu_map, |
6137 | &cpu_to_allnodes_group); | |
9c1cfda2 JH |
6138 | |
6139 | for (i = 0; i < MAX_NUMNODES; i++) { | |
6140 | /* Set up node groups */ | |
6141 | struct sched_group *sg, *prev; | |
6142 | cpumask_t nodemask = node_to_cpumask(i); | |
6143 | cpumask_t domainspan; | |
6144 | cpumask_t covered = CPU_MASK_NONE; | |
6145 | int j; | |
6146 | ||
6147 | cpus_and(nodemask, nodemask, *cpu_map); | |
d1b55138 JH |
6148 | if (cpus_empty(nodemask)) { |
6149 | sched_group_nodes[i] = NULL; | |
9c1cfda2 | 6150 | continue; |
d1b55138 | 6151 | } |
9c1cfda2 JH |
6152 | |
6153 | domainspan = sched_domain_node_span(i); | |
6154 | cpus_and(domainspan, domainspan, *cpu_map); | |
6155 | ||
15f0b676 | 6156 | sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i); |
51888ca2 SV |
6157 | if (!sg) { |
6158 | printk(KERN_WARNING "Can not alloc domain group for " | |
6159 | "node %d\n", i); | |
6160 | goto error; | |
6161 | } | |
9c1cfda2 JH |
6162 | sched_group_nodes[i] = sg; |
6163 | for_each_cpu_mask(j, nodemask) { | |
6164 | struct sched_domain *sd; | |
9761eea8 | 6165 | |
9c1cfda2 JH |
6166 | sd = &per_cpu(node_domains, j); |
6167 | sd->groups = sg; | |
9c1cfda2 | 6168 | } |
5517d86b | 6169 | sg->__cpu_power = 0; |
9c1cfda2 | 6170 | sg->cpumask = nodemask; |
51888ca2 | 6171 | sg->next = sg; |
9c1cfda2 JH |
6172 | cpus_or(covered, covered, nodemask); |
6173 | prev = sg; | |
6174 | ||
6175 | for (j = 0; j < MAX_NUMNODES; j++) { | |
6176 | cpumask_t tmp, notcovered; | |
6177 | int n = (i + j) % MAX_NUMNODES; | |
6178 | ||
6179 | cpus_complement(notcovered, covered); | |
6180 | cpus_and(tmp, notcovered, *cpu_map); | |
6181 | cpus_and(tmp, tmp, domainspan); | |
6182 | if (cpus_empty(tmp)) | |
6183 | break; | |
6184 | ||
6185 | nodemask = node_to_cpumask(n); | |
6186 | cpus_and(tmp, tmp, nodemask); | |
6187 | if (cpus_empty(tmp)) | |
6188 | continue; | |
6189 | ||
15f0b676 SV |
6190 | sg = kmalloc_node(sizeof(struct sched_group), |
6191 | GFP_KERNEL, i); | |
9c1cfda2 JH |
6192 | if (!sg) { |
6193 | printk(KERN_WARNING | |
6194 | "Can not alloc domain group for node %d\n", j); | |
51888ca2 | 6195 | goto error; |
9c1cfda2 | 6196 | } |
5517d86b | 6197 | sg->__cpu_power = 0; |
9c1cfda2 | 6198 | sg->cpumask = tmp; |
51888ca2 | 6199 | sg->next = prev->next; |
9c1cfda2 JH |
6200 | cpus_or(covered, covered, tmp); |
6201 | prev->next = sg; | |
6202 | prev = sg; | |
6203 | } | |
9c1cfda2 | 6204 | } |
1da177e4 LT |
6205 | #endif |
6206 | ||
6207 | /* Calculate CPU power for physical packages and nodes */ | |
5c45bf27 | 6208 | #ifdef CONFIG_SCHED_SMT |
1a20ff27 | 6209 | for_each_cpu_mask(i, *cpu_map) { |
dd41f596 IM |
6210 | struct sched_domain *sd = &per_cpu(cpu_domains, i); |
6211 | ||
89c4710e | 6212 | init_sched_groups_power(i, sd); |
5c45bf27 | 6213 | } |
1da177e4 | 6214 | #endif |
1e9f28fa | 6215 | #ifdef CONFIG_SCHED_MC |
5c45bf27 | 6216 | for_each_cpu_mask(i, *cpu_map) { |
dd41f596 IM |
6217 | struct sched_domain *sd = &per_cpu(core_domains, i); |
6218 | ||
89c4710e | 6219 | init_sched_groups_power(i, sd); |
5c45bf27 SS |
6220 | } |
6221 | #endif | |
1e9f28fa | 6222 | |
5c45bf27 | 6223 | for_each_cpu_mask(i, *cpu_map) { |
dd41f596 IM |
6224 | struct sched_domain *sd = &per_cpu(phys_domains, i); |
6225 | ||
89c4710e | 6226 | init_sched_groups_power(i, sd); |
1da177e4 LT |
6227 | } |
6228 | ||
9c1cfda2 | 6229 | #ifdef CONFIG_NUMA |
08069033 SS |
6230 | for (i = 0; i < MAX_NUMNODES; i++) |
6231 | init_numa_sched_groups_power(sched_group_nodes[i]); | |
9c1cfda2 | 6232 | |
6711cab4 SS |
6233 | if (sd_allnodes) { |
6234 | struct sched_group *sg; | |
f712c0c7 | 6235 | |
6711cab4 | 6236 | cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg); |
f712c0c7 SS |
6237 | init_numa_sched_groups_power(sg); |
6238 | } | |
9c1cfda2 JH |
6239 | #endif |
6240 | ||
1da177e4 | 6241 | /* Attach the domains */ |
1a20ff27 | 6242 | for_each_cpu_mask(i, *cpu_map) { |
1da177e4 LT |
6243 | struct sched_domain *sd; |
6244 | #ifdef CONFIG_SCHED_SMT | |
6245 | sd = &per_cpu(cpu_domains, i); | |
1e9f28fa SS |
6246 | #elif defined(CONFIG_SCHED_MC) |
6247 | sd = &per_cpu(core_domains, i); | |
1da177e4 LT |
6248 | #else |
6249 | sd = &per_cpu(phys_domains, i); | |
6250 | #endif | |
6251 | cpu_attach_domain(sd, i); | |
6252 | } | |
51888ca2 SV |
6253 | |
6254 | return 0; | |
6255 | ||
a616058b | 6256 | #ifdef CONFIG_NUMA |
51888ca2 SV |
6257 | error: |
6258 | free_sched_groups(cpu_map); | |
6259 | return -ENOMEM; | |
a616058b | 6260 | #endif |
1da177e4 | 6261 | } |
1a20ff27 DG |
6262 | /* |
6263 | * Set up scheduler domains and groups. Callers must hold the hotplug lock. | |
6264 | */ | |
51888ca2 | 6265 | static int arch_init_sched_domains(const cpumask_t *cpu_map) |
1a20ff27 DG |
6266 | { |
6267 | cpumask_t cpu_default_map; | |
51888ca2 | 6268 | int err; |
1da177e4 | 6269 | |
1a20ff27 DG |
6270 | /* |
6271 | * Setup mask for cpus without special case scheduling requirements. | |
6272 | * For now this just excludes isolated cpus, but could be used to | |
6273 | * exclude other special cases in the future. | |
6274 | */ | |
6275 | cpus_andnot(cpu_default_map, *cpu_map, cpu_isolated_map); | |
6276 | ||
51888ca2 SV |
6277 | err = build_sched_domains(&cpu_default_map); |
6278 | ||
6279 | return err; | |
1a20ff27 DG |
6280 | } |
6281 | ||
6282 | static void arch_destroy_sched_domains(const cpumask_t *cpu_map) | |
1da177e4 | 6283 | { |
51888ca2 | 6284 | free_sched_groups(cpu_map); |
9c1cfda2 | 6285 | } |
1da177e4 | 6286 | |
1a20ff27 DG |
6287 | /* |
6288 | * Detach sched domains from a group of cpus specified in cpu_map | |
6289 | * These cpus will now be attached to the NULL domain | |
6290 | */ | |
858119e1 | 6291 | static void detach_destroy_domains(const cpumask_t *cpu_map) |
1a20ff27 DG |
6292 | { |
6293 | int i; | |
6294 | ||
6295 | for_each_cpu_mask(i, *cpu_map) | |
6296 | cpu_attach_domain(NULL, i); | |
6297 | synchronize_sched(); | |
6298 | arch_destroy_sched_domains(cpu_map); | |
6299 | } | |
6300 | ||
6301 | /* | |
6302 | * Partition sched domains as specified by the cpumasks below. | |
6303 | * This attaches all cpus from the cpumasks to the NULL domain, | |
6304 | * waits for a RCU quiescent period, recalculates sched | |
6305 | * domain information and then attaches them back to the | |
6306 | * correct sched domains | |
6307 | * Call with hotplug lock held | |
6308 | */ | |
51888ca2 | 6309 | int partition_sched_domains(cpumask_t *partition1, cpumask_t *partition2) |
1a20ff27 DG |
6310 | { |
6311 | cpumask_t change_map; | |
51888ca2 | 6312 | int err = 0; |
1a20ff27 DG |
6313 | |
6314 | cpus_and(*partition1, *partition1, cpu_online_map); | |
6315 | cpus_and(*partition2, *partition2, cpu_online_map); | |
6316 | cpus_or(change_map, *partition1, *partition2); | |
6317 | ||
6318 | /* Detach sched domains from all of the affected cpus */ | |
6319 | detach_destroy_domains(&change_map); | |
6320 | if (!cpus_empty(*partition1)) | |
51888ca2 SV |
6321 | err = build_sched_domains(partition1); |
6322 | if (!err && !cpus_empty(*partition2)) | |
6323 | err = build_sched_domains(partition2); | |
6324 | ||
6325 | return err; | |
1a20ff27 DG |
6326 | } |
6327 | ||
5c45bf27 SS |
6328 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
6329 | int arch_reinit_sched_domains(void) | |
6330 | { | |
6331 | int err; | |
6332 | ||
5be9361c | 6333 | mutex_lock(&sched_hotcpu_mutex); |
5c45bf27 SS |
6334 | detach_destroy_domains(&cpu_online_map); |
6335 | err = arch_init_sched_domains(&cpu_online_map); | |
5be9361c | 6336 | mutex_unlock(&sched_hotcpu_mutex); |
5c45bf27 SS |
6337 | |
6338 | return err; | |
6339 | } | |
6340 | ||
6341 | static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt) | |
6342 | { | |
6343 | int ret; | |
6344 | ||
6345 | if (buf[0] != '0' && buf[0] != '1') | |
6346 | return -EINVAL; | |
6347 | ||
6348 | if (smt) | |
6349 | sched_smt_power_savings = (buf[0] == '1'); | |
6350 | else | |
6351 | sched_mc_power_savings = (buf[0] == '1'); | |
6352 | ||
6353 | ret = arch_reinit_sched_domains(); | |
6354 | ||
6355 | return ret ? ret : count; | |
6356 | } | |
6357 | ||
6358 | int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls) | |
6359 | { | |
6360 | int err = 0; | |
48f24c4d | 6361 | |
5c45bf27 SS |
6362 | #ifdef CONFIG_SCHED_SMT |
6363 | if (smt_capable()) | |
6364 | err = sysfs_create_file(&cls->kset.kobj, | |
6365 | &attr_sched_smt_power_savings.attr); | |
6366 | #endif | |
6367 | #ifdef CONFIG_SCHED_MC | |
6368 | if (!err && mc_capable()) | |
6369 | err = sysfs_create_file(&cls->kset.kobj, | |
6370 | &attr_sched_mc_power_savings.attr); | |
6371 | #endif | |
6372 | return err; | |
6373 | } | |
6374 | #endif | |
6375 | ||
6376 | #ifdef CONFIG_SCHED_MC | |
6377 | static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page) | |
6378 | { | |
6379 | return sprintf(page, "%u\n", sched_mc_power_savings); | |
6380 | } | |
48f24c4d IM |
6381 | static ssize_t sched_mc_power_savings_store(struct sys_device *dev, |
6382 | const char *buf, size_t count) | |
5c45bf27 SS |
6383 | { |
6384 | return sched_power_savings_store(buf, count, 0); | |
6385 | } | |
6386 | SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show, | |
6387 | sched_mc_power_savings_store); | |
6388 | #endif | |
6389 | ||
6390 | #ifdef CONFIG_SCHED_SMT | |
6391 | static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page) | |
6392 | { | |
6393 | return sprintf(page, "%u\n", sched_smt_power_savings); | |
6394 | } | |
48f24c4d IM |
6395 | static ssize_t sched_smt_power_savings_store(struct sys_device *dev, |
6396 | const char *buf, size_t count) | |
5c45bf27 SS |
6397 | { |
6398 | return sched_power_savings_store(buf, count, 1); | |
6399 | } | |
6400 | SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show, | |
6401 | sched_smt_power_savings_store); | |
6402 | #endif | |
6403 | ||
1da177e4 LT |
6404 | /* |
6405 | * Force a reinitialization of the sched domains hierarchy. The domains | |
6406 | * and groups cannot be updated in place without racing with the balancing | |
41c7ce9a | 6407 | * code, so we temporarily attach all running cpus to the NULL domain |
1da177e4 LT |
6408 | * which will prevent rebalancing while the sched domains are recalculated. |
6409 | */ | |
6410 | static int update_sched_domains(struct notifier_block *nfb, | |
6411 | unsigned long action, void *hcpu) | |
6412 | { | |
1da177e4 LT |
6413 | switch (action) { |
6414 | case CPU_UP_PREPARE: | |
8bb78442 | 6415 | case CPU_UP_PREPARE_FROZEN: |
1da177e4 | 6416 | case CPU_DOWN_PREPARE: |
8bb78442 | 6417 | case CPU_DOWN_PREPARE_FROZEN: |
1a20ff27 | 6418 | detach_destroy_domains(&cpu_online_map); |
1da177e4 LT |
6419 | return NOTIFY_OK; |
6420 | ||
6421 | case CPU_UP_CANCELED: | |
8bb78442 | 6422 | case CPU_UP_CANCELED_FROZEN: |
1da177e4 | 6423 | case CPU_DOWN_FAILED: |
8bb78442 | 6424 | case CPU_DOWN_FAILED_FROZEN: |
1da177e4 | 6425 | case CPU_ONLINE: |
8bb78442 | 6426 | case CPU_ONLINE_FROZEN: |
1da177e4 | 6427 | case CPU_DEAD: |
8bb78442 | 6428 | case CPU_DEAD_FROZEN: |
1da177e4 LT |
6429 | /* |
6430 | * Fall through and re-initialise the domains. | |
6431 | */ | |
6432 | break; | |
6433 | default: | |
6434 | return NOTIFY_DONE; | |
6435 | } | |
6436 | ||
6437 | /* The hotplug lock is already held by cpu_up/cpu_down */ | |
1a20ff27 | 6438 | arch_init_sched_domains(&cpu_online_map); |
1da177e4 LT |
6439 | |
6440 | return NOTIFY_OK; | |
6441 | } | |
1da177e4 LT |
6442 | |
6443 | void __init sched_init_smp(void) | |
6444 | { | |
5c1e1767 NP |
6445 | cpumask_t non_isolated_cpus; |
6446 | ||
5be9361c | 6447 | mutex_lock(&sched_hotcpu_mutex); |
1a20ff27 | 6448 | arch_init_sched_domains(&cpu_online_map); |
e5e5673f | 6449 | cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map); |
5c1e1767 NP |
6450 | if (cpus_empty(non_isolated_cpus)) |
6451 | cpu_set(smp_processor_id(), non_isolated_cpus); | |
5be9361c | 6452 | mutex_unlock(&sched_hotcpu_mutex); |
1da177e4 LT |
6453 | /* XXX: Theoretical race here - CPU may be hotplugged now */ |
6454 | hotcpu_notifier(update_sched_domains, 0); | |
5c1e1767 | 6455 | |
e692ab53 NP |
6456 | init_sched_domain_sysctl(); |
6457 | ||
5c1e1767 NP |
6458 | /* Move init over to a non-isolated CPU */ |
6459 | if (set_cpus_allowed(current, non_isolated_cpus) < 0) | |
6460 | BUG(); | |
dd41f596 | 6461 | sched_init_granularity(); |
1da177e4 LT |
6462 | } |
6463 | #else | |
6464 | void __init sched_init_smp(void) | |
6465 | { | |
dd41f596 | 6466 | sched_init_granularity(); |
1da177e4 LT |
6467 | } |
6468 | #endif /* CONFIG_SMP */ | |
6469 | ||
6470 | int in_sched_functions(unsigned long addr) | |
6471 | { | |
6472 | /* Linker adds these: start and end of __sched functions */ | |
6473 | extern char __sched_text_start[], __sched_text_end[]; | |
48f24c4d | 6474 | |
1da177e4 LT |
6475 | return in_lock_functions(addr) || |
6476 | (addr >= (unsigned long)__sched_text_start | |
6477 | && addr < (unsigned long)__sched_text_end); | |
6478 | } | |
6479 | ||
dd41f596 IM |
6480 | static inline void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq) |
6481 | { | |
6482 | cfs_rq->tasks_timeline = RB_ROOT; | |
6483 | cfs_rq->fair_clock = 1; | |
6484 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
6485 | cfs_rq->rq = rq; | |
6486 | #endif | |
6487 | } | |
6488 | ||
1da177e4 LT |
6489 | void __init sched_init(void) |
6490 | { | |
dd41f596 | 6491 | u64 now = sched_clock(); |
476f3534 | 6492 | int highest_cpu = 0; |
dd41f596 IM |
6493 | int i, j; |
6494 | ||
6495 | /* | |
6496 | * Link up the scheduling class hierarchy: | |
6497 | */ | |
6498 | rt_sched_class.next = &fair_sched_class; | |
6499 | fair_sched_class.next = &idle_sched_class; | |
6500 | idle_sched_class.next = NULL; | |
1da177e4 | 6501 | |
0a945022 | 6502 | for_each_possible_cpu(i) { |
dd41f596 | 6503 | struct rt_prio_array *array; |
70b97a7f | 6504 | struct rq *rq; |
1da177e4 LT |
6505 | |
6506 | rq = cpu_rq(i); | |
6507 | spin_lock_init(&rq->lock); | |
fcb99371 | 6508 | lockdep_set_class(&rq->lock, &rq->rq_lock_key); |
7897986b | 6509 | rq->nr_running = 0; |
dd41f596 IM |
6510 | rq->clock = 1; |
6511 | init_cfs_rq(&rq->cfs, rq); | |
6512 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
6513 | INIT_LIST_HEAD(&rq->leaf_cfs_rq_list); | |
6514 | list_add(&rq->cfs.leaf_cfs_rq_list, &rq->leaf_cfs_rq_list); | |
6515 | #endif | |
6516 | rq->ls.load_update_last = now; | |
6517 | rq->ls.load_update_start = now; | |
1da177e4 | 6518 | |
dd41f596 IM |
6519 | for (j = 0; j < CPU_LOAD_IDX_MAX; j++) |
6520 | rq->cpu_load[j] = 0; | |
1da177e4 | 6521 | #ifdef CONFIG_SMP |
41c7ce9a | 6522 | rq->sd = NULL; |
1da177e4 | 6523 | rq->active_balance = 0; |
dd41f596 | 6524 | rq->next_balance = jiffies; |
1da177e4 | 6525 | rq->push_cpu = 0; |
0a2966b4 | 6526 | rq->cpu = i; |
1da177e4 LT |
6527 | rq->migration_thread = NULL; |
6528 | INIT_LIST_HEAD(&rq->migration_queue); | |
6529 | #endif | |
6530 | atomic_set(&rq->nr_iowait, 0); | |
6531 | ||
dd41f596 IM |
6532 | array = &rq->rt.active; |
6533 | for (j = 0; j < MAX_RT_PRIO; j++) { | |
6534 | INIT_LIST_HEAD(array->queue + j); | |
6535 | __clear_bit(j, array->bitmap); | |
1da177e4 | 6536 | } |
476f3534 | 6537 | highest_cpu = i; |
dd41f596 IM |
6538 | /* delimiter for bitsearch: */ |
6539 | __set_bit(MAX_RT_PRIO, array->bitmap); | |
1da177e4 LT |
6540 | } |
6541 | ||
2dd73a4f | 6542 | set_load_weight(&init_task); |
b50f60ce | 6543 | |
e107be36 AK |
6544 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
6545 | INIT_HLIST_HEAD(&init_task.preempt_notifiers); | |
6546 | #endif | |
6547 | ||
c9819f45 | 6548 | #ifdef CONFIG_SMP |
476f3534 | 6549 | nr_cpu_ids = highest_cpu + 1; |
c9819f45 CL |
6550 | open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL); |
6551 | #endif | |
6552 | ||
b50f60ce HC |
6553 | #ifdef CONFIG_RT_MUTEXES |
6554 | plist_head_init(&init_task.pi_waiters, &init_task.pi_lock); | |
6555 | #endif | |
6556 | ||
1da177e4 LT |
6557 | /* |
6558 | * The boot idle thread does lazy MMU switching as well: | |
6559 | */ | |
6560 | atomic_inc(&init_mm.mm_count); | |
6561 | enter_lazy_tlb(&init_mm, current); | |
6562 | ||
6563 | /* | |
6564 | * Make us the idle thread. Technically, schedule() should not be | |
6565 | * called from this thread, however somewhere below it might be, | |
6566 | * but because we are the idle thread, we just pick up running again | |
6567 | * when this runqueue becomes "idle". | |
6568 | */ | |
6569 | init_idle(current, smp_processor_id()); | |
dd41f596 IM |
6570 | /* |
6571 | * During early bootup we pretend to be a normal task: | |
6572 | */ | |
6573 | current->sched_class = &fair_sched_class; | |
1da177e4 LT |
6574 | } |
6575 | ||
6576 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP | |
6577 | void __might_sleep(char *file, int line) | |
6578 | { | |
48f24c4d | 6579 | #ifdef in_atomic |
1da177e4 LT |
6580 | static unsigned long prev_jiffy; /* ratelimiting */ |
6581 | ||
6582 | if ((in_atomic() || irqs_disabled()) && | |
6583 | system_state == SYSTEM_RUNNING && !oops_in_progress) { | |
6584 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) | |
6585 | return; | |
6586 | prev_jiffy = jiffies; | |
91368d73 | 6587 | printk(KERN_ERR "BUG: sleeping function called from invalid" |
1da177e4 LT |
6588 | " context at %s:%d\n", file, line); |
6589 | printk("in_atomic():%d, irqs_disabled():%d\n", | |
6590 | in_atomic(), irqs_disabled()); | |
a4c410f0 | 6591 | debug_show_held_locks(current); |
3117df04 IM |
6592 | if (irqs_disabled()) |
6593 | print_irqtrace_events(current); | |
1da177e4 LT |
6594 | dump_stack(); |
6595 | } | |
6596 | #endif | |
6597 | } | |
6598 | EXPORT_SYMBOL(__might_sleep); | |
6599 | #endif | |
6600 | ||
6601 | #ifdef CONFIG_MAGIC_SYSRQ | |
6602 | void normalize_rt_tasks(void) | |
6603 | { | |
a0f98a1c | 6604 | struct task_struct *g, *p; |
1da177e4 | 6605 | unsigned long flags; |
70b97a7f | 6606 | struct rq *rq; |
dd41f596 | 6607 | int on_rq; |
1da177e4 LT |
6608 | |
6609 | read_lock_irq(&tasklist_lock); | |
a0f98a1c | 6610 | do_each_thread(g, p) { |
dd41f596 IM |
6611 | p->se.fair_key = 0; |
6612 | p->se.wait_runtime = 0; | |
6cfb0d5d | 6613 | p->se.exec_start = 0; |
dd41f596 | 6614 | p->se.wait_start_fair = 0; |
6cfb0d5d IM |
6615 | p->se.sleep_start_fair = 0; |
6616 | #ifdef CONFIG_SCHEDSTATS | |
dd41f596 | 6617 | p->se.wait_start = 0; |
dd41f596 | 6618 | p->se.sleep_start = 0; |
dd41f596 | 6619 | p->se.block_start = 0; |
6cfb0d5d | 6620 | #endif |
dd41f596 IM |
6621 | task_rq(p)->cfs.fair_clock = 0; |
6622 | task_rq(p)->clock = 0; | |
6623 | ||
6624 | if (!rt_task(p)) { | |
6625 | /* | |
6626 | * Renice negative nice level userspace | |
6627 | * tasks back to 0: | |
6628 | */ | |
6629 | if (TASK_NICE(p) < 0 && p->mm) | |
6630 | set_user_nice(p, 0); | |
1da177e4 | 6631 | continue; |
dd41f596 | 6632 | } |
1da177e4 | 6633 | |
b29739f9 IM |
6634 | spin_lock_irqsave(&p->pi_lock, flags); |
6635 | rq = __task_rq_lock(p); | |
dd41f596 IM |
6636 | #ifdef CONFIG_SMP |
6637 | /* | |
6638 | * Do not touch the migration thread: | |
6639 | */ | |
6640 | if (p == rq->migration_thread) | |
6641 | goto out_unlock; | |
6642 | #endif | |
1da177e4 | 6643 | |
dd41f596 | 6644 | on_rq = p->se.on_rq; |
a8e504d2 IM |
6645 | if (on_rq) { |
6646 | update_rq_clock(task_rq(p)); | |
6647 | deactivate_task(task_rq(p), p, 0, task_rq(p)->clock); | |
6648 | } | |
dd41f596 IM |
6649 | __setscheduler(rq, p, SCHED_NORMAL, 0); |
6650 | if (on_rq) { | |
6651 | activate_task(task_rq(p), p, 0); | |
1da177e4 LT |
6652 | resched_task(rq->curr); |
6653 | } | |
dd41f596 IM |
6654 | #ifdef CONFIG_SMP |
6655 | out_unlock: | |
6656 | #endif | |
b29739f9 IM |
6657 | __task_rq_unlock(rq); |
6658 | spin_unlock_irqrestore(&p->pi_lock, flags); | |
a0f98a1c IM |
6659 | } while_each_thread(g, p); |
6660 | ||
1da177e4 LT |
6661 | read_unlock_irq(&tasklist_lock); |
6662 | } | |
6663 | ||
6664 | #endif /* CONFIG_MAGIC_SYSRQ */ | |
1df5c10a LT |
6665 | |
6666 | #ifdef CONFIG_IA64 | |
6667 | /* | |
6668 | * These functions are only useful for the IA64 MCA handling. | |
6669 | * | |
6670 | * They can only be called when the whole system has been | |
6671 | * stopped - every CPU needs to be quiescent, and no scheduling | |
6672 | * activity can take place. Using them for anything else would | |
6673 | * be a serious bug, and as a result, they aren't even visible | |
6674 | * under any other configuration. | |
6675 | */ | |
6676 | ||
6677 | /** | |
6678 | * curr_task - return the current task for a given cpu. | |
6679 | * @cpu: the processor in question. | |
6680 | * | |
6681 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! | |
6682 | */ | |
36c8b586 | 6683 | struct task_struct *curr_task(int cpu) |
1df5c10a LT |
6684 | { |
6685 | return cpu_curr(cpu); | |
6686 | } | |
6687 | ||
6688 | /** | |
6689 | * set_curr_task - set the current task for a given cpu. | |
6690 | * @cpu: the processor in question. | |
6691 | * @p: the task pointer to set. | |
6692 | * | |
6693 | * Description: This function must only be used when non-maskable interrupts | |
6694 | * are serviced on a separate stack. It allows the architecture to switch the | |
6695 | * notion of the current task on a cpu in a non-blocking manner. This function | |
6696 | * must be called with all CPU's synchronized, and interrupts disabled, the | |
6697 | * and caller must save the original value of the current task (see | |
6698 | * curr_task() above) and restore that value before reenabling interrupts and | |
6699 | * re-starting the system. | |
6700 | * | |
6701 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! | |
6702 | */ | |
36c8b586 | 6703 | void set_curr_task(int cpu, struct task_struct *p) |
1df5c10a LT |
6704 | { |
6705 | cpu_curr(cpu) = p; | |
6706 | } | |
6707 | ||
6708 | #endif |