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