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