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