]>
Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 | 2 | /* |
391e43da | 3 | * kernel/sched/core.c |
1da177e4 | 4 | * |
d1ccc66d | 5 | * Core kernel scheduler code and related syscalls |
1da177e4 LT |
6 | * |
7 | * Copyright (C) 1991-2002 Linus Torvalds | |
1da177e4 | 8 | */ |
e66f6481 IM |
9 | #include <linux/highmem.h> |
10 | #include <linux/hrtimer_api.h> | |
11 | #include <linux/ktime_api.h> | |
12 | #include <linux/sched/signal.h> | |
13 | #include <linux/syscalls_api.h> | |
14 | #include <linux/debug_locks.h> | |
15 | #include <linux/prefetch.h> | |
16 | #include <linux/capability.h> | |
17 | #include <linux/pgtable_api.h> | |
18 | #include <linux/wait_bit.h> | |
19 | #include <linux/jiffies.h> | |
20 | #include <linux/spinlock_api.h> | |
21 | #include <linux/cpumask_api.h> | |
22 | #include <linux/lockdep_api.h> | |
23 | #include <linux/hardirq.h> | |
24 | #include <linux/softirq.h> | |
25 | #include <linux/refcount_api.h> | |
26 | #include <linux/topology.h> | |
27 | #include <linux/sched/clock.h> | |
28 | #include <linux/sched/cond_resched.h> | |
d664e399 | 29 | #include <linux/sched/cputime.h> |
e66f6481 | 30 | #include <linux/sched/debug.h> |
d664e399 TG |
31 | #include <linux/sched/hotplug.h> |
32 | #include <linux/sched/init.h> | |
e66f6481 IM |
33 | #include <linux/sched/isolation.h> |
34 | #include <linux/sched/loadavg.h> | |
35 | #include <linux/sched/mm.h> | |
36 | #include <linux/sched/nohz.h> | |
37 | #include <linux/sched/rseq_api.h> | |
38 | #include <linux/sched/rt.h> | |
1da177e4 | 39 | |
6a5850d1 | 40 | #include <linux/blkdev.h> |
e66f6481 IM |
41 | #include <linux/context_tracking.h> |
42 | #include <linux/cpuset.h> | |
43 | #include <linux/delayacct.h> | |
44 | #include <linux/init_task.h> | |
45 | #include <linux/interrupt.h> | |
46 | #include <linux/ioprio.h> | |
47 | #include <linux/kallsyms.h> | |
0ed557aa | 48 | #include <linux/kcov.h> |
e66f6481 IM |
49 | #include <linux/kprobes.h> |
50 | #include <linux/llist_api.h> | |
51 | #include <linux/mmu_context.h> | |
52 | #include <linux/mmzone.h> | |
53 | #include <linux/mutex_api.h> | |
54 | #include <linux/nmi.h> | |
55 | #include <linux/nospec.h> | |
56 | #include <linux/perf_event_api.h> | |
57 | #include <linux/profile.h> | |
58 | #include <linux/psi.h> | |
59 | #include <linux/rcuwait_api.h> | |
60 | #include <linux/sched/wake_q.h> | |
d08b9f0c | 61 | #include <linux/scs.h> |
e66f6481 IM |
62 | #include <linux/slab.h> |
63 | #include <linux/syscalls.h> | |
64 | #include <linux/vtime.h> | |
65 | #include <linux/wait_api.h> | |
66 | #include <linux/workqueue_api.h> | |
67 | ||
68 | #ifdef CONFIG_PREEMPT_DYNAMIC | |
a7b2553b IM |
69 | # ifdef CONFIG_GENERIC_ENTRY |
70 | # include <linux/entry-common.h> | |
71 | # endif | |
e66f6481 IM |
72 | #endif |
73 | ||
74 | #include <uapi/linux/sched/types.h> | |
0ed557aa | 75 | |
bc1cca97 | 76 | #include <asm/irq_regs.h> |
96f951ed | 77 | #include <asm/switch_to.h> |
5517d86b | 78 | #include <asm/tlb.h> |
1da177e4 | 79 | |
9d246053 | 80 | #define CREATE_TRACE_POINTS |
e66f6481 | 81 | #include <linux/sched/rseq_api.h> |
9d246053 | 82 | #include <trace/events/sched.h> |
cc9cb0a7 | 83 | #include <trace/events/ipi.h> |
9d246053 PA |
84 | #undef CREATE_TRACE_POINTS |
85 | ||
325ea10c | 86 | #include "sched.h" |
b9e9c6ca IM |
87 | #include "stats.h" |
88 | #include "autogroup.h" | |
6e0534f2 | 89 | |
e66f6481 | 90 | #include "autogroup.h" |
91c27493 | 91 | #include "pelt.h" |
1f8db415 | 92 | #include "smp.h" |
e66f6481 | 93 | #include "stats.h" |
1da177e4 | 94 | |
ea138446 | 95 | #include "../workqueue_internal.h" |
ed29b0b4 | 96 | #include "../../io_uring/io-wq.h" |
29d5e047 | 97 | #include "../smpboot.h" |
91c27493 | 98 | |
68e2d17c | 99 | EXPORT_TRACEPOINT_SYMBOL_GPL(ipi_send_cpu); |
cc9cb0a7 VS |
100 | EXPORT_TRACEPOINT_SYMBOL_GPL(ipi_send_cpumask); |
101 | ||
a056a5be QY |
102 | /* |
103 | * Export tracepoints that act as a bare tracehook (ie: have no trace event | |
104 | * associated with them) to allow external modules to probe them. | |
105 | */ | |
106 | EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_cfs_tp); | |
107 | EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_rt_tp); | |
108 | EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_dl_tp); | |
109 | EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_irq_tp); | |
110 | EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_se_tp); | |
77cf151b | 111 | EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_thermal_tp); |
51cf18c9 | 112 | EXPORT_TRACEPOINT_SYMBOL_GPL(sched_cpu_capacity_tp); |
a056a5be | 113 | EXPORT_TRACEPOINT_SYMBOL_GPL(sched_overutilized_tp); |
4581bea8 VD |
114 | EXPORT_TRACEPOINT_SYMBOL_GPL(sched_util_est_cfs_tp); |
115 | EXPORT_TRACEPOINT_SYMBOL_GPL(sched_util_est_se_tp); | |
9d246053 | 116 | EXPORT_TRACEPOINT_SYMBOL_GPL(sched_update_nr_running_tp); |
a056a5be | 117 | |
029632fb | 118 | DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); |
dc61b1d6 | 119 | |
a73f863a | 120 | #ifdef CONFIG_SCHED_DEBUG |
bf5c91ba IM |
121 | /* |
122 | * Debugging: various feature bits | |
765cc3a4 PB |
123 | * |
124 | * If SCHED_DEBUG is disabled, each compilation unit has its own copy of | |
125 | * sysctl_sched_features, defined in sched.h, to allow constants propagation | |
126 | * at compile time and compiler optimization based on features default. | |
bf5c91ba | 127 | */ |
f00b45c1 PZ |
128 | #define SCHED_FEAT(name, enabled) \ |
129 | (1UL << __SCHED_FEAT_##name) * enabled | | |
bf5c91ba | 130 | const_debug unsigned int sysctl_sched_features = |
391e43da | 131 | #include "features.h" |
f00b45c1 | 132 | 0; |
f00b45c1 | 133 | #undef SCHED_FEAT |
c006fac5 PT |
134 | |
135 | /* | |
136 | * Print a warning if need_resched is set for the given duration (if | |
137 | * LATENCY_WARN is enabled). | |
138 | * | |
139 | * If sysctl_resched_latency_warn_once is set, only one warning will be shown | |
140 | * per boot. | |
141 | */ | |
142 | __read_mostly int sysctl_resched_latency_warn_ms = 100; | |
143 | __read_mostly int sysctl_resched_latency_warn_once = 1; | |
144 | #endif /* CONFIG_SCHED_DEBUG */ | |
f00b45c1 | 145 | |
b82d9fdd PZ |
146 | /* |
147 | * Number of tasks to iterate in a single balance run. | |
148 | * Limited because this is done with IRQs disabled. | |
149 | */ | |
c59862f8 | 150 | const_debug unsigned int sysctl_sched_nr_migrate = SCHED_NR_MIGRATE_BREAK; |
b82d9fdd | 151 | |
029632fb | 152 | __read_mostly int scheduler_running; |
6892b75e | 153 | |
9edeaea1 PZ |
154 | #ifdef CONFIG_SCHED_CORE |
155 | ||
156 | DEFINE_STATIC_KEY_FALSE(__sched_core_enabled); | |
157 | ||
8a311c74 | 158 | /* kernel prio, less is more */ |
904cbab7 | 159 | static inline int __task_prio(const struct task_struct *p) |
8a311c74 PZ |
160 | { |
161 | if (p->sched_class == &stop_sched_class) /* trumps deadline */ | |
162 | return -2; | |
163 | ||
164 | if (rt_prio(p->prio)) /* includes deadline */ | |
165 | return p->prio; /* [-1, 99] */ | |
166 | ||
167 | if (p->sched_class == &idle_sched_class) | |
168 | return MAX_RT_PRIO + NICE_WIDTH; /* 140 */ | |
169 | ||
170 | return MAX_RT_PRIO + MAX_NICE; /* 120, squash fair */ | |
171 | } | |
172 | ||
173 | /* | |
174 | * l(a,b) | |
175 | * le(a,b) := !l(b,a) | |
176 | * g(a,b) := l(b,a) | |
177 | * ge(a,b) := !l(a,b) | |
178 | */ | |
179 | ||
180 | /* real prio, less is less */ | |
904cbab7 MWO |
181 | static inline bool prio_less(const struct task_struct *a, |
182 | const struct task_struct *b, bool in_fi) | |
8a311c74 PZ |
183 | { |
184 | ||
185 | int pa = __task_prio(a), pb = __task_prio(b); | |
186 | ||
187 | if (-pa < -pb) | |
188 | return true; | |
189 | ||
190 | if (-pb < -pa) | |
191 | return false; | |
192 | ||
193 | if (pa == -1) /* dl_prio() doesn't work because of stop_class above */ | |
194 | return !dl_time_before(a->dl.deadline, b->dl.deadline); | |
195 | ||
c6047c2e JFG |
196 | if (pa == MAX_RT_PRIO + MAX_NICE) /* fair */ |
197 | return cfs_prio_less(a, b, in_fi); | |
8a311c74 PZ |
198 | |
199 | return false; | |
200 | } | |
201 | ||
904cbab7 MWO |
202 | static inline bool __sched_core_less(const struct task_struct *a, |
203 | const struct task_struct *b) | |
8a311c74 PZ |
204 | { |
205 | if (a->core_cookie < b->core_cookie) | |
206 | return true; | |
207 | ||
208 | if (a->core_cookie > b->core_cookie) | |
209 | return false; | |
210 | ||
211 | /* flip prio, so high prio is leftmost */ | |
4feee7d1 | 212 | if (prio_less(b, a, !!task_rq(a)->core->core_forceidle_count)) |
8a311c74 PZ |
213 | return true; |
214 | ||
215 | return false; | |
216 | } | |
217 | ||
218 | #define __node_2_sc(node) rb_entry((node), struct task_struct, core_node) | |
219 | ||
220 | static inline bool rb_sched_core_less(struct rb_node *a, const struct rb_node *b) | |
221 | { | |
222 | return __sched_core_less(__node_2_sc(a), __node_2_sc(b)); | |
223 | } | |
224 | ||
225 | static inline int rb_sched_core_cmp(const void *key, const struct rb_node *node) | |
226 | { | |
227 | const struct task_struct *p = __node_2_sc(node); | |
228 | unsigned long cookie = (unsigned long)key; | |
229 | ||
230 | if (cookie < p->core_cookie) | |
231 | return -1; | |
232 | ||
233 | if (cookie > p->core_cookie) | |
234 | return 1; | |
235 | ||
236 | return 0; | |
237 | } | |
238 | ||
6e33cad0 | 239 | void sched_core_enqueue(struct rq *rq, struct task_struct *p) |
8a311c74 PZ |
240 | { |
241 | rq->core->core_task_seq++; | |
242 | ||
243 | if (!p->core_cookie) | |
244 | return; | |
245 | ||
246 | rb_add(&p->core_node, &rq->core_tree, rb_sched_core_less); | |
247 | } | |
248 | ||
4feee7d1 | 249 | void sched_core_dequeue(struct rq *rq, struct task_struct *p, int flags) |
8a311c74 PZ |
250 | { |
251 | rq->core->core_task_seq++; | |
252 | ||
4feee7d1 JD |
253 | if (sched_core_enqueued(p)) { |
254 | rb_erase(&p->core_node, &rq->core_tree); | |
255 | RB_CLEAR_NODE(&p->core_node); | |
256 | } | |
8a311c74 | 257 | |
4feee7d1 JD |
258 | /* |
259 | * Migrating the last task off the cpu, with the cpu in forced idle | |
260 | * state. Reschedule to create an accounting edge for forced idle, | |
261 | * and re-examine whether the core is still in forced idle state. | |
262 | */ | |
263 | if (!(flags & DEQUEUE_SAVE) && rq->nr_running == 1 && | |
264 | rq->core->core_forceidle_count && rq->curr == rq->idle) | |
265 | resched_curr(rq); | |
8a311c74 PZ |
266 | } |
267 | ||
530bfad1 | 268 | static int sched_task_is_throttled(struct task_struct *p, int cpu) |
8a311c74 | 269 | { |
530bfad1 HJ |
270 | if (p->sched_class->task_is_throttled) |
271 | return p->sched_class->task_is_throttled(p, cpu); | |
8a311c74 | 272 | |
530bfad1 | 273 | return 0; |
8a311c74 PZ |
274 | } |
275 | ||
d2dfa17b PZ |
276 | static struct task_struct *sched_core_next(struct task_struct *p, unsigned long cookie) |
277 | { | |
278 | struct rb_node *node = &p->core_node; | |
530bfad1 | 279 | int cpu = task_cpu(p); |
d2dfa17b | 280 | |
530bfad1 HJ |
281 | do { |
282 | node = rb_next(node); | |
283 | if (!node) | |
284 | return NULL; | |
d2dfa17b | 285 | |
530bfad1 HJ |
286 | p = __node_2_sc(node); |
287 | if (p->core_cookie != cookie) | |
288 | return NULL; | |
289 | ||
290 | } while (sched_task_is_throttled(p, cpu)); | |
291 | ||
292 | return p; | |
293 | } | |
294 | ||
295 | /* | |
296 | * Find left-most (aka, highest priority) and unthrottled task matching @cookie. | |
297 | * If no suitable task is found, NULL will be returned. | |
298 | */ | |
299 | static struct task_struct *sched_core_find(struct rq *rq, unsigned long cookie) | |
300 | { | |
301 | struct task_struct *p; | |
302 | struct rb_node *node; | |
303 | ||
304 | node = rb_find_first((void *)cookie, &rq->core_tree, rb_sched_core_cmp); | |
d2dfa17b PZ |
305 | if (!node) |
306 | return NULL; | |
307 | ||
530bfad1 HJ |
308 | p = __node_2_sc(node); |
309 | if (!sched_task_is_throttled(p, rq->cpu)) | |
310 | return p; | |
d2dfa17b | 311 | |
530bfad1 | 312 | return sched_core_next(p, cookie); |
d2dfa17b PZ |
313 | } |
314 | ||
9edeaea1 PZ |
315 | /* |
316 | * Magic required such that: | |
317 | * | |
318 | * raw_spin_rq_lock(rq); | |
319 | * ... | |
320 | * raw_spin_rq_unlock(rq); | |
321 | * | |
322 | * ends up locking and unlocking the _same_ lock, and all CPUs | |
323 | * always agree on what rq has what lock. | |
324 | * | |
325 | * XXX entirely possible to selectively enable cores, don't bother for now. | |
326 | */ | |
327 | ||
328 | static DEFINE_MUTEX(sched_core_mutex); | |
875feb41 | 329 | static atomic_t sched_core_count; |
9edeaea1 PZ |
330 | static struct cpumask sched_core_mask; |
331 | ||
3c474b32 PZ |
332 | static void sched_core_lock(int cpu, unsigned long *flags) |
333 | { | |
334 | const struct cpumask *smt_mask = cpu_smt_mask(cpu); | |
335 | int t, i = 0; | |
336 | ||
337 | local_irq_save(*flags); | |
338 | for_each_cpu(t, smt_mask) | |
339 | raw_spin_lock_nested(&cpu_rq(t)->__lock, i++); | |
340 | } | |
341 | ||
342 | static void sched_core_unlock(int cpu, unsigned long *flags) | |
343 | { | |
344 | const struct cpumask *smt_mask = cpu_smt_mask(cpu); | |
345 | int t; | |
346 | ||
347 | for_each_cpu(t, smt_mask) | |
348 | raw_spin_unlock(&cpu_rq(t)->__lock); | |
349 | local_irq_restore(*flags); | |
350 | } | |
351 | ||
9edeaea1 PZ |
352 | static void __sched_core_flip(bool enabled) |
353 | { | |
3c474b32 PZ |
354 | unsigned long flags; |
355 | int cpu, t; | |
9edeaea1 PZ |
356 | |
357 | cpus_read_lock(); | |
358 | ||
359 | /* | |
360 | * Toggle the online cores, one by one. | |
361 | */ | |
362 | cpumask_copy(&sched_core_mask, cpu_online_mask); | |
363 | for_each_cpu(cpu, &sched_core_mask) { | |
364 | const struct cpumask *smt_mask = cpu_smt_mask(cpu); | |
365 | ||
3c474b32 | 366 | sched_core_lock(cpu, &flags); |
9edeaea1 PZ |
367 | |
368 | for_each_cpu(t, smt_mask) | |
369 | cpu_rq(t)->core_enabled = enabled; | |
370 | ||
4feee7d1 JD |
371 | cpu_rq(cpu)->core->core_forceidle_start = 0; |
372 | ||
3c474b32 | 373 | sched_core_unlock(cpu, &flags); |
9edeaea1 PZ |
374 | |
375 | cpumask_andnot(&sched_core_mask, &sched_core_mask, smt_mask); | |
376 | } | |
377 | ||
378 | /* | |
379 | * Toggle the offline CPUs. | |
380 | */ | |
585463f0 | 381 | for_each_cpu_andnot(cpu, cpu_possible_mask, cpu_online_mask) |
9edeaea1 PZ |
382 | cpu_rq(cpu)->core_enabled = enabled; |
383 | ||
384 | cpus_read_unlock(); | |
385 | } | |
386 | ||
8a311c74 | 387 | static void sched_core_assert_empty(void) |
9edeaea1 | 388 | { |
8a311c74 | 389 | int cpu; |
9edeaea1 | 390 | |
8a311c74 PZ |
391 | for_each_possible_cpu(cpu) |
392 | WARN_ON_ONCE(!RB_EMPTY_ROOT(&cpu_rq(cpu)->core_tree)); | |
393 | } | |
394 | ||
395 | static void __sched_core_enable(void) | |
396 | { | |
9edeaea1 PZ |
397 | static_branch_enable(&__sched_core_enabled); |
398 | /* | |
399 | * Ensure all previous instances of raw_spin_rq_*lock() have finished | |
400 | * and future ones will observe !sched_core_disabled(). | |
401 | */ | |
402 | synchronize_rcu(); | |
403 | __sched_core_flip(true); | |
8a311c74 | 404 | sched_core_assert_empty(); |
9edeaea1 PZ |
405 | } |
406 | ||
407 | static void __sched_core_disable(void) | |
408 | { | |
8a311c74 | 409 | sched_core_assert_empty(); |
9edeaea1 PZ |
410 | __sched_core_flip(false); |
411 | static_branch_disable(&__sched_core_enabled); | |
412 | } | |
413 | ||
414 | void sched_core_get(void) | |
415 | { | |
875feb41 PZ |
416 | if (atomic_inc_not_zero(&sched_core_count)) |
417 | return; | |
418 | ||
9edeaea1 | 419 | mutex_lock(&sched_core_mutex); |
875feb41 | 420 | if (!atomic_read(&sched_core_count)) |
9edeaea1 | 421 | __sched_core_enable(); |
875feb41 PZ |
422 | |
423 | smp_mb__before_atomic(); | |
424 | atomic_inc(&sched_core_count); | |
9edeaea1 PZ |
425 | mutex_unlock(&sched_core_mutex); |
426 | } | |
427 | ||
875feb41 | 428 | static void __sched_core_put(struct work_struct *work) |
9edeaea1 | 429 | { |
875feb41 | 430 | if (atomic_dec_and_mutex_lock(&sched_core_count, &sched_core_mutex)) { |
9edeaea1 | 431 | __sched_core_disable(); |
875feb41 PZ |
432 | mutex_unlock(&sched_core_mutex); |
433 | } | |
434 | } | |
435 | ||
436 | void sched_core_put(void) | |
437 | { | |
438 | static DECLARE_WORK(_work, __sched_core_put); | |
439 | ||
440 | /* | |
441 | * "There can be only one" | |
442 | * | |
443 | * Either this is the last one, or we don't actually need to do any | |
444 | * 'work'. If it is the last *again*, we rely on | |
445 | * WORK_STRUCT_PENDING_BIT. | |
446 | */ | |
447 | if (!atomic_add_unless(&sched_core_count, -1, 1)) | |
448 | schedule_work(&_work); | |
9edeaea1 PZ |
449 | } |
450 | ||
8a311c74 PZ |
451 | #else /* !CONFIG_SCHED_CORE */ |
452 | ||
453 | static inline void sched_core_enqueue(struct rq *rq, struct task_struct *p) { } | |
4feee7d1 JD |
454 | static inline void |
455 | sched_core_dequeue(struct rq *rq, struct task_struct *p, int flags) { } | |
8a311c74 | 456 | |
9edeaea1 PZ |
457 | #endif /* CONFIG_SCHED_CORE */ |
458 | ||
58877d34 PZ |
459 | /* |
460 | * Serialization rules: | |
461 | * | |
462 | * Lock order: | |
463 | * | |
464 | * p->pi_lock | |
465 | * rq->lock | |
466 | * hrtimer_cpu_base->lock (hrtimer_start() for bandwidth controls) | |
467 | * | |
468 | * rq1->lock | |
469 | * rq2->lock where: rq1 < rq2 | |
470 | * | |
471 | * Regular state: | |
472 | * | |
473 | * Normal scheduling state is serialized by rq->lock. __schedule() takes the | |
474 | * local CPU's rq->lock, it optionally removes the task from the runqueue and | |
b19a888c | 475 | * always looks at the local rq data structures to find the most eligible task |
58877d34 PZ |
476 | * to run next. |
477 | * | |
478 | * Task enqueue is also under rq->lock, possibly taken from another CPU. | |
479 | * Wakeups from another LLC domain might use an IPI to transfer the enqueue to | |
480 | * the local CPU to avoid bouncing the runqueue state around [ see | |
481 | * ttwu_queue_wakelist() ] | |
482 | * | |
483 | * Task wakeup, specifically wakeups that involve migration, are horribly | |
484 | * complicated to avoid having to take two rq->locks. | |
485 | * | |
486 | * Special state: | |
487 | * | |
488 | * System-calls and anything external will use task_rq_lock() which acquires | |
489 | * both p->pi_lock and rq->lock. As a consequence the state they change is | |
490 | * stable while holding either lock: | |
491 | * | |
492 | * - sched_setaffinity()/ | |
493 | * set_cpus_allowed_ptr(): p->cpus_ptr, p->nr_cpus_allowed | |
494 | * - set_user_nice(): p->se.load, p->*prio | |
495 | * - __sched_setscheduler(): p->sched_class, p->policy, p->*prio, | |
496 | * p->se.load, p->rt_priority, | |
497 | * p->dl.dl_{runtime, deadline, period, flags, bw, density} | |
498 | * - sched_setnuma(): p->numa_preferred_nid | |
39c42611 | 499 | * - sched_move_task(): p->sched_task_group |
58877d34 PZ |
500 | * - uclamp_update_active() p->uclamp* |
501 | * | |
502 | * p->state <- TASK_*: | |
503 | * | |
504 | * is changed locklessly using set_current_state(), __set_current_state() or | |
505 | * set_special_state(), see their respective comments, or by | |
506 | * try_to_wake_up(). This latter uses p->pi_lock to serialize against | |
507 | * concurrent self. | |
508 | * | |
509 | * p->on_rq <- { 0, 1 = TASK_ON_RQ_QUEUED, 2 = TASK_ON_RQ_MIGRATING }: | |
510 | * | |
511 | * is set by activate_task() and cleared by deactivate_task(), under | |
512 | * rq->lock. Non-zero indicates the task is runnable, the special | |
513 | * ON_RQ_MIGRATING state is used for migration without holding both | |
514 | * rq->locks. It indicates task_cpu() is not stable, see task_rq_lock(). | |
515 | * | |
516 | * p->on_cpu <- { 0, 1 }: | |
517 | * | |
518 | * is set by prepare_task() and cleared by finish_task() such that it will be | |
519 | * set before p is scheduled-in and cleared after p is scheduled-out, both | |
520 | * under rq->lock. Non-zero indicates the task is running on its CPU. | |
521 | * | |
522 | * [ The astute reader will observe that it is possible for two tasks on one | |
523 | * CPU to have ->on_cpu = 1 at the same time. ] | |
524 | * | |
525 | * task_cpu(p): is changed by set_task_cpu(), the rules are: | |
526 | * | |
527 | * - Don't call set_task_cpu() on a blocked task: | |
528 | * | |
529 | * We don't care what CPU we're not running on, this simplifies hotplug, | |
530 | * the CPU assignment of blocked tasks isn't required to be valid. | |
531 | * | |
532 | * - for try_to_wake_up(), called under p->pi_lock: | |
533 | * | |
534 | * This allows try_to_wake_up() to only take one rq->lock, see its comment. | |
535 | * | |
536 | * - for migration called under rq->lock: | |
537 | * [ see task_on_rq_migrating() in task_rq_lock() ] | |
538 | * | |
539 | * o move_queued_task() | |
540 | * o detach_task() | |
541 | * | |
542 | * - for migration called under double_rq_lock(): | |
543 | * | |
544 | * o __migrate_swap_task() | |
545 | * o push_rt_task() / pull_rt_task() | |
546 | * o push_dl_task() / pull_dl_task() | |
547 | * o dl_task_offline_migration() | |
548 | * | |
549 | */ | |
550 | ||
39d371b7 PZ |
551 | void raw_spin_rq_lock_nested(struct rq *rq, int subclass) |
552 | { | |
d66f1b06 PZ |
553 | raw_spinlock_t *lock; |
554 | ||
9edeaea1 PZ |
555 | /* Matches synchronize_rcu() in __sched_core_enable() */ |
556 | preempt_disable(); | |
d66f1b06 PZ |
557 | if (sched_core_disabled()) { |
558 | raw_spin_lock_nested(&rq->__lock, subclass); | |
9edeaea1 PZ |
559 | /* preempt_count *MUST* be > 1 */ |
560 | preempt_enable_no_resched(); | |
d66f1b06 PZ |
561 | return; |
562 | } | |
563 | ||
564 | for (;;) { | |
9ef7e7e3 | 565 | lock = __rq_lockp(rq); |
d66f1b06 | 566 | raw_spin_lock_nested(lock, subclass); |
9ef7e7e3 | 567 | if (likely(lock == __rq_lockp(rq))) { |
9edeaea1 PZ |
568 | /* preempt_count *MUST* be > 1 */ |
569 | preempt_enable_no_resched(); | |
d66f1b06 | 570 | return; |
9edeaea1 | 571 | } |
d66f1b06 PZ |
572 | raw_spin_unlock(lock); |
573 | } | |
39d371b7 PZ |
574 | } |
575 | ||
576 | bool raw_spin_rq_trylock(struct rq *rq) | |
577 | { | |
d66f1b06 PZ |
578 | raw_spinlock_t *lock; |
579 | bool ret; | |
580 | ||
9edeaea1 PZ |
581 | /* Matches synchronize_rcu() in __sched_core_enable() */ |
582 | preempt_disable(); | |
583 | if (sched_core_disabled()) { | |
584 | ret = raw_spin_trylock(&rq->__lock); | |
585 | preempt_enable(); | |
586 | return ret; | |
587 | } | |
d66f1b06 PZ |
588 | |
589 | for (;;) { | |
9ef7e7e3 | 590 | lock = __rq_lockp(rq); |
d66f1b06 | 591 | ret = raw_spin_trylock(lock); |
9ef7e7e3 | 592 | if (!ret || (likely(lock == __rq_lockp(rq)))) { |
9edeaea1 | 593 | preempt_enable(); |
d66f1b06 | 594 | return ret; |
9edeaea1 | 595 | } |
d66f1b06 PZ |
596 | raw_spin_unlock(lock); |
597 | } | |
39d371b7 PZ |
598 | } |
599 | ||
600 | void raw_spin_rq_unlock(struct rq *rq) | |
601 | { | |
602 | raw_spin_unlock(rq_lockp(rq)); | |
603 | } | |
604 | ||
d66f1b06 PZ |
605 | #ifdef CONFIG_SMP |
606 | /* | |
607 | * double_rq_lock - safely lock two runqueues | |
608 | */ | |
609 | void double_rq_lock(struct rq *rq1, struct rq *rq2) | |
610 | { | |
611 | lockdep_assert_irqs_disabled(); | |
612 | ||
613 | if (rq_order_less(rq2, rq1)) | |
614 | swap(rq1, rq2); | |
615 | ||
616 | raw_spin_rq_lock(rq1); | |
2679a837 HJ |
617 | if (__rq_lockp(rq1) != __rq_lockp(rq2)) |
618 | raw_spin_rq_lock_nested(rq2, SINGLE_DEPTH_NESTING); | |
d66f1b06 | 619 | |
2679a837 | 620 | double_rq_clock_clear_update(rq1, rq2); |
d66f1b06 PZ |
621 | } |
622 | #endif | |
623 | ||
3e71a462 PZ |
624 | /* |
625 | * __task_rq_lock - lock the rq @p resides on. | |
626 | */ | |
eb580751 | 627 | struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf) |
3e71a462 PZ |
628 | __acquires(rq->lock) |
629 | { | |
630 | struct rq *rq; | |
631 | ||
632 | lockdep_assert_held(&p->pi_lock); | |
633 | ||
634 | for (;;) { | |
635 | rq = task_rq(p); | |
5cb9eaa3 | 636 | raw_spin_rq_lock(rq); |
3e71a462 | 637 | if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) { |
d8ac8971 | 638 | rq_pin_lock(rq, rf); |
3e71a462 PZ |
639 | return rq; |
640 | } | |
5cb9eaa3 | 641 | raw_spin_rq_unlock(rq); |
3e71a462 PZ |
642 | |
643 | while (unlikely(task_on_rq_migrating(p))) | |
644 | cpu_relax(); | |
645 | } | |
646 | } | |
647 | ||
648 | /* | |
649 | * task_rq_lock - lock p->pi_lock and lock the rq @p resides on. | |
650 | */ | |
eb580751 | 651 | struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf) |
3e71a462 PZ |
652 | __acquires(p->pi_lock) |
653 | __acquires(rq->lock) | |
654 | { | |
655 | struct rq *rq; | |
656 | ||
657 | for (;;) { | |
eb580751 | 658 | raw_spin_lock_irqsave(&p->pi_lock, rf->flags); |
3e71a462 | 659 | rq = task_rq(p); |
5cb9eaa3 | 660 | raw_spin_rq_lock(rq); |
3e71a462 PZ |
661 | /* |
662 | * move_queued_task() task_rq_lock() | |
663 | * | |
664 | * ACQUIRE (rq->lock) | |
665 | * [S] ->on_rq = MIGRATING [L] rq = task_rq() | |
666 | * WMB (__set_task_cpu()) ACQUIRE (rq->lock); | |
667 | * [S] ->cpu = new_cpu [L] task_rq() | |
668 | * [L] ->on_rq | |
669 | * RELEASE (rq->lock) | |
670 | * | |
c546951d | 671 | * If we observe the old CPU in task_rq_lock(), the acquire of |
3e71a462 PZ |
672 | * the old rq->lock will fully serialize against the stores. |
673 | * | |
c546951d AP |
674 | * If we observe the new CPU in task_rq_lock(), the address |
675 | * dependency headed by '[L] rq = task_rq()' and the acquire | |
676 | * will pair with the WMB to ensure we then also see migrating. | |
3e71a462 PZ |
677 | */ |
678 | if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) { | |
d8ac8971 | 679 | rq_pin_lock(rq, rf); |
3e71a462 PZ |
680 | return rq; |
681 | } | |
5cb9eaa3 | 682 | raw_spin_rq_unlock(rq); |
eb580751 | 683 | raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags); |
3e71a462 PZ |
684 | |
685 | while (unlikely(task_on_rq_migrating(p))) | |
686 | cpu_relax(); | |
687 | } | |
688 | } | |
689 | ||
535b9552 IM |
690 | /* |
691 | * RQ-clock updating methods: | |
692 | */ | |
693 | ||
694 | static void update_rq_clock_task(struct rq *rq, s64 delta) | |
695 | { | |
696 | /* | |
697 | * In theory, the compile should just see 0 here, and optimize out the call | |
698 | * to sched_rt_avg_update. But I don't trust it... | |
699 | */ | |
11d4afd4 VG |
700 | s64 __maybe_unused steal = 0, irq_delta = 0; |
701 | ||
535b9552 IM |
702 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING |
703 | irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time; | |
704 | ||
705 | /* | |
706 | * Since irq_time is only updated on {soft,}irq_exit, we might run into | |
707 | * this case when a previous update_rq_clock() happened inside a | |
708 | * {soft,}irq region. | |
709 | * | |
710 | * When this happens, we stop ->clock_task and only update the | |
711 | * prev_irq_time stamp to account for the part that fit, so that a next | |
712 | * update will consume the rest. This ensures ->clock_task is | |
713 | * monotonic. | |
714 | * | |
715 | * It does however cause some slight miss-attribution of {soft,}irq | |
716 | * time, a more accurate solution would be to update the irq_time using | |
717 | * the current rq->clock timestamp, except that would require using | |
718 | * atomic ops. | |
719 | */ | |
720 | if (irq_delta > delta) | |
721 | irq_delta = delta; | |
722 | ||
723 | rq->prev_irq_time += irq_delta; | |
724 | delta -= irq_delta; | |
52b1364b | 725 | psi_account_irqtime(rq->curr, irq_delta); |
a3b2aeac | 726 | delayacct_irq(rq->curr, irq_delta); |
535b9552 IM |
727 | #endif |
728 | #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING | |
729 | if (static_key_false((¶virt_steal_rq_enabled))) { | |
730 | steal = paravirt_steal_clock(cpu_of(rq)); | |
731 | steal -= rq->prev_steal_time_rq; | |
732 | ||
733 | if (unlikely(steal > delta)) | |
734 | steal = delta; | |
735 | ||
736 | rq->prev_steal_time_rq += steal; | |
737 | delta -= steal; | |
738 | } | |
739 | #endif | |
740 | ||
741 | rq->clock_task += delta; | |
742 | ||
11d4afd4 | 743 | #ifdef CONFIG_HAVE_SCHED_AVG_IRQ |
535b9552 | 744 | if ((irq_delta + steal) && sched_feat(NONTASK_CAPACITY)) |
91c27493 | 745 | update_irq_load_avg(rq, irq_delta + steal); |
535b9552 | 746 | #endif |
23127296 | 747 | update_rq_clock_pelt(rq, delta); |
535b9552 IM |
748 | } |
749 | ||
750 | void update_rq_clock(struct rq *rq) | |
751 | { | |
752 | s64 delta; | |
753 | ||
5cb9eaa3 | 754 | lockdep_assert_rq_held(rq); |
535b9552 IM |
755 | |
756 | if (rq->clock_update_flags & RQCF_ACT_SKIP) | |
757 | return; | |
758 | ||
759 | #ifdef CONFIG_SCHED_DEBUG | |
26ae58d2 PZ |
760 | if (sched_feat(WARN_DOUBLE_CLOCK)) |
761 | SCHED_WARN_ON(rq->clock_update_flags & RQCF_UPDATED); | |
535b9552 IM |
762 | rq->clock_update_flags |= RQCF_UPDATED; |
763 | #endif | |
26ae58d2 | 764 | |
535b9552 IM |
765 | delta = sched_clock_cpu(cpu_of(rq)) - rq->clock; |
766 | if (delta < 0) | |
767 | return; | |
768 | rq->clock += delta; | |
769 | update_rq_clock_task(rq, delta); | |
770 | } | |
771 | ||
8f4d37ec PZ |
772 | #ifdef CONFIG_SCHED_HRTICK |
773 | /* | |
774 | * Use HR-timers to deliver accurate preemption points. | |
8f4d37ec | 775 | */ |
8f4d37ec | 776 | |
8f4d37ec PZ |
777 | static void hrtick_clear(struct rq *rq) |
778 | { | |
779 | if (hrtimer_active(&rq->hrtick_timer)) | |
780 | hrtimer_cancel(&rq->hrtick_timer); | |
781 | } | |
782 | ||
8f4d37ec PZ |
783 | /* |
784 | * High-resolution timer tick. | |
785 | * Runs from hardirq context with interrupts disabled. | |
786 | */ | |
787 | static enum hrtimer_restart hrtick(struct hrtimer *timer) | |
788 | { | |
789 | struct rq *rq = container_of(timer, struct rq, hrtick_timer); | |
8a8c69c3 | 790 | struct rq_flags rf; |
8f4d37ec PZ |
791 | |
792 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); | |
793 | ||
8a8c69c3 | 794 | rq_lock(rq, &rf); |
3e51f33f | 795 | update_rq_clock(rq); |
8f4d37ec | 796 | rq->curr->sched_class->task_tick(rq, rq->curr, 1); |
8a8c69c3 | 797 | rq_unlock(rq, &rf); |
8f4d37ec PZ |
798 | |
799 | return HRTIMER_NORESTART; | |
800 | } | |
801 | ||
95e904c7 | 802 | #ifdef CONFIG_SMP |
971ee28c | 803 | |
4961b6e1 | 804 | static void __hrtick_restart(struct rq *rq) |
971ee28c PZ |
805 | { |
806 | struct hrtimer *timer = &rq->hrtick_timer; | |
156ec6f4 | 807 | ktime_t time = rq->hrtick_time; |
971ee28c | 808 | |
156ec6f4 | 809 | hrtimer_start(timer, time, HRTIMER_MODE_ABS_PINNED_HARD); |
971ee28c PZ |
810 | } |
811 | ||
31656519 PZ |
812 | /* |
813 | * called from hardirq (IPI) context | |
814 | */ | |
815 | static void __hrtick_start(void *arg) | |
b328ca18 | 816 | { |
31656519 | 817 | struct rq *rq = arg; |
8a8c69c3 | 818 | struct rq_flags rf; |
b328ca18 | 819 | |
8a8c69c3 | 820 | rq_lock(rq, &rf); |
971ee28c | 821 | __hrtick_restart(rq); |
8a8c69c3 | 822 | rq_unlock(rq, &rf); |
b328ca18 PZ |
823 | } |
824 | ||
31656519 PZ |
825 | /* |
826 | * Called to set the hrtick timer state. | |
827 | * | |
828 | * called with rq->lock held and irqs disabled | |
829 | */ | |
029632fb | 830 | void hrtick_start(struct rq *rq, u64 delay) |
b328ca18 | 831 | { |
31656519 | 832 | struct hrtimer *timer = &rq->hrtick_timer; |
177ef2a6 | 833 | s64 delta; |
834 | ||
835 | /* | |
836 | * Don't schedule slices shorter than 10000ns, that just | |
837 | * doesn't make sense and can cause timer DoS. | |
838 | */ | |
839 | delta = max_t(s64, delay, 10000LL); | |
156ec6f4 | 840 | rq->hrtick_time = ktime_add_ns(timer->base->get_time(), delta); |
31656519 | 841 | |
fd3eafda | 842 | if (rq == this_rq()) |
971ee28c | 843 | __hrtick_restart(rq); |
fd3eafda | 844 | else |
c46fff2a | 845 | smp_call_function_single_async(cpu_of(rq), &rq->hrtick_csd); |
b328ca18 PZ |
846 | } |
847 | ||
31656519 PZ |
848 | #else |
849 | /* | |
850 | * Called to set the hrtick timer state. | |
851 | * | |
852 | * called with rq->lock held and irqs disabled | |
853 | */ | |
029632fb | 854 | void hrtick_start(struct rq *rq, u64 delay) |
31656519 | 855 | { |
86893335 WL |
856 | /* |
857 | * Don't schedule slices shorter than 10000ns, that just | |
858 | * doesn't make sense. Rely on vruntime for fairness. | |
859 | */ | |
860 | delay = max_t(u64, delay, 10000LL); | |
4961b6e1 | 861 | hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay), |
d5096aa6 | 862 | HRTIMER_MODE_REL_PINNED_HARD); |
31656519 | 863 | } |
90b5363a | 864 | |
31656519 | 865 | #endif /* CONFIG_SMP */ |
8f4d37ec | 866 | |
77a021be | 867 | static void hrtick_rq_init(struct rq *rq) |
8f4d37ec | 868 | { |
31656519 | 869 | #ifdef CONFIG_SMP |
545b8c8d | 870 | INIT_CSD(&rq->hrtick_csd, __hrtick_start, rq); |
31656519 | 871 | #endif |
d5096aa6 | 872 | hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); |
31656519 | 873 | rq->hrtick_timer.function = hrtick; |
8f4d37ec | 874 | } |
006c75f1 | 875 | #else /* CONFIG_SCHED_HRTICK */ |
8f4d37ec PZ |
876 | static inline void hrtick_clear(struct rq *rq) |
877 | { | |
878 | } | |
879 | ||
77a021be | 880 | static inline void hrtick_rq_init(struct rq *rq) |
8f4d37ec PZ |
881 | { |
882 | } | |
006c75f1 | 883 | #endif /* CONFIG_SCHED_HRTICK */ |
8f4d37ec | 884 | |
5529578a FW |
885 | /* |
886 | * cmpxchg based fetch_or, macro so it works for different integer types | |
887 | */ | |
888 | #define fetch_or(ptr, mask) \ | |
889 | ({ \ | |
890 | typeof(ptr) _ptr = (ptr); \ | |
891 | typeof(mask) _mask = (mask); \ | |
c02d5546 | 892 | typeof(*_ptr) _val = *_ptr; \ |
5529578a | 893 | \ |
c02d5546 UB |
894 | do { \ |
895 | } while (!try_cmpxchg(_ptr, &_val, _val | _mask)); \ | |
896 | _val; \ | |
5529578a FW |
897 | }) |
898 | ||
e3baac47 | 899 | #if defined(CONFIG_SMP) && defined(TIF_POLLING_NRFLAG) |
fd99f91a PZ |
900 | /* |
901 | * Atomically set TIF_NEED_RESCHED and test for TIF_POLLING_NRFLAG, | |
902 | * this avoids any races wrt polling state changes and thereby avoids | |
903 | * spurious IPIs. | |
904 | */ | |
c02d5546 | 905 | static inline bool set_nr_and_not_polling(struct task_struct *p) |
fd99f91a PZ |
906 | { |
907 | struct thread_info *ti = task_thread_info(p); | |
908 | return !(fetch_or(&ti->flags, _TIF_NEED_RESCHED) & _TIF_POLLING_NRFLAG); | |
909 | } | |
e3baac47 PZ |
910 | |
911 | /* | |
912 | * Atomically set TIF_NEED_RESCHED if TIF_POLLING_NRFLAG is set. | |
913 | * | |
914 | * If this returns true, then the idle task promises to call | |
915 | * sched_ttwu_pending() and reschedule soon. | |
916 | */ | |
917 | static bool set_nr_if_polling(struct task_struct *p) | |
918 | { | |
919 | struct thread_info *ti = task_thread_info(p); | |
c02d5546 | 920 | typeof(ti->flags) val = READ_ONCE(ti->flags); |
e3baac47 PZ |
921 | |
922 | for (;;) { | |
923 | if (!(val & _TIF_POLLING_NRFLAG)) | |
924 | return false; | |
925 | if (val & _TIF_NEED_RESCHED) | |
926 | return true; | |
c02d5546 | 927 | if (try_cmpxchg(&ti->flags, &val, val | _TIF_NEED_RESCHED)) |
e3baac47 | 928 | break; |
e3baac47 PZ |
929 | } |
930 | return true; | |
931 | } | |
932 | ||
fd99f91a | 933 | #else |
c02d5546 | 934 | static inline bool set_nr_and_not_polling(struct task_struct *p) |
fd99f91a PZ |
935 | { |
936 | set_tsk_need_resched(p); | |
937 | return true; | |
938 | } | |
e3baac47 PZ |
939 | |
940 | #ifdef CONFIG_SMP | |
c02d5546 | 941 | static inline bool set_nr_if_polling(struct task_struct *p) |
e3baac47 PZ |
942 | { |
943 | return false; | |
944 | } | |
945 | #endif | |
fd99f91a PZ |
946 | #endif |
947 | ||
07879c6a | 948 | static bool __wake_q_add(struct wake_q_head *head, struct task_struct *task) |
76751049 PZ |
949 | { |
950 | struct wake_q_node *node = &task->wake_q; | |
951 | ||
952 | /* | |
953 | * Atomically grab the task, if ->wake_q is !nil already it means | |
b19a888c | 954 | * it's already queued (either by us or someone else) and will get the |
76751049 PZ |
955 | * wakeup due to that. |
956 | * | |
4c4e3731 PZ |
957 | * In order to ensure that a pending wakeup will observe our pending |
958 | * state, even in the failed case, an explicit smp_mb() must be used. | |
76751049 | 959 | */ |
4c4e3731 | 960 | smp_mb__before_atomic(); |
87ff19cb | 961 | if (unlikely(cmpxchg_relaxed(&node->next, NULL, WAKE_Q_TAIL))) |
07879c6a | 962 | return false; |
76751049 PZ |
963 | |
964 | /* | |
965 | * The head is context local, there can be no concurrency. | |
966 | */ | |
967 | *head->lastp = node; | |
968 | head->lastp = &node->next; | |
07879c6a DB |
969 | return true; |
970 | } | |
971 | ||
972 | /** | |
973 | * wake_q_add() - queue a wakeup for 'later' waking. | |
974 | * @head: the wake_q_head to add @task to | |
975 | * @task: the task to queue for 'later' wakeup | |
976 | * | |
977 | * Queue a task for later wakeup, most likely by the wake_up_q() call in the | |
978 | * same context, _HOWEVER_ this is not guaranteed, the wakeup can come | |
979 | * instantly. | |
980 | * | |
981 | * This function must be used as-if it were wake_up_process(); IOW the task | |
982 | * must be ready to be woken at this location. | |
983 | */ | |
984 | void wake_q_add(struct wake_q_head *head, struct task_struct *task) | |
985 | { | |
986 | if (__wake_q_add(head, task)) | |
987 | get_task_struct(task); | |
988 | } | |
989 | ||
990 | /** | |
991 | * wake_q_add_safe() - safely queue a wakeup for 'later' waking. | |
992 | * @head: the wake_q_head to add @task to | |
993 | * @task: the task to queue for 'later' wakeup | |
994 | * | |
995 | * Queue a task for later wakeup, most likely by the wake_up_q() call in the | |
996 | * same context, _HOWEVER_ this is not guaranteed, the wakeup can come | |
997 | * instantly. | |
998 | * | |
999 | * This function must be used as-if it were wake_up_process(); IOW the task | |
1000 | * must be ready to be woken at this location. | |
1001 | * | |
1002 | * This function is essentially a task-safe equivalent to wake_q_add(). Callers | |
1003 | * that already hold reference to @task can call the 'safe' version and trust | |
1004 | * wake_q to do the right thing depending whether or not the @task is already | |
1005 | * queued for wakeup. | |
1006 | */ | |
1007 | void wake_q_add_safe(struct wake_q_head *head, struct task_struct *task) | |
1008 | { | |
1009 | if (!__wake_q_add(head, task)) | |
1010 | put_task_struct(task); | |
76751049 PZ |
1011 | } |
1012 | ||
1013 | void wake_up_q(struct wake_q_head *head) | |
1014 | { | |
1015 | struct wake_q_node *node = head->first; | |
1016 | ||
1017 | while (node != WAKE_Q_TAIL) { | |
1018 | struct task_struct *task; | |
1019 | ||
1020 | task = container_of(node, struct task_struct, wake_q); | |
d1ccc66d | 1021 | /* Task can safely be re-inserted now: */ |
76751049 PZ |
1022 | node = node->next; |
1023 | task->wake_q.next = NULL; | |
1024 | ||
1025 | /* | |
7696f991 AP |
1026 | * wake_up_process() executes a full barrier, which pairs with |
1027 | * the queueing in wake_q_add() so as not to miss wakeups. | |
76751049 PZ |
1028 | */ |
1029 | wake_up_process(task); | |
1030 | put_task_struct(task); | |
1031 | } | |
1032 | } | |
1033 | ||
c24d20db | 1034 | /* |
8875125e | 1035 | * resched_curr - mark rq's current task 'to be rescheduled now'. |
c24d20db IM |
1036 | * |
1037 | * On UP this means the setting of the need_resched flag, on SMP it | |
1038 | * might also involve a cross-CPU call to trigger the scheduler on | |
1039 | * the target CPU. | |
1040 | */ | |
8875125e | 1041 | void resched_curr(struct rq *rq) |
c24d20db | 1042 | { |
8875125e | 1043 | struct task_struct *curr = rq->curr; |
c24d20db IM |
1044 | int cpu; |
1045 | ||
5cb9eaa3 | 1046 | lockdep_assert_rq_held(rq); |
c24d20db | 1047 | |
8875125e | 1048 | if (test_tsk_need_resched(curr)) |
c24d20db IM |
1049 | return; |
1050 | ||
8875125e | 1051 | cpu = cpu_of(rq); |
fd99f91a | 1052 | |
f27dde8d | 1053 | if (cpu == smp_processor_id()) { |
8875125e | 1054 | set_tsk_need_resched(curr); |
f27dde8d | 1055 | set_preempt_need_resched(); |
c24d20db | 1056 | return; |
f27dde8d | 1057 | } |
c24d20db | 1058 | |
8875125e | 1059 | if (set_nr_and_not_polling(curr)) |
c24d20db | 1060 | smp_send_reschedule(cpu); |
dfc68f29 AL |
1061 | else |
1062 | trace_sched_wake_idle_without_ipi(cpu); | |
c24d20db IM |
1063 | } |
1064 | ||
029632fb | 1065 | void resched_cpu(int cpu) |
c24d20db IM |
1066 | { |
1067 | struct rq *rq = cpu_rq(cpu); | |
1068 | unsigned long flags; | |
1069 | ||
5cb9eaa3 | 1070 | raw_spin_rq_lock_irqsave(rq, flags); |
a0982dfa PM |
1071 | if (cpu_online(cpu) || cpu == smp_processor_id()) |
1072 | resched_curr(rq); | |
5cb9eaa3 | 1073 | raw_spin_rq_unlock_irqrestore(rq, flags); |
c24d20db | 1074 | } |
06d8308c | 1075 | |
b021fe3e | 1076 | #ifdef CONFIG_SMP |
3451d024 | 1077 | #ifdef CONFIG_NO_HZ_COMMON |
83cd4fe2 | 1078 | /* |
d1ccc66d IM |
1079 | * In the semi idle case, use the nearest busy CPU for migrating timers |
1080 | * from an idle CPU. This is good for power-savings. | |
83cd4fe2 VP |
1081 | * |
1082 | * We don't do similar optimization for completely idle system, as | |
d1ccc66d IM |
1083 | * selecting an idle CPU will add more delays to the timers than intended |
1084 | * (as that CPU's timer base may not be uptodate wrt jiffies etc). | |
83cd4fe2 | 1085 | */ |
bc7a34b8 | 1086 | int get_nohz_timer_target(void) |
83cd4fe2 | 1087 | { |
e938b9c9 | 1088 | int i, cpu = smp_processor_id(), default_cpu = -1; |
83cd4fe2 | 1089 | struct sched_domain *sd; |
031e3bd8 | 1090 | const struct cpumask *hk_mask; |
83cd4fe2 | 1091 | |
04d4e665 | 1092 | if (housekeeping_cpu(cpu, HK_TYPE_TIMER)) { |
e938b9c9 WL |
1093 | if (!idle_cpu(cpu)) |
1094 | return cpu; | |
1095 | default_cpu = cpu; | |
1096 | } | |
6201b4d6 | 1097 | |
04d4e665 | 1098 | hk_mask = housekeeping_cpumask(HK_TYPE_TIMER); |
031e3bd8 | 1099 | |
057f3fad | 1100 | rcu_read_lock(); |
83cd4fe2 | 1101 | for_each_domain(cpu, sd) { |
031e3bd8 | 1102 | for_each_cpu_and(i, sched_domain_span(sd), hk_mask) { |
44496922 WL |
1103 | if (cpu == i) |
1104 | continue; | |
1105 | ||
e938b9c9 | 1106 | if (!idle_cpu(i)) { |
057f3fad PZ |
1107 | cpu = i; |
1108 | goto unlock; | |
1109 | } | |
1110 | } | |
83cd4fe2 | 1111 | } |
9642d18e | 1112 | |
e938b9c9 | 1113 | if (default_cpu == -1) |
04d4e665 | 1114 | default_cpu = housekeeping_any_cpu(HK_TYPE_TIMER); |
e938b9c9 | 1115 | cpu = default_cpu; |
057f3fad PZ |
1116 | unlock: |
1117 | rcu_read_unlock(); | |
83cd4fe2 VP |
1118 | return cpu; |
1119 | } | |
d1ccc66d | 1120 | |
06d8308c TG |
1121 | /* |
1122 | * When add_timer_on() enqueues a timer into the timer wheel of an | |
1123 | * idle CPU then this timer might expire before the next timer event | |
1124 | * which is scheduled to wake up that CPU. In case of a completely | |
1125 | * idle system the next event might even be infinite time into the | |
1126 | * future. wake_up_idle_cpu() ensures that the CPU is woken up and | |
1127 | * leaves the inner idle loop so the newly added timer is taken into | |
1128 | * account when the CPU goes back to idle and evaluates the timer | |
1129 | * wheel for the next timer event. | |
1130 | */ | |
1c20091e | 1131 | static void wake_up_idle_cpu(int cpu) |
06d8308c TG |
1132 | { |
1133 | struct rq *rq = cpu_rq(cpu); | |
1134 | ||
1135 | if (cpu == smp_processor_id()) | |
1136 | return; | |
1137 | ||
67b9ca70 | 1138 | if (set_nr_and_not_polling(rq->idle)) |
06d8308c | 1139 | smp_send_reschedule(cpu); |
dfc68f29 AL |
1140 | else |
1141 | trace_sched_wake_idle_without_ipi(cpu); | |
45bf76df IM |
1142 | } |
1143 | ||
c5bfece2 | 1144 | static bool wake_up_full_nohz_cpu(int cpu) |
1c20091e | 1145 | { |
53c5fa16 FW |
1146 | /* |
1147 | * We just need the target to call irq_exit() and re-evaluate | |
1148 | * the next tick. The nohz full kick at least implies that. | |
1149 | * If needed we can still optimize that later with an | |
1150 | * empty IRQ. | |
1151 | */ | |
379d9ecb PM |
1152 | if (cpu_is_offline(cpu)) |
1153 | return true; /* Don't try to wake offline CPUs. */ | |
c5bfece2 | 1154 | if (tick_nohz_full_cpu(cpu)) { |
1c20091e FW |
1155 | if (cpu != smp_processor_id() || |
1156 | tick_nohz_tick_stopped()) | |
53c5fa16 | 1157 | tick_nohz_full_kick_cpu(cpu); |
1c20091e FW |
1158 | return true; |
1159 | } | |
1160 | ||
1161 | return false; | |
1162 | } | |
1163 | ||
379d9ecb PM |
1164 | /* |
1165 | * Wake up the specified CPU. If the CPU is going offline, it is the | |
1166 | * caller's responsibility to deal with the lost wakeup, for example, | |
1167 | * by hooking into the CPU_DEAD notifier like timers and hrtimers do. | |
1168 | */ | |
1c20091e FW |
1169 | void wake_up_nohz_cpu(int cpu) |
1170 | { | |
c5bfece2 | 1171 | if (!wake_up_full_nohz_cpu(cpu)) |
1c20091e FW |
1172 | wake_up_idle_cpu(cpu); |
1173 | } | |
1174 | ||
19a1f5ec | 1175 | static void nohz_csd_func(void *info) |
45bf76df | 1176 | { |
19a1f5ec PZ |
1177 | struct rq *rq = info; |
1178 | int cpu = cpu_of(rq); | |
1179 | unsigned int flags; | |
873b4c65 VG |
1180 | |
1181 | /* | |
19a1f5ec | 1182 | * Release the rq::nohz_csd. |
873b4c65 | 1183 | */ |
c6f88654 | 1184 | flags = atomic_fetch_andnot(NOHZ_KICK_MASK | NOHZ_NEWILB_KICK, nohz_flags(cpu)); |
19a1f5ec | 1185 | WARN_ON(!(flags & NOHZ_KICK_MASK)); |
45bf76df | 1186 | |
19a1f5ec PZ |
1187 | rq->idle_balance = idle_cpu(cpu); |
1188 | if (rq->idle_balance && !need_resched()) { | |
1189 | rq->nohz_idle_balance = flags; | |
90b5363a PZI |
1190 | raise_softirq_irqoff(SCHED_SOFTIRQ); |
1191 | } | |
2069dd75 PZ |
1192 | } |
1193 | ||
3451d024 | 1194 | #endif /* CONFIG_NO_HZ_COMMON */ |
d842de87 | 1195 | |
ce831b38 | 1196 | #ifdef CONFIG_NO_HZ_FULL |
76d92ac3 | 1197 | bool sched_can_stop_tick(struct rq *rq) |
ce831b38 | 1198 | { |
76d92ac3 FW |
1199 | int fifo_nr_running; |
1200 | ||
1201 | /* Deadline tasks, even if single, need the tick */ | |
1202 | if (rq->dl.dl_nr_running) | |
1203 | return false; | |
1204 | ||
1e78cdbd | 1205 | /* |
b19a888c | 1206 | * If there are more than one RR tasks, we need the tick to affect the |
2548d546 | 1207 | * actual RR behaviour. |
1e78cdbd | 1208 | */ |
76d92ac3 FW |
1209 | if (rq->rt.rr_nr_running) { |
1210 | if (rq->rt.rr_nr_running == 1) | |
1211 | return true; | |
1212 | else | |
1213 | return false; | |
1e78cdbd RR |
1214 | } |
1215 | ||
2548d546 PZ |
1216 | /* |
1217 | * If there's no RR tasks, but FIFO tasks, we can skip the tick, no | |
1218 | * forced preemption between FIFO tasks. | |
1219 | */ | |
1220 | fifo_nr_running = rq->rt.rt_nr_running - rq->rt.rr_nr_running; | |
1221 | if (fifo_nr_running) | |
1222 | return true; | |
1223 | ||
1224 | /* | |
1225 | * If there are no DL,RR/FIFO tasks, there must only be CFS tasks left; | |
1226 | * if there's more than one we need the tick for involuntary | |
1227 | * preemption. | |
1228 | */ | |
1229 | if (rq->nr_running > 1) | |
541b8264 | 1230 | return false; |
ce831b38 | 1231 | |
541b8264 | 1232 | return true; |
ce831b38 FW |
1233 | } |
1234 | #endif /* CONFIG_NO_HZ_FULL */ | |
6d6bc0ad | 1235 | #endif /* CONFIG_SMP */ |
18d95a28 | 1236 | |
a790de99 PT |
1237 | #if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \ |
1238 | (defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH))) | |
c09595f6 | 1239 | /* |
8277434e PT |
1240 | * Iterate task_group tree rooted at *from, calling @down when first entering a |
1241 | * node and @up when leaving it for the final time. | |
1242 | * | |
1243 | * Caller must hold rcu_lock or sufficient equivalent. | |
c09595f6 | 1244 | */ |
029632fb | 1245 | int walk_tg_tree_from(struct task_group *from, |
8277434e | 1246 | tg_visitor down, tg_visitor up, void *data) |
c09595f6 PZ |
1247 | { |
1248 | struct task_group *parent, *child; | |
eb755805 | 1249 | int ret; |
c09595f6 | 1250 | |
8277434e PT |
1251 | parent = from; |
1252 | ||
c09595f6 | 1253 | down: |
eb755805 PZ |
1254 | ret = (*down)(parent, data); |
1255 | if (ret) | |
8277434e | 1256 | goto out; |
c09595f6 PZ |
1257 | list_for_each_entry_rcu(child, &parent->children, siblings) { |
1258 | parent = child; | |
1259 | goto down; | |
1260 | ||
1261 | up: | |
1262 | continue; | |
1263 | } | |
eb755805 | 1264 | ret = (*up)(parent, data); |
8277434e PT |
1265 | if (ret || parent == from) |
1266 | goto out; | |
c09595f6 PZ |
1267 | |
1268 | child = parent; | |
1269 | parent = parent->parent; | |
1270 | if (parent) | |
1271 | goto up; | |
8277434e | 1272 | out: |
eb755805 | 1273 | return ret; |
c09595f6 PZ |
1274 | } |
1275 | ||
029632fb | 1276 | int tg_nop(struct task_group *tg, void *data) |
eb755805 | 1277 | { |
e2b245f8 | 1278 | return 0; |
eb755805 | 1279 | } |
18d95a28 PZ |
1280 | #endif |
1281 | ||
b1e82065 | 1282 | static void set_load_weight(struct task_struct *p, bool update_load) |
45bf76df | 1283 | { |
f05998d4 NR |
1284 | int prio = p->static_prio - MAX_RT_PRIO; |
1285 | struct load_weight *load = &p->se.load; | |
1286 | ||
dd41f596 IM |
1287 | /* |
1288 | * SCHED_IDLE tasks get minimal weight: | |
1289 | */ | |
1da1843f | 1290 | if (task_has_idle_policy(p)) { |
c8b28116 | 1291 | load->weight = scale_load(WEIGHT_IDLEPRIO); |
f05998d4 | 1292 | load->inv_weight = WMULT_IDLEPRIO; |
dd41f596 IM |
1293 | return; |
1294 | } | |
71f8bd46 | 1295 | |
9059393e VG |
1296 | /* |
1297 | * SCHED_OTHER tasks have to update their load when changing their | |
1298 | * weight | |
1299 | */ | |
1300 | if (update_load && p->sched_class == &fair_sched_class) { | |
1301 | reweight_task(p, prio); | |
1302 | } else { | |
1303 | load->weight = scale_load(sched_prio_to_weight[prio]); | |
1304 | load->inv_weight = sched_prio_to_wmult[prio]; | |
1305 | } | |
71f8bd46 IM |
1306 | } |
1307 | ||
69842cba | 1308 | #ifdef CONFIG_UCLAMP_TASK |
2480c093 PB |
1309 | /* |
1310 | * Serializes updates of utilization clamp values | |
1311 | * | |
1312 | * The (slow-path) user-space triggers utilization clamp value updates which | |
1313 | * can require updates on (fast-path) scheduler's data structures used to | |
1314 | * support enqueue/dequeue operations. | |
1315 | * While the per-CPU rq lock protects fast-path update operations, user-space | |
1316 | * requests are serialized using a mutex to reduce the risk of conflicting | |
1317 | * updates or API abuses. | |
1318 | */ | |
1319 | static DEFINE_MUTEX(uclamp_mutex); | |
1320 | ||
e8f14172 | 1321 | /* Max allowed minimum utilization */ |
494dcdf4 | 1322 | static unsigned int __maybe_unused sysctl_sched_uclamp_util_min = SCHED_CAPACITY_SCALE; |
e8f14172 PB |
1323 | |
1324 | /* Max allowed maximum utilization */ | |
494dcdf4 | 1325 | static unsigned int __maybe_unused sysctl_sched_uclamp_util_max = SCHED_CAPACITY_SCALE; |
e8f14172 | 1326 | |
13685c4a QY |
1327 | /* |
1328 | * By default RT tasks run at the maximum performance point/capacity of the | |
1329 | * system. Uclamp enforces this by always setting UCLAMP_MIN of RT tasks to | |
1330 | * SCHED_CAPACITY_SCALE. | |
1331 | * | |
1332 | * This knob allows admins to change the default behavior when uclamp is being | |
1333 | * used. In battery powered devices, particularly, running at the maximum | |
1334 | * capacity and frequency will increase energy consumption and shorten the | |
1335 | * battery life. | |
1336 | * | |
1337 | * This knob only affects RT tasks that their uclamp_se->user_defined == false. | |
1338 | * | |
1339 | * This knob will not override the system default sched_util_clamp_min defined | |
1340 | * above. | |
1341 | */ | |
3267e015 | 1342 | static unsigned int sysctl_sched_uclamp_util_min_rt_default = SCHED_CAPACITY_SCALE; |
13685c4a | 1343 | |
e8f14172 PB |
1344 | /* All clamps are required to be less or equal than these values */ |
1345 | static struct uclamp_se uclamp_default[UCLAMP_CNT]; | |
69842cba | 1346 | |
46609ce2 QY |
1347 | /* |
1348 | * This static key is used to reduce the uclamp overhead in the fast path. It | |
1349 | * primarily disables the call to uclamp_rq_{inc, dec}() in | |
1350 | * enqueue/dequeue_task(). | |
1351 | * | |
1352 | * This allows users to continue to enable uclamp in their kernel config with | |
1353 | * minimum uclamp overhead in the fast path. | |
1354 | * | |
1355 | * As soon as userspace modifies any of the uclamp knobs, the static key is | |
1356 | * enabled, since we have an actual users that make use of uclamp | |
1357 | * functionality. | |
1358 | * | |
1359 | * The knobs that would enable this static key are: | |
1360 | * | |
1361 | * * A task modifying its uclamp value with sched_setattr(). | |
1362 | * * An admin modifying the sysctl_sched_uclamp_{min, max} via procfs. | |
1363 | * * An admin modifying the cgroup cpu.uclamp.{min, max} | |
1364 | */ | |
1365 | DEFINE_STATIC_KEY_FALSE(sched_uclamp_used); | |
1366 | ||
69842cba PB |
1367 | /* Integer rounded range for each bucket */ |
1368 | #define UCLAMP_BUCKET_DELTA DIV_ROUND_CLOSEST(SCHED_CAPACITY_SCALE, UCLAMP_BUCKETS) | |
1369 | ||
1370 | #define for_each_clamp_id(clamp_id) \ | |
1371 | for ((clamp_id) = 0; (clamp_id) < UCLAMP_CNT; (clamp_id)++) | |
1372 | ||
1373 | static inline unsigned int uclamp_bucket_id(unsigned int clamp_value) | |
1374 | { | |
6d2f8909 | 1375 | return min_t(unsigned int, clamp_value / UCLAMP_BUCKET_DELTA, UCLAMP_BUCKETS - 1); |
69842cba PB |
1376 | } |
1377 | ||
7763baac | 1378 | static inline unsigned int uclamp_none(enum uclamp_id clamp_id) |
69842cba PB |
1379 | { |
1380 | if (clamp_id == UCLAMP_MIN) | |
1381 | return 0; | |
1382 | return SCHED_CAPACITY_SCALE; | |
1383 | } | |
1384 | ||
a509a7cd PB |
1385 | static inline void uclamp_se_set(struct uclamp_se *uc_se, |
1386 | unsigned int value, bool user_defined) | |
69842cba PB |
1387 | { |
1388 | uc_se->value = value; | |
1389 | uc_se->bucket_id = uclamp_bucket_id(value); | |
a509a7cd | 1390 | uc_se->user_defined = user_defined; |
69842cba PB |
1391 | } |
1392 | ||
e496187d | 1393 | static inline unsigned int |
0413d7f3 | 1394 | uclamp_idle_value(struct rq *rq, enum uclamp_id clamp_id, |
e496187d PB |
1395 | unsigned int clamp_value) |
1396 | { | |
1397 | /* | |
1398 | * Avoid blocked utilization pushing up the frequency when we go | |
1399 | * idle (which drops the max-clamp) by retaining the last known | |
1400 | * max-clamp. | |
1401 | */ | |
1402 | if (clamp_id == UCLAMP_MAX) { | |
1403 | rq->uclamp_flags |= UCLAMP_FLAG_IDLE; | |
1404 | return clamp_value; | |
1405 | } | |
1406 | ||
1407 | return uclamp_none(UCLAMP_MIN); | |
1408 | } | |
1409 | ||
0413d7f3 | 1410 | static inline void uclamp_idle_reset(struct rq *rq, enum uclamp_id clamp_id, |
e496187d PB |
1411 | unsigned int clamp_value) |
1412 | { | |
1413 | /* Reset max-clamp retention only on idle exit */ | |
1414 | if (!(rq->uclamp_flags & UCLAMP_FLAG_IDLE)) | |
1415 | return; | |
1416 | ||
24422603 | 1417 | uclamp_rq_set(rq, clamp_id, clamp_value); |
e496187d PB |
1418 | } |
1419 | ||
69842cba | 1420 | static inline |
7763baac | 1421 | unsigned int uclamp_rq_max_value(struct rq *rq, enum uclamp_id clamp_id, |
0413d7f3 | 1422 | unsigned int clamp_value) |
69842cba PB |
1423 | { |
1424 | struct uclamp_bucket *bucket = rq->uclamp[clamp_id].bucket; | |
1425 | int bucket_id = UCLAMP_BUCKETS - 1; | |
1426 | ||
1427 | /* | |
1428 | * Since both min and max clamps are max aggregated, find the | |
1429 | * top most bucket with tasks in. | |
1430 | */ | |
1431 | for ( ; bucket_id >= 0; bucket_id--) { | |
1432 | if (!bucket[bucket_id].tasks) | |
1433 | continue; | |
1434 | return bucket[bucket_id].value; | |
1435 | } | |
1436 | ||
1437 | /* No tasks -- default clamp values */ | |
e496187d | 1438 | return uclamp_idle_value(rq, clamp_id, clamp_value); |
69842cba PB |
1439 | } |
1440 | ||
13685c4a QY |
1441 | static void __uclamp_update_util_min_rt_default(struct task_struct *p) |
1442 | { | |
1443 | unsigned int default_util_min; | |
1444 | struct uclamp_se *uc_se; | |
1445 | ||
1446 | lockdep_assert_held(&p->pi_lock); | |
1447 | ||
1448 | uc_se = &p->uclamp_req[UCLAMP_MIN]; | |
1449 | ||
1450 | /* Only sync if user didn't override the default */ | |
1451 | if (uc_se->user_defined) | |
1452 | return; | |
1453 | ||
1454 | default_util_min = sysctl_sched_uclamp_util_min_rt_default; | |
1455 | uclamp_se_set(uc_se, default_util_min, false); | |
1456 | } | |
1457 | ||
1458 | static void uclamp_update_util_min_rt_default(struct task_struct *p) | |
1459 | { | |
1460 | struct rq_flags rf; | |
1461 | struct rq *rq; | |
1462 | ||
1463 | if (!rt_task(p)) | |
1464 | return; | |
1465 | ||
1466 | /* Protect updates to p->uclamp_* */ | |
1467 | rq = task_rq_lock(p, &rf); | |
1468 | __uclamp_update_util_min_rt_default(p); | |
1469 | task_rq_unlock(rq, p, &rf); | |
1470 | } | |
1471 | ||
3eac870a | 1472 | static inline struct uclamp_se |
0413d7f3 | 1473 | uclamp_tg_restrict(struct task_struct *p, enum uclamp_id clamp_id) |
3eac870a | 1474 | { |
0213b708 | 1475 | /* Copy by value as we could modify it */ |
3eac870a PB |
1476 | struct uclamp_se uc_req = p->uclamp_req[clamp_id]; |
1477 | #ifdef CONFIG_UCLAMP_TASK_GROUP | |
0213b708 | 1478 | unsigned int tg_min, tg_max, value; |
3eac870a PB |
1479 | |
1480 | /* | |
1481 | * Tasks in autogroups or root task group will be | |
1482 | * restricted by system defaults. | |
1483 | */ | |
1484 | if (task_group_is_autogroup(task_group(p))) | |
1485 | return uc_req; | |
1486 | if (task_group(p) == &root_task_group) | |
1487 | return uc_req; | |
1488 | ||
0213b708 QY |
1489 | tg_min = task_group(p)->uclamp[UCLAMP_MIN].value; |
1490 | tg_max = task_group(p)->uclamp[UCLAMP_MAX].value; | |
1491 | value = uc_req.value; | |
1492 | value = clamp(value, tg_min, tg_max); | |
1493 | uclamp_se_set(&uc_req, value, false); | |
3eac870a PB |
1494 | #endif |
1495 | ||
1496 | return uc_req; | |
1497 | } | |
1498 | ||
e8f14172 PB |
1499 | /* |
1500 | * The effective clamp bucket index of a task depends on, by increasing | |
1501 | * priority: | |
1502 | * - the task specific clamp value, when explicitly requested from userspace | |
3eac870a PB |
1503 | * - the task group effective clamp value, for tasks not either in the root |
1504 | * group or in an autogroup | |
e8f14172 PB |
1505 | * - the system default clamp value, defined by the sysadmin |
1506 | */ | |
1507 | static inline struct uclamp_se | |
0413d7f3 | 1508 | uclamp_eff_get(struct task_struct *p, enum uclamp_id clamp_id) |
e8f14172 | 1509 | { |
3eac870a | 1510 | struct uclamp_se uc_req = uclamp_tg_restrict(p, clamp_id); |
e8f14172 PB |
1511 | struct uclamp_se uc_max = uclamp_default[clamp_id]; |
1512 | ||
1513 | /* System default restrictions always apply */ | |
1514 | if (unlikely(uc_req.value > uc_max.value)) | |
1515 | return uc_max; | |
1516 | ||
1517 | return uc_req; | |
1518 | } | |
1519 | ||
686516b5 | 1520 | unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id) |
9d20ad7d PB |
1521 | { |
1522 | struct uclamp_se uc_eff; | |
1523 | ||
1524 | /* Task currently refcounted: use back-annotated (effective) value */ | |
1525 | if (p->uclamp[clamp_id].active) | |
686516b5 | 1526 | return (unsigned long)p->uclamp[clamp_id].value; |
9d20ad7d PB |
1527 | |
1528 | uc_eff = uclamp_eff_get(p, clamp_id); | |
1529 | ||
686516b5 | 1530 | return (unsigned long)uc_eff.value; |
9d20ad7d PB |
1531 | } |
1532 | ||
69842cba PB |
1533 | /* |
1534 | * When a task is enqueued on a rq, the clamp bucket currently defined by the | |
1535 | * task's uclamp::bucket_id is refcounted on that rq. This also immediately | |
1536 | * updates the rq's clamp value if required. | |
60daf9c1 PB |
1537 | * |
1538 | * Tasks can have a task-specific value requested from user-space, track | |
1539 | * within each bucket the maximum value for tasks refcounted in it. | |
1540 | * This "local max aggregation" allows to track the exact "requested" value | |
1541 | * for each bucket when all its RUNNABLE tasks require the same clamp. | |
69842cba PB |
1542 | */ |
1543 | static inline void uclamp_rq_inc_id(struct rq *rq, struct task_struct *p, | |
0413d7f3 | 1544 | enum uclamp_id clamp_id) |
69842cba PB |
1545 | { |
1546 | struct uclamp_rq *uc_rq = &rq->uclamp[clamp_id]; | |
1547 | struct uclamp_se *uc_se = &p->uclamp[clamp_id]; | |
1548 | struct uclamp_bucket *bucket; | |
1549 | ||
5cb9eaa3 | 1550 | lockdep_assert_rq_held(rq); |
69842cba | 1551 | |
e8f14172 PB |
1552 | /* Update task effective clamp */ |
1553 | p->uclamp[clamp_id] = uclamp_eff_get(p, clamp_id); | |
1554 | ||
69842cba PB |
1555 | bucket = &uc_rq->bucket[uc_se->bucket_id]; |
1556 | bucket->tasks++; | |
e8f14172 | 1557 | uc_se->active = true; |
69842cba | 1558 | |
e496187d PB |
1559 | uclamp_idle_reset(rq, clamp_id, uc_se->value); |
1560 | ||
60daf9c1 PB |
1561 | /* |
1562 | * Local max aggregation: rq buckets always track the max | |
1563 | * "requested" clamp value of its RUNNABLE tasks. | |
1564 | */ | |
1565 | if (bucket->tasks == 1 || uc_se->value > bucket->value) | |
1566 | bucket->value = uc_se->value; | |
1567 | ||
24422603 QY |
1568 | if (uc_se->value > uclamp_rq_get(rq, clamp_id)) |
1569 | uclamp_rq_set(rq, clamp_id, uc_se->value); | |
69842cba PB |
1570 | } |
1571 | ||
1572 | /* | |
1573 | * When a task is dequeued from a rq, the clamp bucket refcounted by the task | |
1574 | * is released. If this is the last task reference counting the rq's max | |
1575 | * active clamp value, then the rq's clamp value is updated. | |
1576 | * | |
1577 | * Both refcounted tasks and rq's cached clamp values are expected to be | |
1578 | * always valid. If it's detected they are not, as defensive programming, | |
1579 | * enforce the expected state and warn. | |
1580 | */ | |
1581 | static inline void uclamp_rq_dec_id(struct rq *rq, struct task_struct *p, | |
0413d7f3 | 1582 | enum uclamp_id clamp_id) |
69842cba PB |
1583 | { |
1584 | struct uclamp_rq *uc_rq = &rq->uclamp[clamp_id]; | |
1585 | struct uclamp_se *uc_se = &p->uclamp[clamp_id]; | |
1586 | struct uclamp_bucket *bucket; | |
e496187d | 1587 | unsigned int bkt_clamp; |
69842cba PB |
1588 | unsigned int rq_clamp; |
1589 | ||
5cb9eaa3 | 1590 | lockdep_assert_rq_held(rq); |
69842cba | 1591 | |
46609ce2 QY |
1592 | /* |
1593 | * If sched_uclamp_used was enabled after task @p was enqueued, | |
1594 | * we could end up with unbalanced call to uclamp_rq_dec_id(). | |
1595 | * | |
1596 | * In this case the uc_se->active flag should be false since no uclamp | |
1597 | * accounting was performed at enqueue time and we can just return | |
1598 | * here. | |
1599 | * | |
b19a888c | 1600 | * Need to be careful of the following enqueue/dequeue ordering |
46609ce2 QY |
1601 | * problem too |
1602 | * | |
1603 | * enqueue(taskA) | |
1604 | * // sched_uclamp_used gets enabled | |
1605 | * enqueue(taskB) | |
1606 | * dequeue(taskA) | |
b19a888c | 1607 | * // Must not decrement bucket->tasks here |
46609ce2 QY |
1608 | * dequeue(taskB) |
1609 | * | |
1610 | * where we could end up with stale data in uc_se and | |
1611 | * bucket[uc_se->bucket_id]. | |
1612 | * | |
1613 | * The following check here eliminates the possibility of such race. | |
1614 | */ | |
1615 | if (unlikely(!uc_se->active)) | |
1616 | return; | |
1617 | ||
69842cba | 1618 | bucket = &uc_rq->bucket[uc_se->bucket_id]; |
46609ce2 | 1619 | |
69842cba PB |
1620 | SCHED_WARN_ON(!bucket->tasks); |
1621 | if (likely(bucket->tasks)) | |
1622 | bucket->tasks--; | |
46609ce2 | 1623 | |
e8f14172 | 1624 | uc_se->active = false; |
69842cba | 1625 | |
60daf9c1 PB |
1626 | /* |
1627 | * Keep "local max aggregation" simple and accept to (possibly) | |
1628 | * overboost some RUNNABLE tasks in the same bucket. | |
1629 | * The rq clamp bucket value is reset to its base value whenever | |
1630 | * there are no more RUNNABLE tasks refcounting it. | |
1631 | */ | |
69842cba PB |
1632 | if (likely(bucket->tasks)) |
1633 | return; | |
1634 | ||
24422603 | 1635 | rq_clamp = uclamp_rq_get(rq, clamp_id); |
69842cba PB |
1636 | /* |
1637 | * Defensive programming: this should never happen. If it happens, | |
1638 | * e.g. due to future modification, warn and fixup the expected value. | |
1639 | */ | |
1640 | SCHED_WARN_ON(bucket->value > rq_clamp); | |
e496187d PB |
1641 | if (bucket->value >= rq_clamp) { |
1642 | bkt_clamp = uclamp_rq_max_value(rq, clamp_id, uc_se->value); | |
24422603 | 1643 | uclamp_rq_set(rq, clamp_id, bkt_clamp); |
e496187d | 1644 | } |
69842cba PB |
1645 | } |
1646 | ||
1647 | static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p) | |
1648 | { | |
0413d7f3 | 1649 | enum uclamp_id clamp_id; |
69842cba | 1650 | |
46609ce2 QY |
1651 | /* |
1652 | * Avoid any overhead until uclamp is actually used by the userspace. | |
1653 | * | |
1654 | * The condition is constructed such that a NOP is generated when | |
1655 | * sched_uclamp_used is disabled. | |
1656 | */ | |
1657 | if (!static_branch_unlikely(&sched_uclamp_used)) | |
1658 | return; | |
1659 | ||
69842cba PB |
1660 | if (unlikely(!p->sched_class->uclamp_enabled)) |
1661 | return; | |
1662 | ||
1663 | for_each_clamp_id(clamp_id) | |
1664 | uclamp_rq_inc_id(rq, p, clamp_id); | |
e496187d PB |
1665 | |
1666 | /* Reset clamp idle holding when there is one RUNNABLE task */ | |
1667 | if (rq->uclamp_flags & UCLAMP_FLAG_IDLE) | |
1668 | rq->uclamp_flags &= ~UCLAMP_FLAG_IDLE; | |
69842cba PB |
1669 | } |
1670 | ||
1671 | static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p) | |
1672 | { | |
0413d7f3 | 1673 | enum uclamp_id clamp_id; |
69842cba | 1674 | |
46609ce2 QY |
1675 | /* |
1676 | * Avoid any overhead until uclamp is actually used by the userspace. | |
1677 | * | |
1678 | * The condition is constructed such that a NOP is generated when | |
1679 | * sched_uclamp_used is disabled. | |
1680 | */ | |
1681 | if (!static_branch_unlikely(&sched_uclamp_used)) | |
1682 | return; | |
1683 | ||
69842cba PB |
1684 | if (unlikely(!p->sched_class->uclamp_enabled)) |
1685 | return; | |
1686 | ||
1687 | for_each_clamp_id(clamp_id) | |
1688 | uclamp_rq_dec_id(rq, p, clamp_id); | |
1689 | } | |
1690 | ||
ca4984a7 QP |
1691 | static inline void uclamp_rq_reinc_id(struct rq *rq, struct task_struct *p, |
1692 | enum uclamp_id clamp_id) | |
1693 | { | |
1694 | if (!p->uclamp[clamp_id].active) | |
1695 | return; | |
1696 | ||
1697 | uclamp_rq_dec_id(rq, p, clamp_id); | |
1698 | uclamp_rq_inc_id(rq, p, clamp_id); | |
1699 | ||
1700 | /* | |
1701 | * Make sure to clear the idle flag if we've transiently reached 0 | |
1702 | * active tasks on rq. | |
1703 | */ | |
1704 | if (clamp_id == UCLAMP_MAX && (rq->uclamp_flags & UCLAMP_FLAG_IDLE)) | |
1705 | rq->uclamp_flags &= ~UCLAMP_FLAG_IDLE; | |
1706 | } | |
1707 | ||
babbe170 | 1708 | static inline void |
0213b708 | 1709 | uclamp_update_active(struct task_struct *p) |
babbe170 | 1710 | { |
0213b708 | 1711 | enum uclamp_id clamp_id; |
babbe170 PB |
1712 | struct rq_flags rf; |
1713 | struct rq *rq; | |
1714 | ||
1715 | /* | |
1716 | * Lock the task and the rq where the task is (or was) queued. | |
1717 | * | |
1718 | * We might lock the (previous) rq of a !RUNNABLE task, but that's the | |
1719 | * price to pay to safely serialize util_{min,max} updates with | |
1720 | * enqueues, dequeues and migration operations. | |
1721 | * This is the same locking schema used by __set_cpus_allowed_ptr(). | |
1722 | */ | |
1723 | rq = task_rq_lock(p, &rf); | |
1724 | ||
1725 | /* | |
1726 | * Setting the clamp bucket is serialized by task_rq_lock(). | |
1727 | * If the task is not yet RUNNABLE and its task_struct is not | |
1728 | * affecting a valid clamp bucket, the next time it's enqueued, | |
1729 | * it will already see the updated clamp bucket value. | |
1730 | */ | |
ca4984a7 QP |
1731 | for_each_clamp_id(clamp_id) |
1732 | uclamp_rq_reinc_id(rq, p, clamp_id); | |
babbe170 PB |
1733 | |
1734 | task_rq_unlock(rq, p, &rf); | |
1735 | } | |
1736 | ||
e3b8b6a0 | 1737 | #ifdef CONFIG_UCLAMP_TASK_GROUP |
babbe170 | 1738 | static inline void |
0213b708 | 1739 | uclamp_update_active_tasks(struct cgroup_subsys_state *css) |
babbe170 PB |
1740 | { |
1741 | struct css_task_iter it; | |
1742 | struct task_struct *p; | |
babbe170 PB |
1743 | |
1744 | css_task_iter_start(css, 0, &it); | |
0213b708 QY |
1745 | while ((p = css_task_iter_next(&it))) |
1746 | uclamp_update_active(p); | |
babbe170 PB |
1747 | css_task_iter_end(&it); |
1748 | } | |
1749 | ||
7274a5c1 | 1750 | static void cpu_util_update_eff(struct cgroup_subsys_state *css); |
494dcdf4 Y |
1751 | #endif |
1752 | ||
1753 | #ifdef CONFIG_SYSCTL | |
1754 | #ifdef CONFIG_UCLAMP_TASK | |
1755 | #ifdef CONFIG_UCLAMP_TASK_GROUP | |
7274a5c1 PB |
1756 | static void uclamp_update_root_tg(void) |
1757 | { | |
1758 | struct task_group *tg = &root_task_group; | |
1759 | ||
1760 | uclamp_se_set(&tg->uclamp_req[UCLAMP_MIN], | |
1761 | sysctl_sched_uclamp_util_min, false); | |
1762 | uclamp_se_set(&tg->uclamp_req[UCLAMP_MAX], | |
1763 | sysctl_sched_uclamp_util_max, false); | |
1764 | ||
1765 | rcu_read_lock(); | |
1766 | cpu_util_update_eff(&root_task_group.css); | |
1767 | rcu_read_unlock(); | |
1768 | } | |
1769 | #else | |
1770 | static void uclamp_update_root_tg(void) { } | |
1771 | #endif | |
1772 | ||
494dcdf4 Y |
1773 | static void uclamp_sync_util_min_rt_default(void) |
1774 | { | |
1775 | struct task_struct *g, *p; | |
1776 | ||
1777 | /* | |
1778 | * copy_process() sysctl_uclamp | |
1779 | * uclamp_min_rt = X; | |
1780 | * write_lock(&tasklist_lock) read_lock(&tasklist_lock) | |
1781 | * // link thread smp_mb__after_spinlock() | |
1782 | * write_unlock(&tasklist_lock) read_unlock(&tasklist_lock); | |
1783 | * sched_post_fork() for_each_process_thread() | |
1784 | * __uclamp_sync_rt() __uclamp_sync_rt() | |
1785 | * | |
1786 | * Ensures that either sched_post_fork() will observe the new | |
1787 | * uclamp_min_rt or for_each_process_thread() will observe the new | |
1788 | * task. | |
1789 | */ | |
1790 | read_lock(&tasklist_lock); | |
1791 | smp_mb__after_spinlock(); | |
1792 | read_unlock(&tasklist_lock); | |
1793 | ||
1794 | rcu_read_lock(); | |
1795 | for_each_process_thread(g, p) | |
1796 | uclamp_update_util_min_rt_default(p); | |
1797 | rcu_read_unlock(); | |
1798 | } | |
1799 | ||
3267e015 | 1800 | static int sysctl_sched_uclamp_handler(struct ctl_table *table, int write, |
32927393 | 1801 | void *buffer, size_t *lenp, loff_t *ppos) |
e8f14172 | 1802 | { |
7274a5c1 | 1803 | bool update_root_tg = false; |
13685c4a | 1804 | int old_min, old_max, old_min_rt; |
e8f14172 PB |
1805 | int result; |
1806 | ||
2480c093 | 1807 | mutex_lock(&uclamp_mutex); |
e8f14172 PB |
1808 | old_min = sysctl_sched_uclamp_util_min; |
1809 | old_max = sysctl_sched_uclamp_util_max; | |
13685c4a | 1810 | old_min_rt = sysctl_sched_uclamp_util_min_rt_default; |
e8f14172 PB |
1811 | |
1812 | result = proc_dointvec(table, write, buffer, lenp, ppos); | |
1813 | if (result) | |
1814 | goto undo; | |
1815 | if (!write) | |
1816 | goto done; | |
1817 | ||
1818 | if (sysctl_sched_uclamp_util_min > sysctl_sched_uclamp_util_max || | |
13685c4a QY |
1819 | sysctl_sched_uclamp_util_max > SCHED_CAPACITY_SCALE || |
1820 | sysctl_sched_uclamp_util_min_rt_default > SCHED_CAPACITY_SCALE) { | |
1821 | ||
e8f14172 PB |
1822 | result = -EINVAL; |
1823 | goto undo; | |
1824 | } | |
1825 | ||
1826 | if (old_min != sysctl_sched_uclamp_util_min) { | |
1827 | uclamp_se_set(&uclamp_default[UCLAMP_MIN], | |
a509a7cd | 1828 | sysctl_sched_uclamp_util_min, false); |
7274a5c1 | 1829 | update_root_tg = true; |
e8f14172 PB |
1830 | } |
1831 | if (old_max != sysctl_sched_uclamp_util_max) { | |
1832 | uclamp_se_set(&uclamp_default[UCLAMP_MAX], | |
a509a7cd | 1833 | sysctl_sched_uclamp_util_max, false); |
7274a5c1 | 1834 | update_root_tg = true; |
e8f14172 PB |
1835 | } |
1836 | ||
46609ce2 QY |
1837 | if (update_root_tg) { |
1838 | static_branch_enable(&sched_uclamp_used); | |
7274a5c1 | 1839 | uclamp_update_root_tg(); |
46609ce2 | 1840 | } |
7274a5c1 | 1841 | |
13685c4a QY |
1842 | if (old_min_rt != sysctl_sched_uclamp_util_min_rt_default) { |
1843 | static_branch_enable(&sched_uclamp_used); | |
1844 | uclamp_sync_util_min_rt_default(); | |
1845 | } | |
7274a5c1 | 1846 | |
e8f14172 | 1847 | /* |
7274a5c1 PB |
1848 | * We update all RUNNABLE tasks only when task groups are in use. |
1849 | * Otherwise, keep it simple and do just a lazy update at each next | |
1850 | * task enqueue time. | |
e8f14172 | 1851 | */ |
7274a5c1 | 1852 | |
e8f14172 PB |
1853 | goto done; |
1854 | ||
1855 | undo: | |
1856 | sysctl_sched_uclamp_util_min = old_min; | |
1857 | sysctl_sched_uclamp_util_max = old_max; | |
13685c4a | 1858 | sysctl_sched_uclamp_util_min_rt_default = old_min_rt; |
e8f14172 | 1859 | done: |
2480c093 | 1860 | mutex_unlock(&uclamp_mutex); |
e8f14172 PB |
1861 | |
1862 | return result; | |
1863 | } | |
494dcdf4 Y |
1864 | #endif |
1865 | #endif | |
e8f14172 | 1866 | |
a509a7cd PB |
1867 | static int uclamp_validate(struct task_struct *p, |
1868 | const struct sched_attr *attr) | |
1869 | { | |
480a6ca2 DE |
1870 | int util_min = p->uclamp_req[UCLAMP_MIN].value; |
1871 | int util_max = p->uclamp_req[UCLAMP_MAX].value; | |
a509a7cd | 1872 | |
480a6ca2 DE |
1873 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN) { |
1874 | util_min = attr->sched_util_min; | |
a509a7cd | 1875 | |
480a6ca2 DE |
1876 | if (util_min + 1 > SCHED_CAPACITY_SCALE + 1) |
1877 | return -EINVAL; | |
1878 | } | |
1879 | ||
1880 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX) { | |
1881 | util_max = attr->sched_util_max; | |
1882 | ||
1883 | if (util_max + 1 > SCHED_CAPACITY_SCALE + 1) | |
1884 | return -EINVAL; | |
1885 | } | |
1886 | ||
1887 | if (util_min != -1 && util_max != -1 && util_min > util_max) | |
a509a7cd PB |
1888 | return -EINVAL; |
1889 | ||
e65855a5 QY |
1890 | /* |
1891 | * We have valid uclamp attributes; make sure uclamp is enabled. | |
1892 | * | |
1893 | * We need to do that here, because enabling static branches is a | |
1894 | * blocking operation which obviously cannot be done while holding | |
1895 | * scheduler locks. | |
1896 | */ | |
1897 | static_branch_enable(&sched_uclamp_used); | |
1898 | ||
a509a7cd PB |
1899 | return 0; |
1900 | } | |
1901 | ||
480a6ca2 DE |
1902 | static bool uclamp_reset(const struct sched_attr *attr, |
1903 | enum uclamp_id clamp_id, | |
1904 | struct uclamp_se *uc_se) | |
1905 | { | |
1906 | /* Reset on sched class change for a non user-defined clamp value. */ | |
1907 | if (likely(!(attr->sched_flags & SCHED_FLAG_UTIL_CLAMP)) && | |
1908 | !uc_se->user_defined) | |
1909 | return true; | |
1910 | ||
1911 | /* Reset on sched_util_{min,max} == -1. */ | |
1912 | if (clamp_id == UCLAMP_MIN && | |
1913 | attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN && | |
1914 | attr->sched_util_min == -1) { | |
1915 | return true; | |
1916 | } | |
1917 | ||
1918 | if (clamp_id == UCLAMP_MAX && | |
1919 | attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX && | |
1920 | attr->sched_util_max == -1) { | |
1921 | return true; | |
1922 | } | |
1923 | ||
1924 | return false; | |
1925 | } | |
1926 | ||
a509a7cd PB |
1927 | static void __setscheduler_uclamp(struct task_struct *p, |
1928 | const struct sched_attr *attr) | |
1929 | { | |
0413d7f3 | 1930 | enum uclamp_id clamp_id; |
1a00d999 | 1931 | |
1a00d999 PB |
1932 | for_each_clamp_id(clamp_id) { |
1933 | struct uclamp_se *uc_se = &p->uclamp_req[clamp_id]; | |
480a6ca2 | 1934 | unsigned int value; |
1a00d999 | 1935 | |
480a6ca2 | 1936 | if (!uclamp_reset(attr, clamp_id, uc_se)) |
1a00d999 PB |
1937 | continue; |
1938 | ||
13685c4a QY |
1939 | /* |
1940 | * RT by default have a 100% boost value that could be modified | |
1941 | * at runtime. | |
1942 | */ | |
1a00d999 | 1943 | if (unlikely(rt_task(p) && clamp_id == UCLAMP_MIN)) |
480a6ca2 | 1944 | value = sysctl_sched_uclamp_util_min_rt_default; |
13685c4a | 1945 | else |
480a6ca2 DE |
1946 | value = uclamp_none(clamp_id); |
1947 | ||
1948 | uclamp_se_set(uc_se, value, false); | |
1a00d999 | 1949 | |
1a00d999 PB |
1950 | } |
1951 | ||
a509a7cd PB |
1952 | if (likely(!(attr->sched_flags & SCHED_FLAG_UTIL_CLAMP))) |
1953 | return; | |
1954 | ||
480a6ca2 DE |
1955 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN && |
1956 | attr->sched_util_min != -1) { | |
a509a7cd PB |
1957 | uclamp_se_set(&p->uclamp_req[UCLAMP_MIN], |
1958 | attr->sched_util_min, true); | |
1959 | } | |
1960 | ||
480a6ca2 DE |
1961 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX && |
1962 | attr->sched_util_max != -1) { | |
a509a7cd PB |
1963 | uclamp_se_set(&p->uclamp_req[UCLAMP_MAX], |
1964 | attr->sched_util_max, true); | |
1965 | } | |
1966 | } | |
1967 | ||
e8f14172 PB |
1968 | static void uclamp_fork(struct task_struct *p) |
1969 | { | |
0413d7f3 | 1970 | enum uclamp_id clamp_id; |
e8f14172 | 1971 | |
13685c4a QY |
1972 | /* |
1973 | * We don't need to hold task_rq_lock() when updating p->uclamp_* here | |
1974 | * as the task is still at its early fork stages. | |
1975 | */ | |
e8f14172 PB |
1976 | for_each_clamp_id(clamp_id) |
1977 | p->uclamp[clamp_id].active = false; | |
a87498ac PB |
1978 | |
1979 | if (likely(!p->sched_reset_on_fork)) | |
1980 | return; | |
1981 | ||
1982 | for_each_clamp_id(clamp_id) { | |
eaf5a92e QP |
1983 | uclamp_se_set(&p->uclamp_req[clamp_id], |
1984 | uclamp_none(clamp_id), false); | |
a87498ac | 1985 | } |
e8f14172 PB |
1986 | } |
1987 | ||
13685c4a QY |
1988 | static void uclamp_post_fork(struct task_struct *p) |
1989 | { | |
1990 | uclamp_update_util_min_rt_default(p); | |
1991 | } | |
1992 | ||
d81ae8aa QY |
1993 | static void __init init_uclamp_rq(struct rq *rq) |
1994 | { | |
1995 | enum uclamp_id clamp_id; | |
1996 | struct uclamp_rq *uc_rq = rq->uclamp; | |
1997 | ||
1998 | for_each_clamp_id(clamp_id) { | |
1999 | uc_rq[clamp_id] = (struct uclamp_rq) { | |
2000 | .value = uclamp_none(clamp_id) | |
2001 | }; | |
2002 | } | |
2003 | ||
315c4f88 | 2004 | rq->uclamp_flags = UCLAMP_FLAG_IDLE; |
d81ae8aa QY |
2005 | } |
2006 | ||
69842cba PB |
2007 | static void __init init_uclamp(void) |
2008 | { | |
e8f14172 | 2009 | struct uclamp_se uc_max = {}; |
0413d7f3 | 2010 | enum uclamp_id clamp_id; |
69842cba PB |
2011 | int cpu; |
2012 | ||
d81ae8aa QY |
2013 | for_each_possible_cpu(cpu) |
2014 | init_uclamp_rq(cpu_rq(cpu)); | |
69842cba | 2015 | |
69842cba | 2016 | for_each_clamp_id(clamp_id) { |
e8f14172 | 2017 | uclamp_se_set(&init_task.uclamp_req[clamp_id], |
a509a7cd | 2018 | uclamp_none(clamp_id), false); |
69842cba | 2019 | } |
e8f14172 PB |
2020 | |
2021 | /* System defaults allow max clamp values for both indexes */ | |
a509a7cd | 2022 | uclamp_se_set(&uc_max, uclamp_none(UCLAMP_MAX), false); |
2480c093 | 2023 | for_each_clamp_id(clamp_id) { |
e8f14172 | 2024 | uclamp_default[clamp_id] = uc_max; |
2480c093 PB |
2025 | #ifdef CONFIG_UCLAMP_TASK_GROUP |
2026 | root_task_group.uclamp_req[clamp_id] = uc_max; | |
0b60ba2d | 2027 | root_task_group.uclamp[clamp_id] = uc_max; |
2480c093 PB |
2028 | #endif |
2029 | } | |
69842cba PB |
2030 | } |
2031 | ||
2032 | #else /* CONFIG_UCLAMP_TASK */ | |
2033 | static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p) { } | |
2034 | static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p) { } | |
a509a7cd PB |
2035 | static inline int uclamp_validate(struct task_struct *p, |
2036 | const struct sched_attr *attr) | |
2037 | { | |
2038 | return -EOPNOTSUPP; | |
2039 | } | |
2040 | static void __setscheduler_uclamp(struct task_struct *p, | |
2041 | const struct sched_attr *attr) { } | |
e8f14172 | 2042 | static inline void uclamp_fork(struct task_struct *p) { } |
13685c4a | 2043 | static inline void uclamp_post_fork(struct task_struct *p) { } |
69842cba PB |
2044 | static inline void init_uclamp(void) { } |
2045 | #endif /* CONFIG_UCLAMP_TASK */ | |
2046 | ||
a1dfb631 MT |
2047 | bool sched_task_on_rq(struct task_struct *p) |
2048 | { | |
2049 | return task_on_rq_queued(p); | |
2050 | } | |
2051 | ||
42a20f86 KC |
2052 | unsigned long get_wchan(struct task_struct *p) |
2053 | { | |
2054 | unsigned long ip = 0; | |
2055 | unsigned int state; | |
2056 | ||
2057 | if (!p || p == current) | |
2058 | return 0; | |
2059 | ||
2060 | /* Only get wchan if task is blocked and we can keep it that way. */ | |
2061 | raw_spin_lock_irq(&p->pi_lock); | |
2062 | state = READ_ONCE(p->__state); | |
2063 | smp_rmb(); /* see try_to_wake_up() */ | |
2064 | if (state != TASK_RUNNING && state != TASK_WAKING && !p->on_rq) | |
2065 | ip = __get_wchan(p); | |
2066 | raw_spin_unlock_irq(&p->pi_lock); | |
2067 | ||
2068 | return ip; | |
2069 | } | |
2070 | ||
1de64443 | 2071 | static inline void enqueue_task(struct rq *rq, struct task_struct *p, int flags) |
2087a1ad | 2072 | { |
0a67d1ee PZ |
2073 | if (!(flags & ENQUEUE_NOCLOCK)) |
2074 | update_rq_clock(rq); | |
2075 | ||
eb414681 | 2076 | if (!(flags & ENQUEUE_RESTORE)) { |
4e29fb70 | 2077 | sched_info_enqueue(rq, p); |
52b33d87 | 2078 | psi_enqueue(p, (flags & ENQUEUE_WAKEUP) && !(flags & ENQUEUE_MIGRATED)); |
eb414681 | 2079 | } |
0a67d1ee | 2080 | |
69842cba | 2081 | uclamp_rq_inc(rq, p); |
371fd7e7 | 2082 | p->sched_class->enqueue_task(rq, p, flags); |
8a311c74 PZ |
2083 | |
2084 | if (sched_core_enabled(rq)) | |
2085 | sched_core_enqueue(rq, p); | |
71f8bd46 IM |
2086 | } |
2087 | ||
1de64443 | 2088 | static inline void dequeue_task(struct rq *rq, struct task_struct *p, int flags) |
71f8bd46 | 2089 | { |
8a311c74 | 2090 | if (sched_core_enabled(rq)) |
4feee7d1 | 2091 | sched_core_dequeue(rq, p, flags); |
8a311c74 | 2092 | |
0a67d1ee PZ |
2093 | if (!(flags & DEQUEUE_NOCLOCK)) |
2094 | update_rq_clock(rq); | |
2095 | ||
eb414681 | 2096 | if (!(flags & DEQUEUE_SAVE)) { |
4e29fb70 | 2097 | sched_info_dequeue(rq, p); |
eb414681 JW |
2098 | psi_dequeue(p, flags & DEQUEUE_SLEEP); |
2099 | } | |
0a67d1ee | 2100 | |
69842cba | 2101 | uclamp_rq_dec(rq, p); |
371fd7e7 | 2102 | p->sched_class->dequeue_task(rq, p, flags); |
71f8bd46 IM |
2103 | } |
2104 | ||
029632fb | 2105 | void activate_task(struct rq *rq, struct task_struct *p, int flags) |
1e3c88bd | 2106 | { |
a53ce18c VG |
2107 | if (task_on_rq_migrating(p)) |
2108 | flags |= ENQUEUE_MIGRATED; | |
223baf9d MD |
2109 | if (flags & ENQUEUE_MIGRATED) |
2110 | sched_mm_cid_migrate_to(rq, p); | |
a53ce18c | 2111 | |
371fd7e7 | 2112 | enqueue_task(rq, p, flags); |
7dd77884 PZ |
2113 | |
2114 | p->on_rq = TASK_ON_RQ_QUEUED; | |
1e3c88bd PZ |
2115 | } |
2116 | ||
029632fb | 2117 | void deactivate_task(struct rq *rq, struct task_struct *p, int flags) |
1e3c88bd | 2118 | { |
7dd77884 PZ |
2119 | p->on_rq = (flags & DEQUEUE_SLEEP) ? 0 : TASK_ON_RQ_MIGRATING; |
2120 | ||
371fd7e7 | 2121 | dequeue_task(rq, p, flags); |
1e3c88bd PZ |
2122 | } |
2123 | ||
f558c2b8 | 2124 | static inline int __normal_prio(int policy, int rt_prio, int nice) |
14531189 | 2125 | { |
f558c2b8 PZ |
2126 | int prio; |
2127 | ||
2128 | if (dl_policy(policy)) | |
2129 | prio = MAX_DL_PRIO - 1; | |
2130 | else if (rt_policy(policy)) | |
2131 | prio = MAX_RT_PRIO - 1 - rt_prio; | |
2132 | else | |
2133 | prio = NICE_TO_PRIO(nice); | |
2134 | ||
2135 | return prio; | |
14531189 IM |
2136 | } |
2137 | ||
b29739f9 IM |
2138 | /* |
2139 | * Calculate the expected normal priority: i.e. priority | |
2140 | * without taking RT-inheritance into account. Might be | |
2141 | * boosted by interactivity modifiers. Changes upon fork, | |
2142 | * setprio syscalls, and whenever the interactivity | |
2143 | * estimator recalculates. | |
2144 | */ | |
36c8b586 | 2145 | static inline int normal_prio(struct task_struct *p) |
b29739f9 | 2146 | { |
f558c2b8 | 2147 | return __normal_prio(p->policy, p->rt_priority, PRIO_TO_NICE(p->static_prio)); |
b29739f9 IM |
2148 | } |
2149 | ||
2150 | /* | |
2151 | * Calculate the current priority, i.e. the priority | |
2152 | * taken into account by the scheduler. This value might | |
2153 | * be boosted by RT tasks, or might be boosted by | |
2154 | * interactivity modifiers. Will be RT if the task got | |
2155 | * RT-boosted. If not then it returns p->normal_prio. | |
2156 | */ | |
36c8b586 | 2157 | static int effective_prio(struct task_struct *p) |
b29739f9 IM |
2158 | { |
2159 | p->normal_prio = normal_prio(p); | |
2160 | /* | |
2161 | * If we are RT tasks or we were boosted to RT priority, | |
2162 | * keep the priority unchanged. Otherwise, update priority | |
2163 | * to the normal priority: | |
2164 | */ | |
2165 | if (!rt_prio(p->prio)) | |
2166 | return p->normal_prio; | |
2167 | return p->prio; | |
2168 | } | |
2169 | ||
1da177e4 LT |
2170 | /** |
2171 | * task_curr - is this task currently executing on a CPU? | |
2172 | * @p: the task in question. | |
e69f6186 YB |
2173 | * |
2174 | * Return: 1 if the task is currently executing. 0 otherwise. | |
1da177e4 | 2175 | */ |
36c8b586 | 2176 | inline int task_curr(const struct task_struct *p) |
1da177e4 LT |
2177 | { |
2178 | return cpu_curr(task_cpu(p)) == p; | |
2179 | } | |
2180 | ||
67dfa1b7 | 2181 | /* |
4c9a4bc8 PZ |
2182 | * switched_from, switched_to and prio_changed must _NOT_ drop rq->lock, |
2183 | * use the balance_callback list if you want balancing. | |
2184 | * | |
2185 | * this means any call to check_class_changed() must be followed by a call to | |
2186 | * balance_callback(). | |
67dfa1b7 | 2187 | */ |
cb469845 SR |
2188 | static inline void check_class_changed(struct rq *rq, struct task_struct *p, |
2189 | const struct sched_class *prev_class, | |
da7a735e | 2190 | int oldprio) |
cb469845 SR |
2191 | { |
2192 | if (prev_class != p->sched_class) { | |
2193 | if (prev_class->switched_from) | |
da7a735e | 2194 | prev_class->switched_from(rq, p); |
4c9a4bc8 | 2195 | |
da7a735e | 2196 | p->sched_class->switched_to(rq, p); |
2d3d891d | 2197 | } else if (oldprio != p->prio || dl_task(p)) |
da7a735e | 2198 | p->sched_class->prio_changed(rq, p, oldprio); |
cb469845 SR |
2199 | } |
2200 | ||
029632fb | 2201 | void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags) |
1e5a7405 | 2202 | { |
aa93cd53 | 2203 | if (p->sched_class == rq->curr->sched_class) |
1e5a7405 | 2204 | rq->curr->sched_class->check_preempt_curr(rq, p, flags); |
546a3fee | 2205 | else if (sched_class_above(p->sched_class, rq->curr->sched_class)) |
aa93cd53 | 2206 | resched_curr(rq); |
1e5a7405 PZ |
2207 | |
2208 | /* | |
2209 | * A queue event has occurred, and we're going to schedule. In | |
2210 | * this case, we can save a useless back to back clock update. | |
2211 | */ | |
da0c1e65 | 2212 | if (task_on_rq_queued(rq->curr) && test_tsk_need_resched(rq->curr)) |
adcc8da8 | 2213 | rq_clock_skip_update(rq); |
1e5a7405 PZ |
2214 | } |
2215 | ||
1da177e4 | 2216 | #ifdef CONFIG_SMP |
175f0e25 | 2217 | |
af449901 | 2218 | static void |
713a2e21 | 2219 | __do_set_cpus_allowed(struct task_struct *p, struct affinity_context *ctx); |
af449901 PZ |
2220 | |
2221 | static int __set_cpus_allowed_ptr(struct task_struct *p, | |
713a2e21 | 2222 | struct affinity_context *ctx); |
af449901 PZ |
2223 | |
2224 | static void migrate_disable_switch(struct rq *rq, struct task_struct *p) | |
2225 | { | |
713a2e21 WL |
2226 | struct affinity_context ac = { |
2227 | .new_mask = cpumask_of(rq->cpu), | |
2228 | .flags = SCA_MIGRATE_DISABLE, | |
2229 | }; | |
2230 | ||
af449901 PZ |
2231 | if (likely(!p->migration_disabled)) |
2232 | return; | |
2233 | ||
2234 | if (p->cpus_ptr != &p->cpus_mask) | |
2235 | return; | |
2236 | ||
2237 | /* | |
2238 | * Violates locking rules! see comment in __do_set_cpus_allowed(). | |
2239 | */ | |
713a2e21 | 2240 | __do_set_cpus_allowed(p, &ac); |
af449901 PZ |
2241 | } |
2242 | ||
2243 | void migrate_disable(void) | |
2244 | { | |
3015ef4b TG |
2245 | struct task_struct *p = current; |
2246 | ||
2247 | if (p->migration_disabled) { | |
2248 | p->migration_disabled++; | |
af449901 | 2249 | return; |
3015ef4b | 2250 | } |
af449901 | 2251 | |
3015ef4b TG |
2252 | preempt_disable(); |
2253 | this_rq()->nr_pinned++; | |
2254 | p->migration_disabled = 1; | |
2255 | preempt_enable(); | |
af449901 PZ |
2256 | } |
2257 | EXPORT_SYMBOL_GPL(migrate_disable); | |
2258 | ||
2259 | void migrate_enable(void) | |
2260 | { | |
2261 | struct task_struct *p = current; | |
713a2e21 WL |
2262 | struct affinity_context ac = { |
2263 | .new_mask = &p->cpus_mask, | |
2264 | .flags = SCA_MIGRATE_ENABLE, | |
2265 | }; | |
af449901 | 2266 | |
6d337eab PZ |
2267 | if (p->migration_disabled > 1) { |
2268 | p->migration_disabled--; | |
af449901 | 2269 | return; |
6d337eab | 2270 | } |
af449901 | 2271 | |
9d0df377 SAS |
2272 | if (WARN_ON_ONCE(!p->migration_disabled)) |
2273 | return; | |
2274 | ||
6d337eab PZ |
2275 | /* |
2276 | * Ensure stop_task runs either before or after this, and that | |
2277 | * __set_cpus_allowed_ptr(SCA_MIGRATE_ENABLE) doesn't schedule(). | |
2278 | */ | |
2279 | preempt_disable(); | |
2280 | if (p->cpus_ptr != &p->cpus_mask) | |
713a2e21 | 2281 | __set_cpus_allowed_ptr(p, &ac); |
6d337eab PZ |
2282 | /* |
2283 | * Mustn't clear migration_disabled() until cpus_ptr points back at the | |
2284 | * regular cpus_mask, otherwise things that race (eg. | |
2285 | * select_fallback_rq) get confused. | |
2286 | */ | |
af449901 | 2287 | barrier(); |
6d337eab | 2288 | p->migration_disabled = 0; |
3015ef4b | 2289 | this_rq()->nr_pinned--; |
6d337eab | 2290 | preempt_enable(); |
af449901 PZ |
2291 | } |
2292 | EXPORT_SYMBOL_GPL(migrate_enable); | |
2293 | ||
3015ef4b TG |
2294 | static inline bool rq_has_pinned_tasks(struct rq *rq) |
2295 | { | |
2296 | return rq->nr_pinned; | |
2297 | } | |
2298 | ||
175f0e25 | 2299 | /* |
bee98539 | 2300 | * Per-CPU kthreads are allowed to run on !active && online CPUs, see |
175f0e25 PZ |
2301 | * __set_cpus_allowed_ptr() and select_fallback_rq(). |
2302 | */ | |
2303 | static inline bool is_cpu_allowed(struct task_struct *p, int cpu) | |
2304 | { | |
5ba2ffba | 2305 | /* When not in the task's cpumask, no point in looking further. */ |
3bd37062 | 2306 | if (!cpumask_test_cpu(cpu, p->cpus_ptr)) |
175f0e25 PZ |
2307 | return false; |
2308 | ||
5ba2ffba PZ |
2309 | /* migrate_disabled() must be allowed to finish. */ |
2310 | if (is_migration_disabled(p)) | |
175f0e25 PZ |
2311 | return cpu_online(cpu); |
2312 | ||
5ba2ffba PZ |
2313 | /* Non kernel threads are not allowed during either online or offline. */ |
2314 | if (!(p->flags & PF_KTHREAD)) | |
9ae606bc | 2315 | return cpu_active(cpu) && task_cpu_possible(cpu, p); |
5ba2ffba PZ |
2316 | |
2317 | /* KTHREAD_IS_PER_CPU is always allowed. */ | |
2318 | if (kthread_is_per_cpu(p)) | |
2319 | return cpu_online(cpu); | |
2320 | ||
2321 | /* Regular kernel threads don't get to stay during offline. */ | |
b5c44773 | 2322 | if (cpu_dying(cpu)) |
5ba2ffba PZ |
2323 | return false; |
2324 | ||
2325 | /* But are allowed during online. */ | |
2326 | return cpu_online(cpu); | |
175f0e25 PZ |
2327 | } |
2328 | ||
5cc389bc PZ |
2329 | /* |
2330 | * This is how migration works: | |
2331 | * | |
2332 | * 1) we invoke migration_cpu_stop() on the target CPU using | |
2333 | * stop_one_cpu(). | |
2334 | * 2) stopper starts to run (implicitly forcing the migrated thread | |
2335 | * off the CPU) | |
2336 | * 3) it checks whether the migrated task is still in the wrong runqueue. | |
2337 | * 4) if it's in the wrong runqueue then the migration thread removes | |
2338 | * it and puts it into the right queue. | |
2339 | * 5) stopper completes and stop_one_cpu() returns and the migration | |
2340 | * is done. | |
2341 | */ | |
2342 | ||
2343 | /* | |
2344 | * move_queued_task - move a queued task to new rq. | |
2345 | * | |
2346 | * Returns (locked) new rq. Old rq's lock is released. | |
2347 | */ | |
8a8c69c3 PZ |
2348 | static struct rq *move_queued_task(struct rq *rq, struct rq_flags *rf, |
2349 | struct task_struct *p, int new_cpu) | |
5cc389bc | 2350 | { |
5cb9eaa3 | 2351 | lockdep_assert_rq_held(rq); |
5cc389bc | 2352 | |
58877d34 | 2353 | deactivate_task(rq, p, DEQUEUE_NOCLOCK); |
5cc389bc | 2354 | set_task_cpu(p, new_cpu); |
8a8c69c3 | 2355 | rq_unlock(rq, rf); |
5cc389bc PZ |
2356 | |
2357 | rq = cpu_rq(new_cpu); | |
2358 | ||
8a8c69c3 | 2359 | rq_lock(rq, rf); |
09348d75 | 2360 | WARN_ON_ONCE(task_cpu(p) != new_cpu); |
58877d34 | 2361 | activate_task(rq, p, 0); |
5cc389bc PZ |
2362 | check_preempt_curr(rq, p, 0); |
2363 | ||
2364 | return rq; | |
2365 | } | |
2366 | ||
2367 | struct migration_arg { | |
6d337eab PZ |
2368 | struct task_struct *task; |
2369 | int dest_cpu; | |
2370 | struct set_affinity_pending *pending; | |
2371 | }; | |
2372 | ||
50caf9c1 PZ |
2373 | /* |
2374 | * @refs: number of wait_for_completion() | |
2375 | * @stop_pending: is @stop_work in use | |
2376 | */ | |
6d337eab PZ |
2377 | struct set_affinity_pending { |
2378 | refcount_t refs; | |
9e81889c | 2379 | unsigned int stop_pending; |
6d337eab PZ |
2380 | struct completion done; |
2381 | struct cpu_stop_work stop_work; | |
2382 | struct migration_arg arg; | |
5cc389bc PZ |
2383 | }; |
2384 | ||
2385 | /* | |
d1ccc66d | 2386 | * Move (not current) task off this CPU, onto the destination CPU. We're doing |
5cc389bc PZ |
2387 | * this because either it can't run here any more (set_cpus_allowed() |
2388 | * away from this CPU, or CPU going down), or because we're | |
2389 | * attempting to rebalance this task on exec (sched_exec). | |
2390 | * | |
2391 | * So we race with normal scheduler movements, but that's OK, as long | |
2392 | * as the task is no longer on this CPU. | |
5cc389bc | 2393 | */ |
8a8c69c3 PZ |
2394 | static struct rq *__migrate_task(struct rq *rq, struct rq_flags *rf, |
2395 | struct task_struct *p, int dest_cpu) | |
5cc389bc | 2396 | { |
5cc389bc | 2397 | /* Affinity changed (again). */ |
175f0e25 | 2398 | if (!is_cpu_allowed(p, dest_cpu)) |
5e16bbc2 | 2399 | return rq; |
5cc389bc | 2400 | |
15ff991e | 2401 | update_rq_clock(rq); |
8a8c69c3 | 2402 | rq = move_queued_task(rq, rf, p, dest_cpu); |
5e16bbc2 PZ |
2403 | |
2404 | return rq; | |
5cc389bc PZ |
2405 | } |
2406 | ||
2407 | /* | |
2408 | * migration_cpu_stop - this will be executed by a highprio stopper thread | |
2409 | * and performs thread migration by bumping thread off CPU then | |
2410 | * 'pushing' onto another runqueue. | |
2411 | */ | |
2412 | static int migration_cpu_stop(void *data) | |
2413 | { | |
2414 | struct migration_arg *arg = data; | |
c20cf065 | 2415 | struct set_affinity_pending *pending = arg->pending; |
5e16bbc2 PZ |
2416 | struct task_struct *p = arg->task; |
2417 | struct rq *rq = this_rq(); | |
6d337eab | 2418 | bool complete = false; |
8a8c69c3 | 2419 | struct rq_flags rf; |
5cc389bc PZ |
2420 | |
2421 | /* | |
d1ccc66d IM |
2422 | * The original target CPU might have gone down and we might |
2423 | * be on another CPU but it doesn't matter. | |
5cc389bc | 2424 | */ |
6d337eab | 2425 | local_irq_save(rf.flags); |
5cc389bc PZ |
2426 | /* |
2427 | * We need to explicitly wake pending tasks before running | |
3bd37062 | 2428 | * __migrate_task() such that we will not miss enforcing cpus_ptr |
5cc389bc PZ |
2429 | * during wakeups, see set_cpus_allowed_ptr()'s TASK_WAKING test. |
2430 | */ | |
16bf5a5e | 2431 | flush_smp_call_function_queue(); |
5e16bbc2 PZ |
2432 | |
2433 | raw_spin_lock(&p->pi_lock); | |
8a8c69c3 | 2434 | rq_lock(rq, &rf); |
6d337eab | 2435 | |
e140749c VS |
2436 | /* |
2437 | * If we were passed a pending, then ->stop_pending was set, thus | |
2438 | * p->migration_pending must have remained stable. | |
2439 | */ | |
2440 | WARN_ON_ONCE(pending && pending != p->migration_pending); | |
2441 | ||
5e16bbc2 PZ |
2442 | /* |
2443 | * If task_rq(p) != rq, it cannot be migrated here, because we're | |
2444 | * holding rq->lock, if p->on_rq == 0 it cannot get enqueued because | |
2445 | * we're holding p->pi_lock. | |
2446 | */ | |
bf89a304 | 2447 | if (task_rq(p) == rq) { |
6d337eab PZ |
2448 | if (is_migration_disabled(p)) |
2449 | goto out; | |
2450 | ||
2451 | if (pending) { | |
e140749c | 2452 | p->migration_pending = NULL; |
6d337eab | 2453 | complete = true; |
6d337eab | 2454 | |
3f1bc119 PZ |
2455 | if (cpumask_test_cpu(task_cpu(p), &p->cpus_mask)) |
2456 | goto out; | |
3f1bc119 | 2457 | } |
6d337eab | 2458 | |
bf89a304 | 2459 | if (task_on_rq_queued(p)) |
475ea6c6 | 2460 | rq = __migrate_task(rq, &rf, p, arg->dest_cpu); |
bf89a304 | 2461 | else |
475ea6c6 | 2462 | p->wake_cpu = arg->dest_cpu; |
6d337eab | 2463 | |
3f1bc119 PZ |
2464 | /* |
2465 | * XXX __migrate_task() can fail, at which point we might end | |
2466 | * up running on a dodgy CPU, AFAICT this can only happen | |
2467 | * during CPU hotplug, at which point we'll get pushed out | |
2468 | * anyway, so it's probably not a big deal. | |
2469 | */ | |
2470 | ||
c20cf065 | 2471 | } else if (pending) { |
6d337eab PZ |
2472 | /* |
2473 | * This happens when we get migrated between migrate_enable()'s | |
2474 | * preempt_enable() and scheduling the stopper task. At that | |
2475 | * point we're a regular task again and not current anymore. | |
2476 | * | |
2477 | * A !PREEMPT kernel has a giant hole here, which makes it far | |
2478 | * more likely. | |
2479 | */ | |
2480 | ||
d707faa6 VS |
2481 | /* |
2482 | * The task moved before the stopper got to run. We're holding | |
2483 | * ->pi_lock, so the allowed mask is stable - if it got | |
2484 | * somewhere allowed, we're done. | |
2485 | */ | |
c20cf065 | 2486 | if (cpumask_test_cpu(task_cpu(p), p->cpus_ptr)) { |
e140749c | 2487 | p->migration_pending = NULL; |
d707faa6 VS |
2488 | complete = true; |
2489 | goto out; | |
2490 | } | |
2491 | ||
6d337eab PZ |
2492 | /* |
2493 | * When migrate_enable() hits a rq mis-match we can't reliably | |
2494 | * determine is_migration_disabled() and so have to chase after | |
2495 | * it. | |
2496 | */ | |
9e81889c | 2497 | WARN_ON_ONCE(!pending->stop_pending); |
6d337eab PZ |
2498 | task_rq_unlock(rq, p, &rf); |
2499 | stop_one_cpu_nowait(task_cpu(p), migration_cpu_stop, | |
2500 | &pending->arg, &pending->stop_work); | |
2501 | return 0; | |
bf89a304 | 2502 | } |
6d337eab | 2503 | out: |
9e81889c PZ |
2504 | if (pending) |
2505 | pending->stop_pending = false; | |
6d337eab PZ |
2506 | task_rq_unlock(rq, p, &rf); |
2507 | ||
2508 | if (complete) | |
2509 | complete_all(&pending->done); | |
2510 | ||
5cc389bc PZ |
2511 | return 0; |
2512 | } | |
2513 | ||
a7c81556 PZ |
2514 | int push_cpu_stop(void *arg) |
2515 | { | |
2516 | struct rq *lowest_rq = NULL, *rq = this_rq(); | |
2517 | struct task_struct *p = arg; | |
2518 | ||
2519 | raw_spin_lock_irq(&p->pi_lock); | |
5cb9eaa3 | 2520 | raw_spin_rq_lock(rq); |
a7c81556 PZ |
2521 | |
2522 | if (task_rq(p) != rq) | |
2523 | goto out_unlock; | |
2524 | ||
2525 | if (is_migration_disabled(p)) { | |
2526 | p->migration_flags |= MDF_PUSH; | |
2527 | goto out_unlock; | |
2528 | } | |
2529 | ||
2530 | p->migration_flags &= ~MDF_PUSH; | |
2531 | ||
2532 | if (p->sched_class->find_lock_rq) | |
2533 | lowest_rq = p->sched_class->find_lock_rq(p, rq); | |
5e16bbc2 | 2534 | |
a7c81556 PZ |
2535 | if (!lowest_rq) |
2536 | goto out_unlock; | |
2537 | ||
2538 | // XXX validate p is still the highest prio task | |
2539 | if (task_rq(p) == rq) { | |
2540 | deactivate_task(rq, p, 0); | |
2541 | set_task_cpu(p, lowest_rq->cpu); | |
2542 | activate_task(lowest_rq, p, 0); | |
2543 | resched_curr(lowest_rq); | |
2544 | } | |
2545 | ||
2546 | double_unlock_balance(rq, lowest_rq); | |
2547 | ||
2548 | out_unlock: | |
2549 | rq->push_busy = false; | |
5cb9eaa3 | 2550 | raw_spin_rq_unlock(rq); |
a7c81556 PZ |
2551 | raw_spin_unlock_irq(&p->pi_lock); |
2552 | ||
2553 | put_task_struct(p); | |
5cc389bc PZ |
2554 | return 0; |
2555 | } | |
2556 | ||
c5b28038 PZ |
2557 | /* |
2558 | * sched_class::set_cpus_allowed must do the below, but is not required to | |
2559 | * actually call this function. | |
2560 | */ | |
713a2e21 | 2561 | void set_cpus_allowed_common(struct task_struct *p, struct affinity_context *ctx) |
5cc389bc | 2562 | { |
713a2e21 WL |
2563 | if (ctx->flags & (SCA_MIGRATE_ENABLE | SCA_MIGRATE_DISABLE)) { |
2564 | p->cpus_ptr = ctx->new_mask; | |
af449901 PZ |
2565 | return; |
2566 | } | |
2567 | ||
713a2e21 WL |
2568 | cpumask_copy(&p->cpus_mask, ctx->new_mask); |
2569 | p->nr_cpus_allowed = cpumask_weight(ctx->new_mask); | |
8f9ea86f WL |
2570 | |
2571 | /* | |
2572 | * Swap in a new user_cpus_ptr if SCA_USER flag set | |
2573 | */ | |
2574 | if (ctx->flags & SCA_USER) | |
2575 | swap(p->user_cpus_ptr, ctx->user_mask); | |
5cc389bc PZ |
2576 | } |
2577 | ||
9cfc3e18 | 2578 | static void |
713a2e21 | 2579 | __do_set_cpus_allowed(struct task_struct *p, struct affinity_context *ctx) |
c5b28038 | 2580 | { |
6c37067e PZ |
2581 | struct rq *rq = task_rq(p); |
2582 | bool queued, running; | |
2583 | ||
af449901 PZ |
2584 | /* |
2585 | * This here violates the locking rules for affinity, since we're only | |
2586 | * supposed to change these variables while holding both rq->lock and | |
2587 | * p->pi_lock. | |
2588 | * | |
2589 | * HOWEVER, it magically works, because ttwu() is the only code that | |
2590 | * accesses these variables under p->pi_lock and only does so after | |
2591 | * smp_cond_load_acquire(&p->on_cpu, !VAL), and we're in __schedule() | |
2592 | * before finish_task(). | |
2593 | * | |
2594 | * XXX do further audits, this smells like something putrid. | |
2595 | */ | |
713a2e21 | 2596 | if (ctx->flags & SCA_MIGRATE_DISABLE) |
af449901 PZ |
2597 | SCHED_WARN_ON(!p->on_cpu); |
2598 | else | |
2599 | lockdep_assert_held(&p->pi_lock); | |
6c37067e PZ |
2600 | |
2601 | queued = task_on_rq_queued(p); | |
2602 | running = task_current(rq, p); | |
2603 | ||
2604 | if (queued) { | |
2605 | /* | |
2606 | * Because __kthread_bind() calls this on blocked tasks without | |
2607 | * holding rq->lock. | |
2608 | */ | |
5cb9eaa3 | 2609 | lockdep_assert_rq_held(rq); |
7a57f32a | 2610 | dequeue_task(rq, p, DEQUEUE_SAVE | DEQUEUE_NOCLOCK); |
6c37067e PZ |
2611 | } |
2612 | if (running) | |
2613 | put_prev_task(rq, p); | |
2614 | ||
713a2e21 | 2615 | p->sched_class->set_cpus_allowed(p, ctx); |
6c37067e | 2616 | |
6c37067e | 2617 | if (queued) |
7134b3e9 | 2618 | enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK); |
a399d233 | 2619 | if (running) |
03b7fad1 | 2620 | set_next_task(rq, p); |
c5b28038 PZ |
2621 | } |
2622 | ||
851a723e WL |
2623 | /* |
2624 | * Used for kthread_bind() and select_fallback_rq(), in both cases the user | |
2625 | * affinity (if any) should be destroyed too. | |
2626 | */ | |
9cfc3e18 PZ |
2627 | void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask) |
2628 | { | |
713a2e21 WL |
2629 | struct affinity_context ac = { |
2630 | .new_mask = new_mask, | |
851a723e WL |
2631 | .user_mask = NULL, |
2632 | .flags = SCA_USER, /* clear the user requested mask */ | |
713a2e21 | 2633 | }; |
9a5418bc WL |
2634 | union cpumask_rcuhead { |
2635 | cpumask_t cpumask; | |
2636 | struct rcu_head rcu; | |
2637 | }; | |
713a2e21 WL |
2638 | |
2639 | __do_set_cpus_allowed(p, &ac); | |
9a5418bc WL |
2640 | |
2641 | /* | |
2642 | * Because this is called with p->pi_lock held, it is not possible | |
2643 | * to use kfree() here (when PREEMPT_RT=y), therefore punt to using | |
2644 | * kfree_rcu(). | |
2645 | */ | |
2646 | kfree_rcu((union cpumask_rcuhead *)ac.user_mask, rcu); | |
2647 | } | |
2648 | ||
2649 | static cpumask_t *alloc_user_cpus_ptr(int node) | |
2650 | { | |
2651 | /* | |
2652 | * See do_set_cpus_allowed() above for the rcu_head usage. | |
2653 | */ | |
2654 | int size = max_t(int, cpumask_size(), sizeof(struct rcu_head)); | |
2655 | ||
2656 | return kmalloc_node(size, GFP_KERNEL, node); | |
9cfc3e18 PZ |
2657 | } |
2658 | ||
b90ca8ba WD |
2659 | int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src, |
2660 | int node) | |
2661 | { | |
87ca4f9e | 2662 | cpumask_t *user_mask; |
8f9ea86f WL |
2663 | unsigned long flags; |
2664 | ||
87ca4f9e WL |
2665 | /* |
2666 | * Always clear dst->user_cpus_ptr first as their user_cpus_ptr's | |
2667 | * may differ by now due to racing. | |
2668 | */ | |
2669 | dst->user_cpus_ptr = NULL; | |
2670 | ||
2671 | /* | |
2672 | * This check is racy and losing the race is a valid situation. | |
2673 | * It is not worth the extra overhead of taking the pi_lock on | |
2674 | * every fork/clone. | |
2675 | */ | |
2676 | if (data_race(!src->user_cpus_ptr)) | |
b90ca8ba WD |
2677 | return 0; |
2678 | ||
9a5418bc | 2679 | user_mask = alloc_user_cpus_ptr(node); |
87ca4f9e | 2680 | if (!user_mask) |
b90ca8ba WD |
2681 | return -ENOMEM; |
2682 | ||
87ca4f9e WL |
2683 | /* |
2684 | * Use pi_lock to protect content of user_cpus_ptr | |
2685 | * | |
2686 | * Though unlikely, user_cpus_ptr can be reset to NULL by a concurrent | |
2687 | * do_set_cpus_allowed(). | |
2688 | */ | |
8f9ea86f | 2689 | raw_spin_lock_irqsave(&src->pi_lock, flags); |
87ca4f9e WL |
2690 | if (src->user_cpus_ptr) { |
2691 | swap(dst->user_cpus_ptr, user_mask); | |
2692 | cpumask_copy(dst->user_cpus_ptr, src->user_cpus_ptr); | |
2693 | } | |
8f9ea86f | 2694 | raw_spin_unlock_irqrestore(&src->pi_lock, flags); |
87ca4f9e WL |
2695 | |
2696 | if (unlikely(user_mask)) | |
2697 | kfree(user_mask); | |
2698 | ||
b90ca8ba WD |
2699 | return 0; |
2700 | } | |
2701 | ||
07ec77a1 WD |
2702 | static inline struct cpumask *clear_user_cpus_ptr(struct task_struct *p) |
2703 | { | |
2704 | struct cpumask *user_mask = NULL; | |
2705 | ||
2706 | swap(p->user_cpus_ptr, user_mask); | |
2707 | ||
2708 | return user_mask; | |
2709 | } | |
2710 | ||
b90ca8ba WD |
2711 | void release_user_cpus_ptr(struct task_struct *p) |
2712 | { | |
07ec77a1 | 2713 | kfree(clear_user_cpus_ptr(p)); |
b90ca8ba WD |
2714 | } |
2715 | ||
6d337eab | 2716 | /* |
c777d847 VS |
2717 | * This function is wildly self concurrent; here be dragons. |
2718 | * | |
2719 | * | |
2720 | * When given a valid mask, __set_cpus_allowed_ptr() must block until the | |
2721 | * designated task is enqueued on an allowed CPU. If that task is currently | |
2722 | * running, we have to kick it out using the CPU stopper. | |
2723 | * | |
2724 | * Migrate-Disable comes along and tramples all over our nice sandcastle. | |
2725 | * Consider: | |
2726 | * | |
2727 | * Initial conditions: P0->cpus_mask = [0, 1] | |
2728 | * | |
2729 | * P0@CPU0 P1 | |
2730 | * | |
2731 | * migrate_disable(); | |
2732 | * <preempted> | |
2733 | * set_cpus_allowed_ptr(P0, [1]); | |
2734 | * | |
2735 | * P1 *cannot* return from this set_cpus_allowed_ptr() call until P0 executes | |
2736 | * its outermost migrate_enable() (i.e. it exits its Migrate-Disable region). | |
2737 | * This means we need the following scheme: | |
2738 | * | |
2739 | * P0@CPU0 P1 | |
2740 | * | |
2741 | * migrate_disable(); | |
2742 | * <preempted> | |
2743 | * set_cpus_allowed_ptr(P0, [1]); | |
2744 | * <blocks> | |
2745 | * <resumes> | |
2746 | * migrate_enable(); | |
2747 | * __set_cpus_allowed_ptr(); | |
2748 | * <wakes local stopper> | |
2749 | * `--> <woken on migration completion> | |
2750 | * | |
2751 | * Now the fun stuff: there may be several P1-like tasks, i.e. multiple | |
2752 | * concurrent set_cpus_allowed_ptr(P0, [*]) calls. CPU affinity changes of any | |
2753 | * task p are serialized by p->pi_lock, which we can leverage: the one that | |
2754 | * should come into effect at the end of the Migrate-Disable region is the last | |
2755 | * one. This means we only need to track a single cpumask (i.e. p->cpus_mask), | |
2756 | * but we still need to properly signal those waiting tasks at the appropriate | |
2757 | * moment. | |
2758 | * | |
2759 | * This is implemented using struct set_affinity_pending. The first | |
2760 | * __set_cpus_allowed_ptr() caller within a given Migrate-Disable region will | |
2761 | * setup an instance of that struct and install it on the targeted task_struct. | |
2762 | * Any and all further callers will reuse that instance. Those then wait for | |
2763 | * a completion signaled at the tail of the CPU stopper callback (1), triggered | |
2764 | * on the end of the Migrate-Disable region (i.e. outermost migrate_enable()). | |
2765 | * | |
2766 | * | |
2767 | * (1) In the cases covered above. There is one more where the completion is | |
2768 | * signaled within affine_move_task() itself: when a subsequent affinity request | |
e140749c VS |
2769 | * occurs after the stopper bailed out due to the targeted task still being |
2770 | * Migrate-Disable. Consider: | |
c777d847 VS |
2771 | * |
2772 | * Initial conditions: P0->cpus_mask = [0, 1] | |
2773 | * | |
e140749c VS |
2774 | * CPU0 P1 P2 |
2775 | * <P0> | |
2776 | * migrate_disable(); | |
2777 | * <preempted> | |
c777d847 VS |
2778 | * set_cpus_allowed_ptr(P0, [1]); |
2779 | * <blocks> | |
e140749c VS |
2780 | * <migration/0> |
2781 | * migration_cpu_stop() | |
2782 | * is_migration_disabled() | |
2783 | * <bails> | |
c777d847 VS |
2784 | * set_cpus_allowed_ptr(P0, [0, 1]); |
2785 | * <signal completion> | |
2786 | * <awakes> | |
2787 | * | |
2788 | * Note that the above is safe vs a concurrent migrate_enable(), as any | |
2789 | * pending affinity completion is preceded by an uninstallation of | |
2790 | * p->migration_pending done with p->pi_lock held. | |
6d337eab PZ |
2791 | */ |
2792 | static int affine_move_task(struct rq *rq, struct task_struct *p, struct rq_flags *rf, | |
2793 | int dest_cpu, unsigned int flags) | |
5584e8ac WL |
2794 | __releases(rq->lock) |
2795 | __releases(p->pi_lock) | |
6d337eab PZ |
2796 | { |
2797 | struct set_affinity_pending my_pending = { }, *pending = NULL; | |
9e81889c | 2798 | bool stop_pending, complete = false; |
6d337eab PZ |
2799 | |
2800 | /* Can the task run on the task's current CPU? If so, we're done */ | |
2801 | if (cpumask_test_cpu(task_cpu(p), &p->cpus_mask)) { | |
a7c81556 PZ |
2802 | struct task_struct *push_task = NULL; |
2803 | ||
2804 | if ((flags & SCA_MIGRATE_ENABLE) && | |
2805 | (p->migration_flags & MDF_PUSH) && !rq->push_busy) { | |
2806 | rq->push_busy = true; | |
2807 | push_task = get_task_struct(p); | |
2808 | } | |
2809 | ||
50caf9c1 PZ |
2810 | /* |
2811 | * If there are pending waiters, but no pending stop_work, | |
2812 | * then complete now. | |
2813 | */ | |
6d337eab | 2814 | pending = p->migration_pending; |
50caf9c1 | 2815 | if (pending && !pending->stop_pending) { |
6d337eab PZ |
2816 | p->migration_pending = NULL; |
2817 | complete = true; | |
2818 | } | |
50caf9c1 | 2819 | |
6d337eab PZ |
2820 | task_rq_unlock(rq, p, rf); |
2821 | ||
a7c81556 PZ |
2822 | if (push_task) { |
2823 | stop_one_cpu_nowait(rq->cpu, push_cpu_stop, | |
2824 | p, &rq->push_work); | |
2825 | } | |
2826 | ||
6d337eab | 2827 | if (complete) |
50caf9c1 | 2828 | complete_all(&pending->done); |
6d337eab PZ |
2829 | |
2830 | return 0; | |
2831 | } | |
2832 | ||
2833 | if (!(flags & SCA_MIGRATE_ENABLE)) { | |
2834 | /* serialized by p->pi_lock */ | |
2835 | if (!p->migration_pending) { | |
c777d847 | 2836 | /* Install the request */ |
6d337eab PZ |
2837 | refcount_set(&my_pending.refs, 1); |
2838 | init_completion(&my_pending.done); | |
8a6edb52 PZ |
2839 | my_pending.arg = (struct migration_arg) { |
2840 | .task = p, | |
475ea6c6 | 2841 | .dest_cpu = dest_cpu, |
8a6edb52 PZ |
2842 | .pending = &my_pending, |
2843 | }; | |
2844 | ||
6d337eab PZ |
2845 | p->migration_pending = &my_pending; |
2846 | } else { | |
2847 | pending = p->migration_pending; | |
2848 | refcount_inc(&pending->refs); | |
475ea6c6 VS |
2849 | /* |
2850 | * Affinity has changed, but we've already installed a | |
2851 | * pending. migration_cpu_stop() *must* see this, else | |
2852 | * we risk a completion of the pending despite having a | |
2853 | * task on a disallowed CPU. | |
2854 | * | |
2855 | * Serialized by p->pi_lock, so this is safe. | |
2856 | */ | |
2857 | pending->arg.dest_cpu = dest_cpu; | |
6d337eab PZ |
2858 | } |
2859 | } | |
2860 | pending = p->migration_pending; | |
2861 | /* | |
2862 | * - !MIGRATE_ENABLE: | |
2863 | * we'll have installed a pending if there wasn't one already. | |
2864 | * | |
2865 | * - MIGRATE_ENABLE: | |
2866 | * we're here because the current CPU isn't matching anymore, | |
2867 | * the only way that can happen is because of a concurrent | |
2868 | * set_cpus_allowed_ptr() call, which should then still be | |
2869 | * pending completion. | |
2870 | * | |
2871 | * Either way, we really should have a @pending here. | |
2872 | */ | |
2873 | if (WARN_ON_ONCE(!pending)) { | |
2874 | task_rq_unlock(rq, p, rf); | |
2875 | return -EINVAL; | |
2876 | } | |
2877 | ||
0b9d46fc | 2878 | if (task_on_cpu(rq, p) || READ_ONCE(p->__state) == TASK_WAKING) { |
c777d847 | 2879 | /* |
58b1a450 PZ |
2880 | * MIGRATE_ENABLE gets here because 'p == current', but for |
2881 | * anything else we cannot do is_migration_disabled(), punt | |
2882 | * and have the stopper function handle it all race-free. | |
c777d847 | 2883 | */ |
9e81889c PZ |
2884 | stop_pending = pending->stop_pending; |
2885 | if (!stop_pending) | |
2886 | pending->stop_pending = true; | |
58b1a450 | 2887 | |
58b1a450 PZ |
2888 | if (flags & SCA_MIGRATE_ENABLE) |
2889 | p->migration_flags &= ~MDF_PUSH; | |
50caf9c1 | 2890 | |
6d337eab | 2891 | task_rq_unlock(rq, p, rf); |
8a6edb52 | 2892 | |
9e81889c PZ |
2893 | if (!stop_pending) { |
2894 | stop_one_cpu_nowait(cpu_of(rq), migration_cpu_stop, | |
2895 | &pending->arg, &pending->stop_work); | |
2896 | } | |
6d337eab | 2897 | |
58b1a450 PZ |
2898 | if (flags & SCA_MIGRATE_ENABLE) |
2899 | return 0; | |
6d337eab PZ |
2900 | } else { |
2901 | ||
2902 | if (!is_migration_disabled(p)) { | |
2903 | if (task_on_rq_queued(p)) | |
2904 | rq = move_queued_task(rq, rf, p, dest_cpu); | |
2905 | ||
50caf9c1 PZ |
2906 | if (!pending->stop_pending) { |
2907 | p->migration_pending = NULL; | |
2908 | complete = true; | |
2909 | } | |
6d337eab PZ |
2910 | } |
2911 | task_rq_unlock(rq, p, rf); | |
2912 | ||
6d337eab PZ |
2913 | if (complete) |
2914 | complete_all(&pending->done); | |
2915 | } | |
2916 | ||
2917 | wait_for_completion(&pending->done); | |
2918 | ||
2919 | if (refcount_dec_and_test(&pending->refs)) | |
50caf9c1 | 2920 | wake_up_var(&pending->refs); /* No UaF, just an address */ |
6d337eab | 2921 | |
c777d847 VS |
2922 | /* |
2923 | * Block the original owner of &pending until all subsequent callers | |
2924 | * have seen the completion and decremented the refcount | |
2925 | */ | |
6d337eab PZ |
2926 | wait_var_event(&my_pending.refs, !refcount_read(&my_pending.refs)); |
2927 | ||
50caf9c1 PZ |
2928 | /* ARGH */ |
2929 | WARN_ON_ONCE(my_pending.stop_pending); | |
2930 | ||
6d337eab PZ |
2931 | return 0; |
2932 | } | |
2933 | ||
5cc389bc | 2934 | /* |
07ec77a1 | 2935 | * Called with both p->pi_lock and rq->lock held; drops both before returning. |
5cc389bc | 2936 | */ |
07ec77a1 | 2937 | static int __set_cpus_allowed_ptr_locked(struct task_struct *p, |
713a2e21 | 2938 | struct affinity_context *ctx, |
07ec77a1 WD |
2939 | struct rq *rq, |
2940 | struct rq_flags *rf) | |
2941 | __releases(rq->lock) | |
2942 | __releases(p->pi_lock) | |
5cc389bc | 2943 | { |
234a503e | 2944 | const struct cpumask *cpu_allowed_mask = task_cpu_possible_mask(p); |
e9d867a6 | 2945 | const struct cpumask *cpu_valid_mask = cpu_active_mask; |
234a503e | 2946 | bool kthread = p->flags & PF_KTHREAD; |
5cc389bc PZ |
2947 | unsigned int dest_cpu; |
2948 | int ret = 0; | |
2949 | ||
a499c3ea | 2950 | update_rq_clock(rq); |
5cc389bc | 2951 | |
234a503e | 2952 | if (kthread || is_migration_disabled(p)) { |
e9d867a6 | 2953 | /* |
741ba80f PZ |
2954 | * Kernel threads are allowed on online && !active CPUs, |
2955 | * however, during cpu-hot-unplug, even these might get pushed | |
2956 | * away if not KTHREAD_IS_PER_CPU. | |
af449901 PZ |
2957 | * |
2958 | * Specifically, migration_disabled() tasks must not fail the | |
2959 | * cpumask_any_and_distribute() pick below, esp. so on | |
2960 | * SCA_MIGRATE_ENABLE, otherwise we'll not call | |
2961 | * set_cpus_allowed_common() and actually reset p->cpus_ptr. | |
e9d867a6 PZI |
2962 | */ |
2963 | cpu_valid_mask = cpu_online_mask; | |
2964 | } | |
2965 | ||
713a2e21 | 2966 | if (!kthread && !cpumask_subset(ctx->new_mask, cpu_allowed_mask)) { |
234a503e WD |
2967 | ret = -EINVAL; |
2968 | goto out; | |
2969 | } | |
2970 | ||
25834c73 PZ |
2971 | /* |
2972 | * Must re-check here, to close a race against __kthread_bind(), | |
2973 | * sched_setaffinity() is not guaranteed to observe the flag. | |
2974 | */ | |
713a2e21 | 2975 | if ((ctx->flags & SCA_CHECK) && (p->flags & PF_NO_SETAFFINITY)) { |
25834c73 PZ |
2976 | ret = -EINVAL; |
2977 | goto out; | |
2978 | } | |
2979 | ||
713a2e21 | 2980 | if (!(ctx->flags & SCA_MIGRATE_ENABLE)) { |
df14b7f9 WL |
2981 | if (cpumask_equal(&p->cpus_mask, ctx->new_mask)) { |
2982 | if (ctx->flags & SCA_USER) | |
2983 | swap(p->user_cpus_ptr, ctx->user_mask); | |
885b3ba4 | 2984 | goto out; |
df14b7f9 | 2985 | } |
885b3ba4 VS |
2986 | |
2987 | if (WARN_ON_ONCE(p == current && | |
2988 | is_migration_disabled(p) && | |
713a2e21 | 2989 | !cpumask_test_cpu(task_cpu(p), ctx->new_mask))) { |
885b3ba4 VS |
2990 | ret = -EBUSY; |
2991 | goto out; | |
2992 | } | |
2993 | } | |
5cc389bc | 2994 | |
46a87b38 PT |
2995 | /* |
2996 | * Picking a ~random cpu helps in cases where we are changing affinity | |
2997 | * for groups of tasks (ie. cpuset), so that load balancing is not | |
2998 | * immediately required to distribute the tasks within their new mask. | |
2999 | */ | |
713a2e21 | 3000 | dest_cpu = cpumask_any_and_distribute(cpu_valid_mask, ctx->new_mask); |
714e501e | 3001 | if (dest_cpu >= nr_cpu_ids) { |
5cc389bc PZ |
3002 | ret = -EINVAL; |
3003 | goto out; | |
3004 | } | |
3005 | ||
713a2e21 | 3006 | __do_set_cpus_allowed(p, ctx); |
07ec77a1 | 3007 | |
8f9ea86f | 3008 | return affine_move_task(rq, p, rf, dest_cpu, ctx->flags); |
5cc389bc | 3009 | |
5cc389bc | 3010 | out: |
07ec77a1 | 3011 | task_rq_unlock(rq, p, rf); |
5cc389bc PZ |
3012 | |
3013 | return ret; | |
3014 | } | |
25834c73 | 3015 | |
07ec77a1 WD |
3016 | /* |
3017 | * Change a given task's CPU affinity. Migrate the thread to a | |
3018 | * proper CPU and schedule it away if the CPU it's executing on | |
3019 | * is removed from the allowed bitmask. | |
3020 | * | |
3021 | * NOTE: the caller must have a valid reference to the task, the | |
3022 | * task must not exit() & deallocate itself prematurely. The | |
3023 | * call is not atomic; no spinlocks may be held. | |
3024 | */ | |
3025 | static int __set_cpus_allowed_ptr(struct task_struct *p, | |
713a2e21 | 3026 | struct affinity_context *ctx) |
07ec77a1 WD |
3027 | { |
3028 | struct rq_flags rf; | |
3029 | struct rq *rq; | |
3030 | ||
3031 | rq = task_rq_lock(p, &rf); | |
da019032 WL |
3032 | /* |
3033 | * Masking should be skipped if SCA_USER or any of the SCA_MIGRATE_* | |
3034 | * flags are set. | |
3035 | */ | |
3036 | if (p->user_cpus_ptr && | |
3037 | !(ctx->flags & (SCA_USER | SCA_MIGRATE_ENABLE | SCA_MIGRATE_DISABLE)) && | |
3038 | cpumask_and(rq->scratch_mask, ctx->new_mask, p->user_cpus_ptr)) | |
3039 | ctx->new_mask = rq->scratch_mask; | |
3040 | ||
713a2e21 | 3041 | return __set_cpus_allowed_ptr_locked(p, ctx, rq, &rf); |
07ec77a1 WD |
3042 | } |
3043 | ||
25834c73 PZ |
3044 | int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask) |
3045 | { | |
713a2e21 WL |
3046 | struct affinity_context ac = { |
3047 | .new_mask = new_mask, | |
3048 | .flags = 0, | |
3049 | }; | |
3050 | ||
3051 | return __set_cpus_allowed_ptr(p, &ac); | |
25834c73 | 3052 | } |
5cc389bc PZ |
3053 | EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr); |
3054 | ||
07ec77a1 WD |
3055 | /* |
3056 | * Change a given task's CPU affinity to the intersection of its current | |
8f9ea86f WL |
3057 | * affinity mask and @subset_mask, writing the resulting mask to @new_mask. |
3058 | * If user_cpus_ptr is defined, use it as the basis for restricting CPU | |
3059 | * affinity or use cpu_online_mask instead. | |
3060 | * | |
07ec77a1 WD |
3061 | * If the resulting mask is empty, leave the affinity unchanged and return |
3062 | * -EINVAL. | |
3063 | */ | |
3064 | static int restrict_cpus_allowed_ptr(struct task_struct *p, | |
3065 | struct cpumask *new_mask, | |
3066 | const struct cpumask *subset_mask) | |
3067 | { | |
8f9ea86f WL |
3068 | struct affinity_context ac = { |
3069 | .new_mask = new_mask, | |
3070 | .flags = 0, | |
3071 | }; | |
07ec77a1 WD |
3072 | struct rq_flags rf; |
3073 | struct rq *rq; | |
3074 | int err; | |
3075 | ||
07ec77a1 WD |
3076 | rq = task_rq_lock(p, &rf); |
3077 | ||
3078 | /* | |
3079 | * Forcefully restricting the affinity of a deadline task is | |
3080 | * likely to cause problems, so fail and noisily override the | |
3081 | * mask entirely. | |
3082 | */ | |
3083 | if (task_has_dl_policy(p) && dl_bandwidth_enabled()) { | |
3084 | err = -EPERM; | |
3085 | goto err_unlock; | |
3086 | } | |
3087 | ||
8f9ea86f | 3088 | if (!cpumask_and(new_mask, task_user_cpus(p), subset_mask)) { |
07ec77a1 WD |
3089 | err = -EINVAL; |
3090 | goto err_unlock; | |
3091 | } | |
3092 | ||
713a2e21 | 3093 | return __set_cpus_allowed_ptr_locked(p, &ac, rq, &rf); |
07ec77a1 WD |
3094 | |
3095 | err_unlock: | |
3096 | task_rq_unlock(rq, p, &rf); | |
07ec77a1 WD |
3097 | return err; |
3098 | } | |
3099 | ||
3100 | /* | |
3101 | * Restrict the CPU affinity of task @p so that it is a subset of | |
5584e8ac | 3102 | * task_cpu_possible_mask() and point @p->user_cpus_ptr to a copy of the |
07ec77a1 WD |
3103 | * old affinity mask. If the resulting mask is empty, we warn and walk |
3104 | * up the cpuset hierarchy until we find a suitable mask. | |
3105 | */ | |
3106 | void force_compatible_cpus_allowed_ptr(struct task_struct *p) | |
3107 | { | |
3108 | cpumask_var_t new_mask; | |
3109 | const struct cpumask *override_mask = task_cpu_possible_mask(p); | |
3110 | ||
3111 | alloc_cpumask_var(&new_mask, GFP_KERNEL); | |
3112 | ||
3113 | /* | |
3114 | * __migrate_task() can fail silently in the face of concurrent | |
3115 | * offlining of the chosen destination CPU, so take the hotplug | |
3116 | * lock to ensure that the migration succeeds. | |
3117 | */ | |
3118 | cpus_read_lock(); | |
3119 | if (!cpumask_available(new_mask)) | |
3120 | goto out_set_mask; | |
3121 | ||
3122 | if (!restrict_cpus_allowed_ptr(p, new_mask, override_mask)) | |
3123 | goto out_free_mask; | |
3124 | ||
3125 | /* | |
3126 | * We failed to find a valid subset of the affinity mask for the | |
3127 | * task, so override it based on its cpuset hierarchy. | |
3128 | */ | |
3129 | cpuset_cpus_allowed(p, new_mask); | |
3130 | override_mask = new_mask; | |
3131 | ||
3132 | out_set_mask: | |
3133 | if (printk_ratelimit()) { | |
3134 | printk_deferred("Overriding affinity for process %d (%s) to CPUs %*pbl\n", | |
3135 | task_pid_nr(p), p->comm, | |
3136 | cpumask_pr_args(override_mask)); | |
3137 | } | |
3138 | ||
3139 | WARN_ON(set_cpus_allowed_ptr(p, override_mask)); | |
3140 | out_free_mask: | |
3141 | cpus_read_unlock(); | |
3142 | free_cpumask_var(new_mask); | |
3143 | } | |
3144 | ||
3145 | static int | |
713a2e21 | 3146 | __sched_setaffinity(struct task_struct *p, struct affinity_context *ctx); |
07ec77a1 WD |
3147 | |
3148 | /* | |
3149 | * Restore the affinity of a task @p which was previously restricted by a | |
8f9ea86f | 3150 | * call to force_compatible_cpus_allowed_ptr(). |
07ec77a1 WD |
3151 | * |
3152 | * It is the caller's responsibility to serialise this with any calls to | |
3153 | * force_compatible_cpus_allowed_ptr(@p). | |
3154 | */ | |
3155 | void relax_compatible_cpus_allowed_ptr(struct task_struct *p) | |
3156 | { | |
713a2e21 | 3157 | struct affinity_context ac = { |
8f9ea86f WL |
3158 | .new_mask = task_user_cpus(p), |
3159 | .flags = 0, | |
713a2e21 | 3160 | }; |
8f9ea86f | 3161 | int ret; |
07ec77a1 WD |
3162 | |
3163 | /* | |
8f9ea86f WL |
3164 | * Try to restore the old affinity mask with __sched_setaffinity(). |
3165 | * Cpuset masking will be done there too. | |
07ec77a1 | 3166 | */ |
8f9ea86f WL |
3167 | ret = __sched_setaffinity(p, &ac); |
3168 | WARN_ON_ONCE(ret); | |
07ec77a1 WD |
3169 | } |
3170 | ||
dd41f596 | 3171 | void set_task_cpu(struct task_struct *p, unsigned int new_cpu) |
c65cc870 | 3172 | { |
e2912009 | 3173 | #ifdef CONFIG_SCHED_DEBUG |
2f064a59 PZ |
3174 | unsigned int state = READ_ONCE(p->__state); |
3175 | ||
e2912009 PZ |
3176 | /* |
3177 | * We should never call set_task_cpu() on a blocked task, | |
3178 | * ttwu() will sort out the placement. | |
3179 | */ | |
2f064a59 | 3180 | WARN_ON_ONCE(state != TASK_RUNNING && state != TASK_WAKING && !p->on_rq); |
0122ec5b | 3181 | |
3ea94de1 JP |
3182 | /* |
3183 | * Migrating fair class task must have p->on_rq = TASK_ON_RQ_MIGRATING, | |
3184 | * because schedstat_wait_{start,end} rebase migrating task's wait_start | |
3185 | * time relying on p->on_rq. | |
3186 | */ | |
2f064a59 | 3187 | WARN_ON_ONCE(state == TASK_RUNNING && |
3ea94de1 JP |
3188 | p->sched_class == &fair_sched_class && |
3189 | (p->on_rq && !task_on_rq_migrating(p))); | |
3190 | ||
0122ec5b | 3191 | #ifdef CONFIG_LOCKDEP |
6c6c54e1 PZ |
3192 | /* |
3193 | * The caller should hold either p->pi_lock or rq->lock, when changing | |
3194 | * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks. | |
3195 | * | |
3196 | * sched_move_task() holds both and thus holding either pins the cgroup, | |
8323f26c | 3197 | * see task_group(). |
6c6c54e1 PZ |
3198 | * |
3199 | * Furthermore, all task_rq users should acquire both locks, see | |
3200 | * task_rq_lock(). | |
3201 | */ | |
0122ec5b | 3202 | WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) || |
9ef7e7e3 | 3203 | lockdep_is_held(__rq_lockp(task_rq(p))))); |
0122ec5b | 3204 | #endif |
4ff9083b PZ |
3205 | /* |
3206 | * Clearly, migrating tasks to offline CPUs is a fairly daft thing. | |
3207 | */ | |
3208 | WARN_ON_ONCE(!cpu_online(new_cpu)); | |
af449901 PZ |
3209 | |
3210 | WARN_ON_ONCE(is_migration_disabled(p)); | |
e2912009 PZ |
3211 | #endif |
3212 | ||
de1d7286 | 3213 | trace_sched_migrate_task(p, new_cpu); |
cbc34ed1 | 3214 | |
0c69774e | 3215 | if (task_cpu(p) != new_cpu) { |
0a74bef8 | 3216 | if (p->sched_class->migrate_task_rq) |
1327237a | 3217 | p->sched_class->migrate_task_rq(p, new_cpu); |
0c69774e | 3218 | p->se.nr_migrations++; |
d7822b1e | 3219 | rseq_migrate(p); |
223baf9d | 3220 | sched_mm_cid_migrate_from(p); |
ff303e66 | 3221 | perf_event_task_migrate(p); |
0c69774e | 3222 | } |
dd41f596 IM |
3223 | |
3224 | __set_task_cpu(p, new_cpu); | |
c65cc870 IM |
3225 | } |
3226 | ||
0ad4e3df | 3227 | #ifdef CONFIG_NUMA_BALANCING |
ac66f547 PZ |
3228 | static void __migrate_swap_task(struct task_struct *p, int cpu) |
3229 | { | |
da0c1e65 | 3230 | if (task_on_rq_queued(p)) { |
ac66f547 | 3231 | struct rq *src_rq, *dst_rq; |
8a8c69c3 | 3232 | struct rq_flags srf, drf; |
ac66f547 PZ |
3233 | |
3234 | src_rq = task_rq(p); | |
3235 | dst_rq = cpu_rq(cpu); | |
3236 | ||
8a8c69c3 PZ |
3237 | rq_pin_lock(src_rq, &srf); |
3238 | rq_pin_lock(dst_rq, &drf); | |
3239 | ||
ac66f547 PZ |
3240 | deactivate_task(src_rq, p, 0); |
3241 | set_task_cpu(p, cpu); | |
3242 | activate_task(dst_rq, p, 0); | |
3243 | check_preempt_curr(dst_rq, p, 0); | |
8a8c69c3 PZ |
3244 | |
3245 | rq_unpin_lock(dst_rq, &drf); | |
3246 | rq_unpin_lock(src_rq, &srf); | |
3247 | ||
ac66f547 PZ |
3248 | } else { |
3249 | /* | |
3250 | * Task isn't running anymore; make it appear like we migrated | |
3251 | * it before it went to sleep. This means on wakeup we make the | |
d1ccc66d | 3252 | * previous CPU our target instead of where it really is. |
ac66f547 PZ |
3253 | */ |
3254 | p->wake_cpu = cpu; | |
3255 | } | |
3256 | } | |
3257 | ||
3258 | struct migration_swap_arg { | |
3259 | struct task_struct *src_task, *dst_task; | |
3260 | int src_cpu, dst_cpu; | |
3261 | }; | |
3262 | ||
3263 | static int migrate_swap_stop(void *data) | |
3264 | { | |
3265 | struct migration_swap_arg *arg = data; | |
3266 | struct rq *src_rq, *dst_rq; | |
3267 | int ret = -EAGAIN; | |
3268 | ||
62694cd5 PZ |
3269 | if (!cpu_active(arg->src_cpu) || !cpu_active(arg->dst_cpu)) |
3270 | return -EAGAIN; | |
3271 | ||
ac66f547 PZ |
3272 | src_rq = cpu_rq(arg->src_cpu); |
3273 | dst_rq = cpu_rq(arg->dst_cpu); | |
3274 | ||
74602315 PZ |
3275 | double_raw_lock(&arg->src_task->pi_lock, |
3276 | &arg->dst_task->pi_lock); | |
ac66f547 | 3277 | double_rq_lock(src_rq, dst_rq); |
62694cd5 | 3278 | |
ac66f547 PZ |
3279 | if (task_cpu(arg->dst_task) != arg->dst_cpu) |
3280 | goto unlock; | |
3281 | ||
3282 | if (task_cpu(arg->src_task) != arg->src_cpu) | |
3283 | goto unlock; | |
3284 | ||
3bd37062 | 3285 | if (!cpumask_test_cpu(arg->dst_cpu, arg->src_task->cpus_ptr)) |
ac66f547 PZ |
3286 | goto unlock; |
3287 | ||
3bd37062 | 3288 | if (!cpumask_test_cpu(arg->src_cpu, arg->dst_task->cpus_ptr)) |
ac66f547 PZ |
3289 | goto unlock; |
3290 | ||
3291 | __migrate_swap_task(arg->src_task, arg->dst_cpu); | |
3292 | __migrate_swap_task(arg->dst_task, arg->src_cpu); | |
3293 | ||
3294 | ret = 0; | |
3295 | ||
3296 | unlock: | |
3297 | double_rq_unlock(src_rq, dst_rq); | |
74602315 PZ |
3298 | raw_spin_unlock(&arg->dst_task->pi_lock); |
3299 | raw_spin_unlock(&arg->src_task->pi_lock); | |
ac66f547 PZ |
3300 | |
3301 | return ret; | |
3302 | } | |
3303 | ||
3304 | /* | |
3305 | * Cross migrate two tasks | |
3306 | */ | |
0ad4e3df SD |
3307 | int migrate_swap(struct task_struct *cur, struct task_struct *p, |
3308 | int target_cpu, int curr_cpu) | |
ac66f547 PZ |
3309 | { |
3310 | struct migration_swap_arg arg; | |
3311 | int ret = -EINVAL; | |
3312 | ||
ac66f547 PZ |
3313 | arg = (struct migration_swap_arg){ |
3314 | .src_task = cur, | |
0ad4e3df | 3315 | .src_cpu = curr_cpu, |
ac66f547 | 3316 | .dst_task = p, |
0ad4e3df | 3317 | .dst_cpu = target_cpu, |
ac66f547 PZ |
3318 | }; |
3319 | ||
3320 | if (arg.src_cpu == arg.dst_cpu) | |
3321 | goto out; | |
3322 | ||
6acce3ef PZ |
3323 | /* |
3324 | * These three tests are all lockless; this is OK since all of them | |
3325 | * will be re-checked with proper locks held further down the line. | |
3326 | */ | |
ac66f547 PZ |
3327 | if (!cpu_active(arg.src_cpu) || !cpu_active(arg.dst_cpu)) |
3328 | goto out; | |
3329 | ||
3bd37062 | 3330 | if (!cpumask_test_cpu(arg.dst_cpu, arg.src_task->cpus_ptr)) |
ac66f547 PZ |
3331 | goto out; |
3332 | ||
3bd37062 | 3333 | if (!cpumask_test_cpu(arg.src_cpu, arg.dst_task->cpus_ptr)) |
ac66f547 PZ |
3334 | goto out; |
3335 | ||
286549dc | 3336 | trace_sched_swap_numa(cur, arg.src_cpu, p, arg.dst_cpu); |
ac66f547 PZ |
3337 | ret = stop_two_cpus(arg.dst_cpu, arg.src_cpu, migrate_swap_stop, &arg); |
3338 | ||
3339 | out: | |
ac66f547 PZ |
3340 | return ret; |
3341 | } | |
0ad4e3df | 3342 | #endif /* CONFIG_NUMA_BALANCING */ |
ac66f547 | 3343 | |
1da177e4 LT |
3344 | /* |
3345 | * wait_task_inactive - wait for a thread to unschedule. | |
3346 | * | |
f9fc8cad PZ |
3347 | * Wait for the thread to block in any of the states set in @match_state. |
3348 | * If it changes, i.e. @p might have woken up, then return zero. When we | |
3349 | * succeed in waiting for @p to be off its CPU, we return a positive number | |
3350 | * (its total switch count). If a second call a short while later returns the | |
3351 | * same number, the caller can be sure that @p has remained unscheduled the | |
3352 | * whole time. | |
85ba2d86 | 3353 | * |
1da177e4 LT |
3354 | * The caller must ensure that the task *will* unschedule sometime soon, |
3355 | * else this function might spin for a *long* time. This function can't | |
3356 | * be called with interrupts off, or it may introduce deadlock with | |
3357 | * smp_call_function() if an IPI is sent by the same process we are | |
3358 | * waiting to become inactive. | |
3359 | */ | |
2f064a59 | 3360 | unsigned long wait_task_inactive(struct task_struct *p, unsigned int match_state) |
1da177e4 | 3361 | { |
da0c1e65 | 3362 | int running, queued; |
eb580751 | 3363 | struct rq_flags rf; |
85ba2d86 | 3364 | unsigned long ncsw; |
70b97a7f | 3365 | struct rq *rq; |
1da177e4 | 3366 | |
3a5c359a AK |
3367 | for (;;) { |
3368 | /* | |
3369 | * We do the initial early heuristics without holding | |
3370 | * any task-queue locks at all. We'll only try to get | |
3371 | * the runqueue lock when things look like they will | |
3372 | * work out! | |
3373 | */ | |
3374 | rq = task_rq(p); | |
fa490cfd | 3375 | |
3a5c359a AK |
3376 | /* |
3377 | * If the task is actively running on another CPU | |
3378 | * still, just relax and busy-wait without holding | |
3379 | * any locks. | |
3380 | * | |
3381 | * NOTE! Since we don't hold any locks, it's not | |
3382 | * even sure that "rq" stays as the right runqueue! | |
0b9d46fc | 3383 | * But we don't care, since "task_on_cpu()" will |
3a5c359a AK |
3384 | * return false if the runqueue has changed and p |
3385 | * is actually now running somewhere else! | |
3386 | */ | |
0b9d46fc | 3387 | while (task_on_cpu(rq, p)) { |
f9fc8cad | 3388 | if (!(READ_ONCE(p->__state) & match_state)) |
85ba2d86 | 3389 | return 0; |
3a5c359a | 3390 | cpu_relax(); |
85ba2d86 | 3391 | } |
fa490cfd | 3392 | |
3a5c359a AK |
3393 | /* |
3394 | * Ok, time to look more closely! We need the rq | |
3395 | * lock now, to be *sure*. If we're wrong, we'll | |
3396 | * just go back and repeat. | |
3397 | */ | |
eb580751 | 3398 | rq = task_rq_lock(p, &rf); |
27a9da65 | 3399 | trace_sched_wait_task(p); |
0b9d46fc | 3400 | running = task_on_cpu(rq, p); |
da0c1e65 | 3401 | queued = task_on_rq_queued(p); |
85ba2d86 | 3402 | ncsw = 0; |
f9fc8cad | 3403 | if (READ_ONCE(p->__state) & match_state) |
93dcf55f | 3404 | ncsw = p->nvcsw | LONG_MIN; /* sets MSB */ |
eb580751 | 3405 | task_rq_unlock(rq, p, &rf); |
fa490cfd | 3406 | |
85ba2d86 RM |
3407 | /* |
3408 | * If it changed from the expected state, bail out now. | |
3409 | */ | |
3410 | if (unlikely(!ncsw)) | |
3411 | break; | |
3412 | ||
3a5c359a AK |
3413 | /* |
3414 | * Was it really running after all now that we | |
3415 | * checked with the proper locks actually held? | |
3416 | * | |
3417 | * Oops. Go back and try again.. | |
3418 | */ | |
3419 | if (unlikely(running)) { | |
3420 | cpu_relax(); | |
3421 | continue; | |
3422 | } | |
fa490cfd | 3423 | |
3a5c359a AK |
3424 | /* |
3425 | * It's not enough that it's not actively running, | |
3426 | * it must be off the runqueue _entirely_, and not | |
3427 | * preempted! | |
3428 | * | |
80dd99b3 | 3429 | * So if it was still runnable (but just not actively |
3a5c359a AK |
3430 | * running right now), it's preempted, and we should |
3431 | * yield - it could be a while. | |
3432 | */ | |
da0c1e65 | 3433 | if (unlikely(queued)) { |
8b0e1953 | 3434 | ktime_t to = NSEC_PER_SEC / HZ; |
8eb90c30 TG |
3435 | |
3436 | set_current_state(TASK_UNINTERRUPTIBLE); | |
c33627e9 | 3437 | schedule_hrtimeout(&to, HRTIMER_MODE_REL_HARD); |
3a5c359a AK |
3438 | continue; |
3439 | } | |
fa490cfd | 3440 | |
3a5c359a AK |
3441 | /* |
3442 | * Ahh, all good. It wasn't running, and it wasn't | |
3443 | * runnable, which means that it will never become | |
3444 | * running in the future either. We're all done! | |
3445 | */ | |
3446 | break; | |
3447 | } | |
85ba2d86 RM |
3448 | |
3449 | return ncsw; | |
1da177e4 LT |
3450 | } |
3451 | ||
3452 | /*** | |
3453 | * kick_process - kick a running thread to enter/exit the kernel | |
3454 | * @p: the to-be-kicked thread | |
3455 | * | |
3456 | * Cause a process which is running on another CPU to enter | |
3457 | * kernel-mode, without any delay. (to get signals handled.) | |
3458 | * | |
25985edc | 3459 | * NOTE: this function doesn't have to take the runqueue lock, |
1da177e4 LT |
3460 | * because all it wants to ensure is that the remote task enters |
3461 | * the kernel. If the IPI races and the task has been migrated | |
3462 | * to another CPU then no harm is done and the purpose has been | |
3463 | * achieved as well. | |
3464 | */ | |
36c8b586 | 3465 | void kick_process(struct task_struct *p) |
1da177e4 LT |
3466 | { |
3467 | int cpu; | |
3468 | ||
3469 | preempt_disable(); | |
3470 | cpu = task_cpu(p); | |
3471 | if ((cpu != smp_processor_id()) && task_curr(p)) | |
3472 | smp_send_reschedule(cpu); | |
3473 | preempt_enable(); | |
3474 | } | |
b43e3521 | 3475 | EXPORT_SYMBOL_GPL(kick_process); |
1da177e4 | 3476 | |
30da688e | 3477 | /* |
3bd37062 | 3478 | * ->cpus_ptr is protected by both rq->lock and p->pi_lock |
e9d867a6 PZI |
3479 | * |
3480 | * A few notes on cpu_active vs cpu_online: | |
3481 | * | |
3482 | * - cpu_active must be a subset of cpu_online | |
3483 | * | |
97fb7a0a | 3484 | * - on CPU-up we allow per-CPU kthreads on the online && !active CPU, |
e9d867a6 | 3485 | * see __set_cpus_allowed_ptr(). At this point the newly online |
d1ccc66d | 3486 | * CPU isn't yet part of the sched domains, and balancing will not |
e9d867a6 PZI |
3487 | * see it. |
3488 | * | |
d1ccc66d | 3489 | * - on CPU-down we clear cpu_active() to mask the sched domains and |
e9d867a6 | 3490 | * avoid the load balancer to place new tasks on the to be removed |
d1ccc66d | 3491 | * CPU. Existing tasks will remain running there and will be taken |
e9d867a6 PZI |
3492 | * off. |
3493 | * | |
3494 | * This means that fallback selection must not select !active CPUs. | |
3495 | * And can assume that any active CPU must be online. Conversely | |
3496 | * select_task_rq() below may allow selection of !active CPUs in order | |
3497 | * to satisfy the above rules. | |
30da688e | 3498 | */ |
5da9a0fb PZ |
3499 | static int select_fallback_rq(int cpu, struct task_struct *p) |
3500 | { | |
aa00d89c TC |
3501 | int nid = cpu_to_node(cpu); |
3502 | const struct cpumask *nodemask = NULL; | |
2baab4e9 PZ |
3503 | enum { cpuset, possible, fail } state = cpuset; |
3504 | int dest_cpu; | |
5da9a0fb | 3505 | |
aa00d89c | 3506 | /* |
d1ccc66d IM |
3507 | * If the node that the CPU is on has been offlined, cpu_to_node() |
3508 | * will return -1. There is no CPU on the node, and we should | |
3509 | * select the CPU on the other node. | |
aa00d89c TC |
3510 | */ |
3511 | if (nid != -1) { | |
3512 | nodemask = cpumask_of_node(nid); | |
3513 | ||
3514 | /* Look for allowed, online CPU in same node. */ | |
3515 | for_each_cpu(dest_cpu, nodemask) { | |
9ae606bc | 3516 | if (is_cpu_allowed(p, dest_cpu)) |
aa00d89c TC |
3517 | return dest_cpu; |
3518 | } | |
2baab4e9 | 3519 | } |
5da9a0fb | 3520 | |
2baab4e9 PZ |
3521 | for (;;) { |
3522 | /* Any allowed, online CPU? */ | |
3bd37062 | 3523 | for_each_cpu(dest_cpu, p->cpus_ptr) { |
175f0e25 | 3524 | if (!is_cpu_allowed(p, dest_cpu)) |
2baab4e9 | 3525 | continue; |
175f0e25 | 3526 | |
2baab4e9 PZ |
3527 | goto out; |
3528 | } | |
5da9a0fb | 3529 | |
e73e85f0 | 3530 | /* No more Mr. Nice Guy. */ |
2baab4e9 PZ |
3531 | switch (state) { |
3532 | case cpuset: | |
97c0054d | 3533 | if (cpuset_cpus_allowed_fallback(p)) { |
e73e85f0 ON |
3534 | state = possible; |
3535 | break; | |
3536 | } | |
df561f66 | 3537 | fallthrough; |
2baab4e9 | 3538 | case possible: |
af449901 PZ |
3539 | /* |
3540 | * XXX When called from select_task_rq() we only | |
3541 | * hold p->pi_lock and again violate locking order. | |
3542 | * | |
3543 | * More yuck to audit. | |
3544 | */ | |
9ae606bc | 3545 | do_set_cpus_allowed(p, task_cpu_possible_mask(p)); |
2baab4e9 PZ |
3546 | state = fail; |
3547 | break; | |
2baab4e9 PZ |
3548 | case fail: |
3549 | BUG(); | |
3550 | break; | |
3551 | } | |
3552 | } | |
3553 | ||
3554 | out: | |
3555 | if (state != cpuset) { | |
3556 | /* | |
3557 | * Don't tell them about moving exiting tasks or | |
3558 | * kernel threads (both mm NULL), since they never | |
3559 | * leave kernel. | |
3560 | */ | |
3561 | if (p->mm && printk_ratelimit()) { | |
aac74dc4 | 3562 | printk_deferred("process %d (%s) no longer affine to cpu%d\n", |
2baab4e9 PZ |
3563 | task_pid_nr(p), p->comm, cpu); |
3564 | } | |
5da9a0fb PZ |
3565 | } |
3566 | ||
3567 | return dest_cpu; | |
3568 | } | |
3569 | ||
e2912009 | 3570 | /* |
3bd37062 | 3571 | * The caller (fork, wakeup) owns p->pi_lock, ->cpus_ptr is stable. |
e2912009 | 3572 | */ |
970b13ba | 3573 | static inline |
3aef1551 | 3574 | int select_task_rq(struct task_struct *p, int cpu, int wake_flags) |
970b13ba | 3575 | { |
cbce1a68 PZ |
3576 | lockdep_assert_held(&p->pi_lock); |
3577 | ||
af449901 | 3578 | if (p->nr_cpus_allowed > 1 && !is_migration_disabled(p)) |
3aef1551 | 3579 | cpu = p->sched_class->select_task_rq(p, cpu, wake_flags); |
e9d867a6 | 3580 | else |
3bd37062 | 3581 | cpu = cpumask_any(p->cpus_ptr); |
e2912009 PZ |
3582 | |
3583 | /* | |
3584 | * In order not to call set_task_cpu() on a blocking task we need | |
3bd37062 | 3585 | * to rely on ttwu() to place the task on a valid ->cpus_ptr |
d1ccc66d | 3586 | * CPU. |
e2912009 PZ |
3587 | * |
3588 | * Since this is common to all placement strategies, this lives here. | |
3589 | * | |
3590 | * [ this allows ->select_task() to simply return task_cpu(p) and | |
3591 | * not worry about this generic constraint ] | |
3592 | */ | |
7af443ee | 3593 | if (unlikely(!is_cpu_allowed(p, cpu))) |
5da9a0fb | 3594 | cpu = select_fallback_rq(task_cpu(p), p); |
e2912009 PZ |
3595 | |
3596 | return cpu; | |
970b13ba | 3597 | } |
09a40af5 | 3598 | |
f5832c19 NP |
3599 | void sched_set_stop_task(int cpu, struct task_struct *stop) |
3600 | { | |
ded467dc | 3601 | static struct lock_class_key stop_pi_lock; |
f5832c19 NP |
3602 | struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 }; |
3603 | struct task_struct *old_stop = cpu_rq(cpu)->stop; | |
3604 | ||
3605 | if (stop) { | |
3606 | /* | |
3607 | * Make it appear like a SCHED_FIFO task, its something | |
3608 | * userspace knows about and won't get confused about. | |
3609 | * | |
3610 | * Also, it will make PI more or less work without too | |
3611 | * much confusion -- but then, stop work should not | |
3612 | * rely on PI working anyway. | |
3613 | */ | |
3614 | sched_setscheduler_nocheck(stop, SCHED_FIFO, ¶m); | |
3615 | ||
3616 | stop->sched_class = &stop_sched_class; | |
ded467dc PZ |
3617 | |
3618 | /* | |
3619 | * The PI code calls rt_mutex_setprio() with ->pi_lock held to | |
3620 | * adjust the effective priority of a task. As a result, | |
3621 | * rt_mutex_setprio() can trigger (RT) balancing operations, | |
3622 | * which can then trigger wakeups of the stop thread to push | |
3623 | * around the current task. | |
3624 | * | |
3625 | * The stop task itself will never be part of the PI-chain, it | |
3626 | * never blocks, therefore that ->pi_lock recursion is safe. | |
3627 | * Tell lockdep about this by placing the stop->pi_lock in its | |
3628 | * own class. | |
3629 | */ | |
3630 | lockdep_set_class(&stop->pi_lock, &stop_pi_lock); | |
f5832c19 NP |
3631 | } |
3632 | ||
3633 | cpu_rq(cpu)->stop = stop; | |
3634 | ||
3635 | if (old_stop) { | |
3636 | /* | |
3637 | * Reset it back to a normal scheduling class so that | |
3638 | * it can die in pieces. | |
3639 | */ | |
3640 | old_stop->sched_class = &rt_sched_class; | |
3641 | } | |
3642 | } | |
3643 | ||
74d862b6 | 3644 | #else /* CONFIG_SMP */ |
25834c73 PZ |
3645 | |
3646 | static inline int __set_cpus_allowed_ptr(struct task_struct *p, | |
713a2e21 | 3647 | struct affinity_context *ctx) |
25834c73 | 3648 | { |
713a2e21 | 3649 | return set_cpus_allowed_ptr(p, ctx->new_mask); |
25834c73 PZ |
3650 | } |
3651 | ||
af449901 PZ |
3652 | static inline void migrate_disable_switch(struct rq *rq, struct task_struct *p) { } |
3653 | ||
3015ef4b TG |
3654 | static inline bool rq_has_pinned_tasks(struct rq *rq) |
3655 | { | |
3656 | return false; | |
3657 | } | |
3658 | ||
9a5418bc WL |
3659 | static inline cpumask_t *alloc_user_cpus_ptr(int node) |
3660 | { | |
3661 | return NULL; | |
3662 | } | |
3663 | ||
74d862b6 | 3664 | #endif /* !CONFIG_SMP */ |
970b13ba | 3665 | |
d7c01d27 | 3666 | static void |
b84cb5df | 3667 | ttwu_stat(struct task_struct *p, int cpu, int wake_flags) |
9ed3811a | 3668 | { |
4fa8d299 | 3669 | struct rq *rq; |
b84cb5df | 3670 | |
4fa8d299 JP |
3671 | if (!schedstat_enabled()) |
3672 | return; | |
3673 | ||
3674 | rq = this_rq(); | |
d7c01d27 | 3675 | |
4fa8d299 JP |
3676 | #ifdef CONFIG_SMP |
3677 | if (cpu == rq->cpu) { | |
b85c8b71 | 3678 | __schedstat_inc(rq->ttwu_local); |
ceeadb83 | 3679 | __schedstat_inc(p->stats.nr_wakeups_local); |
d7c01d27 PZ |
3680 | } else { |
3681 | struct sched_domain *sd; | |
3682 | ||
ceeadb83 | 3683 | __schedstat_inc(p->stats.nr_wakeups_remote); |
057f3fad | 3684 | rcu_read_lock(); |
4fa8d299 | 3685 | for_each_domain(rq->cpu, sd) { |
d7c01d27 | 3686 | if (cpumask_test_cpu(cpu, sched_domain_span(sd))) { |
b85c8b71 | 3687 | __schedstat_inc(sd->ttwu_wake_remote); |
d7c01d27 PZ |
3688 | break; |
3689 | } | |
3690 | } | |
057f3fad | 3691 | rcu_read_unlock(); |
d7c01d27 | 3692 | } |
f339b9dc PZ |
3693 | |
3694 | if (wake_flags & WF_MIGRATED) | |
ceeadb83 | 3695 | __schedstat_inc(p->stats.nr_wakeups_migrate); |
d7c01d27 PZ |
3696 | #endif /* CONFIG_SMP */ |
3697 | ||
b85c8b71 | 3698 | __schedstat_inc(rq->ttwu_count); |
ceeadb83 | 3699 | __schedstat_inc(p->stats.nr_wakeups); |
d7c01d27 PZ |
3700 | |
3701 | if (wake_flags & WF_SYNC) | |
ceeadb83 | 3702 | __schedstat_inc(p->stats.nr_wakeups_sync); |
d7c01d27 PZ |
3703 | } |
3704 | ||
23f41eeb | 3705 | /* |
160fb0d8 | 3706 | * Mark the task runnable. |
23f41eeb | 3707 | */ |
160fb0d8 | 3708 | static inline void ttwu_do_wakeup(struct task_struct *p) |
9ed3811a | 3709 | { |
2f064a59 | 3710 | WRITE_ONCE(p->__state, TASK_RUNNING); |
fbd705a0 | 3711 | trace_sched_wakeup(p); |
160fb0d8 CZ |
3712 | } |
3713 | ||
3714 | static void | |
3715 | ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags, | |
3716 | struct rq_flags *rf) | |
3717 | { | |
3718 | int en_flags = ENQUEUE_WAKEUP | ENQUEUE_NOCLOCK; | |
3719 | ||
3720 | lockdep_assert_rq_held(rq); | |
3721 | ||
3722 | if (p->sched_contributes_to_load) | |
3723 | rq->nr_uninterruptible--; | |
3724 | ||
3725 | #ifdef CONFIG_SMP | |
3726 | if (wake_flags & WF_MIGRATED) | |
3727 | en_flags |= ENQUEUE_MIGRATED; | |
3728 | else | |
3729 | #endif | |
3730 | if (p->in_iowait) { | |
3731 | delayacct_blkio_end(p); | |
3732 | atomic_dec(&task_rq(p)->nr_iowait); | |
3733 | } | |
3734 | ||
3735 | activate_task(rq, p, en_flags); | |
3736 | check_preempt_curr(rq, p, wake_flags); | |
3737 | ||
3738 | ttwu_do_wakeup(p); | |
fbd705a0 | 3739 | |
9ed3811a | 3740 | #ifdef CONFIG_SMP |
4c9a4bc8 PZ |
3741 | if (p->sched_class->task_woken) { |
3742 | /* | |
b19a888c | 3743 | * Our task @p is fully woken up and running; so it's safe to |
cbce1a68 | 3744 | * drop the rq->lock, hereafter rq is only used for statistics. |
4c9a4bc8 | 3745 | */ |
d8ac8971 | 3746 | rq_unpin_lock(rq, rf); |
9ed3811a | 3747 | p->sched_class->task_woken(rq, p); |
d8ac8971 | 3748 | rq_repin_lock(rq, rf); |
4c9a4bc8 | 3749 | } |
9ed3811a | 3750 | |
e69c6341 | 3751 | if (rq->idle_stamp) { |
78becc27 | 3752 | u64 delta = rq_clock(rq) - rq->idle_stamp; |
9bd721c5 | 3753 | u64 max = 2*rq->max_idle_balance_cost; |
9ed3811a | 3754 | |
abfafa54 JL |
3755 | update_avg(&rq->avg_idle, delta); |
3756 | ||
3757 | if (rq->avg_idle > max) | |
9ed3811a | 3758 | rq->avg_idle = max; |
abfafa54 | 3759 | |
94aafc3e PZ |
3760 | rq->wake_stamp = jiffies; |
3761 | rq->wake_avg_idle = rq->avg_idle / 2; | |
3762 | ||
9ed3811a TH |
3763 | rq->idle_stamp = 0; |
3764 | } | |
3765 | #endif | |
3766 | } | |
3767 | ||
c05fbafb | 3768 | /* |
58877d34 PZ |
3769 | * Consider @p being inside a wait loop: |
3770 | * | |
3771 | * for (;;) { | |
3772 | * set_current_state(TASK_UNINTERRUPTIBLE); | |
3773 | * | |
3774 | * if (CONDITION) | |
3775 | * break; | |
3776 | * | |
3777 | * schedule(); | |
3778 | * } | |
3779 | * __set_current_state(TASK_RUNNING); | |
3780 | * | |
3781 | * between set_current_state() and schedule(). In this case @p is still | |
3782 | * runnable, so all that needs doing is change p->state back to TASK_RUNNING in | |
3783 | * an atomic manner. | |
3784 | * | |
3785 | * By taking task_rq(p)->lock we serialize against schedule(), if @p->on_rq | |
3786 | * then schedule() must still happen and p->state can be changed to | |
3787 | * TASK_RUNNING. Otherwise we lost the race, schedule() has happened, and we | |
3788 | * need to do a full wakeup with enqueue. | |
3789 | * | |
3790 | * Returns: %true when the wakeup is done, | |
3791 | * %false otherwise. | |
c05fbafb | 3792 | */ |
58877d34 | 3793 | static int ttwu_runnable(struct task_struct *p, int wake_flags) |
c05fbafb | 3794 | { |
eb580751 | 3795 | struct rq_flags rf; |
c05fbafb PZ |
3796 | struct rq *rq; |
3797 | int ret = 0; | |
3798 | ||
eb580751 | 3799 | rq = __task_rq_lock(p, &rf); |
da0c1e65 | 3800 | if (task_on_rq_queued(p)) { |
efe09385 CZ |
3801 | if (!task_on_cpu(rq, p)) { |
3802 | /* | |
3803 | * When on_rq && !on_cpu the task is preempted, see if | |
3804 | * it should preempt the task that is current now. | |
3805 | */ | |
3806 | update_rq_clock(rq); | |
3807 | check_preempt_curr(rq, p, wake_flags); | |
3808 | } | |
160fb0d8 | 3809 | ttwu_do_wakeup(p); |
c05fbafb PZ |
3810 | ret = 1; |
3811 | } | |
eb580751 | 3812 | __task_rq_unlock(rq, &rf); |
c05fbafb PZ |
3813 | |
3814 | return ret; | |
3815 | } | |
3816 | ||
317f3941 | 3817 | #ifdef CONFIG_SMP |
a1488664 | 3818 | void sched_ttwu_pending(void *arg) |
317f3941 | 3819 | { |
a1488664 | 3820 | struct llist_node *llist = arg; |
317f3941 | 3821 | struct rq *rq = this_rq(); |
73215849 | 3822 | struct task_struct *p, *t; |
d8ac8971 | 3823 | struct rq_flags rf; |
317f3941 | 3824 | |
e3baac47 PZ |
3825 | if (!llist) |
3826 | return; | |
3827 | ||
8a8c69c3 | 3828 | rq_lock_irqsave(rq, &rf); |
77558e4d | 3829 | update_rq_clock(rq); |
317f3941 | 3830 | |
8c4890d1 | 3831 | llist_for_each_entry_safe(p, t, llist, wake_entry.llist) { |
b6e13e85 PZ |
3832 | if (WARN_ON_ONCE(p->on_cpu)) |
3833 | smp_cond_load_acquire(&p->on_cpu, !VAL); | |
3834 | ||
3835 | if (WARN_ON_ONCE(task_cpu(p) != cpu_of(rq))) | |
3836 | set_task_cpu(p, cpu_of(rq)); | |
3837 | ||
73215849 | 3838 | ttwu_do_activate(rq, p, p->sched_remote_wakeup ? WF_MIGRATED : 0, &rf); |
b6e13e85 | 3839 | } |
317f3941 | 3840 | |
d6962c4f TD |
3841 | /* |
3842 | * Must be after enqueueing at least once task such that | |
3843 | * idle_cpu() does not observe a false-negative -- if it does, | |
3844 | * it is possible for select_idle_siblings() to stack a number | |
3845 | * of tasks on this CPU during that window. | |
3846 | * | |
3847 | * It is ok to clear ttwu_pending when another task pending. | |
3848 | * We will receive IPI after local irq enabled and then enqueue it. | |
3849 | * Since now nr_running > 0, idle_cpu() will always get correct result. | |
3850 | */ | |
3851 | WRITE_ONCE(rq->ttwu_pending, 0); | |
8a8c69c3 | 3852 | rq_unlock_irqrestore(rq, &rf); |
317f3941 PZ |
3853 | } |
3854 | ||
68f4ff04 VS |
3855 | /* |
3856 | * Prepare the scene for sending an IPI for a remote smp_call | |
3857 | * | |
3858 | * Returns true if the caller can proceed with sending the IPI. | |
3859 | * Returns false otherwise. | |
3860 | */ | |
3861 | bool call_function_single_prep_ipi(int cpu) | |
317f3941 | 3862 | { |
68f4ff04 | 3863 | if (set_nr_if_polling(cpu_rq(cpu)->idle)) { |
b2a02fc4 | 3864 | trace_sched_wake_idle_without_ipi(cpu); |
68f4ff04 | 3865 | return false; |
cc9cb0a7 | 3866 | } |
68f4ff04 VS |
3867 | |
3868 | return true; | |
317f3941 PZ |
3869 | } |
3870 | ||
2ebb1771 MG |
3871 | /* |
3872 | * Queue a task on the target CPUs wake_list and wake the CPU via IPI if | |
3873 | * necessary. The wakee CPU on receipt of the IPI will queue the task | |
3874 | * via sched_ttwu_wakeup() for activation so the wakee incurs the cost | |
3875 | * of the wakeup instead of the waker. | |
3876 | */ | |
3877 | static void __ttwu_queue_wakelist(struct task_struct *p, int cpu, int wake_flags) | |
317f3941 | 3878 | { |
e3baac47 PZ |
3879 | struct rq *rq = cpu_rq(cpu); |
3880 | ||
b7e7ade3 PZ |
3881 | p->sched_remote_wakeup = !!(wake_flags & WF_MIGRATED); |
3882 | ||
126c2092 | 3883 | WRITE_ONCE(rq->ttwu_pending, 1); |
8c4890d1 | 3884 | __smp_call_single_queue(cpu, &p->wake_entry.llist); |
317f3941 | 3885 | } |
d6aa8f85 | 3886 | |
f6be8af1 CL |
3887 | void wake_up_if_idle(int cpu) |
3888 | { | |
3889 | struct rq *rq = cpu_rq(cpu); | |
8a8c69c3 | 3890 | struct rq_flags rf; |
f6be8af1 | 3891 | |
fd7de1e8 AL |
3892 | rcu_read_lock(); |
3893 | ||
3894 | if (!is_idle_task(rcu_dereference(rq->curr))) | |
3895 | goto out; | |
f6be8af1 | 3896 | |
8850cb66 PZ |
3897 | rq_lock_irqsave(rq, &rf); |
3898 | if (is_idle_task(rq->curr)) | |
3899 | resched_curr(rq); | |
3900 | /* Else CPU is not idle, do nothing here: */ | |
3901 | rq_unlock_irqrestore(rq, &rf); | |
fd7de1e8 AL |
3902 | |
3903 | out: | |
3904 | rcu_read_unlock(); | |
f6be8af1 CL |
3905 | } |
3906 | ||
39be3501 | 3907 | bool cpus_share_cache(int this_cpu, int that_cpu) |
518cd623 | 3908 | { |
42dc938a VD |
3909 | if (this_cpu == that_cpu) |
3910 | return true; | |
3911 | ||
518cd623 PZ |
3912 | return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu); |
3913 | } | |
c6e7bd7a | 3914 | |
751d4cbc | 3915 | static inline bool ttwu_queue_cond(struct task_struct *p, int cpu) |
2ebb1771 | 3916 | { |
5ba2ffba PZ |
3917 | /* |
3918 | * Do not complicate things with the async wake_list while the CPU is | |
3919 | * in hotplug state. | |
3920 | */ | |
3921 | if (!cpu_active(cpu)) | |
3922 | return false; | |
3923 | ||
751d4cbc MG |
3924 | /* Ensure the task will still be allowed to run on the CPU. */ |
3925 | if (!cpumask_test_cpu(cpu, p->cpus_ptr)) | |
3926 | return false; | |
3927 | ||
2ebb1771 MG |
3928 | /* |
3929 | * If the CPU does not share cache, then queue the task on the | |
3930 | * remote rqs wakelist to avoid accessing remote data. | |
3931 | */ | |
3932 | if (!cpus_share_cache(smp_processor_id(), cpu)) | |
3933 | return true; | |
3934 | ||
f3dd3f67 TD |
3935 | if (cpu == smp_processor_id()) |
3936 | return false; | |
3937 | ||
2ebb1771 | 3938 | /* |
f3dd3f67 TD |
3939 | * If the wakee cpu is idle, or the task is descheduling and the |
3940 | * only running task on the CPU, then use the wakelist to offload | |
3941 | * the task activation to the idle (or soon-to-be-idle) CPU as | |
3942 | * the current CPU is likely busy. nr_running is checked to | |
3943 | * avoid unnecessary task stacking. | |
28156108 TD |
3944 | * |
3945 | * Note that we can only get here with (wakee) p->on_rq=0, | |
3946 | * p->on_cpu can be whatever, we've done the dequeue, so | |
3947 | * the wakee has been accounted out of ->nr_running. | |
2ebb1771 | 3948 | */ |
f3dd3f67 | 3949 | if (!cpu_rq(cpu)->nr_running) |
2ebb1771 MG |
3950 | return true; |
3951 | ||
3952 | return false; | |
3953 | } | |
3954 | ||
3955 | static bool ttwu_queue_wakelist(struct task_struct *p, int cpu, int wake_flags) | |
c6e7bd7a | 3956 | { |
751d4cbc | 3957 | if (sched_feat(TTWU_QUEUE) && ttwu_queue_cond(p, cpu)) { |
c6e7bd7a | 3958 | sched_clock_cpu(cpu); /* Sync clocks across CPUs */ |
2ebb1771 | 3959 | __ttwu_queue_wakelist(p, cpu, wake_flags); |
c6e7bd7a PZ |
3960 | return true; |
3961 | } | |
3962 | ||
3963 | return false; | |
3964 | } | |
58877d34 PZ |
3965 | |
3966 | #else /* !CONFIG_SMP */ | |
3967 | ||
3968 | static inline bool ttwu_queue_wakelist(struct task_struct *p, int cpu, int wake_flags) | |
3969 | { | |
3970 | return false; | |
3971 | } | |
3972 | ||
d6aa8f85 | 3973 | #endif /* CONFIG_SMP */ |
317f3941 | 3974 | |
b5179ac7 | 3975 | static void ttwu_queue(struct task_struct *p, int cpu, int wake_flags) |
c05fbafb PZ |
3976 | { |
3977 | struct rq *rq = cpu_rq(cpu); | |
d8ac8971 | 3978 | struct rq_flags rf; |
c05fbafb | 3979 | |
2ebb1771 | 3980 | if (ttwu_queue_wakelist(p, cpu, wake_flags)) |
317f3941 | 3981 | return; |
317f3941 | 3982 | |
8a8c69c3 | 3983 | rq_lock(rq, &rf); |
77558e4d | 3984 | update_rq_clock(rq); |
d8ac8971 | 3985 | ttwu_do_activate(rq, p, wake_flags, &rf); |
8a8c69c3 | 3986 | rq_unlock(rq, &rf); |
9ed3811a TH |
3987 | } |
3988 | ||
43295d73 TG |
3989 | /* |
3990 | * Invoked from try_to_wake_up() to check whether the task can be woken up. | |
3991 | * | |
3992 | * The caller holds p::pi_lock if p != current or has preemption | |
3993 | * disabled when p == current. | |
5f220be2 TG |
3994 | * |
3995 | * The rules of PREEMPT_RT saved_state: | |
3996 | * | |
3997 | * The related locking code always holds p::pi_lock when updating | |
3998 | * p::saved_state, which means the code is fully serialized in both cases. | |
3999 | * | |
4000 | * The lock wait and lock wakeups happen via TASK_RTLOCK_WAIT. No other | |
4001 | * bits set. This allows to distinguish all wakeup scenarios. | |
43295d73 TG |
4002 | */ |
4003 | static __always_inline | |
4004 | bool ttwu_state_match(struct task_struct *p, unsigned int state, int *success) | |
4005 | { | |
5f220be2 TG |
4006 | if (IS_ENABLED(CONFIG_DEBUG_PREEMPT)) { |
4007 | WARN_ON_ONCE((state & TASK_RTLOCK_WAIT) && | |
4008 | state != TASK_RTLOCK_WAIT); | |
4009 | } | |
4010 | ||
43295d73 TG |
4011 | if (READ_ONCE(p->__state) & state) { |
4012 | *success = 1; | |
4013 | return true; | |
4014 | } | |
5f220be2 TG |
4015 | |
4016 | #ifdef CONFIG_PREEMPT_RT | |
4017 | /* | |
4018 | * Saved state preserves the task state across blocking on | |
4019 | * an RT lock. If the state matches, set p::saved_state to | |
4020 | * TASK_RUNNING, but do not wake the task because it waits | |
4021 | * for a lock wakeup. Also indicate success because from | |
4022 | * the regular waker's point of view this has succeeded. | |
4023 | * | |
4024 | * After acquiring the lock the task will restore p::__state | |
4025 | * from p::saved_state which ensures that the regular | |
4026 | * wakeup is not lost. The restore will also set | |
4027 | * p::saved_state to TASK_RUNNING so any further tests will | |
4028 | * not result in false positives vs. @success | |
4029 | */ | |
4030 | if (p->saved_state & state) { | |
4031 | p->saved_state = TASK_RUNNING; | |
4032 | *success = 1; | |
4033 | } | |
4034 | #endif | |
43295d73 TG |
4035 | return false; |
4036 | } | |
4037 | ||
8643cda5 PZ |
4038 | /* |
4039 | * Notes on Program-Order guarantees on SMP systems. | |
4040 | * | |
4041 | * MIGRATION | |
4042 | * | |
4043 | * The basic program-order guarantee on SMP systems is that when a task [t] | |
d1ccc66d IM |
4044 | * migrates, all its activity on its old CPU [c0] happens-before any subsequent |
4045 | * execution on its new CPU [c1]. | |
8643cda5 PZ |
4046 | * |
4047 | * For migration (of runnable tasks) this is provided by the following means: | |
4048 | * | |
4049 | * A) UNLOCK of the rq(c0)->lock scheduling out task t | |
4050 | * B) migration for t is required to synchronize *both* rq(c0)->lock and | |
4051 | * rq(c1)->lock (if not at the same time, then in that order). | |
4052 | * C) LOCK of the rq(c1)->lock scheduling in task | |
4053 | * | |
7696f991 | 4054 | * Release/acquire chaining guarantees that B happens after A and C after B. |
d1ccc66d | 4055 | * Note: the CPU doing B need not be c0 or c1 |
8643cda5 PZ |
4056 | * |
4057 | * Example: | |
4058 | * | |
4059 | * CPU0 CPU1 CPU2 | |
4060 | * | |
4061 | * LOCK rq(0)->lock | |
4062 | * sched-out X | |
4063 | * sched-in Y | |
4064 | * UNLOCK rq(0)->lock | |
4065 | * | |
4066 | * LOCK rq(0)->lock // orders against CPU0 | |
4067 | * dequeue X | |
4068 | * UNLOCK rq(0)->lock | |
4069 | * | |
4070 | * LOCK rq(1)->lock | |
4071 | * enqueue X | |
4072 | * UNLOCK rq(1)->lock | |
4073 | * | |
4074 | * LOCK rq(1)->lock // orders against CPU2 | |
4075 | * sched-out Z | |
4076 | * sched-in X | |
4077 | * UNLOCK rq(1)->lock | |
4078 | * | |
4079 | * | |
4080 | * BLOCKING -- aka. SLEEP + WAKEUP | |
4081 | * | |
4082 | * For blocking we (obviously) need to provide the same guarantee as for | |
4083 | * migration. However the means are completely different as there is no lock | |
4084 | * chain to provide order. Instead we do: | |
4085 | * | |
58877d34 PZ |
4086 | * 1) smp_store_release(X->on_cpu, 0) -- finish_task() |
4087 | * 2) smp_cond_load_acquire(!X->on_cpu) -- try_to_wake_up() | |
8643cda5 PZ |
4088 | * |
4089 | * Example: | |
4090 | * | |
4091 | * CPU0 (schedule) CPU1 (try_to_wake_up) CPU2 (schedule) | |
4092 | * | |
4093 | * LOCK rq(0)->lock LOCK X->pi_lock | |
4094 | * dequeue X | |
4095 | * sched-out X | |
4096 | * smp_store_release(X->on_cpu, 0); | |
4097 | * | |
1f03e8d2 | 4098 | * smp_cond_load_acquire(&X->on_cpu, !VAL); |
8643cda5 PZ |
4099 | * X->state = WAKING |
4100 | * set_task_cpu(X,2) | |
4101 | * | |
4102 | * LOCK rq(2)->lock | |
4103 | * enqueue X | |
4104 | * X->state = RUNNING | |
4105 | * UNLOCK rq(2)->lock | |
4106 | * | |
4107 | * LOCK rq(2)->lock // orders against CPU1 | |
4108 | * sched-out Z | |
4109 | * sched-in X | |
4110 | * UNLOCK rq(2)->lock | |
4111 | * | |
4112 | * UNLOCK X->pi_lock | |
4113 | * UNLOCK rq(0)->lock | |
4114 | * | |
4115 | * | |
7696f991 AP |
4116 | * However, for wakeups there is a second guarantee we must provide, namely we |
4117 | * must ensure that CONDITION=1 done by the caller can not be reordered with | |
4118 | * accesses to the task state; see try_to_wake_up() and set_current_state(). | |
8643cda5 PZ |
4119 | */ |
4120 | ||
9ed3811a | 4121 | /** |
1da177e4 | 4122 | * try_to_wake_up - wake up a thread |
9ed3811a | 4123 | * @p: the thread to be awakened |
1da177e4 | 4124 | * @state: the mask of task states that can be woken |
9ed3811a | 4125 | * @wake_flags: wake modifier flags (WF_*) |
1da177e4 | 4126 | * |
58877d34 PZ |
4127 | * Conceptually does: |
4128 | * | |
4129 | * If (@state & @p->state) @p->state = TASK_RUNNING. | |
1da177e4 | 4130 | * |
a2250238 PZ |
4131 | * If the task was not queued/runnable, also place it back on a runqueue. |
4132 | * | |
58877d34 PZ |
4133 | * This function is atomic against schedule() which would dequeue the task. |
4134 | * | |
4135 | * It issues a full memory barrier before accessing @p->state, see the comment | |
4136 | * with set_current_state(). | |
a2250238 | 4137 | * |
58877d34 | 4138 | * Uses p->pi_lock to serialize against concurrent wake-ups. |
a2250238 | 4139 | * |
58877d34 PZ |
4140 | * Relies on p->pi_lock stabilizing: |
4141 | * - p->sched_class | |
4142 | * - p->cpus_ptr | |
4143 | * - p->sched_task_group | |
4144 | * in order to do migration, see its use of select_task_rq()/set_task_cpu(). | |
4145 | * | |
4146 | * Tries really hard to only take one task_rq(p)->lock for performance. | |
4147 | * Takes rq->lock in: | |
4148 | * - ttwu_runnable() -- old rq, unavoidable, see comment there; | |
4149 | * - ttwu_queue() -- new rq, for enqueue of the task; | |
4150 | * - psi_ttwu_dequeue() -- much sadness :-( accounting will kill us. | |
4151 | * | |
4152 | * As a consequence we race really badly with just about everything. See the | |
4153 | * many memory barriers and their comments for details. | |
7696f991 | 4154 | * |
a2250238 PZ |
4155 | * Return: %true if @p->state changes (an actual wakeup was done), |
4156 | * %false otherwise. | |
1da177e4 | 4157 | */ |
e4a52bcb PZ |
4158 | static int |
4159 | try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags) | |
1da177e4 | 4160 | { |
1da177e4 | 4161 | unsigned long flags; |
c05fbafb | 4162 | int cpu, success = 0; |
2398f2c6 | 4163 | |
e3d85487 | 4164 | preempt_disable(); |
aacedf26 PZ |
4165 | if (p == current) { |
4166 | /* | |
4167 | * We're waking current, this means 'p->on_rq' and 'task_cpu(p) | |
4168 | * == smp_processor_id()'. Together this means we can special | |
58877d34 | 4169 | * case the whole 'p->on_rq && ttwu_runnable()' case below |
aacedf26 PZ |
4170 | * without taking any locks. |
4171 | * | |
4172 | * In particular: | |
4173 | * - we rely on Program-Order guarantees for all the ordering, | |
4174 | * - we're serialized against set_special_state() by virtue of | |
4175 | * it disabling IRQs (this allows not taking ->pi_lock). | |
4176 | */ | |
43295d73 | 4177 | if (!ttwu_state_match(p, state, &success)) |
e3d85487 | 4178 | goto out; |
aacedf26 | 4179 | |
aacedf26 | 4180 | trace_sched_waking(p); |
160fb0d8 | 4181 | ttwu_do_wakeup(p); |
aacedf26 PZ |
4182 | goto out; |
4183 | } | |
4184 | ||
e0acd0a6 ON |
4185 | /* |
4186 | * If we are going to wake up a thread waiting for CONDITION we | |
4187 | * need to ensure that CONDITION=1 done by the caller can not be | |
58877d34 PZ |
4188 | * reordered with p->state check below. This pairs with smp_store_mb() |
4189 | * in set_current_state() that the waiting thread does. | |
e0acd0a6 | 4190 | */ |
013fdb80 | 4191 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
d89e588c | 4192 | smp_mb__after_spinlock(); |
43295d73 | 4193 | if (!ttwu_state_match(p, state, &success)) |
aacedf26 | 4194 | goto unlock; |
1da177e4 | 4195 | |
fbd705a0 PZ |
4196 | trace_sched_waking(p); |
4197 | ||
135e8c92 BS |
4198 | /* |
4199 | * Ensure we load p->on_rq _after_ p->state, otherwise it would | |
4200 | * be possible to, falsely, observe p->on_rq == 0 and get stuck | |
4201 | * in smp_cond_load_acquire() below. | |
4202 | * | |
3d85b270 AP |
4203 | * sched_ttwu_pending() try_to_wake_up() |
4204 | * STORE p->on_rq = 1 LOAD p->state | |
4205 | * UNLOCK rq->lock | |
4206 | * | |
4207 | * __schedule() (switch to task 'p') | |
4208 | * LOCK rq->lock smp_rmb(); | |
4209 | * smp_mb__after_spinlock(); | |
4210 | * UNLOCK rq->lock | |
135e8c92 BS |
4211 | * |
4212 | * [task p] | |
3d85b270 | 4213 | * STORE p->state = UNINTERRUPTIBLE LOAD p->on_rq |
135e8c92 | 4214 | * |
3d85b270 AP |
4215 | * Pairs with the LOCK+smp_mb__after_spinlock() on rq->lock in |
4216 | * __schedule(). See the comment for smp_mb__after_spinlock(). | |
2beaf328 PM |
4217 | * |
4218 | * A similar smb_rmb() lives in try_invoke_on_locked_down_task(). | |
135e8c92 BS |
4219 | */ |
4220 | smp_rmb(); | |
58877d34 | 4221 | if (READ_ONCE(p->on_rq) && ttwu_runnable(p, wake_flags)) |
aacedf26 | 4222 | goto unlock; |
1da177e4 | 4223 | |
1da177e4 | 4224 | #ifdef CONFIG_SMP |
ecf7d01c PZ |
4225 | /* |
4226 | * Ensure we load p->on_cpu _after_ p->on_rq, otherwise it would be | |
4227 | * possible to, falsely, observe p->on_cpu == 0. | |
4228 | * | |
4229 | * One must be running (->on_cpu == 1) in order to remove oneself | |
4230 | * from the runqueue. | |
4231 | * | |
3d85b270 AP |
4232 | * __schedule() (switch to task 'p') try_to_wake_up() |
4233 | * STORE p->on_cpu = 1 LOAD p->on_rq | |
4234 | * UNLOCK rq->lock | |
4235 | * | |
4236 | * __schedule() (put 'p' to sleep) | |
4237 | * LOCK rq->lock smp_rmb(); | |
4238 | * smp_mb__after_spinlock(); | |
4239 | * STORE p->on_rq = 0 LOAD p->on_cpu | |
ecf7d01c | 4240 | * |
3d85b270 AP |
4241 | * Pairs with the LOCK+smp_mb__after_spinlock() on rq->lock in |
4242 | * __schedule(). See the comment for smp_mb__after_spinlock(). | |
dbfb089d PZ |
4243 | * |
4244 | * Form a control-dep-acquire with p->on_rq == 0 above, to ensure | |
4245 | * schedule()'s deactivate_task() has 'happened' and p will no longer | |
4246 | * care about it's own p->state. See the comment in __schedule(). | |
ecf7d01c | 4247 | */ |
dbfb089d PZ |
4248 | smp_acquire__after_ctrl_dep(); |
4249 | ||
4250 | /* | |
4251 | * We're doing the wakeup (@success == 1), they did a dequeue (p->on_rq | |
4252 | * == 0), which means we need to do an enqueue, change p->state to | |
4253 | * TASK_WAKING such that we can unlock p->pi_lock before doing the | |
4254 | * enqueue, such as ttwu_queue_wakelist(). | |
4255 | */ | |
2f064a59 | 4256 | WRITE_ONCE(p->__state, TASK_WAKING); |
ecf7d01c | 4257 | |
c6e7bd7a PZ |
4258 | /* |
4259 | * If the owning (remote) CPU is still in the middle of schedule() with | |
4260 | * this task as prev, considering queueing p on the remote CPUs wake_list | |
4261 | * which potentially sends an IPI instead of spinning on p->on_cpu to | |
4262 | * let the waker make forward progress. This is safe because IRQs are | |
4263 | * disabled and the IPI will deliver after on_cpu is cleared. | |
b6e13e85 PZ |
4264 | * |
4265 | * Ensure we load task_cpu(p) after p->on_cpu: | |
4266 | * | |
4267 | * set_task_cpu(p, cpu); | |
4268 | * STORE p->cpu = @cpu | |
4269 | * __schedule() (switch to task 'p') | |
4270 | * LOCK rq->lock | |
4271 | * smp_mb__after_spin_lock() smp_cond_load_acquire(&p->on_cpu) | |
4272 | * STORE p->on_cpu = 1 LOAD p->cpu | |
4273 | * | |
4274 | * to ensure we observe the correct CPU on which the task is currently | |
4275 | * scheduling. | |
c6e7bd7a | 4276 | */ |
b6e13e85 | 4277 | if (smp_load_acquire(&p->on_cpu) && |
f3dd3f67 | 4278 | ttwu_queue_wakelist(p, task_cpu(p), wake_flags)) |
c6e7bd7a PZ |
4279 | goto unlock; |
4280 | ||
e9c84311 | 4281 | /* |
d1ccc66d | 4282 | * If the owning (remote) CPU is still in the middle of schedule() with |
b19a888c | 4283 | * this task as prev, wait until it's done referencing the task. |
b75a2253 | 4284 | * |
31cb1bc0 | 4285 | * Pairs with the smp_store_release() in finish_task(). |
b75a2253 PZ |
4286 | * |
4287 | * This ensures that tasks getting woken will be fully ordered against | |
4288 | * their previous state and preserve Program Order. | |
0970d299 | 4289 | */ |
1f03e8d2 | 4290 | smp_cond_load_acquire(&p->on_cpu, !VAL); |
1da177e4 | 4291 | |
3aef1551 | 4292 | cpu = select_task_rq(p, p->wake_cpu, wake_flags | WF_TTWU); |
f339b9dc | 4293 | if (task_cpu(p) != cpu) { |
ec618b84 PZ |
4294 | if (p->in_iowait) { |
4295 | delayacct_blkio_end(p); | |
4296 | atomic_dec(&task_rq(p)->nr_iowait); | |
4297 | } | |
4298 | ||
f339b9dc | 4299 | wake_flags |= WF_MIGRATED; |
eb414681 | 4300 | psi_ttwu_dequeue(p); |
e4a52bcb | 4301 | set_task_cpu(p, cpu); |
f339b9dc | 4302 | } |
b6e13e85 PZ |
4303 | #else |
4304 | cpu = task_cpu(p); | |
1da177e4 | 4305 | #endif /* CONFIG_SMP */ |
1da177e4 | 4306 | |
b5179ac7 | 4307 | ttwu_queue(p, cpu, wake_flags); |
aacedf26 | 4308 | unlock: |
013fdb80 | 4309 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
aacedf26 PZ |
4310 | out: |
4311 | if (success) | |
b6e13e85 | 4312 | ttwu_stat(p, task_cpu(p), wake_flags); |
e3d85487 | 4313 | preempt_enable(); |
1da177e4 LT |
4314 | |
4315 | return success; | |
4316 | } | |
4317 | ||
91dabf33 PZ |
4318 | static bool __task_needs_rq_lock(struct task_struct *p) |
4319 | { | |
4320 | unsigned int state = READ_ONCE(p->__state); | |
4321 | ||
4322 | /* | |
4323 | * Since pi->lock blocks try_to_wake_up(), we don't need rq->lock when | |
4324 | * the task is blocked. Make sure to check @state since ttwu() can drop | |
4325 | * locks at the end, see ttwu_queue_wakelist(). | |
4326 | */ | |
4327 | if (state == TASK_RUNNING || state == TASK_WAKING) | |
4328 | return true; | |
4329 | ||
4330 | /* | |
4331 | * Ensure we load p->on_rq after p->__state, otherwise it would be | |
4332 | * possible to, falsely, observe p->on_rq == 0. | |
4333 | * | |
4334 | * See try_to_wake_up() for a longer comment. | |
4335 | */ | |
4336 | smp_rmb(); | |
4337 | if (p->on_rq) | |
4338 | return true; | |
4339 | ||
4340 | #ifdef CONFIG_SMP | |
4341 | /* | |
4342 | * Ensure the task has finished __schedule() and will not be referenced | |
4343 | * anymore. Again, see try_to_wake_up() for a longer comment. | |
4344 | */ | |
4345 | smp_rmb(); | |
4346 | smp_cond_load_acquire(&p->on_cpu, !VAL); | |
4347 | #endif | |
4348 | ||
4349 | return false; | |
4350 | } | |
4351 | ||
2beaf328 | 4352 | /** |
9b3c4ab3 | 4353 | * task_call_func - Invoke a function on task in fixed state |
1b7af295 | 4354 | * @p: Process for which the function is to be invoked, can be @current. |
2beaf328 PM |
4355 | * @func: Function to invoke. |
4356 | * @arg: Argument to function. | |
4357 | * | |
f6ac18fa PZ |
4358 | * Fix the task in it's current state by avoiding wakeups and or rq operations |
4359 | * and call @func(@arg) on it. This function can use ->on_rq and task_curr() | |
4360 | * to work out what the state is, if required. Given that @func can be invoked | |
4361 | * with a runqueue lock held, it had better be quite lightweight. | |
2beaf328 PM |
4362 | * |
4363 | * Returns: | |
f6ac18fa | 4364 | * Whatever @func returns |
2beaf328 | 4365 | */ |
9b3c4ab3 | 4366 | int task_call_func(struct task_struct *p, task_call_f func, void *arg) |
2beaf328 | 4367 | { |
f6ac18fa | 4368 | struct rq *rq = NULL; |
2beaf328 | 4369 | struct rq_flags rf; |
9b3c4ab3 | 4370 | int ret; |
2beaf328 | 4371 | |
1b7af295 | 4372 | raw_spin_lock_irqsave(&p->pi_lock, rf.flags); |
f6ac18fa | 4373 | |
91dabf33 | 4374 | if (__task_needs_rq_lock(p)) |
2beaf328 | 4375 | rq = __task_rq_lock(p, &rf); |
f6ac18fa PZ |
4376 | |
4377 | /* | |
4378 | * At this point the task is pinned; either: | |
4379 | * - blocked and we're holding off wakeups (pi->lock) | |
4380 | * - woken, and we're holding off enqueue (rq->lock) | |
4381 | * - queued, and we're holding off schedule (rq->lock) | |
4382 | * - running, and we're holding off de-schedule (rq->lock) | |
4383 | * | |
4384 | * The called function (@func) can use: task_curr(), p->on_rq and | |
4385 | * p->__state to differentiate between these states. | |
4386 | */ | |
4387 | ret = func(p, arg); | |
4388 | ||
4389 | if (rq) | |
2beaf328 | 4390 | rq_unlock(rq, &rf); |
f6ac18fa | 4391 | |
1b7af295 | 4392 | raw_spin_unlock_irqrestore(&p->pi_lock, rf.flags); |
2beaf328 PM |
4393 | return ret; |
4394 | } | |
4395 | ||
e386b672 PM |
4396 | /** |
4397 | * cpu_curr_snapshot - Return a snapshot of the currently running task | |
4398 | * @cpu: The CPU on which to snapshot the task. | |
4399 | * | |
4400 | * Returns the task_struct pointer of the task "currently" running on | |
4401 | * the specified CPU. If the same task is running on that CPU throughout, | |
4402 | * the return value will be a pointer to that task's task_struct structure. | |
4403 | * If the CPU did any context switches even vaguely concurrently with the | |
4404 | * execution of this function, the return value will be a pointer to the | |
4405 | * task_struct structure of a randomly chosen task that was running on | |
4406 | * that CPU somewhere around the time that this function was executing. | |
4407 | * | |
4408 | * If the specified CPU was offline, the return value is whatever it | |
4409 | * is, perhaps a pointer to the task_struct structure of that CPU's idle | |
4410 | * task, but there is no guarantee. Callers wishing a useful return | |
4411 | * value must take some action to ensure that the specified CPU remains | |
4412 | * online throughout. | |
4413 | * | |
4414 | * This function executes full memory barriers before and after fetching | |
4415 | * the pointer, which permits the caller to confine this function's fetch | |
4416 | * with respect to the caller's accesses to other shared variables. | |
4417 | */ | |
4418 | struct task_struct *cpu_curr_snapshot(int cpu) | |
4419 | { | |
4420 | struct task_struct *t; | |
4421 | ||
4422 | smp_mb(); /* Pairing determined by caller's synchronization design. */ | |
4423 | t = rcu_dereference(cpu_curr(cpu)); | |
4424 | smp_mb(); /* Pairing determined by caller's synchronization design. */ | |
4425 | return t; | |
4426 | } | |
4427 | ||
50fa610a DH |
4428 | /** |
4429 | * wake_up_process - Wake up a specific process | |
4430 | * @p: The process to be woken up. | |
4431 | * | |
4432 | * Attempt to wake up the nominated process and move it to the set of runnable | |
e69f6186 YB |
4433 | * processes. |
4434 | * | |
4435 | * Return: 1 if the process was woken up, 0 if it was already running. | |
50fa610a | 4436 | * |
7696f991 | 4437 | * This function executes a full memory barrier before accessing the task state. |
50fa610a | 4438 | */ |
7ad5b3a5 | 4439 | int wake_up_process(struct task_struct *p) |
1da177e4 | 4440 | { |
9067ac85 | 4441 | return try_to_wake_up(p, TASK_NORMAL, 0); |
1da177e4 | 4442 | } |
1da177e4 LT |
4443 | EXPORT_SYMBOL(wake_up_process); |
4444 | ||
7ad5b3a5 | 4445 | int wake_up_state(struct task_struct *p, unsigned int state) |
1da177e4 LT |
4446 | { |
4447 | return try_to_wake_up(p, state, 0); | |
4448 | } | |
4449 | ||
1da177e4 LT |
4450 | /* |
4451 | * Perform scheduler related setup for a newly forked process p. | |
4452 | * p is forked by current. | |
dd41f596 IM |
4453 | * |
4454 | * __sched_fork() is basic setup used by init_idle() too: | |
4455 | */ | |
5e1576ed | 4456 | static void __sched_fork(unsigned long clone_flags, struct task_struct *p) |
dd41f596 | 4457 | { |
fd2f4419 PZ |
4458 | p->on_rq = 0; |
4459 | ||
4460 | p->se.on_rq = 0; | |
dd41f596 IM |
4461 | p->se.exec_start = 0; |
4462 | p->se.sum_exec_runtime = 0; | |
f6cf891c | 4463 | p->se.prev_sum_exec_runtime = 0; |
6c594c21 | 4464 | p->se.nr_migrations = 0; |
da7a735e | 4465 | p->se.vruntime = 0; |
fd2f4419 | 4466 | INIT_LIST_HEAD(&p->se.group_node); |
6cfb0d5d | 4467 | |
ad936d86 BP |
4468 | #ifdef CONFIG_FAIR_GROUP_SCHED |
4469 | p->se.cfs_rq = NULL; | |
4470 | #endif | |
4471 | ||
6cfb0d5d | 4472 | #ifdef CONFIG_SCHEDSTATS |
cb251765 | 4473 | /* Even if schedstat is disabled, there should not be garbage */ |
ceeadb83 | 4474 | memset(&p->stats, 0, sizeof(p->stats)); |
6cfb0d5d | 4475 | #endif |
476d139c | 4476 | |
aab03e05 | 4477 | RB_CLEAR_NODE(&p->dl.rb_node); |
40767b0d | 4478 | init_dl_task_timer(&p->dl); |
209a0cbd | 4479 | init_dl_inactive_task_timer(&p->dl); |
a5e7be3b | 4480 | __dl_clear_params(p); |
aab03e05 | 4481 | |
fa717060 | 4482 | INIT_LIST_HEAD(&p->rt.run_list); |
ff77e468 PZ |
4483 | p->rt.timeout = 0; |
4484 | p->rt.time_slice = sched_rr_timeslice; | |
4485 | p->rt.on_rq = 0; | |
4486 | p->rt.on_list = 0; | |
476d139c | 4487 | |
e107be36 AK |
4488 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
4489 | INIT_HLIST_HEAD(&p->preempt_notifiers); | |
4490 | #endif | |
cbee9f88 | 4491 | |
5e1f0f09 MG |
4492 | #ifdef CONFIG_COMPACTION |
4493 | p->capture_control = NULL; | |
4494 | #endif | |
13784475 | 4495 | init_numa_balancing(clone_flags, p); |
a1488664 | 4496 | #ifdef CONFIG_SMP |
8c4890d1 | 4497 | p->wake_entry.u_flags = CSD_TYPE_TTWU; |
6d337eab | 4498 | p->migration_pending = NULL; |
a1488664 | 4499 | #endif |
223baf9d | 4500 | init_sched_mm_cid(p); |
dd41f596 IM |
4501 | } |
4502 | ||
2a595721 SD |
4503 | DEFINE_STATIC_KEY_FALSE(sched_numa_balancing); |
4504 | ||
1a687c2e | 4505 | #ifdef CONFIG_NUMA_BALANCING |
c3b9bc5b | 4506 | |
c574bbe9 HY |
4507 | int sysctl_numa_balancing_mode; |
4508 | ||
4509 | static void __set_numabalancing_state(bool enabled) | |
1a687c2e MG |
4510 | { |
4511 | if (enabled) | |
2a595721 | 4512 | static_branch_enable(&sched_numa_balancing); |
1a687c2e | 4513 | else |
2a595721 | 4514 | static_branch_disable(&sched_numa_balancing); |
1a687c2e | 4515 | } |
54a43d54 | 4516 | |
c574bbe9 HY |
4517 | void set_numabalancing_state(bool enabled) |
4518 | { | |
4519 | if (enabled) | |
4520 | sysctl_numa_balancing_mode = NUMA_BALANCING_NORMAL; | |
4521 | else | |
4522 | sysctl_numa_balancing_mode = NUMA_BALANCING_DISABLED; | |
4523 | __set_numabalancing_state(enabled); | |
4524 | } | |
4525 | ||
54a43d54 | 4526 | #ifdef CONFIG_PROC_SYSCTL |
c959924b HY |
4527 | static void reset_memory_tiering(void) |
4528 | { | |
4529 | struct pglist_data *pgdat; | |
4530 | ||
4531 | for_each_online_pgdat(pgdat) { | |
4532 | pgdat->nbp_threshold = 0; | |
4533 | pgdat->nbp_th_nr_cand = node_page_state(pgdat, PGPROMOTE_CANDIDATE); | |
4534 | pgdat->nbp_th_start = jiffies_to_msecs(jiffies); | |
4535 | } | |
4536 | } | |
4537 | ||
0dff89c4 | 4538 | static int sysctl_numa_balancing(struct ctl_table *table, int write, |
32927393 | 4539 | void *buffer, size_t *lenp, loff_t *ppos) |
54a43d54 AK |
4540 | { |
4541 | struct ctl_table t; | |
4542 | int err; | |
c574bbe9 | 4543 | int state = sysctl_numa_balancing_mode; |
54a43d54 AK |
4544 | |
4545 | if (write && !capable(CAP_SYS_ADMIN)) | |
4546 | return -EPERM; | |
4547 | ||
4548 | t = *table; | |
4549 | t.data = &state; | |
4550 | err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos); | |
4551 | if (err < 0) | |
4552 | return err; | |
c574bbe9 | 4553 | if (write) { |
c959924b HY |
4554 | if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) && |
4555 | (state & NUMA_BALANCING_MEMORY_TIERING)) | |
4556 | reset_memory_tiering(); | |
c574bbe9 HY |
4557 | sysctl_numa_balancing_mode = state; |
4558 | __set_numabalancing_state(state); | |
4559 | } | |
54a43d54 AK |
4560 | return err; |
4561 | } | |
4562 | #endif | |
4563 | #endif | |
dd41f596 | 4564 | |
4698f88c JP |
4565 | #ifdef CONFIG_SCHEDSTATS |
4566 | ||
cb251765 MG |
4567 | DEFINE_STATIC_KEY_FALSE(sched_schedstats); |
4568 | ||
cb251765 MG |
4569 | static void set_schedstats(bool enabled) |
4570 | { | |
4571 | if (enabled) | |
4572 | static_branch_enable(&sched_schedstats); | |
4573 | else | |
4574 | static_branch_disable(&sched_schedstats); | |
4575 | } | |
4576 | ||
4577 | void force_schedstat_enabled(void) | |
4578 | { | |
4579 | if (!schedstat_enabled()) { | |
4580 | pr_info("kernel profiling enabled schedstats, disable via kernel.sched_schedstats.\n"); | |
4581 | static_branch_enable(&sched_schedstats); | |
4582 | } | |
4583 | } | |
4584 | ||
4585 | static int __init setup_schedstats(char *str) | |
4586 | { | |
4587 | int ret = 0; | |
4588 | if (!str) | |
4589 | goto out; | |
4590 | ||
4591 | if (!strcmp(str, "enable")) { | |
1faa491a | 4592 | set_schedstats(true); |
cb251765 MG |
4593 | ret = 1; |
4594 | } else if (!strcmp(str, "disable")) { | |
1faa491a | 4595 | set_schedstats(false); |
cb251765 MG |
4596 | ret = 1; |
4597 | } | |
4598 | out: | |
4599 | if (!ret) | |
4600 | pr_warn("Unable to parse schedstats=\n"); | |
4601 | ||
4602 | return ret; | |
4603 | } | |
4604 | __setup("schedstats=", setup_schedstats); | |
4605 | ||
4606 | #ifdef CONFIG_PROC_SYSCTL | |
f5ef06d5 | 4607 | static int sysctl_schedstats(struct ctl_table *table, int write, void *buffer, |
32927393 | 4608 | size_t *lenp, loff_t *ppos) |
cb251765 MG |
4609 | { |
4610 | struct ctl_table t; | |
4611 | int err; | |
4612 | int state = static_branch_likely(&sched_schedstats); | |
4613 | ||
4614 | if (write && !capable(CAP_SYS_ADMIN)) | |
4615 | return -EPERM; | |
4616 | ||
4617 | t = *table; | |
4618 | t.data = &state; | |
4619 | err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos); | |
4620 | if (err < 0) | |
4621 | return err; | |
4622 | if (write) | |
4623 | set_schedstats(state); | |
4624 | return err; | |
4625 | } | |
4698f88c | 4626 | #endif /* CONFIG_PROC_SYSCTL */ |
4698f88c | 4627 | #endif /* CONFIG_SCHEDSTATS */ |
dd41f596 | 4628 | |
3267e015 ZN |
4629 | #ifdef CONFIG_SYSCTL |
4630 | static struct ctl_table sched_core_sysctls[] = { | |
4631 | #ifdef CONFIG_SCHEDSTATS | |
f5ef06d5 ZN |
4632 | { |
4633 | .procname = "sched_schedstats", | |
4634 | .data = NULL, | |
4635 | .maxlen = sizeof(unsigned int), | |
4636 | .mode = 0644, | |
4637 | .proc_handler = sysctl_schedstats, | |
4638 | .extra1 = SYSCTL_ZERO, | |
4639 | .extra2 = SYSCTL_ONE, | |
4640 | }, | |
3267e015 ZN |
4641 | #endif /* CONFIG_SCHEDSTATS */ |
4642 | #ifdef CONFIG_UCLAMP_TASK | |
4643 | { | |
4644 | .procname = "sched_util_clamp_min", | |
4645 | .data = &sysctl_sched_uclamp_util_min, | |
4646 | .maxlen = sizeof(unsigned int), | |
4647 | .mode = 0644, | |
4648 | .proc_handler = sysctl_sched_uclamp_handler, | |
4649 | }, | |
4650 | { | |
4651 | .procname = "sched_util_clamp_max", | |
4652 | .data = &sysctl_sched_uclamp_util_max, | |
4653 | .maxlen = sizeof(unsigned int), | |
4654 | .mode = 0644, | |
4655 | .proc_handler = sysctl_sched_uclamp_handler, | |
4656 | }, | |
4657 | { | |
4658 | .procname = "sched_util_clamp_min_rt_default", | |
4659 | .data = &sysctl_sched_uclamp_util_min_rt_default, | |
4660 | .maxlen = sizeof(unsigned int), | |
4661 | .mode = 0644, | |
4662 | .proc_handler = sysctl_sched_uclamp_handler, | |
4663 | }, | |
4664 | #endif /* CONFIG_UCLAMP_TASK */ | |
0dff89c4 KW |
4665 | #ifdef CONFIG_NUMA_BALANCING |
4666 | { | |
4667 | .procname = "numa_balancing", | |
4668 | .data = NULL, /* filled in by handler */ | |
4669 | .maxlen = sizeof(unsigned int), | |
4670 | .mode = 0644, | |
4671 | .proc_handler = sysctl_numa_balancing, | |
4672 | .extra1 = SYSCTL_ZERO, | |
4673 | .extra2 = SYSCTL_FOUR, | |
4674 | }, | |
4675 | #endif /* CONFIG_NUMA_BALANCING */ | |
f5ef06d5 ZN |
4676 | {} |
4677 | }; | |
3267e015 | 4678 | static int __init sched_core_sysctl_init(void) |
f5ef06d5 | 4679 | { |
3267e015 | 4680 | register_sysctl_init("kernel", sched_core_sysctls); |
f5ef06d5 ZN |
4681 | return 0; |
4682 | } | |
3267e015 ZN |
4683 | late_initcall(sched_core_sysctl_init); |
4684 | #endif /* CONFIG_SYSCTL */ | |
dd41f596 IM |
4685 | |
4686 | /* | |
4687 | * fork()/clone()-time setup: | |
4688 | */ | |
aab03e05 | 4689 | int sched_fork(unsigned long clone_flags, struct task_struct *p) |
dd41f596 | 4690 | { |
5e1576ed | 4691 | __sched_fork(clone_flags, p); |
06b83b5f | 4692 | /* |
7dc603c9 | 4693 | * We mark the process as NEW here. This guarantees that |
06b83b5f PZ |
4694 | * nobody will actually run it, and a signal or other external |
4695 | * event cannot wake it up and insert it on the runqueue either. | |
4696 | */ | |
2f064a59 | 4697 | p->__state = TASK_NEW; |
dd41f596 | 4698 | |
c350a04e MG |
4699 | /* |
4700 | * Make sure we do not leak PI boosting priority to the child. | |
4701 | */ | |
4702 | p->prio = current->normal_prio; | |
4703 | ||
e8f14172 PB |
4704 | uclamp_fork(p); |
4705 | ||
b9dc29e7 MG |
4706 | /* |
4707 | * Revert to default priority/policy on fork if requested. | |
4708 | */ | |
4709 | if (unlikely(p->sched_reset_on_fork)) { | |
aab03e05 | 4710 | if (task_has_dl_policy(p) || task_has_rt_policy(p)) { |
b9dc29e7 | 4711 | p->policy = SCHED_NORMAL; |
6c697bdf | 4712 | p->static_prio = NICE_TO_PRIO(0); |
c350a04e MG |
4713 | p->rt_priority = 0; |
4714 | } else if (PRIO_TO_NICE(p->static_prio) < 0) | |
4715 | p->static_prio = NICE_TO_PRIO(0); | |
4716 | ||
f558c2b8 | 4717 | p->prio = p->normal_prio = p->static_prio; |
b1e82065 | 4718 | set_load_weight(p, false); |
6c697bdf | 4719 | |
b9dc29e7 MG |
4720 | /* |
4721 | * We don't need the reset flag anymore after the fork. It has | |
4722 | * fulfilled its duty: | |
4723 | */ | |
4724 | p->sched_reset_on_fork = 0; | |
4725 | } | |
ca94c442 | 4726 | |
af0fffd9 | 4727 | if (dl_prio(p->prio)) |
aab03e05 | 4728 | return -EAGAIN; |
af0fffd9 | 4729 | else if (rt_prio(p->prio)) |
aab03e05 | 4730 | p->sched_class = &rt_sched_class; |
af0fffd9 | 4731 | else |
2ddbf952 | 4732 | p->sched_class = &fair_sched_class; |
b29739f9 | 4733 | |
7dc603c9 | 4734 | init_entity_runnable_average(&p->se); |
cd29fe6f | 4735 | |
b1e82065 | 4736 | |
f6db8347 | 4737 | #ifdef CONFIG_SCHED_INFO |
dd41f596 | 4738 | if (likely(sched_info_on())) |
52f17b6c | 4739 | memset(&p->sched_info, 0, sizeof(p->sched_info)); |
1da177e4 | 4740 | #endif |
3ca7a440 PZ |
4741 | #if defined(CONFIG_SMP) |
4742 | p->on_cpu = 0; | |
4866cde0 | 4743 | #endif |
01028747 | 4744 | init_task_preempt_count(p); |
806c09a7 | 4745 | #ifdef CONFIG_SMP |
917b627d | 4746 | plist_node_init(&p->pushable_tasks, MAX_PRIO); |
1baca4ce | 4747 | RB_CLEAR_NODE(&p->pushable_dl_tasks); |
806c09a7 | 4748 | #endif |
aab03e05 | 4749 | return 0; |
1da177e4 LT |
4750 | } |
4751 | ||
b1e82065 | 4752 | void sched_cgroup_fork(struct task_struct *p, struct kernel_clone_args *kargs) |
13685c4a | 4753 | { |
4ef0c5c6 | 4754 | unsigned long flags; |
4ef0c5c6 | 4755 | |
b1e82065 PZ |
4756 | /* |
4757 | * Because we're not yet on the pid-hash, p->pi_lock isn't strictly | |
4758 | * required yet, but lockdep gets upset if rules are violated. | |
4759 | */ | |
4ef0c5c6 ZQ |
4760 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
4761 | #ifdef CONFIG_CGROUP_SCHED | |
b1e82065 PZ |
4762 | if (1) { |
4763 | struct task_group *tg; | |
4764 | tg = container_of(kargs->cset->subsys[cpu_cgrp_id], | |
4765 | struct task_group, css); | |
4766 | tg = autogroup_task_group(p, tg); | |
4767 | p->sched_task_group = tg; | |
4768 | } | |
4ef0c5c6 ZQ |
4769 | #endif |
4770 | rseq_migrate(p); | |
4771 | /* | |
4772 | * We're setting the CPU for the first time, we don't migrate, | |
4773 | * so use __set_task_cpu(). | |
4774 | */ | |
4775 | __set_task_cpu(p, smp_processor_id()); | |
4776 | if (p->sched_class->task_fork) | |
4777 | p->sched_class->task_fork(p); | |
4778 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); | |
b1e82065 | 4779 | } |
4ef0c5c6 | 4780 | |
b1e82065 PZ |
4781 | void sched_post_fork(struct task_struct *p) |
4782 | { | |
13685c4a QY |
4783 | uclamp_post_fork(p); |
4784 | } | |
4785 | ||
332ac17e DF |
4786 | unsigned long to_ratio(u64 period, u64 runtime) |
4787 | { | |
4788 | if (runtime == RUNTIME_INF) | |
c52f14d3 | 4789 | return BW_UNIT; |
332ac17e DF |
4790 | |
4791 | /* | |
4792 | * Doing this here saves a lot of checks in all | |
4793 | * the calling paths, and returning zero seems | |
4794 | * safe for them anyway. | |
4795 | */ | |
4796 | if (period == 0) | |
4797 | return 0; | |
4798 | ||
c52f14d3 | 4799 | return div64_u64(runtime << BW_SHIFT, period); |
332ac17e DF |
4800 | } |
4801 | ||
1da177e4 LT |
4802 | /* |
4803 | * wake_up_new_task - wake up a newly created task for the first time. | |
4804 | * | |
4805 | * This function will do some initial scheduler statistics housekeeping | |
4806 | * that must be done for every newly created context, then puts the task | |
4807 | * on the runqueue and wakes it. | |
4808 | */ | |
3e51e3ed | 4809 | void wake_up_new_task(struct task_struct *p) |
1da177e4 | 4810 | { |
eb580751 | 4811 | struct rq_flags rf; |
dd41f596 | 4812 | struct rq *rq; |
fabf318e | 4813 | |
eb580751 | 4814 | raw_spin_lock_irqsave(&p->pi_lock, rf.flags); |
2f064a59 | 4815 | WRITE_ONCE(p->__state, TASK_RUNNING); |
fabf318e PZ |
4816 | #ifdef CONFIG_SMP |
4817 | /* | |
4818 | * Fork balancing, do it here and not earlier because: | |
3bd37062 | 4819 | * - cpus_ptr can change in the fork path |
d1ccc66d | 4820 | * - any previously selected CPU might disappear through hotplug |
e210bffd PZ |
4821 | * |
4822 | * Use __set_task_cpu() to avoid calling sched_class::migrate_task_rq, | |
4823 | * as we're not fully set-up yet. | |
fabf318e | 4824 | */ |
32e839dd | 4825 | p->recent_used_cpu = task_cpu(p); |
ce3614da | 4826 | rseq_migrate(p); |
3aef1551 | 4827 | __set_task_cpu(p, select_task_rq(p, task_cpu(p), WF_FORK)); |
0017d735 | 4828 | #endif |
b7fa30c9 | 4829 | rq = __task_rq_lock(p, &rf); |
4126bad6 | 4830 | update_rq_clock(rq); |
d0fe0b9c | 4831 | post_init_entity_util_avg(p); |
0017d735 | 4832 | |
7a57f32a | 4833 | activate_task(rq, p, ENQUEUE_NOCLOCK); |
fbd705a0 | 4834 | trace_sched_wakeup_new(p); |
a7558e01 | 4835 | check_preempt_curr(rq, p, WF_FORK); |
9a897c5a | 4836 | #ifdef CONFIG_SMP |
0aaafaab PZ |
4837 | if (p->sched_class->task_woken) { |
4838 | /* | |
b19a888c | 4839 | * Nothing relies on rq->lock after this, so it's fine to |
0aaafaab PZ |
4840 | * drop it. |
4841 | */ | |
d8ac8971 | 4842 | rq_unpin_lock(rq, &rf); |
efbbd05a | 4843 | p->sched_class->task_woken(rq, p); |
d8ac8971 | 4844 | rq_repin_lock(rq, &rf); |
0aaafaab | 4845 | } |
9a897c5a | 4846 | #endif |
eb580751 | 4847 | task_rq_unlock(rq, p, &rf); |
1da177e4 LT |
4848 | } |
4849 | ||
e107be36 AK |
4850 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
4851 | ||
b7203428 | 4852 | static DEFINE_STATIC_KEY_FALSE(preempt_notifier_key); |
1cde2930 | 4853 | |
2ecd9d29 PZ |
4854 | void preempt_notifier_inc(void) |
4855 | { | |
b7203428 | 4856 | static_branch_inc(&preempt_notifier_key); |
2ecd9d29 PZ |
4857 | } |
4858 | EXPORT_SYMBOL_GPL(preempt_notifier_inc); | |
4859 | ||
4860 | void preempt_notifier_dec(void) | |
4861 | { | |
b7203428 | 4862 | static_branch_dec(&preempt_notifier_key); |
2ecd9d29 PZ |
4863 | } |
4864 | EXPORT_SYMBOL_GPL(preempt_notifier_dec); | |
4865 | ||
e107be36 | 4866 | /** |
80dd99b3 | 4867 | * preempt_notifier_register - tell me when current is being preempted & rescheduled |
421cee29 | 4868 | * @notifier: notifier struct to register |
e107be36 AK |
4869 | */ |
4870 | void preempt_notifier_register(struct preempt_notifier *notifier) | |
4871 | { | |
b7203428 | 4872 | if (!static_branch_unlikely(&preempt_notifier_key)) |
2ecd9d29 PZ |
4873 | WARN(1, "registering preempt_notifier while notifiers disabled\n"); |
4874 | ||
e107be36 AK |
4875 | hlist_add_head(¬ifier->link, ¤t->preempt_notifiers); |
4876 | } | |
4877 | EXPORT_SYMBOL_GPL(preempt_notifier_register); | |
4878 | ||
4879 | /** | |
4880 | * preempt_notifier_unregister - no longer interested in preemption notifications | |
421cee29 | 4881 | * @notifier: notifier struct to unregister |
e107be36 | 4882 | * |
d84525a8 | 4883 | * This is *not* safe to call from within a preemption notifier. |
e107be36 AK |
4884 | */ |
4885 | void preempt_notifier_unregister(struct preempt_notifier *notifier) | |
4886 | { | |
4887 | hlist_del(¬ifier->link); | |
4888 | } | |
4889 | EXPORT_SYMBOL_GPL(preempt_notifier_unregister); | |
4890 | ||
1cde2930 | 4891 | static void __fire_sched_in_preempt_notifiers(struct task_struct *curr) |
e107be36 AK |
4892 | { |
4893 | struct preempt_notifier *notifier; | |
e107be36 | 4894 | |
b67bfe0d | 4895 | hlist_for_each_entry(notifier, &curr->preempt_notifiers, link) |
e107be36 AK |
4896 | notifier->ops->sched_in(notifier, raw_smp_processor_id()); |
4897 | } | |
4898 | ||
1cde2930 PZ |
4899 | static __always_inline void fire_sched_in_preempt_notifiers(struct task_struct *curr) |
4900 | { | |
b7203428 | 4901 | if (static_branch_unlikely(&preempt_notifier_key)) |
1cde2930 PZ |
4902 | __fire_sched_in_preempt_notifiers(curr); |
4903 | } | |
4904 | ||
e107be36 | 4905 | static void |
1cde2930 PZ |
4906 | __fire_sched_out_preempt_notifiers(struct task_struct *curr, |
4907 | struct task_struct *next) | |
e107be36 AK |
4908 | { |
4909 | struct preempt_notifier *notifier; | |
e107be36 | 4910 | |
b67bfe0d | 4911 | hlist_for_each_entry(notifier, &curr->preempt_notifiers, link) |
e107be36 AK |
4912 | notifier->ops->sched_out(notifier, next); |
4913 | } | |
4914 | ||
1cde2930 PZ |
4915 | static __always_inline void |
4916 | fire_sched_out_preempt_notifiers(struct task_struct *curr, | |
4917 | struct task_struct *next) | |
4918 | { | |
b7203428 | 4919 | if (static_branch_unlikely(&preempt_notifier_key)) |
1cde2930 PZ |
4920 | __fire_sched_out_preempt_notifiers(curr, next); |
4921 | } | |
4922 | ||
6d6bc0ad | 4923 | #else /* !CONFIG_PREEMPT_NOTIFIERS */ |
e107be36 | 4924 | |
1cde2930 | 4925 | static inline void fire_sched_in_preempt_notifiers(struct task_struct *curr) |
e107be36 AK |
4926 | { |
4927 | } | |
4928 | ||
1cde2930 | 4929 | static inline void |
e107be36 AK |
4930 | fire_sched_out_preempt_notifiers(struct task_struct *curr, |
4931 | struct task_struct *next) | |
4932 | { | |
4933 | } | |
4934 | ||
6d6bc0ad | 4935 | #endif /* CONFIG_PREEMPT_NOTIFIERS */ |
e107be36 | 4936 | |
31cb1bc0 | 4937 | static inline void prepare_task(struct task_struct *next) |
4938 | { | |
4939 | #ifdef CONFIG_SMP | |
4940 | /* | |
4941 | * Claim the task as running, we do this before switching to it | |
4942 | * such that any running task will have this set. | |
58877d34 | 4943 | * |
f3dd3f67 TD |
4944 | * See the smp_load_acquire(&p->on_cpu) case in ttwu() and |
4945 | * its ordering comment. | |
31cb1bc0 | 4946 | */ |
58877d34 | 4947 | WRITE_ONCE(next->on_cpu, 1); |
31cb1bc0 | 4948 | #endif |
4949 | } | |
4950 | ||
4951 | static inline void finish_task(struct task_struct *prev) | |
4952 | { | |
4953 | #ifdef CONFIG_SMP | |
4954 | /* | |
58877d34 PZ |
4955 | * This must be the very last reference to @prev from this CPU. After |
4956 | * p->on_cpu is cleared, the task can be moved to a different CPU. We | |
4957 | * must ensure this doesn't happen until the switch is completely | |
31cb1bc0 | 4958 | * finished. |
4959 | * | |
4960 | * In particular, the load of prev->state in finish_task_switch() must | |
4961 | * happen before this. | |
4962 | * | |
4963 | * Pairs with the smp_cond_load_acquire() in try_to_wake_up(). | |
4964 | */ | |
4965 | smp_store_release(&prev->on_cpu, 0); | |
4966 | #endif | |
4967 | } | |
4968 | ||
565790d2 PZ |
4969 | #ifdef CONFIG_SMP |
4970 | ||
8e5bad7d | 4971 | static void do_balance_callbacks(struct rq *rq, struct balance_callback *head) |
565790d2 PZ |
4972 | { |
4973 | void (*func)(struct rq *rq); | |
8e5bad7d | 4974 | struct balance_callback *next; |
565790d2 | 4975 | |
5cb9eaa3 | 4976 | lockdep_assert_rq_held(rq); |
565790d2 PZ |
4977 | |
4978 | while (head) { | |
4979 | func = (void (*)(struct rq *))head->func; | |
4980 | next = head->next; | |
4981 | head->next = NULL; | |
4982 | head = next; | |
4983 | ||
4984 | func(rq); | |
4985 | } | |
4986 | } | |
4987 | ||
ae792702 PZ |
4988 | static void balance_push(struct rq *rq); |
4989 | ||
04193d59 PZ |
4990 | /* |
4991 | * balance_push_callback is a right abuse of the callback interface and plays | |
4992 | * by significantly different rules. | |
4993 | * | |
4994 | * Where the normal balance_callback's purpose is to be ran in the same context | |
4995 | * that queued it (only later, when it's safe to drop rq->lock again), | |
4996 | * balance_push_callback is specifically targeted at __schedule(). | |
4997 | * | |
4998 | * This abuse is tolerated because it places all the unlikely/odd cases behind | |
4999 | * a single test, namely: rq->balance_callback == NULL. | |
5000 | */ | |
8e5bad7d | 5001 | struct balance_callback balance_push_callback = { |
ae792702 | 5002 | .next = NULL, |
8e5bad7d | 5003 | .func = balance_push, |
ae792702 PZ |
5004 | }; |
5005 | ||
8e5bad7d | 5006 | static inline struct balance_callback * |
04193d59 | 5007 | __splice_balance_callbacks(struct rq *rq, bool split) |
565790d2 | 5008 | { |
8e5bad7d | 5009 | struct balance_callback *head = rq->balance_callback; |
565790d2 | 5010 | |
04193d59 PZ |
5011 | if (likely(!head)) |
5012 | return NULL; | |
5013 | ||
5cb9eaa3 | 5014 | lockdep_assert_rq_held(rq); |
04193d59 PZ |
5015 | /* |
5016 | * Must not take balance_push_callback off the list when | |
5017 | * splice_balance_callbacks() and balance_callbacks() are not | |
5018 | * in the same rq->lock section. | |
5019 | * | |
5020 | * In that case it would be possible for __schedule() to interleave | |
5021 | * and observe the list empty. | |
5022 | */ | |
5023 | if (split && head == &balance_push_callback) | |
5024 | head = NULL; | |
5025 | else | |
565790d2 PZ |
5026 | rq->balance_callback = NULL; |
5027 | ||
5028 | return head; | |
5029 | } | |
5030 | ||
8e5bad7d | 5031 | static inline struct balance_callback *splice_balance_callbacks(struct rq *rq) |
04193d59 PZ |
5032 | { |
5033 | return __splice_balance_callbacks(rq, true); | |
5034 | } | |
5035 | ||
565790d2 PZ |
5036 | static void __balance_callbacks(struct rq *rq) |
5037 | { | |
04193d59 | 5038 | do_balance_callbacks(rq, __splice_balance_callbacks(rq, false)); |
565790d2 PZ |
5039 | } |
5040 | ||
8e5bad7d | 5041 | static inline void balance_callbacks(struct rq *rq, struct balance_callback *head) |
565790d2 PZ |
5042 | { |
5043 | unsigned long flags; | |
5044 | ||
5045 | if (unlikely(head)) { | |
5cb9eaa3 | 5046 | raw_spin_rq_lock_irqsave(rq, flags); |
565790d2 | 5047 | do_balance_callbacks(rq, head); |
5cb9eaa3 | 5048 | raw_spin_rq_unlock_irqrestore(rq, flags); |
565790d2 PZ |
5049 | } |
5050 | } | |
5051 | ||
5052 | #else | |
5053 | ||
5054 | static inline void __balance_callbacks(struct rq *rq) | |
5055 | { | |
5056 | } | |
5057 | ||
8e5bad7d | 5058 | static inline struct balance_callback *splice_balance_callbacks(struct rq *rq) |
565790d2 PZ |
5059 | { |
5060 | return NULL; | |
5061 | } | |
5062 | ||
8e5bad7d | 5063 | static inline void balance_callbacks(struct rq *rq, struct balance_callback *head) |
565790d2 PZ |
5064 | { |
5065 | } | |
5066 | ||
5067 | #endif | |
5068 | ||
269d5992 PZ |
5069 | static inline void |
5070 | prepare_lock_switch(struct rq *rq, struct task_struct *next, struct rq_flags *rf) | |
31cb1bc0 | 5071 | { |
269d5992 PZ |
5072 | /* |
5073 | * Since the runqueue lock will be released by the next | |
5074 | * task (which is an invalid locking op but in the case | |
5075 | * of the scheduler it's an obvious special-case), so we | |
5076 | * do an early lockdep release here: | |
5077 | */ | |
5078 | rq_unpin_lock(rq, rf); | |
9ef7e7e3 | 5079 | spin_release(&__rq_lockp(rq)->dep_map, _THIS_IP_); |
31cb1bc0 | 5080 | #ifdef CONFIG_DEBUG_SPINLOCK |
5081 | /* this is a valid case when another task releases the spinlock */ | |
5cb9eaa3 | 5082 | rq_lockp(rq)->owner = next; |
31cb1bc0 | 5083 | #endif |
269d5992 PZ |
5084 | } |
5085 | ||
5086 | static inline void finish_lock_switch(struct rq *rq) | |
5087 | { | |
31cb1bc0 | 5088 | /* |
5089 | * If we are tracking spinlock dependencies then we have to | |
5090 | * fix up the runqueue lock - which gets 'carried over' from | |
5091 | * prev into current: | |
5092 | */ | |
9ef7e7e3 | 5093 | spin_acquire(&__rq_lockp(rq)->dep_map, 0, 0, _THIS_IP_); |
ae792702 | 5094 | __balance_callbacks(rq); |
5cb9eaa3 | 5095 | raw_spin_rq_unlock_irq(rq); |
31cb1bc0 | 5096 | } |
5097 | ||
325ea10c IM |
5098 | /* |
5099 | * NOP if the arch has not defined these: | |
5100 | */ | |
5101 | ||
5102 | #ifndef prepare_arch_switch | |
5103 | # define prepare_arch_switch(next) do { } while (0) | |
5104 | #endif | |
5105 | ||
5106 | #ifndef finish_arch_post_lock_switch | |
5107 | # define finish_arch_post_lock_switch() do { } while (0) | |
5108 | #endif | |
5109 | ||
5fbda3ec TG |
5110 | static inline void kmap_local_sched_out(void) |
5111 | { | |
5112 | #ifdef CONFIG_KMAP_LOCAL | |
5113 | if (unlikely(current->kmap_ctrl.idx)) | |
5114 | __kmap_local_sched_out(); | |
5115 | #endif | |
5116 | } | |
5117 | ||
5118 | static inline void kmap_local_sched_in(void) | |
5119 | { | |
5120 | #ifdef CONFIG_KMAP_LOCAL | |
5121 | if (unlikely(current->kmap_ctrl.idx)) | |
5122 | __kmap_local_sched_in(); | |
5123 | #endif | |
5124 | } | |
5125 | ||
4866cde0 NP |
5126 | /** |
5127 | * prepare_task_switch - prepare to switch tasks | |
5128 | * @rq: the runqueue preparing to switch | |
421cee29 | 5129 | * @prev: the current task that is being switched out |
4866cde0 NP |
5130 | * @next: the task we are going to switch to. |
5131 | * | |
5132 | * This is called with the rq lock held and interrupts off. It must | |
5133 | * be paired with a subsequent finish_task_switch after the context | |
5134 | * switch. | |
5135 | * | |
5136 | * prepare_task_switch sets up locking and calls architecture specific | |
5137 | * hooks. | |
5138 | */ | |
e107be36 AK |
5139 | static inline void |
5140 | prepare_task_switch(struct rq *rq, struct task_struct *prev, | |
5141 | struct task_struct *next) | |
4866cde0 | 5142 | { |
0ed557aa | 5143 | kcov_prepare_switch(prev); |
43148951 | 5144 | sched_info_switch(rq, prev, next); |
fe4b04fa | 5145 | perf_event_task_sched_out(prev, next); |
d7822b1e | 5146 | rseq_preempt(prev); |
e107be36 | 5147 | fire_sched_out_preempt_notifiers(prev, next); |
5fbda3ec | 5148 | kmap_local_sched_out(); |
31cb1bc0 | 5149 | prepare_task(next); |
4866cde0 NP |
5150 | prepare_arch_switch(next); |
5151 | } | |
5152 | ||
1da177e4 LT |
5153 | /** |
5154 | * finish_task_switch - clean up after a task-switch | |
5155 | * @prev: the thread we just switched away from. | |
5156 | * | |
4866cde0 NP |
5157 | * finish_task_switch must be called after the context switch, paired |
5158 | * with a prepare_task_switch call before the context switch. | |
5159 | * finish_task_switch will reconcile locking set up by prepare_task_switch, | |
5160 | * and do any other architecture-specific cleanup actions. | |
1da177e4 LT |
5161 | * |
5162 | * Note that we may have delayed dropping an mm in context_switch(). If | |
41a2d6cf | 5163 | * so, we finish that here outside of the runqueue lock. (Doing it |
1da177e4 LT |
5164 | * with the lock held can cause deadlocks; see schedule() for |
5165 | * details.) | |
dfa50b60 ON |
5166 | * |
5167 | * The context switch have flipped the stack from under us and restored the | |
5168 | * local variables which were saved when this task called schedule() in the | |
5169 | * past. prev == current is still correct but we need to recalculate this_rq | |
5170 | * because prev may have moved to another CPU. | |
1da177e4 | 5171 | */ |
dfa50b60 | 5172 | static struct rq *finish_task_switch(struct task_struct *prev) |
1da177e4 LT |
5173 | __releases(rq->lock) |
5174 | { | |
dfa50b60 | 5175 | struct rq *rq = this_rq(); |
1da177e4 | 5176 | struct mm_struct *mm = rq->prev_mm; |
fa2c3254 | 5177 | unsigned int prev_state; |
1da177e4 | 5178 | |
609ca066 PZ |
5179 | /* |
5180 | * The previous task will have left us with a preempt_count of 2 | |
5181 | * because it left us after: | |
5182 | * | |
5183 | * schedule() | |
5184 | * preempt_disable(); // 1 | |
5185 | * __schedule() | |
5186 | * raw_spin_lock_irq(&rq->lock) // 2 | |
5187 | * | |
5188 | * Also, see FORK_PREEMPT_COUNT. | |
5189 | */ | |
e2bf1c4b PZ |
5190 | if (WARN_ONCE(preempt_count() != 2*PREEMPT_DISABLE_OFFSET, |
5191 | "corrupted preempt_count: %s/%d/0x%x\n", | |
5192 | current->comm, current->pid, preempt_count())) | |
5193 | preempt_count_set(FORK_PREEMPT_COUNT); | |
609ca066 | 5194 | |
1da177e4 LT |
5195 | rq->prev_mm = NULL; |
5196 | ||
5197 | /* | |
5198 | * A task struct has one reference for the use as "current". | |
c394cc9f | 5199 | * If a task dies, then it sets TASK_DEAD in tsk->state and calls |
55a101f8 ON |
5200 | * schedule one last time. The schedule call will never return, and |
5201 | * the scheduled task must drop that reference. | |
95913d97 PZ |
5202 | * |
5203 | * We must observe prev->state before clearing prev->on_cpu (in | |
31cb1bc0 | 5204 | * finish_task), otherwise a concurrent wakeup can get prev |
95913d97 PZ |
5205 | * running on another CPU and we could rave with its RUNNING -> DEAD |
5206 | * transition, resulting in a double drop. | |
1da177e4 | 5207 | */ |
2f064a59 | 5208 | prev_state = READ_ONCE(prev->__state); |
bf9fae9f | 5209 | vtime_task_switch(prev); |
a8d757ef | 5210 | perf_event_task_sched_in(prev, current); |
31cb1bc0 | 5211 | finish_task(prev); |
0fdcccfa | 5212 | tick_nohz_task_switch(); |
31cb1bc0 | 5213 | finish_lock_switch(rq); |
01f23e16 | 5214 | finish_arch_post_lock_switch(); |
0ed557aa | 5215 | kcov_finish_switch(current); |
5fbda3ec TG |
5216 | /* |
5217 | * kmap_local_sched_out() is invoked with rq::lock held and | |
5218 | * interrupts disabled. There is no requirement for that, but the | |
5219 | * sched out code does not have an interrupt enabled section. | |
5220 | * Restoring the maps on sched in does not require interrupts being | |
5221 | * disabled either. | |
5222 | */ | |
5223 | kmap_local_sched_in(); | |
e8fa1362 | 5224 | |
e107be36 | 5225 | fire_sched_in_preempt_notifiers(current); |
306e0604 | 5226 | /* |
70216e18 MD |
5227 | * When switching through a kernel thread, the loop in |
5228 | * membarrier_{private,global}_expedited() may have observed that | |
5229 | * kernel thread and not issued an IPI. It is therefore possible to | |
5230 | * schedule between user->kernel->user threads without passing though | |
5231 | * switch_mm(). Membarrier requires a barrier after storing to | |
5232 | * rq->curr, before returning to userspace, so provide them here: | |
5233 | * | |
5234 | * - a full memory barrier for {PRIVATE,GLOBAL}_EXPEDITED, implicitly | |
aa464ba9 | 5235 | * provided by mmdrop_lazy_tlb(), |
70216e18 | 5236 | * - a sync_core for SYNC_CORE. |
306e0604 | 5237 | */ |
70216e18 MD |
5238 | if (mm) { |
5239 | membarrier_mm_sync_core_before_usermode(mm); | |
aa464ba9 | 5240 | mmdrop_lazy_tlb_sched(mm); |
70216e18 | 5241 | } |
aa464ba9 | 5242 | |
1cef1150 PZ |
5243 | if (unlikely(prev_state == TASK_DEAD)) { |
5244 | if (prev->sched_class->task_dead) | |
5245 | prev->sched_class->task_dead(prev); | |
68f24b08 | 5246 | |
1cef1150 PZ |
5247 | /* Task is done with its stack. */ |
5248 | put_task_stack(prev); | |
5249 | ||
0ff7b2cf | 5250 | put_task_struct_rcu_user(prev); |
c6fd91f0 | 5251 | } |
99e5ada9 | 5252 | |
dfa50b60 | 5253 | return rq; |
1da177e4 LT |
5254 | } |
5255 | ||
5256 | /** | |
5257 | * schedule_tail - first thing a freshly forked thread must call. | |
5258 | * @prev: the thread we just switched away from. | |
5259 | */ | |
722a9f92 | 5260 | asmlinkage __visible void schedule_tail(struct task_struct *prev) |
1da177e4 LT |
5261 | __releases(rq->lock) |
5262 | { | |
609ca066 PZ |
5263 | /* |
5264 | * New tasks start with FORK_PREEMPT_COUNT, see there and | |
5265 | * finish_task_switch() for details. | |
5266 | * | |
5267 | * finish_task_switch() will drop rq->lock() and lower preempt_count | |
5268 | * and the preempt_enable() will end up enabling preemption (on | |
5269 | * PREEMPT_COUNT kernels). | |
5270 | */ | |
5271 | ||
13c2235b | 5272 | finish_task_switch(prev); |
1a43a14a | 5273 | preempt_enable(); |
70b97a7f | 5274 | |
1da177e4 | 5275 | if (current->set_child_tid) |
b488893a | 5276 | put_user(task_pid_vnr(current), current->set_child_tid); |
088fe47c EB |
5277 | |
5278 | calculate_sigpending(); | |
1da177e4 LT |
5279 | } |
5280 | ||
5281 | /* | |
dfa50b60 | 5282 | * context_switch - switch to the new MM and the new thread's register state. |
1da177e4 | 5283 | */ |
04936948 | 5284 | static __always_inline struct rq * |
70b97a7f | 5285 | context_switch(struct rq *rq, struct task_struct *prev, |
d8ac8971 | 5286 | struct task_struct *next, struct rq_flags *rf) |
1da177e4 | 5287 | { |
e107be36 | 5288 | prepare_task_switch(rq, prev, next); |
fe4b04fa | 5289 | |
9226d125 ZA |
5290 | /* |
5291 | * For paravirt, this is coupled with an exit in switch_to to | |
5292 | * combine the page table reload and the switch backend into | |
5293 | * one hypercall. | |
5294 | */ | |
224101ed | 5295 | arch_start_context_switch(prev); |
9226d125 | 5296 | |
306e0604 | 5297 | /* |
139d025c | 5298 | * kernel -> kernel lazy + transfer active |
aa464ba9 | 5299 | * user -> kernel lazy + mmgrab_lazy_tlb() active |
139d025c | 5300 | * |
aa464ba9 | 5301 | * kernel -> user switch + mmdrop_lazy_tlb() active |
139d025c | 5302 | * user -> user switch |
223baf9d MD |
5303 | * |
5304 | * switch_mm_cid() needs to be updated if the barriers provided | |
5305 | * by context_switch() are modified. | |
306e0604 | 5306 | */ |
139d025c PZ |
5307 | if (!next->mm) { // to kernel |
5308 | enter_lazy_tlb(prev->active_mm, next); | |
5309 | ||
5310 | next->active_mm = prev->active_mm; | |
5311 | if (prev->mm) // from user | |
aa464ba9 | 5312 | mmgrab_lazy_tlb(prev->active_mm); |
139d025c PZ |
5313 | else |
5314 | prev->active_mm = NULL; | |
5315 | } else { // to user | |
227a4aad | 5316 | membarrier_switch_mm(rq, prev->active_mm, next->mm); |
139d025c PZ |
5317 | /* |
5318 | * sys_membarrier() requires an smp_mb() between setting | |
227a4aad | 5319 | * rq->curr / membarrier_switch_mm() and returning to userspace. |
139d025c PZ |
5320 | * |
5321 | * The below provides this either through switch_mm(), or in | |
5322 | * case 'prev->active_mm == next->mm' through | |
5323 | * finish_task_switch()'s mmdrop(). | |
5324 | */ | |
139d025c | 5325 | switch_mm_irqs_off(prev->active_mm, next->mm, next); |
bd74fdae | 5326 | lru_gen_use_mm(next->mm); |
1da177e4 | 5327 | |
139d025c | 5328 | if (!prev->mm) { // from kernel |
aa464ba9 | 5329 | /* will mmdrop_lazy_tlb() in finish_task_switch(). */ |
139d025c PZ |
5330 | rq->prev_mm = prev->active_mm; |
5331 | prev->active_mm = NULL; | |
5332 | } | |
1da177e4 | 5333 | } |
92509b73 | 5334 | |
223baf9d MD |
5335 | /* switch_mm_cid() requires the memory barriers above. */ |
5336 | switch_mm_cid(rq, prev, next); | |
5337 | ||
cb42c9a3 | 5338 | rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP); |
92509b73 | 5339 | |
269d5992 | 5340 | prepare_lock_switch(rq, next, rf); |
1da177e4 LT |
5341 | |
5342 | /* Here we just switch the register state and the stack. */ | |
5343 | switch_to(prev, next, prev); | |
dd41f596 | 5344 | barrier(); |
dfa50b60 ON |
5345 | |
5346 | return finish_task_switch(prev); | |
1da177e4 LT |
5347 | } |
5348 | ||
5349 | /* | |
1c3e8264 | 5350 | * nr_running and nr_context_switches: |
1da177e4 LT |
5351 | * |
5352 | * externally visible scheduler statistics: current number of runnable | |
1c3e8264 | 5353 | * threads, total number of context switches performed since bootup. |
1da177e4 | 5354 | */ |
01aee8fd | 5355 | unsigned int nr_running(void) |
1da177e4 | 5356 | { |
01aee8fd | 5357 | unsigned int i, sum = 0; |
1da177e4 LT |
5358 | |
5359 | for_each_online_cpu(i) | |
5360 | sum += cpu_rq(i)->nr_running; | |
5361 | ||
5362 | return sum; | |
f711f609 | 5363 | } |
1da177e4 | 5364 | |
2ee507c4 | 5365 | /* |
d1ccc66d | 5366 | * Check if only the current task is running on the CPU. |
00cc1633 DD |
5367 | * |
5368 | * Caution: this function does not check that the caller has disabled | |
5369 | * preemption, thus the result might have a time-of-check-to-time-of-use | |
5370 | * race. The caller is responsible to use it correctly, for example: | |
5371 | * | |
dfcb245e | 5372 | * - from a non-preemptible section (of course) |
00cc1633 DD |
5373 | * |
5374 | * - from a thread that is bound to a single CPU | |
5375 | * | |
5376 | * - in a loop with very short iterations (e.g. a polling loop) | |
2ee507c4 TC |
5377 | */ |
5378 | bool single_task_running(void) | |
5379 | { | |
00cc1633 | 5380 | return raw_rq()->nr_running == 1; |
2ee507c4 TC |
5381 | } |
5382 | EXPORT_SYMBOL(single_task_running); | |
5383 | ||
7c182722 ZL |
5384 | unsigned long long nr_context_switches_cpu(int cpu) |
5385 | { | |
5386 | return cpu_rq(cpu)->nr_switches; | |
5387 | } | |
5388 | ||
1da177e4 | 5389 | unsigned long long nr_context_switches(void) |
46cb4b7c | 5390 | { |
cc94abfc SR |
5391 | int i; |
5392 | unsigned long long sum = 0; | |
46cb4b7c | 5393 | |
0a945022 | 5394 | for_each_possible_cpu(i) |
1da177e4 | 5395 | sum += cpu_rq(i)->nr_switches; |
46cb4b7c | 5396 | |
1da177e4 LT |
5397 | return sum; |
5398 | } | |
483b4ee6 | 5399 | |
145d952a DL |
5400 | /* |
5401 | * Consumers of these two interfaces, like for example the cpuidle menu | |
5402 | * governor, are using nonsensical data. Preferring shallow idle state selection | |
5403 | * for a CPU that has IO-wait which might not even end up running the task when | |
5404 | * it does become runnable. | |
5405 | */ | |
5406 | ||
8fc2858e | 5407 | unsigned int nr_iowait_cpu(int cpu) |
145d952a DL |
5408 | { |
5409 | return atomic_read(&cpu_rq(cpu)->nr_iowait); | |
5410 | } | |
5411 | ||
e33a9bba | 5412 | /* |
b19a888c | 5413 | * IO-wait accounting, and how it's mostly bollocks (on SMP). |
e33a9bba TH |
5414 | * |
5415 | * The idea behind IO-wait account is to account the idle time that we could | |
5416 | * have spend running if it were not for IO. That is, if we were to improve the | |
5417 | * storage performance, we'd have a proportional reduction in IO-wait time. | |
5418 | * | |
5419 | * This all works nicely on UP, where, when a task blocks on IO, we account | |
5420 | * idle time as IO-wait, because if the storage were faster, it could've been | |
5421 | * running and we'd not be idle. | |
5422 | * | |
5423 | * This has been extended to SMP, by doing the same for each CPU. This however | |
5424 | * is broken. | |
5425 | * | |
5426 | * Imagine for instance the case where two tasks block on one CPU, only the one | |
5427 | * CPU will have IO-wait accounted, while the other has regular idle. Even | |
5428 | * though, if the storage were faster, both could've ran at the same time, | |
5429 | * utilising both CPUs. | |
5430 | * | |
5431 | * This means, that when looking globally, the current IO-wait accounting on | |
5432 | * SMP is a lower bound, by reason of under accounting. | |
5433 | * | |
5434 | * Worse, since the numbers are provided per CPU, they are sometimes | |
5435 | * interpreted per CPU, and that is nonsensical. A blocked task isn't strictly | |
5436 | * associated with any one particular CPU, it can wake to another CPU than it | |
5437 | * blocked on. This means the per CPU IO-wait number is meaningless. | |
5438 | * | |
5439 | * Task CPU affinities can make all that even more 'interesting'. | |
5440 | */ | |
5441 | ||
97455168 | 5442 | unsigned int nr_iowait(void) |
1da177e4 | 5443 | { |
97455168 | 5444 | unsigned int i, sum = 0; |
483b4ee6 | 5445 | |
0a945022 | 5446 | for_each_possible_cpu(i) |
145d952a | 5447 | sum += nr_iowait_cpu(i); |
46cb4b7c | 5448 | |
1da177e4 LT |
5449 | return sum; |
5450 | } | |
483b4ee6 | 5451 | |
dd41f596 | 5452 | #ifdef CONFIG_SMP |
8a0be9ef | 5453 | |
46cb4b7c | 5454 | /* |
38022906 PZ |
5455 | * sched_exec - execve() is a valuable balancing opportunity, because at |
5456 | * this point the task has the smallest effective memory and cache footprint. | |
46cb4b7c | 5457 | */ |
38022906 | 5458 | void sched_exec(void) |
46cb4b7c | 5459 | { |
38022906 | 5460 | struct task_struct *p = current; |
1da177e4 | 5461 | unsigned long flags; |
0017d735 | 5462 | int dest_cpu; |
46cb4b7c | 5463 | |
8f42ced9 | 5464 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
3aef1551 | 5465 | dest_cpu = p->sched_class->select_task_rq(p, task_cpu(p), WF_EXEC); |
0017d735 PZ |
5466 | if (dest_cpu == smp_processor_id()) |
5467 | goto unlock; | |
38022906 | 5468 | |
8f42ced9 | 5469 | if (likely(cpu_active(dest_cpu))) { |
969c7921 | 5470 | struct migration_arg arg = { p, dest_cpu }; |
46cb4b7c | 5471 | |
8f42ced9 PZ |
5472 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
5473 | stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg); | |
1da177e4 LT |
5474 | return; |
5475 | } | |
0017d735 | 5476 | unlock: |
8f42ced9 | 5477 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
1da177e4 | 5478 | } |
dd41f596 | 5479 | |
1da177e4 LT |
5480 | #endif |
5481 | ||
1da177e4 | 5482 | DEFINE_PER_CPU(struct kernel_stat, kstat); |
3292beb3 | 5483 | DEFINE_PER_CPU(struct kernel_cpustat, kernel_cpustat); |
1da177e4 LT |
5484 | |
5485 | EXPORT_PER_CPU_SYMBOL(kstat); | |
3292beb3 | 5486 | EXPORT_PER_CPU_SYMBOL(kernel_cpustat); |
1da177e4 | 5487 | |
6075620b GG |
5488 | /* |
5489 | * The function fair_sched_class.update_curr accesses the struct curr | |
5490 | * and its field curr->exec_start; when called from task_sched_runtime(), | |
5491 | * we observe a high rate of cache misses in practice. | |
5492 | * Prefetching this data results in improved performance. | |
5493 | */ | |
5494 | static inline void prefetch_curr_exec_start(struct task_struct *p) | |
5495 | { | |
5496 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
5497 | struct sched_entity *curr = (&p->se)->cfs_rq->curr; | |
5498 | #else | |
5499 | struct sched_entity *curr = (&task_rq(p)->cfs)->curr; | |
5500 | #endif | |
5501 | prefetch(curr); | |
5502 | prefetch(&curr->exec_start); | |
5503 | } | |
5504 | ||
c5f8d995 HS |
5505 | /* |
5506 | * Return accounted runtime for the task. | |
5507 | * In case the task is currently running, return the runtime plus current's | |
5508 | * pending runtime that have not been accounted yet. | |
5509 | */ | |
5510 | unsigned long long task_sched_runtime(struct task_struct *p) | |
5511 | { | |
eb580751 | 5512 | struct rq_flags rf; |
c5f8d995 | 5513 | struct rq *rq; |
6e998916 | 5514 | u64 ns; |
c5f8d995 | 5515 | |
911b2898 PZ |
5516 | #if defined(CONFIG_64BIT) && defined(CONFIG_SMP) |
5517 | /* | |
97fb7a0a | 5518 | * 64-bit doesn't need locks to atomically read a 64-bit value. |
911b2898 PZ |
5519 | * So we have a optimization chance when the task's delta_exec is 0. |
5520 | * Reading ->on_cpu is racy, but this is ok. | |
5521 | * | |
d1ccc66d IM |
5522 | * If we race with it leaving CPU, we'll take a lock. So we're correct. |
5523 | * If we race with it entering CPU, unaccounted time is 0. This is | |
911b2898 | 5524 | * indistinguishable from the read occurring a few cycles earlier. |
4036ac15 MG |
5525 | * If we see ->on_cpu without ->on_rq, the task is leaving, and has |
5526 | * been accounted, so we're correct here as well. | |
911b2898 | 5527 | */ |
da0c1e65 | 5528 | if (!p->on_cpu || !task_on_rq_queued(p)) |
911b2898 PZ |
5529 | return p->se.sum_exec_runtime; |
5530 | #endif | |
5531 | ||
eb580751 | 5532 | rq = task_rq_lock(p, &rf); |
6e998916 SG |
5533 | /* |
5534 | * Must be ->curr _and_ ->on_rq. If dequeued, we would | |
5535 | * project cycles that may never be accounted to this | |
5536 | * thread, breaking clock_gettime(). | |
5537 | */ | |
5538 | if (task_current(rq, p) && task_on_rq_queued(p)) { | |
6075620b | 5539 | prefetch_curr_exec_start(p); |
6e998916 SG |
5540 | update_rq_clock(rq); |
5541 | p->sched_class->update_curr(rq); | |
5542 | } | |
5543 | ns = p->se.sum_exec_runtime; | |
eb580751 | 5544 | task_rq_unlock(rq, p, &rf); |
c5f8d995 HS |
5545 | |
5546 | return ns; | |
5547 | } | |
48f24c4d | 5548 | |
c006fac5 PT |
5549 | #ifdef CONFIG_SCHED_DEBUG |
5550 | static u64 cpu_resched_latency(struct rq *rq) | |
5551 | { | |
5552 | int latency_warn_ms = READ_ONCE(sysctl_resched_latency_warn_ms); | |
5553 | u64 resched_latency, now = rq_clock(rq); | |
5554 | static bool warned_once; | |
5555 | ||
5556 | if (sysctl_resched_latency_warn_once && warned_once) | |
5557 | return 0; | |
5558 | ||
5559 | if (!need_resched() || !latency_warn_ms) | |
5560 | return 0; | |
5561 | ||
5562 | if (system_state == SYSTEM_BOOTING) | |
5563 | return 0; | |
5564 | ||
5565 | if (!rq->last_seen_need_resched_ns) { | |
5566 | rq->last_seen_need_resched_ns = now; | |
5567 | rq->ticks_without_resched = 0; | |
5568 | return 0; | |
5569 | } | |
5570 | ||
5571 | rq->ticks_without_resched++; | |
5572 | resched_latency = now - rq->last_seen_need_resched_ns; | |
5573 | if (resched_latency <= latency_warn_ms * NSEC_PER_MSEC) | |
5574 | return 0; | |
5575 | ||
5576 | warned_once = true; | |
5577 | ||
5578 | return resched_latency; | |
5579 | } | |
5580 | ||
5581 | static int __init setup_resched_latency_warn_ms(char *str) | |
5582 | { | |
5583 | long val; | |
5584 | ||
5585 | if ((kstrtol(str, 0, &val))) { | |
5586 | pr_warn("Unable to set resched_latency_warn_ms\n"); | |
5587 | return 1; | |
5588 | } | |
5589 | ||
5590 | sysctl_resched_latency_warn_ms = val; | |
5591 | return 1; | |
5592 | } | |
5593 | __setup("resched_latency_warn_ms=", setup_resched_latency_warn_ms); | |
5594 | #else | |
5595 | static inline u64 cpu_resched_latency(struct rq *rq) { return 0; } | |
5596 | #endif /* CONFIG_SCHED_DEBUG */ | |
5597 | ||
7835b98b CL |
5598 | /* |
5599 | * This function gets called by the timer code, with HZ frequency. | |
5600 | * We call it with interrupts disabled. | |
7835b98b CL |
5601 | */ |
5602 | void scheduler_tick(void) | |
5603 | { | |
7835b98b CL |
5604 | int cpu = smp_processor_id(); |
5605 | struct rq *rq = cpu_rq(cpu); | |
dd41f596 | 5606 | struct task_struct *curr = rq->curr; |
8a8c69c3 | 5607 | struct rq_flags rf; |
b4eccf5f | 5608 | unsigned long thermal_pressure; |
c006fac5 | 5609 | u64 resched_latency; |
3e51f33f | 5610 | |
7fb3ff22 YP |
5611 | if (housekeeping_cpu(cpu, HK_TYPE_TICK)) |
5612 | arch_scale_freq_tick(); | |
5613 | ||
3e51f33f | 5614 | sched_clock_tick(); |
dd41f596 | 5615 | |
8a8c69c3 PZ |
5616 | rq_lock(rq, &rf); |
5617 | ||
3e51f33f | 5618 | update_rq_clock(rq); |
b4eccf5f | 5619 | thermal_pressure = arch_scale_thermal_pressure(cpu_of(rq)); |
05289b90 | 5620 | update_thermal_load_avg(rq_clock_thermal(rq), rq, thermal_pressure); |
fa85ae24 | 5621 | curr->sched_class->task_tick(rq, curr, 0); |
c006fac5 PT |
5622 | if (sched_feat(LATENCY_WARN)) |
5623 | resched_latency = cpu_resched_latency(rq); | |
3289bdb4 | 5624 | calc_global_load_tick(rq); |
4feee7d1 | 5625 | sched_core_tick(rq); |
223baf9d | 5626 | task_tick_mm_cid(rq, curr); |
8a8c69c3 PZ |
5627 | |
5628 | rq_unlock(rq, &rf); | |
7835b98b | 5629 | |
c006fac5 PT |
5630 | if (sched_feat(LATENCY_WARN) && resched_latency) |
5631 | resched_latency_warn(cpu, resched_latency); | |
5632 | ||
e9d2b064 | 5633 | perf_event_task_tick(); |
e220d2dc | 5634 | |
e418e1c2 | 5635 | #ifdef CONFIG_SMP |
6eb57e0d | 5636 | rq->idle_balance = idle_cpu(cpu); |
7caff66f | 5637 | trigger_load_balance(rq); |
e418e1c2 | 5638 | #endif |
1da177e4 LT |
5639 | } |
5640 | ||
265f22a9 | 5641 | #ifdef CONFIG_NO_HZ_FULL |
d84b3131 FW |
5642 | |
5643 | struct tick_work { | |
5644 | int cpu; | |
b55bd585 | 5645 | atomic_t state; |
d84b3131 FW |
5646 | struct delayed_work work; |
5647 | }; | |
b55bd585 PM |
5648 | /* Values for ->state, see diagram below. */ |
5649 | #define TICK_SCHED_REMOTE_OFFLINE 0 | |
5650 | #define TICK_SCHED_REMOTE_OFFLINING 1 | |
5651 | #define TICK_SCHED_REMOTE_RUNNING 2 | |
5652 | ||
5653 | /* | |
5654 | * State diagram for ->state: | |
5655 | * | |
5656 | * | |
5657 | * TICK_SCHED_REMOTE_OFFLINE | |
5658 | * | ^ | |
5659 | * | | | |
5660 | * | | sched_tick_remote() | |
5661 | * | | | |
5662 | * | | | |
5663 | * +--TICK_SCHED_REMOTE_OFFLINING | |
5664 | * | ^ | |
5665 | * | | | |
5666 | * sched_tick_start() | | sched_tick_stop() | |
5667 | * | | | |
5668 | * V | | |
5669 | * TICK_SCHED_REMOTE_RUNNING | |
5670 | * | |
5671 | * | |
5672 | * Other transitions get WARN_ON_ONCE(), except that sched_tick_remote() | |
5673 | * and sched_tick_start() are happy to leave the state in RUNNING. | |
5674 | */ | |
d84b3131 FW |
5675 | |
5676 | static struct tick_work __percpu *tick_work_cpu; | |
5677 | ||
5678 | static void sched_tick_remote(struct work_struct *work) | |
5679 | { | |
5680 | struct delayed_work *dwork = to_delayed_work(work); | |
5681 | struct tick_work *twork = container_of(dwork, struct tick_work, work); | |
5682 | int cpu = twork->cpu; | |
5683 | struct rq *rq = cpu_rq(cpu); | |
d9c0ffca | 5684 | struct task_struct *curr; |
d84b3131 | 5685 | struct rq_flags rf; |
d9c0ffca | 5686 | u64 delta; |
b55bd585 | 5687 | int os; |
d84b3131 FW |
5688 | |
5689 | /* | |
5690 | * Handle the tick only if it appears the remote CPU is running in full | |
5691 | * dynticks mode. The check is racy by nature, but missing a tick or | |
5692 | * having one too much is no big deal because the scheduler tick updates | |
5693 | * statistics and checks timeslices in a time-independent way, regardless | |
5694 | * of when exactly it is running. | |
5695 | */ | |
488603b8 | 5696 | if (!tick_nohz_tick_stopped_cpu(cpu)) |
d9c0ffca | 5697 | goto out_requeue; |
d84b3131 | 5698 | |
d9c0ffca FW |
5699 | rq_lock_irq(rq, &rf); |
5700 | curr = rq->curr; | |
488603b8 | 5701 | if (cpu_is_offline(cpu)) |
d9c0ffca | 5702 | goto out_unlock; |
d84b3131 | 5703 | |
d9c0ffca | 5704 | update_rq_clock(rq); |
d9c0ffca | 5705 | |
488603b8 SW |
5706 | if (!is_idle_task(curr)) { |
5707 | /* | |
5708 | * Make sure the next tick runs within a reasonable | |
5709 | * amount of time. | |
5710 | */ | |
5711 | delta = rq_clock_task(rq) - curr->se.exec_start; | |
5712 | WARN_ON_ONCE(delta > (u64)NSEC_PER_SEC * 3); | |
5713 | } | |
d9c0ffca FW |
5714 | curr->sched_class->task_tick(rq, curr, 0); |
5715 | ||
ebc0f83c | 5716 | calc_load_nohz_remote(rq); |
d9c0ffca FW |
5717 | out_unlock: |
5718 | rq_unlock_irq(rq, &rf); | |
d9c0ffca | 5719 | out_requeue: |
ebc0f83c | 5720 | |
d84b3131 FW |
5721 | /* |
5722 | * Run the remote tick once per second (1Hz). This arbitrary | |
5723 | * frequency is large enough to avoid overload but short enough | |
b55bd585 PM |
5724 | * to keep scheduler internal stats reasonably up to date. But |
5725 | * first update state to reflect hotplug activity if required. | |
d84b3131 | 5726 | */ |
b55bd585 PM |
5727 | os = atomic_fetch_add_unless(&twork->state, -1, TICK_SCHED_REMOTE_RUNNING); |
5728 | WARN_ON_ONCE(os == TICK_SCHED_REMOTE_OFFLINE); | |
5729 | if (os == TICK_SCHED_REMOTE_RUNNING) | |
5730 | queue_delayed_work(system_unbound_wq, dwork, HZ); | |
d84b3131 FW |
5731 | } |
5732 | ||
5733 | static void sched_tick_start(int cpu) | |
5734 | { | |
b55bd585 | 5735 | int os; |
d84b3131 FW |
5736 | struct tick_work *twork; |
5737 | ||
04d4e665 | 5738 | if (housekeeping_cpu(cpu, HK_TYPE_TICK)) |
d84b3131 FW |
5739 | return; |
5740 | ||
5741 | WARN_ON_ONCE(!tick_work_cpu); | |
5742 | ||
5743 | twork = per_cpu_ptr(tick_work_cpu, cpu); | |
b55bd585 PM |
5744 | os = atomic_xchg(&twork->state, TICK_SCHED_REMOTE_RUNNING); |
5745 | WARN_ON_ONCE(os == TICK_SCHED_REMOTE_RUNNING); | |
5746 | if (os == TICK_SCHED_REMOTE_OFFLINE) { | |
5747 | twork->cpu = cpu; | |
5748 | INIT_DELAYED_WORK(&twork->work, sched_tick_remote); | |
5749 | queue_delayed_work(system_unbound_wq, &twork->work, HZ); | |
5750 | } | |
d84b3131 FW |
5751 | } |
5752 | ||
5753 | #ifdef CONFIG_HOTPLUG_CPU | |
5754 | static void sched_tick_stop(int cpu) | |
5755 | { | |
5756 | struct tick_work *twork; | |
b55bd585 | 5757 | int os; |
d84b3131 | 5758 | |
04d4e665 | 5759 | if (housekeeping_cpu(cpu, HK_TYPE_TICK)) |
d84b3131 FW |
5760 | return; |
5761 | ||
5762 | WARN_ON_ONCE(!tick_work_cpu); | |
5763 | ||
5764 | twork = per_cpu_ptr(tick_work_cpu, cpu); | |
b55bd585 PM |
5765 | /* There cannot be competing actions, but don't rely on stop-machine. */ |
5766 | os = atomic_xchg(&twork->state, TICK_SCHED_REMOTE_OFFLINING); | |
5767 | WARN_ON_ONCE(os != TICK_SCHED_REMOTE_RUNNING); | |
5768 | /* Don't cancel, as this would mess up the state machine. */ | |
d84b3131 FW |
5769 | } |
5770 | #endif /* CONFIG_HOTPLUG_CPU */ | |
5771 | ||
5772 | int __init sched_tick_offload_init(void) | |
5773 | { | |
5774 | tick_work_cpu = alloc_percpu(struct tick_work); | |
5775 | BUG_ON(!tick_work_cpu); | |
d84b3131 FW |
5776 | return 0; |
5777 | } | |
5778 | ||
5779 | #else /* !CONFIG_NO_HZ_FULL */ | |
5780 | static inline void sched_tick_start(int cpu) { } | |
5781 | static inline void sched_tick_stop(int cpu) { } | |
265f22a9 | 5782 | #endif |
1da177e4 | 5783 | |
c1a280b6 | 5784 | #if defined(CONFIG_PREEMPTION) && (defined(CONFIG_DEBUG_PREEMPT) || \ |
c3bc8fd6 | 5785 | defined(CONFIG_TRACE_PREEMPT_TOGGLE)) |
47252cfb SR |
5786 | /* |
5787 | * If the value passed in is equal to the current preempt count | |
5788 | * then we just disabled preemption. Start timing the latency. | |
5789 | */ | |
5790 | static inline void preempt_latency_start(int val) | |
5791 | { | |
5792 | if (preempt_count() == val) { | |
5793 | unsigned long ip = get_lock_parent_ip(); | |
5794 | #ifdef CONFIG_DEBUG_PREEMPT | |
5795 | current->preempt_disable_ip = ip; | |
5796 | #endif | |
5797 | trace_preempt_off(CALLER_ADDR0, ip); | |
5798 | } | |
5799 | } | |
7e49fcce | 5800 | |
edafe3a5 | 5801 | void preempt_count_add(int val) |
1da177e4 | 5802 | { |
6cd8a4bb | 5803 | #ifdef CONFIG_DEBUG_PREEMPT |
1da177e4 LT |
5804 | /* |
5805 | * Underflow? | |
5806 | */ | |
9a11b49a IM |
5807 | if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0))) |
5808 | return; | |
6cd8a4bb | 5809 | #endif |
bdb43806 | 5810 | __preempt_count_add(val); |
6cd8a4bb | 5811 | #ifdef CONFIG_DEBUG_PREEMPT |
1da177e4 LT |
5812 | /* |
5813 | * Spinlock count overflowing soon? | |
5814 | */ | |
33859f7f MOS |
5815 | DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >= |
5816 | PREEMPT_MASK - 10); | |
6cd8a4bb | 5817 | #endif |
47252cfb | 5818 | preempt_latency_start(val); |
1da177e4 | 5819 | } |
bdb43806 | 5820 | EXPORT_SYMBOL(preempt_count_add); |
edafe3a5 | 5821 | NOKPROBE_SYMBOL(preempt_count_add); |
1da177e4 | 5822 | |
47252cfb SR |
5823 | /* |
5824 | * If the value passed in equals to the current preempt count | |
5825 | * then we just enabled preemption. Stop timing the latency. | |
5826 | */ | |
5827 | static inline void preempt_latency_stop(int val) | |
5828 | { | |
5829 | if (preempt_count() == val) | |
5830 | trace_preempt_on(CALLER_ADDR0, get_lock_parent_ip()); | |
5831 | } | |
5832 | ||
edafe3a5 | 5833 | void preempt_count_sub(int val) |
1da177e4 | 5834 | { |
6cd8a4bb | 5835 | #ifdef CONFIG_DEBUG_PREEMPT |
1da177e4 LT |
5836 | /* |
5837 | * Underflow? | |
5838 | */ | |
01e3eb82 | 5839 | if (DEBUG_LOCKS_WARN_ON(val > preempt_count())) |
9a11b49a | 5840 | return; |
1da177e4 LT |
5841 | /* |
5842 | * Is the spinlock portion underflowing? | |
5843 | */ | |
9a11b49a IM |
5844 | if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) && |
5845 | !(preempt_count() & PREEMPT_MASK))) | |
5846 | return; | |
6cd8a4bb | 5847 | #endif |
9a11b49a | 5848 | |
47252cfb | 5849 | preempt_latency_stop(val); |
bdb43806 | 5850 | __preempt_count_sub(val); |
1da177e4 | 5851 | } |
bdb43806 | 5852 | EXPORT_SYMBOL(preempt_count_sub); |
edafe3a5 | 5853 | NOKPROBE_SYMBOL(preempt_count_sub); |
1da177e4 | 5854 | |
47252cfb SR |
5855 | #else |
5856 | static inline void preempt_latency_start(int val) { } | |
5857 | static inline void preempt_latency_stop(int val) { } | |
1da177e4 LT |
5858 | #endif |
5859 | ||
59ddbcb2 IM |
5860 | static inline unsigned long get_preempt_disable_ip(struct task_struct *p) |
5861 | { | |
5862 | #ifdef CONFIG_DEBUG_PREEMPT | |
5863 | return p->preempt_disable_ip; | |
5864 | #else | |
5865 | return 0; | |
5866 | #endif | |
5867 | } | |
5868 | ||
1da177e4 | 5869 | /* |
dd41f596 | 5870 | * Print scheduling while atomic bug: |
1da177e4 | 5871 | */ |
dd41f596 | 5872 | static noinline void __schedule_bug(struct task_struct *prev) |
1da177e4 | 5873 | { |
d1c6d149 VN |
5874 | /* Save this before calling printk(), since that will clobber it */ |
5875 | unsigned long preempt_disable_ip = get_preempt_disable_ip(current); | |
5876 | ||
664dfa65 DJ |
5877 | if (oops_in_progress) |
5878 | return; | |
5879 | ||
3df0fc5b PZ |
5880 | printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n", |
5881 | prev->comm, prev->pid, preempt_count()); | |
838225b4 | 5882 | |
dd41f596 | 5883 | debug_show_held_locks(prev); |
e21f5b15 | 5884 | print_modules(); |
dd41f596 IM |
5885 | if (irqs_disabled()) |
5886 | print_irqtrace_events(prev); | |
d1c6d149 VN |
5887 | if (IS_ENABLED(CONFIG_DEBUG_PREEMPT) |
5888 | && in_atomic_preempt_off()) { | |
8f47b187 | 5889 | pr_err("Preemption disabled at:"); |
2062a4e8 | 5890 | print_ip_sym(KERN_ERR, preempt_disable_ip); |
8f47b187 | 5891 | } |
79cc1ba7 | 5892 | check_panic_on_warn("scheduling while atomic"); |
748c7201 | 5893 | |
6135fc1e | 5894 | dump_stack(); |
373d4d09 | 5895 | add_taint(TAINT_WARN, LOCKDEP_STILL_OK); |
dd41f596 | 5896 | } |
1da177e4 | 5897 | |
dd41f596 IM |
5898 | /* |
5899 | * Various schedule()-time debugging checks and statistics: | |
5900 | */ | |
312364f3 | 5901 | static inline void schedule_debug(struct task_struct *prev, bool preempt) |
dd41f596 | 5902 | { |
0d9e2632 | 5903 | #ifdef CONFIG_SCHED_STACK_END_CHECK |
29d64551 JH |
5904 | if (task_stack_end_corrupted(prev)) |
5905 | panic("corrupted stack end detected inside scheduler\n"); | |
88485be5 WD |
5906 | |
5907 | if (task_scs_end_corrupted(prev)) | |
5908 | panic("corrupted shadow stack detected inside scheduler\n"); | |
0d9e2632 | 5909 | #endif |
b99def8b | 5910 | |
312364f3 | 5911 | #ifdef CONFIG_DEBUG_ATOMIC_SLEEP |
2f064a59 | 5912 | if (!preempt && READ_ONCE(prev->__state) && prev->non_block_count) { |
312364f3 DV |
5913 | printk(KERN_ERR "BUG: scheduling in a non-blocking section: %s/%d/%i\n", |
5914 | prev->comm, prev->pid, prev->non_block_count); | |
5915 | dump_stack(); | |
5916 | add_taint(TAINT_WARN, LOCKDEP_STILL_OK); | |
5917 | } | |
5918 | #endif | |
5919 | ||
1dc0fffc | 5920 | if (unlikely(in_atomic_preempt_off())) { |
dd41f596 | 5921 | __schedule_bug(prev); |
1dc0fffc PZ |
5922 | preempt_count_set(PREEMPT_DISABLED); |
5923 | } | |
b3fbab05 | 5924 | rcu_sleep_check(); |
9f68b5b7 | 5925 | SCHED_WARN_ON(ct_state() == CONTEXT_USER); |
dd41f596 | 5926 | |
1da177e4 LT |
5927 | profile_hit(SCHED_PROFILING, __builtin_return_address(0)); |
5928 | ||
ae92882e | 5929 | schedstat_inc(this_rq()->sched_count); |
dd41f596 IM |
5930 | } |
5931 | ||
457d1f46 CY |
5932 | static void put_prev_task_balance(struct rq *rq, struct task_struct *prev, |
5933 | struct rq_flags *rf) | |
5934 | { | |
5935 | #ifdef CONFIG_SMP | |
5936 | const struct sched_class *class; | |
5937 | /* | |
5938 | * We must do the balancing pass before put_prev_task(), such | |
5939 | * that when we release the rq->lock the task is in the same | |
5940 | * state as before we took rq->lock. | |
5941 | * | |
5942 | * We can terminate the balance pass as soon as we know there is | |
5943 | * a runnable task of @class priority or higher. | |
5944 | */ | |
5945 | for_class_range(class, prev->sched_class, &idle_sched_class) { | |
5946 | if (class->balance(rq, prev, rf)) | |
5947 | break; | |
5948 | } | |
5949 | #endif | |
5950 | ||
5951 | put_prev_task(rq, prev); | |
5952 | } | |
5953 | ||
dd41f596 IM |
5954 | /* |
5955 | * Pick up the highest-prio task: | |
5956 | */ | |
5957 | static inline struct task_struct * | |
539f6512 | 5958 | __pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) |
dd41f596 | 5959 | { |
49ee5768 | 5960 | const struct sched_class *class; |
dd41f596 | 5961 | struct task_struct *p; |
1da177e4 LT |
5962 | |
5963 | /* | |
0ba87bb2 PZ |
5964 | * Optimization: we know that if all tasks are in the fair class we can |
5965 | * call that function directly, but only if the @prev task wasn't of a | |
b19a888c | 5966 | * higher scheduling class, because otherwise those lose the |
0ba87bb2 | 5967 | * opportunity to pull in more work from other CPUs. |
1da177e4 | 5968 | */ |
546a3fee | 5969 | if (likely(!sched_class_above(prev->sched_class, &fair_sched_class) && |
0ba87bb2 PZ |
5970 | rq->nr_running == rq->cfs.h_nr_running)) { |
5971 | ||
5d7d6056 | 5972 | p = pick_next_task_fair(rq, prev, rf); |
6ccdc84b | 5973 | if (unlikely(p == RETRY_TASK)) |
67692435 | 5974 | goto restart; |
6ccdc84b | 5975 | |
1699949d | 5976 | /* Assume the next prioritized class is idle_sched_class */ |
5d7d6056 | 5977 | if (!p) { |
f488e105 | 5978 | put_prev_task(rq, prev); |
98c2f700 | 5979 | p = pick_next_task_idle(rq); |
f488e105 | 5980 | } |
6ccdc84b PZ |
5981 | |
5982 | return p; | |
1da177e4 LT |
5983 | } |
5984 | ||
67692435 | 5985 | restart: |
457d1f46 | 5986 | put_prev_task_balance(rq, prev, rf); |
67692435 | 5987 | |
34f971f6 | 5988 | for_each_class(class) { |
98c2f700 | 5989 | p = class->pick_next_task(rq); |
67692435 | 5990 | if (p) |
dd41f596 | 5991 | return p; |
dd41f596 | 5992 | } |
34f971f6 | 5993 | |
bc9ffef3 | 5994 | BUG(); /* The idle class should always have a runnable task. */ |
dd41f596 | 5995 | } |
1da177e4 | 5996 | |
9edeaea1 | 5997 | #ifdef CONFIG_SCHED_CORE |
539f6512 PZ |
5998 | static inline bool is_task_rq_idle(struct task_struct *t) |
5999 | { | |
6000 | return (task_rq(t)->idle == t); | |
6001 | } | |
6002 | ||
6003 | static inline bool cookie_equals(struct task_struct *a, unsigned long cookie) | |
6004 | { | |
6005 | return is_task_rq_idle(a) || (a->core_cookie == cookie); | |
6006 | } | |
6007 | ||
6008 | static inline bool cookie_match(struct task_struct *a, struct task_struct *b) | |
6009 | { | |
6010 | if (is_task_rq_idle(a) || is_task_rq_idle(b)) | |
6011 | return true; | |
6012 | ||
6013 | return a->core_cookie == b->core_cookie; | |
6014 | } | |
6015 | ||
bc9ffef3 | 6016 | static inline struct task_struct *pick_task(struct rq *rq) |
539f6512 | 6017 | { |
bc9ffef3 PZ |
6018 | const struct sched_class *class; |
6019 | struct task_struct *p; | |
539f6512 | 6020 | |
bc9ffef3 PZ |
6021 | for_each_class(class) { |
6022 | p = class->pick_task(rq); | |
6023 | if (p) | |
6024 | return p; | |
539f6512 PZ |
6025 | } |
6026 | ||
bc9ffef3 | 6027 | BUG(); /* The idle class should always have a runnable task. */ |
539f6512 PZ |
6028 | } |
6029 | ||
c6047c2e JFG |
6030 | extern void task_vruntime_update(struct rq *rq, struct task_struct *p, bool in_fi); |
6031 | ||
5b6547ed PZ |
6032 | static void queue_core_balance(struct rq *rq); |
6033 | ||
539f6512 PZ |
6034 | static struct task_struct * |
6035 | pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) | |
6036 | { | |
bc9ffef3 | 6037 | struct task_struct *next, *p, *max = NULL; |
539f6512 | 6038 | const struct cpumask *smt_mask; |
c6047c2e | 6039 | bool fi_before = false; |
4feee7d1 | 6040 | bool core_clock_updated = (rq == rq->core); |
bc9ffef3 PZ |
6041 | unsigned long cookie; |
6042 | int i, cpu, occ = 0; | |
6043 | struct rq *rq_i; | |
539f6512 | 6044 | bool need_sync; |
539f6512 PZ |
6045 | |
6046 | if (!sched_core_enabled(rq)) | |
6047 | return __pick_next_task(rq, prev, rf); | |
6048 | ||
6049 | cpu = cpu_of(rq); | |
6050 | ||
6051 | /* Stopper task is switching into idle, no need core-wide selection. */ | |
6052 | if (cpu_is_offline(cpu)) { | |
6053 | /* | |
6054 | * Reset core_pick so that we don't enter the fastpath when | |
6055 | * coming online. core_pick would already be migrated to | |
6056 | * another cpu during offline. | |
6057 | */ | |
6058 | rq->core_pick = NULL; | |
6059 | return __pick_next_task(rq, prev, rf); | |
6060 | } | |
6061 | ||
6062 | /* | |
6063 | * If there were no {en,de}queues since we picked (IOW, the task | |
6064 | * pointers are all still valid), and we haven't scheduled the last | |
6065 | * pick yet, do so now. | |
6066 | * | |
6067 | * rq->core_pick can be NULL if no selection was made for a CPU because | |
6068 | * it was either offline or went offline during a sibling's core-wide | |
6069 | * selection. In this case, do a core-wide selection. | |
6070 | */ | |
6071 | if (rq->core->core_pick_seq == rq->core->core_task_seq && | |
6072 | rq->core->core_pick_seq != rq->core_sched_seq && | |
6073 | rq->core_pick) { | |
6074 | WRITE_ONCE(rq->core_sched_seq, rq->core->core_pick_seq); | |
6075 | ||
6076 | next = rq->core_pick; | |
6077 | if (next != prev) { | |
6078 | put_prev_task(rq, prev); | |
6079 | set_next_task(rq, next); | |
6080 | } | |
6081 | ||
6082 | rq->core_pick = NULL; | |
5b6547ed | 6083 | goto out; |
539f6512 PZ |
6084 | } |
6085 | ||
6086 | put_prev_task_balance(rq, prev, rf); | |
6087 | ||
6088 | smt_mask = cpu_smt_mask(cpu); | |
7afbba11 JFG |
6089 | need_sync = !!rq->core->core_cookie; |
6090 | ||
6091 | /* reset state */ | |
6092 | rq->core->core_cookie = 0UL; | |
4feee7d1 JD |
6093 | if (rq->core->core_forceidle_count) { |
6094 | if (!core_clock_updated) { | |
6095 | update_rq_clock(rq->core); | |
6096 | core_clock_updated = true; | |
6097 | } | |
6098 | sched_core_account_forceidle(rq); | |
6099 | /* reset after accounting force idle */ | |
6100 | rq->core->core_forceidle_start = 0; | |
6101 | rq->core->core_forceidle_count = 0; | |
6102 | rq->core->core_forceidle_occupation = 0; | |
7afbba11 JFG |
6103 | need_sync = true; |
6104 | fi_before = true; | |
7afbba11 | 6105 | } |
539f6512 PZ |
6106 | |
6107 | /* | |
6108 | * core->core_task_seq, core->core_pick_seq, rq->core_sched_seq | |
6109 | * | |
6110 | * @task_seq guards the task state ({en,de}queues) | |
6111 | * @pick_seq is the @task_seq we did a selection on | |
6112 | * @sched_seq is the @pick_seq we scheduled | |
6113 | * | |
6114 | * However, preemptions can cause multiple picks on the same task set. | |
6115 | * 'Fix' this by also increasing @task_seq for every pick. | |
6116 | */ | |
6117 | rq->core->core_task_seq++; | |
539f6512 | 6118 | |
7afbba11 JFG |
6119 | /* |
6120 | * Optimize for common case where this CPU has no cookies | |
6121 | * and there are no cookied tasks running on siblings. | |
6122 | */ | |
6123 | if (!need_sync) { | |
bc9ffef3 | 6124 | next = pick_task(rq); |
7afbba11 JFG |
6125 | if (!next->core_cookie) { |
6126 | rq->core_pick = NULL; | |
c6047c2e JFG |
6127 | /* |
6128 | * For robustness, update the min_vruntime_fi for | |
6129 | * unconstrained picks as well. | |
6130 | */ | |
6131 | WARN_ON_ONCE(fi_before); | |
6132 | task_vruntime_update(rq, next, false); | |
5b6547ed | 6133 | goto out_set_next; |
7afbba11 | 6134 | } |
8039e96f | 6135 | } |
7afbba11 | 6136 | |
bc9ffef3 PZ |
6137 | /* |
6138 | * For each thread: do the regular task pick and find the max prio task | |
6139 | * amongst them. | |
6140 | * | |
6141 | * Tie-break prio towards the current CPU | |
6142 | */ | |
6143 | for_each_cpu_wrap(i, smt_mask, cpu) { | |
6144 | rq_i = cpu_rq(i); | |
539f6512 | 6145 | |
4feee7d1 JD |
6146 | /* |
6147 | * Current cpu always has its clock updated on entrance to | |
6148 | * pick_next_task(). If the current cpu is not the core, | |
6149 | * the core may also have been updated above. | |
6150 | */ | |
6151 | if (i != cpu && (rq_i != rq->core || !core_clock_updated)) | |
539f6512 | 6152 | update_rq_clock(rq_i); |
bc9ffef3 PZ |
6153 | |
6154 | p = rq_i->core_pick = pick_task(rq_i); | |
6155 | if (!max || prio_less(max, p, fi_before)) | |
6156 | max = p; | |
539f6512 PZ |
6157 | } |
6158 | ||
bc9ffef3 PZ |
6159 | cookie = rq->core->core_cookie = max->core_cookie; |
6160 | ||
539f6512 | 6161 | /* |
bc9ffef3 PZ |
6162 | * For each thread: try and find a runnable task that matches @max or |
6163 | * force idle. | |
539f6512 | 6164 | */ |
bc9ffef3 PZ |
6165 | for_each_cpu(i, smt_mask) { |
6166 | rq_i = cpu_rq(i); | |
6167 | p = rq_i->core_pick; | |
539f6512 | 6168 | |
bc9ffef3 PZ |
6169 | if (!cookie_equals(p, cookie)) { |
6170 | p = NULL; | |
6171 | if (cookie) | |
6172 | p = sched_core_find(rq_i, cookie); | |
7afbba11 | 6173 | if (!p) |
bc9ffef3 PZ |
6174 | p = idle_sched_class.pick_task(rq_i); |
6175 | } | |
539f6512 | 6176 | |
bc9ffef3 | 6177 | rq_i->core_pick = p; |
d2dfa17b | 6178 | |
bc9ffef3 PZ |
6179 | if (p == rq_i->idle) { |
6180 | if (rq_i->nr_running) { | |
4feee7d1 | 6181 | rq->core->core_forceidle_count++; |
c6047c2e JFG |
6182 | if (!fi_before) |
6183 | rq->core->core_forceidle_seq++; | |
6184 | } | |
bc9ffef3 PZ |
6185 | } else { |
6186 | occ++; | |
539f6512 | 6187 | } |
539f6512 PZ |
6188 | } |
6189 | ||
4feee7d1 | 6190 | if (schedstat_enabled() && rq->core->core_forceidle_count) { |
b171501f | 6191 | rq->core->core_forceidle_start = rq_clock(rq->core); |
4feee7d1 JD |
6192 | rq->core->core_forceidle_occupation = occ; |
6193 | } | |
6194 | ||
539f6512 PZ |
6195 | rq->core->core_pick_seq = rq->core->core_task_seq; |
6196 | next = rq->core_pick; | |
6197 | rq->core_sched_seq = rq->core->core_pick_seq; | |
6198 | ||
6199 | /* Something should have been selected for current CPU */ | |
6200 | WARN_ON_ONCE(!next); | |
6201 | ||
6202 | /* | |
6203 | * Reschedule siblings | |
6204 | * | |
6205 | * NOTE: L1TF -- at this point we're no longer running the old task and | |
6206 | * sending an IPI (below) ensures the sibling will no longer be running | |
6207 | * their task. This ensures there is no inter-sibling overlap between | |
6208 | * non-matching user state. | |
6209 | */ | |
6210 | for_each_cpu(i, smt_mask) { | |
bc9ffef3 | 6211 | rq_i = cpu_rq(i); |
539f6512 PZ |
6212 | |
6213 | /* | |
6214 | * An online sibling might have gone offline before a task | |
6215 | * could be picked for it, or it might be offline but later | |
6216 | * happen to come online, but its too late and nothing was | |
6217 | * picked for it. That's Ok - it will pick tasks for itself, | |
6218 | * so ignore it. | |
6219 | */ | |
6220 | if (!rq_i->core_pick) | |
6221 | continue; | |
6222 | ||
c6047c2e JFG |
6223 | /* |
6224 | * Update for new !FI->FI transitions, or if continuing to be in !FI: | |
6225 | * fi_before fi update? | |
6226 | * 0 0 1 | |
6227 | * 0 1 1 | |
6228 | * 1 0 1 | |
6229 | * 1 1 0 | |
6230 | */ | |
4feee7d1 JD |
6231 | if (!(fi_before && rq->core->core_forceidle_count)) |
6232 | task_vruntime_update(rq_i, rq_i->core_pick, !!rq->core->core_forceidle_count); | |
539f6512 | 6233 | |
d2dfa17b PZ |
6234 | rq_i->core_pick->core_occupation = occ; |
6235 | ||
539f6512 PZ |
6236 | if (i == cpu) { |
6237 | rq_i->core_pick = NULL; | |
6238 | continue; | |
6239 | } | |
6240 | ||
6241 | /* Did we break L1TF mitigation requirements? */ | |
6242 | WARN_ON_ONCE(!cookie_match(next, rq_i->core_pick)); | |
6243 | ||
6244 | if (rq_i->curr == rq_i->core_pick) { | |
6245 | rq_i->core_pick = NULL; | |
6246 | continue; | |
6247 | } | |
6248 | ||
6249 | resched_curr(rq_i); | |
6250 | } | |
6251 | ||
5b6547ed | 6252 | out_set_next: |
539f6512 | 6253 | set_next_task(rq, next); |
5b6547ed PZ |
6254 | out: |
6255 | if (rq->core->core_forceidle_count && next == rq->idle) | |
6256 | queue_core_balance(rq); | |
6257 | ||
539f6512 PZ |
6258 | return next; |
6259 | } | |
9edeaea1 | 6260 | |
d2dfa17b PZ |
6261 | static bool try_steal_cookie(int this, int that) |
6262 | { | |
6263 | struct rq *dst = cpu_rq(this), *src = cpu_rq(that); | |
6264 | struct task_struct *p; | |
6265 | unsigned long cookie; | |
6266 | bool success = false; | |
6267 | ||
6268 | local_irq_disable(); | |
6269 | double_rq_lock(dst, src); | |
6270 | ||
6271 | cookie = dst->core->core_cookie; | |
6272 | if (!cookie) | |
6273 | goto unlock; | |
6274 | ||
6275 | if (dst->curr != dst->idle) | |
6276 | goto unlock; | |
6277 | ||
6278 | p = sched_core_find(src, cookie); | |
530bfad1 | 6279 | if (!p) |
d2dfa17b PZ |
6280 | goto unlock; |
6281 | ||
6282 | do { | |
6283 | if (p == src->core_pick || p == src->curr) | |
6284 | goto next; | |
6285 | ||
386ef214 | 6286 | if (!is_cpu_allowed(p, this)) |
d2dfa17b PZ |
6287 | goto next; |
6288 | ||
6289 | if (p->core_occupation > dst->idle->core_occupation) | |
6290 | goto next; | |
530bfad1 HJ |
6291 | /* |
6292 | * sched_core_find() and sched_core_next() will ensure that task @p | |
6293 | * is not throttled now, we also need to check whether the runqueue | |
6294 | * of the destination CPU is being throttled. | |
6295 | */ | |
6296 | if (sched_task_is_throttled(p, this)) | |
6297 | goto next; | |
d2dfa17b | 6298 | |
d2dfa17b PZ |
6299 | deactivate_task(src, p, 0); |
6300 | set_task_cpu(p, this); | |
6301 | activate_task(dst, p, 0); | |
d2dfa17b PZ |
6302 | |
6303 | resched_curr(dst); | |
6304 | ||
6305 | success = true; | |
6306 | break; | |
6307 | ||
6308 | next: | |
6309 | p = sched_core_next(p, cookie); | |
6310 | } while (p); | |
6311 | ||
6312 | unlock: | |
6313 | double_rq_unlock(dst, src); | |
6314 | local_irq_enable(); | |
6315 | ||
6316 | return success; | |
6317 | } | |
6318 | ||
6319 | static bool steal_cookie_task(int cpu, struct sched_domain *sd) | |
6320 | { | |
6321 | int i; | |
6322 | ||
8589018a | 6323 | for_each_cpu_wrap(i, sched_domain_span(sd), cpu + 1) { |
d2dfa17b PZ |
6324 | if (i == cpu) |
6325 | continue; | |
6326 | ||
6327 | if (need_resched()) | |
6328 | break; | |
6329 | ||
6330 | if (try_steal_cookie(cpu, i)) | |
6331 | return true; | |
6332 | } | |
6333 | ||
6334 | return false; | |
6335 | } | |
6336 | ||
6337 | static void sched_core_balance(struct rq *rq) | |
6338 | { | |
6339 | struct sched_domain *sd; | |
6340 | int cpu = cpu_of(rq); | |
6341 | ||
6342 | preempt_disable(); | |
6343 | rcu_read_lock(); | |
6344 | raw_spin_rq_unlock_irq(rq); | |
6345 | for_each_domain(cpu, sd) { | |
6346 | if (need_resched()) | |
6347 | break; | |
6348 | ||
6349 | if (steal_cookie_task(cpu, sd)) | |
6350 | break; | |
6351 | } | |
6352 | raw_spin_rq_lock_irq(rq); | |
6353 | rcu_read_unlock(); | |
6354 | preempt_enable(); | |
6355 | } | |
6356 | ||
8e5bad7d | 6357 | static DEFINE_PER_CPU(struct balance_callback, core_balance_head); |
d2dfa17b | 6358 | |
5b6547ed | 6359 | static void queue_core_balance(struct rq *rq) |
d2dfa17b PZ |
6360 | { |
6361 | if (!sched_core_enabled(rq)) | |
6362 | return; | |
6363 | ||
6364 | if (!rq->core->core_cookie) | |
6365 | return; | |
6366 | ||
6367 | if (!rq->nr_running) /* not forced idle */ | |
6368 | return; | |
6369 | ||
6370 | queue_balance_callback(rq, &per_cpu(core_balance_head, rq->cpu), sched_core_balance); | |
6371 | } | |
6372 | ||
3c474b32 | 6373 | static void sched_core_cpu_starting(unsigned int cpu) |
9edeaea1 PZ |
6374 | { |
6375 | const struct cpumask *smt_mask = cpu_smt_mask(cpu); | |
3c474b32 PZ |
6376 | struct rq *rq = cpu_rq(cpu), *core_rq = NULL; |
6377 | unsigned long flags; | |
6378 | int t; | |
9edeaea1 | 6379 | |
3c474b32 | 6380 | sched_core_lock(cpu, &flags); |
9edeaea1 | 6381 | |
3c474b32 PZ |
6382 | WARN_ON_ONCE(rq->core != rq); |
6383 | ||
6384 | /* if we're the first, we'll be our own leader */ | |
6385 | if (cpumask_weight(smt_mask) == 1) | |
6386 | goto unlock; | |
6387 | ||
6388 | /* find the leader */ | |
6389 | for_each_cpu(t, smt_mask) { | |
6390 | if (t == cpu) | |
6391 | continue; | |
6392 | rq = cpu_rq(t); | |
6393 | if (rq->core == rq) { | |
6394 | core_rq = rq; | |
6395 | break; | |
9edeaea1 | 6396 | } |
3c474b32 | 6397 | } |
9edeaea1 | 6398 | |
3c474b32 PZ |
6399 | if (WARN_ON_ONCE(!core_rq)) /* whoopsie */ |
6400 | goto unlock; | |
9edeaea1 | 6401 | |
3c474b32 PZ |
6402 | /* install and validate core_rq */ |
6403 | for_each_cpu(t, smt_mask) { | |
6404 | rq = cpu_rq(t); | |
9edeaea1 | 6405 | |
3c474b32 | 6406 | if (t == cpu) |
9edeaea1 | 6407 | rq->core = core_rq; |
3c474b32 PZ |
6408 | |
6409 | WARN_ON_ONCE(rq->core != core_rq); | |
9edeaea1 | 6410 | } |
3c474b32 PZ |
6411 | |
6412 | unlock: | |
6413 | sched_core_unlock(cpu, &flags); | |
9edeaea1 | 6414 | } |
3c474b32 PZ |
6415 | |
6416 | static void sched_core_cpu_deactivate(unsigned int cpu) | |
6417 | { | |
6418 | const struct cpumask *smt_mask = cpu_smt_mask(cpu); | |
6419 | struct rq *rq = cpu_rq(cpu), *core_rq = NULL; | |
6420 | unsigned long flags; | |
6421 | int t; | |
6422 | ||
6423 | sched_core_lock(cpu, &flags); | |
6424 | ||
6425 | /* if we're the last man standing, nothing to do */ | |
6426 | if (cpumask_weight(smt_mask) == 1) { | |
6427 | WARN_ON_ONCE(rq->core != rq); | |
6428 | goto unlock; | |
6429 | } | |
6430 | ||
6431 | /* if we're not the leader, nothing to do */ | |
6432 | if (rq->core != rq) | |
6433 | goto unlock; | |
6434 | ||
6435 | /* find a new leader */ | |
6436 | for_each_cpu(t, smt_mask) { | |
6437 | if (t == cpu) | |
6438 | continue; | |
6439 | core_rq = cpu_rq(t); | |
6440 | break; | |
6441 | } | |
6442 | ||
6443 | if (WARN_ON_ONCE(!core_rq)) /* impossible */ | |
6444 | goto unlock; | |
6445 | ||
6446 | /* copy the shared state to the new leader */ | |
4feee7d1 JD |
6447 | core_rq->core_task_seq = rq->core_task_seq; |
6448 | core_rq->core_pick_seq = rq->core_pick_seq; | |
6449 | core_rq->core_cookie = rq->core_cookie; | |
6450 | core_rq->core_forceidle_count = rq->core_forceidle_count; | |
6451 | core_rq->core_forceidle_seq = rq->core_forceidle_seq; | |
6452 | core_rq->core_forceidle_occupation = rq->core_forceidle_occupation; | |
6453 | ||
6454 | /* | |
6455 | * Accounting edge for forced idle is handled in pick_next_task(). | |
6456 | * Don't need another one here, since the hotplug thread shouldn't | |
6457 | * have a cookie. | |
6458 | */ | |
6459 | core_rq->core_forceidle_start = 0; | |
3c474b32 PZ |
6460 | |
6461 | /* install new leader */ | |
6462 | for_each_cpu(t, smt_mask) { | |
6463 | rq = cpu_rq(t); | |
6464 | rq->core = core_rq; | |
6465 | } | |
6466 | ||
6467 | unlock: | |
6468 | sched_core_unlock(cpu, &flags); | |
6469 | } | |
6470 | ||
6471 | static inline void sched_core_cpu_dying(unsigned int cpu) | |
6472 | { | |
6473 | struct rq *rq = cpu_rq(cpu); | |
6474 | ||
6475 | if (rq->core != rq) | |
6476 | rq->core = rq; | |
6477 | } | |
6478 | ||
9edeaea1 PZ |
6479 | #else /* !CONFIG_SCHED_CORE */ |
6480 | ||
6481 | static inline void sched_core_cpu_starting(unsigned int cpu) {} | |
3c474b32 PZ |
6482 | static inline void sched_core_cpu_deactivate(unsigned int cpu) {} |
6483 | static inline void sched_core_cpu_dying(unsigned int cpu) {} | |
9edeaea1 | 6484 | |
539f6512 PZ |
6485 | static struct task_struct * |
6486 | pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) | |
6487 | { | |
6488 | return __pick_next_task(rq, prev, rf); | |
6489 | } | |
6490 | ||
9edeaea1 PZ |
6491 | #endif /* CONFIG_SCHED_CORE */ |
6492 | ||
b4bfa3fc TG |
6493 | /* |
6494 | * Constants for the sched_mode argument of __schedule(). | |
6495 | * | |
6496 | * The mode argument allows RT enabled kernels to differentiate a | |
6497 | * preemption from blocking on an 'sleeping' spin/rwlock. Note that | |
6498 | * SM_MASK_PREEMPT for !RT has all bits set, which allows the compiler to | |
6499 | * optimize the AND operation out and just check for zero. | |
6500 | */ | |
6501 | #define SM_NONE 0x0 | |
6502 | #define SM_PREEMPT 0x1 | |
6991436c TG |
6503 | #define SM_RTLOCK_WAIT 0x2 |
6504 | ||
6505 | #ifndef CONFIG_PREEMPT_RT | |
6506 | # define SM_MASK_PREEMPT (~0U) | |
6507 | #else | |
6508 | # define SM_MASK_PREEMPT SM_PREEMPT | |
6509 | #endif | |
b4bfa3fc | 6510 | |
dd41f596 | 6511 | /* |
c259e01a | 6512 | * __schedule() is the main scheduler function. |
edde96ea PE |
6513 | * |
6514 | * The main means of driving the scheduler and thus entering this function are: | |
6515 | * | |
6516 | * 1. Explicit blocking: mutex, semaphore, waitqueue, etc. | |
6517 | * | |
6518 | * 2. TIF_NEED_RESCHED flag is checked on interrupt and userspace return | |
6519 | * paths. For example, see arch/x86/entry_64.S. | |
6520 | * | |
6521 | * To drive preemption between tasks, the scheduler sets the flag in timer | |
6522 | * interrupt handler scheduler_tick(). | |
6523 | * | |
6524 | * 3. Wakeups don't really cause entry into schedule(). They add a | |
6525 | * task to the run-queue and that's it. | |
6526 | * | |
6527 | * Now, if the new task added to the run-queue preempts the current | |
6528 | * task, then the wakeup sets TIF_NEED_RESCHED and schedule() gets | |
6529 | * called on the nearest possible occasion: | |
6530 | * | |
c1a280b6 | 6531 | * - If the kernel is preemptible (CONFIG_PREEMPTION=y): |
edde96ea PE |
6532 | * |
6533 | * - in syscall or exception context, at the next outmost | |
6534 | * preempt_enable(). (this might be as soon as the wake_up()'s | |
6535 | * spin_unlock()!) | |
6536 | * | |
6537 | * - in IRQ context, return from interrupt-handler to | |
6538 | * preemptible context | |
6539 | * | |
c1a280b6 | 6540 | * - If the kernel is not preemptible (CONFIG_PREEMPTION is not set) |
edde96ea PE |
6541 | * then at the next: |
6542 | * | |
6543 | * - cond_resched() call | |
6544 | * - explicit schedule() call | |
6545 | * - return from syscall or exception to user-space | |
6546 | * - return from interrupt-handler to user-space | |
bfd9b2b5 | 6547 | * |
b30f0e3f | 6548 | * WARNING: must be called with preemption disabled! |
dd41f596 | 6549 | */ |
b4bfa3fc | 6550 | static void __sched notrace __schedule(unsigned int sched_mode) |
dd41f596 IM |
6551 | { |
6552 | struct task_struct *prev, *next; | |
67ca7bde | 6553 | unsigned long *switch_count; |
dbfb089d | 6554 | unsigned long prev_state; |
d8ac8971 | 6555 | struct rq_flags rf; |
dd41f596 | 6556 | struct rq *rq; |
31656519 | 6557 | int cpu; |
dd41f596 | 6558 | |
dd41f596 IM |
6559 | cpu = smp_processor_id(); |
6560 | rq = cpu_rq(cpu); | |
dd41f596 | 6561 | prev = rq->curr; |
dd41f596 | 6562 | |
b4bfa3fc | 6563 | schedule_debug(prev, !!sched_mode); |
1da177e4 | 6564 | |
e0ee463c | 6565 | if (sched_feat(HRTICK) || sched_feat(HRTICK_DL)) |
f333fdc9 | 6566 | hrtick_clear(rq); |
8f4d37ec | 6567 | |
46a5d164 | 6568 | local_irq_disable(); |
b4bfa3fc | 6569 | rcu_note_context_switch(!!sched_mode); |
46a5d164 | 6570 | |
e0acd0a6 ON |
6571 | /* |
6572 | * Make sure that signal_pending_state()->signal_pending() below | |
6573 | * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE) | |
dbfb089d PZ |
6574 | * done by the caller to avoid the race with signal_wake_up(): |
6575 | * | |
6576 | * __set_current_state(@state) signal_wake_up() | |
6577 | * schedule() set_tsk_thread_flag(p, TIF_SIGPENDING) | |
6578 | * wake_up_state(p, state) | |
6579 | * LOCK rq->lock LOCK p->pi_state | |
6580 | * smp_mb__after_spinlock() smp_mb__after_spinlock() | |
6581 | * if (signal_pending_state()) if (p->state & @state) | |
306e0604 | 6582 | * |
dbfb089d | 6583 | * Also, the membarrier system call requires a full memory barrier |
306e0604 | 6584 | * after coming from user-space, before storing to rq->curr. |
e0acd0a6 | 6585 | */ |
8a8c69c3 | 6586 | rq_lock(rq, &rf); |
d89e588c | 6587 | smp_mb__after_spinlock(); |
1da177e4 | 6588 | |
d1ccc66d IM |
6589 | /* Promote REQ to ACT */ |
6590 | rq->clock_update_flags <<= 1; | |
bce4dc80 | 6591 | update_rq_clock(rq); |
9edfbfed | 6592 | |
246d86b5 | 6593 | switch_count = &prev->nivcsw; |
d136122f | 6594 | |
dbfb089d | 6595 | /* |
d136122f | 6596 | * We must load prev->state once (task_struct::state is volatile), such |
2500ad1c | 6597 | * that we form a control dependency vs deactivate_task() below. |
dbfb089d | 6598 | */ |
2f064a59 | 6599 | prev_state = READ_ONCE(prev->__state); |
b4bfa3fc | 6600 | if (!(sched_mode & SM_MASK_PREEMPT) && prev_state) { |
dbfb089d | 6601 | if (signal_pending_state(prev_state, prev)) { |
2f064a59 | 6602 | WRITE_ONCE(prev->__state, TASK_RUNNING); |
21aa9af0 | 6603 | } else { |
dbfb089d PZ |
6604 | prev->sched_contributes_to_load = |
6605 | (prev_state & TASK_UNINTERRUPTIBLE) && | |
6606 | !(prev_state & TASK_NOLOAD) && | |
f5d39b02 | 6607 | !(prev_state & TASK_FROZEN); |
dbfb089d PZ |
6608 | |
6609 | if (prev->sched_contributes_to_load) | |
6610 | rq->nr_uninterruptible++; | |
6611 | ||
6612 | /* | |
6613 | * __schedule() ttwu() | |
d136122f PZ |
6614 | * prev_state = prev->state; if (p->on_rq && ...) |
6615 | * if (prev_state) goto out; | |
6616 | * p->on_rq = 0; smp_acquire__after_ctrl_dep(); | |
6617 | * p->state = TASK_WAKING | |
6618 | * | |
6619 | * Where __schedule() and ttwu() have matching control dependencies. | |
dbfb089d PZ |
6620 | * |
6621 | * After this, schedule() must not care about p->state any more. | |
6622 | */ | |
bce4dc80 | 6623 | deactivate_task(rq, prev, DEQUEUE_SLEEP | DEQUEUE_NOCLOCK); |
2acca55e | 6624 | |
e33a9bba TH |
6625 | if (prev->in_iowait) { |
6626 | atomic_inc(&rq->nr_iowait); | |
6627 | delayacct_blkio_start(); | |
6628 | } | |
21aa9af0 | 6629 | } |
dd41f596 | 6630 | switch_count = &prev->nvcsw; |
1da177e4 LT |
6631 | } |
6632 | ||
d8ac8971 | 6633 | next = pick_next_task(rq, prev, &rf); |
f26f9aff | 6634 | clear_tsk_need_resched(prev); |
f27dde8d | 6635 | clear_preempt_need_resched(); |
c006fac5 PT |
6636 | #ifdef CONFIG_SCHED_DEBUG |
6637 | rq->last_seen_need_resched_ns = 0; | |
6638 | #endif | |
1da177e4 | 6639 | |
1da177e4 | 6640 | if (likely(prev != next)) { |
1da177e4 | 6641 | rq->nr_switches++; |
5311a98f EB |
6642 | /* |
6643 | * RCU users of rcu_dereference(rq->curr) may not see | |
6644 | * changes to task_struct made by pick_next_task(). | |
6645 | */ | |
6646 | RCU_INIT_POINTER(rq->curr, next); | |
22e4ebb9 MD |
6647 | /* |
6648 | * The membarrier system call requires each architecture | |
6649 | * to have a full memory barrier after updating | |
306e0604 MD |
6650 | * rq->curr, before returning to user-space. |
6651 | * | |
6652 | * Here are the schemes providing that barrier on the | |
6653 | * various architectures: | |
6654 | * - mm ? switch_mm() : mmdrop() for x86, s390, sparc, PowerPC. | |
6655 | * switch_mm() rely on membarrier_arch_switch_mm() on PowerPC. | |
6656 | * - finish_lock_switch() for weakly-ordered | |
6657 | * architectures where spin_unlock is a full barrier, | |
6658 | * - switch_to() for arm64 (weakly-ordered, spin_unlock | |
6659 | * is a RELEASE barrier), | |
22e4ebb9 | 6660 | */ |
1da177e4 LT |
6661 | ++*switch_count; |
6662 | ||
af449901 | 6663 | migrate_disable_switch(rq, prev); |
b05e75d6 JW |
6664 | psi_sched_switch(prev, next, !task_on_rq_queued(prev)); |
6665 | ||
9c2136be | 6666 | trace_sched_switch(sched_mode & SM_MASK_PREEMPT, prev, next, prev_state); |
d1ccc66d IM |
6667 | |
6668 | /* Also unlocks the rq: */ | |
6669 | rq = context_switch(rq, prev, next, &rf); | |
cbce1a68 | 6670 | } else { |
cb42c9a3 | 6671 | rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP); |
1da177e4 | 6672 | |
565790d2 PZ |
6673 | rq_unpin_lock(rq, &rf); |
6674 | __balance_callbacks(rq); | |
5cb9eaa3 | 6675 | raw_spin_rq_unlock_irq(rq); |
565790d2 | 6676 | } |
1da177e4 | 6677 | } |
c259e01a | 6678 | |
9af6528e PZ |
6679 | void __noreturn do_task_dead(void) |
6680 | { | |
d1ccc66d | 6681 | /* Causes final put_task_struct in finish_task_switch(): */ |
b5bf9a90 | 6682 | set_special_state(TASK_DEAD); |
d1ccc66d IM |
6683 | |
6684 | /* Tell freezer to ignore us: */ | |
6685 | current->flags |= PF_NOFREEZE; | |
6686 | ||
b4bfa3fc | 6687 | __schedule(SM_NONE); |
9af6528e | 6688 | BUG(); |
d1ccc66d IM |
6689 | |
6690 | /* Avoid "noreturn function does return" - but don't continue if BUG() is a NOP: */ | |
9af6528e | 6691 | for (;;) |
d1ccc66d | 6692 | cpu_relax(); |
9af6528e PZ |
6693 | } |
6694 | ||
9c40cef2 TG |
6695 | static inline void sched_submit_work(struct task_struct *tsk) |
6696 | { | |
c1cecf88 SAS |
6697 | unsigned int task_flags; |
6698 | ||
b03fbd4f | 6699 | if (task_is_running(tsk)) |
9c40cef2 | 6700 | return; |
6d25be57 | 6701 | |
c1cecf88 | 6702 | task_flags = tsk->flags; |
6d25be57 | 6703 | /* |
b945efcd TG |
6704 | * If a worker goes to sleep, notify and ask workqueue whether it |
6705 | * wants to wake up a task to maintain concurrency. | |
6d25be57 | 6706 | */ |
c1cecf88 | 6707 | if (task_flags & (PF_WQ_WORKER | PF_IO_WORKER)) { |
c1cecf88 | 6708 | if (task_flags & PF_WQ_WORKER) |
771b53d0 JA |
6709 | wq_worker_sleeping(tsk); |
6710 | else | |
6711 | io_wq_worker_sleeping(tsk); | |
6d25be57 TG |
6712 | } |
6713 | ||
401e4963 JK |
6714 | /* |
6715 | * spinlock and rwlock must not flush block requests. This will | |
6716 | * deadlock if the callback attempts to acquire a lock which is | |
6717 | * already acquired. | |
6718 | */ | |
6719 | SCHED_WARN_ON(current->__state & TASK_RTLOCK_WAIT); | |
b0fdc013 | 6720 | |
9c40cef2 TG |
6721 | /* |
6722 | * If we are going to sleep and we have plugged IO queued, | |
6723 | * make sure to submit it to avoid deadlocks. | |
6724 | */ | |
aa8dccca | 6725 | blk_flush_plug(tsk->plug, true); |
9c40cef2 TG |
6726 | } |
6727 | ||
6d25be57 TG |
6728 | static void sched_update_worker(struct task_struct *tsk) |
6729 | { | |
771b53d0 JA |
6730 | if (tsk->flags & (PF_WQ_WORKER | PF_IO_WORKER)) { |
6731 | if (tsk->flags & PF_WQ_WORKER) | |
6732 | wq_worker_running(tsk); | |
6733 | else | |
6734 | io_wq_worker_running(tsk); | |
6735 | } | |
6d25be57 TG |
6736 | } |
6737 | ||
722a9f92 | 6738 | asmlinkage __visible void __sched schedule(void) |
c259e01a | 6739 | { |
9c40cef2 TG |
6740 | struct task_struct *tsk = current; |
6741 | ||
6742 | sched_submit_work(tsk); | |
bfd9b2b5 | 6743 | do { |
b30f0e3f | 6744 | preempt_disable(); |
b4bfa3fc | 6745 | __schedule(SM_NONE); |
b30f0e3f | 6746 | sched_preempt_enable_no_resched(); |
bfd9b2b5 | 6747 | } while (need_resched()); |
6d25be57 | 6748 | sched_update_worker(tsk); |
c259e01a | 6749 | } |
1da177e4 LT |
6750 | EXPORT_SYMBOL(schedule); |
6751 | ||
8663effb SRV |
6752 | /* |
6753 | * synchronize_rcu_tasks() makes sure that no task is stuck in preempted | |
6754 | * state (have scheduled out non-voluntarily) by making sure that all | |
6755 | * tasks have either left the run queue or have gone into user space. | |
6756 | * As idle tasks do not do either, they must not ever be preempted | |
6757 | * (schedule out non-voluntarily). | |
6758 | * | |
6759 | * schedule_idle() is similar to schedule_preempt_disable() except that it | |
6760 | * never enables preemption because it does not call sched_submit_work(). | |
6761 | */ | |
6762 | void __sched schedule_idle(void) | |
6763 | { | |
6764 | /* | |
6765 | * As this skips calling sched_submit_work(), which the idle task does | |
6766 | * regardless because that function is a nop when the task is in a | |
6767 | * TASK_RUNNING state, make sure this isn't used someplace that the | |
6768 | * current task can be in any other state. Note, idle is always in the | |
6769 | * TASK_RUNNING state. | |
6770 | */ | |
2f064a59 | 6771 | WARN_ON_ONCE(current->__state); |
8663effb | 6772 | do { |
b4bfa3fc | 6773 | __schedule(SM_NONE); |
8663effb SRV |
6774 | } while (need_resched()); |
6775 | } | |
6776 | ||
24a9c541 | 6777 | #if defined(CONFIG_CONTEXT_TRACKING_USER) && !defined(CONFIG_HAVE_CONTEXT_TRACKING_USER_OFFSTACK) |
722a9f92 | 6778 | asmlinkage __visible void __sched schedule_user(void) |
20ab65e3 FW |
6779 | { |
6780 | /* | |
6781 | * If we come here after a random call to set_need_resched(), | |
6782 | * or we have been woken up remotely but the IPI has not yet arrived, | |
6783 | * we haven't yet exited the RCU idle mode. Do it here manually until | |
6784 | * we find a better solution. | |
7cc78f8f AL |
6785 | * |
6786 | * NB: There are buggy callers of this function. Ideally we | |
c467ea76 | 6787 | * should warn if prev_state != CONTEXT_USER, but that will trigger |
7cc78f8f | 6788 | * too frequently to make sense yet. |
20ab65e3 | 6789 | */ |
7cc78f8f | 6790 | enum ctx_state prev_state = exception_enter(); |
20ab65e3 | 6791 | schedule(); |
7cc78f8f | 6792 | exception_exit(prev_state); |
20ab65e3 FW |
6793 | } |
6794 | #endif | |
6795 | ||
c5491ea7 TG |
6796 | /** |
6797 | * schedule_preempt_disabled - called with preemption disabled | |
6798 | * | |
6799 | * Returns with preemption disabled. Note: preempt_count must be 1 | |
6800 | */ | |
6801 | void __sched schedule_preempt_disabled(void) | |
6802 | { | |
ba74c144 | 6803 | sched_preempt_enable_no_resched(); |
c5491ea7 TG |
6804 | schedule(); |
6805 | preempt_disable(); | |
6806 | } | |
6807 | ||
6991436c TG |
6808 | #ifdef CONFIG_PREEMPT_RT |
6809 | void __sched notrace schedule_rtlock(void) | |
6810 | { | |
6811 | do { | |
6812 | preempt_disable(); | |
6813 | __schedule(SM_RTLOCK_WAIT); | |
6814 | sched_preempt_enable_no_resched(); | |
6815 | } while (need_resched()); | |
6816 | } | |
6817 | NOKPROBE_SYMBOL(schedule_rtlock); | |
6818 | #endif | |
6819 | ||
06b1f808 | 6820 | static void __sched notrace preempt_schedule_common(void) |
a18b5d01 FW |
6821 | { |
6822 | do { | |
47252cfb SR |
6823 | /* |
6824 | * Because the function tracer can trace preempt_count_sub() | |
6825 | * and it also uses preempt_enable/disable_notrace(), if | |
6826 | * NEED_RESCHED is set, the preempt_enable_notrace() called | |
6827 | * by the function tracer will call this function again and | |
6828 | * cause infinite recursion. | |
6829 | * | |
6830 | * Preemption must be disabled here before the function | |
6831 | * tracer can trace. Break up preempt_disable() into two | |
6832 | * calls. One to disable preemption without fear of being | |
6833 | * traced. The other to still record the preemption latency, | |
6834 | * which can also be traced by the function tracer. | |
6835 | */ | |
499d7955 | 6836 | preempt_disable_notrace(); |
47252cfb | 6837 | preempt_latency_start(1); |
b4bfa3fc | 6838 | __schedule(SM_PREEMPT); |
47252cfb | 6839 | preempt_latency_stop(1); |
499d7955 | 6840 | preempt_enable_no_resched_notrace(); |
a18b5d01 FW |
6841 | |
6842 | /* | |
6843 | * Check again in case we missed a preemption opportunity | |
6844 | * between schedule and now. | |
6845 | */ | |
a18b5d01 FW |
6846 | } while (need_resched()); |
6847 | } | |
6848 | ||
c1a280b6 | 6849 | #ifdef CONFIG_PREEMPTION |
1da177e4 | 6850 | /* |
a49b4f40 VS |
6851 | * This is the entry point to schedule() from in-kernel preemption |
6852 | * off of preempt_enable. | |
1da177e4 | 6853 | */ |
722a9f92 | 6854 | asmlinkage __visible void __sched notrace preempt_schedule(void) |
1da177e4 | 6855 | { |
1da177e4 LT |
6856 | /* |
6857 | * If there is a non-zero preempt_count or interrupts are disabled, | |
41a2d6cf | 6858 | * we do not want to preempt the current task. Just return.. |
1da177e4 | 6859 | */ |
fbb00b56 | 6860 | if (likely(!preemptible())) |
1da177e4 | 6861 | return; |
a18b5d01 | 6862 | preempt_schedule_common(); |
1da177e4 | 6863 | } |
376e2424 | 6864 | NOKPROBE_SYMBOL(preempt_schedule); |
1da177e4 | 6865 | EXPORT_SYMBOL(preempt_schedule); |
009f60e2 | 6866 | |
2c9a98d3 | 6867 | #ifdef CONFIG_PREEMPT_DYNAMIC |
99cf983c | 6868 | #if defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL) |
8a69fe0b MR |
6869 | #ifndef preempt_schedule_dynamic_enabled |
6870 | #define preempt_schedule_dynamic_enabled preempt_schedule | |
6871 | #define preempt_schedule_dynamic_disabled NULL | |
6872 | #endif | |
6873 | DEFINE_STATIC_CALL(preempt_schedule, preempt_schedule_dynamic_enabled); | |
ef72661e | 6874 | EXPORT_STATIC_CALL_TRAMP(preempt_schedule); |
99cf983c MR |
6875 | #elif defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY) |
6876 | static DEFINE_STATIC_KEY_TRUE(sk_dynamic_preempt_schedule); | |
6877 | void __sched notrace dynamic_preempt_schedule(void) | |
6878 | { | |
6879 | if (!static_branch_unlikely(&sk_dynamic_preempt_schedule)) | |
6880 | return; | |
6881 | preempt_schedule(); | |
6882 | } | |
6883 | NOKPROBE_SYMBOL(dynamic_preempt_schedule); | |
6884 | EXPORT_SYMBOL(dynamic_preempt_schedule); | |
6885 | #endif | |
2c9a98d3 | 6886 | #endif |
2c9a98d3 | 6887 | |
009f60e2 | 6888 | /** |
4eaca0a8 | 6889 | * preempt_schedule_notrace - preempt_schedule called by tracing |
009f60e2 ON |
6890 | * |
6891 | * The tracing infrastructure uses preempt_enable_notrace to prevent | |
6892 | * recursion and tracing preempt enabling caused by the tracing | |
6893 | * infrastructure itself. But as tracing can happen in areas coming | |
6894 | * from userspace or just about to enter userspace, a preempt enable | |
6895 | * can occur before user_exit() is called. This will cause the scheduler | |
6896 | * to be called when the system is still in usermode. | |
6897 | * | |
6898 | * To prevent this, the preempt_enable_notrace will use this function | |
6899 | * instead of preempt_schedule() to exit user context if needed before | |
6900 | * calling the scheduler. | |
6901 | */ | |
4eaca0a8 | 6902 | asmlinkage __visible void __sched notrace preempt_schedule_notrace(void) |
009f60e2 ON |
6903 | { |
6904 | enum ctx_state prev_ctx; | |
6905 | ||
6906 | if (likely(!preemptible())) | |
6907 | return; | |
6908 | ||
6909 | do { | |
47252cfb SR |
6910 | /* |
6911 | * Because the function tracer can trace preempt_count_sub() | |
6912 | * and it also uses preempt_enable/disable_notrace(), if | |
6913 | * NEED_RESCHED is set, the preempt_enable_notrace() called | |
6914 | * by the function tracer will call this function again and | |
6915 | * cause infinite recursion. | |
6916 | * | |
6917 | * Preemption must be disabled here before the function | |
6918 | * tracer can trace. Break up preempt_disable() into two | |
6919 | * calls. One to disable preemption without fear of being | |
6920 | * traced. The other to still record the preemption latency, | |
6921 | * which can also be traced by the function tracer. | |
6922 | */ | |
3d8f74dd | 6923 | preempt_disable_notrace(); |
47252cfb | 6924 | preempt_latency_start(1); |
009f60e2 ON |
6925 | /* |
6926 | * Needs preempt disabled in case user_exit() is traced | |
6927 | * and the tracer calls preempt_enable_notrace() causing | |
6928 | * an infinite recursion. | |
6929 | */ | |
6930 | prev_ctx = exception_enter(); | |
b4bfa3fc | 6931 | __schedule(SM_PREEMPT); |
009f60e2 ON |
6932 | exception_exit(prev_ctx); |
6933 | ||
47252cfb | 6934 | preempt_latency_stop(1); |
3d8f74dd | 6935 | preempt_enable_no_resched_notrace(); |
009f60e2 ON |
6936 | } while (need_resched()); |
6937 | } | |
4eaca0a8 | 6938 | EXPORT_SYMBOL_GPL(preempt_schedule_notrace); |
009f60e2 | 6939 | |
2c9a98d3 | 6940 | #ifdef CONFIG_PREEMPT_DYNAMIC |
99cf983c | 6941 | #if defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL) |
8a69fe0b MR |
6942 | #ifndef preempt_schedule_notrace_dynamic_enabled |
6943 | #define preempt_schedule_notrace_dynamic_enabled preempt_schedule_notrace | |
6944 | #define preempt_schedule_notrace_dynamic_disabled NULL | |
2c9a98d3 | 6945 | #endif |
8a69fe0b | 6946 | DEFINE_STATIC_CALL(preempt_schedule_notrace, preempt_schedule_notrace_dynamic_enabled); |
ef72661e | 6947 | EXPORT_STATIC_CALL_TRAMP(preempt_schedule_notrace); |
99cf983c MR |
6948 | #elif defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY) |
6949 | static DEFINE_STATIC_KEY_TRUE(sk_dynamic_preempt_schedule_notrace); | |
6950 | void __sched notrace dynamic_preempt_schedule_notrace(void) | |
c597bfdd | 6951 | { |
99cf983c MR |
6952 | if (!static_branch_unlikely(&sk_dynamic_preempt_schedule_notrace)) |
6953 | return; | |
6954 | preempt_schedule_notrace(); | |
c597bfdd | 6955 | } |
99cf983c MR |
6956 | NOKPROBE_SYMBOL(dynamic_preempt_schedule_notrace); |
6957 | EXPORT_SYMBOL(dynamic_preempt_schedule_notrace); | |
6958 | #endif | |
2c9a98d3 | 6959 | #endif |
c597bfdd | 6960 | |
c1a280b6 | 6961 | #endif /* CONFIG_PREEMPTION */ |
826bfeb3 | 6962 | |
1da177e4 | 6963 | /* |
a49b4f40 | 6964 | * This is the entry point to schedule() from kernel preemption |
1da177e4 LT |
6965 | * off of irq context. |
6966 | * Note, that this is called and return with irqs disabled. This will | |
6967 | * protect us against recursive calling from irq. | |
6968 | */ | |
722a9f92 | 6969 | asmlinkage __visible void __sched preempt_schedule_irq(void) |
1da177e4 | 6970 | { |
b22366cd | 6971 | enum ctx_state prev_state; |
6478d880 | 6972 | |
2ed6e34f | 6973 | /* Catch callers which need to be fixed */ |
f27dde8d | 6974 | BUG_ON(preempt_count() || !irqs_disabled()); |
1da177e4 | 6975 | |
b22366cd FW |
6976 | prev_state = exception_enter(); |
6977 | ||
3a5c359a | 6978 | do { |
3d8f74dd | 6979 | preempt_disable(); |
3a5c359a | 6980 | local_irq_enable(); |
b4bfa3fc | 6981 | __schedule(SM_PREEMPT); |
3a5c359a | 6982 | local_irq_disable(); |
3d8f74dd | 6983 | sched_preempt_enable_no_resched(); |
5ed0cec0 | 6984 | } while (need_resched()); |
b22366cd FW |
6985 | |
6986 | exception_exit(prev_state); | |
1da177e4 LT |
6987 | } |
6988 | ||
ac6424b9 | 6989 | int default_wake_function(wait_queue_entry_t *curr, unsigned mode, int wake_flags, |
95cdf3b7 | 6990 | void *key) |
1da177e4 | 6991 | { |
062d3f95 | 6992 | WARN_ON_ONCE(IS_ENABLED(CONFIG_SCHED_DEBUG) && wake_flags & ~WF_SYNC); |
63859d4f | 6993 | return try_to_wake_up(curr->private, mode, wake_flags); |
1da177e4 | 6994 | } |
1da177e4 LT |
6995 | EXPORT_SYMBOL(default_wake_function); |
6996 | ||
f558c2b8 PZ |
6997 | static void __setscheduler_prio(struct task_struct *p, int prio) |
6998 | { | |
6999 | if (dl_prio(prio)) | |
7000 | p->sched_class = &dl_sched_class; | |
7001 | else if (rt_prio(prio)) | |
7002 | p->sched_class = &rt_sched_class; | |
7003 | else | |
7004 | p->sched_class = &fair_sched_class; | |
7005 | ||
7006 | p->prio = prio; | |
7007 | } | |
7008 | ||
b29739f9 IM |
7009 | #ifdef CONFIG_RT_MUTEXES |
7010 | ||
acd58620 PZ |
7011 | static inline int __rt_effective_prio(struct task_struct *pi_task, int prio) |
7012 | { | |
7013 | if (pi_task) | |
7014 | prio = min(prio, pi_task->prio); | |
7015 | ||
7016 | return prio; | |
7017 | } | |
7018 | ||
7019 | static inline int rt_effective_prio(struct task_struct *p, int prio) | |
7020 | { | |
7021 | struct task_struct *pi_task = rt_mutex_get_top_task(p); | |
7022 | ||
7023 | return __rt_effective_prio(pi_task, prio); | |
7024 | } | |
7025 | ||
b29739f9 IM |
7026 | /* |
7027 | * rt_mutex_setprio - set the current priority of a task | |
acd58620 PZ |
7028 | * @p: task to boost |
7029 | * @pi_task: donor task | |
b29739f9 IM |
7030 | * |
7031 | * This function changes the 'effective' priority of a task. It does | |
7032 | * not touch ->normal_prio like __setscheduler(). | |
7033 | * | |
c365c292 TG |
7034 | * Used by the rt_mutex code to implement priority inheritance |
7035 | * logic. Call site only calls if the priority of the task changed. | |
b29739f9 | 7036 | */ |
acd58620 | 7037 | void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task) |
b29739f9 | 7038 | { |
acd58620 | 7039 | int prio, oldprio, queued, running, queue_flag = |
7a57f32a | 7040 | DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK; |
83ab0aa0 | 7041 | const struct sched_class *prev_class; |
eb580751 PZ |
7042 | struct rq_flags rf; |
7043 | struct rq *rq; | |
b29739f9 | 7044 | |
acd58620 PZ |
7045 | /* XXX used to be waiter->prio, not waiter->task->prio */ |
7046 | prio = __rt_effective_prio(pi_task, p->normal_prio); | |
7047 | ||
7048 | /* | |
7049 | * If nothing changed; bail early. | |
7050 | */ | |
7051 | if (p->pi_top_task == pi_task && prio == p->prio && !dl_prio(prio)) | |
7052 | return; | |
b29739f9 | 7053 | |
eb580751 | 7054 | rq = __task_rq_lock(p, &rf); |
80f5c1b8 | 7055 | update_rq_clock(rq); |
acd58620 PZ |
7056 | /* |
7057 | * Set under pi_lock && rq->lock, such that the value can be used under | |
7058 | * either lock. | |
7059 | * | |
7060 | * Note that there is loads of tricky to make this pointer cache work | |
7061 | * right. rt_mutex_slowunlock()+rt_mutex_postunlock() work together to | |
7062 | * ensure a task is de-boosted (pi_task is set to NULL) before the | |
7063 | * task is allowed to run again (and can exit). This ensures the pointer | |
b19a888c | 7064 | * points to a blocked task -- which guarantees the task is present. |
acd58620 PZ |
7065 | */ |
7066 | p->pi_top_task = pi_task; | |
7067 | ||
7068 | /* | |
7069 | * For FIFO/RR we only need to set prio, if that matches we're done. | |
7070 | */ | |
7071 | if (prio == p->prio && !dl_prio(prio)) | |
7072 | goto out_unlock; | |
b29739f9 | 7073 | |
1c4dd99b TG |
7074 | /* |
7075 | * Idle task boosting is a nono in general. There is one | |
7076 | * exception, when PREEMPT_RT and NOHZ is active: | |
7077 | * | |
7078 | * The idle task calls get_next_timer_interrupt() and holds | |
7079 | * the timer wheel base->lock on the CPU and another CPU wants | |
7080 | * to access the timer (probably to cancel it). We can safely | |
7081 | * ignore the boosting request, as the idle CPU runs this code | |
7082 | * with interrupts disabled and will complete the lock | |
7083 | * protected section without being interrupted. So there is no | |
7084 | * real need to boost. | |
7085 | */ | |
7086 | if (unlikely(p == rq->idle)) { | |
7087 | WARN_ON(p != rq->curr); | |
7088 | WARN_ON(p->pi_blocked_on); | |
7089 | goto out_unlock; | |
7090 | } | |
7091 | ||
b91473ff | 7092 | trace_sched_pi_setprio(p, pi_task); |
d5f9f942 | 7093 | oldprio = p->prio; |
ff77e468 PZ |
7094 | |
7095 | if (oldprio == prio) | |
7096 | queue_flag &= ~DEQUEUE_MOVE; | |
7097 | ||
83ab0aa0 | 7098 | prev_class = p->sched_class; |
da0c1e65 | 7099 | queued = task_on_rq_queued(p); |
051a1d1a | 7100 | running = task_current(rq, p); |
da0c1e65 | 7101 | if (queued) |
ff77e468 | 7102 | dequeue_task(rq, p, queue_flag); |
0e1f3483 | 7103 | if (running) |
f3cd1c4e | 7104 | put_prev_task(rq, p); |
dd41f596 | 7105 | |
2d3d891d DF |
7106 | /* |
7107 | * Boosting condition are: | |
7108 | * 1. -rt task is running and holds mutex A | |
7109 | * --> -dl task blocks on mutex A | |
7110 | * | |
7111 | * 2. -dl task is running and holds mutex A | |
7112 | * --> -dl task blocks on mutex A and could preempt the | |
7113 | * running task | |
7114 | */ | |
7115 | if (dl_prio(prio)) { | |
466af29b | 7116 | if (!dl_prio(p->normal_prio) || |
740797ce JL |
7117 | (pi_task && dl_prio(pi_task->prio) && |
7118 | dl_entity_preempt(&pi_task->dl, &p->dl))) { | |
2279f540 | 7119 | p->dl.pi_se = pi_task->dl.pi_se; |
ff77e468 | 7120 | queue_flag |= ENQUEUE_REPLENISH; |
2279f540 JL |
7121 | } else { |
7122 | p->dl.pi_se = &p->dl; | |
7123 | } | |
2d3d891d DF |
7124 | } else if (rt_prio(prio)) { |
7125 | if (dl_prio(oldprio)) | |
2279f540 | 7126 | p->dl.pi_se = &p->dl; |
2d3d891d | 7127 | if (oldprio < prio) |
ff77e468 | 7128 | queue_flag |= ENQUEUE_HEAD; |
2d3d891d DF |
7129 | } else { |
7130 | if (dl_prio(oldprio)) | |
2279f540 | 7131 | p->dl.pi_se = &p->dl; |
746db944 BS |
7132 | if (rt_prio(oldprio)) |
7133 | p->rt.timeout = 0; | |
2d3d891d | 7134 | } |
dd41f596 | 7135 | |
f558c2b8 | 7136 | __setscheduler_prio(p, prio); |
b29739f9 | 7137 | |
da0c1e65 | 7138 | if (queued) |
ff77e468 | 7139 | enqueue_task(rq, p, queue_flag); |
a399d233 | 7140 | if (running) |
03b7fad1 | 7141 | set_next_task(rq, p); |
cb469845 | 7142 | |
da7a735e | 7143 | check_class_changed(rq, p, prev_class, oldprio); |
1c4dd99b | 7144 | out_unlock: |
d1ccc66d IM |
7145 | /* Avoid rq from going away on us: */ |
7146 | preempt_disable(); | |
4c9a4bc8 | 7147 | |
565790d2 PZ |
7148 | rq_unpin_lock(rq, &rf); |
7149 | __balance_callbacks(rq); | |
5cb9eaa3 | 7150 | raw_spin_rq_unlock(rq); |
565790d2 | 7151 | |
4c9a4bc8 | 7152 | preempt_enable(); |
b29739f9 | 7153 | } |
acd58620 PZ |
7154 | #else |
7155 | static inline int rt_effective_prio(struct task_struct *p, int prio) | |
7156 | { | |
7157 | return prio; | |
7158 | } | |
b29739f9 | 7159 | #endif |
d50dde5a | 7160 | |
36c8b586 | 7161 | void set_user_nice(struct task_struct *p, long nice) |
1da177e4 | 7162 | { |
49bd21ef | 7163 | bool queued, running; |
53a23364 | 7164 | int old_prio; |
eb580751 | 7165 | struct rq_flags rf; |
70b97a7f | 7166 | struct rq *rq; |
1da177e4 | 7167 | |
75e45d51 | 7168 | if (task_nice(p) == nice || nice < MIN_NICE || nice > MAX_NICE) |
1da177e4 LT |
7169 | return; |
7170 | /* | |
7171 | * We have to be careful, if called from sys_setpriority(), | |
7172 | * the task might be in the middle of scheduling on another CPU. | |
7173 | */ | |
eb580751 | 7174 | rq = task_rq_lock(p, &rf); |
2fb8d367 PZ |
7175 | update_rq_clock(rq); |
7176 | ||
1da177e4 LT |
7177 | /* |
7178 | * The RT priorities are set via sched_setscheduler(), but we still | |
7179 | * allow the 'normal' nice value to be set - but as expected | |
b19a888c | 7180 | * it won't have any effect on scheduling until the task is |
aab03e05 | 7181 | * SCHED_DEADLINE, SCHED_FIFO or SCHED_RR: |
1da177e4 | 7182 | */ |
aab03e05 | 7183 | if (task_has_dl_policy(p) || task_has_rt_policy(p)) { |
1da177e4 LT |
7184 | p->static_prio = NICE_TO_PRIO(nice); |
7185 | goto out_unlock; | |
7186 | } | |
da0c1e65 | 7187 | queued = task_on_rq_queued(p); |
49bd21ef | 7188 | running = task_current(rq, p); |
da0c1e65 | 7189 | if (queued) |
7a57f32a | 7190 | dequeue_task(rq, p, DEQUEUE_SAVE | DEQUEUE_NOCLOCK); |
49bd21ef PZ |
7191 | if (running) |
7192 | put_prev_task(rq, p); | |
1da177e4 | 7193 | |
1da177e4 | 7194 | p->static_prio = NICE_TO_PRIO(nice); |
b1e82065 | 7195 | set_load_weight(p, true); |
b29739f9 IM |
7196 | old_prio = p->prio; |
7197 | p->prio = effective_prio(p); | |
1da177e4 | 7198 | |
5443a0be | 7199 | if (queued) |
7134b3e9 | 7200 | enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK); |
49bd21ef | 7201 | if (running) |
03b7fad1 | 7202 | set_next_task(rq, p); |
5443a0be FW |
7203 | |
7204 | /* | |
7205 | * If the task increased its priority or is running and | |
7206 | * lowered its priority, then reschedule its CPU: | |
7207 | */ | |
7208 | p->sched_class->prio_changed(rq, p, old_prio); | |
7209 | ||
1da177e4 | 7210 | out_unlock: |
eb580751 | 7211 | task_rq_unlock(rq, p, &rf); |
1da177e4 | 7212 | } |
1da177e4 LT |
7213 | EXPORT_SYMBOL(set_user_nice); |
7214 | ||
e43379f1 | 7215 | /* |
700a7833 CG |
7216 | * is_nice_reduction - check if nice value is an actual reduction |
7217 | * | |
7218 | * Similar to can_nice() but does not perform a capability check. | |
7219 | * | |
e43379f1 MM |
7220 | * @p: task |
7221 | * @nice: nice value | |
7222 | */ | |
700a7833 | 7223 | static bool is_nice_reduction(const struct task_struct *p, const int nice) |
e43379f1 | 7224 | { |
d1ccc66d | 7225 | /* Convert nice value [19,-20] to rlimit style value [1,40]: */ |
7aa2c016 | 7226 | int nice_rlim = nice_to_rlimit(nice); |
48f24c4d | 7227 | |
700a7833 CG |
7228 | return (nice_rlim <= task_rlimit(p, RLIMIT_NICE)); |
7229 | } | |
7230 | ||
7231 | /* | |
7232 | * can_nice - check if a task can reduce its nice value | |
7233 | * @p: task | |
7234 | * @nice: nice value | |
7235 | */ | |
7236 | int can_nice(const struct task_struct *p, const int nice) | |
7237 | { | |
7238 | return is_nice_reduction(p, nice) || capable(CAP_SYS_NICE); | |
e43379f1 MM |
7239 | } |
7240 | ||
1da177e4 LT |
7241 | #ifdef __ARCH_WANT_SYS_NICE |
7242 | ||
7243 | /* | |
7244 | * sys_nice - change the priority of the current process. | |
7245 | * @increment: priority increment | |
7246 | * | |
7247 | * sys_setpriority is a more generic, but much slower function that | |
7248 | * does similar things. | |
7249 | */ | |
5add95d4 | 7250 | SYSCALL_DEFINE1(nice, int, increment) |
1da177e4 | 7251 | { |
48f24c4d | 7252 | long nice, retval; |
1da177e4 LT |
7253 | |
7254 | /* | |
7255 | * Setpriority might change our priority at the same moment. | |
7256 | * We don't have to worry. Conceptually one call occurs first | |
7257 | * and we have a single winner. | |
7258 | */ | |
a9467fa3 | 7259 | increment = clamp(increment, -NICE_WIDTH, NICE_WIDTH); |
d0ea0268 | 7260 | nice = task_nice(current) + increment; |
1da177e4 | 7261 | |
a9467fa3 | 7262 | nice = clamp_val(nice, MIN_NICE, MAX_NICE); |
e43379f1 MM |
7263 | if (increment < 0 && !can_nice(current, nice)) |
7264 | return -EPERM; | |
7265 | ||
1da177e4 LT |
7266 | retval = security_task_setnice(current, nice); |
7267 | if (retval) | |
7268 | return retval; | |
7269 | ||
7270 | set_user_nice(current, nice); | |
7271 | return 0; | |
7272 | } | |
7273 | ||
7274 | #endif | |
7275 | ||
7276 | /** | |
7277 | * task_prio - return the priority value of a given task. | |
7278 | * @p: the task in question. | |
7279 | * | |
e69f6186 | 7280 | * Return: The priority value as seen by users in /proc. |
c541bb78 DE |
7281 | * |
7282 | * sched policy return value kernel prio user prio/nice | |
7283 | * | |
7284 | * normal, batch, idle [0 ... 39] [100 ... 139] 0/[-20 ... 19] | |
7285 | * fifo, rr [-2 ... -100] [98 ... 0] [1 ... 99] | |
7286 | * deadline -101 -1 0 | |
1da177e4 | 7287 | */ |
36c8b586 | 7288 | int task_prio(const struct task_struct *p) |
1da177e4 LT |
7289 | { |
7290 | return p->prio - MAX_RT_PRIO; | |
7291 | } | |
7292 | ||
1da177e4 | 7293 | /** |
d1ccc66d | 7294 | * idle_cpu - is a given CPU idle currently? |
1da177e4 | 7295 | * @cpu: the processor in question. |
e69f6186 YB |
7296 | * |
7297 | * Return: 1 if the CPU is currently idle. 0 otherwise. | |
1da177e4 LT |
7298 | */ |
7299 | int idle_cpu(int cpu) | |
7300 | { | |
908a3283 TG |
7301 | struct rq *rq = cpu_rq(cpu); |
7302 | ||
7303 | if (rq->curr != rq->idle) | |
7304 | return 0; | |
7305 | ||
7306 | if (rq->nr_running) | |
7307 | return 0; | |
7308 | ||
7309 | #ifdef CONFIG_SMP | |
126c2092 | 7310 | if (rq->ttwu_pending) |
908a3283 TG |
7311 | return 0; |
7312 | #endif | |
7313 | ||
7314 | return 1; | |
1da177e4 LT |
7315 | } |
7316 | ||
943d355d RJ |
7317 | /** |
7318 | * available_idle_cpu - is a given CPU idle for enqueuing work. | |
7319 | * @cpu: the CPU in question. | |
7320 | * | |
7321 | * Return: 1 if the CPU is currently idle. 0 otherwise. | |
7322 | */ | |
7323 | int available_idle_cpu(int cpu) | |
7324 | { | |
7325 | if (!idle_cpu(cpu)) | |
7326 | return 0; | |
7327 | ||
247f2f6f RJ |
7328 | if (vcpu_is_preempted(cpu)) |
7329 | return 0; | |
7330 | ||
908a3283 | 7331 | return 1; |
1da177e4 LT |
7332 | } |
7333 | ||
1da177e4 | 7334 | /** |
d1ccc66d | 7335 | * idle_task - return the idle task for a given CPU. |
1da177e4 | 7336 | * @cpu: the processor in question. |
e69f6186 | 7337 | * |
d1ccc66d | 7338 | * Return: The idle task for the CPU @cpu. |
1da177e4 | 7339 | */ |
36c8b586 | 7340 | struct task_struct *idle_task(int cpu) |
1da177e4 LT |
7341 | { |
7342 | return cpu_rq(cpu)->idle; | |
7343 | } | |
7344 | ||
7d6a905f VK |
7345 | #ifdef CONFIG_SMP |
7346 | /* | |
7347 | * This function computes an effective utilization for the given CPU, to be | |
7348 | * used for frequency selection given the linear relation: f = u * f_max. | |
7349 | * | |
7350 | * The scheduler tracks the following metrics: | |
7351 | * | |
7352 | * cpu_util_{cfs,rt,dl,irq}() | |
7353 | * cpu_bw_dl() | |
7354 | * | |
7355 | * Where the cfs,rt and dl util numbers are tracked with the same metric and | |
7356 | * synchronized windows and are thus directly comparable. | |
7357 | * | |
7358 | * The cfs,rt,dl utilization are the running times measured with rq->clock_task | |
7359 | * which excludes things like IRQ and steal-time. These latter are then accrued | |
7360 | * in the irq utilization. | |
7361 | * | |
7362 | * The DL bandwidth number otoh is not a measured metric but a value computed | |
7363 | * based on the task model parameters and gives the minimal utilization | |
7364 | * required to meet deadlines. | |
7365 | */ | |
a5418be9 | 7366 | unsigned long effective_cpu_util(int cpu, unsigned long util_cfs, |
bb447999 | 7367 | enum cpu_util_type type, |
7d6a905f VK |
7368 | struct task_struct *p) |
7369 | { | |
bb447999 | 7370 | unsigned long dl_util, util, irq, max; |
7d6a905f VK |
7371 | struct rq *rq = cpu_rq(cpu); |
7372 | ||
bb447999 DE |
7373 | max = arch_scale_cpu_capacity(cpu); |
7374 | ||
7d6a905f VK |
7375 | if (!uclamp_is_used() && |
7376 | type == FREQUENCY_UTIL && rt_rq_is_runnable(&rq->rt)) { | |
7377 | return max; | |
7378 | } | |
7379 | ||
7380 | /* | |
7381 | * Early check to see if IRQ/steal time saturates the CPU, can be | |
7382 | * because of inaccuracies in how we track these -- see | |
7383 | * update_irq_load_avg(). | |
7384 | */ | |
7385 | irq = cpu_util_irq(rq); | |
7386 | if (unlikely(irq >= max)) | |
7387 | return max; | |
7388 | ||
7389 | /* | |
7390 | * Because the time spend on RT/DL tasks is visible as 'lost' time to | |
7391 | * CFS tasks and we use the same metric to track the effective | |
7392 | * utilization (PELT windows are synchronized) we can directly add them | |
7393 | * to obtain the CPU's actual utilization. | |
7394 | * | |
7395 | * CFS and RT utilization can be boosted or capped, depending on | |
7396 | * utilization clamp constraints requested by currently RUNNABLE | |
7397 | * tasks. | |
7398 | * When there are no CFS RUNNABLE tasks, clamps are released and | |
7399 | * frequency will be gracefully reduced with the utilization decay. | |
7400 | */ | |
7401 | util = util_cfs + cpu_util_rt(rq); | |
7402 | if (type == FREQUENCY_UTIL) | |
7403 | util = uclamp_rq_util_with(rq, util, p); | |
7404 | ||
7405 | dl_util = cpu_util_dl(rq); | |
7406 | ||
7407 | /* | |
7408 | * For frequency selection we do not make cpu_util_dl() a permanent part | |
7409 | * of this sum because we want to use cpu_bw_dl() later on, but we need | |
7410 | * to check if the CFS+RT+DL sum is saturated (ie. no idle time) such | |
7411 | * that we select f_max when there is no idle time. | |
7412 | * | |
7413 | * NOTE: numerical errors or stop class might cause us to not quite hit | |
7414 | * saturation when we should -- something for later. | |
7415 | */ | |
7416 | if (util + dl_util >= max) | |
7417 | return max; | |
7418 | ||
7419 | /* | |
7420 | * OTOH, for energy computation we need the estimated running time, so | |
7421 | * include util_dl and ignore dl_bw. | |
7422 | */ | |
7423 | if (type == ENERGY_UTIL) | |
7424 | util += dl_util; | |
7425 | ||
7426 | /* | |
7427 | * There is still idle time; further improve the number by using the | |
7428 | * irq metric. Because IRQ/steal time is hidden from the task clock we | |
7429 | * need to scale the task numbers: | |
7430 | * | |
7431 | * max - irq | |
7432 | * U' = irq + --------- * U | |
7433 | * max | |
7434 | */ | |
7435 | util = scale_irq_capacity(util, irq, max); | |
7436 | util += irq; | |
7437 | ||
7438 | /* | |
7439 | * Bandwidth required by DEADLINE must always be granted while, for | |
7440 | * FAIR and RT, we use blocked utilization of IDLE CPUs as a mechanism | |
7441 | * to gracefully reduce the frequency when no tasks show up for longer | |
7442 | * periods of time. | |
7443 | * | |
7444 | * Ideally we would like to set bw_dl as min/guaranteed freq and util + | |
7445 | * bw_dl as requested freq. However, cpufreq is not yet ready for such | |
7446 | * an interface. So, we only do the latter for now. | |
7447 | */ | |
7448 | if (type == FREQUENCY_UTIL) | |
7449 | util += cpu_bw_dl(rq); | |
7450 | ||
7451 | return min(max, util); | |
7452 | } | |
a5418be9 | 7453 | |
bb447999 | 7454 | unsigned long sched_cpu_util(int cpu) |
a5418be9 | 7455 | { |
bb447999 | 7456 | return effective_cpu_util(cpu, cpu_util_cfs(cpu), ENERGY_UTIL, NULL); |
a5418be9 | 7457 | } |
7d6a905f VK |
7458 | #endif /* CONFIG_SMP */ |
7459 | ||
1da177e4 LT |
7460 | /** |
7461 | * find_process_by_pid - find a process with a matching PID value. | |
7462 | * @pid: the pid in question. | |
e69f6186 YB |
7463 | * |
7464 | * The task of @pid, if found. %NULL otherwise. | |
1da177e4 | 7465 | */ |
a9957449 | 7466 | static struct task_struct *find_process_by_pid(pid_t pid) |
1da177e4 | 7467 | { |
228ebcbe | 7468 | return pid ? find_task_by_vpid(pid) : current; |
1da177e4 LT |
7469 | } |
7470 | ||
c13db6b1 SR |
7471 | /* |
7472 | * sched_setparam() passes in -1 for its policy, to let the functions | |
7473 | * it calls know not to change it. | |
7474 | */ | |
7475 | #define SETPARAM_POLICY -1 | |
7476 | ||
c365c292 TG |
7477 | static void __setscheduler_params(struct task_struct *p, |
7478 | const struct sched_attr *attr) | |
1da177e4 | 7479 | { |
d50dde5a DF |
7480 | int policy = attr->sched_policy; |
7481 | ||
c13db6b1 | 7482 | if (policy == SETPARAM_POLICY) |
39fd8fd2 PZ |
7483 | policy = p->policy; |
7484 | ||
1da177e4 | 7485 | p->policy = policy; |
d50dde5a | 7486 | |
aab03e05 DF |
7487 | if (dl_policy(policy)) |
7488 | __setparam_dl(p, attr); | |
39fd8fd2 | 7489 | else if (fair_policy(policy)) |
d50dde5a DF |
7490 | p->static_prio = NICE_TO_PRIO(attr->sched_nice); |
7491 | ||
39fd8fd2 PZ |
7492 | /* |
7493 | * __sched_setscheduler() ensures attr->sched_priority == 0 when | |
7494 | * !rt_policy. Always setting this ensures that things like | |
7495 | * getparam()/getattr() don't report silly values for !rt tasks. | |
7496 | */ | |
7497 | p->rt_priority = attr->sched_priority; | |
383afd09 | 7498 | p->normal_prio = normal_prio(p); |
b1e82065 | 7499 | set_load_weight(p, true); |
c365c292 | 7500 | } |
39fd8fd2 | 7501 | |
c69e8d9c | 7502 | /* |
d1ccc66d | 7503 | * Check the target process has a UID that matches the current process's: |
c69e8d9c DH |
7504 | */ |
7505 | static bool check_same_owner(struct task_struct *p) | |
7506 | { | |
7507 | const struct cred *cred = current_cred(), *pcred; | |
7508 | bool match; | |
7509 | ||
7510 | rcu_read_lock(); | |
7511 | pcred = __task_cred(p); | |
9c806aa0 EB |
7512 | match = (uid_eq(cred->euid, pcred->euid) || |
7513 | uid_eq(cred->euid, pcred->uid)); | |
c69e8d9c DH |
7514 | rcu_read_unlock(); |
7515 | return match; | |
7516 | } | |
7517 | ||
700a7833 CG |
7518 | /* |
7519 | * Allow unprivileged RT tasks to decrease priority. | |
7520 | * Only issue a capable test if needed and only once to avoid an audit | |
7521 | * event on permitted non-privileged operations: | |
7522 | */ | |
7523 | static int user_check_sched_setscheduler(struct task_struct *p, | |
7524 | const struct sched_attr *attr, | |
7525 | int policy, int reset_on_fork) | |
7526 | { | |
7527 | if (fair_policy(policy)) { | |
7528 | if (attr->sched_nice < task_nice(p) && | |
7529 | !is_nice_reduction(p, attr->sched_nice)) | |
7530 | goto req_priv; | |
7531 | } | |
7532 | ||
7533 | if (rt_policy(policy)) { | |
7534 | unsigned long rlim_rtprio = task_rlimit(p, RLIMIT_RTPRIO); | |
7535 | ||
7536 | /* Can't set/change the rt policy: */ | |
7537 | if (policy != p->policy && !rlim_rtprio) | |
7538 | goto req_priv; | |
7539 | ||
7540 | /* Can't increase priority: */ | |
7541 | if (attr->sched_priority > p->rt_priority && | |
7542 | attr->sched_priority > rlim_rtprio) | |
7543 | goto req_priv; | |
7544 | } | |
7545 | ||
7546 | /* | |
7547 | * Can't set/change SCHED_DEADLINE policy at all for now | |
7548 | * (safest behavior); in the future we would like to allow | |
7549 | * unprivileged DL tasks to increase their relative deadline | |
7550 | * or reduce their runtime (both ways reducing utilization) | |
7551 | */ | |
7552 | if (dl_policy(policy)) | |
7553 | goto req_priv; | |
7554 | ||
7555 | /* | |
7556 | * Treat SCHED_IDLE as nice 20. Only allow a switch to | |
7557 | * SCHED_NORMAL if the RLIMIT_NICE would normally permit it. | |
7558 | */ | |
7559 | if (task_has_idle_policy(p) && !idle_policy(policy)) { | |
7560 | if (!is_nice_reduction(p, task_nice(p))) | |
7561 | goto req_priv; | |
7562 | } | |
7563 | ||
7564 | /* Can't change other user's priorities: */ | |
7565 | if (!check_same_owner(p)) | |
7566 | goto req_priv; | |
7567 | ||
7568 | /* Normal users shall not reset the sched_reset_on_fork flag: */ | |
7569 | if (p->sched_reset_on_fork && !reset_on_fork) | |
7570 | goto req_priv; | |
7571 | ||
7572 | return 0; | |
7573 | ||
7574 | req_priv: | |
7575 | if (!capable(CAP_SYS_NICE)) | |
7576 | return -EPERM; | |
7577 | ||
7578 | return 0; | |
7579 | } | |
7580 | ||
d50dde5a DF |
7581 | static int __sched_setscheduler(struct task_struct *p, |
7582 | const struct sched_attr *attr, | |
dbc7f069 | 7583 | bool user, bool pi) |
1da177e4 | 7584 | { |
f558c2b8 PZ |
7585 | int oldpolicy = -1, policy = attr->sched_policy; |
7586 | int retval, oldprio, newprio, queued, running; | |
83ab0aa0 | 7587 | const struct sched_class *prev_class; |
8e5bad7d | 7588 | struct balance_callback *head; |
eb580751 | 7589 | struct rq_flags rf; |
ca94c442 | 7590 | int reset_on_fork; |
7a57f32a | 7591 | int queue_flags = DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK; |
eb580751 | 7592 | struct rq *rq; |
111cd11b | 7593 | bool cpuset_locked = false; |
1da177e4 | 7594 | |
896bbb25 SRV |
7595 | /* The pi code expects interrupts enabled */ |
7596 | BUG_ON(pi && in_interrupt()); | |
1da177e4 | 7597 | recheck: |
d1ccc66d | 7598 | /* Double check policy once rq lock held: */ |
ca94c442 LP |
7599 | if (policy < 0) { |
7600 | reset_on_fork = p->sched_reset_on_fork; | |
1da177e4 | 7601 | policy = oldpolicy = p->policy; |
ca94c442 | 7602 | } else { |
7479f3c9 | 7603 | reset_on_fork = !!(attr->sched_flags & SCHED_FLAG_RESET_ON_FORK); |
ca94c442 | 7604 | |
20f9cd2a | 7605 | if (!valid_policy(policy)) |
ca94c442 LP |
7606 | return -EINVAL; |
7607 | } | |
7608 | ||
794a56eb | 7609 | if (attr->sched_flags & ~(SCHED_FLAG_ALL | SCHED_FLAG_SUGOV)) |
7479f3c9 PZ |
7610 | return -EINVAL; |
7611 | ||
1da177e4 LT |
7612 | /* |
7613 | * Valid priorities for SCHED_FIFO and SCHED_RR are | |
ae18ad28 | 7614 | * 1..MAX_RT_PRIO-1, valid priority for SCHED_NORMAL, |
dd41f596 | 7615 | * SCHED_BATCH and SCHED_IDLE is 0. |
1da177e4 | 7616 | */ |
ae18ad28 | 7617 | if (attr->sched_priority > MAX_RT_PRIO-1) |
1da177e4 | 7618 | return -EINVAL; |
aab03e05 DF |
7619 | if ((dl_policy(policy) && !__checkparam_dl(attr)) || |
7620 | (rt_policy(policy) != (attr->sched_priority != 0))) | |
1da177e4 LT |
7621 | return -EINVAL; |
7622 | ||
725aad24 | 7623 | if (user) { |
700a7833 CG |
7624 | retval = user_check_sched_setscheduler(p, attr, policy, reset_on_fork); |
7625 | if (retval) | |
7626 | return retval; | |
7627 | ||
794a56eb JL |
7628 | if (attr->sched_flags & SCHED_FLAG_SUGOV) |
7629 | return -EINVAL; | |
7630 | ||
b0ae1981 | 7631 | retval = security_task_setscheduler(p); |
725aad24 JF |
7632 | if (retval) |
7633 | return retval; | |
7634 | } | |
7635 | ||
a509a7cd PB |
7636 | /* Update task specific "requested" clamps */ |
7637 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) { | |
7638 | retval = uclamp_validate(p, attr); | |
7639 | if (retval) | |
7640 | return retval; | |
7641 | } | |
7642 | ||
111cd11b JL |
7643 | /* |
7644 | * SCHED_DEADLINE bandwidth accounting relies on stable cpusets | |
7645 | * information. | |
7646 | */ | |
7647 | if (dl_policy(policy) || dl_policy(p->policy)) { | |
7648 | cpuset_locked = true; | |
7649 | cpuset_lock(); | |
7650 | } | |
710da3c8 | 7651 | |
b29739f9 | 7652 | /* |
d1ccc66d | 7653 | * Make sure no PI-waiters arrive (or leave) while we are |
b29739f9 | 7654 | * changing the priority of the task: |
0122ec5b | 7655 | * |
25985edc | 7656 | * To be able to change p->policy safely, the appropriate |
1da177e4 LT |
7657 | * runqueue lock must be held. |
7658 | */ | |
eb580751 | 7659 | rq = task_rq_lock(p, &rf); |
80f5c1b8 | 7660 | update_rq_clock(rq); |
dc61b1d6 | 7661 | |
34f971f6 | 7662 | /* |
d1ccc66d | 7663 | * Changing the policy of the stop threads its a very bad idea: |
34f971f6 PZ |
7664 | */ |
7665 | if (p == rq->stop) { | |
4b211f2b MP |
7666 | retval = -EINVAL; |
7667 | goto unlock; | |
34f971f6 PZ |
7668 | } |
7669 | ||
a51e9198 | 7670 | /* |
d6b1e911 TG |
7671 | * If not changing anything there's no need to proceed further, |
7672 | * but store a possible modification of reset_on_fork. | |
a51e9198 | 7673 | */ |
d50dde5a | 7674 | if (unlikely(policy == p->policy)) { |
d0ea0268 | 7675 | if (fair_policy(policy) && attr->sched_nice != task_nice(p)) |
d50dde5a DF |
7676 | goto change; |
7677 | if (rt_policy(policy) && attr->sched_priority != p->rt_priority) | |
7678 | goto change; | |
75381608 | 7679 | if (dl_policy(policy) && dl_param_changed(p, attr)) |
aab03e05 | 7680 | goto change; |
a509a7cd PB |
7681 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) |
7682 | goto change; | |
d50dde5a | 7683 | |
d6b1e911 | 7684 | p->sched_reset_on_fork = reset_on_fork; |
4b211f2b MP |
7685 | retval = 0; |
7686 | goto unlock; | |
a51e9198 | 7687 | } |
d50dde5a | 7688 | change: |
a51e9198 | 7689 | |
dc61b1d6 | 7690 | if (user) { |
332ac17e | 7691 | #ifdef CONFIG_RT_GROUP_SCHED |
dc61b1d6 PZ |
7692 | /* |
7693 | * Do not allow realtime tasks into groups that have no runtime | |
7694 | * assigned. | |
7695 | */ | |
7696 | if (rt_bandwidth_enabled() && rt_policy(policy) && | |
f4493771 MG |
7697 | task_group(p)->rt_bandwidth.rt_runtime == 0 && |
7698 | !task_group_is_autogroup(task_group(p))) { | |
4b211f2b MP |
7699 | retval = -EPERM; |
7700 | goto unlock; | |
dc61b1d6 | 7701 | } |
dc61b1d6 | 7702 | #endif |
332ac17e | 7703 | #ifdef CONFIG_SMP |
794a56eb JL |
7704 | if (dl_bandwidth_enabled() && dl_policy(policy) && |
7705 | !(attr->sched_flags & SCHED_FLAG_SUGOV)) { | |
332ac17e | 7706 | cpumask_t *span = rq->rd->span; |
332ac17e DF |
7707 | |
7708 | /* | |
7709 | * Don't allow tasks with an affinity mask smaller than | |
7710 | * the entire root_domain to become SCHED_DEADLINE. We | |
7711 | * will also fail if there's no bandwidth available. | |
7712 | */ | |
3bd37062 | 7713 | if (!cpumask_subset(span, p->cpus_ptr) || |
e4099a5e | 7714 | rq->rd->dl_bw.bw == 0) { |
4b211f2b MP |
7715 | retval = -EPERM; |
7716 | goto unlock; | |
332ac17e DF |
7717 | } |
7718 | } | |
7719 | #endif | |
7720 | } | |
dc61b1d6 | 7721 | |
d1ccc66d | 7722 | /* Re-check policy now with rq lock held: */ |
1da177e4 LT |
7723 | if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { |
7724 | policy = oldpolicy = -1; | |
eb580751 | 7725 | task_rq_unlock(rq, p, &rf); |
111cd11b JL |
7726 | if (cpuset_locked) |
7727 | cpuset_unlock(); | |
1da177e4 LT |
7728 | goto recheck; |
7729 | } | |
332ac17e DF |
7730 | |
7731 | /* | |
7732 | * If setscheduling to SCHED_DEADLINE (or changing the parameters | |
7733 | * of a SCHED_DEADLINE task) we need to check if enough bandwidth | |
7734 | * is available. | |
7735 | */ | |
06a76fe0 | 7736 | if ((dl_policy(policy) || dl_task(p)) && sched_dl_overflow(p, policy, attr)) { |
4b211f2b MP |
7737 | retval = -EBUSY; |
7738 | goto unlock; | |
332ac17e DF |
7739 | } |
7740 | ||
c365c292 TG |
7741 | p->sched_reset_on_fork = reset_on_fork; |
7742 | oldprio = p->prio; | |
7743 | ||
f558c2b8 | 7744 | newprio = __normal_prio(policy, attr->sched_priority, attr->sched_nice); |
dbc7f069 PZ |
7745 | if (pi) { |
7746 | /* | |
7747 | * Take priority boosted tasks into account. If the new | |
7748 | * effective priority is unchanged, we just store the new | |
7749 | * normal parameters and do not touch the scheduler class and | |
7750 | * the runqueue. This will be done when the task deboost | |
7751 | * itself. | |
7752 | */ | |
f558c2b8 PZ |
7753 | newprio = rt_effective_prio(p, newprio); |
7754 | if (newprio == oldprio) | |
ff77e468 | 7755 | queue_flags &= ~DEQUEUE_MOVE; |
c365c292 TG |
7756 | } |
7757 | ||
da0c1e65 | 7758 | queued = task_on_rq_queued(p); |
051a1d1a | 7759 | running = task_current(rq, p); |
da0c1e65 | 7760 | if (queued) |
ff77e468 | 7761 | dequeue_task(rq, p, queue_flags); |
0e1f3483 | 7762 | if (running) |
f3cd1c4e | 7763 | put_prev_task(rq, p); |
f6b53205 | 7764 | |
83ab0aa0 | 7765 | prev_class = p->sched_class; |
a509a7cd | 7766 | |
f558c2b8 PZ |
7767 | if (!(attr->sched_flags & SCHED_FLAG_KEEP_PARAMS)) { |
7768 | __setscheduler_params(p, attr); | |
7769 | __setscheduler_prio(p, newprio); | |
7770 | } | |
a509a7cd | 7771 | __setscheduler_uclamp(p, attr); |
f6b53205 | 7772 | |
da0c1e65 | 7773 | if (queued) { |
81a44c54 TG |
7774 | /* |
7775 | * We enqueue to tail when the priority of a task is | |
7776 | * increased (user space view). | |
7777 | */ | |
ff77e468 PZ |
7778 | if (oldprio < p->prio) |
7779 | queue_flags |= ENQUEUE_HEAD; | |
1de64443 | 7780 | |
ff77e468 | 7781 | enqueue_task(rq, p, queue_flags); |
81a44c54 | 7782 | } |
a399d233 | 7783 | if (running) |
03b7fad1 | 7784 | set_next_task(rq, p); |
cb469845 | 7785 | |
da7a735e | 7786 | check_class_changed(rq, p, prev_class, oldprio); |
d1ccc66d IM |
7787 | |
7788 | /* Avoid rq from going away on us: */ | |
7789 | preempt_disable(); | |
565790d2 | 7790 | head = splice_balance_callbacks(rq); |
eb580751 | 7791 | task_rq_unlock(rq, p, &rf); |
b29739f9 | 7792 | |
710da3c8 | 7793 | if (pi) { |
111cd11b JL |
7794 | if (cpuset_locked) |
7795 | cpuset_unlock(); | |
dbc7f069 | 7796 | rt_mutex_adjust_pi(p); |
710da3c8 | 7797 | } |
95e02ca9 | 7798 | |
d1ccc66d | 7799 | /* Run balance callbacks after we've adjusted the PI chain: */ |
565790d2 | 7800 | balance_callbacks(rq, head); |
4c9a4bc8 | 7801 | preempt_enable(); |
95e02ca9 | 7802 | |
1da177e4 | 7803 | return 0; |
4b211f2b MP |
7804 | |
7805 | unlock: | |
7806 | task_rq_unlock(rq, p, &rf); | |
111cd11b JL |
7807 | if (cpuset_locked) |
7808 | cpuset_unlock(); | |
4b211f2b | 7809 | return retval; |
1da177e4 | 7810 | } |
961ccddd | 7811 | |
7479f3c9 PZ |
7812 | static int _sched_setscheduler(struct task_struct *p, int policy, |
7813 | const struct sched_param *param, bool check) | |
7814 | { | |
7815 | struct sched_attr attr = { | |
7816 | .sched_policy = policy, | |
7817 | .sched_priority = param->sched_priority, | |
7818 | .sched_nice = PRIO_TO_NICE(p->static_prio), | |
7819 | }; | |
7820 | ||
c13db6b1 SR |
7821 | /* Fixup the legacy SCHED_RESET_ON_FORK hack. */ |
7822 | if ((policy != SETPARAM_POLICY) && (policy & SCHED_RESET_ON_FORK)) { | |
7479f3c9 PZ |
7823 | attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK; |
7824 | policy &= ~SCHED_RESET_ON_FORK; | |
7825 | attr.sched_policy = policy; | |
7826 | } | |
7827 | ||
dbc7f069 | 7828 | return __sched_setscheduler(p, &attr, check, true); |
7479f3c9 | 7829 | } |
961ccddd RR |
7830 | /** |
7831 | * sched_setscheduler - change the scheduling policy and/or RT priority of a thread. | |
7832 | * @p: the task in question. | |
7833 | * @policy: new policy. | |
7834 | * @param: structure containing the new RT priority. | |
7835 | * | |
7318d4cc PZ |
7836 | * Use sched_set_fifo(), read its comment. |
7837 | * | |
e69f6186 YB |
7838 | * Return: 0 on success. An error code otherwise. |
7839 | * | |
961ccddd RR |
7840 | * NOTE that the task may be already dead. |
7841 | */ | |
7842 | int sched_setscheduler(struct task_struct *p, int policy, | |
fe7de49f | 7843 | const struct sched_param *param) |
961ccddd | 7844 | { |
7479f3c9 | 7845 | return _sched_setscheduler(p, policy, param, true); |
961ccddd | 7846 | } |
1da177e4 | 7847 | |
d50dde5a DF |
7848 | int sched_setattr(struct task_struct *p, const struct sched_attr *attr) |
7849 | { | |
dbc7f069 | 7850 | return __sched_setscheduler(p, attr, true, true); |
d50dde5a | 7851 | } |
d50dde5a | 7852 | |
794a56eb JL |
7853 | int sched_setattr_nocheck(struct task_struct *p, const struct sched_attr *attr) |
7854 | { | |
7855 | return __sched_setscheduler(p, attr, false, true); | |
7856 | } | |
1eb5dde6 | 7857 | EXPORT_SYMBOL_GPL(sched_setattr_nocheck); |
794a56eb | 7858 | |
961ccddd RR |
7859 | /** |
7860 | * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace. | |
7861 | * @p: the task in question. | |
7862 | * @policy: new policy. | |
7863 | * @param: structure containing the new RT priority. | |
7864 | * | |
7865 | * Just like sched_setscheduler, only don't bother checking if the | |
7866 | * current context has permission. For example, this is needed in | |
7867 | * stop_machine(): we create temporary high priority worker threads, | |
7868 | * but our caller might not have that capability. | |
e69f6186 YB |
7869 | * |
7870 | * Return: 0 on success. An error code otherwise. | |
961ccddd RR |
7871 | */ |
7872 | int sched_setscheduler_nocheck(struct task_struct *p, int policy, | |
fe7de49f | 7873 | const struct sched_param *param) |
961ccddd | 7874 | { |
7479f3c9 | 7875 | return _sched_setscheduler(p, policy, param, false); |
961ccddd RR |
7876 | } |
7877 | ||
7318d4cc PZ |
7878 | /* |
7879 | * SCHED_FIFO is a broken scheduler model; that is, it is fundamentally | |
7880 | * incapable of resource management, which is the one thing an OS really should | |
7881 | * be doing. | |
7882 | * | |
7883 | * This is of course the reason it is limited to privileged users only. | |
7884 | * | |
7885 | * Worse still; it is fundamentally impossible to compose static priority | |
7886 | * workloads. You cannot take two correctly working static prio workloads | |
7887 | * and smash them together and still expect them to work. | |
7888 | * | |
7889 | * For this reason 'all' FIFO tasks the kernel creates are basically at: | |
7890 | * | |
7891 | * MAX_RT_PRIO / 2 | |
7892 | * | |
7893 | * The administrator _MUST_ configure the system, the kernel simply doesn't | |
7894 | * know enough information to make a sensible choice. | |
7895 | */ | |
8b700983 | 7896 | void sched_set_fifo(struct task_struct *p) |
7318d4cc PZ |
7897 | { |
7898 | struct sched_param sp = { .sched_priority = MAX_RT_PRIO / 2 }; | |
8b700983 | 7899 | WARN_ON_ONCE(sched_setscheduler_nocheck(p, SCHED_FIFO, &sp) != 0); |
7318d4cc PZ |
7900 | } |
7901 | EXPORT_SYMBOL_GPL(sched_set_fifo); | |
7902 | ||
7903 | /* | |
7904 | * For when you don't much care about FIFO, but want to be above SCHED_NORMAL. | |
7905 | */ | |
8b700983 | 7906 | void sched_set_fifo_low(struct task_struct *p) |
7318d4cc PZ |
7907 | { |
7908 | struct sched_param sp = { .sched_priority = 1 }; | |
8b700983 | 7909 | WARN_ON_ONCE(sched_setscheduler_nocheck(p, SCHED_FIFO, &sp) != 0); |
7318d4cc PZ |
7910 | } |
7911 | EXPORT_SYMBOL_GPL(sched_set_fifo_low); | |
7912 | ||
8b700983 | 7913 | void sched_set_normal(struct task_struct *p, int nice) |
7318d4cc PZ |
7914 | { |
7915 | struct sched_attr attr = { | |
7916 | .sched_policy = SCHED_NORMAL, | |
7917 | .sched_nice = nice, | |
7918 | }; | |
8b700983 | 7919 | WARN_ON_ONCE(sched_setattr_nocheck(p, &attr) != 0); |
7318d4cc PZ |
7920 | } |
7921 | EXPORT_SYMBOL_GPL(sched_set_normal); | |
961ccddd | 7922 | |
95cdf3b7 IM |
7923 | static int |
7924 | do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param) | |
1da177e4 | 7925 | { |
1da177e4 LT |
7926 | struct sched_param lparam; |
7927 | struct task_struct *p; | |
36c8b586 | 7928 | int retval; |
1da177e4 LT |
7929 | |
7930 | if (!param || pid < 0) | |
7931 | return -EINVAL; | |
7932 | if (copy_from_user(&lparam, param, sizeof(struct sched_param))) | |
7933 | return -EFAULT; | |
5fe1d75f ON |
7934 | |
7935 | rcu_read_lock(); | |
7936 | retval = -ESRCH; | |
1da177e4 | 7937 | p = find_process_by_pid(pid); |
710da3c8 JL |
7938 | if (likely(p)) |
7939 | get_task_struct(p); | |
5fe1d75f | 7940 | rcu_read_unlock(); |
36c8b586 | 7941 | |
710da3c8 JL |
7942 | if (likely(p)) { |
7943 | retval = sched_setscheduler(p, policy, &lparam); | |
7944 | put_task_struct(p); | |
7945 | } | |
7946 | ||
1da177e4 LT |
7947 | return retval; |
7948 | } | |
7949 | ||
d50dde5a DF |
7950 | /* |
7951 | * Mimics kernel/events/core.c perf_copy_attr(). | |
7952 | */ | |
d1ccc66d | 7953 | static int sched_copy_attr(struct sched_attr __user *uattr, struct sched_attr *attr) |
d50dde5a DF |
7954 | { |
7955 | u32 size; | |
7956 | int ret; | |
7957 | ||
d1ccc66d | 7958 | /* Zero the full structure, so that a short copy will be nice: */ |
d50dde5a DF |
7959 | memset(attr, 0, sizeof(*attr)); |
7960 | ||
7961 | ret = get_user(size, &uattr->size); | |
7962 | if (ret) | |
7963 | return ret; | |
7964 | ||
d1ccc66d IM |
7965 | /* ABI compatibility quirk: */ |
7966 | if (!size) | |
d50dde5a | 7967 | size = SCHED_ATTR_SIZE_VER0; |
dff3a85f | 7968 | if (size < SCHED_ATTR_SIZE_VER0 || size > PAGE_SIZE) |
d50dde5a DF |
7969 | goto err_size; |
7970 | ||
dff3a85f AS |
7971 | ret = copy_struct_from_user(attr, sizeof(*attr), uattr, size); |
7972 | if (ret) { | |
7973 | if (ret == -E2BIG) | |
7974 | goto err_size; | |
7975 | return ret; | |
d50dde5a DF |
7976 | } |
7977 | ||
a509a7cd PB |
7978 | if ((attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) && |
7979 | size < SCHED_ATTR_SIZE_VER1) | |
7980 | return -EINVAL; | |
7981 | ||
d50dde5a | 7982 | /* |
d1ccc66d | 7983 | * XXX: Do we want to be lenient like existing syscalls; or do we want |
d50dde5a DF |
7984 | * to be strict and return an error on out-of-bounds values? |
7985 | */ | |
75e45d51 | 7986 | attr->sched_nice = clamp(attr->sched_nice, MIN_NICE, MAX_NICE); |
d50dde5a | 7987 | |
e78c7bca | 7988 | return 0; |
d50dde5a DF |
7989 | |
7990 | err_size: | |
7991 | put_user(sizeof(*attr), &uattr->size); | |
e78c7bca | 7992 | return -E2BIG; |
d50dde5a DF |
7993 | } |
7994 | ||
f4dddf90 QP |
7995 | static void get_params(struct task_struct *p, struct sched_attr *attr) |
7996 | { | |
7997 | if (task_has_dl_policy(p)) | |
7998 | __getparam_dl(p, attr); | |
7999 | else if (task_has_rt_policy(p)) | |
8000 | attr->sched_priority = p->rt_priority; | |
8001 | else | |
8002 | attr->sched_nice = task_nice(p); | |
8003 | } | |
8004 | ||
1da177e4 LT |
8005 | /** |
8006 | * sys_sched_setscheduler - set/change the scheduler policy and RT priority | |
8007 | * @pid: the pid in question. | |
8008 | * @policy: new policy. | |
8009 | * @param: structure containing the new RT priority. | |
e69f6186 YB |
8010 | * |
8011 | * Return: 0 on success. An error code otherwise. | |
1da177e4 | 8012 | */ |
d1ccc66d | 8013 | SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy, struct sched_param __user *, param) |
1da177e4 | 8014 | { |
c21761f1 JB |
8015 | if (policy < 0) |
8016 | return -EINVAL; | |
8017 | ||
1da177e4 LT |
8018 | return do_sched_setscheduler(pid, policy, param); |
8019 | } | |
8020 | ||
8021 | /** | |
8022 | * sys_sched_setparam - set/change the RT priority of a thread | |
8023 | * @pid: the pid in question. | |
8024 | * @param: structure containing the new RT priority. | |
e69f6186 YB |
8025 | * |
8026 | * Return: 0 on success. An error code otherwise. | |
1da177e4 | 8027 | */ |
5add95d4 | 8028 | SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param) |
1da177e4 | 8029 | { |
c13db6b1 | 8030 | return do_sched_setscheduler(pid, SETPARAM_POLICY, param); |
1da177e4 LT |
8031 | } |
8032 | ||
d50dde5a DF |
8033 | /** |
8034 | * sys_sched_setattr - same as above, but with extended sched_attr | |
8035 | * @pid: the pid in question. | |
5778fccf | 8036 | * @uattr: structure containing the extended parameters. |
db66d756 | 8037 | * @flags: for future extension. |
d50dde5a | 8038 | */ |
6d35ab48 PZ |
8039 | SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr, |
8040 | unsigned int, flags) | |
d50dde5a DF |
8041 | { |
8042 | struct sched_attr attr; | |
8043 | struct task_struct *p; | |
8044 | int retval; | |
8045 | ||
6d35ab48 | 8046 | if (!uattr || pid < 0 || flags) |
d50dde5a DF |
8047 | return -EINVAL; |
8048 | ||
143cf23d MK |
8049 | retval = sched_copy_attr(uattr, &attr); |
8050 | if (retval) | |
8051 | return retval; | |
d50dde5a | 8052 | |
b14ed2c2 | 8053 | if ((int)attr.sched_policy < 0) |
dbdb2275 | 8054 | return -EINVAL; |
1d6362fa PB |
8055 | if (attr.sched_flags & SCHED_FLAG_KEEP_POLICY) |
8056 | attr.sched_policy = SETPARAM_POLICY; | |
d50dde5a DF |
8057 | |
8058 | rcu_read_lock(); | |
8059 | retval = -ESRCH; | |
8060 | p = find_process_by_pid(pid); | |
a509a7cd PB |
8061 | if (likely(p)) |
8062 | get_task_struct(p); | |
d50dde5a DF |
8063 | rcu_read_unlock(); |
8064 | ||
a509a7cd | 8065 | if (likely(p)) { |
f4dddf90 QP |
8066 | if (attr.sched_flags & SCHED_FLAG_KEEP_PARAMS) |
8067 | get_params(p, &attr); | |
a509a7cd PB |
8068 | retval = sched_setattr(p, &attr); |
8069 | put_task_struct(p); | |
8070 | } | |
8071 | ||
d50dde5a DF |
8072 | return retval; |
8073 | } | |
8074 | ||
1da177e4 LT |
8075 | /** |
8076 | * sys_sched_getscheduler - get the policy (scheduling class) of a thread | |
8077 | * @pid: the pid in question. | |
e69f6186 YB |
8078 | * |
8079 | * Return: On success, the policy of the thread. Otherwise, a negative error | |
8080 | * code. | |
1da177e4 | 8081 | */ |
5add95d4 | 8082 | SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid) |
1da177e4 | 8083 | { |
36c8b586 | 8084 | struct task_struct *p; |
3a5c359a | 8085 | int retval; |
1da177e4 LT |
8086 | |
8087 | if (pid < 0) | |
3a5c359a | 8088 | return -EINVAL; |
1da177e4 LT |
8089 | |
8090 | retval = -ESRCH; | |
5fe85be0 | 8091 | rcu_read_lock(); |
1da177e4 LT |
8092 | p = find_process_by_pid(pid); |
8093 | if (p) { | |
8094 | retval = security_task_getscheduler(p); | |
8095 | if (!retval) | |
ca94c442 LP |
8096 | retval = p->policy |
8097 | | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0); | |
1da177e4 | 8098 | } |
5fe85be0 | 8099 | rcu_read_unlock(); |
1da177e4 LT |
8100 | return retval; |
8101 | } | |
8102 | ||
8103 | /** | |
ca94c442 | 8104 | * sys_sched_getparam - get the RT priority of a thread |
1da177e4 LT |
8105 | * @pid: the pid in question. |
8106 | * @param: structure containing the RT priority. | |
e69f6186 YB |
8107 | * |
8108 | * Return: On success, 0 and the RT priority is in @param. Otherwise, an error | |
8109 | * code. | |
1da177e4 | 8110 | */ |
5add95d4 | 8111 | SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param) |
1da177e4 | 8112 | { |
ce5f7f82 | 8113 | struct sched_param lp = { .sched_priority = 0 }; |
36c8b586 | 8114 | struct task_struct *p; |
3a5c359a | 8115 | int retval; |
1da177e4 LT |
8116 | |
8117 | if (!param || pid < 0) | |
3a5c359a | 8118 | return -EINVAL; |
1da177e4 | 8119 | |
5fe85be0 | 8120 | rcu_read_lock(); |
1da177e4 LT |
8121 | p = find_process_by_pid(pid); |
8122 | retval = -ESRCH; | |
8123 | if (!p) | |
8124 | goto out_unlock; | |
8125 | ||
8126 | retval = security_task_getscheduler(p); | |
8127 | if (retval) | |
8128 | goto out_unlock; | |
8129 | ||
ce5f7f82 PZ |
8130 | if (task_has_rt_policy(p)) |
8131 | lp.sched_priority = p->rt_priority; | |
5fe85be0 | 8132 | rcu_read_unlock(); |
1da177e4 LT |
8133 | |
8134 | /* | |
8135 | * This one might sleep, we cannot do it with a spinlock held ... | |
8136 | */ | |
8137 | retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0; | |
8138 | ||
1da177e4 LT |
8139 | return retval; |
8140 | ||
8141 | out_unlock: | |
5fe85be0 | 8142 | rcu_read_unlock(); |
1da177e4 LT |
8143 | return retval; |
8144 | } | |
8145 | ||
1251201c IM |
8146 | /* |
8147 | * Copy the kernel size attribute structure (which might be larger | |
8148 | * than what user-space knows about) to user-space. | |
8149 | * | |
8150 | * Note that all cases are valid: user-space buffer can be larger or | |
8151 | * smaller than the kernel-space buffer. The usual case is that both | |
8152 | * have the same size. | |
8153 | */ | |
8154 | static int | |
8155 | sched_attr_copy_to_user(struct sched_attr __user *uattr, | |
8156 | struct sched_attr *kattr, | |
8157 | unsigned int usize) | |
d50dde5a | 8158 | { |
1251201c | 8159 | unsigned int ksize = sizeof(*kattr); |
d50dde5a | 8160 | |
96d4f267 | 8161 | if (!access_ok(uattr, usize)) |
d50dde5a DF |
8162 | return -EFAULT; |
8163 | ||
8164 | /* | |
1251201c IM |
8165 | * sched_getattr() ABI forwards and backwards compatibility: |
8166 | * | |
8167 | * If usize == ksize then we just copy everything to user-space and all is good. | |
8168 | * | |
8169 | * If usize < ksize then we only copy as much as user-space has space for, | |
8170 | * this keeps ABI compatibility as well. We skip the rest. | |
8171 | * | |
8172 | * If usize > ksize then user-space is using a newer version of the ABI, | |
8173 | * which part the kernel doesn't know about. Just ignore it - tooling can | |
8174 | * detect the kernel's knowledge of attributes from the attr->size value | |
8175 | * which is set to ksize in this case. | |
d50dde5a | 8176 | */ |
1251201c | 8177 | kattr->size = min(usize, ksize); |
d50dde5a | 8178 | |
1251201c | 8179 | if (copy_to_user(uattr, kattr, kattr->size)) |
d50dde5a DF |
8180 | return -EFAULT; |
8181 | ||
22400674 | 8182 | return 0; |
d50dde5a DF |
8183 | } |
8184 | ||
8185 | /** | |
aab03e05 | 8186 | * sys_sched_getattr - similar to sched_getparam, but with sched_attr |
d50dde5a | 8187 | * @pid: the pid in question. |
5778fccf | 8188 | * @uattr: structure containing the extended parameters. |
dff3a85f | 8189 | * @usize: sizeof(attr) for fwd/bwd comp. |
db66d756 | 8190 | * @flags: for future extension. |
d50dde5a | 8191 | */ |
6d35ab48 | 8192 | SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr, |
1251201c | 8193 | unsigned int, usize, unsigned int, flags) |
d50dde5a | 8194 | { |
1251201c | 8195 | struct sched_attr kattr = { }; |
d50dde5a DF |
8196 | struct task_struct *p; |
8197 | int retval; | |
8198 | ||
1251201c IM |
8199 | if (!uattr || pid < 0 || usize > PAGE_SIZE || |
8200 | usize < SCHED_ATTR_SIZE_VER0 || flags) | |
d50dde5a DF |
8201 | return -EINVAL; |
8202 | ||
8203 | rcu_read_lock(); | |
8204 | p = find_process_by_pid(pid); | |
8205 | retval = -ESRCH; | |
8206 | if (!p) | |
8207 | goto out_unlock; | |
8208 | ||
8209 | retval = security_task_getscheduler(p); | |
8210 | if (retval) | |
8211 | goto out_unlock; | |
8212 | ||
1251201c | 8213 | kattr.sched_policy = p->policy; |
7479f3c9 | 8214 | if (p->sched_reset_on_fork) |
1251201c | 8215 | kattr.sched_flags |= SCHED_FLAG_RESET_ON_FORK; |
f4dddf90 | 8216 | get_params(p, &kattr); |
7ad721bf | 8217 | kattr.sched_flags &= SCHED_FLAG_ALL; |
d50dde5a | 8218 | |
a509a7cd | 8219 | #ifdef CONFIG_UCLAMP_TASK |
13685c4a QY |
8220 | /* |
8221 | * This could race with another potential updater, but this is fine | |
8222 | * because it'll correctly read the old or the new value. We don't need | |
8223 | * to guarantee who wins the race as long as it doesn't return garbage. | |
8224 | */ | |
1251201c IM |
8225 | kattr.sched_util_min = p->uclamp_req[UCLAMP_MIN].value; |
8226 | kattr.sched_util_max = p->uclamp_req[UCLAMP_MAX].value; | |
a509a7cd PB |
8227 | #endif |
8228 | ||
d50dde5a DF |
8229 | rcu_read_unlock(); |
8230 | ||
1251201c | 8231 | return sched_attr_copy_to_user(uattr, &kattr, usize); |
d50dde5a DF |
8232 | |
8233 | out_unlock: | |
8234 | rcu_read_unlock(); | |
8235 | return retval; | |
8236 | } | |
8237 | ||
234b8ab6 WD |
8238 | #ifdef CONFIG_SMP |
8239 | int dl_task_check_affinity(struct task_struct *p, const struct cpumask *mask) | |
1da177e4 | 8240 | { |
234b8ab6 WD |
8241 | int ret = 0; |
8242 | ||
8243 | /* | |
8244 | * If the task isn't a deadline task or admission control is | |
8245 | * disabled then we don't care about affinity changes. | |
8246 | */ | |
8247 | if (!task_has_dl_policy(p) || !dl_bandwidth_enabled()) | |
8248 | return 0; | |
8249 | ||
8250 | /* | |
8251 | * Since bandwidth control happens on root_domain basis, | |
8252 | * if admission test is enabled, we only admit -deadline | |
8253 | * tasks allowed to run on all the CPUs in the task's | |
8254 | * root_domain. | |
8255 | */ | |
8256 | rcu_read_lock(); | |
8257 | if (!cpumask_subset(task_rq(p)->rd->span, mask)) | |
8258 | ret = -EBUSY; | |
8259 | rcu_read_unlock(); | |
8260 | return ret; | |
8261 | } | |
8262 | #endif | |
8263 | ||
db3b02ae | 8264 | static int |
713a2e21 | 8265 | __sched_setaffinity(struct task_struct *p, struct affinity_context *ctx) |
1da177e4 | 8266 | { |
36c8b586 | 8267 | int retval; |
5a16f3d3 | 8268 | cpumask_var_t cpus_allowed, new_mask; |
1da177e4 | 8269 | |
db3b02ae WD |
8270 | if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) |
8271 | return -ENOMEM; | |
1da177e4 | 8272 | |
5a16f3d3 RR |
8273 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) { |
8274 | retval = -ENOMEM; | |
8275 | goto out_free_cpus_allowed; | |
8276 | } | |
e4099a5e PZ |
8277 | |
8278 | cpuset_cpus_allowed(p, cpus_allowed); | |
713a2e21 WL |
8279 | cpumask_and(new_mask, ctx->new_mask, cpus_allowed); |
8280 | ||
8281 | ctx->new_mask = new_mask; | |
8282 | ctx->flags |= SCA_CHECK; | |
e4099a5e | 8283 | |
234b8ab6 WD |
8284 | retval = dl_task_check_affinity(p, new_mask); |
8285 | if (retval) | |
8286 | goto out_free_new_mask; | |
8f9ea86f | 8287 | |
713a2e21 | 8288 | retval = __set_cpus_allowed_ptr(p, ctx); |
db3b02ae WD |
8289 | if (retval) |
8290 | goto out_free_new_mask; | |
1da177e4 | 8291 | |
db3b02ae WD |
8292 | cpuset_cpus_allowed(p, cpus_allowed); |
8293 | if (!cpumask_subset(new_mask, cpus_allowed)) { | |
8294 | /* | |
8295 | * We must have raced with a concurrent cpuset update. | |
8296 | * Just reset the cpumask to the cpuset's cpus_allowed. | |
8297 | */ | |
8298 | cpumask_copy(new_mask, cpus_allowed); | |
8f9ea86f WL |
8299 | |
8300 | /* | |
8301 | * If SCA_USER is set, a 2nd call to __set_cpus_allowed_ptr() | |
8302 | * will restore the previous user_cpus_ptr value. | |
8303 | * | |
8304 | * In the unlikely event a previous user_cpus_ptr exists, | |
8305 | * we need to further restrict the mask to what is allowed | |
8306 | * by that old user_cpus_ptr. | |
8307 | */ | |
8308 | if (unlikely((ctx->flags & SCA_USER) && ctx->user_mask)) { | |
8309 | bool empty = !cpumask_and(new_mask, new_mask, | |
8310 | ctx->user_mask); | |
8311 | ||
8312 | if (WARN_ON_ONCE(empty)) | |
8313 | cpumask_copy(new_mask, cpus_allowed); | |
8314 | } | |
8315 | __set_cpus_allowed_ptr(p, ctx); | |
8316 | retval = -EINVAL; | |
8707d8b8 | 8317 | } |
db3b02ae | 8318 | |
16303ab2 | 8319 | out_free_new_mask: |
5a16f3d3 RR |
8320 | free_cpumask_var(new_mask); |
8321 | out_free_cpus_allowed: | |
8322 | free_cpumask_var(cpus_allowed); | |
db3b02ae WD |
8323 | return retval; |
8324 | } | |
8325 | ||
8326 | long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) | |
8327 | { | |
8f9ea86f WL |
8328 | struct affinity_context ac; |
8329 | struct cpumask *user_mask; | |
36c8b586 IM |
8330 | struct task_struct *p; |
8331 | int retval; | |
1da177e4 | 8332 | |
23f5d142 | 8333 | rcu_read_lock(); |
1da177e4 LT |
8334 | |
8335 | p = find_process_by_pid(pid); | |
8336 | if (!p) { | |
23f5d142 | 8337 | rcu_read_unlock(); |
1da177e4 LT |
8338 | return -ESRCH; |
8339 | } | |
8340 | ||
23f5d142 | 8341 | /* Prevent p going away */ |
1da177e4 | 8342 | get_task_struct(p); |
23f5d142 | 8343 | rcu_read_unlock(); |
1da177e4 | 8344 | |
14a40ffc TH |
8345 | if (p->flags & PF_NO_SETAFFINITY) { |
8346 | retval = -EINVAL; | |
8347 | goto out_put_task; | |
8348 | } | |
db3b02ae | 8349 | |
4c44aaaf EB |
8350 | if (!check_same_owner(p)) { |
8351 | rcu_read_lock(); | |
8352 | if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) { | |
8353 | rcu_read_unlock(); | |
db3b02ae WD |
8354 | retval = -EPERM; |
8355 | goto out_put_task; | |
4c44aaaf EB |
8356 | } |
8357 | rcu_read_unlock(); | |
8358 | } | |
1da177e4 | 8359 | |
b0ae1981 | 8360 | retval = security_task_setscheduler(p); |
e7834f8f | 8361 | if (retval) |
db3b02ae | 8362 | goto out_put_task; |
1da177e4 | 8363 | |
5657c116 WL |
8364 | /* |
8365 | * With non-SMP configs, user_cpus_ptr/user_mask isn't used and | |
8366 | * alloc_user_cpus_ptr() returns NULL. | |
8367 | */ | |
9a5418bc | 8368 | user_mask = alloc_user_cpus_ptr(NUMA_NO_NODE); |
5657c116 WL |
8369 | if (user_mask) { |
8370 | cpumask_copy(user_mask, in_mask); | |
8371 | } else if (IS_ENABLED(CONFIG_SMP)) { | |
8f9ea86f WL |
8372 | retval = -ENOMEM; |
8373 | goto out_put_task; | |
8374 | } | |
5657c116 | 8375 | |
8f9ea86f WL |
8376 | ac = (struct affinity_context){ |
8377 | .new_mask = in_mask, | |
8378 | .user_mask = user_mask, | |
8379 | .flags = SCA_USER, | |
8380 | }; | |
8381 | ||
713a2e21 | 8382 | retval = __sched_setaffinity(p, &ac); |
8f9ea86f WL |
8383 | kfree(ac.user_mask); |
8384 | ||
5a16f3d3 | 8385 | out_put_task: |
1da177e4 | 8386 | put_task_struct(p); |
1da177e4 LT |
8387 | return retval; |
8388 | } | |
8389 | ||
8390 | static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len, | |
96f874e2 | 8391 | struct cpumask *new_mask) |
1da177e4 | 8392 | { |
96f874e2 RR |
8393 | if (len < cpumask_size()) |
8394 | cpumask_clear(new_mask); | |
8395 | else if (len > cpumask_size()) | |
8396 | len = cpumask_size(); | |
8397 | ||
1da177e4 LT |
8398 | return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0; |
8399 | } | |
8400 | ||
8401 | /** | |
d1ccc66d | 8402 | * sys_sched_setaffinity - set the CPU affinity of a process |
1da177e4 LT |
8403 | * @pid: pid of the process |
8404 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr | |
d1ccc66d | 8405 | * @user_mask_ptr: user-space pointer to the new CPU mask |
e69f6186 YB |
8406 | * |
8407 | * Return: 0 on success. An error code otherwise. | |
1da177e4 | 8408 | */ |
5add95d4 HC |
8409 | SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len, |
8410 | unsigned long __user *, user_mask_ptr) | |
1da177e4 | 8411 | { |
5a16f3d3 | 8412 | cpumask_var_t new_mask; |
1da177e4 LT |
8413 | int retval; |
8414 | ||
5a16f3d3 RR |
8415 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) |
8416 | return -ENOMEM; | |
1da177e4 | 8417 | |
5a16f3d3 RR |
8418 | retval = get_user_cpu_mask(user_mask_ptr, len, new_mask); |
8419 | if (retval == 0) | |
8420 | retval = sched_setaffinity(pid, new_mask); | |
8421 | free_cpumask_var(new_mask); | |
8422 | return retval; | |
1da177e4 LT |
8423 | } |
8424 | ||
96f874e2 | 8425 | long sched_getaffinity(pid_t pid, struct cpumask *mask) |
1da177e4 | 8426 | { |
36c8b586 | 8427 | struct task_struct *p; |
31605683 | 8428 | unsigned long flags; |
1da177e4 | 8429 | int retval; |
1da177e4 | 8430 | |
23f5d142 | 8431 | rcu_read_lock(); |
1da177e4 LT |
8432 | |
8433 | retval = -ESRCH; | |
8434 | p = find_process_by_pid(pid); | |
8435 | if (!p) | |
8436 | goto out_unlock; | |
8437 | ||
e7834f8f DQ |
8438 | retval = security_task_getscheduler(p); |
8439 | if (retval) | |
8440 | goto out_unlock; | |
8441 | ||
013fdb80 | 8442 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
3bd37062 | 8443 | cpumask_and(mask, &p->cpus_mask, cpu_active_mask); |
013fdb80 | 8444 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
1da177e4 LT |
8445 | |
8446 | out_unlock: | |
23f5d142 | 8447 | rcu_read_unlock(); |
1da177e4 | 8448 | |
9531b62f | 8449 | return retval; |
1da177e4 LT |
8450 | } |
8451 | ||
8452 | /** | |
d1ccc66d | 8453 | * sys_sched_getaffinity - get the CPU affinity of a process |
1da177e4 LT |
8454 | * @pid: pid of the process |
8455 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr | |
d1ccc66d | 8456 | * @user_mask_ptr: user-space pointer to hold the current CPU mask |
e69f6186 | 8457 | * |
599b4840 ZW |
8458 | * Return: size of CPU mask copied to user_mask_ptr on success. An |
8459 | * error code otherwise. | |
1da177e4 | 8460 | */ |
5add95d4 HC |
8461 | SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len, |
8462 | unsigned long __user *, user_mask_ptr) | |
1da177e4 LT |
8463 | { |
8464 | int ret; | |
f17c8607 | 8465 | cpumask_var_t mask; |
1da177e4 | 8466 | |
84fba5ec | 8467 | if ((len * BITS_PER_BYTE) < nr_cpu_ids) |
cd3d8031 KM |
8468 | return -EINVAL; |
8469 | if (len & (sizeof(unsigned long)-1)) | |
1da177e4 LT |
8470 | return -EINVAL; |
8471 | ||
6015b1ac | 8472 | if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) |
f17c8607 | 8473 | return -ENOMEM; |
1da177e4 | 8474 | |
f17c8607 RR |
8475 | ret = sched_getaffinity(pid, mask); |
8476 | if (ret == 0) { | |
4de373a1 | 8477 | unsigned int retlen = min(len, cpumask_size()); |
cd3d8031 | 8478 | |
6015b1ac | 8479 | if (copy_to_user(user_mask_ptr, cpumask_bits(mask), retlen)) |
f17c8607 RR |
8480 | ret = -EFAULT; |
8481 | else | |
cd3d8031 | 8482 | ret = retlen; |
f17c8607 RR |
8483 | } |
8484 | free_cpumask_var(mask); | |
1da177e4 | 8485 | |
f17c8607 | 8486 | return ret; |
1da177e4 LT |
8487 | } |
8488 | ||
7d4dd4f1 | 8489 | static void do_sched_yield(void) |
1da177e4 | 8490 | { |
8a8c69c3 PZ |
8491 | struct rq_flags rf; |
8492 | struct rq *rq; | |
8493 | ||
246b3b33 | 8494 | rq = this_rq_lock_irq(&rf); |
1da177e4 | 8495 | |
ae92882e | 8496 | schedstat_inc(rq->yld_count); |
4530d7ab | 8497 | current->sched_class->yield_task(rq); |
1da177e4 | 8498 | |
8a8c69c3 | 8499 | preempt_disable(); |
345a957f | 8500 | rq_unlock_irq(rq, &rf); |
ba74c144 | 8501 | sched_preempt_enable_no_resched(); |
1da177e4 LT |
8502 | |
8503 | schedule(); | |
7d4dd4f1 | 8504 | } |
1da177e4 | 8505 | |
59a74b15 MCC |
8506 | /** |
8507 | * sys_sched_yield - yield the current processor to other threads. | |
8508 | * | |
8509 | * This function yields the current CPU to other tasks. If there are no | |
8510 | * other threads running on this CPU then this function will return. | |
8511 | * | |
8512 | * Return: 0. | |
8513 | */ | |
7d4dd4f1 DB |
8514 | SYSCALL_DEFINE0(sched_yield) |
8515 | { | |
8516 | do_sched_yield(); | |
1da177e4 LT |
8517 | return 0; |
8518 | } | |
8519 | ||
b965f1dd PZI |
8520 | #if !defined(CONFIG_PREEMPTION) || defined(CONFIG_PREEMPT_DYNAMIC) |
8521 | int __sched __cond_resched(void) | |
1da177e4 | 8522 | { |
fe32d3cd | 8523 | if (should_resched(0)) { |
a18b5d01 | 8524 | preempt_schedule_common(); |
1da177e4 LT |
8525 | return 1; |
8526 | } | |
50895825 FW |
8527 | /* |
8528 | * In preemptible kernels, ->rcu_read_lock_nesting tells the tick | |
8529 | * whether the current CPU is in an RCU read-side critical section, | |
8530 | * so the tick can report quiescent states even for CPUs looping | |
8531 | * in kernel context. In contrast, in non-preemptible kernels, | |
8532 | * RCU readers leave no in-memory hints, which means that CPU-bound | |
8533 | * processes executing in kernel context might never report an | |
8534 | * RCU quiescent state. Therefore, the following code causes | |
8535 | * cond_resched() to report a quiescent state, but only when RCU | |
8536 | * is in urgent need of one. | |
8537 | */ | |
b965f1dd | 8538 | #ifndef CONFIG_PREEMPT_RCU |
f79c3ad6 | 8539 | rcu_all_qs(); |
b965f1dd | 8540 | #endif |
1da177e4 LT |
8541 | return 0; |
8542 | } | |
b965f1dd PZI |
8543 | EXPORT_SYMBOL(__cond_resched); |
8544 | #endif | |
8545 | ||
8546 | #ifdef CONFIG_PREEMPT_DYNAMIC | |
99cf983c | 8547 | #if defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL) |
8a69fe0b MR |
8548 | #define cond_resched_dynamic_enabled __cond_resched |
8549 | #define cond_resched_dynamic_disabled ((void *)&__static_call_return0) | |
b965f1dd | 8550 | DEFINE_STATIC_CALL_RET0(cond_resched, __cond_resched); |
ef72661e | 8551 | EXPORT_STATIC_CALL_TRAMP(cond_resched); |
b965f1dd | 8552 | |
8a69fe0b MR |
8553 | #define might_resched_dynamic_enabled __cond_resched |
8554 | #define might_resched_dynamic_disabled ((void *)&__static_call_return0) | |
b965f1dd | 8555 | DEFINE_STATIC_CALL_RET0(might_resched, __cond_resched); |
ef72661e | 8556 | EXPORT_STATIC_CALL_TRAMP(might_resched); |
99cf983c MR |
8557 | #elif defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY) |
8558 | static DEFINE_STATIC_KEY_FALSE(sk_dynamic_cond_resched); | |
8559 | int __sched dynamic_cond_resched(void) | |
8560 | { | |
e3ff7c60 | 8561 | klp_sched_try_switch(); |
99cf983c MR |
8562 | if (!static_branch_unlikely(&sk_dynamic_cond_resched)) |
8563 | return 0; | |
8564 | return __cond_resched(); | |
8565 | } | |
8566 | EXPORT_SYMBOL(dynamic_cond_resched); | |
8567 | ||
8568 | static DEFINE_STATIC_KEY_FALSE(sk_dynamic_might_resched); | |
8569 | int __sched dynamic_might_resched(void) | |
8570 | { | |
8571 | if (!static_branch_unlikely(&sk_dynamic_might_resched)) | |
8572 | return 0; | |
8573 | return __cond_resched(); | |
8574 | } | |
8575 | EXPORT_SYMBOL(dynamic_might_resched); | |
8576 | #endif | |
35a773a0 | 8577 | #endif |
1da177e4 LT |
8578 | |
8579 | /* | |
613afbf8 | 8580 | * __cond_resched_lock() - if a reschedule is pending, drop the given lock, |
1da177e4 LT |
8581 | * call schedule, and on return reacquire the lock. |
8582 | * | |
c1a280b6 | 8583 | * This works OK both with and without CONFIG_PREEMPTION. We do strange low-level |
1da177e4 LT |
8584 | * operations here to prevent schedule() from being called twice (once via |
8585 | * spin_unlock(), once by hand). | |
8586 | */ | |
613afbf8 | 8587 | int __cond_resched_lock(spinlock_t *lock) |
1da177e4 | 8588 | { |
fe32d3cd | 8589 | int resched = should_resched(PREEMPT_LOCK_OFFSET); |
6df3cecb JK |
8590 | int ret = 0; |
8591 | ||
f607c668 PZ |
8592 | lockdep_assert_held(lock); |
8593 | ||
4a81e832 | 8594 | if (spin_needbreak(lock) || resched) { |
1da177e4 | 8595 | spin_unlock(lock); |
7e406d1f | 8596 | if (!_cond_resched()) |
95c354fe | 8597 | cpu_relax(); |
6df3cecb | 8598 | ret = 1; |
1da177e4 | 8599 | spin_lock(lock); |
1da177e4 | 8600 | } |
6df3cecb | 8601 | return ret; |
1da177e4 | 8602 | } |
613afbf8 | 8603 | EXPORT_SYMBOL(__cond_resched_lock); |
1da177e4 | 8604 | |
f3d4b4b1 BG |
8605 | int __cond_resched_rwlock_read(rwlock_t *lock) |
8606 | { | |
8607 | int resched = should_resched(PREEMPT_LOCK_OFFSET); | |
8608 | int ret = 0; | |
8609 | ||
8610 | lockdep_assert_held_read(lock); | |
8611 | ||
8612 | if (rwlock_needbreak(lock) || resched) { | |
8613 | read_unlock(lock); | |
7e406d1f | 8614 | if (!_cond_resched()) |
f3d4b4b1 BG |
8615 | cpu_relax(); |
8616 | ret = 1; | |
8617 | read_lock(lock); | |
8618 | } | |
8619 | return ret; | |
8620 | } | |
8621 | EXPORT_SYMBOL(__cond_resched_rwlock_read); | |
8622 | ||
8623 | int __cond_resched_rwlock_write(rwlock_t *lock) | |
8624 | { | |
8625 | int resched = should_resched(PREEMPT_LOCK_OFFSET); | |
8626 | int ret = 0; | |
8627 | ||
8628 | lockdep_assert_held_write(lock); | |
8629 | ||
8630 | if (rwlock_needbreak(lock) || resched) { | |
8631 | write_unlock(lock); | |
7e406d1f | 8632 | if (!_cond_resched()) |
f3d4b4b1 BG |
8633 | cpu_relax(); |
8634 | ret = 1; | |
8635 | write_lock(lock); | |
8636 | } | |
8637 | return ret; | |
8638 | } | |
8639 | EXPORT_SYMBOL(__cond_resched_rwlock_write); | |
8640 | ||
4c748558 MR |
8641 | #ifdef CONFIG_PREEMPT_DYNAMIC |
8642 | ||
33c64734 | 8643 | #ifdef CONFIG_GENERIC_ENTRY |
4c748558 | 8644 | #include <linux/entry-common.h> |
33c64734 | 8645 | #endif |
4c748558 MR |
8646 | |
8647 | /* | |
8648 | * SC:cond_resched | |
8649 | * SC:might_resched | |
8650 | * SC:preempt_schedule | |
8651 | * SC:preempt_schedule_notrace | |
8652 | * SC:irqentry_exit_cond_resched | |
8653 | * | |
8654 | * | |
8655 | * NONE: | |
8656 | * cond_resched <- __cond_resched | |
8657 | * might_resched <- RET0 | |
8658 | * preempt_schedule <- NOP | |
8659 | * preempt_schedule_notrace <- NOP | |
8660 | * irqentry_exit_cond_resched <- NOP | |
8661 | * | |
8662 | * VOLUNTARY: | |
8663 | * cond_resched <- __cond_resched | |
8664 | * might_resched <- __cond_resched | |
8665 | * preempt_schedule <- NOP | |
8666 | * preempt_schedule_notrace <- NOP | |
8667 | * irqentry_exit_cond_resched <- NOP | |
8668 | * | |
8669 | * FULL: | |
8670 | * cond_resched <- RET0 | |
8671 | * might_resched <- RET0 | |
8672 | * preempt_schedule <- preempt_schedule | |
8673 | * preempt_schedule_notrace <- preempt_schedule_notrace | |
8674 | * irqentry_exit_cond_resched <- irqentry_exit_cond_resched | |
8675 | */ | |
8676 | ||
8677 | enum { | |
8678 | preempt_dynamic_undefined = -1, | |
8679 | preempt_dynamic_none, | |
8680 | preempt_dynamic_voluntary, | |
8681 | preempt_dynamic_full, | |
8682 | }; | |
8683 | ||
8684 | int preempt_dynamic_mode = preempt_dynamic_undefined; | |
8685 | ||
8686 | int sched_dynamic_mode(const char *str) | |
8687 | { | |
8688 | if (!strcmp(str, "none")) | |
8689 | return preempt_dynamic_none; | |
8690 | ||
8691 | if (!strcmp(str, "voluntary")) | |
8692 | return preempt_dynamic_voluntary; | |
8693 | ||
8694 | if (!strcmp(str, "full")) | |
8695 | return preempt_dynamic_full; | |
8696 | ||
8697 | return -EINVAL; | |
8698 | } | |
8699 | ||
99cf983c | 8700 | #if defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL) |
8a69fe0b MR |
8701 | #define preempt_dynamic_enable(f) static_call_update(f, f##_dynamic_enabled) |
8702 | #define preempt_dynamic_disable(f) static_call_update(f, f##_dynamic_disabled) | |
99cf983c MR |
8703 | #elif defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY) |
8704 | #define preempt_dynamic_enable(f) static_key_enable(&sk_dynamic_##f.key) | |
8705 | #define preempt_dynamic_disable(f) static_key_disable(&sk_dynamic_##f.key) | |
8706 | #else | |
8707 | #error "Unsupported PREEMPT_DYNAMIC mechanism" | |
8708 | #endif | |
8a69fe0b | 8709 | |
9b8e1781 | 8710 | static DEFINE_MUTEX(sched_dynamic_mutex); |
e3ff7c60 JP |
8711 | static bool klp_override; |
8712 | ||
8713 | static void __sched_dynamic_update(int mode) | |
4c748558 MR |
8714 | { |
8715 | /* | |
8716 | * Avoid {NONE,VOLUNTARY} -> FULL transitions from ever ending up in | |
8717 | * the ZERO state, which is invalid. | |
8718 | */ | |
e3ff7c60 JP |
8719 | if (!klp_override) |
8720 | preempt_dynamic_enable(cond_resched); | |
8a69fe0b MR |
8721 | preempt_dynamic_enable(might_resched); |
8722 | preempt_dynamic_enable(preempt_schedule); | |
8723 | preempt_dynamic_enable(preempt_schedule_notrace); | |
8724 | preempt_dynamic_enable(irqentry_exit_cond_resched); | |
4c748558 MR |
8725 | |
8726 | switch (mode) { | |
8727 | case preempt_dynamic_none: | |
e3ff7c60 JP |
8728 | if (!klp_override) |
8729 | preempt_dynamic_enable(cond_resched); | |
8a69fe0b MR |
8730 | preempt_dynamic_disable(might_resched); |
8731 | preempt_dynamic_disable(preempt_schedule); | |
8732 | preempt_dynamic_disable(preempt_schedule_notrace); | |
8733 | preempt_dynamic_disable(irqentry_exit_cond_resched); | |
e3ff7c60 JP |
8734 | if (mode != preempt_dynamic_mode) |
8735 | pr_info("Dynamic Preempt: none\n"); | |
4c748558 MR |
8736 | break; |
8737 | ||
8738 | case preempt_dynamic_voluntary: | |
e3ff7c60 JP |
8739 | if (!klp_override) |
8740 | preempt_dynamic_enable(cond_resched); | |
8a69fe0b MR |
8741 | preempt_dynamic_enable(might_resched); |
8742 | preempt_dynamic_disable(preempt_schedule); | |
8743 | preempt_dynamic_disable(preempt_schedule_notrace); | |
8744 | preempt_dynamic_disable(irqentry_exit_cond_resched); | |
e3ff7c60 JP |
8745 | if (mode != preempt_dynamic_mode) |
8746 | pr_info("Dynamic Preempt: voluntary\n"); | |
4c748558 MR |
8747 | break; |
8748 | ||
8749 | case preempt_dynamic_full: | |
e3ff7c60 JP |
8750 | if (!klp_override) |
8751 | preempt_dynamic_disable(cond_resched); | |
8a69fe0b MR |
8752 | preempt_dynamic_disable(might_resched); |
8753 | preempt_dynamic_enable(preempt_schedule); | |
8754 | preempt_dynamic_enable(preempt_schedule_notrace); | |
8755 | preempt_dynamic_enable(irqentry_exit_cond_resched); | |
e3ff7c60 JP |
8756 | if (mode != preempt_dynamic_mode) |
8757 | pr_info("Dynamic Preempt: full\n"); | |
4c748558 MR |
8758 | break; |
8759 | } | |
8760 | ||
8761 | preempt_dynamic_mode = mode; | |
8762 | } | |
8763 | ||
e3ff7c60 JP |
8764 | void sched_dynamic_update(int mode) |
8765 | { | |
8766 | mutex_lock(&sched_dynamic_mutex); | |
8767 | __sched_dynamic_update(mode); | |
8768 | mutex_unlock(&sched_dynamic_mutex); | |
8769 | } | |
8770 | ||
8771 | #ifdef CONFIG_HAVE_PREEMPT_DYNAMIC_CALL | |
8772 | ||
8773 | static int klp_cond_resched(void) | |
8774 | { | |
8775 | __klp_sched_try_switch(); | |
8776 | return __cond_resched(); | |
8777 | } | |
8778 | ||
8779 | void sched_dynamic_klp_enable(void) | |
8780 | { | |
8781 | mutex_lock(&sched_dynamic_mutex); | |
8782 | ||
8783 | klp_override = true; | |
8784 | static_call_update(cond_resched, klp_cond_resched); | |
8785 | ||
8786 | mutex_unlock(&sched_dynamic_mutex); | |
8787 | } | |
8788 | ||
8789 | void sched_dynamic_klp_disable(void) | |
8790 | { | |
8791 | mutex_lock(&sched_dynamic_mutex); | |
8792 | ||
8793 | klp_override = false; | |
8794 | __sched_dynamic_update(preempt_dynamic_mode); | |
8795 | ||
8796 | mutex_unlock(&sched_dynamic_mutex); | |
8797 | } | |
8798 | ||
8799 | #endif /* CONFIG_HAVE_PREEMPT_DYNAMIC_CALL */ | |
8800 | ||
4c748558 MR |
8801 | static int __init setup_preempt_mode(char *str) |
8802 | { | |
8803 | int mode = sched_dynamic_mode(str); | |
8804 | if (mode < 0) { | |
8805 | pr_warn("Dynamic Preempt: unsupported mode: %s\n", str); | |
8806 | return 0; | |
8807 | } | |
8808 | ||
8809 | sched_dynamic_update(mode); | |
8810 | return 1; | |
8811 | } | |
8812 | __setup("preempt=", setup_preempt_mode); | |
8813 | ||
8814 | static void __init preempt_dynamic_init(void) | |
8815 | { | |
8816 | if (preempt_dynamic_mode == preempt_dynamic_undefined) { | |
8817 | if (IS_ENABLED(CONFIG_PREEMPT_NONE)) { | |
8818 | sched_dynamic_update(preempt_dynamic_none); | |
8819 | } else if (IS_ENABLED(CONFIG_PREEMPT_VOLUNTARY)) { | |
8820 | sched_dynamic_update(preempt_dynamic_voluntary); | |
8821 | } else { | |
8822 | /* Default static call setting, nothing to do */ | |
8823 | WARN_ON_ONCE(!IS_ENABLED(CONFIG_PREEMPT)); | |
8824 | preempt_dynamic_mode = preempt_dynamic_full; | |
8825 | pr_info("Dynamic Preempt: full\n"); | |
8826 | } | |
8827 | } | |
8828 | } | |
8829 | ||
cfe43f47 VS |
8830 | #define PREEMPT_MODEL_ACCESSOR(mode) \ |
8831 | bool preempt_model_##mode(void) \ | |
8832 | { \ | |
8833 | WARN_ON_ONCE(preempt_dynamic_mode == preempt_dynamic_undefined); \ | |
8834 | return preempt_dynamic_mode == preempt_dynamic_##mode; \ | |
8835 | } \ | |
8836 | EXPORT_SYMBOL_GPL(preempt_model_##mode) | |
8837 | ||
8838 | PREEMPT_MODEL_ACCESSOR(none); | |
8839 | PREEMPT_MODEL_ACCESSOR(voluntary); | |
8840 | PREEMPT_MODEL_ACCESSOR(full); | |
8841 | ||
4c748558 MR |
8842 | #else /* !CONFIG_PREEMPT_DYNAMIC */ |
8843 | ||
8844 | static inline void preempt_dynamic_init(void) { } | |
8845 | ||
8846 | #endif /* #ifdef CONFIG_PREEMPT_DYNAMIC */ | |
8847 | ||
1da177e4 LT |
8848 | /** |
8849 | * yield - yield the current processor to other threads. | |
8850 | * | |
8e3fabfd PZ |
8851 | * Do not ever use this function, there's a 99% chance you're doing it wrong. |
8852 | * | |
8853 | * The scheduler is at all times free to pick the calling task as the most | |
8854 | * eligible task to run, if removing the yield() call from your code breaks | |
b19a888c | 8855 | * it, it's already broken. |
8e3fabfd PZ |
8856 | * |
8857 | * Typical broken usage is: | |
8858 | * | |
8859 | * while (!event) | |
d1ccc66d | 8860 | * yield(); |
8e3fabfd PZ |
8861 | * |
8862 | * where one assumes that yield() will let 'the other' process run that will | |
8863 | * make event true. If the current task is a SCHED_FIFO task that will never | |
8864 | * happen. Never use yield() as a progress guarantee!! | |
8865 | * | |
8866 | * If you want to use yield() to wait for something, use wait_event(). | |
8867 | * If you want to use yield() to be 'nice' for others, use cond_resched(). | |
8868 | * If you still want to use yield(), do not! | |
1da177e4 LT |
8869 | */ |
8870 | void __sched yield(void) | |
8871 | { | |
8872 | set_current_state(TASK_RUNNING); | |
7d4dd4f1 | 8873 | do_sched_yield(); |
1da177e4 | 8874 | } |
1da177e4 LT |
8875 | EXPORT_SYMBOL(yield); |
8876 | ||
d95f4122 MG |
8877 | /** |
8878 | * yield_to - yield the current processor to another thread in | |
8879 | * your thread group, or accelerate that thread toward the | |
8880 | * processor it's on. | |
16addf95 RD |
8881 | * @p: target task |
8882 | * @preempt: whether task preemption is allowed or not | |
d95f4122 MG |
8883 | * |
8884 | * It's the caller's job to ensure that the target task struct | |
8885 | * can't go away on us before we can do any checks. | |
8886 | * | |
e69f6186 | 8887 | * Return: |
7b270f60 PZ |
8888 | * true (>0) if we indeed boosted the target task. |
8889 | * false (0) if we failed to boost the target. | |
8890 | * -ESRCH if there's no task to yield to. | |
d95f4122 | 8891 | */ |
fa93384f | 8892 | int __sched yield_to(struct task_struct *p, bool preempt) |
d95f4122 MG |
8893 | { |
8894 | struct task_struct *curr = current; | |
8895 | struct rq *rq, *p_rq; | |
8896 | unsigned long flags; | |
c3c18640 | 8897 | int yielded = 0; |
d95f4122 MG |
8898 | |
8899 | local_irq_save(flags); | |
8900 | rq = this_rq(); | |
8901 | ||
8902 | again: | |
8903 | p_rq = task_rq(p); | |
7b270f60 PZ |
8904 | /* |
8905 | * If we're the only runnable task on the rq and target rq also | |
8906 | * has only one task, there's absolutely no point in yielding. | |
8907 | */ | |
8908 | if (rq->nr_running == 1 && p_rq->nr_running == 1) { | |
8909 | yielded = -ESRCH; | |
8910 | goto out_irq; | |
8911 | } | |
8912 | ||
d95f4122 | 8913 | double_rq_lock(rq, p_rq); |
39e24d8f | 8914 | if (task_rq(p) != p_rq) { |
d95f4122 MG |
8915 | double_rq_unlock(rq, p_rq); |
8916 | goto again; | |
8917 | } | |
8918 | ||
8919 | if (!curr->sched_class->yield_to_task) | |
7b270f60 | 8920 | goto out_unlock; |
d95f4122 MG |
8921 | |
8922 | if (curr->sched_class != p->sched_class) | |
7b270f60 | 8923 | goto out_unlock; |
d95f4122 | 8924 | |
0b9d46fc | 8925 | if (task_on_cpu(p_rq, p) || !task_is_running(p)) |
7b270f60 | 8926 | goto out_unlock; |
d95f4122 | 8927 | |
0900acf2 | 8928 | yielded = curr->sched_class->yield_to_task(rq, p); |
6d1cafd8 | 8929 | if (yielded) { |
ae92882e | 8930 | schedstat_inc(rq->yld_count); |
6d1cafd8 VP |
8931 | /* |
8932 | * Make p's CPU reschedule; pick_next_entity takes care of | |
8933 | * fairness. | |
8934 | */ | |
8935 | if (preempt && rq != p_rq) | |
8875125e | 8936 | resched_curr(p_rq); |
6d1cafd8 | 8937 | } |
d95f4122 | 8938 | |
7b270f60 | 8939 | out_unlock: |
d95f4122 | 8940 | double_rq_unlock(rq, p_rq); |
7b270f60 | 8941 | out_irq: |
d95f4122 MG |
8942 | local_irq_restore(flags); |
8943 | ||
7b270f60 | 8944 | if (yielded > 0) |
d95f4122 MG |
8945 | schedule(); |
8946 | ||
8947 | return yielded; | |
8948 | } | |
8949 | EXPORT_SYMBOL_GPL(yield_to); | |
8950 | ||
10ab5643 TH |
8951 | int io_schedule_prepare(void) |
8952 | { | |
8953 | int old_iowait = current->in_iowait; | |
8954 | ||
8955 | current->in_iowait = 1; | |
aa8dccca | 8956 | blk_flush_plug(current->plug, true); |
10ab5643 TH |
8957 | return old_iowait; |
8958 | } | |
8959 | ||
8960 | void io_schedule_finish(int token) | |
8961 | { | |
8962 | current->in_iowait = token; | |
8963 | } | |
8964 | ||
1da177e4 | 8965 | /* |
41a2d6cf | 8966 | * This task is about to go to sleep on IO. Increment rq->nr_iowait so |
1da177e4 | 8967 | * that process accounting knows that this is a task in IO wait state. |
1da177e4 | 8968 | */ |
1da177e4 LT |
8969 | long __sched io_schedule_timeout(long timeout) |
8970 | { | |
10ab5643 | 8971 | int token; |
1da177e4 LT |
8972 | long ret; |
8973 | ||
10ab5643 | 8974 | token = io_schedule_prepare(); |
1da177e4 | 8975 | ret = schedule_timeout(timeout); |
10ab5643 | 8976 | io_schedule_finish(token); |
9cff8ade | 8977 | |
1da177e4 LT |
8978 | return ret; |
8979 | } | |
9cff8ade | 8980 | EXPORT_SYMBOL(io_schedule_timeout); |
1da177e4 | 8981 | |
e3b929b0 | 8982 | void __sched io_schedule(void) |
10ab5643 TH |
8983 | { |
8984 | int token; | |
8985 | ||
8986 | token = io_schedule_prepare(); | |
8987 | schedule(); | |
8988 | io_schedule_finish(token); | |
8989 | } | |
8990 | EXPORT_SYMBOL(io_schedule); | |
8991 | ||
1da177e4 LT |
8992 | /** |
8993 | * sys_sched_get_priority_max - return maximum RT priority. | |
8994 | * @policy: scheduling class. | |
8995 | * | |
e69f6186 YB |
8996 | * Return: On success, this syscall returns the maximum |
8997 | * rt_priority that can be used by a given scheduling class. | |
8998 | * On failure, a negative error code is returned. | |
1da177e4 | 8999 | */ |
5add95d4 | 9000 | SYSCALL_DEFINE1(sched_get_priority_max, int, policy) |
1da177e4 LT |
9001 | { |
9002 | int ret = -EINVAL; | |
9003 | ||
9004 | switch (policy) { | |
9005 | case SCHED_FIFO: | |
9006 | case SCHED_RR: | |
ae18ad28 | 9007 | ret = MAX_RT_PRIO-1; |
1da177e4 | 9008 | break; |
aab03e05 | 9009 | case SCHED_DEADLINE: |
1da177e4 | 9010 | case SCHED_NORMAL: |
b0a9499c | 9011 | case SCHED_BATCH: |
dd41f596 | 9012 | case SCHED_IDLE: |
1da177e4 LT |
9013 | ret = 0; |
9014 | break; | |
9015 | } | |
9016 | return ret; | |
9017 | } | |
9018 | ||
9019 | /** | |
9020 | * sys_sched_get_priority_min - return minimum RT priority. | |
9021 | * @policy: scheduling class. | |
9022 | * | |
e69f6186 YB |
9023 | * Return: On success, this syscall returns the minimum |
9024 | * rt_priority that can be used by a given scheduling class. | |
9025 | * On failure, a negative error code is returned. | |
1da177e4 | 9026 | */ |
5add95d4 | 9027 | SYSCALL_DEFINE1(sched_get_priority_min, int, policy) |
1da177e4 LT |
9028 | { |
9029 | int ret = -EINVAL; | |
9030 | ||
9031 | switch (policy) { | |
9032 | case SCHED_FIFO: | |
9033 | case SCHED_RR: | |
9034 | ret = 1; | |
9035 | break; | |
aab03e05 | 9036 | case SCHED_DEADLINE: |
1da177e4 | 9037 | case SCHED_NORMAL: |
b0a9499c | 9038 | case SCHED_BATCH: |
dd41f596 | 9039 | case SCHED_IDLE: |
1da177e4 LT |
9040 | ret = 0; |
9041 | } | |
9042 | return ret; | |
9043 | } | |
9044 | ||
abca5fc5 | 9045 | static int sched_rr_get_interval(pid_t pid, struct timespec64 *t) |
1da177e4 | 9046 | { |
36c8b586 | 9047 | struct task_struct *p; |
a4ec24b4 | 9048 | unsigned int time_slice; |
eb580751 | 9049 | struct rq_flags rf; |
dba091b9 | 9050 | struct rq *rq; |
3a5c359a | 9051 | int retval; |
1da177e4 LT |
9052 | |
9053 | if (pid < 0) | |
3a5c359a | 9054 | return -EINVAL; |
1da177e4 LT |
9055 | |
9056 | retval = -ESRCH; | |
1a551ae7 | 9057 | rcu_read_lock(); |
1da177e4 LT |
9058 | p = find_process_by_pid(pid); |
9059 | if (!p) | |
9060 | goto out_unlock; | |
9061 | ||
9062 | retval = security_task_getscheduler(p); | |
9063 | if (retval) | |
9064 | goto out_unlock; | |
9065 | ||
eb580751 | 9066 | rq = task_rq_lock(p, &rf); |
a57beec5 PZ |
9067 | time_slice = 0; |
9068 | if (p->sched_class->get_rr_interval) | |
9069 | time_slice = p->sched_class->get_rr_interval(rq, p); | |
eb580751 | 9070 | task_rq_unlock(rq, p, &rf); |
a4ec24b4 | 9071 | |
1a551ae7 | 9072 | rcu_read_unlock(); |
abca5fc5 AV |
9073 | jiffies_to_timespec64(time_slice, t); |
9074 | return 0; | |
3a5c359a | 9075 | |
1da177e4 | 9076 | out_unlock: |
1a551ae7 | 9077 | rcu_read_unlock(); |
1da177e4 LT |
9078 | return retval; |
9079 | } | |
9080 | ||
2064a5ab RD |
9081 | /** |
9082 | * sys_sched_rr_get_interval - return the default timeslice of a process. | |
9083 | * @pid: pid of the process. | |
9084 | * @interval: userspace pointer to the timeslice value. | |
9085 | * | |
9086 | * this syscall writes the default timeslice value of a given process | |
9087 | * into the user-space timespec buffer. A value of '0' means infinity. | |
9088 | * | |
9089 | * Return: On success, 0 and the timeslice is in @interval. Otherwise, | |
9090 | * an error code. | |
9091 | */ | |
abca5fc5 | 9092 | SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid, |
474b9c77 | 9093 | struct __kernel_timespec __user *, interval) |
abca5fc5 AV |
9094 | { |
9095 | struct timespec64 t; | |
9096 | int retval = sched_rr_get_interval(pid, &t); | |
9097 | ||
9098 | if (retval == 0) | |
9099 | retval = put_timespec64(&t, interval); | |
9100 | ||
9101 | return retval; | |
9102 | } | |
9103 | ||
474b9c77 | 9104 | #ifdef CONFIG_COMPAT_32BIT_TIME |
8dabe724 AB |
9105 | SYSCALL_DEFINE2(sched_rr_get_interval_time32, pid_t, pid, |
9106 | struct old_timespec32 __user *, interval) | |
abca5fc5 AV |
9107 | { |
9108 | struct timespec64 t; | |
9109 | int retval = sched_rr_get_interval(pid, &t); | |
9110 | ||
9111 | if (retval == 0) | |
9afc5eee | 9112 | retval = put_old_timespec32(&t, interval); |
abca5fc5 AV |
9113 | return retval; |
9114 | } | |
9115 | #endif | |
9116 | ||
82a1fcb9 | 9117 | void sched_show_task(struct task_struct *p) |
1da177e4 | 9118 | { |
1da177e4 | 9119 | unsigned long free = 0; |
4e79752c | 9120 | int ppid; |
c930b2c0 | 9121 | |
38200502 TH |
9122 | if (!try_get_task_stack(p)) |
9123 | return; | |
20435d84 | 9124 | |
cc172ff3 | 9125 | pr_info("task:%-15.15s state:%c", p->comm, task_state_to_char(p)); |
20435d84 | 9126 | |
b03fbd4f | 9127 | if (task_is_running(p)) |
cc172ff3 | 9128 | pr_cont(" running task "); |
1da177e4 | 9129 | #ifdef CONFIG_DEBUG_STACK_USAGE |
7c9f8861 | 9130 | free = stack_not_used(p); |
1da177e4 | 9131 | #endif |
a90e984c | 9132 | ppid = 0; |
4e79752c | 9133 | rcu_read_lock(); |
a90e984c ON |
9134 | if (pid_alive(p)) |
9135 | ppid = task_pid_nr(rcu_dereference(p->real_parent)); | |
4e79752c | 9136 | rcu_read_unlock(); |
0f03d680 | 9137 | pr_cont(" stack:%-5lu pid:%-5d ppid:%-6d flags:0x%08lx\n", |
cc172ff3 | 9138 | free, task_pid_nr(p), ppid, |
0569b245 | 9139 | read_task_thread_flags(p)); |
1da177e4 | 9140 | |
3d1cb205 | 9141 | print_worker_info(KERN_INFO, p); |
a8b62fd0 | 9142 | print_stop_info(KERN_INFO, p); |
9cb8f069 | 9143 | show_stack(p, NULL, KERN_INFO); |
38200502 | 9144 | put_task_stack(p); |
1da177e4 | 9145 | } |
0032f4e8 | 9146 | EXPORT_SYMBOL_GPL(sched_show_task); |
1da177e4 | 9147 | |
5d68cc95 PZ |
9148 | static inline bool |
9149 | state_filter_match(unsigned long state_filter, struct task_struct *p) | |
9150 | { | |
2f064a59 PZ |
9151 | unsigned int state = READ_ONCE(p->__state); |
9152 | ||
5d68cc95 PZ |
9153 | /* no filter, everything matches */ |
9154 | if (!state_filter) | |
9155 | return true; | |
9156 | ||
9157 | /* filter, but doesn't match */ | |
2f064a59 | 9158 | if (!(state & state_filter)) |
5d68cc95 PZ |
9159 | return false; |
9160 | ||
9161 | /* | |
9162 | * When looking for TASK_UNINTERRUPTIBLE skip TASK_IDLE (allows | |
9163 | * TASK_KILLABLE). | |
9164 | */ | |
5aec788a | 9165 | if (state_filter == TASK_UNINTERRUPTIBLE && (state & TASK_NOLOAD)) |
5d68cc95 PZ |
9166 | return false; |
9167 | ||
9168 | return true; | |
9169 | } | |
9170 | ||
9171 | ||
2f064a59 | 9172 | void show_state_filter(unsigned int state_filter) |
1da177e4 | 9173 | { |
36c8b586 | 9174 | struct task_struct *g, *p; |
1da177e4 | 9175 | |
510f5acc | 9176 | rcu_read_lock(); |
5d07f420 | 9177 | for_each_process_thread(g, p) { |
1da177e4 LT |
9178 | /* |
9179 | * reset the NMI-timeout, listing all files on a slow | |
25985edc | 9180 | * console might take a lot of time: |
57675cb9 AR |
9181 | * Also, reset softlockup watchdogs on all CPUs, because |
9182 | * another CPU might be blocked waiting for us to process | |
9183 | * an IPI. | |
1da177e4 LT |
9184 | */ |
9185 | touch_nmi_watchdog(); | |
57675cb9 | 9186 | touch_all_softlockup_watchdogs(); |
5d68cc95 | 9187 | if (state_filter_match(state_filter, p)) |
82a1fcb9 | 9188 | sched_show_task(p); |
5d07f420 | 9189 | } |
1da177e4 | 9190 | |
dd41f596 | 9191 | #ifdef CONFIG_SCHED_DEBUG |
fb90a6e9 RV |
9192 | if (!state_filter) |
9193 | sysrq_sched_debug_show(); | |
dd41f596 | 9194 | #endif |
510f5acc | 9195 | rcu_read_unlock(); |
e59e2ae2 IM |
9196 | /* |
9197 | * Only show locks if all tasks are dumped: | |
9198 | */ | |
93335a21 | 9199 | if (!state_filter) |
e59e2ae2 | 9200 | debug_show_all_locks(); |
1da177e4 LT |
9201 | } |
9202 | ||
f340c0d1 IM |
9203 | /** |
9204 | * init_idle - set up an idle thread for a given CPU | |
9205 | * @idle: task in question | |
d1ccc66d | 9206 | * @cpu: CPU the idle task belongs to |
f340c0d1 IM |
9207 | * |
9208 | * NOTE: this function does not set the idle thread's NEED_RESCHED | |
9209 | * flag, to make booting more robust. | |
9210 | */ | |
f1a0a376 | 9211 | void __init init_idle(struct task_struct *idle, int cpu) |
1da177e4 | 9212 | { |
713a2e21 WL |
9213 | #ifdef CONFIG_SMP |
9214 | struct affinity_context ac = (struct affinity_context) { | |
9215 | .new_mask = cpumask_of(cpu), | |
9216 | .flags = 0, | |
9217 | }; | |
9218 | #endif | |
70b97a7f | 9219 | struct rq *rq = cpu_rq(cpu); |
1da177e4 LT |
9220 | unsigned long flags; |
9221 | ||
ff51ff84 PZ |
9222 | __sched_fork(0, idle); |
9223 | ||
25834c73 | 9224 | raw_spin_lock_irqsave(&idle->pi_lock, flags); |
5cb9eaa3 | 9225 | raw_spin_rq_lock(rq); |
5cbd54ef | 9226 | |
2f064a59 | 9227 | idle->__state = TASK_RUNNING; |
dd41f596 | 9228 | idle->se.exec_start = sched_clock(); |
00b89fe0 VS |
9229 | /* |
9230 | * PF_KTHREAD should already be set at this point; regardless, make it | |
9231 | * look like a proper per-CPU kthread. | |
9232 | */ | |
9233 | idle->flags |= PF_IDLE | PF_KTHREAD | PF_NO_SETAFFINITY; | |
9234 | kthread_set_per_cpu(idle, cpu); | |
dd41f596 | 9235 | |
de9b8f5d PZ |
9236 | #ifdef CONFIG_SMP |
9237 | /* | |
b19a888c | 9238 | * It's possible that init_idle() gets called multiple times on a task, |
de9b8f5d PZ |
9239 | * in that case do_set_cpus_allowed() will not do the right thing. |
9240 | * | |
9241 | * And since this is boot we can forgo the serialization. | |
9242 | */ | |
713a2e21 | 9243 | set_cpus_allowed_common(idle, &ac); |
de9b8f5d | 9244 | #endif |
6506cf6c PZ |
9245 | /* |
9246 | * We're having a chicken and egg problem, even though we are | |
d1ccc66d | 9247 | * holding rq->lock, the CPU isn't yet set to this CPU so the |
6506cf6c PZ |
9248 | * lockdep check in task_group() will fail. |
9249 | * | |
9250 | * Similar case to sched_fork(). / Alternatively we could | |
9251 | * use task_rq_lock() here and obtain the other rq->lock. | |
9252 | * | |
9253 | * Silence PROVE_RCU | |
9254 | */ | |
9255 | rcu_read_lock(); | |
dd41f596 | 9256 | __set_task_cpu(idle, cpu); |
6506cf6c | 9257 | rcu_read_unlock(); |
1da177e4 | 9258 | |
5311a98f EB |
9259 | rq->idle = idle; |
9260 | rcu_assign_pointer(rq->curr, idle); | |
da0c1e65 | 9261 | idle->on_rq = TASK_ON_RQ_QUEUED; |
de9b8f5d | 9262 | #ifdef CONFIG_SMP |
3ca7a440 | 9263 | idle->on_cpu = 1; |
4866cde0 | 9264 | #endif |
5cb9eaa3 | 9265 | raw_spin_rq_unlock(rq); |
25834c73 | 9266 | raw_spin_unlock_irqrestore(&idle->pi_lock, flags); |
1da177e4 LT |
9267 | |
9268 | /* Set the preempt count _outside_ the spinlocks! */ | |
01028747 | 9269 | init_idle_preempt_count(idle, cpu); |
55cd5340 | 9270 | |
dd41f596 IM |
9271 | /* |
9272 | * The idle tasks have their own, simple scheduling class: | |
9273 | */ | |
9274 | idle->sched_class = &idle_sched_class; | |
868baf07 | 9275 | ftrace_graph_init_idle_task(idle, cpu); |
45eacc69 | 9276 | vtime_init_idle(idle, cpu); |
de9b8f5d | 9277 | #ifdef CONFIG_SMP |
f1c6f1a7 CE |
9278 | sprintf(idle->comm, "%s/%d", INIT_TASK_COMM, cpu); |
9279 | #endif | |
19978ca6 IM |
9280 | } |
9281 | ||
e1d4eeec NP |
9282 | #ifdef CONFIG_SMP |
9283 | ||
f82f8042 JL |
9284 | int cpuset_cpumask_can_shrink(const struct cpumask *cur, |
9285 | const struct cpumask *trial) | |
9286 | { | |
06a76fe0 | 9287 | int ret = 1; |
f82f8042 | 9288 | |
1087ad4e | 9289 | if (cpumask_empty(cur)) |
bb2bc55a MG |
9290 | return ret; |
9291 | ||
06a76fe0 | 9292 | ret = dl_cpuset_cpumask_can_shrink(cur, trial); |
f82f8042 JL |
9293 | |
9294 | return ret; | |
9295 | } | |
9296 | ||
2ef269ef | 9297 | int task_can_attach(struct task_struct *p) |
7f51412a JL |
9298 | { |
9299 | int ret = 0; | |
9300 | ||
9301 | /* | |
9302 | * Kthreads which disallow setaffinity shouldn't be moved | |
d1ccc66d | 9303 | * to a new cpuset; we don't want to change their CPU |
7f51412a JL |
9304 | * affinity and isolating such threads by their set of |
9305 | * allowed nodes is unnecessary. Thus, cpusets are not | |
9306 | * applicable for such threads. This prevents checking for | |
9307 | * success of set_cpus_allowed_ptr() on all attached tasks | |
3bd37062 | 9308 | * before cpus_mask may be changed. |
7f51412a | 9309 | */ |
2ef269ef | 9310 | if (p->flags & PF_NO_SETAFFINITY) |
7f51412a | 9311 | ret = -EINVAL; |
772b6539 | 9312 | |
7f51412a JL |
9313 | return ret; |
9314 | } | |
9315 | ||
f2cb1360 | 9316 | bool sched_smp_initialized __read_mostly; |
e26fbffd | 9317 | |
e6628d5b MG |
9318 | #ifdef CONFIG_NUMA_BALANCING |
9319 | /* Migrate current task p to target_cpu */ | |
9320 | int migrate_task_to(struct task_struct *p, int target_cpu) | |
9321 | { | |
9322 | struct migration_arg arg = { p, target_cpu }; | |
9323 | int curr_cpu = task_cpu(p); | |
9324 | ||
9325 | if (curr_cpu == target_cpu) | |
9326 | return 0; | |
9327 | ||
3bd37062 | 9328 | if (!cpumask_test_cpu(target_cpu, p->cpus_ptr)) |
e6628d5b MG |
9329 | return -EINVAL; |
9330 | ||
9331 | /* TODO: This is not properly updating schedstats */ | |
9332 | ||
286549dc | 9333 | trace_sched_move_numa(p, curr_cpu, target_cpu); |
e6628d5b MG |
9334 | return stop_one_cpu(curr_cpu, migration_cpu_stop, &arg); |
9335 | } | |
0ec8aa00 PZ |
9336 | |
9337 | /* | |
9338 | * Requeue a task on a given node and accurately track the number of NUMA | |
9339 | * tasks on the runqueues | |
9340 | */ | |
9341 | void sched_setnuma(struct task_struct *p, int nid) | |
9342 | { | |
da0c1e65 | 9343 | bool queued, running; |
eb580751 PZ |
9344 | struct rq_flags rf; |
9345 | struct rq *rq; | |
0ec8aa00 | 9346 | |
eb580751 | 9347 | rq = task_rq_lock(p, &rf); |
da0c1e65 | 9348 | queued = task_on_rq_queued(p); |
0ec8aa00 PZ |
9349 | running = task_current(rq, p); |
9350 | ||
da0c1e65 | 9351 | if (queued) |
1de64443 | 9352 | dequeue_task(rq, p, DEQUEUE_SAVE); |
0ec8aa00 | 9353 | if (running) |
f3cd1c4e | 9354 | put_prev_task(rq, p); |
0ec8aa00 PZ |
9355 | |
9356 | p->numa_preferred_nid = nid; | |
0ec8aa00 | 9357 | |
da0c1e65 | 9358 | if (queued) |
7134b3e9 | 9359 | enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK); |
a399d233 | 9360 | if (running) |
03b7fad1 | 9361 | set_next_task(rq, p); |
eb580751 | 9362 | task_rq_unlock(rq, p, &rf); |
0ec8aa00 | 9363 | } |
5cc389bc | 9364 | #endif /* CONFIG_NUMA_BALANCING */ |
f7b4cddc | 9365 | |
1da177e4 | 9366 | #ifdef CONFIG_HOTPLUG_CPU |
054b9108 | 9367 | /* |
d1ccc66d | 9368 | * Ensure that the idle task is using init_mm right before its CPU goes |
48c5ccae | 9369 | * offline. |
054b9108 | 9370 | */ |
48c5ccae | 9371 | void idle_task_exit(void) |
1da177e4 | 9372 | { |
48c5ccae | 9373 | struct mm_struct *mm = current->active_mm; |
e76bd8d9 | 9374 | |
48c5ccae | 9375 | BUG_ON(cpu_online(smp_processor_id())); |
bf2c59fc | 9376 | BUG_ON(current != this_rq()->idle); |
e76bd8d9 | 9377 | |
a53efe5f | 9378 | if (mm != &init_mm) { |
252d2a41 | 9379 | switch_mm(mm, &init_mm, current); |
a53efe5f MS |
9380 | finish_arch_post_lock_switch(); |
9381 | } | |
bf2c59fc PZ |
9382 | |
9383 | /* finish_cpu(), as ran on the BP, will clean up the active_mm state */ | |
1da177e4 LT |
9384 | } |
9385 | ||
2558aacf | 9386 | static int __balance_push_cpu_stop(void *arg) |
1da177e4 | 9387 | { |
2558aacf PZ |
9388 | struct task_struct *p = arg; |
9389 | struct rq *rq = this_rq(); | |
9390 | struct rq_flags rf; | |
9391 | int cpu; | |
1da177e4 | 9392 | |
2558aacf PZ |
9393 | raw_spin_lock_irq(&p->pi_lock); |
9394 | rq_lock(rq, &rf); | |
3f1d2a31 | 9395 | |
2558aacf PZ |
9396 | update_rq_clock(rq); |
9397 | ||
9398 | if (task_rq(p) == rq && task_on_rq_queued(p)) { | |
9399 | cpu = select_fallback_rq(rq->cpu, p); | |
9400 | rq = __migrate_task(rq, &rf, p, cpu); | |
10e7071b | 9401 | } |
3f1d2a31 | 9402 | |
2558aacf PZ |
9403 | rq_unlock(rq, &rf); |
9404 | raw_spin_unlock_irq(&p->pi_lock); | |
9405 | ||
9406 | put_task_struct(p); | |
9407 | ||
9408 | return 0; | |
10e7071b | 9409 | } |
3f1d2a31 | 9410 | |
2558aacf PZ |
9411 | static DEFINE_PER_CPU(struct cpu_stop_work, push_work); |
9412 | ||
48f24c4d | 9413 | /* |
2558aacf | 9414 | * Ensure we only run per-cpu kthreads once the CPU goes !active. |
b5c44773 PZ |
9415 | * |
9416 | * This is enabled below SCHED_AP_ACTIVE; when !cpu_active(), but only | |
9417 | * effective when the hotplug motion is down. | |
1da177e4 | 9418 | */ |
2558aacf | 9419 | static void balance_push(struct rq *rq) |
1da177e4 | 9420 | { |
2558aacf PZ |
9421 | struct task_struct *push_task = rq->curr; |
9422 | ||
5cb9eaa3 | 9423 | lockdep_assert_rq_held(rq); |
b5c44773 | 9424 | |
ae792702 PZ |
9425 | /* |
9426 | * Ensure the thing is persistent until balance_push_set(.on = false); | |
9427 | */ | |
9428 | rq->balance_callback = &balance_push_callback; | |
1da177e4 | 9429 | |
b5c44773 | 9430 | /* |
868ad33b TG |
9431 | * Only active while going offline and when invoked on the outgoing |
9432 | * CPU. | |
b5c44773 | 9433 | */ |
868ad33b | 9434 | if (!cpu_dying(rq->cpu) || rq != this_rq()) |
b5c44773 PZ |
9435 | return; |
9436 | ||
1da177e4 | 9437 | /* |
2558aacf PZ |
9438 | * Both the cpu-hotplug and stop task are in this case and are |
9439 | * required to complete the hotplug process. | |
1da177e4 | 9440 | */ |
00b89fe0 | 9441 | if (kthread_is_per_cpu(push_task) || |
5ba2ffba PZ |
9442 | is_migration_disabled(push_task)) { |
9443 | ||
f2469a1f TG |
9444 | /* |
9445 | * If this is the idle task on the outgoing CPU try to wake | |
9446 | * up the hotplug control thread which might wait for the | |
9447 | * last task to vanish. The rcuwait_active() check is | |
9448 | * accurate here because the waiter is pinned on this CPU | |
9449 | * and can't obviously be running in parallel. | |
3015ef4b TG |
9450 | * |
9451 | * On RT kernels this also has to check whether there are | |
9452 | * pinned and scheduled out tasks on the runqueue. They | |
9453 | * need to leave the migrate disabled section first. | |
f2469a1f | 9454 | */ |
3015ef4b TG |
9455 | if (!rq->nr_running && !rq_has_pinned_tasks(rq) && |
9456 | rcuwait_active(&rq->hotplug_wait)) { | |
5cb9eaa3 | 9457 | raw_spin_rq_unlock(rq); |
f2469a1f | 9458 | rcuwait_wake_up(&rq->hotplug_wait); |
5cb9eaa3 | 9459 | raw_spin_rq_lock(rq); |
f2469a1f | 9460 | } |
2558aacf | 9461 | return; |
f2469a1f | 9462 | } |
48f24c4d | 9463 | |
2558aacf | 9464 | get_task_struct(push_task); |
77bd3970 | 9465 | /* |
2558aacf PZ |
9466 | * Temporarily drop rq->lock such that we can wake-up the stop task. |
9467 | * Both preemption and IRQs are still disabled. | |
77bd3970 | 9468 | */ |
5cb9eaa3 | 9469 | raw_spin_rq_unlock(rq); |
2558aacf PZ |
9470 | stop_one_cpu_nowait(rq->cpu, __balance_push_cpu_stop, push_task, |
9471 | this_cpu_ptr(&push_work)); | |
9472 | /* | |
9473 | * At this point need_resched() is true and we'll take the loop in | |
9474 | * schedule(). The next pick is obviously going to be the stop task | |
5ba2ffba | 9475 | * which kthread_is_per_cpu() and will push this task away. |
2558aacf | 9476 | */ |
5cb9eaa3 | 9477 | raw_spin_rq_lock(rq); |
2558aacf | 9478 | } |
77bd3970 | 9479 | |
2558aacf PZ |
9480 | static void balance_push_set(int cpu, bool on) |
9481 | { | |
9482 | struct rq *rq = cpu_rq(cpu); | |
9483 | struct rq_flags rf; | |
48c5ccae | 9484 | |
2558aacf | 9485 | rq_lock_irqsave(rq, &rf); |
22f667c9 PZ |
9486 | if (on) { |
9487 | WARN_ON_ONCE(rq->balance_callback); | |
ae792702 | 9488 | rq->balance_callback = &balance_push_callback; |
22f667c9 | 9489 | } else if (rq->balance_callback == &balance_push_callback) { |
ae792702 | 9490 | rq->balance_callback = NULL; |
22f667c9 | 9491 | } |
2558aacf PZ |
9492 | rq_unlock_irqrestore(rq, &rf); |
9493 | } | |
e692ab53 | 9494 | |
f2469a1f TG |
9495 | /* |
9496 | * Invoked from a CPUs hotplug control thread after the CPU has been marked | |
9497 | * inactive. All tasks which are not per CPU kernel threads are either | |
9498 | * pushed off this CPU now via balance_push() or placed on a different CPU | |
9499 | * during wakeup. Wait until the CPU is quiescent. | |
9500 | */ | |
9501 | static void balance_hotplug_wait(void) | |
9502 | { | |
9503 | struct rq *rq = this_rq(); | |
5473e0cc | 9504 | |
3015ef4b TG |
9505 | rcuwait_wait_event(&rq->hotplug_wait, |
9506 | rq->nr_running == 1 && !rq_has_pinned_tasks(rq), | |
f2469a1f TG |
9507 | TASK_UNINTERRUPTIBLE); |
9508 | } | |
5473e0cc | 9509 | |
2558aacf | 9510 | #else |
dce48a84 | 9511 | |
2558aacf PZ |
9512 | static inline void balance_push(struct rq *rq) |
9513 | { | |
dce48a84 | 9514 | } |
dce48a84 | 9515 | |
2558aacf PZ |
9516 | static inline void balance_push_set(int cpu, bool on) |
9517 | { | |
9518 | } | |
9519 | ||
f2469a1f TG |
9520 | static inline void balance_hotplug_wait(void) |
9521 | { | |
dce48a84 | 9522 | } |
f2469a1f | 9523 | |
1da177e4 LT |
9524 | #endif /* CONFIG_HOTPLUG_CPU */ |
9525 | ||
f2cb1360 | 9526 | void set_rq_online(struct rq *rq) |
1f11eb6a GH |
9527 | { |
9528 | if (!rq->online) { | |
9529 | const struct sched_class *class; | |
9530 | ||
c6c4927b | 9531 | cpumask_set_cpu(rq->cpu, rq->rd->online); |
1f11eb6a GH |
9532 | rq->online = 1; |
9533 | ||
9534 | for_each_class(class) { | |
9535 | if (class->rq_online) | |
9536 | class->rq_online(rq); | |
9537 | } | |
9538 | } | |
9539 | } | |
9540 | ||
f2cb1360 | 9541 | void set_rq_offline(struct rq *rq) |
1f11eb6a GH |
9542 | { |
9543 | if (rq->online) { | |
9544 | const struct sched_class *class; | |
9545 | ||
9546 | for_each_class(class) { | |
9547 | if (class->rq_offline) | |
9548 | class->rq_offline(rq); | |
9549 | } | |
9550 | ||
c6c4927b | 9551 | cpumask_clear_cpu(rq->cpu, rq->rd->online); |
1f11eb6a GH |
9552 | rq->online = 0; |
9553 | } | |
9554 | } | |
9555 | ||
d1ccc66d IM |
9556 | /* |
9557 | * used to mark begin/end of suspend/resume: | |
9558 | */ | |
9559 | static int num_cpus_frozen; | |
d35be8ba | 9560 | |
1da177e4 | 9561 | /* |
3a101d05 TH |
9562 | * Update cpusets according to cpu_active mask. If cpusets are |
9563 | * disabled, cpuset_update_active_cpus() becomes a simple wrapper | |
9564 | * around partition_sched_domains(). | |
d35be8ba SB |
9565 | * |
9566 | * If we come here as part of a suspend/resume, don't touch cpusets because we | |
9567 | * want to restore it back to its original state upon resume anyway. | |
1da177e4 | 9568 | */ |
40190a78 | 9569 | static void cpuset_cpu_active(void) |
e761b772 | 9570 | { |
40190a78 | 9571 | if (cpuhp_tasks_frozen) { |
d35be8ba SB |
9572 | /* |
9573 | * num_cpus_frozen tracks how many CPUs are involved in suspend | |
9574 | * resume sequence. As long as this is not the last online | |
9575 | * operation in the resume sequence, just build a single sched | |
9576 | * domain, ignoring cpusets. | |
9577 | */ | |
50e76632 PZ |
9578 | partition_sched_domains(1, NULL, NULL); |
9579 | if (--num_cpus_frozen) | |
135fb3e1 | 9580 | return; |
d35be8ba SB |
9581 | /* |
9582 | * This is the last CPU online operation. So fall through and | |
9583 | * restore the original sched domains by considering the | |
9584 | * cpuset configurations. | |
9585 | */ | |
50e76632 | 9586 | cpuset_force_rebuild(); |
3a101d05 | 9587 | } |
30e03acd | 9588 | cpuset_update_active_cpus(); |
3a101d05 | 9589 | } |
e761b772 | 9590 | |
40190a78 | 9591 | static int cpuset_cpu_inactive(unsigned int cpu) |
3a101d05 | 9592 | { |
40190a78 | 9593 | if (!cpuhp_tasks_frozen) { |
85989106 | 9594 | int ret = dl_bw_check_overflow(cpu); |
772b6539 DE |
9595 | |
9596 | if (ret) | |
9597 | return ret; | |
30e03acd | 9598 | cpuset_update_active_cpus(); |
135fb3e1 | 9599 | } else { |
d35be8ba SB |
9600 | num_cpus_frozen++; |
9601 | partition_sched_domains(1, NULL, NULL); | |
e761b772 | 9602 | } |
135fb3e1 | 9603 | return 0; |
e761b772 | 9604 | } |
e761b772 | 9605 | |
40190a78 | 9606 | int sched_cpu_activate(unsigned int cpu) |
135fb3e1 | 9607 | { |
7d976699 | 9608 | struct rq *rq = cpu_rq(cpu); |
8a8c69c3 | 9609 | struct rq_flags rf; |
7d976699 | 9610 | |
22f667c9 | 9611 | /* |
b5c44773 PZ |
9612 | * Clear the balance_push callback and prepare to schedule |
9613 | * regular tasks. | |
22f667c9 | 9614 | */ |
2558aacf PZ |
9615 | balance_push_set(cpu, false); |
9616 | ||
ba2591a5 PZ |
9617 | #ifdef CONFIG_SCHED_SMT |
9618 | /* | |
c5511d03 | 9619 | * When going up, increment the number of cores with SMT present. |
ba2591a5 | 9620 | */ |
c5511d03 PZI |
9621 | if (cpumask_weight(cpu_smt_mask(cpu)) == 2) |
9622 | static_branch_inc_cpuslocked(&sched_smt_present); | |
ba2591a5 | 9623 | #endif |
40190a78 | 9624 | set_cpu_active(cpu, true); |
135fb3e1 | 9625 | |
40190a78 | 9626 | if (sched_smp_initialized) { |
0fb3978b | 9627 | sched_update_numa(cpu, true); |
135fb3e1 | 9628 | sched_domains_numa_masks_set(cpu); |
40190a78 | 9629 | cpuset_cpu_active(); |
e761b772 | 9630 | } |
7d976699 TG |
9631 | |
9632 | /* | |
9633 | * Put the rq online, if not already. This happens: | |
9634 | * | |
9635 | * 1) In the early boot process, because we build the real domains | |
d1ccc66d | 9636 | * after all CPUs have been brought up. |
7d976699 TG |
9637 | * |
9638 | * 2) At runtime, if cpuset_cpu_active() fails to rebuild the | |
9639 | * domains. | |
9640 | */ | |
8a8c69c3 | 9641 | rq_lock_irqsave(rq, &rf); |
7d976699 TG |
9642 | if (rq->rd) { |
9643 | BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); | |
9644 | set_rq_online(rq); | |
9645 | } | |
8a8c69c3 | 9646 | rq_unlock_irqrestore(rq, &rf); |
7d976699 | 9647 | |
40190a78 | 9648 | return 0; |
135fb3e1 TG |
9649 | } |
9650 | ||
40190a78 | 9651 | int sched_cpu_deactivate(unsigned int cpu) |
135fb3e1 | 9652 | { |
120455c5 PZ |
9653 | struct rq *rq = cpu_rq(cpu); |
9654 | struct rq_flags rf; | |
135fb3e1 TG |
9655 | int ret; |
9656 | ||
e0b257c3 AMB |
9657 | /* |
9658 | * Remove CPU from nohz.idle_cpus_mask to prevent participating in | |
9659 | * load balancing when not active | |
9660 | */ | |
9661 | nohz_balance_exit_idle(rq); | |
9662 | ||
40190a78 | 9663 | set_cpu_active(cpu, false); |
741ba80f PZ |
9664 | |
9665 | /* | |
9666 | * From this point forward, this CPU will refuse to run any task that | |
9667 | * is not: migrate_disable() or KTHREAD_IS_PER_CPU, and will actively | |
9668 | * push those tasks away until this gets cleared, see | |
9669 | * sched_cpu_dying(). | |
9670 | */ | |
975707f2 PZ |
9671 | balance_push_set(cpu, true); |
9672 | ||
b2454caa | 9673 | /* |
975707f2 PZ |
9674 | * We've cleared cpu_active_mask / set balance_push, wait for all |
9675 | * preempt-disabled and RCU users of this state to go away such that | |
9676 | * all new such users will observe it. | |
b2454caa | 9677 | * |
5ba2ffba PZ |
9678 | * Specifically, we rely on ttwu to no longer target this CPU, see |
9679 | * ttwu_queue_cond() and is_cpu_allowed(). | |
9680 | * | |
b2454caa PZ |
9681 | * Do sync before park smpboot threads to take care the rcu boost case. |
9682 | */ | |
309ba859 | 9683 | synchronize_rcu(); |
40190a78 | 9684 | |
120455c5 PZ |
9685 | rq_lock_irqsave(rq, &rf); |
9686 | if (rq->rd) { | |
9687 | update_rq_clock(rq); | |
9688 | BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); | |
9689 | set_rq_offline(rq); | |
9690 | } | |
9691 | rq_unlock_irqrestore(rq, &rf); | |
9692 | ||
c5511d03 PZI |
9693 | #ifdef CONFIG_SCHED_SMT |
9694 | /* | |
9695 | * When going down, decrement the number of cores with SMT present. | |
9696 | */ | |
9697 | if (cpumask_weight(cpu_smt_mask(cpu)) == 2) | |
9698 | static_branch_dec_cpuslocked(&sched_smt_present); | |
3c474b32 PZ |
9699 | |
9700 | sched_core_cpu_deactivate(cpu); | |
c5511d03 PZI |
9701 | #endif |
9702 | ||
40190a78 TG |
9703 | if (!sched_smp_initialized) |
9704 | return 0; | |
9705 | ||
0fb3978b | 9706 | sched_update_numa(cpu, false); |
40190a78 TG |
9707 | ret = cpuset_cpu_inactive(cpu); |
9708 | if (ret) { | |
2558aacf | 9709 | balance_push_set(cpu, false); |
40190a78 | 9710 | set_cpu_active(cpu, true); |
0fb3978b | 9711 | sched_update_numa(cpu, true); |
40190a78 | 9712 | return ret; |
135fb3e1 | 9713 | } |
40190a78 TG |
9714 | sched_domains_numa_masks_clear(cpu); |
9715 | return 0; | |
135fb3e1 TG |
9716 | } |
9717 | ||
94baf7a5 TG |
9718 | static void sched_rq_cpu_starting(unsigned int cpu) |
9719 | { | |
9720 | struct rq *rq = cpu_rq(cpu); | |
9721 | ||
9722 | rq->calc_load_update = calc_load_update; | |
94baf7a5 TG |
9723 | update_max_interval(); |
9724 | } | |
9725 | ||
135fb3e1 TG |
9726 | int sched_cpu_starting(unsigned int cpu) |
9727 | { | |
9edeaea1 | 9728 | sched_core_cpu_starting(cpu); |
94baf7a5 | 9729 | sched_rq_cpu_starting(cpu); |
d84b3131 | 9730 | sched_tick_start(cpu); |
135fb3e1 | 9731 | return 0; |
e761b772 | 9732 | } |
e761b772 | 9733 | |
f2785ddb | 9734 | #ifdef CONFIG_HOTPLUG_CPU |
1cf12e08 TG |
9735 | |
9736 | /* | |
9737 | * Invoked immediately before the stopper thread is invoked to bring the | |
9738 | * CPU down completely. At this point all per CPU kthreads except the | |
9739 | * hotplug thread (current) and the stopper thread (inactive) have been | |
9740 | * either parked or have been unbound from the outgoing CPU. Ensure that | |
9741 | * any of those which might be on the way out are gone. | |
9742 | * | |
9743 | * If after this point a bound task is being woken on this CPU then the | |
9744 | * responsible hotplug callback has failed to do it's job. | |
9745 | * sched_cpu_dying() will catch it with the appropriate fireworks. | |
9746 | */ | |
9747 | int sched_cpu_wait_empty(unsigned int cpu) | |
9748 | { | |
9749 | balance_hotplug_wait(); | |
9750 | return 0; | |
9751 | } | |
9752 | ||
9753 | /* | |
9754 | * Since this CPU is going 'away' for a while, fold any nr_active delta we | |
9755 | * might have. Called from the CPU stopper task after ensuring that the | |
9756 | * stopper is the last running task on the CPU, so nr_active count is | |
9757 | * stable. We need to take the teardown thread which is calling this into | |
9758 | * account, so we hand in adjust = 1 to the load calculation. | |
9759 | * | |
9760 | * Also see the comment "Global load-average calculations". | |
9761 | */ | |
9762 | static void calc_load_migrate(struct rq *rq) | |
9763 | { | |
9764 | long delta = calc_load_fold_active(rq, 1); | |
9765 | ||
9766 | if (delta) | |
9767 | atomic_long_add(delta, &calc_load_tasks); | |
9768 | } | |
9769 | ||
36c6e17b VS |
9770 | static void dump_rq_tasks(struct rq *rq, const char *loglvl) |
9771 | { | |
9772 | struct task_struct *g, *p; | |
9773 | int cpu = cpu_of(rq); | |
9774 | ||
5cb9eaa3 | 9775 | lockdep_assert_rq_held(rq); |
36c6e17b VS |
9776 | |
9777 | printk("%sCPU%d enqueued tasks (%u total):\n", loglvl, cpu, rq->nr_running); | |
9778 | for_each_process_thread(g, p) { | |
9779 | if (task_cpu(p) != cpu) | |
9780 | continue; | |
9781 | ||
9782 | if (!task_on_rq_queued(p)) | |
9783 | continue; | |
9784 | ||
9785 | printk("%s\tpid: %d, name: %s\n", loglvl, p->pid, p->comm); | |
9786 | } | |
9787 | } | |
9788 | ||
f2785ddb TG |
9789 | int sched_cpu_dying(unsigned int cpu) |
9790 | { | |
9791 | struct rq *rq = cpu_rq(cpu); | |
8a8c69c3 | 9792 | struct rq_flags rf; |
f2785ddb TG |
9793 | |
9794 | /* Handle pending wakeups and then migrate everything off */ | |
d84b3131 | 9795 | sched_tick_stop(cpu); |
8a8c69c3 PZ |
9796 | |
9797 | rq_lock_irqsave(rq, &rf); | |
36c6e17b VS |
9798 | if (rq->nr_running != 1 || rq_has_pinned_tasks(rq)) { |
9799 | WARN(true, "Dying CPU not properly vacated!"); | |
9800 | dump_rq_tasks(rq, KERN_WARNING); | |
9801 | } | |
8a8c69c3 PZ |
9802 | rq_unlock_irqrestore(rq, &rf); |
9803 | ||
f2785ddb TG |
9804 | calc_load_migrate(rq); |
9805 | update_max_interval(); | |
e5ef27d0 | 9806 | hrtick_clear(rq); |
3c474b32 | 9807 | sched_core_cpu_dying(cpu); |
f2785ddb TG |
9808 | return 0; |
9809 | } | |
9810 | #endif | |
9811 | ||
1da177e4 LT |
9812 | void __init sched_init_smp(void) |
9813 | { | |
0fb3978b | 9814 | sched_init_numa(NUMA_NO_NODE); |
cb83b629 | 9815 | |
6acce3ef PZ |
9816 | /* |
9817 | * There's no userspace yet to cause hotplug operations; hence all the | |
d1ccc66d | 9818 | * CPU masks are stable and all blatant races in the below code cannot |
b5a4e2bb | 9819 | * happen. |
6acce3ef | 9820 | */ |
712555ee | 9821 | mutex_lock(&sched_domains_mutex); |
8d5dc512 | 9822 | sched_init_domains(cpu_active_mask); |
712555ee | 9823 | mutex_unlock(&sched_domains_mutex); |
e761b772 | 9824 | |
5c1e1767 | 9825 | /* Move init over to a non-isolated CPU */ |
04d4e665 | 9826 | if (set_cpus_allowed_ptr(current, housekeeping_cpumask(HK_TYPE_DOMAIN)) < 0) |
5c1e1767 | 9827 | BUG(); |
15faafc6 | 9828 | current->flags &= ~PF_NO_SETAFFINITY; |
19978ca6 | 9829 | sched_init_granularity(); |
4212823f | 9830 | |
0e3900e6 | 9831 | init_sched_rt_class(); |
1baca4ce | 9832 | init_sched_dl_class(); |
1b568f0a | 9833 | |
e26fbffd | 9834 | sched_smp_initialized = true; |
1da177e4 | 9835 | } |
e26fbffd TG |
9836 | |
9837 | static int __init migration_init(void) | |
9838 | { | |
77a5352b | 9839 | sched_cpu_starting(smp_processor_id()); |
e26fbffd | 9840 | return 0; |
1da177e4 | 9841 | } |
e26fbffd TG |
9842 | early_initcall(migration_init); |
9843 | ||
1da177e4 LT |
9844 | #else |
9845 | void __init sched_init_smp(void) | |
9846 | { | |
19978ca6 | 9847 | sched_init_granularity(); |
1da177e4 LT |
9848 | } |
9849 | #endif /* CONFIG_SMP */ | |
9850 | ||
9851 | int in_sched_functions(unsigned long addr) | |
9852 | { | |
1da177e4 LT |
9853 | return in_lock_functions(addr) || |
9854 | (addr >= (unsigned long)__sched_text_start | |
9855 | && addr < (unsigned long)__sched_text_end); | |
9856 | } | |
9857 | ||
029632fb | 9858 | #ifdef CONFIG_CGROUP_SCHED |
27b4b931 LZ |
9859 | /* |
9860 | * Default task group. | |
9861 | * Every task in system belongs to this group at bootup. | |
9862 | */ | |
029632fb | 9863 | struct task_group root_task_group; |
35cf4e50 | 9864 | LIST_HEAD(task_groups); |
b0367629 WL |
9865 | |
9866 | /* Cacheline aligned slab cache for task_group */ | |
9867 | static struct kmem_cache *task_group_cache __read_mostly; | |
052f1dc7 | 9868 | #endif |
6f505b16 | 9869 | |
1da177e4 LT |
9870 | void __init sched_init(void) |
9871 | { | |
a1dc0446 | 9872 | unsigned long ptr = 0; |
55627e3c | 9873 | int i; |
434d53b0 | 9874 | |
c3a340f7 | 9875 | /* Make sure the linker didn't screw up */ |
546a3fee PZ |
9876 | BUG_ON(&idle_sched_class != &fair_sched_class + 1 || |
9877 | &fair_sched_class != &rt_sched_class + 1 || | |
9878 | &rt_sched_class != &dl_sched_class + 1); | |
c3a340f7 | 9879 | #ifdef CONFIG_SMP |
546a3fee | 9880 | BUG_ON(&dl_sched_class != &stop_sched_class + 1); |
c3a340f7 SRV |
9881 | #endif |
9882 | ||
5822a454 | 9883 | wait_bit_init(); |
9dcb8b68 | 9884 | |
434d53b0 | 9885 | #ifdef CONFIG_FAIR_GROUP_SCHED |
a1dc0446 | 9886 | ptr += 2 * nr_cpu_ids * sizeof(void **); |
434d53b0 MT |
9887 | #endif |
9888 | #ifdef CONFIG_RT_GROUP_SCHED | |
a1dc0446 | 9889 | ptr += 2 * nr_cpu_ids * sizeof(void **); |
434d53b0 | 9890 | #endif |
a1dc0446 QC |
9891 | if (ptr) { |
9892 | ptr = (unsigned long)kzalloc(ptr, GFP_NOWAIT); | |
434d53b0 MT |
9893 | |
9894 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
07e06b01 | 9895 | root_task_group.se = (struct sched_entity **)ptr; |
434d53b0 MT |
9896 | ptr += nr_cpu_ids * sizeof(void **); |
9897 | ||
07e06b01 | 9898 | root_task_group.cfs_rq = (struct cfs_rq **)ptr; |
434d53b0 | 9899 | ptr += nr_cpu_ids * sizeof(void **); |
eff766a6 | 9900 | |
b1d1779e WY |
9901 | root_task_group.shares = ROOT_TASK_GROUP_LOAD; |
9902 | init_cfs_bandwidth(&root_task_group.cfs_bandwidth); | |
6d6bc0ad | 9903 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
434d53b0 | 9904 | #ifdef CONFIG_RT_GROUP_SCHED |
07e06b01 | 9905 | root_task_group.rt_se = (struct sched_rt_entity **)ptr; |
434d53b0 MT |
9906 | ptr += nr_cpu_ids * sizeof(void **); |
9907 | ||
07e06b01 | 9908 | root_task_group.rt_rq = (struct rt_rq **)ptr; |
eff766a6 PZ |
9909 | ptr += nr_cpu_ids * sizeof(void **); |
9910 | ||
6d6bc0ad | 9911 | #endif /* CONFIG_RT_GROUP_SCHED */ |
b74e6278 | 9912 | } |
dd41f596 | 9913 | |
d1ccc66d | 9914 | init_rt_bandwidth(&def_rt_bandwidth, global_rt_period(), global_rt_runtime()); |
332ac17e | 9915 | |
57d885fe GH |
9916 | #ifdef CONFIG_SMP |
9917 | init_defrootdomain(); | |
9918 | #endif | |
9919 | ||
d0b27fa7 | 9920 | #ifdef CONFIG_RT_GROUP_SCHED |
07e06b01 | 9921 | init_rt_bandwidth(&root_task_group.rt_bandwidth, |
d0b27fa7 | 9922 | global_rt_period(), global_rt_runtime()); |
6d6bc0ad | 9923 | #endif /* CONFIG_RT_GROUP_SCHED */ |
d0b27fa7 | 9924 | |
7c941438 | 9925 | #ifdef CONFIG_CGROUP_SCHED |
b0367629 WL |
9926 | task_group_cache = KMEM_CACHE(task_group, 0); |
9927 | ||
07e06b01 YZ |
9928 | list_add(&root_task_group.list, &task_groups); |
9929 | INIT_LIST_HEAD(&root_task_group.children); | |
f4d6f6c2 | 9930 | INIT_LIST_HEAD(&root_task_group.siblings); |
5091faa4 | 9931 | autogroup_init(&init_task); |
7c941438 | 9932 | #endif /* CONFIG_CGROUP_SCHED */ |
6f505b16 | 9933 | |
0a945022 | 9934 | for_each_possible_cpu(i) { |
70b97a7f | 9935 | struct rq *rq; |
1da177e4 LT |
9936 | |
9937 | rq = cpu_rq(i); | |
5cb9eaa3 | 9938 | raw_spin_lock_init(&rq->__lock); |
7897986b | 9939 | rq->nr_running = 0; |
dce48a84 TG |
9940 | rq->calc_load_active = 0; |
9941 | rq->calc_load_update = jiffies + LOAD_FREQ; | |
acb5a9ba | 9942 | init_cfs_rq(&rq->cfs); |
07c54f7a AV |
9943 | init_rt_rq(&rq->rt); |
9944 | init_dl_rq(&rq->dl); | |
dd41f596 | 9945 | #ifdef CONFIG_FAIR_GROUP_SCHED |
6f505b16 | 9946 | INIT_LIST_HEAD(&rq->leaf_cfs_rq_list); |
9c2791f9 | 9947 | rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; |
354d60c2 | 9948 | /* |
d1ccc66d | 9949 | * How much CPU bandwidth does root_task_group get? |
354d60c2 DG |
9950 | * |
9951 | * In case of task-groups formed thr' the cgroup filesystem, it | |
d1ccc66d IM |
9952 | * gets 100% of the CPU resources in the system. This overall |
9953 | * system CPU resource is divided among the tasks of | |
07e06b01 | 9954 | * root_task_group and its child task-groups in a fair manner, |
354d60c2 DG |
9955 | * based on each entity's (task or task-group's) weight |
9956 | * (se->load.weight). | |
9957 | * | |
07e06b01 | 9958 | * In other words, if root_task_group has 10 tasks of weight |
354d60c2 | 9959 | * 1024) and two child groups A0 and A1 (of weight 1024 each), |
d1ccc66d | 9960 | * then A0's share of the CPU resource is: |
354d60c2 | 9961 | * |
0d905bca | 9962 | * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33% |
354d60c2 | 9963 | * |
07e06b01 YZ |
9964 | * We achieve this by letting root_task_group's tasks sit |
9965 | * directly in rq->cfs (i.e root_task_group->se[] = NULL). | |
354d60c2 | 9966 | */ |
07e06b01 | 9967 | init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL); |
354d60c2 DG |
9968 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
9969 | ||
9970 | rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime; | |
052f1dc7 | 9971 | #ifdef CONFIG_RT_GROUP_SCHED |
07e06b01 | 9972 | init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL); |
dd41f596 | 9973 | #endif |
1da177e4 | 9974 | #ifdef CONFIG_SMP |
41c7ce9a | 9975 | rq->sd = NULL; |
57d885fe | 9976 | rq->rd = NULL; |
ca6d75e6 | 9977 | rq->cpu_capacity = rq->cpu_capacity_orig = SCHED_CAPACITY_SCALE; |
b5c44773 | 9978 | rq->balance_callback = &balance_push_callback; |
1da177e4 | 9979 | rq->active_balance = 0; |
dd41f596 | 9980 | rq->next_balance = jiffies; |
1da177e4 | 9981 | rq->push_cpu = 0; |
0a2966b4 | 9982 | rq->cpu = i; |
1f11eb6a | 9983 | rq->online = 0; |
eae0c9df MG |
9984 | rq->idle_stamp = 0; |
9985 | rq->avg_idle = 2*sysctl_sched_migration_cost; | |
94aafc3e PZ |
9986 | rq->wake_stamp = jiffies; |
9987 | rq->wake_avg_idle = rq->avg_idle; | |
9bd721c5 | 9988 | rq->max_idle_balance_cost = sysctl_sched_migration_cost; |
367456c7 PZ |
9989 | |
9990 | INIT_LIST_HEAD(&rq->cfs_tasks); | |
9991 | ||
dc938520 | 9992 | rq_attach_root(rq, &def_root_domain); |
3451d024 | 9993 | #ifdef CONFIG_NO_HZ_COMMON |
e022e0d3 | 9994 | rq->last_blocked_load_update_tick = jiffies; |
a22e47a4 | 9995 | atomic_set(&rq->nohz_flags, 0); |
90b5363a | 9996 | |
545b8c8d | 9997 | INIT_CSD(&rq->nohz_csd, nohz_csd_func, rq); |
83cd4fe2 | 9998 | #endif |
f2469a1f TG |
9999 | #ifdef CONFIG_HOTPLUG_CPU |
10000 | rcuwait_init(&rq->hotplug_wait); | |
83cd4fe2 | 10001 | #endif |
9fd81dd5 | 10002 | #endif /* CONFIG_SMP */ |
77a021be | 10003 | hrtick_rq_init(rq); |
1da177e4 | 10004 | atomic_set(&rq->nr_iowait, 0); |
9edeaea1 PZ |
10005 | |
10006 | #ifdef CONFIG_SCHED_CORE | |
3c474b32 | 10007 | rq->core = rq; |
539f6512 | 10008 | rq->core_pick = NULL; |
9edeaea1 | 10009 | rq->core_enabled = 0; |
539f6512 | 10010 | rq->core_tree = RB_ROOT; |
4feee7d1 JD |
10011 | rq->core_forceidle_count = 0; |
10012 | rq->core_forceidle_occupation = 0; | |
10013 | rq->core_forceidle_start = 0; | |
539f6512 PZ |
10014 | |
10015 | rq->core_cookie = 0UL; | |
9edeaea1 | 10016 | #endif |
da019032 | 10017 | zalloc_cpumask_var_node(&rq->scratch_mask, GFP_KERNEL, cpu_to_node(i)); |
1da177e4 LT |
10018 | } |
10019 | ||
b1e82065 | 10020 | set_load_weight(&init_task, false); |
b50f60ce | 10021 | |
1da177e4 LT |
10022 | /* |
10023 | * The boot idle thread does lazy MMU switching as well: | |
10024 | */ | |
aa464ba9 | 10025 | mmgrab_lazy_tlb(&init_mm); |
1da177e4 LT |
10026 | enter_lazy_tlb(&init_mm, current); |
10027 | ||
40966e31 EB |
10028 | /* |
10029 | * The idle task doesn't need the kthread struct to function, but it | |
10030 | * is dressed up as a per-CPU kthread and thus needs to play the part | |
10031 | * if we want to avoid special-casing it in code that deals with per-CPU | |
10032 | * kthreads. | |
10033 | */ | |
dd621ee0 | 10034 | WARN_ON(!set_kthread_struct(current)); |
40966e31 | 10035 | |
1da177e4 LT |
10036 | /* |
10037 | * Make us the idle thread. Technically, schedule() should not be | |
10038 | * called from this thread, however somewhere below it might be, | |
10039 | * but because we are the idle thread, we just pick up running again | |
10040 | * when this runqueue becomes "idle". | |
10041 | */ | |
10042 | init_idle(current, smp_processor_id()); | |
dce48a84 TG |
10043 | |
10044 | calc_load_update = jiffies + LOAD_FREQ; | |
10045 | ||
bf4d83f6 | 10046 | #ifdef CONFIG_SMP |
29d5e047 | 10047 | idle_thread_set_boot_cpu(); |
b5c44773 | 10048 | balance_push_set(smp_processor_id(), false); |
029632fb PZ |
10049 | #endif |
10050 | init_sched_fair_class(); | |
6a7b3dc3 | 10051 | |
eb414681 JW |
10052 | psi_init(); |
10053 | ||
69842cba PB |
10054 | init_uclamp(); |
10055 | ||
c597bfdd FW |
10056 | preempt_dynamic_init(); |
10057 | ||
6892b75e | 10058 | scheduler_running = 1; |
1da177e4 LT |
10059 | } |
10060 | ||
d902db1e | 10061 | #ifdef CONFIG_DEBUG_ATOMIC_SLEEP |
e4aafea2 | 10062 | |
42a38756 | 10063 | void __might_sleep(const char *file, int line) |
1da177e4 | 10064 | { |
d6c23bb3 | 10065 | unsigned int state = get_current_state(); |
8eb23b9f PZ |
10066 | /* |
10067 | * Blocking primitives will set (and therefore destroy) current->state, | |
10068 | * since we will exit with TASK_RUNNING make sure we enter with it, | |
10069 | * otherwise we will destroy state. | |
10070 | */ | |
d6c23bb3 | 10071 | WARN_ONCE(state != TASK_RUNNING && current->task_state_change, |
8eb23b9f | 10072 | "do not call blocking ops when !TASK_RUNNING; " |
d6c23bb3 | 10073 | "state=%x set at [<%p>] %pS\n", state, |
8eb23b9f | 10074 | (void *)current->task_state_change, |
00845eb9 | 10075 | (void *)current->task_state_change); |
8eb23b9f | 10076 | |
42a38756 | 10077 | __might_resched(file, line, 0); |
3427445a PZ |
10078 | } |
10079 | EXPORT_SYMBOL(__might_sleep); | |
10080 | ||
8d713b69 TG |
10081 | static void print_preempt_disable_ip(int preempt_offset, unsigned long ip) |
10082 | { | |
10083 | if (!IS_ENABLED(CONFIG_DEBUG_PREEMPT)) | |
10084 | return; | |
10085 | ||
10086 | if (preempt_count() == preempt_offset) | |
10087 | return; | |
10088 | ||
10089 | pr_err("Preemption disabled at:"); | |
10090 | print_ip_sym(KERN_ERR, ip); | |
10091 | } | |
10092 | ||
50e081b9 TG |
10093 | static inline bool resched_offsets_ok(unsigned int offsets) |
10094 | { | |
10095 | unsigned int nested = preempt_count(); | |
10096 | ||
10097 | nested += rcu_preempt_depth() << MIGHT_RESCHED_RCU_SHIFT; | |
10098 | ||
10099 | return nested == offsets; | |
10100 | } | |
10101 | ||
10102 | void __might_resched(const char *file, int line, unsigned int offsets) | |
1da177e4 | 10103 | { |
d1ccc66d IM |
10104 | /* Ratelimiting timestamp: */ |
10105 | static unsigned long prev_jiffy; | |
10106 | ||
d1c6d149 | 10107 | unsigned long preempt_disable_ip; |
1da177e4 | 10108 | |
d1ccc66d IM |
10109 | /* WARN_ON_ONCE() by default, no rate limit required: */ |
10110 | rcu_sleep_check(); | |
10111 | ||
50e081b9 | 10112 | if ((resched_offsets_ok(offsets) && !irqs_disabled() && |
312364f3 | 10113 | !is_idle_task(current) && !current->non_block_count) || |
1c3c5eab TG |
10114 | system_state == SYSTEM_BOOTING || system_state > SYSTEM_RUNNING || |
10115 | oops_in_progress) | |
aef745fc | 10116 | return; |
1c3c5eab | 10117 | |
aef745fc IM |
10118 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) |
10119 | return; | |
10120 | prev_jiffy = jiffies; | |
10121 | ||
d1ccc66d | 10122 | /* Save this before calling printk(), since that will clobber it: */ |
d1c6d149 VN |
10123 | preempt_disable_ip = get_preempt_disable_ip(current); |
10124 | ||
a45ed302 TG |
10125 | pr_err("BUG: sleeping function called from invalid context at %s:%d\n", |
10126 | file, line); | |
10127 | pr_err("in_atomic(): %d, irqs_disabled(): %d, non_block: %d, pid: %d, name: %s\n", | |
10128 | in_atomic(), irqs_disabled(), current->non_block_count, | |
10129 | current->pid, current->comm); | |
8d713b69 | 10130 | pr_err("preempt_count: %x, expected: %x\n", preempt_count(), |
50e081b9 | 10131 | offsets & MIGHT_RESCHED_PREEMPT_MASK); |
8d713b69 TG |
10132 | |
10133 | if (IS_ENABLED(CONFIG_PREEMPT_RCU)) { | |
50e081b9 TG |
10134 | pr_err("RCU nest depth: %d, expected: %u\n", |
10135 | rcu_preempt_depth(), offsets >> MIGHT_RESCHED_RCU_SHIFT); | |
8d713b69 | 10136 | } |
aef745fc | 10137 | |
a8b686b3 | 10138 | if (task_stack_end_corrupted(current)) |
a45ed302 | 10139 | pr_emerg("Thread overran stack, or stack corrupted\n"); |
a8b686b3 | 10140 | |
aef745fc IM |
10141 | debug_show_held_locks(current); |
10142 | if (irqs_disabled()) | |
10143 | print_irqtrace_events(current); | |
8d713b69 | 10144 | |
50e081b9 TG |
10145 | print_preempt_disable_ip(offsets & MIGHT_RESCHED_PREEMPT_MASK, |
10146 | preempt_disable_ip); | |
8d713b69 | 10147 | |
aef745fc | 10148 | dump_stack(); |
f0b22e39 | 10149 | add_taint(TAINT_WARN, LOCKDEP_STILL_OK); |
1da177e4 | 10150 | } |
874f670e | 10151 | EXPORT_SYMBOL(__might_resched); |
568f1967 PZ |
10152 | |
10153 | void __cant_sleep(const char *file, int line, int preempt_offset) | |
10154 | { | |
10155 | static unsigned long prev_jiffy; | |
10156 | ||
10157 | if (irqs_disabled()) | |
10158 | return; | |
10159 | ||
10160 | if (!IS_ENABLED(CONFIG_PREEMPT_COUNT)) | |
10161 | return; | |
10162 | ||
10163 | if (preempt_count() > preempt_offset) | |
10164 | return; | |
10165 | ||
10166 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) | |
10167 | return; | |
10168 | prev_jiffy = jiffies; | |
10169 | ||
10170 | printk(KERN_ERR "BUG: assuming atomic context at %s:%d\n", file, line); | |
10171 | printk(KERN_ERR "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n", | |
10172 | in_atomic(), irqs_disabled(), | |
10173 | current->pid, current->comm); | |
10174 | ||
10175 | debug_show_held_locks(current); | |
10176 | dump_stack(); | |
10177 | add_taint(TAINT_WARN, LOCKDEP_STILL_OK); | |
10178 | } | |
10179 | EXPORT_SYMBOL_GPL(__cant_sleep); | |
74d862b6 TG |
10180 | |
10181 | #ifdef CONFIG_SMP | |
10182 | void __cant_migrate(const char *file, int line) | |
10183 | { | |
10184 | static unsigned long prev_jiffy; | |
10185 | ||
10186 | if (irqs_disabled()) | |
10187 | return; | |
10188 | ||
10189 | if (is_migration_disabled(current)) | |
10190 | return; | |
10191 | ||
10192 | if (!IS_ENABLED(CONFIG_PREEMPT_COUNT)) | |
10193 | return; | |
10194 | ||
10195 | if (preempt_count() > 0) | |
10196 | return; | |
10197 | ||
10198 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) | |
10199 | return; | |
10200 | prev_jiffy = jiffies; | |
10201 | ||
10202 | pr_err("BUG: assuming non migratable context at %s:%d\n", file, line); | |
10203 | pr_err("in_atomic(): %d, irqs_disabled(): %d, migration_disabled() %u pid: %d, name: %s\n", | |
10204 | in_atomic(), irqs_disabled(), is_migration_disabled(current), | |
10205 | current->pid, current->comm); | |
10206 | ||
10207 | debug_show_held_locks(current); | |
10208 | dump_stack(); | |
10209 | add_taint(TAINT_WARN, LOCKDEP_STILL_OK); | |
10210 | } | |
10211 | EXPORT_SYMBOL_GPL(__cant_migrate); | |
10212 | #endif | |
1da177e4 LT |
10213 | #endif |
10214 | ||
10215 | #ifdef CONFIG_MAGIC_SYSRQ | |
dbc7f069 | 10216 | void normalize_rt_tasks(void) |
3a5e4dc1 | 10217 | { |
dbc7f069 | 10218 | struct task_struct *g, *p; |
d50dde5a DF |
10219 | struct sched_attr attr = { |
10220 | .sched_policy = SCHED_NORMAL, | |
10221 | }; | |
1da177e4 | 10222 | |
3472eaa1 | 10223 | read_lock(&tasklist_lock); |
5d07f420 | 10224 | for_each_process_thread(g, p) { |
178be793 IM |
10225 | /* |
10226 | * Only normalize user tasks: | |
10227 | */ | |
3472eaa1 | 10228 | if (p->flags & PF_KTHREAD) |
178be793 IM |
10229 | continue; |
10230 | ||
4fa8d299 | 10231 | p->se.exec_start = 0; |
ceeadb83 YS |
10232 | schedstat_set(p->stats.wait_start, 0); |
10233 | schedstat_set(p->stats.sleep_start, 0); | |
10234 | schedstat_set(p->stats.block_start, 0); | |
dd41f596 | 10235 | |
aab03e05 | 10236 | if (!dl_task(p) && !rt_task(p)) { |
dd41f596 IM |
10237 | /* |
10238 | * Renice negative nice level userspace | |
10239 | * tasks back to 0: | |
10240 | */ | |
3472eaa1 | 10241 | if (task_nice(p) < 0) |
dd41f596 | 10242 | set_user_nice(p, 0); |
1da177e4 | 10243 | continue; |
dd41f596 | 10244 | } |
1da177e4 | 10245 | |
dbc7f069 | 10246 | __sched_setscheduler(p, &attr, false, false); |
5d07f420 | 10247 | } |
3472eaa1 | 10248 | read_unlock(&tasklist_lock); |
1da177e4 LT |
10249 | } |
10250 | ||
10251 | #endif /* CONFIG_MAGIC_SYSRQ */ | |
1df5c10a | 10252 | |
67fc4e0c | 10253 | #if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) |
1df5c10a | 10254 | /* |
67fc4e0c | 10255 | * These functions are only useful for the IA64 MCA handling, or kdb. |
1df5c10a LT |
10256 | * |
10257 | * They can only be called when the whole system has been | |
10258 | * stopped - every CPU needs to be quiescent, and no scheduling | |
10259 | * activity can take place. Using them for anything else would | |
10260 | * be a serious bug, and as a result, they aren't even visible | |
10261 | * under any other configuration. | |
10262 | */ | |
10263 | ||
10264 | /** | |
d1ccc66d | 10265 | * curr_task - return the current task for a given CPU. |
1df5c10a LT |
10266 | * @cpu: the processor in question. |
10267 | * | |
10268 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! | |
e69f6186 YB |
10269 | * |
10270 | * Return: The current task for @cpu. | |
1df5c10a | 10271 | */ |
36c8b586 | 10272 | struct task_struct *curr_task(int cpu) |
1df5c10a LT |
10273 | { |
10274 | return cpu_curr(cpu); | |
10275 | } | |
10276 | ||
67fc4e0c JW |
10277 | #endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */ |
10278 | ||
10279 | #ifdef CONFIG_IA64 | |
1df5c10a | 10280 | /** |
5feeb783 | 10281 | * ia64_set_curr_task - set the current task for a given CPU. |
1df5c10a LT |
10282 | * @cpu: the processor in question. |
10283 | * @p: the task pointer to set. | |
10284 | * | |
10285 | * Description: This function must only be used when non-maskable interrupts | |
41a2d6cf | 10286 | * are serviced on a separate stack. It allows the architecture to switch the |
d1ccc66d | 10287 | * notion of the current task on a CPU in a non-blocking manner. This function |
1df5c10a LT |
10288 | * must be called with all CPU's synchronized, and interrupts disabled, the |
10289 | * and caller must save the original value of the current task (see | |
10290 | * curr_task() above) and restore that value before reenabling interrupts and | |
10291 | * re-starting the system. | |
10292 | * | |
10293 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! | |
10294 | */ | |
a458ae2e | 10295 | void ia64_set_curr_task(int cpu, struct task_struct *p) |
1df5c10a LT |
10296 | { |
10297 | cpu_curr(cpu) = p; | |
10298 | } | |
10299 | ||
10300 | #endif | |
29f59db3 | 10301 | |
7c941438 | 10302 | #ifdef CONFIG_CGROUP_SCHED |
029632fb PZ |
10303 | /* task_group_lock serializes the addition/removal of task groups */ |
10304 | static DEFINE_SPINLOCK(task_group_lock); | |
10305 | ||
2480c093 PB |
10306 | static inline void alloc_uclamp_sched_group(struct task_group *tg, |
10307 | struct task_group *parent) | |
10308 | { | |
10309 | #ifdef CONFIG_UCLAMP_TASK_GROUP | |
0413d7f3 | 10310 | enum uclamp_id clamp_id; |
2480c093 PB |
10311 | |
10312 | for_each_clamp_id(clamp_id) { | |
10313 | uclamp_se_set(&tg->uclamp_req[clamp_id], | |
10314 | uclamp_none(clamp_id), false); | |
0b60ba2d | 10315 | tg->uclamp[clamp_id] = parent->uclamp[clamp_id]; |
2480c093 PB |
10316 | } |
10317 | #endif | |
10318 | } | |
10319 | ||
2f5177f0 | 10320 | static void sched_free_group(struct task_group *tg) |
bccbe08a PZ |
10321 | { |
10322 | free_fair_sched_group(tg); | |
10323 | free_rt_sched_group(tg); | |
e9aa1dd1 | 10324 | autogroup_free(tg); |
b0367629 | 10325 | kmem_cache_free(task_group_cache, tg); |
bccbe08a PZ |
10326 | } |
10327 | ||
b027789e MK |
10328 | static void sched_free_group_rcu(struct rcu_head *rcu) |
10329 | { | |
10330 | sched_free_group(container_of(rcu, struct task_group, rcu)); | |
10331 | } | |
10332 | ||
10333 | static void sched_unregister_group(struct task_group *tg) | |
10334 | { | |
10335 | unregister_fair_sched_group(tg); | |
10336 | unregister_rt_sched_group(tg); | |
10337 | /* | |
10338 | * We have to wait for yet another RCU grace period to expire, as | |
10339 | * print_cfs_stats() might run concurrently. | |
10340 | */ | |
10341 | call_rcu(&tg->rcu, sched_free_group_rcu); | |
10342 | } | |
10343 | ||
bccbe08a | 10344 | /* allocate runqueue etc for a new task group */ |
ec7dc8ac | 10345 | struct task_group *sched_create_group(struct task_group *parent) |
bccbe08a PZ |
10346 | { |
10347 | struct task_group *tg; | |
bccbe08a | 10348 | |
b0367629 | 10349 | tg = kmem_cache_alloc(task_group_cache, GFP_KERNEL | __GFP_ZERO); |
bccbe08a PZ |
10350 | if (!tg) |
10351 | return ERR_PTR(-ENOMEM); | |
10352 | ||
ec7dc8ac | 10353 | if (!alloc_fair_sched_group(tg, parent)) |
bccbe08a PZ |
10354 | goto err; |
10355 | ||
ec7dc8ac | 10356 | if (!alloc_rt_sched_group(tg, parent)) |
bccbe08a PZ |
10357 | goto err; |
10358 | ||
2480c093 PB |
10359 | alloc_uclamp_sched_group(tg, parent); |
10360 | ||
ace783b9 LZ |
10361 | return tg; |
10362 | ||
10363 | err: | |
2f5177f0 | 10364 | sched_free_group(tg); |
ace783b9 LZ |
10365 | return ERR_PTR(-ENOMEM); |
10366 | } | |
10367 | ||
10368 | void sched_online_group(struct task_group *tg, struct task_group *parent) | |
10369 | { | |
10370 | unsigned long flags; | |
10371 | ||
8ed36996 | 10372 | spin_lock_irqsave(&task_group_lock, flags); |
6f505b16 | 10373 | list_add_rcu(&tg->list, &task_groups); |
f473aa5e | 10374 | |
d1ccc66d IM |
10375 | /* Root should already exist: */ |
10376 | WARN_ON(!parent); | |
f473aa5e PZ |
10377 | |
10378 | tg->parent = parent; | |
f473aa5e | 10379 | INIT_LIST_HEAD(&tg->children); |
09f2724a | 10380 | list_add_rcu(&tg->siblings, &parent->children); |
8ed36996 | 10381 | spin_unlock_irqrestore(&task_group_lock, flags); |
8663e24d PZ |
10382 | |
10383 | online_fair_sched_group(tg); | |
29f59db3 SV |
10384 | } |
10385 | ||
9b5b7751 | 10386 | /* rcu callback to free various structures associated with a task group */ |
b027789e | 10387 | static void sched_unregister_group_rcu(struct rcu_head *rhp) |
29f59db3 | 10388 | { |
d1ccc66d | 10389 | /* Now it should be safe to free those cfs_rqs: */ |
b027789e | 10390 | sched_unregister_group(container_of(rhp, struct task_group, rcu)); |
29f59db3 SV |
10391 | } |
10392 | ||
4cf86d77 | 10393 | void sched_destroy_group(struct task_group *tg) |
ace783b9 | 10394 | { |
d1ccc66d | 10395 | /* Wait for possible concurrent references to cfs_rqs complete: */ |
b027789e | 10396 | call_rcu(&tg->rcu, sched_unregister_group_rcu); |
ace783b9 LZ |
10397 | } |
10398 | ||
b027789e | 10399 | void sched_release_group(struct task_group *tg) |
29f59db3 | 10400 | { |
8ed36996 | 10401 | unsigned long flags; |
29f59db3 | 10402 | |
b027789e MK |
10403 | /* |
10404 | * Unlink first, to avoid walk_tg_tree_from() from finding us (via | |
10405 | * sched_cfs_period_timer()). | |
10406 | * | |
10407 | * For this to be effective, we have to wait for all pending users of | |
10408 | * this task group to leave their RCU critical section to ensure no new | |
10409 | * user will see our dying task group any more. Specifically ensure | |
10410 | * that tg_unthrottle_up() won't add decayed cfs_rq's to it. | |
10411 | * | |
10412 | * We therefore defer calling unregister_fair_sched_group() to | |
10413 | * sched_unregister_group() which is guarantied to get called only after the | |
10414 | * current RCU grace period has expired. | |
10415 | */ | |
3d4b47b4 | 10416 | spin_lock_irqsave(&task_group_lock, flags); |
6f505b16 | 10417 | list_del_rcu(&tg->list); |
f473aa5e | 10418 | list_del_rcu(&tg->siblings); |
8ed36996 | 10419 | spin_unlock_irqrestore(&task_group_lock, flags); |
29f59db3 SV |
10420 | } |
10421 | ||
eff6c8ce | 10422 | static struct task_group *sched_get_task_group(struct task_struct *tsk) |
29f59db3 | 10423 | { |
8323f26c | 10424 | struct task_group *tg; |
29f59db3 | 10425 | |
f7b8a47d KT |
10426 | /* |
10427 | * All callers are synchronized by task_rq_lock(); we do not use RCU | |
10428 | * which is pointless here. Thus, we pass "true" to task_css_check() | |
10429 | * to prevent lockdep warnings. | |
10430 | */ | |
10431 | tg = container_of(task_css_check(tsk, cpu_cgrp_id, true), | |
8323f26c PZ |
10432 | struct task_group, css); |
10433 | tg = autogroup_task_group(tsk, tg); | |
eff6c8ce | 10434 | |
10435 | return tg; | |
10436 | } | |
10437 | ||
10438 | static void sched_change_group(struct task_struct *tsk, struct task_group *group) | |
10439 | { | |
10440 | tsk->sched_task_group = group; | |
8323f26c | 10441 | |
810b3817 | 10442 | #ifdef CONFIG_FAIR_GROUP_SCHED |
ea86cb4b | 10443 | if (tsk->sched_class->task_change_group) |
39c42611 | 10444 | tsk->sched_class->task_change_group(tsk); |
b2b5ce02 | 10445 | else |
810b3817 | 10446 | #endif |
b2b5ce02 | 10447 | set_task_rq(tsk, task_cpu(tsk)); |
ea86cb4b VG |
10448 | } |
10449 | ||
10450 | /* | |
10451 | * Change task's runqueue when it moves between groups. | |
10452 | * | |
10453 | * The caller of this function should have put the task in its new group by | |
10454 | * now. This function just updates tsk->se.cfs_rq and tsk->se.parent to reflect | |
10455 | * its new group. | |
10456 | */ | |
10457 | void sched_move_task(struct task_struct *tsk) | |
10458 | { | |
7a57f32a PZ |
10459 | int queued, running, queue_flags = |
10460 | DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK; | |
eff6c8ce | 10461 | struct task_group *group; |
ea86cb4b VG |
10462 | struct rq_flags rf; |
10463 | struct rq *rq; | |
10464 | ||
10465 | rq = task_rq_lock(tsk, &rf); | |
eff6c8ce | 10466 | /* |
10467 | * Esp. with SCHED_AUTOGROUP enabled it is possible to get superfluous | |
10468 | * group changes. | |
10469 | */ | |
10470 | group = sched_get_task_group(tsk); | |
10471 | if (group == tsk->sched_task_group) | |
10472 | goto unlock; | |
10473 | ||
1b1d6225 | 10474 | update_rq_clock(rq); |
ea86cb4b VG |
10475 | |
10476 | running = task_current(rq, tsk); | |
10477 | queued = task_on_rq_queued(tsk); | |
10478 | ||
10479 | if (queued) | |
7a57f32a | 10480 | dequeue_task(rq, tsk, queue_flags); |
bb3bac2c | 10481 | if (running) |
ea86cb4b VG |
10482 | put_prev_task(rq, tsk); |
10483 | ||
eff6c8ce | 10484 | sched_change_group(tsk, group); |
810b3817 | 10485 | |
da0c1e65 | 10486 | if (queued) |
7a57f32a | 10487 | enqueue_task(rq, tsk, queue_flags); |
2a4b03ff | 10488 | if (running) { |
03b7fad1 | 10489 | set_next_task(rq, tsk); |
2a4b03ff VG |
10490 | /* |
10491 | * After changing group, the running task may have joined a | |
10492 | * throttled one but it's still the running task. Trigger a | |
10493 | * resched to make sure that task can still run. | |
10494 | */ | |
10495 | resched_curr(rq); | |
10496 | } | |
29f59db3 | 10497 | |
eff6c8ce | 10498 | unlock: |
eb580751 | 10499 | task_rq_unlock(rq, tsk, &rf); |
29f59db3 | 10500 | } |
68318b8e | 10501 | |
a7c6d554 | 10502 | static inline struct task_group *css_tg(struct cgroup_subsys_state *css) |
68318b8e | 10503 | { |
a7c6d554 | 10504 | return css ? container_of(css, struct task_group, css) : NULL; |
68318b8e SV |
10505 | } |
10506 | ||
eb95419b TH |
10507 | static struct cgroup_subsys_state * |
10508 | cpu_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) | |
68318b8e | 10509 | { |
eb95419b TH |
10510 | struct task_group *parent = css_tg(parent_css); |
10511 | struct task_group *tg; | |
68318b8e | 10512 | |
eb95419b | 10513 | if (!parent) { |
68318b8e | 10514 | /* This is early initialization for the top cgroup */ |
07e06b01 | 10515 | return &root_task_group.css; |
68318b8e SV |
10516 | } |
10517 | ||
ec7dc8ac | 10518 | tg = sched_create_group(parent); |
68318b8e SV |
10519 | if (IS_ERR(tg)) |
10520 | return ERR_PTR(-ENOMEM); | |
10521 | ||
68318b8e SV |
10522 | return &tg->css; |
10523 | } | |
10524 | ||
96b77745 KK |
10525 | /* Expose task group only after completing cgroup initialization */ |
10526 | static int cpu_cgroup_css_online(struct cgroup_subsys_state *css) | |
10527 | { | |
10528 | struct task_group *tg = css_tg(css); | |
10529 | struct task_group *parent = css_tg(css->parent); | |
10530 | ||
10531 | if (parent) | |
10532 | sched_online_group(tg, parent); | |
7226017a QY |
10533 | |
10534 | #ifdef CONFIG_UCLAMP_TASK_GROUP | |
10535 | /* Propagate the effective uclamp value for the new group */ | |
93b73858 QY |
10536 | mutex_lock(&uclamp_mutex); |
10537 | rcu_read_lock(); | |
7226017a | 10538 | cpu_util_update_eff(css); |
93b73858 QY |
10539 | rcu_read_unlock(); |
10540 | mutex_unlock(&uclamp_mutex); | |
7226017a QY |
10541 | #endif |
10542 | ||
96b77745 KK |
10543 | return 0; |
10544 | } | |
10545 | ||
2f5177f0 | 10546 | static void cpu_cgroup_css_released(struct cgroup_subsys_state *css) |
ace783b9 | 10547 | { |
eb95419b | 10548 | struct task_group *tg = css_tg(css); |
ace783b9 | 10549 | |
b027789e | 10550 | sched_release_group(tg); |
ace783b9 LZ |
10551 | } |
10552 | ||
eb95419b | 10553 | static void cpu_cgroup_css_free(struct cgroup_subsys_state *css) |
68318b8e | 10554 | { |
eb95419b | 10555 | struct task_group *tg = css_tg(css); |
68318b8e | 10556 | |
2f5177f0 PZ |
10557 | /* |
10558 | * Relies on the RCU grace period between css_released() and this. | |
10559 | */ | |
b027789e | 10560 | sched_unregister_group(tg); |
ace783b9 LZ |
10561 | } |
10562 | ||
df16b71c | 10563 | #ifdef CONFIG_RT_GROUP_SCHED |
1f7dd3e5 | 10564 | static int cpu_cgroup_can_attach(struct cgroup_taskset *tset) |
68318b8e | 10565 | { |
bb9d97b6 | 10566 | struct task_struct *task; |
1f7dd3e5 | 10567 | struct cgroup_subsys_state *css; |
bb9d97b6 | 10568 | |
1f7dd3e5 | 10569 | cgroup_taskset_for_each(task, css, tset) { |
eb95419b | 10570 | if (!sched_rt_can_attach(css_tg(css), task)) |
bb9d97b6 | 10571 | return -EINVAL; |
bb9d97b6 | 10572 | } |
df16b71c | 10573 | return 0; |
be367d09 | 10574 | } |
df16b71c | 10575 | #endif |
68318b8e | 10576 | |
1f7dd3e5 | 10577 | static void cpu_cgroup_attach(struct cgroup_taskset *tset) |
68318b8e | 10578 | { |
bb9d97b6 | 10579 | struct task_struct *task; |
1f7dd3e5 | 10580 | struct cgroup_subsys_state *css; |
bb9d97b6 | 10581 | |
1f7dd3e5 | 10582 | cgroup_taskset_for_each(task, css, tset) |
bb9d97b6 | 10583 | sched_move_task(task); |
68318b8e SV |
10584 | } |
10585 | ||
2480c093 | 10586 | #ifdef CONFIG_UCLAMP_TASK_GROUP |
0b60ba2d PB |
10587 | static void cpu_util_update_eff(struct cgroup_subsys_state *css) |
10588 | { | |
10589 | struct cgroup_subsys_state *top_css = css; | |
10590 | struct uclamp_se *uc_parent = NULL; | |
10591 | struct uclamp_se *uc_se = NULL; | |
10592 | unsigned int eff[UCLAMP_CNT]; | |
0413d7f3 | 10593 | enum uclamp_id clamp_id; |
0b60ba2d PB |
10594 | unsigned int clamps; |
10595 | ||
93b73858 QY |
10596 | lockdep_assert_held(&uclamp_mutex); |
10597 | SCHED_WARN_ON(!rcu_read_lock_held()); | |
10598 | ||
0b60ba2d PB |
10599 | css_for_each_descendant_pre(css, top_css) { |
10600 | uc_parent = css_tg(css)->parent | |
10601 | ? css_tg(css)->parent->uclamp : NULL; | |
10602 | ||
10603 | for_each_clamp_id(clamp_id) { | |
10604 | /* Assume effective clamps matches requested clamps */ | |
10605 | eff[clamp_id] = css_tg(css)->uclamp_req[clamp_id].value; | |
10606 | /* Cap effective clamps with parent's effective clamps */ | |
10607 | if (uc_parent && | |
10608 | eff[clamp_id] > uc_parent[clamp_id].value) { | |
10609 | eff[clamp_id] = uc_parent[clamp_id].value; | |
10610 | } | |
10611 | } | |
10612 | /* Ensure protection is always capped by limit */ | |
10613 | eff[UCLAMP_MIN] = min(eff[UCLAMP_MIN], eff[UCLAMP_MAX]); | |
10614 | ||
10615 | /* Propagate most restrictive effective clamps */ | |
10616 | clamps = 0x0; | |
10617 | uc_se = css_tg(css)->uclamp; | |
10618 | for_each_clamp_id(clamp_id) { | |
10619 | if (eff[clamp_id] == uc_se[clamp_id].value) | |
10620 | continue; | |
10621 | uc_se[clamp_id].value = eff[clamp_id]; | |
10622 | uc_se[clamp_id].bucket_id = uclamp_bucket_id(eff[clamp_id]); | |
10623 | clamps |= (0x1 << clamp_id); | |
10624 | } | |
babbe170 | 10625 | if (!clamps) { |
0b60ba2d | 10626 | css = css_rightmost_descendant(css); |
babbe170 PB |
10627 | continue; |
10628 | } | |
10629 | ||
10630 | /* Immediately update descendants RUNNABLE tasks */ | |
0213b708 | 10631 | uclamp_update_active_tasks(css); |
0b60ba2d PB |
10632 | } |
10633 | } | |
2480c093 PB |
10634 | |
10635 | /* | |
10636 | * Integer 10^N with a given N exponent by casting to integer the literal "1eN" | |
10637 | * C expression. Since there is no way to convert a macro argument (N) into a | |
10638 | * character constant, use two levels of macros. | |
10639 | */ | |
10640 | #define _POW10(exp) ((unsigned int)1e##exp) | |
10641 | #define POW10(exp) _POW10(exp) | |
10642 | ||
10643 | struct uclamp_request { | |
10644 | #define UCLAMP_PERCENT_SHIFT 2 | |
10645 | #define UCLAMP_PERCENT_SCALE (100 * POW10(UCLAMP_PERCENT_SHIFT)) | |
10646 | s64 percent; | |
10647 | u64 util; | |
10648 | int ret; | |
10649 | }; | |
10650 | ||
10651 | static inline struct uclamp_request | |
10652 | capacity_from_percent(char *buf) | |
10653 | { | |
10654 | struct uclamp_request req = { | |
10655 | .percent = UCLAMP_PERCENT_SCALE, | |
10656 | .util = SCHED_CAPACITY_SCALE, | |
10657 | .ret = 0, | |
10658 | }; | |
10659 | ||
10660 | buf = strim(buf); | |
10661 | if (strcmp(buf, "max")) { | |
10662 | req.ret = cgroup_parse_float(buf, UCLAMP_PERCENT_SHIFT, | |
10663 | &req.percent); | |
10664 | if (req.ret) | |
10665 | return req; | |
b562d140 | 10666 | if ((u64)req.percent > UCLAMP_PERCENT_SCALE) { |
2480c093 PB |
10667 | req.ret = -ERANGE; |
10668 | return req; | |
10669 | } | |
10670 | ||
10671 | req.util = req.percent << SCHED_CAPACITY_SHIFT; | |
10672 | req.util = DIV_ROUND_CLOSEST_ULL(req.util, UCLAMP_PERCENT_SCALE); | |
10673 | } | |
10674 | ||
10675 | return req; | |
10676 | } | |
10677 | ||
10678 | static ssize_t cpu_uclamp_write(struct kernfs_open_file *of, char *buf, | |
10679 | size_t nbytes, loff_t off, | |
10680 | enum uclamp_id clamp_id) | |
10681 | { | |
10682 | struct uclamp_request req; | |
10683 | struct task_group *tg; | |
10684 | ||
10685 | req = capacity_from_percent(buf); | |
10686 | if (req.ret) | |
10687 | return req.ret; | |
10688 | ||
46609ce2 QY |
10689 | static_branch_enable(&sched_uclamp_used); |
10690 | ||
2480c093 PB |
10691 | mutex_lock(&uclamp_mutex); |
10692 | rcu_read_lock(); | |
10693 | ||
10694 | tg = css_tg(of_css(of)); | |
10695 | if (tg->uclamp_req[clamp_id].value != req.util) | |
10696 | uclamp_se_set(&tg->uclamp_req[clamp_id], req.util, false); | |
10697 | ||
10698 | /* | |
10699 | * Because of not recoverable conversion rounding we keep track of the | |
10700 | * exact requested value | |
10701 | */ | |
10702 | tg->uclamp_pct[clamp_id] = req.percent; | |
10703 | ||
0b60ba2d PB |
10704 | /* Update effective clamps to track the most restrictive value */ |
10705 | cpu_util_update_eff(of_css(of)); | |
10706 | ||
2480c093 PB |
10707 | rcu_read_unlock(); |
10708 | mutex_unlock(&uclamp_mutex); | |
10709 | ||
10710 | return nbytes; | |
10711 | } | |
10712 | ||
10713 | static ssize_t cpu_uclamp_min_write(struct kernfs_open_file *of, | |
10714 | char *buf, size_t nbytes, | |
10715 | loff_t off) | |
10716 | { | |
10717 | return cpu_uclamp_write(of, buf, nbytes, off, UCLAMP_MIN); | |
10718 | } | |
10719 | ||
10720 | static ssize_t cpu_uclamp_max_write(struct kernfs_open_file *of, | |
10721 | char *buf, size_t nbytes, | |
10722 | loff_t off) | |
10723 | { | |
10724 | return cpu_uclamp_write(of, buf, nbytes, off, UCLAMP_MAX); | |
10725 | } | |
10726 | ||
10727 | static inline void cpu_uclamp_print(struct seq_file *sf, | |
10728 | enum uclamp_id clamp_id) | |
10729 | { | |
10730 | struct task_group *tg; | |
10731 | u64 util_clamp; | |
10732 | u64 percent; | |
10733 | u32 rem; | |
10734 | ||
10735 | rcu_read_lock(); | |
10736 | tg = css_tg(seq_css(sf)); | |
10737 | util_clamp = tg->uclamp_req[clamp_id].value; | |
10738 | rcu_read_unlock(); | |
10739 | ||
10740 | if (util_clamp == SCHED_CAPACITY_SCALE) { | |
10741 | seq_puts(sf, "max\n"); | |
10742 | return; | |
10743 | } | |
10744 | ||
10745 | percent = tg->uclamp_pct[clamp_id]; | |
10746 | percent = div_u64_rem(percent, POW10(UCLAMP_PERCENT_SHIFT), &rem); | |
10747 | seq_printf(sf, "%llu.%0*u\n", percent, UCLAMP_PERCENT_SHIFT, rem); | |
10748 | } | |
10749 | ||
10750 | static int cpu_uclamp_min_show(struct seq_file *sf, void *v) | |
10751 | { | |
10752 | cpu_uclamp_print(sf, UCLAMP_MIN); | |
10753 | return 0; | |
10754 | } | |
10755 | ||
10756 | static int cpu_uclamp_max_show(struct seq_file *sf, void *v) | |
10757 | { | |
10758 | cpu_uclamp_print(sf, UCLAMP_MAX); | |
10759 | return 0; | |
10760 | } | |
10761 | #endif /* CONFIG_UCLAMP_TASK_GROUP */ | |
10762 | ||
052f1dc7 | 10763 | #ifdef CONFIG_FAIR_GROUP_SCHED |
182446d0 TH |
10764 | static int cpu_shares_write_u64(struct cgroup_subsys_state *css, |
10765 | struct cftype *cftype, u64 shareval) | |
68318b8e | 10766 | { |
5b61d50a KK |
10767 | if (shareval > scale_load_down(ULONG_MAX)) |
10768 | shareval = MAX_SHARES; | |
182446d0 | 10769 | return sched_group_set_shares(css_tg(css), scale_load(shareval)); |
68318b8e SV |
10770 | } |
10771 | ||
182446d0 TH |
10772 | static u64 cpu_shares_read_u64(struct cgroup_subsys_state *css, |
10773 | struct cftype *cft) | |
68318b8e | 10774 | { |
182446d0 | 10775 | struct task_group *tg = css_tg(css); |
68318b8e | 10776 | |
c8b28116 | 10777 | return (u64) scale_load_down(tg->shares); |
68318b8e | 10778 | } |
ab84d31e PT |
10779 | |
10780 | #ifdef CONFIG_CFS_BANDWIDTH | |
a790de99 PT |
10781 | static DEFINE_MUTEX(cfs_constraints_mutex); |
10782 | ||
ab84d31e | 10783 | const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */ |
b1546edc | 10784 | static const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */ |
d505b8af HC |
10785 | /* More than 203 days if BW_SHIFT equals 20. */ |
10786 | static const u64 max_cfs_runtime = MAX_BW * NSEC_PER_USEC; | |
ab84d31e | 10787 | |
a790de99 PT |
10788 | static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime); |
10789 | ||
f4183717 HC |
10790 | static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota, |
10791 | u64 burst) | |
ab84d31e | 10792 | { |
56f570e5 | 10793 | int i, ret = 0, runtime_enabled, runtime_was_enabled; |
029632fb | 10794 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; |
ab84d31e PT |
10795 | |
10796 | if (tg == &root_task_group) | |
10797 | return -EINVAL; | |
10798 | ||
10799 | /* | |
10800 | * Ensure we have at some amount of bandwidth every period. This is | |
10801 | * to prevent reaching a state of large arrears when throttled via | |
10802 | * entity_tick() resulting in prolonged exit starvation. | |
10803 | */ | |
10804 | if (quota < min_cfs_quota_period || period < min_cfs_quota_period) | |
10805 | return -EINVAL; | |
10806 | ||
10807 | /* | |
3b03706f | 10808 | * Likewise, bound things on the other side by preventing insane quota |
ab84d31e PT |
10809 | * periods. This also allows us to normalize in computing quota |
10810 | * feasibility. | |
10811 | */ | |
10812 | if (period > max_cfs_quota_period) | |
10813 | return -EINVAL; | |
10814 | ||
d505b8af HC |
10815 | /* |
10816 | * Bound quota to defend quota against overflow during bandwidth shift. | |
10817 | */ | |
10818 | if (quota != RUNTIME_INF && quota > max_cfs_runtime) | |
10819 | return -EINVAL; | |
10820 | ||
f4183717 HC |
10821 | if (quota != RUNTIME_INF && (burst > quota || |
10822 | burst + quota > max_cfs_runtime)) | |
10823 | return -EINVAL; | |
10824 | ||
0e59bdae KT |
10825 | /* |
10826 | * Prevent race between setting of cfs_rq->runtime_enabled and | |
10827 | * unthrottle_offline_cfs_rqs(). | |
10828 | */ | |
746f5ea9 | 10829 | cpus_read_lock(); |
a790de99 PT |
10830 | mutex_lock(&cfs_constraints_mutex); |
10831 | ret = __cfs_schedulable(tg, period, quota); | |
10832 | if (ret) | |
10833 | goto out_unlock; | |
10834 | ||
58088ad0 | 10835 | runtime_enabled = quota != RUNTIME_INF; |
56f570e5 | 10836 | runtime_was_enabled = cfs_b->quota != RUNTIME_INF; |
1ee14e6c BS |
10837 | /* |
10838 | * If we need to toggle cfs_bandwidth_used, off->on must occur | |
10839 | * before making related changes, and on->off must occur afterwards | |
10840 | */ | |
10841 | if (runtime_enabled && !runtime_was_enabled) | |
10842 | cfs_bandwidth_usage_inc(); | |
ab84d31e PT |
10843 | raw_spin_lock_irq(&cfs_b->lock); |
10844 | cfs_b->period = ns_to_ktime(period); | |
10845 | cfs_b->quota = quota; | |
f4183717 | 10846 | cfs_b->burst = burst; |
58088ad0 | 10847 | |
a9cf55b2 | 10848 | __refill_cfs_bandwidth_runtime(cfs_b); |
d1ccc66d IM |
10849 | |
10850 | /* Restart the period timer (if active) to handle new period expiry: */ | |
77a4d1a1 PZ |
10851 | if (runtime_enabled) |
10852 | start_cfs_bandwidth(cfs_b); | |
d1ccc66d | 10853 | |
ab84d31e PT |
10854 | raw_spin_unlock_irq(&cfs_b->lock); |
10855 | ||
0e59bdae | 10856 | for_each_online_cpu(i) { |
ab84d31e | 10857 | struct cfs_rq *cfs_rq = tg->cfs_rq[i]; |
029632fb | 10858 | struct rq *rq = cfs_rq->rq; |
8a8c69c3 | 10859 | struct rq_flags rf; |
ab84d31e | 10860 | |
8a8c69c3 | 10861 | rq_lock_irq(rq, &rf); |
58088ad0 | 10862 | cfs_rq->runtime_enabled = runtime_enabled; |
ab84d31e | 10863 | cfs_rq->runtime_remaining = 0; |
671fd9da | 10864 | |
029632fb | 10865 | if (cfs_rq->throttled) |
671fd9da | 10866 | unthrottle_cfs_rq(cfs_rq); |
8a8c69c3 | 10867 | rq_unlock_irq(rq, &rf); |
ab84d31e | 10868 | } |
1ee14e6c BS |
10869 | if (runtime_was_enabled && !runtime_enabled) |
10870 | cfs_bandwidth_usage_dec(); | |
a790de99 PT |
10871 | out_unlock: |
10872 | mutex_unlock(&cfs_constraints_mutex); | |
746f5ea9 | 10873 | cpus_read_unlock(); |
ab84d31e | 10874 | |
a790de99 | 10875 | return ret; |
ab84d31e PT |
10876 | } |
10877 | ||
b1546edc | 10878 | static int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us) |
ab84d31e | 10879 | { |
f4183717 | 10880 | u64 quota, period, burst; |
ab84d31e | 10881 | |
029632fb | 10882 | period = ktime_to_ns(tg->cfs_bandwidth.period); |
f4183717 | 10883 | burst = tg->cfs_bandwidth.burst; |
ab84d31e PT |
10884 | if (cfs_quota_us < 0) |
10885 | quota = RUNTIME_INF; | |
1a8b4540 | 10886 | else if ((u64)cfs_quota_us <= U64_MAX / NSEC_PER_USEC) |
ab84d31e | 10887 | quota = (u64)cfs_quota_us * NSEC_PER_USEC; |
1a8b4540 KK |
10888 | else |
10889 | return -EINVAL; | |
ab84d31e | 10890 | |
f4183717 | 10891 | return tg_set_cfs_bandwidth(tg, period, quota, burst); |
ab84d31e PT |
10892 | } |
10893 | ||
b1546edc | 10894 | static long tg_get_cfs_quota(struct task_group *tg) |
ab84d31e PT |
10895 | { |
10896 | u64 quota_us; | |
10897 | ||
029632fb | 10898 | if (tg->cfs_bandwidth.quota == RUNTIME_INF) |
ab84d31e PT |
10899 | return -1; |
10900 | ||
029632fb | 10901 | quota_us = tg->cfs_bandwidth.quota; |
ab84d31e PT |
10902 | do_div(quota_us, NSEC_PER_USEC); |
10903 | ||
10904 | return quota_us; | |
10905 | } | |
10906 | ||
b1546edc | 10907 | static int tg_set_cfs_period(struct task_group *tg, long cfs_period_us) |
ab84d31e | 10908 | { |
f4183717 | 10909 | u64 quota, period, burst; |
ab84d31e | 10910 | |
1a8b4540 KK |
10911 | if ((u64)cfs_period_us > U64_MAX / NSEC_PER_USEC) |
10912 | return -EINVAL; | |
10913 | ||
ab84d31e | 10914 | period = (u64)cfs_period_us * NSEC_PER_USEC; |
029632fb | 10915 | quota = tg->cfs_bandwidth.quota; |
f4183717 | 10916 | burst = tg->cfs_bandwidth.burst; |
ab84d31e | 10917 | |
f4183717 | 10918 | return tg_set_cfs_bandwidth(tg, period, quota, burst); |
ab84d31e PT |
10919 | } |
10920 | ||
b1546edc | 10921 | static long tg_get_cfs_period(struct task_group *tg) |
ab84d31e PT |
10922 | { |
10923 | u64 cfs_period_us; | |
10924 | ||
029632fb | 10925 | cfs_period_us = ktime_to_ns(tg->cfs_bandwidth.period); |
ab84d31e PT |
10926 | do_div(cfs_period_us, NSEC_PER_USEC); |
10927 | ||
10928 | return cfs_period_us; | |
10929 | } | |
10930 | ||
f4183717 HC |
10931 | static int tg_set_cfs_burst(struct task_group *tg, long cfs_burst_us) |
10932 | { | |
10933 | u64 quota, period, burst; | |
10934 | ||
10935 | if ((u64)cfs_burst_us > U64_MAX / NSEC_PER_USEC) | |
10936 | return -EINVAL; | |
10937 | ||
10938 | burst = (u64)cfs_burst_us * NSEC_PER_USEC; | |
10939 | period = ktime_to_ns(tg->cfs_bandwidth.period); | |
10940 | quota = tg->cfs_bandwidth.quota; | |
10941 | ||
10942 | return tg_set_cfs_bandwidth(tg, period, quota, burst); | |
10943 | } | |
10944 | ||
10945 | static long tg_get_cfs_burst(struct task_group *tg) | |
10946 | { | |
10947 | u64 burst_us; | |
10948 | ||
10949 | burst_us = tg->cfs_bandwidth.burst; | |
10950 | do_div(burst_us, NSEC_PER_USEC); | |
10951 | ||
10952 | return burst_us; | |
10953 | } | |
10954 | ||
182446d0 TH |
10955 | static s64 cpu_cfs_quota_read_s64(struct cgroup_subsys_state *css, |
10956 | struct cftype *cft) | |
ab84d31e | 10957 | { |
182446d0 | 10958 | return tg_get_cfs_quota(css_tg(css)); |
ab84d31e PT |
10959 | } |
10960 | ||
182446d0 TH |
10961 | static int cpu_cfs_quota_write_s64(struct cgroup_subsys_state *css, |
10962 | struct cftype *cftype, s64 cfs_quota_us) | |
ab84d31e | 10963 | { |
182446d0 | 10964 | return tg_set_cfs_quota(css_tg(css), cfs_quota_us); |
ab84d31e PT |
10965 | } |
10966 | ||
182446d0 TH |
10967 | static u64 cpu_cfs_period_read_u64(struct cgroup_subsys_state *css, |
10968 | struct cftype *cft) | |
ab84d31e | 10969 | { |
182446d0 | 10970 | return tg_get_cfs_period(css_tg(css)); |
ab84d31e PT |
10971 | } |
10972 | ||
182446d0 TH |
10973 | static int cpu_cfs_period_write_u64(struct cgroup_subsys_state *css, |
10974 | struct cftype *cftype, u64 cfs_period_us) | |
ab84d31e | 10975 | { |
182446d0 | 10976 | return tg_set_cfs_period(css_tg(css), cfs_period_us); |
ab84d31e PT |
10977 | } |
10978 | ||
f4183717 HC |
10979 | static u64 cpu_cfs_burst_read_u64(struct cgroup_subsys_state *css, |
10980 | struct cftype *cft) | |
10981 | { | |
10982 | return tg_get_cfs_burst(css_tg(css)); | |
10983 | } | |
10984 | ||
10985 | static int cpu_cfs_burst_write_u64(struct cgroup_subsys_state *css, | |
10986 | struct cftype *cftype, u64 cfs_burst_us) | |
10987 | { | |
10988 | return tg_set_cfs_burst(css_tg(css), cfs_burst_us); | |
10989 | } | |
10990 | ||
a790de99 PT |
10991 | struct cfs_schedulable_data { |
10992 | struct task_group *tg; | |
10993 | u64 period, quota; | |
10994 | }; | |
10995 | ||
10996 | /* | |
10997 | * normalize group quota/period to be quota/max_period | |
10998 | * note: units are usecs | |
10999 | */ | |
11000 | static u64 normalize_cfs_quota(struct task_group *tg, | |
11001 | struct cfs_schedulable_data *d) | |
11002 | { | |
11003 | u64 quota, period; | |
11004 | ||
11005 | if (tg == d->tg) { | |
11006 | period = d->period; | |
11007 | quota = d->quota; | |
11008 | } else { | |
11009 | period = tg_get_cfs_period(tg); | |
11010 | quota = tg_get_cfs_quota(tg); | |
11011 | } | |
11012 | ||
11013 | /* note: these should typically be equivalent */ | |
11014 | if (quota == RUNTIME_INF || quota == -1) | |
11015 | return RUNTIME_INF; | |
11016 | ||
11017 | return to_ratio(period, quota); | |
11018 | } | |
11019 | ||
11020 | static int tg_cfs_schedulable_down(struct task_group *tg, void *data) | |
11021 | { | |
11022 | struct cfs_schedulable_data *d = data; | |
029632fb | 11023 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; |
a790de99 PT |
11024 | s64 quota = 0, parent_quota = -1; |
11025 | ||
11026 | if (!tg->parent) { | |
11027 | quota = RUNTIME_INF; | |
11028 | } else { | |
029632fb | 11029 | struct cfs_bandwidth *parent_b = &tg->parent->cfs_bandwidth; |
a790de99 PT |
11030 | |
11031 | quota = normalize_cfs_quota(tg, d); | |
9c58c79a | 11032 | parent_quota = parent_b->hierarchical_quota; |
a790de99 PT |
11033 | |
11034 | /* | |
c53593e5 TH |
11035 | * Ensure max(child_quota) <= parent_quota. On cgroup2, |
11036 | * always take the min. On cgroup1, only inherit when no | |
d1ccc66d | 11037 | * limit is set: |
a790de99 | 11038 | */ |
c53593e5 TH |
11039 | if (cgroup_subsys_on_dfl(cpu_cgrp_subsys)) { |
11040 | quota = min(quota, parent_quota); | |
11041 | } else { | |
11042 | if (quota == RUNTIME_INF) | |
11043 | quota = parent_quota; | |
11044 | else if (parent_quota != RUNTIME_INF && quota > parent_quota) | |
11045 | return -EINVAL; | |
11046 | } | |
a790de99 | 11047 | } |
9c58c79a | 11048 | cfs_b->hierarchical_quota = quota; |
a790de99 PT |
11049 | |
11050 | return 0; | |
11051 | } | |
11052 | ||
11053 | static int __cfs_schedulable(struct task_group *tg, u64 period, u64 quota) | |
11054 | { | |
8277434e | 11055 | int ret; |
a790de99 PT |
11056 | struct cfs_schedulable_data data = { |
11057 | .tg = tg, | |
11058 | .period = period, | |
11059 | .quota = quota, | |
11060 | }; | |
11061 | ||
11062 | if (quota != RUNTIME_INF) { | |
11063 | do_div(data.period, NSEC_PER_USEC); | |
11064 | do_div(data.quota, NSEC_PER_USEC); | |
11065 | } | |
11066 | ||
8277434e PT |
11067 | rcu_read_lock(); |
11068 | ret = walk_tg_tree(tg_cfs_schedulable_down, tg_nop, &data); | |
11069 | rcu_read_unlock(); | |
11070 | ||
11071 | return ret; | |
a790de99 | 11072 | } |
e8da1b18 | 11073 | |
a1f7164c | 11074 | static int cpu_cfs_stat_show(struct seq_file *sf, void *v) |
e8da1b18 | 11075 | { |
2da8ca82 | 11076 | struct task_group *tg = css_tg(seq_css(sf)); |
029632fb | 11077 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; |
e8da1b18 | 11078 | |
44ffc75b TH |
11079 | seq_printf(sf, "nr_periods %d\n", cfs_b->nr_periods); |
11080 | seq_printf(sf, "nr_throttled %d\n", cfs_b->nr_throttled); | |
11081 | seq_printf(sf, "throttled_time %llu\n", cfs_b->throttled_time); | |
e8da1b18 | 11082 | |
3d6c50c2 | 11083 | if (schedstat_enabled() && tg != &root_task_group) { |
ceeadb83 | 11084 | struct sched_statistics *stats; |
3d6c50c2 YW |
11085 | u64 ws = 0; |
11086 | int i; | |
11087 | ||
ceeadb83 YS |
11088 | for_each_possible_cpu(i) { |
11089 | stats = __schedstats_from_se(tg->se[i]); | |
11090 | ws += schedstat_val(stats->wait_sum); | |
11091 | } | |
3d6c50c2 YW |
11092 | |
11093 | seq_printf(sf, "wait_sum %llu\n", ws); | |
11094 | } | |
11095 | ||
bcb1704a HC |
11096 | seq_printf(sf, "nr_bursts %d\n", cfs_b->nr_burst); |
11097 | seq_printf(sf, "burst_time %llu\n", cfs_b->burst_time); | |
11098 | ||
e8da1b18 NR |
11099 | return 0; |
11100 | } | |
ab84d31e | 11101 | #endif /* CONFIG_CFS_BANDWIDTH */ |
6d6bc0ad | 11102 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
68318b8e | 11103 | |
052f1dc7 | 11104 | #ifdef CONFIG_RT_GROUP_SCHED |
182446d0 TH |
11105 | static int cpu_rt_runtime_write(struct cgroup_subsys_state *css, |
11106 | struct cftype *cft, s64 val) | |
6f505b16 | 11107 | { |
182446d0 | 11108 | return sched_group_set_rt_runtime(css_tg(css), val); |
6f505b16 PZ |
11109 | } |
11110 | ||
182446d0 TH |
11111 | static s64 cpu_rt_runtime_read(struct cgroup_subsys_state *css, |
11112 | struct cftype *cft) | |
6f505b16 | 11113 | { |
182446d0 | 11114 | return sched_group_rt_runtime(css_tg(css)); |
6f505b16 | 11115 | } |
d0b27fa7 | 11116 | |
182446d0 TH |
11117 | static int cpu_rt_period_write_uint(struct cgroup_subsys_state *css, |
11118 | struct cftype *cftype, u64 rt_period_us) | |
d0b27fa7 | 11119 | { |
182446d0 | 11120 | return sched_group_set_rt_period(css_tg(css), rt_period_us); |
d0b27fa7 PZ |
11121 | } |
11122 | ||
182446d0 TH |
11123 | static u64 cpu_rt_period_read_uint(struct cgroup_subsys_state *css, |
11124 | struct cftype *cft) | |
d0b27fa7 | 11125 | { |
182446d0 | 11126 | return sched_group_rt_period(css_tg(css)); |
d0b27fa7 | 11127 | } |
6d6bc0ad | 11128 | #endif /* CONFIG_RT_GROUP_SCHED */ |
6f505b16 | 11129 | |
30400039 JD |
11130 | #ifdef CONFIG_FAIR_GROUP_SCHED |
11131 | static s64 cpu_idle_read_s64(struct cgroup_subsys_state *css, | |
11132 | struct cftype *cft) | |
11133 | { | |
11134 | return css_tg(css)->idle; | |
11135 | } | |
11136 | ||
11137 | static int cpu_idle_write_s64(struct cgroup_subsys_state *css, | |
11138 | struct cftype *cft, s64 idle) | |
11139 | { | |
11140 | return sched_group_set_idle(css_tg(css), idle); | |
11141 | } | |
11142 | #endif | |
11143 | ||
a1f7164c | 11144 | static struct cftype cpu_legacy_files[] = { |
052f1dc7 | 11145 | #ifdef CONFIG_FAIR_GROUP_SCHED |
fe5c7cc2 PM |
11146 | { |
11147 | .name = "shares", | |
f4c753b7 PM |
11148 | .read_u64 = cpu_shares_read_u64, |
11149 | .write_u64 = cpu_shares_write_u64, | |
fe5c7cc2 | 11150 | }, |
30400039 JD |
11151 | { |
11152 | .name = "idle", | |
11153 | .read_s64 = cpu_idle_read_s64, | |
11154 | .write_s64 = cpu_idle_write_s64, | |
11155 | }, | |
052f1dc7 | 11156 | #endif |
ab84d31e PT |
11157 | #ifdef CONFIG_CFS_BANDWIDTH |
11158 | { | |
11159 | .name = "cfs_quota_us", | |
11160 | .read_s64 = cpu_cfs_quota_read_s64, | |
11161 | .write_s64 = cpu_cfs_quota_write_s64, | |
11162 | }, | |
11163 | { | |
11164 | .name = "cfs_period_us", | |
11165 | .read_u64 = cpu_cfs_period_read_u64, | |
11166 | .write_u64 = cpu_cfs_period_write_u64, | |
11167 | }, | |
f4183717 HC |
11168 | { |
11169 | .name = "cfs_burst_us", | |
11170 | .read_u64 = cpu_cfs_burst_read_u64, | |
11171 | .write_u64 = cpu_cfs_burst_write_u64, | |
11172 | }, | |
e8da1b18 NR |
11173 | { |
11174 | .name = "stat", | |
a1f7164c | 11175 | .seq_show = cpu_cfs_stat_show, |
e8da1b18 | 11176 | }, |
ab84d31e | 11177 | #endif |
052f1dc7 | 11178 | #ifdef CONFIG_RT_GROUP_SCHED |
6f505b16 | 11179 | { |
9f0c1e56 | 11180 | .name = "rt_runtime_us", |
06ecb27c PM |
11181 | .read_s64 = cpu_rt_runtime_read, |
11182 | .write_s64 = cpu_rt_runtime_write, | |
6f505b16 | 11183 | }, |
d0b27fa7 PZ |
11184 | { |
11185 | .name = "rt_period_us", | |
f4c753b7 PM |
11186 | .read_u64 = cpu_rt_period_read_uint, |
11187 | .write_u64 = cpu_rt_period_write_uint, | |
d0b27fa7 | 11188 | }, |
2480c093 PB |
11189 | #endif |
11190 | #ifdef CONFIG_UCLAMP_TASK_GROUP | |
11191 | { | |
11192 | .name = "uclamp.min", | |
11193 | .flags = CFTYPE_NOT_ON_ROOT, | |
11194 | .seq_show = cpu_uclamp_min_show, | |
11195 | .write = cpu_uclamp_min_write, | |
11196 | }, | |
11197 | { | |
11198 | .name = "uclamp.max", | |
11199 | .flags = CFTYPE_NOT_ON_ROOT, | |
11200 | .seq_show = cpu_uclamp_max_show, | |
11201 | .write = cpu_uclamp_max_write, | |
11202 | }, | |
052f1dc7 | 11203 | #endif |
d1ccc66d | 11204 | { } /* Terminate */ |
68318b8e SV |
11205 | }; |
11206 | ||
d41bf8c9 TH |
11207 | static int cpu_extra_stat_show(struct seq_file *sf, |
11208 | struct cgroup_subsys_state *css) | |
0d593634 | 11209 | { |
0d593634 TH |
11210 | #ifdef CONFIG_CFS_BANDWIDTH |
11211 | { | |
d41bf8c9 | 11212 | struct task_group *tg = css_tg(css); |
0d593634 | 11213 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; |
bcb1704a | 11214 | u64 throttled_usec, burst_usec; |
0d593634 TH |
11215 | |
11216 | throttled_usec = cfs_b->throttled_time; | |
11217 | do_div(throttled_usec, NSEC_PER_USEC); | |
bcb1704a HC |
11218 | burst_usec = cfs_b->burst_time; |
11219 | do_div(burst_usec, NSEC_PER_USEC); | |
0d593634 TH |
11220 | |
11221 | seq_printf(sf, "nr_periods %d\n" | |
11222 | "nr_throttled %d\n" | |
bcb1704a HC |
11223 | "throttled_usec %llu\n" |
11224 | "nr_bursts %d\n" | |
11225 | "burst_usec %llu\n", | |
0d593634 | 11226 | cfs_b->nr_periods, cfs_b->nr_throttled, |
bcb1704a | 11227 | throttled_usec, cfs_b->nr_burst, burst_usec); |
0d593634 TH |
11228 | } |
11229 | #endif | |
11230 | return 0; | |
11231 | } | |
11232 | ||
11233 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
11234 | static u64 cpu_weight_read_u64(struct cgroup_subsys_state *css, | |
11235 | struct cftype *cft) | |
11236 | { | |
11237 | struct task_group *tg = css_tg(css); | |
11238 | u64 weight = scale_load_down(tg->shares); | |
11239 | ||
11240 | return DIV_ROUND_CLOSEST_ULL(weight * CGROUP_WEIGHT_DFL, 1024); | |
11241 | } | |
11242 | ||
11243 | static int cpu_weight_write_u64(struct cgroup_subsys_state *css, | |
11244 | struct cftype *cft, u64 weight) | |
11245 | { | |
11246 | /* | |
11247 | * cgroup weight knobs should use the common MIN, DFL and MAX | |
11248 | * values which are 1, 100 and 10000 respectively. While it loses | |
11249 | * a bit of range on both ends, it maps pretty well onto the shares | |
11250 | * value used by scheduler and the round-trip conversions preserve | |
11251 | * the original value over the entire range. | |
11252 | */ | |
11253 | if (weight < CGROUP_WEIGHT_MIN || weight > CGROUP_WEIGHT_MAX) | |
11254 | return -ERANGE; | |
11255 | ||
11256 | weight = DIV_ROUND_CLOSEST_ULL(weight * 1024, CGROUP_WEIGHT_DFL); | |
11257 | ||
11258 | return sched_group_set_shares(css_tg(css), scale_load(weight)); | |
11259 | } | |
11260 | ||
11261 | static s64 cpu_weight_nice_read_s64(struct cgroup_subsys_state *css, | |
11262 | struct cftype *cft) | |
11263 | { | |
11264 | unsigned long weight = scale_load_down(css_tg(css)->shares); | |
11265 | int last_delta = INT_MAX; | |
11266 | int prio, delta; | |
11267 | ||
11268 | /* find the closest nice value to the current weight */ | |
11269 | for (prio = 0; prio < ARRAY_SIZE(sched_prio_to_weight); prio++) { | |
11270 | delta = abs(sched_prio_to_weight[prio] - weight); | |
11271 | if (delta >= last_delta) | |
11272 | break; | |
11273 | last_delta = delta; | |
11274 | } | |
11275 | ||
11276 | return PRIO_TO_NICE(prio - 1 + MAX_RT_PRIO); | |
11277 | } | |
11278 | ||
11279 | static int cpu_weight_nice_write_s64(struct cgroup_subsys_state *css, | |
11280 | struct cftype *cft, s64 nice) | |
11281 | { | |
11282 | unsigned long weight; | |
7281c8de | 11283 | int idx; |
0d593634 TH |
11284 | |
11285 | if (nice < MIN_NICE || nice > MAX_NICE) | |
11286 | return -ERANGE; | |
11287 | ||
7281c8de PZ |
11288 | idx = NICE_TO_PRIO(nice) - MAX_RT_PRIO; |
11289 | idx = array_index_nospec(idx, 40); | |
11290 | weight = sched_prio_to_weight[idx]; | |
11291 | ||
0d593634 TH |
11292 | return sched_group_set_shares(css_tg(css), scale_load(weight)); |
11293 | } | |
11294 | #endif | |
11295 | ||
11296 | static void __maybe_unused cpu_period_quota_print(struct seq_file *sf, | |
11297 | long period, long quota) | |
11298 | { | |
11299 | if (quota < 0) | |
11300 | seq_puts(sf, "max"); | |
11301 | else | |
11302 | seq_printf(sf, "%ld", quota); | |
11303 | ||
11304 | seq_printf(sf, " %ld\n", period); | |
11305 | } | |
11306 | ||
11307 | /* caller should put the current value in *@periodp before calling */ | |
11308 | static int __maybe_unused cpu_period_quota_parse(char *buf, | |
11309 | u64 *periodp, u64 *quotap) | |
11310 | { | |
11311 | char tok[21]; /* U64_MAX */ | |
11312 | ||
4c47acd8 | 11313 | if (sscanf(buf, "%20s %llu", tok, periodp) < 1) |
0d593634 TH |
11314 | return -EINVAL; |
11315 | ||
11316 | *periodp *= NSEC_PER_USEC; | |
11317 | ||
11318 | if (sscanf(tok, "%llu", quotap)) | |
11319 | *quotap *= NSEC_PER_USEC; | |
11320 | else if (!strcmp(tok, "max")) | |
11321 | *quotap = RUNTIME_INF; | |
11322 | else | |
11323 | return -EINVAL; | |
11324 | ||
11325 | return 0; | |
11326 | } | |
11327 | ||
11328 | #ifdef CONFIG_CFS_BANDWIDTH | |
11329 | static int cpu_max_show(struct seq_file *sf, void *v) | |
11330 | { | |
11331 | struct task_group *tg = css_tg(seq_css(sf)); | |
11332 | ||
11333 | cpu_period_quota_print(sf, tg_get_cfs_period(tg), tg_get_cfs_quota(tg)); | |
11334 | return 0; | |
11335 | } | |
11336 | ||
11337 | static ssize_t cpu_max_write(struct kernfs_open_file *of, | |
11338 | char *buf, size_t nbytes, loff_t off) | |
11339 | { | |
11340 | struct task_group *tg = css_tg(of_css(of)); | |
11341 | u64 period = tg_get_cfs_period(tg); | |
f4183717 | 11342 | u64 burst = tg_get_cfs_burst(tg); |
0d593634 TH |
11343 | u64 quota; |
11344 | int ret; | |
11345 | ||
11346 | ret = cpu_period_quota_parse(buf, &period, "a); | |
11347 | if (!ret) | |
f4183717 | 11348 | ret = tg_set_cfs_bandwidth(tg, period, quota, burst); |
0d593634 TH |
11349 | return ret ?: nbytes; |
11350 | } | |
11351 | #endif | |
11352 | ||
11353 | static struct cftype cpu_files[] = { | |
0d593634 TH |
11354 | #ifdef CONFIG_FAIR_GROUP_SCHED |
11355 | { | |
11356 | .name = "weight", | |
11357 | .flags = CFTYPE_NOT_ON_ROOT, | |
11358 | .read_u64 = cpu_weight_read_u64, | |
11359 | .write_u64 = cpu_weight_write_u64, | |
11360 | }, | |
11361 | { | |
11362 | .name = "weight.nice", | |
11363 | .flags = CFTYPE_NOT_ON_ROOT, | |
11364 | .read_s64 = cpu_weight_nice_read_s64, | |
11365 | .write_s64 = cpu_weight_nice_write_s64, | |
11366 | }, | |
30400039 JD |
11367 | { |
11368 | .name = "idle", | |
11369 | .flags = CFTYPE_NOT_ON_ROOT, | |
11370 | .read_s64 = cpu_idle_read_s64, | |
11371 | .write_s64 = cpu_idle_write_s64, | |
11372 | }, | |
0d593634 TH |
11373 | #endif |
11374 | #ifdef CONFIG_CFS_BANDWIDTH | |
11375 | { | |
11376 | .name = "max", | |
11377 | .flags = CFTYPE_NOT_ON_ROOT, | |
11378 | .seq_show = cpu_max_show, | |
11379 | .write = cpu_max_write, | |
11380 | }, | |
f4183717 HC |
11381 | { |
11382 | .name = "max.burst", | |
11383 | .flags = CFTYPE_NOT_ON_ROOT, | |
11384 | .read_u64 = cpu_cfs_burst_read_u64, | |
11385 | .write_u64 = cpu_cfs_burst_write_u64, | |
11386 | }, | |
2480c093 PB |
11387 | #endif |
11388 | #ifdef CONFIG_UCLAMP_TASK_GROUP | |
11389 | { | |
11390 | .name = "uclamp.min", | |
11391 | .flags = CFTYPE_NOT_ON_ROOT, | |
11392 | .seq_show = cpu_uclamp_min_show, | |
11393 | .write = cpu_uclamp_min_write, | |
11394 | }, | |
11395 | { | |
11396 | .name = "uclamp.max", | |
11397 | .flags = CFTYPE_NOT_ON_ROOT, | |
11398 | .seq_show = cpu_uclamp_max_show, | |
11399 | .write = cpu_uclamp_max_write, | |
11400 | }, | |
0d593634 TH |
11401 | #endif |
11402 | { } /* terminate */ | |
11403 | }; | |
11404 | ||
073219e9 | 11405 | struct cgroup_subsys cpu_cgrp_subsys = { |
92fb9748 | 11406 | .css_alloc = cpu_cgroup_css_alloc, |
96b77745 | 11407 | .css_online = cpu_cgroup_css_online, |
2f5177f0 | 11408 | .css_released = cpu_cgroup_css_released, |
92fb9748 | 11409 | .css_free = cpu_cgroup_css_free, |
d41bf8c9 | 11410 | .css_extra_stat_show = cpu_extra_stat_show, |
df16b71c | 11411 | #ifdef CONFIG_RT_GROUP_SCHED |
bb9d97b6 | 11412 | .can_attach = cpu_cgroup_can_attach, |
df16b71c | 11413 | #endif |
bb9d97b6 | 11414 | .attach = cpu_cgroup_attach, |
a1f7164c | 11415 | .legacy_cftypes = cpu_legacy_files, |
0d593634 | 11416 | .dfl_cftypes = cpu_files, |
b38e42e9 | 11417 | .early_init = true, |
0d593634 | 11418 | .threaded = true, |
68318b8e SV |
11419 | }; |
11420 | ||
052f1dc7 | 11421 | #endif /* CONFIG_CGROUP_SCHED */ |
d842de87 | 11422 | |
b637a328 PM |
11423 | void dump_cpu_task(int cpu) |
11424 | { | |
bc1cca97 ZL |
11425 | if (cpu == smp_processor_id() && in_hardirq()) { |
11426 | struct pt_regs *regs; | |
11427 | ||
11428 | regs = get_irq_regs(); | |
11429 | if (regs) { | |
11430 | show_regs(regs); | |
11431 | return; | |
11432 | } | |
11433 | } | |
11434 | ||
e73dfe30 ZL |
11435 | if (trigger_single_cpu_backtrace(cpu)) |
11436 | return; | |
11437 | ||
b637a328 PM |
11438 | pr_info("Task dump for CPU %d:\n", cpu); |
11439 | sched_show_task(cpu_curr(cpu)); | |
11440 | } | |
ed82b8a1 AK |
11441 | |
11442 | /* | |
11443 | * Nice levels are multiplicative, with a gentle 10% change for every | |
11444 | * nice level changed. I.e. when a CPU-bound task goes from nice 0 to | |
11445 | * nice 1, it will get ~10% less CPU time than another CPU-bound task | |
11446 | * that remained on nice 0. | |
11447 | * | |
11448 | * The "10% effect" is relative and cumulative: from _any_ nice level, | |
11449 | * if you go up 1 level, it's -10% CPU usage, if you go down 1 level | |
11450 | * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25. | |
11451 | * If a task goes up by ~10% and another task goes down by ~10% then | |
11452 | * the relative distance between them is ~25%.) | |
11453 | */ | |
11454 | const int sched_prio_to_weight[40] = { | |
11455 | /* -20 */ 88761, 71755, 56483, 46273, 36291, | |
11456 | /* -15 */ 29154, 23254, 18705, 14949, 11916, | |
11457 | /* -10 */ 9548, 7620, 6100, 4904, 3906, | |
11458 | /* -5 */ 3121, 2501, 1991, 1586, 1277, | |
11459 | /* 0 */ 1024, 820, 655, 526, 423, | |
11460 | /* 5 */ 335, 272, 215, 172, 137, | |
11461 | /* 10 */ 110, 87, 70, 56, 45, | |
11462 | /* 15 */ 36, 29, 23, 18, 15, | |
11463 | }; | |
11464 | ||
11465 | /* | |
11466 | * Inverse (2^32/x) values of the sched_prio_to_weight[] array, precalculated. | |
11467 | * | |
11468 | * In cases where the weight does not change often, we can use the | |
11469 | * precalculated inverse to speed up arithmetics by turning divisions | |
11470 | * into multiplications: | |
11471 | */ | |
11472 | const u32 sched_prio_to_wmult[40] = { | |
11473 | /* -20 */ 48388, 59856, 76040, 92818, 118348, | |
11474 | /* -15 */ 147320, 184698, 229616, 287308, 360437, | |
11475 | /* -10 */ 449829, 563644, 704093, 875809, 1099582, | |
11476 | /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326, | |
11477 | /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587, | |
11478 | /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126, | |
11479 | /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717, | |
11480 | /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153, | |
11481 | }; | |
14a7405b | 11482 | |
9d246053 PA |
11483 | void call_trace_sched_update_nr_running(struct rq *rq, int count) |
11484 | { | |
11485 | trace_sched_update_nr_running_tp(rq, count); | |
11486 | } | |
af7f588d MD |
11487 | |
11488 | #ifdef CONFIG_SCHED_MM_CID | |
223baf9d MD |
11489 | |
11490 | /** | |
11491 | * @cid_lock: Guarantee forward-progress of cid allocation. | |
11492 | * | |
11493 | * Concurrency ID allocation within a bitmap is mostly lock-free. The cid_lock | |
11494 | * is only used when contention is detected by the lock-free allocation so | |
11495 | * forward progress can be guaranteed. | |
11496 | */ | |
11497 | DEFINE_RAW_SPINLOCK(cid_lock); | |
11498 | ||
11499 | /** | |
11500 | * @use_cid_lock: Select cid allocation behavior: lock-free vs spinlock. | |
11501 | * | |
11502 | * When @use_cid_lock is 0, the cid allocation is lock-free. When contention is | |
11503 | * detected, it is set to 1 to ensure that all newly coming allocations are | |
11504 | * serialized by @cid_lock until the allocation which detected contention | |
11505 | * completes and sets @use_cid_lock back to 0. This guarantees forward progress | |
11506 | * of a cid allocation. | |
11507 | */ | |
11508 | int use_cid_lock; | |
11509 | ||
11510 | /* | |
11511 | * mm_cid remote-clear implements a lock-free algorithm to clear per-mm/cpu cid | |
11512 | * concurrently with respect to the execution of the source runqueue context | |
11513 | * switch. | |
11514 | * | |
11515 | * There is one basic properties we want to guarantee here: | |
11516 | * | |
11517 | * (1) Remote-clear should _never_ mark a per-cpu cid UNSET when it is actively | |
11518 | * used by a task. That would lead to concurrent allocation of the cid and | |
11519 | * userspace corruption. | |
11520 | * | |
11521 | * Provide this guarantee by introducing a Dekker memory ordering to guarantee | |
11522 | * that a pair of loads observe at least one of a pair of stores, which can be | |
11523 | * shown as: | |
11524 | * | |
11525 | * X = Y = 0 | |
11526 | * | |
11527 | * w[X]=1 w[Y]=1 | |
11528 | * MB MB | |
11529 | * r[Y]=y r[X]=x | |
11530 | * | |
11531 | * Which guarantees that x==0 && y==0 is impossible. But rather than using | |
11532 | * values 0 and 1, this algorithm cares about specific state transitions of the | |
11533 | * runqueue current task (as updated by the scheduler context switch), and the | |
11534 | * per-mm/cpu cid value. | |
11535 | * | |
11536 | * Let's introduce task (Y) which has task->mm == mm and task (N) which has | |
11537 | * task->mm != mm for the rest of the discussion. There are two scheduler state | |
11538 | * transitions on context switch we care about: | |
11539 | * | |
11540 | * (TSA) Store to rq->curr with transition from (N) to (Y) | |
11541 | * | |
11542 | * (TSB) Store to rq->curr with transition from (Y) to (N) | |
11543 | * | |
11544 | * On the remote-clear side, there is one transition we care about: | |
11545 | * | |
11546 | * (TMA) cmpxchg to *pcpu_cid to set the LAZY flag | |
11547 | * | |
11548 | * There is also a transition to UNSET state which can be performed from all | |
11549 | * sides (scheduler, remote-clear). It is always performed with a cmpxchg which | |
11550 | * guarantees that only a single thread will succeed: | |
11551 | * | |
11552 | * (TMB) cmpxchg to *pcpu_cid to mark UNSET | |
11553 | * | |
11554 | * Just to be clear, what we do _not_ want to happen is a transition to UNSET | |
11555 | * when a thread is actively using the cid (property (1)). | |
11556 | * | |
11557 | * Let's looks at the relevant combinations of TSA/TSB, and TMA transitions. | |
11558 | * | |
11559 | * Scenario A) (TSA)+(TMA) (from next task perspective) | |
11560 | * | |
11561 | * CPU0 CPU1 | |
11562 | * | |
11563 | * Context switch CS-1 Remote-clear | |
11564 | * - store to rq->curr: (N)->(Y) (TSA) - cmpxchg to *pcpu_id to LAZY (TMA) | |
11565 | * (implied barrier after cmpxchg) | |
11566 | * - switch_mm_cid() | |
11567 | * - memory barrier (see switch_mm_cid() | |
11568 | * comment explaining how this barrier | |
11569 | * is combined with other scheduler | |
11570 | * barriers) | |
11571 | * - mm_cid_get (next) | |
11572 | * - READ_ONCE(*pcpu_cid) - rcu_dereference(src_rq->curr) | |
11573 | * | |
11574 | * This Dekker ensures that either task (Y) is observed by the | |
11575 | * rcu_dereference() or the LAZY flag is observed by READ_ONCE(), or both are | |
11576 | * observed. | |
11577 | * | |
11578 | * If task (Y) store is observed by rcu_dereference(), it means that there is | |
11579 | * still an active task on the cpu. Remote-clear will therefore not transition | |
11580 | * to UNSET, which fulfills property (1). | |
11581 | * | |
11582 | * If task (Y) is not observed, but the lazy flag is observed by READ_ONCE(), | |
11583 | * it will move its state to UNSET, which clears the percpu cid perhaps | |
11584 | * uselessly (which is not an issue for correctness). Because task (Y) is not | |
11585 | * observed, CPU1 can move ahead to set the state to UNSET. Because moving | |
11586 | * state to UNSET is done with a cmpxchg expecting that the old state has the | |
11587 | * LAZY flag set, only one thread will successfully UNSET. | |
11588 | * | |
11589 | * If both states (LAZY flag and task (Y)) are observed, the thread on CPU0 | |
11590 | * will observe the LAZY flag and transition to UNSET (perhaps uselessly), and | |
11591 | * CPU1 will observe task (Y) and do nothing more, which is fine. | |
11592 | * | |
11593 | * What we are effectively preventing with this Dekker is a scenario where | |
11594 | * neither LAZY flag nor store (Y) are observed, which would fail property (1) | |
11595 | * because this would UNSET a cid which is actively used. | |
11596 | */ | |
11597 | ||
11598 | void sched_mm_cid_migrate_from(struct task_struct *t) | |
11599 | { | |
11600 | t->migrate_from_cpu = task_cpu(t); | |
11601 | } | |
11602 | ||
11603 | static | |
11604 | int __sched_mm_cid_migrate_from_fetch_cid(struct rq *src_rq, | |
11605 | struct task_struct *t, | |
11606 | struct mm_cid *src_pcpu_cid) | |
af7f588d MD |
11607 | { |
11608 | struct mm_struct *mm = t->mm; | |
223baf9d MD |
11609 | struct task_struct *src_task; |
11610 | int src_cid, last_mm_cid; | |
af7f588d MD |
11611 | |
11612 | if (!mm) | |
223baf9d MD |
11613 | return -1; |
11614 | ||
11615 | last_mm_cid = t->last_mm_cid; | |
11616 | /* | |
11617 | * If the migrated task has no last cid, or if the current | |
11618 | * task on src rq uses the cid, it means the source cid does not need | |
11619 | * to be moved to the destination cpu. | |
11620 | */ | |
11621 | if (last_mm_cid == -1) | |
11622 | return -1; | |
11623 | src_cid = READ_ONCE(src_pcpu_cid->cid); | |
11624 | if (!mm_cid_is_valid(src_cid) || last_mm_cid != src_cid) | |
11625 | return -1; | |
11626 | ||
11627 | /* | |
11628 | * If we observe an active task using the mm on this rq, it means we | |
11629 | * are not the last task to be migrated from this cpu for this mm, so | |
11630 | * there is no need to move src_cid to the destination cpu. | |
11631 | */ | |
11632 | rcu_read_lock(); | |
11633 | src_task = rcu_dereference(src_rq->curr); | |
11634 | if (READ_ONCE(src_task->mm_cid_active) && src_task->mm == mm) { | |
11635 | rcu_read_unlock(); | |
11636 | t->last_mm_cid = -1; | |
11637 | return -1; | |
11638 | } | |
11639 | rcu_read_unlock(); | |
11640 | ||
11641 | return src_cid; | |
11642 | } | |
11643 | ||
11644 | static | |
11645 | int __sched_mm_cid_migrate_from_try_steal_cid(struct rq *src_rq, | |
11646 | struct task_struct *t, | |
11647 | struct mm_cid *src_pcpu_cid, | |
11648 | int src_cid) | |
11649 | { | |
11650 | struct task_struct *src_task; | |
11651 | struct mm_struct *mm = t->mm; | |
11652 | int lazy_cid; | |
11653 | ||
11654 | if (src_cid == -1) | |
11655 | return -1; | |
11656 | ||
11657 | /* | |
11658 | * Attempt to clear the source cpu cid to move it to the destination | |
11659 | * cpu. | |
11660 | */ | |
11661 | lazy_cid = mm_cid_set_lazy_put(src_cid); | |
11662 | if (!try_cmpxchg(&src_pcpu_cid->cid, &src_cid, lazy_cid)) | |
11663 | return -1; | |
11664 | ||
11665 | /* | |
11666 | * The implicit barrier after cmpxchg per-mm/cpu cid before loading | |
11667 | * rq->curr->mm matches the scheduler barrier in context_switch() | |
11668 | * between store to rq->curr and load of prev and next task's | |
11669 | * per-mm/cpu cid. | |
11670 | * | |
11671 | * The implicit barrier after cmpxchg per-mm/cpu cid before loading | |
11672 | * rq->curr->mm_cid_active matches the barrier in | |
11673 | * sched_mm_cid_exit_signals(), sched_mm_cid_before_execve(), and | |
11674 | * sched_mm_cid_after_execve() between store to t->mm_cid_active and | |
11675 | * load of per-mm/cpu cid. | |
11676 | */ | |
11677 | ||
11678 | /* | |
11679 | * If we observe an active task using the mm on this rq after setting | |
11680 | * the lazy-put flag, this task will be responsible for transitioning | |
11681 | * from lazy-put flag set to MM_CID_UNSET. | |
11682 | */ | |
11683 | rcu_read_lock(); | |
11684 | src_task = rcu_dereference(src_rq->curr); | |
11685 | if (READ_ONCE(src_task->mm_cid_active) && src_task->mm == mm) { | |
11686 | rcu_read_unlock(); | |
11687 | /* | |
11688 | * We observed an active task for this mm, there is therefore | |
11689 | * no point in moving this cid to the destination cpu. | |
11690 | */ | |
11691 | t->last_mm_cid = -1; | |
11692 | return -1; | |
11693 | } | |
11694 | rcu_read_unlock(); | |
11695 | ||
11696 | /* | |
11697 | * The src_cid is unused, so it can be unset. | |
11698 | */ | |
11699 | if (!try_cmpxchg(&src_pcpu_cid->cid, &lazy_cid, MM_CID_UNSET)) | |
11700 | return -1; | |
11701 | return src_cid; | |
11702 | } | |
11703 | ||
11704 | /* | |
11705 | * Migration to dst cpu. Called with dst_rq lock held. | |
11706 | * Interrupts are disabled, which keeps the window of cid ownership without the | |
11707 | * source rq lock held small. | |
11708 | */ | |
11709 | void sched_mm_cid_migrate_to(struct rq *dst_rq, struct task_struct *t) | |
11710 | { | |
11711 | struct mm_cid *src_pcpu_cid, *dst_pcpu_cid; | |
11712 | struct mm_struct *mm = t->mm; | |
11713 | int src_cid, dst_cid, src_cpu; | |
11714 | struct rq *src_rq; | |
11715 | ||
11716 | lockdep_assert_rq_held(dst_rq); | |
af7f588d MD |
11717 | |
11718 | if (!mm) | |
11719 | return; | |
223baf9d MD |
11720 | src_cpu = t->migrate_from_cpu; |
11721 | if (src_cpu == -1) { | |
11722 | t->last_mm_cid = -1; | |
11723 | return; | |
11724 | } | |
11725 | /* | |
11726 | * Move the src cid if the dst cid is unset. This keeps id | |
11727 | * allocation closest to 0 in cases where few threads migrate around | |
11728 | * many cpus. | |
11729 | * | |
11730 | * If destination cid is already set, we may have to just clear | |
11731 | * the src cid to ensure compactness in frequent migrations | |
11732 | * scenarios. | |
11733 | * | |
11734 | * It is not useful to clear the src cid when the number of threads is | |
11735 | * greater or equal to the number of allowed cpus, because user-space | |
11736 | * can expect that the number of allowed cids can reach the number of | |
11737 | * allowed cpus. | |
11738 | */ | |
11739 | dst_pcpu_cid = per_cpu_ptr(mm->pcpu_cid, cpu_of(dst_rq)); | |
11740 | dst_cid = READ_ONCE(dst_pcpu_cid->cid); | |
11741 | if (!mm_cid_is_unset(dst_cid) && | |
11742 | atomic_read(&mm->mm_users) >= t->nr_cpus_allowed) | |
11743 | return; | |
11744 | src_pcpu_cid = per_cpu_ptr(mm->pcpu_cid, src_cpu); | |
11745 | src_rq = cpu_rq(src_cpu); | |
11746 | src_cid = __sched_mm_cid_migrate_from_fetch_cid(src_rq, t, src_pcpu_cid); | |
11747 | if (src_cid == -1) | |
11748 | return; | |
11749 | src_cid = __sched_mm_cid_migrate_from_try_steal_cid(src_rq, t, src_pcpu_cid, | |
11750 | src_cid); | |
11751 | if (src_cid == -1) | |
11752 | return; | |
11753 | if (!mm_cid_is_unset(dst_cid)) { | |
11754 | __mm_cid_put(mm, src_cid); | |
11755 | return; | |
11756 | } | |
11757 | /* Move src_cid to dst cpu. */ | |
11758 | mm_cid_snapshot_time(dst_rq, mm); | |
11759 | WRITE_ONCE(dst_pcpu_cid->cid, src_cid); | |
11760 | } | |
11761 | ||
11762 | static void sched_mm_cid_remote_clear(struct mm_struct *mm, struct mm_cid *pcpu_cid, | |
11763 | int cpu) | |
11764 | { | |
11765 | struct rq *rq = cpu_rq(cpu); | |
11766 | struct task_struct *t; | |
11767 | unsigned long flags; | |
11768 | int cid, lazy_cid; | |
11769 | ||
11770 | cid = READ_ONCE(pcpu_cid->cid); | |
11771 | if (!mm_cid_is_valid(cid)) | |
af7f588d | 11772 | return; |
223baf9d MD |
11773 | |
11774 | /* | |
11775 | * Clear the cpu cid if it is set to keep cid allocation compact. If | |
11776 | * there happens to be other tasks left on the source cpu using this | |
11777 | * mm, the next task using this mm will reallocate its cid on context | |
11778 | * switch. | |
11779 | */ | |
11780 | lazy_cid = mm_cid_set_lazy_put(cid); | |
11781 | if (!try_cmpxchg(&pcpu_cid->cid, &cid, lazy_cid)) | |
11782 | return; | |
11783 | ||
11784 | /* | |
11785 | * The implicit barrier after cmpxchg per-mm/cpu cid before loading | |
11786 | * rq->curr->mm matches the scheduler barrier in context_switch() | |
11787 | * between store to rq->curr and load of prev and next task's | |
11788 | * per-mm/cpu cid. | |
11789 | * | |
11790 | * The implicit barrier after cmpxchg per-mm/cpu cid before loading | |
11791 | * rq->curr->mm_cid_active matches the barrier in | |
11792 | * sched_mm_cid_exit_signals(), sched_mm_cid_before_execve(), and | |
11793 | * sched_mm_cid_after_execve() between store to t->mm_cid_active and | |
11794 | * load of per-mm/cpu cid. | |
11795 | */ | |
11796 | ||
11797 | /* | |
11798 | * If we observe an active task using the mm on this rq after setting | |
11799 | * the lazy-put flag, that task will be responsible for transitioning | |
11800 | * from lazy-put flag set to MM_CID_UNSET. | |
11801 | */ | |
11802 | rcu_read_lock(); | |
11803 | t = rcu_dereference(rq->curr); | |
11804 | if (READ_ONCE(t->mm_cid_active) && t->mm == mm) { | |
11805 | rcu_read_unlock(); | |
11806 | return; | |
11807 | } | |
11808 | rcu_read_unlock(); | |
11809 | ||
11810 | /* | |
11811 | * The cid is unused, so it can be unset. | |
11812 | * Disable interrupts to keep the window of cid ownership without rq | |
11813 | * lock small. | |
11814 | */ | |
af7f588d | 11815 | local_irq_save(flags); |
223baf9d MD |
11816 | if (try_cmpxchg(&pcpu_cid->cid, &lazy_cid, MM_CID_UNSET)) |
11817 | __mm_cid_put(mm, cid); | |
af7f588d MD |
11818 | local_irq_restore(flags); |
11819 | } | |
11820 | ||
223baf9d MD |
11821 | static void sched_mm_cid_remote_clear_old(struct mm_struct *mm, int cpu) |
11822 | { | |
11823 | struct rq *rq = cpu_rq(cpu); | |
11824 | struct mm_cid *pcpu_cid; | |
11825 | struct task_struct *curr; | |
11826 | u64 rq_clock; | |
11827 | ||
11828 | /* | |
11829 | * rq->clock load is racy on 32-bit but one spurious clear once in a | |
11830 | * while is irrelevant. | |
11831 | */ | |
11832 | rq_clock = READ_ONCE(rq->clock); | |
11833 | pcpu_cid = per_cpu_ptr(mm->pcpu_cid, cpu); | |
11834 | ||
11835 | /* | |
11836 | * In order to take care of infrequently scheduled tasks, bump the time | |
11837 | * snapshot associated with this cid if an active task using the mm is | |
11838 | * observed on this rq. | |
11839 | */ | |
11840 | rcu_read_lock(); | |
11841 | curr = rcu_dereference(rq->curr); | |
11842 | if (READ_ONCE(curr->mm_cid_active) && curr->mm == mm) { | |
11843 | WRITE_ONCE(pcpu_cid->time, rq_clock); | |
11844 | rcu_read_unlock(); | |
11845 | return; | |
11846 | } | |
11847 | rcu_read_unlock(); | |
11848 | ||
11849 | if (rq_clock < pcpu_cid->time + SCHED_MM_CID_PERIOD_NS) | |
11850 | return; | |
11851 | sched_mm_cid_remote_clear(mm, pcpu_cid, cpu); | |
11852 | } | |
11853 | ||
11854 | static void sched_mm_cid_remote_clear_weight(struct mm_struct *mm, int cpu, | |
11855 | int weight) | |
11856 | { | |
11857 | struct mm_cid *pcpu_cid; | |
11858 | int cid; | |
11859 | ||
11860 | pcpu_cid = per_cpu_ptr(mm->pcpu_cid, cpu); | |
11861 | cid = READ_ONCE(pcpu_cid->cid); | |
11862 | if (!mm_cid_is_valid(cid) || cid < weight) | |
11863 | return; | |
11864 | sched_mm_cid_remote_clear(mm, pcpu_cid, cpu); | |
11865 | } | |
11866 | ||
11867 | static void task_mm_cid_work(struct callback_head *work) | |
11868 | { | |
11869 | unsigned long now = jiffies, old_scan, next_scan; | |
11870 | struct task_struct *t = current; | |
11871 | struct cpumask *cidmask; | |
11872 | struct mm_struct *mm; | |
11873 | int weight, cpu; | |
11874 | ||
11875 | SCHED_WARN_ON(t != container_of(work, struct task_struct, cid_work)); | |
11876 | ||
11877 | work->next = work; /* Prevent double-add */ | |
11878 | if (t->flags & PF_EXITING) | |
11879 | return; | |
11880 | mm = t->mm; | |
11881 | if (!mm) | |
11882 | return; | |
11883 | old_scan = READ_ONCE(mm->mm_cid_next_scan); | |
11884 | next_scan = now + msecs_to_jiffies(MM_CID_SCAN_DELAY); | |
11885 | if (!old_scan) { | |
11886 | unsigned long res; | |
11887 | ||
11888 | res = cmpxchg(&mm->mm_cid_next_scan, old_scan, next_scan); | |
11889 | if (res != old_scan) | |
11890 | old_scan = res; | |
11891 | else | |
11892 | old_scan = next_scan; | |
11893 | } | |
11894 | if (time_before(now, old_scan)) | |
11895 | return; | |
11896 | if (!try_cmpxchg(&mm->mm_cid_next_scan, &old_scan, next_scan)) | |
11897 | return; | |
11898 | cidmask = mm_cidmask(mm); | |
11899 | /* Clear cids that were not recently used. */ | |
11900 | for_each_possible_cpu(cpu) | |
11901 | sched_mm_cid_remote_clear_old(mm, cpu); | |
11902 | weight = cpumask_weight(cidmask); | |
11903 | /* | |
11904 | * Clear cids that are greater or equal to the cidmask weight to | |
11905 | * recompact it. | |
11906 | */ | |
11907 | for_each_possible_cpu(cpu) | |
11908 | sched_mm_cid_remote_clear_weight(mm, cpu, weight); | |
11909 | } | |
11910 | ||
11911 | void init_sched_mm_cid(struct task_struct *t) | |
11912 | { | |
11913 | struct mm_struct *mm = t->mm; | |
11914 | int mm_users = 0; | |
11915 | ||
11916 | if (mm) { | |
11917 | mm_users = atomic_read(&mm->mm_users); | |
11918 | if (mm_users == 1) | |
11919 | mm->mm_cid_next_scan = jiffies + msecs_to_jiffies(MM_CID_SCAN_DELAY); | |
11920 | } | |
11921 | t->cid_work.next = &t->cid_work; /* Protect against double add */ | |
11922 | init_task_work(&t->cid_work, task_mm_cid_work); | |
11923 | } | |
11924 | ||
11925 | void task_tick_mm_cid(struct rq *rq, struct task_struct *curr) | |
11926 | { | |
11927 | struct callback_head *work = &curr->cid_work; | |
11928 | unsigned long now = jiffies; | |
11929 | ||
11930 | if (!curr->mm || (curr->flags & (PF_EXITING | PF_KTHREAD)) || | |
11931 | work->next != work) | |
11932 | return; | |
11933 | if (time_before(now, READ_ONCE(curr->mm->mm_cid_next_scan))) | |
11934 | return; | |
11935 | task_work_add(curr, work, TWA_RESUME); | |
11936 | } | |
11937 | ||
11938 | void sched_mm_cid_exit_signals(struct task_struct *t) | |
11939 | { | |
11940 | struct mm_struct *mm = t->mm; | |
11941 | struct rq_flags rf; | |
11942 | struct rq *rq; | |
11943 | ||
11944 | if (!mm) | |
11945 | return; | |
11946 | ||
11947 | preempt_disable(); | |
11948 | rq = this_rq(); | |
11949 | rq_lock_irqsave(rq, &rf); | |
11950 | preempt_enable_no_resched(); /* holding spinlock */ | |
11951 | WRITE_ONCE(t->mm_cid_active, 0); | |
11952 | /* | |
11953 | * Store t->mm_cid_active before loading per-mm/cpu cid. | |
11954 | * Matches barrier in sched_mm_cid_remote_clear_old(). | |
11955 | */ | |
11956 | smp_mb(); | |
11957 | mm_cid_put(mm); | |
11958 | t->last_mm_cid = t->mm_cid = -1; | |
11959 | rq_unlock_irqrestore(rq, &rf); | |
11960 | } | |
11961 | ||
af7f588d MD |
11962 | void sched_mm_cid_before_execve(struct task_struct *t) |
11963 | { | |
11964 | struct mm_struct *mm = t->mm; | |
223baf9d MD |
11965 | struct rq_flags rf; |
11966 | struct rq *rq; | |
af7f588d MD |
11967 | |
11968 | if (!mm) | |
11969 | return; | |
223baf9d MD |
11970 | |
11971 | preempt_disable(); | |
11972 | rq = this_rq(); | |
11973 | rq_lock_irqsave(rq, &rf); | |
11974 | preempt_enable_no_resched(); /* holding spinlock */ | |
11975 | WRITE_ONCE(t->mm_cid_active, 0); | |
11976 | /* | |
11977 | * Store t->mm_cid_active before loading per-mm/cpu cid. | |
11978 | * Matches barrier in sched_mm_cid_remote_clear_old(). | |
11979 | */ | |
11980 | smp_mb(); | |
11981 | mm_cid_put(mm); | |
11982 | t->last_mm_cid = t->mm_cid = -1; | |
11983 | rq_unlock_irqrestore(rq, &rf); | |
af7f588d MD |
11984 | } |
11985 | ||
11986 | void sched_mm_cid_after_execve(struct task_struct *t) | |
11987 | { | |
11988 | struct mm_struct *mm = t->mm; | |
223baf9d MD |
11989 | struct rq_flags rf; |
11990 | struct rq *rq; | |
af7f588d | 11991 | |
bbd0b031 MD |
11992 | if (!mm) |
11993 | return; | |
223baf9d MD |
11994 | |
11995 | preempt_disable(); | |
11996 | rq = this_rq(); | |
11997 | rq_lock_irqsave(rq, &rf); | |
11998 | preempt_enable_no_resched(); /* holding spinlock */ | |
11999 | WRITE_ONCE(t->mm_cid_active, 1); | |
12000 | /* | |
12001 | * Store t->mm_cid_active before loading per-mm/cpu cid. | |
12002 | * Matches barrier in sched_mm_cid_remote_clear_old(). | |
12003 | */ | |
12004 | smp_mb(); | |
12005 | t->last_mm_cid = t->mm_cid = mm_cid_get(rq, mm); | |
12006 | rq_unlock_irqrestore(rq, &rf); | |
af7f588d MD |
12007 | rseq_set_notify_resume(t); |
12008 | } | |
12009 | ||
12010 | void sched_mm_cid_fork(struct task_struct *t) | |
12011 | { | |
bbd0b031 | 12012 | WARN_ON_ONCE(!t->mm || t->mm_cid != -1); |
af7f588d MD |
12013 | t->mm_cid_active = 1; |
12014 | } | |
12015 | #endif |