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