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