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