blob: 09fd62568ba949abb70e3a941c4286dbfa9f55e5 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Peter Zijlstra391e43d2011-11-15 17:14:39 +01003 * kernel/sched/core.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Ingo Molnard1ccc662017-02-01 11:46:42 +01005 * Core kernel scheduler code and related syscalls
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Copyright (C) 1991-2002 Linus Torvalds
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
Phil Auld9d246052020-06-29 15:23:03 -04009#define CREATE_TRACE_POINTS
10#include <trace/events/sched.h>
11#undef CREATE_TRACE_POINTS
12
Ingo Molnar325ea102018-03-03 12:20:47 +010013#include "sched.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Peter Zijlstra7281c8d2018-04-20 14:29:51 +020015#include <linux/nospec.h>
Peter Zijlstra85f1abe2018-05-01 18:14:45 +020016
Mark Rutland0ed557a2018-06-14 15:27:41 -070017#include <linux/kcov.h>
Sami Tolvanend08b9f02020-04-27 09:00:07 -070018#include <linux/scs.h>
Mark Rutland0ed557a2018-06-14 15:27:41 -070019
David Howells96f951e2012-03-28 18:30:03 +010020#include <asm/switch_to.h>
Eric Dumazet5517d862007-05-08 00:32:57 -070021#include <asm/tlb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Tejun Heoea138442013-01-18 14:05:55 -080023#include "../workqueue_internal.h"
Jens Axboe771b53d02019-10-22 10:25:58 -060024#include "../../fs/io-wq.h"
Thomas Gleixner29d5e042012-04-20 13:05:45 +000025#include "../smpboot.h"
Gregory Haskins6e0534f2008-05-12 21:21:01 +020026
Vincent Guittot91c27492018-06-28 17:45:09 +020027#include "pelt.h"
Ingo Molnar1f8db412020-05-28 11:01:34 +020028#include "smp.h"
Vincent Guittot91c27492018-06-28 17:45:09 +020029
Qais Yousefa056a5b2019-06-04 12:14:59 +010030/*
31 * Export tracepoints that act as a bare tracehook (ie: have no trace event
32 * associated with them) to allow external modules to probe them.
33 */
34EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_cfs_tp);
35EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_rt_tp);
36EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_dl_tp);
37EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_irq_tp);
38EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_se_tp);
39EXPORT_TRACEPOINT_SYMBOL_GPL(sched_overutilized_tp);
Vincent Donnefort4581bea2020-05-27 17:39:14 +010040EXPORT_TRACEPOINT_SYMBOL_GPL(sched_util_est_cfs_tp);
41EXPORT_TRACEPOINT_SYMBOL_GPL(sched_util_est_se_tp);
Phil Auld9d246052020-06-29 15:23:03 -040042EXPORT_TRACEPOINT_SYMBOL_GPL(sched_update_nr_running_tp);
Qais Yousefa056a5b2019-06-04 12:14:59 +010043
Peter Zijlstra029632f2011-10-25 10:00:11 +020044DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +020045
Masahiro Yamadae9666d12018-12-31 00:14:15 +090046#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_JUMP_LABEL)
Ingo Molnare436d802007-07-19 21:28:35 +020047/*
Ingo Molnarbf5c91b2007-10-15 17:00:04 +020048 * Debugging: various feature bits
Patrick Bellasi765cc3a2017-11-08 18:41:01 +000049 *
50 * If SCHED_DEBUG is disabled, each compilation unit has its own copy of
51 * sysctl_sched_features, defined in sched.h, to allow constants propagation
52 * at compile time and compiler optimization based on features default.
Ingo Molnarbf5c91b2007-10-15 17:00:04 +020053 */
Peter Zijlstraf00b45c2008-04-19 19:45:00 +020054#define SCHED_FEAT(name, enabled) \
Peter Zijlstraf00b45c2008-04-19 19:45:00 +020055 (1UL << __SCHED_FEAT_##name) * enabled |
Peter Zijlstraf00b45c2008-04-19 19:45:00 +020056const_debug unsigned int sysctl_sched_features =
Peter Zijlstra391e43d2011-11-15 17:14:39 +010057#include "features.h"
Peter Zijlstraf00b45c2008-04-19 19:45:00 +020058 0;
Peter Zijlstraf00b45c2008-04-19 19:45:00 +020059#undef SCHED_FEAT
Patrick Bellasi765cc3a2017-11-08 18:41:01 +000060#endif
Peter Zijlstraf00b45c2008-04-19 19:45:00 +020061
Ingo Molnarbf5c91b2007-10-15 17:00:04 +020062/*
Peter Zijlstrab82d9fd2007-11-09 22:39:39 +010063 * Number of tasks to iterate in a single balance run.
64 * Limited because this is done with IRQs disabled.
65 */
66const_debug unsigned int sysctl_sched_nr_migrate = 32;
67
68/*
Ingo Molnard1ccc662017-02-01 11:46:42 +010069 * period over which we measure -rt task CPU usage in us.
Peter Zijlstrafa85ae22008-01-25 21:08:29 +010070 * default: 1s
71 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +010072unsigned int sysctl_sched_rt_period = 1000000;
Peter Zijlstrafa85ae22008-01-25 21:08:29 +010073
Peter Zijlstra029632f2011-10-25 10:00:11 +020074__read_mostly int scheduler_running;
Ingo Molnar6892b752008-02-13 14:02:36 +010075
Peter Zijlstrafa85ae22008-01-25 21:08:29 +010076/*
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +010077 * part of the period that we allow rt tasks to run in us.
78 * default: 0.95s
Peter Zijlstrafa85ae22008-01-25 21:08:29 +010079 */
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +010080int sysctl_sched_rt_runtime = 950000;
81
Peter Zijlstra58877d32020-07-02 14:52:11 +020082
83/*
84 * Serialization rules:
85 *
86 * Lock order:
87 *
88 * p->pi_lock
89 * rq->lock
90 * hrtimer_cpu_base->lock (hrtimer_start() for bandwidth controls)
91 *
92 * rq1->lock
93 * rq2->lock where: rq1 < rq2
94 *
95 * Regular state:
96 *
97 * Normal scheduling state is serialized by rq->lock. __schedule() takes the
98 * local CPU's rq->lock, it optionally removes the task from the runqueue and
99 * always looks at the local rq data structures to find the most elegible task
100 * to run next.
101 *
102 * Task enqueue is also under rq->lock, possibly taken from another CPU.
103 * Wakeups from another LLC domain might use an IPI to transfer the enqueue to
104 * the local CPU to avoid bouncing the runqueue state around [ see
105 * ttwu_queue_wakelist() ]
106 *
107 * Task wakeup, specifically wakeups that involve migration, are horribly
108 * complicated to avoid having to take two rq->locks.
109 *
110 * Special state:
111 *
112 * System-calls and anything external will use task_rq_lock() which acquires
113 * both p->pi_lock and rq->lock. As a consequence the state they change is
114 * stable while holding either lock:
115 *
116 * - sched_setaffinity()/
117 * set_cpus_allowed_ptr(): p->cpus_ptr, p->nr_cpus_allowed
118 * - set_user_nice(): p->se.load, p->*prio
119 * - __sched_setscheduler(): p->sched_class, p->policy, p->*prio,
120 * p->se.load, p->rt_priority,
121 * p->dl.dl_{runtime, deadline, period, flags, bw, density}
122 * - sched_setnuma(): p->numa_preferred_nid
123 * - sched_move_task()/
124 * cpu_cgroup_fork(): p->sched_task_group
125 * - uclamp_update_active() p->uclamp*
126 *
127 * p->state <- TASK_*:
128 *
129 * is changed locklessly using set_current_state(), __set_current_state() or
130 * set_special_state(), see their respective comments, or by
131 * try_to_wake_up(). This latter uses p->pi_lock to serialize against
132 * concurrent self.
133 *
134 * p->on_rq <- { 0, 1 = TASK_ON_RQ_QUEUED, 2 = TASK_ON_RQ_MIGRATING }:
135 *
136 * is set by activate_task() and cleared by deactivate_task(), under
137 * rq->lock. Non-zero indicates the task is runnable, the special
138 * ON_RQ_MIGRATING state is used for migration without holding both
139 * rq->locks. It indicates task_cpu() is not stable, see task_rq_lock().
140 *
141 * p->on_cpu <- { 0, 1 }:
142 *
143 * is set by prepare_task() and cleared by finish_task() such that it will be
144 * set before p is scheduled-in and cleared after p is scheduled-out, both
145 * under rq->lock. Non-zero indicates the task is running on its CPU.
146 *
147 * [ The astute reader will observe that it is possible for two tasks on one
148 * CPU to have ->on_cpu = 1 at the same time. ]
149 *
150 * task_cpu(p): is changed by set_task_cpu(), the rules are:
151 *
152 * - Don't call set_task_cpu() on a blocked task:
153 *
154 * We don't care what CPU we're not running on, this simplifies hotplug,
155 * the CPU assignment of blocked tasks isn't required to be valid.
156 *
157 * - for try_to_wake_up(), called under p->pi_lock:
158 *
159 * This allows try_to_wake_up() to only take one rq->lock, see its comment.
160 *
161 * - for migration called under rq->lock:
162 * [ see task_on_rq_migrating() in task_rq_lock() ]
163 *
164 * o move_queued_task()
165 * o detach_task()
166 *
167 * - for migration called under double_rq_lock():
168 *
169 * o __migrate_swap_task()
170 * o push_rt_task() / pull_rt_task()
171 * o push_dl_task() / pull_dl_task()
172 * o dl_task_offline_migration()
173 *
174 */
175
Dario Faggioli332ac172013-11-07 14:43:45 +0100176/*
Peter Zijlstra3e71a462016-04-28 16:16:33 +0200177 * __task_rq_lock - lock the rq @p resides on.
178 */
Peter Zijlstraeb580752015-07-31 21:28:18 +0200179struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
Peter Zijlstra3e71a462016-04-28 16:16:33 +0200180 __acquires(rq->lock)
181{
182 struct rq *rq;
183
184 lockdep_assert_held(&p->pi_lock);
185
186 for (;;) {
187 rq = task_rq(p);
188 raw_spin_lock(&rq->lock);
189 if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) {
Matt Flemingd8ac8972016-09-21 14:38:10 +0100190 rq_pin_lock(rq, rf);
Peter Zijlstra3e71a462016-04-28 16:16:33 +0200191 return rq;
192 }
193 raw_spin_unlock(&rq->lock);
194
195 while (unlikely(task_on_rq_migrating(p)))
196 cpu_relax();
197 }
198}
199
200/*
201 * task_rq_lock - lock p->pi_lock and lock the rq @p resides on.
202 */
Peter Zijlstraeb580752015-07-31 21:28:18 +0200203struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
Peter Zijlstra3e71a462016-04-28 16:16:33 +0200204 __acquires(p->pi_lock)
205 __acquires(rq->lock)
206{
207 struct rq *rq;
208
209 for (;;) {
Peter Zijlstraeb580752015-07-31 21:28:18 +0200210 raw_spin_lock_irqsave(&p->pi_lock, rf->flags);
Peter Zijlstra3e71a462016-04-28 16:16:33 +0200211 rq = task_rq(p);
212 raw_spin_lock(&rq->lock);
213 /*
214 * move_queued_task() task_rq_lock()
215 *
216 * ACQUIRE (rq->lock)
217 * [S] ->on_rq = MIGRATING [L] rq = task_rq()
218 * WMB (__set_task_cpu()) ACQUIRE (rq->lock);
219 * [S] ->cpu = new_cpu [L] task_rq()
220 * [L] ->on_rq
221 * RELEASE (rq->lock)
222 *
Andrea Parric5469512019-01-21 16:52:40 +0100223 * If we observe the old CPU in task_rq_lock(), the acquire of
Peter Zijlstra3e71a462016-04-28 16:16:33 +0200224 * the old rq->lock will fully serialize against the stores.
225 *
Andrea Parric5469512019-01-21 16:52:40 +0100226 * If we observe the new CPU in task_rq_lock(), the address
227 * dependency headed by '[L] rq = task_rq()' and the acquire
228 * will pair with the WMB to ensure we then also see migrating.
Peter Zijlstra3e71a462016-04-28 16:16:33 +0200229 */
230 if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) {
Matt Flemingd8ac8972016-09-21 14:38:10 +0100231 rq_pin_lock(rq, rf);
Peter Zijlstra3e71a462016-04-28 16:16:33 +0200232 return rq;
233 }
234 raw_spin_unlock(&rq->lock);
Peter Zijlstraeb580752015-07-31 21:28:18 +0200235 raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
Peter Zijlstra3e71a462016-04-28 16:16:33 +0200236
237 while (unlikely(task_on_rq_migrating(p)))
238 cpu_relax();
239 }
240}
241
Ingo Molnar535b9552017-02-01 12:29:21 +0100242/*
243 * RQ-clock updating methods:
244 */
245
246static void update_rq_clock_task(struct rq *rq, s64 delta)
247{
248/*
249 * In theory, the compile should just see 0 here, and optimize out the call
250 * to sched_rt_avg_update. But I don't trust it...
251 */
Vincent Guittot11d4afd2018-09-25 11:17:42 +0200252 s64 __maybe_unused steal = 0, irq_delta = 0;
253
Ingo Molnar535b9552017-02-01 12:29:21 +0100254#ifdef CONFIG_IRQ_TIME_ACCOUNTING
255 irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time;
256
257 /*
258 * Since irq_time is only updated on {soft,}irq_exit, we might run into
259 * this case when a previous update_rq_clock() happened inside a
260 * {soft,}irq region.
261 *
262 * When this happens, we stop ->clock_task and only update the
263 * prev_irq_time stamp to account for the part that fit, so that a next
264 * update will consume the rest. This ensures ->clock_task is
265 * monotonic.
266 *
267 * It does however cause some slight miss-attribution of {soft,}irq
268 * time, a more accurate solution would be to update the irq_time using
269 * the current rq->clock timestamp, except that would require using
270 * atomic ops.
271 */
272 if (irq_delta > delta)
273 irq_delta = delta;
274
275 rq->prev_irq_time += irq_delta;
276 delta -= irq_delta;
277#endif
278#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
279 if (static_key_false((&paravirt_steal_rq_enabled))) {
280 steal = paravirt_steal_clock(cpu_of(rq));
281 steal -= rq->prev_steal_time_rq;
282
283 if (unlikely(steal > delta))
284 steal = delta;
285
286 rq->prev_steal_time_rq += steal;
287 delta -= steal;
288 }
289#endif
290
291 rq->clock_task += delta;
292
Vincent Guittot11d4afd2018-09-25 11:17:42 +0200293#ifdef CONFIG_HAVE_SCHED_AVG_IRQ
Ingo Molnar535b9552017-02-01 12:29:21 +0100294 if ((irq_delta + steal) && sched_feat(NONTASK_CAPACITY))
Vincent Guittot91c27492018-06-28 17:45:09 +0200295 update_irq_load_avg(rq, irq_delta + steal);
Ingo Molnar535b9552017-02-01 12:29:21 +0100296#endif
Vincent Guittot23127292019-01-23 16:26:53 +0100297 update_rq_clock_pelt(rq, delta);
Ingo Molnar535b9552017-02-01 12:29:21 +0100298}
299
300void update_rq_clock(struct rq *rq)
301{
302 s64 delta;
303
304 lockdep_assert_held(&rq->lock);
305
306 if (rq->clock_update_flags & RQCF_ACT_SKIP)
307 return;
308
309#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra26ae58d2016-10-03 16:53:49 +0200310 if (sched_feat(WARN_DOUBLE_CLOCK))
311 SCHED_WARN_ON(rq->clock_update_flags & RQCF_UPDATED);
Ingo Molnar535b9552017-02-01 12:29:21 +0100312 rq->clock_update_flags |= RQCF_UPDATED;
313#endif
Peter Zijlstra26ae58d2016-10-03 16:53:49 +0200314
Ingo Molnar535b9552017-02-01 12:29:21 +0100315 delta = sched_clock_cpu(cpu_of(rq)) - rq->clock;
316 if (delta < 0)
317 return;
318 rq->clock += delta;
319 update_rq_clock_task(rq, delta);
320}
321
Peter Zijlstra (Intel)90b53632020-03-27 11:44:56 +0100322static inline void
323rq_csd_init(struct rq *rq, call_single_data_t *csd, smp_call_func_t func)
324{
325 csd->flags = 0;
326 csd->func = func;
327 csd->info = rq;
328}
Ingo Molnar535b9552017-02-01 12:29:21 +0100329
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100330#ifdef CONFIG_SCHED_HRTICK
331/*
332 * Use HR-timers to deliver accurate preemption points.
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100333 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100334
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100335static void hrtick_clear(struct rq *rq)
336{
337 if (hrtimer_active(&rq->hrtick_timer))
338 hrtimer_cancel(&rq->hrtick_timer);
339}
340
341/*
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100342 * High-resolution timer tick.
343 * Runs from hardirq context with interrupts disabled.
344 */
345static enum hrtimer_restart hrtick(struct hrtimer *timer)
346{
347 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +0200348 struct rq_flags rf;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100349
350 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
351
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +0200352 rq_lock(rq, &rf);
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200353 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100354 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +0200355 rq_unlock(rq, &rf);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100356
357 return HRTIMER_NORESTART;
358}
359
Rabin Vincent95e904c2008-05-11 05:55:33 +0530360#ifdef CONFIG_SMP
Peter Zijlstra971ee282013-06-28 11:18:53 +0200361
Thomas Gleixner4961b6e2015-04-14 21:09:05 +0000362static void __hrtick_restart(struct rq *rq)
Peter Zijlstra971ee282013-06-28 11:18:53 +0200363{
364 struct hrtimer *timer = &rq->hrtick_timer;
Peter Zijlstra971ee282013-06-28 11:18:53 +0200365
Sebastian Andrzej Siewiord5096aa2019-07-26 20:30:52 +0200366 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED_HARD);
Peter Zijlstra971ee282013-06-28 11:18:53 +0200367}
368
Peter Zijlstra31656512008-07-18 18:01:23 +0200369/*
370 * called from hardirq (IPI) context
371 */
372static void __hrtick_start(void *arg)
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200373{
Peter Zijlstra31656512008-07-18 18:01:23 +0200374 struct rq *rq = arg;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +0200375 struct rq_flags rf;
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200376
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +0200377 rq_lock(rq, &rf);
Peter Zijlstra971ee282013-06-28 11:18:53 +0200378 __hrtick_restart(rq);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +0200379 rq_unlock(rq, &rf);
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200380}
381
Peter Zijlstra31656512008-07-18 18:01:23 +0200382/*
383 * Called to set the hrtick timer state.
384 *
385 * called with rq->lock held and irqs disabled
386 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200387void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200388{
Peter Zijlstra31656512008-07-18 18:01:23 +0200389 struct hrtimer *timer = &rq->hrtick_timer;
xiaofeng.yan177ef2a2014-08-26 03:15:41 +0000390 ktime_t time;
391 s64 delta;
392
393 /*
394 * Don't schedule slices shorter than 10000ns, that just
395 * doesn't make sense and can cause timer DoS.
396 */
397 delta = max_t(s64, delay, 10000LL);
398 time = ktime_add_ns(timer->base->get_time(), delta);
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200399
Arjan van de Vencc584b22008-09-01 15:02:30 -0700400 hrtimer_set_expires(timer, time);
Peter Zijlstra31656512008-07-18 18:01:23 +0200401
Peter Xufd3eafd2019-12-16 16:31:25 -0500402 if (rq == this_rq())
Peter Zijlstra971ee282013-06-28 11:18:53 +0200403 __hrtick_restart(rq);
Peter Xufd3eafd2019-12-16 16:31:25 -0500404 else
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100405 smp_call_function_single_async(cpu_of(rq), &rq->hrtick_csd);
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200406}
407
Peter Zijlstra31656512008-07-18 18:01:23 +0200408#else
409/*
410 * Called to set the hrtick timer state.
411 *
412 * called with rq->lock held and irqs disabled
413 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200414void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstra31656512008-07-18 18:01:23 +0200415{
Wanpeng Li86893332014-11-26 08:44:06 +0800416 /*
417 * Don't schedule slices shorter than 10000ns, that just
418 * doesn't make sense. Rely on vruntime for fairness.
419 */
420 delay = max_t(u64, delay, 10000LL);
Thomas Gleixner4961b6e2015-04-14 21:09:05 +0000421 hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay),
Sebastian Andrzej Siewiord5096aa2019-07-26 20:30:52 +0200422 HRTIMER_MODE_REL_PINNED_HARD);
Peter Zijlstra31656512008-07-18 18:01:23 +0200423}
Peter Zijlstra (Intel)90b53632020-03-27 11:44:56 +0100424
Rabin Vincent95e904c2008-05-11 05:55:33 +0530425#endif /* CONFIG_SMP */
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200426
Frederic Weisbecker77a021b2018-02-21 05:17:23 +0100427static void hrtick_rq_init(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100428{
Peter Zijlstra31656512008-07-18 18:01:23 +0200429#ifdef CONFIG_SMP
Peter Zijlstra (Intel)90b53632020-03-27 11:44:56 +0100430 rq_csd_init(rq, &rq->hrtick_csd, __hrtick_start);
Peter Zijlstra31656512008-07-18 18:01:23 +0200431#endif
Sebastian Andrzej Siewiord5096aa2019-07-26 20:30:52 +0200432 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100433 rq->hrtick_timer.function = hrtick;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100434}
Andrew Morton006c75f2008-09-22 14:55:46 -0700435#else /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100436static inline void hrtick_clear(struct rq *rq)
437{
438}
439
Frederic Weisbecker77a021b2018-02-21 05:17:23 +0100440static inline void hrtick_rq_init(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100441{
442}
Andrew Morton006c75f2008-09-22 14:55:46 -0700443#endif /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100444
Frederic Weisbecker55295782016-03-24 15:38:01 +0100445/*
446 * cmpxchg based fetch_or, macro so it works for different integer types
447 */
448#define fetch_or(ptr, mask) \
449 ({ \
450 typeof(ptr) _ptr = (ptr); \
451 typeof(mask) _mask = (mask); \
452 typeof(*_ptr) _old, _val = *_ptr; \
453 \
454 for (;;) { \
455 _old = cmpxchg(_ptr, _val, _val | _mask); \
456 if (_old == _val) \
457 break; \
458 _val = _old; \
459 } \
460 _old; \
461})
462
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700463#if defined(CONFIG_SMP) && defined(TIF_POLLING_NRFLAG)
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200464/*
465 * Atomically set TIF_NEED_RESCHED and test for TIF_POLLING_NRFLAG,
466 * this avoids any races wrt polling state changes and thereby avoids
467 * spurious IPIs.
468 */
469static bool set_nr_and_not_polling(struct task_struct *p)
470{
471 struct thread_info *ti = task_thread_info(p);
472 return !(fetch_or(&ti->flags, _TIF_NEED_RESCHED) & _TIF_POLLING_NRFLAG);
473}
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700474
475/*
476 * Atomically set TIF_NEED_RESCHED if TIF_POLLING_NRFLAG is set.
477 *
478 * If this returns true, then the idle task promises to call
479 * sched_ttwu_pending() and reschedule soon.
480 */
481static bool set_nr_if_polling(struct task_struct *p)
482{
483 struct thread_info *ti = task_thread_info(p);
Jason Low316c1608d2015-04-28 13:00:20 -0700484 typeof(ti->flags) old, val = READ_ONCE(ti->flags);
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700485
486 for (;;) {
487 if (!(val & _TIF_POLLING_NRFLAG))
488 return false;
489 if (val & _TIF_NEED_RESCHED)
490 return true;
491 old = cmpxchg(&ti->flags, val, val | _TIF_NEED_RESCHED);
492 if (old == val)
493 break;
494 val = old;
495 }
496 return true;
497}
498
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200499#else
500static bool set_nr_and_not_polling(struct task_struct *p)
501{
502 set_tsk_need_resched(p);
503 return true;
504}
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700505
506#ifdef CONFIG_SMP
507static bool set_nr_if_polling(struct task_struct *p)
508{
509 return false;
510}
511#endif
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200512#endif
513
Davidlohr Bueso07879c62018-12-18 11:53:52 -0800514static bool __wake_q_add(struct wake_q_head *head, struct task_struct *task)
515{
516 struct wake_q_node *node = &task->wake_q;
517
518 /*
519 * Atomically grab the task, if ->wake_q is !nil already it means
520 * its already queued (either by us or someone else) and will get the
521 * wakeup due to that.
522 *
523 * In order to ensure that a pending wakeup will observe our pending
524 * state, even in the failed case, an explicit smp_mb() must be used.
525 */
526 smp_mb__before_atomic();
527 if (unlikely(cmpxchg_relaxed(&node->next, NULL, WAKE_Q_TAIL)))
528 return false;
529
530 /*
531 * The head is context local, there can be no concurrency.
532 */
533 *head->lastp = node;
534 head->lastp = &node->next;
535 return true;
536}
537
Peter Zijlstrae6018c02018-12-17 10:14:53 +0100538/**
539 * wake_q_add() - queue a wakeup for 'later' waking.
540 * @head: the wake_q_head to add @task to
541 * @task: the task to queue for 'later' wakeup
542 *
543 * Queue a task for later wakeup, most likely by the wake_up_q() call in the
544 * same context, _HOWEVER_ this is not guaranteed, the wakeup can come
545 * instantly.
546 *
547 * This function must be used as-if it were wake_up_process(); IOW the task
548 * must be ready to be woken at this location.
549 */
Peter Zijlstra76751042015-05-01 08:27:50 -0700550void wake_q_add(struct wake_q_head *head, struct task_struct *task)
551{
Davidlohr Bueso07879c62018-12-18 11:53:52 -0800552 if (__wake_q_add(head, task))
553 get_task_struct(task);
554}
Peter Zijlstra76751042015-05-01 08:27:50 -0700555
Davidlohr Bueso07879c62018-12-18 11:53:52 -0800556/**
557 * wake_q_add_safe() - safely queue a wakeup for 'later' waking.
558 * @head: the wake_q_head to add @task to
559 * @task: the task to queue for 'later' wakeup
560 *
561 * Queue a task for later wakeup, most likely by the wake_up_q() call in the
562 * same context, _HOWEVER_ this is not guaranteed, the wakeup can come
563 * instantly.
564 *
565 * This function must be used as-if it were wake_up_process(); IOW the task
566 * must be ready to be woken at this location.
567 *
568 * This function is essentially a task-safe equivalent to wake_q_add(). Callers
569 * that already hold reference to @task can call the 'safe' version and trust
570 * wake_q to do the right thing depending whether or not the @task is already
571 * queued for wakeup.
572 */
573void wake_q_add_safe(struct wake_q_head *head, struct task_struct *task)
574{
575 if (!__wake_q_add(head, task))
576 put_task_struct(task);
Peter Zijlstra76751042015-05-01 08:27:50 -0700577}
578
579void wake_up_q(struct wake_q_head *head)
580{
581 struct wake_q_node *node = head->first;
582
583 while (node != WAKE_Q_TAIL) {
584 struct task_struct *task;
585
586 task = container_of(node, struct task_struct, wake_q);
587 BUG_ON(!task);
Ingo Molnard1ccc662017-02-01 11:46:42 +0100588 /* Task can safely be re-inserted now: */
Peter Zijlstra76751042015-05-01 08:27:50 -0700589 node = node->next;
590 task->wake_q.next = NULL;
591
592 /*
Andrea Parri7696f992018-07-16 11:06:03 -0700593 * wake_up_process() executes a full barrier, which pairs with
594 * the queueing in wake_q_add() so as not to miss wakeups.
Peter Zijlstra76751042015-05-01 08:27:50 -0700595 */
596 wake_up_process(task);
597 put_task_struct(task);
598 }
599}
600
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200601/*
Kirill Tkhai88751252014-06-29 00:03:57 +0400602 * resched_curr - mark rq's current task 'to be rescheduled now'.
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200603 *
604 * On UP this means the setting of the need_resched flag, on SMP it
605 * might also involve a cross-CPU call to trigger the scheduler on
606 * the target CPU.
607 */
Kirill Tkhai88751252014-06-29 00:03:57 +0400608void resched_curr(struct rq *rq)
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200609{
Kirill Tkhai88751252014-06-29 00:03:57 +0400610 struct task_struct *curr = rq->curr;
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200611 int cpu;
612
Kirill Tkhai88751252014-06-29 00:03:57 +0400613 lockdep_assert_held(&rq->lock);
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200614
Kirill Tkhai88751252014-06-29 00:03:57 +0400615 if (test_tsk_need_resched(curr))
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200616 return;
617
Kirill Tkhai88751252014-06-29 00:03:57 +0400618 cpu = cpu_of(rq);
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200619
Peter Zijlstraf27dde82013-08-14 14:55:31 +0200620 if (cpu == smp_processor_id()) {
Kirill Tkhai88751252014-06-29 00:03:57 +0400621 set_tsk_need_resched(curr);
Peter Zijlstraf27dde82013-08-14 14:55:31 +0200622 set_preempt_need_resched();
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200623 return;
Peter Zijlstraf27dde82013-08-14 14:55:31 +0200624 }
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200625
Kirill Tkhai88751252014-06-29 00:03:57 +0400626 if (set_nr_and_not_polling(curr))
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200627 smp_send_reschedule(cpu);
Andy Lutomirskidfc68f22014-06-04 10:31:15 -0700628 else
629 trace_sched_wake_idle_without_ipi(cpu);
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200630}
631
Peter Zijlstra029632f2011-10-25 10:00:11 +0200632void resched_cpu(int cpu)
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200633{
634 struct rq *rq = cpu_rq(cpu);
635 unsigned long flags;
636
Paul E. McKenney7c2102e2017-09-18 08:54:40 -0700637 raw_spin_lock_irqsave(&rq->lock, flags);
Paul E. McKenneya0982df2017-10-13 16:24:28 -0700638 if (cpu_online(cpu) || cpu == smp_processor_id())
639 resched_curr(rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100640 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200641}
Thomas Gleixner06d83082008-03-22 09:20:24 +0100642
Peter Zijlstrab021fe32013-09-17 09:30:55 +0200643#ifdef CONFIG_SMP
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200644#ifdef CONFIG_NO_HZ_COMMON
Thomas Gleixner06d83082008-03-22 09:20:24 +0100645/*
Ingo Molnard1ccc662017-02-01 11:46:42 +0100646 * In the semi idle case, use the nearest busy CPU for migrating timers
647 * from an idle CPU. This is good for power-savings.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700648 *
649 * We don't do similar optimization for completely idle system, as
Ingo Molnard1ccc662017-02-01 11:46:42 +0100650 * selecting an idle CPU will add more delays to the timers than intended
651 * (as that CPU's timer base may not be uptodate wrt jiffies etc).
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700652 */
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000653int get_nohz_timer_target(void)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700654{
Wanpeng Lie938b9c2020-01-13 08:50:27 +0800655 int i, cpu = smp_processor_id(), default_cpu = -1;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700656 struct sched_domain *sd;
657
Wanpeng Lie938b9c2020-01-13 08:50:27 +0800658 if (housekeeping_cpu(cpu, HK_FLAG_TIMER)) {
659 if (!idle_cpu(cpu))
660 return cpu;
661 default_cpu = cpu;
662 }
Viresh Kumar6201b4d2014-03-18 16:26:07 +0530663
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200664 rcu_read_lock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700665 for_each_domain(cpu, sd) {
Wanpeng Lie938b9c2020-01-13 08:50:27 +0800666 for_each_cpu_and(i, sched_domain_span(sd),
667 housekeeping_cpumask(HK_FLAG_TIMER)) {
Wanpeng Li44496922016-05-04 14:45:34 +0800668 if (cpu == i)
669 continue;
670
Wanpeng Lie938b9c2020-01-13 08:50:27 +0800671 if (!idle_cpu(i)) {
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200672 cpu = i;
673 goto unlock;
674 }
675 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700676 }
Vatika Harlalka9642d182015-09-01 16:50:59 +0200677
Wanpeng Lie938b9c2020-01-13 08:50:27 +0800678 if (default_cpu == -1)
679 default_cpu = housekeeping_any_cpu(HK_FLAG_TIMER);
680 cpu = default_cpu;
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200681unlock:
682 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700683 return cpu;
684}
Ingo Molnard1ccc662017-02-01 11:46:42 +0100685
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700686/*
Thomas Gleixner06d83082008-03-22 09:20:24 +0100687 * When add_timer_on() enqueues a timer into the timer wheel of an
688 * idle CPU then this timer might expire before the next timer event
689 * which is scheduled to wake up that CPU. In case of a completely
690 * idle system the next event might even be infinite time into the
691 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
692 * leaves the inner idle loop so the newly added timer is taken into
693 * account when the CPU goes back to idle and evaluates the timer
694 * wheel for the next timer event.
695 */
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200696static void wake_up_idle_cpu(int cpu)
Thomas Gleixner06d83082008-03-22 09:20:24 +0100697{
698 struct rq *rq = cpu_rq(cpu);
699
700 if (cpu == smp_processor_id())
701 return;
702
Andy Lutomirski67b9ca72014-06-04 10:31:17 -0700703 if (set_nr_and_not_polling(rq->idle))
Thomas Gleixner06d83082008-03-22 09:20:24 +0100704 smp_send_reschedule(cpu);
Andy Lutomirskidfc68f22014-06-04 10:31:15 -0700705 else
706 trace_sched_wake_idle_without_ipi(cpu);
Thomas Gleixner06d83082008-03-22 09:20:24 +0100707}
Mike Galbraith39c0cbe2010-03-11 17:17:13 +0100708
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200709static bool wake_up_full_nohz_cpu(int cpu)
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200710{
Frederic Weisbecker53c5fa12014-06-04 16:20:21 +0200711 /*
712 * We just need the target to call irq_exit() and re-evaluate
713 * the next tick. The nohz full kick at least implies that.
714 * If needed we can still optimize that later with an
715 * empty IRQ.
716 */
Paul E. McKenney379d9ec2016-06-30 10:37:20 -0700717 if (cpu_is_offline(cpu))
718 return true; /* Don't try to wake offline CPUs. */
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200719 if (tick_nohz_full_cpu(cpu)) {
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200720 if (cpu != smp_processor_id() ||
721 tick_nohz_tick_stopped())
Frederic Weisbecker53c5fa12014-06-04 16:20:21 +0200722 tick_nohz_full_kick_cpu(cpu);
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200723 return true;
724 }
725
726 return false;
727}
728
Paul E. McKenney379d9ec2016-06-30 10:37:20 -0700729/*
730 * Wake up the specified CPU. If the CPU is going offline, it is the
731 * caller's responsibility to deal with the lost wakeup, for example,
732 * by hooking into the CPU_DEAD notifier like timers and hrtimers do.
733 */
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200734void wake_up_nohz_cpu(int cpu)
735{
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200736 if (!wake_up_full_nohz_cpu(cpu))
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200737 wake_up_idle_cpu(cpu);
738}
739
Peter Zijlstra (Intel)90b53632020-03-27 11:44:56 +0100740static void nohz_csd_func(void *info)
Suresh Siddhaca380622011-10-03 15:09:00 -0700741{
Peter Zijlstra (Intel)90b53632020-03-27 11:44:56 +0100742 struct rq *rq = info;
Peter Zijlstra19a1f5e2020-05-26 18:10:58 +0200743 int cpu = cpu_of(rq);
744 unsigned int flags;
Vincent Guittot873b4c62013-06-05 10:13:11 +0200745
746 /*
Peter Zijlstra19a1f5e2020-05-26 18:10:58 +0200747 * Release the rq::nohz_csd.
Vincent Guittot873b4c62013-06-05 10:13:11 +0200748 */
Peter Zijlstra19a1f5e2020-05-26 18:10:58 +0200749 flags = atomic_fetch_andnot(NOHZ_KICK_MASK, nohz_flags(cpu));
750 WARN_ON(!(flags & NOHZ_KICK_MASK));
Suresh Siddhaca380622011-10-03 15:09:00 -0700751
Peter Zijlstra19a1f5e2020-05-26 18:10:58 +0200752 rq->idle_balance = idle_cpu(cpu);
753 if (rq->idle_balance && !need_resched()) {
754 rq->nohz_idle_balance = flags;
Peter Zijlstra (Intel)90b53632020-03-27 11:44:56 +0100755 raise_softirq_irqoff(SCHED_SOFTIRQ);
756 }
Suresh Siddhaca380622011-10-03 15:09:00 -0700757}
758
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200759#endif /* CONFIG_NO_HZ_COMMON */
Thomas Gleixner06d83082008-03-22 09:20:24 +0100760
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200761#ifdef CONFIG_NO_HZ_FULL
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +0200762bool sched_can_stop_tick(struct rq *rq)
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200763{
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +0200764 int fifo_nr_running;
765
766 /* Deadline tasks, even if single, need the tick */
767 if (rq->dl.dl_nr_running)
768 return false;
769
Frederic Weisbecker3882ec62014-03-18 22:54:04 +0100770 /*
Peter Zijlstra2548d542016-04-21 18:03:15 +0200771 * If there are more than one RR tasks, we need the tick to effect the
772 * actual RR behaviour.
Rik van Riel1e78cdb2015-02-16 15:23:49 -0500773 */
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +0200774 if (rq->rt.rr_nr_running) {
775 if (rq->rt.rr_nr_running == 1)
776 return true;
777 else
778 return false;
Rik van Riel1e78cdb2015-02-16 15:23:49 -0500779 }
780
Peter Zijlstra2548d542016-04-21 18:03:15 +0200781 /*
782 * If there's no RR tasks, but FIFO tasks, we can skip the tick, no
783 * forced preemption between FIFO tasks.
784 */
785 fifo_nr_running = rq->rt.rt_nr_running - rq->rt.rr_nr_running;
786 if (fifo_nr_running)
787 return true;
788
789 /*
790 * If there are no DL,RR/FIFO tasks, there must only be CFS tasks left;
791 * if there's more than one we need the tick for involuntary
792 * preemption.
793 */
794 if (rq->nr_running > 1)
Viresh Kumar541b8262014-06-24 14:04:12 +0530795 return false;
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200796
Viresh Kumar541b8262014-06-24 14:04:12 +0530797 return true;
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200798}
799#endif /* CONFIG_NO_HZ_FULL */
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200800#endif /* CONFIG_SMP */
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200801
Paul Turnera790de92011-07-21 09:43:29 -0700802#if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \
803 (defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH)))
Peter Zijlstraeb755802008-08-19 12:33:05 +0200804/*
Paul Turner82774342011-07-21 09:43:35 -0700805 * Iterate task_group tree rooted at *from, calling @down when first entering a
806 * node and @up when leaving it for the final time.
807 *
808 * Caller must hold rcu_lock or sufficient equivalent.
Peter Zijlstraeb755802008-08-19 12:33:05 +0200809 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200810int walk_tg_tree_from(struct task_group *from,
Paul Turner82774342011-07-21 09:43:35 -0700811 tg_visitor down, tg_visitor up, void *data)
Peter Zijlstraeb755802008-08-19 12:33:05 +0200812{
813 struct task_group *parent, *child;
814 int ret;
815
Paul Turner82774342011-07-21 09:43:35 -0700816 parent = from;
817
Peter Zijlstraeb755802008-08-19 12:33:05 +0200818down:
819 ret = (*down)(parent, data);
820 if (ret)
Paul Turner82774342011-07-21 09:43:35 -0700821 goto out;
Peter Zijlstraeb755802008-08-19 12:33:05 +0200822 list_for_each_entry_rcu(child, &parent->children, siblings) {
823 parent = child;
824 goto down;
825
826up:
827 continue;
828 }
829 ret = (*up)(parent, data);
Paul Turner82774342011-07-21 09:43:35 -0700830 if (ret || parent == from)
831 goto out;
Peter Zijlstraeb755802008-08-19 12:33:05 +0200832
833 child = parent;
834 parent = parent->parent;
835 if (parent)
836 goto up;
Paul Turner82774342011-07-21 09:43:35 -0700837out:
Peter Zijlstraeb755802008-08-19 12:33:05 +0200838 return ret;
839}
840
Peter Zijlstra029632f2011-10-25 10:00:11 +0200841int tg_nop(struct task_group *tg, void *data)
Peter Zijlstraeb755802008-08-19 12:33:05 +0200842{
843 return 0;
844}
845#endif
846
Vincent Guittot90593932017-05-17 11:50:45 +0200847static void set_load_weight(struct task_struct *p, bool update_load)
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200848{
Nikhil Raof05998d2011-05-18 10:09:38 -0700849 int prio = p->static_prio - MAX_RT_PRIO;
850 struct load_weight *load = &p->se.load;
851
Ingo Molnardd41f592007-07-09 18:51:59 +0200852 /*
853 * SCHED_IDLE tasks get minimal weight:
854 */
Viresh Kumar1da18432018-11-05 16:51:55 +0530855 if (task_has_idle_policy(p)) {
Nikhil Raoc8b28112011-05-18 14:37:48 -0700856 load->weight = scale_load(WEIGHT_IDLEPRIO);
Nikhil Raof05998d2011-05-18 10:09:38 -0700857 load->inv_weight = WMULT_IDLEPRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +0200858 return;
859 }
860
Vincent Guittot90593932017-05-17 11:50:45 +0200861 /*
862 * SCHED_OTHER tasks have to update their load when changing their
863 * weight
864 */
865 if (update_load && p->sched_class == &fair_sched_class) {
866 reweight_task(p, prio);
867 } else {
868 load->weight = scale_load(sched_prio_to_weight[prio]);
869 load->inv_weight = sched_prio_to_wmult[prio];
870 }
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200871}
872
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100873#ifdef CONFIG_UCLAMP_TASK
Patrick Bellasi2480c092019-08-22 14:28:06 +0100874/*
875 * Serializes updates of utilization clamp values
876 *
877 * The (slow-path) user-space triggers utilization clamp value updates which
878 * can require updates on (fast-path) scheduler's data structures used to
879 * support enqueue/dequeue operations.
880 * While the per-CPU rq lock protects fast-path update operations, user-space
881 * requests are serialized using a mutex to reduce the risk of conflicting
882 * updates or API abuses.
883 */
884static DEFINE_MUTEX(uclamp_mutex);
885
Patrick Bellasie8f14172019-06-21 09:42:05 +0100886/* Max allowed minimum utilization */
887unsigned int sysctl_sched_uclamp_util_min = SCHED_CAPACITY_SCALE;
888
889/* Max allowed maximum utilization */
890unsigned int sysctl_sched_uclamp_util_max = SCHED_CAPACITY_SCALE;
891
Qais Yousef13685c42020-07-16 12:03:45 +0100892/*
893 * By default RT tasks run at the maximum performance point/capacity of the
894 * system. Uclamp enforces this by always setting UCLAMP_MIN of RT tasks to
895 * SCHED_CAPACITY_SCALE.
896 *
897 * This knob allows admins to change the default behavior when uclamp is being
898 * used. In battery powered devices, particularly, running at the maximum
899 * capacity and frequency will increase energy consumption and shorten the
900 * battery life.
901 *
902 * This knob only affects RT tasks that their uclamp_se->user_defined == false.
903 *
904 * This knob will not override the system default sched_util_clamp_min defined
905 * above.
906 */
907unsigned int sysctl_sched_uclamp_util_min_rt_default = SCHED_CAPACITY_SCALE;
908
Patrick Bellasie8f14172019-06-21 09:42:05 +0100909/* All clamps are required to be less or equal than these values */
910static struct uclamp_se uclamp_default[UCLAMP_CNT];
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100911
Qais Yousef46609ce2020-06-30 12:21:23 +0100912/*
913 * This static key is used to reduce the uclamp overhead in the fast path. It
914 * primarily disables the call to uclamp_rq_{inc, dec}() in
915 * enqueue/dequeue_task().
916 *
917 * This allows users to continue to enable uclamp in their kernel config with
918 * minimum uclamp overhead in the fast path.
919 *
920 * As soon as userspace modifies any of the uclamp knobs, the static key is
921 * enabled, since we have an actual users that make use of uclamp
922 * functionality.
923 *
924 * The knobs that would enable this static key are:
925 *
926 * * A task modifying its uclamp value with sched_setattr().
927 * * An admin modifying the sysctl_sched_uclamp_{min, max} via procfs.
928 * * An admin modifying the cgroup cpu.uclamp.{min, max}
929 */
930DEFINE_STATIC_KEY_FALSE(sched_uclamp_used);
931
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100932/* Integer rounded range for each bucket */
933#define UCLAMP_BUCKET_DELTA DIV_ROUND_CLOSEST(SCHED_CAPACITY_SCALE, UCLAMP_BUCKETS)
934
935#define for_each_clamp_id(clamp_id) \
936 for ((clamp_id) = 0; (clamp_id) < UCLAMP_CNT; (clamp_id)++)
937
938static inline unsigned int uclamp_bucket_id(unsigned int clamp_value)
939{
940 return clamp_value / UCLAMP_BUCKET_DELTA;
941}
942
Patrick Bellasi60daf9c2019-06-21 09:42:03 +0100943static inline unsigned int uclamp_bucket_base_value(unsigned int clamp_value)
944{
945 return UCLAMP_BUCKET_DELTA * uclamp_bucket_id(clamp_value);
946}
947
Valentin Schneider7763baa2019-11-15 10:39:08 +0000948static inline unsigned int uclamp_none(enum uclamp_id clamp_id)
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100949{
950 if (clamp_id == UCLAMP_MIN)
951 return 0;
952 return SCHED_CAPACITY_SCALE;
953}
954
Patrick Bellasia509a7c2019-06-21 09:42:07 +0100955static inline void uclamp_se_set(struct uclamp_se *uc_se,
956 unsigned int value, bool user_defined)
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100957{
958 uc_se->value = value;
959 uc_se->bucket_id = uclamp_bucket_id(value);
Patrick Bellasia509a7c2019-06-21 09:42:07 +0100960 uc_se->user_defined = user_defined;
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100961}
962
Patrick Bellasie4961872019-06-21 09:42:04 +0100963static inline unsigned int
Patrick Bellasi0413d7f2019-08-22 14:28:11 +0100964uclamp_idle_value(struct rq *rq, enum uclamp_id clamp_id,
Patrick Bellasie4961872019-06-21 09:42:04 +0100965 unsigned int clamp_value)
966{
967 /*
968 * Avoid blocked utilization pushing up the frequency when we go
969 * idle (which drops the max-clamp) by retaining the last known
970 * max-clamp.
971 */
972 if (clamp_id == UCLAMP_MAX) {
973 rq->uclamp_flags |= UCLAMP_FLAG_IDLE;
974 return clamp_value;
975 }
976
977 return uclamp_none(UCLAMP_MIN);
978}
979
Patrick Bellasi0413d7f2019-08-22 14:28:11 +0100980static inline void uclamp_idle_reset(struct rq *rq, enum uclamp_id clamp_id,
Patrick Bellasie4961872019-06-21 09:42:04 +0100981 unsigned int clamp_value)
982{
983 /* Reset max-clamp retention only on idle exit */
984 if (!(rq->uclamp_flags & UCLAMP_FLAG_IDLE))
985 return;
986
987 WRITE_ONCE(rq->uclamp[clamp_id].value, clamp_value);
988}
989
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100990static inline
Valentin Schneider7763baa2019-11-15 10:39:08 +0000991unsigned int uclamp_rq_max_value(struct rq *rq, enum uclamp_id clamp_id,
Patrick Bellasi0413d7f2019-08-22 14:28:11 +0100992 unsigned int clamp_value)
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100993{
994 struct uclamp_bucket *bucket = rq->uclamp[clamp_id].bucket;
995 int bucket_id = UCLAMP_BUCKETS - 1;
996
997 /*
998 * Since both min and max clamps are max aggregated, find the
999 * top most bucket with tasks in.
1000 */
1001 for ( ; bucket_id >= 0; bucket_id--) {
1002 if (!bucket[bucket_id].tasks)
1003 continue;
1004 return bucket[bucket_id].value;
1005 }
1006
1007 /* No tasks -- default clamp values */
Patrick Bellasie4961872019-06-21 09:42:04 +01001008 return uclamp_idle_value(rq, clamp_id, clamp_value);
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001009}
1010
Qais Yousef13685c42020-07-16 12:03:45 +01001011static void __uclamp_update_util_min_rt_default(struct task_struct *p)
1012{
1013 unsigned int default_util_min;
1014 struct uclamp_se *uc_se;
1015
1016 lockdep_assert_held(&p->pi_lock);
1017
1018 uc_se = &p->uclamp_req[UCLAMP_MIN];
1019
1020 /* Only sync if user didn't override the default */
1021 if (uc_se->user_defined)
1022 return;
1023
1024 default_util_min = sysctl_sched_uclamp_util_min_rt_default;
1025 uclamp_se_set(uc_se, default_util_min, false);
1026}
1027
1028static void uclamp_update_util_min_rt_default(struct task_struct *p)
1029{
1030 struct rq_flags rf;
1031 struct rq *rq;
1032
1033 if (!rt_task(p))
1034 return;
1035
1036 /* Protect updates to p->uclamp_* */
1037 rq = task_rq_lock(p, &rf);
1038 __uclamp_update_util_min_rt_default(p);
1039 task_rq_unlock(rq, p, &rf);
1040}
1041
1042static void uclamp_sync_util_min_rt_default(void)
1043{
1044 struct task_struct *g, *p;
1045
1046 /*
1047 * copy_process() sysctl_uclamp
1048 * uclamp_min_rt = X;
1049 * write_lock(&tasklist_lock) read_lock(&tasklist_lock)
1050 * // link thread smp_mb__after_spinlock()
1051 * write_unlock(&tasklist_lock) read_unlock(&tasklist_lock);
1052 * sched_post_fork() for_each_process_thread()
1053 * __uclamp_sync_rt() __uclamp_sync_rt()
1054 *
1055 * Ensures that either sched_post_fork() will observe the new
1056 * uclamp_min_rt or for_each_process_thread() will observe the new
1057 * task.
1058 */
1059 read_lock(&tasklist_lock);
1060 smp_mb__after_spinlock();
1061 read_unlock(&tasklist_lock);
1062
1063 rcu_read_lock();
1064 for_each_process_thread(g, p)
1065 uclamp_update_util_min_rt_default(p);
1066 rcu_read_unlock();
1067}
1068
Patrick Bellasi3eac8702019-08-22 14:28:09 +01001069static inline struct uclamp_se
Patrick Bellasi0413d7f2019-08-22 14:28:11 +01001070uclamp_tg_restrict(struct task_struct *p, enum uclamp_id clamp_id)
Patrick Bellasi3eac8702019-08-22 14:28:09 +01001071{
1072 struct uclamp_se uc_req = p->uclamp_req[clamp_id];
1073#ifdef CONFIG_UCLAMP_TASK_GROUP
1074 struct uclamp_se uc_max;
1075
1076 /*
1077 * Tasks in autogroups or root task group will be
1078 * restricted by system defaults.
1079 */
1080 if (task_group_is_autogroup(task_group(p)))
1081 return uc_req;
1082 if (task_group(p) == &root_task_group)
1083 return uc_req;
1084
1085 uc_max = task_group(p)->uclamp[clamp_id];
1086 if (uc_req.value > uc_max.value || !uc_req.user_defined)
1087 return uc_max;
1088#endif
1089
1090 return uc_req;
1091}
1092
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001093/*
Patrick Bellasie8f14172019-06-21 09:42:05 +01001094 * The effective clamp bucket index of a task depends on, by increasing
1095 * priority:
1096 * - the task specific clamp value, when explicitly requested from userspace
Patrick Bellasi3eac8702019-08-22 14:28:09 +01001097 * - the task group effective clamp value, for tasks not either in the root
1098 * group or in an autogroup
Patrick Bellasie8f14172019-06-21 09:42:05 +01001099 * - the system default clamp value, defined by the sysadmin
1100 */
1101static inline struct uclamp_se
Patrick Bellasi0413d7f2019-08-22 14:28:11 +01001102uclamp_eff_get(struct task_struct *p, enum uclamp_id clamp_id)
Patrick Bellasie8f14172019-06-21 09:42:05 +01001103{
Patrick Bellasi3eac8702019-08-22 14:28:09 +01001104 struct uclamp_se uc_req = uclamp_tg_restrict(p, clamp_id);
Patrick Bellasie8f14172019-06-21 09:42:05 +01001105 struct uclamp_se uc_max = uclamp_default[clamp_id];
1106
1107 /* System default restrictions always apply */
1108 if (unlikely(uc_req.value > uc_max.value))
1109 return uc_max;
1110
1111 return uc_req;
1112}
1113
Valentin Schneider686516b2019-12-11 11:38:48 +00001114unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id)
Patrick Bellasi9d20ad72019-06-21 09:42:11 +01001115{
1116 struct uclamp_se uc_eff;
1117
1118 /* Task currently refcounted: use back-annotated (effective) value */
1119 if (p->uclamp[clamp_id].active)
Valentin Schneider686516b2019-12-11 11:38:48 +00001120 return (unsigned long)p->uclamp[clamp_id].value;
Patrick Bellasi9d20ad72019-06-21 09:42:11 +01001121
1122 uc_eff = uclamp_eff_get(p, clamp_id);
1123
Valentin Schneider686516b2019-12-11 11:38:48 +00001124 return (unsigned long)uc_eff.value;
Patrick Bellasi9d20ad72019-06-21 09:42:11 +01001125}
1126
Patrick Bellasie8f14172019-06-21 09:42:05 +01001127/*
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001128 * When a task is enqueued on a rq, the clamp bucket currently defined by the
1129 * task's uclamp::bucket_id is refcounted on that rq. This also immediately
1130 * updates the rq's clamp value if required.
Patrick Bellasi60daf9c2019-06-21 09:42:03 +01001131 *
1132 * Tasks can have a task-specific value requested from user-space, track
1133 * within each bucket the maximum value for tasks refcounted in it.
1134 * This "local max aggregation" allows to track the exact "requested" value
1135 * for each bucket when all its RUNNABLE tasks require the same clamp.
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001136 */
1137static inline void uclamp_rq_inc_id(struct rq *rq, struct task_struct *p,
Patrick Bellasi0413d7f2019-08-22 14:28:11 +01001138 enum uclamp_id clamp_id)
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001139{
1140 struct uclamp_rq *uc_rq = &rq->uclamp[clamp_id];
1141 struct uclamp_se *uc_se = &p->uclamp[clamp_id];
1142 struct uclamp_bucket *bucket;
1143
1144 lockdep_assert_held(&rq->lock);
1145
Patrick Bellasie8f14172019-06-21 09:42:05 +01001146 /* Update task effective clamp */
1147 p->uclamp[clamp_id] = uclamp_eff_get(p, clamp_id);
1148
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001149 bucket = &uc_rq->bucket[uc_se->bucket_id];
1150 bucket->tasks++;
Patrick Bellasie8f14172019-06-21 09:42:05 +01001151 uc_se->active = true;
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001152
Patrick Bellasie4961872019-06-21 09:42:04 +01001153 uclamp_idle_reset(rq, clamp_id, uc_se->value);
1154
Patrick Bellasi60daf9c2019-06-21 09:42:03 +01001155 /*
1156 * Local max aggregation: rq buckets always track the max
1157 * "requested" clamp value of its RUNNABLE tasks.
1158 */
1159 if (bucket->tasks == 1 || uc_se->value > bucket->value)
1160 bucket->value = uc_se->value;
1161
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001162 if (uc_se->value > READ_ONCE(uc_rq->value))
Patrick Bellasi60daf9c2019-06-21 09:42:03 +01001163 WRITE_ONCE(uc_rq->value, uc_se->value);
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001164}
1165
1166/*
1167 * When a task is dequeued from a rq, the clamp bucket refcounted by the task
1168 * is released. If this is the last task reference counting the rq's max
1169 * active clamp value, then the rq's clamp value is updated.
1170 *
1171 * Both refcounted tasks and rq's cached clamp values are expected to be
1172 * always valid. If it's detected they are not, as defensive programming,
1173 * enforce the expected state and warn.
1174 */
1175static inline void uclamp_rq_dec_id(struct rq *rq, struct task_struct *p,
Patrick Bellasi0413d7f2019-08-22 14:28:11 +01001176 enum uclamp_id clamp_id)
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001177{
1178 struct uclamp_rq *uc_rq = &rq->uclamp[clamp_id];
1179 struct uclamp_se *uc_se = &p->uclamp[clamp_id];
1180 struct uclamp_bucket *bucket;
Patrick Bellasie4961872019-06-21 09:42:04 +01001181 unsigned int bkt_clamp;
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001182 unsigned int rq_clamp;
1183
1184 lockdep_assert_held(&rq->lock);
1185
Qais Yousef46609ce2020-06-30 12:21:23 +01001186 /*
1187 * If sched_uclamp_used was enabled after task @p was enqueued,
1188 * we could end up with unbalanced call to uclamp_rq_dec_id().
1189 *
1190 * In this case the uc_se->active flag should be false since no uclamp
1191 * accounting was performed at enqueue time and we can just return
1192 * here.
1193 *
1194 * Need to be careful of the following enqeueue/dequeue ordering
1195 * problem too
1196 *
1197 * enqueue(taskA)
1198 * // sched_uclamp_used gets enabled
1199 * enqueue(taskB)
1200 * dequeue(taskA)
1201 * // Must not decrement bukcet->tasks here
1202 * dequeue(taskB)
1203 *
1204 * where we could end up with stale data in uc_se and
1205 * bucket[uc_se->bucket_id].
1206 *
1207 * The following check here eliminates the possibility of such race.
1208 */
1209 if (unlikely(!uc_se->active))
1210 return;
1211
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001212 bucket = &uc_rq->bucket[uc_se->bucket_id];
Qais Yousef46609ce2020-06-30 12:21:23 +01001213
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001214 SCHED_WARN_ON(!bucket->tasks);
1215 if (likely(bucket->tasks))
1216 bucket->tasks--;
Qais Yousef46609ce2020-06-30 12:21:23 +01001217
Patrick Bellasie8f14172019-06-21 09:42:05 +01001218 uc_se->active = false;
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001219
Patrick Bellasi60daf9c2019-06-21 09:42:03 +01001220 /*
1221 * Keep "local max aggregation" simple and accept to (possibly)
1222 * overboost some RUNNABLE tasks in the same bucket.
1223 * The rq clamp bucket value is reset to its base value whenever
1224 * there are no more RUNNABLE tasks refcounting it.
1225 */
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001226 if (likely(bucket->tasks))
1227 return;
1228
1229 rq_clamp = READ_ONCE(uc_rq->value);
1230 /*
1231 * Defensive programming: this should never happen. If it happens,
1232 * e.g. due to future modification, warn and fixup the expected value.
1233 */
1234 SCHED_WARN_ON(bucket->value > rq_clamp);
Patrick Bellasie4961872019-06-21 09:42:04 +01001235 if (bucket->value >= rq_clamp) {
1236 bkt_clamp = uclamp_rq_max_value(rq, clamp_id, uc_se->value);
1237 WRITE_ONCE(uc_rq->value, bkt_clamp);
1238 }
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001239}
1240
1241static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p)
1242{
Patrick Bellasi0413d7f2019-08-22 14:28:11 +01001243 enum uclamp_id clamp_id;
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001244
Qais Yousef46609ce2020-06-30 12:21:23 +01001245 /*
1246 * Avoid any overhead until uclamp is actually used by the userspace.
1247 *
1248 * The condition is constructed such that a NOP is generated when
1249 * sched_uclamp_used is disabled.
1250 */
1251 if (!static_branch_unlikely(&sched_uclamp_used))
1252 return;
1253
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001254 if (unlikely(!p->sched_class->uclamp_enabled))
1255 return;
1256
1257 for_each_clamp_id(clamp_id)
1258 uclamp_rq_inc_id(rq, p, clamp_id);
Patrick Bellasie4961872019-06-21 09:42:04 +01001259
1260 /* Reset clamp idle holding when there is one RUNNABLE task */
1261 if (rq->uclamp_flags & UCLAMP_FLAG_IDLE)
1262 rq->uclamp_flags &= ~UCLAMP_FLAG_IDLE;
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001263}
1264
1265static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p)
1266{
Patrick Bellasi0413d7f2019-08-22 14:28:11 +01001267 enum uclamp_id clamp_id;
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001268
Qais Yousef46609ce2020-06-30 12:21:23 +01001269 /*
1270 * Avoid any overhead until uclamp is actually used by the userspace.
1271 *
1272 * The condition is constructed such that a NOP is generated when
1273 * sched_uclamp_used is disabled.
1274 */
1275 if (!static_branch_unlikely(&sched_uclamp_used))
1276 return;
1277
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001278 if (unlikely(!p->sched_class->uclamp_enabled))
1279 return;
1280
1281 for_each_clamp_id(clamp_id)
1282 uclamp_rq_dec_id(rq, p, clamp_id);
1283}
1284
Patrick Bellasibabbe172019-08-22 14:28:10 +01001285static inline void
Patrick Bellasi0413d7f2019-08-22 14:28:11 +01001286uclamp_update_active(struct task_struct *p, enum uclamp_id clamp_id)
Patrick Bellasibabbe172019-08-22 14:28:10 +01001287{
1288 struct rq_flags rf;
1289 struct rq *rq;
1290
1291 /*
1292 * Lock the task and the rq where the task is (or was) queued.
1293 *
1294 * We might lock the (previous) rq of a !RUNNABLE task, but that's the
1295 * price to pay to safely serialize util_{min,max} updates with
1296 * enqueues, dequeues and migration operations.
1297 * This is the same locking schema used by __set_cpus_allowed_ptr().
1298 */
1299 rq = task_rq_lock(p, &rf);
1300
1301 /*
1302 * Setting the clamp bucket is serialized by task_rq_lock().
1303 * If the task is not yet RUNNABLE and its task_struct is not
1304 * affecting a valid clamp bucket, the next time it's enqueued,
1305 * it will already see the updated clamp bucket value.
1306 */
Qais Yousef6e1ff072019-11-14 21:10:52 +00001307 if (p->uclamp[clamp_id].active) {
Patrick Bellasibabbe172019-08-22 14:28:10 +01001308 uclamp_rq_dec_id(rq, p, clamp_id);
1309 uclamp_rq_inc_id(rq, p, clamp_id);
1310 }
1311
1312 task_rq_unlock(rq, p, &rf);
1313}
1314
Qais Yousefe3b8b6a2019-11-05 11:22:12 +00001315#ifdef CONFIG_UCLAMP_TASK_GROUP
Patrick Bellasibabbe172019-08-22 14:28:10 +01001316static inline void
1317uclamp_update_active_tasks(struct cgroup_subsys_state *css,
1318 unsigned int clamps)
1319{
Patrick Bellasi0413d7f2019-08-22 14:28:11 +01001320 enum uclamp_id clamp_id;
Patrick Bellasibabbe172019-08-22 14:28:10 +01001321 struct css_task_iter it;
1322 struct task_struct *p;
Patrick Bellasibabbe172019-08-22 14:28:10 +01001323
1324 css_task_iter_start(css, 0, &it);
1325 while ((p = css_task_iter_next(&it))) {
1326 for_each_clamp_id(clamp_id) {
1327 if ((0x1 << clamp_id) & clamps)
1328 uclamp_update_active(p, clamp_id);
1329 }
1330 }
1331 css_task_iter_end(&it);
1332}
1333
Patrick Bellasi7274a5c2019-08-22 14:28:08 +01001334static void cpu_util_update_eff(struct cgroup_subsys_state *css);
1335static void uclamp_update_root_tg(void)
1336{
1337 struct task_group *tg = &root_task_group;
1338
1339 uclamp_se_set(&tg->uclamp_req[UCLAMP_MIN],
1340 sysctl_sched_uclamp_util_min, false);
1341 uclamp_se_set(&tg->uclamp_req[UCLAMP_MAX],
1342 sysctl_sched_uclamp_util_max, false);
1343
1344 rcu_read_lock();
1345 cpu_util_update_eff(&root_task_group.css);
1346 rcu_read_unlock();
1347}
1348#else
1349static void uclamp_update_root_tg(void) { }
1350#endif
1351
Patrick Bellasie8f14172019-06-21 09:42:05 +01001352int sysctl_sched_uclamp_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001353 void *buffer, size_t *lenp, loff_t *ppos)
Patrick Bellasie8f14172019-06-21 09:42:05 +01001354{
Patrick Bellasi7274a5c2019-08-22 14:28:08 +01001355 bool update_root_tg = false;
Qais Yousef13685c42020-07-16 12:03:45 +01001356 int old_min, old_max, old_min_rt;
Patrick Bellasie8f14172019-06-21 09:42:05 +01001357 int result;
1358
Patrick Bellasi2480c092019-08-22 14:28:06 +01001359 mutex_lock(&uclamp_mutex);
Patrick Bellasie8f14172019-06-21 09:42:05 +01001360 old_min = sysctl_sched_uclamp_util_min;
1361 old_max = sysctl_sched_uclamp_util_max;
Qais Yousef13685c42020-07-16 12:03:45 +01001362 old_min_rt = sysctl_sched_uclamp_util_min_rt_default;
Patrick Bellasie8f14172019-06-21 09:42:05 +01001363
1364 result = proc_dointvec(table, write, buffer, lenp, ppos);
1365 if (result)
1366 goto undo;
1367 if (!write)
1368 goto done;
1369
1370 if (sysctl_sched_uclamp_util_min > sysctl_sched_uclamp_util_max ||
Qais Yousef13685c42020-07-16 12:03:45 +01001371 sysctl_sched_uclamp_util_max > SCHED_CAPACITY_SCALE ||
1372 sysctl_sched_uclamp_util_min_rt_default > SCHED_CAPACITY_SCALE) {
1373
Patrick Bellasie8f14172019-06-21 09:42:05 +01001374 result = -EINVAL;
1375 goto undo;
1376 }
1377
1378 if (old_min != sysctl_sched_uclamp_util_min) {
1379 uclamp_se_set(&uclamp_default[UCLAMP_MIN],
Patrick Bellasia509a7c2019-06-21 09:42:07 +01001380 sysctl_sched_uclamp_util_min, false);
Patrick Bellasi7274a5c2019-08-22 14:28:08 +01001381 update_root_tg = true;
Patrick Bellasie8f14172019-06-21 09:42:05 +01001382 }
1383 if (old_max != sysctl_sched_uclamp_util_max) {
1384 uclamp_se_set(&uclamp_default[UCLAMP_MAX],
Patrick Bellasia509a7c2019-06-21 09:42:07 +01001385 sysctl_sched_uclamp_util_max, false);
Patrick Bellasi7274a5c2019-08-22 14:28:08 +01001386 update_root_tg = true;
Patrick Bellasie8f14172019-06-21 09:42:05 +01001387 }
1388
Qais Yousef46609ce2020-06-30 12:21:23 +01001389 if (update_root_tg) {
1390 static_branch_enable(&sched_uclamp_used);
Patrick Bellasi7274a5c2019-08-22 14:28:08 +01001391 uclamp_update_root_tg();
Qais Yousef46609ce2020-06-30 12:21:23 +01001392 }
Patrick Bellasi7274a5c2019-08-22 14:28:08 +01001393
Qais Yousef13685c42020-07-16 12:03:45 +01001394 if (old_min_rt != sysctl_sched_uclamp_util_min_rt_default) {
1395 static_branch_enable(&sched_uclamp_used);
1396 uclamp_sync_util_min_rt_default();
1397 }
Patrick Bellasie8f14172019-06-21 09:42:05 +01001398
1399 /*
Patrick Bellasi7274a5c2019-08-22 14:28:08 +01001400 * We update all RUNNABLE tasks only when task groups are in use.
1401 * Otherwise, keep it simple and do just a lazy update at each next
1402 * task enqueue time.
Patrick Bellasie8f14172019-06-21 09:42:05 +01001403 */
Patrick Bellasi7274a5c2019-08-22 14:28:08 +01001404
Patrick Bellasie8f14172019-06-21 09:42:05 +01001405 goto done;
1406
1407undo:
1408 sysctl_sched_uclamp_util_min = old_min;
1409 sysctl_sched_uclamp_util_max = old_max;
Qais Yousef13685c42020-07-16 12:03:45 +01001410 sysctl_sched_uclamp_util_min_rt_default = old_min_rt;
Patrick Bellasie8f14172019-06-21 09:42:05 +01001411done:
Patrick Bellasi2480c092019-08-22 14:28:06 +01001412 mutex_unlock(&uclamp_mutex);
Patrick Bellasie8f14172019-06-21 09:42:05 +01001413
1414 return result;
1415}
1416
Patrick Bellasia509a7c2019-06-21 09:42:07 +01001417static int uclamp_validate(struct task_struct *p,
1418 const struct sched_attr *attr)
1419{
1420 unsigned int lower_bound = p->uclamp_req[UCLAMP_MIN].value;
1421 unsigned int upper_bound = p->uclamp_req[UCLAMP_MAX].value;
1422
1423 if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN)
1424 lower_bound = attr->sched_util_min;
1425 if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX)
1426 upper_bound = attr->sched_util_max;
1427
1428 if (lower_bound > upper_bound)
1429 return -EINVAL;
1430 if (upper_bound > SCHED_CAPACITY_SCALE)
1431 return -EINVAL;
1432
Qais Yousefe65855a2020-07-16 12:03:47 +01001433 /*
1434 * We have valid uclamp attributes; make sure uclamp is enabled.
1435 *
1436 * We need to do that here, because enabling static branches is a
1437 * blocking operation which obviously cannot be done while holding
1438 * scheduler locks.
1439 */
1440 static_branch_enable(&sched_uclamp_used);
1441
Patrick Bellasia509a7c2019-06-21 09:42:07 +01001442 return 0;
1443}
1444
1445static void __setscheduler_uclamp(struct task_struct *p,
1446 const struct sched_attr *attr)
1447{
Patrick Bellasi0413d7f2019-08-22 14:28:11 +01001448 enum uclamp_id clamp_id;
Patrick Bellasi1a00d992019-06-21 09:42:09 +01001449
1450 /*
1451 * On scheduling class change, reset to default clamps for tasks
1452 * without a task-specific value.
1453 */
1454 for_each_clamp_id(clamp_id) {
1455 struct uclamp_se *uc_se = &p->uclamp_req[clamp_id];
Patrick Bellasi1a00d992019-06-21 09:42:09 +01001456
1457 /* Keep using defined clamps across class changes */
1458 if (uc_se->user_defined)
1459 continue;
1460
Qais Yousef13685c42020-07-16 12:03:45 +01001461 /*
1462 * RT by default have a 100% boost value that could be modified
1463 * at runtime.
1464 */
Patrick Bellasi1a00d992019-06-21 09:42:09 +01001465 if (unlikely(rt_task(p) && clamp_id == UCLAMP_MIN))
Qais Yousef13685c42020-07-16 12:03:45 +01001466 __uclamp_update_util_min_rt_default(p);
1467 else
1468 uclamp_se_set(uc_se, uclamp_none(clamp_id), false);
Patrick Bellasi1a00d992019-06-21 09:42:09 +01001469
Patrick Bellasi1a00d992019-06-21 09:42:09 +01001470 }
1471
Patrick Bellasia509a7c2019-06-21 09:42:07 +01001472 if (likely(!(attr->sched_flags & SCHED_FLAG_UTIL_CLAMP)))
1473 return;
1474
1475 if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN) {
1476 uclamp_se_set(&p->uclamp_req[UCLAMP_MIN],
1477 attr->sched_util_min, true);
1478 }
1479
1480 if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX) {
1481 uclamp_se_set(&p->uclamp_req[UCLAMP_MAX],
1482 attr->sched_util_max, true);
1483 }
1484}
1485
Patrick Bellasie8f14172019-06-21 09:42:05 +01001486static void uclamp_fork(struct task_struct *p)
1487{
Patrick Bellasi0413d7f2019-08-22 14:28:11 +01001488 enum uclamp_id clamp_id;
Patrick Bellasie8f14172019-06-21 09:42:05 +01001489
Qais Yousef13685c42020-07-16 12:03:45 +01001490 /*
1491 * We don't need to hold task_rq_lock() when updating p->uclamp_* here
1492 * as the task is still at its early fork stages.
1493 */
Patrick Bellasie8f14172019-06-21 09:42:05 +01001494 for_each_clamp_id(clamp_id)
1495 p->uclamp[clamp_id].active = false;
Patrick Bellasia87498a2019-06-21 09:42:08 +01001496
1497 if (likely(!p->sched_reset_on_fork))
1498 return;
1499
1500 for_each_clamp_id(clamp_id) {
Quentin Perreteaf5a922020-04-16 09:59:56 +01001501 uclamp_se_set(&p->uclamp_req[clamp_id],
1502 uclamp_none(clamp_id), false);
Patrick Bellasia87498a2019-06-21 09:42:08 +01001503 }
Patrick Bellasie8f14172019-06-21 09:42:05 +01001504}
1505
Qais Yousef13685c42020-07-16 12:03:45 +01001506static void uclamp_post_fork(struct task_struct *p)
1507{
1508 uclamp_update_util_min_rt_default(p);
1509}
1510
Qais Yousefd81ae8a2020-06-30 12:21:22 +01001511static void __init init_uclamp_rq(struct rq *rq)
1512{
1513 enum uclamp_id clamp_id;
1514 struct uclamp_rq *uc_rq = rq->uclamp;
1515
1516 for_each_clamp_id(clamp_id) {
1517 uc_rq[clamp_id] = (struct uclamp_rq) {
1518 .value = uclamp_none(clamp_id)
1519 };
1520 }
1521
1522 rq->uclamp_flags = 0;
1523}
1524
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001525static void __init init_uclamp(void)
1526{
Patrick Bellasie8f14172019-06-21 09:42:05 +01001527 struct uclamp_se uc_max = {};
Patrick Bellasi0413d7f2019-08-22 14:28:11 +01001528 enum uclamp_id clamp_id;
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001529 int cpu;
1530
Qais Yousefd81ae8a2020-06-30 12:21:22 +01001531 for_each_possible_cpu(cpu)
1532 init_uclamp_rq(cpu_rq(cpu));
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001533
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001534 for_each_clamp_id(clamp_id) {
Patrick Bellasie8f14172019-06-21 09:42:05 +01001535 uclamp_se_set(&init_task.uclamp_req[clamp_id],
Patrick Bellasia509a7c2019-06-21 09:42:07 +01001536 uclamp_none(clamp_id), false);
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001537 }
Patrick Bellasie8f14172019-06-21 09:42:05 +01001538
1539 /* System defaults allow max clamp values for both indexes */
Patrick Bellasia509a7c2019-06-21 09:42:07 +01001540 uclamp_se_set(&uc_max, uclamp_none(UCLAMP_MAX), false);
Patrick Bellasi2480c092019-08-22 14:28:06 +01001541 for_each_clamp_id(clamp_id) {
Patrick Bellasie8f14172019-06-21 09:42:05 +01001542 uclamp_default[clamp_id] = uc_max;
Patrick Bellasi2480c092019-08-22 14:28:06 +01001543#ifdef CONFIG_UCLAMP_TASK_GROUP
1544 root_task_group.uclamp_req[clamp_id] = uc_max;
Patrick Bellasi0b60ba22019-08-22 14:28:07 +01001545 root_task_group.uclamp[clamp_id] = uc_max;
Patrick Bellasi2480c092019-08-22 14:28:06 +01001546#endif
1547 }
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001548}
1549
1550#else /* CONFIG_UCLAMP_TASK */
1551static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p) { }
1552static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p) { }
Patrick Bellasia509a7c2019-06-21 09:42:07 +01001553static inline int uclamp_validate(struct task_struct *p,
1554 const struct sched_attr *attr)
1555{
1556 return -EOPNOTSUPP;
1557}
1558static void __setscheduler_uclamp(struct task_struct *p,
1559 const struct sched_attr *attr) { }
Patrick Bellasie8f14172019-06-21 09:42:05 +01001560static inline void uclamp_fork(struct task_struct *p) { }
Qais Yousef13685c42020-07-16 12:03:45 +01001561static inline void uclamp_post_fork(struct task_struct *p) { }
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001562static inline void init_uclamp(void) { }
1563#endif /* CONFIG_UCLAMP_TASK */
1564
Peter Zijlstra1de64442015-09-30 17:44:13 +02001565static inline void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
Gregory Haskins2087a1a2008-06-27 14:30:00 -06001566{
Peter Zijlstra0a67d1e2016-10-04 16:29:45 +02001567 if (!(flags & ENQUEUE_NOCLOCK))
1568 update_rq_clock(rq);
1569
Johannes Weinereb414682018-10-26 15:06:27 -07001570 if (!(flags & ENQUEUE_RESTORE)) {
Peter Zijlstra1de64442015-09-30 17:44:13 +02001571 sched_info_queued(rq, p);
Johannes Weinereb414682018-10-26 15:06:27 -07001572 psi_enqueue(p, flags & ENQUEUE_WAKEUP);
1573 }
Peter Zijlstra0a67d1e2016-10-04 16:29:45 +02001574
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001575 uclamp_rq_inc(rq, p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001576 p->sched_class->enqueue_task(rq, p, flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02001577}
1578
Peter Zijlstra1de64442015-09-30 17:44:13 +02001579static inline void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnardd41f592007-07-09 18:51:59 +02001580{
Peter Zijlstra0a67d1e2016-10-04 16:29:45 +02001581 if (!(flags & DEQUEUE_NOCLOCK))
1582 update_rq_clock(rq);
1583
Johannes Weinereb414682018-10-26 15:06:27 -07001584 if (!(flags & DEQUEUE_SAVE)) {
Peter Zijlstra1de64442015-09-30 17:44:13 +02001585 sched_info_dequeued(rq, p);
Johannes Weinereb414682018-10-26 15:06:27 -07001586 psi_dequeue(p, flags & DEQUEUE_SLEEP);
1587 }
Peter Zijlstra0a67d1e2016-10-04 16:29:45 +02001588
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001589 uclamp_rq_dec(rq, p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001590 p->sched_class->dequeue_task(rq, p, flags);
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001591}
1592
Peter Zijlstra029632f2011-10-25 10:00:11 +02001593void activate_task(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001594{
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001595 enqueue_task(rq, p, flags);
Peter Zijlstra7dd77882019-04-09 09:59:05 +02001596
1597 p->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001598}
1599
Peter Zijlstra029632f2011-10-25 10:00:11 +02001600void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001601{
Peter Zijlstra7dd77882019-04-09 09:59:05 +02001602 p->on_rq = (flags & DEQUEUE_SLEEP) ? 0 : TASK_ON_RQ_MIGRATING;
1603
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001604 dequeue_task(rq, p, flags);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001605}
1606
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001607/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001608 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001609 */
Ingo Molnar14531182007-07-09 18:51:59 +02001610static inline int __normal_prio(struct task_struct *p)
1611{
Ingo Molnardd41f592007-07-09 18:51:59 +02001612 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001613}
1614
1615/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001616 * Calculate the expected normal priority: i.e. priority
1617 * without taking RT-inheritance into account. Might be
1618 * boosted by interactivity modifiers. Changes upon fork,
1619 * setprio syscalls, and whenever the interactivity
1620 * estimator recalculates.
1621 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001622static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001623{
1624 int prio;
1625
Dario Faggioliaab03e02013-11-28 11:14:43 +01001626 if (task_has_dl_policy(p))
1627 prio = MAX_DL_PRIO-1;
1628 else if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001629 prio = MAX_RT_PRIO-1 - p->rt_priority;
1630 else
1631 prio = __normal_prio(p);
1632 return prio;
1633}
1634
1635/*
1636 * Calculate the current priority, i.e. the priority
1637 * taken into account by the scheduler. This value might
1638 * be boosted by RT tasks, or might be boosted by
1639 * interactivity modifiers. Will be RT if the task got
1640 * RT-boosted. If not then it returns p->normal_prio.
1641 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001642static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001643{
1644 p->normal_prio = normal_prio(p);
1645 /*
1646 * If we are RT tasks or we were boosted to RT priority,
1647 * keep the priority unchanged. Otherwise, update priority
1648 * to the normal priority:
1649 */
1650 if (!rt_prio(p->prio))
1651 return p->normal_prio;
1652 return p->prio;
1653}
1654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655/**
1656 * task_curr - is this task currently executing on a CPU?
1657 * @p: the task in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02001658 *
1659 * Return: 1 if the task is currently executing. 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001661inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
1663 return cpu_curr(task_cpu(p)) == p;
1664}
1665
Kirill Tkhai67dfa1b2014-10-27 17:40:52 +03001666/*
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02001667 * switched_from, switched_to and prio_changed must _NOT_ drop rq->lock,
1668 * use the balance_callback list if you want balancing.
1669 *
1670 * this means any call to check_class_changed() must be followed by a call to
1671 * balance_callback().
Kirill Tkhai67dfa1b2014-10-27 17:40:52 +03001672 */
Steven Rostedtcb469842008-01-25 21:08:22 +01001673static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1674 const struct sched_class *prev_class,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001675 int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01001676{
1677 if (prev_class != p->sched_class) {
1678 if (prev_class->switched_from)
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001679 prev_class->switched_from(rq, p);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02001680
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001681 p->sched_class->switched_to(rq, p);
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001682 } else if (oldprio != p->prio || dl_task(p))
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001683 p->sched_class->prio_changed(rq, p, oldprio);
Steven Rostedtcb469842008-01-25 21:08:22 +01001684}
1685
Peter Zijlstra029632f2011-10-25 10:00:11 +02001686void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e5a7402010-10-31 12:37:04 +01001687{
Kirill Tkhaiaa93cd52019-12-19 16:44:55 -05001688 if (p->sched_class == rq->curr->sched_class)
Peter Zijlstra1e5a7402010-10-31 12:37:04 +01001689 rq->curr->sched_class->check_preempt_curr(rq, p, flags);
Kirill Tkhaiaa93cd52019-12-19 16:44:55 -05001690 else if (p->sched_class > rq->curr->sched_class)
1691 resched_curr(rq);
Peter Zijlstra1e5a7402010-10-31 12:37:04 +01001692
1693 /*
1694 * A queue event has occurred, and we're going to schedule. In
1695 * this case, we can save a useless back to back clock update.
1696 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001697 if (task_on_rq_queued(rq->curr) && test_tsk_need_resched(rq->curr))
Davidlohr Buesoadcc8da2018-04-04 09:15:39 -07001698 rq_clock_skip_update(rq);
Peter Zijlstra1e5a7402010-10-31 12:37:04 +01001699}
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701#ifdef CONFIG_SMP
Peter Zijlstra175f0e22017-07-25 18:58:21 +02001702
Peter Zijlstra175f0e22017-07-25 18:58:21 +02001703/*
Joel Savitzbee98532019-03-06 20:13:33 -05001704 * Per-CPU kthreads are allowed to run on !active && online CPUs, see
Peter Zijlstra175f0e22017-07-25 18:58:21 +02001705 * __set_cpus_allowed_ptr() and select_fallback_rq().
1706 */
1707static inline bool is_cpu_allowed(struct task_struct *p, int cpu)
1708{
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02001709 if (!cpumask_test_cpu(cpu, p->cpus_ptr))
Peter Zijlstra175f0e22017-07-25 18:58:21 +02001710 return false;
1711
1712 if (is_per_cpu_kthread(p))
1713 return cpu_online(cpu);
1714
1715 return cpu_active(cpu);
1716}
1717
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001718/*
1719 * This is how migration works:
1720 *
1721 * 1) we invoke migration_cpu_stop() on the target CPU using
1722 * stop_one_cpu().
1723 * 2) stopper starts to run (implicitly forcing the migrated thread
1724 * off the CPU)
1725 * 3) it checks whether the migrated task is still in the wrong runqueue.
1726 * 4) if it's in the wrong runqueue then the migration thread removes
1727 * it and puts it into the right queue.
1728 * 5) stopper completes and stop_one_cpu() returns and the migration
1729 * is done.
1730 */
1731
1732/*
1733 * move_queued_task - move a queued task to new rq.
1734 *
1735 * Returns (locked) new rq. Old rq's lock is released.
1736 */
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001737static struct rq *move_queued_task(struct rq *rq, struct rq_flags *rf,
1738 struct task_struct *p, int new_cpu)
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001739{
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001740 lockdep_assert_held(&rq->lock);
1741
Peter Zijlstra58877d32020-07-02 14:52:11 +02001742 deactivate_task(rq, p, DEQUEUE_NOCLOCK);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001743 set_task_cpu(p, new_cpu);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001744 rq_unlock(rq, rf);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001745
1746 rq = cpu_rq(new_cpu);
1747
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001748 rq_lock(rq, rf);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001749 BUG_ON(task_cpu(p) != new_cpu);
Peter Zijlstra58877d32020-07-02 14:52:11 +02001750 activate_task(rq, p, 0);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001751 check_preempt_curr(rq, p, 0);
1752
1753 return rq;
1754}
1755
1756struct migration_arg {
1757 struct task_struct *task;
1758 int dest_cpu;
1759};
1760
1761/*
Ingo Molnard1ccc662017-02-01 11:46:42 +01001762 * Move (not current) task off this CPU, onto the destination CPU. We're doing
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001763 * this because either it can't run here any more (set_cpus_allowed()
1764 * away from this CPU, or CPU going down), or because we're
1765 * attempting to rebalance this task on exec (sched_exec).
1766 *
1767 * So we race with normal scheduler movements, but that's OK, as long
1768 * as the task is no longer on this CPU.
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001769 */
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001770static struct rq *__migrate_task(struct rq *rq, struct rq_flags *rf,
1771 struct task_struct *p, int dest_cpu)
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001772{
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001773 /* Affinity changed (again). */
Peter Zijlstra175f0e22017-07-25 18:58:21 +02001774 if (!is_cpu_allowed(p, dest_cpu))
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001775 return rq;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001776
Peter Zijlstra15ff9912016-10-05 17:59:32 +02001777 update_rq_clock(rq);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001778 rq = move_queued_task(rq, rf, p, dest_cpu);
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001779
1780 return rq;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001781}
1782
1783/*
1784 * migration_cpu_stop - this will be executed by a highprio stopper thread
1785 * and performs thread migration by bumping thread off CPU then
1786 * 'pushing' onto another runqueue.
1787 */
1788static int migration_cpu_stop(void *data)
1789{
1790 struct migration_arg *arg = data;
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001791 struct task_struct *p = arg->task;
1792 struct rq *rq = this_rq();
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001793 struct rq_flags rf;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001794
1795 /*
Ingo Molnard1ccc662017-02-01 11:46:42 +01001796 * The original target CPU might have gone down and we might
1797 * be on another CPU but it doesn't matter.
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001798 */
1799 local_irq_disable();
1800 /*
1801 * We need to explicitly wake pending tasks before running
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02001802 * __migrate_task() such that we will not miss enforcing cpus_ptr
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001803 * during wakeups, see set_cpus_allowed_ptr()'s TASK_WAKING test.
1804 */
Peter Zijlstraa14886642020-05-26 18:11:04 +02001805 flush_smp_call_function_from_idle();
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001806
1807 raw_spin_lock(&p->pi_lock);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001808 rq_lock(rq, &rf);
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001809 /*
1810 * If task_rq(p) != rq, it cannot be migrated here, because we're
1811 * holding rq->lock, if p->on_rq == 0 it cannot get enqueued because
1812 * we're holding p->pi_lock.
1813 */
Cheng Chaobf89a302016-09-14 10:01:50 +08001814 if (task_rq(p) == rq) {
1815 if (task_on_rq_queued(p))
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001816 rq = __migrate_task(rq, &rf, p, arg->dest_cpu);
Cheng Chaobf89a302016-09-14 10:01:50 +08001817 else
1818 p->wake_cpu = arg->dest_cpu;
1819 }
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001820 rq_unlock(rq, &rf);
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001821 raw_spin_unlock(&p->pi_lock);
1822
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001823 local_irq_enable();
1824 return 0;
1825}
1826
Peter Zijlstrac5b28032015-05-15 17:43:35 +02001827/*
1828 * sched_class::set_cpus_allowed must do the below, but is not required to
1829 * actually call this function.
1830 */
1831void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask)
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001832{
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02001833 cpumask_copy(&p->cpus_mask, new_mask);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001834 p->nr_cpus_allowed = cpumask_weight(new_mask);
1835}
1836
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001837void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
1838{
Peter Zijlstra6c370672015-05-15 17:43:36 +02001839 struct rq *rq = task_rq(p);
1840 bool queued, running;
1841
Peter Zijlstra25834c72015-05-15 17:43:34 +02001842 lockdep_assert_held(&p->pi_lock);
Peter Zijlstra6c370672015-05-15 17:43:36 +02001843
1844 queued = task_on_rq_queued(p);
1845 running = task_current(rq, p);
1846
1847 if (queued) {
1848 /*
1849 * Because __kthread_bind() calls this on blocked tasks without
1850 * holding rq->lock.
1851 */
1852 lockdep_assert_held(&rq->lock);
Peter Zijlstra7a57f322017-02-21 14:47:02 +01001853 dequeue_task(rq, p, DEQUEUE_SAVE | DEQUEUE_NOCLOCK);
Peter Zijlstra6c370672015-05-15 17:43:36 +02001854 }
1855 if (running)
1856 put_prev_task(rq, p);
1857
Peter Zijlstrac5b28032015-05-15 17:43:35 +02001858 p->sched_class->set_cpus_allowed(p, new_mask);
Peter Zijlstra6c370672015-05-15 17:43:36 +02001859
Peter Zijlstra6c370672015-05-15 17:43:36 +02001860 if (queued)
Peter Zijlstra7134b3e2017-02-21 14:23:38 +01001861 enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK);
Vincent Guittota399d232016-09-12 09:47:52 +02001862 if (running)
Peter Zijlstra03b7fad2019-05-29 20:36:41 +00001863 set_next_task(rq, p);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001864}
1865
1866/*
1867 * Change a given task's CPU affinity. Migrate the thread to a
1868 * proper CPU and schedule it away if the CPU it's executing on
1869 * is removed from the allowed bitmask.
1870 *
1871 * NOTE: the caller must have a valid reference to the task, the
1872 * task must not exit() & deallocate itself prematurely. The
1873 * call is not atomic; no spinlocks may be held.
1874 */
Peter Zijlstra25834c72015-05-15 17:43:34 +02001875static int __set_cpus_allowed_ptr(struct task_struct *p,
1876 const struct cpumask *new_mask, bool check)
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001877{
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01001878 const struct cpumask *cpu_valid_mask = cpu_active_mask;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001879 unsigned int dest_cpu;
Peter Zijlstraeb580752015-07-31 21:28:18 +02001880 struct rq_flags rf;
1881 struct rq *rq;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001882 int ret = 0;
1883
Peter Zijlstraeb580752015-07-31 21:28:18 +02001884 rq = task_rq_lock(p, &rf);
Wanpeng Lia499c3e2017-02-21 23:52:55 -08001885 update_rq_clock(rq);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001886
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01001887 if (p->flags & PF_KTHREAD) {
1888 /*
1889 * Kernel threads are allowed on online && !active CPUs
1890 */
1891 cpu_valid_mask = cpu_online_mask;
1892 }
1893
Peter Zijlstra25834c72015-05-15 17:43:34 +02001894 /*
1895 * Must re-check here, to close a race against __kthread_bind(),
1896 * sched_setaffinity() is not guaranteed to observe the flag.
1897 */
1898 if (check && (p->flags & PF_NO_SETAFFINITY)) {
1899 ret = -EINVAL;
1900 goto out;
1901 }
1902
Scott Woodfd844ba2020-06-17 14:17:42 +02001903 if (cpumask_equal(&p->cpus_mask, new_mask))
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001904 goto out;
1905
Paul Turner46a87b32020-03-10 18:01:13 -07001906 /*
1907 * Picking a ~random cpu helps in cases where we are changing affinity
1908 * for groups of tasks (ie. cpuset), so that load balancing is not
1909 * immediately required to distribute the tasks within their new mask.
1910 */
1911 dest_cpu = cpumask_any_and_distribute(cpu_valid_mask, new_mask);
KeMeng Shi714e5012019-09-16 06:53:28 +00001912 if (dest_cpu >= nr_cpu_ids) {
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001913 ret = -EINVAL;
1914 goto out;
1915 }
1916
1917 do_set_cpus_allowed(p, new_mask);
1918
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01001919 if (p->flags & PF_KTHREAD) {
1920 /*
1921 * For kernel threads that do indeed end up on online &&
Ingo Molnard1ccc662017-02-01 11:46:42 +01001922 * !active we want to ensure they are strict per-CPU threads.
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01001923 */
1924 WARN_ON(cpumask_intersects(new_mask, cpu_online_mask) &&
1925 !cpumask_intersects(new_mask, cpu_active_mask) &&
1926 p->nr_cpus_allowed != 1);
1927 }
1928
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001929 /* Can the task run on the task's current CPU? If so, we're done */
1930 if (cpumask_test_cpu(task_cpu(p), new_mask))
1931 goto out;
1932
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001933 if (task_running(rq, p) || p->state == TASK_WAKING) {
1934 struct migration_arg arg = { p, dest_cpu };
1935 /* Need help from migration thread: drop lock and wait. */
Peter Zijlstraeb580752015-07-31 21:28:18 +02001936 task_rq_unlock(rq, p, &rf);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001937 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001938 return 0;
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001939 } else if (task_on_rq_queued(p)) {
1940 /*
1941 * OK, since we're going to drop the lock immediately
1942 * afterwards anyway.
1943 */
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001944 rq = move_queued_task(rq, &rf, p, dest_cpu);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001945 }
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001946out:
Peter Zijlstraeb580752015-07-31 21:28:18 +02001947 task_rq_unlock(rq, p, &rf);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001948
1949 return ret;
1950}
Peter Zijlstra25834c72015-05-15 17:43:34 +02001951
1952int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
1953{
1954 return __set_cpus_allowed_ptr(p, new_mask, false);
1955}
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001956EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
1957
Ingo Molnardd41f592007-07-09 18:51:59 +02001958void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001959{
Peter Zijlstrae2912002009-12-16 18:04:36 +01001960#ifdef CONFIG_SCHED_DEBUG
1961 /*
1962 * We should never call set_task_cpu() on a blocked task,
1963 * ttwu() will sort out the placement.
1964 */
Peter Zijlstra077614e2009-12-17 13:16:31 +01001965 WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING &&
Oleg Nesterove2336f62014-10-08 20:33:48 +02001966 !p->on_rq);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001967
Joonwoo Park3ea94de2015-11-12 19:38:54 -08001968 /*
1969 * Migrating fair class task must have p->on_rq = TASK_ON_RQ_MIGRATING,
1970 * because schedstat_wait_{start,end} rebase migrating task's wait_start
1971 * time relying on p->on_rq.
1972 */
1973 WARN_ON_ONCE(p->state == TASK_RUNNING &&
1974 p->sched_class == &fair_sched_class &&
1975 (p->on_rq && !task_on_rq_migrating(p)));
1976
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001977#ifdef CONFIG_LOCKDEP
Peter Zijlstra6c6c54e2011-06-03 17:37:07 +02001978 /*
1979 * The caller should hold either p->pi_lock or rq->lock, when changing
1980 * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks.
1981 *
1982 * sched_move_task() holds both and thus holding either pins the cgroup,
Peter Zijlstra8323f262012-06-22 13:36:05 +02001983 * see task_group().
Peter Zijlstra6c6c54e2011-06-03 17:37:07 +02001984 *
1985 * Furthermore, all task_rq users should acquire both locks, see
1986 * task_rq_lock().
1987 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001988 WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) ||
1989 lockdep_is_held(&task_rq(p)->lock)));
1990#endif
Peter Zijlstra4ff90832017-09-07 17:03:52 +02001991 /*
1992 * Clearly, migrating tasks to offline CPUs is a fairly daft thing.
1993 */
1994 WARN_ON_ONCE(!cpu_online(new_cpu));
Peter Zijlstrae2912002009-12-16 18:04:36 +01001995#endif
1996
Mathieu Desnoyersde1d7282009-05-05 16:49:59 +08001997 trace_sched_migrate_task(p, new_cpu);
Peter Zijlstracbc34ed2008-12-10 08:08:22 +01001998
Peter Zijlstra0c697742009-12-22 15:43:19 +01001999 if (task_cpu(p) != new_cpu) {
Paul Turner0a74bef2012-10-04 13:18:30 +02002000 if (p->sched_class->migrate_task_rq)
Srikar Dronamraju1327237a52018-09-21 23:18:57 +05302001 p->sched_class->migrate_task_rq(p, new_cpu);
Peter Zijlstra0c697742009-12-22 15:43:19 +01002002 p->se.nr_migrations++;
Mathieu Desnoyersd7822b12018-06-02 08:43:54 -04002003 rseq_migrate(p);
Peter Zijlstraff303e62015-04-17 20:05:30 +02002004 perf_event_task_migrate(p);
Peter Zijlstra0c697742009-12-22 15:43:19 +01002005 }
Ingo Molnardd41f592007-07-09 18:51:59 +02002006
2007 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02002008}
2009
Srikar Dronamraju0ad4e3d2018-06-20 22:32:50 +05302010#ifdef CONFIG_NUMA_BALANCING
Peter Zijlstraac66f542013-10-07 11:29:16 +01002011static void __migrate_swap_task(struct task_struct *p, int cpu)
2012{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002013 if (task_on_rq_queued(p)) {
Peter Zijlstraac66f542013-10-07 11:29:16 +01002014 struct rq *src_rq, *dst_rq;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02002015 struct rq_flags srf, drf;
Peter Zijlstraac66f542013-10-07 11:29:16 +01002016
2017 src_rq = task_rq(p);
2018 dst_rq = cpu_rq(cpu);
2019
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02002020 rq_pin_lock(src_rq, &srf);
2021 rq_pin_lock(dst_rq, &drf);
2022
Peter Zijlstraac66f542013-10-07 11:29:16 +01002023 deactivate_task(src_rq, p, 0);
2024 set_task_cpu(p, cpu);
2025 activate_task(dst_rq, p, 0);
2026 check_preempt_curr(dst_rq, p, 0);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02002027
2028 rq_unpin_lock(dst_rq, &drf);
2029 rq_unpin_lock(src_rq, &srf);
2030
Peter Zijlstraac66f542013-10-07 11:29:16 +01002031 } else {
2032 /*
2033 * Task isn't running anymore; make it appear like we migrated
2034 * it before it went to sleep. This means on wakeup we make the
Ingo Molnard1ccc662017-02-01 11:46:42 +01002035 * previous CPU our target instead of where it really is.
Peter Zijlstraac66f542013-10-07 11:29:16 +01002036 */
2037 p->wake_cpu = cpu;
2038 }
2039}
2040
2041struct migration_swap_arg {
2042 struct task_struct *src_task, *dst_task;
2043 int src_cpu, dst_cpu;
2044};
2045
2046static int migrate_swap_stop(void *data)
2047{
2048 struct migration_swap_arg *arg = data;
2049 struct rq *src_rq, *dst_rq;
2050 int ret = -EAGAIN;
2051
Peter Zijlstra62694cd2015-10-09 18:36:29 +02002052 if (!cpu_active(arg->src_cpu) || !cpu_active(arg->dst_cpu))
2053 return -EAGAIN;
2054
Peter Zijlstraac66f542013-10-07 11:29:16 +01002055 src_rq = cpu_rq(arg->src_cpu);
2056 dst_rq = cpu_rq(arg->dst_cpu);
2057
Peter Zijlstra74602312013-10-10 20:17:22 +02002058 double_raw_lock(&arg->src_task->pi_lock,
2059 &arg->dst_task->pi_lock);
Peter Zijlstraac66f542013-10-07 11:29:16 +01002060 double_rq_lock(src_rq, dst_rq);
Peter Zijlstra62694cd2015-10-09 18:36:29 +02002061
Peter Zijlstraac66f542013-10-07 11:29:16 +01002062 if (task_cpu(arg->dst_task) != arg->dst_cpu)
2063 goto unlock;
2064
2065 if (task_cpu(arg->src_task) != arg->src_cpu)
2066 goto unlock;
2067
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02002068 if (!cpumask_test_cpu(arg->dst_cpu, arg->src_task->cpus_ptr))
Peter Zijlstraac66f542013-10-07 11:29:16 +01002069 goto unlock;
2070
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02002071 if (!cpumask_test_cpu(arg->src_cpu, arg->dst_task->cpus_ptr))
Peter Zijlstraac66f542013-10-07 11:29:16 +01002072 goto unlock;
2073
2074 __migrate_swap_task(arg->src_task, arg->dst_cpu);
2075 __migrate_swap_task(arg->dst_task, arg->src_cpu);
2076
2077 ret = 0;
2078
2079unlock:
2080 double_rq_unlock(src_rq, dst_rq);
Peter Zijlstra74602312013-10-10 20:17:22 +02002081 raw_spin_unlock(&arg->dst_task->pi_lock);
2082 raw_spin_unlock(&arg->src_task->pi_lock);
Peter Zijlstraac66f542013-10-07 11:29:16 +01002083
2084 return ret;
2085}
2086
2087/*
2088 * Cross migrate two tasks
2089 */
Srikar Dronamraju0ad4e3d2018-06-20 22:32:50 +05302090int migrate_swap(struct task_struct *cur, struct task_struct *p,
2091 int target_cpu, int curr_cpu)
Peter Zijlstraac66f542013-10-07 11:29:16 +01002092{
2093 struct migration_swap_arg arg;
2094 int ret = -EINVAL;
2095
Peter Zijlstraac66f542013-10-07 11:29:16 +01002096 arg = (struct migration_swap_arg){
2097 .src_task = cur,
Srikar Dronamraju0ad4e3d2018-06-20 22:32:50 +05302098 .src_cpu = curr_cpu,
Peter Zijlstraac66f542013-10-07 11:29:16 +01002099 .dst_task = p,
Srikar Dronamraju0ad4e3d2018-06-20 22:32:50 +05302100 .dst_cpu = target_cpu,
Peter Zijlstraac66f542013-10-07 11:29:16 +01002101 };
2102
2103 if (arg.src_cpu == arg.dst_cpu)
2104 goto out;
2105
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02002106 /*
2107 * These three tests are all lockless; this is OK since all of them
2108 * will be re-checked with proper locks held further down the line.
2109 */
Peter Zijlstraac66f542013-10-07 11:29:16 +01002110 if (!cpu_active(arg.src_cpu) || !cpu_active(arg.dst_cpu))
2111 goto out;
2112
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02002113 if (!cpumask_test_cpu(arg.dst_cpu, arg.src_task->cpus_ptr))
Peter Zijlstraac66f542013-10-07 11:29:16 +01002114 goto out;
2115
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02002116 if (!cpumask_test_cpu(arg.src_cpu, arg.dst_task->cpus_ptr))
Peter Zijlstraac66f542013-10-07 11:29:16 +01002117 goto out;
2118
Mel Gorman286549d2014-01-21 15:51:03 -08002119 trace_sched_swap_numa(cur, arg.src_cpu, p, arg.dst_cpu);
Peter Zijlstraac66f542013-10-07 11:29:16 +01002120 ret = stop_two_cpus(arg.dst_cpu, arg.src_cpu, migrate_swap_stop, &arg);
2121
2122out:
Peter Zijlstraac66f542013-10-07 11:29:16 +01002123 return ret;
2124}
Srikar Dronamraju0ad4e3d2018-06-20 22:32:50 +05302125#endif /* CONFIG_NUMA_BALANCING */
Peter Zijlstraac66f542013-10-07 11:29:16 +01002126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 * wait_task_inactive - wait for a thread to unschedule.
2129 *
Roland McGrath85ba2d82008-07-25 19:45:58 -07002130 * If @match_state is nonzero, it's the @p->state value just checked and
2131 * not expected to change. If it changes, i.e. @p might have woken up,
2132 * then return zero. When we succeed in waiting for @p to be off its CPU,
2133 * we return a positive number (its total switch count). If a second call
2134 * a short while later returns the same number, the caller can be sure that
2135 * @p has remained unscheduled the whole time.
2136 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 * The caller must ensure that the task *will* unschedule sometime soon,
2138 * else this function might spin for a *long* time. This function can't
2139 * be called with interrupts off, or it may introduce deadlock with
2140 * smp_call_function() if an IPI is sent by the same process we are
2141 * waiting to become inactive.
2142 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07002143unsigned long wait_task_inactive(struct task_struct *p, long match_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002145 int running, queued;
Peter Zijlstraeb580752015-07-31 21:28:18 +02002146 struct rq_flags rf;
Roland McGrath85ba2d82008-07-25 19:45:58 -07002147 unsigned long ncsw;
Ingo Molnar70b97a72006-07-03 00:25:42 -07002148 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Andi Kleen3a5c3592007-10-15 17:00:14 +02002150 for (;;) {
2151 /*
2152 * We do the initial early heuristics without holding
2153 * any task-queue locks at all. We'll only try to get
2154 * the runqueue lock when things look like they will
2155 * work out!
2156 */
2157 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002158
Andi Kleen3a5c3592007-10-15 17:00:14 +02002159 /*
2160 * If the task is actively running on another CPU
2161 * still, just relax and busy-wait without holding
2162 * any locks.
2163 *
2164 * NOTE! Since we don't hold any locks, it's not
2165 * even sure that "rq" stays as the right runqueue!
2166 * But we don't care, since "task_running()" will
2167 * return false if the runqueue has changed and p
2168 * is actually now running somewhere else!
2169 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07002170 while (task_running(rq, p)) {
2171 if (match_state && unlikely(p->state != match_state))
2172 return 0;
Andi Kleen3a5c3592007-10-15 17:00:14 +02002173 cpu_relax();
Roland McGrath85ba2d82008-07-25 19:45:58 -07002174 }
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002175
Andi Kleen3a5c3592007-10-15 17:00:14 +02002176 /*
2177 * Ok, time to look more closely! We need the rq
2178 * lock now, to be *sure*. If we're wrong, we'll
2179 * just go back and repeat.
2180 */
Peter Zijlstraeb580752015-07-31 21:28:18 +02002181 rq = task_rq_lock(p, &rf);
Peter Zijlstra27a9da62010-05-04 20:36:56 +02002182 trace_sched_wait_task(p);
Andi Kleen3a5c3592007-10-15 17:00:14 +02002183 running = task_running(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002184 queued = task_on_rq_queued(p);
Roland McGrath85ba2d82008-07-25 19:45:58 -07002185 ncsw = 0;
Oleg Nesterovf31e11d2008-08-20 16:54:44 -07002186 if (!match_state || p->state == match_state)
Oleg Nesterov93dcf552008-08-20 16:54:44 -07002187 ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
Peter Zijlstraeb580752015-07-31 21:28:18 +02002188 task_rq_unlock(rq, p, &rf);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07002189
Andi Kleen3a5c3592007-10-15 17:00:14 +02002190 /*
Roland McGrath85ba2d82008-07-25 19:45:58 -07002191 * If it changed from the expected state, bail out now.
2192 */
2193 if (unlikely(!ncsw))
2194 break;
2195
2196 /*
Andi Kleen3a5c3592007-10-15 17:00:14 +02002197 * Was it really running after all now that we
2198 * checked with the proper locks actually held?
2199 *
2200 * Oops. Go back and try again..
2201 */
2202 if (unlikely(running)) {
2203 cpu_relax();
2204 continue;
2205 }
2206
2207 /*
2208 * It's not enough that it's not actively running,
2209 * it must be off the runqueue _entirely_, and not
2210 * preempted!
2211 *
Luis Henriques80dd99b2009-03-16 19:58:09 +00002212 * So if it was still runnable (but just not actively
Andi Kleen3a5c3592007-10-15 17:00:14 +02002213 * running right now), it's preempted, and we should
2214 * yield - it could be a while.
2215 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002216 if (unlikely(queued)) {
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01002217 ktime_t to = NSEC_PER_SEC / HZ;
Thomas Gleixner8eb90c32011-02-23 23:52:21 +00002218
2219 set_current_state(TASK_UNINTERRUPTIBLE);
2220 schedule_hrtimeout(&to, HRTIMER_MODE_REL);
Andi Kleen3a5c3592007-10-15 17:00:14 +02002221 continue;
2222 }
2223
2224 /*
2225 * Ahh, all good. It wasn't running, and it wasn't
2226 * runnable, which means that it will never become
2227 * running in the future either. We're all done!
2228 */
2229 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 }
Roland McGrath85ba2d82008-07-25 19:45:58 -07002231
2232 return ncsw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
2235/***
2236 * kick_process - kick a running thread to enter/exit the kernel
2237 * @p: the to-be-kicked thread
2238 *
2239 * Cause a process which is running on another CPU to enter
2240 * kernel-mode, without any delay. (to get signals handled.)
2241 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002242 * NOTE: this function doesn't have to take the runqueue lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 * because all it wants to ensure is that the remote task enters
2244 * the kernel. If the IPI races and the task has been migrated
2245 * to another CPU then no harm is done and the purpose has been
2246 * achieved as well.
2247 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002248void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
2250 int cpu;
2251
2252 preempt_disable();
2253 cpu = task_cpu(p);
2254 if ((cpu != smp_processor_id()) && task_curr(p))
2255 smp_send_reschedule(cpu);
2256 preempt_enable();
2257}
Rusty Russellb43e3522009-06-12 22:27:00 -06002258EXPORT_SYMBOL_GPL(kick_process);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Oleg Nesterov30da6882010-03-15 10:10:19 +01002260/*
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02002261 * ->cpus_ptr is protected by both rq->lock and p->pi_lock
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01002262 *
2263 * A few notes on cpu_active vs cpu_online:
2264 *
2265 * - cpu_active must be a subset of cpu_online
2266 *
Ingo Molnar97fb7a02018-03-03 14:01:12 +01002267 * - on CPU-up we allow per-CPU kthreads on the online && !active CPU,
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01002268 * see __set_cpus_allowed_ptr(). At this point the newly online
Ingo Molnard1ccc662017-02-01 11:46:42 +01002269 * CPU isn't yet part of the sched domains, and balancing will not
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01002270 * see it.
2271 *
Ingo Molnard1ccc662017-02-01 11:46:42 +01002272 * - on CPU-down we clear cpu_active() to mask the sched domains and
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01002273 * avoid the load balancer to place new tasks on the to be removed
Ingo Molnard1ccc662017-02-01 11:46:42 +01002274 * CPU. Existing tasks will remain running there and will be taken
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01002275 * off.
2276 *
2277 * This means that fallback selection must not select !active CPUs.
2278 * And can assume that any active CPU must be online. Conversely
2279 * select_task_rq() below may allow selection of !active CPUs in order
2280 * to satisfy the above rules.
Oleg Nesterov30da6882010-03-15 10:10:19 +01002281 */
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002282static int select_fallback_rq(int cpu, struct task_struct *p)
2283{
Tang Chenaa00d892013-02-22 16:33:33 -08002284 int nid = cpu_to_node(cpu);
2285 const struct cpumask *nodemask = NULL;
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002286 enum { cpuset, possible, fail } state = cpuset;
2287 int dest_cpu;
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002288
Tang Chenaa00d892013-02-22 16:33:33 -08002289 /*
Ingo Molnard1ccc662017-02-01 11:46:42 +01002290 * If the node that the CPU is on has been offlined, cpu_to_node()
2291 * will return -1. There is no CPU on the node, and we should
2292 * select the CPU on the other node.
Tang Chenaa00d892013-02-22 16:33:33 -08002293 */
2294 if (nid != -1) {
2295 nodemask = cpumask_of_node(nid);
2296
2297 /* Look for allowed, online CPU in same node. */
2298 for_each_cpu(dest_cpu, nodemask) {
Tang Chenaa00d892013-02-22 16:33:33 -08002299 if (!cpu_active(dest_cpu))
2300 continue;
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02002301 if (cpumask_test_cpu(dest_cpu, p->cpus_ptr))
Tang Chenaa00d892013-02-22 16:33:33 -08002302 return dest_cpu;
2303 }
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002304 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002305
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002306 for (;;) {
2307 /* Any allowed, online CPU? */
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02002308 for_each_cpu(dest_cpu, p->cpus_ptr) {
Peter Zijlstra175f0e22017-07-25 18:58:21 +02002309 if (!is_cpu_allowed(p, dest_cpu))
Tejun Heofeb245e2016-06-16 15:35:04 -04002310 continue;
Peter Zijlstra175f0e22017-07-25 18:58:21 +02002311
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002312 goto out;
2313 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002314
Oleg Nesterove73e85f2015-10-10 20:53:15 +02002315 /* No more Mr. Nice Guy. */
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002316 switch (state) {
2317 case cpuset:
Oleg Nesterove73e85f2015-10-10 20:53:15 +02002318 if (IS_ENABLED(CONFIG_CPUSETS)) {
2319 cpuset_cpus_allowed_fallback(p);
2320 state = possible;
2321 break;
2322 }
Ingo Molnard1ccc662017-02-01 11:46:42 +01002323 /* Fall-through */
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002324 case possible:
2325 do_set_cpus_allowed(p, cpu_possible_mask);
2326 state = fail;
2327 break;
2328
2329 case fail:
2330 BUG();
2331 break;
2332 }
2333 }
2334
2335out:
2336 if (state != cpuset) {
2337 /*
2338 * Don't tell them about moving exiting tasks or
2339 * kernel threads (both mm NULL), since they never
2340 * leave kernel.
2341 */
2342 if (p->mm && printk_ratelimit()) {
John Stultzaac74dc2014-06-04 16:11:40 -07002343 printk_deferred("process %d (%s) no longer affine to cpu%d\n",
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002344 task_pid_nr(p), p->comm, cpu);
2345 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002346 }
2347
2348 return dest_cpu;
2349}
2350
Peter Zijlstrae2912002009-12-16 18:04:36 +01002351/*
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02002352 * The caller (fork, wakeup) owns p->pi_lock, ->cpus_ptr is stable.
Peter Zijlstrae2912002009-12-16 18:04:36 +01002353 */
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002354static inline
Peter Zijlstraac66f542013-10-07 11:29:16 +01002355int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags)
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002356{
Peter Zijlstracbce1a62015-06-11 14:46:54 +02002357 lockdep_assert_held(&p->pi_lock);
2358
Ingo Molnar4b53a342017-02-05 15:41:03 +01002359 if (p->nr_cpus_allowed > 1)
Wanpeng Li6c1d9412014-11-05 09:14:37 +08002360 cpu = p->sched_class->select_task_rq(p, cpu, sd_flags, wake_flags);
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01002361 else
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02002362 cpu = cpumask_any(p->cpus_ptr);
Peter Zijlstrae2912002009-12-16 18:04:36 +01002363
2364 /*
2365 * In order not to call set_task_cpu() on a blocking task we need
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02002366 * to rely on ttwu() to place the task on a valid ->cpus_ptr
Ingo Molnard1ccc662017-02-01 11:46:42 +01002367 * CPU.
Peter Zijlstrae2912002009-12-16 18:04:36 +01002368 *
2369 * Since this is common to all placement strategies, this lives here.
2370 *
2371 * [ this allows ->select_task() to simply return task_cpu(p) and
2372 * not worry about this generic constraint ]
2373 */
Paul Burton7af443e2018-05-26 08:46:47 -07002374 if (unlikely(!is_cpu_allowed(p, cpu)))
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002375 cpu = select_fallback_rq(task_cpu(p), p);
Peter Zijlstrae2912002009-12-16 18:04:36 +01002376
2377 return cpu;
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002378}
Mike Galbraith09a40af2010-04-15 07:29:59 +02002379
Nicolas Pitref5832c12017-05-29 17:02:57 -04002380void sched_set_stop_task(int cpu, struct task_struct *stop)
2381{
2382 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
2383 struct task_struct *old_stop = cpu_rq(cpu)->stop;
2384
2385 if (stop) {
2386 /*
2387 * Make it appear like a SCHED_FIFO task, its something
2388 * userspace knows about and won't get confused about.
2389 *
2390 * Also, it will make PI more or less work without too
2391 * much confusion -- but then, stop work should not
2392 * rely on PI working anyway.
2393 */
2394 sched_setscheduler_nocheck(stop, SCHED_FIFO, &param);
2395
2396 stop->sched_class = &stop_sched_class;
2397 }
2398
2399 cpu_rq(cpu)->stop = stop;
2400
2401 if (old_stop) {
2402 /*
2403 * Reset it back to a normal scheduling class so that
2404 * it can die in pieces.
2405 */
2406 old_stop->sched_class = &rt_sched_class;
2407 }
2408}
2409
Peter Zijlstra25834c72015-05-15 17:43:34 +02002410#else
2411
2412static inline int __set_cpus_allowed_ptr(struct task_struct *p,
2413 const struct cpumask *new_mask, bool check)
2414{
2415 return set_cpus_allowed_ptr(p, new_mask);
2416}
2417
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02002418#endif /* CONFIG_SMP */
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002419
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002420static void
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02002421ttwu_stat(struct task_struct *p, int cpu, int wake_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09002422{
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05002423 struct rq *rq;
2424
2425 if (!schedstat_enabled())
2426 return;
2427
2428 rq = this_rq();
Tejun Heo9ed38112009-12-03 15:08:03 +09002429
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002430#ifdef CONFIG_SMP
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05002431 if (cpu == rq->cpu) {
Peter Zijlstrab85c8b72018-01-16 20:51:06 +01002432 __schedstat_inc(rq->ttwu_local);
2433 __schedstat_inc(p->se.statistics.nr_wakeups_local);
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002434 } else {
2435 struct sched_domain *sd;
2436
Peter Zijlstrab85c8b72018-01-16 20:51:06 +01002437 __schedstat_inc(p->se.statistics.nr_wakeups_remote);
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02002438 rcu_read_lock();
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05002439 for_each_domain(rq->cpu, sd) {
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002440 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Peter Zijlstrab85c8b72018-01-16 20:51:06 +01002441 __schedstat_inc(sd->ttwu_wake_remote);
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002442 break;
2443 }
2444 }
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02002445 rcu_read_unlock();
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002446 }
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02002447
2448 if (wake_flags & WF_MIGRATED)
Peter Zijlstrab85c8b72018-01-16 20:51:06 +01002449 __schedstat_inc(p->se.statistics.nr_wakeups_migrate);
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002450#endif /* CONFIG_SMP */
2451
Peter Zijlstrab85c8b72018-01-16 20:51:06 +01002452 __schedstat_inc(rq->ttwu_count);
2453 __schedstat_inc(p->se.statistics.nr_wakeups);
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002454
2455 if (wake_flags & WF_SYNC)
Peter Zijlstrab85c8b72018-01-16 20:51:06 +01002456 __schedstat_inc(p->se.statistics.nr_wakeups_sync);
Tejun Heo9ed38112009-12-03 15:08:03 +09002457}
2458
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02002459/*
2460 * Mark the task runnable and perform wakeup-preemption.
2461 */
Peter Zijlstrae7904a22015-08-01 19:25:08 +02002462static void ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags,
Matt Flemingd8ac8972016-09-21 14:38:10 +01002463 struct rq_flags *rf)
Tejun Heo9ed38112009-12-03 15:08:03 +09002464{
Tejun Heo9ed38112009-12-03 15:08:03 +09002465 check_preempt_curr(rq, p, wake_flags);
Tejun Heo9ed38112009-12-03 15:08:03 +09002466 p->state = TASK_RUNNING;
Peter Zijlstrafbd705a2015-06-09 11:13:36 +02002467 trace_sched_wakeup(p);
2468
Tejun Heo9ed38112009-12-03 15:08:03 +09002469#ifdef CONFIG_SMP
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02002470 if (p->sched_class->task_woken) {
2471 /*
Peter Zijlstracbce1a62015-06-11 14:46:54 +02002472 * Our task @p is fully woken up and running; so its safe to
2473 * drop the rq->lock, hereafter rq is only used for statistics.
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02002474 */
Matt Flemingd8ac8972016-09-21 14:38:10 +01002475 rq_unpin_lock(rq, rf);
Tejun Heo9ed38112009-12-03 15:08:03 +09002476 p->sched_class->task_woken(rq, p);
Matt Flemingd8ac8972016-09-21 14:38:10 +01002477 rq_repin_lock(rq, rf);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02002478 }
Tejun Heo9ed38112009-12-03 15:08:03 +09002479
Steven Rostedte69c6342010-12-06 17:10:31 -05002480 if (rq->idle_stamp) {
Frederic Weisbecker78becc22013-04-12 01:51:02 +02002481 u64 delta = rq_clock(rq) - rq->idle_stamp;
Jason Low9bd721c2013-09-13 11:26:52 -07002482 u64 max = 2*rq->max_idle_balance_cost;
Tejun Heo9ed38112009-12-03 15:08:03 +09002483
Jason Lowabfafa52013-09-13 11:26:51 -07002484 update_avg(&rq->avg_idle, delta);
2485
2486 if (rq->avg_idle > max)
Tejun Heo9ed38112009-12-03 15:08:03 +09002487 rq->avg_idle = max;
Jason Lowabfafa52013-09-13 11:26:51 -07002488
Tejun Heo9ed38112009-12-03 15:08:03 +09002489 rq->idle_stamp = 0;
2490 }
2491#endif
2492}
2493
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002494static void
Peter Zijlstrae7904a22015-08-01 19:25:08 +02002495ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags,
Matt Flemingd8ac8972016-09-21 14:38:10 +01002496 struct rq_flags *rf)
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002497{
Peter Zijlstra77558e42017-02-21 14:36:23 +01002498 int en_flags = ENQUEUE_WAKEUP | ENQUEUE_NOCLOCK;
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02002499
Peter Zijlstracbce1a62015-06-11 14:46:54 +02002500 lockdep_assert_held(&rq->lock);
2501
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002502 if (p->sched_contributes_to_load)
2503 rq->nr_uninterruptible--;
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02002504
Peter Zijlstradbfb0892020-07-03 12:40:33 +02002505#ifdef CONFIG_SMP
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02002506 if (wake_flags & WF_MIGRATED)
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02002507 en_flags |= ENQUEUE_MIGRATED;
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002508#endif
2509
Peter Zijlstra1b174a22019-04-09 09:53:13 +02002510 activate_task(rq, p, en_flags);
Matt Flemingd8ac8972016-09-21 14:38:10 +01002511 ttwu_do_wakeup(rq, p, wake_flags, rf);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002512}
2513
2514/*
Peter Zijlstra58877d32020-07-02 14:52:11 +02002515 * Consider @p being inside a wait loop:
2516 *
2517 * for (;;) {
2518 * set_current_state(TASK_UNINTERRUPTIBLE);
2519 *
2520 * if (CONDITION)
2521 * break;
2522 *
2523 * schedule();
2524 * }
2525 * __set_current_state(TASK_RUNNING);
2526 *
2527 * between set_current_state() and schedule(). In this case @p is still
2528 * runnable, so all that needs doing is change p->state back to TASK_RUNNING in
2529 * an atomic manner.
2530 *
2531 * By taking task_rq(p)->lock we serialize against schedule(), if @p->on_rq
2532 * then schedule() must still happen and p->state can be changed to
2533 * TASK_RUNNING. Otherwise we lost the race, schedule() has happened, and we
2534 * need to do a full wakeup with enqueue.
2535 *
2536 * Returns: %true when the wakeup is done,
2537 * %false otherwise.
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002538 */
Peter Zijlstra58877d32020-07-02 14:52:11 +02002539static int ttwu_runnable(struct task_struct *p, int wake_flags)
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002540{
Peter Zijlstraeb580752015-07-31 21:28:18 +02002541 struct rq_flags rf;
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002542 struct rq *rq;
2543 int ret = 0;
2544
Peter Zijlstraeb580752015-07-31 21:28:18 +02002545 rq = __task_rq_lock(p, &rf);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002546 if (task_on_rq_queued(p)) {
Frederic Weisbecker1ad4ec02013-04-12 01:51:00 +02002547 /* check_preempt_curr() may use rq clock */
2548 update_rq_clock(rq);
Matt Flemingd8ac8972016-09-21 14:38:10 +01002549 ttwu_do_wakeup(rq, p, wake_flags, &rf);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002550 ret = 1;
2551 }
Peter Zijlstraeb580752015-07-31 21:28:18 +02002552 __task_rq_unlock(rq, &rf);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002553
2554 return ret;
2555}
2556
Peter Zijlstra317f3942011-04-05 17:23:58 +02002557#ifdef CONFIG_SMP
Peter Zijlstraa14886642020-05-26 18:11:04 +02002558void sched_ttwu_pending(void *arg)
Peter Zijlstra317f3942011-04-05 17:23:58 +02002559{
Peter Zijlstraa14886642020-05-26 18:11:04 +02002560 struct llist_node *llist = arg;
Peter Zijlstra317f3942011-04-05 17:23:58 +02002561 struct rq *rq = this_rq();
Byungchul Park73215842017-05-12 09:39:44 +09002562 struct task_struct *p, *t;
Matt Flemingd8ac8972016-09-21 14:38:10 +01002563 struct rq_flags rf;
Peter Zijlstra317f3942011-04-05 17:23:58 +02002564
Peter Zijlstrae3baac42014-06-04 10:31:18 -07002565 if (!llist)
2566 return;
2567
Peter Zijlstra126c2092020-05-26 18:11:03 +02002568 /*
2569 * rq::ttwu_pending racy indication of out-standing wakeups.
2570 * Races such that false-negatives are possible, since they
2571 * are shorter lived that false-positives would be.
2572 */
2573 WRITE_ONCE(rq->ttwu_pending, 0);
2574
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02002575 rq_lock_irqsave(rq, &rf);
Peter Zijlstra77558e42017-02-21 14:36:23 +01002576 update_rq_clock(rq);
Peter Zijlstra317f3942011-04-05 17:23:58 +02002577
Peter Zijlstra8c4890d2020-06-22 12:01:25 +02002578 llist_for_each_entry_safe(p, t, llist, wake_entry.llist) {
Peter Zijlstrab6e13e82020-06-22 12:01:23 +02002579 if (WARN_ON_ONCE(p->on_cpu))
2580 smp_cond_load_acquire(&p->on_cpu, !VAL);
2581
2582 if (WARN_ON_ONCE(task_cpu(p) != cpu_of(rq)))
2583 set_task_cpu(p, cpu_of(rq));
2584
Byungchul Park73215842017-05-12 09:39:44 +09002585 ttwu_do_activate(rq, p, p->sched_remote_wakeup ? WF_MIGRATED : 0, &rf);
Peter Zijlstrab6e13e82020-06-22 12:01:23 +02002586 }
Peter Zijlstra317f3942011-04-05 17:23:58 +02002587
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02002588 rq_unlock_irqrestore(rq, &rf);
Peter Zijlstra317f3942011-04-05 17:23:58 +02002589}
2590
Peter Zijlstrab2a02fc2020-05-26 18:11:01 +02002591void send_call_function_single_ipi(int cpu)
Peter Zijlstra317f3942011-04-05 17:23:58 +02002592{
Peter Zijlstrab2a02fc2020-05-26 18:11:01 +02002593 struct rq *rq = cpu_rq(cpu);
Peter Zijlstraf27dde82013-08-14 14:55:31 +02002594
Peter Zijlstrab2a02fc2020-05-26 18:11:01 +02002595 if (!set_nr_if_polling(rq->idle))
2596 arch_send_call_function_single_ipi(cpu);
2597 else
2598 trace_sched_wake_idle_without_ipi(cpu);
Peter Zijlstra317f3942011-04-05 17:23:58 +02002599}
2600
Mel Gorman2ebb1772020-05-24 21:29:56 +01002601/*
2602 * Queue a task on the target CPUs wake_list and wake the CPU via IPI if
2603 * necessary. The wakee CPU on receipt of the IPI will queue the task
2604 * via sched_ttwu_wakeup() for activation so the wakee incurs the cost
2605 * of the wakeup instead of the waker.
2606 */
2607static void __ttwu_queue_wakelist(struct task_struct *p, int cpu, int wake_flags)
Peter Zijlstra317f3942011-04-05 17:23:58 +02002608{
Peter Zijlstrae3baac42014-06-04 10:31:18 -07002609 struct rq *rq = cpu_rq(cpu);
2610
Peter Zijlstrab7e7ade2016-05-23 11:19:07 +02002611 p->sched_remote_wakeup = !!(wake_flags & WF_MIGRATED);
2612
Peter Zijlstra126c2092020-05-26 18:11:03 +02002613 WRITE_ONCE(rq->ttwu_pending, 1);
Peter Zijlstra8c4890d2020-06-22 12:01:25 +02002614 __smp_call_single_queue(cpu, &p->wake_entry.llist);
Peter Zijlstra317f3942011-04-05 17:23:58 +02002615}
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02002616
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08002617void wake_up_if_idle(int cpu)
2618{
2619 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02002620 struct rq_flags rf;
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08002621
Andy Lutomirskifd7de1e82014-11-29 08:13:51 -08002622 rcu_read_lock();
2623
2624 if (!is_idle_task(rcu_dereference(rq->curr)))
2625 goto out;
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08002626
2627 if (set_nr_if_polling(rq->idle)) {
2628 trace_sched_wake_idle_without_ipi(cpu);
2629 } else {
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02002630 rq_lock_irqsave(rq, &rf);
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08002631 if (is_idle_task(rq->curr))
2632 smp_send_reschedule(cpu);
Ingo Molnard1ccc662017-02-01 11:46:42 +01002633 /* Else CPU is not idle, do nothing here: */
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02002634 rq_unlock_irqrestore(rq, &rf);
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08002635 }
Andy Lutomirskifd7de1e82014-11-29 08:13:51 -08002636
2637out:
2638 rcu_read_unlock();
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08002639}
2640
Peter Zijlstra39be3502012-01-26 12:44:34 +01002641bool cpus_share_cache(int this_cpu, int that_cpu)
Peter Zijlstra518cd622011-12-07 15:07:31 +01002642{
2643 return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
2644}
Peter Zijlstrac6e7bd72020-05-24 21:29:55 +01002645
Mel Gorman2ebb1772020-05-24 21:29:56 +01002646static inline bool ttwu_queue_cond(int cpu, int wake_flags)
Peter Zijlstrac6e7bd72020-05-24 21:29:55 +01002647{
Mel Gorman2ebb1772020-05-24 21:29:56 +01002648 /*
2649 * If the CPU does not share cache, then queue the task on the
2650 * remote rqs wakelist to avoid accessing remote data.
2651 */
2652 if (!cpus_share_cache(smp_processor_id(), cpu))
2653 return true;
2654
2655 /*
2656 * If the task is descheduling and the only running task on the
2657 * CPU then use the wakelist to offload the task activation to
2658 * the soon-to-be-idle CPU as the current CPU is likely busy.
2659 * nr_running is checked to avoid unnecessary task stacking.
2660 */
Peter Zijlstra739f70b2020-06-22 12:01:24 +02002661 if ((wake_flags & WF_ON_CPU) && cpu_rq(cpu)->nr_running <= 1)
Mel Gorman2ebb1772020-05-24 21:29:56 +01002662 return true;
2663
2664 return false;
2665}
2666
2667static bool ttwu_queue_wakelist(struct task_struct *p, int cpu, int wake_flags)
2668{
2669 if (sched_feat(TTWU_QUEUE) && ttwu_queue_cond(cpu, wake_flags)) {
Peter Zijlstrab6e13e82020-06-22 12:01:23 +02002670 if (WARN_ON_ONCE(cpu == smp_processor_id()))
2671 return false;
2672
Peter Zijlstrac6e7bd72020-05-24 21:29:55 +01002673 sched_clock_cpu(cpu); /* Sync clocks across CPUs */
Mel Gorman2ebb1772020-05-24 21:29:56 +01002674 __ttwu_queue_wakelist(p, cpu, wake_flags);
Peter Zijlstrac6e7bd72020-05-24 21:29:55 +01002675 return true;
2676 }
2677
2678 return false;
2679}
Peter Zijlstra58877d32020-07-02 14:52:11 +02002680
2681#else /* !CONFIG_SMP */
2682
2683static inline bool ttwu_queue_wakelist(struct task_struct *p, int cpu, int wake_flags)
2684{
2685 return false;
2686}
2687
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02002688#endif /* CONFIG_SMP */
Peter Zijlstra317f3942011-04-05 17:23:58 +02002689
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02002690static void ttwu_queue(struct task_struct *p, int cpu, int wake_flags)
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002691{
2692 struct rq *rq = cpu_rq(cpu);
Matt Flemingd8ac8972016-09-21 14:38:10 +01002693 struct rq_flags rf;
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002694
Mel Gorman2ebb1772020-05-24 21:29:56 +01002695 if (ttwu_queue_wakelist(p, cpu, wake_flags))
Peter Zijlstra317f3942011-04-05 17:23:58 +02002696 return;
Peter Zijlstra317f3942011-04-05 17:23:58 +02002697
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02002698 rq_lock(rq, &rf);
Peter Zijlstra77558e42017-02-21 14:36:23 +01002699 update_rq_clock(rq);
Matt Flemingd8ac8972016-09-21 14:38:10 +01002700 ttwu_do_activate(rq, p, wake_flags, &rf);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02002701 rq_unlock(rq, &rf);
Tejun Heo9ed38112009-12-03 15:08:03 +09002702}
2703
Peter Zijlstra8643cda2015-11-17 19:01:11 +01002704/*
2705 * Notes on Program-Order guarantees on SMP systems.
2706 *
2707 * MIGRATION
2708 *
2709 * The basic program-order guarantee on SMP systems is that when a task [t]
Ingo Molnard1ccc662017-02-01 11:46:42 +01002710 * migrates, all its activity on its old CPU [c0] happens-before any subsequent
2711 * execution on its new CPU [c1].
Peter Zijlstra8643cda2015-11-17 19:01:11 +01002712 *
2713 * For migration (of runnable tasks) this is provided by the following means:
2714 *
2715 * A) UNLOCK of the rq(c0)->lock scheduling out task t
2716 * B) migration for t is required to synchronize *both* rq(c0)->lock and
2717 * rq(c1)->lock (if not at the same time, then in that order).
2718 * C) LOCK of the rq(c1)->lock scheduling in task
2719 *
Andrea Parri7696f992018-07-16 11:06:03 -07002720 * Release/acquire chaining guarantees that B happens after A and C after B.
Ingo Molnard1ccc662017-02-01 11:46:42 +01002721 * Note: the CPU doing B need not be c0 or c1
Peter Zijlstra8643cda2015-11-17 19:01:11 +01002722 *
2723 * Example:
2724 *
2725 * CPU0 CPU1 CPU2
2726 *
2727 * LOCK rq(0)->lock
2728 * sched-out X
2729 * sched-in Y
2730 * UNLOCK rq(0)->lock
2731 *
2732 * LOCK rq(0)->lock // orders against CPU0
2733 * dequeue X
2734 * UNLOCK rq(0)->lock
2735 *
2736 * LOCK rq(1)->lock
2737 * enqueue X
2738 * UNLOCK rq(1)->lock
2739 *
2740 * LOCK rq(1)->lock // orders against CPU2
2741 * sched-out Z
2742 * sched-in X
2743 * UNLOCK rq(1)->lock
2744 *
2745 *
2746 * BLOCKING -- aka. SLEEP + WAKEUP
2747 *
2748 * For blocking we (obviously) need to provide the same guarantee as for
2749 * migration. However the means are completely different as there is no lock
2750 * chain to provide order. Instead we do:
2751 *
Peter Zijlstra58877d32020-07-02 14:52:11 +02002752 * 1) smp_store_release(X->on_cpu, 0) -- finish_task()
2753 * 2) smp_cond_load_acquire(!X->on_cpu) -- try_to_wake_up()
Peter Zijlstra8643cda2015-11-17 19:01:11 +01002754 *
2755 * Example:
2756 *
2757 * CPU0 (schedule) CPU1 (try_to_wake_up) CPU2 (schedule)
2758 *
2759 * LOCK rq(0)->lock LOCK X->pi_lock
2760 * dequeue X
2761 * sched-out X
2762 * smp_store_release(X->on_cpu, 0);
2763 *
Peter Zijlstra1f03e8d2016-04-04 10:57:12 +02002764 * smp_cond_load_acquire(&X->on_cpu, !VAL);
Peter Zijlstra8643cda2015-11-17 19:01:11 +01002765 * X->state = WAKING
2766 * set_task_cpu(X,2)
2767 *
2768 * LOCK rq(2)->lock
2769 * enqueue X
2770 * X->state = RUNNING
2771 * UNLOCK rq(2)->lock
2772 *
2773 * LOCK rq(2)->lock // orders against CPU1
2774 * sched-out Z
2775 * sched-in X
2776 * UNLOCK rq(2)->lock
2777 *
2778 * UNLOCK X->pi_lock
2779 * UNLOCK rq(0)->lock
2780 *
2781 *
Andrea Parri7696f992018-07-16 11:06:03 -07002782 * However, for wakeups there is a second guarantee we must provide, namely we
2783 * must ensure that CONDITION=1 done by the caller can not be reordered with
2784 * accesses to the task state; see try_to_wake_up() and set_current_state().
Peter Zijlstra8643cda2015-11-17 19:01:11 +01002785 */
2786
Tejun Heo9ed38112009-12-03 15:08:03 +09002787/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 * try_to_wake_up - wake up a thread
Tejun Heo9ed38112009-12-03 15:08:03 +09002789 * @p: the thread to be awakened
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 * @state: the mask of task states that can be woken
Tejun Heo9ed38112009-12-03 15:08:03 +09002791 * @wake_flags: wake modifier flags (WF_*)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 *
Peter Zijlstra58877d32020-07-02 14:52:11 +02002793 * Conceptually does:
2794 *
2795 * If (@state & @p->state) @p->state = TASK_RUNNING.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 *
Peter Zijlstraa2250232016-10-19 15:45:27 +02002797 * If the task was not queued/runnable, also place it back on a runqueue.
2798 *
Peter Zijlstra58877d32020-07-02 14:52:11 +02002799 * This function is atomic against schedule() which would dequeue the task.
Peter Zijlstraa2250232016-10-19 15:45:27 +02002800 *
Peter Zijlstra58877d32020-07-02 14:52:11 +02002801 * It issues a full memory barrier before accessing @p->state, see the comment
2802 * with set_current_state().
2803 *
2804 * Uses p->pi_lock to serialize against concurrent wake-ups.
2805 *
2806 * Relies on p->pi_lock stabilizing:
2807 * - p->sched_class
2808 * - p->cpus_ptr
2809 * - p->sched_task_group
2810 * in order to do migration, see its use of select_task_rq()/set_task_cpu().
2811 *
2812 * Tries really hard to only take one task_rq(p)->lock for performance.
2813 * Takes rq->lock in:
2814 * - ttwu_runnable() -- old rq, unavoidable, see comment there;
2815 * - ttwu_queue() -- new rq, for enqueue of the task;
2816 * - psi_ttwu_dequeue() -- much sadness :-( accounting will kill us.
2817 *
2818 * As a consequence we race really badly with just about everything. See the
2819 * many memory barriers and their comments for details.
Andrea Parri7696f992018-07-16 11:06:03 -07002820 *
Peter Zijlstraa2250232016-10-19 15:45:27 +02002821 * Return: %true if @p->state changes (an actual wakeup was done),
2822 * %false otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 */
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02002824static int
2825try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 unsigned long flags;
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002828 int cpu, success = 0;
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02002829
Peter Zijlstrae3d85482019-07-10 12:57:36 +02002830 preempt_disable();
Peter Zijlstraaacedf22019-06-07 15:39:49 +02002831 if (p == current) {
2832 /*
2833 * We're waking current, this means 'p->on_rq' and 'task_cpu(p)
2834 * == smp_processor_id()'. Together this means we can special
Peter Zijlstra58877d32020-07-02 14:52:11 +02002835 * case the whole 'p->on_rq && ttwu_runnable()' case below
Peter Zijlstraaacedf22019-06-07 15:39:49 +02002836 * without taking any locks.
2837 *
2838 * In particular:
2839 * - we rely on Program-Order guarantees for all the ordering,
2840 * - we're serialized against set_special_state() by virtue of
2841 * it disabling IRQs (this allows not taking ->pi_lock).
2842 */
2843 if (!(p->state & state))
Peter Zijlstrae3d85482019-07-10 12:57:36 +02002844 goto out;
Peter Zijlstraaacedf22019-06-07 15:39:49 +02002845
2846 success = 1;
Peter Zijlstraaacedf22019-06-07 15:39:49 +02002847 trace_sched_waking(p);
2848 p->state = TASK_RUNNING;
2849 trace_sched_wakeup(p);
2850 goto out;
2851 }
2852
Oleg Nesterove0acd0a2013-08-12 18:14:00 +02002853 /*
2854 * If we are going to wake up a thread waiting for CONDITION we
2855 * need to ensure that CONDITION=1 done by the caller can not be
Peter Zijlstra58877d32020-07-02 14:52:11 +02002856 * reordered with p->state check below. This pairs with smp_store_mb()
2857 * in set_current_state() that the waiting thread does.
Oleg Nesterove0acd0a2013-08-12 18:14:00 +02002858 */
Peter Zijlstra013fdb82011-04-05 17:23:45 +02002859 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstrad89e588c2016-09-05 11:37:53 +02002860 smp_mb__after_spinlock();
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002861 if (!(p->state & state))
Peter Zijlstraaacedf22019-06-07 15:39:49 +02002862 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Peter Zijlstrafbd705a2015-06-09 11:13:36 +02002864 trace_sched_waking(p);
2865
Ingo Molnard1ccc662017-02-01 11:46:42 +01002866 /* We're going to change ->state: */
2867 success = 1;
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002868
Balbir Singh135e8c92016-09-05 13:16:40 +10002869 /*
2870 * Ensure we load p->on_rq _after_ p->state, otherwise it would
2871 * be possible to, falsely, observe p->on_rq == 0 and get stuck
2872 * in smp_cond_load_acquire() below.
2873 *
Andrea Parri3d85b272018-07-16 11:06:02 -07002874 * sched_ttwu_pending() try_to_wake_up()
2875 * STORE p->on_rq = 1 LOAD p->state
2876 * UNLOCK rq->lock
2877 *
2878 * __schedule() (switch to task 'p')
2879 * LOCK rq->lock smp_rmb();
2880 * smp_mb__after_spinlock();
2881 * UNLOCK rq->lock
Balbir Singh135e8c92016-09-05 13:16:40 +10002882 *
2883 * [task p]
Andrea Parri3d85b272018-07-16 11:06:02 -07002884 * STORE p->state = UNINTERRUPTIBLE LOAD p->on_rq
Balbir Singh135e8c92016-09-05 13:16:40 +10002885 *
Andrea Parri3d85b272018-07-16 11:06:02 -07002886 * Pairs with the LOCK+smp_mb__after_spinlock() on rq->lock in
2887 * __schedule(). See the comment for smp_mb__after_spinlock().
Paul E. McKenney2beaf322020-03-11 14:23:21 -07002888 *
2889 * A similar smb_rmb() lives in try_invoke_on_locked_down_task().
Balbir Singh135e8c92016-09-05 13:16:40 +10002890 */
2891 smp_rmb();
Peter Zijlstra58877d32020-07-02 14:52:11 +02002892 if (READ_ONCE(p->on_rq) && ttwu_runnable(p, wake_flags))
Peter Zijlstraaacedf22019-06-07 15:39:49 +02002893 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
Peter Zijlstrac6e7bd72020-05-24 21:29:55 +01002895 if (p->in_iowait) {
2896 delayacct_blkio_end(p);
2897 atomic_dec(&task_rq(p)->nr_iowait);
2898 }
2899
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900#ifdef CONFIG_SMP
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002901 /*
Peter Zijlstraecf7d012015-10-07 14:14:13 +02002902 * Ensure we load p->on_cpu _after_ p->on_rq, otherwise it would be
2903 * possible to, falsely, observe p->on_cpu == 0.
2904 *
2905 * One must be running (->on_cpu == 1) in order to remove oneself
2906 * from the runqueue.
2907 *
Andrea Parri3d85b272018-07-16 11:06:02 -07002908 * __schedule() (switch to task 'p') try_to_wake_up()
2909 * STORE p->on_cpu = 1 LOAD p->on_rq
2910 * UNLOCK rq->lock
Peter Zijlstraecf7d012015-10-07 14:14:13 +02002911 *
Andrea Parri3d85b272018-07-16 11:06:02 -07002912 * __schedule() (put 'p' to sleep)
2913 * LOCK rq->lock smp_rmb();
2914 * smp_mb__after_spinlock();
2915 * STORE p->on_rq = 0 LOAD p->on_cpu
2916 *
2917 * Pairs with the LOCK+smp_mb__after_spinlock() on rq->lock in
2918 * __schedule(). See the comment for smp_mb__after_spinlock().
Peter Zijlstradbfb0892020-07-03 12:40:33 +02002919 *
2920 * Form a control-dep-acquire with p->on_rq == 0 above, to ensure
2921 * schedule()'s deactivate_task() has 'happened' and p will no longer
2922 * care about it's own p->state. See the comment in __schedule().
Peter Zijlstraecf7d012015-10-07 14:14:13 +02002923 */
Peter Zijlstradbfb0892020-07-03 12:40:33 +02002924 smp_acquire__after_ctrl_dep();
2925
2926 /*
2927 * We're doing the wakeup (@success == 1), they did a dequeue (p->on_rq
2928 * == 0), which means we need to do an enqueue, change p->state to
2929 * TASK_WAKING such that we can unlock p->pi_lock before doing the
2930 * enqueue, such as ttwu_queue_wakelist().
2931 */
2932 p->state = TASK_WAKING;
Peter Zijlstraecf7d012015-10-07 14:14:13 +02002933
2934 /*
Ingo Molnard1ccc662017-02-01 11:46:42 +01002935 * If the owning (remote) CPU is still in the middle of schedule() with
Peter Zijlstrac6e7bd72020-05-24 21:29:55 +01002936 * this task as prev, considering queueing p on the remote CPUs wake_list
2937 * which potentially sends an IPI instead of spinning on p->on_cpu to
2938 * let the waker make forward progress. This is safe because IRQs are
2939 * disabled and the IPI will deliver after on_cpu is cleared.
Peter Zijlstrab6e13e82020-06-22 12:01:23 +02002940 *
2941 * Ensure we load task_cpu(p) after p->on_cpu:
2942 *
2943 * set_task_cpu(p, cpu);
2944 * STORE p->cpu = @cpu
2945 * __schedule() (switch to task 'p')
2946 * LOCK rq->lock
2947 * smp_mb__after_spin_lock() smp_cond_load_acquire(&p->on_cpu)
2948 * STORE p->on_cpu = 1 LOAD p->cpu
2949 *
2950 * to ensure we observe the correct CPU on which the task is currently
2951 * scheduling.
Peter Zijlstrac6e7bd72020-05-24 21:29:55 +01002952 */
Peter Zijlstrab6e13e82020-06-22 12:01:23 +02002953 if (smp_load_acquire(&p->on_cpu) &&
Peter Zijlstra739f70b2020-06-22 12:01:24 +02002954 ttwu_queue_wakelist(p, task_cpu(p), wake_flags | WF_ON_CPU))
Peter Zijlstrac6e7bd72020-05-24 21:29:55 +01002955 goto unlock;
2956
2957 /*
2958 * If the owning (remote) CPU is still in the middle of schedule() with
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002959 * this task as prev, wait until its done referencing the task.
Peter Zijlstrab75a2252015-10-06 14:36:17 +02002960 *
rodrigosiqueira31cb1bc2017-12-15 12:06:03 -02002961 * Pairs with the smp_store_release() in finish_task().
Peter Zijlstrab75a2252015-10-06 14:36:17 +02002962 *
2963 * This ensures that tasks getting woken will be fully ordered against
2964 * their previous state and preserve Program Order.
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02002965 */
Peter Zijlstra1f03e8d2016-04-04 10:57:12 +02002966 smp_cond_load_acquire(&p->on_cpu, !VAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Peter Zijlstraac66f542013-10-07 11:29:16 +01002968 cpu = select_task_rq(p, p->wake_cpu, SD_BALANCE_WAKE, wake_flags);
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02002969 if (task_cpu(p) != cpu) {
2970 wake_flags |= WF_MIGRATED;
Johannes Weinereb414682018-10-26 15:06:27 -07002971 psi_ttwu_dequeue(p);
Mike Galbraith055a0082009-11-12 11:07:44 +01002972 set_task_cpu(p, cpu);
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02002973 }
Peter Zijlstrab6e13e82020-06-22 12:01:23 +02002974#else
2975 cpu = task_cpu(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02002978 ttwu_queue(p, cpu, wake_flags);
Peter Zijlstraaacedf22019-06-07 15:39:49 +02002979unlock:
Peter Zijlstra013fdb82011-04-05 17:23:45 +02002980 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Peter Zijlstraaacedf22019-06-07 15:39:49 +02002981out:
2982 if (success)
Peter Zijlstrab6e13e82020-06-22 12:01:23 +02002983 ttwu_stat(p, task_cpu(p), wake_flags);
Peter Zijlstrae3d85482019-07-10 12:57:36 +02002984 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
2986 return success;
2987}
2988
David Howells50fa6102009-04-28 15:01:38 +01002989/**
Paul E. McKenney2beaf322020-03-11 14:23:21 -07002990 * try_invoke_on_locked_down_task - Invoke a function on task in fixed state
2991 * @p: Process for which the function is to be invoked.
2992 * @func: Function to invoke.
2993 * @arg: Argument to function.
2994 *
2995 * If the specified task can be quickly locked into a definite state
2996 * (either sleeping or on a given runqueue), arrange to keep it in that
2997 * state while invoking @func(@arg). This function can use ->on_rq and
2998 * task_curr() to work out what the state is, if required. Given that
2999 * @func can be invoked with a runqueue lock held, it had better be quite
3000 * lightweight.
3001 *
3002 * Returns:
3003 * @false if the task slipped out from under the locks.
3004 * @true if the task was locked onto a runqueue or is sleeping.
3005 * However, @func can override this by returning @false.
3006 */
3007bool try_invoke_on_locked_down_task(struct task_struct *p, bool (*func)(struct task_struct *t, void *arg), void *arg)
3008{
3009 bool ret = false;
3010 struct rq_flags rf;
3011 struct rq *rq;
3012
3013 lockdep_assert_irqs_enabled();
3014 raw_spin_lock_irq(&p->pi_lock);
3015 if (p->on_rq) {
3016 rq = __task_rq_lock(p, &rf);
3017 if (task_rq(p) == rq)
3018 ret = func(p, arg);
3019 rq_unlock(rq, &rf);
3020 } else {
3021 switch (p->state) {
3022 case TASK_RUNNING:
3023 case TASK_WAKING:
3024 break;
3025 default:
3026 smp_rmb(); // See smp_rmb() comment in try_to_wake_up().
3027 if (!p->on_rq)
3028 ret = func(p, arg);
3029 }
3030 }
3031 raw_spin_unlock_irq(&p->pi_lock);
3032 return ret;
3033}
3034
3035/**
David Howells50fa6102009-04-28 15:01:38 +01003036 * wake_up_process - Wake up a specific process
3037 * @p: The process to be woken up.
3038 *
3039 * Attempt to wake up the nominated process and move it to the set of runnable
Yacine Belkadie69f6182013-07-12 20:45:47 +02003040 * processes.
3041 *
3042 * Return: 1 if the process was woken up, 0 if it was already running.
David Howells50fa6102009-04-28 15:01:38 +01003043 *
Andrea Parri7696f992018-07-16 11:06:03 -07003044 * This function executes a full memory barrier before accessing the task state.
David Howells50fa6102009-04-28 15:01:38 +01003045 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08003046int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
Oleg Nesterov9067ac82013-01-21 20:48:17 +01003048 return try_to_wake_up(p, TASK_NORMAL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050EXPORT_SYMBOL(wake_up_process);
3051
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08003052int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053{
3054 return try_to_wake_up(p, state, 0);
3055}
3056
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057/*
3058 * Perform scheduler related setup for a newly forked process p.
3059 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02003060 *
3061 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 */
Rik van Riel5e1576e2013-10-07 11:29:26 +01003063static void __sched_fork(unsigned long clone_flags, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02003065 p->on_rq = 0;
3066
3067 p->se.on_rq = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02003068 p->se.exec_start = 0;
3069 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02003070 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01003071 p->se.nr_migrations = 0;
Peter Zijlstrada7a7352011-01-17 17:03:27 +01003072 p->se.vruntime = 0;
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02003073 INIT_LIST_HEAD(&p->se.group_node);
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02003074
Byungchul Parkad936d82015-10-24 01:16:19 +09003075#ifdef CONFIG_FAIR_GROUP_SCHED
3076 p->se.cfs_rq = NULL;
3077#endif
3078
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02003079#ifdef CONFIG_SCHEDSTATS
Mel Gormancb251762016-02-05 09:08:36 +00003080 /* Even if schedstat is disabled, there should not be garbage */
Lucas De Marchi41acab82010-03-10 23:37:45 -03003081 memset(&p->se.statistics, 0, sizeof(p->se.statistics));
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02003082#endif
Nick Piggin476d1392005-06-25 14:57:29 -07003083
Dario Faggioliaab03e02013-11-28 11:14:43 +01003084 RB_CLEAR_NODE(&p->dl.rb_node);
Peter Zijlstra40767b02015-01-28 15:08:03 +01003085 init_dl_task_timer(&p->dl);
Luca Abeni209a0cb2017-05-18 22:13:29 +02003086 init_dl_inactive_task_timer(&p->dl);
Juri Lellia5e7be32014-09-19 10:22:39 +01003087 __dl_clear_params(p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01003088
Peter Zijlstrafa717062008-01-25 21:08:27 +01003089 INIT_LIST_HEAD(&p->rt.run_list);
Peter Zijlstraff77e462016-01-18 15:27:07 +01003090 p->rt.timeout = 0;
3091 p->rt.time_slice = sched_rr_timeslice;
3092 p->rt.on_rq = 0;
3093 p->rt.on_list = 0;
Nick Piggin476d1392005-06-25 14:57:29 -07003094
Avi Kivitye107be32007-07-26 13:40:43 +02003095#ifdef CONFIG_PREEMPT_NOTIFIERS
3096 INIT_HLIST_HEAD(&p->preempt_notifiers);
3097#endif
Peter Zijlstracbee9f82012-10-25 14:16:43 +02003098
Mel Gorman5e1f0f02019-03-05 15:45:41 -08003099#ifdef CONFIG_COMPACTION
3100 p->capture_control = NULL;
3101#endif
Mel Gorman13784472018-05-04 16:41:09 +01003102 init_numa_balancing(clone_flags, p);
Peter Zijlstraa14886642020-05-26 18:11:04 +02003103#ifdef CONFIG_SMP
Peter Zijlstra8c4890d2020-06-22 12:01:25 +02003104 p->wake_entry.u_flags = CSD_TYPE_TTWU;
Peter Zijlstraa14886642020-05-26 18:11:04 +02003105#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02003106}
3107
Srikar Dronamraju2a595722015-08-11 21:54:21 +05303108DEFINE_STATIC_KEY_FALSE(sched_numa_balancing);
3109
Mel Gorman1a687c22012-11-22 11:16:36 +00003110#ifdef CONFIG_NUMA_BALANCING
Mel Gorman3105b862012-11-23 11:23:49 +00003111
3112void set_numabalancing_state(bool enabled)
3113{
Srikar Dronamraju2a595722015-08-11 21:54:21 +05303114 if (enabled)
3115 static_branch_enable(&sched_numa_balancing);
3116 else
3117 static_branch_disable(&sched_numa_balancing);
Mel Gorman3105b862012-11-23 11:23:49 +00003118}
Andi Kleen54a43d52014-01-23 15:53:13 -08003119
3120#ifdef CONFIG_PROC_SYSCTL
3121int sysctl_numa_balancing(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02003122 void *buffer, size_t *lenp, loff_t *ppos)
Andi Kleen54a43d52014-01-23 15:53:13 -08003123{
3124 struct ctl_table t;
3125 int err;
Srikar Dronamraju2a595722015-08-11 21:54:21 +05303126 int state = static_branch_likely(&sched_numa_balancing);
Andi Kleen54a43d52014-01-23 15:53:13 -08003127
3128 if (write && !capable(CAP_SYS_ADMIN))
3129 return -EPERM;
3130
3131 t = *table;
3132 t.data = &state;
3133 err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
3134 if (err < 0)
3135 return err;
3136 if (write)
3137 set_numabalancing_state(state);
3138 return err;
3139}
3140#endif
3141#endif
Mel Gorman1a687c22012-11-22 11:16:36 +00003142
Mel Gormancb251762016-02-05 09:08:36 +00003143#ifdef CONFIG_SCHEDSTATS
Josh Poimboeuf4698f882016-06-07 14:43:16 -05003144
3145DEFINE_STATIC_KEY_FALSE(sched_schedstats);
3146static bool __initdata __sched_schedstats = false;
3147
Mel Gormancb251762016-02-05 09:08:36 +00003148static void set_schedstats(bool enabled)
3149{
3150 if (enabled)
3151 static_branch_enable(&sched_schedstats);
3152 else
3153 static_branch_disable(&sched_schedstats);
3154}
3155
3156void force_schedstat_enabled(void)
3157{
3158 if (!schedstat_enabled()) {
3159 pr_info("kernel profiling enabled schedstats, disable via kernel.sched_schedstats.\n");
3160 static_branch_enable(&sched_schedstats);
3161 }
3162}
3163
3164static int __init setup_schedstats(char *str)
3165{
3166 int ret = 0;
3167 if (!str)
3168 goto out;
3169
Josh Poimboeuf4698f882016-06-07 14:43:16 -05003170 /*
3171 * This code is called before jump labels have been set up, so we can't
3172 * change the static branch directly just yet. Instead set a temporary
3173 * variable so init_schedstats() can do it later.
3174 */
Mel Gormancb251762016-02-05 09:08:36 +00003175 if (!strcmp(str, "enable")) {
Josh Poimboeuf4698f882016-06-07 14:43:16 -05003176 __sched_schedstats = true;
Mel Gormancb251762016-02-05 09:08:36 +00003177 ret = 1;
3178 } else if (!strcmp(str, "disable")) {
Josh Poimboeuf4698f882016-06-07 14:43:16 -05003179 __sched_schedstats = false;
Mel Gormancb251762016-02-05 09:08:36 +00003180 ret = 1;
3181 }
3182out:
3183 if (!ret)
3184 pr_warn("Unable to parse schedstats=\n");
3185
3186 return ret;
3187}
3188__setup("schedstats=", setup_schedstats);
3189
Josh Poimboeuf4698f882016-06-07 14:43:16 -05003190static void __init init_schedstats(void)
3191{
3192 set_schedstats(__sched_schedstats);
3193}
3194
Mel Gormancb251762016-02-05 09:08:36 +00003195#ifdef CONFIG_PROC_SYSCTL
Christoph Hellwig32927392020-04-24 08:43:38 +02003196int sysctl_schedstats(struct ctl_table *table, int write, void *buffer,
3197 size_t *lenp, loff_t *ppos)
Mel Gormancb251762016-02-05 09:08:36 +00003198{
3199 struct ctl_table t;
3200 int err;
3201 int state = static_branch_likely(&sched_schedstats);
3202
3203 if (write && !capable(CAP_SYS_ADMIN))
3204 return -EPERM;
3205
3206 t = *table;
3207 t.data = &state;
3208 err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
3209 if (err < 0)
3210 return err;
3211 if (write)
3212 set_schedstats(state);
3213 return err;
3214}
Josh Poimboeuf4698f882016-06-07 14:43:16 -05003215#endif /* CONFIG_PROC_SYSCTL */
3216#else /* !CONFIG_SCHEDSTATS */
3217static inline void init_schedstats(void) {}
3218#endif /* CONFIG_SCHEDSTATS */
Mel Gormancb251762016-02-05 09:08:36 +00003219
Ingo Molnardd41f592007-07-09 18:51:59 +02003220/*
3221 * fork()/clone()-time setup:
3222 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01003223int sched_fork(unsigned long clone_flags, struct task_struct *p)
Ingo Molnardd41f592007-07-09 18:51:59 +02003224{
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003225 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02003226
Rik van Riel5e1576e2013-10-07 11:29:26 +01003227 __sched_fork(clone_flags, p);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01003228 /*
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02003229 * We mark the process as NEW here. This guarantees that
Peter Zijlstra06b83b52009-12-16 18:04:35 +01003230 * nobody will actually run it, and a signal or other external
3231 * event cannot wake it up and insert it on the runqueue either.
3232 */
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02003233 p->state = TASK_NEW;
Ingo Molnardd41f592007-07-09 18:51:59 +02003234
Ingo Molnarb29739f2006-06-27 02:54:51 -07003235 /*
Mike Galbraithc350a042011-07-27 17:14:55 +02003236 * Make sure we do not leak PI boosting priority to the child.
3237 */
3238 p->prio = current->normal_prio;
3239
Patrick Bellasie8f14172019-06-21 09:42:05 +01003240 uclamp_fork(p);
3241
Mike Galbraithc350a042011-07-27 17:14:55 +02003242 /*
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02003243 * Revert to default priority/policy on fork if requested.
3244 */
3245 if (unlikely(p->sched_reset_on_fork)) {
Dario Faggioliaab03e02013-11-28 11:14:43 +01003246 if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02003247 p->policy = SCHED_NORMAL;
Mike Galbraith6c697bd2009-06-17 10:48:02 +02003248 p->static_prio = NICE_TO_PRIO(0);
Mike Galbraithc350a042011-07-27 17:14:55 +02003249 p->rt_priority = 0;
3250 } else if (PRIO_TO_NICE(p->static_prio) < 0)
3251 p->static_prio = NICE_TO_PRIO(0);
3252
3253 p->prio = p->normal_prio = __normal_prio(p);
Vincent Guittot90593932017-05-17 11:50:45 +02003254 set_load_weight(p, false);
Mike Galbraith6c697bd2009-06-17 10:48:02 +02003255
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02003256 /*
3257 * We don't need the reset flag anymore after the fork. It has
3258 * fulfilled its duty:
3259 */
3260 p->sched_reset_on_fork = 0;
3261 }
Lennart Poetteringca94c442009-06-15 17:17:47 +02003262
Sebastian Andrzej Siewioraf0fffd2018-07-06 15:06:15 +02003263 if (dl_prio(p->prio))
Dario Faggioliaab03e02013-11-28 11:14:43 +01003264 return -EAGAIN;
Sebastian Andrzej Siewioraf0fffd2018-07-06 15:06:15 +02003265 else if (rt_prio(p->prio))
Dario Faggioliaab03e02013-11-28 11:14:43 +01003266 p->sched_class = &rt_sched_class;
Sebastian Andrzej Siewioraf0fffd2018-07-06 15:06:15 +02003267 else
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02003268 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07003269
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02003270 init_entity_runnable_average(&p->se);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01003271
Peter Zijlstra86951592010-06-22 11:44:53 +02003272 /*
3273 * The child is not yet in the pid-hash so no cgroup attach races,
3274 * and the cgroup is pinned to this child due to cgroup_fork()
3275 * is ran before sched_fork().
3276 *
3277 * Silence PROVE_RCU.
3278 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003279 raw_spin_lock_irqsave(&p->pi_lock, flags);
Mathieu Desnoyersce3614d2020-07-06 16:49:10 -04003280 rseq_migrate(p);
Peter Zijlstrae210bff2016-06-16 18:51:48 +02003281 /*
Ingo Molnard1ccc662017-02-01 11:46:42 +01003282 * We're setting the CPU for the first time, we don't migrate,
Peter Zijlstrae210bff2016-06-16 18:51:48 +02003283 * so use __set_task_cpu().
3284 */
Sebastian Andrzej Siewioraf0fffd2018-07-06 15:06:15 +02003285 __set_task_cpu(p, smp_processor_id());
Peter Zijlstrae210bff2016-06-16 18:51:48 +02003286 if (p->sched_class->task_fork)
3287 p->sched_class->task_fork(p);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003288 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Peter Zijlstra5f3edc12009-09-10 13:42:00 +02003289
Naveen N. Raof6db8342015-06-25 23:53:37 +05303290#ifdef CONFIG_SCHED_INFO
Ingo Molnardd41f592007-07-09 18:51:59 +02003291 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07003292 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293#endif
Peter Zijlstra3ca7a442011-04-05 17:23:40 +02003294#if defined(CONFIG_SMP)
3295 p->on_cpu = 0;
Nick Piggin4866cde2005-06-25 14:57:23 -07003296#endif
Peter Zijlstra01028742013-08-14 14:55:46 +02003297 init_task_preempt_count(p);
Dario Faggioli806c09a2010-11-30 19:51:33 +01003298#ifdef CONFIG_SMP
Gregory Haskins917b6272008-12-29 09:39:53 -05003299 plist_node_init(&p->pushable_tasks, MAX_PRIO);
Juri Lelli1baca4c2013-11-07 14:43:38 +01003300 RB_CLEAR_NODE(&p->pushable_dl_tasks);
Dario Faggioli806c09a2010-11-30 19:51:33 +01003301#endif
Dario Faggioliaab03e02013-11-28 11:14:43 +01003302 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303}
3304
Qais Yousef13685c42020-07-16 12:03:45 +01003305void sched_post_fork(struct task_struct *p)
3306{
3307 uclamp_post_fork(p);
3308}
3309
Dario Faggioli332ac172013-11-07 14:43:45 +01003310unsigned long to_ratio(u64 period, u64 runtime)
3311{
3312 if (runtime == RUNTIME_INF)
Luca Abenic52f14d2017-05-18 22:13:31 +02003313 return BW_UNIT;
Dario Faggioli332ac172013-11-07 14:43:45 +01003314
3315 /*
3316 * Doing this here saves a lot of checks in all
3317 * the calling paths, and returning zero seems
3318 * safe for them anyway.
3319 */
3320 if (period == 0)
3321 return 0;
3322
Luca Abenic52f14d2017-05-18 22:13:31 +02003323 return div64_u64(runtime << BW_SHIFT, period);
Dario Faggioli332ac172013-11-07 14:43:45 +01003324}
3325
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326/*
3327 * wake_up_new_task - wake up a newly created task for the first time.
3328 *
3329 * This function will do some initial scheduler statistics housekeeping
3330 * that must be done for every newly created context, then puts the task
3331 * on the runqueue and wakes it.
3332 */
Samir Bellabes3e51e3e2011-05-11 18:18:05 +02003333void wake_up_new_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
Peter Zijlstraeb580752015-07-31 21:28:18 +02003335 struct rq_flags rf;
Ingo Molnardd41f592007-07-09 18:51:59 +02003336 struct rq *rq;
Peter Zijlstrafabf3182010-01-21 21:04:57 +01003337
Peter Zijlstraeb580752015-07-31 21:28:18 +02003338 raw_spin_lock_irqsave(&p->pi_lock, rf.flags);
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02003339 p->state = TASK_RUNNING;
Peter Zijlstrafabf3182010-01-21 21:04:57 +01003340#ifdef CONFIG_SMP
3341 /*
3342 * Fork balancing, do it here and not earlier because:
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02003343 * - cpus_ptr can change in the fork path
Ingo Molnard1ccc662017-02-01 11:46:42 +01003344 * - any previously selected CPU might disappear through hotplug
Peter Zijlstrae210bff2016-06-16 18:51:48 +02003345 *
3346 * Use __set_task_cpu() to avoid calling sched_class::migrate_task_rq,
3347 * as we're not fully set-up yet.
Peter Zijlstrafabf3182010-01-21 21:04:57 +01003348 */
Mel Gorman32e839d2018-01-30 10:45:55 +00003349 p->recent_used_cpu = task_cpu(p);
Mathieu Desnoyersce3614d2020-07-06 16:49:10 -04003350 rseq_migrate(p);
Peter Zijlstrae210bff2016-06-16 18:51:48 +02003351 __set_task_cpu(p, select_task_rq(p, task_cpu(p), SD_BALANCE_FORK, 0));
Peter Zijlstrafabf3182010-01-21 21:04:57 +01003352#endif
Peter Zijlstrab7fa30c2016-06-09 15:07:50 +02003353 rq = __task_rq_lock(p, &rf);
Peter Zijlstra4126bad2016-10-03 16:20:59 +02003354 update_rq_clock(rq);
Dietmar Eggemannd0fe0b92019-01-22 16:25:01 +00003355 post_init_entity_util_avg(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
Peter Zijlstra7a57f322017-02-21 14:47:02 +01003357 activate_task(rq, p, ENQUEUE_NOCLOCK);
Peter Zijlstrafbd705a2015-06-09 11:13:36 +02003358 trace_sched_wakeup_new(p);
Peter Zijlstraa7558e02009-09-14 20:02:34 +02003359 check_preempt_curr(rq, p, WF_FORK);
Steven Rostedt9a897c52008-01-25 21:08:22 +01003360#ifdef CONFIG_SMP
Peter Zijlstra0aaafaa2015-10-23 11:50:08 +02003361 if (p->sched_class->task_woken) {
3362 /*
3363 * Nothing relies on rq->lock after this, so its fine to
3364 * drop it.
3365 */
Matt Flemingd8ac8972016-09-21 14:38:10 +01003366 rq_unpin_lock(rq, &rf);
Peter Zijlstraefbbd052009-12-16 18:04:40 +01003367 p->sched_class->task_woken(rq, p);
Matt Flemingd8ac8972016-09-21 14:38:10 +01003368 rq_repin_lock(rq, &rf);
Peter Zijlstra0aaafaa2015-10-23 11:50:08 +02003369 }
Steven Rostedt9a897c52008-01-25 21:08:22 +01003370#endif
Peter Zijlstraeb580752015-07-31 21:28:18 +02003371 task_rq_unlock(rq, p, &rf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372}
3373
Avi Kivitye107be32007-07-26 13:40:43 +02003374#ifdef CONFIG_PREEMPT_NOTIFIERS
3375
Davidlohr Buesob7203422018-03-26 14:09:26 -07003376static DEFINE_STATIC_KEY_FALSE(preempt_notifier_key);
Peter Zijlstra1cde2932015-06-08 16:00:30 +02003377
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +02003378void preempt_notifier_inc(void)
3379{
Davidlohr Buesob7203422018-03-26 14:09:26 -07003380 static_branch_inc(&preempt_notifier_key);
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +02003381}
3382EXPORT_SYMBOL_GPL(preempt_notifier_inc);
3383
3384void preempt_notifier_dec(void)
3385{
Davidlohr Buesob7203422018-03-26 14:09:26 -07003386 static_branch_dec(&preempt_notifier_key);
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +02003387}
3388EXPORT_SYMBOL_GPL(preempt_notifier_dec);
3389
Avi Kivitye107be32007-07-26 13:40:43 +02003390/**
Luis Henriques80dd99b2009-03-16 19:58:09 +00003391 * preempt_notifier_register - tell me when current is being preempted & rescheduled
Randy Dunlap421cee22007-07-31 00:37:50 -07003392 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02003393 */
3394void preempt_notifier_register(struct preempt_notifier *notifier)
3395{
Davidlohr Buesob7203422018-03-26 14:09:26 -07003396 if (!static_branch_unlikely(&preempt_notifier_key))
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +02003397 WARN(1, "registering preempt_notifier while notifiers disabled\n");
3398
Avi Kivitye107be32007-07-26 13:40:43 +02003399 hlist_add_head(&notifier->link, &current->preempt_notifiers);
3400}
3401EXPORT_SYMBOL_GPL(preempt_notifier_register);
3402
3403/**
3404 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07003405 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02003406 *
Mathieu Desnoyersd84525a2015-05-17 12:53:10 -04003407 * This is *not* safe to call from within a preemption notifier.
Avi Kivitye107be32007-07-26 13:40:43 +02003408 */
3409void preempt_notifier_unregister(struct preempt_notifier *notifier)
3410{
3411 hlist_del(&notifier->link);
3412}
3413EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
3414
Peter Zijlstra1cde2932015-06-08 16:00:30 +02003415static void __fire_sched_in_preempt_notifiers(struct task_struct *curr)
Avi Kivitye107be32007-07-26 13:40:43 +02003416{
3417 struct preempt_notifier *notifier;
Avi Kivitye107be32007-07-26 13:40:43 +02003418
Sasha Levinb67bfe02013-02-27 17:06:00 -08003419 hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
Avi Kivitye107be32007-07-26 13:40:43 +02003420 notifier->ops->sched_in(notifier, raw_smp_processor_id());
3421}
3422
Peter Zijlstra1cde2932015-06-08 16:00:30 +02003423static __always_inline void fire_sched_in_preempt_notifiers(struct task_struct *curr)
3424{
Davidlohr Buesob7203422018-03-26 14:09:26 -07003425 if (static_branch_unlikely(&preempt_notifier_key))
Peter Zijlstra1cde2932015-06-08 16:00:30 +02003426 __fire_sched_in_preempt_notifiers(curr);
3427}
3428
Avi Kivitye107be32007-07-26 13:40:43 +02003429static void
Peter Zijlstra1cde2932015-06-08 16:00:30 +02003430__fire_sched_out_preempt_notifiers(struct task_struct *curr,
3431 struct task_struct *next)
Avi Kivitye107be32007-07-26 13:40:43 +02003432{
3433 struct preempt_notifier *notifier;
Avi Kivitye107be32007-07-26 13:40:43 +02003434
Sasha Levinb67bfe02013-02-27 17:06:00 -08003435 hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
Avi Kivitye107be32007-07-26 13:40:43 +02003436 notifier->ops->sched_out(notifier, next);
3437}
3438
Peter Zijlstra1cde2932015-06-08 16:00:30 +02003439static __always_inline void
3440fire_sched_out_preempt_notifiers(struct task_struct *curr,
3441 struct task_struct *next)
3442{
Davidlohr Buesob7203422018-03-26 14:09:26 -07003443 if (static_branch_unlikely(&preempt_notifier_key))
Peter Zijlstra1cde2932015-06-08 16:00:30 +02003444 __fire_sched_out_preempt_notifiers(curr, next);
3445}
3446
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02003447#else /* !CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02003448
Peter Zijlstra1cde2932015-06-08 16:00:30 +02003449static inline void fire_sched_in_preempt_notifiers(struct task_struct *curr)
Avi Kivitye107be32007-07-26 13:40:43 +02003450{
3451}
3452
Peter Zijlstra1cde2932015-06-08 16:00:30 +02003453static inline void
Avi Kivitye107be32007-07-26 13:40:43 +02003454fire_sched_out_preempt_notifiers(struct task_struct *curr,
3455 struct task_struct *next)
3456{
3457}
3458
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02003459#endif /* CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02003460
rodrigosiqueira31cb1bc2017-12-15 12:06:03 -02003461static inline void prepare_task(struct task_struct *next)
3462{
3463#ifdef CONFIG_SMP
3464 /*
3465 * Claim the task as running, we do this before switching to it
3466 * such that any running task will have this set.
Peter Zijlstra58877d32020-07-02 14:52:11 +02003467 *
3468 * See the ttwu() WF_ON_CPU case and its ordering comment.
rodrigosiqueira31cb1bc2017-12-15 12:06:03 -02003469 */
Peter Zijlstra58877d32020-07-02 14:52:11 +02003470 WRITE_ONCE(next->on_cpu, 1);
rodrigosiqueira31cb1bc2017-12-15 12:06:03 -02003471#endif
3472}
3473
3474static inline void finish_task(struct task_struct *prev)
3475{
3476#ifdef CONFIG_SMP
3477 /*
Peter Zijlstra58877d32020-07-02 14:52:11 +02003478 * This must be the very last reference to @prev from this CPU. After
3479 * p->on_cpu is cleared, the task can be moved to a different CPU. We
3480 * must ensure this doesn't happen until the switch is completely
rodrigosiqueira31cb1bc2017-12-15 12:06:03 -02003481 * finished.
3482 *
3483 * In particular, the load of prev->state in finish_task_switch() must
3484 * happen before this.
3485 *
3486 * Pairs with the smp_cond_load_acquire() in try_to_wake_up().
3487 */
3488 smp_store_release(&prev->on_cpu, 0);
3489#endif
3490}
3491
Peter Zijlstra269d5992018-02-06 17:52:13 +01003492static inline void
3493prepare_lock_switch(struct rq *rq, struct task_struct *next, struct rq_flags *rf)
rodrigosiqueira31cb1bc2017-12-15 12:06:03 -02003494{
Peter Zijlstra269d5992018-02-06 17:52:13 +01003495 /*
3496 * Since the runqueue lock will be released by the next
3497 * task (which is an invalid locking op but in the case
3498 * of the scheduler it's an obvious special-case), so we
3499 * do an early lockdep release here:
3500 */
3501 rq_unpin_lock(rq, rf);
Qian Cai5facae42019-09-19 12:09:40 -04003502 spin_release(&rq->lock.dep_map, _THIS_IP_);
rodrigosiqueira31cb1bc2017-12-15 12:06:03 -02003503#ifdef CONFIG_DEBUG_SPINLOCK
3504 /* this is a valid case when another task releases the spinlock */
Peter Zijlstra269d5992018-02-06 17:52:13 +01003505 rq->lock.owner = next;
rodrigosiqueira31cb1bc2017-12-15 12:06:03 -02003506#endif
Peter Zijlstra269d5992018-02-06 17:52:13 +01003507}
3508
3509static inline void finish_lock_switch(struct rq *rq)
3510{
rodrigosiqueira31cb1bc2017-12-15 12:06:03 -02003511 /*
3512 * If we are tracking spinlock dependencies then we have to
3513 * fix up the runqueue lock - which gets 'carried over' from
3514 * prev into current:
3515 */
3516 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
rodrigosiqueira31cb1bc2017-12-15 12:06:03 -02003517 raw_spin_unlock_irq(&rq->lock);
3518}
3519
Ingo Molnar325ea102018-03-03 12:20:47 +01003520/*
3521 * NOP if the arch has not defined these:
3522 */
3523
3524#ifndef prepare_arch_switch
3525# define prepare_arch_switch(next) do { } while (0)
3526#endif
3527
3528#ifndef finish_arch_post_lock_switch
3529# define finish_arch_post_lock_switch() do { } while (0)
3530#endif
3531
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532/**
Nick Piggin4866cde2005-06-25 14:57:23 -07003533 * prepare_task_switch - prepare to switch tasks
3534 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07003535 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07003536 * @next: the task we are going to switch to.
3537 *
3538 * This is called with the rq lock held and interrupts off. It must
3539 * be paired with a subsequent finish_task_switch after the context
3540 * switch.
3541 *
3542 * prepare_task_switch sets up locking and calls architecture specific
3543 * hooks.
3544 */
Avi Kivitye107be32007-07-26 13:40:43 +02003545static inline void
3546prepare_task_switch(struct rq *rq, struct task_struct *prev,
3547 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07003548{
Mark Rutland0ed557a2018-06-14 15:27:41 -07003549 kcov_prepare_switch(prev);
Michael S. Tsirkin43148952013-09-22 17:20:54 +03003550 sched_info_switch(rq, prev, next);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003551 perf_event_task_sched_out(prev, next);
Mathieu Desnoyersd7822b12018-06-02 08:43:54 -04003552 rseq_preempt(prev);
Avi Kivitye107be32007-07-26 13:40:43 +02003553 fire_sched_out_preempt_notifiers(prev, next);
rodrigosiqueira31cb1bc2017-12-15 12:06:03 -02003554 prepare_task(next);
Nick Piggin4866cde2005-06-25 14:57:23 -07003555 prepare_arch_switch(next);
3556}
3557
3558/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 * finish_task_switch - clean up after a task-switch
3560 * @prev: the thread we just switched away from.
3561 *
Nick Piggin4866cde2005-06-25 14:57:23 -07003562 * finish_task_switch must be called after the context switch, paired
3563 * with a prepare_task_switch call before the context switch.
3564 * finish_task_switch will reconcile locking set up by prepare_task_switch,
3565 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 *
3567 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003568 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 * with the lock held can cause deadlocks; see schedule() for
3570 * details.)
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02003571 *
3572 * The context switch have flipped the stack from under us and restored the
3573 * local variables which were saved when this task called schedule() in the
3574 * past. prev == current is still correct but we need to recalculate this_rq
3575 * because prev may have moved to another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 */
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02003577static struct rq *finish_task_switch(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 __releases(rq->lock)
3579{
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02003580 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07003582 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
Peter Zijlstra609ca062015-09-28 17:52:18 +02003584 /*
3585 * The previous task will have left us with a preempt_count of 2
3586 * because it left us after:
3587 *
3588 * schedule()
3589 * preempt_disable(); // 1
3590 * __schedule()
3591 * raw_spin_lock_irq(&rq->lock) // 2
3592 *
3593 * Also, see FORK_PREEMPT_COUNT.
3594 */
Peter Zijlstrae2bf1c4b2015-09-29 12:18:46 +02003595 if (WARN_ONCE(preempt_count() != 2*PREEMPT_DISABLE_OFFSET,
3596 "corrupted preempt_count: %s/%d/0x%x\n",
3597 current->comm, current->pid, preempt_count()))
3598 preempt_count_set(FORK_PREEMPT_COUNT);
Peter Zijlstra609ca062015-09-28 17:52:18 +02003599
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 rq->prev_mm = NULL;
3601
3602 /*
3603 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07003604 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07003605 * schedule one last time. The schedule call will never return, and
3606 * the scheduled task must drop that reference.
Peter Zijlstra95913d92015-09-29 14:45:09 +02003607 *
3608 * We must observe prev->state before clearing prev->on_cpu (in
rodrigosiqueira31cb1bc2017-12-15 12:06:03 -02003609 * finish_task), otherwise a concurrent wakeup can get prev
Peter Zijlstra95913d92015-09-29 14:45:09 +02003610 * running on another CPU and we could rave with its RUNNING -> DEAD
3611 * transition, resulting in a double drop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07003613 prev_state = prev->state;
Frederic Weisbeckerbf9fae92012-09-08 15:23:11 +02003614 vtime_task_switch(prev);
Stephane Eraniana8d757e2011-08-25 15:58:03 +02003615 perf_event_task_sched_in(prev, current);
rodrigosiqueira31cb1bc2017-12-15 12:06:03 -02003616 finish_task(prev);
3617 finish_lock_switch(rq);
Catalin Marinas01f23e12011-11-27 21:43:10 +00003618 finish_arch_post_lock_switch();
Mark Rutland0ed557a2018-06-14 15:27:41 -07003619 kcov_finish_switch(current);
Steven Rostedte8fa1362008-01-25 21:08:05 +01003620
Avi Kivitye107be32007-07-26 13:40:43 +02003621 fire_sched_in_preempt_notifiers(current);
Mathieu Desnoyers306e0602018-01-29 15:20:12 -05003622 /*
Mathieu Desnoyers70216e12018-01-29 15:20:17 -05003623 * When switching through a kernel thread, the loop in
3624 * membarrier_{private,global}_expedited() may have observed that
3625 * kernel thread and not issued an IPI. It is therefore possible to
3626 * schedule between user->kernel->user threads without passing though
3627 * switch_mm(). Membarrier requires a barrier after storing to
3628 * rq->curr, before returning to userspace, so provide them here:
3629 *
3630 * - a full memory barrier for {PRIVATE,GLOBAL}_EXPEDITED, implicitly
3631 * provided by mmdrop(),
3632 * - a sync_core for SYNC_CORE.
Mathieu Desnoyers306e0602018-01-29 15:20:12 -05003633 */
Mathieu Desnoyers70216e12018-01-29 15:20:17 -05003634 if (mm) {
3635 membarrier_mm_sync_core_before_usermode(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 mmdrop(mm);
Mathieu Desnoyers70216e12018-01-29 15:20:17 -05003637 }
Peter Zijlstra1cef1152018-06-07 11:45:49 +02003638 if (unlikely(prev_state == TASK_DEAD)) {
3639 if (prev->sched_class->task_dead)
3640 prev->sched_class->task_dead(prev);
Dario Faggiolie6c390f2013-11-07 14:43:35 +01003641
Peter Zijlstra1cef1152018-06-07 11:45:49 +02003642 /*
3643 * Remove function-return probe instances associated with this
3644 * task and put them back on the free list.
3645 */
3646 kprobe_flush_task(prev);
Andy Lutomirski68f24b082016-09-15 22:45:48 -07003647
Peter Zijlstra1cef1152018-06-07 11:45:49 +02003648 /* Task is done with its stack. */
3649 put_task_stack(prev);
Andy Lutomirski68f24b082016-09-15 22:45:48 -07003650
Eric W. Biederman0ff7b2c2019-09-14 07:33:58 -05003651 put_task_struct_rcu_user(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08003652 }
Frederic Weisbecker99e5ada2013-04-20 17:11:50 +02003653
Frederic Weisbeckerde734f82015-06-11 18:07:12 +02003654 tick_nohz_task_switch();
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02003655 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656}
3657
Gregory Haskins3f029d32009-07-29 11:08:47 -04003658#ifdef CONFIG_SMP
3659
Gregory Haskins3f029d32009-07-29 11:08:47 -04003660/* rq->lock is NOT held, but preemption is disabled */
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02003661static void __balance_callback(struct rq *rq)
Gregory Haskins3f029d32009-07-29 11:08:47 -04003662{
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02003663 struct callback_head *head, *next;
3664 void (*func)(struct rq *rq);
3665 unsigned long flags;
Gregory Haskins3f029d32009-07-29 11:08:47 -04003666
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02003667 raw_spin_lock_irqsave(&rq->lock, flags);
3668 head = rq->balance_callback;
3669 rq->balance_callback = NULL;
3670 while (head) {
3671 func = (void (*)(struct rq *))head->func;
3672 next = head->next;
3673 head->next = NULL;
3674 head = next;
Gregory Haskins3f029d32009-07-29 11:08:47 -04003675
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02003676 func(rq);
Gregory Haskins3f029d32009-07-29 11:08:47 -04003677 }
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02003678 raw_spin_unlock_irqrestore(&rq->lock, flags);
3679}
3680
3681static inline void balance_callback(struct rq *rq)
3682{
3683 if (unlikely(rq->balance_callback))
3684 __balance_callback(rq);
Gregory Haskins3f029d32009-07-29 11:08:47 -04003685}
3686
3687#else
3688
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02003689static inline void balance_callback(struct rq *rq)
Gregory Haskins3f029d32009-07-29 11:08:47 -04003690{
3691}
3692
3693#endif
3694
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695/**
3696 * schedule_tail - first thing a freshly forked thread must call.
3697 * @prev: the thread we just switched away from.
3698 */
Andi Kleen722a9f92014-05-02 00:44:38 +02003699asmlinkage __visible void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 __releases(rq->lock)
3701{
Oleg Nesterov1a43a142014-10-08 21:36:44 +02003702 struct rq *rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003703
Peter Zijlstra609ca062015-09-28 17:52:18 +02003704 /*
3705 * New tasks start with FORK_PREEMPT_COUNT, see there and
3706 * finish_task_switch() for details.
3707 *
3708 * finish_task_switch() will drop rq->lock() and lower preempt_count
3709 * and the preempt_enable() will end up enabling preemption (on
3710 * PREEMPT_COUNT kernels).
3711 */
3712
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02003713 rq = finish_task_switch(prev);
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02003714 balance_callback(rq);
Oleg Nesterov1a43a142014-10-08 21:36:44 +02003715 preempt_enable();
Steven Rostedtda19ab52009-07-29 00:21:22 -04003716
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003718 put_user(task_pid_vnr(current), current->set_child_tid);
Eric W. Biederman088fe472018-07-23 17:26:49 -05003719
3720 calculate_sigpending();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721}
3722
3723/*
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02003724 * context_switch - switch to the new MM and the new thread's register state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 */
Josh Poimboeuf04936942016-02-28 22:22:39 -06003726static __always_inline struct rq *
Ingo Molnar70b97a72006-07-03 00:25:42 -07003727context_switch(struct rq *rq, struct task_struct *prev,
Matt Flemingd8ac8972016-09-21 14:38:10 +01003728 struct task_struct *next, struct rq_flags *rf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729{
Avi Kivitye107be32007-07-26 13:40:43 +02003730 prepare_task_switch(rq, prev, next);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003731
Zachary Amsden9226d122007-02-13 13:26:21 +01003732 /*
3733 * For paravirt, this is coupled with an exit in switch_to to
3734 * combine the page table reload and the switch backend into
3735 * one hypercall.
3736 */
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -08003737 arch_start_context_switch(prev);
Zachary Amsden9226d122007-02-13 13:26:21 +01003738
Mathieu Desnoyers306e0602018-01-29 15:20:12 -05003739 /*
Peter Zijlstra139d0252019-07-29 16:05:15 +02003740 * kernel -> kernel lazy + transfer active
3741 * user -> kernel lazy + mmgrab() active
3742 *
3743 * kernel -> user switch + mmdrop() active
3744 * user -> user switch
Mathieu Desnoyers306e0602018-01-29 15:20:12 -05003745 */
Peter Zijlstra139d0252019-07-29 16:05:15 +02003746 if (!next->mm) { // to kernel
3747 enter_lazy_tlb(prev->active_mm, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
Peter Zijlstra139d0252019-07-29 16:05:15 +02003749 next->active_mm = prev->active_mm;
3750 if (prev->mm) // from user
3751 mmgrab(prev->active_mm);
3752 else
3753 prev->active_mm = NULL;
3754 } else { // to user
Mathieu Desnoyers227a4aa2019-09-19 13:37:02 -04003755 membarrier_switch_mm(rq, prev->active_mm, next->mm);
Peter Zijlstra139d0252019-07-29 16:05:15 +02003756 /*
3757 * sys_membarrier() requires an smp_mb() between setting
Mathieu Desnoyers227a4aa2019-09-19 13:37:02 -04003758 * rq->curr / membarrier_switch_mm() and returning to userspace.
Peter Zijlstra139d0252019-07-29 16:05:15 +02003759 *
3760 * The below provides this either through switch_mm(), or in
3761 * case 'prev->active_mm == next->mm' through
3762 * finish_task_switch()'s mmdrop().
3763 */
Peter Zijlstra139d0252019-07-29 16:05:15 +02003764 switch_mm_irqs_off(prev->active_mm, next->mm, next);
3765
3766 if (!prev->mm) { // from kernel
3767 /* will mmdrop() in finish_task_switch(). */
3768 rq->prev_mm = prev->active_mm;
3769 prev->active_mm = NULL;
3770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 }
Matt Fleming92509b72016-09-21 14:38:11 +01003772
Matt Flemingcb42c9a2016-09-21 14:38:13 +01003773 rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP);
Matt Fleming92509b72016-09-21 14:38:11 +01003774
Peter Zijlstra269d5992018-02-06 17:52:13 +01003775 prepare_lock_switch(rq, next, rf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
3777 /* Here we just switch the register state and the stack. */
3778 switch_to(prev, next, prev);
Ingo Molnardd41f592007-07-09 18:51:59 +02003779 barrier();
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02003780
3781 return finish_task_switch(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782}
3783
3784/*
Sha Zhengju1c3e8262013-02-20 17:14:38 +08003785 * nr_running and nr_context_switches:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 *
3787 * externally visible scheduler statistics: current number of runnable
Sha Zhengju1c3e8262013-02-20 17:14:38 +08003788 * threads, total number of context switches performed since bootup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 */
3790unsigned long nr_running(void)
3791{
3792 unsigned long i, sum = 0;
3793
3794 for_each_online_cpu(i)
3795 sum += cpu_rq(i)->nr_running;
3796
3797 return sum;
3798}
3799
Tim Chen2ee507c2014-07-31 10:29:48 -07003800/*
Ingo Molnard1ccc662017-02-01 11:46:42 +01003801 * Check if only the current task is running on the CPU.
Dominik Dingel00cc16332015-09-18 11:27:45 +02003802 *
3803 * Caution: this function does not check that the caller has disabled
3804 * preemption, thus the result might have a time-of-check-to-time-of-use
3805 * race. The caller is responsible to use it correctly, for example:
3806 *
Ingo Molnardfcb2452018-12-03 10:05:56 +01003807 * - from a non-preemptible section (of course)
Dominik Dingel00cc16332015-09-18 11:27:45 +02003808 *
3809 * - from a thread that is bound to a single CPU
3810 *
3811 * - in a loop with very short iterations (e.g. a polling loop)
Tim Chen2ee507c2014-07-31 10:29:48 -07003812 */
3813bool single_task_running(void)
3814{
Dominik Dingel00cc16332015-09-18 11:27:45 +02003815 return raw_rq()->nr_running == 1;
Tim Chen2ee507c2014-07-31 10:29:48 -07003816}
3817EXPORT_SYMBOL(single_task_running);
3818
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819unsigned long long nr_context_switches(void)
3820{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07003821 int i;
3822 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08003824 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 sum += cpu_rq(i)->nr_switches;
3826
3827 return sum;
3828}
3829
Tejun Heoe33a9bb2016-12-07 15:48:41 -05003830/*
Daniel Lezcano145d9522018-10-04 14:04:02 +02003831 * Consumers of these two interfaces, like for example the cpuidle menu
3832 * governor, are using nonsensical data. Preferring shallow idle state selection
3833 * for a CPU that has IO-wait which might not even end up running the task when
3834 * it does become runnable.
3835 */
3836
3837unsigned long nr_iowait_cpu(int cpu)
3838{
3839 return atomic_read(&cpu_rq(cpu)->nr_iowait);
3840}
3841
Daniel Lezcano145d9522018-10-04 14:04:02 +02003842/*
Tejun Heoe33a9bb2016-12-07 15:48:41 -05003843 * IO-wait accounting, and how its mostly bollocks (on SMP).
3844 *
3845 * The idea behind IO-wait account is to account the idle time that we could
3846 * have spend running if it were not for IO. That is, if we were to improve the
3847 * storage performance, we'd have a proportional reduction in IO-wait time.
3848 *
3849 * This all works nicely on UP, where, when a task blocks on IO, we account
3850 * idle time as IO-wait, because if the storage were faster, it could've been
3851 * running and we'd not be idle.
3852 *
3853 * This has been extended to SMP, by doing the same for each CPU. This however
3854 * is broken.
3855 *
3856 * Imagine for instance the case where two tasks block on one CPU, only the one
3857 * CPU will have IO-wait accounted, while the other has regular idle. Even
3858 * though, if the storage were faster, both could've ran at the same time,
3859 * utilising both CPUs.
3860 *
3861 * This means, that when looking globally, the current IO-wait accounting on
3862 * SMP is a lower bound, by reason of under accounting.
3863 *
3864 * Worse, since the numbers are provided per CPU, they are sometimes
3865 * interpreted per CPU, and that is nonsensical. A blocked task isn't strictly
3866 * associated with any one particular CPU, it can wake to another CPU than it
3867 * blocked on. This means the per CPU IO-wait number is meaningless.
3868 *
3869 * Task CPU affinities can make all that even more 'interesting'.
3870 */
3871
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872unsigned long nr_iowait(void)
3873{
3874 unsigned long i, sum = 0;
3875
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08003876 for_each_possible_cpu(i)
Daniel Lezcano145d9522018-10-04 14:04:02 +02003877 sum += nr_iowait_cpu(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878
3879 return sum;
3880}
3881
Ingo Molnardd41f592007-07-09 18:51:59 +02003882#ifdef CONFIG_SMP
3883
Ingo Molnar48f24c42006-07-03 00:25:40 -07003884/*
Peter Zijlstra38022902009-12-16 18:04:37 +01003885 * sched_exec - execve() is a valuable balancing opportunity, because at
3886 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 */
Peter Zijlstra38022902009-12-16 18:04:37 +01003888void sched_exec(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889{
Peter Zijlstra38022902009-12-16 18:04:37 +01003890 struct task_struct *p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 unsigned long flags;
Peter Zijlstra0017d732010-03-24 18:34:10 +01003892 int dest_cpu;
Peter Zijlstra38022902009-12-16 18:04:37 +01003893
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02003894 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstraac66f542013-10-07 11:29:16 +01003895 dest_cpu = p->sched_class->select_task_rq(p, task_cpu(p), SD_BALANCE_EXEC, 0);
Peter Zijlstra0017d732010-03-24 18:34:10 +01003896 if (dest_cpu == smp_processor_id())
3897 goto unlock;
Peter Zijlstra38022902009-12-16 18:04:37 +01003898
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02003899 if (likely(cpu_active(dest_cpu))) {
Tejun Heo969c7922010-05-06 18:49:21 +02003900 struct migration_arg arg = { p, dest_cpu };
Ingo Molnar36c8b582006-07-03 00:25:41 -07003901
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02003902 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
3903 stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 return;
3905 }
Peter Zijlstra0017d732010-03-24 18:34:10 +01003906unlock:
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02003907 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908}
3909
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910#endif
3911
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912DEFINE_PER_CPU(struct kernel_stat, kstat);
Glauber Costa3292beb2011-11-28 14:45:17 -02003913DEFINE_PER_CPU(struct kernel_cpustat, kernel_cpustat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914
3915EXPORT_PER_CPU_SYMBOL(kstat);
Glauber Costa3292beb2011-11-28 14:45:17 -02003916EXPORT_PER_CPU_SYMBOL(kernel_cpustat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917
3918/*
Giovanni Gherdovich60756202016-08-05 10:21:56 +02003919 * The function fair_sched_class.update_curr accesses the struct curr
3920 * and its field curr->exec_start; when called from task_sched_runtime(),
3921 * we observe a high rate of cache misses in practice.
3922 * Prefetching this data results in improved performance.
3923 */
3924static inline void prefetch_curr_exec_start(struct task_struct *p)
3925{
3926#ifdef CONFIG_FAIR_GROUP_SCHED
3927 struct sched_entity *curr = (&p->se)->cfs_rq->curr;
3928#else
3929 struct sched_entity *curr = (&task_rq(p)->cfs)->curr;
3930#endif
3931 prefetch(curr);
3932 prefetch(&curr->exec_start);
3933}
3934
3935/*
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003936 * Return accounted runtime for the task.
3937 * In case the task is currently running, return the runtime plus current's
3938 * pending runtime that have not been accounted yet.
3939 */
3940unsigned long long task_sched_runtime(struct task_struct *p)
3941{
Peter Zijlstraeb580752015-07-31 21:28:18 +02003942 struct rq_flags rf;
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003943 struct rq *rq;
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01003944 u64 ns;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003945
Peter Zijlstra911b2892013-11-11 18:21:56 +01003946#if defined(CONFIG_64BIT) && defined(CONFIG_SMP)
3947 /*
Ingo Molnar97fb7a02018-03-03 14:01:12 +01003948 * 64-bit doesn't need locks to atomically read a 64-bit value.
Peter Zijlstra911b2892013-11-11 18:21:56 +01003949 * So we have a optimization chance when the task's delta_exec is 0.
3950 * Reading ->on_cpu is racy, but this is ok.
3951 *
Ingo Molnard1ccc662017-02-01 11:46:42 +01003952 * If we race with it leaving CPU, we'll take a lock. So we're correct.
3953 * If we race with it entering CPU, unaccounted time is 0. This is
Peter Zijlstra911b2892013-11-11 18:21:56 +01003954 * indistinguishable from the read occurring a few cycles earlier.
Mike Galbraith4036ac12014-06-24 07:49:40 +02003955 * If we see ->on_cpu without ->on_rq, the task is leaving, and has
3956 * been accounted, so we're correct here as well.
Peter Zijlstra911b2892013-11-11 18:21:56 +01003957 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003958 if (!p->on_cpu || !task_on_rq_queued(p))
Peter Zijlstra911b2892013-11-11 18:21:56 +01003959 return p->se.sum_exec_runtime;
3960#endif
3961
Peter Zijlstraeb580752015-07-31 21:28:18 +02003962 rq = task_rq_lock(p, &rf);
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01003963 /*
3964 * Must be ->curr _and_ ->on_rq. If dequeued, we would
3965 * project cycles that may never be accounted to this
3966 * thread, breaking clock_gettime().
3967 */
3968 if (task_current(rq, p) && task_on_rq_queued(p)) {
Giovanni Gherdovich60756202016-08-05 10:21:56 +02003969 prefetch_curr_exec_start(p);
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01003970 update_rq_clock(rq);
3971 p->sched_class->update_curr(rq);
3972 }
3973 ns = p->se.sum_exec_runtime;
Peter Zijlstraeb580752015-07-31 21:28:18 +02003974 task_rq_unlock(rq, p, &rf);
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003975
3976 return ns;
3977}
3978
Balbir Singh49048622008-09-05 18:12:23 +02003979/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003980 * This function gets called by the timer code, with HZ frequency.
3981 * We call it with interrupts disabled.
Christoph Lameter7835b982006-12-10 02:20:22 -08003982 */
3983void scheduler_tick(void)
3984{
Christoph Lameter7835b982006-12-10 02:20:22 -08003985 int cpu = smp_processor_id();
3986 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003987 struct task_struct *curr = rq->curr;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02003988 struct rq_flags rf;
Thara Gopinathb4eccf52020-02-21 19:52:10 -05003989 unsigned long thermal_pressure;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02003990
Giovanni Gherdovich1567c3e32020-01-22 16:16:12 +01003991 arch_scale_freq_tick();
Peter Zijlstra3e51f332008-05-03 18:29:28 +02003992 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08003993
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02003994 rq_lock(rq, &rf);
3995
Peter Zijlstra3e51f332008-05-03 18:29:28 +02003996 update_rq_clock(rq);
Thara Gopinathb4eccf52020-02-21 19:52:10 -05003997 thermal_pressure = arch_scale_thermal_pressure(cpu_of(rq));
Thara Gopinath05289b92020-02-21 19:52:13 -05003998 update_thermal_load_avg(rq_clock_thermal(rq), rq, thermal_pressure);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01003999 curr->sched_class->task_tick(rq, curr, 0);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02004000 calc_global_load_tick(rq);
Johannes Weinereb414682018-10-26 15:06:27 -07004001 psi_task_tick(rq);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02004002
4003 rq_unlock(rq, &rf);
Ingo Molnardd41f592007-07-09 18:51:59 +02004004
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02004005 perf_event_task_tick();
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02004006
Christoph Lametere418e1c2006-12-10 02:20:23 -08004007#ifdef CONFIG_SMP
Suresh Siddha6eb57e02011-10-03 15:09:01 -07004008 rq->idle_balance = idle_cpu(cpu);
Daniel Lezcano7caff662014-01-06 12:34:38 +01004009 trigger_load_balance(rq);
Christoph Lametere418e1c2006-12-10 02:20:23 -08004010#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011}
4012
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02004013#ifdef CONFIG_NO_HZ_FULL
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01004014
4015struct tick_work {
4016 int cpu;
Paul E. McKenneyb55bd582019-05-30 05:39:25 -07004017 atomic_t state;
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01004018 struct delayed_work work;
4019};
Paul E. McKenneyb55bd582019-05-30 05:39:25 -07004020/* Values for ->state, see diagram below. */
4021#define TICK_SCHED_REMOTE_OFFLINE 0
4022#define TICK_SCHED_REMOTE_OFFLINING 1
4023#define TICK_SCHED_REMOTE_RUNNING 2
4024
4025/*
4026 * State diagram for ->state:
4027 *
4028 *
4029 * TICK_SCHED_REMOTE_OFFLINE
4030 * | ^
4031 * | |
4032 * | | sched_tick_remote()
4033 * | |
4034 * | |
4035 * +--TICK_SCHED_REMOTE_OFFLINING
4036 * | ^
4037 * | |
4038 * sched_tick_start() | | sched_tick_stop()
4039 * | |
4040 * V |
4041 * TICK_SCHED_REMOTE_RUNNING
4042 *
4043 *
4044 * Other transitions get WARN_ON_ONCE(), except that sched_tick_remote()
4045 * and sched_tick_start() are happy to leave the state in RUNNING.
4046 */
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01004047
4048static struct tick_work __percpu *tick_work_cpu;
4049
4050static void sched_tick_remote(struct work_struct *work)
4051{
4052 struct delayed_work *dwork = to_delayed_work(work);
4053 struct tick_work *twork = container_of(dwork, struct tick_work, work);
4054 int cpu = twork->cpu;
4055 struct rq *rq = cpu_rq(cpu);
Frederic Weisbeckerd9c0ffc2018-06-28 18:29:41 +02004056 struct task_struct *curr;
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01004057 struct rq_flags rf;
Frederic Weisbeckerd9c0ffc2018-06-28 18:29:41 +02004058 u64 delta;
Paul E. McKenneyb55bd582019-05-30 05:39:25 -07004059 int os;
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01004060
4061 /*
4062 * Handle the tick only if it appears the remote CPU is running in full
4063 * dynticks mode. The check is racy by nature, but missing a tick or
4064 * having one too much is no big deal because the scheduler tick updates
4065 * statistics and checks timeslices in a time-independent way, regardless
4066 * of when exactly it is running.
4067 */
Scott Wood488603b2020-01-11 04:53:38 -05004068 if (!tick_nohz_tick_stopped_cpu(cpu))
Frederic Weisbeckerd9c0ffc2018-06-28 18:29:41 +02004069 goto out_requeue;
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01004070
Frederic Weisbeckerd9c0ffc2018-06-28 18:29:41 +02004071 rq_lock_irq(rq, &rf);
4072 curr = rq->curr;
Scott Wood488603b2020-01-11 04:53:38 -05004073 if (cpu_is_offline(cpu))
Frederic Weisbeckerd9c0ffc2018-06-28 18:29:41 +02004074 goto out_unlock;
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01004075
Frederic Weisbeckerd9c0ffc2018-06-28 18:29:41 +02004076 update_rq_clock(rq);
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01004077
Scott Wood488603b2020-01-11 04:53:38 -05004078 if (!is_idle_task(curr)) {
4079 /*
4080 * Make sure the next tick runs within a reasonable
4081 * amount of time.
4082 */
4083 delta = rq_clock_task(rq) - curr->se.exec_start;
4084 WARN_ON_ONCE(delta > (u64)NSEC_PER_SEC * 3);
4085 }
Frederic Weisbeckerd9c0ffc2018-06-28 18:29:41 +02004086 curr->sched_class->task_tick(rq, curr, 0);
4087
Peter Zijlstra (Intel)ebc0f832020-01-11 04:53:39 -05004088 calc_load_nohz_remote(rq);
Frederic Weisbeckerd9c0ffc2018-06-28 18:29:41 +02004089out_unlock:
4090 rq_unlock_irq(rq, &rf);
Frederic Weisbeckerd9c0ffc2018-06-28 18:29:41 +02004091out_requeue:
Peter Zijlstra (Intel)ebc0f832020-01-11 04:53:39 -05004092
Frederic Weisbeckerd9c0ffc2018-06-28 18:29:41 +02004093 /*
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01004094 * Run the remote tick once per second (1Hz). This arbitrary
4095 * frequency is large enough to avoid overload but short enough
Paul E. McKenneyb55bd582019-05-30 05:39:25 -07004096 * to keep scheduler internal stats reasonably up to date. But
4097 * first update state to reflect hotplug activity if required.
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01004098 */
Paul E. McKenneyb55bd582019-05-30 05:39:25 -07004099 os = atomic_fetch_add_unless(&twork->state, -1, TICK_SCHED_REMOTE_RUNNING);
4100 WARN_ON_ONCE(os == TICK_SCHED_REMOTE_OFFLINE);
4101 if (os == TICK_SCHED_REMOTE_RUNNING)
4102 queue_delayed_work(system_unbound_wq, dwork, HZ);
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01004103}
4104
4105static void sched_tick_start(int cpu)
4106{
Paul E. McKenneyb55bd582019-05-30 05:39:25 -07004107 int os;
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01004108 struct tick_work *twork;
4109
4110 if (housekeeping_cpu(cpu, HK_FLAG_TICK))
4111 return;
4112
4113 WARN_ON_ONCE(!tick_work_cpu);
4114
4115 twork = per_cpu_ptr(tick_work_cpu, cpu);
Paul E. McKenneyb55bd582019-05-30 05:39:25 -07004116 os = atomic_xchg(&twork->state, TICK_SCHED_REMOTE_RUNNING);
4117 WARN_ON_ONCE(os == TICK_SCHED_REMOTE_RUNNING);
4118 if (os == TICK_SCHED_REMOTE_OFFLINE) {
4119 twork->cpu = cpu;
4120 INIT_DELAYED_WORK(&twork->work, sched_tick_remote);
4121 queue_delayed_work(system_unbound_wq, &twork->work, HZ);
4122 }
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01004123}
4124
4125#ifdef CONFIG_HOTPLUG_CPU
4126static void sched_tick_stop(int cpu)
4127{
4128 struct tick_work *twork;
Paul E. McKenneyb55bd582019-05-30 05:39:25 -07004129 int os;
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01004130
4131 if (housekeeping_cpu(cpu, HK_FLAG_TICK))
4132 return;
4133
4134 WARN_ON_ONCE(!tick_work_cpu);
4135
4136 twork = per_cpu_ptr(tick_work_cpu, cpu);
Paul E. McKenneyb55bd582019-05-30 05:39:25 -07004137 /* There cannot be competing actions, but don't rely on stop-machine. */
4138 os = atomic_xchg(&twork->state, TICK_SCHED_REMOTE_OFFLINING);
4139 WARN_ON_ONCE(os != TICK_SCHED_REMOTE_RUNNING);
4140 /* Don't cancel, as this would mess up the state machine. */
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01004141}
4142#endif /* CONFIG_HOTPLUG_CPU */
4143
4144int __init sched_tick_offload_init(void)
4145{
4146 tick_work_cpu = alloc_percpu(struct tick_work);
4147 BUG_ON(!tick_work_cpu);
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01004148 return 0;
4149}
4150
4151#else /* !CONFIG_NO_HZ_FULL */
4152static inline void sched_tick_start(int cpu) { }
4153static inline void sched_tick_stop(int cpu) { }
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02004154#endif
4155
Thomas Gleixnerc1a280b2019-07-26 23:19:37 +02004156#if defined(CONFIG_PREEMPTION) && (defined(CONFIG_DEBUG_PREEMPT) || \
Joel Fernandes (Google)c3bc8fd2018-07-30 15:24:23 -07004157 defined(CONFIG_TRACE_PREEMPT_TOGGLE))
Steven Rostedt47252cf2016-03-21 11:23:39 -04004158/*
4159 * If the value passed in is equal to the current preempt count
4160 * then we just disabled preemption. Start timing the latency.
4161 */
4162static inline void preempt_latency_start(int val)
4163{
4164 if (preempt_count() == val) {
4165 unsigned long ip = get_lock_parent_ip();
4166#ifdef CONFIG_DEBUG_PREEMPT
4167 current->preempt_disable_ip = ip;
4168#endif
4169 trace_preempt_off(CALLER_ADDR0, ip);
4170 }
4171}
Steven Rostedt7e49fcc2009-01-22 19:01:40 -05004172
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09004173void preempt_count_add(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004175#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 /*
4177 * Underflow?
4178 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004179 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
4180 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004181#endif
Peter Zijlstrabdb43802013-09-10 12:15:23 +02004182 __preempt_count_add(val);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004183#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 /*
4185 * Spinlock count overflowing soon?
4186 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08004187 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
4188 PREEMPT_MASK - 10);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004189#endif
Steven Rostedt47252cf2016-03-21 11:23:39 -04004190 preempt_latency_start(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191}
Peter Zijlstrabdb43802013-09-10 12:15:23 +02004192EXPORT_SYMBOL(preempt_count_add);
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09004193NOKPROBE_SYMBOL(preempt_count_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194
Steven Rostedt47252cf2016-03-21 11:23:39 -04004195/*
4196 * If the value passed in equals to the current preempt count
4197 * then we just enabled preemption. Stop timing the latency.
4198 */
4199static inline void preempt_latency_stop(int val)
4200{
4201 if (preempt_count() == val)
4202 trace_preempt_on(CALLER_ADDR0, get_lock_parent_ip());
4203}
4204
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09004205void preempt_count_sub(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004207#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 /*
4209 * Underflow?
4210 */
Ingo Molnar01e3eb82009-01-12 13:00:50 +01004211 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004212 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 /*
4214 * Is the spinlock portion underflowing?
4215 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004216 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
4217 !(preempt_count() & PREEMPT_MASK)))
4218 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02004219#endif
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07004220
Steven Rostedt47252cf2016-03-21 11:23:39 -04004221 preempt_latency_stop(val);
Peter Zijlstrabdb43802013-09-10 12:15:23 +02004222 __preempt_count_sub(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223}
Peter Zijlstrabdb43802013-09-10 12:15:23 +02004224EXPORT_SYMBOL(preempt_count_sub);
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09004225NOKPROBE_SYMBOL(preempt_count_sub);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226
Steven Rostedt47252cf2016-03-21 11:23:39 -04004227#else
4228static inline void preempt_latency_start(int val) { }
4229static inline void preempt_latency_stop(int val) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230#endif
4231
Ingo Molnar59ddbcb2017-02-03 23:37:48 +01004232static inline unsigned long get_preempt_disable_ip(struct task_struct *p)
4233{
4234#ifdef CONFIG_DEBUG_PREEMPT
4235 return p->preempt_disable_ip;
4236#else
4237 return 0;
4238#endif
4239}
4240
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241/*
Ingo Molnardd41f592007-07-09 18:51:59 +02004242 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004244static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245{
Vegard Nossumd1c6d142016-07-23 09:46:39 +02004246 /* Save this before calling printk(), since that will clobber it */
4247 unsigned long preempt_disable_ip = get_preempt_disable_ip(current);
4248
Dave Jones664dfa62011-12-22 16:39:30 -05004249 if (oops_in_progress)
4250 return;
4251
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004252 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4253 prev->comm, prev->pid, preempt_count());
Satyam Sharma838225b2007-10-24 18:23:50 +02004254
Ingo Molnardd41f592007-07-09 18:51:59 +02004255 debug_show_held_locks(prev);
Arjan van de Vene21f5b12008-05-23 09:05:58 -07004256 print_modules();
Ingo Molnardd41f592007-07-09 18:51:59 +02004257 if (irqs_disabled())
4258 print_irqtrace_events(prev);
Vegard Nossumd1c6d142016-07-23 09:46:39 +02004259 if (IS_ENABLED(CONFIG_DEBUG_PREEMPT)
4260 && in_atomic_preempt_off()) {
Thomas Gleixner8f47b182014-02-07 20:58:39 +01004261 pr_err("Preemption disabled at:");
Dmitry Safonov2062a4e2020-06-08 21:29:56 -07004262 print_ip_sym(KERN_ERR, preempt_disable_ip);
Thomas Gleixner8f47b182014-02-07 20:58:39 +01004263 }
Daniel Bristot de Oliveira748c7202016-06-03 17:10:18 -03004264 if (panic_on_warn)
4265 panic("scheduling while atomic\n");
4266
Stephen Boyd6135fc12012-03-28 17:10:47 -07004267 dump_stack();
Rusty Russell373d4d02013-01-21 17:17:39 +10304268 add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
Ingo Molnardd41f592007-07-09 18:51:59 +02004269}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270
Ingo Molnardd41f592007-07-09 18:51:59 +02004271/*
4272 * Various schedule()-time debugging checks and statistics:
4273 */
Daniel Vetter312364f32019-08-26 22:14:23 +02004274static inline void schedule_debug(struct task_struct *prev, bool preempt)
Ingo Molnardd41f592007-07-09 18:51:59 +02004275{
Aaron Tomlin0d9e2632014-09-12 14:16:19 +01004276#ifdef CONFIG_SCHED_STACK_END_CHECK
Jann Horn29d64552016-06-01 11:55:07 +02004277 if (task_stack_end_corrupted(prev))
4278 panic("corrupted stack end detected inside scheduler\n");
Will Deacon88485be2020-05-15 14:56:05 +01004279
4280 if (task_scs_end_corrupted(prev))
4281 panic("corrupted shadow stack detected inside scheduler\n");
Aaron Tomlin0d9e2632014-09-12 14:16:19 +01004282#endif
Peter Zijlstrab99def82015-09-28 18:02:03 +02004283
Daniel Vetter312364f32019-08-26 22:14:23 +02004284#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
4285 if (!preempt && prev->state && prev->non_block_count) {
4286 printk(KERN_ERR "BUG: scheduling in a non-blocking section: %s/%d/%i\n",
4287 prev->comm, prev->pid, prev->non_block_count);
4288 dump_stack();
4289 add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
4290 }
4291#endif
4292
Peter Zijlstra1dc0fff2015-09-28 17:57:39 +02004293 if (unlikely(in_atomic_preempt_off())) {
Ingo Molnardd41f592007-07-09 18:51:59 +02004294 __schedule_bug(prev);
Peter Zijlstra1dc0fff2015-09-28 17:57:39 +02004295 preempt_count_set(PREEMPT_DISABLED);
4296 }
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004297 rcu_sleep_check();
Ingo Molnardd41f592007-07-09 18:51:59 +02004298
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4300
Josh Poimboeufae928822016-06-17 12:43:24 -05004301 schedstat_inc(this_rq()->sched_count);
Ingo Molnardd41f592007-07-09 18:51:59 +02004302}
4303
Chen Yu457d1f42020-04-21 18:50:43 +08004304static void put_prev_task_balance(struct rq *rq, struct task_struct *prev,
4305 struct rq_flags *rf)
4306{
4307#ifdef CONFIG_SMP
4308 const struct sched_class *class;
4309 /*
4310 * We must do the balancing pass before put_prev_task(), such
4311 * that when we release the rq->lock the task is in the same
4312 * state as before we took rq->lock.
4313 *
4314 * We can terminate the balance pass as soon as we know there is
4315 * a runnable task of @class priority or higher.
4316 */
4317 for_class_range(class, prev->sched_class, &idle_sched_class) {
4318 if (class->balance(rq, prev, rf))
4319 break;
4320 }
4321#endif
4322
4323 put_prev_task(rq, prev);
4324}
4325
Ingo Molnardd41f592007-07-09 18:51:59 +02004326/*
4327 * Pick up the highest-prio task:
4328 */
4329static inline struct task_struct *
Matt Flemingd8ac8972016-09-21 14:38:10 +01004330pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
Ingo Molnardd41f592007-07-09 18:51:59 +02004331{
Peter Zijlstra49ee5762017-01-19 18:44:08 +01004332 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004333 struct task_struct *p;
4334
4335 /*
Peter Zijlstra0ba87bb2017-03-01 10:51:47 +01004336 * Optimization: we know that if all tasks are in the fair class we can
4337 * call that function directly, but only if the @prev task wasn't of a
4338 * higher scheduling class, because otherwise those loose the
4339 * opportunity to pull in more work from other CPUs.
Ingo Molnardd41f592007-07-09 18:51:59 +02004340 */
Kirill Tkhaiaa93cd52019-12-19 16:44:55 -05004341 if (likely(prev->sched_class <= &fair_sched_class &&
Peter Zijlstra0ba87bb2017-03-01 10:51:47 +01004342 rq->nr_running == rq->cfs.h_nr_running)) {
4343
Peter Zijlstra5d7d6052019-11-08 14:15:57 +01004344 p = pick_next_task_fair(rq, prev, rf);
Peter Zijlstra6ccdc842014-04-24 12:00:47 +02004345 if (unlikely(p == RETRY_TASK))
Peter Zijlstra67692432019-05-29 20:36:44 +00004346 goto restart;
Peter Zijlstra6ccdc842014-04-24 12:00:47 +02004347
Ingo Molnard1ccc662017-02-01 11:46:42 +01004348 /* Assumes fair_sched_class->next == idle_sched_class */
Peter Zijlstra5d7d6052019-11-08 14:15:57 +01004349 if (!p) {
Peter Zijlstraf488e102019-11-08 14:15:56 +01004350 put_prev_task(rq, prev);
Peter Zijlstra98c2f702019-11-08 14:15:58 +01004351 p = pick_next_task_idle(rq);
Peter Zijlstraf488e102019-11-08 14:15:56 +01004352 }
Peter Zijlstra6ccdc842014-04-24 12:00:47 +02004353
4354 return p;
Ingo Molnardd41f592007-07-09 18:51:59 +02004355 }
4356
Peter Zijlstra67692432019-05-29 20:36:44 +00004357restart:
Chen Yu457d1f42020-04-21 18:50:43 +08004358 put_prev_task_balance(rq, prev, rf);
Peter Zijlstra67692432019-05-29 20:36:44 +00004359
Peter Zijlstra34f971f2010-09-22 13:53:15 +02004360 for_each_class(class) {
Peter Zijlstra98c2f702019-11-08 14:15:58 +01004361 p = class->pick_next_task(rq);
Peter Zijlstra67692432019-05-29 20:36:44 +00004362 if (p)
Ingo Molnardd41f592007-07-09 18:51:59 +02004363 return p;
Ingo Molnardd41f592007-07-09 18:51:59 +02004364 }
Peter Zijlstra34f971f2010-09-22 13:53:15 +02004365
Ingo Molnard1ccc662017-02-01 11:46:42 +01004366 /* The idle class should always have a runnable task: */
4367 BUG();
Ingo Molnardd41f592007-07-09 18:51:59 +02004368}
4369
4370/*
Thomas Gleixnerc259e012011-06-22 19:47:00 +02004371 * __schedule() is the main scheduler function.
Pekka Enbergedde96e2012-08-04 11:49:47 +03004372 *
4373 * The main means of driving the scheduler and thus entering this function are:
4374 *
4375 * 1. Explicit blocking: mutex, semaphore, waitqueue, etc.
4376 *
4377 * 2. TIF_NEED_RESCHED flag is checked on interrupt and userspace return
4378 * paths. For example, see arch/x86/entry_64.S.
4379 *
4380 * To drive preemption between tasks, the scheduler sets the flag in timer
4381 * interrupt handler scheduler_tick().
4382 *
4383 * 3. Wakeups don't really cause entry into schedule(). They add a
4384 * task to the run-queue and that's it.
4385 *
4386 * Now, if the new task added to the run-queue preempts the current
4387 * task, then the wakeup sets TIF_NEED_RESCHED and schedule() gets
4388 * called on the nearest possible occasion:
4389 *
Thomas Gleixnerc1a280b2019-07-26 23:19:37 +02004390 * - If the kernel is preemptible (CONFIG_PREEMPTION=y):
Pekka Enbergedde96e2012-08-04 11:49:47 +03004391 *
4392 * - in syscall or exception context, at the next outmost
4393 * preempt_enable(). (this might be as soon as the wake_up()'s
4394 * spin_unlock()!)
4395 *
4396 * - in IRQ context, return from interrupt-handler to
4397 * preemptible context
4398 *
Thomas Gleixnerc1a280b2019-07-26 23:19:37 +02004399 * - If the kernel is not preemptible (CONFIG_PREEMPTION is not set)
Pekka Enbergedde96e2012-08-04 11:49:47 +03004400 * then at the next:
4401 *
4402 * - cond_resched() call
4403 * - explicit schedule() call
4404 * - return from syscall or exception to user-space
4405 * - return from interrupt-handler to user-space
Frederic Weisbeckerbfd9b2b2015-01-28 01:24:09 +01004406 *
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02004407 * WARNING: must be called with preemption disabled!
Ingo Molnardd41f592007-07-09 18:51:59 +02004408 */
Peter Zijlstra499d7952015-09-28 18:52:36 +02004409static void __sched notrace __schedule(bool preempt)
Ingo Molnardd41f592007-07-09 18:51:59 +02004410{
4411 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004412 unsigned long *switch_count;
Peter Zijlstradbfb0892020-07-03 12:40:33 +02004413 unsigned long prev_state;
Matt Flemingd8ac8972016-09-21 14:38:10 +01004414 struct rq_flags rf;
Ingo Molnardd41f592007-07-09 18:51:59 +02004415 struct rq *rq;
Peter Zijlstra31656512008-07-18 18:01:23 +02004416 int cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02004417
Ingo Molnardd41f592007-07-09 18:51:59 +02004418 cpu = smp_processor_id();
4419 rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004420 prev = rq->curr;
Ingo Molnardd41f592007-07-09 18:51:59 +02004421
Daniel Vetter312364f32019-08-26 22:14:23 +02004422 schedule_debug(prev, preempt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
Peter Zijlstra31656512008-07-18 18:01:23 +02004424 if (sched_feat(HRTICK))
Mike Galbraithf333fdc2008-05-12 21:20:55 +02004425 hrtick_clear(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004426
Paul E. McKenney46a5d162015-10-07 09:10:48 -07004427 local_irq_disable();
Paul E. McKenneybcbfdd02017-04-11 15:50:41 -07004428 rcu_note_context_switch(preempt);
Paul E. McKenney46a5d162015-10-07 09:10:48 -07004429
Oleg Nesterove0acd0a2013-08-12 18:14:00 +02004430 /*
4431 * Make sure that signal_pending_state()->signal_pending() below
4432 * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE)
Peter Zijlstradbfb0892020-07-03 12:40:33 +02004433 * done by the caller to avoid the race with signal_wake_up():
Mathieu Desnoyers306e0602018-01-29 15:20:12 -05004434 *
Peter Zijlstradbfb0892020-07-03 12:40:33 +02004435 * __set_current_state(@state) signal_wake_up()
4436 * schedule() set_tsk_thread_flag(p, TIF_SIGPENDING)
4437 * wake_up_state(p, state)
4438 * LOCK rq->lock LOCK p->pi_state
4439 * smp_mb__after_spinlock() smp_mb__after_spinlock()
4440 * if (signal_pending_state()) if (p->state & @state)
4441 *
4442 * Also, the membarrier system call requires a full memory barrier
Mathieu Desnoyers306e0602018-01-29 15:20:12 -05004443 * after coming from user-space, before storing to rq->curr.
Oleg Nesterove0acd0a2013-08-12 18:14:00 +02004444 */
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02004445 rq_lock(rq, &rf);
Peter Zijlstrad89e588c2016-09-05 11:37:53 +02004446 smp_mb__after_spinlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Ingo Molnard1ccc662017-02-01 11:46:42 +01004448 /* Promote REQ to ACT */
4449 rq->clock_update_flags <<= 1;
Peter Zijlstrabce4dc82017-02-21 14:40:35 +01004450 update_rq_clock(rq);
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01004451
Oleg Nesterov246d86b2010-05-19 14:57:11 +02004452 switch_count = &prev->nivcsw;
Peter Zijlstrad1361222020-07-20 17:20:21 +02004453
Peter Zijlstradbfb0892020-07-03 12:40:33 +02004454 /*
Peter Zijlstrad1361222020-07-20 17:20:21 +02004455 * We must load prev->state once (task_struct::state is volatile), such
4456 * that:
4457 *
4458 * - we form a control dependency vs deactivate_task() below.
4459 * - ptrace_{,un}freeze_traced() can change ->state underneath us.
Peter Zijlstradbfb0892020-07-03 12:40:33 +02004460 */
Peter Zijlstrad1361222020-07-20 17:20:21 +02004461 prev_state = prev->state;
4462 if (!preempt && prev_state) {
Peter Zijlstradbfb0892020-07-03 12:40:33 +02004463 if (signal_pending_state(prev_state, prev)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02004464 prev->state = TASK_RUNNING;
Tejun Heo21aa9af2010-06-08 21:40:37 +02004465 } else {
Peter Zijlstradbfb0892020-07-03 12:40:33 +02004466 prev->sched_contributes_to_load =
4467 (prev_state & TASK_UNINTERRUPTIBLE) &&
4468 !(prev_state & TASK_NOLOAD) &&
4469 !(prev->flags & PF_FROZEN);
4470
4471 if (prev->sched_contributes_to_load)
4472 rq->nr_uninterruptible++;
4473
4474 /*
4475 * __schedule() ttwu()
Peter Zijlstrad1361222020-07-20 17:20:21 +02004476 * prev_state = prev->state; if (p->on_rq && ...)
4477 * if (prev_state) goto out;
4478 * p->on_rq = 0; smp_acquire__after_ctrl_dep();
4479 * p->state = TASK_WAKING
4480 *
4481 * Where __schedule() and ttwu() have matching control dependencies.
Peter Zijlstradbfb0892020-07-03 12:40:33 +02004482 *
4483 * After this, schedule() must not care about p->state any more.
4484 */
Peter Zijlstrabce4dc82017-02-21 14:40:35 +01004485 deactivate_task(rq, prev, DEQUEUE_SLEEP | DEQUEUE_NOCLOCK);
Peter Zijlstra2acca552011-04-05 17:23:50 +02004486
Tejun Heoe33a9bb2016-12-07 15:48:41 -05004487 if (prev->in_iowait) {
4488 atomic_inc(&rq->nr_iowait);
4489 delayacct_blkio_start();
4490 }
Tejun Heo21aa9af2010-06-08 21:40:37 +02004491 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004492 switch_count = &prev->nvcsw;
4493 }
4494
Matt Flemingd8ac8972016-09-21 14:38:10 +01004495 next = pick_next_task(rq, prev, &rf);
Mike Galbraithf26f9af2010-12-08 11:05:42 +01004496 clear_tsk_need_resched(prev);
Peter Zijlstraf27dde82013-08-14 14:55:31 +02004497 clear_preempt_need_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 rq->nr_switches++;
Eric W. Biederman5311a982019-09-14 07:35:02 -05004501 /*
4502 * RCU users of rcu_dereference(rq->curr) may not see
4503 * changes to task_struct made by pick_next_task().
4504 */
4505 RCU_INIT_POINTER(rq->curr, next);
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -04004506 /*
4507 * The membarrier system call requires each architecture
4508 * to have a full memory barrier after updating
Mathieu Desnoyers306e0602018-01-29 15:20:12 -05004509 * rq->curr, before returning to user-space.
4510 *
4511 * Here are the schemes providing that barrier on the
4512 * various architectures:
4513 * - mm ? switch_mm() : mmdrop() for x86, s390, sparc, PowerPC.
4514 * switch_mm() rely on membarrier_arch_switch_mm() on PowerPC.
4515 * - finish_lock_switch() for weakly-ordered
4516 * architectures where spin_unlock is a full barrier,
4517 * - switch_to() for arm64 (weakly-ordered, spin_unlock
4518 * is a RELEASE barrier),
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -04004519 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 ++*switch_count;
4521
Johannes Weinerb05e75d2020-03-16 15:13:31 -04004522 psi_sched_switch(prev, next, !task_on_rq_queued(prev));
4523
Peter Zijlstrac73464b2015-09-28 18:06:56 +02004524 trace_sched_switch(preempt, prev, next);
Ingo Molnard1ccc662017-02-01 11:46:42 +01004525
4526 /* Also unlocks the rq: */
4527 rq = context_switch(rq, prev, next, &rf);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02004528 } else {
Matt Flemingcb42c9a2016-09-21 14:38:13 +01004529 rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02004530 rq_unlock_irq(rq, &rf);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02004531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02004533 balance_callback(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534}
Thomas Gleixnerc259e012011-06-22 19:47:00 +02004535
Peter Zijlstra9af65282016-09-13 18:37:29 +02004536void __noreturn do_task_dead(void)
4537{
Ingo Molnard1ccc662017-02-01 11:46:42 +01004538 /* Causes final put_task_struct in finish_task_switch(): */
Peter Zijlstrab5bf9a92018-04-30 14:51:01 +02004539 set_special_state(TASK_DEAD);
Ingo Molnard1ccc662017-02-01 11:46:42 +01004540
4541 /* Tell freezer to ignore us: */
4542 current->flags |= PF_NOFREEZE;
4543
Peter Zijlstra9af65282016-09-13 18:37:29 +02004544 __schedule(false);
4545 BUG();
Ingo Molnard1ccc662017-02-01 11:46:42 +01004546
4547 /* Avoid "noreturn function does return" - but don't continue if BUG() is a NOP: */
Peter Zijlstra9af65282016-09-13 18:37:29 +02004548 for (;;)
Ingo Molnard1ccc662017-02-01 11:46:42 +01004549 cpu_relax();
Peter Zijlstra9af65282016-09-13 18:37:29 +02004550}
4551
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02004552static inline void sched_submit_work(struct task_struct *tsk)
4553{
Sebastian Andrzej Siewiorb0fdc012019-08-16 18:06:26 +02004554 if (!tsk->state)
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02004555 return;
Thomas Gleixner6d25be52019-03-13 17:55:48 +01004556
4557 /*
4558 * If a worker went to sleep, notify and ask workqueue whether
4559 * it wants to wake up a task to maintain concurrency.
4560 * As this function is called inside the schedule() context,
4561 * we disable preemption to avoid it calling schedule() again
Sebastian Andrzej Siewior62849a92020-03-28 00:29:59 +01004562 * in the possible wakeup of a kworker and because wq_worker_sleeping()
4563 * requires it.
Thomas Gleixner6d25be52019-03-13 17:55:48 +01004564 */
Jens Axboe771b53d02019-10-22 10:25:58 -06004565 if (tsk->flags & (PF_WQ_WORKER | PF_IO_WORKER)) {
Thomas Gleixner6d25be52019-03-13 17:55:48 +01004566 preempt_disable();
Jens Axboe771b53d02019-10-22 10:25:58 -06004567 if (tsk->flags & PF_WQ_WORKER)
4568 wq_worker_sleeping(tsk);
4569 else
4570 io_wq_worker_sleeping(tsk);
Thomas Gleixner6d25be52019-03-13 17:55:48 +01004571 preempt_enable_no_resched();
4572 }
4573
Sebastian Andrzej Siewiorb0fdc012019-08-16 18:06:26 +02004574 if (tsk_is_pi_blocked(tsk))
4575 return;
4576
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02004577 /*
4578 * If we are going to sleep and we have plugged IO queued,
4579 * make sure to submit it to avoid deadlocks.
4580 */
4581 if (blk_needs_flush_plug(tsk))
4582 blk_schedule_flush_plug(tsk);
4583}
4584
Thomas Gleixner6d25be52019-03-13 17:55:48 +01004585static void sched_update_worker(struct task_struct *tsk)
4586{
Jens Axboe771b53d02019-10-22 10:25:58 -06004587 if (tsk->flags & (PF_WQ_WORKER | PF_IO_WORKER)) {
4588 if (tsk->flags & PF_WQ_WORKER)
4589 wq_worker_running(tsk);
4590 else
4591 io_wq_worker_running(tsk);
4592 }
Thomas Gleixner6d25be52019-03-13 17:55:48 +01004593}
4594
Andi Kleen722a9f92014-05-02 00:44:38 +02004595asmlinkage __visible void __sched schedule(void)
Thomas Gleixnerc259e012011-06-22 19:47:00 +02004596{
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02004597 struct task_struct *tsk = current;
4598
4599 sched_submit_work(tsk);
Frederic Weisbeckerbfd9b2b2015-01-28 01:24:09 +01004600 do {
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02004601 preempt_disable();
Peter Zijlstrafc13aeb2015-09-28 18:05:34 +02004602 __schedule(false);
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02004603 sched_preempt_enable_no_resched();
Frederic Weisbeckerbfd9b2b2015-01-28 01:24:09 +01004604 } while (need_resched());
Thomas Gleixner6d25be52019-03-13 17:55:48 +01004605 sched_update_worker(tsk);
Thomas Gleixnerc259e012011-06-22 19:47:00 +02004606}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607EXPORT_SYMBOL(schedule);
4608
Steven Rostedt (VMware)8663eff2017-04-14 08:48:09 -04004609/*
4610 * synchronize_rcu_tasks() makes sure that no task is stuck in preempted
4611 * state (have scheduled out non-voluntarily) by making sure that all
4612 * tasks have either left the run queue or have gone into user space.
4613 * As idle tasks do not do either, they must not ever be preempted
4614 * (schedule out non-voluntarily).
4615 *
4616 * schedule_idle() is similar to schedule_preempt_disable() except that it
4617 * never enables preemption because it does not call sched_submit_work().
4618 */
4619void __sched schedule_idle(void)
4620{
4621 /*
4622 * As this skips calling sched_submit_work(), which the idle task does
4623 * regardless because that function is a nop when the task is in a
4624 * TASK_RUNNING state, make sure this isn't used someplace that the
4625 * current task can be in any other state. Note, idle is always in the
4626 * TASK_RUNNING state.
4627 */
4628 WARN_ON_ONCE(current->state);
4629 do {
4630 __schedule(false);
4631 } while (need_resched());
4632}
4633
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +01004634#ifdef CONFIG_CONTEXT_TRACKING
Andi Kleen722a9f92014-05-02 00:44:38 +02004635asmlinkage __visible void __sched schedule_user(void)
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02004636{
4637 /*
4638 * If we come here after a random call to set_need_resched(),
4639 * or we have been woken up remotely but the IPI has not yet arrived,
4640 * we haven't yet exited the RCU idle mode. Do it here manually until
4641 * we find a better solution.
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08004642 *
4643 * NB: There are buggy callers of this function. Ideally we
Frederic Weisbeckerc467ea72015-03-04 18:06:33 +01004644 * should warn if prev_state != CONTEXT_USER, but that will trigger
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08004645 * too frequently to make sense yet.
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02004646 */
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08004647 enum ctx_state prev_state = exception_enter();
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02004648 schedule();
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08004649 exception_exit(prev_state);
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02004650}
4651#endif
4652
Thomas Gleixnerc5491ea2011-03-21 12:09:35 +01004653/**
4654 * schedule_preempt_disabled - called with preemption disabled
4655 *
4656 * Returns with preemption disabled. Note: preempt_count must be 1
4657 */
4658void __sched schedule_preempt_disabled(void)
4659{
Thomas Gleixnerba74c142011-03-21 13:32:17 +01004660 sched_preempt_enable_no_resched();
Thomas Gleixnerc5491ea2011-03-21 12:09:35 +01004661 schedule();
4662 preempt_disable();
4663}
4664
Frederic Weisbecker06b1f802015-02-16 19:20:07 +01004665static void __sched notrace preempt_schedule_common(void)
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01004666{
4667 do {
Steven Rostedt47252cf2016-03-21 11:23:39 -04004668 /*
4669 * Because the function tracer can trace preempt_count_sub()
4670 * and it also uses preempt_enable/disable_notrace(), if
4671 * NEED_RESCHED is set, the preempt_enable_notrace() called
4672 * by the function tracer will call this function again and
4673 * cause infinite recursion.
4674 *
4675 * Preemption must be disabled here before the function
4676 * tracer can trace. Break up preempt_disable() into two
4677 * calls. One to disable preemption without fear of being
4678 * traced. The other to still record the preemption latency,
4679 * which can also be traced by the function tracer.
4680 */
Peter Zijlstra499d7952015-09-28 18:52:36 +02004681 preempt_disable_notrace();
Steven Rostedt47252cf2016-03-21 11:23:39 -04004682 preempt_latency_start(1);
Peter Zijlstrafc13aeb2015-09-28 18:05:34 +02004683 __schedule(true);
Steven Rostedt47252cf2016-03-21 11:23:39 -04004684 preempt_latency_stop(1);
Peter Zijlstra499d7952015-09-28 18:52:36 +02004685 preempt_enable_no_resched_notrace();
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01004686
4687 /*
4688 * Check again in case we missed a preemption opportunity
4689 * between schedule and now.
4690 */
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01004691 } while (need_resched());
4692}
4693
Thomas Gleixnerc1a280b2019-07-26 23:19:37 +02004694#ifdef CONFIG_PREEMPTION
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695/*
Valentin Schneidera49b4f42019-09-23 15:36:12 +01004696 * This is the entry point to schedule() from in-kernel preemption
4697 * off of preempt_enable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 */
Andi Kleen722a9f92014-05-02 00:44:38 +02004699asmlinkage __visible void __sched notrace preempt_schedule(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 /*
4702 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004703 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 */
Frederic Weisbeckerfbb00b52013-06-19 23:56:22 +02004705 if (likely(!preemptible()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 return;
4707
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01004708 preempt_schedule_common();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709}
Masami Hiramatsu376e2422014-04-17 17:17:05 +09004710NOKPROBE_SYMBOL(preempt_schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711EXPORT_SYMBOL(preempt_schedule);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02004712
Oleg Nesterov009f60e2014-10-05 22:23:22 +02004713/**
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +02004714 * preempt_schedule_notrace - preempt_schedule called by tracing
Oleg Nesterov009f60e2014-10-05 22:23:22 +02004715 *
4716 * The tracing infrastructure uses preempt_enable_notrace to prevent
4717 * recursion and tracing preempt enabling caused by the tracing
4718 * infrastructure itself. But as tracing can happen in areas coming
4719 * from userspace or just about to enter userspace, a preempt enable
4720 * can occur before user_exit() is called. This will cause the scheduler
4721 * to be called when the system is still in usermode.
4722 *
4723 * To prevent this, the preempt_enable_notrace will use this function
4724 * instead of preempt_schedule() to exit user context if needed before
4725 * calling the scheduler.
4726 */
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +02004727asmlinkage __visible void __sched notrace preempt_schedule_notrace(void)
Oleg Nesterov009f60e2014-10-05 22:23:22 +02004728{
4729 enum ctx_state prev_ctx;
4730
4731 if (likely(!preemptible()))
4732 return;
4733
4734 do {
Steven Rostedt47252cf2016-03-21 11:23:39 -04004735 /*
4736 * Because the function tracer can trace preempt_count_sub()
4737 * and it also uses preempt_enable/disable_notrace(), if
4738 * NEED_RESCHED is set, the preempt_enable_notrace() called
4739 * by the function tracer will call this function again and
4740 * cause infinite recursion.
4741 *
4742 * Preemption must be disabled here before the function
4743 * tracer can trace. Break up preempt_disable() into two
4744 * calls. One to disable preemption without fear of being
4745 * traced. The other to still record the preemption latency,
4746 * which can also be traced by the function tracer.
4747 */
Peter Zijlstra3d8f74d2015-09-28 18:09:19 +02004748 preempt_disable_notrace();
Steven Rostedt47252cf2016-03-21 11:23:39 -04004749 preempt_latency_start(1);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02004750 /*
4751 * Needs preempt disabled in case user_exit() is traced
4752 * and the tracer calls preempt_enable_notrace() causing
4753 * an infinite recursion.
4754 */
4755 prev_ctx = exception_enter();
Peter Zijlstrafc13aeb2015-09-28 18:05:34 +02004756 __schedule(true);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02004757 exception_exit(prev_ctx);
4758
Steven Rostedt47252cf2016-03-21 11:23:39 -04004759 preempt_latency_stop(1);
Peter Zijlstra3d8f74d2015-09-28 18:09:19 +02004760 preempt_enable_no_resched_notrace();
Oleg Nesterov009f60e2014-10-05 22:23:22 +02004761 } while (need_resched());
4762}
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +02004763EXPORT_SYMBOL_GPL(preempt_schedule_notrace);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02004764
Thomas Gleixnerc1a280b2019-07-26 23:19:37 +02004765#endif /* CONFIG_PREEMPTION */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
4767/*
Valentin Schneidera49b4f42019-09-23 15:36:12 +01004768 * This is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 * off of irq context.
4770 * Note, that this is called and return with irqs disabled. This will
4771 * protect us against recursive calling from irq.
4772 */
Andi Kleen722a9f92014-05-02 00:44:38 +02004773asmlinkage __visible void __sched preempt_schedule_irq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774{
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01004775 enum ctx_state prev_state;
Ingo Molnar6478d882008-01-25 21:08:33 +01004776
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004777 /* Catch callers which need to be fixed */
Peter Zijlstraf27dde82013-08-14 14:55:31 +02004778 BUG_ON(preempt_count() || !irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01004780 prev_state = exception_enter();
4781
Andi Kleen3a5c3592007-10-15 17:00:14 +02004782 do {
Peter Zijlstra3d8f74d2015-09-28 18:09:19 +02004783 preempt_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004784 local_irq_enable();
Peter Zijlstrafc13aeb2015-09-28 18:05:34 +02004785 __schedule(true);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004786 local_irq_disable();
Peter Zijlstra3d8f74d2015-09-28 18:09:19 +02004787 sched_preempt_enable_no_resched();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08004788 } while (need_resched());
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01004789
4790 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791}
4792
Ingo Molnarac6424b2017-06-20 12:06:13 +02004793int default_wake_function(wait_queue_entry_t *curr, unsigned mode, int wake_flags,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004794 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795{
Chris Wilson062d3f92020-07-23 21:10:42 +01004796 WARN_ON_ONCE(IS_ENABLED(CONFIG_SCHED_DEBUG) && wake_flags & ~WF_SYNC);
Peter Zijlstra63859d42009-09-15 19:14:42 +02004797 return try_to_wake_up(curr->private, mode, wake_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799EXPORT_SYMBOL(default_wake_function);
4800
Ingo Molnarb29739f2006-06-27 02:54:51 -07004801#ifdef CONFIG_RT_MUTEXES
4802
Peter Zijlstraacd58622017-03-23 15:56:11 +01004803static inline int __rt_effective_prio(struct task_struct *pi_task, int prio)
4804{
4805 if (pi_task)
4806 prio = min(prio, pi_task->prio);
4807
4808 return prio;
4809}
4810
4811static inline int rt_effective_prio(struct task_struct *p, int prio)
4812{
4813 struct task_struct *pi_task = rt_mutex_get_top_task(p);
4814
4815 return __rt_effective_prio(pi_task, prio);
4816}
4817
Ingo Molnarb29739f2006-06-27 02:54:51 -07004818/*
4819 * rt_mutex_setprio - set the current priority of a task
Peter Zijlstraacd58622017-03-23 15:56:11 +01004820 * @p: task to boost
4821 * @pi_task: donor task
Ingo Molnarb29739f2006-06-27 02:54:51 -07004822 *
4823 * This function changes the 'effective' priority of a task. It does
4824 * not touch ->normal_prio like __setscheduler().
4825 *
Thomas Gleixnerc365c292014-02-07 20:58:42 +01004826 * Used by the rt_mutex code to implement priority inheritance
4827 * logic. Call site only calls if the priority of the task changed.
Ingo Molnarb29739f2006-06-27 02:54:51 -07004828 */
Peter Zijlstraacd58622017-03-23 15:56:11 +01004829void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004830{
Peter Zijlstraacd58622017-03-23 15:56:11 +01004831 int prio, oldprio, queued, running, queue_flag =
Peter Zijlstra7a57f322017-02-21 14:47:02 +01004832 DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004833 const struct sched_class *prev_class;
Peter Zijlstraeb580752015-07-31 21:28:18 +02004834 struct rq_flags rf;
4835 struct rq *rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004836
Peter Zijlstraacd58622017-03-23 15:56:11 +01004837 /* XXX used to be waiter->prio, not waiter->task->prio */
4838 prio = __rt_effective_prio(pi_task, p->normal_prio);
4839
4840 /*
4841 * If nothing changed; bail early.
4842 */
4843 if (p->pi_top_task == pi_task && prio == p->prio && !dl_prio(prio))
4844 return;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004845
Peter Zijlstraeb580752015-07-31 21:28:18 +02004846 rq = __task_rq_lock(p, &rf);
Peter Zijlstra80f5c1b2016-10-03 16:28:37 +02004847 update_rq_clock(rq);
Peter Zijlstraacd58622017-03-23 15:56:11 +01004848 /*
4849 * Set under pi_lock && rq->lock, such that the value can be used under
4850 * either lock.
4851 *
4852 * Note that there is loads of tricky to make this pointer cache work
4853 * right. rt_mutex_slowunlock()+rt_mutex_postunlock() work together to
4854 * ensure a task is de-boosted (pi_task is set to NULL) before the
4855 * task is allowed to run again (and can exit). This ensures the pointer
4856 * points to a blocked task -- which guaratees the task is present.
4857 */
4858 p->pi_top_task = pi_task;
4859
4860 /*
4861 * For FIFO/RR we only need to set prio, if that matches we're done.
4862 */
4863 if (prio == p->prio && !dl_prio(prio))
4864 goto out_unlock;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004865
Thomas Gleixner1c4dd992011-06-06 20:07:38 +02004866 /*
4867 * Idle task boosting is a nono in general. There is one
4868 * exception, when PREEMPT_RT and NOHZ is active:
4869 *
4870 * The idle task calls get_next_timer_interrupt() and holds
4871 * the timer wheel base->lock on the CPU and another CPU wants
4872 * to access the timer (probably to cancel it). We can safely
4873 * ignore the boosting request, as the idle CPU runs this code
4874 * with interrupts disabled and will complete the lock
4875 * protected section without being interrupted. So there is no
4876 * real need to boost.
4877 */
4878 if (unlikely(p == rq->idle)) {
4879 WARN_ON(p != rq->curr);
4880 WARN_ON(p->pi_blocked_on);
4881 goto out_unlock;
4882 }
4883
Peter Zijlstrab91473f2017-03-23 15:56:12 +01004884 trace_sched_pi_setprio(p, pi_task);
Andrew Mortond5f9f942007-05-08 20:27:06 -07004885 oldprio = p->prio;
Peter Zijlstraff77e462016-01-18 15:27:07 +01004886
4887 if (oldprio == prio)
4888 queue_flag &= ~DEQUEUE_MOVE;
4889
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004890 prev_class = p->sched_class;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004891 queued = task_on_rq_queued(p);
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004892 running = task_current(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004893 if (queued)
Peter Zijlstraff77e462016-01-18 15:27:07 +01004894 dequeue_task(rq, p, queue_flag);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004895 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04004896 put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004897
Dario Faggioli2d3d8912013-11-07 14:43:44 +01004898 /*
4899 * Boosting condition are:
4900 * 1. -rt task is running and holds mutex A
4901 * --> -dl task blocks on mutex A
4902 *
4903 * 2. -dl task is running and holds mutex A
4904 * --> -dl task blocks on mutex A and could preempt the
4905 * running task
4906 */
4907 if (dl_prio(prio)) {
Oleg Nesterov466af292014-06-06 18:52:06 +02004908 if (!dl_prio(p->normal_prio) ||
Juri Lelli740797c2018-11-19 16:32:01 +01004909 (pi_task && dl_prio(pi_task->prio) &&
4910 dl_entity_preempt(&pi_task->dl, &p->dl))) {
Dario Faggioli2d3d8912013-11-07 14:43:44 +01004911 p->dl.dl_boosted = 1;
Peter Zijlstraff77e462016-01-18 15:27:07 +01004912 queue_flag |= ENQUEUE_REPLENISH;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01004913 } else
4914 p->dl.dl_boosted = 0;
Dario Faggioliaab03e02013-11-28 11:14:43 +01004915 p->sched_class = &dl_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01004916 } else if (rt_prio(prio)) {
4917 if (dl_prio(oldprio))
4918 p->dl.dl_boosted = 0;
4919 if (oldprio < prio)
Peter Zijlstraff77e462016-01-18 15:27:07 +01004920 queue_flag |= ENQUEUE_HEAD;
Ingo Molnardd41f592007-07-09 18:51:59 +02004921 p->sched_class = &rt_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01004922 } else {
4923 if (dl_prio(oldprio))
4924 p->dl.dl_boosted = 0;
Brian Silverman746db942015-02-18 16:23:56 -08004925 if (rt_prio(oldprio))
4926 p->rt.timeout = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02004927 p->sched_class = &fair_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01004928 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004929
Ingo Molnarb29739f2006-06-27 02:54:51 -07004930 p->prio = prio;
4931
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004932 if (queued)
Peter Zijlstraff77e462016-01-18 15:27:07 +01004933 enqueue_task(rq, p, queue_flag);
Vincent Guittota399d232016-09-12 09:47:52 +02004934 if (running)
Peter Zijlstra03b7fad2019-05-29 20:36:41 +00004935 set_next_task(rq, p);
Steven Rostedtcb469842008-01-25 21:08:22 +01004936
Peter Zijlstrada7a7352011-01-17 17:03:27 +01004937 check_class_changed(rq, p, prev_class, oldprio);
Thomas Gleixner1c4dd992011-06-06 20:07:38 +02004938out_unlock:
Ingo Molnard1ccc662017-02-01 11:46:42 +01004939 /* Avoid rq from going away on us: */
4940 preempt_disable();
Peter Zijlstraeb580752015-07-31 21:28:18 +02004941 __task_rq_unlock(rq, &rf);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02004942
4943 balance_callback(rq);
4944 preempt_enable();
Ingo Molnarb29739f2006-06-27 02:54:51 -07004945}
Peter Zijlstraacd58622017-03-23 15:56:11 +01004946#else
4947static inline int rt_effective_prio(struct task_struct *p, int prio)
4948{
4949 return prio;
4950}
Ingo Molnarb29739f2006-06-27 02:54:51 -07004951#endif
Dario Faggiolid50dde52013-11-07 14:43:36 +01004952
Ingo Molnar36c8b582006-07-03 00:25:41 -07004953void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954{
Peter Zijlstra49bd21e2016-09-20 22:06:01 +02004955 bool queued, running;
Qian Cai53a23362019-12-19 09:03:14 -05004956 int old_prio;
Peter Zijlstraeb580752015-07-31 21:28:18 +02004957 struct rq_flags rf;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004958 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959
Dongsheng Yang75e45d52014-02-11 15:34:50 +08004960 if (task_nice(p) == nice || nice < MIN_NICE || nice > MAX_NICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 return;
4962 /*
4963 * We have to be careful, if called from sys_setpriority(),
4964 * the task might be in the middle of scheduling on another CPU.
4965 */
Peter Zijlstraeb580752015-07-31 21:28:18 +02004966 rq = task_rq_lock(p, &rf);
Peter Zijlstra2fb8d362016-10-03 16:44:25 +02004967 update_rq_clock(rq);
4968
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 /*
4970 * The RT priorities are set via sched_setscheduler(), but we still
4971 * allow the 'normal' nice value to be set - but as expected
4972 * it wont have any effect on scheduling until the task is
Dario Faggioliaab03e02013-11-28 11:14:43 +01004973 * SCHED_DEADLINE, SCHED_FIFO or SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01004975 if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 p->static_prio = NICE_TO_PRIO(nice);
4977 goto out_unlock;
4978 }
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004979 queued = task_on_rq_queued(p);
Peter Zijlstra49bd21e2016-09-20 22:06:01 +02004980 running = task_current(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004981 if (queued)
Peter Zijlstra7a57f322017-02-21 14:47:02 +01004982 dequeue_task(rq, p, DEQUEUE_SAVE | DEQUEUE_NOCLOCK);
Peter Zijlstra49bd21e2016-09-20 22:06:01 +02004983 if (running)
4984 put_prev_task(rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 p->static_prio = NICE_TO_PRIO(nice);
Vincent Guittot90593932017-05-17 11:50:45 +02004987 set_load_weight(p, true);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004988 old_prio = p->prio;
4989 p->prio = effective_prio(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990
Frederic Weisbecker5443a0b2019-12-03 17:01:06 +01004991 if (queued)
Peter Zijlstra7134b3e2017-02-21 14:23:38 +01004992 enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK);
Peter Zijlstra49bd21e2016-09-20 22:06:01 +02004993 if (running)
Peter Zijlstra03b7fad2019-05-29 20:36:41 +00004994 set_next_task(rq, p);
Frederic Weisbecker5443a0b2019-12-03 17:01:06 +01004995
4996 /*
4997 * If the task increased its priority or is running and
4998 * lowered its priority, then reschedule its CPU:
4999 */
5000 p->sched_class->prio_changed(rq, p, old_prio);
5001
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002out_unlock:
Peter Zijlstraeb580752015-07-31 21:28:18 +02005003 task_rq_unlock(rq, p, &rf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005EXPORT_SYMBOL(set_user_nice);
5006
Matt Mackalle43379f2005-05-01 08:59:00 -07005007/*
5008 * can_nice - check if a task can reduce its nice value
5009 * @p: task
5010 * @nice: nice value
5011 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005012int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07005013{
Ingo Molnard1ccc662017-02-01 11:46:42 +01005014 /* Convert nice value [19,-20] to rlimit style value [1,40]: */
Dongsheng Yang7aa2c012014-05-08 18:33:49 +09005015 int nice_rlim = nice_to_rlimit(nice);
Ingo Molnar48f24c42006-07-03 00:25:40 -07005016
Jiri Slaby78d7d402010-03-05 13:42:54 -08005017 return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
Matt Mackalle43379f2005-05-01 08:59:00 -07005018 capable(CAP_SYS_NICE));
5019}
5020
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021#ifdef __ARCH_WANT_SYS_NICE
5022
5023/*
5024 * sys_nice - change the priority of the current process.
5025 * @increment: priority increment
5026 *
5027 * sys_setpriority is a more generic, but much slower function that
5028 * does similar things.
5029 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005030SYSCALL_DEFINE1(nice, int, increment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031{
Ingo Molnar48f24c42006-07-03 00:25:40 -07005032 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033
5034 /*
5035 * Setpriority might change our priority at the same moment.
5036 * We don't have to worry. Conceptually one call occurs first
5037 * and we have a single winner.
5038 */
Dongsheng Yanga9467fa2014-05-08 18:35:15 +09005039 increment = clamp(increment, -NICE_WIDTH, NICE_WIDTH);
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05005040 nice = task_nice(current) + increment;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041
Dongsheng Yanga9467fa2014-05-08 18:35:15 +09005042 nice = clamp_val(nice, MIN_NICE, MAX_NICE);
Matt Mackalle43379f2005-05-01 08:59:00 -07005043 if (increment < 0 && !can_nice(current, nice))
5044 return -EPERM;
5045
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 retval = security_task_setnice(current, nice);
5047 if (retval)
5048 return retval;
5049
5050 set_user_nice(current, nice);
5051 return 0;
5052}
5053
5054#endif
5055
5056/**
5057 * task_prio - return the priority value of a given task.
5058 * @p: the task in question.
5059 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02005060 * Return: The priority value as seen by users in /proc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 * RT tasks are offset by -200. Normal tasks are centered
5062 * around 0, value goes from -16 to +15.
5063 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005064int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065{
5066 return p->prio - MAX_RT_PRIO;
5067}
5068
5069/**
Ingo Molnard1ccc662017-02-01 11:46:42 +01005070 * idle_cpu - is a given CPU idle currently?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 * @cpu: the processor in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02005072 *
5073 * Return: 1 if the CPU is currently idle. 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 */
5075int idle_cpu(int cpu)
5076{
Thomas Gleixner908a3282011-09-15 15:32:06 +02005077 struct rq *rq = cpu_rq(cpu);
5078
5079 if (rq->curr != rq->idle)
5080 return 0;
5081
5082 if (rq->nr_running)
5083 return 0;
5084
5085#ifdef CONFIG_SMP
Peter Zijlstra126c2092020-05-26 18:11:03 +02005086 if (rq->ttwu_pending)
Thomas Gleixner908a3282011-09-15 15:32:06 +02005087 return 0;
5088#endif
5089
5090 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091}
5092
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093/**
Rohit Jain943d3552018-05-09 09:39:48 -07005094 * available_idle_cpu - is a given CPU idle for enqueuing work.
5095 * @cpu: the CPU in question.
5096 *
5097 * Return: 1 if the CPU is currently idle. 0 otherwise.
5098 */
5099int available_idle_cpu(int cpu)
5100{
5101 if (!idle_cpu(cpu))
5102 return 0;
5103
Rohit Jain247f2f62018-05-02 13:52:10 -07005104 if (vcpu_is_preempted(cpu))
5105 return 0;
5106
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 return 1;
5108}
5109
5110/**
Ingo Molnard1ccc662017-02-01 11:46:42 +01005111 * idle_task - return the idle task for a given CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 * @cpu: the processor in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02005113 *
Ingo Molnard1ccc662017-02-01 11:46:42 +01005114 * Return: The idle task for the CPU @cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07005116struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117{
5118 return cpu_rq(cpu)->idle;
5119}
5120
5121/**
5122 * find_process_by_pid - find a process with a matching PID value.
5123 * @pid: the pid in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02005124 *
5125 * The task of @pid, if found. %NULL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02005127static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07005129 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130}
5131
Dario Faggioliaab03e02013-11-28 11:14:43 +01005132/*
Steven Rostedtc13db6b2014-07-23 11:28:26 -04005133 * sched_setparam() passes in -1 for its policy, to let the functions
5134 * it calls know not to change it.
5135 */
5136#define SETPARAM_POLICY -1
5137
Thomas Gleixnerc365c292014-02-07 20:58:42 +01005138static void __setscheduler_params(struct task_struct *p,
5139 const struct sched_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140{
Dario Faggiolid50dde52013-11-07 14:43:36 +01005141 int policy = attr->sched_policy;
5142
Steven Rostedtc13db6b2014-07-23 11:28:26 -04005143 if (policy == SETPARAM_POLICY)
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01005144 policy = p->policy;
5145
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 p->policy = policy;
Dario Faggiolid50dde52013-11-07 14:43:36 +01005147
Dario Faggioliaab03e02013-11-28 11:14:43 +01005148 if (dl_policy(policy))
5149 __setparam_dl(p, attr);
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01005150 else if (fair_policy(policy))
Dario Faggiolid50dde52013-11-07 14:43:36 +01005151 p->static_prio = NICE_TO_PRIO(attr->sched_nice);
5152
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01005153 /*
5154 * __sched_setscheduler() ensures attr->sched_priority == 0 when
5155 * !rt_policy. Always setting this ensures that things like
5156 * getparam()/getattr() don't report silly values for !rt tasks.
5157 */
5158 p->rt_priority = attr->sched_priority;
Steven Rostedt383afd02014-03-11 19:24:20 -04005159 p->normal_prio = normal_prio(p);
Vincent Guittot90593932017-05-17 11:50:45 +02005160 set_load_weight(p, true);
Thomas Gleixnerc365c292014-02-07 20:58:42 +01005161}
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01005162
Thomas Gleixnerc365c292014-02-07 20:58:42 +01005163/* Actually do priority change: must hold pi & rq lock. */
5164static void __setscheduler(struct rq *rq, struct task_struct *p,
Thomas Gleixner0782e632015-05-05 19:49:49 +02005165 const struct sched_attr *attr, bool keep_boost)
Thomas Gleixnerc365c292014-02-07 20:58:42 +01005166{
Patrick Bellasia509a7c2019-06-21 09:42:07 +01005167 /*
5168 * If params can't change scheduling class changes aren't allowed
5169 * either.
5170 */
5171 if (attr->sched_flags & SCHED_FLAG_KEEP_PARAMS)
5172 return;
5173
Thomas Gleixnerc365c292014-02-07 20:58:42 +01005174 __setscheduler_params(p, attr);
Dario Faggiolid50dde52013-11-07 14:43:36 +01005175
Steven Rostedt383afd02014-03-11 19:24:20 -04005176 /*
Thomas Gleixner0782e632015-05-05 19:49:49 +02005177 * Keep a potential priority boosting if called from
5178 * sched_setscheduler().
Steven Rostedt383afd02014-03-11 19:24:20 -04005179 */
Peter Zijlstraacd58622017-03-23 15:56:11 +01005180 p->prio = normal_prio(p);
Thomas Gleixner0782e632015-05-05 19:49:49 +02005181 if (keep_boost)
Peter Zijlstraacd58622017-03-23 15:56:11 +01005182 p->prio = rt_effective_prio(p, p->prio);
Steven Rostedt383afd02014-03-11 19:24:20 -04005183
Dario Faggioliaab03e02013-11-28 11:14:43 +01005184 if (dl_prio(p->prio))
5185 p->sched_class = &dl_sched_class;
5186 else if (rt_prio(p->prio))
Peter Zijlstraffd44db2009-11-10 20:12:01 +01005187 p->sched_class = &rt_sched_class;
5188 else
5189 p->sched_class = &fair_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190}
Dario Faggioliaab03e02013-11-28 11:14:43 +01005191
David Howellsc69e8d92008-11-14 10:39:19 +11005192/*
Ingo Molnard1ccc662017-02-01 11:46:42 +01005193 * Check the target process has a UID that matches the current process's:
David Howellsc69e8d92008-11-14 10:39:19 +11005194 */
5195static bool check_same_owner(struct task_struct *p)
5196{
5197 const struct cred *cred = current_cred(), *pcred;
5198 bool match;
5199
5200 rcu_read_lock();
5201 pcred = __task_cred(p);
Eric W. Biederman9c806aa2012-02-02 18:54:02 -08005202 match = (uid_eq(cred->euid, pcred->euid) ||
5203 uid_eq(cred->euid, pcred->uid));
David Howellsc69e8d92008-11-14 10:39:19 +11005204 rcu_read_unlock();
5205 return match;
5206}
5207
Dario Faggiolid50dde52013-11-07 14:43:36 +01005208static int __sched_setscheduler(struct task_struct *p,
5209 const struct sched_attr *attr,
Peter Zijlstradbc7f062015-06-11 14:46:38 +02005210 bool user, bool pi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211{
Steven Rostedt383afd02014-03-11 19:24:20 -04005212 int newprio = dl_policy(attr->sched_policy) ? MAX_DL_PRIO - 1 :
5213 MAX_RT_PRIO - 1 - attr->sched_priority;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005214 int retval, oldprio, oldpolicy = -1, queued, running;
Thomas Gleixner0782e632015-05-05 19:49:49 +02005215 int new_effective_prio, policy = attr->sched_policy;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01005216 const struct sched_class *prev_class;
Peter Zijlstraeb580752015-07-31 21:28:18 +02005217 struct rq_flags rf;
Lennart Poetteringca94c442009-06-15 17:17:47 +02005218 int reset_on_fork;
Peter Zijlstra7a57f322017-02-21 14:47:02 +01005219 int queue_flags = DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK;
Peter Zijlstraeb580752015-07-31 21:28:18 +02005220 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221
Steven Rostedt (VMware)896bbb22017-03-09 10:18:42 -05005222 /* The pi code expects interrupts enabled */
5223 BUG_ON(pi && in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224recheck:
Ingo Molnard1ccc662017-02-01 11:46:42 +01005225 /* Double check policy once rq lock held: */
Lennart Poetteringca94c442009-06-15 17:17:47 +02005226 if (policy < 0) {
5227 reset_on_fork = p->sched_reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 policy = oldpolicy = p->policy;
Lennart Poetteringca94c442009-06-15 17:17:47 +02005229 } else {
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01005230 reset_on_fork = !!(attr->sched_flags & SCHED_FLAG_RESET_ON_FORK);
Lennart Poetteringca94c442009-06-15 17:17:47 +02005231
Henrik Austad20f9cd22015-09-09 17:00:41 +02005232 if (!valid_policy(policy))
Lennart Poetteringca94c442009-06-15 17:17:47 +02005233 return -EINVAL;
5234 }
5235
Juri Lelli794a56e2017-12-04 11:23:20 +01005236 if (attr->sched_flags & ~(SCHED_FLAG_ALL | SCHED_FLAG_SUGOV))
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01005237 return -EINVAL;
5238
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 /*
5240 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02005241 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5242 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 */
Peter Zijlstra0bb040a2014-01-15 17:15:13 +01005244 if ((p->mm && attr->sched_priority > MAX_USER_RT_PRIO-1) ||
Dario Faggiolid50dde52013-11-07 14:43:36 +01005245 (!p->mm && attr->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 return -EINVAL;
Dario Faggioliaab03e02013-11-28 11:14:43 +01005247 if ((dl_policy(policy) && !__checkparam_dl(attr)) ||
5248 (rt_policy(policy) != (attr->sched_priority != 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 return -EINVAL;
5250
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005251 /*
5252 * Allow unprivileged RT tasks to decrease priority:
5253 */
Rusty Russell961ccdd2008-06-23 13:55:38 +10005254 if (user && !capable(CAP_SYS_NICE)) {
Dario Faggiolid50dde52013-11-07 14:43:36 +01005255 if (fair_policy(policy)) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05005256 if (attr->sched_nice < task_nice(p) &&
Peter Zijlstraeaad4512014-01-16 17:54:25 +01005257 !can_nice(p, attr->sched_nice))
Dario Faggiolid50dde52013-11-07 14:43:36 +01005258 return -EPERM;
5259 }
5260
Ingo Molnare05606d2007-07-09 18:51:59 +02005261 if (rt_policy(policy)) {
Oleg Nesterova44702e82010-06-11 01:09:44 +02005262 unsigned long rlim_rtprio =
5263 task_rlimit(p, RLIMIT_RTPRIO);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005264
Ingo Molnard1ccc662017-02-01 11:46:42 +01005265 /* Can't set/change the rt policy: */
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005266 if (policy != p->policy && !rlim_rtprio)
5267 return -EPERM;
5268
Ingo Molnard1ccc662017-02-01 11:46:42 +01005269 /* Can't increase priority: */
Dario Faggiolid50dde52013-11-07 14:43:36 +01005270 if (attr->sched_priority > p->rt_priority &&
5271 attr->sched_priority > rlim_rtprio)
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005272 return -EPERM;
5273 }
Darren Hartc02aa732011-02-17 15:37:07 -08005274
Juri Lellid44753b2014-03-03 12:09:21 +01005275 /*
5276 * Can't set/change SCHED_DEADLINE policy at all for now
5277 * (safest behavior); in the future we would like to allow
5278 * unprivileged DL tasks to increase their relative deadline
5279 * or reduce their runtime (both ways reducing utilization)
5280 */
5281 if (dl_policy(policy))
5282 return -EPERM;
5283
Ingo Molnardd41f592007-07-09 18:51:59 +02005284 /*
Darren Hartc02aa732011-02-17 15:37:07 -08005285 * Treat SCHED_IDLE as nice 20. Only allow a switch to
5286 * SCHED_NORMAL if the RLIMIT_NICE would normally permit it.
Ingo Molnardd41f592007-07-09 18:51:59 +02005287 */
Viresh Kumar1da18432018-11-05 16:51:55 +05305288 if (task_has_idle_policy(p) && !idle_policy(policy)) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05005289 if (!can_nice(p, task_nice(p)))
Darren Hartc02aa732011-02-17 15:37:07 -08005290 return -EPERM;
5291 }
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005292
Ingo Molnard1ccc662017-02-01 11:46:42 +01005293 /* Can't change other user's priorities: */
David Howellsc69e8d92008-11-14 10:39:19 +11005294 if (!check_same_owner(p))
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005295 return -EPERM;
Lennart Poetteringca94c442009-06-15 17:17:47 +02005296
Ingo Molnard1ccc662017-02-01 11:46:42 +01005297 /* Normal users shall not reset the sched_reset_on_fork flag: */
Lennart Poetteringca94c442009-06-15 17:17:47 +02005298 if (p->sched_reset_on_fork && !reset_on_fork)
5299 return -EPERM;
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005302 if (user) {
Juri Lelli794a56e2017-12-04 11:23:20 +01005303 if (attr->sched_flags & SCHED_FLAG_SUGOV)
5304 return -EINVAL;
5305
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09005306 retval = security_task_setscheduler(p);
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005307 if (retval)
5308 return retval;
5309 }
5310
Patrick Bellasia509a7c2019-06-21 09:42:07 +01005311 /* Update task specific "requested" clamps */
5312 if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) {
5313 retval = uclamp_validate(p, attr);
5314 if (retval)
5315 return retval;
5316 }
5317
Juri Lelli710da3c2019-07-19 16:00:00 +02005318 if (pi)
5319 cpuset_read_lock();
5320
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 /*
Ingo Molnard1ccc662017-02-01 11:46:42 +01005322 * Make sure no PI-waiters arrive (or leave) while we are
Ingo Molnarb29739f2006-06-27 02:54:51 -07005323 * changing the priority of the task:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02005324 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005325 * To be able to change p->policy safely, the appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 * runqueue lock must be held.
5327 */
Peter Zijlstraeb580752015-07-31 21:28:18 +02005328 rq = task_rq_lock(p, &rf);
Peter Zijlstra80f5c1b2016-10-03 16:28:37 +02005329 update_rq_clock(rq);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02005330
Peter Zijlstra34f971f2010-09-22 13:53:15 +02005331 /*
Ingo Molnard1ccc662017-02-01 11:46:42 +01005332 * Changing the policy of the stop threads its a very bad idea:
Peter Zijlstra34f971f2010-09-22 13:53:15 +02005333 */
5334 if (p == rq->stop) {
Mathieu Poirier4b211f22019-07-19 15:59:54 +02005335 retval = -EINVAL;
5336 goto unlock;
Peter Zijlstra34f971f2010-09-22 13:53:15 +02005337 }
5338
Dario Faggiolia51e9192011-03-24 14:00:18 +01005339 /*
Thomas Gleixnerd6b1e912014-02-07 20:58:40 +01005340 * If not changing anything there's no need to proceed further,
5341 * but store a possible modification of reset_on_fork.
Dario Faggiolia51e9192011-03-24 14:00:18 +01005342 */
Dario Faggiolid50dde52013-11-07 14:43:36 +01005343 if (unlikely(policy == p->policy)) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05005344 if (fair_policy(policy) && attr->sched_nice != task_nice(p))
Dario Faggiolid50dde52013-11-07 14:43:36 +01005345 goto change;
5346 if (rt_policy(policy) && attr->sched_priority != p->rt_priority)
5347 goto change;
Wanpeng Li75381602014-11-26 08:44:04 +08005348 if (dl_policy(policy) && dl_param_changed(p, attr))
Dario Faggioliaab03e02013-11-28 11:14:43 +01005349 goto change;
Patrick Bellasia509a7c2019-06-21 09:42:07 +01005350 if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP)
5351 goto change;
Dario Faggiolid50dde52013-11-07 14:43:36 +01005352
Thomas Gleixnerd6b1e912014-02-07 20:58:40 +01005353 p->sched_reset_on_fork = reset_on_fork;
Mathieu Poirier4b211f22019-07-19 15:59:54 +02005354 retval = 0;
5355 goto unlock;
Dario Faggiolia51e9192011-03-24 14:00:18 +01005356 }
Dario Faggiolid50dde52013-11-07 14:43:36 +01005357change:
Dario Faggiolia51e9192011-03-24 14:00:18 +01005358
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02005359 if (user) {
Dario Faggioli332ac172013-11-07 14:43:45 +01005360#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02005361 /*
5362 * Do not allow realtime tasks into groups that have no runtime
5363 * assigned.
5364 */
5365 if (rt_bandwidth_enabled() && rt_policy(policy) &&
Mike Galbraithf4493772011-01-13 04:54:50 +01005366 task_group(p)->rt_bandwidth.rt_runtime == 0 &&
5367 !task_group_is_autogroup(task_group(p))) {
Mathieu Poirier4b211f22019-07-19 15:59:54 +02005368 retval = -EPERM;
5369 goto unlock;
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02005370 }
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02005371#endif
Dario Faggioli332ac172013-11-07 14:43:45 +01005372#ifdef CONFIG_SMP
Juri Lelli794a56e2017-12-04 11:23:20 +01005373 if (dl_bandwidth_enabled() && dl_policy(policy) &&
5374 !(attr->sched_flags & SCHED_FLAG_SUGOV)) {
Dario Faggioli332ac172013-11-07 14:43:45 +01005375 cpumask_t *span = rq->rd->span;
Dario Faggioli332ac172013-11-07 14:43:45 +01005376
5377 /*
5378 * Don't allow tasks with an affinity mask smaller than
5379 * the entire root_domain to become SCHED_DEADLINE. We
5380 * will also fail if there's no bandwidth available.
5381 */
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02005382 if (!cpumask_subset(span, p->cpus_ptr) ||
Peter Zijlstrae4099a52013-12-17 10:03:34 +01005383 rq->rd->dl_bw.bw == 0) {
Mathieu Poirier4b211f22019-07-19 15:59:54 +02005384 retval = -EPERM;
5385 goto unlock;
Dario Faggioli332ac172013-11-07 14:43:45 +01005386 }
5387 }
5388#endif
5389 }
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02005390
Ingo Molnard1ccc662017-02-01 11:46:42 +01005391 /* Re-check policy now with rq lock held: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
5393 policy = oldpolicy = -1;
Peter Zijlstraeb580752015-07-31 21:28:18 +02005394 task_rq_unlock(rq, p, &rf);
Juri Lelli710da3c2019-07-19 16:00:00 +02005395 if (pi)
5396 cpuset_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 goto recheck;
5398 }
Dario Faggioli332ac172013-11-07 14:43:45 +01005399
5400 /*
5401 * If setscheduling to SCHED_DEADLINE (or changing the parameters
5402 * of a SCHED_DEADLINE task) we need to check if enough bandwidth
5403 * is available.
5404 */
Nicolas Pitre06a76fe2017-06-21 14:22:01 -04005405 if ((dl_policy(policy) || dl_task(p)) && sched_dl_overflow(p, policy, attr)) {
Mathieu Poirier4b211f22019-07-19 15:59:54 +02005406 retval = -EBUSY;
5407 goto unlock;
Dario Faggioli332ac172013-11-07 14:43:45 +01005408 }
5409
Thomas Gleixnerc365c292014-02-07 20:58:42 +01005410 p->sched_reset_on_fork = reset_on_fork;
5411 oldprio = p->prio;
5412
Peter Zijlstradbc7f062015-06-11 14:46:38 +02005413 if (pi) {
5414 /*
5415 * Take priority boosted tasks into account. If the new
5416 * effective priority is unchanged, we just store the new
5417 * normal parameters and do not touch the scheduler class and
5418 * the runqueue. This will be done when the task deboost
5419 * itself.
5420 */
Peter Zijlstraacd58622017-03-23 15:56:11 +01005421 new_effective_prio = rt_effective_prio(p, newprio);
Peter Zijlstraff77e462016-01-18 15:27:07 +01005422 if (new_effective_prio == oldprio)
5423 queue_flags &= ~DEQUEUE_MOVE;
Thomas Gleixnerc365c292014-02-07 20:58:42 +01005424 }
5425
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005426 queued = task_on_rq_queued(p);
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005427 running = task_current(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005428 if (queued)
Peter Zijlstraff77e462016-01-18 15:27:07 +01005429 dequeue_task(rq, p, queue_flags);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005430 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04005431 put_prev_task(rq, p);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005432
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01005433 prev_class = p->sched_class;
Patrick Bellasia509a7c2019-06-21 09:42:07 +01005434
Peter Zijlstradbc7f062015-06-11 14:46:38 +02005435 __setscheduler(rq, p, attr, pi);
Patrick Bellasia509a7c2019-06-21 09:42:07 +01005436 __setscheduler_uclamp(p, attr);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005437
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005438 if (queued) {
Thomas Gleixner81a44c52014-02-07 20:58:41 +01005439 /*
5440 * We enqueue to tail when the priority of a task is
5441 * increased (user space view).
5442 */
Peter Zijlstraff77e462016-01-18 15:27:07 +01005443 if (oldprio < p->prio)
5444 queue_flags |= ENQUEUE_HEAD;
Peter Zijlstra1de64442015-09-30 17:44:13 +02005445
Peter Zijlstraff77e462016-01-18 15:27:07 +01005446 enqueue_task(rq, p, queue_flags);
Thomas Gleixner81a44c52014-02-07 20:58:41 +01005447 }
Vincent Guittota399d232016-09-12 09:47:52 +02005448 if (running)
Peter Zijlstra03b7fad2019-05-29 20:36:41 +00005449 set_next_task(rq, p);
Steven Rostedtcb469842008-01-25 21:08:22 +01005450
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005451 check_class_changed(rq, p, prev_class, oldprio);
Ingo Molnard1ccc662017-02-01 11:46:42 +01005452
5453 /* Avoid rq from going away on us: */
5454 preempt_disable();
Peter Zijlstraeb580752015-07-31 21:28:18 +02005455 task_rq_unlock(rq, p, &rf);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005456
Juri Lelli710da3c2019-07-19 16:00:00 +02005457 if (pi) {
5458 cpuset_read_unlock();
Peter Zijlstradbc7f062015-06-11 14:46:38 +02005459 rt_mutex_adjust_pi(p);
Juri Lelli710da3c2019-07-19 16:00:00 +02005460 }
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07005461
Ingo Molnard1ccc662017-02-01 11:46:42 +01005462 /* Run balance callbacks after we've adjusted the PI chain: */
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02005463 balance_callback(rq);
5464 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465
5466 return 0;
Mathieu Poirier4b211f22019-07-19 15:59:54 +02005467
5468unlock:
5469 task_rq_unlock(rq, p, &rf);
Juri Lelli710da3c2019-07-19 16:00:00 +02005470 if (pi)
5471 cpuset_read_unlock();
Mathieu Poirier4b211f22019-07-19 15:59:54 +02005472 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473}
Rusty Russell961ccdd2008-06-23 13:55:38 +10005474
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01005475static int _sched_setscheduler(struct task_struct *p, int policy,
5476 const struct sched_param *param, bool check)
5477{
5478 struct sched_attr attr = {
5479 .sched_policy = policy,
5480 .sched_priority = param->sched_priority,
5481 .sched_nice = PRIO_TO_NICE(p->static_prio),
5482 };
5483
Steven Rostedtc13db6b2014-07-23 11:28:26 -04005484 /* Fixup the legacy SCHED_RESET_ON_FORK hack. */
5485 if ((policy != SETPARAM_POLICY) && (policy & SCHED_RESET_ON_FORK)) {
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01005486 attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
5487 policy &= ~SCHED_RESET_ON_FORK;
5488 attr.sched_policy = policy;
5489 }
5490
Peter Zijlstradbc7f062015-06-11 14:46:38 +02005491 return __sched_setscheduler(p, &attr, check, true);
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01005492}
Rusty Russell961ccdd2008-06-23 13:55:38 +10005493/**
5494 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
5495 * @p: the task in question.
5496 * @policy: new policy.
5497 * @param: structure containing the new RT priority.
5498 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02005499 * Return: 0 on success. An error code otherwise.
5500 *
Rusty Russell961ccdd2008-06-23 13:55:38 +10005501 * NOTE that the task may be already dead.
5502 */
5503int sched_setscheduler(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07005504 const struct sched_param *param)
Rusty Russell961ccdd2008-06-23 13:55:38 +10005505{
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01005506 return _sched_setscheduler(p, policy, param, true);
Rusty Russell961ccdd2008-06-23 13:55:38 +10005507}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508EXPORT_SYMBOL_GPL(sched_setscheduler);
5509
Dario Faggiolid50dde52013-11-07 14:43:36 +01005510int sched_setattr(struct task_struct *p, const struct sched_attr *attr)
5511{
Peter Zijlstradbc7f062015-06-11 14:46:38 +02005512 return __sched_setscheduler(p, attr, true, true);
Dario Faggiolid50dde52013-11-07 14:43:36 +01005513}
5514EXPORT_SYMBOL_GPL(sched_setattr);
5515
Juri Lelli794a56e2017-12-04 11:23:20 +01005516int sched_setattr_nocheck(struct task_struct *p, const struct sched_attr *attr)
5517{
5518 return __sched_setscheduler(p, attr, false, true);
5519}
5520
Rusty Russell961ccdd2008-06-23 13:55:38 +10005521/**
5522 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
5523 * @p: the task in question.
5524 * @policy: new policy.
5525 * @param: structure containing the new RT priority.
5526 *
5527 * Just like sched_setscheduler, only don't bother checking if the
5528 * current context has permission. For example, this is needed in
5529 * stop_machine(): we create temporary high priority worker threads,
5530 * but our caller might not have that capability.
Yacine Belkadie69f6182013-07-12 20:45:47 +02005531 *
5532 * Return: 0 on success. An error code otherwise.
Rusty Russell961ccdd2008-06-23 13:55:38 +10005533 */
5534int sched_setscheduler_nocheck(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07005535 const struct sched_param *param)
Rusty Russell961ccdd2008-06-23 13:55:38 +10005536{
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01005537 return _sched_setscheduler(p, policy, param, false);
Rusty Russell961ccdd2008-06-23 13:55:38 +10005538}
Davidlohr Bueso84778472015-09-02 01:28:44 -07005539EXPORT_SYMBOL_GPL(sched_setscheduler_nocheck);
Rusty Russell961ccdd2008-06-23 13:55:38 +10005540
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005541static int
5542do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 struct sched_param lparam;
5545 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005546 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547
5548 if (!param || pid < 0)
5549 return -EINVAL;
5550 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
5551 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005552
5553 rcu_read_lock();
5554 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 p = find_process_by_pid(pid);
Juri Lelli710da3c2019-07-19 16:00:00 +02005556 if (likely(p))
5557 get_task_struct(p);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005558 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07005559
Juri Lelli710da3c2019-07-19 16:00:00 +02005560 if (likely(p)) {
5561 retval = sched_setscheduler(p, policy, &lparam);
5562 put_task_struct(p);
5563 }
5564
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 return retval;
5566}
5567
Dario Faggiolid50dde52013-11-07 14:43:36 +01005568/*
5569 * Mimics kernel/events/core.c perf_copy_attr().
5570 */
Ingo Molnard1ccc662017-02-01 11:46:42 +01005571static int sched_copy_attr(struct sched_attr __user *uattr, struct sched_attr *attr)
Dario Faggiolid50dde52013-11-07 14:43:36 +01005572{
5573 u32 size;
5574 int ret;
5575
Ingo Molnard1ccc662017-02-01 11:46:42 +01005576 /* Zero the full structure, so that a short copy will be nice: */
Dario Faggiolid50dde52013-11-07 14:43:36 +01005577 memset(attr, 0, sizeof(*attr));
5578
5579 ret = get_user(size, &uattr->size);
5580 if (ret)
5581 return ret;
5582
Ingo Molnard1ccc662017-02-01 11:46:42 +01005583 /* ABI compatibility quirk: */
5584 if (!size)
Dario Faggiolid50dde52013-11-07 14:43:36 +01005585 size = SCHED_ATTR_SIZE_VER0;
Aleksa Saraidff3a852019-10-01 11:10:54 +10005586 if (size < SCHED_ATTR_SIZE_VER0 || size > PAGE_SIZE)
Dario Faggiolid50dde52013-11-07 14:43:36 +01005587 goto err_size;
5588
Aleksa Saraidff3a852019-10-01 11:10:54 +10005589 ret = copy_struct_from_user(attr, sizeof(*attr), uattr, size);
5590 if (ret) {
5591 if (ret == -E2BIG)
5592 goto err_size;
5593 return ret;
Dario Faggiolid50dde52013-11-07 14:43:36 +01005594 }
5595
Patrick Bellasia509a7c2019-06-21 09:42:07 +01005596 if ((attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) &&
5597 size < SCHED_ATTR_SIZE_VER1)
5598 return -EINVAL;
5599
Dario Faggiolid50dde52013-11-07 14:43:36 +01005600 /*
Ingo Molnard1ccc662017-02-01 11:46:42 +01005601 * XXX: Do we want to be lenient like existing syscalls; or do we want
Dario Faggiolid50dde52013-11-07 14:43:36 +01005602 * to be strict and return an error on out-of-bounds values?
5603 */
Dongsheng Yang75e45d52014-02-11 15:34:50 +08005604 attr->sched_nice = clamp(attr->sched_nice, MIN_NICE, MAX_NICE);
Dario Faggiolid50dde52013-11-07 14:43:36 +01005605
Michael Kerriske78c7bc2014-05-09 16:54:28 +02005606 return 0;
Dario Faggiolid50dde52013-11-07 14:43:36 +01005607
5608err_size:
5609 put_user(sizeof(*attr), &uattr->size);
Michael Kerriske78c7bc2014-05-09 16:54:28 +02005610 return -E2BIG;
Dario Faggiolid50dde52013-11-07 14:43:36 +01005611}
5612
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613/**
5614 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5615 * @pid: the pid in question.
5616 * @policy: new policy.
5617 * @param: structure containing the new RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02005618 *
5619 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 */
Ingo Molnard1ccc662017-02-01 11:46:42 +01005621SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622{
Jason Baronc21761f2006-01-18 17:43:03 -08005623 if (policy < 0)
5624 return -EINVAL;
5625
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 return do_sched_setscheduler(pid, policy, param);
5627}
5628
5629/**
5630 * sys_sched_setparam - set/change the RT priority of a thread
5631 * @pid: the pid in question.
5632 * @param: structure containing the new RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02005633 *
5634 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005636SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637{
Steven Rostedtc13db6b2014-07-23 11:28:26 -04005638 return do_sched_setscheduler(pid, SETPARAM_POLICY, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639}
5640
5641/**
Dario Faggiolid50dde52013-11-07 14:43:36 +01005642 * sys_sched_setattr - same as above, but with extended sched_attr
5643 * @pid: the pid in question.
Juri Lelli5778fcc2014-01-14 16:10:39 +01005644 * @uattr: structure containing the extended parameters.
Masanari Iidadb66d752014-04-18 01:59:15 +09005645 * @flags: for future extension.
Dario Faggiolid50dde52013-11-07 14:43:36 +01005646 */
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01005647SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr,
5648 unsigned int, flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01005649{
5650 struct sched_attr attr;
5651 struct task_struct *p;
5652 int retval;
5653
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01005654 if (!uattr || pid < 0 || flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01005655 return -EINVAL;
5656
Michael Kerrisk143cf232014-05-09 16:54:15 +02005657 retval = sched_copy_attr(uattr, &attr);
5658 if (retval)
5659 return retval;
Dario Faggiolid50dde52013-11-07 14:43:36 +01005660
Richard Weinbergerb14ed2c2014-06-02 22:38:34 +02005661 if ((int)attr.sched_policy < 0)
Peter Zijlstradbdb2272014-05-09 10:49:03 +02005662 return -EINVAL;
Patrick Bellasi1d6362f2019-06-21 09:42:06 +01005663 if (attr.sched_flags & SCHED_FLAG_KEEP_POLICY)
5664 attr.sched_policy = SETPARAM_POLICY;
Dario Faggiolid50dde52013-11-07 14:43:36 +01005665
5666 rcu_read_lock();
5667 retval = -ESRCH;
5668 p = find_process_by_pid(pid);
Patrick Bellasia509a7c2019-06-21 09:42:07 +01005669 if (likely(p))
5670 get_task_struct(p);
Dario Faggiolid50dde52013-11-07 14:43:36 +01005671 rcu_read_unlock();
5672
Patrick Bellasia509a7c2019-06-21 09:42:07 +01005673 if (likely(p)) {
5674 retval = sched_setattr(p, &attr);
5675 put_task_struct(p);
5676 }
5677
Dario Faggiolid50dde52013-11-07 14:43:36 +01005678 return retval;
5679}
5680
5681/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5683 * @pid: the pid in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02005684 *
5685 * Return: On success, the policy of the thread. Otherwise, a negative error
5686 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005688SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005690 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005691 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692
5693 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005694 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695
5696 retval = -ESRCH;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00005697 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 p = find_process_by_pid(pid);
5699 if (p) {
5700 retval = security_task_getscheduler(p);
5701 if (!retval)
Lennart Poetteringca94c442009-06-15 17:17:47 +02005702 retval = p->policy
5703 | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 }
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00005705 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 return retval;
5707}
5708
5709/**
Lennart Poetteringca94c442009-06-15 17:17:47 +02005710 * sys_sched_getparam - get the RT priority of a thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 * @pid: the pid in question.
5712 * @param: structure containing the RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02005713 *
5714 * Return: On success, 0 and the RT priority is in @param. Otherwise, an error
5715 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005717SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718{
Peter Zijlstrace5f7f82014-05-12 22:50:34 +02005719 struct sched_param lp = { .sched_priority = 0 };
Ingo Molnar36c8b582006-07-03 00:25:41 -07005720 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005721 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722
5723 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005724 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00005726 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 p = find_process_by_pid(pid);
5728 retval = -ESRCH;
5729 if (!p)
5730 goto out_unlock;
5731
5732 retval = security_task_getscheduler(p);
5733 if (retval)
5734 goto out_unlock;
5735
Peter Zijlstrace5f7f82014-05-12 22:50:34 +02005736 if (task_has_rt_policy(p))
5737 lp.sched_priority = p->rt_priority;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00005738 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739
5740 /*
5741 * This one might sleep, we cannot do it with a spinlock held ...
5742 */
5743 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
5744
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 return retval;
5746
5747out_unlock:
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00005748 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 return retval;
5750}
5751
Ingo Molnar12512012019-09-04 09:55:32 +02005752/*
5753 * Copy the kernel size attribute structure (which might be larger
5754 * than what user-space knows about) to user-space.
5755 *
5756 * Note that all cases are valid: user-space buffer can be larger or
5757 * smaller than the kernel-space buffer. The usual case is that both
5758 * have the same size.
5759 */
5760static int
5761sched_attr_copy_to_user(struct sched_attr __user *uattr,
5762 struct sched_attr *kattr,
5763 unsigned int usize)
Dario Faggiolid50dde52013-11-07 14:43:36 +01005764{
Ingo Molnar12512012019-09-04 09:55:32 +02005765 unsigned int ksize = sizeof(*kattr);
Dario Faggiolid50dde52013-11-07 14:43:36 +01005766
Linus Torvalds96d4f262019-01-03 18:57:57 -08005767 if (!access_ok(uattr, usize))
Dario Faggiolid50dde52013-11-07 14:43:36 +01005768 return -EFAULT;
5769
5770 /*
Ingo Molnar12512012019-09-04 09:55:32 +02005771 * sched_getattr() ABI forwards and backwards compatibility:
5772 *
5773 * If usize == ksize then we just copy everything to user-space and all is good.
5774 *
5775 * If usize < ksize then we only copy as much as user-space has space for,
5776 * this keeps ABI compatibility as well. We skip the rest.
5777 *
5778 * If usize > ksize then user-space is using a newer version of the ABI,
5779 * which part the kernel doesn't know about. Just ignore it - tooling can
5780 * detect the kernel's knowledge of attributes from the attr->size value
5781 * which is set to ksize in this case.
Dario Faggiolid50dde52013-11-07 14:43:36 +01005782 */
Ingo Molnar12512012019-09-04 09:55:32 +02005783 kattr->size = min(usize, ksize);
Dario Faggiolid50dde52013-11-07 14:43:36 +01005784
Ingo Molnar12512012019-09-04 09:55:32 +02005785 if (copy_to_user(uattr, kattr, kattr->size))
Dario Faggiolid50dde52013-11-07 14:43:36 +01005786 return -EFAULT;
5787
Michael Kerrisk22400672014-05-09 16:54:33 +02005788 return 0;
Dario Faggiolid50dde52013-11-07 14:43:36 +01005789}
5790
5791/**
Dario Faggioliaab03e02013-11-28 11:14:43 +01005792 * sys_sched_getattr - similar to sched_getparam, but with sched_attr
Dario Faggiolid50dde52013-11-07 14:43:36 +01005793 * @pid: the pid in question.
Juri Lelli5778fcc2014-01-14 16:10:39 +01005794 * @uattr: structure containing the extended parameters.
Aleksa Saraidff3a852019-10-01 11:10:54 +10005795 * @usize: sizeof(attr) for fwd/bwd comp.
Masanari Iidadb66d752014-04-18 01:59:15 +09005796 * @flags: for future extension.
Dario Faggiolid50dde52013-11-07 14:43:36 +01005797 */
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01005798SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr,
Ingo Molnar12512012019-09-04 09:55:32 +02005799 unsigned int, usize, unsigned int, flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01005800{
Ingo Molnar12512012019-09-04 09:55:32 +02005801 struct sched_attr kattr = { };
Dario Faggiolid50dde52013-11-07 14:43:36 +01005802 struct task_struct *p;
5803 int retval;
5804
Ingo Molnar12512012019-09-04 09:55:32 +02005805 if (!uattr || pid < 0 || usize > PAGE_SIZE ||
5806 usize < SCHED_ATTR_SIZE_VER0 || flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01005807 return -EINVAL;
5808
5809 rcu_read_lock();
5810 p = find_process_by_pid(pid);
5811 retval = -ESRCH;
5812 if (!p)
5813 goto out_unlock;
5814
5815 retval = security_task_getscheduler(p);
5816 if (retval)
5817 goto out_unlock;
5818
Ingo Molnar12512012019-09-04 09:55:32 +02005819 kattr.sched_policy = p->policy;
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01005820 if (p->sched_reset_on_fork)
Ingo Molnar12512012019-09-04 09:55:32 +02005821 kattr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
Dario Faggioliaab03e02013-11-28 11:14:43 +01005822 if (task_has_dl_policy(p))
Ingo Molnar12512012019-09-04 09:55:32 +02005823 __getparam_dl(p, &kattr);
Dario Faggioliaab03e02013-11-28 11:14:43 +01005824 else if (task_has_rt_policy(p))
Ingo Molnar12512012019-09-04 09:55:32 +02005825 kattr.sched_priority = p->rt_priority;
Dario Faggiolid50dde52013-11-07 14:43:36 +01005826 else
Ingo Molnar12512012019-09-04 09:55:32 +02005827 kattr.sched_nice = task_nice(p);
Dario Faggiolid50dde52013-11-07 14:43:36 +01005828
Patrick Bellasia509a7c2019-06-21 09:42:07 +01005829#ifdef CONFIG_UCLAMP_TASK
Qais Yousef13685c42020-07-16 12:03:45 +01005830 /*
5831 * This could race with another potential updater, but this is fine
5832 * because it'll correctly read the old or the new value. We don't need
5833 * to guarantee who wins the race as long as it doesn't return garbage.
5834 */
Ingo Molnar12512012019-09-04 09:55:32 +02005835 kattr.sched_util_min = p->uclamp_req[UCLAMP_MIN].value;
5836 kattr.sched_util_max = p->uclamp_req[UCLAMP_MAX].value;
Patrick Bellasia509a7c2019-06-21 09:42:07 +01005837#endif
5838
Dario Faggiolid50dde52013-11-07 14:43:36 +01005839 rcu_read_unlock();
5840
Ingo Molnar12512012019-09-04 09:55:32 +02005841 return sched_attr_copy_to_user(uattr, &kattr, usize);
Dario Faggiolid50dde52013-11-07 14:43:36 +01005842
5843out_unlock:
5844 rcu_read_unlock();
5845 return retval;
5846}
5847
Rusty Russell96f874e22008-11-25 02:35:14 +10305848long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305850 cpumask_var_t cpus_allowed, new_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005851 struct task_struct *p;
5852 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005854 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855
5856 p = find_process_by_pid(pid);
5857 if (!p) {
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005858 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 return -ESRCH;
5860 }
5861
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005862 /* Prevent p going away */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 get_task_struct(p);
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005864 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865
Tejun Heo14a40ff2013-03-19 13:45:20 -07005866 if (p->flags & PF_NO_SETAFFINITY) {
5867 retval = -EINVAL;
5868 goto out_put_task;
5869 }
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305870 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
5871 retval = -ENOMEM;
5872 goto out_put_task;
5873 }
5874 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
5875 retval = -ENOMEM;
5876 goto out_free_cpus_allowed;
5877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 retval = -EPERM;
Eric W. Biederman4c44aaa2012-07-26 05:05:21 -07005879 if (!check_same_owner(p)) {
5880 rcu_read_lock();
5881 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
5882 rcu_read_unlock();
Kirill Tkhai16303ab2014-09-22 22:36:30 +04005883 goto out_free_new_mask;
Eric W. Biederman4c44aaa2012-07-26 05:05:21 -07005884 }
5885 rcu_read_unlock();
5886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09005888 retval = security_task_setscheduler(p);
David Quigleye7834f82006-06-23 02:03:59 -07005889 if (retval)
Kirill Tkhai16303ab2014-09-22 22:36:30 +04005890 goto out_free_new_mask;
David Quigleye7834f82006-06-23 02:03:59 -07005891
Peter Zijlstrae4099a52013-12-17 10:03:34 +01005892
5893 cpuset_cpus_allowed(p, cpus_allowed);
5894 cpumask_and(new_mask, in_mask, cpus_allowed);
5895
Dario Faggioli332ac172013-11-07 14:43:45 +01005896 /*
5897 * Since bandwidth control happens on root_domain basis,
5898 * if admission test is enabled, we only admit -deadline
5899 * tasks allowed to run on all the CPUs in the task's
5900 * root_domain.
5901 */
5902#ifdef CONFIG_SMP
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04005903 if (task_has_dl_policy(p) && dl_bandwidth_enabled()) {
5904 rcu_read_lock();
5905 if (!cpumask_subset(task_rq(p)->rd->span, new_mask)) {
Dario Faggioli332ac172013-11-07 14:43:45 +01005906 retval = -EBUSY;
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04005907 rcu_read_unlock();
Kirill Tkhai16303ab2014-09-22 22:36:30 +04005908 goto out_free_new_mask;
Dario Faggioli332ac172013-11-07 14:43:45 +01005909 }
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04005910 rcu_read_unlock();
Dario Faggioli332ac172013-11-07 14:43:45 +01005911 }
5912#endif
Peter Zijlstra49246272010-10-17 21:46:10 +02005913again:
Peter Zijlstra25834c72015-05-15 17:43:34 +02005914 retval = __set_cpus_allowed_ptr(p, new_mask, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915
Paul Menage8707d8b2007-10-18 23:40:22 -07005916 if (!retval) {
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305917 cpuset_cpus_allowed(p, cpus_allowed);
5918 if (!cpumask_subset(new_mask, cpus_allowed)) {
Paul Menage8707d8b2007-10-18 23:40:22 -07005919 /*
5920 * We must have raced with a concurrent cpuset
5921 * update. Just reset the cpus_allowed to the
5922 * cpuset's cpus_allowed
5923 */
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305924 cpumask_copy(new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005925 goto again;
5926 }
5927 }
Kirill Tkhai16303ab2014-09-22 22:36:30 +04005928out_free_new_mask:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305929 free_cpumask_var(new_mask);
5930out_free_cpus_allowed:
5931 free_cpumask_var(cpus_allowed);
5932out_put_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 return retval;
5935}
5936
5937static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
Rusty Russell96f874e22008-11-25 02:35:14 +10305938 struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939{
Rusty Russell96f874e22008-11-25 02:35:14 +10305940 if (len < cpumask_size())
5941 cpumask_clear(new_mask);
5942 else if (len > cpumask_size())
5943 len = cpumask_size();
5944
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5946}
5947
5948/**
Ingo Molnard1ccc662017-02-01 11:46:42 +01005949 * sys_sched_setaffinity - set the CPU affinity of a process
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 * @pid: pid of the process
5951 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
Ingo Molnard1ccc662017-02-01 11:46:42 +01005952 * @user_mask_ptr: user-space pointer to the new CPU mask
Yacine Belkadie69f6182013-07-12 20:45:47 +02005953 *
5954 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005956SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
5957 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305959 cpumask_var_t new_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 int retval;
5961
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305962 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
5963 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305965 retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
5966 if (retval == 0)
5967 retval = sched_setaffinity(pid, new_mask);
5968 free_cpumask_var(new_mask);
5969 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970}
5971
Rusty Russell96f874e22008-11-25 02:35:14 +10305972long sched_getaffinity(pid_t pid, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005974 struct task_struct *p;
Thomas Gleixner31605682009-12-08 20:24:16 +00005975 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005978 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979
5980 retval = -ESRCH;
5981 p = find_process_by_pid(pid);
5982 if (!p)
5983 goto out_unlock;
5984
David Quigleye7834f82006-06-23 02:03:59 -07005985 retval = security_task_getscheduler(p);
5986 if (retval)
5987 goto out_unlock;
5988
Peter Zijlstra013fdb82011-04-05 17:23:45 +02005989 raw_spin_lock_irqsave(&p->pi_lock, flags);
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02005990 cpumask_and(mask, &p->cpus_mask, cpu_active_mask);
Peter Zijlstra013fdb82011-04-05 17:23:45 +02005991 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992
5993out_unlock:
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005994 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995
Ulrich Drepper9531b622007-08-09 11:16:46 +02005996 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997}
5998
5999/**
Ingo Molnard1ccc662017-02-01 11:46:42 +01006000 * sys_sched_getaffinity - get the CPU affinity of a process
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 * @pid: pid of the process
6002 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
Ingo Molnard1ccc662017-02-01 11:46:42 +01006003 * @user_mask_ptr: user-space pointer to hold the current CPU mask
Yacine Belkadie69f6182013-07-12 20:45:47 +02006004 *
Zev Weiss599b4842016-06-26 16:13:23 -05006005 * Return: size of CPU mask copied to user_mask_ptr on success. An
6006 * error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01006008SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
6009 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010{
6011 int ret;
Rusty Russellf17c8602008-11-25 02:35:11 +10306012 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013
Anton Blanchard84fba5e2010-04-06 17:02:19 +10006014 if ((len * BITS_PER_BYTE) < nr_cpu_ids)
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09006015 return -EINVAL;
6016 if (len & (sizeof(unsigned long)-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 return -EINVAL;
6018
Rusty Russellf17c8602008-11-25 02:35:11 +10306019 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
6020 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021
Rusty Russellf17c8602008-11-25 02:35:11 +10306022 ret = sched_getaffinity(pid, mask);
6023 if (ret == 0) {
Alexey Dobriyan4de373a2018-02-06 15:39:37 -08006024 unsigned int retlen = min(len, cpumask_size());
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09006025
6026 if (copy_to_user(user_mask_ptr, mask, retlen))
Rusty Russellf17c8602008-11-25 02:35:11 +10306027 ret = -EFAULT;
6028 else
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09006029 ret = retlen;
Rusty Russellf17c8602008-11-25 02:35:11 +10306030 }
6031 free_cpumask_var(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032
Rusty Russellf17c8602008-11-25 02:35:11 +10306033 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034}
6035
6036/**
6037 * sys_sched_yield - yield the current processor to other threads.
6038 *
Ingo Molnardd41f592007-07-09 18:51:59 +02006039 * This function yields the current CPU to other tasks. If there are no
6040 * other threads running on this CPU then this function will return.
Yacine Belkadie69f6182013-07-12 20:45:47 +02006041 *
6042 * Return: 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 */
Dominik Brodowski7d4dd4f2018-03-14 22:40:35 +01006044static void do_sched_yield(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045{
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006046 struct rq_flags rf;
6047 struct rq *rq;
6048
Johannes Weiner246b3b32018-10-26 15:06:23 -07006049 rq = this_rq_lock_irq(&rf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050
Josh Poimboeufae928822016-06-17 12:43:24 -05006051 schedstat_inc(rq->yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02006052 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053
6054 /*
6055 * Since we are going to call schedule() anyway, there's
6056 * no need to preempt or enable interrupts:
6057 */
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006058 preempt_disable();
6059 rq_unlock(rq, &rf);
Thomas Gleixnerba74c142011-03-21 13:32:17 +01006060 sched_preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061
6062 schedule();
Dominik Brodowski7d4dd4f2018-03-14 22:40:35 +01006063}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064
Dominik Brodowski7d4dd4f2018-03-14 22:40:35 +01006065SYSCALL_DEFINE0(sched_yield)
6066{
6067 do_sched_yield();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 return 0;
6069}
6070
Thomas Gleixnerc1a280b2019-07-26 23:19:37 +02006071#ifndef CONFIG_PREEMPTION
Herbert Xu02b67cc32008-01-25 21:08:28 +01006072int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073{
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +03006074 if (should_resched(0)) {
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01006075 preempt_schedule_common();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 return 1;
6077 }
Paul E. McKenneyf79c3ad2016-11-30 06:24:30 -08006078 rcu_all_qs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 return 0;
6080}
Herbert Xu02b67cc32008-01-25 21:08:28 +01006081EXPORT_SYMBOL(_cond_resched);
Peter Zijlstra35a773a2016-09-19 12:57:53 +02006082#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083
6084/*
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02006085 * __cond_resched_lock() - if a reschedule is pending, drop the given lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 * call schedule, and on return reacquire the lock.
6087 *
Thomas Gleixnerc1a280b2019-07-26 23:19:37 +02006088 * This works OK both with and without CONFIG_PREEMPTION. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 * operations here to prevent schedule() from being called twice (once via
6090 * spin_unlock(), once by hand).
6091 */
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02006092int __cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093{
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +03006094 int resched = should_resched(PREEMPT_LOCK_OFFSET);
Jan Kara6df3cec2005-06-13 15:52:32 -07006095 int ret = 0;
6096
Peter Zijlstraf607c662009-07-20 19:16:29 +02006097 lockdep_assert_held(lock);
6098
Paul E. McKenney4a81e832014-06-20 16:49:01 -07006099 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 spin_unlock(lock);
Peter Zijlstrad86ee482009-07-10 14:57:57 +02006101 if (resched)
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01006102 preempt_schedule_common();
Nick Piggin95c354f2008-01-30 13:31:20 +01006103 else
6104 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07006105 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 }
Jan Kara6df3cec2005-06-13 15:52:32 -07006108 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02006110EXPORT_SYMBOL(__cond_resched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112/**
6113 * yield - yield the current processor to other threads.
6114 *
Peter Zijlstra8e3fabf2012-03-06 18:54:26 +01006115 * Do not ever use this function, there's a 99% chance you're doing it wrong.
6116 *
6117 * The scheduler is at all times free to pick the calling task as the most
6118 * eligible task to run, if removing the yield() call from your code breaks
6119 * it, its already broken.
6120 *
6121 * Typical broken usage is:
6122 *
6123 * while (!event)
Ingo Molnard1ccc662017-02-01 11:46:42 +01006124 * yield();
Peter Zijlstra8e3fabf2012-03-06 18:54:26 +01006125 *
6126 * where one assumes that yield() will let 'the other' process run that will
6127 * make event true. If the current task is a SCHED_FIFO task that will never
6128 * happen. Never use yield() as a progress guarantee!!
6129 *
6130 * If you want to use yield() to wait for something, use wait_event().
6131 * If you want to use yield() to be 'nice' for others, use cond_resched().
6132 * If you still want to use yield(), do not!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 */
6134void __sched yield(void)
6135{
6136 set_current_state(TASK_RUNNING);
Dominik Brodowski7d4dd4f2018-03-14 22:40:35 +01006137 do_sched_yield();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139EXPORT_SYMBOL(yield);
6140
Mike Galbraithd95f4122011-02-01 09:50:51 -05006141/**
6142 * yield_to - yield the current processor to another thread in
6143 * your thread group, or accelerate that thread toward the
6144 * processor it's on.
Randy Dunlap16addf92011-03-18 09:34:53 -07006145 * @p: target task
6146 * @preempt: whether task preemption is allowed or not
Mike Galbraithd95f4122011-02-01 09:50:51 -05006147 *
6148 * It's the caller's job to ensure that the target task struct
6149 * can't go away on us before we can do any checks.
6150 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02006151 * Return:
Peter Zijlstra7b270f62013-01-22 13:09:13 +05306152 * true (>0) if we indeed boosted the target task.
6153 * false (0) if we failed to boost the target.
6154 * -ESRCH if there's no task to yield to.
Mike Galbraithd95f4122011-02-01 09:50:51 -05006155 */
Dan Carpenterfa933842014-05-23 13:20:42 +03006156int __sched yield_to(struct task_struct *p, bool preempt)
Mike Galbraithd95f4122011-02-01 09:50:51 -05006157{
6158 struct task_struct *curr = current;
6159 struct rq *rq, *p_rq;
6160 unsigned long flags;
Dan Carpenterc3c18642013-02-05 14:37:51 +03006161 int yielded = 0;
Mike Galbraithd95f4122011-02-01 09:50:51 -05006162
6163 local_irq_save(flags);
6164 rq = this_rq();
6165
6166again:
6167 p_rq = task_rq(p);
Peter Zijlstra7b270f62013-01-22 13:09:13 +05306168 /*
6169 * If we're the only runnable task on the rq and target rq also
6170 * has only one task, there's absolutely no point in yielding.
6171 */
6172 if (rq->nr_running == 1 && p_rq->nr_running == 1) {
6173 yielded = -ESRCH;
6174 goto out_irq;
6175 }
6176
Mike Galbraithd95f4122011-02-01 09:50:51 -05006177 double_rq_lock(rq, p_rq);
Shigeru Yoshida39e24d8f2013-11-23 18:38:01 +09006178 if (task_rq(p) != p_rq) {
Mike Galbraithd95f4122011-02-01 09:50:51 -05006179 double_rq_unlock(rq, p_rq);
6180 goto again;
6181 }
6182
6183 if (!curr->sched_class->yield_to_task)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05306184 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05006185
6186 if (curr->sched_class != p->sched_class)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05306187 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05006188
6189 if (task_running(p_rq, p) || p->state)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05306190 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05006191
Dietmar Eggemann0900acf2020-06-03 10:03:02 +02006192 yielded = curr->sched_class->yield_to_task(rq, p);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08006193 if (yielded) {
Josh Poimboeufae928822016-06-17 12:43:24 -05006194 schedstat_inc(rq->yld_count);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08006195 /*
6196 * Make p's CPU reschedule; pick_next_entity takes care of
6197 * fairness.
6198 */
6199 if (preempt && rq != p_rq)
Kirill Tkhai88751252014-06-29 00:03:57 +04006200 resched_curr(p_rq);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08006201 }
Mike Galbraithd95f4122011-02-01 09:50:51 -05006202
Peter Zijlstra7b270f62013-01-22 13:09:13 +05306203out_unlock:
Mike Galbraithd95f4122011-02-01 09:50:51 -05006204 double_rq_unlock(rq, p_rq);
Peter Zijlstra7b270f62013-01-22 13:09:13 +05306205out_irq:
Mike Galbraithd95f4122011-02-01 09:50:51 -05006206 local_irq_restore(flags);
6207
Peter Zijlstra7b270f62013-01-22 13:09:13 +05306208 if (yielded > 0)
Mike Galbraithd95f4122011-02-01 09:50:51 -05006209 schedule();
6210
6211 return yielded;
6212}
6213EXPORT_SYMBOL_GPL(yield_to);
6214
Tejun Heo10ab5642016-10-28 12:58:10 -04006215int io_schedule_prepare(void)
6216{
6217 int old_iowait = current->in_iowait;
6218
6219 current->in_iowait = 1;
6220 blk_schedule_flush_plug(current);
6221
6222 return old_iowait;
6223}
6224
6225void io_schedule_finish(int token)
6226{
6227 current->in_iowait = token;
6228}
6229
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01006231 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 * that process accounting knows that this is a task in IO wait state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234long __sched io_schedule_timeout(long timeout)
6235{
Tejun Heo10ab5642016-10-28 12:58:10 -04006236 int token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 long ret;
6238
Tejun Heo10ab5642016-10-28 12:58:10 -04006239 token = io_schedule_prepare();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240 ret = schedule_timeout(timeout);
Tejun Heo10ab5642016-10-28 12:58:10 -04006241 io_schedule_finish(token);
NeilBrown9cff8ad2015-02-13 15:49:17 +11006242
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 return ret;
6244}
NeilBrown9cff8ad2015-02-13 15:49:17 +11006245EXPORT_SYMBOL(io_schedule_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246
Gao Xiange3b929b2019-06-03 17:13:38 +08006247void __sched io_schedule(void)
Tejun Heo10ab5642016-10-28 12:58:10 -04006248{
6249 int token;
6250
6251 token = io_schedule_prepare();
6252 schedule();
6253 io_schedule_finish(token);
6254}
6255EXPORT_SYMBOL(io_schedule);
6256
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257/**
6258 * sys_sched_get_priority_max - return maximum RT priority.
6259 * @policy: scheduling class.
6260 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02006261 * Return: On success, this syscall returns the maximum
6262 * rt_priority that can be used by a given scheduling class.
6263 * On failure, a negative error code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01006265SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266{
6267 int ret = -EINVAL;
6268
6269 switch (policy) {
6270 case SCHED_FIFO:
6271 case SCHED_RR:
6272 ret = MAX_USER_RT_PRIO-1;
6273 break;
Dario Faggioliaab03e02013-11-28 11:14:43 +01006274 case SCHED_DEADLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08006276 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02006277 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278 ret = 0;
6279 break;
6280 }
6281 return ret;
6282}
6283
6284/**
6285 * sys_sched_get_priority_min - return minimum RT priority.
6286 * @policy: scheduling class.
6287 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02006288 * Return: On success, this syscall returns the minimum
6289 * rt_priority that can be used by a given scheduling class.
6290 * On failure, a negative error code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01006292SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293{
6294 int ret = -EINVAL;
6295
6296 switch (policy) {
6297 case SCHED_FIFO:
6298 case SCHED_RR:
6299 ret = 1;
6300 break;
Dario Faggioliaab03e02013-11-28 11:14:43 +01006301 case SCHED_DEADLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08006303 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02006304 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 ret = 0;
6306 }
6307 return ret;
6308}
6309
Al Viroabca5fc2017-09-19 18:17:46 -04006310static int sched_rr_get_interval(pid_t pid, struct timespec64 *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311{
Ingo Molnar36c8b582006-07-03 00:25:41 -07006312 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02006313 unsigned int time_slice;
Peter Zijlstraeb580752015-07-31 21:28:18 +02006314 struct rq_flags rf;
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01006315 struct rq *rq;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006316 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317
6318 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02006319 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320
6321 retval = -ESRCH;
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00006322 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323 p = find_process_by_pid(pid);
6324 if (!p)
6325 goto out_unlock;
6326
6327 retval = security_task_getscheduler(p);
6328 if (retval)
6329 goto out_unlock;
6330
Peter Zijlstraeb580752015-07-31 21:28:18 +02006331 rq = task_rq_lock(p, &rf);
Peter Zijlstraa57beec2014-01-27 11:54:13 +01006332 time_slice = 0;
6333 if (p->sched_class->get_rr_interval)
6334 time_slice = p->sched_class->get_rr_interval(rq, p);
Peter Zijlstraeb580752015-07-31 21:28:18 +02006335 task_rq_unlock(rq, p, &rf);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02006336
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00006337 rcu_read_unlock();
Al Viroabca5fc2017-09-19 18:17:46 -04006338 jiffies_to_timespec64(time_slice, t);
6339 return 0;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006340
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341out_unlock:
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00006342 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 return retval;
6344}
6345
Randy Dunlap2064a5a2017-12-03 13:19:00 -08006346/**
6347 * sys_sched_rr_get_interval - return the default timeslice of a process.
6348 * @pid: pid of the process.
6349 * @interval: userspace pointer to the timeslice value.
6350 *
6351 * this syscall writes the default timeslice value of a given process
6352 * into the user-space timespec buffer. A value of '0' means infinity.
6353 *
6354 * Return: On success, 0 and the timeslice is in @interval. Otherwise,
6355 * an error code.
6356 */
Al Viroabca5fc2017-09-19 18:17:46 -04006357SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
Arnd Bergmann474b9c72018-04-17 21:59:47 +02006358 struct __kernel_timespec __user *, interval)
Al Viroabca5fc2017-09-19 18:17:46 -04006359{
6360 struct timespec64 t;
6361 int retval = sched_rr_get_interval(pid, &t);
6362
6363 if (retval == 0)
6364 retval = put_timespec64(&t, interval);
6365
6366 return retval;
6367}
6368
Arnd Bergmann474b9c72018-04-17 21:59:47 +02006369#ifdef CONFIG_COMPAT_32BIT_TIME
Arnd Bergmann8dabe722019-01-07 00:33:08 +01006370SYSCALL_DEFINE2(sched_rr_get_interval_time32, pid_t, pid,
6371 struct old_timespec32 __user *, interval)
Al Viroabca5fc2017-09-19 18:17:46 -04006372{
6373 struct timespec64 t;
6374 int retval = sched_rr_get_interval(pid, &t);
6375
6376 if (retval == 0)
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +02006377 retval = put_old_timespec32(&t, interval);
Al Viroabca5fc2017-09-19 18:17:46 -04006378 return retval;
6379}
6380#endif
6381
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01006382void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 unsigned long free = 0;
Paul E. McKenney4e797522012-11-07 13:35:32 -08006385 int ppid;
Ingo Molnarc930b2c2017-02-03 12:22:54 +01006386
Tetsuo Handa38200502016-11-02 19:50:29 +09006387 if (!try_get_task_stack(p))
6388 return;
Xie XiuQi20435d82017-08-07 16:44:23 +08006389
Libing Zhoucc172ff2020-08-14 11:02:36 +08006390 pr_info("task:%-15.15s state:%c", p->comm, task_state_to_char(p));
Xie XiuQi20435d82017-08-07 16:44:23 +08006391
6392 if (p->state == TASK_RUNNING)
Libing Zhoucc172ff2020-08-14 11:02:36 +08006393 pr_cont(" running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394#ifdef CONFIG_DEBUG_STACK_USAGE
Eric Sandeen7c9f8862008-04-22 16:38:23 -05006395 free = stack_not_used(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396#endif
Oleg Nesterova90e9842014-12-10 15:45:21 -08006397 ppid = 0;
Paul E. McKenney4e797522012-11-07 13:35:32 -08006398 rcu_read_lock();
Oleg Nesterova90e9842014-12-10 15:45:21 -08006399 if (pid_alive(p))
6400 ppid = task_pid_nr(rcu_dereference(p->real_parent));
Paul E. McKenney4e797522012-11-07 13:35:32 -08006401 rcu_read_unlock();
Libing Zhoucc172ff2020-08-14 11:02:36 +08006402 pr_cont(" stack:%5lu pid:%5d ppid:%6d flags:0x%08lx\n",
6403 free, task_pid_nr(p), ppid,
David Rientjesaa47b7e2009-05-04 01:38:05 -07006404 (unsigned long)task_thread_info(p)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405
Tejun Heo3d1cb202013-04-30 15:27:22 -07006406 print_worker_info(KERN_INFO, p);
Dmitry Safonov9cb8f062020-06-08 21:32:29 -07006407 show_stack(p, NULL, KERN_INFO);
Tetsuo Handa38200502016-11-02 19:50:29 +09006408 put_task_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409}
Paul E. McKenney0032f4e2017-08-30 10:40:17 -07006410EXPORT_SYMBOL_GPL(sched_show_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411
Peter Zijlstra5d68cc92017-09-22 18:32:41 +02006412static inline bool
6413state_filter_match(unsigned long state_filter, struct task_struct *p)
6414{
6415 /* no filter, everything matches */
6416 if (!state_filter)
6417 return true;
6418
6419 /* filter, but doesn't match */
6420 if (!(p->state & state_filter))
6421 return false;
6422
6423 /*
6424 * When looking for TASK_UNINTERRUPTIBLE skip TASK_IDLE (allows
6425 * TASK_KILLABLE).
6426 */
6427 if (state_filter == TASK_UNINTERRUPTIBLE && p->state == TASK_IDLE)
6428 return false;
6429
6430 return true;
6431}
6432
6433
Ingo Molnare59e2ae2006-12-06 20:35:59 -08006434void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435{
Ingo Molnar36c8b582006-07-03 00:25:41 -07006436 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437
Thomas Gleixner510f5ac2011-07-17 20:47:54 +02006438 rcu_read_lock();
Oleg Nesterov5d07f422014-08-13 21:19:53 +02006439 for_each_process_thread(g, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 /*
6441 * reset the NMI-timeout, listing all files on a slow
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006442 * console might take a lot of time:
Andrey Ryabinin57675cb2016-06-09 15:20:05 +03006443 * Also, reset softlockup watchdogs on all CPUs, because
6444 * another CPU might be blocked waiting for us to process
6445 * an IPI.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446 */
6447 touch_nmi_watchdog();
Andrey Ryabinin57675cb2016-06-09 15:20:05 +03006448 touch_all_softlockup_watchdogs();
Peter Zijlstra5d68cc92017-09-22 18:32:41 +02006449 if (state_filter_match(state_filter, p))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01006450 sched_show_task(p);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02006451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452
Ingo Molnardd41f592007-07-09 18:51:59 +02006453#ifdef CONFIG_SCHED_DEBUG
Rabin Vincentfb90a6e2016-04-04 15:42:02 +02006454 if (!state_filter)
6455 sysrq_sched_debug_show();
Ingo Molnardd41f592007-07-09 18:51:59 +02006456#endif
Thomas Gleixner510f5ac2011-07-17 20:47:54 +02006457 rcu_read_unlock();
Ingo Molnare59e2ae2006-12-06 20:35:59 -08006458 /*
6459 * Only show locks if all tasks are dumped:
6460 */
Shmulik Ladkani93335a22009-11-25 15:23:41 +02006461 if (!state_filter)
Ingo Molnare59e2ae2006-12-06 20:35:59 -08006462 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463}
6464
Ingo Molnarf340c0d2005-06-28 16:40:42 +02006465/**
6466 * init_idle - set up an idle thread for a given CPU
6467 * @idle: task in question
Ingo Molnard1ccc662017-02-01 11:46:42 +01006468 * @cpu: CPU the idle task belongs to
Ingo Molnarf340c0d2005-06-28 16:40:42 +02006469 *
6470 * NOTE: this function does not set the idle thread's NEED_RESCHED
6471 * flag, to make booting more robust.
6472 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04006473void init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006475 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 unsigned long flags;
6477
Peter Zijlstraff51ff82019-10-01 11:18:37 +02006478 __sched_fork(0, idle);
6479
Peter Zijlstra25834c72015-05-15 17:43:34 +02006480 raw_spin_lock_irqsave(&idle->pi_lock, flags);
6481 raw_spin_lock(&rq->lock);
Ingo Molnar5cbd54e2008-11-12 20:05:50 +01006482
Peter Zijlstra06b83b52009-12-16 18:04:35 +01006483 idle->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02006484 idle->se.exec_start = sched_clock();
Peter Zijlstrac1de45c2016-11-28 23:03:05 -08006485 idle->flags |= PF_IDLE;
Ingo Molnardd41f592007-07-09 18:51:59 +02006486
Sami Tolvanend08b9f02020-04-27 09:00:07 -07006487 scs_task_reset(idle);
Mark Rutlande1b77c92016-03-09 14:08:18 -08006488 kasan_unpoison_task_stack(idle);
6489
Peter Zijlstrade9b8f52015-08-13 23:09:29 +02006490#ifdef CONFIG_SMP
6491 /*
6492 * Its possible that init_idle() gets called multiple times on a task,
6493 * in that case do_set_cpus_allowed() will not do the right thing.
6494 *
6495 * And since this is boot we can forgo the serialization.
6496 */
6497 set_cpus_allowed_common(idle, cpumask_of(cpu));
6498#endif
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02006499 /*
6500 * We're having a chicken and egg problem, even though we are
Ingo Molnard1ccc662017-02-01 11:46:42 +01006501 * holding rq->lock, the CPU isn't yet set to this CPU so the
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02006502 * lockdep check in task_group() will fail.
6503 *
6504 * Similar case to sched_fork(). / Alternatively we could
6505 * use task_rq_lock() here and obtain the other rq->lock.
6506 *
6507 * Silence PROVE_RCU
6508 */
6509 rcu_read_lock();
Ingo Molnardd41f592007-07-09 18:51:59 +02006510 __set_task_cpu(idle, cpu);
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02006511 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512
Eric W. Biederman5311a982019-09-14 07:35:02 -05006513 rq->idle = idle;
6514 rcu_assign_pointer(rq->curr, idle);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04006515 idle->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstrade9b8f52015-08-13 23:09:29 +02006516#ifdef CONFIG_SMP
Peter Zijlstra3ca7a442011-04-05 17:23:40 +02006517 idle->on_cpu = 1;
Nick Piggin4866cde2005-06-25 14:57:23 -07006518#endif
Peter Zijlstra25834c72015-05-15 17:43:34 +02006519 raw_spin_unlock(&rq->lock);
6520 raw_spin_unlock_irqrestore(&idle->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521
6522 /* Set the preempt count _outside_ the spinlocks! */
Peter Zijlstra01028742013-08-14 14:55:46 +02006523 init_idle_preempt_count(idle, cpu);
Jonathan Corbet625f2a32011-04-22 11:19:10 -06006524
Ingo Molnardd41f592007-07-09 18:51:59 +02006525 /*
6526 * The idle tasks have their own, simple scheduling class:
6527 */
6528 idle->sched_class = &idle_sched_class;
Steven Rostedt868baf02011-02-10 21:26:13 -05006529 ftrace_graph_init_idle_task(idle, cpu);
Frederic Weisbecker45eacc62013-05-15 22:16:32 +02006530 vtime_init_idle(idle, cpu);
Peter Zijlstrade9b8f52015-08-13 23:09:29 +02006531#ifdef CONFIG_SMP
Carsten Emdef1c6f1a2011-10-26 23:14:16 +02006532 sprintf(idle->comm, "%s/%d", INIT_TASK_COMM, cpu);
6533#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534}
6535
Nicolas Pitree1d4eee2017-06-14 13:19:23 -04006536#ifdef CONFIG_SMP
6537
Juri Lellif82f8042014-10-07 09:52:11 +01006538int cpuset_cpumask_can_shrink(const struct cpumask *cur,
6539 const struct cpumask *trial)
6540{
Nicolas Pitre06a76fe2017-06-21 14:22:01 -04006541 int ret = 1;
Juri Lellif82f8042014-10-07 09:52:11 +01006542
Mike Galbraithbb2bc552015-01-28 04:53:55 +01006543 if (!cpumask_weight(cur))
6544 return ret;
6545
Nicolas Pitre06a76fe2017-06-21 14:22:01 -04006546 ret = dl_cpuset_cpumask_can_shrink(cur, trial);
Juri Lellif82f8042014-10-07 09:52:11 +01006547
6548 return ret;
6549}
6550
Juri Lelli7f514122014-09-19 10:22:40 +01006551int task_can_attach(struct task_struct *p,
6552 const struct cpumask *cs_cpus_allowed)
6553{
6554 int ret = 0;
6555
6556 /*
6557 * Kthreads which disallow setaffinity shouldn't be moved
Ingo Molnard1ccc662017-02-01 11:46:42 +01006558 * to a new cpuset; we don't want to change their CPU
Juri Lelli7f514122014-09-19 10:22:40 +01006559 * affinity and isolating such threads by their set of
6560 * allowed nodes is unnecessary. Thus, cpusets are not
6561 * applicable for such threads. This prevents checking for
6562 * success of set_cpus_allowed_ptr() on all attached tasks
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02006563 * before cpus_mask may be changed.
Juri Lelli7f514122014-09-19 10:22:40 +01006564 */
6565 if (p->flags & PF_NO_SETAFFINITY) {
6566 ret = -EINVAL;
6567 goto out;
6568 }
6569
Juri Lelli7f514122014-09-19 10:22:40 +01006570 if (dl_task(p) && !cpumask_intersects(task_rq(p)->rd->span,
Nicolas Pitre06a76fe2017-06-21 14:22:01 -04006571 cs_cpus_allowed))
6572 ret = dl_task_can_attach(p, cs_cpus_allowed);
Juri Lelli7f514122014-09-19 10:22:40 +01006573
Juri Lelli7f514122014-09-19 10:22:40 +01006574out:
6575 return ret;
6576}
6577
Ingo Molnarf2cb1362017-02-01 13:10:18 +01006578bool sched_smp_initialized __read_mostly;
Thomas Gleixnere26fbff2016-03-10 12:54:10 +01006579
Mel Gormane6628d52013-10-07 11:29:02 +01006580#ifdef CONFIG_NUMA_BALANCING
6581/* Migrate current task p to target_cpu */
6582int migrate_task_to(struct task_struct *p, int target_cpu)
6583{
6584 struct migration_arg arg = { p, target_cpu };
6585 int curr_cpu = task_cpu(p);
6586
6587 if (curr_cpu == target_cpu)
6588 return 0;
6589
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02006590 if (!cpumask_test_cpu(target_cpu, p->cpus_ptr))
Mel Gormane6628d52013-10-07 11:29:02 +01006591 return -EINVAL;
6592
6593 /* TODO: This is not properly updating schedstats */
6594
Mel Gorman286549d2014-01-21 15:51:03 -08006595 trace_sched_move_numa(p, curr_cpu, target_cpu);
Mel Gormane6628d52013-10-07 11:29:02 +01006596 return stop_one_cpu(curr_cpu, migration_cpu_stop, &arg);
6597}
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006598
6599/*
6600 * Requeue a task on a given node and accurately track the number of NUMA
6601 * tasks on the runqueues
6602 */
6603void sched_setnuma(struct task_struct *p, int nid)
6604{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04006605 bool queued, running;
Peter Zijlstraeb580752015-07-31 21:28:18 +02006606 struct rq_flags rf;
6607 struct rq *rq;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006608
Peter Zijlstraeb580752015-07-31 21:28:18 +02006609 rq = task_rq_lock(p, &rf);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04006610 queued = task_on_rq_queued(p);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006611 running = task_current(rq, p);
6612
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04006613 if (queued)
Peter Zijlstra1de64442015-09-30 17:44:13 +02006614 dequeue_task(rq, p, DEQUEUE_SAVE);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006615 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04006616 put_prev_task(rq, p);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006617
6618 p->numa_preferred_nid = nid;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006619
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04006620 if (queued)
Peter Zijlstra7134b3e2017-02-21 14:23:38 +01006621 enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK);
Vincent Guittota399d232016-09-12 09:47:52 +02006622 if (running)
Peter Zijlstra03b7fad2019-05-29 20:36:41 +00006623 set_next_task(rq, p);
Peter Zijlstraeb580752015-07-31 21:28:18 +02006624 task_rq_unlock(rq, p, &rf);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006625}
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02006626#endif /* CONFIG_NUMA_BALANCING */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627
6628#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar48f24c42006-07-03 00:25:40 -07006629/*
Ingo Molnard1ccc662017-02-01 11:46:42 +01006630 * Ensure that the idle task is using init_mm right before its CPU goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631 * offline.
6632 */
6633void idle_task_exit(void)
6634{
6635 struct mm_struct *mm = current->active_mm;
6636
6637 BUG_ON(cpu_online(smp_processor_id()));
Peter Zijlstrabf2c59f2020-04-01 17:40:33 -04006638 BUG_ON(current != this_rq()->idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639
Martin Schwidefskya53efe52012-10-26 17:17:44 +02006640 if (mm != &init_mm) {
Andy Lutomirski252d2a42017-06-09 11:49:15 -07006641 switch_mm(mm, &init_mm, current);
Martin Schwidefskya53efe52012-10-26 17:17:44 +02006642 finish_arch_post_lock_switch();
6643 }
Peter Zijlstrabf2c59f2020-04-01 17:40:33 -04006644
6645 /* finish_cpu(), as ran on the BP, will clean up the active_mm state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646}
6647
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006648/*
Peter Zijlstra5d180232012-08-20 11:26:57 +02006649 * Since this CPU is going 'away' for a while, fold any nr_active delta
6650 * we might have. Assumes we're called after migrate_tasks() so that the
Thomas Gleixnerd60585c2016-07-12 18:33:56 +02006651 * nr_active count is stable. We need to take the teardown thread which
6652 * is calling this into account, so we hand in adjust = 1 to the load
6653 * calculation.
Peter Zijlstra5d180232012-08-20 11:26:57 +02006654 *
6655 * Also see the comment "Global load-average calculations".
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006656 */
Peter Zijlstra5d180232012-08-20 11:26:57 +02006657static void calc_load_migrate(struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658{
Thomas Gleixnerd60585c2016-07-12 18:33:56 +02006659 long delta = calc_load_fold_active(rq, 1);
Peter Zijlstra5d180232012-08-20 11:26:57 +02006660 if (delta)
6661 atomic_long_add(delta, &calc_load_tasks);
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02006662}
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006663
Peter Zijlstra10e70712019-08-06 15:13:17 +02006664static struct task_struct *__pick_migrate_task(struct rq *rq)
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01006665{
Peter Zijlstra10e70712019-08-06 15:13:17 +02006666 const struct sched_class *class;
6667 struct task_struct *next;
6668
6669 for_each_class(class) {
Peter Zijlstra98c2f702019-11-08 14:15:58 +01006670 next = class->pick_next_task(rq);
Peter Zijlstra10e70712019-08-06 15:13:17 +02006671 if (next) {
Peter Zijlstra6e2df052019-11-08 11:11:52 +01006672 next->sched_class->put_prev_task(rq, next);
Peter Zijlstra10e70712019-08-06 15:13:17 +02006673 return next;
6674 }
6675 }
6676
6677 /* The idle class should always have a runnable task */
6678 BUG();
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01006679}
6680
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006681/*
6682 * Migrate all tasks from the rq, sleeping tasks will be migrated by
6683 * try_to_wake_up()->select_task_rq().
6684 *
6685 * Called with rq->lock held even though we'er in stop_machine() and
6686 * there's no concurrency possible, we hold the required locks anyway
6687 * because of lock validation efforts.
6688 */
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006689static void migrate_tasks(struct rq *dead_rq, struct rq_flags *rf)
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006690{
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02006691 struct rq *rq = dead_rq;
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006692 struct task_struct *next, *stop = rq->stop;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006693 struct rq_flags orf = *rf;
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006694 int dest_cpu;
6695
6696 /*
6697 * Fudge the rq selection such that the below task selection loop
6698 * doesn't get stuck on the currently eligible stop task.
6699 *
6700 * We're currently inside stop_machine() and the rq is either stuck
6701 * in the stop_machine_cpu_stop() loop, or we're executing this code,
6702 * either way we should never end up calling schedule() until we're
6703 * done here.
6704 */
6705 rq->stop = NULL;
6706
Frederic Weisbecker77bd3972013-04-12 01:50:58 +02006707 /*
6708 * put_prev_task() and pick_next_task() sched
6709 * class method both need to have an up-to-date
6710 * value of rq->clock[_task]
6711 */
6712 update_rq_clock(rq);
6713
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02006714 for (;;) {
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006715 /*
6716 * There's this thread running, bail when that's the only
Ingo Molnard1ccc662017-02-01 11:46:42 +01006717 * remaining thread:
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006718 */
6719 if (rq->nr_running == 1)
6720 break;
6721
Peter Zijlstra10e70712019-08-06 15:13:17 +02006722 next = __pick_migrate_task(rq);
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006723
Wanpeng Li5473e0cc2015-08-28 14:55:56 +08006724 /*
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02006725 * Rules for changing task_struct::cpus_mask are holding
Wanpeng Li5473e0cc2015-08-28 14:55:56 +08006726 * both pi_lock and rq->lock, such that holding either
6727 * stabilizes the mask.
6728 *
6729 * Drop rq->lock is not quite as disastrous as it usually is
6730 * because !cpu_active at this point, which means load-balance
6731 * will not interfere. Also, stop-machine.
6732 */
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006733 rq_unlock(rq, rf);
Wanpeng Li5473e0cc2015-08-28 14:55:56 +08006734 raw_spin_lock(&next->pi_lock);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006735 rq_relock(rq, rf);
Wanpeng Li5473e0cc2015-08-28 14:55:56 +08006736
6737 /*
6738 * Since we're inside stop-machine, _nothing_ should have
6739 * changed the task, WARN if weird stuff happened, because in
6740 * that case the above rq->lock drop is a fail too.
6741 */
6742 if (WARN_ON(task_rq(next) != rq || !task_on_rq_queued(next))) {
6743 raw_spin_unlock(&next->pi_lock);
6744 continue;
6745 }
6746
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006747 /* Find suitable destination for @next, with force if needed. */
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02006748 dest_cpu = select_fallback_rq(dead_rq->cpu, next);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006749 rq = __migrate_task(rq, rf, next, dest_cpu);
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02006750 if (rq != dead_rq) {
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006751 rq_unlock(rq, rf);
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02006752 rq = dead_rq;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006753 *rf = orf;
6754 rq_relock(rq, rf);
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02006755 }
Wanpeng Li5473e0cc2015-08-28 14:55:56 +08006756 raw_spin_unlock(&next->pi_lock);
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006757 }
6758
6759 rq->stop = stop;
6760}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761#endif /* CONFIG_HOTPLUG_CPU */
6762
Ingo Molnarf2cb1362017-02-01 13:10:18 +01006763void set_rq_online(struct rq *rq)
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006764{
6765 if (!rq->online) {
6766 const struct sched_class *class;
6767
Rusty Russellc6c49272008-11-25 02:35:05 +10306768 cpumask_set_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006769 rq->online = 1;
6770
6771 for_each_class(class) {
6772 if (class->rq_online)
6773 class->rq_online(rq);
6774 }
6775 }
6776}
6777
Ingo Molnarf2cb1362017-02-01 13:10:18 +01006778void set_rq_offline(struct rq *rq)
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006779{
6780 if (rq->online) {
6781 const struct sched_class *class;
6782
6783 for_each_class(class) {
6784 if (class->rq_offline)
6785 class->rq_offline(rq);
6786 }
6787
Rusty Russellc6c49272008-11-25 02:35:05 +10306788 cpumask_clear_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006789 rq->online = 0;
6790 }
6791}
6792
Ingo Molnard1ccc662017-02-01 11:46:42 +01006793/*
6794 * used to mark begin/end of suspend/resume:
6795 */
6796static int num_cpus_frozen;
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05306797
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798/*
Tejun Heo3a101d02010-06-08 21:40:36 +02006799 * Update cpusets according to cpu_active mask. If cpusets are
6800 * disabled, cpuset_update_active_cpus() becomes a simple wrapper
6801 * around partition_sched_domains().
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05306802 *
6803 * If we come here as part of a suspend/resume, don't touch cpusets because we
6804 * want to restore it back to its original state upon resume anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 */
Thomas Gleixner40190a72016-03-10 12:54:13 +01006806static void cpuset_cpu_active(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807{
Thomas Gleixner40190a72016-03-10 12:54:13 +01006808 if (cpuhp_tasks_frozen) {
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05306809 /*
6810 * num_cpus_frozen tracks how many CPUs are involved in suspend
6811 * resume sequence. As long as this is not the last online
6812 * operation in the resume sequence, just build a single sched
6813 * domain, ignoring cpusets.
6814 */
Peter Zijlstra50e76632017-09-07 11:13:38 +02006815 partition_sched_domains(1, NULL, NULL);
6816 if (--num_cpus_frozen)
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006817 return;
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05306818 /*
6819 * This is the last CPU online operation. So fall through and
6820 * restore the original sched domains by considering the
6821 * cpuset configurations.
6822 */
Peter Zijlstra50e76632017-09-07 11:13:38 +02006823 cpuset_force_rebuild();
Max Krasnyanskye761b772008-07-15 04:43:49 -07006824 }
Rakib Mullick30e03ac2017-04-09 07:36:14 +06006825 cpuset_update_active_cpus();
Max Krasnyanskye761b772008-07-15 04:43:49 -07006826}
Tejun Heo3a101d02010-06-08 21:40:36 +02006827
Thomas Gleixner40190a72016-03-10 12:54:13 +01006828static int cpuset_cpu_inactive(unsigned int cpu)
Tejun Heo3a101d02010-06-08 21:40:36 +02006829{
Thomas Gleixner40190a72016-03-10 12:54:13 +01006830 if (!cpuhp_tasks_frozen) {
Nicolas Pitre06a76fe2017-06-21 14:22:01 -04006831 if (dl_cpu_busy(cpu))
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006832 return -EBUSY;
Rakib Mullick30e03ac2017-04-09 07:36:14 +06006833 cpuset_update_active_cpus();
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006834 } else {
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05306835 num_cpus_frozen++;
6836 partition_sched_domains(1, NULL, NULL);
Tejun Heo3a101d02010-06-08 21:40:36 +02006837 }
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006838 return 0;
Tejun Heo3a101d02010-06-08 21:40:36 +02006839}
Max Krasnyanskye761b772008-07-15 04:43:49 -07006840
Thomas Gleixner40190a72016-03-10 12:54:13 +01006841int sched_cpu_activate(unsigned int cpu)
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006842{
Thomas Gleixner7d976692016-03-10 12:54:17 +01006843 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006844 struct rq_flags rf;
Thomas Gleixner7d976692016-03-10 12:54:17 +01006845
Peter Zijlstraba2591a2018-05-29 16:43:46 +02006846#ifdef CONFIG_SCHED_SMT
6847 /*
Peter Zijlstra (Intel)c5511d02018-11-25 19:33:36 +01006848 * When going up, increment the number of cores with SMT present.
Peter Zijlstraba2591a2018-05-29 16:43:46 +02006849 */
Peter Zijlstra (Intel)c5511d02018-11-25 19:33:36 +01006850 if (cpumask_weight(cpu_smt_mask(cpu)) == 2)
6851 static_branch_inc_cpuslocked(&sched_smt_present);
Peter Zijlstraba2591a2018-05-29 16:43:46 +02006852#endif
Thomas Gleixner40190a72016-03-10 12:54:13 +01006853 set_cpu_active(cpu, true);
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006854
Thomas Gleixner40190a72016-03-10 12:54:13 +01006855 if (sched_smp_initialized) {
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006856 sched_domains_numa_masks_set(cpu);
Thomas Gleixner40190a72016-03-10 12:54:13 +01006857 cpuset_cpu_active();
Nick Piggin5c1e1762006-10-03 01:14:04 -07006858 }
Thomas Gleixner7d976692016-03-10 12:54:17 +01006859
6860 /*
6861 * Put the rq online, if not already. This happens:
6862 *
6863 * 1) In the early boot process, because we build the real domains
Ingo Molnard1ccc662017-02-01 11:46:42 +01006864 * after all CPUs have been brought up.
Thomas Gleixner7d976692016-03-10 12:54:17 +01006865 *
6866 * 2) At runtime, if cpuset_cpu_active() fails to rebuild the
6867 * domains.
6868 */
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006869 rq_lock_irqsave(rq, &rf);
Thomas Gleixner7d976692016-03-10 12:54:17 +01006870 if (rq->rd) {
6871 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
6872 set_rq_online(rq);
6873 }
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006874 rq_unlock_irqrestore(rq, &rf);
Thomas Gleixner7d976692016-03-10 12:54:17 +01006875
Thomas Gleixner40190a72016-03-10 12:54:13 +01006876 return 0;
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006877}
6878
Thomas Gleixner40190a72016-03-10 12:54:13 +01006879int sched_cpu_deactivate(unsigned int cpu)
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006880{
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006881 int ret;
6882
Thomas Gleixner40190a72016-03-10 12:54:13 +01006883 set_cpu_active(cpu, false);
Peter Zijlstrab2454ca2016-03-10 12:54:14 +01006884 /*
6885 * We've cleared cpu_active_mask, wait for all preempt-disabled and RCU
6886 * users of this state to go away such that all new such users will
6887 * observe it.
6888 *
Peter Zijlstrab2454ca2016-03-10 12:54:14 +01006889 * Do sync before park smpboot threads to take care the rcu boost case.
6890 */
Paul E. McKenney309ba852018-07-11 14:36:49 -07006891 synchronize_rcu();
Thomas Gleixner40190a72016-03-10 12:54:13 +01006892
Peter Zijlstra (Intel)c5511d02018-11-25 19:33:36 +01006893#ifdef CONFIG_SCHED_SMT
6894 /*
6895 * When going down, decrement the number of cores with SMT present.
6896 */
6897 if (cpumask_weight(cpu_smt_mask(cpu)) == 2)
6898 static_branch_dec_cpuslocked(&sched_smt_present);
6899#endif
6900
Thomas Gleixner40190a72016-03-10 12:54:13 +01006901 if (!sched_smp_initialized)
6902 return 0;
6903
6904 ret = cpuset_cpu_inactive(cpu);
6905 if (ret) {
6906 set_cpu_active(cpu, true);
6907 return ret;
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006908 }
Thomas Gleixner40190a72016-03-10 12:54:13 +01006909 sched_domains_numa_masks_clear(cpu);
6910 return 0;
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006911}
6912
Thomas Gleixner94baf7a2016-03-10 12:54:15 +01006913static void sched_rq_cpu_starting(unsigned int cpu)
6914{
6915 struct rq *rq = cpu_rq(cpu);
6916
6917 rq->calc_load_update = calc_load_update;
Thomas Gleixner94baf7a2016-03-10 12:54:15 +01006918 update_max_interval();
6919}
6920
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006921int sched_cpu_starting(unsigned int cpu)
6922{
Thomas Gleixner94baf7a2016-03-10 12:54:15 +01006923 sched_rq_cpu_starting(cpu);
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01006924 sched_tick_start(cpu);
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006925 return 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01006926}
6927
Thomas Gleixnerf2785dd2016-03-10 12:54:18 +01006928#ifdef CONFIG_HOTPLUG_CPU
6929int sched_cpu_dying(unsigned int cpu)
6930{
6931 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006932 struct rq_flags rf;
Thomas Gleixnerf2785dd2016-03-10 12:54:18 +01006933
6934 /* Handle pending wakeups and then migrate everything off */
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01006935 sched_tick_stop(cpu);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006936
6937 rq_lock_irqsave(rq, &rf);
Thomas Gleixnerf2785dd2016-03-10 12:54:18 +01006938 if (rq->rd) {
6939 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
6940 set_rq_offline(rq);
6941 }
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006942 migrate_tasks(rq, &rf);
Thomas Gleixnerf2785dd2016-03-10 12:54:18 +01006943 BUG_ON(rq->nr_running != 1);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006944 rq_unlock_irqrestore(rq, &rf);
6945
Thomas Gleixnerf2785dd2016-03-10 12:54:18 +01006946 calc_load_migrate(rq);
6947 update_max_interval();
Peter Zijlstra00357f52017-12-21 15:06:50 +01006948 nohz_balance_exit_idle(rq);
Thomas Gleixnere5ef27d2016-03-10 12:54:21 +01006949 hrtick_clear(rq);
Thomas Gleixnerf2785dd2016-03-10 12:54:18 +01006950 return 0;
6951}
6952#endif
6953
Peter Zijlstra6f505b12008-01-25 21:08:30 +01006954void __init sched_init_smp(void)
6955{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956 sched_init_numa();
Christoph Lameter476f3532007-05-06 14:48:58 -07006957
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02006958 /*
6959 * There's no userspace yet to cause hotplug operations; hence all the
Ingo Molnard1ccc662017-02-01 11:46:42 +01006960 * CPU masks are stable and all blatant races in the below code cannot
Valentin Schneiderb5a4e2b2018-12-19 18:23:16 +00006961 * happen.
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02006962 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963 mutex_lock(&sched_domains_mutex);
Peter Zijlstra8d5dc512017-04-25 15:29:40 +02006964 sched_init_domains(cpu_active_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965 mutex_unlock(&sched_domains_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966
Mike Travis434d53b2008-04-04 18:11:04 -07006967 /* Move init over to a non-isolated CPU */
Frederic Weisbeckeredb93822017-10-27 04:42:37 +02006968 if (set_cpus_allowed_ptr(current, housekeeping_cpumask(HK_FLAG_DOMAIN)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006969 BUG();
6970 sched_init_granularity();
Rusty Russell42128232008-11-25 02:35:12 +10306971
Rusty Russell0e3900e2008-11-25 02:35:13 +10306972 init_sched_rt_class();
Juri Lelli1baca4c2013-11-07 14:43:38 +01006973 init_sched_dl_class();
Peter Zijlstra1b568f02016-05-09 10:38:41 +02006974
Thomas Gleixnere26fbff2016-03-10 12:54:10 +01006975 sched_smp_initialized = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976}
Thomas Gleixnere26fbff2016-03-10 12:54:10 +01006977
6978static int __init migration_init(void)
6979{
Nicholas Piggin77a53522019-04-11 13:34:44 +10006980 sched_cpu_starting(smp_processor_id());
Thomas Gleixnere26fbff2016-03-10 12:54:10 +01006981 return 0;
6982}
6983early_initcall(migration_init);
6984
Ingo Molnardd41f592007-07-09 18:51:59 +02006985#else
6986void __init sched_init_smp(void)
6987{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006988 sched_init_granularity();
6989}
Peter Williams2dd73a42006-06-27 02:54:34 -07006990#endif /* CONFIG_SMP */
Heiko Carstensb50f60c2006-07-30 03:03:52 -07006991
Avi Kivitye107be32007-07-26 13:40:43 +02006992int in_sched_functions(unsigned long addr)
6993{
6994 return in_lock_functions(addr) ||
6995 (addr >= (unsigned long)__sched_text_start
Christoph Lameterc9819f42006-12-10 02:20:25 -08006996 && addr < (unsigned long)__sched_text_end);
Christoph Lameter476f3532007-05-06 14:48:58 -07006997}
Christoph Lameterc9819f42006-12-10 02:20:25 -08006998
Peter Zijlstra029632f2011-10-25 10:00:11 +02006999#ifdef CONFIG_CGROUP_SCHED
Li Zefan27b4b932013-03-05 16:07:52 +08007000/*
7001 * Default task group.
7002 * Every task in system belongs to this group at bootup.
7003 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02007004struct task_group root_task_group;
Mike Galbraith35cf4e52012-08-07 05:00:13 +02007005LIST_HEAD(task_groups);
Waiman Longb0367622015-12-02 13:41:49 -05007006
7007/* Cacheline aligned slab cache for task_group */
7008static struct kmem_cache *task_group_cache __read_mostly;
Heiko Carstensb50f60c2006-07-30 03:03:52 -07007009#endif
7010
Joonsoo Kime6252c32013-04-23 17:27:41 +09007011DECLARE_PER_CPU(cpumask_var_t, load_balance_mask);
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02007012DECLARE_PER_CPU(cpumask_var_t, select_idle_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013
7014void __init sched_init(void)
7015{
Qian Caia1dc0442019-07-19 21:23:19 -04007016 unsigned long ptr = 0;
Dietmar Eggemann55627e32019-05-27 07:21:13 +01007017 int i;
Mike Travis434d53b2008-04-04 18:11:04 -07007018
Steven Rostedt (VMware)c3a340f2019-12-19 16:44:53 -05007019 /* Make sure the linker didn't screw up */
7020 BUG_ON(&idle_sched_class + 1 != &fair_sched_class ||
7021 &fair_sched_class + 1 != &rt_sched_class ||
7022 &rt_sched_class + 1 != &dl_sched_class);
7023#ifdef CONFIG_SMP
7024 BUG_ON(&dl_sched_class + 1 != &stop_sched_class);
7025#endif
7026
Ingo Molnar5822a452017-03-05 13:09:07 +01007027 wait_bit_init();
Linus Torvalds9dcb8b62016-10-26 10:15:30 -07007028
Mike Travis434d53b2008-04-04 18:11:04 -07007029#ifdef CONFIG_FAIR_GROUP_SCHED
Qian Caia1dc0442019-07-19 21:23:19 -04007030 ptr += 2 * nr_cpu_ids * sizeof(void **);
Mike Travis434d53b2008-04-04 18:11:04 -07007031#endif
7032#ifdef CONFIG_RT_GROUP_SCHED
Qian Caia1dc0442019-07-19 21:23:19 -04007033 ptr += 2 * nr_cpu_ids * sizeof(void **);
Mike Travis434d53b2008-04-04 18:11:04 -07007034#endif
Qian Caia1dc0442019-07-19 21:23:19 -04007035 if (ptr) {
7036 ptr = (unsigned long)kzalloc(ptr, GFP_NOWAIT);
Mike Travis434d53b2008-04-04 18:11:04 -07007037
7038#ifdef CONFIG_FAIR_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007039 root_task_group.se = (struct sched_entity **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007040 ptr += nr_cpu_ids * sizeof(void **);
7041
Yong Zhang07e06b02011-01-07 15:17:36 +08007042 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007043 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007044
Wei Yangb1d17792020-04-23 21:44:43 +00007045 root_task_group.shares = ROOT_TASK_GROUP_LOAD;
7046 init_cfs_bandwidth(&root_task_group.cfs_bandwidth);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007047#endif /* CONFIG_FAIR_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07007048#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007049 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07007050 ptr += nr_cpu_ids * sizeof(void **);
7051
Yong Zhang07e06b02011-01-07 15:17:36 +08007052 root_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02007053 ptr += nr_cpu_ids * sizeof(void **);
7054
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007055#endif /* CONFIG_RT_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07007056 }
Alex Thorltonb74e6272014-12-18 12:44:30 -06007057#ifdef CONFIG_CPUMASK_OFFSTACK
7058 for_each_possible_cpu(i) {
7059 per_cpu(load_balance_mask, i) = (cpumask_var_t)kzalloc_node(
7060 cpumask_size(), GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02007061 per_cpu(select_idle_mask, i) = (cpumask_var_t)kzalloc_node(
7062 cpumask_size(), GFP_KERNEL, cpu_to_node(i));
Alex Thorltonb74e6272014-12-18 12:44:30 -06007063 }
7064#endif /* CONFIG_CPUMASK_OFFSTACK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065
Ingo Molnard1ccc662017-02-01 11:46:42 +01007066 init_rt_bandwidth(&def_rt_bandwidth, global_rt_period(), global_rt_runtime());
7067 init_dl_bandwidth(&def_dl_bandwidth, global_rt_period(), global_rt_runtime());
Dario Faggioli332ac172013-11-07 14:43:45 +01007068
Gregory Haskins57d885f2008-01-25 21:08:18 +01007069#ifdef CONFIG_SMP
7070 init_defrootdomain();
7071#endif
7072
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007073#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007074 init_rt_bandwidth(&root_task_group.rt_bandwidth,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007075 global_rt_period(), global_rt_runtime());
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007076#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007077
Dhaval Giani7c941432010-01-20 13:26:18 +01007078#ifdef CONFIG_CGROUP_SCHED
Waiman Longb0367622015-12-02 13:41:49 -05007079 task_group_cache = KMEM_CACHE(task_group, 0);
7080
Yong Zhang07e06b02011-01-07 15:17:36 +08007081 list_add(&root_task_group.list, &task_groups);
7082 INIT_LIST_HEAD(&root_task_group.children);
Glauber Costaf4d6f6c2011-11-01 19:19:07 -02007083 INIT_LIST_HEAD(&root_task_group.siblings);
Mike Galbraith5091faa2010-11-30 14:18:03 +01007084 autogroup_init(&init_task);
Dhaval Giani7c941432010-01-20 13:26:18 +01007085#endif /* CONFIG_CGROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007086
Ingo Molnardd41f592007-07-09 18:51:59 +02007087 for_each_possible_cpu(i) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007088 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089
7090 rq = cpu_rq(i);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01007091 raw_spin_lock_init(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092 rq->nr_running = 0;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007093 rq->calc_load_active = 0;
7094 rq->calc_load_update = jiffies + LOAD_FREQ;
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02007095 init_cfs_rq(&rq->cfs);
Abel Vesa07c54f72015-03-03 13:50:27 +02007096 init_rt_rq(&rq->rt);
7097 init_dl_rq(&rq->dl);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007098#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007099 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Vincent Guittot9c2791f2016-11-08 10:53:43 +01007100 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list;
Dhaval Giani354d60c2008-04-19 19:44:59 +02007101 /*
Ingo Molnard1ccc662017-02-01 11:46:42 +01007102 * How much CPU bandwidth does root_task_group get?
Dhaval Giani354d60c2008-04-19 19:44:59 +02007103 *
7104 * In case of task-groups formed thr' the cgroup filesystem, it
Ingo Molnard1ccc662017-02-01 11:46:42 +01007105 * gets 100% of the CPU resources in the system. This overall
7106 * system CPU resource is divided among the tasks of
Yong Zhang07e06b02011-01-07 15:17:36 +08007107 * root_task_group and its child task-groups in a fair manner,
Dhaval Giani354d60c2008-04-19 19:44:59 +02007108 * based on each entity's (task or task-group's) weight
7109 * (se->load.weight).
7110 *
Yong Zhang07e06b02011-01-07 15:17:36 +08007111 * In other words, if root_task_group has 10 tasks of weight
Dhaval Giani354d60c2008-04-19 19:44:59 +02007112 * 1024) and two child groups A0 and A1 (of weight 1024 each),
Ingo Molnard1ccc662017-02-01 11:46:42 +01007113 * then A0's share of the CPU resource is:
Dhaval Giani354d60c2008-04-19 19:44:59 +02007114 *
Ingo Molnar0d905bc2009-05-04 19:13:30 +02007115 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
Dhaval Giani354d60c2008-04-19 19:44:59 +02007116 *
Yong Zhang07e06b02011-01-07 15:17:36 +08007117 * We achieve this by letting root_task_group's tasks sit
7118 * directly in rq->cfs (i.e root_task_group->se[] = NULL).
Dhaval Giani354d60c2008-04-19 19:44:59 +02007119 */
Yong Zhang07e06b02011-01-07 15:17:36 +08007120 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02007121#endif /* CONFIG_FAIR_GROUP_SCHED */
7122
7123 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007124#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08007125 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007126#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127#ifdef CONFIG_SMP
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08007128 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01007129 rq->rd = NULL;
Vincent Guittotca6d75e2015-02-27 16:54:09 +01007130 rq->cpu_capacity = rq->cpu_capacity_orig = SCHED_CAPACITY_SCALE;
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02007131 rq->balance_callback = NULL;
Ingo Molnar3117df02006-12-13 00:34:43 -08007132 rq->active_balance = 0;
7133 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134 rq->push_cpu = 0;
7135 rq->cpu = i;
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04007136 rq->online = 0;
Mike Galbraitheae0c9d2009-11-10 03:50:02 +01007137 rq->idle_stamp = 0;
7138 rq->avg_idle = 2*sysctl_sched_migration_cost;
Jason Low9bd721c2013-09-13 11:26:52 -07007139 rq->max_idle_balance_cost = sysctl_sched_migration_cost;
Peter Zijlstra367456c2012-02-20 21:49:09 +01007140
7141 INIT_LIST_HEAD(&rq->cfs_tasks);
7142
Gregory Haskinsdc938522008-01-25 21:08:26 +01007143 rq_attach_root(rq, &def_root_domain);
Frederic Weisbecker3451d022011-08-10 23:21:01 +02007144#ifdef CONFIG_NO_HZ_COMMON
Peter Zijlstrae022e0d2017-12-21 11:20:23 +01007145 rq->last_blocked_load_update_tick = jiffies;
Peter Zijlstraa22e47a2017-12-21 10:01:24 +01007146 atomic_set(&rq->nohz_flags, 0);
Peter Zijlstra (Intel)90b53632020-03-27 11:44:56 +01007147
7148 rq_csd_init(rq, &rq->nohz_csd, nohz_csd_func);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07007149#endif
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02007150#endif /* CONFIG_SMP */
Frederic Weisbecker77a021b2018-02-21 05:17:23 +01007151 hrtick_rq_init(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153 }
Ingo Molnara0f98a12007-06-17 18:37:45 +02007154
Vincent Guittot90593932017-05-17 11:50:45 +02007155 set_load_weight(&init_task, false);
Ingo Molnardd41f592007-07-09 18:51:59 +02007156
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157 /*
7158 * The boot idle thread does lazy MMU switching as well:
7159 */
Vegard Nossumf1f10072017-02-27 14:30:07 -08007160 mmgrab(&init_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161 enter_lazy_tlb(&init_mm, current);
7162
7163 /*
7164 * Make us the idle thread. Technically, schedule() should not be
7165 * called from this thread, however somewhere below it might be,
7166 * but because we are the idle thread, we just pick up running again
7167 * when this runqueue becomes "idle".
7168 */
7169 init_idle(current, smp_processor_id());
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02007170
7171 calc_load_update = jiffies + LOAD_FREQ;
7172
Rusty Russellbf4d83f2008-11-25 09:57:51 +10307173#ifdef CONFIG_SMP
Thomas Gleixner29d5e042012-04-20 13:05:45 +00007174 idle_thread_set_boot_cpu();
Peter Zijlstra029632f2011-10-25 10:00:11 +02007175#endif
7176 init_sched_fair_class();
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10307177
Josh Poimboeuf4698f882016-06-07 14:43:16 -05007178 init_schedstats();
7179
Johannes Weinereb414682018-10-26 15:06:27 -07007180 psi_init();
7181
Patrick Bellasi69842cb2019-06-21 09:42:02 +01007182 init_uclamp();
7183
Ingo Molnar6892b752008-02-13 14:02:36 +01007184 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185}
7186
Frederic Weisbeckerd902db12011-06-08 19:31:56 +02007187#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007188static inline int preempt_count_equals(int preempt_offset)
7189{
Peter Zijlstrada7142e2015-09-28 18:11:45 +02007190 int nested = preempt_count() + rcu_preempt_depth();
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007191
Arnd Bergmann4ba82162011-01-25 22:52:22 +01007192 return (nested == preempt_offset);
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02007193}
7194
Simon Kagstromd8948372009-12-23 11:08:18 +01007195void __might_sleep(const char *file, int line, int preempt_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196{
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02007197 /*
7198 * Blocking primitives will set (and therefore destroy) current->state,
7199 * since we will exit with TASK_RUNNING make sure we enter with it,
7200 * otherwise we will destroy state.
7201 */
Linus Torvalds00845eb2015-02-01 12:23:32 -08007202 WARN_ONCE(current->state != TASK_RUNNING && current->task_state_change,
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02007203 "do not call blocking ops when !TASK_RUNNING; "
7204 "state=%lx set at [<%p>] %pS\n",
7205 current->state,
7206 (void *)current->task_state_change,
Linus Torvalds00845eb2015-02-01 12:23:32 -08007207 (void *)current->task_state_change);
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02007208
Peter Zijlstra34274452014-09-24 10:18:56 +02007209 ___might_sleep(file, line, preempt_offset);
7210}
7211EXPORT_SYMBOL(__might_sleep);
7212
7213void ___might_sleep(const char *file, int line, int preempt_offset)
7214{
Ingo Molnard1ccc662017-02-01 11:46:42 +01007215 /* Ratelimiting timestamp: */
7216 static unsigned long prev_jiffy;
7217
Vegard Nossumd1c6d142016-07-23 09:46:39 +02007218 unsigned long preempt_disable_ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219
Ingo Molnard1ccc662017-02-01 11:46:42 +01007220 /* WARN_ON_ONCE() by default, no rate limit required: */
7221 rcu_sleep_check();
7222
Thomas Gleixnerdb273be2014-02-07 20:58:38 +01007223 if ((preempt_count_equals(preempt_offset) && !irqs_disabled() &&
Daniel Vetter312364f32019-08-26 22:14:23 +02007224 !is_idle_task(current) && !current->non_block_count) ||
Thomas Gleixner1c3c5ea2017-05-16 20:42:48 +02007225 system_state == SYSTEM_BOOTING || system_state > SYSTEM_RUNNING ||
7226 oops_in_progress)
Ingo Molnaraef745f2008-08-28 11:34:43 +02007227 return;
Thomas Gleixner1c3c5ea2017-05-16 20:42:48 +02007228
Ingo Molnaraef745f2008-08-28 11:34:43 +02007229 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
7230 return;
7231 prev_jiffy = jiffies;
7232
Ingo Molnard1ccc662017-02-01 11:46:42 +01007233 /* Save this before calling printk(), since that will clobber it: */
Vegard Nossumd1c6d142016-07-23 09:46:39 +02007234 preempt_disable_ip = get_preempt_disable_ip(current);
7235
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01007236 printk(KERN_ERR
7237 "BUG: sleeping function called from invalid context at %s:%d\n",
7238 file, line);
7239 printk(KERN_ERR
Daniel Vetter312364f32019-08-26 22:14:23 +02007240 "in_atomic(): %d, irqs_disabled(): %d, non_block: %d, pid: %d, name: %s\n",
7241 in_atomic(), irqs_disabled(), current->non_block_count,
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01007242 current->pid, current->comm);
Ingo Molnaraef745f2008-08-28 11:34:43 +02007243
Eric Sandeena8b686b2014-12-16 16:25:28 -06007244 if (task_stack_end_corrupted(current))
7245 printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
7246
Ingo Molnaraef745f2008-08-28 11:34:43 +02007247 debug_show_held_locks(current);
7248 if (irqs_disabled())
7249 print_irqtrace_events(current);
Vegard Nossumd1c6d142016-07-23 09:46:39 +02007250 if (IS_ENABLED(CONFIG_DEBUG_PREEMPT)
7251 && !preempt_count_equals(preempt_offset)) {
Thomas Gleixner8f47b182014-02-07 20:58:39 +01007252 pr_err("Preemption disabled at:");
Dmitry Safonov2062a4e2020-06-08 21:29:56 -07007253 print_ip_sym(KERN_ERR, preempt_disable_ip);
Thomas Gleixner8f47b182014-02-07 20:58:39 +01007254 }
Ingo Molnaraef745f2008-08-28 11:34:43 +02007255 dump_stack();
Vegard Nossumf0b22e32016-07-22 21:46:02 +02007256 add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257}
Peter Zijlstra34274452014-09-24 10:18:56 +02007258EXPORT_SYMBOL(___might_sleep);
Peter Zijlstra568f1962019-01-28 17:21:52 -08007259
7260void __cant_sleep(const char *file, int line, int preempt_offset)
7261{
7262 static unsigned long prev_jiffy;
7263
7264 if (irqs_disabled())
7265 return;
7266
7267 if (!IS_ENABLED(CONFIG_PREEMPT_COUNT))
7268 return;
7269
7270 if (preempt_count() > preempt_offset)
7271 return;
7272
7273 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
7274 return;
7275 prev_jiffy = jiffies;
7276
7277 printk(KERN_ERR "BUG: assuming atomic context at %s:%d\n", file, line);
7278 printk(KERN_ERR "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
7279 in_atomic(), irqs_disabled(),
7280 current->pid, current->comm);
7281
7282 debug_show_held_locks(current);
7283 dump_stack();
7284 add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
7285}
7286EXPORT_SYMBOL_GPL(__cant_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287#endif
7288
7289#ifdef CONFIG_MAGIC_SYSRQ
7290void normalize_rt_tasks(void)
7291{
7292 struct task_struct *g, *p;
Peter Zijlstradbc7f062015-06-11 14:46:38 +02007293 struct sched_attr attr = {
7294 .sched_policy = SCHED_NORMAL,
7295 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007297 read_lock(&tasklist_lock);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007298 for_each_process_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02007299 /*
7300 * Only normalize user tasks:
7301 */
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007302 if (p->flags & PF_KTHREAD)
Ingo Molnar178be792007-10-15 17:00:18 +02007303 continue;
7304
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05007305 p->se.exec_start = 0;
7306 schedstat_set(p->se.statistics.wait_start, 0);
7307 schedstat_set(p->se.statistics.sleep_start, 0);
7308 schedstat_set(p->se.statistics.block_start, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02007309
Dario Faggioliaab03e02013-11-28 11:14:43 +01007310 if (!dl_task(p) && !rt_task(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007311 /*
7312 * Renice negative nice level userspace
7313 * tasks back to 0:
7314 */
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007315 if (task_nice(p) < 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02007316 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007317 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02007318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319
Peter Zijlstradbc7f062015-06-11 14:46:38 +02007320 __sched_setscheduler(p, &attr, false, false);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007321 }
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007322 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323}
7324
7325#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07007326
Jason Wessel67fc4e02010-05-20 21:04:21 -05007327#if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007328/*
Jason Wessel67fc4e02010-05-20 21:04:21 -05007329 * These functions are only useful for the IA64 MCA handling, or kdb.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007330 *
7331 * They can only be called when the whole system has been
7332 * stopped - every CPU needs to be quiescent, and no scheduling
7333 * activity can take place. Using them for anything else would
7334 * be a serious bug, and as a result, they aren't even visible
7335 * under any other configuration.
7336 */
7337
7338/**
Ingo Molnard1ccc662017-02-01 11:46:42 +01007339 * curr_task - return the current task for a given CPU.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007340 * @cpu: the processor in question.
7341 *
7342 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
Yacine Belkadie69f6182013-07-12 20:45:47 +02007343 *
7344 * Return: The current task for @cpu.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007345 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007346struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007347{
7348 return cpu_curr(cpu);
7349}
7350
Jason Wessel67fc4e02010-05-20 21:04:21 -05007351#endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */
7352
7353#ifdef CONFIG_IA64
Linus Torvalds1df5c102005-09-12 07:59:21 -07007354/**
Peter Zijlstra5feeb782019-05-29 20:36:38 +00007355 * ia64_set_curr_task - set the current task for a given CPU.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007356 * @cpu: the processor in question.
7357 * @p: the task pointer to set.
7358 *
7359 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007360 * are serviced on a separate stack. It allows the architecture to switch the
Ingo Molnard1ccc662017-02-01 11:46:42 +01007361 * notion of the current task on a CPU in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07007362 * must be called with all CPU's synchronized, and interrupts disabled, the
7363 * and caller must save the original value of the current task (see
7364 * curr_task() above) and restore that value before reenabling interrupts and
7365 * re-starting the system.
7366 *
7367 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7368 */
Peter Zijlstraa458ae22016-09-20 20:29:40 +02007369void ia64_set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007370{
7371 cpu_curr(cpu) = p;
7372}
7373
7374#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007375
Dhaval Giani7c941432010-01-20 13:26:18 +01007376#ifdef CONFIG_CGROUP_SCHED
Peter Zijlstra029632f2011-10-25 10:00:11 +02007377/* task_group_lock serializes the addition/removal of task groups */
7378static DEFINE_SPINLOCK(task_group_lock);
7379
Patrick Bellasi2480c092019-08-22 14:28:06 +01007380static inline void alloc_uclamp_sched_group(struct task_group *tg,
7381 struct task_group *parent)
7382{
7383#ifdef CONFIG_UCLAMP_TASK_GROUP
Patrick Bellasi0413d7f2019-08-22 14:28:11 +01007384 enum uclamp_id clamp_id;
Patrick Bellasi2480c092019-08-22 14:28:06 +01007385
7386 for_each_clamp_id(clamp_id) {
7387 uclamp_se_set(&tg->uclamp_req[clamp_id],
7388 uclamp_none(clamp_id), false);
Patrick Bellasi0b60ba22019-08-22 14:28:07 +01007389 tg->uclamp[clamp_id] = parent->uclamp[clamp_id];
Patrick Bellasi2480c092019-08-22 14:28:06 +01007390 }
7391#endif
7392}
7393
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007394static void sched_free_group(struct task_group *tg)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007395{
7396 free_fair_sched_group(tg);
7397 free_rt_sched_group(tg);
Mike Galbraithe9aa1dd2011-01-05 11:11:25 +01007398 autogroup_free(tg);
Waiman Longb0367622015-12-02 13:41:49 -05007399 kmem_cache_free(task_group_cache, tg);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007400}
7401
7402/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007403struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007404{
7405 struct task_group *tg;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007406
Waiman Longb0367622015-12-02 13:41:49 -05007407 tg = kmem_cache_alloc(task_group_cache, GFP_KERNEL | __GFP_ZERO);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007408 if (!tg)
7409 return ERR_PTR(-ENOMEM);
7410
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007411 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007412 goto err;
7413
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007414 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007415 goto err;
7416
Patrick Bellasi2480c092019-08-22 14:28:06 +01007417 alloc_uclamp_sched_group(tg, parent);
7418
Li Zefanace783b2013-01-24 14:30:48 +08007419 return tg;
7420
7421err:
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007422 sched_free_group(tg);
Li Zefanace783b2013-01-24 14:30:48 +08007423 return ERR_PTR(-ENOMEM);
7424}
7425
7426void sched_online_group(struct task_group *tg, struct task_group *parent)
7427{
7428 unsigned long flags;
7429
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007430 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007431 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007432
Ingo Molnard1ccc662017-02-01 11:46:42 +01007433 /* Root should already exist: */
7434 WARN_ON(!parent);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007435
7436 tg->parent = parent;
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007437 INIT_LIST_HEAD(&tg->children);
Zhang, Yanmin09f27242030-08-14 15:56:40 +08007438 list_add_rcu(&tg->siblings, &parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007439 spin_unlock_irqrestore(&task_group_lock, flags);
Peter Zijlstra8663e242016-06-22 14:58:02 +02007440
7441 online_fair_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007442}
7443
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007444/* rcu callback to free various structures associated with a task group */
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007445static void sched_free_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007446{
Ingo Molnard1ccc662017-02-01 11:46:42 +01007447 /* Now it should be safe to free those cfs_rqs: */
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007448 sched_free_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007449}
7450
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007451void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007452{
Ingo Molnard1ccc662017-02-01 11:46:42 +01007453 /* Wait for possible concurrent references to cfs_rqs complete: */
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007454 call_rcu(&tg->rcu, sched_free_group_rcu);
Li Zefanace783b2013-01-24 14:30:48 +08007455}
7456
7457void sched_offline_group(struct task_group *tg)
7458{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007459 unsigned long flags;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007460
Ingo Molnard1ccc662017-02-01 11:46:42 +01007461 /* End participation in shares distribution: */
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01007462 unregister_fair_sched_group(tg);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08007463
7464 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007465 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007466 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007467 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007468}
7469
Vincent Guittotea86cb42016-06-17 13:38:55 +02007470static void sched_change_group(struct task_struct *tsk, int type)
7471{
7472 struct task_group *tg;
7473
7474 /*
7475 * All callers are synchronized by task_rq_lock(); we do not use RCU
7476 * which is pointless here. Thus, we pass "true" to task_css_check()
7477 * to prevent lockdep warnings.
7478 */
7479 tg = container_of(task_css_check(tsk, cpu_cgrp_id, true),
7480 struct task_group, css);
7481 tg = autogroup_task_group(tsk, tg);
7482 tsk->sched_task_group = tg;
7483
7484#ifdef CONFIG_FAIR_GROUP_SCHED
7485 if (tsk->sched_class->task_change_group)
7486 tsk->sched_class->task_change_group(tsk, type);
7487 else
7488#endif
7489 set_task_rq(tsk, task_cpu(tsk));
7490}
7491
7492/*
7493 * Change task's runqueue when it moves between groups.
7494 *
7495 * The caller of this function should have put the task in its new group by
7496 * now. This function just updates tsk->se.cfs_rq and tsk->se.parent to reflect
7497 * its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007498 */
7499void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007500{
Peter Zijlstra7a57f322017-02-21 14:47:02 +01007501 int queued, running, queue_flags =
7502 DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK;
Peter Zijlstraeb580752015-07-31 21:28:18 +02007503 struct rq_flags rf;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007504 struct rq *rq;
7505
Peter Zijlstraeb580752015-07-31 21:28:18 +02007506 rq = task_rq_lock(tsk, &rf);
Peter Zijlstra1b1d6222017-01-23 16:05:55 +01007507 update_rq_clock(rq);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007508
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01007509 running = task_current(rq, tsk);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007510 queued = task_on_rq_queued(tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007511
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007512 if (queued)
Peter Zijlstra7a57f322017-02-21 14:47:02 +01007513 dequeue_task(rq, tsk, queue_flags);
Steven Rostedt (VMware)bb3bac22017-02-06 11:04:26 -05007514 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04007515 put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007516
Vincent Guittotea86cb42016-06-17 13:38:55 +02007517 sched_change_group(tsk, TASK_MOVE_GROUP);
Peter Zijlstra810b3812008-02-29 15:21:01 -05007518
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007519 if (queued)
Peter Zijlstra7a57f322017-02-21 14:47:02 +01007520 enqueue_task(rq, tsk, queue_flags);
Vincent Guittot2a4b03f2020-01-14 15:13:56 +01007521 if (running) {
Peter Zijlstra03b7fad2019-05-29 20:36:41 +00007522 set_next_task(rq, tsk);
Vincent Guittot2a4b03f2020-01-14 15:13:56 +01007523 /*
7524 * After changing group, the running task may have joined a
7525 * throttled one but it's still the running task. Trigger a
7526 * resched to make sure that task can still run.
7527 */
7528 resched_curr(rq);
7529 }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007530
Peter Zijlstraeb580752015-07-31 21:28:18 +02007531 task_rq_unlock(rq, tsk, &rf);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007532}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007533
Tejun Heoa7c6d552013-08-08 20:11:23 -04007534static inline struct task_group *css_tg(struct cgroup_subsys_state *css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007535{
Tejun Heoa7c6d552013-08-08 20:11:23 -04007536 return css ? container_of(css, struct task_group, css) : NULL;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007537}
7538
Tejun Heoeb954192013-08-08 20:11:23 -04007539static struct cgroup_subsys_state *
7540cpu_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007541{
Tejun Heoeb954192013-08-08 20:11:23 -04007542 struct task_group *parent = css_tg(parent_css);
7543 struct task_group *tg;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007544
Tejun Heoeb954192013-08-08 20:11:23 -04007545 if (!parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007546 /* This is early initialization for the top cgroup */
Yong Zhang07e06b02011-01-07 15:17:36 +08007547 return &root_task_group.css;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007548 }
7549
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007550 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007551 if (IS_ERR(tg))
7552 return ERR_PTR(-ENOMEM);
7553
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007554 return &tg->css;
7555}
7556
Konstantin Khlebnikov96b77742017-02-08 14:27:27 +03007557/* Expose task group only after completing cgroup initialization */
7558static int cpu_cgroup_css_online(struct cgroup_subsys_state *css)
7559{
7560 struct task_group *tg = css_tg(css);
7561 struct task_group *parent = css_tg(css->parent);
7562
7563 if (parent)
7564 sched_online_group(tg, parent);
Qais Yousef72260172019-12-24 11:54:04 +00007565
7566#ifdef CONFIG_UCLAMP_TASK_GROUP
7567 /* Propagate the effective uclamp value for the new group */
7568 cpu_util_update_eff(css);
7569#endif
7570
Konstantin Khlebnikov96b77742017-02-08 14:27:27 +03007571 return 0;
7572}
7573
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007574static void cpu_cgroup_css_released(struct cgroup_subsys_state *css)
Li Zefanace783b2013-01-24 14:30:48 +08007575{
Tejun Heoeb954192013-08-08 20:11:23 -04007576 struct task_group *tg = css_tg(css);
Li Zefanace783b2013-01-24 14:30:48 +08007577
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007578 sched_offline_group(tg);
Li Zefanace783b2013-01-24 14:30:48 +08007579}
7580
Tejun Heoeb954192013-08-08 20:11:23 -04007581static void cpu_cgroup_css_free(struct cgroup_subsys_state *css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007582{
Tejun Heoeb954192013-08-08 20:11:23 -04007583 struct task_group *tg = css_tg(css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007584
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007585 /*
7586 * Relies on the RCU grace period between css_released() and this.
7587 */
7588 sched_free_group(tg);
Li Zefanace783b2013-01-24 14:30:48 +08007589}
7590
Vincent Guittotea86cb42016-06-17 13:38:55 +02007591/*
7592 * This is called before wake_up_new_task(), therefore we really only
7593 * have to set its group bits, all the other stuff does not apply.
7594 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05007595static void cpu_cgroup_fork(struct task_struct *task)
Kirill Tkhaieeb61e52014-10-27 14:18:25 +04007596{
Vincent Guittotea86cb42016-06-17 13:38:55 +02007597 struct rq_flags rf;
7598 struct rq *rq;
7599
7600 rq = task_rq_lock(task, &rf);
7601
Peter Zijlstra80f5c1b2016-10-03 16:28:37 +02007602 update_rq_clock(rq);
Vincent Guittotea86cb42016-06-17 13:38:55 +02007603 sched_change_group(task, TASK_SET_GROUP);
7604
7605 task_rq_unlock(rq, task, &rf);
Kirill Tkhaieeb61e52014-10-27 14:18:25 +04007606}
7607
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05007608static int cpu_cgroup_can_attach(struct cgroup_taskset *tset)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007609{
Tejun Heobb9d97b2011-12-12 18:12:21 -08007610 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05007611 struct cgroup_subsys_state *css;
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02007612 int ret = 0;
Tejun Heobb9d97b2011-12-12 18:12:21 -08007613
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05007614 cgroup_taskset_for_each(task, css, tset) {
Peter Zijlstrab68aa232008-02-13 15:45:40 +01007615#ifdef CONFIG_RT_GROUP_SCHED
Tejun Heoeb954192013-08-08 20:11:23 -04007616 if (!sched_rt_can_attach(css_tg(css), task))
Tejun Heobb9d97b2011-12-12 18:12:21 -08007617 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01007618#endif
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02007619 /*
7620 * Serialize against wake_up_new_task() such that if its
7621 * running, we're sure to observe its full state.
7622 */
7623 raw_spin_lock_irq(&task->pi_lock);
7624 /*
7625 * Avoid calling sched_move_task() before wake_up_new_task()
7626 * has happened. This would lead to problems with PELT, due to
7627 * move wanting to detach+attach while we're not attached yet.
7628 */
7629 if (task->state == TASK_NEW)
7630 ret = -EINVAL;
7631 raw_spin_unlock_irq(&task->pi_lock);
7632
7633 if (ret)
7634 break;
Tejun Heobb9d97b2011-12-12 18:12:21 -08007635 }
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02007636 return ret;
Ben Blumbe367d02009-09-23 15:56:31 -07007637}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007638
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05007639static void cpu_cgroup_attach(struct cgroup_taskset *tset)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007640{
Tejun Heobb9d97b2011-12-12 18:12:21 -08007641 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05007642 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -08007643
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05007644 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08007645 sched_move_task(task);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007646}
7647
Patrick Bellasi2480c092019-08-22 14:28:06 +01007648#ifdef CONFIG_UCLAMP_TASK_GROUP
Patrick Bellasi0b60ba22019-08-22 14:28:07 +01007649static void cpu_util_update_eff(struct cgroup_subsys_state *css)
7650{
7651 struct cgroup_subsys_state *top_css = css;
7652 struct uclamp_se *uc_parent = NULL;
7653 struct uclamp_se *uc_se = NULL;
7654 unsigned int eff[UCLAMP_CNT];
Patrick Bellasi0413d7f2019-08-22 14:28:11 +01007655 enum uclamp_id clamp_id;
Patrick Bellasi0b60ba22019-08-22 14:28:07 +01007656 unsigned int clamps;
7657
7658 css_for_each_descendant_pre(css, top_css) {
7659 uc_parent = css_tg(css)->parent
7660 ? css_tg(css)->parent->uclamp : NULL;
7661
7662 for_each_clamp_id(clamp_id) {
7663 /* Assume effective clamps matches requested clamps */
7664 eff[clamp_id] = css_tg(css)->uclamp_req[clamp_id].value;
7665 /* Cap effective clamps with parent's effective clamps */
7666 if (uc_parent &&
7667 eff[clamp_id] > uc_parent[clamp_id].value) {
7668 eff[clamp_id] = uc_parent[clamp_id].value;
7669 }
7670 }
7671 /* Ensure protection is always capped by limit */
7672 eff[UCLAMP_MIN] = min(eff[UCLAMP_MIN], eff[UCLAMP_MAX]);
7673
7674 /* Propagate most restrictive effective clamps */
7675 clamps = 0x0;
7676 uc_se = css_tg(css)->uclamp;
7677 for_each_clamp_id(clamp_id) {
7678 if (eff[clamp_id] == uc_se[clamp_id].value)
7679 continue;
7680 uc_se[clamp_id].value = eff[clamp_id];
7681 uc_se[clamp_id].bucket_id = uclamp_bucket_id(eff[clamp_id]);
7682 clamps |= (0x1 << clamp_id);
7683 }
Patrick Bellasibabbe172019-08-22 14:28:10 +01007684 if (!clamps) {
Patrick Bellasi0b60ba22019-08-22 14:28:07 +01007685 css = css_rightmost_descendant(css);
Patrick Bellasibabbe172019-08-22 14:28:10 +01007686 continue;
7687 }
7688
7689 /* Immediately update descendants RUNNABLE tasks */
7690 uclamp_update_active_tasks(css, clamps);
Patrick Bellasi0b60ba22019-08-22 14:28:07 +01007691 }
7692}
Patrick Bellasi2480c092019-08-22 14:28:06 +01007693
7694/*
7695 * Integer 10^N with a given N exponent by casting to integer the literal "1eN"
7696 * C expression. Since there is no way to convert a macro argument (N) into a
7697 * character constant, use two levels of macros.
7698 */
7699#define _POW10(exp) ((unsigned int)1e##exp)
7700#define POW10(exp) _POW10(exp)
7701
7702struct uclamp_request {
7703#define UCLAMP_PERCENT_SHIFT 2
7704#define UCLAMP_PERCENT_SCALE (100 * POW10(UCLAMP_PERCENT_SHIFT))
7705 s64 percent;
7706 u64 util;
7707 int ret;
7708};
7709
7710static inline struct uclamp_request
7711capacity_from_percent(char *buf)
7712{
7713 struct uclamp_request req = {
7714 .percent = UCLAMP_PERCENT_SCALE,
7715 .util = SCHED_CAPACITY_SCALE,
7716 .ret = 0,
7717 };
7718
7719 buf = strim(buf);
7720 if (strcmp(buf, "max")) {
7721 req.ret = cgroup_parse_float(buf, UCLAMP_PERCENT_SHIFT,
7722 &req.percent);
7723 if (req.ret)
7724 return req;
Qais Yousefb562d142020-01-14 21:09:47 +00007725 if ((u64)req.percent > UCLAMP_PERCENT_SCALE) {
Patrick Bellasi2480c092019-08-22 14:28:06 +01007726 req.ret = -ERANGE;
7727 return req;
7728 }
7729
7730 req.util = req.percent << SCHED_CAPACITY_SHIFT;
7731 req.util = DIV_ROUND_CLOSEST_ULL(req.util, UCLAMP_PERCENT_SCALE);
7732 }
7733
7734 return req;
7735}
7736
7737static ssize_t cpu_uclamp_write(struct kernfs_open_file *of, char *buf,
7738 size_t nbytes, loff_t off,
7739 enum uclamp_id clamp_id)
7740{
7741 struct uclamp_request req;
7742 struct task_group *tg;
7743
7744 req = capacity_from_percent(buf);
7745 if (req.ret)
7746 return req.ret;
7747
Qais Yousef46609ce2020-06-30 12:21:23 +01007748 static_branch_enable(&sched_uclamp_used);
7749
Patrick Bellasi2480c092019-08-22 14:28:06 +01007750 mutex_lock(&uclamp_mutex);
7751 rcu_read_lock();
7752
7753 tg = css_tg(of_css(of));
7754 if (tg->uclamp_req[clamp_id].value != req.util)
7755 uclamp_se_set(&tg->uclamp_req[clamp_id], req.util, false);
7756
7757 /*
7758 * Because of not recoverable conversion rounding we keep track of the
7759 * exact requested value
7760 */
7761 tg->uclamp_pct[clamp_id] = req.percent;
7762
Patrick Bellasi0b60ba22019-08-22 14:28:07 +01007763 /* Update effective clamps to track the most restrictive value */
7764 cpu_util_update_eff(of_css(of));
7765
Patrick Bellasi2480c092019-08-22 14:28:06 +01007766 rcu_read_unlock();
7767 mutex_unlock(&uclamp_mutex);
7768
7769 return nbytes;
7770}
7771
7772static ssize_t cpu_uclamp_min_write(struct kernfs_open_file *of,
7773 char *buf, size_t nbytes,
7774 loff_t off)
7775{
7776 return cpu_uclamp_write(of, buf, nbytes, off, UCLAMP_MIN);
7777}
7778
7779static ssize_t cpu_uclamp_max_write(struct kernfs_open_file *of,
7780 char *buf, size_t nbytes,
7781 loff_t off)
7782{
7783 return cpu_uclamp_write(of, buf, nbytes, off, UCLAMP_MAX);
7784}
7785
7786static inline void cpu_uclamp_print(struct seq_file *sf,
7787 enum uclamp_id clamp_id)
7788{
7789 struct task_group *tg;
7790 u64 util_clamp;
7791 u64 percent;
7792 u32 rem;
7793
7794 rcu_read_lock();
7795 tg = css_tg(seq_css(sf));
7796 util_clamp = tg->uclamp_req[clamp_id].value;
7797 rcu_read_unlock();
7798
7799 if (util_clamp == SCHED_CAPACITY_SCALE) {
7800 seq_puts(sf, "max\n");
7801 return;
7802 }
7803
7804 percent = tg->uclamp_pct[clamp_id];
7805 percent = div_u64_rem(percent, POW10(UCLAMP_PERCENT_SHIFT), &rem);
7806 seq_printf(sf, "%llu.%0*u\n", percent, UCLAMP_PERCENT_SHIFT, rem);
7807}
7808
7809static int cpu_uclamp_min_show(struct seq_file *sf, void *v)
7810{
7811 cpu_uclamp_print(sf, UCLAMP_MIN);
7812 return 0;
7813}
7814
7815static int cpu_uclamp_max_show(struct seq_file *sf, void *v)
7816{
7817 cpu_uclamp_print(sf, UCLAMP_MAX);
7818 return 0;
7819}
7820#endif /* CONFIG_UCLAMP_TASK_GROUP */
7821
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007822#ifdef CONFIG_FAIR_GROUP_SCHED
Tejun Heo182446d2013-08-08 20:11:24 -04007823static int cpu_shares_write_u64(struct cgroup_subsys_state *css,
7824 struct cftype *cftype, u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007825{
Konstantin Khlebnikov5b61d502019-02-27 11:10:18 +03007826 if (shareval > scale_load_down(ULONG_MAX))
7827 shareval = MAX_SHARES;
Tejun Heo182446d2013-08-08 20:11:24 -04007828 return sched_group_set_shares(css_tg(css), scale_load(shareval));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007829}
7830
Tejun Heo182446d2013-08-08 20:11:24 -04007831static u64 cpu_shares_read_u64(struct cgroup_subsys_state *css,
7832 struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007833{
Tejun Heo182446d2013-08-08 20:11:24 -04007834 struct task_group *tg = css_tg(css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007835
Nikhil Raoc8b28112011-05-18 14:37:48 -07007836 return (u64) scale_load_down(tg->shares);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007837}
Paul Turnerab84d312011-07-21 09:43:28 -07007838
7839#ifdef CONFIG_CFS_BANDWIDTH
Paul Turnera790de92011-07-21 09:43:29 -07007840static DEFINE_MUTEX(cfs_constraints_mutex);
7841
Paul Turnerab84d312011-07-21 09:43:28 -07007842const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */
YueHaibingb1546ed2019-04-18 22:47:13 +08007843static const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */
Huaixin Changd505b8a2020-04-25 18:52:48 +08007844/* More than 203 days if BW_SHIFT equals 20. */
7845static const u64 max_cfs_runtime = MAX_BW * NSEC_PER_USEC;
Paul Turnerab84d312011-07-21 09:43:28 -07007846
Paul Turnera790de92011-07-21 09:43:29 -07007847static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime);
7848
Paul Turnerab84d312011-07-21 09:43:28 -07007849static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota)
7850{
Paul Turner56f570e2011-11-07 20:26:33 -08007851 int i, ret = 0, runtime_enabled, runtime_was_enabled;
Peter Zijlstra029632f2011-10-25 10:00:11 +02007852 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Paul Turnerab84d312011-07-21 09:43:28 -07007853
7854 if (tg == &root_task_group)
7855 return -EINVAL;
7856
7857 /*
7858 * Ensure we have at some amount of bandwidth every period. This is
7859 * to prevent reaching a state of large arrears when throttled via
7860 * entity_tick() resulting in prolonged exit starvation.
7861 */
7862 if (quota < min_cfs_quota_period || period < min_cfs_quota_period)
7863 return -EINVAL;
7864
7865 /*
7866 * Likewise, bound things on the otherside by preventing insane quota
7867 * periods. This also allows us to normalize in computing quota
7868 * feasibility.
7869 */
7870 if (period > max_cfs_quota_period)
7871 return -EINVAL;
7872
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04007873 /*
Huaixin Changd505b8a2020-04-25 18:52:48 +08007874 * Bound quota to defend quota against overflow during bandwidth shift.
7875 */
7876 if (quota != RUNTIME_INF && quota > max_cfs_runtime)
7877 return -EINVAL;
7878
7879 /*
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04007880 * Prevent race between setting of cfs_rq->runtime_enabled and
7881 * unthrottle_offline_cfs_rqs().
7882 */
7883 get_online_cpus();
Paul Turnera790de92011-07-21 09:43:29 -07007884 mutex_lock(&cfs_constraints_mutex);
7885 ret = __cfs_schedulable(tg, period, quota);
7886 if (ret)
7887 goto out_unlock;
7888
Paul Turner58088ad2011-07-21 09:43:31 -07007889 runtime_enabled = quota != RUNTIME_INF;
Paul Turner56f570e2011-11-07 20:26:33 -08007890 runtime_was_enabled = cfs_b->quota != RUNTIME_INF;
Ben Segall1ee14e62013-10-16 11:16:12 -07007891 /*
7892 * If we need to toggle cfs_bandwidth_used, off->on must occur
7893 * before making related changes, and on->off must occur afterwards
7894 */
7895 if (runtime_enabled && !runtime_was_enabled)
7896 cfs_bandwidth_usage_inc();
Paul Turnerab84d312011-07-21 09:43:28 -07007897 raw_spin_lock_irq(&cfs_b->lock);
7898 cfs_b->period = ns_to_ktime(period);
7899 cfs_b->quota = quota;
Paul Turner58088ad2011-07-21 09:43:31 -07007900
Paul Turnera9cf55b2011-07-21 09:43:32 -07007901 __refill_cfs_bandwidth_runtime(cfs_b);
Ingo Molnard1ccc662017-02-01 11:46:42 +01007902
7903 /* Restart the period timer (if active) to handle new period expiry: */
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02007904 if (runtime_enabled)
7905 start_cfs_bandwidth(cfs_b);
Ingo Molnard1ccc662017-02-01 11:46:42 +01007906
Paul Turnerab84d312011-07-21 09:43:28 -07007907 raw_spin_unlock_irq(&cfs_b->lock);
7908
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04007909 for_each_online_cpu(i) {
Paul Turnerab84d312011-07-21 09:43:28 -07007910 struct cfs_rq *cfs_rq = tg->cfs_rq[i];
Peter Zijlstra029632f2011-10-25 10:00:11 +02007911 struct rq *rq = cfs_rq->rq;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02007912 struct rq_flags rf;
Paul Turnerab84d312011-07-21 09:43:28 -07007913
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02007914 rq_lock_irq(rq, &rf);
Paul Turner58088ad2011-07-21 09:43:31 -07007915 cfs_rq->runtime_enabled = runtime_enabled;
Paul Turnerab84d312011-07-21 09:43:28 -07007916 cfs_rq->runtime_remaining = 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07007917
Peter Zijlstra029632f2011-10-25 10:00:11 +02007918 if (cfs_rq->throttled)
Paul Turner671fd9d2011-07-21 09:43:34 -07007919 unthrottle_cfs_rq(cfs_rq);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02007920 rq_unlock_irq(rq, &rf);
Paul Turnerab84d312011-07-21 09:43:28 -07007921 }
Ben Segall1ee14e62013-10-16 11:16:12 -07007922 if (runtime_was_enabled && !runtime_enabled)
7923 cfs_bandwidth_usage_dec();
Paul Turnera790de92011-07-21 09:43:29 -07007924out_unlock:
7925 mutex_unlock(&cfs_constraints_mutex);
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04007926 put_online_cpus();
Paul Turnerab84d312011-07-21 09:43:28 -07007927
Paul Turnera790de92011-07-21 09:43:29 -07007928 return ret;
Paul Turnerab84d312011-07-21 09:43:28 -07007929}
7930
YueHaibingb1546ed2019-04-18 22:47:13 +08007931static int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us)
Paul Turnerab84d312011-07-21 09:43:28 -07007932{
7933 u64 quota, period;
7934
Peter Zijlstra029632f2011-10-25 10:00:11 +02007935 period = ktime_to_ns(tg->cfs_bandwidth.period);
Paul Turnerab84d312011-07-21 09:43:28 -07007936 if (cfs_quota_us < 0)
7937 quota = RUNTIME_INF;
Konstantin Khlebnikov1a8b4542019-02-27 11:10:20 +03007938 else if ((u64)cfs_quota_us <= U64_MAX / NSEC_PER_USEC)
Paul Turnerab84d312011-07-21 09:43:28 -07007939 quota = (u64)cfs_quota_us * NSEC_PER_USEC;
Konstantin Khlebnikov1a8b4542019-02-27 11:10:20 +03007940 else
7941 return -EINVAL;
Paul Turnerab84d312011-07-21 09:43:28 -07007942
7943 return tg_set_cfs_bandwidth(tg, period, quota);
7944}
7945
YueHaibingb1546ed2019-04-18 22:47:13 +08007946static long tg_get_cfs_quota(struct task_group *tg)
Paul Turnerab84d312011-07-21 09:43:28 -07007947{
7948 u64 quota_us;
7949
Peter Zijlstra029632f2011-10-25 10:00:11 +02007950 if (tg->cfs_bandwidth.quota == RUNTIME_INF)
Paul Turnerab84d312011-07-21 09:43:28 -07007951 return -1;
7952
Peter Zijlstra029632f2011-10-25 10:00:11 +02007953 quota_us = tg->cfs_bandwidth.quota;
Paul Turnerab84d312011-07-21 09:43:28 -07007954 do_div(quota_us, NSEC_PER_USEC);
7955
7956 return quota_us;
7957}
7958
YueHaibingb1546ed2019-04-18 22:47:13 +08007959static int tg_set_cfs_period(struct task_group *tg, long cfs_period_us)
Paul Turnerab84d312011-07-21 09:43:28 -07007960{
7961 u64 quota, period;
7962
Konstantin Khlebnikov1a8b4542019-02-27 11:10:20 +03007963 if ((u64)cfs_period_us > U64_MAX / NSEC_PER_USEC)
7964 return -EINVAL;
7965
Paul Turnerab84d312011-07-21 09:43:28 -07007966 period = (u64)cfs_period_us * NSEC_PER_USEC;
Peter Zijlstra029632f2011-10-25 10:00:11 +02007967 quota = tg->cfs_bandwidth.quota;
Paul Turnerab84d312011-07-21 09:43:28 -07007968
Paul Turnerab84d312011-07-21 09:43:28 -07007969 return tg_set_cfs_bandwidth(tg, period, quota);
7970}
7971
YueHaibingb1546ed2019-04-18 22:47:13 +08007972static long tg_get_cfs_period(struct task_group *tg)
Paul Turnerab84d312011-07-21 09:43:28 -07007973{
7974 u64 cfs_period_us;
7975
Peter Zijlstra029632f2011-10-25 10:00:11 +02007976 cfs_period_us = ktime_to_ns(tg->cfs_bandwidth.period);
Paul Turnerab84d312011-07-21 09:43:28 -07007977 do_div(cfs_period_us, NSEC_PER_USEC);
7978
7979 return cfs_period_us;
7980}
7981
Tejun Heo182446d2013-08-08 20:11:24 -04007982static s64 cpu_cfs_quota_read_s64(struct cgroup_subsys_state *css,
7983 struct cftype *cft)
Paul Turnerab84d312011-07-21 09:43:28 -07007984{
Tejun Heo182446d2013-08-08 20:11:24 -04007985 return tg_get_cfs_quota(css_tg(css));
Paul Turnerab84d312011-07-21 09:43:28 -07007986}
7987
Tejun Heo182446d2013-08-08 20:11:24 -04007988static int cpu_cfs_quota_write_s64(struct cgroup_subsys_state *css,
7989 struct cftype *cftype, s64 cfs_quota_us)
Paul Turnerab84d312011-07-21 09:43:28 -07007990{
Tejun Heo182446d2013-08-08 20:11:24 -04007991 return tg_set_cfs_quota(css_tg(css), cfs_quota_us);
Paul Turnerab84d312011-07-21 09:43:28 -07007992}
7993
Tejun Heo182446d2013-08-08 20:11:24 -04007994static u64 cpu_cfs_period_read_u64(struct cgroup_subsys_state *css,
7995 struct cftype *cft)
Paul Turnerab84d312011-07-21 09:43:28 -07007996{
Tejun Heo182446d2013-08-08 20:11:24 -04007997 return tg_get_cfs_period(css_tg(css));
Paul Turnerab84d312011-07-21 09:43:28 -07007998}
7999
Tejun Heo182446d2013-08-08 20:11:24 -04008000static int cpu_cfs_period_write_u64(struct cgroup_subsys_state *css,
8001 struct cftype *cftype, u64 cfs_period_us)
Paul Turnerab84d312011-07-21 09:43:28 -07008002{
Tejun Heo182446d2013-08-08 20:11:24 -04008003 return tg_set_cfs_period(css_tg(css), cfs_period_us);
Paul Turnerab84d312011-07-21 09:43:28 -07008004}
8005
Paul Turnera790de92011-07-21 09:43:29 -07008006struct cfs_schedulable_data {
8007 struct task_group *tg;
8008 u64 period, quota;
8009};
8010
8011/*
8012 * normalize group quota/period to be quota/max_period
8013 * note: units are usecs
8014 */
8015static u64 normalize_cfs_quota(struct task_group *tg,
8016 struct cfs_schedulable_data *d)
8017{
8018 u64 quota, period;
8019
8020 if (tg == d->tg) {
8021 period = d->period;
8022 quota = d->quota;
8023 } else {
8024 period = tg_get_cfs_period(tg);
8025 quota = tg_get_cfs_quota(tg);
8026 }
8027
8028 /* note: these should typically be equivalent */
8029 if (quota == RUNTIME_INF || quota == -1)
8030 return RUNTIME_INF;
8031
8032 return to_ratio(period, quota);
8033}
8034
8035static int tg_cfs_schedulable_down(struct task_group *tg, void *data)
8036{
8037 struct cfs_schedulable_data *d = data;
Peter Zijlstra029632f2011-10-25 10:00:11 +02008038 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Paul Turnera790de92011-07-21 09:43:29 -07008039 s64 quota = 0, parent_quota = -1;
8040
8041 if (!tg->parent) {
8042 quota = RUNTIME_INF;
8043 } else {
Peter Zijlstra029632f2011-10-25 10:00:11 +02008044 struct cfs_bandwidth *parent_b = &tg->parent->cfs_bandwidth;
Paul Turnera790de92011-07-21 09:43:29 -07008045
8046 quota = normalize_cfs_quota(tg, d);
Zhihui Zhang9c58c792014-09-20 21:24:36 -04008047 parent_quota = parent_b->hierarchical_quota;
Paul Turnera790de92011-07-21 09:43:29 -07008048
8049 /*
Tejun Heoc53593e2018-01-22 11:26:18 -08008050 * Ensure max(child_quota) <= parent_quota. On cgroup2,
8051 * always take the min. On cgroup1, only inherit when no
Ingo Molnard1ccc662017-02-01 11:46:42 +01008052 * limit is set:
Paul Turnera790de92011-07-21 09:43:29 -07008053 */
Tejun Heoc53593e2018-01-22 11:26:18 -08008054 if (cgroup_subsys_on_dfl(cpu_cgrp_subsys)) {
8055 quota = min(quota, parent_quota);
8056 } else {
8057 if (quota == RUNTIME_INF)
8058 quota = parent_quota;
8059 else if (parent_quota != RUNTIME_INF && quota > parent_quota)
8060 return -EINVAL;
8061 }
Paul Turnera790de92011-07-21 09:43:29 -07008062 }
Zhihui Zhang9c58c792014-09-20 21:24:36 -04008063 cfs_b->hierarchical_quota = quota;
Paul Turnera790de92011-07-21 09:43:29 -07008064
8065 return 0;
8066}
8067
8068static int __cfs_schedulable(struct task_group *tg, u64 period, u64 quota)
8069{
Paul Turner82774342011-07-21 09:43:35 -07008070 int ret;
Paul Turnera790de92011-07-21 09:43:29 -07008071 struct cfs_schedulable_data data = {
8072 .tg = tg,
8073 .period = period,
8074 .quota = quota,
8075 };
8076
8077 if (quota != RUNTIME_INF) {
8078 do_div(data.period, NSEC_PER_USEC);
8079 do_div(data.quota, NSEC_PER_USEC);
8080 }
8081
Paul Turner82774342011-07-21 09:43:35 -07008082 rcu_read_lock();
8083 ret = walk_tg_tree(tg_cfs_schedulable_down, tg_nop, &data);
8084 rcu_read_unlock();
8085
8086 return ret;
Paul Turnera790de92011-07-21 09:43:29 -07008087}
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008088
Tejun Heoa1f71642017-09-25 09:00:18 -07008089static int cpu_cfs_stat_show(struct seq_file *sf, void *v)
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008090{
Tejun Heo2da8ca82013-12-05 12:28:04 -05008091 struct task_group *tg = css_tg(seq_css(sf));
Peter Zijlstra029632f2011-10-25 10:00:11 +02008092 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008093
Tejun Heo44ffc752013-12-05 12:28:01 -05008094 seq_printf(sf, "nr_periods %d\n", cfs_b->nr_periods);
8095 seq_printf(sf, "nr_throttled %d\n", cfs_b->nr_throttled);
8096 seq_printf(sf, "throttled_time %llu\n", cfs_b->throttled_time);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008097
Yun Wang3d6c50c2018-07-04 11:27:27 +08008098 if (schedstat_enabled() && tg != &root_task_group) {
8099 u64 ws = 0;
8100 int i;
8101
8102 for_each_possible_cpu(i)
8103 ws += schedstat_val(tg->se[i]->statistics.wait_sum);
8104
8105 seq_printf(sf, "wait_sum %llu\n", ws);
8106 }
8107
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008108 return 0;
8109}
Paul Turnerab84d312011-07-21 09:43:28 -07008110#endif /* CONFIG_CFS_BANDWIDTH */
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008111#endif /* CONFIG_FAIR_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008112
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008113#ifdef CONFIG_RT_GROUP_SCHED
Tejun Heo182446d2013-08-08 20:11:24 -04008114static int cpu_rt_runtime_write(struct cgroup_subsys_state *css,
8115 struct cftype *cft, s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008116{
Tejun Heo182446d2013-08-08 20:11:24 -04008117 return sched_group_set_rt_runtime(css_tg(css), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008118}
8119
Tejun Heo182446d2013-08-08 20:11:24 -04008120static s64 cpu_rt_runtime_read(struct cgroup_subsys_state *css,
8121 struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008122{
Tejun Heo182446d2013-08-08 20:11:24 -04008123 return sched_group_rt_runtime(css_tg(css));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008124}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008125
Tejun Heo182446d2013-08-08 20:11:24 -04008126static int cpu_rt_period_write_uint(struct cgroup_subsys_state *css,
8127 struct cftype *cftype, u64 rt_period_us)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008128{
Tejun Heo182446d2013-08-08 20:11:24 -04008129 return sched_group_set_rt_period(css_tg(css), rt_period_us);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008130}
8131
Tejun Heo182446d2013-08-08 20:11:24 -04008132static u64 cpu_rt_period_read_uint(struct cgroup_subsys_state *css,
8133 struct cftype *cft)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008134{
Tejun Heo182446d2013-08-08 20:11:24 -04008135 return sched_group_rt_period(css_tg(css));
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008136}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02008137#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008138
Tejun Heoa1f71642017-09-25 09:00:18 -07008139static struct cftype cpu_legacy_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008140#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008141 {
8142 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07008143 .read_u64 = cpu_shares_read_u64,
8144 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01008145 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008146#endif
Paul Turnerab84d312011-07-21 09:43:28 -07008147#ifdef CONFIG_CFS_BANDWIDTH
8148 {
8149 .name = "cfs_quota_us",
8150 .read_s64 = cpu_cfs_quota_read_s64,
8151 .write_s64 = cpu_cfs_quota_write_s64,
8152 },
8153 {
8154 .name = "cfs_period_us",
8155 .read_u64 = cpu_cfs_period_read_u64,
8156 .write_u64 = cpu_cfs_period_write_u64,
8157 },
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008158 {
8159 .name = "stat",
Tejun Heoa1f71642017-09-25 09:00:18 -07008160 .seq_show = cpu_cfs_stat_show,
Nikhil Raoe8da1b12011-07-21 09:43:40 -07008161 },
Paul Turnerab84d312011-07-21 09:43:28 -07008162#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008163#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008164 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01008165 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07008166 .read_s64 = cpu_rt_runtime_read,
8167 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01008168 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008169 {
8170 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07008171 .read_u64 = cpu_rt_period_read_uint,
8172 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02008173 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008174#endif
Patrick Bellasi2480c092019-08-22 14:28:06 +01008175#ifdef CONFIG_UCLAMP_TASK_GROUP
8176 {
8177 .name = "uclamp.min",
8178 .flags = CFTYPE_NOT_ON_ROOT,
8179 .seq_show = cpu_uclamp_min_show,
8180 .write = cpu_uclamp_min_write,
8181 },
8182 {
8183 .name = "uclamp.max",
8184 .flags = CFTYPE_NOT_ON_ROOT,
8185 .seq_show = cpu_uclamp_max_show,
8186 .write = cpu_uclamp_max_write,
8187 },
8188#endif
Ingo Molnard1ccc662017-02-01 11:46:42 +01008189 { } /* Terminate */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008190};
8191
Tejun Heod41bf8c2017-10-23 16:18:27 -07008192static int cpu_extra_stat_show(struct seq_file *sf,
8193 struct cgroup_subsys_state *css)
Tejun Heo0d593632017-09-25 09:00:19 -07008194{
Tejun Heo0d593632017-09-25 09:00:19 -07008195#ifdef CONFIG_CFS_BANDWIDTH
8196 {
Tejun Heod41bf8c2017-10-23 16:18:27 -07008197 struct task_group *tg = css_tg(css);
Tejun Heo0d593632017-09-25 09:00:19 -07008198 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
8199 u64 throttled_usec;
8200
8201 throttled_usec = cfs_b->throttled_time;
8202 do_div(throttled_usec, NSEC_PER_USEC);
8203
8204 seq_printf(sf, "nr_periods %d\n"
8205 "nr_throttled %d\n"
8206 "throttled_usec %llu\n",
8207 cfs_b->nr_periods, cfs_b->nr_throttled,
8208 throttled_usec);
8209 }
8210#endif
8211 return 0;
8212}
8213
8214#ifdef CONFIG_FAIR_GROUP_SCHED
8215static u64 cpu_weight_read_u64(struct cgroup_subsys_state *css,
8216 struct cftype *cft)
8217{
8218 struct task_group *tg = css_tg(css);
8219 u64 weight = scale_load_down(tg->shares);
8220
8221 return DIV_ROUND_CLOSEST_ULL(weight * CGROUP_WEIGHT_DFL, 1024);
8222}
8223
8224static int cpu_weight_write_u64(struct cgroup_subsys_state *css,
8225 struct cftype *cft, u64 weight)
8226{
8227 /*
8228 * cgroup weight knobs should use the common MIN, DFL and MAX
8229 * values which are 1, 100 and 10000 respectively. While it loses
8230 * a bit of range on both ends, it maps pretty well onto the shares
8231 * value used by scheduler and the round-trip conversions preserve
8232 * the original value over the entire range.
8233 */
8234 if (weight < CGROUP_WEIGHT_MIN || weight > CGROUP_WEIGHT_MAX)
8235 return -ERANGE;
8236
8237 weight = DIV_ROUND_CLOSEST_ULL(weight * 1024, CGROUP_WEIGHT_DFL);
8238
8239 return sched_group_set_shares(css_tg(css), scale_load(weight));
8240}
8241
8242static s64 cpu_weight_nice_read_s64(struct cgroup_subsys_state *css,
8243 struct cftype *cft)
8244{
8245 unsigned long weight = scale_load_down(css_tg(css)->shares);
8246 int last_delta = INT_MAX;
8247 int prio, delta;
8248
8249 /* find the closest nice value to the current weight */
8250 for (prio = 0; prio < ARRAY_SIZE(sched_prio_to_weight); prio++) {
8251 delta = abs(sched_prio_to_weight[prio] - weight);
8252 if (delta >= last_delta)
8253 break;
8254 last_delta = delta;
8255 }
8256
8257 return PRIO_TO_NICE(prio - 1 + MAX_RT_PRIO);
8258}
8259
8260static int cpu_weight_nice_write_s64(struct cgroup_subsys_state *css,
8261 struct cftype *cft, s64 nice)
8262{
8263 unsigned long weight;
Peter Zijlstra7281c8d2018-04-20 14:29:51 +02008264 int idx;
Tejun Heo0d593632017-09-25 09:00:19 -07008265
8266 if (nice < MIN_NICE || nice > MAX_NICE)
8267 return -ERANGE;
8268
Peter Zijlstra7281c8d2018-04-20 14:29:51 +02008269 idx = NICE_TO_PRIO(nice) - MAX_RT_PRIO;
8270 idx = array_index_nospec(idx, 40);
8271 weight = sched_prio_to_weight[idx];
8272
Tejun Heo0d593632017-09-25 09:00:19 -07008273 return sched_group_set_shares(css_tg(css), scale_load(weight));
8274}
8275#endif
8276
8277static void __maybe_unused cpu_period_quota_print(struct seq_file *sf,
8278 long period, long quota)
8279{
8280 if (quota < 0)
8281 seq_puts(sf, "max");
8282 else
8283 seq_printf(sf, "%ld", quota);
8284
8285 seq_printf(sf, " %ld\n", period);
8286}
8287
8288/* caller should put the current value in *@periodp before calling */
8289static int __maybe_unused cpu_period_quota_parse(char *buf,
8290 u64 *periodp, u64 *quotap)
8291{
8292 char tok[21]; /* U64_MAX */
8293
Konstantin Khlebnikov4c47acd2019-03-06 20:11:42 +03008294 if (sscanf(buf, "%20s %llu", tok, periodp) < 1)
Tejun Heo0d593632017-09-25 09:00:19 -07008295 return -EINVAL;
8296
8297 *periodp *= NSEC_PER_USEC;
8298
8299 if (sscanf(tok, "%llu", quotap))
8300 *quotap *= NSEC_PER_USEC;
8301 else if (!strcmp(tok, "max"))
8302 *quotap = RUNTIME_INF;
8303 else
8304 return -EINVAL;
8305
8306 return 0;
8307}
8308
8309#ifdef CONFIG_CFS_BANDWIDTH
8310static int cpu_max_show(struct seq_file *sf, void *v)
8311{
8312 struct task_group *tg = css_tg(seq_css(sf));
8313
8314 cpu_period_quota_print(sf, tg_get_cfs_period(tg), tg_get_cfs_quota(tg));
8315 return 0;
8316}
8317
8318static ssize_t cpu_max_write(struct kernfs_open_file *of,
8319 char *buf, size_t nbytes, loff_t off)
8320{
8321 struct task_group *tg = css_tg(of_css(of));
8322 u64 period = tg_get_cfs_period(tg);
8323 u64 quota;
8324 int ret;
8325
8326 ret = cpu_period_quota_parse(buf, &period, &quota);
8327 if (!ret)
8328 ret = tg_set_cfs_bandwidth(tg, period, quota);
8329 return ret ?: nbytes;
8330}
8331#endif
8332
8333static struct cftype cpu_files[] = {
Tejun Heo0d593632017-09-25 09:00:19 -07008334#ifdef CONFIG_FAIR_GROUP_SCHED
8335 {
8336 .name = "weight",
8337 .flags = CFTYPE_NOT_ON_ROOT,
8338 .read_u64 = cpu_weight_read_u64,
8339 .write_u64 = cpu_weight_write_u64,
8340 },
8341 {
8342 .name = "weight.nice",
8343 .flags = CFTYPE_NOT_ON_ROOT,
8344 .read_s64 = cpu_weight_nice_read_s64,
8345 .write_s64 = cpu_weight_nice_write_s64,
8346 },
8347#endif
8348#ifdef CONFIG_CFS_BANDWIDTH
8349 {
8350 .name = "max",
8351 .flags = CFTYPE_NOT_ON_ROOT,
8352 .seq_show = cpu_max_show,
8353 .write = cpu_max_write,
8354 },
8355#endif
Patrick Bellasi2480c092019-08-22 14:28:06 +01008356#ifdef CONFIG_UCLAMP_TASK_GROUP
8357 {
8358 .name = "uclamp.min",
8359 .flags = CFTYPE_NOT_ON_ROOT,
8360 .seq_show = cpu_uclamp_min_show,
8361 .write = cpu_uclamp_min_write,
8362 },
8363 {
8364 .name = "uclamp.max",
8365 .flags = CFTYPE_NOT_ON_ROOT,
8366 .seq_show = cpu_uclamp_max_show,
8367 .write = cpu_uclamp_max_write,
8368 },
8369#endif
Tejun Heo0d593632017-09-25 09:00:19 -07008370 { } /* terminate */
8371};
8372
Tejun Heo073219e2014-02-08 10:36:58 -05008373struct cgroup_subsys cpu_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08008374 .css_alloc = cpu_cgroup_css_alloc,
Konstantin Khlebnikov96b77742017-02-08 14:27:27 +03008375 .css_online = cpu_cgroup_css_online,
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01008376 .css_released = cpu_cgroup_css_released,
Tejun Heo92fb9742012-11-19 08:13:38 -08008377 .css_free = cpu_cgroup_css_free,
Tejun Heod41bf8c2017-10-23 16:18:27 -07008378 .css_extra_stat_show = cpu_extra_stat_show,
Kirill Tkhaieeb61e52014-10-27 14:18:25 +04008379 .fork = cpu_cgroup_fork,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008380 .can_attach = cpu_cgroup_can_attach,
8381 .attach = cpu_cgroup_attach,
Tejun Heoa1f71642017-09-25 09:00:18 -07008382 .legacy_cftypes = cpu_legacy_files,
Tejun Heo0d593632017-09-25 09:00:19 -07008383 .dfl_cftypes = cpu_files,
Tejun Heob38e42e2016-02-23 10:00:50 -05008384 .early_init = true,
Tejun Heo0d593632017-09-25 09:00:19 -07008385 .threaded = true,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008386};
8387
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008388#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008389
Paul E. McKenneyb637a322012-09-19 16:58:38 -07008390void dump_cpu_task(int cpu)
8391{
8392 pr_info("Task dump for CPU %d:\n", cpu);
8393 sched_show_task(cpu_curr(cpu));
8394}
Andi Kleened82b8a2015-11-29 20:59:43 -08008395
8396/*
8397 * Nice levels are multiplicative, with a gentle 10% change for every
8398 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
8399 * nice 1, it will get ~10% less CPU time than another CPU-bound task
8400 * that remained on nice 0.
8401 *
8402 * The "10% effect" is relative and cumulative: from _any_ nice level,
8403 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
8404 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
8405 * If a task goes up by ~10% and another task goes down by ~10% then
8406 * the relative distance between them is ~25%.)
8407 */
8408const int sched_prio_to_weight[40] = {
8409 /* -20 */ 88761, 71755, 56483, 46273, 36291,
8410 /* -15 */ 29154, 23254, 18705, 14949, 11916,
8411 /* -10 */ 9548, 7620, 6100, 4904, 3906,
8412 /* -5 */ 3121, 2501, 1991, 1586, 1277,
8413 /* 0 */ 1024, 820, 655, 526, 423,
8414 /* 5 */ 335, 272, 215, 172, 137,
8415 /* 10 */ 110, 87, 70, 56, 45,
8416 /* 15 */ 36, 29, 23, 18, 15,
8417};
8418
8419/*
8420 * Inverse (2^32/x) values of the sched_prio_to_weight[] array, precalculated.
8421 *
8422 * In cases where the weight does not change often, we can use the
8423 * precalculated inverse to speed up arithmetics by turning divisions
8424 * into multiplications:
8425 */
8426const u32 sched_prio_to_wmult[40] = {
8427 /* -20 */ 48388, 59856, 76040, 92818, 118348,
8428 /* -15 */ 147320, 184698, 229616, 287308, 360437,
8429 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
8430 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
8431 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
8432 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
8433 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
8434 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
8435};
Ingo Molnar14a74052018-03-03 16:32:24 +01008436
Phil Auld9d246052020-06-29 15:23:03 -04008437void call_trace_sched_update_nr_running(struct rq *rq, int count)
8438{
8439 trace_sched_update_nr_running_tp(rq, count);
8440}