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