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 | */ |
9d246053 PA |
9 | #define CREATE_TRACE_POINTS |
10 | #include <trace/events/sched.h> | |
11 | #undef CREATE_TRACE_POINTS | |
12 | ||
325ea10c | 13 | #include "sched.h" |
1da177e4 | 14 | |
7281c8de | 15 | #include <linux/nospec.h> |
85f1abe0 | 16 | |
0ed557aa | 17 | #include <linux/kcov.h> |
d08b9f0c | 18 | #include <linux/scs.h> |
0ed557aa | 19 | |
96f951ed | 20 | #include <asm/switch_to.h> |
5517d86b | 21 | #include <asm/tlb.h> |
1da177e4 | 22 | |
ea138446 | 23 | #include "../workqueue_internal.h" |
771b53d0 | 24 | #include "../../fs/io-wq.h" |
29d5e047 | 25 | #include "../smpboot.h" |
6e0534f2 | 26 | |
91c27493 | 27 | #include "pelt.h" |
1f8db415 | 28 | #include "smp.h" |
91c27493 | 29 | |
a056a5be QY |
30 | /* |
31 | * Export tracepoints that act as a bare tracehook (ie: have no trace event | |
32 | * associated with them) to allow external modules to probe them. | |
33 | */ | |
34 | EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_cfs_tp); | |
35 | EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_rt_tp); | |
36 | EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_dl_tp); | |
37 | EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_irq_tp); | |
38 | EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_se_tp); | |
51cf18c9 | 39 | EXPORT_TRACEPOINT_SYMBOL_GPL(sched_cpu_capacity_tp); |
a056a5be | 40 | EXPORT_TRACEPOINT_SYMBOL_GPL(sched_overutilized_tp); |
4581bea8 VD |
41 | EXPORT_TRACEPOINT_SYMBOL_GPL(sched_util_est_cfs_tp); |
42 | EXPORT_TRACEPOINT_SYMBOL_GPL(sched_util_est_se_tp); | |
9d246053 | 43 | EXPORT_TRACEPOINT_SYMBOL_GPL(sched_update_nr_running_tp); |
a056a5be | 44 | |
029632fb | 45 | DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); |
dc61b1d6 | 46 | |
a73f863a | 47 | #ifdef CONFIG_SCHED_DEBUG |
bf5c91ba IM |
48 | /* |
49 | * Debugging: various feature bits | |
765cc3a4 PB |
50 | * |
51 | * If SCHED_DEBUG is disabled, each compilation unit has its own copy of | |
52 | * sysctl_sched_features, defined in sched.h, to allow constants propagation | |
53 | * at compile time and compiler optimization based on features default. | |
bf5c91ba | 54 | */ |
f00b45c1 PZ |
55 | #define SCHED_FEAT(name, enabled) \ |
56 | (1UL << __SCHED_FEAT_##name) * enabled | | |
bf5c91ba | 57 | const_debug unsigned int sysctl_sched_features = |
391e43da | 58 | #include "features.h" |
f00b45c1 | 59 | 0; |
f00b45c1 | 60 | #undef SCHED_FEAT |
c006fac5 PT |
61 | |
62 | /* | |
63 | * Print a warning if need_resched is set for the given duration (if | |
64 | * LATENCY_WARN is enabled). | |
65 | * | |
66 | * If sysctl_resched_latency_warn_once is set, only one warning will be shown | |
67 | * per boot. | |
68 | */ | |
69 | __read_mostly int sysctl_resched_latency_warn_ms = 100; | |
70 | __read_mostly int sysctl_resched_latency_warn_once = 1; | |
71 | #endif /* CONFIG_SCHED_DEBUG */ | |
f00b45c1 | 72 | |
b82d9fdd PZ |
73 | /* |
74 | * Number of tasks to iterate in a single balance run. | |
75 | * Limited because this is done with IRQs disabled. | |
76 | */ | |
77 | const_debug unsigned int sysctl_sched_nr_migrate = 32; | |
78 | ||
fa85ae24 | 79 | /* |
d1ccc66d | 80 | * period over which we measure -rt task CPU usage in us. |
fa85ae24 PZ |
81 | * default: 1s |
82 | */ | |
9f0c1e56 | 83 | unsigned int sysctl_sched_rt_period = 1000000; |
fa85ae24 | 84 | |
029632fb | 85 | __read_mostly int scheduler_running; |
6892b75e | 86 | |
9edeaea1 PZ |
87 | #ifdef CONFIG_SCHED_CORE |
88 | ||
89 | DEFINE_STATIC_KEY_FALSE(__sched_core_enabled); | |
90 | ||
8a311c74 PZ |
91 | /* kernel prio, less is more */ |
92 | static inline int __task_prio(struct task_struct *p) | |
93 | { | |
94 | if (p->sched_class == &stop_sched_class) /* trumps deadline */ | |
95 | return -2; | |
96 | ||
97 | if (rt_prio(p->prio)) /* includes deadline */ | |
98 | return p->prio; /* [-1, 99] */ | |
99 | ||
100 | if (p->sched_class == &idle_sched_class) | |
101 | return MAX_RT_PRIO + NICE_WIDTH; /* 140 */ | |
102 | ||
103 | return MAX_RT_PRIO + MAX_NICE; /* 120, squash fair */ | |
104 | } | |
105 | ||
106 | /* | |
107 | * l(a,b) | |
108 | * le(a,b) := !l(b,a) | |
109 | * g(a,b) := l(b,a) | |
110 | * ge(a,b) := !l(a,b) | |
111 | */ | |
112 | ||
113 | /* real prio, less is less */ | |
c6047c2e | 114 | static inline bool prio_less(struct task_struct *a, struct task_struct *b, bool in_fi) |
8a311c74 PZ |
115 | { |
116 | ||
117 | int pa = __task_prio(a), pb = __task_prio(b); | |
118 | ||
119 | if (-pa < -pb) | |
120 | return true; | |
121 | ||
122 | if (-pb < -pa) | |
123 | return false; | |
124 | ||
125 | if (pa == -1) /* dl_prio() doesn't work because of stop_class above */ | |
126 | return !dl_time_before(a->dl.deadline, b->dl.deadline); | |
127 | ||
c6047c2e JFG |
128 | if (pa == MAX_RT_PRIO + MAX_NICE) /* fair */ |
129 | return cfs_prio_less(a, b, in_fi); | |
8a311c74 PZ |
130 | |
131 | return false; | |
132 | } | |
133 | ||
134 | static inline bool __sched_core_less(struct task_struct *a, struct task_struct *b) | |
135 | { | |
136 | if (a->core_cookie < b->core_cookie) | |
137 | return true; | |
138 | ||
139 | if (a->core_cookie > b->core_cookie) | |
140 | return false; | |
141 | ||
142 | /* flip prio, so high prio is leftmost */ | |
c6047c2e | 143 | if (prio_less(b, a, task_rq(a)->core->core_forceidle)) |
8a311c74 PZ |
144 | return true; |
145 | ||
146 | return false; | |
147 | } | |
148 | ||
149 | #define __node_2_sc(node) rb_entry((node), struct task_struct, core_node) | |
150 | ||
151 | static inline bool rb_sched_core_less(struct rb_node *a, const struct rb_node *b) | |
152 | { | |
153 | return __sched_core_less(__node_2_sc(a), __node_2_sc(b)); | |
154 | } | |
155 | ||
156 | static inline int rb_sched_core_cmp(const void *key, const struct rb_node *node) | |
157 | { | |
158 | const struct task_struct *p = __node_2_sc(node); | |
159 | unsigned long cookie = (unsigned long)key; | |
160 | ||
161 | if (cookie < p->core_cookie) | |
162 | return -1; | |
163 | ||
164 | if (cookie > p->core_cookie) | |
165 | return 1; | |
166 | ||
167 | return 0; | |
168 | } | |
169 | ||
6e33cad0 | 170 | void sched_core_enqueue(struct rq *rq, struct task_struct *p) |
8a311c74 PZ |
171 | { |
172 | rq->core->core_task_seq++; | |
173 | ||
174 | if (!p->core_cookie) | |
175 | return; | |
176 | ||
177 | rb_add(&p->core_node, &rq->core_tree, rb_sched_core_less); | |
178 | } | |
179 | ||
6e33cad0 | 180 | void sched_core_dequeue(struct rq *rq, struct task_struct *p) |
8a311c74 PZ |
181 | { |
182 | rq->core->core_task_seq++; | |
183 | ||
6e33cad0 | 184 | if (!sched_core_enqueued(p)) |
8a311c74 PZ |
185 | return; |
186 | ||
187 | rb_erase(&p->core_node, &rq->core_tree); | |
6e33cad0 | 188 | RB_CLEAR_NODE(&p->core_node); |
8a311c74 PZ |
189 | } |
190 | ||
191 | /* | |
192 | * Find left-most (aka, highest priority) task matching @cookie. | |
193 | */ | |
194 | static struct task_struct *sched_core_find(struct rq *rq, unsigned long cookie) | |
195 | { | |
196 | struct rb_node *node; | |
197 | ||
198 | node = rb_find_first((void *)cookie, &rq->core_tree, rb_sched_core_cmp); | |
199 | /* | |
200 | * The idle task always matches any cookie! | |
201 | */ | |
202 | if (!node) | |
203 | return idle_sched_class.pick_task(rq); | |
204 | ||
205 | return __node_2_sc(node); | |
206 | } | |
207 | ||
d2dfa17b PZ |
208 | static struct task_struct *sched_core_next(struct task_struct *p, unsigned long cookie) |
209 | { | |
210 | struct rb_node *node = &p->core_node; | |
211 | ||
212 | node = rb_next(node); | |
213 | if (!node) | |
214 | return NULL; | |
215 | ||
216 | p = container_of(node, struct task_struct, core_node); | |
217 | if (p->core_cookie != cookie) | |
218 | return NULL; | |
219 | ||
220 | return p; | |
221 | } | |
222 | ||
9edeaea1 PZ |
223 | /* |
224 | * Magic required such that: | |
225 | * | |
226 | * raw_spin_rq_lock(rq); | |
227 | * ... | |
228 | * raw_spin_rq_unlock(rq); | |
229 | * | |
230 | * ends up locking and unlocking the _same_ lock, and all CPUs | |
231 | * always agree on what rq has what lock. | |
232 | * | |
233 | * XXX entirely possible to selectively enable cores, don't bother for now. | |
234 | */ | |
235 | ||
236 | static DEFINE_MUTEX(sched_core_mutex); | |
875feb41 | 237 | static atomic_t sched_core_count; |
9edeaea1 PZ |
238 | static struct cpumask sched_core_mask; |
239 | ||
240 | static void __sched_core_flip(bool enabled) | |
241 | { | |
242 | int cpu, t, i; | |
243 | ||
244 | cpus_read_lock(); | |
245 | ||
246 | /* | |
247 | * Toggle the online cores, one by one. | |
248 | */ | |
249 | cpumask_copy(&sched_core_mask, cpu_online_mask); | |
250 | for_each_cpu(cpu, &sched_core_mask) { | |
251 | const struct cpumask *smt_mask = cpu_smt_mask(cpu); | |
252 | ||
253 | i = 0; | |
254 | local_irq_disable(); | |
255 | for_each_cpu(t, smt_mask) { | |
256 | /* supports up to SMT8 */ | |
257 | raw_spin_lock_nested(&cpu_rq(t)->__lock, i++); | |
258 | } | |
259 | ||
260 | for_each_cpu(t, smt_mask) | |
261 | cpu_rq(t)->core_enabled = enabled; | |
262 | ||
263 | for_each_cpu(t, smt_mask) | |
264 | raw_spin_unlock(&cpu_rq(t)->__lock); | |
265 | local_irq_enable(); | |
266 | ||
267 | cpumask_andnot(&sched_core_mask, &sched_core_mask, smt_mask); | |
268 | } | |
269 | ||
270 | /* | |
271 | * Toggle the offline CPUs. | |
272 | */ | |
273 | cpumask_copy(&sched_core_mask, cpu_possible_mask); | |
274 | cpumask_andnot(&sched_core_mask, &sched_core_mask, cpu_online_mask); | |
275 | ||
276 | for_each_cpu(cpu, &sched_core_mask) | |
277 | cpu_rq(cpu)->core_enabled = enabled; | |
278 | ||
279 | cpus_read_unlock(); | |
280 | } | |
281 | ||
8a311c74 | 282 | static void sched_core_assert_empty(void) |
9edeaea1 | 283 | { |
8a311c74 | 284 | int cpu; |
9edeaea1 | 285 | |
8a311c74 PZ |
286 | for_each_possible_cpu(cpu) |
287 | WARN_ON_ONCE(!RB_EMPTY_ROOT(&cpu_rq(cpu)->core_tree)); | |
288 | } | |
289 | ||
290 | static void __sched_core_enable(void) | |
291 | { | |
9edeaea1 PZ |
292 | static_branch_enable(&__sched_core_enabled); |
293 | /* | |
294 | * Ensure all previous instances of raw_spin_rq_*lock() have finished | |
295 | * and future ones will observe !sched_core_disabled(). | |
296 | */ | |
297 | synchronize_rcu(); | |
298 | __sched_core_flip(true); | |
8a311c74 | 299 | sched_core_assert_empty(); |
9edeaea1 PZ |
300 | } |
301 | ||
302 | static void __sched_core_disable(void) | |
303 | { | |
8a311c74 | 304 | sched_core_assert_empty(); |
9edeaea1 PZ |
305 | __sched_core_flip(false); |
306 | static_branch_disable(&__sched_core_enabled); | |
307 | } | |
308 | ||
309 | void sched_core_get(void) | |
310 | { | |
875feb41 PZ |
311 | if (atomic_inc_not_zero(&sched_core_count)) |
312 | return; | |
313 | ||
9edeaea1 | 314 | mutex_lock(&sched_core_mutex); |
875feb41 | 315 | if (!atomic_read(&sched_core_count)) |
9edeaea1 | 316 | __sched_core_enable(); |
875feb41 PZ |
317 | |
318 | smp_mb__before_atomic(); | |
319 | atomic_inc(&sched_core_count); | |
9edeaea1 PZ |
320 | mutex_unlock(&sched_core_mutex); |
321 | } | |
322 | ||
875feb41 | 323 | static void __sched_core_put(struct work_struct *work) |
9edeaea1 | 324 | { |
875feb41 | 325 | if (atomic_dec_and_mutex_lock(&sched_core_count, &sched_core_mutex)) { |
9edeaea1 | 326 | __sched_core_disable(); |
875feb41 PZ |
327 | mutex_unlock(&sched_core_mutex); |
328 | } | |
329 | } | |
330 | ||
331 | void sched_core_put(void) | |
332 | { | |
333 | static DECLARE_WORK(_work, __sched_core_put); | |
334 | ||
335 | /* | |
336 | * "There can be only one" | |
337 | * | |
338 | * Either this is the last one, or we don't actually need to do any | |
339 | * 'work'. If it is the last *again*, we rely on | |
340 | * WORK_STRUCT_PENDING_BIT. | |
341 | */ | |
342 | if (!atomic_add_unless(&sched_core_count, -1, 1)) | |
343 | schedule_work(&_work); | |
9edeaea1 PZ |
344 | } |
345 | ||
8a311c74 PZ |
346 | #else /* !CONFIG_SCHED_CORE */ |
347 | ||
348 | static inline void sched_core_enqueue(struct rq *rq, struct task_struct *p) { } | |
349 | static inline void sched_core_dequeue(struct rq *rq, struct task_struct *p) { } | |
350 | ||
9edeaea1 PZ |
351 | #endif /* CONFIG_SCHED_CORE */ |
352 | ||
9f0c1e56 PZ |
353 | /* |
354 | * part of the period that we allow rt tasks to run in us. | |
355 | * default: 0.95s | |
356 | */ | |
357 | int sysctl_sched_rt_runtime = 950000; | |
fa85ae24 | 358 | |
58877d34 PZ |
359 | |
360 | /* | |
361 | * Serialization rules: | |
362 | * | |
363 | * Lock order: | |
364 | * | |
365 | * p->pi_lock | |
366 | * rq->lock | |
367 | * hrtimer_cpu_base->lock (hrtimer_start() for bandwidth controls) | |
368 | * | |
369 | * rq1->lock | |
370 | * rq2->lock where: rq1 < rq2 | |
371 | * | |
372 | * Regular state: | |
373 | * | |
374 | * Normal scheduling state is serialized by rq->lock. __schedule() takes the | |
375 | * local CPU's rq->lock, it optionally removes the task from the runqueue and | |
b19a888c | 376 | * always looks at the local rq data structures to find the most eligible task |
58877d34 PZ |
377 | * to run next. |
378 | * | |
379 | * Task enqueue is also under rq->lock, possibly taken from another CPU. | |
380 | * Wakeups from another LLC domain might use an IPI to transfer the enqueue to | |
381 | * the local CPU to avoid bouncing the runqueue state around [ see | |
382 | * ttwu_queue_wakelist() ] | |
383 | * | |
384 | * Task wakeup, specifically wakeups that involve migration, are horribly | |
385 | * complicated to avoid having to take two rq->locks. | |
386 | * | |
387 | * Special state: | |
388 | * | |
389 | * System-calls and anything external will use task_rq_lock() which acquires | |
390 | * both p->pi_lock and rq->lock. As a consequence the state they change is | |
391 | * stable while holding either lock: | |
392 | * | |
393 | * - sched_setaffinity()/ | |
394 | * set_cpus_allowed_ptr(): p->cpus_ptr, p->nr_cpus_allowed | |
395 | * - set_user_nice(): p->se.load, p->*prio | |
396 | * - __sched_setscheduler(): p->sched_class, p->policy, p->*prio, | |
397 | * p->se.load, p->rt_priority, | |
398 | * p->dl.dl_{runtime, deadline, period, flags, bw, density} | |
399 | * - sched_setnuma(): p->numa_preferred_nid | |
400 | * - sched_move_task()/ | |
401 | * cpu_cgroup_fork(): p->sched_task_group | |
402 | * - uclamp_update_active() p->uclamp* | |
403 | * | |
404 | * p->state <- TASK_*: | |
405 | * | |
406 | * is changed locklessly using set_current_state(), __set_current_state() or | |
407 | * set_special_state(), see their respective comments, or by | |
408 | * try_to_wake_up(). This latter uses p->pi_lock to serialize against | |
409 | * concurrent self. | |
410 | * | |
411 | * p->on_rq <- { 0, 1 = TASK_ON_RQ_QUEUED, 2 = TASK_ON_RQ_MIGRATING }: | |
412 | * | |
413 | * is set by activate_task() and cleared by deactivate_task(), under | |
414 | * rq->lock. Non-zero indicates the task is runnable, the special | |
415 | * ON_RQ_MIGRATING state is used for migration without holding both | |
416 | * rq->locks. It indicates task_cpu() is not stable, see task_rq_lock(). | |
417 | * | |
418 | * p->on_cpu <- { 0, 1 }: | |
419 | * | |
420 | * is set by prepare_task() and cleared by finish_task() such that it will be | |
421 | * set before p is scheduled-in and cleared after p is scheduled-out, both | |
422 | * under rq->lock. Non-zero indicates the task is running on its CPU. | |
423 | * | |
424 | * [ The astute reader will observe that it is possible for two tasks on one | |
425 | * CPU to have ->on_cpu = 1 at the same time. ] | |
426 | * | |
427 | * task_cpu(p): is changed by set_task_cpu(), the rules are: | |
428 | * | |
429 | * - Don't call set_task_cpu() on a blocked task: | |
430 | * | |
431 | * We don't care what CPU we're not running on, this simplifies hotplug, | |
432 | * the CPU assignment of blocked tasks isn't required to be valid. | |
433 | * | |
434 | * - for try_to_wake_up(), called under p->pi_lock: | |
435 | * | |
436 | * This allows try_to_wake_up() to only take one rq->lock, see its comment. | |
437 | * | |
438 | * - for migration called under rq->lock: | |
439 | * [ see task_on_rq_migrating() in task_rq_lock() ] | |
440 | * | |
441 | * o move_queued_task() | |
442 | * o detach_task() | |
443 | * | |
444 | * - for migration called under double_rq_lock(): | |
445 | * | |
446 | * o __migrate_swap_task() | |
447 | * o push_rt_task() / pull_rt_task() | |
448 | * o push_dl_task() / pull_dl_task() | |
449 | * o dl_task_offline_migration() | |
450 | * | |
451 | */ | |
452 | ||
39d371b7 PZ |
453 | void raw_spin_rq_lock_nested(struct rq *rq, int subclass) |
454 | { | |
d66f1b06 PZ |
455 | raw_spinlock_t *lock; |
456 | ||
9edeaea1 PZ |
457 | /* Matches synchronize_rcu() in __sched_core_enable() */ |
458 | preempt_disable(); | |
d66f1b06 PZ |
459 | if (sched_core_disabled()) { |
460 | raw_spin_lock_nested(&rq->__lock, subclass); | |
9edeaea1 PZ |
461 | /* preempt_count *MUST* be > 1 */ |
462 | preempt_enable_no_resched(); | |
d66f1b06 PZ |
463 | return; |
464 | } | |
465 | ||
466 | for (;;) { | |
9ef7e7e3 | 467 | lock = __rq_lockp(rq); |
d66f1b06 | 468 | raw_spin_lock_nested(lock, subclass); |
9ef7e7e3 | 469 | if (likely(lock == __rq_lockp(rq))) { |
9edeaea1 PZ |
470 | /* preempt_count *MUST* be > 1 */ |
471 | preempt_enable_no_resched(); | |
d66f1b06 | 472 | return; |
9edeaea1 | 473 | } |
d66f1b06 PZ |
474 | raw_spin_unlock(lock); |
475 | } | |
39d371b7 PZ |
476 | } |
477 | ||
478 | bool raw_spin_rq_trylock(struct rq *rq) | |
479 | { | |
d66f1b06 PZ |
480 | raw_spinlock_t *lock; |
481 | bool ret; | |
482 | ||
9edeaea1 PZ |
483 | /* Matches synchronize_rcu() in __sched_core_enable() */ |
484 | preempt_disable(); | |
485 | if (sched_core_disabled()) { | |
486 | ret = raw_spin_trylock(&rq->__lock); | |
487 | preempt_enable(); | |
488 | return ret; | |
489 | } | |
d66f1b06 PZ |
490 | |
491 | for (;;) { | |
9ef7e7e3 | 492 | lock = __rq_lockp(rq); |
d66f1b06 | 493 | ret = raw_spin_trylock(lock); |
9ef7e7e3 | 494 | if (!ret || (likely(lock == __rq_lockp(rq)))) { |
9edeaea1 | 495 | preempt_enable(); |
d66f1b06 | 496 | return ret; |
9edeaea1 | 497 | } |
d66f1b06 PZ |
498 | raw_spin_unlock(lock); |
499 | } | |
39d371b7 PZ |
500 | } |
501 | ||
502 | void raw_spin_rq_unlock(struct rq *rq) | |
503 | { | |
504 | raw_spin_unlock(rq_lockp(rq)); | |
505 | } | |
506 | ||
d66f1b06 PZ |
507 | #ifdef CONFIG_SMP |
508 | /* | |
509 | * double_rq_lock - safely lock two runqueues | |
510 | */ | |
511 | void double_rq_lock(struct rq *rq1, struct rq *rq2) | |
512 | { | |
513 | lockdep_assert_irqs_disabled(); | |
514 | ||
515 | if (rq_order_less(rq2, rq1)) | |
516 | swap(rq1, rq2); | |
517 | ||
518 | raw_spin_rq_lock(rq1); | |
9ef7e7e3 | 519 | if (__rq_lockp(rq1) == __rq_lockp(rq2)) |
d66f1b06 PZ |
520 | return; |
521 | ||
522 | raw_spin_rq_lock_nested(rq2, SINGLE_DEPTH_NESTING); | |
523 | } | |
524 | #endif | |
525 | ||
3e71a462 PZ |
526 | /* |
527 | * __task_rq_lock - lock the rq @p resides on. | |
528 | */ | |
eb580751 | 529 | struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf) |
3e71a462 PZ |
530 | __acquires(rq->lock) |
531 | { | |
532 | struct rq *rq; | |
533 | ||
534 | lockdep_assert_held(&p->pi_lock); | |
535 | ||
536 | for (;;) { | |
537 | rq = task_rq(p); | |
5cb9eaa3 | 538 | raw_spin_rq_lock(rq); |
3e71a462 | 539 | if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) { |
d8ac8971 | 540 | rq_pin_lock(rq, rf); |
3e71a462 PZ |
541 | return rq; |
542 | } | |
5cb9eaa3 | 543 | raw_spin_rq_unlock(rq); |
3e71a462 PZ |
544 | |
545 | while (unlikely(task_on_rq_migrating(p))) | |
546 | cpu_relax(); | |
547 | } | |
548 | } | |
549 | ||
550 | /* | |
551 | * task_rq_lock - lock p->pi_lock and lock the rq @p resides on. | |
552 | */ | |
eb580751 | 553 | struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf) |
3e71a462 PZ |
554 | __acquires(p->pi_lock) |
555 | __acquires(rq->lock) | |
556 | { | |
557 | struct rq *rq; | |
558 | ||
559 | for (;;) { | |
eb580751 | 560 | raw_spin_lock_irqsave(&p->pi_lock, rf->flags); |
3e71a462 | 561 | rq = task_rq(p); |
5cb9eaa3 | 562 | raw_spin_rq_lock(rq); |
3e71a462 PZ |
563 | /* |
564 | * move_queued_task() task_rq_lock() | |
565 | * | |
566 | * ACQUIRE (rq->lock) | |
567 | * [S] ->on_rq = MIGRATING [L] rq = task_rq() | |
568 | * WMB (__set_task_cpu()) ACQUIRE (rq->lock); | |
569 | * [S] ->cpu = new_cpu [L] task_rq() | |
570 | * [L] ->on_rq | |
571 | * RELEASE (rq->lock) | |
572 | * | |
c546951d | 573 | * If we observe the old CPU in task_rq_lock(), the acquire of |
3e71a462 PZ |
574 | * the old rq->lock will fully serialize against the stores. |
575 | * | |
c546951d AP |
576 | * If we observe the new CPU in task_rq_lock(), the address |
577 | * dependency headed by '[L] rq = task_rq()' and the acquire | |
578 | * will pair with the WMB to ensure we then also see migrating. | |
3e71a462 PZ |
579 | */ |
580 | if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) { | |
d8ac8971 | 581 | rq_pin_lock(rq, rf); |
3e71a462 PZ |
582 | return rq; |
583 | } | |
5cb9eaa3 | 584 | raw_spin_rq_unlock(rq); |
eb580751 | 585 | raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags); |
3e71a462 PZ |
586 | |
587 | while (unlikely(task_on_rq_migrating(p))) | |
588 | cpu_relax(); | |
589 | } | |
590 | } | |
591 | ||
535b9552 IM |
592 | /* |
593 | * RQ-clock updating methods: | |
594 | */ | |
595 | ||
596 | static void update_rq_clock_task(struct rq *rq, s64 delta) | |
597 | { | |
598 | /* | |
599 | * In theory, the compile should just see 0 here, and optimize out the call | |
600 | * to sched_rt_avg_update. But I don't trust it... | |
601 | */ | |
11d4afd4 VG |
602 | s64 __maybe_unused steal = 0, irq_delta = 0; |
603 | ||
535b9552 IM |
604 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING |
605 | irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time; | |
606 | ||
607 | /* | |
608 | * Since irq_time is only updated on {soft,}irq_exit, we might run into | |
609 | * this case when a previous update_rq_clock() happened inside a | |
610 | * {soft,}irq region. | |
611 | * | |
612 | * When this happens, we stop ->clock_task and only update the | |
613 | * prev_irq_time stamp to account for the part that fit, so that a next | |
614 | * update will consume the rest. This ensures ->clock_task is | |
615 | * monotonic. | |
616 | * | |
617 | * It does however cause some slight miss-attribution of {soft,}irq | |
618 | * time, a more accurate solution would be to update the irq_time using | |
619 | * the current rq->clock timestamp, except that would require using | |
620 | * atomic ops. | |
621 | */ | |
622 | if (irq_delta > delta) | |
623 | irq_delta = delta; | |
624 | ||
625 | rq->prev_irq_time += irq_delta; | |
626 | delta -= irq_delta; | |
627 | #endif | |
628 | #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING | |
629 | if (static_key_false((¶virt_steal_rq_enabled))) { | |
630 | steal = paravirt_steal_clock(cpu_of(rq)); | |
631 | steal -= rq->prev_steal_time_rq; | |
632 | ||
633 | if (unlikely(steal > delta)) | |
634 | steal = delta; | |
635 | ||
636 | rq->prev_steal_time_rq += steal; | |
637 | delta -= steal; | |
638 | } | |
639 | #endif | |
640 | ||
641 | rq->clock_task += delta; | |
642 | ||
11d4afd4 | 643 | #ifdef CONFIG_HAVE_SCHED_AVG_IRQ |
535b9552 | 644 | if ((irq_delta + steal) && sched_feat(NONTASK_CAPACITY)) |
91c27493 | 645 | update_irq_load_avg(rq, irq_delta + steal); |
535b9552 | 646 | #endif |
23127296 | 647 | update_rq_clock_pelt(rq, delta); |
535b9552 IM |
648 | } |
649 | ||
650 | void update_rq_clock(struct rq *rq) | |
651 | { | |
652 | s64 delta; | |
653 | ||
5cb9eaa3 | 654 | lockdep_assert_rq_held(rq); |
535b9552 IM |
655 | |
656 | if (rq->clock_update_flags & RQCF_ACT_SKIP) | |
657 | return; | |
658 | ||
659 | #ifdef CONFIG_SCHED_DEBUG | |
26ae58d2 PZ |
660 | if (sched_feat(WARN_DOUBLE_CLOCK)) |
661 | SCHED_WARN_ON(rq->clock_update_flags & RQCF_UPDATED); | |
535b9552 IM |
662 | rq->clock_update_flags |= RQCF_UPDATED; |
663 | #endif | |
26ae58d2 | 664 | |
535b9552 IM |
665 | delta = sched_clock_cpu(cpu_of(rq)) - rq->clock; |
666 | if (delta < 0) | |
667 | return; | |
668 | rq->clock += delta; | |
669 | update_rq_clock_task(rq, delta); | |
670 | } | |
671 | ||
8f4d37ec PZ |
672 | #ifdef CONFIG_SCHED_HRTICK |
673 | /* | |
674 | * Use HR-timers to deliver accurate preemption points. | |
8f4d37ec | 675 | */ |
8f4d37ec | 676 | |
8f4d37ec PZ |
677 | static void hrtick_clear(struct rq *rq) |
678 | { | |
679 | if (hrtimer_active(&rq->hrtick_timer)) | |
680 | hrtimer_cancel(&rq->hrtick_timer); | |
681 | } | |
682 | ||
8f4d37ec PZ |
683 | /* |
684 | * High-resolution timer tick. | |
685 | * Runs from hardirq context with interrupts disabled. | |
686 | */ | |
687 | static enum hrtimer_restart hrtick(struct hrtimer *timer) | |
688 | { | |
689 | struct rq *rq = container_of(timer, struct rq, hrtick_timer); | |
8a8c69c3 | 690 | struct rq_flags rf; |
8f4d37ec PZ |
691 | |
692 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); | |
693 | ||
8a8c69c3 | 694 | rq_lock(rq, &rf); |
3e51f33f | 695 | update_rq_clock(rq); |
8f4d37ec | 696 | rq->curr->sched_class->task_tick(rq, rq->curr, 1); |
8a8c69c3 | 697 | rq_unlock(rq, &rf); |
8f4d37ec PZ |
698 | |
699 | return HRTIMER_NORESTART; | |
700 | } | |
701 | ||
95e904c7 | 702 | #ifdef CONFIG_SMP |
971ee28c | 703 | |
4961b6e1 | 704 | static void __hrtick_restart(struct rq *rq) |
971ee28c PZ |
705 | { |
706 | struct hrtimer *timer = &rq->hrtick_timer; | |
156ec6f4 | 707 | ktime_t time = rq->hrtick_time; |
971ee28c | 708 | |
156ec6f4 | 709 | hrtimer_start(timer, time, HRTIMER_MODE_ABS_PINNED_HARD); |
971ee28c PZ |
710 | } |
711 | ||
31656519 PZ |
712 | /* |
713 | * called from hardirq (IPI) context | |
714 | */ | |
715 | static void __hrtick_start(void *arg) | |
b328ca18 | 716 | { |
31656519 | 717 | struct rq *rq = arg; |
8a8c69c3 | 718 | struct rq_flags rf; |
b328ca18 | 719 | |
8a8c69c3 | 720 | rq_lock(rq, &rf); |
971ee28c | 721 | __hrtick_restart(rq); |
8a8c69c3 | 722 | rq_unlock(rq, &rf); |
b328ca18 PZ |
723 | } |
724 | ||
31656519 PZ |
725 | /* |
726 | * Called to set the hrtick timer state. | |
727 | * | |
728 | * called with rq->lock held and irqs disabled | |
729 | */ | |
029632fb | 730 | void hrtick_start(struct rq *rq, u64 delay) |
b328ca18 | 731 | { |
31656519 | 732 | struct hrtimer *timer = &rq->hrtick_timer; |
177ef2a6 | 733 | s64 delta; |
734 | ||
735 | /* | |
736 | * Don't schedule slices shorter than 10000ns, that just | |
737 | * doesn't make sense and can cause timer DoS. | |
738 | */ | |
739 | delta = max_t(s64, delay, 10000LL); | |
156ec6f4 | 740 | rq->hrtick_time = ktime_add_ns(timer->base->get_time(), delta); |
31656519 | 741 | |
fd3eafda | 742 | if (rq == this_rq()) |
971ee28c | 743 | __hrtick_restart(rq); |
fd3eafda | 744 | else |
c46fff2a | 745 | smp_call_function_single_async(cpu_of(rq), &rq->hrtick_csd); |
b328ca18 PZ |
746 | } |
747 | ||
31656519 PZ |
748 | #else |
749 | /* | |
750 | * Called to set the hrtick timer state. | |
751 | * | |
752 | * called with rq->lock held and irqs disabled | |
753 | */ | |
029632fb | 754 | void hrtick_start(struct rq *rq, u64 delay) |
31656519 | 755 | { |
86893335 WL |
756 | /* |
757 | * Don't schedule slices shorter than 10000ns, that just | |
758 | * doesn't make sense. Rely on vruntime for fairness. | |
759 | */ | |
760 | delay = max_t(u64, delay, 10000LL); | |
4961b6e1 | 761 | hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay), |
d5096aa6 | 762 | HRTIMER_MODE_REL_PINNED_HARD); |
31656519 | 763 | } |
90b5363a | 764 | |
31656519 | 765 | #endif /* CONFIG_SMP */ |
8f4d37ec | 766 | |
77a021be | 767 | static void hrtick_rq_init(struct rq *rq) |
8f4d37ec | 768 | { |
31656519 | 769 | #ifdef CONFIG_SMP |
545b8c8d | 770 | INIT_CSD(&rq->hrtick_csd, __hrtick_start, rq); |
31656519 | 771 | #endif |
d5096aa6 | 772 | hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); |
31656519 | 773 | rq->hrtick_timer.function = hrtick; |
8f4d37ec | 774 | } |
006c75f1 | 775 | #else /* CONFIG_SCHED_HRTICK */ |
8f4d37ec PZ |
776 | static inline void hrtick_clear(struct rq *rq) |
777 | { | |
778 | } | |
779 | ||
77a021be | 780 | static inline void hrtick_rq_init(struct rq *rq) |
8f4d37ec PZ |
781 | { |
782 | } | |
006c75f1 | 783 | #endif /* CONFIG_SCHED_HRTICK */ |
8f4d37ec | 784 | |
5529578a FW |
785 | /* |
786 | * cmpxchg based fetch_or, macro so it works for different integer types | |
787 | */ | |
788 | #define fetch_or(ptr, mask) \ | |
789 | ({ \ | |
790 | typeof(ptr) _ptr = (ptr); \ | |
791 | typeof(mask) _mask = (mask); \ | |
792 | typeof(*_ptr) _old, _val = *_ptr; \ | |
793 | \ | |
794 | for (;;) { \ | |
795 | _old = cmpxchg(_ptr, _val, _val | _mask); \ | |
796 | if (_old == _val) \ | |
797 | break; \ | |
798 | _val = _old; \ | |
799 | } \ | |
800 | _old; \ | |
801 | }) | |
802 | ||
e3baac47 | 803 | #if defined(CONFIG_SMP) && defined(TIF_POLLING_NRFLAG) |
fd99f91a PZ |
804 | /* |
805 | * Atomically set TIF_NEED_RESCHED and test for TIF_POLLING_NRFLAG, | |
806 | * this avoids any races wrt polling state changes and thereby avoids | |
807 | * spurious IPIs. | |
808 | */ | |
809 | static bool set_nr_and_not_polling(struct task_struct *p) | |
810 | { | |
811 | struct thread_info *ti = task_thread_info(p); | |
812 | return !(fetch_or(&ti->flags, _TIF_NEED_RESCHED) & _TIF_POLLING_NRFLAG); | |
813 | } | |
e3baac47 PZ |
814 | |
815 | /* | |
816 | * Atomically set TIF_NEED_RESCHED if TIF_POLLING_NRFLAG is set. | |
817 | * | |
818 | * If this returns true, then the idle task promises to call | |
819 | * sched_ttwu_pending() and reschedule soon. | |
820 | */ | |
821 | static bool set_nr_if_polling(struct task_struct *p) | |
822 | { | |
823 | struct thread_info *ti = task_thread_info(p); | |
316c1608 | 824 | typeof(ti->flags) old, val = READ_ONCE(ti->flags); |
e3baac47 PZ |
825 | |
826 | for (;;) { | |
827 | if (!(val & _TIF_POLLING_NRFLAG)) | |
828 | return false; | |
829 | if (val & _TIF_NEED_RESCHED) | |
830 | return true; | |
831 | old = cmpxchg(&ti->flags, val, val | _TIF_NEED_RESCHED); | |
832 | if (old == val) | |
833 | break; | |
834 | val = old; | |
835 | } | |
836 | return true; | |
837 | } | |
838 | ||
fd99f91a PZ |
839 | #else |
840 | static bool set_nr_and_not_polling(struct task_struct *p) | |
841 | { | |
842 | set_tsk_need_resched(p); | |
843 | return true; | |
844 | } | |
e3baac47 PZ |
845 | |
846 | #ifdef CONFIG_SMP | |
847 | static bool set_nr_if_polling(struct task_struct *p) | |
848 | { | |
849 | return false; | |
850 | } | |
851 | #endif | |
fd99f91a PZ |
852 | #endif |
853 | ||
07879c6a | 854 | static bool __wake_q_add(struct wake_q_head *head, struct task_struct *task) |
76751049 PZ |
855 | { |
856 | struct wake_q_node *node = &task->wake_q; | |
857 | ||
858 | /* | |
859 | * Atomically grab the task, if ->wake_q is !nil already it means | |
b19a888c | 860 | * it's already queued (either by us or someone else) and will get the |
76751049 PZ |
861 | * wakeup due to that. |
862 | * | |
4c4e3731 PZ |
863 | * In order to ensure that a pending wakeup will observe our pending |
864 | * state, even in the failed case, an explicit smp_mb() must be used. | |
76751049 | 865 | */ |
4c4e3731 | 866 | smp_mb__before_atomic(); |
87ff19cb | 867 | if (unlikely(cmpxchg_relaxed(&node->next, NULL, WAKE_Q_TAIL))) |
07879c6a | 868 | return false; |
76751049 PZ |
869 | |
870 | /* | |
871 | * The head is context local, there can be no concurrency. | |
872 | */ | |
873 | *head->lastp = node; | |
874 | head->lastp = &node->next; | |
07879c6a DB |
875 | return true; |
876 | } | |
877 | ||
878 | /** | |
879 | * wake_q_add() - queue a wakeup for 'later' waking. | |
880 | * @head: the wake_q_head to add @task to | |
881 | * @task: the task to queue for 'later' wakeup | |
882 | * | |
883 | * Queue a task for later wakeup, most likely by the wake_up_q() call in the | |
884 | * same context, _HOWEVER_ this is not guaranteed, the wakeup can come | |
885 | * instantly. | |
886 | * | |
887 | * This function must be used as-if it were wake_up_process(); IOW the task | |
888 | * must be ready to be woken at this location. | |
889 | */ | |
890 | void wake_q_add(struct wake_q_head *head, struct task_struct *task) | |
891 | { | |
892 | if (__wake_q_add(head, task)) | |
893 | get_task_struct(task); | |
894 | } | |
895 | ||
896 | /** | |
897 | * wake_q_add_safe() - safely queue a wakeup for 'later' waking. | |
898 | * @head: the wake_q_head to add @task to | |
899 | * @task: the task to queue for 'later' wakeup | |
900 | * | |
901 | * Queue a task for later wakeup, most likely by the wake_up_q() call in the | |
902 | * same context, _HOWEVER_ this is not guaranteed, the wakeup can come | |
903 | * instantly. | |
904 | * | |
905 | * This function must be used as-if it were wake_up_process(); IOW the task | |
906 | * must be ready to be woken at this location. | |
907 | * | |
908 | * This function is essentially a task-safe equivalent to wake_q_add(). Callers | |
909 | * that already hold reference to @task can call the 'safe' version and trust | |
910 | * wake_q to do the right thing depending whether or not the @task is already | |
911 | * queued for wakeup. | |
912 | */ | |
913 | void wake_q_add_safe(struct wake_q_head *head, struct task_struct *task) | |
914 | { | |
915 | if (!__wake_q_add(head, task)) | |
916 | put_task_struct(task); | |
76751049 PZ |
917 | } |
918 | ||
919 | void wake_up_q(struct wake_q_head *head) | |
920 | { | |
921 | struct wake_q_node *node = head->first; | |
922 | ||
923 | while (node != WAKE_Q_TAIL) { | |
924 | struct task_struct *task; | |
925 | ||
926 | task = container_of(node, struct task_struct, wake_q); | |
d1ccc66d | 927 | /* Task can safely be re-inserted now: */ |
76751049 PZ |
928 | node = node->next; |
929 | task->wake_q.next = NULL; | |
930 | ||
931 | /* | |
7696f991 AP |
932 | * wake_up_process() executes a full barrier, which pairs with |
933 | * the queueing in wake_q_add() so as not to miss wakeups. | |
76751049 PZ |
934 | */ |
935 | wake_up_process(task); | |
936 | put_task_struct(task); | |
937 | } | |
938 | } | |
939 | ||
c24d20db | 940 | /* |
8875125e | 941 | * resched_curr - mark rq's current task 'to be rescheduled now'. |
c24d20db IM |
942 | * |
943 | * On UP this means the setting of the need_resched flag, on SMP it | |
944 | * might also involve a cross-CPU call to trigger the scheduler on | |
945 | * the target CPU. | |
946 | */ | |
8875125e | 947 | void resched_curr(struct rq *rq) |
c24d20db | 948 | { |
8875125e | 949 | struct task_struct *curr = rq->curr; |
c24d20db IM |
950 | int cpu; |
951 | ||
5cb9eaa3 | 952 | lockdep_assert_rq_held(rq); |
c24d20db | 953 | |
8875125e | 954 | if (test_tsk_need_resched(curr)) |
c24d20db IM |
955 | return; |
956 | ||
8875125e | 957 | cpu = cpu_of(rq); |
fd99f91a | 958 | |
f27dde8d | 959 | if (cpu == smp_processor_id()) { |
8875125e | 960 | set_tsk_need_resched(curr); |
f27dde8d | 961 | set_preempt_need_resched(); |
c24d20db | 962 | return; |
f27dde8d | 963 | } |
c24d20db | 964 | |
8875125e | 965 | if (set_nr_and_not_polling(curr)) |
c24d20db | 966 | smp_send_reschedule(cpu); |
dfc68f29 AL |
967 | else |
968 | trace_sched_wake_idle_without_ipi(cpu); | |
c24d20db IM |
969 | } |
970 | ||
029632fb | 971 | void resched_cpu(int cpu) |
c24d20db IM |
972 | { |
973 | struct rq *rq = cpu_rq(cpu); | |
974 | unsigned long flags; | |
975 | ||
5cb9eaa3 | 976 | raw_spin_rq_lock_irqsave(rq, flags); |
a0982dfa PM |
977 | if (cpu_online(cpu) || cpu == smp_processor_id()) |
978 | resched_curr(rq); | |
5cb9eaa3 | 979 | raw_spin_rq_unlock_irqrestore(rq, flags); |
c24d20db | 980 | } |
06d8308c | 981 | |
b021fe3e | 982 | #ifdef CONFIG_SMP |
3451d024 | 983 | #ifdef CONFIG_NO_HZ_COMMON |
83cd4fe2 | 984 | /* |
d1ccc66d IM |
985 | * In the semi idle case, use the nearest busy CPU for migrating timers |
986 | * from an idle CPU. This is good for power-savings. | |
83cd4fe2 VP |
987 | * |
988 | * We don't do similar optimization for completely idle system, as | |
d1ccc66d IM |
989 | * selecting an idle CPU will add more delays to the timers than intended |
990 | * (as that CPU's timer base may not be uptodate wrt jiffies etc). | |
83cd4fe2 | 991 | */ |
bc7a34b8 | 992 | int get_nohz_timer_target(void) |
83cd4fe2 | 993 | { |
e938b9c9 | 994 | int i, cpu = smp_processor_id(), default_cpu = -1; |
83cd4fe2 | 995 | struct sched_domain *sd; |
031e3bd8 | 996 | const struct cpumask *hk_mask; |
83cd4fe2 | 997 | |
e938b9c9 WL |
998 | if (housekeeping_cpu(cpu, HK_FLAG_TIMER)) { |
999 | if (!idle_cpu(cpu)) | |
1000 | return cpu; | |
1001 | default_cpu = cpu; | |
1002 | } | |
6201b4d6 | 1003 | |
031e3bd8 YZ |
1004 | hk_mask = housekeeping_cpumask(HK_FLAG_TIMER); |
1005 | ||
057f3fad | 1006 | rcu_read_lock(); |
83cd4fe2 | 1007 | for_each_domain(cpu, sd) { |
031e3bd8 | 1008 | for_each_cpu_and(i, sched_domain_span(sd), hk_mask) { |
44496922 WL |
1009 | if (cpu == i) |
1010 | continue; | |
1011 | ||
e938b9c9 | 1012 | if (!idle_cpu(i)) { |
057f3fad PZ |
1013 | cpu = i; |
1014 | goto unlock; | |
1015 | } | |
1016 | } | |
83cd4fe2 | 1017 | } |
9642d18e | 1018 | |
e938b9c9 WL |
1019 | if (default_cpu == -1) |
1020 | default_cpu = housekeeping_any_cpu(HK_FLAG_TIMER); | |
1021 | cpu = default_cpu; | |
057f3fad PZ |
1022 | unlock: |
1023 | rcu_read_unlock(); | |
83cd4fe2 VP |
1024 | return cpu; |
1025 | } | |
d1ccc66d | 1026 | |
06d8308c TG |
1027 | /* |
1028 | * When add_timer_on() enqueues a timer into the timer wheel of an | |
1029 | * idle CPU then this timer might expire before the next timer event | |
1030 | * which is scheduled to wake up that CPU. In case of a completely | |
1031 | * idle system the next event might even be infinite time into the | |
1032 | * future. wake_up_idle_cpu() ensures that the CPU is woken up and | |
1033 | * leaves the inner idle loop so the newly added timer is taken into | |
1034 | * account when the CPU goes back to idle and evaluates the timer | |
1035 | * wheel for the next timer event. | |
1036 | */ | |
1c20091e | 1037 | static void wake_up_idle_cpu(int cpu) |
06d8308c TG |
1038 | { |
1039 | struct rq *rq = cpu_rq(cpu); | |
1040 | ||
1041 | if (cpu == smp_processor_id()) | |
1042 | return; | |
1043 | ||
67b9ca70 | 1044 | if (set_nr_and_not_polling(rq->idle)) |
06d8308c | 1045 | smp_send_reschedule(cpu); |
dfc68f29 AL |
1046 | else |
1047 | trace_sched_wake_idle_without_ipi(cpu); | |
45bf76df IM |
1048 | } |
1049 | ||
c5bfece2 | 1050 | static bool wake_up_full_nohz_cpu(int cpu) |
1c20091e | 1051 | { |
53c5fa16 FW |
1052 | /* |
1053 | * We just need the target to call irq_exit() and re-evaluate | |
1054 | * the next tick. The nohz full kick at least implies that. | |
1055 | * If needed we can still optimize that later with an | |
1056 | * empty IRQ. | |
1057 | */ | |
379d9ecb PM |
1058 | if (cpu_is_offline(cpu)) |
1059 | return true; /* Don't try to wake offline CPUs. */ | |
c5bfece2 | 1060 | if (tick_nohz_full_cpu(cpu)) { |
1c20091e FW |
1061 | if (cpu != smp_processor_id() || |
1062 | tick_nohz_tick_stopped()) | |
53c5fa16 | 1063 | tick_nohz_full_kick_cpu(cpu); |
1c20091e FW |
1064 | return true; |
1065 | } | |
1066 | ||
1067 | return false; | |
1068 | } | |
1069 | ||
379d9ecb PM |
1070 | /* |
1071 | * Wake up the specified CPU. If the CPU is going offline, it is the | |
1072 | * caller's responsibility to deal with the lost wakeup, for example, | |
1073 | * by hooking into the CPU_DEAD notifier like timers and hrtimers do. | |
1074 | */ | |
1c20091e FW |
1075 | void wake_up_nohz_cpu(int cpu) |
1076 | { | |
c5bfece2 | 1077 | if (!wake_up_full_nohz_cpu(cpu)) |
1c20091e FW |
1078 | wake_up_idle_cpu(cpu); |
1079 | } | |
1080 | ||
19a1f5ec | 1081 | static void nohz_csd_func(void *info) |
45bf76df | 1082 | { |
19a1f5ec PZ |
1083 | struct rq *rq = info; |
1084 | int cpu = cpu_of(rq); | |
1085 | unsigned int flags; | |
873b4c65 VG |
1086 | |
1087 | /* | |
19a1f5ec | 1088 | * Release the rq::nohz_csd. |
873b4c65 | 1089 | */ |
c6f88654 | 1090 | flags = atomic_fetch_andnot(NOHZ_KICK_MASK | NOHZ_NEWILB_KICK, nohz_flags(cpu)); |
19a1f5ec | 1091 | WARN_ON(!(flags & NOHZ_KICK_MASK)); |
45bf76df | 1092 | |
19a1f5ec PZ |
1093 | rq->idle_balance = idle_cpu(cpu); |
1094 | if (rq->idle_balance && !need_resched()) { | |
1095 | rq->nohz_idle_balance = flags; | |
90b5363a PZI |
1096 | raise_softirq_irqoff(SCHED_SOFTIRQ); |
1097 | } | |
2069dd75 PZ |
1098 | } |
1099 | ||
3451d024 | 1100 | #endif /* CONFIG_NO_HZ_COMMON */ |
d842de87 | 1101 | |
ce831b38 | 1102 | #ifdef CONFIG_NO_HZ_FULL |
76d92ac3 | 1103 | bool sched_can_stop_tick(struct rq *rq) |
ce831b38 | 1104 | { |
76d92ac3 FW |
1105 | int fifo_nr_running; |
1106 | ||
1107 | /* Deadline tasks, even if single, need the tick */ | |
1108 | if (rq->dl.dl_nr_running) | |
1109 | return false; | |
1110 | ||
1e78cdbd | 1111 | /* |
b19a888c | 1112 | * If there are more than one RR tasks, we need the tick to affect the |
2548d546 | 1113 | * actual RR behaviour. |
1e78cdbd | 1114 | */ |
76d92ac3 FW |
1115 | if (rq->rt.rr_nr_running) { |
1116 | if (rq->rt.rr_nr_running == 1) | |
1117 | return true; | |
1118 | else | |
1119 | return false; | |
1e78cdbd RR |
1120 | } |
1121 | ||
2548d546 PZ |
1122 | /* |
1123 | * If there's no RR tasks, but FIFO tasks, we can skip the tick, no | |
1124 | * forced preemption between FIFO tasks. | |
1125 | */ | |
1126 | fifo_nr_running = rq->rt.rt_nr_running - rq->rt.rr_nr_running; | |
1127 | if (fifo_nr_running) | |
1128 | return true; | |
1129 | ||
1130 | /* | |
1131 | * If there are no DL,RR/FIFO tasks, there must only be CFS tasks left; | |
1132 | * if there's more than one we need the tick for involuntary | |
1133 | * preemption. | |
1134 | */ | |
1135 | if (rq->nr_running > 1) | |
541b8264 | 1136 | return false; |
ce831b38 | 1137 | |
541b8264 | 1138 | return true; |
ce831b38 FW |
1139 | } |
1140 | #endif /* CONFIG_NO_HZ_FULL */ | |
6d6bc0ad | 1141 | #endif /* CONFIG_SMP */ |
18d95a28 | 1142 | |
a790de99 PT |
1143 | #if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \ |
1144 | (defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH))) | |
c09595f6 | 1145 | /* |
8277434e PT |
1146 | * Iterate task_group tree rooted at *from, calling @down when first entering a |
1147 | * node and @up when leaving it for the final time. | |
1148 | * | |
1149 | * Caller must hold rcu_lock or sufficient equivalent. | |
c09595f6 | 1150 | */ |
029632fb | 1151 | int walk_tg_tree_from(struct task_group *from, |
8277434e | 1152 | tg_visitor down, tg_visitor up, void *data) |
c09595f6 PZ |
1153 | { |
1154 | struct task_group *parent, *child; | |
eb755805 | 1155 | int ret; |
c09595f6 | 1156 | |
8277434e PT |
1157 | parent = from; |
1158 | ||
c09595f6 | 1159 | down: |
eb755805 PZ |
1160 | ret = (*down)(parent, data); |
1161 | if (ret) | |
8277434e | 1162 | goto out; |
c09595f6 PZ |
1163 | list_for_each_entry_rcu(child, &parent->children, siblings) { |
1164 | parent = child; | |
1165 | goto down; | |
1166 | ||
1167 | up: | |
1168 | continue; | |
1169 | } | |
eb755805 | 1170 | ret = (*up)(parent, data); |
8277434e PT |
1171 | if (ret || parent == from) |
1172 | goto out; | |
c09595f6 PZ |
1173 | |
1174 | child = parent; | |
1175 | parent = parent->parent; | |
1176 | if (parent) | |
1177 | goto up; | |
8277434e | 1178 | out: |
eb755805 | 1179 | return ret; |
c09595f6 PZ |
1180 | } |
1181 | ||
029632fb | 1182 | int tg_nop(struct task_group *tg, void *data) |
eb755805 | 1183 | { |
e2b245f8 | 1184 | return 0; |
eb755805 | 1185 | } |
18d95a28 PZ |
1186 | #endif |
1187 | ||
9059393e | 1188 | static void set_load_weight(struct task_struct *p, bool update_load) |
45bf76df | 1189 | { |
f05998d4 NR |
1190 | int prio = p->static_prio - MAX_RT_PRIO; |
1191 | struct load_weight *load = &p->se.load; | |
1192 | ||
dd41f596 IM |
1193 | /* |
1194 | * SCHED_IDLE tasks get minimal weight: | |
1195 | */ | |
1da1843f | 1196 | if (task_has_idle_policy(p)) { |
c8b28116 | 1197 | load->weight = scale_load(WEIGHT_IDLEPRIO); |
f05998d4 | 1198 | load->inv_weight = WMULT_IDLEPRIO; |
dd41f596 IM |
1199 | return; |
1200 | } | |
71f8bd46 | 1201 | |
9059393e VG |
1202 | /* |
1203 | * SCHED_OTHER tasks have to update their load when changing their | |
1204 | * weight | |
1205 | */ | |
1206 | if (update_load && p->sched_class == &fair_sched_class) { | |
1207 | reweight_task(p, prio); | |
1208 | } else { | |
1209 | load->weight = scale_load(sched_prio_to_weight[prio]); | |
1210 | load->inv_weight = sched_prio_to_wmult[prio]; | |
1211 | } | |
71f8bd46 IM |
1212 | } |
1213 | ||
69842cba | 1214 | #ifdef CONFIG_UCLAMP_TASK |
2480c093 PB |
1215 | /* |
1216 | * Serializes updates of utilization clamp values | |
1217 | * | |
1218 | * The (slow-path) user-space triggers utilization clamp value updates which | |
1219 | * can require updates on (fast-path) scheduler's data structures used to | |
1220 | * support enqueue/dequeue operations. | |
1221 | * While the per-CPU rq lock protects fast-path update operations, user-space | |
1222 | * requests are serialized using a mutex to reduce the risk of conflicting | |
1223 | * updates or API abuses. | |
1224 | */ | |
1225 | static DEFINE_MUTEX(uclamp_mutex); | |
1226 | ||
e8f14172 PB |
1227 | /* Max allowed minimum utilization */ |
1228 | unsigned int sysctl_sched_uclamp_util_min = SCHED_CAPACITY_SCALE; | |
1229 | ||
1230 | /* Max allowed maximum utilization */ | |
1231 | unsigned int sysctl_sched_uclamp_util_max = SCHED_CAPACITY_SCALE; | |
1232 | ||
13685c4a QY |
1233 | /* |
1234 | * By default RT tasks run at the maximum performance point/capacity of the | |
1235 | * system. Uclamp enforces this by always setting UCLAMP_MIN of RT tasks to | |
1236 | * SCHED_CAPACITY_SCALE. | |
1237 | * | |
1238 | * This knob allows admins to change the default behavior when uclamp is being | |
1239 | * used. In battery powered devices, particularly, running at the maximum | |
1240 | * capacity and frequency will increase energy consumption and shorten the | |
1241 | * battery life. | |
1242 | * | |
1243 | * This knob only affects RT tasks that their uclamp_se->user_defined == false. | |
1244 | * | |
1245 | * This knob will not override the system default sched_util_clamp_min defined | |
1246 | * above. | |
1247 | */ | |
1248 | unsigned int sysctl_sched_uclamp_util_min_rt_default = SCHED_CAPACITY_SCALE; | |
1249 | ||
e8f14172 PB |
1250 | /* All clamps are required to be less or equal than these values */ |
1251 | static struct uclamp_se uclamp_default[UCLAMP_CNT]; | |
69842cba | 1252 | |
46609ce2 QY |
1253 | /* |
1254 | * This static key is used to reduce the uclamp overhead in the fast path. It | |
1255 | * primarily disables the call to uclamp_rq_{inc, dec}() in | |
1256 | * enqueue/dequeue_task(). | |
1257 | * | |
1258 | * This allows users to continue to enable uclamp in their kernel config with | |
1259 | * minimum uclamp overhead in the fast path. | |
1260 | * | |
1261 | * As soon as userspace modifies any of the uclamp knobs, the static key is | |
1262 | * enabled, since we have an actual users that make use of uclamp | |
1263 | * functionality. | |
1264 | * | |
1265 | * The knobs that would enable this static key are: | |
1266 | * | |
1267 | * * A task modifying its uclamp value with sched_setattr(). | |
1268 | * * An admin modifying the sysctl_sched_uclamp_{min, max} via procfs. | |
1269 | * * An admin modifying the cgroup cpu.uclamp.{min, max} | |
1270 | */ | |
1271 | DEFINE_STATIC_KEY_FALSE(sched_uclamp_used); | |
1272 | ||
69842cba PB |
1273 | /* Integer rounded range for each bucket */ |
1274 | #define UCLAMP_BUCKET_DELTA DIV_ROUND_CLOSEST(SCHED_CAPACITY_SCALE, UCLAMP_BUCKETS) | |
1275 | ||
1276 | #define for_each_clamp_id(clamp_id) \ | |
1277 | for ((clamp_id) = 0; (clamp_id) < UCLAMP_CNT; (clamp_id)++) | |
1278 | ||
1279 | static inline unsigned int uclamp_bucket_id(unsigned int clamp_value) | |
1280 | { | |
6d2f8909 | 1281 | return min_t(unsigned int, clamp_value / UCLAMP_BUCKET_DELTA, UCLAMP_BUCKETS - 1); |
69842cba PB |
1282 | } |
1283 | ||
7763baac | 1284 | static inline unsigned int uclamp_none(enum uclamp_id clamp_id) |
69842cba PB |
1285 | { |
1286 | if (clamp_id == UCLAMP_MIN) | |
1287 | return 0; | |
1288 | return SCHED_CAPACITY_SCALE; | |
1289 | } | |
1290 | ||
a509a7cd PB |
1291 | static inline void uclamp_se_set(struct uclamp_se *uc_se, |
1292 | unsigned int value, bool user_defined) | |
69842cba PB |
1293 | { |
1294 | uc_se->value = value; | |
1295 | uc_se->bucket_id = uclamp_bucket_id(value); | |
a509a7cd | 1296 | uc_se->user_defined = user_defined; |
69842cba PB |
1297 | } |
1298 | ||
e496187d | 1299 | static inline unsigned int |
0413d7f3 | 1300 | uclamp_idle_value(struct rq *rq, enum uclamp_id clamp_id, |
e496187d PB |
1301 | unsigned int clamp_value) |
1302 | { | |
1303 | /* | |
1304 | * Avoid blocked utilization pushing up the frequency when we go | |
1305 | * idle (which drops the max-clamp) by retaining the last known | |
1306 | * max-clamp. | |
1307 | */ | |
1308 | if (clamp_id == UCLAMP_MAX) { | |
1309 | rq->uclamp_flags |= UCLAMP_FLAG_IDLE; | |
1310 | return clamp_value; | |
1311 | } | |
1312 | ||
1313 | return uclamp_none(UCLAMP_MIN); | |
1314 | } | |
1315 | ||
0413d7f3 | 1316 | static inline void uclamp_idle_reset(struct rq *rq, enum uclamp_id clamp_id, |
e496187d PB |
1317 | unsigned int clamp_value) |
1318 | { | |
1319 | /* Reset max-clamp retention only on idle exit */ | |
1320 | if (!(rq->uclamp_flags & UCLAMP_FLAG_IDLE)) | |
1321 | return; | |
1322 | ||
1323 | WRITE_ONCE(rq->uclamp[clamp_id].value, clamp_value); | |
1324 | } | |
1325 | ||
69842cba | 1326 | static inline |
7763baac | 1327 | unsigned int uclamp_rq_max_value(struct rq *rq, enum uclamp_id clamp_id, |
0413d7f3 | 1328 | unsigned int clamp_value) |
69842cba PB |
1329 | { |
1330 | struct uclamp_bucket *bucket = rq->uclamp[clamp_id].bucket; | |
1331 | int bucket_id = UCLAMP_BUCKETS - 1; | |
1332 | ||
1333 | /* | |
1334 | * Since both min and max clamps are max aggregated, find the | |
1335 | * top most bucket with tasks in. | |
1336 | */ | |
1337 | for ( ; bucket_id >= 0; bucket_id--) { | |
1338 | if (!bucket[bucket_id].tasks) | |
1339 | continue; | |
1340 | return bucket[bucket_id].value; | |
1341 | } | |
1342 | ||
1343 | /* No tasks -- default clamp values */ | |
e496187d | 1344 | return uclamp_idle_value(rq, clamp_id, clamp_value); |
69842cba PB |
1345 | } |
1346 | ||
13685c4a QY |
1347 | static void __uclamp_update_util_min_rt_default(struct task_struct *p) |
1348 | { | |
1349 | unsigned int default_util_min; | |
1350 | struct uclamp_se *uc_se; | |
1351 | ||
1352 | lockdep_assert_held(&p->pi_lock); | |
1353 | ||
1354 | uc_se = &p->uclamp_req[UCLAMP_MIN]; | |
1355 | ||
1356 | /* Only sync if user didn't override the default */ | |
1357 | if (uc_se->user_defined) | |
1358 | return; | |
1359 | ||
1360 | default_util_min = sysctl_sched_uclamp_util_min_rt_default; | |
1361 | uclamp_se_set(uc_se, default_util_min, false); | |
1362 | } | |
1363 | ||
1364 | static void uclamp_update_util_min_rt_default(struct task_struct *p) | |
1365 | { | |
1366 | struct rq_flags rf; | |
1367 | struct rq *rq; | |
1368 | ||
1369 | if (!rt_task(p)) | |
1370 | return; | |
1371 | ||
1372 | /* Protect updates to p->uclamp_* */ | |
1373 | rq = task_rq_lock(p, &rf); | |
1374 | __uclamp_update_util_min_rt_default(p); | |
1375 | task_rq_unlock(rq, p, &rf); | |
1376 | } | |
1377 | ||
1378 | static void uclamp_sync_util_min_rt_default(void) | |
1379 | { | |
1380 | struct task_struct *g, *p; | |
1381 | ||
1382 | /* | |
1383 | * copy_process() sysctl_uclamp | |
1384 | * uclamp_min_rt = X; | |
1385 | * write_lock(&tasklist_lock) read_lock(&tasklist_lock) | |
1386 | * // link thread smp_mb__after_spinlock() | |
1387 | * write_unlock(&tasklist_lock) read_unlock(&tasklist_lock); | |
1388 | * sched_post_fork() for_each_process_thread() | |
1389 | * __uclamp_sync_rt() __uclamp_sync_rt() | |
1390 | * | |
1391 | * Ensures that either sched_post_fork() will observe the new | |
1392 | * uclamp_min_rt or for_each_process_thread() will observe the new | |
1393 | * task. | |
1394 | */ | |
1395 | read_lock(&tasklist_lock); | |
1396 | smp_mb__after_spinlock(); | |
1397 | read_unlock(&tasklist_lock); | |
1398 | ||
1399 | rcu_read_lock(); | |
1400 | for_each_process_thread(g, p) | |
1401 | uclamp_update_util_min_rt_default(p); | |
1402 | rcu_read_unlock(); | |
1403 | } | |
1404 | ||
3eac870a | 1405 | static inline struct uclamp_se |
0413d7f3 | 1406 | uclamp_tg_restrict(struct task_struct *p, enum uclamp_id clamp_id) |
3eac870a | 1407 | { |
0213b708 | 1408 | /* Copy by value as we could modify it */ |
3eac870a PB |
1409 | struct uclamp_se uc_req = p->uclamp_req[clamp_id]; |
1410 | #ifdef CONFIG_UCLAMP_TASK_GROUP | |
0213b708 | 1411 | unsigned int tg_min, tg_max, value; |
3eac870a PB |
1412 | |
1413 | /* | |
1414 | * Tasks in autogroups or root task group will be | |
1415 | * restricted by system defaults. | |
1416 | */ | |
1417 | if (task_group_is_autogroup(task_group(p))) | |
1418 | return uc_req; | |
1419 | if (task_group(p) == &root_task_group) | |
1420 | return uc_req; | |
1421 | ||
0213b708 QY |
1422 | tg_min = task_group(p)->uclamp[UCLAMP_MIN].value; |
1423 | tg_max = task_group(p)->uclamp[UCLAMP_MAX].value; | |
1424 | value = uc_req.value; | |
1425 | value = clamp(value, tg_min, tg_max); | |
1426 | uclamp_se_set(&uc_req, value, false); | |
3eac870a PB |
1427 | #endif |
1428 | ||
1429 | return uc_req; | |
1430 | } | |
1431 | ||
e8f14172 PB |
1432 | /* |
1433 | * The effective clamp bucket index of a task depends on, by increasing | |
1434 | * priority: | |
1435 | * - the task specific clamp value, when explicitly requested from userspace | |
3eac870a PB |
1436 | * - the task group effective clamp value, for tasks not either in the root |
1437 | * group or in an autogroup | |
e8f14172 PB |
1438 | * - the system default clamp value, defined by the sysadmin |
1439 | */ | |
1440 | static inline struct uclamp_se | |
0413d7f3 | 1441 | uclamp_eff_get(struct task_struct *p, enum uclamp_id clamp_id) |
e8f14172 | 1442 | { |
3eac870a | 1443 | struct uclamp_se uc_req = uclamp_tg_restrict(p, clamp_id); |
e8f14172 PB |
1444 | struct uclamp_se uc_max = uclamp_default[clamp_id]; |
1445 | ||
1446 | /* System default restrictions always apply */ | |
1447 | if (unlikely(uc_req.value > uc_max.value)) | |
1448 | return uc_max; | |
1449 | ||
1450 | return uc_req; | |
1451 | } | |
1452 | ||
686516b5 | 1453 | unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id) |
9d20ad7d PB |
1454 | { |
1455 | struct uclamp_se uc_eff; | |
1456 | ||
1457 | /* Task currently refcounted: use back-annotated (effective) value */ | |
1458 | if (p->uclamp[clamp_id].active) | |
686516b5 | 1459 | return (unsigned long)p->uclamp[clamp_id].value; |
9d20ad7d PB |
1460 | |
1461 | uc_eff = uclamp_eff_get(p, clamp_id); | |
1462 | ||
686516b5 | 1463 | return (unsigned long)uc_eff.value; |
9d20ad7d PB |
1464 | } |
1465 | ||
69842cba PB |
1466 | /* |
1467 | * When a task is enqueued on a rq, the clamp bucket currently defined by the | |
1468 | * task's uclamp::bucket_id is refcounted on that rq. This also immediately | |
1469 | * updates the rq's clamp value if required. | |
60daf9c1 PB |
1470 | * |
1471 | * Tasks can have a task-specific value requested from user-space, track | |
1472 | * within each bucket the maximum value for tasks refcounted in it. | |
1473 | * This "local max aggregation" allows to track the exact "requested" value | |
1474 | * for each bucket when all its RUNNABLE tasks require the same clamp. | |
69842cba PB |
1475 | */ |
1476 | static inline void uclamp_rq_inc_id(struct rq *rq, struct task_struct *p, | |
0413d7f3 | 1477 | enum uclamp_id clamp_id) |
69842cba PB |
1478 | { |
1479 | struct uclamp_rq *uc_rq = &rq->uclamp[clamp_id]; | |
1480 | struct uclamp_se *uc_se = &p->uclamp[clamp_id]; | |
1481 | struct uclamp_bucket *bucket; | |
1482 | ||
5cb9eaa3 | 1483 | lockdep_assert_rq_held(rq); |
69842cba | 1484 | |
e8f14172 PB |
1485 | /* Update task effective clamp */ |
1486 | p->uclamp[clamp_id] = uclamp_eff_get(p, clamp_id); | |
1487 | ||
69842cba PB |
1488 | bucket = &uc_rq->bucket[uc_se->bucket_id]; |
1489 | bucket->tasks++; | |
e8f14172 | 1490 | uc_se->active = true; |
69842cba | 1491 | |
e496187d PB |
1492 | uclamp_idle_reset(rq, clamp_id, uc_se->value); |
1493 | ||
60daf9c1 PB |
1494 | /* |
1495 | * Local max aggregation: rq buckets always track the max | |
1496 | * "requested" clamp value of its RUNNABLE tasks. | |
1497 | */ | |
1498 | if (bucket->tasks == 1 || uc_se->value > bucket->value) | |
1499 | bucket->value = uc_se->value; | |
1500 | ||
69842cba | 1501 | if (uc_se->value > READ_ONCE(uc_rq->value)) |
60daf9c1 | 1502 | WRITE_ONCE(uc_rq->value, uc_se->value); |
69842cba PB |
1503 | } |
1504 | ||
1505 | /* | |
1506 | * When a task is dequeued from a rq, the clamp bucket refcounted by the task | |
1507 | * is released. If this is the last task reference counting the rq's max | |
1508 | * active clamp value, then the rq's clamp value is updated. | |
1509 | * | |
1510 | * Both refcounted tasks and rq's cached clamp values are expected to be | |
1511 | * always valid. If it's detected they are not, as defensive programming, | |
1512 | * enforce the expected state and warn. | |
1513 | */ | |
1514 | static inline void uclamp_rq_dec_id(struct rq *rq, struct task_struct *p, | |
0413d7f3 | 1515 | enum uclamp_id clamp_id) |
69842cba PB |
1516 | { |
1517 | struct uclamp_rq *uc_rq = &rq->uclamp[clamp_id]; | |
1518 | struct uclamp_se *uc_se = &p->uclamp[clamp_id]; | |
1519 | struct uclamp_bucket *bucket; | |
e496187d | 1520 | unsigned int bkt_clamp; |
69842cba PB |
1521 | unsigned int rq_clamp; |
1522 | ||
5cb9eaa3 | 1523 | lockdep_assert_rq_held(rq); |
69842cba | 1524 | |
46609ce2 QY |
1525 | /* |
1526 | * If sched_uclamp_used was enabled after task @p was enqueued, | |
1527 | * we could end up with unbalanced call to uclamp_rq_dec_id(). | |
1528 | * | |
1529 | * In this case the uc_se->active flag should be false since no uclamp | |
1530 | * accounting was performed at enqueue time and we can just return | |
1531 | * here. | |
1532 | * | |
b19a888c | 1533 | * Need to be careful of the following enqueue/dequeue ordering |
46609ce2 QY |
1534 | * problem too |
1535 | * | |
1536 | * enqueue(taskA) | |
1537 | * // sched_uclamp_used gets enabled | |
1538 | * enqueue(taskB) | |
1539 | * dequeue(taskA) | |
b19a888c | 1540 | * // Must not decrement bucket->tasks here |
46609ce2 QY |
1541 | * dequeue(taskB) |
1542 | * | |
1543 | * where we could end up with stale data in uc_se and | |
1544 | * bucket[uc_se->bucket_id]. | |
1545 | * | |
1546 | * The following check here eliminates the possibility of such race. | |
1547 | */ | |
1548 | if (unlikely(!uc_se->active)) | |
1549 | return; | |
1550 | ||
69842cba | 1551 | bucket = &uc_rq->bucket[uc_se->bucket_id]; |
46609ce2 | 1552 | |
69842cba PB |
1553 | SCHED_WARN_ON(!bucket->tasks); |
1554 | if (likely(bucket->tasks)) | |
1555 | bucket->tasks--; | |
46609ce2 | 1556 | |
e8f14172 | 1557 | uc_se->active = false; |
69842cba | 1558 | |
60daf9c1 PB |
1559 | /* |
1560 | * Keep "local max aggregation" simple and accept to (possibly) | |
1561 | * overboost some RUNNABLE tasks in the same bucket. | |
1562 | * The rq clamp bucket value is reset to its base value whenever | |
1563 | * there are no more RUNNABLE tasks refcounting it. | |
1564 | */ | |
69842cba PB |
1565 | if (likely(bucket->tasks)) |
1566 | return; | |
1567 | ||
1568 | rq_clamp = READ_ONCE(uc_rq->value); | |
1569 | /* | |
1570 | * Defensive programming: this should never happen. If it happens, | |
1571 | * e.g. due to future modification, warn and fixup the expected value. | |
1572 | */ | |
1573 | SCHED_WARN_ON(bucket->value > rq_clamp); | |
e496187d PB |
1574 | if (bucket->value >= rq_clamp) { |
1575 | bkt_clamp = uclamp_rq_max_value(rq, clamp_id, uc_se->value); | |
1576 | WRITE_ONCE(uc_rq->value, bkt_clamp); | |
1577 | } | |
69842cba PB |
1578 | } |
1579 | ||
1580 | static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p) | |
1581 | { | |
0413d7f3 | 1582 | enum uclamp_id clamp_id; |
69842cba | 1583 | |
46609ce2 QY |
1584 | /* |
1585 | * Avoid any overhead until uclamp is actually used by the userspace. | |
1586 | * | |
1587 | * The condition is constructed such that a NOP is generated when | |
1588 | * sched_uclamp_used is disabled. | |
1589 | */ | |
1590 | if (!static_branch_unlikely(&sched_uclamp_used)) | |
1591 | return; | |
1592 | ||
69842cba PB |
1593 | if (unlikely(!p->sched_class->uclamp_enabled)) |
1594 | return; | |
1595 | ||
1596 | for_each_clamp_id(clamp_id) | |
1597 | uclamp_rq_inc_id(rq, p, clamp_id); | |
e496187d PB |
1598 | |
1599 | /* Reset clamp idle holding when there is one RUNNABLE task */ | |
1600 | if (rq->uclamp_flags & UCLAMP_FLAG_IDLE) | |
1601 | rq->uclamp_flags &= ~UCLAMP_FLAG_IDLE; | |
69842cba PB |
1602 | } |
1603 | ||
1604 | static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p) | |
1605 | { | |
0413d7f3 | 1606 | enum uclamp_id clamp_id; |
69842cba | 1607 | |
46609ce2 QY |
1608 | /* |
1609 | * Avoid any overhead until uclamp is actually used by the userspace. | |
1610 | * | |
1611 | * The condition is constructed such that a NOP is generated when | |
1612 | * sched_uclamp_used is disabled. | |
1613 | */ | |
1614 | if (!static_branch_unlikely(&sched_uclamp_used)) | |
1615 | return; | |
1616 | ||
69842cba PB |
1617 | if (unlikely(!p->sched_class->uclamp_enabled)) |
1618 | return; | |
1619 | ||
1620 | for_each_clamp_id(clamp_id) | |
1621 | uclamp_rq_dec_id(rq, p, clamp_id); | |
1622 | } | |
1623 | ||
ca4984a7 QP |
1624 | static inline void uclamp_rq_reinc_id(struct rq *rq, struct task_struct *p, |
1625 | enum uclamp_id clamp_id) | |
1626 | { | |
1627 | if (!p->uclamp[clamp_id].active) | |
1628 | return; | |
1629 | ||
1630 | uclamp_rq_dec_id(rq, p, clamp_id); | |
1631 | uclamp_rq_inc_id(rq, p, clamp_id); | |
1632 | ||
1633 | /* | |
1634 | * Make sure to clear the idle flag if we've transiently reached 0 | |
1635 | * active tasks on rq. | |
1636 | */ | |
1637 | if (clamp_id == UCLAMP_MAX && (rq->uclamp_flags & UCLAMP_FLAG_IDLE)) | |
1638 | rq->uclamp_flags &= ~UCLAMP_FLAG_IDLE; | |
1639 | } | |
1640 | ||
babbe170 | 1641 | static inline void |
0213b708 | 1642 | uclamp_update_active(struct task_struct *p) |
babbe170 | 1643 | { |
0213b708 | 1644 | enum uclamp_id clamp_id; |
babbe170 PB |
1645 | struct rq_flags rf; |
1646 | struct rq *rq; | |
1647 | ||
1648 | /* | |
1649 | * Lock the task and the rq where the task is (or was) queued. | |
1650 | * | |
1651 | * We might lock the (previous) rq of a !RUNNABLE task, but that's the | |
1652 | * price to pay to safely serialize util_{min,max} updates with | |
1653 | * enqueues, dequeues and migration operations. | |
1654 | * This is the same locking schema used by __set_cpus_allowed_ptr(). | |
1655 | */ | |
1656 | rq = task_rq_lock(p, &rf); | |
1657 | ||
1658 | /* | |
1659 | * Setting the clamp bucket is serialized by task_rq_lock(). | |
1660 | * If the task is not yet RUNNABLE and its task_struct is not | |
1661 | * affecting a valid clamp bucket, the next time it's enqueued, | |
1662 | * it will already see the updated clamp bucket value. | |
1663 | */ | |
ca4984a7 QP |
1664 | for_each_clamp_id(clamp_id) |
1665 | uclamp_rq_reinc_id(rq, p, clamp_id); | |
babbe170 PB |
1666 | |
1667 | task_rq_unlock(rq, p, &rf); | |
1668 | } | |
1669 | ||
e3b8b6a0 | 1670 | #ifdef CONFIG_UCLAMP_TASK_GROUP |
babbe170 | 1671 | static inline void |
0213b708 | 1672 | uclamp_update_active_tasks(struct cgroup_subsys_state *css) |
babbe170 PB |
1673 | { |
1674 | struct css_task_iter it; | |
1675 | struct task_struct *p; | |
babbe170 PB |
1676 | |
1677 | css_task_iter_start(css, 0, &it); | |
0213b708 QY |
1678 | while ((p = css_task_iter_next(&it))) |
1679 | uclamp_update_active(p); | |
babbe170 PB |
1680 | css_task_iter_end(&it); |
1681 | } | |
1682 | ||
7274a5c1 PB |
1683 | static void cpu_util_update_eff(struct cgroup_subsys_state *css); |
1684 | static void uclamp_update_root_tg(void) | |
1685 | { | |
1686 | struct task_group *tg = &root_task_group; | |
1687 | ||
1688 | uclamp_se_set(&tg->uclamp_req[UCLAMP_MIN], | |
1689 | sysctl_sched_uclamp_util_min, false); | |
1690 | uclamp_se_set(&tg->uclamp_req[UCLAMP_MAX], | |
1691 | sysctl_sched_uclamp_util_max, false); | |
1692 | ||
1693 | rcu_read_lock(); | |
1694 | cpu_util_update_eff(&root_task_group.css); | |
1695 | rcu_read_unlock(); | |
1696 | } | |
1697 | #else | |
1698 | static void uclamp_update_root_tg(void) { } | |
1699 | #endif | |
1700 | ||
e8f14172 | 1701 | int sysctl_sched_uclamp_handler(struct ctl_table *table, int write, |
32927393 | 1702 | void *buffer, size_t *lenp, loff_t *ppos) |
e8f14172 | 1703 | { |
7274a5c1 | 1704 | bool update_root_tg = false; |
13685c4a | 1705 | int old_min, old_max, old_min_rt; |
e8f14172 PB |
1706 | int result; |
1707 | ||
2480c093 | 1708 | mutex_lock(&uclamp_mutex); |
e8f14172 PB |
1709 | old_min = sysctl_sched_uclamp_util_min; |
1710 | old_max = sysctl_sched_uclamp_util_max; | |
13685c4a | 1711 | old_min_rt = sysctl_sched_uclamp_util_min_rt_default; |
e8f14172 PB |
1712 | |
1713 | result = proc_dointvec(table, write, buffer, lenp, ppos); | |
1714 | if (result) | |
1715 | goto undo; | |
1716 | if (!write) | |
1717 | goto done; | |
1718 | ||
1719 | if (sysctl_sched_uclamp_util_min > sysctl_sched_uclamp_util_max || | |
13685c4a QY |
1720 | sysctl_sched_uclamp_util_max > SCHED_CAPACITY_SCALE || |
1721 | sysctl_sched_uclamp_util_min_rt_default > SCHED_CAPACITY_SCALE) { | |
1722 | ||
e8f14172 PB |
1723 | result = -EINVAL; |
1724 | goto undo; | |
1725 | } | |
1726 | ||
1727 | if (old_min != sysctl_sched_uclamp_util_min) { | |
1728 | uclamp_se_set(&uclamp_default[UCLAMP_MIN], | |
a509a7cd | 1729 | sysctl_sched_uclamp_util_min, false); |
7274a5c1 | 1730 | update_root_tg = true; |
e8f14172 PB |
1731 | } |
1732 | if (old_max != sysctl_sched_uclamp_util_max) { | |
1733 | uclamp_se_set(&uclamp_default[UCLAMP_MAX], | |
a509a7cd | 1734 | sysctl_sched_uclamp_util_max, false); |
7274a5c1 | 1735 | update_root_tg = true; |
e8f14172 PB |
1736 | } |
1737 | ||
46609ce2 QY |
1738 | if (update_root_tg) { |
1739 | static_branch_enable(&sched_uclamp_used); | |
7274a5c1 | 1740 | uclamp_update_root_tg(); |
46609ce2 | 1741 | } |
7274a5c1 | 1742 | |
13685c4a QY |
1743 | if (old_min_rt != sysctl_sched_uclamp_util_min_rt_default) { |
1744 | static_branch_enable(&sched_uclamp_used); | |
1745 | uclamp_sync_util_min_rt_default(); | |
1746 | } | |
7274a5c1 | 1747 | |
e8f14172 | 1748 | /* |
7274a5c1 PB |
1749 | * We update all RUNNABLE tasks only when task groups are in use. |
1750 | * Otherwise, keep it simple and do just a lazy update at each next | |
1751 | * task enqueue time. | |
e8f14172 | 1752 | */ |
7274a5c1 | 1753 | |
e8f14172 PB |
1754 | goto done; |
1755 | ||
1756 | undo: | |
1757 | sysctl_sched_uclamp_util_min = old_min; | |
1758 | sysctl_sched_uclamp_util_max = old_max; | |
13685c4a | 1759 | sysctl_sched_uclamp_util_min_rt_default = old_min_rt; |
e8f14172 | 1760 | done: |
2480c093 | 1761 | mutex_unlock(&uclamp_mutex); |
e8f14172 PB |
1762 | |
1763 | return result; | |
1764 | } | |
1765 | ||
a509a7cd PB |
1766 | static int uclamp_validate(struct task_struct *p, |
1767 | const struct sched_attr *attr) | |
1768 | { | |
480a6ca2 DE |
1769 | int util_min = p->uclamp_req[UCLAMP_MIN].value; |
1770 | int util_max = p->uclamp_req[UCLAMP_MAX].value; | |
a509a7cd | 1771 | |
480a6ca2 DE |
1772 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN) { |
1773 | util_min = attr->sched_util_min; | |
a509a7cd | 1774 | |
480a6ca2 DE |
1775 | if (util_min + 1 > SCHED_CAPACITY_SCALE + 1) |
1776 | return -EINVAL; | |
1777 | } | |
1778 | ||
1779 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX) { | |
1780 | util_max = attr->sched_util_max; | |
1781 | ||
1782 | if (util_max + 1 > SCHED_CAPACITY_SCALE + 1) | |
1783 | return -EINVAL; | |
1784 | } | |
1785 | ||
1786 | if (util_min != -1 && util_max != -1 && util_min > util_max) | |
a509a7cd PB |
1787 | return -EINVAL; |
1788 | ||
e65855a5 QY |
1789 | /* |
1790 | * We have valid uclamp attributes; make sure uclamp is enabled. | |
1791 | * | |
1792 | * We need to do that here, because enabling static branches is a | |
1793 | * blocking operation which obviously cannot be done while holding | |
1794 | * scheduler locks. | |
1795 | */ | |
1796 | static_branch_enable(&sched_uclamp_used); | |
1797 | ||
a509a7cd PB |
1798 | return 0; |
1799 | } | |
1800 | ||
480a6ca2 DE |
1801 | static bool uclamp_reset(const struct sched_attr *attr, |
1802 | enum uclamp_id clamp_id, | |
1803 | struct uclamp_se *uc_se) | |
1804 | { | |
1805 | /* Reset on sched class change for a non user-defined clamp value. */ | |
1806 | if (likely(!(attr->sched_flags & SCHED_FLAG_UTIL_CLAMP)) && | |
1807 | !uc_se->user_defined) | |
1808 | return true; | |
1809 | ||
1810 | /* Reset on sched_util_{min,max} == -1. */ | |
1811 | if (clamp_id == UCLAMP_MIN && | |
1812 | attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN && | |
1813 | attr->sched_util_min == -1) { | |
1814 | return true; | |
1815 | } | |
1816 | ||
1817 | if (clamp_id == UCLAMP_MAX && | |
1818 | attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX && | |
1819 | attr->sched_util_max == -1) { | |
1820 | return true; | |
1821 | } | |
1822 | ||
1823 | return false; | |
1824 | } | |
1825 | ||
a509a7cd PB |
1826 | static void __setscheduler_uclamp(struct task_struct *p, |
1827 | const struct sched_attr *attr) | |
1828 | { | |
0413d7f3 | 1829 | enum uclamp_id clamp_id; |
1a00d999 | 1830 | |
1a00d999 PB |
1831 | for_each_clamp_id(clamp_id) { |
1832 | struct uclamp_se *uc_se = &p->uclamp_req[clamp_id]; | |
480a6ca2 | 1833 | unsigned int value; |
1a00d999 | 1834 | |
480a6ca2 | 1835 | if (!uclamp_reset(attr, clamp_id, uc_se)) |
1a00d999 PB |
1836 | continue; |
1837 | ||
13685c4a QY |
1838 | /* |
1839 | * RT by default have a 100% boost value that could be modified | |
1840 | * at runtime. | |
1841 | */ | |
1a00d999 | 1842 | if (unlikely(rt_task(p) && clamp_id == UCLAMP_MIN)) |
480a6ca2 | 1843 | value = sysctl_sched_uclamp_util_min_rt_default; |
13685c4a | 1844 | else |
480a6ca2 DE |
1845 | value = uclamp_none(clamp_id); |
1846 | ||
1847 | uclamp_se_set(uc_se, value, false); | |
1a00d999 | 1848 | |
1a00d999 PB |
1849 | } |
1850 | ||
a509a7cd PB |
1851 | if (likely(!(attr->sched_flags & SCHED_FLAG_UTIL_CLAMP))) |
1852 | return; | |
1853 | ||
480a6ca2 DE |
1854 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN && |
1855 | attr->sched_util_min != -1) { | |
a509a7cd PB |
1856 | uclamp_se_set(&p->uclamp_req[UCLAMP_MIN], |
1857 | attr->sched_util_min, true); | |
1858 | } | |
1859 | ||
480a6ca2 DE |
1860 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX && |
1861 | attr->sched_util_max != -1) { | |
a509a7cd PB |
1862 | uclamp_se_set(&p->uclamp_req[UCLAMP_MAX], |
1863 | attr->sched_util_max, true); | |
1864 | } | |
1865 | } | |
1866 | ||
e8f14172 PB |
1867 | static void uclamp_fork(struct task_struct *p) |
1868 | { | |
0413d7f3 | 1869 | enum uclamp_id clamp_id; |
e8f14172 | 1870 | |
13685c4a QY |
1871 | /* |
1872 | * We don't need to hold task_rq_lock() when updating p->uclamp_* here | |
1873 | * as the task is still at its early fork stages. | |
1874 | */ | |
e8f14172 PB |
1875 | for_each_clamp_id(clamp_id) |
1876 | p->uclamp[clamp_id].active = false; | |
a87498ac PB |
1877 | |
1878 | if (likely(!p->sched_reset_on_fork)) | |
1879 | return; | |
1880 | ||
1881 | for_each_clamp_id(clamp_id) { | |
eaf5a92e QP |
1882 | uclamp_se_set(&p->uclamp_req[clamp_id], |
1883 | uclamp_none(clamp_id), false); | |
a87498ac | 1884 | } |
e8f14172 PB |
1885 | } |
1886 | ||
13685c4a QY |
1887 | static void uclamp_post_fork(struct task_struct *p) |
1888 | { | |
1889 | uclamp_update_util_min_rt_default(p); | |
1890 | } | |
1891 | ||
d81ae8aa QY |
1892 | static void __init init_uclamp_rq(struct rq *rq) |
1893 | { | |
1894 | enum uclamp_id clamp_id; | |
1895 | struct uclamp_rq *uc_rq = rq->uclamp; | |
1896 | ||
1897 | for_each_clamp_id(clamp_id) { | |
1898 | uc_rq[clamp_id] = (struct uclamp_rq) { | |
1899 | .value = uclamp_none(clamp_id) | |
1900 | }; | |
1901 | } | |
1902 | ||
1903 | rq->uclamp_flags = 0; | |
1904 | } | |
1905 | ||
69842cba PB |
1906 | static void __init init_uclamp(void) |
1907 | { | |
e8f14172 | 1908 | struct uclamp_se uc_max = {}; |
0413d7f3 | 1909 | enum uclamp_id clamp_id; |
69842cba PB |
1910 | int cpu; |
1911 | ||
d81ae8aa QY |
1912 | for_each_possible_cpu(cpu) |
1913 | init_uclamp_rq(cpu_rq(cpu)); | |
69842cba | 1914 | |
69842cba | 1915 | for_each_clamp_id(clamp_id) { |
e8f14172 | 1916 | uclamp_se_set(&init_task.uclamp_req[clamp_id], |
a509a7cd | 1917 | uclamp_none(clamp_id), false); |
69842cba | 1918 | } |
e8f14172 PB |
1919 | |
1920 | /* System defaults allow max clamp values for both indexes */ | |
a509a7cd | 1921 | uclamp_se_set(&uc_max, uclamp_none(UCLAMP_MAX), false); |
2480c093 | 1922 | for_each_clamp_id(clamp_id) { |
e8f14172 | 1923 | uclamp_default[clamp_id] = uc_max; |
2480c093 PB |
1924 | #ifdef CONFIG_UCLAMP_TASK_GROUP |
1925 | root_task_group.uclamp_req[clamp_id] = uc_max; | |
0b60ba2d | 1926 | root_task_group.uclamp[clamp_id] = uc_max; |
2480c093 PB |
1927 | #endif |
1928 | } | |
69842cba PB |
1929 | } |
1930 | ||
1931 | #else /* CONFIG_UCLAMP_TASK */ | |
1932 | static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p) { } | |
1933 | static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p) { } | |
a509a7cd PB |
1934 | static inline int uclamp_validate(struct task_struct *p, |
1935 | const struct sched_attr *attr) | |
1936 | { | |
1937 | return -EOPNOTSUPP; | |
1938 | } | |
1939 | static void __setscheduler_uclamp(struct task_struct *p, | |
1940 | const struct sched_attr *attr) { } | |
e8f14172 | 1941 | static inline void uclamp_fork(struct task_struct *p) { } |
13685c4a | 1942 | static inline void uclamp_post_fork(struct task_struct *p) { } |
69842cba PB |
1943 | static inline void init_uclamp(void) { } |
1944 | #endif /* CONFIG_UCLAMP_TASK */ | |
1945 | ||
1de64443 | 1946 | static inline void enqueue_task(struct rq *rq, struct task_struct *p, int flags) |
2087a1ad | 1947 | { |
0a67d1ee PZ |
1948 | if (!(flags & ENQUEUE_NOCLOCK)) |
1949 | update_rq_clock(rq); | |
1950 | ||
eb414681 | 1951 | if (!(flags & ENQUEUE_RESTORE)) { |
4e29fb70 | 1952 | sched_info_enqueue(rq, p); |
eb414681 JW |
1953 | psi_enqueue(p, flags & ENQUEUE_WAKEUP); |
1954 | } | |
0a67d1ee | 1955 | |
69842cba | 1956 | uclamp_rq_inc(rq, p); |
371fd7e7 | 1957 | p->sched_class->enqueue_task(rq, p, flags); |
8a311c74 PZ |
1958 | |
1959 | if (sched_core_enabled(rq)) | |
1960 | sched_core_enqueue(rq, p); | |
71f8bd46 IM |
1961 | } |
1962 | ||
1de64443 | 1963 | static inline void dequeue_task(struct rq *rq, struct task_struct *p, int flags) |
71f8bd46 | 1964 | { |
8a311c74 PZ |
1965 | if (sched_core_enabled(rq)) |
1966 | sched_core_dequeue(rq, p); | |
1967 | ||
0a67d1ee PZ |
1968 | if (!(flags & DEQUEUE_NOCLOCK)) |
1969 | update_rq_clock(rq); | |
1970 | ||
eb414681 | 1971 | if (!(flags & DEQUEUE_SAVE)) { |
4e29fb70 | 1972 | sched_info_dequeue(rq, p); |
eb414681 JW |
1973 | psi_dequeue(p, flags & DEQUEUE_SLEEP); |
1974 | } | |
0a67d1ee | 1975 | |
69842cba | 1976 | uclamp_rq_dec(rq, p); |
371fd7e7 | 1977 | p->sched_class->dequeue_task(rq, p, flags); |
71f8bd46 IM |
1978 | } |
1979 | ||
029632fb | 1980 | void activate_task(struct rq *rq, struct task_struct *p, int flags) |
1e3c88bd | 1981 | { |
371fd7e7 | 1982 | enqueue_task(rq, p, flags); |
7dd77884 PZ |
1983 | |
1984 | p->on_rq = TASK_ON_RQ_QUEUED; | |
1e3c88bd PZ |
1985 | } |
1986 | ||
029632fb | 1987 | void deactivate_task(struct rq *rq, struct task_struct *p, int flags) |
1e3c88bd | 1988 | { |
7dd77884 PZ |
1989 | p->on_rq = (flags & DEQUEUE_SLEEP) ? 0 : TASK_ON_RQ_MIGRATING; |
1990 | ||
371fd7e7 | 1991 | dequeue_task(rq, p, flags); |
1e3c88bd PZ |
1992 | } |
1993 | ||
14531189 | 1994 | /* |
dd41f596 | 1995 | * __normal_prio - return the priority that is based on the static prio |
14531189 | 1996 | */ |
14531189 IM |
1997 | static inline int __normal_prio(struct task_struct *p) |
1998 | { | |
dd41f596 | 1999 | return p->static_prio; |
14531189 IM |
2000 | } |
2001 | ||
b29739f9 IM |
2002 | /* |
2003 | * Calculate the expected normal priority: i.e. priority | |
2004 | * without taking RT-inheritance into account. Might be | |
2005 | * boosted by interactivity modifiers. Changes upon fork, | |
2006 | * setprio syscalls, and whenever the interactivity | |
2007 | * estimator recalculates. | |
2008 | */ | |
36c8b586 | 2009 | static inline int normal_prio(struct task_struct *p) |
b29739f9 IM |
2010 | { |
2011 | int prio; | |
2012 | ||
aab03e05 DF |
2013 | if (task_has_dl_policy(p)) |
2014 | prio = MAX_DL_PRIO-1; | |
2015 | else if (task_has_rt_policy(p)) | |
b29739f9 IM |
2016 | prio = MAX_RT_PRIO-1 - p->rt_priority; |
2017 | else | |
2018 | prio = __normal_prio(p); | |
2019 | return prio; | |
2020 | } | |
2021 | ||
2022 | /* | |
2023 | * Calculate the current priority, i.e. the priority | |
2024 | * taken into account by the scheduler. This value might | |
2025 | * be boosted by RT tasks, or might be boosted by | |
2026 | * interactivity modifiers. Will be RT if the task got | |
2027 | * RT-boosted. If not then it returns p->normal_prio. | |
2028 | */ | |
36c8b586 | 2029 | static int effective_prio(struct task_struct *p) |
b29739f9 IM |
2030 | { |
2031 | p->normal_prio = normal_prio(p); | |
2032 | /* | |
2033 | * If we are RT tasks or we were boosted to RT priority, | |
2034 | * keep the priority unchanged. Otherwise, update priority | |
2035 | * to the normal priority: | |
2036 | */ | |
2037 | if (!rt_prio(p->prio)) | |
2038 | return p->normal_prio; | |
2039 | return p->prio; | |
2040 | } | |
2041 | ||
1da177e4 LT |
2042 | /** |
2043 | * task_curr - is this task currently executing on a CPU? | |
2044 | * @p: the task in question. | |
e69f6186 YB |
2045 | * |
2046 | * Return: 1 if the task is currently executing. 0 otherwise. | |
1da177e4 | 2047 | */ |
36c8b586 | 2048 | inline int task_curr(const struct task_struct *p) |
1da177e4 LT |
2049 | { |
2050 | return cpu_curr(task_cpu(p)) == p; | |
2051 | } | |
2052 | ||
67dfa1b7 | 2053 | /* |
4c9a4bc8 PZ |
2054 | * switched_from, switched_to and prio_changed must _NOT_ drop rq->lock, |
2055 | * use the balance_callback list if you want balancing. | |
2056 | * | |
2057 | * this means any call to check_class_changed() must be followed by a call to | |
2058 | * balance_callback(). | |
67dfa1b7 | 2059 | */ |
cb469845 SR |
2060 | static inline void check_class_changed(struct rq *rq, struct task_struct *p, |
2061 | const struct sched_class *prev_class, | |
da7a735e | 2062 | int oldprio) |
cb469845 SR |
2063 | { |
2064 | if (prev_class != p->sched_class) { | |
2065 | if (prev_class->switched_from) | |
da7a735e | 2066 | prev_class->switched_from(rq, p); |
4c9a4bc8 | 2067 | |
da7a735e | 2068 | p->sched_class->switched_to(rq, p); |
2d3d891d | 2069 | } else if (oldprio != p->prio || dl_task(p)) |
da7a735e | 2070 | p->sched_class->prio_changed(rq, p, oldprio); |
cb469845 SR |
2071 | } |
2072 | ||
029632fb | 2073 | void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags) |
1e5a7405 | 2074 | { |
aa93cd53 | 2075 | if (p->sched_class == rq->curr->sched_class) |
1e5a7405 | 2076 | rq->curr->sched_class->check_preempt_curr(rq, p, flags); |
aa93cd53 KT |
2077 | else if (p->sched_class > rq->curr->sched_class) |
2078 | resched_curr(rq); | |
1e5a7405 PZ |
2079 | |
2080 | /* | |
2081 | * A queue event has occurred, and we're going to schedule. In | |
2082 | * this case, we can save a useless back to back clock update. | |
2083 | */ | |
da0c1e65 | 2084 | if (task_on_rq_queued(rq->curr) && test_tsk_need_resched(rq->curr)) |
adcc8da8 | 2085 | rq_clock_skip_update(rq); |
1e5a7405 PZ |
2086 | } |
2087 | ||
1da177e4 | 2088 | #ifdef CONFIG_SMP |
175f0e25 | 2089 | |
af449901 PZ |
2090 | static void |
2091 | __do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask, u32 flags); | |
2092 | ||
2093 | static int __set_cpus_allowed_ptr(struct task_struct *p, | |
2094 | const struct cpumask *new_mask, | |
2095 | u32 flags); | |
2096 | ||
2097 | static void migrate_disable_switch(struct rq *rq, struct task_struct *p) | |
2098 | { | |
2099 | if (likely(!p->migration_disabled)) | |
2100 | return; | |
2101 | ||
2102 | if (p->cpus_ptr != &p->cpus_mask) | |
2103 | return; | |
2104 | ||
2105 | /* | |
2106 | * Violates locking rules! see comment in __do_set_cpus_allowed(). | |
2107 | */ | |
2108 | __do_set_cpus_allowed(p, cpumask_of(rq->cpu), SCA_MIGRATE_DISABLE); | |
2109 | } | |
2110 | ||
2111 | void migrate_disable(void) | |
2112 | { | |
3015ef4b TG |
2113 | struct task_struct *p = current; |
2114 | ||
2115 | if (p->migration_disabled) { | |
2116 | p->migration_disabled++; | |
af449901 | 2117 | return; |
3015ef4b | 2118 | } |
af449901 | 2119 | |
3015ef4b TG |
2120 | preempt_disable(); |
2121 | this_rq()->nr_pinned++; | |
2122 | p->migration_disabled = 1; | |
2123 | preempt_enable(); | |
af449901 PZ |
2124 | } |
2125 | EXPORT_SYMBOL_GPL(migrate_disable); | |
2126 | ||
2127 | void migrate_enable(void) | |
2128 | { | |
2129 | struct task_struct *p = current; | |
2130 | ||
6d337eab PZ |
2131 | if (p->migration_disabled > 1) { |
2132 | p->migration_disabled--; | |
af449901 | 2133 | return; |
6d337eab | 2134 | } |
af449901 | 2135 | |
6d337eab PZ |
2136 | /* |
2137 | * Ensure stop_task runs either before or after this, and that | |
2138 | * __set_cpus_allowed_ptr(SCA_MIGRATE_ENABLE) doesn't schedule(). | |
2139 | */ | |
2140 | preempt_disable(); | |
2141 | if (p->cpus_ptr != &p->cpus_mask) | |
2142 | __set_cpus_allowed_ptr(p, &p->cpus_mask, SCA_MIGRATE_ENABLE); | |
2143 | /* | |
2144 | * Mustn't clear migration_disabled() until cpus_ptr points back at the | |
2145 | * regular cpus_mask, otherwise things that race (eg. | |
2146 | * select_fallback_rq) get confused. | |
2147 | */ | |
af449901 | 2148 | barrier(); |
6d337eab | 2149 | p->migration_disabled = 0; |
3015ef4b | 2150 | this_rq()->nr_pinned--; |
6d337eab | 2151 | preempt_enable(); |
af449901 PZ |
2152 | } |
2153 | EXPORT_SYMBOL_GPL(migrate_enable); | |
2154 | ||
3015ef4b TG |
2155 | static inline bool rq_has_pinned_tasks(struct rq *rq) |
2156 | { | |
2157 | return rq->nr_pinned; | |
2158 | } | |
2159 | ||
175f0e25 | 2160 | /* |
bee98539 | 2161 | * Per-CPU kthreads are allowed to run on !active && online CPUs, see |
175f0e25 PZ |
2162 | * __set_cpus_allowed_ptr() and select_fallback_rq(). |
2163 | */ | |
2164 | static inline bool is_cpu_allowed(struct task_struct *p, int cpu) | |
2165 | { | |
5ba2ffba | 2166 | /* When not in the task's cpumask, no point in looking further. */ |
3bd37062 | 2167 | if (!cpumask_test_cpu(cpu, p->cpus_ptr)) |
175f0e25 PZ |
2168 | return false; |
2169 | ||
5ba2ffba PZ |
2170 | /* migrate_disabled() must be allowed to finish. */ |
2171 | if (is_migration_disabled(p)) | |
175f0e25 PZ |
2172 | return cpu_online(cpu); |
2173 | ||
5ba2ffba PZ |
2174 | /* Non kernel threads are not allowed during either online or offline. */ |
2175 | if (!(p->flags & PF_KTHREAD)) | |
9ae606bc | 2176 | return cpu_active(cpu) && task_cpu_possible(cpu, p); |
5ba2ffba PZ |
2177 | |
2178 | /* KTHREAD_IS_PER_CPU is always allowed. */ | |
2179 | if (kthread_is_per_cpu(p)) | |
2180 | return cpu_online(cpu); | |
2181 | ||
2182 | /* Regular kernel threads don't get to stay during offline. */ | |
b5c44773 | 2183 | if (cpu_dying(cpu)) |
5ba2ffba PZ |
2184 | return false; |
2185 | ||
2186 | /* But are allowed during online. */ | |
2187 | return cpu_online(cpu); | |
175f0e25 PZ |
2188 | } |
2189 | ||
5cc389bc PZ |
2190 | /* |
2191 | * This is how migration works: | |
2192 | * | |
2193 | * 1) we invoke migration_cpu_stop() on the target CPU using | |
2194 | * stop_one_cpu(). | |
2195 | * 2) stopper starts to run (implicitly forcing the migrated thread | |
2196 | * off the CPU) | |
2197 | * 3) it checks whether the migrated task is still in the wrong runqueue. | |
2198 | * 4) if it's in the wrong runqueue then the migration thread removes | |
2199 | * it and puts it into the right queue. | |
2200 | * 5) stopper completes and stop_one_cpu() returns and the migration | |
2201 | * is done. | |
2202 | */ | |
2203 | ||
2204 | /* | |
2205 | * move_queued_task - move a queued task to new rq. | |
2206 | * | |
2207 | * Returns (locked) new rq. Old rq's lock is released. | |
2208 | */ | |
8a8c69c3 PZ |
2209 | static struct rq *move_queued_task(struct rq *rq, struct rq_flags *rf, |
2210 | struct task_struct *p, int new_cpu) | |
5cc389bc | 2211 | { |
5cb9eaa3 | 2212 | lockdep_assert_rq_held(rq); |
5cc389bc | 2213 | |
58877d34 | 2214 | deactivate_task(rq, p, DEQUEUE_NOCLOCK); |
5cc389bc | 2215 | set_task_cpu(p, new_cpu); |
8a8c69c3 | 2216 | rq_unlock(rq, rf); |
5cc389bc PZ |
2217 | |
2218 | rq = cpu_rq(new_cpu); | |
2219 | ||
8a8c69c3 | 2220 | rq_lock(rq, rf); |
5cc389bc | 2221 | BUG_ON(task_cpu(p) != new_cpu); |
58877d34 | 2222 | activate_task(rq, p, 0); |
5cc389bc PZ |
2223 | check_preempt_curr(rq, p, 0); |
2224 | ||
2225 | return rq; | |
2226 | } | |
2227 | ||
2228 | struct migration_arg { | |
6d337eab PZ |
2229 | struct task_struct *task; |
2230 | int dest_cpu; | |
2231 | struct set_affinity_pending *pending; | |
2232 | }; | |
2233 | ||
50caf9c1 PZ |
2234 | /* |
2235 | * @refs: number of wait_for_completion() | |
2236 | * @stop_pending: is @stop_work in use | |
2237 | */ | |
6d337eab PZ |
2238 | struct set_affinity_pending { |
2239 | refcount_t refs; | |
9e81889c | 2240 | unsigned int stop_pending; |
6d337eab PZ |
2241 | struct completion done; |
2242 | struct cpu_stop_work stop_work; | |
2243 | struct migration_arg arg; | |
5cc389bc PZ |
2244 | }; |
2245 | ||
2246 | /* | |
d1ccc66d | 2247 | * Move (not current) task off this CPU, onto the destination CPU. We're doing |
5cc389bc PZ |
2248 | * this because either it can't run here any more (set_cpus_allowed() |
2249 | * away from this CPU, or CPU going down), or because we're | |
2250 | * attempting to rebalance this task on exec (sched_exec). | |
2251 | * | |
2252 | * So we race with normal scheduler movements, but that's OK, as long | |
2253 | * as the task is no longer on this CPU. | |
5cc389bc | 2254 | */ |
8a8c69c3 PZ |
2255 | static struct rq *__migrate_task(struct rq *rq, struct rq_flags *rf, |
2256 | struct task_struct *p, int dest_cpu) | |
5cc389bc | 2257 | { |
5cc389bc | 2258 | /* Affinity changed (again). */ |
175f0e25 | 2259 | if (!is_cpu_allowed(p, dest_cpu)) |
5e16bbc2 | 2260 | return rq; |
5cc389bc | 2261 | |
15ff991e | 2262 | update_rq_clock(rq); |
8a8c69c3 | 2263 | rq = move_queued_task(rq, rf, p, dest_cpu); |
5e16bbc2 PZ |
2264 | |
2265 | return rq; | |
5cc389bc PZ |
2266 | } |
2267 | ||
2268 | /* | |
2269 | * migration_cpu_stop - this will be executed by a highprio stopper thread | |
2270 | * and performs thread migration by bumping thread off CPU then | |
2271 | * 'pushing' onto another runqueue. | |
2272 | */ | |
2273 | static int migration_cpu_stop(void *data) | |
2274 | { | |
2275 | struct migration_arg *arg = data; | |
c20cf065 | 2276 | struct set_affinity_pending *pending = arg->pending; |
5e16bbc2 PZ |
2277 | struct task_struct *p = arg->task; |
2278 | struct rq *rq = this_rq(); | |
6d337eab | 2279 | bool complete = false; |
8a8c69c3 | 2280 | struct rq_flags rf; |
5cc389bc PZ |
2281 | |
2282 | /* | |
d1ccc66d IM |
2283 | * The original target CPU might have gone down and we might |
2284 | * be on another CPU but it doesn't matter. | |
5cc389bc | 2285 | */ |
6d337eab | 2286 | local_irq_save(rf.flags); |
5cc389bc PZ |
2287 | /* |
2288 | * We need to explicitly wake pending tasks before running | |
3bd37062 | 2289 | * __migrate_task() such that we will not miss enforcing cpus_ptr |
5cc389bc PZ |
2290 | * during wakeups, see set_cpus_allowed_ptr()'s TASK_WAKING test. |
2291 | */ | |
a1488664 | 2292 | flush_smp_call_function_from_idle(); |
5e16bbc2 PZ |
2293 | |
2294 | raw_spin_lock(&p->pi_lock); | |
8a8c69c3 | 2295 | rq_lock(rq, &rf); |
6d337eab | 2296 | |
e140749c VS |
2297 | /* |
2298 | * If we were passed a pending, then ->stop_pending was set, thus | |
2299 | * p->migration_pending must have remained stable. | |
2300 | */ | |
2301 | WARN_ON_ONCE(pending && pending != p->migration_pending); | |
2302 | ||
5e16bbc2 PZ |
2303 | /* |
2304 | * If task_rq(p) != rq, it cannot be migrated here, because we're | |
2305 | * holding rq->lock, if p->on_rq == 0 it cannot get enqueued because | |
2306 | * we're holding p->pi_lock. | |
2307 | */ | |
bf89a304 | 2308 | if (task_rq(p) == rq) { |
6d337eab PZ |
2309 | if (is_migration_disabled(p)) |
2310 | goto out; | |
2311 | ||
2312 | if (pending) { | |
e140749c | 2313 | p->migration_pending = NULL; |
6d337eab | 2314 | complete = true; |
6d337eab | 2315 | |
3f1bc119 PZ |
2316 | if (cpumask_test_cpu(task_cpu(p), &p->cpus_mask)) |
2317 | goto out; | |
3f1bc119 | 2318 | } |
6d337eab | 2319 | |
bf89a304 | 2320 | if (task_on_rq_queued(p)) |
475ea6c6 | 2321 | rq = __migrate_task(rq, &rf, p, arg->dest_cpu); |
bf89a304 | 2322 | else |
475ea6c6 | 2323 | p->wake_cpu = arg->dest_cpu; |
6d337eab | 2324 | |
3f1bc119 PZ |
2325 | /* |
2326 | * XXX __migrate_task() can fail, at which point we might end | |
2327 | * up running on a dodgy CPU, AFAICT this can only happen | |
2328 | * during CPU hotplug, at which point we'll get pushed out | |
2329 | * anyway, so it's probably not a big deal. | |
2330 | */ | |
2331 | ||
c20cf065 | 2332 | } else if (pending) { |
6d337eab PZ |
2333 | /* |
2334 | * This happens when we get migrated between migrate_enable()'s | |
2335 | * preempt_enable() and scheduling the stopper task. At that | |
2336 | * point we're a regular task again and not current anymore. | |
2337 | * | |
2338 | * A !PREEMPT kernel has a giant hole here, which makes it far | |
2339 | * more likely. | |
2340 | */ | |
2341 | ||
d707faa6 VS |
2342 | /* |
2343 | * The task moved before the stopper got to run. We're holding | |
2344 | * ->pi_lock, so the allowed mask is stable - if it got | |
2345 | * somewhere allowed, we're done. | |
2346 | */ | |
c20cf065 | 2347 | if (cpumask_test_cpu(task_cpu(p), p->cpus_ptr)) { |
e140749c | 2348 | p->migration_pending = NULL; |
d707faa6 VS |
2349 | complete = true; |
2350 | goto out; | |
2351 | } | |
2352 | ||
6d337eab PZ |
2353 | /* |
2354 | * When migrate_enable() hits a rq mis-match we can't reliably | |
2355 | * determine is_migration_disabled() and so have to chase after | |
2356 | * it. | |
2357 | */ | |
9e81889c | 2358 | WARN_ON_ONCE(!pending->stop_pending); |
6d337eab PZ |
2359 | task_rq_unlock(rq, p, &rf); |
2360 | stop_one_cpu_nowait(task_cpu(p), migration_cpu_stop, | |
2361 | &pending->arg, &pending->stop_work); | |
2362 | return 0; | |
bf89a304 | 2363 | } |
6d337eab | 2364 | out: |
9e81889c PZ |
2365 | if (pending) |
2366 | pending->stop_pending = false; | |
6d337eab PZ |
2367 | task_rq_unlock(rq, p, &rf); |
2368 | ||
2369 | if (complete) | |
2370 | complete_all(&pending->done); | |
2371 | ||
5cc389bc PZ |
2372 | return 0; |
2373 | } | |
2374 | ||
a7c81556 PZ |
2375 | int push_cpu_stop(void *arg) |
2376 | { | |
2377 | struct rq *lowest_rq = NULL, *rq = this_rq(); | |
2378 | struct task_struct *p = arg; | |
2379 | ||
2380 | raw_spin_lock_irq(&p->pi_lock); | |
5cb9eaa3 | 2381 | raw_spin_rq_lock(rq); |
a7c81556 PZ |
2382 | |
2383 | if (task_rq(p) != rq) | |
2384 | goto out_unlock; | |
2385 | ||
2386 | if (is_migration_disabled(p)) { | |
2387 | p->migration_flags |= MDF_PUSH; | |
2388 | goto out_unlock; | |
2389 | } | |
2390 | ||
2391 | p->migration_flags &= ~MDF_PUSH; | |
2392 | ||
2393 | if (p->sched_class->find_lock_rq) | |
2394 | lowest_rq = p->sched_class->find_lock_rq(p, rq); | |
5e16bbc2 | 2395 | |
a7c81556 PZ |
2396 | if (!lowest_rq) |
2397 | goto out_unlock; | |
2398 | ||
2399 | // XXX validate p is still the highest prio task | |
2400 | if (task_rq(p) == rq) { | |
2401 | deactivate_task(rq, p, 0); | |
2402 | set_task_cpu(p, lowest_rq->cpu); | |
2403 | activate_task(lowest_rq, p, 0); | |
2404 | resched_curr(lowest_rq); | |
2405 | } | |
2406 | ||
2407 | double_unlock_balance(rq, lowest_rq); | |
2408 | ||
2409 | out_unlock: | |
2410 | rq->push_busy = false; | |
5cb9eaa3 | 2411 | raw_spin_rq_unlock(rq); |
a7c81556 PZ |
2412 | raw_spin_unlock_irq(&p->pi_lock); |
2413 | ||
2414 | put_task_struct(p); | |
5cc389bc PZ |
2415 | return 0; |
2416 | } | |
2417 | ||
c5b28038 PZ |
2418 | /* |
2419 | * sched_class::set_cpus_allowed must do the below, but is not required to | |
2420 | * actually call this function. | |
2421 | */ | |
9cfc3e18 | 2422 | void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask, u32 flags) |
5cc389bc | 2423 | { |
af449901 PZ |
2424 | if (flags & (SCA_MIGRATE_ENABLE | SCA_MIGRATE_DISABLE)) { |
2425 | p->cpus_ptr = new_mask; | |
2426 | return; | |
2427 | } | |
2428 | ||
3bd37062 | 2429 | cpumask_copy(&p->cpus_mask, new_mask); |
5cc389bc PZ |
2430 | p->nr_cpus_allowed = cpumask_weight(new_mask); |
2431 | } | |
2432 | ||
9cfc3e18 PZ |
2433 | static void |
2434 | __do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask, u32 flags) | |
c5b28038 | 2435 | { |
6c37067e PZ |
2436 | struct rq *rq = task_rq(p); |
2437 | bool queued, running; | |
2438 | ||
af449901 PZ |
2439 | /* |
2440 | * This here violates the locking rules for affinity, since we're only | |
2441 | * supposed to change these variables while holding both rq->lock and | |
2442 | * p->pi_lock. | |
2443 | * | |
2444 | * HOWEVER, it magically works, because ttwu() is the only code that | |
2445 | * accesses these variables under p->pi_lock and only does so after | |
2446 | * smp_cond_load_acquire(&p->on_cpu, !VAL), and we're in __schedule() | |
2447 | * before finish_task(). | |
2448 | * | |
2449 | * XXX do further audits, this smells like something putrid. | |
2450 | */ | |
2451 | if (flags & SCA_MIGRATE_DISABLE) | |
2452 | SCHED_WARN_ON(!p->on_cpu); | |
2453 | else | |
2454 | lockdep_assert_held(&p->pi_lock); | |
6c37067e PZ |
2455 | |
2456 | queued = task_on_rq_queued(p); | |
2457 | running = task_current(rq, p); | |
2458 | ||
2459 | if (queued) { | |
2460 | /* | |
2461 | * Because __kthread_bind() calls this on blocked tasks without | |
2462 | * holding rq->lock. | |
2463 | */ | |
5cb9eaa3 | 2464 | lockdep_assert_rq_held(rq); |
7a57f32a | 2465 | dequeue_task(rq, p, DEQUEUE_SAVE | DEQUEUE_NOCLOCK); |
6c37067e PZ |
2466 | } |
2467 | if (running) | |
2468 | put_prev_task(rq, p); | |
2469 | ||
9cfc3e18 | 2470 | p->sched_class->set_cpus_allowed(p, new_mask, flags); |
6c37067e | 2471 | |
6c37067e | 2472 | if (queued) |
7134b3e9 | 2473 | enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK); |
a399d233 | 2474 | if (running) |
03b7fad1 | 2475 | set_next_task(rq, p); |
c5b28038 PZ |
2476 | } |
2477 | ||
9cfc3e18 PZ |
2478 | void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask) |
2479 | { | |
2480 | __do_set_cpus_allowed(p, new_mask, 0); | |
2481 | } | |
2482 | ||
6d337eab | 2483 | /* |
c777d847 VS |
2484 | * This function is wildly self concurrent; here be dragons. |
2485 | * | |
2486 | * | |
2487 | * When given a valid mask, __set_cpus_allowed_ptr() must block until the | |
2488 | * designated task is enqueued on an allowed CPU. If that task is currently | |
2489 | * running, we have to kick it out using the CPU stopper. | |
2490 | * | |
2491 | * Migrate-Disable comes along and tramples all over our nice sandcastle. | |
2492 | * Consider: | |
2493 | * | |
2494 | * Initial conditions: P0->cpus_mask = [0, 1] | |
2495 | * | |
2496 | * P0@CPU0 P1 | |
2497 | * | |
2498 | * migrate_disable(); | |
2499 | * <preempted> | |
2500 | * set_cpus_allowed_ptr(P0, [1]); | |
2501 | * | |
2502 | * P1 *cannot* return from this set_cpus_allowed_ptr() call until P0 executes | |
2503 | * its outermost migrate_enable() (i.e. it exits its Migrate-Disable region). | |
2504 | * This means we need the following scheme: | |
2505 | * | |
2506 | * P0@CPU0 P1 | |
2507 | * | |
2508 | * migrate_disable(); | |
2509 | * <preempted> | |
2510 | * set_cpus_allowed_ptr(P0, [1]); | |
2511 | * <blocks> | |
2512 | * <resumes> | |
2513 | * migrate_enable(); | |
2514 | * __set_cpus_allowed_ptr(); | |
2515 | * <wakes local stopper> | |
2516 | * `--> <woken on migration completion> | |
2517 | * | |
2518 | * Now the fun stuff: there may be several P1-like tasks, i.e. multiple | |
2519 | * concurrent set_cpus_allowed_ptr(P0, [*]) calls. CPU affinity changes of any | |
2520 | * task p are serialized by p->pi_lock, which we can leverage: the one that | |
2521 | * should come into effect at the end of the Migrate-Disable region is the last | |
2522 | * one. This means we only need to track a single cpumask (i.e. p->cpus_mask), | |
2523 | * but we still need to properly signal those waiting tasks at the appropriate | |
2524 | * moment. | |
2525 | * | |
2526 | * This is implemented using struct set_affinity_pending. The first | |
2527 | * __set_cpus_allowed_ptr() caller within a given Migrate-Disable region will | |
2528 | * setup an instance of that struct and install it on the targeted task_struct. | |
2529 | * Any and all further callers will reuse that instance. Those then wait for | |
2530 | * a completion signaled at the tail of the CPU stopper callback (1), triggered | |
2531 | * on the end of the Migrate-Disable region (i.e. outermost migrate_enable()). | |
2532 | * | |
2533 | * | |
2534 | * (1) In the cases covered above. There is one more where the completion is | |
2535 | * signaled within affine_move_task() itself: when a subsequent affinity request | |
e140749c VS |
2536 | * occurs after the stopper bailed out due to the targeted task still being |
2537 | * Migrate-Disable. Consider: | |
c777d847 VS |
2538 | * |
2539 | * Initial conditions: P0->cpus_mask = [0, 1] | |
2540 | * | |
e140749c VS |
2541 | * CPU0 P1 P2 |
2542 | * <P0> | |
2543 | * migrate_disable(); | |
2544 | * <preempted> | |
c777d847 VS |
2545 | * set_cpus_allowed_ptr(P0, [1]); |
2546 | * <blocks> | |
e140749c VS |
2547 | * <migration/0> |
2548 | * migration_cpu_stop() | |
2549 | * is_migration_disabled() | |
2550 | * <bails> | |
c777d847 VS |
2551 | * set_cpus_allowed_ptr(P0, [0, 1]); |
2552 | * <signal completion> | |
2553 | * <awakes> | |
2554 | * | |
2555 | * Note that the above is safe vs a concurrent migrate_enable(), as any | |
2556 | * pending affinity completion is preceded by an uninstallation of | |
2557 | * p->migration_pending done with p->pi_lock held. | |
6d337eab PZ |
2558 | */ |
2559 | static int affine_move_task(struct rq *rq, struct task_struct *p, struct rq_flags *rf, | |
2560 | int dest_cpu, unsigned int flags) | |
2561 | { | |
2562 | struct set_affinity_pending my_pending = { }, *pending = NULL; | |
9e81889c | 2563 | bool stop_pending, complete = false; |
6d337eab PZ |
2564 | |
2565 | /* Can the task run on the task's current CPU? If so, we're done */ | |
2566 | if (cpumask_test_cpu(task_cpu(p), &p->cpus_mask)) { | |
a7c81556 PZ |
2567 | struct task_struct *push_task = NULL; |
2568 | ||
2569 | if ((flags & SCA_MIGRATE_ENABLE) && | |
2570 | (p->migration_flags & MDF_PUSH) && !rq->push_busy) { | |
2571 | rq->push_busy = true; | |
2572 | push_task = get_task_struct(p); | |
2573 | } | |
2574 | ||
50caf9c1 PZ |
2575 | /* |
2576 | * If there are pending waiters, but no pending stop_work, | |
2577 | * then complete now. | |
2578 | */ | |
6d337eab | 2579 | pending = p->migration_pending; |
50caf9c1 | 2580 | if (pending && !pending->stop_pending) { |
6d337eab PZ |
2581 | p->migration_pending = NULL; |
2582 | complete = true; | |
2583 | } | |
50caf9c1 | 2584 | |
6d337eab PZ |
2585 | task_rq_unlock(rq, p, rf); |
2586 | ||
a7c81556 PZ |
2587 | if (push_task) { |
2588 | stop_one_cpu_nowait(rq->cpu, push_cpu_stop, | |
2589 | p, &rq->push_work); | |
2590 | } | |
2591 | ||
6d337eab | 2592 | if (complete) |
50caf9c1 | 2593 | complete_all(&pending->done); |
6d337eab PZ |
2594 | |
2595 | return 0; | |
2596 | } | |
2597 | ||
2598 | if (!(flags & SCA_MIGRATE_ENABLE)) { | |
2599 | /* serialized by p->pi_lock */ | |
2600 | if (!p->migration_pending) { | |
c777d847 | 2601 | /* Install the request */ |
6d337eab PZ |
2602 | refcount_set(&my_pending.refs, 1); |
2603 | init_completion(&my_pending.done); | |
8a6edb52 PZ |
2604 | my_pending.arg = (struct migration_arg) { |
2605 | .task = p, | |
475ea6c6 | 2606 | .dest_cpu = dest_cpu, |
8a6edb52 PZ |
2607 | .pending = &my_pending, |
2608 | }; | |
2609 | ||
6d337eab PZ |
2610 | p->migration_pending = &my_pending; |
2611 | } else { | |
2612 | pending = p->migration_pending; | |
2613 | refcount_inc(&pending->refs); | |
475ea6c6 VS |
2614 | /* |
2615 | * Affinity has changed, but we've already installed a | |
2616 | * pending. migration_cpu_stop() *must* see this, else | |
2617 | * we risk a completion of the pending despite having a | |
2618 | * task on a disallowed CPU. | |
2619 | * | |
2620 | * Serialized by p->pi_lock, so this is safe. | |
2621 | */ | |
2622 | pending->arg.dest_cpu = dest_cpu; | |
6d337eab PZ |
2623 | } |
2624 | } | |
2625 | pending = p->migration_pending; | |
2626 | /* | |
2627 | * - !MIGRATE_ENABLE: | |
2628 | * we'll have installed a pending if there wasn't one already. | |
2629 | * | |
2630 | * - MIGRATE_ENABLE: | |
2631 | * we're here because the current CPU isn't matching anymore, | |
2632 | * the only way that can happen is because of a concurrent | |
2633 | * set_cpus_allowed_ptr() call, which should then still be | |
2634 | * pending completion. | |
2635 | * | |
2636 | * Either way, we really should have a @pending here. | |
2637 | */ | |
2638 | if (WARN_ON_ONCE(!pending)) { | |
2639 | task_rq_unlock(rq, p, rf); | |
2640 | return -EINVAL; | |
2641 | } | |
2642 | ||
2f064a59 | 2643 | if (task_running(rq, p) || READ_ONCE(p->__state) == TASK_WAKING) { |
c777d847 | 2644 | /* |
58b1a450 PZ |
2645 | * MIGRATE_ENABLE gets here because 'p == current', but for |
2646 | * anything else we cannot do is_migration_disabled(), punt | |
2647 | * and have the stopper function handle it all race-free. | |
c777d847 | 2648 | */ |
9e81889c PZ |
2649 | stop_pending = pending->stop_pending; |
2650 | if (!stop_pending) | |
2651 | pending->stop_pending = true; | |
58b1a450 | 2652 | |
58b1a450 PZ |
2653 | if (flags & SCA_MIGRATE_ENABLE) |
2654 | p->migration_flags &= ~MDF_PUSH; | |
50caf9c1 | 2655 | |
6d337eab | 2656 | task_rq_unlock(rq, p, rf); |
8a6edb52 | 2657 | |
9e81889c PZ |
2658 | if (!stop_pending) { |
2659 | stop_one_cpu_nowait(cpu_of(rq), migration_cpu_stop, | |
2660 | &pending->arg, &pending->stop_work); | |
2661 | } | |
6d337eab | 2662 | |
58b1a450 PZ |
2663 | if (flags & SCA_MIGRATE_ENABLE) |
2664 | return 0; | |
6d337eab PZ |
2665 | } else { |
2666 | ||
2667 | if (!is_migration_disabled(p)) { | |
2668 | if (task_on_rq_queued(p)) | |
2669 | rq = move_queued_task(rq, rf, p, dest_cpu); | |
2670 | ||
50caf9c1 PZ |
2671 | if (!pending->stop_pending) { |
2672 | p->migration_pending = NULL; | |
2673 | complete = true; | |
2674 | } | |
6d337eab PZ |
2675 | } |
2676 | task_rq_unlock(rq, p, rf); | |
2677 | ||
6d337eab PZ |
2678 | if (complete) |
2679 | complete_all(&pending->done); | |
2680 | } | |
2681 | ||
2682 | wait_for_completion(&pending->done); | |
2683 | ||
2684 | if (refcount_dec_and_test(&pending->refs)) | |
50caf9c1 | 2685 | wake_up_var(&pending->refs); /* No UaF, just an address */ |
6d337eab | 2686 | |
c777d847 VS |
2687 | /* |
2688 | * Block the original owner of &pending until all subsequent callers | |
2689 | * have seen the completion and decremented the refcount | |
2690 | */ | |
6d337eab PZ |
2691 | wait_var_event(&my_pending.refs, !refcount_read(&my_pending.refs)); |
2692 | ||
50caf9c1 PZ |
2693 | /* ARGH */ |
2694 | WARN_ON_ONCE(my_pending.stop_pending); | |
2695 | ||
6d337eab PZ |
2696 | return 0; |
2697 | } | |
2698 | ||
5cc389bc PZ |
2699 | /* |
2700 | * Change a given task's CPU affinity. Migrate the thread to a | |
2701 | * proper CPU and schedule it away if the CPU it's executing on | |
2702 | * is removed from the allowed bitmask. | |
2703 | * | |
2704 | * NOTE: the caller must have a valid reference to the task, the | |
2705 | * task must not exit() & deallocate itself prematurely. The | |
2706 | * call is not atomic; no spinlocks may be held. | |
2707 | */ | |
25834c73 | 2708 | static int __set_cpus_allowed_ptr(struct task_struct *p, |
9cfc3e18 PZ |
2709 | const struct cpumask *new_mask, |
2710 | u32 flags) | |
5cc389bc | 2711 | { |
234a503e | 2712 | const struct cpumask *cpu_allowed_mask = task_cpu_possible_mask(p); |
e9d867a6 | 2713 | const struct cpumask *cpu_valid_mask = cpu_active_mask; |
234a503e | 2714 | bool kthread = p->flags & PF_KTHREAD; |
5cc389bc | 2715 | unsigned int dest_cpu; |
eb580751 PZ |
2716 | struct rq_flags rf; |
2717 | struct rq *rq; | |
5cc389bc PZ |
2718 | int ret = 0; |
2719 | ||
eb580751 | 2720 | rq = task_rq_lock(p, &rf); |
a499c3ea | 2721 | update_rq_clock(rq); |
5cc389bc | 2722 | |
234a503e | 2723 | if (kthread || is_migration_disabled(p)) { |
e9d867a6 | 2724 | /* |
741ba80f PZ |
2725 | * Kernel threads are allowed on online && !active CPUs, |
2726 | * however, during cpu-hot-unplug, even these might get pushed | |
2727 | * away if not KTHREAD_IS_PER_CPU. | |
af449901 PZ |
2728 | * |
2729 | * Specifically, migration_disabled() tasks must not fail the | |
2730 | * cpumask_any_and_distribute() pick below, esp. so on | |
2731 | * SCA_MIGRATE_ENABLE, otherwise we'll not call | |
2732 | * set_cpus_allowed_common() and actually reset p->cpus_ptr. | |
e9d867a6 PZI |
2733 | */ |
2734 | cpu_valid_mask = cpu_online_mask; | |
2735 | } | |
2736 | ||
234a503e WD |
2737 | if (!kthread && !cpumask_subset(new_mask, cpu_allowed_mask)) { |
2738 | ret = -EINVAL; | |
2739 | goto out; | |
2740 | } | |
2741 | ||
25834c73 PZ |
2742 | /* |
2743 | * Must re-check here, to close a race against __kthread_bind(), | |
2744 | * sched_setaffinity() is not guaranteed to observe the flag. | |
2745 | */ | |
9cfc3e18 | 2746 | if ((flags & SCA_CHECK) && (p->flags & PF_NO_SETAFFINITY)) { |
25834c73 PZ |
2747 | ret = -EINVAL; |
2748 | goto out; | |
2749 | } | |
2750 | ||
885b3ba4 VS |
2751 | if (!(flags & SCA_MIGRATE_ENABLE)) { |
2752 | if (cpumask_equal(&p->cpus_mask, new_mask)) | |
2753 | goto out; | |
2754 | ||
2755 | if (WARN_ON_ONCE(p == current && | |
2756 | is_migration_disabled(p) && | |
2757 | !cpumask_test_cpu(task_cpu(p), new_mask))) { | |
2758 | ret = -EBUSY; | |
2759 | goto out; | |
2760 | } | |
2761 | } | |
5cc389bc | 2762 | |
46a87b38 PT |
2763 | /* |
2764 | * Picking a ~random cpu helps in cases where we are changing affinity | |
2765 | * for groups of tasks (ie. cpuset), so that load balancing is not | |
2766 | * immediately required to distribute the tasks within their new mask. | |
2767 | */ | |
2768 | dest_cpu = cpumask_any_and_distribute(cpu_valid_mask, new_mask); | |
714e501e | 2769 | if (dest_cpu >= nr_cpu_ids) { |
5cc389bc PZ |
2770 | ret = -EINVAL; |
2771 | goto out; | |
2772 | } | |
2773 | ||
9cfc3e18 | 2774 | __do_set_cpus_allowed(p, new_mask, flags); |
5cc389bc | 2775 | |
6d337eab | 2776 | return affine_move_task(rq, p, &rf, dest_cpu, flags); |
5cc389bc | 2777 | |
5cc389bc | 2778 | out: |
eb580751 | 2779 | task_rq_unlock(rq, p, &rf); |
5cc389bc PZ |
2780 | |
2781 | return ret; | |
2782 | } | |
25834c73 PZ |
2783 | |
2784 | int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask) | |
2785 | { | |
9cfc3e18 | 2786 | return __set_cpus_allowed_ptr(p, new_mask, 0); |
25834c73 | 2787 | } |
5cc389bc PZ |
2788 | EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr); |
2789 | ||
dd41f596 | 2790 | void set_task_cpu(struct task_struct *p, unsigned int new_cpu) |
c65cc870 | 2791 | { |
e2912009 | 2792 | #ifdef CONFIG_SCHED_DEBUG |
2f064a59 PZ |
2793 | unsigned int state = READ_ONCE(p->__state); |
2794 | ||
e2912009 PZ |
2795 | /* |
2796 | * We should never call set_task_cpu() on a blocked task, | |
2797 | * ttwu() will sort out the placement. | |
2798 | */ | |
2f064a59 | 2799 | WARN_ON_ONCE(state != TASK_RUNNING && state != TASK_WAKING && !p->on_rq); |
0122ec5b | 2800 | |
3ea94de1 JP |
2801 | /* |
2802 | * Migrating fair class task must have p->on_rq = TASK_ON_RQ_MIGRATING, | |
2803 | * because schedstat_wait_{start,end} rebase migrating task's wait_start | |
2804 | * time relying on p->on_rq. | |
2805 | */ | |
2f064a59 | 2806 | WARN_ON_ONCE(state == TASK_RUNNING && |
3ea94de1 JP |
2807 | p->sched_class == &fair_sched_class && |
2808 | (p->on_rq && !task_on_rq_migrating(p))); | |
2809 | ||
0122ec5b | 2810 | #ifdef CONFIG_LOCKDEP |
6c6c54e1 PZ |
2811 | /* |
2812 | * The caller should hold either p->pi_lock or rq->lock, when changing | |
2813 | * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks. | |
2814 | * | |
2815 | * sched_move_task() holds both and thus holding either pins the cgroup, | |
8323f26c | 2816 | * see task_group(). |
6c6c54e1 PZ |
2817 | * |
2818 | * Furthermore, all task_rq users should acquire both locks, see | |
2819 | * task_rq_lock(). | |
2820 | */ | |
0122ec5b | 2821 | WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) || |
9ef7e7e3 | 2822 | lockdep_is_held(__rq_lockp(task_rq(p))))); |
0122ec5b | 2823 | #endif |
4ff9083b PZ |
2824 | /* |
2825 | * Clearly, migrating tasks to offline CPUs is a fairly daft thing. | |
2826 | */ | |
2827 | WARN_ON_ONCE(!cpu_online(new_cpu)); | |
af449901 PZ |
2828 | |
2829 | WARN_ON_ONCE(is_migration_disabled(p)); | |
e2912009 PZ |
2830 | #endif |
2831 | ||
de1d7286 | 2832 | trace_sched_migrate_task(p, new_cpu); |
cbc34ed1 | 2833 | |
0c69774e | 2834 | if (task_cpu(p) != new_cpu) { |
0a74bef8 | 2835 | if (p->sched_class->migrate_task_rq) |
1327237a | 2836 | p->sched_class->migrate_task_rq(p, new_cpu); |
0c69774e | 2837 | p->se.nr_migrations++; |
d7822b1e | 2838 | rseq_migrate(p); |
ff303e66 | 2839 | perf_event_task_migrate(p); |
0c69774e | 2840 | } |
dd41f596 IM |
2841 | |
2842 | __set_task_cpu(p, new_cpu); | |
c65cc870 IM |
2843 | } |
2844 | ||
0ad4e3df | 2845 | #ifdef CONFIG_NUMA_BALANCING |
ac66f547 PZ |
2846 | static void __migrate_swap_task(struct task_struct *p, int cpu) |
2847 | { | |
da0c1e65 | 2848 | if (task_on_rq_queued(p)) { |
ac66f547 | 2849 | struct rq *src_rq, *dst_rq; |
8a8c69c3 | 2850 | struct rq_flags srf, drf; |
ac66f547 PZ |
2851 | |
2852 | src_rq = task_rq(p); | |
2853 | dst_rq = cpu_rq(cpu); | |
2854 | ||
8a8c69c3 PZ |
2855 | rq_pin_lock(src_rq, &srf); |
2856 | rq_pin_lock(dst_rq, &drf); | |
2857 | ||
ac66f547 PZ |
2858 | deactivate_task(src_rq, p, 0); |
2859 | set_task_cpu(p, cpu); | |
2860 | activate_task(dst_rq, p, 0); | |
2861 | check_preempt_curr(dst_rq, p, 0); | |
8a8c69c3 PZ |
2862 | |
2863 | rq_unpin_lock(dst_rq, &drf); | |
2864 | rq_unpin_lock(src_rq, &srf); | |
2865 | ||
ac66f547 PZ |
2866 | } else { |
2867 | /* | |
2868 | * Task isn't running anymore; make it appear like we migrated | |
2869 | * it before it went to sleep. This means on wakeup we make the | |
d1ccc66d | 2870 | * previous CPU our target instead of where it really is. |
ac66f547 PZ |
2871 | */ |
2872 | p->wake_cpu = cpu; | |
2873 | } | |
2874 | } | |
2875 | ||
2876 | struct migration_swap_arg { | |
2877 | struct task_struct *src_task, *dst_task; | |
2878 | int src_cpu, dst_cpu; | |
2879 | }; | |
2880 | ||
2881 | static int migrate_swap_stop(void *data) | |
2882 | { | |
2883 | struct migration_swap_arg *arg = data; | |
2884 | struct rq *src_rq, *dst_rq; | |
2885 | int ret = -EAGAIN; | |
2886 | ||
62694cd5 PZ |
2887 | if (!cpu_active(arg->src_cpu) || !cpu_active(arg->dst_cpu)) |
2888 | return -EAGAIN; | |
2889 | ||
ac66f547 PZ |
2890 | src_rq = cpu_rq(arg->src_cpu); |
2891 | dst_rq = cpu_rq(arg->dst_cpu); | |
2892 | ||
74602315 PZ |
2893 | double_raw_lock(&arg->src_task->pi_lock, |
2894 | &arg->dst_task->pi_lock); | |
ac66f547 | 2895 | double_rq_lock(src_rq, dst_rq); |
62694cd5 | 2896 | |
ac66f547 PZ |
2897 | if (task_cpu(arg->dst_task) != arg->dst_cpu) |
2898 | goto unlock; | |
2899 | ||
2900 | if (task_cpu(arg->src_task) != arg->src_cpu) | |
2901 | goto unlock; | |
2902 | ||
3bd37062 | 2903 | if (!cpumask_test_cpu(arg->dst_cpu, arg->src_task->cpus_ptr)) |
ac66f547 PZ |
2904 | goto unlock; |
2905 | ||
3bd37062 | 2906 | if (!cpumask_test_cpu(arg->src_cpu, arg->dst_task->cpus_ptr)) |
ac66f547 PZ |
2907 | goto unlock; |
2908 | ||
2909 | __migrate_swap_task(arg->src_task, arg->dst_cpu); | |
2910 | __migrate_swap_task(arg->dst_task, arg->src_cpu); | |
2911 | ||
2912 | ret = 0; | |
2913 | ||
2914 | unlock: | |
2915 | double_rq_unlock(src_rq, dst_rq); | |
74602315 PZ |
2916 | raw_spin_unlock(&arg->dst_task->pi_lock); |
2917 | raw_spin_unlock(&arg->src_task->pi_lock); | |
ac66f547 PZ |
2918 | |
2919 | return ret; | |
2920 | } | |
2921 | ||
2922 | /* | |
2923 | * Cross migrate two tasks | |
2924 | */ | |
0ad4e3df SD |
2925 | int migrate_swap(struct task_struct *cur, struct task_struct *p, |
2926 | int target_cpu, int curr_cpu) | |
ac66f547 PZ |
2927 | { |
2928 | struct migration_swap_arg arg; | |
2929 | int ret = -EINVAL; | |
2930 | ||
ac66f547 PZ |
2931 | arg = (struct migration_swap_arg){ |
2932 | .src_task = cur, | |
0ad4e3df | 2933 | .src_cpu = curr_cpu, |
ac66f547 | 2934 | .dst_task = p, |
0ad4e3df | 2935 | .dst_cpu = target_cpu, |
ac66f547 PZ |
2936 | }; |
2937 | ||
2938 | if (arg.src_cpu == arg.dst_cpu) | |
2939 | goto out; | |
2940 | ||
6acce3ef PZ |
2941 | /* |
2942 | * These three tests are all lockless; this is OK since all of them | |
2943 | * will be re-checked with proper locks held further down the line. | |
2944 | */ | |
ac66f547 PZ |
2945 | if (!cpu_active(arg.src_cpu) || !cpu_active(arg.dst_cpu)) |
2946 | goto out; | |
2947 | ||
3bd37062 | 2948 | if (!cpumask_test_cpu(arg.dst_cpu, arg.src_task->cpus_ptr)) |
ac66f547 PZ |
2949 | goto out; |
2950 | ||
3bd37062 | 2951 | if (!cpumask_test_cpu(arg.src_cpu, arg.dst_task->cpus_ptr)) |
ac66f547 PZ |
2952 | goto out; |
2953 | ||
286549dc | 2954 | trace_sched_swap_numa(cur, arg.src_cpu, p, arg.dst_cpu); |
ac66f547 PZ |
2955 | ret = stop_two_cpus(arg.dst_cpu, arg.src_cpu, migrate_swap_stop, &arg); |
2956 | ||
2957 | out: | |
ac66f547 PZ |
2958 | return ret; |
2959 | } | |
0ad4e3df | 2960 | #endif /* CONFIG_NUMA_BALANCING */ |
ac66f547 | 2961 | |
1da177e4 LT |
2962 | /* |
2963 | * wait_task_inactive - wait for a thread to unschedule. | |
2964 | * | |
85ba2d86 RM |
2965 | * If @match_state is nonzero, it's the @p->state value just checked and |
2966 | * not expected to change. If it changes, i.e. @p might have woken up, | |
2967 | * then return zero. When we succeed in waiting for @p to be off its CPU, | |
2968 | * we return a positive number (its total switch count). If a second call | |
2969 | * a short while later returns the same number, the caller can be sure that | |
2970 | * @p has remained unscheduled the whole time. | |
2971 | * | |
1da177e4 LT |
2972 | * The caller must ensure that the task *will* unschedule sometime soon, |
2973 | * else this function might spin for a *long* time. This function can't | |
2974 | * be called with interrupts off, or it may introduce deadlock with | |
2975 | * smp_call_function() if an IPI is sent by the same process we are | |
2976 | * waiting to become inactive. | |
2977 | */ | |
2f064a59 | 2978 | unsigned long wait_task_inactive(struct task_struct *p, unsigned int match_state) |
1da177e4 | 2979 | { |
da0c1e65 | 2980 | int running, queued; |
eb580751 | 2981 | struct rq_flags rf; |
85ba2d86 | 2982 | unsigned long ncsw; |
70b97a7f | 2983 | struct rq *rq; |
1da177e4 | 2984 | |
3a5c359a AK |
2985 | for (;;) { |
2986 | /* | |
2987 | * We do the initial early heuristics without holding | |
2988 | * any task-queue locks at all. We'll only try to get | |
2989 | * the runqueue lock when things look like they will | |
2990 | * work out! | |
2991 | */ | |
2992 | rq = task_rq(p); | |
fa490cfd | 2993 | |
3a5c359a AK |
2994 | /* |
2995 | * If the task is actively running on another CPU | |
2996 | * still, just relax and busy-wait without holding | |
2997 | * any locks. | |
2998 | * | |
2999 | * NOTE! Since we don't hold any locks, it's not | |
3000 | * even sure that "rq" stays as the right runqueue! | |
3001 | * But we don't care, since "task_running()" will | |
3002 | * return false if the runqueue has changed and p | |
3003 | * is actually now running somewhere else! | |
3004 | */ | |
85ba2d86 | 3005 | while (task_running(rq, p)) { |
2f064a59 | 3006 | if (match_state && unlikely(READ_ONCE(p->__state) != match_state)) |
85ba2d86 | 3007 | return 0; |
3a5c359a | 3008 | cpu_relax(); |
85ba2d86 | 3009 | } |
fa490cfd | 3010 | |
3a5c359a AK |
3011 | /* |
3012 | * Ok, time to look more closely! We need the rq | |
3013 | * lock now, to be *sure*. If we're wrong, we'll | |
3014 | * just go back and repeat. | |
3015 | */ | |
eb580751 | 3016 | rq = task_rq_lock(p, &rf); |
27a9da65 | 3017 | trace_sched_wait_task(p); |
3a5c359a | 3018 | running = task_running(rq, p); |
da0c1e65 | 3019 | queued = task_on_rq_queued(p); |
85ba2d86 | 3020 | ncsw = 0; |
2f064a59 | 3021 | if (!match_state || READ_ONCE(p->__state) == match_state) |
93dcf55f | 3022 | ncsw = p->nvcsw | LONG_MIN; /* sets MSB */ |
eb580751 | 3023 | task_rq_unlock(rq, p, &rf); |
fa490cfd | 3024 | |
85ba2d86 RM |
3025 | /* |
3026 | * If it changed from the expected state, bail out now. | |
3027 | */ | |
3028 | if (unlikely(!ncsw)) | |
3029 | break; | |
3030 | ||
3a5c359a AK |
3031 | /* |
3032 | * Was it really running after all now that we | |
3033 | * checked with the proper locks actually held? | |
3034 | * | |
3035 | * Oops. Go back and try again.. | |
3036 | */ | |
3037 | if (unlikely(running)) { | |
3038 | cpu_relax(); | |
3039 | continue; | |
3040 | } | |
fa490cfd | 3041 | |
3a5c359a AK |
3042 | /* |
3043 | * It's not enough that it's not actively running, | |
3044 | * it must be off the runqueue _entirely_, and not | |
3045 | * preempted! | |
3046 | * | |
80dd99b3 | 3047 | * So if it was still runnable (but just not actively |
3a5c359a AK |
3048 | * running right now), it's preempted, and we should |
3049 | * yield - it could be a while. | |
3050 | */ | |
da0c1e65 | 3051 | if (unlikely(queued)) { |
8b0e1953 | 3052 | ktime_t to = NSEC_PER_SEC / HZ; |
8eb90c30 TG |
3053 | |
3054 | set_current_state(TASK_UNINTERRUPTIBLE); | |
3055 | schedule_hrtimeout(&to, HRTIMER_MODE_REL); | |
3a5c359a AK |
3056 | continue; |
3057 | } | |
fa490cfd | 3058 | |
3a5c359a AK |
3059 | /* |
3060 | * Ahh, all good. It wasn't running, and it wasn't | |
3061 | * runnable, which means that it will never become | |
3062 | * running in the future either. We're all done! | |
3063 | */ | |
3064 | break; | |
3065 | } | |
85ba2d86 RM |
3066 | |
3067 | return ncsw; | |
1da177e4 LT |
3068 | } |
3069 | ||
3070 | /*** | |
3071 | * kick_process - kick a running thread to enter/exit the kernel | |
3072 | * @p: the to-be-kicked thread | |
3073 | * | |
3074 | * Cause a process which is running on another CPU to enter | |
3075 | * kernel-mode, without any delay. (to get signals handled.) | |
3076 | * | |
25985edc | 3077 | * NOTE: this function doesn't have to take the runqueue lock, |
1da177e4 LT |
3078 | * because all it wants to ensure is that the remote task enters |
3079 | * the kernel. If the IPI races and the task has been migrated | |
3080 | * to another CPU then no harm is done and the purpose has been | |
3081 | * achieved as well. | |
3082 | */ | |
36c8b586 | 3083 | void kick_process(struct task_struct *p) |
1da177e4 LT |
3084 | { |
3085 | int cpu; | |
3086 | ||
3087 | preempt_disable(); | |
3088 | cpu = task_cpu(p); | |
3089 | if ((cpu != smp_processor_id()) && task_curr(p)) | |
3090 | smp_send_reschedule(cpu); | |
3091 | preempt_enable(); | |
3092 | } | |
b43e3521 | 3093 | EXPORT_SYMBOL_GPL(kick_process); |
1da177e4 | 3094 | |
30da688e | 3095 | /* |
3bd37062 | 3096 | * ->cpus_ptr is protected by both rq->lock and p->pi_lock |
e9d867a6 PZI |
3097 | * |
3098 | * A few notes on cpu_active vs cpu_online: | |
3099 | * | |
3100 | * - cpu_active must be a subset of cpu_online | |
3101 | * | |
97fb7a0a | 3102 | * - on CPU-up we allow per-CPU kthreads on the online && !active CPU, |
e9d867a6 | 3103 | * see __set_cpus_allowed_ptr(). At this point the newly online |
d1ccc66d | 3104 | * CPU isn't yet part of the sched domains, and balancing will not |
e9d867a6 PZI |
3105 | * see it. |
3106 | * | |
d1ccc66d | 3107 | * - on CPU-down we clear cpu_active() to mask the sched domains and |
e9d867a6 | 3108 | * avoid the load balancer to place new tasks on the to be removed |
d1ccc66d | 3109 | * CPU. Existing tasks will remain running there and will be taken |
e9d867a6 PZI |
3110 | * off. |
3111 | * | |
3112 | * This means that fallback selection must not select !active CPUs. | |
3113 | * And can assume that any active CPU must be online. Conversely | |
3114 | * select_task_rq() below may allow selection of !active CPUs in order | |
3115 | * to satisfy the above rules. | |
30da688e | 3116 | */ |
5da9a0fb PZ |
3117 | static int select_fallback_rq(int cpu, struct task_struct *p) |
3118 | { | |
aa00d89c TC |
3119 | int nid = cpu_to_node(cpu); |
3120 | const struct cpumask *nodemask = NULL; | |
2baab4e9 PZ |
3121 | enum { cpuset, possible, fail } state = cpuset; |
3122 | int dest_cpu; | |
5da9a0fb | 3123 | |
aa00d89c | 3124 | /* |
d1ccc66d IM |
3125 | * If the node that the CPU is on has been offlined, cpu_to_node() |
3126 | * will return -1. There is no CPU on the node, and we should | |
3127 | * select the CPU on the other node. | |
aa00d89c TC |
3128 | */ |
3129 | if (nid != -1) { | |
3130 | nodemask = cpumask_of_node(nid); | |
3131 | ||
3132 | /* Look for allowed, online CPU in same node. */ | |
3133 | for_each_cpu(dest_cpu, nodemask) { | |
9ae606bc | 3134 | if (is_cpu_allowed(p, dest_cpu)) |
aa00d89c TC |
3135 | return dest_cpu; |
3136 | } | |
2baab4e9 | 3137 | } |
5da9a0fb | 3138 | |
2baab4e9 PZ |
3139 | for (;;) { |
3140 | /* Any allowed, online CPU? */ | |
3bd37062 | 3141 | for_each_cpu(dest_cpu, p->cpus_ptr) { |
175f0e25 | 3142 | if (!is_cpu_allowed(p, dest_cpu)) |
2baab4e9 | 3143 | continue; |
175f0e25 | 3144 | |
2baab4e9 PZ |
3145 | goto out; |
3146 | } | |
5da9a0fb | 3147 | |
e73e85f0 | 3148 | /* No more Mr. Nice Guy. */ |
2baab4e9 PZ |
3149 | switch (state) { |
3150 | case cpuset: | |
97c0054d | 3151 | if (cpuset_cpus_allowed_fallback(p)) { |
e73e85f0 ON |
3152 | state = possible; |
3153 | break; | |
3154 | } | |
df561f66 | 3155 | fallthrough; |
2baab4e9 | 3156 | case possible: |
af449901 PZ |
3157 | /* |
3158 | * XXX When called from select_task_rq() we only | |
3159 | * hold p->pi_lock and again violate locking order. | |
3160 | * | |
3161 | * More yuck to audit. | |
3162 | */ | |
9ae606bc | 3163 | do_set_cpus_allowed(p, task_cpu_possible_mask(p)); |
2baab4e9 PZ |
3164 | state = fail; |
3165 | break; | |
2baab4e9 PZ |
3166 | case fail: |
3167 | BUG(); | |
3168 | break; | |
3169 | } | |
3170 | } | |
3171 | ||
3172 | out: | |
3173 | if (state != cpuset) { | |
3174 | /* | |
3175 | * Don't tell them about moving exiting tasks or | |
3176 | * kernel threads (both mm NULL), since they never | |
3177 | * leave kernel. | |
3178 | */ | |
3179 | if (p->mm && printk_ratelimit()) { | |
aac74dc4 | 3180 | printk_deferred("process %d (%s) no longer affine to cpu%d\n", |
2baab4e9 PZ |
3181 | task_pid_nr(p), p->comm, cpu); |
3182 | } | |
5da9a0fb PZ |
3183 | } |
3184 | ||
3185 | return dest_cpu; | |
3186 | } | |
3187 | ||
e2912009 | 3188 | /* |
3bd37062 | 3189 | * The caller (fork, wakeup) owns p->pi_lock, ->cpus_ptr is stable. |
e2912009 | 3190 | */ |
970b13ba | 3191 | static inline |
3aef1551 | 3192 | int select_task_rq(struct task_struct *p, int cpu, int wake_flags) |
970b13ba | 3193 | { |
cbce1a68 PZ |
3194 | lockdep_assert_held(&p->pi_lock); |
3195 | ||
af449901 | 3196 | if (p->nr_cpus_allowed > 1 && !is_migration_disabled(p)) |
3aef1551 | 3197 | cpu = p->sched_class->select_task_rq(p, cpu, wake_flags); |
e9d867a6 | 3198 | else |
3bd37062 | 3199 | cpu = cpumask_any(p->cpus_ptr); |
e2912009 PZ |
3200 | |
3201 | /* | |
3202 | * In order not to call set_task_cpu() on a blocking task we need | |
3bd37062 | 3203 | * to rely on ttwu() to place the task on a valid ->cpus_ptr |
d1ccc66d | 3204 | * CPU. |
e2912009 PZ |
3205 | * |
3206 | * Since this is common to all placement strategies, this lives here. | |
3207 | * | |
3208 | * [ this allows ->select_task() to simply return task_cpu(p) and | |
3209 | * not worry about this generic constraint ] | |
3210 | */ | |
7af443ee | 3211 | if (unlikely(!is_cpu_allowed(p, cpu))) |
5da9a0fb | 3212 | cpu = select_fallback_rq(task_cpu(p), p); |
e2912009 PZ |
3213 | |
3214 | return cpu; | |
970b13ba | 3215 | } |
09a40af5 | 3216 | |
f5832c19 NP |
3217 | void sched_set_stop_task(int cpu, struct task_struct *stop) |
3218 | { | |
ded467dc | 3219 | static struct lock_class_key stop_pi_lock; |
f5832c19 NP |
3220 | struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 }; |
3221 | struct task_struct *old_stop = cpu_rq(cpu)->stop; | |
3222 | ||
3223 | if (stop) { | |
3224 | /* | |
3225 | * Make it appear like a SCHED_FIFO task, its something | |
3226 | * userspace knows about and won't get confused about. | |
3227 | * | |
3228 | * Also, it will make PI more or less work without too | |
3229 | * much confusion -- but then, stop work should not | |
3230 | * rely on PI working anyway. | |
3231 | */ | |
3232 | sched_setscheduler_nocheck(stop, SCHED_FIFO, ¶m); | |
3233 | ||
3234 | stop->sched_class = &stop_sched_class; | |
ded467dc PZ |
3235 | |
3236 | /* | |
3237 | * The PI code calls rt_mutex_setprio() with ->pi_lock held to | |
3238 | * adjust the effective priority of a task. As a result, | |
3239 | * rt_mutex_setprio() can trigger (RT) balancing operations, | |
3240 | * which can then trigger wakeups of the stop thread to push | |
3241 | * around the current task. | |
3242 | * | |
3243 | * The stop task itself will never be part of the PI-chain, it | |
3244 | * never blocks, therefore that ->pi_lock recursion is safe. | |
3245 | * Tell lockdep about this by placing the stop->pi_lock in its | |
3246 | * own class. | |
3247 | */ | |
3248 | lockdep_set_class(&stop->pi_lock, &stop_pi_lock); | |
f5832c19 NP |
3249 | } |
3250 | ||
3251 | cpu_rq(cpu)->stop = stop; | |
3252 | ||
3253 | if (old_stop) { | |
3254 | /* | |
3255 | * Reset it back to a normal scheduling class so that | |
3256 | * it can die in pieces. | |
3257 | */ | |
3258 | old_stop->sched_class = &rt_sched_class; | |
3259 | } | |
3260 | } | |
3261 | ||
74d862b6 | 3262 | #else /* CONFIG_SMP */ |
25834c73 PZ |
3263 | |
3264 | static inline int __set_cpus_allowed_ptr(struct task_struct *p, | |
9cfc3e18 PZ |
3265 | const struct cpumask *new_mask, |
3266 | u32 flags) | |
25834c73 PZ |
3267 | { |
3268 | return set_cpus_allowed_ptr(p, new_mask); | |
3269 | } | |
3270 | ||
af449901 PZ |
3271 | static inline void migrate_disable_switch(struct rq *rq, struct task_struct *p) { } |
3272 | ||
3015ef4b TG |
3273 | static inline bool rq_has_pinned_tasks(struct rq *rq) |
3274 | { | |
3275 | return false; | |
3276 | } | |
3277 | ||
74d862b6 | 3278 | #endif /* !CONFIG_SMP */ |
970b13ba | 3279 | |
d7c01d27 | 3280 | static void |
b84cb5df | 3281 | ttwu_stat(struct task_struct *p, int cpu, int wake_flags) |
9ed3811a | 3282 | { |
4fa8d299 | 3283 | struct rq *rq; |
b84cb5df | 3284 | |
4fa8d299 JP |
3285 | if (!schedstat_enabled()) |
3286 | return; | |
3287 | ||
3288 | rq = this_rq(); | |
d7c01d27 | 3289 | |
4fa8d299 JP |
3290 | #ifdef CONFIG_SMP |
3291 | if (cpu == rq->cpu) { | |
b85c8b71 PZ |
3292 | __schedstat_inc(rq->ttwu_local); |
3293 | __schedstat_inc(p->se.statistics.nr_wakeups_local); | |
d7c01d27 PZ |
3294 | } else { |
3295 | struct sched_domain *sd; | |
3296 | ||
b85c8b71 | 3297 | __schedstat_inc(p->se.statistics.nr_wakeups_remote); |
057f3fad | 3298 | rcu_read_lock(); |
4fa8d299 | 3299 | for_each_domain(rq->cpu, sd) { |
d7c01d27 | 3300 | if (cpumask_test_cpu(cpu, sched_domain_span(sd))) { |
b85c8b71 | 3301 | __schedstat_inc(sd->ttwu_wake_remote); |
d7c01d27 PZ |
3302 | break; |
3303 | } | |
3304 | } | |
057f3fad | 3305 | rcu_read_unlock(); |
d7c01d27 | 3306 | } |
f339b9dc PZ |
3307 | |
3308 | if (wake_flags & WF_MIGRATED) | |
b85c8b71 | 3309 | __schedstat_inc(p->se.statistics.nr_wakeups_migrate); |
d7c01d27 PZ |
3310 | #endif /* CONFIG_SMP */ |
3311 | ||
b85c8b71 PZ |
3312 | __schedstat_inc(rq->ttwu_count); |
3313 | __schedstat_inc(p->se.statistics.nr_wakeups); | |
d7c01d27 PZ |
3314 | |
3315 | if (wake_flags & WF_SYNC) | |
b85c8b71 | 3316 | __schedstat_inc(p->se.statistics.nr_wakeups_sync); |
d7c01d27 PZ |
3317 | } |
3318 | ||
23f41eeb PZ |
3319 | /* |
3320 | * Mark the task runnable and perform wakeup-preemption. | |
3321 | */ | |
e7904a28 | 3322 | static void ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags, |
d8ac8971 | 3323 | struct rq_flags *rf) |
9ed3811a | 3324 | { |
9ed3811a | 3325 | check_preempt_curr(rq, p, wake_flags); |
2f064a59 | 3326 | WRITE_ONCE(p->__state, TASK_RUNNING); |
fbd705a0 PZ |
3327 | trace_sched_wakeup(p); |
3328 | ||
9ed3811a | 3329 | #ifdef CONFIG_SMP |
4c9a4bc8 PZ |
3330 | if (p->sched_class->task_woken) { |
3331 | /* | |
b19a888c | 3332 | * Our task @p is fully woken up and running; so it's safe to |
cbce1a68 | 3333 | * drop the rq->lock, hereafter rq is only used for statistics. |
4c9a4bc8 | 3334 | */ |
d8ac8971 | 3335 | rq_unpin_lock(rq, rf); |
9ed3811a | 3336 | p->sched_class->task_woken(rq, p); |
d8ac8971 | 3337 | rq_repin_lock(rq, rf); |
4c9a4bc8 | 3338 | } |
9ed3811a | 3339 | |
e69c6341 | 3340 | if (rq->idle_stamp) { |
78becc27 | 3341 | u64 delta = rq_clock(rq) - rq->idle_stamp; |
9bd721c5 | 3342 | u64 max = 2*rq->max_idle_balance_cost; |
9ed3811a | 3343 | |
abfafa54 JL |
3344 | update_avg(&rq->avg_idle, delta); |
3345 | ||
3346 | if (rq->avg_idle > max) | |
9ed3811a | 3347 | rq->avg_idle = max; |
abfafa54 | 3348 | |
94aafc3e PZ |
3349 | rq->wake_stamp = jiffies; |
3350 | rq->wake_avg_idle = rq->avg_idle / 2; | |
3351 | ||
9ed3811a TH |
3352 | rq->idle_stamp = 0; |
3353 | } | |
3354 | #endif | |
3355 | } | |
3356 | ||
c05fbafb | 3357 | static void |
e7904a28 | 3358 | ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags, |
d8ac8971 | 3359 | struct rq_flags *rf) |
c05fbafb | 3360 | { |
77558e4d | 3361 | int en_flags = ENQUEUE_WAKEUP | ENQUEUE_NOCLOCK; |
b5179ac7 | 3362 | |
5cb9eaa3 | 3363 | lockdep_assert_rq_held(rq); |
cbce1a68 | 3364 | |
c05fbafb PZ |
3365 | if (p->sched_contributes_to_load) |
3366 | rq->nr_uninterruptible--; | |
b5179ac7 | 3367 | |
dbfb089d | 3368 | #ifdef CONFIG_SMP |
b5179ac7 | 3369 | if (wake_flags & WF_MIGRATED) |
59efa0ba | 3370 | en_flags |= ENQUEUE_MIGRATED; |
ec618b84 | 3371 | else |
c05fbafb | 3372 | #endif |
ec618b84 PZ |
3373 | if (p->in_iowait) { |
3374 | delayacct_blkio_end(p); | |
3375 | atomic_dec(&task_rq(p)->nr_iowait); | |
3376 | } | |
c05fbafb | 3377 | |
1b174a2c | 3378 | activate_task(rq, p, en_flags); |
d8ac8971 | 3379 | ttwu_do_wakeup(rq, p, wake_flags, rf); |
c05fbafb PZ |
3380 | } |
3381 | ||
3382 | /* | |
58877d34 PZ |
3383 | * Consider @p being inside a wait loop: |
3384 | * | |
3385 | * for (;;) { | |
3386 | * set_current_state(TASK_UNINTERRUPTIBLE); | |
3387 | * | |
3388 | * if (CONDITION) | |
3389 | * break; | |
3390 | * | |
3391 | * schedule(); | |
3392 | * } | |
3393 | * __set_current_state(TASK_RUNNING); | |
3394 | * | |
3395 | * between set_current_state() and schedule(). In this case @p is still | |
3396 | * runnable, so all that needs doing is change p->state back to TASK_RUNNING in | |
3397 | * an atomic manner. | |
3398 | * | |
3399 | * By taking task_rq(p)->lock we serialize against schedule(), if @p->on_rq | |
3400 | * then schedule() must still happen and p->state can be changed to | |
3401 | * TASK_RUNNING. Otherwise we lost the race, schedule() has happened, and we | |
3402 | * need to do a full wakeup with enqueue. | |
3403 | * | |
3404 | * Returns: %true when the wakeup is done, | |
3405 | * %false otherwise. | |
c05fbafb | 3406 | */ |
58877d34 | 3407 | static int ttwu_runnable(struct task_struct *p, int wake_flags) |
c05fbafb | 3408 | { |
eb580751 | 3409 | struct rq_flags rf; |
c05fbafb PZ |
3410 | struct rq *rq; |
3411 | int ret = 0; | |
3412 | ||
eb580751 | 3413 | rq = __task_rq_lock(p, &rf); |
da0c1e65 | 3414 | if (task_on_rq_queued(p)) { |
1ad4ec0d FW |
3415 | /* check_preempt_curr() may use rq clock */ |
3416 | update_rq_clock(rq); | |
d8ac8971 | 3417 | ttwu_do_wakeup(rq, p, wake_flags, &rf); |
c05fbafb PZ |
3418 | ret = 1; |
3419 | } | |
eb580751 | 3420 | __task_rq_unlock(rq, &rf); |
c05fbafb PZ |
3421 | |
3422 | return ret; | |
3423 | } | |
3424 | ||
317f3941 | 3425 | #ifdef CONFIG_SMP |
a1488664 | 3426 | void sched_ttwu_pending(void *arg) |
317f3941 | 3427 | { |
a1488664 | 3428 | struct llist_node *llist = arg; |
317f3941 | 3429 | struct rq *rq = this_rq(); |
73215849 | 3430 | struct task_struct *p, *t; |
d8ac8971 | 3431 | struct rq_flags rf; |
317f3941 | 3432 | |
e3baac47 PZ |
3433 | if (!llist) |
3434 | return; | |
3435 | ||
126c2092 PZ |
3436 | /* |
3437 | * rq::ttwu_pending racy indication of out-standing wakeups. | |
3438 | * Races such that false-negatives are possible, since they | |
3439 | * are shorter lived that false-positives would be. | |
3440 | */ | |
3441 | WRITE_ONCE(rq->ttwu_pending, 0); | |
3442 | ||
8a8c69c3 | 3443 | rq_lock_irqsave(rq, &rf); |
77558e4d | 3444 | update_rq_clock(rq); |
317f3941 | 3445 | |
8c4890d1 | 3446 | llist_for_each_entry_safe(p, t, llist, wake_entry.llist) { |
b6e13e85 PZ |
3447 | if (WARN_ON_ONCE(p->on_cpu)) |
3448 | smp_cond_load_acquire(&p->on_cpu, !VAL); | |
3449 | ||
3450 | if (WARN_ON_ONCE(task_cpu(p) != cpu_of(rq))) | |
3451 | set_task_cpu(p, cpu_of(rq)); | |
3452 | ||
73215849 | 3453 | ttwu_do_activate(rq, p, p->sched_remote_wakeup ? WF_MIGRATED : 0, &rf); |
b6e13e85 | 3454 | } |
317f3941 | 3455 | |
8a8c69c3 | 3456 | rq_unlock_irqrestore(rq, &rf); |
317f3941 PZ |
3457 | } |
3458 | ||
b2a02fc4 | 3459 | void send_call_function_single_ipi(int cpu) |
317f3941 | 3460 | { |
b2a02fc4 | 3461 | struct rq *rq = cpu_rq(cpu); |
ca38062e | 3462 | |
b2a02fc4 PZ |
3463 | if (!set_nr_if_polling(rq->idle)) |
3464 | arch_send_call_function_single_ipi(cpu); | |
3465 | else | |
3466 | trace_sched_wake_idle_without_ipi(cpu); | |
317f3941 PZ |
3467 | } |
3468 | ||
2ebb1771 MG |
3469 | /* |
3470 | * Queue a task on the target CPUs wake_list and wake the CPU via IPI if | |
3471 | * necessary. The wakee CPU on receipt of the IPI will queue the task | |
3472 | * via sched_ttwu_wakeup() for activation so the wakee incurs the cost | |
3473 | * of the wakeup instead of the waker. | |
3474 | */ | |
3475 | static void __ttwu_queue_wakelist(struct task_struct *p, int cpu, int wake_flags) | |
317f3941 | 3476 | { |
e3baac47 PZ |
3477 | struct rq *rq = cpu_rq(cpu); |
3478 | ||
b7e7ade3 PZ |
3479 | p->sched_remote_wakeup = !!(wake_flags & WF_MIGRATED); |
3480 | ||
126c2092 | 3481 | WRITE_ONCE(rq->ttwu_pending, 1); |
8c4890d1 | 3482 | __smp_call_single_queue(cpu, &p->wake_entry.llist); |
317f3941 | 3483 | } |
d6aa8f85 | 3484 | |
f6be8af1 CL |
3485 | void wake_up_if_idle(int cpu) |
3486 | { | |
3487 | struct rq *rq = cpu_rq(cpu); | |
8a8c69c3 | 3488 | struct rq_flags rf; |
f6be8af1 | 3489 | |
fd7de1e8 AL |
3490 | rcu_read_lock(); |
3491 | ||
3492 | if (!is_idle_task(rcu_dereference(rq->curr))) | |
3493 | goto out; | |
f6be8af1 CL |
3494 | |
3495 | if (set_nr_if_polling(rq->idle)) { | |
3496 | trace_sched_wake_idle_without_ipi(cpu); | |
3497 | } else { | |
8a8c69c3 | 3498 | rq_lock_irqsave(rq, &rf); |
f6be8af1 CL |
3499 | if (is_idle_task(rq->curr)) |
3500 | smp_send_reschedule(cpu); | |
d1ccc66d | 3501 | /* Else CPU is not idle, do nothing here: */ |
8a8c69c3 | 3502 | rq_unlock_irqrestore(rq, &rf); |
f6be8af1 | 3503 | } |
fd7de1e8 AL |
3504 | |
3505 | out: | |
3506 | rcu_read_unlock(); | |
f6be8af1 CL |
3507 | } |
3508 | ||
39be3501 | 3509 | bool cpus_share_cache(int this_cpu, int that_cpu) |
518cd623 PZ |
3510 | { |
3511 | return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu); | |
3512 | } | |
c6e7bd7a | 3513 | |
2ebb1771 MG |
3514 | static inline bool ttwu_queue_cond(int cpu, int wake_flags) |
3515 | { | |
5ba2ffba PZ |
3516 | /* |
3517 | * Do not complicate things with the async wake_list while the CPU is | |
3518 | * in hotplug state. | |
3519 | */ | |
3520 | if (!cpu_active(cpu)) | |
3521 | return false; | |
3522 | ||
2ebb1771 MG |
3523 | /* |
3524 | * If the CPU does not share cache, then queue the task on the | |
3525 | * remote rqs wakelist to avoid accessing remote data. | |
3526 | */ | |
3527 | if (!cpus_share_cache(smp_processor_id(), cpu)) | |
3528 | return true; | |
3529 | ||
3530 | /* | |
3531 | * If the task is descheduling and the only running task on the | |
3532 | * CPU then use the wakelist to offload the task activation to | |
3533 | * the soon-to-be-idle CPU as the current CPU is likely busy. | |
3534 | * nr_running is checked to avoid unnecessary task stacking. | |
3535 | */ | |
739f70b4 | 3536 | if ((wake_flags & WF_ON_CPU) && cpu_rq(cpu)->nr_running <= 1) |
2ebb1771 MG |
3537 | return true; |
3538 | ||
3539 | return false; | |
3540 | } | |
3541 | ||
3542 | static bool ttwu_queue_wakelist(struct task_struct *p, int cpu, int wake_flags) | |
c6e7bd7a | 3543 | { |
2ebb1771 | 3544 | if (sched_feat(TTWU_QUEUE) && ttwu_queue_cond(cpu, wake_flags)) { |
b6e13e85 PZ |
3545 | if (WARN_ON_ONCE(cpu == smp_processor_id())) |
3546 | return false; | |
3547 | ||
c6e7bd7a | 3548 | sched_clock_cpu(cpu); /* Sync clocks across CPUs */ |
2ebb1771 | 3549 | __ttwu_queue_wakelist(p, cpu, wake_flags); |
c6e7bd7a PZ |
3550 | return true; |
3551 | } | |
3552 | ||
3553 | return false; | |
3554 | } | |
58877d34 PZ |
3555 | |
3556 | #else /* !CONFIG_SMP */ | |
3557 | ||
3558 | static inline bool ttwu_queue_wakelist(struct task_struct *p, int cpu, int wake_flags) | |
3559 | { | |
3560 | return false; | |
3561 | } | |
3562 | ||
d6aa8f85 | 3563 | #endif /* CONFIG_SMP */ |
317f3941 | 3564 | |
b5179ac7 | 3565 | static void ttwu_queue(struct task_struct *p, int cpu, int wake_flags) |
c05fbafb PZ |
3566 | { |
3567 | struct rq *rq = cpu_rq(cpu); | |
d8ac8971 | 3568 | struct rq_flags rf; |
c05fbafb | 3569 | |
2ebb1771 | 3570 | if (ttwu_queue_wakelist(p, cpu, wake_flags)) |
317f3941 | 3571 | return; |
317f3941 | 3572 | |
8a8c69c3 | 3573 | rq_lock(rq, &rf); |
77558e4d | 3574 | update_rq_clock(rq); |
d8ac8971 | 3575 | ttwu_do_activate(rq, p, wake_flags, &rf); |
8a8c69c3 | 3576 | rq_unlock(rq, &rf); |
9ed3811a TH |
3577 | } |
3578 | ||
8643cda5 PZ |
3579 | /* |
3580 | * Notes on Program-Order guarantees on SMP systems. | |
3581 | * | |
3582 | * MIGRATION | |
3583 | * | |
3584 | * The basic program-order guarantee on SMP systems is that when a task [t] | |
d1ccc66d IM |
3585 | * migrates, all its activity on its old CPU [c0] happens-before any subsequent |
3586 | * execution on its new CPU [c1]. | |
8643cda5 PZ |
3587 | * |
3588 | * For migration (of runnable tasks) this is provided by the following means: | |
3589 | * | |
3590 | * A) UNLOCK of the rq(c0)->lock scheduling out task t | |
3591 | * B) migration for t is required to synchronize *both* rq(c0)->lock and | |
3592 | * rq(c1)->lock (if not at the same time, then in that order). | |
3593 | * C) LOCK of the rq(c1)->lock scheduling in task | |
3594 | * | |
7696f991 | 3595 | * Release/acquire chaining guarantees that B happens after A and C after B. |
d1ccc66d | 3596 | * Note: the CPU doing B need not be c0 or c1 |
8643cda5 PZ |
3597 | * |
3598 | * Example: | |
3599 | * | |
3600 | * CPU0 CPU1 CPU2 | |
3601 | * | |
3602 | * LOCK rq(0)->lock | |
3603 | * sched-out X | |
3604 | * sched-in Y | |
3605 | * UNLOCK rq(0)->lock | |
3606 | * | |
3607 | * LOCK rq(0)->lock // orders against CPU0 | |
3608 | * dequeue X | |
3609 | * UNLOCK rq(0)->lock | |
3610 | * | |
3611 | * LOCK rq(1)->lock | |
3612 | * enqueue X | |
3613 | * UNLOCK rq(1)->lock | |
3614 | * | |
3615 | * LOCK rq(1)->lock // orders against CPU2 | |
3616 | * sched-out Z | |
3617 | * sched-in X | |
3618 | * UNLOCK rq(1)->lock | |
3619 | * | |
3620 | * | |
3621 | * BLOCKING -- aka. SLEEP + WAKEUP | |
3622 | * | |
3623 | * For blocking we (obviously) need to provide the same guarantee as for | |
3624 | * migration. However the means are completely different as there is no lock | |
3625 | * chain to provide order. Instead we do: | |
3626 | * | |
58877d34 PZ |
3627 | * 1) smp_store_release(X->on_cpu, 0) -- finish_task() |
3628 | * 2) smp_cond_load_acquire(!X->on_cpu) -- try_to_wake_up() | |
8643cda5 PZ |
3629 | * |
3630 | * Example: | |
3631 | * | |
3632 | * CPU0 (schedule) CPU1 (try_to_wake_up) CPU2 (schedule) | |
3633 | * | |
3634 | * LOCK rq(0)->lock LOCK X->pi_lock | |
3635 | * dequeue X | |
3636 | * sched-out X | |
3637 | * smp_store_release(X->on_cpu, 0); | |
3638 | * | |
1f03e8d2 | 3639 | * smp_cond_load_acquire(&X->on_cpu, !VAL); |
8643cda5 PZ |
3640 | * X->state = WAKING |
3641 | * set_task_cpu(X,2) | |
3642 | * | |
3643 | * LOCK rq(2)->lock | |
3644 | * enqueue X | |
3645 | * X->state = RUNNING | |
3646 | * UNLOCK rq(2)->lock | |
3647 | * | |
3648 | * LOCK rq(2)->lock // orders against CPU1 | |
3649 | * sched-out Z | |
3650 | * sched-in X | |
3651 | * UNLOCK rq(2)->lock | |
3652 | * | |
3653 | * UNLOCK X->pi_lock | |
3654 | * UNLOCK rq(0)->lock | |
3655 | * | |
3656 | * | |
7696f991 AP |
3657 | * However, for wakeups there is a second guarantee we must provide, namely we |
3658 | * must ensure that CONDITION=1 done by the caller can not be reordered with | |
3659 | * accesses to the task state; see try_to_wake_up() and set_current_state(). | |
8643cda5 PZ |
3660 | */ |
3661 | ||
9ed3811a | 3662 | /** |
1da177e4 | 3663 | * try_to_wake_up - wake up a thread |
9ed3811a | 3664 | * @p: the thread to be awakened |
1da177e4 | 3665 | * @state: the mask of task states that can be woken |
9ed3811a | 3666 | * @wake_flags: wake modifier flags (WF_*) |
1da177e4 | 3667 | * |
58877d34 PZ |
3668 | * Conceptually does: |
3669 | * | |
3670 | * If (@state & @p->state) @p->state = TASK_RUNNING. | |
1da177e4 | 3671 | * |
a2250238 PZ |
3672 | * If the task was not queued/runnable, also place it back on a runqueue. |
3673 | * | |
58877d34 PZ |
3674 | * This function is atomic against schedule() which would dequeue the task. |
3675 | * | |
3676 | * It issues a full memory barrier before accessing @p->state, see the comment | |
3677 | * with set_current_state(). | |
a2250238 | 3678 | * |
58877d34 | 3679 | * Uses p->pi_lock to serialize against concurrent wake-ups. |
a2250238 | 3680 | * |
58877d34 PZ |
3681 | * Relies on p->pi_lock stabilizing: |
3682 | * - p->sched_class | |
3683 | * - p->cpus_ptr | |
3684 | * - p->sched_task_group | |
3685 | * in order to do migration, see its use of select_task_rq()/set_task_cpu(). | |
3686 | * | |
3687 | * Tries really hard to only take one task_rq(p)->lock for performance. | |
3688 | * Takes rq->lock in: | |
3689 | * - ttwu_runnable() -- old rq, unavoidable, see comment there; | |
3690 | * - ttwu_queue() -- new rq, for enqueue of the task; | |
3691 | * - psi_ttwu_dequeue() -- much sadness :-( accounting will kill us. | |
3692 | * | |
3693 | * As a consequence we race really badly with just about everything. See the | |
3694 | * many memory barriers and their comments for details. | |
7696f991 | 3695 | * |
a2250238 PZ |
3696 | * Return: %true if @p->state changes (an actual wakeup was done), |
3697 | * %false otherwise. | |
1da177e4 | 3698 | */ |
e4a52bcb PZ |
3699 | static int |
3700 | try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags) | |
1da177e4 | 3701 | { |
1da177e4 | 3702 | unsigned long flags; |
c05fbafb | 3703 | int cpu, success = 0; |
2398f2c6 | 3704 | |
e3d85487 | 3705 | preempt_disable(); |
aacedf26 PZ |
3706 | if (p == current) { |
3707 | /* | |
3708 | * We're waking current, this means 'p->on_rq' and 'task_cpu(p) | |
3709 | * == smp_processor_id()'. Together this means we can special | |
58877d34 | 3710 | * case the whole 'p->on_rq && ttwu_runnable()' case below |
aacedf26 PZ |
3711 | * without taking any locks. |
3712 | * | |
3713 | * In particular: | |
3714 | * - we rely on Program-Order guarantees for all the ordering, | |
3715 | * - we're serialized against set_special_state() by virtue of | |
3716 | * it disabling IRQs (this allows not taking ->pi_lock). | |
3717 | */ | |
2f064a59 | 3718 | if (!(READ_ONCE(p->__state) & state)) |
e3d85487 | 3719 | goto out; |
aacedf26 PZ |
3720 | |
3721 | success = 1; | |
aacedf26 | 3722 | trace_sched_waking(p); |
2f064a59 | 3723 | WRITE_ONCE(p->__state, TASK_RUNNING); |
aacedf26 PZ |
3724 | trace_sched_wakeup(p); |
3725 | goto out; | |
3726 | } | |
3727 | ||
e0acd0a6 ON |
3728 | /* |
3729 | * If we are going to wake up a thread waiting for CONDITION we | |
3730 | * need to ensure that CONDITION=1 done by the caller can not be | |
58877d34 PZ |
3731 | * reordered with p->state check below. This pairs with smp_store_mb() |
3732 | * in set_current_state() that the waiting thread does. | |
e0acd0a6 | 3733 | */ |
013fdb80 | 3734 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
d89e588c | 3735 | smp_mb__after_spinlock(); |
2f064a59 | 3736 | if (!(READ_ONCE(p->__state) & state)) |
aacedf26 | 3737 | goto unlock; |
1da177e4 | 3738 | |
fbd705a0 PZ |
3739 | trace_sched_waking(p); |
3740 | ||
d1ccc66d IM |
3741 | /* We're going to change ->state: */ |
3742 | success = 1; | |
1da177e4 | 3743 | |
135e8c92 BS |
3744 | /* |
3745 | * Ensure we load p->on_rq _after_ p->state, otherwise it would | |
3746 | * be possible to, falsely, observe p->on_rq == 0 and get stuck | |
3747 | * in smp_cond_load_acquire() below. | |
3748 | * | |
3d85b270 AP |
3749 | * sched_ttwu_pending() try_to_wake_up() |
3750 | * STORE p->on_rq = 1 LOAD p->state | |
3751 | * UNLOCK rq->lock | |
3752 | * | |
3753 | * __schedule() (switch to task 'p') | |
3754 | * LOCK rq->lock smp_rmb(); | |
3755 | * smp_mb__after_spinlock(); | |
3756 | * UNLOCK rq->lock | |
135e8c92 BS |
3757 | * |
3758 | * [task p] | |
3d85b270 | 3759 | * STORE p->state = UNINTERRUPTIBLE LOAD p->on_rq |
135e8c92 | 3760 | * |
3d85b270 AP |
3761 | * Pairs with the LOCK+smp_mb__after_spinlock() on rq->lock in |
3762 | * __schedule(). See the comment for smp_mb__after_spinlock(). | |
2beaf328 PM |
3763 | * |
3764 | * A similar smb_rmb() lives in try_invoke_on_locked_down_task(). | |
135e8c92 BS |
3765 | */ |
3766 | smp_rmb(); | |
58877d34 | 3767 | if (READ_ONCE(p->on_rq) && ttwu_runnable(p, wake_flags)) |
aacedf26 | 3768 | goto unlock; |
1da177e4 | 3769 | |
1da177e4 | 3770 | #ifdef CONFIG_SMP |
ecf7d01c PZ |
3771 | /* |
3772 | * Ensure we load p->on_cpu _after_ p->on_rq, otherwise it would be | |
3773 | * possible to, falsely, observe p->on_cpu == 0. | |
3774 | * | |
3775 | * One must be running (->on_cpu == 1) in order to remove oneself | |
3776 | * from the runqueue. | |
3777 | * | |
3d85b270 AP |
3778 | * __schedule() (switch to task 'p') try_to_wake_up() |
3779 | * STORE p->on_cpu = 1 LOAD p->on_rq | |
3780 | * UNLOCK rq->lock | |
3781 | * | |
3782 | * __schedule() (put 'p' to sleep) | |
3783 | * LOCK rq->lock smp_rmb(); | |
3784 | * smp_mb__after_spinlock(); | |
3785 | * STORE p->on_rq = 0 LOAD p->on_cpu | |
ecf7d01c | 3786 | * |
3d85b270 AP |
3787 | * Pairs with the LOCK+smp_mb__after_spinlock() on rq->lock in |
3788 | * __schedule(). See the comment for smp_mb__after_spinlock(). | |
dbfb089d PZ |
3789 | * |
3790 | * Form a control-dep-acquire with p->on_rq == 0 above, to ensure | |
3791 | * schedule()'s deactivate_task() has 'happened' and p will no longer | |
3792 | * care about it's own p->state. See the comment in __schedule(). | |
ecf7d01c | 3793 | */ |
dbfb089d PZ |
3794 | smp_acquire__after_ctrl_dep(); |
3795 | ||
3796 | /* | |
3797 | * We're doing the wakeup (@success == 1), they did a dequeue (p->on_rq | |
3798 | * == 0), which means we need to do an enqueue, change p->state to | |
3799 | * TASK_WAKING such that we can unlock p->pi_lock before doing the | |
3800 | * enqueue, such as ttwu_queue_wakelist(). | |
3801 | */ | |
2f064a59 | 3802 | WRITE_ONCE(p->__state, TASK_WAKING); |
ecf7d01c | 3803 | |
c6e7bd7a PZ |
3804 | /* |
3805 | * If the owning (remote) CPU is still in the middle of schedule() with | |
3806 | * this task as prev, considering queueing p on the remote CPUs wake_list | |
3807 | * which potentially sends an IPI instead of spinning on p->on_cpu to | |
3808 | * let the waker make forward progress. This is safe because IRQs are | |
3809 | * disabled and the IPI will deliver after on_cpu is cleared. | |
b6e13e85 PZ |
3810 | * |
3811 | * Ensure we load task_cpu(p) after p->on_cpu: | |
3812 | * | |
3813 | * set_task_cpu(p, cpu); | |
3814 | * STORE p->cpu = @cpu | |
3815 | * __schedule() (switch to task 'p') | |
3816 | * LOCK rq->lock | |
3817 | * smp_mb__after_spin_lock() smp_cond_load_acquire(&p->on_cpu) | |
3818 | * STORE p->on_cpu = 1 LOAD p->cpu | |
3819 | * | |
3820 | * to ensure we observe the correct CPU on which the task is currently | |
3821 | * scheduling. | |
c6e7bd7a | 3822 | */ |
b6e13e85 | 3823 | if (smp_load_acquire(&p->on_cpu) && |
739f70b4 | 3824 | ttwu_queue_wakelist(p, task_cpu(p), wake_flags | WF_ON_CPU)) |
c6e7bd7a PZ |
3825 | goto unlock; |
3826 | ||
e9c84311 | 3827 | /* |
d1ccc66d | 3828 | * If the owning (remote) CPU is still in the middle of schedule() with |
b19a888c | 3829 | * this task as prev, wait until it's done referencing the task. |
b75a2253 | 3830 | * |
31cb1bc0 | 3831 | * Pairs with the smp_store_release() in finish_task(). |
b75a2253 PZ |
3832 | * |
3833 | * This ensures that tasks getting woken will be fully ordered against | |
3834 | * their previous state and preserve Program Order. | |
0970d299 | 3835 | */ |
1f03e8d2 | 3836 | smp_cond_load_acquire(&p->on_cpu, !VAL); |
1da177e4 | 3837 | |
3aef1551 | 3838 | cpu = select_task_rq(p, p->wake_cpu, wake_flags | WF_TTWU); |
f339b9dc | 3839 | if (task_cpu(p) != cpu) { |
ec618b84 PZ |
3840 | if (p->in_iowait) { |
3841 | delayacct_blkio_end(p); | |
3842 | atomic_dec(&task_rq(p)->nr_iowait); | |
3843 | } | |
3844 | ||
f339b9dc | 3845 | wake_flags |= WF_MIGRATED; |
eb414681 | 3846 | psi_ttwu_dequeue(p); |
e4a52bcb | 3847 | set_task_cpu(p, cpu); |
f339b9dc | 3848 | } |
b6e13e85 PZ |
3849 | #else |
3850 | cpu = task_cpu(p); | |
1da177e4 | 3851 | #endif /* CONFIG_SMP */ |
1da177e4 | 3852 | |
b5179ac7 | 3853 | ttwu_queue(p, cpu, wake_flags); |
aacedf26 | 3854 | unlock: |
013fdb80 | 3855 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
aacedf26 PZ |
3856 | out: |
3857 | if (success) | |
b6e13e85 | 3858 | ttwu_stat(p, task_cpu(p), wake_flags); |
e3d85487 | 3859 | preempt_enable(); |
1da177e4 LT |
3860 | |
3861 | return success; | |
3862 | } | |
3863 | ||
2beaf328 PM |
3864 | /** |
3865 | * try_invoke_on_locked_down_task - Invoke a function on task in fixed state | |
1b7af295 | 3866 | * @p: Process for which the function is to be invoked, can be @current. |
2beaf328 PM |
3867 | * @func: Function to invoke. |
3868 | * @arg: Argument to function. | |
3869 | * | |
3870 | * If the specified task can be quickly locked into a definite state | |
3871 | * (either sleeping or on a given runqueue), arrange to keep it in that | |
3872 | * state while invoking @func(@arg). This function can use ->on_rq and | |
3873 | * task_curr() to work out what the state is, if required. Given that | |
3874 | * @func can be invoked with a runqueue lock held, it had better be quite | |
3875 | * lightweight. | |
3876 | * | |
3877 | * Returns: | |
3878 | * @false if the task slipped out from under the locks. | |
3879 | * @true if the task was locked onto a runqueue or is sleeping. | |
3880 | * However, @func can override this by returning @false. | |
3881 | */ | |
3882 | bool try_invoke_on_locked_down_task(struct task_struct *p, bool (*func)(struct task_struct *t, void *arg), void *arg) | |
3883 | { | |
2beaf328 | 3884 | struct rq_flags rf; |
1b7af295 | 3885 | bool ret = false; |
2beaf328 PM |
3886 | struct rq *rq; |
3887 | ||
1b7af295 | 3888 | raw_spin_lock_irqsave(&p->pi_lock, rf.flags); |
2beaf328 PM |
3889 | if (p->on_rq) { |
3890 | rq = __task_rq_lock(p, &rf); | |
3891 | if (task_rq(p) == rq) | |
3892 | ret = func(p, arg); | |
3893 | rq_unlock(rq, &rf); | |
3894 | } else { | |
2f064a59 | 3895 | switch (READ_ONCE(p->__state)) { |
2beaf328 PM |
3896 | case TASK_RUNNING: |
3897 | case TASK_WAKING: | |
3898 | break; | |
3899 | default: | |
3900 | smp_rmb(); // See smp_rmb() comment in try_to_wake_up(). | |
3901 | if (!p->on_rq) | |
3902 | ret = func(p, arg); | |
3903 | } | |
3904 | } | |
1b7af295 | 3905 | raw_spin_unlock_irqrestore(&p->pi_lock, rf.flags); |
2beaf328 PM |
3906 | return ret; |
3907 | } | |
3908 | ||
50fa610a DH |
3909 | /** |
3910 | * wake_up_process - Wake up a specific process | |
3911 | * @p: The process to be woken up. | |
3912 | * | |
3913 | * Attempt to wake up the nominated process and move it to the set of runnable | |
e69f6186 YB |
3914 | * processes. |
3915 | * | |
3916 | * Return: 1 if the process was woken up, 0 if it was already running. | |
50fa610a | 3917 | * |
7696f991 | 3918 | * This function executes a full memory barrier before accessing the task state. |
50fa610a | 3919 | */ |
7ad5b3a5 | 3920 | int wake_up_process(struct task_struct *p) |
1da177e4 | 3921 | { |
9067ac85 | 3922 | return try_to_wake_up(p, TASK_NORMAL, 0); |
1da177e4 | 3923 | } |
1da177e4 LT |
3924 | EXPORT_SYMBOL(wake_up_process); |
3925 | ||
7ad5b3a5 | 3926 | int wake_up_state(struct task_struct *p, unsigned int state) |
1da177e4 LT |
3927 | { |
3928 | return try_to_wake_up(p, state, 0); | |
3929 | } | |
3930 | ||
1da177e4 LT |
3931 | /* |
3932 | * Perform scheduler related setup for a newly forked process p. | |
3933 | * p is forked by current. | |
dd41f596 IM |
3934 | * |
3935 | * __sched_fork() is basic setup used by init_idle() too: | |
3936 | */ | |
5e1576ed | 3937 | static void __sched_fork(unsigned long clone_flags, struct task_struct *p) |
dd41f596 | 3938 | { |
fd2f4419 PZ |
3939 | p->on_rq = 0; |
3940 | ||
3941 | p->se.on_rq = 0; | |
dd41f596 IM |
3942 | p->se.exec_start = 0; |
3943 | p->se.sum_exec_runtime = 0; | |
f6cf891c | 3944 | p->se.prev_sum_exec_runtime = 0; |
6c594c21 | 3945 | p->se.nr_migrations = 0; |
da7a735e | 3946 | p->se.vruntime = 0; |
fd2f4419 | 3947 | INIT_LIST_HEAD(&p->se.group_node); |
6cfb0d5d | 3948 | |
ad936d86 BP |
3949 | #ifdef CONFIG_FAIR_GROUP_SCHED |
3950 | p->se.cfs_rq = NULL; | |
3951 | #endif | |
3952 | ||
6cfb0d5d | 3953 | #ifdef CONFIG_SCHEDSTATS |
cb251765 | 3954 | /* Even if schedstat is disabled, there should not be garbage */ |
41acab88 | 3955 | memset(&p->se.statistics, 0, sizeof(p->se.statistics)); |
6cfb0d5d | 3956 | #endif |
476d139c | 3957 | |
aab03e05 | 3958 | RB_CLEAR_NODE(&p->dl.rb_node); |
40767b0d | 3959 | init_dl_task_timer(&p->dl); |
209a0cbd | 3960 | init_dl_inactive_task_timer(&p->dl); |
a5e7be3b | 3961 | __dl_clear_params(p); |
aab03e05 | 3962 | |
fa717060 | 3963 | INIT_LIST_HEAD(&p->rt.run_list); |
ff77e468 PZ |
3964 | p->rt.timeout = 0; |
3965 | p->rt.time_slice = sched_rr_timeslice; | |
3966 | p->rt.on_rq = 0; | |
3967 | p->rt.on_list = 0; | |
476d139c | 3968 | |
e107be36 AK |
3969 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
3970 | INIT_HLIST_HEAD(&p->preempt_notifiers); | |
3971 | #endif | |
cbee9f88 | 3972 | |
5e1f0f09 MG |
3973 | #ifdef CONFIG_COMPACTION |
3974 | p->capture_control = NULL; | |
3975 | #endif | |
13784475 | 3976 | init_numa_balancing(clone_flags, p); |
a1488664 | 3977 | #ifdef CONFIG_SMP |
8c4890d1 | 3978 | p->wake_entry.u_flags = CSD_TYPE_TTWU; |
6d337eab | 3979 | p->migration_pending = NULL; |
a1488664 | 3980 | #endif |
dd41f596 IM |
3981 | } |
3982 | ||
2a595721 SD |
3983 | DEFINE_STATIC_KEY_FALSE(sched_numa_balancing); |
3984 | ||
1a687c2e | 3985 | #ifdef CONFIG_NUMA_BALANCING |
c3b9bc5b | 3986 | |
1a687c2e MG |
3987 | void set_numabalancing_state(bool enabled) |
3988 | { | |
3989 | if (enabled) | |
2a595721 | 3990 | static_branch_enable(&sched_numa_balancing); |
1a687c2e | 3991 | else |
2a595721 | 3992 | static_branch_disable(&sched_numa_balancing); |
1a687c2e | 3993 | } |
54a43d54 AK |
3994 | |
3995 | #ifdef CONFIG_PROC_SYSCTL | |
3996 | int sysctl_numa_balancing(struct ctl_table *table, int write, | |
32927393 | 3997 | void *buffer, size_t *lenp, loff_t *ppos) |
54a43d54 AK |
3998 | { |
3999 | struct ctl_table t; | |
4000 | int err; | |
2a595721 | 4001 | int state = static_branch_likely(&sched_numa_balancing); |
54a43d54 AK |
4002 | |
4003 | if (write && !capable(CAP_SYS_ADMIN)) | |
4004 | return -EPERM; | |
4005 | ||
4006 | t = *table; | |
4007 | t.data = &state; | |
4008 | err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos); | |
4009 | if (err < 0) | |
4010 | return err; | |
4011 | if (write) | |
4012 | set_numabalancing_state(state); | |
4013 | return err; | |
4014 | } | |
4015 | #endif | |
4016 | #endif | |
dd41f596 | 4017 | |
4698f88c JP |
4018 | #ifdef CONFIG_SCHEDSTATS |
4019 | ||
cb251765 MG |
4020 | DEFINE_STATIC_KEY_FALSE(sched_schedstats); |
4021 | ||
cb251765 MG |
4022 | static void set_schedstats(bool enabled) |
4023 | { | |
4024 | if (enabled) | |
4025 | static_branch_enable(&sched_schedstats); | |
4026 | else | |
4027 | static_branch_disable(&sched_schedstats); | |
4028 | } | |
4029 | ||
4030 | void force_schedstat_enabled(void) | |
4031 | { | |
4032 | if (!schedstat_enabled()) { | |
4033 | pr_info("kernel profiling enabled schedstats, disable via kernel.sched_schedstats.\n"); | |
4034 | static_branch_enable(&sched_schedstats); | |
4035 | } | |
4036 | } | |
4037 | ||
4038 | static int __init setup_schedstats(char *str) | |
4039 | { | |
4040 | int ret = 0; | |
4041 | if (!str) | |
4042 | goto out; | |
4043 | ||
4044 | if (!strcmp(str, "enable")) { | |
1faa491a | 4045 | set_schedstats(true); |
cb251765 MG |
4046 | ret = 1; |
4047 | } else if (!strcmp(str, "disable")) { | |
1faa491a | 4048 | set_schedstats(false); |
cb251765 MG |
4049 | ret = 1; |
4050 | } | |
4051 | out: | |
4052 | if (!ret) | |
4053 | pr_warn("Unable to parse schedstats=\n"); | |
4054 | ||
4055 | return ret; | |
4056 | } | |
4057 | __setup("schedstats=", setup_schedstats); | |
4058 | ||
4059 | #ifdef CONFIG_PROC_SYSCTL | |
32927393 CH |
4060 | int sysctl_schedstats(struct ctl_table *table, int write, void *buffer, |
4061 | size_t *lenp, loff_t *ppos) | |
cb251765 MG |
4062 | { |
4063 | struct ctl_table t; | |
4064 | int err; | |
4065 | int state = static_branch_likely(&sched_schedstats); | |
4066 | ||
4067 | if (write && !capable(CAP_SYS_ADMIN)) | |
4068 | return -EPERM; | |
4069 | ||
4070 | t = *table; | |
4071 | t.data = &state; | |
4072 | err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos); | |
4073 | if (err < 0) | |
4074 | return err; | |
4075 | if (write) | |
4076 | set_schedstats(state); | |
4077 | return err; | |
4078 | } | |
4698f88c | 4079 | #endif /* CONFIG_PROC_SYSCTL */ |
4698f88c | 4080 | #endif /* CONFIG_SCHEDSTATS */ |
dd41f596 IM |
4081 | |
4082 | /* | |
4083 | * fork()/clone()-time setup: | |
4084 | */ | |
aab03e05 | 4085 | int sched_fork(unsigned long clone_flags, struct task_struct *p) |
dd41f596 | 4086 | { |
0122ec5b | 4087 | unsigned long flags; |
dd41f596 | 4088 | |
5e1576ed | 4089 | __sched_fork(clone_flags, p); |
06b83b5f | 4090 | /* |
7dc603c9 | 4091 | * We mark the process as NEW here. This guarantees that |
06b83b5f PZ |
4092 | * nobody will actually run it, and a signal or other external |
4093 | * event cannot wake it up and insert it on the runqueue either. | |
4094 | */ | |
2f064a59 | 4095 | p->__state = TASK_NEW; |
dd41f596 | 4096 | |
c350a04e MG |
4097 | /* |
4098 | * Make sure we do not leak PI boosting priority to the child. | |
4099 | */ | |
4100 | p->prio = current->normal_prio; | |
4101 | ||
e8f14172 PB |
4102 | uclamp_fork(p); |
4103 | ||
b9dc29e7 MG |
4104 | /* |
4105 | * Revert to default priority/policy on fork if requested. | |
4106 | */ | |
4107 | if (unlikely(p->sched_reset_on_fork)) { | |
aab03e05 | 4108 | if (task_has_dl_policy(p) || task_has_rt_policy(p)) { |
b9dc29e7 | 4109 | p->policy = SCHED_NORMAL; |
6c697bdf | 4110 | p->static_prio = NICE_TO_PRIO(0); |
c350a04e MG |
4111 | p->rt_priority = 0; |
4112 | } else if (PRIO_TO_NICE(p->static_prio) < 0) | |
4113 | p->static_prio = NICE_TO_PRIO(0); | |
4114 | ||
4115 | p->prio = p->normal_prio = __normal_prio(p); | |
9059393e | 4116 | set_load_weight(p, false); |
6c697bdf | 4117 | |
b9dc29e7 MG |
4118 | /* |
4119 | * We don't need the reset flag anymore after the fork. It has | |
4120 | * fulfilled its duty: | |
4121 | */ | |
4122 | p->sched_reset_on_fork = 0; | |
4123 | } | |
ca94c442 | 4124 | |
af0fffd9 | 4125 | if (dl_prio(p->prio)) |
aab03e05 | 4126 | return -EAGAIN; |
af0fffd9 | 4127 | else if (rt_prio(p->prio)) |
aab03e05 | 4128 | p->sched_class = &rt_sched_class; |
af0fffd9 | 4129 | else |
2ddbf952 | 4130 | p->sched_class = &fair_sched_class; |
b29739f9 | 4131 | |
7dc603c9 | 4132 | init_entity_runnable_average(&p->se); |
cd29fe6f | 4133 | |
86951599 PZ |
4134 | /* |
4135 | * The child is not yet in the pid-hash so no cgroup attach races, | |
4136 | * and the cgroup is pinned to this child due to cgroup_fork() | |
4137 | * is ran before sched_fork(). | |
4138 | * | |
4139 | * Silence PROVE_RCU. | |
4140 | */ | |
0122ec5b | 4141 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
ce3614da | 4142 | rseq_migrate(p); |
e210bffd | 4143 | /* |
d1ccc66d | 4144 | * We're setting the CPU for the first time, we don't migrate, |
e210bffd PZ |
4145 | * so use __set_task_cpu(). |
4146 | */ | |
af0fffd9 | 4147 | __set_task_cpu(p, smp_processor_id()); |
e210bffd PZ |
4148 | if (p->sched_class->task_fork) |
4149 | p->sched_class->task_fork(p); | |
0122ec5b | 4150 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
5f3edc1b | 4151 | |
f6db8347 | 4152 | #ifdef CONFIG_SCHED_INFO |
dd41f596 | 4153 | if (likely(sched_info_on())) |
52f17b6c | 4154 | memset(&p->sched_info, 0, sizeof(p->sched_info)); |
1da177e4 | 4155 | #endif |
3ca7a440 PZ |
4156 | #if defined(CONFIG_SMP) |
4157 | p->on_cpu = 0; | |
4866cde0 | 4158 | #endif |
01028747 | 4159 | init_task_preempt_count(p); |
806c09a7 | 4160 | #ifdef CONFIG_SMP |
917b627d | 4161 | plist_node_init(&p->pushable_tasks, MAX_PRIO); |
1baca4ce | 4162 | RB_CLEAR_NODE(&p->pushable_dl_tasks); |
806c09a7 | 4163 | #endif |
aab03e05 | 4164 | return 0; |
1da177e4 LT |
4165 | } |
4166 | ||
13685c4a QY |
4167 | void sched_post_fork(struct task_struct *p) |
4168 | { | |
4169 | uclamp_post_fork(p); | |
4170 | } | |
4171 | ||
332ac17e DF |
4172 | unsigned long to_ratio(u64 period, u64 runtime) |
4173 | { | |
4174 | if (runtime == RUNTIME_INF) | |
c52f14d3 | 4175 | return BW_UNIT; |
332ac17e DF |
4176 | |
4177 | /* | |
4178 | * Doing this here saves a lot of checks in all | |
4179 | * the calling paths, and returning zero seems | |
4180 | * safe for them anyway. | |
4181 | */ | |
4182 | if (period == 0) | |
4183 | return 0; | |
4184 | ||
c52f14d3 | 4185 | return div64_u64(runtime << BW_SHIFT, period); |
332ac17e DF |
4186 | } |
4187 | ||
1da177e4 LT |
4188 | /* |
4189 | * wake_up_new_task - wake up a newly created task for the first time. | |
4190 | * | |
4191 | * This function will do some initial scheduler statistics housekeeping | |
4192 | * that must be done for every newly created context, then puts the task | |
4193 | * on the runqueue and wakes it. | |
4194 | */ | |
3e51e3ed | 4195 | void wake_up_new_task(struct task_struct *p) |
1da177e4 | 4196 | { |
eb580751 | 4197 | struct rq_flags rf; |
dd41f596 | 4198 | struct rq *rq; |
fabf318e | 4199 | |
eb580751 | 4200 | raw_spin_lock_irqsave(&p->pi_lock, rf.flags); |
2f064a59 | 4201 | WRITE_ONCE(p->__state, TASK_RUNNING); |
fabf318e PZ |
4202 | #ifdef CONFIG_SMP |
4203 | /* | |
4204 | * Fork balancing, do it here and not earlier because: | |
3bd37062 | 4205 | * - cpus_ptr can change in the fork path |
d1ccc66d | 4206 | * - any previously selected CPU might disappear through hotplug |
e210bffd PZ |
4207 | * |
4208 | * Use __set_task_cpu() to avoid calling sched_class::migrate_task_rq, | |
4209 | * as we're not fully set-up yet. | |
fabf318e | 4210 | */ |
32e839dd | 4211 | p->recent_used_cpu = task_cpu(p); |
ce3614da | 4212 | rseq_migrate(p); |
3aef1551 | 4213 | __set_task_cpu(p, select_task_rq(p, task_cpu(p), WF_FORK)); |
0017d735 | 4214 | #endif |
b7fa30c9 | 4215 | rq = __task_rq_lock(p, &rf); |
4126bad6 | 4216 | update_rq_clock(rq); |
d0fe0b9c | 4217 | post_init_entity_util_avg(p); |
0017d735 | 4218 | |
7a57f32a | 4219 | activate_task(rq, p, ENQUEUE_NOCLOCK); |
fbd705a0 | 4220 | trace_sched_wakeup_new(p); |
a7558e01 | 4221 | check_preempt_curr(rq, p, WF_FORK); |
9a897c5a | 4222 | #ifdef CONFIG_SMP |
0aaafaab PZ |
4223 | if (p->sched_class->task_woken) { |
4224 | /* | |
b19a888c | 4225 | * Nothing relies on rq->lock after this, so it's fine to |
0aaafaab PZ |
4226 | * drop it. |
4227 | */ | |
d8ac8971 | 4228 | rq_unpin_lock(rq, &rf); |
efbbd05a | 4229 | p->sched_class->task_woken(rq, p); |
d8ac8971 | 4230 | rq_repin_lock(rq, &rf); |
0aaafaab | 4231 | } |
9a897c5a | 4232 | #endif |
eb580751 | 4233 | task_rq_unlock(rq, p, &rf); |
1da177e4 LT |
4234 | } |
4235 | ||
e107be36 AK |
4236 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
4237 | ||
b7203428 | 4238 | static DEFINE_STATIC_KEY_FALSE(preempt_notifier_key); |
1cde2930 | 4239 | |
2ecd9d29 PZ |
4240 | void preempt_notifier_inc(void) |
4241 | { | |
b7203428 | 4242 | static_branch_inc(&preempt_notifier_key); |
2ecd9d29 PZ |
4243 | } |
4244 | EXPORT_SYMBOL_GPL(preempt_notifier_inc); | |
4245 | ||
4246 | void preempt_notifier_dec(void) | |
4247 | { | |
b7203428 | 4248 | static_branch_dec(&preempt_notifier_key); |
2ecd9d29 PZ |
4249 | } |
4250 | EXPORT_SYMBOL_GPL(preempt_notifier_dec); | |
4251 | ||
e107be36 | 4252 | /** |
80dd99b3 | 4253 | * preempt_notifier_register - tell me when current is being preempted & rescheduled |
421cee29 | 4254 | * @notifier: notifier struct to register |
e107be36 AK |
4255 | */ |
4256 | void preempt_notifier_register(struct preempt_notifier *notifier) | |
4257 | { | |
b7203428 | 4258 | if (!static_branch_unlikely(&preempt_notifier_key)) |
2ecd9d29 PZ |
4259 | WARN(1, "registering preempt_notifier while notifiers disabled\n"); |
4260 | ||
e107be36 AK |
4261 | hlist_add_head(¬ifier->link, ¤t->preempt_notifiers); |
4262 | } | |
4263 | EXPORT_SYMBOL_GPL(preempt_notifier_register); | |
4264 | ||
4265 | /** | |
4266 | * preempt_notifier_unregister - no longer interested in preemption notifications | |
421cee29 | 4267 | * @notifier: notifier struct to unregister |
e107be36 | 4268 | * |
d84525a8 | 4269 | * This is *not* safe to call from within a preemption notifier. |
e107be36 AK |
4270 | */ |
4271 | void preempt_notifier_unregister(struct preempt_notifier *notifier) | |
4272 | { | |
4273 | hlist_del(¬ifier->link); | |
4274 | } | |
4275 | EXPORT_SYMBOL_GPL(preempt_notifier_unregister); | |
4276 | ||
1cde2930 | 4277 | static void __fire_sched_in_preempt_notifiers(struct task_struct *curr) |
e107be36 AK |
4278 | { |
4279 | struct preempt_notifier *notifier; | |
e107be36 | 4280 | |
b67bfe0d | 4281 | hlist_for_each_entry(notifier, &curr->preempt_notifiers, link) |
e107be36 AK |
4282 | notifier->ops->sched_in(notifier, raw_smp_processor_id()); |
4283 | } | |
4284 | ||
1cde2930 PZ |
4285 | static __always_inline void fire_sched_in_preempt_notifiers(struct task_struct *curr) |
4286 | { | |
b7203428 | 4287 | if (static_branch_unlikely(&preempt_notifier_key)) |
1cde2930 PZ |
4288 | __fire_sched_in_preempt_notifiers(curr); |
4289 | } | |
4290 | ||
e107be36 | 4291 | static void |
1cde2930 PZ |
4292 | __fire_sched_out_preempt_notifiers(struct task_struct *curr, |
4293 | struct task_struct *next) | |
e107be36 AK |
4294 | { |
4295 | struct preempt_notifier *notifier; | |
e107be36 | 4296 | |
b67bfe0d | 4297 | hlist_for_each_entry(notifier, &curr->preempt_notifiers, link) |
e107be36 AK |
4298 | notifier->ops->sched_out(notifier, next); |
4299 | } | |
4300 | ||
1cde2930 PZ |
4301 | static __always_inline void |
4302 | fire_sched_out_preempt_notifiers(struct task_struct *curr, | |
4303 | struct task_struct *next) | |
4304 | { | |
b7203428 | 4305 | if (static_branch_unlikely(&preempt_notifier_key)) |
1cde2930 PZ |
4306 | __fire_sched_out_preempt_notifiers(curr, next); |
4307 | } | |
4308 | ||
6d6bc0ad | 4309 | #else /* !CONFIG_PREEMPT_NOTIFIERS */ |
e107be36 | 4310 | |
1cde2930 | 4311 | static inline void fire_sched_in_preempt_notifiers(struct task_struct *curr) |
e107be36 AK |
4312 | { |
4313 | } | |
4314 | ||
1cde2930 | 4315 | static inline void |
e107be36 AK |
4316 | fire_sched_out_preempt_notifiers(struct task_struct *curr, |
4317 | struct task_struct *next) | |
4318 | { | |
4319 | } | |
4320 | ||
6d6bc0ad | 4321 | #endif /* CONFIG_PREEMPT_NOTIFIERS */ |
e107be36 | 4322 | |
31cb1bc0 | 4323 | static inline void prepare_task(struct task_struct *next) |
4324 | { | |
4325 | #ifdef CONFIG_SMP | |
4326 | /* | |
4327 | * Claim the task as running, we do this before switching to it | |
4328 | * such that any running task will have this set. | |
58877d34 PZ |
4329 | * |
4330 | * See the ttwu() WF_ON_CPU case and its ordering comment. | |
31cb1bc0 | 4331 | */ |
58877d34 | 4332 | WRITE_ONCE(next->on_cpu, 1); |
31cb1bc0 | 4333 | #endif |
4334 | } | |
4335 | ||
4336 | static inline void finish_task(struct task_struct *prev) | |
4337 | { | |
4338 | #ifdef CONFIG_SMP | |
4339 | /* | |
58877d34 PZ |
4340 | * This must be the very last reference to @prev from this CPU. After |
4341 | * p->on_cpu is cleared, the task can be moved to a different CPU. We | |
4342 | * must ensure this doesn't happen until the switch is completely | |
31cb1bc0 | 4343 | * finished. |
4344 | * | |
4345 | * In particular, the load of prev->state in finish_task_switch() must | |
4346 | * happen before this. | |
4347 | * | |
4348 | * Pairs with the smp_cond_load_acquire() in try_to_wake_up(). | |
4349 | */ | |
4350 | smp_store_release(&prev->on_cpu, 0); | |
4351 | #endif | |
4352 | } | |
4353 | ||
565790d2 PZ |
4354 | #ifdef CONFIG_SMP |
4355 | ||
4356 | static void do_balance_callbacks(struct rq *rq, struct callback_head *head) | |
4357 | { | |
4358 | void (*func)(struct rq *rq); | |
4359 | struct callback_head *next; | |
4360 | ||
5cb9eaa3 | 4361 | lockdep_assert_rq_held(rq); |
565790d2 PZ |
4362 | |
4363 | while (head) { | |
4364 | func = (void (*)(struct rq *))head->func; | |
4365 | next = head->next; | |
4366 | head->next = NULL; | |
4367 | head = next; | |
4368 | ||
4369 | func(rq); | |
4370 | } | |
4371 | } | |
4372 | ||
ae792702 PZ |
4373 | static void balance_push(struct rq *rq); |
4374 | ||
4375 | struct callback_head balance_push_callback = { | |
4376 | .next = NULL, | |
4377 | .func = (void (*)(struct callback_head *))balance_push, | |
4378 | }; | |
4379 | ||
565790d2 PZ |
4380 | static inline struct callback_head *splice_balance_callbacks(struct rq *rq) |
4381 | { | |
4382 | struct callback_head *head = rq->balance_callback; | |
4383 | ||
5cb9eaa3 | 4384 | lockdep_assert_rq_held(rq); |
ae792702 | 4385 | if (head) |
565790d2 PZ |
4386 | rq->balance_callback = NULL; |
4387 | ||
4388 | return head; | |
4389 | } | |
4390 | ||
4391 | static void __balance_callbacks(struct rq *rq) | |
4392 | { | |
4393 | do_balance_callbacks(rq, splice_balance_callbacks(rq)); | |
4394 | } | |
4395 | ||
4396 | static inline void balance_callbacks(struct rq *rq, struct callback_head *head) | |
4397 | { | |
4398 | unsigned long flags; | |
4399 | ||
4400 | if (unlikely(head)) { | |
5cb9eaa3 | 4401 | raw_spin_rq_lock_irqsave(rq, flags); |
565790d2 | 4402 | do_balance_callbacks(rq, head); |
5cb9eaa3 | 4403 | raw_spin_rq_unlock_irqrestore(rq, flags); |
565790d2 PZ |
4404 | } |
4405 | } | |
4406 | ||
4407 | #else | |
4408 | ||
4409 | static inline void __balance_callbacks(struct rq *rq) | |
4410 | { | |
4411 | } | |
4412 | ||
4413 | static inline struct callback_head *splice_balance_callbacks(struct rq *rq) | |
4414 | { | |
4415 | return NULL; | |
4416 | } | |
4417 | ||
4418 | static inline void balance_callbacks(struct rq *rq, struct callback_head *head) | |
4419 | { | |
4420 | } | |
4421 | ||
4422 | #endif | |
4423 | ||
269d5992 PZ |
4424 | static inline void |
4425 | prepare_lock_switch(struct rq *rq, struct task_struct *next, struct rq_flags *rf) | |
31cb1bc0 | 4426 | { |
269d5992 PZ |
4427 | /* |
4428 | * Since the runqueue lock will be released by the next | |
4429 | * task (which is an invalid locking op but in the case | |
4430 | * of the scheduler it's an obvious special-case), so we | |
4431 | * do an early lockdep release here: | |
4432 | */ | |
4433 | rq_unpin_lock(rq, rf); | |
9ef7e7e3 | 4434 | spin_release(&__rq_lockp(rq)->dep_map, _THIS_IP_); |
31cb1bc0 | 4435 | #ifdef CONFIG_DEBUG_SPINLOCK |
4436 | /* this is a valid case when another task releases the spinlock */ | |
5cb9eaa3 | 4437 | rq_lockp(rq)->owner = next; |
31cb1bc0 | 4438 | #endif |
269d5992 PZ |
4439 | } |
4440 | ||
4441 | static inline void finish_lock_switch(struct rq *rq) | |
4442 | { | |
31cb1bc0 | 4443 | /* |
4444 | * If we are tracking spinlock dependencies then we have to | |
4445 | * fix up the runqueue lock - which gets 'carried over' from | |
4446 | * prev into current: | |
4447 | */ | |
9ef7e7e3 | 4448 | spin_acquire(&__rq_lockp(rq)->dep_map, 0, 0, _THIS_IP_); |
ae792702 | 4449 | __balance_callbacks(rq); |
5cb9eaa3 | 4450 | raw_spin_rq_unlock_irq(rq); |
31cb1bc0 | 4451 | } |
4452 | ||
325ea10c IM |
4453 | /* |
4454 | * NOP if the arch has not defined these: | |
4455 | */ | |
4456 | ||
4457 | #ifndef prepare_arch_switch | |
4458 | # define prepare_arch_switch(next) do { } while (0) | |
4459 | #endif | |
4460 | ||
4461 | #ifndef finish_arch_post_lock_switch | |
4462 | # define finish_arch_post_lock_switch() do { } while (0) | |
4463 | #endif | |
4464 | ||
5fbda3ec TG |
4465 | static inline void kmap_local_sched_out(void) |
4466 | { | |
4467 | #ifdef CONFIG_KMAP_LOCAL | |
4468 | if (unlikely(current->kmap_ctrl.idx)) | |
4469 | __kmap_local_sched_out(); | |
4470 | #endif | |
4471 | } | |
4472 | ||
4473 | static inline void kmap_local_sched_in(void) | |
4474 | { | |
4475 | #ifdef CONFIG_KMAP_LOCAL | |
4476 | if (unlikely(current->kmap_ctrl.idx)) | |
4477 | __kmap_local_sched_in(); | |
4478 | #endif | |
4479 | } | |
4480 | ||
4866cde0 NP |
4481 | /** |
4482 | * prepare_task_switch - prepare to switch tasks | |
4483 | * @rq: the runqueue preparing to switch | |
421cee29 | 4484 | * @prev: the current task that is being switched out |
4866cde0 NP |
4485 | * @next: the task we are going to switch to. |
4486 | * | |
4487 | * This is called with the rq lock held and interrupts off. It must | |
4488 | * be paired with a subsequent finish_task_switch after the context | |
4489 | * switch. | |
4490 | * | |
4491 | * prepare_task_switch sets up locking and calls architecture specific | |
4492 | * hooks. | |
4493 | */ | |
e107be36 AK |
4494 | static inline void |
4495 | prepare_task_switch(struct rq *rq, struct task_struct *prev, | |
4496 | struct task_struct *next) | |
4866cde0 | 4497 | { |
0ed557aa | 4498 | kcov_prepare_switch(prev); |
43148951 | 4499 | sched_info_switch(rq, prev, next); |
fe4b04fa | 4500 | perf_event_task_sched_out(prev, next); |
d7822b1e | 4501 | rseq_preempt(prev); |
e107be36 | 4502 | fire_sched_out_preempt_notifiers(prev, next); |
5fbda3ec | 4503 | kmap_local_sched_out(); |
31cb1bc0 | 4504 | prepare_task(next); |
4866cde0 NP |
4505 | prepare_arch_switch(next); |
4506 | } | |
4507 | ||
1da177e4 LT |
4508 | /** |
4509 | * finish_task_switch - clean up after a task-switch | |
4510 | * @prev: the thread we just switched away from. | |
4511 | * | |
4866cde0 NP |
4512 | * finish_task_switch must be called after the context switch, paired |
4513 | * with a prepare_task_switch call before the context switch. | |
4514 | * finish_task_switch will reconcile locking set up by prepare_task_switch, | |
4515 | * and do any other architecture-specific cleanup actions. | |
1da177e4 LT |
4516 | * |
4517 | * Note that we may have delayed dropping an mm in context_switch(). If | |
41a2d6cf | 4518 | * so, we finish that here outside of the runqueue lock. (Doing it |
1da177e4 LT |
4519 | * with the lock held can cause deadlocks; see schedule() for |
4520 | * details.) | |
dfa50b60 ON |
4521 | * |
4522 | * The context switch have flipped the stack from under us and restored the | |
4523 | * local variables which were saved when this task called schedule() in the | |
4524 | * past. prev == current is still correct but we need to recalculate this_rq | |
4525 | * because prev may have moved to another CPU. | |
1da177e4 | 4526 | */ |
dfa50b60 | 4527 | static struct rq *finish_task_switch(struct task_struct *prev) |
1da177e4 LT |
4528 | __releases(rq->lock) |
4529 | { | |
dfa50b60 | 4530 | struct rq *rq = this_rq(); |
1da177e4 | 4531 | struct mm_struct *mm = rq->prev_mm; |
55a101f8 | 4532 | long prev_state; |
1da177e4 | 4533 | |
609ca066 PZ |
4534 | /* |
4535 | * The previous task will have left us with a preempt_count of 2 | |
4536 | * because it left us after: | |
4537 | * | |
4538 | * schedule() | |
4539 | * preempt_disable(); // 1 | |
4540 | * __schedule() | |
4541 | * raw_spin_lock_irq(&rq->lock) // 2 | |
4542 | * | |
4543 | * Also, see FORK_PREEMPT_COUNT. | |
4544 | */ | |
e2bf1c4b PZ |
4545 | if (WARN_ONCE(preempt_count() != 2*PREEMPT_DISABLE_OFFSET, |
4546 | "corrupted preempt_count: %s/%d/0x%x\n", | |
4547 | current->comm, current->pid, preempt_count())) | |
4548 | preempt_count_set(FORK_PREEMPT_COUNT); | |
609ca066 | 4549 | |
1da177e4 LT |
4550 | rq->prev_mm = NULL; |
4551 | ||
4552 | /* | |
4553 | * A task struct has one reference for the use as "current". | |
c394cc9f | 4554 | * If a task dies, then it sets TASK_DEAD in tsk->state and calls |
55a101f8 ON |
4555 | * schedule one last time. The schedule call will never return, and |
4556 | * the scheduled task must drop that reference. | |
95913d97 PZ |
4557 | * |
4558 | * We must observe prev->state before clearing prev->on_cpu (in | |
31cb1bc0 | 4559 | * finish_task), otherwise a concurrent wakeup can get prev |
95913d97 PZ |
4560 | * running on another CPU and we could rave with its RUNNING -> DEAD |
4561 | * transition, resulting in a double drop. | |
1da177e4 | 4562 | */ |
2f064a59 | 4563 | prev_state = READ_ONCE(prev->__state); |
bf9fae9f | 4564 | vtime_task_switch(prev); |
a8d757ef | 4565 | perf_event_task_sched_in(prev, current); |
31cb1bc0 | 4566 | finish_task(prev); |
4567 | finish_lock_switch(rq); | |
01f23e16 | 4568 | finish_arch_post_lock_switch(); |
0ed557aa | 4569 | kcov_finish_switch(current); |
5fbda3ec TG |
4570 | /* |
4571 | * kmap_local_sched_out() is invoked with rq::lock held and | |
4572 | * interrupts disabled. There is no requirement for that, but the | |
4573 | * sched out code does not have an interrupt enabled section. | |
4574 | * Restoring the maps on sched in does not require interrupts being | |
4575 | * disabled either. | |
4576 | */ | |
4577 | kmap_local_sched_in(); | |
e8fa1362 | 4578 | |
e107be36 | 4579 | fire_sched_in_preempt_notifiers(current); |
306e0604 | 4580 | /* |
70216e18 MD |
4581 | * When switching through a kernel thread, the loop in |
4582 | * membarrier_{private,global}_expedited() may have observed that | |
4583 | * kernel thread and not issued an IPI. It is therefore possible to | |
4584 | * schedule between user->kernel->user threads without passing though | |
4585 | * switch_mm(). Membarrier requires a barrier after storing to | |
4586 | * rq->curr, before returning to userspace, so provide them here: | |
4587 | * | |
4588 | * - a full memory barrier for {PRIVATE,GLOBAL}_EXPEDITED, implicitly | |
4589 | * provided by mmdrop(), | |
4590 | * - a sync_core for SYNC_CORE. | |
306e0604 | 4591 | */ |
70216e18 MD |
4592 | if (mm) { |
4593 | membarrier_mm_sync_core_before_usermode(mm); | |
1da177e4 | 4594 | mmdrop(mm); |
70216e18 | 4595 | } |
1cef1150 PZ |
4596 | if (unlikely(prev_state == TASK_DEAD)) { |
4597 | if (prev->sched_class->task_dead) | |
4598 | prev->sched_class->task_dead(prev); | |
68f24b08 | 4599 | |
1cef1150 PZ |
4600 | /* |
4601 | * Remove function-return probe instances associated with this | |
4602 | * task and put them back on the free list. | |
4603 | */ | |
4604 | kprobe_flush_task(prev); | |
4605 | ||
4606 | /* Task is done with its stack. */ | |
4607 | put_task_stack(prev); | |
4608 | ||
0ff7b2cf | 4609 | put_task_struct_rcu_user(prev); |
c6fd91f0 | 4610 | } |
99e5ada9 | 4611 | |
de734f89 | 4612 | tick_nohz_task_switch(); |
dfa50b60 | 4613 | return rq; |
1da177e4 LT |
4614 | } |
4615 | ||
4616 | /** | |
4617 | * schedule_tail - first thing a freshly forked thread must call. | |
4618 | * @prev: the thread we just switched away from. | |
4619 | */ | |
722a9f92 | 4620 | asmlinkage __visible void schedule_tail(struct task_struct *prev) |
1da177e4 LT |
4621 | __releases(rq->lock) |
4622 | { | |
609ca066 PZ |
4623 | /* |
4624 | * New tasks start with FORK_PREEMPT_COUNT, see there and | |
4625 | * finish_task_switch() for details. | |
4626 | * | |
4627 | * finish_task_switch() will drop rq->lock() and lower preempt_count | |
4628 | * and the preempt_enable() will end up enabling preemption (on | |
4629 | * PREEMPT_COUNT kernels). | |
4630 | */ | |
4631 | ||
13c2235b | 4632 | finish_task_switch(prev); |
1a43a14a | 4633 | preempt_enable(); |
70b97a7f | 4634 | |
1da177e4 | 4635 | if (current->set_child_tid) |
b488893a | 4636 | put_user(task_pid_vnr(current), current->set_child_tid); |
088fe47c EB |
4637 | |
4638 | calculate_sigpending(); | |
1da177e4 LT |
4639 | } |
4640 | ||
4641 | /* | |
dfa50b60 | 4642 | * context_switch - switch to the new MM and the new thread's register state. |
1da177e4 | 4643 | */ |
04936948 | 4644 | static __always_inline struct rq * |
70b97a7f | 4645 | context_switch(struct rq *rq, struct task_struct *prev, |
d8ac8971 | 4646 | struct task_struct *next, struct rq_flags *rf) |
1da177e4 | 4647 | { |
e107be36 | 4648 | prepare_task_switch(rq, prev, next); |
fe4b04fa | 4649 | |
9226d125 ZA |
4650 | /* |
4651 | * For paravirt, this is coupled with an exit in switch_to to | |
4652 | * combine the page table reload and the switch backend into | |
4653 | * one hypercall. | |
4654 | */ | |
224101ed | 4655 | arch_start_context_switch(prev); |
9226d125 | 4656 | |
306e0604 | 4657 | /* |
139d025c PZ |
4658 | * kernel -> kernel lazy + transfer active |
4659 | * user -> kernel lazy + mmgrab() active | |
4660 | * | |
4661 | * kernel -> user switch + mmdrop() active | |
4662 | * user -> user switch | |
306e0604 | 4663 | */ |
139d025c PZ |
4664 | if (!next->mm) { // to kernel |
4665 | enter_lazy_tlb(prev->active_mm, next); | |
4666 | ||
4667 | next->active_mm = prev->active_mm; | |
4668 | if (prev->mm) // from user | |
4669 | mmgrab(prev->active_mm); | |
4670 | else | |
4671 | prev->active_mm = NULL; | |
4672 | } else { // to user | |
227a4aad | 4673 | membarrier_switch_mm(rq, prev->active_mm, next->mm); |
139d025c PZ |
4674 | /* |
4675 | * sys_membarrier() requires an smp_mb() between setting | |
227a4aad | 4676 | * rq->curr / membarrier_switch_mm() and returning to userspace. |
139d025c PZ |
4677 | * |
4678 | * The below provides this either through switch_mm(), or in | |
4679 | * case 'prev->active_mm == next->mm' through | |
4680 | * finish_task_switch()'s mmdrop(). | |
4681 | */ | |
139d025c | 4682 | switch_mm_irqs_off(prev->active_mm, next->mm, next); |
1da177e4 | 4683 | |
139d025c PZ |
4684 | if (!prev->mm) { // from kernel |
4685 | /* will mmdrop() in finish_task_switch(). */ | |
4686 | rq->prev_mm = prev->active_mm; | |
4687 | prev->active_mm = NULL; | |
4688 | } | |
1da177e4 | 4689 | } |
92509b73 | 4690 | |
cb42c9a3 | 4691 | rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP); |
92509b73 | 4692 | |
269d5992 | 4693 | prepare_lock_switch(rq, next, rf); |
1da177e4 LT |
4694 | |
4695 | /* Here we just switch the register state and the stack. */ | |
4696 | switch_to(prev, next, prev); | |
dd41f596 | 4697 | barrier(); |
dfa50b60 ON |
4698 | |
4699 | return finish_task_switch(prev); | |
1da177e4 LT |
4700 | } |
4701 | ||
4702 | /* | |
1c3e8264 | 4703 | * nr_running and nr_context_switches: |
1da177e4 LT |
4704 | * |
4705 | * externally visible scheduler statistics: current number of runnable | |
1c3e8264 | 4706 | * threads, total number of context switches performed since bootup. |
1da177e4 | 4707 | */ |
01aee8fd | 4708 | unsigned int nr_running(void) |
1da177e4 | 4709 | { |
01aee8fd | 4710 | unsigned int i, sum = 0; |
1da177e4 LT |
4711 | |
4712 | for_each_online_cpu(i) | |
4713 | sum += cpu_rq(i)->nr_running; | |
4714 | ||
4715 | return sum; | |
f711f609 | 4716 | } |
1da177e4 | 4717 | |
2ee507c4 | 4718 | /* |
d1ccc66d | 4719 | * Check if only the current task is running on the CPU. |
00cc1633 DD |
4720 | * |
4721 | * Caution: this function does not check that the caller has disabled | |
4722 | * preemption, thus the result might have a time-of-check-to-time-of-use | |
4723 | * race. The caller is responsible to use it correctly, for example: | |
4724 | * | |
dfcb245e | 4725 | * - from a non-preemptible section (of course) |
00cc1633 DD |
4726 | * |
4727 | * - from a thread that is bound to a single CPU | |
4728 | * | |
4729 | * - in a loop with very short iterations (e.g. a polling loop) | |
2ee507c4 TC |
4730 | */ |
4731 | bool single_task_running(void) | |
4732 | { | |
00cc1633 | 4733 | return raw_rq()->nr_running == 1; |
2ee507c4 TC |
4734 | } |
4735 | EXPORT_SYMBOL(single_task_running); | |
4736 | ||
1da177e4 | 4737 | unsigned long long nr_context_switches(void) |
46cb4b7c | 4738 | { |
cc94abfc SR |
4739 | int i; |
4740 | unsigned long long sum = 0; | |
46cb4b7c | 4741 | |
0a945022 | 4742 | for_each_possible_cpu(i) |
1da177e4 | 4743 | sum += cpu_rq(i)->nr_switches; |
46cb4b7c | 4744 | |
1da177e4 LT |
4745 | return sum; |
4746 | } | |
483b4ee6 | 4747 | |
145d952a DL |
4748 | /* |
4749 | * Consumers of these two interfaces, like for example the cpuidle menu | |
4750 | * governor, are using nonsensical data. Preferring shallow idle state selection | |
4751 | * for a CPU that has IO-wait which might not even end up running the task when | |
4752 | * it does become runnable. | |
4753 | */ | |
4754 | ||
8fc2858e | 4755 | unsigned int nr_iowait_cpu(int cpu) |
145d952a DL |
4756 | { |
4757 | return atomic_read(&cpu_rq(cpu)->nr_iowait); | |
4758 | } | |
4759 | ||
e33a9bba | 4760 | /* |
b19a888c | 4761 | * IO-wait accounting, and how it's mostly bollocks (on SMP). |
e33a9bba TH |
4762 | * |
4763 | * The idea behind IO-wait account is to account the idle time that we could | |
4764 | * have spend running if it were not for IO. That is, if we were to improve the | |
4765 | * storage performance, we'd have a proportional reduction in IO-wait time. | |
4766 | * | |
4767 | * This all works nicely on UP, where, when a task blocks on IO, we account | |
4768 | * idle time as IO-wait, because if the storage were faster, it could've been | |
4769 | * running and we'd not be idle. | |
4770 | * | |
4771 | * This has been extended to SMP, by doing the same for each CPU. This however | |
4772 | * is broken. | |
4773 | * | |
4774 | * Imagine for instance the case where two tasks block on one CPU, only the one | |
4775 | * CPU will have IO-wait accounted, while the other has regular idle. Even | |
4776 | * though, if the storage were faster, both could've ran at the same time, | |
4777 | * utilising both CPUs. | |
4778 | * | |
4779 | * This means, that when looking globally, the current IO-wait accounting on | |
4780 | * SMP is a lower bound, by reason of under accounting. | |
4781 | * | |
4782 | * Worse, since the numbers are provided per CPU, they are sometimes | |
4783 | * interpreted per CPU, and that is nonsensical. A blocked task isn't strictly | |
4784 | * associated with any one particular CPU, it can wake to another CPU than it | |
4785 | * blocked on. This means the per CPU IO-wait number is meaningless. | |
4786 | * | |
4787 | * Task CPU affinities can make all that even more 'interesting'. | |
4788 | */ | |
4789 | ||
97455168 | 4790 | unsigned int nr_iowait(void) |
1da177e4 | 4791 | { |
97455168 | 4792 | unsigned int i, sum = 0; |
483b4ee6 | 4793 | |
0a945022 | 4794 | for_each_possible_cpu(i) |
145d952a | 4795 | sum += nr_iowait_cpu(i); |
46cb4b7c | 4796 | |
1da177e4 LT |
4797 | return sum; |
4798 | } | |
483b4ee6 | 4799 | |
dd41f596 | 4800 | #ifdef CONFIG_SMP |
8a0be9ef | 4801 | |
46cb4b7c | 4802 | /* |
38022906 PZ |
4803 | * sched_exec - execve() is a valuable balancing opportunity, because at |
4804 | * this point the task has the smallest effective memory and cache footprint. | |
46cb4b7c | 4805 | */ |
38022906 | 4806 | void sched_exec(void) |
46cb4b7c | 4807 | { |
38022906 | 4808 | struct task_struct *p = current; |
1da177e4 | 4809 | unsigned long flags; |
0017d735 | 4810 | int dest_cpu; |
46cb4b7c | 4811 | |
8f42ced9 | 4812 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
3aef1551 | 4813 | dest_cpu = p->sched_class->select_task_rq(p, task_cpu(p), WF_EXEC); |
0017d735 PZ |
4814 | if (dest_cpu == smp_processor_id()) |
4815 | goto unlock; | |
38022906 | 4816 | |
8f42ced9 | 4817 | if (likely(cpu_active(dest_cpu))) { |
969c7921 | 4818 | struct migration_arg arg = { p, dest_cpu }; |
46cb4b7c | 4819 | |
8f42ced9 PZ |
4820 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
4821 | stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg); | |
1da177e4 LT |
4822 | return; |
4823 | } | |
0017d735 | 4824 | unlock: |
8f42ced9 | 4825 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
1da177e4 | 4826 | } |
dd41f596 | 4827 | |
1da177e4 LT |
4828 | #endif |
4829 | ||
1da177e4 | 4830 | DEFINE_PER_CPU(struct kernel_stat, kstat); |
3292beb3 | 4831 | DEFINE_PER_CPU(struct kernel_cpustat, kernel_cpustat); |
1da177e4 LT |
4832 | |
4833 | EXPORT_PER_CPU_SYMBOL(kstat); | |
3292beb3 | 4834 | EXPORT_PER_CPU_SYMBOL(kernel_cpustat); |
1da177e4 | 4835 | |
6075620b GG |
4836 | /* |
4837 | * The function fair_sched_class.update_curr accesses the struct curr | |
4838 | * and its field curr->exec_start; when called from task_sched_runtime(), | |
4839 | * we observe a high rate of cache misses in practice. | |
4840 | * Prefetching this data results in improved performance. | |
4841 | */ | |
4842 | static inline void prefetch_curr_exec_start(struct task_struct *p) | |
4843 | { | |
4844 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
4845 | struct sched_entity *curr = (&p->se)->cfs_rq->curr; | |
4846 | #else | |
4847 | struct sched_entity *curr = (&task_rq(p)->cfs)->curr; | |
4848 | #endif | |
4849 | prefetch(curr); | |
4850 | prefetch(&curr->exec_start); | |
4851 | } | |
4852 | ||
c5f8d995 HS |
4853 | /* |
4854 | * Return accounted runtime for the task. | |
4855 | * In case the task is currently running, return the runtime plus current's | |
4856 | * pending runtime that have not been accounted yet. | |
4857 | */ | |
4858 | unsigned long long task_sched_runtime(struct task_struct *p) | |
4859 | { | |
eb580751 | 4860 | struct rq_flags rf; |
c5f8d995 | 4861 | struct rq *rq; |
6e998916 | 4862 | u64 ns; |
c5f8d995 | 4863 | |
911b2898 PZ |
4864 | #if defined(CONFIG_64BIT) && defined(CONFIG_SMP) |
4865 | /* | |
97fb7a0a | 4866 | * 64-bit doesn't need locks to atomically read a 64-bit value. |
911b2898 PZ |
4867 | * So we have a optimization chance when the task's delta_exec is 0. |
4868 | * Reading ->on_cpu is racy, but this is ok. | |
4869 | * | |
d1ccc66d IM |
4870 | * If we race with it leaving CPU, we'll take a lock. So we're correct. |
4871 | * If we race with it entering CPU, unaccounted time is 0. This is | |
911b2898 | 4872 | * indistinguishable from the read occurring a few cycles earlier. |
4036ac15 MG |
4873 | * If we see ->on_cpu without ->on_rq, the task is leaving, and has |
4874 | * been accounted, so we're correct here as well. | |
911b2898 | 4875 | */ |
da0c1e65 | 4876 | if (!p->on_cpu || !task_on_rq_queued(p)) |
911b2898 PZ |
4877 | return p->se.sum_exec_runtime; |
4878 | #endif | |
4879 | ||
eb580751 | 4880 | rq = task_rq_lock(p, &rf); |
6e998916 SG |
4881 | /* |
4882 | * Must be ->curr _and_ ->on_rq. If dequeued, we would | |
4883 | * project cycles that may never be accounted to this | |
4884 | * thread, breaking clock_gettime(). | |
4885 | */ | |
4886 | if (task_current(rq, p) && task_on_rq_queued(p)) { | |
6075620b | 4887 | prefetch_curr_exec_start(p); |
6e998916 SG |
4888 | update_rq_clock(rq); |
4889 | p->sched_class->update_curr(rq); | |
4890 | } | |
4891 | ns = p->se.sum_exec_runtime; | |
eb580751 | 4892 | task_rq_unlock(rq, p, &rf); |
c5f8d995 HS |
4893 | |
4894 | return ns; | |
4895 | } | |
48f24c4d | 4896 | |
c006fac5 PT |
4897 | #ifdef CONFIG_SCHED_DEBUG |
4898 | static u64 cpu_resched_latency(struct rq *rq) | |
4899 | { | |
4900 | int latency_warn_ms = READ_ONCE(sysctl_resched_latency_warn_ms); | |
4901 | u64 resched_latency, now = rq_clock(rq); | |
4902 | static bool warned_once; | |
4903 | ||
4904 | if (sysctl_resched_latency_warn_once && warned_once) | |
4905 | return 0; | |
4906 | ||
4907 | if (!need_resched() || !latency_warn_ms) | |
4908 | return 0; | |
4909 | ||
4910 | if (system_state == SYSTEM_BOOTING) | |
4911 | return 0; | |
4912 | ||
4913 | if (!rq->last_seen_need_resched_ns) { | |
4914 | rq->last_seen_need_resched_ns = now; | |
4915 | rq->ticks_without_resched = 0; | |
4916 | return 0; | |
4917 | } | |
4918 | ||
4919 | rq->ticks_without_resched++; | |
4920 | resched_latency = now - rq->last_seen_need_resched_ns; | |
4921 | if (resched_latency <= latency_warn_ms * NSEC_PER_MSEC) | |
4922 | return 0; | |
4923 | ||
4924 | warned_once = true; | |
4925 | ||
4926 | return resched_latency; | |
4927 | } | |
4928 | ||
4929 | static int __init setup_resched_latency_warn_ms(char *str) | |
4930 | { | |
4931 | long val; | |
4932 | ||
4933 | if ((kstrtol(str, 0, &val))) { | |
4934 | pr_warn("Unable to set resched_latency_warn_ms\n"); | |
4935 | return 1; | |
4936 | } | |
4937 | ||
4938 | sysctl_resched_latency_warn_ms = val; | |
4939 | return 1; | |
4940 | } | |
4941 | __setup("resched_latency_warn_ms=", setup_resched_latency_warn_ms); | |
4942 | #else | |
4943 | static inline u64 cpu_resched_latency(struct rq *rq) { return 0; } | |
4944 | #endif /* CONFIG_SCHED_DEBUG */ | |
4945 | ||
7835b98b CL |
4946 | /* |
4947 | * This function gets called by the timer code, with HZ frequency. | |
4948 | * We call it with interrupts disabled. | |
7835b98b CL |
4949 | */ |
4950 | void scheduler_tick(void) | |
4951 | { | |
7835b98b CL |
4952 | int cpu = smp_processor_id(); |
4953 | struct rq *rq = cpu_rq(cpu); | |
dd41f596 | 4954 | struct task_struct *curr = rq->curr; |
8a8c69c3 | 4955 | struct rq_flags rf; |
b4eccf5f | 4956 | unsigned long thermal_pressure; |
c006fac5 | 4957 | u64 resched_latency; |
3e51f33f | 4958 | |
1567c3e3 | 4959 | arch_scale_freq_tick(); |
3e51f33f | 4960 | sched_clock_tick(); |
dd41f596 | 4961 | |
8a8c69c3 PZ |
4962 | rq_lock(rq, &rf); |
4963 | ||
3e51f33f | 4964 | update_rq_clock(rq); |
b4eccf5f | 4965 | thermal_pressure = arch_scale_thermal_pressure(cpu_of(rq)); |
05289b90 | 4966 | update_thermal_load_avg(rq_clock_thermal(rq), rq, thermal_pressure); |
fa85ae24 | 4967 | curr->sched_class->task_tick(rq, curr, 0); |
c006fac5 PT |
4968 | if (sched_feat(LATENCY_WARN)) |
4969 | resched_latency = cpu_resched_latency(rq); | |
3289bdb4 | 4970 | calc_global_load_tick(rq); |
8a8c69c3 PZ |
4971 | |
4972 | rq_unlock(rq, &rf); | |
7835b98b | 4973 | |
c006fac5 PT |
4974 | if (sched_feat(LATENCY_WARN) && resched_latency) |
4975 | resched_latency_warn(cpu, resched_latency); | |
4976 | ||
e9d2b064 | 4977 | perf_event_task_tick(); |
e220d2dc | 4978 | |
e418e1c2 | 4979 | #ifdef CONFIG_SMP |
6eb57e0d | 4980 | rq->idle_balance = idle_cpu(cpu); |
7caff66f | 4981 | trigger_load_balance(rq); |
e418e1c2 | 4982 | #endif |
1da177e4 LT |
4983 | } |
4984 | ||
265f22a9 | 4985 | #ifdef CONFIG_NO_HZ_FULL |
d84b3131 FW |
4986 | |
4987 | struct tick_work { | |
4988 | int cpu; | |
b55bd585 | 4989 | atomic_t state; |
d84b3131 FW |
4990 | struct delayed_work work; |
4991 | }; | |
b55bd585 PM |
4992 | /* Values for ->state, see diagram below. */ |
4993 | #define TICK_SCHED_REMOTE_OFFLINE 0 | |
4994 | #define TICK_SCHED_REMOTE_OFFLINING 1 | |
4995 | #define TICK_SCHED_REMOTE_RUNNING 2 | |
4996 | ||
4997 | /* | |
4998 | * State diagram for ->state: | |
4999 | * | |
5000 | * | |
5001 | * TICK_SCHED_REMOTE_OFFLINE | |
5002 | * | ^ | |
5003 | * | | | |
5004 | * | | sched_tick_remote() | |
5005 | * | | | |
5006 | * | | | |
5007 | * +--TICK_SCHED_REMOTE_OFFLINING | |
5008 | * | ^ | |
5009 | * | | | |
5010 | * sched_tick_start() | | sched_tick_stop() | |
5011 | * | | | |
5012 | * V | | |
5013 | * TICK_SCHED_REMOTE_RUNNING | |
5014 | * | |
5015 | * | |
5016 | * Other transitions get WARN_ON_ONCE(), except that sched_tick_remote() | |
5017 | * and sched_tick_start() are happy to leave the state in RUNNING. | |
5018 | */ | |
d84b3131 FW |
5019 | |
5020 | static struct tick_work __percpu *tick_work_cpu; | |
5021 | ||
5022 | static void sched_tick_remote(struct work_struct *work) | |
5023 | { | |
5024 | struct delayed_work *dwork = to_delayed_work(work); | |
5025 | struct tick_work *twork = container_of(dwork, struct tick_work, work); | |
5026 | int cpu = twork->cpu; | |
5027 | struct rq *rq = cpu_rq(cpu); | |
d9c0ffca | 5028 | struct task_struct *curr; |
d84b3131 | 5029 | struct rq_flags rf; |
d9c0ffca | 5030 | u64 delta; |
b55bd585 | 5031 | int os; |
d84b3131 FW |
5032 | |
5033 | /* | |
5034 | * Handle the tick only if it appears the remote CPU is running in full | |
5035 | * dynticks mode. The check is racy by nature, but missing a tick or | |
5036 | * having one too much is no big deal because the scheduler tick updates | |
5037 | * statistics and checks timeslices in a time-independent way, regardless | |
5038 | * of when exactly it is running. | |
5039 | */ | |
488603b8 | 5040 | if (!tick_nohz_tick_stopped_cpu(cpu)) |
d9c0ffca | 5041 | goto out_requeue; |
d84b3131 | 5042 | |
d9c0ffca FW |
5043 | rq_lock_irq(rq, &rf); |
5044 | curr = rq->curr; | |
488603b8 | 5045 | if (cpu_is_offline(cpu)) |
d9c0ffca | 5046 | goto out_unlock; |
d84b3131 | 5047 | |
d9c0ffca | 5048 | update_rq_clock(rq); |
d9c0ffca | 5049 | |
488603b8 SW |
5050 | if (!is_idle_task(curr)) { |
5051 | /* | |
5052 | * Make sure the next tick runs within a reasonable | |
5053 | * amount of time. | |
5054 | */ | |
5055 | delta = rq_clock_task(rq) - curr->se.exec_start; | |
5056 | WARN_ON_ONCE(delta > (u64)NSEC_PER_SEC * 3); | |
5057 | } | |
d9c0ffca FW |
5058 | curr->sched_class->task_tick(rq, curr, 0); |
5059 | ||
ebc0f83c | 5060 | calc_load_nohz_remote(rq); |
d9c0ffca FW |
5061 | out_unlock: |
5062 | rq_unlock_irq(rq, &rf); | |
d9c0ffca | 5063 | out_requeue: |
ebc0f83c | 5064 | |
d84b3131 FW |
5065 | /* |
5066 | * Run the remote tick once per second (1Hz). This arbitrary | |
5067 | * frequency is large enough to avoid overload but short enough | |
b55bd585 PM |
5068 | * to keep scheduler internal stats reasonably up to date. But |
5069 | * first update state to reflect hotplug activity if required. | |
d84b3131 | 5070 | */ |
b55bd585 PM |
5071 | os = atomic_fetch_add_unless(&twork->state, -1, TICK_SCHED_REMOTE_RUNNING); |
5072 | WARN_ON_ONCE(os == TICK_SCHED_REMOTE_OFFLINE); | |
5073 | if (os == TICK_SCHED_REMOTE_RUNNING) | |
5074 | queue_delayed_work(system_unbound_wq, dwork, HZ); | |
d84b3131 FW |
5075 | } |
5076 | ||
5077 | static void sched_tick_start(int cpu) | |
5078 | { | |
b55bd585 | 5079 | int os; |
d84b3131 FW |
5080 | struct tick_work *twork; |
5081 | ||
5082 | if (housekeeping_cpu(cpu, HK_FLAG_TICK)) | |
5083 | return; | |
5084 | ||
5085 | WARN_ON_ONCE(!tick_work_cpu); | |
5086 | ||
5087 | twork = per_cpu_ptr(tick_work_cpu, cpu); | |
b55bd585 PM |
5088 | os = atomic_xchg(&twork->state, TICK_SCHED_REMOTE_RUNNING); |
5089 | WARN_ON_ONCE(os == TICK_SCHED_REMOTE_RUNNING); | |
5090 | if (os == TICK_SCHED_REMOTE_OFFLINE) { | |
5091 | twork->cpu = cpu; | |
5092 | INIT_DELAYED_WORK(&twork->work, sched_tick_remote); | |
5093 | queue_delayed_work(system_unbound_wq, &twork->work, HZ); | |
5094 | } | |
d84b3131 FW |
5095 | } |
5096 | ||
5097 | #ifdef CONFIG_HOTPLUG_CPU | |
5098 | static void sched_tick_stop(int cpu) | |
5099 | { | |
5100 | struct tick_work *twork; | |
b55bd585 | 5101 | int os; |
d84b3131 FW |
5102 | |
5103 | if (housekeeping_cpu(cpu, HK_FLAG_TICK)) | |
5104 | return; | |
5105 | ||
5106 | WARN_ON_ONCE(!tick_work_cpu); | |
5107 | ||
5108 | twork = per_cpu_ptr(tick_work_cpu, cpu); | |
b55bd585 PM |
5109 | /* There cannot be competing actions, but don't rely on stop-machine. */ |
5110 | os = atomic_xchg(&twork->state, TICK_SCHED_REMOTE_OFFLINING); | |
5111 | WARN_ON_ONCE(os != TICK_SCHED_REMOTE_RUNNING); | |
5112 | /* Don't cancel, as this would mess up the state machine. */ | |
d84b3131 FW |
5113 | } |
5114 | #endif /* CONFIG_HOTPLUG_CPU */ | |
5115 | ||
5116 | int __init sched_tick_offload_init(void) | |
5117 | { | |
5118 | tick_work_cpu = alloc_percpu(struct tick_work); | |
5119 | BUG_ON(!tick_work_cpu); | |
d84b3131 FW |
5120 | return 0; |
5121 | } | |
5122 | ||
5123 | #else /* !CONFIG_NO_HZ_FULL */ | |
5124 | static inline void sched_tick_start(int cpu) { } | |
5125 | static inline void sched_tick_stop(int cpu) { } | |
265f22a9 | 5126 | #endif |
1da177e4 | 5127 | |
c1a280b6 | 5128 | #if defined(CONFIG_PREEMPTION) && (defined(CONFIG_DEBUG_PREEMPT) || \ |
c3bc8fd6 | 5129 | defined(CONFIG_TRACE_PREEMPT_TOGGLE)) |
47252cfb SR |
5130 | /* |
5131 | * If the value passed in is equal to the current preempt count | |
5132 | * then we just disabled preemption. Start timing the latency. | |
5133 | */ | |
5134 | static inline void preempt_latency_start(int val) | |
5135 | { | |
5136 | if (preempt_count() == val) { | |
5137 | unsigned long ip = get_lock_parent_ip(); | |
5138 | #ifdef CONFIG_DEBUG_PREEMPT | |
5139 | current->preempt_disable_ip = ip; | |
5140 | #endif | |
5141 | trace_preempt_off(CALLER_ADDR0, ip); | |
5142 | } | |
5143 | } | |
7e49fcce | 5144 | |
edafe3a5 | 5145 | void preempt_count_add(int val) |
1da177e4 | 5146 | { |
6cd8a4bb | 5147 | #ifdef CONFIG_DEBUG_PREEMPT |
1da177e4 LT |
5148 | /* |
5149 | * Underflow? | |
5150 | */ | |
9a11b49a IM |
5151 | if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0))) |
5152 | return; | |
6cd8a4bb | 5153 | #endif |
bdb43806 | 5154 | __preempt_count_add(val); |
6cd8a4bb | 5155 | #ifdef CONFIG_DEBUG_PREEMPT |
1da177e4 LT |
5156 | /* |
5157 | * Spinlock count overflowing soon? | |
5158 | */ | |
33859f7f MOS |
5159 | DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >= |
5160 | PREEMPT_MASK - 10); | |
6cd8a4bb | 5161 | #endif |
47252cfb | 5162 | preempt_latency_start(val); |
1da177e4 | 5163 | } |
bdb43806 | 5164 | EXPORT_SYMBOL(preempt_count_add); |
edafe3a5 | 5165 | NOKPROBE_SYMBOL(preempt_count_add); |
1da177e4 | 5166 | |
47252cfb SR |
5167 | /* |
5168 | * If the value passed in equals to the current preempt count | |
5169 | * then we just enabled preemption. Stop timing the latency. | |
5170 | */ | |
5171 | static inline void preempt_latency_stop(int val) | |
5172 | { | |
5173 | if (preempt_count() == val) | |
5174 | trace_preempt_on(CALLER_ADDR0, get_lock_parent_ip()); | |
5175 | } | |
5176 | ||
edafe3a5 | 5177 | void preempt_count_sub(int val) |
1da177e4 | 5178 | { |
6cd8a4bb | 5179 | #ifdef CONFIG_DEBUG_PREEMPT |
1da177e4 LT |
5180 | /* |
5181 | * Underflow? | |
5182 | */ | |
01e3eb82 | 5183 | if (DEBUG_LOCKS_WARN_ON(val > preempt_count())) |
9a11b49a | 5184 | return; |
1da177e4 LT |
5185 | /* |
5186 | * Is the spinlock portion underflowing? | |
5187 | */ | |
9a11b49a IM |
5188 | if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) && |
5189 | !(preempt_count() & PREEMPT_MASK))) | |
5190 | return; | |
6cd8a4bb | 5191 | #endif |
9a11b49a | 5192 | |
47252cfb | 5193 | preempt_latency_stop(val); |
bdb43806 | 5194 | __preempt_count_sub(val); |
1da177e4 | 5195 | } |
bdb43806 | 5196 | EXPORT_SYMBOL(preempt_count_sub); |
edafe3a5 | 5197 | NOKPROBE_SYMBOL(preempt_count_sub); |
1da177e4 | 5198 | |
47252cfb SR |
5199 | #else |
5200 | static inline void preempt_latency_start(int val) { } | |
5201 | static inline void preempt_latency_stop(int val) { } | |
1da177e4 LT |
5202 | #endif |
5203 | ||
59ddbcb2 IM |
5204 | static inline unsigned long get_preempt_disable_ip(struct task_struct *p) |
5205 | { | |
5206 | #ifdef CONFIG_DEBUG_PREEMPT | |
5207 | return p->preempt_disable_ip; | |
5208 | #else | |
5209 | return 0; | |
5210 | #endif | |
5211 | } | |
5212 | ||
1da177e4 | 5213 | /* |
dd41f596 | 5214 | * Print scheduling while atomic bug: |
1da177e4 | 5215 | */ |
dd41f596 | 5216 | static noinline void __schedule_bug(struct task_struct *prev) |
1da177e4 | 5217 | { |
d1c6d149 VN |
5218 | /* Save this before calling printk(), since that will clobber it */ |
5219 | unsigned long preempt_disable_ip = get_preempt_disable_ip(current); | |
5220 | ||
664dfa65 DJ |
5221 | if (oops_in_progress) |
5222 | return; | |
5223 | ||
3df0fc5b PZ |
5224 | printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n", |
5225 | prev->comm, prev->pid, preempt_count()); | |
838225b4 | 5226 | |
dd41f596 | 5227 | debug_show_held_locks(prev); |
e21f5b15 | 5228 | print_modules(); |
dd41f596 IM |
5229 | if (irqs_disabled()) |
5230 | print_irqtrace_events(prev); | |
d1c6d149 VN |
5231 | if (IS_ENABLED(CONFIG_DEBUG_PREEMPT) |
5232 | && in_atomic_preempt_off()) { | |
8f47b187 | 5233 | pr_err("Preemption disabled at:"); |
2062a4e8 | 5234 | print_ip_sym(KERN_ERR, preempt_disable_ip); |
8f47b187 | 5235 | } |
748c7201 DBO |
5236 | if (panic_on_warn) |
5237 | panic("scheduling while atomic\n"); | |
5238 | ||
6135fc1e | 5239 | dump_stack(); |
373d4d09 | 5240 | add_taint(TAINT_WARN, LOCKDEP_STILL_OK); |
dd41f596 | 5241 | } |
1da177e4 | 5242 | |
dd41f596 IM |
5243 | /* |
5244 | * Various schedule()-time debugging checks and statistics: | |
5245 | */ | |
312364f3 | 5246 | static inline void schedule_debug(struct task_struct *prev, bool preempt) |
dd41f596 | 5247 | { |
0d9e2632 | 5248 | #ifdef CONFIG_SCHED_STACK_END_CHECK |
29d64551 JH |
5249 | if (task_stack_end_corrupted(prev)) |
5250 | panic("corrupted stack end detected inside scheduler\n"); | |
88485be5 WD |
5251 | |
5252 | if (task_scs_end_corrupted(prev)) | |
5253 | panic("corrupted shadow stack detected inside scheduler\n"); | |
0d9e2632 | 5254 | #endif |
b99def8b | 5255 | |
312364f3 | 5256 | #ifdef CONFIG_DEBUG_ATOMIC_SLEEP |
2f064a59 | 5257 | if (!preempt && READ_ONCE(prev->__state) && prev->non_block_count) { |
312364f3 DV |
5258 | printk(KERN_ERR "BUG: scheduling in a non-blocking section: %s/%d/%i\n", |
5259 | prev->comm, prev->pid, prev->non_block_count); | |
5260 | dump_stack(); | |
5261 | add_taint(TAINT_WARN, LOCKDEP_STILL_OK); | |
5262 | } | |
5263 | #endif | |
5264 | ||
1dc0fffc | 5265 | if (unlikely(in_atomic_preempt_off())) { |
dd41f596 | 5266 | __schedule_bug(prev); |
1dc0fffc PZ |
5267 | preempt_count_set(PREEMPT_DISABLED); |
5268 | } | |
b3fbab05 | 5269 | rcu_sleep_check(); |
9f68b5b7 | 5270 | SCHED_WARN_ON(ct_state() == CONTEXT_USER); |
dd41f596 | 5271 | |
1da177e4 LT |
5272 | profile_hit(SCHED_PROFILING, __builtin_return_address(0)); |
5273 | ||
ae92882e | 5274 | schedstat_inc(this_rq()->sched_count); |
dd41f596 IM |
5275 | } |
5276 | ||
457d1f46 CY |
5277 | static void put_prev_task_balance(struct rq *rq, struct task_struct *prev, |
5278 | struct rq_flags *rf) | |
5279 | { | |
5280 | #ifdef CONFIG_SMP | |
5281 | const struct sched_class *class; | |
5282 | /* | |
5283 | * We must do the balancing pass before put_prev_task(), such | |
5284 | * that when we release the rq->lock the task is in the same | |
5285 | * state as before we took rq->lock. | |
5286 | * | |
5287 | * We can terminate the balance pass as soon as we know there is | |
5288 | * a runnable task of @class priority or higher. | |
5289 | */ | |
5290 | for_class_range(class, prev->sched_class, &idle_sched_class) { | |
5291 | if (class->balance(rq, prev, rf)) | |
5292 | break; | |
5293 | } | |
5294 | #endif | |
5295 | ||
5296 | put_prev_task(rq, prev); | |
5297 | } | |
5298 | ||
dd41f596 IM |
5299 | /* |
5300 | * Pick up the highest-prio task: | |
5301 | */ | |
5302 | static inline struct task_struct * | |
539f6512 | 5303 | __pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) |
dd41f596 | 5304 | { |
49ee5768 | 5305 | const struct sched_class *class; |
dd41f596 | 5306 | struct task_struct *p; |
1da177e4 LT |
5307 | |
5308 | /* | |
0ba87bb2 PZ |
5309 | * Optimization: we know that if all tasks are in the fair class we can |
5310 | * call that function directly, but only if the @prev task wasn't of a | |
b19a888c | 5311 | * higher scheduling class, because otherwise those lose the |
0ba87bb2 | 5312 | * opportunity to pull in more work from other CPUs. |
1da177e4 | 5313 | */ |
aa93cd53 | 5314 | if (likely(prev->sched_class <= &fair_sched_class && |
0ba87bb2 PZ |
5315 | rq->nr_running == rq->cfs.h_nr_running)) { |
5316 | ||
5d7d6056 | 5317 | p = pick_next_task_fair(rq, prev, rf); |
6ccdc84b | 5318 | if (unlikely(p == RETRY_TASK)) |
67692435 | 5319 | goto restart; |
6ccdc84b | 5320 | |
1699949d | 5321 | /* Assume the next prioritized class is idle_sched_class */ |
5d7d6056 | 5322 | if (!p) { |
f488e105 | 5323 | put_prev_task(rq, prev); |
98c2f700 | 5324 | p = pick_next_task_idle(rq); |
f488e105 | 5325 | } |
6ccdc84b PZ |
5326 | |
5327 | return p; | |
1da177e4 LT |
5328 | } |
5329 | ||
67692435 | 5330 | restart: |
457d1f46 | 5331 | put_prev_task_balance(rq, prev, rf); |
67692435 | 5332 | |
34f971f6 | 5333 | for_each_class(class) { |
98c2f700 | 5334 | p = class->pick_next_task(rq); |
67692435 | 5335 | if (p) |
dd41f596 | 5336 | return p; |
dd41f596 | 5337 | } |
34f971f6 | 5338 | |
d1ccc66d IM |
5339 | /* The idle class should always have a runnable task: */ |
5340 | BUG(); | |
dd41f596 | 5341 | } |
1da177e4 | 5342 | |
9edeaea1 | 5343 | #ifdef CONFIG_SCHED_CORE |
539f6512 PZ |
5344 | static inline bool is_task_rq_idle(struct task_struct *t) |
5345 | { | |
5346 | return (task_rq(t)->idle == t); | |
5347 | } | |
5348 | ||
5349 | static inline bool cookie_equals(struct task_struct *a, unsigned long cookie) | |
5350 | { | |
5351 | return is_task_rq_idle(a) || (a->core_cookie == cookie); | |
5352 | } | |
5353 | ||
5354 | static inline bool cookie_match(struct task_struct *a, struct task_struct *b) | |
5355 | { | |
5356 | if (is_task_rq_idle(a) || is_task_rq_idle(b)) | |
5357 | return true; | |
5358 | ||
5359 | return a->core_cookie == b->core_cookie; | |
5360 | } | |
5361 | ||
5362 | // XXX fairness/fwd progress conditions | |
5363 | /* | |
5364 | * Returns | |
5365 | * - NULL if there is no runnable task for this class. | |
5366 | * - the highest priority task for this runqueue if it matches | |
5367 | * rq->core->core_cookie or its priority is greater than max. | |
5368 | * - Else returns idle_task. | |
5369 | */ | |
5370 | static struct task_struct * | |
c6047c2e | 5371 | pick_task(struct rq *rq, const struct sched_class *class, struct task_struct *max, bool in_fi) |
539f6512 PZ |
5372 | { |
5373 | struct task_struct *class_pick, *cookie_pick; | |
5374 | unsigned long cookie = rq->core->core_cookie; | |
5375 | ||
5376 | class_pick = class->pick_task(rq); | |
5377 | if (!class_pick) | |
5378 | return NULL; | |
5379 | ||
5380 | if (!cookie) { | |
5381 | /* | |
5382 | * If class_pick is tagged, return it only if it has | |
5383 | * higher priority than max. | |
5384 | */ | |
5385 | if (max && class_pick->core_cookie && | |
c6047c2e | 5386 | prio_less(class_pick, max, in_fi)) |
539f6512 PZ |
5387 | return idle_sched_class.pick_task(rq); |
5388 | ||
5389 | return class_pick; | |
5390 | } | |
5391 | ||
5392 | /* | |
5393 | * If class_pick is idle or matches cookie, return early. | |
5394 | */ | |
5395 | if (cookie_equals(class_pick, cookie)) | |
5396 | return class_pick; | |
5397 | ||
5398 | cookie_pick = sched_core_find(rq, cookie); | |
5399 | ||
5400 | /* | |
5401 | * If class > max && class > cookie, it is the highest priority task on | |
5402 | * the core (so far) and it must be selected, otherwise we must go with | |
5403 | * the cookie pick in order to satisfy the constraint. | |
5404 | */ | |
c6047c2e JFG |
5405 | if (prio_less(cookie_pick, class_pick, in_fi) && |
5406 | (!max || prio_less(max, class_pick, in_fi))) | |
539f6512 PZ |
5407 | return class_pick; |
5408 | ||
5409 | return cookie_pick; | |
5410 | } | |
5411 | ||
c6047c2e JFG |
5412 | extern void task_vruntime_update(struct rq *rq, struct task_struct *p, bool in_fi); |
5413 | ||
539f6512 PZ |
5414 | static struct task_struct * |
5415 | pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) | |
5416 | { | |
5417 | struct task_struct *next, *max = NULL; | |
5418 | const struct sched_class *class; | |
5419 | const struct cpumask *smt_mask; | |
c6047c2e | 5420 | bool fi_before = false; |
d2dfa17b | 5421 | int i, j, cpu, occ = 0; |
539f6512 | 5422 | bool need_sync; |
539f6512 PZ |
5423 | |
5424 | if (!sched_core_enabled(rq)) | |
5425 | return __pick_next_task(rq, prev, rf); | |
5426 | ||
5427 | cpu = cpu_of(rq); | |
5428 | ||
5429 | /* Stopper task is switching into idle, no need core-wide selection. */ | |
5430 | if (cpu_is_offline(cpu)) { | |
5431 | /* | |
5432 | * Reset core_pick so that we don't enter the fastpath when | |
5433 | * coming online. core_pick would already be migrated to | |
5434 | * another cpu during offline. | |
5435 | */ | |
5436 | rq->core_pick = NULL; | |
5437 | return __pick_next_task(rq, prev, rf); | |
5438 | } | |
5439 | ||
5440 | /* | |
5441 | * If there were no {en,de}queues since we picked (IOW, the task | |
5442 | * pointers are all still valid), and we haven't scheduled the last | |
5443 | * pick yet, do so now. | |
5444 | * | |
5445 | * rq->core_pick can be NULL if no selection was made for a CPU because | |
5446 | * it was either offline or went offline during a sibling's core-wide | |
5447 | * selection. In this case, do a core-wide selection. | |
5448 | */ | |
5449 | if (rq->core->core_pick_seq == rq->core->core_task_seq && | |
5450 | rq->core->core_pick_seq != rq->core_sched_seq && | |
5451 | rq->core_pick) { | |
5452 | WRITE_ONCE(rq->core_sched_seq, rq->core->core_pick_seq); | |
5453 | ||
5454 | next = rq->core_pick; | |
5455 | if (next != prev) { | |
5456 | put_prev_task(rq, prev); | |
5457 | set_next_task(rq, next); | |
5458 | } | |
5459 | ||
5460 | rq->core_pick = NULL; | |
5461 | return next; | |
5462 | } | |
5463 | ||
5464 | put_prev_task_balance(rq, prev, rf); | |
5465 | ||
5466 | smt_mask = cpu_smt_mask(cpu); | |
7afbba11 JFG |
5467 | need_sync = !!rq->core->core_cookie; |
5468 | ||
5469 | /* reset state */ | |
5470 | rq->core->core_cookie = 0UL; | |
5471 | if (rq->core->core_forceidle) { | |
5472 | need_sync = true; | |
5473 | fi_before = true; | |
5474 | rq->core->core_forceidle = false; | |
5475 | } | |
539f6512 PZ |
5476 | |
5477 | /* | |
5478 | * core->core_task_seq, core->core_pick_seq, rq->core_sched_seq | |
5479 | * | |
5480 | * @task_seq guards the task state ({en,de}queues) | |
5481 | * @pick_seq is the @task_seq we did a selection on | |
5482 | * @sched_seq is the @pick_seq we scheduled | |
5483 | * | |
5484 | * However, preemptions can cause multiple picks on the same task set. | |
5485 | * 'Fix' this by also increasing @task_seq for every pick. | |
5486 | */ | |
5487 | rq->core->core_task_seq++; | |
539f6512 | 5488 | |
7afbba11 JFG |
5489 | /* |
5490 | * Optimize for common case where this CPU has no cookies | |
5491 | * and there are no cookied tasks running on siblings. | |
5492 | */ | |
5493 | if (!need_sync) { | |
5494 | for_each_class(class) { | |
5495 | next = class->pick_task(rq); | |
5496 | if (next) | |
5497 | break; | |
5498 | } | |
5499 | ||
5500 | if (!next->core_cookie) { | |
5501 | rq->core_pick = NULL; | |
c6047c2e JFG |
5502 | /* |
5503 | * For robustness, update the min_vruntime_fi for | |
5504 | * unconstrained picks as well. | |
5505 | */ | |
5506 | WARN_ON_ONCE(fi_before); | |
5507 | task_vruntime_update(rq, next, false); | |
7afbba11 JFG |
5508 | goto done; |
5509 | } | |
8039e96f | 5510 | } |
7afbba11 | 5511 | |
539f6512 PZ |
5512 | for_each_cpu(i, smt_mask) { |
5513 | struct rq *rq_i = cpu_rq(i); | |
5514 | ||
5515 | rq_i->core_pick = NULL; | |
5516 | ||
539f6512 PZ |
5517 | if (i != cpu) |
5518 | update_rq_clock(rq_i); | |
5519 | } | |
5520 | ||
5521 | /* | |
cc00c198 | 5522 | * Try and select tasks for each sibling in descending sched_class |
539f6512 PZ |
5523 | * order. |
5524 | */ | |
5525 | for_each_class(class) { | |
5526 | again: | |
5527 | for_each_cpu_wrap(i, smt_mask, cpu) { | |
5528 | struct rq *rq_i = cpu_rq(i); | |
5529 | struct task_struct *p; | |
5530 | ||
5531 | if (rq_i->core_pick) | |
5532 | continue; | |
5533 | ||
5534 | /* | |
5535 | * If this sibling doesn't yet have a suitable task to | |
cc00c198 | 5536 | * run; ask for the most eligible task, given the |
539f6512 PZ |
5537 | * highest priority task already selected for this |
5538 | * core. | |
5539 | */ | |
c6047c2e | 5540 | p = pick_task(rq_i, class, max, fi_before); |
7afbba11 | 5541 | if (!p) |
539f6512 | 5542 | continue; |
539f6512 | 5543 | |
d2dfa17b PZ |
5544 | if (!is_task_rq_idle(p)) |
5545 | occ++; | |
5546 | ||
539f6512 | 5547 | rq_i->core_pick = p; |
c6047c2e JFG |
5548 | if (rq_i->idle == p && rq_i->nr_running) { |
5549 | rq->core->core_forceidle = true; | |
5550 | if (!fi_before) | |
5551 | rq->core->core_forceidle_seq++; | |
5552 | } | |
539f6512 PZ |
5553 | |
5554 | /* | |
5555 | * If this new candidate is of higher priority than the | |
5556 | * previous; and they're incompatible; we need to wipe | |
5557 | * the slate and start over. pick_task makes sure that | |
5558 | * p's priority is more than max if it doesn't match | |
5559 | * max's cookie. | |
5560 | * | |
5561 | * NOTE: this is a linear max-filter and is thus bounded | |
5562 | * in execution time. | |
5563 | */ | |
5564 | if (!max || !cookie_match(max, p)) { | |
5565 | struct task_struct *old_max = max; | |
5566 | ||
5567 | rq->core->core_cookie = p->core_cookie; | |
5568 | max = p; | |
5569 | ||
5570 | if (old_max) { | |
c6047c2e | 5571 | rq->core->core_forceidle = false; |
539f6512 PZ |
5572 | for_each_cpu(j, smt_mask) { |
5573 | if (j == i) | |
5574 | continue; | |
5575 | ||
5576 | cpu_rq(j)->core_pick = NULL; | |
5577 | } | |
d2dfa17b | 5578 | occ = 1; |
539f6512 | 5579 | goto again; |
539f6512 | 5580 | } |
539f6512 PZ |
5581 | } |
5582 | } | |
539f6512 PZ |
5583 | } |
5584 | ||
5585 | rq->core->core_pick_seq = rq->core->core_task_seq; | |
5586 | next = rq->core_pick; | |
5587 | rq->core_sched_seq = rq->core->core_pick_seq; | |
5588 | ||
5589 | /* Something should have been selected for current CPU */ | |
5590 | WARN_ON_ONCE(!next); | |
5591 | ||
5592 | /* | |
5593 | * Reschedule siblings | |
5594 | * | |
5595 | * NOTE: L1TF -- at this point we're no longer running the old task and | |
5596 | * sending an IPI (below) ensures the sibling will no longer be running | |
5597 | * their task. This ensures there is no inter-sibling overlap between | |
5598 | * non-matching user state. | |
5599 | */ | |
5600 | for_each_cpu(i, smt_mask) { | |
5601 | struct rq *rq_i = cpu_rq(i); | |
5602 | ||
5603 | /* | |
5604 | * An online sibling might have gone offline before a task | |
5605 | * could be picked for it, or it might be offline but later | |
5606 | * happen to come online, but its too late and nothing was | |
5607 | * picked for it. That's Ok - it will pick tasks for itself, | |
5608 | * so ignore it. | |
5609 | */ | |
5610 | if (!rq_i->core_pick) | |
5611 | continue; | |
5612 | ||
c6047c2e JFG |
5613 | /* |
5614 | * Update for new !FI->FI transitions, or if continuing to be in !FI: | |
5615 | * fi_before fi update? | |
5616 | * 0 0 1 | |
5617 | * 0 1 1 | |
5618 | * 1 0 1 | |
5619 | * 1 1 0 | |
5620 | */ | |
5621 | if (!(fi_before && rq->core->core_forceidle)) | |
5622 | task_vruntime_update(rq_i, rq_i->core_pick, rq->core->core_forceidle); | |
539f6512 | 5623 | |
d2dfa17b PZ |
5624 | rq_i->core_pick->core_occupation = occ; |
5625 | ||
539f6512 PZ |
5626 | if (i == cpu) { |
5627 | rq_i->core_pick = NULL; | |
5628 | continue; | |
5629 | } | |
5630 | ||
5631 | /* Did we break L1TF mitigation requirements? */ | |
5632 | WARN_ON_ONCE(!cookie_match(next, rq_i->core_pick)); | |
5633 | ||
5634 | if (rq_i->curr == rq_i->core_pick) { | |
5635 | rq_i->core_pick = NULL; | |
5636 | continue; | |
5637 | } | |
5638 | ||
5639 | resched_curr(rq_i); | |
5640 | } | |
5641 | ||
5642 | done: | |
5643 | set_next_task(rq, next); | |
5644 | return next; | |
5645 | } | |
9edeaea1 | 5646 | |
d2dfa17b PZ |
5647 | static bool try_steal_cookie(int this, int that) |
5648 | { | |
5649 | struct rq *dst = cpu_rq(this), *src = cpu_rq(that); | |
5650 | struct task_struct *p; | |
5651 | unsigned long cookie; | |
5652 | bool success = false; | |
5653 | ||
5654 | local_irq_disable(); | |
5655 | double_rq_lock(dst, src); | |
5656 | ||
5657 | cookie = dst->core->core_cookie; | |
5658 | if (!cookie) | |
5659 | goto unlock; | |
5660 | ||
5661 | if (dst->curr != dst->idle) | |
5662 | goto unlock; | |
5663 | ||
5664 | p = sched_core_find(src, cookie); | |
5665 | if (p == src->idle) | |
5666 | goto unlock; | |
5667 | ||
5668 | do { | |
5669 | if (p == src->core_pick || p == src->curr) | |
5670 | goto next; | |
5671 | ||
5672 | if (!cpumask_test_cpu(this, &p->cpus_mask)) | |
5673 | goto next; | |
5674 | ||
5675 | if (p->core_occupation > dst->idle->core_occupation) | |
5676 | goto next; | |
5677 | ||
d2dfa17b PZ |
5678 | deactivate_task(src, p, 0); |
5679 | set_task_cpu(p, this); | |
5680 | activate_task(dst, p, 0); | |
d2dfa17b PZ |
5681 | |
5682 | resched_curr(dst); | |
5683 | ||
5684 | success = true; | |
5685 | break; | |
5686 | ||
5687 | next: | |
5688 | p = sched_core_next(p, cookie); | |
5689 | } while (p); | |
5690 | ||
5691 | unlock: | |
5692 | double_rq_unlock(dst, src); | |
5693 | local_irq_enable(); | |
5694 | ||
5695 | return success; | |
5696 | } | |
5697 | ||
5698 | static bool steal_cookie_task(int cpu, struct sched_domain *sd) | |
5699 | { | |
5700 | int i; | |
5701 | ||
5702 | for_each_cpu_wrap(i, sched_domain_span(sd), cpu) { | |
5703 | if (i == cpu) | |
5704 | continue; | |
5705 | ||
5706 | if (need_resched()) | |
5707 | break; | |
5708 | ||
5709 | if (try_steal_cookie(cpu, i)) | |
5710 | return true; | |
5711 | } | |
5712 | ||
5713 | return false; | |
5714 | } | |
5715 | ||
5716 | static void sched_core_balance(struct rq *rq) | |
5717 | { | |
5718 | struct sched_domain *sd; | |
5719 | int cpu = cpu_of(rq); | |
5720 | ||
5721 | preempt_disable(); | |
5722 | rcu_read_lock(); | |
5723 | raw_spin_rq_unlock_irq(rq); | |
5724 | for_each_domain(cpu, sd) { | |
5725 | if (need_resched()) | |
5726 | break; | |
5727 | ||
5728 | if (steal_cookie_task(cpu, sd)) | |
5729 | break; | |
5730 | } | |
5731 | raw_spin_rq_lock_irq(rq); | |
5732 | rcu_read_unlock(); | |
5733 | preempt_enable(); | |
5734 | } | |
5735 | ||
5736 | static DEFINE_PER_CPU(struct callback_head, core_balance_head); | |
5737 | ||
5738 | void queue_core_balance(struct rq *rq) | |
5739 | { | |
5740 | if (!sched_core_enabled(rq)) | |
5741 | return; | |
5742 | ||
5743 | if (!rq->core->core_cookie) | |
5744 | return; | |
5745 | ||
5746 | if (!rq->nr_running) /* not forced idle */ | |
5747 | return; | |
5748 | ||
5749 | queue_balance_callback(rq, &per_cpu(core_balance_head, rq->cpu), sched_core_balance); | |
5750 | } | |
5751 | ||
9edeaea1 PZ |
5752 | static inline void sched_core_cpu_starting(unsigned int cpu) |
5753 | { | |
5754 | const struct cpumask *smt_mask = cpu_smt_mask(cpu); | |
5755 | struct rq *rq, *core_rq = NULL; | |
5756 | int i; | |
5757 | ||
5758 | core_rq = cpu_rq(cpu)->core; | |
5759 | ||
5760 | if (!core_rq) { | |
5761 | for_each_cpu(i, smt_mask) { | |
5762 | rq = cpu_rq(i); | |
5763 | if (rq->core && rq->core == rq) | |
5764 | core_rq = rq; | |
5765 | } | |
5766 | ||
5767 | if (!core_rq) | |
5768 | core_rq = cpu_rq(cpu); | |
5769 | ||
5770 | for_each_cpu(i, smt_mask) { | |
5771 | rq = cpu_rq(i); | |
5772 | ||
5773 | WARN_ON_ONCE(rq->core && rq->core != core_rq); | |
5774 | rq->core = core_rq; | |
5775 | } | |
5776 | } | |
5777 | } | |
5778 | #else /* !CONFIG_SCHED_CORE */ | |
5779 | ||
5780 | static inline void sched_core_cpu_starting(unsigned int cpu) {} | |
5781 | ||
539f6512 PZ |
5782 | static struct task_struct * |
5783 | pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) | |
5784 | { | |
5785 | return __pick_next_task(rq, prev, rf); | |
5786 | } | |
5787 | ||
9edeaea1 PZ |
5788 | #endif /* CONFIG_SCHED_CORE */ |
5789 | ||
dd41f596 | 5790 | /* |
c259e01a | 5791 | * __schedule() is the main scheduler function. |
edde96ea PE |
5792 | * |
5793 | * The main means of driving the scheduler and thus entering this function are: | |
5794 | * | |
5795 | * 1. Explicit blocking: mutex, semaphore, waitqueue, etc. | |
5796 | * | |
5797 | * 2. TIF_NEED_RESCHED flag is checked on interrupt and userspace return | |
5798 | * paths. For example, see arch/x86/entry_64.S. | |
5799 | * | |
5800 | * To drive preemption between tasks, the scheduler sets the flag in timer | |
5801 | * interrupt handler scheduler_tick(). | |
5802 | * | |
5803 | * 3. Wakeups don't really cause entry into schedule(). They add a | |
5804 | * task to the run-queue and that's it. | |
5805 | * | |
5806 | * Now, if the new task added to the run-queue preempts the current | |
5807 | * task, then the wakeup sets TIF_NEED_RESCHED and schedule() gets | |
5808 | * called on the nearest possible occasion: | |
5809 | * | |
c1a280b6 | 5810 | * - If the kernel is preemptible (CONFIG_PREEMPTION=y): |
edde96ea PE |
5811 | * |
5812 | * - in syscall or exception context, at the next outmost | |
5813 | * preempt_enable(). (this might be as soon as the wake_up()'s | |
5814 | * spin_unlock()!) | |
5815 | * | |
5816 | * - in IRQ context, return from interrupt-handler to | |
5817 | * preemptible context | |
5818 | * | |
c1a280b6 | 5819 | * - If the kernel is not preemptible (CONFIG_PREEMPTION is not set) |
edde96ea PE |
5820 | * then at the next: |
5821 | * | |
5822 | * - cond_resched() call | |
5823 | * - explicit schedule() call | |
5824 | * - return from syscall or exception to user-space | |
5825 | * - return from interrupt-handler to user-space | |
bfd9b2b5 | 5826 | * |
b30f0e3f | 5827 | * WARNING: must be called with preemption disabled! |
dd41f596 | 5828 | */ |
499d7955 | 5829 | static void __sched notrace __schedule(bool preempt) |
dd41f596 IM |
5830 | { |
5831 | struct task_struct *prev, *next; | |
67ca7bde | 5832 | unsigned long *switch_count; |
dbfb089d | 5833 | unsigned long prev_state; |
d8ac8971 | 5834 | struct rq_flags rf; |
dd41f596 | 5835 | struct rq *rq; |
31656519 | 5836 | int cpu; |
dd41f596 | 5837 | |
dd41f596 IM |
5838 | cpu = smp_processor_id(); |
5839 | rq = cpu_rq(cpu); | |
dd41f596 | 5840 | prev = rq->curr; |
dd41f596 | 5841 | |
312364f3 | 5842 | schedule_debug(prev, preempt); |
1da177e4 | 5843 | |
e0ee463c | 5844 | if (sched_feat(HRTICK) || sched_feat(HRTICK_DL)) |
f333fdc9 | 5845 | hrtick_clear(rq); |
8f4d37ec | 5846 | |
46a5d164 | 5847 | local_irq_disable(); |
bcbfdd01 | 5848 | rcu_note_context_switch(preempt); |
46a5d164 | 5849 | |
e0acd0a6 ON |
5850 | /* |
5851 | * Make sure that signal_pending_state()->signal_pending() below | |
5852 | * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE) | |
dbfb089d PZ |
5853 | * done by the caller to avoid the race with signal_wake_up(): |
5854 | * | |
5855 | * __set_current_state(@state) signal_wake_up() | |
5856 | * schedule() set_tsk_thread_flag(p, TIF_SIGPENDING) | |
5857 | * wake_up_state(p, state) | |
5858 | * LOCK rq->lock LOCK p->pi_state | |
5859 | * smp_mb__after_spinlock() smp_mb__after_spinlock() | |
5860 | * if (signal_pending_state()) if (p->state & @state) | |
306e0604 | 5861 | * |
dbfb089d | 5862 | * Also, the membarrier system call requires a full memory barrier |
306e0604 | 5863 | * after coming from user-space, before storing to rq->curr. |
e0acd0a6 | 5864 | */ |
8a8c69c3 | 5865 | rq_lock(rq, &rf); |
d89e588c | 5866 | smp_mb__after_spinlock(); |
1da177e4 | 5867 | |
d1ccc66d IM |
5868 | /* Promote REQ to ACT */ |
5869 | rq->clock_update_flags <<= 1; | |
bce4dc80 | 5870 | update_rq_clock(rq); |
9edfbfed | 5871 | |
246d86b5 | 5872 | switch_count = &prev->nivcsw; |
d136122f | 5873 | |
dbfb089d | 5874 | /* |
d136122f PZ |
5875 | * We must load prev->state once (task_struct::state is volatile), such |
5876 | * that: | |
5877 | * | |
5878 | * - we form a control dependency vs deactivate_task() below. | |
5879 | * - ptrace_{,un}freeze_traced() can change ->state underneath us. | |
dbfb089d | 5880 | */ |
2f064a59 | 5881 | prev_state = READ_ONCE(prev->__state); |
d136122f | 5882 | if (!preempt && prev_state) { |
dbfb089d | 5883 | if (signal_pending_state(prev_state, prev)) { |
2f064a59 | 5884 | WRITE_ONCE(prev->__state, TASK_RUNNING); |
21aa9af0 | 5885 | } else { |
dbfb089d PZ |
5886 | prev->sched_contributes_to_load = |
5887 | (prev_state & TASK_UNINTERRUPTIBLE) && | |
5888 | !(prev_state & TASK_NOLOAD) && | |
5889 | !(prev->flags & PF_FROZEN); | |
5890 | ||
5891 | if (prev->sched_contributes_to_load) | |
5892 | rq->nr_uninterruptible++; | |
5893 | ||
5894 | /* | |
5895 | * __schedule() ttwu() | |
d136122f PZ |
5896 | * prev_state = prev->state; if (p->on_rq && ...) |
5897 | * if (prev_state) goto out; | |
5898 | * p->on_rq = 0; smp_acquire__after_ctrl_dep(); | |
5899 | * p->state = TASK_WAKING | |
5900 | * | |
5901 | * Where __schedule() and ttwu() have matching control dependencies. | |
dbfb089d PZ |
5902 | * |
5903 | * After this, schedule() must not care about p->state any more. | |
5904 | */ | |
bce4dc80 | 5905 | deactivate_task(rq, prev, DEQUEUE_SLEEP | DEQUEUE_NOCLOCK); |
2acca55e | 5906 | |
e33a9bba TH |
5907 | if (prev->in_iowait) { |
5908 | atomic_inc(&rq->nr_iowait); | |
5909 | delayacct_blkio_start(); | |
5910 | } | |
21aa9af0 | 5911 | } |
dd41f596 | 5912 | switch_count = &prev->nvcsw; |
1da177e4 LT |
5913 | } |
5914 | ||
d8ac8971 | 5915 | next = pick_next_task(rq, prev, &rf); |
f26f9aff | 5916 | clear_tsk_need_resched(prev); |
f27dde8d | 5917 | clear_preempt_need_resched(); |
c006fac5 PT |
5918 | #ifdef CONFIG_SCHED_DEBUG |
5919 | rq->last_seen_need_resched_ns = 0; | |
5920 | #endif | |
1da177e4 | 5921 | |
1da177e4 | 5922 | if (likely(prev != next)) { |
1da177e4 | 5923 | rq->nr_switches++; |
5311a98f EB |
5924 | /* |
5925 | * RCU users of rcu_dereference(rq->curr) may not see | |
5926 | * changes to task_struct made by pick_next_task(). | |
5927 | */ | |
5928 | RCU_INIT_POINTER(rq->curr, next); | |
22e4ebb9 MD |
5929 | /* |
5930 | * The membarrier system call requires each architecture | |
5931 | * to have a full memory barrier after updating | |
306e0604 MD |
5932 | * rq->curr, before returning to user-space. |
5933 | * | |
5934 | * Here are the schemes providing that barrier on the | |
5935 | * various architectures: | |
5936 | * - mm ? switch_mm() : mmdrop() for x86, s390, sparc, PowerPC. | |
5937 | * switch_mm() rely on membarrier_arch_switch_mm() on PowerPC. | |
5938 | * - finish_lock_switch() for weakly-ordered | |
5939 | * architectures where spin_unlock is a full barrier, | |
5940 | * - switch_to() for arm64 (weakly-ordered, spin_unlock | |
5941 | * is a RELEASE barrier), | |
22e4ebb9 | 5942 | */ |
1da177e4 LT |
5943 | ++*switch_count; |
5944 | ||
af449901 | 5945 | migrate_disable_switch(rq, prev); |
b05e75d6 JW |
5946 | psi_sched_switch(prev, next, !task_on_rq_queued(prev)); |
5947 | ||
c73464b1 | 5948 | trace_sched_switch(preempt, prev, next); |
d1ccc66d IM |
5949 | |
5950 | /* Also unlocks the rq: */ | |
5951 | rq = context_switch(rq, prev, next, &rf); | |
cbce1a68 | 5952 | } else { |
cb42c9a3 | 5953 | rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP); |
1da177e4 | 5954 | |
565790d2 PZ |
5955 | rq_unpin_lock(rq, &rf); |
5956 | __balance_callbacks(rq); | |
5cb9eaa3 | 5957 | raw_spin_rq_unlock_irq(rq); |
565790d2 | 5958 | } |
1da177e4 | 5959 | } |
c259e01a | 5960 | |
9af6528e PZ |
5961 | void __noreturn do_task_dead(void) |
5962 | { | |
d1ccc66d | 5963 | /* Causes final put_task_struct in finish_task_switch(): */ |
b5bf9a90 | 5964 | set_special_state(TASK_DEAD); |
d1ccc66d IM |
5965 | |
5966 | /* Tell freezer to ignore us: */ | |
5967 | current->flags |= PF_NOFREEZE; | |
5968 | ||
9af6528e PZ |
5969 | __schedule(false); |
5970 | BUG(); | |
d1ccc66d IM |
5971 | |
5972 | /* Avoid "noreturn function does return" - but don't continue if BUG() is a NOP: */ | |
9af6528e | 5973 | for (;;) |
d1ccc66d | 5974 | cpu_relax(); |
9af6528e PZ |
5975 | } |
5976 | ||
9c40cef2 TG |
5977 | static inline void sched_submit_work(struct task_struct *tsk) |
5978 | { | |
c1cecf88 SAS |
5979 | unsigned int task_flags; |
5980 | ||
b03fbd4f | 5981 | if (task_is_running(tsk)) |
9c40cef2 | 5982 | return; |
6d25be57 | 5983 | |
c1cecf88 | 5984 | task_flags = tsk->flags; |
6d25be57 TG |
5985 | /* |
5986 | * If a worker went to sleep, notify and ask workqueue whether | |
5987 | * it wants to wake up a task to maintain concurrency. | |
5988 | * As this function is called inside the schedule() context, | |
5989 | * we disable preemption to avoid it calling schedule() again | |
62849a96 SAS |
5990 | * in the possible wakeup of a kworker and because wq_worker_sleeping() |
5991 | * requires it. | |
6d25be57 | 5992 | */ |
c1cecf88 | 5993 | if (task_flags & (PF_WQ_WORKER | PF_IO_WORKER)) { |
6d25be57 | 5994 | preempt_disable(); |
c1cecf88 | 5995 | if (task_flags & PF_WQ_WORKER) |
771b53d0 JA |
5996 | wq_worker_sleeping(tsk); |
5997 | else | |
5998 | io_wq_worker_sleeping(tsk); | |
6d25be57 TG |
5999 | preempt_enable_no_resched(); |
6000 | } | |
6001 | ||
b0fdc013 SAS |
6002 | if (tsk_is_pi_blocked(tsk)) |
6003 | return; | |
6004 | ||
9c40cef2 TG |
6005 | /* |
6006 | * If we are going to sleep and we have plugged IO queued, | |
6007 | * make sure to submit it to avoid deadlocks. | |
6008 | */ | |
6009 | if (blk_needs_flush_plug(tsk)) | |
6010 | blk_schedule_flush_plug(tsk); | |
6011 | } | |
6012 | ||
6d25be57 TG |
6013 | static void sched_update_worker(struct task_struct *tsk) |
6014 | { | |
771b53d0 JA |
6015 | if (tsk->flags & (PF_WQ_WORKER | PF_IO_WORKER)) { |
6016 | if (tsk->flags & PF_WQ_WORKER) | |
6017 | wq_worker_running(tsk); | |
6018 | else | |
6019 | io_wq_worker_running(tsk); | |
6020 | } | |
6d25be57 TG |
6021 | } |
6022 | ||
722a9f92 | 6023 | asmlinkage __visible void __sched schedule(void) |
c259e01a | 6024 | { |
9c40cef2 TG |
6025 | struct task_struct *tsk = current; |
6026 | ||
6027 | sched_submit_work(tsk); | |
bfd9b2b5 | 6028 | do { |
b30f0e3f | 6029 | preempt_disable(); |
fc13aeba | 6030 | __schedule(false); |
b30f0e3f | 6031 | sched_preempt_enable_no_resched(); |
bfd9b2b5 | 6032 | } while (need_resched()); |
6d25be57 | 6033 | sched_update_worker(tsk); |
c259e01a | 6034 | } |
1da177e4 LT |
6035 | EXPORT_SYMBOL(schedule); |
6036 | ||
8663effb SRV |
6037 | /* |
6038 | * synchronize_rcu_tasks() makes sure that no task is stuck in preempted | |
6039 | * state (have scheduled out non-voluntarily) by making sure that all | |
6040 | * tasks have either left the run queue or have gone into user space. | |
6041 | * As idle tasks do not do either, they must not ever be preempted | |
6042 | * (schedule out non-voluntarily). | |
6043 | * | |
6044 | * schedule_idle() is similar to schedule_preempt_disable() except that it | |
6045 | * never enables preemption because it does not call sched_submit_work(). | |
6046 | */ | |
6047 | void __sched schedule_idle(void) | |
6048 | { | |
6049 | /* | |
6050 | * As this skips calling sched_submit_work(), which the idle task does | |
6051 | * regardless because that function is a nop when the task is in a | |
6052 | * TASK_RUNNING state, make sure this isn't used someplace that the | |
6053 | * current task can be in any other state. Note, idle is always in the | |
6054 | * TASK_RUNNING state. | |
6055 | */ | |
2f064a59 | 6056 | WARN_ON_ONCE(current->__state); |
8663effb SRV |
6057 | do { |
6058 | __schedule(false); | |
6059 | } while (need_resched()); | |
6060 | } | |
6061 | ||
6775de49 | 6062 | #if defined(CONFIG_CONTEXT_TRACKING) && !defined(CONFIG_HAVE_CONTEXT_TRACKING_OFFSTACK) |
722a9f92 | 6063 | asmlinkage __visible void __sched schedule_user(void) |
20ab65e3 FW |
6064 | { |
6065 | /* | |
6066 | * If we come here after a random call to set_need_resched(), | |
6067 | * or we have been woken up remotely but the IPI has not yet arrived, | |
6068 | * we haven't yet exited the RCU idle mode. Do it here manually until | |
6069 | * we find a better solution. | |
7cc78f8f AL |
6070 | * |
6071 | * NB: There are buggy callers of this function. Ideally we | |
c467ea76 | 6072 | * should warn if prev_state != CONTEXT_USER, but that will trigger |
7cc78f8f | 6073 | * too frequently to make sense yet. |
20ab65e3 | 6074 | */ |
7cc78f8f | 6075 | enum ctx_state prev_state = exception_enter(); |
20ab65e3 | 6076 | schedule(); |
7cc78f8f | 6077 | exception_exit(prev_state); |
20ab65e3 FW |
6078 | } |
6079 | #endif | |
6080 | ||
c5491ea7 TG |
6081 | /** |
6082 | * schedule_preempt_disabled - called with preemption disabled | |
6083 | * | |
6084 | * Returns with preemption disabled. Note: preempt_count must be 1 | |
6085 | */ | |
6086 | void __sched schedule_preempt_disabled(void) | |
6087 | { | |
ba74c144 | 6088 | sched_preempt_enable_no_resched(); |
c5491ea7 TG |
6089 | schedule(); |
6090 | preempt_disable(); | |
6091 | } | |
6092 | ||
06b1f808 | 6093 | static void __sched notrace preempt_schedule_common(void) |
a18b5d01 FW |
6094 | { |
6095 | do { | |
47252cfb SR |
6096 | /* |
6097 | * Because the function tracer can trace preempt_count_sub() | |
6098 | * and it also uses preempt_enable/disable_notrace(), if | |
6099 | * NEED_RESCHED is set, the preempt_enable_notrace() called | |
6100 | * by the function tracer will call this function again and | |
6101 | * cause infinite recursion. | |
6102 | * | |
6103 | * Preemption must be disabled here before the function | |
6104 | * tracer can trace. Break up preempt_disable() into two | |
6105 | * calls. One to disable preemption without fear of being | |
6106 | * traced. The other to still record the preemption latency, | |
6107 | * which can also be traced by the function tracer. | |
6108 | */ | |
499d7955 | 6109 | preempt_disable_notrace(); |
47252cfb | 6110 | preempt_latency_start(1); |
fc13aeba | 6111 | __schedule(true); |
47252cfb | 6112 | preempt_latency_stop(1); |
499d7955 | 6113 | preempt_enable_no_resched_notrace(); |
a18b5d01 FW |
6114 | |
6115 | /* | |
6116 | * Check again in case we missed a preemption opportunity | |
6117 | * between schedule and now. | |
6118 | */ | |
a18b5d01 FW |
6119 | } while (need_resched()); |
6120 | } | |
6121 | ||
c1a280b6 | 6122 | #ifdef CONFIG_PREEMPTION |
1da177e4 | 6123 | /* |
a49b4f40 VS |
6124 | * This is the entry point to schedule() from in-kernel preemption |
6125 | * off of preempt_enable. | |
1da177e4 | 6126 | */ |
722a9f92 | 6127 | asmlinkage __visible void __sched notrace preempt_schedule(void) |
1da177e4 | 6128 | { |
1da177e4 LT |
6129 | /* |
6130 | * If there is a non-zero preempt_count or interrupts are disabled, | |
41a2d6cf | 6131 | * we do not want to preempt the current task. Just return.. |
1da177e4 | 6132 | */ |
fbb00b56 | 6133 | if (likely(!preemptible())) |
1da177e4 LT |
6134 | return; |
6135 | ||
a18b5d01 | 6136 | preempt_schedule_common(); |
1da177e4 | 6137 | } |
376e2424 | 6138 | NOKPROBE_SYMBOL(preempt_schedule); |
1da177e4 | 6139 | EXPORT_SYMBOL(preempt_schedule); |
009f60e2 | 6140 | |
2c9a98d3 PZI |
6141 | #ifdef CONFIG_PREEMPT_DYNAMIC |
6142 | DEFINE_STATIC_CALL(preempt_schedule, __preempt_schedule_func); | |
ef72661e | 6143 | EXPORT_STATIC_CALL_TRAMP(preempt_schedule); |
2c9a98d3 PZI |
6144 | #endif |
6145 | ||
6146 | ||
009f60e2 | 6147 | /** |
4eaca0a8 | 6148 | * preempt_schedule_notrace - preempt_schedule called by tracing |
009f60e2 ON |
6149 | * |
6150 | * The tracing infrastructure uses preempt_enable_notrace to prevent | |
6151 | * recursion and tracing preempt enabling caused by the tracing | |
6152 | * infrastructure itself. But as tracing can happen in areas coming | |
6153 | * from userspace or just about to enter userspace, a preempt enable | |
6154 | * can occur before user_exit() is called. This will cause the scheduler | |
6155 | * to be called when the system is still in usermode. | |
6156 | * | |
6157 | * To prevent this, the preempt_enable_notrace will use this function | |
6158 | * instead of preempt_schedule() to exit user context if needed before | |
6159 | * calling the scheduler. | |
6160 | */ | |
4eaca0a8 | 6161 | asmlinkage __visible void __sched notrace preempt_schedule_notrace(void) |
009f60e2 ON |
6162 | { |
6163 | enum ctx_state prev_ctx; | |
6164 | ||
6165 | if (likely(!preemptible())) | |
6166 | return; | |
6167 | ||
6168 | do { | |
47252cfb SR |
6169 | /* |
6170 | * Because the function tracer can trace preempt_count_sub() | |
6171 | * and it also uses preempt_enable/disable_notrace(), if | |
6172 | * NEED_RESCHED is set, the preempt_enable_notrace() called | |
6173 | * by the function tracer will call this function again and | |
6174 | * cause infinite recursion. | |
6175 | * | |
6176 | * Preemption must be disabled here before the function | |
6177 | * tracer can trace. Break up preempt_disable() into two | |
6178 | * calls. One to disable preemption without fear of being | |
6179 | * traced. The other to still record the preemption latency, | |
6180 | * which can also be traced by the function tracer. | |
6181 | */ | |
3d8f74dd | 6182 | preempt_disable_notrace(); |
47252cfb | 6183 | preempt_latency_start(1); |
009f60e2 ON |
6184 | /* |
6185 | * Needs preempt disabled in case user_exit() is traced | |
6186 | * and the tracer calls preempt_enable_notrace() causing | |
6187 | * an infinite recursion. | |
6188 | */ | |
6189 | prev_ctx = exception_enter(); | |
fc13aeba | 6190 | __schedule(true); |
009f60e2 ON |
6191 | exception_exit(prev_ctx); |
6192 | ||
47252cfb | 6193 | preempt_latency_stop(1); |
3d8f74dd | 6194 | preempt_enable_no_resched_notrace(); |
009f60e2 ON |
6195 | } while (need_resched()); |
6196 | } | |
4eaca0a8 | 6197 | EXPORT_SYMBOL_GPL(preempt_schedule_notrace); |
009f60e2 | 6198 | |
2c9a98d3 PZI |
6199 | #ifdef CONFIG_PREEMPT_DYNAMIC |
6200 | DEFINE_STATIC_CALL(preempt_schedule_notrace, __preempt_schedule_notrace_func); | |
ef72661e | 6201 | EXPORT_STATIC_CALL_TRAMP(preempt_schedule_notrace); |
2c9a98d3 PZI |
6202 | #endif |
6203 | ||
c1a280b6 | 6204 | #endif /* CONFIG_PREEMPTION */ |
1da177e4 | 6205 | |
826bfeb3 PZI |
6206 | #ifdef CONFIG_PREEMPT_DYNAMIC |
6207 | ||
6208 | #include <linux/entry-common.h> | |
6209 | ||
6210 | /* | |
6211 | * SC:cond_resched | |
6212 | * SC:might_resched | |
6213 | * SC:preempt_schedule | |
6214 | * SC:preempt_schedule_notrace | |
6215 | * SC:irqentry_exit_cond_resched | |
6216 | * | |
6217 | * | |
6218 | * NONE: | |
6219 | * cond_resched <- __cond_resched | |
6220 | * might_resched <- RET0 | |
6221 | * preempt_schedule <- NOP | |
6222 | * preempt_schedule_notrace <- NOP | |
6223 | * irqentry_exit_cond_resched <- NOP | |
6224 | * | |
6225 | * VOLUNTARY: | |
6226 | * cond_resched <- __cond_resched | |
6227 | * might_resched <- __cond_resched | |
6228 | * preempt_schedule <- NOP | |
6229 | * preempt_schedule_notrace <- NOP | |
6230 | * irqentry_exit_cond_resched <- NOP | |
6231 | * | |
6232 | * FULL: | |
6233 | * cond_resched <- RET0 | |
6234 | * might_resched <- RET0 | |
6235 | * preempt_schedule <- preempt_schedule | |
6236 | * preempt_schedule_notrace <- preempt_schedule_notrace | |
6237 | * irqentry_exit_cond_resched <- irqentry_exit_cond_resched | |
6238 | */ | |
e59e10f8 PZ |
6239 | |
6240 | enum { | |
6241 | preempt_dynamic_none = 0, | |
6242 | preempt_dynamic_voluntary, | |
6243 | preempt_dynamic_full, | |
6244 | }; | |
6245 | ||
1011dcce | 6246 | int preempt_dynamic_mode = preempt_dynamic_full; |
e59e10f8 | 6247 | |
1011dcce | 6248 | int sched_dynamic_mode(const char *str) |
826bfeb3 | 6249 | { |
e59e10f8 | 6250 | if (!strcmp(str, "none")) |
7e1b2eb7 | 6251 | return preempt_dynamic_none; |
e59e10f8 PZ |
6252 | |
6253 | if (!strcmp(str, "voluntary")) | |
7e1b2eb7 | 6254 | return preempt_dynamic_voluntary; |
e59e10f8 PZ |
6255 | |
6256 | if (!strcmp(str, "full")) | |
7e1b2eb7 | 6257 | return preempt_dynamic_full; |
e59e10f8 | 6258 | |
c4681f3f | 6259 | return -EINVAL; |
e59e10f8 PZ |
6260 | } |
6261 | ||
1011dcce | 6262 | void sched_dynamic_update(int mode) |
e59e10f8 PZ |
6263 | { |
6264 | /* | |
6265 | * Avoid {NONE,VOLUNTARY} -> FULL transitions from ever ending up in | |
6266 | * the ZERO state, which is invalid. | |
6267 | */ | |
6268 | static_call_update(cond_resched, __cond_resched); | |
6269 | static_call_update(might_resched, __cond_resched); | |
6270 | static_call_update(preempt_schedule, __preempt_schedule_func); | |
6271 | static_call_update(preempt_schedule_notrace, __preempt_schedule_notrace_func); | |
6272 | static_call_update(irqentry_exit_cond_resched, irqentry_exit_cond_resched); | |
6273 | ||
6274 | switch (mode) { | |
6275 | case preempt_dynamic_none: | |
826bfeb3 | 6276 | static_call_update(cond_resched, __cond_resched); |
9432bbd9 PZ |
6277 | static_call_update(might_resched, (void *)&__static_call_return0); |
6278 | static_call_update(preempt_schedule, NULL); | |
6279 | static_call_update(preempt_schedule_notrace, NULL); | |
6280 | static_call_update(irqentry_exit_cond_resched, NULL); | |
e59e10f8 PZ |
6281 | pr_info("Dynamic Preempt: none\n"); |
6282 | break; | |
6283 | ||
6284 | case preempt_dynamic_voluntary: | |
826bfeb3 PZI |
6285 | static_call_update(cond_resched, __cond_resched); |
6286 | static_call_update(might_resched, __cond_resched); | |
9432bbd9 PZ |
6287 | static_call_update(preempt_schedule, NULL); |
6288 | static_call_update(preempt_schedule_notrace, NULL); | |
6289 | static_call_update(irqentry_exit_cond_resched, NULL); | |
e59e10f8 PZ |
6290 | pr_info("Dynamic Preempt: voluntary\n"); |
6291 | break; | |
6292 | ||
6293 | case preempt_dynamic_full: | |
9432bbd9 PZ |
6294 | static_call_update(cond_resched, (void *)&__static_call_return0); |
6295 | static_call_update(might_resched, (void *)&__static_call_return0); | |
826bfeb3 PZI |
6296 | static_call_update(preempt_schedule, __preempt_schedule_func); |
6297 | static_call_update(preempt_schedule_notrace, __preempt_schedule_notrace_func); | |
6298 | static_call_update(irqentry_exit_cond_resched, irqentry_exit_cond_resched); | |
e59e10f8 PZ |
6299 | pr_info("Dynamic Preempt: full\n"); |
6300 | break; | |
6301 | } | |
6302 | ||
6303 | preempt_dynamic_mode = mode; | |
6304 | } | |
6305 | ||
6306 | static int __init setup_preempt_mode(char *str) | |
6307 | { | |
6308 | int mode = sched_dynamic_mode(str); | |
6309 | if (mode < 0) { | |
6310 | pr_warn("Dynamic Preempt: unsupported mode: %s\n", str); | |
826bfeb3 PZI |
6311 | return 1; |
6312 | } | |
e59e10f8 PZ |
6313 | |
6314 | sched_dynamic_update(mode); | |
826bfeb3 PZI |
6315 | return 0; |
6316 | } | |
6317 | __setup("preempt=", setup_preempt_mode); | |
6318 | ||
6319 | #endif /* CONFIG_PREEMPT_DYNAMIC */ | |
6320 | ||
1da177e4 | 6321 | /* |
a49b4f40 | 6322 | * This is the entry point to schedule() from kernel preemption |
1da177e4 LT |
6323 | * off of irq context. |
6324 | * Note, that this is called and return with irqs disabled. This will | |
6325 | * protect us against recursive calling from irq. | |
6326 | */ | |
722a9f92 | 6327 | asmlinkage __visible void __sched preempt_schedule_irq(void) |
1da177e4 | 6328 | { |
b22366cd | 6329 | enum ctx_state prev_state; |
6478d880 | 6330 | |
2ed6e34f | 6331 | /* Catch callers which need to be fixed */ |
f27dde8d | 6332 | BUG_ON(preempt_count() || !irqs_disabled()); |
1da177e4 | 6333 | |
b22366cd FW |
6334 | prev_state = exception_enter(); |
6335 | ||
3a5c359a | 6336 | do { |
3d8f74dd | 6337 | preempt_disable(); |
3a5c359a | 6338 | local_irq_enable(); |
fc13aeba | 6339 | __schedule(true); |
3a5c359a | 6340 | local_irq_disable(); |
3d8f74dd | 6341 | sched_preempt_enable_no_resched(); |
5ed0cec0 | 6342 | } while (need_resched()); |
b22366cd FW |
6343 | |
6344 | exception_exit(prev_state); | |
1da177e4 LT |
6345 | } |
6346 | ||
ac6424b9 | 6347 | int default_wake_function(wait_queue_entry_t *curr, unsigned mode, int wake_flags, |
95cdf3b7 | 6348 | void *key) |
1da177e4 | 6349 | { |
062d3f95 | 6350 | WARN_ON_ONCE(IS_ENABLED(CONFIG_SCHED_DEBUG) && wake_flags & ~WF_SYNC); |
63859d4f | 6351 | return try_to_wake_up(curr->private, mode, wake_flags); |
1da177e4 | 6352 | } |
1da177e4 LT |
6353 | EXPORT_SYMBOL(default_wake_function); |
6354 | ||
b29739f9 IM |
6355 | #ifdef CONFIG_RT_MUTEXES |
6356 | ||
acd58620 PZ |
6357 | static inline int __rt_effective_prio(struct task_struct *pi_task, int prio) |
6358 | { | |
6359 | if (pi_task) | |
6360 | prio = min(prio, pi_task->prio); | |
6361 | ||
6362 | return prio; | |
6363 | } | |
6364 | ||
6365 | static inline int rt_effective_prio(struct task_struct *p, int prio) | |
6366 | { | |
6367 | struct task_struct *pi_task = rt_mutex_get_top_task(p); | |
6368 | ||
6369 | return __rt_effective_prio(pi_task, prio); | |
6370 | } | |
6371 | ||
b29739f9 IM |
6372 | /* |
6373 | * rt_mutex_setprio - set the current priority of a task | |
acd58620 PZ |
6374 | * @p: task to boost |
6375 | * @pi_task: donor task | |
b29739f9 IM |
6376 | * |
6377 | * This function changes the 'effective' priority of a task. It does | |
6378 | * not touch ->normal_prio like __setscheduler(). | |
6379 | * | |
c365c292 TG |
6380 | * Used by the rt_mutex code to implement priority inheritance |
6381 | * logic. Call site only calls if the priority of the task changed. | |
b29739f9 | 6382 | */ |
acd58620 | 6383 | void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task) |
b29739f9 | 6384 | { |
acd58620 | 6385 | int prio, oldprio, queued, running, queue_flag = |
7a57f32a | 6386 | DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK; |
83ab0aa0 | 6387 | const struct sched_class *prev_class; |
eb580751 PZ |
6388 | struct rq_flags rf; |
6389 | struct rq *rq; | |
b29739f9 | 6390 | |
acd58620 PZ |
6391 | /* XXX used to be waiter->prio, not waiter->task->prio */ |
6392 | prio = __rt_effective_prio(pi_task, p->normal_prio); | |
6393 | ||
6394 | /* | |
6395 | * If nothing changed; bail early. | |
6396 | */ | |
6397 | if (p->pi_top_task == pi_task && prio == p->prio && !dl_prio(prio)) | |
6398 | return; | |
b29739f9 | 6399 | |
eb580751 | 6400 | rq = __task_rq_lock(p, &rf); |
80f5c1b8 | 6401 | update_rq_clock(rq); |
acd58620 PZ |
6402 | /* |
6403 | * Set under pi_lock && rq->lock, such that the value can be used under | |
6404 | * either lock. | |
6405 | * | |
6406 | * Note that there is loads of tricky to make this pointer cache work | |
6407 | * right. rt_mutex_slowunlock()+rt_mutex_postunlock() work together to | |
6408 | * ensure a task is de-boosted (pi_task is set to NULL) before the | |
6409 | * task is allowed to run again (and can exit). This ensures the pointer | |
b19a888c | 6410 | * points to a blocked task -- which guarantees the task is present. |
acd58620 PZ |
6411 | */ |
6412 | p->pi_top_task = pi_task; | |
6413 | ||
6414 | /* | |
6415 | * For FIFO/RR we only need to set prio, if that matches we're done. | |
6416 | */ | |
6417 | if (prio == p->prio && !dl_prio(prio)) | |
6418 | goto out_unlock; | |
b29739f9 | 6419 | |
1c4dd99b TG |
6420 | /* |
6421 | * Idle task boosting is a nono in general. There is one | |
6422 | * exception, when PREEMPT_RT and NOHZ is active: | |
6423 | * | |
6424 | * The idle task calls get_next_timer_interrupt() and holds | |
6425 | * the timer wheel base->lock on the CPU and another CPU wants | |
6426 | * to access the timer (probably to cancel it). We can safely | |
6427 | * ignore the boosting request, as the idle CPU runs this code | |
6428 | * with interrupts disabled and will complete the lock | |
6429 | * protected section without being interrupted. So there is no | |
6430 | * real need to boost. | |
6431 | */ | |
6432 | if (unlikely(p == rq->idle)) { | |
6433 | WARN_ON(p != rq->curr); | |
6434 | WARN_ON(p->pi_blocked_on); | |
6435 | goto out_unlock; | |
6436 | } | |
6437 | ||
b91473ff | 6438 | trace_sched_pi_setprio(p, pi_task); |
d5f9f942 | 6439 | oldprio = p->prio; |
ff77e468 PZ |
6440 | |
6441 | if (oldprio == prio) | |
6442 | queue_flag &= ~DEQUEUE_MOVE; | |
6443 | ||
83ab0aa0 | 6444 | prev_class = p->sched_class; |
da0c1e65 | 6445 | queued = task_on_rq_queued(p); |
051a1d1a | 6446 | running = task_current(rq, p); |
da0c1e65 | 6447 | if (queued) |
ff77e468 | 6448 | dequeue_task(rq, p, queue_flag); |
0e1f3483 | 6449 | if (running) |
f3cd1c4e | 6450 | put_prev_task(rq, p); |
dd41f596 | 6451 | |
2d3d891d DF |
6452 | /* |
6453 | * Boosting condition are: | |
6454 | * 1. -rt task is running and holds mutex A | |
6455 | * --> -dl task blocks on mutex A | |
6456 | * | |
6457 | * 2. -dl task is running and holds mutex A | |
6458 | * --> -dl task blocks on mutex A and could preempt the | |
6459 | * running task | |
6460 | */ | |
6461 | if (dl_prio(prio)) { | |
466af29b | 6462 | if (!dl_prio(p->normal_prio) || |
740797ce JL |
6463 | (pi_task && dl_prio(pi_task->prio) && |
6464 | dl_entity_preempt(&pi_task->dl, &p->dl))) { | |
2279f540 | 6465 | p->dl.pi_se = pi_task->dl.pi_se; |
ff77e468 | 6466 | queue_flag |= ENQUEUE_REPLENISH; |
2279f540 JL |
6467 | } else { |
6468 | p->dl.pi_se = &p->dl; | |
6469 | } | |
aab03e05 | 6470 | p->sched_class = &dl_sched_class; |
2d3d891d DF |
6471 | } else if (rt_prio(prio)) { |
6472 | if (dl_prio(oldprio)) | |
2279f540 | 6473 | p->dl.pi_se = &p->dl; |
2d3d891d | 6474 | if (oldprio < prio) |
ff77e468 | 6475 | queue_flag |= ENQUEUE_HEAD; |
dd41f596 | 6476 | p->sched_class = &rt_sched_class; |
2d3d891d DF |
6477 | } else { |
6478 | if (dl_prio(oldprio)) | |
2279f540 | 6479 | p->dl.pi_se = &p->dl; |
746db944 BS |
6480 | if (rt_prio(oldprio)) |
6481 | p->rt.timeout = 0; | |
dd41f596 | 6482 | p->sched_class = &fair_sched_class; |
2d3d891d | 6483 | } |
dd41f596 | 6484 | |
b29739f9 IM |
6485 | p->prio = prio; |
6486 | ||
da0c1e65 | 6487 | if (queued) |
ff77e468 | 6488 | enqueue_task(rq, p, queue_flag); |
a399d233 | 6489 | if (running) |
03b7fad1 | 6490 | set_next_task(rq, p); |
cb469845 | 6491 | |
da7a735e | 6492 | check_class_changed(rq, p, prev_class, oldprio); |
1c4dd99b | 6493 | out_unlock: |
d1ccc66d IM |
6494 | /* Avoid rq from going away on us: */ |
6495 | preempt_disable(); | |
4c9a4bc8 | 6496 | |
565790d2 PZ |
6497 | rq_unpin_lock(rq, &rf); |
6498 | __balance_callbacks(rq); | |
5cb9eaa3 | 6499 | raw_spin_rq_unlock(rq); |
565790d2 | 6500 | |
4c9a4bc8 | 6501 | preempt_enable(); |
b29739f9 | 6502 | } |
acd58620 PZ |
6503 | #else |
6504 | static inline int rt_effective_prio(struct task_struct *p, int prio) | |
6505 | { | |
6506 | return prio; | |
6507 | } | |
b29739f9 | 6508 | #endif |
d50dde5a | 6509 | |
36c8b586 | 6510 | void set_user_nice(struct task_struct *p, long nice) |
1da177e4 | 6511 | { |
49bd21ef | 6512 | bool queued, running; |
53a23364 | 6513 | int old_prio; |
eb580751 | 6514 | struct rq_flags rf; |
70b97a7f | 6515 | struct rq *rq; |
1da177e4 | 6516 | |
75e45d51 | 6517 | if (task_nice(p) == nice || nice < MIN_NICE || nice > MAX_NICE) |
1da177e4 LT |
6518 | return; |
6519 | /* | |
6520 | * We have to be careful, if called from sys_setpriority(), | |
6521 | * the task might be in the middle of scheduling on another CPU. | |
6522 | */ | |
eb580751 | 6523 | rq = task_rq_lock(p, &rf); |
2fb8d367 PZ |
6524 | update_rq_clock(rq); |
6525 | ||
1da177e4 LT |
6526 | /* |
6527 | * The RT priorities are set via sched_setscheduler(), but we still | |
6528 | * allow the 'normal' nice value to be set - but as expected | |
b19a888c | 6529 | * it won't have any effect on scheduling until the task is |
aab03e05 | 6530 | * SCHED_DEADLINE, SCHED_FIFO or SCHED_RR: |
1da177e4 | 6531 | */ |
aab03e05 | 6532 | if (task_has_dl_policy(p) || task_has_rt_policy(p)) { |
1da177e4 LT |
6533 | p->static_prio = NICE_TO_PRIO(nice); |
6534 | goto out_unlock; | |
6535 | } | |
da0c1e65 | 6536 | queued = task_on_rq_queued(p); |
49bd21ef | 6537 | running = task_current(rq, p); |
da0c1e65 | 6538 | if (queued) |
7a57f32a | 6539 | dequeue_task(rq, p, DEQUEUE_SAVE | DEQUEUE_NOCLOCK); |
49bd21ef PZ |
6540 | if (running) |
6541 | put_prev_task(rq, p); | |
1da177e4 | 6542 | |
1da177e4 | 6543 | p->static_prio = NICE_TO_PRIO(nice); |
9059393e | 6544 | set_load_weight(p, true); |
b29739f9 IM |
6545 | old_prio = p->prio; |
6546 | p->prio = effective_prio(p); | |
1da177e4 | 6547 | |
5443a0be | 6548 | if (queued) |
7134b3e9 | 6549 | enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK); |
49bd21ef | 6550 | if (running) |
03b7fad1 | 6551 | set_next_task(rq, p); |
5443a0be FW |
6552 | |
6553 | /* | |
6554 | * If the task increased its priority or is running and | |
6555 | * lowered its priority, then reschedule its CPU: | |
6556 | */ | |
6557 | p->sched_class->prio_changed(rq, p, old_prio); | |
6558 | ||
1da177e4 | 6559 | out_unlock: |
eb580751 | 6560 | task_rq_unlock(rq, p, &rf); |
1da177e4 | 6561 | } |
1da177e4 LT |
6562 | EXPORT_SYMBOL(set_user_nice); |
6563 | ||
e43379f1 MM |
6564 | /* |
6565 | * can_nice - check if a task can reduce its nice value | |
6566 | * @p: task | |
6567 | * @nice: nice value | |
6568 | */ | |
36c8b586 | 6569 | int can_nice(const struct task_struct *p, const int nice) |
e43379f1 | 6570 | { |
d1ccc66d | 6571 | /* Convert nice value [19,-20] to rlimit style value [1,40]: */ |
7aa2c016 | 6572 | int nice_rlim = nice_to_rlimit(nice); |
48f24c4d | 6573 | |
78d7d407 | 6574 | return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) || |
e43379f1 MM |
6575 | capable(CAP_SYS_NICE)); |
6576 | } | |
6577 | ||
1da177e4 LT |
6578 | #ifdef __ARCH_WANT_SYS_NICE |
6579 | ||
6580 | /* | |
6581 | * sys_nice - change the priority of the current process. | |
6582 | * @increment: priority increment | |
6583 | * | |
6584 | * sys_setpriority is a more generic, but much slower function that | |
6585 | * does similar things. | |
6586 | */ | |
5add95d4 | 6587 | SYSCALL_DEFINE1(nice, int, increment) |
1da177e4 | 6588 | { |
48f24c4d | 6589 | long nice, retval; |
1da177e4 LT |
6590 | |
6591 | /* | |
6592 | * Setpriority might change our priority at the same moment. | |
6593 | * We don't have to worry. Conceptually one call occurs first | |
6594 | * and we have a single winner. | |
6595 | */ | |
a9467fa3 | 6596 | increment = clamp(increment, -NICE_WIDTH, NICE_WIDTH); |
d0ea0268 | 6597 | nice = task_nice(current) + increment; |
1da177e4 | 6598 | |
a9467fa3 | 6599 | nice = clamp_val(nice, MIN_NICE, MAX_NICE); |
e43379f1 MM |
6600 | if (increment < 0 && !can_nice(current, nice)) |
6601 | return -EPERM; | |
6602 | ||
1da177e4 LT |
6603 | retval = security_task_setnice(current, nice); |
6604 | if (retval) | |
6605 | return retval; | |
6606 | ||
6607 | set_user_nice(current, nice); | |
6608 | return 0; | |
6609 | } | |
6610 | ||
6611 | #endif | |
6612 | ||
6613 | /** | |
6614 | * task_prio - return the priority value of a given task. | |
6615 | * @p: the task in question. | |
6616 | * | |
e69f6186 | 6617 | * Return: The priority value as seen by users in /proc. |
c541bb78 DE |
6618 | * |
6619 | * sched policy return value kernel prio user prio/nice | |
6620 | * | |
6621 | * normal, batch, idle [0 ... 39] [100 ... 139] 0/[-20 ... 19] | |
6622 | * fifo, rr [-2 ... -100] [98 ... 0] [1 ... 99] | |
6623 | * deadline -101 -1 0 | |
1da177e4 | 6624 | */ |
36c8b586 | 6625 | int task_prio(const struct task_struct *p) |
1da177e4 LT |
6626 | { |
6627 | return p->prio - MAX_RT_PRIO; | |
6628 | } | |
6629 | ||
1da177e4 | 6630 | /** |
d1ccc66d | 6631 | * idle_cpu - is a given CPU idle currently? |
1da177e4 | 6632 | * @cpu: the processor in question. |
e69f6186 YB |
6633 | * |
6634 | * Return: 1 if the CPU is currently idle. 0 otherwise. | |
1da177e4 LT |
6635 | */ |
6636 | int idle_cpu(int cpu) | |
6637 | { | |
908a3283 TG |
6638 | struct rq *rq = cpu_rq(cpu); |
6639 | ||
6640 | if (rq->curr != rq->idle) | |
6641 | return 0; | |
6642 | ||
6643 | if (rq->nr_running) | |
6644 | return 0; | |
6645 | ||
6646 | #ifdef CONFIG_SMP | |
126c2092 | 6647 | if (rq->ttwu_pending) |
908a3283 TG |
6648 | return 0; |
6649 | #endif | |
6650 | ||
6651 | return 1; | |
1da177e4 LT |
6652 | } |
6653 | ||
943d355d RJ |
6654 | /** |
6655 | * available_idle_cpu - is a given CPU idle for enqueuing work. | |
6656 | * @cpu: the CPU in question. | |
6657 | * | |
6658 | * Return: 1 if the CPU is currently idle. 0 otherwise. | |
6659 | */ | |
6660 | int available_idle_cpu(int cpu) | |
6661 | { | |
6662 | if (!idle_cpu(cpu)) | |
6663 | return 0; | |
6664 | ||
247f2f6f RJ |
6665 | if (vcpu_is_preempted(cpu)) |
6666 | return 0; | |
6667 | ||
908a3283 | 6668 | return 1; |
1da177e4 LT |
6669 | } |
6670 | ||
1da177e4 | 6671 | /** |
d1ccc66d | 6672 | * idle_task - return the idle task for a given CPU. |
1da177e4 | 6673 | * @cpu: the processor in question. |
e69f6186 | 6674 | * |
d1ccc66d | 6675 | * Return: The idle task for the CPU @cpu. |
1da177e4 | 6676 | */ |
36c8b586 | 6677 | struct task_struct *idle_task(int cpu) |
1da177e4 LT |
6678 | { |
6679 | return cpu_rq(cpu)->idle; | |
6680 | } | |
6681 | ||
7d6a905f VK |
6682 | #ifdef CONFIG_SMP |
6683 | /* | |
6684 | * This function computes an effective utilization for the given CPU, to be | |
6685 | * used for frequency selection given the linear relation: f = u * f_max. | |
6686 | * | |
6687 | * The scheduler tracks the following metrics: | |
6688 | * | |
6689 | * cpu_util_{cfs,rt,dl,irq}() | |
6690 | * cpu_bw_dl() | |
6691 | * | |
6692 | * Where the cfs,rt and dl util numbers are tracked with the same metric and | |
6693 | * synchronized windows and are thus directly comparable. | |
6694 | * | |
6695 | * The cfs,rt,dl utilization are the running times measured with rq->clock_task | |
6696 | * which excludes things like IRQ and steal-time. These latter are then accrued | |
6697 | * in the irq utilization. | |
6698 | * | |
6699 | * The DL bandwidth number otoh is not a measured metric but a value computed | |
6700 | * based on the task model parameters and gives the minimal utilization | |
6701 | * required to meet deadlines. | |
6702 | */ | |
a5418be9 VK |
6703 | unsigned long effective_cpu_util(int cpu, unsigned long util_cfs, |
6704 | unsigned long max, enum cpu_util_type type, | |
7d6a905f VK |
6705 | struct task_struct *p) |
6706 | { | |
6707 | unsigned long dl_util, util, irq; | |
6708 | struct rq *rq = cpu_rq(cpu); | |
6709 | ||
6710 | if (!uclamp_is_used() && | |
6711 | type == FREQUENCY_UTIL && rt_rq_is_runnable(&rq->rt)) { | |
6712 | return max; | |
6713 | } | |
6714 | ||
6715 | /* | |
6716 | * Early check to see if IRQ/steal time saturates the CPU, can be | |
6717 | * because of inaccuracies in how we track these -- see | |
6718 | * update_irq_load_avg(). | |
6719 | */ | |
6720 | irq = cpu_util_irq(rq); | |
6721 | if (unlikely(irq >= max)) | |
6722 | return max; | |
6723 | ||
6724 | /* | |
6725 | * Because the time spend on RT/DL tasks is visible as 'lost' time to | |
6726 | * CFS tasks and we use the same metric to track the effective | |
6727 | * utilization (PELT windows are synchronized) we can directly add them | |
6728 | * to obtain the CPU's actual utilization. | |
6729 | * | |
6730 | * CFS and RT utilization can be boosted or capped, depending on | |
6731 | * utilization clamp constraints requested by currently RUNNABLE | |
6732 | * tasks. | |
6733 | * When there are no CFS RUNNABLE tasks, clamps are released and | |
6734 | * frequency will be gracefully reduced with the utilization decay. | |
6735 | */ | |
6736 | util = util_cfs + cpu_util_rt(rq); | |
6737 | if (type == FREQUENCY_UTIL) | |
6738 | util = uclamp_rq_util_with(rq, util, p); | |
6739 | ||
6740 | dl_util = cpu_util_dl(rq); | |
6741 | ||
6742 | /* | |
6743 | * For frequency selection we do not make cpu_util_dl() a permanent part | |
6744 | * of this sum because we want to use cpu_bw_dl() later on, but we need | |
6745 | * to check if the CFS+RT+DL sum is saturated (ie. no idle time) such | |
6746 | * that we select f_max when there is no idle time. | |
6747 | * | |
6748 | * NOTE: numerical errors or stop class might cause us to not quite hit | |
6749 | * saturation when we should -- something for later. | |
6750 | */ | |
6751 | if (util + dl_util >= max) | |
6752 | return max; | |
6753 | ||
6754 | /* | |
6755 | * OTOH, for energy computation we need the estimated running time, so | |
6756 | * include util_dl and ignore dl_bw. | |
6757 | */ | |
6758 | if (type == ENERGY_UTIL) | |
6759 | util += dl_util; | |
6760 | ||
6761 | /* | |
6762 | * There is still idle time; further improve the number by using the | |
6763 | * irq metric. Because IRQ/steal time is hidden from the task clock we | |
6764 | * need to scale the task numbers: | |
6765 | * | |
6766 | * max - irq | |
6767 | * U' = irq + --------- * U | |
6768 | * max | |
6769 | */ | |
6770 | util = scale_irq_capacity(util, irq, max); | |
6771 | util += irq; | |
6772 | ||
6773 | /* | |
6774 | * Bandwidth required by DEADLINE must always be granted while, for | |
6775 | * FAIR and RT, we use blocked utilization of IDLE CPUs as a mechanism | |
6776 | * to gracefully reduce the frequency when no tasks show up for longer | |
6777 | * periods of time. | |
6778 | * | |
6779 | * Ideally we would like to set bw_dl as min/guaranteed freq and util + | |
6780 | * bw_dl as requested freq. However, cpufreq is not yet ready for such | |
6781 | * an interface. So, we only do the latter for now. | |
6782 | */ | |
6783 | if (type == FREQUENCY_UTIL) | |
6784 | util += cpu_bw_dl(rq); | |
6785 | ||
6786 | return min(max, util); | |
6787 | } | |
a5418be9 VK |
6788 | |
6789 | unsigned long sched_cpu_util(int cpu, unsigned long max) | |
6790 | { | |
6791 | return effective_cpu_util(cpu, cpu_util_cfs(cpu_rq(cpu)), max, | |
6792 | ENERGY_UTIL, NULL); | |
6793 | } | |
7d6a905f VK |
6794 | #endif /* CONFIG_SMP */ |
6795 | ||
1da177e4 LT |
6796 | /** |
6797 | * find_process_by_pid - find a process with a matching PID value. | |
6798 | * @pid: the pid in question. | |
e69f6186 YB |
6799 | * |
6800 | * The task of @pid, if found. %NULL otherwise. | |
1da177e4 | 6801 | */ |
a9957449 | 6802 | static struct task_struct *find_process_by_pid(pid_t pid) |
1da177e4 | 6803 | { |
228ebcbe | 6804 | return pid ? find_task_by_vpid(pid) : current; |
1da177e4 LT |
6805 | } |
6806 | ||
c13db6b1 SR |
6807 | /* |
6808 | * sched_setparam() passes in -1 for its policy, to let the functions | |
6809 | * it calls know not to change it. | |
6810 | */ | |
6811 | #define SETPARAM_POLICY -1 | |
6812 | ||
c365c292 TG |
6813 | static void __setscheduler_params(struct task_struct *p, |
6814 | const struct sched_attr *attr) | |
1da177e4 | 6815 | { |
d50dde5a DF |
6816 | int policy = attr->sched_policy; |
6817 | ||
c13db6b1 | 6818 | if (policy == SETPARAM_POLICY) |
39fd8fd2 PZ |
6819 | policy = p->policy; |
6820 | ||
1da177e4 | 6821 | p->policy = policy; |
d50dde5a | 6822 | |
aab03e05 DF |
6823 | if (dl_policy(policy)) |
6824 | __setparam_dl(p, attr); | |
39fd8fd2 | 6825 | else if (fair_policy(policy)) |
d50dde5a DF |
6826 | p->static_prio = NICE_TO_PRIO(attr->sched_nice); |
6827 | ||
39fd8fd2 PZ |
6828 | /* |
6829 | * __sched_setscheduler() ensures attr->sched_priority == 0 when | |
6830 | * !rt_policy. Always setting this ensures that things like | |
6831 | * getparam()/getattr() don't report silly values for !rt tasks. | |
6832 | */ | |
6833 | p->rt_priority = attr->sched_priority; | |
383afd09 | 6834 | p->normal_prio = normal_prio(p); |
9059393e | 6835 | set_load_weight(p, true); |
c365c292 | 6836 | } |
39fd8fd2 | 6837 | |
c365c292 TG |
6838 | /* Actually do priority change: must hold pi & rq lock. */ |
6839 | static void __setscheduler(struct rq *rq, struct task_struct *p, | |
0782e63b | 6840 | const struct sched_attr *attr, bool keep_boost) |
c365c292 | 6841 | { |
a509a7cd PB |
6842 | /* |
6843 | * If params can't change scheduling class changes aren't allowed | |
6844 | * either. | |
6845 | */ | |
6846 | if (attr->sched_flags & SCHED_FLAG_KEEP_PARAMS) | |
6847 | return; | |
6848 | ||
c365c292 | 6849 | __setscheduler_params(p, attr); |
d50dde5a | 6850 | |
383afd09 | 6851 | /* |
0782e63b TG |
6852 | * Keep a potential priority boosting if called from |
6853 | * sched_setscheduler(). | |
383afd09 | 6854 | */ |
acd58620 | 6855 | p->prio = normal_prio(p); |
0782e63b | 6856 | if (keep_boost) |
acd58620 | 6857 | p->prio = rt_effective_prio(p, p->prio); |
383afd09 | 6858 | |
aab03e05 DF |
6859 | if (dl_prio(p->prio)) |
6860 | p->sched_class = &dl_sched_class; | |
6861 | else if (rt_prio(p->prio)) | |
ffd44db5 PZ |
6862 | p->sched_class = &rt_sched_class; |
6863 | else | |
6864 | p->sched_class = &fair_sched_class; | |
1da177e4 | 6865 | } |
aab03e05 | 6866 | |
c69e8d9c | 6867 | /* |
d1ccc66d | 6868 | * Check the target process has a UID that matches the current process's: |
c69e8d9c DH |
6869 | */ |
6870 | static bool check_same_owner(struct task_struct *p) | |
6871 | { | |
6872 | const struct cred *cred = current_cred(), *pcred; | |
6873 | bool match; | |
6874 | ||
6875 | rcu_read_lock(); | |
6876 | pcred = __task_cred(p); | |
9c806aa0 EB |
6877 | match = (uid_eq(cred->euid, pcred->euid) || |
6878 | uid_eq(cred->euid, pcred->uid)); | |
c69e8d9c DH |
6879 | rcu_read_unlock(); |
6880 | return match; | |
6881 | } | |
6882 | ||
d50dde5a DF |
6883 | static int __sched_setscheduler(struct task_struct *p, |
6884 | const struct sched_attr *attr, | |
dbc7f069 | 6885 | bool user, bool pi) |
1da177e4 | 6886 | { |
383afd09 SR |
6887 | int newprio = dl_policy(attr->sched_policy) ? MAX_DL_PRIO - 1 : |
6888 | MAX_RT_PRIO - 1 - attr->sched_priority; | |
da0c1e65 | 6889 | int retval, oldprio, oldpolicy = -1, queued, running; |
0782e63b | 6890 | int new_effective_prio, policy = attr->sched_policy; |
83ab0aa0 | 6891 | const struct sched_class *prev_class; |
565790d2 | 6892 | struct callback_head *head; |
eb580751 | 6893 | struct rq_flags rf; |
ca94c442 | 6894 | int reset_on_fork; |
7a57f32a | 6895 | int queue_flags = DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK; |
eb580751 | 6896 | struct rq *rq; |
1da177e4 | 6897 | |
896bbb25 SRV |
6898 | /* The pi code expects interrupts enabled */ |
6899 | BUG_ON(pi && in_interrupt()); | |
1da177e4 | 6900 | recheck: |
d1ccc66d | 6901 | /* Double check policy once rq lock held: */ |
ca94c442 LP |
6902 | if (policy < 0) { |
6903 | reset_on_fork = p->sched_reset_on_fork; | |
1da177e4 | 6904 | policy = oldpolicy = p->policy; |
ca94c442 | 6905 | } else { |
7479f3c9 | 6906 | reset_on_fork = !!(attr->sched_flags & SCHED_FLAG_RESET_ON_FORK); |
ca94c442 | 6907 | |
20f9cd2a | 6908 | if (!valid_policy(policy)) |
ca94c442 LP |
6909 | return -EINVAL; |
6910 | } | |
6911 | ||
794a56eb | 6912 | if (attr->sched_flags & ~(SCHED_FLAG_ALL | SCHED_FLAG_SUGOV)) |
7479f3c9 PZ |
6913 | return -EINVAL; |
6914 | ||
1da177e4 LT |
6915 | /* |
6916 | * Valid priorities for SCHED_FIFO and SCHED_RR are | |
ae18ad28 | 6917 | * 1..MAX_RT_PRIO-1, valid priority for SCHED_NORMAL, |
dd41f596 | 6918 | * SCHED_BATCH and SCHED_IDLE is 0. |
1da177e4 | 6919 | */ |
ae18ad28 | 6920 | if (attr->sched_priority > MAX_RT_PRIO-1) |
1da177e4 | 6921 | return -EINVAL; |
aab03e05 DF |
6922 | if ((dl_policy(policy) && !__checkparam_dl(attr)) || |
6923 | (rt_policy(policy) != (attr->sched_priority != 0))) | |
1da177e4 LT |
6924 | return -EINVAL; |
6925 | ||
37e4ab3f OC |
6926 | /* |
6927 | * Allow unprivileged RT tasks to decrease priority: | |
6928 | */ | |
961ccddd | 6929 | if (user && !capable(CAP_SYS_NICE)) { |
d50dde5a | 6930 | if (fair_policy(policy)) { |
d0ea0268 | 6931 | if (attr->sched_nice < task_nice(p) && |
eaad4513 | 6932 | !can_nice(p, attr->sched_nice)) |
d50dde5a DF |
6933 | return -EPERM; |
6934 | } | |
6935 | ||
e05606d3 | 6936 | if (rt_policy(policy)) { |
a44702e8 ON |
6937 | unsigned long rlim_rtprio = |
6938 | task_rlimit(p, RLIMIT_RTPRIO); | |
8dc3e909 | 6939 | |
d1ccc66d | 6940 | /* Can't set/change the rt policy: */ |
8dc3e909 ON |
6941 | if (policy != p->policy && !rlim_rtprio) |
6942 | return -EPERM; | |
6943 | ||
d1ccc66d | 6944 | /* Can't increase priority: */ |
d50dde5a DF |
6945 | if (attr->sched_priority > p->rt_priority && |
6946 | attr->sched_priority > rlim_rtprio) | |
8dc3e909 ON |
6947 | return -EPERM; |
6948 | } | |
c02aa73b | 6949 | |
d44753b8 JL |
6950 | /* |
6951 | * Can't set/change SCHED_DEADLINE policy at all for now | |
6952 | * (safest behavior); in the future we would like to allow | |
6953 | * unprivileged DL tasks to increase their relative deadline | |
6954 | * or reduce their runtime (both ways reducing utilization) | |
6955 | */ | |
6956 | if (dl_policy(policy)) | |
6957 | return -EPERM; | |
6958 | ||
dd41f596 | 6959 | /* |
c02aa73b DH |
6960 | * Treat SCHED_IDLE as nice 20. Only allow a switch to |
6961 | * SCHED_NORMAL if the RLIMIT_NICE would normally permit it. | |
dd41f596 | 6962 | */ |
1da1843f | 6963 | if (task_has_idle_policy(p) && !idle_policy(policy)) { |
d0ea0268 | 6964 | if (!can_nice(p, task_nice(p))) |
c02aa73b DH |
6965 | return -EPERM; |
6966 | } | |
5fe1d75f | 6967 | |
d1ccc66d | 6968 | /* Can't change other user's priorities: */ |
c69e8d9c | 6969 | if (!check_same_owner(p)) |
37e4ab3f | 6970 | return -EPERM; |
ca94c442 | 6971 | |
d1ccc66d | 6972 | /* Normal users shall not reset the sched_reset_on_fork flag: */ |
ca94c442 LP |
6973 | if (p->sched_reset_on_fork && !reset_on_fork) |
6974 | return -EPERM; | |
37e4ab3f | 6975 | } |
1da177e4 | 6976 | |
725aad24 | 6977 | if (user) { |
794a56eb JL |
6978 | if (attr->sched_flags & SCHED_FLAG_SUGOV) |
6979 | return -EINVAL; | |
6980 | ||
b0ae1981 | 6981 | retval = security_task_setscheduler(p); |
725aad24 JF |
6982 | if (retval) |
6983 | return retval; | |
6984 | } | |
6985 | ||
a509a7cd PB |
6986 | /* Update task specific "requested" clamps */ |
6987 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) { | |
6988 | retval = uclamp_validate(p, attr); | |
6989 | if (retval) | |
6990 | return retval; | |
6991 | } | |
6992 | ||
710da3c8 JL |
6993 | if (pi) |
6994 | cpuset_read_lock(); | |
6995 | ||
b29739f9 | 6996 | /* |
d1ccc66d | 6997 | * Make sure no PI-waiters arrive (or leave) while we are |
b29739f9 | 6998 | * changing the priority of the task: |
0122ec5b | 6999 | * |
25985edc | 7000 | * To be able to change p->policy safely, the appropriate |
1da177e4 LT |
7001 | * runqueue lock must be held. |
7002 | */ | |
eb580751 | 7003 | rq = task_rq_lock(p, &rf); |
80f5c1b8 | 7004 | update_rq_clock(rq); |
dc61b1d6 | 7005 | |
34f971f6 | 7006 | /* |
d1ccc66d | 7007 | * Changing the policy of the stop threads its a very bad idea: |
34f971f6 PZ |
7008 | */ |
7009 | if (p == rq->stop) { | |
4b211f2b MP |
7010 | retval = -EINVAL; |
7011 | goto unlock; | |
34f971f6 PZ |
7012 | } |
7013 | ||
a51e9198 | 7014 | /* |
d6b1e911 TG |
7015 | * If not changing anything there's no need to proceed further, |
7016 | * but store a possible modification of reset_on_fork. | |
a51e9198 | 7017 | */ |
d50dde5a | 7018 | if (unlikely(policy == p->policy)) { |
d0ea0268 | 7019 | if (fair_policy(policy) && attr->sched_nice != task_nice(p)) |
d50dde5a DF |
7020 | goto change; |
7021 | if (rt_policy(policy) && attr->sched_priority != p->rt_priority) | |
7022 | goto change; | |
75381608 | 7023 | if (dl_policy(policy) && dl_param_changed(p, attr)) |
aab03e05 | 7024 | goto change; |
a509a7cd PB |
7025 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) |
7026 | goto change; | |
d50dde5a | 7027 | |
d6b1e911 | 7028 | p->sched_reset_on_fork = reset_on_fork; |
4b211f2b MP |
7029 | retval = 0; |
7030 | goto unlock; | |
a51e9198 | 7031 | } |
d50dde5a | 7032 | change: |
a51e9198 | 7033 | |
dc61b1d6 | 7034 | if (user) { |
332ac17e | 7035 | #ifdef CONFIG_RT_GROUP_SCHED |
dc61b1d6 PZ |
7036 | /* |
7037 | * Do not allow realtime tasks into groups that have no runtime | |
7038 | * assigned. | |
7039 | */ | |
7040 | if (rt_bandwidth_enabled() && rt_policy(policy) && | |
f4493771 MG |
7041 | task_group(p)->rt_bandwidth.rt_runtime == 0 && |
7042 | !task_group_is_autogroup(task_group(p))) { | |
4b211f2b MP |
7043 | retval = -EPERM; |
7044 | goto unlock; | |
dc61b1d6 | 7045 | } |
dc61b1d6 | 7046 | #endif |
332ac17e | 7047 | #ifdef CONFIG_SMP |
794a56eb JL |
7048 | if (dl_bandwidth_enabled() && dl_policy(policy) && |
7049 | !(attr->sched_flags & SCHED_FLAG_SUGOV)) { | |
332ac17e | 7050 | cpumask_t *span = rq->rd->span; |
332ac17e DF |
7051 | |
7052 | /* | |
7053 | * Don't allow tasks with an affinity mask smaller than | |
7054 | * the entire root_domain to become SCHED_DEADLINE. We | |
7055 | * will also fail if there's no bandwidth available. | |
7056 | */ | |
3bd37062 | 7057 | if (!cpumask_subset(span, p->cpus_ptr) || |
e4099a5e | 7058 | rq->rd->dl_bw.bw == 0) { |
4b211f2b MP |
7059 | retval = -EPERM; |
7060 | goto unlock; | |
332ac17e DF |
7061 | } |
7062 | } | |
7063 | #endif | |
7064 | } | |
dc61b1d6 | 7065 | |
d1ccc66d | 7066 | /* Re-check policy now with rq lock held: */ |
1da177e4 LT |
7067 | if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { |
7068 | policy = oldpolicy = -1; | |
eb580751 | 7069 | task_rq_unlock(rq, p, &rf); |
710da3c8 JL |
7070 | if (pi) |
7071 | cpuset_read_unlock(); | |
1da177e4 LT |
7072 | goto recheck; |
7073 | } | |
332ac17e DF |
7074 | |
7075 | /* | |
7076 | * If setscheduling to SCHED_DEADLINE (or changing the parameters | |
7077 | * of a SCHED_DEADLINE task) we need to check if enough bandwidth | |
7078 | * is available. | |
7079 | */ | |
06a76fe0 | 7080 | if ((dl_policy(policy) || dl_task(p)) && sched_dl_overflow(p, policy, attr)) { |
4b211f2b MP |
7081 | retval = -EBUSY; |
7082 | goto unlock; | |
332ac17e DF |
7083 | } |
7084 | ||
c365c292 TG |
7085 | p->sched_reset_on_fork = reset_on_fork; |
7086 | oldprio = p->prio; | |
7087 | ||
dbc7f069 PZ |
7088 | if (pi) { |
7089 | /* | |
7090 | * Take priority boosted tasks into account. If the new | |
7091 | * effective priority is unchanged, we just store the new | |
7092 | * normal parameters and do not touch the scheduler class and | |
7093 | * the runqueue. This will be done when the task deboost | |
7094 | * itself. | |
7095 | */ | |
acd58620 | 7096 | new_effective_prio = rt_effective_prio(p, newprio); |
ff77e468 PZ |
7097 | if (new_effective_prio == oldprio) |
7098 | queue_flags &= ~DEQUEUE_MOVE; | |
c365c292 TG |
7099 | } |
7100 | ||
da0c1e65 | 7101 | queued = task_on_rq_queued(p); |
051a1d1a | 7102 | running = task_current(rq, p); |
da0c1e65 | 7103 | if (queued) |
ff77e468 | 7104 | dequeue_task(rq, p, queue_flags); |
0e1f3483 | 7105 | if (running) |
f3cd1c4e | 7106 | put_prev_task(rq, p); |
f6b53205 | 7107 | |
83ab0aa0 | 7108 | prev_class = p->sched_class; |
a509a7cd | 7109 | |
dbc7f069 | 7110 | __setscheduler(rq, p, attr, pi); |
a509a7cd | 7111 | __setscheduler_uclamp(p, attr); |
f6b53205 | 7112 | |
da0c1e65 | 7113 | if (queued) { |
81a44c54 TG |
7114 | /* |
7115 | * We enqueue to tail when the priority of a task is | |
7116 | * increased (user space view). | |
7117 | */ | |
ff77e468 PZ |
7118 | if (oldprio < p->prio) |
7119 | queue_flags |= ENQUEUE_HEAD; | |
1de64443 | 7120 | |
ff77e468 | 7121 | enqueue_task(rq, p, queue_flags); |
81a44c54 | 7122 | } |
a399d233 | 7123 | if (running) |
03b7fad1 | 7124 | set_next_task(rq, p); |
cb469845 | 7125 | |
da7a735e | 7126 | check_class_changed(rq, p, prev_class, oldprio); |
d1ccc66d IM |
7127 | |
7128 | /* Avoid rq from going away on us: */ | |
7129 | preempt_disable(); | |
565790d2 | 7130 | head = splice_balance_callbacks(rq); |
eb580751 | 7131 | task_rq_unlock(rq, p, &rf); |
b29739f9 | 7132 | |
710da3c8 JL |
7133 | if (pi) { |
7134 | cpuset_read_unlock(); | |
dbc7f069 | 7135 | rt_mutex_adjust_pi(p); |
710da3c8 | 7136 | } |
95e02ca9 | 7137 | |
d1ccc66d | 7138 | /* Run balance callbacks after we've adjusted the PI chain: */ |
565790d2 | 7139 | balance_callbacks(rq, head); |
4c9a4bc8 | 7140 | preempt_enable(); |
95e02ca9 | 7141 | |
1da177e4 | 7142 | return 0; |
4b211f2b MP |
7143 | |
7144 | unlock: | |
7145 | task_rq_unlock(rq, p, &rf); | |
710da3c8 JL |
7146 | if (pi) |
7147 | cpuset_read_unlock(); | |
4b211f2b | 7148 | return retval; |
1da177e4 | 7149 | } |
961ccddd | 7150 | |
7479f3c9 PZ |
7151 | static int _sched_setscheduler(struct task_struct *p, int policy, |
7152 | const struct sched_param *param, bool check) | |
7153 | { | |
7154 | struct sched_attr attr = { | |
7155 | .sched_policy = policy, | |
7156 | .sched_priority = param->sched_priority, | |
7157 | .sched_nice = PRIO_TO_NICE(p->static_prio), | |
7158 | }; | |
7159 | ||
c13db6b1 SR |
7160 | /* Fixup the legacy SCHED_RESET_ON_FORK hack. */ |
7161 | if ((policy != SETPARAM_POLICY) && (policy & SCHED_RESET_ON_FORK)) { | |
7479f3c9 PZ |
7162 | attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK; |
7163 | policy &= ~SCHED_RESET_ON_FORK; | |
7164 | attr.sched_policy = policy; | |
7165 | } | |
7166 | ||
dbc7f069 | 7167 | return __sched_setscheduler(p, &attr, check, true); |
7479f3c9 | 7168 | } |
961ccddd RR |
7169 | /** |
7170 | * sched_setscheduler - change the scheduling policy and/or RT priority of a thread. | |
7171 | * @p: the task in question. | |
7172 | * @policy: new policy. | |
7173 | * @param: structure containing the new RT priority. | |
7174 | * | |
7318d4cc PZ |
7175 | * Use sched_set_fifo(), read its comment. |
7176 | * | |
e69f6186 YB |
7177 | * Return: 0 on success. An error code otherwise. |
7178 | * | |
961ccddd RR |
7179 | * NOTE that the task may be already dead. |
7180 | */ | |
7181 | int sched_setscheduler(struct task_struct *p, int policy, | |
fe7de49f | 7182 | const struct sched_param *param) |
961ccddd | 7183 | { |
7479f3c9 | 7184 | return _sched_setscheduler(p, policy, param, true); |
961ccddd | 7185 | } |
1da177e4 | 7186 | |
d50dde5a DF |
7187 | int sched_setattr(struct task_struct *p, const struct sched_attr *attr) |
7188 | { | |
dbc7f069 | 7189 | return __sched_setscheduler(p, attr, true, true); |
d50dde5a | 7190 | } |
d50dde5a | 7191 | |
794a56eb JL |
7192 | int sched_setattr_nocheck(struct task_struct *p, const struct sched_attr *attr) |
7193 | { | |
7194 | return __sched_setscheduler(p, attr, false, true); | |
7195 | } | |
4c38f2df | 7196 | EXPORT_SYMBOL_GPL(sched_setattr_nocheck); |
794a56eb | 7197 | |
961ccddd RR |
7198 | /** |
7199 | * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace. | |
7200 | * @p: the task in question. | |
7201 | * @policy: new policy. | |
7202 | * @param: structure containing the new RT priority. | |
7203 | * | |
7204 | * Just like sched_setscheduler, only don't bother checking if the | |
7205 | * current context has permission. For example, this is needed in | |
7206 | * stop_machine(): we create temporary high priority worker threads, | |
7207 | * but our caller might not have that capability. | |
e69f6186 YB |
7208 | * |
7209 | * Return: 0 on success. An error code otherwise. | |
961ccddd RR |
7210 | */ |
7211 | int sched_setscheduler_nocheck(struct task_struct *p, int policy, | |
fe7de49f | 7212 | const struct sched_param *param) |
961ccddd | 7213 | { |
7479f3c9 | 7214 | return _sched_setscheduler(p, policy, param, false); |
961ccddd RR |
7215 | } |
7216 | ||
7318d4cc PZ |
7217 | /* |
7218 | * SCHED_FIFO is a broken scheduler model; that is, it is fundamentally | |
7219 | * incapable of resource management, which is the one thing an OS really should | |
7220 | * be doing. | |
7221 | * | |
7222 | * This is of course the reason it is limited to privileged users only. | |
7223 | * | |
7224 | * Worse still; it is fundamentally impossible to compose static priority | |
7225 | * workloads. You cannot take two correctly working static prio workloads | |
7226 | * and smash them together and still expect them to work. | |
7227 | * | |
7228 | * For this reason 'all' FIFO tasks the kernel creates are basically at: | |
7229 | * | |
7230 | * MAX_RT_PRIO / 2 | |
7231 | * | |
7232 | * The administrator _MUST_ configure the system, the kernel simply doesn't | |
7233 | * know enough information to make a sensible choice. | |
7234 | */ | |
8b700983 | 7235 | void sched_set_fifo(struct task_struct *p) |
7318d4cc PZ |
7236 | { |
7237 | struct sched_param sp = { .sched_priority = MAX_RT_PRIO / 2 }; | |
8b700983 | 7238 | WARN_ON_ONCE(sched_setscheduler_nocheck(p, SCHED_FIFO, &sp) != 0); |
7318d4cc PZ |
7239 | } |
7240 | EXPORT_SYMBOL_GPL(sched_set_fifo); | |
7241 | ||
7242 | /* | |
7243 | * For when you don't much care about FIFO, but want to be above SCHED_NORMAL. | |
7244 | */ | |
8b700983 | 7245 | void sched_set_fifo_low(struct task_struct *p) |
7318d4cc PZ |
7246 | { |
7247 | struct sched_param sp = { .sched_priority = 1 }; | |
8b700983 | 7248 | WARN_ON_ONCE(sched_setscheduler_nocheck(p, SCHED_FIFO, &sp) != 0); |
7318d4cc PZ |
7249 | } |
7250 | EXPORT_SYMBOL_GPL(sched_set_fifo_low); | |
7251 | ||
8b700983 | 7252 | void sched_set_normal(struct task_struct *p, int nice) |
7318d4cc PZ |
7253 | { |
7254 | struct sched_attr attr = { | |
7255 | .sched_policy = SCHED_NORMAL, | |
7256 | .sched_nice = nice, | |
7257 | }; | |
8b700983 | 7258 | WARN_ON_ONCE(sched_setattr_nocheck(p, &attr) != 0); |
7318d4cc PZ |
7259 | } |
7260 | EXPORT_SYMBOL_GPL(sched_set_normal); | |
961ccddd | 7261 | |
95cdf3b7 IM |
7262 | static int |
7263 | do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param) | |
1da177e4 | 7264 | { |
1da177e4 LT |
7265 | struct sched_param lparam; |
7266 | struct task_struct *p; | |
36c8b586 | 7267 | int retval; |
1da177e4 LT |
7268 | |
7269 | if (!param || pid < 0) | |
7270 | return -EINVAL; | |
7271 | if (copy_from_user(&lparam, param, sizeof(struct sched_param))) | |
7272 | return -EFAULT; | |
5fe1d75f ON |
7273 | |
7274 | rcu_read_lock(); | |
7275 | retval = -ESRCH; | |
1da177e4 | 7276 | p = find_process_by_pid(pid); |
710da3c8 JL |
7277 | if (likely(p)) |
7278 | get_task_struct(p); | |
5fe1d75f | 7279 | rcu_read_unlock(); |
36c8b586 | 7280 | |
710da3c8 JL |
7281 | if (likely(p)) { |
7282 | retval = sched_setscheduler(p, policy, &lparam); | |
7283 | put_task_struct(p); | |
7284 | } | |
7285 | ||
1da177e4 LT |
7286 | return retval; |
7287 | } | |
7288 | ||
d50dde5a DF |
7289 | /* |
7290 | * Mimics kernel/events/core.c perf_copy_attr(). | |
7291 | */ | |
d1ccc66d | 7292 | static int sched_copy_attr(struct sched_attr __user *uattr, struct sched_attr *attr) |
d50dde5a DF |
7293 | { |
7294 | u32 size; | |
7295 | int ret; | |
7296 | ||
d1ccc66d | 7297 | /* Zero the full structure, so that a short copy will be nice: */ |
d50dde5a DF |
7298 | memset(attr, 0, sizeof(*attr)); |
7299 | ||
7300 | ret = get_user(size, &uattr->size); | |
7301 | if (ret) | |
7302 | return ret; | |
7303 | ||
d1ccc66d IM |
7304 | /* ABI compatibility quirk: */ |
7305 | if (!size) | |
d50dde5a | 7306 | size = SCHED_ATTR_SIZE_VER0; |
dff3a85f | 7307 | if (size < SCHED_ATTR_SIZE_VER0 || size > PAGE_SIZE) |
d50dde5a DF |
7308 | goto err_size; |
7309 | ||
dff3a85f AS |
7310 | ret = copy_struct_from_user(attr, sizeof(*attr), uattr, size); |
7311 | if (ret) { | |
7312 | if (ret == -E2BIG) | |
7313 | goto err_size; | |
7314 | return ret; | |
d50dde5a DF |
7315 | } |
7316 | ||
a509a7cd PB |
7317 | if ((attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) && |
7318 | size < SCHED_ATTR_SIZE_VER1) | |
7319 | return -EINVAL; | |
7320 | ||
d50dde5a | 7321 | /* |
d1ccc66d | 7322 | * XXX: Do we want to be lenient like existing syscalls; or do we want |
d50dde5a DF |
7323 | * to be strict and return an error on out-of-bounds values? |
7324 | */ | |
75e45d51 | 7325 | attr->sched_nice = clamp(attr->sched_nice, MIN_NICE, MAX_NICE); |
d50dde5a | 7326 | |
e78c7bca | 7327 | return 0; |
d50dde5a DF |
7328 | |
7329 | err_size: | |
7330 | put_user(sizeof(*attr), &uattr->size); | |
e78c7bca | 7331 | return -E2BIG; |
d50dde5a DF |
7332 | } |
7333 | ||
f4dddf90 QP |
7334 | static void get_params(struct task_struct *p, struct sched_attr *attr) |
7335 | { | |
7336 | if (task_has_dl_policy(p)) | |
7337 | __getparam_dl(p, attr); | |
7338 | else if (task_has_rt_policy(p)) | |
7339 | attr->sched_priority = p->rt_priority; | |
7340 | else | |
7341 | attr->sched_nice = task_nice(p); | |
7342 | } | |
7343 | ||
1da177e4 LT |
7344 | /** |
7345 | * sys_sched_setscheduler - set/change the scheduler policy and RT priority | |
7346 | * @pid: the pid in question. | |
7347 | * @policy: new policy. | |
7348 | * @param: structure containing the new RT priority. | |
e69f6186 YB |
7349 | * |
7350 | * Return: 0 on success. An error code otherwise. | |
1da177e4 | 7351 | */ |
d1ccc66d | 7352 | SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy, struct sched_param __user *, param) |
1da177e4 | 7353 | { |
c21761f1 JB |
7354 | if (policy < 0) |
7355 | return -EINVAL; | |
7356 | ||
1da177e4 LT |
7357 | return do_sched_setscheduler(pid, policy, param); |
7358 | } | |
7359 | ||
7360 | /** | |
7361 | * sys_sched_setparam - set/change the RT priority of a thread | |
7362 | * @pid: the pid in question. | |
7363 | * @param: structure containing the new RT priority. | |
e69f6186 YB |
7364 | * |
7365 | * Return: 0 on success. An error code otherwise. | |
1da177e4 | 7366 | */ |
5add95d4 | 7367 | SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param) |
1da177e4 | 7368 | { |
c13db6b1 | 7369 | return do_sched_setscheduler(pid, SETPARAM_POLICY, param); |
1da177e4 LT |
7370 | } |
7371 | ||
d50dde5a DF |
7372 | /** |
7373 | * sys_sched_setattr - same as above, but with extended sched_attr | |
7374 | * @pid: the pid in question. | |
5778fccf | 7375 | * @uattr: structure containing the extended parameters. |
db66d756 | 7376 | * @flags: for future extension. |
d50dde5a | 7377 | */ |
6d35ab48 PZ |
7378 | SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr, |
7379 | unsigned int, flags) | |
d50dde5a DF |
7380 | { |
7381 | struct sched_attr attr; | |
7382 | struct task_struct *p; | |
7383 | int retval; | |
7384 | ||
6d35ab48 | 7385 | if (!uattr || pid < 0 || flags) |
d50dde5a DF |
7386 | return -EINVAL; |
7387 | ||
143cf23d MK |
7388 | retval = sched_copy_attr(uattr, &attr); |
7389 | if (retval) | |
7390 | return retval; | |
d50dde5a | 7391 | |
b14ed2c2 | 7392 | if ((int)attr.sched_policy < 0) |
dbdb2275 | 7393 | return -EINVAL; |
1d6362fa PB |
7394 | if (attr.sched_flags & SCHED_FLAG_KEEP_POLICY) |
7395 | attr.sched_policy = SETPARAM_POLICY; | |
d50dde5a DF |
7396 | |
7397 | rcu_read_lock(); | |
7398 | retval = -ESRCH; | |
7399 | p = find_process_by_pid(pid); | |
a509a7cd PB |
7400 | if (likely(p)) |
7401 | get_task_struct(p); | |
d50dde5a DF |
7402 | rcu_read_unlock(); |
7403 | ||
a509a7cd | 7404 | if (likely(p)) { |
f4dddf90 QP |
7405 | if (attr.sched_flags & SCHED_FLAG_KEEP_PARAMS) |
7406 | get_params(p, &attr); | |
a509a7cd PB |
7407 | retval = sched_setattr(p, &attr); |
7408 | put_task_struct(p); | |
7409 | } | |
7410 | ||
d50dde5a DF |
7411 | return retval; |
7412 | } | |
7413 | ||
1da177e4 LT |
7414 | /** |
7415 | * sys_sched_getscheduler - get the policy (scheduling class) of a thread | |
7416 | * @pid: the pid in question. | |
e69f6186 YB |
7417 | * |
7418 | * Return: On success, the policy of the thread. Otherwise, a negative error | |
7419 | * code. | |
1da177e4 | 7420 | */ |
5add95d4 | 7421 | SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid) |
1da177e4 | 7422 | { |
36c8b586 | 7423 | struct task_struct *p; |
3a5c359a | 7424 | int retval; |
1da177e4 LT |
7425 | |
7426 | if (pid < 0) | |
3a5c359a | 7427 | return -EINVAL; |
1da177e4 LT |
7428 | |
7429 | retval = -ESRCH; | |
5fe85be0 | 7430 | rcu_read_lock(); |
1da177e4 LT |
7431 | p = find_process_by_pid(pid); |
7432 | if (p) { | |
7433 | retval = security_task_getscheduler(p); | |
7434 | if (!retval) | |
ca94c442 LP |
7435 | retval = p->policy |
7436 | | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0); | |
1da177e4 | 7437 | } |
5fe85be0 | 7438 | rcu_read_unlock(); |
1da177e4 LT |
7439 | return retval; |
7440 | } | |
7441 | ||
7442 | /** | |
ca94c442 | 7443 | * sys_sched_getparam - get the RT priority of a thread |
1da177e4 LT |
7444 | * @pid: the pid in question. |
7445 | * @param: structure containing the RT priority. | |
e69f6186 YB |
7446 | * |
7447 | * Return: On success, 0 and the RT priority is in @param. Otherwise, an error | |
7448 | * code. | |
1da177e4 | 7449 | */ |
5add95d4 | 7450 | SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param) |
1da177e4 | 7451 | { |
ce5f7f82 | 7452 | struct sched_param lp = { .sched_priority = 0 }; |
36c8b586 | 7453 | struct task_struct *p; |
3a5c359a | 7454 | int retval; |
1da177e4 LT |
7455 | |
7456 | if (!param || pid < 0) | |
3a5c359a | 7457 | return -EINVAL; |
1da177e4 | 7458 | |
5fe85be0 | 7459 | rcu_read_lock(); |
1da177e4 LT |
7460 | p = find_process_by_pid(pid); |
7461 | retval = -ESRCH; | |
7462 | if (!p) | |
7463 | goto out_unlock; | |
7464 | ||
7465 | retval = security_task_getscheduler(p); | |
7466 | if (retval) | |
7467 | goto out_unlock; | |
7468 | ||
ce5f7f82 PZ |
7469 | if (task_has_rt_policy(p)) |
7470 | lp.sched_priority = p->rt_priority; | |
5fe85be0 | 7471 | rcu_read_unlock(); |
1da177e4 LT |
7472 | |
7473 | /* | |
7474 | * This one might sleep, we cannot do it with a spinlock held ... | |
7475 | */ | |
7476 | retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0; | |
7477 | ||
1da177e4 LT |
7478 | return retval; |
7479 | ||
7480 | out_unlock: | |
5fe85be0 | 7481 | rcu_read_unlock(); |
1da177e4 LT |
7482 | return retval; |
7483 | } | |
7484 | ||
1251201c IM |
7485 | /* |
7486 | * Copy the kernel size attribute structure (which might be larger | |
7487 | * than what user-space knows about) to user-space. | |
7488 | * | |
7489 | * Note that all cases are valid: user-space buffer can be larger or | |
7490 | * smaller than the kernel-space buffer. The usual case is that both | |
7491 | * have the same size. | |
7492 | */ | |
7493 | static int | |
7494 | sched_attr_copy_to_user(struct sched_attr __user *uattr, | |
7495 | struct sched_attr *kattr, | |
7496 | unsigned int usize) | |
d50dde5a | 7497 | { |
1251201c | 7498 | unsigned int ksize = sizeof(*kattr); |
d50dde5a | 7499 | |
96d4f267 | 7500 | if (!access_ok(uattr, usize)) |
d50dde5a DF |
7501 | return -EFAULT; |
7502 | ||
7503 | /* | |
1251201c IM |
7504 | * sched_getattr() ABI forwards and backwards compatibility: |
7505 | * | |
7506 | * If usize == ksize then we just copy everything to user-space and all is good. | |
7507 | * | |
7508 | * If usize < ksize then we only copy as much as user-space has space for, | |
7509 | * this keeps ABI compatibility as well. We skip the rest. | |
7510 | * | |
7511 | * If usize > ksize then user-space is using a newer version of the ABI, | |
7512 | * which part the kernel doesn't know about. Just ignore it - tooling can | |
7513 | * detect the kernel's knowledge of attributes from the attr->size value | |
7514 | * which is set to ksize in this case. | |
d50dde5a | 7515 | */ |
1251201c | 7516 | kattr->size = min(usize, ksize); |
d50dde5a | 7517 | |
1251201c | 7518 | if (copy_to_user(uattr, kattr, kattr->size)) |
d50dde5a DF |
7519 | return -EFAULT; |
7520 | ||
22400674 | 7521 | return 0; |
d50dde5a DF |
7522 | } |
7523 | ||
7524 | /** | |
aab03e05 | 7525 | * sys_sched_getattr - similar to sched_getparam, but with sched_attr |
d50dde5a | 7526 | * @pid: the pid in question. |
5778fccf | 7527 | * @uattr: structure containing the extended parameters. |
dff3a85f | 7528 | * @usize: sizeof(attr) for fwd/bwd comp. |
db66d756 | 7529 | * @flags: for future extension. |
d50dde5a | 7530 | */ |
6d35ab48 | 7531 | SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr, |
1251201c | 7532 | unsigned int, usize, unsigned int, flags) |
d50dde5a | 7533 | { |
1251201c | 7534 | struct sched_attr kattr = { }; |
d50dde5a DF |
7535 | struct task_struct *p; |
7536 | int retval; | |
7537 | ||
1251201c IM |
7538 | if (!uattr || pid < 0 || usize > PAGE_SIZE || |
7539 | usize < SCHED_ATTR_SIZE_VER0 || flags) | |
d50dde5a DF |
7540 | return -EINVAL; |
7541 | ||
7542 | rcu_read_lock(); | |
7543 | p = find_process_by_pid(pid); | |
7544 | retval = -ESRCH; | |
7545 | if (!p) | |
7546 | goto out_unlock; | |
7547 | ||
7548 | retval = security_task_getscheduler(p); | |
7549 | if (retval) | |
7550 | goto out_unlock; | |
7551 | ||
1251201c | 7552 | kattr.sched_policy = p->policy; |
7479f3c9 | 7553 | if (p->sched_reset_on_fork) |
1251201c | 7554 | kattr.sched_flags |= SCHED_FLAG_RESET_ON_FORK; |
f4dddf90 | 7555 | get_params(p, &kattr); |
7ad721bf | 7556 | kattr.sched_flags &= SCHED_FLAG_ALL; |
d50dde5a | 7557 | |
a509a7cd | 7558 | #ifdef CONFIG_UCLAMP_TASK |
13685c4a QY |
7559 | /* |
7560 | * This could race with another potential updater, but this is fine | |
7561 | * because it'll correctly read the old or the new value. We don't need | |
7562 | * to guarantee who wins the race as long as it doesn't return garbage. | |
7563 | */ | |
1251201c IM |
7564 | kattr.sched_util_min = p->uclamp_req[UCLAMP_MIN].value; |
7565 | kattr.sched_util_max = p->uclamp_req[UCLAMP_MAX].value; | |
a509a7cd PB |
7566 | #endif |
7567 | ||
d50dde5a DF |
7568 | rcu_read_unlock(); |
7569 | ||
1251201c | 7570 | return sched_attr_copy_to_user(uattr, &kattr, usize); |
d50dde5a DF |
7571 | |
7572 | out_unlock: | |
7573 | rcu_read_unlock(); | |
7574 | return retval; | |
7575 | } | |
7576 | ||
96f874e2 | 7577 | long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) |
1da177e4 | 7578 | { |
5a16f3d3 | 7579 | cpumask_var_t cpus_allowed, new_mask; |
36c8b586 IM |
7580 | struct task_struct *p; |
7581 | int retval; | |
1da177e4 | 7582 | |
23f5d142 | 7583 | rcu_read_lock(); |
1da177e4 LT |
7584 | |
7585 | p = find_process_by_pid(pid); | |
7586 | if (!p) { | |
23f5d142 | 7587 | rcu_read_unlock(); |
1da177e4 LT |
7588 | return -ESRCH; |
7589 | } | |
7590 | ||
23f5d142 | 7591 | /* Prevent p going away */ |
1da177e4 | 7592 | get_task_struct(p); |
23f5d142 | 7593 | rcu_read_unlock(); |
1da177e4 | 7594 | |
14a40ffc TH |
7595 | if (p->flags & PF_NO_SETAFFINITY) { |
7596 | retval = -EINVAL; | |
7597 | goto out_put_task; | |
7598 | } | |
5a16f3d3 RR |
7599 | if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) { |
7600 | retval = -ENOMEM; | |
7601 | goto out_put_task; | |
7602 | } | |
7603 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) { | |
7604 | retval = -ENOMEM; | |
7605 | goto out_free_cpus_allowed; | |
7606 | } | |
1da177e4 | 7607 | retval = -EPERM; |
4c44aaaf EB |
7608 | if (!check_same_owner(p)) { |
7609 | rcu_read_lock(); | |
7610 | if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) { | |
7611 | rcu_read_unlock(); | |
16303ab2 | 7612 | goto out_free_new_mask; |
4c44aaaf EB |
7613 | } |
7614 | rcu_read_unlock(); | |
7615 | } | |
1da177e4 | 7616 | |
b0ae1981 | 7617 | retval = security_task_setscheduler(p); |
e7834f8f | 7618 | if (retval) |
16303ab2 | 7619 | goto out_free_new_mask; |
e7834f8f | 7620 | |
e4099a5e PZ |
7621 | |
7622 | cpuset_cpus_allowed(p, cpus_allowed); | |
7623 | cpumask_and(new_mask, in_mask, cpus_allowed); | |
7624 | ||
332ac17e DF |
7625 | /* |
7626 | * Since bandwidth control happens on root_domain basis, | |
7627 | * if admission test is enabled, we only admit -deadline | |
7628 | * tasks allowed to run on all the CPUs in the task's | |
7629 | * root_domain. | |
7630 | */ | |
7631 | #ifdef CONFIG_SMP | |
f1e3a093 KT |
7632 | if (task_has_dl_policy(p) && dl_bandwidth_enabled()) { |
7633 | rcu_read_lock(); | |
7634 | if (!cpumask_subset(task_rq(p)->rd->span, new_mask)) { | |
332ac17e | 7635 | retval = -EBUSY; |
f1e3a093 | 7636 | rcu_read_unlock(); |
16303ab2 | 7637 | goto out_free_new_mask; |
332ac17e | 7638 | } |
f1e3a093 | 7639 | rcu_read_unlock(); |
332ac17e DF |
7640 | } |
7641 | #endif | |
49246274 | 7642 | again: |
9cfc3e18 | 7643 | retval = __set_cpus_allowed_ptr(p, new_mask, SCA_CHECK); |
1da177e4 | 7644 | |
8707d8b8 | 7645 | if (!retval) { |
5a16f3d3 RR |
7646 | cpuset_cpus_allowed(p, cpus_allowed); |
7647 | if (!cpumask_subset(new_mask, cpus_allowed)) { | |
8707d8b8 PM |
7648 | /* |
7649 | * We must have raced with a concurrent cpuset | |
7650 | * update. Just reset the cpus_allowed to the | |
7651 | * cpuset's cpus_allowed | |
7652 | */ | |
5a16f3d3 | 7653 | cpumask_copy(new_mask, cpus_allowed); |
8707d8b8 PM |
7654 | goto again; |
7655 | } | |
7656 | } | |
16303ab2 | 7657 | out_free_new_mask: |
5a16f3d3 RR |
7658 | free_cpumask_var(new_mask); |
7659 | out_free_cpus_allowed: | |
7660 | free_cpumask_var(cpus_allowed); | |
7661 | out_put_task: | |
1da177e4 | 7662 | put_task_struct(p); |
1da177e4 LT |
7663 | return retval; |
7664 | } | |
7665 | ||
7666 | static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len, | |
96f874e2 | 7667 | struct cpumask *new_mask) |
1da177e4 | 7668 | { |
96f874e2 RR |
7669 | if (len < cpumask_size()) |
7670 | cpumask_clear(new_mask); | |
7671 | else if (len > cpumask_size()) | |
7672 | len = cpumask_size(); | |
7673 | ||
1da177e4 LT |
7674 | return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0; |
7675 | } | |
7676 | ||
7677 | /** | |
d1ccc66d | 7678 | * sys_sched_setaffinity - set the CPU affinity of a process |
1da177e4 LT |
7679 | * @pid: pid of the process |
7680 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr | |
d1ccc66d | 7681 | * @user_mask_ptr: user-space pointer to the new CPU mask |
e69f6186 YB |
7682 | * |
7683 | * Return: 0 on success. An error code otherwise. | |
1da177e4 | 7684 | */ |
5add95d4 HC |
7685 | SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len, |
7686 | unsigned long __user *, user_mask_ptr) | |
1da177e4 | 7687 | { |
5a16f3d3 | 7688 | cpumask_var_t new_mask; |
1da177e4 LT |
7689 | int retval; |
7690 | ||
5a16f3d3 RR |
7691 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) |
7692 | return -ENOMEM; | |
1da177e4 | 7693 | |
5a16f3d3 RR |
7694 | retval = get_user_cpu_mask(user_mask_ptr, len, new_mask); |
7695 | if (retval == 0) | |
7696 | retval = sched_setaffinity(pid, new_mask); | |
7697 | free_cpumask_var(new_mask); | |
7698 | return retval; | |
1da177e4 LT |
7699 | } |
7700 | ||
96f874e2 | 7701 | long sched_getaffinity(pid_t pid, struct cpumask *mask) |
1da177e4 | 7702 | { |
36c8b586 | 7703 | struct task_struct *p; |
31605683 | 7704 | unsigned long flags; |
1da177e4 | 7705 | int retval; |
1da177e4 | 7706 | |
23f5d142 | 7707 | rcu_read_lock(); |
1da177e4 LT |
7708 | |
7709 | retval = -ESRCH; | |
7710 | p = find_process_by_pid(pid); | |
7711 | if (!p) | |
7712 | goto out_unlock; | |
7713 | ||
e7834f8f DQ |
7714 | retval = security_task_getscheduler(p); |
7715 | if (retval) | |
7716 | goto out_unlock; | |
7717 | ||
013fdb80 | 7718 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
3bd37062 | 7719 | cpumask_and(mask, &p->cpus_mask, cpu_active_mask); |
013fdb80 | 7720 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
1da177e4 LT |
7721 | |
7722 | out_unlock: | |
23f5d142 | 7723 | rcu_read_unlock(); |
1da177e4 | 7724 | |
9531b62f | 7725 | return retval; |
1da177e4 LT |
7726 | } |
7727 | ||
7728 | /** | |
d1ccc66d | 7729 | * sys_sched_getaffinity - get the CPU affinity of a process |
1da177e4 LT |
7730 | * @pid: pid of the process |
7731 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr | |
d1ccc66d | 7732 | * @user_mask_ptr: user-space pointer to hold the current CPU mask |
e69f6186 | 7733 | * |
599b4840 ZW |
7734 | * Return: size of CPU mask copied to user_mask_ptr on success. An |
7735 | * error code otherwise. | |
1da177e4 | 7736 | */ |
5add95d4 HC |
7737 | SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len, |
7738 | unsigned long __user *, user_mask_ptr) | |
1da177e4 LT |
7739 | { |
7740 | int ret; | |
f17c8607 | 7741 | cpumask_var_t mask; |
1da177e4 | 7742 | |
84fba5ec | 7743 | if ((len * BITS_PER_BYTE) < nr_cpu_ids) |
cd3d8031 KM |
7744 | return -EINVAL; |
7745 | if (len & (sizeof(unsigned long)-1)) | |
1da177e4 LT |
7746 | return -EINVAL; |
7747 | ||
f17c8607 RR |
7748 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) |
7749 | return -ENOMEM; | |
1da177e4 | 7750 | |
f17c8607 RR |
7751 | ret = sched_getaffinity(pid, mask); |
7752 | if (ret == 0) { | |
4de373a1 | 7753 | unsigned int retlen = min(len, cpumask_size()); |
cd3d8031 KM |
7754 | |
7755 | if (copy_to_user(user_mask_ptr, mask, retlen)) | |
f17c8607 RR |
7756 | ret = -EFAULT; |
7757 | else | |
cd3d8031 | 7758 | ret = retlen; |
f17c8607 RR |
7759 | } |
7760 | free_cpumask_var(mask); | |
1da177e4 | 7761 | |
f17c8607 | 7762 | return ret; |
1da177e4 LT |
7763 | } |
7764 | ||
7d4dd4f1 | 7765 | static void do_sched_yield(void) |
1da177e4 | 7766 | { |
8a8c69c3 PZ |
7767 | struct rq_flags rf; |
7768 | struct rq *rq; | |
7769 | ||
246b3b33 | 7770 | rq = this_rq_lock_irq(&rf); |
1da177e4 | 7771 | |
ae92882e | 7772 | schedstat_inc(rq->yld_count); |
4530d7ab | 7773 | current->sched_class->yield_task(rq); |
1da177e4 | 7774 | |
8a8c69c3 | 7775 | preempt_disable(); |
345a957f | 7776 | rq_unlock_irq(rq, &rf); |
ba74c144 | 7777 | sched_preempt_enable_no_resched(); |
1da177e4 LT |
7778 | |
7779 | schedule(); | |
7d4dd4f1 | 7780 | } |
1da177e4 | 7781 | |
59a74b15 MCC |
7782 | /** |
7783 | * sys_sched_yield - yield the current processor to other threads. | |
7784 | * | |
7785 | * This function yields the current CPU to other tasks. If there are no | |
7786 | * other threads running on this CPU then this function will return. | |
7787 | * | |
7788 | * Return: 0. | |
7789 | */ | |
7d4dd4f1 DB |
7790 | SYSCALL_DEFINE0(sched_yield) |
7791 | { | |
7792 | do_sched_yield(); | |
1da177e4 LT |
7793 | return 0; |
7794 | } | |
7795 | ||
b965f1dd PZI |
7796 | #if !defined(CONFIG_PREEMPTION) || defined(CONFIG_PREEMPT_DYNAMIC) |
7797 | int __sched __cond_resched(void) | |
1da177e4 | 7798 | { |
fe32d3cd | 7799 | if (should_resched(0)) { |
a18b5d01 | 7800 | preempt_schedule_common(); |
1da177e4 LT |
7801 | return 1; |
7802 | } | |
b965f1dd | 7803 | #ifndef CONFIG_PREEMPT_RCU |
f79c3ad6 | 7804 | rcu_all_qs(); |
b965f1dd | 7805 | #endif |
1da177e4 LT |
7806 | return 0; |
7807 | } | |
b965f1dd PZI |
7808 | EXPORT_SYMBOL(__cond_resched); |
7809 | #endif | |
7810 | ||
7811 | #ifdef CONFIG_PREEMPT_DYNAMIC | |
7812 | DEFINE_STATIC_CALL_RET0(cond_resched, __cond_resched); | |
ef72661e | 7813 | EXPORT_STATIC_CALL_TRAMP(cond_resched); |
b965f1dd PZI |
7814 | |
7815 | DEFINE_STATIC_CALL_RET0(might_resched, __cond_resched); | |
ef72661e | 7816 | EXPORT_STATIC_CALL_TRAMP(might_resched); |
35a773a0 | 7817 | #endif |
1da177e4 LT |
7818 | |
7819 | /* | |
613afbf8 | 7820 | * __cond_resched_lock() - if a reschedule is pending, drop the given lock, |
1da177e4 LT |
7821 | * call schedule, and on return reacquire the lock. |
7822 | * | |
c1a280b6 | 7823 | * This works OK both with and without CONFIG_PREEMPTION. We do strange low-level |
1da177e4 LT |
7824 | * operations here to prevent schedule() from being called twice (once via |
7825 | * spin_unlock(), once by hand). | |
7826 | */ | |
613afbf8 | 7827 | int __cond_resched_lock(spinlock_t *lock) |
1da177e4 | 7828 | { |
fe32d3cd | 7829 | int resched = should_resched(PREEMPT_LOCK_OFFSET); |
6df3cecb JK |
7830 | int ret = 0; |
7831 | ||
f607c668 PZ |
7832 | lockdep_assert_held(lock); |
7833 | ||
4a81e832 | 7834 | if (spin_needbreak(lock) || resched) { |
1da177e4 | 7835 | spin_unlock(lock); |
d86ee480 | 7836 | if (resched) |
a18b5d01 | 7837 | preempt_schedule_common(); |
95c354fe NP |
7838 | else |
7839 | cpu_relax(); | |
6df3cecb | 7840 | ret = 1; |
1da177e4 | 7841 | spin_lock(lock); |
1da177e4 | 7842 | } |
6df3cecb | 7843 | return ret; |
1da177e4 | 7844 | } |
613afbf8 | 7845 | EXPORT_SYMBOL(__cond_resched_lock); |
1da177e4 | 7846 | |
f3d4b4b1 BG |
7847 | int __cond_resched_rwlock_read(rwlock_t *lock) |
7848 | { | |
7849 | int resched = should_resched(PREEMPT_LOCK_OFFSET); | |
7850 | int ret = 0; | |
7851 | ||
7852 | lockdep_assert_held_read(lock); | |
7853 | ||
7854 | if (rwlock_needbreak(lock) || resched) { | |
7855 | read_unlock(lock); | |
7856 | if (resched) | |
7857 | preempt_schedule_common(); | |
7858 | else | |
7859 | cpu_relax(); | |
7860 | ret = 1; | |
7861 | read_lock(lock); | |
7862 | } | |
7863 | return ret; | |
7864 | } | |
7865 | EXPORT_SYMBOL(__cond_resched_rwlock_read); | |
7866 | ||
7867 | int __cond_resched_rwlock_write(rwlock_t *lock) | |
7868 | { | |
7869 | int resched = should_resched(PREEMPT_LOCK_OFFSET); | |
7870 | int ret = 0; | |
7871 | ||
7872 | lockdep_assert_held_write(lock); | |
7873 | ||
7874 | if (rwlock_needbreak(lock) || resched) { | |
7875 | write_unlock(lock); | |
7876 | if (resched) | |
7877 | preempt_schedule_common(); | |
7878 | else | |
7879 | cpu_relax(); | |
7880 | ret = 1; | |
7881 | write_lock(lock); | |
7882 | } | |
7883 | return ret; | |
7884 | } | |
7885 | EXPORT_SYMBOL(__cond_resched_rwlock_write); | |
7886 | ||
1da177e4 LT |
7887 | /** |
7888 | * yield - yield the current processor to other threads. | |
7889 | * | |
8e3fabfd PZ |
7890 | * Do not ever use this function, there's a 99% chance you're doing it wrong. |
7891 | * | |
7892 | * The scheduler is at all times free to pick the calling task as the most | |
7893 | * eligible task to run, if removing the yield() call from your code breaks | |
b19a888c | 7894 | * it, it's already broken. |
8e3fabfd PZ |
7895 | * |
7896 | * Typical broken usage is: | |
7897 | * | |
7898 | * while (!event) | |
d1ccc66d | 7899 | * yield(); |
8e3fabfd PZ |
7900 | * |
7901 | * where one assumes that yield() will let 'the other' process run that will | |
7902 | * make event true. If the current task is a SCHED_FIFO task that will never | |
7903 | * happen. Never use yield() as a progress guarantee!! | |
7904 | * | |
7905 | * If you want to use yield() to wait for something, use wait_event(). | |
7906 | * If you want to use yield() to be 'nice' for others, use cond_resched(). | |
7907 | * If you still want to use yield(), do not! | |
1da177e4 LT |
7908 | */ |
7909 | void __sched yield(void) | |
7910 | { | |
7911 | set_current_state(TASK_RUNNING); | |
7d4dd4f1 | 7912 | do_sched_yield(); |
1da177e4 | 7913 | } |
1da177e4 LT |
7914 | EXPORT_SYMBOL(yield); |
7915 | ||
d95f4122 MG |
7916 | /** |
7917 | * yield_to - yield the current processor to another thread in | |
7918 | * your thread group, or accelerate that thread toward the | |
7919 | * processor it's on. | |
16addf95 RD |
7920 | * @p: target task |
7921 | * @preempt: whether task preemption is allowed or not | |
d95f4122 MG |
7922 | * |
7923 | * It's the caller's job to ensure that the target task struct | |
7924 | * can't go away on us before we can do any checks. | |
7925 | * | |
e69f6186 | 7926 | * Return: |
7b270f60 PZ |
7927 | * true (>0) if we indeed boosted the target task. |
7928 | * false (0) if we failed to boost the target. | |
7929 | * -ESRCH if there's no task to yield to. | |
d95f4122 | 7930 | */ |
fa93384f | 7931 | int __sched yield_to(struct task_struct *p, bool preempt) |
d95f4122 MG |
7932 | { |
7933 | struct task_struct *curr = current; | |
7934 | struct rq *rq, *p_rq; | |
7935 | unsigned long flags; | |
c3c18640 | 7936 | int yielded = 0; |
d95f4122 MG |
7937 | |
7938 | local_irq_save(flags); | |
7939 | rq = this_rq(); | |
7940 | ||
7941 | again: | |
7942 | p_rq = task_rq(p); | |
7b270f60 PZ |
7943 | /* |
7944 | * If we're the only runnable task on the rq and target rq also | |
7945 | * has only one task, there's absolutely no point in yielding. | |
7946 | */ | |
7947 | if (rq->nr_running == 1 && p_rq->nr_running == 1) { | |
7948 | yielded = -ESRCH; | |
7949 | goto out_irq; | |
7950 | } | |
7951 | ||
d95f4122 | 7952 | double_rq_lock(rq, p_rq); |
39e24d8f | 7953 | if (task_rq(p) != p_rq) { |
d95f4122 MG |
7954 | double_rq_unlock(rq, p_rq); |
7955 | goto again; | |
7956 | } | |
7957 | ||
7958 | if (!curr->sched_class->yield_to_task) | |
7b270f60 | 7959 | goto out_unlock; |
d95f4122 MG |
7960 | |
7961 | if (curr->sched_class != p->sched_class) | |
7b270f60 | 7962 | goto out_unlock; |
d95f4122 | 7963 | |
b03fbd4f | 7964 | if (task_running(p_rq, p) || !task_is_running(p)) |
7b270f60 | 7965 | goto out_unlock; |
d95f4122 | 7966 | |
0900acf2 | 7967 | yielded = curr->sched_class->yield_to_task(rq, p); |
6d1cafd8 | 7968 | if (yielded) { |
ae92882e | 7969 | schedstat_inc(rq->yld_count); |
6d1cafd8 VP |
7970 | /* |
7971 | * Make p's CPU reschedule; pick_next_entity takes care of | |
7972 | * fairness. | |
7973 | */ | |
7974 | if (preempt && rq != p_rq) | |
8875125e | 7975 | resched_curr(p_rq); |
6d1cafd8 | 7976 | } |
d95f4122 | 7977 | |
7b270f60 | 7978 | out_unlock: |
d95f4122 | 7979 | double_rq_unlock(rq, p_rq); |
7b270f60 | 7980 | out_irq: |
d95f4122 MG |
7981 | local_irq_restore(flags); |
7982 | ||
7b270f60 | 7983 | if (yielded > 0) |
d95f4122 MG |
7984 | schedule(); |
7985 | ||
7986 | return yielded; | |
7987 | } | |
7988 | EXPORT_SYMBOL_GPL(yield_to); | |
7989 | ||
10ab5643 TH |
7990 | int io_schedule_prepare(void) |
7991 | { | |
7992 | int old_iowait = current->in_iowait; | |
7993 | ||
7994 | current->in_iowait = 1; | |
7995 | blk_schedule_flush_plug(current); | |
7996 | ||
7997 | return old_iowait; | |
7998 | } | |
7999 | ||
8000 | void io_schedule_finish(int token) | |
8001 | { | |
8002 | current->in_iowait = token; | |
8003 | } | |
8004 | ||
1da177e4 | 8005 | /* |
41a2d6cf | 8006 | * This task is about to go to sleep on IO. Increment rq->nr_iowait so |
1da177e4 | 8007 | * that process accounting knows that this is a task in IO wait state. |
1da177e4 | 8008 | */ |
1da177e4 LT |
8009 | long __sched io_schedule_timeout(long timeout) |
8010 | { | |
10ab5643 | 8011 | int token; |
1da177e4 LT |
8012 | long ret; |
8013 | ||
10ab5643 | 8014 | token = io_schedule_prepare(); |
1da177e4 | 8015 | ret = schedule_timeout(timeout); |
10ab5643 | 8016 | io_schedule_finish(token); |
9cff8ade | 8017 | |
1da177e4 LT |
8018 | return ret; |
8019 | } | |
9cff8ade | 8020 | EXPORT_SYMBOL(io_schedule_timeout); |
1da177e4 | 8021 | |
e3b929b0 | 8022 | void __sched io_schedule(void) |
10ab5643 TH |
8023 | { |
8024 | int token; | |
8025 | ||
8026 | token = io_schedule_prepare(); | |
8027 | schedule(); | |
8028 | io_schedule_finish(token); | |
8029 | } | |
8030 | EXPORT_SYMBOL(io_schedule); | |
8031 | ||
1da177e4 LT |
8032 | /** |
8033 | * sys_sched_get_priority_max - return maximum RT priority. | |
8034 | * @policy: scheduling class. | |
8035 | * | |
e69f6186 YB |
8036 | * Return: On success, this syscall returns the maximum |
8037 | * rt_priority that can be used by a given scheduling class. | |
8038 | * On failure, a negative error code is returned. | |
1da177e4 | 8039 | */ |
5add95d4 | 8040 | SYSCALL_DEFINE1(sched_get_priority_max, int, policy) |
1da177e4 LT |
8041 | { |
8042 | int ret = -EINVAL; | |
8043 | ||
8044 | switch (policy) { | |
8045 | case SCHED_FIFO: | |
8046 | case SCHED_RR: | |
ae18ad28 | 8047 | ret = MAX_RT_PRIO-1; |
1da177e4 | 8048 | break; |
aab03e05 | 8049 | case SCHED_DEADLINE: |
1da177e4 | 8050 | case SCHED_NORMAL: |
b0a9499c | 8051 | case SCHED_BATCH: |
dd41f596 | 8052 | case SCHED_IDLE: |
1da177e4 LT |
8053 | ret = 0; |
8054 | break; | |
8055 | } | |
8056 | return ret; | |
8057 | } | |
8058 | ||
8059 | /** | |
8060 | * sys_sched_get_priority_min - return minimum RT priority. | |
8061 | * @policy: scheduling class. | |
8062 | * | |
e69f6186 YB |
8063 | * Return: On success, this syscall returns the minimum |
8064 | * rt_priority that can be used by a given scheduling class. | |
8065 | * On failure, a negative error code is returned. | |
1da177e4 | 8066 | */ |
5add95d4 | 8067 | SYSCALL_DEFINE1(sched_get_priority_min, int, policy) |
1da177e4 LT |
8068 | { |
8069 | int ret = -EINVAL; | |
8070 | ||
8071 | switch (policy) { | |
8072 | case SCHED_FIFO: | |
8073 | case SCHED_RR: | |
8074 | ret = 1; | |
8075 | break; | |
aab03e05 | 8076 | case SCHED_DEADLINE: |
1da177e4 | 8077 | case SCHED_NORMAL: |
b0a9499c | 8078 | case SCHED_BATCH: |
dd41f596 | 8079 | case SCHED_IDLE: |
1da177e4 LT |
8080 | ret = 0; |
8081 | } | |
8082 | return ret; | |
8083 | } | |
8084 | ||
abca5fc5 | 8085 | static int sched_rr_get_interval(pid_t pid, struct timespec64 *t) |
1da177e4 | 8086 | { |
36c8b586 | 8087 | struct task_struct *p; |
a4ec24b4 | 8088 | unsigned int time_slice; |
eb580751 | 8089 | struct rq_flags rf; |
dba091b9 | 8090 | struct rq *rq; |
3a5c359a | 8091 | int retval; |
1da177e4 LT |
8092 | |
8093 | if (pid < 0) | |
3a5c359a | 8094 | return -EINVAL; |
1da177e4 LT |
8095 | |
8096 | retval = -ESRCH; | |
1a551ae7 | 8097 | rcu_read_lock(); |
1da177e4 LT |
8098 | p = find_process_by_pid(pid); |
8099 | if (!p) | |
8100 | goto out_unlock; | |
8101 | ||
8102 | retval = security_task_getscheduler(p); | |
8103 | if (retval) | |
8104 | goto out_unlock; | |
8105 | ||
eb580751 | 8106 | rq = task_rq_lock(p, &rf); |
a57beec5 PZ |
8107 | time_slice = 0; |
8108 | if (p->sched_class->get_rr_interval) | |
8109 | time_slice = p->sched_class->get_rr_interval(rq, p); | |
eb580751 | 8110 | task_rq_unlock(rq, p, &rf); |
a4ec24b4 | 8111 | |
1a551ae7 | 8112 | rcu_read_unlock(); |
abca5fc5 AV |
8113 | jiffies_to_timespec64(time_slice, t); |
8114 | return 0; | |
3a5c359a | 8115 | |
1da177e4 | 8116 | out_unlock: |
1a551ae7 | 8117 | rcu_read_unlock(); |
1da177e4 LT |
8118 | return retval; |
8119 | } | |
8120 | ||
2064a5ab RD |
8121 | /** |
8122 | * sys_sched_rr_get_interval - return the default timeslice of a process. | |
8123 | * @pid: pid of the process. | |
8124 | * @interval: userspace pointer to the timeslice value. | |
8125 | * | |
8126 | * this syscall writes the default timeslice value of a given process | |
8127 | * into the user-space timespec buffer. A value of '0' means infinity. | |
8128 | * | |
8129 | * Return: On success, 0 and the timeslice is in @interval. Otherwise, | |
8130 | * an error code. | |
8131 | */ | |
abca5fc5 | 8132 | SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid, |
474b9c77 | 8133 | struct __kernel_timespec __user *, interval) |
abca5fc5 AV |
8134 | { |
8135 | struct timespec64 t; | |
8136 | int retval = sched_rr_get_interval(pid, &t); | |
8137 | ||
8138 | if (retval == 0) | |
8139 | retval = put_timespec64(&t, interval); | |
8140 | ||
8141 | return retval; | |
8142 | } | |
8143 | ||
474b9c77 | 8144 | #ifdef CONFIG_COMPAT_32BIT_TIME |
8dabe724 AB |
8145 | SYSCALL_DEFINE2(sched_rr_get_interval_time32, pid_t, pid, |
8146 | struct old_timespec32 __user *, interval) | |
abca5fc5 AV |
8147 | { |
8148 | struct timespec64 t; | |
8149 | int retval = sched_rr_get_interval(pid, &t); | |
8150 | ||
8151 | if (retval == 0) | |
9afc5eee | 8152 | retval = put_old_timespec32(&t, interval); |
abca5fc5 AV |
8153 | return retval; |
8154 | } | |
8155 | #endif | |
8156 | ||
82a1fcb9 | 8157 | void sched_show_task(struct task_struct *p) |
1da177e4 | 8158 | { |
1da177e4 | 8159 | unsigned long free = 0; |
4e79752c | 8160 | int ppid; |
c930b2c0 | 8161 | |
38200502 TH |
8162 | if (!try_get_task_stack(p)) |
8163 | return; | |
20435d84 | 8164 | |
cc172ff3 | 8165 | pr_info("task:%-15.15s state:%c", p->comm, task_state_to_char(p)); |
20435d84 | 8166 | |
b03fbd4f | 8167 | if (task_is_running(p)) |
cc172ff3 | 8168 | pr_cont(" running task "); |
1da177e4 | 8169 | #ifdef CONFIG_DEBUG_STACK_USAGE |
7c9f8861 | 8170 | free = stack_not_used(p); |
1da177e4 | 8171 | #endif |
a90e984c | 8172 | ppid = 0; |
4e79752c | 8173 | rcu_read_lock(); |
a90e984c ON |
8174 | if (pid_alive(p)) |
8175 | ppid = task_pid_nr(rcu_dereference(p->real_parent)); | |
4e79752c | 8176 | rcu_read_unlock(); |
cc172ff3 LZ |
8177 | pr_cont(" stack:%5lu pid:%5d ppid:%6d flags:0x%08lx\n", |
8178 | free, task_pid_nr(p), ppid, | |
aa47b7e0 | 8179 | (unsigned long)task_thread_info(p)->flags); |
1da177e4 | 8180 | |
3d1cb205 | 8181 | print_worker_info(KERN_INFO, p); |
a8b62fd0 | 8182 | print_stop_info(KERN_INFO, p); |
9cb8f069 | 8183 | show_stack(p, NULL, KERN_INFO); |
38200502 | 8184 | put_task_stack(p); |
1da177e4 | 8185 | } |
0032f4e8 | 8186 | EXPORT_SYMBOL_GPL(sched_show_task); |
1da177e4 | 8187 | |
5d68cc95 PZ |
8188 | static inline bool |
8189 | state_filter_match(unsigned long state_filter, struct task_struct *p) | |
8190 | { | |
2f064a59 PZ |
8191 | unsigned int state = READ_ONCE(p->__state); |
8192 | ||
5d68cc95 PZ |
8193 | /* no filter, everything matches */ |
8194 | if (!state_filter) | |
8195 | return true; | |
8196 | ||
8197 | /* filter, but doesn't match */ | |
2f064a59 | 8198 | if (!(state & state_filter)) |
5d68cc95 PZ |
8199 | return false; |
8200 | ||
8201 | /* | |
8202 | * When looking for TASK_UNINTERRUPTIBLE skip TASK_IDLE (allows | |
8203 | * TASK_KILLABLE). | |
8204 | */ | |
2f064a59 | 8205 | if (state_filter == TASK_UNINTERRUPTIBLE && state == TASK_IDLE) |
5d68cc95 PZ |
8206 | return false; |
8207 | ||
8208 | return true; | |
8209 | } | |
8210 | ||
8211 | ||
2f064a59 | 8212 | void show_state_filter(unsigned int state_filter) |
1da177e4 | 8213 | { |
36c8b586 | 8214 | struct task_struct *g, *p; |
1da177e4 | 8215 | |
510f5acc | 8216 | rcu_read_lock(); |
5d07f420 | 8217 | for_each_process_thread(g, p) { |
1da177e4 LT |
8218 | /* |
8219 | * reset the NMI-timeout, listing all files on a slow | |
25985edc | 8220 | * console might take a lot of time: |
57675cb9 AR |
8221 | * Also, reset softlockup watchdogs on all CPUs, because |
8222 | * another CPU might be blocked waiting for us to process | |
8223 | * an IPI. | |
1da177e4 LT |
8224 | */ |
8225 | touch_nmi_watchdog(); | |
57675cb9 | 8226 | touch_all_softlockup_watchdogs(); |
5d68cc95 | 8227 | if (state_filter_match(state_filter, p)) |
82a1fcb9 | 8228 | sched_show_task(p); |
5d07f420 | 8229 | } |
1da177e4 | 8230 | |
dd41f596 | 8231 | #ifdef CONFIG_SCHED_DEBUG |
fb90a6e9 RV |
8232 | if (!state_filter) |
8233 | sysrq_sched_debug_show(); | |
dd41f596 | 8234 | #endif |
510f5acc | 8235 | rcu_read_unlock(); |
e59e2ae2 IM |
8236 | /* |
8237 | * Only show locks if all tasks are dumped: | |
8238 | */ | |
93335a21 | 8239 | if (!state_filter) |
e59e2ae2 | 8240 | debug_show_all_locks(); |
1da177e4 LT |
8241 | } |
8242 | ||
f340c0d1 IM |
8243 | /** |
8244 | * init_idle - set up an idle thread for a given CPU | |
8245 | * @idle: task in question | |
d1ccc66d | 8246 | * @cpu: CPU the idle task belongs to |
f340c0d1 IM |
8247 | * |
8248 | * NOTE: this function does not set the idle thread's NEED_RESCHED | |
8249 | * flag, to make booting more robust. | |
8250 | */ | |
f1a0a376 | 8251 | void __init init_idle(struct task_struct *idle, int cpu) |
1da177e4 | 8252 | { |
70b97a7f | 8253 | struct rq *rq = cpu_rq(cpu); |
1da177e4 LT |
8254 | unsigned long flags; |
8255 | ||
ff51ff84 PZ |
8256 | __sched_fork(0, idle); |
8257 | ||
00b89fe0 VS |
8258 | /* |
8259 | * The idle task doesn't need the kthread struct to function, but it | |
8260 | * is dressed up as a per-CPU kthread and thus needs to play the part | |
8261 | * if we want to avoid special-casing it in code that deals with per-CPU | |
8262 | * kthreads. | |
8263 | */ | |
8264 | set_kthread_struct(idle); | |
8265 | ||
25834c73 | 8266 | raw_spin_lock_irqsave(&idle->pi_lock, flags); |
5cb9eaa3 | 8267 | raw_spin_rq_lock(rq); |
5cbd54ef | 8268 | |
2f064a59 | 8269 | idle->__state = TASK_RUNNING; |
dd41f596 | 8270 | idle->se.exec_start = sched_clock(); |
00b89fe0 VS |
8271 | /* |
8272 | * PF_KTHREAD should already be set at this point; regardless, make it | |
8273 | * look like a proper per-CPU kthread. | |
8274 | */ | |
8275 | idle->flags |= PF_IDLE | PF_KTHREAD | PF_NO_SETAFFINITY; | |
8276 | kthread_set_per_cpu(idle, cpu); | |
dd41f596 | 8277 | |
d08b9f0c | 8278 | scs_task_reset(idle); |
e1b77c92 MR |
8279 | kasan_unpoison_task_stack(idle); |
8280 | ||
de9b8f5d PZ |
8281 | #ifdef CONFIG_SMP |
8282 | /* | |
b19a888c | 8283 | * It's possible that init_idle() gets called multiple times on a task, |
de9b8f5d PZ |
8284 | * in that case do_set_cpus_allowed() will not do the right thing. |
8285 | * | |
8286 | * And since this is boot we can forgo the serialization. | |
8287 | */ | |
9cfc3e18 | 8288 | set_cpus_allowed_common(idle, cpumask_of(cpu), 0); |
de9b8f5d | 8289 | #endif |
6506cf6c PZ |
8290 | /* |
8291 | * We're having a chicken and egg problem, even though we are | |
d1ccc66d | 8292 | * holding rq->lock, the CPU isn't yet set to this CPU so the |
6506cf6c PZ |
8293 | * lockdep check in task_group() will fail. |
8294 | * | |
8295 | * Similar case to sched_fork(). / Alternatively we could | |
8296 | * use task_rq_lock() here and obtain the other rq->lock. | |
8297 | * | |
8298 | * Silence PROVE_RCU | |
8299 | */ | |
8300 | rcu_read_lock(); | |
dd41f596 | 8301 | __set_task_cpu(idle, cpu); |
6506cf6c | 8302 | rcu_read_unlock(); |
1da177e4 | 8303 | |
5311a98f EB |
8304 | rq->idle = idle; |
8305 | rcu_assign_pointer(rq->curr, idle); | |
da0c1e65 | 8306 | idle->on_rq = TASK_ON_RQ_QUEUED; |
de9b8f5d | 8307 | #ifdef CONFIG_SMP |
3ca7a440 | 8308 | idle->on_cpu = 1; |
4866cde0 | 8309 | #endif |
5cb9eaa3 | 8310 | raw_spin_rq_unlock(rq); |
25834c73 | 8311 | raw_spin_unlock_irqrestore(&idle->pi_lock, flags); |
1da177e4 LT |
8312 | |
8313 | /* Set the preempt count _outside_ the spinlocks! */ | |
01028747 | 8314 | init_idle_preempt_count(idle, cpu); |
55cd5340 | 8315 | |
dd41f596 IM |
8316 | /* |
8317 | * The idle tasks have their own, simple scheduling class: | |
8318 | */ | |
8319 | idle->sched_class = &idle_sched_class; | |
868baf07 | 8320 | ftrace_graph_init_idle_task(idle, cpu); |
45eacc69 | 8321 | vtime_init_idle(idle, cpu); |
de9b8f5d | 8322 | #ifdef CONFIG_SMP |
f1c6f1a7 CE |
8323 | sprintf(idle->comm, "%s/%d", INIT_TASK_COMM, cpu); |
8324 | #endif | |
19978ca6 IM |
8325 | } |
8326 | ||
e1d4eeec NP |
8327 | #ifdef CONFIG_SMP |
8328 | ||
f82f8042 JL |
8329 | int cpuset_cpumask_can_shrink(const struct cpumask *cur, |
8330 | const struct cpumask *trial) | |
8331 | { | |
06a76fe0 | 8332 | int ret = 1; |
f82f8042 | 8333 | |
bb2bc55a MG |
8334 | if (!cpumask_weight(cur)) |
8335 | return ret; | |
8336 | ||
06a76fe0 | 8337 | ret = dl_cpuset_cpumask_can_shrink(cur, trial); |
f82f8042 JL |
8338 | |
8339 | return ret; | |
8340 | } | |
8341 | ||
7f51412a JL |
8342 | int task_can_attach(struct task_struct *p, |
8343 | const struct cpumask *cs_cpus_allowed) | |
8344 | { | |
8345 | int ret = 0; | |
8346 | ||
8347 | /* | |
8348 | * Kthreads which disallow setaffinity shouldn't be moved | |
d1ccc66d | 8349 | * to a new cpuset; we don't want to change their CPU |
7f51412a JL |
8350 | * affinity and isolating such threads by their set of |
8351 | * allowed nodes is unnecessary. Thus, cpusets are not | |
8352 | * applicable for such threads. This prevents checking for | |
8353 | * success of set_cpus_allowed_ptr() on all attached tasks | |
3bd37062 | 8354 | * before cpus_mask may be changed. |
7f51412a JL |
8355 | */ |
8356 | if (p->flags & PF_NO_SETAFFINITY) { | |
8357 | ret = -EINVAL; | |
8358 | goto out; | |
8359 | } | |
8360 | ||
7f51412a | 8361 | if (dl_task(p) && !cpumask_intersects(task_rq(p)->rd->span, |
06a76fe0 NP |
8362 | cs_cpus_allowed)) |
8363 | ret = dl_task_can_attach(p, cs_cpus_allowed); | |
7f51412a | 8364 | |
7f51412a JL |
8365 | out: |
8366 | return ret; | |
8367 | } | |
8368 | ||
f2cb1360 | 8369 | bool sched_smp_initialized __read_mostly; |
e26fbffd | 8370 | |
e6628d5b MG |
8371 | #ifdef CONFIG_NUMA_BALANCING |
8372 | /* Migrate current task p to target_cpu */ | |
8373 | int migrate_task_to(struct task_struct *p, int target_cpu) | |
8374 | { | |
8375 | struct migration_arg arg = { p, target_cpu }; | |
8376 | int curr_cpu = task_cpu(p); | |
8377 | ||
8378 | if (curr_cpu == target_cpu) | |
8379 | return 0; | |
8380 | ||
3bd37062 | 8381 | if (!cpumask_test_cpu(target_cpu, p->cpus_ptr)) |
e6628d5b MG |
8382 | return -EINVAL; |
8383 | ||
8384 | /* TODO: This is not properly updating schedstats */ | |
8385 | ||
286549dc | 8386 | trace_sched_move_numa(p, curr_cpu, target_cpu); |
e6628d5b MG |
8387 | return stop_one_cpu(curr_cpu, migration_cpu_stop, &arg); |
8388 | } | |
0ec8aa00 PZ |
8389 | |
8390 | /* | |
8391 | * Requeue a task on a given node and accurately track the number of NUMA | |
8392 | * tasks on the runqueues | |
8393 | */ | |
8394 | void sched_setnuma(struct task_struct *p, int nid) | |
8395 | { | |
da0c1e65 | 8396 | bool queued, running; |
eb580751 PZ |
8397 | struct rq_flags rf; |
8398 | struct rq *rq; | |
0ec8aa00 | 8399 | |
eb580751 | 8400 | rq = task_rq_lock(p, &rf); |
da0c1e65 | 8401 | queued = task_on_rq_queued(p); |
0ec8aa00 PZ |
8402 | running = task_current(rq, p); |
8403 | ||
da0c1e65 | 8404 | if (queued) |
1de64443 | 8405 | dequeue_task(rq, p, DEQUEUE_SAVE); |
0ec8aa00 | 8406 | if (running) |
f3cd1c4e | 8407 | put_prev_task(rq, p); |
0ec8aa00 PZ |
8408 | |
8409 | p->numa_preferred_nid = nid; | |
0ec8aa00 | 8410 | |
da0c1e65 | 8411 | if (queued) |
7134b3e9 | 8412 | enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK); |
a399d233 | 8413 | if (running) |
03b7fad1 | 8414 | set_next_task(rq, p); |
eb580751 | 8415 | task_rq_unlock(rq, p, &rf); |
0ec8aa00 | 8416 | } |
5cc389bc | 8417 | #endif /* CONFIG_NUMA_BALANCING */ |
f7b4cddc | 8418 | |
1da177e4 | 8419 | #ifdef CONFIG_HOTPLUG_CPU |
054b9108 | 8420 | /* |
d1ccc66d | 8421 | * Ensure that the idle task is using init_mm right before its CPU goes |
48c5ccae | 8422 | * offline. |
054b9108 | 8423 | */ |
48c5ccae | 8424 | void idle_task_exit(void) |
1da177e4 | 8425 | { |
48c5ccae | 8426 | struct mm_struct *mm = current->active_mm; |
e76bd8d9 | 8427 | |
48c5ccae | 8428 | BUG_ON(cpu_online(smp_processor_id())); |
bf2c59fc | 8429 | BUG_ON(current != this_rq()->idle); |
e76bd8d9 | 8430 | |
a53efe5f | 8431 | if (mm != &init_mm) { |
252d2a41 | 8432 | switch_mm(mm, &init_mm, current); |
a53efe5f MS |
8433 | finish_arch_post_lock_switch(); |
8434 | } | |
bf2c59fc PZ |
8435 | |
8436 | /* finish_cpu(), as ran on the BP, will clean up the active_mm state */ | |
1da177e4 LT |
8437 | } |
8438 | ||
2558aacf | 8439 | static int __balance_push_cpu_stop(void *arg) |
1da177e4 | 8440 | { |
2558aacf PZ |
8441 | struct task_struct *p = arg; |
8442 | struct rq *rq = this_rq(); | |
8443 | struct rq_flags rf; | |
8444 | int cpu; | |
1da177e4 | 8445 | |
2558aacf PZ |
8446 | raw_spin_lock_irq(&p->pi_lock); |
8447 | rq_lock(rq, &rf); | |
3f1d2a31 | 8448 | |
2558aacf PZ |
8449 | update_rq_clock(rq); |
8450 | ||
8451 | if (task_rq(p) == rq && task_on_rq_queued(p)) { | |
8452 | cpu = select_fallback_rq(rq->cpu, p); | |
8453 | rq = __migrate_task(rq, &rf, p, cpu); | |
10e7071b | 8454 | } |
3f1d2a31 | 8455 | |
2558aacf PZ |
8456 | rq_unlock(rq, &rf); |
8457 | raw_spin_unlock_irq(&p->pi_lock); | |
8458 | ||
8459 | put_task_struct(p); | |
8460 | ||
8461 | return 0; | |
10e7071b | 8462 | } |
3f1d2a31 | 8463 | |
2558aacf PZ |
8464 | static DEFINE_PER_CPU(struct cpu_stop_work, push_work); |
8465 | ||
48f24c4d | 8466 | /* |
2558aacf | 8467 | * Ensure we only run per-cpu kthreads once the CPU goes !active. |
b5c44773 PZ |
8468 | * |
8469 | * This is enabled below SCHED_AP_ACTIVE; when !cpu_active(), but only | |
8470 | * effective when the hotplug motion is down. | |
1da177e4 | 8471 | */ |
2558aacf | 8472 | static void balance_push(struct rq *rq) |
1da177e4 | 8473 | { |
2558aacf PZ |
8474 | struct task_struct *push_task = rq->curr; |
8475 | ||
5cb9eaa3 | 8476 | lockdep_assert_rq_held(rq); |
2558aacf | 8477 | SCHED_WARN_ON(rq->cpu != smp_processor_id()); |
b5c44773 | 8478 | |
ae792702 PZ |
8479 | /* |
8480 | * Ensure the thing is persistent until balance_push_set(.on = false); | |
8481 | */ | |
8482 | rq->balance_callback = &balance_push_callback; | |
1da177e4 | 8483 | |
b5c44773 PZ |
8484 | /* |
8485 | * Only active while going offline. | |
8486 | */ | |
8487 | if (!cpu_dying(rq->cpu)) | |
8488 | return; | |
8489 | ||
1da177e4 | 8490 | /* |
2558aacf PZ |
8491 | * Both the cpu-hotplug and stop task are in this case and are |
8492 | * required to complete the hotplug process. | |
1da177e4 | 8493 | */ |
00b89fe0 | 8494 | if (kthread_is_per_cpu(push_task) || |
5ba2ffba PZ |
8495 | is_migration_disabled(push_task)) { |
8496 | ||
f2469a1f TG |
8497 | /* |
8498 | * If this is the idle task on the outgoing CPU try to wake | |
8499 | * up the hotplug control thread which might wait for the | |
8500 | * last task to vanish. The rcuwait_active() check is | |
8501 | * accurate here because the waiter is pinned on this CPU | |
8502 | * and can't obviously be running in parallel. | |
3015ef4b TG |
8503 | * |
8504 | * On RT kernels this also has to check whether there are | |
8505 | * pinned and scheduled out tasks on the runqueue. They | |
8506 | * need to leave the migrate disabled section first. | |
f2469a1f | 8507 | */ |
3015ef4b TG |
8508 | if (!rq->nr_running && !rq_has_pinned_tasks(rq) && |
8509 | rcuwait_active(&rq->hotplug_wait)) { | |
5cb9eaa3 | 8510 | raw_spin_rq_unlock(rq); |
f2469a1f | 8511 | rcuwait_wake_up(&rq->hotplug_wait); |
5cb9eaa3 | 8512 | raw_spin_rq_lock(rq); |
f2469a1f | 8513 | } |
2558aacf | 8514 | return; |
f2469a1f | 8515 | } |
48f24c4d | 8516 | |
2558aacf | 8517 | get_task_struct(push_task); |
77bd3970 | 8518 | /* |
2558aacf PZ |
8519 | * Temporarily drop rq->lock such that we can wake-up the stop task. |
8520 | * Both preemption and IRQs are still disabled. | |
77bd3970 | 8521 | */ |
5cb9eaa3 | 8522 | raw_spin_rq_unlock(rq); |
2558aacf PZ |
8523 | stop_one_cpu_nowait(rq->cpu, __balance_push_cpu_stop, push_task, |
8524 | this_cpu_ptr(&push_work)); | |
8525 | /* | |
8526 | * At this point need_resched() is true and we'll take the loop in | |
8527 | * schedule(). The next pick is obviously going to be the stop task | |
5ba2ffba | 8528 | * which kthread_is_per_cpu() and will push this task away. |
2558aacf | 8529 | */ |
5cb9eaa3 | 8530 | raw_spin_rq_lock(rq); |
2558aacf | 8531 | } |
77bd3970 | 8532 | |
2558aacf PZ |
8533 | static void balance_push_set(int cpu, bool on) |
8534 | { | |
8535 | struct rq *rq = cpu_rq(cpu); | |
8536 | struct rq_flags rf; | |
48c5ccae | 8537 | |
2558aacf | 8538 | rq_lock_irqsave(rq, &rf); |
22f667c9 PZ |
8539 | if (on) { |
8540 | WARN_ON_ONCE(rq->balance_callback); | |
ae792702 | 8541 | rq->balance_callback = &balance_push_callback; |
22f667c9 | 8542 | } else if (rq->balance_callback == &balance_push_callback) { |
ae792702 | 8543 | rq->balance_callback = NULL; |
22f667c9 | 8544 | } |
2558aacf PZ |
8545 | rq_unlock_irqrestore(rq, &rf); |
8546 | } | |
e692ab53 | 8547 | |
f2469a1f TG |
8548 | /* |
8549 | * Invoked from a CPUs hotplug control thread after the CPU has been marked | |
8550 | * inactive. All tasks which are not per CPU kernel threads are either | |
8551 | * pushed off this CPU now via balance_push() or placed on a different CPU | |
8552 | * during wakeup. Wait until the CPU is quiescent. | |
8553 | */ | |
8554 | static void balance_hotplug_wait(void) | |
8555 | { | |
8556 | struct rq *rq = this_rq(); | |
5473e0cc | 8557 | |
3015ef4b TG |
8558 | rcuwait_wait_event(&rq->hotplug_wait, |
8559 | rq->nr_running == 1 && !rq_has_pinned_tasks(rq), | |
f2469a1f TG |
8560 | TASK_UNINTERRUPTIBLE); |
8561 | } | |
5473e0cc | 8562 | |
2558aacf | 8563 | #else |
dce48a84 | 8564 | |
2558aacf PZ |
8565 | static inline void balance_push(struct rq *rq) |
8566 | { | |
dce48a84 | 8567 | } |
dce48a84 | 8568 | |
2558aacf PZ |
8569 | static inline void balance_push_set(int cpu, bool on) |
8570 | { | |
8571 | } | |
8572 | ||
f2469a1f TG |
8573 | static inline void balance_hotplug_wait(void) |
8574 | { | |
dce48a84 | 8575 | } |
f2469a1f | 8576 | |
1da177e4 LT |
8577 | #endif /* CONFIG_HOTPLUG_CPU */ |
8578 | ||
f2cb1360 | 8579 | void set_rq_online(struct rq *rq) |
1f11eb6a GH |
8580 | { |
8581 | if (!rq->online) { | |
8582 | const struct sched_class *class; | |
8583 | ||
c6c4927b | 8584 | cpumask_set_cpu(rq->cpu, rq->rd->online); |
1f11eb6a GH |
8585 | rq->online = 1; |
8586 | ||
8587 | for_each_class(class) { | |
8588 | if (class->rq_online) | |
8589 | class->rq_online(rq); | |
8590 | } | |
8591 | } | |
8592 | } | |
8593 | ||
f2cb1360 | 8594 | void set_rq_offline(struct rq *rq) |
1f11eb6a GH |
8595 | { |
8596 | if (rq->online) { | |
8597 | const struct sched_class *class; | |
8598 | ||
8599 | for_each_class(class) { | |
8600 | if (class->rq_offline) | |
8601 | class->rq_offline(rq); | |
8602 | } | |
8603 | ||
c6c4927b | 8604 | cpumask_clear_cpu(rq->cpu, rq->rd->online); |
1f11eb6a GH |
8605 | rq->online = 0; |
8606 | } | |
8607 | } | |
8608 | ||
d1ccc66d IM |
8609 | /* |
8610 | * used to mark begin/end of suspend/resume: | |
8611 | */ | |
8612 | static int num_cpus_frozen; | |
d35be8ba | 8613 | |
1da177e4 | 8614 | /* |
3a101d05 TH |
8615 | * Update cpusets according to cpu_active mask. If cpusets are |
8616 | * disabled, cpuset_update_active_cpus() becomes a simple wrapper | |
8617 | * around partition_sched_domains(). | |
d35be8ba SB |
8618 | * |
8619 | * If we come here as part of a suspend/resume, don't touch cpusets because we | |
8620 | * want to restore it back to its original state upon resume anyway. | |
1da177e4 | 8621 | */ |
40190a78 | 8622 | static void cpuset_cpu_active(void) |
e761b772 | 8623 | { |
40190a78 | 8624 | if (cpuhp_tasks_frozen) { |
d35be8ba SB |
8625 | /* |
8626 | * num_cpus_frozen tracks how many CPUs are involved in suspend | |
8627 | * resume sequence. As long as this is not the last online | |
8628 | * operation in the resume sequence, just build a single sched | |
8629 | * domain, ignoring cpusets. | |
8630 | */ | |
50e76632 PZ |
8631 | partition_sched_domains(1, NULL, NULL); |
8632 | if (--num_cpus_frozen) | |
135fb3e1 | 8633 | return; |
d35be8ba SB |
8634 | /* |
8635 | * This is the last CPU online operation. So fall through and | |
8636 | * restore the original sched domains by considering the | |
8637 | * cpuset configurations. | |
8638 | */ | |
50e76632 | 8639 | cpuset_force_rebuild(); |
3a101d05 | 8640 | } |
30e03acd | 8641 | cpuset_update_active_cpus(); |
3a101d05 | 8642 | } |
e761b772 | 8643 | |
40190a78 | 8644 | static int cpuset_cpu_inactive(unsigned int cpu) |
3a101d05 | 8645 | { |
40190a78 | 8646 | if (!cpuhp_tasks_frozen) { |
06a76fe0 | 8647 | if (dl_cpu_busy(cpu)) |
135fb3e1 | 8648 | return -EBUSY; |
30e03acd | 8649 | cpuset_update_active_cpus(); |
135fb3e1 | 8650 | } else { |
d35be8ba SB |
8651 | num_cpus_frozen++; |
8652 | partition_sched_domains(1, NULL, NULL); | |
e761b772 | 8653 | } |
135fb3e1 | 8654 | return 0; |
e761b772 | 8655 | } |
e761b772 | 8656 | |
40190a78 | 8657 | int sched_cpu_activate(unsigned int cpu) |
135fb3e1 | 8658 | { |
7d976699 | 8659 | struct rq *rq = cpu_rq(cpu); |
8a8c69c3 | 8660 | struct rq_flags rf; |
7d976699 | 8661 | |
22f667c9 | 8662 | /* |
b5c44773 PZ |
8663 | * Clear the balance_push callback and prepare to schedule |
8664 | * regular tasks. | |
22f667c9 | 8665 | */ |
2558aacf PZ |
8666 | balance_push_set(cpu, false); |
8667 | ||
ba2591a5 PZ |
8668 | #ifdef CONFIG_SCHED_SMT |
8669 | /* | |
c5511d03 | 8670 | * When going up, increment the number of cores with SMT present. |
ba2591a5 | 8671 | */ |
c5511d03 PZI |
8672 | if (cpumask_weight(cpu_smt_mask(cpu)) == 2) |
8673 | static_branch_inc_cpuslocked(&sched_smt_present); | |
ba2591a5 | 8674 | #endif |
40190a78 | 8675 | set_cpu_active(cpu, true); |
135fb3e1 | 8676 | |
40190a78 | 8677 | if (sched_smp_initialized) { |
135fb3e1 | 8678 | sched_domains_numa_masks_set(cpu); |
40190a78 | 8679 | cpuset_cpu_active(); |
e761b772 | 8680 | } |
7d976699 TG |
8681 | |
8682 | /* | |
8683 | * Put the rq online, if not already. This happens: | |
8684 | * | |
8685 | * 1) In the early boot process, because we build the real domains | |
d1ccc66d | 8686 | * after all CPUs have been brought up. |
7d976699 TG |
8687 | * |
8688 | * 2) At runtime, if cpuset_cpu_active() fails to rebuild the | |
8689 | * domains. | |
8690 | */ | |
8a8c69c3 | 8691 | rq_lock_irqsave(rq, &rf); |
7d976699 TG |
8692 | if (rq->rd) { |
8693 | BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); | |
8694 | set_rq_online(rq); | |
8695 | } | |
8a8c69c3 | 8696 | rq_unlock_irqrestore(rq, &rf); |
7d976699 | 8697 | |
40190a78 | 8698 | return 0; |
135fb3e1 TG |
8699 | } |
8700 | ||
40190a78 | 8701 | int sched_cpu_deactivate(unsigned int cpu) |
135fb3e1 | 8702 | { |
120455c5 PZ |
8703 | struct rq *rq = cpu_rq(cpu); |
8704 | struct rq_flags rf; | |
135fb3e1 TG |
8705 | int ret; |
8706 | ||
e0b257c3 AMB |
8707 | /* |
8708 | * Remove CPU from nohz.idle_cpus_mask to prevent participating in | |
8709 | * load balancing when not active | |
8710 | */ | |
8711 | nohz_balance_exit_idle(rq); | |
8712 | ||
40190a78 | 8713 | set_cpu_active(cpu, false); |
741ba80f PZ |
8714 | |
8715 | /* | |
8716 | * From this point forward, this CPU will refuse to run any task that | |
8717 | * is not: migrate_disable() or KTHREAD_IS_PER_CPU, and will actively | |
8718 | * push those tasks away until this gets cleared, see | |
8719 | * sched_cpu_dying(). | |
8720 | */ | |
975707f2 PZ |
8721 | balance_push_set(cpu, true); |
8722 | ||
b2454caa | 8723 | /* |
975707f2 PZ |
8724 | * We've cleared cpu_active_mask / set balance_push, wait for all |
8725 | * preempt-disabled and RCU users of this state to go away such that | |
8726 | * all new such users will observe it. | |
b2454caa | 8727 | * |
5ba2ffba PZ |
8728 | * Specifically, we rely on ttwu to no longer target this CPU, see |
8729 | * ttwu_queue_cond() and is_cpu_allowed(). | |
8730 | * | |
b2454caa PZ |
8731 | * Do sync before park smpboot threads to take care the rcu boost case. |
8732 | */ | |
309ba859 | 8733 | synchronize_rcu(); |
40190a78 | 8734 | |
120455c5 PZ |
8735 | rq_lock_irqsave(rq, &rf); |
8736 | if (rq->rd) { | |
8737 | update_rq_clock(rq); | |
8738 | BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); | |
8739 | set_rq_offline(rq); | |
8740 | } | |
8741 | rq_unlock_irqrestore(rq, &rf); | |
8742 | ||
c5511d03 PZI |
8743 | #ifdef CONFIG_SCHED_SMT |
8744 | /* | |
8745 | * When going down, decrement the number of cores with SMT present. | |
8746 | */ | |
8747 | if (cpumask_weight(cpu_smt_mask(cpu)) == 2) | |
8748 | static_branch_dec_cpuslocked(&sched_smt_present); | |
8749 | #endif | |
8750 | ||
40190a78 TG |
8751 | if (!sched_smp_initialized) |
8752 | return 0; | |
8753 | ||
8754 | ret = cpuset_cpu_inactive(cpu); | |
8755 | if (ret) { | |
2558aacf | 8756 | balance_push_set(cpu, false); |
40190a78 TG |
8757 | set_cpu_active(cpu, true); |
8758 | return ret; | |
135fb3e1 | 8759 | } |
40190a78 TG |
8760 | sched_domains_numa_masks_clear(cpu); |
8761 | return 0; | |
135fb3e1 TG |
8762 | } |
8763 | ||
94baf7a5 TG |
8764 | static void sched_rq_cpu_starting(unsigned int cpu) |
8765 | { | |
8766 | struct rq *rq = cpu_rq(cpu); | |
8767 | ||
8768 | rq->calc_load_update = calc_load_update; | |
94baf7a5 TG |
8769 | update_max_interval(); |
8770 | } | |
8771 | ||
135fb3e1 TG |
8772 | int sched_cpu_starting(unsigned int cpu) |
8773 | { | |
9edeaea1 | 8774 | sched_core_cpu_starting(cpu); |
94baf7a5 | 8775 | sched_rq_cpu_starting(cpu); |
d84b3131 | 8776 | sched_tick_start(cpu); |
135fb3e1 | 8777 | return 0; |
e761b772 | 8778 | } |
e761b772 | 8779 | |
f2785ddb | 8780 | #ifdef CONFIG_HOTPLUG_CPU |
1cf12e08 TG |
8781 | |
8782 | /* | |
8783 | * Invoked immediately before the stopper thread is invoked to bring the | |
8784 | * CPU down completely. At this point all per CPU kthreads except the | |
8785 | * hotplug thread (current) and the stopper thread (inactive) have been | |
8786 | * either parked or have been unbound from the outgoing CPU. Ensure that | |
8787 | * any of those which might be on the way out are gone. | |
8788 | * | |
8789 | * If after this point a bound task is being woken on this CPU then the | |
8790 | * responsible hotplug callback has failed to do it's job. | |
8791 | * sched_cpu_dying() will catch it with the appropriate fireworks. | |
8792 | */ | |
8793 | int sched_cpu_wait_empty(unsigned int cpu) | |
8794 | { | |
8795 | balance_hotplug_wait(); | |
8796 | return 0; | |
8797 | } | |
8798 | ||
8799 | /* | |
8800 | * Since this CPU is going 'away' for a while, fold any nr_active delta we | |
8801 | * might have. Called from the CPU stopper task after ensuring that the | |
8802 | * stopper is the last running task on the CPU, so nr_active count is | |
8803 | * stable. We need to take the teardown thread which is calling this into | |
8804 | * account, so we hand in adjust = 1 to the load calculation. | |
8805 | * | |
8806 | * Also see the comment "Global load-average calculations". | |
8807 | */ | |
8808 | static void calc_load_migrate(struct rq *rq) | |
8809 | { | |
8810 | long delta = calc_load_fold_active(rq, 1); | |
8811 | ||
8812 | if (delta) | |
8813 | atomic_long_add(delta, &calc_load_tasks); | |
8814 | } | |
8815 | ||
36c6e17b VS |
8816 | static void dump_rq_tasks(struct rq *rq, const char *loglvl) |
8817 | { | |
8818 | struct task_struct *g, *p; | |
8819 | int cpu = cpu_of(rq); | |
8820 | ||
5cb9eaa3 | 8821 | lockdep_assert_rq_held(rq); |
36c6e17b VS |
8822 | |
8823 | printk("%sCPU%d enqueued tasks (%u total):\n", loglvl, cpu, rq->nr_running); | |
8824 | for_each_process_thread(g, p) { | |
8825 | if (task_cpu(p) != cpu) | |
8826 | continue; | |
8827 | ||
8828 | if (!task_on_rq_queued(p)) | |
8829 | continue; | |
8830 | ||
8831 | printk("%s\tpid: %d, name: %s\n", loglvl, p->pid, p->comm); | |
8832 | } | |
8833 | } | |
8834 | ||
f2785ddb TG |
8835 | int sched_cpu_dying(unsigned int cpu) |
8836 | { | |
8837 | struct rq *rq = cpu_rq(cpu); | |
8a8c69c3 | 8838 | struct rq_flags rf; |
f2785ddb TG |
8839 | |
8840 | /* Handle pending wakeups and then migrate everything off */ | |
d84b3131 | 8841 | sched_tick_stop(cpu); |
8a8c69c3 PZ |
8842 | |
8843 | rq_lock_irqsave(rq, &rf); | |
36c6e17b VS |
8844 | if (rq->nr_running != 1 || rq_has_pinned_tasks(rq)) { |
8845 | WARN(true, "Dying CPU not properly vacated!"); | |
8846 | dump_rq_tasks(rq, KERN_WARNING); | |
8847 | } | |
8a8c69c3 PZ |
8848 | rq_unlock_irqrestore(rq, &rf); |
8849 | ||
f2785ddb TG |
8850 | calc_load_migrate(rq); |
8851 | update_max_interval(); | |
e5ef27d0 | 8852 | hrtick_clear(rq); |
f2785ddb TG |
8853 | return 0; |
8854 | } | |
8855 | #endif | |
8856 | ||
1da177e4 LT |
8857 | void __init sched_init_smp(void) |
8858 | { | |
cb83b629 PZ |
8859 | sched_init_numa(); |
8860 | ||
6acce3ef PZ |
8861 | /* |
8862 | * There's no userspace yet to cause hotplug operations; hence all the | |
d1ccc66d | 8863 | * CPU masks are stable and all blatant races in the below code cannot |
b5a4e2bb | 8864 | * happen. |
6acce3ef | 8865 | */ |
712555ee | 8866 | mutex_lock(&sched_domains_mutex); |
8d5dc512 | 8867 | sched_init_domains(cpu_active_mask); |
712555ee | 8868 | mutex_unlock(&sched_domains_mutex); |
e761b772 | 8869 | |
5c1e1767 | 8870 | /* Move init over to a non-isolated CPU */ |
edb93821 | 8871 | if (set_cpus_allowed_ptr(current, housekeeping_cpumask(HK_FLAG_DOMAIN)) < 0) |
5c1e1767 | 8872 | BUG(); |
15faafc6 | 8873 | current->flags &= ~PF_NO_SETAFFINITY; |
19978ca6 | 8874 | sched_init_granularity(); |
4212823f | 8875 | |
0e3900e6 | 8876 | init_sched_rt_class(); |
1baca4ce | 8877 | init_sched_dl_class(); |
1b568f0a | 8878 | |
e26fbffd | 8879 | sched_smp_initialized = true; |
1da177e4 | 8880 | } |
e26fbffd TG |
8881 | |
8882 | static int __init migration_init(void) | |
8883 | { | |
77a5352b | 8884 | sched_cpu_starting(smp_processor_id()); |
e26fbffd | 8885 | return 0; |
1da177e4 | 8886 | } |
e26fbffd TG |
8887 | early_initcall(migration_init); |
8888 | ||
1da177e4 LT |
8889 | #else |
8890 | void __init sched_init_smp(void) | |
8891 | { | |
19978ca6 | 8892 | sched_init_granularity(); |
1da177e4 LT |
8893 | } |
8894 | #endif /* CONFIG_SMP */ | |
8895 | ||
8896 | int in_sched_functions(unsigned long addr) | |
8897 | { | |
1da177e4 LT |
8898 | return in_lock_functions(addr) || |
8899 | (addr >= (unsigned long)__sched_text_start | |
8900 | && addr < (unsigned long)__sched_text_end); | |
8901 | } | |
8902 | ||
029632fb | 8903 | #ifdef CONFIG_CGROUP_SCHED |
27b4b931 LZ |
8904 | /* |
8905 | * Default task group. | |
8906 | * Every task in system belongs to this group at bootup. | |
8907 | */ | |
029632fb | 8908 | struct task_group root_task_group; |
35cf4e50 | 8909 | LIST_HEAD(task_groups); |
b0367629 WL |
8910 | |
8911 | /* Cacheline aligned slab cache for task_group */ | |
8912 | static struct kmem_cache *task_group_cache __read_mostly; | |
052f1dc7 | 8913 | #endif |
6f505b16 | 8914 | |
e6252c3e | 8915 | DECLARE_PER_CPU(cpumask_var_t, load_balance_mask); |
10e2f1ac | 8916 | DECLARE_PER_CPU(cpumask_var_t, select_idle_mask); |
6f505b16 | 8917 | |
1da177e4 LT |
8918 | void __init sched_init(void) |
8919 | { | |
a1dc0446 | 8920 | unsigned long ptr = 0; |
55627e3c | 8921 | int i; |
434d53b0 | 8922 | |
c3a340f7 SRV |
8923 | /* Make sure the linker didn't screw up */ |
8924 | BUG_ON(&idle_sched_class + 1 != &fair_sched_class || | |
8925 | &fair_sched_class + 1 != &rt_sched_class || | |
8926 | &rt_sched_class + 1 != &dl_sched_class); | |
8927 | #ifdef CONFIG_SMP | |
8928 | BUG_ON(&dl_sched_class + 1 != &stop_sched_class); | |
8929 | #endif | |
8930 | ||
5822a454 | 8931 | wait_bit_init(); |
9dcb8b68 | 8932 | |
434d53b0 | 8933 | #ifdef CONFIG_FAIR_GROUP_SCHED |
a1dc0446 | 8934 | ptr += 2 * nr_cpu_ids * sizeof(void **); |
434d53b0 MT |
8935 | #endif |
8936 | #ifdef CONFIG_RT_GROUP_SCHED | |
a1dc0446 | 8937 | ptr += 2 * nr_cpu_ids * sizeof(void **); |
434d53b0 | 8938 | #endif |
a1dc0446 QC |
8939 | if (ptr) { |
8940 | ptr = (unsigned long)kzalloc(ptr, GFP_NOWAIT); | |
434d53b0 MT |
8941 | |
8942 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
07e06b01 | 8943 | root_task_group.se = (struct sched_entity **)ptr; |
434d53b0 MT |
8944 | ptr += nr_cpu_ids * sizeof(void **); |
8945 | ||
07e06b01 | 8946 | root_task_group.cfs_rq = (struct cfs_rq **)ptr; |
434d53b0 | 8947 | ptr += nr_cpu_ids * sizeof(void **); |
eff766a6 | 8948 | |
b1d1779e WY |
8949 | root_task_group.shares = ROOT_TASK_GROUP_LOAD; |
8950 | init_cfs_bandwidth(&root_task_group.cfs_bandwidth); | |
6d6bc0ad | 8951 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
434d53b0 | 8952 | #ifdef CONFIG_RT_GROUP_SCHED |
07e06b01 | 8953 | root_task_group.rt_se = (struct sched_rt_entity **)ptr; |
434d53b0 MT |
8954 | ptr += nr_cpu_ids * sizeof(void **); |
8955 | ||
07e06b01 | 8956 | root_task_group.rt_rq = (struct rt_rq **)ptr; |
eff766a6 PZ |
8957 | ptr += nr_cpu_ids * sizeof(void **); |
8958 | ||
6d6bc0ad | 8959 | #endif /* CONFIG_RT_GROUP_SCHED */ |
b74e6278 | 8960 | } |
df7c8e84 | 8961 | #ifdef CONFIG_CPUMASK_OFFSTACK |
b74e6278 AT |
8962 | for_each_possible_cpu(i) { |
8963 | per_cpu(load_balance_mask, i) = (cpumask_var_t)kzalloc_node( | |
8964 | cpumask_size(), GFP_KERNEL, cpu_to_node(i)); | |
10e2f1ac PZ |
8965 | per_cpu(select_idle_mask, i) = (cpumask_var_t)kzalloc_node( |
8966 | cpumask_size(), GFP_KERNEL, cpu_to_node(i)); | |
434d53b0 | 8967 | } |
b74e6278 | 8968 | #endif /* CONFIG_CPUMASK_OFFSTACK */ |
dd41f596 | 8969 | |
d1ccc66d IM |
8970 | init_rt_bandwidth(&def_rt_bandwidth, global_rt_period(), global_rt_runtime()); |
8971 | init_dl_bandwidth(&def_dl_bandwidth, global_rt_period(), global_rt_runtime()); | |
332ac17e | 8972 | |
57d885fe GH |
8973 | #ifdef CONFIG_SMP |
8974 | init_defrootdomain(); | |
8975 | #endif | |
8976 | ||
d0b27fa7 | 8977 | #ifdef CONFIG_RT_GROUP_SCHED |
07e06b01 | 8978 | init_rt_bandwidth(&root_task_group.rt_bandwidth, |
d0b27fa7 | 8979 | global_rt_period(), global_rt_runtime()); |
6d6bc0ad | 8980 | #endif /* CONFIG_RT_GROUP_SCHED */ |
d0b27fa7 | 8981 | |
7c941438 | 8982 | #ifdef CONFIG_CGROUP_SCHED |
b0367629 WL |
8983 | task_group_cache = KMEM_CACHE(task_group, 0); |
8984 | ||
07e06b01 YZ |
8985 | list_add(&root_task_group.list, &task_groups); |
8986 | INIT_LIST_HEAD(&root_task_group.children); | |
f4d6f6c2 | 8987 | INIT_LIST_HEAD(&root_task_group.siblings); |
5091faa4 | 8988 | autogroup_init(&init_task); |
7c941438 | 8989 | #endif /* CONFIG_CGROUP_SCHED */ |
6f505b16 | 8990 | |
0a945022 | 8991 | for_each_possible_cpu(i) { |
70b97a7f | 8992 | struct rq *rq; |
1da177e4 LT |
8993 | |
8994 | rq = cpu_rq(i); | |
5cb9eaa3 | 8995 | raw_spin_lock_init(&rq->__lock); |
7897986b | 8996 | rq->nr_running = 0; |
dce48a84 TG |
8997 | rq->calc_load_active = 0; |
8998 | rq->calc_load_update = jiffies + LOAD_FREQ; | |
acb5a9ba | 8999 | init_cfs_rq(&rq->cfs); |
07c54f7a AV |
9000 | init_rt_rq(&rq->rt); |
9001 | init_dl_rq(&rq->dl); | |
dd41f596 | 9002 | #ifdef CONFIG_FAIR_GROUP_SCHED |
6f505b16 | 9003 | INIT_LIST_HEAD(&rq->leaf_cfs_rq_list); |
9c2791f9 | 9004 | rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; |
354d60c2 | 9005 | /* |
d1ccc66d | 9006 | * How much CPU bandwidth does root_task_group get? |
354d60c2 DG |
9007 | * |
9008 | * In case of task-groups formed thr' the cgroup filesystem, it | |
d1ccc66d IM |
9009 | * gets 100% of the CPU resources in the system. This overall |
9010 | * system CPU resource is divided among the tasks of | |
07e06b01 | 9011 | * root_task_group and its child task-groups in a fair manner, |
354d60c2 DG |
9012 | * based on each entity's (task or task-group's) weight |
9013 | * (se->load.weight). | |
9014 | * | |
07e06b01 | 9015 | * In other words, if root_task_group has 10 tasks of weight |
354d60c2 | 9016 | * 1024) and two child groups A0 and A1 (of weight 1024 each), |
d1ccc66d | 9017 | * then A0's share of the CPU resource is: |
354d60c2 | 9018 | * |
0d905bca | 9019 | * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33% |
354d60c2 | 9020 | * |
07e06b01 YZ |
9021 | * We achieve this by letting root_task_group's tasks sit |
9022 | * directly in rq->cfs (i.e root_task_group->se[] = NULL). | |
354d60c2 | 9023 | */ |
07e06b01 | 9024 | init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL); |
354d60c2 DG |
9025 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
9026 | ||
9027 | rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime; | |
052f1dc7 | 9028 | #ifdef CONFIG_RT_GROUP_SCHED |
07e06b01 | 9029 | init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL); |
dd41f596 | 9030 | #endif |
1da177e4 | 9031 | #ifdef CONFIG_SMP |
41c7ce9a | 9032 | rq->sd = NULL; |
57d885fe | 9033 | rq->rd = NULL; |
ca6d75e6 | 9034 | rq->cpu_capacity = rq->cpu_capacity_orig = SCHED_CAPACITY_SCALE; |
b5c44773 | 9035 | rq->balance_callback = &balance_push_callback; |
1da177e4 | 9036 | rq->active_balance = 0; |
dd41f596 | 9037 | rq->next_balance = jiffies; |
1da177e4 | 9038 | rq->push_cpu = 0; |
0a2966b4 | 9039 | rq->cpu = i; |
1f11eb6a | 9040 | rq->online = 0; |
eae0c9df MG |
9041 | rq->idle_stamp = 0; |
9042 | rq->avg_idle = 2*sysctl_sched_migration_cost; | |
94aafc3e PZ |
9043 | rq->wake_stamp = jiffies; |
9044 | rq->wake_avg_idle = rq->avg_idle; | |
9bd721c5 | 9045 | rq->max_idle_balance_cost = sysctl_sched_migration_cost; |
367456c7 PZ |
9046 | |
9047 | INIT_LIST_HEAD(&rq->cfs_tasks); | |
9048 | ||
dc938520 | 9049 | rq_attach_root(rq, &def_root_domain); |
3451d024 | 9050 | #ifdef CONFIG_NO_HZ_COMMON |
e022e0d3 | 9051 | rq->last_blocked_load_update_tick = jiffies; |
a22e47a4 | 9052 | atomic_set(&rq->nohz_flags, 0); |
90b5363a | 9053 | |
545b8c8d | 9054 | INIT_CSD(&rq->nohz_csd, nohz_csd_func, rq); |
83cd4fe2 | 9055 | #endif |
f2469a1f TG |
9056 | #ifdef CONFIG_HOTPLUG_CPU |
9057 | rcuwait_init(&rq->hotplug_wait); | |
83cd4fe2 | 9058 | #endif |
9fd81dd5 | 9059 | #endif /* CONFIG_SMP */ |
77a021be | 9060 | hrtick_rq_init(rq); |
1da177e4 | 9061 | atomic_set(&rq->nr_iowait, 0); |
9edeaea1 PZ |
9062 | |
9063 | #ifdef CONFIG_SCHED_CORE | |
9064 | rq->core = NULL; | |
539f6512 | 9065 | rq->core_pick = NULL; |
9edeaea1 | 9066 | rq->core_enabled = 0; |
539f6512 PZ |
9067 | rq->core_tree = RB_ROOT; |
9068 | rq->core_forceidle = false; | |
9069 | ||
9070 | rq->core_cookie = 0UL; | |
9edeaea1 | 9071 | #endif |
1da177e4 LT |
9072 | } |
9073 | ||
9059393e | 9074 | set_load_weight(&init_task, false); |
b50f60ce | 9075 | |
1da177e4 LT |
9076 | /* |
9077 | * The boot idle thread does lazy MMU switching as well: | |
9078 | */ | |
f1f10076 | 9079 | mmgrab(&init_mm); |
1da177e4 LT |
9080 | enter_lazy_tlb(&init_mm, current); |
9081 | ||
9082 | /* | |
9083 | * Make us the idle thread. Technically, schedule() should not be | |
9084 | * called from this thread, however somewhere below it might be, | |
9085 | * but because we are the idle thread, we just pick up running again | |
9086 | * when this runqueue becomes "idle". | |
9087 | */ | |
9088 | init_idle(current, smp_processor_id()); | |
dce48a84 TG |
9089 | |
9090 | calc_load_update = jiffies + LOAD_FREQ; | |
9091 | ||
bf4d83f6 | 9092 | #ifdef CONFIG_SMP |
29d5e047 | 9093 | idle_thread_set_boot_cpu(); |
b5c44773 | 9094 | balance_push_set(smp_processor_id(), false); |
029632fb PZ |
9095 | #endif |
9096 | init_sched_fair_class(); | |
6a7b3dc3 | 9097 | |
eb414681 JW |
9098 | psi_init(); |
9099 | ||
69842cba PB |
9100 | init_uclamp(); |
9101 | ||
6892b75e | 9102 | scheduler_running = 1; |
1da177e4 LT |
9103 | } |
9104 | ||
d902db1e | 9105 | #ifdef CONFIG_DEBUG_ATOMIC_SLEEP |
e4aafea2 FW |
9106 | static inline int preempt_count_equals(int preempt_offset) |
9107 | { | |
da7142e2 | 9108 | int nested = preempt_count() + rcu_preempt_depth(); |
e4aafea2 | 9109 | |
4ba8216c | 9110 | return (nested == preempt_offset); |
e4aafea2 FW |
9111 | } |
9112 | ||
d894837f | 9113 | void __might_sleep(const char *file, int line, int preempt_offset) |
1da177e4 | 9114 | { |
d6c23bb3 | 9115 | unsigned int state = get_current_state(); |
8eb23b9f PZ |
9116 | /* |
9117 | * Blocking primitives will set (and therefore destroy) current->state, | |
9118 | * since we will exit with TASK_RUNNING make sure we enter with it, | |
9119 | * otherwise we will destroy state. | |
9120 | */ | |
d6c23bb3 | 9121 | WARN_ONCE(state != TASK_RUNNING && current->task_state_change, |
8eb23b9f | 9122 | "do not call blocking ops when !TASK_RUNNING; " |
d6c23bb3 | 9123 | "state=%x set at [<%p>] %pS\n", state, |
8eb23b9f | 9124 | (void *)current->task_state_change, |
00845eb9 | 9125 | (void *)current->task_state_change); |
8eb23b9f | 9126 | |
3427445a PZ |
9127 | ___might_sleep(file, line, preempt_offset); |
9128 | } | |
9129 | EXPORT_SYMBOL(__might_sleep); | |
9130 | ||
9131 | void ___might_sleep(const char *file, int line, int preempt_offset) | |
1da177e4 | 9132 | { |
d1ccc66d IM |
9133 | /* Ratelimiting timestamp: */ |
9134 | static unsigned long prev_jiffy; | |
9135 | ||
d1c6d149 | 9136 | unsigned long preempt_disable_ip; |
1da177e4 | 9137 | |
d1ccc66d IM |
9138 | /* WARN_ON_ONCE() by default, no rate limit required: */ |
9139 | rcu_sleep_check(); | |
9140 | ||
db273be2 | 9141 | if ((preempt_count_equals(preempt_offset) && !irqs_disabled() && |
312364f3 | 9142 | !is_idle_task(current) && !current->non_block_count) || |
1c3c5eab TG |
9143 | system_state == SYSTEM_BOOTING || system_state > SYSTEM_RUNNING || |
9144 | oops_in_progress) | |
aef745fc | 9145 | return; |
1c3c5eab | 9146 | |
aef745fc IM |
9147 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) |
9148 | return; | |
9149 | prev_jiffy = jiffies; | |
9150 | ||
d1ccc66d | 9151 | /* Save this before calling printk(), since that will clobber it: */ |
d1c6d149 VN |
9152 | preempt_disable_ip = get_preempt_disable_ip(current); |
9153 | ||
3df0fc5b PZ |
9154 | printk(KERN_ERR |
9155 | "BUG: sleeping function called from invalid context at %s:%d\n", | |
9156 | file, line); | |
9157 | printk(KERN_ERR | |
312364f3 DV |
9158 | "in_atomic(): %d, irqs_disabled(): %d, non_block: %d, pid: %d, name: %s\n", |
9159 | in_atomic(), irqs_disabled(), current->non_block_count, | |
3df0fc5b | 9160 | current->pid, current->comm); |
aef745fc | 9161 | |
a8b686b3 ES |
9162 | if (task_stack_end_corrupted(current)) |
9163 | printk(KERN_EMERG "Thread overran stack, or stack corrupted\n"); | |
9164 | ||
aef745fc IM |
9165 | debug_show_held_locks(current); |
9166 | if (irqs_disabled()) | |
9167 | print_irqtrace_events(current); | |
d1c6d149 VN |
9168 | if (IS_ENABLED(CONFIG_DEBUG_PREEMPT) |
9169 | && !preempt_count_equals(preempt_offset)) { | |
8f47b187 | 9170 | pr_err("Preemption disabled at:"); |
2062a4e8 | 9171 | print_ip_sym(KERN_ERR, preempt_disable_ip); |
8f47b187 | 9172 | } |
aef745fc | 9173 | dump_stack(); |
f0b22e39 | 9174 | add_taint(TAINT_WARN, LOCKDEP_STILL_OK); |
1da177e4 | 9175 | } |
3427445a | 9176 | EXPORT_SYMBOL(___might_sleep); |
568f1967 PZ |
9177 | |
9178 | void __cant_sleep(const char *file, int line, int preempt_offset) | |
9179 | { | |
9180 | static unsigned long prev_jiffy; | |
9181 | ||
9182 | if (irqs_disabled()) | |
9183 | return; | |
9184 | ||
9185 | if (!IS_ENABLED(CONFIG_PREEMPT_COUNT)) | |
9186 | return; | |
9187 | ||
9188 | if (preempt_count() > preempt_offset) | |
9189 | return; | |
9190 | ||
9191 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) | |
9192 | return; | |
9193 | prev_jiffy = jiffies; | |
9194 | ||
9195 | printk(KERN_ERR "BUG: assuming atomic context at %s:%d\n", file, line); | |
9196 | printk(KERN_ERR "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n", | |
9197 | in_atomic(), irqs_disabled(), | |
9198 | current->pid, current->comm); | |
9199 | ||
9200 | debug_show_held_locks(current); | |
9201 | dump_stack(); | |
9202 | add_taint(TAINT_WARN, LOCKDEP_STILL_OK); | |
9203 | } | |
9204 | EXPORT_SYMBOL_GPL(__cant_sleep); | |
74d862b6 TG |
9205 | |
9206 | #ifdef CONFIG_SMP | |
9207 | void __cant_migrate(const char *file, int line) | |
9208 | { | |
9209 | static unsigned long prev_jiffy; | |
9210 | ||
9211 | if (irqs_disabled()) | |
9212 | return; | |
9213 | ||
9214 | if (is_migration_disabled(current)) | |
9215 | return; | |
9216 | ||
9217 | if (!IS_ENABLED(CONFIG_PREEMPT_COUNT)) | |
9218 | return; | |
9219 | ||
9220 | if (preempt_count() > 0) | |
9221 | return; | |
9222 | ||
9223 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) | |
9224 | return; | |
9225 | prev_jiffy = jiffies; | |
9226 | ||
9227 | pr_err("BUG: assuming non migratable context at %s:%d\n", file, line); | |
9228 | pr_err("in_atomic(): %d, irqs_disabled(): %d, migration_disabled() %u pid: %d, name: %s\n", | |
9229 | in_atomic(), irqs_disabled(), is_migration_disabled(current), | |
9230 | current->pid, current->comm); | |
9231 | ||
9232 | debug_show_held_locks(current); | |
9233 | dump_stack(); | |
9234 | add_taint(TAINT_WARN, LOCKDEP_STILL_OK); | |
9235 | } | |
9236 | EXPORT_SYMBOL_GPL(__cant_migrate); | |
9237 | #endif | |
1da177e4 LT |
9238 | #endif |
9239 | ||
9240 | #ifdef CONFIG_MAGIC_SYSRQ | |
dbc7f069 | 9241 | void normalize_rt_tasks(void) |
3a5e4dc1 | 9242 | { |
dbc7f069 | 9243 | struct task_struct *g, *p; |
d50dde5a DF |
9244 | struct sched_attr attr = { |
9245 | .sched_policy = SCHED_NORMAL, | |
9246 | }; | |
1da177e4 | 9247 | |
3472eaa1 | 9248 | read_lock(&tasklist_lock); |
5d07f420 | 9249 | for_each_process_thread(g, p) { |
178be793 IM |
9250 | /* |
9251 | * Only normalize user tasks: | |
9252 | */ | |
3472eaa1 | 9253 | if (p->flags & PF_KTHREAD) |
178be793 IM |
9254 | continue; |
9255 | ||
4fa8d299 JP |
9256 | p->se.exec_start = 0; |
9257 | schedstat_set(p->se.statistics.wait_start, 0); | |
9258 | schedstat_set(p->se.statistics.sleep_start, 0); | |
9259 | schedstat_set(p->se.statistics.block_start, 0); | |
dd41f596 | 9260 | |
aab03e05 | 9261 | if (!dl_task(p) && !rt_task(p)) { |
dd41f596 IM |
9262 | /* |
9263 | * Renice negative nice level userspace | |
9264 | * tasks back to 0: | |
9265 | */ | |
3472eaa1 | 9266 | if (task_nice(p) < 0) |
dd41f596 | 9267 | set_user_nice(p, 0); |
1da177e4 | 9268 | continue; |
dd41f596 | 9269 | } |
1da177e4 | 9270 | |
dbc7f069 | 9271 | __sched_setscheduler(p, &attr, false, false); |
5d07f420 | 9272 | } |
3472eaa1 | 9273 | read_unlock(&tasklist_lock); |
1da177e4 LT |
9274 | } |
9275 | ||
9276 | #endif /* CONFIG_MAGIC_SYSRQ */ | |
1df5c10a | 9277 | |
67fc4e0c | 9278 | #if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) |
1df5c10a | 9279 | /* |
67fc4e0c | 9280 | * These functions are only useful for the IA64 MCA handling, or kdb. |
1df5c10a LT |
9281 | * |
9282 | * They can only be called when the whole system has been | |
9283 | * stopped - every CPU needs to be quiescent, and no scheduling | |
9284 | * activity can take place. Using them for anything else would | |
9285 | * be a serious bug, and as a result, they aren't even visible | |
9286 | * under any other configuration. | |
9287 | */ | |
9288 | ||
9289 | /** | |
d1ccc66d | 9290 | * curr_task - return the current task for a given CPU. |
1df5c10a LT |
9291 | * @cpu: the processor in question. |
9292 | * | |
9293 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! | |
e69f6186 YB |
9294 | * |
9295 | * Return: The current task for @cpu. | |
1df5c10a | 9296 | */ |
36c8b586 | 9297 | struct task_struct *curr_task(int cpu) |
1df5c10a LT |
9298 | { |
9299 | return cpu_curr(cpu); | |
9300 | } | |
9301 | ||
67fc4e0c JW |
9302 | #endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */ |
9303 | ||
9304 | #ifdef CONFIG_IA64 | |
1df5c10a | 9305 | /** |
5feeb783 | 9306 | * ia64_set_curr_task - set the current task for a given CPU. |
1df5c10a LT |
9307 | * @cpu: the processor in question. |
9308 | * @p: the task pointer to set. | |
9309 | * | |
9310 | * Description: This function must only be used when non-maskable interrupts | |
41a2d6cf | 9311 | * are serviced on a separate stack. It allows the architecture to switch the |
d1ccc66d | 9312 | * notion of the current task on a CPU in a non-blocking manner. This function |
1df5c10a LT |
9313 | * must be called with all CPU's synchronized, and interrupts disabled, the |
9314 | * and caller must save the original value of the current task (see | |
9315 | * curr_task() above) and restore that value before reenabling interrupts and | |
9316 | * re-starting the system. | |
9317 | * | |
9318 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! | |
9319 | */ | |
a458ae2e | 9320 | void ia64_set_curr_task(int cpu, struct task_struct *p) |
1df5c10a LT |
9321 | { |
9322 | cpu_curr(cpu) = p; | |
9323 | } | |
9324 | ||
9325 | #endif | |
29f59db3 | 9326 | |
7c941438 | 9327 | #ifdef CONFIG_CGROUP_SCHED |
029632fb PZ |
9328 | /* task_group_lock serializes the addition/removal of task groups */ |
9329 | static DEFINE_SPINLOCK(task_group_lock); | |
9330 | ||
2480c093 PB |
9331 | static inline void alloc_uclamp_sched_group(struct task_group *tg, |
9332 | struct task_group *parent) | |
9333 | { | |
9334 | #ifdef CONFIG_UCLAMP_TASK_GROUP | |
0413d7f3 | 9335 | enum uclamp_id clamp_id; |
2480c093 PB |
9336 | |
9337 | for_each_clamp_id(clamp_id) { | |
9338 | uclamp_se_set(&tg->uclamp_req[clamp_id], | |
9339 | uclamp_none(clamp_id), false); | |
0b60ba2d | 9340 | tg->uclamp[clamp_id] = parent->uclamp[clamp_id]; |
2480c093 PB |
9341 | } |
9342 | #endif | |
9343 | } | |
9344 | ||
2f5177f0 | 9345 | static void sched_free_group(struct task_group *tg) |
bccbe08a PZ |
9346 | { |
9347 | free_fair_sched_group(tg); | |
9348 | free_rt_sched_group(tg); | |
e9aa1dd1 | 9349 | autogroup_free(tg); |
b0367629 | 9350 | kmem_cache_free(task_group_cache, tg); |
bccbe08a PZ |
9351 | } |
9352 | ||
9353 | /* allocate runqueue etc for a new task group */ | |
ec7dc8ac | 9354 | struct task_group *sched_create_group(struct task_group *parent) |
bccbe08a PZ |
9355 | { |
9356 | struct task_group *tg; | |
bccbe08a | 9357 | |
b0367629 | 9358 | tg = kmem_cache_alloc(task_group_cache, GFP_KERNEL | __GFP_ZERO); |
bccbe08a PZ |
9359 | if (!tg) |
9360 | return ERR_PTR(-ENOMEM); | |
9361 | ||
ec7dc8ac | 9362 | if (!alloc_fair_sched_group(tg, parent)) |
bccbe08a PZ |
9363 | goto err; |
9364 | ||
ec7dc8ac | 9365 | if (!alloc_rt_sched_group(tg, parent)) |
bccbe08a PZ |
9366 | goto err; |
9367 | ||
2480c093 PB |
9368 | alloc_uclamp_sched_group(tg, parent); |
9369 | ||
ace783b9 LZ |
9370 | return tg; |
9371 | ||
9372 | err: | |
2f5177f0 | 9373 | sched_free_group(tg); |
ace783b9 LZ |
9374 | return ERR_PTR(-ENOMEM); |
9375 | } | |
9376 | ||
9377 | void sched_online_group(struct task_group *tg, struct task_group *parent) | |
9378 | { | |
9379 | unsigned long flags; | |
9380 | ||
8ed36996 | 9381 | spin_lock_irqsave(&task_group_lock, flags); |
6f505b16 | 9382 | list_add_rcu(&tg->list, &task_groups); |
f473aa5e | 9383 | |
d1ccc66d IM |
9384 | /* Root should already exist: */ |
9385 | WARN_ON(!parent); | |
f473aa5e PZ |
9386 | |
9387 | tg->parent = parent; | |
f473aa5e | 9388 | INIT_LIST_HEAD(&tg->children); |
09f2724a | 9389 | list_add_rcu(&tg->siblings, &parent->children); |
8ed36996 | 9390 | spin_unlock_irqrestore(&task_group_lock, flags); |
8663e24d PZ |
9391 | |
9392 | online_fair_sched_group(tg); | |
29f59db3 SV |
9393 | } |
9394 | ||
9b5b7751 | 9395 | /* rcu callback to free various structures associated with a task group */ |
2f5177f0 | 9396 | static void sched_free_group_rcu(struct rcu_head *rhp) |
29f59db3 | 9397 | { |
d1ccc66d | 9398 | /* Now it should be safe to free those cfs_rqs: */ |
2f5177f0 | 9399 | sched_free_group(container_of(rhp, struct task_group, rcu)); |
29f59db3 SV |
9400 | } |
9401 | ||
4cf86d77 | 9402 | void sched_destroy_group(struct task_group *tg) |
ace783b9 | 9403 | { |
d1ccc66d | 9404 | /* Wait for possible concurrent references to cfs_rqs complete: */ |
2f5177f0 | 9405 | call_rcu(&tg->rcu, sched_free_group_rcu); |
ace783b9 LZ |
9406 | } |
9407 | ||
9408 | void sched_offline_group(struct task_group *tg) | |
29f59db3 | 9409 | { |
8ed36996 | 9410 | unsigned long flags; |
29f59db3 | 9411 | |
d1ccc66d | 9412 | /* End participation in shares distribution: */ |
6fe1f348 | 9413 | unregister_fair_sched_group(tg); |
3d4b47b4 PZ |
9414 | |
9415 | spin_lock_irqsave(&task_group_lock, flags); | |
6f505b16 | 9416 | list_del_rcu(&tg->list); |
f473aa5e | 9417 | list_del_rcu(&tg->siblings); |
8ed36996 | 9418 | spin_unlock_irqrestore(&task_group_lock, flags); |
29f59db3 SV |
9419 | } |
9420 | ||
ea86cb4b | 9421 | static void sched_change_group(struct task_struct *tsk, int type) |
29f59db3 | 9422 | { |
8323f26c | 9423 | struct task_group *tg; |
29f59db3 | 9424 | |
f7b8a47d KT |
9425 | /* |
9426 | * All callers are synchronized by task_rq_lock(); we do not use RCU | |
9427 | * which is pointless here. Thus, we pass "true" to task_css_check() | |
9428 | * to prevent lockdep warnings. | |
9429 | */ | |
9430 | tg = container_of(task_css_check(tsk, cpu_cgrp_id, true), | |
8323f26c PZ |
9431 | struct task_group, css); |
9432 | tg = autogroup_task_group(tsk, tg); | |
9433 | tsk->sched_task_group = tg; | |
9434 | ||
810b3817 | 9435 | #ifdef CONFIG_FAIR_GROUP_SCHED |
ea86cb4b VG |
9436 | if (tsk->sched_class->task_change_group) |
9437 | tsk->sched_class->task_change_group(tsk, type); | |
b2b5ce02 | 9438 | else |
810b3817 | 9439 | #endif |
b2b5ce02 | 9440 | set_task_rq(tsk, task_cpu(tsk)); |
ea86cb4b VG |
9441 | } |
9442 | ||
9443 | /* | |
9444 | * Change task's runqueue when it moves between groups. | |
9445 | * | |
9446 | * The caller of this function should have put the task in its new group by | |
9447 | * now. This function just updates tsk->se.cfs_rq and tsk->se.parent to reflect | |
9448 | * its new group. | |
9449 | */ | |
9450 | void sched_move_task(struct task_struct *tsk) | |
9451 | { | |
7a57f32a PZ |
9452 | int queued, running, queue_flags = |
9453 | DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK; | |
ea86cb4b VG |
9454 | struct rq_flags rf; |
9455 | struct rq *rq; | |
9456 | ||
9457 | rq = task_rq_lock(tsk, &rf); | |
1b1d6225 | 9458 | update_rq_clock(rq); |
ea86cb4b VG |
9459 | |
9460 | running = task_current(rq, tsk); | |
9461 | queued = task_on_rq_queued(tsk); | |
9462 | ||
9463 | if (queued) | |
7a57f32a | 9464 | dequeue_task(rq, tsk, queue_flags); |
bb3bac2c | 9465 | if (running) |
ea86cb4b VG |
9466 | put_prev_task(rq, tsk); |
9467 | ||
9468 | sched_change_group(tsk, TASK_MOVE_GROUP); | |
810b3817 | 9469 | |
da0c1e65 | 9470 | if (queued) |
7a57f32a | 9471 | enqueue_task(rq, tsk, queue_flags); |
2a4b03ff | 9472 | if (running) { |
03b7fad1 | 9473 | set_next_task(rq, tsk); |
2a4b03ff VG |
9474 | /* |
9475 | * After changing group, the running task may have joined a | |
9476 | * throttled one but it's still the running task. Trigger a | |
9477 | * resched to make sure that task can still run. | |
9478 | */ | |
9479 | resched_curr(rq); | |
9480 | } | |
29f59db3 | 9481 | |
eb580751 | 9482 | task_rq_unlock(rq, tsk, &rf); |
29f59db3 | 9483 | } |
68318b8e | 9484 | |
a7c6d554 | 9485 | static inline struct task_group *css_tg(struct cgroup_subsys_state *css) |
68318b8e | 9486 | { |
a7c6d554 | 9487 | return css ? container_of(css, struct task_group, css) : NULL; |
68318b8e SV |
9488 | } |
9489 | ||
eb95419b TH |
9490 | static struct cgroup_subsys_state * |
9491 | cpu_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) | |
68318b8e | 9492 | { |
eb95419b TH |
9493 | struct task_group *parent = css_tg(parent_css); |
9494 | struct task_group *tg; | |
68318b8e | 9495 | |
eb95419b | 9496 | if (!parent) { |
68318b8e | 9497 | /* This is early initialization for the top cgroup */ |
07e06b01 | 9498 | return &root_task_group.css; |
68318b8e SV |
9499 | } |
9500 | ||
ec7dc8ac | 9501 | tg = sched_create_group(parent); |
68318b8e SV |
9502 | if (IS_ERR(tg)) |
9503 | return ERR_PTR(-ENOMEM); | |
9504 | ||
68318b8e SV |
9505 | return &tg->css; |
9506 | } | |
9507 | ||
96b77745 KK |
9508 | /* Expose task group only after completing cgroup initialization */ |
9509 | static int cpu_cgroup_css_online(struct cgroup_subsys_state *css) | |
9510 | { | |
9511 | struct task_group *tg = css_tg(css); | |
9512 | struct task_group *parent = css_tg(css->parent); | |
9513 | ||
9514 | if (parent) | |
9515 | sched_online_group(tg, parent); | |
7226017a QY |
9516 | |
9517 | #ifdef CONFIG_UCLAMP_TASK_GROUP | |
9518 | /* Propagate the effective uclamp value for the new group */ | |
93b73858 QY |
9519 | mutex_lock(&uclamp_mutex); |
9520 | rcu_read_lock(); | |
7226017a | 9521 | cpu_util_update_eff(css); |
93b73858 QY |
9522 | rcu_read_unlock(); |
9523 | mutex_unlock(&uclamp_mutex); | |
7226017a QY |
9524 | #endif |
9525 | ||
96b77745 KK |
9526 | return 0; |
9527 | } | |
9528 | ||
2f5177f0 | 9529 | static void cpu_cgroup_css_released(struct cgroup_subsys_state *css) |
ace783b9 | 9530 | { |
eb95419b | 9531 | struct task_group *tg = css_tg(css); |
ace783b9 | 9532 | |
2f5177f0 | 9533 | sched_offline_group(tg); |
ace783b9 LZ |
9534 | } |
9535 | ||
eb95419b | 9536 | static void cpu_cgroup_css_free(struct cgroup_subsys_state *css) |
68318b8e | 9537 | { |
eb95419b | 9538 | struct task_group *tg = css_tg(css); |
68318b8e | 9539 | |
2f5177f0 PZ |
9540 | /* |
9541 | * Relies on the RCU grace period between css_released() and this. | |
9542 | */ | |
9543 | sched_free_group(tg); | |
ace783b9 LZ |
9544 | } |
9545 | ||
ea86cb4b VG |
9546 | /* |
9547 | * This is called before wake_up_new_task(), therefore we really only | |
9548 | * have to set its group bits, all the other stuff does not apply. | |
9549 | */ | |
b53202e6 | 9550 | static void cpu_cgroup_fork(struct task_struct *task) |
eeb61e53 | 9551 | { |
ea86cb4b VG |
9552 | struct rq_flags rf; |
9553 | struct rq *rq; | |
9554 | ||
9555 | rq = task_rq_lock(task, &rf); | |
9556 | ||
80f5c1b8 | 9557 | update_rq_clock(rq); |
ea86cb4b VG |
9558 | sched_change_group(task, TASK_SET_GROUP); |
9559 | ||
9560 | task_rq_unlock(rq, task, &rf); | |
eeb61e53 KT |
9561 | } |
9562 | ||
1f7dd3e5 | 9563 | static int cpu_cgroup_can_attach(struct cgroup_taskset *tset) |
68318b8e | 9564 | { |
bb9d97b6 | 9565 | struct task_struct *task; |
1f7dd3e5 | 9566 | struct cgroup_subsys_state *css; |
7dc603c9 | 9567 | int ret = 0; |
bb9d97b6 | 9568 | |
1f7dd3e5 | 9569 | cgroup_taskset_for_each(task, css, tset) { |
b68aa230 | 9570 | #ifdef CONFIG_RT_GROUP_SCHED |
eb95419b | 9571 | if (!sched_rt_can_attach(css_tg(css), task)) |
bb9d97b6 | 9572 | return -EINVAL; |
b68aa230 | 9573 | #endif |
7dc603c9 | 9574 | /* |
b19a888c | 9575 | * Serialize against wake_up_new_task() such that if it's |
7dc603c9 PZ |
9576 | * running, we're sure to observe its full state. |
9577 | */ | |
9578 | raw_spin_lock_irq(&task->pi_lock); | |
9579 | /* | |
9580 | * Avoid calling sched_move_task() before wake_up_new_task() | |
9581 | * has happened. This would lead to problems with PELT, due to | |
9582 | * move wanting to detach+attach while we're not attached yet. | |
9583 | */ | |
2f064a59 | 9584 | if (READ_ONCE(task->__state) == TASK_NEW) |
7dc603c9 PZ |
9585 | ret = -EINVAL; |
9586 | raw_spin_unlock_irq(&task->pi_lock); | |
9587 | ||
9588 | if (ret) | |
9589 | break; | |
bb9d97b6 | 9590 | } |
7dc603c9 | 9591 | return ret; |
be367d09 | 9592 | } |
68318b8e | 9593 | |
1f7dd3e5 | 9594 | static void cpu_cgroup_attach(struct cgroup_taskset *tset) |
68318b8e | 9595 | { |
bb9d97b6 | 9596 | struct task_struct *task; |
1f7dd3e5 | 9597 | struct cgroup_subsys_state *css; |
bb9d97b6 | 9598 | |
1f7dd3e5 | 9599 | cgroup_taskset_for_each(task, css, tset) |
bb9d97b6 | 9600 | sched_move_task(task); |
68318b8e SV |
9601 | } |
9602 | ||
2480c093 | 9603 | #ifdef CONFIG_UCLAMP_TASK_GROUP |
0b60ba2d PB |
9604 | static void cpu_util_update_eff(struct cgroup_subsys_state *css) |
9605 | { | |
9606 | struct cgroup_subsys_state *top_css = css; | |
9607 | struct uclamp_se *uc_parent = NULL; | |
9608 | struct uclamp_se *uc_se = NULL; | |
9609 | unsigned int eff[UCLAMP_CNT]; | |
0413d7f3 | 9610 | enum uclamp_id clamp_id; |
0b60ba2d PB |
9611 | unsigned int clamps; |
9612 | ||
93b73858 QY |
9613 | lockdep_assert_held(&uclamp_mutex); |
9614 | SCHED_WARN_ON(!rcu_read_lock_held()); | |
9615 | ||
0b60ba2d PB |
9616 | css_for_each_descendant_pre(css, top_css) { |
9617 | uc_parent = css_tg(css)->parent | |
9618 | ? css_tg(css)->parent->uclamp : NULL; | |
9619 | ||
9620 | for_each_clamp_id(clamp_id) { | |
9621 | /* Assume effective clamps matches requested clamps */ | |
9622 | eff[clamp_id] = css_tg(css)->uclamp_req[clamp_id].value; | |
9623 | /* Cap effective clamps with parent's effective clamps */ | |
9624 | if (uc_parent && | |
9625 | eff[clamp_id] > uc_parent[clamp_id].value) { | |
9626 | eff[clamp_id] = uc_parent[clamp_id].value; | |
9627 | } | |
9628 | } | |
9629 | /* Ensure protection is always capped by limit */ | |
9630 | eff[UCLAMP_MIN] = min(eff[UCLAMP_MIN], eff[UCLAMP_MAX]); | |
9631 | ||
9632 | /* Propagate most restrictive effective clamps */ | |
9633 | clamps = 0x0; | |
9634 | uc_se = css_tg(css)->uclamp; | |
9635 | for_each_clamp_id(clamp_id) { | |
9636 | if (eff[clamp_id] == uc_se[clamp_id].value) | |
9637 | continue; | |
9638 | uc_se[clamp_id].value = eff[clamp_id]; | |
9639 | uc_se[clamp_id].bucket_id = uclamp_bucket_id(eff[clamp_id]); | |
9640 | clamps |= (0x1 << clamp_id); | |
9641 | } | |
babbe170 | 9642 | if (!clamps) { |
0b60ba2d | 9643 | css = css_rightmost_descendant(css); |
babbe170 PB |
9644 | continue; |
9645 | } | |
9646 | ||
9647 | /* Immediately update descendants RUNNABLE tasks */ | |
0213b708 | 9648 | uclamp_update_active_tasks(css); |
0b60ba2d PB |
9649 | } |
9650 | } | |
2480c093 PB |
9651 | |
9652 | /* | |
9653 | * Integer 10^N with a given N exponent by casting to integer the literal "1eN" | |
9654 | * C expression. Since there is no way to convert a macro argument (N) into a | |
9655 | * character constant, use two levels of macros. | |
9656 | */ | |
9657 | #define _POW10(exp) ((unsigned int)1e##exp) | |
9658 | #define POW10(exp) _POW10(exp) | |
9659 | ||
9660 | struct uclamp_request { | |
9661 | #define UCLAMP_PERCENT_SHIFT 2 | |
9662 | #define UCLAMP_PERCENT_SCALE (100 * POW10(UCLAMP_PERCENT_SHIFT)) | |
9663 | s64 percent; | |
9664 | u64 util; | |
9665 | int ret; | |
9666 | }; | |
9667 | ||
9668 | static inline struct uclamp_request | |
9669 | capacity_from_percent(char *buf) | |
9670 | { | |
9671 | struct uclamp_request req = { | |
9672 | .percent = UCLAMP_PERCENT_SCALE, | |
9673 | .util = SCHED_CAPACITY_SCALE, | |
9674 | .ret = 0, | |
9675 | }; | |
9676 | ||
9677 | buf = strim(buf); | |
9678 | if (strcmp(buf, "max")) { | |
9679 | req.ret = cgroup_parse_float(buf, UCLAMP_PERCENT_SHIFT, | |
9680 | &req.percent); | |
9681 | if (req.ret) | |
9682 | return req; | |
b562d140 | 9683 | if ((u64)req.percent > UCLAMP_PERCENT_SCALE) { |
2480c093 PB |
9684 | req.ret = -ERANGE; |
9685 | return req; | |
9686 | } | |
9687 | ||
9688 | req.util = req.percent << SCHED_CAPACITY_SHIFT; | |
9689 | req.util = DIV_ROUND_CLOSEST_ULL(req.util, UCLAMP_PERCENT_SCALE); | |
9690 | } | |
9691 | ||
9692 | return req; | |
9693 | } | |
9694 | ||
9695 | static ssize_t cpu_uclamp_write(struct kernfs_open_file *of, char *buf, | |
9696 | size_t nbytes, loff_t off, | |
9697 | enum uclamp_id clamp_id) | |
9698 | { | |
9699 | struct uclamp_request req; | |
9700 | struct task_group *tg; | |
9701 | ||
9702 | req = capacity_from_percent(buf); | |
9703 | if (req.ret) | |
9704 | return req.ret; | |
9705 | ||
46609ce2 QY |
9706 | static_branch_enable(&sched_uclamp_used); |
9707 | ||
2480c093 PB |
9708 | mutex_lock(&uclamp_mutex); |
9709 | rcu_read_lock(); | |
9710 | ||
9711 | tg = css_tg(of_css(of)); | |
9712 | if (tg->uclamp_req[clamp_id].value != req.util) | |
9713 | uclamp_se_set(&tg->uclamp_req[clamp_id], req.util, false); | |
9714 | ||
9715 | /* | |
9716 | * Because of not recoverable conversion rounding we keep track of the | |
9717 | * exact requested value | |
9718 | */ | |
9719 | tg->uclamp_pct[clamp_id] = req.percent; | |
9720 | ||
0b60ba2d PB |
9721 | /* Update effective clamps to track the most restrictive value */ |
9722 | cpu_util_update_eff(of_css(of)); | |
9723 | ||
2480c093 PB |
9724 | rcu_read_unlock(); |
9725 | mutex_unlock(&uclamp_mutex); | |
9726 | ||
9727 | return nbytes; | |
9728 | } | |
9729 | ||
9730 | static ssize_t cpu_uclamp_min_write(struct kernfs_open_file *of, | |
9731 | char *buf, size_t nbytes, | |
9732 | loff_t off) | |
9733 | { | |
9734 | return cpu_uclamp_write(of, buf, nbytes, off, UCLAMP_MIN); | |
9735 | } | |
9736 | ||
9737 | static ssize_t cpu_uclamp_max_write(struct kernfs_open_file *of, | |
9738 | char *buf, size_t nbytes, | |
9739 | loff_t off) | |
9740 | { | |
9741 | return cpu_uclamp_write(of, buf, nbytes, off, UCLAMP_MAX); | |
9742 | } | |
9743 | ||
9744 | static inline void cpu_uclamp_print(struct seq_file *sf, | |
9745 | enum uclamp_id clamp_id) | |
9746 | { | |
9747 | struct task_group *tg; | |
9748 | u64 util_clamp; | |
9749 | u64 percent; | |
9750 | u32 rem; | |
9751 | ||
9752 | rcu_read_lock(); | |
9753 | tg = css_tg(seq_css(sf)); | |
9754 | util_clamp = tg->uclamp_req[clamp_id].value; | |
9755 | rcu_read_unlock(); | |
9756 | ||
9757 | if (util_clamp == SCHED_CAPACITY_SCALE) { | |
9758 | seq_puts(sf, "max\n"); | |
9759 | return; | |
9760 | } | |
9761 | ||
9762 | percent = tg->uclamp_pct[clamp_id]; | |
9763 | percent = div_u64_rem(percent, POW10(UCLAMP_PERCENT_SHIFT), &rem); | |
9764 | seq_printf(sf, "%llu.%0*u\n", percent, UCLAMP_PERCENT_SHIFT, rem); | |
9765 | } | |
9766 | ||
9767 | static int cpu_uclamp_min_show(struct seq_file *sf, void *v) | |
9768 | { | |
9769 | cpu_uclamp_print(sf, UCLAMP_MIN); | |
9770 | return 0; | |
9771 | } | |
9772 | ||
9773 | static int cpu_uclamp_max_show(struct seq_file *sf, void *v) | |
9774 | { | |
9775 | cpu_uclamp_print(sf, UCLAMP_MAX); | |
9776 | return 0; | |
9777 | } | |
9778 | #endif /* CONFIG_UCLAMP_TASK_GROUP */ | |
9779 | ||
052f1dc7 | 9780 | #ifdef CONFIG_FAIR_GROUP_SCHED |
182446d0 TH |
9781 | static int cpu_shares_write_u64(struct cgroup_subsys_state *css, |
9782 | struct cftype *cftype, u64 shareval) | |
68318b8e | 9783 | { |
5b61d50a KK |
9784 | if (shareval > scale_load_down(ULONG_MAX)) |
9785 | shareval = MAX_SHARES; | |
182446d0 | 9786 | return sched_group_set_shares(css_tg(css), scale_load(shareval)); |
68318b8e SV |
9787 | } |
9788 | ||
182446d0 TH |
9789 | static u64 cpu_shares_read_u64(struct cgroup_subsys_state *css, |
9790 | struct cftype *cft) | |
68318b8e | 9791 | { |
182446d0 | 9792 | struct task_group *tg = css_tg(css); |
68318b8e | 9793 | |
c8b28116 | 9794 | return (u64) scale_load_down(tg->shares); |
68318b8e | 9795 | } |
ab84d31e PT |
9796 | |
9797 | #ifdef CONFIG_CFS_BANDWIDTH | |
a790de99 PT |
9798 | static DEFINE_MUTEX(cfs_constraints_mutex); |
9799 | ||
ab84d31e | 9800 | const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */ |
b1546edc | 9801 | static const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */ |
d505b8af HC |
9802 | /* More than 203 days if BW_SHIFT equals 20. */ |
9803 | static const u64 max_cfs_runtime = MAX_BW * NSEC_PER_USEC; | |
ab84d31e | 9804 | |
a790de99 PT |
9805 | static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime); |
9806 | ||
f4183717 HC |
9807 | static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota, |
9808 | u64 burst) | |
ab84d31e | 9809 | { |
56f570e5 | 9810 | int i, ret = 0, runtime_enabled, runtime_was_enabled; |
029632fb | 9811 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; |
ab84d31e PT |
9812 | |
9813 | if (tg == &root_task_group) | |
9814 | return -EINVAL; | |
9815 | ||
9816 | /* | |
9817 | * Ensure we have at some amount of bandwidth every period. This is | |
9818 | * to prevent reaching a state of large arrears when throttled via | |
9819 | * entity_tick() resulting in prolonged exit starvation. | |
9820 | */ | |
9821 | if (quota < min_cfs_quota_period || period < min_cfs_quota_period) | |
9822 | return -EINVAL; | |
9823 | ||
9824 | /* | |
3b03706f | 9825 | * Likewise, bound things on the other side by preventing insane quota |
ab84d31e PT |
9826 | * periods. This also allows us to normalize in computing quota |
9827 | * feasibility. | |
9828 | */ | |
9829 | if (period > max_cfs_quota_period) | |
9830 | return -EINVAL; | |
9831 | ||
d505b8af HC |
9832 | /* |
9833 | * Bound quota to defend quota against overflow during bandwidth shift. | |
9834 | */ | |
9835 | if (quota != RUNTIME_INF && quota > max_cfs_runtime) | |
9836 | return -EINVAL; | |
9837 | ||
f4183717 HC |
9838 | if (quota != RUNTIME_INF && (burst > quota || |
9839 | burst + quota > max_cfs_runtime)) | |
9840 | return -EINVAL; | |
9841 | ||
0e59bdae KT |
9842 | /* |
9843 | * Prevent race between setting of cfs_rq->runtime_enabled and | |
9844 | * unthrottle_offline_cfs_rqs(). | |
9845 | */ | |
746f5ea9 | 9846 | cpus_read_lock(); |
a790de99 PT |
9847 | mutex_lock(&cfs_constraints_mutex); |
9848 | ret = __cfs_schedulable(tg, period, quota); | |
9849 | if (ret) | |
9850 | goto out_unlock; | |
9851 | ||
58088ad0 | 9852 | runtime_enabled = quota != RUNTIME_INF; |
56f570e5 | 9853 | runtime_was_enabled = cfs_b->quota != RUNTIME_INF; |
1ee14e6c BS |
9854 | /* |
9855 | * If we need to toggle cfs_bandwidth_used, off->on must occur | |
9856 | * before making related changes, and on->off must occur afterwards | |
9857 | */ | |
9858 | if (runtime_enabled && !runtime_was_enabled) | |
9859 | cfs_bandwidth_usage_inc(); | |
ab84d31e PT |
9860 | raw_spin_lock_irq(&cfs_b->lock); |
9861 | cfs_b->period = ns_to_ktime(period); | |
9862 | cfs_b->quota = quota; | |
f4183717 | 9863 | cfs_b->burst = burst; |
58088ad0 | 9864 | |
a9cf55b2 | 9865 | __refill_cfs_bandwidth_runtime(cfs_b); |
d1ccc66d IM |
9866 | |
9867 | /* Restart the period timer (if active) to handle new period expiry: */ | |
77a4d1a1 PZ |
9868 | if (runtime_enabled) |
9869 | start_cfs_bandwidth(cfs_b); | |
d1ccc66d | 9870 | |
ab84d31e PT |
9871 | raw_spin_unlock_irq(&cfs_b->lock); |
9872 | ||
0e59bdae | 9873 | for_each_online_cpu(i) { |
ab84d31e | 9874 | struct cfs_rq *cfs_rq = tg->cfs_rq[i]; |
029632fb | 9875 | struct rq *rq = cfs_rq->rq; |
8a8c69c3 | 9876 | struct rq_flags rf; |
ab84d31e | 9877 | |
8a8c69c3 | 9878 | rq_lock_irq(rq, &rf); |
58088ad0 | 9879 | cfs_rq->runtime_enabled = runtime_enabled; |
ab84d31e | 9880 | cfs_rq->runtime_remaining = 0; |
671fd9da | 9881 | |
029632fb | 9882 | if (cfs_rq->throttled) |
671fd9da | 9883 | unthrottle_cfs_rq(cfs_rq); |
8a8c69c3 | 9884 | rq_unlock_irq(rq, &rf); |
ab84d31e | 9885 | } |
1ee14e6c BS |
9886 | if (runtime_was_enabled && !runtime_enabled) |
9887 | cfs_bandwidth_usage_dec(); | |
a790de99 PT |
9888 | out_unlock: |
9889 | mutex_unlock(&cfs_constraints_mutex); | |
746f5ea9 | 9890 | cpus_read_unlock(); |
ab84d31e | 9891 | |
a790de99 | 9892 | return ret; |
ab84d31e PT |
9893 | } |
9894 | ||
b1546edc | 9895 | static int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us) |
ab84d31e | 9896 | { |
f4183717 | 9897 | u64 quota, period, burst; |
ab84d31e | 9898 | |
029632fb | 9899 | period = ktime_to_ns(tg->cfs_bandwidth.period); |
f4183717 | 9900 | burst = tg->cfs_bandwidth.burst; |
ab84d31e PT |
9901 | if (cfs_quota_us < 0) |
9902 | quota = RUNTIME_INF; | |
1a8b4540 | 9903 | else if ((u64)cfs_quota_us <= U64_MAX / NSEC_PER_USEC) |
ab84d31e | 9904 | quota = (u64)cfs_quota_us * NSEC_PER_USEC; |
1a8b4540 KK |
9905 | else |
9906 | return -EINVAL; | |
ab84d31e | 9907 | |
f4183717 | 9908 | return tg_set_cfs_bandwidth(tg, period, quota, burst); |
ab84d31e PT |
9909 | } |
9910 | ||
b1546edc | 9911 | static long tg_get_cfs_quota(struct task_group *tg) |
ab84d31e PT |
9912 | { |
9913 | u64 quota_us; | |
9914 | ||
029632fb | 9915 | if (tg->cfs_bandwidth.quota == RUNTIME_INF) |
ab84d31e PT |
9916 | return -1; |
9917 | ||
029632fb | 9918 | quota_us = tg->cfs_bandwidth.quota; |
ab84d31e PT |
9919 | do_div(quota_us, NSEC_PER_USEC); |
9920 | ||
9921 | return quota_us; | |
9922 | } | |
9923 | ||
b1546edc | 9924 | static int tg_set_cfs_period(struct task_group *tg, long cfs_period_us) |
ab84d31e | 9925 | { |
f4183717 | 9926 | u64 quota, period, burst; |
ab84d31e | 9927 | |
1a8b4540 KK |
9928 | if ((u64)cfs_period_us > U64_MAX / NSEC_PER_USEC) |
9929 | return -EINVAL; | |
9930 | ||
ab84d31e | 9931 | period = (u64)cfs_period_us * NSEC_PER_USEC; |
029632fb | 9932 | quota = tg->cfs_bandwidth.quota; |
f4183717 | 9933 | burst = tg->cfs_bandwidth.burst; |
ab84d31e | 9934 | |
f4183717 | 9935 | return tg_set_cfs_bandwidth(tg, period, quota, burst); |
ab84d31e PT |
9936 | } |
9937 | ||
b1546edc | 9938 | static long tg_get_cfs_period(struct task_group *tg) |
ab84d31e PT |
9939 | { |
9940 | u64 cfs_period_us; | |
9941 | ||
029632fb | 9942 | cfs_period_us = ktime_to_ns(tg->cfs_bandwidth.period); |
ab84d31e PT |
9943 | do_div(cfs_period_us, NSEC_PER_USEC); |
9944 | ||
9945 | return cfs_period_us; | |
9946 | } | |
9947 | ||
f4183717 HC |
9948 | static int tg_set_cfs_burst(struct task_group *tg, long cfs_burst_us) |
9949 | { | |
9950 | u64 quota, period, burst; | |
9951 | ||
9952 | if ((u64)cfs_burst_us > U64_MAX / NSEC_PER_USEC) | |
9953 | return -EINVAL; | |
9954 | ||
9955 | burst = (u64)cfs_burst_us * NSEC_PER_USEC; | |
9956 | period = ktime_to_ns(tg->cfs_bandwidth.period); | |
9957 | quota = tg->cfs_bandwidth.quota; | |
9958 | ||
9959 | return tg_set_cfs_bandwidth(tg, period, quota, burst); | |
9960 | } | |
9961 | ||
9962 | static long tg_get_cfs_burst(struct task_group *tg) | |
9963 | { | |
9964 | u64 burst_us; | |
9965 | ||
9966 | burst_us = tg->cfs_bandwidth.burst; | |
9967 | do_div(burst_us, NSEC_PER_USEC); | |
9968 | ||
9969 | return burst_us; | |
9970 | } | |
9971 | ||
182446d0 TH |
9972 | static s64 cpu_cfs_quota_read_s64(struct cgroup_subsys_state *css, |
9973 | struct cftype *cft) | |
ab84d31e | 9974 | { |
182446d0 | 9975 | return tg_get_cfs_quota(css_tg(css)); |
ab84d31e PT |
9976 | } |
9977 | ||
182446d0 TH |
9978 | static int cpu_cfs_quota_write_s64(struct cgroup_subsys_state *css, |
9979 | struct cftype *cftype, s64 cfs_quota_us) | |
ab84d31e | 9980 | { |
182446d0 | 9981 | return tg_set_cfs_quota(css_tg(css), cfs_quota_us); |
ab84d31e PT |
9982 | } |
9983 | ||
182446d0 TH |
9984 | static u64 cpu_cfs_period_read_u64(struct cgroup_subsys_state *css, |
9985 | struct cftype *cft) | |
ab84d31e | 9986 | { |
182446d0 | 9987 | return tg_get_cfs_period(css_tg(css)); |
ab84d31e PT |
9988 | } |
9989 | ||
182446d0 TH |
9990 | static int cpu_cfs_period_write_u64(struct cgroup_subsys_state *css, |
9991 | struct cftype *cftype, u64 cfs_period_us) | |
ab84d31e | 9992 | { |
182446d0 | 9993 | return tg_set_cfs_period(css_tg(css), cfs_period_us); |
ab84d31e PT |
9994 | } |
9995 | ||
f4183717 HC |
9996 | static u64 cpu_cfs_burst_read_u64(struct cgroup_subsys_state *css, |
9997 | struct cftype *cft) | |
9998 | { | |
9999 | return tg_get_cfs_burst(css_tg(css)); | |
10000 | } | |
10001 | ||
10002 | static int cpu_cfs_burst_write_u64(struct cgroup_subsys_state *css, | |
10003 | struct cftype *cftype, u64 cfs_burst_us) | |
10004 | { | |
10005 | return tg_set_cfs_burst(css_tg(css), cfs_burst_us); | |
10006 | } | |
10007 | ||
a790de99 PT |
10008 | struct cfs_schedulable_data { |
10009 | struct task_group *tg; | |
10010 | u64 period, quota; | |
10011 | }; | |
10012 | ||
10013 | /* | |
10014 | * normalize group quota/period to be quota/max_period | |
10015 | * note: units are usecs | |
10016 | */ | |
10017 | static u64 normalize_cfs_quota(struct task_group *tg, | |
10018 | struct cfs_schedulable_data *d) | |
10019 | { | |
10020 | u64 quota, period; | |
10021 | ||
10022 | if (tg == d->tg) { | |
10023 | period = d->period; | |
10024 | quota = d->quota; | |
10025 | } else { | |
10026 | period = tg_get_cfs_period(tg); | |
10027 | quota = tg_get_cfs_quota(tg); | |
10028 | } | |
10029 | ||
10030 | /* note: these should typically be equivalent */ | |
10031 | if (quota == RUNTIME_INF || quota == -1) | |
10032 | return RUNTIME_INF; | |
10033 | ||
10034 | return to_ratio(period, quota); | |
10035 | } | |
10036 | ||
10037 | static int tg_cfs_schedulable_down(struct task_group *tg, void *data) | |
10038 | { | |
10039 | struct cfs_schedulable_data *d = data; | |
029632fb | 10040 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; |
a790de99 PT |
10041 | s64 quota = 0, parent_quota = -1; |
10042 | ||
10043 | if (!tg->parent) { | |
10044 | quota = RUNTIME_INF; | |
10045 | } else { | |
029632fb | 10046 | struct cfs_bandwidth *parent_b = &tg->parent->cfs_bandwidth; |
a790de99 PT |
10047 | |
10048 | quota = normalize_cfs_quota(tg, d); | |
9c58c79a | 10049 | parent_quota = parent_b->hierarchical_quota; |
a790de99 PT |
10050 | |
10051 | /* | |
c53593e5 TH |
10052 | * Ensure max(child_quota) <= parent_quota. On cgroup2, |
10053 | * always take the min. On cgroup1, only inherit when no | |
d1ccc66d | 10054 | * limit is set: |
a790de99 | 10055 | */ |
c53593e5 TH |
10056 | if (cgroup_subsys_on_dfl(cpu_cgrp_subsys)) { |
10057 | quota = min(quota, parent_quota); | |
10058 | } else { | |
10059 | if (quota == RUNTIME_INF) | |
10060 | quota = parent_quota; | |
10061 | else if (parent_quota != RUNTIME_INF && quota > parent_quota) | |
10062 | return -EINVAL; | |
10063 | } | |
a790de99 | 10064 | } |
9c58c79a | 10065 | cfs_b->hierarchical_quota = quota; |
a790de99 PT |
10066 | |
10067 | return 0; | |
10068 | } | |
10069 | ||
10070 | static int __cfs_schedulable(struct task_group *tg, u64 period, u64 quota) | |
10071 | { | |
8277434e | 10072 | int ret; |
a790de99 PT |
10073 | struct cfs_schedulable_data data = { |
10074 | .tg = tg, | |
10075 | .period = period, | |
10076 | .quota = quota, | |
10077 | }; | |
10078 | ||
10079 | if (quota != RUNTIME_INF) { | |
10080 | do_div(data.period, NSEC_PER_USEC); | |
10081 | do_div(data.quota, NSEC_PER_USEC); | |
10082 | } | |
10083 | ||
8277434e PT |
10084 | rcu_read_lock(); |
10085 | ret = walk_tg_tree(tg_cfs_schedulable_down, tg_nop, &data); | |
10086 | rcu_read_unlock(); | |
10087 | ||
10088 | return ret; | |
a790de99 | 10089 | } |
e8da1b18 | 10090 | |
a1f7164c | 10091 | static int cpu_cfs_stat_show(struct seq_file *sf, void *v) |
e8da1b18 | 10092 | { |
2da8ca82 | 10093 | struct task_group *tg = css_tg(seq_css(sf)); |
029632fb | 10094 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; |
e8da1b18 | 10095 | |
44ffc75b TH |
10096 | seq_printf(sf, "nr_periods %d\n", cfs_b->nr_periods); |
10097 | seq_printf(sf, "nr_throttled %d\n", cfs_b->nr_throttled); | |
10098 | seq_printf(sf, "throttled_time %llu\n", cfs_b->throttled_time); | |
e8da1b18 | 10099 | |
3d6c50c2 YW |
10100 | if (schedstat_enabled() && tg != &root_task_group) { |
10101 | u64 ws = 0; | |
10102 | int i; | |
10103 | ||
10104 | for_each_possible_cpu(i) | |
10105 | ws += schedstat_val(tg->se[i]->statistics.wait_sum); | |
10106 | ||
10107 | seq_printf(sf, "wait_sum %llu\n", ws); | |
10108 | } | |
10109 | ||
e8da1b18 NR |
10110 | return 0; |
10111 | } | |
ab84d31e | 10112 | #endif /* CONFIG_CFS_BANDWIDTH */ |
6d6bc0ad | 10113 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
68318b8e | 10114 | |
052f1dc7 | 10115 | #ifdef CONFIG_RT_GROUP_SCHED |
182446d0 TH |
10116 | static int cpu_rt_runtime_write(struct cgroup_subsys_state *css, |
10117 | struct cftype *cft, s64 val) | |
6f505b16 | 10118 | { |
182446d0 | 10119 | return sched_group_set_rt_runtime(css_tg(css), val); |
6f505b16 PZ |
10120 | } |
10121 | ||
182446d0 TH |
10122 | static s64 cpu_rt_runtime_read(struct cgroup_subsys_state *css, |
10123 | struct cftype *cft) | |
6f505b16 | 10124 | { |
182446d0 | 10125 | return sched_group_rt_runtime(css_tg(css)); |
6f505b16 | 10126 | } |
d0b27fa7 | 10127 | |
182446d0 TH |
10128 | static int cpu_rt_period_write_uint(struct cgroup_subsys_state *css, |
10129 | struct cftype *cftype, u64 rt_period_us) | |
d0b27fa7 | 10130 | { |
182446d0 | 10131 | return sched_group_set_rt_period(css_tg(css), rt_period_us); |
d0b27fa7 PZ |
10132 | } |
10133 | ||
182446d0 TH |
10134 | static u64 cpu_rt_period_read_uint(struct cgroup_subsys_state *css, |
10135 | struct cftype *cft) | |
d0b27fa7 | 10136 | { |
182446d0 | 10137 | return sched_group_rt_period(css_tg(css)); |
d0b27fa7 | 10138 | } |
6d6bc0ad | 10139 | #endif /* CONFIG_RT_GROUP_SCHED */ |
6f505b16 | 10140 | |
30400039 JD |
10141 | #ifdef CONFIG_FAIR_GROUP_SCHED |
10142 | static s64 cpu_idle_read_s64(struct cgroup_subsys_state *css, | |
10143 | struct cftype *cft) | |
10144 | { | |
10145 | return css_tg(css)->idle; | |
10146 | } | |
10147 | ||
10148 | static int cpu_idle_write_s64(struct cgroup_subsys_state *css, | |
10149 | struct cftype *cft, s64 idle) | |
10150 | { | |
10151 | return sched_group_set_idle(css_tg(css), idle); | |
10152 | } | |
10153 | #endif | |
10154 | ||
a1f7164c | 10155 | static struct cftype cpu_legacy_files[] = { |
052f1dc7 | 10156 | #ifdef CONFIG_FAIR_GROUP_SCHED |
fe5c7cc2 PM |
10157 | { |
10158 | .name = "shares", | |
f4c753b7 PM |
10159 | .read_u64 = cpu_shares_read_u64, |
10160 | .write_u64 = cpu_shares_write_u64, | |
fe5c7cc2 | 10161 | }, |
30400039 JD |
10162 | { |
10163 | .name = "idle", | |
10164 | .read_s64 = cpu_idle_read_s64, | |
10165 | .write_s64 = cpu_idle_write_s64, | |
10166 | }, | |
052f1dc7 | 10167 | #endif |
ab84d31e PT |
10168 | #ifdef CONFIG_CFS_BANDWIDTH |
10169 | { | |
10170 | .name = "cfs_quota_us", | |
10171 | .read_s64 = cpu_cfs_quota_read_s64, | |
10172 | .write_s64 = cpu_cfs_quota_write_s64, | |
10173 | }, | |
10174 | { | |
10175 | .name = "cfs_period_us", | |
10176 | .read_u64 = cpu_cfs_period_read_u64, | |
10177 | .write_u64 = cpu_cfs_period_write_u64, | |
10178 | }, | |
f4183717 HC |
10179 | { |
10180 | .name = "cfs_burst_us", | |
10181 | .read_u64 = cpu_cfs_burst_read_u64, | |
10182 | .write_u64 = cpu_cfs_burst_write_u64, | |
10183 | }, | |
e8da1b18 NR |
10184 | { |
10185 | .name = "stat", | |
a1f7164c | 10186 | .seq_show = cpu_cfs_stat_show, |
e8da1b18 | 10187 | }, |
ab84d31e | 10188 | #endif |
052f1dc7 | 10189 | #ifdef CONFIG_RT_GROUP_SCHED |
6f505b16 | 10190 | { |
9f0c1e56 | 10191 | .name = "rt_runtime_us", |
06ecb27c PM |
10192 | .read_s64 = cpu_rt_runtime_read, |
10193 | .write_s64 = cpu_rt_runtime_write, | |
6f505b16 | 10194 | }, |
d0b27fa7 PZ |
10195 | { |
10196 | .name = "rt_period_us", | |
f4c753b7 PM |
10197 | .read_u64 = cpu_rt_period_read_uint, |
10198 | .write_u64 = cpu_rt_period_write_uint, | |
d0b27fa7 | 10199 | }, |
2480c093 PB |
10200 | #endif |
10201 | #ifdef CONFIG_UCLAMP_TASK_GROUP | |
10202 | { | |
10203 | .name = "uclamp.min", | |
10204 | .flags = CFTYPE_NOT_ON_ROOT, | |
10205 | .seq_show = cpu_uclamp_min_show, | |
10206 | .write = cpu_uclamp_min_write, | |
10207 | }, | |
10208 | { | |
10209 | .name = "uclamp.max", | |
10210 | .flags = CFTYPE_NOT_ON_ROOT, | |
10211 | .seq_show = cpu_uclamp_max_show, | |
10212 | .write = cpu_uclamp_max_write, | |
10213 | }, | |
052f1dc7 | 10214 | #endif |
d1ccc66d | 10215 | { } /* Terminate */ |
68318b8e SV |
10216 | }; |
10217 | ||
d41bf8c9 TH |
10218 | static int cpu_extra_stat_show(struct seq_file *sf, |
10219 | struct cgroup_subsys_state *css) | |
0d593634 | 10220 | { |
0d593634 TH |
10221 | #ifdef CONFIG_CFS_BANDWIDTH |
10222 | { | |
d41bf8c9 | 10223 | struct task_group *tg = css_tg(css); |
0d593634 TH |
10224 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; |
10225 | u64 throttled_usec; | |
10226 | ||
10227 | throttled_usec = cfs_b->throttled_time; | |
10228 | do_div(throttled_usec, NSEC_PER_USEC); | |
10229 | ||
10230 | seq_printf(sf, "nr_periods %d\n" | |
10231 | "nr_throttled %d\n" | |
10232 | "throttled_usec %llu\n", | |
10233 | cfs_b->nr_periods, cfs_b->nr_throttled, | |
10234 | throttled_usec); | |
10235 | } | |
10236 | #endif | |
10237 | return 0; | |
10238 | } | |
10239 | ||
10240 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
10241 | static u64 cpu_weight_read_u64(struct cgroup_subsys_state *css, | |
10242 | struct cftype *cft) | |
10243 | { | |
10244 | struct task_group *tg = css_tg(css); | |
10245 | u64 weight = scale_load_down(tg->shares); | |
10246 | ||
10247 | return DIV_ROUND_CLOSEST_ULL(weight * CGROUP_WEIGHT_DFL, 1024); | |
10248 | } | |
10249 | ||
10250 | static int cpu_weight_write_u64(struct cgroup_subsys_state *css, | |
10251 | struct cftype *cft, u64 weight) | |
10252 | { | |
10253 | /* | |
10254 | * cgroup weight knobs should use the common MIN, DFL and MAX | |
10255 | * values which are 1, 100 and 10000 respectively. While it loses | |
10256 | * a bit of range on both ends, it maps pretty well onto the shares | |
10257 | * value used by scheduler and the round-trip conversions preserve | |
10258 | * the original value over the entire range. | |
10259 | */ | |
10260 | if (weight < CGROUP_WEIGHT_MIN || weight > CGROUP_WEIGHT_MAX) | |
10261 | return -ERANGE; | |
10262 | ||
10263 | weight = DIV_ROUND_CLOSEST_ULL(weight * 1024, CGROUP_WEIGHT_DFL); | |
10264 | ||
10265 | return sched_group_set_shares(css_tg(css), scale_load(weight)); | |
10266 | } | |
10267 | ||
10268 | static s64 cpu_weight_nice_read_s64(struct cgroup_subsys_state *css, | |
10269 | struct cftype *cft) | |
10270 | { | |
10271 | unsigned long weight = scale_load_down(css_tg(css)->shares); | |
10272 | int last_delta = INT_MAX; | |
10273 | int prio, delta; | |
10274 | ||
10275 | /* find the closest nice value to the current weight */ | |
10276 | for (prio = 0; prio < ARRAY_SIZE(sched_prio_to_weight); prio++) { | |
10277 | delta = abs(sched_prio_to_weight[prio] - weight); | |
10278 | if (delta >= last_delta) | |
10279 | break; | |
10280 | last_delta = delta; | |
10281 | } | |
10282 | ||
10283 | return PRIO_TO_NICE(prio - 1 + MAX_RT_PRIO); | |
10284 | } | |
10285 | ||
10286 | static int cpu_weight_nice_write_s64(struct cgroup_subsys_state *css, | |
10287 | struct cftype *cft, s64 nice) | |
10288 | { | |
10289 | unsigned long weight; | |
7281c8de | 10290 | int idx; |
0d593634 TH |
10291 | |
10292 | if (nice < MIN_NICE || nice > MAX_NICE) | |
10293 | return -ERANGE; | |
10294 | ||
7281c8de PZ |
10295 | idx = NICE_TO_PRIO(nice) - MAX_RT_PRIO; |
10296 | idx = array_index_nospec(idx, 40); | |
10297 | weight = sched_prio_to_weight[idx]; | |
10298 | ||
0d593634 TH |
10299 | return sched_group_set_shares(css_tg(css), scale_load(weight)); |
10300 | } | |
10301 | #endif | |
10302 | ||
10303 | static void __maybe_unused cpu_period_quota_print(struct seq_file *sf, | |
10304 | long period, long quota) | |
10305 | { | |
10306 | if (quota < 0) | |
10307 | seq_puts(sf, "max"); | |
10308 | else | |
10309 | seq_printf(sf, "%ld", quota); | |
10310 | ||
10311 | seq_printf(sf, " %ld\n", period); | |
10312 | } | |
10313 | ||
10314 | /* caller should put the current value in *@periodp before calling */ | |
10315 | static int __maybe_unused cpu_period_quota_parse(char *buf, | |
10316 | u64 *periodp, u64 *quotap) | |
10317 | { | |
10318 | char tok[21]; /* U64_MAX */ | |
10319 | ||
4c47acd8 | 10320 | if (sscanf(buf, "%20s %llu", tok, periodp) < 1) |
0d593634 TH |
10321 | return -EINVAL; |
10322 | ||
10323 | *periodp *= NSEC_PER_USEC; | |
10324 | ||
10325 | if (sscanf(tok, "%llu", quotap)) | |
10326 | *quotap *= NSEC_PER_USEC; | |
10327 | else if (!strcmp(tok, "max")) | |
10328 | *quotap = RUNTIME_INF; | |
10329 | else | |
10330 | return -EINVAL; | |
10331 | ||
10332 | return 0; | |
10333 | } | |
10334 | ||
10335 | #ifdef CONFIG_CFS_BANDWIDTH | |
10336 | static int cpu_max_show(struct seq_file *sf, void *v) | |
10337 | { | |
10338 | struct task_group *tg = css_tg(seq_css(sf)); | |
10339 | ||
10340 | cpu_period_quota_print(sf, tg_get_cfs_period(tg), tg_get_cfs_quota(tg)); | |
10341 | return 0; | |
10342 | } | |
10343 | ||
10344 | static ssize_t cpu_max_write(struct kernfs_open_file *of, | |
10345 | char *buf, size_t nbytes, loff_t off) | |
10346 | { | |
10347 | struct task_group *tg = css_tg(of_css(of)); | |
10348 | u64 period = tg_get_cfs_period(tg); | |
f4183717 | 10349 | u64 burst = tg_get_cfs_burst(tg); |
0d593634 TH |
10350 | u64 quota; |
10351 | int ret; | |
10352 | ||
10353 | ret = cpu_period_quota_parse(buf, &period, "a); | |
10354 | if (!ret) | |
f4183717 | 10355 | ret = tg_set_cfs_bandwidth(tg, period, quota, burst); |
0d593634 TH |
10356 | return ret ?: nbytes; |
10357 | } | |
10358 | #endif | |
10359 | ||
10360 | static struct cftype cpu_files[] = { | |
0d593634 TH |
10361 | #ifdef CONFIG_FAIR_GROUP_SCHED |
10362 | { | |
10363 | .name = "weight", | |
10364 | .flags = CFTYPE_NOT_ON_ROOT, | |
10365 | .read_u64 = cpu_weight_read_u64, | |
10366 | .write_u64 = cpu_weight_write_u64, | |
10367 | }, | |
10368 | { | |
10369 | .name = "weight.nice", | |
10370 | .flags = CFTYPE_NOT_ON_ROOT, | |
10371 | .read_s64 = cpu_weight_nice_read_s64, | |
10372 | .write_s64 = cpu_weight_nice_write_s64, | |
10373 | }, | |
30400039 JD |
10374 | { |
10375 | .name = "idle", | |
10376 | .flags = CFTYPE_NOT_ON_ROOT, | |
10377 | .read_s64 = cpu_idle_read_s64, | |
10378 | .write_s64 = cpu_idle_write_s64, | |
10379 | }, | |
0d593634 TH |
10380 | #endif |
10381 | #ifdef CONFIG_CFS_BANDWIDTH | |
10382 | { | |
10383 | .name = "max", | |
10384 | .flags = CFTYPE_NOT_ON_ROOT, | |
10385 | .seq_show = cpu_max_show, | |
10386 | .write = cpu_max_write, | |
10387 | }, | |
f4183717 HC |
10388 | { |
10389 | .name = "max.burst", | |
10390 | .flags = CFTYPE_NOT_ON_ROOT, | |
10391 | .read_u64 = cpu_cfs_burst_read_u64, | |
10392 | .write_u64 = cpu_cfs_burst_write_u64, | |
10393 | }, | |
2480c093 PB |
10394 | #endif |
10395 | #ifdef CONFIG_UCLAMP_TASK_GROUP | |
10396 | { | |
10397 | .name = "uclamp.min", | |
10398 | .flags = CFTYPE_NOT_ON_ROOT, | |
10399 | .seq_show = cpu_uclamp_min_show, | |
10400 | .write = cpu_uclamp_min_write, | |
10401 | }, | |
10402 | { | |
10403 | .name = "uclamp.max", | |
10404 | .flags = CFTYPE_NOT_ON_ROOT, | |
10405 | .seq_show = cpu_uclamp_max_show, | |
10406 | .write = cpu_uclamp_max_write, | |
10407 | }, | |
0d593634 TH |
10408 | #endif |
10409 | { } /* terminate */ | |
10410 | }; | |
10411 | ||
073219e9 | 10412 | struct cgroup_subsys cpu_cgrp_subsys = { |
92fb9748 | 10413 | .css_alloc = cpu_cgroup_css_alloc, |
96b77745 | 10414 | .css_online = cpu_cgroup_css_online, |
2f5177f0 | 10415 | .css_released = cpu_cgroup_css_released, |
92fb9748 | 10416 | .css_free = cpu_cgroup_css_free, |
d41bf8c9 | 10417 | .css_extra_stat_show = cpu_extra_stat_show, |
eeb61e53 | 10418 | .fork = cpu_cgroup_fork, |
bb9d97b6 TH |
10419 | .can_attach = cpu_cgroup_can_attach, |
10420 | .attach = cpu_cgroup_attach, | |
a1f7164c | 10421 | .legacy_cftypes = cpu_legacy_files, |
0d593634 | 10422 | .dfl_cftypes = cpu_files, |
b38e42e9 | 10423 | .early_init = true, |
0d593634 | 10424 | .threaded = true, |
68318b8e SV |
10425 | }; |
10426 | ||
052f1dc7 | 10427 | #endif /* CONFIG_CGROUP_SCHED */ |
d842de87 | 10428 | |
b637a328 PM |
10429 | void dump_cpu_task(int cpu) |
10430 | { | |
10431 | pr_info("Task dump for CPU %d:\n", cpu); | |
10432 | sched_show_task(cpu_curr(cpu)); | |
10433 | } | |
ed82b8a1 AK |
10434 | |
10435 | /* | |
10436 | * Nice levels are multiplicative, with a gentle 10% change for every | |
10437 | * nice level changed. I.e. when a CPU-bound task goes from nice 0 to | |
10438 | * nice 1, it will get ~10% less CPU time than another CPU-bound task | |
10439 | * that remained on nice 0. | |
10440 | * | |
10441 | * The "10% effect" is relative and cumulative: from _any_ nice level, | |
10442 | * if you go up 1 level, it's -10% CPU usage, if you go down 1 level | |
10443 | * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25. | |
10444 | * If a task goes up by ~10% and another task goes down by ~10% then | |
10445 | * the relative distance between them is ~25%.) | |
10446 | */ | |
10447 | const int sched_prio_to_weight[40] = { | |
10448 | /* -20 */ 88761, 71755, 56483, 46273, 36291, | |
10449 | /* -15 */ 29154, 23254, 18705, 14949, 11916, | |
10450 | /* -10 */ 9548, 7620, 6100, 4904, 3906, | |
10451 | /* -5 */ 3121, 2501, 1991, 1586, 1277, | |
10452 | /* 0 */ 1024, 820, 655, 526, 423, | |
10453 | /* 5 */ 335, 272, 215, 172, 137, | |
10454 | /* 10 */ 110, 87, 70, 56, 45, | |
10455 | /* 15 */ 36, 29, 23, 18, 15, | |
10456 | }; | |
10457 | ||
10458 | /* | |
10459 | * Inverse (2^32/x) values of the sched_prio_to_weight[] array, precalculated. | |
10460 | * | |
10461 | * In cases where the weight does not change often, we can use the | |
10462 | * precalculated inverse to speed up arithmetics by turning divisions | |
10463 | * into multiplications: | |
10464 | */ | |
10465 | const u32 sched_prio_to_wmult[40] = { | |
10466 | /* -20 */ 48388, 59856, 76040, 92818, 118348, | |
10467 | /* -15 */ 147320, 184698, 229616, 287308, 360437, | |
10468 | /* -10 */ 449829, 563644, 704093, 875809, 1099582, | |
10469 | /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326, | |
10470 | /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587, | |
10471 | /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126, | |
10472 | /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717, | |
10473 | /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153, | |
10474 | }; | |
14a7405b | 10475 | |
9d246053 PA |
10476 | void call_trace_sched_update_nr_running(struct rq *rq, int count) |
10477 | { | |
10478 | trace_sched_update_nr_running_tp(rq, count); | |
10479 | } |