Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
391e43da | 2 | * kernel/sched/core.c |
1da177e4 LT |
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 | 34 | #include <linux/highmem.h> |
1da177e4 LT |
35 | #include <asm/mmu_context.h> |
36 | #include <linux/interrupt.h> | |
c59ede7b | 37 | #include <linux/capability.h> |
1da177e4 LT |
38 | #include <linux/completion.h> |
39 | #include <linux/kernel_stat.h> | |
9a11b49a | 40 | #include <linux/debug_locks.h> |
cdd6c482 | 41 | #include <linux/perf_event.h> |
1da177e4 LT |
42 | #include <linux/security.h> |
43 | #include <linux/notifier.h> | |
44 | #include <linux/profile.h> | |
7dfb7103 | 45 | #include <linux/freezer.h> |
198e2f18 | 46 | #include <linux/vmalloc.h> |
1da177e4 LT |
47 | #include <linux/blkdev.h> |
48 | #include <linux/delay.h> | |
b488893a | 49 | #include <linux/pid_namespace.h> |
1da177e4 LT |
50 | #include <linux/smp.h> |
51 | #include <linux/threads.h> | |
52 | #include <linux/timer.h> | |
53 | #include <linux/rcupdate.h> | |
54 | #include <linux/cpu.h> | |
55 | #include <linux/cpuset.h> | |
56 | #include <linux/percpu.h> | |
b5aadf7f | 57 | #include <linux/proc_fs.h> |
1da177e4 | 58 | #include <linux/seq_file.h> |
e692ab53 | 59 | #include <linux/sysctl.h> |
1da177e4 LT |
60 | #include <linux/syscalls.h> |
61 | #include <linux/times.h> | |
8f0ab514 | 62 | #include <linux/tsacct_kern.h> |
c6fd91f0 | 63 | #include <linux/kprobes.h> |
0ff92245 | 64 | #include <linux/delayacct.h> |
dff06c15 | 65 | #include <linux/unistd.h> |
f5ff8422 | 66 | #include <linux/pagemap.h> |
8f4d37ec | 67 | #include <linux/hrtimer.h> |
30914a58 | 68 | #include <linux/tick.h> |
f00b45c1 PZ |
69 | #include <linux/debugfs.h> |
70 | #include <linux/ctype.h> | |
6cd8a4bb | 71 | #include <linux/ftrace.h> |
5a0e3ad6 | 72 | #include <linux/slab.h> |
f1c6f1a7 | 73 | #include <linux/init_task.h> |
40401530 | 74 | #include <linux/binfmts.h> |
91d1aa43 | 75 | #include <linux/context_tracking.h> |
52f5684c | 76 | #include <linux/compiler.h> |
1da177e4 | 77 | |
96f951ed | 78 | #include <asm/switch_to.h> |
5517d86b | 79 | #include <asm/tlb.h> |
838225b4 | 80 | #include <asm/irq_regs.h> |
db7e527d | 81 | #include <asm/mutex.h> |
e6e6685a GC |
82 | #ifdef CONFIG_PARAVIRT |
83 | #include <asm/paravirt.h> | |
84 | #endif | |
1da177e4 | 85 | |
029632fb | 86 | #include "sched.h" |
ea138446 | 87 | #include "../workqueue_internal.h" |
29d5e047 | 88 | #include "../smpboot.h" |
6e0534f2 | 89 | |
a8d154b0 | 90 | #define CREATE_TRACE_POINTS |
ad8d75ff | 91 | #include <trace/events/sched.h> |
a8d154b0 | 92 | |
029632fb PZ |
93 | DEFINE_MUTEX(sched_domains_mutex); |
94 | DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); | |
dc61b1d6 | 95 | |
fe44d621 | 96 | static void update_rq_clock_task(struct rq *rq, s64 delta); |
305e6835 | 97 | |
029632fb | 98 | void update_rq_clock(struct rq *rq) |
3e51f33f | 99 | { |
fe44d621 | 100 | s64 delta; |
305e6835 | 101 | |
9edfbfed PZ |
102 | lockdep_assert_held(&rq->lock); |
103 | ||
104 | if (rq->clock_skip_update & RQCF_ACT_SKIP) | |
f26f9aff | 105 | return; |
aa483808 | 106 | |
fe44d621 | 107 | delta = sched_clock_cpu(cpu_of(rq)) - rq->clock; |
4036ac15 MG |
108 | if (delta < 0) |
109 | return; | |
fe44d621 PZ |
110 | rq->clock += delta; |
111 | update_rq_clock_task(rq, delta); | |
3e51f33f PZ |
112 | } |
113 | ||
bf5c91ba IM |
114 | /* |
115 | * Debugging: various feature bits | |
116 | */ | |
f00b45c1 | 117 | |
f00b45c1 PZ |
118 | #define SCHED_FEAT(name, enabled) \ |
119 | (1UL << __SCHED_FEAT_##name) * enabled | | |
120 | ||
bf5c91ba | 121 | const_debug unsigned int sysctl_sched_features = |
391e43da | 122 | #include "features.h" |
f00b45c1 PZ |
123 | 0; |
124 | ||
125 | #undef SCHED_FEAT | |
126 | ||
127 | #ifdef CONFIG_SCHED_DEBUG | |
128 | #define SCHED_FEAT(name, enabled) \ | |
129 | #name , | |
130 | ||
1292531f | 131 | static const char * const sched_feat_names[] = { |
391e43da | 132 | #include "features.h" |
f00b45c1 PZ |
133 | }; |
134 | ||
135 | #undef SCHED_FEAT | |
136 | ||
34f3a814 | 137 | static int sched_feat_show(struct seq_file *m, void *v) |
f00b45c1 | 138 | { |
f00b45c1 PZ |
139 | int i; |
140 | ||
f8b6d1cc | 141 | for (i = 0; i < __SCHED_FEAT_NR; i++) { |
34f3a814 LZ |
142 | if (!(sysctl_sched_features & (1UL << i))) |
143 | seq_puts(m, "NO_"); | |
144 | seq_printf(m, "%s ", sched_feat_names[i]); | |
f00b45c1 | 145 | } |
34f3a814 | 146 | seq_puts(m, "\n"); |
f00b45c1 | 147 | |
34f3a814 | 148 | return 0; |
f00b45c1 PZ |
149 | } |
150 | ||
f8b6d1cc PZ |
151 | #ifdef HAVE_JUMP_LABEL |
152 | ||
c5905afb IM |
153 | #define jump_label_key__true STATIC_KEY_INIT_TRUE |
154 | #define jump_label_key__false STATIC_KEY_INIT_FALSE | |
f8b6d1cc PZ |
155 | |
156 | #define SCHED_FEAT(name, enabled) \ | |
157 | jump_label_key__##enabled , | |
158 | ||
c5905afb | 159 | struct static_key sched_feat_keys[__SCHED_FEAT_NR] = { |
f8b6d1cc PZ |
160 | #include "features.h" |
161 | }; | |
162 | ||
163 | #undef SCHED_FEAT | |
164 | ||
165 | static void sched_feat_disable(int i) | |
166 | { | |
c5905afb IM |
167 | if (static_key_enabled(&sched_feat_keys[i])) |
168 | static_key_slow_dec(&sched_feat_keys[i]); | |
f8b6d1cc PZ |
169 | } |
170 | ||
171 | static void sched_feat_enable(int i) | |
172 | { | |
c5905afb IM |
173 | if (!static_key_enabled(&sched_feat_keys[i])) |
174 | static_key_slow_inc(&sched_feat_keys[i]); | |
f8b6d1cc PZ |
175 | } |
176 | #else | |
177 | static void sched_feat_disable(int i) { }; | |
178 | static void sched_feat_enable(int i) { }; | |
179 | #endif /* HAVE_JUMP_LABEL */ | |
180 | ||
1a687c2e | 181 | static int sched_feat_set(char *cmp) |
f00b45c1 | 182 | { |
f00b45c1 | 183 | int i; |
1a687c2e | 184 | int neg = 0; |
f00b45c1 | 185 | |
524429c3 | 186 | if (strncmp(cmp, "NO_", 3) == 0) { |
f00b45c1 PZ |
187 | neg = 1; |
188 | cmp += 3; | |
189 | } | |
190 | ||
f8b6d1cc | 191 | for (i = 0; i < __SCHED_FEAT_NR; i++) { |
7740191c | 192 | if (strcmp(cmp, sched_feat_names[i]) == 0) { |
f8b6d1cc | 193 | if (neg) { |
f00b45c1 | 194 | sysctl_sched_features &= ~(1UL << i); |
f8b6d1cc PZ |
195 | sched_feat_disable(i); |
196 | } else { | |
f00b45c1 | 197 | sysctl_sched_features |= (1UL << i); |
f8b6d1cc PZ |
198 | sched_feat_enable(i); |
199 | } | |
f00b45c1 PZ |
200 | break; |
201 | } | |
202 | } | |
203 | ||
1a687c2e MG |
204 | return i; |
205 | } | |
206 | ||
207 | static ssize_t | |
208 | sched_feat_write(struct file *filp, const char __user *ubuf, | |
209 | size_t cnt, loff_t *ppos) | |
210 | { | |
211 | char buf[64]; | |
212 | char *cmp; | |
213 | int i; | |
5cd08fbf | 214 | struct inode *inode; |
1a687c2e MG |
215 | |
216 | if (cnt > 63) | |
217 | cnt = 63; | |
218 | ||
219 | if (copy_from_user(&buf, ubuf, cnt)) | |
220 | return -EFAULT; | |
221 | ||
222 | buf[cnt] = 0; | |
223 | cmp = strstrip(buf); | |
224 | ||
5cd08fbf JB |
225 | /* Ensure the static_key remains in a consistent state */ |
226 | inode = file_inode(filp); | |
227 | mutex_lock(&inode->i_mutex); | |
1a687c2e | 228 | i = sched_feat_set(cmp); |
5cd08fbf | 229 | mutex_unlock(&inode->i_mutex); |
f8b6d1cc | 230 | if (i == __SCHED_FEAT_NR) |
f00b45c1 PZ |
231 | return -EINVAL; |
232 | ||
42994724 | 233 | *ppos += cnt; |
f00b45c1 PZ |
234 | |
235 | return cnt; | |
236 | } | |
237 | ||
34f3a814 LZ |
238 | static int sched_feat_open(struct inode *inode, struct file *filp) |
239 | { | |
240 | return single_open(filp, sched_feat_show, NULL); | |
241 | } | |
242 | ||
828c0950 | 243 | static const struct file_operations sched_feat_fops = { |
34f3a814 LZ |
244 | .open = sched_feat_open, |
245 | .write = sched_feat_write, | |
246 | .read = seq_read, | |
247 | .llseek = seq_lseek, | |
248 | .release = single_release, | |
f00b45c1 PZ |
249 | }; |
250 | ||
251 | static __init int sched_init_debug(void) | |
252 | { | |
f00b45c1 PZ |
253 | debugfs_create_file("sched_features", 0644, NULL, NULL, |
254 | &sched_feat_fops); | |
255 | ||
256 | return 0; | |
257 | } | |
258 | late_initcall(sched_init_debug); | |
f8b6d1cc | 259 | #endif /* CONFIG_SCHED_DEBUG */ |
bf5c91ba | 260 | |
b82d9fdd PZ |
261 | /* |
262 | * Number of tasks to iterate in a single balance run. | |
263 | * Limited because this is done with IRQs disabled. | |
264 | */ | |
265 | const_debug unsigned int sysctl_sched_nr_migrate = 32; | |
266 | ||
e9e9250b PZ |
267 | /* |
268 | * period over which we average the RT time consumption, measured | |
269 | * in ms. | |
270 | * | |
271 | * default: 1s | |
272 | */ | |
273 | const_debug unsigned int sysctl_sched_time_avg = MSEC_PER_SEC; | |
274 | ||
fa85ae24 | 275 | /* |
9f0c1e56 | 276 | * period over which we measure -rt task cpu usage in us. |
fa85ae24 PZ |
277 | * default: 1s |
278 | */ | |
9f0c1e56 | 279 | unsigned int sysctl_sched_rt_period = 1000000; |
fa85ae24 | 280 | |
029632fb | 281 | __read_mostly int scheduler_running; |
6892b75e | 282 | |
9f0c1e56 PZ |
283 | /* |
284 | * part of the period that we allow rt tasks to run in us. | |
285 | * default: 0.95s | |
286 | */ | |
287 | int sysctl_sched_rt_runtime = 950000; | |
fa85ae24 | 288 | |
3fa0818b RR |
289 | /* cpus with isolated domains */ |
290 | cpumask_var_t cpu_isolated_map; | |
291 | ||
1da177e4 | 292 | /* |
cc2a73b5 | 293 | * this_rq_lock - lock this runqueue and disable interrupts. |
1da177e4 | 294 | */ |
a9957449 | 295 | static struct rq *this_rq_lock(void) |
1da177e4 LT |
296 | __acquires(rq->lock) |
297 | { | |
70b97a7f | 298 | struct rq *rq; |
1da177e4 LT |
299 | |
300 | local_irq_disable(); | |
301 | rq = this_rq(); | |
05fa785c | 302 | raw_spin_lock(&rq->lock); |
1da177e4 LT |
303 | |
304 | return rq; | |
305 | } | |
306 | ||
8f4d37ec PZ |
307 | #ifdef CONFIG_SCHED_HRTICK |
308 | /* | |
309 | * Use HR-timers to deliver accurate preemption points. | |
8f4d37ec | 310 | */ |
8f4d37ec | 311 | |
8f4d37ec PZ |
312 | static void hrtick_clear(struct rq *rq) |
313 | { | |
314 | if (hrtimer_active(&rq->hrtick_timer)) | |
315 | hrtimer_cancel(&rq->hrtick_timer); | |
316 | } | |
317 | ||
8f4d37ec PZ |
318 | /* |
319 | * High-resolution timer tick. | |
320 | * Runs from hardirq context with interrupts disabled. | |
321 | */ | |
322 | static enum hrtimer_restart hrtick(struct hrtimer *timer) | |
323 | { | |
324 | struct rq *rq = container_of(timer, struct rq, hrtick_timer); | |
325 | ||
326 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); | |
327 | ||
05fa785c | 328 | raw_spin_lock(&rq->lock); |
3e51f33f | 329 | update_rq_clock(rq); |
8f4d37ec | 330 | rq->curr->sched_class->task_tick(rq, rq->curr, 1); |
05fa785c | 331 | raw_spin_unlock(&rq->lock); |
8f4d37ec PZ |
332 | |
333 | return HRTIMER_NORESTART; | |
334 | } | |
335 | ||
95e904c7 | 336 | #ifdef CONFIG_SMP |
971ee28c | 337 | |
4961b6e1 | 338 | static void __hrtick_restart(struct rq *rq) |
971ee28c PZ |
339 | { |
340 | struct hrtimer *timer = &rq->hrtick_timer; | |
971ee28c | 341 | |
4961b6e1 | 342 | hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED); |
971ee28c PZ |
343 | } |
344 | ||
31656519 PZ |
345 | /* |
346 | * called from hardirq (IPI) context | |
347 | */ | |
348 | static void __hrtick_start(void *arg) | |
b328ca18 | 349 | { |
31656519 | 350 | struct rq *rq = arg; |
b328ca18 | 351 | |
05fa785c | 352 | raw_spin_lock(&rq->lock); |
971ee28c | 353 | __hrtick_restart(rq); |
31656519 | 354 | rq->hrtick_csd_pending = 0; |
05fa785c | 355 | raw_spin_unlock(&rq->lock); |
b328ca18 PZ |
356 | } |
357 | ||
31656519 PZ |
358 | /* |
359 | * Called to set the hrtick timer state. | |
360 | * | |
361 | * called with rq->lock held and irqs disabled | |
362 | */ | |
029632fb | 363 | void hrtick_start(struct rq *rq, u64 delay) |
b328ca18 | 364 | { |
31656519 | 365 | struct hrtimer *timer = &rq->hrtick_timer; |
177ef2a6 | 366 | ktime_t time; |
367 | s64 delta; | |
368 | ||
369 | /* | |
370 | * Don't schedule slices shorter than 10000ns, that just | |
371 | * doesn't make sense and can cause timer DoS. | |
372 | */ | |
373 | delta = max_t(s64, delay, 10000LL); | |
374 | time = ktime_add_ns(timer->base->get_time(), delta); | |
b328ca18 | 375 | |
cc584b21 | 376 | hrtimer_set_expires(timer, time); |
31656519 PZ |
377 | |
378 | if (rq == this_rq()) { | |
971ee28c | 379 | __hrtick_restart(rq); |
31656519 | 380 | } else if (!rq->hrtick_csd_pending) { |
c46fff2a | 381 | smp_call_function_single_async(cpu_of(rq), &rq->hrtick_csd); |
31656519 PZ |
382 | rq->hrtick_csd_pending = 1; |
383 | } | |
b328ca18 PZ |
384 | } |
385 | ||
386 | static int | |
387 | hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu) | |
388 | { | |
389 | int cpu = (int)(long)hcpu; | |
390 | ||
391 | switch (action) { | |
392 | case CPU_UP_CANCELED: | |
393 | case CPU_UP_CANCELED_FROZEN: | |
394 | case CPU_DOWN_PREPARE: | |
395 | case CPU_DOWN_PREPARE_FROZEN: | |
396 | case CPU_DEAD: | |
397 | case CPU_DEAD_FROZEN: | |
31656519 | 398 | hrtick_clear(cpu_rq(cpu)); |
b328ca18 PZ |
399 | return NOTIFY_OK; |
400 | } | |
401 | ||
402 | return NOTIFY_DONE; | |
403 | } | |
404 | ||
fa748203 | 405 | static __init void init_hrtick(void) |
b328ca18 PZ |
406 | { |
407 | hotcpu_notifier(hotplug_hrtick, 0); | |
408 | } | |
31656519 PZ |
409 | #else |
410 | /* | |
411 | * Called to set the hrtick timer state. | |
412 | * | |
413 | * called with rq->lock held and irqs disabled | |
414 | */ | |
029632fb | 415 | void hrtick_start(struct rq *rq, u64 delay) |
31656519 | 416 | { |
86893335 WL |
417 | /* |
418 | * Don't schedule slices shorter than 10000ns, that just | |
419 | * doesn't make sense. Rely on vruntime for fairness. | |
420 | */ | |
421 | delay = max_t(u64, delay, 10000LL); | |
4961b6e1 TG |
422 | hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay), |
423 | HRTIMER_MODE_REL_PINNED); | |
31656519 | 424 | } |
b328ca18 | 425 | |
006c75f1 | 426 | static inline void init_hrtick(void) |
8f4d37ec | 427 | { |
8f4d37ec | 428 | } |
31656519 | 429 | #endif /* CONFIG_SMP */ |
8f4d37ec | 430 | |
31656519 | 431 | static void init_rq_hrtick(struct rq *rq) |
8f4d37ec | 432 | { |
31656519 PZ |
433 | #ifdef CONFIG_SMP |
434 | rq->hrtick_csd_pending = 0; | |
8f4d37ec | 435 | |
31656519 PZ |
436 | rq->hrtick_csd.flags = 0; |
437 | rq->hrtick_csd.func = __hrtick_start; | |
438 | rq->hrtick_csd.info = rq; | |
439 | #endif | |
8f4d37ec | 440 | |
31656519 PZ |
441 | hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
442 | rq->hrtick_timer.function = hrtick; | |
8f4d37ec | 443 | } |
006c75f1 | 444 | #else /* CONFIG_SCHED_HRTICK */ |
8f4d37ec PZ |
445 | static inline void hrtick_clear(struct rq *rq) |
446 | { | |
447 | } | |
448 | ||
8f4d37ec PZ |
449 | static inline void init_rq_hrtick(struct rq *rq) |
450 | { | |
451 | } | |
452 | ||
b328ca18 PZ |
453 | static inline void init_hrtick(void) |
454 | { | |
455 | } | |
006c75f1 | 456 | #endif /* CONFIG_SCHED_HRTICK */ |
8f4d37ec | 457 | |
fd99f91a PZ |
458 | /* |
459 | * cmpxchg based fetch_or, macro so it works for different integer types | |
460 | */ | |
461 | #define fetch_or(ptr, val) \ | |
462 | ({ typeof(*(ptr)) __old, __val = *(ptr); \ | |
463 | for (;;) { \ | |
464 | __old = cmpxchg((ptr), __val, __val | (val)); \ | |
465 | if (__old == __val) \ | |
466 | break; \ | |
467 | __val = __old; \ | |
468 | } \ | |
469 | __old; \ | |
470 | }) | |
471 | ||
e3baac47 | 472 | #if defined(CONFIG_SMP) && defined(TIF_POLLING_NRFLAG) |
fd99f91a PZ |
473 | /* |
474 | * Atomically set TIF_NEED_RESCHED and test for TIF_POLLING_NRFLAG, | |
475 | * this avoids any races wrt polling state changes and thereby avoids | |
476 | * spurious IPIs. | |
477 | */ | |
478 | static bool set_nr_and_not_polling(struct task_struct *p) | |
479 | { | |
480 | struct thread_info *ti = task_thread_info(p); | |
481 | return !(fetch_or(&ti->flags, _TIF_NEED_RESCHED) & _TIF_POLLING_NRFLAG); | |
482 | } | |
e3baac47 PZ |
483 | |
484 | /* | |
485 | * Atomically set TIF_NEED_RESCHED if TIF_POLLING_NRFLAG is set. | |
486 | * | |
487 | * If this returns true, then the idle task promises to call | |
488 | * sched_ttwu_pending() and reschedule soon. | |
489 | */ | |
490 | static bool set_nr_if_polling(struct task_struct *p) | |
491 | { | |
492 | struct thread_info *ti = task_thread_info(p); | |
316c1608 | 493 | typeof(ti->flags) old, val = READ_ONCE(ti->flags); |
e3baac47 PZ |
494 | |
495 | for (;;) { | |
496 | if (!(val & _TIF_POLLING_NRFLAG)) | |
497 | return false; | |
498 | if (val & _TIF_NEED_RESCHED) | |
499 | return true; | |
500 | old = cmpxchg(&ti->flags, val, val | _TIF_NEED_RESCHED); | |
501 | if (old == val) | |
502 | break; | |
503 | val = old; | |
504 | } | |
505 | return true; | |
506 | } | |
507 | ||
fd99f91a PZ |
508 | #else |
509 | static bool set_nr_and_not_polling(struct task_struct *p) | |
510 | { | |
511 | set_tsk_need_resched(p); | |
512 | return true; | |
513 | } | |
e3baac47 PZ |
514 | |
515 | #ifdef CONFIG_SMP | |
516 | static bool set_nr_if_polling(struct task_struct *p) | |
517 | { | |
518 | return false; | |
519 | } | |
520 | #endif | |
fd99f91a PZ |
521 | #endif |
522 | ||
76751049 PZ |
523 | void wake_q_add(struct wake_q_head *head, struct task_struct *task) |
524 | { | |
525 | struct wake_q_node *node = &task->wake_q; | |
526 | ||
527 | /* | |
528 | * Atomically grab the task, if ->wake_q is !nil already it means | |
529 | * its already queued (either by us or someone else) and will get the | |
530 | * wakeup due to that. | |
531 | * | |
532 | * This cmpxchg() implies a full barrier, which pairs with the write | |
533 | * barrier implied by the wakeup in wake_up_list(). | |
534 | */ | |
535 | if (cmpxchg(&node->next, NULL, WAKE_Q_TAIL)) | |
536 | return; | |
537 | ||
538 | get_task_struct(task); | |
539 | ||
540 | /* | |
541 | * The head is context local, there can be no concurrency. | |
542 | */ | |
543 | *head->lastp = node; | |
544 | head->lastp = &node->next; | |
545 | } | |
546 | ||
547 | void wake_up_q(struct wake_q_head *head) | |
548 | { | |
549 | struct wake_q_node *node = head->first; | |
550 | ||
551 | while (node != WAKE_Q_TAIL) { | |
552 | struct task_struct *task; | |
553 | ||
554 | task = container_of(node, struct task_struct, wake_q); | |
555 | BUG_ON(!task); | |
556 | /* task can safely be re-inserted now */ | |
557 | node = node->next; | |
558 | task->wake_q.next = NULL; | |
559 | ||
560 | /* | |
561 | * wake_up_process() implies a wmb() to pair with the queueing | |
562 | * in wake_q_add() so as not to miss wakeups. | |
563 | */ | |
564 | wake_up_process(task); | |
565 | put_task_struct(task); | |
566 | } | |
567 | } | |
568 | ||
c24d20db | 569 | /* |
8875125e | 570 | * resched_curr - mark rq's current task 'to be rescheduled now'. |
c24d20db IM |
571 | * |
572 | * On UP this means the setting of the need_resched flag, on SMP it | |
573 | * might also involve a cross-CPU call to trigger the scheduler on | |
574 | * the target CPU. | |
575 | */ | |
8875125e | 576 | void resched_curr(struct rq *rq) |
c24d20db | 577 | { |
8875125e | 578 | struct task_struct *curr = rq->curr; |
c24d20db IM |
579 | int cpu; |
580 | ||
8875125e | 581 | lockdep_assert_held(&rq->lock); |
c24d20db | 582 | |
8875125e | 583 | if (test_tsk_need_resched(curr)) |
c24d20db IM |
584 | return; |
585 | ||
8875125e | 586 | cpu = cpu_of(rq); |
fd99f91a | 587 | |
f27dde8d | 588 | if (cpu == smp_processor_id()) { |
8875125e | 589 | set_tsk_need_resched(curr); |
f27dde8d | 590 | set_preempt_need_resched(); |
c24d20db | 591 | return; |
f27dde8d | 592 | } |
c24d20db | 593 | |
8875125e | 594 | if (set_nr_and_not_polling(curr)) |
c24d20db | 595 | smp_send_reschedule(cpu); |
dfc68f29 AL |
596 | else |
597 | trace_sched_wake_idle_without_ipi(cpu); | |
c24d20db IM |
598 | } |
599 | ||
029632fb | 600 | void resched_cpu(int cpu) |
c24d20db IM |
601 | { |
602 | struct rq *rq = cpu_rq(cpu); | |
603 | unsigned long flags; | |
604 | ||
05fa785c | 605 | if (!raw_spin_trylock_irqsave(&rq->lock, flags)) |
c24d20db | 606 | return; |
8875125e | 607 | resched_curr(rq); |
05fa785c | 608 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
c24d20db | 609 | } |
06d8308c | 610 | |
b021fe3e | 611 | #ifdef CONFIG_SMP |
3451d024 | 612 | #ifdef CONFIG_NO_HZ_COMMON |
83cd4fe2 VP |
613 | /* |
614 | * In the semi idle case, use the nearest busy cpu for migrating timers | |
615 | * from an idle cpu. This is good for power-savings. | |
616 | * | |
617 | * We don't do similar optimization for completely idle system, as | |
618 | * selecting an idle cpu will add more delays to the timers than intended | |
619 | * (as that cpu's timer base may not be uptodate wrt jiffies etc). | |
620 | */ | |
6201b4d6 | 621 | int get_nohz_timer_target(int pinned) |
83cd4fe2 VP |
622 | { |
623 | int cpu = smp_processor_id(); | |
624 | int i; | |
625 | struct sched_domain *sd; | |
626 | ||
6201b4d6 VK |
627 | if (pinned || !get_sysctl_timer_migration() || !idle_cpu(cpu)) |
628 | return cpu; | |
629 | ||
057f3fad | 630 | rcu_read_lock(); |
83cd4fe2 | 631 | for_each_domain(cpu, sd) { |
057f3fad PZ |
632 | for_each_cpu(i, sched_domain_span(sd)) { |
633 | if (!idle_cpu(i)) { | |
634 | cpu = i; | |
635 | goto unlock; | |
636 | } | |
637 | } | |
83cd4fe2 | 638 | } |
057f3fad PZ |
639 | unlock: |
640 | rcu_read_unlock(); | |
83cd4fe2 VP |
641 | return cpu; |
642 | } | |
06d8308c TG |
643 | /* |
644 | * When add_timer_on() enqueues a timer into the timer wheel of an | |
645 | * idle CPU then this timer might expire before the next timer event | |
646 | * which is scheduled to wake up that CPU. In case of a completely | |
647 | * idle system the next event might even be infinite time into the | |
648 | * future. wake_up_idle_cpu() ensures that the CPU is woken up and | |
649 | * leaves the inner idle loop so the newly added timer is taken into | |
650 | * account when the CPU goes back to idle and evaluates the timer | |
651 | * wheel for the next timer event. | |
652 | */ | |
1c20091e | 653 | static void wake_up_idle_cpu(int cpu) |
06d8308c TG |
654 | { |
655 | struct rq *rq = cpu_rq(cpu); | |
656 | ||
657 | if (cpu == smp_processor_id()) | |
658 | return; | |
659 | ||
67b9ca70 | 660 | if (set_nr_and_not_polling(rq->idle)) |
06d8308c | 661 | smp_send_reschedule(cpu); |
dfc68f29 AL |
662 | else |
663 | trace_sched_wake_idle_without_ipi(cpu); | |
45bf76df IM |
664 | } |
665 | ||
c5bfece2 | 666 | static bool wake_up_full_nohz_cpu(int cpu) |
1c20091e | 667 | { |
53c5fa16 FW |
668 | /* |
669 | * We just need the target to call irq_exit() and re-evaluate | |
670 | * the next tick. The nohz full kick at least implies that. | |
671 | * If needed we can still optimize that later with an | |
672 | * empty IRQ. | |
673 | */ | |
c5bfece2 | 674 | if (tick_nohz_full_cpu(cpu)) { |
1c20091e FW |
675 | if (cpu != smp_processor_id() || |
676 | tick_nohz_tick_stopped()) | |
53c5fa16 | 677 | tick_nohz_full_kick_cpu(cpu); |
1c20091e FW |
678 | return true; |
679 | } | |
680 | ||
681 | return false; | |
682 | } | |
683 | ||
684 | void wake_up_nohz_cpu(int cpu) | |
685 | { | |
c5bfece2 | 686 | if (!wake_up_full_nohz_cpu(cpu)) |
1c20091e FW |
687 | wake_up_idle_cpu(cpu); |
688 | } | |
689 | ||
ca38062e | 690 | static inline bool got_nohz_idle_kick(void) |
45bf76df | 691 | { |
1c792db7 | 692 | int cpu = smp_processor_id(); |
873b4c65 VG |
693 | |
694 | if (!test_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu))) | |
695 | return false; | |
696 | ||
697 | if (idle_cpu(cpu) && !need_resched()) | |
698 | return true; | |
699 | ||
700 | /* | |
701 | * We can't run Idle Load Balance on this CPU for this time so we | |
702 | * cancel it and clear NOHZ_BALANCE_KICK | |
703 | */ | |
704 | clear_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu)); | |
705 | return false; | |
45bf76df IM |
706 | } |
707 | ||
3451d024 | 708 | #else /* CONFIG_NO_HZ_COMMON */ |
45bf76df | 709 | |
ca38062e | 710 | static inline bool got_nohz_idle_kick(void) |
2069dd75 | 711 | { |
ca38062e | 712 | return false; |
2069dd75 PZ |
713 | } |
714 | ||
3451d024 | 715 | #endif /* CONFIG_NO_HZ_COMMON */ |
d842de87 | 716 | |
ce831b38 FW |
717 | #ifdef CONFIG_NO_HZ_FULL |
718 | bool sched_can_stop_tick(void) | |
719 | { | |
1e78cdbd RR |
720 | /* |
721 | * FIFO realtime policy runs the highest priority task. Other runnable | |
722 | * tasks are of a lower priority. The scheduler tick does nothing. | |
723 | */ | |
724 | if (current->policy == SCHED_FIFO) | |
725 | return true; | |
726 | ||
727 | /* | |
728 | * Round-robin realtime tasks time slice with other tasks at the same | |
729 | * realtime priority. Is this task the only one at this priority? | |
730 | */ | |
731 | if (current->policy == SCHED_RR) { | |
732 | struct sched_rt_entity *rt_se = ¤t->rt; | |
733 | ||
734 | return rt_se->run_list.prev == rt_se->run_list.next; | |
735 | } | |
736 | ||
3882ec64 FW |
737 | /* |
738 | * More than one running task need preemption. | |
739 | * nr_running update is assumed to be visible | |
740 | * after IPI is sent from wakers. | |
741 | */ | |
541b8264 VK |
742 | if (this_rq()->nr_running > 1) |
743 | return false; | |
ce831b38 | 744 | |
541b8264 | 745 | return true; |
ce831b38 FW |
746 | } |
747 | #endif /* CONFIG_NO_HZ_FULL */ | |
d842de87 | 748 | |
029632fb | 749 | void sched_avg_update(struct rq *rq) |
18d95a28 | 750 | { |
e9e9250b PZ |
751 | s64 period = sched_avg_period(); |
752 | ||
78becc27 | 753 | while ((s64)(rq_clock(rq) - rq->age_stamp) > period) { |
0d98bb26 WD |
754 | /* |
755 | * Inline assembly required to prevent the compiler | |
756 | * optimising this loop into a divmod call. | |
757 | * See __iter_div_u64_rem() for another example of this. | |
758 | */ | |
759 | asm("" : "+rm" (rq->age_stamp)); | |
e9e9250b PZ |
760 | rq->age_stamp += period; |
761 | rq->rt_avg /= 2; | |
762 | } | |
18d95a28 PZ |
763 | } |
764 | ||
6d6bc0ad | 765 | #endif /* CONFIG_SMP */ |
18d95a28 | 766 | |
a790de99 PT |
767 | #if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \ |
768 | (defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH))) | |
c09595f6 | 769 | /* |
8277434e PT |
770 | * Iterate task_group tree rooted at *from, calling @down when first entering a |
771 | * node and @up when leaving it for the final time. | |
772 | * | |
773 | * Caller must hold rcu_lock or sufficient equivalent. | |
c09595f6 | 774 | */ |
029632fb | 775 | int walk_tg_tree_from(struct task_group *from, |
8277434e | 776 | tg_visitor down, tg_visitor up, void *data) |
c09595f6 PZ |
777 | { |
778 | struct task_group *parent, *child; | |
eb755805 | 779 | int ret; |
c09595f6 | 780 | |
8277434e PT |
781 | parent = from; |
782 | ||
c09595f6 | 783 | down: |
eb755805 PZ |
784 | ret = (*down)(parent, data); |
785 | if (ret) | |
8277434e | 786 | goto out; |
c09595f6 PZ |
787 | list_for_each_entry_rcu(child, &parent->children, siblings) { |
788 | parent = child; | |
789 | goto down; | |
790 | ||
791 | up: | |
792 | continue; | |
793 | } | |
eb755805 | 794 | ret = (*up)(parent, data); |
8277434e PT |
795 | if (ret || parent == from) |
796 | goto out; | |
c09595f6 PZ |
797 | |
798 | child = parent; | |
799 | parent = parent->parent; | |
800 | if (parent) | |
801 | goto up; | |
8277434e | 802 | out: |
eb755805 | 803 | return ret; |
c09595f6 PZ |
804 | } |
805 | ||
029632fb | 806 | int tg_nop(struct task_group *tg, void *data) |
eb755805 | 807 | { |
e2b245f8 | 808 | return 0; |
eb755805 | 809 | } |
18d95a28 PZ |
810 | #endif |
811 | ||
45bf76df IM |
812 | static void set_load_weight(struct task_struct *p) |
813 | { | |
f05998d4 NR |
814 | int prio = p->static_prio - MAX_RT_PRIO; |
815 | struct load_weight *load = &p->se.load; | |
816 | ||
dd41f596 IM |
817 | /* |
818 | * SCHED_IDLE tasks get minimal weight: | |
819 | */ | |
820 | if (p->policy == SCHED_IDLE) { | |
c8b28116 | 821 | load->weight = scale_load(WEIGHT_IDLEPRIO); |
f05998d4 | 822 | load->inv_weight = WMULT_IDLEPRIO; |
dd41f596 IM |
823 | return; |
824 | } | |
71f8bd46 | 825 | |
c8b28116 | 826 | load->weight = scale_load(prio_to_weight[prio]); |
f05998d4 | 827 | load->inv_weight = prio_to_wmult[prio]; |
71f8bd46 IM |
828 | } |
829 | ||
371fd7e7 | 830 | static void enqueue_task(struct rq *rq, struct task_struct *p, int flags) |
2087a1ad | 831 | { |
a64692a3 | 832 | update_rq_clock(rq); |
43148951 | 833 | sched_info_queued(rq, p); |
371fd7e7 | 834 | p->sched_class->enqueue_task(rq, p, flags); |
71f8bd46 IM |
835 | } |
836 | ||
371fd7e7 | 837 | static void dequeue_task(struct rq *rq, struct task_struct *p, int flags) |
71f8bd46 | 838 | { |
a64692a3 | 839 | update_rq_clock(rq); |
43148951 | 840 | sched_info_dequeued(rq, p); |
371fd7e7 | 841 | p->sched_class->dequeue_task(rq, p, flags); |
71f8bd46 IM |
842 | } |
843 | ||
029632fb | 844 | void activate_task(struct rq *rq, struct task_struct *p, int flags) |
1e3c88bd PZ |
845 | { |
846 | if (task_contributes_to_load(p)) | |
847 | rq->nr_uninterruptible--; | |
848 | ||
371fd7e7 | 849 | enqueue_task(rq, p, flags); |
1e3c88bd PZ |
850 | } |
851 | ||
029632fb | 852 | void deactivate_task(struct rq *rq, struct task_struct *p, int flags) |
1e3c88bd PZ |
853 | { |
854 | if (task_contributes_to_load(p)) | |
855 | rq->nr_uninterruptible++; | |
856 | ||
371fd7e7 | 857 | dequeue_task(rq, p, flags); |
1e3c88bd PZ |
858 | } |
859 | ||
fe44d621 | 860 | static void update_rq_clock_task(struct rq *rq, s64 delta) |
aa483808 | 861 | { |
095c0aa8 GC |
862 | /* |
863 | * In theory, the compile should just see 0 here, and optimize out the call | |
864 | * to sched_rt_avg_update. But I don't trust it... | |
865 | */ | |
866 | #if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING) | |
867 | s64 steal = 0, irq_delta = 0; | |
868 | #endif | |
869 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING | |
8e92c201 | 870 | irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time; |
fe44d621 PZ |
871 | |
872 | /* | |
873 | * Since irq_time is only updated on {soft,}irq_exit, we might run into | |
874 | * this case when a previous update_rq_clock() happened inside a | |
875 | * {soft,}irq region. | |
876 | * | |
877 | * When this happens, we stop ->clock_task and only update the | |
878 | * prev_irq_time stamp to account for the part that fit, so that a next | |
879 | * update will consume the rest. This ensures ->clock_task is | |
880 | * monotonic. | |
881 | * | |
882 | * It does however cause some slight miss-attribution of {soft,}irq | |
883 | * time, a more accurate solution would be to update the irq_time using | |
884 | * the current rq->clock timestamp, except that would require using | |
885 | * atomic ops. | |
886 | */ | |
887 | if (irq_delta > delta) | |
888 | irq_delta = delta; | |
889 | ||
890 | rq->prev_irq_time += irq_delta; | |
891 | delta -= irq_delta; | |
095c0aa8 GC |
892 | #endif |
893 | #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING | |
c5905afb | 894 | if (static_key_false((¶virt_steal_rq_enabled))) { |
095c0aa8 GC |
895 | steal = paravirt_steal_clock(cpu_of(rq)); |
896 | steal -= rq->prev_steal_time_rq; | |
897 | ||
898 | if (unlikely(steal > delta)) | |
899 | steal = delta; | |
900 | ||
095c0aa8 | 901 | rq->prev_steal_time_rq += steal; |
095c0aa8 GC |
902 | delta -= steal; |
903 | } | |
904 | #endif | |
905 | ||
fe44d621 PZ |
906 | rq->clock_task += delta; |
907 | ||
095c0aa8 | 908 | #if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING) |
5d4dfddd | 909 | if ((irq_delta + steal) && sched_feat(NONTASK_CAPACITY)) |
095c0aa8 GC |
910 | sched_rt_avg_update(rq, irq_delta + steal); |
911 | #endif | |
aa483808 VP |
912 | } |
913 | ||
34f971f6 PZ |
914 | void sched_set_stop_task(int cpu, struct task_struct *stop) |
915 | { | |
916 | struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 }; | |
917 | struct task_struct *old_stop = cpu_rq(cpu)->stop; | |
918 | ||
919 | if (stop) { | |
920 | /* | |
921 | * Make it appear like a SCHED_FIFO task, its something | |
922 | * userspace knows about and won't get confused about. | |
923 | * | |
924 | * Also, it will make PI more or less work without too | |
925 | * much confusion -- but then, stop work should not | |
926 | * rely on PI working anyway. | |
927 | */ | |
928 | sched_setscheduler_nocheck(stop, SCHED_FIFO, ¶m); | |
929 | ||
930 | stop->sched_class = &stop_sched_class; | |
931 | } | |
932 | ||
933 | cpu_rq(cpu)->stop = stop; | |
934 | ||
935 | if (old_stop) { | |
936 | /* | |
937 | * Reset it back to a normal scheduling class so that | |
938 | * it can die in pieces. | |
939 | */ | |
940 | old_stop->sched_class = &rt_sched_class; | |
941 | } | |
942 | } | |
943 | ||
14531189 | 944 | /* |
dd41f596 | 945 | * __normal_prio - return the priority that is based on the static prio |
14531189 | 946 | */ |
14531189 IM |
947 | static inline int __normal_prio(struct task_struct *p) |
948 | { | |
dd41f596 | 949 | return p->static_prio; |
14531189 IM |
950 | } |
951 | ||
b29739f9 IM |
952 | /* |
953 | * Calculate the expected normal priority: i.e. priority | |
954 | * without taking RT-inheritance into account. Might be | |
955 | * boosted by interactivity modifiers. Changes upon fork, | |
956 | * setprio syscalls, and whenever the interactivity | |
957 | * estimator recalculates. | |
958 | */ | |
36c8b586 | 959 | static inline int normal_prio(struct task_struct *p) |
b29739f9 IM |
960 | { |
961 | int prio; | |
962 | ||
aab03e05 DF |
963 | if (task_has_dl_policy(p)) |
964 | prio = MAX_DL_PRIO-1; | |
965 | else if (task_has_rt_policy(p)) | |
b29739f9 IM |
966 | prio = MAX_RT_PRIO-1 - p->rt_priority; |
967 | else | |
968 | prio = __normal_prio(p); | |
969 | return prio; | |
970 | } | |
971 | ||
972 | /* | |
973 | * Calculate the current priority, i.e. the priority | |
974 | * taken into account by the scheduler. This value might | |
975 | * be boosted by RT tasks, or might be boosted by | |
976 | * interactivity modifiers. Will be RT if the task got | |
977 | * RT-boosted. If not then it returns p->normal_prio. | |
978 | */ | |
36c8b586 | 979 | static int effective_prio(struct task_struct *p) |
b29739f9 IM |
980 | { |
981 | p->normal_prio = normal_prio(p); | |
982 | /* | |
983 | * If we are RT tasks or we were boosted to RT priority, | |
984 | * keep the priority unchanged. Otherwise, update priority | |
985 | * to the normal priority: | |
986 | */ | |
987 | if (!rt_prio(p->prio)) | |
988 | return p->normal_prio; | |
989 | return p->prio; | |
990 | } | |
991 | ||
1da177e4 LT |
992 | /** |
993 | * task_curr - is this task currently executing on a CPU? | |
994 | * @p: the task in question. | |
e69f6186 YB |
995 | * |
996 | * Return: 1 if the task is currently executing. 0 otherwise. | |
1da177e4 | 997 | */ |
36c8b586 | 998 | inline int task_curr(const struct task_struct *p) |
1da177e4 LT |
999 | { |
1000 | return cpu_curr(task_cpu(p)) == p; | |
1001 | } | |
1002 | ||
67dfa1b7 | 1003 | /* |
4c9a4bc8 PZ |
1004 | * switched_from, switched_to and prio_changed must _NOT_ drop rq->lock, |
1005 | * use the balance_callback list if you want balancing. | |
1006 | * | |
1007 | * this means any call to check_class_changed() must be followed by a call to | |
1008 | * balance_callback(). | |
67dfa1b7 | 1009 | */ |
cb469845 SR |
1010 | static inline void check_class_changed(struct rq *rq, struct task_struct *p, |
1011 | const struct sched_class *prev_class, | |
da7a735e | 1012 | int oldprio) |
cb469845 SR |
1013 | { |
1014 | if (prev_class != p->sched_class) { | |
1015 | if (prev_class->switched_from) | |
da7a735e | 1016 | prev_class->switched_from(rq, p); |
4c9a4bc8 | 1017 | |
da7a735e | 1018 | p->sched_class->switched_to(rq, p); |
2d3d891d | 1019 | } else if (oldprio != p->prio || dl_task(p)) |
da7a735e | 1020 | p->sched_class->prio_changed(rq, p, oldprio); |
cb469845 SR |
1021 | } |
1022 | ||
029632fb | 1023 | void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags) |
1e5a7405 PZ |
1024 | { |
1025 | const struct sched_class *class; | |
1026 | ||
1027 | if (p->sched_class == rq->curr->sched_class) { | |
1028 | rq->curr->sched_class->check_preempt_curr(rq, p, flags); | |
1029 | } else { | |
1030 | for_each_class(class) { | |
1031 | if (class == rq->curr->sched_class) | |
1032 | break; | |
1033 | if (class == p->sched_class) { | |
8875125e | 1034 | resched_curr(rq); |
1e5a7405 PZ |
1035 | break; |
1036 | } | |
1037 | } | |
1038 | } | |
1039 | ||
1040 | /* | |
1041 | * A queue event has occurred, and we're going to schedule. In | |
1042 | * this case, we can save a useless back to back clock update. | |
1043 | */ | |
da0c1e65 | 1044 | if (task_on_rq_queued(rq->curr) && test_tsk_need_resched(rq->curr)) |
9edfbfed | 1045 | rq_clock_skip_update(rq, true); |
1e5a7405 PZ |
1046 | } |
1047 | ||
1da177e4 | 1048 | #ifdef CONFIG_SMP |
5cc389bc PZ |
1049 | /* |
1050 | * This is how migration works: | |
1051 | * | |
1052 | * 1) we invoke migration_cpu_stop() on the target CPU using | |
1053 | * stop_one_cpu(). | |
1054 | * 2) stopper starts to run (implicitly forcing the migrated thread | |
1055 | * off the CPU) | |
1056 | * 3) it checks whether the migrated task is still in the wrong runqueue. | |
1057 | * 4) if it's in the wrong runqueue then the migration thread removes | |
1058 | * it and puts it into the right queue. | |
1059 | * 5) stopper completes and stop_one_cpu() returns and the migration | |
1060 | * is done. | |
1061 | */ | |
1062 | ||
1063 | /* | |
1064 | * move_queued_task - move a queued task to new rq. | |
1065 | * | |
1066 | * Returns (locked) new rq. Old rq's lock is released. | |
1067 | */ | |
1068 | static struct rq *move_queued_task(struct task_struct *p, int new_cpu) | |
1069 | { | |
1070 | struct rq *rq = task_rq(p); | |
1071 | ||
1072 | lockdep_assert_held(&rq->lock); | |
1073 | ||
1074 | dequeue_task(rq, p, 0); | |
1075 | p->on_rq = TASK_ON_RQ_MIGRATING; | |
1076 | set_task_cpu(p, new_cpu); | |
1077 | raw_spin_unlock(&rq->lock); | |
1078 | ||
1079 | rq = cpu_rq(new_cpu); | |
1080 | ||
1081 | raw_spin_lock(&rq->lock); | |
1082 | BUG_ON(task_cpu(p) != new_cpu); | |
1083 | p->on_rq = TASK_ON_RQ_QUEUED; | |
1084 | enqueue_task(rq, p, 0); | |
1085 | check_preempt_curr(rq, p, 0); | |
1086 | ||
1087 | return rq; | |
1088 | } | |
1089 | ||
1090 | struct migration_arg { | |
1091 | struct task_struct *task; | |
1092 | int dest_cpu; | |
1093 | }; | |
1094 | ||
1095 | /* | |
1096 | * Move (not current) task off this cpu, onto dest cpu. We're doing | |
1097 | * this because either it can't run here any more (set_cpus_allowed() | |
1098 | * away from this CPU, or CPU going down), or because we're | |
1099 | * attempting to rebalance this task on exec (sched_exec). | |
1100 | * | |
1101 | * So we race with normal scheduler movements, but that's OK, as long | |
1102 | * as the task is no longer on this CPU. | |
1103 | * | |
1104 | * Returns non-zero if task was successfully migrated. | |
1105 | */ | |
1106 | static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu) | |
1107 | { | |
1108 | struct rq *rq; | |
1109 | int ret = 0; | |
1110 | ||
1111 | if (unlikely(!cpu_active(dest_cpu))) | |
1112 | return ret; | |
1113 | ||
1114 | rq = cpu_rq(src_cpu); | |
1115 | ||
1116 | raw_spin_lock(&p->pi_lock); | |
1117 | raw_spin_lock(&rq->lock); | |
1118 | /* Already moved. */ | |
1119 | if (task_cpu(p) != src_cpu) | |
1120 | goto done; | |
1121 | ||
1122 | /* Affinity changed (again). */ | |
1123 | if (!cpumask_test_cpu(dest_cpu, tsk_cpus_allowed(p))) | |
1124 | goto fail; | |
1125 | ||
1126 | /* | |
1127 | * If we're not on a rq, the next wake-up will ensure we're | |
1128 | * placed properly. | |
1129 | */ | |
1130 | if (task_on_rq_queued(p)) | |
1131 | rq = move_queued_task(p, dest_cpu); | |
1132 | done: | |
1133 | ret = 1; | |
1134 | fail: | |
1135 | raw_spin_unlock(&rq->lock); | |
1136 | raw_spin_unlock(&p->pi_lock); | |
1137 | return ret; | |
1138 | } | |
1139 | ||
1140 | /* | |
1141 | * migration_cpu_stop - this will be executed by a highprio stopper thread | |
1142 | * and performs thread migration by bumping thread off CPU then | |
1143 | * 'pushing' onto another runqueue. | |
1144 | */ | |
1145 | static int migration_cpu_stop(void *data) | |
1146 | { | |
1147 | struct migration_arg *arg = data; | |
1148 | ||
1149 | /* | |
1150 | * The original target cpu might have gone down and we might | |
1151 | * be on another cpu but it doesn't matter. | |
1152 | */ | |
1153 | local_irq_disable(); | |
1154 | /* | |
1155 | * We need to explicitly wake pending tasks before running | |
1156 | * __migrate_task() such that we will not miss enforcing cpus_allowed | |
1157 | * during wakeups, see set_cpus_allowed_ptr()'s TASK_WAKING test. | |
1158 | */ | |
1159 | sched_ttwu_pending(); | |
1160 | __migrate_task(arg->task, raw_smp_processor_id(), arg->dest_cpu); | |
1161 | local_irq_enable(); | |
1162 | return 0; | |
1163 | } | |
1164 | ||
1165 | void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask) | |
1166 | { | |
1167 | if (p->sched_class->set_cpus_allowed) | |
1168 | p->sched_class->set_cpus_allowed(p, new_mask); | |
1169 | ||
1170 | cpumask_copy(&p->cpus_allowed, new_mask); | |
1171 | p->nr_cpus_allowed = cpumask_weight(new_mask); | |
1172 | } | |
1173 | ||
1174 | /* | |
1175 | * Change a given task's CPU affinity. Migrate the thread to a | |
1176 | * proper CPU and schedule it away if the CPU it's executing on | |
1177 | * is removed from the allowed bitmask. | |
1178 | * | |
1179 | * NOTE: the caller must have a valid reference to the task, the | |
1180 | * task must not exit() & deallocate itself prematurely. The | |
1181 | * call is not atomic; no spinlocks may be held. | |
1182 | */ | |
1183 | int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask) | |
1184 | { | |
1185 | unsigned long flags; | |
1186 | struct rq *rq; | |
1187 | unsigned int dest_cpu; | |
1188 | int ret = 0; | |
1189 | ||
1190 | rq = task_rq_lock(p, &flags); | |
1191 | ||
1192 | if (cpumask_equal(&p->cpus_allowed, new_mask)) | |
1193 | goto out; | |
1194 | ||
1195 | if (!cpumask_intersects(new_mask, cpu_active_mask)) { | |
1196 | ret = -EINVAL; | |
1197 | goto out; | |
1198 | } | |
1199 | ||
1200 | do_set_cpus_allowed(p, new_mask); | |
1201 | ||
1202 | /* Can the task run on the task's current CPU? If so, we're done */ | |
1203 | if (cpumask_test_cpu(task_cpu(p), new_mask)) | |
1204 | goto out; | |
1205 | ||
1206 | dest_cpu = cpumask_any_and(cpu_active_mask, new_mask); | |
1207 | if (task_running(rq, p) || p->state == TASK_WAKING) { | |
1208 | struct migration_arg arg = { p, dest_cpu }; | |
1209 | /* Need help from migration thread: drop lock and wait. */ | |
1210 | task_rq_unlock(rq, p, &flags); | |
1211 | stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg); | |
1212 | tlb_migrate_finish(p->mm); | |
1213 | return 0; | |
1214 | } else if (task_on_rq_queued(p)) | |
1215 | rq = move_queued_task(p, dest_cpu); | |
1216 | out: | |
1217 | task_rq_unlock(rq, p, &flags); | |
1218 | ||
1219 | return ret; | |
1220 | } | |
1221 | EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr); | |
1222 | ||
dd41f596 | 1223 | void set_task_cpu(struct task_struct *p, unsigned int new_cpu) |
c65cc870 | 1224 | { |
e2912009 PZ |
1225 | #ifdef CONFIG_SCHED_DEBUG |
1226 | /* | |
1227 | * We should never call set_task_cpu() on a blocked task, | |
1228 | * ttwu() will sort out the placement. | |
1229 | */ | |
077614ee | 1230 | WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING && |
e2336f6e | 1231 | !p->on_rq); |
0122ec5b PZ |
1232 | |
1233 | #ifdef CONFIG_LOCKDEP | |
6c6c54e1 PZ |
1234 | /* |
1235 | * The caller should hold either p->pi_lock or rq->lock, when changing | |
1236 | * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks. | |
1237 | * | |
1238 | * sched_move_task() holds both and thus holding either pins the cgroup, | |
8323f26c | 1239 | * see task_group(). |
6c6c54e1 PZ |
1240 | * |
1241 | * Furthermore, all task_rq users should acquire both locks, see | |
1242 | * task_rq_lock(). | |
1243 | */ | |
0122ec5b PZ |
1244 | WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) || |
1245 | lockdep_is_held(&task_rq(p)->lock))); | |
1246 | #endif | |
e2912009 PZ |
1247 | #endif |
1248 | ||
de1d7286 | 1249 | trace_sched_migrate_task(p, new_cpu); |
cbc34ed1 | 1250 | |
0c69774e | 1251 | if (task_cpu(p) != new_cpu) { |
0a74bef8 PT |
1252 | if (p->sched_class->migrate_task_rq) |
1253 | p->sched_class->migrate_task_rq(p, new_cpu); | |
0c69774e | 1254 | p->se.nr_migrations++; |
86038c5e | 1255 | perf_sw_event_sched(PERF_COUNT_SW_CPU_MIGRATIONS, 1, 0); |
0c69774e | 1256 | } |
dd41f596 IM |
1257 | |
1258 | __set_task_cpu(p, new_cpu); | |
c65cc870 IM |
1259 | } |
1260 | ||
ac66f547 PZ |
1261 | static void __migrate_swap_task(struct task_struct *p, int cpu) |
1262 | { | |
da0c1e65 | 1263 | if (task_on_rq_queued(p)) { |
ac66f547 PZ |
1264 | struct rq *src_rq, *dst_rq; |
1265 | ||
1266 | src_rq = task_rq(p); | |
1267 | dst_rq = cpu_rq(cpu); | |
1268 | ||
1269 | deactivate_task(src_rq, p, 0); | |
1270 | set_task_cpu(p, cpu); | |
1271 | activate_task(dst_rq, p, 0); | |
1272 | check_preempt_curr(dst_rq, p, 0); | |
1273 | } else { | |
1274 | /* | |
1275 | * Task isn't running anymore; make it appear like we migrated | |
1276 | * it before it went to sleep. This means on wakeup we make the | |
1277 | * previous cpu our targer instead of where it really is. | |
1278 | */ | |
1279 | p->wake_cpu = cpu; | |
1280 | } | |
1281 | } | |
1282 | ||
1283 | struct migration_swap_arg { | |
1284 | struct task_struct *src_task, *dst_task; | |
1285 | int src_cpu, dst_cpu; | |
1286 | }; | |
1287 | ||
1288 | static int migrate_swap_stop(void *data) | |
1289 | { | |
1290 | struct migration_swap_arg *arg = data; | |
1291 | struct rq *src_rq, *dst_rq; | |
1292 | int ret = -EAGAIN; | |
1293 | ||
1294 | src_rq = cpu_rq(arg->src_cpu); | |
1295 | dst_rq = cpu_rq(arg->dst_cpu); | |
1296 | ||
74602315 PZ |
1297 | double_raw_lock(&arg->src_task->pi_lock, |
1298 | &arg->dst_task->pi_lock); | |
ac66f547 PZ |
1299 | double_rq_lock(src_rq, dst_rq); |
1300 | if (task_cpu(arg->dst_task) != arg->dst_cpu) | |
1301 | goto unlock; | |
1302 | ||
1303 | if (task_cpu(arg->src_task) != arg->src_cpu) | |
1304 | goto unlock; | |
1305 | ||
1306 | if (!cpumask_test_cpu(arg->dst_cpu, tsk_cpus_allowed(arg->src_task))) | |
1307 | goto unlock; | |
1308 | ||
1309 | if (!cpumask_test_cpu(arg->src_cpu, tsk_cpus_allowed(arg->dst_task))) | |
1310 | goto unlock; | |
1311 | ||
1312 | __migrate_swap_task(arg->src_task, arg->dst_cpu); | |
1313 | __migrate_swap_task(arg->dst_task, arg->src_cpu); | |
1314 | ||
1315 | ret = 0; | |
1316 | ||
1317 | unlock: | |
1318 | double_rq_unlock(src_rq, dst_rq); | |
74602315 PZ |
1319 | raw_spin_unlock(&arg->dst_task->pi_lock); |
1320 | raw_spin_unlock(&arg->src_task->pi_lock); | |
ac66f547 PZ |
1321 | |
1322 | return ret; | |
1323 | } | |
1324 | ||
1325 | /* | |
1326 | * Cross migrate two tasks | |
1327 | */ | |
1328 | int migrate_swap(struct task_struct *cur, struct task_struct *p) | |
1329 | { | |
1330 | struct migration_swap_arg arg; | |
1331 | int ret = -EINVAL; | |
1332 | ||
ac66f547 PZ |
1333 | arg = (struct migration_swap_arg){ |
1334 | .src_task = cur, | |
1335 | .src_cpu = task_cpu(cur), | |
1336 | .dst_task = p, | |
1337 | .dst_cpu = task_cpu(p), | |
1338 | }; | |
1339 | ||
1340 | if (arg.src_cpu == arg.dst_cpu) | |
1341 | goto out; | |
1342 | ||
6acce3ef PZ |
1343 | /* |
1344 | * These three tests are all lockless; this is OK since all of them | |
1345 | * will be re-checked with proper locks held further down the line. | |
1346 | */ | |
ac66f547 PZ |
1347 | if (!cpu_active(arg.src_cpu) || !cpu_active(arg.dst_cpu)) |
1348 | goto out; | |
1349 | ||
1350 | if (!cpumask_test_cpu(arg.dst_cpu, tsk_cpus_allowed(arg.src_task))) | |
1351 | goto out; | |
1352 | ||
1353 | if (!cpumask_test_cpu(arg.src_cpu, tsk_cpus_allowed(arg.dst_task))) | |
1354 | goto out; | |
1355 | ||
286549dc | 1356 | trace_sched_swap_numa(cur, arg.src_cpu, p, arg.dst_cpu); |
ac66f547 PZ |
1357 | ret = stop_two_cpus(arg.dst_cpu, arg.src_cpu, migrate_swap_stop, &arg); |
1358 | ||
1359 | out: | |
ac66f547 PZ |
1360 | return ret; |
1361 | } | |
1362 | ||
1da177e4 LT |
1363 | /* |
1364 | * wait_task_inactive - wait for a thread to unschedule. | |
1365 | * | |
85ba2d86 RM |
1366 | * If @match_state is nonzero, it's the @p->state value just checked and |
1367 | * not expected to change. If it changes, i.e. @p might have woken up, | |
1368 | * then return zero. When we succeed in waiting for @p to be off its CPU, | |
1369 | * we return a positive number (its total switch count). If a second call | |
1370 | * a short while later returns the same number, the caller can be sure that | |
1371 | * @p has remained unscheduled the whole time. | |
1372 | * | |
1da177e4 LT |
1373 | * The caller must ensure that the task *will* unschedule sometime soon, |
1374 | * else this function might spin for a *long* time. This function can't | |
1375 | * be called with interrupts off, or it may introduce deadlock with | |
1376 | * smp_call_function() if an IPI is sent by the same process we are | |
1377 | * waiting to become inactive. | |
1378 | */ | |
85ba2d86 | 1379 | unsigned long wait_task_inactive(struct task_struct *p, long match_state) |
1da177e4 LT |
1380 | { |
1381 | unsigned long flags; | |
da0c1e65 | 1382 | int running, queued; |
85ba2d86 | 1383 | unsigned long ncsw; |
70b97a7f | 1384 | struct rq *rq; |
1da177e4 | 1385 | |
3a5c359a AK |
1386 | for (;;) { |
1387 | /* | |
1388 | * We do the initial early heuristics without holding | |
1389 | * any task-queue locks at all. We'll only try to get | |
1390 | * the runqueue lock when things look like they will | |
1391 | * work out! | |
1392 | */ | |
1393 | rq = task_rq(p); | |
fa490cfd | 1394 | |
3a5c359a AK |
1395 | /* |
1396 | * If the task is actively running on another CPU | |
1397 | * still, just relax and busy-wait without holding | |
1398 | * any locks. | |
1399 | * | |
1400 | * NOTE! Since we don't hold any locks, it's not | |
1401 | * even sure that "rq" stays as the right runqueue! | |
1402 | * But we don't care, since "task_running()" will | |
1403 | * return false if the runqueue has changed and p | |
1404 | * is actually now running somewhere else! | |
1405 | */ | |
85ba2d86 RM |
1406 | while (task_running(rq, p)) { |
1407 | if (match_state && unlikely(p->state != match_state)) | |
1408 | return 0; | |
3a5c359a | 1409 | cpu_relax(); |
85ba2d86 | 1410 | } |
fa490cfd | 1411 | |
3a5c359a AK |
1412 | /* |
1413 | * Ok, time to look more closely! We need the rq | |
1414 | * lock now, to be *sure*. If we're wrong, we'll | |
1415 | * just go back and repeat. | |
1416 | */ | |
1417 | rq = task_rq_lock(p, &flags); | |
27a9da65 | 1418 | trace_sched_wait_task(p); |
3a5c359a | 1419 | running = task_running(rq, p); |
da0c1e65 | 1420 | queued = task_on_rq_queued(p); |
85ba2d86 | 1421 | ncsw = 0; |
f31e11d8 | 1422 | if (!match_state || p->state == match_state) |
93dcf55f | 1423 | ncsw = p->nvcsw | LONG_MIN; /* sets MSB */ |
0122ec5b | 1424 | task_rq_unlock(rq, p, &flags); |
fa490cfd | 1425 | |
85ba2d86 RM |
1426 | /* |
1427 | * If it changed from the expected state, bail out now. | |
1428 | */ | |
1429 | if (unlikely(!ncsw)) | |
1430 | break; | |
1431 | ||
3a5c359a AK |
1432 | /* |
1433 | * Was it really running after all now that we | |
1434 | * checked with the proper locks actually held? | |
1435 | * | |
1436 | * Oops. Go back and try again.. | |
1437 | */ | |
1438 | if (unlikely(running)) { | |
1439 | cpu_relax(); | |
1440 | continue; | |
1441 | } | |
fa490cfd | 1442 | |
3a5c359a AK |
1443 | /* |
1444 | * It's not enough that it's not actively running, | |
1445 | * it must be off the runqueue _entirely_, and not | |
1446 | * preempted! | |
1447 | * | |
80dd99b3 | 1448 | * So if it was still runnable (but just not actively |
3a5c359a AK |
1449 | * running right now), it's preempted, and we should |
1450 | * yield - it could be a while. | |
1451 | */ | |
da0c1e65 | 1452 | if (unlikely(queued)) { |
8eb90c30 TG |
1453 | ktime_t to = ktime_set(0, NSEC_PER_SEC/HZ); |
1454 | ||
1455 | set_current_state(TASK_UNINTERRUPTIBLE); | |
1456 | schedule_hrtimeout(&to, HRTIMER_MODE_REL); | |
3a5c359a AK |
1457 | continue; |
1458 | } | |
fa490cfd | 1459 | |
3a5c359a AK |
1460 | /* |
1461 | * Ahh, all good. It wasn't running, and it wasn't | |
1462 | * runnable, which means that it will never become | |
1463 | * running in the future either. We're all done! | |
1464 | */ | |
1465 | break; | |
1466 | } | |
85ba2d86 RM |
1467 | |
1468 | return ncsw; | |
1da177e4 LT |
1469 | } |
1470 | ||
1471 | /*** | |
1472 | * kick_process - kick a running thread to enter/exit the kernel | |
1473 | * @p: the to-be-kicked thread | |
1474 | * | |
1475 | * Cause a process which is running on another CPU to enter | |
1476 | * kernel-mode, without any delay. (to get signals handled.) | |
1477 | * | |
25985edc | 1478 | * NOTE: this function doesn't have to take the runqueue lock, |
1da177e4 LT |
1479 | * because all it wants to ensure is that the remote task enters |
1480 | * the kernel. If the IPI races and the task has been migrated | |
1481 | * to another CPU then no harm is done and the purpose has been | |
1482 | * achieved as well. | |
1483 | */ | |
36c8b586 | 1484 | void kick_process(struct task_struct *p) |
1da177e4 LT |
1485 | { |
1486 | int cpu; | |
1487 | ||
1488 | preempt_disable(); | |
1489 | cpu = task_cpu(p); | |
1490 | if ((cpu != smp_processor_id()) && task_curr(p)) | |
1491 | smp_send_reschedule(cpu); | |
1492 | preempt_enable(); | |
1493 | } | |
b43e3521 | 1494 | EXPORT_SYMBOL_GPL(kick_process); |
1da177e4 | 1495 | |
30da688e | 1496 | /* |
013fdb80 | 1497 | * ->cpus_allowed is protected by both rq->lock and p->pi_lock |
30da688e | 1498 | */ |
5da9a0fb PZ |
1499 | static int select_fallback_rq(int cpu, struct task_struct *p) |
1500 | { | |
aa00d89c TC |
1501 | int nid = cpu_to_node(cpu); |
1502 | const struct cpumask *nodemask = NULL; | |
2baab4e9 PZ |
1503 | enum { cpuset, possible, fail } state = cpuset; |
1504 | int dest_cpu; | |
5da9a0fb | 1505 | |
aa00d89c TC |
1506 | /* |
1507 | * If the node that the cpu is on has been offlined, cpu_to_node() | |
1508 | * will return -1. There is no cpu on the node, and we should | |
1509 | * select the cpu on the other node. | |
1510 | */ | |
1511 | if (nid != -1) { | |
1512 | nodemask = cpumask_of_node(nid); | |
1513 | ||
1514 | /* Look for allowed, online CPU in same node. */ | |
1515 | for_each_cpu(dest_cpu, nodemask) { | |
1516 | if (!cpu_online(dest_cpu)) | |
1517 | continue; | |
1518 | if (!cpu_active(dest_cpu)) | |
1519 | continue; | |
1520 | if (cpumask_test_cpu(dest_cpu, tsk_cpus_allowed(p))) | |
1521 | return dest_cpu; | |
1522 | } | |
2baab4e9 | 1523 | } |
5da9a0fb | 1524 | |
2baab4e9 PZ |
1525 | for (;;) { |
1526 | /* Any allowed, online CPU? */ | |
e3831edd | 1527 | for_each_cpu(dest_cpu, tsk_cpus_allowed(p)) { |
2baab4e9 PZ |
1528 | if (!cpu_online(dest_cpu)) |
1529 | continue; | |
1530 | if (!cpu_active(dest_cpu)) | |
1531 | continue; | |
1532 | goto out; | |
1533 | } | |
5da9a0fb | 1534 | |
2baab4e9 PZ |
1535 | switch (state) { |
1536 | case cpuset: | |
1537 | /* No more Mr. Nice Guy. */ | |
1538 | cpuset_cpus_allowed_fallback(p); | |
1539 | state = possible; | |
1540 | break; | |
1541 | ||
1542 | case possible: | |
1543 | do_set_cpus_allowed(p, cpu_possible_mask); | |
1544 | state = fail; | |
1545 | break; | |
1546 | ||
1547 | case fail: | |
1548 | BUG(); | |
1549 | break; | |
1550 | } | |
1551 | } | |
1552 | ||
1553 | out: | |
1554 | if (state != cpuset) { | |
1555 | /* | |
1556 | * Don't tell them about moving exiting tasks or | |
1557 | * kernel threads (both mm NULL), since they never | |
1558 | * leave kernel. | |
1559 | */ | |
1560 | if (p->mm && printk_ratelimit()) { | |
aac74dc4 | 1561 | printk_deferred("process %d (%s) no longer affine to cpu%d\n", |
2baab4e9 PZ |
1562 | task_pid_nr(p), p->comm, cpu); |
1563 | } | |
5da9a0fb PZ |
1564 | } |
1565 | ||
1566 | return dest_cpu; | |
1567 | } | |
1568 | ||
e2912009 | 1569 | /* |
013fdb80 | 1570 | * The caller (fork, wakeup) owns p->pi_lock, ->cpus_allowed is stable. |
e2912009 | 1571 | */ |
970b13ba | 1572 | static inline |
ac66f547 | 1573 | int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags) |
970b13ba | 1574 | { |
6c1d9410 WL |
1575 | if (p->nr_cpus_allowed > 1) |
1576 | cpu = p->sched_class->select_task_rq(p, cpu, sd_flags, wake_flags); | |
e2912009 PZ |
1577 | |
1578 | /* | |
1579 | * In order not to call set_task_cpu() on a blocking task we need | |
1580 | * to rely on ttwu() to place the task on a valid ->cpus_allowed | |
1581 | * cpu. | |
1582 | * | |
1583 | * Since this is common to all placement strategies, this lives here. | |
1584 | * | |
1585 | * [ this allows ->select_task() to simply return task_cpu(p) and | |
1586 | * not worry about this generic constraint ] | |
1587 | */ | |
fa17b507 | 1588 | if (unlikely(!cpumask_test_cpu(cpu, tsk_cpus_allowed(p)) || |
70f11205 | 1589 | !cpu_online(cpu))) |
5da9a0fb | 1590 | cpu = select_fallback_rq(task_cpu(p), p); |
e2912009 PZ |
1591 | |
1592 | return cpu; | |
970b13ba | 1593 | } |
09a40af5 MG |
1594 | |
1595 | static void update_avg(u64 *avg, u64 sample) | |
1596 | { | |
1597 | s64 diff = sample - *avg; | |
1598 | *avg += diff >> 3; | |
1599 | } | |
5cc389bc | 1600 | #endif /* CONFIG_SMP */ |
970b13ba | 1601 | |
d7c01d27 | 1602 | static void |
b84cb5df | 1603 | ttwu_stat(struct task_struct *p, int cpu, int wake_flags) |
9ed3811a | 1604 | { |
d7c01d27 | 1605 | #ifdef CONFIG_SCHEDSTATS |
b84cb5df PZ |
1606 | struct rq *rq = this_rq(); |
1607 | ||
d7c01d27 PZ |
1608 | #ifdef CONFIG_SMP |
1609 | int this_cpu = smp_processor_id(); | |
1610 | ||
1611 | if (cpu == this_cpu) { | |
1612 | schedstat_inc(rq, ttwu_local); | |
1613 | schedstat_inc(p, se.statistics.nr_wakeups_local); | |
1614 | } else { | |
1615 | struct sched_domain *sd; | |
1616 | ||
1617 | schedstat_inc(p, se.statistics.nr_wakeups_remote); | |
057f3fad | 1618 | rcu_read_lock(); |
d7c01d27 PZ |
1619 | for_each_domain(this_cpu, sd) { |
1620 | if (cpumask_test_cpu(cpu, sched_domain_span(sd))) { | |
1621 | schedstat_inc(sd, ttwu_wake_remote); | |
1622 | break; | |
1623 | } | |
1624 | } | |
057f3fad | 1625 | rcu_read_unlock(); |
d7c01d27 | 1626 | } |
f339b9dc PZ |
1627 | |
1628 | if (wake_flags & WF_MIGRATED) | |
1629 | schedstat_inc(p, se.statistics.nr_wakeups_migrate); | |
1630 | ||
d7c01d27 PZ |
1631 | #endif /* CONFIG_SMP */ |
1632 | ||
1633 | schedstat_inc(rq, ttwu_count); | |
9ed3811a | 1634 | schedstat_inc(p, se.statistics.nr_wakeups); |
d7c01d27 PZ |
1635 | |
1636 | if (wake_flags & WF_SYNC) | |
9ed3811a | 1637 | schedstat_inc(p, se.statistics.nr_wakeups_sync); |
d7c01d27 | 1638 | |
d7c01d27 PZ |
1639 | #endif /* CONFIG_SCHEDSTATS */ |
1640 | } | |
1641 | ||
1642 | static void ttwu_activate(struct rq *rq, struct task_struct *p, int en_flags) | |
1643 | { | |
9ed3811a | 1644 | activate_task(rq, p, en_flags); |
da0c1e65 | 1645 | p->on_rq = TASK_ON_RQ_QUEUED; |
c2f7115e PZ |
1646 | |
1647 | /* if a worker is waking up, notify workqueue */ | |
1648 | if (p->flags & PF_WQ_WORKER) | |
1649 | wq_worker_waking_up(p, cpu_of(rq)); | |
9ed3811a TH |
1650 | } |
1651 | ||
23f41eeb PZ |
1652 | /* |
1653 | * Mark the task runnable and perform wakeup-preemption. | |
1654 | */ | |
89363381 | 1655 | static void |
23f41eeb | 1656 | ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags) |
9ed3811a | 1657 | { |
9ed3811a | 1658 | check_preempt_curr(rq, p, wake_flags); |
a8d7ad52 | 1659 | trace_sched_wakeup(p, true); |
9ed3811a TH |
1660 | |
1661 | p->state = TASK_RUNNING; | |
1662 | #ifdef CONFIG_SMP | |
4c9a4bc8 PZ |
1663 | if (p->sched_class->task_woken) { |
1664 | /* | |
1665 | * XXX can drop rq->lock; most likely ok. | |
1666 | */ | |
9ed3811a | 1667 | p->sched_class->task_woken(rq, p); |
4c9a4bc8 | 1668 | } |
9ed3811a | 1669 | |
e69c6341 | 1670 | if (rq->idle_stamp) { |
78becc27 | 1671 | u64 delta = rq_clock(rq) - rq->idle_stamp; |
9bd721c5 | 1672 | u64 max = 2*rq->max_idle_balance_cost; |
9ed3811a | 1673 | |
abfafa54 JL |
1674 | update_avg(&rq->avg_idle, delta); |
1675 | ||
1676 | if (rq->avg_idle > max) | |
9ed3811a | 1677 | rq->avg_idle = max; |
abfafa54 | 1678 | |
9ed3811a TH |
1679 | rq->idle_stamp = 0; |
1680 | } | |
1681 | #endif | |
1682 | } | |
1683 | ||
c05fbafb PZ |
1684 | static void |
1685 | ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags) | |
1686 | { | |
1687 | #ifdef CONFIG_SMP | |
1688 | if (p->sched_contributes_to_load) | |
1689 | rq->nr_uninterruptible--; | |
1690 | #endif | |
1691 | ||
1692 | ttwu_activate(rq, p, ENQUEUE_WAKEUP | ENQUEUE_WAKING); | |
1693 | ttwu_do_wakeup(rq, p, wake_flags); | |
1694 | } | |
1695 | ||
1696 | /* | |
1697 | * Called in case the task @p isn't fully descheduled from its runqueue, | |
1698 | * in this case we must do a remote wakeup. Its a 'light' wakeup though, | |
1699 | * since all we need to do is flip p->state to TASK_RUNNING, since | |
1700 | * the task is still ->on_rq. | |
1701 | */ | |
1702 | static int ttwu_remote(struct task_struct *p, int wake_flags) | |
1703 | { | |
1704 | struct rq *rq; | |
1705 | int ret = 0; | |
1706 | ||
1707 | rq = __task_rq_lock(p); | |
da0c1e65 | 1708 | if (task_on_rq_queued(p)) { |
1ad4ec0d FW |
1709 | /* check_preempt_curr() may use rq clock */ |
1710 | update_rq_clock(rq); | |
c05fbafb PZ |
1711 | ttwu_do_wakeup(rq, p, wake_flags); |
1712 | ret = 1; | |
1713 | } | |
1714 | __task_rq_unlock(rq); | |
1715 | ||
1716 | return ret; | |
1717 | } | |
1718 | ||
317f3941 | 1719 | #ifdef CONFIG_SMP |
e3baac47 | 1720 | void sched_ttwu_pending(void) |
317f3941 PZ |
1721 | { |
1722 | struct rq *rq = this_rq(); | |
fa14ff4a PZ |
1723 | struct llist_node *llist = llist_del_all(&rq->wake_list); |
1724 | struct task_struct *p; | |
e3baac47 | 1725 | unsigned long flags; |
317f3941 | 1726 | |
e3baac47 PZ |
1727 | if (!llist) |
1728 | return; | |
1729 | ||
1730 | raw_spin_lock_irqsave(&rq->lock, flags); | |
317f3941 | 1731 | |
fa14ff4a PZ |
1732 | while (llist) { |
1733 | p = llist_entry(llist, struct task_struct, wake_entry); | |
1734 | llist = llist_next(llist); | |
317f3941 PZ |
1735 | ttwu_do_activate(rq, p, 0); |
1736 | } | |
1737 | ||
e3baac47 | 1738 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
317f3941 PZ |
1739 | } |
1740 | ||
1741 | void scheduler_ipi(void) | |
1742 | { | |
f27dde8d PZ |
1743 | /* |
1744 | * Fold TIF_NEED_RESCHED into the preempt_count; anybody setting | |
1745 | * TIF_NEED_RESCHED remotely (for the first time) will also send | |
1746 | * this IPI. | |
1747 | */ | |
8cb75e0c | 1748 | preempt_fold_need_resched(); |
f27dde8d | 1749 | |
fd2ac4f4 | 1750 | if (llist_empty(&this_rq()->wake_list) && !got_nohz_idle_kick()) |
c5d753a5 PZ |
1751 | return; |
1752 | ||
1753 | /* | |
1754 | * Not all reschedule IPI handlers call irq_enter/irq_exit, since | |
1755 | * traditionally all their work was done from the interrupt return | |
1756 | * path. Now that we actually do some work, we need to make sure | |
1757 | * we do call them. | |
1758 | * | |
1759 | * Some archs already do call them, luckily irq_enter/exit nest | |
1760 | * properly. | |
1761 | * | |
1762 | * Arguably we should visit all archs and update all handlers, | |
1763 | * however a fair share of IPIs are still resched only so this would | |
1764 | * somewhat pessimize the simple resched case. | |
1765 | */ | |
1766 | irq_enter(); | |
fa14ff4a | 1767 | sched_ttwu_pending(); |
ca38062e SS |
1768 | |
1769 | /* | |
1770 | * Check if someone kicked us for doing the nohz idle load balance. | |
1771 | */ | |
873b4c65 | 1772 | if (unlikely(got_nohz_idle_kick())) { |
6eb57e0d | 1773 | this_rq()->idle_balance = 1; |
ca38062e | 1774 | raise_softirq_irqoff(SCHED_SOFTIRQ); |
6eb57e0d | 1775 | } |
c5d753a5 | 1776 | irq_exit(); |
317f3941 PZ |
1777 | } |
1778 | ||
1779 | static void ttwu_queue_remote(struct task_struct *p, int cpu) | |
1780 | { | |
e3baac47 PZ |
1781 | struct rq *rq = cpu_rq(cpu); |
1782 | ||
1783 | if (llist_add(&p->wake_entry, &cpu_rq(cpu)->wake_list)) { | |
1784 | if (!set_nr_if_polling(rq->idle)) | |
1785 | smp_send_reschedule(cpu); | |
1786 | else | |
1787 | trace_sched_wake_idle_without_ipi(cpu); | |
1788 | } | |
317f3941 | 1789 | } |
d6aa8f85 | 1790 | |
f6be8af1 CL |
1791 | void wake_up_if_idle(int cpu) |
1792 | { | |
1793 | struct rq *rq = cpu_rq(cpu); | |
1794 | unsigned long flags; | |
1795 | ||
fd7de1e8 AL |
1796 | rcu_read_lock(); |
1797 | ||
1798 | if (!is_idle_task(rcu_dereference(rq->curr))) | |
1799 | goto out; | |
f6be8af1 CL |
1800 | |
1801 | if (set_nr_if_polling(rq->idle)) { | |
1802 | trace_sched_wake_idle_without_ipi(cpu); | |
1803 | } else { | |
1804 | raw_spin_lock_irqsave(&rq->lock, flags); | |
1805 | if (is_idle_task(rq->curr)) | |
1806 | smp_send_reschedule(cpu); | |
1807 | /* Else cpu is not in idle, do nothing here */ | |
1808 | raw_spin_unlock_irqrestore(&rq->lock, flags); | |
1809 | } | |
fd7de1e8 AL |
1810 | |
1811 | out: | |
1812 | rcu_read_unlock(); | |
f6be8af1 CL |
1813 | } |
1814 | ||
39be3501 | 1815 | bool cpus_share_cache(int this_cpu, int that_cpu) |
518cd623 PZ |
1816 | { |
1817 | return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu); | |
1818 | } | |
d6aa8f85 | 1819 | #endif /* CONFIG_SMP */ |
317f3941 | 1820 | |
c05fbafb PZ |
1821 | static void ttwu_queue(struct task_struct *p, int cpu) |
1822 | { | |
1823 | struct rq *rq = cpu_rq(cpu); | |
1824 | ||
17d9f311 | 1825 | #if defined(CONFIG_SMP) |
39be3501 | 1826 | if (sched_feat(TTWU_QUEUE) && !cpus_share_cache(smp_processor_id(), cpu)) { |
f01114cb | 1827 | sched_clock_cpu(cpu); /* sync clocks x-cpu */ |
317f3941 PZ |
1828 | ttwu_queue_remote(p, cpu); |
1829 | return; | |
1830 | } | |
1831 | #endif | |
1832 | ||
c05fbafb PZ |
1833 | raw_spin_lock(&rq->lock); |
1834 | ttwu_do_activate(rq, p, 0); | |
1835 | raw_spin_unlock(&rq->lock); | |
9ed3811a TH |
1836 | } |
1837 | ||
1838 | /** | |
1da177e4 | 1839 | * try_to_wake_up - wake up a thread |
9ed3811a | 1840 | * @p: the thread to be awakened |
1da177e4 | 1841 | * @state: the mask of task states that can be woken |
9ed3811a | 1842 | * @wake_flags: wake modifier flags (WF_*) |
1da177e4 LT |
1843 | * |
1844 | * Put it on the run-queue if it's not already there. The "current" | |
1845 | * thread is always on the run-queue (except when the actual | |
1846 | * re-schedule is in progress), and as such you're allowed to do | |
1847 | * the simpler "current->state = TASK_RUNNING" to mark yourself | |
1848 | * runnable without the overhead of this. | |
1849 | * | |
e69f6186 | 1850 | * Return: %true if @p was woken up, %false if it was already running. |
9ed3811a | 1851 | * or @state didn't match @p's state. |
1da177e4 | 1852 | */ |
e4a52bcb PZ |
1853 | static int |
1854 | try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags) | |
1da177e4 | 1855 | { |
1da177e4 | 1856 | unsigned long flags; |
c05fbafb | 1857 | int cpu, success = 0; |
2398f2c6 | 1858 | |
e0acd0a6 ON |
1859 | /* |
1860 | * If we are going to wake up a thread waiting for CONDITION we | |
1861 | * need to ensure that CONDITION=1 done by the caller can not be | |
1862 | * reordered with p->state check below. This pairs with mb() in | |
1863 | * set_current_state() the waiting thread does. | |
1864 | */ | |
1865 | smp_mb__before_spinlock(); | |
013fdb80 | 1866 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
e9c84311 | 1867 | if (!(p->state & state)) |
1da177e4 LT |
1868 | goto out; |
1869 | ||
c05fbafb | 1870 | success = 1; /* we're going to change ->state */ |
1da177e4 | 1871 | cpu = task_cpu(p); |
1da177e4 | 1872 | |
c05fbafb PZ |
1873 | if (p->on_rq && ttwu_remote(p, wake_flags)) |
1874 | goto stat; | |
1da177e4 | 1875 | |
1da177e4 | 1876 | #ifdef CONFIG_SMP |
e9c84311 | 1877 | /* |
c05fbafb PZ |
1878 | * If the owning (remote) cpu is still in the middle of schedule() with |
1879 | * this task as prev, wait until its done referencing the task. | |
e9c84311 | 1880 | */ |
f3e94786 | 1881 | while (p->on_cpu) |
e4a52bcb | 1882 | cpu_relax(); |
0970d299 | 1883 | /* |
e4a52bcb | 1884 | * Pairs with the smp_wmb() in finish_lock_switch(). |
0970d299 | 1885 | */ |
e4a52bcb | 1886 | smp_rmb(); |
1da177e4 | 1887 | |
a8e4f2ea | 1888 | p->sched_contributes_to_load = !!task_contributes_to_load(p); |
e9c84311 | 1889 | p->state = TASK_WAKING; |
e7693a36 | 1890 | |
e4a52bcb | 1891 | if (p->sched_class->task_waking) |
74f8e4b2 | 1892 | p->sched_class->task_waking(p); |
efbbd05a | 1893 | |
ac66f547 | 1894 | cpu = select_task_rq(p, p->wake_cpu, SD_BALANCE_WAKE, wake_flags); |
f339b9dc PZ |
1895 | if (task_cpu(p) != cpu) { |
1896 | wake_flags |= WF_MIGRATED; | |
e4a52bcb | 1897 | set_task_cpu(p, cpu); |
f339b9dc | 1898 | } |
1da177e4 | 1899 | #endif /* CONFIG_SMP */ |
1da177e4 | 1900 | |
c05fbafb PZ |
1901 | ttwu_queue(p, cpu); |
1902 | stat: | |
b84cb5df | 1903 | ttwu_stat(p, cpu, wake_flags); |
1da177e4 | 1904 | out: |
013fdb80 | 1905 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
1da177e4 LT |
1906 | |
1907 | return success; | |
1908 | } | |
1909 | ||
21aa9af0 TH |
1910 | /** |
1911 | * try_to_wake_up_local - try to wake up a local task with rq lock held | |
1912 | * @p: the thread to be awakened | |
1913 | * | |
2acca55e | 1914 | * Put @p on the run-queue if it's not already there. The caller must |
21aa9af0 | 1915 | * ensure that this_rq() is locked, @p is bound to this_rq() and not |
2acca55e | 1916 | * the current task. |
21aa9af0 TH |
1917 | */ |
1918 | static void try_to_wake_up_local(struct task_struct *p) | |
1919 | { | |
1920 | struct rq *rq = task_rq(p); | |
21aa9af0 | 1921 | |
383efcd0 TH |
1922 | if (WARN_ON_ONCE(rq != this_rq()) || |
1923 | WARN_ON_ONCE(p == current)) | |
1924 | return; | |
1925 | ||
21aa9af0 TH |
1926 | lockdep_assert_held(&rq->lock); |
1927 | ||
2acca55e PZ |
1928 | if (!raw_spin_trylock(&p->pi_lock)) { |
1929 | raw_spin_unlock(&rq->lock); | |
1930 | raw_spin_lock(&p->pi_lock); | |
1931 | raw_spin_lock(&rq->lock); | |
1932 | } | |
1933 | ||
21aa9af0 | 1934 | if (!(p->state & TASK_NORMAL)) |
2acca55e | 1935 | goto out; |
21aa9af0 | 1936 | |
da0c1e65 | 1937 | if (!task_on_rq_queued(p)) |
d7c01d27 PZ |
1938 | ttwu_activate(rq, p, ENQUEUE_WAKEUP); |
1939 | ||
23f41eeb | 1940 | ttwu_do_wakeup(rq, p, 0); |
b84cb5df | 1941 | ttwu_stat(p, smp_processor_id(), 0); |
2acca55e PZ |
1942 | out: |
1943 | raw_spin_unlock(&p->pi_lock); | |
21aa9af0 TH |
1944 | } |
1945 | ||
50fa610a DH |
1946 | /** |
1947 | * wake_up_process - Wake up a specific process | |
1948 | * @p: The process to be woken up. | |
1949 | * | |
1950 | * Attempt to wake up the nominated process and move it to the set of runnable | |
e69f6186 YB |
1951 | * processes. |
1952 | * | |
1953 | * Return: 1 if the process was woken up, 0 if it was already running. | |
50fa610a DH |
1954 | * |
1955 | * It may be assumed that this function implies a write memory barrier before | |
1956 | * changing the task state if and only if any tasks are woken up. | |
1957 | */ | |
7ad5b3a5 | 1958 | int wake_up_process(struct task_struct *p) |
1da177e4 | 1959 | { |
9067ac85 ON |
1960 | WARN_ON(task_is_stopped_or_traced(p)); |
1961 | return try_to_wake_up(p, TASK_NORMAL, 0); | |
1da177e4 | 1962 | } |
1da177e4 LT |
1963 | EXPORT_SYMBOL(wake_up_process); |
1964 | ||
7ad5b3a5 | 1965 | int wake_up_state(struct task_struct *p, unsigned int state) |
1da177e4 LT |
1966 | { |
1967 | return try_to_wake_up(p, state, 0); | |
1968 | } | |
1969 | ||
a5e7be3b JL |
1970 | /* |
1971 | * This function clears the sched_dl_entity static params. | |
1972 | */ | |
1973 | void __dl_clear_params(struct task_struct *p) | |
1974 | { | |
1975 | struct sched_dl_entity *dl_se = &p->dl; | |
1976 | ||
1977 | dl_se->dl_runtime = 0; | |
1978 | dl_se->dl_deadline = 0; | |
1979 | dl_se->dl_period = 0; | |
1980 | dl_se->flags = 0; | |
1981 | dl_se->dl_bw = 0; | |
40767b0d PZ |
1982 | |
1983 | dl_se->dl_throttled = 0; | |
1984 | dl_se->dl_new = 1; | |
1985 | dl_se->dl_yielded = 0; | |
a5e7be3b JL |
1986 | } |
1987 | ||
1da177e4 LT |
1988 | /* |
1989 | * Perform scheduler related setup for a newly forked process p. | |
1990 | * p is forked by current. | |
dd41f596 IM |
1991 | * |
1992 | * __sched_fork() is basic setup used by init_idle() too: | |
1993 | */ | |
5e1576ed | 1994 | static void __sched_fork(unsigned long clone_flags, struct task_struct *p) |
dd41f596 | 1995 | { |
fd2f4419 PZ |
1996 | p->on_rq = 0; |
1997 | ||
1998 | p->se.on_rq = 0; | |
dd41f596 IM |
1999 | p->se.exec_start = 0; |
2000 | p->se.sum_exec_runtime = 0; | |
f6cf891c | 2001 | p->se.prev_sum_exec_runtime = 0; |
6c594c21 | 2002 | p->se.nr_migrations = 0; |
da7a735e | 2003 | p->se.vruntime = 0; |
bb04159d KT |
2004 | #ifdef CONFIG_SMP |
2005 | p->se.avg.decay_count = 0; | |
2006 | #endif | |
fd2f4419 | 2007 | INIT_LIST_HEAD(&p->se.group_node); |
6cfb0d5d IM |
2008 | |
2009 | #ifdef CONFIG_SCHEDSTATS | |
41acab88 | 2010 | memset(&p->se.statistics, 0, sizeof(p->se.statistics)); |
6cfb0d5d | 2011 | #endif |
476d139c | 2012 | |
aab03e05 | 2013 | RB_CLEAR_NODE(&p->dl.rb_node); |
40767b0d | 2014 | init_dl_task_timer(&p->dl); |
a5e7be3b | 2015 | __dl_clear_params(p); |
aab03e05 | 2016 | |
fa717060 | 2017 | INIT_LIST_HEAD(&p->rt.run_list); |
476d139c | 2018 | |
e107be36 AK |
2019 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
2020 | INIT_HLIST_HEAD(&p->preempt_notifiers); | |
2021 | #endif | |
cbee9f88 PZ |
2022 | |
2023 | #ifdef CONFIG_NUMA_BALANCING | |
2024 | if (p->mm && atomic_read(&p->mm->mm_users) == 1) { | |
7e8d16b6 | 2025 | p->mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay); |
cbee9f88 PZ |
2026 | p->mm->numa_scan_seq = 0; |
2027 | } | |
2028 | ||
5e1576ed RR |
2029 | if (clone_flags & CLONE_VM) |
2030 | p->numa_preferred_nid = current->numa_preferred_nid; | |
2031 | else | |
2032 | p->numa_preferred_nid = -1; | |
2033 | ||
cbee9f88 PZ |
2034 | p->node_stamp = 0ULL; |
2035 | p->numa_scan_seq = p->mm ? p->mm->numa_scan_seq : 0; | |
4b96a29b | 2036 | p->numa_scan_period = sysctl_numa_balancing_scan_delay; |
cbee9f88 | 2037 | p->numa_work.next = &p->numa_work; |
44dba3d5 | 2038 | p->numa_faults = NULL; |
7e2703e6 RR |
2039 | p->last_task_numa_placement = 0; |
2040 | p->last_sum_exec_runtime = 0; | |
8c8a743c | 2041 | |
8c8a743c | 2042 | p->numa_group = NULL; |
cbee9f88 | 2043 | #endif /* CONFIG_NUMA_BALANCING */ |
dd41f596 IM |
2044 | } |
2045 | ||
1a687c2e | 2046 | #ifdef CONFIG_NUMA_BALANCING |
3105b86a | 2047 | #ifdef CONFIG_SCHED_DEBUG |
1a687c2e MG |
2048 | void set_numabalancing_state(bool enabled) |
2049 | { | |
2050 | if (enabled) | |
2051 | sched_feat_set("NUMA"); | |
2052 | else | |
2053 | sched_feat_set("NO_NUMA"); | |
2054 | } | |
3105b86a MG |
2055 | #else |
2056 | __read_mostly bool numabalancing_enabled; | |
2057 | ||
2058 | void set_numabalancing_state(bool enabled) | |
2059 | { | |
2060 | numabalancing_enabled = enabled; | |
dd41f596 | 2061 | } |
3105b86a | 2062 | #endif /* CONFIG_SCHED_DEBUG */ |
54a43d54 AK |
2063 | |
2064 | #ifdef CONFIG_PROC_SYSCTL | |
2065 | int sysctl_numa_balancing(struct ctl_table *table, int write, | |
2066 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
2067 | { | |
2068 | struct ctl_table t; | |
2069 | int err; | |
2070 | int state = numabalancing_enabled; | |
2071 | ||
2072 | if (write && !capable(CAP_SYS_ADMIN)) | |
2073 | return -EPERM; | |
2074 | ||
2075 | t = *table; | |
2076 | t.data = &state; | |
2077 | err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos); | |
2078 | if (err < 0) | |
2079 | return err; | |
2080 | if (write) | |
2081 | set_numabalancing_state(state); | |
2082 | return err; | |
2083 | } | |
2084 | #endif | |
2085 | #endif | |
dd41f596 IM |
2086 | |
2087 | /* | |
2088 | * fork()/clone()-time setup: | |
2089 | */ | |
aab03e05 | 2090 | int sched_fork(unsigned long clone_flags, struct task_struct *p) |
dd41f596 | 2091 | { |
0122ec5b | 2092 | unsigned long flags; |
dd41f596 IM |
2093 | int cpu = get_cpu(); |
2094 | ||
5e1576ed | 2095 | __sched_fork(clone_flags, p); |
06b83b5f | 2096 | /* |
0017d735 | 2097 | * We mark the process as running here. This guarantees that |
06b83b5f PZ |
2098 | * nobody will actually run it, and a signal or other external |
2099 | * event cannot wake it up and insert it on the runqueue either. | |
2100 | */ | |
0017d735 | 2101 | p->state = TASK_RUNNING; |
dd41f596 | 2102 | |
c350a04e MG |
2103 | /* |
2104 | * Make sure we do not leak PI boosting priority to the child. | |
2105 | */ | |
2106 | p->prio = current->normal_prio; | |
2107 | ||
b9dc29e7 MG |
2108 | /* |
2109 | * Revert to default priority/policy on fork if requested. | |
2110 | */ | |
2111 | if (unlikely(p->sched_reset_on_fork)) { | |
aab03e05 | 2112 | if (task_has_dl_policy(p) || task_has_rt_policy(p)) { |
b9dc29e7 | 2113 | p->policy = SCHED_NORMAL; |
6c697bdf | 2114 | p->static_prio = NICE_TO_PRIO(0); |
c350a04e MG |
2115 | p->rt_priority = 0; |
2116 | } else if (PRIO_TO_NICE(p->static_prio) < 0) | |
2117 | p->static_prio = NICE_TO_PRIO(0); | |
2118 | ||
2119 | p->prio = p->normal_prio = __normal_prio(p); | |
2120 | set_load_weight(p); | |
6c697bdf | 2121 | |
b9dc29e7 MG |
2122 | /* |
2123 | * We don't need the reset flag anymore after the fork. It has | |
2124 | * fulfilled its duty: | |
2125 | */ | |
2126 | p->sched_reset_on_fork = 0; | |
2127 | } | |
ca94c442 | 2128 | |
aab03e05 DF |
2129 | if (dl_prio(p->prio)) { |
2130 | put_cpu(); | |
2131 | return -EAGAIN; | |
2132 | } else if (rt_prio(p->prio)) { | |
2133 | p->sched_class = &rt_sched_class; | |
2134 | } else { | |
2ddbf952 | 2135 | p->sched_class = &fair_sched_class; |
aab03e05 | 2136 | } |
b29739f9 | 2137 | |
cd29fe6f PZ |
2138 | if (p->sched_class->task_fork) |
2139 | p->sched_class->task_fork(p); | |
2140 | ||
86951599 PZ |
2141 | /* |
2142 | * The child is not yet in the pid-hash so no cgroup attach races, | |
2143 | * and the cgroup is pinned to this child due to cgroup_fork() | |
2144 | * is ran before sched_fork(). | |
2145 | * | |
2146 | * Silence PROVE_RCU. | |
2147 | */ | |
0122ec5b | 2148 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
5f3edc1b | 2149 | set_task_cpu(p, cpu); |
0122ec5b | 2150 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
5f3edc1b | 2151 | |
52f17b6c | 2152 | #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT) |
dd41f596 | 2153 | if (likely(sched_info_on())) |
52f17b6c | 2154 | memset(&p->sched_info, 0, sizeof(p->sched_info)); |
1da177e4 | 2155 | #endif |
3ca7a440 PZ |
2156 | #if defined(CONFIG_SMP) |
2157 | p->on_cpu = 0; | |
4866cde0 | 2158 | #endif |
01028747 | 2159 | init_task_preempt_count(p); |
806c09a7 | 2160 | #ifdef CONFIG_SMP |
917b627d | 2161 | plist_node_init(&p->pushable_tasks, MAX_PRIO); |
1baca4ce | 2162 | RB_CLEAR_NODE(&p->pushable_dl_tasks); |
806c09a7 | 2163 | #endif |
917b627d | 2164 | |
476d139c | 2165 | put_cpu(); |
aab03e05 | 2166 | return 0; |
1da177e4 LT |
2167 | } |
2168 | ||
332ac17e DF |
2169 | unsigned long to_ratio(u64 period, u64 runtime) |
2170 | { | |
2171 | if (runtime == RUNTIME_INF) | |
2172 | return 1ULL << 20; | |
2173 | ||
2174 | /* | |
2175 | * Doing this here saves a lot of checks in all | |
2176 | * the calling paths, and returning zero seems | |
2177 | * safe for them anyway. | |
2178 | */ | |
2179 | if (period == 0) | |
2180 | return 0; | |
2181 | ||
2182 | return div64_u64(runtime << 20, period); | |
2183 | } | |
2184 | ||
2185 | #ifdef CONFIG_SMP | |
2186 | inline struct dl_bw *dl_bw_of(int i) | |
2187 | { | |
66339c31 KT |
2188 | rcu_lockdep_assert(rcu_read_lock_sched_held(), |
2189 | "sched RCU must be held"); | |
332ac17e DF |
2190 | return &cpu_rq(i)->rd->dl_bw; |
2191 | } | |
2192 | ||
de212f18 | 2193 | static inline int dl_bw_cpus(int i) |
332ac17e | 2194 | { |
de212f18 PZ |
2195 | struct root_domain *rd = cpu_rq(i)->rd; |
2196 | int cpus = 0; | |
2197 | ||
66339c31 KT |
2198 | rcu_lockdep_assert(rcu_read_lock_sched_held(), |
2199 | "sched RCU must be held"); | |
de212f18 PZ |
2200 | for_each_cpu_and(i, rd->span, cpu_active_mask) |
2201 | cpus++; | |
2202 | ||
2203 | return cpus; | |
332ac17e DF |
2204 | } |
2205 | #else | |
2206 | inline struct dl_bw *dl_bw_of(int i) | |
2207 | { | |
2208 | return &cpu_rq(i)->dl.dl_bw; | |
2209 | } | |
2210 | ||
de212f18 | 2211 | static inline int dl_bw_cpus(int i) |
332ac17e DF |
2212 | { |
2213 | return 1; | |
2214 | } | |
2215 | #endif | |
2216 | ||
332ac17e DF |
2217 | /* |
2218 | * We must be sure that accepting a new task (or allowing changing the | |
2219 | * parameters of an existing one) is consistent with the bandwidth | |
2220 | * constraints. If yes, this function also accordingly updates the currently | |
2221 | * allocated bandwidth to reflect the new situation. | |
2222 | * | |
2223 | * This function is called while holding p's rq->lock. | |
40767b0d PZ |
2224 | * |
2225 | * XXX we should delay bw change until the task's 0-lag point, see | |
2226 | * __setparam_dl(). | |
332ac17e DF |
2227 | */ |
2228 | static int dl_overflow(struct task_struct *p, int policy, | |
2229 | const struct sched_attr *attr) | |
2230 | { | |
2231 | ||
2232 | struct dl_bw *dl_b = dl_bw_of(task_cpu(p)); | |
4df1638c | 2233 | u64 period = attr->sched_period ?: attr->sched_deadline; |
332ac17e DF |
2234 | u64 runtime = attr->sched_runtime; |
2235 | u64 new_bw = dl_policy(policy) ? to_ratio(period, runtime) : 0; | |
de212f18 | 2236 | int cpus, err = -1; |
332ac17e DF |
2237 | |
2238 | if (new_bw == p->dl.dl_bw) | |
2239 | return 0; | |
2240 | ||
2241 | /* | |
2242 | * Either if a task, enters, leave, or stays -deadline but changes | |
2243 | * its parameters, we may need to update accordingly the total | |
2244 | * allocated bandwidth of the container. | |
2245 | */ | |
2246 | raw_spin_lock(&dl_b->lock); | |
de212f18 | 2247 | cpus = dl_bw_cpus(task_cpu(p)); |
332ac17e DF |
2248 | if (dl_policy(policy) && !task_has_dl_policy(p) && |
2249 | !__dl_overflow(dl_b, cpus, 0, new_bw)) { | |
2250 | __dl_add(dl_b, new_bw); | |
2251 | err = 0; | |
2252 | } else if (dl_policy(policy) && task_has_dl_policy(p) && | |
2253 | !__dl_overflow(dl_b, cpus, p->dl.dl_bw, new_bw)) { | |
2254 | __dl_clear(dl_b, p->dl.dl_bw); | |
2255 | __dl_add(dl_b, new_bw); | |
2256 | err = 0; | |
2257 | } else if (!dl_policy(policy) && task_has_dl_policy(p)) { | |
2258 | __dl_clear(dl_b, p->dl.dl_bw); | |
2259 | err = 0; | |
2260 | } | |
2261 | raw_spin_unlock(&dl_b->lock); | |
2262 | ||
2263 | return err; | |
2264 | } | |
2265 | ||
2266 | extern void init_dl_bw(struct dl_bw *dl_b); | |
2267 | ||
1da177e4 LT |
2268 | /* |
2269 | * wake_up_new_task - wake up a newly created task for the first time. | |
2270 | * | |
2271 | * This function will do some initial scheduler statistics housekeeping | |
2272 | * that must be done for every newly created context, then puts the task | |
2273 | * on the runqueue and wakes it. | |
2274 | */ | |
3e51e3ed | 2275 | void wake_up_new_task(struct task_struct *p) |
1da177e4 LT |
2276 | { |
2277 | unsigned long flags; | |
dd41f596 | 2278 | struct rq *rq; |
fabf318e | 2279 | |
ab2515c4 | 2280 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
fabf318e PZ |
2281 | #ifdef CONFIG_SMP |
2282 | /* | |
2283 | * Fork balancing, do it here and not earlier because: | |
2284 | * - cpus_allowed can change in the fork path | |
2285 | * - any previously selected cpu might disappear through hotplug | |
fabf318e | 2286 | */ |
ac66f547 | 2287 | set_task_cpu(p, select_task_rq(p, task_cpu(p), SD_BALANCE_FORK, 0)); |
0017d735 PZ |
2288 | #endif |
2289 | ||
a75cdaa9 AS |
2290 | /* Initialize new task's runnable average */ |
2291 | init_task_runnable_average(p); | |
ab2515c4 | 2292 | rq = __task_rq_lock(p); |
cd29fe6f | 2293 | activate_task(rq, p, 0); |
da0c1e65 | 2294 | p->on_rq = TASK_ON_RQ_QUEUED; |
89363381 | 2295 | trace_sched_wakeup_new(p, true); |
a7558e01 | 2296 | check_preempt_curr(rq, p, WF_FORK); |
9a897c5a | 2297 | #ifdef CONFIG_SMP |
efbbd05a PZ |
2298 | if (p->sched_class->task_woken) |
2299 | p->sched_class->task_woken(rq, p); | |
9a897c5a | 2300 | #endif |
0122ec5b | 2301 | task_rq_unlock(rq, p, &flags); |
1da177e4 LT |
2302 | } |
2303 | ||
e107be36 AK |
2304 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
2305 | ||
2306 | /** | |
80dd99b3 | 2307 | * preempt_notifier_register - tell me when current is being preempted & rescheduled |
421cee29 | 2308 | * @notifier: notifier struct to register |
e107be36 AK |
2309 | */ |
2310 | void preempt_notifier_register(struct preempt_notifier *notifier) | |
2311 | { | |
2312 | hlist_add_head(¬ifier->link, ¤t->preempt_notifiers); | |
2313 | } | |
2314 | EXPORT_SYMBOL_GPL(preempt_notifier_register); | |
2315 | ||
2316 | /** | |
2317 | * preempt_notifier_unregister - no longer interested in preemption notifications | |
421cee29 | 2318 | * @notifier: notifier struct to unregister |
e107be36 AK |
2319 | * |
2320 | * This is safe to call from within a preemption notifier. | |
2321 | */ | |
2322 | void preempt_notifier_unregister(struct preempt_notifier *notifier) | |
2323 | { | |
2324 | hlist_del(¬ifier->link); | |
2325 | } | |
2326 | EXPORT_SYMBOL_GPL(preempt_notifier_unregister); | |
2327 | ||
2328 | static void fire_sched_in_preempt_notifiers(struct task_struct *curr) | |
2329 | { | |
2330 | struct preempt_notifier *notifier; | |
e107be36 | 2331 | |
b67bfe0d | 2332 | hlist_for_each_entry(notifier, &curr->preempt_notifiers, link) |
e107be36 AK |
2333 | notifier->ops->sched_in(notifier, raw_smp_processor_id()); |
2334 | } | |
2335 | ||
2336 | static void | |
2337 | fire_sched_out_preempt_notifiers(struct task_struct *curr, | |
2338 | struct task_struct *next) | |
2339 | { | |
2340 | struct preempt_notifier *notifier; | |
e107be36 | 2341 | |
b67bfe0d | 2342 | hlist_for_each_entry(notifier, &curr->preempt_notifiers, link) |
e107be36 AK |
2343 | notifier->ops->sched_out(notifier, next); |
2344 | } | |
2345 | ||
6d6bc0ad | 2346 | #else /* !CONFIG_PREEMPT_NOTIFIERS */ |
e107be36 AK |
2347 | |
2348 | static void fire_sched_in_preempt_notifiers(struct task_struct *curr) | |
2349 | { | |
2350 | } | |
2351 | ||
2352 | static void | |
2353 | fire_sched_out_preempt_notifiers(struct task_struct *curr, | |
2354 | struct task_struct *next) | |
2355 | { | |
2356 | } | |
2357 | ||
6d6bc0ad | 2358 | #endif /* CONFIG_PREEMPT_NOTIFIERS */ |
e107be36 | 2359 | |
4866cde0 NP |
2360 | /** |
2361 | * prepare_task_switch - prepare to switch tasks | |
2362 | * @rq: the runqueue preparing to switch | |
421cee29 | 2363 | * @prev: the current task that is being switched out |
4866cde0 NP |
2364 | * @next: the task we are going to switch to. |
2365 | * | |
2366 | * This is called with the rq lock held and interrupts off. It must | |
2367 | * be paired with a subsequent finish_task_switch after the context | |
2368 | * switch. | |
2369 | * | |
2370 | * prepare_task_switch sets up locking and calls architecture specific | |
2371 | * hooks. | |
2372 | */ | |
e107be36 AK |
2373 | static inline void |
2374 | prepare_task_switch(struct rq *rq, struct task_struct *prev, | |
2375 | struct task_struct *next) | |
4866cde0 | 2376 | { |
895dd92c | 2377 | trace_sched_switch(prev, next); |
43148951 | 2378 | sched_info_switch(rq, prev, next); |
fe4b04fa | 2379 | perf_event_task_sched_out(prev, next); |
e107be36 | 2380 | fire_sched_out_preempt_notifiers(prev, next); |
4866cde0 NP |
2381 | prepare_lock_switch(rq, next); |
2382 | prepare_arch_switch(next); | |
2383 | } | |
2384 | ||
1da177e4 LT |
2385 | /** |
2386 | * finish_task_switch - clean up after a task-switch | |
2387 | * @prev: the thread we just switched away from. | |
2388 | * | |
4866cde0 NP |
2389 | * finish_task_switch must be called after the context switch, paired |
2390 | * with a prepare_task_switch call before the context switch. | |
2391 | * finish_task_switch will reconcile locking set up by prepare_task_switch, | |
2392 | * and do any other architecture-specific cleanup actions. | |
1da177e4 LT |
2393 | * |
2394 | * Note that we may have delayed dropping an mm in context_switch(). If | |
41a2d6cf | 2395 | * so, we finish that here outside of the runqueue lock. (Doing it |
1da177e4 LT |
2396 | * with the lock held can cause deadlocks; see schedule() for |
2397 | * details.) | |
dfa50b60 ON |
2398 | * |
2399 | * The context switch have flipped the stack from under us and restored the | |
2400 | * local variables which were saved when this task called schedule() in the | |
2401 | * past. prev == current is still correct but we need to recalculate this_rq | |
2402 | * because prev may have moved to another CPU. | |
1da177e4 | 2403 | */ |
dfa50b60 | 2404 | static struct rq *finish_task_switch(struct task_struct *prev) |
1da177e4 LT |
2405 | __releases(rq->lock) |
2406 | { | |
dfa50b60 | 2407 | struct rq *rq = this_rq(); |
1da177e4 | 2408 | struct mm_struct *mm = rq->prev_mm; |
55a101f8 | 2409 | long prev_state; |
1da177e4 LT |
2410 | |
2411 | rq->prev_mm = NULL; | |
2412 | ||
2413 | /* | |
2414 | * A task struct has one reference for the use as "current". | |
c394cc9f | 2415 | * If a task dies, then it sets TASK_DEAD in tsk->state and calls |
55a101f8 ON |
2416 | * schedule one last time. The schedule call will never return, and |
2417 | * the scheduled task must drop that reference. | |
c394cc9f | 2418 | * The test for TASK_DEAD must occur while the runqueue locks are |
1da177e4 LT |
2419 | * still held, otherwise prev could be scheduled on another cpu, die |
2420 | * there before we look at prev->state, and then the reference would | |
2421 | * be dropped twice. | |
2422 | * Manfred Spraul <manfred@colorfullife.com> | |
2423 | */ | |
55a101f8 | 2424 | prev_state = prev->state; |
bf9fae9f | 2425 | vtime_task_switch(prev); |
4866cde0 | 2426 | finish_arch_switch(prev); |
a8d757ef | 2427 | perf_event_task_sched_in(prev, current); |
4866cde0 | 2428 | finish_lock_switch(rq, prev); |
01f23e16 | 2429 | finish_arch_post_lock_switch(); |
e8fa1362 | 2430 | |
e107be36 | 2431 | fire_sched_in_preempt_notifiers(current); |
1da177e4 LT |
2432 | if (mm) |
2433 | mmdrop(mm); | |
c394cc9f | 2434 | if (unlikely(prev_state == TASK_DEAD)) { |
e6c390f2 DF |
2435 | if (prev->sched_class->task_dead) |
2436 | prev->sched_class->task_dead(prev); | |
2437 | ||
c6fd91f0 | 2438 | /* |
2439 | * Remove function-return probe instances associated with this | |
2440 | * task and put them back on the free list. | |
9761eea8 | 2441 | */ |
c6fd91f0 | 2442 | kprobe_flush_task(prev); |
1da177e4 | 2443 | put_task_struct(prev); |
c6fd91f0 | 2444 | } |
99e5ada9 FW |
2445 | |
2446 | tick_nohz_task_switch(current); | |
dfa50b60 | 2447 | return rq; |
1da177e4 LT |
2448 | } |
2449 | ||
3f029d3c GH |
2450 | #ifdef CONFIG_SMP |
2451 | ||
3f029d3c | 2452 | /* rq->lock is NOT held, but preemption is disabled */ |
e3fca9e7 | 2453 | static void __balance_callback(struct rq *rq) |
3f029d3c | 2454 | { |
e3fca9e7 PZ |
2455 | struct callback_head *head, *next; |
2456 | void (*func)(struct rq *rq); | |
2457 | unsigned long flags; | |
3f029d3c | 2458 | |
e3fca9e7 PZ |
2459 | raw_spin_lock_irqsave(&rq->lock, flags); |
2460 | head = rq->balance_callback; | |
2461 | rq->balance_callback = NULL; | |
2462 | while (head) { | |
2463 | func = (void (*)(struct rq *))head->func; | |
2464 | next = head->next; | |
2465 | head->next = NULL; | |
2466 | head = next; | |
3f029d3c | 2467 | |
e3fca9e7 | 2468 | func(rq); |
3f029d3c | 2469 | } |
e3fca9e7 PZ |
2470 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
2471 | } | |
2472 | ||
2473 | static inline void balance_callback(struct rq *rq) | |
2474 | { | |
2475 | if (unlikely(rq->balance_callback)) | |
2476 | __balance_callback(rq); | |
3f029d3c GH |
2477 | } |
2478 | ||
2479 | #else | |
da19ab51 | 2480 | |
e3fca9e7 | 2481 | static inline void balance_callback(struct rq *rq) |
3f029d3c | 2482 | { |
1da177e4 LT |
2483 | } |
2484 | ||
3f029d3c GH |
2485 | #endif |
2486 | ||
1da177e4 LT |
2487 | /** |
2488 | * schedule_tail - first thing a freshly forked thread must call. | |
2489 | * @prev: the thread we just switched away from. | |
2490 | */ | |
722a9f92 | 2491 | asmlinkage __visible void schedule_tail(struct task_struct *prev) |
1da177e4 LT |
2492 | __releases(rq->lock) |
2493 | { | |
1a43a14a | 2494 | struct rq *rq; |
da19ab51 | 2495 | |
1a43a14a ON |
2496 | /* finish_task_switch() drops rq->lock and enables preemtion */ |
2497 | preempt_disable(); | |
dfa50b60 | 2498 | rq = finish_task_switch(prev); |
e3fca9e7 | 2499 | balance_callback(rq); |
1a43a14a | 2500 | preempt_enable(); |
70b97a7f | 2501 | |
1da177e4 | 2502 | if (current->set_child_tid) |
b488893a | 2503 | put_user(task_pid_vnr(current), current->set_child_tid); |
1da177e4 LT |
2504 | } |
2505 | ||
2506 | /* | |
dfa50b60 | 2507 | * context_switch - switch to the new MM and the new thread's register state. |
1da177e4 | 2508 | */ |
dfa50b60 | 2509 | static inline struct rq * |
70b97a7f | 2510 | context_switch(struct rq *rq, struct task_struct *prev, |
36c8b586 | 2511 | struct task_struct *next) |
1da177e4 | 2512 | { |
dd41f596 | 2513 | struct mm_struct *mm, *oldmm; |
1da177e4 | 2514 | |
e107be36 | 2515 | prepare_task_switch(rq, prev, next); |
fe4b04fa | 2516 | |
dd41f596 IM |
2517 | mm = next->mm; |
2518 | oldmm = prev->active_mm; | |
9226d125 ZA |
2519 | /* |
2520 | * For paravirt, this is coupled with an exit in switch_to to | |
2521 | * combine the page table reload and the switch backend into | |
2522 | * one hypercall. | |
2523 | */ | |
224101ed | 2524 | arch_start_context_switch(prev); |
9226d125 | 2525 | |
31915ab4 | 2526 | if (!mm) { |
1da177e4 LT |
2527 | next->active_mm = oldmm; |
2528 | atomic_inc(&oldmm->mm_count); | |
2529 | enter_lazy_tlb(oldmm, next); | |
2530 | } else | |
2531 | switch_mm(oldmm, mm, next); | |
2532 | ||
31915ab4 | 2533 | if (!prev->mm) { |
1da177e4 | 2534 | prev->active_mm = NULL; |
1da177e4 LT |
2535 | rq->prev_mm = oldmm; |
2536 | } | |
3a5f5e48 IM |
2537 | /* |
2538 | * Since the runqueue lock will be released by the next | |
2539 | * task (which is an invalid locking op but in the case | |
2540 | * of the scheduler it's an obvious special-case), so we | |
2541 | * do an early lockdep release here: | |
2542 | */ | |
8a25d5de | 2543 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); |
1da177e4 | 2544 | |
91d1aa43 | 2545 | context_tracking_task_switch(prev, next); |
1da177e4 LT |
2546 | /* Here we just switch the register state and the stack. */ |
2547 | switch_to(prev, next, prev); | |
dd41f596 | 2548 | barrier(); |
dfa50b60 ON |
2549 | |
2550 | return finish_task_switch(prev); | |
1da177e4 LT |
2551 | } |
2552 | ||
2553 | /* | |
1c3e8264 | 2554 | * nr_running and nr_context_switches: |
1da177e4 LT |
2555 | * |
2556 | * externally visible scheduler statistics: current number of runnable | |
1c3e8264 | 2557 | * threads, total number of context switches performed since bootup. |
1da177e4 LT |
2558 | */ |
2559 | unsigned long nr_running(void) | |
2560 | { | |
2561 | unsigned long i, sum = 0; | |
2562 | ||
2563 | for_each_online_cpu(i) | |
2564 | sum += cpu_rq(i)->nr_running; | |
2565 | ||
2566 | return sum; | |
f711f609 | 2567 | } |
1da177e4 | 2568 | |
2ee507c4 TC |
2569 | /* |
2570 | * Check if only the current task is running on the cpu. | |
2571 | */ | |
2572 | bool single_task_running(void) | |
2573 | { | |
2574 | if (cpu_rq(smp_processor_id())->nr_running == 1) | |
2575 | return true; | |
2576 | else | |
2577 | return false; | |
2578 | } | |
2579 | EXPORT_SYMBOL(single_task_running); | |
2580 | ||
1da177e4 | 2581 | unsigned long long nr_context_switches(void) |
46cb4b7c | 2582 | { |
cc94abfc SR |
2583 | int i; |
2584 | unsigned long long sum = 0; | |
46cb4b7c | 2585 | |
0a945022 | 2586 | for_each_possible_cpu(i) |
1da177e4 | 2587 | sum += cpu_rq(i)->nr_switches; |
46cb4b7c | 2588 | |
1da177e4 LT |
2589 | return sum; |
2590 | } | |
483b4ee6 | 2591 | |
1da177e4 LT |
2592 | unsigned long nr_iowait(void) |
2593 | { | |
2594 | unsigned long i, sum = 0; | |
483b4ee6 | 2595 | |
0a945022 | 2596 | for_each_possible_cpu(i) |
1da177e4 | 2597 | sum += atomic_read(&cpu_rq(i)->nr_iowait); |
46cb4b7c | 2598 | |
1da177e4 LT |
2599 | return sum; |
2600 | } | |
483b4ee6 | 2601 | |
8c215bd3 | 2602 | unsigned long nr_iowait_cpu(int cpu) |
69d25870 | 2603 | { |
8c215bd3 | 2604 | struct rq *this = cpu_rq(cpu); |
69d25870 AV |
2605 | return atomic_read(&this->nr_iowait); |
2606 | } | |
46cb4b7c | 2607 | |
372ba8cb MG |
2608 | void get_iowait_load(unsigned long *nr_waiters, unsigned long *load) |
2609 | { | |
3289bdb4 PZ |
2610 | struct rq *rq = this_rq(); |
2611 | *nr_waiters = atomic_read(&rq->nr_iowait); | |
2612 | *load = rq->load.weight; | |
372ba8cb MG |
2613 | } |
2614 | ||
dd41f596 | 2615 | #ifdef CONFIG_SMP |
8a0be9ef | 2616 | |
46cb4b7c | 2617 | /* |
38022906 PZ |
2618 | * sched_exec - execve() is a valuable balancing opportunity, because at |
2619 | * this point the task has the smallest effective memory and cache footprint. | |
46cb4b7c | 2620 | */ |
38022906 | 2621 | void sched_exec(void) |
46cb4b7c | 2622 | { |
38022906 | 2623 | struct task_struct *p = current; |
1da177e4 | 2624 | unsigned long flags; |
0017d735 | 2625 | int dest_cpu; |
46cb4b7c | 2626 | |
8f42ced9 | 2627 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
ac66f547 | 2628 | dest_cpu = p->sched_class->select_task_rq(p, task_cpu(p), SD_BALANCE_EXEC, 0); |
0017d735 PZ |
2629 | if (dest_cpu == smp_processor_id()) |
2630 | goto unlock; | |
38022906 | 2631 | |
8f42ced9 | 2632 | if (likely(cpu_active(dest_cpu))) { |
969c7921 | 2633 | struct migration_arg arg = { p, dest_cpu }; |
46cb4b7c | 2634 | |
8f42ced9 PZ |
2635 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
2636 | stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg); | |
1da177e4 LT |
2637 | return; |
2638 | } | |
0017d735 | 2639 | unlock: |
8f42ced9 | 2640 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
1da177e4 | 2641 | } |
dd41f596 | 2642 | |
1da177e4 LT |
2643 | #endif |
2644 | ||
1da177e4 | 2645 | DEFINE_PER_CPU(struct kernel_stat, kstat); |
3292beb3 | 2646 | DEFINE_PER_CPU(struct kernel_cpustat, kernel_cpustat); |
1da177e4 LT |
2647 | |
2648 | EXPORT_PER_CPU_SYMBOL(kstat); | |
3292beb3 | 2649 | EXPORT_PER_CPU_SYMBOL(kernel_cpustat); |
1da177e4 | 2650 | |
c5f8d995 HS |
2651 | /* |
2652 | * Return accounted runtime for the task. | |
2653 | * In case the task is currently running, return the runtime plus current's | |
2654 | * pending runtime that have not been accounted yet. | |
2655 | */ | |
2656 | unsigned long long task_sched_runtime(struct task_struct *p) | |
2657 | { | |
2658 | unsigned long flags; | |
2659 | struct rq *rq; | |
6e998916 | 2660 | u64 ns; |
c5f8d995 | 2661 | |
911b2898 PZ |
2662 | #if defined(CONFIG_64BIT) && defined(CONFIG_SMP) |
2663 | /* | |
2664 | * 64-bit doesn't need locks to atomically read a 64bit value. | |
2665 | * So we have a optimization chance when the task's delta_exec is 0. | |
2666 | * Reading ->on_cpu is racy, but this is ok. | |
2667 | * | |
2668 | * If we race with it leaving cpu, we'll take a lock. So we're correct. | |
2669 | * If we race with it entering cpu, unaccounted time is 0. This is | |
2670 | * indistinguishable from the read occurring a few cycles earlier. | |
4036ac15 MG |
2671 | * If we see ->on_cpu without ->on_rq, the task is leaving, and has |
2672 | * been accounted, so we're correct here as well. | |
911b2898 | 2673 | */ |
da0c1e65 | 2674 | if (!p->on_cpu || !task_on_rq_queued(p)) |
911b2898 PZ |
2675 | return p->se.sum_exec_runtime; |
2676 | #endif | |
2677 | ||
c5f8d995 | 2678 | rq = task_rq_lock(p, &flags); |
6e998916 SG |
2679 | /* |
2680 | * Must be ->curr _and_ ->on_rq. If dequeued, we would | |
2681 | * project cycles that may never be accounted to this | |
2682 | * thread, breaking clock_gettime(). | |
2683 | */ | |
2684 | if (task_current(rq, p) && task_on_rq_queued(p)) { | |
2685 | update_rq_clock(rq); | |
2686 | p->sched_class->update_curr(rq); | |
2687 | } | |
2688 | ns = p->se.sum_exec_runtime; | |
0122ec5b | 2689 | task_rq_unlock(rq, p, &flags); |
c5f8d995 HS |
2690 | |
2691 | return ns; | |
2692 | } | |
48f24c4d | 2693 | |
7835b98b CL |
2694 | /* |
2695 | * This function gets called by the timer code, with HZ frequency. | |
2696 | * We call it with interrupts disabled. | |
7835b98b CL |
2697 | */ |
2698 | void scheduler_tick(void) | |
2699 | { | |
7835b98b CL |
2700 | int cpu = smp_processor_id(); |
2701 | struct rq *rq = cpu_rq(cpu); | |
dd41f596 | 2702 | struct task_struct *curr = rq->curr; |
3e51f33f PZ |
2703 | |
2704 | sched_clock_tick(); | |
dd41f596 | 2705 | |
05fa785c | 2706 | raw_spin_lock(&rq->lock); |
3e51f33f | 2707 | update_rq_clock(rq); |
fa85ae24 | 2708 | curr->sched_class->task_tick(rq, curr, 0); |
83dfd523 | 2709 | update_cpu_load_active(rq); |
3289bdb4 | 2710 | calc_global_load_tick(rq); |
05fa785c | 2711 | raw_spin_unlock(&rq->lock); |
7835b98b | 2712 | |
e9d2b064 | 2713 | perf_event_task_tick(); |
e220d2dc | 2714 | |
e418e1c2 | 2715 | #ifdef CONFIG_SMP |
6eb57e0d | 2716 | rq->idle_balance = idle_cpu(cpu); |
7caff66f | 2717 | trigger_load_balance(rq); |
e418e1c2 | 2718 | #endif |
265f22a9 | 2719 | rq_last_tick_reset(rq); |
1da177e4 LT |
2720 | } |
2721 | ||
265f22a9 FW |
2722 | #ifdef CONFIG_NO_HZ_FULL |
2723 | /** | |
2724 | * scheduler_tick_max_deferment | |
2725 | * | |
2726 | * Keep at least one tick per second when a single | |
2727 | * active task is running because the scheduler doesn't | |
2728 | * yet completely support full dynticks environment. | |
2729 | * | |
2730 | * This makes sure that uptime, CFS vruntime, load | |
2731 | * balancing, etc... continue to move forward, even | |
2732 | * with a very low granularity. | |
e69f6186 YB |
2733 | * |
2734 | * Return: Maximum deferment in nanoseconds. | |
265f22a9 FW |
2735 | */ |
2736 | u64 scheduler_tick_max_deferment(void) | |
2737 | { | |
2738 | struct rq *rq = this_rq(); | |
316c1608 | 2739 | unsigned long next, now = READ_ONCE(jiffies); |
265f22a9 FW |
2740 | |
2741 | next = rq->last_sched_tick + HZ; | |
2742 | ||
2743 | if (time_before_eq(next, now)) | |
2744 | return 0; | |
2745 | ||
8fe8ff09 | 2746 | return jiffies_to_nsecs(next - now); |
1da177e4 | 2747 | } |
265f22a9 | 2748 | #endif |
1da177e4 | 2749 | |
132380a0 | 2750 | notrace unsigned long get_parent_ip(unsigned long addr) |
6cd8a4bb SR |
2751 | { |
2752 | if (in_lock_functions(addr)) { | |
2753 | addr = CALLER_ADDR2; | |
2754 | if (in_lock_functions(addr)) | |
2755 | addr = CALLER_ADDR3; | |
2756 | } | |
2757 | return addr; | |
2758 | } | |
1da177e4 | 2759 | |
7e49fcce SR |
2760 | #if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \ |
2761 | defined(CONFIG_PREEMPT_TRACER)) | |
2762 | ||
edafe3a5 | 2763 | void preempt_count_add(int val) |
1da177e4 | 2764 | { |
6cd8a4bb | 2765 | #ifdef CONFIG_DEBUG_PREEMPT |
1da177e4 LT |
2766 | /* |
2767 | * Underflow? | |
2768 | */ | |
9a11b49a IM |
2769 | if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0))) |
2770 | return; | |
6cd8a4bb | 2771 | #endif |
bdb43806 | 2772 | __preempt_count_add(val); |
6cd8a4bb | 2773 | #ifdef CONFIG_DEBUG_PREEMPT |
1da177e4 LT |
2774 | /* |
2775 | * Spinlock count overflowing soon? | |
2776 | */ | |
33859f7f MOS |
2777 | DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >= |
2778 | PREEMPT_MASK - 10); | |
6cd8a4bb | 2779 | #endif |
8f47b187 TG |
2780 | if (preempt_count() == val) { |
2781 | unsigned long ip = get_parent_ip(CALLER_ADDR1); | |
2782 | #ifdef CONFIG_DEBUG_PREEMPT | |
2783 | current->preempt_disable_ip = ip; | |
2784 | #endif | |
2785 | trace_preempt_off(CALLER_ADDR0, ip); | |
2786 | } | |
1da177e4 | 2787 | } |
bdb43806 | 2788 | EXPORT_SYMBOL(preempt_count_add); |
edafe3a5 | 2789 | NOKPROBE_SYMBOL(preempt_count_add); |
1da177e4 | 2790 | |
edafe3a5 | 2791 | void preempt_count_sub(int val) |
1da177e4 | 2792 | { |
6cd8a4bb | 2793 | #ifdef CONFIG_DEBUG_PREEMPT |
1da177e4 LT |
2794 | /* |
2795 | * Underflow? | |
2796 | */ | |
01e3eb82 | 2797 | if (DEBUG_LOCKS_WARN_ON(val > preempt_count())) |
9a11b49a | 2798 | return; |
1da177e4 LT |
2799 | /* |
2800 | * Is the spinlock portion underflowing? | |
2801 | */ | |
9a11b49a IM |
2802 | if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) && |
2803 | !(preempt_count() & PREEMPT_MASK))) | |
2804 | return; | |
6cd8a4bb | 2805 | #endif |
9a11b49a | 2806 | |
6cd8a4bb SR |
2807 | if (preempt_count() == val) |
2808 | trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1)); | |
bdb43806 | 2809 | __preempt_count_sub(val); |
1da177e4 | 2810 | } |
bdb43806 | 2811 | EXPORT_SYMBOL(preempt_count_sub); |
edafe3a5 | 2812 | NOKPROBE_SYMBOL(preempt_count_sub); |
1da177e4 LT |
2813 | |
2814 | #endif | |
2815 | ||
2816 | /* | |
dd41f596 | 2817 | * Print scheduling while atomic bug: |
1da177e4 | 2818 | */ |
dd41f596 | 2819 | static noinline void __schedule_bug(struct task_struct *prev) |
1da177e4 | 2820 | { |
664dfa65 DJ |
2821 | if (oops_in_progress) |
2822 | return; | |
2823 | ||
3df0fc5b PZ |
2824 | printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n", |
2825 | prev->comm, prev->pid, preempt_count()); | |
838225b4 | 2826 | |
dd41f596 | 2827 | debug_show_held_locks(prev); |
e21f5b15 | 2828 | print_modules(); |
dd41f596 IM |
2829 | if (irqs_disabled()) |
2830 | print_irqtrace_events(prev); | |
8f47b187 TG |
2831 | #ifdef CONFIG_DEBUG_PREEMPT |
2832 | if (in_atomic_preempt_off()) { | |
2833 | pr_err("Preemption disabled at:"); | |
2834 | print_ip_sym(current->preempt_disable_ip); | |
2835 | pr_cont("\n"); | |
2836 | } | |
2837 | #endif | |
6135fc1e | 2838 | dump_stack(); |
373d4d09 | 2839 | add_taint(TAINT_WARN, LOCKDEP_STILL_OK); |
dd41f596 | 2840 | } |
1da177e4 | 2841 | |
dd41f596 IM |
2842 | /* |
2843 | * Various schedule()-time debugging checks and statistics: | |
2844 | */ | |
2845 | static inline void schedule_debug(struct task_struct *prev) | |
2846 | { | |
0d9e2632 AT |
2847 | #ifdef CONFIG_SCHED_STACK_END_CHECK |
2848 | BUG_ON(unlikely(task_stack_end_corrupted(prev))); | |
2849 | #endif | |
1da177e4 | 2850 | /* |
41a2d6cf | 2851 | * Test if we are atomic. Since do_exit() needs to call into |
192301e7 ON |
2852 | * schedule() atomically, we ignore that path. Otherwise whine |
2853 | * if we are scheduling when we should not. | |
1da177e4 | 2854 | */ |
192301e7 | 2855 | if (unlikely(in_atomic_preempt_off() && prev->state != TASK_DEAD)) |
dd41f596 | 2856 | __schedule_bug(prev); |
b3fbab05 | 2857 | rcu_sleep_check(); |
dd41f596 | 2858 | |
1da177e4 LT |
2859 | profile_hit(SCHED_PROFILING, __builtin_return_address(0)); |
2860 | ||
2d72376b | 2861 | schedstat_inc(this_rq(), sched_count); |
dd41f596 IM |
2862 | } |
2863 | ||
2864 | /* | |
2865 | * Pick up the highest-prio task: | |
2866 | */ | |
2867 | static inline struct task_struct * | |
606dba2e | 2868 | pick_next_task(struct rq *rq, struct task_struct *prev) |
dd41f596 | 2869 | { |
37e117c0 | 2870 | const struct sched_class *class = &fair_sched_class; |
dd41f596 | 2871 | struct task_struct *p; |
1da177e4 LT |
2872 | |
2873 | /* | |
dd41f596 IM |
2874 | * Optimization: we know that if all tasks are in |
2875 | * the fair class we can call that function directly: | |
1da177e4 | 2876 | */ |
37e117c0 | 2877 | if (likely(prev->sched_class == class && |
38033c37 | 2878 | rq->nr_running == rq->cfs.h_nr_running)) { |
606dba2e | 2879 | p = fair_sched_class.pick_next_task(rq, prev); |
6ccdc84b PZ |
2880 | if (unlikely(p == RETRY_TASK)) |
2881 | goto again; | |
2882 | ||
2883 | /* assumes fair_sched_class->next == idle_sched_class */ | |
2884 | if (unlikely(!p)) | |
2885 | p = idle_sched_class.pick_next_task(rq, prev); | |
2886 | ||
2887 | return p; | |
1da177e4 LT |
2888 | } |
2889 | ||
37e117c0 | 2890 | again: |
34f971f6 | 2891 | for_each_class(class) { |
606dba2e | 2892 | p = class->pick_next_task(rq, prev); |
37e117c0 PZ |
2893 | if (p) { |
2894 | if (unlikely(p == RETRY_TASK)) | |
2895 | goto again; | |
dd41f596 | 2896 | return p; |
37e117c0 | 2897 | } |
dd41f596 | 2898 | } |
34f971f6 PZ |
2899 | |
2900 | BUG(); /* the idle class will always have a runnable task */ | |
dd41f596 | 2901 | } |
1da177e4 | 2902 | |
dd41f596 | 2903 | /* |
c259e01a | 2904 | * __schedule() is the main scheduler function. |
edde96ea PE |
2905 | * |
2906 | * The main means of driving the scheduler and thus entering this function are: | |
2907 | * | |
2908 | * 1. Explicit blocking: mutex, semaphore, waitqueue, etc. | |
2909 | * | |
2910 | * 2. TIF_NEED_RESCHED flag is checked on interrupt and userspace return | |
2911 | * paths. For example, see arch/x86/entry_64.S. | |
2912 | * | |
2913 | * To drive preemption between tasks, the scheduler sets the flag in timer | |
2914 | * interrupt handler scheduler_tick(). | |
2915 | * | |
2916 | * 3. Wakeups don't really cause entry into schedule(). They add a | |
2917 | * task to the run-queue and that's it. | |
2918 | * | |
2919 | * Now, if the new task added to the run-queue preempts the current | |
2920 | * task, then the wakeup sets TIF_NEED_RESCHED and schedule() gets | |
2921 | * called on the nearest possible occasion: | |
2922 | * | |
2923 | * - If the kernel is preemptible (CONFIG_PREEMPT=y): | |
2924 | * | |
2925 | * - in syscall or exception context, at the next outmost | |
2926 | * preempt_enable(). (this might be as soon as the wake_up()'s | |
2927 | * spin_unlock()!) | |
2928 | * | |
2929 | * - in IRQ context, return from interrupt-handler to | |
2930 | * preemptible context | |
2931 | * | |
2932 | * - If the kernel is not preemptible (CONFIG_PREEMPT is not set) | |
2933 | * then at the next: | |
2934 | * | |
2935 | * - cond_resched() call | |
2936 | * - explicit schedule() call | |
2937 | * - return from syscall or exception to user-space | |
2938 | * - return from interrupt-handler to user-space | |
bfd9b2b5 | 2939 | * |
b30f0e3f | 2940 | * WARNING: must be called with preemption disabled! |
dd41f596 | 2941 | */ |
c259e01a | 2942 | static void __sched __schedule(void) |
dd41f596 IM |
2943 | { |
2944 | struct task_struct *prev, *next; | |
67ca7bde | 2945 | unsigned long *switch_count; |
dd41f596 | 2946 | struct rq *rq; |
31656519 | 2947 | int cpu; |
dd41f596 | 2948 | |
dd41f596 IM |
2949 | cpu = smp_processor_id(); |
2950 | rq = cpu_rq(cpu); | |
38200cf2 | 2951 | rcu_note_context_switch(); |
dd41f596 | 2952 | prev = rq->curr; |
dd41f596 | 2953 | |
dd41f596 | 2954 | schedule_debug(prev); |
1da177e4 | 2955 | |
31656519 | 2956 | if (sched_feat(HRTICK)) |
f333fdc9 | 2957 | hrtick_clear(rq); |
8f4d37ec | 2958 | |
e0acd0a6 ON |
2959 | /* |
2960 | * Make sure that signal_pending_state()->signal_pending() below | |
2961 | * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE) | |
2962 | * done by the caller to avoid the race with signal_wake_up(). | |
2963 | */ | |
2964 | smp_mb__before_spinlock(); | |
05fa785c | 2965 | raw_spin_lock_irq(&rq->lock); |
1da177e4 | 2966 | |
9edfbfed PZ |
2967 | rq->clock_skip_update <<= 1; /* promote REQ to ACT */ |
2968 | ||
246d86b5 | 2969 | switch_count = &prev->nivcsw; |
1da177e4 | 2970 | if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) { |
21aa9af0 | 2971 | if (unlikely(signal_pending_state(prev->state, prev))) { |
1da177e4 | 2972 | prev->state = TASK_RUNNING; |
21aa9af0 | 2973 | } else { |
2acca55e PZ |
2974 | deactivate_task(rq, prev, DEQUEUE_SLEEP); |
2975 | prev->on_rq = 0; | |
2976 | ||
21aa9af0 | 2977 | /* |
2acca55e PZ |
2978 | * If a worker went to sleep, notify and ask workqueue |
2979 | * whether it wants to wake up a task to maintain | |
2980 | * concurrency. | |
21aa9af0 TH |
2981 | */ |
2982 | if (prev->flags & PF_WQ_WORKER) { | |
2983 | struct task_struct *to_wakeup; | |
2984 | ||
2985 | to_wakeup = wq_worker_sleeping(prev, cpu); | |
2986 | if (to_wakeup) | |
2987 | try_to_wake_up_local(to_wakeup); | |
2988 | } | |
21aa9af0 | 2989 | } |
dd41f596 | 2990 | switch_count = &prev->nvcsw; |
1da177e4 LT |
2991 | } |
2992 | ||
9edfbfed | 2993 | if (task_on_rq_queued(prev)) |
606dba2e PZ |
2994 | update_rq_clock(rq); |
2995 | ||
2996 | next = pick_next_task(rq, prev); | |
f26f9aff | 2997 | clear_tsk_need_resched(prev); |
f27dde8d | 2998 | clear_preempt_need_resched(); |
9edfbfed | 2999 | rq->clock_skip_update = 0; |
1da177e4 | 3000 | |
1da177e4 | 3001 | if (likely(prev != next)) { |
1da177e4 LT |
3002 | rq->nr_switches++; |
3003 | rq->curr = next; | |
3004 | ++*switch_count; | |
3005 | ||
dfa50b60 ON |
3006 | rq = context_switch(rq, prev, next); /* unlocks the rq */ |
3007 | cpu = cpu_of(rq); | |
1da177e4 | 3008 | } else |
05fa785c | 3009 | raw_spin_unlock_irq(&rq->lock); |
1da177e4 | 3010 | |
e3fca9e7 | 3011 | balance_callback(rq); |
1da177e4 | 3012 | } |
c259e01a | 3013 | |
9c40cef2 TG |
3014 | static inline void sched_submit_work(struct task_struct *tsk) |
3015 | { | |
3c7d5184 | 3016 | if (!tsk->state || tsk_is_pi_blocked(tsk)) |
9c40cef2 TG |
3017 | return; |
3018 | /* | |
3019 | * If we are going to sleep and we have plugged IO queued, | |
3020 | * make sure to submit it to avoid deadlocks. | |
3021 | */ | |
3022 | if (blk_needs_flush_plug(tsk)) | |
3023 | blk_schedule_flush_plug(tsk); | |
3024 | } | |
3025 | ||
722a9f92 | 3026 | asmlinkage __visible void __sched schedule(void) |
c259e01a | 3027 | { |
9c40cef2 TG |
3028 | struct task_struct *tsk = current; |
3029 | ||
3030 | sched_submit_work(tsk); | |
bfd9b2b5 | 3031 | do { |
b30f0e3f | 3032 | preempt_disable(); |
bfd9b2b5 | 3033 | __schedule(); |
b30f0e3f | 3034 | sched_preempt_enable_no_resched(); |
bfd9b2b5 | 3035 | } while (need_resched()); |
c259e01a | 3036 | } |
1da177e4 LT |
3037 | EXPORT_SYMBOL(schedule); |
3038 | ||
91d1aa43 | 3039 | #ifdef CONFIG_CONTEXT_TRACKING |
722a9f92 | 3040 | asmlinkage __visible void __sched schedule_user(void) |
20ab65e3 FW |
3041 | { |
3042 | /* | |
3043 | * If we come here after a random call to set_need_resched(), | |
3044 | * or we have been woken up remotely but the IPI has not yet arrived, | |
3045 | * we haven't yet exited the RCU idle mode. Do it here manually until | |
3046 | * we find a better solution. | |
7cc78f8f AL |
3047 | * |
3048 | * NB: There are buggy callers of this function. Ideally we | |
c467ea76 | 3049 | * should warn if prev_state != CONTEXT_USER, but that will trigger |
7cc78f8f | 3050 | * too frequently to make sense yet. |
20ab65e3 | 3051 | */ |
7cc78f8f | 3052 | enum ctx_state prev_state = exception_enter(); |
20ab65e3 | 3053 | schedule(); |
7cc78f8f | 3054 | exception_exit(prev_state); |
20ab65e3 FW |
3055 | } |
3056 | #endif | |
3057 | ||
c5491ea7 TG |
3058 | /** |
3059 | * schedule_preempt_disabled - called with preemption disabled | |
3060 | * | |
3061 | * Returns with preemption disabled. Note: preempt_count must be 1 | |
3062 | */ | |
3063 | void __sched schedule_preempt_disabled(void) | |
3064 | { | |
ba74c144 | 3065 | sched_preempt_enable_no_resched(); |
c5491ea7 TG |
3066 | schedule(); |
3067 | preempt_disable(); | |
3068 | } | |
3069 | ||
06b1f808 | 3070 | static void __sched notrace preempt_schedule_common(void) |
a18b5d01 FW |
3071 | { |
3072 | do { | |
b30f0e3f | 3073 | preempt_active_enter(); |
a18b5d01 | 3074 | __schedule(); |
b30f0e3f | 3075 | preempt_active_exit(); |
a18b5d01 FW |
3076 | |
3077 | /* | |
3078 | * Check again in case we missed a preemption opportunity | |
3079 | * between schedule and now. | |
3080 | */ | |
a18b5d01 FW |
3081 | } while (need_resched()); |
3082 | } | |
3083 | ||
1da177e4 LT |
3084 | #ifdef CONFIG_PREEMPT |
3085 | /* | |
2ed6e34f | 3086 | * this is the entry point to schedule() from in-kernel preemption |
41a2d6cf | 3087 | * off of preempt_enable. Kernel preemptions off return from interrupt |
1da177e4 LT |
3088 | * occur there and call schedule directly. |
3089 | */ | |
722a9f92 | 3090 | asmlinkage __visible void __sched notrace preempt_schedule(void) |
1da177e4 | 3091 | { |
1da177e4 LT |
3092 | /* |
3093 | * If there is a non-zero preempt_count or interrupts are disabled, | |
41a2d6cf | 3094 | * we do not want to preempt the current task. Just return.. |
1da177e4 | 3095 | */ |
fbb00b56 | 3096 | if (likely(!preemptible())) |
1da177e4 LT |
3097 | return; |
3098 | ||
a18b5d01 | 3099 | preempt_schedule_common(); |
1da177e4 | 3100 | } |
376e2424 | 3101 | NOKPROBE_SYMBOL(preempt_schedule); |
1da177e4 | 3102 | EXPORT_SYMBOL(preempt_schedule); |
009f60e2 | 3103 | |
009f60e2 | 3104 | /** |
4eaca0a8 | 3105 | * preempt_schedule_notrace - preempt_schedule called by tracing |
009f60e2 ON |
3106 | * |
3107 | * The tracing infrastructure uses preempt_enable_notrace to prevent | |
3108 | * recursion and tracing preempt enabling caused by the tracing | |
3109 | * infrastructure itself. But as tracing can happen in areas coming | |
3110 | * from userspace or just about to enter userspace, a preempt enable | |
3111 | * can occur before user_exit() is called. This will cause the scheduler | |
3112 | * to be called when the system is still in usermode. | |
3113 | * | |
3114 | * To prevent this, the preempt_enable_notrace will use this function | |
3115 | * instead of preempt_schedule() to exit user context if needed before | |
3116 | * calling the scheduler. | |
3117 | */ | |
4eaca0a8 | 3118 | asmlinkage __visible void __sched notrace preempt_schedule_notrace(void) |
009f60e2 ON |
3119 | { |
3120 | enum ctx_state prev_ctx; | |
3121 | ||
3122 | if (likely(!preemptible())) | |
3123 | return; | |
3124 | ||
3125 | do { | |
be690035 FW |
3126 | /* |
3127 | * Use raw __prempt_count() ops that don't call function. | |
3128 | * We can't call functions before disabling preemption which | |
3129 | * disarm preemption tracing recursions. | |
3130 | */ | |
3131 | __preempt_count_add(PREEMPT_ACTIVE + PREEMPT_DISABLE_OFFSET); | |
3132 | barrier(); | |
009f60e2 ON |
3133 | /* |
3134 | * Needs preempt disabled in case user_exit() is traced | |
3135 | * and the tracer calls preempt_enable_notrace() causing | |
3136 | * an infinite recursion. | |
3137 | */ | |
3138 | prev_ctx = exception_enter(); | |
3139 | __schedule(); | |
3140 | exception_exit(prev_ctx); | |
3141 | ||
be690035 FW |
3142 | barrier(); |
3143 | __preempt_count_sub(PREEMPT_ACTIVE + PREEMPT_DISABLE_OFFSET); | |
009f60e2 ON |
3144 | } while (need_resched()); |
3145 | } | |
4eaca0a8 | 3146 | EXPORT_SYMBOL_GPL(preempt_schedule_notrace); |
009f60e2 | 3147 | |
32e475d7 | 3148 | #endif /* CONFIG_PREEMPT */ |
1da177e4 LT |
3149 | |
3150 | /* | |
2ed6e34f | 3151 | * this is the entry point to schedule() from kernel preemption |
1da177e4 LT |
3152 | * off of irq context. |
3153 | * Note, that this is called and return with irqs disabled. This will | |
3154 | * protect us against recursive calling from irq. | |
3155 | */ | |
722a9f92 | 3156 | asmlinkage __visible void __sched preempt_schedule_irq(void) |
1da177e4 | 3157 | { |
b22366cd | 3158 | enum ctx_state prev_state; |
6478d880 | 3159 | |
2ed6e34f | 3160 | /* Catch callers which need to be fixed */ |
f27dde8d | 3161 | BUG_ON(preempt_count() || !irqs_disabled()); |
1da177e4 | 3162 | |
b22366cd FW |
3163 | prev_state = exception_enter(); |
3164 | ||
3a5c359a | 3165 | do { |
b30f0e3f | 3166 | preempt_active_enter(); |
3a5c359a | 3167 | local_irq_enable(); |
c259e01a | 3168 | __schedule(); |
3a5c359a | 3169 | local_irq_disable(); |
b30f0e3f | 3170 | preempt_active_exit(); |
5ed0cec0 | 3171 | } while (need_resched()); |
b22366cd FW |
3172 | |
3173 | exception_exit(prev_state); | |
1da177e4 LT |
3174 | } |
3175 | ||
63859d4f | 3176 | int default_wake_function(wait_queue_t *curr, unsigned mode, int wake_flags, |
95cdf3b7 | 3177 | void *key) |
1da177e4 | 3178 | { |
63859d4f | 3179 | return try_to_wake_up(curr->private, mode, wake_flags); |
1da177e4 | 3180 | } |
1da177e4 LT |
3181 | EXPORT_SYMBOL(default_wake_function); |
3182 | ||
b29739f9 IM |
3183 | #ifdef CONFIG_RT_MUTEXES |
3184 | ||
3185 | /* | |
3186 | * rt_mutex_setprio - set the current priority of a task | |
3187 | * @p: task | |
3188 | * @prio: prio value (kernel-internal form) | |
3189 | * | |
3190 | * This function changes the 'effective' priority of a task. It does | |
3191 | * not touch ->normal_prio like __setscheduler(). | |
3192 | * | |
c365c292 TG |
3193 | * Used by the rt_mutex code to implement priority inheritance |
3194 | * logic. Call site only calls if the priority of the task changed. | |
b29739f9 | 3195 | */ |
36c8b586 | 3196 | void rt_mutex_setprio(struct task_struct *p, int prio) |
b29739f9 | 3197 | { |
da0c1e65 | 3198 | int oldprio, queued, running, enqueue_flag = 0; |
70b97a7f | 3199 | struct rq *rq; |
83ab0aa0 | 3200 | const struct sched_class *prev_class; |
b29739f9 | 3201 | |
aab03e05 | 3202 | BUG_ON(prio > MAX_PRIO); |
b29739f9 | 3203 | |
0122ec5b | 3204 | rq = __task_rq_lock(p); |
b29739f9 | 3205 | |
1c4dd99b TG |
3206 | /* |
3207 | * Idle task boosting is a nono in general. There is one | |
3208 | * exception, when PREEMPT_RT and NOHZ is active: | |
3209 | * | |
3210 | * The idle task calls get_next_timer_interrupt() and holds | |
3211 | * the timer wheel base->lock on the CPU and another CPU wants | |
3212 | * to access the timer (probably to cancel it). We can safely | |
3213 | * ignore the boosting request, as the idle CPU runs this code | |
3214 | * with interrupts disabled and will complete the lock | |
3215 | * protected section without being interrupted. So there is no | |
3216 | * real need to boost. | |
3217 | */ | |
3218 | if (unlikely(p == rq->idle)) { | |
3219 | WARN_ON(p != rq->curr); | |
3220 | WARN_ON(p->pi_blocked_on); | |
3221 | goto out_unlock; | |
3222 | } | |
3223 | ||
a8027073 | 3224 | trace_sched_pi_setprio(p, prio); |
d5f9f942 | 3225 | oldprio = p->prio; |
83ab0aa0 | 3226 | prev_class = p->sched_class; |
da0c1e65 | 3227 | queued = task_on_rq_queued(p); |
051a1d1a | 3228 | running = task_current(rq, p); |
da0c1e65 | 3229 | if (queued) |
69be72c1 | 3230 | dequeue_task(rq, p, 0); |
0e1f3483 | 3231 | if (running) |
f3cd1c4e | 3232 | put_prev_task(rq, p); |
dd41f596 | 3233 | |
2d3d891d DF |
3234 | /* |
3235 | * Boosting condition are: | |
3236 | * 1. -rt task is running and holds mutex A | |
3237 | * --> -dl task blocks on mutex A | |
3238 | * | |
3239 | * 2. -dl task is running and holds mutex A | |
3240 | * --> -dl task blocks on mutex A and could preempt the | |
3241 | * running task | |
3242 | */ | |
3243 | if (dl_prio(prio)) { | |
466af29b ON |
3244 | struct task_struct *pi_task = rt_mutex_get_top_task(p); |
3245 | if (!dl_prio(p->normal_prio) || | |
3246 | (pi_task && dl_entity_preempt(&pi_task->dl, &p->dl))) { | |
2d3d891d DF |
3247 | p->dl.dl_boosted = 1; |
3248 | p->dl.dl_throttled = 0; | |
3249 | enqueue_flag = ENQUEUE_REPLENISH; | |
3250 | } else | |
3251 | p->dl.dl_boosted = 0; | |
aab03e05 | 3252 | p->sched_class = &dl_sched_class; |
2d3d891d DF |
3253 | } else if (rt_prio(prio)) { |
3254 | if (dl_prio(oldprio)) | |
3255 | p->dl.dl_boosted = 0; | |
3256 | if (oldprio < prio) | |
3257 | enqueue_flag = ENQUEUE_HEAD; | |
dd41f596 | 3258 | p->sched_class = &rt_sched_class; |
2d3d891d DF |
3259 | } else { |
3260 | if (dl_prio(oldprio)) | |
3261 | p->dl.dl_boosted = 0; | |
746db944 BS |
3262 | if (rt_prio(oldprio)) |
3263 | p->rt.timeout = 0; | |
dd41f596 | 3264 | p->sched_class = &fair_sched_class; |
2d3d891d | 3265 | } |
dd41f596 | 3266 | |
b29739f9 IM |
3267 | p->prio = prio; |
3268 | ||
0e1f3483 HS |
3269 | if (running) |
3270 | p->sched_class->set_curr_task(rq); | |
da0c1e65 | 3271 | if (queued) |
2d3d891d | 3272 | enqueue_task(rq, p, enqueue_flag); |
cb469845 | 3273 | |
da7a735e | 3274 | check_class_changed(rq, p, prev_class, oldprio); |
1c4dd99b | 3275 | out_unlock: |
4c9a4bc8 | 3276 | preempt_disable(); /* avoid rq from going away on us */ |
0122ec5b | 3277 | __task_rq_unlock(rq); |
4c9a4bc8 PZ |
3278 | |
3279 | balance_callback(rq); | |
3280 | preempt_enable(); | |
b29739f9 | 3281 | } |
b29739f9 | 3282 | #endif |
d50dde5a | 3283 | |
36c8b586 | 3284 | void set_user_nice(struct task_struct *p, long nice) |
1da177e4 | 3285 | { |
da0c1e65 | 3286 | int old_prio, delta, queued; |
1da177e4 | 3287 | unsigned long flags; |
70b97a7f | 3288 | struct rq *rq; |
1da177e4 | 3289 | |
75e45d51 | 3290 | if (task_nice(p) == nice || nice < MIN_NICE || nice > MAX_NICE) |
1da177e4 LT |
3291 | return; |
3292 | /* | |
3293 | * We have to be careful, if called from sys_setpriority(), | |
3294 | * the task might be in the middle of scheduling on another CPU. | |
3295 | */ | |
3296 | rq = task_rq_lock(p, &flags); | |
3297 | /* | |
3298 | * The RT priorities are set via sched_setscheduler(), but we still | |
3299 | * allow the 'normal' nice value to be set - but as expected | |
3300 | * it wont have any effect on scheduling until the task is | |
aab03e05 | 3301 | * SCHED_DEADLINE, SCHED_FIFO or SCHED_RR: |
1da177e4 | 3302 | */ |
aab03e05 | 3303 | if (task_has_dl_policy(p) || task_has_rt_policy(p)) { |
1da177e4 LT |
3304 | p->static_prio = NICE_TO_PRIO(nice); |
3305 | goto out_unlock; | |
3306 | } | |
da0c1e65 KT |
3307 | queued = task_on_rq_queued(p); |
3308 | if (queued) | |
69be72c1 | 3309 | dequeue_task(rq, p, 0); |
1da177e4 | 3310 | |
1da177e4 | 3311 | p->static_prio = NICE_TO_PRIO(nice); |
2dd73a4f | 3312 | set_load_weight(p); |
b29739f9 IM |
3313 | old_prio = p->prio; |
3314 | p->prio = effective_prio(p); | |
3315 | delta = p->prio - old_prio; | |
1da177e4 | 3316 | |
da0c1e65 | 3317 | if (queued) { |
371fd7e7 | 3318 | enqueue_task(rq, p, 0); |
1da177e4 | 3319 | /* |
d5f9f942 AM |
3320 | * If the task increased its priority or is running and |
3321 | * lowered its priority, then reschedule its CPU: | |
1da177e4 | 3322 | */ |
d5f9f942 | 3323 | if (delta < 0 || (delta > 0 && task_running(rq, p))) |
8875125e | 3324 | resched_curr(rq); |
1da177e4 LT |
3325 | } |
3326 | out_unlock: | |
0122ec5b | 3327 | task_rq_unlock(rq, p, &flags); |
1da177e4 | 3328 | } |
1da177e4 LT |
3329 | EXPORT_SYMBOL(set_user_nice); |
3330 | ||
e43379f1 MM |
3331 | /* |
3332 | * can_nice - check if a task can reduce its nice value | |
3333 | * @p: task | |
3334 | * @nice: nice value | |
3335 | */ | |
36c8b586 | 3336 | int can_nice(const struct task_struct *p, const int nice) |
e43379f1 | 3337 | { |
024f4747 | 3338 | /* convert nice value [19,-20] to rlimit style value [1,40] */ |
7aa2c016 | 3339 | int nice_rlim = nice_to_rlimit(nice); |
48f24c4d | 3340 | |
78d7d407 | 3341 | return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) || |
e43379f1 MM |
3342 | capable(CAP_SYS_NICE)); |
3343 | } | |
3344 | ||
1da177e4 LT |
3345 | #ifdef __ARCH_WANT_SYS_NICE |
3346 | ||
3347 | /* | |
3348 | * sys_nice - change the priority of the current process. | |
3349 | * @increment: priority increment | |
3350 | * | |
3351 | * sys_setpriority is a more generic, but much slower function that | |
3352 | * does similar things. | |
3353 | */ | |
5add95d4 | 3354 | SYSCALL_DEFINE1(nice, int, increment) |
1da177e4 | 3355 | { |
48f24c4d | 3356 | long nice, retval; |
1da177e4 LT |
3357 | |
3358 | /* | |
3359 | * Setpriority might change our priority at the same moment. | |
3360 | * We don't have to worry. Conceptually one call occurs first | |
3361 | * and we have a single winner. | |
3362 | */ | |
a9467fa3 | 3363 | increment = clamp(increment, -NICE_WIDTH, NICE_WIDTH); |
d0ea0268 | 3364 | nice = task_nice(current) + increment; |
1da177e4 | 3365 | |
a9467fa3 | 3366 | nice = clamp_val(nice, MIN_NICE, MAX_NICE); |
e43379f1 MM |
3367 | if (increment < 0 && !can_nice(current, nice)) |
3368 | return -EPERM; | |
3369 | ||
1da177e4 LT |
3370 | retval = security_task_setnice(current, nice); |
3371 | if (retval) | |
3372 | return retval; | |
3373 | ||
3374 | set_user_nice(current, nice); | |
3375 | return 0; | |
3376 | } | |
3377 | ||
3378 | #endif | |
3379 | ||
3380 | /** | |
3381 | * task_prio - return the priority value of a given task. | |
3382 | * @p: the task in question. | |
3383 | * | |
e69f6186 | 3384 | * Return: The priority value as seen by users in /proc. |
1da177e4 LT |
3385 | * RT tasks are offset by -200. Normal tasks are centered |
3386 | * around 0, value goes from -16 to +15. | |
3387 | */ | |
36c8b586 | 3388 | int task_prio(const struct task_struct *p) |
1da177e4 LT |
3389 | { |
3390 | return p->prio - MAX_RT_PRIO; | |
3391 | } | |
3392 | ||
1da177e4 LT |
3393 | /** |
3394 | * idle_cpu - is a given cpu idle currently? | |
3395 | * @cpu: the processor in question. | |
e69f6186 YB |
3396 | * |
3397 | * Return: 1 if the CPU is currently idle. 0 otherwise. | |
1da177e4 LT |
3398 | */ |
3399 | int idle_cpu(int cpu) | |
3400 | { | |
908a3283 TG |
3401 | struct rq *rq = cpu_rq(cpu); |
3402 | ||
3403 | if (rq->curr != rq->idle) | |
3404 | return 0; | |
3405 | ||
3406 | if (rq->nr_running) | |
3407 | return 0; | |
3408 | ||
3409 | #ifdef CONFIG_SMP | |
3410 | if (!llist_empty(&rq->wake_list)) | |
3411 | return 0; | |
3412 | #endif | |
3413 | ||
3414 | return 1; | |
1da177e4 LT |
3415 | } |
3416 | ||
1da177e4 LT |
3417 | /** |
3418 | * idle_task - return the idle task for a given cpu. | |
3419 | * @cpu: the processor in question. | |
e69f6186 YB |
3420 | * |
3421 | * Return: The idle task for the cpu @cpu. | |
1da177e4 | 3422 | */ |
36c8b586 | 3423 | struct task_struct *idle_task(int cpu) |
1da177e4 LT |
3424 | { |
3425 | return cpu_rq(cpu)->idle; | |
3426 | } | |
3427 | ||
3428 | /** | |
3429 | * find_process_by_pid - find a process with a matching PID value. | |
3430 | * @pid: the pid in question. | |
e69f6186 YB |
3431 | * |
3432 | * The task of @pid, if found. %NULL otherwise. | |
1da177e4 | 3433 | */ |
a9957449 | 3434 | static struct task_struct *find_process_by_pid(pid_t pid) |
1da177e4 | 3435 | { |
228ebcbe | 3436 | return pid ? find_task_by_vpid(pid) : current; |
1da177e4 LT |
3437 | } |
3438 | ||
aab03e05 DF |
3439 | /* |
3440 | * This function initializes the sched_dl_entity of a newly becoming | |
3441 | * SCHED_DEADLINE task. | |
3442 | * | |
3443 | * Only the static values are considered here, the actual runtime and the | |
3444 | * absolute deadline will be properly calculated when the task is enqueued | |
3445 | * for the first time with its new policy. | |
3446 | */ | |
3447 | static void | |
3448 | __setparam_dl(struct task_struct *p, const struct sched_attr *attr) | |
3449 | { | |
3450 | struct sched_dl_entity *dl_se = &p->dl; | |
3451 | ||
aab03e05 DF |
3452 | dl_se->dl_runtime = attr->sched_runtime; |
3453 | dl_se->dl_deadline = attr->sched_deadline; | |
755378a4 | 3454 | dl_se->dl_period = attr->sched_period ?: dl_se->dl_deadline; |
aab03e05 | 3455 | dl_se->flags = attr->sched_flags; |
332ac17e | 3456 | dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime); |
40767b0d PZ |
3457 | |
3458 | /* | |
3459 | * Changing the parameters of a task is 'tricky' and we're not doing | |
3460 | * the correct thing -- also see task_dead_dl() and switched_from_dl(). | |
3461 | * | |
3462 | * What we SHOULD do is delay the bandwidth release until the 0-lag | |
3463 | * point. This would include retaining the task_struct until that time | |
3464 | * and change dl_overflow() to not immediately decrement the current | |
3465 | * amount. | |
3466 | * | |
3467 | * Instead we retain the current runtime/deadline and let the new | |
3468 | * parameters take effect after the current reservation period lapses. | |
3469 | * This is safe (albeit pessimistic) because the 0-lag point is always | |
3470 | * before the current scheduling deadline. | |
3471 | * | |
3472 | * We can still have temporary overloads because we do not delay the | |
3473 | * change in bandwidth until that time; so admission control is | |
3474 | * not on the safe side. It does however guarantee tasks will never | |
3475 | * consume more than promised. | |
3476 | */ | |
aab03e05 DF |
3477 | } |
3478 | ||
c13db6b1 SR |
3479 | /* |
3480 | * sched_setparam() passes in -1 for its policy, to let the functions | |
3481 | * it calls know not to change it. | |
3482 | */ | |
3483 | #define SETPARAM_POLICY -1 | |
3484 | ||
c365c292 TG |
3485 | static void __setscheduler_params(struct task_struct *p, |
3486 | const struct sched_attr *attr) | |
1da177e4 | 3487 | { |
d50dde5a DF |
3488 | int policy = attr->sched_policy; |
3489 | ||
c13db6b1 | 3490 | if (policy == SETPARAM_POLICY) |
39fd8fd2 PZ |
3491 | policy = p->policy; |
3492 | ||
1da177e4 | 3493 | p->policy = policy; |
d50dde5a | 3494 | |
aab03e05 DF |
3495 | if (dl_policy(policy)) |
3496 | __setparam_dl(p, attr); | |
39fd8fd2 | 3497 | else if (fair_policy(policy)) |
d50dde5a DF |
3498 | p->static_prio = NICE_TO_PRIO(attr->sched_nice); |
3499 | ||
39fd8fd2 PZ |
3500 | /* |
3501 | * __sched_setscheduler() ensures attr->sched_priority == 0 when | |
3502 | * !rt_policy. Always setting this ensures that things like | |
3503 | * getparam()/getattr() don't report silly values for !rt tasks. | |
3504 | */ | |
3505 | p->rt_priority = attr->sched_priority; | |
383afd09 | 3506 | p->normal_prio = normal_prio(p); |
c365c292 TG |
3507 | set_load_weight(p); |
3508 | } | |
39fd8fd2 | 3509 | |
c365c292 TG |
3510 | /* Actually do priority change: must hold pi & rq lock. */ |
3511 | static void __setscheduler(struct rq *rq, struct task_struct *p, | |
0782e63b | 3512 | const struct sched_attr *attr, bool keep_boost) |
c365c292 TG |
3513 | { |
3514 | __setscheduler_params(p, attr); | |
d50dde5a | 3515 | |
383afd09 | 3516 | /* |
0782e63b TG |
3517 | * Keep a potential priority boosting if called from |
3518 | * sched_setscheduler(). | |
383afd09 | 3519 | */ |
0782e63b TG |
3520 | if (keep_boost) |
3521 | p->prio = rt_mutex_get_effective_prio(p, normal_prio(p)); | |
3522 | else | |
3523 | p->prio = normal_prio(p); | |
383afd09 | 3524 | |
aab03e05 DF |
3525 | if (dl_prio(p->prio)) |
3526 | p->sched_class = &dl_sched_class; | |
3527 | else if (rt_prio(p->prio)) | |
ffd44db5 PZ |
3528 | p->sched_class = &rt_sched_class; |
3529 | else | |
3530 | p->sched_class = &fair_sched_class; | |
1da177e4 | 3531 | } |
aab03e05 DF |
3532 | |
3533 | static void | |
3534 | __getparam_dl(struct task_struct *p, struct sched_attr *attr) | |
3535 | { | |
3536 | struct sched_dl_entity *dl_se = &p->dl; | |
3537 | ||
3538 | attr->sched_priority = p->rt_priority; | |
3539 | attr->sched_runtime = dl_se->dl_runtime; | |
3540 | attr->sched_deadline = dl_se->dl_deadline; | |
755378a4 | 3541 | attr->sched_period = dl_se->dl_period; |
aab03e05 DF |
3542 | attr->sched_flags = dl_se->flags; |
3543 | } | |
3544 | ||
3545 | /* | |
3546 | * This function validates the new parameters of a -deadline task. | |
3547 | * We ask for the deadline not being zero, and greater or equal | |
755378a4 | 3548 | * than the runtime, as well as the period of being zero or |
332ac17e | 3549 | * greater than deadline. Furthermore, we have to be sure that |
b0827819 JL |
3550 | * user parameters are above the internal resolution of 1us (we |
3551 | * check sched_runtime only since it is always the smaller one) and | |
3552 | * below 2^63 ns (we have to check both sched_deadline and | |
3553 | * sched_period, as the latter can be zero). | |
aab03e05 DF |
3554 | */ |
3555 | static bool | |
3556 | __checkparam_dl(const struct sched_attr *attr) | |
3557 | { | |
b0827819 JL |
3558 | /* deadline != 0 */ |
3559 | if (attr->sched_deadline == 0) | |
3560 | return false; | |
3561 | ||
3562 | /* | |
3563 | * Since we truncate DL_SCALE bits, make sure we're at least | |
3564 | * that big. | |
3565 | */ | |
3566 | if (attr->sched_runtime < (1ULL << DL_SCALE)) | |
3567 | return false; | |
3568 | ||
3569 | /* | |
3570 | * Since we use the MSB for wrap-around and sign issues, make | |
3571 | * sure it's not set (mind that period can be equal to zero). | |
3572 | */ | |
3573 | if (attr->sched_deadline & (1ULL << 63) || | |
3574 | attr->sched_period & (1ULL << 63)) | |
3575 | return false; | |
3576 | ||
3577 | /* runtime <= deadline <= period (if period != 0) */ | |
3578 | if ((attr->sched_period != 0 && | |
3579 | attr->sched_period < attr->sched_deadline) || | |
3580 | attr->sched_deadline < attr->sched_runtime) | |
3581 | return false; | |
3582 | ||
3583 | return true; | |
aab03e05 DF |
3584 | } |
3585 | ||
c69e8d9c DH |
3586 | /* |
3587 | * check the target process has a UID that matches the current process's | |
3588 | */ | |
3589 | static bool check_same_owner(struct task_struct *p) | |
3590 | { | |
3591 | const struct cred *cred = current_cred(), *pcred; | |
3592 | bool match; | |
3593 | ||
3594 | rcu_read_lock(); | |
3595 | pcred = __task_cred(p); | |
9c806aa0 EB |
3596 | match = (uid_eq(cred->euid, pcred->euid) || |
3597 | uid_eq(cred->euid, pcred->uid)); | |
c69e8d9c DH |
3598 | rcu_read_unlock(); |
3599 | return match; | |
3600 | } | |
3601 | ||
75381608 WL |
3602 | static bool dl_param_changed(struct task_struct *p, |
3603 | const struct sched_attr *attr) | |
3604 | { | |
3605 | struct sched_dl_entity *dl_se = &p->dl; | |
3606 | ||
3607 | if (dl_se->dl_runtime != attr->sched_runtime || | |
3608 | dl_se->dl_deadline != attr->sched_deadline || | |
3609 | dl_se->dl_period != attr->sched_period || | |
3610 | dl_se->flags != attr->sched_flags) | |
3611 | return true; | |
3612 | ||
3613 | return false; | |
3614 | } | |
3615 | ||
d50dde5a DF |
3616 | static int __sched_setscheduler(struct task_struct *p, |
3617 | const struct sched_attr *attr, | |
dbc7f069 | 3618 | bool user, bool pi) |
1da177e4 | 3619 | { |
383afd09 SR |
3620 | int newprio = dl_policy(attr->sched_policy) ? MAX_DL_PRIO - 1 : |
3621 | MAX_RT_PRIO - 1 - attr->sched_priority; | |
da0c1e65 | 3622 | int retval, oldprio, oldpolicy = -1, queued, running; |
0782e63b | 3623 | int new_effective_prio, policy = attr->sched_policy; |
1da177e4 | 3624 | unsigned long flags; |
83ab0aa0 | 3625 | const struct sched_class *prev_class; |
70b97a7f | 3626 | struct rq *rq; |
ca94c442 | 3627 | int reset_on_fork; |
1da177e4 | 3628 | |
66e5393a SR |
3629 | /* may grab non-irq protected spin_locks */ |
3630 | BUG_ON(in_interrupt()); | |
1da177e4 LT |
3631 | recheck: |
3632 | /* double check policy once rq lock held */ | |
ca94c442 LP |
3633 | if (policy < 0) { |
3634 | reset_on_fork = p->sched_reset_on_fork; | |
1da177e4 | 3635 | policy = oldpolicy = p->policy; |
ca94c442 | 3636 | } else { |
7479f3c9 | 3637 | reset_on_fork = !!(attr->sched_flags & SCHED_FLAG_RESET_ON_FORK); |
ca94c442 | 3638 | |
aab03e05 DF |
3639 | if (policy != SCHED_DEADLINE && |
3640 | policy != SCHED_FIFO && policy != SCHED_RR && | |
ca94c442 LP |
3641 | policy != SCHED_NORMAL && policy != SCHED_BATCH && |
3642 | policy != SCHED_IDLE) | |
3643 | return -EINVAL; | |
3644 | } | |
3645 | ||
7479f3c9 PZ |
3646 | if (attr->sched_flags & ~(SCHED_FLAG_RESET_ON_FORK)) |
3647 | return -EINVAL; | |
3648 | ||
1da177e4 LT |
3649 | /* |
3650 | * Valid priorities for SCHED_FIFO and SCHED_RR are | |
dd41f596 IM |
3651 | * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL, |
3652 | * SCHED_BATCH and SCHED_IDLE is 0. | |
1da177e4 | 3653 | */ |
0bb040a4 | 3654 | if ((p->mm && attr->sched_priority > MAX_USER_RT_PRIO-1) || |
d50dde5a | 3655 | (!p->mm && attr->sched_priority > MAX_RT_PRIO-1)) |
1da177e4 | 3656 | return -EINVAL; |
aab03e05 DF |
3657 | if ((dl_policy(policy) && !__checkparam_dl(attr)) || |
3658 | (rt_policy(policy) != (attr->sched_priority != 0))) | |
1da177e4 LT |
3659 | return -EINVAL; |
3660 | ||
37e4ab3f OC |
3661 | /* |
3662 | * Allow unprivileged RT tasks to decrease priority: | |
3663 | */ | |
961ccddd | 3664 | if (user && !capable(CAP_SYS_NICE)) { |
d50dde5a | 3665 | if (fair_policy(policy)) { |
d0ea0268 | 3666 | if (attr->sched_nice < task_nice(p) && |
eaad4513 | 3667 | !can_nice(p, attr->sched_nice)) |
d50dde5a DF |
3668 | return -EPERM; |
3669 | } | |
3670 | ||
e05606d3 | 3671 | if (rt_policy(policy)) { |
a44702e8 ON |
3672 | unsigned long rlim_rtprio = |
3673 | task_rlimit(p, RLIMIT_RTPRIO); | |
8dc3e909 ON |
3674 | |
3675 | /* can't set/change the rt policy */ | |
3676 | if (policy != p->policy && !rlim_rtprio) | |
3677 | return -EPERM; | |
3678 | ||
3679 | /* can't increase priority */ | |
d50dde5a DF |
3680 | if (attr->sched_priority > p->rt_priority && |
3681 | attr->sched_priority > rlim_rtprio) | |
8dc3e909 ON |
3682 | return -EPERM; |
3683 | } | |
c02aa73b | 3684 | |
d44753b8 JL |
3685 | /* |
3686 | * Can't set/change SCHED_DEADLINE policy at all for now | |
3687 | * (safest behavior); in the future we would like to allow | |
3688 | * unprivileged DL tasks to increase their relative deadline | |
3689 | * or reduce their runtime (both ways reducing utilization) | |
3690 | */ | |
3691 | if (dl_policy(policy)) | |
3692 | return -EPERM; | |
3693 | ||
dd41f596 | 3694 | /* |
c02aa73b DH |
3695 | * Treat SCHED_IDLE as nice 20. Only allow a switch to |
3696 | * SCHED_NORMAL if the RLIMIT_NICE would normally permit it. | |
dd41f596 | 3697 | */ |
c02aa73b | 3698 | if (p->policy == SCHED_IDLE && policy != SCHED_IDLE) { |
d0ea0268 | 3699 | if (!can_nice(p, task_nice(p))) |
c02aa73b DH |
3700 | return -EPERM; |
3701 | } | |
5fe1d75f | 3702 | |
37e4ab3f | 3703 | /* can't change other user's priorities */ |
c69e8d9c | 3704 | if (!check_same_owner(p)) |
37e4ab3f | 3705 | return -EPERM; |
ca94c442 LP |
3706 | |
3707 | /* Normal users shall not reset the sched_reset_on_fork flag */ | |
3708 | if (p->sched_reset_on_fork && !reset_on_fork) | |
3709 | return -EPERM; | |
37e4ab3f | 3710 | } |
1da177e4 | 3711 | |
725aad24 | 3712 | if (user) { |
b0ae1981 | 3713 | retval = security_task_setscheduler(p); |
725aad24 JF |
3714 | if (retval) |
3715 | return retval; | |
3716 | } | |
3717 | ||
b29739f9 IM |
3718 | /* |
3719 | * make sure no PI-waiters arrive (or leave) while we are | |
3720 | * changing the priority of the task: | |
0122ec5b | 3721 | * |
25985edc | 3722 | * To be able to change p->policy safely, the appropriate |
1da177e4 LT |
3723 | * runqueue lock must be held. |
3724 | */ | |
0122ec5b | 3725 | rq = task_rq_lock(p, &flags); |
dc61b1d6 | 3726 | |
34f971f6 PZ |
3727 | /* |
3728 | * Changing the policy of the stop threads its a very bad idea | |
3729 | */ | |
3730 | if (p == rq->stop) { | |
0122ec5b | 3731 | task_rq_unlock(rq, p, &flags); |
34f971f6 PZ |
3732 | return -EINVAL; |
3733 | } | |
3734 | ||
a51e9198 | 3735 | /* |
d6b1e911 TG |
3736 | * If not changing anything there's no need to proceed further, |
3737 | * but store a possible modification of reset_on_fork. | |
a51e9198 | 3738 | */ |
d50dde5a | 3739 | if (unlikely(policy == p->policy)) { |
d0ea0268 | 3740 | if (fair_policy(policy) && attr->sched_nice != task_nice(p)) |
d50dde5a DF |
3741 | goto change; |
3742 | if (rt_policy(policy) && attr->sched_priority != p->rt_priority) | |
3743 | goto change; | |
75381608 | 3744 | if (dl_policy(policy) && dl_param_changed(p, attr)) |
aab03e05 | 3745 | goto change; |
d50dde5a | 3746 | |
d6b1e911 | 3747 | p->sched_reset_on_fork = reset_on_fork; |
45afb173 | 3748 | task_rq_unlock(rq, p, &flags); |
a51e9198 DF |
3749 | return 0; |
3750 | } | |
d50dde5a | 3751 | change: |
a51e9198 | 3752 | |
dc61b1d6 | 3753 | if (user) { |
332ac17e | 3754 | #ifdef CONFIG_RT_GROUP_SCHED |
dc61b1d6 PZ |
3755 | /* |
3756 | * Do not allow realtime tasks into groups that have no runtime | |
3757 | * assigned. | |
3758 | */ | |
3759 | if (rt_bandwidth_enabled() && rt_policy(policy) && | |
f4493771 MG |
3760 | task_group(p)->rt_bandwidth.rt_runtime == 0 && |
3761 | !task_group_is_autogroup(task_group(p))) { | |
0122ec5b | 3762 | task_rq_unlock(rq, p, &flags); |
dc61b1d6 PZ |
3763 | return -EPERM; |
3764 | } | |
dc61b1d6 | 3765 | #endif |
332ac17e DF |
3766 | #ifdef CONFIG_SMP |
3767 | if (dl_bandwidth_enabled() && dl_policy(policy)) { | |
3768 | cpumask_t *span = rq->rd->span; | |
332ac17e DF |
3769 | |
3770 | /* | |
3771 | * Don't allow tasks with an affinity mask smaller than | |
3772 | * the entire root_domain to become SCHED_DEADLINE. We | |
3773 | * will also fail if there's no bandwidth available. | |
3774 | */ | |
e4099a5e PZ |
3775 | if (!cpumask_subset(span, &p->cpus_allowed) || |
3776 | rq->rd->dl_bw.bw == 0) { | |
332ac17e DF |
3777 | task_rq_unlock(rq, p, &flags); |
3778 | return -EPERM; | |
3779 | } | |
3780 | } | |
3781 | #endif | |
3782 | } | |
dc61b1d6 | 3783 | |
1da177e4 LT |
3784 | /* recheck policy now with rq lock held */ |
3785 | if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { | |
3786 | policy = oldpolicy = -1; | |
0122ec5b | 3787 | task_rq_unlock(rq, p, &flags); |
1da177e4 LT |
3788 | goto recheck; |
3789 | } | |
332ac17e DF |
3790 | |
3791 | /* | |
3792 | * If setscheduling to SCHED_DEADLINE (or changing the parameters | |
3793 | * of a SCHED_DEADLINE task) we need to check if enough bandwidth | |
3794 | * is available. | |
3795 | */ | |
e4099a5e | 3796 | if ((dl_policy(policy) || dl_task(p)) && dl_overflow(p, policy, attr)) { |
332ac17e DF |
3797 | task_rq_unlock(rq, p, &flags); |
3798 | return -EBUSY; | |
3799 | } | |
3800 | ||
c365c292 TG |
3801 | p->sched_reset_on_fork = reset_on_fork; |
3802 | oldprio = p->prio; | |
3803 | ||
dbc7f069 PZ |
3804 | if (pi) { |
3805 | /* | |
3806 | * Take priority boosted tasks into account. If the new | |
3807 | * effective priority is unchanged, we just store the new | |
3808 | * normal parameters and do not touch the scheduler class and | |
3809 | * the runqueue. This will be done when the task deboost | |
3810 | * itself. | |
3811 | */ | |
3812 | new_effective_prio = rt_mutex_get_effective_prio(p, newprio); | |
3813 | if (new_effective_prio == oldprio) { | |
3814 | __setscheduler_params(p, attr); | |
3815 | task_rq_unlock(rq, p, &flags); | |
3816 | return 0; | |
3817 | } | |
c365c292 TG |
3818 | } |
3819 | ||
da0c1e65 | 3820 | queued = task_on_rq_queued(p); |
051a1d1a | 3821 | running = task_current(rq, p); |
da0c1e65 | 3822 | if (queued) |
4ca9b72b | 3823 | dequeue_task(rq, p, 0); |
0e1f3483 | 3824 | if (running) |
f3cd1c4e | 3825 | put_prev_task(rq, p); |
f6b53205 | 3826 | |
83ab0aa0 | 3827 | prev_class = p->sched_class; |
dbc7f069 | 3828 | __setscheduler(rq, p, attr, pi); |
f6b53205 | 3829 | |
0e1f3483 HS |
3830 | if (running) |
3831 | p->sched_class->set_curr_task(rq); | |
da0c1e65 | 3832 | if (queued) { |
81a44c54 TG |
3833 | /* |
3834 | * We enqueue to tail when the priority of a task is | |
3835 | * increased (user space view). | |
3836 | */ | |
3837 | enqueue_task(rq, p, oldprio <= p->prio ? ENQUEUE_HEAD : 0); | |
3838 | } | |
cb469845 | 3839 | |
da7a735e | 3840 | check_class_changed(rq, p, prev_class, oldprio); |
4c9a4bc8 | 3841 | preempt_disable(); /* avoid rq from going away on us */ |
0122ec5b | 3842 | task_rq_unlock(rq, p, &flags); |
b29739f9 | 3843 | |
dbc7f069 PZ |
3844 | if (pi) |
3845 | rt_mutex_adjust_pi(p); | |
95e02ca9 | 3846 | |
4c9a4bc8 PZ |
3847 | /* |
3848 | * Run balance callbacks after we've adjusted the PI chain. | |
3849 | */ | |
3850 | balance_callback(rq); | |
3851 | preempt_enable(); | |
3852 | ||
1da177e4 LT |
3853 | return 0; |
3854 | } | |
961ccddd | 3855 | |
7479f3c9 PZ |
3856 | static int _sched_setscheduler(struct task_struct *p, int policy, |
3857 | const struct sched_param *param, bool check) | |
3858 | { | |
3859 | struct sched_attr attr = { | |
3860 | .sched_policy = policy, | |
3861 | .sched_priority = param->sched_priority, | |
3862 | .sched_nice = PRIO_TO_NICE(p->static_prio), | |
3863 | }; | |
3864 | ||
c13db6b1 SR |
3865 | /* Fixup the legacy SCHED_RESET_ON_FORK hack. */ |
3866 | if ((policy != SETPARAM_POLICY) && (policy & SCHED_RESET_ON_FORK)) { | |
7479f3c9 PZ |
3867 | attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK; |
3868 | policy &= ~SCHED_RESET_ON_FORK; | |
3869 | attr.sched_policy = policy; | |
3870 | } | |
3871 | ||
dbc7f069 | 3872 | return __sched_setscheduler(p, &attr, check, true); |
7479f3c9 | 3873 | } |
961ccddd RR |
3874 | /** |
3875 | * sched_setscheduler - change the scheduling policy and/or RT priority of a thread. | |
3876 | * @p: the task in question. | |
3877 | * @policy: new policy. | |
3878 | * @param: structure containing the new RT priority. | |
3879 | * | |
e69f6186 YB |
3880 | * Return: 0 on success. An error code otherwise. |
3881 | * | |
961ccddd RR |
3882 | * NOTE that the task may be already dead. |
3883 | */ | |
3884 | int sched_setscheduler(struct task_struct *p, int policy, | |
fe7de49f | 3885 | const struct sched_param *param) |
961ccddd | 3886 | { |
7479f3c9 | 3887 | return _sched_setscheduler(p, policy, param, true); |
961ccddd | 3888 | } |
1da177e4 LT |
3889 | EXPORT_SYMBOL_GPL(sched_setscheduler); |
3890 | ||
d50dde5a DF |
3891 | int sched_setattr(struct task_struct *p, const struct sched_attr *attr) |
3892 | { | |
dbc7f069 | 3893 | return __sched_setscheduler(p, attr, true, true); |
d50dde5a DF |
3894 | } |
3895 | EXPORT_SYMBOL_GPL(sched_setattr); | |
3896 | ||
961ccddd RR |
3897 | /** |
3898 | * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace. | |
3899 | * @p: the task in question. | |
3900 | * @policy: new policy. | |
3901 | * @param: structure containing the new RT priority. | |
3902 | * | |
3903 | * Just like sched_setscheduler, only don't bother checking if the | |
3904 | * current context has permission. For example, this is needed in | |
3905 | * stop_machine(): we create temporary high priority worker threads, | |
3906 | * but our caller might not have that capability. | |
e69f6186 YB |
3907 | * |
3908 | * Return: 0 on success. An error code otherwise. | |
961ccddd RR |
3909 | */ |
3910 | int sched_setscheduler_nocheck(struct task_struct *p, int policy, | |
fe7de49f | 3911 | const struct sched_param *param) |
961ccddd | 3912 | { |
7479f3c9 | 3913 | return _sched_setscheduler(p, policy, param, false); |
961ccddd RR |
3914 | } |
3915 | ||
95cdf3b7 IM |
3916 | static int |
3917 | do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param) | |
1da177e4 | 3918 | { |
1da177e4 LT |
3919 | struct sched_param lparam; |
3920 | struct task_struct *p; | |
36c8b586 | 3921 | int retval; |
1da177e4 LT |
3922 | |
3923 | if (!param || pid < 0) | |
3924 | return -EINVAL; | |
3925 | if (copy_from_user(&lparam, param, sizeof(struct sched_param))) | |
3926 | return -EFAULT; | |
5fe1d75f ON |
3927 | |
3928 | rcu_read_lock(); | |
3929 | retval = -ESRCH; | |
1da177e4 | 3930 | p = find_process_by_pid(pid); |
5fe1d75f ON |
3931 | if (p != NULL) |
3932 | retval = sched_setscheduler(p, policy, &lparam); | |
3933 | rcu_read_unlock(); | |
36c8b586 | 3934 | |
1da177e4 LT |
3935 | return retval; |
3936 | } | |
3937 | ||
d50dde5a DF |
3938 | /* |
3939 | * Mimics kernel/events/core.c perf_copy_attr(). | |
3940 | */ | |
3941 | static int sched_copy_attr(struct sched_attr __user *uattr, | |
3942 | struct sched_attr *attr) | |
3943 | { | |
3944 | u32 size; | |
3945 | int ret; | |
3946 | ||
3947 | if (!access_ok(VERIFY_WRITE, uattr, SCHED_ATTR_SIZE_VER0)) | |
3948 | return -EFAULT; | |
3949 | ||
3950 | /* | |
3951 | * zero the full structure, so that a short copy will be nice. | |
3952 | */ | |
3953 | memset(attr, 0, sizeof(*attr)); | |
3954 | ||
3955 | ret = get_user(size, &uattr->size); | |
3956 | if (ret) | |
3957 | return ret; | |
3958 | ||
3959 | if (size > PAGE_SIZE) /* silly large */ | |
3960 | goto err_size; | |
3961 | ||
3962 | if (!size) /* abi compat */ | |
3963 | size = SCHED_ATTR_SIZE_VER0; | |
3964 | ||
3965 | if (size < SCHED_ATTR_SIZE_VER0) | |
3966 | goto err_size; | |
3967 | ||
3968 | /* | |
3969 | * If we're handed a bigger struct than we know of, | |
3970 | * ensure all the unknown bits are 0 - i.e. new | |
3971 | * user-space does not rely on any kernel feature | |
3972 | * extensions we dont know about yet. | |
3973 | */ | |
3974 | if (size > sizeof(*attr)) { | |
3975 | unsigned char __user *addr; | |
3976 | unsigned char __user *end; | |
3977 | unsigned char val; | |
3978 | ||
3979 | addr = (void __user *)uattr + sizeof(*attr); | |
3980 | end = (void __user *)uattr + size; | |
3981 | ||
3982 | for (; addr < end; addr++) { | |
3983 | ret = get_user(val, addr); | |
3984 | if (ret) | |
3985 | return ret; | |
3986 | if (val) | |
3987 | goto err_size; | |
3988 | } | |
3989 | size = sizeof(*attr); | |
3990 | } | |
3991 | ||
3992 | ret = copy_from_user(attr, uattr, size); | |
3993 | if (ret) | |
3994 | return -EFAULT; | |
3995 | ||
3996 | /* | |
3997 | * XXX: do we want to be lenient like existing syscalls; or do we want | |
3998 | * to be strict and return an error on out-of-bounds values? | |
3999 | */ | |
75e45d51 | 4000 | attr->sched_nice = clamp(attr->sched_nice, MIN_NICE, MAX_NICE); |
d50dde5a | 4001 | |
e78c7bca | 4002 | return 0; |
d50dde5a DF |
4003 | |
4004 | err_size: | |
4005 | put_user(sizeof(*attr), &uattr->size); | |
e78c7bca | 4006 | return -E2BIG; |
d50dde5a DF |
4007 | } |
4008 | ||
1da177e4 LT |
4009 | /** |
4010 | * sys_sched_setscheduler - set/change the scheduler policy and RT priority | |
4011 | * @pid: the pid in question. | |
4012 | * @policy: new policy. | |
4013 | * @param: structure containing the new RT priority. | |
e69f6186 YB |
4014 | * |
4015 | * Return: 0 on success. An error code otherwise. | |
1da177e4 | 4016 | */ |
5add95d4 HC |
4017 | SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy, |
4018 | struct sched_param __user *, param) | |
1da177e4 | 4019 | { |
c21761f1 JB |
4020 | /* negative values for policy are not valid */ |
4021 | if (policy < 0) | |
4022 | return -EINVAL; | |
4023 | ||
1da177e4 LT |
4024 | return do_sched_setscheduler(pid, policy, param); |
4025 | } | |
4026 | ||
4027 | /** | |
4028 | * sys_sched_setparam - set/change the RT priority of a thread | |
4029 | * @pid: the pid in question. | |
4030 | * @param: structure containing the new RT priority. | |
e69f6186 YB |
4031 | * |
4032 | * Return: 0 on success. An error code otherwise. | |
1da177e4 | 4033 | */ |
5add95d4 | 4034 | SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param) |
1da177e4 | 4035 | { |
c13db6b1 | 4036 | return do_sched_setscheduler(pid, SETPARAM_POLICY, param); |
1da177e4 LT |
4037 | } |
4038 | ||
d50dde5a DF |
4039 | /** |
4040 | * sys_sched_setattr - same as above, but with extended sched_attr | |
4041 | * @pid: the pid in question. | |
5778fccf | 4042 | * @uattr: structure containing the extended parameters. |
db66d756 | 4043 | * @flags: for future extension. |
d50dde5a | 4044 | */ |
6d35ab48 PZ |
4045 | SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr, |
4046 | unsigned int, flags) | |
d50dde5a DF |
4047 | { |
4048 | struct sched_attr attr; | |
4049 | struct task_struct *p; | |
4050 | int retval; | |
4051 | ||
6d35ab48 | 4052 | if (!uattr || pid < 0 || flags) |
d50dde5a DF |
4053 | return -EINVAL; |
4054 | ||
143cf23d MK |
4055 | retval = sched_copy_attr(uattr, &attr); |
4056 | if (retval) | |
4057 | return retval; | |
d50dde5a | 4058 | |
b14ed2c2 | 4059 | if ((int)attr.sched_policy < 0) |
dbdb2275 | 4060 | return -EINVAL; |
d50dde5a DF |
4061 | |
4062 | rcu_read_lock(); | |
4063 | retval = -ESRCH; | |
4064 | p = find_process_by_pid(pid); | |
4065 | if (p != NULL) | |
4066 | retval = sched_setattr(p, &attr); | |
4067 | rcu_read_unlock(); | |
4068 | ||
4069 | return retval; | |
4070 | } | |
4071 | ||
1da177e4 LT |
4072 | /** |
4073 | * sys_sched_getscheduler - get the policy (scheduling class) of a thread | |
4074 | * @pid: the pid in question. | |
e69f6186 YB |
4075 | * |
4076 | * Return: On success, the policy of the thread. Otherwise, a negative error | |
4077 | * code. | |
1da177e4 | 4078 | */ |
5add95d4 | 4079 | SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid) |
1da177e4 | 4080 | { |
36c8b586 | 4081 | struct task_struct *p; |
3a5c359a | 4082 | int retval; |
1da177e4 LT |
4083 | |
4084 | if (pid < 0) | |
3a5c359a | 4085 | return -EINVAL; |
1da177e4 LT |
4086 | |
4087 | retval = -ESRCH; | |
5fe85be0 | 4088 | rcu_read_lock(); |
1da177e4 LT |
4089 | p = find_process_by_pid(pid); |
4090 | if (p) { | |
4091 | retval = security_task_getscheduler(p); | |
4092 | if (!retval) | |
ca94c442 LP |
4093 | retval = p->policy |
4094 | | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0); | |
1da177e4 | 4095 | } |
5fe85be0 | 4096 | rcu_read_unlock(); |
1da177e4 LT |
4097 | return retval; |
4098 | } | |
4099 | ||
4100 | /** | |
ca94c442 | 4101 | * sys_sched_getparam - get the RT priority of a thread |
1da177e4 LT |
4102 | * @pid: the pid in question. |
4103 | * @param: structure containing the RT priority. | |
e69f6186 YB |
4104 | * |
4105 | * Return: On success, 0 and the RT priority is in @param. Otherwise, an error | |
4106 | * code. | |
1da177e4 | 4107 | */ |
5add95d4 | 4108 | SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param) |
1da177e4 | 4109 | { |
ce5f7f82 | 4110 | struct sched_param lp = { .sched_priority = 0 }; |
36c8b586 | 4111 | struct task_struct *p; |
3a5c359a | 4112 | int retval; |
1da177e4 LT |
4113 | |
4114 | if (!param || pid < 0) | |
3a5c359a | 4115 | return -EINVAL; |
1da177e4 | 4116 | |
5fe85be0 | 4117 | rcu_read_lock(); |
1da177e4 LT |
4118 | p = find_process_by_pid(pid); |
4119 | retval = -ESRCH; | |
4120 | if (!p) | |
4121 | goto out_unlock; | |
4122 | ||
4123 | retval = security_task_getscheduler(p); | |
4124 | if (retval) | |
4125 | goto out_unlock; | |
4126 | ||
ce5f7f82 PZ |
4127 | if (task_has_rt_policy(p)) |
4128 | lp.sched_priority = p->rt_priority; | |
5fe85be0 | 4129 | rcu_read_unlock(); |
1da177e4 LT |
4130 | |
4131 | /* | |
4132 | * This one might sleep, we cannot do it with a spinlock held ... | |
4133 | */ | |
4134 | retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0; | |
4135 | ||
1da177e4 LT |
4136 | return retval; |
4137 | ||
4138 | out_unlock: | |
5fe85be0 | 4139 | rcu_read_unlock(); |
1da177e4 LT |
4140 | return retval; |
4141 | } | |
4142 | ||
d50dde5a DF |
4143 | static int sched_read_attr(struct sched_attr __user *uattr, |
4144 | struct sched_attr *attr, | |
4145 | unsigned int usize) | |
4146 | { | |
4147 | int ret; | |
4148 | ||
4149 | if (!access_ok(VERIFY_WRITE, uattr, usize)) | |
4150 | return -EFAULT; | |
4151 | ||
4152 | /* | |
4153 | * If we're handed a smaller struct than we know of, | |
4154 | * ensure all the unknown bits are 0 - i.e. old | |
4155 | * user-space does not get uncomplete information. | |
4156 | */ | |
4157 | if (usize < sizeof(*attr)) { | |
4158 | unsigned char *addr; | |
4159 | unsigned char *end; | |
4160 | ||
4161 | addr = (void *)attr + usize; | |
4162 | end = (void *)attr + sizeof(*attr); | |
4163 | ||
4164 | for (; addr < end; addr++) { | |
4165 | if (*addr) | |
22400674 | 4166 | return -EFBIG; |
d50dde5a DF |
4167 | } |
4168 | ||
4169 | attr->size = usize; | |
4170 | } | |
4171 | ||
4efbc454 | 4172 | ret = copy_to_user(uattr, attr, attr->size); |
d50dde5a DF |
4173 | if (ret) |
4174 | return -EFAULT; | |
4175 | ||
22400674 | 4176 | return 0; |
d50dde5a DF |
4177 | } |
4178 | ||
4179 | /** | |
aab03e05 | 4180 | * sys_sched_getattr - similar to sched_getparam, but with sched_attr |
d50dde5a | 4181 | * @pid: the pid in question. |
5778fccf | 4182 | * @uattr: structure containing the extended parameters. |
d50dde5a | 4183 | * @size: sizeof(attr) for fwd/bwd comp. |
db66d756 | 4184 | * @flags: for future extension. |
d50dde5a | 4185 | */ |
6d35ab48 PZ |
4186 | SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr, |
4187 | unsigned int, size, unsigned int, flags) | |
d50dde5a DF |
4188 | { |
4189 | struct sched_attr attr = { | |
4190 | .size = sizeof(struct sched_attr), | |
4191 | }; | |
4192 | struct task_struct *p; | |
4193 | int retval; | |
4194 | ||
4195 | if (!uattr || pid < 0 || size > PAGE_SIZE || | |
6d35ab48 | 4196 | size < SCHED_ATTR_SIZE_VER0 || flags) |
d50dde5a DF |
4197 | return -EINVAL; |
4198 | ||
4199 | rcu_read_lock(); | |
4200 | p = find_process_by_pid(pid); | |
4201 | retval = -ESRCH; | |
4202 | if (!p) | |
4203 | goto out_unlock; | |
4204 | ||
4205 | retval = security_task_getscheduler(p); | |
4206 | if (retval) | |
4207 | goto out_unlock; | |
4208 | ||
4209 | attr.sched_policy = p->policy; | |
7479f3c9 PZ |
4210 | if (p->sched_reset_on_fork) |
4211 | attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK; | |
aab03e05 DF |
4212 | if (task_has_dl_policy(p)) |
4213 | __getparam_dl(p, &attr); | |
4214 | else if (task_has_rt_policy(p)) | |
d50dde5a DF |
4215 | attr.sched_priority = p->rt_priority; |
4216 | else | |
d0ea0268 | 4217 | attr.sched_nice = task_nice(p); |
d50dde5a DF |
4218 | |
4219 | rcu_read_unlock(); | |
4220 | ||
4221 | retval = sched_read_attr(uattr, &attr, size); | |
4222 | return retval; | |
4223 | ||
4224 | out_unlock: | |
4225 | rcu_read_unlock(); | |
4226 | return retval; | |
4227 | } | |
4228 | ||
96f874e2 | 4229 | long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) |
1da177e4 | 4230 | { |
5a16f3d3 | 4231 | cpumask_var_t cpus_allowed, new_mask; |
36c8b586 IM |
4232 | struct task_struct *p; |
4233 | int retval; | |
1da177e4 | 4234 | |
23f5d142 | 4235 | rcu_read_lock(); |
1da177e4 LT |
4236 | |
4237 | p = find_process_by_pid(pid); | |
4238 | if (!p) { | |
23f5d142 | 4239 | rcu_read_unlock(); |
1da177e4 LT |
4240 | return -ESRCH; |
4241 | } | |
4242 | ||
23f5d142 | 4243 | /* Prevent p going away */ |
1da177e4 | 4244 | get_task_struct(p); |
23f5d142 | 4245 | rcu_read_unlock(); |
1da177e4 | 4246 | |
14a40ffc TH |
4247 | if (p->flags & PF_NO_SETAFFINITY) { |
4248 | retval = -EINVAL; | |
4249 | goto out_put_task; | |
4250 | } | |
5a16f3d3 RR |
4251 | if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) { |
4252 | retval = -ENOMEM; | |
4253 | goto out_put_task; | |
4254 | } | |
4255 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) { | |
4256 | retval = -ENOMEM; | |
4257 | goto out_free_cpus_allowed; | |
4258 | } | |
1da177e4 | 4259 | retval = -EPERM; |
4c44aaaf EB |
4260 | if (!check_same_owner(p)) { |
4261 | rcu_read_lock(); | |
4262 | if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) { | |
4263 | rcu_read_unlock(); | |
16303ab2 | 4264 | goto out_free_new_mask; |
4c44aaaf EB |
4265 | } |
4266 | rcu_read_unlock(); | |
4267 | } | |
1da177e4 | 4268 | |
b0ae1981 | 4269 | retval = security_task_setscheduler(p); |
e7834f8f | 4270 | if (retval) |
16303ab2 | 4271 | goto out_free_new_mask; |
e7834f8f | 4272 | |
e4099a5e PZ |
4273 | |
4274 | cpuset_cpus_allowed(p, cpus_allowed); | |
4275 | cpumask_and(new_mask, in_mask, cpus_allowed); | |
4276 | ||
332ac17e DF |
4277 | /* |
4278 | * Since bandwidth control happens on root_domain basis, | |
4279 | * if admission test is enabled, we only admit -deadline | |
4280 | * tasks allowed to run on all the CPUs in the task's | |
4281 | * root_domain. | |
4282 | */ | |
4283 | #ifdef CONFIG_SMP | |
f1e3a093 KT |
4284 | if (task_has_dl_policy(p) && dl_bandwidth_enabled()) { |
4285 | rcu_read_lock(); | |
4286 | if (!cpumask_subset(task_rq(p)->rd->span, new_mask)) { | |
332ac17e | 4287 | retval = -EBUSY; |
f1e3a093 | 4288 | rcu_read_unlock(); |
16303ab2 | 4289 | goto out_free_new_mask; |
332ac17e | 4290 | } |
f1e3a093 | 4291 | rcu_read_unlock(); |
332ac17e DF |
4292 | } |
4293 | #endif | |
49246274 | 4294 | again: |
5a16f3d3 | 4295 | retval = set_cpus_allowed_ptr(p, new_mask); |
1da177e4 | 4296 | |
8707d8b8 | 4297 | if (!retval) { |
5a16f3d3 RR |
4298 | cpuset_cpus_allowed(p, cpus_allowed); |
4299 | if (!cpumask_subset(new_mask, cpus_allowed)) { | |
8707d8b8 PM |
4300 | /* |
4301 | * We must have raced with a concurrent cpuset | |
4302 | * update. Just reset the cpus_allowed to the | |
4303 | * cpuset's cpus_allowed | |
4304 | */ | |
5a16f3d3 | 4305 | cpumask_copy(new_mask, cpus_allowed); |
8707d8b8 PM |
4306 | goto again; |
4307 | } | |
4308 | } | |
16303ab2 | 4309 | out_free_new_mask: |
5a16f3d3 RR |
4310 | free_cpumask_var(new_mask); |
4311 | out_free_cpus_allowed: | |
4312 | free_cpumask_var(cpus_allowed); | |
4313 | out_put_task: | |
1da177e4 | 4314 | put_task_struct(p); |
1da177e4 LT |
4315 | return retval; |
4316 | } | |
4317 | ||
4318 | static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len, | |
96f874e2 | 4319 | struct cpumask *new_mask) |
1da177e4 | 4320 | { |
96f874e2 RR |
4321 | if (len < cpumask_size()) |
4322 | cpumask_clear(new_mask); | |
4323 | else if (len > cpumask_size()) | |
4324 | len = cpumask_size(); | |
4325 | ||
1da177e4 LT |
4326 | return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0; |
4327 | } | |
4328 | ||
4329 | /** | |
4330 | * sys_sched_setaffinity - set the cpu affinity of a process | |
4331 | * @pid: pid of the process | |
4332 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr | |
4333 | * @user_mask_ptr: user-space pointer to the new cpu mask | |
e69f6186 YB |
4334 | * |
4335 | * Return: 0 on success. An error code otherwise. | |
1da177e4 | 4336 | */ |
5add95d4 HC |
4337 | SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len, |
4338 | unsigned long __user *, user_mask_ptr) | |
1da177e4 | 4339 | { |
5a16f3d3 | 4340 | cpumask_var_t new_mask; |
1da177e4 LT |
4341 | int retval; |
4342 | ||
5a16f3d3 RR |
4343 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) |
4344 | return -ENOMEM; | |
1da177e4 | 4345 | |
5a16f3d3 RR |
4346 | retval = get_user_cpu_mask(user_mask_ptr, len, new_mask); |
4347 | if (retval == 0) | |
4348 | retval = sched_setaffinity(pid, new_mask); | |
4349 | free_cpumask_var(new_mask); | |
4350 | return retval; | |
1da177e4 LT |
4351 | } |
4352 | ||
96f874e2 | 4353 | long sched_getaffinity(pid_t pid, struct cpumask *mask) |
1da177e4 | 4354 | { |
36c8b586 | 4355 | struct task_struct *p; |
31605683 | 4356 | unsigned long flags; |
1da177e4 | 4357 | int retval; |
1da177e4 | 4358 | |
23f5d142 | 4359 | rcu_read_lock(); |
1da177e4 LT |
4360 | |
4361 | retval = -ESRCH; | |
4362 | p = find_process_by_pid(pid); | |
4363 | if (!p) | |
4364 | goto out_unlock; | |
4365 | ||
e7834f8f DQ |
4366 | retval = security_task_getscheduler(p); |
4367 | if (retval) | |
4368 | goto out_unlock; | |
4369 | ||
013fdb80 | 4370 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
6acce3ef | 4371 | cpumask_and(mask, &p->cpus_allowed, cpu_active_mask); |
013fdb80 | 4372 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
1da177e4 LT |
4373 | |
4374 | out_unlock: | |
23f5d142 | 4375 | rcu_read_unlock(); |
1da177e4 | 4376 | |
9531b62f | 4377 | return retval; |
1da177e4 LT |
4378 | } |
4379 | ||
4380 | /** | |
4381 | * sys_sched_getaffinity - get the cpu affinity of a process | |
4382 | * @pid: pid of the process | |
4383 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr | |
4384 | * @user_mask_ptr: user-space pointer to hold the current cpu mask | |
e69f6186 YB |
4385 | * |
4386 | * Return: 0 on success. An error code otherwise. | |
1da177e4 | 4387 | */ |
5add95d4 HC |
4388 | SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len, |
4389 | unsigned long __user *, user_mask_ptr) | |
1da177e4 LT |
4390 | { |
4391 | int ret; | |
f17c8607 | 4392 | cpumask_var_t mask; |
1da177e4 | 4393 | |
84fba5ec | 4394 | if ((len * BITS_PER_BYTE) < nr_cpu_ids) |
cd3d8031 KM |
4395 | return -EINVAL; |
4396 | if (len & (sizeof(unsigned long)-1)) | |
1da177e4 LT |
4397 | return -EINVAL; |
4398 | ||
f17c8607 RR |
4399 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) |
4400 | return -ENOMEM; | |
1da177e4 | 4401 | |
f17c8607 RR |
4402 | ret = sched_getaffinity(pid, mask); |
4403 | if (ret == 0) { | |
8bc037fb | 4404 | size_t retlen = min_t(size_t, len, cpumask_size()); |
cd3d8031 KM |
4405 | |
4406 | if (copy_to_user(user_mask_ptr, mask, retlen)) | |
f17c8607 RR |
4407 | ret = -EFAULT; |
4408 | else | |
cd3d8031 | 4409 | ret = retlen; |
f17c8607 RR |
4410 | } |
4411 | free_cpumask_var(mask); | |
1da177e4 | 4412 | |
f17c8607 | 4413 | return ret; |
1da177e4 LT |
4414 | } |
4415 | ||
4416 | /** | |
4417 | * sys_sched_yield - yield the current processor to other threads. | |
4418 | * | |
dd41f596 IM |
4419 | * This function yields the current CPU to other tasks. If there are no |
4420 | * other threads running on this CPU then this function will return. | |
e69f6186 YB |
4421 | * |
4422 | * Return: 0. | |
1da177e4 | 4423 | */ |
5add95d4 | 4424 | SYSCALL_DEFINE0(sched_yield) |
1da177e4 | 4425 | { |
70b97a7f | 4426 | struct rq *rq = this_rq_lock(); |
1da177e4 | 4427 | |
2d72376b | 4428 | schedstat_inc(rq, yld_count); |
4530d7ab | 4429 | current->sched_class->yield_task(rq); |
1da177e4 LT |
4430 | |
4431 | /* | |
4432 | * Since we are going to call schedule() anyway, there's | |
4433 | * no need to preempt or enable interrupts: | |
4434 | */ | |
4435 | __release(rq->lock); | |
8a25d5de | 4436 | spin_release(&rq->lock.dep_map, 1, _THIS_IP_); |
9828ea9d | 4437 | do_raw_spin_unlock(&rq->lock); |
ba74c144 | 4438 | sched_preempt_enable_no_resched(); |
1da177e4 LT |
4439 | |
4440 | schedule(); | |
4441 | ||
4442 | return 0; | |
4443 | } | |
4444 | ||
02b67cc3 | 4445 | int __sched _cond_resched(void) |
1da177e4 | 4446 | { |
d86ee480 | 4447 | if (should_resched()) { |
a18b5d01 | 4448 | preempt_schedule_common(); |
1da177e4 LT |
4449 | return 1; |
4450 | } | |
4451 | return 0; | |
4452 | } | |
02b67cc3 | 4453 | EXPORT_SYMBOL(_cond_resched); |
1da177e4 LT |
4454 | |
4455 | /* | |
613afbf8 | 4456 | * __cond_resched_lock() - if a reschedule is pending, drop the given lock, |
1da177e4 LT |
4457 | * call schedule, and on return reacquire the lock. |
4458 | * | |
41a2d6cf | 4459 | * This works OK both with and without CONFIG_PREEMPT. We do strange low-level |
1da177e4 LT |
4460 | * operations here to prevent schedule() from being called twice (once via |
4461 | * spin_unlock(), once by hand). | |
4462 | */ | |
613afbf8 | 4463 | int __cond_resched_lock(spinlock_t *lock) |
1da177e4 | 4464 | { |
d86ee480 | 4465 | int resched = should_resched(); |
6df3cecb JK |
4466 | int ret = 0; |
4467 | ||
f607c668 PZ |
4468 | lockdep_assert_held(lock); |
4469 | ||
4a81e832 | 4470 | if (spin_needbreak(lock) || resched) { |
1da177e4 | 4471 | spin_unlock(lock); |
d86ee480 | 4472 | if (resched) |
a18b5d01 | 4473 | preempt_schedule_common(); |
95c354fe NP |
4474 | else |
4475 | cpu_relax(); | |
6df3cecb | 4476 | ret = 1; |
1da177e4 | 4477 | spin_lock(lock); |
1da177e4 | 4478 | } |
6df3cecb | 4479 | return ret; |
1da177e4 | 4480 | } |
613afbf8 | 4481 | EXPORT_SYMBOL(__cond_resched_lock); |
1da177e4 | 4482 | |
613afbf8 | 4483 | int __sched __cond_resched_softirq(void) |
1da177e4 LT |
4484 | { |
4485 | BUG_ON(!in_softirq()); | |
4486 | ||
d86ee480 | 4487 | if (should_resched()) { |
98d82567 | 4488 | local_bh_enable(); |
a18b5d01 | 4489 | preempt_schedule_common(); |
1da177e4 LT |
4490 | local_bh_disable(); |
4491 | return 1; | |
4492 | } | |
4493 | return 0; | |
4494 | } | |
613afbf8 | 4495 | EXPORT_SYMBOL(__cond_resched_softirq); |
1da177e4 | 4496 | |
1da177e4 LT |
4497 | /** |
4498 | * yield - yield the current processor to other threads. | |
4499 | * | |
8e3fabfd PZ |
4500 | * Do not ever use this function, there's a 99% chance you're doing it wrong. |
4501 | * | |
4502 | * The scheduler is at all times free to pick the calling task as the most | |
4503 | * eligible task to run, if removing the yield() call from your code breaks | |
4504 | * it, its already broken. | |
4505 | * | |
4506 | * Typical broken usage is: | |
4507 | * | |
4508 | * while (!event) | |
4509 | * yield(); | |
4510 | * | |
4511 | * where one assumes that yield() will let 'the other' process run that will | |
4512 | * make event true. If the current task is a SCHED_FIFO task that will never | |
4513 | * happen. Never use yield() as a progress guarantee!! | |
4514 | * | |
4515 | * If you want to use yield() to wait for something, use wait_event(). | |
4516 | * If you want to use yield() to be 'nice' for others, use cond_resched(). | |
4517 | * If you still want to use yield(), do not! | |
1da177e4 LT |
4518 | */ |
4519 | void __sched yield(void) | |
4520 | { | |
4521 | set_current_state(TASK_RUNNING); | |
4522 | sys_sched_yield(); | |
4523 | } | |
1da177e4 LT |
4524 | EXPORT_SYMBOL(yield); |
4525 | ||
d95f4122 MG |
4526 | /** |
4527 | * yield_to - yield the current processor to another thread in | |
4528 | * your thread group, or accelerate that thread toward the | |
4529 | * processor it's on. | |
16addf95 RD |
4530 | * @p: target task |
4531 | * @preempt: whether task preemption is allowed or not | |
d95f4122 MG |
4532 | * |
4533 | * It's the caller's job to ensure that the target task struct | |
4534 | * can't go away on us before we can do any checks. | |
4535 | * | |
e69f6186 | 4536 | * Return: |
7b270f60 PZ |
4537 | * true (>0) if we indeed boosted the target task. |
4538 | * false (0) if we failed to boost the target. | |
4539 | * -ESRCH if there's no task to yield to. | |
d95f4122 | 4540 | */ |
fa93384f | 4541 | int __sched yield_to(struct task_struct *p, bool preempt) |
d95f4122 MG |
4542 | { |
4543 | struct task_struct *curr = current; | |
4544 | struct rq *rq, *p_rq; | |
4545 | unsigned long flags; | |
c3c18640 | 4546 | int yielded = 0; |
d95f4122 MG |
4547 | |
4548 | local_irq_save(flags); | |
4549 | rq = this_rq(); | |
4550 | ||
4551 | again: | |
4552 | p_rq = task_rq(p); | |
7b270f60 PZ |
4553 | /* |
4554 | * If we're the only runnable task on the rq and target rq also | |
4555 | * has only one task, there's absolutely no point in yielding. | |
4556 | */ | |
4557 | if (rq->nr_running == 1 && p_rq->nr_running == 1) { | |
4558 | yielded = -ESRCH; | |
4559 | goto out_irq; | |
4560 | } | |
4561 | ||
d95f4122 | 4562 | double_rq_lock(rq, p_rq); |
39e24d8f | 4563 | if (task_rq(p) != p_rq) { |
d95f4122 MG |
4564 | double_rq_unlock(rq, p_rq); |
4565 | goto again; | |
4566 | } | |
4567 | ||
4568 | if (!curr->sched_class->yield_to_task) | |
7b270f60 | 4569 | goto out_unlock; |
d95f4122 MG |
4570 | |
4571 | if (curr->sched_class != p->sched_class) | |
7b270f60 | 4572 | goto out_unlock; |
d95f4122 MG |
4573 | |
4574 | if (task_running(p_rq, p) || p->state) | |
7b270f60 | 4575 | goto out_unlock; |
d95f4122 MG |
4576 | |
4577 | yielded = curr->sched_class->yield_to_task(rq, p, preempt); | |
6d1cafd8 | 4578 | if (yielded) { |
d95f4122 | 4579 | schedstat_inc(rq, yld_count); |
6d1cafd8 VP |
4580 | /* |
4581 | * Make p's CPU reschedule; pick_next_entity takes care of | |
4582 | * fairness. | |
4583 | */ | |
4584 | if (preempt && rq != p_rq) | |
8875125e | 4585 | resched_curr(p_rq); |
6d1cafd8 | 4586 | } |
d95f4122 | 4587 | |
7b270f60 | 4588 | out_unlock: |
d95f4122 | 4589 | double_rq_unlock(rq, p_rq); |
7b270f60 | 4590 | out_irq: |
d95f4122 MG |
4591 | local_irq_restore(flags); |
4592 | ||
7b270f60 | 4593 | if (yielded > 0) |
d95f4122 MG |
4594 | schedule(); |
4595 | ||
4596 | return yielded; | |
4597 | } | |
4598 | EXPORT_SYMBOL_GPL(yield_to); | |
4599 | ||
1da177e4 | 4600 | /* |
41a2d6cf | 4601 | * This task is about to go to sleep on IO. Increment rq->nr_iowait so |
1da177e4 | 4602 | * that process accounting knows that this is a task in IO wait state. |
1da177e4 | 4603 | */ |
1da177e4 LT |
4604 | long __sched io_schedule_timeout(long timeout) |
4605 | { | |
9cff8ade N |
4606 | int old_iowait = current->in_iowait; |
4607 | struct rq *rq; | |
1da177e4 LT |
4608 | long ret; |
4609 | ||
9cff8ade | 4610 | current->in_iowait = 1; |
10d784ea | 4611 | blk_schedule_flush_plug(current); |
9cff8ade | 4612 | |
0ff92245 | 4613 | delayacct_blkio_start(); |
9cff8ade | 4614 | rq = raw_rq(); |
1da177e4 LT |
4615 | atomic_inc(&rq->nr_iowait); |
4616 | ret = schedule_timeout(timeout); | |
9cff8ade | 4617 | current->in_iowait = old_iowait; |
1da177e4 | 4618 | atomic_dec(&rq->nr_iowait); |
0ff92245 | 4619 | delayacct_blkio_end(); |
9cff8ade | 4620 | |
1da177e4 LT |
4621 | return ret; |
4622 | } | |
9cff8ade | 4623 | EXPORT_SYMBOL(io_schedule_timeout); |
1da177e4 LT |
4624 | |
4625 | /** | |
4626 | * sys_sched_get_priority_max - return maximum RT priority. | |
4627 | * @policy: scheduling class. | |
4628 | * | |
e69f6186 YB |
4629 | * Return: On success, this syscall returns the maximum |
4630 | * rt_priority that can be used by a given scheduling class. | |
4631 | * On failure, a negative error code is returned. | |
1da177e4 | 4632 | */ |
5add95d4 | 4633 | SYSCALL_DEFINE1(sched_get_priority_max, int, policy) |
1da177e4 LT |
4634 | { |
4635 | int ret = -EINVAL; | |
4636 | ||
4637 | switch (policy) { | |
4638 | case SCHED_FIFO: | |
4639 | case SCHED_RR: | |
4640 | ret = MAX_USER_RT_PRIO-1; | |
4641 | break; | |
aab03e05 | 4642 | case SCHED_DEADLINE: |
1da177e4 | 4643 | case SCHED_NORMAL: |
b0a9499c | 4644 | case SCHED_BATCH: |
dd41f596 | 4645 | case SCHED_IDLE: |
1da177e4 LT |
4646 | ret = 0; |
4647 | break; | |
4648 | } | |
4649 | return ret; | |
4650 | } | |
4651 | ||
4652 | /** | |
4653 | * sys_sched_get_priority_min - return minimum RT priority. | |
4654 | * @policy: scheduling class. | |
4655 | * | |
e69f6186 YB |
4656 | * Return: On success, this syscall returns the minimum |
4657 | * rt_priority that can be used by a given scheduling class. | |
4658 | * On failure, a negative error code is returned. | |
1da177e4 | 4659 | */ |
5add95d4 | 4660 | SYSCALL_DEFINE1(sched_get_priority_min, int, policy) |
1da177e4 LT |
4661 | { |
4662 | int ret = -EINVAL; | |
4663 | ||
4664 | switch (policy) { | |
4665 | case SCHED_FIFO: | |
4666 | case SCHED_RR: | |
4667 | ret = 1; | |
4668 | break; | |
aab03e05 | 4669 | case SCHED_DEADLINE: |
1da177e4 | 4670 | case SCHED_NORMAL: |
b0a9499c | 4671 | case SCHED_BATCH: |
dd41f596 | 4672 | case SCHED_IDLE: |
1da177e4 LT |
4673 | ret = 0; |
4674 | } | |
4675 | return ret; | |
4676 | } | |
4677 | ||
4678 | /** | |
4679 | * sys_sched_rr_get_interval - return the default timeslice of a process. | |
4680 | * @pid: pid of the process. | |
4681 | * @interval: userspace pointer to the timeslice value. | |
4682 | * | |
4683 | * this syscall writes the default timeslice value of a given process | |
4684 | * into the user-space timespec buffer. A value of '0' means infinity. | |
e69f6186 YB |
4685 | * |
4686 | * Return: On success, 0 and the timeslice is in @interval. Otherwise, | |
4687 | * an error code. | |
1da177e4 | 4688 | */ |
17da2bd9 | 4689 | SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid, |
754fe8d2 | 4690 | struct timespec __user *, interval) |
1da177e4 | 4691 | { |
36c8b586 | 4692 | struct task_struct *p; |
a4ec24b4 | 4693 | unsigned int time_slice; |
dba091b9 TG |
4694 | unsigned long flags; |
4695 | struct rq *rq; | |
3a5c359a | 4696 | int retval; |
1da177e4 | 4697 | struct timespec t; |
1da177e4 LT |
4698 | |
4699 | if (pid < 0) | |
3a5c359a | 4700 | return -EINVAL; |
1da177e4 LT |
4701 | |
4702 | retval = -ESRCH; | |
1a551ae7 | 4703 | rcu_read_lock(); |
1da177e4 LT |
4704 | p = find_process_by_pid(pid); |
4705 | if (!p) | |
4706 | goto out_unlock; | |
4707 | ||
4708 | retval = security_task_getscheduler(p); | |
4709 | if (retval) | |
4710 | goto out_unlock; | |
4711 | ||
dba091b9 | 4712 | rq = task_rq_lock(p, &flags); |
a57beec5 PZ |
4713 | time_slice = 0; |
4714 | if (p->sched_class->get_rr_interval) | |
4715 | time_slice = p->sched_class->get_rr_interval(rq, p); | |
0122ec5b | 4716 | task_rq_unlock(rq, p, &flags); |
a4ec24b4 | 4717 | |
1a551ae7 | 4718 | rcu_read_unlock(); |
a4ec24b4 | 4719 | jiffies_to_timespec(time_slice, &t); |
1da177e4 | 4720 | retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0; |
1da177e4 | 4721 | return retval; |
3a5c359a | 4722 | |
1da177e4 | 4723 | out_unlock: |
1a551ae7 | 4724 | rcu_read_unlock(); |
1da177e4 LT |
4725 | return retval; |
4726 | } | |
4727 | ||
7c731e0a | 4728 | static const char stat_nam[] = TASK_STATE_TO_CHAR_STR; |
36c8b586 | 4729 | |
82a1fcb9 | 4730 | void sched_show_task(struct task_struct *p) |
1da177e4 | 4731 | { |
1da177e4 | 4732 | unsigned long free = 0; |
4e79752c | 4733 | int ppid; |
1f8a7633 | 4734 | unsigned long state = p->state; |
1da177e4 | 4735 | |
1f8a7633 TH |
4736 | if (state) |
4737 | state = __ffs(state) + 1; | |
28d0686c | 4738 | printk(KERN_INFO "%-15.15s %c", p->comm, |
2ed6e34f | 4739 | state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?'); |
4bd77321 | 4740 | #if BITS_PER_LONG == 32 |
1da177e4 | 4741 | if (state == TASK_RUNNING) |
3df0fc5b | 4742 | printk(KERN_CONT " running "); |
1da177e4 | 4743 | else |
3df0fc5b | 4744 | printk(KERN_CONT " %08lx ", thread_saved_pc(p)); |
1da177e4 LT |
4745 | #else |
4746 | if (state == TASK_RUNNING) | |
3df0fc5b | 4747 | printk(KERN_CONT " running task "); |
1da177e4 | 4748 | else |
3df0fc5b | 4749 | printk(KERN_CONT " %016lx ", thread_saved_pc(p)); |
1da177e4 LT |
4750 | #endif |
4751 | #ifdef CONFIG_DEBUG_STACK_USAGE | |
7c9f8861 | 4752 | free = stack_not_used(p); |
1da177e4 | 4753 | #endif |
a90e984c | 4754 | ppid = 0; |
4e79752c | 4755 | rcu_read_lock(); |
a90e984c ON |
4756 | if (pid_alive(p)) |
4757 | ppid = task_pid_nr(rcu_dereference(p->real_parent)); | |
4e79752c | 4758 | rcu_read_unlock(); |
3df0fc5b | 4759 | printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free, |
4e79752c | 4760 | task_pid_nr(p), ppid, |
aa47b7e0 | 4761 | (unsigned long)task_thread_info(p)->flags); |
1da177e4 | 4762 | |
3d1cb205 | 4763 | print_worker_info(KERN_INFO, p); |
5fb5e6de | 4764 | show_stack(p, NULL); |
1da177e4 LT |
4765 | } |
4766 | ||
e59e2ae2 | 4767 | void show_state_filter(unsigned long state_filter) |
1da177e4 | 4768 | { |
36c8b586 | 4769 | struct task_struct *g, *p; |
1da177e4 | 4770 | |
4bd77321 | 4771 | #if BITS_PER_LONG == 32 |
3df0fc5b PZ |
4772 | printk(KERN_INFO |
4773 | " task PC stack pid father\n"); | |
1da177e4 | 4774 | #else |
3df0fc5b PZ |
4775 | printk(KERN_INFO |
4776 | " task PC stack pid father\n"); | |
1da177e4 | 4777 | #endif |
510f5acc | 4778 | rcu_read_lock(); |
5d07f420 | 4779 | for_each_process_thread(g, p) { |
1da177e4 LT |
4780 | /* |
4781 | * reset the NMI-timeout, listing all files on a slow | |
25985edc | 4782 | * console might take a lot of time: |
1da177e4 LT |
4783 | */ |
4784 | touch_nmi_watchdog(); | |
39bc89fd | 4785 | if (!state_filter || (p->state & state_filter)) |
82a1fcb9 | 4786 | sched_show_task(p); |
5d07f420 | 4787 | } |
1da177e4 | 4788 | |
04c9167f JF |
4789 | touch_all_softlockup_watchdogs(); |
4790 | ||
dd41f596 IM |
4791 | #ifdef CONFIG_SCHED_DEBUG |
4792 | sysrq_sched_debug_show(); | |
4793 | #endif | |
510f5acc | 4794 | rcu_read_unlock(); |
e59e2ae2 IM |
4795 | /* |
4796 | * Only show locks if all tasks are dumped: | |
4797 | */ | |
93335a21 | 4798 | if (!state_filter) |
e59e2ae2 | 4799 | debug_show_all_locks(); |
1da177e4 LT |
4800 | } |
4801 | ||
0db0628d | 4802 | void init_idle_bootup_task(struct task_struct *idle) |
1df21055 | 4803 | { |
dd41f596 | 4804 | idle->sched_class = &idle_sched_class; |
1df21055 IM |
4805 | } |
4806 | ||
f340c0d1 IM |
4807 | /** |
4808 | * init_idle - set up an idle thread for a given CPU | |
4809 | * @idle: task in question | |
4810 | * @cpu: cpu the idle task belongs to | |
4811 | * | |
4812 | * NOTE: this function does not set the idle thread's NEED_RESCHED | |
4813 | * flag, to make booting more robust. | |
4814 | */ | |
0db0628d | 4815 | void init_idle(struct task_struct *idle, int cpu) |
1da177e4 | 4816 | { |
70b97a7f | 4817 | struct rq *rq = cpu_rq(cpu); |
1da177e4 LT |
4818 | unsigned long flags; |
4819 | ||
05fa785c | 4820 | raw_spin_lock_irqsave(&rq->lock, flags); |
5cbd54ef | 4821 | |
5e1576ed | 4822 | __sched_fork(0, idle); |
06b83b5f | 4823 | idle->state = TASK_RUNNING; |
dd41f596 IM |
4824 | idle->se.exec_start = sched_clock(); |
4825 | ||
1e1b6c51 | 4826 | do_set_cpus_allowed(idle, cpumask_of(cpu)); |
6506cf6c PZ |
4827 | /* |
4828 | * We're having a chicken and egg problem, even though we are | |
4829 | * holding rq->lock, the cpu isn't yet set to this cpu so the | |
4830 | * lockdep check in task_group() will fail. | |
4831 | * | |
4832 | * Similar case to sched_fork(). / Alternatively we could | |
4833 | * use task_rq_lock() here and obtain the other rq->lock. | |
4834 | * | |
4835 | * Silence PROVE_RCU | |
4836 | */ | |
4837 | rcu_read_lock(); | |
dd41f596 | 4838 | __set_task_cpu(idle, cpu); |
6506cf6c | 4839 | rcu_read_unlock(); |
1da177e4 | 4840 | |
1da177e4 | 4841 | rq->curr = rq->idle = idle; |
da0c1e65 | 4842 | idle->on_rq = TASK_ON_RQ_QUEUED; |
3ca7a440 PZ |
4843 | #if defined(CONFIG_SMP) |
4844 | idle->on_cpu = 1; | |
4866cde0 | 4845 | #endif |
05fa785c | 4846 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
1da177e4 LT |
4847 | |
4848 | /* Set the preempt count _outside_ the spinlocks! */ | |
01028747 | 4849 | init_idle_preempt_count(idle, cpu); |
55cd5340 | 4850 | |
dd41f596 IM |
4851 | /* |
4852 | * The idle tasks have their own, simple scheduling class: | |
4853 | */ | |
4854 | idle->sched_class = &idle_sched_class; | |
868baf07 | 4855 | ftrace_graph_init_idle_task(idle, cpu); |
45eacc69 | 4856 | vtime_init_idle(idle, cpu); |
f1c6f1a7 CE |
4857 | #if defined(CONFIG_SMP) |
4858 | sprintf(idle->comm, "%s/%d", INIT_TASK_COMM, cpu); | |
4859 | #endif | |
19978ca6 IM |
4860 | } |
4861 | ||
f82f8042 JL |
4862 | int cpuset_cpumask_can_shrink(const struct cpumask *cur, |
4863 | const struct cpumask *trial) | |
4864 | { | |
4865 | int ret = 1, trial_cpus; | |
4866 | struct dl_bw *cur_dl_b; | |
4867 | unsigned long flags; | |
4868 | ||
bb2bc55a MG |
4869 | if (!cpumask_weight(cur)) |
4870 | return ret; | |
4871 | ||
75e23e49 | 4872 | rcu_read_lock_sched(); |
f82f8042 JL |
4873 | cur_dl_b = dl_bw_of(cpumask_any(cur)); |
4874 | trial_cpus = cpumask_weight(trial); | |
4875 | ||
4876 | raw_spin_lock_irqsave(&cur_dl_b->lock, flags); | |
4877 | if (cur_dl_b->bw != -1 && | |
4878 | cur_dl_b->bw * trial_cpus < cur_dl_b->total_bw) | |
4879 | ret = 0; | |
4880 | raw_spin_unlock_irqrestore(&cur_dl_b->lock, flags); | |
75e23e49 | 4881 | rcu_read_unlock_sched(); |
f82f8042 JL |
4882 | |
4883 | return ret; | |
4884 | } | |
4885 | ||
7f51412a JL |
4886 | int task_can_attach(struct task_struct *p, |
4887 | const struct cpumask *cs_cpus_allowed) | |
4888 | { | |
4889 | int ret = 0; | |
4890 | ||
4891 | /* | |
4892 | * Kthreads which disallow setaffinity shouldn't be moved | |
4893 | * to a new cpuset; we don't want to change their cpu | |
4894 | * affinity and isolating such threads by their set of | |
4895 | * allowed nodes is unnecessary. Thus, cpusets are not | |
4896 | * applicable for such threads. This prevents checking for | |
4897 | * success of set_cpus_allowed_ptr() on all attached tasks | |
4898 | * before cpus_allowed may be changed. | |
4899 | */ | |
4900 | if (p->flags & PF_NO_SETAFFINITY) { | |
4901 | ret = -EINVAL; | |
4902 | goto out; | |
4903 | } | |
4904 | ||
4905 | #ifdef CONFIG_SMP | |
4906 | if (dl_task(p) && !cpumask_intersects(task_rq(p)->rd->span, | |
4907 | cs_cpus_allowed)) { | |
4908 | unsigned int dest_cpu = cpumask_any_and(cpu_active_mask, | |
4909 | cs_cpus_allowed); | |
75e23e49 | 4910 | struct dl_bw *dl_b; |
7f51412a JL |
4911 | bool overflow; |
4912 | int cpus; | |
4913 | unsigned long flags; | |
4914 | ||
75e23e49 JL |
4915 | rcu_read_lock_sched(); |
4916 | dl_b = dl_bw_of(dest_cpu); | |
7f51412a JL |
4917 | raw_spin_lock_irqsave(&dl_b->lock, flags); |
4918 | cpus = dl_bw_cpus(dest_cpu); | |
4919 | overflow = __dl_overflow(dl_b, cpus, 0, p->dl.dl_bw); | |
4920 | if (overflow) | |
4921 | ret = -EBUSY; | |
4922 | else { | |
4923 | /* | |
4924 | * We reserve space for this task in the destination | |
4925 | * root_domain, as we can't fail after this point. | |
4926 | * We will free resources in the source root_domain | |
4927 | * later on (see set_cpus_allowed_dl()). | |
4928 | */ | |
4929 | __dl_add(dl_b, p->dl.dl_bw); | |
4930 | } | |
4931 | raw_spin_unlock_irqrestore(&dl_b->lock, flags); | |
75e23e49 | 4932 | rcu_read_unlock_sched(); |
7f51412a JL |
4933 | |
4934 | } | |
4935 | #endif | |
4936 | out: | |
4937 | return ret; | |
4938 | } | |
4939 | ||
1da177e4 | 4940 | #ifdef CONFIG_SMP |
1da177e4 | 4941 | |
e6628d5b MG |
4942 | #ifdef CONFIG_NUMA_BALANCING |
4943 | /* Migrate current task p to target_cpu */ | |
4944 | int migrate_task_to(struct task_struct *p, int target_cpu) | |
4945 | { | |
4946 | struct migration_arg arg = { p, target_cpu }; | |
4947 | int curr_cpu = task_cpu(p); | |
4948 | ||
4949 | if (curr_cpu == target_cpu) | |
4950 | return 0; | |
4951 | ||
4952 | if (!cpumask_test_cpu(target_cpu, tsk_cpus_allowed(p))) | |
4953 | return -EINVAL; | |
4954 | ||
4955 | /* TODO: This is not properly updating schedstats */ | |
4956 | ||
286549dc | 4957 | trace_sched_move_numa(p, curr_cpu, target_cpu); |
e6628d5b MG |
4958 | return stop_one_cpu(curr_cpu, migration_cpu_stop, &arg); |
4959 | } | |
0ec8aa00 PZ |
4960 | |
4961 | /* | |
4962 | * Requeue a task on a given node and accurately track the number of NUMA | |
4963 | * tasks on the runqueues | |
4964 | */ | |
4965 | void sched_setnuma(struct task_struct *p, int nid) | |
4966 | { | |
4967 | struct rq *rq; | |
4968 | unsigned long flags; | |
da0c1e65 | 4969 | bool queued, running; |
0ec8aa00 PZ |
4970 | |
4971 | rq = task_rq_lock(p, &flags); | |
da0c1e65 | 4972 | queued = task_on_rq_queued(p); |
0ec8aa00 PZ |
4973 | running = task_current(rq, p); |
4974 | ||
da0c1e65 | 4975 | if (queued) |
0ec8aa00 PZ |
4976 | dequeue_task(rq, p, 0); |
4977 | if (running) | |
f3cd1c4e | 4978 | put_prev_task(rq, p); |
0ec8aa00 PZ |
4979 | |
4980 | p->numa_preferred_nid = nid; | |
0ec8aa00 PZ |
4981 | |
4982 | if (running) | |
4983 | p->sched_class->set_curr_task(rq); | |
da0c1e65 | 4984 | if (queued) |
0ec8aa00 PZ |
4985 | enqueue_task(rq, p, 0); |
4986 | task_rq_unlock(rq, p, &flags); | |
4987 | } | |
5cc389bc | 4988 | #endif /* CONFIG_NUMA_BALANCING */ |
f7b4cddc | 4989 | |
1da177e4 | 4990 | #ifdef CONFIG_HOTPLUG_CPU |
054b9108 | 4991 | /* |
48c5ccae PZ |
4992 | * Ensures that the idle task is using init_mm right before its cpu goes |
4993 | * offline. | |
054b9108 | 4994 | */ |
48c5ccae | 4995 | void idle_task_exit(void) |
1da177e4 | 4996 | { |
48c5ccae | 4997 | struct mm_struct *mm = current->active_mm; |
e76bd8d9 | 4998 | |
48c5ccae | 4999 | BUG_ON(cpu_online(smp_processor_id())); |
e76bd8d9 | 5000 | |
a53efe5f | 5001 | if (mm != &init_mm) { |
48c5ccae | 5002 | switch_mm(mm, &init_mm, current); |
a53efe5f MS |
5003 | finish_arch_post_lock_switch(); |
5004 | } | |
48c5ccae | 5005 | mmdrop(mm); |
1da177e4 LT |
5006 | } |
5007 | ||
5008 | /* | |
5d180232 PZ |
5009 | * Since this CPU is going 'away' for a while, fold any nr_active delta |
5010 | * we might have. Assumes we're called after migrate_tasks() so that the | |
5011 | * nr_active count is stable. | |
5012 | * | |
5013 | * Also see the comment "Global load-average calculations". | |
1da177e4 | 5014 | */ |
5d180232 | 5015 | static void calc_load_migrate(struct rq *rq) |
1da177e4 | 5016 | { |
5d180232 PZ |
5017 | long delta = calc_load_fold_active(rq); |
5018 | if (delta) | |
5019 | atomic_long_add(delta, &calc_load_tasks); | |
1da177e4 LT |
5020 | } |
5021 | ||
3f1d2a31 PZ |
5022 | static void put_prev_task_fake(struct rq *rq, struct task_struct *prev) |
5023 | { | |
5024 | } | |
5025 | ||
5026 | static const struct sched_class fake_sched_class = { | |
5027 | .put_prev_task = put_prev_task_fake, | |
5028 | }; | |
5029 | ||
5030 | static struct task_struct fake_task = { | |
5031 | /* | |
5032 | * Avoid pull_{rt,dl}_task() | |
5033 | */ | |
5034 | .prio = MAX_PRIO + 1, | |
5035 | .sched_class = &fake_sched_class, | |
5036 | }; | |
5037 | ||
48f24c4d | 5038 | /* |
48c5ccae PZ |
5039 | * Migrate all tasks from the rq, sleeping tasks will be migrated by |
5040 | * try_to_wake_up()->select_task_rq(). | |
5041 | * | |
5042 | * Called with rq->lock held even though we'er in stop_machine() and | |
5043 | * there's no concurrency possible, we hold the required locks anyway | |
5044 | * because of lock validation efforts. | |
1da177e4 | 5045 | */ |
48c5ccae | 5046 | static void migrate_tasks(unsigned int dead_cpu) |
1da177e4 | 5047 | { |
70b97a7f | 5048 | struct rq *rq = cpu_rq(dead_cpu); |
48c5ccae PZ |
5049 | struct task_struct *next, *stop = rq->stop; |
5050 | int dest_cpu; | |
1da177e4 LT |
5051 | |
5052 | /* | |
48c5ccae PZ |
5053 | * Fudge the rq selection such that the below task selection loop |
5054 | * doesn't get stuck on the currently eligible stop task. | |
5055 | * | |
5056 | * We're currently inside stop_machine() and the rq is either stuck | |
5057 | * in the stop_machine_cpu_stop() loop, or we're executing this code, | |
5058 | * either way we should never end up calling schedule() until we're | |
5059 | * done here. | |
1da177e4 | 5060 | */ |
48c5ccae | 5061 | rq->stop = NULL; |
48f24c4d | 5062 | |
77bd3970 FW |
5063 | /* |
5064 | * put_prev_task() and pick_next_task() sched | |
5065 | * class method both need to have an up-to-date | |
5066 | * value of rq->clock[_task] | |
5067 | */ | |
5068 | update_rq_clock(rq); | |
5069 | ||
dd41f596 | 5070 | for ( ; ; ) { |
48c5ccae PZ |
5071 | /* |
5072 | * There's this thread running, bail when that's the only | |
5073 | * remaining thread. | |
5074 | */ | |
5075 | if (rq->nr_running == 1) | |
dd41f596 | 5076 | break; |
48c5ccae | 5077 | |
3f1d2a31 | 5078 | next = pick_next_task(rq, &fake_task); |
48c5ccae | 5079 | BUG_ON(!next); |
79c53799 | 5080 | next->sched_class->put_prev_task(rq, next); |
e692ab53 | 5081 | |
48c5ccae PZ |
5082 | /* Find suitable destination for @next, with force if needed. */ |
5083 | dest_cpu = select_fallback_rq(dead_cpu, next); | |
5084 | raw_spin_unlock(&rq->lock); | |
5085 | ||
5086 | __migrate_task(next, dead_cpu, dest_cpu); | |
5087 | ||
5088 | raw_spin_lock(&rq->lock); | |
1da177e4 | 5089 | } |
dce48a84 | 5090 | |
48c5ccae | 5091 | rq->stop = stop; |
dce48a84 | 5092 | } |
1da177e4 LT |
5093 | #endif /* CONFIG_HOTPLUG_CPU */ |
5094 | ||
e692ab53 NP |
5095 | #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL) |
5096 | ||
5097 | static struct ctl_table sd_ctl_dir[] = { | |
e0361851 AD |
5098 | { |
5099 | .procname = "sched_domain", | |
c57baf1e | 5100 | .mode = 0555, |
e0361851 | 5101 | }, |
56992309 | 5102 | {} |
e692ab53 NP |
5103 | }; |
5104 | ||
5105 | static struct ctl_table sd_ctl_root[] = { | |
e0361851 AD |
5106 | { |
5107 | .procname = "kernel", | |
c57baf1e | 5108 | .mode = 0555, |
e0361851 AD |
5109 | .child = sd_ctl_dir, |
5110 | }, | |
56992309 | 5111 | {} |
e692ab53 NP |
5112 | }; |
5113 | ||
5114 | static struct ctl_table *sd_alloc_ctl_entry(int n) | |
5115 | { | |
5116 | struct ctl_table *entry = | |
5cf9f062 | 5117 | kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL); |
e692ab53 | 5118 | |
e692ab53 NP |
5119 | return entry; |
5120 | } | |
5121 | ||
6382bc90 MM |
5122 | static void sd_free_ctl_entry(struct ctl_table **tablep) |
5123 | { | |
cd790076 | 5124 | struct ctl_table *entry; |
6382bc90 | 5125 | |
cd790076 MM |
5126 | /* |
5127 | * In the intermediate directories, both the child directory and | |
5128 | * procname are dynamically allocated and could fail but the mode | |
41a2d6cf | 5129 | * will always be set. In the lowest directory the names are |
cd790076 MM |
5130 | * static strings and all have proc handlers. |
5131 | */ | |
5132 | for (entry = *tablep; entry->mode; entry++) { | |
6382bc90 MM |
5133 | if (entry->child) |
5134 | sd_free_ctl_entry(&entry->child); | |
cd790076 MM |
5135 | if (entry->proc_handler == NULL) |
5136 | kfree(entry->procname); | |
5137 | } | |
6382bc90 MM |
5138 | |
5139 | kfree(*tablep); | |
5140 | *tablep = NULL; | |
5141 | } | |
5142 | ||
201c373e | 5143 | static int min_load_idx = 0; |
fd9b86d3 | 5144 | static int max_load_idx = CPU_LOAD_IDX_MAX-1; |
201c373e | 5145 | |
e692ab53 | 5146 | static void |
e0361851 | 5147 | set_table_entry(struct ctl_table *entry, |
e692ab53 | 5148 | const char *procname, void *data, int maxlen, |
201c373e NK |
5149 | umode_t mode, proc_handler *proc_handler, |
5150 | bool load_idx) | |
e692ab53 | 5151 | { |
e692ab53 NP |
5152 | entry->procname = procname; |
5153 | entry->data = data; | |
5154 | entry->maxlen = maxlen; | |
5155 | entry->mode = mode; | |
5156 | entry->proc_handler = proc_handler; | |
201c373e NK |
5157 | |
5158 | if (load_idx) { | |
5159 | entry->extra1 = &min_load_idx; | |
5160 | entry->extra2 = &max_load_idx; | |
5161 | } | |
e692ab53 NP |
5162 | } |
5163 | ||
5164 | static struct ctl_table * | |
5165 | sd_alloc_ctl_domain_table(struct sched_domain *sd) | |
5166 | { | |
37e6bae8 | 5167 | struct ctl_table *table = sd_alloc_ctl_entry(14); |
e692ab53 | 5168 | |
ad1cdc1d MM |
5169 | if (table == NULL) |
5170 | return NULL; | |
5171 | ||
e0361851 | 5172 | set_table_entry(&table[0], "min_interval", &sd->min_interval, |
201c373e | 5173 | sizeof(long), 0644, proc_doulongvec_minmax, false); |
e0361851 | 5174 | set_table_entry(&table[1], "max_interval", &sd->max_interval, |
201c373e | 5175 | sizeof(long), 0644, proc_doulongvec_minmax, false); |
e0361851 | 5176 | set_table_entry(&table[2], "busy_idx", &sd->busy_idx, |
201c373e | 5177 | sizeof(int), 0644, proc_dointvec_minmax, true); |
e0361851 | 5178 | set_table_entry(&table[3], "idle_idx", &sd->idle_idx, |
201c373e | 5179 | sizeof(int), 0644, proc_dointvec_minmax, true); |
e0361851 | 5180 | set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx, |
201c373e | 5181 | sizeof(int), 0644, proc_dointvec_minmax, true); |
e0361851 | 5182 | set_table_entry(&table[5], "wake_idx", &sd->wake_idx, |
201c373e | 5183 | sizeof(int), 0644, proc_dointvec_minmax, true); |
e0361851 | 5184 | set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx, |
201c373e | 5185 | sizeof(int), 0644, proc_dointvec_minmax, true); |
e0361851 | 5186 | set_table_entry(&table[7], "busy_factor", &sd->busy_factor, |
201c373e | 5187 | sizeof(int), 0644, proc_dointvec_minmax, false); |
e0361851 | 5188 | set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct, |
201c373e | 5189 | sizeof(int), 0644, proc_dointvec_minmax, false); |
ace8b3d6 | 5190 | set_table_entry(&table[9], "cache_nice_tries", |
e692ab53 | 5191 | &sd->cache_nice_tries, |
201c373e | 5192 | sizeof(int), 0644, proc_dointvec_minmax, false); |
ace8b3d6 | 5193 | set_table_entry(&table[10], "flags", &sd->flags, |
201c373e | 5194 | sizeof(int), 0644, proc_dointvec_minmax, false); |
37e6bae8 AS |
5195 | set_table_entry(&table[11], "max_newidle_lb_cost", |
5196 | &sd->max_newidle_lb_cost, | |
5197 | sizeof(long), 0644, proc_doulongvec_minmax, false); | |
5198 | set_table_entry(&table[12], "name", sd->name, | |
201c373e | 5199 | CORENAME_MAX_SIZE, 0444, proc_dostring, false); |
37e6bae8 | 5200 | /* &table[13] is terminator */ |
e692ab53 NP |
5201 | |
5202 | return table; | |
5203 | } | |
5204 | ||
be7002e6 | 5205 | static struct ctl_table *sd_alloc_ctl_cpu_table(int cpu) |
e692ab53 NP |
5206 | { |
5207 | struct ctl_table *entry, *table; | |
5208 | struct sched_domain *sd; | |
5209 | int domain_num = 0, i; | |
5210 | char buf[32]; | |
5211 | ||
5212 | for_each_domain(cpu, sd) | |
5213 | domain_num++; | |
5214 | entry = table = sd_alloc_ctl_entry(domain_num + 1); | |
ad1cdc1d MM |
5215 | if (table == NULL) |
5216 | return NULL; | |
e692ab53 NP |
5217 | |
5218 | i = 0; | |
5219 | for_each_domain(cpu, sd) { | |
5220 | snprintf(buf, 32, "domain%d", i); | |
e692ab53 | 5221 | entry->procname = kstrdup(buf, GFP_KERNEL); |
c57baf1e | 5222 | entry->mode = 0555; |
e692ab53 NP |
5223 | entry->child = sd_alloc_ctl_domain_table(sd); |
5224 | entry++; | |
5225 | i++; | |
5226 | } | |
5227 | return table; | |
5228 | } | |
5229 | ||
5230 | static struct ctl_table_header *sd_sysctl_header; | |
6382bc90 | 5231 | static void register_sched_domain_sysctl(void) |
e692ab53 | 5232 | { |
6ad4c188 | 5233 | int i, cpu_num = num_possible_cpus(); |
e692ab53 NP |
5234 | struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1); |
5235 | char buf[32]; | |
5236 | ||
7378547f MM |
5237 | WARN_ON(sd_ctl_dir[0].child); |
5238 | sd_ctl_dir[0].child = entry; | |
5239 | ||
ad1cdc1d MM |
5240 | if (entry == NULL) |
5241 | return; | |
5242 | ||
6ad4c188 | 5243 | for_each_possible_cpu(i) { |
e692ab53 | 5244 | snprintf(buf, 32, "cpu%d", i); |
e692ab53 | 5245 | entry->procname = kstrdup(buf, GFP_KERNEL); |
c57baf1e | 5246 | entry->mode = 0555; |
e692ab53 | 5247 | entry->child = sd_alloc_ctl_cpu_table(i); |
97b6ea7b | 5248 | entry++; |
e692ab53 | 5249 | } |
7378547f MM |
5250 | |
5251 | WARN_ON(sd_sysctl_header); | |
e692ab53 NP |
5252 | sd_sysctl_header = register_sysctl_table(sd_ctl_root); |
5253 | } | |
6382bc90 | 5254 | |
7378547f | 5255 | /* may be called multiple times per register */ |
6382bc90 MM |
5256 | static void unregister_sched_domain_sysctl(void) |
5257 | { | |
7378547f MM |
5258 | if (sd_sysctl_header) |
5259 | unregister_sysctl_table(sd_sysctl_header); | |
6382bc90 | 5260 | sd_sysctl_header = NULL; |
7378547f MM |
5261 | if (sd_ctl_dir[0].child) |
5262 | sd_free_ctl_entry(&sd_ctl_dir[0].child); | |
6382bc90 | 5263 | } |
e692ab53 | 5264 | #else |
6382bc90 MM |
5265 | static void register_sched_domain_sysctl(void) |
5266 | { | |
5267 | } | |
5268 | static void unregister_sched_domain_sysctl(void) | |
e692ab53 NP |
5269 | { |
5270 | } | |
5cc389bc | 5271 | #endif /* CONFIG_SCHED_DEBUG && CONFIG_SYSCTL */ |
e692ab53 | 5272 | |
1f11eb6a GH |
5273 | static void set_rq_online(struct rq *rq) |
5274 | { | |
5275 | if (!rq->online) { | |
5276 | const struct sched_class *class; | |
5277 | ||
c6c4927b | 5278 | cpumask_set_cpu(rq->cpu, rq->rd->online); |
1f11eb6a GH |
5279 | rq->online = 1; |
5280 | ||
5281 | for_each_class(class) { | |
5282 | if (class->rq_online) | |
5283 | class->rq_online(rq); | |
5284 | } | |
5285 | } | |
5286 | } | |
5287 | ||
5288 | static void set_rq_offline(struct rq *rq) | |
5289 | { | |
5290 | if (rq->online) { | |
5291 | const struct sched_class *class; | |
5292 | ||
5293 | for_each_class(class) { | |
5294 | if (class->rq_offline) | |
5295 | class->rq_offline(rq); | |
5296 | } | |
5297 | ||
c6c4927b | 5298 | cpumask_clear_cpu(rq->cpu, rq->rd->online); |
1f11eb6a GH |
5299 | rq->online = 0; |
5300 | } | |
5301 | } | |
5302 | ||
1da177e4 LT |
5303 | /* |
5304 | * migration_call - callback that gets triggered when a CPU is added. | |
5305 | * Here we can start up the necessary migration thread for the new CPU. | |
5306 | */ | |
0db0628d | 5307 | static int |
48f24c4d | 5308 | migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu) |
1da177e4 | 5309 | { |
48f24c4d | 5310 | int cpu = (long)hcpu; |
1da177e4 | 5311 | unsigned long flags; |
969c7921 | 5312 | struct rq *rq = cpu_rq(cpu); |
1da177e4 | 5313 | |
48c5ccae | 5314 | switch (action & ~CPU_TASKS_FROZEN) { |
5be9361c | 5315 | |
1da177e4 | 5316 | case CPU_UP_PREPARE: |
a468d389 | 5317 | rq->calc_load_update = calc_load_update; |
1da177e4 | 5318 | break; |
48f24c4d | 5319 | |
1da177e4 | 5320 | case CPU_ONLINE: |
1f94ef59 | 5321 | /* Update our root-domain */ |
05fa785c | 5322 | raw_spin_lock_irqsave(&rq->lock, flags); |
1f94ef59 | 5323 | if (rq->rd) { |
c6c4927b | 5324 | BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); |
1f11eb6a GH |
5325 | |
5326 | set_rq_online(rq); | |
1f94ef59 | 5327 | } |
05fa785c | 5328 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
1da177e4 | 5329 | break; |
48f24c4d | 5330 | |
1da177e4 | 5331 | #ifdef CONFIG_HOTPLUG_CPU |
08f503b0 | 5332 | case CPU_DYING: |
317f3941 | 5333 | sched_ttwu_pending(); |
57d885fe | 5334 | /* Update our root-domain */ |
05fa785c | 5335 | raw_spin_lock_irqsave(&rq->lock, flags); |
57d885fe | 5336 | if (rq->rd) { |
c6c4927b | 5337 | BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); |
1f11eb6a | 5338 | set_rq_offline(rq); |
57d885fe | 5339 | } |
48c5ccae PZ |
5340 | migrate_tasks(cpu); |
5341 | BUG_ON(rq->nr_running != 1); /* the migration thread */ | |
05fa785c | 5342 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
5d180232 | 5343 | break; |
48c5ccae | 5344 | |
5d180232 | 5345 | case CPU_DEAD: |
f319da0c | 5346 | calc_load_migrate(rq); |
57d885fe | 5347 | break; |
1da177e4 LT |
5348 | #endif |
5349 | } | |
49c022e6 PZ |
5350 | |
5351 | update_max_interval(); | |
5352 | ||
1da177e4 LT |
5353 | return NOTIFY_OK; |
5354 | } | |
5355 | ||
f38b0820 PM |
5356 | /* |
5357 | * Register at high priority so that task migration (migrate_all_tasks) | |
5358 | * happens before everything else. This has to be lower priority than | |
cdd6c482 | 5359 | * the notifier in the perf_event subsystem, though. |
1da177e4 | 5360 | */ |
0db0628d | 5361 | static struct notifier_block migration_notifier = { |
1da177e4 | 5362 | .notifier_call = migration_call, |
50a323b7 | 5363 | .priority = CPU_PRI_MIGRATION, |
1da177e4 LT |
5364 | }; |
5365 | ||
6a82b60d | 5366 | static void set_cpu_rq_start_time(void) |
a803f026 CM |
5367 | { |
5368 | int cpu = smp_processor_id(); | |
5369 | struct rq *rq = cpu_rq(cpu); | |
5370 | rq->age_stamp = sched_clock_cpu(cpu); | |
5371 | } | |
5372 | ||
0db0628d | 5373 | static int sched_cpu_active(struct notifier_block *nfb, |
3a101d05 TH |
5374 | unsigned long action, void *hcpu) |
5375 | { | |
5376 | switch (action & ~CPU_TASKS_FROZEN) { | |
a803f026 CM |
5377 | case CPU_STARTING: |
5378 | set_cpu_rq_start_time(); | |
5379 | return NOTIFY_OK; | |
3a101d05 TH |
5380 | case CPU_DOWN_FAILED: |
5381 | set_cpu_active((long)hcpu, true); | |
5382 | return NOTIFY_OK; | |
5383 | default: | |
5384 | return NOTIFY_DONE; | |
5385 | } | |
5386 | } | |
5387 | ||
0db0628d | 5388 | static int sched_cpu_inactive(struct notifier_block *nfb, |
3a101d05 TH |
5389 | unsigned long action, void *hcpu) |
5390 | { | |
5391 | switch (action & ~CPU_TASKS_FROZEN) { | |
5392 | case CPU_DOWN_PREPARE: | |
3c18d447 | 5393 | set_cpu_active((long)hcpu, false); |
3a101d05 | 5394 | return NOTIFY_OK; |
3c18d447 JL |
5395 | default: |
5396 | return NOTIFY_DONE; | |
3a101d05 TH |
5397 | } |
5398 | } | |
5399 | ||
7babe8db | 5400 | static int __init migration_init(void) |
1da177e4 LT |
5401 | { |
5402 | void *cpu = (void *)(long)smp_processor_id(); | |
07dccf33 | 5403 | int err; |
48f24c4d | 5404 | |
3a101d05 | 5405 | /* Initialize migration for the boot CPU */ |
07dccf33 AM |
5406 | err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu); |
5407 | BUG_ON(err == NOTIFY_BAD); | |
1da177e4 LT |
5408 | migration_call(&migration_notifier, CPU_ONLINE, cpu); |
5409 | register_cpu_notifier(&migration_notifier); | |
7babe8db | 5410 | |
3a101d05 TH |
5411 | /* Register cpu active notifiers */ |
5412 | cpu_notifier(sched_cpu_active, CPU_PRI_SCHED_ACTIVE); | |
5413 | cpu_notifier(sched_cpu_inactive, CPU_PRI_SCHED_INACTIVE); | |
5414 | ||
a004cd42 | 5415 | return 0; |
1da177e4 | 5416 | } |
7babe8db | 5417 | early_initcall(migration_init); |
476f3534 | 5418 | |
4cb98839 PZ |
5419 | static cpumask_var_t sched_domains_tmpmask; /* sched_domains_mutex */ |
5420 | ||
3e9830dc | 5421 | #ifdef CONFIG_SCHED_DEBUG |
4dcf6aff | 5422 | |
d039ac60 | 5423 | static __read_mostly int sched_debug_enabled; |
f6630114 | 5424 | |
d039ac60 | 5425 | static int __init sched_debug_setup(char *str) |
f6630114 | 5426 | { |
d039ac60 | 5427 | sched_debug_enabled = 1; |
f6630114 MT |
5428 | |
5429 | return 0; | |
5430 | } | |
d039ac60 PZ |
5431 | early_param("sched_debug", sched_debug_setup); |
5432 | ||
5433 | static inline bool sched_debug(void) | |
5434 | { | |
5435 | return sched_debug_enabled; | |
5436 | } | |
f6630114 | 5437 | |
7c16ec58 | 5438 | static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level, |
96f874e2 | 5439 | struct cpumask *groupmask) |
1da177e4 | 5440 | { |
4dcf6aff | 5441 | struct sched_group *group = sd->groups; |
1da177e4 | 5442 | |
96f874e2 | 5443 | cpumask_clear(groupmask); |
4dcf6aff IM |
5444 | |
5445 | printk(KERN_DEBUG "%*s domain %d: ", level, "", level); | |
5446 | ||
5447 | if (!(sd->flags & SD_LOAD_BALANCE)) { | |
3df0fc5b | 5448 | printk("does not load-balance\n"); |
4dcf6aff | 5449 | if (sd->parent) |
3df0fc5b PZ |
5450 | printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain" |
5451 | " has parent"); | |
4dcf6aff | 5452 | return -1; |
41c7ce9a NP |
5453 | } |
5454 | ||
333470ee TH |
5455 | printk(KERN_CONT "span %*pbl level %s\n", |
5456 | cpumask_pr_args(sched_domain_span(sd)), sd->name); | |
4dcf6aff | 5457 | |
758b2cdc | 5458 | if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) { |
3df0fc5b PZ |
5459 | printk(KERN_ERR "ERROR: domain->span does not contain " |
5460 | "CPU%d\n", cpu); | |
4dcf6aff | 5461 | } |
758b2cdc | 5462 | if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) { |
3df0fc5b PZ |
5463 | printk(KERN_ERR "ERROR: domain->groups does not contain" |
5464 | " CPU%d\n", cpu); | |
4dcf6aff | 5465 | } |
1da177e4 | 5466 | |
4dcf6aff | 5467 | printk(KERN_DEBUG "%*s groups:", level + 1, ""); |
1da177e4 | 5468 | do { |
4dcf6aff | 5469 | if (!group) { |
3df0fc5b PZ |
5470 | printk("\n"); |
5471 | printk(KERN_ERR "ERROR: group is NULL\n"); | |
1da177e4 LT |
5472 | break; |
5473 | } | |
5474 | ||
758b2cdc | 5475 | if (!cpumask_weight(sched_group_cpus(group))) { |
3df0fc5b PZ |
5476 | printk(KERN_CONT "\n"); |
5477 | printk(KERN_ERR "ERROR: empty group\n"); | |
4dcf6aff IM |
5478 | break; |
5479 | } | |
1da177e4 | 5480 | |
cb83b629 PZ |
5481 | if (!(sd->flags & SD_OVERLAP) && |
5482 | cpumask_intersects(groupmask, sched_group_cpus(group))) { | |
3df0fc5b PZ |
5483 | printk(KERN_CONT "\n"); |
5484 | printk(KERN_ERR "ERROR: repeated CPUs\n"); | |
4dcf6aff IM |
5485 | break; |
5486 | } | |
1da177e4 | 5487 | |
758b2cdc | 5488 | cpumask_or(groupmask, groupmask, sched_group_cpus(group)); |
1da177e4 | 5489 | |
333470ee TH |
5490 | printk(KERN_CONT " %*pbl", |
5491 | cpumask_pr_args(sched_group_cpus(group))); | |
ca8ce3d0 | 5492 | if (group->sgc->capacity != SCHED_CAPACITY_SCALE) { |
63b2ca30 NP |
5493 | printk(KERN_CONT " (cpu_capacity = %d)", |
5494 | group->sgc->capacity); | |
381512cf | 5495 | } |
1da177e4 | 5496 | |
4dcf6aff IM |
5497 | group = group->next; |
5498 | } while (group != sd->groups); | |
3df0fc5b | 5499 | printk(KERN_CONT "\n"); |
1da177e4 | 5500 | |
758b2cdc | 5501 | if (!cpumask_equal(sched_domain_span(sd), groupmask)) |
3df0fc5b | 5502 | printk(KERN_ERR "ERROR: groups don't span domain->span\n"); |
1da177e4 | 5503 | |
758b2cdc RR |
5504 | if (sd->parent && |
5505 | !cpumask_subset(groupmask, sched_domain_span(sd->parent))) | |
3df0fc5b PZ |
5506 | printk(KERN_ERR "ERROR: parent span is not a superset " |
5507 | "of domain->span\n"); | |
4dcf6aff IM |
5508 | return 0; |
5509 | } | |
1da177e4 | 5510 | |
4dcf6aff IM |
5511 | static void sched_domain_debug(struct sched_domain *sd, int cpu) |
5512 | { | |
5513 | int level = 0; | |
1da177e4 | 5514 | |
d039ac60 | 5515 | if (!sched_debug_enabled) |
f6630114 MT |
5516 | return; |
5517 | ||
4dcf6aff IM |
5518 | if (!sd) { |
5519 | printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu); | |
5520 | return; | |
5521 | } | |
1da177e4 | 5522 | |
4dcf6aff IM |
5523 | printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu); |
5524 | ||
5525 | for (;;) { | |
4cb98839 | 5526 | if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask)) |
4dcf6aff | 5527 | break; |
1da177e4 LT |
5528 | level++; |
5529 | sd = sd->parent; | |
33859f7f | 5530 | if (!sd) |
4dcf6aff IM |
5531 | break; |
5532 | } | |
1da177e4 | 5533 | } |
6d6bc0ad | 5534 | #else /* !CONFIG_SCHED_DEBUG */ |
48f24c4d | 5535 | # define sched_domain_debug(sd, cpu) do { } while (0) |
d039ac60 PZ |
5536 | static inline bool sched_debug(void) |
5537 | { | |
5538 | return false; | |
5539 | } | |
6d6bc0ad | 5540 | #endif /* CONFIG_SCHED_DEBUG */ |
1da177e4 | 5541 | |
1a20ff27 | 5542 | static int sd_degenerate(struct sched_domain *sd) |
245af2c7 | 5543 | { |
758b2cdc | 5544 | if (cpumask_weight(sched_domain_span(sd)) == 1) |
245af2c7 SS |
5545 | return 1; |
5546 | ||
5547 | /* Following flags need at least 2 groups */ | |
5548 | if (sd->flags & (SD_LOAD_BALANCE | | |
5549 | SD_BALANCE_NEWIDLE | | |
5550 | SD_BALANCE_FORK | | |
89c4710e | 5551 | SD_BALANCE_EXEC | |
5d4dfddd | 5552 | SD_SHARE_CPUCAPACITY | |
d77b3ed5 VG |
5553 | SD_SHARE_PKG_RESOURCES | |
5554 | SD_SHARE_POWERDOMAIN)) { | |
245af2c7 SS |
5555 | if (sd->groups != sd->groups->next) |
5556 | return 0; | |
5557 | } | |
5558 | ||
5559 | /* Following flags don't use groups */ | |
c88d5910 | 5560 | if (sd->flags & (SD_WAKE_AFFINE)) |
245af2c7 SS |
5561 | return 0; |
5562 | ||
5563 | return 1; | |
5564 | } | |
5565 | ||
48f24c4d IM |
5566 | static int |
5567 | sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent) | |
245af2c7 SS |
5568 | { |
5569 | unsigned long cflags = sd->flags, pflags = parent->flags; | |
5570 | ||
5571 | if (sd_degenerate(parent)) | |
5572 | return 1; | |
5573 | ||
758b2cdc | 5574 | if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent))) |
245af2c7 SS |
5575 | return 0; |
5576 | ||
245af2c7 SS |
5577 | /* Flags needing groups don't count if only 1 group in parent */ |
5578 | if (parent->groups == parent->groups->next) { | |
5579 | pflags &= ~(SD_LOAD_BALANCE | | |
5580 | SD_BALANCE_NEWIDLE | | |
5581 | SD_BALANCE_FORK | | |
89c4710e | 5582 | SD_BALANCE_EXEC | |
5d4dfddd | 5583 | SD_SHARE_CPUCAPACITY | |
10866e62 | 5584 | SD_SHARE_PKG_RESOURCES | |
d77b3ed5 VG |
5585 | SD_PREFER_SIBLING | |
5586 | SD_SHARE_POWERDOMAIN); | |
5436499e KC |
5587 | if (nr_node_ids == 1) |
5588 | pflags &= ~SD_SERIALIZE; | |
245af2c7 SS |
5589 | } |
5590 | if (~cflags & pflags) | |
5591 | return 0; | |
5592 | ||
5593 | return 1; | |
5594 | } | |
5595 | ||
dce840a0 | 5596 | static void free_rootdomain(struct rcu_head *rcu) |
c6c4927b | 5597 | { |
dce840a0 | 5598 | struct root_domain *rd = container_of(rcu, struct root_domain, rcu); |
047106ad | 5599 | |
68e74568 | 5600 | cpupri_cleanup(&rd->cpupri); |
6bfd6d72 | 5601 | cpudl_cleanup(&rd->cpudl); |
1baca4ce | 5602 | free_cpumask_var(rd->dlo_mask); |
c6c4927b RR |
5603 | free_cpumask_var(rd->rto_mask); |
5604 | free_cpumask_var(rd->online); | |
5605 | free_cpumask_var(rd->span); | |
5606 | kfree(rd); | |
5607 | } | |
5608 | ||
57d885fe GH |
5609 | static void rq_attach_root(struct rq *rq, struct root_domain *rd) |
5610 | { | |
a0490fa3 | 5611 | struct root_domain *old_rd = NULL; |
57d885fe | 5612 | unsigned long flags; |
57d885fe | 5613 | |
05fa785c | 5614 | raw_spin_lock_irqsave(&rq->lock, flags); |
57d885fe GH |
5615 | |
5616 | if (rq->rd) { | |
a0490fa3 | 5617 | old_rd = rq->rd; |
57d885fe | 5618 | |
c6c4927b | 5619 | if (cpumask_test_cpu(rq->cpu, old_rd->online)) |
1f11eb6a | 5620 | set_rq_offline(rq); |
57d885fe | 5621 | |
c6c4927b | 5622 | cpumask_clear_cpu(rq->cpu, old_rd->span); |
dc938520 | 5623 | |
a0490fa3 | 5624 | /* |
0515973f | 5625 | * If we dont want to free the old_rd yet then |
a0490fa3 IM |
5626 | * set old_rd to NULL to skip the freeing later |
5627 | * in this function: | |
5628 | */ | |
5629 | if (!atomic_dec_and_test(&old_rd->refcount)) | |
5630 | old_rd = NULL; | |
57d885fe GH |
5631 | } |
5632 | ||
5633 | atomic_inc(&rd->refcount); | |
5634 | rq->rd = rd; | |
5635 | ||
c6c4927b | 5636 | cpumask_set_cpu(rq->cpu, rd->span); |
00aec93d | 5637 | if (cpumask_test_cpu(rq->cpu, cpu_active_mask)) |
1f11eb6a | 5638 | set_rq_online(rq); |
57d885fe | 5639 | |
05fa785c | 5640 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
a0490fa3 IM |
5641 | |
5642 | if (old_rd) | |
dce840a0 | 5643 | call_rcu_sched(&old_rd->rcu, free_rootdomain); |
57d885fe GH |
5644 | } |
5645 | ||
68c38fc3 | 5646 | static int init_rootdomain(struct root_domain *rd) |
57d885fe GH |
5647 | { |
5648 | memset(rd, 0, sizeof(*rd)); | |
5649 | ||
68c38fc3 | 5650 | if (!alloc_cpumask_var(&rd->span, GFP_KERNEL)) |
0c910d28 | 5651 | goto out; |
68c38fc3 | 5652 | if (!alloc_cpumask_var(&rd->online, GFP_KERNEL)) |
c6c4927b | 5653 | goto free_span; |
1baca4ce | 5654 | if (!alloc_cpumask_var(&rd->dlo_mask, GFP_KERNEL)) |
c6c4927b | 5655 | goto free_online; |
1baca4ce JL |
5656 | if (!alloc_cpumask_var(&rd->rto_mask, GFP_KERNEL)) |
5657 | goto free_dlo_mask; | |
6e0534f2 | 5658 | |
332ac17e | 5659 | init_dl_bw(&rd->dl_bw); |
6bfd6d72 JL |
5660 | if (cpudl_init(&rd->cpudl) != 0) |
5661 | goto free_dlo_mask; | |
332ac17e | 5662 | |
68c38fc3 | 5663 | if (cpupri_init(&rd->cpupri) != 0) |
68e74568 | 5664 | goto free_rto_mask; |
c6c4927b | 5665 | return 0; |
6e0534f2 | 5666 | |
68e74568 RR |
5667 | free_rto_mask: |
5668 | free_cpumask_var(rd->rto_mask); | |
1baca4ce JL |
5669 | free_dlo_mask: |
5670 | free_cpumask_var(rd->dlo_mask); | |
c6c4927b RR |
5671 | free_online: |
5672 | free_cpumask_var(rd->online); | |
5673 | free_span: | |
5674 | free_cpumask_var(rd->span); | |
0c910d28 | 5675 | out: |
c6c4927b | 5676 | return -ENOMEM; |
57d885fe GH |
5677 | } |
5678 | ||
029632fb PZ |
5679 | /* |
5680 | * By default the system creates a single root-domain with all cpus as | |
5681 | * members (mimicking the global state we have today). | |
5682 | */ | |
5683 | struct root_domain def_root_domain; | |
5684 | ||
57d885fe GH |
5685 | static void init_defrootdomain(void) |
5686 | { | |
68c38fc3 | 5687 | init_rootdomain(&def_root_domain); |
c6c4927b | 5688 | |
57d885fe GH |
5689 | atomic_set(&def_root_domain.refcount, 1); |
5690 | } | |
5691 | ||
dc938520 | 5692 | static struct root_domain *alloc_rootdomain(void) |
57d885fe GH |
5693 | { |
5694 | struct root_domain *rd; | |
5695 | ||
5696 | rd = kmalloc(sizeof(*rd), GFP_KERNEL); | |
5697 | if (!rd) | |
5698 | return NULL; | |
5699 | ||
68c38fc3 | 5700 | if (init_rootdomain(rd) != 0) { |
c6c4927b RR |
5701 | kfree(rd); |
5702 | return NULL; | |
5703 | } | |
57d885fe GH |
5704 | |
5705 | return rd; | |
5706 | } | |
5707 | ||
63b2ca30 | 5708 | static void free_sched_groups(struct sched_group *sg, int free_sgc) |
e3589f6c PZ |
5709 | { |
5710 | struct sched_group *tmp, *first; | |
5711 | ||
5712 | if (!sg) | |
5713 | return; | |
5714 | ||
5715 | first = sg; | |
5716 | do { | |
5717 | tmp = sg->next; | |
5718 | ||
63b2ca30 NP |
5719 | if (free_sgc && atomic_dec_and_test(&sg->sgc->ref)) |
5720 | kfree(sg->sgc); | |
e3589f6c PZ |
5721 | |
5722 | kfree(sg); | |
5723 | sg = tmp; | |
5724 | } while (sg != first); | |
5725 | } | |
5726 | ||
dce840a0 PZ |
5727 | static void free_sched_domain(struct rcu_head *rcu) |
5728 | { | |
5729 | struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu); | |
e3589f6c PZ |
5730 | |
5731 | /* | |
5732 | * If its an overlapping domain it has private groups, iterate and | |
5733 | * nuke them all. | |
5734 | */ | |
5735 | if (sd->flags & SD_OVERLAP) { | |
5736 | free_sched_groups(sd->groups, 1); | |
5737 | } else if (atomic_dec_and_test(&sd->groups->ref)) { | |
63b2ca30 | 5738 | kfree(sd->groups->sgc); |
dce840a0 | 5739 | kfree(sd->groups); |
9c3f75cb | 5740 | } |
dce840a0 PZ |
5741 | kfree(sd); |
5742 | } | |
5743 | ||
5744 | static void destroy_sched_domain(struct sched_domain *sd, int cpu) | |
5745 | { | |
5746 | call_rcu(&sd->rcu, free_sched_domain); | |
5747 | } | |
5748 | ||
5749 | static void destroy_sched_domains(struct sched_domain *sd, int cpu) | |
5750 | { | |
5751 | for (; sd; sd = sd->parent) | |
5752 | destroy_sched_domain(sd, cpu); | |
5753 | } | |
5754 | ||
518cd623 PZ |
5755 | /* |
5756 | * Keep a special pointer to the highest sched_domain that has | |
5757 | * SD_SHARE_PKG_RESOURCE set (Last Level Cache Domain) for this | |
5758 | * allows us to avoid some pointer chasing select_idle_sibling(). | |
5759 | * | |
5760 | * Also keep a unique ID per domain (we use the first cpu number in | |
5761 | * the cpumask of the domain), this allows us to quickly tell if | |
39be3501 | 5762 | * two cpus are in the same cache domain, see cpus_share_cache(). |
518cd623 PZ |
5763 | */ |
5764 | DEFINE_PER_CPU(struct sched_domain *, sd_llc); | |
7d9ffa89 | 5765 | DEFINE_PER_CPU(int, sd_llc_size); |
518cd623 | 5766 | DEFINE_PER_CPU(int, sd_llc_id); |
fb13c7ee | 5767 | DEFINE_PER_CPU(struct sched_domain *, sd_numa); |
37dc6b50 PM |
5768 | DEFINE_PER_CPU(struct sched_domain *, sd_busy); |
5769 | DEFINE_PER_CPU(struct sched_domain *, sd_asym); | |
518cd623 PZ |
5770 | |
5771 | static void update_top_cache_domain(int cpu) | |
5772 | { | |
5773 | struct sched_domain *sd; | |
5d4cf996 | 5774 | struct sched_domain *busy_sd = NULL; |
518cd623 | 5775 | int id = cpu; |
7d9ffa89 | 5776 | int size = 1; |
518cd623 PZ |
5777 | |
5778 | sd = highest_flag_domain(cpu, SD_SHARE_PKG_RESOURCES); | |
7d9ffa89 | 5779 | if (sd) { |
518cd623 | 5780 | id = cpumask_first(sched_domain_span(sd)); |
7d9ffa89 | 5781 | size = cpumask_weight(sched_domain_span(sd)); |
5d4cf996 | 5782 | busy_sd = sd->parent; /* sd_busy */ |
7d9ffa89 | 5783 | } |
5d4cf996 | 5784 | rcu_assign_pointer(per_cpu(sd_busy, cpu), busy_sd); |
518cd623 PZ |
5785 | |
5786 | rcu_assign_pointer(per_cpu(sd_llc, cpu), sd); | |
7d9ffa89 | 5787 | per_cpu(sd_llc_size, cpu) = size; |
518cd623 | 5788 | per_cpu(sd_llc_id, cpu) = id; |
fb13c7ee MG |
5789 | |
5790 | sd = lowest_flag_domain(cpu, SD_NUMA); | |
5791 | rcu_assign_pointer(per_cpu(sd_numa, cpu), sd); | |
37dc6b50 PM |
5792 | |
5793 | sd = highest_flag_domain(cpu, SD_ASYM_PACKING); | |
5794 | rcu_assign_pointer(per_cpu(sd_asym, cpu), sd); | |
518cd623 PZ |
5795 | } |
5796 | ||
1da177e4 | 5797 | /* |
0eab9146 | 5798 | * Attach the domain 'sd' to 'cpu' as its base domain. Callers must |
1da177e4 LT |
5799 | * hold the hotplug lock. |
5800 | */ | |
0eab9146 IM |
5801 | static void |
5802 | cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu) | |
1da177e4 | 5803 | { |
70b97a7f | 5804 | struct rq *rq = cpu_rq(cpu); |
245af2c7 SS |
5805 | struct sched_domain *tmp; |
5806 | ||
5807 | /* Remove the sched domains which do not contribute to scheduling. */ | |
f29c9b1c | 5808 | for (tmp = sd; tmp; ) { |
245af2c7 SS |
5809 | struct sched_domain *parent = tmp->parent; |
5810 | if (!parent) | |
5811 | break; | |
f29c9b1c | 5812 | |
1a848870 | 5813 | if (sd_parent_degenerate(tmp, parent)) { |
245af2c7 | 5814 | tmp->parent = parent->parent; |
1a848870 SS |
5815 | if (parent->parent) |
5816 | parent->parent->child = tmp; | |
10866e62 PZ |
5817 | /* |
5818 | * Transfer SD_PREFER_SIBLING down in case of a | |
5819 | * degenerate parent; the spans match for this | |
5820 | * so the property transfers. | |
5821 | */ | |
5822 | if (parent->flags & SD_PREFER_SIBLING) | |
5823 | tmp->flags |= SD_PREFER_SIBLING; | |
dce840a0 | 5824 | destroy_sched_domain(parent, cpu); |
f29c9b1c LZ |
5825 | } else |
5826 | tmp = tmp->parent; | |
245af2c7 SS |
5827 | } |
5828 | ||
1a848870 | 5829 | if (sd && sd_degenerate(sd)) { |
dce840a0 | 5830 | tmp = sd; |
245af2c7 | 5831 | sd = sd->parent; |
dce840a0 | 5832 | destroy_sched_domain(tmp, cpu); |
1a848870 SS |
5833 | if (sd) |
5834 | sd->child = NULL; | |
5835 | } | |
1da177e4 | 5836 | |
4cb98839 | 5837 | sched_domain_debug(sd, cpu); |
1da177e4 | 5838 | |
57d885fe | 5839 | rq_attach_root(rq, rd); |
dce840a0 | 5840 | tmp = rq->sd; |
674311d5 | 5841 | rcu_assign_pointer(rq->sd, sd); |
dce840a0 | 5842 | destroy_sched_domains(tmp, cpu); |
518cd623 PZ |
5843 | |
5844 | update_top_cache_domain(cpu); | |
1da177e4 LT |
5845 | } |
5846 | ||
1da177e4 LT |
5847 | /* Setup the mask of cpus configured for isolated domains */ |
5848 | static int __init isolated_cpu_setup(char *str) | |
5849 | { | |
bdddd296 | 5850 | alloc_bootmem_cpumask_var(&cpu_isolated_map); |
968ea6d8 | 5851 | cpulist_parse(str, cpu_isolated_map); |
1da177e4 LT |
5852 | return 1; |
5853 | } | |
5854 | ||
8927f494 | 5855 | __setup("isolcpus=", isolated_cpu_setup); |
1da177e4 | 5856 | |
49a02c51 | 5857 | struct s_data { |
21d42ccf | 5858 | struct sched_domain ** __percpu sd; |
49a02c51 AH |
5859 | struct root_domain *rd; |
5860 | }; | |
5861 | ||
2109b99e | 5862 | enum s_alloc { |
2109b99e | 5863 | sa_rootdomain, |
21d42ccf | 5864 | sa_sd, |
dce840a0 | 5865 | sa_sd_storage, |
2109b99e AH |
5866 | sa_none, |
5867 | }; | |
5868 | ||
c1174876 PZ |
5869 | /* |
5870 | * Build an iteration mask that can exclude certain CPUs from the upwards | |
5871 | * domain traversal. | |
5872 | * | |
5873 | * Asymmetric node setups can result in situations where the domain tree is of | |
5874 | * unequal depth, make sure to skip domains that already cover the entire | |
5875 | * range. | |
5876 | * | |
5877 | * In that case build_sched_domains() will have terminated the iteration early | |
5878 | * and our sibling sd spans will be empty. Domains should always include the | |
5879 | * cpu they're built on, so check that. | |
5880 | * | |
5881 | */ | |
5882 | static void build_group_mask(struct sched_domain *sd, struct sched_group *sg) | |
5883 | { | |
5884 | const struct cpumask *span = sched_domain_span(sd); | |
5885 | struct sd_data *sdd = sd->private; | |
5886 | struct sched_domain *sibling; | |
5887 | int i; | |
5888 | ||
5889 | for_each_cpu(i, span) { | |
5890 | sibling = *per_cpu_ptr(sdd->sd, i); | |
5891 | if (!cpumask_test_cpu(i, sched_domain_span(sibling))) | |
5892 | continue; | |
5893 | ||
5894 | cpumask_set_cpu(i, sched_group_mask(sg)); | |
5895 | } | |
5896 | } | |
5897 | ||
5898 | /* | |
5899 | * Return the canonical balance cpu for this group, this is the first cpu | |
5900 | * of this group that's also in the iteration mask. | |
5901 | */ | |
5902 | int group_balance_cpu(struct sched_group *sg) | |
5903 | { | |
5904 | return cpumask_first_and(sched_group_cpus(sg), sched_group_mask(sg)); | |
5905 | } | |
5906 | ||
e3589f6c PZ |
5907 | static int |
5908 | build_overlap_sched_groups(struct sched_domain *sd, int cpu) | |
5909 | { | |
5910 | struct sched_group *first = NULL, *last = NULL, *groups = NULL, *sg; | |
5911 | const struct cpumask *span = sched_domain_span(sd); | |
5912 | struct cpumask *covered = sched_domains_tmpmask; | |
5913 | struct sd_data *sdd = sd->private; | |
aaecac4a | 5914 | struct sched_domain *sibling; |
e3589f6c PZ |
5915 | int i; |
5916 | ||
5917 | cpumask_clear(covered); | |
5918 | ||
5919 | for_each_cpu(i, span) { | |
5920 | struct cpumask *sg_span; | |
5921 | ||
5922 | if (cpumask_test_cpu(i, covered)) | |
5923 | continue; | |
5924 | ||
aaecac4a | 5925 | sibling = *per_cpu_ptr(sdd->sd, i); |
c1174876 PZ |
5926 | |
5927 | /* See the comment near build_group_mask(). */ | |
aaecac4a | 5928 | if (!cpumask_test_cpu(i, sched_domain_span(sibling))) |
c1174876 PZ |
5929 | continue; |
5930 | ||
e3589f6c | 5931 | sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(), |
4d78a223 | 5932 | GFP_KERNEL, cpu_to_node(cpu)); |
e3589f6c PZ |
5933 | |
5934 | if (!sg) | |
5935 | goto fail; | |
5936 | ||
5937 | sg_span = sched_group_cpus(sg); | |
aaecac4a ZZ |
5938 | if (sibling->child) |
5939 | cpumask_copy(sg_span, sched_domain_span(sibling->child)); | |
5940 | else | |
e3589f6c PZ |
5941 | cpumask_set_cpu(i, sg_span); |
5942 | ||
5943 | cpumask_or(covered, covered, sg_span); | |
5944 | ||
63b2ca30 NP |
5945 | sg->sgc = *per_cpu_ptr(sdd->sgc, i); |
5946 | if (atomic_inc_return(&sg->sgc->ref) == 1) | |
c1174876 PZ |
5947 | build_group_mask(sd, sg); |
5948 | ||
c3decf0d | 5949 | /* |
63b2ca30 | 5950 | * Initialize sgc->capacity such that even if we mess up the |
c3decf0d PZ |
5951 | * domains and no possible iteration will get us here, we won't |
5952 | * die on a /0 trap. | |
5953 | */ | |
ca8ce3d0 | 5954 | sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sg_span); |
e3589f6c | 5955 | |
c1174876 PZ |
5956 | /* |
5957 | * Make sure the first group of this domain contains the | |
5958 | * canonical balance cpu. Otherwise the sched_domain iteration | |
5959 | * breaks. See update_sg_lb_stats(). | |
5960 | */ | |
74a5ce20 | 5961 | if ((!groups && cpumask_test_cpu(cpu, sg_span)) || |
c1174876 | 5962 | group_balance_cpu(sg) == cpu) |
e3589f6c PZ |
5963 | groups = sg; |
5964 | ||
5965 | if (!first) | |
5966 | first = sg; | |
5967 | if (last) | |
5968 | last->next = sg; | |
5969 | last = sg; | |
5970 | last->next = first; | |
5971 | } | |
5972 | sd->groups = groups; | |
5973 | ||
5974 | return 0; | |
5975 | ||
5976 | fail: | |
5977 | free_sched_groups(first, 0); | |
5978 | ||
5979 | return -ENOMEM; | |
5980 | } | |
5981 | ||
dce840a0 | 5982 | static int get_group(int cpu, struct sd_data *sdd, struct sched_group **sg) |
1da177e4 | 5983 | { |
dce840a0 PZ |
5984 | struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu); |
5985 | struct sched_domain *child = sd->child; | |
1da177e4 | 5986 | |
dce840a0 PZ |
5987 | if (child) |
5988 | cpu = cpumask_first(sched_domain_span(child)); | |
1e9f28fa | 5989 | |
9c3f75cb | 5990 | if (sg) { |
dce840a0 | 5991 | *sg = *per_cpu_ptr(sdd->sg, cpu); |
63b2ca30 NP |
5992 | (*sg)->sgc = *per_cpu_ptr(sdd->sgc, cpu); |
5993 | atomic_set(&(*sg)->sgc->ref, 1); /* for claim_allocations */ | |
9c3f75cb | 5994 | } |
dce840a0 PZ |
5995 | |
5996 | return cpu; | |
1e9f28fa | 5997 | } |
1e9f28fa | 5998 | |
01a08546 | 5999 | /* |
dce840a0 PZ |
6000 | * build_sched_groups will build a circular linked list of the groups |
6001 | * covered by the given span, and will set each group's ->cpumask correctly, | |
ced549fa | 6002 | * and ->cpu_capacity to 0. |
e3589f6c PZ |
6003 | * |
6004 | * Assumes the sched_domain tree is fully constructed | |
01a08546 | 6005 | */ |
e3589f6c PZ |
6006 | static int |
6007 | build_sched_groups(struct sched_domain *sd, int cpu) | |
1da177e4 | 6008 | { |
dce840a0 PZ |
6009 | struct sched_group *first = NULL, *last = NULL; |
6010 | struct sd_data *sdd = sd->private; | |
6011 | const struct cpumask *span = sched_domain_span(sd); | |
f96225fd | 6012 | struct cpumask *covered; |
dce840a0 | 6013 | int i; |
9c1cfda2 | 6014 | |
e3589f6c PZ |
6015 | get_group(cpu, sdd, &sd->groups); |
6016 | atomic_inc(&sd->groups->ref); | |
6017 | ||
0936629f | 6018 | if (cpu != cpumask_first(span)) |
e3589f6c PZ |
6019 | return 0; |
6020 | ||
f96225fd PZ |
6021 | lockdep_assert_held(&sched_domains_mutex); |
6022 | covered = sched_domains_tmpmask; | |
6023 | ||
dce840a0 | 6024 | cpumask_clear(covered); |
6711cab4 | 6025 | |
dce840a0 PZ |
6026 | for_each_cpu(i, span) { |
6027 | struct sched_group *sg; | |
cd08e923 | 6028 | int group, j; |
6711cab4 | 6029 | |
dce840a0 PZ |
6030 | if (cpumask_test_cpu(i, covered)) |
6031 | continue; | |
6711cab4 | 6032 | |
cd08e923 | 6033 | group = get_group(i, sdd, &sg); |
c1174876 | 6034 | cpumask_setall(sched_group_mask(sg)); |
0601a88d | 6035 | |
dce840a0 PZ |
6036 | for_each_cpu(j, span) { |
6037 | if (get_group(j, sdd, NULL) != group) | |
6038 | continue; | |
0601a88d | 6039 | |
dce840a0 PZ |
6040 | cpumask_set_cpu(j, covered); |
6041 | cpumask_set_cpu(j, sched_group_cpus(sg)); | |
6042 | } | |
0601a88d | 6043 | |
dce840a0 PZ |
6044 | if (!first) |
6045 | first = sg; | |
6046 | if (last) | |
6047 | last->next = sg; | |
6048 | last = sg; | |
6049 | } | |
6050 | last->next = first; | |
e3589f6c PZ |
6051 | |
6052 | return 0; | |
0601a88d | 6053 | } |
51888ca2 | 6054 | |
89c4710e | 6055 | /* |
63b2ca30 | 6056 | * Initialize sched groups cpu_capacity. |
89c4710e | 6057 | * |
63b2ca30 | 6058 | * cpu_capacity indicates the capacity of sched group, which is used while |
89c4710e | 6059 | * distributing the load between different sched groups in a sched domain. |
63b2ca30 NP |
6060 | * Typically cpu_capacity for all the groups in a sched domain will be same |
6061 | * unless there are asymmetries in the topology. If there are asymmetries, | |
6062 | * group having more cpu_capacity will pickup more load compared to the | |
6063 | * group having less cpu_capacity. | |
89c4710e | 6064 | */ |
63b2ca30 | 6065 | static void init_sched_groups_capacity(int cpu, struct sched_domain *sd) |
89c4710e | 6066 | { |
e3589f6c | 6067 | struct sched_group *sg = sd->groups; |
89c4710e | 6068 | |
94c95ba6 | 6069 | WARN_ON(!sg); |
e3589f6c PZ |
6070 | |
6071 | do { | |
6072 | sg->group_weight = cpumask_weight(sched_group_cpus(sg)); | |
6073 | sg = sg->next; | |
6074 | } while (sg != sd->groups); | |
89c4710e | 6075 | |
c1174876 | 6076 | if (cpu != group_balance_cpu(sg)) |
e3589f6c | 6077 | return; |
aae6d3dd | 6078 | |
63b2ca30 NP |
6079 | update_group_capacity(sd, cpu); |
6080 | atomic_set(&sg->sgc->nr_busy_cpus, sg->group_weight); | |
89c4710e SS |
6081 | } |
6082 | ||
7c16ec58 MT |
6083 | /* |
6084 | * Initializers for schedule domains | |
6085 | * Non-inlined to reduce accumulated stack pressure in build_sched_domains() | |
6086 | */ | |
6087 | ||
1d3504fc | 6088 | static int default_relax_domain_level = -1; |
60495e77 | 6089 | int sched_domain_level_max; |
1d3504fc HS |
6090 | |
6091 | static int __init setup_relax_domain_level(char *str) | |
6092 | { | |
a841f8ce DS |
6093 | if (kstrtoint(str, 0, &default_relax_domain_level)) |
6094 | pr_warn("Unable to set relax_domain_level\n"); | |
30e0e178 | 6095 | |
1d3504fc HS |
6096 | return 1; |
6097 | } | |
6098 | __setup("relax_domain_level=", setup_relax_domain_level); | |
6099 | ||
6100 | static void set_domain_attribute(struct sched_domain *sd, | |
6101 | struct sched_domain_attr *attr) | |
6102 | { | |
6103 | int request; | |
6104 | ||
6105 | if (!attr || attr->relax_domain_level < 0) { | |
6106 | if (default_relax_domain_level < 0) | |
6107 | return; | |
6108 | else | |
6109 | request = default_relax_domain_level; | |
6110 | } else | |
6111 | request = attr->relax_domain_level; | |
6112 | if (request < sd->level) { | |
6113 | /* turn off idle balance on this domain */ | |
c88d5910 | 6114 | sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE); |
1d3504fc HS |
6115 | } else { |
6116 | /* turn on idle balance on this domain */ | |
c88d5910 | 6117 | sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE); |
1d3504fc HS |
6118 | } |
6119 | } | |
6120 | ||
54ab4ff4 PZ |
6121 | static void __sdt_free(const struct cpumask *cpu_map); |
6122 | static int __sdt_alloc(const struct cpumask *cpu_map); | |
6123 | ||
2109b99e AH |
6124 | static void __free_domain_allocs(struct s_data *d, enum s_alloc what, |
6125 | const struct cpumask *cpu_map) | |
6126 | { | |
6127 | switch (what) { | |
2109b99e | 6128 | case sa_rootdomain: |
822ff793 PZ |
6129 | if (!atomic_read(&d->rd->refcount)) |
6130 | free_rootdomain(&d->rd->rcu); /* fall through */ | |
21d42ccf PZ |
6131 | case sa_sd: |
6132 | free_percpu(d->sd); /* fall through */ | |
dce840a0 | 6133 | case sa_sd_storage: |
54ab4ff4 | 6134 | __sdt_free(cpu_map); /* fall through */ |
2109b99e AH |
6135 | case sa_none: |
6136 | break; | |
6137 | } | |
6138 | } | |
3404c8d9 | 6139 | |
2109b99e AH |
6140 | static enum s_alloc __visit_domain_allocation_hell(struct s_data *d, |
6141 | const struct cpumask *cpu_map) | |
6142 | { | |
dce840a0 PZ |
6143 | memset(d, 0, sizeof(*d)); |
6144 | ||
54ab4ff4 PZ |
6145 | if (__sdt_alloc(cpu_map)) |
6146 | return sa_sd_storage; | |
dce840a0 PZ |
6147 | d->sd = alloc_percpu(struct sched_domain *); |
6148 | if (!d->sd) | |
6149 | return sa_sd_storage; | |
2109b99e | 6150 | d->rd = alloc_rootdomain(); |
dce840a0 | 6151 | if (!d->rd) |
21d42ccf | 6152 | return sa_sd; |
2109b99e AH |
6153 | return sa_rootdomain; |
6154 | } | |
57d885fe | 6155 | |
dce840a0 PZ |
6156 | /* |
6157 | * NULL the sd_data elements we've used to build the sched_domain and | |
6158 | * sched_group structure so that the subsequent __free_domain_allocs() | |
6159 | * will not free the data we're using. | |
6160 | */ | |
6161 | static void claim_allocations(int cpu, struct sched_domain *sd) | |
6162 | { | |
6163 | struct sd_data *sdd = sd->private; | |
dce840a0 PZ |
6164 | |
6165 | WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd); | |
6166 | *per_cpu_ptr(sdd->sd, cpu) = NULL; | |
6167 | ||
e3589f6c | 6168 | if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref)) |
dce840a0 | 6169 | *per_cpu_ptr(sdd->sg, cpu) = NULL; |
e3589f6c | 6170 | |
63b2ca30 NP |
6171 | if (atomic_read(&(*per_cpu_ptr(sdd->sgc, cpu))->ref)) |
6172 | *per_cpu_ptr(sdd->sgc, cpu) = NULL; | |
dce840a0 PZ |
6173 | } |
6174 | ||
cb83b629 | 6175 | #ifdef CONFIG_NUMA |
cb83b629 | 6176 | static int sched_domains_numa_levels; |
e3fe70b1 | 6177 | enum numa_topology_type sched_numa_topology_type; |
cb83b629 | 6178 | static int *sched_domains_numa_distance; |
9942f79b | 6179 | int sched_max_numa_distance; |
cb83b629 PZ |
6180 | static struct cpumask ***sched_domains_numa_masks; |
6181 | static int sched_domains_curr_level; | |
143e1e28 | 6182 | #endif |
cb83b629 | 6183 | |
143e1e28 VG |
6184 | /* |
6185 | * SD_flags allowed in topology descriptions. | |
6186 | * | |
5d4dfddd | 6187 | * SD_SHARE_CPUCAPACITY - describes SMT topologies |
143e1e28 VG |
6188 | * SD_SHARE_PKG_RESOURCES - describes shared caches |
6189 | * SD_NUMA - describes NUMA topologies | |
d77b3ed5 | 6190 | * SD_SHARE_POWERDOMAIN - describes shared power domain |
143e1e28 VG |
6191 | * |
6192 | * Odd one out: | |
6193 | * SD_ASYM_PACKING - describes SMT quirks | |
6194 | */ | |
6195 | #define TOPOLOGY_SD_FLAGS \ | |
5d4dfddd | 6196 | (SD_SHARE_CPUCAPACITY | \ |
143e1e28 VG |
6197 | SD_SHARE_PKG_RESOURCES | \ |
6198 | SD_NUMA | \ | |
d77b3ed5 VG |
6199 | SD_ASYM_PACKING | \ |
6200 | SD_SHARE_POWERDOMAIN) | |
cb83b629 PZ |
6201 | |
6202 | static struct sched_domain * | |
143e1e28 | 6203 | sd_init(struct sched_domain_topology_level *tl, int cpu) |
cb83b629 PZ |
6204 | { |
6205 | struct sched_domain *sd = *per_cpu_ptr(tl->data.sd, cpu); | |
143e1e28 VG |
6206 | int sd_weight, sd_flags = 0; |
6207 | ||
6208 | #ifdef CONFIG_NUMA | |
6209 | /* | |
6210 | * Ugly hack to pass state to sd_numa_mask()... | |
6211 | */ | |
6212 | sched_domains_curr_level = tl->numa_level; | |
6213 | #endif | |
6214 | ||
6215 | sd_weight = cpumask_weight(tl->mask(cpu)); | |
6216 | ||
6217 | if (tl->sd_flags) | |
6218 | sd_flags = (*tl->sd_flags)(); | |
6219 | if (WARN_ONCE(sd_flags & ~TOPOLOGY_SD_FLAGS, | |
6220 | "wrong sd_flags in topology description\n")) | |
6221 | sd_flags &= ~TOPOLOGY_SD_FLAGS; | |
cb83b629 PZ |
6222 | |
6223 | *sd = (struct sched_domain){ | |
6224 | .min_interval = sd_weight, | |
6225 | .max_interval = 2*sd_weight, | |
6226 | .busy_factor = 32, | |
870a0bb5 | 6227 | .imbalance_pct = 125, |
143e1e28 VG |
6228 | |
6229 | .cache_nice_tries = 0, | |
6230 | .busy_idx = 0, | |
6231 | .idle_idx = 0, | |
cb83b629 PZ |
6232 | .newidle_idx = 0, |
6233 | .wake_idx = 0, | |
6234 | .forkexec_idx = 0, | |
6235 | ||
6236 | .flags = 1*SD_LOAD_BALANCE | |
6237 | | 1*SD_BALANCE_NEWIDLE | |
143e1e28 VG |
6238 | | 1*SD_BALANCE_EXEC |
6239 | | 1*SD_BALANCE_FORK | |
cb83b629 | 6240 | | 0*SD_BALANCE_WAKE |
143e1e28 | 6241 | | 1*SD_WAKE_AFFINE |
5d4dfddd | 6242 | | 0*SD_SHARE_CPUCAPACITY |
cb83b629 | 6243 | | 0*SD_SHARE_PKG_RESOURCES |
143e1e28 | 6244 | | 0*SD_SERIALIZE |
cb83b629 | 6245 | | 0*SD_PREFER_SIBLING |
143e1e28 VG |
6246 | | 0*SD_NUMA |
6247 | | sd_flags | |
cb83b629 | 6248 | , |
143e1e28 | 6249 | |
cb83b629 PZ |
6250 | .last_balance = jiffies, |
6251 | .balance_interval = sd_weight, | |
143e1e28 | 6252 | .smt_gain = 0, |
2b4cfe64 JL |
6253 | .max_newidle_lb_cost = 0, |
6254 | .next_decay_max_lb_cost = jiffies, | |
143e1e28 VG |
6255 | #ifdef CONFIG_SCHED_DEBUG |
6256 | .name = tl->name, | |
6257 | #endif | |
cb83b629 | 6258 | }; |
cb83b629 PZ |
6259 | |
6260 | /* | |
143e1e28 | 6261 | * Convert topological properties into behaviour. |
cb83b629 | 6262 | */ |
143e1e28 | 6263 | |
5d4dfddd | 6264 | if (sd->flags & SD_SHARE_CPUCAPACITY) { |
caff37ef | 6265 | sd->flags |= SD_PREFER_SIBLING; |
143e1e28 VG |
6266 | sd->imbalance_pct = 110; |
6267 | sd->smt_gain = 1178; /* ~15% */ | |
143e1e28 VG |
6268 | |
6269 | } else if (sd->flags & SD_SHARE_PKG_RESOURCES) { | |
6270 | sd->imbalance_pct = 117; | |
6271 | sd->cache_nice_tries = 1; | |
6272 | sd->busy_idx = 2; | |
6273 | ||
6274 | #ifdef CONFIG_NUMA | |
6275 | } else if (sd->flags & SD_NUMA) { | |
6276 | sd->cache_nice_tries = 2; | |
6277 | sd->busy_idx = 3; | |
6278 | sd->idle_idx = 2; | |
6279 | ||
6280 | sd->flags |= SD_SERIALIZE; | |
6281 | if (sched_domains_numa_distance[tl->numa_level] > RECLAIM_DISTANCE) { | |
6282 | sd->flags &= ~(SD_BALANCE_EXEC | | |
6283 | SD_BALANCE_FORK | | |
6284 | SD_WAKE_AFFINE); | |
6285 | } | |
6286 | ||
6287 | #endif | |
6288 | } else { | |
6289 | sd->flags |= SD_PREFER_SIBLING; | |
6290 | sd->cache_nice_tries = 1; | |
6291 | sd->busy_idx = 2; | |
6292 | sd->idle_idx = 1; | |
6293 | } | |
6294 | ||
6295 | sd->private = &tl->data; | |
cb83b629 PZ |
6296 | |
6297 | return sd; | |
6298 | } | |
6299 | ||
143e1e28 VG |
6300 | /* |
6301 | * Topology list, bottom-up. | |
6302 | */ | |
6303 | static struct sched_domain_topology_level default_topology[] = { | |
6304 | #ifdef CONFIG_SCHED_SMT | |
6305 | { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) }, | |
6306 | #endif | |
6307 | #ifdef CONFIG_SCHED_MC | |
6308 | { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) }, | |
143e1e28 VG |
6309 | #endif |
6310 | { cpu_cpu_mask, SD_INIT_NAME(DIE) }, | |
6311 | { NULL, }, | |
6312 | }; | |
6313 | ||
6314 | struct sched_domain_topology_level *sched_domain_topology = default_topology; | |
6315 | ||
6316 | #define for_each_sd_topology(tl) \ | |
6317 | for (tl = sched_domain_topology; tl->mask; tl++) | |
6318 | ||
6319 | void set_sched_topology(struct sched_domain_topology_level *tl) | |
6320 | { | |
6321 | sched_domain_topology = tl; | |
6322 | } | |
6323 | ||
6324 | #ifdef CONFIG_NUMA | |
6325 | ||
cb83b629 PZ |
6326 | static const struct cpumask *sd_numa_mask(int cpu) |
6327 | { | |
6328 | return sched_domains_numa_masks[sched_domains_curr_level][cpu_to_node(cpu)]; | |
6329 | } | |
6330 | ||
d039ac60 PZ |
6331 | static void sched_numa_warn(const char *str) |
6332 | { | |
6333 | static int done = false; | |
6334 | int i,j; | |
6335 | ||
6336 | if (done) | |
6337 | return; | |
6338 | ||
6339 | done = true; | |
6340 | ||
6341 | printk(KERN_WARNING "ERROR: %s\n\n", str); | |
6342 | ||
6343 | for (i = 0; i < nr_node_ids; i++) { | |
6344 | printk(KERN_WARNING " "); | |
6345 | for (j = 0; j < nr_node_ids; j++) | |
6346 | printk(KERN_CONT "%02d ", node_distance(i,j)); | |
6347 | printk(KERN_CONT "\n"); | |
6348 | } | |
6349 | printk(KERN_WARNING "\n"); | |
6350 | } | |
6351 | ||
9942f79b | 6352 | bool find_numa_distance(int distance) |
d039ac60 PZ |
6353 | { |
6354 | int i; | |
6355 | ||
6356 | if (distance == node_distance(0, 0)) | |
6357 | return true; | |
6358 | ||
6359 | for (i = 0; i < sched_domains_numa_levels; i++) { | |
6360 | if (sched_domains_numa_distance[i] == distance) | |
6361 | return true; | |
6362 | } | |
6363 | ||
6364 | return false; | |
6365 | } | |
6366 | ||
e3fe70b1 RR |
6367 | /* |
6368 | * A system can have three types of NUMA topology: | |
6369 | * NUMA_DIRECT: all nodes are directly connected, or not a NUMA system | |
6370 | * NUMA_GLUELESS_MESH: some nodes reachable through intermediary nodes | |
6371 | * NUMA_BACKPLANE: nodes can reach other nodes through a backplane | |
6372 | * | |
6373 | * The difference between a glueless mesh topology and a backplane | |
6374 | * topology lies in whether communication between not directly | |
6375 | * connected nodes goes through intermediary nodes (where programs | |
6376 | * could run), or through backplane controllers. This affects | |
6377 | * placement of programs. | |
6378 | * | |
6379 | * The type of topology can be discerned with the following tests: | |
6380 | * - If the maximum distance between any nodes is 1 hop, the system | |
6381 | * is directly connected. | |
6382 | * - If for two nodes A and B, located N > 1 hops away from each other, | |
6383 | * there is an intermediary node C, which is < N hops away from both | |
6384 | * nodes A and B, the system is a glueless mesh. | |
6385 | */ | |
6386 | static void init_numa_topology_type(void) | |
6387 | { | |
6388 | int a, b, c, n; | |
6389 | ||
6390 | n = sched_max_numa_distance; | |
6391 | ||
6392 | if (n <= 1) | |
6393 | sched_numa_topology_type = NUMA_DIRECT; | |
6394 | ||
6395 | for_each_online_node(a) { | |
6396 | for_each_online_node(b) { | |
6397 | /* Find two nodes furthest removed from each other. */ | |
6398 | if (node_distance(a, b) < n) | |
6399 | continue; | |
6400 | ||
6401 | /* Is there an intermediary node between a and b? */ | |
6402 | for_each_online_node(c) { | |
6403 | if (node_distance(a, c) < n && | |
6404 | node_distance(b, c) < n) { | |
6405 | sched_numa_topology_type = | |
6406 | NUMA_GLUELESS_MESH; | |
6407 | return; | |
6408 | } | |
6409 | } | |
6410 | ||
6411 | sched_numa_topology_type = NUMA_BACKPLANE; | |
6412 | return; | |
6413 | } | |
6414 | } | |
6415 | } | |
6416 | ||
cb83b629 PZ |
6417 | static void sched_init_numa(void) |
6418 | { | |
6419 | int next_distance, curr_distance = node_distance(0, 0); | |
6420 | struct sched_domain_topology_level *tl; | |
6421 | int level = 0; | |
6422 | int i, j, k; | |
6423 | ||
cb83b629 PZ |
6424 | sched_domains_numa_distance = kzalloc(sizeof(int) * nr_node_ids, GFP_KERNEL); |
6425 | if (!sched_domains_numa_distance) | |
6426 | return; | |
6427 | ||
6428 | /* | |
6429 | * O(nr_nodes^2) deduplicating selection sort -- in order to find the | |
6430 | * unique distances in the node_distance() table. | |
6431 | * | |
6432 | * Assumes node_distance(0,j) includes all distances in | |
6433 | * node_distance(i,j) in order to avoid cubic time. | |
cb83b629 PZ |
6434 | */ |
6435 | next_distance = curr_distance; | |
6436 | for (i = 0; i < nr_node_ids; i++) { | |
6437 | for (j = 0; j < nr_node_ids; j++) { | |
d039ac60 PZ |
6438 | for (k = 0; k < nr_node_ids; k++) { |
6439 | int distance = node_distance(i, k); | |
6440 | ||
6441 | if (distance > curr_distance && | |
6442 | (distance < next_distance || | |
6443 | next_distance == curr_distance)) | |
6444 | next_distance = distance; | |
6445 | ||
6446 | /* | |
6447 | * While not a strong assumption it would be nice to know | |
6448 | * about cases where if node A is connected to B, B is not | |
6449 | * equally connected to A. | |
6450 | */ | |
6451 | if (sched_debug() && node_distance(k, i) != distance) | |
6452 | sched_numa_warn("Node-distance not symmetric"); | |
6453 | ||
6454 | if (sched_debug() && i && !find_numa_distance(distance)) | |
6455 | sched_numa_warn("Node-0 not representative"); | |
6456 | } | |
6457 | if (next_distance != curr_distance) { | |
6458 | sched_domains_numa_distance[level++] = next_distance; | |
6459 | sched_domains_numa_levels = level; | |
6460 | curr_distance = next_distance; | |
6461 | } else break; | |
cb83b629 | 6462 | } |
d039ac60 PZ |
6463 | |
6464 | /* | |
6465 | * In case of sched_debug() we verify the above assumption. | |
6466 | */ | |
6467 | if (!sched_debug()) | |
6468 | break; | |
cb83b629 | 6469 | } |
c123588b AR |
6470 | |
6471 | if (!level) | |
6472 | return; | |
6473 | ||
cb83b629 PZ |
6474 | /* |
6475 | * 'level' contains the number of unique distances, excluding the | |
6476 | * identity distance node_distance(i,i). | |
6477 | * | |
28b4a521 | 6478 | * The sched_domains_numa_distance[] array includes the actual distance |
cb83b629 PZ |
6479 | * numbers. |
6480 | */ | |
6481 | ||
5f7865f3 TC |
6482 | /* |
6483 | * Here, we should temporarily reset sched_domains_numa_levels to 0. | |
6484 | * If it fails to allocate memory for array sched_domains_numa_masks[][], | |
6485 | * the array will contain less then 'level' members. This could be | |
6486 | * dangerous when we use it to iterate array sched_domains_numa_masks[][] | |
6487 | * in other functions. | |
6488 | * | |
6489 | * We reset it to 'level' at the end of this function. | |
6490 | */ | |
6491 | sched_domains_numa_levels = 0; | |
6492 | ||
cb83b629 PZ |
6493 | sched_domains_numa_masks = kzalloc(sizeof(void *) * level, GFP_KERNEL); |
6494 | if (!sched_domains_numa_masks) | |
6495 | return; | |
6496 | ||
6497 | /* | |
6498 | * Now for each level, construct a mask per node which contains all | |
6499 | * cpus of nodes that are that many hops away from us. | |
6500 | */ | |
6501 | for (i = 0; i < level; i++) { | |
6502 | sched_domains_numa_masks[i] = | |
6503 | kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL); | |
6504 | if (!sched_domains_numa_masks[i]) | |
6505 | return; | |
6506 | ||
6507 | for (j = 0; j < nr_node_ids; j++) { | |
2ea45800 | 6508 | struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL); |
cb83b629 PZ |
6509 | if (!mask) |
6510 | return; | |
6511 | ||
6512 | sched_domains_numa_masks[i][j] = mask; | |
6513 | ||
6514 | for (k = 0; k < nr_node_ids; k++) { | |
dd7d8634 | 6515 | if (node_distance(j, k) > sched_domains_numa_distance[i]) |
cb83b629 PZ |
6516 | continue; |
6517 | ||
6518 | cpumask_or(mask, mask, cpumask_of_node(k)); | |
6519 | } | |
6520 | } | |
6521 | } | |
6522 | ||
143e1e28 VG |
6523 | /* Compute default topology size */ |
6524 | for (i = 0; sched_domain_topology[i].mask; i++); | |
6525 | ||
c515db8c | 6526 | tl = kzalloc((i + level + 1) * |
cb83b629 PZ |
6527 | sizeof(struct sched_domain_topology_level), GFP_KERNEL); |
6528 | if (!tl) | |
6529 | return; | |
6530 | ||
6531 | /* | |
6532 | * Copy the default topology bits.. | |
6533 | */ | |
143e1e28 VG |
6534 | for (i = 0; sched_domain_topology[i].mask; i++) |
6535 | tl[i] = sched_domain_topology[i]; | |
cb83b629 PZ |
6536 | |
6537 | /* | |
6538 | * .. and append 'j' levels of NUMA goodness. | |
6539 | */ | |
6540 | for (j = 0; j < level; i++, j++) { | |
6541 | tl[i] = (struct sched_domain_topology_level){ | |
cb83b629 | 6542 | .mask = sd_numa_mask, |
143e1e28 | 6543 | .sd_flags = cpu_numa_flags, |
cb83b629 PZ |
6544 | .flags = SDTL_OVERLAP, |
6545 | .numa_level = j, | |
143e1e28 | 6546 | SD_INIT_NAME(NUMA) |
cb83b629 PZ |
6547 | }; |
6548 | } | |
6549 | ||
6550 | sched_domain_topology = tl; | |
5f7865f3 TC |
6551 | |
6552 | sched_domains_numa_levels = level; | |
9942f79b | 6553 | sched_max_numa_distance = sched_domains_numa_distance[level - 1]; |
e3fe70b1 RR |
6554 | |
6555 | init_numa_topology_type(); | |
cb83b629 | 6556 | } |
301a5cba TC |
6557 | |
6558 | static void sched_domains_numa_masks_set(int cpu) | |
6559 | { | |
6560 | int i, j; | |
6561 | int node = cpu_to_node(cpu); | |
6562 | ||
6563 | for (i = 0; i < sched_domains_numa_levels; i++) { | |
6564 | for (j = 0; j < nr_node_ids; j++) { | |
6565 | if (node_distance(j, node) <= sched_domains_numa_distance[i]) | |
6566 | cpumask_set_cpu(cpu, sched_domains_numa_masks[i][j]); | |
6567 | } | |
6568 | } | |
6569 | } | |
6570 | ||
6571 | static void sched_domains_numa_masks_clear(int cpu) | |
6572 | { | |
6573 | int i, j; | |
6574 | for (i = 0; i < sched_domains_numa_levels; i++) { | |
6575 | for (j = 0; j < nr_node_ids; j++) | |
6576 | cpumask_clear_cpu(cpu, sched_domains_numa_masks[i][j]); | |
6577 | } | |
6578 | } | |
6579 | ||
6580 | /* | |
6581 | * Update sched_domains_numa_masks[level][node] array when new cpus | |
6582 | * are onlined. | |
6583 | */ | |
6584 | static int sched_domains_numa_masks_update(struct notifier_block *nfb, | |
6585 | unsigned long action, | |
6586 | void *hcpu) | |
6587 | { | |
6588 | int cpu = (long)hcpu; | |
6589 | ||
6590 | switch (action & ~CPU_TASKS_FROZEN) { | |
6591 | case CPU_ONLINE: | |
6592 | sched_domains_numa_masks_set(cpu); | |
6593 | break; | |
6594 | ||
6595 | case CPU_DEAD: | |
6596 | sched_domains_numa_masks_clear(cpu); | |
6597 | break; | |
6598 | ||
6599 | default: | |
6600 | return NOTIFY_DONE; | |
6601 | } | |
6602 | ||
6603 | return NOTIFY_OK; | |
cb83b629 PZ |
6604 | } |
6605 | #else | |
6606 | static inline void sched_init_numa(void) | |
6607 | { | |
6608 | } | |
301a5cba TC |
6609 | |
6610 | static int sched_domains_numa_masks_update(struct notifier_block *nfb, | |
6611 | unsigned long action, | |
6612 | void *hcpu) | |
6613 | { | |
6614 | return 0; | |
6615 | } | |
cb83b629 PZ |
6616 | #endif /* CONFIG_NUMA */ |
6617 | ||
54ab4ff4 PZ |
6618 | static int __sdt_alloc(const struct cpumask *cpu_map) |
6619 | { | |
6620 | struct sched_domain_topology_level *tl; | |
6621 | int j; | |
6622 | ||
27723a68 | 6623 | for_each_sd_topology(tl) { |
54ab4ff4 PZ |
6624 | struct sd_data *sdd = &tl->data; |
6625 | ||
6626 | sdd->sd = alloc_percpu(struct sched_domain *); | |
6627 | if (!sdd->sd) | |
6628 | return -ENOMEM; | |
6629 | ||
6630 | sdd->sg = alloc_percpu(struct sched_group *); | |
6631 | if (!sdd->sg) | |
6632 | return -ENOMEM; | |
6633 | ||
63b2ca30 NP |
6634 | sdd->sgc = alloc_percpu(struct sched_group_capacity *); |
6635 | if (!sdd->sgc) | |
9c3f75cb PZ |
6636 | return -ENOMEM; |
6637 | ||
54ab4ff4 PZ |
6638 | for_each_cpu(j, cpu_map) { |
6639 | struct sched_domain *sd; | |
6640 | struct sched_group *sg; | |
63b2ca30 | 6641 | struct sched_group_capacity *sgc; |
54ab4ff4 | 6642 | |
5cc389bc | 6643 | sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(), |
54ab4ff4 PZ |
6644 | GFP_KERNEL, cpu_to_node(j)); |
6645 | if (!sd) | |
6646 | return -ENOMEM; | |
6647 | ||
6648 | *per_cpu_ptr(sdd->sd, j) = sd; | |
6649 | ||
6650 | sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(), | |
6651 | GFP_KERNEL, cpu_to_node(j)); | |
6652 | if (!sg) | |
6653 | return -ENOMEM; | |
6654 | ||
30b4e9eb IM |
6655 | sg->next = sg; |
6656 | ||
54ab4ff4 | 6657 | *per_cpu_ptr(sdd->sg, j) = sg; |
9c3f75cb | 6658 | |
63b2ca30 | 6659 | sgc = kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size(), |
9c3f75cb | 6660 | GFP_KERNEL, cpu_to_node(j)); |
63b2ca30 | 6661 | if (!sgc) |
9c3f75cb PZ |
6662 | return -ENOMEM; |
6663 | ||
63b2ca30 | 6664 | *per_cpu_ptr(sdd->sgc, j) = sgc; |
54ab4ff4 PZ |
6665 | } |
6666 | } | |
6667 | ||
6668 | return 0; | |
6669 | } | |
6670 | ||
6671 | static void __sdt_free(const struct cpumask *cpu_map) | |
6672 | { | |
6673 | struct sched_domain_topology_level *tl; | |
6674 | int j; | |
6675 | ||
27723a68 | 6676 | for_each_sd_topology(tl) { |
54ab4ff4 PZ |
6677 | struct sd_data *sdd = &tl->data; |
6678 | ||
6679 | for_each_cpu(j, cpu_map) { | |
fb2cf2c6 | 6680 | struct sched_domain *sd; |
6681 | ||
6682 | if (sdd->sd) { | |
6683 | sd = *per_cpu_ptr(sdd->sd, j); | |
6684 | if (sd && (sd->flags & SD_OVERLAP)) | |
6685 | free_sched_groups(sd->groups, 0); | |
6686 | kfree(*per_cpu_ptr(sdd->sd, j)); | |
6687 | } | |
6688 | ||
6689 | if (sdd->sg) | |
6690 | kfree(*per_cpu_ptr(sdd->sg, j)); | |
63b2ca30 NP |
6691 | if (sdd->sgc) |
6692 | kfree(*per_cpu_ptr(sdd->sgc, j)); | |
54ab4ff4 PZ |
6693 | } |
6694 | free_percpu(sdd->sd); | |
fb2cf2c6 | 6695 | sdd->sd = NULL; |
54ab4ff4 | 6696 | free_percpu(sdd->sg); |
fb2cf2c6 | 6697 | sdd->sg = NULL; |
63b2ca30 NP |
6698 | free_percpu(sdd->sgc); |
6699 | sdd->sgc = NULL; | |
54ab4ff4 PZ |
6700 | } |
6701 | } | |
6702 | ||
2c402dc3 | 6703 | struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl, |
4a850cbe VK |
6704 | const struct cpumask *cpu_map, struct sched_domain_attr *attr, |
6705 | struct sched_domain *child, int cpu) | |
2c402dc3 | 6706 | { |
143e1e28 | 6707 | struct sched_domain *sd = sd_init(tl, cpu); |
2c402dc3 | 6708 | if (!sd) |
d069b916 | 6709 | return child; |
2c402dc3 | 6710 | |
2c402dc3 | 6711 | cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu)); |
60495e77 PZ |
6712 | if (child) { |
6713 | sd->level = child->level + 1; | |
6714 | sched_domain_level_max = max(sched_domain_level_max, sd->level); | |
d069b916 | 6715 | child->parent = sd; |
c75e0128 | 6716 | sd->child = child; |
6ae72dff PZ |
6717 | |
6718 | if (!cpumask_subset(sched_domain_span(child), | |
6719 | sched_domain_span(sd))) { | |
6720 | pr_err("BUG: arch topology borken\n"); | |
6721 | #ifdef CONFIG_SCHED_DEBUG | |
6722 | pr_err(" the %s domain not a subset of the %s domain\n", | |
6723 | child->name, sd->name); | |
6724 | #endif | |
6725 | /* Fixup, ensure @sd has at least @child cpus. */ | |
6726 | cpumask_or(sched_domain_span(sd), | |
6727 | sched_domain_span(sd), | |
6728 | sched_domain_span(child)); | |
6729 | } | |
6730 | ||
60495e77 | 6731 | } |
a841f8ce | 6732 | set_domain_attribute(sd, attr); |
2c402dc3 PZ |
6733 | |
6734 | return sd; | |
6735 | } | |
6736 | ||
2109b99e AH |
6737 | /* |
6738 | * Build sched domains for a given set of cpus and attach the sched domains | |
6739 | * to the individual cpus | |
6740 | */ | |
dce840a0 PZ |
6741 | static int build_sched_domains(const struct cpumask *cpu_map, |
6742 | struct sched_domain_attr *attr) | |
2109b99e | 6743 | { |
1c632169 | 6744 | enum s_alloc alloc_state; |
dce840a0 | 6745 | struct sched_domain *sd; |
2109b99e | 6746 | struct s_data d; |
822ff793 | 6747 | int i, ret = -ENOMEM; |
9c1cfda2 | 6748 | |
2109b99e AH |
6749 | alloc_state = __visit_domain_allocation_hell(&d, cpu_map); |
6750 | if (alloc_state != sa_rootdomain) | |
6751 | goto error; | |
9c1cfda2 | 6752 | |
dce840a0 | 6753 | /* Set up domains for cpus specified by the cpu_map. */ |
abcd083a | 6754 | for_each_cpu(i, cpu_map) { |
eb7a74e6 PZ |
6755 | struct sched_domain_topology_level *tl; |
6756 | ||
3bd65a80 | 6757 | sd = NULL; |
27723a68 | 6758 | for_each_sd_topology(tl) { |
4a850cbe | 6759 | sd = build_sched_domain(tl, cpu_map, attr, sd, i); |
22da9569 VK |
6760 | if (tl == sched_domain_topology) |
6761 | *per_cpu_ptr(d.sd, i) = sd; | |
e3589f6c PZ |
6762 | if (tl->flags & SDTL_OVERLAP || sched_feat(FORCE_SD_OVERLAP)) |
6763 | sd->flags |= SD_OVERLAP; | |
d110235d PZ |
6764 | if (cpumask_equal(cpu_map, sched_domain_span(sd))) |
6765 | break; | |
e3589f6c | 6766 | } |
dce840a0 PZ |
6767 | } |
6768 | ||
6769 | /* Build the groups for the domains */ | |
6770 | for_each_cpu(i, cpu_map) { | |
6771 | for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) { | |
6772 | sd->span_weight = cpumask_weight(sched_domain_span(sd)); | |
e3589f6c PZ |
6773 | if (sd->flags & SD_OVERLAP) { |
6774 | if (build_overlap_sched_groups(sd, i)) | |
6775 | goto error; | |
6776 | } else { | |
6777 | if (build_sched_groups(sd, i)) | |
6778 | goto error; | |
6779 | } | |
1cf51902 | 6780 | } |
a06dadbe | 6781 | } |
9c1cfda2 | 6782 | |
ced549fa | 6783 | /* Calculate CPU capacity for physical packages and nodes */ |
a9c9a9b6 PZ |
6784 | for (i = nr_cpumask_bits-1; i >= 0; i--) { |
6785 | if (!cpumask_test_cpu(i, cpu_map)) | |
6786 | continue; | |
9c1cfda2 | 6787 | |
dce840a0 PZ |
6788 | for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) { |
6789 | claim_allocations(i, sd); | |
63b2ca30 | 6790 | init_sched_groups_capacity(i, sd); |
dce840a0 | 6791 | } |
f712c0c7 | 6792 | } |
9c1cfda2 | 6793 | |
1da177e4 | 6794 | /* Attach the domains */ |
dce840a0 | 6795 | rcu_read_lock(); |
abcd083a | 6796 | for_each_cpu(i, cpu_map) { |
21d42ccf | 6797 | sd = *per_cpu_ptr(d.sd, i); |
49a02c51 | 6798 | cpu_attach_domain(sd, d.rd, i); |
1da177e4 | 6799 | } |
dce840a0 | 6800 | rcu_read_unlock(); |
51888ca2 | 6801 | |
822ff793 | 6802 | ret = 0; |
51888ca2 | 6803 | error: |
2109b99e | 6804 | __free_domain_allocs(&d, alloc_state, cpu_map); |
822ff793 | 6805 | return ret; |
1da177e4 | 6806 | } |
029190c5 | 6807 | |
acc3f5d7 | 6808 | static cpumask_var_t *doms_cur; /* current sched domains */ |
029190c5 | 6809 | static int ndoms_cur; /* number of sched domains in 'doms_cur' */ |
4285f594 IM |
6810 | static struct sched_domain_attr *dattr_cur; |
6811 | /* attribues of custom domains in 'doms_cur' */ | |
029190c5 PJ |
6812 | |
6813 | /* | |
6814 | * Special case: If a kmalloc of a doms_cur partition (array of | |
4212823f RR |
6815 | * cpumask) fails, then fallback to a single sched domain, |
6816 | * as determined by the single cpumask fallback_doms. | |
029190c5 | 6817 | */ |
4212823f | 6818 | static cpumask_var_t fallback_doms; |
029190c5 | 6819 | |
ee79d1bd HC |
6820 | /* |
6821 | * arch_update_cpu_topology lets virtualized architectures update the | |
6822 | * cpu core maps. It is supposed to return 1 if the topology changed | |
6823 | * or 0 if it stayed the same. | |
6824 | */ | |
52f5684c | 6825 | int __weak arch_update_cpu_topology(void) |
22e52b07 | 6826 | { |
ee79d1bd | 6827 | return 0; |
22e52b07 HC |
6828 | } |
6829 | ||
acc3f5d7 RR |
6830 | cpumask_var_t *alloc_sched_domains(unsigned int ndoms) |
6831 | { | |
6832 | int i; | |
6833 | cpumask_var_t *doms; | |
6834 | ||
6835 | doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL); | |
6836 | if (!doms) | |
6837 | return NULL; | |
6838 | for (i = 0; i < ndoms; i++) { | |
6839 | if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) { | |
6840 | free_sched_domains(doms, i); | |
6841 | return NULL; | |
6842 | } | |
6843 | } | |
6844 | return doms; | |
6845 | } | |
6846 | ||
6847 | void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms) | |
6848 | { | |
6849 | unsigned int i; | |
6850 | for (i = 0; i < ndoms; i++) | |
6851 | free_cpumask_var(doms[i]); | |
6852 | kfree(doms); | |
6853 | } | |
6854 | ||
1a20ff27 | 6855 | /* |
41a2d6cf | 6856 | * Set up scheduler domains and groups. Callers must hold the hotplug lock. |
029190c5 PJ |
6857 | * For now this just excludes isolated cpus, but could be used to |
6858 | * exclude other special cases in the future. | |
1a20ff27 | 6859 | */ |
c4a8849a | 6860 | static int init_sched_domains(const struct cpumask *cpu_map) |
1a20ff27 | 6861 | { |
7378547f MM |
6862 | int err; |
6863 | ||
22e52b07 | 6864 | arch_update_cpu_topology(); |
029190c5 | 6865 | ndoms_cur = 1; |
acc3f5d7 | 6866 | doms_cur = alloc_sched_domains(ndoms_cur); |
029190c5 | 6867 | if (!doms_cur) |
acc3f5d7 RR |
6868 | doms_cur = &fallback_doms; |
6869 | cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map); | |
dce840a0 | 6870 | err = build_sched_domains(doms_cur[0], NULL); |
6382bc90 | 6871 | register_sched_domain_sysctl(); |
7378547f MM |
6872 | |
6873 | return err; | |
1a20ff27 DG |
6874 | } |
6875 | ||
1a20ff27 DG |
6876 | /* |
6877 | * Detach sched domains from a group of cpus specified in cpu_map | |
6878 | * These cpus will now be attached to the NULL domain | |
6879 | */ | |
96f874e2 | 6880 | static void detach_destroy_domains(const struct cpumask *cpu_map) |
1a20ff27 DG |
6881 | { |
6882 | int i; | |
6883 | ||
dce840a0 | 6884 | rcu_read_lock(); |
abcd083a | 6885 | for_each_cpu(i, cpu_map) |
57d885fe | 6886 | cpu_attach_domain(NULL, &def_root_domain, i); |
dce840a0 | 6887 | rcu_read_unlock(); |
1a20ff27 DG |
6888 | } |
6889 | ||
1d3504fc HS |
6890 | /* handle null as "default" */ |
6891 | static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur, | |
6892 | struct sched_domain_attr *new, int idx_new) | |
6893 | { | |
6894 | struct sched_domain_attr tmp; | |
6895 | ||
6896 | /* fast path */ | |
6897 | if (!new && !cur) | |
6898 | return 1; | |
6899 | ||
6900 | tmp = SD_ATTR_INIT; | |
6901 | return !memcmp(cur ? (cur + idx_cur) : &tmp, | |
6902 | new ? (new + idx_new) : &tmp, | |
6903 | sizeof(struct sched_domain_attr)); | |
6904 | } | |
6905 | ||
029190c5 PJ |
6906 | /* |
6907 | * Partition sched domains as specified by the 'ndoms_new' | |
41a2d6cf | 6908 | * cpumasks in the array doms_new[] of cpumasks. This compares |
029190c5 PJ |
6909 | * doms_new[] to the current sched domain partitioning, doms_cur[]. |
6910 | * It destroys each deleted domain and builds each new domain. | |
6911 | * | |
acc3f5d7 | 6912 | * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'. |
41a2d6cf IM |
6913 | * The masks don't intersect (don't overlap.) We should setup one |
6914 | * sched domain for each mask. CPUs not in any of the cpumasks will | |
6915 | * not be load balanced. If the same cpumask appears both in the | |
029190c5 PJ |
6916 | * current 'doms_cur' domains and in the new 'doms_new', we can leave |
6917 | * it as it is. | |
6918 | * | |
acc3f5d7 RR |
6919 | * The passed in 'doms_new' should be allocated using |
6920 | * alloc_sched_domains. This routine takes ownership of it and will | |
6921 | * free_sched_domains it when done with it. If the caller failed the | |
6922 | * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1, | |
6923 | * and partition_sched_domains() will fallback to the single partition | |
6924 | * 'fallback_doms', it also forces the domains to be rebuilt. | |
029190c5 | 6925 | * |
96f874e2 | 6926 | * If doms_new == NULL it will be replaced with cpu_online_mask. |
700018e0 LZ |
6927 | * ndoms_new == 0 is a special case for destroying existing domains, |
6928 | * and it will not create the default domain. | |
dfb512ec | 6929 | * |
029190c5 PJ |
6930 | * Call with hotplug lock held |
6931 | */ | |
acc3f5d7 | 6932 | void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[], |
1d3504fc | 6933 | struct sched_domain_attr *dattr_new) |
029190c5 | 6934 | { |
dfb512ec | 6935 | int i, j, n; |
d65bd5ec | 6936 | int new_topology; |
029190c5 | 6937 | |
712555ee | 6938 | mutex_lock(&sched_domains_mutex); |
a1835615 | 6939 | |
7378547f MM |
6940 | /* always unregister in case we don't destroy any domains */ |
6941 | unregister_sched_domain_sysctl(); | |
6942 | ||
d65bd5ec HC |
6943 | /* Let architecture update cpu core mappings. */ |
6944 | new_topology = arch_update_cpu_topology(); | |
6945 | ||
dfb512ec | 6946 | n = doms_new ? ndoms_new : 0; |
029190c5 PJ |
6947 | |
6948 | /* Destroy deleted domains */ | |
6949 | for (i = 0; i < ndoms_cur; i++) { | |
d65bd5ec | 6950 | for (j = 0; j < n && !new_topology; j++) { |
acc3f5d7 | 6951 | if (cpumask_equal(doms_cur[i], doms_new[j]) |
1d3504fc | 6952 | && dattrs_equal(dattr_cur, i, dattr_new, j)) |
029190c5 PJ |
6953 | goto match1; |
6954 | } | |
6955 | /* no match - a current sched domain not in new doms_new[] */ | |
acc3f5d7 | 6956 | detach_destroy_domains(doms_cur[i]); |
029190c5 PJ |
6957 | match1: |
6958 | ; | |
6959 | } | |
6960 | ||
c8d2d47a | 6961 | n = ndoms_cur; |
e761b772 | 6962 | if (doms_new == NULL) { |
c8d2d47a | 6963 | n = 0; |
acc3f5d7 | 6964 | doms_new = &fallback_doms; |
6ad4c188 | 6965 | cpumask_andnot(doms_new[0], cpu_active_mask, cpu_isolated_map); |
faa2f98f | 6966 | WARN_ON_ONCE(dattr_new); |
e761b772 MK |
6967 | } |
6968 | ||
029190c5 PJ |
6969 | /* Build new domains */ |
6970 | for (i = 0; i < ndoms_new; i++) { | |
c8d2d47a | 6971 | for (j = 0; j < n && !new_topology; j++) { |
acc3f5d7 | 6972 | if (cpumask_equal(doms_new[i], doms_cur[j]) |
1d3504fc | 6973 | && dattrs_equal(dattr_new, i, dattr_cur, j)) |
029190c5 PJ |
6974 | goto match2; |
6975 | } | |
6976 | /* no match - add a new doms_new */ | |
dce840a0 | 6977 | build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL); |
029190c5 PJ |
6978 | match2: |
6979 | ; | |
6980 | } | |
6981 | ||
6982 | /* Remember the new sched domains */ | |
acc3f5d7 RR |
6983 | if (doms_cur != &fallback_doms) |
6984 | free_sched_domains(doms_cur, ndoms_cur); | |
1d3504fc | 6985 | kfree(dattr_cur); /* kfree(NULL) is safe */ |
029190c5 | 6986 | doms_cur = doms_new; |
1d3504fc | 6987 | dattr_cur = dattr_new; |
029190c5 | 6988 | ndoms_cur = ndoms_new; |
7378547f MM |
6989 | |
6990 | register_sched_domain_sysctl(); | |
a1835615 | 6991 | |
712555ee | 6992 | mutex_unlock(&sched_domains_mutex); |
029190c5 PJ |
6993 | } |
6994 | ||
d35be8ba SB |
6995 | static int num_cpus_frozen; /* used to mark begin/end of suspend/resume */ |
6996 | ||
1da177e4 | 6997 | /* |
3a101d05 TH |
6998 | * Update cpusets according to cpu_active mask. If cpusets are |
6999 | * disabled, cpuset_update_active_cpus() becomes a simple wrapper | |
7000 | * around partition_sched_domains(). | |
d35be8ba SB |
7001 | * |
7002 | * If we come here as part of a suspend/resume, don't touch cpusets because we | |
7003 | * want to restore it back to its original state upon resume anyway. | |
1da177e4 | 7004 | */ |
0b2e918a TH |
7005 | static int cpuset_cpu_active(struct notifier_block *nfb, unsigned long action, |
7006 | void *hcpu) | |
e761b772 | 7007 | { |
d35be8ba SB |
7008 | switch (action) { |
7009 | case CPU_ONLINE_FROZEN: | |
7010 | case CPU_DOWN_FAILED_FROZEN: | |
7011 | ||
7012 | /* | |
7013 | * num_cpus_frozen tracks how many CPUs are involved in suspend | |
7014 | * resume sequence. As long as this is not the last online | |
7015 | * operation in the resume sequence, just build a single sched | |
7016 | * domain, ignoring cpusets. | |
7017 | */ | |
7018 | num_cpus_frozen--; | |
7019 | if (likely(num_cpus_frozen)) { | |
7020 | partition_sched_domains(1, NULL, NULL); | |
7021 | break; | |
7022 | } | |
7023 | ||
7024 | /* | |
7025 | * This is the last CPU online operation. So fall through and | |
7026 | * restore the original sched domains by considering the | |
7027 | * cpuset configurations. | |
7028 | */ | |
7029 | ||
e761b772 | 7030 | case CPU_ONLINE: |
7ddf96b0 | 7031 | cpuset_update_active_cpus(true); |
d35be8ba | 7032 | break; |
3a101d05 TH |
7033 | default: |
7034 | return NOTIFY_DONE; | |
7035 | } | |
d35be8ba | 7036 | return NOTIFY_OK; |
3a101d05 | 7037 | } |
e761b772 | 7038 | |
0b2e918a TH |
7039 | static int cpuset_cpu_inactive(struct notifier_block *nfb, unsigned long action, |
7040 | void *hcpu) | |
3a101d05 | 7041 | { |
3c18d447 JL |
7042 | unsigned long flags; |
7043 | long cpu = (long)hcpu; | |
7044 | struct dl_bw *dl_b; | |
533445c6 OS |
7045 | bool overflow; |
7046 | int cpus; | |
3c18d447 | 7047 | |
533445c6 | 7048 | switch (action) { |
3a101d05 | 7049 | case CPU_DOWN_PREPARE: |
533445c6 OS |
7050 | rcu_read_lock_sched(); |
7051 | dl_b = dl_bw_of(cpu); | |
3c18d447 | 7052 | |
533445c6 OS |
7053 | raw_spin_lock_irqsave(&dl_b->lock, flags); |
7054 | cpus = dl_bw_cpus(cpu); | |
7055 | overflow = __dl_overflow(dl_b, cpus, 0, 0); | |
7056 | raw_spin_unlock_irqrestore(&dl_b->lock, flags); | |
3c18d447 | 7057 | |
533445c6 | 7058 | rcu_read_unlock_sched(); |
3c18d447 | 7059 | |
533445c6 OS |
7060 | if (overflow) |
7061 | return notifier_from_errno(-EBUSY); | |
7ddf96b0 | 7062 | cpuset_update_active_cpus(false); |
d35be8ba SB |
7063 | break; |
7064 | case CPU_DOWN_PREPARE_FROZEN: | |
7065 | num_cpus_frozen++; | |
7066 | partition_sched_domains(1, NULL, NULL); | |
7067 | break; | |
e761b772 MK |
7068 | default: |
7069 | return NOTIFY_DONE; | |
7070 | } | |
d35be8ba | 7071 | return NOTIFY_OK; |
e761b772 | 7072 | } |
e761b772 | 7073 | |
1da177e4 LT |
7074 | void __init sched_init_smp(void) |
7075 | { | |
dcc30a35 RR |
7076 | cpumask_var_t non_isolated_cpus; |
7077 | ||
7078 | alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL); | |
cb5fd13f | 7079 | alloc_cpumask_var(&fallback_doms, GFP_KERNEL); |
5c1e1767 | 7080 | |
cb83b629 PZ |
7081 | sched_init_numa(); |
7082 | ||
6acce3ef PZ |
7083 | /* |
7084 | * There's no userspace yet to cause hotplug operations; hence all the | |
7085 | * cpu masks are stable and all blatant races in the below code cannot | |
7086 | * happen. | |
7087 | */ | |
712555ee | 7088 | mutex_lock(&sched_domains_mutex); |
c4a8849a | 7089 | init_sched_domains(cpu_active_mask); |
dcc30a35 RR |
7090 | cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map); |
7091 | if (cpumask_empty(non_isolated_cpus)) | |
7092 | cpumask_set_cpu(smp_processor_id(), non_isolated_cpus); | |
712555ee | 7093 | mutex_unlock(&sched_domains_mutex); |
e761b772 | 7094 | |
301a5cba | 7095 | hotcpu_notifier(sched_domains_numa_masks_update, CPU_PRI_SCHED_ACTIVE); |
3a101d05 TH |
7096 | hotcpu_notifier(cpuset_cpu_active, CPU_PRI_CPUSET_ACTIVE); |
7097 | hotcpu_notifier(cpuset_cpu_inactive, CPU_PRI_CPUSET_INACTIVE); | |
e761b772 | 7098 | |
b328ca18 | 7099 | init_hrtick(); |
5c1e1767 NP |
7100 | |
7101 | /* Move init over to a non-isolated CPU */ | |
dcc30a35 | 7102 | if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0) |
5c1e1767 | 7103 | BUG(); |
19978ca6 | 7104 | sched_init_granularity(); |
dcc30a35 | 7105 | free_cpumask_var(non_isolated_cpus); |
4212823f | 7106 | |
0e3900e6 | 7107 | init_sched_rt_class(); |
1baca4ce | 7108 | init_sched_dl_class(); |
1da177e4 LT |
7109 | } |
7110 | #else | |
7111 | void __init sched_init_smp(void) | |
7112 | { | |
19978ca6 | 7113 | sched_init_granularity(); |
1da177e4 LT |
7114 | } |
7115 | #endif /* CONFIG_SMP */ | |
7116 | ||
cd1bb94b AB |
7117 | const_debug unsigned int sysctl_timer_migration = 1; |
7118 | ||
1da177e4 LT |
7119 | int in_sched_functions(unsigned long addr) |
7120 | { | |
1da177e4 LT |
7121 | return in_lock_functions(addr) || |
7122 | (addr >= (unsigned long)__sched_text_start | |
7123 | && addr < (unsigned long)__sched_text_end); | |
7124 | } | |
7125 | ||
029632fb | 7126 | #ifdef CONFIG_CGROUP_SCHED |
27b4b931 LZ |
7127 | /* |
7128 | * Default task group. | |
7129 | * Every task in system belongs to this group at bootup. | |
7130 | */ | |
029632fb | 7131 | struct task_group root_task_group; |
35cf4e50 | 7132 | LIST_HEAD(task_groups); |
052f1dc7 | 7133 | #endif |
6f505b16 | 7134 | |
e6252c3e | 7135 | DECLARE_PER_CPU(cpumask_var_t, load_balance_mask); |
6f505b16 | 7136 | |
1da177e4 LT |
7137 | void __init sched_init(void) |
7138 | { | |
dd41f596 | 7139 | int i, j; |
434d53b0 MT |
7140 | unsigned long alloc_size = 0, ptr; |
7141 | ||
7142 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
7143 | alloc_size += 2 * nr_cpu_ids * sizeof(void **); | |
7144 | #endif | |
7145 | #ifdef CONFIG_RT_GROUP_SCHED | |
7146 | alloc_size += 2 * nr_cpu_ids * sizeof(void **); | |
7147 | #endif | |
434d53b0 | 7148 | if (alloc_size) { |
36b7b6d4 | 7149 | ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT); |
434d53b0 MT |
7150 | |
7151 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
07e06b01 | 7152 | root_task_group.se = (struct sched_entity **)ptr; |
434d53b0 MT |
7153 | ptr += nr_cpu_ids * sizeof(void **); |
7154 | ||
07e06b01 | 7155 | root_task_group.cfs_rq = (struct cfs_rq **)ptr; |
434d53b0 | 7156 | ptr += nr_cpu_ids * sizeof(void **); |
eff766a6 | 7157 | |
6d6bc0ad | 7158 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
434d53b0 | 7159 | #ifdef CONFIG_RT_GROUP_SCHED |
07e06b01 | 7160 | root_task_group.rt_se = (struct sched_rt_entity **)ptr; |
434d53b0 MT |
7161 | ptr += nr_cpu_ids * sizeof(void **); |
7162 | ||
07e06b01 | 7163 | root_task_group.rt_rq = (struct rt_rq **)ptr; |
eff766a6 PZ |
7164 | ptr += nr_cpu_ids * sizeof(void **); |
7165 | ||
6d6bc0ad | 7166 | #endif /* CONFIG_RT_GROUP_SCHED */ |
b74e6278 | 7167 | } |
df7c8e84 | 7168 | #ifdef CONFIG_CPUMASK_OFFSTACK |
b74e6278 AT |
7169 | for_each_possible_cpu(i) { |
7170 | per_cpu(load_balance_mask, i) = (cpumask_var_t)kzalloc_node( | |
7171 | cpumask_size(), GFP_KERNEL, cpu_to_node(i)); | |
434d53b0 | 7172 | } |
b74e6278 | 7173 | #endif /* CONFIG_CPUMASK_OFFSTACK */ |
dd41f596 | 7174 | |
332ac17e DF |
7175 | init_rt_bandwidth(&def_rt_bandwidth, |
7176 | global_rt_period(), global_rt_runtime()); | |
7177 | init_dl_bandwidth(&def_dl_bandwidth, | |
1724813d | 7178 | global_rt_period(), global_rt_runtime()); |
332ac17e | 7179 | |
57d885fe GH |
7180 | #ifdef CONFIG_SMP |
7181 | init_defrootdomain(); | |
7182 | #endif | |
7183 | ||
d0b27fa7 | 7184 | #ifdef CONFIG_RT_GROUP_SCHED |
07e06b01 | 7185 | init_rt_bandwidth(&root_task_group.rt_bandwidth, |
d0b27fa7 | 7186 | global_rt_period(), global_rt_runtime()); |
6d6bc0ad | 7187 | #endif /* CONFIG_RT_GROUP_SCHED */ |
d0b27fa7 | 7188 | |
7c941438 | 7189 | #ifdef CONFIG_CGROUP_SCHED |
07e06b01 YZ |
7190 | list_add(&root_task_group.list, &task_groups); |
7191 | INIT_LIST_HEAD(&root_task_group.children); | |
f4d6f6c2 | 7192 | INIT_LIST_HEAD(&root_task_group.siblings); |
5091faa4 | 7193 | autogroup_init(&init_task); |
54c707e9 | 7194 | |
7c941438 | 7195 | #endif /* CONFIG_CGROUP_SCHED */ |
6f505b16 | 7196 | |
0a945022 | 7197 | for_each_possible_cpu(i) { |
70b97a7f | 7198 | struct rq *rq; |
1da177e4 LT |
7199 | |
7200 | rq = cpu_rq(i); | |
05fa785c | 7201 | raw_spin_lock_init(&rq->lock); |
7897986b | 7202 | rq->nr_running = 0; |
dce48a84 TG |
7203 | rq->calc_load_active = 0; |
7204 | rq->calc_load_update = jiffies + LOAD_FREQ; | |
acb5a9ba | 7205 | init_cfs_rq(&rq->cfs); |
07c54f7a AV |
7206 | init_rt_rq(&rq->rt); |
7207 | init_dl_rq(&rq->dl); | |
dd41f596 | 7208 | #ifdef CONFIG_FAIR_GROUP_SCHED |
029632fb | 7209 | root_task_group.shares = ROOT_TASK_GROUP_LOAD; |
6f505b16 | 7210 | INIT_LIST_HEAD(&rq->leaf_cfs_rq_list); |
354d60c2 | 7211 | /* |
07e06b01 | 7212 | * How much cpu bandwidth does root_task_group get? |
354d60c2 DG |
7213 | * |
7214 | * In case of task-groups formed thr' the cgroup filesystem, it | |
7215 | * gets 100% of the cpu resources in the system. This overall | |
7216 | * system cpu resource is divided among the tasks of | |
07e06b01 | 7217 | * root_task_group and its child task-groups in a fair manner, |
354d60c2 DG |
7218 | * based on each entity's (task or task-group's) weight |
7219 | * (se->load.weight). | |
7220 | * | |
07e06b01 | 7221 | * In other words, if root_task_group has 10 tasks of weight |
354d60c2 DG |
7222 | * 1024) and two child groups A0 and A1 (of weight 1024 each), |
7223 | * then A0's share of the cpu resource is: | |
7224 | * | |
0d905bca | 7225 | * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33% |
354d60c2 | 7226 | * |
07e06b01 YZ |
7227 | * We achieve this by letting root_task_group's tasks sit |
7228 | * directly in rq->cfs (i.e root_task_group->se[] = NULL). | |
354d60c2 | 7229 | */ |
ab84d31e | 7230 | init_cfs_bandwidth(&root_task_group.cfs_bandwidth); |
07e06b01 | 7231 | init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL); |
354d60c2 DG |
7232 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
7233 | ||
7234 | rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime; | |
052f1dc7 | 7235 | #ifdef CONFIG_RT_GROUP_SCHED |
07e06b01 | 7236 | init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL); |
dd41f596 | 7237 | #endif |
1da177e4 | 7238 | |
dd41f596 IM |
7239 | for (j = 0; j < CPU_LOAD_IDX_MAX; j++) |
7240 | rq->cpu_load[j] = 0; | |
fdf3e95d VP |
7241 | |
7242 | rq->last_load_update_tick = jiffies; | |
7243 | ||
1da177e4 | 7244 | #ifdef CONFIG_SMP |
41c7ce9a | 7245 | rq->sd = NULL; |
57d885fe | 7246 | rq->rd = NULL; |
ca6d75e6 | 7247 | rq->cpu_capacity = rq->cpu_capacity_orig = SCHED_CAPACITY_SCALE; |
e3fca9e7 | 7248 | rq->balance_callback = NULL; |
1da177e4 | 7249 | rq->active_balance = 0; |
dd41f596 | 7250 | rq->next_balance = jiffies; |
1da177e4 | 7251 | rq->push_cpu = 0; |
0a2966b4 | 7252 | rq->cpu = i; |
1f11eb6a | 7253 | rq->online = 0; |
eae0c9df MG |
7254 | rq->idle_stamp = 0; |
7255 | rq->avg_idle = 2*sysctl_sched_migration_cost; | |
9bd721c5 | 7256 | rq->max_idle_balance_cost = sysctl_sched_migration_cost; |
367456c7 PZ |
7257 | |
7258 | INIT_LIST_HEAD(&rq->cfs_tasks); | |
7259 | ||
dc938520 | 7260 | rq_attach_root(rq, &def_root_domain); |
3451d024 | 7261 | #ifdef CONFIG_NO_HZ_COMMON |
1c792db7 | 7262 | rq->nohz_flags = 0; |
83cd4fe2 | 7263 | #endif |
265f22a9 FW |
7264 | #ifdef CONFIG_NO_HZ_FULL |
7265 | rq->last_sched_tick = 0; | |
7266 | #endif | |
1da177e4 | 7267 | #endif |
8f4d37ec | 7268 | init_rq_hrtick(rq); |
1da177e4 | 7269 | atomic_set(&rq->nr_iowait, 0); |
1da177e4 LT |
7270 | } |
7271 | ||
2dd73a4f | 7272 | set_load_weight(&init_task); |
b50f60ce | 7273 | |
e107be36 AK |
7274 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
7275 | INIT_HLIST_HEAD(&init_task.preempt_notifiers); | |
7276 | #endif | |
7277 | ||
1da177e4 LT |
7278 | /* |
7279 | * The boot idle thread does lazy MMU switching as well: | |
7280 | */ | |
7281 | atomic_inc(&init_mm.mm_count); | |
7282 | enter_lazy_tlb(&init_mm, current); | |
7283 | ||
1b537c7d YD |
7284 | /* |
7285 | * During early bootup we pretend to be a normal task: | |
7286 | */ | |
7287 | current->sched_class = &fair_sched_class; | |
7288 | ||
1da177e4 LT |
7289 | /* |
7290 | * Make us the idle thread. Technically, schedule() should not be | |
7291 | * called from this thread, however somewhere below it might be, | |
7292 | * but because we are the idle thread, we just pick up running again | |
7293 | * when this runqueue becomes "idle". | |
7294 | */ | |
7295 | init_idle(current, smp_processor_id()); | |
dce48a84 TG |
7296 | |
7297 | calc_load_update = jiffies + LOAD_FREQ; | |
7298 | ||
bf4d83f6 | 7299 | #ifdef CONFIG_SMP |
4cb98839 | 7300 | zalloc_cpumask_var(&sched_domains_tmpmask, GFP_NOWAIT); |
bdddd296 RR |
7301 | /* May be allocated at isolcpus cmdline parse time */ |
7302 | if (cpu_isolated_map == NULL) | |
7303 | zalloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT); | |
29d5e047 | 7304 | idle_thread_set_boot_cpu(); |
a803f026 | 7305 | set_cpu_rq_start_time(); |
029632fb PZ |
7306 | #endif |
7307 | init_sched_fair_class(); | |
6a7b3dc3 | 7308 | |
6892b75e | 7309 | scheduler_running = 1; |
1da177e4 LT |
7310 | } |
7311 | ||
d902db1e | 7312 | #ifdef CONFIG_DEBUG_ATOMIC_SLEEP |
e4aafea2 FW |
7313 | static inline int preempt_count_equals(int preempt_offset) |
7314 | { | |
234da7bc | 7315 | int nested = (preempt_count() & ~PREEMPT_ACTIVE) + rcu_preempt_depth(); |
e4aafea2 | 7316 | |
4ba8216c | 7317 | return (nested == preempt_offset); |
e4aafea2 FW |
7318 | } |
7319 | ||
d894837f | 7320 | void __might_sleep(const char *file, int line, int preempt_offset) |
1da177e4 | 7321 | { |
8eb23b9f PZ |
7322 | /* |
7323 | * Blocking primitives will set (and therefore destroy) current->state, | |
7324 | * since we will exit with TASK_RUNNING make sure we enter with it, | |
7325 | * otherwise we will destroy state. | |
7326 | */ | |
00845eb9 | 7327 | WARN_ONCE(current->state != TASK_RUNNING && current->task_state_change, |
8eb23b9f PZ |
7328 | "do not call blocking ops when !TASK_RUNNING; " |
7329 | "state=%lx set at [<%p>] %pS\n", | |
7330 | current->state, | |
7331 | (void *)current->task_state_change, | |
00845eb9 | 7332 | (void *)current->task_state_change); |
8eb23b9f | 7333 | |
3427445a PZ |
7334 | ___might_sleep(file, line, preempt_offset); |
7335 | } | |
7336 | EXPORT_SYMBOL(__might_sleep); | |
7337 | ||
7338 | void ___might_sleep(const char *file, int line, int preempt_offset) | |
1da177e4 | 7339 | { |
1da177e4 LT |
7340 | static unsigned long prev_jiffy; /* ratelimiting */ |
7341 | ||
b3fbab05 | 7342 | rcu_sleep_check(); /* WARN_ON_ONCE() by default, no rate limit reqd. */ |
db273be2 TG |
7343 | if ((preempt_count_equals(preempt_offset) && !irqs_disabled() && |
7344 | !is_idle_task(current)) || | |
e4aafea2 | 7345 | system_state != SYSTEM_RUNNING || oops_in_progress) |
aef745fc IM |
7346 | return; |
7347 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) | |
7348 | return; | |
7349 | prev_jiffy = jiffies; | |
7350 | ||
3df0fc5b PZ |
7351 | printk(KERN_ERR |
7352 | "BUG: sleeping function called from invalid context at %s:%d\n", | |
7353 | file, line); | |
7354 | printk(KERN_ERR | |
7355 | "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n", | |
7356 | in_atomic(), irqs_disabled(), | |
7357 | current->pid, current->comm); | |
aef745fc | 7358 | |
a8b686b3 ES |
7359 | if (task_stack_end_corrupted(current)) |
7360 | printk(KERN_EMERG "Thread overran stack, or stack corrupted\n"); | |
7361 | ||
aef745fc IM |
7362 | debug_show_held_locks(current); |
7363 | if (irqs_disabled()) | |
7364 | print_irqtrace_events(current); | |
8f47b187 TG |
7365 | #ifdef CONFIG_DEBUG_PREEMPT |
7366 | if (!preempt_count_equals(preempt_offset)) { | |
7367 | pr_err("Preemption disabled at:"); | |
7368 | print_ip_sym(current->preempt_disable_ip); | |
7369 | pr_cont("\n"); | |
7370 | } | |
7371 | #endif | |
aef745fc | 7372 | dump_stack(); |
1da177e4 | 7373 | } |
3427445a | 7374 | EXPORT_SYMBOL(___might_sleep); |
1da177e4 LT |
7375 | #endif |
7376 | ||
7377 | #ifdef CONFIG_MAGIC_SYSRQ | |
dbc7f069 | 7378 | void normalize_rt_tasks(void) |
3a5e4dc1 | 7379 | { |
dbc7f069 | 7380 | struct task_struct *g, *p; |
d50dde5a DF |
7381 | struct sched_attr attr = { |
7382 | .sched_policy = SCHED_NORMAL, | |
7383 | }; | |
1da177e4 | 7384 | |
3472eaa1 | 7385 | read_lock(&tasklist_lock); |
5d07f420 | 7386 | for_each_process_thread(g, p) { |
178be793 IM |
7387 | /* |
7388 | * Only normalize user tasks: | |
7389 | */ | |
3472eaa1 | 7390 | if (p->flags & PF_KTHREAD) |
178be793 IM |
7391 | continue; |
7392 | ||
6cfb0d5d | 7393 | p->se.exec_start = 0; |
6cfb0d5d | 7394 | #ifdef CONFIG_SCHEDSTATS |
41acab88 LDM |
7395 | p->se.statistics.wait_start = 0; |
7396 | p->se.statistics.sleep_start = 0; | |
7397 | p->se.statistics.block_start = 0; | |
6cfb0d5d | 7398 | #endif |
dd41f596 | 7399 | |
aab03e05 | 7400 | if (!dl_task(p) && !rt_task(p)) { |
dd41f596 IM |
7401 | /* |
7402 | * Renice negative nice level userspace | |
7403 | * tasks back to 0: | |
7404 | */ | |
3472eaa1 | 7405 | if (task_nice(p) < 0) |
dd41f596 | 7406 | set_user_nice(p, 0); |
1da177e4 | 7407 | continue; |
dd41f596 | 7408 | } |
1da177e4 | 7409 | |
dbc7f069 | 7410 | __sched_setscheduler(p, &attr, false, false); |
5d07f420 | 7411 | } |
3472eaa1 | 7412 | read_unlock(&tasklist_lock); |
1da177e4 LT |
7413 | } |
7414 | ||
7415 | #endif /* CONFIG_MAGIC_SYSRQ */ | |
1df5c10a | 7416 | |
67fc4e0c | 7417 | #if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) |
1df5c10a | 7418 | /* |
67fc4e0c | 7419 | * These functions are only useful for the IA64 MCA handling, or kdb. |
1df5c10a LT |
7420 | * |
7421 | * They can only be called when the whole system has been | |
7422 | * stopped - every CPU needs to be quiescent, and no scheduling | |
7423 | * activity can take place. Using them for anything else would | |
7424 | * be a serious bug, and as a result, they aren't even visible | |
7425 | * under any other configuration. | |
7426 | */ | |
7427 | ||
7428 | /** | |
7429 | * curr_task - return the current task for a given cpu. | |
7430 | * @cpu: the processor in question. | |
7431 | * | |
7432 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! | |
e69f6186 YB |
7433 | * |
7434 | * Return: The current task for @cpu. | |
1df5c10a | 7435 | */ |
36c8b586 | 7436 | struct task_struct *curr_task(int cpu) |
1df5c10a LT |
7437 | { |
7438 | return cpu_curr(cpu); | |
7439 | } | |
7440 | ||
67fc4e0c JW |
7441 | #endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */ |
7442 | ||
7443 | #ifdef CONFIG_IA64 | |
1df5c10a LT |
7444 | /** |
7445 | * set_curr_task - set the current task for a given cpu. | |
7446 | * @cpu: the processor in question. | |
7447 | * @p: the task pointer to set. | |
7448 | * | |
7449 | * Description: This function must only be used when non-maskable interrupts | |
41a2d6cf IM |
7450 | * are serviced on a separate stack. It allows the architecture to switch the |
7451 | * notion of the current task on a cpu in a non-blocking manner. This function | |
1df5c10a LT |
7452 | * must be called with all CPU's synchronized, and interrupts disabled, the |
7453 | * and caller must save the original value of the current task (see | |
7454 | * curr_task() above) and restore that value before reenabling interrupts and | |
7455 | * re-starting the system. | |
7456 | * | |
7457 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! | |
7458 | */ | |
36c8b586 | 7459 | void set_curr_task(int cpu, struct task_struct *p) |
1df5c10a LT |
7460 | { |
7461 | cpu_curr(cpu) = p; | |
7462 | } | |
7463 | ||
7464 | #endif | |
29f59db3 | 7465 | |
7c941438 | 7466 | #ifdef CONFIG_CGROUP_SCHED |
029632fb PZ |
7467 | /* task_group_lock serializes the addition/removal of task groups */ |
7468 | static DEFINE_SPINLOCK(task_group_lock); | |
7469 | ||
bccbe08a PZ |
7470 | static void free_sched_group(struct task_group *tg) |
7471 | { | |
7472 | free_fair_sched_group(tg); | |
7473 | free_rt_sched_group(tg); | |
e9aa1dd1 | 7474 | autogroup_free(tg); |
bccbe08a PZ |
7475 | kfree(tg); |
7476 | } | |
7477 | ||
7478 | /* allocate runqueue etc for a new task group */ | |
ec7dc8ac | 7479 | struct task_group *sched_create_group(struct task_group *parent) |
bccbe08a PZ |
7480 | { |
7481 | struct task_group *tg; | |
bccbe08a PZ |
7482 | |
7483 | tg = kzalloc(sizeof(*tg), GFP_KERNEL); | |
7484 | if (!tg) | |
7485 | return ERR_PTR(-ENOMEM); | |
7486 | ||
ec7dc8ac | 7487 | if (!alloc_fair_sched_group(tg, parent)) |
bccbe08a PZ |
7488 | goto err; |
7489 | ||
ec7dc8ac | 7490 | if (!alloc_rt_sched_group(tg, parent)) |
bccbe08a PZ |
7491 | goto err; |
7492 | ||
ace783b9 LZ |
7493 | return tg; |
7494 | ||
7495 | err: | |
7496 | free_sched_group(tg); | |
7497 | return ERR_PTR(-ENOMEM); | |
7498 | } | |
7499 | ||
7500 | void sched_online_group(struct task_group *tg, struct task_group *parent) | |
7501 | { | |
7502 | unsigned long flags; | |
7503 | ||
8ed36996 | 7504 | spin_lock_irqsave(&task_group_lock, flags); |
6f505b16 | 7505 | list_add_rcu(&tg->list, &task_groups); |
f473aa5e PZ |
7506 | |
7507 | WARN_ON(!parent); /* root should already exist */ | |
7508 | ||
7509 | tg->parent = parent; | |
f473aa5e | 7510 | INIT_LIST_HEAD(&tg->children); |
09f2724a | 7511 | list_add_rcu(&tg->siblings, &parent->children); |
8ed36996 | 7512 | spin_unlock_irqrestore(&task_group_lock, flags); |
29f59db3 SV |
7513 | } |
7514 | ||
9b5b7751 | 7515 | /* rcu callback to free various structures associated with a task group */ |
6f505b16 | 7516 | static void free_sched_group_rcu(struct rcu_head *rhp) |
29f59db3 | 7517 | { |
29f59db3 | 7518 | /* now it should be safe to free those cfs_rqs */ |
6f505b16 | 7519 | free_sched_group(container_of(rhp, struct task_group, rcu)); |
29f59db3 SV |
7520 | } |
7521 | ||
9b5b7751 | 7522 | /* Destroy runqueue etc associated with a task group */ |
4cf86d77 | 7523 | void sched_destroy_group(struct task_group *tg) |
ace783b9 LZ |
7524 | { |
7525 | /* wait for possible concurrent references to cfs_rqs complete */ | |
7526 | call_rcu(&tg->rcu, free_sched_group_rcu); | |
7527 | } | |
7528 | ||
7529 | void sched_offline_group(struct task_group *tg) | |
29f59db3 | 7530 | { |
8ed36996 | 7531 | unsigned long flags; |
9b5b7751 | 7532 | int i; |
29f59db3 | 7533 | |
3d4b47b4 PZ |
7534 | /* end participation in shares distribution */ |
7535 | for_each_possible_cpu(i) | |
bccbe08a | 7536 | unregister_fair_sched_group(tg, i); |
3d4b47b4 PZ |
7537 | |
7538 | spin_lock_irqsave(&task_group_lock, flags); | |
6f505b16 | 7539 | list_del_rcu(&tg->list); |
f473aa5e | 7540 | list_del_rcu(&tg->siblings); |
8ed36996 | 7541 | spin_unlock_irqrestore(&task_group_lock, flags); |
29f59db3 SV |
7542 | } |
7543 | ||
9b5b7751 | 7544 | /* change task's runqueue when it moves between groups. |
3a252015 IM |
7545 | * The caller of this function should have put the task in its new group |
7546 | * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to | |
7547 | * reflect its new group. | |
9b5b7751 SV |
7548 | */ |
7549 | void sched_move_task(struct task_struct *tsk) | |
29f59db3 | 7550 | { |
8323f26c | 7551 | struct task_group *tg; |
da0c1e65 | 7552 | int queued, running; |
29f59db3 SV |
7553 | unsigned long flags; |
7554 | struct rq *rq; | |
7555 | ||
7556 | rq = task_rq_lock(tsk, &flags); | |
7557 | ||
051a1d1a | 7558 | running = task_current(rq, tsk); |
da0c1e65 | 7559 | queued = task_on_rq_queued(tsk); |
29f59db3 | 7560 | |
da0c1e65 | 7561 | if (queued) |
29f59db3 | 7562 | dequeue_task(rq, tsk, 0); |
0e1f3483 | 7563 | if (unlikely(running)) |
f3cd1c4e | 7564 | put_prev_task(rq, tsk); |
29f59db3 | 7565 | |
f7b8a47d KT |
7566 | /* |
7567 | * All callers are synchronized by task_rq_lock(); we do not use RCU | |
7568 | * which is pointless here. Thus, we pass "true" to task_css_check() | |
7569 | * to prevent lockdep warnings. | |
7570 | */ | |
7571 | tg = container_of(task_css_check(tsk, cpu_cgrp_id, true), | |
8323f26c PZ |
7572 | struct task_group, css); |
7573 | tg = autogroup_task_group(tsk, tg); | |
7574 | tsk->sched_task_group = tg; | |
7575 | ||
810b3817 | 7576 | #ifdef CONFIG_FAIR_GROUP_SCHED |
b2b5ce02 | 7577 | if (tsk->sched_class->task_move_group) |
da0c1e65 | 7578 | tsk->sched_class->task_move_group(tsk, queued); |
b2b5ce02 | 7579 | else |
810b3817 | 7580 | #endif |
b2b5ce02 | 7581 | set_task_rq(tsk, task_cpu(tsk)); |
810b3817 | 7582 | |
0e1f3483 HS |
7583 | if (unlikely(running)) |
7584 | tsk->sched_class->set_curr_task(rq); | |
da0c1e65 | 7585 | if (queued) |
371fd7e7 | 7586 | enqueue_task(rq, tsk, 0); |
29f59db3 | 7587 | |
0122ec5b | 7588 | task_rq_unlock(rq, tsk, &flags); |
29f59db3 | 7589 | } |
7c941438 | 7590 | #endif /* CONFIG_CGROUP_SCHED */ |
29f59db3 | 7591 | |
a790de99 PT |
7592 | #ifdef CONFIG_RT_GROUP_SCHED |
7593 | /* | |
7594 | * Ensure that the real time constraints are schedulable. | |
7595 | */ | |
7596 | static DEFINE_MUTEX(rt_constraints_mutex); | |
9f0c1e56 | 7597 | |
9a7e0b18 PZ |
7598 | /* Must be called with tasklist_lock held */ |
7599 | static inline int tg_has_rt_tasks(struct task_group *tg) | |
b40b2e8e | 7600 | { |
9a7e0b18 | 7601 | struct task_struct *g, *p; |
b40b2e8e | 7602 | |
1fe89e1b PZ |
7603 | /* |
7604 | * Autogroups do not have RT tasks; see autogroup_create(). | |
7605 | */ | |
7606 | if (task_group_is_autogroup(tg)) | |
7607 | return 0; | |
7608 | ||
5d07f420 | 7609 | for_each_process_thread(g, p) { |
8651c658 | 7610 | if (rt_task(p) && task_group(p) == tg) |
9a7e0b18 | 7611 | return 1; |
5d07f420 | 7612 | } |
b40b2e8e | 7613 | |
9a7e0b18 PZ |
7614 | return 0; |
7615 | } | |
b40b2e8e | 7616 | |
9a7e0b18 PZ |
7617 | struct rt_schedulable_data { |
7618 | struct task_group *tg; | |
7619 | u64 rt_period; | |
7620 | u64 rt_runtime; | |
7621 | }; | |
b40b2e8e | 7622 | |
a790de99 | 7623 | static int tg_rt_schedulable(struct task_group *tg, void *data) |
9a7e0b18 PZ |
7624 | { |
7625 | struct rt_schedulable_data *d = data; | |
7626 | struct task_group *child; | |
7627 | unsigned long total, sum = 0; | |
7628 | u64 period, runtime; | |
b40b2e8e | 7629 | |
9a7e0b18 PZ |
7630 | period = ktime_to_ns(tg->rt_bandwidth.rt_period); |
7631 | runtime = tg->rt_bandwidth.rt_runtime; | |
b40b2e8e | 7632 | |
9a7e0b18 PZ |
7633 | if (tg == d->tg) { |
7634 | period = d->rt_period; | |
7635 | runtime = d->rt_runtime; | |
b40b2e8e | 7636 | } |
b40b2e8e | 7637 | |
4653f803 PZ |
7638 | /* |
7639 | * Cannot have more runtime than the period. | |
7640 | */ | |
7641 | if (runtime > period && runtime != RUNTIME_INF) | |
7642 | return -EINVAL; | |
6f505b16 | 7643 | |
4653f803 PZ |
7644 | /* |
7645 | * Ensure we don't starve existing RT tasks. | |
7646 | */ | |
9a7e0b18 PZ |
7647 | if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg)) |
7648 | return -EBUSY; | |
6f505b16 | 7649 | |
9a7e0b18 | 7650 | total = to_ratio(period, runtime); |
6f505b16 | 7651 | |
4653f803 PZ |
7652 | /* |
7653 | * Nobody can have more than the global setting allows. | |
7654 | */ | |
7655 | if (total > to_ratio(global_rt_period(), global_rt_runtime())) | |
7656 | return -EINVAL; | |
6f505b16 | 7657 | |
4653f803 PZ |
7658 | /* |
7659 | * The sum of our children's runtime should not exceed our own. | |
7660 | */ | |
9a7e0b18 PZ |
7661 | list_for_each_entry_rcu(child, &tg->children, siblings) { |
7662 | period = ktime_to_ns(child->rt_bandwidth.rt_period); | |
7663 | runtime = child->rt_bandwidth.rt_runtime; | |
6f505b16 | 7664 | |
9a7e0b18 PZ |
7665 | if (child == d->tg) { |
7666 | period = d->rt_period; | |
7667 | runtime = d->rt_runtime; | |
7668 | } | |
6f505b16 | 7669 | |
9a7e0b18 | 7670 | sum += to_ratio(period, runtime); |
9f0c1e56 | 7671 | } |
6f505b16 | 7672 | |
9a7e0b18 PZ |
7673 | if (sum > total) |
7674 | return -EINVAL; | |
7675 | ||
7676 | return 0; | |
6f505b16 PZ |
7677 | } |
7678 | ||
9a7e0b18 | 7679 | static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime) |
521f1a24 | 7680 | { |
8277434e PT |
7681 | int ret; |
7682 | ||
9a7e0b18 PZ |
7683 | struct rt_schedulable_data data = { |
7684 | .tg = tg, | |
7685 | .rt_period = period, | |
7686 | .rt_runtime = runtime, | |
7687 | }; | |
7688 | ||
8277434e PT |
7689 | rcu_read_lock(); |
7690 | ret = walk_tg_tree(tg_rt_schedulable, tg_nop, &data); | |
7691 | rcu_read_unlock(); | |
7692 | ||
7693 | return ret; | |
521f1a24 DG |
7694 | } |
7695 | ||
ab84d31e | 7696 | static int tg_set_rt_bandwidth(struct task_group *tg, |
d0b27fa7 | 7697 | u64 rt_period, u64 rt_runtime) |
6f505b16 | 7698 | { |
ac086bc2 | 7699 | int i, err = 0; |
9f0c1e56 | 7700 | |
2636ed5f PZ |
7701 | /* |
7702 | * Disallowing the root group RT runtime is BAD, it would disallow the | |
7703 | * kernel creating (and or operating) RT threads. | |
7704 | */ | |
7705 | if (tg == &root_task_group && rt_runtime == 0) | |
7706 | return -EINVAL; | |
7707 | ||
7708 | /* No period doesn't make any sense. */ | |
7709 | if (rt_period == 0) | |
7710 | return -EINVAL; | |
7711 | ||
9f0c1e56 | 7712 | mutex_lock(&rt_constraints_mutex); |
521f1a24 | 7713 | read_lock(&tasklist_lock); |
9a7e0b18 PZ |
7714 | err = __rt_schedulable(tg, rt_period, rt_runtime); |
7715 | if (err) | |
9f0c1e56 | 7716 | goto unlock; |
ac086bc2 | 7717 | |
0986b11b | 7718 | raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock); |
d0b27fa7 PZ |
7719 | tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period); |
7720 | tg->rt_bandwidth.rt_runtime = rt_runtime; | |
ac086bc2 PZ |
7721 | |
7722 | for_each_possible_cpu(i) { | |
7723 | struct rt_rq *rt_rq = tg->rt_rq[i]; | |
7724 | ||
0986b11b | 7725 | raw_spin_lock(&rt_rq->rt_runtime_lock); |
ac086bc2 | 7726 | rt_rq->rt_runtime = rt_runtime; |
0986b11b | 7727 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
ac086bc2 | 7728 | } |
0986b11b | 7729 | raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock); |
49246274 | 7730 | unlock: |
521f1a24 | 7731 | read_unlock(&tasklist_lock); |
9f0c1e56 PZ |
7732 | mutex_unlock(&rt_constraints_mutex); |
7733 | ||
7734 | return err; | |
6f505b16 PZ |
7735 | } |
7736 | ||
25cc7da7 | 7737 | static int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us) |
d0b27fa7 PZ |
7738 | { |
7739 | u64 rt_runtime, rt_period; | |
7740 | ||
7741 | rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period); | |
7742 | rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC; | |
7743 | if (rt_runtime_us < 0) | |
7744 | rt_runtime = RUNTIME_INF; | |
7745 | ||
ab84d31e | 7746 | return tg_set_rt_bandwidth(tg, rt_period, rt_runtime); |
d0b27fa7 PZ |
7747 | } |
7748 | ||
25cc7da7 | 7749 | static long sched_group_rt_runtime(struct task_group *tg) |
9f0c1e56 PZ |
7750 | { |
7751 | u64 rt_runtime_us; | |
7752 | ||
d0b27fa7 | 7753 | if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF) |
9f0c1e56 PZ |
7754 | return -1; |
7755 | ||
d0b27fa7 | 7756 | rt_runtime_us = tg->rt_bandwidth.rt_runtime; |
9f0c1e56 PZ |
7757 | do_div(rt_runtime_us, NSEC_PER_USEC); |
7758 | return rt_runtime_us; | |
7759 | } | |
d0b27fa7 | 7760 | |
ce2f5fe4 | 7761 | static int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us) |
d0b27fa7 PZ |
7762 | { |
7763 | u64 rt_runtime, rt_period; | |
7764 | ||
ce2f5fe4 | 7765 | rt_period = rt_period_us * NSEC_PER_USEC; |
d0b27fa7 PZ |
7766 | rt_runtime = tg->rt_bandwidth.rt_runtime; |
7767 | ||
ab84d31e | 7768 | return tg_set_rt_bandwidth(tg, rt_period, rt_runtime); |
d0b27fa7 PZ |
7769 | } |
7770 | ||
25cc7da7 | 7771 | static long sched_group_rt_period(struct task_group *tg) |
d0b27fa7 PZ |
7772 | { |
7773 | u64 rt_period_us; | |
7774 | ||
7775 | rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period); | |
7776 | do_div(rt_period_us, NSEC_PER_USEC); | |
7777 | return rt_period_us; | |
7778 | } | |
332ac17e | 7779 | #endif /* CONFIG_RT_GROUP_SCHED */ |
d0b27fa7 | 7780 | |
332ac17e | 7781 | #ifdef CONFIG_RT_GROUP_SCHED |
d0b27fa7 PZ |
7782 | static int sched_rt_global_constraints(void) |
7783 | { | |
7784 | int ret = 0; | |
7785 | ||
7786 | mutex_lock(&rt_constraints_mutex); | |
9a7e0b18 | 7787 | read_lock(&tasklist_lock); |
4653f803 | 7788 | ret = __rt_schedulable(NULL, 0, 0); |
9a7e0b18 | 7789 | read_unlock(&tasklist_lock); |
d0b27fa7 PZ |
7790 | mutex_unlock(&rt_constraints_mutex); |
7791 | ||
7792 | return ret; | |
7793 | } | |
54e99124 | 7794 | |
25cc7da7 | 7795 | static int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk) |
54e99124 DG |
7796 | { |
7797 | /* Don't accept realtime tasks when there is no way for them to run */ | |
7798 | if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0) | |
7799 | return 0; | |
7800 | ||
7801 | return 1; | |
7802 | } | |
7803 | ||
6d6bc0ad | 7804 | #else /* !CONFIG_RT_GROUP_SCHED */ |
d0b27fa7 PZ |
7805 | static int sched_rt_global_constraints(void) |
7806 | { | |
ac086bc2 | 7807 | unsigned long flags; |
332ac17e | 7808 | int i, ret = 0; |
ec5d4989 | 7809 | |
0986b11b | 7810 | raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags); |
ac086bc2 PZ |
7811 | for_each_possible_cpu(i) { |
7812 | struct rt_rq *rt_rq = &cpu_rq(i)->rt; | |
7813 | ||
0986b11b | 7814 | raw_spin_lock(&rt_rq->rt_runtime_lock); |
ac086bc2 | 7815 | rt_rq->rt_runtime = global_rt_runtime(); |
0986b11b | 7816 | raw_spin_unlock(&rt_rq->rt_runtime_lock); |
ac086bc2 | 7817 | } |
0986b11b | 7818 | raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags); |
ac086bc2 | 7819 | |
332ac17e | 7820 | return ret; |
d0b27fa7 | 7821 | } |
6d6bc0ad | 7822 | #endif /* CONFIG_RT_GROUP_SCHED */ |
d0b27fa7 | 7823 | |
a1963b81 | 7824 | static int sched_dl_global_validate(void) |
332ac17e | 7825 | { |
1724813d PZ |
7826 | u64 runtime = global_rt_runtime(); |
7827 | u64 period = global_rt_period(); | |
332ac17e | 7828 | u64 new_bw = to_ratio(period, runtime); |
f10e00f4 | 7829 | struct dl_bw *dl_b; |
1724813d | 7830 | int cpu, ret = 0; |
49516342 | 7831 | unsigned long flags; |
332ac17e DF |
7832 | |
7833 | /* | |
7834 | * Here we want to check the bandwidth not being set to some | |
7835 | * value smaller than the currently allocated bandwidth in | |
7836 | * any of the root_domains. | |
7837 | * | |
7838 | * FIXME: Cycling on all the CPUs is overdoing, but simpler than | |
7839 | * cycling on root_domains... Discussion on different/better | |
7840 | * solutions is welcome! | |
7841 | */ | |
1724813d | 7842 | for_each_possible_cpu(cpu) { |
f10e00f4 KT |
7843 | rcu_read_lock_sched(); |
7844 | dl_b = dl_bw_of(cpu); | |
332ac17e | 7845 | |
49516342 | 7846 | raw_spin_lock_irqsave(&dl_b->lock, flags); |
1724813d PZ |
7847 | if (new_bw < dl_b->total_bw) |
7848 | ret = -EBUSY; | |
49516342 | 7849 | raw_spin_unlock_irqrestore(&dl_b->lock, flags); |
1724813d | 7850 | |
f10e00f4 KT |
7851 | rcu_read_unlock_sched(); |
7852 | ||
1724813d PZ |
7853 | if (ret) |
7854 | break; | |
332ac17e DF |
7855 | } |
7856 | ||
1724813d | 7857 | return ret; |
332ac17e DF |
7858 | } |
7859 | ||
1724813d | 7860 | static void sched_dl_do_global(void) |
ce0dbbbb | 7861 | { |
1724813d | 7862 | u64 new_bw = -1; |
f10e00f4 | 7863 | struct dl_bw *dl_b; |
1724813d | 7864 | int cpu; |
49516342 | 7865 | unsigned long flags; |
ce0dbbbb | 7866 | |
1724813d PZ |
7867 | def_dl_bandwidth.dl_period = global_rt_period(); |
7868 | def_dl_bandwidth.dl_runtime = global_rt_runtime(); | |
7869 | ||
7870 | if (global_rt_runtime() != RUNTIME_INF) | |
7871 | new_bw = to_ratio(global_rt_period(), global_rt_runtime()); | |
7872 | ||
7873 | /* | |
7874 | * FIXME: As above... | |
7875 | */ | |
7876 | for_each_possible_cpu(cpu) { | |
f10e00f4 KT |
7877 | rcu_read_lock_sched(); |
7878 | dl_b = dl_bw_of(cpu); | |
1724813d | 7879 | |
49516342 | 7880 | raw_spin_lock_irqsave(&dl_b->lock, flags); |
1724813d | 7881 | dl_b->bw = new_bw; |
49516342 | 7882 | raw_spin_unlock_irqrestore(&dl_b->lock, flags); |
f10e00f4 KT |
7883 | |
7884 | rcu_read_unlock_sched(); | |
ce0dbbbb | 7885 | } |
1724813d PZ |
7886 | } |
7887 | ||
7888 | static int sched_rt_global_validate(void) | |
7889 | { | |
7890 | if (sysctl_sched_rt_period <= 0) | |
7891 | return -EINVAL; | |
7892 | ||
e9e7cb38 JL |
7893 | if ((sysctl_sched_rt_runtime != RUNTIME_INF) && |
7894 | (sysctl_sched_rt_runtime > sysctl_sched_rt_period)) | |
1724813d PZ |
7895 | return -EINVAL; |
7896 | ||
7897 | return 0; | |
7898 | } | |
7899 | ||
7900 | static void sched_rt_do_global(void) | |
7901 | { | |
7902 | def_rt_bandwidth.rt_runtime = global_rt_runtime(); | |
7903 | def_rt_bandwidth.rt_period = ns_to_ktime(global_rt_period()); | |
ce0dbbbb CW |
7904 | } |
7905 | ||
d0b27fa7 | 7906 | int sched_rt_handler(struct ctl_table *table, int write, |
8d65af78 | 7907 | void __user *buffer, size_t *lenp, |
d0b27fa7 PZ |
7908 | loff_t *ppos) |
7909 | { | |
d0b27fa7 PZ |
7910 | int old_period, old_runtime; |
7911 | static DEFINE_MUTEX(mutex); | |
1724813d | 7912 | int ret; |
d0b27fa7 PZ |
7913 | |
7914 | mutex_lock(&mutex); | |
7915 | old_period = sysctl_sched_rt_period; | |
7916 | old_runtime = sysctl_sched_rt_runtime; | |
7917 | ||
8d65af78 | 7918 | ret = proc_dointvec(table, write, buffer, lenp, ppos); |
d0b27fa7 PZ |
7919 | |
7920 | if (!ret && write) { | |
1724813d PZ |
7921 | ret = sched_rt_global_validate(); |
7922 | if (ret) | |
7923 | goto undo; | |
7924 | ||
a1963b81 | 7925 | ret = sched_dl_global_validate(); |
1724813d PZ |
7926 | if (ret) |
7927 | goto undo; | |
7928 | ||
a1963b81 | 7929 | ret = sched_rt_global_constraints(); |
1724813d PZ |
7930 | if (ret) |
7931 | goto undo; | |
7932 | ||
7933 | sched_rt_do_global(); | |
7934 | sched_dl_do_global(); | |
7935 | } | |
7936 | if (0) { | |
7937 | undo: | |
7938 | sysctl_sched_rt_period = old_period; | |
7939 | sysctl_sched_rt_runtime = old_runtime; | |
d0b27fa7 PZ |
7940 | } |
7941 | mutex_unlock(&mutex); | |
7942 | ||
7943 | return ret; | |
7944 | } | |
68318b8e | 7945 | |
1724813d | 7946 | int sched_rr_handler(struct ctl_table *table, int write, |
332ac17e DF |
7947 | void __user *buffer, size_t *lenp, |
7948 | loff_t *ppos) | |
7949 | { | |
7950 | int ret; | |
332ac17e | 7951 | static DEFINE_MUTEX(mutex); |
332ac17e DF |
7952 | |
7953 | mutex_lock(&mutex); | |
332ac17e | 7954 | ret = proc_dointvec(table, write, buffer, lenp, ppos); |
1724813d PZ |
7955 | /* make sure that internally we keep jiffies */ |
7956 | /* also, writing zero resets timeslice to default */ | |
332ac17e | 7957 | if (!ret && write) { |
1724813d PZ |
7958 | sched_rr_timeslice = sched_rr_timeslice <= 0 ? |
7959 | RR_TIMESLICE : msecs_to_jiffies(sched_rr_timeslice); | |
332ac17e DF |
7960 | } |
7961 | mutex_unlock(&mutex); | |
332ac17e DF |
7962 | return ret; |
7963 | } | |
7964 | ||
052f1dc7 | 7965 | #ifdef CONFIG_CGROUP_SCHED |
68318b8e | 7966 | |
a7c6d554 | 7967 | static inline struct task_group *css_tg(struct cgroup_subsys_state *css) |
68318b8e | 7968 | { |
a7c6d554 | 7969 | return css ? container_of(css, struct task_group, css) : NULL; |
68318b8e SV |
7970 | } |
7971 | ||
eb95419b TH |
7972 | static struct cgroup_subsys_state * |
7973 | cpu_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) | |
68318b8e | 7974 | { |
eb95419b TH |
7975 | struct task_group *parent = css_tg(parent_css); |
7976 | struct task_group *tg; | |
68318b8e | 7977 | |
eb95419b | 7978 | if (!parent) { |
68318b8e | 7979 | /* This is early initialization for the top cgroup */ |
07e06b01 | 7980 | return &root_task_group.css; |
68318b8e SV |
7981 | } |
7982 | ||
ec7dc8ac | 7983 | tg = sched_create_group(parent); |
68318b8e SV |
7984 | if (IS_ERR(tg)) |
7985 | return ERR_PTR(-ENOMEM); | |
7986 | ||
68318b8e SV |
7987 | return &tg->css; |
7988 | } | |
7989 | ||
eb95419b | 7990 | static int cpu_cgroup_css_online(struct cgroup_subsys_state *css) |
ace783b9 | 7991 | { |
eb95419b | 7992 | struct task_group *tg = css_tg(css); |
5c9d535b | 7993 | struct task_group *parent = css_tg(css->parent); |
ace783b9 | 7994 | |
63876986 TH |
7995 | if (parent) |
7996 | sched_online_group(tg, parent); | |
ace783b9 LZ |
7997 | return 0; |
7998 | } | |
7999 | ||
eb95419b | 8000 | static void cpu_cgroup_css_free(struct cgroup_subsys_state *css) |
68318b8e | 8001 | { |
eb95419b | 8002 | struct task_group *tg = css_tg(css); |
68318b8e SV |
8003 | |
8004 | sched_destroy_group(tg); | |
8005 | } | |
8006 | ||
eb95419b | 8007 | static void cpu_cgroup_css_offline(struct cgroup_subsys_state *css) |
ace783b9 | 8008 | { |
eb95419b | 8009 | struct task_group *tg = css_tg(css); |
ace783b9 LZ |
8010 | |
8011 | sched_offline_group(tg); | |
8012 | } | |
8013 | ||
eeb61e53 KT |
8014 | static void cpu_cgroup_fork(struct task_struct *task) |
8015 | { | |
8016 | sched_move_task(task); | |
8017 | } | |
8018 | ||
eb95419b | 8019 | static int cpu_cgroup_can_attach(struct cgroup_subsys_state *css, |
bb9d97b6 | 8020 | struct cgroup_taskset *tset) |
68318b8e | 8021 | { |
bb9d97b6 TH |
8022 | struct task_struct *task; |
8023 | ||
924f0d9a | 8024 | cgroup_taskset_for_each(task, tset) { |
b68aa230 | 8025 | #ifdef CONFIG_RT_GROUP_SCHED |
eb95419b | 8026 | if (!sched_rt_can_attach(css_tg(css), task)) |
bb9d97b6 | 8027 | return -EINVAL; |
b68aa230 | 8028 | #else |
bb9d97b6 TH |
8029 | /* We don't support RT-tasks being in separate groups */ |
8030 | if (task->sched_class != &fair_sched_class) | |
8031 | return -EINVAL; | |
b68aa230 | 8032 | #endif |
bb9d97b6 | 8033 | } |
be367d09 BB |
8034 | return 0; |
8035 | } | |
68318b8e | 8036 | |
eb95419b | 8037 | static void cpu_cgroup_attach(struct cgroup_subsys_state *css, |
bb9d97b6 | 8038 | struct cgroup_taskset *tset) |
68318b8e | 8039 | { |
bb9d97b6 TH |
8040 | struct task_struct *task; |
8041 | ||
924f0d9a | 8042 | cgroup_taskset_for_each(task, tset) |
bb9d97b6 | 8043 | sched_move_task(task); |
68318b8e SV |
8044 | } |
8045 | ||
eb95419b TH |
8046 | static void cpu_cgroup_exit(struct cgroup_subsys_state *css, |
8047 | struct cgroup_subsys_state *old_css, | |
8048 | struct task_struct *task) | |
068c5cc5 PZ |
8049 | { |
8050 | /* | |
8051 | * cgroup_exit() is called in the copy_process() failure path. | |
8052 | * Ignore this case since the task hasn't ran yet, this avoids | |
8053 | * trying to poke a half freed task state from generic code. | |
8054 | */ | |
8055 | if (!(task->flags & PF_EXITING)) | |
8056 | return; | |
8057 | ||
8058 | sched_move_task(task); | |
8059 | } | |
8060 | ||
052f1dc7 | 8061 | #ifdef CONFIG_FAIR_GROUP_SCHED |
182446d0 TH |
8062 | static int cpu_shares_write_u64(struct cgroup_subsys_state *css, |
8063 | struct cftype *cftype, u64 shareval) | |
68318b8e | 8064 | { |
182446d0 | 8065 | return sched_group_set_shares(css_tg(css), scale_load(shareval)); |
68318b8e SV |
8066 | } |
8067 | ||
182446d0 TH |
8068 | static u64 cpu_shares_read_u64(struct cgroup_subsys_state *css, |
8069 | struct cftype *cft) | |
68318b8e | 8070 | { |
182446d0 | 8071 | struct task_group *tg = css_tg(css); |
68318b8e | 8072 | |
c8b28116 | 8073 | return (u64) scale_load_down(tg->shares); |
68318b8e | 8074 | } |
ab84d31e PT |
8075 | |
8076 | #ifdef CONFIG_CFS_BANDWIDTH | |
a790de99 PT |
8077 | static DEFINE_MUTEX(cfs_constraints_mutex); |
8078 | ||
ab84d31e PT |
8079 | const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */ |
8080 | const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */ | |
8081 | ||
a790de99 PT |
8082 | static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime); |
8083 | ||
ab84d31e PT |
8084 | static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota) |
8085 | { | |
56f570e5 | 8086 | int i, ret = 0, runtime_enabled, runtime_was_enabled; |
029632fb | 8087 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; |
ab84d31e PT |
8088 | |
8089 | if (tg == &root_task_group) | |
8090 | return -EINVAL; | |
8091 | ||
8092 | /* | |
8093 | * Ensure we have at some amount of bandwidth every period. This is | |
8094 | * to prevent reaching a state of large arrears when throttled via | |
8095 | * entity_tick() resulting in prolonged exit starvation. | |
8096 | */ | |
8097 | if (quota < min_cfs_quota_period || period < min_cfs_quota_period) | |
8098 | return -EINVAL; | |
8099 | ||
8100 | /* | |
8101 | * Likewise, bound things on the otherside by preventing insane quota | |
8102 | * periods. This also allows us to normalize in computing quota | |
8103 | * feasibility. | |
8104 | */ | |
8105 | if (period > max_cfs_quota_period) | |
8106 | return -EINVAL; | |
8107 | ||
0e59bdae KT |
8108 | /* |
8109 | * Prevent race between setting of cfs_rq->runtime_enabled and | |
8110 | * unthrottle_offline_cfs_rqs(). | |
8111 | */ | |
8112 | get_online_cpus(); | |
a790de99 PT |
8113 | mutex_lock(&cfs_constraints_mutex); |
8114 | ret = __cfs_schedulable(tg, period, quota); | |
8115 | if (ret) | |
8116 | goto out_unlock; | |
8117 | ||
58088ad0 | 8118 | runtime_enabled = quota != RUNTIME_INF; |
56f570e5 | 8119 | runtime_was_enabled = cfs_b->quota != RUNTIME_INF; |
1ee14e6c BS |
8120 | /* |
8121 | * If we need to toggle cfs_bandwidth_used, off->on must occur | |
8122 | * before making related changes, and on->off must occur afterwards | |
8123 | */ | |
8124 | if (runtime_enabled && !runtime_was_enabled) | |
8125 | cfs_bandwidth_usage_inc(); | |
ab84d31e PT |
8126 | raw_spin_lock_irq(&cfs_b->lock); |
8127 | cfs_b->period = ns_to_ktime(period); | |
8128 | cfs_b->quota = quota; | |
58088ad0 | 8129 | |
a9cf55b2 | 8130 | __refill_cfs_bandwidth_runtime(cfs_b); |
58088ad0 | 8131 | /* restart the period timer (if active) to handle new period expiry */ |
77a4d1a1 PZ |
8132 | if (runtime_enabled) |
8133 | start_cfs_bandwidth(cfs_b); | |
ab84d31e PT |
8134 | raw_spin_unlock_irq(&cfs_b->lock); |
8135 | ||
0e59bdae | 8136 | for_each_online_cpu(i) { |
ab84d31e | 8137 | struct cfs_rq *cfs_rq = tg->cfs_rq[i]; |
029632fb | 8138 | struct rq *rq = cfs_rq->rq; |
ab84d31e PT |
8139 | |
8140 | raw_spin_lock_irq(&rq->lock); | |
58088ad0 | 8141 | cfs_rq->runtime_enabled = runtime_enabled; |
ab84d31e | 8142 | cfs_rq->runtime_remaining = 0; |
671fd9da | 8143 | |
029632fb | 8144 | if (cfs_rq->throttled) |
671fd9da | 8145 | unthrottle_cfs_rq(cfs_rq); |
ab84d31e PT |
8146 | raw_spin_unlock_irq(&rq->lock); |
8147 | } | |
1ee14e6c BS |
8148 | if (runtime_was_enabled && !runtime_enabled) |
8149 | cfs_bandwidth_usage_dec(); | |
a790de99 PT |
8150 | out_unlock: |
8151 | mutex_unlock(&cfs_constraints_mutex); | |
0e59bdae | 8152 | put_online_cpus(); |
ab84d31e | 8153 | |
a790de99 | 8154 | return ret; |
ab84d31e PT |
8155 | } |
8156 | ||
8157 | int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us) | |
8158 | { | |
8159 | u64 quota, period; | |
8160 | ||
029632fb | 8161 | period = ktime_to_ns(tg->cfs_bandwidth.period); |
ab84d31e PT |
8162 | if (cfs_quota_us < 0) |
8163 | quota = RUNTIME_INF; | |
8164 | else | |
8165 | quota = (u64)cfs_quota_us * NSEC_PER_USEC; | |
8166 | ||
8167 | return tg_set_cfs_bandwidth(tg, period, quota); | |
8168 | } | |
8169 | ||
8170 | long tg_get_cfs_quota(struct task_group *tg) | |
8171 | { | |
8172 | u64 quota_us; | |
8173 | ||
029632fb | 8174 | if (tg->cfs_bandwidth.quota == RUNTIME_INF) |
ab84d31e PT |
8175 | return -1; |
8176 | ||
029632fb | 8177 | quota_us = tg->cfs_bandwidth.quota; |
ab84d31e PT |
8178 | do_div(quota_us, NSEC_PER_USEC); |
8179 | ||
8180 | return quota_us; | |
8181 | } | |
8182 | ||
8183 | int tg_set_cfs_period(struct task_group *tg, long cfs_period_us) | |
8184 | { | |
8185 | u64 quota, period; | |
8186 | ||
8187 | period = (u64)cfs_period_us * NSEC_PER_USEC; | |
029632fb | 8188 | quota = tg->cfs_bandwidth.quota; |
ab84d31e | 8189 | |
ab84d31e PT |
8190 | return tg_set_cfs_bandwidth(tg, period, quota); |
8191 | } | |
8192 | ||
8193 | long tg_get_cfs_period(struct task_group *tg) | |
8194 | { | |
8195 | u64 cfs_period_us; | |
8196 | ||
029632fb | 8197 | cfs_period_us = ktime_to_ns(tg->cfs_bandwidth.period); |
ab84d31e PT |
8198 | do_div(cfs_period_us, NSEC_PER_USEC); |
8199 | ||
8200 | return cfs_period_us; | |
8201 | } | |
8202 | ||
182446d0 TH |
8203 | static s64 cpu_cfs_quota_read_s64(struct cgroup_subsys_state *css, |
8204 | struct cftype *cft) | |
ab84d31e | 8205 | { |
182446d0 | 8206 | return tg_get_cfs_quota(css_tg(css)); |
ab84d31e PT |
8207 | } |
8208 | ||
182446d0 TH |
8209 | static int cpu_cfs_quota_write_s64(struct cgroup_subsys_state *css, |
8210 | struct cftype *cftype, s64 cfs_quota_us) | |
ab84d31e | 8211 | { |
182446d0 | 8212 | return tg_set_cfs_quota(css_tg(css), cfs_quota_us); |
ab84d31e PT |
8213 | } |
8214 | ||
182446d0 TH |
8215 | static u64 cpu_cfs_period_read_u64(struct cgroup_subsys_state *css, |
8216 | struct cftype *cft) | |
ab84d31e | 8217 | { |
182446d0 | 8218 | return tg_get_cfs_period(css_tg(css)); |
ab84d31e PT |
8219 | } |
8220 | ||
182446d0 TH |
8221 | static int cpu_cfs_period_write_u64(struct cgroup_subsys_state *css, |
8222 | struct cftype *cftype, u64 cfs_period_us) | |
ab84d31e | 8223 | { |
182446d0 | 8224 | return tg_set_cfs_period(css_tg(css), cfs_period_us); |
ab84d31e PT |
8225 | } |
8226 | ||
a790de99 PT |
8227 | struct cfs_schedulable_data { |
8228 | struct task_group *tg; | |
8229 | u64 period, quota; | |
8230 | }; | |
8231 | ||
8232 | /* | |
8233 | * normalize group quota/period to be quota/max_period | |
8234 | * note: units are usecs | |
8235 | */ | |
8236 | static u64 normalize_cfs_quota(struct task_group *tg, | |
8237 | struct cfs_schedulable_data *d) | |
8238 | { | |
8239 | u64 quota, period; | |
8240 | ||
8241 | if (tg == d->tg) { | |
8242 | period = d->period; | |
8243 | quota = d->quota; | |
8244 | } else { | |
8245 | period = tg_get_cfs_period(tg); | |
8246 | quota = tg_get_cfs_quota(tg); | |
8247 | } | |
8248 | ||
8249 | /* note: these should typically be equivalent */ | |
8250 | if (quota == RUNTIME_INF || quota == -1) | |
8251 | return RUNTIME_INF; | |
8252 | ||
8253 | return to_ratio(period, quota); | |
8254 | } | |
8255 | ||
8256 | static int tg_cfs_schedulable_down(struct task_group *tg, void *data) | |
8257 | { | |
8258 | struct cfs_schedulable_data *d = data; | |
029632fb | 8259 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; |
a790de99 PT |
8260 | s64 quota = 0, parent_quota = -1; |
8261 | ||
8262 | if (!tg->parent) { | |
8263 | quota = RUNTIME_INF; | |
8264 | } else { | |
029632fb | 8265 | struct cfs_bandwidth *parent_b = &tg->parent->cfs_bandwidth; |
a790de99 PT |
8266 | |
8267 | quota = normalize_cfs_quota(tg, d); | |
9c58c79a | 8268 | parent_quota = parent_b->hierarchical_quota; |
a790de99 PT |
8269 | |
8270 | /* | |
8271 | * ensure max(child_quota) <= parent_quota, inherit when no | |
8272 | * limit is set | |
8273 | */ | |
8274 | if (quota == RUNTIME_INF) | |
8275 | quota = parent_quota; | |
8276 | else if (parent_quota != RUNTIME_INF && quota > parent_quota) | |
8277 | return -EINVAL; | |
8278 | } | |
9c58c79a | 8279 | cfs_b->hierarchical_quota = quota; |
a790de99 PT |
8280 | |
8281 | return 0; | |
8282 | } | |
8283 | ||
8284 | static int __cfs_schedulable(struct task_group *tg, u64 period, u64 quota) | |
8285 | { | |
8277434e | 8286 | int ret; |
a790de99 PT |
8287 | struct cfs_schedulable_data data = { |
8288 | .tg = tg, | |
8289 | .period = period, | |
8290 | .quota = quota, | |
8291 | }; | |
8292 | ||
8293 | if (quota != RUNTIME_INF) { | |
8294 | do_div(data.period, NSEC_PER_USEC); | |
8295 | do_div(data.quota, NSEC_PER_USEC); | |
8296 | } | |
8297 | ||
8277434e PT |
8298 | rcu_read_lock(); |
8299 | ret = walk_tg_tree(tg_cfs_schedulable_down, tg_nop, &data); | |
8300 | rcu_read_unlock(); | |
8301 | ||
8302 | return ret; | |
a790de99 | 8303 | } |
e8da1b18 | 8304 | |
2da8ca82 | 8305 | static int cpu_stats_show(struct seq_file *sf, void *v) |
e8da1b18 | 8306 | { |
2da8ca82 | 8307 | struct task_group *tg = css_tg(seq_css(sf)); |
029632fb | 8308 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; |
e8da1b18 | 8309 | |
44ffc75b TH |
8310 | seq_printf(sf, "nr_periods %d\n", cfs_b->nr_periods); |
8311 | seq_printf(sf, "nr_throttled %d\n", cfs_b->nr_throttled); | |
8312 | seq_printf(sf, "throttled_time %llu\n", cfs_b->throttled_time); | |
e8da1b18 NR |
8313 | |
8314 | return 0; | |
8315 | } | |
ab84d31e | 8316 | #endif /* CONFIG_CFS_BANDWIDTH */ |
6d6bc0ad | 8317 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
68318b8e | 8318 | |
052f1dc7 | 8319 | #ifdef CONFIG_RT_GROUP_SCHED |
182446d0 TH |
8320 | static int cpu_rt_runtime_write(struct cgroup_subsys_state *css, |
8321 | struct cftype *cft, s64 val) | |
6f505b16 | 8322 | { |
182446d0 | 8323 | return sched_group_set_rt_runtime(css_tg(css), val); |
6f505b16 PZ |
8324 | } |
8325 | ||
182446d0 TH |
8326 | static s64 cpu_rt_runtime_read(struct cgroup_subsys_state *css, |
8327 | struct cftype *cft) | |
6f505b16 | 8328 | { |
182446d0 | 8329 | return sched_group_rt_runtime(css_tg(css)); |
6f505b16 | 8330 | } |
d0b27fa7 | 8331 | |
182446d0 TH |
8332 | static int cpu_rt_period_write_uint(struct cgroup_subsys_state *css, |
8333 | struct cftype *cftype, u64 rt_period_us) | |
d0b27fa7 | 8334 | { |
182446d0 | 8335 | return sched_group_set_rt_period(css_tg(css), rt_period_us); |
d0b27fa7 PZ |
8336 | } |
8337 | ||
182446d0 TH |
8338 | static u64 cpu_rt_period_read_uint(struct cgroup_subsys_state *css, |
8339 | struct cftype *cft) | |
d0b27fa7 | 8340 | { |
182446d0 | 8341 | return sched_group_rt_period(css_tg(css)); |
d0b27fa7 | 8342 | } |
6d6bc0ad | 8343 | #endif /* CONFIG_RT_GROUP_SCHED */ |
6f505b16 | 8344 | |
fe5c7cc2 | 8345 | static struct cftype cpu_files[] = { |
052f1dc7 | 8346 | #ifdef CONFIG_FAIR_GROUP_SCHED |
fe5c7cc2 PM |
8347 | { |
8348 | .name = "shares", | |
f4c753b7 PM |
8349 | .read_u64 = cpu_shares_read_u64, |
8350 | .write_u64 = cpu_shares_write_u64, | |
fe5c7cc2 | 8351 | }, |
052f1dc7 | 8352 | #endif |
ab84d31e PT |
8353 | #ifdef CONFIG_CFS_BANDWIDTH |
8354 | { | |
8355 | .name = "cfs_quota_us", | |
8356 | .read_s64 = cpu_cfs_quota_read_s64, | |
8357 | .write_s64 = cpu_cfs_quota_write_s64, | |
8358 | }, | |
8359 | { | |
8360 | .name = "cfs_period_us", | |
8361 | .read_u64 = cpu_cfs_period_read_u64, | |
8362 | .write_u64 = cpu_cfs_period_write_u64, | |
8363 | }, | |
e8da1b18 NR |
8364 | { |
8365 | .name = "stat", | |
2da8ca82 | 8366 | .seq_show = cpu_stats_show, |
e8da1b18 | 8367 | }, |
ab84d31e | 8368 | #endif |
052f1dc7 | 8369 | #ifdef CONFIG_RT_GROUP_SCHED |
6f505b16 | 8370 | { |
9f0c1e56 | 8371 | .name = "rt_runtime_us", |
06ecb27c PM |
8372 | .read_s64 = cpu_rt_runtime_read, |
8373 | .write_s64 = cpu_rt_runtime_write, | |
6f505b16 | 8374 | }, |
d0b27fa7 PZ |
8375 | { |
8376 | .name = "rt_period_us", | |
f4c753b7 PM |
8377 | .read_u64 = cpu_rt_period_read_uint, |
8378 | .write_u64 = cpu_rt_period_write_uint, | |
d0b27fa7 | 8379 | }, |
052f1dc7 | 8380 | #endif |
4baf6e33 | 8381 | { } /* terminate */ |
68318b8e SV |
8382 | }; |
8383 | ||
073219e9 | 8384 | struct cgroup_subsys cpu_cgrp_subsys = { |
92fb9748 TH |
8385 | .css_alloc = cpu_cgroup_css_alloc, |
8386 | .css_free = cpu_cgroup_css_free, | |
ace783b9 LZ |
8387 | .css_online = cpu_cgroup_css_online, |
8388 | .css_offline = cpu_cgroup_css_offline, | |
eeb61e53 | 8389 | .fork = cpu_cgroup_fork, |
bb9d97b6 TH |
8390 | .can_attach = cpu_cgroup_can_attach, |
8391 | .attach = cpu_cgroup_attach, | |
068c5cc5 | 8392 | .exit = cpu_cgroup_exit, |
5577964e | 8393 | .legacy_cftypes = cpu_files, |
68318b8e SV |
8394 | .early_init = 1, |
8395 | }; | |
8396 | ||
052f1dc7 | 8397 | #endif /* CONFIG_CGROUP_SCHED */ |
d842de87 | 8398 | |
b637a328 PM |
8399 | void dump_cpu_task(int cpu) |
8400 | { | |
8401 | pr_info("Task dump for CPU %d:\n", cpu); | |
8402 | sched_show_task(cpu_curr(cpu)); | |
8403 | } |