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