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