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