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