]>
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> | |
33 | #include <linux/security.h> | |
34 | #include <linux/notifier.h> | |
35 | #include <linux/profile.h> | |
36 | #include <linux/suspend.h> | |
37 | #include <linux/blkdev.h> | |
38 | #include <linux/delay.h> | |
39 | #include <linux/smp.h> | |
40 | #include <linux/threads.h> | |
41 | #include <linux/timer.h> | |
42 | #include <linux/rcupdate.h> | |
43 | #include <linux/cpu.h> | |
44 | #include <linux/cpuset.h> | |
45 | #include <linux/percpu.h> | |
46 | #include <linux/kthread.h> | |
47 | #include <linux/seq_file.h> | |
48 | #include <linux/syscalls.h> | |
49 | #include <linux/times.h> | |
50 | #include <linux/acct.h> | |
51 | #include <asm/tlb.h> | |
52 | ||
53 | #include <asm/unistd.h> | |
54 | ||
55 | /* | |
56 | * Convert user-nice values [ -20 ... 0 ... 19 ] | |
57 | * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ], | |
58 | * and back. | |
59 | */ | |
60 | #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20) | |
61 | #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20) | |
62 | #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio) | |
63 | ||
64 | /* | |
65 | * 'User priority' is the nice value converted to something we | |
66 | * can work with better when scaling various scheduler parameters, | |
67 | * it's a [ 0 ... 39 ] range. | |
68 | */ | |
69 | #define USER_PRIO(p) ((p)-MAX_RT_PRIO) | |
70 | #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio) | |
71 | #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO)) | |
72 | ||
73 | /* | |
74 | * Some helpers for converting nanosecond timing to jiffy resolution | |
75 | */ | |
76 | #define NS_TO_JIFFIES(TIME) ((TIME) / (1000000000 / HZ)) | |
77 | #define JIFFIES_TO_NS(TIME) ((TIME) * (1000000000 / HZ)) | |
78 | ||
79 | /* | |
80 | * These are the 'tuning knobs' of the scheduler: | |
81 | * | |
82 | * Minimum timeslice is 5 msecs (or 1 jiffy, whichever is larger), | |
83 | * default timeslice is 100 msecs, maximum timeslice is 800 msecs. | |
84 | * Timeslices get refilled after they expire. | |
85 | */ | |
86 | #define MIN_TIMESLICE max(5 * HZ / 1000, 1) | |
87 | #define DEF_TIMESLICE (100 * HZ / 1000) | |
88 | #define ON_RUNQUEUE_WEIGHT 30 | |
89 | #define CHILD_PENALTY 95 | |
90 | #define PARENT_PENALTY 100 | |
91 | #define EXIT_WEIGHT 3 | |
92 | #define PRIO_BONUS_RATIO 25 | |
93 | #define MAX_BONUS (MAX_USER_PRIO * PRIO_BONUS_RATIO / 100) | |
94 | #define INTERACTIVE_DELTA 2 | |
95 | #define MAX_SLEEP_AVG (DEF_TIMESLICE * MAX_BONUS) | |
96 | #define STARVATION_LIMIT (MAX_SLEEP_AVG) | |
97 | #define NS_MAX_SLEEP_AVG (JIFFIES_TO_NS(MAX_SLEEP_AVG)) | |
98 | ||
99 | /* | |
100 | * If a task is 'interactive' then we reinsert it in the active | |
101 | * array after it has expired its current timeslice. (it will not | |
102 | * continue to run immediately, it will still roundrobin with | |
103 | * other interactive tasks.) | |
104 | * | |
105 | * This part scales the interactivity limit depending on niceness. | |
106 | * | |
107 | * We scale it linearly, offset by the INTERACTIVE_DELTA delta. | |
108 | * Here are a few examples of different nice levels: | |
109 | * | |
110 | * TASK_INTERACTIVE(-20): [1,1,1,1,1,1,1,1,1,0,0] | |
111 | * TASK_INTERACTIVE(-10): [1,1,1,1,1,1,1,0,0,0,0] | |
112 | * TASK_INTERACTIVE( 0): [1,1,1,1,0,0,0,0,0,0,0] | |
113 | * TASK_INTERACTIVE( 10): [1,1,0,0,0,0,0,0,0,0,0] | |
114 | * TASK_INTERACTIVE( 19): [0,0,0,0,0,0,0,0,0,0,0] | |
115 | * | |
116 | * (the X axis represents the possible -5 ... 0 ... +5 dynamic | |
117 | * priority range a task can explore, a value of '1' means the | |
118 | * task is rated interactive.) | |
119 | * | |
120 | * Ie. nice +19 tasks can never get 'interactive' enough to be | |
121 | * reinserted into the active array. And only heavily CPU-hog nice -20 | |
122 | * tasks will be expired. Default nice 0 tasks are somewhere between, | |
123 | * it takes some effort for them to get interactive, but it's not | |
124 | * too hard. | |
125 | */ | |
126 | ||
127 | #define CURRENT_BONUS(p) \ | |
128 | (NS_TO_JIFFIES((p)->sleep_avg) * MAX_BONUS / \ | |
129 | MAX_SLEEP_AVG) | |
130 | ||
131 | #define GRANULARITY (10 * HZ / 1000 ? : 1) | |
132 | ||
133 | #ifdef CONFIG_SMP | |
134 | #define TIMESLICE_GRANULARITY(p) (GRANULARITY * \ | |
135 | (1 << (((MAX_BONUS - CURRENT_BONUS(p)) ? : 1) - 1)) * \ | |
136 | num_online_cpus()) | |
137 | #else | |
138 | #define TIMESLICE_GRANULARITY(p) (GRANULARITY * \ | |
139 | (1 << (((MAX_BONUS - CURRENT_BONUS(p)) ? : 1) - 1))) | |
140 | #endif | |
141 | ||
142 | #define SCALE(v1,v1_max,v2_max) \ | |
143 | (v1) * (v2_max) / (v1_max) | |
144 | ||
145 | #define DELTA(p) \ | |
146 | (SCALE(TASK_NICE(p), 40, MAX_BONUS) + INTERACTIVE_DELTA) | |
147 | ||
148 | #define TASK_INTERACTIVE(p) \ | |
149 | ((p)->prio <= (p)->static_prio - DELTA(p)) | |
150 | ||
151 | #define INTERACTIVE_SLEEP(p) \ | |
152 | (JIFFIES_TO_NS(MAX_SLEEP_AVG * \ | |
153 | (MAX_BONUS / 2 + DELTA((p)) + 1) / MAX_BONUS - 1)) | |
154 | ||
155 | #define TASK_PREEMPTS_CURR(p, rq) \ | |
156 | ((p)->prio < (rq)->curr->prio) | |
157 | ||
158 | /* | |
159 | * task_timeslice() scales user-nice values [ -20 ... 0 ... 19 ] | |
160 | * to time slice values: [800ms ... 100ms ... 5ms] | |
161 | * | |
162 | * The higher a thread's priority, the bigger timeslices | |
163 | * it gets during one round of execution. But even the lowest | |
164 | * priority thread gets MIN_TIMESLICE worth of execution time. | |
165 | */ | |
166 | ||
167 | #define SCALE_PRIO(x, prio) \ | |
168 | max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO/2), MIN_TIMESLICE) | |
169 | ||
48c08d3f | 170 | static unsigned int task_timeslice(task_t *p) |
1da177e4 LT |
171 | { |
172 | if (p->static_prio < NICE_TO_PRIO(0)) | |
173 | return SCALE_PRIO(DEF_TIMESLICE*4, p->static_prio); | |
174 | else | |
175 | return SCALE_PRIO(DEF_TIMESLICE, p->static_prio); | |
176 | } | |
177 | #define task_hot(p, now, sd) ((long long) ((now) - (p)->last_ran) \ | |
178 | < (long long) (sd)->cache_hot_time) | |
179 | ||
e56d0903 IM |
180 | void __put_task_struct_cb(struct rcu_head *rhp) |
181 | { | |
182 | __put_task_struct(container_of(rhp, struct task_struct, rcu)); | |
183 | } | |
184 | ||
185 | EXPORT_SYMBOL_GPL(__put_task_struct_cb); | |
186 | ||
1da177e4 LT |
187 | /* |
188 | * These are the runqueue data structures: | |
189 | */ | |
190 | ||
191 | #define BITMAP_SIZE ((((MAX_PRIO+1+7)/8)+sizeof(long)-1)/sizeof(long)) | |
192 | ||
193 | typedef struct runqueue runqueue_t; | |
194 | ||
195 | struct prio_array { | |
196 | unsigned int nr_active; | |
197 | unsigned long bitmap[BITMAP_SIZE]; | |
198 | struct list_head queue[MAX_PRIO]; | |
199 | }; | |
200 | ||
201 | /* | |
202 | * This is the main, per-CPU runqueue data structure. | |
203 | * | |
204 | * Locking rule: those places that want to lock multiple runqueues | |
205 | * (such as the load balancing or the thread migration code), lock | |
206 | * acquire operations must be ordered by ascending &runqueue. | |
207 | */ | |
208 | struct runqueue { | |
209 | spinlock_t lock; | |
210 | ||
211 | /* | |
212 | * nr_running and cpu_load should be in the same cacheline because | |
213 | * remote CPUs use both these fields when doing load calculation. | |
214 | */ | |
215 | unsigned long nr_running; | |
216 | #ifdef CONFIG_SMP | |
b910472d | 217 | unsigned long prio_bias; |
7897986b | 218 | unsigned long cpu_load[3]; |
1da177e4 LT |
219 | #endif |
220 | unsigned long long nr_switches; | |
221 | ||
222 | /* | |
223 | * This is part of a global counter where only the total sum | |
224 | * over all CPUs matters. A task can increase this counter on | |
225 | * one CPU and if it got migrated afterwards it may decrease | |
226 | * it on another CPU. Always updated under the runqueue lock: | |
227 | */ | |
228 | unsigned long nr_uninterruptible; | |
229 | ||
230 | unsigned long expired_timestamp; | |
231 | unsigned long long timestamp_last_tick; | |
232 | task_t *curr, *idle; | |
233 | struct mm_struct *prev_mm; | |
234 | prio_array_t *active, *expired, arrays[2]; | |
235 | int best_expired_prio; | |
236 | atomic_t nr_iowait; | |
237 | ||
238 | #ifdef CONFIG_SMP | |
239 | struct sched_domain *sd; | |
240 | ||
241 | /* For active balancing */ | |
242 | int active_balance; | |
243 | int push_cpu; | |
244 | ||
245 | task_t *migration_thread; | |
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 | |
268 | }; | |
269 | ||
270 | static DEFINE_PER_CPU(struct runqueue, runqueues); | |
271 | ||
674311d5 NP |
272 | /* |
273 | * The domain tree (rq->sd) is protected by RCU's quiescent state transition. | |
1a20ff27 | 274 | * See detach_destroy_domains: synchronize_sched for details. |
674311d5 NP |
275 | * |
276 | * The domain tree of any CPU may only be accessed from within | |
277 | * preempt-disabled sections. | |
278 | */ | |
1da177e4 | 279 | #define for_each_domain(cpu, domain) \ |
674311d5 | 280 | for (domain = rcu_dereference(cpu_rq(cpu)->sd); domain; domain = domain->parent) |
1da177e4 LT |
281 | |
282 | #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu))) | |
283 | #define this_rq() (&__get_cpu_var(runqueues)) | |
284 | #define task_rq(p) cpu_rq(task_cpu(p)) | |
285 | #define cpu_curr(cpu) (cpu_rq(cpu)->curr) | |
286 | ||
1da177e4 | 287 | #ifndef prepare_arch_switch |
4866cde0 NP |
288 | # define prepare_arch_switch(next) do { } while (0) |
289 | #endif | |
290 | #ifndef finish_arch_switch | |
291 | # define finish_arch_switch(prev) do { } while (0) | |
292 | #endif | |
293 | ||
294 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW | |
295 | static inline int task_running(runqueue_t *rq, task_t *p) | |
296 | { | |
297 | return rq->curr == p; | |
298 | } | |
299 | ||
300 | static inline void prepare_lock_switch(runqueue_t *rq, task_t *next) | |
301 | { | |
302 | } | |
303 | ||
304 | static inline void finish_lock_switch(runqueue_t *rq, task_t *prev) | |
305 | { | |
da04c035 IM |
306 | #ifdef CONFIG_DEBUG_SPINLOCK |
307 | /* this is a valid case when another task releases the spinlock */ | |
308 | rq->lock.owner = current; | |
309 | #endif | |
4866cde0 NP |
310 | spin_unlock_irq(&rq->lock); |
311 | } | |
312 | ||
313 | #else /* __ARCH_WANT_UNLOCKED_CTXSW */ | |
314 | static inline int task_running(runqueue_t *rq, task_t *p) | |
315 | { | |
316 | #ifdef CONFIG_SMP | |
317 | return p->oncpu; | |
318 | #else | |
319 | return rq->curr == p; | |
320 | #endif | |
321 | } | |
322 | ||
323 | static inline void prepare_lock_switch(runqueue_t *rq, task_t *next) | |
324 | { | |
325 | #ifdef CONFIG_SMP | |
326 | /* | |
327 | * We can optimise this out completely for !SMP, because the | |
328 | * SMP rebalancing from interrupt is the only thing that cares | |
329 | * here. | |
330 | */ | |
331 | next->oncpu = 1; | |
332 | #endif | |
333 | #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW | |
334 | spin_unlock_irq(&rq->lock); | |
335 | #else | |
336 | spin_unlock(&rq->lock); | |
337 | #endif | |
338 | } | |
339 | ||
340 | static inline void finish_lock_switch(runqueue_t *rq, task_t *prev) | |
341 | { | |
342 | #ifdef CONFIG_SMP | |
343 | /* | |
344 | * After ->oncpu is cleared, the task can be moved to a different CPU. | |
345 | * We must ensure this doesn't happen until the switch is completely | |
346 | * finished. | |
347 | */ | |
348 | smp_wmb(); | |
349 | prev->oncpu = 0; | |
350 | #endif | |
351 | #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW | |
352 | local_irq_enable(); | |
1da177e4 | 353 | #endif |
4866cde0 NP |
354 | } |
355 | #endif /* __ARCH_WANT_UNLOCKED_CTXSW */ | |
1da177e4 LT |
356 | |
357 | /* | |
358 | * task_rq_lock - lock the runqueue a given task resides on and disable | |
359 | * interrupts. Note the ordering: we can safely lookup the task_rq without | |
360 | * explicitly disabling preemption. | |
361 | */ | |
362 | static inline runqueue_t *task_rq_lock(task_t *p, unsigned long *flags) | |
363 | __acquires(rq->lock) | |
364 | { | |
365 | struct runqueue *rq; | |
366 | ||
367 | repeat_lock_task: | |
368 | local_irq_save(*flags); | |
369 | rq = task_rq(p); | |
370 | spin_lock(&rq->lock); | |
371 | if (unlikely(rq != task_rq(p))) { | |
372 | spin_unlock_irqrestore(&rq->lock, *flags); | |
373 | goto repeat_lock_task; | |
374 | } | |
375 | return rq; | |
376 | } | |
377 | ||
378 | static inline void task_rq_unlock(runqueue_t *rq, unsigned long *flags) | |
379 | __releases(rq->lock) | |
380 | { | |
381 | spin_unlock_irqrestore(&rq->lock, *flags); | |
382 | } | |
383 | ||
384 | #ifdef CONFIG_SCHEDSTATS | |
385 | /* | |
386 | * bump this up when changing the output format or the meaning of an existing | |
387 | * format, so that tools can adapt (or abort) | |
388 | */ | |
68767a0a | 389 | #define SCHEDSTAT_VERSION 12 |
1da177e4 LT |
390 | |
391 | static int show_schedstat(struct seq_file *seq, void *v) | |
392 | { | |
393 | int cpu; | |
394 | ||
395 | seq_printf(seq, "version %d\n", SCHEDSTAT_VERSION); | |
396 | seq_printf(seq, "timestamp %lu\n", jiffies); | |
397 | for_each_online_cpu(cpu) { | |
398 | runqueue_t *rq = cpu_rq(cpu); | |
399 | #ifdef CONFIG_SMP | |
400 | struct sched_domain *sd; | |
401 | int dcnt = 0; | |
402 | #endif | |
403 | ||
404 | /* runqueue-specific stats */ | |
405 | seq_printf(seq, | |
406 | "cpu%d %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu", | |
407 | cpu, rq->yld_both_empty, | |
408 | rq->yld_act_empty, rq->yld_exp_empty, rq->yld_cnt, | |
409 | rq->sched_switch, rq->sched_cnt, rq->sched_goidle, | |
410 | rq->ttwu_cnt, rq->ttwu_local, | |
411 | rq->rq_sched_info.cpu_time, | |
412 | rq->rq_sched_info.run_delay, rq->rq_sched_info.pcnt); | |
413 | ||
414 | seq_printf(seq, "\n"); | |
415 | ||
416 | #ifdef CONFIG_SMP | |
417 | /* domain-specific stats */ | |
674311d5 | 418 | preempt_disable(); |
1da177e4 LT |
419 | for_each_domain(cpu, sd) { |
420 | enum idle_type itype; | |
421 | char mask_str[NR_CPUS]; | |
422 | ||
423 | cpumask_scnprintf(mask_str, NR_CPUS, sd->span); | |
424 | seq_printf(seq, "domain%d %s", dcnt++, mask_str); | |
425 | for (itype = SCHED_IDLE; itype < MAX_IDLE_TYPES; | |
426 | itype++) { | |
427 | seq_printf(seq, " %lu %lu %lu %lu %lu %lu %lu %lu", | |
428 | sd->lb_cnt[itype], | |
429 | sd->lb_balanced[itype], | |
430 | sd->lb_failed[itype], | |
431 | sd->lb_imbalance[itype], | |
432 | sd->lb_gained[itype], | |
433 | sd->lb_hot_gained[itype], | |
434 | sd->lb_nobusyq[itype], | |
435 | sd->lb_nobusyg[itype]); | |
436 | } | |
68767a0a | 437 | seq_printf(seq, " %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n", |
1da177e4 | 438 | sd->alb_cnt, sd->alb_failed, sd->alb_pushed, |
68767a0a NP |
439 | sd->sbe_cnt, sd->sbe_balanced, sd->sbe_pushed, |
440 | sd->sbf_cnt, sd->sbf_balanced, sd->sbf_pushed, | |
1da177e4 LT |
441 | sd->ttwu_wake_remote, sd->ttwu_move_affine, sd->ttwu_move_balance); |
442 | } | |
674311d5 | 443 | preempt_enable(); |
1da177e4 LT |
444 | #endif |
445 | } | |
446 | return 0; | |
447 | } | |
448 | ||
449 | static int schedstat_open(struct inode *inode, struct file *file) | |
450 | { | |
451 | unsigned int size = PAGE_SIZE * (1 + num_online_cpus() / 32); | |
452 | char *buf = kmalloc(size, GFP_KERNEL); | |
453 | struct seq_file *m; | |
454 | int res; | |
455 | ||
456 | if (!buf) | |
457 | return -ENOMEM; | |
458 | res = single_open(file, show_schedstat, NULL); | |
459 | if (!res) { | |
460 | m = file->private_data; | |
461 | m->buf = buf; | |
462 | m->size = size; | |
463 | } else | |
464 | kfree(buf); | |
465 | return res; | |
466 | } | |
467 | ||
468 | struct file_operations proc_schedstat_operations = { | |
469 | .open = schedstat_open, | |
470 | .read = seq_read, | |
471 | .llseek = seq_lseek, | |
472 | .release = single_release, | |
473 | }; | |
474 | ||
475 | # define schedstat_inc(rq, field) do { (rq)->field++; } while (0) | |
476 | # define schedstat_add(rq, field, amt) do { (rq)->field += (amt); } while (0) | |
477 | #else /* !CONFIG_SCHEDSTATS */ | |
478 | # define schedstat_inc(rq, field) do { } while (0) | |
479 | # define schedstat_add(rq, field, amt) do { } while (0) | |
480 | #endif | |
481 | ||
482 | /* | |
483 | * rq_lock - lock a given runqueue and disable interrupts. | |
484 | */ | |
485 | static inline runqueue_t *this_rq_lock(void) | |
486 | __acquires(rq->lock) | |
487 | { | |
488 | runqueue_t *rq; | |
489 | ||
490 | local_irq_disable(); | |
491 | rq = this_rq(); | |
492 | spin_lock(&rq->lock); | |
493 | ||
494 | return rq; | |
495 | } | |
496 | ||
1da177e4 LT |
497 | #ifdef CONFIG_SCHEDSTATS |
498 | /* | |
499 | * Called when a process is dequeued from the active array and given | |
500 | * the cpu. We should note that with the exception of interactive | |
501 | * tasks, the expired queue will become the active queue after the active | |
502 | * queue is empty, without explicitly dequeuing and requeuing tasks in the | |
503 | * expired queue. (Interactive tasks may be requeued directly to the | |
504 | * active queue, thus delaying tasks in the expired queue from running; | |
505 | * see scheduler_tick()). | |
506 | * | |
507 | * This function is only called from sched_info_arrive(), rather than | |
508 | * dequeue_task(). Even though a task may be queued and dequeued multiple | |
509 | * times as it is shuffled about, we're really interested in knowing how | |
510 | * long it was from the *first* time it was queued to the time that it | |
511 | * finally hit a cpu. | |
512 | */ | |
513 | static inline void sched_info_dequeued(task_t *t) | |
514 | { | |
515 | t->sched_info.last_queued = 0; | |
516 | } | |
517 | ||
518 | /* | |
519 | * Called when a task finally hits the cpu. We can now calculate how | |
520 | * long it was waiting to run. We also note when it began so that we | |
521 | * can keep stats on how long its timeslice is. | |
522 | */ | |
523 | static inline void sched_info_arrive(task_t *t) | |
524 | { | |
525 | unsigned long now = jiffies, diff = 0; | |
526 | struct runqueue *rq = task_rq(t); | |
527 | ||
528 | if (t->sched_info.last_queued) | |
529 | diff = now - t->sched_info.last_queued; | |
530 | sched_info_dequeued(t); | |
531 | t->sched_info.run_delay += diff; | |
532 | t->sched_info.last_arrival = now; | |
533 | t->sched_info.pcnt++; | |
534 | ||
535 | if (!rq) | |
536 | return; | |
537 | ||
538 | rq->rq_sched_info.run_delay += diff; | |
539 | rq->rq_sched_info.pcnt++; | |
540 | } | |
541 | ||
542 | /* | |
543 | * Called when a process is queued into either the active or expired | |
544 | * array. The time is noted and later used to determine how long we | |
545 | * had to wait for us to reach the cpu. Since the expired queue will | |
546 | * become the active queue after active queue is empty, without dequeuing | |
547 | * and requeuing any tasks, we are interested in queuing to either. It | |
548 | * is unusual but not impossible for tasks to be dequeued and immediately | |
549 | * requeued in the same or another array: this can happen in sched_yield(), | |
550 | * set_user_nice(), and even load_balance() as it moves tasks from runqueue | |
551 | * to runqueue. | |
552 | * | |
553 | * This function is only called from enqueue_task(), but also only updates | |
554 | * the timestamp if it is already not set. It's assumed that | |
555 | * sched_info_dequeued() will clear that stamp when appropriate. | |
556 | */ | |
557 | static inline void sched_info_queued(task_t *t) | |
558 | { | |
559 | if (!t->sched_info.last_queued) | |
560 | t->sched_info.last_queued = jiffies; | |
561 | } | |
562 | ||
563 | /* | |
564 | * Called when a process ceases being the active-running process, either | |
565 | * voluntarily or involuntarily. Now we can calculate how long we ran. | |
566 | */ | |
567 | static inline void sched_info_depart(task_t *t) | |
568 | { | |
569 | struct runqueue *rq = task_rq(t); | |
570 | unsigned long diff = jiffies - t->sched_info.last_arrival; | |
571 | ||
572 | t->sched_info.cpu_time += diff; | |
573 | ||
574 | if (rq) | |
575 | rq->rq_sched_info.cpu_time += diff; | |
576 | } | |
577 | ||
578 | /* | |
579 | * Called when tasks are switched involuntarily due, typically, to expiring | |
580 | * their time slice. (This may also be called when switching to or from | |
581 | * the idle task.) We are only called when prev != next. | |
582 | */ | |
583 | static inline void sched_info_switch(task_t *prev, task_t *next) | |
584 | { | |
585 | struct runqueue *rq = task_rq(prev); | |
586 | ||
587 | /* | |
588 | * prev now departs the cpu. It's not interesting to record | |
589 | * stats about how efficient we were at scheduling the idle | |
590 | * process, however. | |
591 | */ | |
592 | if (prev != rq->idle) | |
593 | sched_info_depart(prev); | |
594 | ||
595 | if (next != rq->idle) | |
596 | sched_info_arrive(next); | |
597 | } | |
598 | #else | |
599 | #define sched_info_queued(t) do { } while (0) | |
600 | #define sched_info_switch(t, next) do { } while (0) | |
601 | #endif /* CONFIG_SCHEDSTATS */ | |
602 | ||
603 | /* | |
604 | * Adding/removing a task to/from a priority array: | |
605 | */ | |
606 | static void dequeue_task(struct task_struct *p, prio_array_t *array) | |
607 | { | |
608 | array->nr_active--; | |
609 | list_del(&p->run_list); | |
610 | if (list_empty(array->queue + p->prio)) | |
611 | __clear_bit(p->prio, array->bitmap); | |
612 | } | |
613 | ||
614 | static void enqueue_task(struct task_struct *p, prio_array_t *array) | |
615 | { | |
616 | sched_info_queued(p); | |
617 | list_add_tail(&p->run_list, array->queue + p->prio); | |
618 | __set_bit(p->prio, array->bitmap); | |
619 | array->nr_active++; | |
620 | p->array = array; | |
621 | } | |
622 | ||
623 | /* | |
624 | * Put task to the end of the run list without the overhead of dequeue | |
625 | * followed by enqueue. | |
626 | */ | |
627 | static void requeue_task(struct task_struct *p, prio_array_t *array) | |
628 | { | |
629 | list_move_tail(&p->run_list, array->queue + p->prio); | |
630 | } | |
631 | ||
632 | static inline void enqueue_task_head(struct task_struct *p, prio_array_t *array) | |
633 | { | |
634 | list_add(&p->run_list, array->queue + p->prio); | |
635 | __set_bit(p->prio, array->bitmap); | |
636 | array->nr_active++; | |
637 | p->array = array; | |
638 | } | |
639 | ||
640 | /* | |
641 | * effective_prio - return the priority that is based on the static | |
642 | * priority but is modified by bonuses/penalties. | |
643 | * | |
644 | * We scale the actual sleep average [0 .... MAX_SLEEP_AVG] | |
645 | * into the -5 ... 0 ... +5 bonus/penalty range. | |
646 | * | |
647 | * We use 25% of the full 0...39 priority range so that: | |
648 | * | |
649 | * 1) nice +19 interactive tasks do not preempt nice 0 CPU hogs. | |
650 | * 2) nice -20 CPU hogs do not get preempted by nice 0 tasks. | |
651 | * | |
652 | * Both properties are important to certain workloads. | |
653 | */ | |
654 | static int effective_prio(task_t *p) | |
655 | { | |
656 | int bonus, prio; | |
657 | ||
658 | if (rt_task(p)) | |
659 | return p->prio; | |
660 | ||
661 | bonus = CURRENT_BONUS(p) - MAX_BONUS / 2; | |
662 | ||
663 | prio = p->static_prio - bonus; | |
664 | if (prio < MAX_RT_PRIO) | |
665 | prio = MAX_RT_PRIO; | |
666 | if (prio > MAX_PRIO-1) | |
667 | prio = MAX_PRIO-1; | |
668 | return prio; | |
669 | } | |
670 | ||
b910472d | 671 | #ifdef CONFIG_SMP |
dad1c65c | 672 | static inline void inc_prio_bias(runqueue_t *rq, int prio) |
b910472d | 673 | { |
dad1c65c | 674 | rq->prio_bias += MAX_PRIO - prio; |
b910472d CK |
675 | } |
676 | ||
dad1c65c | 677 | static inline void dec_prio_bias(runqueue_t *rq, int prio) |
b910472d | 678 | { |
dad1c65c | 679 | rq->prio_bias -= MAX_PRIO - prio; |
b910472d | 680 | } |
ede3d0fb CK |
681 | |
682 | static inline void inc_nr_running(task_t *p, runqueue_t *rq) | |
683 | { | |
684 | rq->nr_running++; | |
685 | if (rt_task(p)) { | |
686 | if (p != rq->migration_thread) | |
687 | /* | |
688 | * The migration thread does the actual balancing. Do | |
689 | * not bias by its priority as the ultra high priority | |
690 | * will skew balancing adversely. | |
691 | */ | |
692 | inc_prio_bias(rq, p->prio); | |
693 | } else | |
694 | inc_prio_bias(rq, p->static_prio); | |
695 | } | |
696 | ||
697 | static inline void dec_nr_running(task_t *p, runqueue_t *rq) | |
698 | { | |
699 | rq->nr_running--; | |
700 | if (rt_task(p)) { | |
701 | if (p != rq->migration_thread) | |
702 | dec_prio_bias(rq, p->prio); | |
703 | } else | |
704 | dec_prio_bias(rq, p->static_prio); | |
705 | } | |
b910472d | 706 | #else |
dad1c65c | 707 | static inline void inc_prio_bias(runqueue_t *rq, int prio) |
b910472d CK |
708 | { |
709 | } | |
710 | ||
dad1c65c | 711 | static inline void dec_prio_bias(runqueue_t *rq, int prio) |
b910472d CK |
712 | { |
713 | } | |
b910472d CK |
714 | |
715 | static inline void inc_nr_running(task_t *p, runqueue_t *rq) | |
716 | { | |
717 | rq->nr_running++; | |
b910472d CK |
718 | } |
719 | ||
720 | static inline void dec_nr_running(task_t *p, runqueue_t *rq) | |
721 | { | |
722 | rq->nr_running--; | |
b910472d | 723 | } |
ede3d0fb | 724 | #endif |
b910472d | 725 | |
1da177e4 LT |
726 | /* |
727 | * __activate_task - move a task to the runqueue. | |
728 | */ | |
729 | static inline void __activate_task(task_t *p, runqueue_t *rq) | |
730 | { | |
731 | enqueue_task(p, rq->active); | |
b910472d | 732 | inc_nr_running(p, rq); |
1da177e4 LT |
733 | } |
734 | ||
735 | /* | |
736 | * __activate_idle_task - move idle task to the _front_ of runqueue. | |
737 | */ | |
738 | static inline void __activate_idle_task(task_t *p, runqueue_t *rq) | |
739 | { | |
740 | enqueue_task_head(p, rq->active); | |
b910472d | 741 | inc_nr_running(p, rq); |
1da177e4 LT |
742 | } |
743 | ||
a3464a10 | 744 | static int recalc_task_prio(task_t *p, unsigned long long now) |
1da177e4 LT |
745 | { |
746 | /* Caller must always ensure 'now >= p->timestamp' */ | |
747 | unsigned long long __sleep_time = now - p->timestamp; | |
748 | unsigned long sleep_time; | |
749 | ||
750 | if (__sleep_time > NS_MAX_SLEEP_AVG) | |
751 | sleep_time = NS_MAX_SLEEP_AVG; | |
752 | else | |
753 | sleep_time = (unsigned long)__sleep_time; | |
754 | ||
755 | if (likely(sleep_time > 0)) { | |
756 | /* | |
757 | * User tasks that sleep a long time are categorised as | |
758 | * idle and will get just interactive status to stay active & | |
759 | * prevent them suddenly becoming cpu hogs and starving | |
760 | * other processes. | |
761 | */ | |
762 | if (p->mm && p->activated != -1 && | |
763 | sleep_time > INTERACTIVE_SLEEP(p)) { | |
764 | p->sleep_avg = JIFFIES_TO_NS(MAX_SLEEP_AVG - | |
765 | DEF_TIMESLICE); | |
766 | } else { | |
767 | /* | |
768 | * The lower the sleep avg a task has the more | |
769 | * rapidly it will rise with sleep time. | |
770 | */ | |
771 | sleep_time *= (MAX_BONUS - CURRENT_BONUS(p)) ? : 1; | |
772 | ||
773 | /* | |
774 | * Tasks waking from uninterruptible sleep are | |
775 | * limited in their sleep_avg rise as they | |
776 | * are likely to be waiting on I/O | |
777 | */ | |
778 | if (p->activated == -1 && p->mm) { | |
779 | if (p->sleep_avg >= INTERACTIVE_SLEEP(p)) | |
780 | sleep_time = 0; | |
781 | else if (p->sleep_avg + sleep_time >= | |
782 | INTERACTIVE_SLEEP(p)) { | |
783 | p->sleep_avg = INTERACTIVE_SLEEP(p); | |
784 | sleep_time = 0; | |
785 | } | |
786 | } | |
787 | ||
788 | /* | |
789 | * This code gives a bonus to interactive tasks. | |
790 | * | |
791 | * The boost works by updating the 'average sleep time' | |
792 | * value here, based on ->timestamp. The more time a | |
793 | * task spends sleeping, the higher the average gets - | |
794 | * and the higher the priority boost gets as well. | |
795 | */ | |
796 | p->sleep_avg += sleep_time; | |
797 | ||
798 | if (p->sleep_avg > NS_MAX_SLEEP_AVG) | |
799 | p->sleep_avg = NS_MAX_SLEEP_AVG; | |
800 | } | |
801 | } | |
802 | ||
a3464a10 | 803 | return effective_prio(p); |
1da177e4 LT |
804 | } |
805 | ||
806 | /* | |
807 | * activate_task - move a task to the runqueue and do priority recalculation | |
808 | * | |
809 | * Update all the scheduling statistics stuff. (sleep average | |
810 | * calculation, priority modifiers, etc.) | |
811 | */ | |
812 | static void activate_task(task_t *p, runqueue_t *rq, int local) | |
813 | { | |
814 | unsigned long long now; | |
815 | ||
816 | now = sched_clock(); | |
817 | #ifdef CONFIG_SMP | |
818 | if (!local) { | |
819 | /* Compensate for drifting sched_clock */ | |
820 | runqueue_t *this_rq = this_rq(); | |
821 | now = (now - this_rq->timestamp_last_tick) | |
822 | + rq->timestamp_last_tick; | |
823 | } | |
824 | #endif | |
825 | ||
a47ab937 KC |
826 | if (!rt_task(p)) |
827 | p->prio = recalc_task_prio(p, now); | |
1da177e4 LT |
828 | |
829 | /* | |
830 | * This checks to make sure it's not an uninterruptible task | |
831 | * that is now waking up. | |
832 | */ | |
833 | if (!p->activated) { | |
834 | /* | |
835 | * Tasks which were woken up by interrupts (ie. hw events) | |
836 | * are most likely of interactive nature. So we give them | |
837 | * the credit of extending their sleep time to the period | |
838 | * of time they spend on the runqueue, waiting for execution | |
839 | * on a CPU, first time around: | |
840 | */ | |
841 | if (in_interrupt()) | |
842 | p->activated = 2; | |
843 | else { | |
844 | /* | |
845 | * Normal first-time wakeups get a credit too for | |
846 | * on-runqueue time, but it will be weighted down: | |
847 | */ | |
848 | p->activated = 1; | |
849 | } | |
850 | } | |
851 | p->timestamp = now; | |
852 | ||
853 | __activate_task(p, rq); | |
854 | } | |
855 | ||
856 | /* | |
857 | * deactivate_task - remove a task from the runqueue. | |
858 | */ | |
859 | static void deactivate_task(struct task_struct *p, runqueue_t *rq) | |
860 | { | |
b910472d | 861 | dec_nr_running(p, rq); |
1da177e4 LT |
862 | dequeue_task(p, p->array); |
863 | p->array = NULL; | |
864 | } | |
865 | ||
866 | /* | |
867 | * resched_task - mark a task 'to be rescheduled now'. | |
868 | * | |
869 | * On UP this means the setting of the need_resched flag, on SMP it | |
870 | * might also involve a cross-CPU call to trigger the scheduler on | |
871 | * the target CPU. | |
872 | */ | |
873 | #ifdef CONFIG_SMP | |
874 | static void resched_task(task_t *p) | |
875 | { | |
64c7c8f8 | 876 | int cpu; |
1da177e4 LT |
877 | |
878 | assert_spin_locked(&task_rq(p)->lock); | |
879 | ||
64c7c8f8 NP |
880 | if (unlikely(test_tsk_thread_flag(p, TIF_NEED_RESCHED))) |
881 | return; | |
882 | ||
883 | set_tsk_thread_flag(p, TIF_NEED_RESCHED); | |
1da177e4 | 884 | |
64c7c8f8 NP |
885 | cpu = task_cpu(p); |
886 | if (cpu == smp_processor_id()) | |
887 | return; | |
888 | ||
889 | /* NEED_RESCHED must be visible before we test POLLING_NRFLAG */ | |
890 | smp_mb(); | |
891 | if (!test_tsk_thread_flag(p, TIF_POLLING_NRFLAG)) | |
892 | smp_send_reschedule(cpu); | |
1da177e4 LT |
893 | } |
894 | #else | |
895 | static inline void resched_task(task_t *p) | |
896 | { | |
64c7c8f8 | 897 | assert_spin_locked(&task_rq(p)->lock); |
1da177e4 LT |
898 | set_tsk_need_resched(p); |
899 | } | |
900 | #endif | |
901 | ||
902 | /** | |
903 | * task_curr - is this task currently executing on a CPU? | |
904 | * @p: the task in question. | |
905 | */ | |
906 | inline int task_curr(const task_t *p) | |
907 | { | |
908 | return cpu_curr(task_cpu(p)) == p; | |
909 | } | |
910 | ||
911 | #ifdef CONFIG_SMP | |
1da177e4 LT |
912 | typedef struct { |
913 | struct list_head list; | |
1da177e4 | 914 | |
1da177e4 LT |
915 | task_t *task; |
916 | int dest_cpu; | |
917 | ||
1da177e4 LT |
918 | struct completion done; |
919 | } migration_req_t; | |
920 | ||
921 | /* | |
922 | * The task's runqueue lock must be held. | |
923 | * Returns true if you have to wait for migration thread. | |
924 | */ | |
925 | static int migrate_task(task_t *p, int dest_cpu, migration_req_t *req) | |
926 | { | |
927 | runqueue_t *rq = task_rq(p); | |
928 | ||
929 | /* | |
930 | * If the task is not on a runqueue (and not running), then | |
931 | * it is sufficient to simply update the task's cpu field. | |
932 | */ | |
933 | if (!p->array && !task_running(rq, p)) { | |
934 | set_task_cpu(p, dest_cpu); | |
935 | return 0; | |
936 | } | |
937 | ||
938 | init_completion(&req->done); | |
1da177e4 LT |
939 | req->task = p; |
940 | req->dest_cpu = dest_cpu; | |
941 | list_add(&req->list, &rq->migration_queue); | |
942 | return 1; | |
943 | } | |
944 | ||
945 | /* | |
946 | * wait_task_inactive - wait for a thread to unschedule. | |
947 | * | |
948 | * The caller must ensure that the task *will* unschedule sometime soon, | |
949 | * else this function might spin for a *long* time. This function can't | |
950 | * be called with interrupts off, or it may introduce deadlock with | |
951 | * smp_call_function() if an IPI is sent by the same process we are | |
952 | * waiting to become inactive. | |
953 | */ | |
95cdf3b7 | 954 | void wait_task_inactive(task_t *p) |
1da177e4 LT |
955 | { |
956 | unsigned long flags; | |
957 | runqueue_t *rq; | |
958 | int preempted; | |
959 | ||
960 | repeat: | |
961 | rq = task_rq_lock(p, &flags); | |
962 | /* Must be off runqueue entirely, not preempted. */ | |
963 | if (unlikely(p->array || task_running(rq, p))) { | |
964 | /* If it's preempted, we yield. It could be a while. */ | |
965 | preempted = !task_running(rq, p); | |
966 | task_rq_unlock(rq, &flags); | |
967 | cpu_relax(); | |
968 | if (preempted) | |
969 | yield(); | |
970 | goto repeat; | |
971 | } | |
972 | task_rq_unlock(rq, &flags); | |
973 | } | |
974 | ||
975 | /*** | |
976 | * kick_process - kick a running thread to enter/exit the kernel | |
977 | * @p: the to-be-kicked thread | |
978 | * | |
979 | * Cause a process which is running on another CPU to enter | |
980 | * kernel-mode, without any delay. (to get signals handled.) | |
981 | * | |
982 | * NOTE: this function doesnt have to take the runqueue lock, | |
983 | * because all it wants to ensure is that the remote task enters | |
984 | * the kernel. If the IPI races and the task has been migrated | |
985 | * to another CPU then no harm is done and the purpose has been | |
986 | * achieved as well. | |
987 | */ | |
988 | void kick_process(task_t *p) | |
989 | { | |
990 | int cpu; | |
991 | ||
992 | preempt_disable(); | |
993 | cpu = task_cpu(p); | |
994 | if ((cpu != smp_processor_id()) && task_curr(p)) | |
995 | smp_send_reschedule(cpu); | |
996 | preempt_enable(); | |
997 | } | |
998 | ||
999 | /* | |
1000 | * Return a low guess at the load of a migration-source cpu. | |
1001 | * | |
1002 | * We want to under-estimate the load of migration sources, to | |
1003 | * balance conservatively. | |
1004 | */ | |
b910472d | 1005 | static inline unsigned long __source_load(int cpu, int type, enum idle_type idle) |
1da177e4 LT |
1006 | { |
1007 | runqueue_t *rq = cpu_rq(cpu); | |
6dd4a85b | 1008 | unsigned long running = rq->nr_running; |
3b0bd9bc | 1009 | unsigned long source_load, cpu_load = rq->cpu_load[type-1], |
6dd4a85b | 1010 | load_now = running * SCHED_LOAD_SCALE; |
b910472d | 1011 | |
3b0bd9bc CK |
1012 | if (type == 0) |
1013 | source_load = load_now; | |
1014 | else | |
1015 | source_load = min(cpu_load, load_now); | |
1016 | ||
6dd4a85b | 1017 | if (running > 1 || (idle == NOT_IDLE && running)) |
b910472d | 1018 | /* |
3b0bd9bc CK |
1019 | * If we are busy rebalancing the load is biased by |
1020 | * priority to create 'nice' support across cpus. When | |
1021 | * idle rebalancing we should only bias the source_load if | |
1022 | * there is more than one task running on that queue to | |
1023 | * prevent idle rebalance from trying to pull tasks from a | |
1024 | * queue with only one running task. | |
b910472d | 1025 | */ |
6dd4a85b | 1026 | source_load = source_load * rq->prio_bias / running; |
b910472d | 1027 | |
3b0bd9bc | 1028 | return source_load; |
b910472d CK |
1029 | } |
1030 | ||
1031 | static inline unsigned long source_load(int cpu, int type) | |
1032 | { | |
1033 | return __source_load(cpu, type, NOT_IDLE); | |
1da177e4 LT |
1034 | } |
1035 | ||
1036 | /* | |
1037 | * Return a high guess at the load of a migration-target cpu | |
1038 | */ | |
b910472d | 1039 | static inline unsigned long __target_load(int cpu, int type, enum idle_type idle) |
1da177e4 LT |
1040 | { |
1041 | runqueue_t *rq = cpu_rq(cpu); | |
6dd4a85b | 1042 | unsigned long running = rq->nr_running; |
3b0bd9bc | 1043 | unsigned long target_load, cpu_load = rq->cpu_load[type-1], |
6dd4a85b | 1044 | load_now = running * SCHED_LOAD_SCALE; |
b910472d | 1045 | |
7897986b | 1046 | if (type == 0) |
3b0bd9bc CK |
1047 | target_load = load_now; |
1048 | else | |
1049 | target_load = max(cpu_load, load_now); | |
1da177e4 | 1050 | |
6dd4a85b CK |
1051 | if (running > 1 || (idle == NOT_IDLE && running)) |
1052 | target_load = target_load * rq->prio_bias / running; | |
3b0bd9bc CK |
1053 | |
1054 | return target_load; | |
b910472d CK |
1055 | } |
1056 | ||
1057 | static inline unsigned long target_load(int cpu, int type) | |
1058 | { | |
1059 | return __target_load(cpu, type, NOT_IDLE); | |
1da177e4 LT |
1060 | } |
1061 | ||
147cbb4b NP |
1062 | /* |
1063 | * find_idlest_group finds and returns the least busy CPU group within the | |
1064 | * domain. | |
1065 | */ | |
1066 | static struct sched_group * | |
1067 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu) | |
1068 | { | |
1069 | struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups; | |
1070 | unsigned long min_load = ULONG_MAX, this_load = 0; | |
1071 | int load_idx = sd->forkexec_idx; | |
1072 | int imbalance = 100 + (sd->imbalance_pct-100)/2; | |
1073 | ||
1074 | do { | |
1075 | unsigned long load, avg_load; | |
1076 | int local_group; | |
1077 | int i; | |
1078 | ||
da5a5522 BD |
1079 | /* Skip over this group if it has no CPUs allowed */ |
1080 | if (!cpus_intersects(group->cpumask, p->cpus_allowed)) | |
1081 | goto nextgroup; | |
1082 | ||
147cbb4b | 1083 | local_group = cpu_isset(this_cpu, group->cpumask); |
147cbb4b NP |
1084 | |
1085 | /* Tally up the load of all CPUs in the group */ | |
1086 | avg_load = 0; | |
1087 | ||
1088 | for_each_cpu_mask(i, group->cpumask) { | |
1089 | /* Bias balancing toward cpus of our domain */ | |
1090 | if (local_group) | |
1091 | load = source_load(i, load_idx); | |
1092 | else | |
1093 | load = target_load(i, load_idx); | |
1094 | ||
1095 | avg_load += load; | |
1096 | } | |
1097 | ||
1098 | /* Adjust by relative CPU power of the group */ | |
1099 | avg_load = (avg_load * SCHED_LOAD_SCALE) / group->cpu_power; | |
1100 | ||
1101 | if (local_group) { | |
1102 | this_load = avg_load; | |
1103 | this = group; | |
1104 | } else if (avg_load < min_load) { | |
1105 | min_load = avg_load; | |
1106 | idlest = group; | |
1107 | } | |
da5a5522 | 1108 | nextgroup: |
147cbb4b NP |
1109 | group = group->next; |
1110 | } while (group != sd->groups); | |
1111 | ||
1112 | if (!idlest || 100*this_load < imbalance*min_load) | |
1113 | return NULL; | |
1114 | return idlest; | |
1115 | } | |
1116 | ||
1117 | /* | |
1118 | * find_idlest_queue - find the idlest runqueue among the cpus in group. | |
1119 | */ | |
95cdf3b7 IM |
1120 | static int |
1121 | find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu) | |
147cbb4b | 1122 | { |
da5a5522 | 1123 | cpumask_t tmp; |
147cbb4b NP |
1124 | unsigned long load, min_load = ULONG_MAX; |
1125 | int idlest = -1; | |
1126 | int i; | |
1127 | ||
da5a5522 BD |
1128 | /* Traverse only the allowed CPUs */ |
1129 | cpus_and(tmp, group->cpumask, p->cpus_allowed); | |
1130 | ||
1131 | for_each_cpu_mask(i, tmp) { | |
147cbb4b NP |
1132 | load = source_load(i, 0); |
1133 | ||
1134 | if (load < min_load || (load == min_load && i == this_cpu)) { | |
1135 | min_load = load; | |
1136 | idlest = i; | |
1137 | } | |
1138 | } | |
1139 | ||
1140 | return idlest; | |
1141 | } | |
1142 | ||
476d139c NP |
1143 | /* |
1144 | * sched_balance_self: balance the current task (running on cpu) in domains | |
1145 | * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and | |
1146 | * SD_BALANCE_EXEC. | |
1147 | * | |
1148 | * Balance, ie. select the least loaded group. | |
1149 | * | |
1150 | * Returns the target CPU number, or the same CPU if no balancing is needed. | |
1151 | * | |
1152 | * preempt must be disabled. | |
1153 | */ | |
1154 | static int sched_balance_self(int cpu, int flag) | |
1155 | { | |
1156 | struct task_struct *t = current; | |
1157 | struct sched_domain *tmp, *sd = NULL; | |
147cbb4b | 1158 | |
476d139c NP |
1159 | for_each_domain(cpu, tmp) |
1160 | if (tmp->flags & flag) | |
1161 | sd = tmp; | |
1162 | ||
1163 | while (sd) { | |
1164 | cpumask_t span; | |
1165 | struct sched_group *group; | |
1166 | int new_cpu; | |
1167 | int weight; | |
1168 | ||
1169 | span = sd->span; | |
1170 | group = find_idlest_group(sd, t, cpu); | |
1171 | if (!group) | |
1172 | goto nextlevel; | |
1173 | ||
da5a5522 | 1174 | new_cpu = find_idlest_cpu(group, t, cpu); |
476d139c NP |
1175 | if (new_cpu == -1 || new_cpu == cpu) |
1176 | goto nextlevel; | |
1177 | ||
1178 | /* Now try balancing at a lower domain level */ | |
1179 | cpu = new_cpu; | |
1180 | nextlevel: | |
1181 | sd = NULL; | |
1182 | weight = cpus_weight(span); | |
1183 | for_each_domain(cpu, tmp) { | |
1184 | if (weight <= cpus_weight(tmp->span)) | |
1185 | break; | |
1186 | if (tmp->flags & flag) | |
1187 | sd = tmp; | |
1188 | } | |
1189 | /* while loop will break here if sd == NULL */ | |
1190 | } | |
1191 | ||
1192 | return cpu; | |
1193 | } | |
1194 | ||
1195 | #endif /* CONFIG_SMP */ | |
1da177e4 LT |
1196 | |
1197 | /* | |
1198 | * wake_idle() will wake a task on an idle cpu if task->cpu is | |
1199 | * not idle and an idle cpu is available. The span of cpus to | |
1200 | * search starts with cpus closest then further out as needed, | |
1201 | * so we always favor a closer, idle cpu. | |
1202 | * | |
1203 | * Returns the CPU we should wake onto. | |
1204 | */ | |
1205 | #if defined(ARCH_HAS_SCHED_WAKE_IDLE) | |
1206 | static int wake_idle(int cpu, task_t *p) | |
1207 | { | |
1208 | cpumask_t tmp; | |
1209 | struct sched_domain *sd; | |
1210 | int i; | |
1211 | ||
1212 | if (idle_cpu(cpu)) | |
1213 | return cpu; | |
1214 | ||
1215 | for_each_domain(cpu, sd) { | |
1216 | if (sd->flags & SD_WAKE_IDLE) { | |
e0f364f4 | 1217 | cpus_and(tmp, sd->span, p->cpus_allowed); |
1da177e4 LT |
1218 | for_each_cpu_mask(i, tmp) { |
1219 | if (idle_cpu(i)) | |
1220 | return i; | |
1221 | } | |
1222 | } | |
e0f364f4 NP |
1223 | else |
1224 | break; | |
1da177e4 LT |
1225 | } |
1226 | return cpu; | |
1227 | } | |
1228 | #else | |
1229 | static inline int wake_idle(int cpu, task_t *p) | |
1230 | { | |
1231 | return cpu; | |
1232 | } | |
1233 | #endif | |
1234 | ||
1235 | /*** | |
1236 | * try_to_wake_up - wake up a thread | |
1237 | * @p: the to-be-woken-up thread | |
1238 | * @state: the mask of task states that can be woken | |
1239 | * @sync: do a synchronous wakeup? | |
1240 | * | |
1241 | * Put it on the run-queue if it's not already there. The "current" | |
1242 | * thread is always on the run-queue (except when the actual | |
1243 | * re-schedule is in progress), and as such you're allowed to do | |
1244 | * the simpler "current->state = TASK_RUNNING" to mark yourself | |
1245 | * runnable without the overhead of this. | |
1246 | * | |
1247 | * returns failure only if the task is already active. | |
1248 | */ | |
95cdf3b7 | 1249 | static int try_to_wake_up(task_t *p, unsigned int state, int sync) |
1da177e4 LT |
1250 | { |
1251 | int cpu, this_cpu, success = 0; | |
1252 | unsigned long flags; | |
1253 | long old_state; | |
1254 | runqueue_t *rq; | |
1255 | #ifdef CONFIG_SMP | |
1256 | unsigned long load, this_load; | |
7897986b | 1257 | struct sched_domain *sd, *this_sd = NULL; |
1da177e4 LT |
1258 | int new_cpu; |
1259 | #endif | |
1260 | ||
1261 | rq = task_rq_lock(p, &flags); | |
1262 | old_state = p->state; | |
1263 | if (!(old_state & state)) | |
1264 | goto out; | |
1265 | ||
1266 | if (p->array) | |
1267 | goto out_running; | |
1268 | ||
1269 | cpu = task_cpu(p); | |
1270 | this_cpu = smp_processor_id(); | |
1271 | ||
1272 | #ifdef CONFIG_SMP | |
1273 | if (unlikely(task_running(rq, p))) | |
1274 | goto out_activate; | |
1275 | ||
7897986b NP |
1276 | new_cpu = cpu; |
1277 | ||
1da177e4 LT |
1278 | schedstat_inc(rq, ttwu_cnt); |
1279 | if (cpu == this_cpu) { | |
1280 | schedstat_inc(rq, ttwu_local); | |
7897986b NP |
1281 | goto out_set_cpu; |
1282 | } | |
1283 | ||
1284 | for_each_domain(this_cpu, sd) { | |
1285 | if (cpu_isset(cpu, sd->span)) { | |
1286 | schedstat_inc(sd, ttwu_wake_remote); | |
1287 | this_sd = sd; | |
1288 | break; | |
1da177e4 LT |
1289 | } |
1290 | } | |
1da177e4 | 1291 | |
7897986b | 1292 | if (unlikely(!cpu_isset(this_cpu, p->cpus_allowed))) |
1da177e4 LT |
1293 | goto out_set_cpu; |
1294 | ||
1da177e4 | 1295 | /* |
7897986b | 1296 | * Check for affine wakeup and passive balancing possibilities. |
1da177e4 | 1297 | */ |
7897986b NP |
1298 | if (this_sd) { |
1299 | int idx = this_sd->wake_idx; | |
1300 | unsigned int imbalance; | |
1da177e4 | 1301 | |
a3f21bce NP |
1302 | imbalance = 100 + (this_sd->imbalance_pct - 100) / 2; |
1303 | ||
7897986b NP |
1304 | load = source_load(cpu, idx); |
1305 | this_load = target_load(this_cpu, idx); | |
1da177e4 | 1306 | |
7897986b NP |
1307 | new_cpu = this_cpu; /* Wake to this CPU if we can */ |
1308 | ||
a3f21bce NP |
1309 | if (this_sd->flags & SD_WAKE_AFFINE) { |
1310 | unsigned long tl = this_load; | |
1da177e4 | 1311 | /* |
a3f21bce NP |
1312 | * If sync wakeup then subtract the (maximum possible) |
1313 | * effect of the currently running task from the load | |
1314 | * of the current CPU: | |
1da177e4 | 1315 | */ |
a3f21bce NP |
1316 | if (sync) |
1317 | tl -= SCHED_LOAD_SCALE; | |
1318 | ||
1319 | if ((tl <= load && | |
1320 | tl + target_load(cpu, idx) <= SCHED_LOAD_SCALE) || | |
1321 | 100*(tl + SCHED_LOAD_SCALE) <= imbalance*load) { | |
1322 | /* | |
1323 | * This domain has SD_WAKE_AFFINE and | |
1324 | * p is cache cold in this domain, and | |
1325 | * there is no bad imbalance. | |
1326 | */ | |
1327 | schedstat_inc(this_sd, ttwu_move_affine); | |
1328 | goto out_set_cpu; | |
1329 | } | |
1330 | } | |
1331 | ||
1332 | /* | |
1333 | * Start passive balancing when half the imbalance_pct | |
1334 | * limit is reached. | |
1335 | */ | |
1336 | if (this_sd->flags & SD_WAKE_BALANCE) { | |
1337 | if (imbalance*this_load <= 100*load) { | |
1338 | schedstat_inc(this_sd, ttwu_move_balance); | |
1339 | goto out_set_cpu; | |
1340 | } | |
1da177e4 LT |
1341 | } |
1342 | } | |
1343 | ||
1344 | new_cpu = cpu; /* Could not wake to this_cpu. Wake to cpu instead */ | |
1345 | out_set_cpu: | |
1346 | new_cpu = wake_idle(new_cpu, p); | |
1347 | if (new_cpu != cpu) { | |
1348 | set_task_cpu(p, new_cpu); | |
1349 | task_rq_unlock(rq, &flags); | |
1350 | /* might preempt at this point */ | |
1351 | rq = task_rq_lock(p, &flags); | |
1352 | old_state = p->state; | |
1353 | if (!(old_state & state)) | |
1354 | goto out; | |
1355 | if (p->array) | |
1356 | goto out_running; | |
1357 | ||
1358 | this_cpu = smp_processor_id(); | |
1359 | cpu = task_cpu(p); | |
1360 | } | |
1361 | ||
1362 | out_activate: | |
1363 | #endif /* CONFIG_SMP */ | |
1364 | if (old_state == TASK_UNINTERRUPTIBLE) { | |
1365 | rq->nr_uninterruptible--; | |
1366 | /* | |
1367 | * Tasks on involuntary sleep don't earn | |
1368 | * sleep_avg beyond just interactive state. | |
1369 | */ | |
1370 | p->activated = -1; | |
1371 | } | |
1372 | ||
d79fc0fc IM |
1373 | /* |
1374 | * Tasks that have marked their sleep as noninteractive get | |
1375 | * woken up without updating their sleep average. (i.e. their | |
1376 | * sleep is handled in a priority-neutral manner, no priority | |
1377 | * boost and no penalty.) | |
1378 | */ | |
1379 | if (old_state & TASK_NONINTERACTIVE) | |
1380 | __activate_task(p, rq); | |
1381 | else | |
1382 | activate_task(p, rq, cpu == this_cpu); | |
1da177e4 LT |
1383 | /* |
1384 | * Sync wakeups (i.e. those types of wakeups where the waker | |
1385 | * has indicated that it will leave the CPU in short order) | |
1386 | * don't trigger a preemption, if the woken up task will run on | |
1387 | * this cpu. (in this case the 'I will reschedule' promise of | |
1388 | * the waker guarantees that the freshly woken up task is going | |
1389 | * to be considered on this CPU.) | |
1390 | */ | |
1da177e4 LT |
1391 | if (!sync || cpu != this_cpu) { |
1392 | if (TASK_PREEMPTS_CURR(p, rq)) | |
1393 | resched_task(rq->curr); | |
1394 | } | |
1395 | success = 1; | |
1396 | ||
1397 | out_running: | |
1398 | p->state = TASK_RUNNING; | |
1399 | out: | |
1400 | task_rq_unlock(rq, &flags); | |
1401 | ||
1402 | return success; | |
1403 | } | |
1404 | ||
95cdf3b7 | 1405 | int fastcall wake_up_process(task_t *p) |
1da177e4 LT |
1406 | { |
1407 | return try_to_wake_up(p, TASK_STOPPED | TASK_TRACED | | |
1408 | TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE, 0); | |
1409 | } | |
1410 | ||
1411 | EXPORT_SYMBOL(wake_up_process); | |
1412 | ||
1413 | int fastcall wake_up_state(task_t *p, unsigned int state) | |
1414 | { | |
1415 | return try_to_wake_up(p, state, 0); | |
1416 | } | |
1417 | ||
1da177e4 LT |
1418 | /* |
1419 | * Perform scheduler related setup for a newly forked process p. | |
1420 | * p is forked by current. | |
1421 | */ | |
476d139c | 1422 | void fastcall sched_fork(task_t *p, int clone_flags) |
1da177e4 | 1423 | { |
476d139c NP |
1424 | int cpu = get_cpu(); |
1425 | ||
1426 | #ifdef CONFIG_SMP | |
1427 | cpu = sched_balance_self(cpu, SD_BALANCE_FORK); | |
1428 | #endif | |
1429 | set_task_cpu(p, cpu); | |
1430 | ||
1da177e4 LT |
1431 | /* |
1432 | * We mark the process as running here, but have not actually | |
1433 | * inserted it onto the runqueue yet. This guarantees that | |
1434 | * nobody will actually run it, and a signal or other external | |
1435 | * event cannot wake it up and insert it on the runqueue either. | |
1436 | */ | |
1437 | p->state = TASK_RUNNING; | |
1438 | INIT_LIST_HEAD(&p->run_list); | |
1439 | p->array = NULL; | |
1da177e4 LT |
1440 | #ifdef CONFIG_SCHEDSTATS |
1441 | memset(&p->sched_info, 0, sizeof(p->sched_info)); | |
1442 | #endif | |
4866cde0 NP |
1443 | #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW) |
1444 | p->oncpu = 0; | |
1445 | #endif | |
1da177e4 | 1446 | #ifdef CONFIG_PREEMPT |
4866cde0 | 1447 | /* Want to start with kernel preemption disabled. */ |
a1261f54 | 1448 | task_thread_info(p)->preempt_count = 1; |
1da177e4 LT |
1449 | #endif |
1450 | /* | |
1451 | * Share the timeslice between parent and child, thus the | |
1452 | * total amount of pending timeslices in the system doesn't change, | |
1453 | * resulting in more scheduling fairness. | |
1454 | */ | |
1455 | local_irq_disable(); | |
1456 | p->time_slice = (current->time_slice + 1) >> 1; | |
1457 | /* | |
1458 | * The remainder of the first timeslice might be recovered by | |
1459 | * the parent if the child exits early enough. | |
1460 | */ | |
1461 | p->first_time_slice = 1; | |
1462 | current->time_slice >>= 1; | |
1463 | p->timestamp = sched_clock(); | |
1464 | if (unlikely(!current->time_slice)) { | |
1465 | /* | |
1466 | * This case is rare, it happens when the parent has only | |
1467 | * a single jiffy left from its timeslice. Taking the | |
1468 | * runqueue lock is not a problem. | |
1469 | */ | |
1470 | current->time_slice = 1; | |
1da177e4 | 1471 | scheduler_tick(); |
476d139c NP |
1472 | } |
1473 | local_irq_enable(); | |
1474 | put_cpu(); | |
1da177e4 LT |
1475 | } |
1476 | ||
1477 | /* | |
1478 | * wake_up_new_task - wake up a newly created task for the first time. | |
1479 | * | |
1480 | * This function will do some initial scheduler statistics housekeeping | |
1481 | * that must be done for every newly created context, then puts the task | |
1482 | * on the runqueue and wakes it. | |
1483 | */ | |
95cdf3b7 | 1484 | void fastcall wake_up_new_task(task_t *p, unsigned long clone_flags) |
1da177e4 LT |
1485 | { |
1486 | unsigned long flags; | |
1487 | int this_cpu, cpu; | |
1488 | runqueue_t *rq, *this_rq; | |
1489 | ||
1490 | rq = task_rq_lock(p, &flags); | |
147cbb4b | 1491 | BUG_ON(p->state != TASK_RUNNING); |
1da177e4 | 1492 | this_cpu = smp_processor_id(); |
147cbb4b | 1493 | cpu = task_cpu(p); |
1da177e4 | 1494 | |
1da177e4 LT |
1495 | /* |
1496 | * We decrease the sleep average of forking parents | |
1497 | * and children as well, to keep max-interactive tasks | |
1498 | * from forking tasks that are max-interactive. The parent | |
1499 | * (current) is done further down, under its lock. | |
1500 | */ | |
1501 | p->sleep_avg = JIFFIES_TO_NS(CURRENT_BONUS(p) * | |
1502 | CHILD_PENALTY / 100 * MAX_SLEEP_AVG / MAX_BONUS); | |
1503 | ||
1504 | p->prio = effective_prio(p); | |
1505 | ||
1506 | if (likely(cpu == this_cpu)) { | |
1507 | if (!(clone_flags & CLONE_VM)) { | |
1508 | /* | |
1509 | * The VM isn't cloned, so we're in a good position to | |
1510 | * do child-runs-first in anticipation of an exec. This | |
1511 | * usually avoids a lot of COW overhead. | |
1512 | */ | |
1513 | if (unlikely(!current->array)) | |
1514 | __activate_task(p, rq); | |
1515 | else { | |
1516 | p->prio = current->prio; | |
1517 | list_add_tail(&p->run_list, ¤t->run_list); | |
1518 | p->array = current->array; | |
1519 | p->array->nr_active++; | |
b910472d | 1520 | inc_nr_running(p, rq); |
1da177e4 LT |
1521 | } |
1522 | set_need_resched(); | |
1523 | } else | |
1524 | /* Run child last */ | |
1525 | __activate_task(p, rq); | |
1526 | /* | |
1527 | * We skip the following code due to cpu == this_cpu | |
1528 | * | |
1529 | * task_rq_unlock(rq, &flags); | |
1530 | * this_rq = task_rq_lock(current, &flags); | |
1531 | */ | |
1532 | this_rq = rq; | |
1533 | } else { | |
1534 | this_rq = cpu_rq(this_cpu); | |
1535 | ||
1536 | /* | |
1537 | * Not the local CPU - must adjust timestamp. This should | |
1538 | * get optimised away in the !CONFIG_SMP case. | |
1539 | */ | |
1540 | p->timestamp = (p->timestamp - this_rq->timestamp_last_tick) | |
1541 | + rq->timestamp_last_tick; | |
1542 | __activate_task(p, rq); | |
1543 | if (TASK_PREEMPTS_CURR(p, rq)) | |
1544 | resched_task(rq->curr); | |
1545 | ||
1546 | /* | |
1547 | * Parent and child are on different CPUs, now get the | |
1548 | * parent runqueue to update the parent's ->sleep_avg: | |
1549 | */ | |
1550 | task_rq_unlock(rq, &flags); | |
1551 | this_rq = task_rq_lock(current, &flags); | |
1552 | } | |
1553 | current->sleep_avg = JIFFIES_TO_NS(CURRENT_BONUS(current) * | |
1554 | PARENT_PENALTY / 100 * MAX_SLEEP_AVG / MAX_BONUS); | |
1555 | task_rq_unlock(this_rq, &flags); | |
1556 | } | |
1557 | ||
1558 | /* | |
1559 | * Potentially available exiting-child timeslices are | |
1560 | * retrieved here - this way the parent does not get | |
1561 | * penalized for creating too many threads. | |
1562 | * | |
1563 | * (this cannot be used to 'generate' timeslices | |
1564 | * artificially, because any timeslice recovered here | |
1565 | * was given away by the parent in the first place.) | |
1566 | */ | |
95cdf3b7 | 1567 | void fastcall sched_exit(task_t *p) |
1da177e4 LT |
1568 | { |
1569 | unsigned long flags; | |
1570 | runqueue_t *rq; | |
1571 | ||
1572 | /* | |
1573 | * If the child was a (relative-) CPU hog then decrease | |
1574 | * the sleep_avg of the parent as well. | |
1575 | */ | |
1576 | rq = task_rq_lock(p->parent, &flags); | |
889dfafe | 1577 | if (p->first_time_slice && task_cpu(p) == task_cpu(p->parent)) { |
1da177e4 LT |
1578 | p->parent->time_slice += p->time_slice; |
1579 | if (unlikely(p->parent->time_slice > task_timeslice(p))) | |
1580 | p->parent->time_slice = task_timeslice(p); | |
1581 | } | |
1582 | if (p->sleep_avg < p->parent->sleep_avg) | |
1583 | p->parent->sleep_avg = p->parent->sleep_avg / | |
1584 | (EXIT_WEIGHT + 1) * EXIT_WEIGHT + p->sleep_avg / | |
1585 | (EXIT_WEIGHT + 1); | |
1586 | task_rq_unlock(rq, &flags); | |
1587 | } | |
1588 | ||
4866cde0 NP |
1589 | /** |
1590 | * prepare_task_switch - prepare to switch tasks | |
1591 | * @rq: the runqueue preparing to switch | |
1592 | * @next: the task we are going to switch to. | |
1593 | * | |
1594 | * This is called with the rq lock held and interrupts off. It must | |
1595 | * be paired with a subsequent finish_task_switch after the context | |
1596 | * switch. | |
1597 | * | |
1598 | * prepare_task_switch sets up locking and calls architecture specific | |
1599 | * hooks. | |
1600 | */ | |
1601 | static inline void prepare_task_switch(runqueue_t *rq, task_t *next) | |
1602 | { | |
1603 | prepare_lock_switch(rq, next); | |
1604 | prepare_arch_switch(next); | |
1605 | } | |
1606 | ||
1da177e4 LT |
1607 | /** |
1608 | * finish_task_switch - clean up after a task-switch | |
344babaa | 1609 | * @rq: runqueue associated with task-switch |
1da177e4 LT |
1610 | * @prev: the thread we just switched away from. |
1611 | * | |
4866cde0 NP |
1612 | * finish_task_switch must be called after the context switch, paired |
1613 | * with a prepare_task_switch call before the context switch. | |
1614 | * finish_task_switch will reconcile locking set up by prepare_task_switch, | |
1615 | * and do any other architecture-specific cleanup actions. | |
1da177e4 LT |
1616 | * |
1617 | * Note that we may have delayed dropping an mm in context_switch(). If | |
1618 | * so, we finish that here outside of the runqueue lock. (Doing it | |
1619 | * with the lock held can cause deadlocks; see schedule() for | |
1620 | * details.) | |
1621 | */ | |
4866cde0 | 1622 | static inline void finish_task_switch(runqueue_t *rq, task_t *prev) |
1da177e4 LT |
1623 | __releases(rq->lock) |
1624 | { | |
1da177e4 LT |
1625 | struct mm_struct *mm = rq->prev_mm; |
1626 | unsigned long prev_task_flags; | |
1627 | ||
1628 | rq->prev_mm = NULL; | |
1629 | ||
1630 | /* | |
1631 | * A task struct has one reference for the use as "current". | |
1632 | * If a task dies, then it sets EXIT_ZOMBIE in tsk->exit_state and | |
1633 | * calls schedule one last time. The schedule call will never return, | |
1634 | * and the scheduled task must drop that reference. | |
1635 | * The test for EXIT_ZOMBIE must occur while the runqueue locks are | |
1636 | * still held, otherwise prev could be scheduled on another cpu, die | |
1637 | * there before we look at prev->state, and then the reference would | |
1638 | * be dropped twice. | |
1639 | * Manfred Spraul <manfred@colorfullife.com> | |
1640 | */ | |
1641 | prev_task_flags = prev->flags; | |
4866cde0 NP |
1642 | finish_arch_switch(prev); |
1643 | finish_lock_switch(rq, prev); | |
1da177e4 LT |
1644 | if (mm) |
1645 | mmdrop(mm); | |
1646 | if (unlikely(prev_task_flags & PF_DEAD)) | |
1647 | put_task_struct(prev); | |
1648 | } | |
1649 | ||
1650 | /** | |
1651 | * schedule_tail - first thing a freshly forked thread must call. | |
1652 | * @prev: the thread we just switched away from. | |
1653 | */ | |
1654 | asmlinkage void schedule_tail(task_t *prev) | |
1655 | __releases(rq->lock) | |
1656 | { | |
4866cde0 NP |
1657 | runqueue_t *rq = this_rq(); |
1658 | finish_task_switch(rq, prev); | |
1659 | #ifdef __ARCH_WANT_UNLOCKED_CTXSW | |
1660 | /* In this case, finish_task_switch does not reenable preemption */ | |
1661 | preempt_enable(); | |
1662 | #endif | |
1da177e4 LT |
1663 | if (current->set_child_tid) |
1664 | put_user(current->pid, current->set_child_tid); | |
1665 | } | |
1666 | ||
1667 | /* | |
1668 | * context_switch - switch to the new MM and the new | |
1669 | * thread's register state. | |
1670 | */ | |
1671 | static inline | |
1672 | task_t * context_switch(runqueue_t *rq, task_t *prev, task_t *next) | |
1673 | { | |
1674 | struct mm_struct *mm = next->mm; | |
1675 | struct mm_struct *oldmm = prev->active_mm; | |
1676 | ||
1677 | if (unlikely(!mm)) { | |
1678 | next->active_mm = oldmm; | |
1679 | atomic_inc(&oldmm->mm_count); | |
1680 | enter_lazy_tlb(oldmm, next); | |
1681 | } else | |
1682 | switch_mm(oldmm, mm, next); | |
1683 | ||
1684 | if (unlikely(!prev->mm)) { | |
1685 | prev->active_mm = NULL; | |
1686 | WARN_ON(rq->prev_mm); | |
1687 | rq->prev_mm = oldmm; | |
1688 | } | |
1689 | ||
1690 | /* Here we just switch the register state and the stack. */ | |
1691 | switch_to(prev, next, prev); | |
1692 | ||
1693 | return prev; | |
1694 | } | |
1695 | ||
1696 | /* | |
1697 | * nr_running, nr_uninterruptible and nr_context_switches: | |
1698 | * | |
1699 | * externally visible scheduler statistics: current number of runnable | |
1700 | * threads, current number of uninterruptible-sleeping threads, total | |
1701 | * number of context switches performed since bootup. | |
1702 | */ | |
1703 | unsigned long nr_running(void) | |
1704 | { | |
1705 | unsigned long i, sum = 0; | |
1706 | ||
1707 | for_each_online_cpu(i) | |
1708 | sum += cpu_rq(i)->nr_running; | |
1709 | ||
1710 | return sum; | |
1711 | } | |
1712 | ||
1713 | unsigned long nr_uninterruptible(void) | |
1714 | { | |
1715 | unsigned long i, sum = 0; | |
1716 | ||
1717 | for_each_cpu(i) | |
1718 | sum += cpu_rq(i)->nr_uninterruptible; | |
1719 | ||
1720 | /* | |
1721 | * Since we read the counters lockless, it might be slightly | |
1722 | * inaccurate. Do not allow it to go below zero though: | |
1723 | */ | |
1724 | if (unlikely((long)sum < 0)) | |
1725 | sum = 0; | |
1726 | ||
1727 | return sum; | |
1728 | } | |
1729 | ||
1730 | unsigned long long nr_context_switches(void) | |
1731 | { | |
1732 | unsigned long long i, sum = 0; | |
1733 | ||
1734 | for_each_cpu(i) | |
1735 | sum += cpu_rq(i)->nr_switches; | |
1736 | ||
1737 | return sum; | |
1738 | } | |
1739 | ||
1740 | unsigned long nr_iowait(void) | |
1741 | { | |
1742 | unsigned long i, sum = 0; | |
1743 | ||
1744 | for_each_cpu(i) | |
1745 | sum += atomic_read(&cpu_rq(i)->nr_iowait); | |
1746 | ||
1747 | return sum; | |
1748 | } | |
1749 | ||
1750 | #ifdef CONFIG_SMP | |
1751 | ||
1752 | /* | |
1753 | * double_rq_lock - safely lock two runqueues | |
1754 | * | |
1755 | * Note this does not disable interrupts like task_rq_lock, | |
1756 | * you need to do so manually before calling. | |
1757 | */ | |
1758 | static void double_rq_lock(runqueue_t *rq1, runqueue_t *rq2) | |
1759 | __acquires(rq1->lock) | |
1760 | __acquires(rq2->lock) | |
1761 | { | |
1762 | if (rq1 == rq2) { | |
1763 | spin_lock(&rq1->lock); | |
1764 | __acquire(rq2->lock); /* Fake it out ;) */ | |
1765 | } else { | |
1766 | if (rq1 < rq2) { | |
1767 | spin_lock(&rq1->lock); | |
1768 | spin_lock(&rq2->lock); | |
1769 | } else { | |
1770 | spin_lock(&rq2->lock); | |
1771 | spin_lock(&rq1->lock); | |
1772 | } | |
1773 | } | |
1774 | } | |
1775 | ||
1776 | /* | |
1777 | * double_rq_unlock - safely unlock two runqueues | |
1778 | * | |
1779 | * Note this does not restore interrupts like task_rq_unlock, | |
1780 | * you need to do so manually after calling. | |
1781 | */ | |
1782 | static void double_rq_unlock(runqueue_t *rq1, runqueue_t *rq2) | |
1783 | __releases(rq1->lock) | |
1784 | __releases(rq2->lock) | |
1785 | { | |
1786 | spin_unlock(&rq1->lock); | |
1787 | if (rq1 != rq2) | |
1788 | spin_unlock(&rq2->lock); | |
1789 | else | |
1790 | __release(rq2->lock); | |
1791 | } | |
1792 | ||
1793 | /* | |
1794 | * double_lock_balance - lock the busiest runqueue, this_rq is locked already. | |
1795 | */ | |
1796 | static void double_lock_balance(runqueue_t *this_rq, runqueue_t *busiest) | |
1797 | __releases(this_rq->lock) | |
1798 | __acquires(busiest->lock) | |
1799 | __acquires(this_rq->lock) | |
1800 | { | |
1801 | if (unlikely(!spin_trylock(&busiest->lock))) { | |
1802 | if (busiest < this_rq) { | |
1803 | spin_unlock(&this_rq->lock); | |
1804 | spin_lock(&busiest->lock); | |
1805 | spin_lock(&this_rq->lock); | |
1806 | } else | |
1807 | spin_lock(&busiest->lock); | |
1808 | } | |
1809 | } | |
1810 | ||
1da177e4 LT |
1811 | /* |
1812 | * If dest_cpu is allowed for this process, migrate the task to it. | |
1813 | * This is accomplished by forcing the cpu_allowed mask to only | |
1814 | * allow dest_cpu, which will force the cpu onto dest_cpu. Then | |
1815 | * the cpu_allowed mask is restored. | |
1816 | */ | |
1817 | static void sched_migrate_task(task_t *p, int dest_cpu) | |
1818 | { | |
1819 | migration_req_t req; | |
1820 | runqueue_t *rq; | |
1821 | unsigned long flags; | |
1822 | ||
1823 | rq = task_rq_lock(p, &flags); | |
1824 | if (!cpu_isset(dest_cpu, p->cpus_allowed) | |
1825 | || unlikely(cpu_is_offline(dest_cpu))) | |
1826 | goto out; | |
1827 | ||
1828 | /* force the process onto the specified CPU */ | |
1829 | if (migrate_task(p, dest_cpu, &req)) { | |
1830 | /* Need to wait for migration thread (might exit: take ref). */ | |
1831 | struct task_struct *mt = rq->migration_thread; | |
1832 | get_task_struct(mt); | |
1833 | task_rq_unlock(rq, &flags); | |
1834 | wake_up_process(mt); | |
1835 | put_task_struct(mt); | |
1836 | wait_for_completion(&req.done); | |
1837 | return; | |
1838 | } | |
1839 | out: | |
1840 | task_rq_unlock(rq, &flags); | |
1841 | } | |
1842 | ||
1843 | /* | |
476d139c NP |
1844 | * sched_exec - execve() is a valuable balancing opportunity, because at |
1845 | * this point the task has the smallest effective memory and cache footprint. | |
1da177e4 LT |
1846 | */ |
1847 | void sched_exec(void) | |
1848 | { | |
1da177e4 | 1849 | int new_cpu, this_cpu = get_cpu(); |
476d139c | 1850 | new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC); |
1da177e4 | 1851 | put_cpu(); |
476d139c NP |
1852 | if (new_cpu != this_cpu) |
1853 | sched_migrate_task(current, new_cpu); | |
1da177e4 LT |
1854 | } |
1855 | ||
1856 | /* | |
1857 | * pull_task - move a task from a remote runqueue to the local runqueue. | |
1858 | * Both runqueues must be locked. | |
1859 | */ | |
1860 | static inline | |
1861 | void pull_task(runqueue_t *src_rq, prio_array_t *src_array, task_t *p, | |
1862 | runqueue_t *this_rq, prio_array_t *this_array, int this_cpu) | |
1863 | { | |
1864 | dequeue_task(p, src_array); | |
b910472d | 1865 | dec_nr_running(p, src_rq); |
1da177e4 | 1866 | set_task_cpu(p, this_cpu); |
b910472d | 1867 | inc_nr_running(p, this_rq); |
1da177e4 LT |
1868 | enqueue_task(p, this_array); |
1869 | p->timestamp = (p->timestamp - src_rq->timestamp_last_tick) | |
1870 | + this_rq->timestamp_last_tick; | |
1871 | /* | |
1872 | * Note that idle threads have a prio of MAX_PRIO, for this test | |
1873 | * to be always true for them. | |
1874 | */ | |
1875 | if (TASK_PREEMPTS_CURR(p, this_rq)) | |
1876 | resched_task(this_rq->curr); | |
1877 | } | |
1878 | ||
1879 | /* | |
1880 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? | |
1881 | */ | |
1882 | static inline | |
1883 | int can_migrate_task(task_t *p, runqueue_t *rq, int this_cpu, | |
95cdf3b7 IM |
1884 | struct sched_domain *sd, enum idle_type idle, |
1885 | int *all_pinned) | |
1da177e4 LT |
1886 | { |
1887 | /* | |
1888 | * We do not migrate tasks that are: | |
1889 | * 1) running (obviously), or | |
1890 | * 2) cannot be migrated to this CPU due to cpus_allowed, or | |
1891 | * 3) are cache-hot on their current CPU. | |
1892 | */ | |
1da177e4 LT |
1893 | if (!cpu_isset(this_cpu, p->cpus_allowed)) |
1894 | return 0; | |
81026794 NP |
1895 | *all_pinned = 0; |
1896 | ||
1897 | if (task_running(rq, p)) | |
1898 | return 0; | |
1da177e4 LT |
1899 | |
1900 | /* | |
1901 | * Aggressive migration if: | |
cafb20c1 | 1902 | * 1) task is cache cold, or |
1da177e4 LT |
1903 | * 2) too many balance attempts have failed. |
1904 | */ | |
1905 | ||
cafb20c1 | 1906 | if (sd->nr_balance_failed > sd->cache_nice_tries) |
1da177e4 LT |
1907 | return 1; |
1908 | ||
1909 | if (task_hot(p, rq->timestamp_last_tick, sd)) | |
81026794 | 1910 | return 0; |
1da177e4 LT |
1911 | return 1; |
1912 | } | |
1913 | ||
1914 | /* | |
1915 | * move_tasks tries to move up to max_nr_move tasks from busiest to this_rq, | |
1916 | * as part of a balancing operation within "domain". Returns the number of | |
1917 | * tasks moved. | |
1918 | * | |
1919 | * Called with both runqueues locked. | |
1920 | */ | |
1921 | static int move_tasks(runqueue_t *this_rq, int this_cpu, runqueue_t *busiest, | |
1922 | unsigned long max_nr_move, struct sched_domain *sd, | |
81026794 | 1923 | enum idle_type idle, int *all_pinned) |
1da177e4 LT |
1924 | { |
1925 | prio_array_t *array, *dst_array; | |
1926 | struct list_head *head, *curr; | |
81026794 | 1927 | int idx, pulled = 0, pinned = 0; |
1da177e4 LT |
1928 | task_t *tmp; |
1929 | ||
81026794 | 1930 | if (max_nr_move == 0) |
1da177e4 LT |
1931 | goto out; |
1932 | ||
81026794 NP |
1933 | pinned = 1; |
1934 | ||
1da177e4 LT |
1935 | /* |
1936 | * We first consider expired tasks. Those will likely not be | |
1937 | * executed in the near future, and they are most likely to | |
1938 | * be cache-cold, thus switching CPUs has the least effect | |
1939 | * on them. | |
1940 | */ | |
1941 | if (busiest->expired->nr_active) { | |
1942 | array = busiest->expired; | |
1943 | dst_array = this_rq->expired; | |
1944 | } else { | |
1945 | array = busiest->active; | |
1946 | dst_array = this_rq->active; | |
1947 | } | |
1948 | ||
1949 | new_array: | |
1950 | /* Start searching at priority 0: */ | |
1951 | idx = 0; | |
1952 | skip_bitmap: | |
1953 | if (!idx) | |
1954 | idx = sched_find_first_bit(array->bitmap); | |
1955 | else | |
1956 | idx = find_next_bit(array->bitmap, MAX_PRIO, idx); | |
1957 | if (idx >= MAX_PRIO) { | |
1958 | if (array == busiest->expired && busiest->active->nr_active) { | |
1959 | array = busiest->active; | |
1960 | dst_array = this_rq->active; | |
1961 | goto new_array; | |
1962 | } | |
1963 | goto out; | |
1964 | } | |
1965 | ||
1966 | head = array->queue + idx; | |
1967 | curr = head->prev; | |
1968 | skip_queue: | |
1969 | tmp = list_entry(curr, task_t, run_list); | |
1970 | ||
1971 | curr = curr->prev; | |
1972 | ||
81026794 | 1973 | if (!can_migrate_task(tmp, busiest, this_cpu, sd, idle, &pinned)) { |
1da177e4 LT |
1974 | if (curr != head) |
1975 | goto skip_queue; | |
1976 | idx++; | |
1977 | goto skip_bitmap; | |
1978 | } | |
1979 | ||
1980 | #ifdef CONFIG_SCHEDSTATS | |
1981 | if (task_hot(tmp, busiest->timestamp_last_tick, sd)) | |
1982 | schedstat_inc(sd, lb_hot_gained[idle]); | |
1983 | #endif | |
1984 | ||
1985 | pull_task(busiest, array, tmp, this_rq, dst_array, this_cpu); | |
1986 | pulled++; | |
1987 | ||
1988 | /* We only want to steal up to the prescribed number of tasks. */ | |
1989 | if (pulled < max_nr_move) { | |
1990 | if (curr != head) | |
1991 | goto skip_queue; | |
1992 | idx++; | |
1993 | goto skip_bitmap; | |
1994 | } | |
1995 | out: | |
1996 | /* | |
1997 | * Right now, this is the only place pull_task() is called, | |
1998 | * so we can safely collect pull_task() stats here rather than | |
1999 | * inside pull_task(). | |
2000 | */ | |
2001 | schedstat_add(sd, lb_gained[idle], pulled); | |
81026794 NP |
2002 | |
2003 | if (all_pinned) | |
2004 | *all_pinned = pinned; | |
1da177e4 LT |
2005 | return pulled; |
2006 | } | |
2007 | ||
2008 | /* | |
2009 | * find_busiest_group finds and returns the busiest CPU group within the | |
2010 | * domain. It calculates and returns the number of tasks which should be | |
2011 | * moved to restore balance via the imbalance parameter. | |
2012 | */ | |
2013 | static struct sched_group * | |
2014 | find_busiest_group(struct sched_domain *sd, int this_cpu, | |
5969fe06 | 2015 | unsigned long *imbalance, enum idle_type idle, int *sd_idle) |
1da177e4 LT |
2016 | { |
2017 | struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups; | |
2018 | unsigned long max_load, avg_load, total_load, this_load, total_pwr; | |
0c117f1b | 2019 | unsigned long max_pull; |
7897986b | 2020 | int load_idx; |
1da177e4 LT |
2021 | |
2022 | max_load = this_load = total_load = total_pwr = 0; | |
7897986b NP |
2023 | if (idle == NOT_IDLE) |
2024 | load_idx = sd->busy_idx; | |
2025 | else if (idle == NEWLY_IDLE) | |
2026 | load_idx = sd->newidle_idx; | |
2027 | else | |
2028 | load_idx = sd->idle_idx; | |
1da177e4 LT |
2029 | |
2030 | do { | |
2031 | unsigned long load; | |
2032 | int local_group; | |
2033 | int i; | |
2034 | ||
2035 | local_group = cpu_isset(this_cpu, group->cpumask); | |
2036 | ||
2037 | /* Tally up the load of all CPUs in the group */ | |
2038 | avg_load = 0; | |
2039 | ||
2040 | for_each_cpu_mask(i, group->cpumask) { | |
5969fe06 NP |
2041 | if (*sd_idle && !idle_cpu(i)) |
2042 | *sd_idle = 0; | |
2043 | ||
1da177e4 LT |
2044 | /* Bias balancing toward cpus of our domain */ |
2045 | if (local_group) | |
b910472d | 2046 | load = __target_load(i, load_idx, idle); |
1da177e4 | 2047 | else |
b910472d | 2048 | load = __source_load(i, load_idx, idle); |
1da177e4 LT |
2049 | |
2050 | avg_load += load; | |
2051 | } | |
2052 | ||
2053 | total_load += avg_load; | |
2054 | total_pwr += group->cpu_power; | |
2055 | ||
2056 | /* Adjust by relative CPU power of the group */ | |
2057 | avg_load = (avg_load * SCHED_LOAD_SCALE) / group->cpu_power; | |
2058 | ||
2059 | if (local_group) { | |
2060 | this_load = avg_load; | |
2061 | this = group; | |
1da177e4 LT |
2062 | } else if (avg_load > max_load) { |
2063 | max_load = avg_load; | |
2064 | busiest = group; | |
2065 | } | |
1da177e4 LT |
2066 | group = group->next; |
2067 | } while (group != sd->groups); | |
2068 | ||
0c117f1b | 2069 | if (!busiest || this_load >= max_load || max_load <= SCHED_LOAD_SCALE) |
1da177e4 LT |
2070 | goto out_balanced; |
2071 | ||
2072 | avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr; | |
2073 | ||
2074 | if (this_load >= avg_load || | |
2075 | 100*max_load <= sd->imbalance_pct*this_load) | |
2076 | goto out_balanced; | |
2077 | ||
2078 | /* | |
2079 | * We're trying to get all the cpus to the average_load, so we don't | |
2080 | * want to push ourselves above the average load, nor do we wish to | |
2081 | * reduce the max loaded cpu below the average load, as either of these | |
2082 | * actions would just result in more rebalancing later, and ping-pong | |
2083 | * tasks around. Thus we look for the minimum possible imbalance. | |
2084 | * Negative imbalances (*we* are more loaded than anyone else) will | |
2085 | * be counted as no imbalance for these purposes -- we can't fix that | |
2086 | * by pulling tasks to us. Be careful of negative numbers as they'll | |
2087 | * appear as very large values with unsigned longs. | |
2088 | */ | |
0c117f1b SS |
2089 | |
2090 | /* Don't want to pull so many tasks that a group would go idle */ | |
2091 | max_pull = min(max_load - avg_load, max_load - SCHED_LOAD_SCALE); | |
2092 | ||
1da177e4 | 2093 | /* How much load to actually move to equalise the imbalance */ |
0c117f1b | 2094 | *imbalance = min(max_pull * busiest->cpu_power, |
1da177e4 LT |
2095 | (avg_load - this_load) * this->cpu_power) |
2096 | / SCHED_LOAD_SCALE; | |
2097 | ||
2098 | if (*imbalance < SCHED_LOAD_SCALE) { | |
2099 | unsigned long pwr_now = 0, pwr_move = 0; | |
2100 | unsigned long tmp; | |
2101 | ||
2102 | if (max_load - this_load >= SCHED_LOAD_SCALE*2) { | |
2103 | *imbalance = 1; | |
2104 | return busiest; | |
2105 | } | |
2106 | ||
2107 | /* | |
2108 | * OK, we don't have enough imbalance to justify moving tasks, | |
2109 | * however we may be able to increase total CPU power used by | |
2110 | * moving them. | |
2111 | */ | |
2112 | ||
2113 | pwr_now += busiest->cpu_power*min(SCHED_LOAD_SCALE, max_load); | |
2114 | pwr_now += this->cpu_power*min(SCHED_LOAD_SCALE, this_load); | |
2115 | pwr_now /= SCHED_LOAD_SCALE; | |
2116 | ||
2117 | /* Amount of load we'd subtract */ | |
2118 | tmp = SCHED_LOAD_SCALE*SCHED_LOAD_SCALE/busiest->cpu_power; | |
2119 | if (max_load > tmp) | |
2120 | pwr_move += busiest->cpu_power*min(SCHED_LOAD_SCALE, | |
2121 | max_load - tmp); | |
2122 | ||
2123 | /* Amount of load we'd add */ | |
2124 | if (max_load*busiest->cpu_power < | |
2125 | SCHED_LOAD_SCALE*SCHED_LOAD_SCALE) | |
2126 | tmp = max_load*busiest->cpu_power/this->cpu_power; | |
2127 | else | |
2128 | tmp = SCHED_LOAD_SCALE*SCHED_LOAD_SCALE/this->cpu_power; | |
2129 | pwr_move += this->cpu_power*min(SCHED_LOAD_SCALE, this_load + tmp); | |
2130 | pwr_move /= SCHED_LOAD_SCALE; | |
2131 | ||
2132 | /* Move if we gain throughput */ | |
2133 | if (pwr_move <= pwr_now) | |
2134 | goto out_balanced; | |
2135 | ||
2136 | *imbalance = 1; | |
2137 | return busiest; | |
2138 | } | |
2139 | ||
2140 | /* Get rid of the scaling factor, rounding down as we divide */ | |
2141 | *imbalance = *imbalance / SCHED_LOAD_SCALE; | |
1da177e4 LT |
2142 | return busiest; |
2143 | ||
2144 | out_balanced: | |
1da177e4 LT |
2145 | |
2146 | *imbalance = 0; | |
2147 | return NULL; | |
2148 | } | |
2149 | ||
2150 | /* | |
2151 | * find_busiest_queue - find the busiest runqueue among the cpus in group. | |
2152 | */ | |
b910472d CK |
2153 | static runqueue_t *find_busiest_queue(struct sched_group *group, |
2154 | enum idle_type idle) | |
1da177e4 LT |
2155 | { |
2156 | unsigned long load, max_load = 0; | |
2157 | runqueue_t *busiest = NULL; | |
2158 | int i; | |
2159 | ||
2160 | for_each_cpu_mask(i, group->cpumask) { | |
b910472d | 2161 | load = __source_load(i, 0, idle); |
1da177e4 LT |
2162 | |
2163 | if (load > max_load) { | |
2164 | max_load = load; | |
2165 | busiest = cpu_rq(i); | |
2166 | } | |
2167 | } | |
2168 | ||
2169 | return busiest; | |
2170 | } | |
2171 | ||
77391d71 NP |
2172 | /* |
2173 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but | |
2174 | * so long as it is large enough. | |
2175 | */ | |
2176 | #define MAX_PINNED_INTERVAL 512 | |
2177 | ||
1da177e4 LT |
2178 | /* |
2179 | * Check this_cpu to ensure it is balanced within domain. Attempt to move | |
2180 | * tasks if there is an imbalance. | |
2181 | * | |
2182 | * Called with this_rq unlocked. | |
2183 | */ | |
2184 | static int load_balance(int this_cpu, runqueue_t *this_rq, | |
2185 | struct sched_domain *sd, enum idle_type idle) | |
2186 | { | |
2187 | struct sched_group *group; | |
2188 | runqueue_t *busiest; | |
2189 | unsigned long imbalance; | |
77391d71 | 2190 | int nr_moved, all_pinned = 0; |
81026794 | 2191 | int active_balance = 0; |
5969fe06 NP |
2192 | int sd_idle = 0; |
2193 | ||
2194 | if (idle != NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER) | |
2195 | sd_idle = 1; | |
1da177e4 | 2196 | |
1da177e4 LT |
2197 | schedstat_inc(sd, lb_cnt[idle]); |
2198 | ||
5969fe06 | 2199 | group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle); |
1da177e4 LT |
2200 | if (!group) { |
2201 | schedstat_inc(sd, lb_nobusyg[idle]); | |
2202 | goto out_balanced; | |
2203 | } | |
2204 | ||
b910472d | 2205 | busiest = find_busiest_queue(group, idle); |
1da177e4 LT |
2206 | if (!busiest) { |
2207 | schedstat_inc(sd, lb_nobusyq[idle]); | |
2208 | goto out_balanced; | |
2209 | } | |
2210 | ||
db935dbd | 2211 | BUG_ON(busiest == this_rq); |
1da177e4 LT |
2212 | |
2213 | schedstat_add(sd, lb_imbalance[idle], imbalance); | |
2214 | ||
2215 | nr_moved = 0; | |
2216 | if (busiest->nr_running > 1) { | |
2217 | /* | |
2218 | * Attempt to move tasks. If find_busiest_group has found | |
2219 | * an imbalance but busiest->nr_running <= 1, the group is | |
2220 | * still unbalanced. nr_moved simply stays zero, so it is | |
2221 | * correctly treated as an imbalance. | |
2222 | */ | |
e17224bf | 2223 | double_rq_lock(this_rq, busiest); |
1da177e4 | 2224 | nr_moved = move_tasks(this_rq, this_cpu, busiest, |
d6d5cfaf | 2225 | imbalance, sd, idle, &all_pinned); |
e17224bf | 2226 | double_rq_unlock(this_rq, busiest); |
81026794 NP |
2227 | |
2228 | /* All tasks on this runqueue were pinned by CPU affinity */ | |
2229 | if (unlikely(all_pinned)) | |
2230 | goto out_balanced; | |
1da177e4 | 2231 | } |
81026794 | 2232 | |
1da177e4 LT |
2233 | if (!nr_moved) { |
2234 | schedstat_inc(sd, lb_failed[idle]); | |
2235 | sd->nr_balance_failed++; | |
2236 | ||
2237 | if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) { | |
1da177e4 LT |
2238 | |
2239 | spin_lock(&busiest->lock); | |
fa3b6ddc SS |
2240 | |
2241 | /* don't kick the migration_thread, if the curr | |
2242 | * task on busiest cpu can't be moved to this_cpu | |
2243 | */ | |
2244 | if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) { | |
2245 | spin_unlock(&busiest->lock); | |
2246 | all_pinned = 1; | |
2247 | goto out_one_pinned; | |
2248 | } | |
2249 | ||
1da177e4 LT |
2250 | if (!busiest->active_balance) { |
2251 | busiest->active_balance = 1; | |
2252 | busiest->push_cpu = this_cpu; | |
81026794 | 2253 | active_balance = 1; |
1da177e4 LT |
2254 | } |
2255 | spin_unlock(&busiest->lock); | |
81026794 | 2256 | if (active_balance) |
1da177e4 LT |
2257 | wake_up_process(busiest->migration_thread); |
2258 | ||
2259 | /* | |
2260 | * We've kicked active balancing, reset the failure | |
2261 | * counter. | |
2262 | */ | |
39507451 | 2263 | sd->nr_balance_failed = sd->cache_nice_tries+1; |
1da177e4 | 2264 | } |
81026794 | 2265 | } else |
1da177e4 LT |
2266 | sd->nr_balance_failed = 0; |
2267 | ||
81026794 | 2268 | if (likely(!active_balance)) { |
1da177e4 LT |
2269 | /* We were unbalanced, so reset the balancing interval */ |
2270 | sd->balance_interval = sd->min_interval; | |
81026794 NP |
2271 | } else { |
2272 | /* | |
2273 | * If we've begun active balancing, start to back off. This | |
2274 | * case may not be covered by the all_pinned logic if there | |
2275 | * is only 1 task on the busy runqueue (because we don't call | |
2276 | * move_tasks). | |
2277 | */ | |
2278 | if (sd->balance_interval < sd->max_interval) | |
2279 | sd->balance_interval *= 2; | |
1da177e4 LT |
2280 | } |
2281 | ||
5969fe06 NP |
2282 | if (!nr_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER) |
2283 | return -1; | |
1da177e4 LT |
2284 | return nr_moved; |
2285 | ||
2286 | out_balanced: | |
1da177e4 LT |
2287 | schedstat_inc(sd, lb_balanced[idle]); |
2288 | ||
16cfb1c0 | 2289 | sd->nr_balance_failed = 0; |
fa3b6ddc SS |
2290 | |
2291 | out_one_pinned: | |
1da177e4 | 2292 | /* tune up the balancing interval */ |
77391d71 NP |
2293 | if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) || |
2294 | (sd->balance_interval < sd->max_interval)) | |
1da177e4 LT |
2295 | sd->balance_interval *= 2; |
2296 | ||
5969fe06 NP |
2297 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER) |
2298 | return -1; | |
1da177e4 LT |
2299 | return 0; |
2300 | } | |
2301 | ||
2302 | /* | |
2303 | * Check this_cpu to ensure it is balanced within domain. Attempt to move | |
2304 | * tasks if there is an imbalance. | |
2305 | * | |
2306 | * Called from schedule when this_rq is about to become idle (NEWLY_IDLE). | |
2307 | * this_rq is locked. | |
2308 | */ | |
2309 | static int load_balance_newidle(int this_cpu, runqueue_t *this_rq, | |
2310 | struct sched_domain *sd) | |
2311 | { | |
2312 | struct sched_group *group; | |
2313 | runqueue_t *busiest = NULL; | |
2314 | unsigned long imbalance; | |
2315 | int nr_moved = 0; | |
5969fe06 NP |
2316 | int sd_idle = 0; |
2317 | ||
2318 | if (sd->flags & SD_SHARE_CPUPOWER) | |
2319 | sd_idle = 1; | |
1da177e4 LT |
2320 | |
2321 | schedstat_inc(sd, lb_cnt[NEWLY_IDLE]); | |
5969fe06 | 2322 | group = find_busiest_group(sd, this_cpu, &imbalance, NEWLY_IDLE, &sd_idle); |
1da177e4 | 2323 | if (!group) { |
1da177e4 | 2324 | schedstat_inc(sd, lb_nobusyg[NEWLY_IDLE]); |
16cfb1c0 | 2325 | goto out_balanced; |
1da177e4 LT |
2326 | } |
2327 | ||
b910472d | 2328 | busiest = find_busiest_queue(group, NEWLY_IDLE); |
db935dbd | 2329 | if (!busiest) { |
1da177e4 | 2330 | schedstat_inc(sd, lb_nobusyq[NEWLY_IDLE]); |
16cfb1c0 | 2331 | goto out_balanced; |
1da177e4 LT |
2332 | } |
2333 | ||
db935dbd NP |
2334 | BUG_ON(busiest == this_rq); |
2335 | ||
1da177e4 | 2336 | schedstat_add(sd, lb_imbalance[NEWLY_IDLE], imbalance); |
d6d5cfaf NP |
2337 | |
2338 | nr_moved = 0; | |
2339 | if (busiest->nr_running > 1) { | |
2340 | /* Attempt to move tasks */ | |
2341 | double_lock_balance(this_rq, busiest); | |
2342 | nr_moved = move_tasks(this_rq, this_cpu, busiest, | |
81026794 | 2343 | imbalance, sd, NEWLY_IDLE, NULL); |
d6d5cfaf NP |
2344 | spin_unlock(&busiest->lock); |
2345 | } | |
2346 | ||
5969fe06 | 2347 | if (!nr_moved) { |
1da177e4 | 2348 | schedstat_inc(sd, lb_failed[NEWLY_IDLE]); |
5969fe06 NP |
2349 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER) |
2350 | return -1; | |
2351 | } else | |
16cfb1c0 | 2352 | sd->nr_balance_failed = 0; |
1da177e4 | 2353 | |
1da177e4 | 2354 | return nr_moved; |
16cfb1c0 NP |
2355 | |
2356 | out_balanced: | |
2357 | schedstat_inc(sd, lb_balanced[NEWLY_IDLE]); | |
5969fe06 NP |
2358 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER) |
2359 | return -1; | |
16cfb1c0 NP |
2360 | sd->nr_balance_failed = 0; |
2361 | return 0; | |
1da177e4 LT |
2362 | } |
2363 | ||
2364 | /* | |
2365 | * idle_balance is called by schedule() if this_cpu is about to become | |
2366 | * idle. Attempts to pull tasks from other CPUs. | |
2367 | */ | |
2368 | static inline void idle_balance(int this_cpu, runqueue_t *this_rq) | |
2369 | { | |
2370 | struct sched_domain *sd; | |
2371 | ||
2372 | for_each_domain(this_cpu, sd) { | |
2373 | if (sd->flags & SD_BALANCE_NEWIDLE) { | |
2374 | if (load_balance_newidle(this_cpu, this_rq, sd)) { | |
2375 | /* We've pulled tasks over so stop searching */ | |
2376 | break; | |
2377 | } | |
2378 | } | |
2379 | } | |
2380 | } | |
2381 | ||
2382 | /* | |
2383 | * active_load_balance is run by migration threads. It pushes running tasks | |
2384 | * off the busiest CPU onto idle CPUs. It requires at least 1 task to be | |
2385 | * running on each physical CPU where possible, and avoids physical / | |
2386 | * logical imbalances. | |
2387 | * | |
2388 | * Called with busiest_rq locked. | |
2389 | */ | |
2390 | static void active_load_balance(runqueue_t *busiest_rq, int busiest_cpu) | |
2391 | { | |
2392 | struct sched_domain *sd; | |
1da177e4 | 2393 | runqueue_t *target_rq; |
39507451 NP |
2394 | int target_cpu = busiest_rq->push_cpu; |
2395 | ||
2396 | if (busiest_rq->nr_running <= 1) | |
2397 | /* no task to move */ | |
2398 | return; | |
2399 | ||
2400 | target_rq = cpu_rq(target_cpu); | |
1da177e4 LT |
2401 | |
2402 | /* | |
39507451 NP |
2403 | * This condition is "impossible", if it occurs |
2404 | * we need to fix it. Originally reported by | |
2405 | * Bjorn Helgaas on a 128-cpu setup. | |
1da177e4 | 2406 | */ |
39507451 | 2407 | BUG_ON(busiest_rq == target_rq); |
1da177e4 | 2408 | |
39507451 NP |
2409 | /* move a task from busiest_rq to target_rq */ |
2410 | double_lock_balance(busiest_rq, target_rq); | |
2411 | ||
2412 | /* Search for an sd spanning us and the target CPU. */ | |
2413 | for_each_domain(target_cpu, sd) | |
2414 | if ((sd->flags & SD_LOAD_BALANCE) && | |
2415 | cpu_isset(busiest_cpu, sd->span)) | |
2416 | break; | |
2417 | ||
2418 | if (unlikely(sd == NULL)) | |
2419 | goto out; | |
2420 | ||
2421 | schedstat_inc(sd, alb_cnt); | |
2422 | ||
2423 | if (move_tasks(target_rq, target_cpu, busiest_rq, 1, sd, SCHED_IDLE, NULL)) | |
2424 | schedstat_inc(sd, alb_pushed); | |
2425 | else | |
2426 | schedstat_inc(sd, alb_failed); | |
2427 | out: | |
2428 | spin_unlock(&target_rq->lock); | |
1da177e4 LT |
2429 | } |
2430 | ||
2431 | /* | |
2432 | * rebalance_tick will get called every timer tick, on every CPU. | |
2433 | * | |
2434 | * It checks each scheduling domain to see if it is due to be balanced, | |
2435 | * and initiates a balancing operation if so. | |
2436 | * | |
2437 | * Balancing parameters are set up in arch_init_sched_domains. | |
2438 | */ | |
2439 | ||
2440 | /* Don't have all balancing operations going off at once */ | |
2441 | #define CPU_OFFSET(cpu) (HZ * cpu / NR_CPUS) | |
2442 | ||
2443 | static void rebalance_tick(int this_cpu, runqueue_t *this_rq, | |
2444 | enum idle_type idle) | |
2445 | { | |
2446 | unsigned long old_load, this_load; | |
2447 | unsigned long j = jiffies + CPU_OFFSET(this_cpu); | |
2448 | struct sched_domain *sd; | |
7897986b | 2449 | int i; |
1da177e4 | 2450 | |
1da177e4 | 2451 | this_load = this_rq->nr_running * SCHED_LOAD_SCALE; |
7897986b NP |
2452 | /* Update our load */ |
2453 | for (i = 0; i < 3; i++) { | |
2454 | unsigned long new_load = this_load; | |
2455 | int scale = 1 << i; | |
2456 | old_load = this_rq->cpu_load[i]; | |
2457 | /* | |
2458 | * Round up the averaging division if load is increasing. This | |
2459 | * prevents us from getting stuck on 9 if the load is 10, for | |
2460 | * example. | |
2461 | */ | |
2462 | if (new_load > old_load) | |
2463 | new_load += scale-1; | |
2464 | this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) / scale; | |
2465 | } | |
1da177e4 LT |
2466 | |
2467 | for_each_domain(this_cpu, sd) { | |
2468 | unsigned long interval; | |
2469 | ||
2470 | if (!(sd->flags & SD_LOAD_BALANCE)) | |
2471 | continue; | |
2472 | ||
2473 | interval = sd->balance_interval; | |
2474 | if (idle != SCHED_IDLE) | |
2475 | interval *= sd->busy_factor; | |
2476 | ||
2477 | /* scale ms to jiffies */ | |
2478 | interval = msecs_to_jiffies(interval); | |
2479 | if (unlikely(!interval)) | |
2480 | interval = 1; | |
2481 | ||
2482 | if (j - sd->last_balance >= interval) { | |
2483 | if (load_balance(this_cpu, this_rq, sd, idle)) { | |
fa3b6ddc SS |
2484 | /* |
2485 | * We've pulled tasks over so either we're no | |
5969fe06 NP |
2486 | * longer idle, or one of our SMT siblings is |
2487 | * not idle. | |
2488 | */ | |
1da177e4 LT |
2489 | idle = NOT_IDLE; |
2490 | } | |
2491 | sd->last_balance += interval; | |
2492 | } | |
2493 | } | |
2494 | } | |
2495 | #else | |
2496 | /* | |
2497 | * on UP we do not need to balance between CPUs: | |
2498 | */ | |
2499 | static inline void rebalance_tick(int cpu, runqueue_t *rq, enum idle_type idle) | |
2500 | { | |
2501 | } | |
2502 | static inline void idle_balance(int cpu, runqueue_t *rq) | |
2503 | { | |
2504 | } | |
2505 | #endif | |
2506 | ||
2507 | static inline int wake_priority_sleeper(runqueue_t *rq) | |
2508 | { | |
2509 | int ret = 0; | |
2510 | #ifdef CONFIG_SCHED_SMT | |
2511 | spin_lock(&rq->lock); | |
2512 | /* | |
2513 | * If an SMT sibling task has been put to sleep for priority | |
2514 | * reasons reschedule the idle task to see if it can now run. | |
2515 | */ | |
2516 | if (rq->nr_running) { | |
2517 | resched_task(rq->idle); | |
2518 | ret = 1; | |
2519 | } | |
2520 | spin_unlock(&rq->lock); | |
2521 | #endif | |
2522 | return ret; | |
2523 | } | |
2524 | ||
2525 | DEFINE_PER_CPU(struct kernel_stat, kstat); | |
2526 | ||
2527 | EXPORT_PER_CPU_SYMBOL(kstat); | |
2528 | ||
2529 | /* | |
2530 | * This is called on clock ticks and on context switches. | |
2531 | * Bank in p->sched_time the ns elapsed since the last tick or switch. | |
2532 | */ | |
2533 | static inline void update_cpu_clock(task_t *p, runqueue_t *rq, | |
2534 | unsigned long long now) | |
2535 | { | |
2536 | unsigned long long last = max(p->timestamp, rq->timestamp_last_tick); | |
2537 | p->sched_time += now - last; | |
2538 | } | |
2539 | ||
2540 | /* | |
2541 | * Return current->sched_time plus any more ns on the sched_clock | |
2542 | * that have not yet been banked. | |
2543 | */ | |
2544 | unsigned long long current_sched_time(const task_t *tsk) | |
2545 | { | |
2546 | unsigned long long ns; | |
2547 | unsigned long flags; | |
2548 | local_irq_save(flags); | |
2549 | ns = max(tsk->timestamp, task_rq(tsk)->timestamp_last_tick); | |
2550 | ns = tsk->sched_time + (sched_clock() - ns); | |
2551 | local_irq_restore(flags); | |
2552 | return ns; | |
2553 | } | |
2554 | ||
2555 | /* | |
2556 | * We place interactive tasks back into the active array, if possible. | |
2557 | * | |
2558 | * To guarantee that this does not starve expired tasks we ignore the | |
2559 | * interactivity of a task if the first expired task had to wait more | |
2560 | * than a 'reasonable' amount of time. This deadline timeout is | |
2561 | * load-dependent, as the frequency of array switched decreases with | |
2562 | * increasing number of running tasks. We also ignore the interactivity | |
2563 | * if a better static_prio task has expired: | |
2564 | */ | |
2565 | #define EXPIRED_STARVING(rq) \ | |
2566 | ((STARVATION_LIMIT && ((rq)->expired_timestamp && \ | |
2567 | (jiffies - (rq)->expired_timestamp >= \ | |
2568 | STARVATION_LIMIT * ((rq)->nr_running) + 1))) || \ | |
2569 | ((rq)->curr->static_prio > (rq)->best_expired_prio)) | |
2570 | ||
2571 | /* | |
2572 | * Account user cpu time to a process. | |
2573 | * @p: the process that the cpu time gets accounted to | |
2574 | * @hardirq_offset: the offset to subtract from hardirq_count() | |
2575 | * @cputime: the cpu time spent in user space since the last update | |
2576 | */ | |
2577 | void account_user_time(struct task_struct *p, cputime_t cputime) | |
2578 | { | |
2579 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | |
2580 | cputime64_t tmp; | |
2581 | ||
2582 | p->utime = cputime_add(p->utime, cputime); | |
2583 | ||
2584 | /* Add user time to cpustat. */ | |
2585 | tmp = cputime_to_cputime64(cputime); | |
2586 | if (TASK_NICE(p) > 0) | |
2587 | cpustat->nice = cputime64_add(cpustat->nice, tmp); | |
2588 | else | |
2589 | cpustat->user = cputime64_add(cpustat->user, tmp); | |
2590 | } | |
2591 | ||
2592 | /* | |
2593 | * Account system cpu time to a process. | |
2594 | * @p: the process that the cpu time gets accounted to | |
2595 | * @hardirq_offset: the offset to subtract from hardirq_count() | |
2596 | * @cputime: the cpu time spent in kernel space since the last update | |
2597 | */ | |
2598 | void account_system_time(struct task_struct *p, int hardirq_offset, | |
2599 | cputime_t cputime) | |
2600 | { | |
2601 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | |
2602 | runqueue_t *rq = this_rq(); | |
2603 | cputime64_t tmp; | |
2604 | ||
2605 | p->stime = cputime_add(p->stime, cputime); | |
2606 | ||
2607 | /* Add system time to cpustat. */ | |
2608 | tmp = cputime_to_cputime64(cputime); | |
2609 | if (hardirq_count() - hardirq_offset) | |
2610 | cpustat->irq = cputime64_add(cpustat->irq, tmp); | |
2611 | else if (softirq_count()) | |
2612 | cpustat->softirq = cputime64_add(cpustat->softirq, tmp); | |
2613 | else if (p != rq->idle) | |
2614 | cpustat->system = cputime64_add(cpustat->system, tmp); | |
2615 | else if (atomic_read(&rq->nr_iowait) > 0) | |
2616 | cpustat->iowait = cputime64_add(cpustat->iowait, tmp); | |
2617 | else | |
2618 | cpustat->idle = cputime64_add(cpustat->idle, tmp); | |
2619 | /* Account for system time used */ | |
2620 | acct_update_integrals(p); | |
1da177e4 LT |
2621 | } |
2622 | ||
2623 | /* | |
2624 | * Account for involuntary wait time. | |
2625 | * @p: the process from which the cpu time has been stolen | |
2626 | * @steal: the cpu time spent in involuntary wait | |
2627 | */ | |
2628 | void account_steal_time(struct task_struct *p, cputime_t steal) | |
2629 | { | |
2630 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | |
2631 | cputime64_t tmp = cputime_to_cputime64(steal); | |
2632 | runqueue_t *rq = this_rq(); | |
2633 | ||
2634 | if (p == rq->idle) { | |
2635 | p->stime = cputime_add(p->stime, steal); | |
2636 | if (atomic_read(&rq->nr_iowait) > 0) | |
2637 | cpustat->iowait = cputime64_add(cpustat->iowait, tmp); | |
2638 | else | |
2639 | cpustat->idle = cputime64_add(cpustat->idle, tmp); | |
2640 | } else | |
2641 | cpustat->steal = cputime64_add(cpustat->steal, tmp); | |
2642 | } | |
2643 | ||
2644 | /* | |
2645 | * This function gets called by the timer code, with HZ frequency. | |
2646 | * We call it with interrupts disabled. | |
2647 | * | |
2648 | * It also gets called by the fork code, when changing the parent's | |
2649 | * timeslices. | |
2650 | */ | |
2651 | void scheduler_tick(void) | |
2652 | { | |
2653 | int cpu = smp_processor_id(); | |
2654 | runqueue_t *rq = this_rq(); | |
2655 | task_t *p = current; | |
2656 | unsigned long long now = sched_clock(); | |
2657 | ||
2658 | update_cpu_clock(p, rq, now); | |
2659 | ||
2660 | rq->timestamp_last_tick = now; | |
2661 | ||
2662 | if (p == rq->idle) { | |
2663 | if (wake_priority_sleeper(rq)) | |
2664 | goto out; | |
2665 | rebalance_tick(cpu, rq, SCHED_IDLE); | |
2666 | return; | |
2667 | } | |
2668 | ||
2669 | /* Task might have expired already, but not scheduled off yet */ | |
2670 | if (p->array != rq->active) { | |
2671 | set_tsk_need_resched(p); | |
2672 | goto out; | |
2673 | } | |
2674 | spin_lock(&rq->lock); | |
2675 | /* | |
2676 | * The task was running during this tick - update the | |
2677 | * time slice counter. Note: we do not update a thread's | |
2678 | * priority until it either goes to sleep or uses up its | |
2679 | * timeslice. This makes it possible for interactive tasks | |
2680 | * to use up their timeslices at their highest priority levels. | |
2681 | */ | |
2682 | if (rt_task(p)) { | |
2683 | /* | |
2684 | * RR tasks need a special form of timeslice management. | |
2685 | * FIFO tasks have no timeslices. | |
2686 | */ | |
2687 | if ((p->policy == SCHED_RR) && !--p->time_slice) { | |
2688 | p->time_slice = task_timeslice(p); | |
2689 | p->first_time_slice = 0; | |
2690 | set_tsk_need_resched(p); | |
2691 | ||
2692 | /* put it at the end of the queue: */ | |
2693 | requeue_task(p, rq->active); | |
2694 | } | |
2695 | goto out_unlock; | |
2696 | } | |
2697 | if (!--p->time_slice) { | |
2698 | dequeue_task(p, rq->active); | |
2699 | set_tsk_need_resched(p); | |
2700 | p->prio = effective_prio(p); | |
2701 | p->time_slice = task_timeslice(p); | |
2702 | p->first_time_slice = 0; | |
2703 | ||
2704 | if (!rq->expired_timestamp) | |
2705 | rq->expired_timestamp = jiffies; | |
2706 | if (!TASK_INTERACTIVE(p) || EXPIRED_STARVING(rq)) { | |
2707 | enqueue_task(p, rq->expired); | |
2708 | if (p->static_prio < rq->best_expired_prio) | |
2709 | rq->best_expired_prio = p->static_prio; | |
2710 | } else | |
2711 | enqueue_task(p, rq->active); | |
2712 | } else { | |
2713 | /* | |
2714 | * Prevent a too long timeslice allowing a task to monopolize | |
2715 | * the CPU. We do this by splitting up the timeslice into | |
2716 | * smaller pieces. | |
2717 | * | |
2718 | * Note: this does not mean the task's timeslices expire or | |
2719 | * get lost in any way, they just might be preempted by | |
2720 | * another task of equal priority. (one with higher | |
2721 | * priority would have preempted this task already.) We | |
2722 | * requeue this task to the end of the list on this priority | |
2723 | * level, which is in essence a round-robin of tasks with | |
2724 | * equal priority. | |
2725 | * | |
2726 | * This only applies to tasks in the interactive | |
2727 | * delta range with at least TIMESLICE_GRANULARITY to requeue. | |
2728 | */ | |
2729 | if (TASK_INTERACTIVE(p) && !((task_timeslice(p) - | |
2730 | p->time_slice) % TIMESLICE_GRANULARITY(p)) && | |
2731 | (p->time_slice >= TIMESLICE_GRANULARITY(p)) && | |
2732 | (p->array == rq->active)) { | |
2733 | ||
2734 | requeue_task(p, rq->active); | |
2735 | set_tsk_need_resched(p); | |
2736 | } | |
2737 | } | |
2738 | out_unlock: | |
2739 | spin_unlock(&rq->lock); | |
2740 | out: | |
2741 | rebalance_tick(cpu, rq, NOT_IDLE); | |
2742 | } | |
2743 | ||
2744 | #ifdef CONFIG_SCHED_SMT | |
fc38ed75 CK |
2745 | static inline void wakeup_busy_runqueue(runqueue_t *rq) |
2746 | { | |
2747 | /* If an SMT runqueue is sleeping due to priority reasons wake it up */ | |
2748 | if (rq->curr == rq->idle && rq->nr_running) | |
2749 | resched_task(rq->idle); | |
2750 | } | |
2751 | ||
1da177e4 LT |
2752 | static inline void wake_sleeping_dependent(int this_cpu, runqueue_t *this_rq) |
2753 | { | |
41c7ce9a | 2754 | struct sched_domain *tmp, *sd = NULL; |
1da177e4 LT |
2755 | cpumask_t sibling_map; |
2756 | int i; | |
2757 | ||
41c7ce9a NP |
2758 | for_each_domain(this_cpu, tmp) |
2759 | if (tmp->flags & SD_SHARE_CPUPOWER) | |
2760 | sd = tmp; | |
2761 | ||
2762 | if (!sd) | |
1da177e4 LT |
2763 | return; |
2764 | ||
2765 | /* | |
2766 | * Unlock the current runqueue because we have to lock in | |
2767 | * CPU order to avoid deadlocks. Caller knows that we might | |
2768 | * unlock. We keep IRQs disabled. | |
2769 | */ | |
2770 | spin_unlock(&this_rq->lock); | |
2771 | ||
2772 | sibling_map = sd->span; | |
2773 | ||
2774 | for_each_cpu_mask(i, sibling_map) | |
2775 | spin_lock(&cpu_rq(i)->lock); | |
2776 | /* | |
2777 | * We clear this CPU from the mask. This both simplifies the | |
2778 | * inner loop and keps this_rq locked when we exit: | |
2779 | */ | |
2780 | cpu_clear(this_cpu, sibling_map); | |
2781 | ||
2782 | for_each_cpu_mask(i, sibling_map) { | |
2783 | runqueue_t *smt_rq = cpu_rq(i); | |
2784 | ||
fc38ed75 | 2785 | wakeup_busy_runqueue(smt_rq); |
1da177e4 LT |
2786 | } |
2787 | ||
2788 | for_each_cpu_mask(i, sibling_map) | |
2789 | spin_unlock(&cpu_rq(i)->lock); | |
2790 | /* | |
2791 | * We exit with this_cpu's rq still held and IRQs | |
2792 | * still disabled: | |
2793 | */ | |
2794 | } | |
2795 | ||
67f9a619 IM |
2796 | /* |
2797 | * number of 'lost' timeslices this task wont be able to fully | |
2798 | * utilize, if another task runs on a sibling. This models the | |
2799 | * slowdown effect of other tasks running on siblings: | |
2800 | */ | |
2801 | static inline unsigned long smt_slice(task_t *p, struct sched_domain *sd) | |
2802 | { | |
2803 | return p->time_slice * (100 - sd->per_cpu_gain) / 100; | |
2804 | } | |
2805 | ||
1da177e4 LT |
2806 | static inline int dependent_sleeper(int this_cpu, runqueue_t *this_rq) |
2807 | { | |
41c7ce9a | 2808 | struct sched_domain *tmp, *sd = NULL; |
1da177e4 LT |
2809 | cpumask_t sibling_map; |
2810 | prio_array_t *array; | |
2811 | int ret = 0, i; | |
2812 | task_t *p; | |
2813 | ||
41c7ce9a NP |
2814 | for_each_domain(this_cpu, tmp) |
2815 | if (tmp->flags & SD_SHARE_CPUPOWER) | |
2816 | sd = tmp; | |
2817 | ||
2818 | if (!sd) | |
1da177e4 LT |
2819 | return 0; |
2820 | ||
2821 | /* | |
2822 | * The same locking rules and details apply as for | |
2823 | * wake_sleeping_dependent(): | |
2824 | */ | |
2825 | spin_unlock(&this_rq->lock); | |
2826 | sibling_map = sd->span; | |
2827 | for_each_cpu_mask(i, sibling_map) | |
2828 | spin_lock(&cpu_rq(i)->lock); | |
2829 | cpu_clear(this_cpu, sibling_map); | |
2830 | ||
2831 | /* | |
2832 | * Establish next task to be run - it might have gone away because | |
2833 | * we released the runqueue lock above: | |
2834 | */ | |
2835 | if (!this_rq->nr_running) | |
2836 | goto out_unlock; | |
2837 | array = this_rq->active; | |
2838 | if (!array->nr_active) | |
2839 | array = this_rq->expired; | |
2840 | BUG_ON(!array->nr_active); | |
2841 | ||
2842 | p = list_entry(array->queue[sched_find_first_bit(array->bitmap)].next, | |
2843 | task_t, run_list); | |
2844 | ||
2845 | for_each_cpu_mask(i, sibling_map) { | |
2846 | runqueue_t *smt_rq = cpu_rq(i); | |
2847 | task_t *smt_curr = smt_rq->curr; | |
2848 | ||
fc38ed75 CK |
2849 | /* Kernel threads do not participate in dependent sleeping */ |
2850 | if (!p->mm || !smt_curr->mm || rt_task(p)) | |
2851 | goto check_smt_task; | |
2852 | ||
1da177e4 LT |
2853 | /* |
2854 | * If a user task with lower static priority than the | |
2855 | * running task on the SMT sibling is trying to schedule, | |
2856 | * delay it till there is proportionately less timeslice | |
2857 | * left of the sibling task to prevent a lower priority | |
2858 | * task from using an unfair proportion of the | |
2859 | * physical cpu's resources. -ck | |
2860 | */ | |
fc38ed75 CK |
2861 | if (rt_task(smt_curr)) { |
2862 | /* | |
2863 | * With real time tasks we run non-rt tasks only | |
2864 | * per_cpu_gain% of the time. | |
2865 | */ | |
2866 | if ((jiffies % DEF_TIMESLICE) > | |
2867 | (sd->per_cpu_gain * DEF_TIMESLICE / 100)) | |
2868 | ret = 1; | |
2869 | } else | |
67f9a619 IM |
2870 | if (smt_curr->static_prio < p->static_prio && |
2871 | !TASK_PREEMPTS_CURR(p, smt_rq) && | |
2872 | smt_slice(smt_curr, sd) > task_timeslice(p)) | |
fc38ed75 CK |
2873 | ret = 1; |
2874 | ||
2875 | check_smt_task: | |
2876 | if ((!smt_curr->mm && smt_curr != smt_rq->idle) || | |
2877 | rt_task(smt_curr)) | |
2878 | continue; | |
2879 | if (!p->mm) { | |
2880 | wakeup_busy_runqueue(smt_rq); | |
2881 | continue; | |
2882 | } | |
1da177e4 LT |
2883 | |
2884 | /* | |
fc38ed75 CK |
2885 | * Reschedule a lower priority task on the SMT sibling for |
2886 | * it to be put to sleep, or wake it up if it has been put to | |
2887 | * sleep for priority reasons to see if it should run now. | |
1da177e4 | 2888 | */ |
fc38ed75 CK |
2889 | if (rt_task(p)) { |
2890 | if ((jiffies % DEF_TIMESLICE) > | |
2891 | (sd->per_cpu_gain * DEF_TIMESLICE / 100)) | |
2892 | resched_task(smt_curr); | |
2893 | } else { | |
67f9a619 IM |
2894 | if (TASK_PREEMPTS_CURR(p, smt_rq) && |
2895 | smt_slice(p, sd) > task_timeslice(smt_curr)) | |
fc38ed75 CK |
2896 | resched_task(smt_curr); |
2897 | else | |
2898 | wakeup_busy_runqueue(smt_rq); | |
2899 | } | |
1da177e4 LT |
2900 | } |
2901 | out_unlock: | |
2902 | for_each_cpu_mask(i, sibling_map) | |
2903 | spin_unlock(&cpu_rq(i)->lock); | |
2904 | return ret; | |
2905 | } | |
2906 | #else | |
2907 | static inline void wake_sleeping_dependent(int this_cpu, runqueue_t *this_rq) | |
2908 | { | |
2909 | } | |
2910 | ||
2911 | static inline int dependent_sleeper(int this_cpu, runqueue_t *this_rq) | |
2912 | { | |
2913 | return 0; | |
2914 | } | |
2915 | #endif | |
2916 | ||
2917 | #if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT) | |
2918 | ||
2919 | void fastcall add_preempt_count(int val) | |
2920 | { | |
2921 | /* | |
2922 | * Underflow? | |
2923 | */ | |
be5b4fbd | 2924 | BUG_ON((preempt_count() < 0)); |
1da177e4 LT |
2925 | preempt_count() += val; |
2926 | /* | |
2927 | * Spinlock count overflowing soon? | |
2928 | */ | |
2929 | BUG_ON((preempt_count() & PREEMPT_MASK) >= PREEMPT_MASK-10); | |
2930 | } | |
2931 | EXPORT_SYMBOL(add_preempt_count); | |
2932 | ||
2933 | void fastcall sub_preempt_count(int val) | |
2934 | { | |
2935 | /* | |
2936 | * Underflow? | |
2937 | */ | |
2938 | BUG_ON(val > preempt_count()); | |
2939 | /* | |
2940 | * Is the spinlock portion underflowing? | |
2941 | */ | |
2942 | BUG_ON((val < PREEMPT_MASK) && !(preempt_count() & PREEMPT_MASK)); | |
2943 | preempt_count() -= val; | |
2944 | } | |
2945 | EXPORT_SYMBOL(sub_preempt_count); | |
2946 | ||
2947 | #endif | |
2948 | ||
2949 | /* | |
2950 | * schedule() is the main scheduler function. | |
2951 | */ | |
2952 | asmlinkage void __sched schedule(void) | |
2953 | { | |
2954 | long *switch_count; | |
2955 | task_t *prev, *next; | |
2956 | runqueue_t *rq; | |
2957 | prio_array_t *array; | |
2958 | struct list_head *queue; | |
2959 | unsigned long long now; | |
2960 | unsigned long run_time; | |
a3464a10 | 2961 | int cpu, idx, new_prio; |
1da177e4 LT |
2962 | |
2963 | /* | |
2964 | * Test if we are atomic. Since do_exit() needs to call into | |
2965 | * schedule() atomically, we ignore that path for now. | |
2966 | * Otherwise, whine if we are scheduling when we should not be. | |
2967 | */ | |
2968 | if (likely(!current->exit_state)) { | |
2969 | if (unlikely(in_atomic())) { | |
2970 | printk(KERN_ERR "scheduling while atomic: " | |
2971 | "%s/0x%08x/%d\n", | |
2972 | current->comm, preempt_count(), current->pid); | |
2973 | dump_stack(); | |
2974 | } | |
2975 | } | |
2976 | profile_hit(SCHED_PROFILING, __builtin_return_address(0)); | |
2977 | ||
2978 | need_resched: | |
2979 | preempt_disable(); | |
2980 | prev = current; | |
2981 | release_kernel_lock(prev); | |
2982 | need_resched_nonpreemptible: | |
2983 | rq = this_rq(); | |
2984 | ||
2985 | /* | |
2986 | * The idle thread is not allowed to schedule! | |
2987 | * Remove this check after it has been exercised a bit. | |
2988 | */ | |
2989 | if (unlikely(prev == rq->idle) && prev->state != TASK_RUNNING) { | |
2990 | printk(KERN_ERR "bad: scheduling from the idle thread!\n"); | |
2991 | dump_stack(); | |
2992 | } | |
2993 | ||
2994 | schedstat_inc(rq, sched_cnt); | |
2995 | now = sched_clock(); | |
238628ed | 2996 | if (likely((long long)(now - prev->timestamp) < NS_MAX_SLEEP_AVG)) { |
1da177e4 | 2997 | run_time = now - prev->timestamp; |
238628ed | 2998 | if (unlikely((long long)(now - prev->timestamp) < 0)) |
1da177e4 LT |
2999 | run_time = 0; |
3000 | } else | |
3001 | run_time = NS_MAX_SLEEP_AVG; | |
3002 | ||
3003 | /* | |
3004 | * Tasks charged proportionately less run_time at high sleep_avg to | |
3005 | * delay them losing their interactive status | |
3006 | */ | |
3007 | run_time /= (CURRENT_BONUS(prev) ? : 1); | |
3008 | ||
3009 | spin_lock_irq(&rq->lock); | |
3010 | ||
3011 | if (unlikely(prev->flags & PF_DEAD)) | |
3012 | prev->state = EXIT_DEAD; | |
3013 | ||
3014 | switch_count = &prev->nivcsw; | |
3015 | if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) { | |
3016 | switch_count = &prev->nvcsw; | |
3017 | if (unlikely((prev->state & TASK_INTERRUPTIBLE) && | |
3018 | unlikely(signal_pending(prev)))) | |
3019 | prev->state = TASK_RUNNING; | |
3020 | else { | |
3021 | if (prev->state == TASK_UNINTERRUPTIBLE) | |
3022 | rq->nr_uninterruptible++; | |
3023 | deactivate_task(prev, rq); | |
3024 | } | |
3025 | } | |
3026 | ||
3027 | cpu = smp_processor_id(); | |
3028 | if (unlikely(!rq->nr_running)) { | |
3029 | go_idle: | |
3030 | idle_balance(cpu, rq); | |
3031 | if (!rq->nr_running) { | |
3032 | next = rq->idle; | |
3033 | rq->expired_timestamp = 0; | |
3034 | wake_sleeping_dependent(cpu, rq); | |
3035 | /* | |
3036 | * wake_sleeping_dependent() might have released | |
3037 | * the runqueue, so break out if we got new | |
3038 | * tasks meanwhile: | |
3039 | */ | |
3040 | if (!rq->nr_running) | |
3041 | goto switch_tasks; | |
3042 | } | |
3043 | } else { | |
3044 | if (dependent_sleeper(cpu, rq)) { | |
3045 | next = rq->idle; | |
3046 | goto switch_tasks; | |
3047 | } | |
3048 | /* | |
3049 | * dependent_sleeper() releases and reacquires the runqueue | |
3050 | * lock, hence go into the idle loop if the rq went | |
3051 | * empty meanwhile: | |
3052 | */ | |
3053 | if (unlikely(!rq->nr_running)) | |
3054 | goto go_idle; | |
3055 | } | |
3056 | ||
3057 | array = rq->active; | |
3058 | if (unlikely(!array->nr_active)) { | |
3059 | /* | |
3060 | * Switch the active and expired arrays. | |
3061 | */ | |
3062 | schedstat_inc(rq, sched_switch); | |
3063 | rq->active = rq->expired; | |
3064 | rq->expired = array; | |
3065 | array = rq->active; | |
3066 | rq->expired_timestamp = 0; | |
3067 | rq->best_expired_prio = MAX_PRIO; | |
3068 | } | |
3069 | ||
3070 | idx = sched_find_first_bit(array->bitmap); | |
3071 | queue = array->queue + idx; | |
3072 | next = list_entry(queue->next, task_t, run_list); | |
3073 | ||
3074 | if (!rt_task(next) && next->activated > 0) { | |
3075 | unsigned long long delta = now - next->timestamp; | |
238628ed | 3076 | if (unlikely((long long)(now - next->timestamp) < 0)) |
1da177e4 LT |
3077 | delta = 0; |
3078 | ||
3079 | if (next->activated == 1) | |
3080 | delta = delta * (ON_RUNQUEUE_WEIGHT * 128 / 100) / 128; | |
3081 | ||
3082 | array = next->array; | |
a3464a10 CS |
3083 | new_prio = recalc_task_prio(next, next->timestamp + delta); |
3084 | ||
3085 | if (unlikely(next->prio != new_prio)) { | |
3086 | dequeue_task(next, array); | |
3087 | next->prio = new_prio; | |
3088 | enqueue_task(next, array); | |
3089 | } else | |
3090 | requeue_task(next, array); | |
1da177e4 LT |
3091 | } |
3092 | next->activated = 0; | |
3093 | switch_tasks: | |
3094 | if (next == rq->idle) | |
3095 | schedstat_inc(rq, sched_goidle); | |
3096 | prefetch(next); | |
383f2835 | 3097 | prefetch_stack(next); |
1da177e4 LT |
3098 | clear_tsk_need_resched(prev); |
3099 | rcu_qsctr_inc(task_cpu(prev)); | |
3100 | ||
3101 | update_cpu_clock(prev, rq, now); | |
3102 | ||
3103 | prev->sleep_avg -= run_time; | |
3104 | if ((long)prev->sleep_avg <= 0) | |
3105 | prev->sleep_avg = 0; | |
3106 | prev->timestamp = prev->last_ran = now; | |
3107 | ||
3108 | sched_info_switch(prev, next); | |
3109 | if (likely(prev != next)) { | |
3110 | next->timestamp = now; | |
3111 | rq->nr_switches++; | |
3112 | rq->curr = next; | |
3113 | ++*switch_count; | |
3114 | ||
4866cde0 | 3115 | prepare_task_switch(rq, next); |
1da177e4 LT |
3116 | prev = context_switch(rq, prev, next); |
3117 | barrier(); | |
4866cde0 NP |
3118 | /* |
3119 | * this_rq must be evaluated again because prev may have moved | |
3120 | * CPUs since it called schedule(), thus the 'rq' on its stack | |
3121 | * frame will be invalid. | |
3122 | */ | |
3123 | finish_task_switch(this_rq(), prev); | |
1da177e4 LT |
3124 | } else |
3125 | spin_unlock_irq(&rq->lock); | |
3126 | ||
3127 | prev = current; | |
3128 | if (unlikely(reacquire_kernel_lock(prev) < 0)) | |
3129 | goto need_resched_nonpreemptible; | |
3130 | preempt_enable_no_resched(); | |
3131 | if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) | |
3132 | goto need_resched; | |
3133 | } | |
3134 | ||
3135 | EXPORT_SYMBOL(schedule); | |
3136 | ||
3137 | #ifdef CONFIG_PREEMPT | |
3138 | /* | |
3139 | * this is is the entry point to schedule() from in-kernel preemption | |
3140 | * off of preempt_enable. Kernel preemptions off return from interrupt | |
3141 | * occur there and call schedule directly. | |
3142 | */ | |
3143 | asmlinkage void __sched preempt_schedule(void) | |
3144 | { | |
3145 | struct thread_info *ti = current_thread_info(); | |
3146 | #ifdef CONFIG_PREEMPT_BKL | |
3147 | struct task_struct *task = current; | |
3148 | int saved_lock_depth; | |
3149 | #endif | |
3150 | /* | |
3151 | * If there is a non-zero preempt_count or interrupts are disabled, | |
3152 | * we do not want to preempt the current task. Just return.. | |
3153 | */ | |
3154 | if (unlikely(ti->preempt_count || irqs_disabled())) | |
3155 | return; | |
3156 | ||
3157 | need_resched: | |
3158 | add_preempt_count(PREEMPT_ACTIVE); | |
3159 | /* | |
3160 | * We keep the big kernel semaphore locked, but we | |
3161 | * clear ->lock_depth so that schedule() doesnt | |
3162 | * auto-release the semaphore: | |
3163 | */ | |
3164 | #ifdef CONFIG_PREEMPT_BKL | |
3165 | saved_lock_depth = task->lock_depth; | |
3166 | task->lock_depth = -1; | |
3167 | #endif | |
3168 | schedule(); | |
3169 | #ifdef CONFIG_PREEMPT_BKL | |
3170 | task->lock_depth = saved_lock_depth; | |
3171 | #endif | |
3172 | sub_preempt_count(PREEMPT_ACTIVE); | |
3173 | ||
3174 | /* we could miss a preemption opportunity between schedule and now */ | |
3175 | barrier(); | |
3176 | if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) | |
3177 | goto need_resched; | |
3178 | } | |
3179 | ||
3180 | EXPORT_SYMBOL(preempt_schedule); | |
3181 | ||
3182 | /* | |
3183 | * this is is the entry point to schedule() from kernel preemption | |
3184 | * off of irq context. | |
3185 | * Note, that this is called and return with irqs disabled. This will | |
3186 | * protect us against recursive calling from irq. | |
3187 | */ | |
3188 | asmlinkage void __sched preempt_schedule_irq(void) | |
3189 | { | |
3190 | struct thread_info *ti = current_thread_info(); | |
3191 | #ifdef CONFIG_PREEMPT_BKL | |
3192 | struct task_struct *task = current; | |
3193 | int saved_lock_depth; | |
3194 | #endif | |
3195 | /* Catch callers which need to be fixed*/ | |
3196 | BUG_ON(ti->preempt_count || !irqs_disabled()); | |
3197 | ||
3198 | need_resched: | |
3199 | add_preempt_count(PREEMPT_ACTIVE); | |
3200 | /* | |
3201 | * We keep the big kernel semaphore locked, but we | |
3202 | * clear ->lock_depth so that schedule() doesnt | |
3203 | * auto-release the semaphore: | |
3204 | */ | |
3205 | #ifdef CONFIG_PREEMPT_BKL | |
3206 | saved_lock_depth = task->lock_depth; | |
3207 | task->lock_depth = -1; | |
3208 | #endif | |
3209 | local_irq_enable(); | |
3210 | schedule(); | |
3211 | local_irq_disable(); | |
3212 | #ifdef CONFIG_PREEMPT_BKL | |
3213 | task->lock_depth = saved_lock_depth; | |
3214 | #endif | |
3215 | sub_preempt_count(PREEMPT_ACTIVE); | |
3216 | ||
3217 | /* we could miss a preemption opportunity between schedule and now */ | |
3218 | barrier(); | |
3219 | if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) | |
3220 | goto need_resched; | |
3221 | } | |
3222 | ||
3223 | #endif /* CONFIG_PREEMPT */ | |
3224 | ||
95cdf3b7 IM |
3225 | int default_wake_function(wait_queue_t *curr, unsigned mode, int sync, |
3226 | void *key) | |
1da177e4 | 3227 | { |
c43dc2fd | 3228 | task_t *p = curr->private; |
1da177e4 LT |
3229 | return try_to_wake_up(p, mode, sync); |
3230 | } | |
3231 | ||
3232 | EXPORT_SYMBOL(default_wake_function); | |
3233 | ||
3234 | /* | |
3235 | * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just | |
3236 | * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve | |
3237 | * number) then we wake all the non-exclusive tasks and one exclusive task. | |
3238 | * | |
3239 | * There are circumstances in which we can try to wake a task which has already | |
3240 | * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns | |
3241 | * zero in this (rare) case, and we handle it by continuing to scan the queue. | |
3242 | */ | |
3243 | static void __wake_up_common(wait_queue_head_t *q, unsigned int mode, | |
3244 | int nr_exclusive, int sync, void *key) | |
3245 | { | |
3246 | struct list_head *tmp, *next; | |
3247 | ||
3248 | list_for_each_safe(tmp, next, &q->task_list) { | |
3249 | wait_queue_t *curr; | |
3250 | unsigned flags; | |
3251 | curr = list_entry(tmp, wait_queue_t, task_list); | |
3252 | flags = curr->flags; | |
3253 | if (curr->func(curr, mode, sync, key) && | |
3254 | (flags & WQ_FLAG_EXCLUSIVE) && | |
3255 | !--nr_exclusive) | |
3256 | break; | |
3257 | } | |
3258 | } | |
3259 | ||
3260 | /** | |
3261 | * __wake_up - wake up threads blocked on a waitqueue. | |
3262 | * @q: the waitqueue | |
3263 | * @mode: which threads | |
3264 | * @nr_exclusive: how many wake-one or wake-many threads to wake up | |
67be2dd1 | 3265 | * @key: is directly passed to the wakeup function |
1da177e4 LT |
3266 | */ |
3267 | void fastcall __wake_up(wait_queue_head_t *q, unsigned int mode, | |
95cdf3b7 | 3268 | int nr_exclusive, void *key) |
1da177e4 LT |
3269 | { |
3270 | unsigned long flags; | |
3271 | ||
3272 | spin_lock_irqsave(&q->lock, flags); | |
3273 | __wake_up_common(q, mode, nr_exclusive, 0, key); | |
3274 | spin_unlock_irqrestore(&q->lock, flags); | |
3275 | } | |
3276 | ||
3277 | EXPORT_SYMBOL(__wake_up); | |
3278 | ||
3279 | /* | |
3280 | * Same as __wake_up but called with the spinlock in wait_queue_head_t held. | |
3281 | */ | |
3282 | void fastcall __wake_up_locked(wait_queue_head_t *q, unsigned int mode) | |
3283 | { | |
3284 | __wake_up_common(q, mode, 1, 0, NULL); | |
3285 | } | |
3286 | ||
3287 | /** | |
67be2dd1 | 3288 | * __wake_up_sync - wake up threads blocked on a waitqueue. |
1da177e4 LT |
3289 | * @q: the waitqueue |
3290 | * @mode: which threads | |
3291 | * @nr_exclusive: how many wake-one or wake-many threads to wake up | |
3292 | * | |
3293 | * The sync wakeup differs that the waker knows that it will schedule | |
3294 | * away soon, so while the target thread will be woken up, it will not | |
3295 | * be migrated to another CPU - ie. the two threads are 'synchronized' | |
3296 | * with each other. This can prevent needless bouncing between CPUs. | |
3297 | * | |
3298 | * On UP it can prevent extra preemption. | |
3299 | */ | |
95cdf3b7 IM |
3300 | void fastcall |
3301 | __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive) | |
1da177e4 LT |
3302 | { |
3303 | unsigned long flags; | |
3304 | int sync = 1; | |
3305 | ||
3306 | if (unlikely(!q)) | |
3307 | return; | |
3308 | ||
3309 | if (unlikely(!nr_exclusive)) | |
3310 | sync = 0; | |
3311 | ||
3312 | spin_lock_irqsave(&q->lock, flags); | |
3313 | __wake_up_common(q, mode, nr_exclusive, sync, NULL); | |
3314 | spin_unlock_irqrestore(&q->lock, flags); | |
3315 | } | |
3316 | EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */ | |
3317 | ||
3318 | void fastcall complete(struct completion *x) | |
3319 | { | |
3320 | unsigned long flags; | |
3321 | ||
3322 | spin_lock_irqsave(&x->wait.lock, flags); | |
3323 | x->done++; | |
3324 | __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, | |
3325 | 1, 0, NULL); | |
3326 | spin_unlock_irqrestore(&x->wait.lock, flags); | |
3327 | } | |
3328 | EXPORT_SYMBOL(complete); | |
3329 | ||
3330 | void fastcall complete_all(struct completion *x) | |
3331 | { | |
3332 | unsigned long flags; | |
3333 | ||
3334 | spin_lock_irqsave(&x->wait.lock, flags); | |
3335 | x->done += UINT_MAX/2; | |
3336 | __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, | |
3337 | 0, 0, NULL); | |
3338 | spin_unlock_irqrestore(&x->wait.lock, flags); | |
3339 | } | |
3340 | EXPORT_SYMBOL(complete_all); | |
3341 | ||
3342 | void fastcall __sched wait_for_completion(struct completion *x) | |
3343 | { | |
3344 | might_sleep(); | |
3345 | spin_lock_irq(&x->wait.lock); | |
3346 | if (!x->done) { | |
3347 | DECLARE_WAITQUEUE(wait, current); | |
3348 | ||
3349 | wait.flags |= WQ_FLAG_EXCLUSIVE; | |
3350 | __add_wait_queue_tail(&x->wait, &wait); | |
3351 | do { | |
3352 | __set_current_state(TASK_UNINTERRUPTIBLE); | |
3353 | spin_unlock_irq(&x->wait.lock); | |
3354 | schedule(); | |
3355 | spin_lock_irq(&x->wait.lock); | |
3356 | } while (!x->done); | |
3357 | __remove_wait_queue(&x->wait, &wait); | |
3358 | } | |
3359 | x->done--; | |
3360 | spin_unlock_irq(&x->wait.lock); | |
3361 | } | |
3362 | EXPORT_SYMBOL(wait_for_completion); | |
3363 | ||
3364 | unsigned long fastcall __sched | |
3365 | wait_for_completion_timeout(struct completion *x, unsigned long timeout) | |
3366 | { | |
3367 | might_sleep(); | |
3368 | ||
3369 | spin_lock_irq(&x->wait.lock); | |
3370 | if (!x->done) { | |
3371 | DECLARE_WAITQUEUE(wait, current); | |
3372 | ||
3373 | wait.flags |= WQ_FLAG_EXCLUSIVE; | |
3374 | __add_wait_queue_tail(&x->wait, &wait); | |
3375 | do { | |
3376 | __set_current_state(TASK_UNINTERRUPTIBLE); | |
3377 | spin_unlock_irq(&x->wait.lock); | |
3378 | timeout = schedule_timeout(timeout); | |
3379 | spin_lock_irq(&x->wait.lock); | |
3380 | if (!timeout) { | |
3381 | __remove_wait_queue(&x->wait, &wait); | |
3382 | goto out; | |
3383 | } | |
3384 | } while (!x->done); | |
3385 | __remove_wait_queue(&x->wait, &wait); | |
3386 | } | |
3387 | x->done--; | |
3388 | out: | |
3389 | spin_unlock_irq(&x->wait.lock); | |
3390 | return timeout; | |
3391 | } | |
3392 | EXPORT_SYMBOL(wait_for_completion_timeout); | |
3393 | ||
3394 | int fastcall __sched wait_for_completion_interruptible(struct completion *x) | |
3395 | { | |
3396 | int ret = 0; | |
3397 | ||
3398 | might_sleep(); | |
3399 | ||
3400 | spin_lock_irq(&x->wait.lock); | |
3401 | if (!x->done) { | |
3402 | DECLARE_WAITQUEUE(wait, current); | |
3403 | ||
3404 | wait.flags |= WQ_FLAG_EXCLUSIVE; | |
3405 | __add_wait_queue_tail(&x->wait, &wait); | |
3406 | do { | |
3407 | if (signal_pending(current)) { | |
3408 | ret = -ERESTARTSYS; | |
3409 | __remove_wait_queue(&x->wait, &wait); | |
3410 | goto out; | |
3411 | } | |
3412 | __set_current_state(TASK_INTERRUPTIBLE); | |
3413 | spin_unlock_irq(&x->wait.lock); | |
3414 | schedule(); | |
3415 | spin_lock_irq(&x->wait.lock); | |
3416 | } while (!x->done); | |
3417 | __remove_wait_queue(&x->wait, &wait); | |
3418 | } | |
3419 | x->done--; | |
3420 | out: | |
3421 | spin_unlock_irq(&x->wait.lock); | |
3422 | ||
3423 | return ret; | |
3424 | } | |
3425 | EXPORT_SYMBOL(wait_for_completion_interruptible); | |
3426 | ||
3427 | unsigned long fastcall __sched | |
3428 | wait_for_completion_interruptible_timeout(struct completion *x, | |
3429 | unsigned long timeout) | |
3430 | { | |
3431 | might_sleep(); | |
3432 | ||
3433 | spin_lock_irq(&x->wait.lock); | |
3434 | if (!x->done) { | |
3435 | DECLARE_WAITQUEUE(wait, current); | |
3436 | ||
3437 | wait.flags |= WQ_FLAG_EXCLUSIVE; | |
3438 | __add_wait_queue_tail(&x->wait, &wait); | |
3439 | do { | |
3440 | if (signal_pending(current)) { | |
3441 | timeout = -ERESTARTSYS; | |
3442 | __remove_wait_queue(&x->wait, &wait); | |
3443 | goto out; | |
3444 | } | |
3445 | __set_current_state(TASK_INTERRUPTIBLE); | |
3446 | spin_unlock_irq(&x->wait.lock); | |
3447 | timeout = schedule_timeout(timeout); | |
3448 | spin_lock_irq(&x->wait.lock); | |
3449 | if (!timeout) { | |
3450 | __remove_wait_queue(&x->wait, &wait); | |
3451 | goto out; | |
3452 | } | |
3453 | } while (!x->done); | |
3454 | __remove_wait_queue(&x->wait, &wait); | |
3455 | } | |
3456 | x->done--; | |
3457 | out: | |
3458 | spin_unlock_irq(&x->wait.lock); | |
3459 | return timeout; | |
3460 | } | |
3461 | EXPORT_SYMBOL(wait_for_completion_interruptible_timeout); | |
3462 | ||
3463 | ||
3464 | #define SLEEP_ON_VAR \ | |
3465 | unsigned long flags; \ | |
3466 | wait_queue_t wait; \ | |
3467 | init_waitqueue_entry(&wait, current); | |
3468 | ||
3469 | #define SLEEP_ON_HEAD \ | |
3470 | spin_lock_irqsave(&q->lock,flags); \ | |
3471 | __add_wait_queue(q, &wait); \ | |
3472 | spin_unlock(&q->lock); | |
3473 | ||
3474 | #define SLEEP_ON_TAIL \ | |
3475 | spin_lock_irq(&q->lock); \ | |
3476 | __remove_wait_queue(q, &wait); \ | |
3477 | spin_unlock_irqrestore(&q->lock, flags); | |
3478 | ||
3479 | void fastcall __sched interruptible_sleep_on(wait_queue_head_t *q) | |
3480 | { | |
3481 | SLEEP_ON_VAR | |
3482 | ||
3483 | current->state = TASK_INTERRUPTIBLE; | |
3484 | ||
3485 | SLEEP_ON_HEAD | |
3486 | schedule(); | |
3487 | SLEEP_ON_TAIL | |
3488 | } | |
3489 | ||
3490 | EXPORT_SYMBOL(interruptible_sleep_on); | |
3491 | ||
95cdf3b7 IM |
3492 | long fastcall __sched |
3493 | interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout) | |
1da177e4 LT |
3494 | { |
3495 | SLEEP_ON_VAR | |
3496 | ||
3497 | current->state = TASK_INTERRUPTIBLE; | |
3498 | ||
3499 | SLEEP_ON_HEAD | |
3500 | timeout = schedule_timeout(timeout); | |
3501 | SLEEP_ON_TAIL | |
3502 | ||
3503 | return timeout; | |
3504 | } | |
3505 | ||
3506 | EXPORT_SYMBOL(interruptible_sleep_on_timeout); | |
3507 | ||
3508 | void fastcall __sched sleep_on(wait_queue_head_t *q) | |
3509 | { | |
3510 | SLEEP_ON_VAR | |
3511 | ||
3512 | current->state = TASK_UNINTERRUPTIBLE; | |
3513 | ||
3514 | SLEEP_ON_HEAD | |
3515 | schedule(); | |
3516 | SLEEP_ON_TAIL | |
3517 | } | |
3518 | ||
3519 | EXPORT_SYMBOL(sleep_on); | |
3520 | ||
3521 | long fastcall __sched sleep_on_timeout(wait_queue_head_t *q, long timeout) | |
3522 | { | |
3523 | SLEEP_ON_VAR | |
3524 | ||
3525 | current->state = TASK_UNINTERRUPTIBLE; | |
3526 | ||
3527 | SLEEP_ON_HEAD | |
3528 | timeout = schedule_timeout(timeout); | |
3529 | SLEEP_ON_TAIL | |
3530 | ||
3531 | return timeout; | |
3532 | } | |
3533 | ||
3534 | EXPORT_SYMBOL(sleep_on_timeout); | |
3535 | ||
3536 | void set_user_nice(task_t *p, long nice) | |
3537 | { | |
3538 | unsigned long flags; | |
3539 | prio_array_t *array; | |
3540 | runqueue_t *rq; | |
3541 | int old_prio, new_prio, delta; | |
3542 | ||
3543 | if (TASK_NICE(p) == nice || nice < -20 || nice > 19) | |
3544 | return; | |
3545 | /* | |
3546 | * We have to be careful, if called from sys_setpriority(), | |
3547 | * the task might be in the middle of scheduling on another CPU. | |
3548 | */ | |
3549 | rq = task_rq_lock(p, &flags); | |
3550 | /* | |
3551 | * The RT priorities are set via sched_setscheduler(), but we still | |
3552 | * allow the 'normal' nice value to be set - but as expected | |
3553 | * it wont have any effect on scheduling until the task is | |
3554 | * not SCHED_NORMAL: | |
3555 | */ | |
3556 | if (rt_task(p)) { | |
3557 | p->static_prio = NICE_TO_PRIO(nice); | |
3558 | goto out_unlock; | |
3559 | } | |
3560 | array = p->array; | |
738a2ccb | 3561 | if (array) { |
1da177e4 | 3562 | dequeue_task(p, array); |
738a2ccb CK |
3563 | dec_prio_bias(rq, p->static_prio); |
3564 | } | |
1da177e4 LT |
3565 | |
3566 | old_prio = p->prio; | |
3567 | new_prio = NICE_TO_PRIO(nice); | |
3568 | delta = new_prio - old_prio; | |
3569 | p->static_prio = NICE_TO_PRIO(nice); | |
3570 | p->prio += delta; | |
3571 | ||
3572 | if (array) { | |
3573 | enqueue_task(p, array); | |
738a2ccb | 3574 | inc_prio_bias(rq, p->static_prio); |
1da177e4 LT |
3575 | /* |
3576 | * If the task increased its priority or is running and | |
3577 | * lowered its priority, then reschedule its CPU: | |
3578 | */ | |
3579 | if (delta < 0 || (delta > 0 && task_running(rq, p))) | |
3580 | resched_task(rq->curr); | |
3581 | } | |
3582 | out_unlock: | |
3583 | task_rq_unlock(rq, &flags); | |
3584 | } | |
3585 | ||
3586 | EXPORT_SYMBOL(set_user_nice); | |
3587 | ||
e43379f1 MM |
3588 | /* |
3589 | * can_nice - check if a task can reduce its nice value | |
3590 | * @p: task | |
3591 | * @nice: nice value | |
3592 | */ | |
3593 | int can_nice(const task_t *p, const int nice) | |
3594 | { | |
024f4747 MM |
3595 | /* convert nice value [19,-20] to rlimit style value [1,40] */ |
3596 | int nice_rlim = 20 - nice; | |
e43379f1 MM |
3597 | return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur || |
3598 | capable(CAP_SYS_NICE)); | |
3599 | } | |
3600 | ||
1da177e4 LT |
3601 | #ifdef __ARCH_WANT_SYS_NICE |
3602 | ||
3603 | /* | |
3604 | * sys_nice - change the priority of the current process. | |
3605 | * @increment: priority increment | |
3606 | * | |
3607 | * sys_setpriority is a more generic, but much slower function that | |
3608 | * does similar things. | |
3609 | */ | |
3610 | asmlinkage long sys_nice(int increment) | |
3611 | { | |
3612 | int retval; | |
3613 | long nice; | |
3614 | ||
3615 | /* | |
3616 | * Setpriority might change our priority at the same moment. | |
3617 | * We don't have to worry. Conceptually one call occurs first | |
3618 | * and we have a single winner. | |
3619 | */ | |
e43379f1 MM |
3620 | if (increment < -40) |
3621 | increment = -40; | |
1da177e4 LT |
3622 | if (increment > 40) |
3623 | increment = 40; | |
3624 | ||
3625 | nice = PRIO_TO_NICE(current->static_prio) + increment; | |
3626 | if (nice < -20) | |
3627 | nice = -20; | |
3628 | if (nice > 19) | |
3629 | nice = 19; | |
3630 | ||
e43379f1 MM |
3631 | if (increment < 0 && !can_nice(current, nice)) |
3632 | return -EPERM; | |
3633 | ||
1da177e4 LT |
3634 | retval = security_task_setnice(current, nice); |
3635 | if (retval) | |
3636 | return retval; | |
3637 | ||
3638 | set_user_nice(current, nice); | |
3639 | return 0; | |
3640 | } | |
3641 | ||
3642 | #endif | |
3643 | ||
3644 | /** | |
3645 | * task_prio - return the priority value of a given task. | |
3646 | * @p: the task in question. | |
3647 | * | |
3648 | * This is the priority value as seen by users in /proc. | |
3649 | * RT tasks are offset by -200. Normal tasks are centered | |
3650 | * around 0, value goes from -16 to +15. | |
3651 | */ | |
3652 | int task_prio(const task_t *p) | |
3653 | { | |
3654 | return p->prio - MAX_RT_PRIO; | |
3655 | } | |
3656 | ||
3657 | /** | |
3658 | * task_nice - return the nice value of a given task. | |
3659 | * @p: the task in question. | |
3660 | */ | |
3661 | int task_nice(const task_t *p) | |
3662 | { | |
3663 | return TASK_NICE(p); | |
3664 | } | |
1da177e4 | 3665 | EXPORT_SYMBOL_GPL(task_nice); |
1da177e4 LT |
3666 | |
3667 | /** | |
3668 | * idle_cpu - is a given cpu idle currently? | |
3669 | * @cpu: the processor in question. | |
3670 | */ | |
3671 | int idle_cpu(int cpu) | |
3672 | { | |
3673 | return cpu_curr(cpu) == cpu_rq(cpu)->idle; | |
3674 | } | |
3675 | ||
1da177e4 LT |
3676 | /** |
3677 | * idle_task - return the idle task for a given cpu. | |
3678 | * @cpu: the processor in question. | |
3679 | */ | |
3680 | task_t *idle_task(int cpu) | |
3681 | { | |
3682 | return cpu_rq(cpu)->idle; | |
3683 | } | |
3684 | ||
3685 | /** | |
3686 | * find_process_by_pid - find a process with a matching PID value. | |
3687 | * @pid: the pid in question. | |
3688 | */ | |
3689 | static inline task_t *find_process_by_pid(pid_t pid) | |
3690 | { | |
3691 | return pid ? find_task_by_pid(pid) : current; | |
3692 | } | |
3693 | ||
3694 | /* Actually do priority change: must hold rq lock. */ | |
3695 | static void __setscheduler(struct task_struct *p, int policy, int prio) | |
3696 | { | |
3697 | BUG_ON(p->array); | |
3698 | p->policy = policy; | |
3699 | p->rt_priority = prio; | |
3700 | if (policy != SCHED_NORMAL) | |
d46523ea | 3701 | p->prio = MAX_RT_PRIO-1 - p->rt_priority; |
1da177e4 LT |
3702 | else |
3703 | p->prio = p->static_prio; | |
3704 | } | |
3705 | ||
3706 | /** | |
3707 | * sched_setscheduler - change the scheduling policy and/or RT priority of | |
3708 | * a thread. | |
3709 | * @p: the task in question. | |
3710 | * @policy: new policy. | |
3711 | * @param: structure containing the new RT priority. | |
3712 | */ | |
95cdf3b7 IM |
3713 | int sched_setscheduler(struct task_struct *p, int policy, |
3714 | struct sched_param *param) | |
1da177e4 LT |
3715 | { |
3716 | int retval; | |
3717 | int oldprio, oldpolicy = -1; | |
3718 | prio_array_t *array; | |
3719 | unsigned long flags; | |
3720 | runqueue_t *rq; | |
3721 | ||
3722 | recheck: | |
3723 | /* double check policy once rq lock held */ | |
3724 | if (policy < 0) | |
3725 | policy = oldpolicy = p->policy; | |
3726 | else if (policy != SCHED_FIFO && policy != SCHED_RR && | |
3727 | policy != SCHED_NORMAL) | |
3728 | return -EINVAL; | |
3729 | /* | |
3730 | * Valid priorities for SCHED_FIFO and SCHED_RR are | |
3731 | * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL is 0. | |
3732 | */ | |
3733 | if (param->sched_priority < 0 || | |
95cdf3b7 | 3734 | (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) || |
d46523ea | 3735 | (!p->mm && param->sched_priority > MAX_RT_PRIO-1)) |
1da177e4 LT |
3736 | return -EINVAL; |
3737 | if ((policy == SCHED_NORMAL) != (param->sched_priority == 0)) | |
3738 | return -EINVAL; | |
3739 | ||
37e4ab3f OC |
3740 | /* |
3741 | * Allow unprivileged RT tasks to decrease priority: | |
3742 | */ | |
3743 | if (!capable(CAP_SYS_NICE)) { | |
3744 | /* can't change policy */ | |
18586e72 AS |
3745 | if (policy != p->policy && |
3746 | !p->signal->rlim[RLIMIT_RTPRIO].rlim_cur) | |
37e4ab3f OC |
3747 | return -EPERM; |
3748 | /* can't increase priority */ | |
3749 | if (policy != SCHED_NORMAL && | |
3750 | param->sched_priority > p->rt_priority && | |
3751 | param->sched_priority > | |
3752 | p->signal->rlim[RLIMIT_RTPRIO].rlim_cur) | |
3753 | return -EPERM; | |
3754 | /* can't change other user's priorities */ | |
3755 | if ((current->euid != p->euid) && | |
3756 | (current->euid != p->uid)) | |
3757 | return -EPERM; | |
3758 | } | |
1da177e4 LT |
3759 | |
3760 | retval = security_task_setscheduler(p, policy, param); | |
3761 | if (retval) | |
3762 | return retval; | |
3763 | /* | |
3764 | * To be able to change p->policy safely, the apropriate | |
3765 | * runqueue lock must be held. | |
3766 | */ | |
3767 | rq = task_rq_lock(p, &flags); | |
3768 | /* recheck policy now with rq lock held */ | |
3769 | if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { | |
3770 | policy = oldpolicy = -1; | |
3771 | task_rq_unlock(rq, &flags); | |
3772 | goto recheck; | |
3773 | } | |
3774 | array = p->array; | |
3775 | if (array) | |
3776 | deactivate_task(p, rq); | |
3777 | oldprio = p->prio; | |
3778 | __setscheduler(p, policy, param->sched_priority); | |
3779 | if (array) { | |
3780 | __activate_task(p, rq); | |
3781 | /* | |
3782 | * Reschedule if we are currently running on this runqueue and | |
3783 | * our priority decreased, or if we are not currently running on | |
3784 | * this runqueue and our priority is higher than the current's | |
3785 | */ | |
3786 | if (task_running(rq, p)) { | |
3787 | if (p->prio > oldprio) | |
3788 | resched_task(rq->curr); | |
3789 | } else if (TASK_PREEMPTS_CURR(p, rq)) | |
3790 | resched_task(rq->curr); | |
3791 | } | |
3792 | task_rq_unlock(rq, &flags); | |
3793 | return 0; | |
3794 | } | |
3795 | EXPORT_SYMBOL_GPL(sched_setscheduler); | |
3796 | ||
95cdf3b7 IM |
3797 | static int |
3798 | do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param) | |
1da177e4 LT |
3799 | { |
3800 | int retval; | |
3801 | struct sched_param lparam; | |
3802 | struct task_struct *p; | |
3803 | ||
3804 | if (!param || pid < 0) | |
3805 | return -EINVAL; | |
3806 | if (copy_from_user(&lparam, param, sizeof(struct sched_param))) | |
3807 | return -EFAULT; | |
3808 | read_lock_irq(&tasklist_lock); | |
3809 | p = find_process_by_pid(pid); | |
3810 | if (!p) { | |
3811 | read_unlock_irq(&tasklist_lock); | |
3812 | return -ESRCH; | |
3813 | } | |
3814 | retval = sched_setscheduler(p, policy, &lparam); | |
3815 | read_unlock_irq(&tasklist_lock); | |
3816 | return retval; | |
3817 | } | |
3818 | ||
3819 | /** | |
3820 | * sys_sched_setscheduler - set/change the scheduler policy and RT priority | |
3821 | * @pid: the pid in question. | |
3822 | * @policy: new policy. | |
3823 | * @param: structure containing the new RT priority. | |
3824 | */ | |
3825 | asmlinkage long sys_sched_setscheduler(pid_t pid, int policy, | |
3826 | struct sched_param __user *param) | |
3827 | { | |
3828 | return do_sched_setscheduler(pid, policy, param); | |
3829 | } | |
3830 | ||
3831 | /** | |
3832 | * sys_sched_setparam - set/change the RT priority of a thread | |
3833 | * @pid: the pid in question. | |
3834 | * @param: structure containing the new RT priority. | |
3835 | */ | |
3836 | asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param) | |
3837 | { | |
3838 | return do_sched_setscheduler(pid, -1, param); | |
3839 | } | |
3840 | ||
3841 | /** | |
3842 | * sys_sched_getscheduler - get the policy (scheduling class) of a thread | |
3843 | * @pid: the pid in question. | |
3844 | */ | |
3845 | asmlinkage long sys_sched_getscheduler(pid_t pid) | |
3846 | { | |
3847 | int retval = -EINVAL; | |
3848 | task_t *p; | |
3849 | ||
3850 | if (pid < 0) | |
3851 | goto out_nounlock; | |
3852 | ||
3853 | retval = -ESRCH; | |
3854 | read_lock(&tasklist_lock); | |
3855 | p = find_process_by_pid(pid); | |
3856 | if (p) { | |
3857 | retval = security_task_getscheduler(p); | |
3858 | if (!retval) | |
3859 | retval = p->policy; | |
3860 | } | |
3861 | read_unlock(&tasklist_lock); | |
3862 | ||
3863 | out_nounlock: | |
3864 | return retval; | |
3865 | } | |
3866 | ||
3867 | /** | |
3868 | * sys_sched_getscheduler - get the RT priority of a thread | |
3869 | * @pid: the pid in question. | |
3870 | * @param: structure containing the RT priority. | |
3871 | */ | |
3872 | asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param) | |
3873 | { | |
3874 | struct sched_param lp; | |
3875 | int retval = -EINVAL; | |
3876 | task_t *p; | |
3877 | ||
3878 | if (!param || pid < 0) | |
3879 | goto out_nounlock; | |
3880 | ||
3881 | read_lock(&tasklist_lock); | |
3882 | p = find_process_by_pid(pid); | |
3883 | retval = -ESRCH; | |
3884 | if (!p) | |
3885 | goto out_unlock; | |
3886 | ||
3887 | retval = security_task_getscheduler(p); | |
3888 | if (retval) | |
3889 | goto out_unlock; | |
3890 | ||
3891 | lp.sched_priority = p->rt_priority; | |
3892 | read_unlock(&tasklist_lock); | |
3893 | ||
3894 | /* | |
3895 | * This one might sleep, we cannot do it with a spinlock held ... | |
3896 | */ | |
3897 | retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0; | |
3898 | ||
3899 | out_nounlock: | |
3900 | return retval; | |
3901 | ||
3902 | out_unlock: | |
3903 | read_unlock(&tasklist_lock); | |
3904 | return retval; | |
3905 | } | |
3906 | ||
3907 | long sched_setaffinity(pid_t pid, cpumask_t new_mask) | |
3908 | { | |
3909 | task_t *p; | |
3910 | int retval; | |
3911 | cpumask_t cpus_allowed; | |
3912 | ||
3913 | lock_cpu_hotplug(); | |
3914 | read_lock(&tasklist_lock); | |
3915 | ||
3916 | p = find_process_by_pid(pid); | |
3917 | if (!p) { | |
3918 | read_unlock(&tasklist_lock); | |
3919 | unlock_cpu_hotplug(); | |
3920 | return -ESRCH; | |
3921 | } | |
3922 | ||
3923 | /* | |
3924 | * It is not safe to call set_cpus_allowed with the | |
3925 | * tasklist_lock held. We will bump the task_struct's | |
3926 | * usage count and then drop tasklist_lock. | |
3927 | */ | |
3928 | get_task_struct(p); | |
3929 | read_unlock(&tasklist_lock); | |
3930 | ||
3931 | retval = -EPERM; | |
3932 | if ((current->euid != p->euid) && (current->euid != p->uid) && | |
3933 | !capable(CAP_SYS_NICE)) | |
3934 | goto out_unlock; | |
3935 | ||
3936 | cpus_allowed = cpuset_cpus_allowed(p); | |
3937 | cpus_and(new_mask, new_mask, cpus_allowed); | |
3938 | retval = set_cpus_allowed(p, new_mask); | |
3939 | ||
3940 | out_unlock: | |
3941 | put_task_struct(p); | |
3942 | unlock_cpu_hotplug(); | |
3943 | return retval; | |
3944 | } | |
3945 | ||
3946 | static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len, | |
3947 | cpumask_t *new_mask) | |
3948 | { | |
3949 | if (len < sizeof(cpumask_t)) { | |
3950 | memset(new_mask, 0, sizeof(cpumask_t)); | |
3951 | } else if (len > sizeof(cpumask_t)) { | |
3952 | len = sizeof(cpumask_t); | |
3953 | } | |
3954 | return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0; | |
3955 | } | |
3956 | ||
3957 | /** | |
3958 | * sys_sched_setaffinity - set the cpu affinity of a process | |
3959 | * @pid: pid of the process | |
3960 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr | |
3961 | * @user_mask_ptr: user-space pointer to the new cpu mask | |
3962 | */ | |
3963 | asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len, | |
3964 | unsigned long __user *user_mask_ptr) | |
3965 | { | |
3966 | cpumask_t new_mask; | |
3967 | int retval; | |
3968 | ||
3969 | retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask); | |
3970 | if (retval) | |
3971 | return retval; | |
3972 | ||
3973 | return sched_setaffinity(pid, new_mask); | |
3974 | } | |
3975 | ||
3976 | /* | |
3977 | * Represents all cpu's present in the system | |
3978 | * In systems capable of hotplug, this map could dynamically grow | |
3979 | * as new cpu's are detected in the system via any platform specific | |
3980 | * method, such as ACPI for e.g. | |
3981 | */ | |
3982 | ||
3983 | cpumask_t cpu_present_map; | |
3984 | EXPORT_SYMBOL(cpu_present_map); | |
3985 | ||
3986 | #ifndef CONFIG_SMP | |
3987 | cpumask_t cpu_online_map = CPU_MASK_ALL; | |
3988 | cpumask_t cpu_possible_map = CPU_MASK_ALL; | |
3989 | #endif | |
3990 | ||
3991 | long sched_getaffinity(pid_t pid, cpumask_t *mask) | |
3992 | { | |
3993 | int retval; | |
3994 | task_t *p; | |
3995 | ||
3996 | lock_cpu_hotplug(); | |
3997 | read_lock(&tasklist_lock); | |
3998 | ||
3999 | retval = -ESRCH; | |
4000 | p = find_process_by_pid(pid); | |
4001 | if (!p) | |
4002 | goto out_unlock; | |
4003 | ||
4004 | retval = 0; | |
4005 | cpus_and(*mask, p->cpus_allowed, cpu_possible_map); | |
4006 | ||
4007 | out_unlock: | |
4008 | read_unlock(&tasklist_lock); | |
4009 | unlock_cpu_hotplug(); | |
4010 | if (retval) | |
4011 | return retval; | |
4012 | ||
4013 | return 0; | |
4014 | } | |
4015 | ||
4016 | /** | |
4017 | * sys_sched_getaffinity - get the cpu affinity of a process | |
4018 | * @pid: pid of the process | |
4019 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr | |
4020 | * @user_mask_ptr: user-space pointer to hold the current cpu mask | |
4021 | */ | |
4022 | asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len, | |
4023 | unsigned long __user *user_mask_ptr) | |
4024 | { | |
4025 | int ret; | |
4026 | cpumask_t mask; | |
4027 | ||
4028 | if (len < sizeof(cpumask_t)) | |
4029 | return -EINVAL; | |
4030 | ||
4031 | ret = sched_getaffinity(pid, &mask); | |
4032 | if (ret < 0) | |
4033 | return ret; | |
4034 | ||
4035 | if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t))) | |
4036 | return -EFAULT; | |
4037 | ||
4038 | return sizeof(cpumask_t); | |
4039 | } | |
4040 | ||
4041 | /** | |
4042 | * sys_sched_yield - yield the current processor to other threads. | |
4043 | * | |
4044 | * this function yields the current CPU by moving the calling thread | |
4045 | * to the expired array. If there are no other threads running on this | |
4046 | * CPU then this function will return. | |
4047 | */ | |
4048 | asmlinkage long sys_sched_yield(void) | |
4049 | { | |
4050 | runqueue_t *rq = this_rq_lock(); | |
4051 | prio_array_t *array = current->array; | |
4052 | prio_array_t *target = rq->expired; | |
4053 | ||
4054 | schedstat_inc(rq, yld_cnt); | |
4055 | /* | |
4056 | * We implement yielding by moving the task into the expired | |
4057 | * queue. | |
4058 | * | |
4059 | * (special rule: RT tasks will just roundrobin in the active | |
4060 | * array.) | |
4061 | */ | |
4062 | if (rt_task(current)) | |
4063 | target = rq->active; | |
4064 | ||
5927ad78 | 4065 | if (array->nr_active == 1) { |
1da177e4 LT |
4066 | schedstat_inc(rq, yld_act_empty); |
4067 | if (!rq->expired->nr_active) | |
4068 | schedstat_inc(rq, yld_both_empty); | |
4069 | } else if (!rq->expired->nr_active) | |
4070 | schedstat_inc(rq, yld_exp_empty); | |
4071 | ||
4072 | if (array != target) { | |
4073 | dequeue_task(current, array); | |
4074 | enqueue_task(current, target); | |
4075 | } else | |
4076 | /* | |
4077 | * requeue_task is cheaper so perform that if possible. | |
4078 | */ | |
4079 | requeue_task(current, array); | |
4080 | ||
4081 | /* | |
4082 | * Since we are going to call schedule() anyway, there's | |
4083 | * no need to preempt or enable interrupts: | |
4084 | */ | |
4085 | __release(rq->lock); | |
4086 | _raw_spin_unlock(&rq->lock); | |
4087 | preempt_enable_no_resched(); | |
4088 | ||
4089 | schedule(); | |
4090 | ||
4091 | return 0; | |
4092 | } | |
4093 | ||
4094 | static inline void __cond_resched(void) | |
4095 | { | |
5bbcfd90 IM |
4096 | /* |
4097 | * The BKS might be reacquired before we have dropped | |
4098 | * PREEMPT_ACTIVE, which could trigger a second | |
4099 | * cond_resched() call. | |
4100 | */ | |
4101 | if (unlikely(preempt_count())) | |
4102 | return; | |
1da177e4 LT |
4103 | do { |
4104 | add_preempt_count(PREEMPT_ACTIVE); | |
4105 | schedule(); | |
4106 | sub_preempt_count(PREEMPT_ACTIVE); | |
4107 | } while (need_resched()); | |
4108 | } | |
4109 | ||
4110 | int __sched cond_resched(void) | |
4111 | { | |
4112 | if (need_resched()) { | |
4113 | __cond_resched(); | |
4114 | return 1; | |
4115 | } | |
4116 | return 0; | |
4117 | } | |
4118 | ||
4119 | EXPORT_SYMBOL(cond_resched); | |
4120 | ||
4121 | /* | |
4122 | * cond_resched_lock() - if a reschedule is pending, drop the given lock, | |
4123 | * call schedule, and on return reacquire the lock. | |
4124 | * | |
4125 | * This works OK both with and without CONFIG_PREEMPT. We do strange low-level | |
4126 | * operations here to prevent schedule() from being called twice (once via | |
4127 | * spin_unlock(), once by hand). | |
4128 | */ | |
95cdf3b7 | 4129 | int cond_resched_lock(spinlock_t *lock) |
1da177e4 | 4130 | { |
6df3cecb JK |
4131 | int ret = 0; |
4132 | ||
1da177e4 LT |
4133 | if (need_lockbreak(lock)) { |
4134 | spin_unlock(lock); | |
4135 | cpu_relax(); | |
6df3cecb | 4136 | ret = 1; |
1da177e4 LT |
4137 | spin_lock(lock); |
4138 | } | |
4139 | if (need_resched()) { | |
4140 | _raw_spin_unlock(lock); | |
4141 | preempt_enable_no_resched(); | |
4142 | __cond_resched(); | |
6df3cecb | 4143 | ret = 1; |
1da177e4 | 4144 | spin_lock(lock); |
1da177e4 | 4145 | } |
6df3cecb | 4146 | return ret; |
1da177e4 LT |
4147 | } |
4148 | ||
4149 | EXPORT_SYMBOL(cond_resched_lock); | |
4150 | ||
4151 | int __sched cond_resched_softirq(void) | |
4152 | { | |
4153 | BUG_ON(!in_softirq()); | |
4154 | ||
4155 | if (need_resched()) { | |
4156 | __local_bh_enable(); | |
4157 | __cond_resched(); | |
4158 | local_bh_disable(); | |
4159 | return 1; | |
4160 | } | |
4161 | return 0; | |
4162 | } | |
4163 | ||
4164 | EXPORT_SYMBOL(cond_resched_softirq); | |
4165 | ||
4166 | ||
4167 | /** | |
4168 | * yield - yield the current processor to other threads. | |
4169 | * | |
4170 | * this is a shortcut for kernel-space yielding - it marks the | |
4171 | * thread runnable and calls sys_sched_yield(). | |
4172 | */ | |
4173 | void __sched yield(void) | |
4174 | { | |
4175 | set_current_state(TASK_RUNNING); | |
4176 | sys_sched_yield(); | |
4177 | } | |
4178 | ||
4179 | EXPORT_SYMBOL(yield); | |
4180 | ||
4181 | /* | |
4182 | * This task is about to go to sleep on IO. Increment rq->nr_iowait so | |
4183 | * that process accounting knows that this is a task in IO wait state. | |
4184 | * | |
4185 | * But don't do that if it is a deliberate, throttling IO wait (this task | |
4186 | * has set its backing_dev_info: the queue against which it should throttle) | |
4187 | */ | |
4188 | void __sched io_schedule(void) | |
4189 | { | |
39c715b7 | 4190 | struct runqueue *rq = &per_cpu(runqueues, raw_smp_processor_id()); |
1da177e4 LT |
4191 | |
4192 | atomic_inc(&rq->nr_iowait); | |
4193 | schedule(); | |
4194 | atomic_dec(&rq->nr_iowait); | |
4195 | } | |
4196 | ||
4197 | EXPORT_SYMBOL(io_schedule); | |
4198 | ||
4199 | long __sched io_schedule_timeout(long timeout) | |
4200 | { | |
39c715b7 | 4201 | struct runqueue *rq = &per_cpu(runqueues, raw_smp_processor_id()); |
1da177e4 LT |
4202 | long ret; |
4203 | ||
4204 | atomic_inc(&rq->nr_iowait); | |
4205 | ret = schedule_timeout(timeout); | |
4206 | atomic_dec(&rq->nr_iowait); | |
4207 | return ret; | |
4208 | } | |
4209 | ||
4210 | /** | |
4211 | * sys_sched_get_priority_max - return maximum RT priority. | |
4212 | * @policy: scheduling class. | |
4213 | * | |
4214 | * this syscall returns the maximum rt_priority that can be used | |
4215 | * by a given scheduling class. | |
4216 | */ | |
4217 | asmlinkage long sys_sched_get_priority_max(int policy) | |
4218 | { | |
4219 | int ret = -EINVAL; | |
4220 | ||
4221 | switch (policy) { | |
4222 | case SCHED_FIFO: | |
4223 | case SCHED_RR: | |
4224 | ret = MAX_USER_RT_PRIO-1; | |
4225 | break; | |
4226 | case SCHED_NORMAL: | |
4227 | ret = 0; | |
4228 | break; | |
4229 | } | |
4230 | return ret; | |
4231 | } | |
4232 | ||
4233 | /** | |
4234 | * sys_sched_get_priority_min - return minimum RT priority. | |
4235 | * @policy: scheduling class. | |
4236 | * | |
4237 | * this syscall returns the minimum rt_priority that can be used | |
4238 | * by a given scheduling class. | |
4239 | */ | |
4240 | asmlinkage long sys_sched_get_priority_min(int policy) | |
4241 | { | |
4242 | int ret = -EINVAL; | |
4243 | ||
4244 | switch (policy) { | |
4245 | case SCHED_FIFO: | |
4246 | case SCHED_RR: | |
4247 | ret = 1; | |
4248 | break; | |
4249 | case SCHED_NORMAL: | |
4250 | ret = 0; | |
4251 | } | |
4252 | return ret; | |
4253 | } | |
4254 | ||
4255 | /** | |
4256 | * sys_sched_rr_get_interval - return the default timeslice of a process. | |
4257 | * @pid: pid of the process. | |
4258 | * @interval: userspace pointer to the timeslice value. | |
4259 | * | |
4260 | * this syscall writes the default timeslice value of a given process | |
4261 | * into the user-space timespec buffer. A value of '0' means infinity. | |
4262 | */ | |
4263 | asmlinkage | |
4264 | long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval) | |
4265 | { | |
4266 | int retval = -EINVAL; | |
4267 | struct timespec t; | |
4268 | task_t *p; | |
4269 | ||
4270 | if (pid < 0) | |
4271 | goto out_nounlock; | |
4272 | ||
4273 | retval = -ESRCH; | |
4274 | read_lock(&tasklist_lock); | |
4275 | p = find_process_by_pid(pid); | |
4276 | if (!p) | |
4277 | goto out_unlock; | |
4278 | ||
4279 | retval = security_task_getscheduler(p); | |
4280 | if (retval) | |
4281 | goto out_unlock; | |
4282 | ||
4283 | jiffies_to_timespec(p->policy & SCHED_FIFO ? | |
4284 | 0 : task_timeslice(p), &t); | |
4285 | read_unlock(&tasklist_lock); | |
4286 | retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0; | |
4287 | out_nounlock: | |
4288 | return retval; | |
4289 | out_unlock: | |
4290 | read_unlock(&tasklist_lock); | |
4291 | return retval; | |
4292 | } | |
4293 | ||
4294 | static inline struct task_struct *eldest_child(struct task_struct *p) | |
4295 | { | |
4296 | if (list_empty(&p->children)) return NULL; | |
4297 | return list_entry(p->children.next,struct task_struct,sibling); | |
4298 | } | |
4299 | ||
4300 | static inline struct task_struct *older_sibling(struct task_struct *p) | |
4301 | { | |
4302 | if (p->sibling.prev==&p->parent->children) return NULL; | |
4303 | return list_entry(p->sibling.prev,struct task_struct,sibling); | |
4304 | } | |
4305 | ||
4306 | static inline struct task_struct *younger_sibling(struct task_struct *p) | |
4307 | { | |
4308 | if (p->sibling.next==&p->parent->children) return NULL; | |
4309 | return list_entry(p->sibling.next,struct task_struct,sibling); | |
4310 | } | |
4311 | ||
95cdf3b7 | 4312 | static void show_task(task_t *p) |
1da177e4 LT |
4313 | { |
4314 | task_t *relative; | |
4315 | unsigned state; | |
4316 | unsigned long free = 0; | |
4317 | static const char *stat_nam[] = { "R", "S", "D", "T", "t", "Z", "X" }; | |
4318 | ||
4319 | printk("%-13.13s ", p->comm); | |
4320 | state = p->state ? __ffs(p->state) + 1 : 0; | |
4321 | if (state < ARRAY_SIZE(stat_nam)) | |
4322 | printk(stat_nam[state]); | |
4323 | else | |
4324 | printk("?"); | |
4325 | #if (BITS_PER_LONG == 32) | |
4326 | if (state == TASK_RUNNING) | |
4327 | printk(" running "); | |
4328 | else | |
4329 | printk(" %08lX ", thread_saved_pc(p)); | |
4330 | #else | |
4331 | if (state == TASK_RUNNING) | |
4332 | printk(" running task "); | |
4333 | else | |
4334 | printk(" %016lx ", thread_saved_pc(p)); | |
4335 | #endif | |
4336 | #ifdef CONFIG_DEBUG_STACK_USAGE | |
4337 | { | |
10ebffde | 4338 | unsigned long *n = end_of_stack(p); |
1da177e4 LT |
4339 | while (!*n) |
4340 | n++; | |
10ebffde | 4341 | free = (unsigned long)n - (unsigned long)end_of_stack(p); |
1da177e4 LT |
4342 | } |
4343 | #endif | |
4344 | printk("%5lu %5d %6d ", free, p->pid, p->parent->pid); | |
4345 | if ((relative = eldest_child(p))) | |
4346 | printk("%5d ", relative->pid); | |
4347 | else | |
4348 | printk(" "); | |
4349 | if ((relative = younger_sibling(p))) | |
4350 | printk("%7d", relative->pid); | |
4351 | else | |
4352 | printk(" "); | |
4353 | if ((relative = older_sibling(p))) | |
4354 | printk(" %5d", relative->pid); | |
4355 | else | |
4356 | printk(" "); | |
4357 | if (!p->mm) | |
4358 | printk(" (L-TLB)\n"); | |
4359 | else | |
4360 | printk(" (NOTLB)\n"); | |
4361 | ||
4362 | if (state != TASK_RUNNING) | |
4363 | show_stack(p, NULL); | |
4364 | } | |
4365 | ||
4366 | void show_state(void) | |
4367 | { | |
4368 | task_t *g, *p; | |
4369 | ||
4370 | #if (BITS_PER_LONG == 32) | |
4371 | printk("\n" | |
4372 | " sibling\n"); | |
4373 | printk(" task PC pid father child younger older\n"); | |
4374 | #else | |
4375 | printk("\n" | |
4376 | " sibling\n"); | |
4377 | printk(" task PC pid father child younger older\n"); | |
4378 | #endif | |
4379 | read_lock(&tasklist_lock); | |
4380 | do_each_thread(g, p) { | |
4381 | /* | |
4382 | * reset the NMI-timeout, listing all files on a slow | |
4383 | * console might take alot of time: | |
4384 | */ | |
4385 | touch_nmi_watchdog(); | |
4386 | show_task(p); | |
4387 | } while_each_thread(g, p); | |
4388 | ||
4389 | read_unlock(&tasklist_lock); | |
de5097c2 | 4390 | mutex_debug_show_all_locks(); |
1da177e4 LT |
4391 | } |
4392 | ||
f340c0d1 IM |
4393 | /** |
4394 | * init_idle - set up an idle thread for a given CPU | |
4395 | * @idle: task in question | |
4396 | * @cpu: cpu the idle task belongs to | |
4397 | * | |
4398 | * NOTE: this function does not set the idle thread's NEED_RESCHED | |
4399 | * flag, to make booting more robust. | |
4400 | */ | |
1da177e4 LT |
4401 | void __devinit init_idle(task_t *idle, int cpu) |
4402 | { | |
4403 | runqueue_t *rq = cpu_rq(cpu); | |
4404 | unsigned long flags; | |
4405 | ||
4406 | idle->sleep_avg = 0; | |
4407 | idle->array = NULL; | |
4408 | idle->prio = MAX_PRIO; | |
4409 | idle->state = TASK_RUNNING; | |
4410 | idle->cpus_allowed = cpumask_of_cpu(cpu); | |
4411 | set_task_cpu(idle, cpu); | |
4412 | ||
4413 | spin_lock_irqsave(&rq->lock, flags); | |
4414 | rq->curr = rq->idle = idle; | |
4866cde0 NP |
4415 | #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW) |
4416 | idle->oncpu = 1; | |
4417 | #endif | |
1da177e4 LT |
4418 | spin_unlock_irqrestore(&rq->lock, flags); |
4419 | ||
4420 | /* Set the preempt count _outside_ the spinlocks! */ | |
4421 | #if defined(CONFIG_PREEMPT) && !defined(CONFIG_PREEMPT_BKL) | |
a1261f54 | 4422 | task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0); |
1da177e4 | 4423 | #else |
a1261f54 | 4424 | task_thread_info(idle)->preempt_count = 0; |
1da177e4 LT |
4425 | #endif |
4426 | } | |
4427 | ||
4428 | /* | |
4429 | * In a system that switches off the HZ timer nohz_cpu_mask | |
4430 | * indicates which cpus entered this state. This is used | |
4431 | * in the rcu update to wait only for active cpus. For system | |
4432 | * which do not switch off the HZ timer nohz_cpu_mask should | |
4433 | * always be CPU_MASK_NONE. | |
4434 | */ | |
4435 | cpumask_t nohz_cpu_mask = CPU_MASK_NONE; | |
4436 | ||
4437 | #ifdef CONFIG_SMP | |
4438 | /* | |
4439 | * This is how migration works: | |
4440 | * | |
4441 | * 1) we queue a migration_req_t structure in the source CPU's | |
4442 | * runqueue and wake up that CPU's migration thread. | |
4443 | * 2) we down() the locked semaphore => thread blocks. | |
4444 | * 3) migration thread wakes up (implicitly it forces the migrated | |
4445 | * thread off the CPU) | |
4446 | * 4) it gets the migration request and checks whether the migrated | |
4447 | * task is still in the wrong runqueue. | |
4448 | * 5) if it's in the wrong runqueue then the migration thread removes | |
4449 | * it and puts it into the right queue. | |
4450 | * 6) migration thread up()s the semaphore. | |
4451 | * 7) we wake up and the migration is done. | |
4452 | */ | |
4453 | ||
4454 | /* | |
4455 | * Change a given task's CPU affinity. Migrate the thread to a | |
4456 | * proper CPU and schedule it away if the CPU it's executing on | |
4457 | * is removed from the allowed bitmask. | |
4458 | * | |
4459 | * NOTE: the caller must have a valid reference to the task, the | |
4460 | * task must not exit() & deallocate itself prematurely. The | |
4461 | * call is not atomic; no spinlocks may be held. | |
4462 | */ | |
4463 | int set_cpus_allowed(task_t *p, cpumask_t new_mask) | |
4464 | { | |
4465 | unsigned long flags; | |
4466 | int ret = 0; | |
4467 | migration_req_t req; | |
4468 | runqueue_t *rq; | |
4469 | ||
4470 | rq = task_rq_lock(p, &flags); | |
4471 | if (!cpus_intersects(new_mask, cpu_online_map)) { | |
4472 | ret = -EINVAL; | |
4473 | goto out; | |
4474 | } | |
4475 | ||
4476 | p->cpus_allowed = new_mask; | |
4477 | /* Can the task run on the task's current CPU? If so, we're done */ | |
4478 | if (cpu_isset(task_cpu(p), new_mask)) | |
4479 | goto out; | |
4480 | ||
4481 | if (migrate_task(p, any_online_cpu(new_mask), &req)) { | |
4482 | /* Need help from migration thread: drop lock and wait. */ | |
4483 | task_rq_unlock(rq, &flags); | |
4484 | wake_up_process(rq->migration_thread); | |
4485 | wait_for_completion(&req.done); | |
4486 | tlb_migrate_finish(p->mm); | |
4487 | return 0; | |
4488 | } | |
4489 | out: | |
4490 | task_rq_unlock(rq, &flags); | |
4491 | return ret; | |
4492 | } | |
4493 | ||
4494 | EXPORT_SYMBOL_GPL(set_cpus_allowed); | |
4495 | ||
4496 | /* | |
4497 | * Move (not current) task off this cpu, onto dest cpu. We're doing | |
4498 | * this because either it can't run here any more (set_cpus_allowed() | |
4499 | * away from this CPU, or CPU going down), or because we're | |
4500 | * attempting to rebalance this task on exec (sched_exec). | |
4501 | * | |
4502 | * So we race with normal scheduler movements, but that's OK, as long | |
4503 | * as the task is no longer on this CPU. | |
4504 | */ | |
4505 | static void __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu) | |
4506 | { | |
4507 | runqueue_t *rq_dest, *rq_src; | |
4508 | ||
4509 | if (unlikely(cpu_is_offline(dest_cpu))) | |
4510 | return; | |
4511 | ||
4512 | rq_src = cpu_rq(src_cpu); | |
4513 | rq_dest = cpu_rq(dest_cpu); | |
4514 | ||
4515 | double_rq_lock(rq_src, rq_dest); | |
4516 | /* Already moved. */ | |
4517 | if (task_cpu(p) != src_cpu) | |
4518 | goto out; | |
4519 | /* Affinity changed (again). */ | |
4520 | if (!cpu_isset(dest_cpu, p->cpus_allowed)) | |
4521 | goto out; | |
4522 | ||
4523 | set_task_cpu(p, dest_cpu); | |
4524 | if (p->array) { | |
4525 | /* | |
4526 | * Sync timestamp with rq_dest's before activating. | |
4527 | * The same thing could be achieved by doing this step | |
4528 | * afterwards, and pretending it was a local activate. | |
4529 | * This way is cleaner and logically correct. | |
4530 | */ | |
4531 | p->timestamp = p->timestamp - rq_src->timestamp_last_tick | |
4532 | + rq_dest->timestamp_last_tick; | |
4533 | deactivate_task(p, rq_src); | |
4534 | activate_task(p, rq_dest, 0); | |
4535 | if (TASK_PREEMPTS_CURR(p, rq_dest)) | |
4536 | resched_task(rq_dest->curr); | |
4537 | } | |
4538 | ||
4539 | out: | |
4540 | double_rq_unlock(rq_src, rq_dest); | |
4541 | } | |
4542 | ||
4543 | /* | |
4544 | * migration_thread - this is a highprio system thread that performs | |
4545 | * thread migration by bumping thread off CPU then 'pushing' onto | |
4546 | * another runqueue. | |
4547 | */ | |
95cdf3b7 | 4548 | static int migration_thread(void *data) |
1da177e4 LT |
4549 | { |
4550 | runqueue_t *rq; | |
4551 | int cpu = (long)data; | |
4552 | ||
4553 | rq = cpu_rq(cpu); | |
4554 | BUG_ON(rq->migration_thread != current); | |
4555 | ||
4556 | set_current_state(TASK_INTERRUPTIBLE); | |
4557 | while (!kthread_should_stop()) { | |
4558 | struct list_head *head; | |
4559 | migration_req_t *req; | |
4560 | ||
3e1d1d28 | 4561 | try_to_freeze(); |
1da177e4 LT |
4562 | |
4563 | spin_lock_irq(&rq->lock); | |
4564 | ||
4565 | if (cpu_is_offline(cpu)) { | |
4566 | spin_unlock_irq(&rq->lock); | |
4567 | goto wait_to_die; | |
4568 | } | |
4569 | ||
4570 | if (rq->active_balance) { | |
4571 | active_load_balance(rq, cpu); | |
4572 | rq->active_balance = 0; | |
4573 | } | |
4574 | ||
4575 | head = &rq->migration_queue; | |
4576 | ||
4577 | if (list_empty(head)) { | |
4578 | spin_unlock_irq(&rq->lock); | |
4579 | schedule(); | |
4580 | set_current_state(TASK_INTERRUPTIBLE); | |
4581 | continue; | |
4582 | } | |
4583 | req = list_entry(head->next, migration_req_t, list); | |
4584 | list_del_init(head->next); | |
4585 | ||
674311d5 NP |
4586 | spin_unlock(&rq->lock); |
4587 | __migrate_task(req->task, cpu, req->dest_cpu); | |
4588 | local_irq_enable(); | |
1da177e4 LT |
4589 | |
4590 | complete(&req->done); | |
4591 | } | |
4592 | __set_current_state(TASK_RUNNING); | |
4593 | return 0; | |
4594 | ||
4595 | wait_to_die: | |
4596 | /* Wait for kthread_stop */ | |
4597 | set_current_state(TASK_INTERRUPTIBLE); | |
4598 | while (!kthread_should_stop()) { | |
4599 | schedule(); | |
4600 | set_current_state(TASK_INTERRUPTIBLE); | |
4601 | } | |
4602 | __set_current_state(TASK_RUNNING); | |
4603 | return 0; | |
4604 | } | |
4605 | ||
4606 | #ifdef CONFIG_HOTPLUG_CPU | |
4607 | /* Figure out where task on dead CPU should go, use force if neccessary. */ | |
4608 | static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *tsk) | |
4609 | { | |
4610 | int dest_cpu; | |
4611 | cpumask_t mask; | |
4612 | ||
4613 | /* On same node? */ | |
4614 | mask = node_to_cpumask(cpu_to_node(dead_cpu)); | |
4615 | cpus_and(mask, mask, tsk->cpus_allowed); | |
4616 | dest_cpu = any_online_cpu(mask); | |
4617 | ||
4618 | /* On any allowed CPU? */ | |
4619 | if (dest_cpu == NR_CPUS) | |
4620 | dest_cpu = any_online_cpu(tsk->cpus_allowed); | |
4621 | ||
4622 | /* No more Mr. Nice Guy. */ | |
4623 | if (dest_cpu == NR_CPUS) { | |
b39c4fab | 4624 | cpus_setall(tsk->cpus_allowed); |
1da177e4 LT |
4625 | dest_cpu = any_online_cpu(tsk->cpus_allowed); |
4626 | ||
4627 | /* | |
4628 | * Don't tell them about moving exiting tasks or | |
4629 | * kernel threads (both mm NULL), since they never | |
4630 | * leave kernel. | |
4631 | */ | |
4632 | if (tsk->mm && printk_ratelimit()) | |
4633 | printk(KERN_INFO "process %d (%s) no " | |
4634 | "longer affine to cpu%d\n", | |
4635 | tsk->pid, tsk->comm, dead_cpu); | |
4636 | } | |
4637 | __migrate_task(tsk, dead_cpu, dest_cpu); | |
4638 | } | |
4639 | ||
4640 | /* | |
4641 | * While a dead CPU has no uninterruptible tasks queued at this point, | |
4642 | * it might still have a nonzero ->nr_uninterruptible counter, because | |
4643 | * for performance reasons the counter is not stricly tracking tasks to | |
4644 | * their home CPUs. So we just add the counter to another CPU's counter, | |
4645 | * to keep the global sum constant after CPU-down: | |
4646 | */ | |
4647 | static void migrate_nr_uninterruptible(runqueue_t *rq_src) | |
4648 | { | |
4649 | runqueue_t *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL)); | |
4650 | unsigned long flags; | |
4651 | ||
4652 | local_irq_save(flags); | |
4653 | double_rq_lock(rq_src, rq_dest); | |
4654 | rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible; | |
4655 | rq_src->nr_uninterruptible = 0; | |
4656 | double_rq_unlock(rq_src, rq_dest); | |
4657 | local_irq_restore(flags); | |
4658 | } | |
4659 | ||
4660 | /* Run through task list and migrate tasks from the dead cpu. */ | |
4661 | static void migrate_live_tasks(int src_cpu) | |
4662 | { | |
4663 | struct task_struct *tsk, *t; | |
4664 | ||
4665 | write_lock_irq(&tasklist_lock); | |
4666 | ||
4667 | do_each_thread(t, tsk) { | |
4668 | if (tsk == current) | |
4669 | continue; | |
4670 | ||
4671 | if (task_cpu(tsk) == src_cpu) | |
4672 | move_task_off_dead_cpu(src_cpu, tsk); | |
4673 | } while_each_thread(t, tsk); | |
4674 | ||
4675 | write_unlock_irq(&tasklist_lock); | |
4676 | } | |
4677 | ||
4678 | /* Schedules idle task to be the next runnable task on current CPU. | |
4679 | * It does so by boosting its priority to highest possible and adding it to | |
4680 | * the _front_ of runqueue. Used by CPU offline code. | |
4681 | */ | |
4682 | void sched_idle_next(void) | |
4683 | { | |
4684 | int cpu = smp_processor_id(); | |
4685 | runqueue_t *rq = this_rq(); | |
4686 | struct task_struct *p = rq->idle; | |
4687 | unsigned long flags; | |
4688 | ||
4689 | /* cpu has to be offline */ | |
4690 | BUG_ON(cpu_online(cpu)); | |
4691 | ||
4692 | /* Strictly not necessary since rest of the CPUs are stopped by now | |
4693 | * and interrupts disabled on current cpu. | |
4694 | */ | |
4695 | spin_lock_irqsave(&rq->lock, flags); | |
4696 | ||
4697 | __setscheduler(p, SCHED_FIFO, MAX_RT_PRIO-1); | |
4698 | /* Add idle task to _front_ of it's priority queue */ | |
4699 | __activate_idle_task(p, rq); | |
4700 | ||
4701 | spin_unlock_irqrestore(&rq->lock, flags); | |
4702 | } | |
4703 | ||
4704 | /* Ensures that the idle task is using init_mm right before its cpu goes | |
4705 | * offline. | |
4706 | */ | |
4707 | void idle_task_exit(void) | |
4708 | { | |
4709 | struct mm_struct *mm = current->active_mm; | |
4710 | ||
4711 | BUG_ON(cpu_online(smp_processor_id())); | |
4712 | ||
4713 | if (mm != &init_mm) | |
4714 | switch_mm(mm, &init_mm, current); | |
4715 | mmdrop(mm); | |
4716 | } | |
4717 | ||
4718 | static void migrate_dead(unsigned int dead_cpu, task_t *tsk) | |
4719 | { | |
4720 | struct runqueue *rq = cpu_rq(dead_cpu); | |
4721 | ||
4722 | /* Must be exiting, otherwise would be on tasklist. */ | |
4723 | BUG_ON(tsk->exit_state != EXIT_ZOMBIE && tsk->exit_state != EXIT_DEAD); | |
4724 | ||
4725 | /* Cannot have done final schedule yet: would have vanished. */ | |
4726 | BUG_ON(tsk->flags & PF_DEAD); | |
4727 | ||
4728 | get_task_struct(tsk); | |
4729 | ||
4730 | /* | |
4731 | * Drop lock around migration; if someone else moves it, | |
4732 | * that's OK. No task can be added to this CPU, so iteration is | |
4733 | * fine. | |
4734 | */ | |
4735 | spin_unlock_irq(&rq->lock); | |
4736 | move_task_off_dead_cpu(dead_cpu, tsk); | |
4737 | spin_lock_irq(&rq->lock); | |
4738 | ||
4739 | put_task_struct(tsk); | |
4740 | } | |
4741 | ||
4742 | /* release_task() removes task from tasklist, so we won't find dead tasks. */ | |
4743 | static void migrate_dead_tasks(unsigned int dead_cpu) | |
4744 | { | |
4745 | unsigned arr, i; | |
4746 | struct runqueue *rq = cpu_rq(dead_cpu); | |
4747 | ||
4748 | for (arr = 0; arr < 2; arr++) { | |
4749 | for (i = 0; i < MAX_PRIO; i++) { | |
4750 | struct list_head *list = &rq->arrays[arr].queue[i]; | |
4751 | while (!list_empty(list)) | |
4752 | migrate_dead(dead_cpu, | |
4753 | list_entry(list->next, task_t, | |
4754 | run_list)); | |
4755 | } | |
4756 | } | |
4757 | } | |
4758 | #endif /* CONFIG_HOTPLUG_CPU */ | |
4759 | ||
4760 | /* | |
4761 | * migration_call - callback that gets triggered when a CPU is added. | |
4762 | * Here we can start up the necessary migration thread for the new CPU. | |
4763 | */ | |
4764 | static int migration_call(struct notifier_block *nfb, unsigned long action, | |
4765 | void *hcpu) | |
4766 | { | |
4767 | int cpu = (long)hcpu; | |
4768 | struct task_struct *p; | |
4769 | struct runqueue *rq; | |
4770 | unsigned long flags; | |
4771 | ||
4772 | switch (action) { | |
4773 | case CPU_UP_PREPARE: | |
4774 | p = kthread_create(migration_thread, hcpu, "migration/%d",cpu); | |
4775 | if (IS_ERR(p)) | |
4776 | return NOTIFY_BAD; | |
4777 | p->flags |= PF_NOFREEZE; | |
4778 | kthread_bind(p, cpu); | |
4779 | /* Must be high prio: stop_machine expects to yield to it. */ | |
4780 | rq = task_rq_lock(p, &flags); | |
4781 | __setscheduler(p, SCHED_FIFO, MAX_RT_PRIO-1); | |
4782 | task_rq_unlock(rq, &flags); | |
4783 | cpu_rq(cpu)->migration_thread = p; | |
4784 | break; | |
4785 | case CPU_ONLINE: | |
4786 | /* Strictly unneccessary, as first user will wake it. */ | |
4787 | wake_up_process(cpu_rq(cpu)->migration_thread); | |
4788 | break; | |
4789 | #ifdef CONFIG_HOTPLUG_CPU | |
4790 | case CPU_UP_CANCELED: | |
4791 | /* Unbind it from offline cpu so it can run. Fall thru. */ | |
a4c4af7c HC |
4792 | kthread_bind(cpu_rq(cpu)->migration_thread, |
4793 | any_online_cpu(cpu_online_map)); | |
1da177e4 LT |
4794 | kthread_stop(cpu_rq(cpu)->migration_thread); |
4795 | cpu_rq(cpu)->migration_thread = NULL; | |
4796 | break; | |
4797 | case CPU_DEAD: | |
4798 | migrate_live_tasks(cpu); | |
4799 | rq = cpu_rq(cpu); | |
4800 | kthread_stop(rq->migration_thread); | |
4801 | rq->migration_thread = NULL; | |
4802 | /* Idle task back to normal (off runqueue, low prio) */ | |
4803 | rq = task_rq_lock(rq->idle, &flags); | |
4804 | deactivate_task(rq->idle, rq); | |
4805 | rq->idle->static_prio = MAX_PRIO; | |
4806 | __setscheduler(rq->idle, SCHED_NORMAL, 0); | |
4807 | migrate_dead_tasks(cpu); | |
4808 | task_rq_unlock(rq, &flags); | |
4809 | migrate_nr_uninterruptible(rq); | |
4810 | BUG_ON(rq->nr_running != 0); | |
4811 | ||
4812 | /* No need to migrate the tasks: it was best-effort if | |
4813 | * they didn't do lock_cpu_hotplug(). Just wake up | |
4814 | * the requestors. */ | |
4815 | spin_lock_irq(&rq->lock); | |
4816 | while (!list_empty(&rq->migration_queue)) { | |
4817 | migration_req_t *req; | |
4818 | req = list_entry(rq->migration_queue.next, | |
4819 | migration_req_t, list); | |
1da177e4 LT |
4820 | list_del_init(&req->list); |
4821 | complete(&req->done); | |
4822 | } | |
4823 | spin_unlock_irq(&rq->lock); | |
4824 | break; | |
4825 | #endif | |
4826 | } | |
4827 | return NOTIFY_OK; | |
4828 | } | |
4829 | ||
4830 | /* Register at highest priority so that task migration (migrate_all_tasks) | |
4831 | * happens before everything else. | |
4832 | */ | |
4833 | static struct notifier_block __devinitdata migration_notifier = { | |
4834 | .notifier_call = migration_call, | |
4835 | .priority = 10 | |
4836 | }; | |
4837 | ||
4838 | int __init migration_init(void) | |
4839 | { | |
4840 | void *cpu = (void *)(long)smp_processor_id(); | |
4841 | /* Start one for boot CPU. */ | |
4842 | migration_call(&migration_notifier, CPU_UP_PREPARE, cpu); | |
4843 | migration_call(&migration_notifier, CPU_ONLINE, cpu); | |
4844 | register_cpu_notifier(&migration_notifier); | |
4845 | return 0; | |
4846 | } | |
4847 | #endif | |
4848 | ||
4849 | #ifdef CONFIG_SMP | |
1a20ff27 | 4850 | #undef SCHED_DOMAIN_DEBUG |
1da177e4 LT |
4851 | #ifdef SCHED_DOMAIN_DEBUG |
4852 | static void sched_domain_debug(struct sched_domain *sd, int cpu) | |
4853 | { | |
4854 | int level = 0; | |
4855 | ||
41c7ce9a NP |
4856 | if (!sd) { |
4857 | printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu); | |
4858 | return; | |
4859 | } | |
4860 | ||
1da177e4 LT |
4861 | printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu); |
4862 | ||
4863 | do { | |
4864 | int i; | |
4865 | char str[NR_CPUS]; | |
4866 | struct sched_group *group = sd->groups; | |
4867 | cpumask_t groupmask; | |
4868 | ||
4869 | cpumask_scnprintf(str, NR_CPUS, sd->span); | |
4870 | cpus_clear(groupmask); | |
4871 | ||
4872 | printk(KERN_DEBUG); | |
4873 | for (i = 0; i < level + 1; i++) | |
4874 | printk(" "); | |
4875 | printk("domain %d: ", level); | |
4876 | ||
4877 | if (!(sd->flags & SD_LOAD_BALANCE)) { | |
4878 | printk("does not load-balance\n"); | |
4879 | if (sd->parent) | |
4880 | printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain has parent"); | |
4881 | break; | |
4882 | } | |
4883 | ||
4884 | printk("span %s\n", str); | |
4885 | ||
4886 | if (!cpu_isset(cpu, sd->span)) | |
4887 | printk(KERN_ERR "ERROR: domain->span does not contain CPU%d\n", cpu); | |
4888 | if (!cpu_isset(cpu, group->cpumask)) | |
4889 | printk(KERN_ERR "ERROR: domain->groups does not contain CPU%d\n", cpu); | |
4890 | ||
4891 | printk(KERN_DEBUG); | |
4892 | for (i = 0; i < level + 2; i++) | |
4893 | printk(" "); | |
4894 | printk("groups:"); | |
4895 | do { | |
4896 | if (!group) { | |
4897 | printk("\n"); | |
4898 | printk(KERN_ERR "ERROR: group is NULL\n"); | |
4899 | break; | |
4900 | } | |
4901 | ||
4902 | if (!group->cpu_power) { | |
4903 | printk("\n"); | |
4904 | printk(KERN_ERR "ERROR: domain->cpu_power not set\n"); | |
4905 | } | |
4906 | ||
4907 | if (!cpus_weight(group->cpumask)) { | |
4908 | printk("\n"); | |
4909 | printk(KERN_ERR "ERROR: empty group\n"); | |
4910 | } | |
4911 | ||
4912 | if (cpus_intersects(groupmask, group->cpumask)) { | |
4913 | printk("\n"); | |
4914 | printk(KERN_ERR "ERROR: repeated CPUs\n"); | |
4915 | } | |
4916 | ||
4917 | cpus_or(groupmask, groupmask, group->cpumask); | |
4918 | ||
4919 | cpumask_scnprintf(str, NR_CPUS, group->cpumask); | |
4920 | printk(" %s", str); | |
4921 | ||
4922 | group = group->next; | |
4923 | } while (group != sd->groups); | |
4924 | printk("\n"); | |
4925 | ||
4926 | if (!cpus_equal(sd->span, groupmask)) | |
4927 | printk(KERN_ERR "ERROR: groups don't span domain->span\n"); | |
4928 | ||
4929 | level++; | |
4930 | sd = sd->parent; | |
4931 | ||
4932 | if (sd) { | |
4933 | if (!cpus_subset(groupmask, sd->span)) | |
4934 | printk(KERN_ERR "ERROR: parent span is not a superset of domain->span\n"); | |
4935 | } | |
4936 | ||
4937 | } while (sd); | |
4938 | } | |
4939 | #else | |
4940 | #define sched_domain_debug(sd, cpu) {} | |
4941 | #endif | |
4942 | ||
1a20ff27 | 4943 | static int sd_degenerate(struct sched_domain *sd) |
245af2c7 SS |
4944 | { |
4945 | if (cpus_weight(sd->span) == 1) | |
4946 | return 1; | |
4947 | ||
4948 | /* Following flags need at least 2 groups */ | |
4949 | if (sd->flags & (SD_LOAD_BALANCE | | |
4950 | SD_BALANCE_NEWIDLE | | |
4951 | SD_BALANCE_FORK | | |
4952 | SD_BALANCE_EXEC)) { | |
4953 | if (sd->groups != sd->groups->next) | |
4954 | return 0; | |
4955 | } | |
4956 | ||
4957 | /* Following flags don't use groups */ | |
4958 | if (sd->flags & (SD_WAKE_IDLE | | |
4959 | SD_WAKE_AFFINE | | |
4960 | SD_WAKE_BALANCE)) | |
4961 | return 0; | |
4962 | ||
4963 | return 1; | |
4964 | } | |
4965 | ||
1a20ff27 | 4966 | static int sd_parent_degenerate(struct sched_domain *sd, |
245af2c7 SS |
4967 | struct sched_domain *parent) |
4968 | { | |
4969 | unsigned long cflags = sd->flags, pflags = parent->flags; | |
4970 | ||
4971 | if (sd_degenerate(parent)) | |
4972 | return 1; | |
4973 | ||
4974 | if (!cpus_equal(sd->span, parent->span)) | |
4975 | return 0; | |
4976 | ||
4977 | /* Does parent contain flags not in child? */ | |
4978 | /* WAKE_BALANCE is a subset of WAKE_AFFINE */ | |
4979 | if (cflags & SD_WAKE_AFFINE) | |
4980 | pflags &= ~SD_WAKE_BALANCE; | |
4981 | /* Flags needing groups don't count if only 1 group in parent */ | |
4982 | if (parent->groups == parent->groups->next) { | |
4983 | pflags &= ~(SD_LOAD_BALANCE | | |
4984 | SD_BALANCE_NEWIDLE | | |
4985 | SD_BALANCE_FORK | | |
4986 | SD_BALANCE_EXEC); | |
4987 | } | |
4988 | if (~cflags & pflags) | |
4989 | return 0; | |
4990 | ||
4991 | return 1; | |
4992 | } | |
4993 | ||
1da177e4 LT |
4994 | /* |
4995 | * Attach the domain 'sd' to 'cpu' as its base domain. Callers must | |
4996 | * hold the hotplug lock. | |
4997 | */ | |
9c1cfda2 | 4998 | static void cpu_attach_domain(struct sched_domain *sd, int cpu) |
1da177e4 | 4999 | { |
1da177e4 | 5000 | runqueue_t *rq = cpu_rq(cpu); |
245af2c7 SS |
5001 | struct sched_domain *tmp; |
5002 | ||
5003 | /* Remove the sched domains which do not contribute to scheduling. */ | |
5004 | for (tmp = sd; tmp; tmp = tmp->parent) { | |
5005 | struct sched_domain *parent = tmp->parent; | |
5006 | if (!parent) | |
5007 | break; | |
5008 | if (sd_parent_degenerate(tmp, parent)) | |
5009 | tmp->parent = parent->parent; | |
5010 | } | |
5011 | ||
5012 | if (sd && sd_degenerate(sd)) | |
5013 | sd = sd->parent; | |
1da177e4 LT |
5014 | |
5015 | sched_domain_debug(sd, cpu); | |
5016 | ||
674311d5 | 5017 | rcu_assign_pointer(rq->sd, sd); |
1da177e4 LT |
5018 | } |
5019 | ||
5020 | /* cpus with isolated domains */ | |
9c1cfda2 | 5021 | static cpumask_t __devinitdata cpu_isolated_map = CPU_MASK_NONE; |
1da177e4 LT |
5022 | |
5023 | /* Setup the mask of cpus configured for isolated domains */ | |
5024 | static int __init isolated_cpu_setup(char *str) | |
5025 | { | |
5026 | int ints[NR_CPUS], i; | |
5027 | ||
5028 | str = get_options(str, ARRAY_SIZE(ints), ints); | |
5029 | cpus_clear(cpu_isolated_map); | |
5030 | for (i = 1; i <= ints[0]; i++) | |
5031 | if (ints[i] < NR_CPUS) | |
5032 | cpu_set(ints[i], cpu_isolated_map); | |
5033 | return 1; | |
5034 | } | |
5035 | ||
5036 | __setup ("isolcpus=", isolated_cpu_setup); | |
5037 | ||
5038 | /* | |
5039 | * init_sched_build_groups takes an array of groups, the cpumask we wish | |
5040 | * to span, and a pointer to a function which identifies what group a CPU | |
5041 | * belongs to. The return value of group_fn must be a valid index into the | |
5042 | * groups[] array, and must be >= 0 and < NR_CPUS (due to the fact that we | |
5043 | * keep track of groups covered with a cpumask_t). | |
5044 | * | |
5045 | * init_sched_build_groups will build a circular linked list of the groups | |
5046 | * covered by the given span, and will set each group's ->cpumask correctly, | |
5047 | * and ->cpu_power to 0. | |
5048 | */ | |
9c1cfda2 JH |
5049 | static void init_sched_build_groups(struct sched_group groups[], cpumask_t span, |
5050 | int (*group_fn)(int cpu)) | |
1da177e4 LT |
5051 | { |
5052 | struct sched_group *first = NULL, *last = NULL; | |
5053 | cpumask_t covered = CPU_MASK_NONE; | |
5054 | int i; | |
5055 | ||
5056 | for_each_cpu_mask(i, span) { | |
5057 | int group = group_fn(i); | |
5058 | struct sched_group *sg = &groups[group]; | |
5059 | int j; | |
5060 | ||
5061 | if (cpu_isset(i, covered)) | |
5062 | continue; | |
5063 | ||
5064 | sg->cpumask = CPU_MASK_NONE; | |
5065 | sg->cpu_power = 0; | |
5066 | ||
5067 | for_each_cpu_mask(j, span) { | |
5068 | if (group_fn(j) != group) | |
5069 | continue; | |
5070 | ||
5071 | cpu_set(j, covered); | |
5072 | cpu_set(j, sg->cpumask); | |
5073 | } | |
5074 | if (!first) | |
5075 | first = sg; | |
5076 | if (last) | |
5077 | last->next = sg; | |
5078 | last = sg; | |
5079 | } | |
5080 | last->next = first; | |
5081 | } | |
5082 | ||
9c1cfda2 | 5083 | #define SD_NODES_PER_DOMAIN 16 |
1da177e4 | 5084 | |
9c1cfda2 JH |
5085 | #ifdef CONFIG_NUMA |
5086 | /** | |
5087 | * find_next_best_node - find the next node to include in a sched_domain | |
5088 | * @node: node whose sched_domain we're building | |
5089 | * @used_nodes: nodes already in the sched_domain | |
5090 | * | |
5091 | * Find the next node to include in a given scheduling domain. Simply | |
5092 | * finds the closest node not already in the @used_nodes map. | |
5093 | * | |
5094 | * Should use nodemask_t. | |
5095 | */ | |
5096 | static int find_next_best_node(int node, unsigned long *used_nodes) | |
5097 | { | |
5098 | int i, n, val, min_val, best_node = 0; | |
5099 | ||
5100 | min_val = INT_MAX; | |
5101 | ||
5102 | for (i = 0; i < MAX_NUMNODES; i++) { | |
5103 | /* Start at @node */ | |
5104 | n = (node + i) % MAX_NUMNODES; | |
5105 | ||
5106 | if (!nr_cpus_node(n)) | |
5107 | continue; | |
5108 | ||
5109 | /* Skip already used nodes */ | |
5110 | if (test_bit(n, used_nodes)) | |
5111 | continue; | |
5112 | ||
5113 | /* Simple min distance search */ | |
5114 | val = node_distance(node, n); | |
5115 | ||
5116 | if (val < min_val) { | |
5117 | min_val = val; | |
5118 | best_node = n; | |
5119 | } | |
5120 | } | |
5121 | ||
5122 | set_bit(best_node, used_nodes); | |
5123 | return best_node; | |
5124 | } | |
5125 | ||
5126 | /** | |
5127 | * sched_domain_node_span - get a cpumask for a node's sched_domain | |
5128 | * @node: node whose cpumask we're constructing | |
5129 | * @size: number of nodes to include in this span | |
5130 | * | |
5131 | * Given a node, construct a good cpumask for its sched_domain to span. It | |
5132 | * should be one that prevents unnecessary balancing, but also spreads tasks | |
5133 | * out optimally. | |
5134 | */ | |
5135 | static cpumask_t sched_domain_node_span(int node) | |
5136 | { | |
5137 | int i; | |
5138 | cpumask_t span, nodemask; | |
5139 | DECLARE_BITMAP(used_nodes, MAX_NUMNODES); | |
5140 | ||
5141 | cpus_clear(span); | |
5142 | bitmap_zero(used_nodes, MAX_NUMNODES); | |
5143 | ||
5144 | nodemask = node_to_cpumask(node); | |
5145 | cpus_or(span, span, nodemask); | |
5146 | set_bit(node, used_nodes); | |
5147 | ||
5148 | for (i = 1; i < SD_NODES_PER_DOMAIN; i++) { | |
5149 | int next_node = find_next_best_node(node, used_nodes); | |
5150 | nodemask = node_to_cpumask(next_node); | |
5151 | cpus_or(span, span, nodemask); | |
5152 | } | |
5153 | ||
5154 | return span; | |
5155 | } | |
5156 | #endif | |
5157 | ||
5158 | /* | |
5159 | * At the moment, CONFIG_SCHED_SMT is never defined, but leave it in so we | |
5160 | * can switch it on easily if needed. | |
5161 | */ | |
1da177e4 LT |
5162 | #ifdef CONFIG_SCHED_SMT |
5163 | static DEFINE_PER_CPU(struct sched_domain, cpu_domains); | |
5164 | static struct sched_group sched_group_cpus[NR_CPUS]; | |
1a20ff27 | 5165 | static int cpu_to_cpu_group(int cpu) |
1da177e4 LT |
5166 | { |
5167 | return cpu; | |
5168 | } | |
5169 | #endif | |
5170 | ||
5171 | static DEFINE_PER_CPU(struct sched_domain, phys_domains); | |
5172 | static struct sched_group sched_group_phys[NR_CPUS]; | |
1a20ff27 | 5173 | static int cpu_to_phys_group(int cpu) |
1da177e4 LT |
5174 | { |
5175 | #ifdef CONFIG_SCHED_SMT | |
5176 | return first_cpu(cpu_sibling_map[cpu]); | |
5177 | #else | |
5178 | return cpu; | |
5179 | #endif | |
5180 | } | |
5181 | ||
5182 | #ifdef CONFIG_NUMA | |
1da177e4 | 5183 | /* |
9c1cfda2 JH |
5184 | * The init_sched_build_groups can't handle what we want to do with node |
5185 | * groups, so roll our own. Now each node has its own list of groups which | |
5186 | * gets dynamically allocated. | |
1da177e4 | 5187 | */ |
9c1cfda2 | 5188 | static DEFINE_PER_CPU(struct sched_domain, node_domains); |
d1b55138 | 5189 | static struct sched_group **sched_group_nodes_bycpu[NR_CPUS]; |
1da177e4 | 5190 | |
9c1cfda2 | 5191 | static DEFINE_PER_CPU(struct sched_domain, allnodes_domains); |
d1b55138 | 5192 | static struct sched_group *sched_group_allnodes_bycpu[NR_CPUS]; |
9c1cfda2 JH |
5193 | |
5194 | static int cpu_to_allnodes_group(int cpu) | |
5195 | { | |
5196 | return cpu_to_node(cpu); | |
1da177e4 LT |
5197 | } |
5198 | #endif | |
5199 | ||
5200 | /* | |
1a20ff27 DG |
5201 | * Build sched domains for a given set of cpus and attach the sched domains |
5202 | * to the individual cpus | |
1da177e4 | 5203 | */ |
9c1cfda2 | 5204 | void build_sched_domains(const cpumask_t *cpu_map) |
1da177e4 LT |
5205 | { |
5206 | int i; | |
d1b55138 JH |
5207 | #ifdef CONFIG_NUMA |
5208 | struct sched_group **sched_group_nodes = NULL; | |
5209 | struct sched_group *sched_group_allnodes = NULL; | |
5210 | ||
5211 | /* | |
5212 | * Allocate the per-node list of sched groups | |
5213 | */ | |
5214 | sched_group_nodes = kmalloc(sizeof(struct sched_group*)*MAX_NUMNODES, | |
5215 | GFP_ATOMIC); | |
5216 | if (!sched_group_nodes) { | |
5217 | printk(KERN_WARNING "Can not alloc sched group node list\n"); | |
5218 | return; | |
5219 | } | |
5220 | sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes; | |
5221 | #endif | |
1da177e4 LT |
5222 | |
5223 | /* | |
1a20ff27 | 5224 | * Set up domains for cpus specified by the cpu_map. |
1da177e4 | 5225 | */ |
1a20ff27 | 5226 | for_each_cpu_mask(i, *cpu_map) { |
1da177e4 LT |
5227 | int group; |
5228 | struct sched_domain *sd = NULL, *p; | |
5229 | cpumask_t nodemask = node_to_cpumask(cpu_to_node(i)); | |
5230 | ||
1a20ff27 | 5231 | cpus_and(nodemask, nodemask, *cpu_map); |
1da177e4 LT |
5232 | |
5233 | #ifdef CONFIG_NUMA | |
d1b55138 | 5234 | if (cpus_weight(*cpu_map) |
9c1cfda2 | 5235 | > SD_NODES_PER_DOMAIN*cpus_weight(nodemask)) { |
d1b55138 JH |
5236 | if (!sched_group_allnodes) { |
5237 | sched_group_allnodes | |
5238 | = kmalloc(sizeof(struct sched_group) | |
5239 | * MAX_NUMNODES, | |
5240 | GFP_KERNEL); | |
5241 | if (!sched_group_allnodes) { | |
5242 | printk(KERN_WARNING | |
5243 | "Can not alloc allnodes sched group\n"); | |
5244 | break; | |
5245 | } | |
5246 | sched_group_allnodes_bycpu[i] | |
5247 | = sched_group_allnodes; | |
5248 | } | |
9c1cfda2 JH |
5249 | sd = &per_cpu(allnodes_domains, i); |
5250 | *sd = SD_ALLNODES_INIT; | |
5251 | sd->span = *cpu_map; | |
5252 | group = cpu_to_allnodes_group(i); | |
5253 | sd->groups = &sched_group_allnodes[group]; | |
5254 | p = sd; | |
5255 | } else | |
5256 | p = NULL; | |
5257 | ||
1da177e4 | 5258 | sd = &per_cpu(node_domains, i); |
1da177e4 | 5259 | *sd = SD_NODE_INIT; |
9c1cfda2 JH |
5260 | sd->span = sched_domain_node_span(cpu_to_node(i)); |
5261 | sd->parent = p; | |
5262 | cpus_and(sd->span, sd->span, *cpu_map); | |
1da177e4 LT |
5263 | #endif |
5264 | ||
5265 | p = sd; | |
5266 | sd = &per_cpu(phys_domains, i); | |
5267 | group = cpu_to_phys_group(i); | |
5268 | *sd = SD_CPU_INIT; | |
5269 | sd->span = nodemask; | |
5270 | sd->parent = p; | |
5271 | sd->groups = &sched_group_phys[group]; | |
5272 | ||
5273 | #ifdef CONFIG_SCHED_SMT | |
5274 | p = sd; | |
5275 | sd = &per_cpu(cpu_domains, i); | |
5276 | group = cpu_to_cpu_group(i); | |
5277 | *sd = SD_SIBLING_INIT; | |
5278 | sd->span = cpu_sibling_map[i]; | |
1a20ff27 | 5279 | cpus_and(sd->span, sd->span, *cpu_map); |
1da177e4 LT |
5280 | sd->parent = p; |
5281 | sd->groups = &sched_group_cpus[group]; | |
5282 | #endif | |
5283 | } | |
5284 | ||
5285 | #ifdef CONFIG_SCHED_SMT | |
5286 | /* Set up CPU (sibling) groups */ | |
9c1cfda2 | 5287 | for_each_cpu_mask(i, *cpu_map) { |
1da177e4 | 5288 | cpumask_t this_sibling_map = cpu_sibling_map[i]; |
1a20ff27 | 5289 | cpus_and(this_sibling_map, this_sibling_map, *cpu_map); |
1da177e4 LT |
5290 | if (i != first_cpu(this_sibling_map)) |
5291 | continue; | |
5292 | ||
5293 | init_sched_build_groups(sched_group_cpus, this_sibling_map, | |
5294 | &cpu_to_cpu_group); | |
5295 | } | |
5296 | #endif | |
5297 | ||
5298 | /* Set up physical groups */ | |
5299 | for (i = 0; i < MAX_NUMNODES; i++) { | |
5300 | cpumask_t nodemask = node_to_cpumask(i); | |
5301 | ||
1a20ff27 | 5302 | cpus_and(nodemask, nodemask, *cpu_map); |
1da177e4 LT |
5303 | if (cpus_empty(nodemask)) |
5304 | continue; | |
5305 | ||
5306 | init_sched_build_groups(sched_group_phys, nodemask, | |
5307 | &cpu_to_phys_group); | |
5308 | } | |
5309 | ||
5310 | #ifdef CONFIG_NUMA | |
5311 | /* Set up node groups */ | |
d1b55138 JH |
5312 | if (sched_group_allnodes) |
5313 | init_sched_build_groups(sched_group_allnodes, *cpu_map, | |
5314 | &cpu_to_allnodes_group); | |
9c1cfda2 JH |
5315 | |
5316 | for (i = 0; i < MAX_NUMNODES; i++) { | |
5317 | /* Set up node groups */ | |
5318 | struct sched_group *sg, *prev; | |
5319 | cpumask_t nodemask = node_to_cpumask(i); | |
5320 | cpumask_t domainspan; | |
5321 | cpumask_t covered = CPU_MASK_NONE; | |
5322 | int j; | |
5323 | ||
5324 | cpus_and(nodemask, nodemask, *cpu_map); | |
d1b55138 JH |
5325 | if (cpus_empty(nodemask)) { |
5326 | sched_group_nodes[i] = NULL; | |
9c1cfda2 | 5327 | continue; |
d1b55138 | 5328 | } |
9c1cfda2 JH |
5329 | |
5330 | domainspan = sched_domain_node_span(i); | |
5331 | cpus_and(domainspan, domainspan, *cpu_map); | |
5332 | ||
5333 | sg = kmalloc(sizeof(struct sched_group), GFP_KERNEL); | |
5334 | sched_group_nodes[i] = sg; | |
5335 | for_each_cpu_mask(j, nodemask) { | |
5336 | struct sched_domain *sd; | |
5337 | sd = &per_cpu(node_domains, j); | |
5338 | sd->groups = sg; | |
5339 | if (sd->groups == NULL) { | |
5340 | /* Turn off balancing if we have no groups */ | |
5341 | sd->flags = 0; | |
5342 | } | |
5343 | } | |
5344 | if (!sg) { | |
5345 | printk(KERN_WARNING | |
5346 | "Can not alloc domain group for node %d\n", i); | |
5347 | continue; | |
5348 | } | |
5349 | sg->cpu_power = 0; | |
5350 | sg->cpumask = nodemask; | |
5351 | cpus_or(covered, covered, nodemask); | |
5352 | prev = sg; | |
5353 | ||
5354 | for (j = 0; j < MAX_NUMNODES; j++) { | |
5355 | cpumask_t tmp, notcovered; | |
5356 | int n = (i + j) % MAX_NUMNODES; | |
5357 | ||
5358 | cpus_complement(notcovered, covered); | |
5359 | cpus_and(tmp, notcovered, *cpu_map); | |
5360 | cpus_and(tmp, tmp, domainspan); | |
5361 | if (cpus_empty(tmp)) | |
5362 | break; | |
5363 | ||
5364 | nodemask = node_to_cpumask(n); | |
5365 | cpus_and(tmp, tmp, nodemask); | |
5366 | if (cpus_empty(tmp)) | |
5367 | continue; | |
5368 | ||
5369 | sg = kmalloc(sizeof(struct sched_group), GFP_KERNEL); | |
5370 | if (!sg) { | |
5371 | printk(KERN_WARNING | |
5372 | "Can not alloc domain group for node %d\n", j); | |
5373 | break; | |
5374 | } | |
5375 | sg->cpu_power = 0; | |
5376 | sg->cpumask = tmp; | |
5377 | cpus_or(covered, covered, tmp); | |
5378 | prev->next = sg; | |
5379 | prev = sg; | |
5380 | } | |
5381 | prev->next = sched_group_nodes[i]; | |
5382 | } | |
1da177e4 LT |
5383 | #endif |
5384 | ||
5385 | /* Calculate CPU power for physical packages and nodes */ | |
1a20ff27 | 5386 | for_each_cpu_mask(i, *cpu_map) { |
1da177e4 LT |
5387 | int power; |
5388 | struct sched_domain *sd; | |
5389 | #ifdef CONFIG_SCHED_SMT | |
5390 | sd = &per_cpu(cpu_domains, i); | |
5391 | power = SCHED_LOAD_SCALE; | |
5392 | sd->groups->cpu_power = power; | |
5393 | #endif | |
5394 | ||
5395 | sd = &per_cpu(phys_domains, i); | |
5396 | power = SCHED_LOAD_SCALE + SCHED_LOAD_SCALE * | |
5397 | (cpus_weight(sd->groups->cpumask)-1) / 10; | |
5398 | sd->groups->cpu_power = power; | |
5399 | ||
5400 | #ifdef CONFIG_NUMA | |
9c1cfda2 JH |
5401 | sd = &per_cpu(allnodes_domains, i); |
5402 | if (sd->groups) { | |
5403 | power = SCHED_LOAD_SCALE + SCHED_LOAD_SCALE * | |
5404 | (cpus_weight(sd->groups->cpumask)-1) / 10; | |
5405 | sd->groups->cpu_power = power; | |
1da177e4 LT |
5406 | } |
5407 | #endif | |
5408 | } | |
5409 | ||
9c1cfda2 JH |
5410 | #ifdef CONFIG_NUMA |
5411 | for (i = 0; i < MAX_NUMNODES; i++) { | |
5412 | struct sched_group *sg = sched_group_nodes[i]; | |
5413 | int j; | |
5414 | ||
5415 | if (sg == NULL) | |
5416 | continue; | |
5417 | next_sg: | |
5418 | for_each_cpu_mask(j, sg->cpumask) { | |
5419 | struct sched_domain *sd; | |
5420 | int power; | |
5421 | ||
5422 | sd = &per_cpu(phys_domains, j); | |
5423 | if (j != first_cpu(sd->groups->cpumask)) { | |
5424 | /* | |
5425 | * Only add "power" once for each | |
5426 | * physical package. | |
5427 | */ | |
5428 | continue; | |
5429 | } | |
5430 | power = SCHED_LOAD_SCALE + SCHED_LOAD_SCALE * | |
5431 | (cpus_weight(sd->groups->cpumask)-1) / 10; | |
5432 | ||
5433 | sg->cpu_power += power; | |
5434 | } | |
5435 | sg = sg->next; | |
5436 | if (sg != sched_group_nodes[i]) | |
5437 | goto next_sg; | |
5438 | } | |
5439 | #endif | |
5440 | ||
1da177e4 | 5441 | /* Attach the domains */ |
1a20ff27 | 5442 | for_each_cpu_mask(i, *cpu_map) { |
1da177e4 LT |
5443 | struct sched_domain *sd; |
5444 | #ifdef CONFIG_SCHED_SMT | |
5445 | sd = &per_cpu(cpu_domains, i); | |
5446 | #else | |
5447 | sd = &per_cpu(phys_domains, i); | |
5448 | #endif | |
5449 | cpu_attach_domain(sd, i); | |
5450 | } | |
5451 | } | |
1a20ff27 DG |
5452 | /* |
5453 | * Set up scheduler domains and groups. Callers must hold the hotplug lock. | |
5454 | */ | |
9c1cfda2 | 5455 | static void arch_init_sched_domains(const cpumask_t *cpu_map) |
1a20ff27 DG |
5456 | { |
5457 | cpumask_t cpu_default_map; | |
1da177e4 | 5458 | |
1a20ff27 DG |
5459 | /* |
5460 | * Setup mask for cpus without special case scheduling requirements. | |
5461 | * For now this just excludes isolated cpus, but could be used to | |
5462 | * exclude other special cases in the future. | |
5463 | */ | |
5464 | cpus_andnot(cpu_default_map, *cpu_map, cpu_isolated_map); | |
5465 | ||
5466 | build_sched_domains(&cpu_default_map); | |
5467 | } | |
5468 | ||
5469 | static void arch_destroy_sched_domains(const cpumask_t *cpu_map) | |
1da177e4 | 5470 | { |
9c1cfda2 JH |
5471 | #ifdef CONFIG_NUMA |
5472 | int i; | |
d1b55138 | 5473 | int cpu; |
1da177e4 | 5474 | |
d1b55138 JH |
5475 | for_each_cpu_mask(cpu, *cpu_map) { |
5476 | struct sched_group *sched_group_allnodes | |
5477 | = sched_group_allnodes_bycpu[cpu]; | |
5478 | struct sched_group **sched_group_nodes | |
5479 | = sched_group_nodes_bycpu[cpu]; | |
9c1cfda2 | 5480 | |
d1b55138 JH |
5481 | if (sched_group_allnodes) { |
5482 | kfree(sched_group_allnodes); | |
5483 | sched_group_allnodes_bycpu[cpu] = NULL; | |
5484 | } | |
5485 | ||
5486 | if (!sched_group_nodes) | |
9c1cfda2 | 5487 | continue; |
d1b55138 JH |
5488 | |
5489 | for (i = 0; i < MAX_NUMNODES; i++) { | |
5490 | cpumask_t nodemask = node_to_cpumask(i); | |
5491 | struct sched_group *oldsg, *sg = sched_group_nodes[i]; | |
5492 | ||
5493 | cpus_and(nodemask, nodemask, *cpu_map); | |
5494 | if (cpus_empty(nodemask)) | |
5495 | continue; | |
5496 | ||
5497 | if (sg == NULL) | |
5498 | continue; | |
5499 | sg = sg->next; | |
9c1cfda2 | 5500 | next_sg: |
d1b55138 JH |
5501 | oldsg = sg; |
5502 | sg = sg->next; | |
5503 | kfree(oldsg); | |
5504 | if (oldsg != sched_group_nodes[i]) | |
5505 | goto next_sg; | |
5506 | } | |
5507 | kfree(sched_group_nodes); | |
5508 | sched_group_nodes_bycpu[cpu] = NULL; | |
9c1cfda2 JH |
5509 | } |
5510 | #endif | |
5511 | } | |
1da177e4 | 5512 | |
1a20ff27 DG |
5513 | /* |
5514 | * Detach sched domains from a group of cpus specified in cpu_map | |
5515 | * These cpus will now be attached to the NULL domain | |
5516 | */ | |
5517 | static inline void detach_destroy_domains(const cpumask_t *cpu_map) | |
5518 | { | |
5519 | int i; | |
5520 | ||
5521 | for_each_cpu_mask(i, *cpu_map) | |
5522 | cpu_attach_domain(NULL, i); | |
5523 | synchronize_sched(); | |
5524 | arch_destroy_sched_domains(cpu_map); | |
5525 | } | |
5526 | ||
5527 | /* | |
5528 | * Partition sched domains as specified by the cpumasks below. | |
5529 | * This attaches all cpus from the cpumasks to the NULL domain, | |
5530 | * waits for a RCU quiescent period, recalculates sched | |
5531 | * domain information and then attaches them back to the | |
5532 | * correct sched domains | |
5533 | * Call with hotplug lock held | |
5534 | */ | |
5535 | void partition_sched_domains(cpumask_t *partition1, cpumask_t *partition2) | |
5536 | { | |
5537 | cpumask_t change_map; | |
5538 | ||
5539 | cpus_and(*partition1, *partition1, cpu_online_map); | |
5540 | cpus_and(*partition2, *partition2, cpu_online_map); | |
5541 | cpus_or(change_map, *partition1, *partition2); | |
5542 | ||
5543 | /* Detach sched domains from all of the affected cpus */ | |
5544 | detach_destroy_domains(&change_map); | |
5545 | if (!cpus_empty(*partition1)) | |
5546 | build_sched_domains(partition1); | |
5547 | if (!cpus_empty(*partition2)) | |
5548 | build_sched_domains(partition2); | |
5549 | } | |
5550 | ||
1da177e4 LT |
5551 | #ifdef CONFIG_HOTPLUG_CPU |
5552 | /* | |
5553 | * Force a reinitialization of the sched domains hierarchy. The domains | |
5554 | * and groups cannot be updated in place without racing with the balancing | |
41c7ce9a | 5555 | * code, so we temporarily attach all running cpus to the NULL domain |
1da177e4 LT |
5556 | * which will prevent rebalancing while the sched domains are recalculated. |
5557 | */ | |
5558 | static int update_sched_domains(struct notifier_block *nfb, | |
5559 | unsigned long action, void *hcpu) | |
5560 | { | |
1da177e4 LT |
5561 | switch (action) { |
5562 | case CPU_UP_PREPARE: | |
5563 | case CPU_DOWN_PREPARE: | |
1a20ff27 | 5564 | detach_destroy_domains(&cpu_online_map); |
1da177e4 LT |
5565 | return NOTIFY_OK; |
5566 | ||
5567 | case CPU_UP_CANCELED: | |
5568 | case CPU_DOWN_FAILED: | |
5569 | case CPU_ONLINE: | |
5570 | case CPU_DEAD: | |
5571 | /* | |
5572 | * Fall through and re-initialise the domains. | |
5573 | */ | |
5574 | break; | |
5575 | default: | |
5576 | return NOTIFY_DONE; | |
5577 | } | |
5578 | ||
5579 | /* The hotplug lock is already held by cpu_up/cpu_down */ | |
1a20ff27 | 5580 | arch_init_sched_domains(&cpu_online_map); |
1da177e4 LT |
5581 | |
5582 | return NOTIFY_OK; | |
5583 | } | |
5584 | #endif | |
5585 | ||
5586 | void __init sched_init_smp(void) | |
5587 | { | |
5588 | lock_cpu_hotplug(); | |
1a20ff27 | 5589 | arch_init_sched_domains(&cpu_online_map); |
1da177e4 LT |
5590 | unlock_cpu_hotplug(); |
5591 | /* XXX: Theoretical race here - CPU may be hotplugged now */ | |
5592 | hotcpu_notifier(update_sched_domains, 0); | |
5593 | } | |
5594 | #else | |
5595 | void __init sched_init_smp(void) | |
5596 | { | |
5597 | } | |
5598 | #endif /* CONFIG_SMP */ | |
5599 | ||
5600 | int in_sched_functions(unsigned long addr) | |
5601 | { | |
5602 | /* Linker adds these: start and end of __sched functions */ | |
5603 | extern char __sched_text_start[], __sched_text_end[]; | |
5604 | return in_lock_functions(addr) || | |
5605 | (addr >= (unsigned long)__sched_text_start | |
5606 | && addr < (unsigned long)__sched_text_end); | |
5607 | } | |
5608 | ||
5609 | void __init sched_init(void) | |
5610 | { | |
5611 | runqueue_t *rq; | |
5612 | int i, j, k; | |
5613 | ||
5614 | for (i = 0; i < NR_CPUS; i++) { | |
5615 | prio_array_t *array; | |
5616 | ||
5617 | rq = cpu_rq(i); | |
5618 | spin_lock_init(&rq->lock); | |
7897986b | 5619 | rq->nr_running = 0; |
1da177e4 LT |
5620 | rq->active = rq->arrays; |
5621 | rq->expired = rq->arrays + 1; | |
5622 | rq->best_expired_prio = MAX_PRIO; | |
5623 | ||
5624 | #ifdef CONFIG_SMP | |
41c7ce9a | 5625 | rq->sd = NULL; |
7897986b NP |
5626 | for (j = 1; j < 3; j++) |
5627 | rq->cpu_load[j] = 0; | |
1da177e4 LT |
5628 | rq->active_balance = 0; |
5629 | rq->push_cpu = 0; | |
5630 | rq->migration_thread = NULL; | |
5631 | INIT_LIST_HEAD(&rq->migration_queue); | |
5632 | #endif | |
5633 | atomic_set(&rq->nr_iowait, 0); | |
5634 | ||
5635 | for (j = 0; j < 2; j++) { | |
5636 | array = rq->arrays + j; | |
5637 | for (k = 0; k < MAX_PRIO; k++) { | |
5638 | INIT_LIST_HEAD(array->queue + k); | |
5639 | __clear_bit(k, array->bitmap); | |
5640 | } | |
5641 | // delimiter for bitsearch | |
5642 | __set_bit(MAX_PRIO, array->bitmap); | |
5643 | } | |
5644 | } | |
5645 | ||
5646 | /* | |
5647 | * The boot idle thread does lazy MMU switching as well: | |
5648 | */ | |
5649 | atomic_inc(&init_mm.mm_count); | |
5650 | enter_lazy_tlb(&init_mm, current); | |
5651 | ||
5652 | /* | |
5653 | * Make us the idle thread. Technically, schedule() should not be | |
5654 | * called from this thread, however somewhere below it might be, | |
5655 | * but because we are the idle thread, we just pick up running again | |
5656 | * when this runqueue becomes "idle". | |
5657 | */ | |
5658 | init_idle(current, smp_processor_id()); | |
5659 | } | |
5660 | ||
5661 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP | |
5662 | void __might_sleep(char *file, int line) | |
5663 | { | |
5664 | #if defined(in_atomic) | |
5665 | static unsigned long prev_jiffy; /* ratelimiting */ | |
5666 | ||
5667 | if ((in_atomic() || irqs_disabled()) && | |
5668 | system_state == SYSTEM_RUNNING && !oops_in_progress) { | |
5669 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) | |
5670 | return; | |
5671 | prev_jiffy = jiffies; | |
5672 | printk(KERN_ERR "Debug: sleeping function called from invalid" | |
5673 | " context at %s:%d\n", file, line); | |
5674 | printk("in_atomic():%d, irqs_disabled():%d\n", | |
5675 | in_atomic(), irqs_disabled()); | |
5676 | dump_stack(); | |
5677 | } | |
5678 | #endif | |
5679 | } | |
5680 | EXPORT_SYMBOL(__might_sleep); | |
5681 | #endif | |
5682 | ||
5683 | #ifdef CONFIG_MAGIC_SYSRQ | |
5684 | void normalize_rt_tasks(void) | |
5685 | { | |
5686 | struct task_struct *p; | |
5687 | prio_array_t *array; | |
5688 | unsigned long flags; | |
5689 | runqueue_t *rq; | |
5690 | ||
5691 | read_lock_irq(&tasklist_lock); | |
5692 | for_each_process (p) { | |
5693 | if (!rt_task(p)) | |
5694 | continue; | |
5695 | ||
5696 | rq = task_rq_lock(p, &flags); | |
5697 | ||
5698 | array = p->array; | |
5699 | if (array) | |
5700 | deactivate_task(p, task_rq(p)); | |
5701 | __setscheduler(p, SCHED_NORMAL, 0); | |
5702 | if (array) { | |
5703 | __activate_task(p, task_rq(p)); | |
5704 | resched_task(rq->curr); | |
5705 | } | |
5706 | ||
5707 | task_rq_unlock(rq, &flags); | |
5708 | } | |
5709 | read_unlock_irq(&tasklist_lock); | |
5710 | } | |
5711 | ||
5712 | #endif /* CONFIG_MAGIC_SYSRQ */ | |
1df5c10a LT |
5713 | |
5714 | #ifdef CONFIG_IA64 | |
5715 | /* | |
5716 | * These functions are only useful for the IA64 MCA handling. | |
5717 | * | |
5718 | * They can only be called when the whole system has been | |
5719 | * stopped - every CPU needs to be quiescent, and no scheduling | |
5720 | * activity can take place. Using them for anything else would | |
5721 | * be a serious bug, and as a result, they aren't even visible | |
5722 | * under any other configuration. | |
5723 | */ | |
5724 | ||
5725 | /** | |
5726 | * curr_task - return the current task for a given cpu. | |
5727 | * @cpu: the processor in question. | |
5728 | * | |
5729 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! | |
5730 | */ | |
5731 | task_t *curr_task(int cpu) | |
5732 | { | |
5733 | return cpu_curr(cpu); | |
5734 | } | |
5735 | ||
5736 | /** | |
5737 | * set_curr_task - set the current task for a given cpu. | |
5738 | * @cpu: the processor in question. | |
5739 | * @p: the task pointer to set. | |
5740 | * | |
5741 | * Description: This function must only be used when non-maskable interrupts | |
5742 | * are serviced on a separate stack. It allows the architecture to switch the | |
5743 | * notion of the current task on a cpu in a non-blocking manner. This function | |
5744 | * must be called with all CPU's synchronized, and interrupts disabled, the | |
5745 | * and caller must save the original value of the current task (see | |
5746 | * curr_task() above) and restore that value before reenabling interrupts and | |
5747 | * re-starting the system. | |
5748 | * | |
5749 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! | |
5750 | */ | |
5751 | void set_curr_task(int cpu, task_t *p) | |
5752 | { | |
5753 | cpu_curr(cpu) = p; | |
5754 | } | |
5755 | ||
5756 | #endif |