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 | |
b9131769 IM |
25 | * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins, |
26 | * Thomas Gleixner, Mike Kravetz | |
1da177e4 LT |
27 | */ |
28 | ||
29 | #include <linux/mm.h> | |
30 | #include <linux/module.h> | |
31 | #include <linux/nmi.h> | |
32 | #include <linux/init.h> | |
dff06c15 | 33 | #include <linux/uaccess.h> |
1da177e4 LT |
34 | #include <linux/highmem.h> |
35 | #include <linux/smp_lock.h> | |
36 | #include <asm/mmu_context.h> | |
37 | #include <linux/interrupt.h> | |
c59ede7b | 38 | #include <linux/capability.h> |
1da177e4 LT |
39 | #include <linux/completion.h> |
40 | #include <linux/kernel_stat.h> | |
9a11b49a | 41 | #include <linux/debug_locks.h> |
1da177e4 LT |
42 | #include <linux/security.h> |
43 | #include <linux/notifier.h> | |
44 | #include <linux/profile.h> | |
7dfb7103 | 45 | #include <linux/freezer.h> |
198e2f18 | 46 | #include <linux/vmalloc.h> |
1da177e4 LT |
47 | #include <linux/blkdev.h> |
48 | #include <linux/delay.h> | |
b488893a | 49 | #include <linux/pid_namespace.h> |
1da177e4 LT |
50 | #include <linux/smp.h> |
51 | #include <linux/threads.h> | |
52 | #include <linux/timer.h> | |
53 | #include <linux/rcupdate.h> | |
54 | #include <linux/cpu.h> | |
55 | #include <linux/cpuset.h> | |
56 | #include <linux/percpu.h> | |
57 | #include <linux/kthread.h> | |
58 | #include <linux/seq_file.h> | |
e692ab53 | 59 | #include <linux/sysctl.h> |
1da177e4 LT |
60 | #include <linux/syscalls.h> |
61 | #include <linux/times.h> | |
8f0ab514 | 62 | #include <linux/tsacct_kern.h> |
c6fd91f0 | 63 | #include <linux/kprobes.h> |
0ff92245 | 64 | #include <linux/delayacct.h> |
5517d86b | 65 | #include <linux/reciprocal_div.h> |
dff06c15 | 66 | #include <linux/unistd.h> |
f5ff8422 | 67 | #include <linux/pagemap.h> |
8f4d37ec | 68 | #include <linux/hrtimer.h> |
30914a58 | 69 | #include <linux/tick.h> |
434d53b0 | 70 | #include <linux/bootmem.h> |
f00b45c1 PZ |
71 | #include <linux/debugfs.h> |
72 | #include <linux/ctype.h> | |
1da177e4 | 73 | |
5517d86b | 74 | #include <asm/tlb.h> |
838225b4 | 75 | #include <asm/irq_regs.h> |
1da177e4 | 76 | |
b035b6de AD |
77 | /* |
78 | * Scheduler clock - returns current time in nanosec units. | |
79 | * This is default implementation. | |
80 | * Architectures and sub-architectures can override this. | |
81 | */ | |
82 | unsigned long long __attribute__((weak)) sched_clock(void) | |
83 | { | |
d6322faf | 84 | return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ); |
b035b6de AD |
85 | } |
86 | ||
1da177e4 LT |
87 | /* |
88 | * Convert user-nice values [ -20 ... 0 ... 19 ] | |
89 | * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ], | |
90 | * and back. | |
91 | */ | |
92 | #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20) | |
93 | #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20) | |
94 | #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio) | |
95 | ||
96 | /* | |
97 | * 'User priority' is the nice value converted to something we | |
98 | * can work with better when scaling various scheduler parameters, | |
99 | * it's a [ 0 ... 39 ] range. | |
100 | */ | |
101 | #define USER_PRIO(p) ((p)-MAX_RT_PRIO) | |
102 | #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio) | |
103 | #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO)) | |
104 | ||
105 | /* | |
d7876a08 | 106 | * Helpers for converting nanosecond timing to jiffy resolution |
1da177e4 | 107 | */ |
d6322faf | 108 | #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ)) |
1da177e4 | 109 | |
6aa645ea IM |
110 | #define NICE_0_LOAD SCHED_LOAD_SCALE |
111 | #define NICE_0_SHIFT SCHED_LOAD_SHIFT | |
112 | ||
1da177e4 LT |
113 | /* |
114 | * These are the 'tuning knobs' of the scheduler: | |
115 | * | |
a4ec24b4 | 116 | * default timeslice is 100 msecs (used only for SCHED_RR tasks). |
1da177e4 LT |
117 | * Timeslices get refilled after they expire. |
118 | */ | |
1da177e4 | 119 | #define DEF_TIMESLICE (100 * HZ / 1000) |
2dd73a4f | 120 | |
d0b27fa7 PZ |
121 | /* |
122 | * single value that denotes runtime == period, ie unlimited time. | |
123 | */ | |
124 | #define RUNTIME_INF ((u64)~0ULL) | |
125 | ||
5517d86b ED |
126 | #ifdef CONFIG_SMP |
127 | /* | |
128 | * Divide a load by a sched group cpu_power : (load / sg->__cpu_power) | |
129 | * Since cpu_power is a 'constant', we can use a reciprocal divide. | |
130 | */ | |
131 | static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load) | |
132 | { | |
133 | return reciprocal_divide(load, sg->reciprocal_cpu_power); | |
134 | } | |
135 | ||
136 | /* | |
137 | * Each time a sched group cpu_power is changed, | |
138 | * we must compute its reciprocal value | |
139 | */ | |
140 | static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val) | |
141 | { | |
142 | sg->__cpu_power += val; | |
143 | sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power); | |
144 | } | |
145 | #endif | |
146 | ||
e05606d3 IM |
147 | static inline int rt_policy(int policy) |
148 | { | |
149 | if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR)) | |
150 | return 1; | |
151 | return 0; | |
152 | } | |
153 | ||
154 | static inline int task_has_rt_policy(struct task_struct *p) | |
155 | { | |
156 | return rt_policy(p->policy); | |
157 | } | |
158 | ||
1da177e4 | 159 | /* |
6aa645ea | 160 | * This is the priority-queue data structure of the RT scheduling class: |
1da177e4 | 161 | */ |
6aa645ea IM |
162 | struct rt_prio_array { |
163 | DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */ | |
164 | struct list_head queue[MAX_RT_PRIO]; | |
165 | }; | |
166 | ||
d0b27fa7 | 167 | struct rt_bandwidth { |
ea736ed5 IM |
168 | /* nests inside the rq lock: */ |
169 | spinlock_t rt_runtime_lock; | |
170 | ktime_t rt_period; | |
171 | u64 rt_runtime; | |
172 | struct hrtimer rt_period_timer; | |
d0b27fa7 PZ |
173 | }; |
174 | ||
175 | static struct rt_bandwidth def_rt_bandwidth; | |
176 | ||
177 | static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun); | |
178 | ||
179 | static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer) | |
180 | { | |
181 | struct rt_bandwidth *rt_b = | |
182 | container_of(timer, struct rt_bandwidth, rt_period_timer); | |
183 | ktime_t now; | |
184 | int overrun; | |
185 | int idle = 0; | |
186 | ||
187 | for (;;) { | |
188 | now = hrtimer_cb_get_time(timer); | |
189 | overrun = hrtimer_forward(timer, now, rt_b->rt_period); | |
190 | ||
191 | if (!overrun) | |
192 | break; | |
193 | ||
194 | idle = do_sched_rt_period_timer(rt_b, overrun); | |
195 | } | |
196 | ||
197 | return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; | |
198 | } | |
199 | ||
200 | static | |
201 | void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime) | |
202 | { | |
203 | rt_b->rt_period = ns_to_ktime(period); | |
204 | rt_b->rt_runtime = runtime; | |
205 | ||
ac086bc2 PZ |
206 | spin_lock_init(&rt_b->rt_runtime_lock); |
207 | ||
d0b27fa7 PZ |
208 | hrtimer_init(&rt_b->rt_period_timer, |
209 | CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
210 | rt_b->rt_period_timer.function = sched_rt_period_timer; | |
211 | rt_b->rt_period_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ; | |
212 | } | |
213 | ||
214 | static void start_rt_bandwidth(struct rt_bandwidth *rt_b) | |
215 | { | |
216 | ktime_t now; | |
217 | ||
218 | if (rt_b->rt_runtime == RUNTIME_INF) | |
219 | return; | |
220 | ||
221 | if (hrtimer_active(&rt_b->rt_period_timer)) | |
222 | return; | |
223 | ||
224 | spin_lock(&rt_b->rt_runtime_lock); | |
225 | for (;;) { | |
226 | if (hrtimer_active(&rt_b->rt_period_timer)) | |
227 | break; | |
228 | ||
229 | now = hrtimer_cb_get_time(&rt_b->rt_period_timer); | |
230 | hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period); | |
231 | hrtimer_start(&rt_b->rt_period_timer, | |
232 | rt_b->rt_period_timer.expires, | |
233 | HRTIMER_MODE_ABS); | |
234 | } | |
235 | spin_unlock(&rt_b->rt_runtime_lock); | |
236 | } | |
237 | ||
238 | #ifdef CONFIG_RT_GROUP_SCHED | |
239 | static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b) | |
240 | { | |
241 | hrtimer_cancel(&rt_b->rt_period_timer); | |
242 | } | |
243 | #endif | |
244 | ||
052f1dc7 | 245 | #ifdef CONFIG_GROUP_SCHED |
29f59db3 | 246 | |
68318b8e SV |
247 | #include <linux/cgroup.h> |
248 | ||
29f59db3 SV |
249 | struct cfs_rq; |
250 | ||
6f505b16 PZ |
251 | static LIST_HEAD(task_groups); |
252 | ||
29f59db3 | 253 | /* task group related information */ |
4cf86d77 | 254 | struct task_group { |
052f1dc7 | 255 | #ifdef CONFIG_CGROUP_SCHED |
68318b8e SV |
256 | struct cgroup_subsys_state css; |
257 | #endif | |
052f1dc7 PZ |
258 | |
259 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
29f59db3 SV |
260 | /* schedulable entities of this group on each cpu */ |
261 | struct sched_entity **se; | |
262 | /* runqueue "owned" by this group on each cpu */ | |
263 | struct cfs_rq **cfs_rq; | |
264 | unsigned long shares; | |
052f1dc7 PZ |
265 | #endif |
266 | ||
267 | #ifdef CONFIG_RT_GROUP_SCHED | |
268 | struct sched_rt_entity **rt_se; | |
269 | struct rt_rq **rt_rq; | |
270 | ||
d0b27fa7 | 271 | struct rt_bandwidth rt_bandwidth; |
052f1dc7 | 272 | #endif |
6b2d7700 | 273 | |
ae8393e5 | 274 | struct rcu_head rcu; |
6f505b16 | 275 | struct list_head list; |
f473aa5e PZ |
276 | |
277 | struct task_group *parent; | |
278 | struct list_head siblings; | |
279 | struct list_head children; | |
29f59db3 SV |
280 | }; |
281 | ||
354d60c2 | 282 | #ifdef CONFIG_USER_SCHED |
eff766a6 PZ |
283 | |
284 | /* | |
285 | * Root task group. | |
286 | * Every UID task group (including init_task_group aka UID-0) will | |
287 | * be a child to this group. | |
288 | */ | |
289 | struct task_group root_task_group; | |
290 | ||
052f1dc7 | 291 | #ifdef CONFIG_FAIR_GROUP_SCHED |
29f59db3 SV |
292 | /* Default task group's sched entity on each cpu */ |
293 | static DEFINE_PER_CPU(struct sched_entity, init_sched_entity); | |
294 | /* Default task group's cfs_rq on each cpu */ | |
295 | static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp; | |
052f1dc7 PZ |
296 | #endif |
297 | ||
298 | #ifdef CONFIG_RT_GROUP_SCHED | |
299 | static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity); | |
300 | static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp; | |
052f1dc7 | 301 | #endif |
eff766a6 PZ |
302 | #else |
303 | #define root_task_group init_task_group | |
354d60c2 | 304 | #endif |
6f505b16 | 305 | |
8ed36996 | 306 | /* task_group_lock serializes add/remove of task groups and also changes to |
ec2c507f SV |
307 | * a task group's cpu shares. |
308 | */ | |
8ed36996 | 309 | static DEFINE_SPINLOCK(task_group_lock); |
ec2c507f | 310 | |
a1835615 SV |
311 | /* doms_cur_mutex serializes access to doms_cur[] array */ |
312 | static DEFINE_MUTEX(doms_cur_mutex); | |
313 | ||
052f1dc7 | 314 | #ifdef CONFIG_FAIR_GROUP_SCHED |
052f1dc7 PZ |
315 | #ifdef CONFIG_USER_SCHED |
316 | # define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD) | |
317 | #else | |
318 | # define INIT_TASK_GROUP_LOAD NICE_0_LOAD | |
319 | #endif | |
320 | ||
18d95a28 PZ |
321 | #define MIN_SHARES 2 |
322 | ||
052f1dc7 PZ |
323 | static int init_task_group_load = INIT_TASK_GROUP_LOAD; |
324 | #endif | |
325 | ||
29f59db3 | 326 | /* Default task group. |
3a252015 | 327 | * Every task in system belong to this group at bootup. |
29f59db3 | 328 | */ |
434d53b0 | 329 | struct task_group init_task_group; |
29f59db3 SV |
330 | |
331 | /* return group to which a task belongs */ | |
4cf86d77 | 332 | static inline struct task_group *task_group(struct task_struct *p) |
29f59db3 | 333 | { |
4cf86d77 | 334 | struct task_group *tg; |
9b5b7751 | 335 | |
052f1dc7 | 336 | #ifdef CONFIG_USER_SCHED |
24e377a8 | 337 | tg = p->user->tg; |
052f1dc7 | 338 | #elif defined(CONFIG_CGROUP_SCHED) |
68318b8e SV |
339 | tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id), |
340 | struct task_group, css); | |
24e377a8 | 341 | #else |
41a2d6cf | 342 | tg = &init_task_group; |
24e377a8 | 343 | #endif |
9b5b7751 | 344 | return tg; |
29f59db3 SV |
345 | } |
346 | ||
347 | /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */ | |
6f505b16 | 348 | static inline void set_task_rq(struct task_struct *p, unsigned int cpu) |
29f59db3 | 349 | { |
052f1dc7 | 350 | #ifdef CONFIG_FAIR_GROUP_SCHED |
ce96b5ac DA |
351 | p->se.cfs_rq = task_group(p)->cfs_rq[cpu]; |
352 | p->se.parent = task_group(p)->se[cpu]; | |
052f1dc7 | 353 | #endif |
6f505b16 | 354 | |
052f1dc7 | 355 | #ifdef CONFIG_RT_GROUP_SCHED |
6f505b16 PZ |
356 | p->rt.rt_rq = task_group(p)->rt_rq[cpu]; |
357 | p->rt.parent = task_group(p)->rt_se[cpu]; | |
052f1dc7 | 358 | #endif |
29f59db3 SV |
359 | } |
360 | ||
a1835615 SV |
361 | static inline void lock_doms_cur(void) |
362 | { | |
363 | mutex_lock(&doms_cur_mutex); | |
364 | } | |
365 | ||
366 | static inline void unlock_doms_cur(void) | |
367 | { | |
368 | mutex_unlock(&doms_cur_mutex); | |
369 | } | |
370 | ||
29f59db3 SV |
371 | #else |
372 | ||
6f505b16 | 373 | static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { } |
a1835615 SV |
374 | static inline void lock_doms_cur(void) { } |
375 | static inline void unlock_doms_cur(void) { } | |
29f59db3 | 376 | |
052f1dc7 | 377 | #endif /* CONFIG_GROUP_SCHED */ |
29f59db3 | 378 | |
6aa645ea IM |
379 | /* CFS-related fields in a runqueue */ |
380 | struct cfs_rq { | |
381 | struct load_weight load; | |
382 | unsigned long nr_running; | |
383 | ||
6aa645ea | 384 | u64 exec_clock; |
e9acbff6 | 385 | u64 min_vruntime; |
6aa645ea IM |
386 | |
387 | struct rb_root tasks_timeline; | |
388 | struct rb_node *rb_leftmost; | |
4a55bd5e PZ |
389 | |
390 | struct list_head tasks; | |
391 | struct list_head *balance_iterator; | |
392 | ||
393 | /* | |
394 | * 'curr' points to currently running entity on this cfs_rq. | |
6aa645ea IM |
395 | * It is set to NULL otherwise (i.e when none are currently running). |
396 | */ | |
aa2ac252 | 397 | struct sched_entity *curr, *next; |
ddc97297 PZ |
398 | |
399 | unsigned long nr_spread_over; | |
400 | ||
62160e3f | 401 | #ifdef CONFIG_FAIR_GROUP_SCHED |
6aa645ea IM |
402 | struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */ |
403 | ||
41a2d6cf IM |
404 | /* |
405 | * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in | |
6aa645ea IM |
406 | * a hierarchy). Non-leaf lrqs hold other higher schedulable entities |
407 | * (like users, containers etc.) | |
408 | * | |
409 | * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This | |
410 | * list is used during load balance. | |
411 | */ | |
41a2d6cf IM |
412 | struct list_head leaf_cfs_rq_list; |
413 | struct task_group *tg; /* group that "owns" this runqueue */ | |
18d95a28 PZ |
414 | |
415 | #ifdef CONFIG_SMP | |
416 | unsigned long task_weight; | |
417 | unsigned long shares; | |
418 | /* | |
419 | * We need space to build a sched_domain wide view of the full task | |
420 | * group tree, in order to avoid depending on dynamic memory allocation | |
421 | * during the load balancing we place this in the per cpu task group | |
422 | * hierarchy. This limits the load balancing to one instance per cpu, | |
423 | * but more should not be needed anyway. | |
424 | */ | |
425 | struct aggregate_struct { | |
426 | /* | |
427 | * load = weight(cpus) * f(tg) | |
428 | * | |
429 | * Where f(tg) is the recursive weight fraction assigned to | |
430 | * this group. | |
431 | */ | |
432 | unsigned long load; | |
433 | ||
434 | /* | |
435 | * part of the group weight distributed to this span. | |
436 | */ | |
437 | unsigned long shares; | |
438 | ||
439 | /* | |
440 | * The sum of all runqueue weights within this span. | |
441 | */ | |
442 | unsigned long rq_weight; | |
443 | ||
444 | /* | |
445 | * Weight contributed by tasks; this is the part we can | |
446 | * influence by moving tasks around. | |
447 | */ | |
448 | unsigned long task_weight; | |
449 | } aggregate; | |
450 | #endif | |
6aa645ea IM |
451 | #endif |
452 | }; | |
1da177e4 | 453 | |
6aa645ea IM |
454 | /* Real-Time classes' related field in a runqueue: */ |
455 | struct rt_rq { | |
456 | struct rt_prio_array active; | |
63489e45 | 457 | unsigned long rt_nr_running; |
052f1dc7 | 458 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
6f505b16 PZ |
459 | int highest_prio; /* highest queued rt task prio */ |
460 | #endif | |
fa85ae24 | 461 | #ifdef CONFIG_SMP |
73fe6aae | 462 | unsigned long rt_nr_migratory; |
a22d7fc1 | 463 | int overloaded; |
fa85ae24 | 464 | #endif |
6f505b16 | 465 | int rt_throttled; |
fa85ae24 | 466 | u64 rt_time; |
ac086bc2 | 467 | u64 rt_runtime; |
ea736ed5 | 468 | /* Nests inside the rq lock: */ |
ac086bc2 | 469 | spinlock_t rt_runtime_lock; |
6f505b16 | 470 | |
052f1dc7 | 471 | #ifdef CONFIG_RT_GROUP_SCHED |
23b0fdfc PZ |
472 | unsigned long rt_nr_boosted; |
473 | ||
6f505b16 PZ |
474 | struct rq *rq; |
475 | struct list_head leaf_rt_rq_list; | |
476 | struct task_group *tg; | |
477 | struct sched_rt_entity *rt_se; | |
478 | #endif | |
6aa645ea IM |
479 | }; |
480 | ||
57d885fe GH |
481 | #ifdef CONFIG_SMP |
482 | ||
483 | /* | |
484 | * We add the notion of a root-domain which will be used to define per-domain | |
0eab9146 IM |
485 | * variables. Each exclusive cpuset essentially defines an island domain by |
486 | * fully partitioning the member cpus from any other cpuset. Whenever a new | |
57d885fe GH |
487 | * exclusive cpuset is created, we also create and attach a new root-domain |
488 | * object. | |
489 | * | |
57d885fe GH |
490 | */ |
491 | struct root_domain { | |
492 | atomic_t refcount; | |
493 | cpumask_t span; | |
494 | cpumask_t online; | |
637f5085 | 495 | |
0eab9146 | 496 | /* |
637f5085 GH |
497 | * The "RT overload" flag: it gets set if a CPU has more than |
498 | * one runnable RT task. | |
499 | */ | |
500 | cpumask_t rto_mask; | |
0eab9146 | 501 | atomic_t rto_count; |
57d885fe GH |
502 | }; |
503 | ||
dc938520 GH |
504 | /* |
505 | * By default the system creates a single root-domain with all cpus as | |
506 | * members (mimicking the global state we have today). | |
507 | */ | |
57d885fe GH |
508 | static struct root_domain def_root_domain; |
509 | ||
510 | #endif | |
511 | ||
1da177e4 LT |
512 | /* |
513 | * This is the main, per-CPU runqueue data structure. | |
514 | * | |
515 | * Locking rule: those places that want to lock multiple runqueues | |
516 | * (such as the load balancing or the thread migration code), lock | |
517 | * acquire operations must be ordered by ascending &runqueue. | |
518 | */ | |
70b97a7f | 519 | struct rq { |
d8016491 IM |
520 | /* runqueue lock: */ |
521 | spinlock_t lock; | |
1da177e4 LT |
522 | |
523 | /* | |
524 | * nr_running and cpu_load should be in the same cacheline because | |
525 | * remote CPUs use both these fields when doing load calculation. | |
526 | */ | |
527 | unsigned long nr_running; | |
6aa645ea IM |
528 | #define CPU_LOAD_IDX_MAX 5 |
529 | unsigned long cpu_load[CPU_LOAD_IDX_MAX]; | |
bdecea3a | 530 | unsigned char idle_at_tick; |
46cb4b7c | 531 | #ifdef CONFIG_NO_HZ |
15934a37 | 532 | unsigned long last_tick_seen; |
46cb4b7c SS |
533 | unsigned char in_nohz_recently; |
534 | #endif | |
d8016491 IM |
535 | /* capture load from *all* tasks on this cpu: */ |
536 | struct load_weight load; | |
6aa645ea IM |
537 | unsigned long nr_load_updates; |
538 | u64 nr_switches; | |
539 | ||
540 | struct cfs_rq cfs; | |
6f505b16 | 541 | struct rt_rq rt; |
6f505b16 | 542 | |
6aa645ea | 543 | #ifdef CONFIG_FAIR_GROUP_SCHED |
d8016491 IM |
544 | /* list of leaf cfs_rq on this cpu: */ |
545 | struct list_head leaf_cfs_rq_list; | |
052f1dc7 PZ |
546 | #endif |
547 | #ifdef CONFIG_RT_GROUP_SCHED | |
6f505b16 | 548 | struct list_head leaf_rt_rq_list; |
1da177e4 | 549 | #endif |
1da177e4 LT |
550 | |
551 | /* | |
552 | * This is part of a global counter where only the total sum | |
553 | * over all CPUs matters. A task can increase this counter on | |
554 | * one CPU and if it got migrated afterwards it may decrease | |
555 | * it on another CPU. Always updated under the runqueue lock: | |
556 | */ | |
557 | unsigned long nr_uninterruptible; | |
558 | ||
36c8b586 | 559 | struct task_struct *curr, *idle; |
c9819f45 | 560 | unsigned long next_balance; |
1da177e4 | 561 | struct mm_struct *prev_mm; |
6aa645ea | 562 | |
6aa645ea IM |
563 | u64 clock, prev_clock_raw; |
564 | s64 clock_max_delta; | |
565 | ||
cc203d24 | 566 | unsigned int clock_warps, clock_overflows, clock_underflows; |
2aa44d05 IM |
567 | u64 idle_clock; |
568 | unsigned int clock_deep_idle_events; | |
529c7726 | 569 | u64 tick_timestamp; |
6aa645ea | 570 | |
1da177e4 LT |
571 | atomic_t nr_iowait; |
572 | ||
573 | #ifdef CONFIG_SMP | |
0eab9146 | 574 | struct root_domain *rd; |
1da177e4 LT |
575 | struct sched_domain *sd; |
576 | ||
577 | /* For active balancing */ | |
578 | int active_balance; | |
579 | int push_cpu; | |
d8016491 IM |
580 | /* cpu of this runqueue: */ |
581 | int cpu; | |
1da177e4 | 582 | |
36c8b586 | 583 | struct task_struct *migration_thread; |
1da177e4 LT |
584 | struct list_head migration_queue; |
585 | #endif | |
586 | ||
8f4d37ec PZ |
587 | #ifdef CONFIG_SCHED_HRTICK |
588 | unsigned long hrtick_flags; | |
589 | ktime_t hrtick_expire; | |
590 | struct hrtimer hrtick_timer; | |
591 | #endif | |
592 | ||
1da177e4 LT |
593 | #ifdef CONFIG_SCHEDSTATS |
594 | /* latency stats */ | |
595 | struct sched_info rq_sched_info; | |
596 | ||
597 | /* sys_sched_yield() stats */ | |
480b9434 KC |
598 | unsigned int yld_exp_empty; |
599 | unsigned int yld_act_empty; | |
600 | unsigned int yld_both_empty; | |
601 | unsigned int yld_count; | |
1da177e4 LT |
602 | |
603 | /* schedule() stats */ | |
480b9434 KC |
604 | unsigned int sched_switch; |
605 | unsigned int sched_count; | |
606 | unsigned int sched_goidle; | |
1da177e4 LT |
607 | |
608 | /* try_to_wake_up() stats */ | |
480b9434 KC |
609 | unsigned int ttwu_count; |
610 | unsigned int ttwu_local; | |
b8efb561 IM |
611 | |
612 | /* BKL stats */ | |
480b9434 | 613 | unsigned int bkl_count; |
1da177e4 | 614 | #endif |
fcb99371 | 615 | struct lock_class_key rq_lock_key; |
1da177e4 LT |
616 | }; |
617 | ||
f34e3b61 | 618 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); |
1da177e4 | 619 | |
dd41f596 IM |
620 | static inline void check_preempt_curr(struct rq *rq, struct task_struct *p) |
621 | { | |
622 | rq->curr->sched_class->check_preempt_curr(rq, p); | |
623 | } | |
624 | ||
0a2966b4 CL |
625 | static inline int cpu_of(struct rq *rq) |
626 | { | |
627 | #ifdef CONFIG_SMP | |
628 | return rq->cpu; | |
629 | #else | |
630 | return 0; | |
631 | #endif | |
632 | } | |
633 | ||
15934a37 GC |
634 | #ifdef CONFIG_NO_HZ |
635 | static inline bool nohz_on(int cpu) | |
636 | { | |
637 | return tick_get_tick_sched(cpu)->nohz_mode != NOHZ_MODE_INACTIVE; | |
638 | } | |
639 | ||
640 | static inline u64 max_skipped_ticks(struct rq *rq) | |
641 | { | |
642 | return nohz_on(cpu_of(rq)) ? jiffies - rq->last_tick_seen + 2 : 1; | |
643 | } | |
644 | ||
645 | static inline void update_last_tick_seen(struct rq *rq) | |
646 | { | |
647 | rq->last_tick_seen = jiffies; | |
648 | } | |
649 | #else | |
650 | static inline u64 max_skipped_ticks(struct rq *rq) | |
651 | { | |
652 | return 1; | |
653 | } | |
654 | ||
655 | static inline void update_last_tick_seen(struct rq *rq) | |
656 | { | |
657 | } | |
658 | #endif | |
659 | ||
20d315d4 | 660 | /* |
b04a0f4c IM |
661 | * Update the per-runqueue clock, as finegrained as the platform can give |
662 | * us, but without assuming monotonicity, etc.: | |
20d315d4 | 663 | */ |
b04a0f4c | 664 | static void __update_rq_clock(struct rq *rq) |
20d315d4 IM |
665 | { |
666 | u64 prev_raw = rq->prev_clock_raw; | |
667 | u64 now = sched_clock(); | |
668 | s64 delta = now - prev_raw; | |
669 | u64 clock = rq->clock; | |
670 | ||
b04a0f4c IM |
671 | #ifdef CONFIG_SCHED_DEBUG |
672 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); | |
673 | #endif | |
20d315d4 IM |
674 | /* |
675 | * Protect against sched_clock() occasionally going backwards: | |
676 | */ | |
677 | if (unlikely(delta < 0)) { | |
678 | clock++; | |
679 | rq->clock_warps++; | |
680 | } else { | |
681 | /* | |
682 | * Catch too large forward jumps too: | |
683 | */ | |
15934a37 GC |
684 | u64 max_jump = max_skipped_ticks(rq) * TICK_NSEC; |
685 | u64 max_time = rq->tick_timestamp + max_jump; | |
686 | ||
687 | if (unlikely(clock + delta > max_time)) { | |
688 | if (clock < max_time) | |
689 | clock = max_time; | |
529c7726 IM |
690 | else |
691 | clock++; | |
20d315d4 IM |
692 | rq->clock_overflows++; |
693 | } else { | |
694 | if (unlikely(delta > rq->clock_max_delta)) | |
695 | rq->clock_max_delta = delta; | |
696 | clock += delta; | |
697 | } | |
698 | } | |
699 | ||
700 | rq->prev_clock_raw = now; | |
701 | rq->clock = clock; | |
b04a0f4c | 702 | } |
20d315d4 | 703 | |
b04a0f4c IM |
704 | static void update_rq_clock(struct rq *rq) |
705 | { | |
706 | if (likely(smp_processor_id() == cpu_of(rq))) | |
707 | __update_rq_clock(rq); | |
20d315d4 IM |
708 | } |
709 | ||
674311d5 NP |
710 | /* |
711 | * The domain tree (rq->sd) is protected by RCU's quiescent state transition. | |
1a20ff27 | 712 | * See detach_destroy_domains: synchronize_sched for details. |
674311d5 NP |
713 | * |
714 | * The domain tree of any CPU may only be accessed from within | |
715 | * preempt-disabled sections. | |
716 | */ | |
48f24c4d IM |
717 | #define for_each_domain(cpu, __sd) \ |
718 | for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent) | |
1da177e4 LT |
719 | |
720 | #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu))) | |
721 | #define this_rq() (&__get_cpu_var(runqueues)) | |
722 | #define task_rq(p) cpu_rq(task_cpu(p)) | |
723 | #define cpu_curr(cpu) (cpu_rq(cpu)->curr) | |
724 | ||
bf5c91ba IM |
725 | /* |
726 | * Tunables that become constants when CONFIG_SCHED_DEBUG is off: | |
727 | */ | |
728 | #ifdef CONFIG_SCHED_DEBUG | |
729 | # define const_debug __read_mostly | |
730 | #else | |
731 | # define const_debug static const | |
732 | #endif | |
733 | ||
734 | /* | |
735 | * Debugging: various feature bits | |
736 | */ | |
f00b45c1 PZ |
737 | |
738 | #define SCHED_FEAT(name, enabled) \ | |
739 | __SCHED_FEAT_##name , | |
740 | ||
bf5c91ba | 741 | enum { |
f00b45c1 | 742 | #include "sched_features.h" |
bf5c91ba IM |
743 | }; |
744 | ||
f00b45c1 PZ |
745 | #undef SCHED_FEAT |
746 | ||
747 | #define SCHED_FEAT(name, enabled) \ | |
748 | (1UL << __SCHED_FEAT_##name) * enabled | | |
749 | ||
bf5c91ba | 750 | const_debug unsigned int sysctl_sched_features = |
f00b45c1 PZ |
751 | #include "sched_features.h" |
752 | 0; | |
753 | ||
754 | #undef SCHED_FEAT | |
755 | ||
756 | #ifdef CONFIG_SCHED_DEBUG | |
757 | #define SCHED_FEAT(name, enabled) \ | |
758 | #name , | |
759 | ||
760 | __read_mostly char *sched_feat_names[] = { | |
761 | #include "sched_features.h" | |
762 | NULL | |
763 | }; | |
764 | ||
765 | #undef SCHED_FEAT | |
766 | ||
767 | int sched_feat_open(struct inode *inode, struct file *filp) | |
768 | { | |
769 | filp->private_data = inode->i_private; | |
770 | return 0; | |
771 | } | |
772 | ||
773 | static ssize_t | |
774 | sched_feat_read(struct file *filp, char __user *ubuf, | |
775 | size_t cnt, loff_t *ppos) | |
776 | { | |
777 | char *buf; | |
778 | int r = 0; | |
779 | int len = 0; | |
780 | int i; | |
781 | ||
782 | for (i = 0; sched_feat_names[i]; i++) { | |
783 | len += strlen(sched_feat_names[i]); | |
784 | len += 4; | |
785 | } | |
786 | ||
787 | buf = kmalloc(len + 2, GFP_KERNEL); | |
788 | if (!buf) | |
789 | return -ENOMEM; | |
790 | ||
791 | for (i = 0; sched_feat_names[i]; i++) { | |
792 | if (sysctl_sched_features & (1UL << i)) | |
793 | r += sprintf(buf + r, "%s ", sched_feat_names[i]); | |
794 | else | |
c24b7c52 | 795 | r += sprintf(buf + r, "NO_%s ", sched_feat_names[i]); |
f00b45c1 PZ |
796 | } |
797 | ||
798 | r += sprintf(buf + r, "\n"); | |
799 | WARN_ON(r >= len + 2); | |
800 | ||
801 | r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r); | |
802 | ||
803 | kfree(buf); | |
804 | ||
805 | return r; | |
806 | } | |
807 | ||
808 | static ssize_t | |
809 | sched_feat_write(struct file *filp, const char __user *ubuf, | |
810 | size_t cnt, loff_t *ppos) | |
811 | { | |
812 | char buf[64]; | |
813 | char *cmp = buf; | |
814 | int neg = 0; | |
815 | int i; | |
816 | ||
817 | if (cnt > 63) | |
818 | cnt = 63; | |
819 | ||
820 | if (copy_from_user(&buf, ubuf, cnt)) | |
821 | return -EFAULT; | |
822 | ||
823 | buf[cnt] = 0; | |
824 | ||
c24b7c52 | 825 | if (strncmp(buf, "NO_", 3) == 0) { |
f00b45c1 PZ |
826 | neg = 1; |
827 | cmp += 3; | |
828 | } | |
829 | ||
830 | for (i = 0; sched_feat_names[i]; i++) { | |
831 | int len = strlen(sched_feat_names[i]); | |
832 | ||
833 | if (strncmp(cmp, sched_feat_names[i], len) == 0) { | |
834 | if (neg) | |
835 | sysctl_sched_features &= ~(1UL << i); | |
836 | else | |
837 | sysctl_sched_features |= (1UL << i); | |
838 | break; | |
839 | } | |
840 | } | |
841 | ||
842 | if (!sched_feat_names[i]) | |
843 | return -EINVAL; | |
844 | ||
845 | filp->f_pos += cnt; | |
846 | ||
847 | return cnt; | |
848 | } | |
849 | ||
850 | static struct file_operations sched_feat_fops = { | |
851 | .open = sched_feat_open, | |
852 | .read = sched_feat_read, | |
853 | .write = sched_feat_write, | |
854 | }; | |
855 | ||
856 | static __init int sched_init_debug(void) | |
857 | { | |
f00b45c1 PZ |
858 | debugfs_create_file("sched_features", 0644, NULL, NULL, |
859 | &sched_feat_fops); | |
860 | ||
861 | return 0; | |
862 | } | |
863 | late_initcall(sched_init_debug); | |
864 | ||
865 | #endif | |
866 | ||
867 | #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x)) | |
bf5c91ba | 868 | |
b82d9fdd PZ |
869 | /* |
870 | * Number of tasks to iterate in a single balance run. | |
871 | * Limited because this is done with IRQs disabled. | |
872 | */ | |
873 | const_debug unsigned int sysctl_sched_nr_migrate = 32; | |
874 | ||
fa85ae24 | 875 | /* |
9f0c1e56 | 876 | * period over which we measure -rt task cpu usage in us. |
fa85ae24 PZ |
877 | * default: 1s |
878 | */ | |
9f0c1e56 | 879 | unsigned int sysctl_sched_rt_period = 1000000; |
fa85ae24 | 880 | |
6892b75e IM |
881 | static __read_mostly int scheduler_running; |
882 | ||
9f0c1e56 PZ |
883 | /* |
884 | * part of the period that we allow rt tasks to run in us. | |
885 | * default: 0.95s | |
886 | */ | |
887 | int sysctl_sched_rt_runtime = 950000; | |
fa85ae24 | 888 | |
d0b27fa7 PZ |
889 | static inline u64 global_rt_period(void) |
890 | { | |
891 | return (u64)sysctl_sched_rt_period * NSEC_PER_USEC; | |
892 | } | |
893 | ||
894 | static inline u64 global_rt_runtime(void) | |
895 | { | |
896 | if (sysctl_sched_rt_period < 0) | |
897 | return RUNTIME_INF; | |
898 | ||
899 | return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC; | |
900 | } | |
fa85ae24 | 901 | |
27ec4407 IM |
902 | static const unsigned long long time_sync_thresh = 100000; |
903 | ||
904 | static DEFINE_PER_CPU(unsigned long long, time_offset); | |
905 | static DEFINE_PER_CPU(unsigned long long, prev_cpu_time); | |
906 | ||
e436d800 | 907 | /* |
27ec4407 IM |
908 | * Global lock which we take every now and then to synchronize |
909 | * the CPUs time. This method is not warp-safe, but it's good | |
910 | * enough to synchronize slowly diverging time sources and thus | |
911 | * it's good enough for tracing: | |
e436d800 | 912 | */ |
27ec4407 IM |
913 | static DEFINE_SPINLOCK(time_sync_lock); |
914 | static unsigned long long prev_global_time; | |
915 | ||
916 | static unsigned long long __sync_cpu_clock(cycles_t time, int cpu) | |
917 | { | |
918 | unsigned long flags; | |
919 | ||
920 | spin_lock_irqsave(&time_sync_lock, flags); | |
921 | ||
922 | if (time < prev_global_time) { | |
923 | per_cpu(time_offset, cpu) += prev_global_time - time; | |
924 | time = prev_global_time; | |
925 | } else { | |
926 | prev_global_time = time; | |
927 | } | |
928 | ||
929 | spin_unlock_irqrestore(&time_sync_lock, flags); | |
930 | ||
931 | return time; | |
932 | } | |
933 | ||
934 | static unsigned long long __cpu_clock(int cpu) | |
e436d800 | 935 | { |
e436d800 IM |
936 | unsigned long long now; |
937 | unsigned long flags; | |
b04a0f4c | 938 | struct rq *rq; |
e436d800 | 939 | |
8ced5f69 IM |
940 | /* |
941 | * Only call sched_clock() if the scheduler has already been | |
942 | * initialized (some code might call cpu_clock() very early): | |
943 | */ | |
6892b75e IM |
944 | if (unlikely(!scheduler_running)) |
945 | return 0; | |
946 | ||
947 | local_irq_save(flags); | |
948 | rq = cpu_rq(cpu); | |
949 | update_rq_clock(rq); | |
b04a0f4c | 950 | now = rq->clock; |
2cd4d0ea | 951 | local_irq_restore(flags); |
e436d800 IM |
952 | |
953 | return now; | |
954 | } | |
27ec4407 IM |
955 | |
956 | /* | |
957 | * For kernel-internal use: high-speed (but slightly incorrect) per-cpu | |
958 | * clock constructed from sched_clock(): | |
959 | */ | |
960 | unsigned long long cpu_clock(int cpu) | |
961 | { | |
962 | unsigned long long prev_cpu_time, time, delta_time; | |
963 | ||
964 | prev_cpu_time = per_cpu(prev_cpu_time, cpu); | |
965 | time = __cpu_clock(cpu) + per_cpu(time_offset, cpu); | |
966 | delta_time = time-prev_cpu_time; | |
967 | ||
968 | if (unlikely(delta_time > time_sync_thresh)) | |
969 | time = __sync_cpu_clock(time, cpu); | |
970 | ||
971 | return time; | |
972 | } | |
a58f6f25 | 973 | EXPORT_SYMBOL_GPL(cpu_clock); |
e436d800 | 974 | |
1da177e4 | 975 | #ifndef prepare_arch_switch |
4866cde0 NP |
976 | # define prepare_arch_switch(next) do { } while (0) |
977 | #endif | |
978 | #ifndef finish_arch_switch | |
979 | # define finish_arch_switch(prev) do { } while (0) | |
980 | #endif | |
981 | ||
051a1d1a DA |
982 | static inline int task_current(struct rq *rq, struct task_struct *p) |
983 | { | |
984 | return rq->curr == p; | |
985 | } | |
986 | ||
4866cde0 | 987 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW |
70b97a7f | 988 | static inline int task_running(struct rq *rq, struct task_struct *p) |
4866cde0 | 989 | { |
051a1d1a | 990 | return task_current(rq, p); |
4866cde0 NP |
991 | } |
992 | ||
70b97a7f | 993 | static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next) |
4866cde0 NP |
994 | { |
995 | } | |
996 | ||
70b97a7f | 997 | static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev) |
4866cde0 | 998 | { |
da04c035 IM |
999 | #ifdef CONFIG_DEBUG_SPINLOCK |
1000 | /* this is a valid case when another task releases the spinlock */ | |
1001 | rq->lock.owner = current; | |
1002 | #endif | |
8a25d5de IM |
1003 | /* |
1004 | * If we are tracking spinlock dependencies then we have to | |
1005 | * fix up the runqueue lock - which gets 'carried over' from | |
1006 | * prev into current: | |
1007 | */ | |
1008 | spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_); | |
1009 | ||
4866cde0 NP |
1010 | spin_unlock_irq(&rq->lock); |
1011 | } | |
1012 | ||
1013 | #else /* __ARCH_WANT_UNLOCKED_CTXSW */ | |
70b97a7f | 1014 | static inline int task_running(struct rq *rq, struct task_struct *p) |
4866cde0 NP |
1015 | { |
1016 | #ifdef CONFIG_SMP | |
1017 | return p->oncpu; | |
1018 | #else | |
051a1d1a | 1019 | return task_current(rq, p); |
4866cde0 NP |
1020 | #endif |
1021 | } | |
1022 | ||
70b97a7f | 1023 | static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next) |
4866cde0 NP |
1024 | { |
1025 | #ifdef CONFIG_SMP | |
1026 | /* | |
1027 | * We can optimise this out completely for !SMP, because the | |
1028 | * SMP rebalancing from interrupt is the only thing that cares | |
1029 | * here. | |
1030 | */ | |
1031 | next->oncpu = 1; | |
1032 | #endif | |
1033 | #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW | |
1034 | spin_unlock_irq(&rq->lock); | |
1035 | #else | |
1036 | spin_unlock(&rq->lock); | |
1037 | #endif | |
1038 | } | |
1039 | ||
70b97a7f | 1040 | static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev) |
4866cde0 NP |
1041 | { |
1042 | #ifdef CONFIG_SMP | |
1043 | /* | |
1044 | * After ->oncpu is cleared, the task can be moved to a different CPU. | |
1045 | * We must ensure this doesn't happen until the switch is completely | |
1046 | * finished. | |
1047 | */ | |
1048 | smp_wmb(); | |
1049 | prev->oncpu = 0; | |
1050 | #endif | |
1051 | #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW | |
1052 | local_irq_enable(); | |
1da177e4 | 1053 | #endif |
4866cde0 NP |
1054 | } |
1055 | #endif /* __ARCH_WANT_UNLOCKED_CTXSW */ | |
1da177e4 | 1056 | |
b29739f9 IM |
1057 | /* |
1058 | * __task_rq_lock - lock the runqueue a given task resides on. | |
1059 | * Must be called interrupts disabled. | |
1060 | */ | |
70b97a7f | 1061 | static inline struct rq *__task_rq_lock(struct task_struct *p) |
b29739f9 IM |
1062 | __acquires(rq->lock) |
1063 | { | |
3a5c359a AK |
1064 | for (;;) { |
1065 | struct rq *rq = task_rq(p); | |
1066 | spin_lock(&rq->lock); | |
1067 | if (likely(rq == task_rq(p))) | |
1068 | return rq; | |
b29739f9 | 1069 | spin_unlock(&rq->lock); |
b29739f9 | 1070 | } |
b29739f9 IM |
1071 | } |
1072 | ||
1da177e4 LT |
1073 | /* |
1074 | * task_rq_lock - lock the runqueue a given task resides on and disable | |
41a2d6cf | 1075 | * interrupts. Note the ordering: we can safely lookup the task_rq without |
1da177e4 LT |
1076 | * explicitly disabling preemption. |
1077 | */ | |
70b97a7f | 1078 | static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags) |
1da177e4 LT |
1079 | __acquires(rq->lock) |
1080 | { | |
70b97a7f | 1081 | struct rq *rq; |
1da177e4 | 1082 | |
3a5c359a AK |
1083 | for (;;) { |
1084 | local_irq_save(*flags); | |
1085 | rq = task_rq(p); | |
1086 | spin_lock(&rq->lock); | |
1087 | if (likely(rq == task_rq(p))) | |
1088 | return rq; | |
1da177e4 | 1089 | spin_unlock_irqrestore(&rq->lock, *flags); |
1da177e4 | 1090 | } |
1da177e4 LT |
1091 | } |
1092 | ||
a9957449 | 1093 | static void __task_rq_unlock(struct rq *rq) |
b29739f9 IM |
1094 | __releases(rq->lock) |
1095 | { | |
1096 | spin_unlock(&rq->lock); | |
1097 | } | |
1098 | ||
70b97a7f | 1099 | static inline void task_rq_unlock(struct rq *rq, unsigned long *flags) |
1da177e4 LT |
1100 | __releases(rq->lock) |
1101 | { | |
1102 | spin_unlock_irqrestore(&rq->lock, *flags); | |
1103 | } | |
1104 | ||
1da177e4 | 1105 | /* |
cc2a73b5 | 1106 | * this_rq_lock - lock this runqueue and disable interrupts. |
1da177e4 | 1107 | */ |
a9957449 | 1108 | static struct rq *this_rq_lock(void) |
1da177e4 LT |
1109 | __acquires(rq->lock) |
1110 | { | |
70b97a7f | 1111 | struct rq *rq; |
1da177e4 LT |
1112 | |
1113 | local_irq_disable(); | |
1114 | rq = this_rq(); | |
1115 | spin_lock(&rq->lock); | |
1116 | ||
1117 | return rq; | |
1118 | } | |
1119 | ||
1b9f19c2 | 1120 | /* |
2aa44d05 | 1121 | * We are going deep-idle (irqs are disabled): |
1b9f19c2 | 1122 | */ |
2aa44d05 | 1123 | void sched_clock_idle_sleep_event(void) |
1b9f19c2 | 1124 | { |
2aa44d05 IM |
1125 | struct rq *rq = cpu_rq(smp_processor_id()); |
1126 | ||
1127 | spin_lock(&rq->lock); | |
1128 | __update_rq_clock(rq); | |
1129 | spin_unlock(&rq->lock); | |
1130 | rq->clock_deep_idle_events++; | |
1131 | } | |
1132 | EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event); | |
1133 | ||
1134 | /* | |
1135 | * We just idled delta nanoseconds (called with irqs disabled): | |
1136 | */ | |
1137 | void sched_clock_idle_wakeup_event(u64 delta_ns) | |
1138 | { | |
1139 | struct rq *rq = cpu_rq(smp_processor_id()); | |
1140 | u64 now = sched_clock(); | |
1b9f19c2 | 1141 | |
2aa44d05 IM |
1142 | rq->idle_clock += delta_ns; |
1143 | /* | |
1144 | * Override the previous timestamp and ignore all | |
1145 | * sched_clock() deltas that occured while we idled, | |
1146 | * and use the PM-provided delta_ns to advance the | |
1147 | * rq clock: | |
1148 | */ | |
1149 | spin_lock(&rq->lock); | |
1150 | rq->prev_clock_raw = now; | |
1151 | rq->clock += delta_ns; | |
1152 | spin_unlock(&rq->lock); | |
782daeee | 1153 | touch_softlockup_watchdog(); |
1b9f19c2 | 1154 | } |
2aa44d05 | 1155 | EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event); |
1b9f19c2 | 1156 | |
8f4d37ec PZ |
1157 | static void __resched_task(struct task_struct *p, int tif_bit); |
1158 | ||
1159 | static inline void resched_task(struct task_struct *p) | |
1160 | { | |
1161 | __resched_task(p, TIF_NEED_RESCHED); | |
1162 | } | |
1163 | ||
1164 | #ifdef CONFIG_SCHED_HRTICK | |
1165 | /* | |
1166 | * Use HR-timers to deliver accurate preemption points. | |
1167 | * | |
1168 | * Its all a bit involved since we cannot program an hrt while holding the | |
1169 | * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a | |
1170 | * reschedule event. | |
1171 | * | |
1172 | * When we get rescheduled we reprogram the hrtick_timer outside of the | |
1173 | * rq->lock. | |
1174 | */ | |
1175 | static inline void resched_hrt(struct task_struct *p) | |
1176 | { | |
1177 | __resched_task(p, TIF_HRTICK_RESCHED); | |
1178 | } | |
1179 | ||
1180 | static inline void resched_rq(struct rq *rq) | |
1181 | { | |
1182 | unsigned long flags; | |
1183 | ||
1184 | spin_lock_irqsave(&rq->lock, flags); | |
1185 | resched_task(rq->curr); | |
1186 | spin_unlock_irqrestore(&rq->lock, flags); | |
1187 | } | |
1188 | ||
1189 | enum { | |
1190 | HRTICK_SET, /* re-programm hrtick_timer */ | |
1191 | HRTICK_RESET, /* not a new slice */ | |
1192 | }; | |
1193 | ||
1194 | /* | |
1195 | * Use hrtick when: | |
1196 | * - enabled by features | |
1197 | * - hrtimer is actually high res | |
1198 | */ | |
1199 | static inline int hrtick_enabled(struct rq *rq) | |
1200 | { | |
1201 | if (!sched_feat(HRTICK)) | |
1202 | return 0; | |
1203 | return hrtimer_is_hres_active(&rq->hrtick_timer); | |
1204 | } | |
1205 | ||
1206 | /* | |
1207 | * Called to set the hrtick timer state. | |
1208 | * | |
1209 | * called with rq->lock held and irqs disabled | |
1210 | */ | |
1211 | static void hrtick_start(struct rq *rq, u64 delay, int reset) | |
1212 | { | |
1213 | assert_spin_locked(&rq->lock); | |
1214 | ||
1215 | /* | |
1216 | * preempt at: now + delay | |
1217 | */ | |
1218 | rq->hrtick_expire = | |
1219 | ktime_add_ns(rq->hrtick_timer.base->get_time(), delay); | |
1220 | /* | |
1221 | * indicate we need to program the timer | |
1222 | */ | |
1223 | __set_bit(HRTICK_SET, &rq->hrtick_flags); | |
1224 | if (reset) | |
1225 | __set_bit(HRTICK_RESET, &rq->hrtick_flags); | |
1226 | ||
1227 | /* | |
1228 | * New slices are called from the schedule path and don't need a | |
1229 | * forced reschedule. | |
1230 | */ | |
1231 | if (reset) | |
1232 | resched_hrt(rq->curr); | |
1233 | } | |
1234 | ||
1235 | static void hrtick_clear(struct rq *rq) | |
1236 | { | |
1237 | if (hrtimer_active(&rq->hrtick_timer)) | |
1238 | hrtimer_cancel(&rq->hrtick_timer); | |
1239 | } | |
1240 | ||
1241 | /* | |
1242 | * Update the timer from the possible pending state. | |
1243 | */ | |
1244 | static void hrtick_set(struct rq *rq) | |
1245 | { | |
1246 | ktime_t time; | |
1247 | int set, reset; | |
1248 | unsigned long flags; | |
1249 | ||
1250 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); | |
1251 | ||
1252 | spin_lock_irqsave(&rq->lock, flags); | |
1253 | set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags); | |
1254 | reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags); | |
1255 | time = rq->hrtick_expire; | |
1256 | clear_thread_flag(TIF_HRTICK_RESCHED); | |
1257 | spin_unlock_irqrestore(&rq->lock, flags); | |
1258 | ||
1259 | if (set) { | |
1260 | hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS); | |
1261 | if (reset && !hrtimer_active(&rq->hrtick_timer)) | |
1262 | resched_rq(rq); | |
1263 | } else | |
1264 | hrtick_clear(rq); | |
1265 | } | |
1266 | ||
1267 | /* | |
1268 | * High-resolution timer tick. | |
1269 | * Runs from hardirq context with interrupts disabled. | |
1270 | */ | |
1271 | static enum hrtimer_restart hrtick(struct hrtimer *timer) | |
1272 | { | |
1273 | struct rq *rq = container_of(timer, struct rq, hrtick_timer); | |
1274 | ||
1275 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); | |
1276 | ||
1277 | spin_lock(&rq->lock); | |
1278 | __update_rq_clock(rq); | |
1279 | rq->curr->sched_class->task_tick(rq, rq->curr, 1); | |
1280 | spin_unlock(&rq->lock); | |
1281 | ||
1282 | return HRTIMER_NORESTART; | |
1283 | } | |
1284 | ||
1285 | static inline void init_rq_hrtick(struct rq *rq) | |
1286 | { | |
1287 | rq->hrtick_flags = 0; | |
1288 | hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
1289 | rq->hrtick_timer.function = hrtick; | |
1290 | rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ; | |
1291 | } | |
1292 | ||
1293 | void hrtick_resched(void) | |
1294 | { | |
1295 | struct rq *rq; | |
1296 | unsigned long flags; | |
1297 | ||
1298 | if (!test_thread_flag(TIF_HRTICK_RESCHED)) | |
1299 | return; | |
1300 | ||
1301 | local_irq_save(flags); | |
1302 | rq = cpu_rq(smp_processor_id()); | |
1303 | hrtick_set(rq); | |
1304 | local_irq_restore(flags); | |
1305 | } | |
1306 | #else | |
1307 | static inline void hrtick_clear(struct rq *rq) | |
1308 | { | |
1309 | } | |
1310 | ||
1311 | static inline void hrtick_set(struct rq *rq) | |
1312 | { | |
1313 | } | |
1314 | ||
1315 | static inline void init_rq_hrtick(struct rq *rq) | |
1316 | { | |
1317 | } | |
1318 | ||
1319 | void hrtick_resched(void) | |
1320 | { | |
1321 | } | |
1322 | #endif | |
1323 | ||
c24d20db IM |
1324 | /* |
1325 | * resched_task - mark a task 'to be rescheduled now'. | |
1326 | * | |
1327 | * On UP this means the setting of the need_resched flag, on SMP it | |
1328 | * might also involve a cross-CPU call to trigger the scheduler on | |
1329 | * the target CPU. | |
1330 | */ | |
1331 | #ifdef CONFIG_SMP | |
1332 | ||
1333 | #ifndef tsk_is_polling | |
1334 | #define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG) | |
1335 | #endif | |
1336 | ||
8f4d37ec | 1337 | static void __resched_task(struct task_struct *p, int tif_bit) |
c24d20db IM |
1338 | { |
1339 | int cpu; | |
1340 | ||
1341 | assert_spin_locked(&task_rq(p)->lock); | |
1342 | ||
8f4d37ec | 1343 | if (unlikely(test_tsk_thread_flag(p, tif_bit))) |
c24d20db IM |
1344 | return; |
1345 | ||
8f4d37ec | 1346 | set_tsk_thread_flag(p, tif_bit); |
c24d20db IM |
1347 | |
1348 | cpu = task_cpu(p); | |
1349 | if (cpu == smp_processor_id()) | |
1350 | return; | |
1351 | ||
1352 | /* NEED_RESCHED must be visible before we test polling */ | |
1353 | smp_mb(); | |
1354 | if (!tsk_is_polling(p)) | |
1355 | smp_send_reschedule(cpu); | |
1356 | } | |
1357 | ||
1358 | static void resched_cpu(int cpu) | |
1359 | { | |
1360 | struct rq *rq = cpu_rq(cpu); | |
1361 | unsigned long flags; | |
1362 | ||
1363 | if (!spin_trylock_irqsave(&rq->lock, flags)) | |
1364 | return; | |
1365 | resched_task(cpu_curr(cpu)); | |
1366 | spin_unlock_irqrestore(&rq->lock, flags); | |
1367 | } | |
06d8308c TG |
1368 | |
1369 | #ifdef CONFIG_NO_HZ | |
1370 | /* | |
1371 | * When add_timer_on() enqueues a timer into the timer wheel of an | |
1372 | * idle CPU then this timer might expire before the next timer event | |
1373 | * which is scheduled to wake up that CPU. In case of a completely | |
1374 | * idle system the next event might even be infinite time into the | |
1375 | * future. wake_up_idle_cpu() ensures that the CPU is woken up and | |
1376 | * leaves the inner idle loop so the newly added timer is taken into | |
1377 | * account when the CPU goes back to idle and evaluates the timer | |
1378 | * wheel for the next timer event. | |
1379 | */ | |
1380 | void wake_up_idle_cpu(int cpu) | |
1381 | { | |
1382 | struct rq *rq = cpu_rq(cpu); | |
1383 | ||
1384 | if (cpu == smp_processor_id()) | |
1385 | return; | |
1386 | ||
1387 | /* | |
1388 | * This is safe, as this function is called with the timer | |
1389 | * wheel base lock of (cpu) held. When the CPU is on the way | |
1390 | * to idle and has not yet set rq->curr to idle then it will | |
1391 | * be serialized on the timer wheel base lock and take the new | |
1392 | * timer into account automatically. | |
1393 | */ | |
1394 | if (rq->curr != rq->idle) | |
1395 | return; | |
1396 | ||
1397 | /* | |
1398 | * We can set TIF_RESCHED on the idle task of the other CPU | |
1399 | * lockless. The worst case is that the other CPU runs the | |
1400 | * idle task through an additional NOOP schedule() | |
1401 | */ | |
1402 | set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED); | |
1403 | ||
1404 | /* NEED_RESCHED must be visible before we test polling */ | |
1405 | smp_mb(); | |
1406 | if (!tsk_is_polling(rq->idle)) | |
1407 | smp_send_reschedule(cpu); | |
1408 | } | |
1409 | #endif | |
1410 | ||
c24d20db | 1411 | #else |
8f4d37ec | 1412 | static void __resched_task(struct task_struct *p, int tif_bit) |
c24d20db IM |
1413 | { |
1414 | assert_spin_locked(&task_rq(p)->lock); | |
8f4d37ec | 1415 | set_tsk_thread_flag(p, tif_bit); |
c24d20db IM |
1416 | } |
1417 | #endif | |
1418 | ||
45bf76df IM |
1419 | #if BITS_PER_LONG == 32 |
1420 | # define WMULT_CONST (~0UL) | |
1421 | #else | |
1422 | # define WMULT_CONST (1UL << 32) | |
1423 | #endif | |
1424 | ||
1425 | #define WMULT_SHIFT 32 | |
1426 | ||
194081eb IM |
1427 | /* |
1428 | * Shift right and round: | |
1429 | */ | |
cf2ab469 | 1430 | #define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y)) |
194081eb | 1431 | |
8f1bc385 PZ |
1432 | /* |
1433 | * delta *= weight / lw | |
1434 | */ | |
cb1c4fc9 | 1435 | static unsigned long |
45bf76df IM |
1436 | calc_delta_mine(unsigned long delta_exec, unsigned long weight, |
1437 | struct load_weight *lw) | |
1438 | { | |
1439 | u64 tmp; | |
1440 | ||
1441 | if (unlikely(!lw->inv_weight)) | |
27d11726 | 1442 | lw->inv_weight = (WMULT_CONST-lw->weight/2) / (lw->weight+1); |
45bf76df IM |
1443 | |
1444 | tmp = (u64)delta_exec * weight; | |
1445 | /* | |
1446 | * Check whether we'd overflow the 64-bit multiplication: | |
1447 | */ | |
194081eb | 1448 | if (unlikely(tmp > WMULT_CONST)) |
cf2ab469 | 1449 | tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight, |
194081eb IM |
1450 | WMULT_SHIFT/2); |
1451 | else | |
cf2ab469 | 1452 | tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT); |
45bf76df | 1453 | |
ecf691da | 1454 | return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX); |
45bf76df IM |
1455 | } |
1456 | ||
1091985b | 1457 | static inline void update_load_add(struct load_weight *lw, unsigned long inc) |
45bf76df IM |
1458 | { |
1459 | lw->weight += inc; | |
e89996ae | 1460 | lw->inv_weight = 0; |
45bf76df IM |
1461 | } |
1462 | ||
1091985b | 1463 | static inline void update_load_sub(struct load_weight *lw, unsigned long dec) |
45bf76df IM |
1464 | { |
1465 | lw->weight -= dec; | |
e89996ae | 1466 | lw->inv_weight = 0; |
45bf76df IM |
1467 | } |
1468 | ||
2dd73a4f PW |
1469 | /* |
1470 | * To aid in avoiding the subversion of "niceness" due to uneven distribution | |
1471 | * of tasks with abnormal "nice" values across CPUs the contribution that | |
1472 | * each task makes to its run queue's load is weighted according to its | |
41a2d6cf | 1473 | * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a |
2dd73a4f PW |
1474 | * scaled version of the new time slice allocation that they receive on time |
1475 | * slice expiry etc. | |
1476 | */ | |
1477 | ||
dd41f596 IM |
1478 | #define WEIGHT_IDLEPRIO 2 |
1479 | #define WMULT_IDLEPRIO (1 << 31) | |
1480 | ||
1481 | /* | |
1482 | * Nice levels are multiplicative, with a gentle 10% change for every | |
1483 | * nice level changed. I.e. when a CPU-bound task goes from nice 0 to | |
1484 | * nice 1, it will get ~10% less CPU time than another CPU-bound task | |
1485 | * that remained on nice 0. | |
1486 | * | |
1487 | * The "10% effect" is relative and cumulative: from _any_ nice level, | |
1488 | * if you go up 1 level, it's -10% CPU usage, if you go down 1 level | |
f9153ee6 IM |
1489 | * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25. |
1490 | * If a task goes up by ~10% and another task goes down by ~10% then | |
1491 | * the relative distance between them is ~25%.) | |
dd41f596 IM |
1492 | */ |
1493 | static const int prio_to_weight[40] = { | |
254753dc IM |
1494 | /* -20 */ 88761, 71755, 56483, 46273, 36291, |
1495 | /* -15 */ 29154, 23254, 18705, 14949, 11916, | |
1496 | /* -10 */ 9548, 7620, 6100, 4904, 3906, | |
1497 | /* -5 */ 3121, 2501, 1991, 1586, 1277, | |
1498 | /* 0 */ 1024, 820, 655, 526, 423, | |
1499 | /* 5 */ 335, 272, 215, 172, 137, | |
1500 | /* 10 */ 110, 87, 70, 56, 45, | |
1501 | /* 15 */ 36, 29, 23, 18, 15, | |
dd41f596 IM |
1502 | }; |
1503 | ||
5714d2de IM |
1504 | /* |
1505 | * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated. | |
1506 | * | |
1507 | * In cases where the weight does not change often, we can use the | |
1508 | * precalculated inverse to speed up arithmetics by turning divisions | |
1509 | * into multiplications: | |
1510 | */ | |
dd41f596 | 1511 | static const u32 prio_to_wmult[40] = { |
254753dc IM |
1512 | /* -20 */ 48388, 59856, 76040, 92818, 118348, |
1513 | /* -15 */ 147320, 184698, 229616, 287308, 360437, | |
1514 | /* -10 */ 449829, 563644, 704093, 875809, 1099582, | |
1515 | /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326, | |
1516 | /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587, | |
1517 | /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126, | |
1518 | /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717, | |
1519 | /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153, | |
dd41f596 | 1520 | }; |
2dd73a4f | 1521 | |
dd41f596 IM |
1522 | static void activate_task(struct rq *rq, struct task_struct *p, int wakeup); |
1523 | ||
1524 | /* | |
1525 | * runqueue iterator, to support SMP load-balancing between different | |
1526 | * scheduling classes, without having to expose their internal data | |
1527 | * structures to the load-balancing proper: | |
1528 | */ | |
1529 | struct rq_iterator { | |
1530 | void *arg; | |
1531 | struct task_struct *(*start)(void *); | |
1532 | struct task_struct *(*next)(void *); | |
1533 | }; | |
1534 | ||
e1d1484f PW |
1535 | #ifdef CONFIG_SMP |
1536 | static unsigned long | |
1537 | balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, | |
1538 | unsigned long max_load_move, struct sched_domain *sd, | |
1539 | enum cpu_idle_type idle, int *all_pinned, | |
1540 | int *this_best_prio, struct rq_iterator *iterator); | |
1541 | ||
1542 | static int | |
1543 | iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest, | |
1544 | struct sched_domain *sd, enum cpu_idle_type idle, | |
1545 | struct rq_iterator *iterator); | |
e1d1484f | 1546 | #endif |
dd41f596 | 1547 | |
d842de87 SV |
1548 | #ifdef CONFIG_CGROUP_CPUACCT |
1549 | static void cpuacct_charge(struct task_struct *tsk, u64 cputime); | |
1550 | #else | |
1551 | static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {} | |
1552 | #endif | |
1553 | ||
18d95a28 PZ |
1554 | static inline void inc_cpu_load(struct rq *rq, unsigned long load) |
1555 | { | |
1556 | update_load_add(&rq->load, load); | |
1557 | } | |
1558 | ||
1559 | static inline void dec_cpu_load(struct rq *rq, unsigned long load) | |
1560 | { | |
1561 | update_load_sub(&rq->load, load); | |
1562 | } | |
1563 | ||
e7693a36 GH |
1564 | #ifdef CONFIG_SMP |
1565 | static unsigned long source_load(int cpu, int type); | |
1566 | static unsigned long target_load(int cpu, int type); | |
1567 | static unsigned long cpu_avg_load_per_task(int cpu); | |
1568 | static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd); | |
18d95a28 PZ |
1569 | |
1570 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
1571 | ||
1572 | /* | |
1573 | * Group load balancing. | |
1574 | * | |
1575 | * We calculate a few balance domain wide aggregate numbers; load and weight. | |
1576 | * Given the pictures below, and assuming each item has equal weight: | |
1577 | * | |
1578 | * root 1 - thread | |
1579 | * / | \ A - group | |
1580 | * A 1 B | |
1581 | * /|\ / \ | |
1582 | * C 2 D 3 4 | |
1583 | * | | | |
1584 | * 5 6 | |
1585 | * | |
1586 | * load: | |
1587 | * A and B get 1/3-rd of the total load. C and D get 1/3-rd of A's 1/3-rd, | |
1588 | * which equals 1/9-th of the total load. | |
1589 | * | |
1590 | * shares: | |
1591 | * The weight of this group on the selected cpus. | |
1592 | * | |
1593 | * rq_weight: | |
1594 | * Direct sum of all the cpu's their rq weight, e.g. A would get 3 while | |
1595 | * B would get 2. | |
1596 | * | |
1597 | * task_weight: | |
1598 | * Part of the rq_weight contributed by tasks; all groups except B would | |
1599 | * get 1, B gets 2. | |
1600 | */ | |
1601 | ||
1602 | static inline struct aggregate_struct * | |
1603 | aggregate(struct task_group *tg, struct sched_domain *sd) | |
1604 | { | |
1605 | return &tg->cfs_rq[sd->first_cpu]->aggregate; | |
1606 | } | |
1607 | ||
1608 | typedef void (*aggregate_func)(struct task_group *, struct sched_domain *); | |
1609 | ||
1610 | /* | |
1611 | * Iterate the full tree, calling @down when first entering a node and @up when | |
1612 | * leaving it for the final time. | |
1613 | */ | |
1614 | static | |
1615 | void aggregate_walk_tree(aggregate_func down, aggregate_func up, | |
1616 | struct sched_domain *sd) | |
1617 | { | |
1618 | struct task_group *parent, *child; | |
1619 | ||
1620 | rcu_read_lock(); | |
1621 | parent = &root_task_group; | |
1622 | down: | |
1623 | (*down)(parent, sd); | |
1624 | list_for_each_entry_rcu(child, &parent->children, siblings) { | |
1625 | parent = child; | |
1626 | goto down; | |
1627 | ||
1628 | up: | |
1629 | continue; | |
1630 | } | |
1631 | (*up)(parent, sd); | |
1632 | ||
1633 | child = parent; | |
1634 | parent = parent->parent; | |
1635 | if (parent) | |
1636 | goto up; | |
1637 | rcu_read_unlock(); | |
1638 | } | |
1639 | ||
1640 | /* | |
1641 | * Calculate the aggregate runqueue weight. | |
1642 | */ | |
1643 | static | |
1644 | void aggregate_group_weight(struct task_group *tg, struct sched_domain *sd) | |
1645 | { | |
1646 | unsigned long rq_weight = 0; | |
1647 | unsigned long task_weight = 0; | |
1648 | int i; | |
1649 | ||
1650 | for_each_cpu_mask(i, sd->span) { | |
1651 | rq_weight += tg->cfs_rq[i]->load.weight; | |
1652 | task_weight += tg->cfs_rq[i]->task_weight; | |
1653 | } | |
1654 | ||
1655 | aggregate(tg, sd)->rq_weight = rq_weight; | |
1656 | aggregate(tg, sd)->task_weight = task_weight; | |
1657 | } | |
1658 | ||
1659 | /* | |
1660 | * Redistribute tg->shares amongst all tg->cfs_rq[]s. | |
1661 | */ | |
1662 | static void __aggregate_redistribute_shares(struct task_group *tg) | |
1663 | { | |
1664 | int i, max_cpu = smp_processor_id(); | |
1665 | unsigned long rq_weight = 0; | |
1666 | unsigned long shares, max_shares = 0, shares_rem = tg->shares; | |
1667 | ||
1668 | for_each_possible_cpu(i) | |
1669 | rq_weight += tg->cfs_rq[i]->load.weight; | |
1670 | ||
1671 | for_each_possible_cpu(i) { | |
1672 | /* | |
1673 | * divide shares proportional to the rq_weights. | |
1674 | */ | |
1675 | shares = tg->shares * tg->cfs_rq[i]->load.weight; | |
1676 | shares /= rq_weight + 1; | |
1677 | ||
1678 | tg->cfs_rq[i]->shares = shares; | |
1679 | ||
1680 | if (shares > max_shares) { | |
1681 | max_shares = shares; | |
1682 | max_cpu = i; | |
1683 | } | |
1684 | shares_rem -= shares; | |
1685 | } | |
1686 | ||
1687 | /* | |
1688 | * Ensure it all adds up to tg->shares; we can loose a few | |
1689 | * due to rounding down when computing the per-cpu shares. | |
1690 | */ | |
1691 | if (shares_rem) | |
1692 | tg->cfs_rq[max_cpu]->shares += shares_rem; | |
1693 | } | |
1694 | ||
1695 | /* | |
1696 | * Compute the weight of this group on the given cpus. | |
1697 | */ | |
1698 | static | |
1699 | void aggregate_group_shares(struct task_group *tg, struct sched_domain *sd) | |
1700 | { | |
1701 | unsigned long shares = 0; | |
1702 | int i; | |
1703 | ||
1704 | again: | |
1705 | for_each_cpu_mask(i, sd->span) | |
1706 | shares += tg->cfs_rq[i]->shares; | |
1707 | ||
1708 | /* | |
1709 | * When the span doesn't have any shares assigned, but does have | |
1710 | * tasks to run do a machine wide rebalance (should be rare). | |
1711 | */ | |
1712 | if (unlikely(!shares && aggregate(tg, sd)->rq_weight)) { | |
1713 | __aggregate_redistribute_shares(tg); | |
1714 | goto again; | |
1715 | } | |
1716 | ||
1717 | aggregate(tg, sd)->shares = shares; | |
1718 | } | |
1719 | ||
1720 | /* | |
1721 | * Compute the load fraction assigned to this group, relies on the aggregate | |
1722 | * weight and this group's parent's load, i.e. top-down. | |
1723 | */ | |
1724 | static | |
1725 | void aggregate_group_load(struct task_group *tg, struct sched_domain *sd) | |
1726 | { | |
1727 | unsigned long load; | |
1728 | ||
1729 | if (!tg->parent) { | |
1730 | int i; | |
1731 | ||
1732 | load = 0; | |
1733 | for_each_cpu_mask(i, sd->span) | |
1734 | load += cpu_rq(i)->load.weight; | |
1735 | ||
1736 | } else { | |
1737 | load = aggregate(tg->parent, sd)->load; | |
1738 | ||
1739 | /* | |
1740 | * shares is our weight in the parent's rq so | |
1741 | * shares/parent->rq_weight gives our fraction of the load | |
1742 | */ | |
1743 | load *= aggregate(tg, sd)->shares; | |
1744 | load /= aggregate(tg->parent, sd)->rq_weight + 1; | |
1745 | } | |
1746 | ||
1747 | aggregate(tg, sd)->load = load; | |
1748 | } | |
1749 | ||
1750 | static void __set_se_shares(struct sched_entity *se, unsigned long shares); | |
1751 | ||
1752 | /* | |
1753 | * Calculate and set the cpu's group shares. | |
1754 | */ | |
1755 | static void | |
1756 | __update_group_shares_cpu(struct task_group *tg, struct sched_domain *sd, | |
1757 | int tcpu) | |
1758 | { | |
1759 | int boost = 0; | |
1760 | unsigned long shares; | |
1761 | unsigned long rq_weight; | |
1762 | ||
1763 | if (!tg->se[tcpu]) | |
1764 | return; | |
1765 | ||
1766 | rq_weight = tg->cfs_rq[tcpu]->load.weight; | |
1767 | ||
1768 | /* | |
1769 | * If there are currently no tasks on the cpu pretend there is one of | |
1770 | * average load so that when a new task gets to run here it will not | |
1771 | * get delayed by group starvation. | |
1772 | */ | |
1773 | if (!rq_weight) { | |
1774 | boost = 1; | |
1775 | rq_weight = NICE_0_LOAD; | |
1776 | } | |
1777 | ||
1778 | /* | |
1779 | * \Sum shares * rq_weight | |
1780 | * shares = ----------------------- | |
1781 | * \Sum rq_weight | |
1782 | * | |
1783 | */ | |
1784 | shares = aggregate(tg, sd)->shares * rq_weight; | |
1785 | shares /= aggregate(tg, sd)->rq_weight + 1; | |
1786 | ||
1787 | /* | |
1788 | * record the actual number of shares, not the boosted amount. | |
1789 | */ | |
1790 | tg->cfs_rq[tcpu]->shares = boost ? 0 : shares; | |
1791 | ||
1792 | if (shares < MIN_SHARES) | |
1793 | shares = MIN_SHARES; | |
1794 | ||
1795 | __set_se_shares(tg->se[tcpu], shares); | |
1796 | } | |
1797 | ||
1798 | /* | |
1799 | * Re-adjust the weights on the cpu the task came from and on the cpu the | |
1800 | * task went to. | |
1801 | */ | |
1802 | static void | |
1803 | __move_group_shares(struct task_group *tg, struct sched_domain *sd, | |
1804 | int scpu, int dcpu) | |
1805 | { | |
1806 | unsigned long shares; | |
1807 | ||
1808 | shares = tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares; | |
1809 | ||
1810 | __update_group_shares_cpu(tg, sd, scpu); | |
1811 | __update_group_shares_cpu(tg, sd, dcpu); | |
1812 | ||
1813 | /* | |
1814 | * ensure we never loose shares due to rounding errors in the | |
1815 | * above redistribution. | |
1816 | */ | |
1817 | shares -= tg->cfs_rq[scpu]->shares + tg->cfs_rq[dcpu]->shares; | |
1818 | if (shares) | |
1819 | tg->cfs_rq[dcpu]->shares += shares; | |
1820 | } | |
1821 | ||
1822 | /* | |
1823 | * Because changing a group's shares changes the weight of the super-group | |
1824 | * we need to walk up the tree and change all shares until we hit the root. | |
1825 | */ | |
1826 | static void | |
1827 | move_group_shares(struct task_group *tg, struct sched_domain *sd, | |
1828 | int scpu, int dcpu) | |
1829 | { | |
1830 | while (tg) { | |
1831 | __move_group_shares(tg, sd, scpu, dcpu); | |
1832 | tg = tg->parent; | |
1833 | } | |
1834 | } | |
1835 | ||
1836 | static | |
1837 | void aggregate_group_set_shares(struct task_group *tg, struct sched_domain *sd) | |
1838 | { | |
1839 | unsigned long shares = aggregate(tg, sd)->shares; | |
1840 | int i; | |
1841 | ||
1842 | for_each_cpu_mask(i, sd->span) { | |
1843 | struct rq *rq = cpu_rq(i); | |
1844 | unsigned long flags; | |
1845 | ||
1846 | spin_lock_irqsave(&rq->lock, flags); | |
1847 | __update_group_shares_cpu(tg, sd, i); | |
1848 | spin_unlock_irqrestore(&rq->lock, flags); | |
1849 | } | |
1850 | ||
1851 | aggregate_group_shares(tg, sd); | |
1852 | ||
1853 | /* | |
1854 | * ensure we never loose shares due to rounding errors in the | |
1855 | * above redistribution. | |
1856 | */ | |
1857 | shares -= aggregate(tg, sd)->shares; | |
1858 | if (shares) { | |
1859 | tg->cfs_rq[sd->first_cpu]->shares += shares; | |
1860 | aggregate(tg, sd)->shares += shares; | |
1861 | } | |
1862 | } | |
1863 | ||
1864 | /* | |
1865 | * Calculate the accumulative weight and recursive load of each task group | |
1866 | * while walking down the tree. | |
1867 | */ | |
1868 | static | |
1869 | void aggregate_get_down(struct task_group *tg, struct sched_domain *sd) | |
1870 | { | |
1871 | aggregate_group_weight(tg, sd); | |
1872 | aggregate_group_shares(tg, sd); | |
1873 | aggregate_group_load(tg, sd); | |
1874 | } | |
1875 | ||
1876 | /* | |
1877 | * Rebalance the cpu shares while walking back up the tree. | |
1878 | */ | |
1879 | static | |
1880 | void aggregate_get_up(struct task_group *tg, struct sched_domain *sd) | |
1881 | { | |
1882 | aggregate_group_set_shares(tg, sd); | |
1883 | } | |
1884 | ||
1885 | static DEFINE_PER_CPU(spinlock_t, aggregate_lock); | |
1886 | ||
1887 | static void __init init_aggregate(void) | |
1888 | { | |
1889 | int i; | |
1890 | ||
1891 | for_each_possible_cpu(i) | |
1892 | spin_lock_init(&per_cpu(aggregate_lock, i)); | |
1893 | } | |
1894 | ||
1895 | static int get_aggregate(struct sched_domain *sd) | |
1896 | { | |
1897 | if (!spin_trylock(&per_cpu(aggregate_lock, sd->first_cpu))) | |
1898 | return 0; | |
1899 | ||
1900 | aggregate_walk_tree(aggregate_get_down, aggregate_get_up, sd); | |
1901 | return 1; | |
1902 | } | |
1903 | ||
1904 | static void put_aggregate(struct sched_domain *sd) | |
1905 | { | |
1906 | spin_unlock(&per_cpu(aggregate_lock, sd->first_cpu)); | |
1907 | } | |
1908 | ||
1909 | static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares) | |
1910 | { | |
1911 | cfs_rq->shares = shares; | |
1912 | } | |
1913 | ||
1914 | #else | |
1915 | ||
1916 | static inline void init_aggregate(void) | |
1917 | { | |
1918 | } | |
1919 | ||
1920 | static inline int get_aggregate(struct sched_domain *sd) | |
1921 | { | |
1922 | return 0; | |
1923 | } | |
1924 | ||
1925 | static inline void put_aggregate(struct sched_domain *sd) | |
1926 | { | |
1927 | } | |
1928 | #endif | |
1929 | ||
1930 | #else /* CONFIG_SMP */ | |
1931 | ||
1932 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
1933 | static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares) | |
1934 | { | |
1935 | } | |
1936 | #endif | |
1937 | ||
e7693a36 GH |
1938 | #endif /* CONFIG_SMP */ |
1939 | ||
dd41f596 | 1940 | #include "sched_stats.h" |
dd41f596 | 1941 | #include "sched_idletask.c" |
5522d5d5 IM |
1942 | #include "sched_fair.c" |
1943 | #include "sched_rt.c" | |
dd41f596 IM |
1944 | #ifdef CONFIG_SCHED_DEBUG |
1945 | # include "sched_debug.c" | |
1946 | #endif | |
1947 | ||
1948 | #define sched_class_highest (&rt_sched_class) | |
1949 | ||
18d95a28 | 1950 | static void inc_nr_running(struct rq *rq) |
9c217245 IM |
1951 | { |
1952 | rq->nr_running++; | |
9c217245 IM |
1953 | } |
1954 | ||
18d95a28 | 1955 | static void dec_nr_running(struct rq *rq) |
9c217245 IM |
1956 | { |
1957 | rq->nr_running--; | |
9c217245 IM |
1958 | } |
1959 | ||
45bf76df IM |
1960 | static void set_load_weight(struct task_struct *p) |
1961 | { | |
1962 | if (task_has_rt_policy(p)) { | |
dd41f596 IM |
1963 | p->se.load.weight = prio_to_weight[0] * 2; |
1964 | p->se.load.inv_weight = prio_to_wmult[0] >> 1; | |
1965 | return; | |
1966 | } | |
45bf76df | 1967 | |
dd41f596 IM |
1968 | /* |
1969 | * SCHED_IDLE tasks get minimal weight: | |
1970 | */ | |
1971 | if (p->policy == SCHED_IDLE) { | |
1972 | p->se.load.weight = WEIGHT_IDLEPRIO; | |
1973 | p->se.load.inv_weight = WMULT_IDLEPRIO; | |
1974 | return; | |
1975 | } | |
71f8bd46 | 1976 | |
dd41f596 IM |
1977 | p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO]; |
1978 | p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO]; | |
71f8bd46 IM |
1979 | } |
1980 | ||
8159f87e | 1981 | static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup) |
71f8bd46 | 1982 | { |
dd41f596 | 1983 | sched_info_queued(p); |
fd390f6a | 1984 | p->sched_class->enqueue_task(rq, p, wakeup); |
dd41f596 | 1985 | p->se.on_rq = 1; |
71f8bd46 IM |
1986 | } |
1987 | ||
69be72c1 | 1988 | static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep) |
71f8bd46 | 1989 | { |
f02231e5 | 1990 | p->sched_class->dequeue_task(rq, p, sleep); |
dd41f596 | 1991 | p->se.on_rq = 0; |
71f8bd46 IM |
1992 | } |
1993 | ||
14531189 | 1994 | /* |
dd41f596 | 1995 | * __normal_prio - return the priority that is based on the static prio |
14531189 | 1996 | */ |
14531189 IM |
1997 | static inline int __normal_prio(struct task_struct *p) |
1998 | { | |
dd41f596 | 1999 | return p->static_prio; |
14531189 IM |
2000 | } |
2001 | ||
b29739f9 IM |
2002 | /* |
2003 | * Calculate the expected normal priority: i.e. priority | |
2004 | * without taking RT-inheritance into account. Might be | |
2005 | * boosted by interactivity modifiers. Changes upon fork, | |
2006 | * setprio syscalls, and whenever the interactivity | |
2007 | * estimator recalculates. | |
2008 | */ | |
36c8b586 | 2009 | static inline int normal_prio(struct task_struct *p) |
b29739f9 IM |
2010 | { |
2011 | int prio; | |
2012 | ||
e05606d3 | 2013 | if (task_has_rt_policy(p)) |
b29739f9 IM |
2014 | prio = MAX_RT_PRIO-1 - p->rt_priority; |
2015 | else | |
2016 | prio = __normal_prio(p); | |
2017 | return prio; | |
2018 | } | |
2019 | ||
2020 | /* | |
2021 | * Calculate the current priority, i.e. the priority | |
2022 | * taken into account by the scheduler. This value might | |
2023 | * be boosted by RT tasks, or might be boosted by | |
2024 | * interactivity modifiers. Will be RT if the task got | |
2025 | * RT-boosted. If not then it returns p->normal_prio. | |
2026 | */ | |
36c8b586 | 2027 | static int effective_prio(struct task_struct *p) |
b29739f9 IM |
2028 | { |
2029 | p->normal_prio = normal_prio(p); | |
2030 | /* | |
2031 | * If we are RT tasks or we were boosted to RT priority, | |
2032 | * keep the priority unchanged. Otherwise, update priority | |
2033 | * to the normal priority: | |
2034 | */ | |
2035 | if (!rt_prio(p->prio)) | |
2036 | return p->normal_prio; | |
2037 | return p->prio; | |
2038 | } | |
2039 | ||
1da177e4 | 2040 | /* |
dd41f596 | 2041 | * activate_task - move a task to the runqueue. |
1da177e4 | 2042 | */ |
dd41f596 | 2043 | static void activate_task(struct rq *rq, struct task_struct *p, int wakeup) |
1da177e4 | 2044 | { |
d9514f6c | 2045 | if (task_contributes_to_load(p)) |
dd41f596 | 2046 | rq->nr_uninterruptible--; |
1da177e4 | 2047 | |
8159f87e | 2048 | enqueue_task(rq, p, wakeup); |
18d95a28 | 2049 | inc_nr_running(rq); |
1da177e4 LT |
2050 | } |
2051 | ||
1da177e4 LT |
2052 | /* |
2053 | * deactivate_task - remove a task from the runqueue. | |
2054 | */ | |
2e1cb74a | 2055 | static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep) |
1da177e4 | 2056 | { |
d9514f6c | 2057 | if (task_contributes_to_load(p)) |
dd41f596 IM |
2058 | rq->nr_uninterruptible++; |
2059 | ||
69be72c1 | 2060 | dequeue_task(rq, p, sleep); |
18d95a28 | 2061 | dec_nr_running(rq); |
1da177e4 LT |
2062 | } |
2063 | ||
1da177e4 LT |
2064 | /** |
2065 | * task_curr - is this task currently executing on a CPU? | |
2066 | * @p: the task in question. | |
2067 | */ | |
36c8b586 | 2068 | inline int task_curr(const struct task_struct *p) |
1da177e4 LT |
2069 | { |
2070 | return cpu_curr(task_cpu(p)) == p; | |
2071 | } | |
2072 | ||
2dd73a4f PW |
2073 | /* Used instead of source_load when we know the type == 0 */ |
2074 | unsigned long weighted_cpuload(const int cpu) | |
2075 | { | |
495eca49 | 2076 | return cpu_rq(cpu)->load.weight; |
dd41f596 IM |
2077 | } |
2078 | ||
2079 | static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu) | |
2080 | { | |
6f505b16 | 2081 | set_task_rq(p, cpu); |
dd41f596 | 2082 | #ifdef CONFIG_SMP |
ce96b5ac DA |
2083 | /* |
2084 | * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be | |
2085 | * successfuly executed on another CPU. We must ensure that updates of | |
2086 | * per-task data have been completed by this moment. | |
2087 | */ | |
2088 | smp_wmb(); | |
dd41f596 | 2089 | task_thread_info(p)->cpu = cpu; |
dd41f596 | 2090 | #endif |
2dd73a4f PW |
2091 | } |
2092 | ||
cb469845 SR |
2093 | static inline void check_class_changed(struct rq *rq, struct task_struct *p, |
2094 | const struct sched_class *prev_class, | |
2095 | int oldprio, int running) | |
2096 | { | |
2097 | if (prev_class != p->sched_class) { | |
2098 | if (prev_class->switched_from) | |
2099 | prev_class->switched_from(rq, p, running); | |
2100 | p->sched_class->switched_to(rq, p, running); | |
2101 | } else | |
2102 | p->sched_class->prio_changed(rq, p, oldprio, running); | |
2103 | } | |
2104 | ||
1da177e4 | 2105 | #ifdef CONFIG_SMP |
c65cc870 | 2106 | |
cc367732 IM |
2107 | /* |
2108 | * Is this task likely cache-hot: | |
2109 | */ | |
e7693a36 | 2110 | static int |
cc367732 IM |
2111 | task_hot(struct task_struct *p, u64 now, struct sched_domain *sd) |
2112 | { | |
2113 | s64 delta; | |
2114 | ||
f540a608 IM |
2115 | /* |
2116 | * Buddy candidates are cache hot: | |
2117 | */ | |
d25ce4cd | 2118 | if (sched_feat(CACHE_HOT_BUDDY) && (&p->se == cfs_rq_of(&p->se)->next)) |
f540a608 IM |
2119 | return 1; |
2120 | ||
cc367732 IM |
2121 | if (p->sched_class != &fair_sched_class) |
2122 | return 0; | |
2123 | ||
6bc1665b IM |
2124 | if (sysctl_sched_migration_cost == -1) |
2125 | return 1; | |
2126 | if (sysctl_sched_migration_cost == 0) | |
2127 | return 0; | |
2128 | ||
cc367732 IM |
2129 | delta = now - p->se.exec_start; |
2130 | ||
2131 | return delta < (s64)sysctl_sched_migration_cost; | |
2132 | } | |
2133 | ||
2134 | ||
dd41f596 | 2135 | void set_task_cpu(struct task_struct *p, unsigned int new_cpu) |
c65cc870 | 2136 | { |
dd41f596 IM |
2137 | int old_cpu = task_cpu(p); |
2138 | struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu); | |
2830cf8c SV |
2139 | struct cfs_rq *old_cfsrq = task_cfs_rq(p), |
2140 | *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu); | |
bbdba7c0 | 2141 | u64 clock_offset; |
dd41f596 IM |
2142 | |
2143 | clock_offset = old_rq->clock - new_rq->clock; | |
6cfb0d5d IM |
2144 | |
2145 | #ifdef CONFIG_SCHEDSTATS | |
2146 | if (p->se.wait_start) | |
2147 | p->se.wait_start -= clock_offset; | |
dd41f596 IM |
2148 | if (p->se.sleep_start) |
2149 | p->se.sleep_start -= clock_offset; | |
2150 | if (p->se.block_start) | |
2151 | p->se.block_start -= clock_offset; | |
cc367732 IM |
2152 | if (old_cpu != new_cpu) { |
2153 | schedstat_inc(p, se.nr_migrations); | |
2154 | if (task_hot(p, old_rq->clock, NULL)) | |
2155 | schedstat_inc(p, se.nr_forced2_migrations); | |
2156 | } | |
6cfb0d5d | 2157 | #endif |
2830cf8c SV |
2158 | p->se.vruntime -= old_cfsrq->min_vruntime - |
2159 | new_cfsrq->min_vruntime; | |
dd41f596 IM |
2160 | |
2161 | __set_task_cpu(p, new_cpu); | |
c65cc870 IM |
2162 | } |
2163 | ||
70b97a7f | 2164 | struct migration_req { |
1da177e4 | 2165 | struct list_head list; |
1da177e4 | 2166 | |
36c8b586 | 2167 | struct task_struct *task; |
1da177e4 LT |
2168 | int dest_cpu; |
2169 | ||
1da177e4 | 2170 | struct completion done; |
70b97a7f | 2171 | }; |
1da177e4 LT |
2172 | |
2173 | /* | |
2174 | * The task's runqueue lock must be held. | |
2175 | * Returns true if you have to wait for migration thread. | |
2176 | */ | |
36c8b586 | 2177 | static int |
70b97a7f | 2178 | migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req) |
1da177e4 | 2179 | { |
70b97a7f | 2180 | struct rq *rq = task_rq(p); |
1da177e4 LT |
2181 | |
2182 | /* | |
2183 | * If the task is not on a runqueue (and not running), then | |
2184 | * it is sufficient to simply update the task's cpu field. | |
2185 | */ | |
dd41f596 | 2186 | if (!p->se.on_rq && !task_running(rq, p)) { |
1da177e4 LT |
2187 | set_task_cpu(p, dest_cpu); |
2188 | return 0; | |
2189 | } | |
2190 | ||
2191 | init_completion(&req->done); | |
1da177e4 LT |
2192 | req->task = p; |
2193 | req->dest_cpu = dest_cpu; | |
2194 | list_add(&req->list, &rq->migration_queue); | |
48f24c4d | 2195 | |
1da177e4 LT |
2196 | return 1; |
2197 | } | |
2198 | ||
2199 | /* | |
2200 | * wait_task_inactive - wait for a thread to unschedule. | |
2201 | * | |
2202 | * The caller must ensure that the task *will* unschedule sometime soon, | |
2203 | * else this function might spin for a *long* time. This function can't | |
2204 | * be called with interrupts off, or it may introduce deadlock with | |
2205 | * smp_call_function() if an IPI is sent by the same process we are | |
2206 | * waiting to become inactive. | |
2207 | */ | |
36c8b586 | 2208 | void wait_task_inactive(struct task_struct *p) |
1da177e4 LT |
2209 | { |
2210 | unsigned long flags; | |
dd41f596 | 2211 | int running, on_rq; |
70b97a7f | 2212 | struct rq *rq; |
1da177e4 | 2213 | |
3a5c359a AK |
2214 | for (;;) { |
2215 | /* | |
2216 | * We do the initial early heuristics without holding | |
2217 | * any task-queue locks at all. We'll only try to get | |
2218 | * the runqueue lock when things look like they will | |
2219 | * work out! | |
2220 | */ | |
2221 | rq = task_rq(p); | |
fa490cfd | 2222 | |
3a5c359a AK |
2223 | /* |
2224 | * If the task is actively running on another CPU | |
2225 | * still, just relax and busy-wait without holding | |
2226 | * any locks. | |
2227 | * | |
2228 | * NOTE! Since we don't hold any locks, it's not | |
2229 | * even sure that "rq" stays as the right runqueue! | |
2230 | * But we don't care, since "task_running()" will | |
2231 | * return false if the runqueue has changed and p | |
2232 | * is actually now running somewhere else! | |
2233 | */ | |
2234 | while (task_running(rq, p)) | |
2235 | cpu_relax(); | |
fa490cfd | 2236 | |
3a5c359a AK |
2237 | /* |
2238 | * Ok, time to look more closely! We need the rq | |
2239 | * lock now, to be *sure*. If we're wrong, we'll | |
2240 | * just go back and repeat. | |
2241 | */ | |
2242 | rq = task_rq_lock(p, &flags); | |
2243 | running = task_running(rq, p); | |
2244 | on_rq = p->se.on_rq; | |
2245 | task_rq_unlock(rq, &flags); | |
fa490cfd | 2246 | |
3a5c359a AK |
2247 | /* |
2248 | * Was it really running after all now that we | |
2249 | * checked with the proper locks actually held? | |
2250 | * | |
2251 | * Oops. Go back and try again.. | |
2252 | */ | |
2253 | if (unlikely(running)) { | |
2254 | cpu_relax(); | |
2255 | continue; | |
2256 | } | |
fa490cfd | 2257 | |
3a5c359a AK |
2258 | /* |
2259 | * It's not enough that it's not actively running, | |
2260 | * it must be off the runqueue _entirely_, and not | |
2261 | * preempted! | |
2262 | * | |
2263 | * So if it wa still runnable (but just not actively | |
2264 | * running right now), it's preempted, and we should | |
2265 | * yield - it could be a while. | |
2266 | */ | |
2267 | if (unlikely(on_rq)) { | |
2268 | schedule_timeout_uninterruptible(1); | |
2269 | continue; | |
2270 | } | |
fa490cfd | 2271 | |
3a5c359a AK |
2272 | /* |
2273 | * Ahh, all good. It wasn't running, and it wasn't | |
2274 | * runnable, which means that it will never become | |
2275 | * running in the future either. We're all done! | |
2276 | */ | |
2277 | break; | |
2278 | } | |
1da177e4 LT |
2279 | } |
2280 | ||
2281 | /*** | |
2282 | * kick_process - kick a running thread to enter/exit the kernel | |
2283 | * @p: the to-be-kicked thread | |
2284 | * | |
2285 | * Cause a process which is running on another CPU to enter | |
2286 | * kernel-mode, without any delay. (to get signals handled.) | |
2287 | * | |
2288 | * NOTE: this function doesnt have to take the runqueue lock, | |
2289 | * because all it wants to ensure is that the remote task enters | |
2290 | * the kernel. If the IPI races and the task has been migrated | |
2291 | * to another CPU then no harm is done and the purpose has been | |
2292 | * achieved as well. | |
2293 | */ | |
36c8b586 | 2294 | void kick_process(struct task_struct *p) |
1da177e4 LT |
2295 | { |
2296 | int cpu; | |
2297 | ||
2298 | preempt_disable(); | |
2299 | cpu = task_cpu(p); | |
2300 | if ((cpu != smp_processor_id()) && task_curr(p)) | |
2301 | smp_send_reschedule(cpu); | |
2302 | preempt_enable(); | |
2303 | } | |
2304 | ||
2305 | /* | |
2dd73a4f PW |
2306 | * Return a low guess at the load of a migration-source cpu weighted |
2307 | * according to the scheduling class and "nice" value. | |
1da177e4 LT |
2308 | * |
2309 | * We want to under-estimate the load of migration sources, to | |
2310 | * balance conservatively. | |
2311 | */ | |
a9957449 | 2312 | static unsigned long source_load(int cpu, int type) |
1da177e4 | 2313 | { |
70b97a7f | 2314 | struct rq *rq = cpu_rq(cpu); |
dd41f596 | 2315 | unsigned long total = weighted_cpuload(cpu); |
2dd73a4f | 2316 | |
3b0bd9bc | 2317 | if (type == 0) |
dd41f596 | 2318 | return total; |
b910472d | 2319 | |
dd41f596 | 2320 | return min(rq->cpu_load[type-1], total); |
1da177e4 LT |
2321 | } |
2322 | ||
2323 | /* | |
2dd73a4f PW |
2324 | * Return a high guess at the load of a migration-target cpu weighted |
2325 | * according to the scheduling class and "nice" value. | |
1da177e4 | 2326 | */ |
a9957449 | 2327 | static unsigned long target_load(int cpu, int type) |
1da177e4 | 2328 | { |
70b97a7f | 2329 | struct rq *rq = cpu_rq(cpu); |
dd41f596 | 2330 | unsigned long total = weighted_cpuload(cpu); |
2dd73a4f | 2331 | |
7897986b | 2332 | if (type == 0) |
dd41f596 | 2333 | return total; |
3b0bd9bc | 2334 | |
dd41f596 | 2335 | return max(rq->cpu_load[type-1], total); |
2dd73a4f PW |
2336 | } |
2337 | ||
2338 | /* | |
2339 | * Return the average load per task on the cpu's run queue | |
2340 | */ | |
e7693a36 | 2341 | static unsigned long cpu_avg_load_per_task(int cpu) |
2dd73a4f | 2342 | { |
70b97a7f | 2343 | struct rq *rq = cpu_rq(cpu); |
dd41f596 | 2344 | unsigned long total = weighted_cpuload(cpu); |
2dd73a4f PW |
2345 | unsigned long n = rq->nr_running; |
2346 | ||
dd41f596 | 2347 | return n ? total / n : SCHED_LOAD_SCALE; |
1da177e4 LT |
2348 | } |
2349 | ||
147cbb4b NP |
2350 | /* |
2351 | * find_idlest_group finds and returns the least busy CPU group within the | |
2352 | * domain. | |
2353 | */ | |
2354 | static struct sched_group * | |
2355 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu) | |
2356 | { | |
2357 | struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups; | |
2358 | unsigned long min_load = ULONG_MAX, this_load = 0; | |
2359 | int load_idx = sd->forkexec_idx; | |
2360 | int imbalance = 100 + (sd->imbalance_pct-100)/2; | |
2361 | ||
2362 | do { | |
2363 | unsigned long load, avg_load; | |
2364 | int local_group; | |
2365 | int i; | |
2366 | ||
da5a5522 BD |
2367 | /* Skip over this group if it has no CPUs allowed */ |
2368 | if (!cpus_intersects(group->cpumask, p->cpus_allowed)) | |
3a5c359a | 2369 | continue; |
da5a5522 | 2370 | |
147cbb4b | 2371 | local_group = cpu_isset(this_cpu, group->cpumask); |
147cbb4b NP |
2372 | |
2373 | /* Tally up the load of all CPUs in the group */ | |
2374 | avg_load = 0; | |
2375 | ||
2376 | for_each_cpu_mask(i, group->cpumask) { | |
2377 | /* Bias balancing toward cpus of our domain */ | |
2378 | if (local_group) | |
2379 | load = source_load(i, load_idx); | |
2380 | else | |
2381 | load = target_load(i, load_idx); | |
2382 | ||
2383 | avg_load += load; | |
2384 | } | |
2385 | ||
2386 | /* Adjust by relative CPU power of the group */ | |
5517d86b ED |
2387 | avg_load = sg_div_cpu_power(group, |
2388 | avg_load * SCHED_LOAD_SCALE); | |
147cbb4b NP |
2389 | |
2390 | if (local_group) { | |
2391 | this_load = avg_load; | |
2392 | this = group; | |
2393 | } else if (avg_load < min_load) { | |
2394 | min_load = avg_load; | |
2395 | idlest = group; | |
2396 | } | |
3a5c359a | 2397 | } while (group = group->next, group != sd->groups); |
147cbb4b NP |
2398 | |
2399 | if (!idlest || 100*this_load < imbalance*min_load) | |
2400 | return NULL; | |
2401 | return idlest; | |
2402 | } | |
2403 | ||
2404 | /* | |
0feaece9 | 2405 | * find_idlest_cpu - find the idlest cpu among the cpus in group. |
147cbb4b | 2406 | */ |
95cdf3b7 | 2407 | static int |
7c16ec58 MT |
2408 | find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu, |
2409 | cpumask_t *tmp) | |
147cbb4b NP |
2410 | { |
2411 | unsigned long load, min_load = ULONG_MAX; | |
2412 | int idlest = -1; | |
2413 | int i; | |
2414 | ||
da5a5522 | 2415 | /* Traverse only the allowed CPUs */ |
7c16ec58 | 2416 | cpus_and(*tmp, group->cpumask, p->cpus_allowed); |
da5a5522 | 2417 | |
7c16ec58 | 2418 | for_each_cpu_mask(i, *tmp) { |
2dd73a4f | 2419 | load = weighted_cpuload(i); |
147cbb4b NP |
2420 | |
2421 | if (load < min_load || (load == min_load && i == this_cpu)) { | |
2422 | min_load = load; | |
2423 | idlest = i; | |
2424 | } | |
2425 | } | |
2426 | ||
2427 | return idlest; | |
2428 | } | |
2429 | ||
476d139c NP |
2430 | /* |
2431 | * sched_balance_self: balance the current task (running on cpu) in domains | |
2432 | * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and | |
2433 | * SD_BALANCE_EXEC. | |
2434 | * | |
2435 | * Balance, ie. select the least loaded group. | |
2436 | * | |
2437 | * Returns the target CPU number, or the same CPU if no balancing is needed. | |
2438 | * | |
2439 | * preempt must be disabled. | |
2440 | */ | |
2441 | static int sched_balance_self(int cpu, int flag) | |
2442 | { | |
2443 | struct task_struct *t = current; | |
2444 | struct sched_domain *tmp, *sd = NULL; | |
147cbb4b | 2445 | |
c96d145e | 2446 | for_each_domain(cpu, tmp) { |
9761eea8 IM |
2447 | /* |
2448 | * If power savings logic is enabled for a domain, stop there. | |
2449 | */ | |
5c45bf27 SS |
2450 | if (tmp->flags & SD_POWERSAVINGS_BALANCE) |
2451 | break; | |
476d139c NP |
2452 | if (tmp->flags & flag) |
2453 | sd = tmp; | |
c96d145e | 2454 | } |
476d139c NP |
2455 | |
2456 | while (sd) { | |
7c16ec58 | 2457 | cpumask_t span, tmpmask; |
476d139c | 2458 | struct sched_group *group; |
1a848870 SS |
2459 | int new_cpu, weight; |
2460 | ||
2461 | if (!(sd->flags & flag)) { | |
2462 | sd = sd->child; | |
2463 | continue; | |
2464 | } | |
476d139c NP |
2465 | |
2466 | span = sd->span; | |
2467 | group = find_idlest_group(sd, t, cpu); | |
1a848870 SS |
2468 | if (!group) { |
2469 | sd = sd->child; | |
2470 | continue; | |
2471 | } | |
476d139c | 2472 | |
7c16ec58 | 2473 | new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask); |
1a848870 SS |
2474 | if (new_cpu == -1 || new_cpu == cpu) { |
2475 | /* Now try balancing at a lower domain level of cpu */ | |
2476 | sd = sd->child; | |
2477 | continue; | |
2478 | } | |
476d139c | 2479 | |
1a848870 | 2480 | /* Now try balancing at a lower domain level of new_cpu */ |
476d139c | 2481 | cpu = new_cpu; |
476d139c NP |
2482 | sd = NULL; |
2483 | weight = cpus_weight(span); | |
2484 | for_each_domain(cpu, tmp) { | |
2485 | if (weight <= cpus_weight(tmp->span)) | |
2486 | break; | |
2487 | if (tmp->flags & flag) | |
2488 | sd = tmp; | |
2489 | } | |
2490 | /* while loop will break here if sd == NULL */ | |
2491 | } | |
2492 | ||
2493 | return cpu; | |
2494 | } | |
2495 | ||
2496 | #endif /* CONFIG_SMP */ | |
1da177e4 | 2497 | |
1da177e4 LT |
2498 | /*** |
2499 | * try_to_wake_up - wake up a thread | |
2500 | * @p: the to-be-woken-up thread | |
2501 | * @state: the mask of task states that can be woken | |
2502 | * @sync: do a synchronous wakeup? | |
2503 | * | |
2504 | * Put it on the run-queue if it's not already there. The "current" | |
2505 | * thread is always on the run-queue (except when the actual | |
2506 | * re-schedule is in progress), and as such you're allowed to do | |
2507 | * the simpler "current->state = TASK_RUNNING" to mark yourself | |
2508 | * runnable without the overhead of this. | |
2509 | * | |
2510 | * returns failure only if the task is already active. | |
2511 | */ | |
36c8b586 | 2512 | static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync) |
1da177e4 | 2513 | { |
cc367732 | 2514 | int cpu, orig_cpu, this_cpu, success = 0; |
1da177e4 LT |
2515 | unsigned long flags; |
2516 | long old_state; | |
70b97a7f | 2517 | struct rq *rq; |
1da177e4 | 2518 | |
b85d0667 IM |
2519 | if (!sched_feat(SYNC_WAKEUPS)) |
2520 | sync = 0; | |
2521 | ||
04e2f174 | 2522 | smp_wmb(); |
1da177e4 LT |
2523 | rq = task_rq_lock(p, &flags); |
2524 | old_state = p->state; | |
2525 | if (!(old_state & state)) | |
2526 | goto out; | |
2527 | ||
dd41f596 | 2528 | if (p->se.on_rq) |
1da177e4 LT |
2529 | goto out_running; |
2530 | ||
2531 | cpu = task_cpu(p); | |
cc367732 | 2532 | orig_cpu = cpu; |
1da177e4 LT |
2533 | this_cpu = smp_processor_id(); |
2534 | ||
2535 | #ifdef CONFIG_SMP | |
2536 | if (unlikely(task_running(rq, p))) | |
2537 | goto out_activate; | |
2538 | ||
5d2f5a61 DA |
2539 | cpu = p->sched_class->select_task_rq(p, sync); |
2540 | if (cpu != orig_cpu) { | |
2541 | set_task_cpu(p, cpu); | |
1da177e4 LT |
2542 | task_rq_unlock(rq, &flags); |
2543 | /* might preempt at this point */ | |
2544 | rq = task_rq_lock(p, &flags); | |
2545 | old_state = p->state; | |
2546 | if (!(old_state & state)) | |
2547 | goto out; | |
dd41f596 | 2548 | if (p->se.on_rq) |
1da177e4 LT |
2549 | goto out_running; |
2550 | ||
2551 | this_cpu = smp_processor_id(); | |
2552 | cpu = task_cpu(p); | |
2553 | } | |
2554 | ||
e7693a36 GH |
2555 | #ifdef CONFIG_SCHEDSTATS |
2556 | schedstat_inc(rq, ttwu_count); | |
2557 | if (cpu == this_cpu) | |
2558 | schedstat_inc(rq, ttwu_local); | |
2559 | else { | |
2560 | struct sched_domain *sd; | |
2561 | for_each_domain(this_cpu, sd) { | |
2562 | if (cpu_isset(cpu, sd->span)) { | |
2563 | schedstat_inc(sd, ttwu_wake_remote); | |
2564 | break; | |
2565 | } | |
2566 | } | |
2567 | } | |
e7693a36 GH |
2568 | #endif |
2569 | ||
1da177e4 LT |
2570 | out_activate: |
2571 | #endif /* CONFIG_SMP */ | |
cc367732 IM |
2572 | schedstat_inc(p, se.nr_wakeups); |
2573 | if (sync) | |
2574 | schedstat_inc(p, se.nr_wakeups_sync); | |
2575 | if (orig_cpu != cpu) | |
2576 | schedstat_inc(p, se.nr_wakeups_migrate); | |
2577 | if (cpu == this_cpu) | |
2578 | schedstat_inc(p, se.nr_wakeups_local); | |
2579 | else | |
2580 | schedstat_inc(p, se.nr_wakeups_remote); | |
2daa3577 | 2581 | update_rq_clock(rq); |
dd41f596 | 2582 | activate_task(rq, p, 1); |
1da177e4 LT |
2583 | success = 1; |
2584 | ||
2585 | out_running: | |
4ae7d5ce IM |
2586 | check_preempt_curr(rq, p); |
2587 | ||
1da177e4 | 2588 | p->state = TASK_RUNNING; |
9a897c5a SR |
2589 | #ifdef CONFIG_SMP |
2590 | if (p->sched_class->task_wake_up) | |
2591 | p->sched_class->task_wake_up(rq, p); | |
2592 | #endif | |
1da177e4 LT |
2593 | out: |
2594 | task_rq_unlock(rq, &flags); | |
2595 | ||
2596 | return success; | |
2597 | } | |
2598 | ||
7ad5b3a5 | 2599 | int wake_up_process(struct task_struct *p) |
1da177e4 | 2600 | { |
d9514f6c | 2601 | return try_to_wake_up(p, TASK_ALL, 0); |
1da177e4 | 2602 | } |
1da177e4 LT |
2603 | EXPORT_SYMBOL(wake_up_process); |
2604 | ||
7ad5b3a5 | 2605 | int wake_up_state(struct task_struct *p, unsigned int state) |
1da177e4 LT |
2606 | { |
2607 | return try_to_wake_up(p, state, 0); | |
2608 | } | |
2609 | ||
1da177e4 LT |
2610 | /* |
2611 | * Perform scheduler related setup for a newly forked process p. | |
2612 | * p is forked by current. | |
dd41f596 IM |
2613 | * |
2614 | * __sched_fork() is basic setup used by init_idle() too: | |
2615 | */ | |
2616 | static void __sched_fork(struct task_struct *p) | |
2617 | { | |
dd41f596 IM |
2618 | p->se.exec_start = 0; |
2619 | p->se.sum_exec_runtime = 0; | |
f6cf891c | 2620 | p->se.prev_sum_exec_runtime = 0; |
4ae7d5ce IM |
2621 | p->se.last_wakeup = 0; |
2622 | p->se.avg_overlap = 0; | |
6cfb0d5d IM |
2623 | |
2624 | #ifdef CONFIG_SCHEDSTATS | |
2625 | p->se.wait_start = 0; | |
dd41f596 IM |
2626 | p->se.sum_sleep_runtime = 0; |
2627 | p->se.sleep_start = 0; | |
dd41f596 IM |
2628 | p->se.block_start = 0; |
2629 | p->se.sleep_max = 0; | |
2630 | p->se.block_max = 0; | |
2631 | p->se.exec_max = 0; | |
eba1ed4b | 2632 | p->se.slice_max = 0; |
dd41f596 | 2633 | p->se.wait_max = 0; |
6cfb0d5d | 2634 | #endif |
476d139c | 2635 | |
fa717060 | 2636 | INIT_LIST_HEAD(&p->rt.run_list); |
dd41f596 | 2637 | p->se.on_rq = 0; |
4a55bd5e | 2638 | INIT_LIST_HEAD(&p->se.group_node); |
476d139c | 2639 | |
e107be36 AK |
2640 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
2641 | INIT_HLIST_HEAD(&p->preempt_notifiers); | |
2642 | #endif | |
2643 | ||
1da177e4 LT |
2644 | /* |
2645 | * We mark the process as running here, but have not actually | |
2646 | * inserted it onto the runqueue yet. This guarantees that | |
2647 | * nobody will actually run it, and a signal or other external | |
2648 | * event cannot wake it up and insert it on the runqueue either. | |
2649 | */ | |
2650 | p->state = TASK_RUNNING; | |
dd41f596 IM |
2651 | } |
2652 | ||
2653 | /* | |
2654 | * fork()/clone()-time setup: | |
2655 | */ | |
2656 | void sched_fork(struct task_struct *p, int clone_flags) | |
2657 | { | |
2658 | int cpu = get_cpu(); | |
2659 | ||
2660 | __sched_fork(p); | |
2661 | ||
2662 | #ifdef CONFIG_SMP | |
2663 | cpu = sched_balance_self(cpu, SD_BALANCE_FORK); | |
2664 | #endif | |
02e4bac2 | 2665 | set_task_cpu(p, cpu); |
b29739f9 IM |
2666 | |
2667 | /* | |
2668 | * Make sure we do not leak PI boosting priority to the child: | |
2669 | */ | |
2670 | p->prio = current->normal_prio; | |
2ddbf952 HS |
2671 | if (!rt_prio(p->prio)) |
2672 | p->sched_class = &fair_sched_class; | |
b29739f9 | 2673 | |
52f17b6c | 2674 | #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT) |
dd41f596 | 2675 | if (likely(sched_info_on())) |
52f17b6c | 2676 | memset(&p->sched_info, 0, sizeof(p->sched_info)); |
1da177e4 | 2677 | #endif |
d6077cb8 | 2678 | #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW) |
4866cde0 NP |
2679 | p->oncpu = 0; |
2680 | #endif | |
1da177e4 | 2681 | #ifdef CONFIG_PREEMPT |
4866cde0 | 2682 | /* Want to start with kernel preemption disabled. */ |
a1261f54 | 2683 | task_thread_info(p)->preempt_count = 1; |
1da177e4 | 2684 | #endif |
476d139c | 2685 | put_cpu(); |
1da177e4 LT |
2686 | } |
2687 | ||
2688 | /* | |
2689 | * wake_up_new_task - wake up a newly created task for the first time. | |
2690 | * | |
2691 | * This function will do some initial scheduler statistics housekeeping | |
2692 | * that must be done for every newly created context, then puts the task | |
2693 | * on the runqueue and wakes it. | |
2694 | */ | |
7ad5b3a5 | 2695 | void wake_up_new_task(struct task_struct *p, unsigned long clone_flags) |
1da177e4 LT |
2696 | { |
2697 | unsigned long flags; | |
dd41f596 | 2698 | struct rq *rq; |
1da177e4 LT |
2699 | |
2700 | rq = task_rq_lock(p, &flags); | |
147cbb4b | 2701 | BUG_ON(p->state != TASK_RUNNING); |
a8e504d2 | 2702 | update_rq_clock(rq); |
1da177e4 LT |
2703 | |
2704 | p->prio = effective_prio(p); | |
2705 | ||
b9dca1e0 | 2706 | if (!p->sched_class->task_new || !current->se.on_rq) { |
dd41f596 | 2707 | activate_task(rq, p, 0); |
1da177e4 | 2708 | } else { |
1da177e4 | 2709 | /* |
dd41f596 IM |
2710 | * Let the scheduling class do new task startup |
2711 | * management (if any): | |
1da177e4 | 2712 | */ |
ee0827d8 | 2713 | p->sched_class->task_new(rq, p); |
18d95a28 | 2714 | inc_nr_running(rq); |
1da177e4 | 2715 | } |
dd41f596 | 2716 | check_preempt_curr(rq, p); |
9a897c5a SR |
2717 | #ifdef CONFIG_SMP |
2718 | if (p->sched_class->task_wake_up) | |
2719 | p->sched_class->task_wake_up(rq, p); | |
2720 | #endif | |
dd41f596 | 2721 | task_rq_unlock(rq, &flags); |
1da177e4 LT |
2722 | } |
2723 | ||
e107be36 AK |
2724 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
2725 | ||
2726 | /** | |
421cee29 RD |
2727 | * preempt_notifier_register - tell me when current is being being preempted & rescheduled |
2728 | * @notifier: notifier struct to register | |
e107be36 AK |
2729 | */ |
2730 | void preempt_notifier_register(struct preempt_notifier *notifier) | |
2731 | { | |
2732 | hlist_add_head(¬ifier->link, ¤t->preempt_notifiers); | |
2733 | } | |
2734 | EXPORT_SYMBOL_GPL(preempt_notifier_register); | |
2735 | ||
2736 | /** | |
2737 | * preempt_notifier_unregister - no longer interested in preemption notifications | |
421cee29 | 2738 | * @notifier: notifier struct to unregister |
e107be36 AK |
2739 | * |
2740 | * This is safe to call from within a preemption notifier. | |
2741 | */ | |
2742 | void preempt_notifier_unregister(struct preempt_notifier *notifier) | |
2743 | { | |
2744 | hlist_del(¬ifier->link); | |
2745 | } | |
2746 | EXPORT_SYMBOL_GPL(preempt_notifier_unregister); | |
2747 | ||
2748 | static void fire_sched_in_preempt_notifiers(struct task_struct *curr) | |
2749 | { | |
2750 | struct preempt_notifier *notifier; | |
2751 | struct hlist_node *node; | |
2752 | ||
2753 | hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link) | |
2754 | notifier->ops->sched_in(notifier, raw_smp_processor_id()); | |
2755 | } | |
2756 | ||
2757 | static void | |
2758 | fire_sched_out_preempt_notifiers(struct task_struct *curr, | |
2759 | struct task_struct *next) | |
2760 | { | |
2761 | struct preempt_notifier *notifier; | |
2762 | struct hlist_node *node; | |
2763 | ||
2764 | hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link) | |
2765 | notifier->ops->sched_out(notifier, next); | |
2766 | } | |
2767 | ||
2768 | #else | |
2769 | ||
2770 | static void fire_sched_in_preempt_notifiers(struct task_struct *curr) | |
2771 | { | |
2772 | } | |
2773 | ||
2774 | static void | |
2775 | fire_sched_out_preempt_notifiers(struct task_struct *curr, | |
2776 | struct task_struct *next) | |
2777 | { | |
2778 | } | |
2779 | ||
2780 | #endif | |
2781 | ||
4866cde0 NP |
2782 | /** |
2783 | * prepare_task_switch - prepare to switch tasks | |
2784 | * @rq: the runqueue preparing to switch | |
421cee29 | 2785 | * @prev: the current task that is being switched out |
4866cde0 NP |
2786 | * @next: the task we are going to switch to. |
2787 | * | |
2788 | * This is called with the rq lock held and interrupts off. It must | |
2789 | * be paired with a subsequent finish_task_switch after the context | |
2790 | * switch. | |
2791 | * | |
2792 | * prepare_task_switch sets up locking and calls architecture specific | |
2793 | * hooks. | |
2794 | */ | |
e107be36 AK |
2795 | static inline void |
2796 | prepare_task_switch(struct rq *rq, struct task_struct *prev, | |
2797 | struct task_struct *next) | |
4866cde0 | 2798 | { |
e107be36 | 2799 | fire_sched_out_preempt_notifiers(prev, next); |
4866cde0 NP |
2800 | prepare_lock_switch(rq, next); |
2801 | prepare_arch_switch(next); | |
2802 | } | |
2803 | ||
1da177e4 LT |
2804 | /** |
2805 | * finish_task_switch - clean up after a task-switch | |
344babaa | 2806 | * @rq: runqueue associated with task-switch |
1da177e4 LT |
2807 | * @prev: the thread we just switched away from. |
2808 | * | |
4866cde0 NP |
2809 | * finish_task_switch must be called after the context switch, paired |
2810 | * with a prepare_task_switch call before the context switch. | |
2811 | * finish_task_switch will reconcile locking set up by prepare_task_switch, | |
2812 | * and do any other architecture-specific cleanup actions. | |
1da177e4 LT |
2813 | * |
2814 | * Note that we may have delayed dropping an mm in context_switch(). If | |
41a2d6cf | 2815 | * so, we finish that here outside of the runqueue lock. (Doing it |
1da177e4 LT |
2816 | * with the lock held can cause deadlocks; see schedule() for |
2817 | * details.) | |
2818 | */ | |
a9957449 | 2819 | static void finish_task_switch(struct rq *rq, struct task_struct *prev) |
1da177e4 LT |
2820 | __releases(rq->lock) |
2821 | { | |
1da177e4 | 2822 | struct mm_struct *mm = rq->prev_mm; |
55a101f8 | 2823 | long prev_state; |
1da177e4 LT |
2824 | |
2825 | rq->prev_mm = NULL; | |
2826 | ||
2827 | /* | |
2828 | * A task struct has one reference for the use as "current". | |
c394cc9f | 2829 | * If a task dies, then it sets TASK_DEAD in tsk->state and calls |
55a101f8 ON |
2830 | * schedule one last time. The schedule call will never return, and |
2831 | * the scheduled task must drop that reference. | |
c394cc9f | 2832 | * The test for TASK_DEAD must occur while the runqueue locks are |
1da177e4 LT |
2833 | * still held, otherwise prev could be scheduled on another cpu, die |
2834 | * there before we look at prev->state, and then the reference would | |
2835 | * be dropped twice. | |
2836 | * Manfred Spraul <manfred@colorfullife.com> | |
2837 | */ | |
55a101f8 | 2838 | prev_state = prev->state; |
4866cde0 NP |
2839 | finish_arch_switch(prev); |
2840 | finish_lock_switch(rq, prev); | |
9a897c5a SR |
2841 | #ifdef CONFIG_SMP |
2842 | if (current->sched_class->post_schedule) | |
2843 | current->sched_class->post_schedule(rq); | |
2844 | #endif | |
e8fa1362 | 2845 | |
e107be36 | 2846 | fire_sched_in_preempt_notifiers(current); |
1da177e4 LT |
2847 | if (mm) |
2848 | mmdrop(mm); | |
c394cc9f | 2849 | if (unlikely(prev_state == TASK_DEAD)) { |
c6fd91f0 | 2850 | /* |
2851 | * Remove function-return probe instances associated with this | |
2852 | * task and put them back on the free list. | |
9761eea8 | 2853 | */ |
c6fd91f0 | 2854 | kprobe_flush_task(prev); |
1da177e4 | 2855 | put_task_struct(prev); |
c6fd91f0 | 2856 | } |
1da177e4 LT |
2857 | } |
2858 | ||
2859 | /** | |
2860 | * schedule_tail - first thing a freshly forked thread must call. | |
2861 | * @prev: the thread we just switched away from. | |
2862 | */ | |
36c8b586 | 2863 | asmlinkage void schedule_tail(struct task_struct *prev) |
1da177e4 LT |
2864 | __releases(rq->lock) |
2865 | { | |
70b97a7f IM |
2866 | struct rq *rq = this_rq(); |
2867 | ||
4866cde0 NP |
2868 | finish_task_switch(rq, prev); |
2869 | #ifdef __ARCH_WANT_UNLOCKED_CTXSW | |
2870 | /* In this case, finish_task_switch does not reenable preemption */ | |
2871 | preempt_enable(); | |
2872 | #endif | |
1da177e4 | 2873 | if (current->set_child_tid) |
b488893a | 2874 | put_user(task_pid_vnr(current), current->set_child_tid); |
1da177e4 LT |
2875 | } |
2876 | ||
2877 | /* | |
2878 | * context_switch - switch to the new MM and the new | |
2879 | * thread's register state. | |
2880 | */ | |
dd41f596 | 2881 | static inline void |
70b97a7f | 2882 | context_switch(struct rq *rq, struct task_struct *prev, |
36c8b586 | 2883 | struct task_struct *next) |
1da177e4 | 2884 | { |
dd41f596 | 2885 | struct mm_struct *mm, *oldmm; |
1da177e4 | 2886 | |
e107be36 | 2887 | prepare_task_switch(rq, prev, next); |
dd41f596 IM |
2888 | mm = next->mm; |
2889 | oldmm = prev->active_mm; | |
9226d125 ZA |
2890 | /* |
2891 | * For paravirt, this is coupled with an exit in switch_to to | |
2892 | * combine the page table reload and the switch backend into | |
2893 | * one hypercall. | |
2894 | */ | |
2895 | arch_enter_lazy_cpu_mode(); | |
2896 | ||
dd41f596 | 2897 | if (unlikely(!mm)) { |
1da177e4 LT |
2898 | next->active_mm = oldmm; |
2899 | atomic_inc(&oldmm->mm_count); | |
2900 | enter_lazy_tlb(oldmm, next); | |
2901 | } else | |
2902 | switch_mm(oldmm, mm, next); | |
2903 | ||
dd41f596 | 2904 | if (unlikely(!prev->mm)) { |
1da177e4 | 2905 | prev->active_mm = NULL; |
1da177e4 LT |
2906 | rq->prev_mm = oldmm; |
2907 | } | |
3a5f5e48 IM |
2908 | /* |
2909 | * Since the runqueue lock will be released by the next | |
2910 | * task (which is an invalid locking op but in the case | |
2911 | * of the scheduler it's an obvious special-case), so we | |
2912 | * do an early lockdep release here: | |
2913 | */ | |
2914 | #ifndef __ARCH_WANT_UNLOCKED_CTXSW | |
8a25d5de | 2915 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); |
3a5f5e48 | 2916 | #endif |
1da177e4 LT |
2917 | |
2918 | /* Here we just switch the register state and the stack. */ | |
2919 | switch_to(prev, next, prev); | |
2920 | ||
dd41f596 IM |
2921 | barrier(); |
2922 | /* | |
2923 | * this_rq must be evaluated again because prev may have moved | |
2924 | * CPUs since it called schedule(), thus the 'rq' on its stack | |
2925 | * frame will be invalid. | |
2926 | */ | |
2927 | finish_task_switch(this_rq(), prev); | |
1da177e4 LT |
2928 | } |
2929 | ||
2930 | /* | |
2931 | * nr_running, nr_uninterruptible and nr_context_switches: | |
2932 | * | |
2933 | * externally visible scheduler statistics: current number of runnable | |
2934 | * threads, current number of uninterruptible-sleeping threads, total | |
2935 | * number of context switches performed since bootup. | |
2936 | */ | |
2937 | unsigned long nr_running(void) | |
2938 | { | |
2939 | unsigned long i, sum = 0; | |
2940 | ||
2941 | for_each_online_cpu(i) | |
2942 | sum += cpu_rq(i)->nr_running; | |
2943 | ||
2944 | return sum; | |
2945 | } | |
2946 | ||
2947 | unsigned long nr_uninterruptible(void) | |
2948 | { | |
2949 | unsigned long i, sum = 0; | |
2950 | ||
0a945022 | 2951 | for_each_possible_cpu(i) |
1da177e4 LT |
2952 | sum += cpu_rq(i)->nr_uninterruptible; |
2953 | ||
2954 | /* | |
2955 | * Since we read the counters lockless, it might be slightly | |
2956 | * inaccurate. Do not allow it to go below zero though: | |
2957 | */ | |
2958 | if (unlikely((long)sum < 0)) | |
2959 | sum = 0; | |
2960 | ||
2961 | return sum; | |
2962 | } | |
2963 | ||
2964 | unsigned long long nr_context_switches(void) | |
2965 | { | |
cc94abfc SR |
2966 | int i; |
2967 | unsigned long long sum = 0; | |
1da177e4 | 2968 | |
0a945022 | 2969 | for_each_possible_cpu(i) |
1da177e4 LT |
2970 | sum += cpu_rq(i)->nr_switches; |
2971 | ||
2972 | return sum; | |
2973 | } | |
2974 | ||
2975 | unsigned long nr_iowait(void) | |
2976 | { | |
2977 | unsigned long i, sum = 0; | |
2978 | ||
0a945022 | 2979 | for_each_possible_cpu(i) |
1da177e4 LT |
2980 | sum += atomic_read(&cpu_rq(i)->nr_iowait); |
2981 | ||
2982 | return sum; | |
2983 | } | |
2984 | ||
db1b1fef JS |
2985 | unsigned long nr_active(void) |
2986 | { | |
2987 | unsigned long i, running = 0, uninterruptible = 0; | |
2988 | ||
2989 | for_each_online_cpu(i) { | |
2990 | running += cpu_rq(i)->nr_running; | |
2991 | uninterruptible += cpu_rq(i)->nr_uninterruptible; | |
2992 | } | |
2993 | ||
2994 | if (unlikely((long)uninterruptible < 0)) | |
2995 | uninterruptible = 0; | |
2996 | ||
2997 | return running + uninterruptible; | |
2998 | } | |
2999 | ||
48f24c4d | 3000 | /* |
dd41f596 IM |
3001 | * Update rq->cpu_load[] statistics. This function is usually called every |
3002 | * scheduler tick (TICK_NSEC). | |
48f24c4d | 3003 | */ |
dd41f596 | 3004 | static void update_cpu_load(struct rq *this_rq) |
48f24c4d | 3005 | { |
495eca49 | 3006 | unsigned long this_load = this_rq->load.weight; |
dd41f596 IM |
3007 | int i, scale; |
3008 | ||
3009 | this_rq->nr_load_updates++; | |
dd41f596 IM |
3010 | |
3011 | /* Update our load: */ | |
3012 | for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) { | |
3013 | unsigned long old_load, new_load; | |
3014 | ||
3015 | /* scale is effectively 1 << i now, and >> i divides by scale */ | |
3016 | ||
3017 | old_load = this_rq->cpu_load[i]; | |
3018 | new_load = this_load; | |
a25707f3 IM |
3019 | /* |
3020 | * Round up the averaging division if load is increasing. This | |
3021 | * prevents us from getting stuck on 9 if the load is 10, for | |
3022 | * example. | |
3023 | */ | |
3024 | if (new_load > old_load) | |
3025 | new_load += scale-1; | |
dd41f596 IM |
3026 | this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i; |
3027 | } | |
48f24c4d IM |
3028 | } |
3029 | ||
dd41f596 IM |
3030 | #ifdef CONFIG_SMP |
3031 | ||
1da177e4 LT |
3032 | /* |
3033 | * double_rq_lock - safely lock two runqueues | |
3034 | * | |
3035 | * Note this does not disable interrupts like task_rq_lock, | |
3036 | * you need to do so manually before calling. | |
3037 | */ | |
70b97a7f | 3038 | static void double_rq_lock(struct rq *rq1, struct rq *rq2) |
1da177e4 LT |
3039 | __acquires(rq1->lock) |
3040 | __acquires(rq2->lock) | |
3041 | { | |
054b9108 | 3042 | BUG_ON(!irqs_disabled()); |
1da177e4 LT |
3043 | if (rq1 == rq2) { |
3044 | spin_lock(&rq1->lock); | |
3045 | __acquire(rq2->lock); /* Fake it out ;) */ | |
3046 | } else { | |
c96d145e | 3047 | if (rq1 < rq2) { |
1da177e4 LT |
3048 | spin_lock(&rq1->lock); |
3049 | spin_lock(&rq2->lock); | |
3050 | } else { | |
3051 | spin_lock(&rq2->lock); | |
3052 | spin_lock(&rq1->lock); | |
3053 | } | |
3054 | } | |
6e82a3be IM |
3055 | update_rq_clock(rq1); |
3056 | update_rq_clock(rq2); | |
1da177e4 LT |
3057 | } |
3058 | ||
3059 | /* | |
3060 | * double_rq_unlock - safely unlock two runqueues | |
3061 | * | |
3062 | * Note this does not restore interrupts like task_rq_unlock, | |
3063 | * you need to do so manually after calling. | |
3064 | */ | |
70b97a7f | 3065 | static void double_rq_unlock(struct rq *rq1, struct rq *rq2) |
1da177e4 LT |
3066 | __releases(rq1->lock) |
3067 | __releases(rq2->lock) | |
3068 | { | |
3069 | spin_unlock(&rq1->lock); | |
3070 | if (rq1 != rq2) | |
3071 | spin_unlock(&rq2->lock); | |
3072 | else | |
3073 | __release(rq2->lock); | |
3074 | } | |
3075 | ||
3076 | /* | |
3077 | * double_lock_balance - lock the busiest runqueue, this_rq is locked already. | |
3078 | */ | |
e8fa1362 | 3079 | static int double_lock_balance(struct rq *this_rq, struct rq *busiest) |
1da177e4 LT |
3080 | __releases(this_rq->lock) |
3081 | __acquires(busiest->lock) | |
3082 | __acquires(this_rq->lock) | |
3083 | { | |
e8fa1362 SR |
3084 | int ret = 0; |
3085 | ||
054b9108 KK |
3086 | if (unlikely(!irqs_disabled())) { |
3087 | /* printk() doesn't work good under rq->lock */ | |
3088 | spin_unlock(&this_rq->lock); | |
3089 | BUG_ON(1); | |
3090 | } | |
1da177e4 | 3091 | if (unlikely(!spin_trylock(&busiest->lock))) { |
c96d145e | 3092 | if (busiest < this_rq) { |
1da177e4 LT |
3093 | spin_unlock(&this_rq->lock); |
3094 | spin_lock(&busiest->lock); | |
3095 | spin_lock(&this_rq->lock); | |
e8fa1362 | 3096 | ret = 1; |
1da177e4 LT |
3097 | } else |
3098 | spin_lock(&busiest->lock); | |
3099 | } | |
e8fa1362 | 3100 | return ret; |
1da177e4 LT |
3101 | } |
3102 | ||
1da177e4 LT |
3103 | /* |
3104 | * If dest_cpu is allowed for this process, migrate the task to it. | |
3105 | * This is accomplished by forcing the cpu_allowed mask to only | |
41a2d6cf | 3106 | * allow dest_cpu, which will force the cpu onto dest_cpu. Then |
1da177e4 LT |
3107 | * the cpu_allowed mask is restored. |
3108 | */ | |
36c8b586 | 3109 | static void sched_migrate_task(struct task_struct *p, int dest_cpu) |
1da177e4 | 3110 | { |
70b97a7f | 3111 | struct migration_req req; |
1da177e4 | 3112 | unsigned long flags; |
70b97a7f | 3113 | struct rq *rq; |
1da177e4 LT |
3114 | |
3115 | rq = task_rq_lock(p, &flags); | |
3116 | if (!cpu_isset(dest_cpu, p->cpus_allowed) | |
3117 | || unlikely(cpu_is_offline(dest_cpu))) | |
3118 | goto out; | |
3119 | ||
3120 | /* force the process onto the specified CPU */ | |
3121 | if (migrate_task(p, dest_cpu, &req)) { | |
3122 | /* Need to wait for migration thread (might exit: take ref). */ | |
3123 | struct task_struct *mt = rq->migration_thread; | |
36c8b586 | 3124 | |
1da177e4 LT |
3125 | get_task_struct(mt); |
3126 | task_rq_unlock(rq, &flags); | |
3127 | wake_up_process(mt); | |
3128 | put_task_struct(mt); | |
3129 | wait_for_completion(&req.done); | |
36c8b586 | 3130 | |
1da177e4 LT |
3131 | return; |
3132 | } | |
3133 | out: | |
3134 | task_rq_unlock(rq, &flags); | |
3135 | } | |
3136 | ||
3137 | /* | |
476d139c NP |
3138 | * sched_exec - execve() is a valuable balancing opportunity, because at |
3139 | * this point the task has the smallest effective memory and cache footprint. | |
1da177e4 LT |
3140 | */ |
3141 | void sched_exec(void) | |
3142 | { | |
1da177e4 | 3143 | int new_cpu, this_cpu = get_cpu(); |
476d139c | 3144 | new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC); |
1da177e4 | 3145 | put_cpu(); |
476d139c NP |
3146 | if (new_cpu != this_cpu) |
3147 | sched_migrate_task(current, new_cpu); | |
1da177e4 LT |
3148 | } |
3149 | ||
3150 | /* | |
3151 | * pull_task - move a task from a remote runqueue to the local runqueue. | |
3152 | * Both runqueues must be locked. | |
3153 | */ | |
dd41f596 IM |
3154 | static void pull_task(struct rq *src_rq, struct task_struct *p, |
3155 | struct rq *this_rq, int this_cpu) | |
1da177e4 | 3156 | { |
2e1cb74a | 3157 | deactivate_task(src_rq, p, 0); |
1da177e4 | 3158 | set_task_cpu(p, this_cpu); |
dd41f596 | 3159 | activate_task(this_rq, p, 0); |
1da177e4 LT |
3160 | /* |
3161 | * Note that idle threads have a prio of MAX_PRIO, for this test | |
3162 | * to be always true for them. | |
3163 | */ | |
dd41f596 | 3164 | check_preempt_curr(this_rq, p); |
1da177e4 LT |
3165 | } |
3166 | ||
3167 | /* | |
3168 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? | |
3169 | */ | |
858119e1 | 3170 | static |
70b97a7f | 3171 | int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu, |
d15bcfdb | 3172 | struct sched_domain *sd, enum cpu_idle_type idle, |
95cdf3b7 | 3173 | int *all_pinned) |
1da177e4 LT |
3174 | { |
3175 | /* | |
3176 | * We do not migrate tasks that are: | |
3177 | * 1) running (obviously), or | |
3178 | * 2) cannot be migrated to this CPU due to cpus_allowed, or | |
3179 | * 3) are cache-hot on their current CPU. | |
3180 | */ | |
cc367732 IM |
3181 | if (!cpu_isset(this_cpu, p->cpus_allowed)) { |
3182 | schedstat_inc(p, se.nr_failed_migrations_affine); | |
1da177e4 | 3183 | return 0; |
cc367732 | 3184 | } |
81026794 NP |
3185 | *all_pinned = 0; |
3186 | ||
cc367732 IM |
3187 | if (task_running(rq, p)) { |
3188 | schedstat_inc(p, se.nr_failed_migrations_running); | |
81026794 | 3189 | return 0; |
cc367732 | 3190 | } |
1da177e4 | 3191 | |
da84d961 IM |
3192 | /* |
3193 | * Aggressive migration if: | |
3194 | * 1) task is cache cold, or | |
3195 | * 2) too many balance attempts have failed. | |
3196 | */ | |
3197 | ||
6bc1665b IM |
3198 | if (!task_hot(p, rq->clock, sd) || |
3199 | sd->nr_balance_failed > sd->cache_nice_tries) { | |
da84d961 | 3200 | #ifdef CONFIG_SCHEDSTATS |
cc367732 | 3201 | if (task_hot(p, rq->clock, sd)) { |
da84d961 | 3202 | schedstat_inc(sd, lb_hot_gained[idle]); |
cc367732 IM |
3203 | schedstat_inc(p, se.nr_forced_migrations); |
3204 | } | |
da84d961 IM |
3205 | #endif |
3206 | return 1; | |
3207 | } | |
3208 | ||
cc367732 IM |
3209 | if (task_hot(p, rq->clock, sd)) { |
3210 | schedstat_inc(p, se.nr_failed_migrations_hot); | |
da84d961 | 3211 | return 0; |
cc367732 | 3212 | } |
1da177e4 LT |
3213 | return 1; |
3214 | } | |
3215 | ||
e1d1484f PW |
3216 | static unsigned long |
3217 | balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, | |
3218 | unsigned long max_load_move, struct sched_domain *sd, | |
3219 | enum cpu_idle_type idle, int *all_pinned, | |
3220 | int *this_best_prio, struct rq_iterator *iterator) | |
1da177e4 | 3221 | { |
b82d9fdd | 3222 | int loops = 0, pulled = 0, pinned = 0, skip_for_load; |
dd41f596 IM |
3223 | struct task_struct *p; |
3224 | long rem_load_move = max_load_move; | |
1da177e4 | 3225 | |
e1d1484f | 3226 | if (max_load_move == 0) |
1da177e4 LT |
3227 | goto out; |
3228 | ||
81026794 NP |
3229 | pinned = 1; |
3230 | ||
1da177e4 | 3231 | /* |
dd41f596 | 3232 | * Start the load-balancing iterator: |
1da177e4 | 3233 | */ |
dd41f596 IM |
3234 | p = iterator->start(iterator->arg); |
3235 | next: | |
b82d9fdd | 3236 | if (!p || loops++ > sysctl_sched_nr_migrate) |
1da177e4 | 3237 | goto out; |
50ddd969 | 3238 | /* |
b82d9fdd | 3239 | * To help distribute high priority tasks across CPUs we don't |
50ddd969 PW |
3240 | * skip a task if it will be the highest priority task (i.e. smallest |
3241 | * prio value) on its new queue regardless of its load weight | |
3242 | */ | |
dd41f596 IM |
3243 | skip_for_load = (p->se.load.weight >> 1) > rem_load_move + |
3244 | SCHED_LOAD_SCALE_FUZZ; | |
a4ac01c3 | 3245 | if ((skip_for_load && p->prio >= *this_best_prio) || |
dd41f596 | 3246 | !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) { |
dd41f596 IM |
3247 | p = iterator->next(iterator->arg); |
3248 | goto next; | |
1da177e4 LT |
3249 | } |
3250 | ||
dd41f596 | 3251 | pull_task(busiest, p, this_rq, this_cpu); |
1da177e4 | 3252 | pulled++; |
dd41f596 | 3253 | rem_load_move -= p->se.load.weight; |
1da177e4 | 3254 | |
2dd73a4f | 3255 | /* |
b82d9fdd | 3256 | * We only want to steal up to the prescribed amount of weighted load. |
2dd73a4f | 3257 | */ |
e1d1484f | 3258 | if (rem_load_move > 0) { |
a4ac01c3 PW |
3259 | if (p->prio < *this_best_prio) |
3260 | *this_best_prio = p->prio; | |
dd41f596 IM |
3261 | p = iterator->next(iterator->arg); |
3262 | goto next; | |
1da177e4 LT |
3263 | } |
3264 | out: | |
3265 | /* | |
e1d1484f | 3266 | * Right now, this is one of only two places pull_task() is called, |
1da177e4 LT |
3267 | * so we can safely collect pull_task() stats here rather than |
3268 | * inside pull_task(). | |
3269 | */ | |
3270 | schedstat_add(sd, lb_gained[idle], pulled); | |
81026794 NP |
3271 | |
3272 | if (all_pinned) | |
3273 | *all_pinned = pinned; | |
e1d1484f PW |
3274 | |
3275 | return max_load_move - rem_load_move; | |
1da177e4 LT |
3276 | } |
3277 | ||
dd41f596 | 3278 | /* |
43010659 PW |
3279 | * move_tasks tries to move up to max_load_move weighted load from busiest to |
3280 | * this_rq, as part of a balancing operation within domain "sd". | |
3281 | * Returns 1 if successful and 0 otherwise. | |
dd41f596 IM |
3282 | * |
3283 | * Called with both runqueues locked. | |
3284 | */ | |
3285 | static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, | |
43010659 | 3286 | unsigned long max_load_move, |
dd41f596 IM |
3287 | struct sched_domain *sd, enum cpu_idle_type idle, |
3288 | int *all_pinned) | |
3289 | { | |
5522d5d5 | 3290 | const struct sched_class *class = sched_class_highest; |
43010659 | 3291 | unsigned long total_load_moved = 0; |
a4ac01c3 | 3292 | int this_best_prio = this_rq->curr->prio; |
dd41f596 IM |
3293 | |
3294 | do { | |
43010659 PW |
3295 | total_load_moved += |
3296 | class->load_balance(this_rq, this_cpu, busiest, | |
e1d1484f | 3297 | max_load_move - total_load_moved, |
a4ac01c3 | 3298 | sd, idle, all_pinned, &this_best_prio); |
dd41f596 | 3299 | class = class->next; |
43010659 | 3300 | } while (class && max_load_move > total_load_moved); |
dd41f596 | 3301 | |
43010659 PW |
3302 | return total_load_moved > 0; |
3303 | } | |
3304 | ||
e1d1484f PW |
3305 | static int |
3306 | iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest, | |
3307 | struct sched_domain *sd, enum cpu_idle_type idle, | |
3308 | struct rq_iterator *iterator) | |
3309 | { | |
3310 | struct task_struct *p = iterator->start(iterator->arg); | |
3311 | int pinned = 0; | |
3312 | ||
3313 | while (p) { | |
3314 | if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) { | |
3315 | pull_task(busiest, p, this_rq, this_cpu); | |
3316 | /* | |
3317 | * Right now, this is only the second place pull_task() | |
3318 | * is called, so we can safely collect pull_task() | |
3319 | * stats here rather than inside pull_task(). | |
3320 | */ | |
3321 | schedstat_inc(sd, lb_gained[idle]); | |
3322 | ||
3323 | return 1; | |
3324 | } | |
3325 | p = iterator->next(iterator->arg); | |
3326 | } | |
3327 | ||
3328 | return 0; | |
3329 | } | |
3330 | ||
43010659 PW |
3331 | /* |
3332 | * move_one_task tries to move exactly one task from busiest to this_rq, as | |
3333 | * part of active balancing operations within "domain". | |
3334 | * Returns 1 if successful and 0 otherwise. | |
3335 | * | |
3336 | * Called with both runqueues locked. | |
3337 | */ | |
3338 | static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest, | |
3339 | struct sched_domain *sd, enum cpu_idle_type idle) | |
3340 | { | |
5522d5d5 | 3341 | const struct sched_class *class; |
43010659 PW |
3342 | |
3343 | for (class = sched_class_highest; class; class = class->next) | |
e1d1484f | 3344 | if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle)) |
43010659 PW |
3345 | return 1; |
3346 | ||
3347 | return 0; | |
dd41f596 IM |
3348 | } |
3349 | ||
1da177e4 LT |
3350 | /* |
3351 | * find_busiest_group finds and returns the busiest CPU group within the | |
48f24c4d IM |
3352 | * domain. It calculates and returns the amount of weighted load which |
3353 | * should be moved to restore balance via the imbalance parameter. | |
1da177e4 LT |
3354 | */ |
3355 | static struct sched_group * | |
3356 | find_busiest_group(struct sched_domain *sd, int this_cpu, | |
dd41f596 | 3357 | unsigned long *imbalance, enum cpu_idle_type idle, |
7c16ec58 | 3358 | int *sd_idle, const cpumask_t *cpus, int *balance) |
1da177e4 LT |
3359 | { |
3360 | struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups; | |
3361 | unsigned long max_load, avg_load, total_load, this_load, total_pwr; | |
0c117f1b | 3362 | unsigned long max_pull; |
2dd73a4f PW |
3363 | unsigned long busiest_load_per_task, busiest_nr_running; |
3364 | unsigned long this_load_per_task, this_nr_running; | |
908a7c1b | 3365 | int load_idx, group_imb = 0; |
5c45bf27 SS |
3366 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
3367 | int power_savings_balance = 1; | |
3368 | unsigned long leader_nr_running = 0, min_load_per_task = 0; | |
3369 | unsigned long min_nr_running = ULONG_MAX; | |
3370 | struct sched_group *group_min = NULL, *group_leader = NULL; | |
3371 | #endif | |
1da177e4 LT |
3372 | |
3373 | max_load = this_load = total_load = total_pwr = 0; | |
2dd73a4f PW |
3374 | busiest_load_per_task = busiest_nr_running = 0; |
3375 | this_load_per_task = this_nr_running = 0; | |
d15bcfdb | 3376 | if (idle == CPU_NOT_IDLE) |
7897986b | 3377 | load_idx = sd->busy_idx; |
d15bcfdb | 3378 | else if (idle == CPU_NEWLY_IDLE) |
7897986b NP |
3379 | load_idx = sd->newidle_idx; |
3380 | else | |
3381 | load_idx = sd->idle_idx; | |
1da177e4 LT |
3382 | |
3383 | do { | |
908a7c1b | 3384 | unsigned long load, group_capacity, max_cpu_load, min_cpu_load; |
1da177e4 LT |
3385 | int local_group; |
3386 | int i; | |
908a7c1b | 3387 | int __group_imb = 0; |
783609c6 | 3388 | unsigned int balance_cpu = -1, first_idle_cpu = 0; |
2dd73a4f | 3389 | unsigned long sum_nr_running, sum_weighted_load; |
1da177e4 LT |
3390 | |
3391 | local_group = cpu_isset(this_cpu, group->cpumask); | |
3392 | ||
783609c6 SS |
3393 | if (local_group) |
3394 | balance_cpu = first_cpu(group->cpumask); | |
3395 | ||
1da177e4 | 3396 | /* Tally up the load of all CPUs in the group */ |
2dd73a4f | 3397 | sum_weighted_load = sum_nr_running = avg_load = 0; |
908a7c1b KC |
3398 | max_cpu_load = 0; |
3399 | min_cpu_load = ~0UL; | |
1da177e4 LT |
3400 | |
3401 | for_each_cpu_mask(i, group->cpumask) { | |
0a2966b4 CL |
3402 | struct rq *rq; |
3403 | ||
3404 | if (!cpu_isset(i, *cpus)) | |
3405 | continue; | |
3406 | ||
3407 | rq = cpu_rq(i); | |
2dd73a4f | 3408 | |
9439aab8 | 3409 | if (*sd_idle && rq->nr_running) |
5969fe06 NP |
3410 | *sd_idle = 0; |
3411 | ||
1da177e4 | 3412 | /* Bias balancing toward cpus of our domain */ |
783609c6 SS |
3413 | if (local_group) { |
3414 | if (idle_cpu(i) && !first_idle_cpu) { | |
3415 | first_idle_cpu = 1; | |
3416 | balance_cpu = i; | |
3417 | } | |
3418 | ||
a2000572 | 3419 | load = target_load(i, load_idx); |
908a7c1b | 3420 | } else { |
a2000572 | 3421 | load = source_load(i, load_idx); |
908a7c1b KC |
3422 | if (load > max_cpu_load) |
3423 | max_cpu_load = load; | |
3424 | if (min_cpu_load > load) | |
3425 | min_cpu_load = load; | |
3426 | } | |
1da177e4 LT |
3427 | |
3428 | avg_load += load; | |
2dd73a4f | 3429 | sum_nr_running += rq->nr_running; |
dd41f596 | 3430 | sum_weighted_load += weighted_cpuload(i); |
1da177e4 LT |
3431 | } |
3432 | ||
783609c6 SS |
3433 | /* |
3434 | * First idle cpu or the first cpu(busiest) in this sched group | |
3435 | * is eligible for doing load balancing at this and above | |
9439aab8 SS |
3436 | * domains. In the newly idle case, we will allow all the cpu's |
3437 | * to do the newly idle load balance. | |
783609c6 | 3438 | */ |
9439aab8 SS |
3439 | if (idle != CPU_NEWLY_IDLE && local_group && |
3440 | balance_cpu != this_cpu && balance) { | |
783609c6 SS |
3441 | *balance = 0; |
3442 | goto ret; | |
3443 | } | |
3444 | ||
1da177e4 | 3445 | total_load += avg_load; |
5517d86b | 3446 | total_pwr += group->__cpu_power; |
1da177e4 LT |
3447 | |
3448 | /* Adjust by relative CPU power of the group */ | |
5517d86b ED |
3449 | avg_load = sg_div_cpu_power(group, |
3450 | avg_load * SCHED_LOAD_SCALE); | |
1da177e4 | 3451 | |
908a7c1b KC |
3452 | if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE) |
3453 | __group_imb = 1; | |
3454 | ||
5517d86b | 3455 | group_capacity = group->__cpu_power / SCHED_LOAD_SCALE; |
5c45bf27 | 3456 | |
1da177e4 LT |
3457 | if (local_group) { |
3458 | this_load = avg_load; | |
3459 | this = group; | |
2dd73a4f PW |
3460 | this_nr_running = sum_nr_running; |
3461 | this_load_per_task = sum_weighted_load; | |
3462 | } else if (avg_load > max_load && | |
908a7c1b | 3463 | (sum_nr_running > group_capacity || __group_imb)) { |
1da177e4 LT |
3464 | max_load = avg_load; |
3465 | busiest = group; | |
2dd73a4f PW |
3466 | busiest_nr_running = sum_nr_running; |
3467 | busiest_load_per_task = sum_weighted_load; | |
908a7c1b | 3468 | group_imb = __group_imb; |
1da177e4 | 3469 | } |
5c45bf27 SS |
3470 | |
3471 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) | |
3472 | /* | |
3473 | * Busy processors will not participate in power savings | |
3474 | * balance. | |
3475 | */ | |
dd41f596 IM |
3476 | if (idle == CPU_NOT_IDLE || |
3477 | !(sd->flags & SD_POWERSAVINGS_BALANCE)) | |
3478 | goto group_next; | |
5c45bf27 SS |
3479 | |
3480 | /* | |
3481 | * If the local group is idle or completely loaded | |
3482 | * no need to do power savings balance at this domain | |
3483 | */ | |
3484 | if (local_group && (this_nr_running >= group_capacity || | |
3485 | !this_nr_running)) | |
3486 | power_savings_balance = 0; | |
3487 | ||
dd41f596 | 3488 | /* |
5c45bf27 SS |
3489 | * If a group is already running at full capacity or idle, |
3490 | * don't include that group in power savings calculations | |
dd41f596 IM |
3491 | */ |
3492 | if (!power_savings_balance || sum_nr_running >= group_capacity | |
5c45bf27 | 3493 | || !sum_nr_running) |
dd41f596 | 3494 | goto group_next; |
5c45bf27 | 3495 | |
dd41f596 | 3496 | /* |
5c45bf27 | 3497 | * Calculate the group which has the least non-idle load. |
dd41f596 IM |
3498 | * This is the group from where we need to pick up the load |
3499 | * for saving power | |
3500 | */ | |
3501 | if ((sum_nr_running < min_nr_running) || | |
3502 | (sum_nr_running == min_nr_running && | |
5c45bf27 SS |
3503 | first_cpu(group->cpumask) < |
3504 | first_cpu(group_min->cpumask))) { | |
dd41f596 IM |
3505 | group_min = group; |
3506 | min_nr_running = sum_nr_running; | |
5c45bf27 SS |
3507 | min_load_per_task = sum_weighted_load / |
3508 | sum_nr_running; | |
dd41f596 | 3509 | } |
5c45bf27 | 3510 | |
dd41f596 | 3511 | /* |
5c45bf27 | 3512 | * Calculate the group which is almost near its |
dd41f596 IM |
3513 | * capacity but still has some space to pick up some load |
3514 | * from other group and save more power | |
3515 | */ | |
3516 | if (sum_nr_running <= group_capacity - 1) { | |
3517 | if (sum_nr_running > leader_nr_running || | |
3518 | (sum_nr_running == leader_nr_running && | |
3519 | first_cpu(group->cpumask) > | |
3520 | first_cpu(group_leader->cpumask))) { | |
3521 | group_leader = group; | |
3522 | leader_nr_running = sum_nr_running; | |
3523 | } | |
48f24c4d | 3524 | } |
5c45bf27 SS |
3525 | group_next: |
3526 | #endif | |
1da177e4 LT |
3527 | group = group->next; |
3528 | } while (group != sd->groups); | |
3529 | ||
2dd73a4f | 3530 | if (!busiest || this_load >= max_load || busiest_nr_running == 0) |
1da177e4 LT |
3531 | goto out_balanced; |
3532 | ||
3533 | avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr; | |
3534 | ||
3535 | if (this_load >= avg_load || | |
3536 | 100*max_load <= sd->imbalance_pct*this_load) | |
3537 | goto out_balanced; | |
3538 | ||
2dd73a4f | 3539 | busiest_load_per_task /= busiest_nr_running; |
908a7c1b KC |
3540 | if (group_imb) |
3541 | busiest_load_per_task = min(busiest_load_per_task, avg_load); | |
3542 | ||
1da177e4 LT |
3543 | /* |
3544 | * We're trying to get all the cpus to the average_load, so we don't | |
3545 | * want to push ourselves above the average load, nor do we wish to | |
3546 | * reduce the max loaded cpu below the average load, as either of these | |
3547 | * actions would just result in more rebalancing later, and ping-pong | |
3548 | * tasks around. Thus we look for the minimum possible imbalance. | |
3549 | * Negative imbalances (*we* are more loaded than anyone else) will | |
3550 | * be counted as no imbalance for these purposes -- we can't fix that | |
41a2d6cf | 3551 | * by pulling tasks to us. Be careful of negative numbers as they'll |
1da177e4 LT |
3552 | * appear as very large values with unsigned longs. |
3553 | */ | |
2dd73a4f PW |
3554 | if (max_load <= busiest_load_per_task) |
3555 | goto out_balanced; | |
3556 | ||
3557 | /* | |
3558 | * In the presence of smp nice balancing, certain scenarios can have | |
3559 | * max load less than avg load(as we skip the groups at or below | |
3560 | * its cpu_power, while calculating max_load..) | |
3561 | */ | |
3562 | if (max_load < avg_load) { | |
3563 | *imbalance = 0; | |
3564 | goto small_imbalance; | |
3565 | } | |
0c117f1b SS |
3566 | |
3567 | /* Don't want to pull so many tasks that a group would go idle */ | |
2dd73a4f | 3568 | max_pull = min(max_load - avg_load, max_load - busiest_load_per_task); |
0c117f1b | 3569 | |
1da177e4 | 3570 | /* How much load to actually move to equalise the imbalance */ |
5517d86b ED |
3571 | *imbalance = min(max_pull * busiest->__cpu_power, |
3572 | (avg_load - this_load) * this->__cpu_power) | |
1da177e4 LT |
3573 | / SCHED_LOAD_SCALE; |
3574 | ||
2dd73a4f PW |
3575 | /* |
3576 | * if *imbalance is less than the average load per runnable task | |
3577 | * there is no gaurantee that any tasks will be moved so we'll have | |
3578 | * a think about bumping its value to force at least one task to be | |
3579 | * moved | |
3580 | */ | |
7fd0d2dd | 3581 | if (*imbalance < busiest_load_per_task) { |
48f24c4d | 3582 | unsigned long tmp, pwr_now, pwr_move; |
2dd73a4f PW |
3583 | unsigned int imbn; |
3584 | ||
3585 | small_imbalance: | |
3586 | pwr_move = pwr_now = 0; | |
3587 | imbn = 2; | |
3588 | if (this_nr_running) { | |
3589 | this_load_per_task /= this_nr_running; | |
3590 | if (busiest_load_per_task > this_load_per_task) | |
3591 | imbn = 1; | |
3592 | } else | |
3593 | this_load_per_task = SCHED_LOAD_SCALE; | |
1da177e4 | 3594 | |
dd41f596 IM |
3595 | if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >= |
3596 | busiest_load_per_task * imbn) { | |
2dd73a4f | 3597 | *imbalance = busiest_load_per_task; |
1da177e4 LT |
3598 | return busiest; |
3599 | } | |
3600 | ||
3601 | /* | |
3602 | * OK, we don't have enough imbalance to justify moving tasks, | |
3603 | * however we may be able to increase total CPU power used by | |
3604 | * moving them. | |
3605 | */ | |
3606 | ||
5517d86b ED |
3607 | pwr_now += busiest->__cpu_power * |
3608 | min(busiest_load_per_task, max_load); | |
3609 | pwr_now += this->__cpu_power * | |
3610 | min(this_load_per_task, this_load); | |
1da177e4 LT |
3611 | pwr_now /= SCHED_LOAD_SCALE; |
3612 | ||
3613 | /* Amount of load we'd subtract */ | |
5517d86b ED |
3614 | tmp = sg_div_cpu_power(busiest, |
3615 | busiest_load_per_task * SCHED_LOAD_SCALE); | |
1da177e4 | 3616 | if (max_load > tmp) |
5517d86b | 3617 | pwr_move += busiest->__cpu_power * |
2dd73a4f | 3618 | min(busiest_load_per_task, max_load - tmp); |
1da177e4 LT |
3619 | |
3620 | /* Amount of load we'd add */ | |
5517d86b | 3621 | if (max_load * busiest->__cpu_power < |
33859f7f | 3622 | busiest_load_per_task * SCHED_LOAD_SCALE) |
5517d86b ED |
3623 | tmp = sg_div_cpu_power(this, |
3624 | max_load * busiest->__cpu_power); | |
1da177e4 | 3625 | else |
5517d86b ED |
3626 | tmp = sg_div_cpu_power(this, |
3627 | busiest_load_per_task * SCHED_LOAD_SCALE); | |
3628 | pwr_move += this->__cpu_power * | |
3629 | min(this_load_per_task, this_load + tmp); | |
1da177e4 LT |
3630 | pwr_move /= SCHED_LOAD_SCALE; |
3631 | ||
3632 | /* Move if we gain throughput */ | |
7fd0d2dd SS |
3633 | if (pwr_move > pwr_now) |
3634 | *imbalance = busiest_load_per_task; | |
1da177e4 LT |
3635 | } |
3636 | ||
1da177e4 LT |
3637 | return busiest; |
3638 | ||
3639 | out_balanced: | |
5c45bf27 | 3640 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
d15bcfdb | 3641 | if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE)) |
5c45bf27 | 3642 | goto ret; |
1da177e4 | 3643 | |
5c45bf27 SS |
3644 | if (this == group_leader && group_leader != group_min) { |
3645 | *imbalance = min_load_per_task; | |
3646 | return group_min; | |
3647 | } | |
5c45bf27 | 3648 | #endif |
783609c6 | 3649 | ret: |
1da177e4 LT |
3650 | *imbalance = 0; |
3651 | return NULL; | |
3652 | } | |
3653 | ||
3654 | /* | |
3655 | * find_busiest_queue - find the busiest runqueue among the cpus in group. | |
3656 | */ | |
70b97a7f | 3657 | static struct rq * |
d15bcfdb | 3658 | find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle, |
7c16ec58 | 3659 | unsigned long imbalance, const cpumask_t *cpus) |
1da177e4 | 3660 | { |
70b97a7f | 3661 | struct rq *busiest = NULL, *rq; |
2dd73a4f | 3662 | unsigned long max_load = 0; |
1da177e4 LT |
3663 | int i; |
3664 | ||
3665 | for_each_cpu_mask(i, group->cpumask) { | |
dd41f596 | 3666 | unsigned long wl; |
0a2966b4 CL |
3667 | |
3668 | if (!cpu_isset(i, *cpus)) | |
3669 | continue; | |
3670 | ||
48f24c4d | 3671 | rq = cpu_rq(i); |
dd41f596 | 3672 | wl = weighted_cpuload(i); |
2dd73a4f | 3673 | |
dd41f596 | 3674 | if (rq->nr_running == 1 && wl > imbalance) |
2dd73a4f | 3675 | continue; |
1da177e4 | 3676 | |
dd41f596 IM |
3677 | if (wl > max_load) { |
3678 | max_load = wl; | |
48f24c4d | 3679 | busiest = rq; |
1da177e4 LT |
3680 | } |
3681 | } | |
3682 | ||
3683 | return busiest; | |
3684 | } | |
3685 | ||
77391d71 NP |
3686 | /* |
3687 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but | |
3688 | * so long as it is large enough. | |
3689 | */ | |
3690 | #define MAX_PINNED_INTERVAL 512 | |
3691 | ||
1da177e4 LT |
3692 | /* |
3693 | * Check this_cpu to ensure it is balanced within domain. Attempt to move | |
3694 | * tasks if there is an imbalance. | |
1da177e4 | 3695 | */ |
70b97a7f | 3696 | static int load_balance(int this_cpu, struct rq *this_rq, |
d15bcfdb | 3697 | struct sched_domain *sd, enum cpu_idle_type idle, |
7c16ec58 | 3698 | int *balance, cpumask_t *cpus) |
1da177e4 | 3699 | { |
43010659 | 3700 | int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0; |
1da177e4 | 3701 | struct sched_group *group; |
1da177e4 | 3702 | unsigned long imbalance; |
70b97a7f | 3703 | struct rq *busiest; |
fe2eea3f | 3704 | unsigned long flags; |
18d95a28 | 3705 | int unlock_aggregate; |
5969fe06 | 3706 | |
7c16ec58 MT |
3707 | cpus_setall(*cpus); |
3708 | ||
18d95a28 PZ |
3709 | unlock_aggregate = get_aggregate(sd); |
3710 | ||
89c4710e SS |
3711 | /* |
3712 | * When power savings policy is enabled for the parent domain, idle | |
3713 | * sibling can pick up load irrespective of busy siblings. In this case, | |
dd41f596 | 3714 | * let the state of idle sibling percolate up as CPU_IDLE, instead of |
d15bcfdb | 3715 | * portraying it as CPU_NOT_IDLE. |
89c4710e | 3716 | */ |
d15bcfdb | 3717 | if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER && |
89c4710e | 3718 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
5969fe06 | 3719 | sd_idle = 1; |
1da177e4 | 3720 | |
2d72376b | 3721 | schedstat_inc(sd, lb_count[idle]); |
1da177e4 | 3722 | |
0a2966b4 CL |
3723 | redo: |
3724 | group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle, | |
7c16ec58 | 3725 | cpus, balance); |
783609c6 | 3726 | |
06066714 | 3727 | if (*balance == 0) |
783609c6 | 3728 | goto out_balanced; |
783609c6 | 3729 | |
1da177e4 LT |
3730 | if (!group) { |
3731 | schedstat_inc(sd, lb_nobusyg[idle]); | |
3732 | goto out_balanced; | |
3733 | } | |
3734 | ||
7c16ec58 | 3735 | busiest = find_busiest_queue(group, idle, imbalance, cpus); |
1da177e4 LT |
3736 | if (!busiest) { |
3737 | schedstat_inc(sd, lb_nobusyq[idle]); | |
3738 | goto out_balanced; | |
3739 | } | |
3740 | ||
db935dbd | 3741 | BUG_ON(busiest == this_rq); |
1da177e4 LT |
3742 | |
3743 | schedstat_add(sd, lb_imbalance[idle], imbalance); | |
3744 | ||
43010659 | 3745 | ld_moved = 0; |
1da177e4 LT |
3746 | if (busiest->nr_running > 1) { |
3747 | /* | |
3748 | * Attempt to move tasks. If find_busiest_group has found | |
3749 | * an imbalance but busiest->nr_running <= 1, the group is | |
43010659 | 3750 | * still unbalanced. ld_moved simply stays zero, so it is |
1da177e4 LT |
3751 | * correctly treated as an imbalance. |
3752 | */ | |
fe2eea3f | 3753 | local_irq_save(flags); |
e17224bf | 3754 | double_rq_lock(this_rq, busiest); |
43010659 | 3755 | ld_moved = move_tasks(this_rq, this_cpu, busiest, |
48f24c4d | 3756 | imbalance, sd, idle, &all_pinned); |
e17224bf | 3757 | double_rq_unlock(this_rq, busiest); |
fe2eea3f | 3758 | local_irq_restore(flags); |
81026794 | 3759 | |
46cb4b7c SS |
3760 | /* |
3761 | * some other cpu did the load balance for us. | |
3762 | */ | |
43010659 | 3763 | if (ld_moved && this_cpu != smp_processor_id()) |
46cb4b7c SS |
3764 | resched_cpu(this_cpu); |
3765 | ||
81026794 | 3766 | /* All tasks on this runqueue were pinned by CPU affinity */ |
0a2966b4 | 3767 | if (unlikely(all_pinned)) { |
7c16ec58 MT |
3768 | cpu_clear(cpu_of(busiest), *cpus); |
3769 | if (!cpus_empty(*cpus)) | |
0a2966b4 | 3770 | goto redo; |
81026794 | 3771 | goto out_balanced; |
0a2966b4 | 3772 | } |
1da177e4 | 3773 | } |
81026794 | 3774 | |
43010659 | 3775 | if (!ld_moved) { |
1da177e4 LT |
3776 | schedstat_inc(sd, lb_failed[idle]); |
3777 | sd->nr_balance_failed++; | |
3778 | ||
3779 | if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) { | |
1da177e4 | 3780 | |
fe2eea3f | 3781 | spin_lock_irqsave(&busiest->lock, flags); |
fa3b6ddc SS |
3782 | |
3783 | /* don't kick the migration_thread, if the curr | |
3784 | * task on busiest cpu can't be moved to this_cpu | |
3785 | */ | |
3786 | if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) { | |
fe2eea3f | 3787 | spin_unlock_irqrestore(&busiest->lock, flags); |
fa3b6ddc SS |
3788 | all_pinned = 1; |
3789 | goto out_one_pinned; | |
3790 | } | |
3791 | ||
1da177e4 LT |
3792 | if (!busiest->active_balance) { |
3793 | busiest->active_balance = 1; | |
3794 | busiest->push_cpu = this_cpu; | |
81026794 | 3795 | active_balance = 1; |
1da177e4 | 3796 | } |
fe2eea3f | 3797 | spin_unlock_irqrestore(&busiest->lock, flags); |
81026794 | 3798 | if (active_balance) |
1da177e4 LT |
3799 | wake_up_process(busiest->migration_thread); |
3800 | ||
3801 | /* | |
3802 | * We've kicked active balancing, reset the failure | |
3803 | * counter. | |
3804 | */ | |
39507451 | 3805 | sd->nr_balance_failed = sd->cache_nice_tries+1; |
1da177e4 | 3806 | } |
81026794 | 3807 | } else |
1da177e4 LT |
3808 | sd->nr_balance_failed = 0; |
3809 | ||
81026794 | 3810 | if (likely(!active_balance)) { |
1da177e4 LT |
3811 | /* We were unbalanced, so reset the balancing interval */ |
3812 | sd->balance_interval = sd->min_interval; | |
81026794 NP |
3813 | } else { |
3814 | /* | |
3815 | * If we've begun active balancing, start to back off. This | |
3816 | * case may not be covered by the all_pinned logic if there | |
3817 | * is only 1 task on the busy runqueue (because we don't call | |
3818 | * move_tasks). | |
3819 | */ | |
3820 | if (sd->balance_interval < sd->max_interval) | |
3821 | sd->balance_interval *= 2; | |
1da177e4 LT |
3822 | } |
3823 | ||
43010659 | 3824 | if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
89c4710e | 3825 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
18d95a28 PZ |
3826 | ld_moved = -1; |
3827 | ||
3828 | goto out; | |
1da177e4 LT |
3829 | |
3830 | out_balanced: | |
1da177e4 LT |
3831 | schedstat_inc(sd, lb_balanced[idle]); |
3832 | ||
16cfb1c0 | 3833 | sd->nr_balance_failed = 0; |
fa3b6ddc SS |
3834 | |
3835 | out_one_pinned: | |
1da177e4 | 3836 | /* tune up the balancing interval */ |
77391d71 NP |
3837 | if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) || |
3838 | (sd->balance_interval < sd->max_interval)) | |
1da177e4 LT |
3839 | sd->balance_interval *= 2; |
3840 | ||
48f24c4d | 3841 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
89c4710e | 3842 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
18d95a28 PZ |
3843 | ld_moved = -1; |
3844 | else | |
3845 | ld_moved = 0; | |
3846 | out: | |
3847 | if (unlock_aggregate) | |
3848 | put_aggregate(sd); | |
3849 | return ld_moved; | |
1da177e4 LT |
3850 | } |
3851 | ||
3852 | /* | |
3853 | * Check this_cpu to ensure it is balanced within domain. Attempt to move | |
3854 | * tasks if there is an imbalance. | |
3855 | * | |
d15bcfdb | 3856 | * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE). |
1da177e4 LT |
3857 | * this_rq is locked. |
3858 | */ | |
48f24c4d | 3859 | static int |
7c16ec58 MT |
3860 | load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd, |
3861 | cpumask_t *cpus) | |
1da177e4 LT |
3862 | { |
3863 | struct sched_group *group; | |
70b97a7f | 3864 | struct rq *busiest = NULL; |
1da177e4 | 3865 | unsigned long imbalance; |
43010659 | 3866 | int ld_moved = 0; |
5969fe06 | 3867 | int sd_idle = 0; |
969bb4e4 | 3868 | int all_pinned = 0; |
7c16ec58 MT |
3869 | |
3870 | cpus_setall(*cpus); | |
5969fe06 | 3871 | |
89c4710e SS |
3872 | /* |
3873 | * When power savings policy is enabled for the parent domain, idle | |
3874 | * sibling can pick up load irrespective of busy siblings. In this case, | |
3875 | * let the state of idle sibling percolate up as IDLE, instead of | |
d15bcfdb | 3876 | * portraying it as CPU_NOT_IDLE. |
89c4710e SS |
3877 | */ |
3878 | if (sd->flags & SD_SHARE_CPUPOWER && | |
3879 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) | |
5969fe06 | 3880 | sd_idle = 1; |
1da177e4 | 3881 | |
2d72376b | 3882 | schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]); |
0a2966b4 | 3883 | redo: |
d15bcfdb | 3884 | group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE, |
7c16ec58 | 3885 | &sd_idle, cpus, NULL); |
1da177e4 | 3886 | if (!group) { |
d15bcfdb | 3887 | schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]); |
16cfb1c0 | 3888 | goto out_balanced; |
1da177e4 LT |
3889 | } |
3890 | ||
7c16ec58 | 3891 | busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus); |
db935dbd | 3892 | if (!busiest) { |
d15bcfdb | 3893 | schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]); |
16cfb1c0 | 3894 | goto out_balanced; |
1da177e4 LT |
3895 | } |
3896 | ||
db935dbd NP |
3897 | BUG_ON(busiest == this_rq); |
3898 | ||
d15bcfdb | 3899 | schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance); |
d6d5cfaf | 3900 | |
43010659 | 3901 | ld_moved = 0; |
d6d5cfaf NP |
3902 | if (busiest->nr_running > 1) { |
3903 | /* Attempt to move tasks */ | |
3904 | double_lock_balance(this_rq, busiest); | |
6e82a3be IM |
3905 | /* this_rq->clock is already updated */ |
3906 | update_rq_clock(busiest); | |
43010659 | 3907 | ld_moved = move_tasks(this_rq, this_cpu, busiest, |
969bb4e4 SS |
3908 | imbalance, sd, CPU_NEWLY_IDLE, |
3909 | &all_pinned); | |
d6d5cfaf | 3910 | spin_unlock(&busiest->lock); |
0a2966b4 | 3911 | |
969bb4e4 | 3912 | if (unlikely(all_pinned)) { |
7c16ec58 MT |
3913 | cpu_clear(cpu_of(busiest), *cpus); |
3914 | if (!cpus_empty(*cpus)) | |
0a2966b4 CL |
3915 | goto redo; |
3916 | } | |
d6d5cfaf NP |
3917 | } |
3918 | ||
43010659 | 3919 | if (!ld_moved) { |
d15bcfdb | 3920 | schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]); |
89c4710e SS |
3921 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
3922 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) | |
5969fe06 NP |
3923 | return -1; |
3924 | } else | |
16cfb1c0 | 3925 | sd->nr_balance_failed = 0; |
1da177e4 | 3926 | |
43010659 | 3927 | return ld_moved; |
16cfb1c0 NP |
3928 | |
3929 | out_balanced: | |
d15bcfdb | 3930 | schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]); |
48f24c4d | 3931 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
89c4710e | 3932 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
5969fe06 | 3933 | return -1; |
16cfb1c0 | 3934 | sd->nr_balance_failed = 0; |
48f24c4d | 3935 | |
16cfb1c0 | 3936 | return 0; |
1da177e4 LT |
3937 | } |
3938 | ||
3939 | /* | |
3940 | * idle_balance is called by schedule() if this_cpu is about to become | |
3941 | * idle. Attempts to pull tasks from other CPUs. | |
3942 | */ | |
70b97a7f | 3943 | static void idle_balance(int this_cpu, struct rq *this_rq) |
1da177e4 LT |
3944 | { |
3945 | struct sched_domain *sd; | |
dd41f596 IM |
3946 | int pulled_task = -1; |
3947 | unsigned long next_balance = jiffies + HZ; | |
7c16ec58 | 3948 | cpumask_t tmpmask; |
1da177e4 LT |
3949 | |
3950 | for_each_domain(this_cpu, sd) { | |
92c4ca5c CL |
3951 | unsigned long interval; |
3952 | ||
3953 | if (!(sd->flags & SD_LOAD_BALANCE)) | |
3954 | continue; | |
3955 | ||
3956 | if (sd->flags & SD_BALANCE_NEWIDLE) | |
48f24c4d | 3957 | /* If we've pulled tasks over stop searching: */ |
7c16ec58 MT |
3958 | pulled_task = load_balance_newidle(this_cpu, this_rq, |
3959 | sd, &tmpmask); | |
92c4ca5c CL |
3960 | |
3961 | interval = msecs_to_jiffies(sd->balance_interval); | |
3962 | if (time_after(next_balance, sd->last_balance + interval)) | |
3963 | next_balance = sd->last_balance + interval; | |
3964 | if (pulled_task) | |
3965 | break; | |
1da177e4 | 3966 | } |
dd41f596 | 3967 | if (pulled_task || time_after(jiffies, this_rq->next_balance)) { |
1bd77f2d CL |
3968 | /* |
3969 | * We are going idle. next_balance may be set based on | |
3970 | * a busy processor. So reset next_balance. | |
3971 | */ | |
3972 | this_rq->next_balance = next_balance; | |
dd41f596 | 3973 | } |
1da177e4 LT |
3974 | } |
3975 | ||
3976 | /* | |
3977 | * active_load_balance is run by migration threads. It pushes running tasks | |
3978 | * off the busiest CPU onto idle CPUs. It requires at least 1 task to be | |
3979 | * running on each physical CPU where possible, and avoids physical / | |
3980 | * logical imbalances. | |
3981 | * | |
3982 | * Called with busiest_rq locked. | |
3983 | */ | |
70b97a7f | 3984 | static void active_load_balance(struct rq *busiest_rq, int busiest_cpu) |
1da177e4 | 3985 | { |
39507451 | 3986 | int target_cpu = busiest_rq->push_cpu; |
70b97a7f IM |
3987 | struct sched_domain *sd; |
3988 | struct rq *target_rq; | |
39507451 | 3989 | |
48f24c4d | 3990 | /* Is there any task to move? */ |
39507451 | 3991 | if (busiest_rq->nr_running <= 1) |
39507451 NP |
3992 | return; |
3993 | ||
3994 | target_rq = cpu_rq(target_cpu); | |
1da177e4 LT |
3995 | |
3996 | /* | |
39507451 | 3997 | * This condition is "impossible", if it occurs |
41a2d6cf | 3998 | * we need to fix it. Originally reported by |
39507451 | 3999 | * Bjorn Helgaas on a 128-cpu setup. |
1da177e4 | 4000 | */ |
39507451 | 4001 | BUG_ON(busiest_rq == target_rq); |
1da177e4 | 4002 | |
39507451 NP |
4003 | /* move a task from busiest_rq to target_rq */ |
4004 | double_lock_balance(busiest_rq, target_rq); | |
6e82a3be IM |
4005 | update_rq_clock(busiest_rq); |
4006 | update_rq_clock(target_rq); | |
39507451 NP |
4007 | |
4008 | /* Search for an sd spanning us and the target CPU. */ | |
c96d145e | 4009 | for_each_domain(target_cpu, sd) { |
39507451 | 4010 | if ((sd->flags & SD_LOAD_BALANCE) && |
48f24c4d | 4011 | cpu_isset(busiest_cpu, sd->span)) |
39507451 | 4012 | break; |
c96d145e | 4013 | } |
39507451 | 4014 | |
48f24c4d | 4015 | if (likely(sd)) { |
2d72376b | 4016 | schedstat_inc(sd, alb_count); |
39507451 | 4017 | |
43010659 PW |
4018 | if (move_one_task(target_rq, target_cpu, busiest_rq, |
4019 | sd, CPU_IDLE)) | |
48f24c4d IM |
4020 | schedstat_inc(sd, alb_pushed); |
4021 | else | |
4022 | schedstat_inc(sd, alb_failed); | |
4023 | } | |
39507451 | 4024 | spin_unlock(&target_rq->lock); |
1da177e4 LT |
4025 | } |
4026 | ||
46cb4b7c SS |
4027 | #ifdef CONFIG_NO_HZ |
4028 | static struct { | |
4029 | atomic_t load_balancer; | |
41a2d6cf | 4030 | cpumask_t cpu_mask; |
46cb4b7c SS |
4031 | } nohz ____cacheline_aligned = { |
4032 | .load_balancer = ATOMIC_INIT(-1), | |
4033 | .cpu_mask = CPU_MASK_NONE, | |
4034 | }; | |
4035 | ||
7835b98b | 4036 | /* |
46cb4b7c SS |
4037 | * This routine will try to nominate the ilb (idle load balancing) |
4038 | * owner among the cpus whose ticks are stopped. ilb owner will do the idle | |
4039 | * load balancing on behalf of all those cpus. If all the cpus in the system | |
4040 | * go into this tickless mode, then there will be no ilb owner (as there is | |
4041 | * no need for one) and all the cpus will sleep till the next wakeup event | |
4042 | * arrives... | |
4043 | * | |
4044 | * For the ilb owner, tick is not stopped. And this tick will be used | |
4045 | * for idle load balancing. ilb owner will still be part of | |
4046 | * nohz.cpu_mask.. | |
7835b98b | 4047 | * |
46cb4b7c SS |
4048 | * While stopping the tick, this cpu will become the ilb owner if there |
4049 | * is no other owner. And will be the owner till that cpu becomes busy | |
4050 | * or if all cpus in the system stop their ticks at which point | |
4051 | * there is no need for ilb owner. | |
4052 | * | |
4053 | * When the ilb owner becomes busy, it nominates another owner, during the | |
4054 | * next busy scheduler_tick() | |
4055 | */ | |
4056 | int select_nohz_load_balancer(int stop_tick) | |
4057 | { | |
4058 | int cpu = smp_processor_id(); | |
4059 | ||
4060 | if (stop_tick) { | |
4061 | cpu_set(cpu, nohz.cpu_mask); | |
4062 | cpu_rq(cpu)->in_nohz_recently = 1; | |
4063 | ||
4064 | /* | |
4065 | * If we are going offline and still the leader, give up! | |
4066 | */ | |
4067 | if (cpu_is_offline(cpu) && | |
4068 | atomic_read(&nohz.load_balancer) == cpu) { | |
4069 | if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu) | |
4070 | BUG(); | |
4071 | return 0; | |
4072 | } | |
4073 | ||
4074 | /* time for ilb owner also to sleep */ | |
4075 | if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) { | |
4076 | if (atomic_read(&nohz.load_balancer) == cpu) | |
4077 | atomic_set(&nohz.load_balancer, -1); | |
4078 | return 0; | |
4079 | } | |
4080 | ||
4081 | if (atomic_read(&nohz.load_balancer) == -1) { | |
4082 | /* make me the ilb owner */ | |
4083 | if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1) | |
4084 | return 1; | |
4085 | } else if (atomic_read(&nohz.load_balancer) == cpu) | |
4086 | return 1; | |
4087 | } else { | |
4088 | if (!cpu_isset(cpu, nohz.cpu_mask)) | |
4089 | return 0; | |
4090 | ||
4091 | cpu_clear(cpu, nohz.cpu_mask); | |
4092 | ||
4093 | if (atomic_read(&nohz.load_balancer) == cpu) | |
4094 | if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu) | |
4095 | BUG(); | |
4096 | } | |
4097 | return 0; | |
4098 | } | |
4099 | #endif | |
4100 | ||
4101 | static DEFINE_SPINLOCK(balancing); | |
4102 | ||
4103 | /* | |
7835b98b CL |
4104 | * It checks each scheduling domain to see if it is due to be balanced, |
4105 | * and initiates a balancing operation if so. | |
4106 | * | |
4107 | * Balancing parameters are set up in arch_init_sched_domains. | |
4108 | */ | |
a9957449 | 4109 | static void rebalance_domains(int cpu, enum cpu_idle_type idle) |
7835b98b | 4110 | { |
46cb4b7c SS |
4111 | int balance = 1; |
4112 | struct rq *rq = cpu_rq(cpu); | |
7835b98b CL |
4113 | unsigned long interval; |
4114 | struct sched_domain *sd; | |
46cb4b7c | 4115 | /* Earliest time when we have to do rebalance again */ |
c9819f45 | 4116 | unsigned long next_balance = jiffies + 60*HZ; |
f549da84 | 4117 | int update_next_balance = 0; |
7c16ec58 | 4118 | cpumask_t tmp; |
1da177e4 | 4119 | |
46cb4b7c | 4120 | for_each_domain(cpu, sd) { |
1da177e4 LT |
4121 | if (!(sd->flags & SD_LOAD_BALANCE)) |
4122 | continue; | |
4123 | ||
4124 | interval = sd->balance_interval; | |
d15bcfdb | 4125 | if (idle != CPU_IDLE) |
1da177e4 LT |
4126 | interval *= sd->busy_factor; |
4127 | ||
4128 | /* scale ms to jiffies */ | |
4129 | interval = msecs_to_jiffies(interval); | |
4130 | if (unlikely(!interval)) | |
4131 | interval = 1; | |
dd41f596 IM |
4132 | if (interval > HZ*NR_CPUS/10) |
4133 | interval = HZ*NR_CPUS/10; | |
4134 | ||
1da177e4 | 4135 | |
08c183f3 CL |
4136 | if (sd->flags & SD_SERIALIZE) { |
4137 | if (!spin_trylock(&balancing)) | |
4138 | goto out; | |
4139 | } | |
4140 | ||
c9819f45 | 4141 | if (time_after_eq(jiffies, sd->last_balance + interval)) { |
7c16ec58 | 4142 | if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) { |
fa3b6ddc SS |
4143 | /* |
4144 | * We've pulled tasks over so either we're no | |
5969fe06 NP |
4145 | * longer idle, or one of our SMT siblings is |
4146 | * not idle. | |
4147 | */ | |
d15bcfdb | 4148 | idle = CPU_NOT_IDLE; |
1da177e4 | 4149 | } |
1bd77f2d | 4150 | sd->last_balance = jiffies; |
1da177e4 | 4151 | } |
08c183f3 CL |
4152 | if (sd->flags & SD_SERIALIZE) |
4153 | spin_unlock(&balancing); | |
4154 | out: | |
f549da84 | 4155 | if (time_after(next_balance, sd->last_balance + interval)) { |
c9819f45 | 4156 | next_balance = sd->last_balance + interval; |
f549da84 SS |
4157 | update_next_balance = 1; |
4158 | } | |
783609c6 SS |
4159 | |
4160 | /* | |
4161 | * Stop the load balance at this level. There is another | |
4162 | * CPU in our sched group which is doing load balancing more | |
4163 | * actively. | |
4164 | */ | |
4165 | if (!balance) | |
4166 | break; | |
1da177e4 | 4167 | } |
f549da84 SS |
4168 | |
4169 | /* | |
4170 | * next_balance will be updated only when there is a need. | |
4171 | * When the cpu is attached to null domain for ex, it will not be | |
4172 | * updated. | |
4173 | */ | |
4174 | if (likely(update_next_balance)) | |
4175 | rq->next_balance = next_balance; | |
46cb4b7c SS |
4176 | } |
4177 | ||
4178 | /* | |
4179 | * run_rebalance_domains is triggered when needed from the scheduler tick. | |
4180 | * In CONFIG_NO_HZ case, the idle load balance owner will do the | |
4181 | * rebalancing for all the cpus for whom scheduler ticks are stopped. | |
4182 | */ | |
4183 | static void run_rebalance_domains(struct softirq_action *h) | |
4184 | { | |
dd41f596 IM |
4185 | int this_cpu = smp_processor_id(); |
4186 | struct rq *this_rq = cpu_rq(this_cpu); | |
4187 | enum cpu_idle_type idle = this_rq->idle_at_tick ? | |
4188 | CPU_IDLE : CPU_NOT_IDLE; | |
46cb4b7c | 4189 | |
dd41f596 | 4190 | rebalance_domains(this_cpu, idle); |
46cb4b7c SS |
4191 | |
4192 | #ifdef CONFIG_NO_HZ | |
4193 | /* | |
4194 | * If this cpu is the owner for idle load balancing, then do the | |
4195 | * balancing on behalf of the other idle cpus whose ticks are | |
4196 | * stopped. | |
4197 | */ | |
dd41f596 IM |
4198 | if (this_rq->idle_at_tick && |
4199 | atomic_read(&nohz.load_balancer) == this_cpu) { | |
46cb4b7c SS |
4200 | cpumask_t cpus = nohz.cpu_mask; |
4201 | struct rq *rq; | |
4202 | int balance_cpu; | |
4203 | ||
dd41f596 | 4204 | cpu_clear(this_cpu, cpus); |
46cb4b7c SS |
4205 | for_each_cpu_mask(balance_cpu, cpus) { |
4206 | /* | |
4207 | * If this cpu gets work to do, stop the load balancing | |
4208 | * work being done for other cpus. Next load | |
4209 | * balancing owner will pick it up. | |
4210 | */ | |
4211 | if (need_resched()) | |
4212 | break; | |
4213 | ||
de0cf899 | 4214 | rebalance_domains(balance_cpu, CPU_IDLE); |
46cb4b7c SS |
4215 | |
4216 | rq = cpu_rq(balance_cpu); | |
dd41f596 IM |
4217 | if (time_after(this_rq->next_balance, rq->next_balance)) |
4218 | this_rq->next_balance = rq->next_balance; | |
46cb4b7c SS |
4219 | } |
4220 | } | |
4221 | #endif | |
4222 | } | |
4223 | ||
4224 | /* | |
4225 | * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing. | |
4226 | * | |
4227 | * In case of CONFIG_NO_HZ, this is the place where we nominate a new | |
4228 | * idle load balancing owner or decide to stop the periodic load balancing, | |
4229 | * if the whole system is idle. | |
4230 | */ | |
dd41f596 | 4231 | static inline void trigger_load_balance(struct rq *rq, int cpu) |
46cb4b7c | 4232 | { |
46cb4b7c SS |
4233 | #ifdef CONFIG_NO_HZ |
4234 | /* | |
4235 | * If we were in the nohz mode recently and busy at the current | |
4236 | * scheduler tick, then check if we need to nominate new idle | |
4237 | * load balancer. | |
4238 | */ | |
4239 | if (rq->in_nohz_recently && !rq->idle_at_tick) { | |
4240 | rq->in_nohz_recently = 0; | |
4241 | ||
4242 | if (atomic_read(&nohz.load_balancer) == cpu) { | |
4243 | cpu_clear(cpu, nohz.cpu_mask); | |
4244 | atomic_set(&nohz.load_balancer, -1); | |
4245 | } | |
4246 | ||
4247 | if (atomic_read(&nohz.load_balancer) == -1) { | |
4248 | /* | |
4249 | * simple selection for now: Nominate the | |
4250 | * first cpu in the nohz list to be the next | |
4251 | * ilb owner. | |
4252 | * | |
4253 | * TBD: Traverse the sched domains and nominate | |
4254 | * the nearest cpu in the nohz.cpu_mask. | |
4255 | */ | |
4256 | int ilb = first_cpu(nohz.cpu_mask); | |
4257 | ||
434d53b0 | 4258 | if (ilb < nr_cpu_ids) |
46cb4b7c SS |
4259 | resched_cpu(ilb); |
4260 | } | |
4261 | } | |
4262 | ||
4263 | /* | |
4264 | * If this cpu is idle and doing idle load balancing for all the | |
4265 | * cpus with ticks stopped, is it time for that to stop? | |
4266 | */ | |
4267 | if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu && | |
4268 | cpus_weight(nohz.cpu_mask) == num_online_cpus()) { | |
4269 | resched_cpu(cpu); | |
4270 | return; | |
4271 | } | |
4272 | ||
4273 | /* | |
4274 | * If this cpu is idle and the idle load balancing is done by | |
4275 | * someone else, then no need raise the SCHED_SOFTIRQ | |
4276 | */ | |
4277 | if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu && | |
4278 | cpu_isset(cpu, nohz.cpu_mask)) | |
4279 | return; | |
4280 | #endif | |
4281 | if (time_after_eq(jiffies, rq->next_balance)) | |
4282 | raise_softirq(SCHED_SOFTIRQ); | |
1da177e4 | 4283 | } |
dd41f596 IM |
4284 | |
4285 | #else /* CONFIG_SMP */ | |
4286 | ||
1da177e4 LT |
4287 | /* |
4288 | * on UP we do not need to balance between CPUs: | |
4289 | */ | |
70b97a7f | 4290 | static inline void idle_balance(int cpu, struct rq *rq) |
1da177e4 LT |
4291 | { |
4292 | } | |
dd41f596 | 4293 | |
1da177e4 LT |
4294 | #endif |
4295 | ||
1da177e4 LT |
4296 | DEFINE_PER_CPU(struct kernel_stat, kstat); |
4297 | ||
4298 | EXPORT_PER_CPU_SYMBOL(kstat); | |
4299 | ||
4300 | /* | |
41b86e9c IM |
4301 | * Return p->sum_exec_runtime plus any more ns on the sched_clock |
4302 | * that have not yet been banked in case the task is currently running. | |
1da177e4 | 4303 | */ |
41b86e9c | 4304 | unsigned long long task_sched_runtime(struct task_struct *p) |
1da177e4 | 4305 | { |
1da177e4 | 4306 | unsigned long flags; |
41b86e9c IM |
4307 | u64 ns, delta_exec; |
4308 | struct rq *rq; | |
48f24c4d | 4309 | |
41b86e9c IM |
4310 | rq = task_rq_lock(p, &flags); |
4311 | ns = p->se.sum_exec_runtime; | |
051a1d1a | 4312 | if (task_current(rq, p)) { |
a8e504d2 IM |
4313 | update_rq_clock(rq); |
4314 | delta_exec = rq->clock - p->se.exec_start; | |
41b86e9c IM |
4315 | if ((s64)delta_exec > 0) |
4316 | ns += delta_exec; | |
4317 | } | |
4318 | task_rq_unlock(rq, &flags); | |
48f24c4d | 4319 | |
1da177e4 LT |
4320 | return ns; |
4321 | } | |
4322 | ||
1da177e4 LT |
4323 | /* |
4324 | * Account user cpu time to a process. | |
4325 | * @p: the process that the cpu time gets accounted to | |
1da177e4 LT |
4326 | * @cputime: the cpu time spent in user space since the last update |
4327 | */ | |
4328 | void account_user_time(struct task_struct *p, cputime_t cputime) | |
4329 | { | |
4330 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | |
4331 | cputime64_t tmp; | |
4332 | ||
4333 | p->utime = cputime_add(p->utime, cputime); | |
4334 | ||
4335 | /* Add user time to cpustat. */ | |
4336 | tmp = cputime_to_cputime64(cputime); | |
4337 | if (TASK_NICE(p) > 0) | |
4338 | cpustat->nice = cputime64_add(cpustat->nice, tmp); | |
4339 | else | |
4340 | cpustat->user = cputime64_add(cpustat->user, tmp); | |
4341 | } | |
4342 | ||
94886b84 LV |
4343 | /* |
4344 | * Account guest cpu time to a process. | |
4345 | * @p: the process that the cpu time gets accounted to | |
4346 | * @cputime: the cpu time spent in virtual machine since the last update | |
4347 | */ | |
f7402e03 | 4348 | static void account_guest_time(struct task_struct *p, cputime_t cputime) |
94886b84 LV |
4349 | { |
4350 | cputime64_t tmp; | |
4351 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | |
4352 | ||
4353 | tmp = cputime_to_cputime64(cputime); | |
4354 | ||
4355 | p->utime = cputime_add(p->utime, cputime); | |
4356 | p->gtime = cputime_add(p->gtime, cputime); | |
4357 | ||
4358 | cpustat->user = cputime64_add(cpustat->user, tmp); | |
4359 | cpustat->guest = cputime64_add(cpustat->guest, tmp); | |
4360 | } | |
4361 | ||
c66f08be MN |
4362 | /* |
4363 | * Account scaled user cpu time to a process. | |
4364 | * @p: the process that the cpu time gets accounted to | |
4365 | * @cputime: the cpu time spent in user space since the last update | |
4366 | */ | |
4367 | void account_user_time_scaled(struct task_struct *p, cputime_t cputime) | |
4368 | { | |
4369 | p->utimescaled = cputime_add(p->utimescaled, cputime); | |
4370 | } | |
4371 | ||
1da177e4 LT |
4372 | /* |
4373 | * Account system cpu time to a process. | |
4374 | * @p: the process that the cpu time gets accounted to | |
4375 | * @hardirq_offset: the offset to subtract from hardirq_count() | |
4376 | * @cputime: the cpu time spent in kernel space since the last update | |
4377 | */ | |
4378 | void account_system_time(struct task_struct *p, int hardirq_offset, | |
4379 | cputime_t cputime) | |
4380 | { | |
4381 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | |
70b97a7f | 4382 | struct rq *rq = this_rq(); |
1da177e4 LT |
4383 | cputime64_t tmp; |
4384 | ||
9778385d CB |
4385 | if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) |
4386 | return account_guest_time(p, cputime); | |
94886b84 | 4387 | |
1da177e4 LT |
4388 | p->stime = cputime_add(p->stime, cputime); |
4389 | ||
4390 | /* Add system time to cpustat. */ | |
4391 | tmp = cputime_to_cputime64(cputime); | |
4392 | if (hardirq_count() - hardirq_offset) | |
4393 | cpustat->irq = cputime64_add(cpustat->irq, tmp); | |
4394 | else if (softirq_count()) | |
4395 | cpustat->softirq = cputime64_add(cpustat->softirq, tmp); | |
cfb52856 | 4396 | else if (p != rq->idle) |
1da177e4 | 4397 | cpustat->system = cputime64_add(cpustat->system, tmp); |
cfb52856 | 4398 | else if (atomic_read(&rq->nr_iowait) > 0) |
1da177e4 LT |
4399 | cpustat->iowait = cputime64_add(cpustat->iowait, tmp); |
4400 | else | |
4401 | cpustat->idle = cputime64_add(cpustat->idle, tmp); | |
4402 | /* Account for system time used */ | |
4403 | acct_update_integrals(p); | |
1da177e4 LT |
4404 | } |
4405 | ||
c66f08be MN |
4406 | /* |
4407 | * Account scaled system cpu time to a process. | |
4408 | * @p: the process that the cpu time gets accounted to | |
4409 | * @hardirq_offset: the offset to subtract from hardirq_count() | |
4410 | * @cputime: the cpu time spent in kernel space since the last update | |
4411 | */ | |
4412 | void account_system_time_scaled(struct task_struct *p, cputime_t cputime) | |
4413 | { | |
4414 | p->stimescaled = cputime_add(p->stimescaled, cputime); | |
4415 | } | |
4416 | ||
1da177e4 LT |
4417 | /* |
4418 | * Account for involuntary wait time. | |
4419 | * @p: the process from which the cpu time has been stolen | |
4420 | * @steal: the cpu time spent in involuntary wait | |
4421 | */ | |
4422 | void account_steal_time(struct task_struct *p, cputime_t steal) | |
4423 | { | |
4424 | struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat; | |
4425 | cputime64_t tmp = cputime_to_cputime64(steal); | |
70b97a7f | 4426 | struct rq *rq = this_rq(); |
1da177e4 LT |
4427 | |
4428 | if (p == rq->idle) { | |
4429 | p->stime = cputime_add(p->stime, steal); | |
4430 | if (atomic_read(&rq->nr_iowait) > 0) | |
4431 | cpustat->iowait = cputime64_add(cpustat->iowait, tmp); | |
4432 | else | |
4433 | cpustat->idle = cputime64_add(cpustat->idle, tmp); | |
cfb52856 | 4434 | } else |
1da177e4 LT |
4435 | cpustat->steal = cputime64_add(cpustat->steal, tmp); |
4436 | } | |
4437 | ||
7835b98b CL |
4438 | /* |
4439 | * This function gets called by the timer code, with HZ frequency. | |
4440 | * We call it with interrupts disabled. | |
4441 | * | |
4442 | * It also gets called by the fork code, when changing the parent's | |
4443 | * timeslices. | |
4444 | */ | |
4445 | void scheduler_tick(void) | |
4446 | { | |
7835b98b CL |
4447 | int cpu = smp_processor_id(); |
4448 | struct rq *rq = cpu_rq(cpu); | |
dd41f596 | 4449 | struct task_struct *curr = rq->curr; |
529c7726 | 4450 | u64 next_tick = rq->tick_timestamp + TICK_NSEC; |
dd41f596 IM |
4451 | |
4452 | spin_lock(&rq->lock); | |
546fe3c9 | 4453 | __update_rq_clock(rq); |
529c7726 IM |
4454 | /* |
4455 | * Let rq->clock advance by at least TICK_NSEC: | |
4456 | */ | |
cc203d24 | 4457 | if (unlikely(rq->clock < next_tick)) { |
529c7726 | 4458 | rq->clock = next_tick; |
cc203d24 GC |
4459 | rq->clock_underflows++; |
4460 | } | |
529c7726 | 4461 | rq->tick_timestamp = rq->clock; |
15934a37 | 4462 | update_last_tick_seen(rq); |
f1a438d8 | 4463 | update_cpu_load(rq); |
fa85ae24 | 4464 | curr->sched_class->task_tick(rq, curr, 0); |
dd41f596 | 4465 | spin_unlock(&rq->lock); |
7835b98b | 4466 | |
e418e1c2 | 4467 | #ifdef CONFIG_SMP |
dd41f596 IM |
4468 | rq->idle_at_tick = idle_cpu(cpu); |
4469 | trigger_load_balance(rq, cpu); | |
e418e1c2 | 4470 | #endif |
1da177e4 LT |
4471 | } |
4472 | ||
1da177e4 LT |
4473 | #if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT) |
4474 | ||
43627582 | 4475 | void __kprobes add_preempt_count(int val) |
1da177e4 LT |
4476 | { |
4477 | /* | |
4478 | * Underflow? | |
4479 | */ | |
9a11b49a IM |
4480 | if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0))) |
4481 | return; | |
1da177e4 LT |
4482 | preempt_count() += val; |
4483 | /* | |
4484 | * Spinlock count overflowing soon? | |
4485 | */ | |
33859f7f MOS |
4486 | DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >= |
4487 | PREEMPT_MASK - 10); | |
1da177e4 LT |
4488 | } |
4489 | EXPORT_SYMBOL(add_preempt_count); | |
4490 | ||
43627582 | 4491 | void __kprobes sub_preempt_count(int val) |
1da177e4 LT |
4492 | { |
4493 | /* | |
4494 | * Underflow? | |
4495 | */ | |
9a11b49a IM |
4496 | if (DEBUG_LOCKS_WARN_ON(val > preempt_count())) |
4497 | return; | |
1da177e4 LT |
4498 | /* |
4499 | * Is the spinlock portion underflowing? | |
4500 | */ | |
9a11b49a IM |
4501 | if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) && |
4502 | !(preempt_count() & PREEMPT_MASK))) | |
4503 | return; | |
4504 | ||
1da177e4 LT |
4505 | preempt_count() -= val; |
4506 | } | |
4507 | EXPORT_SYMBOL(sub_preempt_count); | |
4508 | ||
4509 | #endif | |
4510 | ||
4511 | /* | |
dd41f596 | 4512 | * Print scheduling while atomic bug: |
1da177e4 | 4513 | */ |
dd41f596 | 4514 | static noinline void __schedule_bug(struct task_struct *prev) |
1da177e4 | 4515 | { |
838225b4 SS |
4516 | struct pt_regs *regs = get_irq_regs(); |
4517 | ||
4518 | printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n", | |
4519 | prev->comm, prev->pid, preempt_count()); | |
4520 | ||
dd41f596 IM |
4521 | debug_show_held_locks(prev); |
4522 | if (irqs_disabled()) | |
4523 | print_irqtrace_events(prev); | |
838225b4 SS |
4524 | |
4525 | if (regs) | |
4526 | show_regs(regs); | |
4527 | else | |
4528 | dump_stack(); | |
dd41f596 | 4529 | } |
1da177e4 | 4530 | |
dd41f596 IM |
4531 | /* |
4532 | * Various schedule()-time debugging checks and statistics: | |
4533 | */ | |
4534 | static inline void schedule_debug(struct task_struct *prev) | |
4535 | { | |
1da177e4 | 4536 | /* |
41a2d6cf | 4537 | * Test if we are atomic. Since do_exit() needs to call into |
1da177e4 LT |
4538 | * schedule() atomically, we ignore that path for now. |
4539 | * Otherwise, whine if we are scheduling when we should not be. | |
4540 | */ | |
dd41f596 IM |
4541 | if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state)) |
4542 | __schedule_bug(prev); | |
4543 | ||
1da177e4 LT |
4544 | profile_hit(SCHED_PROFILING, __builtin_return_address(0)); |
4545 | ||
2d72376b | 4546 | schedstat_inc(this_rq(), sched_count); |
b8efb561 IM |
4547 | #ifdef CONFIG_SCHEDSTATS |
4548 | if (unlikely(prev->lock_depth >= 0)) { | |
2d72376b IM |
4549 | schedstat_inc(this_rq(), bkl_count); |
4550 | schedstat_inc(prev, sched_info.bkl_count); | |
b8efb561 IM |
4551 | } |
4552 | #endif | |
dd41f596 IM |
4553 | } |
4554 | ||
4555 | /* | |
4556 | * Pick up the highest-prio task: | |
4557 | */ | |
4558 | static inline struct task_struct * | |
ff95f3df | 4559 | pick_next_task(struct rq *rq, struct task_struct *prev) |
dd41f596 | 4560 | { |
5522d5d5 | 4561 | const struct sched_class *class; |
dd41f596 | 4562 | struct task_struct *p; |
1da177e4 LT |
4563 | |
4564 | /* | |
dd41f596 IM |
4565 | * Optimization: we know that if all tasks are in |
4566 | * the fair class we can call that function directly: | |
1da177e4 | 4567 | */ |
dd41f596 | 4568 | if (likely(rq->nr_running == rq->cfs.nr_running)) { |
fb8d4724 | 4569 | p = fair_sched_class.pick_next_task(rq); |
dd41f596 IM |
4570 | if (likely(p)) |
4571 | return p; | |
1da177e4 LT |
4572 | } |
4573 | ||
dd41f596 IM |
4574 | class = sched_class_highest; |
4575 | for ( ; ; ) { | |
fb8d4724 | 4576 | p = class->pick_next_task(rq); |
dd41f596 IM |
4577 | if (p) |
4578 | return p; | |
4579 | /* | |
4580 | * Will never be NULL as the idle class always | |
4581 | * returns a non-NULL p: | |
4582 | */ | |
4583 | class = class->next; | |
4584 | } | |
4585 | } | |
1da177e4 | 4586 | |
dd41f596 IM |
4587 | /* |
4588 | * schedule() is the main scheduler function. | |
4589 | */ | |
4590 | asmlinkage void __sched schedule(void) | |
4591 | { | |
4592 | struct task_struct *prev, *next; | |
67ca7bde | 4593 | unsigned long *switch_count; |
dd41f596 | 4594 | struct rq *rq; |
dd41f596 IM |
4595 | int cpu; |
4596 | ||
4597 | need_resched: | |
4598 | preempt_disable(); | |
4599 | cpu = smp_processor_id(); | |
4600 | rq = cpu_rq(cpu); | |
4601 | rcu_qsctr_inc(cpu); | |
4602 | prev = rq->curr; | |
4603 | switch_count = &prev->nivcsw; | |
4604 | ||
4605 | release_kernel_lock(prev); | |
4606 | need_resched_nonpreemptible: | |
4607 | ||
4608 | schedule_debug(prev); | |
1da177e4 | 4609 | |
8f4d37ec PZ |
4610 | hrtick_clear(rq); |
4611 | ||
1e819950 IM |
4612 | /* |
4613 | * Do the rq-clock update outside the rq lock: | |
4614 | */ | |
4615 | local_irq_disable(); | |
c1b3da3e | 4616 | __update_rq_clock(rq); |
1e819950 IM |
4617 | spin_lock(&rq->lock); |
4618 | clear_tsk_need_resched(prev); | |
1da177e4 | 4619 | |
1da177e4 | 4620 | if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) { |
1da177e4 | 4621 | if (unlikely((prev->state & TASK_INTERRUPTIBLE) && |
23e3c3cd | 4622 | signal_pending(prev))) { |
1da177e4 | 4623 | prev->state = TASK_RUNNING; |
dd41f596 | 4624 | } else { |
2e1cb74a | 4625 | deactivate_task(rq, prev, 1); |
1da177e4 | 4626 | } |
dd41f596 | 4627 | switch_count = &prev->nvcsw; |
1da177e4 LT |
4628 | } |
4629 | ||
9a897c5a SR |
4630 | #ifdef CONFIG_SMP |
4631 | if (prev->sched_class->pre_schedule) | |
4632 | prev->sched_class->pre_schedule(rq, prev); | |
4633 | #endif | |
f65eda4f | 4634 | |
dd41f596 | 4635 | if (unlikely(!rq->nr_running)) |
1da177e4 | 4636 | idle_balance(cpu, rq); |
1da177e4 | 4637 | |
31ee529c | 4638 | prev->sched_class->put_prev_task(rq, prev); |
ff95f3df | 4639 | next = pick_next_task(rq, prev); |
1da177e4 LT |
4640 | |
4641 | sched_info_switch(prev, next); | |
dd41f596 | 4642 | |
1da177e4 | 4643 | if (likely(prev != next)) { |
1da177e4 LT |
4644 | rq->nr_switches++; |
4645 | rq->curr = next; | |
4646 | ++*switch_count; | |
4647 | ||
dd41f596 | 4648 | context_switch(rq, prev, next); /* unlocks the rq */ |
8f4d37ec PZ |
4649 | /* |
4650 | * the context switch might have flipped the stack from under | |
4651 | * us, hence refresh the local variables. | |
4652 | */ | |
4653 | cpu = smp_processor_id(); | |
4654 | rq = cpu_rq(cpu); | |
1da177e4 LT |
4655 | } else |
4656 | spin_unlock_irq(&rq->lock); | |
4657 | ||
8f4d37ec PZ |
4658 | hrtick_set(rq); |
4659 | ||
4660 | if (unlikely(reacquire_kernel_lock(current) < 0)) | |
1da177e4 | 4661 | goto need_resched_nonpreemptible; |
8f4d37ec | 4662 | |
1da177e4 LT |
4663 | preempt_enable_no_resched(); |
4664 | if (unlikely(test_thread_flag(TIF_NEED_RESCHED))) | |
4665 | goto need_resched; | |
4666 | } | |
1da177e4 LT |
4667 | EXPORT_SYMBOL(schedule); |
4668 | ||
4669 | #ifdef CONFIG_PREEMPT | |
4670 | /* | |
2ed6e34f | 4671 | * this is the entry point to schedule() from in-kernel preemption |
41a2d6cf | 4672 | * off of preempt_enable. Kernel preemptions off return from interrupt |
1da177e4 LT |
4673 | * occur there and call schedule directly. |
4674 | */ | |
4675 | asmlinkage void __sched preempt_schedule(void) | |
4676 | { | |
4677 | struct thread_info *ti = current_thread_info(); | |
1da177e4 LT |
4678 | struct task_struct *task = current; |
4679 | int saved_lock_depth; | |
6478d880 | 4680 | |
1da177e4 LT |
4681 | /* |
4682 | * If there is a non-zero preempt_count or interrupts are disabled, | |
41a2d6cf | 4683 | * we do not want to preempt the current task. Just return.. |
1da177e4 | 4684 | */ |
beed33a8 | 4685 | if (likely(ti->preempt_count || irqs_disabled())) |
1da177e4 LT |
4686 | return; |
4687 | ||
3a5c359a AK |
4688 | do { |
4689 | add_preempt_count(PREEMPT_ACTIVE); | |
4690 | ||
4691 | /* | |
4692 | * We keep the big kernel semaphore locked, but we | |
4693 | * clear ->lock_depth so that schedule() doesnt | |
4694 | * auto-release the semaphore: | |
4695 | */ | |
3a5c359a AK |
4696 | saved_lock_depth = task->lock_depth; |
4697 | task->lock_depth = -1; | |
3a5c359a | 4698 | schedule(); |
3a5c359a | 4699 | task->lock_depth = saved_lock_depth; |
3a5c359a | 4700 | sub_preempt_count(PREEMPT_ACTIVE); |
1da177e4 | 4701 | |
3a5c359a AK |
4702 | /* |
4703 | * Check again in case we missed a preemption opportunity | |
4704 | * between schedule and now. | |
4705 | */ | |
4706 | barrier(); | |
4707 | } while (unlikely(test_thread_flag(TIF_NEED_RESCHED))); | |
1da177e4 | 4708 | } |
1da177e4 LT |
4709 | EXPORT_SYMBOL(preempt_schedule); |
4710 | ||
4711 | /* | |
2ed6e34f | 4712 | * this is the entry point to schedule() from kernel preemption |
1da177e4 LT |
4713 | * off of irq context. |
4714 | * Note, that this is called and return with irqs disabled. This will | |
4715 | * protect us against recursive calling from irq. | |
4716 | */ | |
4717 | asmlinkage void __sched preempt_schedule_irq(void) | |
4718 | { | |
4719 | struct thread_info *ti = current_thread_info(); | |
1da177e4 LT |
4720 | struct task_struct *task = current; |
4721 | int saved_lock_depth; | |
6478d880 | 4722 | |
2ed6e34f | 4723 | /* Catch callers which need to be fixed */ |
1da177e4 LT |
4724 | BUG_ON(ti->preempt_count || !irqs_disabled()); |
4725 | ||
3a5c359a AK |
4726 | do { |
4727 | add_preempt_count(PREEMPT_ACTIVE); | |
4728 | ||
4729 | /* | |
4730 | * We keep the big kernel semaphore locked, but we | |
4731 | * clear ->lock_depth so that schedule() doesnt | |
4732 | * auto-release the semaphore: | |
4733 | */ | |
3a5c359a AK |
4734 | saved_lock_depth = task->lock_depth; |
4735 | task->lock_depth = -1; | |
3a5c359a AK |
4736 | local_irq_enable(); |
4737 | schedule(); | |
4738 | local_irq_disable(); | |
3a5c359a | 4739 | task->lock_depth = saved_lock_depth; |
3a5c359a | 4740 | sub_preempt_count(PREEMPT_ACTIVE); |
1da177e4 | 4741 | |
3a5c359a AK |
4742 | /* |
4743 | * Check again in case we missed a preemption opportunity | |
4744 | * between schedule and now. | |
4745 | */ | |
4746 | barrier(); | |
4747 | } while (unlikely(test_thread_flag(TIF_NEED_RESCHED))); | |
1da177e4 LT |
4748 | } |
4749 | ||
4750 | #endif /* CONFIG_PREEMPT */ | |
4751 | ||
95cdf3b7 IM |
4752 | int default_wake_function(wait_queue_t *curr, unsigned mode, int sync, |
4753 | void *key) | |
1da177e4 | 4754 | { |
48f24c4d | 4755 | return try_to_wake_up(curr->private, mode, sync); |
1da177e4 | 4756 | } |
1da177e4 LT |
4757 | EXPORT_SYMBOL(default_wake_function); |
4758 | ||
4759 | /* | |
41a2d6cf IM |
4760 | * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just |
4761 | * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve | |
1da177e4 LT |
4762 | * number) then we wake all the non-exclusive tasks and one exclusive task. |
4763 | * | |
4764 | * There are circumstances in which we can try to wake a task which has already | |
41a2d6cf | 4765 | * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns |
1da177e4 LT |
4766 | * zero in this (rare) case, and we handle it by continuing to scan the queue. |
4767 | */ | |
4768 | static void __wake_up_common(wait_queue_head_t *q, unsigned int mode, | |
4769 | int nr_exclusive, int sync, void *key) | |
4770 | { | |
2e45874c | 4771 | wait_queue_t *curr, *next; |
1da177e4 | 4772 | |
2e45874c | 4773 | list_for_each_entry_safe(curr, next, &q->task_list, task_list) { |
48f24c4d IM |
4774 | unsigned flags = curr->flags; |
4775 | ||
1da177e4 | 4776 | if (curr->func(curr, mode, sync, key) && |
48f24c4d | 4777 | (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive) |
1da177e4 LT |
4778 | break; |
4779 | } | |
4780 | } | |
4781 | ||
4782 | /** | |
4783 | * __wake_up - wake up threads blocked on a waitqueue. | |
4784 | * @q: the waitqueue | |
4785 | * @mode: which threads | |
4786 | * @nr_exclusive: how many wake-one or wake-many threads to wake up | |
67be2dd1 | 4787 | * @key: is directly passed to the wakeup function |
1da177e4 | 4788 | */ |
7ad5b3a5 | 4789 | void __wake_up(wait_queue_head_t *q, unsigned int mode, |
95cdf3b7 | 4790 | int nr_exclusive, void *key) |
1da177e4 LT |
4791 | { |
4792 | unsigned long flags; | |
4793 | ||
4794 | spin_lock_irqsave(&q->lock, flags); | |
4795 | __wake_up_common(q, mode, nr_exclusive, 0, key); | |
4796 | spin_unlock_irqrestore(&q->lock, flags); | |
4797 | } | |
1da177e4 LT |
4798 | EXPORT_SYMBOL(__wake_up); |
4799 | ||
4800 | /* | |
4801 | * Same as __wake_up but called with the spinlock in wait_queue_head_t held. | |
4802 | */ | |
7ad5b3a5 | 4803 | void __wake_up_locked(wait_queue_head_t *q, unsigned int mode) |
1da177e4 LT |
4804 | { |
4805 | __wake_up_common(q, mode, 1, 0, NULL); | |
4806 | } | |
4807 | ||
4808 | /** | |
67be2dd1 | 4809 | * __wake_up_sync - wake up threads blocked on a waitqueue. |
1da177e4 LT |
4810 | * @q: the waitqueue |
4811 | * @mode: which threads | |
4812 | * @nr_exclusive: how many wake-one or wake-many threads to wake up | |
4813 | * | |
4814 | * The sync wakeup differs that the waker knows that it will schedule | |
4815 | * away soon, so while the target thread will be woken up, it will not | |
4816 | * be migrated to another CPU - ie. the two threads are 'synchronized' | |
4817 | * with each other. This can prevent needless bouncing between CPUs. | |
4818 | * | |
4819 | * On UP it can prevent extra preemption. | |
4820 | */ | |
7ad5b3a5 | 4821 | void |
95cdf3b7 | 4822 | __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive) |
1da177e4 LT |
4823 | { |
4824 | unsigned long flags; | |
4825 | int sync = 1; | |
4826 | ||
4827 | if (unlikely(!q)) | |
4828 | return; | |
4829 | ||
4830 | if (unlikely(!nr_exclusive)) | |
4831 | sync = 0; | |
4832 | ||
4833 | spin_lock_irqsave(&q->lock, flags); | |
4834 | __wake_up_common(q, mode, nr_exclusive, sync, NULL); | |
4835 | spin_unlock_irqrestore(&q->lock, flags); | |
4836 | } | |
4837 | EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */ | |
4838 | ||
b15136e9 | 4839 | void complete(struct completion *x) |
1da177e4 LT |
4840 | { |
4841 | unsigned long flags; | |
4842 | ||
4843 | spin_lock_irqsave(&x->wait.lock, flags); | |
4844 | x->done++; | |
d9514f6c | 4845 | __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL); |
1da177e4 LT |
4846 | spin_unlock_irqrestore(&x->wait.lock, flags); |
4847 | } | |
4848 | EXPORT_SYMBOL(complete); | |
4849 | ||
b15136e9 | 4850 | void complete_all(struct completion *x) |
1da177e4 LT |
4851 | { |
4852 | unsigned long flags; | |
4853 | ||
4854 | spin_lock_irqsave(&x->wait.lock, flags); | |
4855 | x->done += UINT_MAX/2; | |
d9514f6c | 4856 | __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL); |
1da177e4 LT |
4857 | spin_unlock_irqrestore(&x->wait.lock, flags); |
4858 | } | |
4859 | EXPORT_SYMBOL(complete_all); | |
4860 | ||
8cbbe86d AK |
4861 | static inline long __sched |
4862 | do_wait_for_common(struct completion *x, long timeout, int state) | |
1da177e4 | 4863 | { |
1da177e4 LT |
4864 | if (!x->done) { |
4865 | DECLARE_WAITQUEUE(wait, current); | |
4866 | ||
4867 | wait.flags |= WQ_FLAG_EXCLUSIVE; | |
4868 | __add_wait_queue_tail(&x->wait, &wait); | |
4869 | do { | |
009e577e MW |
4870 | if ((state == TASK_INTERRUPTIBLE && |
4871 | signal_pending(current)) || | |
4872 | (state == TASK_KILLABLE && | |
4873 | fatal_signal_pending(current))) { | |
8cbbe86d AK |
4874 | __remove_wait_queue(&x->wait, &wait); |
4875 | return -ERESTARTSYS; | |
4876 | } | |
4877 | __set_current_state(state); | |
1da177e4 LT |
4878 | spin_unlock_irq(&x->wait.lock); |
4879 | timeout = schedule_timeout(timeout); | |
4880 | spin_lock_irq(&x->wait.lock); | |
4881 | if (!timeout) { | |
4882 | __remove_wait_queue(&x->wait, &wait); | |
8cbbe86d | 4883 | return timeout; |
1da177e4 LT |
4884 | } |
4885 | } while (!x->done); | |
4886 | __remove_wait_queue(&x->wait, &wait); | |
4887 | } | |
4888 | x->done--; | |
1da177e4 LT |
4889 | return timeout; |
4890 | } | |
1da177e4 | 4891 | |
8cbbe86d AK |
4892 | static long __sched |
4893 | wait_for_common(struct completion *x, long timeout, int state) | |
1da177e4 | 4894 | { |
1da177e4 LT |
4895 | might_sleep(); |
4896 | ||
4897 | spin_lock_irq(&x->wait.lock); | |
8cbbe86d | 4898 | timeout = do_wait_for_common(x, timeout, state); |
1da177e4 | 4899 | spin_unlock_irq(&x->wait.lock); |
8cbbe86d AK |
4900 | return timeout; |
4901 | } | |
1da177e4 | 4902 | |
b15136e9 | 4903 | void __sched wait_for_completion(struct completion *x) |
8cbbe86d AK |
4904 | { |
4905 | wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE); | |
1da177e4 | 4906 | } |
8cbbe86d | 4907 | EXPORT_SYMBOL(wait_for_completion); |
1da177e4 | 4908 | |
b15136e9 | 4909 | unsigned long __sched |
8cbbe86d | 4910 | wait_for_completion_timeout(struct completion *x, unsigned long timeout) |
1da177e4 | 4911 | { |
8cbbe86d | 4912 | return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE); |
1da177e4 | 4913 | } |
8cbbe86d | 4914 | EXPORT_SYMBOL(wait_for_completion_timeout); |
1da177e4 | 4915 | |
8cbbe86d | 4916 | int __sched wait_for_completion_interruptible(struct completion *x) |
0fec171c | 4917 | { |
51e97990 AK |
4918 | long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE); |
4919 | if (t == -ERESTARTSYS) | |
4920 | return t; | |
4921 | return 0; | |
0fec171c | 4922 | } |
8cbbe86d | 4923 | EXPORT_SYMBOL(wait_for_completion_interruptible); |
1da177e4 | 4924 | |
b15136e9 | 4925 | unsigned long __sched |
8cbbe86d AK |
4926 | wait_for_completion_interruptible_timeout(struct completion *x, |
4927 | unsigned long timeout) | |
0fec171c | 4928 | { |
8cbbe86d | 4929 | return wait_for_common(x, timeout, TASK_INTERRUPTIBLE); |
0fec171c | 4930 | } |
8cbbe86d | 4931 | EXPORT_SYMBOL(wait_for_completion_interruptible_timeout); |
1da177e4 | 4932 | |
009e577e MW |
4933 | int __sched wait_for_completion_killable(struct completion *x) |
4934 | { | |
4935 | long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE); | |
4936 | if (t == -ERESTARTSYS) | |
4937 | return t; | |
4938 | return 0; | |
4939 | } | |
4940 | EXPORT_SYMBOL(wait_for_completion_killable); | |
4941 | ||
8cbbe86d AK |
4942 | static long __sched |
4943 | sleep_on_common(wait_queue_head_t *q, int state, long timeout) | |
1da177e4 | 4944 | { |
0fec171c IM |
4945 | unsigned long flags; |
4946 | wait_queue_t wait; | |
4947 | ||
4948 | init_waitqueue_entry(&wait, current); | |
1da177e4 | 4949 | |
8cbbe86d | 4950 | __set_current_state(state); |
1da177e4 | 4951 | |
8cbbe86d AK |
4952 | spin_lock_irqsave(&q->lock, flags); |
4953 | __add_wait_queue(q, &wait); | |
4954 | spin_unlock(&q->lock); | |
4955 | timeout = schedule_timeout(timeout); | |
4956 | spin_lock_irq(&q->lock); | |
4957 | __remove_wait_queue(q, &wait); | |
4958 | spin_unlock_irqrestore(&q->lock, flags); | |
4959 | ||
4960 | return timeout; | |
4961 | } | |
4962 | ||
4963 | void __sched interruptible_sleep_on(wait_queue_head_t *q) | |
4964 | { | |
4965 | sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); | |
1da177e4 | 4966 | } |
1da177e4 LT |
4967 | EXPORT_SYMBOL(interruptible_sleep_on); |
4968 | ||
0fec171c | 4969 | long __sched |
95cdf3b7 | 4970 | interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout) |
1da177e4 | 4971 | { |
8cbbe86d | 4972 | return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout); |
1da177e4 | 4973 | } |
1da177e4 LT |
4974 | EXPORT_SYMBOL(interruptible_sleep_on_timeout); |
4975 | ||
0fec171c | 4976 | void __sched sleep_on(wait_queue_head_t *q) |
1da177e4 | 4977 | { |
8cbbe86d | 4978 | sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
1da177e4 | 4979 | } |
1da177e4 LT |
4980 | EXPORT_SYMBOL(sleep_on); |
4981 | ||
0fec171c | 4982 | long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout) |
1da177e4 | 4983 | { |
8cbbe86d | 4984 | return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout); |
1da177e4 | 4985 | } |
1da177e4 LT |
4986 | EXPORT_SYMBOL(sleep_on_timeout); |
4987 | ||
b29739f9 IM |
4988 | #ifdef CONFIG_RT_MUTEXES |
4989 | ||
4990 | /* | |
4991 | * rt_mutex_setprio - set the current priority of a task | |
4992 | * @p: task | |
4993 | * @prio: prio value (kernel-internal form) | |
4994 | * | |
4995 | * This function changes the 'effective' priority of a task. It does | |
4996 | * not touch ->normal_prio like __setscheduler(). | |
4997 | * | |
4998 | * Used by the rt_mutex code to implement priority inheritance logic. | |
4999 | */ | |
36c8b586 | 5000 | void rt_mutex_setprio(struct task_struct *p, int prio) |
b29739f9 IM |
5001 | { |
5002 | unsigned long flags; | |
83b699ed | 5003 | int oldprio, on_rq, running; |
70b97a7f | 5004 | struct rq *rq; |
cb469845 | 5005 | const struct sched_class *prev_class = p->sched_class; |
b29739f9 IM |
5006 | |
5007 | BUG_ON(prio < 0 || prio > MAX_PRIO); | |
5008 | ||
5009 | rq = task_rq_lock(p, &flags); | |
a8e504d2 | 5010 | update_rq_clock(rq); |
b29739f9 | 5011 | |
d5f9f942 | 5012 | oldprio = p->prio; |
dd41f596 | 5013 | on_rq = p->se.on_rq; |
051a1d1a | 5014 | running = task_current(rq, p); |
0e1f3483 | 5015 | if (on_rq) |
69be72c1 | 5016 | dequeue_task(rq, p, 0); |
0e1f3483 HS |
5017 | if (running) |
5018 | p->sched_class->put_prev_task(rq, p); | |
dd41f596 IM |
5019 | |
5020 | if (rt_prio(prio)) | |
5021 | p->sched_class = &rt_sched_class; | |
5022 | else | |
5023 | p->sched_class = &fair_sched_class; | |
5024 | ||
b29739f9 IM |
5025 | p->prio = prio; |
5026 | ||
0e1f3483 HS |
5027 | if (running) |
5028 | p->sched_class->set_curr_task(rq); | |
dd41f596 | 5029 | if (on_rq) { |
8159f87e | 5030 | enqueue_task(rq, p, 0); |
cb469845 SR |
5031 | |
5032 | check_class_changed(rq, p, prev_class, oldprio, running); | |
b29739f9 IM |
5033 | } |
5034 | task_rq_unlock(rq, &flags); | |
5035 | } | |
5036 | ||
5037 | #endif | |
5038 | ||
36c8b586 | 5039 | void set_user_nice(struct task_struct *p, long nice) |
1da177e4 | 5040 | { |
dd41f596 | 5041 | int old_prio, delta, on_rq; |
1da177e4 | 5042 | unsigned long flags; |
70b97a7f | 5043 | struct rq *rq; |
1da177e4 LT |
5044 | |
5045 | if (TASK_NICE(p) == nice || nice < -20 || nice > 19) | |
5046 | return; | |
5047 | /* | |
5048 | * We have to be careful, if called from sys_setpriority(), | |
5049 | * the task might be in the middle of scheduling on another CPU. | |
5050 | */ | |
5051 | rq = task_rq_lock(p, &flags); | |
a8e504d2 | 5052 | update_rq_clock(rq); |
1da177e4 LT |
5053 | /* |
5054 | * The RT priorities are set via sched_setscheduler(), but we still | |
5055 | * allow the 'normal' nice value to be set - but as expected | |
5056 | * it wont have any effect on scheduling until the task is | |
dd41f596 | 5057 | * SCHED_FIFO/SCHED_RR: |
1da177e4 | 5058 | */ |
e05606d3 | 5059 | if (task_has_rt_policy(p)) { |
1da177e4 LT |
5060 | p->static_prio = NICE_TO_PRIO(nice); |
5061 | goto out_unlock; | |
5062 | } | |
dd41f596 | 5063 | on_rq = p->se.on_rq; |
18d95a28 | 5064 | if (on_rq) |
69be72c1 | 5065 | dequeue_task(rq, p, 0); |
1da177e4 | 5066 | |
1da177e4 | 5067 | p->static_prio = NICE_TO_PRIO(nice); |
2dd73a4f | 5068 | set_load_weight(p); |
b29739f9 IM |
5069 | old_prio = p->prio; |
5070 | p->prio = effective_prio(p); | |
5071 | delta = p->prio - old_prio; | |
1da177e4 | 5072 | |
dd41f596 | 5073 | if (on_rq) { |
8159f87e | 5074 | enqueue_task(rq, p, 0); |
1da177e4 | 5075 | /* |
d5f9f942 AM |
5076 | * If the task increased its priority or is running and |
5077 | * lowered its priority, then reschedule its CPU: | |
1da177e4 | 5078 | */ |
d5f9f942 | 5079 | if (delta < 0 || (delta > 0 && task_running(rq, p))) |
1da177e4 LT |
5080 | resched_task(rq->curr); |
5081 | } | |
5082 | out_unlock: | |
5083 | task_rq_unlock(rq, &flags); | |
5084 | } | |
1da177e4 LT |
5085 | EXPORT_SYMBOL(set_user_nice); |
5086 | ||
e43379f1 MM |
5087 | /* |
5088 | * can_nice - check if a task can reduce its nice value | |
5089 | * @p: task | |
5090 | * @nice: nice value | |
5091 | */ | |
36c8b586 | 5092 | int can_nice(const struct task_struct *p, const int nice) |
e43379f1 | 5093 | { |
024f4747 MM |
5094 | /* convert nice value [19,-20] to rlimit style value [1,40] */ |
5095 | int nice_rlim = 20 - nice; | |
48f24c4d | 5096 | |
e43379f1 MM |
5097 | return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur || |
5098 | capable(CAP_SYS_NICE)); | |
5099 | } | |
5100 | ||
1da177e4 LT |
5101 | #ifdef __ARCH_WANT_SYS_NICE |
5102 | ||
5103 | /* | |
5104 | * sys_nice - change the priority of the current process. | |
5105 | * @increment: priority increment | |
5106 | * | |
5107 | * sys_setpriority is a more generic, but much slower function that | |
5108 | * does similar things. | |
5109 | */ | |
5110 | asmlinkage long sys_nice(int increment) | |
5111 | { | |
48f24c4d | 5112 | long nice, retval; |
1da177e4 LT |
5113 | |
5114 | /* | |
5115 | * Setpriority might change our priority at the same moment. | |
5116 | * We don't have to worry. Conceptually one call occurs first | |
5117 | * and we have a single winner. | |
5118 | */ | |
e43379f1 MM |
5119 | if (increment < -40) |
5120 | increment = -40; | |
1da177e4 LT |
5121 | if (increment > 40) |
5122 | increment = 40; | |
5123 | ||
5124 | nice = PRIO_TO_NICE(current->static_prio) + increment; | |
5125 | if (nice < -20) | |
5126 | nice = -20; | |
5127 | if (nice > 19) | |
5128 | nice = 19; | |
5129 | ||
e43379f1 MM |
5130 | if (increment < 0 && !can_nice(current, nice)) |
5131 | return -EPERM; | |
5132 | ||
1da177e4 LT |
5133 | retval = security_task_setnice(current, nice); |
5134 | if (retval) | |
5135 | return retval; | |
5136 | ||
5137 | set_user_nice(current, nice); | |
5138 | return 0; | |
5139 | } | |
5140 | ||
5141 | #endif | |
5142 | ||
5143 | /** | |
5144 | * task_prio - return the priority value of a given task. | |
5145 | * @p: the task in question. | |
5146 | * | |
5147 | * This is the priority value as seen by users in /proc. | |
5148 | * RT tasks are offset by -200. Normal tasks are centered | |
5149 | * around 0, value goes from -16 to +15. | |
5150 | */ | |
36c8b586 | 5151 | int task_prio(const struct task_struct *p) |
1da177e4 LT |
5152 | { |
5153 | return p->prio - MAX_RT_PRIO; | |
5154 | } | |
5155 | ||
5156 | /** | |
5157 | * task_nice - return the nice value of a given task. | |
5158 | * @p: the task in question. | |
5159 | */ | |
36c8b586 | 5160 | int task_nice(const struct task_struct *p) |
1da177e4 LT |
5161 | { |
5162 | return TASK_NICE(p); | |
5163 | } | |
150d8bed | 5164 | EXPORT_SYMBOL(task_nice); |
1da177e4 LT |
5165 | |
5166 | /** | |
5167 | * idle_cpu - is a given cpu idle currently? | |
5168 | * @cpu: the processor in question. | |
5169 | */ | |
5170 | int idle_cpu(int cpu) | |
5171 | { | |
5172 | return cpu_curr(cpu) == cpu_rq(cpu)->idle; | |
5173 | } | |
5174 | ||
1da177e4 LT |
5175 | /** |
5176 | * idle_task - return the idle task for a given cpu. | |
5177 | * @cpu: the processor in question. | |
5178 | */ | |
36c8b586 | 5179 | struct task_struct *idle_task(int cpu) |
1da177e4 LT |
5180 | { |
5181 | return cpu_rq(cpu)->idle; | |
5182 | } | |
5183 | ||
5184 | /** | |
5185 | * find_process_by_pid - find a process with a matching PID value. | |
5186 | * @pid: the pid in question. | |
5187 | */ | |
a9957449 | 5188 | static struct task_struct *find_process_by_pid(pid_t pid) |
1da177e4 | 5189 | { |
228ebcbe | 5190 | return pid ? find_task_by_vpid(pid) : current; |
1da177e4 LT |
5191 | } |
5192 | ||
5193 | /* Actually do priority change: must hold rq lock. */ | |
dd41f596 IM |
5194 | static void |
5195 | __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio) | |
1da177e4 | 5196 | { |
dd41f596 | 5197 | BUG_ON(p->se.on_rq); |
48f24c4d | 5198 | |
1da177e4 | 5199 | p->policy = policy; |
dd41f596 IM |
5200 | switch (p->policy) { |
5201 | case SCHED_NORMAL: | |
5202 | case SCHED_BATCH: | |
5203 | case SCHED_IDLE: | |
5204 | p->sched_class = &fair_sched_class; | |
5205 | break; | |
5206 | case SCHED_FIFO: | |
5207 | case SCHED_RR: | |
5208 | p->sched_class = &rt_sched_class; | |
5209 | break; | |
5210 | } | |
5211 | ||
1da177e4 | 5212 | p->rt_priority = prio; |
b29739f9 IM |
5213 | p->normal_prio = normal_prio(p); |
5214 | /* we are holding p->pi_lock already */ | |
5215 | p->prio = rt_mutex_getprio(p); | |
2dd73a4f | 5216 | set_load_weight(p); |
1da177e4 LT |
5217 | } |
5218 | ||
5219 | /** | |
72fd4a35 | 5220 | * sched_setscheduler - change the scheduling policy and/or RT priority of a thread. |
1da177e4 LT |
5221 | * @p: the task in question. |
5222 | * @policy: new policy. | |
5223 | * @param: structure containing the new RT priority. | |
5fe1d75f | 5224 | * |
72fd4a35 | 5225 | * NOTE that the task may be already dead. |
1da177e4 | 5226 | */ |
95cdf3b7 IM |
5227 | int sched_setscheduler(struct task_struct *p, int policy, |
5228 | struct sched_param *param) | |
1da177e4 | 5229 | { |
83b699ed | 5230 | int retval, oldprio, oldpolicy = -1, on_rq, running; |
1da177e4 | 5231 | unsigned long flags; |
cb469845 | 5232 | const struct sched_class *prev_class = p->sched_class; |
70b97a7f | 5233 | struct rq *rq; |
1da177e4 | 5234 | |
66e5393a SR |
5235 | /* may grab non-irq protected spin_locks */ |
5236 | BUG_ON(in_interrupt()); | |
1da177e4 LT |
5237 | recheck: |
5238 | /* double check policy once rq lock held */ | |
5239 | if (policy < 0) | |
5240 | policy = oldpolicy = p->policy; | |
5241 | else if (policy != SCHED_FIFO && policy != SCHED_RR && | |
dd41f596 IM |
5242 | policy != SCHED_NORMAL && policy != SCHED_BATCH && |
5243 | policy != SCHED_IDLE) | |
b0a9499c | 5244 | return -EINVAL; |
1da177e4 LT |
5245 | /* |
5246 | * Valid priorities for SCHED_FIFO and SCHED_RR are | |
dd41f596 IM |
5247 | * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL, |
5248 | * SCHED_BATCH and SCHED_IDLE is 0. | |
1da177e4 LT |
5249 | */ |
5250 | if (param->sched_priority < 0 || | |
95cdf3b7 | 5251 | (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) || |
d46523ea | 5252 | (!p->mm && param->sched_priority > MAX_RT_PRIO-1)) |
1da177e4 | 5253 | return -EINVAL; |
e05606d3 | 5254 | if (rt_policy(policy) != (param->sched_priority != 0)) |
1da177e4 LT |
5255 | return -EINVAL; |
5256 | ||
37e4ab3f OC |
5257 | /* |
5258 | * Allow unprivileged RT tasks to decrease priority: | |
5259 | */ | |
5260 | if (!capable(CAP_SYS_NICE)) { | |
e05606d3 | 5261 | if (rt_policy(policy)) { |
8dc3e909 | 5262 | unsigned long rlim_rtprio; |
8dc3e909 ON |
5263 | |
5264 | if (!lock_task_sighand(p, &flags)) | |
5265 | return -ESRCH; | |
5266 | rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur; | |
5267 | unlock_task_sighand(p, &flags); | |
5268 | ||
5269 | /* can't set/change the rt policy */ | |
5270 | if (policy != p->policy && !rlim_rtprio) | |
5271 | return -EPERM; | |
5272 | ||
5273 | /* can't increase priority */ | |
5274 | if (param->sched_priority > p->rt_priority && | |
5275 | param->sched_priority > rlim_rtprio) | |
5276 | return -EPERM; | |
5277 | } | |
dd41f596 IM |
5278 | /* |
5279 | * Like positive nice levels, dont allow tasks to | |
5280 | * move out of SCHED_IDLE either: | |
5281 | */ | |
5282 | if (p->policy == SCHED_IDLE && policy != SCHED_IDLE) | |
5283 | return -EPERM; | |
5fe1d75f | 5284 | |
37e4ab3f OC |
5285 | /* can't change other user's priorities */ |
5286 | if ((current->euid != p->euid) && | |
5287 | (current->euid != p->uid)) | |
5288 | return -EPERM; | |
5289 | } | |
1da177e4 | 5290 | |
b68aa230 PZ |
5291 | #ifdef CONFIG_RT_GROUP_SCHED |
5292 | /* | |
5293 | * Do not allow realtime tasks into groups that have no runtime | |
5294 | * assigned. | |
5295 | */ | |
d0b27fa7 | 5296 | if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0) |
b68aa230 PZ |
5297 | return -EPERM; |
5298 | #endif | |
5299 | ||
1da177e4 LT |
5300 | retval = security_task_setscheduler(p, policy, param); |
5301 | if (retval) | |
5302 | return retval; | |
b29739f9 IM |
5303 | /* |
5304 | * make sure no PI-waiters arrive (or leave) while we are | |
5305 | * changing the priority of the task: | |
5306 | */ | |
5307 | spin_lock_irqsave(&p->pi_lock, flags); | |
1da177e4 LT |
5308 | /* |
5309 | * To be able to change p->policy safely, the apropriate | |
5310 | * runqueue lock must be held. | |
5311 | */ | |
b29739f9 | 5312 | rq = __task_rq_lock(p); |
1da177e4 LT |
5313 | /* recheck policy now with rq lock held */ |
5314 | if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { | |
5315 | policy = oldpolicy = -1; | |
b29739f9 IM |
5316 | __task_rq_unlock(rq); |
5317 | spin_unlock_irqrestore(&p->pi_lock, flags); | |
1da177e4 LT |
5318 | goto recheck; |
5319 | } | |
2daa3577 | 5320 | update_rq_clock(rq); |
dd41f596 | 5321 | on_rq = p->se.on_rq; |
051a1d1a | 5322 | running = task_current(rq, p); |
0e1f3483 | 5323 | if (on_rq) |
2e1cb74a | 5324 | deactivate_task(rq, p, 0); |
0e1f3483 HS |
5325 | if (running) |
5326 | p->sched_class->put_prev_task(rq, p); | |
f6b53205 | 5327 | |
1da177e4 | 5328 | oldprio = p->prio; |
dd41f596 | 5329 | __setscheduler(rq, p, policy, param->sched_priority); |
f6b53205 | 5330 | |
0e1f3483 HS |
5331 | if (running) |
5332 | p->sched_class->set_curr_task(rq); | |
dd41f596 IM |
5333 | if (on_rq) { |
5334 | activate_task(rq, p, 0); | |
cb469845 SR |
5335 | |
5336 | check_class_changed(rq, p, prev_class, oldprio, running); | |
1da177e4 | 5337 | } |
b29739f9 IM |
5338 | __task_rq_unlock(rq); |
5339 | spin_unlock_irqrestore(&p->pi_lock, flags); | |
5340 | ||
95e02ca9 TG |
5341 | rt_mutex_adjust_pi(p); |
5342 | ||
1da177e4 LT |
5343 | return 0; |
5344 | } | |
5345 | EXPORT_SYMBOL_GPL(sched_setscheduler); | |
5346 | ||
95cdf3b7 IM |
5347 | static int |
5348 | do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param) | |
1da177e4 | 5349 | { |
1da177e4 LT |
5350 | struct sched_param lparam; |
5351 | struct task_struct *p; | |
36c8b586 | 5352 | int retval; |
1da177e4 LT |
5353 | |
5354 | if (!param || pid < 0) | |
5355 | return -EINVAL; | |
5356 | if (copy_from_user(&lparam, param, sizeof(struct sched_param))) | |
5357 | return -EFAULT; | |
5fe1d75f ON |
5358 | |
5359 | rcu_read_lock(); | |
5360 | retval = -ESRCH; | |
1da177e4 | 5361 | p = find_process_by_pid(pid); |
5fe1d75f ON |
5362 | if (p != NULL) |
5363 | retval = sched_setscheduler(p, policy, &lparam); | |
5364 | rcu_read_unlock(); | |
36c8b586 | 5365 | |
1da177e4 LT |
5366 | return retval; |
5367 | } | |
5368 | ||
5369 | /** | |
5370 | * sys_sched_setscheduler - set/change the scheduler policy and RT priority | |
5371 | * @pid: the pid in question. | |
5372 | * @policy: new policy. | |
5373 | * @param: structure containing the new RT priority. | |
5374 | */ | |
41a2d6cf IM |
5375 | asmlinkage long |
5376 | sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param) | |
1da177e4 | 5377 | { |
c21761f1 JB |
5378 | /* negative values for policy are not valid */ |
5379 | if (policy < 0) | |
5380 | return -EINVAL; | |
5381 | ||
1da177e4 LT |
5382 | return do_sched_setscheduler(pid, policy, param); |
5383 | } | |
5384 | ||
5385 | /** | |
5386 | * sys_sched_setparam - set/change the RT priority of a thread | |
5387 | * @pid: the pid in question. | |
5388 | * @param: structure containing the new RT priority. | |
5389 | */ | |
5390 | asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param) | |
5391 | { | |
5392 | return do_sched_setscheduler(pid, -1, param); | |
5393 | } | |
5394 | ||
5395 | /** | |
5396 | * sys_sched_getscheduler - get the policy (scheduling class) of a thread | |
5397 | * @pid: the pid in question. | |
5398 | */ | |
5399 | asmlinkage long sys_sched_getscheduler(pid_t pid) | |
5400 | { | |
36c8b586 | 5401 | struct task_struct *p; |
3a5c359a | 5402 | int retval; |
1da177e4 LT |
5403 | |
5404 | if (pid < 0) | |
3a5c359a | 5405 | return -EINVAL; |
1da177e4 LT |
5406 | |
5407 | retval = -ESRCH; | |
5408 | read_lock(&tasklist_lock); | |
5409 | p = find_process_by_pid(pid); | |
5410 | if (p) { | |
5411 | retval = security_task_getscheduler(p); | |
5412 | if (!retval) | |
5413 | retval = p->policy; | |
5414 | } | |
5415 | read_unlock(&tasklist_lock); | |
1da177e4 LT |
5416 | return retval; |
5417 | } | |
5418 | ||
5419 | /** | |
5420 | * sys_sched_getscheduler - get the RT priority of a thread | |
5421 | * @pid: the pid in question. | |
5422 | * @param: structure containing the RT priority. | |
5423 | */ | |
5424 | asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param) | |
5425 | { | |
5426 | struct sched_param lp; | |
36c8b586 | 5427 | struct task_struct *p; |
3a5c359a | 5428 | int retval; |
1da177e4 LT |
5429 | |
5430 | if (!param || pid < 0) | |
3a5c359a | 5431 | return -EINVAL; |
1da177e4 LT |
5432 | |
5433 | read_lock(&tasklist_lock); | |
5434 | p = find_process_by_pid(pid); | |
5435 | retval = -ESRCH; | |
5436 | if (!p) | |
5437 | goto out_unlock; | |
5438 | ||
5439 | retval = security_task_getscheduler(p); | |
5440 | if (retval) | |
5441 | goto out_unlock; | |
5442 | ||
5443 | lp.sched_priority = p->rt_priority; | |
5444 | read_unlock(&tasklist_lock); | |
5445 | ||
5446 | /* | |
5447 | * This one might sleep, we cannot do it with a spinlock held ... | |
5448 | */ | |
5449 | retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0; | |
5450 | ||
1da177e4 LT |
5451 | return retval; |
5452 | ||
5453 | out_unlock: | |
5454 | read_unlock(&tasklist_lock); | |
5455 | return retval; | |
5456 | } | |
5457 | ||
b53e921b | 5458 | long sched_setaffinity(pid_t pid, const cpumask_t *in_mask) |
1da177e4 | 5459 | { |
1da177e4 | 5460 | cpumask_t cpus_allowed; |
b53e921b | 5461 | cpumask_t new_mask = *in_mask; |
36c8b586 IM |
5462 | struct task_struct *p; |
5463 | int retval; | |
1da177e4 | 5464 | |
95402b38 | 5465 | get_online_cpus(); |
1da177e4 LT |
5466 | read_lock(&tasklist_lock); |
5467 | ||
5468 | p = find_process_by_pid(pid); | |
5469 | if (!p) { | |
5470 | read_unlock(&tasklist_lock); | |
95402b38 | 5471 | put_online_cpus(); |
1da177e4 LT |
5472 | return -ESRCH; |
5473 | } | |
5474 | ||
5475 | /* | |
5476 | * It is not safe to call set_cpus_allowed with the | |
41a2d6cf | 5477 | * tasklist_lock held. We will bump the task_struct's |
1da177e4 LT |
5478 | * usage count and then drop tasklist_lock. |
5479 | */ | |
5480 | get_task_struct(p); | |
5481 | read_unlock(&tasklist_lock); | |
5482 | ||
5483 | retval = -EPERM; | |
5484 | if ((current->euid != p->euid) && (current->euid != p->uid) && | |
5485 | !capable(CAP_SYS_NICE)) | |
5486 | goto out_unlock; | |
5487 | ||
e7834f8f DQ |
5488 | retval = security_task_setscheduler(p, 0, NULL); |
5489 | if (retval) | |
5490 | goto out_unlock; | |
5491 | ||
f9a86fcb | 5492 | cpuset_cpus_allowed(p, &cpus_allowed); |
1da177e4 | 5493 | cpus_and(new_mask, new_mask, cpus_allowed); |
8707d8b8 | 5494 | again: |
7c16ec58 | 5495 | retval = set_cpus_allowed_ptr(p, &new_mask); |
1da177e4 | 5496 | |
8707d8b8 | 5497 | if (!retval) { |
f9a86fcb | 5498 | cpuset_cpus_allowed(p, &cpus_allowed); |
8707d8b8 PM |
5499 | if (!cpus_subset(new_mask, cpus_allowed)) { |
5500 | /* | |
5501 | * We must have raced with a concurrent cpuset | |
5502 | * update. Just reset the cpus_allowed to the | |
5503 | * cpuset's cpus_allowed | |
5504 | */ | |
5505 | new_mask = cpus_allowed; | |
5506 | goto again; | |
5507 | } | |
5508 | } | |
1da177e4 LT |
5509 | out_unlock: |
5510 | put_task_struct(p); | |
95402b38 | 5511 | put_online_cpus(); |
1da177e4 LT |
5512 | return retval; |
5513 | } | |
5514 | ||
5515 | static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len, | |
5516 | cpumask_t *new_mask) | |
5517 | { | |
5518 | if (len < sizeof(cpumask_t)) { | |
5519 | memset(new_mask, 0, sizeof(cpumask_t)); | |
5520 | } else if (len > sizeof(cpumask_t)) { | |
5521 | len = sizeof(cpumask_t); | |
5522 | } | |
5523 | return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0; | |
5524 | } | |
5525 | ||
5526 | /** | |
5527 | * sys_sched_setaffinity - set the cpu affinity of a process | |
5528 | * @pid: pid of the process | |
5529 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr | |
5530 | * @user_mask_ptr: user-space pointer to the new cpu mask | |
5531 | */ | |
5532 | asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len, | |
5533 | unsigned long __user *user_mask_ptr) | |
5534 | { | |
5535 | cpumask_t new_mask; | |
5536 | int retval; | |
5537 | ||
5538 | retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask); | |
5539 | if (retval) | |
5540 | return retval; | |
5541 | ||
b53e921b | 5542 | return sched_setaffinity(pid, &new_mask); |
1da177e4 LT |
5543 | } |
5544 | ||
5545 | /* | |
5546 | * Represents all cpu's present in the system | |
5547 | * In systems capable of hotplug, this map could dynamically grow | |
5548 | * as new cpu's are detected in the system via any platform specific | |
5549 | * method, such as ACPI for e.g. | |
5550 | */ | |
5551 | ||
4cef0c61 | 5552 | cpumask_t cpu_present_map __read_mostly; |
1da177e4 LT |
5553 | EXPORT_SYMBOL(cpu_present_map); |
5554 | ||
5555 | #ifndef CONFIG_SMP | |
4cef0c61 | 5556 | cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL; |
e16b38f7 GB |
5557 | EXPORT_SYMBOL(cpu_online_map); |
5558 | ||
4cef0c61 | 5559 | cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL; |
e16b38f7 | 5560 | EXPORT_SYMBOL(cpu_possible_map); |
1da177e4 LT |
5561 | #endif |
5562 | ||
5563 | long sched_getaffinity(pid_t pid, cpumask_t *mask) | |
5564 | { | |
36c8b586 | 5565 | struct task_struct *p; |
1da177e4 | 5566 | int retval; |
1da177e4 | 5567 | |
95402b38 | 5568 | get_online_cpus(); |
1da177e4 LT |
5569 | read_lock(&tasklist_lock); |
5570 | ||
5571 | retval = -ESRCH; | |
5572 | p = find_process_by_pid(pid); | |
5573 | if (!p) | |
5574 | goto out_unlock; | |
5575 | ||
e7834f8f DQ |
5576 | retval = security_task_getscheduler(p); |
5577 | if (retval) | |
5578 | goto out_unlock; | |
5579 | ||
2f7016d9 | 5580 | cpus_and(*mask, p->cpus_allowed, cpu_online_map); |
1da177e4 LT |
5581 | |
5582 | out_unlock: | |
5583 | read_unlock(&tasklist_lock); | |
95402b38 | 5584 | put_online_cpus(); |
1da177e4 | 5585 | |
9531b62f | 5586 | return retval; |
1da177e4 LT |
5587 | } |
5588 | ||
5589 | /** | |
5590 | * sys_sched_getaffinity - get the cpu affinity of a process | |
5591 | * @pid: pid of the process | |
5592 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr | |
5593 | * @user_mask_ptr: user-space pointer to hold the current cpu mask | |
5594 | */ | |
5595 | asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len, | |
5596 | unsigned long __user *user_mask_ptr) | |
5597 | { | |
5598 | int ret; | |
5599 | cpumask_t mask; | |
5600 | ||
5601 | if (len < sizeof(cpumask_t)) | |
5602 | return -EINVAL; | |
5603 | ||
5604 | ret = sched_getaffinity(pid, &mask); | |
5605 | if (ret < 0) | |
5606 | return ret; | |
5607 | ||
5608 | if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t))) | |
5609 | return -EFAULT; | |
5610 | ||
5611 | return sizeof(cpumask_t); | |
5612 | } | |
5613 | ||
5614 | /** | |
5615 | * sys_sched_yield - yield the current processor to other threads. | |
5616 | * | |
dd41f596 IM |
5617 | * This function yields the current CPU to other tasks. If there are no |
5618 | * other threads running on this CPU then this function will return. | |
1da177e4 LT |
5619 | */ |
5620 | asmlinkage long sys_sched_yield(void) | |
5621 | { | |
70b97a7f | 5622 | struct rq *rq = this_rq_lock(); |
1da177e4 | 5623 | |
2d72376b | 5624 | schedstat_inc(rq, yld_count); |
4530d7ab | 5625 | current->sched_class->yield_task(rq); |
1da177e4 LT |
5626 | |
5627 | /* | |
5628 | * Since we are going to call schedule() anyway, there's | |
5629 | * no need to preempt or enable interrupts: | |
5630 | */ | |
5631 | __release(rq->lock); | |
8a25d5de | 5632 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); |
1da177e4 LT |
5633 | _raw_spin_unlock(&rq->lock); |
5634 | preempt_enable_no_resched(); | |
5635 | ||
5636 | schedule(); | |
5637 | ||
5638 | return 0; | |
5639 | } | |
5640 | ||
e7b38404 | 5641 | static void __cond_resched(void) |
1da177e4 | 5642 | { |
8e0a43d8 IM |
5643 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP |
5644 | __might_sleep(__FILE__, __LINE__); | |
5645 | #endif | |
5bbcfd90 IM |
5646 | /* |
5647 | * The BKS might be reacquired before we have dropped | |
5648 | * PREEMPT_ACTIVE, which could trigger a second | |
5649 | * cond_resched() call. | |
5650 | */ | |
1da177e4 LT |
5651 | do { |
5652 | add_preempt_count(PREEMPT_ACTIVE); | |
5653 | schedule(); | |
5654 | sub_preempt_count(PREEMPT_ACTIVE); | |
5655 | } while (need_resched()); | |
5656 | } | |
5657 | ||
02b67cc3 HX |
5658 | #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY) |
5659 | int __sched _cond_resched(void) | |
1da177e4 | 5660 | { |
9414232f IM |
5661 | if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) && |
5662 | system_state == SYSTEM_RUNNING) { | |
1da177e4 LT |
5663 | __cond_resched(); |
5664 | return 1; | |
5665 | } | |
5666 | return 0; | |
5667 | } | |
02b67cc3 HX |
5668 | EXPORT_SYMBOL(_cond_resched); |
5669 | #endif | |
1da177e4 LT |
5670 | |
5671 | /* | |
5672 | * cond_resched_lock() - if a reschedule is pending, drop the given lock, | |
5673 | * call schedule, and on return reacquire the lock. | |
5674 | * | |
41a2d6cf | 5675 | * This works OK both with and without CONFIG_PREEMPT. We do strange low-level |
1da177e4 LT |
5676 | * operations here to prevent schedule() from being called twice (once via |
5677 | * spin_unlock(), once by hand). | |
5678 | */ | |
95cdf3b7 | 5679 | int cond_resched_lock(spinlock_t *lock) |
1da177e4 | 5680 | { |
95c354fe | 5681 | int resched = need_resched() && system_state == SYSTEM_RUNNING; |
6df3cecb JK |
5682 | int ret = 0; |
5683 | ||
95c354fe | 5684 | if (spin_needbreak(lock) || resched) { |
1da177e4 | 5685 | spin_unlock(lock); |
95c354fe NP |
5686 | if (resched && need_resched()) |
5687 | __cond_resched(); | |
5688 | else | |
5689 | cpu_relax(); | |
6df3cecb | 5690 | ret = 1; |
1da177e4 | 5691 | spin_lock(lock); |
1da177e4 | 5692 | } |
6df3cecb | 5693 | return ret; |
1da177e4 | 5694 | } |
1da177e4 LT |
5695 | EXPORT_SYMBOL(cond_resched_lock); |
5696 | ||
5697 | int __sched cond_resched_softirq(void) | |
5698 | { | |
5699 | BUG_ON(!in_softirq()); | |
5700 | ||
9414232f | 5701 | if (need_resched() && system_state == SYSTEM_RUNNING) { |
98d82567 | 5702 | local_bh_enable(); |
1da177e4 LT |
5703 | __cond_resched(); |
5704 | local_bh_disable(); | |
5705 | return 1; | |
5706 | } | |
5707 | return 0; | |
5708 | } | |
1da177e4 LT |
5709 | EXPORT_SYMBOL(cond_resched_softirq); |
5710 | ||
1da177e4 LT |
5711 | /** |
5712 | * yield - yield the current processor to other threads. | |
5713 | * | |
72fd4a35 | 5714 | * This is a shortcut for kernel-space yielding - it marks the |
1da177e4 LT |
5715 | * thread runnable and calls sys_sched_yield(). |
5716 | */ | |
5717 | void __sched yield(void) | |
5718 | { | |
5719 | set_current_state(TASK_RUNNING); | |
5720 | sys_sched_yield(); | |
5721 | } | |
1da177e4 LT |
5722 | EXPORT_SYMBOL(yield); |
5723 | ||
5724 | /* | |
41a2d6cf | 5725 | * This task is about to go to sleep on IO. Increment rq->nr_iowait so |
1da177e4 LT |
5726 | * that process accounting knows that this is a task in IO wait state. |
5727 | * | |
5728 | * But don't do that if it is a deliberate, throttling IO wait (this task | |
5729 | * has set its backing_dev_info: the queue against which it should throttle) | |
5730 | */ | |
5731 | void __sched io_schedule(void) | |
5732 | { | |
70b97a7f | 5733 | struct rq *rq = &__raw_get_cpu_var(runqueues); |
1da177e4 | 5734 | |
0ff92245 | 5735 | delayacct_blkio_start(); |
1da177e4 LT |
5736 | atomic_inc(&rq->nr_iowait); |
5737 | schedule(); | |
5738 | atomic_dec(&rq->nr_iowait); | |
0ff92245 | 5739 | delayacct_blkio_end(); |
1da177e4 | 5740 | } |
1da177e4 LT |
5741 | EXPORT_SYMBOL(io_schedule); |
5742 | ||
5743 | long __sched io_schedule_timeout(long timeout) | |
5744 | { | |
70b97a7f | 5745 | struct rq *rq = &__raw_get_cpu_var(runqueues); |
1da177e4 LT |
5746 | long ret; |
5747 | ||
0ff92245 | 5748 | delayacct_blkio_start(); |
1da177e4 LT |
5749 | atomic_inc(&rq->nr_iowait); |
5750 | ret = schedule_timeout(timeout); | |
5751 | atomic_dec(&rq->nr_iowait); | |
0ff92245 | 5752 | delayacct_blkio_end(); |
1da177e4 LT |
5753 | return ret; |
5754 | } | |
5755 | ||
5756 | /** | |
5757 | * sys_sched_get_priority_max - return maximum RT priority. | |
5758 | * @policy: scheduling class. | |
5759 | * | |
5760 | * this syscall returns the maximum rt_priority that can be used | |
5761 | * by a given scheduling class. | |
5762 | */ | |
5763 | asmlinkage long sys_sched_get_priority_max(int policy) | |
5764 | { | |
5765 | int ret = -EINVAL; | |
5766 | ||
5767 | switch (policy) { | |
5768 | case SCHED_FIFO: | |
5769 | case SCHED_RR: | |
5770 | ret = MAX_USER_RT_PRIO-1; | |
5771 | break; | |
5772 | case SCHED_NORMAL: | |
b0a9499c | 5773 | case SCHED_BATCH: |
dd41f596 | 5774 | case SCHED_IDLE: |
1da177e4 LT |
5775 | ret = 0; |
5776 | break; | |
5777 | } | |
5778 | return ret; | |
5779 | } | |
5780 | ||
5781 | /** | |
5782 | * sys_sched_get_priority_min - return minimum RT priority. | |
5783 | * @policy: scheduling class. | |
5784 | * | |
5785 | * this syscall returns the minimum rt_priority that can be used | |
5786 | * by a given scheduling class. | |
5787 | */ | |
5788 | asmlinkage long sys_sched_get_priority_min(int policy) | |
5789 | { | |
5790 | int ret = -EINVAL; | |
5791 | ||
5792 | switch (policy) { | |
5793 | case SCHED_FIFO: | |
5794 | case SCHED_RR: | |
5795 | ret = 1; | |
5796 | break; | |
5797 | case SCHED_NORMAL: | |
b0a9499c | 5798 | case SCHED_BATCH: |
dd41f596 | 5799 | case SCHED_IDLE: |
1da177e4 LT |
5800 | ret = 0; |
5801 | } | |
5802 | return ret; | |
5803 | } | |
5804 | ||
5805 | /** | |
5806 | * sys_sched_rr_get_interval - return the default timeslice of a process. | |
5807 | * @pid: pid of the process. | |
5808 | * @interval: userspace pointer to the timeslice value. | |
5809 | * | |
5810 | * this syscall writes the default timeslice value of a given process | |
5811 | * into the user-space timespec buffer. A value of '0' means infinity. | |
5812 | */ | |
5813 | asmlinkage | |
5814 | long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval) | |
5815 | { | |
36c8b586 | 5816 | struct task_struct *p; |
a4ec24b4 | 5817 | unsigned int time_slice; |
3a5c359a | 5818 | int retval; |
1da177e4 | 5819 | struct timespec t; |
1da177e4 LT |
5820 | |
5821 | if (pid < 0) | |
3a5c359a | 5822 | return -EINVAL; |
1da177e4 LT |
5823 | |
5824 | retval = -ESRCH; | |
5825 | read_lock(&tasklist_lock); | |
5826 | p = find_process_by_pid(pid); | |
5827 | if (!p) | |
5828 | goto out_unlock; | |
5829 | ||
5830 | retval = security_task_getscheduler(p); | |
5831 | if (retval) | |
5832 | goto out_unlock; | |
5833 | ||
77034937 IM |
5834 | /* |
5835 | * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER | |
5836 | * tasks that are on an otherwise idle runqueue: | |
5837 | */ | |
5838 | time_slice = 0; | |
5839 | if (p->policy == SCHED_RR) { | |
a4ec24b4 | 5840 | time_slice = DEF_TIMESLICE; |
1868f958 | 5841 | } else if (p->policy != SCHED_FIFO) { |
a4ec24b4 DA |
5842 | struct sched_entity *se = &p->se; |
5843 | unsigned long flags; | |
5844 | struct rq *rq; | |
5845 | ||
5846 | rq = task_rq_lock(p, &flags); | |
77034937 IM |
5847 | if (rq->cfs.load.weight) |
5848 | time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se)); | |
a4ec24b4 DA |
5849 | task_rq_unlock(rq, &flags); |
5850 | } | |
1da177e4 | 5851 | read_unlock(&tasklist_lock); |
a4ec24b4 | 5852 | jiffies_to_timespec(time_slice, &t); |
1da177e4 | 5853 | retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0; |
1da177e4 | 5854 | return retval; |
3a5c359a | 5855 | |
1da177e4 LT |
5856 | out_unlock: |
5857 | read_unlock(&tasklist_lock); | |
5858 | return retval; | |
5859 | } | |
5860 | ||
2ed6e34f | 5861 | static const char stat_nam[] = "RSDTtZX"; |
36c8b586 | 5862 | |
82a1fcb9 | 5863 | void sched_show_task(struct task_struct *p) |
1da177e4 | 5864 | { |
1da177e4 | 5865 | unsigned long free = 0; |
36c8b586 | 5866 | unsigned state; |
1da177e4 | 5867 | |
1da177e4 | 5868 | state = p->state ? __ffs(p->state) + 1 : 0; |
cc4ea795 | 5869 | printk(KERN_INFO "%-13.13s %c", p->comm, |
2ed6e34f | 5870 | state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?'); |
4bd77321 | 5871 | #if BITS_PER_LONG == 32 |
1da177e4 | 5872 | if (state == TASK_RUNNING) |
cc4ea795 | 5873 | printk(KERN_CONT " running "); |
1da177e4 | 5874 | else |
cc4ea795 | 5875 | printk(KERN_CONT " %08lx ", thread_saved_pc(p)); |
1da177e4 LT |
5876 | #else |
5877 | if (state == TASK_RUNNING) | |
cc4ea795 | 5878 | printk(KERN_CONT " running task "); |
1da177e4 | 5879 | else |
cc4ea795 | 5880 | printk(KERN_CONT " %016lx ", thread_saved_pc(p)); |
1da177e4 LT |
5881 | #endif |
5882 | #ifdef CONFIG_DEBUG_STACK_USAGE | |
5883 | { | |
10ebffde | 5884 | unsigned long *n = end_of_stack(p); |
1da177e4 LT |
5885 | while (!*n) |
5886 | n++; | |
10ebffde | 5887 | free = (unsigned long)n - (unsigned long)end_of_stack(p); |
1da177e4 LT |
5888 | } |
5889 | #endif | |
ba25f9dc | 5890 | printk(KERN_CONT "%5lu %5d %6d\n", free, |
fcfd50af | 5891 | task_pid_nr(p), task_pid_nr(p->real_parent)); |
1da177e4 | 5892 | |
5fb5e6de | 5893 | show_stack(p, NULL); |
1da177e4 LT |
5894 | } |
5895 | ||
e59e2ae2 | 5896 | void show_state_filter(unsigned long state_filter) |
1da177e4 | 5897 | { |
36c8b586 | 5898 | struct task_struct *g, *p; |
1da177e4 | 5899 | |
4bd77321 IM |
5900 | #if BITS_PER_LONG == 32 |
5901 | printk(KERN_INFO | |
5902 | " task PC stack pid father\n"); | |
1da177e4 | 5903 | #else |
4bd77321 IM |
5904 | printk(KERN_INFO |
5905 | " task PC stack pid father\n"); | |
1da177e4 LT |
5906 | #endif |
5907 | read_lock(&tasklist_lock); | |
5908 | do_each_thread(g, p) { | |
5909 | /* | |
5910 | * reset the NMI-timeout, listing all files on a slow | |
5911 | * console might take alot of time: | |
5912 | */ | |
5913 | touch_nmi_watchdog(); | |
39bc89fd | 5914 | if (!state_filter || (p->state & state_filter)) |
82a1fcb9 | 5915 | sched_show_task(p); |
1da177e4 LT |
5916 | } while_each_thread(g, p); |
5917 | ||
04c9167f JF |
5918 | touch_all_softlockup_watchdogs(); |
5919 | ||
dd41f596 IM |
5920 | #ifdef CONFIG_SCHED_DEBUG |
5921 | sysrq_sched_debug_show(); | |
5922 | #endif | |
1da177e4 | 5923 | read_unlock(&tasklist_lock); |
e59e2ae2 IM |
5924 | /* |
5925 | * Only show locks if all tasks are dumped: | |
5926 | */ | |
5927 | if (state_filter == -1) | |
5928 | debug_show_all_locks(); | |
1da177e4 LT |
5929 | } |
5930 | ||
1df21055 IM |
5931 | void __cpuinit init_idle_bootup_task(struct task_struct *idle) |
5932 | { | |
dd41f596 | 5933 | idle->sched_class = &idle_sched_class; |
1df21055 IM |
5934 | } |
5935 | ||
f340c0d1 IM |
5936 | /** |
5937 | * init_idle - set up an idle thread for a given CPU | |
5938 | * @idle: task in question | |
5939 | * @cpu: cpu the idle task belongs to | |
5940 | * | |
5941 | * NOTE: this function does not set the idle thread's NEED_RESCHED | |
5942 | * flag, to make booting more robust. | |
5943 | */ | |
5c1e1767 | 5944 | void __cpuinit init_idle(struct task_struct *idle, int cpu) |
1da177e4 | 5945 | { |
70b97a7f | 5946 | struct rq *rq = cpu_rq(cpu); |
1da177e4 LT |
5947 | unsigned long flags; |
5948 | ||
dd41f596 IM |
5949 | __sched_fork(idle); |
5950 | idle->se.exec_start = sched_clock(); | |
5951 | ||
b29739f9 | 5952 | idle->prio = idle->normal_prio = MAX_PRIO; |
1da177e4 | 5953 | idle->cpus_allowed = cpumask_of_cpu(cpu); |
dd41f596 | 5954 | __set_task_cpu(idle, cpu); |
1da177e4 LT |
5955 | |
5956 | spin_lock_irqsave(&rq->lock, flags); | |
5957 | rq->curr = rq->idle = idle; | |
4866cde0 NP |
5958 | #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW) |
5959 | idle->oncpu = 1; | |
5960 | #endif | |
1da177e4 LT |
5961 | spin_unlock_irqrestore(&rq->lock, flags); |
5962 | ||
5963 | /* Set the preempt count _outside_ the spinlocks! */ | |
a1261f54 | 5964 | task_thread_info(idle)->preempt_count = 0; |
6478d880 | 5965 | |
dd41f596 IM |
5966 | /* |
5967 | * The idle tasks have their own, simple scheduling class: | |
5968 | */ | |
5969 | idle->sched_class = &idle_sched_class; | |
1da177e4 LT |
5970 | } |
5971 | ||
5972 | /* | |
5973 | * In a system that switches off the HZ timer nohz_cpu_mask | |
5974 | * indicates which cpus entered this state. This is used | |
5975 | * in the rcu update to wait only for active cpus. For system | |
5976 | * which do not switch off the HZ timer nohz_cpu_mask should | |
5977 | * always be CPU_MASK_NONE. | |
5978 | */ | |
5979 | cpumask_t nohz_cpu_mask = CPU_MASK_NONE; | |
5980 | ||
19978ca6 IM |
5981 | /* |
5982 | * Increase the granularity value when there are more CPUs, | |
5983 | * because with more CPUs the 'effective latency' as visible | |
5984 | * to users decreases. But the relationship is not linear, | |
5985 | * so pick a second-best guess by going with the log2 of the | |
5986 | * number of CPUs. | |
5987 | * | |
5988 | * This idea comes from the SD scheduler of Con Kolivas: | |
5989 | */ | |
5990 | static inline void sched_init_granularity(void) | |
5991 | { | |
5992 | unsigned int factor = 1 + ilog2(num_online_cpus()); | |
5993 | const unsigned long limit = 200000000; | |
5994 | ||
5995 | sysctl_sched_min_granularity *= factor; | |
5996 | if (sysctl_sched_min_granularity > limit) | |
5997 | sysctl_sched_min_granularity = limit; | |
5998 | ||
5999 | sysctl_sched_latency *= factor; | |
6000 | if (sysctl_sched_latency > limit) | |
6001 | sysctl_sched_latency = limit; | |
6002 | ||
6003 | sysctl_sched_wakeup_granularity *= factor; | |
19978ca6 IM |
6004 | } |
6005 | ||
1da177e4 LT |
6006 | #ifdef CONFIG_SMP |
6007 | /* | |
6008 | * This is how migration works: | |
6009 | * | |
70b97a7f | 6010 | * 1) we queue a struct migration_req structure in the source CPU's |
1da177e4 LT |
6011 | * runqueue and wake up that CPU's migration thread. |
6012 | * 2) we down() the locked semaphore => thread blocks. | |
6013 | * 3) migration thread wakes up (implicitly it forces the migrated | |
6014 | * thread off the CPU) | |
6015 | * 4) it gets the migration request and checks whether the migrated | |
6016 | * task is still in the wrong runqueue. | |
6017 | * 5) if it's in the wrong runqueue then the migration thread removes | |
6018 | * it and puts it into the right queue. | |
6019 | * 6) migration thread up()s the semaphore. | |
6020 | * 7) we wake up and the migration is done. | |
6021 | */ | |
6022 | ||
6023 | /* | |
6024 | * Change a given task's CPU affinity. Migrate the thread to a | |
6025 | * proper CPU and schedule it away if the CPU it's executing on | |
6026 | * is removed from the allowed bitmask. | |
6027 | * | |
6028 | * NOTE: the caller must have a valid reference to the task, the | |
41a2d6cf | 6029 | * task must not exit() & deallocate itself prematurely. The |
1da177e4 LT |
6030 | * call is not atomic; no spinlocks may be held. |
6031 | */ | |
cd8ba7cd | 6032 | int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask) |
1da177e4 | 6033 | { |
70b97a7f | 6034 | struct migration_req req; |
1da177e4 | 6035 | unsigned long flags; |
70b97a7f | 6036 | struct rq *rq; |
48f24c4d | 6037 | int ret = 0; |
1da177e4 LT |
6038 | |
6039 | rq = task_rq_lock(p, &flags); | |
cd8ba7cd | 6040 | if (!cpus_intersects(*new_mask, cpu_online_map)) { |
1da177e4 LT |
6041 | ret = -EINVAL; |
6042 | goto out; | |
6043 | } | |
6044 | ||
73fe6aae | 6045 | if (p->sched_class->set_cpus_allowed) |
cd8ba7cd | 6046 | p->sched_class->set_cpus_allowed(p, new_mask); |
73fe6aae | 6047 | else { |
cd8ba7cd MT |
6048 | p->cpus_allowed = *new_mask; |
6049 | p->rt.nr_cpus_allowed = cpus_weight(*new_mask); | |
73fe6aae GH |
6050 | } |
6051 | ||
1da177e4 | 6052 | /* Can the task run on the task's current CPU? If so, we're done */ |
cd8ba7cd | 6053 | if (cpu_isset(task_cpu(p), *new_mask)) |
1da177e4 LT |
6054 | goto out; |
6055 | ||
cd8ba7cd | 6056 | if (migrate_task(p, any_online_cpu(*new_mask), &req)) { |
1da177e4 LT |
6057 | /* Need help from migration thread: drop lock and wait. */ |
6058 | task_rq_unlock(rq, &flags); | |
6059 | wake_up_process(rq->migration_thread); | |
6060 | wait_for_completion(&req.done); | |
6061 | tlb_migrate_finish(p->mm); | |
6062 | return 0; | |
6063 | } | |
6064 | out: | |
6065 | task_rq_unlock(rq, &flags); | |
48f24c4d | 6066 | |
1da177e4 LT |
6067 | return ret; |
6068 | } | |
cd8ba7cd | 6069 | EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr); |
1da177e4 LT |
6070 | |
6071 | /* | |
41a2d6cf | 6072 | * Move (not current) task off this cpu, onto dest cpu. We're doing |
1da177e4 LT |
6073 | * this because either it can't run here any more (set_cpus_allowed() |
6074 | * away from this CPU, or CPU going down), or because we're | |
6075 | * attempting to rebalance this task on exec (sched_exec). | |
6076 | * | |
6077 | * So we race with normal scheduler movements, but that's OK, as long | |
6078 | * as the task is no longer on this CPU. | |
efc30814 KK |
6079 | * |
6080 | * Returns non-zero if task was successfully migrated. | |
1da177e4 | 6081 | */ |
efc30814 | 6082 | static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu) |
1da177e4 | 6083 | { |
70b97a7f | 6084 | struct rq *rq_dest, *rq_src; |
dd41f596 | 6085 | int ret = 0, on_rq; |
1da177e4 LT |
6086 | |
6087 | if (unlikely(cpu_is_offline(dest_cpu))) | |
efc30814 | 6088 | return ret; |
1da177e4 LT |
6089 | |
6090 | rq_src = cpu_rq(src_cpu); | |
6091 | rq_dest = cpu_rq(dest_cpu); | |
6092 | ||
6093 | double_rq_lock(rq_src, rq_dest); | |
6094 | /* Already moved. */ | |
6095 | if (task_cpu(p) != src_cpu) | |
6096 | goto out; | |
6097 | /* Affinity changed (again). */ | |
6098 | if (!cpu_isset(dest_cpu, p->cpus_allowed)) | |
6099 | goto out; | |
6100 | ||
dd41f596 | 6101 | on_rq = p->se.on_rq; |
6e82a3be | 6102 | if (on_rq) |
2e1cb74a | 6103 | deactivate_task(rq_src, p, 0); |
6e82a3be | 6104 | |
1da177e4 | 6105 | set_task_cpu(p, dest_cpu); |
dd41f596 IM |
6106 | if (on_rq) { |
6107 | activate_task(rq_dest, p, 0); | |
6108 | check_preempt_curr(rq_dest, p); | |
1da177e4 | 6109 | } |
efc30814 | 6110 | ret = 1; |
1da177e4 LT |
6111 | out: |
6112 | double_rq_unlock(rq_src, rq_dest); | |
efc30814 | 6113 | return ret; |
1da177e4 LT |
6114 | } |
6115 | ||
6116 | /* | |
6117 | * migration_thread - this is a highprio system thread that performs | |
6118 | * thread migration by bumping thread off CPU then 'pushing' onto | |
6119 | * another runqueue. | |
6120 | */ | |
95cdf3b7 | 6121 | static int migration_thread(void *data) |
1da177e4 | 6122 | { |
1da177e4 | 6123 | int cpu = (long)data; |
70b97a7f | 6124 | struct rq *rq; |
1da177e4 LT |
6125 | |
6126 | rq = cpu_rq(cpu); | |
6127 | BUG_ON(rq->migration_thread != current); | |
6128 | ||
6129 | set_current_state(TASK_INTERRUPTIBLE); | |
6130 | while (!kthread_should_stop()) { | |
70b97a7f | 6131 | struct migration_req *req; |
1da177e4 | 6132 | struct list_head *head; |
1da177e4 | 6133 | |
1da177e4 LT |
6134 | spin_lock_irq(&rq->lock); |
6135 | ||
6136 | if (cpu_is_offline(cpu)) { | |
6137 | spin_unlock_irq(&rq->lock); | |
6138 | goto wait_to_die; | |
6139 | } | |
6140 | ||
6141 | if (rq->active_balance) { | |
6142 | active_load_balance(rq, cpu); | |
6143 | rq->active_balance = 0; | |
6144 | } | |
6145 | ||
6146 | head = &rq->migration_queue; | |
6147 | ||
6148 | if (list_empty(head)) { | |
6149 | spin_unlock_irq(&rq->lock); | |
6150 | schedule(); | |
6151 | set_current_state(TASK_INTERRUPTIBLE); | |
6152 | continue; | |
6153 | } | |
70b97a7f | 6154 | req = list_entry(head->next, struct migration_req, list); |
1da177e4 LT |
6155 | list_del_init(head->next); |
6156 | ||
674311d5 NP |
6157 | spin_unlock(&rq->lock); |
6158 | __migrate_task(req->task, cpu, req->dest_cpu); | |
6159 | local_irq_enable(); | |
1da177e4 LT |
6160 | |
6161 | complete(&req->done); | |
6162 | } | |
6163 | __set_current_state(TASK_RUNNING); | |
6164 | return 0; | |
6165 | ||
6166 | wait_to_die: | |
6167 | /* Wait for kthread_stop */ | |
6168 | set_current_state(TASK_INTERRUPTIBLE); | |
6169 | while (!kthread_should_stop()) { | |
6170 | schedule(); | |
6171 | set_current_state(TASK_INTERRUPTIBLE); | |
6172 | } | |
6173 | __set_current_state(TASK_RUNNING); | |
6174 | return 0; | |
6175 | } | |
6176 | ||
6177 | #ifdef CONFIG_HOTPLUG_CPU | |
f7b4cddc ON |
6178 | |
6179 | static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu) | |
6180 | { | |
6181 | int ret; | |
6182 | ||
6183 | local_irq_disable(); | |
6184 | ret = __migrate_task(p, src_cpu, dest_cpu); | |
6185 | local_irq_enable(); | |
6186 | return ret; | |
6187 | } | |
6188 | ||
054b9108 | 6189 | /* |
3a4fa0a2 | 6190 | * Figure out where task on dead CPU should go, use force if necessary. |
054b9108 KK |
6191 | * NOTE: interrupts should be disabled by the caller |
6192 | */ | |
48f24c4d | 6193 | static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p) |
1da177e4 | 6194 | { |
efc30814 | 6195 | unsigned long flags; |
1da177e4 | 6196 | cpumask_t mask; |
70b97a7f IM |
6197 | struct rq *rq; |
6198 | int dest_cpu; | |
1da177e4 | 6199 | |
3a5c359a AK |
6200 | do { |
6201 | /* On same node? */ | |
6202 | mask = node_to_cpumask(cpu_to_node(dead_cpu)); | |
6203 | cpus_and(mask, mask, p->cpus_allowed); | |
6204 | dest_cpu = any_online_cpu(mask); | |
6205 | ||
6206 | /* On any allowed CPU? */ | |
434d53b0 | 6207 | if (dest_cpu >= nr_cpu_ids) |
3a5c359a AK |
6208 | dest_cpu = any_online_cpu(p->cpus_allowed); |
6209 | ||
6210 | /* No more Mr. Nice Guy. */ | |
434d53b0 | 6211 | if (dest_cpu >= nr_cpu_ids) { |
f9a86fcb MT |
6212 | cpumask_t cpus_allowed; |
6213 | ||
6214 | cpuset_cpus_allowed_locked(p, &cpus_allowed); | |
470fd646 CW |
6215 | /* |
6216 | * Try to stay on the same cpuset, where the | |
6217 | * current cpuset may be a subset of all cpus. | |
6218 | * The cpuset_cpus_allowed_locked() variant of | |
41a2d6cf | 6219 | * cpuset_cpus_allowed() will not block. It must be |
470fd646 CW |
6220 | * called within calls to cpuset_lock/cpuset_unlock. |
6221 | */ | |
3a5c359a | 6222 | rq = task_rq_lock(p, &flags); |
470fd646 | 6223 | p->cpus_allowed = cpus_allowed; |
3a5c359a AK |
6224 | dest_cpu = any_online_cpu(p->cpus_allowed); |
6225 | task_rq_unlock(rq, &flags); | |
1da177e4 | 6226 | |
3a5c359a AK |
6227 | /* |
6228 | * Don't tell them about moving exiting tasks or | |
6229 | * kernel threads (both mm NULL), since they never | |
6230 | * leave kernel. | |
6231 | */ | |
41a2d6cf | 6232 | if (p->mm && printk_ratelimit()) { |
3a5c359a AK |
6233 | printk(KERN_INFO "process %d (%s) no " |
6234 | "longer affine to cpu%d\n", | |
41a2d6cf IM |
6235 | task_pid_nr(p), p->comm, dead_cpu); |
6236 | } | |
3a5c359a | 6237 | } |
f7b4cddc | 6238 | } while (!__migrate_task_irq(p, dead_cpu, dest_cpu)); |
1da177e4 LT |
6239 | } |
6240 | ||
6241 | /* | |
6242 | * While a dead CPU has no uninterruptible tasks queued at this point, | |
6243 | * it might still have a nonzero ->nr_uninterruptible counter, because | |
6244 | * for performance reasons the counter is not stricly tracking tasks to | |
6245 | * their home CPUs. So we just add the counter to another CPU's counter, | |
6246 | * to keep the global sum constant after CPU-down: | |
6247 | */ | |
70b97a7f | 6248 | static void migrate_nr_uninterruptible(struct rq *rq_src) |
1da177e4 | 6249 | { |
7c16ec58 | 6250 | struct rq *rq_dest = cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR)); |
1da177e4 LT |
6251 | unsigned long flags; |
6252 | ||
6253 | local_irq_save(flags); | |
6254 | double_rq_lock(rq_src, rq_dest); | |
6255 | rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible; | |
6256 | rq_src->nr_uninterruptible = 0; | |
6257 | double_rq_unlock(rq_src, rq_dest); | |
6258 | local_irq_restore(flags); | |
6259 | } | |
6260 | ||
6261 | /* Run through task list and migrate tasks from the dead cpu. */ | |
6262 | static void migrate_live_tasks(int src_cpu) | |
6263 | { | |
48f24c4d | 6264 | struct task_struct *p, *t; |
1da177e4 | 6265 | |
f7b4cddc | 6266 | read_lock(&tasklist_lock); |
1da177e4 | 6267 | |
48f24c4d IM |
6268 | do_each_thread(t, p) { |
6269 | if (p == current) | |
1da177e4 LT |
6270 | continue; |
6271 | ||
48f24c4d IM |
6272 | if (task_cpu(p) == src_cpu) |
6273 | move_task_off_dead_cpu(src_cpu, p); | |
6274 | } while_each_thread(t, p); | |
1da177e4 | 6275 | |
f7b4cddc | 6276 | read_unlock(&tasklist_lock); |
1da177e4 LT |
6277 | } |
6278 | ||
dd41f596 IM |
6279 | /* |
6280 | * Schedules idle task to be the next runnable task on current CPU. | |
94bc9a7b DA |
6281 | * It does so by boosting its priority to highest possible. |
6282 | * Used by CPU offline code. | |
1da177e4 LT |
6283 | */ |
6284 | void sched_idle_next(void) | |
6285 | { | |
48f24c4d | 6286 | int this_cpu = smp_processor_id(); |
70b97a7f | 6287 | struct rq *rq = cpu_rq(this_cpu); |
1da177e4 LT |
6288 | struct task_struct *p = rq->idle; |
6289 | unsigned long flags; | |
6290 | ||
6291 | /* cpu has to be offline */ | |
48f24c4d | 6292 | BUG_ON(cpu_online(this_cpu)); |
1da177e4 | 6293 | |
48f24c4d IM |
6294 | /* |
6295 | * Strictly not necessary since rest of the CPUs are stopped by now | |
6296 | * and interrupts disabled on the current cpu. | |
1da177e4 LT |
6297 | */ |
6298 | spin_lock_irqsave(&rq->lock, flags); | |
6299 | ||
dd41f596 | 6300 | __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1); |
48f24c4d | 6301 | |
94bc9a7b DA |
6302 | update_rq_clock(rq); |
6303 | activate_task(rq, p, 0); | |
1da177e4 LT |
6304 | |
6305 | spin_unlock_irqrestore(&rq->lock, flags); | |
6306 | } | |
6307 | ||
48f24c4d IM |
6308 | /* |
6309 | * Ensures that the idle task is using init_mm right before its cpu goes | |
1da177e4 LT |
6310 | * offline. |
6311 | */ | |
6312 | void idle_task_exit(void) | |
6313 | { | |
6314 | struct mm_struct *mm = current->active_mm; | |
6315 | ||
6316 | BUG_ON(cpu_online(smp_processor_id())); | |
6317 | ||
6318 | if (mm != &init_mm) | |
6319 | switch_mm(mm, &init_mm, current); | |
6320 | mmdrop(mm); | |
6321 | } | |
6322 | ||
054b9108 | 6323 | /* called under rq->lock with disabled interrupts */ |
36c8b586 | 6324 | static void migrate_dead(unsigned int dead_cpu, struct task_struct *p) |
1da177e4 | 6325 | { |
70b97a7f | 6326 | struct rq *rq = cpu_rq(dead_cpu); |
1da177e4 LT |
6327 | |
6328 | /* Must be exiting, otherwise would be on tasklist. */ | |
270f722d | 6329 | BUG_ON(!p->exit_state); |
1da177e4 LT |
6330 | |
6331 | /* Cannot have done final schedule yet: would have vanished. */ | |
c394cc9f | 6332 | BUG_ON(p->state == TASK_DEAD); |
1da177e4 | 6333 | |
48f24c4d | 6334 | get_task_struct(p); |
1da177e4 LT |
6335 | |
6336 | /* | |
6337 | * Drop lock around migration; if someone else moves it, | |
41a2d6cf | 6338 | * that's OK. No task can be added to this CPU, so iteration is |
1da177e4 LT |
6339 | * fine. |
6340 | */ | |
f7b4cddc | 6341 | spin_unlock_irq(&rq->lock); |
48f24c4d | 6342 | move_task_off_dead_cpu(dead_cpu, p); |
f7b4cddc | 6343 | spin_lock_irq(&rq->lock); |
1da177e4 | 6344 | |
48f24c4d | 6345 | put_task_struct(p); |
1da177e4 LT |
6346 | } |
6347 | ||
6348 | /* release_task() removes task from tasklist, so we won't find dead tasks. */ | |
6349 | static void migrate_dead_tasks(unsigned int dead_cpu) | |
6350 | { | |
70b97a7f | 6351 | struct rq *rq = cpu_rq(dead_cpu); |
dd41f596 | 6352 | struct task_struct *next; |
48f24c4d | 6353 | |
dd41f596 IM |
6354 | for ( ; ; ) { |
6355 | if (!rq->nr_running) | |
6356 | break; | |
a8e504d2 | 6357 | update_rq_clock(rq); |
ff95f3df | 6358 | next = pick_next_task(rq, rq->curr); |
dd41f596 IM |
6359 | if (!next) |
6360 | break; | |
6361 | migrate_dead(dead_cpu, next); | |
e692ab53 | 6362 | |
1da177e4 LT |
6363 | } |
6364 | } | |
6365 | #endif /* CONFIG_HOTPLUG_CPU */ | |
6366 | ||
e692ab53 NP |
6367 | #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL) |
6368 | ||
6369 | static struct ctl_table sd_ctl_dir[] = { | |
e0361851 AD |
6370 | { |
6371 | .procname = "sched_domain", | |
c57baf1e | 6372 | .mode = 0555, |
e0361851 | 6373 | }, |
38605cae | 6374 | {0, }, |
e692ab53 NP |
6375 | }; |
6376 | ||
6377 | static struct ctl_table sd_ctl_root[] = { | |
e0361851 | 6378 | { |
c57baf1e | 6379 | .ctl_name = CTL_KERN, |
e0361851 | 6380 | .procname = "kernel", |
c57baf1e | 6381 | .mode = 0555, |
e0361851 AD |
6382 | .child = sd_ctl_dir, |
6383 | }, | |
38605cae | 6384 | {0, }, |
e692ab53 NP |
6385 | }; |
6386 | ||
6387 | static struct ctl_table *sd_alloc_ctl_entry(int n) | |
6388 | { | |
6389 | struct ctl_table *entry = | |
5cf9f062 | 6390 | kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL); |
e692ab53 | 6391 | |
e692ab53 NP |
6392 | return entry; |
6393 | } | |
6394 | ||
6382bc90 MM |
6395 | static void sd_free_ctl_entry(struct ctl_table **tablep) |
6396 | { | |
cd790076 | 6397 | struct ctl_table *entry; |
6382bc90 | 6398 | |
cd790076 MM |
6399 | /* |
6400 | * In the intermediate directories, both the child directory and | |
6401 | * procname are dynamically allocated and could fail but the mode | |
41a2d6cf | 6402 | * will always be set. In the lowest directory the names are |
cd790076 MM |
6403 | * static strings and all have proc handlers. |
6404 | */ | |
6405 | for (entry = *tablep; entry->mode; entry++) { | |
6382bc90 MM |
6406 | if (entry->child) |
6407 | sd_free_ctl_entry(&entry->child); | |
cd790076 MM |
6408 | if (entry->proc_handler == NULL) |
6409 | kfree(entry->procname); | |
6410 | } | |
6382bc90 MM |
6411 | |
6412 | kfree(*tablep); | |
6413 | *tablep = NULL; | |
6414 | } | |
6415 | ||
e692ab53 | 6416 | static void |
e0361851 | 6417 | set_table_entry(struct ctl_table *entry, |
e692ab53 NP |
6418 | const char *procname, void *data, int maxlen, |
6419 | mode_t mode, proc_handler *proc_handler) | |
6420 | { | |
e692ab53 NP |
6421 | entry->procname = procname; |
6422 | entry->data = data; | |
6423 | entry->maxlen = maxlen; | |
6424 | entry->mode = mode; | |
6425 | entry->proc_handler = proc_handler; | |
6426 | } | |
6427 | ||
6428 | static struct ctl_table * | |
6429 | sd_alloc_ctl_domain_table(struct sched_domain *sd) | |
6430 | { | |
ace8b3d6 | 6431 | struct ctl_table *table = sd_alloc_ctl_entry(12); |
e692ab53 | 6432 | |
ad1cdc1d MM |
6433 | if (table == NULL) |
6434 | return NULL; | |
6435 | ||
e0361851 | 6436 | set_table_entry(&table[0], "min_interval", &sd->min_interval, |
e692ab53 | 6437 | sizeof(long), 0644, proc_doulongvec_minmax); |
e0361851 | 6438 | set_table_entry(&table[1], "max_interval", &sd->max_interval, |
e692ab53 | 6439 | sizeof(long), 0644, proc_doulongvec_minmax); |
e0361851 | 6440 | set_table_entry(&table[2], "busy_idx", &sd->busy_idx, |
e692ab53 | 6441 | sizeof(int), 0644, proc_dointvec_minmax); |
e0361851 | 6442 | set_table_entry(&table[3], "idle_idx", &sd->idle_idx, |
e692ab53 | 6443 | sizeof(int), 0644, proc_dointvec_minmax); |
e0361851 | 6444 | set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx, |
e692ab53 | 6445 | sizeof(int), 0644, proc_dointvec_minmax); |
e0361851 | 6446 | set_table_entry(&table[5], "wake_idx", &sd->wake_idx, |
e692ab53 | 6447 | sizeof(int), 0644, proc_dointvec_minmax); |
e0361851 | 6448 | set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx, |
e692ab53 | 6449 | sizeof(int), 0644, proc_dointvec_minmax); |
e0361851 | 6450 | set_table_entry(&table[7], "busy_factor", &sd->busy_factor, |
e692ab53 | 6451 | sizeof(int), 0644, proc_dointvec_minmax); |
e0361851 | 6452 | set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct, |
e692ab53 | 6453 | sizeof(int), 0644, proc_dointvec_minmax); |
ace8b3d6 | 6454 | set_table_entry(&table[9], "cache_nice_tries", |
e692ab53 NP |
6455 | &sd->cache_nice_tries, |
6456 | sizeof(int), 0644, proc_dointvec_minmax); | |
ace8b3d6 | 6457 | set_table_entry(&table[10], "flags", &sd->flags, |
e692ab53 | 6458 | sizeof(int), 0644, proc_dointvec_minmax); |
6323469f | 6459 | /* &table[11] is terminator */ |
e692ab53 NP |
6460 | |
6461 | return table; | |
6462 | } | |
6463 | ||
9a4e7159 | 6464 | static ctl_table *sd_alloc_ctl_cpu_table(int cpu) |
e692ab53 NP |
6465 | { |
6466 | struct ctl_table *entry, *table; | |
6467 | struct sched_domain *sd; | |
6468 | int domain_num = 0, i; | |
6469 | char buf[32]; | |
6470 | ||
6471 | for_each_domain(cpu, sd) | |
6472 | domain_num++; | |
6473 | entry = table = sd_alloc_ctl_entry(domain_num + 1); | |
ad1cdc1d MM |
6474 | if (table == NULL) |
6475 | return NULL; | |
e692ab53 NP |
6476 | |
6477 | i = 0; | |
6478 | for_each_domain(cpu, sd) { | |
6479 | snprintf(buf, 32, "domain%d", i); | |
e692ab53 | 6480 | entry->procname = kstrdup(buf, GFP_KERNEL); |
c57baf1e | 6481 | entry->mode = 0555; |
e692ab53 NP |
6482 | entry->child = sd_alloc_ctl_domain_table(sd); |
6483 | entry++; | |
6484 | i++; | |
6485 | } | |
6486 | return table; | |
6487 | } | |
6488 | ||
6489 | static struct ctl_table_header *sd_sysctl_header; | |
6382bc90 | 6490 | static void register_sched_domain_sysctl(void) |
e692ab53 NP |
6491 | { |
6492 | int i, cpu_num = num_online_cpus(); | |
6493 | struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1); | |
6494 | char buf[32]; | |
6495 | ||
7378547f MM |
6496 | WARN_ON(sd_ctl_dir[0].child); |
6497 | sd_ctl_dir[0].child = entry; | |
6498 | ||
ad1cdc1d MM |
6499 | if (entry == NULL) |
6500 | return; | |
6501 | ||
97b6ea7b | 6502 | for_each_online_cpu(i) { |
e692ab53 | 6503 | snprintf(buf, 32, "cpu%d", i); |
e692ab53 | 6504 | entry->procname = kstrdup(buf, GFP_KERNEL); |
c57baf1e | 6505 | entry->mode = 0555; |
e692ab53 | 6506 | entry->child = sd_alloc_ctl_cpu_table(i); |
97b6ea7b | 6507 | entry++; |
e692ab53 | 6508 | } |
7378547f MM |
6509 | |
6510 | WARN_ON(sd_sysctl_header); | |
e692ab53 NP |
6511 | sd_sysctl_header = register_sysctl_table(sd_ctl_root); |
6512 | } | |
6382bc90 | 6513 | |
7378547f | 6514 | /* may be called multiple times per register */ |
6382bc90 MM |
6515 | static void unregister_sched_domain_sysctl(void) |
6516 | { | |
7378547f MM |
6517 | if (sd_sysctl_header) |
6518 | unregister_sysctl_table(sd_sysctl_header); | |
6382bc90 | 6519 | sd_sysctl_header = NULL; |
7378547f MM |
6520 | if (sd_ctl_dir[0].child) |
6521 | sd_free_ctl_entry(&sd_ctl_dir[0].child); | |
6382bc90 | 6522 | } |
e692ab53 | 6523 | #else |
6382bc90 MM |
6524 | static void register_sched_domain_sysctl(void) |
6525 | { | |
6526 | } | |
6527 | static void unregister_sched_domain_sysctl(void) | |
e692ab53 NP |
6528 | { |
6529 | } | |
6530 | #endif | |
6531 | ||
1da177e4 LT |
6532 | /* |
6533 | * migration_call - callback that gets triggered when a CPU is added. | |
6534 | * Here we can start up the necessary migration thread for the new CPU. | |
6535 | */ | |
48f24c4d IM |
6536 | static int __cpuinit |
6537 | migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu) | |
1da177e4 | 6538 | { |
1da177e4 | 6539 | struct task_struct *p; |
48f24c4d | 6540 | int cpu = (long)hcpu; |
1da177e4 | 6541 | unsigned long flags; |
70b97a7f | 6542 | struct rq *rq; |
1da177e4 LT |
6543 | |
6544 | switch (action) { | |
5be9361c | 6545 | |
1da177e4 | 6546 | case CPU_UP_PREPARE: |
8bb78442 | 6547 | case CPU_UP_PREPARE_FROZEN: |
dd41f596 | 6548 | p = kthread_create(migration_thread, hcpu, "migration/%d", cpu); |
1da177e4 LT |
6549 | if (IS_ERR(p)) |
6550 | return NOTIFY_BAD; | |
1da177e4 LT |
6551 | kthread_bind(p, cpu); |
6552 | /* Must be high prio: stop_machine expects to yield to it. */ | |
6553 | rq = task_rq_lock(p, &flags); | |
dd41f596 | 6554 | __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1); |
1da177e4 LT |
6555 | task_rq_unlock(rq, &flags); |
6556 | cpu_rq(cpu)->migration_thread = p; | |
6557 | break; | |
48f24c4d | 6558 | |
1da177e4 | 6559 | case CPU_ONLINE: |
8bb78442 | 6560 | case CPU_ONLINE_FROZEN: |
3a4fa0a2 | 6561 | /* Strictly unnecessary, as first user will wake it. */ |
1da177e4 | 6562 | wake_up_process(cpu_rq(cpu)->migration_thread); |
1f94ef59 GH |
6563 | |
6564 | /* Update our root-domain */ | |
6565 | rq = cpu_rq(cpu); | |
6566 | spin_lock_irqsave(&rq->lock, flags); | |
6567 | if (rq->rd) { | |
6568 | BUG_ON(!cpu_isset(cpu, rq->rd->span)); | |
6569 | cpu_set(cpu, rq->rd->online); | |
6570 | } | |
6571 | spin_unlock_irqrestore(&rq->lock, flags); | |
1da177e4 | 6572 | break; |
48f24c4d | 6573 | |
1da177e4 LT |
6574 | #ifdef CONFIG_HOTPLUG_CPU |
6575 | case CPU_UP_CANCELED: | |
8bb78442 | 6576 | case CPU_UP_CANCELED_FROZEN: |
fc75cdfa HC |
6577 | if (!cpu_rq(cpu)->migration_thread) |
6578 | break; | |
41a2d6cf | 6579 | /* Unbind it from offline cpu so it can run. Fall thru. */ |
a4c4af7c HC |
6580 | kthread_bind(cpu_rq(cpu)->migration_thread, |
6581 | any_online_cpu(cpu_online_map)); | |
1da177e4 LT |
6582 | kthread_stop(cpu_rq(cpu)->migration_thread); |
6583 | cpu_rq(cpu)->migration_thread = NULL; | |
6584 | break; | |
48f24c4d | 6585 | |
1da177e4 | 6586 | case CPU_DEAD: |
8bb78442 | 6587 | case CPU_DEAD_FROZEN: |
470fd646 | 6588 | cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */ |
1da177e4 LT |
6589 | migrate_live_tasks(cpu); |
6590 | rq = cpu_rq(cpu); | |
6591 | kthread_stop(rq->migration_thread); | |
6592 | rq->migration_thread = NULL; | |
6593 | /* Idle task back to normal (off runqueue, low prio) */ | |
d2da272a | 6594 | spin_lock_irq(&rq->lock); |
a8e504d2 | 6595 | update_rq_clock(rq); |
2e1cb74a | 6596 | deactivate_task(rq, rq->idle, 0); |
1da177e4 | 6597 | rq->idle->static_prio = MAX_PRIO; |
dd41f596 IM |
6598 | __setscheduler(rq, rq->idle, SCHED_NORMAL, 0); |
6599 | rq->idle->sched_class = &idle_sched_class; | |
1da177e4 | 6600 | migrate_dead_tasks(cpu); |
d2da272a | 6601 | spin_unlock_irq(&rq->lock); |
470fd646 | 6602 | cpuset_unlock(); |
1da177e4 LT |
6603 | migrate_nr_uninterruptible(rq); |
6604 | BUG_ON(rq->nr_running != 0); | |
6605 | ||
41a2d6cf IM |
6606 | /* |
6607 | * No need to migrate the tasks: it was best-effort if | |
6608 | * they didn't take sched_hotcpu_mutex. Just wake up | |
6609 | * the requestors. | |
6610 | */ | |
1da177e4 LT |
6611 | spin_lock_irq(&rq->lock); |
6612 | while (!list_empty(&rq->migration_queue)) { | |
70b97a7f IM |
6613 | struct migration_req *req; |
6614 | ||
1da177e4 | 6615 | req = list_entry(rq->migration_queue.next, |
70b97a7f | 6616 | struct migration_req, list); |
1da177e4 LT |
6617 | list_del_init(&req->list); |
6618 | complete(&req->done); | |
6619 | } | |
6620 | spin_unlock_irq(&rq->lock); | |
6621 | break; | |
57d885fe | 6622 | |
08f503b0 GH |
6623 | case CPU_DYING: |
6624 | case CPU_DYING_FROZEN: | |
57d885fe GH |
6625 | /* Update our root-domain */ |
6626 | rq = cpu_rq(cpu); | |
6627 | spin_lock_irqsave(&rq->lock, flags); | |
6628 | if (rq->rd) { | |
6629 | BUG_ON(!cpu_isset(cpu, rq->rd->span)); | |
6630 | cpu_clear(cpu, rq->rd->online); | |
6631 | } | |
6632 | spin_unlock_irqrestore(&rq->lock, flags); | |
6633 | break; | |
1da177e4 LT |
6634 | #endif |
6635 | } | |
6636 | return NOTIFY_OK; | |
6637 | } | |
6638 | ||
6639 | /* Register at highest priority so that task migration (migrate_all_tasks) | |
6640 | * happens before everything else. | |
6641 | */ | |
26c2143b | 6642 | static struct notifier_block __cpuinitdata migration_notifier = { |
1da177e4 LT |
6643 | .notifier_call = migration_call, |
6644 | .priority = 10 | |
6645 | }; | |
6646 | ||
e6fe6649 | 6647 | void __init migration_init(void) |
1da177e4 LT |
6648 | { |
6649 | void *cpu = (void *)(long)smp_processor_id(); | |
07dccf33 | 6650 | int err; |
48f24c4d IM |
6651 | |
6652 | /* Start one for the boot CPU: */ | |
07dccf33 AM |
6653 | err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu); |
6654 | BUG_ON(err == NOTIFY_BAD); | |
1da177e4 LT |
6655 | migration_call(&migration_notifier, CPU_ONLINE, cpu); |
6656 | register_cpu_notifier(&migration_notifier); | |
1da177e4 LT |
6657 | } |
6658 | #endif | |
6659 | ||
6660 | #ifdef CONFIG_SMP | |
476f3534 | 6661 | |
3e9830dc | 6662 | #ifdef CONFIG_SCHED_DEBUG |
4dcf6aff | 6663 | |
7c16ec58 MT |
6664 | static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level, |
6665 | cpumask_t *groupmask) | |
1da177e4 | 6666 | { |
4dcf6aff | 6667 | struct sched_group *group = sd->groups; |
434d53b0 | 6668 | char str[256]; |
1da177e4 | 6669 | |
434d53b0 | 6670 | cpulist_scnprintf(str, sizeof(str), sd->span); |
7c16ec58 | 6671 | cpus_clear(*groupmask); |
4dcf6aff IM |
6672 | |
6673 | printk(KERN_DEBUG "%*s domain %d: ", level, "", level); | |
6674 | ||
6675 | if (!(sd->flags & SD_LOAD_BALANCE)) { | |
6676 | printk("does not load-balance\n"); | |
6677 | if (sd->parent) | |
6678 | printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain" | |
6679 | " has parent"); | |
6680 | return -1; | |
41c7ce9a NP |
6681 | } |
6682 | ||
4dcf6aff IM |
6683 | printk(KERN_CONT "span %s\n", str); |
6684 | ||
6685 | if (!cpu_isset(cpu, sd->span)) { | |
6686 | printk(KERN_ERR "ERROR: domain->span does not contain " | |
6687 | "CPU%d\n", cpu); | |
6688 | } | |
6689 | if (!cpu_isset(cpu, group->cpumask)) { | |
6690 | printk(KERN_ERR "ERROR: domain->groups does not contain" | |
6691 | " CPU%d\n", cpu); | |
6692 | } | |
1da177e4 | 6693 | |
4dcf6aff | 6694 | printk(KERN_DEBUG "%*s groups:", level + 1, ""); |
1da177e4 | 6695 | do { |
4dcf6aff IM |
6696 | if (!group) { |
6697 | printk("\n"); | |
6698 | printk(KERN_ERR "ERROR: group is NULL\n"); | |
1da177e4 LT |
6699 | break; |
6700 | } | |
6701 | ||
4dcf6aff IM |
6702 | if (!group->__cpu_power) { |
6703 | printk(KERN_CONT "\n"); | |
6704 | printk(KERN_ERR "ERROR: domain->cpu_power not " | |
6705 | "set\n"); | |
6706 | break; | |
6707 | } | |
1da177e4 | 6708 | |
4dcf6aff IM |
6709 | if (!cpus_weight(group->cpumask)) { |
6710 | printk(KERN_CONT "\n"); | |
6711 | printk(KERN_ERR "ERROR: empty group\n"); | |
6712 | break; | |
6713 | } | |
1da177e4 | 6714 | |
7c16ec58 | 6715 | if (cpus_intersects(*groupmask, group->cpumask)) { |
4dcf6aff IM |
6716 | printk(KERN_CONT "\n"); |
6717 | printk(KERN_ERR "ERROR: repeated CPUs\n"); | |
6718 | break; | |
6719 | } | |
1da177e4 | 6720 | |
7c16ec58 | 6721 | cpus_or(*groupmask, *groupmask, group->cpumask); |
1da177e4 | 6722 | |
434d53b0 | 6723 | cpulist_scnprintf(str, sizeof(str), group->cpumask); |
4dcf6aff | 6724 | printk(KERN_CONT " %s", str); |
1da177e4 | 6725 | |
4dcf6aff IM |
6726 | group = group->next; |
6727 | } while (group != sd->groups); | |
6728 | printk(KERN_CONT "\n"); | |
1da177e4 | 6729 | |
7c16ec58 | 6730 | if (!cpus_equal(sd->span, *groupmask)) |
4dcf6aff | 6731 | printk(KERN_ERR "ERROR: groups don't span domain->span\n"); |
1da177e4 | 6732 | |
7c16ec58 | 6733 | if (sd->parent && !cpus_subset(*groupmask, sd->parent->span)) |
4dcf6aff IM |
6734 | printk(KERN_ERR "ERROR: parent span is not a superset " |
6735 | "of domain->span\n"); | |
6736 | return 0; | |
6737 | } | |
1da177e4 | 6738 | |
4dcf6aff IM |
6739 | static void sched_domain_debug(struct sched_domain *sd, int cpu) |
6740 | { | |
7c16ec58 | 6741 | cpumask_t *groupmask; |
4dcf6aff | 6742 | int level = 0; |
1da177e4 | 6743 | |
4dcf6aff IM |
6744 | if (!sd) { |
6745 | printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu); | |
6746 | return; | |
6747 | } | |
1da177e4 | 6748 | |
4dcf6aff IM |
6749 | printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu); |
6750 | ||
7c16ec58 MT |
6751 | groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL); |
6752 | if (!groupmask) { | |
6753 | printk(KERN_DEBUG "Cannot load-balance (out of memory)\n"); | |
6754 | return; | |
6755 | } | |
6756 | ||
4dcf6aff | 6757 | for (;;) { |
7c16ec58 | 6758 | if (sched_domain_debug_one(sd, cpu, level, groupmask)) |
4dcf6aff | 6759 | break; |
1da177e4 LT |
6760 | level++; |
6761 | sd = sd->parent; | |
33859f7f | 6762 | if (!sd) |
4dcf6aff IM |
6763 | break; |
6764 | } | |
7c16ec58 | 6765 | kfree(groupmask); |
1da177e4 LT |
6766 | } |
6767 | #else | |
48f24c4d | 6768 | # define sched_domain_debug(sd, cpu) do { } while (0) |
1da177e4 LT |
6769 | #endif |
6770 | ||
1a20ff27 | 6771 | static int sd_degenerate(struct sched_domain *sd) |
245af2c7 SS |
6772 | { |
6773 | if (cpus_weight(sd->span) == 1) | |
6774 | return 1; | |
6775 | ||
6776 | /* Following flags need at least 2 groups */ | |
6777 | if (sd->flags & (SD_LOAD_BALANCE | | |
6778 | SD_BALANCE_NEWIDLE | | |
6779 | SD_BALANCE_FORK | | |
89c4710e SS |
6780 | SD_BALANCE_EXEC | |
6781 | SD_SHARE_CPUPOWER | | |
6782 | SD_SHARE_PKG_RESOURCES)) { | |
245af2c7 SS |
6783 | if (sd->groups != sd->groups->next) |
6784 | return 0; | |
6785 | } | |
6786 | ||
6787 | /* Following flags don't use groups */ | |
6788 | if (sd->flags & (SD_WAKE_IDLE | | |
6789 | SD_WAKE_AFFINE | | |
6790 | SD_WAKE_BALANCE)) | |
6791 | return 0; | |
6792 | ||
6793 | return 1; | |
6794 | } | |
6795 | ||
48f24c4d IM |
6796 | static int |
6797 | sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent) | |
245af2c7 SS |
6798 | { |
6799 | unsigned long cflags = sd->flags, pflags = parent->flags; | |
6800 | ||
6801 | if (sd_degenerate(parent)) | |
6802 | return 1; | |
6803 | ||
6804 | if (!cpus_equal(sd->span, parent->span)) | |
6805 | return 0; | |
6806 | ||
6807 | /* Does parent contain flags not in child? */ | |
6808 | /* WAKE_BALANCE is a subset of WAKE_AFFINE */ | |
6809 | if (cflags & SD_WAKE_AFFINE) | |
6810 | pflags &= ~SD_WAKE_BALANCE; | |
6811 | /* Flags needing groups don't count if only 1 group in parent */ | |
6812 | if (parent->groups == parent->groups->next) { | |
6813 | pflags &= ~(SD_LOAD_BALANCE | | |
6814 | SD_BALANCE_NEWIDLE | | |
6815 | SD_BALANCE_FORK | | |
89c4710e SS |
6816 | SD_BALANCE_EXEC | |
6817 | SD_SHARE_CPUPOWER | | |
6818 | SD_SHARE_PKG_RESOURCES); | |
245af2c7 SS |
6819 | } |
6820 | if (~cflags & pflags) | |
6821 | return 0; | |
6822 | ||
6823 | return 1; | |
6824 | } | |
6825 | ||
57d885fe GH |
6826 | static void rq_attach_root(struct rq *rq, struct root_domain *rd) |
6827 | { | |
6828 | unsigned long flags; | |
6829 | const struct sched_class *class; | |
6830 | ||
6831 | spin_lock_irqsave(&rq->lock, flags); | |
6832 | ||
6833 | if (rq->rd) { | |
6834 | struct root_domain *old_rd = rq->rd; | |
6835 | ||
0eab9146 | 6836 | for (class = sched_class_highest; class; class = class->next) { |
57d885fe GH |
6837 | if (class->leave_domain) |
6838 | class->leave_domain(rq); | |
0eab9146 | 6839 | } |
57d885fe | 6840 | |
dc938520 GH |
6841 | cpu_clear(rq->cpu, old_rd->span); |
6842 | cpu_clear(rq->cpu, old_rd->online); | |
6843 | ||
57d885fe GH |
6844 | if (atomic_dec_and_test(&old_rd->refcount)) |
6845 | kfree(old_rd); | |
6846 | } | |
6847 | ||
6848 | atomic_inc(&rd->refcount); | |
6849 | rq->rd = rd; | |
6850 | ||
dc938520 | 6851 | cpu_set(rq->cpu, rd->span); |
1f94ef59 GH |
6852 | if (cpu_isset(rq->cpu, cpu_online_map)) |
6853 | cpu_set(rq->cpu, rd->online); | |
dc938520 | 6854 | |
0eab9146 | 6855 | for (class = sched_class_highest; class; class = class->next) { |
57d885fe GH |
6856 | if (class->join_domain) |
6857 | class->join_domain(rq); | |
0eab9146 | 6858 | } |
57d885fe GH |
6859 | |
6860 | spin_unlock_irqrestore(&rq->lock, flags); | |
6861 | } | |
6862 | ||
dc938520 | 6863 | static void init_rootdomain(struct root_domain *rd) |
57d885fe GH |
6864 | { |
6865 | memset(rd, 0, sizeof(*rd)); | |
6866 | ||
dc938520 GH |
6867 | cpus_clear(rd->span); |
6868 | cpus_clear(rd->online); | |
57d885fe GH |
6869 | } |
6870 | ||
6871 | static void init_defrootdomain(void) | |
6872 | { | |
dc938520 | 6873 | init_rootdomain(&def_root_domain); |
57d885fe GH |
6874 | atomic_set(&def_root_domain.refcount, 1); |
6875 | } | |
6876 | ||
dc938520 | 6877 | static struct root_domain *alloc_rootdomain(void) |
57d885fe GH |
6878 | { |
6879 | struct root_domain *rd; | |
6880 | ||
6881 | rd = kmalloc(sizeof(*rd), GFP_KERNEL); | |
6882 | if (!rd) | |
6883 | return NULL; | |
6884 | ||
dc938520 | 6885 | init_rootdomain(rd); |
57d885fe GH |
6886 | |
6887 | return rd; | |
6888 | } | |
6889 | ||
1da177e4 | 6890 | /* |
0eab9146 | 6891 | * Attach the domain 'sd' to 'cpu' as its base domain. Callers must |
1da177e4 LT |
6892 | * hold the hotplug lock. |
6893 | */ | |
0eab9146 IM |
6894 | static void |
6895 | cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu) | |
1da177e4 | 6896 | { |
70b97a7f | 6897 | struct rq *rq = cpu_rq(cpu); |
245af2c7 SS |
6898 | struct sched_domain *tmp; |
6899 | ||
6900 | /* Remove the sched domains which do not contribute to scheduling. */ | |
6901 | for (tmp = sd; tmp; tmp = tmp->parent) { | |
6902 | struct sched_domain *parent = tmp->parent; | |
6903 | if (!parent) | |
6904 | break; | |
1a848870 | 6905 | if (sd_parent_degenerate(tmp, parent)) { |
245af2c7 | 6906 | tmp->parent = parent->parent; |
1a848870 SS |
6907 | if (parent->parent) |
6908 | parent->parent->child = tmp; | |
6909 | } | |
245af2c7 SS |
6910 | } |
6911 | ||
1a848870 | 6912 | if (sd && sd_degenerate(sd)) { |
245af2c7 | 6913 | sd = sd->parent; |
1a848870 SS |
6914 | if (sd) |
6915 | sd->child = NULL; | |
6916 | } | |
1da177e4 LT |
6917 | |
6918 | sched_domain_debug(sd, cpu); | |
6919 | ||
57d885fe | 6920 | rq_attach_root(rq, rd); |
674311d5 | 6921 | rcu_assign_pointer(rq->sd, sd); |
1da177e4 LT |
6922 | } |
6923 | ||
6924 | /* cpus with isolated domains */ | |
67af63a6 | 6925 | static cpumask_t cpu_isolated_map = CPU_MASK_NONE; |
1da177e4 LT |
6926 | |
6927 | /* Setup the mask of cpus configured for isolated domains */ | |
6928 | static int __init isolated_cpu_setup(char *str) | |
6929 | { | |
6930 | int ints[NR_CPUS], i; | |
6931 | ||
6932 | str = get_options(str, ARRAY_SIZE(ints), ints); | |
6933 | cpus_clear(cpu_isolated_map); | |
6934 | for (i = 1; i <= ints[0]; i++) | |
6935 | if (ints[i] < NR_CPUS) | |
6936 | cpu_set(ints[i], cpu_isolated_map); | |
6937 | return 1; | |
6938 | } | |
6939 | ||
8927f494 | 6940 | __setup("isolcpus=", isolated_cpu_setup); |
1da177e4 LT |
6941 | |
6942 | /* | |
6711cab4 SS |
6943 | * init_sched_build_groups takes the cpumask we wish to span, and a pointer |
6944 | * to a function which identifies what group(along with sched group) a CPU | |
6945 | * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS | |
6946 | * (due to the fact that we keep track of groups covered with a cpumask_t). | |
1da177e4 LT |
6947 | * |
6948 | * init_sched_build_groups will build a circular linked list of the groups | |
6949 | * covered by the given span, and will set each group's ->cpumask correctly, | |
6950 | * and ->cpu_power to 0. | |
6951 | */ | |
a616058b | 6952 | static void |
7c16ec58 | 6953 | init_sched_build_groups(const cpumask_t *span, const cpumask_t *cpu_map, |
6711cab4 | 6954 | int (*group_fn)(int cpu, const cpumask_t *cpu_map, |
7c16ec58 MT |
6955 | struct sched_group **sg, |
6956 | cpumask_t *tmpmask), | |
6957 | cpumask_t *covered, cpumask_t *tmpmask) | |
1da177e4 LT |
6958 | { |
6959 | struct sched_group *first = NULL, *last = NULL; | |
1da177e4 LT |
6960 | int i; |
6961 | ||
7c16ec58 MT |
6962 | cpus_clear(*covered); |
6963 | ||
6964 | for_each_cpu_mask(i, *span) { | |
6711cab4 | 6965 | struct sched_group *sg; |
7c16ec58 | 6966 | int group = group_fn(i, cpu_map, &sg, tmpmask); |
1da177e4 LT |
6967 | int j; |
6968 | ||
7c16ec58 | 6969 | if (cpu_isset(i, *covered)) |
1da177e4 LT |
6970 | continue; |
6971 | ||
7c16ec58 | 6972 | cpus_clear(sg->cpumask); |
5517d86b | 6973 | sg->__cpu_power = 0; |
1da177e4 | 6974 | |
7c16ec58 MT |
6975 | for_each_cpu_mask(j, *span) { |
6976 | if (group_fn(j, cpu_map, NULL, tmpmask) != group) | |
1da177e4 LT |
6977 | continue; |
6978 | ||
7c16ec58 | 6979 | cpu_set(j, *covered); |
1da177e4 LT |
6980 | cpu_set(j, sg->cpumask); |
6981 | } | |
6982 | if (!first) | |
6983 | first = sg; | |
6984 | if (last) | |
6985 | last->next = sg; | |
6986 | last = sg; | |
6987 | } | |
6988 | last->next = first; | |
6989 | } | |
6990 | ||
9c1cfda2 | 6991 | #define SD_NODES_PER_DOMAIN 16 |
1da177e4 | 6992 | |
9c1cfda2 | 6993 | #ifdef CONFIG_NUMA |
198e2f18 | 6994 | |
9c1cfda2 JH |
6995 | /** |
6996 | * find_next_best_node - find the next node to include in a sched_domain | |
6997 | * @node: node whose sched_domain we're building | |
6998 | * @used_nodes: nodes already in the sched_domain | |
6999 | * | |
41a2d6cf | 7000 | * Find the next node to include in a given scheduling domain. Simply |
9c1cfda2 JH |
7001 | * finds the closest node not already in the @used_nodes map. |
7002 | * | |
7003 | * Should use nodemask_t. | |
7004 | */ | |
c5f59f08 | 7005 | static int find_next_best_node(int node, nodemask_t *used_nodes) |
9c1cfda2 JH |
7006 | { |
7007 | int i, n, val, min_val, best_node = 0; | |
7008 | ||
7009 | min_val = INT_MAX; | |
7010 | ||
7011 | for (i = 0; i < MAX_NUMNODES; i++) { | |
7012 | /* Start at @node */ | |
7013 | n = (node + i) % MAX_NUMNODES; | |
7014 | ||
7015 | if (!nr_cpus_node(n)) | |
7016 | continue; | |
7017 | ||
7018 | /* Skip already used nodes */ | |
c5f59f08 | 7019 | if (node_isset(n, *used_nodes)) |
9c1cfda2 JH |
7020 | continue; |
7021 | ||
7022 | /* Simple min distance search */ | |
7023 | val = node_distance(node, n); | |
7024 | ||
7025 | if (val < min_val) { | |
7026 | min_val = val; | |
7027 | best_node = n; | |
7028 | } | |
7029 | } | |
7030 | ||
c5f59f08 | 7031 | node_set(best_node, *used_nodes); |
9c1cfda2 JH |
7032 | return best_node; |
7033 | } | |
7034 | ||
7035 | /** | |
7036 | * sched_domain_node_span - get a cpumask for a node's sched_domain | |
7037 | * @node: node whose cpumask we're constructing | |
73486722 | 7038 | * @span: resulting cpumask |
9c1cfda2 | 7039 | * |
41a2d6cf | 7040 | * Given a node, construct a good cpumask for its sched_domain to span. It |
9c1cfda2 JH |
7041 | * should be one that prevents unnecessary balancing, but also spreads tasks |
7042 | * out optimally. | |
7043 | */ | |
4bdbaad3 | 7044 | static void sched_domain_node_span(int node, cpumask_t *span) |
9c1cfda2 | 7045 | { |
c5f59f08 | 7046 | nodemask_t used_nodes; |
c5f59f08 | 7047 | node_to_cpumask_ptr(nodemask, node); |
48f24c4d | 7048 | int i; |
9c1cfda2 | 7049 | |
4bdbaad3 | 7050 | cpus_clear(*span); |
c5f59f08 | 7051 | nodes_clear(used_nodes); |
9c1cfda2 | 7052 | |
4bdbaad3 | 7053 | cpus_or(*span, *span, *nodemask); |
c5f59f08 | 7054 | node_set(node, used_nodes); |
9c1cfda2 JH |
7055 | |
7056 | for (i = 1; i < SD_NODES_PER_DOMAIN; i++) { | |
c5f59f08 | 7057 | int next_node = find_next_best_node(node, &used_nodes); |
48f24c4d | 7058 | |
c5f59f08 | 7059 | node_to_cpumask_ptr_next(nodemask, next_node); |
4bdbaad3 | 7060 | cpus_or(*span, *span, *nodemask); |
9c1cfda2 | 7061 | } |
9c1cfda2 JH |
7062 | } |
7063 | #endif | |
7064 | ||
5c45bf27 | 7065 | int sched_smt_power_savings = 0, sched_mc_power_savings = 0; |
48f24c4d | 7066 | |
9c1cfda2 | 7067 | /* |
48f24c4d | 7068 | * SMT sched-domains: |
9c1cfda2 | 7069 | */ |
1da177e4 LT |
7070 | #ifdef CONFIG_SCHED_SMT |
7071 | static DEFINE_PER_CPU(struct sched_domain, cpu_domains); | |
6711cab4 | 7072 | static DEFINE_PER_CPU(struct sched_group, sched_group_cpus); |
48f24c4d | 7073 | |
41a2d6cf | 7074 | static int |
7c16ec58 MT |
7075 | cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg, |
7076 | cpumask_t *unused) | |
1da177e4 | 7077 | { |
6711cab4 SS |
7078 | if (sg) |
7079 | *sg = &per_cpu(sched_group_cpus, cpu); | |
1da177e4 LT |
7080 | return cpu; |
7081 | } | |
7082 | #endif | |
7083 | ||
48f24c4d IM |
7084 | /* |
7085 | * multi-core sched-domains: | |
7086 | */ | |
1e9f28fa SS |
7087 | #ifdef CONFIG_SCHED_MC |
7088 | static DEFINE_PER_CPU(struct sched_domain, core_domains); | |
6711cab4 | 7089 | static DEFINE_PER_CPU(struct sched_group, sched_group_core); |
1e9f28fa SS |
7090 | #endif |
7091 | ||
7092 | #if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT) | |
41a2d6cf | 7093 | static int |
7c16ec58 MT |
7094 | cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg, |
7095 | cpumask_t *mask) | |
1e9f28fa | 7096 | { |
6711cab4 | 7097 | int group; |
7c16ec58 MT |
7098 | |
7099 | *mask = per_cpu(cpu_sibling_map, cpu); | |
7100 | cpus_and(*mask, *mask, *cpu_map); | |
7101 | group = first_cpu(*mask); | |
6711cab4 SS |
7102 | if (sg) |
7103 | *sg = &per_cpu(sched_group_core, group); | |
7104 | return group; | |
1e9f28fa SS |
7105 | } |
7106 | #elif defined(CONFIG_SCHED_MC) | |
41a2d6cf | 7107 | static int |
7c16ec58 MT |
7108 | cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg, |
7109 | cpumask_t *unused) | |
1e9f28fa | 7110 | { |
6711cab4 SS |
7111 | if (sg) |
7112 | *sg = &per_cpu(sched_group_core, cpu); | |
1e9f28fa SS |
7113 | return cpu; |
7114 | } | |
7115 | #endif | |
7116 | ||
1da177e4 | 7117 | static DEFINE_PER_CPU(struct sched_domain, phys_domains); |
6711cab4 | 7118 | static DEFINE_PER_CPU(struct sched_group, sched_group_phys); |
48f24c4d | 7119 | |
41a2d6cf | 7120 | static int |
7c16ec58 MT |
7121 | cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg, |
7122 | cpumask_t *mask) | |
1da177e4 | 7123 | { |
6711cab4 | 7124 | int group; |
48f24c4d | 7125 | #ifdef CONFIG_SCHED_MC |
7c16ec58 MT |
7126 | *mask = cpu_coregroup_map(cpu); |
7127 | cpus_and(*mask, *mask, *cpu_map); | |
7128 | group = first_cpu(*mask); | |
1e9f28fa | 7129 | #elif defined(CONFIG_SCHED_SMT) |
7c16ec58 MT |
7130 | *mask = per_cpu(cpu_sibling_map, cpu); |
7131 | cpus_and(*mask, *mask, *cpu_map); | |
7132 | group = first_cpu(*mask); | |
1da177e4 | 7133 | #else |
6711cab4 | 7134 | group = cpu; |
1da177e4 | 7135 | #endif |
6711cab4 SS |
7136 | if (sg) |
7137 | *sg = &per_cpu(sched_group_phys, group); | |
7138 | return group; | |
1da177e4 LT |
7139 | } |
7140 | ||
7141 | #ifdef CONFIG_NUMA | |
1da177e4 | 7142 | /* |
9c1cfda2 JH |
7143 | * The init_sched_build_groups can't handle what we want to do with node |
7144 | * groups, so roll our own. Now each node has its own list of groups which | |
7145 | * gets dynamically allocated. | |
1da177e4 | 7146 | */ |
9c1cfda2 | 7147 | static DEFINE_PER_CPU(struct sched_domain, node_domains); |
434d53b0 | 7148 | static struct sched_group ***sched_group_nodes_bycpu; |
1da177e4 | 7149 | |
9c1cfda2 | 7150 | static DEFINE_PER_CPU(struct sched_domain, allnodes_domains); |
6711cab4 | 7151 | static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes); |
9c1cfda2 | 7152 | |
6711cab4 | 7153 | static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map, |
7c16ec58 | 7154 | struct sched_group **sg, cpumask_t *nodemask) |
9c1cfda2 | 7155 | { |
6711cab4 SS |
7156 | int group; |
7157 | ||
7c16ec58 MT |
7158 | *nodemask = node_to_cpumask(cpu_to_node(cpu)); |
7159 | cpus_and(*nodemask, *nodemask, *cpu_map); | |
7160 | group = first_cpu(*nodemask); | |
6711cab4 SS |
7161 | |
7162 | if (sg) | |
7163 | *sg = &per_cpu(sched_group_allnodes, group); | |
7164 | return group; | |
1da177e4 | 7165 | } |
6711cab4 | 7166 | |
08069033 SS |
7167 | static void init_numa_sched_groups_power(struct sched_group *group_head) |
7168 | { | |
7169 | struct sched_group *sg = group_head; | |
7170 | int j; | |
7171 | ||
7172 | if (!sg) | |
7173 | return; | |
3a5c359a AK |
7174 | do { |
7175 | for_each_cpu_mask(j, sg->cpumask) { | |
7176 | struct sched_domain *sd; | |
08069033 | 7177 | |
3a5c359a AK |
7178 | sd = &per_cpu(phys_domains, j); |
7179 | if (j != first_cpu(sd->groups->cpumask)) { | |
7180 | /* | |
7181 | * Only add "power" once for each | |
7182 | * physical package. | |
7183 | */ | |
7184 | continue; | |
7185 | } | |
08069033 | 7186 | |
3a5c359a AK |
7187 | sg_inc_cpu_power(sg, sd->groups->__cpu_power); |
7188 | } | |
7189 | sg = sg->next; | |
7190 | } while (sg != group_head); | |
08069033 | 7191 | } |
1da177e4 LT |
7192 | #endif |
7193 | ||
a616058b | 7194 | #ifdef CONFIG_NUMA |
51888ca2 | 7195 | /* Free memory allocated for various sched_group structures */ |
7c16ec58 | 7196 | static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask) |
51888ca2 | 7197 | { |
a616058b | 7198 | int cpu, i; |
51888ca2 SV |
7199 | |
7200 | for_each_cpu_mask(cpu, *cpu_map) { | |
51888ca2 SV |
7201 | struct sched_group **sched_group_nodes |
7202 | = sched_group_nodes_bycpu[cpu]; | |
7203 | ||
51888ca2 SV |
7204 | if (!sched_group_nodes) |
7205 | continue; | |
7206 | ||
7207 | for (i = 0; i < MAX_NUMNODES; i++) { | |
51888ca2 SV |
7208 | struct sched_group *oldsg, *sg = sched_group_nodes[i]; |
7209 | ||
7c16ec58 MT |
7210 | *nodemask = node_to_cpumask(i); |
7211 | cpus_and(*nodemask, *nodemask, *cpu_map); | |
7212 | if (cpus_empty(*nodemask)) | |
51888ca2 SV |
7213 | continue; |
7214 | ||
7215 | if (sg == NULL) | |
7216 | continue; | |
7217 | sg = sg->next; | |
7218 | next_sg: | |
7219 | oldsg = sg; | |
7220 | sg = sg->next; | |
7221 | kfree(oldsg); | |
7222 | if (oldsg != sched_group_nodes[i]) | |
7223 | goto next_sg; | |
7224 | } | |
7225 | kfree(sched_group_nodes); | |
7226 | sched_group_nodes_bycpu[cpu] = NULL; | |
7227 | } | |
51888ca2 | 7228 | } |
a616058b | 7229 | #else |
7c16ec58 | 7230 | static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask) |
a616058b SS |
7231 | { |
7232 | } | |
7233 | #endif | |
51888ca2 | 7234 | |
89c4710e SS |
7235 | /* |
7236 | * Initialize sched groups cpu_power. | |
7237 | * | |
7238 | * cpu_power indicates the capacity of sched group, which is used while | |
7239 | * distributing the load between different sched groups in a sched domain. | |
7240 | * Typically cpu_power for all the groups in a sched domain will be same unless | |
7241 | * there are asymmetries in the topology. If there are asymmetries, group | |
7242 | * having more cpu_power will pickup more load compared to the group having | |
7243 | * less cpu_power. | |
7244 | * | |
7245 | * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents | |
7246 | * the maximum number of tasks a group can handle in the presence of other idle | |
7247 | * or lightly loaded groups in the same sched domain. | |
7248 | */ | |
7249 | static void init_sched_groups_power(int cpu, struct sched_domain *sd) | |
7250 | { | |
7251 | struct sched_domain *child; | |
7252 | struct sched_group *group; | |
7253 | ||
7254 | WARN_ON(!sd || !sd->groups); | |
7255 | ||
7256 | if (cpu != first_cpu(sd->groups->cpumask)) | |
7257 | return; | |
7258 | ||
7259 | child = sd->child; | |
7260 | ||
5517d86b ED |
7261 | sd->groups->__cpu_power = 0; |
7262 | ||
89c4710e SS |
7263 | /* |
7264 | * For perf policy, if the groups in child domain share resources | |
7265 | * (for example cores sharing some portions of the cache hierarchy | |
7266 | * or SMT), then set this domain groups cpu_power such that each group | |
7267 | * can handle only one task, when there are other idle groups in the | |
7268 | * same sched domain. | |
7269 | */ | |
7270 | if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) && | |
7271 | (child->flags & | |
7272 | (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) { | |
5517d86b | 7273 | sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE); |
89c4710e SS |
7274 | return; |
7275 | } | |
7276 | ||
89c4710e SS |
7277 | /* |
7278 | * add cpu_power of each child group to this groups cpu_power | |
7279 | */ | |
7280 | group = child->groups; | |
7281 | do { | |
5517d86b | 7282 | sg_inc_cpu_power(sd->groups, group->__cpu_power); |
89c4710e SS |
7283 | group = group->next; |
7284 | } while (group != child->groups); | |
7285 | } | |
7286 | ||
7c16ec58 MT |
7287 | /* |
7288 | * Initializers for schedule domains | |
7289 | * Non-inlined to reduce accumulated stack pressure in build_sched_domains() | |
7290 | */ | |
7291 | ||
7292 | #define SD_INIT(sd, type) sd_init_##type(sd) | |
7293 | #define SD_INIT_FUNC(type) \ | |
7294 | static noinline void sd_init_##type(struct sched_domain *sd) \ | |
7295 | { \ | |
7296 | memset(sd, 0, sizeof(*sd)); \ | |
7297 | *sd = SD_##type##_INIT; \ | |
1d3504fc | 7298 | sd->level = SD_LV_##type; \ |
7c16ec58 MT |
7299 | } |
7300 | ||
7301 | SD_INIT_FUNC(CPU) | |
7302 | #ifdef CONFIG_NUMA | |
7303 | SD_INIT_FUNC(ALLNODES) | |
7304 | SD_INIT_FUNC(NODE) | |
7305 | #endif | |
7306 | #ifdef CONFIG_SCHED_SMT | |
7307 | SD_INIT_FUNC(SIBLING) | |
7308 | #endif | |
7309 | #ifdef CONFIG_SCHED_MC | |
7310 | SD_INIT_FUNC(MC) | |
7311 | #endif | |
7312 | ||
7313 | /* | |
7314 | * To minimize stack usage kmalloc room for cpumasks and share the | |
7315 | * space as the usage in build_sched_domains() dictates. Used only | |
7316 | * if the amount of space is significant. | |
7317 | */ | |
7318 | struct allmasks { | |
7319 | cpumask_t tmpmask; /* make this one first */ | |
7320 | union { | |
7321 | cpumask_t nodemask; | |
7322 | cpumask_t this_sibling_map; | |
7323 | cpumask_t this_core_map; | |
7324 | }; | |
7325 | cpumask_t send_covered; | |
7326 | ||
7327 | #ifdef CONFIG_NUMA | |
7328 | cpumask_t domainspan; | |
7329 | cpumask_t covered; | |
7330 | cpumask_t notcovered; | |
7331 | #endif | |
7332 | }; | |
7333 | ||
7334 | #if NR_CPUS > 128 | |
7335 | #define SCHED_CPUMASK_ALLOC 1 | |
7336 | #define SCHED_CPUMASK_FREE(v) kfree(v) | |
7337 | #define SCHED_CPUMASK_DECLARE(v) struct allmasks *v | |
7338 | #else | |
7339 | #define SCHED_CPUMASK_ALLOC 0 | |
7340 | #define SCHED_CPUMASK_FREE(v) | |
7341 | #define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v | |
7342 | #endif | |
7343 | ||
7344 | #define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \ | |
7345 | ((unsigned long)(a) + offsetof(struct allmasks, v)) | |
7346 | ||
1d3504fc HS |
7347 | static int default_relax_domain_level = -1; |
7348 | ||
7349 | static int __init setup_relax_domain_level(char *str) | |
7350 | { | |
7351 | default_relax_domain_level = simple_strtoul(str, NULL, 0); | |
7352 | return 1; | |
7353 | } | |
7354 | __setup("relax_domain_level=", setup_relax_domain_level); | |
7355 | ||
7356 | static void set_domain_attribute(struct sched_domain *sd, | |
7357 | struct sched_domain_attr *attr) | |
7358 | { | |
7359 | int request; | |
7360 | ||
7361 | if (!attr || attr->relax_domain_level < 0) { | |
7362 | if (default_relax_domain_level < 0) | |
7363 | return; | |
7364 | else | |
7365 | request = default_relax_domain_level; | |
7366 | } else | |
7367 | request = attr->relax_domain_level; | |
7368 | if (request < sd->level) { | |
7369 | /* turn off idle balance on this domain */ | |
7370 | sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE); | |
7371 | } else { | |
7372 | /* turn on idle balance on this domain */ | |
7373 | sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE); | |
7374 | } | |
7375 | } | |
7376 | ||
1da177e4 | 7377 | /* |
1a20ff27 DG |
7378 | * Build sched domains for a given set of cpus and attach the sched domains |
7379 | * to the individual cpus | |
1da177e4 | 7380 | */ |
1d3504fc HS |
7381 | static int __build_sched_domains(const cpumask_t *cpu_map, |
7382 | struct sched_domain_attr *attr) | |
1da177e4 LT |
7383 | { |
7384 | int i; | |
57d885fe | 7385 | struct root_domain *rd; |
7c16ec58 MT |
7386 | SCHED_CPUMASK_DECLARE(allmasks); |
7387 | cpumask_t *tmpmask; | |
d1b55138 JH |
7388 | #ifdef CONFIG_NUMA |
7389 | struct sched_group **sched_group_nodes = NULL; | |
6711cab4 | 7390 | int sd_allnodes = 0; |
d1b55138 JH |
7391 | |
7392 | /* | |
7393 | * Allocate the per-node list of sched groups | |
7394 | */ | |
5cf9f062 | 7395 | sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *), |
41a2d6cf | 7396 | GFP_KERNEL); |
d1b55138 JH |
7397 | if (!sched_group_nodes) { |
7398 | printk(KERN_WARNING "Can not alloc sched group node list\n"); | |
51888ca2 | 7399 | return -ENOMEM; |
d1b55138 | 7400 | } |
d1b55138 | 7401 | #endif |
1da177e4 | 7402 | |
dc938520 | 7403 | rd = alloc_rootdomain(); |
57d885fe GH |
7404 | if (!rd) { |
7405 | printk(KERN_WARNING "Cannot alloc root domain\n"); | |
7c16ec58 MT |
7406 | #ifdef CONFIG_NUMA |
7407 | kfree(sched_group_nodes); | |
7408 | #endif | |
57d885fe GH |
7409 | return -ENOMEM; |
7410 | } | |
7411 | ||
7c16ec58 MT |
7412 | #if SCHED_CPUMASK_ALLOC |
7413 | /* get space for all scratch cpumask variables */ | |
7414 | allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL); | |
7415 | if (!allmasks) { | |
7416 | printk(KERN_WARNING "Cannot alloc cpumask array\n"); | |
7417 | kfree(rd); | |
7418 | #ifdef CONFIG_NUMA | |
7419 | kfree(sched_group_nodes); | |
7420 | #endif | |
7421 | return -ENOMEM; | |
7422 | } | |
7423 | #endif | |
7424 | tmpmask = (cpumask_t *)allmasks; | |
7425 | ||
7426 | ||
7427 | #ifdef CONFIG_NUMA | |
7428 | sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes; | |
7429 | #endif | |
7430 | ||
1da177e4 | 7431 | /* |
1a20ff27 | 7432 | * Set up domains for cpus specified by the cpu_map. |
1da177e4 | 7433 | */ |
1a20ff27 | 7434 | for_each_cpu_mask(i, *cpu_map) { |
1da177e4 | 7435 | struct sched_domain *sd = NULL, *p; |
7c16ec58 | 7436 | SCHED_CPUMASK_VAR(nodemask, allmasks); |
1da177e4 | 7437 | |
7c16ec58 MT |
7438 | *nodemask = node_to_cpumask(cpu_to_node(i)); |
7439 | cpus_and(*nodemask, *nodemask, *cpu_map); | |
1da177e4 LT |
7440 | |
7441 | #ifdef CONFIG_NUMA | |
dd41f596 | 7442 | if (cpus_weight(*cpu_map) > |
7c16ec58 | 7443 | SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) { |
9c1cfda2 | 7444 | sd = &per_cpu(allnodes_domains, i); |
7c16ec58 | 7445 | SD_INIT(sd, ALLNODES); |
1d3504fc | 7446 | set_domain_attribute(sd, attr); |
9c1cfda2 | 7447 | sd->span = *cpu_map; |
18d95a28 | 7448 | sd->first_cpu = first_cpu(sd->span); |
7c16ec58 | 7449 | cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask); |
9c1cfda2 | 7450 | p = sd; |
6711cab4 | 7451 | sd_allnodes = 1; |
9c1cfda2 JH |
7452 | } else |
7453 | p = NULL; | |
7454 | ||
1da177e4 | 7455 | sd = &per_cpu(node_domains, i); |
7c16ec58 | 7456 | SD_INIT(sd, NODE); |
1d3504fc | 7457 | set_domain_attribute(sd, attr); |
4bdbaad3 | 7458 | sched_domain_node_span(cpu_to_node(i), &sd->span); |
18d95a28 | 7459 | sd->first_cpu = first_cpu(sd->span); |
9c1cfda2 | 7460 | sd->parent = p; |
1a848870 SS |
7461 | if (p) |
7462 | p->child = sd; | |
9c1cfda2 | 7463 | cpus_and(sd->span, sd->span, *cpu_map); |
1da177e4 LT |
7464 | #endif |
7465 | ||
7466 | p = sd; | |
7467 | sd = &per_cpu(phys_domains, i); | |
7c16ec58 | 7468 | SD_INIT(sd, CPU); |
1d3504fc | 7469 | set_domain_attribute(sd, attr); |
7c16ec58 | 7470 | sd->span = *nodemask; |
18d95a28 | 7471 | sd->first_cpu = first_cpu(sd->span); |
1da177e4 | 7472 | sd->parent = p; |
1a848870 SS |
7473 | if (p) |
7474 | p->child = sd; | |
7c16ec58 | 7475 | cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask); |
1da177e4 | 7476 | |
1e9f28fa SS |
7477 | #ifdef CONFIG_SCHED_MC |
7478 | p = sd; | |
7479 | sd = &per_cpu(core_domains, i); | |
7c16ec58 | 7480 | SD_INIT(sd, MC); |
1d3504fc | 7481 | set_domain_attribute(sd, attr); |
1e9f28fa | 7482 | sd->span = cpu_coregroup_map(i); |
18d95a28 | 7483 | sd->first_cpu = first_cpu(sd->span); |
1e9f28fa SS |
7484 | cpus_and(sd->span, sd->span, *cpu_map); |
7485 | sd->parent = p; | |
1a848870 | 7486 | p->child = sd; |
7c16ec58 | 7487 | cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask); |
1e9f28fa SS |
7488 | #endif |
7489 | ||
1da177e4 LT |
7490 | #ifdef CONFIG_SCHED_SMT |
7491 | p = sd; | |
7492 | sd = &per_cpu(cpu_domains, i); | |
7c16ec58 | 7493 | SD_INIT(sd, SIBLING); |
1d3504fc | 7494 | set_domain_attribute(sd, attr); |
d5a7430d | 7495 | sd->span = per_cpu(cpu_sibling_map, i); |
18d95a28 | 7496 | sd->first_cpu = first_cpu(sd->span); |
1a20ff27 | 7497 | cpus_and(sd->span, sd->span, *cpu_map); |
1da177e4 | 7498 | sd->parent = p; |
1a848870 | 7499 | p->child = sd; |
7c16ec58 | 7500 | cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask); |
1da177e4 LT |
7501 | #endif |
7502 | } | |
7503 | ||
7504 | #ifdef CONFIG_SCHED_SMT | |
7505 | /* Set up CPU (sibling) groups */ | |
9c1cfda2 | 7506 | for_each_cpu_mask(i, *cpu_map) { |
7c16ec58 MT |
7507 | SCHED_CPUMASK_VAR(this_sibling_map, allmasks); |
7508 | SCHED_CPUMASK_VAR(send_covered, allmasks); | |
7509 | ||
7510 | *this_sibling_map = per_cpu(cpu_sibling_map, i); | |
7511 | cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map); | |
7512 | if (i != first_cpu(*this_sibling_map)) | |
1da177e4 LT |
7513 | continue; |
7514 | ||
dd41f596 | 7515 | init_sched_build_groups(this_sibling_map, cpu_map, |
7c16ec58 MT |
7516 | &cpu_to_cpu_group, |
7517 | send_covered, tmpmask); | |
1da177e4 LT |
7518 | } |
7519 | #endif | |
7520 | ||
1e9f28fa SS |
7521 | #ifdef CONFIG_SCHED_MC |
7522 | /* Set up multi-core groups */ | |
7523 | for_each_cpu_mask(i, *cpu_map) { | |
7c16ec58 MT |
7524 | SCHED_CPUMASK_VAR(this_core_map, allmasks); |
7525 | SCHED_CPUMASK_VAR(send_covered, allmasks); | |
7526 | ||
7527 | *this_core_map = cpu_coregroup_map(i); | |
7528 | cpus_and(*this_core_map, *this_core_map, *cpu_map); | |
7529 | if (i != first_cpu(*this_core_map)) | |
1e9f28fa | 7530 | continue; |
7c16ec58 | 7531 | |
dd41f596 | 7532 | init_sched_build_groups(this_core_map, cpu_map, |
7c16ec58 MT |
7533 | &cpu_to_core_group, |
7534 | send_covered, tmpmask); | |
1e9f28fa SS |
7535 | } |
7536 | #endif | |
7537 | ||
1da177e4 LT |
7538 | /* Set up physical groups */ |
7539 | for (i = 0; i < MAX_NUMNODES; i++) { | |
7c16ec58 MT |
7540 | SCHED_CPUMASK_VAR(nodemask, allmasks); |
7541 | SCHED_CPUMASK_VAR(send_covered, allmasks); | |
1da177e4 | 7542 | |
7c16ec58 MT |
7543 | *nodemask = node_to_cpumask(i); |
7544 | cpus_and(*nodemask, *nodemask, *cpu_map); | |
7545 | if (cpus_empty(*nodemask)) | |
1da177e4 LT |
7546 | continue; |
7547 | ||
7c16ec58 MT |
7548 | init_sched_build_groups(nodemask, cpu_map, |
7549 | &cpu_to_phys_group, | |
7550 | send_covered, tmpmask); | |
1da177e4 LT |
7551 | } |
7552 | ||
7553 | #ifdef CONFIG_NUMA | |
7554 | /* Set up node groups */ | |
7c16ec58 MT |
7555 | if (sd_allnodes) { |
7556 | SCHED_CPUMASK_VAR(send_covered, allmasks); | |
7557 | ||
7558 | init_sched_build_groups(cpu_map, cpu_map, | |
7559 | &cpu_to_allnodes_group, | |
7560 | send_covered, tmpmask); | |
7561 | } | |
9c1cfda2 JH |
7562 | |
7563 | for (i = 0; i < MAX_NUMNODES; i++) { | |
7564 | /* Set up node groups */ | |
7565 | struct sched_group *sg, *prev; | |
7c16ec58 MT |
7566 | SCHED_CPUMASK_VAR(nodemask, allmasks); |
7567 | SCHED_CPUMASK_VAR(domainspan, allmasks); | |
7568 | SCHED_CPUMASK_VAR(covered, allmasks); | |
9c1cfda2 JH |
7569 | int j; |
7570 | ||
7c16ec58 MT |
7571 | *nodemask = node_to_cpumask(i); |
7572 | cpus_clear(*covered); | |
7573 | ||
7574 | cpus_and(*nodemask, *nodemask, *cpu_map); | |
7575 | if (cpus_empty(*nodemask)) { | |
d1b55138 | 7576 | sched_group_nodes[i] = NULL; |
9c1cfda2 | 7577 | continue; |
d1b55138 | 7578 | } |
9c1cfda2 | 7579 | |
4bdbaad3 | 7580 | sched_domain_node_span(i, domainspan); |
7c16ec58 | 7581 | cpus_and(*domainspan, *domainspan, *cpu_map); |
9c1cfda2 | 7582 | |
15f0b676 | 7583 | sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i); |
51888ca2 SV |
7584 | if (!sg) { |
7585 | printk(KERN_WARNING "Can not alloc domain group for " | |
7586 | "node %d\n", i); | |
7587 | goto error; | |
7588 | } | |
9c1cfda2 | 7589 | sched_group_nodes[i] = sg; |
7c16ec58 | 7590 | for_each_cpu_mask(j, *nodemask) { |
9c1cfda2 | 7591 | struct sched_domain *sd; |
9761eea8 | 7592 | |
9c1cfda2 JH |
7593 | sd = &per_cpu(node_domains, j); |
7594 | sd->groups = sg; | |
9c1cfda2 | 7595 | } |
5517d86b | 7596 | sg->__cpu_power = 0; |
7c16ec58 | 7597 | sg->cpumask = *nodemask; |
51888ca2 | 7598 | sg->next = sg; |
7c16ec58 | 7599 | cpus_or(*covered, *covered, *nodemask); |
9c1cfda2 JH |
7600 | prev = sg; |
7601 | ||
7602 | for (j = 0; j < MAX_NUMNODES; j++) { | |
7c16ec58 | 7603 | SCHED_CPUMASK_VAR(notcovered, allmasks); |
9c1cfda2 | 7604 | int n = (i + j) % MAX_NUMNODES; |
c5f59f08 | 7605 | node_to_cpumask_ptr(pnodemask, n); |
9c1cfda2 | 7606 | |
7c16ec58 MT |
7607 | cpus_complement(*notcovered, *covered); |
7608 | cpus_and(*tmpmask, *notcovered, *cpu_map); | |
7609 | cpus_and(*tmpmask, *tmpmask, *domainspan); | |
7610 | if (cpus_empty(*tmpmask)) | |
9c1cfda2 JH |
7611 | break; |
7612 | ||
7c16ec58 MT |
7613 | cpus_and(*tmpmask, *tmpmask, *pnodemask); |
7614 | if (cpus_empty(*tmpmask)) | |
9c1cfda2 JH |
7615 | continue; |
7616 | ||
15f0b676 SV |
7617 | sg = kmalloc_node(sizeof(struct sched_group), |
7618 | GFP_KERNEL, i); | |
9c1cfda2 JH |
7619 | if (!sg) { |
7620 | printk(KERN_WARNING | |
7621 | "Can not alloc domain group for node %d\n", j); | |
51888ca2 | 7622 | goto error; |
9c1cfda2 | 7623 | } |
5517d86b | 7624 | sg->__cpu_power = 0; |
7c16ec58 | 7625 | sg->cpumask = *tmpmask; |
51888ca2 | 7626 | sg->next = prev->next; |
7c16ec58 | 7627 | cpus_or(*covered, *covered, *tmpmask); |
9c1cfda2 JH |
7628 | prev->next = sg; |
7629 | prev = sg; | |
7630 | } | |
9c1cfda2 | 7631 | } |
1da177e4 LT |
7632 | #endif |
7633 | ||
7634 | /* Calculate CPU power for physical packages and nodes */ | |
5c45bf27 | 7635 | #ifdef CONFIG_SCHED_SMT |
1a20ff27 | 7636 | for_each_cpu_mask(i, *cpu_map) { |
dd41f596 IM |
7637 | struct sched_domain *sd = &per_cpu(cpu_domains, i); |
7638 | ||
89c4710e | 7639 | init_sched_groups_power(i, sd); |
5c45bf27 | 7640 | } |
1da177e4 | 7641 | #endif |
1e9f28fa | 7642 | #ifdef CONFIG_SCHED_MC |
5c45bf27 | 7643 | for_each_cpu_mask(i, *cpu_map) { |
dd41f596 IM |
7644 | struct sched_domain *sd = &per_cpu(core_domains, i); |
7645 | ||
89c4710e | 7646 | init_sched_groups_power(i, sd); |
5c45bf27 SS |
7647 | } |
7648 | #endif | |
1e9f28fa | 7649 | |
5c45bf27 | 7650 | for_each_cpu_mask(i, *cpu_map) { |
dd41f596 IM |
7651 | struct sched_domain *sd = &per_cpu(phys_domains, i); |
7652 | ||
89c4710e | 7653 | init_sched_groups_power(i, sd); |
1da177e4 LT |
7654 | } |
7655 | ||
9c1cfda2 | 7656 | #ifdef CONFIG_NUMA |
08069033 SS |
7657 | for (i = 0; i < MAX_NUMNODES; i++) |
7658 | init_numa_sched_groups_power(sched_group_nodes[i]); | |
9c1cfda2 | 7659 | |
6711cab4 SS |
7660 | if (sd_allnodes) { |
7661 | struct sched_group *sg; | |
f712c0c7 | 7662 | |
7c16ec58 MT |
7663 | cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg, |
7664 | tmpmask); | |
f712c0c7 SS |
7665 | init_numa_sched_groups_power(sg); |
7666 | } | |
9c1cfda2 JH |
7667 | #endif |
7668 | ||
1da177e4 | 7669 | /* Attach the domains */ |
1a20ff27 | 7670 | for_each_cpu_mask(i, *cpu_map) { |
1da177e4 LT |
7671 | struct sched_domain *sd; |
7672 | #ifdef CONFIG_SCHED_SMT | |
7673 | sd = &per_cpu(cpu_domains, i); | |
1e9f28fa SS |
7674 | #elif defined(CONFIG_SCHED_MC) |
7675 | sd = &per_cpu(core_domains, i); | |
1da177e4 LT |
7676 | #else |
7677 | sd = &per_cpu(phys_domains, i); | |
7678 | #endif | |
57d885fe | 7679 | cpu_attach_domain(sd, rd, i); |
1da177e4 | 7680 | } |
51888ca2 | 7681 | |
7c16ec58 | 7682 | SCHED_CPUMASK_FREE((void *)allmasks); |
51888ca2 SV |
7683 | return 0; |
7684 | ||
a616058b | 7685 | #ifdef CONFIG_NUMA |
51888ca2 | 7686 | error: |
7c16ec58 MT |
7687 | free_sched_groups(cpu_map, tmpmask); |
7688 | SCHED_CPUMASK_FREE((void *)allmasks); | |
51888ca2 | 7689 | return -ENOMEM; |
a616058b | 7690 | #endif |
1da177e4 | 7691 | } |
029190c5 | 7692 | |
1d3504fc HS |
7693 | static int build_sched_domains(const cpumask_t *cpu_map) |
7694 | { | |
7695 | return __build_sched_domains(cpu_map, NULL); | |
7696 | } | |
7697 | ||
029190c5 PJ |
7698 | static cpumask_t *doms_cur; /* current sched domains */ |
7699 | static int ndoms_cur; /* number of sched domains in 'doms_cur' */ | |
1d3504fc HS |
7700 | static struct sched_domain_attr *dattr_cur; /* attribues of custom domains |
7701 | in 'doms_cur' */ | |
029190c5 PJ |
7702 | |
7703 | /* | |
7704 | * Special case: If a kmalloc of a doms_cur partition (array of | |
7705 | * cpumask_t) fails, then fallback to a single sched domain, | |
7706 | * as determined by the single cpumask_t fallback_doms. | |
7707 | */ | |
7708 | static cpumask_t fallback_doms; | |
7709 | ||
22e52b07 HC |
7710 | void __attribute__((weak)) arch_update_cpu_topology(void) |
7711 | { | |
7712 | } | |
7713 | ||
1a20ff27 | 7714 | /* |
41a2d6cf | 7715 | * Set up scheduler domains and groups. Callers must hold the hotplug lock. |
029190c5 PJ |
7716 | * For now this just excludes isolated cpus, but could be used to |
7717 | * exclude other special cases in the future. | |
1a20ff27 | 7718 | */ |
51888ca2 | 7719 | static int arch_init_sched_domains(const cpumask_t *cpu_map) |
1a20ff27 | 7720 | { |
7378547f MM |
7721 | int err; |
7722 | ||
22e52b07 | 7723 | arch_update_cpu_topology(); |
029190c5 PJ |
7724 | ndoms_cur = 1; |
7725 | doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL); | |
7726 | if (!doms_cur) | |
7727 | doms_cur = &fallback_doms; | |
7728 | cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map); | |
1d3504fc | 7729 | dattr_cur = NULL; |
7378547f | 7730 | err = build_sched_domains(doms_cur); |
6382bc90 | 7731 | register_sched_domain_sysctl(); |
7378547f MM |
7732 | |
7733 | return err; | |
1a20ff27 DG |
7734 | } |
7735 | ||
7c16ec58 MT |
7736 | static void arch_destroy_sched_domains(const cpumask_t *cpu_map, |
7737 | cpumask_t *tmpmask) | |
1da177e4 | 7738 | { |
7c16ec58 | 7739 | free_sched_groups(cpu_map, tmpmask); |
9c1cfda2 | 7740 | } |
1da177e4 | 7741 | |
1a20ff27 DG |
7742 | /* |
7743 | * Detach sched domains from a group of cpus specified in cpu_map | |
7744 | * These cpus will now be attached to the NULL domain | |
7745 | */ | |
858119e1 | 7746 | static void detach_destroy_domains(const cpumask_t *cpu_map) |
1a20ff27 | 7747 | { |
7c16ec58 | 7748 | cpumask_t tmpmask; |
1a20ff27 DG |
7749 | int i; |
7750 | ||
6382bc90 MM |
7751 | unregister_sched_domain_sysctl(); |
7752 | ||
1a20ff27 | 7753 | for_each_cpu_mask(i, *cpu_map) |
57d885fe | 7754 | cpu_attach_domain(NULL, &def_root_domain, i); |
1a20ff27 | 7755 | synchronize_sched(); |
7c16ec58 | 7756 | arch_destroy_sched_domains(cpu_map, &tmpmask); |
1a20ff27 DG |
7757 | } |
7758 | ||
1d3504fc HS |
7759 | /* handle null as "default" */ |
7760 | static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur, | |
7761 | struct sched_domain_attr *new, int idx_new) | |
7762 | { | |
7763 | struct sched_domain_attr tmp; | |
7764 | ||
7765 | /* fast path */ | |
7766 | if (!new && !cur) | |
7767 | return 1; | |
7768 | ||
7769 | tmp = SD_ATTR_INIT; | |
7770 | return !memcmp(cur ? (cur + idx_cur) : &tmp, | |
7771 | new ? (new + idx_new) : &tmp, | |
7772 | sizeof(struct sched_domain_attr)); | |
7773 | } | |
7774 | ||
029190c5 PJ |
7775 | /* |
7776 | * Partition sched domains as specified by the 'ndoms_new' | |
41a2d6cf | 7777 | * cpumasks in the array doms_new[] of cpumasks. This compares |
029190c5 PJ |
7778 | * doms_new[] to the current sched domain partitioning, doms_cur[]. |
7779 | * It destroys each deleted domain and builds each new domain. | |
7780 | * | |
7781 | * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'. | |
41a2d6cf IM |
7782 | * The masks don't intersect (don't overlap.) We should setup one |
7783 | * sched domain for each mask. CPUs not in any of the cpumasks will | |
7784 | * not be load balanced. If the same cpumask appears both in the | |
029190c5 PJ |
7785 | * current 'doms_cur' domains and in the new 'doms_new', we can leave |
7786 | * it as it is. | |
7787 | * | |
41a2d6cf IM |
7788 | * The passed in 'doms_new' should be kmalloc'd. This routine takes |
7789 | * ownership of it and will kfree it when done with it. If the caller | |
029190c5 PJ |
7790 | * failed the kmalloc call, then it can pass in doms_new == NULL, |
7791 | * and partition_sched_domains() will fallback to the single partition | |
7792 | * 'fallback_doms'. | |
7793 | * | |
7794 | * Call with hotplug lock held | |
7795 | */ | |
1d3504fc HS |
7796 | void partition_sched_domains(int ndoms_new, cpumask_t *doms_new, |
7797 | struct sched_domain_attr *dattr_new) | |
029190c5 PJ |
7798 | { |
7799 | int i, j; | |
7800 | ||
a1835615 SV |
7801 | lock_doms_cur(); |
7802 | ||
7378547f MM |
7803 | /* always unregister in case we don't destroy any domains */ |
7804 | unregister_sched_domain_sysctl(); | |
7805 | ||
029190c5 PJ |
7806 | if (doms_new == NULL) { |
7807 | ndoms_new = 1; | |
7808 | doms_new = &fallback_doms; | |
7809 | cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map); | |
1d3504fc | 7810 | dattr_new = NULL; |
029190c5 PJ |
7811 | } |
7812 | ||
7813 | /* Destroy deleted domains */ | |
7814 | for (i = 0; i < ndoms_cur; i++) { | |
7815 | for (j = 0; j < ndoms_new; j++) { | |
1d3504fc HS |
7816 | if (cpus_equal(doms_cur[i], doms_new[j]) |
7817 | && dattrs_equal(dattr_cur, i, dattr_new, j)) | |
029190c5 PJ |
7818 | goto match1; |
7819 | } | |
7820 | /* no match - a current sched domain not in new doms_new[] */ | |
7821 | detach_destroy_domains(doms_cur + i); | |
7822 | match1: | |
7823 | ; | |
7824 | } | |
7825 | ||
7826 | /* Build new domains */ | |
7827 | for (i = 0; i < ndoms_new; i++) { | |
7828 | for (j = 0; j < ndoms_cur; j++) { | |
1d3504fc HS |
7829 | if (cpus_equal(doms_new[i], doms_cur[j]) |
7830 | && dattrs_equal(dattr_new, i, dattr_cur, j)) | |
029190c5 PJ |
7831 | goto match2; |
7832 | } | |
7833 | /* no match - add a new doms_new */ | |
1d3504fc HS |
7834 | __build_sched_domains(doms_new + i, |
7835 | dattr_new ? dattr_new + i : NULL); | |
029190c5 PJ |
7836 | match2: |
7837 | ; | |
7838 | } | |
7839 | ||
7840 | /* Remember the new sched domains */ | |
7841 | if (doms_cur != &fallback_doms) | |
7842 | kfree(doms_cur); | |
1d3504fc | 7843 | kfree(dattr_cur); /* kfree(NULL) is safe */ |
029190c5 | 7844 | doms_cur = doms_new; |
1d3504fc | 7845 | dattr_cur = dattr_new; |
029190c5 | 7846 | ndoms_cur = ndoms_new; |
7378547f MM |
7847 | |
7848 | register_sched_domain_sysctl(); | |
a1835615 SV |
7849 | |
7850 | unlock_doms_cur(); | |
029190c5 PJ |
7851 | } |
7852 | ||
5c45bf27 | 7853 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
9aefd0ab | 7854 | int arch_reinit_sched_domains(void) |
5c45bf27 SS |
7855 | { |
7856 | int err; | |
7857 | ||
95402b38 | 7858 | get_online_cpus(); |
5c45bf27 SS |
7859 | detach_destroy_domains(&cpu_online_map); |
7860 | err = arch_init_sched_domains(&cpu_online_map); | |
95402b38 | 7861 | put_online_cpus(); |
5c45bf27 SS |
7862 | |
7863 | return err; | |
7864 | } | |
7865 | ||
7866 | static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt) | |
7867 | { | |
7868 | int ret; | |
7869 | ||
7870 | if (buf[0] != '0' && buf[0] != '1') | |
7871 | return -EINVAL; | |
7872 | ||
7873 | if (smt) | |
7874 | sched_smt_power_savings = (buf[0] == '1'); | |
7875 | else | |
7876 | sched_mc_power_savings = (buf[0] == '1'); | |
7877 | ||
7878 | ret = arch_reinit_sched_domains(); | |
7879 | ||
7880 | return ret ? ret : count; | |
7881 | } | |
7882 | ||
5c45bf27 SS |
7883 | #ifdef CONFIG_SCHED_MC |
7884 | static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page) | |
7885 | { | |
7886 | return sprintf(page, "%u\n", sched_mc_power_savings); | |
7887 | } | |
48f24c4d IM |
7888 | static ssize_t sched_mc_power_savings_store(struct sys_device *dev, |
7889 | const char *buf, size_t count) | |
5c45bf27 SS |
7890 | { |
7891 | return sched_power_savings_store(buf, count, 0); | |
7892 | } | |
6707de00 AB |
7893 | static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show, |
7894 | sched_mc_power_savings_store); | |
5c45bf27 SS |
7895 | #endif |
7896 | ||
7897 | #ifdef CONFIG_SCHED_SMT | |
7898 | static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page) | |
7899 | { | |
7900 | return sprintf(page, "%u\n", sched_smt_power_savings); | |
7901 | } | |
48f24c4d IM |
7902 | static ssize_t sched_smt_power_savings_store(struct sys_device *dev, |
7903 | const char *buf, size_t count) | |
5c45bf27 SS |
7904 | { |
7905 | return sched_power_savings_store(buf, count, 1); | |
7906 | } | |
6707de00 AB |
7907 | static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show, |
7908 | sched_smt_power_savings_store); | |
7909 | #endif | |
7910 | ||
7911 | int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls) | |
7912 | { | |
7913 | int err = 0; | |
7914 | ||
7915 | #ifdef CONFIG_SCHED_SMT | |
7916 | if (smt_capable()) | |
7917 | err = sysfs_create_file(&cls->kset.kobj, | |
7918 | &attr_sched_smt_power_savings.attr); | |
7919 | #endif | |
7920 | #ifdef CONFIG_SCHED_MC | |
7921 | if (!err && mc_capable()) | |
7922 | err = sysfs_create_file(&cls->kset.kobj, | |
7923 | &attr_sched_mc_power_savings.attr); | |
7924 | #endif | |
7925 | return err; | |
7926 | } | |
5c45bf27 SS |
7927 | #endif |
7928 | ||
1da177e4 | 7929 | /* |
41a2d6cf | 7930 | * Force a reinitialization of the sched domains hierarchy. The domains |
1da177e4 | 7931 | * and groups cannot be updated in place without racing with the balancing |
41c7ce9a | 7932 | * code, so we temporarily attach all running cpus to the NULL domain |
1da177e4 LT |
7933 | * which will prevent rebalancing while the sched domains are recalculated. |
7934 | */ | |
7935 | static int update_sched_domains(struct notifier_block *nfb, | |
7936 | unsigned long action, void *hcpu) | |
7937 | { | |
1da177e4 LT |
7938 | switch (action) { |
7939 | case CPU_UP_PREPARE: | |
8bb78442 | 7940 | case CPU_UP_PREPARE_FROZEN: |
1da177e4 | 7941 | case CPU_DOWN_PREPARE: |
8bb78442 | 7942 | case CPU_DOWN_PREPARE_FROZEN: |
1a20ff27 | 7943 | detach_destroy_domains(&cpu_online_map); |
1da177e4 LT |
7944 | return NOTIFY_OK; |
7945 | ||
7946 | case CPU_UP_CANCELED: | |
8bb78442 | 7947 | case CPU_UP_CANCELED_FROZEN: |
1da177e4 | 7948 | case CPU_DOWN_FAILED: |
8bb78442 | 7949 | case CPU_DOWN_FAILED_FROZEN: |
1da177e4 | 7950 | case CPU_ONLINE: |
8bb78442 | 7951 | case CPU_ONLINE_FROZEN: |
1da177e4 | 7952 | case CPU_DEAD: |
8bb78442 | 7953 | case CPU_DEAD_FROZEN: |
1da177e4 LT |
7954 | /* |
7955 | * Fall through and re-initialise the domains. | |
7956 | */ | |
7957 | break; | |
7958 | default: | |
7959 | return NOTIFY_DONE; | |
7960 | } | |
7961 | ||
7962 | /* The hotplug lock is already held by cpu_up/cpu_down */ | |
1a20ff27 | 7963 | arch_init_sched_domains(&cpu_online_map); |
1da177e4 LT |
7964 | |
7965 | return NOTIFY_OK; | |
7966 | } | |
1da177e4 LT |
7967 | |
7968 | void __init sched_init_smp(void) | |
7969 | { | |
5c1e1767 NP |
7970 | cpumask_t non_isolated_cpus; |
7971 | ||
434d53b0 MT |
7972 | #if defined(CONFIG_NUMA) |
7973 | sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **), | |
7974 | GFP_KERNEL); | |
7975 | BUG_ON(sched_group_nodes_bycpu == NULL); | |
7976 | #endif | |
95402b38 | 7977 | get_online_cpus(); |
1a20ff27 | 7978 | arch_init_sched_domains(&cpu_online_map); |
e5e5673f | 7979 | cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map); |
5c1e1767 NP |
7980 | if (cpus_empty(non_isolated_cpus)) |
7981 | cpu_set(smp_processor_id(), non_isolated_cpus); | |
95402b38 | 7982 | put_online_cpus(); |
1da177e4 LT |
7983 | /* XXX: Theoretical race here - CPU may be hotplugged now */ |
7984 | hotcpu_notifier(update_sched_domains, 0); | |
5c1e1767 NP |
7985 | |
7986 | /* Move init over to a non-isolated CPU */ | |
7c16ec58 | 7987 | if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0) |
5c1e1767 | 7988 | BUG(); |
19978ca6 | 7989 | sched_init_granularity(); |
1da177e4 LT |
7990 | } |
7991 | #else | |
7992 | void __init sched_init_smp(void) | |
7993 | { | |
434d53b0 MT |
7994 | #if defined(CONFIG_NUMA) |
7995 | sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **), | |
7996 | GFP_KERNEL); | |
7997 | BUG_ON(sched_group_nodes_bycpu == NULL); | |
7998 | #endif | |
19978ca6 | 7999 | sched_init_granularity(); |
1da177e4 LT |
8000 | } |
8001 | #endif /* CONFIG_SMP */ | |
8002 | ||
8003 | int in_sched_functions(unsigned long addr) | |
8004 | { | |
1da177e4 LT |
8005 | return in_lock_functions(addr) || |
8006 | (addr >= (unsigned long)__sched_text_start | |
8007 | && addr < (unsigned long)__sched_text_end); | |
8008 | } | |
8009 | ||
a9957449 | 8010 | static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq) |
dd41f596 IM |
8011 | { |
8012 | cfs_rq->tasks_timeline = RB_ROOT; | |
4a55bd5e | 8013 | INIT_LIST_HEAD(&cfs_rq->tasks); |
dd41f596 IM |
8014 | #ifdef CONFIG_FAIR_GROUP_SCHED |
8015 | cfs_rq->rq = rq; | |
8016 | #endif | |
67e9fb2a | 8017 | cfs_rq->min_vruntime = (u64)(-(1LL << 20)); |
dd41f596 IM |
8018 | } |
8019 | ||
fa85ae24 PZ |
8020 | static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq) |
8021 | { | |
8022 | struct rt_prio_array *array; | |
8023 | int i; | |
8024 | ||
8025 | array = &rt_rq->active; | |
8026 | for (i = 0; i < MAX_RT_PRIO; i++) { | |
8027 | INIT_LIST_HEAD(array->queue + i); | |
8028 | __clear_bit(i, array->bitmap); | |
8029 | } | |
8030 | /* delimiter for bitsearch: */ | |
8031 | __set_bit(MAX_RT_PRIO, array->bitmap); | |
8032 | ||
052f1dc7 | 8033 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED |
48d5e258 PZ |
8034 | rt_rq->highest_prio = MAX_RT_PRIO; |
8035 | #endif | |
fa85ae24 PZ |
8036 | #ifdef CONFIG_SMP |
8037 | rt_rq->rt_nr_migratory = 0; | |
fa85ae24 PZ |
8038 | rt_rq->overloaded = 0; |
8039 | #endif | |
8040 | ||
8041 | rt_rq->rt_time = 0; | |
8042 | rt_rq->rt_throttled = 0; | |
ac086bc2 PZ |
8043 | rt_rq->rt_runtime = 0; |
8044 | spin_lock_init(&rt_rq->rt_runtime_lock); | |
6f505b16 | 8045 | |
052f1dc7 | 8046 | #ifdef CONFIG_RT_GROUP_SCHED |
23b0fdfc | 8047 | rt_rq->rt_nr_boosted = 0; |
6f505b16 PZ |
8048 | rt_rq->rq = rq; |
8049 | #endif | |
fa85ae24 PZ |
8050 | } |
8051 | ||
6f505b16 | 8052 | #ifdef CONFIG_FAIR_GROUP_SCHED |
ec7dc8ac DG |
8053 | static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, |
8054 | struct sched_entity *se, int cpu, int add, | |
8055 | struct sched_entity *parent) | |
6f505b16 | 8056 | { |
ec7dc8ac | 8057 | struct rq *rq = cpu_rq(cpu); |
6f505b16 PZ |
8058 | tg->cfs_rq[cpu] = cfs_rq; |
8059 | init_cfs_rq(cfs_rq, rq); | |
8060 | cfs_rq->tg = tg; | |
8061 | if (add) | |
8062 | list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list); | |
8063 | ||
8064 | tg->se[cpu] = se; | |
354d60c2 DG |
8065 | /* se could be NULL for init_task_group */ |
8066 | if (!se) | |
8067 | return; | |
8068 | ||
ec7dc8ac DG |
8069 | if (!parent) |
8070 | se->cfs_rq = &rq->cfs; | |
8071 | else | |
8072 | se->cfs_rq = parent->my_q; | |
8073 | ||
6f505b16 PZ |
8074 | se->my_q = cfs_rq; |
8075 | se->load.weight = tg->shares; | |
8076 | se->load.inv_weight = div64_64(1ULL<<32, se->load.weight); | |
ec7dc8ac | 8077 | se->parent = parent; |
6f505b16 | 8078 | } |
052f1dc7 | 8079 | #endif |
6f505b16 | 8080 | |
052f1dc7 | 8081 | #ifdef CONFIG_RT_GROUP_SCHED |
ec7dc8ac DG |
8082 | static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq, |
8083 | struct sched_rt_entity *rt_se, int cpu, int add, | |
8084 | struct sched_rt_entity *parent) | |
6f505b16 | 8085 | { |
ec7dc8ac DG |
8086 | struct rq *rq = cpu_rq(cpu); |
8087 | ||
6f505b16 PZ |
8088 | tg->rt_rq[cpu] = rt_rq; |
8089 | init_rt_rq(rt_rq, rq); | |
8090 | rt_rq->tg = tg; | |
8091 | rt_rq->rt_se = rt_se; | |
ac086bc2 | 8092 | rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime; |
6f505b16 PZ |
8093 | if (add) |
8094 | list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list); | |
8095 | ||
8096 | tg->rt_se[cpu] = rt_se; | |
354d60c2 DG |
8097 | if (!rt_se) |
8098 | return; | |
8099 | ||
ec7dc8ac DG |
8100 | if (!parent) |
8101 | rt_se->rt_rq = &rq->rt; | |
8102 | else | |
8103 | rt_se->rt_rq = parent->my_q; | |
8104 | ||
6f505b16 PZ |
8105 | rt_se->rt_rq = &rq->rt; |
8106 | rt_se->my_q = rt_rq; | |
ec7dc8ac | 8107 | rt_se->parent = parent; |
6f505b16 PZ |
8108 | INIT_LIST_HEAD(&rt_se->run_list); |
8109 | } | |
8110 | #endif | |
8111 | ||
1da177e4 LT |
8112 | void __init sched_init(void) |
8113 | { | |
dd41f596 | 8114 | int i, j; |
434d53b0 MT |
8115 | unsigned long alloc_size = 0, ptr; |
8116 | ||
8117 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
8118 | alloc_size += 2 * nr_cpu_ids * sizeof(void **); | |
8119 | #endif | |
8120 | #ifdef CONFIG_RT_GROUP_SCHED | |
8121 | alloc_size += 2 * nr_cpu_ids * sizeof(void **); | |
eff766a6 PZ |
8122 | #endif |
8123 | #ifdef CONFIG_USER_SCHED | |
8124 | alloc_size *= 2; | |
434d53b0 MT |
8125 | #endif |
8126 | /* | |
8127 | * As sched_init() is called before page_alloc is setup, | |
8128 | * we use alloc_bootmem(). | |
8129 | */ | |
8130 | if (alloc_size) { | |
5a9d3225 | 8131 | ptr = (unsigned long)alloc_bootmem(alloc_size); |
434d53b0 MT |
8132 | |
8133 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
8134 | init_task_group.se = (struct sched_entity **)ptr; | |
8135 | ptr += nr_cpu_ids * sizeof(void **); | |
8136 | ||
8137 | init_task_group.cfs_rq = (struct cfs_rq **)ptr; | |
8138 | ptr += nr_cpu_ids * sizeof(void **); | |
eff766a6 PZ |
8139 | |
8140 | #ifdef CONFIG_USER_SCHED | |
8141 | root_task_group.se = (struct sched_entity **)ptr; | |
8142 | ptr += nr_cpu_ids * sizeof(void **); | |
8143 | ||
8144 | root_task_group.cfs_rq = (struct cfs_rq **)ptr; | |
8145 | ptr += nr_cpu_ids * sizeof(void **); | |
8146 | #endif | |
434d53b0 MT |
8147 | #endif |
8148 | #ifdef CONFIG_RT_GROUP_SCHED | |
8149 | init_task_group.rt_se = (struct sched_rt_entity **)ptr; | |
8150 | ptr += nr_cpu_ids * sizeof(void **); | |
8151 | ||
8152 | init_task_group.rt_rq = (struct rt_rq **)ptr; | |
eff766a6 PZ |
8153 | ptr += nr_cpu_ids * sizeof(void **); |
8154 | ||
8155 | #ifdef CONFIG_USER_SCHED | |
8156 | root_task_group.rt_se = (struct sched_rt_entity **)ptr; | |
8157 | ptr += nr_cpu_ids * sizeof(void **); | |
8158 | ||
8159 | root_task_group.rt_rq = (struct rt_rq **)ptr; | |
8160 | ptr += nr_cpu_ids * sizeof(void **); | |
8161 | #endif | |
434d53b0 MT |
8162 | #endif |
8163 | } | |
dd41f596 | 8164 | |
57d885fe | 8165 | #ifdef CONFIG_SMP |
18d95a28 | 8166 | init_aggregate(); |
57d885fe GH |
8167 | init_defrootdomain(); |
8168 | #endif | |
8169 | ||
d0b27fa7 PZ |
8170 | init_rt_bandwidth(&def_rt_bandwidth, |
8171 | global_rt_period(), global_rt_runtime()); | |
8172 | ||
8173 | #ifdef CONFIG_RT_GROUP_SCHED | |
8174 | init_rt_bandwidth(&init_task_group.rt_bandwidth, | |
8175 | global_rt_period(), global_rt_runtime()); | |
eff766a6 PZ |
8176 | #ifdef CONFIG_USER_SCHED |
8177 | init_rt_bandwidth(&root_task_group.rt_bandwidth, | |
8178 | global_rt_period(), RUNTIME_INF); | |
8179 | #endif | |
d0b27fa7 PZ |
8180 | #endif |
8181 | ||
052f1dc7 | 8182 | #ifdef CONFIG_GROUP_SCHED |
6f505b16 | 8183 | list_add(&init_task_group.list, &task_groups); |
f473aa5e PZ |
8184 | INIT_LIST_HEAD(&init_task_group.children); |
8185 | ||
8186 | #ifdef CONFIG_USER_SCHED | |
8187 | INIT_LIST_HEAD(&root_task_group.children); | |
8188 | init_task_group.parent = &root_task_group; | |
8189 | list_add(&init_task_group.siblings, &root_task_group.children); | |
8190 | #endif | |
6f505b16 PZ |
8191 | #endif |
8192 | ||
0a945022 | 8193 | for_each_possible_cpu(i) { |
70b97a7f | 8194 | struct rq *rq; |
1da177e4 LT |
8195 | |
8196 | rq = cpu_rq(i); | |
8197 | spin_lock_init(&rq->lock); | |
fcb99371 | 8198 | lockdep_set_class(&rq->lock, &rq->rq_lock_key); |
7897986b | 8199 | rq->nr_running = 0; |
dd41f596 | 8200 | rq->clock = 1; |
15934a37 | 8201 | update_last_tick_seen(rq); |
dd41f596 | 8202 | init_cfs_rq(&rq->cfs, rq); |
6f505b16 | 8203 | init_rt_rq(&rq->rt, rq); |
dd41f596 | 8204 | #ifdef CONFIG_FAIR_GROUP_SCHED |
4cf86d77 | 8205 | init_task_group.shares = init_task_group_load; |
6f505b16 | 8206 | INIT_LIST_HEAD(&rq->leaf_cfs_rq_list); |
354d60c2 DG |
8207 | #ifdef CONFIG_CGROUP_SCHED |
8208 | /* | |
8209 | * How much cpu bandwidth does init_task_group get? | |
8210 | * | |
8211 | * In case of task-groups formed thr' the cgroup filesystem, it | |
8212 | * gets 100% of the cpu resources in the system. This overall | |
8213 | * system cpu resource is divided among the tasks of | |
8214 | * init_task_group and its child task-groups in a fair manner, | |
8215 | * based on each entity's (task or task-group's) weight | |
8216 | * (se->load.weight). | |
8217 | * | |
8218 | * In other words, if init_task_group has 10 tasks of weight | |
8219 | * 1024) and two child groups A0 and A1 (of weight 1024 each), | |
8220 | * then A0's share of the cpu resource is: | |
8221 | * | |
8222 | * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33% | |
8223 | * | |
8224 | * We achieve this by letting init_task_group's tasks sit | |
8225 | * directly in rq->cfs (i.e init_task_group->se[] = NULL). | |
8226 | */ | |
ec7dc8ac | 8227 | init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL); |
354d60c2 | 8228 | #elif defined CONFIG_USER_SCHED |
eff766a6 PZ |
8229 | root_task_group.shares = NICE_0_LOAD; |
8230 | init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL); | |
354d60c2 DG |
8231 | /* |
8232 | * In case of task-groups formed thr' the user id of tasks, | |
8233 | * init_task_group represents tasks belonging to root user. | |
8234 | * Hence it forms a sibling of all subsequent groups formed. | |
8235 | * In this case, init_task_group gets only a fraction of overall | |
8236 | * system cpu resource, based on the weight assigned to root | |
8237 | * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished | |
8238 | * by letting tasks of init_task_group sit in a separate cfs_rq | |
8239 | * (init_cfs_rq) and having one entity represent this group of | |
8240 | * tasks in rq->cfs (i.e init_task_group->se[] != NULL). | |
8241 | */ | |
ec7dc8ac | 8242 | init_tg_cfs_entry(&init_task_group, |
6f505b16 | 8243 | &per_cpu(init_cfs_rq, i), |
eff766a6 PZ |
8244 | &per_cpu(init_sched_entity, i), i, 1, |
8245 | root_task_group.se[i]); | |
6f505b16 | 8246 | |
052f1dc7 | 8247 | #endif |
354d60c2 DG |
8248 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
8249 | ||
8250 | rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime; | |
052f1dc7 | 8251 | #ifdef CONFIG_RT_GROUP_SCHED |
6f505b16 | 8252 | INIT_LIST_HEAD(&rq->leaf_rt_rq_list); |
354d60c2 | 8253 | #ifdef CONFIG_CGROUP_SCHED |
ec7dc8ac | 8254 | init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL); |
354d60c2 | 8255 | #elif defined CONFIG_USER_SCHED |
eff766a6 | 8256 | init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL); |
ec7dc8ac | 8257 | init_tg_rt_entry(&init_task_group, |
6f505b16 | 8258 | &per_cpu(init_rt_rq, i), |
eff766a6 PZ |
8259 | &per_cpu(init_sched_rt_entity, i), i, 1, |
8260 | root_task_group.rt_se[i]); | |
354d60c2 | 8261 | #endif |
dd41f596 | 8262 | #endif |
1da177e4 | 8263 | |
dd41f596 IM |
8264 | for (j = 0; j < CPU_LOAD_IDX_MAX; j++) |
8265 | rq->cpu_load[j] = 0; | |
1da177e4 | 8266 | #ifdef CONFIG_SMP |
41c7ce9a | 8267 | rq->sd = NULL; |
57d885fe | 8268 | rq->rd = NULL; |
1da177e4 | 8269 | rq->active_balance = 0; |
dd41f596 | 8270 | rq->next_balance = jiffies; |
1da177e4 | 8271 | rq->push_cpu = 0; |
0a2966b4 | 8272 | rq->cpu = i; |
1da177e4 LT |
8273 | rq->migration_thread = NULL; |
8274 | INIT_LIST_HEAD(&rq->migration_queue); | |
dc938520 | 8275 | rq_attach_root(rq, &def_root_domain); |
1da177e4 | 8276 | #endif |
8f4d37ec | 8277 | init_rq_hrtick(rq); |
1da177e4 | 8278 | atomic_set(&rq->nr_iowait, 0); |
1da177e4 LT |
8279 | } |
8280 | ||
2dd73a4f | 8281 | set_load_weight(&init_task); |
b50f60ce | 8282 | |
e107be36 AK |
8283 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
8284 | INIT_HLIST_HEAD(&init_task.preempt_notifiers); | |
8285 | #endif | |
8286 | ||
c9819f45 CL |
8287 | #ifdef CONFIG_SMP |
8288 | open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL); | |
8289 | #endif | |
8290 | ||
b50f60ce HC |
8291 | #ifdef CONFIG_RT_MUTEXES |
8292 | plist_head_init(&init_task.pi_waiters, &init_task.pi_lock); | |
8293 | #endif | |
8294 | ||
1da177e4 LT |
8295 | /* |
8296 | * The boot idle thread does lazy MMU switching as well: | |
8297 | */ | |
8298 | atomic_inc(&init_mm.mm_count); | |
8299 | enter_lazy_tlb(&init_mm, current); | |
8300 | ||
8301 | /* | |
8302 | * Make us the idle thread. Technically, schedule() should not be | |
8303 | * called from this thread, however somewhere below it might be, | |
8304 | * but because we are the idle thread, we just pick up running again | |
8305 | * when this runqueue becomes "idle". | |
8306 | */ | |
8307 | init_idle(current, smp_processor_id()); | |
dd41f596 IM |
8308 | /* |
8309 | * During early bootup we pretend to be a normal task: | |
8310 | */ | |
8311 | current->sched_class = &fair_sched_class; | |
6892b75e IM |
8312 | |
8313 | scheduler_running = 1; | |
1da177e4 LT |
8314 | } |
8315 | ||
8316 | #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP | |
8317 | void __might_sleep(char *file, int line) | |
8318 | { | |
48f24c4d | 8319 | #ifdef in_atomic |
1da177e4 LT |
8320 | static unsigned long prev_jiffy; /* ratelimiting */ |
8321 | ||
8322 | if ((in_atomic() || irqs_disabled()) && | |
8323 | system_state == SYSTEM_RUNNING && !oops_in_progress) { | |
8324 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) | |
8325 | return; | |
8326 | prev_jiffy = jiffies; | |
91368d73 | 8327 | printk(KERN_ERR "BUG: sleeping function called from invalid" |
1da177e4 LT |
8328 | " context at %s:%d\n", file, line); |
8329 | printk("in_atomic():%d, irqs_disabled():%d\n", | |
8330 | in_atomic(), irqs_disabled()); | |
a4c410f0 | 8331 | debug_show_held_locks(current); |
3117df04 IM |
8332 | if (irqs_disabled()) |
8333 | print_irqtrace_events(current); | |
1da177e4 LT |
8334 | dump_stack(); |
8335 | } | |
8336 | #endif | |
8337 | } | |
8338 | EXPORT_SYMBOL(__might_sleep); | |
8339 | #endif | |
8340 | ||
8341 | #ifdef CONFIG_MAGIC_SYSRQ | |
3a5e4dc1 AK |
8342 | static void normalize_task(struct rq *rq, struct task_struct *p) |
8343 | { | |
8344 | int on_rq; | |
8345 | update_rq_clock(rq); | |
8346 | on_rq = p->se.on_rq; | |
8347 | if (on_rq) | |
8348 | deactivate_task(rq, p, 0); | |
8349 | __setscheduler(rq, p, SCHED_NORMAL, 0); | |
8350 | if (on_rq) { | |
8351 | activate_task(rq, p, 0); | |
8352 | resched_task(rq->curr); | |
8353 | } | |
8354 | } | |
8355 | ||
1da177e4 LT |
8356 | void normalize_rt_tasks(void) |
8357 | { | |
a0f98a1c | 8358 | struct task_struct *g, *p; |
1da177e4 | 8359 | unsigned long flags; |
70b97a7f | 8360 | struct rq *rq; |
1da177e4 | 8361 | |
4cf5d77a | 8362 | read_lock_irqsave(&tasklist_lock, flags); |
a0f98a1c | 8363 | do_each_thread(g, p) { |
178be793 IM |
8364 | /* |
8365 | * Only normalize user tasks: | |
8366 | */ | |
8367 | if (!p->mm) | |
8368 | continue; | |
8369 | ||
6cfb0d5d | 8370 | p->se.exec_start = 0; |
6cfb0d5d | 8371 | #ifdef CONFIG_SCHEDSTATS |
dd41f596 | 8372 | p->se.wait_start = 0; |
dd41f596 | 8373 | p->se.sleep_start = 0; |
dd41f596 | 8374 | p->se.block_start = 0; |
6cfb0d5d | 8375 | #endif |
dd41f596 IM |
8376 | task_rq(p)->clock = 0; |
8377 | ||
8378 | if (!rt_task(p)) { | |
8379 | /* | |
8380 | * Renice negative nice level userspace | |
8381 | * tasks back to 0: | |
8382 | */ | |
8383 | if (TASK_NICE(p) < 0 && p->mm) | |
8384 | set_user_nice(p, 0); | |
1da177e4 | 8385 | continue; |
dd41f596 | 8386 | } |
1da177e4 | 8387 | |
4cf5d77a | 8388 | spin_lock(&p->pi_lock); |
b29739f9 | 8389 | rq = __task_rq_lock(p); |
1da177e4 | 8390 | |
178be793 | 8391 | normalize_task(rq, p); |
3a5e4dc1 | 8392 | |
b29739f9 | 8393 | __task_rq_unlock(rq); |
4cf5d77a | 8394 | spin_unlock(&p->pi_lock); |
a0f98a1c IM |
8395 | } while_each_thread(g, p); |
8396 | ||
4cf5d77a | 8397 | read_unlock_irqrestore(&tasklist_lock, flags); |
1da177e4 LT |
8398 | } |
8399 | ||
8400 | #endif /* CONFIG_MAGIC_SYSRQ */ | |
1df5c10a LT |
8401 | |
8402 | #ifdef CONFIG_IA64 | |
8403 | /* | |
8404 | * These functions are only useful for the IA64 MCA handling. | |
8405 | * | |
8406 | * They can only be called when the whole system has been | |
8407 | * stopped - every CPU needs to be quiescent, and no scheduling | |
8408 | * activity can take place. Using them for anything else would | |
8409 | * be a serious bug, and as a result, they aren't even visible | |
8410 | * under any other configuration. | |
8411 | */ | |
8412 | ||
8413 | /** | |
8414 | * curr_task - return the current task for a given cpu. | |
8415 | * @cpu: the processor in question. | |
8416 | * | |
8417 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! | |
8418 | */ | |
36c8b586 | 8419 | struct task_struct *curr_task(int cpu) |
1df5c10a LT |
8420 | { |
8421 | return cpu_curr(cpu); | |
8422 | } | |
8423 | ||
8424 | /** | |
8425 | * set_curr_task - set the current task for a given cpu. | |
8426 | * @cpu: the processor in question. | |
8427 | * @p: the task pointer to set. | |
8428 | * | |
8429 | * Description: This function must only be used when non-maskable interrupts | |
41a2d6cf IM |
8430 | * are serviced on a separate stack. It allows the architecture to switch the |
8431 | * notion of the current task on a cpu in a non-blocking manner. This function | |
1df5c10a LT |
8432 | * must be called with all CPU's synchronized, and interrupts disabled, the |
8433 | * and caller must save the original value of the current task (see | |
8434 | * curr_task() above) and restore that value before reenabling interrupts and | |
8435 | * re-starting the system. | |
8436 | * | |
8437 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! | |
8438 | */ | |
36c8b586 | 8439 | void set_curr_task(int cpu, struct task_struct *p) |
1df5c10a LT |
8440 | { |
8441 | cpu_curr(cpu) = p; | |
8442 | } | |
8443 | ||
8444 | #endif | |
29f59db3 | 8445 | |
bccbe08a PZ |
8446 | #ifdef CONFIG_FAIR_GROUP_SCHED |
8447 | static void free_fair_sched_group(struct task_group *tg) | |
6f505b16 PZ |
8448 | { |
8449 | int i; | |
8450 | ||
8451 | for_each_possible_cpu(i) { | |
8452 | if (tg->cfs_rq) | |
8453 | kfree(tg->cfs_rq[i]); | |
8454 | if (tg->se) | |
8455 | kfree(tg->se[i]); | |
6f505b16 PZ |
8456 | } |
8457 | ||
8458 | kfree(tg->cfs_rq); | |
8459 | kfree(tg->se); | |
6f505b16 PZ |
8460 | } |
8461 | ||
ec7dc8ac DG |
8462 | static |
8463 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | |
29f59db3 | 8464 | { |
29f59db3 | 8465 | struct cfs_rq *cfs_rq; |
ec7dc8ac | 8466 | struct sched_entity *se, *parent_se; |
9b5b7751 | 8467 | struct rq *rq; |
29f59db3 SV |
8468 | int i; |
8469 | ||
434d53b0 | 8470 | tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL); |
29f59db3 SV |
8471 | if (!tg->cfs_rq) |
8472 | goto err; | |
434d53b0 | 8473 | tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL); |
29f59db3 SV |
8474 | if (!tg->se) |
8475 | goto err; | |
052f1dc7 PZ |
8476 | |
8477 | tg->shares = NICE_0_LOAD; | |
29f59db3 SV |
8478 | |
8479 | for_each_possible_cpu(i) { | |
9b5b7751 | 8480 | rq = cpu_rq(i); |
29f59db3 | 8481 | |
6f505b16 PZ |
8482 | cfs_rq = kmalloc_node(sizeof(struct cfs_rq), |
8483 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); | |
29f59db3 SV |
8484 | if (!cfs_rq) |
8485 | goto err; | |
8486 | ||
6f505b16 PZ |
8487 | se = kmalloc_node(sizeof(struct sched_entity), |
8488 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); | |
29f59db3 SV |
8489 | if (!se) |
8490 | goto err; | |
8491 | ||
ec7dc8ac DG |
8492 | parent_se = parent ? parent->se[i] : NULL; |
8493 | init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se); | |
bccbe08a PZ |
8494 | } |
8495 | ||
8496 | return 1; | |
8497 | ||
8498 | err: | |
8499 | return 0; | |
8500 | } | |
8501 | ||
8502 | static inline void register_fair_sched_group(struct task_group *tg, int cpu) | |
8503 | { | |
8504 | list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list, | |
8505 | &cpu_rq(cpu)->leaf_cfs_rq_list); | |
8506 | } | |
8507 | ||
8508 | static inline void unregister_fair_sched_group(struct task_group *tg, int cpu) | |
8509 | { | |
8510 | list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list); | |
8511 | } | |
8512 | #else | |
8513 | static inline void free_fair_sched_group(struct task_group *tg) | |
8514 | { | |
8515 | } | |
8516 | ||
ec7dc8ac DG |
8517 | static inline |
8518 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) | |
bccbe08a PZ |
8519 | { |
8520 | return 1; | |
8521 | } | |
8522 | ||
8523 | static inline void register_fair_sched_group(struct task_group *tg, int cpu) | |
8524 | { | |
8525 | } | |
8526 | ||
8527 | static inline void unregister_fair_sched_group(struct task_group *tg, int cpu) | |
8528 | { | |
8529 | } | |
052f1dc7 PZ |
8530 | #endif |
8531 | ||
8532 | #ifdef CONFIG_RT_GROUP_SCHED | |
bccbe08a PZ |
8533 | static void free_rt_sched_group(struct task_group *tg) |
8534 | { | |
8535 | int i; | |
8536 | ||
d0b27fa7 PZ |
8537 | destroy_rt_bandwidth(&tg->rt_bandwidth); |
8538 | ||
bccbe08a PZ |
8539 | for_each_possible_cpu(i) { |
8540 | if (tg->rt_rq) | |
8541 | kfree(tg->rt_rq[i]); | |
8542 | if (tg->rt_se) | |
8543 | kfree(tg->rt_se[i]); | |
8544 | } | |
8545 | ||
8546 | kfree(tg->rt_rq); | |
8547 | kfree(tg->rt_se); | |
8548 | } | |
8549 | ||
ec7dc8ac DG |
8550 | static |
8551 | int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) | |
bccbe08a PZ |
8552 | { |
8553 | struct rt_rq *rt_rq; | |
ec7dc8ac | 8554 | struct sched_rt_entity *rt_se, *parent_se; |
bccbe08a PZ |
8555 | struct rq *rq; |
8556 | int i; | |
8557 | ||
434d53b0 | 8558 | tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL); |
bccbe08a PZ |
8559 | if (!tg->rt_rq) |
8560 | goto err; | |
434d53b0 | 8561 | tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL); |
bccbe08a PZ |
8562 | if (!tg->rt_se) |
8563 | goto err; | |
8564 | ||
d0b27fa7 PZ |
8565 | init_rt_bandwidth(&tg->rt_bandwidth, |
8566 | ktime_to_ns(def_rt_bandwidth.rt_period), 0); | |
bccbe08a PZ |
8567 | |
8568 | for_each_possible_cpu(i) { | |
8569 | rq = cpu_rq(i); | |
8570 | ||
6f505b16 PZ |
8571 | rt_rq = kmalloc_node(sizeof(struct rt_rq), |
8572 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); | |
8573 | if (!rt_rq) | |
8574 | goto err; | |
29f59db3 | 8575 | |
6f505b16 PZ |
8576 | rt_se = kmalloc_node(sizeof(struct sched_rt_entity), |
8577 | GFP_KERNEL|__GFP_ZERO, cpu_to_node(i)); | |
8578 | if (!rt_se) | |
8579 | goto err; | |
29f59db3 | 8580 | |
ec7dc8ac DG |
8581 | parent_se = parent ? parent->rt_se[i] : NULL; |
8582 | init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent_se); | |
29f59db3 SV |
8583 | } |
8584 | ||
bccbe08a PZ |
8585 | return 1; |
8586 | ||
8587 | err: | |
8588 | return 0; | |
8589 | } | |
8590 | ||
8591 | static inline void register_rt_sched_group(struct task_group *tg, int cpu) | |
8592 | { | |
8593 | list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list, | |
8594 | &cpu_rq(cpu)->leaf_rt_rq_list); | |
8595 | } | |
8596 | ||
8597 | static inline void unregister_rt_sched_group(struct task_group *tg, int cpu) | |
8598 | { | |
8599 | list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list); | |
8600 | } | |
8601 | #else | |
8602 | static inline void free_rt_sched_group(struct task_group *tg) | |
8603 | { | |
8604 | } | |
8605 | ||
ec7dc8ac DG |
8606 | static inline |
8607 | int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) | |
bccbe08a PZ |
8608 | { |
8609 | return 1; | |
8610 | } | |
8611 | ||
8612 | static inline void register_rt_sched_group(struct task_group *tg, int cpu) | |
8613 | { | |
8614 | } | |
8615 | ||
8616 | static inline void unregister_rt_sched_group(struct task_group *tg, int cpu) | |
8617 | { | |
8618 | } | |
8619 | #endif | |
8620 | ||
d0b27fa7 | 8621 | #ifdef CONFIG_GROUP_SCHED |
bccbe08a PZ |
8622 | static void free_sched_group(struct task_group *tg) |
8623 | { | |
8624 | free_fair_sched_group(tg); | |
8625 | free_rt_sched_group(tg); | |
8626 | kfree(tg); | |
8627 | } | |
8628 | ||
8629 | /* allocate runqueue etc for a new task group */ | |
ec7dc8ac | 8630 | struct task_group *sched_create_group(struct task_group *parent) |
bccbe08a PZ |
8631 | { |
8632 | struct task_group *tg; | |
8633 | unsigned long flags; | |
8634 | int i; | |
8635 | ||
8636 | tg = kzalloc(sizeof(*tg), GFP_KERNEL); | |
8637 | if (!tg) | |
8638 | return ERR_PTR(-ENOMEM); | |
8639 | ||
ec7dc8ac | 8640 | if (!alloc_fair_sched_group(tg, parent)) |
bccbe08a PZ |
8641 | goto err; |
8642 | ||
ec7dc8ac | 8643 | if (!alloc_rt_sched_group(tg, parent)) |
bccbe08a PZ |
8644 | goto err; |
8645 | ||
8ed36996 | 8646 | spin_lock_irqsave(&task_group_lock, flags); |
9b5b7751 | 8647 | for_each_possible_cpu(i) { |
bccbe08a PZ |
8648 | register_fair_sched_group(tg, i); |
8649 | register_rt_sched_group(tg, i); | |
9b5b7751 | 8650 | } |
6f505b16 | 8651 | list_add_rcu(&tg->list, &task_groups); |
f473aa5e PZ |
8652 | |
8653 | WARN_ON(!parent); /* root should already exist */ | |
8654 | ||
8655 | tg->parent = parent; | |
8656 | list_add_rcu(&tg->siblings, &parent->children); | |
8657 | INIT_LIST_HEAD(&tg->children); | |
8ed36996 | 8658 | spin_unlock_irqrestore(&task_group_lock, flags); |
29f59db3 | 8659 | |
9b5b7751 | 8660 | return tg; |
29f59db3 SV |
8661 | |
8662 | err: | |
6f505b16 | 8663 | free_sched_group(tg); |
29f59db3 SV |
8664 | return ERR_PTR(-ENOMEM); |
8665 | } | |
8666 | ||
9b5b7751 | 8667 | /* rcu callback to free various structures associated with a task group */ |
6f505b16 | 8668 | static void free_sched_group_rcu(struct rcu_head *rhp) |
29f59db3 | 8669 | { |
29f59db3 | 8670 | /* now it should be safe to free those cfs_rqs */ |
6f505b16 | 8671 | free_sched_group(container_of(rhp, struct task_group, rcu)); |
29f59db3 SV |
8672 | } |
8673 | ||
9b5b7751 | 8674 | /* Destroy runqueue etc associated with a task group */ |
4cf86d77 | 8675 | void sched_destroy_group(struct task_group *tg) |
29f59db3 | 8676 | { |
8ed36996 | 8677 | unsigned long flags; |
9b5b7751 | 8678 | int i; |
29f59db3 | 8679 | |
8ed36996 | 8680 | spin_lock_irqsave(&task_group_lock, flags); |
9b5b7751 | 8681 | for_each_possible_cpu(i) { |
bccbe08a PZ |
8682 | unregister_fair_sched_group(tg, i); |
8683 | unregister_rt_sched_group(tg, i); | |
9b5b7751 | 8684 | } |
6f505b16 | 8685 | list_del_rcu(&tg->list); |
f473aa5e | 8686 | list_del_rcu(&tg->siblings); |
8ed36996 | 8687 | spin_unlock_irqrestore(&task_group_lock, flags); |
9b5b7751 | 8688 | |
9b5b7751 | 8689 | /* wait for possible concurrent references to cfs_rqs complete */ |
6f505b16 | 8690 | call_rcu(&tg->rcu, free_sched_group_rcu); |
29f59db3 SV |
8691 | } |
8692 | ||
9b5b7751 | 8693 | /* change task's runqueue when it moves between groups. |
3a252015 IM |
8694 | * The caller of this function should have put the task in its new group |
8695 | * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to | |
8696 | * reflect its new group. | |
9b5b7751 SV |
8697 | */ |
8698 | void sched_move_task(struct task_struct *tsk) | |
29f59db3 SV |
8699 | { |
8700 | int on_rq, running; | |
8701 | unsigned long flags; | |
8702 | struct rq *rq; | |
8703 | ||
8704 | rq = task_rq_lock(tsk, &flags); | |
8705 | ||
29f59db3 SV |
8706 | update_rq_clock(rq); |
8707 | ||
051a1d1a | 8708 | running = task_current(rq, tsk); |
29f59db3 SV |
8709 | on_rq = tsk->se.on_rq; |
8710 | ||
0e1f3483 | 8711 | if (on_rq) |
29f59db3 | 8712 | dequeue_task(rq, tsk, 0); |
0e1f3483 HS |
8713 | if (unlikely(running)) |
8714 | tsk->sched_class->put_prev_task(rq, tsk); | |
29f59db3 | 8715 | |
6f505b16 | 8716 | set_task_rq(tsk, task_cpu(tsk)); |
29f59db3 | 8717 | |
810b3817 PZ |
8718 | #ifdef CONFIG_FAIR_GROUP_SCHED |
8719 | if (tsk->sched_class->moved_group) | |
8720 | tsk->sched_class->moved_group(tsk); | |
8721 | #endif | |
8722 | ||
0e1f3483 HS |
8723 | if (unlikely(running)) |
8724 | tsk->sched_class->set_curr_task(rq); | |
8725 | if (on_rq) | |
7074badb | 8726 | enqueue_task(rq, tsk, 0); |
29f59db3 | 8727 | |
29f59db3 SV |
8728 | task_rq_unlock(rq, &flags); |
8729 | } | |
d0b27fa7 | 8730 | #endif |
29f59db3 | 8731 | |
052f1dc7 | 8732 | #ifdef CONFIG_FAIR_GROUP_SCHED |
18d95a28 | 8733 | static void __set_se_shares(struct sched_entity *se, unsigned long shares) |
29f59db3 SV |
8734 | { |
8735 | struct cfs_rq *cfs_rq = se->cfs_rq; | |
29f59db3 SV |
8736 | int on_rq; |
8737 | ||
29f59db3 | 8738 | on_rq = se->on_rq; |
62fb1851 | 8739 | if (on_rq) |
29f59db3 SV |
8740 | dequeue_entity(cfs_rq, se, 0); |
8741 | ||
8742 | se->load.weight = shares; | |
8743 | se->load.inv_weight = div64_64((1ULL<<32), shares); | |
8744 | ||
62fb1851 | 8745 | if (on_rq) |
29f59db3 | 8746 | enqueue_entity(cfs_rq, se, 0); |
18d95a28 | 8747 | } |
62fb1851 | 8748 | |
18d95a28 PZ |
8749 | static void set_se_shares(struct sched_entity *se, unsigned long shares) |
8750 | { | |
8751 | struct cfs_rq *cfs_rq = se->cfs_rq; | |
8752 | struct rq *rq = cfs_rq->rq; | |
8753 | unsigned long flags; | |
8754 | ||
8755 | spin_lock_irqsave(&rq->lock, flags); | |
8756 | __set_se_shares(se, shares); | |
8757 | spin_unlock_irqrestore(&rq->lock, flags); | |
29f59db3 SV |
8758 | } |
8759 | ||
8ed36996 PZ |
8760 | static DEFINE_MUTEX(shares_mutex); |
8761 | ||
4cf86d77 | 8762 | int sched_group_set_shares(struct task_group *tg, unsigned long shares) |
29f59db3 SV |
8763 | { |
8764 | int i; | |
8ed36996 | 8765 | unsigned long flags; |
c61935fd | 8766 | |
ec7dc8ac DG |
8767 | /* |
8768 | * We can't change the weight of the root cgroup. | |
8769 | */ | |
8770 | if (!tg->se[0]) | |
8771 | return -EINVAL; | |
8772 | ||
62fb1851 PZ |
8773 | /* |
8774 | * A weight of 0 or 1 can cause arithmetics problems. | |
8775 | * (The default weight is 1024 - so there's no practical | |
8776 | * limitation from this.) | |
8777 | */ | |
18d95a28 PZ |
8778 | if (shares < MIN_SHARES) |
8779 | shares = MIN_SHARES; | |
62fb1851 | 8780 | |
8ed36996 | 8781 | mutex_lock(&shares_mutex); |
9b5b7751 | 8782 | if (tg->shares == shares) |
5cb350ba | 8783 | goto done; |
29f59db3 | 8784 | |
8ed36996 | 8785 | spin_lock_irqsave(&task_group_lock, flags); |
bccbe08a PZ |
8786 | for_each_possible_cpu(i) |
8787 | unregister_fair_sched_group(tg, i); | |
f473aa5e | 8788 | list_del_rcu(&tg->siblings); |
8ed36996 | 8789 | spin_unlock_irqrestore(&task_group_lock, flags); |
6b2d7700 SV |
8790 | |
8791 | /* wait for any ongoing reference to this group to finish */ | |
8792 | synchronize_sched(); | |
8793 | ||
8794 | /* | |
8795 | * Now we are free to modify the group's share on each cpu | |
8796 | * w/o tripping rebalance_share or load_balance_fair. | |
8797 | */ | |
9b5b7751 | 8798 | tg->shares = shares; |
18d95a28 PZ |
8799 | for_each_possible_cpu(i) { |
8800 | /* | |
8801 | * force a rebalance | |
8802 | */ | |
8803 | cfs_rq_set_shares(tg->cfs_rq[i], 0); | |
8804 | set_se_shares(tg->se[i], shares/nr_cpu_ids); | |
8805 | } | |
29f59db3 | 8806 | |
6b2d7700 SV |
8807 | /* |
8808 | * Enable load balance activity on this group, by inserting it back on | |
8809 | * each cpu's rq->leaf_cfs_rq_list. | |
8810 | */ | |
8ed36996 | 8811 | spin_lock_irqsave(&task_group_lock, flags); |
bccbe08a PZ |
8812 | for_each_possible_cpu(i) |
8813 | register_fair_sched_group(tg, i); | |
f473aa5e | 8814 | list_add_rcu(&tg->siblings, &tg->parent->children); |
8ed36996 | 8815 | spin_unlock_irqrestore(&task_group_lock, flags); |
5cb350ba | 8816 | done: |
8ed36996 | 8817 | mutex_unlock(&shares_mutex); |
9b5b7751 | 8818 | return 0; |
29f59db3 SV |
8819 | } |
8820 | ||
5cb350ba DG |
8821 | unsigned long sched_group_shares(struct task_group *tg) |
8822 | { | |
8823 | return tg->shares; | |
8824 | } | |
052f1dc7 | 8825 | #endif |
5cb350ba | 8826 | |
052f1dc7 | 8827 | #ifdef CONFIG_RT_GROUP_SCHED |
6f505b16 | 8828 | /* |
9f0c1e56 | 8829 | * Ensure that the real time constraints are schedulable. |
6f505b16 | 8830 | */ |
9f0c1e56 PZ |
8831 | static DEFINE_MUTEX(rt_constraints_mutex); |
8832 | ||
8833 | static unsigned long to_ratio(u64 period, u64 runtime) | |
8834 | { | |
8835 | if (runtime == RUNTIME_INF) | |
8836 | return 1ULL << 16; | |
8837 | ||
2692a240 | 8838 | return div64_64(runtime << 16, period); |
9f0c1e56 PZ |
8839 | } |
8840 | ||
b40b2e8e PZ |
8841 | #ifdef CONFIG_CGROUP_SCHED |
8842 | static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime) | |
8843 | { | |
8844 | struct task_group *tgi, *parent = tg->parent; | |
8845 | unsigned long total = 0; | |
8846 | ||
8847 | if (!parent) { | |
8848 | if (global_rt_period() < period) | |
8849 | return 0; | |
8850 | ||
8851 | return to_ratio(period, runtime) < | |
8852 | to_ratio(global_rt_period(), global_rt_runtime()); | |
8853 | } | |
8854 | ||
8855 | if (ktime_to_ns(parent->rt_bandwidth.rt_period) < period) | |
8856 | return 0; | |
8857 | ||
8858 | rcu_read_lock(); | |
8859 | list_for_each_entry_rcu(tgi, &parent->children, siblings) { | |
8860 | if (tgi == tg) | |
8861 | continue; | |
8862 | ||
8863 | total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period), | |
8864 | tgi->rt_bandwidth.rt_runtime); | |
8865 | } | |
8866 | rcu_read_unlock(); | |
8867 | ||
8868 | return total + to_ratio(period, runtime) < | |
8869 | to_ratio(ktime_to_ns(parent->rt_bandwidth.rt_period), | |
8870 | parent->rt_bandwidth.rt_runtime); | |
8871 | } | |
8872 | #elif defined CONFIG_USER_SCHED | |
9f0c1e56 | 8873 | static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime) |
6f505b16 PZ |
8874 | { |
8875 | struct task_group *tgi; | |
8876 | unsigned long total = 0; | |
9f0c1e56 | 8877 | unsigned long global_ratio = |
d0b27fa7 | 8878 | to_ratio(global_rt_period(), global_rt_runtime()); |
6f505b16 PZ |
8879 | |
8880 | rcu_read_lock(); | |
9f0c1e56 PZ |
8881 | list_for_each_entry_rcu(tgi, &task_groups, list) { |
8882 | if (tgi == tg) | |
8883 | continue; | |
6f505b16 | 8884 | |
d0b27fa7 PZ |
8885 | total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period), |
8886 | tgi->rt_bandwidth.rt_runtime); | |
9f0c1e56 PZ |
8887 | } |
8888 | rcu_read_unlock(); | |
6f505b16 | 8889 | |
9f0c1e56 | 8890 | return total + to_ratio(period, runtime) < global_ratio; |
6f505b16 | 8891 | } |
b40b2e8e | 8892 | #endif |
6f505b16 | 8893 | |
521f1a24 DG |
8894 | /* Must be called with tasklist_lock held */ |
8895 | static inline int tg_has_rt_tasks(struct task_group *tg) | |
8896 | { | |
8897 | struct task_struct *g, *p; | |
8898 | do_each_thread(g, p) { | |
8899 | if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg) | |
8900 | return 1; | |
8901 | } while_each_thread(g, p); | |
8902 | return 0; | |
8903 | } | |
8904 | ||
d0b27fa7 PZ |
8905 | static int tg_set_bandwidth(struct task_group *tg, |
8906 | u64 rt_period, u64 rt_runtime) | |
6f505b16 | 8907 | { |
ac086bc2 | 8908 | int i, err = 0; |
9f0c1e56 | 8909 | |
9f0c1e56 | 8910 | mutex_lock(&rt_constraints_mutex); |
521f1a24 | 8911 | read_lock(&tasklist_lock); |
ac086bc2 | 8912 | if (rt_runtime == 0 && tg_has_rt_tasks(tg)) { |
521f1a24 DG |
8913 | err = -EBUSY; |
8914 | goto unlock; | |
8915 | } | |
9f0c1e56 PZ |
8916 | if (!__rt_schedulable(tg, rt_period, rt_runtime)) { |
8917 | err = -EINVAL; | |
8918 | goto unlock; | |
8919 | } | |
ac086bc2 PZ |
8920 | |
8921 | spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock); | |
d0b27fa7 PZ |
8922 | tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period); |
8923 | tg->rt_bandwidth.rt_runtime = rt_runtime; | |
ac086bc2 PZ |
8924 | |
8925 | for_each_possible_cpu(i) { | |
8926 | struct rt_rq *rt_rq = tg->rt_rq[i]; | |
8927 | ||
8928 | spin_lock(&rt_rq->rt_runtime_lock); | |
8929 | rt_rq->rt_runtime = rt_runtime; | |
8930 | spin_unlock(&rt_rq->rt_runtime_lock); | |
8931 | } | |
8932 | spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock); | |
9f0c1e56 | 8933 | unlock: |
521f1a24 | 8934 | read_unlock(&tasklist_lock); |
9f0c1e56 PZ |
8935 | mutex_unlock(&rt_constraints_mutex); |
8936 | ||
8937 | return err; | |
6f505b16 PZ |
8938 | } |
8939 | ||
d0b27fa7 PZ |
8940 | int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us) |
8941 | { | |
8942 | u64 rt_runtime, rt_period; | |
8943 | ||
8944 | rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period); | |
8945 | rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC; | |
8946 | if (rt_runtime_us < 0) | |
8947 | rt_runtime = RUNTIME_INF; | |
8948 | ||
8949 | return tg_set_bandwidth(tg, rt_period, rt_runtime); | |
8950 | } | |
8951 | ||
9f0c1e56 PZ |
8952 | long sched_group_rt_runtime(struct task_group *tg) |
8953 | { | |
8954 | u64 rt_runtime_us; | |
8955 | ||
d0b27fa7 | 8956 | if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF) |
9f0c1e56 PZ |
8957 | return -1; |
8958 | ||
d0b27fa7 | 8959 | rt_runtime_us = tg->rt_bandwidth.rt_runtime; |
9f0c1e56 PZ |
8960 | do_div(rt_runtime_us, NSEC_PER_USEC); |
8961 | return rt_runtime_us; | |
8962 | } | |
d0b27fa7 PZ |
8963 | |
8964 | int sched_group_set_rt_period(struct task_group *tg, long rt_period_us) | |
8965 | { | |
8966 | u64 rt_runtime, rt_period; | |
8967 | ||
8968 | rt_period = (u64)rt_period_us * NSEC_PER_USEC; | |
8969 | rt_runtime = tg->rt_bandwidth.rt_runtime; | |
8970 | ||
8971 | return tg_set_bandwidth(tg, rt_period, rt_runtime); | |
8972 | } | |
8973 | ||
8974 | long sched_group_rt_period(struct task_group *tg) | |
8975 | { | |
8976 | u64 rt_period_us; | |
8977 | ||
8978 | rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period); | |
8979 | do_div(rt_period_us, NSEC_PER_USEC); | |
8980 | return rt_period_us; | |
8981 | } | |
8982 | ||
8983 | static int sched_rt_global_constraints(void) | |
8984 | { | |
8985 | int ret = 0; | |
8986 | ||
8987 | mutex_lock(&rt_constraints_mutex); | |
8988 | if (!__rt_schedulable(NULL, 1, 0)) | |
8989 | ret = -EINVAL; | |
8990 | mutex_unlock(&rt_constraints_mutex); | |
8991 | ||
8992 | return ret; | |
8993 | } | |
8994 | #else | |
8995 | static int sched_rt_global_constraints(void) | |
8996 | { | |
ac086bc2 PZ |
8997 | unsigned long flags; |
8998 | int i; | |
8999 | ||
9000 | spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags); | |
9001 | for_each_possible_cpu(i) { | |
9002 | struct rt_rq *rt_rq = &cpu_rq(i)->rt; | |
9003 | ||
9004 | spin_lock(&rt_rq->rt_runtime_lock); | |
9005 | rt_rq->rt_runtime = global_rt_runtime(); | |
9006 | spin_unlock(&rt_rq->rt_runtime_lock); | |
9007 | } | |
9008 | spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags); | |
9009 | ||
d0b27fa7 PZ |
9010 | return 0; |
9011 | } | |
052f1dc7 | 9012 | #endif |
d0b27fa7 PZ |
9013 | |
9014 | int sched_rt_handler(struct ctl_table *table, int write, | |
9015 | struct file *filp, void __user *buffer, size_t *lenp, | |
9016 | loff_t *ppos) | |
9017 | { | |
9018 | int ret; | |
9019 | int old_period, old_runtime; | |
9020 | static DEFINE_MUTEX(mutex); | |
9021 | ||
9022 | mutex_lock(&mutex); | |
9023 | old_period = sysctl_sched_rt_period; | |
9024 | old_runtime = sysctl_sched_rt_runtime; | |
9025 | ||
9026 | ret = proc_dointvec(table, write, filp, buffer, lenp, ppos); | |
9027 | ||
9028 | if (!ret && write) { | |
9029 | ret = sched_rt_global_constraints(); | |
9030 | if (ret) { | |
9031 | sysctl_sched_rt_period = old_period; | |
9032 | sysctl_sched_rt_runtime = old_runtime; | |
9033 | } else { | |
9034 | def_rt_bandwidth.rt_runtime = global_rt_runtime(); | |
9035 | def_rt_bandwidth.rt_period = | |
9036 | ns_to_ktime(global_rt_period()); | |
9037 | } | |
9038 | } | |
9039 | mutex_unlock(&mutex); | |
9040 | ||
9041 | return ret; | |
9042 | } | |
68318b8e | 9043 | |
052f1dc7 | 9044 | #ifdef CONFIG_CGROUP_SCHED |
68318b8e SV |
9045 | |
9046 | /* return corresponding task_group object of a cgroup */ | |
2b01dfe3 | 9047 | static inline struct task_group *cgroup_tg(struct cgroup *cgrp) |
68318b8e | 9048 | { |
2b01dfe3 PM |
9049 | return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id), |
9050 | struct task_group, css); | |
68318b8e SV |
9051 | } |
9052 | ||
9053 | static struct cgroup_subsys_state * | |
2b01dfe3 | 9054 | cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp) |
68318b8e | 9055 | { |
ec7dc8ac | 9056 | struct task_group *tg, *parent; |
68318b8e | 9057 | |
2b01dfe3 | 9058 | if (!cgrp->parent) { |
68318b8e | 9059 | /* This is early initialization for the top cgroup */ |
2b01dfe3 | 9060 | init_task_group.css.cgroup = cgrp; |
68318b8e SV |
9061 | return &init_task_group.css; |
9062 | } | |
9063 | ||
ec7dc8ac DG |
9064 | parent = cgroup_tg(cgrp->parent); |
9065 | tg = sched_create_group(parent); | |
68318b8e SV |
9066 | if (IS_ERR(tg)) |
9067 | return ERR_PTR(-ENOMEM); | |
9068 | ||
9069 | /* Bind the cgroup to task_group object we just created */ | |
2b01dfe3 | 9070 | tg->css.cgroup = cgrp; |
68318b8e SV |
9071 | |
9072 | return &tg->css; | |
9073 | } | |
9074 | ||
41a2d6cf IM |
9075 | static void |
9076 | cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp) | |
68318b8e | 9077 | { |
2b01dfe3 | 9078 | struct task_group *tg = cgroup_tg(cgrp); |
68318b8e SV |
9079 | |
9080 | sched_destroy_group(tg); | |
9081 | } | |
9082 | ||
41a2d6cf IM |
9083 | static int |
9084 | cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp, | |
9085 | struct task_struct *tsk) | |
68318b8e | 9086 | { |
b68aa230 PZ |
9087 | #ifdef CONFIG_RT_GROUP_SCHED |
9088 | /* Don't accept realtime tasks when there is no way for them to run */ | |
d0b27fa7 | 9089 | if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0) |
b68aa230 PZ |
9090 | return -EINVAL; |
9091 | #else | |
68318b8e SV |
9092 | /* We don't support RT-tasks being in separate groups */ |
9093 | if (tsk->sched_class != &fair_sched_class) | |
9094 | return -EINVAL; | |
b68aa230 | 9095 | #endif |
68318b8e SV |
9096 | |
9097 | return 0; | |
9098 | } | |
9099 | ||
9100 | static void | |
2b01dfe3 | 9101 | cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp, |
68318b8e SV |
9102 | struct cgroup *old_cont, struct task_struct *tsk) |
9103 | { | |
9104 | sched_move_task(tsk); | |
9105 | } | |
9106 | ||
052f1dc7 | 9107 | #ifdef CONFIG_FAIR_GROUP_SCHED |
2b01dfe3 PM |
9108 | static int cpu_shares_write_uint(struct cgroup *cgrp, struct cftype *cftype, |
9109 | u64 shareval) | |
68318b8e | 9110 | { |
2b01dfe3 | 9111 | return sched_group_set_shares(cgroup_tg(cgrp), shareval); |
68318b8e SV |
9112 | } |
9113 | ||
2b01dfe3 | 9114 | static u64 cpu_shares_read_uint(struct cgroup *cgrp, struct cftype *cft) |
68318b8e | 9115 | { |
2b01dfe3 | 9116 | struct task_group *tg = cgroup_tg(cgrp); |
68318b8e SV |
9117 | |
9118 | return (u64) tg->shares; | |
9119 | } | |
052f1dc7 | 9120 | #endif |
68318b8e | 9121 | |
052f1dc7 | 9122 | #ifdef CONFIG_RT_GROUP_SCHED |
ac086bc2 | 9123 | static ssize_t cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft, |
9f0c1e56 PZ |
9124 | struct file *file, |
9125 | const char __user *userbuf, | |
9126 | size_t nbytes, loff_t *unused_ppos) | |
6f505b16 | 9127 | { |
9f0c1e56 PZ |
9128 | char buffer[64]; |
9129 | int retval = 0; | |
9130 | s64 val; | |
9131 | char *end; | |
9132 | ||
9133 | if (!nbytes) | |
9134 | return -EINVAL; | |
9135 | if (nbytes >= sizeof(buffer)) | |
9136 | return -E2BIG; | |
9137 | if (copy_from_user(buffer, userbuf, nbytes)) | |
9138 | return -EFAULT; | |
9139 | ||
9140 | buffer[nbytes] = 0; /* nul-terminate */ | |
9141 | ||
9142 | /* strip newline if necessary */ | |
9143 | if (nbytes && (buffer[nbytes-1] == '\n')) | |
9144 | buffer[nbytes-1] = 0; | |
9145 | val = simple_strtoll(buffer, &end, 0); | |
9146 | if (*end) | |
9147 | return -EINVAL; | |
9148 | ||
9149 | /* Pass to subsystem */ | |
9150 | retval = sched_group_set_rt_runtime(cgroup_tg(cgrp), val); | |
9151 | if (!retval) | |
9152 | retval = nbytes; | |
9153 | return retval; | |
6f505b16 PZ |
9154 | } |
9155 | ||
9f0c1e56 PZ |
9156 | static ssize_t cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft, |
9157 | struct file *file, | |
9158 | char __user *buf, size_t nbytes, | |
9159 | loff_t *ppos) | |
6f505b16 | 9160 | { |
9f0c1e56 PZ |
9161 | char tmp[64]; |
9162 | long val = sched_group_rt_runtime(cgroup_tg(cgrp)); | |
9163 | int len = sprintf(tmp, "%ld\n", val); | |
6f505b16 | 9164 | |
9f0c1e56 | 9165 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
6f505b16 | 9166 | } |
d0b27fa7 PZ |
9167 | |
9168 | static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype, | |
9169 | u64 rt_period_us) | |
9170 | { | |
9171 | return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us); | |
9172 | } | |
9173 | ||
9174 | static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft) | |
9175 | { | |
9176 | return sched_group_rt_period(cgroup_tg(cgrp)); | |
9177 | } | |
052f1dc7 | 9178 | #endif |
6f505b16 | 9179 | |
fe5c7cc2 | 9180 | static struct cftype cpu_files[] = { |
052f1dc7 | 9181 | #ifdef CONFIG_FAIR_GROUP_SCHED |
fe5c7cc2 PM |
9182 | { |
9183 | .name = "shares", | |
9184 | .read_uint = cpu_shares_read_uint, | |
9185 | .write_uint = cpu_shares_write_uint, | |
9186 | }, | |
052f1dc7 PZ |
9187 | #endif |
9188 | #ifdef CONFIG_RT_GROUP_SCHED | |
6f505b16 | 9189 | { |
9f0c1e56 PZ |
9190 | .name = "rt_runtime_us", |
9191 | .read = cpu_rt_runtime_read, | |
9192 | .write = cpu_rt_runtime_write, | |
6f505b16 | 9193 | }, |
d0b27fa7 PZ |
9194 | { |
9195 | .name = "rt_period_us", | |
9196 | .read_uint = cpu_rt_period_read_uint, | |
9197 | .write_uint = cpu_rt_period_write_uint, | |
9198 | }, | |
052f1dc7 | 9199 | #endif |
68318b8e SV |
9200 | }; |
9201 | ||
9202 | static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont) | |
9203 | { | |
fe5c7cc2 | 9204 | return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files)); |
68318b8e SV |
9205 | } |
9206 | ||
9207 | struct cgroup_subsys cpu_cgroup_subsys = { | |
38605cae IM |
9208 | .name = "cpu", |
9209 | .create = cpu_cgroup_create, | |
9210 | .destroy = cpu_cgroup_destroy, | |
9211 | .can_attach = cpu_cgroup_can_attach, | |
9212 | .attach = cpu_cgroup_attach, | |
9213 | .populate = cpu_cgroup_populate, | |
9214 | .subsys_id = cpu_cgroup_subsys_id, | |
68318b8e SV |
9215 | .early_init = 1, |
9216 | }; | |
9217 | ||
052f1dc7 | 9218 | #endif /* CONFIG_CGROUP_SCHED */ |
d842de87 SV |
9219 | |
9220 | #ifdef CONFIG_CGROUP_CPUACCT | |
9221 | ||
9222 | /* | |
9223 | * CPU accounting code for task groups. | |
9224 | * | |
9225 | * Based on the work by Paul Menage (menage@google.com) and Balbir Singh | |
9226 | * (balbir@in.ibm.com). | |
9227 | */ | |
9228 | ||
9229 | /* track cpu usage of a group of tasks */ | |
9230 | struct cpuacct { | |
9231 | struct cgroup_subsys_state css; | |
9232 | /* cpuusage holds pointer to a u64-type object on every cpu */ | |
9233 | u64 *cpuusage; | |
9234 | }; | |
9235 | ||
9236 | struct cgroup_subsys cpuacct_subsys; | |
9237 | ||
9238 | /* return cpu accounting group corresponding to this container */ | |
32cd756a | 9239 | static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp) |
d842de87 | 9240 | { |
32cd756a | 9241 | return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id), |
d842de87 SV |
9242 | struct cpuacct, css); |
9243 | } | |
9244 | ||
9245 | /* return cpu accounting group to which this task belongs */ | |
9246 | static inline struct cpuacct *task_ca(struct task_struct *tsk) | |
9247 | { | |
9248 | return container_of(task_subsys_state(tsk, cpuacct_subsys_id), | |
9249 | struct cpuacct, css); | |
9250 | } | |
9251 | ||
9252 | /* create a new cpu accounting group */ | |
9253 | static struct cgroup_subsys_state *cpuacct_create( | |
32cd756a | 9254 | struct cgroup_subsys *ss, struct cgroup *cgrp) |
d842de87 SV |
9255 | { |
9256 | struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL); | |
9257 | ||
9258 | if (!ca) | |
9259 | return ERR_PTR(-ENOMEM); | |
9260 | ||
9261 | ca->cpuusage = alloc_percpu(u64); | |
9262 | if (!ca->cpuusage) { | |
9263 | kfree(ca); | |
9264 | return ERR_PTR(-ENOMEM); | |
9265 | } | |
9266 | ||
9267 | return &ca->css; | |
9268 | } | |
9269 | ||
9270 | /* destroy an existing cpu accounting group */ | |
41a2d6cf | 9271 | static void |
32cd756a | 9272 | cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp) |
d842de87 | 9273 | { |
32cd756a | 9274 | struct cpuacct *ca = cgroup_ca(cgrp); |
d842de87 SV |
9275 | |
9276 | free_percpu(ca->cpuusage); | |
9277 | kfree(ca); | |
9278 | } | |
9279 | ||
9280 | /* return total cpu usage (in nanoseconds) of a group */ | |
32cd756a | 9281 | static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft) |
d842de87 | 9282 | { |
32cd756a | 9283 | struct cpuacct *ca = cgroup_ca(cgrp); |
d842de87 SV |
9284 | u64 totalcpuusage = 0; |
9285 | int i; | |
9286 | ||
9287 | for_each_possible_cpu(i) { | |
9288 | u64 *cpuusage = percpu_ptr(ca->cpuusage, i); | |
9289 | ||
9290 | /* | |
9291 | * Take rq->lock to make 64-bit addition safe on 32-bit | |
9292 | * platforms. | |
9293 | */ | |
9294 | spin_lock_irq(&cpu_rq(i)->lock); | |
9295 | totalcpuusage += *cpuusage; | |
9296 | spin_unlock_irq(&cpu_rq(i)->lock); | |
9297 | } | |
9298 | ||
9299 | return totalcpuusage; | |
9300 | } | |
9301 | ||
0297b803 DG |
9302 | static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype, |
9303 | u64 reset) | |
9304 | { | |
9305 | struct cpuacct *ca = cgroup_ca(cgrp); | |
9306 | int err = 0; | |
9307 | int i; | |
9308 | ||
9309 | if (reset) { | |
9310 | err = -EINVAL; | |
9311 | goto out; | |
9312 | } | |
9313 | ||
9314 | for_each_possible_cpu(i) { | |
9315 | u64 *cpuusage = percpu_ptr(ca->cpuusage, i); | |
9316 | ||
9317 | spin_lock_irq(&cpu_rq(i)->lock); | |
9318 | *cpuusage = 0; | |
9319 | spin_unlock_irq(&cpu_rq(i)->lock); | |
9320 | } | |
9321 | out: | |
9322 | return err; | |
9323 | } | |
9324 | ||
d842de87 SV |
9325 | static struct cftype files[] = { |
9326 | { | |
9327 | .name = "usage", | |
9328 | .read_uint = cpuusage_read, | |
0297b803 | 9329 | .write_uint = cpuusage_write, |
d842de87 SV |
9330 | }, |
9331 | }; | |
9332 | ||
32cd756a | 9333 | static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp) |
d842de87 | 9334 | { |
32cd756a | 9335 | return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files)); |
d842de87 SV |
9336 | } |
9337 | ||
9338 | /* | |
9339 | * charge this task's execution time to its accounting group. | |
9340 | * | |
9341 | * called with rq->lock held. | |
9342 | */ | |
9343 | static void cpuacct_charge(struct task_struct *tsk, u64 cputime) | |
9344 | { | |
9345 | struct cpuacct *ca; | |
9346 | ||
9347 | if (!cpuacct_subsys.active) | |
9348 | return; | |
9349 | ||
9350 | ca = task_ca(tsk); | |
9351 | if (ca) { | |
9352 | u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk)); | |
9353 | ||
9354 | *cpuusage += cputime; | |
9355 | } | |
9356 | } | |
9357 | ||
9358 | struct cgroup_subsys cpuacct_subsys = { | |
9359 | .name = "cpuacct", | |
9360 | .create = cpuacct_create, | |
9361 | .destroy = cpuacct_destroy, | |
9362 | .populate = cpuacct_populate, | |
9363 | .subsys_id = cpuacct_subsys_id, | |
9364 | }; | |
9365 | #endif /* CONFIG_CGROUP_CPUACCT */ |