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