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