]>
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 | */ |
325ea10c | 9 | #include "sched.h" |
1da177e4 | 10 | |
7281c8de | 11 | #include <linux/nospec.h> |
85f1abe0 | 12 | |
0ed557aa | 13 | #include <linux/kcov.h> |
d08b9f0c | 14 | #include <linux/scs.h> |
0ed557aa | 15 | |
96f951ed | 16 | #include <asm/switch_to.h> |
5517d86b | 17 | #include <asm/tlb.h> |
1da177e4 | 18 | |
ea138446 | 19 | #include "../workqueue_internal.h" |
771b53d0 | 20 | #include "../../fs/io-wq.h" |
29d5e047 | 21 | #include "../smpboot.h" |
6e0534f2 | 22 | |
91c27493 VG |
23 | #include "pelt.h" |
24 | ||
a8d154b0 | 25 | #define CREATE_TRACE_POINTS |
ad8d75ff | 26 | #include <trace/events/sched.h> |
a8d154b0 | 27 | |
a056a5be QY |
28 | /* |
29 | * Export tracepoints that act as a bare tracehook (ie: have no trace event | |
30 | * associated with them) to allow external modules to probe them. | |
31 | */ | |
32 | EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_cfs_tp); | |
33 | EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_rt_tp); | |
34 | EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_dl_tp); | |
35 | EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_irq_tp); | |
36 | EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_se_tp); | |
37 | EXPORT_TRACEPOINT_SYMBOL_GPL(sched_overutilized_tp); | |
38 | ||
029632fb | 39 | DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues); |
dc61b1d6 | 40 | |
e9666d10 | 41 | #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_JUMP_LABEL) |
bf5c91ba IM |
42 | /* |
43 | * Debugging: various feature bits | |
765cc3a4 PB |
44 | * |
45 | * If SCHED_DEBUG is disabled, each compilation unit has its own copy of | |
46 | * sysctl_sched_features, defined in sched.h, to allow constants propagation | |
47 | * at compile time and compiler optimization based on features default. | |
bf5c91ba | 48 | */ |
f00b45c1 PZ |
49 | #define SCHED_FEAT(name, enabled) \ |
50 | (1UL << __SCHED_FEAT_##name) * enabled | | |
bf5c91ba | 51 | const_debug unsigned int sysctl_sched_features = |
391e43da | 52 | #include "features.h" |
f00b45c1 | 53 | 0; |
f00b45c1 | 54 | #undef SCHED_FEAT |
765cc3a4 | 55 | #endif |
f00b45c1 | 56 | |
b82d9fdd PZ |
57 | /* |
58 | * Number of tasks to iterate in a single balance run. | |
59 | * Limited because this is done with IRQs disabled. | |
60 | */ | |
61 | const_debug unsigned int sysctl_sched_nr_migrate = 32; | |
62 | ||
fa85ae24 | 63 | /* |
d1ccc66d | 64 | * period over which we measure -rt task CPU usage in us. |
fa85ae24 PZ |
65 | * default: 1s |
66 | */ | |
9f0c1e56 | 67 | unsigned int sysctl_sched_rt_period = 1000000; |
fa85ae24 | 68 | |
029632fb | 69 | __read_mostly int scheduler_running; |
6892b75e | 70 | |
9f0c1e56 PZ |
71 | /* |
72 | * part of the period that we allow rt tasks to run in us. | |
73 | * default: 0.95s | |
74 | */ | |
75 | int sysctl_sched_rt_runtime = 950000; | |
fa85ae24 | 76 | |
3e71a462 PZ |
77 | /* |
78 | * __task_rq_lock - lock the rq @p resides on. | |
79 | */ | |
eb580751 | 80 | struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf) |
3e71a462 PZ |
81 | __acquires(rq->lock) |
82 | { | |
83 | struct rq *rq; | |
84 | ||
85 | lockdep_assert_held(&p->pi_lock); | |
86 | ||
87 | for (;;) { | |
88 | rq = task_rq(p); | |
89 | raw_spin_lock(&rq->lock); | |
90 | if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) { | |
d8ac8971 | 91 | rq_pin_lock(rq, rf); |
3e71a462 PZ |
92 | return rq; |
93 | } | |
94 | raw_spin_unlock(&rq->lock); | |
95 | ||
96 | while (unlikely(task_on_rq_migrating(p))) | |
97 | cpu_relax(); | |
98 | } | |
99 | } | |
100 | ||
101 | /* | |
102 | * task_rq_lock - lock p->pi_lock and lock the rq @p resides on. | |
103 | */ | |
eb580751 | 104 | struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf) |
3e71a462 PZ |
105 | __acquires(p->pi_lock) |
106 | __acquires(rq->lock) | |
107 | { | |
108 | struct rq *rq; | |
109 | ||
110 | for (;;) { | |
eb580751 | 111 | raw_spin_lock_irqsave(&p->pi_lock, rf->flags); |
3e71a462 PZ |
112 | rq = task_rq(p); |
113 | raw_spin_lock(&rq->lock); | |
114 | /* | |
115 | * move_queued_task() task_rq_lock() | |
116 | * | |
117 | * ACQUIRE (rq->lock) | |
118 | * [S] ->on_rq = MIGRATING [L] rq = task_rq() | |
119 | * WMB (__set_task_cpu()) ACQUIRE (rq->lock); | |
120 | * [S] ->cpu = new_cpu [L] task_rq() | |
121 | * [L] ->on_rq | |
122 | * RELEASE (rq->lock) | |
123 | * | |
c546951d | 124 | * If we observe the old CPU in task_rq_lock(), the acquire of |
3e71a462 PZ |
125 | * the old rq->lock will fully serialize against the stores. |
126 | * | |
c546951d AP |
127 | * If we observe the new CPU in task_rq_lock(), the address |
128 | * dependency headed by '[L] rq = task_rq()' and the acquire | |
129 | * will pair with the WMB to ensure we then also see migrating. | |
3e71a462 PZ |
130 | */ |
131 | if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) { | |
d8ac8971 | 132 | rq_pin_lock(rq, rf); |
3e71a462 PZ |
133 | return rq; |
134 | } | |
135 | raw_spin_unlock(&rq->lock); | |
eb580751 | 136 | raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags); |
3e71a462 PZ |
137 | |
138 | while (unlikely(task_on_rq_migrating(p))) | |
139 | cpu_relax(); | |
140 | } | |
141 | } | |
142 | ||
535b9552 IM |
143 | /* |
144 | * RQ-clock updating methods: | |
145 | */ | |
146 | ||
147 | static void update_rq_clock_task(struct rq *rq, s64 delta) | |
148 | { | |
149 | /* | |
150 | * In theory, the compile should just see 0 here, and optimize out the call | |
151 | * to sched_rt_avg_update. But I don't trust it... | |
152 | */ | |
11d4afd4 VG |
153 | s64 __maybe_unused steal = 0, irq_delta = 0; |
154 | ||
535b9552 IM |
155 | #ifdef CONFIG_IRQ_TIME_ACCOUNTING |
156 | irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time; | |
157 | ||
158 | /* | |
159 | * Since irq_time is only updated on {soft,}irq_exit, we might run into | |
160 | * this case when a previous update_rq_clock() happened inside a | |
161 | * {soft,}irq region. | |
162 | * | |
163 | * When this happens, we stop ->clock_task and only update the | |
164 | * prev_irq_time stamp to account for the part that fit, so that a next | |
165 | * update will consume the rest. This ensures ->clock_task is | |
166 | * monotonic. | |
167 | * | |
168 | * It does however cause some slight miss-attribution of {soft,}irq | |
169 | * time, a more accurate solution would be to update the irq_time using | |
170 | * the current rq->clock timestamp, except that would require using | |
171 | * atomic ops. | |
172 | */ | |
173 | if (irq_delta > delta) | |
174 | irq_delta = delta; | |
175 | ||
176 | rq->prev_irq_time += irq_delta; | |
177 | delta -= irq_delta; | |
178 | #endif | |
179 | #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING | |
180 | if (static_key_false((¶virt_steal_rq_enabled))) { | |
181 | steal = paravirt_steal_clock(cpu_of(rq)); | |
182 | steal -= rq->prev_steal_time_rq; | |
183 | ||
184 | if (unlikely(steal > delta)) | |
185 | steal = delta; | |
186 | ||
187 | rq->prev_steal_time_rq += steal; | |
188 | delta -= steal; | |
189 | } | |
190 | #endif | |
191 | ||
192 | rq->clock_task += delta; | |
193 | ||
11d4afd4 | 194 | #ifdef CONFIG_HAVE_SCHED_AVG_IRQ |
535b9552 | 195 | if ((irq_delta + steal) && sched_feat(NONTASK_CAPACITY)) |
91c27493 | 196 | update_irq_load_avg(rq, irq_delta + steal); |
535b9552 | 197 | #endif |
23127296 | 198 | update_rq_clock_pelt(rq, delta); |
535b9552 IM |
199 | } |
200 | ||
201 | void update_rq_clock(struct rq *rq) | |
202 | { | |
203 | s64 delta; | |
204 | ||
205 | lockdep_assert_held(&rq->lock); | |
206 | ||
207 | if (rq->clock_update_flags & RQCF_ACT_SKIP) | |
208 | return; | |
209 | ||
210 | #ifdef CONFIG_SCHED_DEBUG | |
26ae58d2 PZ |
211 | if (sched_feat(WARN_DOUBLE_CLOCK)) |
212 | SCHED_WARN_ON(rq->clock_update_flags & RQCF_UPDATED); | |
535b9552 IM |
213 | rq->clock_update_flags |= RQCF_UPDATED; |
214 | #endif | |
26ae58d2 | 215 | |
535b9552 IM |
216 | delta = sched_clock_cpu(cpu_of(rq)) - rq->clock; |
217 | if (delta < 0) | |
218 | return; | |
219 | rq->clock += delta; | |
220 | update_rq_clock_task(rq, delta); | |
221 | } | |
222 | ||
223 | ||
8f4d37ec PZ |
224 | #ifdef CONFIG_SCHED_HRTICK |
225 | /* | |
226 | * Use HR-timers to deliver accurate preemption points. | |
8f4d37ec | 227 | */ |
8f4d37ec | 228 | |
8f4d37ec PZ |
229 | static void hrtick_clear(struct rq *rq) |
230 | { | |
231 | if (hrtimer_active(&rq->hrtick_timer)) | |
232 | hrtimer_cancel(&rq->hrtick_timer); | |
233 | } | |
234 | ||
8f4d37ec PZ |
235 | /* |
236 | * High-resolution timer tick. | |
237 | * Runs from hardirq context with interrupts disabled. | |
238 | */ | |
239 | static enum hrtimer_restart hrtick(struct hrtimer *timer) | |
240 | { | |
241 | struct rq *rq = container_of(timer, struct rq, hrtick_timer); | |
8a8c69c3 | 242 | struct rq_flags rf; |
8f4d37ec PZ |
243 | |
244 | WARN_ON_ONCE(cpu_of(rq) != smp_processor_id()); | |
245 | ||
8a8c69c3 | 246 | rq_lock(rq, &rf); |
3e51f33f | 247 | update_rq_clock(rq); |
8f4d37ec | 248 | rq->curr->sched_class->task_tick(rq, rq->curr, 1); |
8a8c69c3 | 249 | rq_unlock(rq, &rf); |
8f4d37ec PZ |
250 | |
251 | return HRTIMER_NORESTART; | |
252 | } | |
253 | ||
95e904c7 | 254 | #ifdef CONFIG_SMP |
971ee28c | 255 | |
4961b6e1 | 256 | static void __hrtick_restart(struct rq *rq) |
971ee28c PZ |
257 | { |
258 | struct hrtimer *timer = &rq->hrtick_timer; | |
971ee28c | 259 | |
d5096aa6 | 260 | hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED_HARD); |
971ee28c PZ |
261 | } |
262 | ||
31656519 PZ |
263 | /* |
264 | * called from hardirq (IPI) context | |
265 | */ | |
266 | static void __hrtick_start(void *arg) | |
b328ca18 | 267 | { |
31656519 | 268 | struct rq *rq = arg; |
8a8c69c3 | 269 | struct rq_flags rf; |
b328ca18 | 270 | |
8a8c69c3 | 271 | rq_lock(rq, &rf); |
971ee28c | 272 | __hrtick_restart(rq); |
8a8c69c3 | 273 | rq_unlock(rq, &rf); |
b328ca18 PZ |
274 | } |
275 | ||
31656519 PZ |
276 | /* |
277 | * Called to set the hrtick timer state. | |
278 | * | |
279 | * called with rq->lock held and irqs disabled | |
280 | */ | |
029632fb | 281 | void hrtick_start(struct rq *rq, u64 delay) |
b328ca18 | 282 | { |
31656519 | 283 | struct hrtimer *timer = &rq->hrtick_timer; |
177ef2a6 | 284 | ktime_t time; |
285 | s64 delta; | |
286 | ||
287 | /* | |
288 | * Don't schedule slices shorter than 10000ns, that just | |
289 | * doesn't make sense and can cause timer DoS. | |
290 | */ | |
291 | delta = max_t(s64, delay, 10000LL); | |
292 | time = ktime_add_ns(timer->base->get_time(), delta); | |
b328ca18 | 293 | |
cc584b21 | 294 | hrtimer_set_expires(timer, time); |
31656519 | 295 | |
fd3eafda | 296 | if (rq == this_rq()) |
971ee28c | 297 | __hrtick_restart(rq); |
fd3eafda | 298 | else |
c46fff2a | 299 | smp_call_function_single_async(cpu_of(rq), &rq->hrtick_csd); |
b328ca18 PZ |
300 | } |
301 | ||
31656519 PZ |
302 | #else |
303 | /* | |
304 | * Called to set the hrtick timer state. | |
305 | * | |
306 | * called with rq->lock held and irqs disabled | |
307 | */ | |
029632fb | 308 | void hrtick_start(struct rq *rq, u64 delay) |
31656519 | 309 | { |
86893335 WL |
310 | /* |
311 | * Don't schedule slices shorter than 10000ns, that just | |
312 | * doesn't make sense. Rely on vruntime for fairness. | |
313 | */ | |
314 | delay = max_t(u64, delay, 10000LL); | |
4961b6e1 | 315 | hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay), |
d5096aa6 | 316 | HRTIMER_MODE_REL_PINNED_HARD); |
31656519 | 317 | } |
31656519 | 318 | #endif /* CONFIG_SMP */ |
8f4d37ec | 319 | |
77a021be | 320 | static void hrtick_rq_init(struct rq *rq) |
8f4d37ec | 321 | { |
31656519 | 322 | #ifdef CONFIG_SMP |
31656519 PZ |
323 | rq->hrtick_csd.flags = 0; |
324 | rq->hrtick_csd.func = __hrtick_start; | |
325 | rq->hrtick_csd.info = rq; | |
326 | #endif | |
8f4d37ec | 327 | |
d5096aa6 | 328 | hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); |
31656519 | 329 | rq->hrtick_timer.function = hrtick; |
8f4d37ec | 330 | } |
006c75f1 | 331 | #else /* CONFIG_SCHED_HRTICK */ |
8f4d37ec PZ |
332 | static inline void hrtick_clear(struct rq *rq) |
333 | { | |
334 | } | |
335 | ||
77a021be | 336 | static inline void hrtick_rq_init(struct rq *rq) |
8f4d37ec PZ |
337 | { |
338 | } | |
006c75f1 | 339 | #endif /* CONFIG_SCHED_HRTICK */ |
8f4d37ec | 340 | |
5529578a FW |
341 | /* |
342 | * cmpxchg based fetch_or, macro so it works for different integer types | |
343 | */ | |
344 | #define fetch_or(ptr, mask) \ | |
345 | ({ \ | |
346 | typeof(ptr) _ptr = (ptr); \ | |
347 | typeof(mask) _mask = (mask); \ | |
348 | typeof(*_ptr) _old, _val = *_ptr; \ | |
349 | \ | |
350 | for (;;) { \ | |
351 | _old = cmpxchg(_ptr, _val, _val | _mask); \ | |
352 | if (_old == _val) \ | |
353 | break; \ | |
354 | _val = _old; \ | |
355 | } \ | |
356 | _old; \ | |
357 | }) | |
358 | ||
e3baac47 | 359 | #if defined(CONFIG_SMP) && defined(TIF_POLLING_NRFLAG) |
fd99f91a PZ |
360 | /* |
361 | * Atomically set TIF_NEED_RESCHED and test for TIF_POLLING_NRFLAG, | |
362 | * this avoids any races wrt polling state changes and thereby avoids | |
363 | * spurious IPIs. | |
364 | */ | |
365 | static bool set_nr_and_not_polling(struct task_struct *p) | |
366 | { | |
367 | struct thread_info *ti = task_thread_info(p); | |
368 | return !(fetch_or(&ti->flags, _TIF_NEED_RESCHED) & _TIF_POLLING_NRFLAG); | |
369 | } | |
e3baac47 PZ |
370 | |
371 | /* | |
372 | * Atomically set TIF_NEED_RESCHED if TIF_POLLING_NRFLAG is set. | |
373 | * | |
374 | * If this returns true, then the idle task promises to call | |
375 | * sched_ttwu_pending() and reschedule soon. | |
376 | */ | |
377 | static bool set_nr_if_polling(struct task_struct *p) | |
378 | { | |
379 | struct thread_info *ti = task_thread_info(p); | |
316c1608 | 380 | typeof(ti->flags) old, val = READ_ONCE(ti->flags); |
e3baac47 PZ |
381 | |
382 | for (;;) { | |
383 | if (!(val & _TIF_POLLING_NRFLAG)) | |
384 | return false; | |
385 | if (val & _TIF_NEED_RESCHED) | |
386 | return true; | |
387 | old = cmpxchg(&ti->flags, val, val | _TIF_NEED_RESCHED); | |
388 | if (old == val) | |
389 | break; | |
390 | val = old; | |
391 | } | |
392 | return true; | |
393 | } | |
394 | ||
fd99f91a PZ |
395 | #else |
396 | static bool set_nr_and_not_polling(struct task_struct *p) | |
397 | { | |
398 | set_tsk_need_resched(p); | |
399 | return true; | |
400 | } | |
e3baac47 PZ |
401 | |
402 | #ifdef CONFIG_SMP | |
403 | static bool set_nr_if_polling(struct task_struct *p) | |
404 | { | |
405 | return false; | |
406 | } | |
407 | #endif | |
fd99f91a PZ |
408 | #endif |
409 | ||
07879c6a | 410 | static bool __wake_q_add(struct wake_q_head *head, struct task_struct *task) |
76751049 PZ |
411 | { |
412 | struct wake_q_node *node = &task->wake_q; | |
413 | ||
414 | /* | |
415 | * Atomically grab the task, if ->wake_q is !nil already it means | |
416 | * its already queued (either by us or someone else) and will get the | |
417 | * wakeup due to that. | |
418 | * | |
4c4e3731 PZ |
419 | * In order to ensure that a pending wakeup will observe our pending |
420 | * state, even in the failed case, an explicit smp_mb() must be used. | |
76751049 | 421 | */ |
4c4e3731 | 422 | smp_mb__before_atomic(); |
87ff19cb | 423 | if (unlikely(cmpxchg_relaxed(&node->next, NULL, WAKE_Q_TAIL))) |
07879c6a | 424 | return false; |
76751049 PZ |
425 | |
426 | /* | |
427 | * The head is context local, there can be no concurrency. | |
428 | */ | |
429 | *head->lastp = node; | |
430 | head->lastp = &node->next; | |
07879c6a DB |
431 | return true; |
432 | } | |
433 | ||
434 | /** | |
435 | * wake_q_add() - queue a wakeup for 'later' waking. | |
436 | * @head: the wake_q_head to add @task to | |
437 | * @task: the task to queue for 'later' wakeup | |
438 | * | |
439 | * Queue a task for later wakeup, most likely by the wake_up_q() call in the | |
440 | * same context, _HOWEVER_ this is not guaranteed, the wakeup can come | |
441 | * instantly. | |
442 | * | |
443 | * This function must be used as-if it were wake_up_process(); IOW the task | |
444 | * must be ready to be woken at this location. | |
445 | */ | |
446 | void wake_q_add(struct wake_q_head *head, struct task_struct *task) | |
447 | { | |
448 | if (__wake_q_add(head, task)) | |
449 | get_task_struct(task); | |
450 | } | |
451 | ||
452 | /** | |
453 | * wake_q_add_safe() - safely queue a wakeup for 'later' waking. | |
454 | * @head: the wake_q_head to add @task to | |
455 | * @task: the task to queue for 'later' wakeup | |
456 | * | |
457 | * Queue a task for later wakeup, most likely by the wake_up_q() call in the | |
458 | * same context, _HOWEVER_ this is not guaranteed, the wakeup can come | |
459 | * instantly. | |
460 | * | |
461 | * This function must be used as-if it were wake_up_process(); IOW the task | |
462 | * must be ready to be woken at this location. | |
463 | * | |
464 | * This function is essentially a task-safe equivalent to wake_q_add(). Callers | |
465 | * that already hold reference to @task can call the 'safe' version and trust | |
466 | * wake_q to do the right thing depending whether or not the @task is already | |
467 | * queued for wakeup. | |
468 | */ | |
469 | void wake_q_add_safe(struct wake_q_head *head, struct task_struct *task) | |
470 | { | |
471 | if (!__wake_q_add(head, task)) | |
472 | put_task_struct(task); | |
76751049 PZ |
473 | } |
474 | ||
475 | void wake_up_q(struct wake_q_head *head) | |
476 | { | |
477 | struct wake_q_node *node = head->first; | |
478 | ||
479 | while (node != WAKE_Q_TAIL) { | |
480 | struct task_struct *task; | |
481 | ||
482 | task = container_of(node, struct task_struct, wake_q); | |
483 | BUG_ON(!task); | |
d1ccc66d | 484 | /* Task can safely be re-inserted now: */ |
76751049 PZ |
485 | node = node->next; |
486 | task->wake_q.next = NULL; | |
487 | ||
488 | /* | |
7696f991 AP |
489 | * wake_up_process() executes a full barrier, which pairs with |
490 | * the queueing in wake_q_add() so as not to miss wakeups. | |
76751049 PZ |
491 | */ |
492 | wake_up_process(task); | |
493 | put_task_struct(task); | |
494 | } | |
495 | } | |
496 | ||
c24d20db | 497 | /* |
8875125e | 498 | * resched_curr - mark rq's current task 'to be rescheduled now'. |
c24d20db IM |
499 | * |
500 | * On UP this means the setting of the need_resched flag, on SMP it | |
501 | * might also involve a cross-CPU call to trigger the scheduler on | |
502 | * the target CPU. | |
503 | */ | |
8875125e | 504 | void resched_curr(struct rq *rq) |
c24d20db | 505 | { |
8875125e | 506 | struct task_struct *curr = rq->curr; |
c24d20db IM |
507 | int cpu; |
508 | ||
8875125e | 509 | lockdep_assert_held(&rq->lock); |
c24d20db | 510 | |
8875125e | 511 | if (test_tsk_need_resched(curr)) |
c24d20db IM |
512 | return; |
513 | ||
8875125e | 514 | cpu = cpu_of(rq); |
fd99f91a | 515 | |
f27dde8d | 516 | if (cpu == smp_processor_id()) { |
8875125e | 517 | set_tsk_need_resched(curr); |
f27dde8d | 518 | set_preempt_need_resched(); |
c24d20db | 519 | return; |
f27dde8d | 520 | } |
c24d20db | 521 | |
8875125e | 522 | if (set_nr_and_not_polling(curr)) |
c24d20db | 523 | smp_send_reschedule(cpu); |
dfc68f29 AL |
524 | else |
525 | trace_sched_wake_idle_without_ipi(cpu); | |
c24d20db IM |
526 | } |
527 | ||
029632fb | 528 | void resched_cpu(int cpu) |
c24d20db IM |
529 | { |
530 | struct rq *rq = cpu_rq(cpu); | |
531 | unsigned long flags; | |
532 | ||
7c2102e5 | 533 | raw_spin_lock_irqsave(&rq->lock, flags); |
a0982dfa PM |
534 | if (cpu_online(cpu) || cpu == smp_processor_id()) |
535 | resched_curr(rq); | |
05fa785c | 536 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
c24d20db | 537 | } |
06d8308c | 538 | |
b021fe3e | 539 | #ifdef CONFIG_SMP |
3451d024 | 540 | #ifdef CONFIG_NO_HZ_COMMON |
83cd4fe2 | 541 | /* |
d1ccc66d IM |
542 | * In the semi idle case, use the nearest busy CPU for migrating timers |
543 | * from an idle CPU. This is good for power-savings. | |
83cd4fe2 VP |
544 | * |
545 | * We don't do similar optimization for completely idle system, as | |
d1ccc66d IM |
546 | * selecting an idle CPU will add more delays to the timers than intended |
547 | * (as that CPU's timer base may not be uptodate wrt jiffies etc). | |
83cd4fe2 | 548 | */ |
bc7a34b8 | 549 | int get_nohz_timer_target(void) |
83cd4fe2 | 550 | { |
e938b9c9 | 551 | int i, cpu = smp_processor_id(), default_cpu = -1; |
83cd4fe2 VP |
552 | struct sched_domain *sd; |
553 | ||
e938b9c9 WL |
554 | if (housekeeping_cpu(cpu, HK_FLAG_TIMER)) { |
555 | if (!idle_cpu(cpu)) | |
556 | return cpu; | |
557 | default_cpu = cpu; | |
558 | } | |
6201b4d6 | 559 | |
057f3fad | 560 | rcu_read_lock(); |
83cd4fe2 | 561 | for_each_domain(cpu, sd) { |
e938b9c9 WL |
562 | for_each_cpu_and(i, sched_domain_span(sd), |
563 | housekeeping_cpumask(HK_FLAG_TIMER)) { | |
44496922 WL |
564 | if (cpu == i) |
565 | continue; | |
566 | ||
e938b9c9 | 567 | if (!idle_cpu(i)) { |
057f3fad PZ |
568 | cpu = i; |
569 | goto unlock; | |
570 | } | |
571 | } | |
83cd4fe2 | 572 | } |
9642d18e | 573 | |
e938b9c9 WL |
574 | if (default_cpu == -1) |
575 | default_cpu = housekeeping_any_cpu(HK_FLAG_TIMER); | |
576 | cpu = default_cpu; | |
057f3fad PZ |
577 | unlock: |
578 | rcu_read_unlock(); | |
83cd4fe2 VP |
579 | return cpu; |
580 | } | |
d1ccc66d | 581 | |
06d8308c TG |
582 | /* |
583 | * When add_timer_on() enqueues a timer into the timer wheel of an | |
584 | * idle CPU then this timer might expire before the next timer event | |
585 | * which is scheduled to wake up that CPU. In case of a completely | |
586 | * idle system the next event might even be infinite time into the | |
587 | * future. wake_up_idle_cpu() ensures that the CPU is woken up and | |
588 | * leaves the inner idle loop so the newly added timer is taken into | |
589 | * account when the CPU goes back to idle and evaluates the timer | |
590 | * wheel for the next timer event. | |
591 | */ | |
1c20091e | 592 | static void wake_up_idle_cpu(int cpu) |
06d8308c TG |
593 | { |
594 | struct rq *rq = cpu_rq(cpu); | |
595 | ||
596 | if (cpu == smp_processor_id()) | |
597 | return; | |
598 | ||
67b9ca70 | 599 | if (set_nr_and_not_polling(rq->idle)) |
06d8308c | 600 | smp_send_reschedule(cpu); |
dfc68f29 AL |
601 | else |
602 | trace_sched_wake_idle_without_ipi(cpu); | |
45bf76df IM |
603 | } |
604 | ||
c5bfece2 | 605 | static bool wake_up_full_nohz_cpu(int cpu) |
1c20091e | 606 | { |
53c5fa16 FW |
607 | /* |
608 | * We just need the target to call irq_exit() and re-evaluate | |
609 | * the next tick. The nohz full kick at least implies that. | |
610 | * If needed we can still optimize that later with an | |
611 | * empty IRQ. | |
612 | */ | |
379d9ecb PM |
613 | if (cpu_is_offline(cpu)) |
614 | return true; /* Don't try to wake offline CPUs. */ | |
c5bfece2 | 615 | if (tick_nohz_full_cpu(cpu)) { |
1c20091e FW |
616 | if (cpu != smp_processor_id() || |
617 | tick_nohz_tick_stopped()) | |
53c5fa16 | 618 | tick_nohz_full_kick_cpu(cpu); |
1c20091e FW |
619 | return true; |
620 | } | |
621 | ||
622 | return false; | |
623 | } | |
624 | ||
379d9ecb PM |
625 | /* |
626 | * Wake up the specified CPU. If the CPU is going offline, it is the | |
627 | * caller's responsibility to deal with the lost wakeup, for example, | |
628 | * by hooking into the CPU_DEAD notifier like timers and hrtimers do. | |
629 | */ | |
1c20091e FW |
630 | void wake_up_nohz_cpu(int cpu) |
631 | { | |
c5bfece2 | 632 | if (!wake_up_full_nohz_cpu(cpu)) |
1c20091e FW |
633 | wake_up_idle_cpu(cpu); |
634 | } | |
635 | ||
ca38062e | 636 | static inline bool got_nohz_idle_kick(void) |
45bf76df | 637 | { |
1c792db7 | 638 | int cpu = smp_processor_id(); |
873b4c65 | 639 | |
b7031a02 | 640 | if (!(atomic_read(nohz_flags(cpu)) & NOHZ_KICK_MASK)) |
873b4c65 VG |
641 | return false; |
642 | ||
643 | if (idle_cpu(cpu) && !need_resched()) | |
644 | return true; | |
645 | ||
646 | /* | |
647 | * We can't run Idle Load Balance on this CPU for this time so we | |
648 | * cancel it and clear NOHZ_BALANCE_KICK | |
649 | */ | |
b7031a02 | 650 | atomic_andnot(NOHZ_KICK_MASK, nohz_flags(cpu)); |
873b4c65 | 651 | return false; |
45bf76df IM |
652 | } |
653 | ||
3451d024 | 654 | #else /* CONFIG_NO_HZ_COMMON */ |
45bf76df | 655 | |
ca38062e | 656 | static inline bool got_nohz_idle_kick(void) |
2069dd75 | 657 | { |
ca38062e | 658 | return false; |
2069dd75 PZ |
659 | } |
660 | ||
3451d024 | 661 | #endif /* CONFIG_NO_HZ_COMMON */ |
d842de87 | 662 | |
ce831b38 | 663 | #ifdef CONFIG_NO_HZ_FULL |
76d92ac3 | 664 | bool sched_can_stop_tick(struct rq *rq) |
ce831b38 | 665 | { |
76d92ac3 FW |
666 | int fifo_nr_running; |
667 | ||
668 | /* Deadline tasks, even if single, need the tick */ | |
669 | if (rq->dl.dl_nr_running) | |
670 | return false; | |
671 | ||
1e78cdbd | 672 | /* |
2548d546 PZ |
673 | * If there are more than one RR tasks, we need the tick to effect the |
674 | * actual RR behaviour. | |
1e78cdbd | 675 | */ |
76d92ac3 FW |
676 | if (rq->rt.rr_nr_running) { |
677 | if (rq->rt.rr_nr_running == 1) | |
678 | return true; | |
679 | else | |
680 | return false; | |
1e78cdbd RR |
681 | } |
682 | ||
2548d546 PZ |
683 | /* |
684 | * If there's no RR tasks, but FIFO tasks, we can skip the tick, no | |
685 | * forced preemption between FIFO tasks. | |
686 | */ | |
687 | fifo_nr_running = rq->rt.rt_nr_running - rq->rt.rr_nr_running; | |
688 | if (fifo_nr_running) | |
689 | return true; | |
690 | ||
691 | /* | |
692 | * If there are no DL,RR/FIFO tasks, there must only be CFS tasks left; | |
693 | * if there's more than one we need the tick for involuntary | |
694 | * preemption. | |
695 | */ | |
696 | if (rq->nr_running > 1) | |
541b8264 | 697 | return false; |
ce831b38 | 698 | |
541b8264 | 699 | return true; |
ce831b38 FW |
700 | } |
701 | #endif /* CONFIG_NO_HZ_FULL */ | |
6d6bc0ad | 702 | #endif /* CONFIG_SMP */ |
18d95a28 | 703 | |
a790de99 PT |
704 | #if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \ |
705 | (defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH))) | |
c09595f6 | 706 | /* |
8277434e PT |
707 | * Iterate task_group tree rooted at *from, calling @down when first entering a |
708 | * node and @up when leaving it for the final time. | |
709 | * | |
710 | * Caller must hold rcu_lock or sufficient equivalent. | |
c09595f6 | 711 | */ |
029632fb | 712 | int walk_tg_tree_from(struct task_group *from, |
8277434e | 713 | tg_visitor down, tg_visitor up, void *data) |
c09595f6 PZ |
714 | { |
715 | struct task_group *parent, *child; | |
eb755805 | 716 | int ret; |
c09595f6 | 717 | |
8277434e PT |
718 | parent = from; |
719 | ||
c09595f6 | 720 | down: |
eb755805 PZ |
721 | ret = (*down)(parent, data); |
722 | if (ret) | |
8277434e | 723 | goto out; |
c09595f6 PZ |
724 | list_for_each_entry_rcu(child, &parent->children, siblings) { |
725 | parent = child; | |
726 | goto down; | |
727 | ||
728 | up: | |
729 | continue; | |
730 | } | |
eb755805 | 731 | ret = (*up)(parent, data); |
8277434e PT |
732 | if (ret || parent == from) |
733 | goto out; | |
c09595f6 PZ |
734 | |
735 | child = parent; | |
736 | parent = parent->parent; | |
737 | if (parent) | |
738 | goto up; | |
8277434e | 739 | out: |
eb755805 | 740 | return ret; |
c09595f6 PZ |
741 | } |
742 | ||
029632fb | 743 | int tg_nop(struct task_group *tg, void *data) |
eb755805 | 744 | { |
e2b245f8 | 745 | return 0; |
eb755805 | 746 | } |
18d95a28 PZ |
747 | #endif |
748 | ||
9059393e | 749 | static void set_load_weight(struct task_struct *p, bool update_load) |
45bf76df | 750 | { |
f05998d4 NR |
751 | int prio = p->static_prio - MAX_RT_PRIO; |
752 | struct load_weight *load = &p->se.load; | |
753 | ||
dd41f596 IM |
754 | /* |
755 | * SCHED_IDLE tasks get minimal weight: | |
756 | */ | |
1da1843f | 757 | if (task_has_idle_policy(p)) { |
c8b28116 | 758 | load->weight = scale_load(WEIGHT_IDLEPRIO); |
f05998d4 | 759 | load->inv_weight = WMULT_IDLEPRIO; |
dd41f596 IM |
760 | return; |
761 | } | |
71f8bd46 | 762 | |
9059393e VG |
763 | /* |
764 | * SCHED_OTHER tasks have to update their load when changing their | |
765 | * weight | |
766 | */ | |
767 | if (update_load && p->sched_class == &fair_sched_class) { | |
768 | reweight_task(p, prio); | |
769 | } else { | |
770 | load->weight = scale_load(sched_prio_to_weight[prio]); | |
771 | load->inv_weight = sched_prio_to_wmult[prio]; | |
772 | } | |
71f8bd46 IM |
773 | } |
774 | ||
69842cba | 775 | #ifdef CONFIG_UCLAMP_TASK |
2480c093 PB |
776 | /* |
777 | * Serializes updates of utilization clamp values | |
778 | * | |
779 | * The (slow-path) user-space triggers utilization clamp value updates which | |
780 | * can require updates on (fast-path) scheduler's data structures used to | |
781 | * support enqueue/dequeue operations. | |
782 | * While the per-CPU rq lock protects fast-path update operations, user-space | |
783 | * requests are serialized using a mutex to reduce the risk of conflicting | |
784 | * updates or API abuses. | |
785 | */ | |
786 | static DEFINE_MUTEX(uclamp_mutex); | |
787 | ||
e8f14172 PB |
788 | /* Max allowed minimum utilization */ |
789 | unsigned int sysctl_sched_uclamp_util_min = SCHED_CAPACITY_SCALE; | |
790 | ||
791 | /* Max allowed maximum utilization */ | |
792 | unsigned int sysctl_sched_uclamp_util_max = SCHED_CAPACITY_SCALE; | |
793 | ||
794 | /* All clamps are required to be less or equal than these values */ | |
795 | static struct uclamp_se uclamp_default[UCLAMP_CNT]; | |
69842cba PB |
796 | |
797 | /* Integer rounded range for each bucket */ | |
798 | #define UCLAMP_BUCKET_DELTA DIV_ROUND_CLOSEST(SCHED_CAPACITY_SCALE, UCLAMP_BUCKETS) | |
799 | ||
800 | #define for_each_clamp_id(clamp_id) \ | |
801 | for ((clamp_id) = 0; (clamp_id) < UCLAMP_CNT; (clamp_id)++) | |
802 | ||
803 | static inline unsigned int uclamp_bucket_id(unsigned int clamp_value) | |
804 | { | |
805 | return clamp_value / UCLAMP_BUCKET_DELTA; | |
806 | } | |
807 | ||
60daf9c1 PB |
808 | static inline unsigned int uclamp_bucket_base_value(unsigned int clamp_value) |
809 | { | |
810 | return UCLAMP_BUCKET_DELTA * uclamp_bucket_id(clamp_value); | |
811 | } | |
812 | ||
7763baac | 813 | static inline unsigned int uclamp_none(enum uclamp_id clamp_id) |
69842cba PB |
814 | { |
815 | if (clamp_id == UCLAMP_MIN) | |
816 | return 0; | |
817 | return SCHED_CAPACITY_SCALE; | |
818 | } | |
819 | ||
a509a7cd PB |
820 | static inline void uclamp_se_set(struct uclamp_se *uc_se, |
821 | unsigned int value, bool user_defined) | |
69842cba PB |
822 | { |
823 | uc_se->value = value; | |
824 | uc_se->bucket_id = uclamp_bucket_id(value); | |
a509a7cd | 825 | uc_se->user_defined = user_defined; |
69842cba PB |
826 | } |
827 | ||
e496187d | 828 | static inline unsigned int |
0413d7f3 | 829 | uclamp_idle_value(struct rq *rq, enum uclamp_id clamp_id, |
e496187d PB |
830 | unsigned int clamp_value) |
831 | { | |
832 | /* | |
833 | * Avoid blocked utilization pushing up the frequency when we go | |
834 | * idle (which drops the max-clamp) by retaining the last known | |
835 | * max-clamp. | |
836 | */ | |
837 | if (clamp_id == UCLAMP_MAX) { | |
838 | rq->uclamp_flags |= UCLAMP_FLAG_IDLE; | |
839 | return clamp_value; | |
840 | } | |
841 | ||
842 | return uclamp_none(UCLAMP_MIN); | |
843 | } | |
844 | ||
0413d7f3 | 845 | static inline void uclamp_idle_reset(struct rq *rq, enum uclamp_id clamp_id, |
e496187d PB |
846 | unsigned int clamp_value) |
847 | { | |
848 | /* Reset max-clamp retention only on idle exit */ | |
849 | if (!(rq->uclamp_flags & UCLAMP_FLAG_IDLE)) | |
850 | return; | |
851 | ||
852 | WRITE_ONCE(rq->uclamp[clamp_id].value, clamp_value); | |
853 | } | |
854 | ||
69842cba | 855 | static inline |
7763baac | 856 | unsigned int uclamp_rq_max_value(struct rq *rq, enum uclamp_id clamp_id, |
0413d7f3 | 857 | unsigned int clamp_value) |
69842cba PB |
858 | { |
859 | struct uclamp_bucket *bucket = rq->uclamp[clamp_id].bucket; | |
860 | int bucket_id = UCLAMP_BUCKETS - 1; | |
861 | ||
862 | /* | |
863 | * Since both min and max clamps are max aggregated, find the | |
864 | * top most bucket with tasks in. | |
865 | */ | |
866 | for ( ; bucket_id >= 0; bucket_id--) { | |
867 | if (!bucket[bucket_id].tasks) | |
868 | continue; | |
869 | return bucket[bucket_id].value; | |
870 | } | |
871 | ||
872 | /* No tasks -- default clamp values */ | |
e496187d | 873 | return uclamp_idle_value(rq, clamp_id, clamp_value); |
69842cba PB |
874 | } |
875 | ||
3eac870a | 876 | static inline struct uclamp_se |
0413d7f3 | 877 | uclamp_tg_restrict(struct task_struct *p, enum uclamp_id clamp_id) |
3eac870a PB |
878 | { |
879 | struct uclamp_se uc_req = p->uclamp_req[clamp_id]; | |
880 | #ifdef CONFIG_UCLAMP_TASK_GROUP | |
881 | struct uclamp_se uc_max; | |
882 | ||
883 | /* | |
884 | * Tasks in autogroups or root task group will be | |
885 | * restricted by system defaults. | |
886 | */ | |
887 | if (task_group_is_autogroup(task_group(p))) | |
888 | return uc_req; | |
889 | if (task_group(p) == &root_task_group) | |
890 | return uc_req; | |
891 | ||
892 | uc_max = task_group(p)->uclamp[clamp_id]; | |
893 | if (uc_req.value > uc_max.value || !uc_req.user_defined) | |
894 | return uc_max; | |
895 | #endif | |
896 | ||
897 | return uc_req; | |
898 | } | |
899 | ||
e8f14172 PB |
900 | /* |
901 | * The effective clamp bucket index of a task depends on, by increasing | |
902 | * priority: | |
903 | * - the task specific clamp value, when explicitly requested from userspace | |
3eac870a PB |
904 | * - the task group effective clamp value, for tasks not either in the root |
905 | * group or in an autogroup | |
e8f14172 PB |
906 | * - the system default clamp value, defined by the sysadmin |
907 | */ | |
908 | static inline struct uclamp_se | |
0413d7f3 | 909 | uclamp_eff_get(struct task_struct *p, enum uclamp_id clamp_id) |
e8f14172 | 910 | { |
3eac870a | 911 | struct uclamp_se uc_req = uclamp_tg_restrict(p, clamp_id); |
e8f14172 PB |
912 | struct uclamp_se uc_max = uclamp_default[clamp_id]; |
913 | ||
914 | /* System default restrictions always apply */ | |
915 | if (unlikely(uc_req.value > uc_max.value)) | |
916 | return uc_max; | |
917 | ||
918 | return uc_req; | |
919 | } | |
920 | ||
686516b5 | 921 | unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id) |
9d20ad7d PB |
922 | { |
923 | struct uclamp_se uc_eff; | |
924 | ||
925 | /* Task currently refcounted: use back-annotated (effective) value */ | |
926 | if (p->uclamp[clamp_id].active) | |
686516b5 | 927 | return (unsigned long)p->uclamp[clamp_id].value; |
9d20ad7d PB |
928 | |
929 | uc_eff = uclamp_eff_get(p, clamp_id); | |
930 | ||
686516b5 | 931 | return (unsigned long)uc_eff.value; |
9d20ad7d PB |
932 | } |
933 | ||
69842cba PB |
934 | /* |
935 | * When a task is enqueued on a rq, the clamp bucket currently defined by the | |
936 | * task's uclamp::bucket_id is refcounted on that rq. This also immediately | |
937 | * updates the rq's clamp value if required. | |
60daf9c1 PB |
938 | * |
939 | * Tasks can have a task-specific value requested from user-space, track | |
940 | * within each bucket the maximum value for tasks refcounted in it. | |
941 | * This "local max aggregation" allows to track the exact "requested" value | |
942 | * for each bucket when all its RUNNABLE tasks require the same clamp. | |
69842cba PB |
943 | */ |
944 | static inline void uclamp_rq_inc_id(struct rq *rq, struct task_struct *p, | |
0413d7f3 | 945 | enum uclamp_id clamp_id) |
69842cba PB |
946 | { |
947 | struct uclamp_rq *uc_rq = &rq->uclamp[clamp_id]; | |
948 | struct uclamp_se *uc_se = &p->uclamp[clamp_id]; | |
949 | struct uclamp_bucket *bucket; | |
950 | ||
951 | lockdep_assert_held(&rq->lock); | |
952 | ||
e8f14172 PB |
953 | /* Update task effective clamp */ |
954 | p->uclamp[clamp_id] = uclamp_eff_get(p, clamp_id); | |
955 | ||
69842cba PB |
956 | bucket = &uc_rq->bucket[uc_se->bucket_id]; |
957 | bucket->tasks++; | |
e8f14172 | 958 | uc_se->active = true; |
69842cba | 959 | |
e496187d PB |
960 | uclamp_idle_reset(rq, clamp_id, uc_se->value); |
961 | ||
60daf9c1 PB |
962 | /* |
963 | * Local max aggregation: rq buckets always track the max | |
964 | * "requested" clamp value of its RUNNABLE tasks. | |
965 | */ | |
966 | if (bucket->tasks == 1 || uc_se->value > bucket->value) | |
967 | bucket->value = uc_se->value; | |
968 | ||
69842cba | 969 | if (uc_se->value > READ_ONCE(uc_rq->value)) |
60daf9c1 | 970 | WRITE_ONCE(uc_rq->value, uc_se->value); |
69842cba PB |
971 | } |
972 | ||
973 | /* | |
974 | * When a task is dequeued from a rq, the clamp bucket refcounted by the task | |
975 | * is released. If this is the last task reference counting the rq's max | |
976 | * active clamp value, then the rq's clamp value is updated. | |
977 | * | |
978 | * Both refcounted tasks and rq's cached clamp values are expected to be | |
979 | * always valid. If it's detected they are not, as defensive programming, | |
980 | * enforce the expected state and warn. | |
981 | */ | |
982 | static inline void uclamp_rq_dec_id(struct rq *rq, struct task_struct *p, | |
0413d7f3 | 983 | enum uclamp_id clamp_id) |
69842cba PB |
984 | { |
985 | struct uclamp_rq *uc_rq = &rq->uclamp[clamp_id]; | |
986 | struct uclamp_se *uc_se = &p->uclamp[clamp_id]; | |
987 | struct uclamp_bucket *bucket; | |
e496187d | 988 | unsigned int bkt_clamp; |
69842cba PB |
989 | unsigned int rq_clamp; |
990 | ||
991 | lockdep_assert_held(&rq->lock); | |
992 | ||
993 | bucket = &uc_rq->bucket[uc_se->bucket_id]; | |
994 | SCHED_WARN_ON(!bucket->tasks); | |
995 | if (likely(bucket->tasks)) | |
996 | bucket->tasks--; | |
e8f14172 | 997 | uc_se->active = false; |
69842cba | 998 | |
60daf9c1 PB |
999 | /* |
1000 | * Keep "local max aggregation" simple and accept to (possibly) | |
1001 | * overboost some RUNNABLE tasks in the same bucket. | |
1002 | * The rq clamp bucket value is reset to its base value whenever | |
1003 | * there are no more RUNNABLE tasks refcounting it. | |
1004 | */ | |
69842cba PB |
1005 | if (likely(bucket->tasks)) |
1006 | return; | |
1007 | ||
1008 | rq_clamp = READ_ONCE(uc_rq->value); | |
1009 | /* | |
1010 | * Defensive programming: this should never happen. If it happens, | |
1011 | * e.g. due to future modification, warn and fixup the expected value. | |
1012 | */ | |
1013 | SCHED_WARN_ON(bucket->value > rq_clamp); | |
e496187d PB |
1014 | if (bucket->value >= rq_clamp) { |
1015 | bkt_clamp = uclamp_rq_max_value(rq, clamp_id, uc_se->value); | |
1016 | WRITE_ONCE(uc_rq->value, bkt_clamp); | |
1017 | } | |
69842cba PB |
1018 | } |
1019 | ||
1020 | static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p) | |
1021 | { | |
0413d7f3 | 1022 | enum uclamp_id clamp_id; |
69842cba PB |
1023 | |
1024 | if (unlikely(!p->sched_class->uclamp_enabled)) | |
1025 | return; | |
1026 | ||
1027 | for_each_clamp_id(clamp_id) | |
1028 | uclamp_rq_inc_id(rq, p, clamp_id); | |
e496187d PB |
1029 | |
1030 | /* Reset clamp idle holding when there is one RUNNABLE task */ | |
1031 | if (rq->uclamp_flags & UCLAMP_FLAG_IDLE) | |
1032 | rq->uclamp_flags &= ~UCLAMP_FLAG_IDLE; | |
69842cba PB |
1033 | } |
1034 | ||
1035 | static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p) | |
1036 | { | |
0413d7f3 | 1037 | enum uclamp_id clamp_id; |
69842cba PB |
1038 | |
1039 | if (unlikely(!p->sched_class->uclamp_enabled)) | |
1040 | return; | |
1041 | ||
1042 | for_each_clamp_id(clamp_id) | |
1043 | uclamp_rq_dec_id(rq, p, clamp_id); | |
1044 | } | |
1045 | ||
babbe170 | 1046 | static inline void |
0413d7f3 | 1047 | uclamp_update_active(struct task_struct *p, enum uclamp_id clamp_id) |
babbe170 PB |
1048 | { |
1049 | struct rq_flags rf; | |
1050 | struct rq *rq; | |
1051 | ||
1052 | /* | |
1053 | * Lock the task and the rq where the task is (or was) queued. | |
1054 | * | |
1055 | * We might lock the (previous) rq of a !RUNNABLE task, but that's the | |
1056 | * price to pay to safely serialize util_{min,max} updates with | |
1057 | * enqueues, dequeues and migration operations. | |
1058 | * This is the same locking schema used by __set_cpus_allowed_ptr(). | |
1059 | */ | |
1060 | rq = task_rq_lock(p, &rf); | |
1061 | ||
1062 | /* | |
1063 | * Setting the clamp bucket is serialized by task_rq_lock(). | |
1064 | * If the task is not yet RUNNABLE and its task_struct is not | |
1065 | * affecting a valid clamp bucket, the next time it's enqueued, | |
1066 | * it will already see the updated clamp bucket value. | |
1067 | */ | |
6e1ff077 | 1068 | if (p->uclamp[clamp_id].active) { |
babbe170 PB |
1069 | uclamp_rq_dec_id(rq, p, clamp_id); |
1070 | uclamp_rq_inc_id(rq, p, clamp_id); | |
1071 | } | |
1072 | ||
1073 | task_rq_unlock(rq, p, &rf); | |
1074 | } | |
1075 | ||
e3b8b6a0 | 1076 | #ifdef CONFIG_UCLAMP_TASK_GROUP |
babbe170 PB |
1077 | static inline void |
1078 | uclamp_update_active_tasks(struct cgroup_subsys_state *css, | |
1079 | unsigned int clamps) | |
1080 | { | |
0413d7f3 | 1081 | enum uclamp_id clamp_id; |
babbe170 PB |
1082 | struct css_task_iter it; |
1083 | struct task_struct *p; | |
babbe170 PB |
1084 | |
1085 | css_task_iter_start(css, 0, &it); | |
1086 | while ((p = css_task_iter_next(&it))) { | |
1087 | for_each_clamp_id(clamp_id) { | |
1088 | if ((0x1 << clamp_id) & clamps) | |
1089 | uclamp_update_active(p, clamp_id); | |
1090 | } | |
1091 | } | |
1092 | css_task_iter_end(&it); | |
1093 | } | |
1094 | ||
7274a5c1 PB |
1095 | static void cpu_util_update_eff(struct cgroup_subsys_state *css); |
1096 | static void uclamp_update_root_tg(void) | |
1097 | { | |
1098 | struct task_group *tg = &root_task_group; | |
1099 | ||
1100 | uclamp_se_set(&tg->uclamp_req[UCLAMP_MIN], | |
1101 | sysctl_sched_uclamp_util_min, false); | |
1102 | uclamp_se_set(&tg->uclamp_req[UCLAMP_MAX], | |
1103 | sysctl_sched_uclamp_util_max, false); | |
1104 | ||
1105 | rcu_read_lock(); | |
1106 | cpu_util_update_eff(&root_task_group.css); | |
1107 | rcu_read_unlock(); | |
1108 | } | |
1109 | #else | |
1110 | static void uclamp_update_root_tg(void) { } | |
1111 | #endif | |
1112 | ||
e8f14172 PB |
1113 | int sysctl_sched_uclamp_handler(struct ctl_table *table, int write, |
1114 | void __user *buffer, size_t *lenp, | |
1115 | loff_t *ppos) | |
1116 | { | |
7274a5c1 | 1117 | bool update_root_tg = false; |
e8f14172 | 1118 | int old_min, old_max; |
e8f14172 PB |
1119 | int result; |
1120 | ||
2480c093 | 1121 | mutex_lock(&uclamp_mutex); |
e8f14172 PB |
1122 | old_min = sysctl_sched_uclamp_util_min; |
1123 | old_max = sysctl_sched_uclamp_util_max; | |
1124 | ||
1125 | result = proc_dointvec(table, write, buffer, lenp, ppos); | |
1126 | if (result) | |
1127 | goto undo; | |
1128 | if (!write) | |
1129 | goto done; | |
1130 | ||
1131 | if (sysctl_sched_uclamp_util_min > sysctl_sched_uclamp_util_max || | |
1132 | sysctl_sched_uclamp_util_max > SCHED_CAPACITY_SCALE) { | |
1133 | result = -EINVAL; | |
1134 | goto undo; | |
1135 | } | |
1136 | ||
1137 | if (old_min != sysctl_sched_uclamp_util_min) { | |
1138 | uclamp_se_set(&uclamp_default[UCLAMP_MIN], | |
a509a7cd | 1139 | sysctl_sched_uclamp_util_min, false); |
7274a5c1 | 1140 | update_root_tg = true; |
e8f14172 PB |
1141 | } |
1142 | if (old_max != sysctl_sched_uclamp_util_max) { | |
1143 | uclamp_se_set(&uclamp_default[UCLAMP_MAX], | |
a509a7cd | 1144 | sysctl_sched_uclamp_util_max, false); |
7274a5c1 | 1145 | update_root_tg = true; |
e8f14172 PB |
1146 | } |
1147 | ||
7274a5c1 PB |
1148 | if (update_root_tg) |
1149 | uclamp_update_root_tg(); | |
1150 | ||
e8f14172 | 1151 | /* |
7274a5c1 PB |
1152 | * We update all RUNNABLE tasks only when task groups are in use. |
1153 | * Otherwise, keep it simple and do just a lazy update at each next | |
1154 | * task enqueue time. | |
e8f14172 | 1155 | */ |
7274a5c1 | 1156 | |
e8f14172 PB |
1157 | goto done; |
1158 | ||
1159 | undo: | |
1160 | sysctl_sched_uclamp_util_min = old_min; | |
1161 | sysctl_sched_uclamp_util_max = old_max; | |
1162 | done: | |
2480c093 | 1163 | mutex_unlock(&uclamp_mutex); |
e8f14172 PB |
1164 | |
1165 | return result; | |
1166 | } | |
1167 | ||
a509a7cd PB |
1168 | static int uclamp_validate(struct task_struct *p, |
1169 | const struct sched_attr *attr) | |
1170 | { | |
1171 | unsigned int lower_bound = p->uclamp_req[UCLAMP_MIN].value; | |
1172 | unsigned int upper_bound = p->uclamp_req[UCLAMP_MAX].value; | |
1173 | ||
1174 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN) | |
1175 | lower_bound = attr->sched_util_min; | |
1176 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX) | |
1177 | upper_bound = attr->sched_util_max; | |
1178 | ||
1179 | if (lower_bound > upper_bound) | |
1180 | return -EINVAL; | |
1181 | if (upper_bound > SCHED_CAPACITY_SCALE) | |
1182 | return -EINVAL; | |
1183 | ||
1184 | return 0; | |
1185 | } | |
1186 | ||
1187 | static void __setscheduler_uclamp(struct task_struct *p, | |
1188 | const struct sched_attr *attr) | |
1189 | { | |
0413d7f3 | 1190 | enum uclamp_id clamp_id; |
1a00d999 PB |
1191 | |
1192 | /* | |
1193 | * On scheduling class change, reset to default clamps for tasks | |
1194 | * without a task-specific value. | |
1195 | */ | |
1196 | for_each_clamp_id(clamp_id) { | |
1197 | struct uclamp_se *uc_se = &p->uclamp_req[clamp_id]; | |
1198 | unsigned int clamp_value = uclamp_none(clamp_id); | |
1199 | ||
1200 | /* Keep using defined clamps across class changes */ | |
1201 | if (uc_se->user_defined) | |
1202 | continue; | |
1203 | ||
1204 | /* By default, RT tasks always get 100% boost */ | |
1205 | if (unlikely(rt_task(p) && clamp_id == UCLAMP_MIN)) | |
1206 | clamp_value = uclamp_none(UCLAMP_MAX); | |
1207 | ||
1208 | uclamp_se_set(uc_se, clamp_value, false); | |
1209 | } | |
1210 | ||
a509a7cd PB |
1211 | if (likely(!(attr->sched_flags & SCHED_FLAG_UTIL_CLAMP))) |
1212 | return; | |
1213 | ||
1214 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN) { | |
1215 | uclamp_se_set(&p->uclamp_req[UCLAMP_MIN], | |
1216 | attr->sched_util_min, true); | |
1217 | } | |
1218 | ||
1219 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX) { | |
1220 | uclamp_se_set(&p->uclamp_req[UCLAMP_MAX], | |
1221 | attr->sched_util_max, true); | |
1222 | } | |
1223 | } | |
1224 | ||
e8f14172 PB |
1225 | static void uclamp_fork(struct task_struct *p) |
1226 | { | |
0413d7f3 | 1227 | enum uclamp_id clamp_id; |
e8f14172 PB |
1228 | |
1229 | for_each_clamp_id(clamp_id) | |
1230 | p->uclamp[clamp_id].active = false; | |
a87498ac PB |
1231 | |
1232 | if (likely(!p->sched_reset_on_fork)) | |
1233 | return; | |
1234 | ||
1235 | for_each_clamp_id(clamp_id) { | |
eaf5a92e QP |
1236 | uclamp_se_set(&p->uclamp_req[clamp_id], |
1237 | uclamp_none(clamp_id), false); | |
a87498ac | 1238 | } |
e8f14172 PB |
1239 | } |
1240 | ||
69842cba PB |
1241 | static void __init init_uclamp(void) |
1242 | { | |
e8f14172 | 1243 | struct uclamp_se uc_max = {}; |
0413d7f3 | 1244 | enum uclamp_id clamp_id; |
69842cba PB |
1245 | int cpu; |
1246 | ||
2480c093 PB |
1247 | mutex_init(&uclamp_mutex); |
1248 | ||
e496187d | 1249 | for_each_possible_cpu(cpu) { |
dcd6dffb LG |
1250 | memset(&cpu_rq(cpu)->uclamp, 0, |
1251 | sizeof(struct uclamp_rq)*UCLAMP_CNT); | |
e496187d PB |
1252 | cpu_rq(cpu)->uclamp_flags = 0; |
1253 | } | |
69842cba | 1254 | |
69842cba | 1255 | for_each_clamp_id(clamp_id) { |
e8f14172 | 1256 | uclamp_se_set(&init_task.uclamp_req[clamp_id], |
a509a7cd | 1257 | uclamp_none(clamp_id), false); |
69842cba | 1258 | } |
e8f14172 PB |
1259 | |
1260 | /* System defaults allow max clamp values for both indexes */ | |
a509a7cd | 1261 | uclamp_se_set(&uc_max, uclamp_none(UCLAMP_MAX), false); |
2480c093 | 1262 | for_each_clamp_id(clamp_id) { |
e8f14172 | 1263 | uclamp_default[clamp_id] = uc_max; |
2480c093 PB |
1264 | #ifdef CONFIG_UCLAMP_TASK_GROUP |
1265 | root_task_group.uclamp_req[clamp_id] = uc_max; | |
0b60ba2d | 1266 | root_task_group.uclamp[clamp_id] = uc_max; |
2480c093 PB |
1267 | #endif |
1268 | } | |
69842cba PB |
1269 | } |
1270 | ||
1271 | #else /* CONFIG_UCLAMP_TASK */ | |
1272 | static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p) { } | |
1273 | static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p) { } | |
a509a7cd PB |
1274 | static inline int uclamp_validate(struct task_struct *p, |
1275 | const struct sched_attr *attr) | |
1276 | { | |
1277 | return -EOPNOTSUPP; | |
1278 | } | |
1279 | static void __setscheduler_uclamp(struct task_struct *p, | |
1280 | const struct sched_attr *attr) { } | |
e8f14172 | 1281 | static inline void uclamp_fork(struct task_struct *p) { } |
69842cba PB |
1282 | static inline void init_uclamp(void) { } |
1283 | #endif /* CONFIG_UCLAMP_TASK */ | |
1284 | ||
1de64443 | 1285 | static inline void enqueue_task(struct rq *rq, struct task_struct *p, int flags) |
2087a1ad | 1286 | { |
0a67d1ee PZ |
1287 | if (!(flags & ENQUEUE_NOCLOCK)) |
1288 | update_rq_clock(rq); | |
1289 | ||
eb414681 | 1290 | if (!(flags & ENQUEUE_RESTORE)) { |
1de64443 | 1291 | sched_info_queued(rq, p); |
eb414681 JW |
1292 | psi_enqueue(p, flags & ENQUEUE_WAKEUP); |
1293 | } | |
0a67d1ee | 1294 | |
69842cba | 1295 | uclamp_rq_inc(rq, p); |
371fd7e7 | 1296 | p->sched_class->enqueue_task(rq, p, flags); |
71f8bd46 IM |
1297 | } |
1298 | ||
1de64443 | 1299 | static inline void dequeue_task(struct rq *rq, struct task_struct *p, int flags) |
71f8bd46 | 1300 | { |
0a67d1ee PZ |
1301 | if (!(flags & DEQUEUE_NOCLOCK)) |
1302 | update_rq_clock(rq); | |
1303 | ||
eb414681 | 1304 | if (!(flags & DEQUEUE_SAVE)) { |
1de64443 | 1305 | sched_info_dequeued(rq, p); |
eb414681 JW |
1306 | psi_dequeue(p, flags & DEQUEUE_SLEEP); |
1307 | } | |
0a67d1ee | 1308 | |
69842cba | 1309 | uclamp_rq_dec(rq, p); |
371fd7e7 | 1310 | p->sched_class->dequeue_task(rq, p, flags); |
71f8bd46 IM |
1311 | } |
1312 | ||
029632fb | 1313 | void activate_task(struct rq *rq, struct task_struct *p, int flags) |
1e3c88bd PZ |
1314 | { |
1315 | if (task_contributes_to_load(p)) | |
1316 | rq->nr_uninterruptible--; | |
1317 | ||
371fd7e7 | 1318 | enqueue_task(rq, p, flags); |
7dd77884 PZ |
1319 | |
1320 | p->on_rq = TASK_ON_RQ_QUEUED; | |
1e3c88bd PZ |
1321 | } |
1322 | ||
029632fb | 1323 | void deactivate_task(struct rq *rq, struct task_struct *p, int flags) |
1e3c88bd | 1324 | { |
7dd77884 PZ |
1325 | p->on_rq = (flags & DEQUEUE_SLEEP) ? 0 : TASK_ON_RQ_MIGRATING; |
1326 | ||
1e3c88bd PZ |
1327 | if (task_contributes_to_load(p)) |
1328 | rq->nr_uninterruptible++; | |
1329 | ||
371fd7e7 | 1330 | dequeue_task(rq, p, flags); |
1e3c88bd PZ |
1331 | } |
1332 | ||
14531189 | 1333 | /* |
dd41f596 | 1334 | * __normal_prio - return the priority that is based on the static prio |
14531189 | 1335 | */ |
14531189 IM |
1336 | static inline int __normal_prio(struct task_struct *p) |
1337 | { | |
dd41f596 | 1338 | return p->static_prio; |
14531189 IM |
1339 | } |
1340 | ||
b29739f9 IM |
1341 | /* |
1342 | * Calculate the expected normal priority: i.e. priority | |
1343 | * without taking RT-inheritance into account. Might be | |
1344 | * boosted by interactivity modifiers. Changes upon fork, | |
1345 | * setprio syscalls, and whenever the interactivity | |
1346 | * estimator recalculates. | |
1347 | */ | |
36c8b586 | 1348 | static inline int normal_prio(struct task_struct *p) |
b29739f9 IM |
1349 | { |
1350 | int prio; | |
1351 | ||
aab03e05 DF |
1352 | if (task_has_dl_policy(p)) |
1353 | prio = MAX_DL_PRIO-1; | |
1354 | else if (task_has_rt_policy(p)) | |
b29739f9 IM |
1355 | prio = MAX_RT_PRIO-1 - p->rt_priority; |
1356 | else | |
1357 | prio = __normal_prio(p); | |
1358 | return prio; | |
1359 | } | |
1360 | ||
1361 | /* | |
1362 | * Calculate the current priority, i.e. the priority | |
1363 | * taken into account by the scheduler. This value might | |
1364 | * be boosted by RT tasks, or might be boosted by | |
1365 | * interactivity modifiers. Will be RT if the task got | |
1366 | * RT-boosted. If not then it returns p->normal_prio. | |
1367 | */ | |
36c8b586 | 1368 | static int effective_prio(struct task_struct *p) |
b29739f9 IM |
1369 | { |
1370 | p->normal_prio = normal_prio(p); | |
1371 | /* | |
1372 | * If we are RT tasks or we were boosted to RT priority, | |
1373 | * keep the priority unchanged. Otherwise, update priority | |
1374 | * to the normal priority: | |
1375 | */ | |
1376 | if (!rt_prio(p->prio)) | |
1377 | return p->normal_prio; | |
1378 | return p->prio; | |
1379 | } | |
1380 | ||
1da177e4 LT |
1381 | /** |
1382 | * task_curr - is this task currently executing on a CPU? | |
1383 | * @p: the task in question. | |
e69f6186 YB |
1384 | * |
1385 | * Return: 1 if the task is currently executing. 0 otherwise. | |
1da177e4 | 1386 | */ |
36c8b586 | 1387 | inline int task_curr(const struct task_struct *p) |
1da177e4 LT |
1388 | { |
1389 | return cpu_curr(task_cpu(p)) == p; | |
1390 | } | |
1391 | ||
67dfa1b7 | 1392 | /* |
4c9a4bc8 PZ |
1393 | * switched_from, switched_to and prio_changed must _NOT_ drop rq->lock, |
1394 | * use the balance_callback list if you want balancing. | |
1395 | * | |
1396 | * this means any call to check_class_changed() must be followed by a call to | |
1397 | * balance_callback(). | |
67dfa1b7 | 1398 | */ |
cb469845 SR |
1399 | static inline void check_class_changed(struct rq *rq, struct task_struct *p, |
1400 | const struct sched_class *prev_class, | |
da7a735e | 1401 | int oldprio) |
cb469845 SR |
1402 | { |
1403 | if (prev_class != p->sched_class) { | |
1404 | if (prev_class->switched_from) | |
da7a735e | 1405 | prev_class->switched_from(rq, p); |
4c9a4bc8 | 1406 | |
da7a735e | 1407 | p->sched_class->switched_to(rq, p); |
2d3d891d | 1408 | } else if (oldprio != p->prio || dl_task(p)) |
da7a735e | 1409 | p->sched_class->prio_changed(rq, p, oldprio); |
cb469845 SR |
1410 | } |
1411 | ||
029632fb | 1412 | void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags) |
1e5a7405 PZ |
1413 | { |
1414 | const struct sched_class *class; | |
1415 | ||
1416 | if (p->sched_class == rq->curr->sched_class) { | |
1417 | rq->curr->sched_class->check_preempt_curr(rq, p, flags); | |
1418 | } else { | |
1419 | for_each_class(class) { | |
1420 | if (class == rq->curr->sched_class) | |
1421 | break; | |
1422 | if (class == p->sched_class) { | |
8875125e | 1423 | resched_curr(rq); |
1e5a7405 PZ |
1424 | break; |
1425 | } | |
1426 | } | |
1427 | } | |
1428 | ||
1429 | /* | |
1430 | * A queue event has occurred, and we're going to schedule. In | |
1431 | * this case, we can save a useless back to back clock update. | |
1432 | */ | |
da0c1e65 | 1433 | if (task_on_rq_queued(rq->curr) && test_tsk_need_resched(rq->curr)) |
adcc8da8 | 1434 | rq_clock_skip_update(rq); |
1e5a7405 PZ |
1435 | } |
1436 | ||
1da177e4 | 1437 | #ifdef CONFIG_SMP |
175f0e25 | 1438 | |
175f0e25 | 1439 | /* |
bee98539 | 1440 | * Per-CPU kthreads are allowed to run on !active && online CPUs, see |
175f0e25 PZ |
1441 | * __set_cpus_allowed_ptr() and select_fallback_rq(). |
1442 | */ | |
1443 | static inline bool is_cpu_allowed(struct task_struct *p, int cpu) | |
1444 | { | |
3bd37062 | 1445 | if (!cpumask_test_cpu(cpu, p->cpus_ptr)) |
175f0e25 PZ |
1446 | return false; |
1447 | ||
1448 | if (is_per_cpu_kthread(p)) | |
1449 | return cpu_online(cpu); | |
1450 | ||
1451 | return cpu_active(cpu); | |
1452 | } | |
1453 | ||
5cc389bc PZ |
1454 | /* |
1455 | * This is how migration works: | |
1456 | * | |
1457 | * 1) we invoke migration_cpu_stop() on the target CPU using | |
1458 | * stop_one_cpu(). | |
1459 | * 2) stopper starts to run (implicitly forcing the migrated thread | |
1460 | * off the CPU) | |
1461 | * 3) it checks whether the migrated task is still in the wrong runqueue. | |
1462 | * 4) if it's in the wrong runqueue then the migration thread removes | |
1463 | * it and puts it into the right queue. | |
1464 | * 5) stopper completes and stop_one_cpu() returns and the migration | |
1465 | * is done. | |
1466 | */ | |
1467 | ||
1468 | /* | |
1469 | * move_queued_task - move a queued task to new rq. | |
1470 | * | |
1471 | * Returns (locked) new rq. Old rq's lock is released. | |
1472 | */ | |
8a8c69c3 PZ |
1473 | static struct rq *move_queued_task(struct rq *rq, struct rq_flags *rf, |
1474 | struct task_struct *p, int new_cpu) | |
5cc389bc | 1475 | { |
5cc389bc PZ |
1476 | lockdep_assert_held(&rq->lock); |
1477 | ||
c546951d | 1478 | WRITE_ONCE(p->on_rq, TASK_ON_RQ_MIGRATING); |
15ff991e | 1479 | dequeue_task(rq, p, DEQUEUE_NOCLOCK); |
5cc389bc | 1480 | set_task_cpu(p, new_cpu); |
8a8c69c3 | 1481 | rq_unlock(rq, rf); |
5cc389bc PZ |
1482 | |
1483 | rq = cpu_rq(new_cpu); | |
1484 | ||
8a8c69c3 | 1485 | rq_lock(rq, rf); |
5cc389bc | 1486 | BUG_ON(task_cpu(p) != new_cpu); |
5cc389bc | 1487 | enqueue_task(rq, p, 0); |
3ea94de1 | 1488 | p->on_rq = TASK_ON_RQ_QUEUED; |
5cc389bc PZ |
1489 | check_preempt_curr(rq, p, 0); |
1490 | ||
1491 | return rq; | |
1492 | } | |
1493 | ||
1494 | struct migration_arg { | |
1495 | struct task_struct *task; | |
1496 | int dest_cpu; | |
1497 | }; | |
1498 | ||
1499 | /* | |
d1ccc66d | 1500 | * Move (not current) task off this CPU, onto the destination CPU. We're doing |
5cc389bc PZ |
1501 | * this because either it can't run here any more (set_cpus_allowed() |
1502 | * away from this CPU, or CPU going down), or because we're | |
1503 | * attempting to rebalance this task on exec (sched_exec). | |
1504 | * | |
1505 | * So we race with normal scheduler movements, but that's OK, as long | |
1506 | * as the task is no longer on this CPU. | |
5cc389bc | 1507 | */ |
8a8c69c3 PZ |
1508 | static struct rq *__migrate_task(struct rq *rq, struct rq_flags *rf, |
1509 | struct task_struct *p, int dest_cpu) | |
5cc389bc | 1510 | { |
5cc389bc | 1511 | /* Affinity changed (again). */ |
175f0e25 | 1512 | if (!is_cpu_allowed(p, dest_cpu)) |
5e16bbc2 | 1513 | return rq; |
5cc389bc | 1514 | |
15ff991e | 1515 | update_rq_clock(rq); |
8a8c69c3 | 1516 | rq = move_queued_task(rq, rf, p, dest_cpu); |
5e16bbc2 PZ |
1517 | |
1518 | return rq; | |
5cc389bc PZ |
1519 | } |
1520 | ||
1521 | /* | |
1522 | * migration_cpu_stop - this will be executed by a highprio stopper thread | |
1523 | * and performs thread migration by bumping thread off CPU then | |
1524 | * 'pushing' onto another runqueue. | |
1525 | */ | |
1526 | static int migration_cpu_stop(void *data) | |
1527 | { | |
1528 | struct migration_arg *arg = data; | |
5e16bbc2 PZ |
1529 | struct task_struct *p = arg->task; |
1530 | struct rq *rq = this_rq(); | |
8a8c69c3 | 1531 | struct rq_flags rf; |
5cc389bc PZ |
1532 | |
1533 | /* | |
d1ccc66d IM |
1534 | * The original target CPU might have gone down and we might |
1535 | * be on another CPU but it doesn't matter. | |
5cc389bc PZ |
1536 | */ |
1537 | local_irq_disable(); | |
1538 | /* | |
1539 | * We need to explicitly wake pending tasks before running | |
3bd37062 | 1540 | * __migrate_task() such that we will not miss enforcing cpus_ptr |
5cc389bc PZ |
1541 | * during wakeups, see set_cpus_allowed_ptr()'s TASK_WAKING test. |
1542 | */ | |
1543 | sched_ttwu_pending(); | |
5e16bbc2 PZ |
1544 | |
1545 | raw_spin_lock(&p->pi_lock); | |
8a8c69c3 | 1546 | rq_lock(rq, &rf); |
5e16bbc2 PZ |
1547 | /* |
1548 | * If task_rq(p) != rq, it cannot be migrated here, because we're | |
1549 | * holding rq->lock, if p->on_rq == 0 it cannot get enqueued because | |
1550 | * we're holding p->pi_lock. | |
1551 | */ | |
bf89a304 CC |
1552 | if (task_rq(p) == rq) { |
1553 | if (task_on_rq_queued(p)) | |
8a8c69c3 | 1554 | rq = __migrate_task(rq, &rf, p, arg->dest_cpu); |
bf89a304 CC |
1555 | else |
1556 | p->wake_cpu = arg->dest_cpu; | |
1557 | } | |
8a8c69c3 | 1558 | rq_unlock(rq, &rf); |
5e16bbc2 PZ |
1559 | raw_spin_unlock(&p->pi_lock); |
1560 | ||
5cc389bc PZ |
1561 | local_irq_enable(); |
1562 | return 0; | |
1563 | } | |
1564 | ||
c5b28038 PZ |
1565 | /* |
1566 | * sched_class::set_cpus_allowed must do the below, but is not required to | |
1567 | * actually call this function. | |
1568 | */ | |
1569 | void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask) | |
5cc389bc | 1570 | { |
3bd37062 | 1571 | cpumask_copy(&p->cpus_mask, new_mask); |
5cc389bc PZ |
1572 | p->nr_cpus_allowed = cpumask_weight(new_mask); |
1573 | } | |
1574 | ||
c5b28038 PZ |
1575 | void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask) |
1576 | { | |
6c37067e PZ |
1577 | struct rq *rq = task_rq(p); |
1578 | bool queued, running; | |
1579 | ||
c5b28038 | 1580 | lockdep_assert_held(&p->pi_lock); |
6c37067e PZ |
1581 | |
1582 | queued = task_on_rq_queued(p); | |
1583 | running = task_current(rq, p); | |
1584 | ||
1585 | if (queued) { | |
1586 | /* | |
1587 | * Because __kthread_bind() calls this on blocked tasks without | |
1588 | * holding rq->lock. | |
1589 | */ | |
1590 | lockdep_assert_held(&rq->lock); | |
7a57f32a | 1591 | dequeue_task(rq, p, DEQUEUE_SAVE | DEQUEUE_NOCLOCK); |
6c37067e PZ |
1592 | } |
1593 | if (running) | |
1594 | put_prev_task(rq, p); | |
1595 | ||
c5b28038 | 1596 | p->sched_class->set_cpus_allowed(p, new_mask); |
6c37067e | 1597 | |
6c37067e | 1598 | if (queued) |
7134b3e9 | 1599 | enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK); |
a399d233 | 1600 | if (running) |
03b7fad1 | 1601 | set_next_task(rq, p); |
c5b28038 PZ |
1602 | } |
1603 | ||
5cc389bc PZ |
1604 | /* |
1605 | * Change a given task's CPU affinity. Migrate the thread to a | |
1606 | * proper CPU and schedule it away if the CPU it's executing on | |
1607 | * is removed from the allowed bitmask. | |
1608 | * | |
1609 | * NOTE: the caller must have a valid reference to the task, the | |
1610 | * task must not exit() & deallocate itself prematurely. The | |
1611 | * call is not atomic; no spinlocks may be held. | |
1612 | */ | |
25834c73 PZ |
1613 | static int __set_cpus_allowed_ptr(struct task_struct *p, |
1614 | const struct cpumask *new_mask, bool check) | |
5cc389bc | 1615 | { |
e9d867a6 | 1616 | const struct cpumask *cpu_valid_mask = cpu_active_mask; |
5cc389bc | 1617 | unsigned int dest_cpu; |
eb580751 PZ |
1618 | struct rq_flags rf; |
1619 | struct rq *rq; | |
5cc389bc PZ |
1620 | int ret = 0; |
1621 | ||
eb580751 | 1622 | rq = task_rq_lock(p, &rf); |
a499c3ea | 1623 | update_rq_clock(rq); |
5cc389bc | 1624 | |
e9d867a6 PZI |
1625 | if (p->flags & PF_KTHREAD) { |
1626 | /* | |
1627 | * Kernel threads are allowed on online && !active CPUs | |
1628 | */ | |
1629 | cpu_valid_mask = cpu_online_mask; | |
1630 | } | |
1631 | ||
25834c73 PZ |
1632 | /* |
1633 | * Must re-check here, to close a race against __kthread_bind(), | |
1634 | * sched_setaffinity() is not guaranteed to observe the flag. | |
1635 | */ | |
1636 | if (check && (p->flags & PF_NO_SETAFFINITY)) { | |
1637 | ret = -EINVAL; | |
1638 | goto out; | |
1639 | } | |
1640 | ||
3bd37062 | 1641 | if (cpumask_equal(p->cpus_ptr, new_mask)) |
5cc389bc PZ |
1642 | goto out; |
1643 | ||
46a87b38 PT |
1644 | /* |
1645 | * Picking a ~random cpu helps in cases where we are changing affinity | |
1646 | * for groups of tasks (ie. cpuset), so that load balancing is not | |
1647 | * immediately required to distribute the tasks within their new mask. | |
1648 | */ | |
1649 | dest_cpu = cpumask_any_and_distribute(cpu_valid_mask, new_mask); | |
714e501e | 1650 | if (dest_cpu >= nr_cpu_ids) { |
5cc389bc PZ |
1651 | ret = -EINVAL; |
1652 | goto out; | |
1653 | } | |
1654 | ||
1655 | do_set_cpus_allowed(p, new_mask); | |
1656 | ||
e9d867a6 PZI |
1657 | if (p->flags & PF_KTHREAD) { |
1658 | /* | |
1659 | * For kernel threads that do indeed end up on online && | |
d1ccc66d | 1660 | * !active we want to ensure they are strict per-CPU threads. |
e9d867a6 PZI |
1661 | */ |
1662 | WARN_ON(cpumask_intersects(new_mask, cpu_online_mask) && | |
1663 | !cpumask_intersects(new_mask, cpu_active_mask) && | |
1664 | p->nr_cpus_allowed != 1); | |
1665 | } | |
1666 | ||
5cc389bc PZ |
1667 | /* Can the task run on the task's current CPU? If so, we're done */ |
1668 | if (cpumask_test_cpu(task_cpu(p), new_mask)) | |
1669 | goto out; | |
1670 | ||
5cc389bc PZ |
1671 | if (task_running(rq, p) || p->state == TASK_WAKING) { |
1672 | struct migration_arg arg = { p, dest_cpu }; | |
1673 | /* Need help from migration thread: drop lock and wait. */ | |
eb580751 | 1674 | task_rq_unlock(rq, p, &rf); |
5cc389bc | 1675 | stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg); |
5cc389bc | 1676 | return 0; |
cbce1a68 PZ |
1677 | } else if (task_on_rq_queued(p)) { |
1678 | /* | |
1679 | * OK, since we're going to drop the lock immediately | |
1680 | * afterwards anyway. | |
1681 | */ | |
8a8c69c3 | 1682 | rq = move_queued_task(rq, &rf, p, dest_cpu); |
cbce1a68 | 1683 | } |
5cc389bc | 1684 | out: |
eb580751 | 1685 | task_rq_unlock(rq, p, &rf); |
5cc389bc PZ |
1686 | |
1687 | return ret; | |
1688 | } | |
25834c73 PZ |
1689 | |
1690 | int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask) | |
1691 | { | |
1692 | return __set_cpus_allowed_ptr(p, new_mask, false); | |
1693 | } | |
5cc389bc PZ |
1694 | EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr); |
1695 | ||
dd41f596 | 1696 | void set_task_cpu(struct task_struct *p, unsigned int new_cpu) |
c65cc870 | 1697 | { |
e2912009 PZ |
1698 | #ifdef CONFIG_SCHED_DEBUG |
1699 | /* | |
1700 | * We should never call set_task_cpu() on a blocked task, | |
1701 | * ttwu() will sort out the placement. | |
1702 | */ | |
077614ee | 1703 | WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING && |
e2336f6e | 1704 | !p->on_rq); |
0122ec5b | 1705 | |
3ea94de1 JP |
1706 | /* |
1707 | * Migrating fair class task must have p->on_rq = TASK_ON_RQ_MIGRATING, | |
1708 | * because schedstat_wait_{start,end} rebase migrating task's wait_start | |
1709 | * time relying on p->on_rq. | |
1710 | */ | |
1711 | WARN_ON_ONCE(p->state == TASK_RUNNING && | |
1712 | p->sched_class == &fair_sched_class && | |
1713 | (p->on_rq && !task_on_rq_migrating(p))); | |
1714 | ||
0122ec5b | 1715 | #ifdef CONFIG_LOCKDEP |
6c6c54e1 PZ |
1716 | /* |
1717 | * The caller should hold either p->pi_lock or rq->lock, when changing | |
1718 | * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks. | |
1719 | * | |
1720 | * sched_move_task() holds both and thus holding either pins the cgroup, | |
8323f26c | 1721 | * see task_group(). |
6c6c54e1 PZ |
1722 | * |
1723 | * Furthermore, all task_rq users should acquire both locks, see | |
1724 | * task_rq_lock(). | |
1725 | */ | |
0122ec5b PZ |
1726 | WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) || |
1727 | lockdep_is_held(&task_rq(p)->lock))); | |
1728 | #endif | |
4ff9083b PZ |
1729 | /* |
1730 | * Clearly, migrating tasks to offline CPUs is a fairly daft thing. | |
1731 | */ | |
1732 | WARN_ON_ONCE(!cpu_online(new_cpu)); | |
e2912009 PZ |
1733 | #endif |
1734 | ||
de1d7286 | 1735 | trace_sched_migrate_task(p, new_cpu); |
cbc34ed1 | 1736 | |
0c69774e | 1737 | if (task_cpu(p) != new_cpu) { |
0a74bef8 | 1738 | if (p->sched_class->migrate_task_rq) |
1327237a | 1739 | p->sched_class->migrate_task_rq(p, new_cpu); |
0c69774e | 1740 | p->se.nr_migrations++; |
d7822b1e | 1741 | rseq_migrate(p); |
ff303e66 | 1742 | perf_event_task_migrate(p); |
0c69774e | 1743 | } |
dd41f596 IM |
1744 | |
1745 | __set_task_cpu(p, new_cpu); | |
c65cc870 IM |
1746 | } |
1747 | ||
0ad4e3df | 1748 | #ifdef CONFIG_NUMA_BALANCING |
ac66f547 PZ |
1749 | static void __migrate_swap_task(struct task_struct *p, int cpu) |
1750 | { | |
da0c1e65 | 1751 | if (task_on_rq_queued(p)) { |
ac66f547 | 1752 | struct rq *src_rq, *dst_rq; |
8a8c69c3 | 1753 | struct rq_flags srf, drf; |
ac66f547 PZ |
1754 | |
1755 | src_rq = task_rq(p); | |
1756 | dst_rq = cpu_rq(cpu); | |
1757 | ||
8a8c69c3 PZ |
1758 | rq_pin_lock(src_rq, &srf); |
1759 | rq_pin_lock(dst_rq, &drf); | |
1760 | ||
ac66f547 PZ |
1761 | deactivate_task(src_rq, p, 0); |
1762 | set_task_cpu(p, cpu); | |
1763 | activate_task(dst_rq, p, 0); | |
1764 | check_preempt_curr(dst_rq, p, 0); | |
8a8c69c3 PZ |
1765 | |
1766 | rq_unpin_lock(dst_rq, &drf); | |
1767 | rq_unpin_lock(src_rq, &srf); | |
1768 | ||
ac66f547 PZ |
1769 | } else { |
1770 | /* | |
1771 | * Task isn't running anymore; make it appear like we migrated | |
1772 | * it before it went to sleep. This means on wakeup we make the | |
d1ccc66d | 1773 | * previous CPU our target instead of where it really is. |
ac66f547 PZ |
1774 | */ |
1775 | p->wake_cpu = cpu; | |
1776 | } | |
1777 | } | |
1778 | ||
1779 | struct migration_swap_arg { | |
1780 | struct task_struct *src_task, *dst_task; | |
1781 | int src_cpu, dst_cpu; | |
1782 | }; | |
1783 | ||
1784 | static int migrate_swap_stop(void *data) | |
1785 | { | |
1786 | struct migration_swap_arg *arg = data; | |
1787 | struct rq *src_rq, *dst_rq; | |
1788 | int ret = -EAGAIN; | |
1789 | ||
62694cd5 PZ |
1790 | if (!cpu_active(arg->src_cpu) || !cpu_active(arg->dst_cpu)) |
1791 | return -EAGAIN; | |
1792 | ||
ac66f547 PZ |
1793 | src_rq = cpu_rq(arg->src_cpu); |
1794 | dst_rq = cpu_rq(arg->dst_cpu); | |
1795 | ||
74602315 PZ |
1796 | double_raw_lock(&arg->src_task->pi_lock, |
1797 | &arg->dst_task->pi_lock); | |
ac66f547 | 1798 | double_rq_lock(src_rq, dst_rq); |
62694cd5 | 1799 | |
ac66f547 PZ |
1800 | if (task_cpu(arg->dst_task) != arg->dst_cpu) |
1801 | goto unlock; | |
1802 | ||
1803 | if (task_cpu(arg->src_task) != arg->src_cpu) | |
1804 | goto unlock; | |
1805 | ||
3bd37062 | 1806 | if (!cpumask_test_cpu(arg->dst_cpu, arg->src_task->cpus_ptr)) |
ac66f547 PZ |
1807 | goto unlock; |
1808 | ||
3bd37062 | 1809 | if (!cpumask_test_cpu(arg->src_cpu, arg->dst_task->cpus_ptr)) |
ac66f547 PZ |
1810 | goto unlock; |
1811 | ||
1812 | __migrate_swap_task(arg->src_task, arg->dst_cpu); | |
1813 | __migrate_swap_task(arg->dst_task, arg->src_cpu); | |
1814 | ||
1815 | ret = 0; | |
1816 | ||
1817 | unlock: | |
1818 | double_rq_unlock(src_rq, dst_rq); | |
74602315 PZ |
1819 | raw_spin_unlock(&arg->dst_task->pi_lock); |
1820 | raw_spin_unlock(&arg->src_task->pi_lock); | |
ac66f547 PZ |
1821 | |
1822 | return ret; | |
1823 | } | |
1824 | ||
1825 | /* | |
1826 | * Cross migrate two tasks | |
1827 | */ | |
0ad4e3df SD |
1828 | int migrate_swap(struct task_struct *cur, struct task_struct *p, |
1829 | int target_cpu, int curr_cpu) | |
ac66f547 PZ |
1830 | { |
1831 | struct migration_swap_arg arg; | |
1832 | int ret = -EINVAL; | |
1833 | ||
ac66f547 PZ |
1834 | arg = (struct migration_swap_arg){ |
1835 | .src_task = cur, | |
0ad4e3df | 1836 | .src_cpu = curr_cpu, |
ac66f547 | 1837 | .dst_task = p, |
0ad4e3df | 1838 | .dst_cpu = target_cpu, |
ac66f547 PZ |
1839 | }; |
1840 | ||
1841 | if (arg.src_cpu == arg.dst_cpu) | |
1842 | goto out; | |
1843 | ||
6acce3ef PZ |
1844 | /* |
1845 | * These three tests are all lockless; this is OK since all of them | |
1846 | * will be re-checked with proper locks held further down the line. | |
1847 | */ | |
ac66f547 PZ |
1848 | if (!cpu_active(arg.src_cpu) || !cpu_active(arg.dst_cpu)) |
1849 | goto out; | |
1850 | ||
3bd37062 | 1851 | if (!cpumask_test_cpu(arg.dst_cpu, arg.src_task->cpus_ptr)) |
ac66f547 PZ |
1852 | goto out; |
1853 | ||
3bd37062 | 1854 | if (!cpumask_test_cpu(arg.src_cpu, arg.dst_task->cpus_ptr)) |
ac66f547 PZ |
1855 | goto out; |
1856 | ||
286549dc | 1857 | trace_sched_swap_numa(cur, arg.src_cpu, p, arg.dst_cpu); |
ac66f547 PZ |
1858 | ret = stop_two_cpus(arg.dst_cpu, arg.src_cpu, migrate_swap_stop, &arg); |
1859 | ||
1860 | out: | |
ac66f547 PZ |
1861 | return ret; |
1862 | } | |
0ad4e3df | 1863 | #endif /* CONFIG_NUMA_BALANCING */ |
ac66f547 | 1864 | |
1da177e4 LT |
1865 | /* |
1866 | * wait_task_inactive - wait for a thread to unschedule. | |
1867 | * | |
85ba2d86 RM |
1868 | * If @match_state is nonzero, it's the @p->state value just checked and |
1869 | * not expected to change. If it changes, i.e. @p might have woken up, | |
1870 | * then return zero. When we succeed in waiting for @p to be off its CPU, | |
1871 | * we return a positive number (its total switch count). If a second call | |
1872 | * a short while later returns the same number, the caller can be sure that | |
1873 | * @p has remained unscheduled the whole time. | |
1874 | * | |
1da177e4 LT |
1875 | * The caller must ensure that the task *will* unschedule sometime soon, |
1876 | * else this function might spin for a *long* time. This function can't | |
1877 | * be called with interrupts off, or it may introduce deadlock with | |
1878 | * smp_call_function() if an IPI is sent by the same process we are | |
1879 | * waiting to become inactive. | |
1880 | */ | |
85ba2d86 | 1881 | unsigned long wait_task_inactive(struct task_struct *p, long match_state) |
1da177e4 | 1882 | { |
da0c1e65 | 1883 | int running, queued; |
eb580751 | 1884 | struct rq_flags rf; |
85ba2d86 | 1885 | unsigned long ncsw; |
70b97a7f | 1886 | struct rq *rq; |
1da177e4 | 1887 | |
3a5c359a AK |
1888 | for (;;) { |
1889 | /* | |
1890 | * We do the initial early heuristics without holding | |
1891 | * any task-queue locks at all. We'll only try to get | |
1892 | * the runqueue lock when things look like they will | |
1893 | * work out! | |
1894 | */ | |
1895 | rq = task_rq(p); | |
fa490cfd | 1896 | |
3a5c359a AK |
1897 | /* |
1898 | * If the task is actively running on another CPU | |
1899 | * still, just relax and busy-wait without holding | |
1900 | * any locks. | |
1901 | * | |
1902 | * NOTE! Since we don't hold any locks, it's not | |
1903 | * even sure that "rq" stays as the right runqueue! | |
1904 | * But we don't care, since "task_running()" will | |
1905 | * return false if the runqueue has changed and p | |
1906 | * is actually now running somewhere else! | |
1907 | */ | |
85ba2d86 RM |
1908 | while (task_running(rq, p)) { |
1909 | if (match_state && unlikely(p->state != match_state)) | |
1910 | return 0; | |
3a5c359a | 1911 | cpu_relax(); |
85ba2d86 | 1912 | } |
fa490cfd | 1913 | |
3a5c359a AK |
1914 | /* |
1915 | * Ok, time to look more closely! We need the rq | |
1916 | * lock now, to be *sure*. If we're wrong, we'll | |
1917 | * just go back and repeat. | |
1918 | */ | |
eb580751 | 1919 | rq = task_rq_lock(p, &rf); |
27a9da65 | 1920 | trace_sched_wait_task(p); |
3a5c359a | 1921 | running = task_running(rq, p); |
da0c1e65 | 1922 | queued = task_on_rq_queued(p); |
85ba2d86 | 1923 | ncsw = 0; |
f31e11d8 | 1924 | if (!match_state || p->state == match_state) |
93dcf55f | 1925 | ncsw = p->nvcsw | LONG_MIN; /* sets MSB */ |
eb580751 | 1926 | task_rq_unlock(rq, p, &rf); |
fa490cfd | 1927 | |
85ba2d86 RM |
1928 | /* |
1929 | * If it changed from the expected state, bail out now. | |
1930 | */ | |
1931 | if (unlikely(!ncsw)) | |
1932 | break; | |
1933 | ||
3a5c359a AK |
1934 | /* |
1935 | * Was it really running after all now that we | |
1936 | * checked with the proper locks actually held? | |
1937 | * | |
1938 | * Oops. Go back and try again.. | |
1939 | */ | |
1940 | if (unlikely(running)) { | |
1941 | cpu_relax(); | |
1942 | continue; | |
1943 | } | |
fa490cfd | 1944 | |
3a5c359a AK |
1945 | /* |
1946 | * It's not enough that it's not actively running, | |
1947 | * it must be off the runqueue _entirely_, and not | |
1948 | * preempted! | |
1949 | * | |
80dd99b3 | 1950 | * So if it was still runnable (but just not actively |
3a5c359a AK |
1951 | * running right now), it's preempted, and we should |
1952 | * yield - it could be a while. | |
1953 | */ | |
da0c1e65 | 1954 | if (unlikely(queued)) { |
8b0e1953 | 1955 | ktime_t to = NSEC_PER_SEC / HZ; |
8eb90c30 TG |
1956 | |
1957 | set_current_state(TASK_UNINTERRUPTIBLE); | |
1958 | schedule_hrtimeout(&to, HRTIMER_MODE_REL); | |
3a5c359a AK |
1959 | continue; |
1960 | } | |
fa490cfd | 1961 | |
3a5c359a AK |
1962 | /* |
1963 | * Ahh, all good. It wasn't running, and it wasn't | |
1964 | * runnable, which means that it will never become | |
1965 | * running in the future either. We're all done! | |
1966 | */ | |
1967 | break; | |
1968 | } | |
85ba2d86 RM |
1969 | |
1970 | return ncsw; | |
1da177e4 LT |
1971 | } |
1972 | ||
1973 | /*** | |
1974 | * kick_process - kick a running thread to enter/exit the kernel | |
1975 | * @p: the to-be-kicked thread | |
1976 | * | |
1977 | * Cause a process which is running on another CPU to enter | |
1978 | * kernel-mode, without any delay. (to get signals handled.) | |
1979 | * | |
25985edc | 1980 | * NOTE: this function doesn't have to take the runqueue lock, |
1da177e4 LT |
1981 | * because all it wants to ensure is that the remote task enters |
1982 | * the kernel. If the IPI races and the task has been migrated | |
1983 | * to another CPU then no harm is done and the purpose has been | |
1984 | * achieved as well. | |
1985 | */ | |
36c8b586 | 1986 | void kick_process(struct task_struct *p) |
1da177e4 LT |
1987 | { |
1988 | int cpu; | |
1989 | ||
1990 | preempt_disable(); | |
1991 | cpu = task_cpu(p); | |
1992 | if ((cpu != smp_processor_id()) && task_curr(p)) | |
1993 | smp_send_reschedule(cpu); | |
1994 | preempt_enable(); | |
1995 | } | |
b43e3521 | 1996 | EXPORT_SYMBOL_GPL(kick_process); |
1da177e4 | 1997 | |
30da688e | 1998 | /* |
3bd37062 | 1999 | * ->cpus_ptr is protected by both rq->lock and p->pi_lock |
e9d867a6 PZI |
2000 | * |
2001 | * A few notes on cpu_active vs cpu_online: | |
2002 | * | |
2003 | * - cpu_active must be a subset of cpu_online | |
2004 | * | |
97fb7a0a | 2005 | * - on CPU-up we allow per-CPU kthreads on the online && !active CPU, |
e9d867a6 | 2006 | * see __set_cpus_allowed_ptr(). At this point the newly online |
d1ccc66d | 2007 | * CPU isn't yet part of the sched domains, and balancing will not |
e9d867a6 PZI |
2008 | * see it. |
2009 | * | |
d1ccc66d | 2010 | * - on CPU-down we clear cpu_active() to mask the sched domains and |
e9d867a6 | 2011 | * avoid the load balancer to place new tasks on the to be removed |
d1ccc66d | 2012 | * CPU. Existing tasks will remain running there and will be taken |
e9d867a6 PZI |
2013 | * off. |
2014 | * | |
2015 | * This means that fallback selection must not select !active CPUs. | |
2016 | * And can assume that any active CPU must be online. Conversely | |
2017 | * select_task_rq() below may allow selection of !active CPUs in order | |
2018 | * to satisfy the above rules. | |
30da688e | 2019 | */ |
5da9a0fb PZ |
2020 | static int select_fallback_rq(int cpu, struct task_struct *p) |
2021 | { | |
aa00d89c TC |
2022 | int nid = cpu_to_node(cpu); |
2023 | const struct cpumask *nodemask = NULL; | |
2baab4e9 PZ |
2024 | enum { cpuset, possible, fail } state = cpuset; |
2025 | int dest_cpu; | |
5da9a0fb | 2026 | |
aa00d89c | 2027 | /* |
d1ccc66d IM |
2028 | * If the node that the CPU is on has been offlined, cpu_to_node() |
2029 | * will return -1. There is no CPU on the node, and we should | |
2030 | * select the CPU on the other node. | |
aa00d89c TC |
2031 | */ |
2032 | if (nid != -1) { | |
2033 | nodemask = cpumask_of_node(nid); | |
2034 | ||
2035 | /* Look for allowed, online CPU in same node. */ | |
2036 | for_each_cpu(dest_cpu, nodemask) { | |
aa00d89c TC |
2037 | if (!cpu_active(dest_cpu)) |
2038 | continue; | |
3bd37062 | 2039 | if (cpumask_test_cpu(dest_cpu, p->cpus_ptr)) |
aa00d89c TC |
2040 | return dest_cpu; |
2041 | } | |
2baab4e9 | 2042 | } |
5da9a0fb | 2043 | |
2baab4e9 PZ |
2044 | for (;;) { |
2045 | /* Any allowed, online CPU? */ | |
3bd37062 | 2046 | for_each_cpu(dest_cpu, p->cpus_ptr) { |
175f0e25 | 2047 | if (!is_cpu_allowed(p, dest_cpu)) |
2baab4e9 | 2048 | continue; |
175f0e25 | 2049 | |
2baab4e9 PZ |
2050 | goto out; |
2051 | } | |
5da9a0fb | 2052 | |
e73e85f0 | 2053 | /* No more Mr. Nice Guy. */ |
2baab4e9 PZ |
2054 | switch (state) { |
2055 | case cpuset: | |
e73e85f0 ON |
2056 | if (IS_ENABLED(CONFIG_CPUSETS)) { |
2057 | cpuset_cpus_allowed_fallback(p); | |
2058 | state = possible; | |
2059 | break; | |
2060 | } | |
d1ccc66d | 2061 | /* Fall-through */ |
2baab4e9 PZ |
2062 | case possible: |
2063 | do_set_cpus_allowed(p, cpu_possible_mask); | |
2064 | state = fail; | |
2065 | break; | |
2066 | ||
2067 | case fail: | |
2068 | BUG(); | |
2069 | break; | |
2070 | } | |
2071 | } | |
2072 | ||
2073 | out: | |
2074 | if (state != cpuset) { | |
2075 | /* | |
2076 | * Don't tell them about moving exiting tasks or | |
2077 | * kernel threads (both mm NULL), since they never | |
2078 | * leave kernel. | |
2079 | */ | |
2080 | if (p->mm && printk_ratelimit()) { | |
aac74dc4 | 2081 | printk_deferred("process %d (%s) no longer affine to cpu%d\n", |
2baab4e9 PZ |
2082 | task_pid_nr(p), p->comm, cpu); |
2083 | } | |
5da9a0fb PZ |
2084 | } |
2085 | ||
2086 | return dest_cpu; | |
2087 | } | |
2088 | ||
e2912009 | 2089 | /* |
3bd37062 | 2090 | * The caller (fork, wakeup) owns p->pi_lock, ->cpus_ptr is stable. |
e2912009 | 2091 | */ |
970b13ba | 2092 | static inline |
ac66f547 | 2093 | int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags) |
970b13ba | 2094 | { |
cbce1a68 PZ |
2095 | lockdep_assert_held(&p->pi_lock); |
2096 | ||
4b53a341 | 2097 | if (p->nr_cpus_allowed > 1) |
6c1d9410 | 2098 | cpu = p->sched_class->select_task_rq(p, cpu, sd_flags, wake_flags); |
e9d867a6 | 2099 | else |
3bd37062 | 2100 | cpu = cpumask_any(p->cpus_ptr); |
e2912009 PZ |
2101 | |
2102 | /* | |
2103 | * In order not to call set_task_cpu() on a blocking task we need | |
3bd37062 | 2104 | * to rely on ttwu() to place the task on a valid ->cpus_ptr |
d1ccc66d | 2105 | * CPU. |
e2912009 PZ |
2106 | * |
2107 | * Since this is common to all placement strategies, this lives here. | |
2108 | * | |
2109 | * [ this allows ->select_task() to simply return task_cpu(p) and | |
2110 | * not worry about this generic constraint ] | |
2111 | */ | |
7af443ee | 2112 | if (unlikely(!is_cpu_allowed(p, cpu))) |
5da9a0fb | 2113 | cpu = select_fallback_rq(task_cpu(p), p); |
e2912009 PZ |
2114 | |
2115 | return cpu; | |
970b13ba | 2116 | } |
09a40af5 | 2117 | |
f5832c19 NP |
2118 | void sched_set_stop_task(int cpu, struct task_struct *stop) |
2119 | { | |
2120 | struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 }; | |
2121 | struct task_struct *old_stop = cpu_rq(cpu)->stop; | |
2122 | ||
2123 | if (stop) { | |
2124 | /* | |
2125 | * Make it appear like a SCHED_FIFO task, its something | |
2126 | * userspace knows about and won't get confused about. | |
2127 | * | |
2128 | * Also, it will make PI more or less work without too | |
2129 | * much confusion -- but then, stop work should not | |
2130 | * rely on PI working anyway. | |
2131 | */ | |
2132 | sched_setscheduler_nocheck(stop, SCHED_FIFO, ¶m); | |
2133 | ||
2134 | stop->sched_class = &stop_sched_class; | |
2135 | } | |
2136 | ||
2137 | cpu_rq(cpu)->stop = stop; | |
2138 | ||
2139 | if (old_stop) { | |
2140 | /* | |
2141 | * Reset it back to a normal scheduling class so that | |
2142 | * it can die in pieces. | |
2143 | */ | |
2144 | old_stop->sched_class = &rt_sched_class; | |
2145 | } | |
2146 | } | |
2147 | ||
25834c73 PZ |
2148 | #else |
2149 | ||
2150 | static inline int __set_cpus_allowed_ptr(struct task_struct *p, | |
2151 | const struct cpumask *new_mask, bool check) | |
2152 | { | |
2153 | return set_cpus_allowed_ptr(p, new_mask); | |
2154 | } | |
2155 | ||
5cc389bc | 2156 | #endif /* CONFIG_SMP */ |
970b13ba | 2157 | |
d7c01d27 | 2158 | static void |
b84cb5df | 2159 | ttwu_stat(struct task_struct *p, int cpu, int wake_flags) |
9ed3811a | 2160 | { |
4fa8d299 | 2161 | struct rq *rq; |
b84cb5df | 2162 | |
4fa8d299 JP |
2163 | if (!schedstat_enabled()) |
2164 | return; | |
2165 | ||
2166 | rq = this_rq(); | |
d7c01d27 | 2167 | |
4fa8d299 JP |
2168 | #ifdef CONFIG_SMP |
2169 | if (cpu == rq->cpu) { | |
b85c8b71 PZ |
2170 | __schedstat_inc(rq->ttwu_local); |
2171 | __schedstat_inc(p->se.statistics.nr_wakeups_local); | |
d7c01d27 PZ |
2172 | } else { |
2173 | struct sched_domain *sd; | |
2174 | ||
b85c8b71 | 2175 | __schedstat_inc(p->se.statistics.nr_wakeups_remote); |
057f3fad | 2176 | rcu_read_lock(); |
4fa8d299 | 2177 | for_each_domain(rq->cpu, sd) { |
d7c01d27 | 2178 | if (cpumask_test_cpu(cpu, sched_domain_span(sd))) { |
b85c8b71 | 2179 | __schedstat_inc(sd->ttwu_wake_remote); |
d7c01d27 PZ |
2180 | break; |
2181 | } | |
2182 | } | |
057f3fad | 2183 | rcu_read_unlock(); |
d7c01d27 | 2184 | } |
f339b9dc PZ |
2185 | |
2186 | if (wake_flags & WF_MIGRATED) | |
b85c8b71 | 2187 | __schedstat_inc(p->se.statistics.nr_wakeups_migrate); |
d7c01d27 PZ |
2188 | #endif /* CONFIG_SMP */ |
2189 | ||
b85c8b71 PZ |
2190 | __schedstat_inc(rq->ttwu_count); |
2191 | __schedstat_inc(p->se.statistics.nr_wakeups); | |
d7c01d27 PZ |
2192 | |
2193 | if (wake_flags & WF_SYNC) | |
b85c8b71 | 2194 | __schedstat_inc(p->se.statistics.nr_wakeups_sync); |
d7c01d27 PZ |
2195 | } |
2196 | ||
23f41eeb PZ |
2197 | /* |
2198 | * Mark the task runnable and perform wakeup-preemption. | |
2199 | */ | |
e7904a28 | 2200 | static void ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags, |
d8ac8971 | 2201 | struct rq_flags *rf) |
9ed3811a | 2202 | { |
9ed3811a | 2203 | check_preempt_curr(rq, p, wake_flags); |
9ed3811a | 2204 | p->state = TASK_RUNNING; |
fbd705a0 PZ |
2205 | trace_sched_wakeup(p); |
2206 | ||
9ed3811a | 2207 | #ifdef CONFIG_SMP |
4c9a4bc8 PZ |
2208 | if (p->sched_class->task_woken) { |
2209 | /* | |
cbce1a68 PZ |
2210 | * Our task @p is fully woken up and running; so its safe to |
2211 | * drop the rq->lock, hereafter rq is only used for statistics. | |
4c9a4bc8 | 2212 | */ |
d8ac8971 | 2213 | rq_unpin_lock(rq, rf); |
9ed3811a | 2214 | p->sched_class->task_woken(rq, p); |
d8ac8971 | 2215 | rq_repin_lock(rq, rf); |
4c9a4bc8 | 2216 | } |
9ed3811a | 2217 | |
e69c6341 | 2218 | if (rq->idle_stamp) { |
78becc27 | 2219 | u64 delta = rq_clock(rq) - rq->idle_stamp; |
9bd721c5 | 2220 | u64 max = 2*rq->max_idle_balance_cost; |
9ed3811a | 2221 | |
abfafa54 JL |
2222 | update_avg(&rq->avg_idle, delta); |
2223 | ||
2224 | if (rq->avg_idle > max) | |
9ed3811a | 2225 | rq->avg_idle = max; |
abfafa54 | 2226 | |
9ed3811a TH |
2227 | rq->idle_stamp = 0; |
2228 | } | |
2229 | #endif | |
2230 | } | |
2231 | ||
c05fbafb | 2232 | static void |
e7904a28 | 2233 | ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags, |
d8ac8971 | 2234 | struct rq_flags *rf) |
c05fbafb | 2235 | { |
77558e4d | 2236 | int en_flags = ENQUEUE_WAKEUP | ENQUEUE_NOCLOCK; |
b5179ac7 | 2237 | |
cbce1a68 PZ |
2238 | lockdep_assert_held(&rq->lock); |
2239 | ||
c05fbafb PZ |
2240 | #ifdef CONFIG_SMP |
2241 | if (p->sched_contributes_to_load) | |
2242 | rq->nr_uninterruptible--; | |
b5179ac7 | 2243 | |
b5179ac7 | 2244 | if (wake_flags & WF_MIGRATED) |
59efa0ba | 2245 | en_flags |= ENQUEUE_MIGRATED; |
c05fbafb PZ |
2246 | #endif |
2247 | ||
1b174a2c | 2248 | activate_task(rq, p, en_flags); |
d8ac8971 | 2249 | ttwu_do_wakeup(rq, p, wake_flags, rf); |
c05fbafb PZ |
2250 | } |
2251 | ||
2252 | /* | |
2253 | * Called in case the task @p isn't fully descheduled from its runqueue, | |
2254 | * in this case we must do a remote wakeup. Its a 'light' wakeup though, | |
2255 | * since all we need to do is flip p->state to TASK_RUNNING, since | |
2256 | * the task is still ->on_rq. | |
2257 | */ | |
2258 | static int ttwu_remote(struct task_struct *p, int wake_flags) | |
2259 | { | |
eb580751 | 2260 | struct rq_flags rf; |
c05fbafb PZ |
2261 | struct rq *rq; |
2262 | int ret = 0; | |
2263 | ||
eb580751 | 2264 | rq = __task_rq_lock(p, &rf); |
da0c1e65 | 2265 | if (task_on_rq_queued(p)) { |
1ad4ec0d FW |
2266 | /* check_preempt_curr() may use rq clock */ |
2267 | update_rq_clock(rq); | |
d8ac8971 | 2268 | ttwu_do_wakeup(rq, p, wake_flags, &rf); |
c05fbafb PZ |
2269 | ret = 1; |
2270 | } | |
eb580751 | 2271 | __task_rq_unlock(rq, &rf); |
c05fbafb PZ |
2272 | |
2273 | return ret; | |
2274 | } | |
2275 | ||
317f3941 | 2276 | #ifdef CONFIG_SMP |
e3baac47 | 2277 | void sched_ttwu_pending(void) |
317f3941 PZ |
2278 | { |
2279 | struct rq *rq = this_rq(); | |
fa14ff4a | 2280 | struct llist_node *llist = llist_del_all(&rq->wake_list); |
73215849 | 2281 | struct task_struct *p, *t; |
d8ac8971 | 2282 | struct rq_flags rf; |
317f3941 | 2283 | |
e3baac47 PZ |
2284 | if (!llist) |
2285 | return; | |
2286 | ||
8a8c69c3 | 2287 | rq_lock_irqsave(rq, &rf); |
77558e4d | 2288 | update_rq_clock(rq); |
317f3941 | 2289 | |
73215849 BP |
2290 | llist_for_each_entry_safe(p, t, llist, wake_entry) |
2291 | ttwu_do_activate(rq, p, p->sched_remote_wakeup ? WF_MIGRATED : 0, &rf); | |
317f3941 | 2292 | |
8a8c69c3 | 2293 | rq_unlock_irqrestore(rq, &rf); |
317f3941 PZ |
2294 | } |
2295 | ||
2296 | void scheduler_ipi(void) | |
2297 | { | |
f27dde8d PZ |
2298 | /* |
2299 | * Fold TIF_NEED_RESCHED into the preempt_count; anybody setting | |
2300 | * TIF_NEED_RESCHED remotely (for the first time) will also send | |
2301 | * this IPI. | |
2302 | */ | |
8cb75e0c | 2303 | preempt_fold_need_resched(); |
f27dde8d | 2304 | |
fd2ac4f4 | 2305 | if (llist_empty(&this_rq()->wake_list) && !got_nohz_idle_kick()) |
c5d753a5 PZ |
2306 | return; |
2307 | ||
2308 | /* | |
2309 | * Not all reschedule IPI handlers call irq_enter/irq_exit, since | |
2310 | * traditionally all their work was done from the interrupt return | |
2311 | * path. Now that we actually do some work, we need to make sure | |
2312 | * we do call them. | |
2313 | * | |
2314 | * Some archs already do call them, luckily irq_enter/exit nest | |
2315 | * properly. | |
2316 | * | |
2317 | * Arguably we should visit all archs and update all handlers, | |
2318 | * however a fair share of IPIs are still resched only so this would | |
2319 | * somewhat pessimize the simple resched case. | |
2320 | */ | |
2321 | irq_enter(); | |
fa14ff4a | 2322 | sched_ttwu_pending(); |
ca38062e SS |
2323 | |
2324 | /* | |
2325 | * Check if someone kicked us for doing the nohz idle load balance. | |
2326 | */ | |
873b4c65 | 2327 | if (unlikely(got_nohz_idle_kick())) { |
6eb57e0d | 2328 | this_rq()->idle_balance = 1; |
ca38062e | 2329 | raise_softirq_irqoff(SCHED_SOFTIRQ); |
6eb57e0d | 2330 | } |
c5d753a5 | 2331 | irq_exit(); |
317f3941 PZ |
2332 | } |
2333 | ||
b7e7ade3 | 2334 | static void ttwu_queue_remote(struct task_struct *p, int cpu, int wake_flags) |
317f3941 | 2335 | { |
e3baac47 PZ |
2336 | struct rq *rq = cpu_rq(cpu); |
2337 | ||
b7e7ade3 PZ |
2338 | p->sched_remote_wakeup = !!(wake_flags & WF_MIGRATED); |
2339 | ||
e3baac47 PZ |
2340 | if (llist_add(&p->wake_entry, &cpu_rq(cpu)->wake_list)) { |
2341 | if (!set_nr_if_polling(rq->idle)) | |
2342 | smp_send_reschedule(cpu); | |
2343 | else | |
2344 | trace_sched_wake_idle_without_ipi(cpu); | |
2345 | } | |
317f3941 | 2346 | } |
d6aa8f85 | 2347 | |
f6be8af1 CL |
2348 | void wake_up_if_idle(int cpu) |
2349 | { | |
2350 | struct rq *rq = cpu_rq(cpu); | |
8a8c69c3 | 2351 | struct rq_flags rf; |
f6be8af1 | 2352 | |
fd7de1e8 AL |
2353 | rcu_read_lock(); |
2354 | ||
2355 | if (!is_idle_task(rcu_dereference(rq->curr))) | |
2356 | goto out; | |
f6be8af1 CL |
2357 | |
2358 | if (set_nr_if_polling(rq->idle)) { | |
2359 | trace_sched_wake_idle_without_ipi(cpu); | |
2360 | } else { | |
8a8c69c3 | 2361 | rq_lock_irqsave(rq, &rf); |
f6be8af1 CL |
2362 | if (is_idle_task(rq->curr)) |
2363 | smp_send_reschedule(cpu); | |
d1ccc66d | 2364 | /* Else CPU is not idle, do nothing here: */ |
8a8c69c3 | 2365 | rq_unlock_irqrestore(rq, &rf); |
f6be8af1 | 2366 | } |
fd7de1e8 AL |
2367 | |
2368 | out: | |
2369 | rcu_read_unlock(); | |
f6be8af1 CL |
2370 | } |
2371 | ||
39be3501 | 2372 | bool cpus_share_cache(int this_cpu, int that_cpu) |
518cd623 PZ |
2373 | { |
2374 | return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu); | |
2375 | } | |
d6aa8f85 | 2376 | #endif /* CONFIG_SMP */ |
317f3941 | 2377 | |
b5179ac7 | 2378 | static void ttwu_queue(struct task_struct *p, int cpu, int wake_flags) |
c05fbafb PZ |
2379 | { |
2380 | struct rq *rq = cpu_rq(cpu); | |
d8ac8971 | 2381 | struct rq_flags rf; |
c05fbafb | 2382 | |
17d9f311 | 2383 | #if defined(CONFIG_SMP) |
39be3501 | 2384 | if (sched_feat(TTWU_QUEUE) && !cpus_share_cache(smp_processor_id(), cpu)) { |
d1ccc66d | 2385 | sched_clock_cpu(cpu); /* Sync clocks across CPUs */ |
b7e7ade3 | 2386 | ttwu_queue_remote(p, cpu, wake_flags); |
317f3941 PZ |
2387 | return; |
2388 | } | |
2389 | #endif | |
2390 | ||
8a8c69c3 | 2391 | rq_lock(rq, &rf); |
77558e4d | 2392 | update_rq_clock(rq); |
d8ac8971 | 2393 | ttwu_do_activate(rq, p, wake_flags, &rf); |
8a8c69c3 | 2394 | rq_unlock(rq, &rf); |
9ed3811a TH |
2395 | } |
2396 | ||
8643cda5 PZ |
2397 | /* |
2398 | * Notes on Program-Order guarantees on SMP systems. | |
2399 | * | |
2400 | * MIGRATION | |
2401 | * | |
2402 | * The basic program-order guarantee on SMP systems is that when a task [t] | |
d1ccc66d IM |
2403 | * migrates, all its activity on its old CPU [c0] happens-before any subsequent |
2404 | * execution on its new CPU [c1]. | |
8643cda5 PZ |
2405 | * |
2406 | * For migration (of runnable tasks) this is provided by the following means: | |
2407 | * | |
2408 | * A) UNLOCK of the rq(c0)->lock scheduling out task t | |
2409 | * B) migration for t is required to synchronize *both* rq(c0)->lock and | |
2410 | * rq(c1)->lock (if not at the same time, then in that order). | |
2411 | * C) LOCK of the rq(c1)->lock scheduling in task | |
2412 | * | |
7696f991 | 2413 | * Release/acquire chaining guarantees that B happens after A and C after B. |
d1ccc66d | 2414 | * Note: the CPU doing B need not be c0 or c1 |
8643cda5 PZ |
2415 | * |
2416 | * Example: | |
2417 | * | |
2418 | * CPU0 CPU1 CPU2 | |
2419 | * | |
2420 | * LOCK rq(0)->lock | |
2421 | * sched-out X | |
2422 | * sched-in Y | |
2423 | * UNLOCK rq(0)->lock | |
2424 | * | |
2425 | * LOCK rq(0)->lock // orders against CPU0 | |
2426 | * dequeue X | |
2427 | * UNLOCK rq(0)->lock | |
2428 | * | |
2429 | * LOCK rq(1)->lock | |
2430 | * enqueue X | |
2431 | * UNLOCK rq(1)->lock | |
2432 | * | |
2433 | * LOCK rq(1)->lock // orders against CPU2 | |
2434 | * sched-out Z | |
2435 | * sched-in X | |
2436 | * UNLOCK rq(1)->lock | |
2437 | * | |
2438 | * | |
2439 | * BLOCKING -- aka. SLEEP + WAKEUP | |
2440 | * | |
2441 | * For blocking we (obviously) need to provide the same guarantee as for | |
2442 | * migration. However the means are completely different as there is no lock | |
2443 | * chain to provide order. Instead we do: | |
2444 | * | |
2445 | * 1) smp_store_release(X->on_cpu, 0) | |
1f03e8d2 | 2446 | * 2) smp_cond_load_acquire(!X->on_cpu) |
8643cda5 PZ |
2447 | * |
2448 | * Example: | |
2449 | * | |
2450 | * CPU0 (schedule) CPU1 (try_to_wake_up) CPU2 (schedule) | |
2451 | * | |
2452 | * LOCK rq(0)->lock LOCK X->pi_lock | |
2453 | * dequeue X | |
2454 | * sched-out X | |
2455 | * smp_store_release(X->on_cpu, 0); | |
2456 | * | |
1f03e8d2 | 2457 | * smp_cond_load_acquire(&X->on_cpu, !VAL); |
8643cda5 PZ |
2458 | * X->state = WAKING |
2459 | * set_task_cpu(X,2) | |
2460 | * | |
2461 | * LOCK rq(2)->lock | |
2462 | * enqueue X | |
2463 | * X->state = RUNNING | |
2464 | * UNLOCK rq(2)->lock | |
2465 | * | |
2466 | * LOCK rq(2)->lock // orders against CPU1 | |
2467 | * sched-out Z | |
2468 | * sched-in X | |
2469 | * UNLOCK rq(2)->lock | |
2470 | * | |
2471 | * UNLOCK X->pi_lock | |
2472 | * UNLOCK rq(0)->lock | |
2473 | * | |
2474 | * | |
7696f991 AP |
2475 | * However, for wakeups there is a second guarantee we must provide, namely we |
2476 | * must ensure that CONDITION=1 done by the caller can not be reordered with | |
2477 | * accesses to the task state; see try_to_wake_up() and set_current_state(). | |
8643cda5 PZ |
2478 | */ |
2479 | ||
9ed3811a | 2480 | /** |
1da177e4 | 2481 | * try_to_wake_up - wake up a thread |
9ed3811a | 2482 | * @p: the thread to be awakened |
1da177e4 | 2483 | * @state: the mask of task states that can be woken |
9ed3811a | 2484 | * @wake_flags: wake modifier flags (WF_*) |
1da177e4 | 2485 | * |
a2250238 | 2486 | * If (@state & @p->state) @p->state = TASK_RUNNING. |
1da177e4 | 2487 | * |
a2250238 PZ |
2488 | * If the task was not queued/runnable, also place it back on a runqueue. |
2489 | * | |
2490 | * Atomic against schedule() which would dequeue a task, also see | |
2491 | * set_current_state(). | |
2492 | * | |
7696f991 AP |
2493 | * This function executes a full memory barrier before accessing the task |
2494 | * state; see set_current_state(). | |
2495 | * | |
a2250238 PZ |
2496 | * Return: %true if @p->state changes (an actual wakeup was done), |
2497 | * %false otherwise. | |
1da177e4 | 2498 | */ |
e4a52bcb PZ |
2499 | static int |
2500 | try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags) | |
1da177e4 | 2501 | { |
1da177e4 | 2502 | unsigned long flags; |
c05fbafb | 2503 | int cpu, success = 0; |
2398f2c6 | 2504 | |
e3d85487 | 2505 | preempt_disable(); |
aacedf26 PZ |
2506 | if (p == current) { |
2507 | /* | |
2508 | * We're waking current, this means 'p->on_rq' and 'task_cpu(p) | |
2509 | * == smp_processor_id()'. Together this means we can special | |
2510 | * case the whole 'p->on_rq && ttwu_remote()' case below | |
2511 | * without taking any locks. | |
2512 | * | |
2513 | * In particular: | |
2514 | * - we rely on Program-Order guarantees for all the ordering, | |
2515 | * - we're serialized against set_special_state() by virtue of | |
2516 | * it disabling IRQs (this allows not taking ->pi_lock). | |
2517 | */ | |
2518 | if (!(p->state & state)) | |
e3d85487 | 2519 | goto out; |
aacedf26 PZ |
2520 | |
2521 | success = 1; | |
2522 | cpu = task_cpu(p); | |
2523 | trace_sched_waking(p); | |
2524 | p->state = TASK_RUNNING; | |
2525 | trace_sched_wakeup(p); | |
2526 | goto out; | |
2527 | } | |
2528 | ||
e0acd0a6 ON |
2529 | /* |
2530 | * If we are going to wake up a thread waiting for CONDITION we | |
2531 | * need to ensure that CONDITION=1 done by the caller can not be | |
2532 | * reordered with p->state check below. This pairs with mb() in | |
2533 | * set_current_state() the waiting thread does. | |
2534 | */ | |
013fdb80 | 2535 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
d89e588c | 2536 | smp_mb__after_spinlock(); |
e9c84311 | 2537 | if (!(p->state & state)) |
aacedf26 | 2538 | goto unlock; |
1da177e4 | 2539 | |
fbd705a0 PZ |
2540 | trace_sched_waking(p); |
2541 | ||
d1ccc66d IM |
2542 | /* We're going to change ->state: */ |
2543 | success = 1; | |
1da177e4 | 2544 | cpu = task_cpu(p); |
1da177e4 | 2545 | |
135e8c92 BS |
2546 | /* |
2547 | * Ensure we load p->on_rq _after_ p->state, otherwise it would | |
2548 | * be possible to, falsely, observe p->on_rq == 0 and get stuck | |
2549 | * in smp_cond_load_acquire() below. | |
2550 | * | |
3d85b270 AP |
2551 | * sched_ttwu_pending() try_to_wake_up() |
2552 | * STORE p->on_rq = 1 LOAD p->state | |
2553 | * UNLOCK rq->lock | |
2554 | * | |
2555 | * __schedule() (switch to task 'p') | |
2556 | * LOCK rq->lock smp_rmb(); | |
2557 | * smp_mb__after_spinlock(); | |
2558 | * UNLOCK rq->lock | |
135e8c92 BS |
2559 | * |
2560 | * [task p] | |
3d85b270 | 2561 | * STORE p->state = UNINTERRUPTIBLE LOAD p->on_rq |
135e8c92 | 2562 | * |
3d85b270 AP |
2563 | * Pairs with the LOCK+smp_mb__after_spinlock() on rq->lock in |
2564 | * __schedule(). See the comment for smp_mb__after_spinlock(). | |
135e8c92 BS |
2565 | */ |
2566 | smp_rmb(); | |
c05fbafb | 2567 | if (p->on_rq && ttwu_remote(p, wake_flags)) |
aacedf26 | 2568 | goto unlock; |
1da177e4 | 2569 | |
1da177e4 | 2570 | #ifdef CONFIG_SMP |
ecf7d01c PZ |
2571 | /* |
2572 | * Ensure we load p->on_cpu _after_ p->on_rq, otherwise it would be | |
2573 | * possible to, falsely, observe p->on_cpu == 0. | |
2574 | * | |
2575 | * One must be running (->on_cpu == 1) in order to remove oneself | |
2576 | * from the runqueue. | |
2577 | * | |
3d85b270 AP |
2578 | * __schedule() (switch to task 'p') try_to_wake_up() |
2579 | * STORE p->on_cpu = 1 LOAD p->on_rq | |
2580 | * UNLOCK rq->lock | |
2581 | * | |
2582 | * __schedule() (put 'p' to sleep) | |
2583 | * LOCK rq->lock smp_rmb(); | |
2584 | * smp_mb__after_spinlock(); | |
2585 | * STORE p->on_rq = 0 LOAD p->on_cpu | |
ecf7d01c | 2586 | * |
3d85b270 AP |
2587 | * Pairs with the LOCK+smp_mb__after_spinlock() on rq->lock in |
2588 | * __schedule(). See the comment for smp_mb__after_spinlock(). | |
ecf7d01c PZ |
2589 | */ |
2590 | smp_rmb(); | |
2591 | ||
e9c84311 | 2592 | /* |
d1ccc66d | 2593 | * If the owning (remote) CPU is still in the middle of schedule() with |
c05fbafb | 2594 | * this task as prev, wait until its done referencing the task. |
b75a2253 | 2595 | * |
31cb1bc0 | 2596 | * Pairs with the smp_store_release() in finish_task(). |
b75a2253 PZ |
2597 | * |
2598 | * This ensures that tasks getting woken will be fully ordered against | |
2599 | * their previous state and preserve Program Order. | |
0970d299 | 2600 | */ |
1f03e8d2 | 2601 | smp_cond_load_acquire(&p->on_cpu, !VAL); |
1da177e4 | 2602 | |
a8e4f2ea | 2603 | p->sched_contributes_to_load = !!task_contributes_to_load(p); |
e9c84311 | 2604 | p->state = TASK_WAKING; |
e7693a36 | 2605 | |
e33a9bba | 2606 | if (p->in_iowait) { |
c96f5471 | 2607 | delayacct_blkio_end(p); |
e33a9bba TH |
2608 | atomic_dec(&task_rq(p)->nr_iowait); |
2609 | } | |
2610 | ||
ac66f547 | 2611 | cpu = select_task_rq(p, p->wake_cpu, SD_BALANCE_WAKE, wake_flags); |
f339b9dc PZ |
2612 | if (task_cpu(p) != cpu) { |
2613 | wake_flags |= WF_MIGRATED; | |
eb414681 | 2614 | psi_ttwu_dequeue(p); |
e4a52bcb | 2615 | set_task_cpu(p, cpu); |
f339b9dc | 2616 | } |
e33a9bba TH |
2617 | |
2618 | #else /* CONFIG_SMP */ | |
2619 | ||
2620 | if (p->in_iowait) { | |
c96f5471 | 2621 | delayacct_blkio_end(p); |
e33a9bba TH |
2622 | atomic_dec(&task_rq(p)->nr_iowait); |
2623 | } | |
2624 | ||
1da177e4 | 2625 | #endif /* CONFIG_SMP */ |
1da177e4 | 2626 | |
b5179ac7 | 2627 | ttwu_queue(p, cpu, wake_flags); |
aacedf26 | 2628 | unlock: |
013fdb80 | 2629 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
aacedf26 PZ |
2630 | out: |
2631 | if (success) | |
2632 | ttwu_stat(p, cpu, wake_flags); | |
e3d85487 | 2633 | preempt_enable(); |
1da177e4 LT |
2634 | |
2635 | return success; | |
2636 | } | |
2637 | ||
50fa610a DH |
2638 | /** |
2639 | * wake_up_process - Wake up a specific process | |
2640 | * @p: The process to be woken up. | |
2641 | * | |
2642 | * Attempt to wake up the nominated process and move it to the set of runnable | |
e69f6186 YB |
2643 | * processes. |
2644 | * | |
2645 | * Return: 1 if the process was woken up, 0 if it was already running. | |
50fa610a | 2646 | * |
7696f991 | 2647 | * This function executes a full memory barrier before accessing the task state. |
50fa610a | 2648 | */ |
7ad5b3a5 | 2649 | int wake_up_process(struct task_struct *p) |
1da177e4 | 2650 | { |
9067ac85 | 2651 | return try_to_wake_up(p, TASK_NORMAL, 0); |
1da177e4 | 2652 | } |
1da177e4 LT |
2653 | EXPORT_SYMBOL(wake_up_process); |
2654 | ||
7ad5b3a5 | 2655 | int wake_up_state(struct task_struct *p, unsigned int state) |
1da177e4 LT |
2656 | { |
2657 | return try_to_wake_up(p, state, 0); | |
2658 | } | |
2659 | ||
1da177e4 LT |
2660 | /* |
2661 | * Perform scheduler related setup for a newly forked process p. | |
2662 | * p is forked by current. | |
dd41f596 IM |
2663 | * |
2664 | * __sched_fork() is basic setup used by init_idle() too: | |
2665 | */ | |
5e1576ed | 2666 | static void __sched_fork(unsigned long clone_flags, struct task_struct *p) |
dd41f596 | 2667 | { |
fd2f4419 PZ |
2668 | p->on_rq = 0; |
2669 | ||
2670 | p->se.on_rq = 0; | |
dd41f596 IM |
2671 | p->se.exec_start = 0; |
2672 | p->se.sum_exec_runtime = 0; | |
f6cf891c | 2673 | p->se.prev_sum_exec_runtime = 0; |
6c594c21 | 2674 | p->se.nr_migrations = 0; |
da7a735e | 2675 | p->se.vruntime = 0; |
fd2f4419 | 2676 | INIT_LIST_HEAD(&p->se.group_node); |
6cfb0d5d | 2677 | |
ad936d86 BP |
2678 | #ifdef CONFIG_FAIR_GROUP_SCHED |
2679 | p->se.cfs_rq = NULL; | |
2680 | #endif | |
2681 | ||
6cfb0d5d | 2682 | #ifdef CONFIG_SCHEDSTATS |
cb251765 | 2683 | /* Even if schedstat is disabled, there should not be garbage */ |
41acab88 | 2684 | memset(&p->se.statistics, 0, sizeof(p->se.statistics)); |
6cfb0d5d | 2685 | #endif |
476d139c | 2686 | |
aab03e05 | 2687 | RB_CLEAR_NODE(&p->dl.rb_node); |
40767b0d | 2688 | init_dl_task_timer(&p->dl); |
209a0cbd | 2689 | init_dl_inactive_task_timer(&p->dl); |
a5e7be3b | 2690 | __dl_clear_params(p); |
aab03e05 | 2691 | |
fa717060 | 2692 | INIT_LIST_HEAD(&p->rt.run_list); |
ff77e468 PZ |
2693 | p->rt.timeout = 0; |
2694 | p->rt.time_slice = sched_rr_timeslice; | |
2695 | p->rt.on_rq = 0; | |
2696 | p->rt.on_list = 0; | |
476d139c | 2697 | |
e107be36 AK |
2698 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
2699 | INIT_HLIST_HEAD(&p->preempt_notifiers); | |
2700 | #endif | |
cbee9f88 | 2701 | |
5e1f0f09 MG |
2702 | #ifdef CONFIG_COMPACTION |
2703 | p->capture_control = NULL; | |
2704 | #endif | |
13784475 | 2705 | init_numa_balancing(clone_flags, p); |
dd41f596 IM |
2706 | } |
2707 | ||
2a595721 SD |
2708 | DEFINE_STATIC_KEY_FALSE(sched_numa_balancing); |
2709 | ||
1a687c2e | 2710 | #ifdef CONFIG_NUMA_BALANCING |
c3b9bc5b | 2711 | |
1a687c2e MG |
2712 | void set_numabalancing_state(bool enabled) |
2713 | { | |
2714 | if (enabled) | |
2a595721 | 2715 | static_branch_enable(&sched_numa_balancing); |
1a687c2e | 2716 | else |
2a595721 | 2717 | static_branch_disable(&sched_numa_balancing); |
1a687c2e | 2718 | } |
54a43d54 AK |
2719 | |
2720 | #ifdef CONFIG_PROC_SYSCTL | |
2721 | int sysctl_numa_balancing(struct ctl_table *table, int write, | |
2722 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
2723 | { | |
2724 | struct ctl_table t; | |
2725 | int err; | |
2a595721 | 2726 | int state = static_branch_likely(&sched_numa_balancing); |
54a43d54 AK |
2727 | |
2728 | if (write && !capable(CAP_SYS_ADMIN)) | |
2729 | return -EPERM; | |
2730 | ||
2731 | t = *table; | |
2732 | t.data = &state; | |
2733 | err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos); | |
2734 | if (err < 0) | |
2735 | return err; | |
2736 | if (write) | |
2737 | set_numabalancing_state(state); | |
2738 | return err; | |
2739 | } | |
2740 | #endif | |
2741 | #endif | |
dd41f596 | 2742 | |
4698f88c JP |
2743 | #ifdef CONFIG_SCHEDSTATS |
2744 | ||
cb251765 | 2745 | DEFINE_STATIC_KEY_FALSE(sched_schedstats); |
4698f88c | 2746 | static bool __initdata __sched_schedstats = false; |
cb251765 | 2747 | |
cb251765 MG |
2748 | static void set_schedstats(bool enabled) |
2749 | { | |
2750 | if (enabled) | |
2751 | static_branch_enable(&sched_schedstats); | |
2752 | else | |
2753 | static_branch_disable(&sched_schedstats); | |
2754 | } | |
2755 | ||
2756 | void force_schedstat_enabled(void) | |
2757 | { | |
2758 | if (!schedstat_enabled()) { | |
2759 | pr_info("kernel profiling enabled schedstats, disable via kernel.sched_schedstats.\n"); | |
2760 | static_branch_enable(&sched_schedstats); | |
2761 | } | |
2762 | } | |
2763 | ||
2764 | static int __init setup_schedstats(char *str) | |
2765 | { | |
2766 | int ret = 0; | |
2767 | if (!str) | |
2768 | goto out; | |
2769 | ||
4698f88c JP |
2770 | /* |
2771 | * This code is called before jump labels have been set up, so we can't | |
2772 | * change the static branch directly just yet. Instead set a temporary | |
2773 | * variable so init_schedstats() can do it later. | |
2774 | */ | |
cb251765 | 2775 | if (!strcmp(str, "enable")) { |
4698f88c | 2776 | __sched_schedstats = true; |
cb251765 MG |
2777 | ret = 1; |
2778 | } else if (!strcmp(str, "disable")) { | |
4698f88c | 2779 | __sched_schedstats = false; |
cb251765 MG |
2780 | ret = 1; |
2781 | } | |
2782 | out: | |
2783 | if (!ret) | |
2784 | pr_warn("Unable to parse schedstats=\n"); | |
2785 | ||
2786 | return ret; | |
2787 | } | |
2788 | __setup("schedstats=", setup_schedstats); | |
2789 | ||
4698f88c JP |
2790 | static void __init init_schedstats(void) |
2791 | { | |
2792 | set_schedstats(__sched_schedstats); | |
2793 | } | |
2794 | ||
cb251765 MG |
2795 | #ifdef CONFIG_PROC_SYSCTL |
2796 | int sysctl_schedstats(struct ctl_table *table, int write, | |
2797 | void __user *buffer, size_t *lenp, loff_t *ppos) | |
2798 | { | |
2799 | struct ctl_table t; | |
2800 | int err; | |
2801 | int state = static_branch_likely(&sched_schedstats); | |
2802 | ||
2803 | if (write && !capable(CAP_SYS_ADMIN)) | |
2804 | return -EPERM; | |
2805 | ||
2806 | t = *table; | |
2807 | t.data = &state; | |
2808 | err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos); | |
2809 | if (err < 0) | |
2810 | return err; | |
2811 | if (write) | |
2812 | set_schedstats(state); | |
2813 | return err; | |
2814 | } | |
4698f88c JP |
2815 | #endif /* CONFIG_PROC_SYSCTL */ |
2816 | #else /* !CONFIG_SCHEDSTATS */ | |
2817 | static inline void init_schedstats(void) {} | |
2818 | #endif /* CONFIG_SCHEDSTATS */ | |
dd41f596 IM |
2819 | |
2820 | /* | |
2821 | * fork()/clone()-time setup: | |
2822 | */ | |
aab03e05 | 2823 | int sched_fork(unsigned long clone_flags, struct task_struct *p) |
dd41f596 | 2824 | { |
0122ec5b | 2825 | unsigned long flags; |
dd41f596 | 2826 | |
5e1576ed | 2827 | __sched_fork(clone_flags, p); |
06b83b5f | 2828 | /* |
7dc603c9 | 2829 | * We mark the process as NEW here. This guarantees that |
06b83b5f PZ |
2830 | * nobody will actually run it, and a signal or other external |
2831 | * event cannot wake it up and insert it on the runqueue either. | |
2832 | */ | |
7dc603c9 | 2833 | p->state = TASK_NEW; |
dd41f596 | 2834 | |
c350a04e MG |
2835 | /* |
2836 | * Make sure we do not leak PI boosting priority to the child. | |
2837 | */ | |
2838 | p->prio = current->normal_prio; | |
2839 | ||
e8f14172 PB |
2840 | uclamp_fork(p); |
2841 | ||
b9dc29e7 MG |
2842 | /* |
2843 | * Revert to default priority/policy on fork if requested. | |
2844 | */ | |
2845 | if (unlikely(p->sched_reset_on_fork)) { | |
aab03e05 | 2846 | if (task_has_dl_policy(p) || task_has_rt_policy(p)) { |
b9dc29e7 | 2847 | p->policy = SCHED_NORMAL; |
6c697bdf | 2848 | p->static_prio = NICE_TO_PRIO(0); |
c350a04e MG |
2849 | p->rt_priority = 0; |
2850 | } else if (PRIO_TO_NICE(p->static_prio) < 0) | |
2851 | p->static_prio = NICE_TO_PRIO(0); | |
2852 | ||
2853 | p->prio = p->normal_prio = __normal_prio(p); | |
9059393e | 2854 | set_load_weight(p, false); |
6c697bdf | 2855 | |
b9dc29e7 MG |
2856 | /* |
2857 | * We don't need the reset flag anymore after the fork. It has | |
2858 | * fulfilled its duty: | |
2859 | */ | |
2860 | p->sched_reset_on_fork = 0; | |
2861 | } | |
ca94c442 | 2862 | |
af0fffd9 | 2863 | if (dl_prio(p->prio)) |
aab03e05 | 2864 | return -EAGAIN; |
af0fffd9 | 2865 | else if (rt_prio(p->prio)) |
aab03e05 | 2866 | p->sched_class = &rt_sched_class; |
af0fffd9 | 2867 | else |
2ddbf952 | 2868 | p->sched_class = &fair_sched_class; |
b29739f9 | 2869 | |
7dc603c9 | 2870 | init_entity_runnable_average(&p->se); |
cd29fe6f | 2871 | |
86951599 PZ |
2872 | /* |
2873 | * The child is not yet in the pid-hash so no cgroup attach races, | |
2874 | * and the cgroup is pinned to this child due to cgroup_fork() | |
2875 | * is ran before sched_fork(). | |
2876 | * | |
2877 | * Silence PROVE_RCU. | |
2878 | */ | |
0122ec5b | 2879 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
e210bffd | 2880 | /* |
d1ccc66d | 2881 | * We're setting the CPU for the first time, we don't migrate, |
e210bffd PZ |
2882 | * so use __set_task_cpu(). |
2883 | */ | |
af0fffd9 | 2884 | __set_task_cpu(p, smp_processor_id()); |
e210bffd PZ |
2885 | if (p->sched_class->task_fork) |
2886 | p->sched_class->task_fork(p); | |
0122ec5b | 2887 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
5f3edc1b | 2888 | |
f6db8347 | 2889 | #ifdef CONFIG_SCHED_INFO |
dd41f596 | 2890 | if (likely(sched_info_on())) |
52f17b6c | 2891 | memset(&p->sched_info, 0, sizeof(p->sched_info)); |
1da177e4 | 2892 | #endif |
3ca7a440 PZ |
2893 | #if defined(CONFIG_SMP) |
2894 | p->on_cpu = 0; | |
4866cde0 | 2895 | #endif |
01028747 | 2896 | init_task_preempt_count(p); |
806c09a7 | 2897 | #ifdef CONFIG_SMP |
917b627d | 2898 | plist_node_init(&p->pushable_tasks, MAX_PRIO); |
1baca4ce | 2899 | RB_CLEAR_NODE(&p->pushable_dl_tasks); |
806c09a7 | 2900 | #endif |
aab03e05 | 2901 | return 0; |
1da177e4 LT |
2902 | } |
2903 | ||
332ac17e DF |
2904 | unsigned long to_ratio(u64 period, u64 runtime) |
2905 | { | |
2906 | if (runtime == RUNTIME_INF) | |
c52f14d3 | 2907 | return BW_UNIT; |
332ac17e DF |
2908 | |
2909 | /* | |
2910 | * Doing this here saves a lot of checks in all | |
2911 | * the calling paths, and returning zero seems | |
2912 | * safe for them anyway. | |
2913 | */ | |
2914 | if (period == 0) | |
2915 | return 0; | |
2916 | ||
c52f14d3 | 2917 | return div64_u64(runtime << BW_SHIFT, period); |
332ac17e DF |
2918 | } |
2919 | ||
1da177e4 LT |
2920 | /* |
2921 | * wake_up_new_task - wake up a newly created task for the first time. | |
2922 | * | |
2923 | * This function will do some initial scheduler statistics housekeeping | |
2924 | * that must be done for every newly created context, then puts the task | |
2925 | * on the runqueue and wakes it. | |
2926 | */ | |
3e51e3ed | 2927 | void wake_up_new_task(struct task_struct *p) |
1da177e4 | 2928 | { |
eb580751 | 2929 | struct rq_flags rf; |
dd41f596 | 2930 | struct rq *rq; |
fabf318e | 2931 | |
eb580751 | 2932 | raw_spin_lock_irqsave(&p->pi_lock, rf.flags); |
7dc603c9 | 2933 | p->state = TASK_RUNNING; |
fabf318e PZ |
2934 | #ifdef CONFIG_SMP |
2935 | /* | |
2936 | * Fork balancing, do it here and not earlier because: | |
3bd37062 | 2937 | * - cpus_ptr can change in the fork path |
d1ccc66d | 2938 | * - any previously selected CPU might disappear through hotplug |
e210bffd PZ |
2939 | * |
2940 | * Use __set_task_cpu() to avoid calling sched_class::migrate_task_rq, | |
2941 | * as we're not fully set-up yet. | |
fabf318e | 2942 | */ |
32e839dd | 2943 | p->recent_used_cpu = task_cpu(p); |
e210bffd | 2944 | __set_task_cpu(p, select_task_rq(p, task_cpu(p), SD_BALANCE_FORK, 0)); |
0017d735 | 2945 | #endif |
b7fa30c9 | 2946 | rq = __task_rq_lock(p, &rf); |
4126bad6 | 2947 | update_rq_clock(rq); |
d0fe0b9c | 2948 | post_init_entity_util_avg(p); |
0017d735 | 2949 | |
7a57f32a | 2950 | activate_task(rq, p, ENQUEUE_NOCLOCK); |
fbd705a0 | 2951 | trace_sched_wakeup_new(p); |
a7558e01 | 2952 | check_preempt_curr(rq, p, WF_FORK); |
9a897c5a | 2953 | #ifdef CONFIG_SMP |
0aaafaab PZ |
2954 | if (p->sched_class->task_woken) { |
2955 | /* | |
2956 | * Nothing relies on rq->lock after this, so its fine to | |
2957 | * drop it. | |
2958 | */ | |
d8ac8971 | 2959 | rq_unpin_lock(rq, &rf); |
efbbd05a | 2960 | p->sched_class->task_woken(rq, p); |
d8ac8971 | 2961 | rq_repin_lock(rq, &rf); |
0aaafaab | 2962 | } |
9a897c5a | 2963 | #endif |
eb580751 | 2964 | task_rq_unlock(rq, p, &rf); |
1da177e4 LT |
2965 | } |
2966 | ||
e107be36 AK |
2967 | #ifdef CONFIG_PREEMPT_NOTIFIERS |
2968 | ||
b7203428 | 2969 | static DEFINE_STATIC_KEY_FALSE(preempt_notifier_key); |
1cde2930 | 2970 | |
2ecd9d29 PZ |
2971 | void preempt_notifier_inc(void) |
2972 | { | |
b7203428 | 2973 | static_branch_inc(&preempt_notifier_key); |
2ecd9d29 PZ |
2974 | } |
2975 | EXPORT_SYMBOL_GPL(preempt_notifier_inc); | |
2976 | ||
2977 | void preempt_notifier_dec(void) | |
2978 | { | |
b7203428 | 2979 | static_branch_dec(&preempt_notifier_key); |
2ecd9d29 PZ |
2980 | } |
2981 | EXPORT_SYMBOL_GPL(preempt_notifier_dec); | |
2982 | ||
e107be36 | 2983 | /** |
80dd99b3 | 2984 | * preempt_notifier_register - tell me when current is being preempted & rescheduled |
421cee29 | 2985 | * @notifier: notifier struct to register |
e107be36 AK |
2986 | */ |
2987 | void preempt_notifier_register(struct preempt_notifier *notifier) | |
2988 | { | |
b7203428 | 2989 | if (!static_branch_unlikely(&preempt_notifier_key)) |
2ecd9d29 PZ |
2990 | WARN(1, "registering preempt_notifier while notifiers disabled\n"); |
2991 | ||
e107be36 AK |
2992 | hlist_add_head(¬ifier->link, ¤t->preempt_notifiers); |
2993 | } | |
2994 | EXPORT_SYMBOL_GPL(preempt_notifier_register); | |
2995 | ||
2996 | /** | |
2997 | * preempt_notifier_unregister - no longer interested in preemption notifications | |
421cee29 | 2998 | * @notifier: notifier struct to unregister |
e107be36 | 2999 | * |
d84525a8 | 3000 | * This is *not* safe to call from within a preemption notifier. |
e107be36 AK |
3001 | */ |
3002 | void preempt_notifier_unregister(struct preempt_notifier *notifier) | |
3003 | { | |
3004 | hlist_del(¬ifier->link); | |
3005 | } | |
3006 | EXPORT_SYMBOL_GPL(preempt_notifier_unregister); | |
3007 | ||
1cde2930 | 3008 | static void __fire_sched_in_preempt_notifiers(struct task_struct *curr) |
e107be36 AK |
3009 | { |
3010 | struct preempt_notifier *notifier; | |
e107be36 | 3011 | |
b67bfe0d | 3012 | hlist_for_each_entry(notifier, &curr->preempt_notifiers, link) |
e107be36 AK |
3013 | notifier->ops->sched_in(notifier, raw_smp_processor_id()); |
3014 | } | |
3015 | ||
1cde2930 PZ |
3016 | static __always_inline void fire_sched_in_preempt_notifiers(struct task_struct *curr) |
3017 | { | |
b7203428 | 3018 | if (static_branch_unlikely(&preempt_notifier_key)) |
1cde2930 PZ |
3019 | __fire_sched_in_preempt_notifiers(curr); |
3020 | } | |
3021 | ||
e107be36 | 3022 | static void |
1cde2930 PZ |
3023 | __fire_sched_out_preempt_notifiers(struct task_struct *curr, |
3024 | struct task_struct *next) | |
e107be36 AK |
3025 | { |
3026 | struct preempt_notifier *notifier; | |
e107be36 | 3027 | |
b67bfe0d | 3028 | hlist_for_each_entry(notifier, &curr->preempt_notifiers, link) |
e107be36 AK |
3029 | notifier->ops->sched_out(notifier, next); |
3030 | } | |
3031 | ||
1cde2930 PZ |
3032 | static __always_inline void |
3033 | fire_sched_out_preempt_notifiers(struct task_struct *curr, | |
3034 | struct task_struct *next) | |
3035 | { | |
b7203428 | 3036 | if (static_branch_unlikely(&preempt_notifier_key)) |
1cde2930 PZ |
3037 | __fire_sched_out_preempt_notifiers(curr, next); |
3038 | } | |
3039 | ||
6d6bc0ad | 3040 | #else /* !CONFIG_PREEMPT_NOTIFIERS */ |
e107be36 | 3041 | |
1cde2930 | 3042 | static inline void fire_sched_in_preempt_notifiers(struct task_struct *curr) |
e107be36 AK |
3043 | { |
3044 | } | |
3045 | ||
1cde2930 | 3046 | static inline void |
e107be36 AK |
3047 | fire_sched_out_preempt_notifiers(struct task_struct *curr, |
3048 | struct task_struct *next) | |
3049 | { | |
3050 | } | |
3051 | ||
6d6bc0ad | 3052 | #endif /* CONFIG_PREEMPT_NOTIFIERS */ |
e107be36 | 3053 | |
31cb1bc0 | 3054 | static inline void prepare_task(struct task_struct *next) |
3055 | { | |
3056 | #ifdef CONFIG_SMP | |
3057 | /* | |
3058 | * Claim the task as running, we do this before switching to it | |
3059 | * such that any running task will have this set. | |
3060 | */ | |
3061 | next->on_cpu = 1; | |
3062 | #endif | |
3063 | } | |
3064 | ||
3065 | static inline void finish_task(struct task_struct *prev) | |
3066 | { | |
3067 | #ifdef CONFIG_SMP | |
3068 | /* | |
3069 | * After ->on_cpu is cleared, the task can be moved to a different CPU. | |
3070 | * We must ensure this doesn't happen until the switch is completely | |
3071 | * finished. | |
3072 | * | |
3073 | * In particular, the load of prev->state in finish_task_switch() must | |
3074 | * happen before this. | |
3075 | * | |
3076 | * Pairs with the smp_cond_load_acquire() in try_to_wake_up(). | |
3077 | */ | |
3078 | smp_store_release(&prev->on_cpu, 0); | |
3079 | #endif | |
3080 | } | |
3081 | ||
269d5992 PZ |
3082 | static inline void |
3083 | prepare_lock_switch(struct rq *rq, struct task_struct *next, struct rq_flags *rf) | |
31cb1bc0 | 3084 | { |
269d5992 PZ |
3085 | /* |
3086 | * Since the runqueue lock will be released by the next | |
3087 | * task (which is an invalid locking op but in the case | |
3088 | * of the scheduler it's an obvious special-case), so we | |
3089 | * do an early lockdep release here: | |
3090 | */ | |
3091 | rq_unpin_lock(rq, rf); | |
5facae4f | 3092 | spin_release(&rq->lock.dep_map, _THIS_IP_); |
31cb1bc0 | 3093 | #ifdef CONFIG_DEBUG_SPINLOCK |
3094 | /* this is a valid case when another task releases the spinlock */ | |
269d5992 | 3095 | rq->lock.owner = next; |
31cb1bc0 | 3096 | #endif |
269d5992 PZ |
3097 | } |
3098 | ||
3099 | static inline void finish_lock_switch(struct rq *rq) | |
3100 | { | |
31cb1bc0 | 3101 | /* |
3102 | * If we are tracking spinlock dependencies then we have to | |
3103 | * fix up the runqueue lock - which gets 'carried over' from | |
3104 | * prev into current: | |
3105 | */ | |
3106 | spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_); | |
31cb1bc0 | 3107 | raw_spin_unlock_irq(&rq->lock); |
3108 | } | |
3109 | ||
325ea10c IM |
3110 | /* |
3111 | * NOP if the arch has not defined these: | |
3112 | */ | |
3113 | ||
3114 | #ifndef prepare_arch_switch | |
3115 | # define prepare_arch_switch(next) do { } while (0) | |
3116 | #endif | |
3117 | ||
3118 | #ifndef finish_arch_post_lock_switch | |
3119 | # define finish_arch_post_lock_switch() do { } while (0) | |
3120 | #endif | |
3121 | ||
4866cde0 NP |
3122 | /** |
3123 | * prepare_task_switch - prepare to switch tasks | |
3124 | * @rq: the runqueue preparing to switch | |
421cee29 | 3125 | * @prev: the current task that is being switched out |
4866cde0 NP |
3126 | * @next: the task we are going to switch to. |
3127 | * | |
3128 | * This is called with the rq lock held and interrupts off. It must | |
3129 | * be paired with a subsequent finish_task_switch after the context | |
3130 | * switch. | |
3131 | * | |
3132 | * prepare_task_switch sets up locking and calls architecture specific | |
3133 | * hooks. | |
3134 | */ | |
e107be36 AK |
3135 | static inline void |
3136 | prepare_task_switch(struct rq *rq, struct task_struct *prev, | |
3137 | struct task_struct *next) | |
4866cde0 | 3138 | { |
0ed557aa | 3139 | kcov_prepare_switch(prev); |
43148951 | 3140 | sched_info_switch(rq, prev, next); |
fe4b04fa | 3141 | perf_event_task_sched_out(prev, next); |
d7822b1e | 3142 | rseq_preempt(prev); |
e107be36 | 3143 | fire_sched_out_preempt_notifiers(prev, next); |
31cb1bc0 | 3144 | prepare_task(next); |
4866cde0 NP |
3145 | prepare_arch_switch(next); |
3146 | } | |
3147 | ||
1da177e4 LT |
3148 | /** |
3149 | * finish_task_switch - clean up after a task-switch | |
3150 | * @prev: the thread we just switched away from. | |
3151 | * | |
4866cde0 NP |
3152 | * finish_task_switch must be called after the context switch, paired |
3153 | * with a prepare_task_switch call before the context switch. | |
3154 | * finish_task_switch will reconcile locking set up by prepare_task_switch, | |
3155 | * and do any other architecture-specific cleanup actions. | |
1da177e4 LT |
3156 | * |
3157 | * Note that we may have delayed dropping an mm in context_switch(). If | |
41a2d6cf | 3158 | * so, we finish that here outside of the runqueue lock. (Doing it |
1da177e4 LT |
3159 | * with the lock held can cause deadlocks; see schedule() for |
3160 | * details.) | |
dfa50b60 ON |
3161 | * |
3162 | * The context switch have flipped the stack from under us and restored the | |
3163 | * local variables which were saved when this task called schedule() in the | |
3164 | * past. prev == current is still correct but we need to recalculate this_rq | |
3165 | * because prev may have moved to another CPU. | |
1da177e4 | 3166 | */ |
dfa50b60 | 3167 | static struct rq *finish_task_switch(struct task_struct *prev) |
1da177e4 LT |
3168 | __releases(rq->lock) |
3169 | { | |
dfa50b60 | 3170 | struct rq *rq = this_rq(); |
1da177e4 | 3171 | struct mm_struct *mm = rq->prev_mm; |
55a101f8 | 3172 | long prev_state; |
1da177e4 | 3173 | |
609ca066 PZ |
3174 | /* |
3175 | * The previous task will have left us with a preempt_count of 2 | |
3176 | * because it left us after: | |
3177 | * | |
3178 | * schedule() | |
3179 | * preempt_disable(); // 1 | |
3180 | * __schedule() | |
3181 | * raw_spin_lock_irq(&rq->lock) // 2 | |
3182 | * | |
3183 | * Also, see FORK_PREEMPT_COUNT. | |
3184 | */ | |
e2bf1c4b PZ |
3185 | if (WARN_ONCE(preempt_count() != 2*PREEMPT_DISABLE_OFFSET, |
3186 | "corrupted preempt_count: %s/%d/0x%x\n", | |
3187 | current->comm, current->pid, preempt_count())) | |
3188 | preempt_count_set(FORK_PREEMPT_COUNT); | |
609ca066 | 3189 | |
1da177e4 LT |
3190 | rq->prev_mm = NULL; |
3191 | ||
3192 | /* | |
3193 | * A task struct has one reference for the use as "current". | |
c394cc9f | 3194 | * If a task dies, then it sets TASK_DEAD in tsk->state and calls |
55a101f8 ON |
3195 | * schedule one last time. The schedule call will never return, and |
3196 | * the scheduled task must drop that reference. | |
95913d97 PZ |
3197 | * |
3198 | * We must observe prev->state before clearing prev->on_cpu (in | |
31cb1bc0 | 3199 | * finish_task), otherwise a concurrent wakeup can get prev |
95913d97 PZ |
3200 | * running on another CPU and we could rave with its RUNNING -> DEAD |
3201 | * transition, resulting in a double drop. | |
1da177e4 | 3202 | */ |
55a101f8 | 3203 | prev_state = prev->state; |
bf9fae9f | 3204 | vtime_task_switch(prev); |
a8d757ef | 3205 | perf_event_task_sched_in(prev, current); |
31cb1bc0 | 3206 | finish_task(prev); |
3207 | finish_lock_switch(rq); | |
01f23e16 | 3208 | finish_arch_post_lock_switch(); |
0ed557aa | 3209 | kcov_finish_switch(current); |
e8fa1362 | 3210 | |
e107be36 | 3211 | fire_sched_in_preempt_notifiers(current); |
306e0604 | 3212 | /* |
70216e18 MD |
3213 | * When switching through a kernel thread, the loop in |
3214 | * membarrier_{private,global}_expedited() may have observed that | |
3215 | * kernel thread and not issued an IPI. It is therefore possible to | |
3216 | * schedule between user->kernel->user threads without passing though | |
3217 | * switch_mm(). Membarrier requires a barrier after storing to | |
3218 | * rq->curr, before returning to userspace, so provide them here: | |
3219 | * | |
3220 | * - a full memory barrier for {PRIVATE,GLOBAL}_EXPEDITED, implicitly | |
3221 | * provided by mmdrop(), | |
3222 | * - a sync_core for SYNC_CORE. | |
306e0604 | 3223 | */ |
70216e18 MD |
3224 | if (mm) { |
3225 | membarrier_mm_sync_core_before_usermode(mm); | |
1da177e4 | 3226 | mmdrop(mm); |
70216e18 | 3227 | } |
1cef1150 PZ |
3228 | if (unlikely(prev_state == TASK_DEAD)) { |
3229 | if (prev->sched_class->task_dead) | |
3230 | prev->sched_class->task_dead(prev); | |
68f24b08 | 3231 | |
1cef1150 PZ |
3232 | /* |
3233 | * Remove function-return probe instances associated with this | |
3234 | * task and put them back on the free list. | |
3235 | */ | |
3236 | kprobe_flush_task(prev); | |
3237 | ||
3238 | /* Task is done with its stack. */ | |
3239 | put_task_stack(prev); | |
3240 | ||
0ff7b2cf | 3241 | put_task_struct_rcu_user(prev); |
c6fd91f0 | 3242 | } |
99e5ada9 | 3243 | |
de734f89 | 3244 | tick_nohz_task_switch(); |
dfa50b60 | 3245 | return rq; |
1da177e4 LT |
3246 | } |
3247 | ||
3f029d3c GH |
3248 | #ifdef CONFIG_SMP |
3249 | ||
3f029d3c | 3250 | /* rq->lock is NOT held, but preemption is disabled */ |
e3fca9e7 | 3251 | static void __balance_callback(struct rq *rq) |
3f029d3c | 3252 | { |
e3fca9e7 PZ |
3253 | struct callback_head *head, *next; |
3254 | void (*func)(struct rq *rq); | |
3255 | unsigned long flags; | |
3f029d3c | 3256 | |
e3fca9e7 PZ |
3257 | raw_spin_lock_irqsave(&rq->lock, flags); |
3258 | head = rq->balance_callback; | |
3259 | rq->balance_callback = NULL; | |
3260 | while (head) { | |
3261 | func = (void (*)(struct rq *))head->func; | |
3262 | next = head->next; | |
3263 | head->next = NULL; | |
3264 | head = next; | |
3f029d3c | 3265 | |
e3fca9e7 | 3266 | func(rq); |
3f029d3c | 3267 | } |
e3fca9e7 PZ |
3268 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
3269 | } | |
3270 | ||
3271 | static inline void balance_callback(struct rq *rq) | |
3272 | { | |
3273 | if (unlikely(rq->balance_callback)) | |
3274 | __balance_callback(rq); | |
3f029d3c GH |
3275 | } |
3276 | ||
3277 | #else | |
da19ab51 | 3278 | |
e3fca9e7 | 3279 | static inline void balance_callback(struct rq *rq) |
3f029d3c | 3280 | { |
1da177e4 LT |
3281 | } |
3282 | ||
3f029d3c GH |
3283 | #endif |
3284 | ||
1da177e4 LT |
3285 | /** |
3286 | * schedule_tail - first thing a freshly forked thread must call. | |
3287 | * @prev: the thread we just switched away from. | |
3288 | */ | |
722a9f92 | 3289 | asmlinkage __visible void schedule_tail(struct task_struct *prev) |
1da177e4 LT |
3290 | __releases(rq->lock) |
3291 | { | |
1a43a14a | 3292 | struct rq *rq; |
da19ab51 | 3293 | |
609ca066 PZ |
3294 | /* |
3295 | * New tasks start with FORK_PREEMPT_COUNT, see there and | |
3296 | * finish_task_switch() for details. | |
3297 | * | |
3298 | * finish_task_switch() will drop rq->lock() and lower preempt_count | |
3299 | * and the preempt_enable() will end up enabling preemption (on | |
3300 | * PREEMPT_COUNT kernels). | |
3301 | */ | |
3302 | ||
dfa50b60 | 3303 | rq = finish_task_switch(prev); |
e3fca9e7 | 3304 | balance_callback(rq); |
1a43a14a | 3305 | preempt_enable(); |
70b97a7f | 3306 | |
1da177e4 | 3307 | if (current->set_child_tid) |
b488893a | 3308 | put_user(task_pid_vnr(current), current->set_child_tid); |
088fe47c EB |
3309 | |
3310 | calculate_sigpending(); | |
1da177e4 LT |
3311 | } |
3312 | ||
3313 | /* | |
dfa50b60 | 3314 | * context_switch - switch to the new MM and the new thread's register state. |
1da177e4 | 3315 | */ |
04936948 | 3316 | static __always_inline struct rq * |
70b97a7f | 3317 | context_switch(struct rq *rq, struct task_struct *prev, |
d8ac8971 | 3318 | struct task_struct *next, struct rq_flags *rf) |
1da177e4 | 3319 | { |
e107be36 | 3320 | prepare_task_switch(rq, prev, next); |
fe4b04fa | 3321 | |
9226d125 ZA |
3322 | /* |
3323 | * For paravirt, this is coupled with an exit in switch_to to | |
3324 | * combine the page table reload and the switch backend into | |
3325 | * one hypercall. | |
3326 | */ | |
224101ed | 3327 | arch_start_context_switch(prev); |
9226d125 | 3328 | |
306e0604 | 3329 | /* |
139d025c PZ |
3330 | * kernel -> kernel lazy + transfer active |
3331 | * user -> kernel lazy + mmgrab() active | |
3332 | * | |
3333 | * kernel -> user switch + mmdrop() active | |
3334 | * user -> user switch | |
306e0604 | 3335 | */ |
139d025c PZ |
3336 | if (!next->mm) { // to kernel |
3337 | enter_lazy_tlb(prev->active_mm, next); | |
3338 | ||
3339 | next->active_mm = prev->active_mm; | |
3340 | if (prev->mm) // from user | |
3341 | mmgrab(prev->active_mm); | |
3342 | else | |
3343 | prev->active_mm = NULL; | |
3344 | } else { // to user | |
227a4aad | 3345 | membarrier_switch_mm(rq, prev->active_mm, next->mm); |
139d025c PZ |
3346 | /* |
3347 | * sys_membarrier() requires an smp_mb() between setting | |
227a4aad | 3348 | * rq->curr / membarrier_switch_mm() and returning to userspace. |
139d025c PZ |
3349 | * |
3350 | * The below provides this either through switch_mm(), or in | |
3351 | * case 'prev->active_mm == next->mm' through | |
3352 | * finish_task_switch()'s mmdrop(). | |
3353 | */ | |
139d025c | 3354 | switch_mm_irqs_off(prev->active_mm, next->mm, next); |
1da177e4 | 3355 | |
139d025c PZ |
3356 | if (!prev->mm) { // from kernel |
3357 | /* will mmdrop() in finish_task_switch(). */ | |
3358 | rq->prev_mm = prev->active_mm; | |
3359 | prev->active_mm = NULL; | |
3360 | } | |
1da177e4 | 3361 | } |
92509b73 | 3362 | |
cb42c9a3 | 3363 | rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP); |
92509b73 | 3364 | |
269d5992 | 3365 | prepare_lock_switch(rq, next, rf); |
1da177e4 LT |
3366 | |
3367 | /* Here we just switch the register state and the stack. */ | |
3368 | switch_to(prev, next, prev); | |
dd41f596 | 3369 | barrier(); |
dfa50b60 ON |
3370 | |
3371 | return finish_task_switch(prev); | |
1da177e4 LT |
3372 | } |
3373 | ||
3374 | /* | |
1c3e8264 | 3375 | * nr_running and nr_context_switches: |
1da177e4 LT |
3376 | * |
3377 | * externally visible scheduler statistics: current number of runnable | |
1c3e8264 | 3378 | * threads, total number of context switches performed since bootup. |
1da177e4 LT |
3379 | */ |
3380 | unsigned long nr_running(void) | |
3381 | { | |
3382 | unsigned long i, sum = 0; | |
3383 | ||
3384 | for_each_online_cpu(i) | |
3385 | sum += cpu_rq(i)->nr_running; | |
3386 | ||
3387 | return sum; | |
f711f609 | 3388 | } |
1da177e4 | 3389 | |
2ee507c4 | 3390 | /* |
d1ccc66d | 3391 | * Check if only the current task is running on the CPU. |
00cc1633 DD |
3392 | * |
3393 | * Caution: this function does not check that the caller has disabled | |
3394 | * preemption, thus the result might have a time-of-check-to-time-of-use | |
3395 | * race. The caller is responsible to use it correctly, for example: | |
3396 | * | |
dfcb245e | 3397 | * - from a non-preemptible section (of course) |
00cc1633 DD |
3398 | * |
3399 | * - from a thread that is bound to a single CPU | |
3400 | * | |
3401 | * - in a loop with very short iterations (e.g. a polling loop) | |
2ee507c4 TC |
3402 | */ |
3403 | bool single_task_running(void) | |
3404 | { | |
00cc1633 | 3405 | return raw_rq()->nr_running == 1; |
2ee507c4 TC |
3406 | } |
3407 | EXPORT_SYMBOL(single_task_running); | |
3408 | ||
1da177e4 | 3409 | unsigned long long nr_context_switches(void) |
46cb4b7c | 3410 | { |
cc94abfc SR |
3411 | int i; |
3412 | unsigned long long sum = 0; | |
46cb4b7c | 3413 | |
0a945022 | 3414 | for_each_possible_cpu(i) |
1da177e4 | 3415 | sum += cpu_rq(i)->nr_switches; |
46cb4b7c | 3416 | |
1da177e4 LT |
3417 | return sum; |
3418 | } | |
483b4ee6 | 3419 | |
145d952a DL |
3420 | /* |
3421 | * Consumers of these two interfaces, like for example the cpuidle menu | |
3422 | * governor, are using nonsensical data. Preferring shallow idle state selection | |
3423 | * for a CPU that has IO-wait which might not even end up running the task when | |
3424 | * it does become runnable. | |
3425 | */ | |
3426 | ||
3427 | unsigned long nr_iowait_cpu(int cpu) | |
3428 | { | |
3429 | return atomic_read(&cpu_rq(cpu)->nr_iowait); | |
3430 | } | |
3431 | ||
e33a9bba TH |
3432 | /* |
3433 | * IO-wait accounting, and how its mostly bollocks (on SMP). | |
3434 | * | |
3435 | * The idea behind IO-wait account is to account the idle time that we could | |
3436 | * have spend running if it were not for IO. That is, if we were to improve the | |
3437 | * storage performance, we'd have a proportional reduction in IO-wait time. | |
3438 | * | |
3439 | * This all works nicely on UP, where, when a task blocks on IO, we account | |
3440 | * idle time as IO-wait, because if the storage were faster, it could've been | |
3441 | * running and we'd not be idle. | |
3442 | * | |
3443 | * This has been extended to SMP, by doing the same for each CPU. This however | |
3444 | * is broken. | |
3445 | * | |
3446 | * Imagine for instance the case where two tasks block on one CPU, only the one | |
3447 | * CPU will have IO-wait accounted, while the other has regular idle. Even | |
3448 | * though, if the storage were faster, both could've ran at the same time, | |
3449 | * utilising both CPUs. | |
3450 | * | |
3451 | * This means, that when looking globally, the current IO-wait accounting on | |
3452 | * SMP is a lower bound, by reason of under accounting. | |
3453 | * | |
3454 | * Worse, since the numbers are provided per CPU, they are sometimes | |
3455 | * interpreted per CPU, and that is nonsensical. A blocked task isn't strictly | |
3456 | * associated with any one particular CPU, it can wake to another CPU than it | |
3457 | * blocked on. This means the per CPU IO-wait number is meaningless. | |
3458 | * | |
3459 | * Task CPU affinities can make all that even more 'interesting'. | |
3460 | */ | |
3461 | ||
1da177e4 LT |
3462 | unsigned long nr_iowait(void) |
3463 | { | |
3464 | unsigned long i, sum = 0; | |
483b4ee6 | 3465 | |
0a945022 | 3466 | for_each_possible_cpu(i) |
145d952a | 3467 | sum += nr_iowait_cpu(i); |
46cb4b7c | 3468 | |
1da177e4 LT |
3469 | return sum; |
3470 | } | |
483b4ee6 | 3471 | |
dd41f596 | 3472 | #ifdef CONFIG_SMP |
8a0be9ef | 3473 | |
46cb4b7c | 3474 | /* |
38022906 PZ |
3475 | * sched_exec - execve() is a valuable balancing opportunity, because at |
3476 | * this point the task has the smallest effective memory and cache footprint. | |
46cb4b7c | 3477 | */ |
38022906 | 3478 | void sched_exec(void) |
46cb4b7c | 3479 | { |
38022906 | 3480 | struct task_struct *p = current; |
1da177e4 | 3481 | unsigned long flags; |
0017d735 | 3482 | int dest_cpu; |
46cb4b7c | 3483 | |
8f42ced9 | 3484 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
ac66f547 | 3485 | dest_cpu = p->sched_class->select_task_rq(p, task_cpu(p), SD_BALANCE_EXEC, 0); |
0017d735 PZ |
3486 | if (dest_cpu == smp_processor_id()) |
3487 | goto unlock; | |
38022906 | 3488 | |
8f42ced9 | 3489 | if (likely(cpu_active(dest_cpu))) { |
969c7921 | 3490 | struct migration_arg arg = { p, dest_cpu }; |
46cb4b7c | 3491 | |
8f42ced9 PZ |
3492 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
3493 | stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg); | |
1da177e4 LT |
3494 | return; |
3495 | } | |
0017d735 | 3496 | unlock: |
8f42ced9 | 3497 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
1da177e4 | 3498 | } |
dd41f596 | 3499 | |
1da177e4 LT |
3500 | #endif |
3501 | ||
1da177e4 | 3502 | DEFINE_PER_CPU(struct kernel_stat, kstat); |
3292beb3 | 3503 | DEFINE_PER_CPU(struct kernel_cpustat, kernel_cpustat); |
1da177e4 LT |
3504 | |
3505 | EXPORT_PER_CPU_SYMBOL(kstat); | |
3292beb3 | 3506 | EXPORT_PER_CPU_SYMBOL(kernel_cpustat); |
1da177e4 | 3507 | |
6075620b GG |
3508 | /* |
3509 | * The function fair_sched_class.update_curr accesses the struct curr | |
3510 | * and its field curr->exec_start; when called from task_sched_runtime(), | |
3511 | * we observe a high rate of cache misses in practice. | |
3512 | * Prefetching this data results in improved performance. | |
3513 | */ | |
3514 | static inline void prefetch_curr_exec_start(struct task_struct *p) | |
3515 | { | |
3516 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
3517 | struct sched_entity *curr = (&p->se)->cfs_rq->curr; | |
3518 | #else | |
3519 | struct sched_entity *curr = (&task_rq(p)->cfs)->curr; | |
3520 | #endif | |
3521 | prefetch(curr); | |
3522 | prefetch(&curr->exec_start); | |
3523 | } | |
3524 | ||
c5f8d995 HS |
3525 | /* |
3526 | * Return accounted runtime for the task. | |
3527 | * In case the task is currently running, return the runtime plus current's | |
3528 | * pending runtime that have not been accounted yet. | |
3529 | */ | |
3530 | unsigned long long task_sched_runtime(struct task_struct *p) | |
3531 | { | |
eb580751 | 3532 | struct rq_flags rf; |
c5f8d995 | 3533 | struct rq *rq; |
6e998916 | 3534 | u64 ns; |
c5f8d995 | 3535 | |
911b2898 PZ |
3536 | #if defined(CONFIG_64BIT) && defined(CONFIG_SMP) |
3537 | /* | |
97fb7a0a | 3538 | * 64-bit doesn't need locks to atomically read a 64-bit value. |
911b2898 PZ |
3539 | * So we have a optimization chance when the task's delta_exec is 0. |
3540 | * Reading ->on_cpu is racy, but this is ok. | |
3541 | * | |
d1ccc66d IM |
3542 | * If we race with it leaving CPU, we'll take a lock. So we're correct. |
3543 | * If we race with it entering CPU, unaccounted time is 0. This is | |
911b2898 | 3544 | * indistinguishable from the read occurring a few cycles earlier. |
4036ac15 MG |
3545 | * If we see ->on_cpu without ->on_rq, the task is leaving, and has |
3546 | * been accounted, so we're correct here as well. | |
911b2898 | 3547 | */ |
da0c1e65 | 3548 | if (!p->on_cpu || !task_on_rq_queued(p)) |
911b2898 PZ |
3549 | return p->se.sum_exec_runtime; |
3550 | #endif | |
3551 | ||
eb580751 | 3552 | rq = task_rq_lock(p, &rf); |
6e998916 SG |
3553 | /* |
3554 | * Must be ->curr _and_ ->on_rq. If dequeued, we would | |
3555 | * project cycles that may never be accounted to this | |
3556 | * thread, breaking clock_gettime(). | |
3557 | */ | |
3558 | if (task_current(rq, p) && task_on_rq_queued(p)) { | |
6075620b | 3559 | prefetch_curr_exec_start(p); |
6e998916 SG |
3560 | update_rq_clock(rq); |
3561 | p->sched_class->update_curr(rq); | |
3562 | } | |
3563 | ns = p->se.sum_exec_runtime; | |
eb580751 | 3564 | task_rq_unlock(rq, p, &rf); |
c5f8d995 HS |
3565 | |
3566 | return ns; | |
3567 | } | |
48f24c4d | 3568 | |
14533a16 IM |
3569 | DEFINE_PER_CPU(unsigned long, thermal_pressure); |
3570 | ||
3571 | void arch_set_thermal_pressure(struct cpumask *cpus, | |
3572 | unsigned long th_pressure) | |
3573 | { | |
3574 | int cpu; | |
3575 | ||
3576 | for_each_cpu(cpu, cpus) | |
3577 | WRITE_ONCE(per_cpu(thermal_pressure, cpu), th_pressure); | |
3578 | } | |
3579 | ||
7835b98b CL |
3580 | /* |
3581 | * This function gets called by the timer code, with HZ frequency. | |
3582 | * We call it with interrupts disabled. | |
7835b98b CL |
3583 | */ |
3584 | void scheduler_tick(void) | |
3585 | { | |
7835b98b CL |
3586 | int cpu = smp_processor_id(); |
3587 | struct rq *rq = cpu_rq(cpu); | |
dd41f596 | 3588 | struct task_struct *curr = rq->curr; |
8a8c69c3 | 3589 | struct rq_flags rf; |
b4eccf5f | 3590 | unsigned long thermal_pressure; |
3e51f33f | 3591 | |
1567c3e3 | 3592 | arch_scale_freq_tick(); |
3e51f33f | 3593 | sched_clock_tick(); |
dd41f596 | 3594 | |
8a8c69c3 PZ |
3595 | rq_lock(rq, &rf); |
3596 | ||
3e51f33f | 3597 | update_rq_clock(rq); |
b4eccf5f | 3598 | thermal_pressure = arch_scale_thermal_pressure(cpu_of(rq)); |
05289b90 | 3599 | update_thermal_load_avg(rq_clock_thermal(rq), rq, thermal_pressure); |
fa85ae24 | 3600 | curr->sched_class->task_tick(rq, curr, 0); |
3289bdb4 | 3601 | calc_global_load_tick(rq); |
eb414681 | 3602 | psi_task_tick(rq); |
8a8c69c3 PZ |
3603 | |
3604 | rq_unlock(rq, &rf); | |
7835b98b | 3605 | |
e9d2b064 | 3606 | perf_event_task_tick(); |
e220d2dc | 3607 | |
e418e1c2 | 3608 | #ifdef CONFIG_SMP |
6eb57e0d | 3609 | rq->idle_balance = idle_cpu(cpu); |
7caff66f | 3610 | trigger_load_balance(rq); |
e418e1c2 | 3611 | #endif |
1da177e4 LT |
3612 | } |
3613 | ||
265f22a9 | 3614 | #ifdef CONFIG_NO_HZ_FULL |
d84b3131 FW |
3615 | |
3616 | struct tick_work { | |
3617 | int cpu; | |
b55bd585 | 3618 | atomic_t state; |
d84b3131 FW |
3619 | struct delayed_work work; |
3620 | }; | |
b55bd585 PM |
3621 | /* Values for ->state, see diagram below. */ |
3622 | #define TICK_SCHED_REMOTE_OFFLINE 0 | |
3623 | #define TICK_SCHED_REMOTE_OFFLINING 1 | |
3624 | #define TICK_SCHED_REMOTE_RUNNING 2 | |
3625 | ||
3626 | /* | |
3627 | * State diagram for ->state: | |
3628 | * | |
3629 | * | |
3630 | * TICK_SCHED_REMOTE_OFFLINE | |
3631 | * | ^ | |
3632 | * | | | |
3633 | * | | sched_tick_remote() | |
3634 | * | | | |
3635 | * | | | |
3636 | * +--TICK_SCHED_REMOTE_OFFLINING | |
3637 | * | ^ | |
3638 | * | | | |
3639 | * sched_tick_start() | | sched_tick_stop() | |
3640 | * | | | |
3641 | * V | | |
3642 | * TICK_SCHED_REMOTE_RUNNING | |
3643 | * | |
3644 | * | |
3645 | * Other transitions get WARN_ON_ONCE(), except that sched_tick_remote() | |
3646 | * and sched_tick_start() are happy to leave the state in RUNNING. | |
3647 | */ | |
d84b3131 FW |
3648 | |
3649 | static struct tick_work __percpu *tick_work_cpu; | |
3650 | ||
3651 | static void sched_tick_remote(struct work_struct *work) | |
3652 | { | |
3653 | struct delayed_work *dwork = to_delayed_work(work); | |
3654 | struct tick_work *twork = container_of(dwork, struct tick_work, work); | |
3655 | int cpu = twork->cpu; | |
3656 | struct rq *rq = cpu_rq(cpu); | |
d9c0ffca | 3657 | struct task_struct *curr; |
d84b3131 | 3658 | struct rq_flags rf; |
d9c0ffca | 3659 | u64 delta; |
b55bd585 | 3660 | int os; |
d84b3131 FW |
3661 | |
3662 | /* | |
3663 | * Handle the tick only if it appears the remote CPU is running in full | |
3664 | * dynticks mode. The check is racy by nature, but missing a tick or | |
3665 | * having one too much is no big deal because the scheduler tick updates | |
3666 | * statistics and checks timeslices in a time-independent way, regardless | |
3667 | * of when exactly it is running. | |
3668 | */ | |
488603b8 | 3669 | if (!tick_nohz_tick_stopped_cpu(cpu)) |
d9c0ffca | 3670 | goto out_requeue; |
d84b3131 | 3671 | |
d9c0ffca FW |
3672 | rq_lock_irq(rq, &rf); |
3673 | curr = rq->curr; | |
488603b8 | 3674 | if (cpu_is_offline(cpu)) |
d9c0ffca | 3675 | goto out_unlock; |
d84b3131 | 3676 | |
d9c0ffca | 3677 | update_rq_clock(rq); |
d9c0ffca | 3678 | |
488603b8 SW |
3679 | if (!is_idle_task(curr)) { |
3680 | /* | |
3681 | * Make sure the next tick runs within a reasonable | |
3682 | * amount of time. | |
3683 | */ | |
3684 | delta = rq_clock_task(rq) - curr->se.exec_start; | |
3685 | WARN_ON_ONCE(delta > (u64)NSEC_PER_SEC * 3); | |
3686 | } | |
d9c0ffca FW |
3687 | curr->sched_class->task_tick(rq, curr, 0); |
3688 | ||
ebc0f83c | 3689 | calc_load_nohz_remote(rq); |
d9c0ffca FW |
3690 | out_unlock: |
3691 | rq_unlock_irq(rq, &rf); | |
d9c0ffca | 3692 | out_requeue: |
ebc0f83c | 3693 | |
d84b3131 FW |
3694 | /* |
3695 | * Run the remote tick once per second (1Hz). This arbitrary | |
3696 | * frequency is large enough to avoid overload but short enough | |
b55bd585 PM |
3697 | * to keep scheduler internal stats reasonably up to date. But |
3698 | * first update state to reflect hotplug activity if required. | |
d84b3131 | 3699 | */ |
b55bd585 PM |
3700 | os = atomic_fetch_add_unless(&twork->state, -1, TICK_SCHED_REMOTE_RUNNING); |
3701 | WARN_ON_ONCE(os == TICK_SCHED_REMOTE_OFFLINE); | |
3702 | if (os == TICK_SCHED_REMOTE_RUNNING) | |
3703 | queue_delayed_work(system_unbound_wq, dwork, HZ); | |
d84b3131 FW |
3704 | } |
3705 | ||
3706 | static void sched_tick_start(int cpu) | |
3707 | { | |
b55bd585 | 3708 | int os; |
d84b3131 FW |
3709 | struct tick_work *twork; |
3710 | ||
3711 | if (housekeeping_cpu(cpu, HK_FLAG_TICK)) | |
3712 | return; | |
3713 | ||
3714 | WARN_ON_ONCE(!tick_work_cpu); | |
3715 | ||
3716 | twork = per_cpu_ptr(tick_work_cpu, cpu); | |
b55bd585 PM |
3717 | os = atomic_xchg(&twork->state, TICK_SCHED_REMOTE_RUNNING); |
3718 | WARN_ON_ONCE(os == TICK_SCHED_REMOTE_RUNNING); | |
3719 | if (os == TICK_SCHED_REMOTE_OFFLINE) { | |
3720 | twork->cpu = cpu; | |
3721 | INIT_DELAYED_WORK(&twork->work, sched_tick_remote); | |
3722 | queue_delayed_work(system_unbound_wq, &twork->work, HZ); | |
3723 | } | |
d84b3131 FW |
3724 | } |
3725 | ||
3726 | #ifdef CONFIG_HOTPLUG_CPU | |
3727 | static void sched_tick_stop(int cpu) | |
3728 | { | |
3729 | struct tick_work *twork; | |
b55bd585 | 3730 | int os; |
d84b3131 FW |
3731 | |
3732 | if (housekeeping_cpu(cpu, HK_FLAG_TICK)) | |
3733 | return; | |
3734 | ||
3735 | WARN_ON_ONCE(!tick_work_cpu); | |
3736 | ||
3737 | twork = per_cpu_ptr(tick_work_cpu, cpu); | |
b55bd585 PM |
3738 | /* There cannot be competing actions, but don't rely on stop-machine. */ |
3739 | os = atomic_xchg(&twork->state, TICK_SCHED_REMOTE_OFFLINING); | |
3740 | WARN_ON_ONCE(os != TICK_SCHED_REMOTE_RUNNING); | |
3741 | /* Don't cancel, as this would mess up the state machine. */ | |
d84b3131 FW |
3742 | } |
3743 | #endif /* CONFIG_HOTPLUG_CPU */ | |
3744 | ||
3745 | int __init sched_tick_offload_init(void) | |
3746 | { | |
3747 | tick_work_cpu = alloc_percpu(struct tick_work); | |
3748 | BUG_ON(!tick_work_cpu); | |
d84b3131 FW |
3749 | return 0; |
3750 | } | |
3751 | ||
3752 | #else /* !CONFIG_NO_HZ_FULL */ | |
3753 | static inline void sched_tick_start(int cpu) { } | |
3754 | static inline void sched_tick_stop(int cpu) { } | |
265f22a9 | 3755 | #endif |
1da177e4 | 3756 | |
c1a280b6 | 3757 | #if defined(CONFIG_PREEMPTION) && (defined(CONFIG_DEBUG_PREEMPT) || \ |
c3bc8fd6 | 3758 | defined(CONFIG_TRACE_PREEMPT_TOGGLE)) |
47252cfb SR |
3759 | /* |
3760 | * If the value passed in is equal to the current preempt count | |
3761 | * then we just disabled preemption. Start timing the latency. | |
3762 | */ | |
3763 | static inline void preempt_latency_start(int val) | |
3764 | { | |
3765 | if (preempt_count() == val) { | |
3766 | unsigned long ip = get_lock_parent_ip(); | |
3767 | #ifdef CONFIG_DEBUG_PREEMPT | |
3768 | current->preempt_disable_ip = ip; | |
3769 | #endif | |
3770 | trace_preempt_off(CALLER_ADDR0, ip); | |
3771 | } | |
3772 | } | |
7e49fcce | 3773 | |
edafe3a5 | 3774 | void preempt_count_add(int val) |
1da177e4 | 3775 | { |
6cd8a4bb | 3776 | #ifdef CONFIG_DEBUG_PREEMPT |
1da177e4 LT |
3777 | /* |
3778 | * Underflow? | |
3779 | */ | |
9a11b49a IM |
3780 | if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0))) |
3781 | return; | |
6cd8a4bb | 3782 | #endif |
bdb43806 | 3783 | __preempt_count_add(val); |
6cd8a4bb | 3784 | #ifdef CONFIG_DEBUG_PREEMPT |
1da177e4 LT |
3785 | /* |
3786 | * Spinlock count overflowing soon? | |
3787 | */ | |
33859f7f MOS |
3788 | DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >= |
3789 | PREEMPT_MASK - 10); | |
6cd8a4bb | 3790 | #endif |
47252cfb | 3791 | preempt_latency_start(val); |
1da177e4 | 3792 | } |
bdb43806 | 3793 | EXPORT_SYMBOL(preempt_count_add); |
edafe3a5 | 3794 | NOKPROBE_SYMBOL(preempt_count_add); |
1da177e4 | 3795 | |
47252cfb SR |
3796 | /* |
3797 | * If the value passed in equals to the current preempt count | |
3798 | * then we just enabled preemption. Stop timing the latency. | |
3799 | */ | |
3800 | static inline void preempt_latency_stop(int val) | |
3801 | { | |
3802 | if (preempt_count() == val) | |
3803 | trace_preempt_on(CALLER_ADDR0, get_lock_parent_ip()); | |
3804 | } | |
3805 | ||
edafe3a5 | 3806 | void preempt_count_sub(int val) |
1da177e4 | 3807 | { |
6cd8a4bb | 3808 | #ifdef CONFIG_DEBUG_PREEMPT |
1da177e4 LT |
3809 | /* |
3810 | * Underflow? | |
3811 | */ | |
01e3eb82 | 3812 | if (DEBUG_LOCKS_WARN_ON(val > preempt_count())) |
9a11b49a | 3813 | return; |
1da177e4 LT |
3814 | /* |
3815 | * Is the spinlock portion underflowing? | |
3816 | */ | |
9a11b49a IM |
3817 | if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) && |
3818 | !(preempt_count() & PREEMPT_MASK))) | |
3819 | return; | |
6cd8a4bb | 3820 | #endif |
9a11b49a | 3821 | |
47252cfb | 3822 | preempt_latency_stop(val); |
bdb43806 | 3823 | __preempt_count_sub(val); |
1da177e4 | 3824 | } |
bdb43806 | 3825 | EXPORT_SYMBOL(preempt_count_sub); |
edafe3a5 | 3826 | NOKPROBE_SYMBOL(preempt_count_sub); |
1da177e4 | 3827 | |
47252cfb SR |
3828 | #else |
3829 | static inline void preempt_latency_start(int val) { } | |
3830 | static inline void preempt_latency_stop(int val) { } | |
1da177e4 LT |
3831 | #endif |
3832 | ||
59ddbcb2 IM |
3833 | static inline unsigned long get_preempt_disable_ip(struct task_struct *p) |
3834 | { | |
3835 | #ifdef CONFIG_DEBUG_PREEMPT | |
3836 | return p->preempt_disable_ip; | |
3837 | #else | |
3838 | return 0; | |
3839 | #endif | |
3840 | } | |
3841 | ||
1da177e4 | 3842 | /* |
dd41f596 | 3843 | * Print scheduling while atomic bug: |
1da177e4 | 3844 | */ |
dd41f596 | 3845 | static noinline void __schedule_bug(struct task_struct *prev) |
1da177e4 | 3846 | { |
d1c6d149 VN |
3847 | /* Save this before calling printk(), since that will clobber it */ |
3848 | unsigned long preempt_disable_ip = get_preempt_disable_ip(current); | |
3849 | ||
664dfa65 DJ |
3850 | if (oops_in_progress) |
3851 | return; | |
3852 | ||
3df0fc5b PZ |
3853 | printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n", |
3854 | prev->comm, prev->pid, preempt_count()); | |
838225b4 | 3855 | |
dd41f596 | 3856 | debug_show_held_locks(prev); |
e21f5b15 | 3857 | print_modules(); |
dd41f596 IM |
3858 | if (irqs_disabled()) |
3859 | print_irqtrace_events(prev); | |
d1c6d149 VN |
3860 | if (IS_ENABLED(CONFIG_DEBUG_PREEMPT) |
3861 | && in_atomic_preempt_off()) { | |
8f47b187 | 3862 | pr_err("Preemption disabled at:"); |
d1c6d149 | 3863 | print_ip_sym(preempt_disable_ip); |
8f47b187 TG |
3864 | pr_cont("\n"); |
3865 | } | |
748c7201 DBO |
3866 | if (panic_on_warn) |
3867 | panic("scheduling while atomic\n"); | |
3868 | ||
6135fc1e | 3869 | dump_stack(); |
373d4d09 | 3870 | add_taint(TAINT_WARN, LOCKDEP_STILL_OK); |
dd41f596 | 3871 | } |
1da177e4 | 3872 | |
dd41f596 IM |
3873 | /* |
3874 | * Various schedule()-time debugging checks and statistics: | |
3875 | */ | |
312364f3 | 3876 | static inline void schedule_debug(struct task_struct *prev, bool preempt) |
dd41f596 | 3877 | { |
0d9e2632 | 3878 | #ifdef CONFIG_SCHED_STACK_END_CHECK |
29d64551 JH |
3879 | if (task_stack_end_corrupted(prev)) |
3880 | panic("corrupted stack end detected inside scheduler\n"); | |
0d9e2632 | 3881 | #endif |
b99def8b | 3882 | |
312364f3 DV |
3883 | #ifdef CONFIG_DEBUG_ATOMIC_SLEEP |
3884 | if (!preempt && prev->state && prev->non_block_count) { | |
3885 | printk(KERN_ERR "BUG: scheduling in a non-blocking section: %s/%d/%i\n", | |
3886 | prev->comm, prev->pid, prev->non_block_count); | |
3887 | dump_stack(); | |
3888 | add_taint(TAINT_WARN, LOCKDEP_STILL_OK); | |
3889 | } | |
3890 | #endif | |
3891 | ||
1dc0fffc | 3892 | if (unlikely(in_atomic_preempt_off())) { |
dd41f596 | 3893 | __schedule_bug(prev); |
1dc0fffc PZ |
3894 | preempt_count_set(PREEMPT_DISABLED); |
3895 | } | |
b3fbab05 | 3896 | rcu_sleep_check(); |
dd41f596 | 3897 | |
1da177e4 LT |
3898 | profile_hit(SCHED_PROFILING, __builtin_return_address(0)); |
3899 | ||
ae92882e | 3900 | schedstat_inc(this_rq()->sched_count); |
dd41f596 IM |
3901 | } |
3902 | ||
3903 | /* | |
3904 | * Pick up the highest-prio task: | |
3905 | */ | |
3906 | static inline struct task_struct * | |
d8ac8971 | 3907 | pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf) |
dd41f596 | 3908 | { |
49ee5768 | 3909 | const struct sched_class *class; |
dd41f596 | 3910 | struct task_struct *p; |
1da177e4 LT |
3911 | |
3912 | /* | |
0ba87bb2 PZ |
3913 | * Optimization: we know that if all tasks are in the fair class we can |
3914 | * call that function directly, but only if the @prev task wasn't of a | |
3915 | * higher scheduling class, because otherwise those loose the | |
3916 | * opportunity to pull in more work from other CPUs. | |
1da177e4 | 3917 | */ |
0ba87bb2 PZ |
3918 | if (likely((prev->sched_class == &idle_sched_class || |
3919 | prev->sched_class == &fair_sched_class) && | |
3920 | rq->nr_running == rq->cfs.h_nr_running)) { | |
3921 | ||
5d7d6056 | 3922 | p = pick_next_task_fair(rq, prev, rf); |
6ccdc84b | 3923 | if (unlikely(p == RETRY_TASK)) |
67692435 | 3924 | goto restart; |
6ccdc84b | 3925 | |
d1ccc66d | 3926 | /* Assumes fair_sched_class->next == idle_sched_class */ |
5d7d6056 | 3927 | if (!p) { |
f488e105 | 3928 | put_prev_task(rq, prev); |
98c2f700 | 3929 | p = pick_next_task_idle(rq); |
f488e105 | 3930 | } |
6ccdc84b PZ |
3931 | |
3932 | return p; | |
1da177e4 LT |
3933 | } |
3934 | ||
67692435 | 3935 | restart: |
6e2df058 | 3936 | #ifdef CONFIG_SMP |
67692435 | 3937 | /* |
6e2df058 PZ |
3938 | * We must do the balancing pass before put_next_task(), such |
3939 | * that when we release the rq->lock the task is in the same | |
3940 | * state as before we took rq->lock. | |
3941 | * | |
3942 | * We can terminate the balance pass as soon as we know there is | |
3943 | * a runnable task of @class priority or higher. | |
67692435 | 3944 | */ |
6e2df058 PZ |
3945 | for_class_range(class, prev->sched_class, &idle_sched_class) { |
3946 | if (class->balance(rq, prev, rf)) | |
3947 | break; | |
3948 | } | |
3949 | #endif | |
3950 | ||
3951 | put_prev_task(rq, prev); | |
67692435 | 3952 | |
34f971f6 | 3953 | for_each_class(class) { |
98c2f700 | 3954 | p = class->pick_next_task(rq); |
67692435 | 3955 | if (p) |
dd41f596 | 3956 | return p; |
dd41f596 | 3957 | } |
34f971f6 | 3958 | |
d1ccc66d IM |
3959 | /* The idle class should always have a runnable task: */ |
3960 | BUG(); | |
dd41f596 | 3961 | } |
1da177e4 | 3962 | |
dd41f596 | 3963 | /* |
c259e01a | 3964 | * __schedule() is the main scheduler function. |
edde96ea PE |
3965 | * |
3966 | * The main means of driving the scheduler and thus entering this function are: | |
3967 | * | |
3968 | * 1. Explicit blocking: mutex, semaphore, waitqueue, etc. | |
3969 | * | |
3970 | * 2. TIF_NEED_RESCHED flag is checked on interrupt and userspace return | |
3971 | * paths. For example, see arch/x86/entry_64.S. | |
3972 | * | |
3973 | * To drive preemption between tasks, the scheduler sets the flag in timer | |
3974 | * interrupt handler scheduler_tick(). | |
3975 | * | |
3976 | * 3. Wakeups don't really cause entry into schedule(). They add a | |
3977 | * task to the run-queue and that's it. | |
3978 | * | |
3979 | * Now, if the new task added to the run-queue preempts the current | |
3980 | * task, then the wakeup sets TIF_NEED_RESCHED and schedule() gets | |
3981 | * called on the nearest possible occasion: | |
3982 | * | |
c1a280b6 | 3983 | * - If the kernel is preemptible (CONFIG_PREEMPTION=y): |
edde96ea PE |
3984 | * |
3985 | * - in syscall or exception context, at the next outmost | |
3986 | * preempt_enable(). (this might be as soon as the wake_up()'s | |
3987 | * spin_unlock()!) | |
3988 | * | |
3989 | * - in IRQ context, return from interrupt-handler to | |
3990 | * preemptible context | |
3991 | * | |
c1a280b6 | 3992 | * - If the kernel is not preemptible (CONFIG_PREEMPTION is not set) |
edde96ea PE |
3993 | * then at the next: |
3994 | * | |
3995 | * - cond_resched() call | |
3996 | * - explicit schedule() call | |
3997 | * - return from syscall or exception to user-space | |
3998 | * - return from interrupt-handler to user-space | |
bfd9b2b5 | 3999 | * |
b30f0e3f | 4000 | * WARNING: must be called with preemption disabled! |
dd41f596 | 4001 | */ |
499d7955 | 4002 | static void __sched notrace __schedule(bool preempt) |
dd41f596 IM |
4003 | { |
4004 | struct task_struct *prev, *next; | |
67ca7bde | 4005 | unsigned long *switch_count; |
d8ac8971 | 4006 | struct rq_flags rf; |
dd41f596 | 4007 | struct rq *rq; |
31656519 | 4008 | int cpu; |
dd41f596 | 4009 | |
dd41f596 IM |
4010 | cpu = smp_processor_id(); |
4011 | rq = cpu_rq(cpu); | |
dd41f596 | 4012 | prev = rq->curr; |
dd41f596 | 4013 | |
312364f3 | 4014 | schedule_debug(prev, preempt); |
1da177e4 | 4015 | |
31656519 | 4016 | if (sched_feat(HRTICK)) |
f333fdc9 | 4017 | hrtick_clear(rq); |
8f4d37ec | 4018 | |
46a5d164 | 4019 | local_irq_disable(); |
bcbfdd01 | 4020 | rcu_note_context_switch(preempt); |
46a5d164 | 4021 | |
e0acd0a6 ON |
4022 | /* |
4023 | * Make sure that signal_pending_state()->signal_pending() below | |
4024 | * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE) | |
4025 | * done by the caller to avoid the race with signal_wake_up(). | |
306e0604 MD |
4026 | * |
4027 | * The membarrier system call requires a full memory barrier | |
4028 | * after coming from user-space, before storing to rq->curr. | |
e0acd0a6 | 4029 | */ |
8a8c69c3 | 4030 | rq_lock(rq, &rf); |
d89e588c | 4031 | smp_mb__after_spinlock(); |
1da177e4 | 4032 | |
d1ccc66d IM |
4033 | /* Promote REQ to ACT */ |
4034 | rq->clock_update_flags <<= 1; | |
bce4dc80 | 4035 | update_rq_clock(rq); |
9edfbfed | 4036 | |
246d86b5 | 4037 | switch_count = &prev->nivcsw; |
fc13aeba | 4038 | if (!preempt && prev->state) { |
34ec35ad | 4039 | if (signal_pending_state(prev->state, prev)) { |
1da177e4 | 4040 | prev->state = TASK_RUNNING; |
21aa9af0 | 4041 | } else { |
bce4dc80 | 4042 | deactivate_task(rq, prev, DEQUEUE_SLEEP | DEQUEUE_NOCLOCK); |
2acca55e | 4043 | |
e33a9bba TH |
4044 | if (prev->in_iowait) { |
4045 | atomic_inc(&rq->nr_iowait); | |
4046 | delayacct_blkio_start(); | |
4047 | } | |
21aa9af0 | 4048 | } |
dd41f596 | 4049 | switch_count = &prev->nvcsw; |
1da177e4 LT |
4050 | } |
4051 | ||
d8ac8971 | 4052 | next = pick_next_task(rq, prev, &rf); |
f26f9aff | 4053 | clear_tsk_need_resched(prev); |
f27dde8d | 4054 | clear_preempt_need_resched(); |
1da177e4 | 4055 | |
1da177e4 | 4056 | if (likely(prev != next)) { |
1da177e4 | 4057 | rq->nr_switches++; |
5311a98f EB |
4058 | /* |
4059 | * RCU users of rcu_dereference(rq->curr) may not see | |
4060 | * changes to task_struct made by pick_next_task(). | |
4061 | */ | |
4062 | RCU_INIT_POINTER(rq->curr, next); | |
22e4ebb9 MD |
4063 | /* |
4064 | * The membarrier system call requires each architecture | |
4065 | * to have a full memory barrier after updating | |
306e0604 MD |
4066 | * rq->curr, before returning to user-space. |
4067 | * | |
4068 | * Here are the schemes providing that barrier on the | |
4069 | * various architectures: | |
4070 | * - mm ? switch_mm() : mmdrop() for x86, s390, sparc, PowerPC. | |
4071 | * switch_mm() rely on membarrier_arch_switch_mm() on PowerPC. | |
4072 | * - finish_lock_switch() for weakly-ordered | |
4073 | * architectures where spin_unlock is a full barrier, | |
4074 | * - switch_to() for arm64 (weakly-ordered, spin_unlock | |
4075 | * is a RELEASE barrier), | |
22e4ebb9 | 4076 | */ |
1da177e4 LT |
4077 | ++*switch_count; |
4078 | ||
b05e75d6 JW |
4079 | psi_sched_switch(prev, next, !task_on_rq_queued(prev)); |
4080 | ||
c73464b1 | 4081 | trace_sched_switch(preempt, prev, next); |
d1ccc66d IM |
4082 | |
4083 | /* Also unlocks the rq: */ | |
4084 | rq = context_switch(rq, prev, next, &rf); | |
cbce1a68 | 4085 | } else { |
cb42c9a3 | 4086 | rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP); |
8a8c69c3 | 4087 | rq_unlock_irq(rq, &rf); |
cbce1a68 | 4088 | } |
1da177e4 | 4089 | |
e3fca9e7 | 4090 | balance_callback(rq); |
1da177e4 | 4091 | } |
c259e01a | 4092 | |
9af6528e PZ |
4093 | void __noreturn do_task_dead(void) |
4094 | { | |
d1ccc66d | 4095 | /* Causes final put_task_struct in finish_task_switch(): */ |
b5bf9a90 | 4096 | set_special_state(TASK_DEAD); |
d1ccc66d IM |
4097 | |
4098 | /* Tell freezer to ignore us: */ | |
4099 | current->flags |= PF_NOFREEZE; | |
4100 | ||
9af6528e PZ |
4101 | __schedule(false); |
4102 | BUG(); | |
d1ccc66d IM |
4103 | |
4104 | /* Avoid "noreturn function does return" - but don't continue if BUG() is a NOP: */ | |
9af6528e | 4105 | for (;;) |
d1ccc66d | 4106 | cpu_relax(); |
9af6528e PZ |
4107 | } |
4108 | ||
9c40cef2 TG |
4109 | static inline void sched_submit_work(struct task_struct *tsk) |
4110 | { | |
b0fdc013 | 4111 | if (!tsk->state) |
9c40cef2 | 4112 | return; |
6d25be57 TG |
4113 | |
4114 | /* | |
4115 | * If a worker went to sleep, notify and ask workqueue whether | |
4116 | * it wants to wake up a task to maintain concurrency. | |
4117 | * As this function is called inside the schedule() context, | |
4118 | * we disable preemption to avoid it calling schedule() again | |
62849a96 SAS |
4119 | * in the possible wakeup of a kworker and because wq_worker_sleeping() |
4120 | * requires it. | |
6d25be57 | 4121 | */ |
771b53d0 | 4122 | if (tsk->flags & (PF_WQ_WORKER | PF_IO_WORKER)) { |
6d25be57 | 4123 | preempt_disable(); |
771b53d0 JA |
4124 | if (tsk->flags & PF_WQ_WORKER) |
4125 | wq_worker_sleeping(tsk); | |
4126 | else | |
4127 | io_wq_worker_sleeping(tsk); | |
6d25be57 TG |
4128 | preempt_enable_no_resched(); |
4129 | } | |
4130 | ||
b0fdc013 SAS |
4131 | if (tsk_is_pi_blocked(tsk)) |
4132 | return; | |
4133 | ||
9c40cef2 TG |
4134 | /* |
4135 | * If we are going to sleep and we have plugged IO queued, | |
4136 | * make sure to submit it to avoid deadlocks. | |
4137 | */ | |
4138 | if (blk_needs_flush_plug(tsk)) | |
4139 | blk_schedule_flush_plug(tsk); | |
4140 | } | |
4141 | ||
6d25be57 TG |
4142 | static void sched_update_worker(struct task_struct *tsk) |
4143 | { | |
771b53d0 JA |
4144 | if (tsk->flags & (PF_WQ_WORKER | PF_IO_WORKER)) { |
4145 | if (tsk->flags & PF_WQ_WORKER) | |
4146 | wq_worker_running(tsk); | |
4147 | else | |
4148 | io_wq_worker_running(tsk); | |
4149 | } | |
6d25be57 TG |
4150 | } |
4151 | ||
722a9f92 | 4152 | asmlinkage __visible void __sched schedule(void) |
c259e01a | 4153 | { |
9c40cef2 TG |
4154 | struct task_struct *tsk = current; |
4155 | ||
4156 | sched_submit_work(tsk); | |
bfd9b2b5 | 4157 | do { |
b30f0e3f | 4158 | preempt_disable(); |
fc13aeba | 4159 | __schedule(false); |
b30f0e3f | 4160 | sched_preempt_enable_no_resched(); |
bfd9b2b5 | 4161 | } while (need_resched()); |
6d25be57 | 4162 | sched_update_worker(tsk); |
c259e01a | 4163 | } |
1da177e4 LT |
4164 | EXPORT_SYMBOL(schedule); |
4165 | ||
8663effb SRV |
4166 | /* |
4167 | * synchronize_rcu_tasks() makes sure that no task is stuck in preempted | |
4168 | * state (have scheduled out non-voluntarily) by making sure that all | |
4169 | * tasks have either left the run queue or have gone into user space. | |
4170 | * As idle tasks do not do either, they must not ever be preempted | |
4171 | * (schedule out non-voluntarily). | |
4172 | * | |
4173 | * schedule_idle() is similar to schedule_preempt_disable() except that it | |
4174 | * never enables preemption because it does not call sched_submit_work(). | |
4175 | */ | |
4176 | void __sched schedule_idle(void) | |
4177 | { | |
4178 | /* | |
4179 | * As this skips calling sched_submit_work(), which the idle task does | |
4180 | * regardless because that function is a nop when the task is in a | |
4181 | * TASK_RUNNING state, make sure this isn't used someplace that the | |
4182 | * current task can be in any other state. Note, idle is always in the | |
4183 | * TASK_RUNNING state. | |
4184 | */ | |
4185 | WARN_ON_ONCE(current->state); | |
4186 | do { | |
4187 | __schedule(false); | |
4188 | } while (need_resched()); | |
4189 | } | |
4190 | ||
91d1aa43 | 4191 | #ifdef CONFIG_CONTEXT_TRACKING |
722a9f92 | 4192 | asmlinkage __visible void __sched schedule_user(void) |
20ab65e3 FW |
4193 | { |
4194 | /* | |
4195 | * If we come here after a random call to set_need_resched(), | |
4196 | * or we have been woken up remotely but the IPI has not yet arrived, | |
4197 | * we haven't yet exited the RCU idle mode. Do it here manually until | |
4198 | * we find a better solution. | |
7cc78f8f AL |
4199 | * |
4200 | * NB: There are buggy callers of this function. Ideally we | |
c467ea76 | 4201 | * should warn if prev_state != CONTEXT_USER, but that will trigger |
7cc78f8f | 4202 | * too frequently to make sense yet. |
20ab65e3 | 4203 | */ |
7cc78f8f | 4204 | enum ctx_state prev_state = exception_enter(); |
20ab65e3 | 4205 | schedule(); |
7cc78f8f | 4206 | exception_exit(prev_state); |
20ab65e3 FW |
4207 | } |
4208 | #endif | |
4209 | ||
c5491ea7 TG |
4210 | /** |
4211 | * schedule_preempt_disabled - called with preemption disabled | |
4212 | * | |
4213 | * Returns with preemption disabled. Note: preempt_count must be 1 | |
4214 | */ | |
4215 | void __sched schedule_preempt_disabled(void) | |
4216 | { | |
ba74c144 | 4217 | sched_preempt_enable_no_resched(); |
c5491ea7 TG |
4218 | schedule(); |
4219 | preempt_disable(); | |
4220 | } | |
4221 | ||
06b1f808 | 4222 | static void __sched notrace preempt_schedule_common(void) |
a18b5d01 FW |
4223 | { |
4224 | do { | |
47252cfb SR |
4225 | /* |
4226 | * Because the function tracer can trace preempt_count_sub() | |
4227 | * and it also uses preempt_enable/disable_notrace(), if | |
4228 | * NEED_RESCHED is set, the preempt_enable_notrace() called | |
4229 | * by the function tracer will call this function again and | |
4230 | * cause infinite recursion. | |
4231 | * | |
4232 | * Preemption must be disabled here before the function | |
4233 | * tracer can trace. Break up preempt_disable() into two | |
4234 | * calls. One to disable preemption without fear of being | |
4235 | * traced. The other to still record the preemption latency, | |
4236 | * which can also be traced by the function tracer. | |
4237 | */ | |
499d7955 | 4238 | preempt_disable_notrace(); |
47252cfb | 4239 | preempt_latency_start(1); |
fc13aeba | 4240 | __schedule(true); |
47252cfb | 4241 | preempt_latency_stop(1); |
499d7955 | 4242 | preempt_enable_no_resched_notrace(); |
a18b5d01 FW |
4243 | |
4244 | /* | |
4245 | * Check again in case we missed a preemption opportunity | |
4246 | * between schedule and now. | |
4247 | */ | |
a18b5d01 FW |
4248 | } while (need_resched()); |
4249 | } | |
4250 | ||
c1a280b6 | 4251 | #ifdef CONFIG_PREEMPTION |
1da177e4 | 4252 | /* |
a49b4f40 VS |
4253 | * This is the entry point to schedule() from in-kernel preemption |
4254 | * off of preempt_enable. | |
1da177e4 | 4255 | */ |
722a9f92 | 4256 | asmlinkage __visible void __sched notrace preempt_schedule(void) |
1da177e4 | 4257 | { |
1da177e4 LT |
4258 | /* |
4259 | * If there is a non-zero preempt_count or interrupts are disabled, | |
41a2d6cf | 4260 | * we do not want to preempt the current task. Just return.. |
1da177e4 | 4261 | */ |
fbb00b56 | 4262 | if (likely(!preemptible())) |
1da177e4 LT |
4263 | return; |
4264 | ||
a18b5d01 | 4265 | preempt_schedule_common(); |
1da177e4 | 4266 | } |
376e2424 | 4267 | NOKPROBE_SYMBOL(preempt_schedule); |
1da177e4 | 4268 | EXPORT_SYMBOL(preempt_schedule); |
009f60e2 | 4269 | |
009f60e2 | 4270 | /** |
4eaca0a8 | 4271 | * preempt_schedule_notrace - preempt_schedule called by tracing |
009f60e2 ON |
4272 | * |
4273 | * The tracing infrastructure uses preempt_enable_notrace to prevent | |
4274 | * recursion and tracing preempt enabling caused by the tracing | |
4275 | * infrastructure itself. But as tracing can happen in areas coming | |
4276 | * from userspace or just about to enter userspace, a preempt enable | |
4277 | * can occur before user_exit() is called. This will cause the scheduler | |
4278 | * to be called when the system is still in usermode. | |
4279 | * | |
4280 | * To prevent this, the preempt_enable_notrace will use this function | |
4281 | * instead of preempt_schedule() to exit user context if needed before | |
4282 | * calling the scheduler. | |
4283 | */ | |
4eaca0a8 | 4284 | asmlinkage __visible void __sched notrace preempt_schedule_notrace(void) |
009f60e2 ON |
4285 | { |
4286 | enum ctx_state prev_ctx; | |
4287 | ||
4288 | if (likely(!preemptible())) | |
4289 | return; | |
4290 | ||
4291 | do { | |
47252cfb SR |
4292 | /* |
4293 | * Because the function tracer can trace preempt_count_sub() | |
4294 | * and it also uses preempt_enable/disable_notrace(), if | |
4295 | * NEED_RESCHED is set, the preempt_enable_notrace() called | |
4296 | * by the function tracer will call this function again and | |
4297 | * cause infinite recursion. | |
4298 | * | |
4299 | * Preemption must be disabled here before the function | |
4300 | * tracer can trace. Break up preempt_disable() into two | |
4301 | * calls. One to disable preemption without fear of being | |
4302 | * traced. The other to still record the preemption latency, | |
4303 | * which can also be traced by the function tracer. | |
4304 | */ | |
3d8f74dd | 4305 | preempt_disable_notrace(); |
47252cfb | 4306 | preempt_latency_start(1); |
009f60e2 ON |
4307 | /* |
4308 | * Needs preempt disabled in case user_exit() is traced | |
4309 | * and the tracer calls preempt_enable_notrace() causing | |
4310 | * an infinite recursion. | |
4311 | */ | |
4312 | prev_ctx = exception_enter(); | |
fc13aeba | 4313 | __schedule(true); |
009f60e2 ON |
4314 | exception_exit(prev_ctx); |
4315 | ||
47252cfb | 4316 | preempt_latency_stop(1); |
3d8f74dd | 4317 | preempt_enable_no_resched_notrace(); |
009f60e2 ON |
4318 | } while (need_resched()); |
4319 | } | |
4eaca0a8 | 4320 | EXPORT_SYMBOL_GPL(preempt_schedule_notrace); |
009f60e2 | 4321 | |
c1a280b6 | 4322 | #endif /* CONFIG_PREEMPTION */ |
1da177e4 LT |
4323 | |
4324 | /* | |
a49b4f40 | 4325 | * This is the entry point to schedule() from kernel preemption |
1da177e4 LT |
4326 | * off of irq context. |
4327 | * Note, that this is called and return with irqs disabled. This will | |
4328 | * protect us against recursive calling from irq. | |
4329 | */ | |
722a9f92 | 4330 | asmlinkage __visible void __sched preempt_schedule_irq(void) |
1da177e4 | 4331 | { |
b22366cd | 4332 | enum ctx_state prev_state; |
6478d880 | 4333 | |
2ed6e34f | 4334 | /* Catch callers which need to be fixed */ |
f27dde8d | 4335 | BUG_ON(preempt_count() || !irqs_disabled()); |
1da177e4 | 4336 | |
b22366cd FW |
4337 | prev_state = exception_enter(); |
4338 | ||
3a5c359a | 4339 | do { |
3d8f74dd | 4340 | preempt_disable(); |
3a5c359a | 4341 | local_irq_enable(); |
fc13aeba | 4342 | __schedule(true); |
3a5c359a | 4343 | local_irq_disable(); |
3d8f74dd | 4344 | sched_preempt_enable_no_resched(); |
5ed0cec0 | 4345 | } while (need_resched()); |
b22366cd FW |
4346 | |
4347 | exception_exit(prev_state); | |
1da177e4 LT |
4348 | } |
4349 | ||
ac6424b9 | 4350 | int default_wake_function(wait_queue_entry_t *curr, unsigned mode, int wake_flags, |
95cdf3b7 | 4351 | void *key) |
1da177e4 | 4352 | { |
63859d4f | 4353 | return try_to_wake_up(curr->private, mode, wake_flags); |
1da177e4 | 4354 | } |
1da177e4 LT |
4355 | EXPORT_SYMBOL(default_wake_function); |
4356 | ||
b29739f9 IM |
4357 | #ifdef CONFIG_RT_MUTEXES |
4358 | ||
acd58620 PZ |
4359 | static inline int __rt_effective_prio(struct task_struct *pi_task, int prio) |
4360 | { | |
4361 | if (pi_task) | |
4362 | prio = min(prio, pi_task->prio); | |
4363 | ||
4364 | return prio; | |
4365 | } | |
4366 | ||
4367 | static inline int rt_effective_prio(struct task_struct *p, int prio) | |
4368 | { | |
4369 | struct task_struct *pi_task = rt_mutex_get_top_task(p); | |
4370 | ||
4371 | return __rt_effective_prio(pi_task, prio); | |
4372 | } | |
4373 | ||
b29739f9 IM |
4374 | /* |
4375 | * rt_mutex_setprio - set the current priority of a task | |
acd58620 PZ |
4376 | * @p: task to boost |
4377 | * @pi_task: donor task | |
b29739f9 IM |
4378 | * |
4379 | * This function changes the 'effective' priority of a task. It does | |
4380 | * not touch ->normal_prio like __setscheduler(). | |
4381 | * | |
c365c292 TG |
4382 | * Used by the rt_mutex code to implement priority inheritance |
4383 | * logic. Call site only calls if the priority of the task changed. | |
b29739f9 | 4384 | */ |
acd58620 | 4385 | void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task) |
b29739f9 | 4386 | { |
acd58620 | 4387 | int prio, oldprio, queued, running, queue_flag = |
7a57f32a | 4388 | DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK; |
83ab0aa0 | 4389 | const struct sched_class *prev_class; |
eb580751 PZ |
4390 | struct rq_flags rf; |
4391 | struct rq *rq; | |
b29739f9 | 4392 | |
acd58620 PZ |
4393 | /* XXX used to be waiter->prio, not waiter->task->prio */ |
4394 | prio = __rt_effective_prio(pi_task, p->normal_prio); | |
4395 | ||
4396 | /* | |
4397 | * If nothing changed; bail early. | |
4398 | */ | |
4399 | if (p->pi_top_task == pi_task && prio == p->prio && !dl_prio(prio)) | |
4400 | return; | |
b29739f9 | 4401 | |
eb580751 | 4402 | rq = __task_rq_lock(p, &rf); |
80f5c1b8 | 4403 | update_rq_clock(rq); |
acd58620 PZ |
4404 | /* |
4405 | * Set under pi_lock && rq->lock, such that the value can be used under | |
4406 | * either lock. | |
4407 | * | |
4408 | * Note that there is loads of tricky to make this pointer cache work | |
4409 | * right. rt_mutex_slowunlock()+rt_mutex_postunlock() work together to | |
4410 | * ensure a task is de-boosted (pi_task is set to NULL) before the | |
4411 | * task is allowed to run again (and can exit). This ensures the pointer | |
4412 | * points to a blocked task -- which guaratees the task is present. | |
4413 | */ | |
4414 | p->pi_top_task = pi_task; | |
4415 | ||
4416 | /* | |
4417 | * For FIFO/RR we only need to set prio, if that matches we're done. | |
4418 | */ | |
4419 | if (prio == p->prio && !dl_prio(prio)) | |
4420 | goto out_unlock; | |
b29739f9 | 4421 | |
1c4dd99b TG |
4422 | /* |
4423 | * Idle task boosting is a nono in general. There is one | |
4424 | * exception, when PREEMPT_RT and NOHZ is active: | |
4425 | * | |
4426 | * The idle task calls get_next_timer_interrupt() and holds | |
4427 | * the timer wheel base->lock on the CPU and another CPU wants | |
4428 | * to access the timer (probably to cancel it). We can safely | |
4429 | * ignore the boosting request, as the idle CPU runs this code | |
4430 | * with interrupts disabled and will complete the lock | |
4431 | * protected section without being interrupted. So there is no | |
4432 | * real need to boost. | |
4433 | */ | |
4434 | if (unlikely(p == rq->idle)) { | |
4435 | WARN_ON(p != rq->curr); | |
4436 | WARN_ON(p->pi_blocked_on); | |
4437 | goto out_unlock; | |
4438 | } | |
4439 | ||
b91473ff | 4440 | trace_sched_pi_setprio(p, pi_task); |
d5f9f942 | 4441 | oldprio = p->prio; |
ff77e468 PZ |
4442 | |
4443 | if (oldprio == prio) | |
4444 | queue_flag &= ~DEQUEUE_MOVE; | |
4445 | ||
83ab0aa0 | 4446 | prev_class = p->sched_class; |
da0c1e65 | 4447 | queued = task_on_rq_queued(p); |
051a1d1a | 4448 | running = task_current(rq, p); |
da0c1e65 | 4449 | if (queued) |
ff77e468 | 4450 | dequeue_task(rq, p, queue_flag); |
0e1f3483 | 4451 | if (running) |
f3cd1c4e | 4452 | put_prev_task(rq, p); |
dd41f596 | 4453 | |
2d3d891d DF |
4454 | /* |
4455 | * Boosting condition are: | |
4456 | * 1. -rt task is running and holds mutex A | |
4457 | * --> -dl task blocks on mutex A | |
4458 | * | |
4459 | * 2. -dl task is running and holds mutex A | |
4460 | * --> -dl task blocks on mutex A and could preempt the | |
4461 | * running task | |
4462 | */ | |
4463 | if (dl_prio(prio)) { | |
466af29b ON |
4464 | if (!dl_prio(p->normal_prio) || |
4465 | (pi_task && dl_entity_preempt(&pi_task->dl, &p->dl))) { | |
2d3d891d | 4466 | p->dl.dl_boosted = 1; |
ff77e468 | 4467 | queue_flag |= ENQUEUE_REPLENISH; |
2d3d891d DF |
4468 | } else |
4469 | p->dl.dl_boosted = 0; | |
aab03e05 | 4470 | p->sched_class = &dl_sched_class; |
2d3d891d DF |
4471 | } else if (rt_prio(prio)) { |
4472 | if (dl_prio(oldprio)) | |
4473 | p->dl.dl_boosted = 0; | |
4474 | if (oldprio < prio) | |
ff77e468 | 4475 | queue_flag |= ENQUEUE_HEAD; |
dd41f596 | 4476 | p->sched_class = &rt_sched_class; |
2d3d891d DF |
4477 | } else { |
4478 | if (dl_prio(oldprio)) | |
4479 | p->dl.dl_boosted = 0; | |
746db944 BS |
4480 | if (rt_prio(oldprio)) |
4481 | p->rt.timeout = 0; | |
dd41f596 | 4482 | p->sched_class = &fair_sched_class; |
2d3d891d | 4483 | } |
dd41f596 | 4484 | |
b29739f9 IM |
4485 | p->prio = prio; |
4486 | ||
da0c1e65 | 4487 | if (queued) |
ff77e468 | 4488 | enqueue_task(rq, p, queue_flag); |
a399d233 | 4489 | if (running) |
03b7fad1 | 4490 | set_next_task(rq, p); |
cb469845 | 4491 | |
da7a735e | 4492 | check_class_changed(rq, p, prev_class, oldprio); |
1c4dd99b | 4493 | out_unlock: |
d1ccc66d IM |
4494 | /* Avoid rq from going away on us: */ |
4495 | preempt_disable(); | |
eb580751 | 4496 | __task_rq_unlock(rq, &rf); |
4c9a4bc8 PZ |
4497 | |
4498 | balance_callback(rq); | |
4499 | preempt_enable(); | |
b29739f9 | 4500 | } |
acd58620 PZ |
4501 | #else |
4502 | static inline int rt_effective_prio(struct task_struct *p, int prio) | |
4503 | { | |
4504 | return prio; | |
4505 | } | |
b29739f9 | 4506 | #endif |
d50dde5a | 4507 | |
36c8b586 | 4508 | void set_user_nice(struct task_struct *p, long nice) |
1da177e4 | 4509 | { |
49bd21ef | 4510 | bool queued, running; |
53a23364 | 4511 | int old_prio; |
eb580751 | 4512 | struct rq_flags rf; |
70b97a7f | 4513 | struct rq *rq; |
1da177e4 | 4514 | |
75e45d51 | 4515 | if (task_nice(p) == nice || nice < MIN_NICE || nice > MAX_NICE) |
1da177e4 LT |
4516 | return; |
4517 | /* | |
4518 | * We have to be careful, if called from sys_setpriority(), | |
4519 | * the task might be in the middle of scheduling on another CPU. | |
4520 | */ | |
eb580751 | 4521 | rq = task_rq_lock(p, &rf); |
2fb8d367 PZ |
4522 | update_rq_clock(rq); |
4523 | ||
1da177e4 LT |
4524 | /* |
4525 | * The RT priorities are set via sched_setscheduler(), but we still | |
4526 | * allow the 'normal' nice value to be set - but as expected | |
4527 | * it wont have any effect on scheduling until the task is | |
aab03e05 | 4528 | * SCHED_DEADLINE, SCHED_FIFO or SCHED_RR: |
1da177e4 | 4529 | */ |
aab03e05 | 4530 | if (task_has_dl_policy(p) || task_has_rt_policy(p)) { |
1da177e4 LT |
4531 | p->static_prio = NICE_TO_PRIO(nice); |
4532 | goto out_unlock; | |
4533 | } | |
da0c1e65 | 4534 | queued = task_on_rq_queued(p); |
49bd21ef | 4535 | running = task_current(rq, p); |
da0c1e65 | 4536 | if (queued) |
7a57f32a | 4537 | dequeue_task(rq, p, DEQUEUE_SAVE | DEQUEUE_NOCLOCK); |
49bd21ef PZ |
4538 | if (running) |
4539 | put_prev_task(rq, p); | |
1da177e4 | 4540 | |
1da177e4 | 4541 | p->static_prio = NICE_TO_PRIO(nice); |
9059393e | 4542 | set_load_weight(p, true); |
b29739f9 IM |
4543 | old_prio = p->prio; |
4544 | p->prio = effective_prio(p); | |
1da177e4 | 4545 | |
5443a0be | 4546 | if (queued) |
7134b3e9 | 4547 | enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK); |
49bd21ef | 4548 | if (running) |
03b7fad1 | 4549 | set_next_task(rq, p); |
5443a0be FW |
4550 | |
4551 | /* | |
4552 | * If the task increased its priority or is running and | |
4553 | * lowered its priority, then reschedule its CPU: | |
4554 | */ | |
4555 | p->sched_class->prio_changed(rq, p, old_prio); | |
4556 | ||
1da177e4 | 4557 | out_unlock: |
eb580751 | 4558 | task_rq_unlock(rq, p, &rf); |
1da177e4 | 4559 | } |
1da177e4 LT |
4560 | EXPORT_SYMBOL(set_user_nice); |
4561 | ||
e43379f1 MM |
4562 | /* |
4563 | * can_nice - check if a task can reduce its nice value | |
4564 | * @p: task | |
4565 | * @nice: nice value | |
4566 | */ | |
36c8b586 | 4567 | int can_nice(const struct task_struct *p, const int nice) |
e43379f1 | 4568 | { |
d1ccc66d | 4569 | /* Convert nice value [19,-20] to rlimit style value [1,40]: */ |
7aa2c016 | 4570 | int nice_rlim = nice_to_rlimit(nice); |
48f24c4d | 4571 | |
78d7d407 | 4572 | return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) || |
e43379f1 MM |
4573 | capable(CAP_SYS_NICE)); |
4574 | } | |
4575 | ||
1da177e4 LT |
4576 | #ifdef __ARCH_WANT_SYS_NICE |
4577 | ||
4578 | /* | |
4579 | * sys_nice - change the priority of the current process. | |
4580 | * @increment: priority increment | |
4581 | * | |
4582 | * sys_setpriority is a more generic, but much slower function that | |
4583 | * does similar things. | |
4584 | */ | |
5add95d4 | 4585 | SYSCALL_DEFINE1(nice, int, increment) |
1da177e4 | 4586 | { |
48f24c4d | 4587 | long nice, retval; |
1da177e4 LT |
4588 | |
4589 | /* | |
4590 | * Setpriority might change our priority at the same moment. | |
4591 | * We don't have to worry. Conceptually one call occurs first | |
4592 | * and we have a single winner. | |
4593 | */ | |
a9467fa3 | 4594 | increment = clamp(increment, -NICE_WIDTH, NICE_WIDTH); |
d0ea0268 | 4595 | nice = task_nice(current) + increment; |
1da177e4 | 4596 | |
a9467fa3 | 4597 | nice = clamp_val(nice, MIN_NICE, MAX_NICE); |
e43379f1 MM |
4598 | if (increment < 0 && !can_nice(current, nice)) |
4599 | return -EPERM; | |
4600 | ||
1da177e4 LT |
4601 | retval = security_task_setnice(current, nice); |
4602 | if (retval) | |
4603 | return retval; | |
4604 | ||
4605 | set_user_nice(current, nice); | |
4606 | return 0; | |
4607 | } | |
4608 | ||
4609 | #endif | |
4610 | ||
4611 | /** | |
4612 | * task_prio - return the priority value of a given task. | |
4613 | * @p: the task in question. | |
4614 | * | |
e69f6186 | 4615 | * Return: The priority value as seen by users in /proc. |
1da177e4 LT |
4616 | * RT tasks are offset by -200. Normal tasks are centered |
4617 | * around 0, value goes from -16 to +15. | |
4618 | */ | |
36c8b586 | 4619 | int task_prio(const struct task_struct *p) |
1da177e4 LT |
4620 | { |
4621 | return p->prio - MAX_RT_PRIO; | |
4622 | } | |
4623 | ||
1da177e4 | 4624 | /** |
d1ccc66d | 4625 | * idle_cpu - is a given CPU idle currently? |
1da177e4 | 4626 | * @cpu: the processor in question. |
e69f6186 YB |
4627 | * |
4628 | * Return: 1 if the CPU is currently idle. 0 otherwise. | |
1da177e4 LT |
4629 | */ |
4630 | int idle_cpu(int cpu) | |
4631 | { | |
908a3283 TG |
4632 | struct rq *rq = cpu_rq(cpu); |
4633 | ||
4634 | if (rq->curr != rq->idle) | |
4635 | return 0; | |
4636 | ||
4637 | if (rq->nr_running) | |
4638 | return 0; | |
4639 | ||
4640 | #ifdef CONFIG_SMP | |
4641 | if (!llist_empty(&rq->wake_list)) | |
4642 | return 0; | |
4643 | #endif | |
4644 | ||
4645 | return 1; | |
1da177e4 LT |
4646 | } |
4647 | ||
943d355d RJ |
4648 | /** |
4649 | * available_idle_cpu - is a given CPU idle for enqueuing work. | |
4650 | * @cpu: the CPU in question. | |
4651 | * | |
4652 | * Return: 1 if the CPU is currently idle. 0 otherwise. | |
4653 | */ | |
4654 | int available_idle_cpu(int cpu) | |
4655 | { | |
4656 | if (!idle_cpu(cpu)) | |
4657 | return 0; | |
4658 | ||
247f2f6f RJ |
4659 | if (vcpu_is_preempted(cpu)) |
4660 | return 0; | |
4661 | ||
908a3283 | 4662 | return 1; |
1da177e4 LT |
4663 | } |
4664 | ||
1da177e4 | 4665 | /** |
d1ccc66d | 4666 | * idle_task - return the idle task for a given CPU. |
1da177e4 | 4667 | * @cpu: the processor in question. |
e69f6186 | 4668 | * |
d1ccc66d | 4669 | * Return: The idle task for the CPU @cpu. |
1da177e4 | 4670 | */ |
36c8b586 | 4671 | struct task_struct *idle_task(int cpu) |
1da177e4 LT |
4672 | { |
4673 | return cpu_rq(cpu)->idle; | |
4674 | } | |
4675 | ||
4676 | /** | |
4677 | * find_process_by_pid - find a process with a matching PID value. | |
4678 | * @pid: the pid in question. | |
e69f6186 YB |
4679 | * |
4680 | * The task of @pid, if found. %NULL otherwise. | |
1da177e4 | 4681 | */ |
a9957449 | 4682 | static struct task_struct *find_process_by_pid(pid_t pid) |
1da177e4 | 4683 | { |
228ebcbe | 4684 | return pid ? find_task_by_vpid(pid) : current; |
1da177e4 LT |
4685 | } |
4686 | ||
c13db6b1 SR |
4687 | /* |
4688 | * sched_setparam() passes in -1 for its policy, to let the functions | |
4689 | * it calls know not to change it. | |
4690 | */ | |
4691 | #define SETPARAM_POLICY -1 | |
4692 | ||
c365c292 TG |
4693 | static void __setscheduler_params(struct task_struct *p, |
4694 | const struct sched_attr *attr) | |
1da177e4 | 4695 | { |
d50dde5a DF |
4696 | int policy = attr->sched_policy; |
4697 | ||
c13db6b1 | 4698 | if (policy == SETPARAM_POLICY) |
39fd8fd2 PZ |
4699 | policy = p->policy; |
4700 | ||
1da177e4 | 4701 | p->policy = policy; |
d50dde5a | 4702 | |
aab03e05 DF |
4703 | if (dl_policy(policy)) |
4704 | __setparam_dl(p, attr); | |
39fd8fd2 | 4705 | else if (fair_policy(policy)) |
d50dde5a DF |
4706 | p->static_prio = NICE_TO_PRIO(attr->sched_nice); |
4707 | ||
39fd8fd2 PZ |
4708 | /* |
4709 | * __sched_setscheduler() ensures attr->sched_priority == 0 when | |
4710 | * !rt_policy. Always setting this ensures that things like | |
4711 | * getparam()/getattr() don't report silly values for !rt tasks. | |
4712 | */ | |
4713 | p->rt_priority = attr->sched_priority; | |
383afd09 | 4714 | p->normal_prio = normal_prio(p); |
9059393e | 4715 | set_load_weight(p, true); |
c365c292 | 4716 | } |
39fd8fd2 | 4717 | |
c365c292 TG |
4718 | /* Actually do priority change: must hold pi & rq lock. */ |
4719 | static void __setscheduler(struct rq *rq, struct task_struct *p, | |
0782e63b | 4720 | const struct sched_attr *attr, bool keep_boost) |
c365c292 | 4721 | { |
a509a7cd PB |
4722 | /* |
4723 | * If params can't change scheduling class changes aren't allowed | |
4724 | * either. | |
4725 | */ | |
4726 | if (attr->sched_flags & SCHED_FLAG_KEEP_PARAMS) | |
4727 | return; | |
4728 | ||
c365c292 | 4729 | __setscheduler_params(p, attr); |
d50dde5a | 4730 | |
383afd09 | 4731 | /* |
0782e63b TG |
4732 | * Keep a potential priority boosting if called from |
4733 | * sched_setscheduler(). | |
383afd09 | 4734 | */ |
acd58620 | 4735 | p->prio = normal_prio(p); |
0782e63b | 4736 | if (keep_boost) |
acd58620 | 4737 | p->prio = rt_effective_prio(p, p->prio); |
383afd09 | 4738 | |
aab03e05 DF |
4739 | if (dl_prio(p->prio)) |
4740 | p->sched_class = &dl_sched_class; | |
4741 | else if (rt_prio(p->prio)) | |
ffd44db5 PZ |
4742 | p->sched_class = &rt_sched_class; |
4743 | else | |
4744 | p->sched_class = &fair_sched_class; | |
1da177e4 | 4745 | } |
aab03e05 | 4746 | |
c69e8d9c | 4747 | /* |
d1ccc66d | 4748 | * Check the target process has a UID that matches the current process's: |
c69e8d9c DH |
4749 | */ |
4750 | static bool check_same_owner(struct task_struct *p) | |
4751 | { | |
4752 | const struct cred *cred = current_cred(), *pcred; | |
4753 | bool match; | |
4754 | ||
4755 | rcu_read_lock(); | |
4756 | pcred = __task_cred(p); | |
9c806aa0 EB |
4757 | match = (uid_eq(cred->euid, pcred->euid) || |
4758 | uid_eq(cred->euid, pcred->uid)); | |
c69e8d9c DH |
4759 | rcu_read_unlock(); |
4760 | return match; | |
4761 | } | |
4762 | ||
d50dde5a DF |
4763 | static int __sched_setscheduler(struct task_struct *p, |
4764 | const struct sched_attr *attr, | |
dbc7f069 | 4765 | bool user, bool pi) |
1da177e4 | 4766 | { |
383afd09 SR |
4767 | int newprio = dl_policy(attr->sched_policy) ? MAX_DL_PRIO - 1 : |
4768 | MAX_RT_PRIO - 1 - attr->sched_priority; | |
da0c1e65 | 4769 | int retval, oldprio, oldpolicy = -1, queued, running; |
0782e63b | 4770 | int new_effective_prio, policy = attr->sched_policy; |
83ab0aa0 | 4771 | const struct sched_class *prev_class; |
eb580751 | 4772 | struct rq_flags rf; |
ca94c442 | 4773 | int reset_on_fork; |
7a57f32a | 4774 | int queue_flags = DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK; |
eb580751 | 4775 | struct rq *rq; |
1da177e4 | 4776 | |
896bbb25 SRV |
4777 | /* The pi code expects interrupts enabled */ |
4778 | BUG_ON(pi && in_interrupt()); | |
1da177e4 | 4779 | recheck: |
d1ccc66d | 4780 | /* Double check policy once rq lock held: */ |
ca94c442 LP |
4781 | if (policy < 0) { |
4782 | reset_on_fork = p->sched_reset_on_fork; | |
1da177e4 | 4783 | policy = oldpolicy = p->policy; |
ca94c442 | 4784 | } else { |
7479f3c9 | 4785 | reset_on_fork = !!(attr->sched_flags & SCHED_FLAG_RESET_ON_FORK); |
ca94c442 | 4786 | |
20f9cd2a | 4787 | if (!valid_policy(policy)) |
ca94c442 LP |
4788 | return -EINVAL; |
4789 | } | |
4790 | ||
794a56eb | 4791 | if (attr->sched_flags & ~(SCHED_FLAG_ALL | SCHED_FLAG_SUGOV)) |
7479f3c9 PZ |
4792 | return -EINVAL; |
4793 | ||
1da177e4 LT |
4794 | /* |
4795 | * Valid priorities for SCHED_FIFO and SCHED_RR are | |
dd41f596 IM |
4796 | * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL, |
4797 | * SCHED_BATCH and SCHED_IDLE is 0. | |
1da177e4 | 4798 | */ |
0bb040a4 | 4799 | if ((p->mm && attr->sched_priority > MAX_USER_RT_PRIO-1) || |
d50dde5a | 4800 | (!p->mm && attr->sched_priority > MAX_RT_PRIO-1)) |
1da177e4 | 4801 | return -EINVAL; |
aab03e05 DF |
4802 | if ((dl_policy(policy) && !__checkparam_dl(attr)) || |
4803 | (rt_policy(policy) != (attr->sched_priority != 0))) | |
1da177e4 LT |
4804 | return -EINVAL; |
4805 | ||
37e4ab3f OC |
4806 | /* |
4807 | * Allow unprivileged RT tasks to decrease priority: | |
4808 | */ | |
961ccddd | 4809 | if (user && !capable(CAP_SYS_NICE)) { |
d50dde5a | 4810 | if (fair_policy(policy)) { |
d0ea0268 | 4811 | if (attr->sched_nice < task_nice(p) && |
eaad4513 | 4812 | !can_nice(p, attr->sched_nice)) |
d50dde5a DF |
4813 | return -EPERM; |
4814 | } | |
4815 | ||
e05606d3 | 4816 | if (rt_policy(policy)) { |
a44702e8 ON |
4817 | unsigned long rlim_rtprio = |
4818 | task_rlimit(p, RLIMIT_RTPRIO); | |
8dc3e909 | 4819 | |
d1ccc66d | 4820 | /* Can't set/change the rt policy: */ |
8dc3e909 ON |
4821 | if (policy != p->policy && !rlim_rtprio) |
4822 | return -EPERM; | |
4823 | ||
d1ccc66d | 4824 | /* Can't increase priority: */ |
d50dde5a DF |
4825 | if (attr->sched_priority > p->rt_priority && |
4826 | attr->sched_priority > rlim_rtprio) | |
8dc3e909 ON |
4827 | return -EPERM; |
4828 | } | |
c02aa73b | 4829 | |
d44753b8 JL |
4830 | /* |
4831 | * Can't set/change SCHED_DEADLINE policy at all for now | |
4832 | * (safest behavior); in the future we would like to allow | |
4833 | * unprivileged DL tasks to increase their relative deadline | |
4834 | * or reduce their runtime (both ways reducing utilization) | |
4835 | */ | |
4836 | if (dl_policy(policy)) | |
4837 | return -EPERM; | |
4838 | ||
dd41f596 | 4839 | /* |
c02aa73b DH |
4840 | * Treat SCHED_IDLE as nice 20. Only allow a switch to |
4841 | * SCHED_NORMAL if the RLIMIT_NICE would normally permit it. | |
dd41f596 | 4842 | */ |
1da1843f | 4843 | if (task_has_idle_policy(p) && !idle_policy(policy)) { |
d0ea0268 | 4844 | if (!can_nice(p, task_nice(p))) |
c02aa73b DH |
4845 | return -EPERM; |
4846 | } | |
5fe1d75f | 4847 | |
d1ccc66d | 4848 | /* Can't change other user's priorities: */ |
c69e8d9c | 4849 | if (!check_same_owner(p)) |
37e4ab3f | 4850 | return -EPERM; |
ca94c442 | 4851 | |
d1ccc66d | 4852 | /* Normal users shall not reset the sched_reset_on_fork flag: */ |
ca94c442 LP |
4853 | if (p->sched_reset_on_fork && !reset_on_fork) |
4854 | return -EPERM; | |
37e4ab3f | 4855 | } |
1da177e4 | 4856 | |
725aad24 | 4857 | if (user) { |
794a56eb JL |
4858 | if (attr->sched_flags & SCHED_FLAG_SUGOV) |
4859 | return -EINVAL; | |
4860 | ||
b0ae1981 | 4861 | retval = security_task_setscheduler(p); |
725aad24 JF |
4862 | if (retval) |
4863 | return retval; | |
4864 | } | |
4865 | ||
a509a7cd PB |
4866 | /* Update task specific "requested" clamps */ |
4867 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) { | |
4868 | retval = uclamp_validate(p, attr); | |
4869 | if (retval) | |
4870 | return retval; | |
4871 | } | |
4872 | ||
710da3c8 JL |
4873 | if (pi) |
4874 | cpuset_read_lock(); | |
4875 | ||
b29739f9 | 4876 | /* |
d1ccc66d | 4877 | * Make sure no PI-waiters arrive (or leave) while we are |
b29739f9 | 4878 | * changing the priority of the task: |
0122ec5b | 4879 | * |
25985edc | 4880 | * To be able to change p->policy safely, the appropriate |
1da177e4 LT |
4881 | * runqueue lock must be held. |
4882 | */ | |
eb580751 | 4883 | rq = task_rq_lock(p, &rf); |
80f5c1b8 | 4884 | update_rq_clock(rq); |
dc61b1d6 | 4885 | |
34f971f6 | 4886 | /* |
d1ccc66d | 4887 | * Changing the policy of the stop threads its a very bad idea: |
34f971f6 PZ |
4888 | */ |
4889 | if (p == rq->stop) { | |
4b211f2b MP |
4890 | retval = -EINVAL; |
4891 | goto unlock; | |
34f971f6 PZ |
4892 | } |
4893 | ||
a51e9198 | 4894 | /* |
d6b1e911 TG |
4895 | * If not changing anything there's no need to proceed further, |
4896 | * but store a possible modification of reset_on_fork. | |
a51e9198 | 4897 | */ |
d50dde5a | 4898 | if (unlikely(policy == p->policy)) { |
d0ea0268 | 4899 | if (fair_policy(policy) && attr->sched_nice != task_nice(p)) |
d50dde5a DF |
4900 | goto change; |
4901 | if (rt_policy(policy) && attr->sched_priority != p->rt_priority) | |
4902 | goto change; | |
75381608 | 4903 | if (dl_policy(policy) && dl_param_changed(p, attr)) |
aab03e05 | 4904 | goto change; |
a509a7cd PB |
4905 | if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) |
4906 | goto change; | |
d50dde5a | 4907 | |
d6b1e911 | 4908 | p->sched_reset_on_fork = reset_on_fork; |
4b211f2b MP |
4909 | retval = 0; |
4910 | goto unlock; | |
a51e9198 | 4911 | } |
d50dde5a | 4912 | change: |
a51e9198 | 4913 | |
dc61b1d6 | 4914 | if (user) { |
332ac17e | 4915 | #ifdef CONFIG_RT_GROUP_SCHED |
dc61b1d6 PZ |
4916 | /* |
4917 | * Do not allow realtime tasks into groups that have no runtime | |
4918 | * assigned. | |
4919 | */ | |
4920 | if (rt_bandwidth_enabled() && rt_policy(policy) && | |
f4493771 MG |
4921 | task_group(p)->rt_bandwidth.rt_runtime == 0 && |
4922 | !task_group_is_autogroup(task_group(p))) { | |
4b211f2b MP |
4923 | retval = -EPERM; |
4924 | goto unlock; | |
dc61b1d6 | 4925 | } |
dc61b1d6 | 4926 | #endif |
332ac17e | 4927 | #ifdef CONFIG_SMP |
794a56eb JL |
4928 | if (dl_bandwidth_enabled() && dl_policy(policy) && |
4929 | !(attr->sched_flags & SCHED_FLAG_SUGOV)) { | |
332ac17e | 4930 | cpumask_t *span = rq->rd->span; |
332ac17e DF |
4931 | |
4932 | /* | |
4933 | * Don't allow tasks with an affinity mask smaller than | |
4934 | * the entire root_domain to become SCHED_DEADLINE. We | |
4935 | * will also fail if there's no bandwidth available. | |
4936 | */ | |
3bd37062 | 4937 | if (!cpumask_subset(span, p->cpus_ptr) || |
e4099a5e | 4938 | rq->rd->dl_bw.bw == 0) { |
4b211f2b MP |
4939 | retval = -EPERM; |
4940 | goto unlock; | |
332ac17e DF |
4941 | } |
4942 | } | |
4943 | #endif | |
4944 | } | |
dc61b1d6 | 4945 | |
d1ccc66d | 4946 | /* Re-check policy now with rq lock held: */ |
1da177e4 LT |
4947 | if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) { |
4948 | policy = oldpolicy = -1; | |
eb580751 | 4949 | task_rq_unlock(rq, p, &rf); |
710da3c8 JL |
4950 | if (pi) |
4951 | cpuset_read_unlock(); | |
1da177e4 LT |
4952 | goto recheck; |
4953 | } | |
332ac17e DF |
4954 | |
4955 | /* | |
4956 | * If setscheduling to SCHED_DEADLINE (or changing the parameters | |
4957 | * of a SCHED_DEADLINE task) we need to check if enough bandwidth | |
4958 | * is available. | |
4959 | */ | |
06a76fe0 | 4960 | if ((dl_policy(policy) || dl_task(p)) && sched_dl_overflow(p, policy, attr)) { |
4b211f2b MP |
4961 | retval = -EBUSY; |
4962 | goto unlock; | |
332ac17e DF |
4963 | } |
4964 | ||
c365c292 TG |
4965 | p->sched_reset_on_fork = reset_on_fork; |
4966 | oldprio = p->prio; | |
4967 | ||
dbc7f069 PZ |
4968 | if (pi) { |
4969 | /* | |
4970 | * Take priority boosted tasks into account. If the new | |
4971 | * effective priority is unchanged, we just store the new | |
4972 | * normal parameters and do not touch the scheduler class and | |
4973 | * the runqueue. This will be done when the task deboost | |
4974 | * itself. | |
4975 | */ | |
acd58620 | 4976 | new_effective_prio = rt_effective_prio(p, newprio); |
ff77e468 PZ |
4977 | if (new_effective_prio == oldprio) |
4978 | queue_flags &= ~DEQUEUE_MOVE; | |
c365c292 TG |
4979 | } |
4980 | ||
da0c1e65 | 4981 | queued = task_on_rq_queued(p); |
051a1d1a | 4982 | running = task_current(rq, p); |
da0c1e65 | 4983 | if (queued) |
ff77e468 | 4984 | dequeue_task(rq, p, queue_flags); |
0e1f3483 | 4985 | if (running) |
f3cd1c4e | 4986 | put_prev_task(rq, p); |
f6b53205 | 4987 | |
83ab0aa0 | 4988 | prev_class = p->sched_class; |
a509a7cd | 4989 | |
dbc7f069 | 4990 | __setscheduler(rq, p, attr, pi); |
a509a7cd | 4991 | __setscheduler_uclamp(p, attr); |
f6b53205 | 4992 | |
da0c1e65 | 4993 | if (queued) { |
81a44c54 TG |
4994 | /* |
4995 | * We enqueue to tail when the priority of a task is | |
4996 | * increased (user space view). | |
4997 | */ | |
ff77e468 PZ |
4998 | if (oldprio < p->prio) |
4999 | queue_flags |= ENQUEUE_HEAD; | |
1de64443 | 5000 | |
ff77e468 | 5001 | enqueue_task(rq, p, queue_flags); |
81a44c54 | 5002 | } |
a399d233 | 5003 | if (running) |
03b7fad1 | 5004 | set_next_task(rq, p); |
cb469845 | 5005 | |
da7a735e | 5006 | check_class_changed(rq, p, prev_class, oldprio); |
d1ccc66d IM |
5007 | |
5008 | /* Avoid rq from going away on us: */ | |
5009 | preempt_disable(); | |
eb580751 | 5010 | task_rq_unlock(rq, p, &rf); |
b29739f9 | 5011 | |
710da3c8 JL |
5012 | if (pi) { |
5013 | cpuset_read_unlock(); | |
dbc7f069 | 5014 | rt_mutex_adjust_pi(p); |
710da3c8 | 5015 | } |
95e02ca9 | 5016 | |
d1ccc66d | 5017 | /* Run balance callbacks after we've adjusted the PI chain: */ |
4c9a4bc8 PZ |
5018 | balance_callback(rq); |
5019 | preempt_enable(); | |
95e02ca9 | 5020 | |
1da177e4 | 5021 | return 0; |
4b211f2b MP |
5022 | |
5023 | unlock: | |
5024 | task_rq_unlock(rq, p, &rf); | |
710da3c8 JL |
5025 | if (pi) |
5026 | cpuset_read_unlock(); | |
4b211f2b | 5027 | return retval; |
1da177e4 | 5028 | } |
961ccddd | 5029 | |
7479f3c9 PZ |
5030 | static int _sched_setscheduler(struct task_struct *p, int policy, |
5031 | const struct sched_param *param, bool check) | |
5032 | { | |
5033 | struct sched_attr attr = { | |
5034 | .sched_policy = policy, | |
5035 | .sched_priority = param->sched_priority, | |
5036 | .sched_nice = PRIO_TO_NICE(p->static_prio), | |
5037 | }; | |
5038 | ||
c13db6b1 SR |
5039 | /* Fixup the legacy SCHED_RESET_ON_FORK hack. */ |
5040 | if ((policy != SETPARAM_POLICY) && (policy & SCHED_RESET_ON_FORK)) { | |
7479f3c9 PZ |
5041 | attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK; |
5042 | policy &= ~SCHED_RESET_ON_FORK; | |
5043 | attr.sched_policy = policy; | |
5044 | } | |
5045 | ||
dbc7f069 | 5046 | return __sched_setscheduler(p, &attr, check, true); |
7479f3c9 | 5047 | } |
961ccddd RR |
5048 | /** |
5049 | * sched_setscheduler - change the scheduling policy and/or RT priority of a thread. | |
5050 | * @p: the task in question. | |
5051 | * @policy: new policy. | |
5052 | * @param: structure containing the new RT priority. | |
5053 | * | |
e69f6186 YB |
5054 | * Return: 0 on success. An error code otherwise. |
5055 | * | |
961ccddd RR |
5056 | * NOTE that the task may be already dead. |
5057 | */ | |
5058 | int sched_setscheduler(struct task_struct *p, int policy, | |
fe7de49f | 5059 | const struct sched_param *param) |
961ccddd | 5060 | { |
7479f3c9 | 5061 | return _sched_setscheduler(p, policy, param, true); |
961ccddd | 5062 | } |
1da177e4 LT |
5063 | EXPORT_SYMBOL_GPL(sched_setscheduler); |
5064 | ||
d50dde5a DF |
5065 | int sched_setattr(struct task_struct *p, const struct sched_attr *attr) |
5066 | { | |
dbc7f069 | 5067 | return __sched_setscheduler(p, attr, true, true); |
d50dde5a DF |
5068 | } |
5069 | EXPORT_SYMBOL_GPL(sched_setattr); | |
5070 | ||
794a56eb JL |
5071 | int sched_setattr_nocheck(struct task_struct *p, const struct sched_attr *attr) |
5072 | { | |
5073 | return __sched_setscheduler(p, attr, false, true); | |
5074 | } | |
5075 | ||
961ccddd RR |
5076 | /** |
5077 | * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace. | |
5078 | * @p: the task in question. | |
5079 | * @policy: new policy. | |
5080 | * @param: structure containing the new RT priority. | |
5081 | * | |
5082 | * Just like sched_setscheduler, only don't bother checking if the | |
5083 | * current context has permission. For example, this is needed in | |
5084 | * stop_machine(): we create temporary high priority worker threads, | |
5085 | * but our caller might not have that capability. | |
e69f6186 YB |
5086 | * |
5087 | * Return: 0 on success. An error code otherwise. | |
961ccddd RR |
5088 | */ |
5089 | int sched_setscheduler_nocheck(struct task_struct *p, int policy, | |
fe7de49f | 5090 | const struct sched_param *param) |
961ccddd | 5091 | { |
7479f3c9 | 5092 | return _sched_setscheduler(p, policy, param, false); |
961ccddd | 5093 | } |
84778472 | 5094 | EXPORT_SYMBOL_GPL(sched_setscheduler_nocheck); |
961ccddd | 5095 | |
95cdf3b7 IM |
5096 | static int |
5097 | do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param) | |
1da177e4 | 5098 | { |
1da177e4 LT |
5099 | struct sched_param lparam; |
5100 | struct task_struct *p; | |
36c8b586 | 5101 | int retval; |
1da177e4 LT |
5102 | |
5103 | if (!param || pid < 0) | |
5104 | return -EINVAL; | |
5105 | if (copy_from_user(&lparam, param, sizeof(struct sched_param))) | |
5106 | return -EFAULT; | |
5fe1d75f ON |
5107 | |
5108 | rcu_read_lock(); | |
5109 | retval = -ESRCH; | |
1da177e4 | 5110 | p = find_process_by_pid(pid); |
710da3c8 JL |
5111 | if (likely(p)) |
5112 | get_task_struct(p); | |
5fe1d75f | 5113 | rcu_read_unlock(); |
36c8b586 | 5114 | |
710da3c8 JL |
5115 | if (likely(p)) { |
5116 | retval = sched_setscheduler(p, policy, &lparam); | |
5117 | put_task_struct(p); | |
5118 | } | |
5119 | ||
1da177e4 LT |
5120 | return retval; |
5121 | } | |
5122 | ||
d50dde5a DF |
5123 | /* |
5124 | * Mimics kernel/events/core.c perf_copy_attr(). | |
5125 | */ | |
d1ccc66d | 5126 | static int sched_copy_attr(struct sched_attr __user *uattr, struct sched_attr *attr) |
d50dde5a DF |
5127 | { |
5128 | u32 size; | |
5129 | int ret; | |
5130 | ||
d1ccc66d | 5131 | /* Zero the full structure, so that a short copy will be nice: */ |
d50dde5a DF |
5132 | memset(attr, 0, sizeof(*attr)); |
5133 | ||
5134 | ret = get_user(size, &uattr->size); | |
5135 | if (ret) | |
5136 | return ret; | |
5137 | ||
d1ccc66d IM |
5138 | /* ABI compatibility quirk: */ |
5139 | if (!size) | |
d50dde5a | 5140 | size = SCHED_ATTR_SIZE_VER0; |
dff3a85f | 5141 | if (size < SCHED_ATTR_SIZE_VER0 || size > PAGE_SIZE) |
d50dde5a DF |
5142 | goto err_size; |
5143 | ||
dff3a85f AS |
5144 | ret = copy_struct_from_user(attr, sizeof(*attr), uattr, size); |
5145 | if (ret) { | |
5146 | if (ret == -E2BIG) | |
5147 | goto err_size; | |
5148 | return ret; | |
d50dde5a DF |
5149 | } |
5150 | ||
a509a7cd PB |
5151 | if ((attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) && |
5152 | size < SCHED_ATTR_SIZE_VER1) | |
5153 | return -EINVAL; | |
5154 | ||
d50dde5a | 5155 | /* |
d1ccc66d | 5156 | * XXX: Do we want to be lenient like existing syscalls; or do we want |
d50dde5a DF |
5157 | * to be strict and return an error on out-of-bounds values? |
5158 | */ | |
75e45d51 | 5159 | attr->sched_nice = clamp(attr->sched_nice, MIN_NICE, MAX_NICE); |
d50dde5a | 5160 | |
e78c7bca | 5161 | return 0; |
d50dde5a DF |
5162 | |
5163 | err_size: | |
5164 | put_user(sizeof(*attr), &uattr->size); | |
e78c7bca | 5165 | return -E2BIG; |
d50dde5a DF |
5166 | } |
5167 | ||
1da177e4 LT |
5168 | /** |
5169 | * sys_sched_setscheduler - set/change the scheduler policy and RT priority | |
5170 | * @pid: the pid in question. | |
5171 | * @policy: new policy. | |
5172 | * @param: structure containing the new RT priority. | |
e69f6186 YB |
5173 | * |
5174 | * Return: 0 on success. An error code otherwise. | |
1da177e4 | 5175 | */ |
d1ccc66d | 5176 | SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy, struct sched_param __user *, param) |
1da177e4 | 5177 | { |
c21761f1 JB |
5178 | if (policy < 0) |
5179 | return -EINVAL; | |
5180 | ||
1da177e4 LT |
5181 | return do_sched_setscheduler(pid, policy, param); |
5182 | } | |
5183 | ||
5184 | /** | |
5185 | * sys_sched_setparam - set/change the RT priority of a thread | |
5186 | * @pid: the pid in question. | |
5187 | * @param: structure containing the new RT priority. | |
e69f6186 YB |
5188 | * |
5189 | * Return: 0 on success. An error code otherwise. | |
1da177e4 | 5190 | */ |
5add95d4 | 5191 | SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param) |
1da177e4 | 5192 | { |
c13db6b1 | 5193 | return do_sched_setscheduler(pid, SETPARAM_POLICY, param); |
1da177e4 LT |
5194 | } |
5195 | ||
d50dde5a DF |
5196 | /** |
5197 | * sys_sched_setattr - same as above, but with extended sched_attr | |
5198 | * @pid: the pid in question. | |
5778fccf | 5199 | * @uattr: structure containing the extended parameters. |
db66d756 | 5200 | * @flags: for future extension. |
d50dde5a | 5201 | */ |
6d35ab48 PZ |
5202 | SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr, |
5203 | unsigned int, flags) | |
d50dde5a DF |
5204 | { |
5205 | struct sched_attr attr; | |
5206 | struct task_struct *p; | |
5207 | int retval; | |
5208 | ||
6d35ab48 | 5209 | if (!uattr || pid < 0 || flags) |
d50dde5a DF |
5210 | return -EINVAL; |
5211 | ||
143cf23d MK |
5212 | retval = sched_copy_attr(uattr, &attr); |
5213 | if (retval) | |
5214 | return retval; | |
d50dde5a | 5215 | |
b14ed2c2 | 5216 | if ((int)attr.sched_policy < 0) |
dbdb2275 | 5217 | return -EINVAL; |
1d6362fa PB |
5218 | if (attr.sched_flags & SCHED_FLAG_KEEP_POLICY) |
5219 | attr.sched_policy = SETPARAM_POLICY; | |
d50dde5a DF |
5220 | |
5221 | rcu_read_lock(); | |
5222 | retval = -ESRCH; | |
5223 | p = find_process_by_pid(pid); | |
a509a7cd PB |
5224 | if (likely(p)) |
5225 | get_task_struct(p); | |
d50dde5a DF |
5226 | rcu_read_unlock(); |
5227 | ||
a509a7cd PB |
5228 | if (likely(p)) { |
5229 | retval = sched_setattr(p, &attr); | |
5230 | put_task_struct(p); | |
5231 | } | |
5232 | ||
d50dde5a DF |
5233 | return retval; |
5234 | } | |
5235 | ||
1da177e4 LT |
5236 | /** |
5237 | * sys_sched_getscheduler - get the policy (scheduling class) of a thread | |
5238 | * @pid: the pid in question. | |
e69f6186 YB |
5239 | * |
5240 | * Return: On success, the policy of the thread. Otherwise, a negative error | |
5241 | * code. | |
1da177e4 | 5242 | */ |
5add95d4 | 5243 | SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid) |
1da177e4 | 5244 | { |
36c8b586 | 5245 | struct task_struct *p; |
3a5c359a | 5246 | int retval; |
1da177e4 LT |
5247 | |
5248 | if (pid < 0) | |
3a5c359a | 5249 | return -EINVAL; |
1da177e4 LT |
5250 | |
5251 | retval = -ESRCH; | |
5fe85be0 | 5252 | rcu_read_lock(); |
1da177e4 LT |
5253 | p = find_process_by_pid(pid); |
5254 | if (p) { | |
5255 | retval = security_task_getscheduler(p); | |
5256 | if (!retval) | |
ca94c442 LP |
5257 | retval = p->policy |
5258 | | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0); | |
1da177e4 | 5259 | } |
5fe85be0 | 5260 | rcu_read_unlock(); |
1da177e4 LT |
5261 | return retval; |
5262 | } | |
5263 | ||
5264 | /** | |
ca94c442 | 5265 | * sys_sched_getparam - get the RT priority of a thread |
1da177e4 LT |
5266 | * @pid: the pid in question. |
5267 | * @param: structure containing the RT priority. | |
e69f6186 YB |
5268 | * |
5269 | * Return: On success, 0 and the RT priority is in @param. Otherwise, an error | |
5270 | * code. | |
1da177e4 | 5271 | */ |
5add95d4 | 5272 | SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param) |
1da177e4 | 5273 | { |
ce5f7f82 | 5274 | struct sched_param lp = { .sched_priority = 0 }; |
36c8b586 | 5275 | struct task_struct *p; |
3a5c359a | 5276 | int retval; |
1da177e4 LT |
5277 | |
5278 | if (!param || pid < 0) | |
3a5c359a | 5279 | return -EINVAL; |
1da177e4 | 5280 | |
5fe85be0 | 5281 | rcu_read_lock(); |
1da177e4 LT |
5282 | p = find_process_by_pid(pid); |
5283 | retval = -ESRCH; | |
5284 | if (!p) | |
5285 | goto out_unlock; | |
5286 | ||
5287 | retval = security_task_getscheduler(p); | |
5288 | if (retval) | |
5289 | goto out_unlock; | |
5290 | ||
ce5f7f82 PZ |
5291 | if (task_has_rt_policy(p)) |
5292 | lp.sched_priority = p->rt_priority; | |
5fe85be0 | 5293 | rcu_read_unlock(); |
1da177e4 LT |
5294 | |
5295 | /* | |
5296 | * This one might sleep, we cannot do it with a spinlock held ... | |
5297 | */ | |
5298 | retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0; | |
5299 | ||
1da177e4 LT |
5300 | return retval; |
5301 | ||
5302 | out_unlock: | |
5fe85be0 | 5303 | rcu_read_unlock(); |
1da177e4 LT |
5304 | return retval; |
5305 | } | |
5306 | ||
1251201c IM |
5307 | /* |
5308 | * Copy the kernel size attribute structure (which might be larger | |
5309 | * than what user-space knows about) to user-space. | |
5310 | * | |
5311 | * Note that all cases are valid: user-space buffer can be larger or | |
5312 | * smaller than the kernel-space buffer. The usual case is that both | |
5313 | * have the same size. | |
5314 | */ | |
5315 | static int | |
5316 | sched_attr_copy_to_user(struct sched_attr __user *uattr, | |
5317 | struct sched_attr *kattr, | |
5318 | unsigned int usize) | |
d50dde5a | 5319 | { |
1251201c | 5320 | unsigned int ksize = sizeof(*kattr); |
d50dde5a | 5321 | |
96d4f267 | 5322 | if (!access_ok(uattr, usize)) |
d50dde5a DF |
5323 | return -EFAULT; |
5324 | ||
5325 | /* | |
1251201c IM |
5326 | * sched_getattr() ABI forwards and backwards compatibility: |
5327 | * | |
5328 | * If usize == ksize then we just copy everything to user-space and all is good. | |
5329 | * | |
5330 | * If usize < ksize then we only copy as much as user-space has space for, | |
5331 | * this keeps ABI compatibility as well. We skip the rest. | |
5332 | * | |
5333 | * If usize > ksize then user-space is using a newer version of the ABI, | |
5334 | * which part the kernel doesn't know about. Just ignore it - tooling can | |
5335 | * detect the kernel's knowledge of attributes from the attr->size value | |
5336 | * which is set to ksize in this case. | |
d50dde5a | 5337 | */ |
1251201c | 5338 | kattr->size = min(usize, ksize); |
d50dde5a | 5339 | |
1251201c | 5340 | if (copy_to_user(uattr, kattr, kattr->size)) |
d50dde5a DF |
5341 | return -EFAULT; |
5342 | ||
22400674 | 5343 | return 0; |
d50dde5a DF |
5344 | } |
5345 | ||
5346 | /** | |
aab03e05 | 5347 | * sys_sched_getattr - similar to sched_getparam, but with sched_attr |
d50dde5a | 5348 | * @pid: the pid in question. |
5778fccf | 5349 | * @uattr: structure containing the extended parameters. |
dff3a85f | 5350 | * @usize: sizeof(attr) for fwd/bwd comp. |
db66d756 | 5351 | * @flags: for future extension. |
d50dde5a | 5352 | */ |
6d35ab48 | 5353 | SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr, |
1251201c | 5354 | unsigned int, usize, unsigned int, flags) |
d50dde5a | 5355 | { |
1251201c | 5356 | struct sched_attr kattr = { }; |
d50dde5a DF |
5357 | struct task_struct *p; |
5358 | int retval; | |
5359 | ||
1251201c IM |
5360 | if (!uattr || pid < 0 || usize > PAGE_SIZE || |
5361 | usize < SCHED_ATTR_SIZE_VER0 || flags) | |
d50dde5a DF |
5362 | return -EINVAL; |
5363 | ||
5364 | rcu_read_lock(); | |
5365 | p = find_process_by_pid(pid); | |
5366 | retval = -ESRCH; | |
5367 | if (!p) | |
5368 | goto out_unlock; | |
5369 | ||
5370 | retval = security_task_getscheduler(p); | |
5371 | if (retval) | |
5372 | goto out_unlock; | |
5373 | ||
1251201c | 5374 | kattr.sched_policy = p->policy; |
7479f3c9 | 5375 | if (p->sched_reset_on_fork) |
1251201c | 5376 | kattr.sched_flags |= SCHED_FLAG_RESET_ON_FORK; |
aab03e05 | 5377 | if (task_has_dl_policy(p)) |
1251201c | 5378 | __getparam_dl(p, &kattr); |
aab03e05 | 5379 | else if (task_has_rt_policy(p)) |
1251201c | 5380 | kattr.sched_priority = p->rt_priority; |
d50dde5a | 5381 | else |
1251201c | 5382 | kattr.sched_nice = task_nice(p); |
d50dde5a | 5383 | |
a509a7cd | 5384 | #ifdef CONFIG_UCLAMP_TASK |
1251201c IM |
5385 | kattr.sched_util_min = p->uclamp_req[UCLAMP_MIN].value; |
5386 | kattr.sched_util_max = p->uclamp_req[UCLAMP_MAX].value; | |
a509a7cd PB |
5387 | #endif |
5388 | ||
d50dde5a DF |
5389 | rcu_read_unlock(); |
5390 | ||
1251201c | 5391 | return sched_attr_copy_to_user(uattr, &kattr, usize); |
d50dde5a DF |
5392 | |
5393 | out_unlock: | |
5394 | rcu_read_unlock(); | |
5395 | return retval; | |
5396 | } | |
5397 | ||
96f874e2 | 5398 | long sched_setaffinity(pid_t pid, const struct cpumask *in_mask) |
1da177e4 | 5399 | { |
5a16f3d3 | 5400 | cpumask_var_t cpus_allowed, new_mask; |
36c8b586 IM |
5401 | struct task_struct *p; |
5402 | int retval; | |
1da177e4 | 5403 | |
23f5d142 | 5404 | rcu_read_lock(); |
1da177e4 LT |
5405 | |
5406 | p = find_process_by_pid(pid); | |
5407 | if (!p) { | |
23f5d142 | 5408 | rcu_read_unlock(); |
1da177e4 LT |
5409 | return -ESRCH; |
5410 | } | |
5411 | ||
23f5d142 | 5412 | /* Prevent p going away */ |
1da177e4 | 5413 | get_task_struct(p); |
23f5d142 | 5414 | rcu_read_unlock(); |
1da177e4 | 5415 | |
14a40ffc TH |
5416 | if (p->flags & PF_NO_SETAFFINITY) { |
5417 | retval = -EINVAL; | |
5418 | goto out_put_task; | |
5419 | } | |
5a16f3d3 RR |
5420 | if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) { |
5421 | retval = -ENOMEM; | |
5422 | goto out_put_task; | |
5423 | } | |
5424 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) { | |
5425 | retval = -ENOMEM; | |
5426 | goto out_free_cpus_allowed; | |
5427 | } | |
1da177e4 | 5428 | retval = -EPERM; |
4c44aaaf EB |
5429 | if (!check_same_owner(p)) { |
5430 | rcu_read_lock(); | |
5431 | if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) { | |
5432 | rcu_read_unlock(); | |
16303ab2 | 5433 | goto out_free_new_mask; |
4c44aaaf EB |
5434 | } |
5435 | rcu_read_unlock(); | |
5436 | } | |
1da177e4 | 5437 | |
b0ae1981 | 5438 | retval = security_task_setscheduler(p); |
e7834f8f | 5439 | if (retval) |
16303ab2 | 5440 | goto out_free_new_mask; |
e7834f8f | 5441 | |
e4099a5e PZ |
5442 | |
5443 | cpuset_cpus_allowed(p, cpus_allowed); | |
5444 | cpumask_and(new_mask, in_mask, cpus_allowed); | |
5445 | ||
332ac17e DF |
5446 | /* |
5447 | * Since bandwidth control happens on root_domain basis, | |
5448 | * if admission test is enabled, we only admit -deadline | |
5449 | * tasks allowed to run on all the CPUs in the task's | |
5450 | * root_domain. | |
5451 | */ | |
5452 | #ifdef CONFIG_SMP | |
f1e3a093 KT |
5453 | if (task_has_dl_policy(p) && dl_bandwidth_enabled()) { |
5454 | rcu_read_lock(); | |
5455 | if (!cpumask_subset(task_rq(p)->rd->span, new_mask)) { | |
332ac17e | 5456 | retval = -EBUSY; |
f1e3a093 | 5457 | rcu_read_unlock(); |
16303ab2 | 5458 | goto out_free_new_mask; |
332ac17e | 5459 | } |
f1e3a093 | 5460 | rcu_read_unlock(); |
332ac17e DF |
5461 | } |
5462 | #endif | |
49246274 | 5463 | again: |
25834c73 | 5464 | retval = __set_cpus_allowed_ptr(p, new_mask, true); |
1da177e4 | 5465 | |
8707d8b8 | 5466 | if (!retval) { |
5a16f3d3 RR |
5467 | cpuset_cpus_allowed(p, cpus_allowed); |
5468 | if (!cpumask_subset(new_mask, cpus_allowed)) { | |
8707d8b8 PM |
5469 | /* |
5470 | * We must have raced with a concurrent cpuset | |
5471 | * update. Just reset the cpus_allowed to the | |
5472 | * cpuset's cpus_allowed | |
5473 | */ | |
5a16f3d3 | 5474 | cpumask_copy(new_mask, cpus_allowed); |
8707d8b8 PM |
5475 | goto again; |
5476 | } | |
5477 | } | |
16303ab2 | 5478 | out_free_new_mask: |
5a16f3d3 RR |
5479 | free_cpumask_var(new_mask); |
5480 | out_free_cpus_allowed: | |
5481 | free_cpumask_var(cpus_allowed); | |
5482 | out_put_task: | |
1da177e4 | 5483 | put_task_struct(p); |
1da177e4 LT |
5484 | return retval; |
5485 | } | |
5486 | ||
5487 | static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len, | |
96f874e2 | 5488 | struct cpumask *new_mask) |
1da177e4 | 5489 | { |
96f874e2 RR |
5490 | if (len < cpumask_size()) |
5491 | cpumask_clear(new_mask); | |
5492 | else if (len > cpumask_size()) | |
5493 | len = cpumask_size(); | |
5494 | ||
1da177e4 LT |
5495 | return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0; |
5496 | } | |
5497 | ||
5498 | /** | |
d1ccc66d | 5499 | * sys_sched_setaffinity - set the CPU affinity of a process |
1da177e4 LT |
5500 | * @pid: pid of the process |
5501 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr | |
d1ccc66d | 5502 | * @user_mask_ptr: user-space pointer to the new CPU mask |
e69f6186 YB |
5503 | * |
5504 | * Return: 0 on success. An error code otherwise. | |
1da177e4 | 5505 | */ |
5add95d4 HC |
5506 | SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len, |
5507 | unsigned long __user *, user_mask_ptr) | |
1da177e4 | 5508 | { |
5a16f3d3 | 5509 | cpumask_var_t new_mask; |
1da177e4 LT |
5510 | int retval; |
5511 | ||
5a16f3d3 RR |
5512 | if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) |
5513 | return -ENOMEM; | |
1da177e4 | 5514 | |
5a16f3d3 RR |
5515 | retval = get_user_cpu_mask(user_mask_ptr, len, new_mask); |
5516 | if (retval == 0) | |
5517 | retval = sched_setaffinity(pid, new_mask); | |
5518 | free_cpumask_var(new_mask); | |
5519 | return retval; | |
1da177e4 LT |
5520 | } |
5521 | ||
96f874e2 | 5522 | long sched_getaffinity(pid_t pid, struct cpumask *mask) |
1da177e4 | 5523 | { |
36c8b586 | 5524 | struct task_struct *p; |
31605683 | 5525 | unsigned long flags; |
1da177e4 | 5526 | int retval; |
1da177e4 | 5527 | |
23f5d142 | 5528 | rcu_read_lock(); |
1da177e4 LT |
5529 | |
5530 | retval = -ESRCH; | |
5531 | p = find_process_by_pid(pid); | |
5532 | if (!p) | |
5533 | goto out_unlock; | |
5534 | ||
e7834f8f DQ |
5535 | retval = security_task_getscheduler(p); |
5536 | if (retval) | |
5537 | goto out_unlock; | |
5538 | ||
013fdb80 | 5539 | raw_spin_lock_irqsave(&p->pi_lock, flags); |
3bd37062 | 5540 | cpumask_and(mask, &p->cpus_mask, cpu_active_mask); |
013fdb80 | 5541 | raw_spin_unlock_irqrestore(&p->pi_lock, flags); |
1da177e4 LT |
5542 | |
5543 | out_unlock: | |
23f5d142 | 5544 | rcu_read_unlock(); |
1da177e4 | 5545 | |
9531b62f | 5546 | return retval; |
1da177e4 LT |
5547 | } |
5548 | ||
5549 | /** | |
d1ccc66d | 5550 | * sys_sched_getaffinity - get the CPU affinity of a process |
1da177e4 LT |
5551 | * @pid: pid of the process |
5552 | * @len: length in bytes of the bitmask pointed to by user_mask_ptr | |
d1ccc66d | 5553 | * @user_mask_ptr: user-space pointer to hold the current CPU mask |
e69f6186 | 5554 | * |
599b4840 ZW |
5555 | * Return: size of CPU mask copied to user_mask_ptr on success. An |
5556 | * error code otherwise. | |
1da177e4 | 5557 | */ |
5add95d4 HC |
5558 | SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len, |
5559 | unsigned long __user *, user_mask_ptr) | |
1da177e4 LT |
5560 | { |
5561 | int ret; | |
f17c8607 | 5562 | cpumask_var_t mask; |
1da177e4 | 5563 | |
84fba5ec | 5564 | if ((len * BITS_PER_BYTE) < nr_cpu_ids) |
cd3d8031 KM |
5565 | return -EINVAL; |
5566 | if (len & (sizeof(unsigned long)-1)) | |
1da177e4 LT |
5567 | return -EINVAL; |
5568 | ||
f17c8607 RR |
5569 | if (!alloc_cpumask_var(&mask, GFP_KERNEL)) |
5570 | return -ENOMEM; | |
1da177e4 | 5571 | |
f17c8607 RR |
5572 | ret = sched_getaffinity(pid, mask); |
5573 | if (ret == 0) { | |
4de373a1 | 5574 | unsigned int retlen = min(len, cpumask_size()); |
cd3d8031 KM |
5575 | |
5576 | if (copy_to_user(user_mask_ptr, mask, retlen)) | |
f17c8607 RR |
5577 | ret = -EFAULT; |
5578 | else | |
cd3d8031 | 5579 | ret = retlen; |
f17c8607 RR |
5580 | } |
5581 | free_cpumask_var(mask); | |
1da177e4 | 5582 | |
f17c8607 | 5583 | return ret; |
1da177e4 LT |
5584 | } |
5585 | ||
5586 | /** | |
5587 | * sys_sched_yield - yield the current processor to other threads. | |
5588 | * | |
dd41f596 IM |
5589 | * This function yields the current CPU to other tasks. If there are no |
5590 | * other threads running on this CPU then this function will return. | |
e69f6186 YB |
5591 | * |
5592 | * Return: 0. | |
1da177e4 | 5593 | */ |
7d4dd4f1 | 5594 | static void do_sched_yield(void) |
1da177e4 | 5595 | { |
8a8c69c3 PZ |
5596 | struct rq_flags rf; |
5597 | struct rq *rq; | |
5598 | ||
246b3b33 | 5599 | rq = this_rq_lock_irq(&rf); |
1da177e4 | 5600 | |
ae92882e | 5601 | schedstat_inc(rq->yld_count); |
4530d7ab | 5602 | current->sched_class->yield_task(rq); |
1da177e4 LT |
5603 | |
5604 | /* | |
5605 | * Since we are going to call schedule() anyway, there's | |
5606 | * no need to preempt or enable interrupts: | |
5607 | */ | |
8a8c69c3 PZ |
5608 | preempt_disable(); |
5609 | rq_unlock(rq, &rf); | |
ba74c144 | 5610 | sched_preempt_enable_no_resched(); |
1da177e4 LT |
5611 | |
5612 | schedule(); | |
7d4dd4f1 | 5613 | } |
1da177e4 | 5614 | |
7d4dd4f1 DB |
5615 | SYSCALL_DEFINE0(sched_yield) |
5616 | { | |
5617 | do_sched_yield(); | |
1da177e4 LT |
5618 | return 0; |
5619 | } | |
5620 | ||
c1a280b6 | 5621 | #ifndef CONFIG_PREEMPTION |
02b67cc3 | 5622 | int __sched _cond_resched(void) |
1da177e4 | 5623 | { |
fe32d3cd | 5624 | if (should_resched(0)) { |
a18b5d01 | 5625 | preempt_schedule_common(); |
1da177e4 LT |
5626 | return 1; |
5627 | } | |
f79c3ad6 | 5628 | rcu_all_qs(); |
1da177e4 LT |
5629 | return 0; |
5630 | } | |
02b67cc3 | 5631 | EXPORT_SYMBOL(_cond_resched); |
35a773a0 | 5632 | #endif |
1da177e4 LT |
5633 | |
5634 | /* | |
613afbf8 | 5635 | * __cond_resched_lock() - if a reschedule is pending, drop the given lock, |
1da177e4 LT |
5636 | * call schedule, and on return reacquire the lock. |
5637 | * | |
c1a280b6 | 5638 | * This works OK both with and without CONFIG_PREEMPTION. We do strange low-level |
1da177e4 LT |
5639 | * operations here to prevent schedule() from being called twice (once via |
5640 | * spin_unlock(), once by hand). | |
5641 | */ | |
613afbf8 | 5642 | int __cond_resched_lock(spinlock_t *lock) |
1da177e4 | 5643 | { |
fe32d3cd | 5644 | int resched = should_resched(PREEMPT_LOCK_OFFSET); |
6df3cecb JK |
5645 | int ret = 0; |
5646 | ||
f607c668 PZ |
5647 | lockdep_assert_held(lock); |
5648 | ||
4a81e832 | 5649 | if (spin_needbreak(lock) || resched) { |
1da177e4 | 5650 | spin_unlock(lock); |
d86ee480 | 5651 | if (resched) |
a18b5d01 | 5652 | preempt_schedule_common(); |
95c354fe NP |
5653 | else |
5654 | cpu_relax(); | |
6df3cecb | 5655 | ret = 1; |
1da177e4 | 5656 | spin_lock(lock); |
1da177e4 | 5657 | } |
6df3cecb | 5658 | return ret; |
1da177e4 | 5659 | } |
613afbf8 | 5660 | EXPORT_SYMBOL(__cond_resched_lock); |
1da177e4 | 5661 | |
1da177e4 LT |
5662 | /** |
5663 | * yield - yield the current processor to other threads. | |
5664 | * | |
8e3fabfd PZ |
5665 | * Do not ever use this function, there's a 99% chance you're doing it wrong. |
5666 | * | |
5667 | * The scheduler is at all times free to pick the calling task as the most | |
5668 | * eligible task to run, if removing the yield() call from your code breaks | |
5669 | * it, its already broken. | |
5670 | * | |
5671 | * Typical broken usage is: | |
5672 | * | |
5673 | * while (!event) | |
d1ccc66d | 5674 | * yield(); |
8e3fabfd PZ |
5675 | * |
5676 | * where one assumes that yield() will let 'the other' process run that will | |
5677 | * make event true. If the current task is a SCHED_FIFO task that will never | |
5678 | * happen. Never use yield() as a progress guarantee!! | |
5679 | * | |
5680 | * If you want to use yield() to wait for something, use wait_event(). | |
5681 | * If you want to use yield() to be 'nice' for others, use cond_resched(). | |
5682 | * If you still want to use yield(), do not! | |
1da177e4 LT |
5683 | */ |
5684 | void __sched yield(void) | |
5685 | { | |
5686 | set_current_state(TASK_RUNNING); | |
7d4dd4f1 | 5687 | do_sched_yield(); |
1da177e4 | 5688 | } |
1da177e4 LT |
5689 | EXPORT_SYMBOL(yield); |
5690 | ||
d95f4122 MG |
5691 | /** |
5692 | * yield_to - yield the current processor to another thread in | |
5693 | * your thread group, or accelerate that thread toward the | |
5694 | * processor it's on. | |
16addf95 RD |
5695 | * @p: target task |
5696 | * @preempt: whether task preemption is allowed or not | |
d95f4122 MG |
5697 | * |
5698 | * It's the caller's job to ensure that the target task struct | |
5699 | * can't go away on us before we can do any checks. | |
5700 | * | |
e69f6186 | 5701 | * Return: |
7b270f60 PZ |
5702 | * true (>0) if we indeed boosted the target task. |
5703 | * false (0) if we failed to boost the target. | |
5704 | * -ESRCH if there's no task to yield to. | |
d95f4122 | 5705 | */ |
fa93384f | 5706 | int __sched yield_to(struct task_struct *p, bool preempt) |
d95f4122 MG |
5707 | { |
5708 | struct task_struct *curr = current; | |
5709 | struct rq *rq, *p_rq; | |
5710 | unsigned long flags; | |
c3c18640 | 5711 | int yielded = 0; |
d95f4122 MG |
5712 | |
5713 | local_irq_save(flags); | |
5714 | rq = this_rq(); | |
5715 | ||
5716 | again: | |
5717 | p_rq = task_rq(p); | |
7b270f60 PZ |
5718 | /* |
5719 | * If we're the only runnable task on the rq and target rq also | |
5720 | * has only one task, there's absolutely no point in yielding. | |
5721 | */ | |
5722 | if (rq->nr_running == 1 && p_rq->nr_running == 1) { | |
5723 | yielded = -ESRCH; | |
5724 | goto out_irq; | |
5725 | } | |
5726 | ||
d95f4122 | 5727 | double_rq_lock(rq, p_rq); |
39e24d8f | 5728 | if (task_rq(p) != p_rq) { |
d95f4122 MG |
5729 | double_rq_unlock(rq, p_rq); |
5730 | goto again; | |
5731 | } | |
5732 | ||
5733 | if (!curr->sched_class->yield_to_task) | |
7b270f60 | 5734 | goto out_unlock; |
d95f4122 MG |
5735 | |
5736 | if (curr->sched_class != p->sched_class) | |
7b270f60 | 5737 | goto out_unlock; |
d95f4122 MG |
5738 | |
5739 | if (task_running(p_rq, p) || p->state) | |
7b270f60 | 5740 | goto out_unlock; |
d95f4122 MG |
5741 | |
5742 | yielded = curr->sched_class->yield_to_task(rq, p, preempt); | |
6d1cafd8 | 5743 | if (yielded) { |
ae92882e | 5744 | schedstat_inc(rq->yld_count); |
6d1cafd8 VP |
5745 | /* |
5746 | * Make p's CPU reschedule; pick_next_entity takes care of | |
5747 | * fairness. | |
5748 | */ | |
5749 | if (preempt && rq != p_rq) | |
8875125e | 5750 | resched_curr(p_rq); |
6d1cafd8 | 5751 | } |
d95f4122 | 5752 | |
7b270f60 | 5753 | out_unlock: |
d95f4122 | 5754 | double_rq_unlock(rq, p_rq); |
7b270f60 | 5755 | out_irq: |
d95f4122 MG |
5756 | local_irq_restore(flags); |
5757 | ||
7b270f60 | 5758 | if (yielded > 0) |
d95f4122 MG |
5759 | schedule(); |
5760 | ||
5761 | return yielded; | |
5762 | } | |
5763 | EXPORT_SYMBOL_GPL(yield_to); | |
5764 | ||
10ab5643 TH |
5765 | int io_schedule_prepare(void) |
5766 | { | |
5767 | int old_iowait = current->in_iowait; | |
5768 | ||
5769 | current->in_iowait = 1; | |
5770 | blk_schedule_flush_plug(current); | |
5771 | ||
5772 | return old_iowait; | |
5773 | } | |
5774 | ||
5775 | void io_schedule_finish(int token) | |
5776 | { | |
5777 | current->in_iowait = token; | |
5778 | } | |
5779 | ||
1da177e4 | 5780 | /* |
41a2d6cf | 5781 | * This task is about to go to sleep on IO. Increment rq->nr_iowait so |
1da177e4 | 5782 | * that process accounting knows that this is a task in IO wait state. |
1da177e4 | 5783 | */ |
1da177e4 LT |
5784 | long __sched io_schedule_timeout(long timeout) |
5785 | { | |
10ab5643 | 5786 | int token; |
1da177e4 LT |
5787 | long ret; |
5788 | ||
10ab5643 | 5789 | token = io_schedule_prepare(); |
1da177e4 | 5790 | ret = schedule_timeout(timeout); |
10ab5643 | 5791 | io_schedule_finish(token); |
9cff8ade | 5792 | |
1da177e4 LT |
5793 | return ret; |
5794 | } | |
9cff8ade | 5795 | EXPORT_SYMBOL(io_schedule_timeout); |
1da177e4 | 5796 | |
e3b929b0 | 5797 | void __sched io_schedule(void) |
10ab5643 TH |
5798 | { |
5799 | int token; | |
5800 | ||
5801 | token = io_schedule_prepare(); | |
5802 | schedule(); | |
5803 | io_schedule_finish(token); | |
5804 | } | |
5805 | EXPORT_SYMBOL(io_schedule); | |
5806 | ||
1da177e4 LT |
5807 | /** |
5808 | * sys_sched_get_priority_max - return maximum RT priority. | |
5809 | * @policy: scheduling class. | |
5810 | * | |
e69f6186 YB |
5811 | * Return: On success, this syscall returns the maximum |
5812 | * rt_priority that can be used by a given scheduling class. | |
5813 | * On failure, a negative error code is returned. | |
1da177e4 | 5814 | */ |
5add95d4 | 5815 | SYSCALL_DEFINE1(sched_get_priority_max, int, policy) |
1da177e4 LT |
5816 | { |
5817 | int ret = -EINVAL; | |
5818 | ||
5819 | switch (policy) { | |
5820 | case SCHED_FIFO: | |
5821 | case SCHED_RR: | |
5822 | ret = MAX_USER_RT_PRIO-1; | |
5823 | break; | |
aab03e05 | 5824 | case SCHED_DEADLINE: |
1da177e4 | 5825 | case SCHED_NORMAL: |
b0a9499c | 5826 | case SCHED_BATCH: |
dd41f596 | 5827 | case SCHED_IDLE: |
1da177e4 LT |
5828 | ret = 0; |
5829 | break; | |
5830 | } | |
5831 | return ret; | |
5832 | } | |
5833 | ||
5834 | /** | |
5835 | * sys_sched_get_priority_min - return minimum RT priority. | |
5836 | * @policy: scheduling class. | |
5837 | * | |
e69f6186 YB |
5838 | * Return: On success, this syscall returns the minimum |
5839 | * rt_priority that can be used by a given scheduling class. | |
5840 | * On failure, a negative error code is returned. | |
1da177e4 | 5841 | */ |
5add95d4 | 5842 | SYSCALL_DEFINE1(sched_get_priority_min, int, policy) |
1da177e4 LT |
5843 | { |
5844 | int ret = -EINVAL; | |
5845 | ||
5846 | switch (policy) { | |
5847 | case SCHED_FIFO: | |
5848 | case SCHED_RR: | |
5849 | ret = 1; | |
5850 | break; | |
aab03e05 | 5851 | case SCHED_DEADLINE: |
1da177e4 | 5852 | case SCHED_NORMAL: |
b0a9499c | 5853 | case SCHED_BATCH: |
dd41f596 | 5854 | case SCHED_IDLE: |
1da177e4 LT |
5855 | ret = 0; |
5856 | } | |
5857 | return ret; | |
5858 | } | |
5859 | ||
abca5fc5 | 5860 | static int sched_rr_get_interval(pid_t pid, struct timespec64 *t) |
1da177e4 | 5861 | { |
36c8b586 | 5862 | struct task_struct *p; |
a4ec24b4 | 5863 | unsigned int time_slice; |
eb580751 | 5864 | struct rq_flags rf; |
dba091b9 | 5865 | struct rq *rq; |
3a5c359a | 5866 | int retval; |
1da177e4 LT |
5867 | |
5868 | if (pid < 0) | |
3a5c359a | 5869 | return -EINVAL; |
1da177e4 LT |
5870 | |
5871 | retval = -ESRCH; | |
1a551ae7 | 5872 | rcu_read_lock(); |
1da177e4 LT |
5873 | p = find_process_by_pid(pid); |
5874 | if (!p) | |
5875 | goto out_unlock; | |
5876 | ||
5877 | retval = security_task_getscheduler(p); | |
5878 | if (retval) | |
5879 | goto out_unlock; | |
5880 | ||
eb580751 | 5881 | rq = task_rq_lock(p, &rf); |
a57beec5 PZ |
5882 | time_slice = 0; |
5883 | if (p->sched_class->get_rr_interval) | |
5884 | time_slice = p->sched_class->get_rr_interval(rq, p); | |
eb580751 | 5885 | task_rq_unlock(rq, p, &rf); |
a4ec24b4 | 5886 | |
1a551ae7 | 5887 | rcu_read_unlock(); |
abca5fc5 AV |
5888 | jiffies_to_timespec64(time_slice, t); |
5889 | return 0; | |
3a5c359a | 5890 | |
1da177e4 | 5891 | out_unlock: |
1a551ae7 | 5892 | rcu_read_unlock(); |
1da177e4 LT |
5893 | return retval; |
5894 | } | |
5895 | ||
2064a5ab RD |
5896 | /** |
5897 | * sys_sched_rr_get_interval - return the default timeslice of a process. | |
5898 | * @pid: pid of the process. | |
5899 | * @interval: userspace pointer to the timeslice value. | |
5900 | * | |
5901 | * this syscall writes the default timeslice value of a given process | |
5902 | * into the user-space timespec buffer. A value of '0' means infinity. | |
5903 | * | |
5904 | * Return: On success, 0 and the timeslice is in @interval. Otherwise, | |
5905 | * an error code. | |
5906 | */ | |
abca5fc5 | 5907 | SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid, |
474b9c77 | 5908 | struct __kernel_timespec __user *, interval) |
abca5fc5 AV |
5909 | { |
5910 | struct timespec64 t; | |
5911 | int retval = sched_rr_get_interval(pid, &t); | |
5912 | ||
5913 | if (retval == 0) | |
5914 | retval = put_timespec64(&t, interval); | |
5915 | ||
5916 | return retval; | |
5917 | } | |
5918 | ||
474b9c77 | 5919 | #ifdef CONFIG_COMPAT_32BIT_TIME |
8dabe724 AB |
5920 | SYSCALL_DEFINE2(sched_rr_get_interval_time32, pid_t, pid, |
5921 | struct old_timespec32 __user *, interval) | |
abca5fc5 AV |
5922 | { |
5923 | struct timespec64 t; | |
5924 | int retval = sched_rr_get_interval(pid, &t); | |
5925 | ||
5926 | if (retval == 0) | |
9afc5eee | 5927 | retval = put_old_timespec32(&t, interval); |
abca5fc5 AV |
5928 | return retval; |
5929 | } | |
5930 | #endif | |
5931 | ||
82a1fcb9 | 5932 | void sched_show_task(struct task_struct *p) |
1da177e4 | 5933 | { |
1da177e4 | 5934 | unsigned long free = 0; |
4e79752c | 5935 | int ppid; |
c930b2c0 | 5936 | |
38200502 TH |
5937 | if (!try_get_task_stack(p)) |
5938 | return; | |
20435d84 XX |
5939 | |
5940 | printk(KERN_INFO "%-15.15s %c", p->comm, task_state_to_char(p)); | |
5941 | ||
5942 | if (p->state == TASK_RUNNING) | |
3df0fc5b | 5943 | printk(KERN_CONT " running task "); |
1da177e4 | 5944 | #ifdef CONFIG_DEBUG_STACK_USAGE |
7c9f8861 | 5945 | free = stack_not_used(p); |
1da177e4 | 5946 | #endif |
a90e984c | 5947 | ppid = 0; |
4e79752c | 5948 | rcu_read_lock(); |
a90e984c ON |
5949 | if (pid_alive(p)) |
5950 | ppid = task_pid_nr(rcu_dereference(p->real_parent)); | |
4e79752c | 5951 | rcu_read_unlock(); |
3df0fc5b | 5952 | printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free, |
4e79752c | 5953 | task_pid_nr(p), ppid, |
aa47b7e0 | 5954 | (unsigned long)task_thread_info(p)->flags); |
1da177e4 | 5955 | |
3d1cb205 | 5956 | print_worker_info(KERN_INFO, p); |
5fb5e6de | 5957 | show_stack(p, NULL); |
38200502 | 5958 | put_task_stack(p); |
1da177e4 | 5959 | } |
0032f4e8 | 5960 | EXPORT_SYMBOL_GPL(sched_show_task); |
1da177e4 | 5961 | |
5d68cc95 PZ |
5962 | static inline bool |
5963 | state_filter_match(unsigned long state_filter, struct task_struct *p) | |
5964 | { | |
5965 | /* no filter, everything matches */ | |
5966 | if (!state_filter) | |
5967 | return true; | |
5968 | ||
5969 | /* filter, but doesn't match */ | |
5970 | if (!(p->state & state_filter)) | |
5971 | return false; | |
5972 | ||
5973 | /* | |
5974 | * When looking for TASK_UNINTERRUPTIBLE skip TASK_IDLE (allows | |
5975 | * TASK_KILLABLE). | |
5976 | */ | |
5977 | if (state_filter == TASK_UNINTERRUPTIBLE && p->state == TASK_IDLE) | |
5978 | return false; | |
5979 | ||
5980 | return true; | |
5981 | } | |
5982 | ||
5983 | ||
e59e2ae2 | 5984 | void show_state_filter(unsigned long state_filter) |
1da177e4 | 5985 | { |
36c8b586 | 5986 | struct task_struct *g, *p; |
1da177e4 | 5987 | |
4bd77321 | 5988 | #if BITS_PER_LONG == 32 |
3df0fc5b PZ |
5989 | printk(KERN_INFO |
5990 | " task PC stack pid father\n"); | |
1da177e4 | 5991 | #else |
3df0fc5b PZ |
5992 | printk(KERN_INFO |
5993 | " task PC stack pid father\n"); | |
1da177e4 | 5994 | #endif |
510f5acc | 5995 | rcu_read_lock(); |
5d07f420 | 5996 | for_each_process_thread(g, p) { |
1da177e4 LT |
5997 | /* |
5998 | * reset the NMI-timeout, listing all files on a slow | |
25985edc | 5999 | * console might take a lot of time: |
57675cb9 AR |
6000 | * Also, reset softlockup watchdogs on all CPUs, because |
6001 | * another CPU might be blocked waiting for us to process | |
6002 | * an IPI. | |
1da177e4 LT |
6003 | */ |
6004 | touch_nmi_watchdog(); | |
57675cb9 | 6005 | touch_all_softlockup_watchdogs(); |
5d68cc95 | 6006 | if (state_filter_match(state_filter, p)) |
82a1fcb9 | 6007 | sched_show_task(p); |
5d07f420 | 6008 | } |
1da177e4 | 6009 | |
dd41f596 | 6010 | #ifdef CONFIG_SCHED_DEBUG |
fb90a6e9 RV |
6011 | if (!state_filter) |
6012 | sysrq_sched_debug_show(); | |
dd41f596 | 6013 | #endif |
510f5acc | 6014 | rcu_read_unlock(); |
e59e2ae2 IM |
6015 | /* |
6016 | * Only show locks if all tasks are dumped: | |
6017 | */ | |
93335a21 | 6018 | if (!state_filter) |
e59e2ae2 | 6019 | debug_show_all_locks(); |
1da177e4 LT |
6020 | } |
6021 | ||
f340c0d1 IM |
6022 | /** |
6023 | * init_idle - set up an idle thread for a given CPU | |
6024 | * @idle: task in question | |
d1ccc66d | 6025 | * @cpu: CPU the idle task belongs to |
f340c0d1 IM |
6026 | * |
6027 | * NOTE: this function does not set the idle thread's NEED_RESCHED | |
6028 | * flag, to make booting more robust. | |
6029 | */ | |
0db0628d | 6030 | void init_idle(struct task_struct *idle, int cpu) |
1da177e4 | 6031 | { |
70b97a7f | 6032 | struct rq *rq = cpu_rq(cpu); |
1da177e4 LT |
6033 | unsigned long flags; |
6034 | ||
ff51ff84 PZ |
6035 | __sched_fork(0, idle); |
6036 | ||
25834c73 PZ |
6037 | raw_spin_lock_irqsave(&idle->pi_lock, flags); |
6038 | raw_spin_lock(&rq->lock); | |
5cbd54ef | 6039 | |
06b83b5f | 6040 | idle->state = TASK_RUNNING; |
dd41f596 | 6041 | idle->se.exec_start = sched_clock(); |
c1de45ca | 6042 | idle->flags |= PF_IDLE; |
dd41f596 | 6043 | |
d08b9f0c | 6044 | scs_task_reset(idle); |
e1b77c92 MR |
6045 | kasan_unpoison_task_stack(idle); |
6046 | ||
de9b8f5d PZ |
6047 | #ifdef CONFIG_SMP |
6048 | /* | |
6049 | * Its possible that init_idle() gets called multiple times on a task, | |
6050 | * in that case do_set_cpus_allowed() will not do the right thing. | |
6051 | * | |
6052 | * And since this is boot we can forgo the serialization. | |
6053 | */ | |
6054 | set_cpus_allowed_common(idle, cpumask_of(cpu)); | |
6055 | #endif | |
6506cf6c PZ |
6056 | /* |
6057 | * We're having a chicken and egg problem, even though we are | |
d1ccc66d | 6058 | * holding rq->lock, the CPU isn't yet set to this CPU so the |
6506cf6c PZ |
6059 | * lockdep check in task_group() will fail. |
6060 | * | |
6061 | * Similar case to sched_fork(). / Alternatively we could | |
6062 | * use task_rq_lock() here and obtain the other rq->lock. | |
6063 | * | |
6064 | * Silence PROVE_RCU | |
6065 | */ | |
6066 | rcu_read_lock(); | |
dd41f596 | 6067 | __set_task_cpu(idle, cpu); |
6506cf6c | 6068 | rcu_read_unlock(); |
1da177e4 | 6069 | |
5311a98f EB |
6070 | rq->idle = idle; |
6071 | rcu_assign_pointer(rq->curr, idle); | |
da0c1e65 | 6072 | idle->on_rq = TASK_ON_RQ_QUEUED; |
de9b8f5d | 6073 | #ifdef CONFIG_SMP |
3ca7a440 | 6074 | idle->on_cpu = 1; |
4866cde0 | 6075 | #endif |
25834c73 PZ |
6076 | raw_spin_unlock(&rq->lock); |
6077 | raw_spin_unlock_irqrestore(&idle->pi_lock, flags); | |
1da177e4 LT |
6078 | |
6079 | /* Set the preempt count _outside_ the spinlocks! */ | |
01028747 | 6080 | init_idle_preempt_count(idle, cpu); |
55cd5340 | 6081 | |
dd41f596 IM |
6082 | /* |
6083 | * The idle tasks have their own, simple scheduling class: | |
6084 | */ | |
6085 | idle->sched_class = &idle_sched_class; | |
868baf07 | 6086 | ftrace_graph_init_idle_task(idle, cpu); |
45eacc69 | 6087 | vtime_init_idle(idle, cpu); |
de9b8f5d | 6088 | #ifdef CONFIG_SMP |
f1c6f1a7 CE |
6089 | sprintf(idle->comm, "%s/%d", INIT_TASK_COMM, cpu); |
6090 | #endif | |
19978ca6 IM |
6091 | } |
6092 | ||
e1d4eeec NP |
6093 | #ifdef CONFIG_SMP |
6094 | ||
f82f8042 JL |
6095 | int cpuset_cpumask_can_shrink(const struct cpumask *cur, |
6096 | const struct cpumask *trial) | |
6097 | { | |
06a76fe0 | 6098 | int ret = 1; |
f82f8042 | 6099 | |
bb2bc55a MG |
6100 | if (!cpumask_weight(cur)) |
6101 | return ret; | |
6102 | ||
06a76fe0 | 6103 | ret = dl_cpuset_cpumask_can_shrink(cur, trial); |
f82f8042 JL |
6104 | |
6105 | return ret; | |
6106 | } | |
6107 | ||
7f51412a JL |
6108 | int task_can_attach(struct task_struct *p, |
6109 | const struct cpumask *cs_cpus_allowed) | |
6110 | { | |
6111 | int ret = 0; | |
6112 | ||
6113 | /* | |
6114 | * Kthreads which disallow setaffinity shouldn't be moved | |
d1ccc66d | 6115 | * to a new cpuset; we don't want to change their CPU |
7f51412a JL |
6116 | * affinity and isolating such threads by their set of |
6117 | * allowed nodes is unnecessary. Thus, cpusets are not | |
6118 | * applicable for such threads. This prevents checking for | |
6119 | * success of set_cpus_allowed_ptr() on all attached tasks | |
3bd37062 | 6120 | * before cpus_mask may be changed. |
7f51412a JL |
6121 | */ |
6122 | if (p->flags & PF_NO_SETAFFINITY) { | |
6123 | ret = -EINVAL; | |
6124 | goto out; | |
6125 | } | |
6126 | ||
7f51412a | 6127 | if (dl_task(p) && !cpumask_intersects(task_rq(p)->rd->span, |
06a76fe0 NP |
6128 | cs_cpus_allowed)) |
6129 | ret = dl_task_can_attach(p, cs_cpus_allowed); | |
7f51412a | 6130 | |
7f51412a JL |
6131 | out: |
6132 | return ret; | |
6133 | } | |
6134 | ||
f2cb1360 | 6135 | bool sched_smp_initialized __read_mostly; |
e26fbffd | 6136 | |
e6628d5b MG |
6137 | #ifdef CONFIG_NUMA_BALANCING |
6138 | /* Migrate current task p to target_cpu */ | |
6139 | int migrate_task_to(struct task_struct *p, int target_cpu) | |
6140 | { | |
6141 | struct migration_arg arg = { p, target_cpu }; | |
6142 | int curr_cpu = task_cpu(p); | |
6143 | ||
6144 | if (curr_cpu == target_cpu) | |
6145 | return 0; | |
6146 | ||
3bd37062 | 6147 | if (!cpumask_test_cpu(target_cpu, p->cpus_ptr)) |
e6628d5b MG |
6148 | return -EINVAL; |
6149 | ||
6150 | /* TODO: This is not properly updating schedstats */ | |
6151 | ||
286549dc | 6152 | trace_sched_move_numa(p, curr_cpu, target_cpu); |
e6628d5b MG |
6153 | return stop_one_cpu(curr_cpu, migration_cpu_stop, &arg); |
6154 | } | |
0ec8aa00 PZ |
6155 | |
6156 | /* | |
6157 | * Requeue a task on a given node and accurately track the number of NUMA | |
6158 | * tasks on the runqueues | |
6159 | */ | |
6160 | void sched_setnuma(struct task_struct *p, int nid) | |
6161 | { | |
da0c1e65 | 6162 | bool queued, running; |
eb580751 PZ |
6163 | struct rq_flags rf; |
6164 | struct rq *rq; | |
0ec8aa00 | 6165 | |
eb580751 | 6166 | rq = task_rq_lock(p, &rf); |
da0c1e65 | 6167 | queued = task_on_rq_queued(p); |
0ec8aa00 PZ |
6168 | running = task_current(rq, p); |
6169 | ||
da0c1e65 | 6170 | if (queued) |
1de64443 | 6171 | dequeue_task(rq, p, DEQUEUE_SAVE); |
0ec8aa00 | 6172 | if (running) |
f3cd1c4e | 6173 | put_prev_task(rq, p); |
0ec8aa00 PZ |
6174 | |
6175 | p->numa_preferred_nid = nid; | |
0ec8aa00 | 6176 | |
da0c1e65 | 6177 | if (queued) |
7134b3e9 | 6178 | enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK); |
a399d233 | 6179 | if (running) |
03b7fad1 | 6180 | set_next_task(rq, p); |
eb580751 | 6181 | task_rq_unlock(rq, p, &rf); |
0ec8aa00 | 6182 | } |
5cc389bc | 6183 | #endif /* CONFIG_NUMA_BALANCING */ |
f7b4cddc | 6184 | |
1da177e4 | 6185 | #ifdef CONFIG_HOTPLUG_CPU |
054b9108 | 6186 | /* |
d1ccc66d | 6187 | * Ensure that the idle task is using init_mm right before its CPU goes |
48c5ccae | 6188 | * offline. |
054b9108 | 6189 | */ |
48c5ccae | 6190 | void idle_task_exit(void) |
1da177e4 | 6191 | { |
48c5ccae | 6192 | struct mm_struct *mm = current->active_mm; |
e76bd8d9 | 6193 | |
48c5ccae | 6194 | BUG_ON(cpu_online(smp_processor_id())); |
e76bd8d9 | 6195 | |
a53efe5f | 6196 | if (mm != &init_mm) { |
252d2a41 | 6197 | switch_mm(mm, &init_mm, current); |
3eda69c9 | 6198 | current->active_mm = &init_mm; |
a53efe5f MS |
6199 | finish_arch_post_lock_switch(); |
6200 | } | |
48c5ccae | 6201 | mmdrop(mm); |
1da177e4 LT |
6202 | } |
6203 | ||
6204 | /* | |
5d180232 PZ |
6205 | * Since this CPU is going 'away' for a while, fold any nr_active delta |
6206 | * we might have. Assumes we're called after migrate_tasks() so that the | |
d60585c5 TG |
6207 | * nr_active count is stable. We need to take the teardown thread which |
6208 | * is calling this into account, so we hand in adjust = 1 to the load | |
6209 | * calculation. | |
5d180232 PZ |
6210 | * |
6211 | * Also see the comment "Global load-average calculations". | |
1da177e4 | 6212 | */ |
5d180232 | 6213 | static void calc_load_migrate(struct rq *rq) |
1da177e4 | 6214 | { |
d60585c5 | 6215 | long delta = calc_load_fold_active(rq, 1); |
5d180232 PZ |
6216 | if (delta) |
6217 | atomic_long_add(delta, &calc_load_tasks); | |
1da177e4 LT |
6218 | } |
6219 | ||
10e7071b | 6220 | static struct task_struct *__pick_migrate_task(struct rq *rq) |
3f1d2a31 | 6221 | { |
10e7071b PZ |
6222 | const struct sched_class *class; |
6223 | struct task_struct *next; | |
3f1d2a31 | 6224 | |
10e7071b | 6225 | for_each_class(class) { |
98c2f700 | 6226 | next = class->pick_next_task(rq); |
10e7071b | 6227 | if (next) { |
6e2df058 | 6228 | next->sched_class->put_prev_task(rq, next); |
10e7071b PZ |
6229 | return next; |
6230 | } | |
6231 | } | |
3f1d2a31 | 6232 | |
10e7071b PZ |
6233 | /* The idle class should always have a runnable task */ |
6234 | BUG(); | |
6235 | } | |
3f1d2a31 | 6236 | |
48f24c4d | 6237 | /* |
48c5ccae PZ |
6238 | * Migrate all tasks from the rq, sleeping tasks will be migrated by |
6239 | * try_to_wake_up()->select_task_rq(). | |
6240 | * | |
6241 | * Called with rq->lock held even though we'er in stop_machine() and | |
6242 | * there's no concurrency possible, we hold the required locks anyway | |
6243 | * because of lock validation efforts. | |
1da177e4 | 6244 | */ |
8a8c69c3 | 6245 | static void migrate_tasks(struct rq *dead_rq, struct rq_flags *rf) |
1da177e4 | 6246 | { |
5e16bbc2 | 6247 | struct rq *rq = dead_rq; |
48c5ccae | 6248 | struct task_struct *next, *stop = rq->stop; |
8a8c69c3 | 6249 | struct rq_flags orf = *rf; |
48c5ccae | 6250 | int dest_cpu; |
1da177e4 LT |
6251 | |
6252 | /* | |
48c5ccae PZ |
6253 | * Fudge the rq selection such that the below task selection loop |
6254 | * doesn't get stuck on the currently eligible stop task. | |
6255 | * | |
6256 | * We're currently inside stop_machine() and the rq is either stuck | |
6257 | * in the stop_machine_cpu_stop() loop, or we're executing this code, | |
6258 | * either way we should never end up calling schedule() until we're | |
6259 | * done here. | |
1da177e4 | 6260 | */ |
48c5ccae | 6261 | rq->stop = NULL; |
48f24c4d | 6262 | |
77bd3970 FW |
6263 | /* |
6264 | * put_prev_task() and pick_next_task() sched | |
6265 | * class method both need to have an up-to-date | |
6266 | * value of rq->clock[_task] | |
6267 | */ | |
6268 | update_rq_clock(rq); | |
6269 | ||
5e16bbc2 | 6270 | for (;;) { |
48c5ccae PZ |
6271 | /* |
6272 | * There's this thread running, bail when that's the only | |
d1ccc66d | 6273 | * remaining thread: |
48c5ccae PZ |
6274 | */ |
6275 | if (rq->nr_running == 1) | |
dd41f596 | 6276 | break; |
48c5ccae | 6277 | |
10e7071b | 6278 | next = __pick_migrate_task(rq); |
e692ab53 | 6279 | |
5473e0cc | 6280 | /* |
3bd37062 | 6281 | * Rules for changing task_struct::cpus_mask are holding |
5473e0cc WL |
6282 | * both pi_lock and rq->lock, such that holding either |
6283 | * stabilizes the mask. | |
6284 | * | |
6285 | * Drop rq->lock is not quite as disastrous as it usually is | |
6286 | * because !cpu_active at this point, which means load-balance | |
6287 | * will not interfere. Also, stop-machine. | |
6288 | */ | |
8a8c69c3 | 6289 | rq_unlock(rq, rf); |
5473e0cc | 6290 | raw_spin_lock(&next->pi_lock); |
8a8c69c3 | 6291 | rq_relock(rq, rf); |
5473e0cc WL |
6292 | |
6293 | /* | |
6294 | * Since we're inside stop-machine, _nothing_ should have | |
6295 | * changed the task, WARN if weird stuff happened, because in | |
6296 | * that case the above rq->lock drop is a fail too. | |
6297 | */ | |
6298 | if (WARN_ON(task_rq(next) != rq || !task_on_rq_queued(next))) { | |
6299 | raw_spin_unlock(&next->pi_lock); | |
6300 | continue; | |
6301 | } | |
6302 | ||
48c5ccae | 6303 | /* Find suitable destination for @next, with force if needed. */ |
5e16bbc2 | 6304 | dest_cpu = select_fallback_rq(dead_rq->cpu, next); |
8a8c69c3 | 6305 | rq = __migrate_task(rq, rf, next, dest_cpu); |
5e16bbc2 | 6306 | if (rq != dead_rq) { |
8a8c69c3 | 6307 | rq_unlock(rq, rf); |
5e16bbc2 | 6308 | rq = dead_rq; |
8a8c69c3 PZ |
6309 | *rf = orf; |
6310 | rq_relock(rq, rf); | |
5e16bbc2 | 6311 | } |
5473e0cc | 6312 | raw_spin_unlock(&next->pi_lock); |
1da177e4 | 6313 | } |
dce48a84 | 6314 | |
48c5ccae | 6315 | rq->stop = stop; |
dce48a84 | 6316 | } |
1da177e4 LT |
6317 | #endif /* CONFIG_HOTPLUG_CPU */ |
6318 | ||
f2cb1360 | 6319 | void set_rq_online(struct rq *rq) |
1f11eb6a GH |
6320 | { |
6321 | if (!rq->online) { | |
6322 | const struct sched_class *class; | |
6323 | ||
c6c4927b | 6324 | cpumask_set_cpu(rq->cpu, rq->rd->online); |
1f11eb6a GH |
6325 | rq->online = 1; |
6326 | ||
6327 | for_each_class(class) { | |
6328 | if (class->rq_online) | |
6329 | class->rq_online(rq); | |
6330 | } | |
6331 | } | |
6332 | } | |
6333 | ||
f2cb1360 | 6334 | void set_rq_offline(struct rq *rq) |
1f11eb6a GH |
6335 | { |
6336 | if (rq->online) { | |
6337 | const struct sched_class *class; | |
6338 | ||
6339 | for_each_class(class) { | |
6340 | if (class->rq_offline) | |
6341 | class->rq_offline(rq); | |
6342 | } | |
6343 | ||
c6c4927b | 6344 | cpumask_clear_cpu(rq->cpu, rq->rd->online); |
1f11eb6a GH |
6345 | rq->online = 0; |
6346 | } | |
6347 | } | |
6348 | ||
d1ccc66d IM |
6349 | /* |
6350 | * used to mark begin/end of suspend/resume: | |
6351 | */ | |
6352 | static int num_cpus_frozen; | |
d35be8ba | 6353 | |
1da177e4 | 6354 | /* |
3a101d05 TH |
6355 | * Update cpusets according to cpu_active mask. If cpusets are |
6356 | * disabled, cpuset_update_active_cpus() becomes a simple wrapper | |
6357 | * around partition_sched_domains(). | |
d35be8ba SB |
6358 | * |
6359 | * If we come here as part of a suspend/resume, don't touch cpusets because we | |
6360 | * want to restore it back to its original state upon resume anyway. | |
1da177e4 | 6361 | */ |
40190a78 | 6362 | static void cpuset_cpu_active(void) |
e761b772 | 6363 | { |
40190a78 | 6364 | if (cpuhp_tasks_frozen) { |
d35be8ba SB |
6365 | /* |
6366 | * num_cpus_frozen tracks how many CPUs are involved in suspend | |
6367 | * resume sequence. As long as this is not the last online | |
6368 | * operation in the resume sequence, just build a single sched | |
6369 | * domain, ignoring cpusets. | |
6370 | */ | |
50e76632 PZ |
6371 | partition_sched_domains(1, NULL, NULL); |
6372 | if (--num_cpus_frozen) | |
135fb3e1 | 6373 | return; |
d35be8ba SB |
6374 | /* |
6375 | * This is the last CPU online operation. So fall through and | |
6376 | * restore the original sched domains by considering the | |
6377 | * cpuset configurations. | |
6378 | */ | |
50e76632 | 6379 | cpuset_force_rebuild(); |
3a101d05 | 6380 | } |
30e03acd | 6381 | cpuset_update_active_cpus(); |
3a101d05 | 6382 | } |
e761b772 | 6383 | |
40190a78 | 6384 | static int cpuset_cpu_inactive(unsigned int cpu) |
3a101d05 | 6385 | { |
40190a78 | 6386 | if (!cpuhp_tasks_frozen) { |
06a76fe0 | 6387 | if (dl_cpu_busy(cpu)) |
135fb3e1 | 6388 | return -EBUSY; |
30e03acd | 6389 | cpuset_update_active_cpus(); |
135fb3e1 | 6390 | } else { |
d35be8ba SB |
6391 | num_cpus_frozen++; |
6392 | partition_sched_domains(1, NULL, NULL); | |
e761b772 | 6393 | } |
135fb3e1 | 6394 | return 0; |
e761b772 | 6395 | } |
e761b772 | 6396 | |
40190a78 | 6397 | int sched_cpu_activate(unsigned int cpu) |
135fb3e1 | 6398 | { |
7d976699 | 6399 | struct rq *rq = cpu_rq(cpu); |
8a8c69c3 | 6400 | struct rq_flags rf; |
7d976699 | 6401 | |
ba2591a5 PZ |
6402 | #ifdef CONFIG_SCHED_SMT |
6403 | /* | |
c5511d03 | 6404 | * When going up, increment the number of cores with SMT present. |
ba2591a5 | 6405 | */ |
c5511d03 PZI |
6406 | if (cpumask_weight(cpu_smt_mask(cpu)) == 2) |
6407 | static_branch_inc_cpuslocked(&sched_smt_present); | |
ba2591a5 | 6408 | #endif |
40190a78 | 6409 | set_cpu_active(cpu, true); |
135fb3e1 | 6410 | |
40190a78 | 6411 | if (sched_smp_initialized) { |
135fb3e1 | 6412 | sched_domains_numa_masks_set(cpu); |
40190a78 | 6413 | cpuset_cpu_active(); |
e761b772 | 6414 | } |
7d976699 TG |
6415 | |
6416 | /* | |
6417 | * Put the rq online, if not already. This happens: | |
6418 | * | |
6419 | * 1) In the early boot process, because we build the real domains | |
d1ccc66d | 6420 | * after all CPUs have been brought up. |
7d976699 TG |
6421 | * |
6422 | * 2) At runtime, if cpuset_cpu_active() fails to rebuild the | |
6423 | * domains. | |
6424 | */ | |
8a8c69c3 | 6425 | rq_lock_irqsave(rq, &rf); |
7d976699 TG |
6426 | if (rq->rd) { |
6427 | BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); | |
6428 | set_rq_online(rq); | |
6429 | } | |
8a8c69c3 | 6430 | rq_unlock_irqrestore(rq, &rf); |
7d976699 | 6431 | |
40190a78 | 6432 | return 0; |
135fb3e1 TG |
6433 | } |
6434 | ||
40190a78 | 6435 | int sched_cpu_deactivate(unsigned int cpu) |
135fb3e1 | 6436 | { |
135fb3e1 TG |
6437 | int ret; |
6438 | ||
40190a78 | 6439 | set_cpu_active(cpu, false); |
b2454caa PZ |
6440 | /* |
6441 | * We've cleared cpu_active_mask, wait for all preempt-disabled and RCU | |
6442 | * users of this state to go away such that all new such users will | |
6443 | * observe it. | |
6444 | * | |
b2454caa PZ |
6445 | * Do sync before park smpboot threads to take care the rcu boost case. |
6446 | */ | |
309ba859 | 6447 | synchronize_rcu(); |
40190a78 | 6448 | |
c5511d03 PZI |
6449 | #ifdef CONFIG_SCHED_SMT |
6450 | /* | |
6451 | * When going down, decrement the number of cores with SMT present. | |
6452 | */ | |
6453 | if (cpumask_weight(cpu_smt_mask(cpu)) == 2) | |
6454 | static_branch_dec_cpuslocked(&sched_smt_present); | |
6455 | #endif | |
6456 | ||
40190a78 TG |
6457 | if (!sched_smp_initialized) |
6458 | return 0; | |
6459 | ||
6460 | ret = cpuset_cpu_inactive(cpu); | |
6461 | if (ret) { | |
6462 | set_cpu_active(cpu, true); | |
6463 | return ret; | |
135fb3e1 | 6464 | } |
40190a78 TG |
6465 | sched_domains_numa_masks_clear(cpu); |
6466 | return 0; | |
135fb3e1 TG |
6467 | } |
6468 | ||
94baf7a5 TG |
6469 | static void sched_rq_cpu_starting(unsigned int cpu) |
6470 | { | |
6471 | struct rq *rq = cpu_rq(cpu); | |
6472 | ||
6473 | rq->calc_load_update = calc_load_update; | |
94baf7a5 TG |
6474 | update_max_interval(); |
6475 | } | |
6476 | ||
135fb3e1 TG |
6477 | int sched_cpu_starting(unsigned int cpu) |
6478 | { | |
94baf7a5 | 6479 | sched_rq_cpu_starting(cpu); |
d84b3131 | 6480 | sched_tick_start(cpu); |
135fb3e1 | 6481 | return 0; |
e761b772 | 6482 | } |
e761b772 | 6483 | |
f2785ddb TG |
6484 | #ifdef CONFIG_HOTPLUG_CPU |
6485 | int sched_cpu_dying(unsigned int cpu) | |
6486 | { | |
6487 | struct rq *rq = cpu_rq(cpu); | |
8a8c69c3 | 6488 | struct rq_flags rf; |
f2785ddb TG |
6489 | |
6490 | /* Handle pending wakeups and then migrate everything off */ | |
6491 | sched_ttwu_pending(); | |
d84b3131 | 6492 | sched_tick_stop(cpu); |
8a8c69c3 PZ |
6493 | |
6494 | rq_lock_irqsave(rq, &rf); | |
f2785ddb TG |
6495 | if (rq->rd) { |
6496 | BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span)); | |
6497 | set_rq_offline(rq); | |
6498 | } | |
8a8c69c3 | 6499 | migrate_tasks(rq, &rf); |
f2785ddb | 6500 | BUG_ON(rq->nr_running != 1); |
8a8c69c3 PZ |
6501 | rq_unlock_irqrestore(rq, &rf); |
6502 | ||
f2785ddb TG |
6503 | calc_load_migrate(rq); |
6504 | update_max_interval(); | |
00357f5e | 6505 | nohz_balance_exit_idle(rq); |
e5ef27d0 | 6506 | hrtick_clear(rq); |
f2785ddb TG |
6507 | return 0; |
6508 | } | |
6509 | #endif | |
6510 | ||
1da177e4 LT |
6511 | void __init sched_init_smp(void) |
6512 | { | |
cb83b629 PZ |
6513 | sched_init_numa(); |
6514 | ||
6acce3ef PZ |
6515 | /* |
6516 | * There's no userspace yet to cause hotplug operations; hence all the | |
d1ccc66d | 6517 | * CPU masks are stable and all blatant races in the below code cannot |
b5a4e2bb | 6518 | * happen. |
6acce3ef | 6519 | */ |
712555ee | 6520 | mutex_lock(&sched_domains_mutex); |
8d5dc512 | 6521 | sched_init_domains(cpu_active_mask); |
712555ee | 6522 | mutex_unlock(&sched_domains_mutex); |
e761b772 | 6523 | |
5c1e1767 | 6524 | /* Move init over to a non-isolated CPU */ |
edb93821 | 6525 | if (set_cpus_allowed_ptr(current, housekeeping_cpumask(HK_FLAG_DOMAIN)) < 0) |
5c1e1767 | 6526 | BUG(); |
19978ca6 | 6527 | sched_init_granularity(); |
4212823f | 6528 | |
0e3900e6 | 6529 | init_sched_rt_class(); |
1baca4ce | 6530 | init_sched_dl_class(); |
1b568f0a | 6531 | |
e26fbffd | 6532 | sched_smp_initialized = true; |
1da177e4 | 6533 | } |
e26fbffd TG |
6534 | |
6535 | static int __init migration_init(void) | |
6536 | { | |
77a5352b | 6537 | sched_cpu_starting(smp_processor_id()); |
e26fbffd | 6538 | return 0; |
1da177e4 | 6539 | } |
e26fbffd TG |
6540 | early_initcall(migration_init); |
6541 | ||
1da177e4 LT |
6542 | #else |
6543 | void __init sched_init_smp(void) | |
6544 | { | |
19978ca6 | 6545 | sched_init_granularity(); |
1da177e4 LT |
6546 | } |
6547 | #endif /* CONFIG_SMP */ | |
6548 | ||
6549 | int in_sched_functions(unsigned long addr) | |
6550 | { | |
1da177e4 LT |
6551 | return in_lock_functions(addr) || |
6552 | (addr >= (unsigned long)__sched_text_start | |
6553 | && addr < (unsigned long)__sched_text_end); | |
6554 | } | |
6555 | ||
029632fb | 6556 | #ifdef CONFIG_CGROUP_SCHED |
27b4b931 LZ |
6557 | /* |
6558 | * Default task group. | |
6559 | * Every task in system belongs to this group at bootup. | |
6560 | */ | |
029632fb | 6561 | struct task_group root_task_group; |
35cf4e50 | 6562 | LIST_HEAD(task_groups); |
b0367629 WL |
6563 | |
6564 | /* Cacheline aligned slab cache for task_group */ | |
6565 | static struct kmem_cache *task_group_cache __read_mostly; | |
052f1dc7 | 6566 | #endif |
6f505b16 | 6567 | |
e6252c3e | 6568 | DECLARE_PER_CPU(cpumask_var_t, load_balance_mask); |
10e2f1ac | 6569 | DECLARE_PER_CPU(cpumask_var_t, select_idle_mask); |
6f505b16 | 6570 | |
1da177e4 LT |
6571 | void __init sched_init(void) |
6572 | { | |
a1dc0446 | 6573 | unsigned long ptr = 0; |
55627e3c | 6574 | int i; |
434d53b0 | 6575 | |
5822a454 | 6576 | wait_bit_init(); |
9dcb8b68 | 6577 | |
434d53b0 | 6578 | #ifdef CONFIG_FAIR_GROUP_SCHED |
a1dc0446 | 6579 | ptr += 2 * nr_cpu_ids * sizeof(void **); |
434d53b0 MT |
6580 | #endif |
6581 | #ifdef CONFIG_RT_GROUP_SCHED | |
a1dc0446 | 6582 | ptr += 2 * nr_cpu_ids * sizeof(void **); |
434d53b0 | 6583 | #endif |
a1dc0446 QC |
6584 | if (ptr) { |
6585 | ptr = (unsigned long)kzalloc(ptr, GFP_NOWAIT); | |
434d53b0 MT |
6586 | |
6587 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
07e06b01 | 6588 | root_task_group.se = (struct sched_entity **)ptr; |
434d53b0 MT |
6589 | ptr += nr_cpu_ids * sizeof(void **); |
6590 | ||
07e06b01 | 6591 | root_task_group.cfs_rq = (struct cfs_rq **)ptr; |
434d53b0 | 6592 | ptr += nr_cpu_ids * sizeof(void **); |
eff766a6 | 6593 | |
6d6bc0ad | 6594 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
434d53b0 | 6595 | #ifdef CONFIG_RT_GROUP_SCHED |
07e06b01 | 6596 | root_task_group.rt_se = (struct sched_rt_entity **)ptr; |
434d53b0 MT |
6597 | ptr += nr_cpu_ids * sizeof(void **); |
6598 | ||
07e06b01 | 6599 | root_task_group.rt_rq = (struct rt_rq **)ptr; |
eff766a6 PZ |
6600 | ptr += nr_cpu_ids * sizeof(void **); |
6601 | ||
6d6bc0ad | 6602 | #endif /* CONFIG_RT_GROUP_SCHED */ |
b74e6278 | 6603 | } |
df7c8e84 | 6604 | #ifdef CONFIG_CPUMASK_OFFSTACK |
b74e6278 AT |
6605 | for_each_possible_cpu(i) { |
6606 | per_cpu(load_balance_mask, i) = (cpumask_var_t)kzalloc_node( | |
6607 | cpumask_size(), GFP_KERNEL, cpu_to_node(i)); | |
10e2f1ac PZ |
6608 | per_cpu(select_idle_mask, i) = (cpumask_var_t)kzalloc_node( |
6609 | cpumask_size(), GFP_KERNEL, cpu_to_node(i)); | |
434d53b0 | 6610 | } |
b74e6278 | 6611 | #endif /* CONFIG_CPUMASK_OFFSTACK */ |
dd41f596 | 6612 | |
d1ccc66d IM |
6613 | init_rt_bandwidth(&def_rt_bandwidth, global_rt_period(), global_rt_runtime()); |
6614 | init_dl_bandwidth(&def_dl_bandwidth, global_rt_period(), global_rt_runtime()); | |
332ac17e | 6615 | |
57d885fe GH |
6616 | #ifdef CONFIG_SMP |
6617 | init_defrootdomain(); | |
6618 | #endif | |
6619 | ||
d0b27fa7 | 6620 | #ifdef CONFIG_RT_GROUP_SCHED |
07e06b01 | 6621 | init_rt_bandwidth(&root_task_group.rt_bandwidth, |
d0b27fa7 | 6622 | global_rt_period(), global_rt_runtime()); |
6d6bc0ad | 6623 | #endif /* CONFIG_RT_GROUP_SCHED */ |
d0b27fa7 | 6624 | |
7c941438 | 6625 | #ifdef CONFIG_CGROUP_SCHED |
b0367629 WL |
6626 | task_group_cache = KMEM_CACHE(task_group, 0); |
6627 | ||
07e06b01 YZ |
6628 | list_add(&root_task_group.list, &task_groups); |
6629 | INIT_LIST_HEAD(&root_task_group.children); | |
f4d6f6c2 | 6630 | INIT_LIST_HEAD(&root_task_group.siblings); |
5091faa4 | 6631 | autogroup_init(&init_task); |
7c941438 | 6632 | #endif /* CONFIG_CGROUP_SCHED */ |
6f505b16 | 6633 | |
0a945022 | 6634 | for_each_possible_cpu(i) { |
70b97a7f | 6635 | struct rq *rq; |
1da177e4 LT |
6636 | |
6637 | rq = cpu_rq(i); | |
05fa785c | 6638 | raw_spin_lock_init(&rq->lock); |
7897986b | 6639 | rq->nr_running = 0; |
dce48a84 TG |
6640 | rq->calc_load_active = 0; |
6641 | rq->calc_load_update = jiffies + LOAD_FREQ; | |
acb5a9ba | 6642 | init_cfs_rq(&rq->cfs); |
07c54f7a AV |
6643 | init_rt_rq(&rq->rt); |
6644 | init_dl_rq(&rq->dl); | |
dd41f596 | 6645 | #ifdef CONFIG_FAIR_GROUP_SCHED |
029632fb | 6646 | root_task_group.shares = ROOT_TASK_GROUP_LOAD; |
6f505b16 | 6647 | INIT_LIST_HEAD(&rq->leaf_cfs_rq_list); |
9c2791f9 | 6648 | rq->tmp_alone_branch = &rq->leaf_cfs_rq_list; |
354d60c2 | 6649 | /* |
d1ccc66d | 6650 | * How much CPU bandwidth does root_task_group get? |
354d60c2 DG |
6651 | * |
6652 | * In case of task-groups formed thr' the cgroup filesystem, it | |
d1ccc66d IM |
6653 | * gets 100% of the CPU resources in the system. This overall |
6654 | * system CPU resource is divided among the tasks of | |
07e06b01 | 6655 | * root_task_group and its child task-groups in a fair manner, |
354d60c2 DG |
6656 | * based on each entity's (task or task-group's) weight |
6657 | * (se->load.weight). | |
6658 | * | |
07e06b01 | 6659 | * In other words, if root_task_group has 10 tasks of weight |
354d60c2 | 6660 | * 1024) and two child groups A0 and A1 (of weight 1024 each), |
d1ccc66d | 6661 | * then A0's share of the CPU resource is: |
354d60c2 | 6662 | * |
0d905bca | 6663 | * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33% |
354d60c2 | 6664 | * |
07e06b01 YZ |
6665 | * We achieve this by letting root_task_group's tasks sit |
6666 | * directly in rq->cfs (i.e root_task_group->se[] = NULL). | |
354d60c2 | 6667 | */ |
ab84d31e | 6668 | init_cfs_bandwidth(&root_task_group.cfs_bandwidth); |
07e06b01 | 6669 | init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL); |
354d60c2 DG |
6670 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
6671 | ||
6672 | rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime; | |
052f1dc7 | 6673 | #ifdef CONFIG_RT_GROUP_SCHED |
07e06b01 | 6674 | init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL); |
dd41f596 | 6675 | #endif |
1da177e4 | 6676 | #ifdef CONFIG_SMP |
41c7ce9a | 6677 | rq->sd = NULL; |
57d885fe | 6678 | rq->rd = NULL; |
ca6d75e6 | 6679 | rq->cpu_capacity = rq->cpu_capacity_orig = SCHED_CAPACITY_SCALE; |
e3fca9e7 | 6680 | rq->balance_callback = NULL; |
1da177e4 | 6681 | rq->active_balance = 0; |
dd41f596 | 6682 | rq->next_balance = jiffies; |
1da177e4 | 6683 | rq->push_cpu = 0; |
0a2966b4 | 6684 | rq->cpu = i; |
1f11eb6a | 6685 | rq->online = 0; |
eae0c9df MG |
6686 | rq->idle_stamp = 0; |
6687 | rq->avg_idle = 2*sysctl_sched_migration_cost; | |
9bd721c5 | 6688 | rq->max_idle_balance_cost = sysctl_sched_migration_cost; |
367456c7 PZ |
6689 | |
6690 | INIT_LIST_HEAD(&rq->cfs_tasks); | |
6691 | ||
dc938520 | 6692 | rq_attach_root(rq, &def_root_domain); |
3451d024 | 6693 | #ifdef CONFIG_NO_HZ_COMMON |
e022e0d3 | 6694 | rq->last_blocked_load_update_tick = jiffies; |
a22e47a4 | 6695 | atomic_set(&rq->nohz_flags, 0); |
83cd4fe2 | 6696 | #endif |
9fd81dd5 | 6697 | #endif /* CONFIG_SMP */ |
77a021be | 6698 | hrtick_rq_init(rq); |
1da177e4 | 6699 | atomic_set(&rq->nr_iowait, 0); |
1da177e4 LT |
6700 | } |
6701 | ||
9059393e | 6702 | set_load_weight(&init_task, false); |
b50f60ce | 6703 | |
1da177e4 LT |
6704 | /* |
6705 | * The boot idle thread does lazy MMU switching as well: | |
6706 | */ | |
f1f10076 | 6707 | mmgrab(&init_mm); |
1da177e4 LT |
6708 | enter_lazy_tlb(&init_mm, current); |
6709 | ||
6710 | /* | |
6711 | * Make us the idle thread. Technically, schedule() should not be | |
6712 | * called from this thread, however somewhere below it might be, | |
6713 | * but because we are the idle thread, we just pick up running again | |
6714 | * when this runqueue becomes "idle". | |
6715 | */ | |
6716 | init_idle(current, smp_processor_id()); | |
dce48a84 TG |
6717 | |
6718 | calc_load_update = jiffies + LOAD_FREQ; | |
6719 | ||
bf4d83f6 | 6720 | #ifdef CONFIG_SMP |
29d5e047 | 6721 | idle_thread_set_boot_cpu(); |
029632fb PZ |
6722 | #endif |
6723 | init_sched_fair_class(); | |
6a7b3dc3 | 6724 | |
4698f88c JP |
6725 | init_schedstats(); |
6726 | ||
eb414681 JW |
6727 | psi_init(); |
6728 | ||
69842cba PB |
6729 | init_uclamp(); |
6730 | ||
6892b75e | 6731 | scheduler_running = 1; |
1da177e4 LT |
6732 | } |
6733 | ||
d902db1e | 6734 | #ifdef CONFIG_DEBUG_ATOMIC_SLEEP |
e4aafea2 FW |
6735 | static inline int preempt_count_equals(int preempt_offset) |
6736 | { | |
da7142e2 | 6737 | int nested = preempt_count() + rcu_preempt_depth(); |
e4aafea2 | 6738 | |
4ba8216c | 6739 | return (nested == preempt_offset); |
e4aafea2 FW |
6740 | } |
6741 | ||
d894837f | 6742 | void __might_sleep(const char *file, int line, int preempt_offset) |
1da177e4 | 6743 | { |
8eb23b9f PZ |
6744 | /* |
6745 | * Blocking primitives will set (and therefore destroy) current->state, | |
6746 | * since we will exit with TASK_RUNNING make sure we enter with it, | |
6747 | * otherwise we will destroy state. | |
6748 | */ | |
00845eb9 | 6749 | WARN_ONCE(current->state != TASK_RUNNING && current->task_state_change, |
8eb23b9f PZ |
6750 | "do not call blocking ops when !TASK_RUNNING; " |
6751 | "state=%lx set at [<%p>] %pS\n", | |
6752 | current->state, | |
6753 | (void *)current->task_state_change, | |
00845eb9 | 6754 | (void *)current->task_state_change); |
8eb23b9f | 6755 | |
3427445a PZ |
6756 | ___might_sleep(file, line, preempt_offset); |
6757 | } | |
6758 | EXPORT_SYMBOL(__might_sleep); | |
6759 | ||
6760 | void ___might_sleep(const char *file, int line, int preempt_offset) | |
1da177e4 | 6761 | { |
d1ccc66d IM |
6762 | /* Ratelimiting timestamp: */ |
6763 | static unsigned long prev_jiffy; | |
6764 | ||
d1c6d149 | 6765 | unsigned long preempt_disable_ip; |
1da177e4 | 6766 | |
d1ccc66d IM |
6767 | /* WARN_ON_ONCE() by default, no rate limit required: */ |
6768 | rcu_sleep_check(); | |
6769 | ||
db273be2 | 6770 | if ((preempt_count_equals(preempt_offset) && !irqs_disabled() && |
312364f3 | 6771 | !is_idle_task(current) && !current->non_block_count) || |
1c3c5eab TG |
6772 | system_state == SYSTEM_BOOTING || system_state > SYSTEM_RUNNING || |
6773 | oops_in_progress) | |
aef745fc | 6774 | return; |
1c3c5eab | 6775 | |
aef745fc IM |
6776 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) |
6777 | return; | |
6778 | prev_jiffy = jiffies; | |
6779 | ||
d1ccc66d | 6780 | /* Save this before calling printk(), since that will clobber it: */ |
d1c6d149 VN |
6781 | preempt_disable_ip = get_preempt_disable_ip(current); |
6782 | ||
3df0fc5b PZ |
6783 | printk(KERN_ERR |
6784 | "BUG: sleeping function called from invalid context at %s:%d\n", | |
6785 | file, line); | |
6786 | printk(KERN_ERR | |
312364f3 DV |
6787 | "in_atomic(): %d, irqs_disabled(): %d, non_block: %d, pid: %d, name: %s\n", |
6788 | in_atomic(), irqs_disabled(), current->non_block_count, | |
3df0fc5b | 6789 | current->pid, current->comm); |
aef745fc | 6790 | |
a8b686b3 ES |
6791 | if (task_stack_end_corrupted(current)) |
6792 | printk(KERN_EMERG "Thread overran stack, or stack corrupted\n"); | |
6793 | ||
aef745fc IM |
6794 | debug_show_held_locks(current); |
6795 | if (irqs_disabled()) | |
6796 | print_irqtrace_events(current); | |
d1c6d149 VN |
6797 | if (IS_ENABLED(CONFIG_DEBUG_PREEMPT) |
6798 | && !preempt_count_equals(preempt_offset)) { | |
8f47b187 | 6799 | pr_err("Preemption disabled at:"); |
d1c6d149 | 6800 | print_ip_sym(preempt_disable_ip); |
8f47b187 TG |
6801 | pr_cont("\n"); |
6802 | } | |
aef745fc | 6803 | dump_stack(); |
f0b22e39 | 6804 | add_taint(TAINT_WARN, LOCKDEP_STILL_OK); |
1da177e4 | 6805 | } |
3427445a | 6806 | EXPORT_SYMBOL(___might_sleep); |
568f1967 PZ |
6807 | |
6808 | void __cant_sleep(const char *file, int line, int preempt_offset) | |
6809 | { | |
6810 | static unsigned long prev_jiffy; | |
6811 | ||
6812 | if (irqs_disabled()) | |
6813 | return; | |
6814 | ||
6815 | if (!IS_ENABLED(CONFIG_PREEMPT_COUNT)) | |
6816 | return; | |
6817 | ||
6818 | if (preempt_count() > preempt_offset) | |
6819 | return; | |
6820 | ||
6821 | if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy) | |
6822 | return; | |
6823 | prev_jiffy = jiffies; | |
6824 | ||
6825 | printk(KERN_ERR "BUG: assuming atomic context at %s:%d\n", file, line); | |
6826 | printk(KERN_ERR "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n", | |
6827 | in_atomic(), irqs_disabled(), | |
6828 | current->pid, current->comm); | |
6829 | ||
6830 | debug_show_held_locks(current); | |
6831 | dump_stack(); | |
6832 | add_taint(TAINT_WARN, LOCKDEP_STILL_OK); | |
6833 | } | |
6834 | EXPORT_SYMBOL_GPL(__cant_sleep); | |
1da177e4 LT |
6835 | #endif |
6836 | ||
6837 | #ifdef CONFIG_MAGIC_SYSRQ | |
dbc7f069 | 6838 | void normalize_rt_tasks(void) |
3a5e4dc1 | 6839 | { |
dbc7f069 | 6840 | struct task_struct *g, *p; |
d50dde5a DF |
6841 | struct sched_attr attr = { |
6842 | .sched_policy = SCHED_NORMAL, | |
6843 | }; | |
1da177e4 | 6844 | |
3472eaa1 | 6845 | read_lock(&tasklist_lock); |
5d07f420 | 6846 | for_each_process_thread(g, p) { |
178be793 IM |
6847 | /* |
6848 | * Only normalize user tasks: | |
6849 | */ | |
3472eaa1 | 6850 | if (p->flags & PF_KTHREAD) |
178be793 IM |
6851 | continue; |
6852 | ||
4fa8d299 JP |
6853 | p->se.exec_start = 0; |
6854 | schedstat_set(p->se.statistics.wait_start, 0); | |
6855 | schedstat_set(p->se.statistics.sleep_start, 0); | |
6856 | schedstat_set(p->se.statistics.block_start, 0); | |
dd41f596 | 6857 | |
aab03e05 | 6858 | if (!dl_task(p) && !rt_task(p)) { |
dd41f596 IM |
6859 | /* |
6860 | * Renice negative nice level userspace | |
6861 | * tasks back to 0: | |
6862 | */ | |
3472eaa1 | 6863 | if (task_nice(p) < 0) |
dd41f596 | 6864 | set_user_nice(p, 0); |
1da177e4 | 6865 | continue; |
dd41f596 | 6866 | } |
1da177e4 | 6867 | |
dbc7f069 | 6868 | __sched_setscheduler(p, &attr, false, false); |
5d07f420 | 6869 | } |
3472eaa1 | 6870 | read_unlock(&tasklist_lock); |
1da177e4 LT |
6871 | } |
6872 | ||
6873 | #endif /* CONFIG_MAGIC_SYSRQ */ | |
1df5c10a | 6874 | |
67fc4e0c | 6875 | #if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) |
1df5c10a | 6876 | /* |
67fc4e0c | 6877 | * These functions are only useful for the IA64 MCA handling, or kdb. |
1df5c10a LT |
6878 | * |
6879 | * They can only be called when the whole system has been | |
6880 | * stopped - every CPU needs to be quiescent, and no scheduling | |
6881 | * activity can take place. Using them for anything else would | |
6882 | * be a serious bug, and as a result, they aren't even visible | |
6883 | * under any other configuration. | |
6884 | */ | |
6885 | ||
6886 | /** | |
d1ccc66d | 6887 | * curr_task - return the current task for a given CPU. |
1df5c10a LT |
6888 | * @cpu: the processor in question. |
6889 | * | |
6890 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! | |
e69f6186 YB |
6891 | * |
6892 | * Return: The current task for @cpu. | |
1df5c10a | 6893 | */ |
36c8b586 | 6894 | struct task_struct *curr_task(int cpu) |
1df5c10a LT |
6895 | { |
6896 | return cpu_curr(cpu); | |
6897 | } | |
6898 | ||
67fc4e0c JW |
6899 | #endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */ |
6900 | ||
6901 | #ifdef CONFIG_IA64 | |
1df5c10a | 6902 | /** |
5feeb783 | 6903 | * ia64_set_curr_task - set the current task for a given CPU. |
1df5c10a LT |
6904 | * @cpu: the processor in question. |
6905 | * @p: the task pointer to set. | |
6906 | * | |
6907 | * Description: This function must only be used when non-maskable interrupts | |
41a2d6cf | 6908 | * are serviced on a separate stack. It allows the architecture to switch the |
d1ccc66d | 6909 | * notion of the current task on a CPU in a non-blocking manner. This function |
1df5c10a LT |
6910 | * must be called with all CPU's synchronized, and interrupts disabled, the |
6911 | * and caller must save the original value of the current task (see | |
6912 | * curr_task() above) and restore that value before reenabling interrupts and | |
6913 | * re-starting the system. | |
6914 | * | |
6915 | * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED! | |
6916 | */ | |
a458ae2e | 6917 | void ia64_set_curr_task(int cpu, struct task_struct *p) |
1df5c10a LT |
6918 | { |
6919 | cpu_curr(cpu) = p; | |
6920 | } | |
6921 | ||
6922 | #endif | |
29f59db3 | 6923 | |
7c941438 | 6924 | #ifdef CONFIG_CGROUP_SCHED |
029632fb PZ |
6925 | /* task_group_lock serializes the addition/removal of task groups */ |
6926 | static DEFINE_SPINLOCK(task_group_lock); | |
6927 | ||
2480c093 PB |
6928 | static inline void alloc_uclamp_sched_group(struct task_group *tg, |
6929 | struct task_group *parent) | |
6930 | { | |
6931 | #ifdef CONFIG_UCLAMP_TASK_GROUP | |
0413d7f3 | 6932 | enum uclamp_id clamp_id; |
2480c093 PB |
6933 | |
6934 | for_each_clamp_id(clamp_id) { | |
6935 | uclamp_se_set(&tg->uclamp_req[clamp_id], | |
6936 | uclamp_none(clamp_id), false); | |
0b60ba2d | 6937 | tg->uclamp[clamp_id] = parent->uclamp[clamp_id]; |
2480c093 PB |
6938 | } |
6939 | #endif | |
6940 | } | |
6941 | ||
2f5177f0 | 6942 | static void sched_free_group(struct task_group *tg) |
bccbe08a PZ |
6943 | { |
6944 | free_fair_sched_group(tg); | |
6945 | free_rt_sched_group(tg); | |
e9aa1dd1 | 6946 | autogroup_free(tg); |
b0367629 | 6947 | kmem_cache_free(task_group_cache, tg); |
bccbe08a PZ |
6948 | } |
6949 | ||
6950 | /* allocate runqueue etc for a new task group */ | |
ec7dc8ac | 6951 | struct task_group *sched_create_group(struct task_group *parent) |
bccbe08a PZ |
6952 | { |
6953 | struct task_group *tg; | |
bccbe08a | 6954 | |
b0367629 | 6955 | tg = kmem_cache_alloc(task_group_cache, GFP_KERNEL | __GFP_ZERO); |
bccbe08a PZ |
6956 | if (!tg) |
6957 | return ERR_PTR(-ENOMEM); | |
6958 | ||
ec7dc8ac | 6959 | if (!alloc_fair_sched_group(tg, parent)) |
bccbe08a PZ |
6960 | goto err; |
6961 | ||
ec7dc8ac | 6962 | if (!alloc_rt_sched_group(tg, parent)) |
bccbe08a PZ |
6963 | goto err; |
6964 | ||
2480c093 PB |
6965 | alloc_uclamp_sched_group(tg, parent); |
6966 | ||
ace783b9 LZ |
6967 | return tg; |
6968 | ||
6969 | err: | |
2f5177f0 | 6970 | sched_free_group(tg); |
ace783b9 LZ |
6971 | return ERR_PTR(-ENOMEM); |
6972 | } | |
6973 | ||
6974 | void sched_online_group(struct task_group *tg, struct task_group *parent) | |
6975 | { | |
6976 | unsigned long flags; | |
6977 | ||
8ed36996 | 6978 | spin_lock_irqsave(&task_group_lock, flags); |
6f505b16 | 6979 | list_add_rcu(&tg->list, &task_groups); |
f473aa5e | 6980 | |
d1ccc66d IM |
6981 | /* Root should already exist: */ |
6982 | WARN_ON(!parent); | |
f473aa5e PZ |
6983 | |
6984 | tg->parent = parent; | |
f473aa5e | 6985 | INIT_LIST_HEAD(&tg->children); |
09f2724a | 6986 | list_add_rcu(&tg->siblings, &parent->children); |
8ed36996 | 6987 | spin_unlock_irqrestore(&task_group_lock, flags); |
8663e24d PZ |
6988 | |
6989 | online_fair_sched_group(tg); | |
29f59db3 SV |
6990 | } |
6991 | ||
9b5b7751 | 6992 | /* rcu callback to free various structures associated with a task group */ |
2f5177f0 | 6993 | static void sched_free_group_rcu(struct rcu_head *rhp) |
29f59db3 | 6994 | { |
d1ccc66d | 6995 | /* Now it should be safe to free those cfs_rqs: */ |
2f5177f0 | 6996 | sched_free_group(container_of(rhp, struct task_group, rcu)); |
29f59db3 SV |
6997 | } |
6998 | ||
4cf86d77 | 6999 | void sched_destroy_group(struct task_group *tg) |
ace783b9 | 7000 | { |
d1ccc66d | 7001 | /* Wait for possible concurrent references to cfs_rqs complete: */ |
2f5177f0 | 7002 | call_rcu(&tg->rcu, sched_free_group_rcu); |
ace783b9 LZ |
7003 | } |
7004 | ||
7005 | void sched_offline_group(struct task_group *tg) | |
29f59db3 | 7006 | { |
8ed36996 | 7007 | unsigned long flags; |
29f59db3 | 7008 | |
d1ccc66d | 7009 | /* End participation in shares distribution: */ |
6fe1f348 | 7010 | unregister_fair_sched_group(tg); |
3d4b47b4 PZ |
7011 | |
7012 | spin_lock_irqsave(&task_group_lock, flags); | |
6f505b16 | 7013 | list_del_rcu(&tg->list); |
f473aa5e | 7014 | list_del_rcu(&tg->siblings); |
8ed36996 | 7015 | spin_unlock_irqrestore(&task_group_lock, flags); |
29f59db3 SV |
7016 | } |
7017 | ||
ea86cb4b | 7018 | static void sched_change_group(struct task_struct *tsk, int type) |
29f59db3 | 7019 | { |
8323f26c | 7020 | struct task_group *tg; |
29f59db3 | 7021 | |
f7b8a47d KT |
7022 | /* |
7023 | * All callers are synchronized by task_rq_lock(); we do not use RCU | |
7024 | * which is pointless here. Thus, we pass "true" to task_css_check() | |
7025 | * to prevent lockdep warnings. | |
7026 | */ | |
7027 | tg = container_of(task_css_check(tsk, cpu_cgrp_id, true), | |
8323f26c PZ |
7028 | struct task_group, css); |
7029 | tg = autogroup_task_group(tsk, tg); | |
7030 | tsk->sched_task_group = tg; | |
7031 | ||
810b3817 | 7032 | #ifdef CONFIG_FAIR_GROUP_SCHED |
ea86cb4b VG |
7033 | if (tsk->sched_class->task_change_group) |
7034 | tsk->sched_class->task_change_group(tsk, type); | |
b2b5ce02 | 7035 | else |
810b3817 | 7036 | #endif |
b2b5ce02 | 7037 | set_task_rq(tsk, task_cpu(tsk)); |
ea86cb4b VG |
7038 | } |
7039 | ||
7040 | /* | |
7041 | * Change task's runqueue when it moves between groups. | |
7042 | * | |
7043 | * The caller of this function should have put the task in its new group by | |
7044 | * now. This function just updates tsk->se.cfs_rq and tsk->se.parent to reflect | |
7045 | * its new group. | |
7046 | */ | |
7047 | void sched_move_task(struct task_struct *tsk) | |
7048 | { | |
7a57f32a PZ |
7049 | int queued, running, queue_flags = |
7050 | DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK; | |
ea86cb4b VG |
7051 | struct rq_flags rf; |
7052 | struct rq *rq; | |
7053 | ||
7054 | rq = task_rq_lock(tsk, &rf); | |
1b1d6225 | 7055 | update_rq_clock(rq); |
ea86cb4b VG |
7056 | |
7057 | running = task_current(rq, tsk); | |
7058 | queued = task_on_rq_queued(tsk); | |
7059 | ||
7060 | if (queued) | |
7a57f32a | 7061 | dequeue_task(rq, tsk, queue_flags); |
bb3bac2c | 7062 | if (running) |
ea86cb4b VG |
7063 | put_prev_task(rq, tsk); |
7064 | ||
7065 | sched_change_group(tsk, TASK_MOVE_GROUP); | |
810b3817 | 7066 | |
da0c1e65 | 7067 | if (queued) |
7a57f32a | 7068 | enqueue_task(rq, tsk, queue_flags); |
2a4b03ff | 7069 | if (running) { |
03b7fad1 | 7070 | set_next_task(rq, tsk); |
2a4b03ff VG |
7071 | /* |
7072 | * After changing group, the running task may have joined a | |
7073 | * throttled one but it's still the running task. Trigger a | |
7074 | * resched to make sure that task can still run. | |
7075 | */ | |
7076 | resched_curr(rq); | |
7077 | } | |
29f59db3 | 7078 | |
eb580751 | 7079 | task_rq_unlock(rq, tsk, &rf); |
29f59db3 | 7080 | } |
68318b8e | 7081 | |
a7c6d554 | 7082 | static inline struct task_group *css_tg(struct cgroup_subsys_state *css) |
68318b8e | 7083 | { |
a7c6d554 | 7084 | return css ? container_of(css, struct task_group, css) : NULL; |
68318b8e SV |
7085 | } |
7086 | ||
eb95419b TH |
7087 | static struct cgroup_subsys_state * |
7088 | cpu_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) | |
68318b8e | 7089 | { |
eb95419b TH |
7090 | struct task_group *parent = css_tg(parent_css); |
7091 | struct task_group *tg; | |
68318b8e | 7092 | |
eb95419b | 7093 | if (!parent) { |
68318b8e | 7094 | /* This is early initialization for the top cgroup */ |
07e06b01 | 7095 | return &root_task_group.css; |
68318b8e SV |
7096 | } |
7097 | ||
ec7dc8ac | 7098 | tg = sched_create_group(parent); |
68318b8e SV |
7099 | if (IS_ERR(tg)) |
7100 | return ERR_PTR(-ENOMEM); | |
7101 | ||
68318b8e SV |
7102 | return &tg->css; |
7103 | } | |
7104 | ||
96b77745 KK |
7105 | /* Expose task group only after completing cgroup initialization */ |
7106 | static int cpu_cgroup_css_online(struct cgroup_subsys_state *css) | |
7107 | { | |
7108 | struct task_group *tg = css_tg(css); | |
7109 | struct task_group *parent = css_tg(css->parent); | |
7110 | ||
7111 | if (parent) | |
7112 | sched_online_group(tg, parent); | |
7226017a QY |
7113 | |
7114 | #ifdef CONFIG_UCLAMP_TASK_GROUP | |
7115 | /* Propagate the effective uclamp value for the new group */ | |
7116 | cpu_util_update_eff(css); | |
7117 | #endif | |
7118 | ||
96b77745 KK |
7119 | return 0; |
7120 | } | |
7121 | ||
2f5177f0 | 7122 | static void cpu_cgroup_css_released(struct cgroup_subsys_state *css) |
ace783b9 | 7123 | { |
eb95419b | 7124 | struct task_group *tg = css_tg(css); |
ace783b9 | 7125 | |
2f5177f0 | 7126 | sched_offline_group(tg); |
ace783b9 LZ |
7127 | } |
7128 | ||
eb95419b | 7129 | static void cpu_cgroup_css_free(struct cgroup_subsys_state *css) |
68318b8e | 7130 | { |
eb95419b | 7131 | struct task_group *tg = css_tg(css); |
68318b8e | 7132 | |
2f5177f0 PZ |
7133 | /* |
7134 | * Relies on the RCU grace period between css_released() and this. | |
7135 | */ | |
7136 | sched_free_group(tg); | |
ace783b9 LZ |
7137 | } |
7138 | ||
ea86cb4b VG |
7139 | /* |
7140 | * This is called before wake_up_new_task(), therefore we really only | |
7141 | * have to set its group bits, all the other stuff does not apply. | |
7142 | */ | |
b53202e6 | 7143 | static void cpu_cgroup_fork(struct task_struct *task) |
eeb61e53 | 7144 | { |
ea86cb4b VG |
7145 | struct rq_flags rf; |
7146 | struct rq *rq; | |
7147 | ||
7148 | rq = task_rq_lock(task, &rf); | |
7149 | ||
80f5c1b8 | 7150 | update_rq_clock(rq); |
ea86cb4b VG |
7151 | sched_change_group(task, TASK_SET_GROUP); |
7152 | ||
7153 | task_rq_unlock(rq, task, &rf); | |
eeb61e53 KT |
7154 | } |
7155 | ||
1f7dd3e5 | 7156 | static int cpu_cgroup_can_attach(struct cgroup_taskset *tset) |
68318b8e | 7157 | { |
bb9d97b6 | 7158 | struct task_struct *task; |
1f7dd3e5 | 7159 | struct cgroup_subsys_state *css; |
7dc603c9 | 7160 | int ret = 0; |
bb9d97b6 | 7161 | |
1f7dd3e5 | 7162 | cgroup_taskset_for_each(task, css, tset) { |
b68aa230 | 7163 | #ifdef CONFIG_RT_GROUP_SCHED |
eb95419b | 7164 | if (!sched_rt_can_attach(css_tg(css), task)) |
bb9d97b6 | 7165 | return -EINVAL; |
b68aa230 | 7166 | #endif |
7dc603c9 PZ |
7167 | /* |
7168 | * Serialize against wake_up_new_task() such that if its | |
7169 | * running, we're sure to observe its full state. | |
7170 | */ | |
7171 | raw_spin_lock_irq(&task->pi_lock); | |
7172 | /* | |
7173 | * Avoid calling sched_move_task() before wake_up_new_task() | |
7174 | * has happened. This would lead to problems with PELT, due to | |
7175 | * move wanting to detach+attach while we're not attached yet. | |
7176 | */ | |
7177 | if (task->state == TASK_NEW) | |
7178 | ret = -EINVAL; | |
7179 | raw_spin_unlock_irq(&task->pi_lock); | |
7180 | ||
7181 | if (ret) | |
7182 | break; | |
bb9d97b6 | 7183 | } |
7dc603c9 | 7184 | return ret; |
be367d09 | 7185 | } |
68318b8e | 7186 | |
1f7dd3e5 | 7187 | static void cpu_cgroup_attach(struct cgroup_taskset *tset) |
68318b8e | 7188 | { |
bb9d97b6 | 7189 | struct task_struct *task; |
1f7dd3e5 | 7190 | struct cgroup_subsys_state *css; |
bb9d97b6 | 7191 | |
1f7dd3e5 | 7192 | cgroup_taskset_for_each(task, css, tset) |
bb9d97b6 | 7193 | sched_move_task(task); |
68318b8e SV |
7194 | } |
7195 | ||
2480c093 | 7196 | #ifdef CONFIG_UCLAMP_TASK_GROUP |
0b60ba2d PB |
7197 | static void cpu_util_update_eff(struct cgroup_subsys_state *css) |
7198 | { | |
7199 | struct cgroup_subsys_state *top_css = css; | |
7200 | struct uclamp_se *uc_parent = NULL; | |
7201 | struct uclamp_se *uc_se = NULL; | |
7202 | unsigned int eff[UCLAMP_CNT]; | |
0413d7f3 | 7203 | enum uclamp_id clamp_id; |
0b60ba2d PB |
7204 | unsigned int clamps; |
7205 | ||
7206 | css_for_each_descendant_pre(css, top_css) { | |
7207 | uc_parent = css_tg(css)->parent | |
7208 | ? css_tg(css)->parent->uclamp : NULL; | |
7209 | ||
7210 | for_each_clamp_id(clamp_id) { | |
7211 | /* Assume effective clamps matches requested clamps */ | |
7212 | eff[clamp_id] = css_tg(css)->uclamp_req[clamp_id].value; | |
7213 | /* Cap effective clamps with parent's effective clamps */ | |
7214 | if (uc_parent && | |
7215 | eff[clamp_id] > uc_parent[clamp_id].value) { | |
7216 | eff[clamp_id] = uc_parent[clamp_id].value; | |
7217 | } | |
7218 | } | |
7219 | /* Ensure protection is always capped by limit */ | |
7220 | eff[UCLAMP_MIN] = min(eff[UCLAMP_MIN], eff[UCLAMP_MAX]); | |
7221 | ||
7222 | /* Propagate most restrictive effective clamps */ | |
7223 | clamps = 0x0; | |
7224 | uc_se = css_tg(css)->uclamp; | |
7225 | for_each_clamp_id(clamp_id) { | |
7226 | if (eff[clamp_id] == uc_se[clamp_id].value) | |
7227 | continue; | |
7228 | uc_se[clamp_id].value = eff[clamp_id]; | |
7229 | uc_se[clamp_id].bucket_id = uclamp_bucket_id(eff[clamp_id]); | |
7230 | clamps |= (0x1 << clamp_id); | |
7231 | } | |
babbe170 | 7232 | if (!clamps) { |
0b60ba2d | 7233 | css = css_rightmost_descendant(css); |
babbe170 PB |
7234 | continue; |
7235 | } | |
7236 | ||
7237 | /* Immediately update descendants RUNNABLE tasks */ | |
7238 | uclamp_update_active_tasks(css, clamps); | |
0b60ba2d PB |
7239 | } |
7240 | } | |
2480c093 PB |
7241 | |
7242 | /* | |
7243 | * Integer 10^N with a given N exponent by casting to integer the literal "1eN" | |
7244 | * C expression. Since there is no way to convert a macro argument (N) into a | |
7245 | * character constant, use two levels of macros. | |
7246 | */ | |
7247 | #define _POW10(exp) ((unsigned int)1e##exp) | |
7248 | #define POW10(exp) _POW10(exp) | |
7249 | ||
7250 | struct uclamp_request { | |
7251 | #define UCLAMP_PERCENT_SHIFT 2 | |
7252 | #define UCLAMP_PERCENT_SCALE (100 * POW10(UCLAMP_PERCENT_SHIFT)) | |
7253 | s64 percent; | |
7254 | u64 util; | |
7255 | int ret; | |
7256 | }; | |
7257 | ||
7258 | static inline struct uclamp_request | |
7259 | capacity_from_percent(char *buf) | |
7260 | { | |
7261 | struct uclamp_request req = { | |
7262 | .percent = UCLAMP_PERCENT_SCALE, | |
7263 | .util = SCHED_CAPACITY_SCALE, | |
7264 | .ret = 0, | |
7265 | }; | |
7266 | ||
7267 | buf = strim(buf); | |
7268 | if (strcmp(buf, "max")) { | |
7269 | req.ret = cgroup_parse_float(buf, UCLAMP_PERCENT_SHIFT, | |
7270 | &req.percent); | |
7271 | if (req.ret) | |
7272 | return req; | |
b562d140 | 7273 | if ((u64)req.percent > UCLAMP_PERCENT_SCALE) { |
2480c093 PB |
7274 | req.ret = -ERANGE; |
7275 | return req; | |
7276 | } | |
7277 | ||
7278 | req.util = req.percent << SCHED_CAPACITY_SHIFT; | |
7279 | req.util = DIV_ROUND_CLOSEST_ULL(req.util, UCLAMP_PERCENT_SCALE); | |
7280 | } | |
7281 | ||
7282 | return req; | |
7283 | } | |
7284 | ||
7285 | static ssize_t cpu_uclamp_write(struct kernfs_open_file *of, char *buf, | |
7286 | size_t nbytes, loff_t off, | |
7287 | enum uclamp_id clamp_id) | |
7288 | { | |
7289 | struct uclamp_request req; | |
7290 | struct task_group *tg; | |
7291 | ||
7292 | req = capacity_from_percent(buf); | |
7293 | if (req.ret) | |
7294 | return req.ret; | |
7295 | ||
7296 | mutex_lock(&uclamp_mutex); | |
7297 | rcu_read_lock(); | |
7298 | ||
7299 | tg = css_tg(of_css(of)); | |
7300 | if (tg->uclamp_req[clamp_id].value != req.util) | |
7301 | uclamp_se_set(&tg->uclamp_req[clamp_id], req.util, false); | |
7302 | ||
7303 | /* | |
7304 | * Because of not recoverable conversion rounding we keep track of the | |
7305 | * exact requested value | |
7306 | */ | |
7307 | tg->uclamp_pct[clamp_id] = req.percent; | |
7308 | ||
0b60ba2d PB |
7309 | /* Update effective clamps to track the most restrictive value */ |
7310 | cpu_util_update_eff(of_css(of)); | |
7311 | ||
2480c093 PB |
7312 | rcu_read_unlock(); |
7313 | mutex_unlock(&uclamp_mutex); | |
7314 | ||
7315 | return nbytes; | |
7316 | } | |
7317 | ||
7318 | static ssize_t cpu_uclamp_min_write(struct kernfs_open_file *of, | |
7319 | char *buf, size_t nbytes, | |
7320 | loff_t off) | |
7321 | { | |
7322 | return cpu_uclamp_write(of, buf, nbytes, off, UCLAMP_MIN); | |
7323 | } | |
7324 | ||
7325 | static ssize_t cpu_uclamp_max_write(struct kernfs_open_file *of, | |
7326 | char *buf, size_t nbytes, | |
7327 | loff_t off) | |
7328 | { | |
7329 | return cpu_uclamp_write(of, buf, nbytes, off, UCLAMP_MAX); | |
7330 | } | |
7331 | ||
7332 | static inline void cpu_uclamp_print(struct seq_file *sf, | |
7333 | enum uclamp_id clamp_id) | |
7334 | { | |
7335 | struct task_group *tg; | |
7336 | u64 util_clamp; | |
7337 | u64 percent; | |
7338 | u32 rem; | |
7339 | ||
7340 | rcu_read_lock(); | |
7341 | tg = css_tg(seq_css(sf)); | |
7342 | util_clamp = tg->uclamp_req[clamp_id].value; | |
7343 | rcu_read_unlock(); | |
7344 | ||
7345 | if (util_clamp == SCHED_CAPACITY_SCALE) { | |
7346 | seq_puts(sf, "max\n"); | |
7347 | return; | |
7348 | } | |
7349 | ||
7350 | percent = tg->uclamp_pct[clamp_id]; | |
7351 | percent = div_u64_rem(percent, POW10(UCLAMP_PERCENT_SHIFT), &rem); | |
7352 | seq_printf(sf, "%llu.%0*u\n", percent, UCLAMP_PERCENT_SHIFT, rem); | |
7353 | } | |
7354 | ||
7355 | static int cpu_uclamp_min_show(struct seq_file *sf, void *v) | |
7356 | { | |
7357 | cpu_uclamp_print(sf, UCLAMP_MIN); | |
7358 | return 0; | |
7359 | } | |
7360 | ||
7361 | static int cpu_uclamp_max_show(struct seq_file *sf, void *v) | |
7362 | { | |
7363 | cpu_uclamp_print(sf, UCLAMP_MAX); | |
7364 | return 0; | |
7365 | } | |
7366 | #endif /* CONFIG_UCLAMP_TASK_GROUP */ | |
7367 | ||
052f1dc7 | 7368 | #ifdef CONFIG_FAIR_GROUP_SCHED |
182446d0 TH |
7369 | static int cpu_shares_write_u64(struct cgroup_subsys_state *css, |
7370 | struct cftype *cftype, u64 shareval) | |
68318b8e | 7371 | { |
5b61d50a KK |
7372 | if (shareval > scale_load_down(ULONG_MAX)) |
7373 | shareval = MAX_SHARES; | |
182446d0 | 7374 | return sched_group_set_shares(css_tg(css), scale_load(shareval)); |
68318b8e SV |
7375 | } |
7376 | ||
182446d0 TH |
7377 | static u64 cpu_shares_read_u64(struct cgroup_subsys_state *css, |
7378 | struct cftype *cft) | |
68318b8e | 7379 | { |
182446d0 | 7380 | struct task_group *tg = css_tg(css); |
68318b8e | 7381 | |
c8b28116 | 7382 | return (u64) scale_load_down(tg->shares); |
68318b8e | 7383 | } |
ab84d31e PT |
7384 | |
7385 | #ifdef CONFIG_CFS_BANDWIDTH | |
a790de99 PT |
7386 | static DEFINE_MUTEX(cfs_constraints_mutex); |
7387 | ||
ab84d31e | 7388 | const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */ |
b1546edc | 7389 | static const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */ |
ab84d31e | 7390 | |
a790de99 PT |
7391 | static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime); |
7392 | ||
ab84d31e PT |
7393 | static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota) |
7394 | { | |
56f570e5 | 7395 | int i, ret = 0, runtime_enabled, runtime_was_enabled; |
029632fb | 7396 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; |
ab84d31e PT |
7397 | |
7398 | if (tg == &root_task_group) | |
7399 | return -EINVAL; | |
7400 | ||
7401 | /* | |
7402 | * Ensure we have at some amount of bandwidth every period. This is | |
7403 | * to prevent reaching a state of large arrears when throttled via | |
7404 | * entity_tick() resulting in prolonged exit starvation. | |
7405 | */ | |
7406 | if (quota < min_cfs_quota_period || period < min_cfs_quota_period) | |
7407 | return -EINVAL; | |
7408 | ||
7409 | /* | |
7410 | * Likewise, bound things on the otherside by preventing insane quota | |
7411 | * periods. This also allows us to normalize in computing quota | |
7412 | * feasibility. | |
7413 | */ | |
7414 | if (period > max_cfs_quota_period) | |
7415 | return -EINVAL; | |
7416 | ||
0e59bdae KT |
7417 | /* |
7418 | * Prevent race between setting of cfs_rq->runtime_enabled and | |
7419 | * unthrottle_offline_cfs_rqs(). | |
7420 | */ | |
7421 | get_online_cpus(); | |
a790de99 PT |
7422 | mutex_lock(&cfs_constraints_mutex); |
7423 | ret = __cfs_schedulable(tg, period, quota); | |
7424 | if (ret) | |
7425 | goto out_unlock; | |
7426 | ||
58088ad0 | 7427 | runtime_enabled = quota != RUNTIME_INF; |
56f570e5 | 7428 | runtime_was_enabled = cfs_b->quota != RUNTIME_INF; |
1ee14e6c BS |
7429 | /* |
7430 | * If we need to toggle cfs_bandwidth_used, off->on must occur | |
7431 | * before making related changes, and on->off must occur afterwards | |
7432 | */ | |
7433 | if (runtime_enabled && !runtime_was_enabled) | |
7434 | cfs_bandwidth_usage_inc(); | |
ab84d31e PT |
7435 | raw_spin_lock_irq(&cfs_b->lock); |
7436 | cfs_b->period = ns_to_ktime(period); | |
7437 | cfs_b->quota = quota; | |
58088ad0 | 7438 | |
a9cf55b2 | 7439 | __refill_cfs_bandwidth_runtime(cfs_b); |
d1ccc66d IM |
7440 | |
7441 | /* Restart the period timer (if active) to handle new period expiry: */ | |
77a4d1a1 PZ |
7442 | if (runtime_enabled) |
7443 | start_cfs_bandwidth(cfs_b); | |
d1ccc66d | 7444 | |
ab84d31e PT |
7445 | raw_spin_unlock_irq(&cfs_b->lock); |
7446 | ||
0e59bdae | 7447 | for_each_online_cpu(i) { |
ab84d31e | 7448 | struct cfs_rq *cfs_rq = tg->cfs_rq[i]; |
029632fb | 7449 | struct rq *rq = cfs_rq->rq; |
8a8c69c3 | 7450 | struct rq_flags rf; |
ab84d31e | 7451 | |
8a8c69c3 | 7452 | rq_lock_irq(rq, &rf); |
58088ad0 | 7453 | cfs_rq->runtime_enabled = runtime_enabled; |
ab84d31e | 7454 | cfs_rq->runtime_remaining = 0; |
671fd9da | 7455 | |
029632fb | 7456 | if (cfs_rq->throttled) |
671fd9da | 7457 | unthrottle_cfs_rq(cfs_rq); |
8a8c69c3 | 7458 | rq_unlock_irq(rq, &rf); |
ab84d31e | 7459 | } |
1ee14e6c BS |
7460 | if (runtime_was_enabled && !runtime_enabled) |
7461 | cfs_bandwidth_usage_dec(); | |
a790de99 PT |
7462 | out_unlock: |
7463 | mutex_unlock(&cfs_constraints_mutex); | |
0e59bdae | 7464 | put_online_cpus(); |
ab84d31e | 7465 | |
a790de99 | 7466 | return ret; |
ab84d31e PT |
7467 | } |
7468 | ||
b1546edc | 7469 | static int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us) |
ab84d31e PT |
7470 | { |
7471 | u64 quota, period; | |
7472 | ||
029632fb | 7473 | period = ktime_to_ns(tg->cfs_bandwidth.period); |
ab84d31e PT |
7474 | if (cfs_quota_us < 0) |
7475 | quota = RUNTIME_INF; | |
1a8b4540 | 7476 | else if ((u64)cfs_quota_us <= U64_MAX / NSEC_PER_USEC) |
ab84d31e | 7477 | quota = (u64)cfs_quota_us * NSEC_PER_USEC; |
1a8b4540 KK |
7478 | else |
7479 | return -EINVAL; | |
ab84d31e PT |
7480 | |
7481 | return tg_set_cfs_bandwidth(tg, period, quota); | |
7482 | } | |
7483 | ||
b1546edc | 7484 | static long tg_get_cfs_quota(struct task_group *tg) |
ab84d31e PT |
7485 | { |
7486 | u64 quota_us; | |
7487 | ||
029632fb | 7488 | if (tg->cfs_bandwidth.quota == RUNTIME_INF) |
ab84d31e PT |
7489 | return -1; |
7490 | ||
029632fb | 7491 | quota_us = tg->cfs_bandwidth.quota; |
ab84d31e PT |
7492 | do_div(quota_us, NSEC_PER_USEC); |
7493 | ||
7494 | return quota_us; | |
7495 | } | |
7496 | ||
b1546edc | 7497 | static int tg_set_cfs_period(struct task_group *tg, long cfs_period_us) |
ab84d31e PT |
7498 | { |
7499 | u64 quota, period; | |
7500 | ||
1a8b4540 KK |
7501 | if ((u64)cfs_period_us > U64_MAX / NSEC_PER_USEC) |
7502 | return -EINVAL; | |
7503 | ||
ab84d31e | 7504 | period = (u64)cfs_period_us * NSEC_PER_USEC; |
029632fb | 7505 | quota = tg->cfs_bandwidth.quota; |
ab84d31e | 7506 | |
ab84d31e PT |
7507 | return tg_set_cfs_bandwidth(tg, period, quota); |
7508 | } | |
7509 | ||
b1546edc | 7510 | static long tg_get_cfs_period(struct task_group *tg) |
ab84d31e PT |
7511 | { |
7512 | u64 cfs_period_us; | |
7513 | ||
029632fb | 7514 | cfs_period_us = ktime_to_ns(tg->cfs_bandwidth.period); |
ab84d31e PT |
7515 | do_div(cfs_period_us, NSEC_PER_USEC); |
7516 | ||
7517 | return cfs_period_us; | |
7518 | } | |
7519 | ||
182446d0 TH |
7520 | static s64 cpu_cfs_quota_read_s64(struct cgroup_subsys_state *css, |
7521 | struct cftype *cft) | |
ab84d31e | 7522 | { |
182446d0 | 7523 | return tg_get_cfs_quota(css_tg(css)); |
ab84d31e PT |
7524 | } |
7525 | ||
182446d0 TH |
7526 | static int cpu_cfs_quota_write_s64(struct cgroup_subsys_state *css, |
7527 | struct cftype *cftype, s64 cfs_quota_us) | |
ab84d31e | 7528 | { |
182446d0 | 7529 | return tg_set_cfs_quota(css_tg(css), cfs_quota_us); |
ab84d31e PT |
7530 | } |
7531 | ||
182446d0 TH |
7532 | static u64 cpu_cfs_period_read_u64(struct cgroup_subsys_state *css, |
7533 | struct cftype *cft) | |
ab84d31e | 7534 | { |
182446d0 | 7535 | return tg_get_cfs_period(css_tg(css)); |
ab84d31e PT |
7536 | } |
7537 | ||
182446d0 TH |
7538 | static int cpu_cfs_period_write_u64(struct cgroup_subsys_state *css, |
7539 | struct cftype *cftype, u64 cfs_period_us) | |
ab84d31e | 7540 | { |
182446d0 | 7541 | return tg_set_cfs_period(css_tg(css), cfs_period_us); |
ab84d31e PT |
7542 | } |
7543 | ||
a790de99 PT |
7544 | struct cfs_schedulable_data { |
7545 | struct task_group *tg; | |
7546 | u64 period, quota; | |
7547 | }; | |
7548 | ||
7549 | /* | |
7550 | * normalize group quota/period to be quota/max_period | |
7551 | * note: units are usecs | |
7552 | */ | |
7553 | static u64 normalize_cfs_quota(struct task_group *tg, | |
7554 | struct cfs_schedulable_data *d) | |
7555 | { | |
7556 | u64 quota, period; | |
7557 | ||
7558 | if (tg == d->tg) { | |
7559 | period = d->period; | |
7560 | quota = d->quota; | |
7561 | } else { | |
7562 | period = tg_get_cfs_period(tg); | |
7563 | quota = tg_get_cfs_quota(tg); | |
7564 | } | |
7565 | ||
7566 | /* note: these should typically be equivalent */ | |
7567 | if (quota == RUNTIME_INF || quota == -1) | |
7568 | return RUNTIME_INF; | |
7569 | ||
7570 | return to_ratio(period, quota); | |
7571 | } | |
7572 | ||
7573 | static int tg_cfs_schedulable_down(struct task_group *tg, void *data) | |
7574 | { | |
7575 | struct cfs_schedulable_data *d = data; | |
029632fb | 7576 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; |
a790de99 PT |
7577 | s64 quota = 0, parent_quota = -1; |
7578 | ||
7579 | if (!tg->parent) { | |
7580 | quota = RUNTIME_INF; | |
7581 | } else { | |
029632fb | 7582 | struct cfs_bandwidth *parent_b = &tg->parent->cfs_bandwidth; |
a790de99 PT |
7583 | |
7584 | quota = normalize_cfs_quota(tg, d); | |
9c58c79a | 7585 | parent_quota = parent_b->hierarchical_quota; |
a790de99 PT |
7586 | |
7587 | /* | |
c53593e5 TH |
7588 | * Ensure max(child_quota) <= parent_quota. On cgroup2, |
7589 | * always take the min. On cgroup1, only inherit when no | |
d1ccc66d | 7590 | * limit is set: |
a790de99 | 7591 | */ |
c53593e5 TH |
7592 | if (cgroup_subsys_on_dfl(cpu_cgrp_subsys)) { |
7593 | quota = min(quota, parent_quota); | |
7594 | } else { | |
7595 | if (quota == RUNTIME_INF) | |
7596 | quota = parent_quota; | |
7597 | else if (parent_quota != RUNTIME_INF && quota > parent_quota) | |
7598 | return -EINVAL; | |
7599 | } | |
a790de99 | 7600 | } |
9c58c79a | 7601 | cfs_b->hierarchical_quota = quota; |
a790de99 PT |
7602 | |
7603 | return 0; | |
7604 | } | |
7605 | ||
7606 | static int __cfs_schedulable(struct task_group *tg, u64 period, u64 quota) | |
7607 | { | |
8277434e | 7608 | int ret; |
a790de99 PT |
7609 | struct cfs_schedulable_data data = { |
7610 | .tg = tg, | |
7611 | .period = period, | |
7612 | .quota = quota, | |
7613 | }; | |
7614 | ||
7615 | if (quota != RUNTIME_INF) { | |
7616 | do_div(data.period, NSEC_PER_USEC); | |
7617 | do_div(data.quota, NSEC_PER_USEC); | |
7618 | } | |
7619 | ||
8277434e PT |
7620 | rcu_read_lock(); |
7621 | ret = walk_tg_tree(tg_cfs_schedulable_down, tg_nop, &data); | |
7622 | rcu_read_unlock(); | |
7623 | ||
7624 | return ret; | |
a790de99 | 7625 | } |
e8da1b18 | 7626 | |
a1f7164c | 7627 | static int cpu_cfs_stat_show(struct seq_file *sf, void *v) |
e8da1b18 | 7628 | { |
2da8ca82 | 7629 | struct task_group *tg = css_tg(seq_css(sf)); |
029632fb | 7630 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; |
e8da1b18 | 7631 | |
44ffc75b TH |
7632 | seq_printf(sf, "nr_periods %d\n", cfs_b->nr_periods); |
7633 | seq_printf(sf, "nr_throttled %d\n", cfs_b->nr_throttled); | |
7634 | seq_printf(sf, "throttled_time %llu\n", cfs_b->throttled_time); | |
e8da1b18 | 7635 | |
3d6c50c2 YW |
7636 | if (schedstat_enabled() && tg != &root_task_group) { |
7637 | u64 ws = 0; | |
7638 | int i; | |
7639 | ||
7640 | for_each_possible_cpu(i) | |
7641 | ws += schedstat_val(tg->se[i]->statistics.wait_sum); | |
7642 | ||
7643 | seq_printf(sf, "wait_sum %llu\n", ws); | |
7644 | } | |
7645 | ||
e8da1b18 NR |
7646 | return 0; |
7647 | } | |
ab84d31e | 7648 | #endif /* CONFIG_CFS_BANDWIDTH */ |
6d6bc0ad | 7649 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
68318b8e | 7650 | |
052f1dc7 | 7651 | #ifdef CONFIG_RT_GROUP_SCHED |
182446d0 TH |
7652 | static int cpu_rt_runtime_write(struct cgroup_subsys_state *css, |
7653 | struct cftype *cft, s64 val) | |
6f505b16 | 7654 | { |
182446d0 | 7655 | return sched_group_set_rt_runtime(css_tg(css), val); |
6f505b16 PZ |
7656 | } |
7657 | ||
182446d0 TH |
7658 | static s64 cpu_rt_runtime_read(struct cgroup_subsys_state *css, |
7659 | struct cftype *cft) | |
6f505b16 | 7660 | { |
182446d0 | 7661 | return sched_group_rt_runtime(css_tg(css)); |
6f505b16 | 7662 | } |
d0b27fa7 | 7663 | |
182446d0 TH |
7664 | static int cpu_rt_period_write_uint(struct cgroup_subsys_state *css, |
7665 | struct cftype *cftype, u64 rt_period_us) | |
d0b27fa7 | 7666 | { |
182446d0 | 7667 | return sched_group_set_rt_period(css_tg(css), rt_period_us); |
d0b27fa7 PZ |
7668 | } |
7669 | ||
182446d0 TH |
7670 | static u64 cpu_rt_period_read_uint(struct cgroup_subsys_state *css, |
7671 | struct cftype *cft) | |
d0b27fa7 | 7672 | { |
182446d0 | 7673 | return sched_group_rt_period(css_tg(css)); |
d0b27fa7 | 7674 | } |
6d6bc0ad | 7675 | #endif /* CONFIG_RT_GROUP_SCHED */ |
6f505b16 | 7676 | |
a1f7164c | 7677 | static struct cftype cpu_legacy_files[] = { |
052f1dc7 | 7678 | #ifdef CONFIG_FAIR_GROUP_SCHED |
fe5c7cc2 PM |
7679 | { |
7680 | .name = "shares", | |
f4c753b7 PM |
7681 | .read_u64 = cpu_shares_read_u64, |
7682 | .write_u64 = cpu_shares_write_u64, | |
fe5c7cc2 | 7683 | }, |
052f1dc7 | 7684 | #endif |
ab84d31e PT |
7685 | #ifdef CONFIG_CFS_BANDWIDTH |
7686 | { | |
7687 | .name = "cfs_quota_us", | |
7688 | .read_s64 = cpu_cfs_quota_read_s64, | |
7689 | .write_s64 = cpu_cfs_quota_write_s64, | |
7690 | }, | |
7691 | { | |
7692 | .name = "cfs_period_us", | |
7693 | .read_u64 = cpu_cfs_period_read_u64, | |
7694 | .write_u64 = cpu_cfs_period_write_u64, | |
7695 | }, | |
e8da1b18 NR |
7696 | { |
7697 | .name = "stat", | |
a1f7164c | 7698 | .seq_show = cpu_cfs_stat_show, |
e8da1b18 | 7699 | }, |
ab84d31e | 7700 | #endif |
052f1dc7 | 7701 | #ifdef CONFIG_RT_GROUP_SCHED |
6f505b16 | 7702 | { |
9f0c1e56 | 7703 | .name = "rt_runtime_us", |
06ecb27c PM |
7704 | .read_s64 = cpu_rt_runtime_read, |
7705 | .write_s64 = cpu_rt_runtime_write, | |
6f505b16 | 7706 | }, |
d0b27fa7 PZ |
7707 | { |
7708 | .name = "rt_period_us", | |
f4c753b7 PM |
7709 | .read_u64 = cpu_rt_period_read_uint, |
7710 | .write_u64 = cpu_rt_period_write_uint, | |
d0b27fa7 | 7711 | }, |
2480c093 PB |
7712 | #endif |
7713 | #ifdef CONFIG_UCLAMP_TASK_GROUP | |
7714 | { | |
7715 | .name = "uclamp.min", | |
7716 | .flags = CFTYPE_NOT_ON_ROOT, | |
7717 | .seq_show = cpu_uclamp_min_show, | |
7718 | .write = cpu_uclamp_min_write, | |
7719 | }, | |
7720 | { | |
7721 | .name = "uclamp.max", | |
7722 | .flags = CFTYPE_NOT_ON_ROOT, | |
7723 | .seq_show = cpu_uclamp_max_show, | |
7724 | .write = cpu_uclamp_max_write, | |
7725 | }, | |
052f1dc7 | 7726 | #endif |
d1ccc66d | 7727 | { } /* Terminate */ |
68318b8e SV |
7728 | }; |
7729 | ||
d41bf8c9 TH |
7730 | static int cpu_extra_stat_show(struct seq_file *sf, |
7731 | struct cgroup_subsys_state *css) | |
0d593634 | 7732 | { |
0d593634 TH |
7733 | #ifdef CONFIG_CFS_BANDWIDTH |
7734 | { | |
d41bf8c9 | 7735 | struct task_group *tg = css_tg(css); |
0d593634 TH |
7736 | struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth; |
7737 | u64 throttled_usec; | |
7738 | ||
7739 | throttled_usec = cfs_b->throttled_time; | |
7740 | do_div(throttled_usec, NSEC_PER_USEC); | |
7741 | ||
7742 | seq_printf(sf, "nr_periods %d\n" | |
7743 | "nr_throttled %d\n" | |
7744 | "throttled_usec %llu\n", | |
7745 | cfs_b->nr_periods, cfs_b->nr_throttled, | |
7746 | throttled_usec); | |
7747 | } | |
7748 | #endif | |
7749 | return 0; | |
7750 | } | |
7751 | ||
7752 | #ifdef CONFIG_FAIR_GROUP_SCHED | |
7753 | static u64 cpu_weight_read_u64(struct cgroup_subsys_state *css, | |
7754 | struct cftype *cft) | |
7755 | { | |
7756 | struct task_group *tg = css_tg(css); | |
7757 | u64 weight = scale_load_down(tg->shares); | |
7758 | ||
7759 | return DIV_ROUND_CLOSEST_ULL(weight * CGROUP_WEIGHT_DFL, 1024); | |
7760 | } | |
7761 | ||
7762 | static int cpu_weight_write_u64(struct cgroup_subsys_state *css, | |
7763 | struct cftype *cft, u64 weight) | |
7764 | { | |
7765 | /* | |
7766 | * cgroup weight knobs should use the common MIN, DFL and MAX | |
7767 | * values which are 1, 100 and 10000 respectively. While it loses | |
7768 | * a bit of range on both ends, it maps pretty well onto the shares | |
7769 | * value used by scheduler and the round-trip conversions preserve | |
7770 | * the original value over the entire range. | |
7771 | */ | |
7772 | if (weight < CGROUP_WEIGHT_MIN || weight > CGROUP_WEIGHT_MAX) | |
7773 | return -ERANGE; | |
7774 | ||
7775 | weight = DIV_ROUND_CLOSEST_ULL(weight * 1024, CGROUP_WEIGHT_DFL); | |
7776 | ||
7777 | return sched_group_set_shares(css_tg(css), scale_load(weight)); | |
7778 | } | |
7779 | ||
7780 | static s64 cpu_weight_nice_read_s64(struct cgroup_subsys_state *css, | |
7781 | struct cftype *cft) | |
7782 | { | |
7783 | unsigned long weight = scale_load_down(css_tg(css)->shares); | |
7784 | int last_delta = INT_MAX; | |
7785 | int prio, delta; | |
7786 | ||
7787 | /* find the closest nice value to the current weight */ | |
7788 | for (prio = 0; prio < ARRAY_SIZE(sched_prio_to_weight); prio++) { | |
7789 | delta = abs(sched_prio_to_weight[prio] - weight); | |
7790 | if (delta >= last_delta) | |
7791 | break; | |
7792 | last_delta = delta; | |
7793 | } | |
7794 | ||
7795 | return PRIO_TO_NICE(prio - 1 + MAX_RT_PRIO); | |
7796 | } | |
7797 | ||
7798 | static int cpu_weight_nice_write_s64(struct cgroup_subsys_state *css, | |
7799 | struct cftype *cft, s64 nice) | |
7800 | { | |
7801 | unsigned long weight; | |
7281c8de | 7802 | int idx; |
0d593634 TH |
7803 | |
7804 | if (nice < MIN_NICE || nice > MAX_NICE) | |
7805 | return -ERANGE; | |
7806 | ||
7281c8de PZ |
7807 | idx = NICE_TO_PRIO(nice) - MAX_RT_PRIO; |
7808 | idx = array_index_nospec(idx, 40); | |
7809 | weight = sched_prio_to_weight[idx]; | |
7810 | ||
0d593634 TH |
7811 | return sched_group_set_shares(css_tg(css), scale_load(weight)); |
7812 | } | |
7813 | #endif | |
7814 | ||
7815 | static void __maybe_unused cpu_period_quota_print(struct seq_file *sf, | |
7816 | long period, long quota) | |
7817 | { | |
7818 | if (quota < 0) | |
7819 | seq_puts(sf, "max"); | |
7820 | else | |
7821 | seq_printf(sf, "%ld", quota); | |
7822 | ||
7823 | seq_printf(sf, " %ld\n", period); | |
7824 | } | |
7825 | ||
7826 | /* caller should put the current value in *@periodp before calling */ | |
7827 | static int __maybe_unused cpu_period_quota_parse(char *buf, | |
7828 | u64 *periodp, u64 *quotap) | |
7829 | { | |
7830 | char tok[21]; /* U64_MAX */ | |
7831 | ||
4c47acd8 | 7832 | if (sscanf(buf, "%20s %llu", tok, periodp) < 1) |
0d593634 TH |
7833 | return -EINVAL; |
7834 | ||
7835 | *periodp *= NSEC_PER_USEC; | |
7836 | ||
7837 | if (sscanf(tok, "%llu", quotap)) | |
7838 | *quotap *= NSEC_PER_USEC; | |
7839 | else if (!strcmp(tok, "max")) | |
7840 | *quotap = RUNTIME_INF; | |
7841 | else | |
7842 | return -EINVAL; | |
7843 | ||
7844 | return 0; | |
7845 | } | |
7846 | ||
7847 | #ifdef CONFIG_CFS_BANDWIDTH | |
7848 | static int cpu_max_show(struct seq_file *sf, void *v) | |
7849 | { | |
7850 | struct task_group *tg = css_tg(seq_css(sf)); | |
7851 | ||
7852 | cpu_period_quota_print(sf, tg_get_cfs_period(tg), tg_get_cfs_quota(tg)); | |
7853 | return 0; | |
7854 | } | |
7855 | ||
7856 | static ssize_t cpu_max_write(struct kernfs_open_file *of, | |
7857 | char *buf, size_t nbytes, loff_t off) | |
7858 | { | |
7859 | struct task_group *tg = css_tg(of_css(of)); | |
7860 | u64 period = tg_get_cfs_period(tg); | |
7861 | u64 quota; | |
7862 | int ret; | |
7863 | ||
7864 | ret = cpu_period_quota_parse(buf, &period, "a); | |
7865 | if (!ret) | |
7866 | ret = tg_set_cfs_bandwidth(tg, period, quota); | |
7867 | return ret ?: nbytes; | |
7868 | } | |
7869 | #endif | |
7870 | ||
7871 | static struct cftype cpu_files[] = { | |
0d593634 TH |
7872 | #ifdef CONFIG_FAIR_GROUP_SCHED |
7873 | { | |
7874 | .name = "weight", | |
7875 | .flags = CFTYPE_NOT_ON_ROOT, | |
7876 | .read_u64 = cpu_weight_read_u64, | |
7877 | .write_u64 = cpu_weight_write_u64, | |
7878 | }, | |
7879 | { | |
7880 | .name = "weight.nice", | |
7881 | .flags = CFTYPE_NOT_ON_ROOT, | |
7882 | .read_s64 = cpu_weight_nice_read_s64, | |
7883 | .write_s64 = cpu_weight_nice_write_s64, | |
7884 | }, | |
7885 | #endif | |
7886 | #ifdef CONFIG_CFS_BANDWIDTH | |
7887 | { | |
7888 | .name = "max", | |
7889 | .flags = CFTYPE_NOT_ON_ROOT, | |
7890 | .seq_show = cpu_max_show, | |
7891 | .write = cpu_max_write, | |
7892 | }, | |
2480c093 PB |
7893 | #endif |
7894 | #ifdef CONFIG_UCLAMP_TASK_GROUP | |
7895 | { | |
7896 | .name = "uclamp.min", | |
7897 | .flags = CFTYPE_NOT_ON_ROOT, | |
7898 | .seq_show = cpu_uclamp_min_show, | |
7899 | .write = cpu_uclamp_min_write, | |
7900 | }, | |
7901 | { | |
7902 | .name = "uclamp.max", | |
7903 | .flags = CFTYPE_NOT_ON_ROOT, | |
7904 | .seq_show = cpu_uclamp_max_show, | |
7905 | .write = cpu_uclamp_max_write, | |
7906 | }, | |
0d593634 TH |
7907 | #endif |
7908 | { } /* terminate */ | |
7909 | }; | |
7910 | ||
073219e9 | 7911 | struct cgroup_subsys cpu_cgrp_subsys = { |
92fb9748 | 7912 | .css_alloc = cpu_cgroup_css_alloc, |
96b77745 | 7913 | .css_online = cpu_cgroup_css_online, |
2f5177f0 | 7914 | .css_released = cpu_cgroup_css_released, |
92fb9748 | 7915 | .css_free = cpu_cgroup_css_free, |
d41bf8c9 | 7916 | .css_extra_stat_show = cpu_extra_stat_show, |
eeb61e53 | 7917 | .fork = cpu_cgroup_fork, |
bb9d97b6 TH |
7918 | .can_attach = cpu_cgroup_can_attach, |
7919 | .attach = cpu_cgroup_attach, | |
a1f7164c | 7920 | .legacy_cftypes = cpu_legacy_files, |
0d593634 | 7921 | .dfl_cftypes = cpu_files, |
b38e42e9 | 7922 | .early_init = true, |
0d593634 | 7923 | .threaded = true, |
68318b8e SV |
7924 | }; |
7925 | ||
052f1dc7 | 7926 | #endif /* CONFIG_CGROUP_SCHED */ |
d842de87 | 7927 | |
b637a328 PM |
7928 | void dump_cpu_task(int cpu) |
7929 | { | |
7930 | pr_info("Task dump for CPU %d:\n", cpu); | |
7931 | sched_show_task(cpu_curr(cpu)); | |
7932 | } | |
ed82b8a1 AK |
7933 | |
7934 | /* | |
7935 | * Nice levels are multiplicative, with a gentle 10% change for every | |
7936 | * nice level changed. I.e. when a CPU-bound task goes from nice 0 to | |
7937 | * nice 1, it will get ~10% less CPU time than another CPU-bound task | |
7938 | * that remained on nice 0. | |
7939 | * | |
7940 | * The "10% effect" is relative and cumulative: from _any_ nice level, | |
7941 | * if you go up 1 level, it's -10% CPU usage, if you go down 1 level | |
7942 | * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25. | |
7943 | * If a task goes up by ~10% and another task goes down by ~10% then | |
7944 | * the relative distance between them is ~25%.) | |
7945 | */ | |
7946 | const int sched_prio_to_weight[40] = { | |
7947 | /* -20 */ 88761, 71755, 56483, 46273, 36291, | |
7948 | /* -15 */ 29154, 23254, 18705, 14949, 11916, | |
7949 | /* -10 */ 9548, 7620, 6100, 4904, 3906, | |
7950 | /* -5 */ 3121, 2501, 1991, 1586, 1277, | |
7951 | /* 0 */ 1024, 820, 655, 526, 423, | |
7952 | /* 5 */ 335, 272, 215, 172, 137, | |
7953 | /* 10 */ 110, 87, 70, 56, 45, | |
7954 | /* 15 */ 36, 29, 23, 18, 15, | |
7955 | }; | |
7956 | ||
7957 | /* | |
7958 | * Inverse (2^32/x) values of the sched_prio_to_weight[] array, precalculated. | |
7959 | * | |
7960 | * In cases where the weight does not change often, we can use the | |
7961 | * precalculated inverse to speed up arithmetics by turning divisions | |
7962 | * into multiplications: | |
7963 | */ | |
7964 | const u32 sched_prio_to_wmult[40] = { | |
7965 | /* -20 */ 48388, 59856, 76040, 92818, 118348, | |
7966 | /* -15 */ 147320, 184698, 229616, 287308, 360437, | |
7967 | /* -10 */ 449829, 563644, 704093, 875809, 1099582, | |
7968 | /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326, | |
7969 | /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587, | |
7970 | /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126, | |
7971 | /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717, | |
7972 | /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153, | |
7973 | }; | |
14a7405b IM |
7974 | |
7975 | #undef CREATE_TRACE_POINTS |