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