blob: ff0519551188508d9b306a969769482f89326251 [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
Dario Faggioli332ac172013-11-07 14:43:45 +010082/*
Peter Zijlstra3e71a462016-04-28 16:16:33 +020083 * __task_rq_lock - lock the rq @p resides on.
84 */
Peter Zijlstraeb580752015-07-31 21:28:18 +020085struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
Peter Zijlstra3e71a462016-04-28 16:16:33 +020086 __acquires(rq->lock)
87{
88 struct rq *rq;
89
90 lockdep_assert_held(&p->pi_lock);
91
92 for (;;) {
93 rq = task_rq(p);
94 raw_spin_lock(&rq->lock);
95 if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) {
Matt Flemingd8ac8972016-09-21 14:38:10 +010096 rq_pin_lock(rq, rf);
Peter Zijlstra3e71a462016-04-28 16:16:33 +020097 return rq;
98 }
99 raw_spin_unlock(&rq->lock);
100
101 while (unlikely(task_on_rq_migrating(p)))
102 cpu_relax();
103 }
104}
105
106/*
107 * task_rq_lock - lock p->pi_lock and lock the rq @p resides on.
108 */
Peter Zijlstraeb580752015-07-31 21:28:18 +0200109struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
Peter Zijlstra3e71a462016-04-28 16:16:33 +0200110 __acquires(p->pi_lock)
111 __acquires(rq->lock)
112{
113 struct rq *rq;
114
115 for (;;) {
Peter Zijlstraeb580752015-07-31 21:28:18 +0200116 raw_spin_lock_irqsave(&p->pi_lock, rf->flags);
Peter Zijlstra3e71a462016-04-28 16:16:33 +0200117 rq = task_rq(p);
118 raw_spin_lock(&rq->lock);
119 /*
120 * move_queued_task() task_rq_lock()
121 *
122 * ACQUIRE (rq->lock)
123 * [S] ->on_rq = MIGRATING [L] rq = task_rq()
124 * WMB (__set_task_cpu()) ACQUIRE (rq->lock);
125 * [S] ->cpu = new_cpu [L] task_rq()
126 * [L] ->on_rq
127 * RELEASE (rq->lock)
128 *
Andrea Parric5469512019-01-21 16:52:40 +0100129 * If we observe the old CPU in task_rq_lock(), the acquire of
Peter Zijlstra3e71a462016-04-28 16:16:33 +0200130 * the old rq->lock will fully serialize against the stores.
131 *
Andrea Parric5469512019-01-21 16:52:40 +0100132 * If we observe the new CPU in task_rq_lock(), the address
133 * dependency headed by '[L] rq = task_rq()' and the acquire
134 * will pair with the WMB to ensure we then also see migrating.
Peter Zijlstra3e71a462016-04-28 16:16:33 +0200135 */
136 if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) {
Matt Flemingd8ac8972016-09-21 14:38:10 +0100137 rq_pin_lock(rq, rf);
Peter Zijlstra3e71a462016-04-28 16:16:33 +0200138 return rq;
139 }
140 raw_spin_unlock(&rq->lock);
Peter Zijlstraeb580752015-07-31 21:28:18 +0200141 raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
Peter Zijlstra3e71a462016-04-28 16:16:33 +0200142
143 while (unlikely(task_on_rq_migrating(p)))
144 cpu_relax();
145 }
146}
147
Ingo Molnar535b9552017-02-01 12:29:21 +0100148/*
149 * RQ-clock updating methods:
150 */
151
152static void update_rq_clock_task(struct rq *rq, s64 delta)
153{
154/*
155 * In theory, the compile should just see 0 here, and optimize out the call
156 * to sched_rt_avg_update. But I don't trust it...
157 */
Vincent Guittot11d4afd2018-09-25 11:17:42 +0200158 s64 __maybe_unused steal = 0, irq_delta = 0;
159
Ingo Molnar535b9552017-02-01 12:29:21 +0100160#ifdef CONFIG_IRQ_TIME_ACCOUNTING
161 irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time;
162
163 /*
164 * Since irq_time is only updated on {soft,}irq_exit, we might run into
165 * this case when a previous update_rq_clock() happened inside a
166 * {soft,}irq region.
167 *
168 * When this happens, we stop ->clock_task and only update the
169 * prev_irq_time stamp to account for the part that fit, so that a next
170 * update will consume the rest. This ensures ->clock_task is
171 * monotonic.
172 *
173 * It does however cause some slight miss-attribution of {soft,}irq
174 * time, a more accurate solution would be to update the irq_time using
175 * the current rq->clock timestamp, except that would require using
176 * atomic ops.
177 */
178 if (irq_delta > delta)
179 irq_delta = delta;
180
181 rq->prev_irq_time += irq_delta;
182 delta -= irq_delta;
183#endif
184#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
185 if (static_key_false((&paravirt_steal_rq_enabled))) {
186 steal = paravirt_steal_clock(cpu_of(rq));
187 steal -= rq->prev_steal_time_rq;
188
189 if (unlikely(steal > delta))
190 steal = delta;
191
192 rq->prev_steal_time_rq += steal;
193 delta -= steal;
194 }
195#endif
196
197 rq->clock_task += delta;
198
Vincent Guittot11d4afd2018-09-25 11:17:42 +0200199#ifdef CONFIG_HAVE_SCHED_AVG_IRQ
Ingo Molnar535b9552017-02-01 12:29:21 +0100200 if ((irq_delta + steal) && sched_feat(NONTASK_CAPACITY))
Vincent Guittot91c27492018-06-28 17:45:09 +0200201 update_irq_load_avg(rq, irq_delta + steal);
Ingo Molnar535b9552017-02-01 12:29:21 +0100202#endif
Vincent Guittot23127292019-01-23 16:26:53 +0100203 update_rq_clock_pelt(rq, delta);
Ingo Molnar535b9552017-02-01 12:29:21 +0100204}
205
206void update_rq_clock(struct rq *rq)
207{
208 s64 delta;
209
210 lockdep_assert_held(&rq->lock);
211
212 if (rq->clock_update_flags & RQCF_ACT_SKIP)
213 return;
214
215#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra26ae58d2016-10-03 16:53:49 +0200216 if (sched_feat(WARN_DOUBLE_CLOCK))
217 SCHED_WARN_ON(rq->clock_update_flags & RQCF_UPDATED);
Ingo Molnar535b9552017-02-01 12:29:21 +0100218 rq->clock_update_flags |= RQCF_UPDATED;
219#endif
Peter Zijlstra26ae58d2016-10-03 16:53:49 +0200220
Ingo Molnar535b9552017-02-01 12:29:21 +0100221 delta = sched_clock_cpu(cpu_of(rq)) - rq->clock;
222 if (delta < 0)
223 return;
224 rq->clock += delta;
225 update_rq_clock_task(rq, delta);
226}
227
Peter Zijlstra (Intel)90b53632020-03-27 11:44:56 +0100228static inline void
229rq_csd_init(struct rq *rq, call_single_data_t *csd, smp_call_func_t func)
230{
231 csd->flags = 0;
232 csd->func = func;
233 csd->info = rq;
234}
Ingo Molnar535b9552017-02-01 12:29:21 +0100235
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100236#ifdef CONFIG_SCHED_HRTICK
237/*
238 * Use HR-timers to deliver accurate preemption points.
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100239 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100240
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100241static void hrtick_clear(struct rq *rq)
242{
243 if (hrtimer_active(&rq->hrtick_timer))
244 hrtimer_cancel(&rq->hrtick_timer);
245}
246
247/*
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100248 * High-resolution timer tick.
249 * Runs from hardirq context with interrupts disabled.
250 */
251static enum hrtimer_restart hrtick(struct hrtimer *timer)
252{
253 struct rq *rq = container_of(timer, struct rq, hrtick_timer);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +0200254 struct rq_flags rf;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100255
256 WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
257
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +0200258 rq_lock(rq, &rf);
Peter Zijlstra3e51f332008-05-03 18:29:28 +0200259 update_rq_clock(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100260 rq->curr->sched_class->task_tick(rq, rq->curr, 1);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +0200261 rq_unlock(rq, &rf);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100262
263 return HRTIMER_NORESTART;
264}
265
Rabin Vincent95e904c2008-05-11 05:55:33 +0530266#ifdef CONFIG_SMP
Peter Zijlstra971ee282013-06-28 11:18:53 +0200267
Thomas Gleixner4961b6e2015-04-14 21:09:05 +0000268static void __hrtick_restart(struct rq *rq)
Peter Zijlstra971ee282013-06-28 11:18:53 +0200269{
270 struct hrtimer *timer = &rq->hrtick_timer;
Peter Zijlstra971ee282013-06-28 11:18:53 +0200271
Sebastian Andrzej Siewiord5096aa2019-07-26 20:30:52 +0200272 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED_HARD);
Peter Zijlstra971ee282013-06-28 11:18:53 +0200273}
274
Peter Zijlstra31656512008-07-18 18:01:23 +0200275/*
276 * called from hardirq (IPI) context
277 */
278static void __hrtick_start(void *arg)
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200279{
Peter Zijlstra31656512008-07-18 18:01:23 +0200280 struct rq *rq = arg;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +0200281 struct rq_flags rf;
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200282
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +0200283 rq_lock(rq, &rf);
Peter Zijlstra971ee282013-06-28 11:18:53 +0200284 __hrtick_restart(rq);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +0200285 rq_unlock(rq, &rf);
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200286}
287
Peter Zijlstra31656512008-07-18 18:01:23 +0200288/*
289 * Called to set the hrtick timer state.
290 *
291 * called with rq->lock held and irqs disabled
292 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200293void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200294{
Peter Zijlstra31656512008-07-18 18:01:23 +0200295 struct hrtimer *timer = &rq->hrtick_timer;
xiaofeng.yan177ef2a2014-08-26 03:15:41 +0000296 ktime_t time;
297 s64 delta;
298
299 /*
300 * Don't schedule slices shorter than 10000ns, that just
301 * doesn't make sense and can cause timer DoS.
302 */
303 delta = max_t(s64, delay, 10000LL);
304 time = ktime_add_ns(timer->base->get_time(), delta);
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200305
Arjan van de Vencc584b22008-09-01 15:02:30 -0700306 hrtimer_set_expires(timer, time);
Peter Zijlstra31656512008-07-18 18:01:23 +0200307
Peter Xufd3eafd2019-12-16 16:31:25 -0500308 if (rq == this_rq())
Peter Zijlstra971ee282013-06-28 11:18:53 +0200309 __hrtick_restart(rq);
Peter Xufd3eafd2019-12-16 16:31:25 -0500310 else
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100311 smp_call_function_single_async(cpu_of(rq), &rq->hrtick_csd);
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200312}
313
Peter Zijlstra31656512008-07-18 18:01:23 +0200314#else
315/*
316 * Called to set the hrtick timer state.
317 *
318 * called with rq->lock held and irqs disabled
319 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200320void hrtick_start(struct rq *rq, u64 delay)
Peter Zijlstra31656512008-07-18 18:01:23 +0200321{
Wanpeng Li86893332014-11-26 08:44:06 +0800322 /*
323 * Don't schedule slices shorter than 10000ns, that just
324 * doesn't make sense. Rely on vruntime for fairness.
325 */
326 delay = max_t(u64, delay, 10000LL);
Thomas Gleixner4961b6e2015-04-14 21:09:05 +0000327 hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay),
Sebastian Andrzej Siewiord5096aa2019-07-26 20:30:52 +0200328 HRTIMER_MODE_REL_PINNED_HARD);
Peter Zijlstra31656512008-07-18 18:01:23 +0200329}
Peter Zijlstra (Intel)90b53632020-03-27 11:44:56 +0100330
Rabin Vincent95e904c2008-05-11 05:55:33 +0530331#endif /* CONFIG_SMP */
Peter Zijlstrab328ca12008-04-29 10:02:46 +0200332
Frederic Weisbecker77a021b2018-02-21 05:17:23 +0100333static void hrtick_rq_init(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100334{
Peter Zijlstra31656512008-07-18 18:01:23 +0200335#ifdef CONFIG_SMP
Peter Zijlstra (Intel)90b53632020-03-27 11:44:56 +0100336 rq_csd_init(rq, &rq->hrtick_csd, __hrtick_start);
Peter Zijlstra31656512008-07-18 18:01:23 +0200337#endif
Sebastian Andrzej Siewiord5096aa2019-07-26 20:30:52 +0200338 hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100339 rq->hrtick_timer.function = hrtick;
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100340}
Andrew Morton006c75f2008-09-22 14:55:46 -0700341#else /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100342static inline void hrtick_clear(struct rq *rq)
343{
344}
345
Frederic Weisbecker77a021b2018-02-21 05:17:23 +0100346static inline void hrtick_rq_init(struct rq *rq)
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100347{
348}
Andrew Morton006c75f2008-09-22 14:55:46 -0700349#endif /* CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100350
Frederic Weisbecker55295782016-03-24 15:38:01 +0100351/*
352 * cmpxchg based fetch_or, macro so it works for different integer types
353 */
354#define fetch_or(ptr, mask) \
355 ({ \
356 typeof(ptr) _ptr = (ptr); \
357 typeof(mask) _mask = (mask); \
358 typeof(*_ptr) _old, _val = *_ptr; \
359 \
360 for (;;) { \
361 _old = cmpxchg(_ptr, _val, _val | _mask); \
362 if (_old == _val) \
363 break; \
364 _val = _old; \
365 } \
366 _old; \
367})
368
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700369#if defined(CONFIG_SMP) && defined(TIF_POLLING_NRFLAG)
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200370/*
371 * Atomically set TIF_NEED_RESCHED and test for TIF_POLLING_NRFLAG,
372 * this avoids any races wrt polling state changes and thereby avoids
373 * spurious IPIs.
374 */
375static bool set_nr_and_not_polling(struct task_struct *p)
376{
377 struct thread_info *ti = task_thread_info(p);
378 return !(fetch_or(&ti->flags, _TIF_NEED_RESCHED) & _TIF_POLLING_NRFLAG);
379}
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700380
381/*
382 * Atomically set TIF_NEED_RESCHED if TIF_POLLING_NRFLAG is set.
383 *
384 * If this returns true, then the idle task promises to call
385 * sched_ttwu_pending() and reschedule soon.
386 */
387static bool set_nr_if_polling(struct task_struct *p)
388{
389 struct thread_info *ti = task_thread_info(p);
Jason Low316c1608d2015-04-28 13:00:20 -0700390 typeof(ti->flags) old, val = READ_ONCE(ti->flags);
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700391
392 for (;;) {
393 if (!(val & _TIF_POLLING_NRFLAG))
394 return false;
395 if (val & _TIF_NEED_RESCHED)
396 return true;
397 old = cmpxchg(&ti->flags, val, val | _TIF_NEED_RESCHED);
398 if (old == val)
399 break;
400 val = old;
401 }
402 return true;
403}
404
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200405#else
406static bool set_nr_and_not_polling(struct task_struct *p)
407{
408 set_tsk_need_resched(p);
409 return true;
410}
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700411
412#ifdef CONFIG_SMP
413static bool set_nr_if_polling(struct task_struct *p)
414{
415 return false;
416}
417#endif
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200418#endif
419
Davidlohr Bueso07879c62018-12-18 11:53:52 -0800420static bool __wake_q_add(struct wake_q_head *head, struct task_struct *task)
421{
422 struct wake_q_node *node = &task->wake_q;
423
424 /*
425 * Atomically grab the task, if ->wake_q is !nil already it means
426 * its already queued (either by us or someone else) and will get the
427 * wakeup due to that.
428 *
429 * In order to ensure that a pending wakeup will observe our pending
430 * state, even in the failed case, an explicit smp_mb() must be used.
431 */
432 smp_mb__before_atomic();
433 if (unlikely(cmpxchg_relaxed(&node->next, NULL, WAKE_Q_TAIL)))
434 return false;
435
436 /*
437 * The head is context local, there can be no concurrency.
438 */
439 *head->lastp = node;
440 head->lastp = &node->next;
441 return true;
442}
443
Peter Zijlstrae6018c02018-12-17 10:14:53 +0100444/**
445 * wake_q_add() - queue a wakeup for 'later' waking.
446 * @head: the wake_q_head to add @task to
447 * @task: the task to queue for 'later' wakeup
448 *
449 * Queue a task for later wakeup, most likely by the wake_up_q() call in the
450 * same context, _HOWEVER_ this is not guaranteed, the wakeup can come
451 * instantly.
452 *
453 * This function must be used as-if it were wake_up_process(); IOW the task
454 * must be ready to be woken at this location.
455 */
Peter Zijlstra76751042015-05-01 08:27:50 -0700456void wake_q_add(struct wake_q_head *head, struct task_struct *task)
457{
Davidlohr Bueso07879c62018-12-18 11:53:52 -0800458 if (__wake_q_add(head, task))
459 get_task_struct(task);
460}
Peter Zijlstra76751042015-05-01 08:27:50 -0700461
Davidlohr Bueso07879c62018-12-18 11:53:52 -0800462/**
463 * wake_q_add_safe() - safely queue a wakeup for 'later' waking.
464 * @head: the wake_q_head to add @task to
465 * @task: the task to queue for 'later' wakeup
466 *
467 * Queue a task for later wakeup, most likely by the wake_up_q() call in the
468 * same context, _HOWEVER_ this is not guaranteed, the wakeup can come
469 * instantly.
470 *
471 * This function must be used as-if it were wake_up_process(); IOW the task
472 * must be ready to be woken at this location.
473 *
474 * This function is essentially a task-safe equivalent to wake_q_add(). Callers
475 * that already hold reference to @task can call the 'safe' version and trust
476 * wake_q to do the right thing depending whether or not the @task is already
477 * queued for wakeup.
478 */
479void wake_q_add_safe(struct wake_q_head *head, struct task_struct *task)
480{
481 if (!__wake_q_add(head, task))
482 put_task_struct(task);
Peter Zijlstra76751042015-05-01 08:27:50 -0700483}
484
485void wake_up_q(struct wake_q_head *head)
486{
487 struct wake_q_node *node = head->first;
488
489 while (node != WAKE_Q_TAIL) {
490 struct task_struct *task;
491
492 task = container_of(node, struct task_struct, wake_q);
493 BUG_ON(!task);
Ingo Molnard1ccc662017-02-01 11:46:42 +0100494 /* Task can safely be re-inserted now: */
Peter Zijlstra76751042015-05-01 08:27:50 -0700495 node = node->next;
496 task->wake_q.next = NULL;
497
498 /*
Andrea Parri7696f992018-07-16 11:06:03 -0700499 * wake_up_process() executes a full barrier, which pairs with
500 * the queueing in wake_q_add() so as not to miss wakeups.
Peter Zijlstra76751042015-05-01 08:27:50 -0700501 */
502 wake_up_process(task);
503 put_task_struct(task);
504 }
505}
506
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200507/*
Kirill Tkhai88751252014-06-29 00:03:57 +0400508 * resched_curr - mark rq's current task 'to be rescheduled now'.
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200509 *
510 * On UP this means the setting of the need_resched flag, on SMP it
511 * might also involve a cross-CPU call to trigger the scheduler on
512 * the target CPU.
513 */
Kirill Tkhai88751252014-06-29 00:03:57 +0400514void resched_curr(struct rq *rq)
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200515{
Kirill Tkhai88751252014-06-29 00:03:57 +0400516 struct task_struct *curr = rq->curr;
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200517 int cpu;
518
Kirill Tkhai88751252014-06-29 00:03:57 +0400519 lockdep_assert_held(&rq->lock);
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200520
Kirill Tkhai88751252014-06-29 00:03:57 +0400521 if (test_tsk_need_resched(curr))
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200522 return;
523
Kirill Tkhai88751252014-06-29 00:03:57 +0400524 cpu = cpu_of(rq);
Peter Zijlstrafd99f912014-04-09 15:35:08 +0200525
Peter Zijlstraf27dde82013-08-14 14:55:31 +0200526 if (cpu == smp_processor_id()) {
Kirill Tkhai88751252014-06-29 00:03:57 +0400527 set_tsk_need_resched(curr);
Peter Zijlstraf27dde82013-08-14 14:55:31 +0200528 set_preempt_need_resched();
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200529 return;
Peter Zijlstraf27dde82013-08-14 14:55:31 +0200530 }
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200531
Kirill Tkhai88751252014-06-29 00:03:57 +0400532 if (set_nr_and_not_polling(curr))
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200533 smp_send_reschedule(cpu);
Andy Lutomirskidfc68f22014-06-04 10:31:15 -0700534 else
535 trace_sched_wake_idle_without_ipi(cpu);
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200536}
537
Peter Zijlstra029632f2011-10-25 10:00:11 +0200538void resched_cpu(int cpu)
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200539{
540 struct rq *rq = cpu_rq(cpu);
541 unsigned long flags;
542
Paul E. McKenney7c2102e2017-09-18 08:54:40 -0700543 raw_spin_lock_irqsave(&rq->lock, flags);
Paul E. McKenneya0982df2017-10-13 16:24:28 -0700544 if (cpu_online(cpu) || cpu == smp_processor_id())
545 resched_curr(rq);
Thomas Gleixner05fa7852009-11-17 14:28:38 +0100546 raw_spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200547}
Thomas Gleixner06d83082008-03-22 09:20:24 +0100548
Peter Zijlstrab021fe32013-09-17 09:30:55 +0200549#ifdef CONFIG_SMP
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200550#ifdef CONFIG_NO_HZ_COMMON
Thomas Gleixner06d83082008-03-22 09:20:24 +0100551/*
Ingo Molnard1ccc662017-02-01 11:46:42 +0100552 * In the semi idle case, use the nearest busy CPU for migrating timers
553 * from an idle CPU. This is good for power-savings.
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700554 *
555 * We don't do similar optimization for completely idle system, as
Ingo Molnard1ccc662017-02-01 11:46:42 +0100556 * selecting an idle CPU will add more delays to the timers than intended
557 * (as that CPU's timer base may not be uptodate wrt jiffies etc).
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700558 */
Thomas Gleixnerbc7a34b2015-05-26 22:50:33 +0000559int get_nohz_timer_target(void)
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700560{
Wanpeng Lie938b9c2020-01-13 08:50:27 +0800561 int i, cpu = smp_processor_id(), default_cpu = -1;
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700562 struct sched_domain *sd;
563
Wanpeng Lie938b9c2020-01-13 08:50:27 +0800564 if (housekeeping_cpu(cpu, HK_FLAG_TIMER)) {
565 if (!idle_cpu(cpu))
566 return cpu;
567 default_cpu = cpu;
568 }
Viresh Kumar6201b4d2014-03-18 16:26:07 +0530569
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200570 rcu_read_lock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700571 for_each_domain(cpu, sd) {
Wanpeng Lie938b9c2020-01-13 08:50:27 +0800572 for_each_cpu_and(i, sched_domain_span(sd),
573 housekeeping_cpumask(HK_FLAG_TIMER)) {
Wanpeng Li44496922016-05-04 14:45:34 +0800574 if (cpu == i)
575 continue;
576
Wanpeng Lie938b9c2020-01-13 08:50:27 +0800577 if (!idle_cpu(i)) {
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200578 cpu = i;
579 goto unlock;
580 }
581 }
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700582 }
Vatika Harlalka9642d182015-09-01 16:50:59 +0200583
Wanpeng Lie938b9c2020-01-13 08:50:27 +0800584 if (default_cpu == -1)
585 default_cpu = housekeeping_any_cpu(HK_FLAG_TIMER);
586 cpu = default_cpu;
Peter Zijlstra057f3fa2011-04-18 11:24:34 +0200587unlock:
588 rcu_read_unlock();
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700589 return cpu;
590}
Ingo Molnard1ccc662017-02-01 11:46:42 +0100591
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700592/*
Thomas Gleixner06d83082008-03-22 09:20:24 +0100593 * When add_timer_on() enqueues a timer into the timer wheel of an
594 * idle CPU then this timer might expire before the next timer event
595 * which is scheduled to wake up that CPU. In case of a completely
596 * idle system the next event might even be infinite time into the
597 * future. wake_up_idle_cpu() ensures that the CPU is woken up and
598 * leaves the inner idle loop so the newly added timer is taken into
599 * account when the CPU goes back to idle and evaluates the timer
600 * wheel for the next timer event.
601 */
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200602static void wake_up_idle_cpu(int cpu)
Thomas Gleixner06d83082008-03-22 09:20:24 +0100603{
604 struct rq *rq = cpu_rq(cpu);
605
606 if (cpu == smp_processor_id())
607 return;
608
Andy Lutomirski67b9ca72014-06-04 10:31:17 -0700609 if (set_nr_and_not_polling(rq->idle))
Thomas Gleixner06d83082008-03-22 09:20:24 +0100610 smp_send_reschedule(cpu);
Andy Lutomirskidfc68f22014-06-04 10:31:15 -0700611 else
612 trace_sched_wake_idle_without_ipi(cpu);
Thomas Gleixner06d83082008-03-22 09:20:24 +0100613}
Mike Galbraith39c0cbe2010-03-11 17:17:13 +0100614
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200615static bool wake_up_full_nohz_cpu(int cpu)
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200616{
Frederic Weisbecker53c5fa12014-06-04 16:20:21 +0200617 /*
618 * We just need the target to call irq_exit() and re-evaluate
619 * the next tick. The nohz full kick at least implies that.
620 * If needed we can still optimize that later with an
621 * empty IRQ.
622 */
Paul E. McKenney379d9ec2016-06-30 10:37:20 -0700623 if (cpu_is_offline(cpu))
624 return true; /* Don't try to wake offline CPUs. */
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200625 if (tick_nohz_full_cpu(cpu)) {
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200626 if (cpu != smp_processor_id() ||
627 tick_nohz_tick_stopped())
Frederic Weisbecker53c5fa12014-06-04 16:20:21 +0200628 tick_nohz_full_kick_cpu(cpu);
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200629 return true;
630 }
631
632 return false;
633}
634
Paul E. McKenney379d9ec2016-06-30 10:37:20 -0700635/*
636 * Wake up the specified CPU. If the CPU is going offline, it is the
637 * caller's responsibility to deal with the lost wakeup, for example,
638 * by hooking into the CPU_DEAD notifier like timers and hrtimers do.
639 */
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200640void wake_up_nohz_cpu(int cpu)
641{
Frederic Weisbeckerc5bfece2013-04-12 16:45:34 +0200642 if (!wake_up_full_nohz_cpu(cpu))
Frederic Weisbecker1c200912011-08-10 23:21:01 +0200643 wake_up_idle_cpu(cpu);
644}
645
Peter Zijlstra (Intel)90b53632020-03-27 11:44:56 +0100646static void nohz_csd_func(void *info)
Suresh Siddhaca380622011-10-03 15:09:00 -0700647{
Peter Zijlstra (Intel)90b53632020-03-27 11:44:56 +0100648 struct rq *rq = info;
Peter Zijlstra19a1f5e2020-05-26 18:10:58 +0200649 int cpu = cpu_of(rq);
650 unsigned int flags;
Vincent Guittot873b4c62013-06-05 10:13:11 +0200651
652 /*
Peter Zijlstra19a1f5e2020-05-26 18:10:58 +0200653 * Release the rq::nohz_csd.
Vincent Guittot873b4c62013-06-05 10:13:11 +0200654 */
Peter Zijlstra19a1f5e2020-05-26 18:10:58 +0200655 flags = atomic_fetch_andnot(NOHZ_KICK_MASK, nohz_flags(cpu));
656 WARN_ON(!(flags & NOHZ_KICK_MASK));
Suresh Siddhaca380622011-10-03 15:09:00 -0700657
Peter Zijlstra19a1f5e2020-05-26 18:10:58 +0200658 rq->idle_balance = idle_cpu(cpu);
659 if (rq->idle_balance && !need_resched()) {
660 rq->nohz_idle_balance = flags;
Peter Zijlstra (Intel)90b53632020-03-27 11:44:56 +0100661 raise_softirq_irqoff(SCHED_SOFTIRQ);
662 }
Suresh Siddhaca380622011-10-03 15:09:00 -0700663}
664
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200665#endif /* CONFIG_NO_HZ_COMMON */
Thomas Gleixner06d83082008-03-22 09:20:24 +0100666
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200667#ifdef CONFIG_NO_HZ_FULL
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +0200668bool sched_can_stop_tick(struct rq *rq)
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200669{
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +0200670 int fifo_nr_running;
671
672 /* Deadline tasks, even if single, need the tick */
673 if (rq->dl.dl_nr_running)
674 return false;
675
Frederic Weisbecker3882ec62014-03-18 22:54:04 +0100676 /*
Peter Zijlstra2548d542016-04-21 18:03:15 +0200677 * If there are more than one RR tasks, we need the tick to effect the
678 * actual RR behaviour.
Rik van Riel1e78cdb2015-02-16 15:23:49 -0500679 */
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +0200680 if (rq->rt.rr_nr_running) {
681 if (rq->rt.rr_nr_running == 1)
682 return true;
683 else
684 return false;
Rik van Riel1e78cdb2015-02-16 15:23:49 -0500685 }
686
Peter Zijlstra2548d542016-04-21 18:03:15 +0200687 /*
688 * If there's no RR tasks, but FIFO tasks, we can skip the tick, no
689 * forced preemption between FIFO tasks.
690 */
691 fifo_nr_running = rq->rt.rt_nr_running - rq->rt.rr_nr_running;
692 if (fifo_nr_running)
693 return true;
694
695 /*
696 * If there are no DL,RR/FIFO tasks, there must only be CFS tasks left;
697 * if there's more than one we need the tick for involuntary
698 * preemption.
699 */
700 if (rq->nr_running > 1)
Viresh Kumar541b8262014-06-24 14:04:12 +0530701 return false;
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200702
Viresh Kumar541b8262014-06-24 14:04:12 +0530703 return true;
Frederic Weisbeckerce831b32013-04-20 15:15:35 +0200704}
705#endif /* CONFIG_NO_HZ_FULL */
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +0200706#endif /* CONFIG_SMP */
Ingo Molnarc24d20d2007-07-09 18:51:59 +0200707
Paul Turnera790de92011-07-21 09:43:29 -0700708#if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \
709 (defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH)))
Peter Zijlstraeb755802008-08-19 12:33:05 +0200710/*
Paul Turner82774342011-07-21 09:43:35 -0700711 * Iterate task_group tree rooted at *from, calling @down when first entering a
712 * node and @up when leaving it for the final time.
713 *
714 * Caller must hold rcu_lock or sufficient equivalent.
Peter Zijlstraeb755802008-08-19 12:33:05 +0200715 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200716int walk_tg_tree_from(struct task_group *from,
Paul Turner82774342011-07-21 09:43:35 -0700717 tg_visitor down, tg_visitor up, void *data)
Peter Zijlstraeb755802008-08-19 12:33:05 +0200718{
719 struct task_group *parent, *child;
720 int ret;
721
Paul Turner82774342011-07-21 09:43:35 -0700722 parent = from;
723
Peter Zijlstraeb755802008-08-19 12:33:05 +0200724down:
725 ret = (*down)(parent, data);
726 if (ret)
Paul Turner82774342011-07-21 09:43:35 -0700727 goto out;
Peter Zijlstraeb755802008-08-19 12:33:05 +0200728 list_for_each_entry_rcu(child, &parent->children, siblings) {
729 parent = child;
730 goto down;
731
732up:
733 continue;
734 }
735 ret = (*up)(parent, data);
Paul Turner82774342011-07-21 09:43:35 -0700736 if (ret || parent == from)
737 goto out;
Peter Zijlstraeb755802008-08-19 12:33:05 +0200738
739 child = parent;
740 parent = parent->parent;
741 if (parent)
742 goto up;
Paul Turner82774342011-07-21 09:43:35 -0700743out:
Peter Zijlstraeb755802008-08-19 12:33:05 +0200744 return ret;
745}
746
Peter Zijlstra029632f2011-10-25 10:00:11 +0200747int tg_nop(struct task_group *tg, void *data)
Peter Zijlstraeb755802008-08-19 12:33:05 +0200748{
749 return 0;
750}
751#endif
752
Vincent Guittot90593932017-05-17 11:50:45 +0200753static void set_load_weight(struct task_struct *p, bool update_load)
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200754{
Nikhil Raof05998d2011-05-18 10:09:38 -0700755 int prio = p->static_prio - MAX_RT_PRIO;
756 struct load_weight *load = &p->se.load;
757
Ingo Molnardd41f592007-07-09 18:51:59 +0200758 /*
759 * SCHED_IDLE tasks get minimal weight:
760 */
Viresh Kumar1da18432018-11-05 16:51:55 +0530761 if (task_has_idle_policy(p)) {
Nikhil Raoc8b28112011-05-18 14:37:48 -0700762 load->weight = scale_load(WEIGHT_IDLEPRIO);
Nikhil Raof05998d2011-05-18 10:09:38 -0700763 load->inv_weight = WMULT_IDLEPRIO;
Ingo Molnardd41f592007-07-09 18:51:59 +0200764 return;
765 }
766
Vincent Guittot90593932017-05-17 11:50:45 +0200767 /*
768 * SCHED_OTHER tasks have to update their load when changing their
769 * weight
770 */
771 if (update_load && p->sched_class == &fair_sched_class) {
772 reweight_task(p, prio);
773 } else {
774 load->weight = scale_load(sched_prio_to_weight[prio]);
775 load->inv_weight = sched_prio_to_wmult[prio];
776 }
Ingo Molnar45bf76d2007-07-09 18:51:59 +0200777}
778
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100779#ifdef CONFIG_UCLAMP_TASK
Patrick Bellasi2480c092019-08-22 14:28:06 +0100780/*
781 * Serializes updates of utilization clamp values
782 *
783 * The (slow-path) user-space triggers utilization clamp value updates which
784 * can require updates on (fast-path) scheduler's data structures used to
785 * support enqueue/dequeue operations.
786 * While the per-CPU rq lock protects fast-path update operations, user-space
787 * requests are serialized using a mutex to reduce the risk of conflicting
788 * updates or API abuses.
789 */
790static DEFINE_MUTEX(uclamp_mutex);
791
Patrick Bellasie8f14172019-06-21 09:42:05 +0100792/* Max allowed minimum utilization */
793unsigned int sysctl_sched_uclamp_util_min = SCHED_CAPACITY_SCALE;
794
795/* Max allowed maximum utilization */
796unsigned int sysctl_sched_uclamp_util_max = SCHED_CAPACITY_SCALE;
797
798/* All clamps are required to be less or equal than these values */
799static struct uclamp_se uclamp_default[UCLAMP_CNT];
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100800
Qais Yousef46609ce2020-06-30 12:21:23 +0100801/*
802 * This static key is used to reduce the uclamp overhead in the fast path. It
803 * primarily disables the call to uclamp_rq_{inc, dec}() in
804 * enqueue/dequeue_task().
805 *
806 * This allows users to continue to enable uclamp in their kernel config with
807 * minimum uclamp overhead in the fast path.
808 *
809 * As soon as userspace modifies any of the uclamp knobs, the static key is
810 * enabled, since we have an actual users that make use of uclamp
811 * functionality.
812 *
813 * The knobs that would enable this static key are:
814 *
815 * * A task modifying its uclamp value with sched_setattr().
816 * * An admin modifying the sysctl_sched_uclamp_{min, max} via procfs.
817 * * An admin modifying the cgroup cpu.uclamp.{min, max}
818 */
819DEFINE_STATIC_KEY_FALSE(sched_uclamp_used);
820
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100821/* Integer rounded range for each bucket */
822#define UCLAMP_BUCKET_DELTA DIV_ROUND_CLOSEST(SCHED_CAPACITY_SCALE, UCLAMP_BUCKETS)
823
824#define for_each_clamp_id(clamp_id) \
825 for ((clamp_id) = 0; (clamp_id) < UCLAMP_CNT; (clamp_id)++)
826
827static inline unsigned int uclamp_bucket_id(unsigned int clamp_value)
828{
829 return clamp_value / UCLAMP_BUCKET_DELTA;
830}
831
Patrick Bellasi60daf9c2019-06-21 09:42:03 +0100832static inline unsigned int uclamp_bucket_base_value(unsigned int clamp_value)
833{
834 return UCLAMP_BUCKET_DELTA * uclamp_bucket_id(clamp_value);
835}
836
Valentin Schneider7763baa2019-11-15 10:39:08 +0000837static inline unsigned int uclamp_none(enum uclamp_id clamp_id)
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100838{
839 if (clamp_id == UCLAMP_MIN)
840 return 0;
841 return SCHED_CAPACITY_SCALE;
842}
843
Patrick Bellasia509a7c2019-06-21 09:42:07 +0100844static inline void uclamp_se_set(struct uclamp_se *uc_se,
845 unsigned int value, bool user_defined)
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100846{
847 uc_se->value = value;
848 uc_se->bucket_id = uclamp_bucket_id(value);
Patrick Bellasia509a7c2019-06-21 09:42:07 +0100849 uc_se->user_defined = user_defined;
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100850}
851
Patrick Bellasie4961872019-06-21 09:42:04 +0100852static inline unsigned int
Patrick Bellasi0413d7f2019-08-22 14:28:11 +0100853uclamp_idle_value(struct rq *rq, enum uclamp_id clamp_id,
Patrick Bellasie4961872019-06-21 09:42:04 +0100854 unsigned int clamp_value)
855{
856 /*
857 * Avoid blocked utilization pushing up the frequency when we go
858 * idle (which drops the max-clamp) by retaining the last known
859 * max-clamp.
860 */
861 if (clamp_id == UCLAMP_MAX) {
862 rq->uclamp_flags |= UCLAMP_FLAG_IDLE;
863 return clamp_value;
864 }
865
866 return uclamp_none(UCLAMP_MIN);
867}
868
Patrick Bellasi0413d7f2019-08-22 14:28:11 +0100869static inline void uclamp_idle_reset(struct rq *rq, enum uclamp_id clamp_id,
Patrick Bellasie4961872019-06-21 09:42:04 +0100870 unsigned int clamp_value)
871{
872 /* Reset max-clamp retention only on idle exit */
873 if (!(rq->uclamp_flags & UCLAMP_FLAG_IDLE))
874 return;
875
876 WRITE_ONCE(rq->uclamp[clamp_id].value, clamp_value);
877}
878
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100879static inline
Valentin Schneider7763baa2019-11-15 10:39:08 +0000880unsigned int uclamp_rq_max_value(struct rq *rq, enum uclamp_id clamp_id,
Patrick Bellasi0413d7f2019-08-22 14:28:11 +0100881 unsigned int clamp_value)
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100882{
883 struct uclamp_bucket *bucket = rq->uclamp[clamp_id].bucket;
884 int bucket_id = UCLAMP_BUCKETS - 1;
885
886 /*
887 * Since both min and max clamps are max aggregated, find the
888 * top most bucket with tasks in.
889 */
890 for ( ; bucket_id >= 0; bucket_id--) {
891 if (!bucket[bucket_id].tasks)
892 continue;
893 return bucket[bucket_id].value;
894 }
895
896 /* No tasks -- default clamp values */
Patrick Bellasie4961872019-06-21 09:42:04 +0100897 return uclamp_idle_value(rq, clamp_id, clamp_value);
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100898}
899
Patrick Bellasi3eac8702019-08-22 14:28:09 +0100900static inline struct uclamp_se
Patrick Bellasi0413d7f2019-08-22 14:28:11 +0100901uclamp_tg_restrict(struct task_struct *p, enum uclamp_id clamp_id)
Patrick Bellasi3eac8702019-08-22 14:28:09 +0100902{
903 struct uclamp_se uc_req = p->uclamp_req[clamp_id];
904#ifdef CONFIG_UCLAMP_TASK_GROUP
905 struct uclamp_se uc_max;
906
907 /*
908 * Tasks in autogroups or root task group will be
909 * restricted by system defaults.
910 */
911 if (task_group_is_autogroup(task_group(p)))
912 return uc_req;
913 if (task_group(p) == &root_task_group)
914 return uc_req;
915
916 uc_max = task_group(p)->uclamp[clamp_id];
917 if (uc_req.value > uc_max.value || !uc_req.user_defined)
918 return uc_max;
919#endif
920
921 return uc_req;
922}
923
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100924/*
Patrick Bellasie8f14172019-06-21 09:42:05 +0100925 * The effective clamp bucket index of a task depends on, by increasing
926 * priority:
927 * - the task specific clamp value, when explicitly requested from userspace
Patrick Bellasi3eac8702019-08-22 14:28:09 +0100928 * - the task group effective clamp value, for tasks not either in the root
929 * group or in an autogroup
Patrick Bellasie8f14172019-06-21 09:42:05 +0100930 * - the system default clamp value, defined by the sysadmin
931 */
932static inline struct uclamp_se
Patrick Bellasi0413d7f2019-08-22 14:28:11 +0100933uclamp_eff_get(struct task_struct *p, enum uclamp_id clamp_id)
Patrick Bellasie8f14172019-06-21 09:42:05 +0100934{
Patrick Bellasi3eac8702019-08-22 14:28:09 +0100935 struct uclamp_se uc_req = uclamp_tg_restrict(p, clamp_id);
Patrick Bellasie8f14172019-06-21 09:42:05 +0100936 struct uclamp_se uc_max = uclamp_default[clamp_id];
937
938 /* System default restrictions always apply */
939 if (unlikely(uc_req.value > uc_max.value))
940 return uc_max;
941
942 return uc_req;
943}
944
Valentin Schneider686516b2019-12-11 11:38:48 +0000945unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id)
Patrick Bellasi9d20ad72019-06-21 09:42:11 +0100946{
947 struct uclamp_se uc_eff;
948
949 /* Task currently refcounted: use back-annotated (effective) value */
950 if (p->uclamp[clamp_id].active)
Valentin Schneider686516b2019-12-11 11:38:48 +0000951 return (unsigned long)p->uclamp[clamp_id].value;
Patrick Bellasi9d20ad72019-06-21 09:42:11 +0100952
953 uc_eff = uclamp_eff_get(p, clamp_id);
954
Valentin Schneider686516b2019-12-11 11:38:48 +0000955 return (unsigned long)uc_eff.value;
Patrick Bellasi9d20ad72019-06-21 09:42:11 +0100956}
957
Patrick Bellasie8f14172019-06-21 09:42:05 +0100958/*
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100959 * When a task is enqueued on a rq, the clamp bucket currently defined by the
960 * task's uclamp::bucket_id is refcounted on that rq. This also immediately
961 * updates the rq's clamp value if required.
Patrick Bellasi60daf9c2019-06-21 09:42:03 +0100962 *
963 * Tasks can have a task-specific value requested from user-space, track
964 * within each bucket the maximum value for tasks refcounted in it.
965 * This "local max aggregation" allows to track the exact "requested" value
966 * for each bucket when all its RUNNABLE tasks require the same clamp.
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100967 */
968static inline void uclamp_rq_inc_id(struct rq *rq, struct task_struct *p,
Patrick Bellasi0413d7f2019-08-22 14:28:11 +0100969 enum uclamp_id clamp_id)
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100970{
971 struct uclamp_rq *uc_rq = &rq->uclamp[clamp_id];
972 struct uclamp_se *uc_se = &p->uclamp[clamp_id];
973 struct uclamp_bucket *bucket;
974
975 lockdep_assert_held(&rq->lock);
976
Patrick Bellasie8f14172019-06-21 09:42:05 +0100977 /* Update task effective clamp */
978 p->uclamp[clamp_id] = uclamp_eff_get(p, clamp_id);
979
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100980 bucket = &uc_rq->bucket[uc_se->bucket_id];
981 bucket->tasks++;
Patrick Bellasie8f14172019-06-21 09:42:05 +0100982 uc_se->active = true;
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100983
Patrick Bellasie4961872019-06-21 09:42:04 +0100984 uclamp_idle_reset(rq, clamp_id, uc_se->value);
985
Patrick Bellasi60daf9c2019-06-21 09:42:03 +0100986 /*
987 * Local max aggregation: rq buckets always track the max
988 * "requested" clamp value of its RUNNABLE tasks.
989 */
990 if (bucket->tasks == 1 || uc_se->value > bucket->value)
991 bucket->value = uc_se->value;
992
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100993 if (uc_se->value > READ_ONCE(uc_rq->value))
Patrick Bellasi60daf9c2019-06-21 09:42:03 +0100994 WRITE_ONCE(uc_rq->value, uc_se->value);
Patrick Bellasi69842cb2019-06-21 09:42:02 +0100995}
996
997/*
998 * When a task is dequeued from a rq, the clamp bucket refcounted by the task
999 * is released. If this is the last task reference counting the rq's max
1000 * active clamp value, then the rq's clamp value is updated.
1001 *
1002 * Both refcounted tasks and rq's cached clamp values are expected to be
1003 * always valid. If it's detected they are not, as defensive programming,
1004 * enforce the expected state and warn.
1005 */
1006static inline void uclamp_rq_dec_id(struct rq *rq, struct task_struct *p,
Patrick Bellasi0413d7f2019-08-22 14:28:11 +01001007 enum uclamp_id clamp_id)
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001008{
1009 struct uclamp_rq *uc_rq = &rq->uclamp[clamp_id];
1010 struct uclamp_se *uc_se = &p->uclamp[clamp_id];
1011 struct uclamp_bucket *bucket;
Patrick Bellasie4961872019-06-21 09:42:04 +01001012 unsigned int bkt_clamp;
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001013 unsigned int rq_clamp;
1014
1015 lockdep_assert_held(&rq->lock);
1016
Qais Yousef46609ce2020-06-30 12:21:23 +01001017 /*
1018 * If sched_uclamp_used was enabled after task @p was enqueued,
1019 * we could end up with unbalanced call to uclamp_rq_dec_id().
1020 *
1021 * In this case the uc_se->active flag should be false since no uclamp
1022 * accounting was performed at enqueue time and we can just return
1023 * here.
1024 *
1025 * Need to be careful of the following enqeueue/dequeue ordering
1026 * problem too
1027 *
1028 * enqueue(taskA)
1029 * // sched_uclamp_used gets enabled
1030 * enqueue(taskB)
1031 * dequeue(taskA)
1032 * // Must not decrement bukcet->tasks here
1033 * dequeue(taskB)
1034 *
1035 * where we could end up with stale data in uc_se and
1036 * bucket[uc_se->bucket_id].
1037 *
1038 * The following check here eliminates the possibility of such race.
1039 */
1040 if (unlikely(!uc_se->active))
1041 return;
1042
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001043 bucket = &uc_rq->bucket[uc_se->bucket_id];
Qais Yousef46609ce2020-06-30 12:21:23 +01001044
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001045 SCHED_WARN_ON(!bucket->tasks);
1046 if (likely(bucket->tasks))
1047 bucket->tasks--;
Qais Yousef46609ce2020-06-30 12:21:23 +01001048
Patrick Bellasie8f14172019-06-21 09:42:05 +01001049 uc_se->active = false;
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001050
Patrick Bellasi60daf9c2019-06-21 09:42:03 +01001051 /*
1052 * Keep "local max aggregation" simple and accept to (possibly)
1053 * overboost some RUNNABLE tasks in the same bucket.
1054 * The rq clamp bucket value is reset to its base value whenever
1055 * there are no more RUNNABLE tasks refcounting it.
1056 */
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001057 if (likely(bucket->tasks))
1058 return;
1059
1060 rq_clamp = READ_ONCE(uc_rq->value);
1061 /*
1062 * Defensive programming: this should never happen. If it happens,
1063 * e.g. due to future modification, warn and fixup the expected value.
1064 */
1065 SCHED_WARN_ON(bucket->value > rq_clamp);
Patrick Bellasie4961872019-06-21 09:42:04 +01001066 if (bucket->value >= rq_clamp) {
1067 bkt_clamp = uclamp_rq_max_value(rq, clamp_id, uc_se->value);
1068 WRITE_ONCE(uc_rq->value, bkt_clamp);
1069 }
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001070}
1071
1072static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p)
1073{
Patrick Bellasi0413d7f2019-08-22 14:28:11 +01001074 enum uclamp_id clamp_id;
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001075
Qais Yousef46609ce2020-06-30 12:21:23 +01001076 /*
1077 * Avoid any overhead until uclamp is actually used by the userspace.
1078 *
1079 * The condition is constructed such that a NOP is generated when
1080 * sched_uclamp_used is disabled.
1081 */
1082 if (!static_branch_unlikely(&sched_uclamp_used))
1083 return;
1084
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001085 if (unlikely(!p->sched_class->uclamp_enabled))
1086 return;
1087
1088 for_each_clamp_id(clamp_id)
1089 uclamp_rq_inc_id(rq, p, clamp_id);
Patrick Bellasie4961872019-06-21 09:42:04 +01001090
1091 /* Reset clamp idle holding when there is one RUNNABLE task */
1092 if (rq->uclamp_flags & UCLAMP_FLAG_IDLE)
1093 rq->uclamp_flags &= ~UCLAMP_FLAG_IDLE;
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001094}
1095
1096static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p)
1097{
Patrick Bellasi0413d7f2019-08-22 14:28:11 +01001098 enum uclamp_id clamp_id;
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001099
Qais Yousef46609ce2020-06-30 12:21:23 +01001100 /*
1101 * Avoid any overhead until uclamp is actually used by the userspace.
1102 *
1103 * The condition is constructed such that a NOP is generated when
1104 * sched_uclamp_used is disabled.
1105 */
1106 if (!static_branch_unlikely(&sched_uclamp_used))
1107 return;
1108
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001109 if (unlikely(!p->sched_class->uclamp_enabled))
1110 return;
1111
1112 for_each_clamp_id(clamp_id)
1113 uclamp_rq_dec_id(rq, p, clamp_id);
1114}
1115
Patrick Bellasibabbe172019-08-22 14:28:10 +01001116static inline void
Patrick Bellasi0413d7f2019-08-22 14:28:11 +01001117uclamp_update_active(struct task_struct *p, enum uclamp_id clamp_id)
Patrick Bellasibabbe172019-08-22 14:28:10 +01001118{
1119 struct rq_flags rf;
1120 struct rq *rq;
1121
1122 /*
1123 * Lock the task and the rq where the task is (or was) queued.
1124 *
1125 * We might lock the (previous) rq of a !RUNNABLE task, but that's the
1126 * price to pay to safely serialize util_{min,max} updates with
1127 * enqueues, dequeues and migration operations.
1128 * This is the same locking schema used by __set_cpus_allowed_ptr().
1129 */
1130 rq = task_rq_lock(p, &rf);
1131
1132 /*
1133 * Setting the clamp bucket is serialized by task_rq_lock().
1134 * If the task is not yet RUNNABLE and its task_struct is not
1135 * affecting a valid clamp bucket, the next time it's enqueued,
1136 * it will already see the updated clamp bucket value.
1137 */
Qais Yousef6e1ff072019-11-14 21:10:52 +00001138 if (p->uclamp[clamp_id].active) {
Patrick Bellasibabbe172019-08-22 14:28:10 +01001139 uclamp_rq_dec_id(rq, p, clamp_id);
1140 uclamp_rq_inc_id(rq, p, clamp_id);
1141 }
1142
1143 task_rq_unlock(rq, p, &rf);
1144}
1145
Qais Yousefe3b8b6a2019-11-05 11:22:12 +00001146#ifdef CONFIG_UCLAMP_TASK_GROUP
Patrick Bellasibabbe172019-08-22 14:28:10 +01001147static inline void
1148uclamp_update_active_tasks(struct cgroup_subsys_state *css,
1149 unsigned int clamps)
1150{
Patrick Bellasi0413d7f2019-08-22 14:28:11 +01001151 enum uclamp_id clamp_id;
Patrick Bellasibabbe172019-08-22 14:28:10 +01001152 struct css_task_iter it;
1153 struct task_struct *p;
Patrick Bellasibabbe172019-08-22 14:28:10 +01001154
1155 css_task_iter_start(css, 0, &it);
1156 while ((p = css_task_iter_next(&it))) {
1157 for_each_clamp_id(clamp_id) {
1158 if ((0x1 << clamp_id) & clamps)
1159 uclamp_update_active(p, clamp_id);
1160 }
1161 }
1162 css_task_iter_end(&it);
1163}
1164
Patrick Bellasi7274a5c2019-08-22 14:28:08 +01001165static void cpu_util_update_eff(struct cgroup_subsys_state *css);
1166static void uclamp_update_root_tg(void)
1167{
1168 struct task_group *tg = &root_task_group;
1169
1170 uclamp_se_set(&tg->uclamp_req[UCLAMP_MIN],
1171 sysctl_sched_uclamp_util_min, false);
1172 uclamp_se_set(&tg->uclamp_req[UCLAMP_MAX],
1173 sysctl_sched_uclamp_util_max, false);
1174
1175 rcu_read_lock();
1176 cpu_util_update_eff(&root_task_group.css);
1177 rcu_read_unlock();
1178}
1179#else
1180static void uclamp_update_root_tg(void) { }
1181#endif
1182
Patrick Bellasie8f14172019-06-21 09:42:05 +01001183int sysctl_sched_uclamp_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02001184 void *buffer, size_t *lenp, loff_t *ppos)
Patrick Bellasie8f14172019-06-21 09:42:05 +01001185{
Patrick Bellasi7274a5c2019-08-22 14:28:08 +01001186 bool update_root_tg = false;
Patrick Bellasie8f14172019-06-21 09:42:05 +01001187 int old_min, old_max;
Patrick Bellasie8f14172019-06-21 09:42:05 +01001188 int result;
1189
Patrick Bellasi2480c092019-08-22 14:28:06 +01001190 mutex_lock(&uclamp_mutex);
Patrick Bellasie8f14172019-06-21 09:42:05 +01001191 old_min = sysctl_sched_uclamp_util_min;
1192 old_max = sysctl_sched_uclamp_util_max;
1193
1194 result = proc_dointvec(table, write, buffer, lenp, ppos);
1195 if (result)
1196 goto undo;
1197 if (!write)
1198 goto done;
1199
1200 if (sysctl_sched_uclamp_util_min > sysctl_sched_uclamp_util_max ||
1201 sysctl_sched_uclamp_util_max > SCHED_CAPACITY_SCALE) {
1202 result = -EINVAL;
1203 goto undo;
1204 }
1205
1206 if (old_min != sysctl_sched_uclamp_util_min) {
1207 uclamp_se_set(&uclamp_default[UCLAMP_MIN],
Patrick Bellasia509a7c2019-06-21 09:42:07 +01001208 sysctl_sched_uclamp_util_min, false);
Patrick Bellasi7274a5c2019-08-22 14:28:08 +01001209 update_root_tg = true;
Patrick Bellasie8f14172019-06-21 09:42:05 +01001210 }
1211 if (old_max != sysctl_sched_uclamp_util_max) {
1212 uclamp_se_set(&uclamp_default[UCLAMP_MAX],
Patrick Bellasia509a7c2019-06-21 09:42:07 +01001213 sysctl_sched_uclamp_util_max, false);
Patrick Bellasi7274a5c2019-08-22 14:28:08 +01001214 update_root_tg = true;
Patrick Bellasie8f14172019-06-21 09:42:05 +01001215 }
1216
Qais Yousef46609ce2020-06-30 12:21:23 +01001217 if (update_root_tg) {
1218 static_branch_enable(&sched_uclamp_used);
Patrick Bellasi7274a5c2019-08-22 14:28:08 +01001219 uclamp_update_root_tg();
Qais Yousef46609ce2020-06-30 12:21:23 +01001220 }
Patrick Bellasi7274a5c2019-08-22 14:28:08 +01001221
Patrick Bellasie8f14172019-06-21 09:42:05 +01001222 /*
Patrick Bellasi7274a5c2019-08-22 14:28:08 +01001223 * We update all RUNNABLE tasks only when task groups are in use.
1224 * Otherwise, keep it simple and do just a lazy update at each next
1225 * task enqueue time.
Patrick Bellasie8f14172019-06-21 09:42:05 +01001226 */
Patrick Bellasi7274a5c2019-08-22 14:28:08 +01001227
Patrick Bellasie8f14172019-06-21 09:42:05 +01001228 goto done;
1229
1230undo:
1231 sysctl_sched_uclamp_util_min = old_min;
1232 sysctl_sched_uclamp_util_max = old_max;
1233done:
Patrick Bellasi2480c092019-08-22 14:28:06 +01001234 mutex_unlock(&uclamp_mutex);
Patrick Bellasie8f14172019-06-21 09:42:05 +01001235
1236 return result;
1237}
1238
Patrick Bellasia509a7c2019-06-21 09:42:07 +01001239static int uclamp_validate(struct task_struct *p,
1240 const struct sched_attr *attr)
1241{
1242 unsigned int lower_bound = p->uclamp_req[UCLAMP_MIN].value;
1243 unsigned int upper_bound = p->uclamp_req[UCLAMP_MAX].value;
1244
1245 if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN)
1246 lower_bound = attr->sched_util_min;
1247 if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX)
1248 upper_bound = attr->sched_util_max;
1249
1250 if (lower_bound > upper_bound)
1251 return -EINVAL;
1252 if (upper_bound > SCHED_CAPACITY_SCALE)
1253 return -EINVAL;
1254
1255 return 0;
1256}
1257
1258static void __setscheduler_uclamp(struct task_struct *p,
1259 const struct sched_attr *attr)
1260{
Patrick Bellasi0413d7f2019-08-22 14:28:11 +01001261 enum uclamp_id clamp_id;
Patrick Bellasi1a00d992019-06-21 09:42:09 +01001262
1263 /*
1264 * On scheduling class change, reset to default clamps for tasks
1265 * without a task-specific value.
1266 */
1267 for_each_clamp_id(clamp_id) {
1268 struct uclamp_se *uc_se = &p->uclamp_req[clamp_id];
1269 unsigned int clamp_value = uclamp_none(clamp_id);
1270
1271 /* Keep using defined clamps across class changes */
1272 if (uc_se->user_defined)
1273 continue;
1274
1275 /* By default, RT tasks always get 100% boost */
1276 if (unlikely(rt_task(p) && clamp_id == UCLAMP_MIN))
1277 clamp_value = uclamp_none(UCLAMP_MAX);
1278
1279 uclamp_se_set(uc_se, clamp_value, false);
1280 }
1281
Patrick Bellasia509a7c2019-06-21 09:42:07 +01001282 if (likely(!(attr->sched_flags & SCHED_FLAG_UTIL_CLAMP)))
1283 return;
1284
Qais Yousef46609ce2020-06-30 12:21:23 +01001285 static_branch_enable(&sched_uclamp_used);
1286
Patrick Bellasia509a7c2019-06-21 09:42:07 +01001287 if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MIN) {
1288 uclamp_se_set(&p->uclamp_req[UCLAMP_MIN],
1289 attr->sched_util_min, true);
1290 }
1291
1292 if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP_MAX) {
1293 uclamp_se_set(&p->uclamp_req[UCLAMP_MAX],
1294 attr->sched_util_max, true);
1295 }
1296}
1297
Patrick Bellasie8f14172019-06-21 09:42:05 +01001298static void uclamp_fork(struct task_struct *p)
1299{
Patrick Bellasi0413d7f2019-08-22 14:28:11 +01001300 enum uclamp_id clamp_id;
Patrick Bellasie8f14172019-06-21 09:42:05 +01001301
1302 for_each_clamp_id(clamp_id)
1303 p->uclamp[clamp_id].active = false;
Patrick Bellasia87498a2019-06-21 09:42:08 +01001304
1305 if (likely(!p->sched_reset_on_fork))
1306 return;
1307
1308 for_each_clamp_id(clamp_id) {
Quentin Perreteaf5a922020-04-16 09:59:56 +01001309 uclamp_se_set(&p->uclamp_req[clamp_id],
1310 uclamp_none(clamp_id), false);
Patrick Bellasia87498a2019-06-21 09:42:08 +01001311 }
Patrick Bellasie8f14172019-06-21 09:42:05 +01001312}
1313
Qais Yousefd81ae8a2020-06-30 12:21:22 +01001314static void __init init_uclamp_rq(struct rq *rq)
1315{
1316 enum uclamp_id clamp_id;
1317 struct uclamp_rq *uc_rq = rq->uclamp;
1318
1319 for_each_clamp_id(clamp_id) {
1320 uc_rq[clamp_id] = (struct uclamp_rq) {
1321 .value = uclamp_none(clamp_id)
1322 };
1323 }
1324
1325 rq->uclamp_flags = 0;
1326}
1327
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001328static void __init init_uclamp(void)
1329{
Patrick Bellasie8f14172019-06-21 09:42:05 +01001330 struct uclamp_se uc_max = {};
Patrick Bellasi0413d7f2019-08-22 14:28:11 +01001331 enum uclamp_id clamp_id;
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001332 int cpu;
1333
Patrick Bellasi2480c092019-08-22 14:28:06 +01001334 mutex_init(&uclamp_mutex);
1335
Qais Yousefd81ae8a2020-06-30 12:21:22 +01001336 for_each_possible_cpu(cpu)
1337 init_uclamp_rq(cpu_rq(cpu));
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001338
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001339 for_each_clamp_id(clamp_id) {
Patrick Bellasie8f14172019-06-21 09:42:05 +01001340 uclamp_se_set(&init_task.uclamp_req[clamp_id],
Patrick Bellasia509a7c2019-06-21 09:42:07 +01001341 uclamp_none(clamp_id), false);
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001342 }
Patrick Bellasie8f14172019-06-21 09:42:05 +01001343
1344 /* System defaults allow max clamp values for both indexes */
Patrick Bellasia509a7c2019-06-21 09:42:07 +01001345 uclamp_se_set(&uc_max, uclamp_none(UCLAMP_MAX), false);
Patrick Bellasi2480c092019-08-22 14:28:06 +01001346 for_each_clamp_id(clamp_id) {
Patrick Bellasie8f14172019-06-21 09:42:05 +01001347 uclamp_default[clamp_id] = uc_max;
Patrick Bellasi2480c092019-08-22 14:28:06 +01001348#ifdef CONFIG_UCLAMP_TASK_GROUP
1349 root_task_group.uclamp_req[clamp_id] = uc_max;
Patrick Bellasi0b60ba22019-08-22 14:28:07 +01001350 root_task_group.uclamp[clamp_id] = uc_max;
Patrick Bellasi2480c092019-08-22 14:28:06 +01001351#endif
1352 }
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001353}
1354
1355#else /* CONFIG_UCLAMP_TASK */
1356static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p) { }
1357static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p) { }
Patrick Bellasia509a7c2019-06-21 09:42:07 +01001358static inline int uclamp_validate(struct task_struct *p,
1359 const struct sched_attr *attr)
1360{
1361 return -EOPNOTSUPP;
1362}
1363static void __setscheduler_uclamp(struct task_struct *p,
1364 const struct sched_attr *attr) { }
Patrick Bellasie8f14172019-06-21 09:42:05 +01001365static inline void uclamp_fork(struct task_struct *p) { }
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001366static inline void init_uclamp(void) { }
1367#endif /* CONFIG_UCLAMP_TASK */
1368
Peter Zijlstra1de64442015-09-30 17:44:13 +02001369static inline void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
Gregory Haskins2087a1a2008-06-27 14:30:00 -06001370{
Peter Zijlstra0a67d1e2016-10-04 16:29:45 +02001371 if (!(flags & ENQUEUE_NOCLOCK))
1372 update_rq_clock(rq);
1373
Johannes Weinereb414682018-10-26 15:06:27 -07001374 if (!(flags & ENQUEUE_RESTORE)) {
Peter Zijlstra1de64442015-09-30 17:44:13 +02001375 sched_info_queued(rq, p);
Johannes Weinereb414682018-10-26 15:06:27 -07001376 psi_enqueue(p, flags & ENQUEUE_WAKEUP);
1377 }
Peter Zijlstra0a67d1e2016-10-04 16:29:45 +02001378
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001379 uclamp_rq_inc(rq, p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001380 p->sched_class->enqueue_task(rq, p, flags);
Ingo Molnardd41f592007-07-09 18:51:59 +02001381}
1382
Peter Zijlstra1de64442015-09-30 17:44:13 +02001383static inline void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
Ingo Molnardd41f592007-07-09 18:51:59 +02001384{
Peter Zijlstra0a67d1e2016-10-04 16:29:45 +02001385 if (!(flags & DEQUEUE_NOCLOCK))
1386 update_rq_clock(rq);
1387
Johannes Weinereb414682018-10-26 15:06:27 -07001388 if (!(flags & DEQUEUE_SAVE)) {
Peter Zijlstra1de64442015-09-30 17:44:13 +02001389 sched_info_dequeued(rq, p);
Johannes Weinereb414682018-10-26 15:06:27 -07001390 psi_dequeue(p, flags & DEQUEUE_SLEEP);
1391 }
Peter Zijlstra0a67d1e2016-10-04 16:29:45 +02001392
Patrick Bellasi69842cb2019-06-21 09:42:02 +01001393 uclamp_rq_dec(rq, p);
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001394 p->sched_class->dequeue_task(rq, p, flags);
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001395}
1396
Peter Zijlstra029632f2011-10-25 10:00:11 +02001397void activate_task(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001398{
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001399 enqueue_task(rq, p, flags);
Peter Zijlstra7dd77882019-04-09 09:59:05 +02001400
1401 p->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001402}
1403
Peter Zijlstra029632f2011-10-25 10:00:11 +02001404void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001405{
Peter Zijlstra7dd77882019-04-09 09:59:05 +02001406 p->on_rq = (flags & DEQUEUE_SLEEP) ? 0 : TASK_ON_RQ_MIGRATING;
1407
Peter Zijlstra371fd7e2010-03-24 16:38:48 +01001408 dequeue_task(rq, p, flags);
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001409}
1410
Peter Zijlstra1e3c88b2009-12-17 17:00:43 +01001411/*
Ingo Molnardd41f592007-07-09 18:51:59 +02001412 * __normal_prio - return the priority that is based on the static prio
Ingo Molnar71f8bd42007-07-09 18:51:59 +02001413 */
Ingo Molnar14531182007-07-09 18:51:59 +02001414static inline int __normal_prio(struct task_struct *p)
1415{
Ingo Molnardd41f592007-07-09 18:51:59 +02001416 return p->static_prio;
Ingo Molnar14531182007-07-09 18:51:59 +02001417}
1418
1419/*
Ingo Molnarb29739f2006-06-27 02:54:51 -07001420 * Calculate the expected normal priority: i.e. priority
1421 * without taking RT-inheritance into account. Might be
1422 * boosted by interactivity modifiers. Changes upon fork,
1423 * setprio syscalls, and whenever the interactivity
1424 * estimator recalculates.
1425 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001426static inline int normal_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001427{
1428 int prio;
1429
Dario Faggioliaab03e02013-11-28 11:14:43 +01001430 if (task_has_dl_policy(p))
1431 prio = MAX_DL_PRIO-1;
1432 else if (task_has_rt_policy(p))
Ingo Molnarb29739f2006-06-27 02:54:51 -07001433 prio = MAX_RT_PRIO-1 - p->rt_priority;
1434 else
1435 prio = __normal_prio(p);
1436 return prio;
1437}
1438
1439/*
1440 * Calculate the current priority, i.e. the priority
1441 * taken into account by the scheduler. This value might
1442 * be boosted by RT tasks, or might be boosted by
1443 * interactivity modifiers. Will be RT if the task got
1444 * RT-boosted. If not then it returns p->normal_prio.
1445 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001446static int effective_prio(struct task_struct *p)
Ingo Molnarb29739f2006-06-27 02:54:51 -07001447{
1448 p->normal_prio = normal_prio(p);
1449 /*
1450 * If we are RT tasks or we were boosted to RT priority,
1451 * keep the priority unchanged. Otherwise, update priority
1452 * to the normal priority:
1453 */
1454 if (!rt_prio(p->prio))
1455 return p->normal_prio;
1456 return p->prio;
1457}
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459/**
1460 * task_curr - is this task currently executing on a CPU?
1461 * @p: the task in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02001462 *
1463 * Return: 1 if the task is currently executing. 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07001465inline int task_curr(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
1467 return cpu_curr(task_cpu(p)) == p;
1468}
1469
Kirill Tkhai67dfa1b2014-10-27 17:40:52 +03001470/*
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02001471 * switched_from, switched_to and prio_changed must _NOT_ drop rq->lock,
1472 * use the balance_callback list if you want balancing.
1473 *
1474 * this means any call to check_class_changed() must be followed by a call to
1475 * balance_callback().
Kirill Tkhai67dfa1b2014-10-27 17:40:52 +03001476 */
Steven Rostedtcb469842008-01-25 21:08:22 +01001477static inline void check_class_changed(struct rq *rq, struct task_struct *p,
1478 const struct sched_class *prev_class,
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001479 int oldprio)
Steven Rostedtcb469842008-01-25 21:08:22 +01001480{
1481 if (prev_class != p->sched_class) {
1482 if (prev_class->switched_from)
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001483 prev_class->switched_from(rq, p);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02001484
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001485 p->sched_class->switched_to(rq, p);
Dario Faggioli2d3d8912013-11-07 14:43:44 +01001486 } else if (oldprio != p->prio || dl_task(p))
Peter Zijlstrada7a7352011-01-17 17:03:27 +01001487 p->sched_class->prio_changed(rq, p, oldprio);
Steven Rostedtcb469842008-01-25 21:08:22 +01001488}
1489
Peter Zijlstra029632f2011-10-25 10:00:11 +02001490void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
Peter Zijlstra1e5a7402010-10-31 12:37:04 +01001491{
Kirill Tkhaiaa93cd52019-12-19 16:44:55 -05001492 if (p->sched_class == rq->curr->sched_class)
Peter Zijlstra1e5a7402010-10-31 12:37:04 +01001493 rq->curr->sched_class->check_preempt_curr(rq, p, flags);
Kirill Tkhaiaa93cd52019-12-19 16:44:55 -05001494 else if (p->sched_class > rq->curr->sched_class)
1495 resched_curr(rq);
Peter Zijlstra1e5a7402010-10-31 12:37:04 +01001496
1497 /*
1498 * A queue event has occurred, and we're going to schedule. In
1499 * this case, we can save a useless back to back clock update.
1500 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001501 if (task_on_rq_queued(rq->curr) && test_tsk_need_resched(rq->curr))
Davidlohr Buesoadcc8da2018-04-04 09:15:39 -07001502 rq_clock_skip_update(rq);
Peter Zijlstra1e5a7402010-10-31 12:37:04 +01001503}
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505#ifdef CONFIG_SMP
Peter Zijlstra175f0e22017-07-25 18:58:21 +02001506
Peter Zijlstra175f0e22017-07-25 18:58:21 +02001507/*
Joel Savitzbee98532019-03-06 20:13:33 -05001508 * Per-CPU kthreads are allowed to run on !active && online CPUs, see
Peter Zijlstra175f0e22017-07-25 18:58:21 +02001509 * __set_cpus_allowed_ptr() and select_fallback_rq().
1510 */
1511static inline bool is_cpu_allowed(struct task_struct *p, int cpu)
1512{
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02001513 if (!cpumask_test_cpu(cpu, p->cpus_ptr))
Peter Zijlstra175f0e22017-07-25 18:58:21 +02001514 return false;
1515
1516 if (is_per_cpu_kthread(p))
1517 return cpu_online(cpu);
1518
1519 return cpu_active(cpu);
1520}
1521
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001522/*
1523 * This is how migration works:
1524 *
1525 * 1) we invoke migration_cpu_stop() on the target CPU using
1526 * stop_one_cpu().
1527 * 2) stopper starts to run (implicitly forcing the migrated thread
1528 * off the CPU)
1529 * 3) it checks whether the migrated task is still in the wrong runqueue.
1530 * 4) if it's in the wrong runqueue then the migration thread removes
1531 * it and puts it into the right queue.
1532 * 5) stopper completes and stop_one_cpu() returns and the migration
1533 * is done.
1534 */
1535
1536/*
1537 * move_queued_task - move a queued task to new rq.
1538 *
1539 * Returns (locked) new rq. Old rq's lock is released.
1540 */
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001541static struct rq *move_queued_task(struct rq *rq, struct rq_flags *rf,
1542 struct task_struct *p, int new_cpu)
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001543{
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001544 lockdep_assert_held(&rq->lock);
1545
Andrea Parric5469512019-01-21 16:52:40 +01001546 WRITE_ONCE(p->on_rq, TASK_ON_RQ_MIGRATING);
Peter Zijlstra15ff9912016-10-05 17:59:32 +02001547 dequeue_task(rq, p, DEQUEUE_NOCLOCK);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001548 set_task_cpu(p, new_cpu);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001549 rq_unlock(rq, rf);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001550
1551 rq = cpu_rq(new_cpu);
1552
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001553 rq_lock(rq, rf);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001554 BUG_ON(task_cpu(p) != new_cpu);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001555 enqueue_task(rq, p, 0);
Joonwoo Park3ea94de2015-11-12 19:38:54 -08001556 p->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001557 check_preempt_curr(rq, p, 0);
1558
1559 return rq;
1560}
1561
1562struct migration_arg {
1563 struct task_struct *task;
1564 int dest_cpu;
1565};
1566
1567/*
Ingo Molnard1ccc662017-02-01 11:46:42 +01001568 * Move (not current) task off this CPU, onto the destination CPU. We're doing
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001569 * this because either it can't run here any more (set_cpus_allowed()
1570 * away from this CPU, or CPU going down), or because we're
1571 * attempting to rebalance this task on exec (sched_exec).
1572 *
1573 * So we race with normal scheduler movements, but that's OK, as long
1574 * as the task is no longer on this CPU.
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001575 */
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001576static struct rq *__migrate_task(struct rq *rq, struct rq_flags *rf,
1577 struct task_struct *p, int dest_cpu)
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001578{
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001579 /* Affinity changed (again). */
Peter Zijlstra175f0e22017-07-25 18:58:21 +02001580 if (!is_cpu_allowed(p, dest_cpu))
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001581 return rq;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001582
Peter Zijlstra15ff9912016-10-05 17:59:32 +02001583 update_rq_clock(rq);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001584 rq = move_queued_task(rq, rf, p, dest_cpu);
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001585
1586 return rq;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001587}
1588
1589/*
1590 * migration_cpu_stop - this will be executed by a highprio stopper thread
1591 * and performs thread migration by bumping thread off CPU then
1592 * 'pushing' onto another runqueue.
1593 */
1594static int migration_cpu_stop(void *data)
1595{
1596 struct migration_arg *arg = data;
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001597 struct task_struct *p = arg->task;
1598 struct rq *rq = this_rq();
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001599 struct rq_flags rf;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001600
1601 /*
Ingo Molnard1ccc662017-02-01 11:46:42 +01001602 * The original target CPU might have gone down and we might
1603 * be on another CPU but it doesn't matter.
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001604 */
1605 local_irq_disable();
1606 /*
1607 * We need to explicitly wake pending tasks before running
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02001608 * __migrate_task() such that we will not miss enforcing cpus_ptr
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001609 * during wakeups, see set_cpus_allowed_ptr()'s TASK_WAKING test.
1610 */
Peter Zijlstraa14886642020-05-26 18:11:04 +02001611 flush_smp_call_function_from_idle();
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001612
1613 raw_spin_lock(&p->pi_lock);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001614 rq_lock(rq, &rf);
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001615 /*
1616 * If task_rq(p) != rq, it cannot be migrated here, because we're
1617 * holding rq->lock, if p->on_rq == 0 it cannot get enqueued because
1618 * we're holding p->pi_lock.
1619 */
Cheng Chaobf89a302016-09-14 10:01:50 +08001620 if (task_rq(p) == rq) {
1621 if (task_on_rq_queued(p))
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001622 rq = __migrate_task(rq, &rf, p, arg->dest_cpu);
Cheng Chaobf89a302016-09-14 10:01:50 +08001623 else
1624 p->wake_cpu = arg->dest_cpu;
1625 }
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001626 rq_unlock(rq, &rf);
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02001627 raw_spin_unlock(&p->pi_lock);
1628
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001629 local_irq_enable();
1630 return 0;
1631}
1632
Peter Zijlstrac5b28032015-05-15 17:43:35 +02001633/*
1634 * sched_class::set_cpus_allowed must do the below, but is not required to
1635 * actually call this function.
1636 */
1637void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask)
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001638{
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02001639 cpumask_copy(&p->cpus_mask, new_mask);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001640 p->nr_cpus_allowed = cpumask_weight(new_mask);
1641}
1642
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001643void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
1644{
Peter Zijlstra6c370672015-05-15 17:43:36 +02001645 struct rq *rq = task_rq(p);
1646 bool queued, running;
1647
Peter Zijlstra25834c72015-05-15 17:43:34 +02001648 lockdep_assert_held(&p->pi_lock);
Peter Zijlstra6c370672015-05-15 17:43:36 +02001649
1650 queued = task_on_rq_queued(p);
1651 running = task_current(rq, p);
1652
1653 if (queued) {
1654 /*
1655 * Because __kthread_bind() calls this on blocked tasks without
1656 * holding rq->lock.
1657 */
1658 lockdep_assert_held(&rq->lock);
Peter Zijlstra7a57f322017-02-21 14:47:02 +01001659 dequeue_task(rq, p, DEQUEUE_SAVE | DEQUEUE_NOCLOCK);
Peter Zijlstra6c370672015-05-15 17:43:36 +02001660 }
1661 if (running)
1662 put_prev_task(rq, p);
1663
Peter Zijlstrac5b28032015-05-15 17:43:35 +02001664 p->sched_class->set_cpus_allowed(p, new_mask);
Peter Zijlstra6c370672015-05-15 17:43:36 +02001665
Peter Zijlstra6c370672015-05-15 17:43:36 +02001666 if (queued)
Peter Zijlstra7134b3e2017-02-21 14:23:38 +01001667 enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK);
Vincent Guittota399d232016-09-12 09:47:52 +02001668 if (running)
Peter Zijlstra03b7fad2019-05-29 20:36:41 +00001669 set_next_task(rq, p);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001670}
1671
1672/*
1673 * Change a given task's CPU affinity. Migrate the thread to a
1674 * proper CPU and schedule it away if the CPU it's executing on
1675 * is removed from the allowed bitmask.
1676 *
1677 * NOTE: the caller must have a valid reference to the task, the
1678 * task must not exit() & deallocate itself prematurely. The
1679 * call is not atomic; no spinlocks may be held.
1680 */
Peter Zijlstra25834c72015-05-15 17:43:34 +02001681static int __set_cpus_allowed_ptr(struct task_struct *p,
1682 const struct cpumask *new_mask, bool check)
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001683{
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01001684 const struct cpumask *cpu_valid_mask = cpu_active_mask;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001685 unsigned int dest_cpu;
Peter Zijlstraeb580752015-07-31 21:28:18 +02001686 struct rq_flags rf;
1687 struct rq *rq;
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001688 int ret = 0;
1689
Peter Zijlstraeb580752015-07-31 21:28:18 +02001690 rq = task_rq_lock(p, &rf);
Wanpeng Lia499c3e2017-02-21 23:52:55 -08001691 update_rq_clock(rq);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001692
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01001693 if (p->flags & PF_KTHREAD) {
1694 /*
1695 * Kernel threads are allowed on online && !active CPUs
1696 */
1697 cpu_valid_mask = cpu_online_mask;
1698 }
1699
Peter Zijlstra25834c72015-05-15 17:43:34 +02001700 /*
1701 * Must re-check here, to close a race against __kthread_bind(),
1702 * sched_setaffinity() is not guaranteed to observe the flag.
1703 */
1704 if (check && (p->flags & PF_NO_SETAFFINITY)) {
1705 ret = -EINVAL;
1706 goto out;
1707 }
1708
Scott Woodfd844ba2020-06-17 14:17:42 +02001709 if (cpumask_equal(&p->cpus_mask, new_mask))
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001710 goto out;
1711
Paul Turner46a87b32020-03-10 18:01:13 -07001712 /*
1713 * Picking a ~random cpu helps in cases where we are changing affinity
1714 * for groups of tasks (ie. cpuset), so that load balancing is not
1715 * immediately required to distribute the tasks within their new mask.
1716 */
1717 dest_cpu = cpumask_any_and_distribute(cpu_valid_mask, new_mask);
KeMeng Shi714e5012019-09-16 06:53:28 +00001718 if (dest_cpu >= nr_cpu_ids) {
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001719 ret = -EINVAL;
1720 goto out;
1721 }
1722
1723 do_set_cpus_allowed(p, new_mask);
1724
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01001725 if (p->flags & PF_KTHREAD) {
1726 /*
1727 * For kernel threads that do indeed end up on online &&
Ingo Molnard1ccc662017-02-01 11:46:42 +01001728 * !active we want to ensure they are strict per-CPU threads.
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01001729 */
1730 WARN_ON(cpumask_intersects(new_mask, cpu_online_mask) &&
1731 !cpumask_intersects(new_mask, cpu_active_mask) &&
1732 p->nr_cpus_allowed != 1);
1733 }
1734
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001735 /* Can the task run on the task's current CPU? If so, we're done */
1736 if (cpumask_test_cpu(task_cpu(p), new_mask))
1737 goto out;
1738
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001739 if (task_running(rq, p) || p->state == TASK_WAKING) {
1740 struct migration_arg arg = { p, dest_cpu };
1741 /* Need help from migration thread: drop lock and wait. */
Peter Zijlstraeb580752015-07-31 21:28:18 +02001742 task_rq_unlock(rq, p, &rf);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001743 stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001744 return 0;
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001745 } else if (task_on_rq_queued(p)) {
1746 /*
1747 * OK, since we're going to drop the lock immediately
1748 * afterwards anyway.
1749 */
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001750 rq = move_queued_task(rq, &rf, p, dest_cpu);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02001751 }
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001752out:
Peter Zijlstraeb580752015-07-31 21:28:18 +02001753 task_rq_unlock(rq, p, &rf);
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001754
1755 return ret;
1756}
Peter Zijlstra25834c72015-05-15 17:43:34 +02001757
1758int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
1759{
1760 return __set_cpus_allowed_ptr(p, new_mask, false);
1761}
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02001762EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
1763
Ingo Molnardd41f592007-07-09 18:51:59 +02001764void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
Ingo Molnarc65cc872007-07-09 18:51:58 +02001765{
Peter Zijlstrae2912002009-12-16 18:04:36 +01001766#ifdef CONFIG_SCHED_DEBUG
1767 /*
1768 * We should never call set_task_cpu() on a blocked task,
1769 * ttwu() will sort out the placement.
1770 */
Peter Zijlstra077614e2009-12-17 13:16:31 +01001771 WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING &&
Oleg Nesterove2336f62014-10-08 20:33:48 +02001772 !p->on_rq);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001773
Joonwoo Park3ea94de2015-11-12 19:38:54 -08001774 /*
1775 * Migrating fair class task must have p->on_rq = TASK_ON_RQ_MIGRATING,
1776 * because schedstat_wait_{start,end} rebase migrating task's wait_start
1777 * time relying on p->on_rq.
1778 */
1779 WARN_ON_ONCE(p->state == TASK_RUNNING &&
1780 p->sched_class == &fair_sched_class &&
1781 (p->on_rq && !task_on_rq_migrating(p)));
1782
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001783#ifdef CONFIG_LOCKDEP
Peter Zijlstra6c6c54e2011-06-03 17:37:07 +02001784 /*
1785 * The caller should hold either p->pi_lock or rq->lock, when changing
1786 * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks.
1787 *
1788 * sched_move_task() holds both and thus holding either pins the cgroup,
Peter Zijlstra8323f262012-06-22 13:36:05 +02001789 * see task_group().
Peter Zijlstra6c6c54e2011-06-03 17:37:07 +02001790 *
1791 * Furthermore, all task_rq users should acquire both locks, see
1792 * task_rq_lock().
1793 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02001794 WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) ||
1795 lockdep_is_held(&task_rq(p)->lock)));
1796#endif
Peter Zijlstra4ff90832017-09-07 17:03:52 +02001797 /*
1798 * Clearly, migrating tasks to offline CPUs is a fairly daft thing.
1799 */
1800 WARN_ON_ONCE(!cpu_online(new_cpu));
Peter Zijlstrae2912002009-12-16 18:04:36 +01001801#endif
1802
Mathieu Desnoyersde1d7282009-05-05 16:49:59 +08001803 trace_sched_migrate_task(p, new_cpu);
Peter Zijlstracbc34ed2008-12-10 08:08:22 +01001804
Peter Zijlstra0c697742009-12-22 15:43:19 +01001805 if (task_cpu(p) != new_cpu) {
Paul Turner0a74bef2012-10-04 13:18:30 +02001806 if (p->sched_class->migrate_task_rq)
Srikar Dronamraju1327237a52018-09-21 23:18:57 +05301807 p->sched_class->migrate_task_rq(p, new_cpu);
Peter Zijlstra0c697742009-12-22 15:43:19 +01001808 p->se.nr_migrations++;
Mathieu Desnoyersd7822b12018-06-02 08:43:54 -04001809 rseq_migrate(p);
Peter Zijlstraff303e62015-04-17 20:05:30 +02001810 perf_event_task_migrate(p);
Peter Zijlstra0c697742009-12-22 15:43:19 +01001811 }
Ingo Molnardd41f592007-07-09 18:51:59 +02001812
1813 __set_task_cpu(p, new_cpu);
Ingo Molnarc65cc872007-07-09 18:51:58 +02001814}
1815
Srikar Dronamraju0ad4e3d2018-06-20 22:32:50 +05301816#ifdef CONFIG_NUMA_BALANCING
Peter Zijlstraac66f542013-10-07 11:29:16 +01001817static void __migrate_swap_task(struct task_struct *p, int cpu)
1818{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001819 if (task_on_rq_queued(p)) {
Peter Zijlstraac66f542013-10-07 11:29:16 +01001820 struct rq *src_rq, *dst_rq;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001821 struct rq_flags srf, drf;
Peter Zijlstraac66f542013-10-07 11:29:16 +01001822
1823 src_rq = task_rq(p);
1824 dst_rq = cpu_rq(cpu);
1825
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001826 rq_pin_lock(src_rq, &srf);
1827 rq_pin_lock(dst_rq, &drf);
1828
Peter Zijlstraac66f542013-10-07 11:29:16 +01001829 deactivate_task(src_rq, p, 0);
1830 set_task_cpu(p, cpu);
1831 activate_task(dst_rq, p, 0);
1832 check_preempt_curr(dst_rq, p, 0);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001833
1834 rq_unpin_lock(dst_rq, &drf);
1835 rq_unpin_lock(src_rq, &srf);
1836
Peter Zijlstraac66f542013-10-07 11:29:16 +01001837 } else {
1838 /*
1839 * Task isn't running anymore; make it appear like we migrated
1840 * it before it went to sleep. This means on wakeup we make the
Ingo Molnard1ccc662017-02-01 11:46:42 +01001841 * previous CPU our target instead of where it really is.
Peter Zijlstraac66f542013-10-07 11:29:16 +01001842 */
1843 p->wake_cpu = cpu;
1844 }
1845}
1846
1847struct migration_swap_arg {
1848 struct task_struct *src_task, *dst_task;
1849 int src_cpu, dst_cpu;
1850};
1851
1852static int migrate_swap_stop(void *data)
1853{
1854 struct migration_swap_arg *arg = data;
1855 struct rq *src_rq, *dst_rq;
1856 int ret = -EAGAIN;
1857
Peter Zijlstra62694cd2015-10-09 18:36:29 +02001858 if (!cpu_active(arg->src_cpu) || !cpu_active(arg->dst_cpu))
1859 return -EAGAIN;
1860
Peter Zijlstraac66f542013-10-07 11:29:16 +01001861 src_rq = cpu_rq(arg->src_cpu);
1862 dst_rq = cpu_rq(arg->dst_cpu);
1863
Peter Zijlstra74602312013-10-10 20:17:22 +02001864 double_raw_lock(&arg->src_task->pi_lock,
1865 &arg->dst_task->pi_lock);
Peter Zijlstraac66f542013-10-07 11:29:16 +01001866 double_rq_lock(src_rq, dst_rq);
Peter Zijlstra62694cd2015-10-09 18:36:29 +02001867
Peter Zijlstraac66f542013-10-07 11:29:16 +01001868 if (task_cpu(arg->dst_task) != arg->dst_cpu)
1869 goto unlock;
1870
1871 if (task_cpu(arg->src_task) != arg->src_cpu)
1872 goto unlock;
1873
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02001874 if (!cpumask_test_cpu(arg->dst_cpu, arg->src_task->cpus_ptr))
Peter Zijlstraac66f542013-10-07 11:29:16 +01001875 goto unlock;
1876
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02001877 if (!cpumask_test_cpu(arg->src_cpu, arg->dst_task->cpus_ptr))
Peter Zijlstraac66f542013-10-07 11:29:16 +01001878 goto unlock;
1879
1880 __migrate_swap_task(arg->src_task, arg->dst_cpu);
1881 __migrate_swap_task(arg->dst_task, arg->src_cpu);
1882
1883 ret = 0;
1884
1885unlock:
1886 double_rq_unlock(src_rq, dst_rq);
Peter Zijlstra74602312013-10-10 20:17:22 +02001887 raw_spin_unlock(&arg->dst_task->pi_lock);
1888 raw_spin_unlock(&arg->src_task->pi_lock);
Peter Zijlstraac66f542013-10-07 11:29:16 +01001889
1890 return ret;
1891}
1892
1893/*
1894 * Cross migrate two tasks
1895 */
Srikar Dronamraju0ad4e3d2018-06-20 22:32:50 +05301896int migrate_swap(struct task_struct *cur, struct task_struct *p,
1897 int target_cpu, int curr_cpu)
Peter Zijlstraac66f542013-10-07 11:29:16 +01001898{
1899 struct migration_swap_arg arg;
1900 int ret = -EINVAL;
1901
Peter Zijlstraac66f542013-10-07 11:29:16 +01001902 arg = (struct migration_swap_arg){
1903 .src_task = cur,
Srikar Dronamraju0ad4e3d2018-06-20 22:32:50 +05301904 .src_cpu = curr_cpu,
Peter Zijlstraac66f542013-10-07 11:29:16 +01001905 .dst_task = p,
Srikar Dronamraju0ad4e3d2018-06-20 22:32:50 +05301906 .dst_cpu = target_cpu,
Peter Zijlstraac66f542013-10-07 11:29:16 +01001907 };
1908
1909 if (arg.src_cpu == arg.dst_cpu)
1910 goto out;
1911
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02001912 /*
1913 * These three tests are all lockless; this is OK since all of them
1914 * will be re-checked with proper locks held further down the line.
1915 */
Peter Zijlstraac66f542013-10-07 11:29:16 +01001916 if (!cpu_active(arg.src_cpu) || !cpu_active(arg.dst_cpu))
1917 goto out;
1918
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02001919 if (!cpumask_test_cpu(arg.dst_cpu, arg.src_task->cpus_ptr))
Peter Zijlstraac66f542013-10-07 11:29:16 +01001920 goto out;
1921
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02001922 if (!cpumask_test_cpu(arg.src_cpu, arg.dst_task->cpus_ptr))
Peter Zijlstraac66f542013-10-07 11:29:16 +01001923 goto out;
1924
Mel Gorman286549d2014-01-21 15:51:03 -08001925 trace_sched_swap_numa(cur, arg.src_cpu, p, arg.dst_cpu);
Peter Zijlstraac66f542013-10-07 11:29:16 +01001926 ret = stop_two_cpus(arg.dst_cpu, arg.src_cpu, migrate_swap_stop, &arg);
1927
1928out:
Peter Zijlstraac66f542013-10-07 11:29:16 +01001929 return ret;
1930}
Srikar Dronamraju0ad4e3d2018-06-20 22:32:50 +05301931#endif /* CONFIG_NUMA_BALANCING */
Peter Zijlstraac66f542013-10-07 11:29:16 +01001932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 * wait_task_inactive - wait for a thread to unschedule.
1935 *
Roland McGrath85ba2d82008-07-25 19:45:58 -07001936 * If @match_state is nonzero, it's the @p->state value just checked and
1937 * not expected to change. If it changes, i.e. @p might have woken up,
1938 * then return zero. When we succeed in waiting for @p to be off its CPU,
1939 * we return a positive number (its total switch count). If a second call
1940 * a short while later returns the same number, the caller can be sure that
1941 * @p has remained unscheduled the whole time.
1942 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 * The caller must ensure that the task *will* unschedule sometime soon,
1944 * else this function might spin for a *long* time. This function can't
1945 * be called with interrupts off, or it may introduce deadlock with
1946 * smp_call_function() if an IPI is sent by the same process we are
1947 * waiting to become inactive.
1948 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07001949unsigned long wait_task_inactive(struct task_struct *p, long match_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001951 int running, queued;
Peter Zijlstraeb580752015-07-31 21:28:18 +02001952 struct rq_flags rf;
Roland McGrath85ba2d82008-07-25 19:45:58 -07001953 unsigned long ncsw;
Ingo Molnar70b97a72006-07-03 00:25:42 -07001954 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Andi Kleen3a5c3592007-10-15 17:00:14 +02001956 for (;;) {
1957 /*
1958 * We do the initial early heuristics without holding
1959 * any task-queue locks at all. We'll only try to get
1960 * the runqueue lock when things look like they will
1961 * work out!
1962 */
1963 rq = task_rq(p);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001964
Andi Kleen3a5c3592007-10-15 17:00:14 +02001965 /*
1966 * If the task is actively running on another CPU
1967 * still, just relax and busy-wait without holding
1968 * any locks.
1969 *
1970 * NOTE! Since we don't hold any locks, it's not
1971 * even sure that "rq" stays as the right runqueue!
1972 * But we don't care, since "task_running()" will
1973 * return false if the runqueue has changed and p
1974 * is actually now running somewhere else!
1975 */
Roland McGrath85ba2d82008-07-25 19:45:58 -07001976 while (task_running(rq, p)) {
1977 if (match_state && unlikely(p->state != match_state))
1978 return 0;
Andi Kleen3a5c3592007-10-15 17:00:14 +02001979 cpu_relax();
Roland McGrath85ba2d82008-07-25 19:45:58 -07001980 }
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001981
Andi Kleen3a5c3592007-10-15 17:00:14 +02001982 /*
1983 * Ok, time to look more closely! We need the rq
1984 * lock now, to be *sure*. If we're wrong, we'll
1985 * just go back and repeat.
1986 */
Peter Zijlstraeb580752015-07-31 21:28:18 +02001987 rq = task_rq_lock(p, &rf);
Peter Zijlstra27a9da62010-05-04 20:36:56 +02001988 trace_sched_wait_task(p);
Andi Kleen3a5c3592007-10-15 17:00:14 +02001989 running = task_running(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001990 queued = task_on_rq_queued(p);
Roland McGrath85ba2d82008-07-25 19:45:58 -07001991 ncsw = 0;
Oleg Nesterovf31e11d2008-08-20 16:54:44 -07001992 if (!match_state || p->state == match_state)
Oleg Nesterov93dcf552008-08-20 16:54:44 -07001993 ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
Peter Zijlstraeb580752015-07-31 21:28:18 +02001994 task_rq_unlock(rq, p, &rf);
Linus Torvaldsfa490cf2007-06-18 09:34:40 -07001995
Andi Kleen3a5c3592007-10-15 17:00:14 +02001996 /*
Roland McGrath85ba2d82008-07-25 19:45:58 -07001997 * If it changed from the expected state, bail out now.
1998 */
1999 if (unlikely(!ncsw))
2000 break;
2001
2002 /*
Andi Kleen3a5c3592007-10-15 17:00:14 +02002003 * Was it really running after all now that we
2004 * checked with the proper locks actually held?
2005 *
2006 * Oops. Go back and try again..
2007 */
2008 if (unlikely(running)) {
2009 cpu_relax();
2010 continue;
2011 }
2012
2013 /*
2014 * It's not enough that it's not actively running,
2015 * it must be off the runqueue _entirely_, and not
2016 * preempted!
2017 *
Luis Henriques80dd99b2009-03-16 19:58:09 +00002018 * So if it was still runnable (but just not actively
Andi Kleen3a5c3592007-10-15 17:00:14 +02002019 * running right now), it's preempted, and we should
2020 * yield - it could be a while.
2021 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002022 if (unlikely(queued)) {
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01002023 ktime_t to = NSEC_PER_SEC / HZ;
Thomas Gleixner8eb90c32011-02-23 23:52:21 +00002024
2025 set_current_state(TASK_UNINTERRUPTIBLE);
2026 schedule_hrtimeout(&to, HRTIMER_MODE_REL);
Andi Kleen3a5c3592007-10-15 17:00:14 +02002027 continue;
2028 }
2029
2030 /*
2031 * Ahh, all good. It wasn't running, and it wasn't
2032 * runnable, which means that it will never become
2033 * running in the future either. We're all done!
2034 */
2035 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 }
Roland McGrath85ba2d82008-07-25 19:45:58 -07002037
2038 return ncsw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039}
2040
2041/***
2042 * kick_process - kick a running thread to enter/exit the kernel
2043 * @p: the to-be-kicked thread
2044 *
2045 * Cause a process which is running on another CPU to enter
2046 * kernel-mode, without any delay. (to get signals handled.)
2047 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002048 * NOTE: this function doesn't have to take the runqueue lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 * because all it wants to ensure is that the remote task enters
2050 * the kernel. If the IPI races and the task has been migrated
2051 * to another CPU then no harm is done and the purpose has been
2052 * achieved as well.
2053 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07002054void kick_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
2056 int cpu;
2057
2058 preempt_disable();
2059 cpu = task_cpu(p);
2060 if ((cpu != smp_processor_id()) && task_curr(p))
2061 smp_send_reschedule(cpu);
2062 preempt_enable();
2063}
Rusty Russellb43e3522009-06-12 22:27:00 -06002064EXPORT_SYMBOL_GPL(kick_process);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Oleg Nesterov30da6882010-03-15 10:10:19 +01002066/*
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02002067 * ->cpus_ptr is protected by both rq->lock and p->pi_lock
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01002068 *
2069 * A few notes on cpu_active vs cpu_online:
2070 *
2071 * - cpu_active must be a subset of cpu_online
2072 *
Ingo Molnar97fb7a02018-03-03 14:01:12 +01002073 * - on CPU-up we allow per-CPU kthreads on the online && !active CPU,
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01002074 * see __set_cpus_allowed_ptr(). At this point the newly online
Ingo Molnard1ccc662017-02-01 11:46:42 +01002075 * CPU isn't yet part of the sched domains, and balancing will not
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01002076 * see it.
2077 *
Ingo Molnard1ccc662017-02-01 11:46:42 +01002078 * - on CPU-down we clear cpu_active() to mask the sched domains and
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01002079 * avoid the load balancer to place new tasks on the to be removed
Ingo Molnard1ccc662017-02-01 11:46:42 +01002080 * CPU. Existing tasks will remain running there and will be taken
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01002081 * off.
2082 *
2083 * This means that fallback selection must not select !active CPUs.
2084 * And can assume that any active CPU must be online. Conversely
2085 * select_task_rq() below may allow selection of !active CPUs in order
2086 * to satisfy the above rules.
Oleg Nesterov30da6882010-03-15 10:10:19 +01002087 */
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002088static int select_fallback_rq(int cpu, struct task_struct *p)
2089{
Tang Chenaa00d892013-02-22 16:33:33 -08002090 int nid = cpu_to_node(cpu);
2091 const struct cpumask *nodemask = NULL;
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002092 enum { cpuset, possible, fail } state = cpuset;
2093 int dest_cpu;
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002094
Tang Chenaa00d892013-02-22 16:33:33 -08002095 /*
Ingo Molnard1ccc662017-02-01 11:46:42 +01002096 * If the node that the CPU is on has been offlined, cpu_to_node()
2097 * will return -1. There is no CPU on the node, and we should
2098 * select the CPU on the other node.
Tang Chenaa00d892013-02-22 16:33:33 -08002099 */
2100 if (nid != -1) {
2101 nodemask = cpumask_of_node(nid);
2102
2103 /* Look for allowed, online CPU in same node. */
2104 for_each_cpu(dest_cpu, nodemask) {
Tang Chenaa00d892013-02-22 16:33:33 -08002105 if (!cpu_active(dest_cpu))
2106 continue;
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02002107 if (cpumask_test_cpu(dest_cpu, p->cpus_ptr))
Tang Chenaa00d892013-02-22 16:33:33 -08002108 return dest_cpu;
2109 }
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002110 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002111
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002112 for (;;) {
2113 /* Any allowed, online CPU? */
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02002114 for_each_cpu(dest_cpu, p->cpus_ptr) {
Peter Zijlstra175f0e22017-07-25 18:58:21 +02002115 if (!is_cpu_allowed(p, dest_cpu))
Tejun Heofeb245e2016-06-16 15:35:04 -04002116 continue;
Peter Zijlstra175f0e22017-07-25 18:58:21 +02002117
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002118 goto out;
2119 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002120
Oleg Nesterove73e85f2015-10-10 20:53:15 +02002121 /* No more Mr. Nice Guy. */
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002122 switch (state) {
2123 case cpuset:
Oleg Nesterove73e85f2015-10-10 20:53:15 +02002124 if (IS_ENABLED(CONFIG_CPUSETS)) {
2125 cpuset_cpus_allowed_fallback(p);
2126 state = possible;
2127 break;
2128 }
Ingo Molnard1ccc662017-02-01 11:46:42 +01002129 /* Fall-through */
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002130 case possible:
2131 do_set_cpus_allowed(p, cpu_possible_mask);
2132 state = fail;
2133 break;
2134
2135 case fail:
2136 BUG();
2137 break;
2138 }
2139 }
2140
2141out:
2142 if (state != cpuset) {
2143 /*
2144 * Don't tell them about moving exiting tasks or
2145 * kernel threads (both mm NULL), since they never
2146 * leave kernel.
2147 */
2148 if (p->mm && printk_ratelimit()) {
John Stultzaac74dc2014-06-04 16:11:40 -07002149 printk_deferred("process %d (%s) no longer affine to cpu%d\n",
Peter Zijlstra2baab4e2012-03-20 15:57:01 +01002150 task_pid_nr(p), p->comm, cpu);
2151 }
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002152 }
2153
2154 return dest_cpu;
2155}
2156
Peter Zijlstrae2912002009-12-16 18:04:36 +01002157/*
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02002158 * The caller (fork, wakeup) owns p->pi_lock, ->cpus_ptr is stable.
Peter Zijlstrae2912002009-12-16 18:04:36 +01002159 */
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002160static inline
Peter Zijlstraac66f542013-10-07 11:29:16 +01002161int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags)
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002162{
Peter Zijlstracbce1a62015-06-11 14:46:54 +02002163 lockdep_assert_held(&p->pi_lock);
2164
Ingo Molnar4b53a342017-02-05 15:41:03 +01002165 if (p->nr_cpus_allowed > 1)
Wanpeng Li6c1d9412014-11-05 09:14:37 +08002166 cpu = p->sched_class->select_task_rq(p, cpu, sd_flags, wake_flags);
Peter Zijlstra (Intel)e9d867a2016-03-10 12:54:08 +01002167 else
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02002168 cpu = cpumask_any(p->cpus_ptr);
Peter Zijlstrae2912002009-12-16 18:04:36 +01002169
2170 /*
2171 * In order not to call set_task_cpu() on a blocking task we need
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02002172 * to rely on ttwu() to place the task on a valid ->cpus_ptr
Ingo Molnard1ccc662017-02-01 11:46:42 +01002173 * CPU.
Peter Zijlstrae2912002009-12-16 18:04:36 +01002174 *
2175 * Since this is common to all placement strategies, this lives here.
2176 *
2177 * [ this allows ->select_task() to simply return task_cpu(p) and
2178 * not worry about this generic constraint ]
2179 */
Paul Burton7af443e2018-05-26 08:46:47 -07002180 if (unlikely(!is_cpu_allowed(p, cpu)))
Peter Zijlstra5da9a0f2009-12-16 18:04:38 +01002181 cpu = select_fallback_rq(task_cpu(p), p);
Peter Zijlstrae2912002009-12-16 18:04:36 +01002182
2183 return cpu;
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002184}
Mike Galbraith09a40af2010-04-15 07:29:59 +02002185
Nicolas Pitref5832c12017-05-29 17:02:57 -04002186void sched_set_stop_task(int cpu, struct task_struct *stop)
2187{
2188 struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
2189 struct task_struct *old_stop = cpu_rq(cpu)->stop;
2190
2191 if (stop) {
2192 /*
2193 * Make it appear like a SCHED_FIFO task, its something
2194 * userspace knows about and won't get confused about.
2195 *
2196 * Also, it will make PI more or less work without too
2197 * much confusion -- but then, stop work should not
2198 * rely on PI working anyway.
2199 */
2200 sched_setscheduler_nocheck(stop, SCHED_FIFO, &param);
2201
2202 stop->sched_class = &stop_sched_class;
2203 }
2204
2205 cpu_rq(cpu)->stop = stop;
2206
2207 if (old_stop) {
2208 /*
2209 * Reset it back to a normal scheduling class so that
2210 * it can die in pieces.
2211 */
2212 old_stop->sched_class = &rt_sched_class;
2213 }
2214}
2215
Peter Zijlstra25834c72015-05-15 17:43:34 +02002216#else
2217
2218static inline int __set_cpus_allowed_ptr(struct task_struct *p,
2219 const struct cpumask *new_mask, bool check)
2220{
2221 return set_cpus_allowed_ptr(p, new_mask);
2222}
2223
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02002224#endif /* CONFIG_SMP */
Peter Zijlstra970b13b2009-11-25 13:31:39 +01002225
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002226static void
Peter Zijlstrab84cb5d2011-04-05 17:23:55 +02002227ttwu_stat(struct task_struct *p, int cpu, int wake_flags)
Tejun Heo9ed38112009-12-03 15:08:03 +09002228{
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05002229 struct rq *rq;
2230
2231 if (!schedstat_enabled())
2232 return;
2233
2234 rq = this_rq();
Tejun Heo9ed38112009-12-03 15:08:03 +09002235
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002236#ifdef CONFIG_SMP
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05002237 if (cpu == rq->cpu) {
Peter Zijlstrab85c8b72018-01-16 20:51:06 +01002238 __schedstat_inc(rq->ttwu_local);
2239 __schedstat_inc(p->se.statistics.nr_wakeups_local);
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002240 } else {
2241 struct sched_domain *sd;
2242
Peter Zijlstrab85c8b72018-01-16 20:51:06 +01002243 __schedstat_inc(p->se.statistics.nr_wakeups_remote);
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02002244 rcu_read_lock();
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05002245 for_each_domain(rq->cpu, sd) {
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002246 if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
Peter Zijlstrab85c8b72018-01-16 20:51:06 +01002247 __schedstat_inc(sd->ttwu_wake_remote);
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002248 break;
2249 }
2250 }
Peter Zijlstra057f3fa2011-04-18 11:24:34 +02002251 rcu_read_unlock();
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002252 }
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02002253
2254 if (wake_flags & WF_MIGRATED)
Peter Zijlstrab85c8b72018-01-16 20:51:06 +01002255 __schedstat_inc(p->se.statistics.nr_wakeups_migrate);
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002256#endif /* CONFIG_SMP */
2257
Peter Zijlstrab85c8b72018-01-16 20:51:06 +01002258 __schedstat_inc(rq->ttwu_count);
2259 __schedstat_inc(p->se.statistics.nr_wakeups);
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002260
2261 if (wake_flags & WF_SYNC)
Peter Zijlstrab85c8b72018-01-16 20:51:06 +01002262 __schedstat_inc(p->se.statistics.nr_wakeups_sync);
Tejun Heo9ed38112009-12-03 15:08:03 +09002263}
2264
Peter Zijlstra23f41ee2011-04-05 17:23:56 +02002265/*
2266 * Mark the task runnable and perform wakeup-preemption.
2267 */
Peter Zijlstrae7904a22015-08-01 19:25:08 +02002268static void ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags,
Matt Flemingd8ac8972016-09-21 14:38:10 +01002269 struct rq_flags *rf)
Tejun Heo9ed38112009-12-03 15:08:03 +09002270{
Tejun Heo9ed38112009-12-03 15:08:03 +09002271 check_preempt_curr(rq, p, wake_flags);
Tejun Heo9ed38112009-12-03 15:08:03 +09002272 p->state = TASK_RUNNING;
Peter Zijlstrafbd705a2015-06-09 11:13:36 +02002273 trace_sched_wakeup(p);
2274
Tejun Heo9ed38112009-12-03 15:08:03 +09002275#ifdef CONFIG_SMP
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02002276 if (p->sched_class->task_woken) {
2277 /*
Peter Zijlstracbce1a62015-06-11 14:46:54 +02002278 * Our task @p is fully woken up and running; so its safe to
2279 * drop the rq->lock, hereafter rq is only used for statistics.
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02002280 */
Matt Flemingd8ac8972016-09-21 14:38:10 +01002281 rq_unpin_lock(rq, rf);
Tejun Heo9ed38112009-12-03 15:08:03 +09002282 p->sched_class->task_woken(rq, p);
Matt Flemingd8ac8972016-09-21 14:38:10 +01002283 rq_repin_lock(rq, rf);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02002284 }
Tejun Heo9ed38112009-12-03 15:08:03 +09002285
Steven Rostedte69c6342010-12-06 17:10:31 -05002286 if (rq->idle_stamp) {
Frederic Weisbecker78becc22013-04-12 01:51:02 +02002287 u64 delta = rq_clock(rq) - rq->idle_stamp;
Jason Low9bd721c2013-09-13 11:26:52 -07002288 u64 max = 2*rq->max_idle_balance_cost;
Tejun Heo9ed38112009-12-03 15:08:03 +09002289
Jason Lowabfafa52013-09-13 11:26:51 -07002290 update_avg(&rq->avg_idle, delta);
2291
2292 if (rq->avg_idle > max)
Tejun Heo9ed38112009-12-03 15:08:03 +09002293 rq->avg_idle = max;
Jason Lowabfafa52013-09-13 11:26:51 -07002294
Tejun Heo9ed38112009-12-03 15:08:03 +09002295 rq->idle_stamp = 0;
2296 }
2297#endif
2298}
2299
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002300static void
Peter Zijlstrae7904a22015-08-01 19:25:08 +02002301ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags,
Matt Flemingd8ac8972016-09-21 14:38:10 +01002302 struct rq_flags *rf)
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002303{
Peter Zijlstra77558e42017-02-21 14:36:23 +01002304 int en_flags = ENQUEUE_WAKEUP | ENQUEUE_NOCLOCK;
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02002305
Peter Zijlstracbce1a62015-06-11 14:46:54 +02002306 lockdep_assert_held(&rq->lock);
2307
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002308 if (p->sched_contributes_to_load)
2309 rq->nr_uninterruptible--;
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02002310
Peter Zijlstradbfb0892020-07-03 12:40:33 +02002311#ifdef CONFIG_SMP
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02002312 if (wake_flags & WF_MIGRATED)
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02002313 en_flags |= ENQUEUE_MIGRATED;
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002314#endif
2315
Peter Zijlstra1b174a22019-04-09 09:53:13 +02002316 activate_task(rq, p, en_flags);
Matt Flemingd8ac8972016-09-21 14:38:10 +01002317 ttwu_do_wakeup(rq, p, wake_flags, rf);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002318}
2319
2320/*
2321 * Called in case the task @p isn't fully descheduled from its runqueue,
2322 * in this case we must do a remote wakeup. Its a 'light' wakeup though,
2323 * since all we need to do is flip p->state to TASK_RUNNING, since
2324 * the task is still ->on_rq.
2325 */
2326static int ttwu_remote(struct task_struct *p, int wake_flags)
2327{
Peter Zijlstraeb580752015-07-31 21:28:18 +02002328 struct rq_flags rf;
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002329 struct rq *rq;
2330 int ret = 0;
2331
Peter Zijlstraeb580752015-07-31 21:28:18 +02002332 rq = __task_rq_lock(p, &rf);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04002333 if (task_on_rq_queued(p)) {
Frederic Weisbecker1ad4ec02013-04-12 01:51:00 +02002334 /* check_preempt_curr() may use rq clock */
2335 update_rq_clock(rq);
Matt Flemingd8ac8972016-09-21 14:38:10 +01002336 ttwu_do_wakeup(rq, p, wake_flags, &rf);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002337 ret = 1;
2338 }
Peter Zijlstraeb580752015-07-31 21:28:18 +02002339 __task_rq_unlock(rq, &rf);
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002340
2341 return ret;
2342}
2343
Peter Zijlstra317f3942011-04-05 17:23:58 +02002344#ifdef CONFIG_SMP
Peter Zijlstraa14886642020-05-26 18:11:04 +02002345void sched_ttwu_pending(void *arg)
Peter Zijlstra317f3942011-04-05 17:23:58 +02002346{
Peter Zijlstraa14886642020-05-26 18:11:04 +02002347 struct llist_node *llist = arg;
Peter Zijlstra317f3942011-04-05 17:23:58 +02002348 struct rq *rq = this_rq();
Byungchul Park73215842017-05-12 09:39:44 +09002349 struct task_struct *p, *t;
Matt Flemingd8ac8972016-09-21 14:38:10 +01002350 struct rq_flags rf;
Peter Zijlstra317f3942011-04-05 17:23:58 +02002351
Peter Zijlstrae3baac42014-06-04 10:31:18 -07002352 if (!llist)
2353 return;
2354
Peter Zijlstra126c2092020-05-26 18:11:03 +02002355 /*
2356 * rq::ttwu_pending racy indication of out-standing wakeups.
2357 * Races such that false-negatives are possible, since they
2358 * are shorter lived that false-positives would be.
2359 */
2360 WRITE_ONCE(rq->ttwu_pending, 0);
2361
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02002362 rq_lock_irqsave(rq, &rf);
Peter Zijlstra77558e42017-02-21 14:36:23 +01002363 update_rq_clock(rq);
Peter Zijlstra317f3942011-04-05 17:23:58 +02002364
Peter Zijlstra8c4890d2020-06-22 12:01:25 +02002365 llist_for_each_entry_safe(p, t, llist, wake_entry.llist) {
Peter Zijlstrab6e13e82020-06-22 12:01:23 +02002366 if (WARN_ON_ONCE(p->on_cpu))
2367 smp_cond_load_acquire(&p->on_cpu, !VAL);
2368
2369 if (WARN_ON_ONCE(task_cpu(p) != cpu_of(rq)))
2370 set_task_cpu(p, cpu_of(rq));
2371
Byungchul Park73215842017-05-12 09:39:44 +09002372 ttwu_do_activate(rq, p, p->sched_remote_wakeup ? WF_MIGRATED : 0, &rf);
Peter Zijlstrab6e13e82020-06-22 12:01:23 +02002373 }
Peter Zijlstra317f3942011-04-05 17:23:58 +02002374
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02002375 rq_unlock_irqrestore(rq, &rf);
Peter Zijlstra317f3942011-04-05 17:23:58 +02002376}
2377
Peter Zijlstrab2a02fc2020-05-26 18:11:01 +02002378void send_call_function_single_ipi(int cpu)
Peter Zijlstra317f3942011-04-05 17:23:58 +02002379{
Peter Zijlstrab2a02fc2020-05-26 18:11:01 +02002380 struct rq *rq = cpu_rq(cpu);
Peter Zijlstraf27dde82013-08-14 14:55:31 +02002381
Peter Zijlstrab2a02fc2020-05-26 18:11:01 +02002382 if (!set_nr_if_polling(rq->idle))
2383 arch_send_call_function_single_ipi(cpu);
2384 else
2385 trace_sched_wake_idle_without_ipi(cpu);
Peter Zijlstra317f3942011-04-05 17:23:58 +02002386}
2387
Mel Gorman2ebb1772020-05-24 21:29:56 +01002388/*
2389 * Queue a task on the target CPUs wake_list and wake the CPU via IPI if
2390 * necessary. The wakee CPU on receipt of the IPI will queue the task
2391 * via sched_ttwu_wakeup() for activation so the wakee incurs the cost
2392 * of the wakeup instead of the waker.
2393 */
2394static void __ttwu_queue_wakelist(struct task_struct *p, int cpu, int wake_flags)
Peter Zijlstra317f3942011-04-05 17:23:58 +02002395{
Peter Zijlstrae3baac42014-06-04 10:31:18 -07002396 struct rq *rq = cpu_rq(cpu);
2397
Peter Zijlstrab7e7ade2016-05-23 11:19:07 +02002398 p->sched_remote_wakeup = !!(wake_flags & WF_MIGRATED);
2399
Peter Zijlstra126c2092020-05-26 18:11:03 +02002400 WRITE_ONCE(rq->ttwu_pending, 1);
Peter Zijlstra8c4890d2020-06-22 12:01:25 +02002401 __smp_call_single_queue(cpu, &p->wake_entry.llist);
Peter Zijlstra317f3942011-04-05 17:23:58 +02002402}
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02002403
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08002404void wake_up_if_idle(int cpu)
2405{
2406 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02002407 struct rq_flags rf;
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08002408
Andy Lutomirskifd7de1e82014-11-29 08:13:51 -08002409 rcu_read_lock();
2410
2411 if (!is_idle_task(rcu_dereference(rq->curr)))
2412 goto out;
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08002413
2414 if (set_nr_if_polling(rq->idle)) {
2415 trace_sched_wake_idle_without_ipi(cpu);
2416 } else {
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02002417 rq_lock_irqsave(rq, &rf);
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08002418 if (is_idle_task(rq->curr))
2419 smp_send_reschedule(cpu);
Ingo Molnard1ccc662017-02-01 11:46:42 +01002420 /* Else CPU is not idle, do nothing here: */
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02002421 rq_unlock_irqrestore(rq, &rf);
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08002422 }
Andy Lutomirskifd7de1e82014-11-29 08:13:51 -08002423
2424out:
2425 rcu_read_unlock();
Chuansheng Liuf6be8af2014-09-04 15:17:53 +08002426}
2427
Peter Zijlstra39be3502012-01-26 12:44:34 +01002428bool cpus_share_cache(int this_cpu, int that_cpu)
Peter Zijlstra518cd622011-12-07 15:07:31 +01002429{
2430 return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
2431}
Peter Zijlstrac6e7bd72020-05-24 21:29:55 +01002432
Mel Gorman2ebb1772020-05-24 21:29:56 +01002433static inline bool ttwu_queue_cond(int cpu, int wake_flags)
Peter Zijlstrac6e7bd72020-05-24 21:29:55 +01002434{
Mel Gorman2ebb1772020-05-24 21:29:56 +01002435 /*
2436 * If the CPU does not share cache, then queue the task on the
2437 * remote rqs wakelist to avoid accessing remote data.
2438 */
2439 if (!cpus_share_cache(smp_processor_id(), cpu))
2440 return true;
2441
2442 /*
2443 * If the task is descheduling and the only running task on the
2444 * CPU then use the wakelist to offload the task activation to
2445 * the soon-to-be-idle CPU as the current CPU is likely busy.
2446 * nr_running is checked to avoid unnecessary task stacking.
2447 */
Peter Zijlstra739f70b2020-06-22 12:01:24 +02002448 if ((wake_flags & WF_ON_CPU) && cpu_rq(cpu)->nr_running <= 1)
Mel Gorman2ebb1772020-05-24 21:29:56 +01002449 return true;
2450
2451 return false;
2452}
2453
2454static bool ttwu_queue_wakelist(struct task_struct *p, int cpu, int wake_flags)
2455{
2456 if (sched_feat(TTWU_QUEUE) && ttwu_queue_cond(cpu, wake_flags)) {
Peter Zijlstrab6e13e82020-06-22 12:01:23 +02002457 if (WARN_ON_ONCE(cpu == smp_processor_id()))
2458 return false;
2459
Peter Zijlstrac6e7bd72020-05-24 21:29:55 +01002460 sched_clock_cpu(cpu); /* Sync clocks across CPUs */
Mel Gorman2ebb1772020-05-24 21:29:56 +01002461 __ttwu_queue_wakelist(p, cpu, wake_flags);
Peter Zijlstrac6e7bd72020-05-24 21:29:55 +01002462 return true;
2463 }
2464
2465 return false;
2466}
Peter Zijlstrad6aa8f82011-05-26 14:21:33 +02002467#endif /* CONFIG_SMP */
Peter Zijlstra317f3942011-04-05 17:23:58 +02002468
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02002469static void ttwu_queue(struct task_struct *p, int cpu, int wake_flags)
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002470{
2471 struct rq *rq = cpu_rq(cpu);
Matt Flemingd8ac8972016-09-21 14:38:10 +01002472 struct rq_flags rf;
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002473
Daniel Hellstrom17d9f312011-05-20 04:01:10 +00002474#if defined(CONFIG_SMP)
Mel Gorman2ebb1772020-05-24 21:29:56 +01002475 if (ttwu_queue_wakelist(p, cpu, wake_flags))
Peter Zijlstra317f3942011-04-05 17:23:58 +02002476 return;
Peter Zijlstra317f3942011-04-05 17:23:58 +02002477#endif
2478
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02002479 rq_lock(rq, &rf);
Peter Zijlstra77558e42017-02-21 14:36:23 +01002480 update_rq_clock(rq);
Matt Flemingd8ac8972016-09-21 14:38:10 +01002481 ttwu_do_activate(rq, p, wake_flags, &rf);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02002482 rq_unlock(rq, &rf);
Tejun Heo9ed38112009-12-03 15:08:03 +09002483}
2484
Peter Zijlstra8643cda2015-11-17 19:01:11 +01002485/*
2486 * Notes on Program-Order guarantees on SMP systems.
2487 *
2488 * MIGRATION
2489 *
2490 * The basic program-order guarantee on SMP systems is that when a task [t]
Ingo Molnard1ccc662017-02-01 11:46:42 +01002491 * migrates, all its activity on its old CPU [c0] happens-before any subsequent
2492 * execution on its new CPU [c1].
Peter Zijlstra8643cda2015-11-17 19:01:11 +01002493 *
2494 * For migration (of runnable tasks) this is provided by the following means:
2495 *
2496 * A) UNLOCK of the rq(c0)->lock scheduling out task t
2497 * B) migration for t is required to synchronize *both* rq(c0)->lock and
2498 * rq(c1)->lock (if not at the same time, then in that order).
2499 * C) LOCK of the rq(c1)->lock scheduling in task
2500 *
Andrea Parri7696f992018-07-16 11:06:03 -07002501 * Release/acquire chaining guarantees that B happens after A and C after B.
Ingo Molnard1ccc662017-02-01 11:46:42 +01002502 * Note: the CPU doing B need not be c0 or c1
Peter Zijlstra8643cda2015-11-17 19:01:11 +01002503 *
2504 * Example:
2505 *
2506 * CPU0 CPU1 CPU2
2507 *
2508 * LOCK rq(0)->lock
2509 * sched-out X
2510 * sched-in Y
2511 * UNLOCK rq(0)->lock
2512 *
2513 * LOCK rq(0)->lock // orders against CPU0
2514 * dequeue X
2515 * UNLOCK rq(0)->lock
2516 *
2517 * LOCK rq(1)->lock
2518 * enqueue X
2519 * UNLOCK rq(1)->lock
2520 *
2521 * LOCK rq(1)->lock // orders against CPU2
2522 * sched-out Z
2523 * sched-in X
2524 * UNLOCK rq(1)->lock
2525 *
2526 *
2527 * BLOCKING -- aka. SLEEP + WAKEUP
2528 *
2529 * For blocking we (obviously) need to provide the same guarantee as for
2530 * migration. However the means are completely different as there is no lock
2531 * chain to provide order. Instead we do:
2532 *
2533 * 1) smp_store_release(X->on_cpu, 0)
Peter Zijlstra1f03e8d2016-04-04 10:57:12 +02002534 * 2) smp_cond_load_acquire(!X->on_cpu)
Peter Zijlstra8643cda2015-11-17 19:01:11 +01002535 *
2536 * Example:
2537 *
2538 * CPU0 (schedule) CPU1 (try_to_wake_up) CPU2 (schedule)
2539 *
2540 * LOCK rq(0)->lock LOCK X->pi_lock
2541 * dequeue X
2542 * sched-out X
2543 * smp_store_release(X->on_cpu, 0);
2544 *
Peter Zijlstra1f03e8d2016-04-04 10:57:12 +02002545 * smp_cond_load_acquire(&X->on_cpu, !VAL);
Peter Zijlstra8643cda2015-11-17 19:01:11 +01002546 * X->state = WAKING
2547 * set_task_cpu(X,2)
2548 *
2549 * LOCK rq(2)->lock
2550 * enqueue X
2551 * X->state = RUNNING
2552 * UNLOCK rq(2)->lock
2553 *
2554 * LOCK rq(2)->lock // orders against CPU1
2555 * sched-out Z
2556 * sched-in X
2557 * UNLOCK rq(2)->lock
2558 *
2559 * UNLOCK X->pi_lock
2560 * UNLOCK rq(0)->lock
2561 *
2562 *
Andrea Parri7696f992018-07-16 11:06:03 -07002563 * However, for wakeups there is a second guarantee we must provide, namely we
2564 * must ensure that CONDITION=1 done by the caller can not be reordered with
2565 * accesses to the task state; see try_to_wake_up() and set_current_state().
Peter Zijlstra8643cda2015-11-17 19:01:11 +01002566 */
2567
Tejun Heo9ed38112009-12-03 15:08:03 +09002568/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 * try_to_wake_up - wake up a thread
Tejun Heo9ed38112009-12-03 15:08:03 +09002570 * @p: the thread to be awakened
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 * @state: the mask of task states that can be woken
Tejun Heo9ed38112009-12-03 15:08:03 +09002572 * @wake_flags: wake modifier flags (WF_*)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 *
Peter Zijlstraa2250232016-10-19 15:45:27 +02002574 * If (@state & @p->state) @p->state = TASK_RUNNING.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 *
Peter Zijlstraa2250232016-10-19 15:45:27 +02002576 * If the task was not queued/runnable, also place it back on a runqueue.
2577 *
2578 * Atomic against schedule() which would dequeue a task, also see
2579 * set_current_state().
2580 *
Andrea Parri7696f992018-07-16 11:06:03 -07002581 * This function executes a full memory barrier before accessing the task
2582 * state; see set_current_state().
2583 *
Peter Zijlstraa2250232016-10-19 15:45:27 +02002584 * Return: %true if @p->state changes (an actual wakeup was done),
2585 * %false otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 */
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02002587static int
2588try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 unsigned long flags;
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002591 int cpu, success = 0;
Peter Zijlstra2398f2c2008-06-27 13:41:35 +02002592
Peter Zijlstrae3d85482019-07-10 12:57:36 +02002593 preempt_disable();
Peter Zijlstraaacedf22019-06-07 15:39:49 +02002594 if (p == current) {
2595 /*
2596 * We're waking current, this means 'p->on_rq' and 'task_cpu(p)
2597 * == smp_processor_id()'. Together this means we can special
2598 * case the whole 'p->on_rq && ttwu_remote()' case below
2599 * without taking any locks.
2600 *
2601 * In particular:
2602 * - we rely on Program-Order guarantees for all the ordering,
2603 * - we're serialized against set_special_state() by virtue of
2604 * it disabling IRQs (this allows not taking ->pi_lock).
2605 */
2606 if (!(p->state & state))
Peter Zijlstrae3d85482019-07-10 12:57:36 +02002607 goto out;
Peter Zijlstraaacedf22019-06-07 15:39:49 +02002608
2609 success = 1;
Peter Zijlstraaacedf22019-06-07 15:39:49 +02002610 trace_sched_waking(p);
2611 p->state = TASK_RUNNING;
2612 trace_sched_wakeup(p);
2613 goto out;
2614 }
2615
Oleg Nesterove0acd0a2013-08-12 18:14:00 +02002616 /*
2617 * If we are going to wake up a thread waiting for CONDITION we
2618 * need to ensure that CONDITION=1 done by the caller can not be
2619 * reordered with p->state check below. This pairs with mb() in
2620 * set_current_state() the waiting thread does.
2621 */
Peter Zijlstra013fdb82011-04-05 17:23:45 +02002622 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstrad89e588c2016-09-05 11:37:53 +02002623 smp_mb__after_spinlock();
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002624 if (!(p->state & state))
Peter Zijlstraaacedf22019-06-07 15:39:49 +02002625 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
Peter Zijlstrafbd705a2015-06-09 11:13:36 +02002627 trace_sched_waking(p);
2628
Ingo Molnard1ccc662017-02-01 11:46:42 +01002629 /* We're going to change ->state: */
2630 success = 1;
Peter Zijlstrad7c01d22011-04-05 17:23:43 +02002631
Balbir Singh135e8c92016-09-05 13:16:40 +10002632 /*
2633 * Ensure we load p->on_rq _after_ p->state, otherwise it would
2634 * be possible to, falsely, observe p->on_rq == 0 and get stuck
2635 * in smp_cond_load_acquire() below.
2636 *
Andrea Parri3d85b272018-07-16 11:06:02 -07002637 * sched_ttwu_pending() try_to_wake_up()
2638 * STORE p->on_rq = 1 LOAD p->state
2639 * UNLOCK rq->lock
2640 *
2641 * __schedule() (switch to task 'p')
2642 * LOCK rq->lock smp_rmb();
2643 * smp_mb__after_spinlock();
2644 * UNLOCK rq->lock
Balbir Singh135e8c92016-09-05 13:16:40 +10002645 *
2646 * [task p]
Andrea Parri3d85b272018-07-16 11:06:02 -07002647 * STORE p->state = UNINTERRUPTIBLE LOAD p->on_rq
Balbir Singh135e8c92016-09-05 13:16:40 +10002648 *
Andrea Parri3d85b272018-07-16 11:06:02 -07002649 * Pairs with the LOCK+smp_mb__after_spinlock() on rq->lock in
2650 * __schedule(). See the comment for smp_mb__after_spinlock().
Paul E. McKenney2beaf322020-03-11 14:23:21 -07002651 *
2652 * A similar smb_rmb() lives in try_invoke_on_locked_down_task().
Balbir Singh135e8c92016-09-05 13:16:40 +10002653 */
2654 smp_rmb();
Peter Zijlstradbfb0892020-07-03 12:40:33 +02002655 if (READ_ONCE(p->on_rq) && ttwu_remote(p, wake_flags))
Peter Zijlstraaacedf22019-06-07 15:39:49 +02002656 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
Peter Zijlstrac6e7bd72020-05-24 21:29:55 +01002658 if (p->in_iowait) {
2659 delayacct_blkio_end(p);
2660 atomic_dec(&task_rq(p)->nr_iowait);
2661 }
2662
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663#ifdef CONFIG_SMP
Peter Zijlstrae9c84312009-09-15 14:43:03 +02002664 /*
Peter Zijlstraecf7d012015-10-07 14:14:13 +02002665 * Ensure we load p->on_cpu _after_ p->on_rq, otherwise it would be
2666 * possible to, falsely, observe p->on_cpu == 0.
2667 *
2668 * One must be running (->on_cpu == 1) in order to remove oneself
2669 * from the runqueue.
2670 *
Andrea Parri3d85b272018-07-16 11:06:02 -07002671 * __schedule() (switch to task 'p') try_to_wake_up()
2672 * STORE p->on_cpu = 1 LOAD p->on_rq
2673 * UNLOCK rq->lock
Peter Zijlstraecf7d012015-10-07 14:14:13 +02002674 *
Andrea Parri3d85b272018-07-16 11:06:02 -07002675 * __schedule() (put 'p' to sleep)
2676 * LOCK rq->lock smp_rmb();
2677 * smp_mb__after_spinlock();
2678 * STORE p->on_rq = 0 LOAD p->on_cpu
2679 *
2680 * Pairs with the LOCK+smp_mb__after_spinlock() on rq->lock in
2681 * __schedule(). See the comment for smp_mb__after_spinlock().
Peter Zijlstradbfb0892020-07-03 12:40:33 +02002682 *
2683 * Form a control-dep-acquire with p->on_rq == 0 above, to ensure
2684 * schedule()'s deactivate_task() has 'happened' and p will no longer
2685 * care about it's own p->state. See the comment in __schedule().
Peter Zijlstraecf7d012015-10-07 14:14:13 +02002686 */
Peter Zijlstradbfb0892020-07-03 12:40:33 +02002687 smp_acquire__after_ctrl_dep();
2688
2689 /*
2690 * We're doing the wakeup (@success == 1), they did a dequeue (p->on_rq
2691 * == 0), which means we need to do an enqueue, change p->state to
2692 * TASK_WAKING such that we can unlock p->pi_lock before doing the
2693 * enqueue, such as ttwu_queue_wakelist().
2694 */
2695 p->state = TASK_WAKING;
Peter Zijlstraecf7d012015-10-07 14:14:13 +02002696
2697 /*
Ingo Molnard1ccc662017-02-01 11:46:42 +01002698 * If the owning (remote) CPU is still in the middle of schedule() with
Peter Zijlstrac6e7bd72020-05-24 21:29:55 +01002699 * this task as prev, considering queueing p on the remote CPUs wake_list
2700 * which potentially sends an IPI instead of spinning on p->on_cpu to
2701 * let the waker make forward progress. This is safe because IRQs are
2702 * disabled and the IPI will deliver after on_cpu is cleared.
Peter Zijlstrab6e13e82020-06-22 12:01:23 +02002703 *
2704 * Ensure we load task_cpu(p) after p->on_cpu:
2705 *
2706 * set_task_cpu(p, cpu);
2707 * STORE p->cpu = @cpu
2708 * __schedule() (switch to task 'p')
2709 * LOCK rq->lock
2710 * smp_mb__after_spin_lock() smp_cond_load_acquire(&p->on_cpu)
2711 * STORE p->on_cpu = 1 LOAD p->cpu
2712 *
2713 * to ensure we observe the correct CPU on which the task is currently
2714 * scheduling.
Peter Zijlstrac6e7bd72020-05-24 21:29:55 +01002715 */
Peter Zijlstrab6e13e82020-06-22 12:01:23 +02002716 if (smp_load_acquire(&p->on_cpu) &&
Peter Zijlstra739f70b2020-06-22 12:01:24 +02002717 ttwu_queue_wakelist(p, task_cpu(p), wake_flags | WF_ON_CPU))
Peter Zijlstrac6e7bd72020-05-24 21:29:55 +01002718 goto unlock;
2719
2720 /*
2721 * If the owning (remote) CPU is still in the middle of schedule() with
Peter Zijlstrac05fbaf2011-04-05 17:23:57 +02002722 * this task as prev, wait until its done referencing the task.
Peter Zijlstrab75a2252015-10-06 14:36:17 +02002723 *
rodrigosiqueira31cb1bc2017-12-15 12:06:03 -02002724 * Pairs with the smp_store_release() in finish_task().
Peter Zijlstrab75a2252015-10-06 14:36:17 +02002725 *
2726 * This ensures that tasks getting woken will be fully ordered against
2727 * their previous state and preserve Program Order.
Peter Zijlstrae4a52bc2011-04-05 17:23:54 +02002728 */
Peter Zijlstra1f03e8d2016-04-04 10:57:12 +02002729 smp_cond_load_acquire(&p->on_cpu, !VAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Peter Zijlstraac66f542013-10-07 11:29:16 +01002731 cpu = select_task_rq(p, p->wake_cpu, SD_BALANCE_WAKE, wake_flags);
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02002732 if (task_cpu(p) != cpu) {
2733 wake_flags |= WF_MIGRATED;
Johannes Weinereb414682018-10-26 15:06:27 -07002734 psi_ttwu_dequeue(p);
Mike Galbraith055a0082009-11-12 11:07:44 +01002735 set_task_cpu(p, cpu);
Peter Zijlstraf339b9d2011-05-31 10:49:20 +02002736 }
Peter Zijlstrab6e13e82020-06-22 12:01:23 +02002737#else
2738 cpu = task_cpu(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Peter Zijlstrab5179ac2016-05-11 16:10:34 +02002741 ttwu_queue(p, cpu, wake_flags);
Peter Zijlstraaacedf22019-06-07 15:39:49 +02002742unlock:
Peter Zijlstra013fdb82011-04-05 17:23:45 +02002743 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Peter Zijlstraaacedf22019-06-07 15:39:49 +02002744out:
2745 if (success)
Peter Zijlstrab6e13e82020-06-22 12:01:23 +02002746 ttwu_stat(p, task_cpu(p), wake_flags);
Peter Zijlstrae3d85482019-07-10 12:57:36 +02002747 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
2749 return success;
2750}
2751
David Howells50fa6102009-04-28 15:01:38 +01002752/**
Paul E. McKenney2beaf322020-03-11 14:23:21 -07002753 * try_invoke_on_locked_down_task - Invoke a function on task in fixed state
2754 * @p: Process for which the function is to be invoked.
2755 * @func: Function to invoke.
2756 * @arg: Argument to function.
2757 *
2758 * If the specified task can be quickly locked into a definite state
2759 * (either sleeping or on a given runqueue), arrange to keep it in that
2760 * state while invoking @func(@arg). This function can use ->on_rq and
2761 * task_curr() to work out what the state is, if required. Given that
2762 * @func can be invoked with a runqueue lock held, it had better be quite
2763 * lightweight.
2764 *
2765 * Returns:
2766 * @false if the task slipped out from under the locks.
2767 * @true if the task was locked onto a runqueue or is sleeping.
2768 * However, @func can override this by returning @false.
2769 */
2770bool try_invoke_on_locked_down_task(struct task_struct *p, bool (*func)(struct task_struct *t, void *arg), void *arg)
2771{
2772 bool ret = false;
2773 struct rq_flags rf;
2774 struct rq *rq;
2775
2776 lockdep_assert_irqs_enabled();
2777 raw_spin_lock_irq(&p->pi_lock);
2778 if (p->on_rq) {
2779 rq = __task_rq_lock(p, &rf);
2780 if (task_rq(p) == rq)
2781 ret = func(p, arg);
2782 rq_unlock(rq, &rf);
2783 } else {
2784 switch (p->state) {
2785 case TASK_RUNNING:
2786 case TASK_WAKING:
2787 break;
2788 default:
2789 smp_rmb(); // See smp_rmb() comment in try_to_wake_up().
2790 if (!p->on_rq)
2791 ret = func(p, arg);
2792 }
2793 }
2794 raw_spin_unlock_irq(&p->pi_lock);
2795 return ret;
2796}
2797
2798/**
David Howells50fa6102009-04-28 15:01:38 +01002799 * wake_up_process - Wake up a specific process
2800 * @p: The process to be woken up.
2801 *
2802 * Attempt to wake up the nominated process and move it to the set of runnable
Yacine Belkadie69f6182013-07-12 20:45:47 +02002803 * processes.
2804 *
2805 * Return: 1 if the process was woken up, 0 if it was already running.
David Howells50fa6102009-04-28 15:01:38 +01002806 *
Andrea Parri7696f992018-07-16 11:06:03 -07002807 * This function executes a full memory barrier before accessing the task state.
David Howells50fa6102009-04-28 15:01:38 +01002808 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002809int wake_up_process(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810{
Oleg Nesterov9067ac82013-01-21 20:48:17 +01002811 return try_to_wake_up(p, TASK_NORMAL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813EXPORT_SYMBOL(wake_up_process);
2814
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -08002815int wake_up_state(struct task_struct *p, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816{
2817 return try_to_wake_up(p, state, 0);
2818}
2819
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820/*
2821 * Perform scheduler related setup for a newly forked process p.
2822 * p is forked by current.
Ingo Molnardd41f592007-07-09 18:51:59 +02002823 *
2824 * __sched_fork() is basic setup used by init_idle() too:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 */
Rik van Riel5e1576e2013-10-07 11:29:26 +01002826static void __sched_fork(unsigned long clone_flags, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827{
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002828 p->on_rq = 0;
2829
2830 p->se.on_rq = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02002831 p->se.exec_start = 0;
2832 p->se.sum_exec_runtime = 0;
Ingo Molnarf6cf8912007-08-28 12:53:24 +02002833 p->se.prev_sum_exec_runtime = 0;
Ingo Molnar6c594c22008-12-14 12:34:15 +01002834 p->se.nr_migrations = 0;
Peter Zijlstrada7a7352011-01-17 17:03:27 +01002835 p->se.vruntime = 0;
Peter Zijlstrafd2f4412011-04-05 17:23:44 +02002836 INIT_LIST_HEAD(&p->se.group_node);
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002837
Byungchul Parkad936d82015-10-24 01:16:19 +09002838#ifdef CONFIG_FAIR_GROUP_SCHED
2839 p->se.cfs_rq = NULL;
2840#endif
2841
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002842#ifdef CONFIG_SCHEDSTATS
Mel Gormancb251762016-02-05 09:08:36 +00002843 /* Even if schedstat is disabled, there should not be garbage */
Lucas De Marchi41acab82010-03-10 23:37:45 -03002844 memset(&p->se.statistics, 0, sizeof(p->se.statistics));
Ingo Molnar6cfb0d52007-08-02 17:41:40 +02002845#endif
Nick Piggin476d1392005-06-25 14:57:29 -07002846
Dario Faggioliaab03e02013-11-28 11:14:43 +01002847 RB_CLEAR_NODE(&p->dl.rb_node);
Peter Zijlstra40767b02015-01-28 15:08:03 +01002848 init_dl_task_timer(&p->dl);
Luca Abeni209a0cb2017-05-18 22:13:29 +02002849 init_dl_inactive_task_timer(&p->dl);
Juri Lellia5e7be32014-09-19 10:22:39 +01002850 __dl_clear_params(p);
Dario Faggioliaab03e02013-11-28 11:14:43 +01002851
Peter Zijlstrafa717062008-01-25 21:08:27 +01002852 INIT_LIST_HEAD(&p->rt.run_list);
Peter Zijlstraff77e462016-01-18 15:27:07 +01002853 p->rt.timeout = 0;
2854 p->rt.time_slice = sched_rr_timeslice;
2855 p->rt.on_rq = 0;
2856 p->rt.on_list = 0;
Nick Piggin476d1392005-06-25 14:57:29 -07002857
Avi Kivitye107be32007-07-26 13:40:43 +02002858#ifdef CONFIG_PREEMPT_NOTIFIERS
2859 INIT_HLIST_HEAD(&p->preempt_notifiers);
2860#endif
Peter Zijlstracbee9f82012-10-25 14:16:43 +02002861
Mel Gorman5e1f0f02019-03-05 15:45:41 -08002862#ifdef CONFIG_COMPACTION
2863 p->capture_control = NULL;
2864#endif
Mel Gorman13784472018-05-04 16:41:09 +01002865 init_numa_balancing(clone_flags, p);
Peter Zijlstraa14886642020-05-26 18:11:04 +02002866#ifdef CONFIG_SMP
Peter Zijlstra8c4890d2020-06-22 12:01:25 +02002867 p->wake_entry.u_flags = CSD_TYPE_TTWU;
Peter Zijlstraa14886642020-05-26 18:11:04 +02002868#endif
Ingo Molnardd41f592007-07-09 18:51:59 +02002869}
2870
Srikar Dronamraju2a595722015-08-11 21:54:21 +05302871DEFINE_STATIC_KEY_FALSE(sched_numa_balancing);
2872
Mel Gorman1a687c22012-11-22 11:16:36 +00002873#ifdef CONFIG_NUMA_BALANCING
Mel Gorman3105b862012-11-23 11:23:49 +00002874
2875void set_numabalancing_state(bool enabled)
2876{
Srikar Dronamraju2a595722015-08-11 21:54:21 +05302877 if (enabled)
2878 static_branch_enable(&sched_numa_balancing);
2879 else
2880 static_branch_disable(&sched_numa_balancing);
Mel Gorman3105b862012-11-23 11:23:49 +00002881}
Andi Kleen54a43d52014-01-23 15:53:13 -08002882
2883#ifdef CONFIG_PROC_SYSCTL
2884int sysctl_numa_balancing(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +02002885 void *buffer, size_t *lenp, loff_t *ppos)
Andi Kleen54a43d52014-01-23 15:53:13 -08002886{
2887 struct ctl_table t;
2888 int err;
Srikar Dronamraju2a595722015-08-11 21:54:21 +05302889 int state = static_branch_likely(&sched_numa_balancing);
Andi Kleen54a43d52014-01-23 15:53:13 -08002890
2891 if (write && !capable(CAP_SYS_ADMIN))
2892 return -EPERM;
2893
2894 t = *table;
2895 t.data = &state;
2896 err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
2897 if (err < 0)
2898 return err;
2899 if (write)
2900 set_numabalancing_state(state);
2901 return err;
2902}
2903#endif
2904#endif
Mel Gorman1a687c22012-11-22 11:16:36 +00002905
Mel Gormancb251762016-02-05 09:08:36 +00002906#ifdef CONFIG_SCHEDSTATS
Josh Poimboeuf4698f882016-06-07 14:43:16 -05002907
2908DEFINE_STATIC_KEY_FALSE(sched_schedstats);
2909static bool __initdata __sched_schedstats = false;
2910
Mel Gormancb251762016-02-05 09:08:36 +00002911static void set_schedstats(bool enabled)
2912{
2913 if (enabled)
2914 static_branch_enable(&sched_schedstats);
2915 else
2916 static_branch_disable(&sched_schedstats);
2917}
2918
2919void force_schedstat_enabled(void)
2920{
2921 if (!schedstat_enabled()) {
2922 pr_info("kernel profiling enabled schedstats, disable via kernel.sched_schedstats.\n");
2923 static_branch_enable(&sched_schedstats);
2924 }
2925}
2926
2927static int __init setup_schedstats(char *str)
2928{
2929 int ret = 0;
2930 if (!str)
2931 goto out;
2932
Josh Poimboeuf4698f882016-06-07 14:43:16 -05002933 /*
2934 * This code is called before jump labels have been set up, so we can't
2935 * change the static branch directly just yet. Instead set a temporary
2936 * variable so init_schedstats() can do it later.
2937 */
Mel Gormancb251762016-02-05 09:08:36 +00002938 if (!strcmp(str, "enable")) {
Josh Poimboeuf4698f882016-06-07 14:43:16 -05002939 __sched_schedstats = true;
Mel Gormancb251762016-02-05 09:08:36 +00002940 ret = 1;
2941 } else if (!strcmp(str, "disable")) {
Josh Poimboeuf4698f882016-06-07 14:43:16 -05002942 __sched_schedstats = false;
Mel Gormancb251762016-02-05 09:08:36 +00002943 ret = 1;
2944 }
2945out:
2946 if (!ret)
2947 pr_warn("Unable to parse schedstats=\n");
2948
2949 return ret;
2950}
2951__setup("schedstats=", setup_schedstats);
2952
Josh Poimboeuf4698f882016-06-07 14:43:16 -05002953static void __init init_schedstats(void)
2954{
2955 set_schedstats(__sched_schedstats);
2956}
2957
Mel Gormancb251762016-02-05 09:08:36 +00002958#ifdef CONFIG_PROC_SYSCTL
Christoph Hellwig32927392020-04-24 08:43:38 +02002959int sysctl_schedstats(struct ctl_table *table, int write, void *buffer,
2960 size_t *lenp, loff_t *ppos)
Mel Gormancb251762016-02-05 09:08:36 +00002961{
2962 struct ctl_table t;
2963 int err;
2964 int state = static_branch_likely(&sched_schedstats);
2965
2966 if (write && !capable(CAP_SYS_ADMIN))
2967 return -EPERM;
2968
2969 t = *table;
2970 t.data = &state;
2971 err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
2972 if (err < 0)
2973 return err;
2974 if (write)
2975 set_schedstats(state);
2976 return err;
2977}
Josh Poimboeuf4698f882016-06-07 14:43:16 -05002978#endif /* CONFIG_PROC_SYSCTL */
2979#else /* !CONFIG_SCHEDSTATS */
2980static inline void init_schedstats(void) {}
2981#endif /* CONFIG_SCHEDSTATS */
Mel Gormancb251762016-02-05 09:08:36 +00002982
Ingo Molnardd41f592007-07-09 18:51:59 +02002983/*
2984 * fork()/clone()-time setup:
2985 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01002986int sched_fork(unsigned long clone_flags, struct task_struct *p)
Ingo Molnardd41f592007-07-09 18:51:59 +02002987{
Peter Zijlstra0122ec52011-04-05 17:23:51 +02002988 unsigned long flags;
Ingo Molnardd41f592007-07-09 18:51:59 +02002989
Rik van Riel5e1576e2013-10-07 11:29:26 +01002990 __sched_fork(clone_flags, p);
Peter Zijlstra06b83b52009-12-16 18:04:35 +01002991 /*
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02002992 * We mark the process as NEW here. This guarantees that
Peter Zijlstra06b83b52009-12-16 18:04:35 +01002993 * nobody will actually run it, and a signal or other external
2994 * event cannot wake it up and insert it on the runqueue either.
2995 */
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02002996 p->state = TASK_NEW;
Ingo Molnardd41f592007-07-09 18:51:59 +02002997
Ingo Molnarb29739f2006-06-27 02:54:51 -07002998 /*
Mike Galbraithc350a042011-07-27 17:14:55 +02002999 * Make sure we do not leak PI boosting priority to the child.
3000 */
3001 p->prio = current->normal_prio;
3002
Patrick Bellasie8f14172019-06-21 09:42:05 +01003003 uclamp_fork(p);
3004
Mike Galbraithc350a042011-07-27 17:14:55 +02003005 /*
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02003006 * Revert to default priority/policy on fork if requested.
3007 */
3008 if (unlikely(p->sched_reset_on_fork)) {
Dario Faggioliaab03e02013-11-28 11:14:43 +01003009 if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02003010 p->policy = SCHED_NORMAL;
Mike Galbraith6c697bd2009-06-17 10:48:02 +02003011 p->static_prio = NICE_TO_PRIO(0);
Mike Galbraithc350a042011-07-27 17:14:55 +02003012 p->rt_priority = 0;
3013 } else if (PRIO_TO_NICE(p->static_prio) < 0)
3014 p->static_prio = NICE_TO_PRIO(0);
3015
3016 p->prio = p->normal_prio = __normal_prio(p);
Vincent Guittot90593932017-05-17 11:50:45 +02003017 set_load_weight(p, false);
Mike Galbraith6c697bd2009-06-17 10:48:02 +02003018
Mike Galbraithb9dc29e2009-06-17 10:46:01 +02003019 /*
3020 * We don't need the reset flag anymore after the fork. It has
3021 * fulfilled its duty:
3022 */
3023 p->sched_reset_on_fork = 0;
3024 }
Lennart Poetteringca94c442009-06-15 17:17:47 +02003025
Sebastian Andrzej Siewioraf0fffd2018-07-06 15:06:15 +02003026 if (dl_prio(p->prio))
Dario Faggioliaab03e02013-11-28 11:14:43 +01003027 return -EAGAIN;
Sebastian Andrzej Siewioraf0fffd2018-07-06 15:06:15 +02003028 else if (rt_prio(p->prio))
Dario Faggioliaab03e02013-11-28 11:14:43 +01003029 p->sched_class = &rt_sched_class;
Sebastian Andrzej Siewioraf0fffd2018-07-06 15:06:15 +02003030 else
Hiroshi Shimamoto2ddbf952007-10-15 17:00:11 +02003031 p->sched_class = &fair_sched_class;
Ingo Molnarb29739f2006-06-27 02:54:51 -07003032
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02003033 init_entity_runnable_average(&p->se);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01003034
Peter Zijlstra86951592010-06-22 11:44:53 +02003035 /*
3036 * The child is not yet in the pid-hash so no cgroup attach races,
3037 * and the cgroup is pinned to this child due to cgroup_fork()
3038 * is ran before sched_fork().
3039 *
3040 * Silence PROVE_RCU.
3041 */
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003042 raw_spin_lock_irqsave(&p->pi_lock, flags);
Mathieu Desnoyersce3614d2020-07-06 16:49:10 -04003043 rseq_migrate(p);
Peter Zijlstrae210bff2016-06-16 18:51:48 +02003044 /*
Ingo Molnard1ccc662017-02-01 11:46:42 +01003045 * We're setting the CPU for the first time, we don't migrate,
Peter Zijlstrae210bff2016-06-16 18:51:48 +02003046 * so use __set_task_cpu().
3047 */
Sebastian Andrzej Siewioraf0fffd2018-07-06 15:06:15 +02003048 __set_task_cpu(p, smp_processor_id());
Peter Zijlstrae210bff2016-06-16 18:51:48 +02003049 if (p->sched_class->task_fork)
3050 p->sched_class->task_fork(p);
Peter Zijlstra0122ec52011-04-05 17:23:51 +02003051 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Peter Zijlstra5f3edc12009-09-10 13:42:00 +02003052
Naveen N. Raof6db8342015-06-25 23:53:37 +05303053#ifdef CONFIG_SCHED_INFO
Ingo Molnardd41f592007-07-09 18:51:59 +02003054 if (likely(sched_info_on()))
Chandra Seetharaman52f17b62006-07-14 00:24:38 -07003055 memset(&p->sched_info, 0, sizeof(p->sched_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056#endif
Peter Zijlstra3ca7a442011-04-05 17:23:40 +02003057#if defined(CONFIG_SMP)
3058 p->on_cpu = 0;
Nick Piggin4866cde2005-06-25 14:57:23 -07003059#endif
Peter Zijlstra01028742013-08-14 14:55:46 +02003060 init_task_preempt_count(p);
Dario Faggioli806c09a2010-11-30 19:51:33 +01003061#ifdef CONFIG_SMP
Gregory Haskins917b6272008-12-29 09:39:53 -05003062 plist_node_init(&p->pushable_tasks, MAX_PRIO);
Juri Lelli1baca4c2013-11-07 14:43:38 +01003063 RB_CLEAR_NODE(&p->pushable_dl_tasks);
Dario Faggioli806c09a2010-11-30 19:51:33 +01003064#endif
Dario Faggioliaab03e02013-11-28 11:14:43 +01003065 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066}
3067
Dario Faggioli332ac172013-11-07 14:43:45 +01003068unsigned long to_ratio(u64 period, u64 runtime)
3069{
3070 if (runtime == RUNTIME_INF)
Luca Abenic52f14d2017-05-18 22:13:31 +02003071 return BW_UNIT;
Dario Faggioli332ac172013-11-07 14:43:45 +01003072
3073 /*
3074 * Doing this here saves a lot of checks in all
3075 * the calling paths, and returning zero seems
3076 * safe for them anyway.
3077 */
3078 if (period == 0)
3079 return 0;
3080
Luca Abenic52f14d2017-05-18 22:13:31 +02003081 return div64_u64(runtime << BW_SHIFT, period);
Dario Faggioli332ac172013-11-07 14:43:45 +01003082}
3083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084/*
3085 * wake_up_new_task - wake up a newly created task for the first time.
3086 *
3087 * This function will do some initial scheduler statistics housekeeping
3088 * that must be done for every newly created context, then puts the task
3089 * on the runqueue and wakes it.
3090 */
Samir Bellabes3e51e3e2011-05-11 18:18:05 +02003091void wake_up_new_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092{
Peter Zijlstraeb580752015-07-31 21:28:18 +02003093 struct rq_flags rf;
Ingo Molnardd41f592007-07-09 18:51:59 +02003094 struct rq *rq;
Peter Zijlstrafabf3182010-01-21 21:04:57 +01003095
Peter Zijlstraeb580752015-07-31 21:28:18 +02003096 raw_spin_lock_irqsave(&p->pi_lock, rf.flags);
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02003097 p->state = TASK_RUNNING;
Peter Zijlstrafabf3182010-01-21 21:04:57 +01003098#ifdef CONFIG_SMP
3099 /*
3100 * Fork balancing, do it here and not earlier because:
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02003101 * - cpus_ptr can change in the fork path
Ingo Molnard1ccc662017-02-01 11:46:42 +01003102 * - any previously selected CPU might disappear through hotplug
Peter Zijlstrae210bff2016-06-16 18:51:48 +02003103 *
3104 * Use __set_task_cpu() to avoid calling sched_class::migrate_task_rq,
3105 * as we're not fully set-up yet.
Peter Zijlstrafabf3182010-01-21 21:04:57 +01003106 */
Mel Gorman32e839d2018-01-30 10:45:55 +00003107 p->recent_used_cpu = task_cpu(p);
Mathieu Desnoyersce3614d2020-07-06 16:49:10 -04003108 rseq_migrate(p);
Peter Zijlstrae210bff2016-06-16 18:51:48 +02003109 __set_task_cpu(p, select_task_rq(p, task_cpu(p), SD_BALANCE_FORK, 0));
Peter Zijlstrafabf3182010-01-21 21:04:57 +01003110#endif
Peter Zijlstrab7fa30c2016-06-09 15:07:50 +02003111 rq = __task_rq_lock(p, &rf);
Peter Zijlstra4126bad2016-10-03 16:20:59 +02003112 update_rq_clock(rq);
Dietmar Eggemannd0fe0b92019-01-22 16:25:01 +00003113 post_init_entity_util_avg(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
Peter Zijlstra7a57f322017-02-21 14:47:02 +01003115 activate_task(rq, p, ENQUEUE_NOCLOCK);
Peter Zijlstrafbd705a2015-06-09 11:13:36 +02003116 trace_sched_wakeup_new(p);
Peter Zijlstraa7558e02009-09-14 20:02:34 +02003117 check_preempt_curr(rq, p, WF_FORK);
Steven Rostedt9a897c52008-01-25 21:08:22 +01003118#ifdef CONFIG_SMP
Peter Zijlstra0aaafaa2015-10-23 11:50:08 +02003119 if (p->sched_class->task_woken) {
3120 /*
3121 * Nothing relies on rq->lock after this, so its fine to
3122 * drop it.
3123 */
Matt Flemingd8ac8972016-09-21 14:38:10 +01003124 rq_unpin_lock(rq, &rf);
Peter Zijlstraefbbd052009-12-16 18:04:40 +01003125 p->sched_class->task_woken(rq, p);
Matt Flemingd8ac8972016-09-21 14:38:10 +01003126 rq_repin_lock(rq, &rf);
Peter Zijlstra0aaafaa2015-10-23 11:50:08 +02003127 }
Steven Rostedt9a897c52008-01-25 21:08:22 +01003128#endif
Peter Zijlstraeb580752015-07-31 21:28:18 +02003129 task_rq_unlock(rq, p, &rf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130}
3131
Avi Kivitye107be32007-07-26 13:40:43 +02003132#ifdef CONFIG_PREEMPT_NOTIFIERS
3133
Davidlohr Buesob7203422018-03-26 14:09:26 -07003134static DEFINE_STATIC_KEY_FALSE(preempt_notifier_key);
Peter Zijlstra1cde2932015-06-08 16:00:30 +02003135
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +02003136void preempt_notifier_inc(void)
3137{
Davidlohr Buesob7203422018-03-26 14:09:26 -07003138 static_branch_inc(&preempt_notifier_key);
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +02003139}
3140EXPORT_SYMBOL_GPL(preempt_notifier_inc);
3141
3142void preempt_notifier_dec(void)
3143{
Davidlohr Buesob7203422018-03-26 14:09:26 -07003144 static_branch_dec(&preempt_notifier_key);
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +02003145}
3146EXPORT_SYMBOL_GPL(preempt_notifier_dec);
3147
Avi Kivitye107be32007-07-26 13:40:43 +02003148/**
Luis Henriques80dd99b2009-03-16 19:58:09 +00003149 * preempt_notifier_register - tell me when current is being preempted & rescheduled
Randy Dunlap421cee22007-07-31 00:37:50 -07003150 * @notifier: notifier struct to register
Avi Kivitye107be32007-07-26 13:40:43 +02003151 */
3152void preempt_notifier_register(struct preempt_notifier *notifier)
3153{
Davidlohr Buesob7203422018-03-26 14:09:26 -07003154 if (!static_branch_unlikely(&preempt_notifier_key))
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +02003155 WARN(1, "registering preempt_notifier while notifiers disabled\n");
3156
Avi Kivitye107be32007-07-26 13:40:43 +02003157 hlist_add_head(&notifier->link, &current->preempt_notifiers);
3158}
3159EXPORT_SYMBOL_GPL(preempt_notifier_register);
3160
3161/**
3162 * preempt_notifier_unregister - no longer interested in preemption notifications
Randy Dunlap421cee22007-07-31 00:37:50 -07003163 * @notifier: notifier struct to unregister
Avi Kivitye107be32007-07-26 13:40:43 +02003164 *
Mathieu Desnoyersd84525a2015-05-17 12:53:10 -04003165 * This is *not* safe to call from within a preemption notifier.
Avi Kivitye107be32007-07-26 13:40:43 +02003166 */
3167void preempt_notifier_unregister(struct preempt_notifier *notifier)
3168{
3169 hlist_del(&notifier->link);
3170}
3171EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
3172
Peter Zijlstra1cde2932015-06-08 16:00:30 +02003173static void __fire_sched_in_preempt_notifiers(struct task_struct *curr)
Avi Kivitye107be32007-07-26 13:40:43 +02003174{
3175 struct preempt_notifier *notifier;
Avi Kivitye107be32007-07-26 13:40:43 +02003176
Sasha Levinb67bfe02013-02-27 17:06:00 -08003177 hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
Avi Kivitye107be32007-07-26 13:40:43 +02003178 notifier->ops->sched_in(notifier, raw_smp_processor_id());
3179}
3180
Peter Zijlstra1cde2932015-06-08 16:00:30 +02003181static __always_inline void fire_sched_in_preempt_notifiers(struct task_struct *curr)
3182{
Davidlohr Buesob7203422018-03-26 14:09:26 -07003183 if (static_branch_unlikely(&preempt_notifier_key))
Peter Zijlstra1cde2932015-06-08 16:00:30 +02003184 __fire_sched_in_preempt_notifiers(curr);
3185}
3186
Avi Kivitye107be32007-07-26 13:40:43 +02003187static void
Peter Zijlstra1cde2932015-06-08 16:00:30 +02003188__fire_sched_out_preempt_notifiers(struct task_struct *curr,
3189 struct task_struct *next)
Avi Kivitye107be32007-07-26 13:40:43 +02003190{
3191 struct preempt_notifier *notifier;
Avi Kivitye107be32007-07-26 13:40:43 +02003192
Sasha Levinb67bfe02013-02-27 17:06:00 -08003193 hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
Avi Kivitye107be32007-07-26 13:40:43 +02003194 notifier->ops->sched_out(notifier, next);
3195}
3196
Peter Zijlstra1cde2932015-06-08 16:00:30 +02003197static __always_inline void
3198fire_sched_out_preempt_notifiers(struct task_struct *curr,
3199 struct task_struct *next)
3200{
Davidlohr Buesob7203422018-03-26 14:09:26 -07003201 if (static_branch_unlikely(&preempt_notifier_key))
Peter Zijlstra1cde2932015-06-08 16:00:30 +02003202 __fire_sched_out_preempt_notifiers(curr, next);
3203}
3204
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02003205#else /* !CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02003206
Peter Zijlstra1cde2932015-06-08 16:00:30 +02003207static inline void fire_sched_in_preempt_notifiers(struct task_struct *curr)
Avi Kivitye107be32007-07-26 13:40:43 +02003208{
3209}
3210
Peter Zijlstra1cde2932015-06-08 16:00:30 +02003211static inline void
Avi Kivitye107be32007-07-26 13:40:43 +02003212fire_sched_out_preempt_notifiers(struct task_struct *curr,
3213 struct task_struct *next)
3214{
3215}
3216
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02003217#endif /* CONFIG_PREEMPT_NOTIFIERS */
Avi Kivitye107be32007-07-26 13:40:43 +02003218
rodrigosiqueira31cb1bc2017-12-15 12:06:03 -02003219static inline void prepare_task(struct task_struct *next)
3220{
3221#ifdef CONFIG_SMP
3222 /*
3223 * Claim the task as running, we do this before switching to it
3224 * such that any running task will have this set.
3225 */
3226 next->on_cpu = 1;
3227#endif
3228}
3229
3230static inline void finish_task(struct task_struct *prev)
3231{
3232#ifdef CONFIG_SMP
3233 /*
3234 * After ->on_cpu is cleared, the task can be moved to a different CPU.
3235 * We must ensure this doesn't happen until the switch is completely
3236 * finished.
3237 *
3238 * In particular, the load of prev->state in finish_task_switch() must
3239 * happen before this.
3240 *
3241 * Pairs with the smp_cond_load_acquire() in try_to_wake_up().
3242 */
3243 smp_store_release(&prev->on_cpu, 0);
3244#endif
3245}
3246
Peter Zijlstra269d5992018-02-06 17:52:13 +01003247static inline void
3248prepare_lock_switch(struct rq *rq, struct task_struct *next, struct rq_flags *rf)
rodrigosiqueira31cb1bc2017-12-15 12:06:03 -02003249{
Peter Zijlstra269d5992018-02-06 17:52:13 +01003250 /*
3251 * Since the runqueue lock will be released by the next
3252 * task (which is an invalid locking op but in the case
3253 * of the scheduler it's an obvious special-case), so we
3254 * do an early lockdep release here:
3255 */
3256 rq_unpin_lock(rq, rf);
Qian Cai5facae42019-09-19 12:09:40 -04003257 spin_release(&rq->lock.dep_map, _THIS_IP_);
rodrigosiqueira31cb1bc2017-12-15 12:06:03 -02003258#ifdef CONFIG_DEBUG_SPINLOCK
3259 /* this is a valid case when another task releases the spinlock */
Peter Zijlstra269d5992018-02-06 17:52:13 +01003260 rq->lock.owner = next;
rodrigosiqueira31cb1bc2017-12-15 12:06:03 -02003261#endif
Peter Zijlstra269d5992018-02-06 17:52:13 +01003262}
3263
3264static inline void finish_lock_switch(struct rq *rq)
3265{
rodrigosiqueira31cb1bc2017-12-15 12:06:03 -02003266 /*
3267 * If we are tracking spinlock dependencies then we have to
3268 * fix up the runqueue lock - which gets 'carried over' from
3269 * prev into current:
3270 */
3271 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
rodrigosiqueira31cb1bc2017-12-15 12:06:03 -02003272 raw_spin_unlock_irq(&rq->lock);
3273}
3274
Ingo Molnar325ea102018-03-03 12:20:47 +01003275/*
3276 * NOP if the arch has not defined these:
3277 */
3278
3279#ifndef prepare_arch_switch
3280# define prepare_arch_switch(next) do { } while (0)
3281#endif
3282
3283#ifndef finish_arch_post_lock_switch
3284# define finish_arch_post_lock_switch() do { } while (0)
3285#endif
3286
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287/**
Nick Piggin4866cde2005-06-25 14:57:23 -07003288 * prepare_task_switch - prepare to switch tasks
3289 * @rq: the runqueue preparing to switch
Randy Dunlap421cee22007-07-31 00:37:50 -07003290 * @prev: the current task that is being switched out
Nick Piggin4866cde2005-06-25 14:57:23 -07003291 * @next: the task we are going to switch to.
3292 *
3293 * This is called with the rq lock held and interrupts off. It must
3294 * be paired with a subsequent finish_task_switch after the context
3295 * switch.
3296 *
3297 * prepare_task_switch sets up locking and calls architecture specific
3298 * hooks.
3299 */
Avi Kivitye107be32007-07-26 13:40:43 +02003300static inline void
3301prepare_task_switch(struct rq *rq, struct task_struct *prev,
3302 struct task_struct *next)
Nick Piggin4866cde2005-06-25 14:57:23 -07003303{
Mark Rutland0ed557a2018-06-14 15:27:41 -07003304 kcov_prepare_switch(prev);
Michael S. Tsirkin43148952013-09-22 17:20:54 +03003305 sched_info_switch(rq, prev, next);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003306 perf_event_task_sched_out(prev, next);
Mathieu Desnoyersd7822b12018-06-02 08:43:54 -04003307 rseq_preempt(prev);
Avi Kivitye107be32007-07-26 13:40:43 +02003308 fire_sched_out_preempt_notifiers(prev, next);
rodrigosiqueira31cb1bc2017-12-15 12:06:03 -02003309 prepare_task(next);
Nick Piggin4866cde2005-06-25 14:57:23 -07003310 prepare_arch_switch(next);
3311}
3312
3313/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 * finish_task_switch - clean up after a task-switch
3315 * @prev: the thread we just switched away from.
3316 *
Nick Piggin4866cde2005-06-25 14:57:23 -07003317 * finish_task_switch must be called after the context switch, paired
3318 * with a prepare_task_switch call before the context switch.
3319 * finish_task_switch will reconcile locking set up by prepare_task_switch,
3320 * and do any other architecture-specific cleanup actions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 *
3322 * Note that we may have delayed dropping an mm in context_switch(). If
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01003323 * so, we finish that here outside of the runqueue lock. (Doing it
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 * with the lock held can cause deadlocks; see schedule() for
3325 * details.)
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02003326 *
3327 * The context switch have flipped the stack from under us and restored the
3328 * local variables which were saved when this task called schedule() in the
3329 * past. prev == current is still correct but we need to recalculate this_rq
3330 * because prev may have moved to another CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 */
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02003332static struct rq *finish_task_switch(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 __releases(rq->lock)
3334{
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02003335 struct rq *rq = this_rq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 struct mm_struct *mm = rq->prev_mm;
Oleg Nesterov55a101f2006-09-29 02:01:10 -07003337 long prev_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
Peter Zijlstra609ca062015-09-28 17:52:18 +02003339 /*
3340 * The previous task will have left us with a preempt_count of 2
3341 * because it left us after:
3342 *
3343 * schedule()
3344 * preempt_disable(); // 1
3345 * __schedule()
3346 * raw_spin_lock_irq(&rq->lock) // 2
3347 *
3348 * Also, see FORK_PREEMPT_COUNT.
3349 */
Peter Zijlstrae2bf1c4b2015-09-29 12:18:46 +02003350 if (WARN_ONCE(preempt_count() != 2*PREEMPT_DISABLE_OFFSET,
3351 "corrupted preempt_count: %s/%d/0x%x\n",
3352 current->comm, current->pid, preempt_count()))
3353 preempt_count_set(FORK_PREEMPT_COUNT);
Peter Zijlstra609ca062015-09-28 17:52:18 +02003354
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 rq->prev_mm = NULL;
3356
3357 /*
3358 * A task struct has one reference for the use as "current".
Oleg Nesterovc394cc92006-09-29 02:01:11 -07003359 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
Oleg Nesterov55a101f2006-09-29 02:01:10 -07003360 * schedule one last time. The schedule call will never return, and
3361 * the scheduled task must drop that reference.
Peter Zijlstra95913d92015-09-29 14:45:09 +02003362 *
3363 * We must observe prev->state before clearing prev->on_cpu (in
rodrigosiqueira31cb1bc2017-12-15 12:06:03 -02003364 * finish_task), otherwise a concurrent wakeup can get prev
Peter Zijlstra95913d92015-09-29 14:45:09 +02003365 * running on another CPU and we could rave with its RUNNING -> DEAD
3366 * transition, resulting in a double drop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 */
Oleg Nesterov55a101f2006-09-29 02:01:10 -07003368 prev_state = prev->state;
Frederic Weisbeckerbf9fae92012-09-08 15:23:11 +02003369 vtime_task_switch(prev);
Stephane Eraniana8d757e2011-08-25 15:58:03 +02003370 perf_event_task_sched_in(prev, current);
rodrigosiqueira31cb1bc2017-12-15 12:06:03 -02003371 finish_task(prev);
3372 finish_lock_switch(rq);
Catalin Marinas01f23e12011-11-27 21:43:10 +00003373 finish_arch_post_lock_switch();
Mark Rutland0ed557a2018-06-14 15:27:41 -07003374 kcov_finish_switch(current);
Steven Rostedte8fa1362008-01-25 21:08:05 +01003375
Avi Kivitye107be32007-07-26 13:40:43 +02003376 fire_sched_in_preempt_notifiers(current);
Mathieu Desnoyers306e0602018-01-29 15:20:12 -05003377 /*
Mathieu Desnoyers70216e12018-01-29 15:20:17 -05003378 * When switching through a kernel thread, the loop in
3379 * membarrier_{private,global}_expedited() may have observed that
3380 * kernel thread and not issued an IPI. It is therefore possible to
3381 * schedule between user->kernel->user threads without passing though
3382 * switch_mm(). Membarrier requires a barrier after storing to
3383 * rq->curr, before returning to userspace, so provide them here:
3384 *
3385 * - a full memory barrier for {PRIVATE,GLOBAL}_EXPEDITED, implicitly
3386 * provided by mmdrop(),
3387 * - a sync_core for SYNC_CORE.
Mathieu Desnoyers306e0602018-01-29 15:20:12 -05003388 */
Mathieu Desnoyers70216e12018-01-29 15:20:17 -05003389 if (mm) {
3390 membarrier_mm_sync_core_before_usermode(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 mmdrop(mm);
Mathieu Desnoyers70216e12018-01-29 15:20:17 -05003392 }
Peter Zijlstra1cef1152018-06-07 11:45:49 +02003393 if (unlikely(prev_state == TASK_DEAD)) {
3394 if (prev->sched_class->task_dead)
3395 prev->sched_class->task_dead(prev);
Dario Faggiolie6c390f2013-11-07 14:43:35 +01003396
Peter Zijlstra1cef1152018-06-07 11:45:49 +02003397 /*
3398 * Remove function-return probe instances associated with this
3399 * task and put them back on the free list.
3400 */
3401 kprobe_flush_task(prev);
Andy Lutomirski68f24b082016-09-15 22:45:48 -07003402
Peter Zijlstra1cef1152018-06-07 11:45:49 +02003403 /* Task is done with its stack. */
3404 put_task_stack(prev);
Andy Lutomirski68f24b082016-09-15 22:45:48 -07003405
Eric W. Biederman0ff7b2c2019-09-14 07:33:58 -05003406 put_task_struct_rcu_user(prev);
bibo maoc6fd91f2006-03-26 01:38:20 -08003407 }
Frederic Weisbecker99e5ada2013-04-20 17:11:50 +02003408
Frederic Weisbeckerde734f82015-06-11 18:07:12 +02003409 tick_nohz_task_switch();
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02003410 return rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411}
3412
Gregory Haskins3f029d32009-07-29 11:08:47 -04003413#ifdef CONFIG_SMP
3414
Gregory Haskins3f029d32009-07-29 11:08:47 -04003415/* rq->lock is NOT held, but preemption is disabled */
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02003416static void __balance_callback(struct rq *rq)
Gregory Haskins3f029d32009-07-29 11:08:47 -04003417{
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02003418 struct callback_head *head, *next;
3419 void (*func)(struct rq *rq);
3420 unsigned long flags;
Gregory Haskins3f029d32009-07-29 11:08:47 -04003421
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02003422 raw_spin_lock_irqsave(&rq->lock, flags);
3423 head = rq->balance_callback;
3424 rq->balance_callback = NULL;
3425 while (head) {
3426 func = (void (*)(struct rq *))head->func;
3427 next = head->next;
3428 head->next = NULL;
3429 head = next;
Gregory Haskins3f029d32009-07-29 11:08:47 -04003430
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02003431 func(rq);
Gregory Haskins3f029d32009-07-29 11:08:47 -04003432 }
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02003433 raw_spin_unlock_irqrestore(&rq->lock, flags);
3434}
3435
3436static inline void balance_callback(struct rq *rq)
3437{
3438 if (unlikely(rq->balance_callback))
3439 __balance_callback(rq);
Gregory Haskins3f029d32009-07-29 11:08:47 -04003440}
3441
3442#else
3443
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02003444static inline void balance_callback(struct rq *rq)
Gregory Haskins3f029d32009-07-29 11:08:47 -04003445{
3446}
3447
3448#endif
3449
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450/**
3451 * schedule_tail - first thing a freshly forked thread must call.
3452 * @prev: the thread we just switched away from.
3453 */
Andi Kleen722a9f92014-05-02 00:44:38 +02003454asmlinkage __visible void schedule_tail(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 __releases(rq->lock)
3456{
Oleg Nesterov1a43a142014-10-08 21:36:44 +02003457 struct rq *rq;
Ingo Molnar70b97a72006-07-03 00:25:42 -07003458
Peter Zijlstra609ca062015-09-28 17:52:18 +02003459 /*
3460 * New tasks start with FORK_PREEMPT_COUNT, see there and
3461 * finish_task_switch() for details.
3462 *
3463 * finish_task_switch() will drop rq->lock() and lower preempt_count
3464 * and the preempt_enable() will end up enabling preemption (on
3465 * PREEMPT_COUNT kernels).
3466 */
3467
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02003468 rq = finish_task_switch(prev);
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02003469 balance_callback(rq);
Oleg Nesterov1a43a142014-10-08 21:36:44 +02003470 preempt_enable();
Steven Rostedtda19ab52009-07-29 00:21:22 -04003471
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 if (current->set_child_tid)
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003473 put_user(task_pid_vnr(current), current->set_child_tid);
Eric W. Biederman088fe472018-07-23 17:26:49 -05003474
3475 calculate_sigpending();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476}
3477
3478/*
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02003479 * context_switch - switch to the new MM and the new thread's register state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 */
Josh Poimboeuf04936942016-02-28 22:22:39 -06003481static __always_inline struct rq *
Ingo Molnar70b97a72006-07-03 00:25:42 -07003482context_switch(struct rq *rq, struct task_struct *prev,
Matt Flemingd8ac8972016-09-21 14:38:10 +01003483 struct task_struct *next, struct rq_flags *rf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484{
Avi Kivitye107be32007-07-26 13:40:43 +02003485 prepare_task_switch(rq, prev, next);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003486
Zachary Amsden9226d122007-02-13 13:26:21 +01003487 /*
3488 * For paravirt, this is coupled with an exit in switch_to to
3489 * combine the page table reload and the switch backend into
3490 * one hypercall.
3491 */
Jeremy Fitzhardinge224101e2009-02-18 11:18:57 -08003492 arch_start_context_switch(prev);
Zachary Amsden9226d122007-02-13 13:26:21 +01003493
Mathieu Desnoyers306e0602018-01-29 15:20:12 -05003494 /*
Peter Zijlstra139d0252019-07-29 16:05:15 +02003495 * kernel -> kernel lazy + transfer active
3496 * user -> kernel lazy + mmgrab() active
3497 *
3498 * kernel -> user switch + mmdrop() active
3499 * user -> user switch
Mathieu Desnoyers306e0602018-01-29 15:20:12 -05003500 */
Peter Zijlstra139d0252019-07-29 16:05:15 +02003501 if (!next->mm) { // to kernel
3502 enter_lazy_tlb(prev->active_mm, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
Peter Zijlstra139d0252019-07-29 16:05:15 +02003504 next->active_mm = prev->active_mm;
3505 if (prev->mm) // from user
3506 mmgrab(prev->active_mm);
3507 else
3508 prev->active_mm = NULL;
3509 } else { // to user
Mathieu Desnoyers227a4aa2019-09-19 13:37:02 -04003510 membarrier_switch_mm(rq, prev->active_mm, next->mm);
Peter Zijlstra139d0252019-07-29 16:05:15 +02003511 /*
3512 * sys_membarrier() requires an smp_mb() between setting
Mathieu Desnoyers227a4aa2019-09-19 13:37:02 -04003513 * rq->curr / membarrier_switch_mm() and returning to userspace.
Peter Zijlstra139d0252019-07-29 16:05:15 +02003514 *
3515 * The below provides this either through switch_mm(), or in
3516 * case 'prev->active_mm == next->mm' through
3517 * finish_task_switch()'s mmdrop().
3518 */
Peter Zijlstra139d0252019-07-29 16:05:15 +02003519 switch_mm_irqs_off(prev->active_mm, next->mm, next);
3520
3521 if (!prev->mm) { // from kernel
3522 /* will mmdrop() in finish_task_switch(). */
3523 rq->prev_mm = prev->active_mm;
3524 prev->active_mm = NULL;
3525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 }
Matt Fleming92509b72016-09-21 14:38:11 +01003527
Matt Flemingcb42c9a2016-09-21 14:38:13 +01003528 rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP);
Matt Fleming92509b72016-09-21 14:38:11 +01003529
Peter Zijlstra269d5992018-02-06 17:52:13 +01003530 prepare_lock_switch(rq, next, rf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
3532 /* Here we just switch the register state and the stack. */
3533 switch_to(prev, next, prev);
Ingo Molnardd41f592007-07-09 18:51:59 +02003534 barrier();
Oleg Nesterovdfa50b62014-10-09 21:32:32 +02003535
3536 return finish_task_switch(prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537}
3538
3539/*
Sha Zhengju1c3e8262013-02-20 17:14:38 +08003540 * nr_running and nr_context_switches:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 *
3542 * externally visible scheduler statistics: current number of runnable
Sha Zhengju1c3e8262013-02-20 17:14:38 +08003543 * threads, total number of context switches performed since bootup.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 */
3545unsigned long nr_running(void)
3546{
3547 unsigned long i, sum = 0;
3548
3549 for_each_online_cpu(i)
3550 sum += cpu_rq(i)->nr_running;
3551
3552 return sum;
3553}
3554
Tim Chen2ee507c2014-07-31 10:29:48 -07003555/*
Ingo Molnard1ccc662017-02-01 11:46:42 +01003556 * Check if only the current task is running on the CPU.
Dominik Dingel00cc16332015-09-18 11:27:45 +02003557 *
3558 * Caution: this function does not check that the caller has disabled
3559 * preemption, thus the result might have a time-of-check-to-time-of-use
3560 * race. The caller is responsible to use it correctly, for example:
3561 *
Ingo Molnardfcb2452018-12-03 10:05:56 +01003562 * - from a non-preemptible section (of course)
Dominik Dingel00cc16332015-09-18 11:27:45 +02003563 *
3564 * - from a thread that is bound to a single CPU
3565 *
3566 * - in a loop with very short iterations (e.g. a polling loop)
Tim Chen2ee507c2014-07-31 10:29:48 -07003567 */
3568bool single_task_running(void)
3569{
Dominik Dingel00cc16332015-09-18 11:27:45 +02003570 return raw_rq()->nr_running == 1;
Tim Chen2ee507c2014-07-31 10:29:48 -07003571}
3572EXPORT_SYMBOL(single_task_running);
3573
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574unsigned long long nr_context_switches(void)
3575{
Steven Rostedtcc94abf2006-06-27 02:54:31 -07003576 int i;
3577 unsigned long long sum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08003579 for_each_possible_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 sum += cpu_rq(i)->nr_switches;
3581
3582 return sum;
3583}
3584
Tejun Heoe33a9bb2016-12-07 15:48:41 -05003585/*
Daniel Lezcano145d9522018-10-04 14:04:02 +02003586 * Consumers of these two interfaces, like for example the cpuidle menu
3587 * governor, are using nonsensical data. Preferring shallow idle state selection
3588 * for a CPU that has IO-wait which might not even end up running the task when
3589 * it does become runnable.
3590 */
3591
3592unsigned long nr_iowait_cpu(int cpu)
3593{
3594 return atomic_read(&cpu_rq(cpu)->nr_iowait);
3595}
3596
Daniel Lezcano145d9522018-10-04 14:04:02 +02003597/*
Tejun Heoe33a9bb2016-12-07 15:48:41 -05003598 * IO-wait accounting, and how its mostly bollocks (on SMP).
3599 *
3600 * The idea behind IO-wait account is to account the idle time that we could
3601 * have spend running if it were not for IO. That is, if we were to improve the
3602 * storage performance, we'd have a proportional reduction in IO-wait time.
3603 *
3604 * This all works nicely on UP, where, when a task blocks on IO, we account
3605 * idle time as IO-wait, because if the storage were faster, it could've been
3606 * running and we'd not be idle.
3607 *
3608 * This has been extended to SMP, by doing the same for each CPU. This however
3609 * is broken.
3610 *
3611 * Imagine for instance the case where two tasks block on one CPU, only the one
3612 * CPU will have IO-wait accounted, while the other has regular idle. Even
3613 * though, if the storage were faster, both could've ran at the same time,
3614 * utilising both CPUs.
3615 *
3616 * This means, that when looking globally, the current IO-wait accounting on
3617 * SMP is a lower bound, by reason of under accounting.
3618 *
3619 * Worse, since the numbers are provided per CPU, they are sometimes
3620 * interpreted per CPU, and that is nonsensical. A blocked task isn't strictly
3621 * associated with any one particular CPU, it can wake to another CPU than it
3622 * blocked on. This means the per CPU IO-wait number is meaningless.
3623 *
3624 * Task CPU affinities can make all that even more 'interesting'.
3625 */
3626
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627unsigned long nr_iowait(void)
3628{
3629 unsigned long i, sum = 0;
3630
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08003631 for_each_possible_cpu(i)
Daniel Lezcano145d9522018-10-04 14:04:02 +02003632 sum += nr_iowait_cpu(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
3634 return sum;
3635}
3636
Ingo Molnardd41f592007-07-09 18:51:59 +02003637#ifdef CONFIG_SMP
3638
Ingo Molnar48f24c42006-07-03 00:25:40 -07003639/*
Peter Zijlstra38022902009-12-16 18:04:37 +01003640 * sched_exec - execve() is a valuable balancing opportunity, because at
3641 * this point the task has the smallest effective memory and cache footprint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 */
Peter Zijlstra38022902009-12-16 18:04:37 +01003643void sched_exec(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644{
Peter Zijlstra38022902009-12-16 18:04:37 +01003645 struct task_struct *p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 unsigned long flags;
Peter Zijlstra0017d732010-03-24 18:34:10 +01003647 int dest_cpu;
Peter Zijlstra38022902009-12-16 18:04:37 +01003648
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02003649 raw_spin_lock_irqsave(&p->pi_lock, flags);
Peter Zijlstraac66f542013-10-07 11:29:16 +01003650 dest_cpu = p->sched_class->select_task_rq(p, task_cpu(p), SD_BALANCE_EXEC, 0);
Peter Zijlstra0017d732010-03-24 18:34:10 +01003651 if (dest_cpu == smp_processor_id())
3652 goto unlock;
Peter Zijlstra38022902009-12-16 18:04:37 +01003653
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02003654 if (likely(cpu_active(dest_cpu))) {
Tejun Heo969c7922010-05-06 18:49:21 +02003655 struct migration_arg arg = { p, dest_cpu };
Ingo Molnar36c8b582006-07-03 00:25:41 -07003656
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02003657 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
3658 stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 return;
3660 }
Peter Zijlstra0017d732010-03-24 18:34:10 +01003661unlock:
Peter Zijlstra8f42ced2011-04-05 17:23:53 +02003662 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663}
3664
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665#endif
3666
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667DEFINE_PER_CPU(struct kernel_stat, kstat);
Glauber Costa3292beb2011-11-28 14:45:17 -02003668DEFINE_PER_CPU(struct kernel_cpustat, kernel_cpustat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669
3670EXPORT_PER_CPU_SYMBOL(kstat);
Glauber Costa3292beb2011-11-28 14:45:17 -02003671EXPORT_PER_CPU_SYMBOL(kernel_cpustat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
3673/*
Giovanni Gherdovich60756202016-08-05 10:21:56 +02003674 * The function fair_sched_class.update_curr accesses the struct curr
3675 * and its field curr->exec_start; when called from task_sched_runtime(),
3676 * we observe a high rate of cache misses in practice.
3677 * Prefetching this data results in improved performance.
3678 */
3679static inline void prefetch_curr_exec_start(struct task_struct *p)
3680{
3681#ifdef CONFIG_FAIR_GROUP_SCHED
3682 struct sched_entity *curr = (&p->se)->cfs_rq->curr;
3683#else
3684 struct sched_entity *curr = (&task_rq(p)->cfs)->curr;
3685#endif
3686 prefetch(curr);
3687 prefetch(&curr->exec_start);
3688}
3689
3690/*
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003691 * Return accounted runtime for the task.
3692 * In case the task is currently running, return the runtime plus current's
3693 * pending runtime that have not been accounted yet.
3694 */
3695unsigned long long task_sched_runtime(struct task_struct *p)
3696{
Peter Zijlstraeb580752015-07-31 21:28:18 +02003697 struct rq_flags rf;
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003698 struct rq *rq;
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01003699 u64 ns;
Ingo Molnar48f24c42006-07-03 00:25:40 -07003700
Peter Zijlstra911b2892013-11-11 18:21:56 +01003701#if defined(CONFIG_64BIT) && defined(CONFIG_SMP)
3702 /*
Ingo Molnar97fb7a02018-03-03 14:01:12 +01003703 * 64-bit doesn't need locks to atomically read a 64-bit value.
Peter Zijlstra911b2892013-11-11 18:21:56 +01003704 * So we have a optimization chance when the task's delta_exec is 0.
3705 * Reading ->on_cpu is racy, but this is ok.
3706 *
Ingo Molnard1ccc662017-02-01 11:46:42 +01003707 * If we race with it leaving CPU, we'll take a lock. So we're correct.
3708 * If we race with it entering CPU, unaccounted time is 0. This is
Peter Zijlstra911b2892013-11-11 18:21:56 +01003709 * indistinguishable from the read occurring a few cycles earlier.
Mike Galbraith4036ac12014-06-24 07:49:40 +02003710 * If we see ->on_cpu without ->on_rq, the task is leaving, and has
3711 * been accounted, so we're correct here as well.
Peter Zijlstra911b2892013-11-11 18:21:56 +01003712 */
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04003713 if (!p->on_cpu || !task_on_rq_queued(p))
Peter Zijlstra911b2892013-11-11 18:21:56 +01003714 return p->se.sum_exec_runtime;
3715#endif
3716
Peter Zijlstraeb580752015-07-31 21:28:18 +02003717 rq = task_rq_lock(p, &rf);
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01003718 /*
3719 * Must be ->curr _and_ ->on_rq. If dequeued, we would
3720 * project cycles that may never be accounted to this
3721 * thread, breaking clock_gettime().
3722 */
3723 if (task_current(rq, p) && task_on_rq_queued(p)) {
Giovanni Gherdovich60756202016-08-05 10:21:56 +02003724 prefetch_curr_exec_start(p);
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01003725 update_rq_clock(rq);
3726 p->sched_class->update_curr(rq);
3727 }
3728 ns = p->se.sum_exec_runtime;
Peter Zijlstraeb580752015-07-31 21:28:18 +02003729 task_rq_unlock(rq, p, &rf);
Hidetoshi Setoc5f8d992009-03-31 16:56:03 +09003730
3731 return ns;
3732}
3733
Ingo Molnar14533a12020-03-06 14:26:31 +01003734DEFINE_PER_CPU(unsigned long, thermal_pressure);
3735
3736void arch_set_thermal_pressure(struct cpumask *cpus,
3737 unsigned long th_pressure)
3738{
3739 int cpu;
3740
3741 for_each_cpu(cpu, cpus)
3742 WRITE_ONCE(per_cpu(thermal_pressure, cpu), th_pressure);
3743}
3744
Balbir Singh49048622008-09-05 18:12:23 +02003745/*
Christoph Lameter7835b982006-12-10 02:20:22 -08003746 * This function gets called by the timer code, with HZ frequency.
3747 * We call it with interrupts disabled.
Christoph Lameter7835b982006-12-10 02:20:22 -08003748 */
3749void scheduler_tick(void)
3750{
Christoph Lameter7835b982006-12-10 02:20:22 -08003751 int cpu = smp_processor_id();
3752 struct rq *rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02003753 struct task_struct *curr = rq->curr;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02003754 struct rq_flags rf;
Thara Gopinathb4eccf52020-02-21 19:52:10 -05003755 unsigned long thermal_pressure;
Peter Zijlstra3e51f332008-05-03 18:29:28 +02003756
Giovanni Gherdovich1567c3e32020-01-22 16:16:12 +01003757 arch_scale_freq_tick();
Peter Zijlstra3e51f332008-05-03 18:29:28 +02003758 sched_clock_tick();
Christoph Lameter7835b982006-12-10 02:20:22 -08003759
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02003760 rq_lock(rq, &rf);
3761
Peter Zijlstra3e51f332008-05-03 18:29:28 +02003762 update_rq_clock(rq);
Thara Gopinathb4eccf52020-02-21 19:52:10 -05003763 thermal_pressure = arch_scale_thermal_pressure(cpu_of(rq));
Thara Gopinath05289b92020-02-21 19:52:13 -05003764 update_thermal_load_avg(rq_clock_thermal(rq), rq, thermal_pressure);
Peter Zijlstrafa85ae22008-01-25 21:08:29 +01003765 curr->sched_class->task_tick(rq, curr, 0);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +02003766 calc_global_load_tick(rq);
Johannes Weinereb414682018-10-26 15:06:27 -07003767 psi_task_tick(rq);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02003768
3769 rq_unlock(rq, &rf);
Ingo Molnardd41f592007-07-09 18:51:59 +02003770
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003771 perf_event_task_tick();
Peter Zijlstrae220d2d2009-05-23 18:28:55 +02003772
Christoph Lametere418e1c2006-12-10 02:20:23 -08003773#ifdef CONFIG_SMP
Suresh Siddha6eb57e02011-10-03 15:09:01 -07003774 rq->idle_balance = idle_cpu(cpu);
Daniel Lezcano7caff662014-01-06 12:34:38 +01003775 trigger_load_balance(rq);
Christoph Lametere418e1c2006-12-10 02:20:23 -08003776#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777}
3778
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02003779#ifdef CONFIG_NO_HZ_FULL
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01003780
3781struct tick_work {
3782 int cpu;
Paul E. McKenneyb55bd582019-05-30 05:39:25 -07003783 atomic_t state;
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01003784 struct delayed_work work;
3785};
Paul E. McKenneyb55bd582019-05-30 05:39:25 -07003786/* Values for ->state, see diagram below. */
3787#define TICK_SCHED_REMOTE_OFFLINE 0
3788#define TICK_SCHED_REMOTE_OFFLINING 1
3789#define TICK_SCHED_REMOTE_RUNNING 2
3790
3791/*
3792 * State diagram for ->state:
3793 *
3794 *
3795 * TICK_SCHED_REMOTE_OFFLINE
3796 * | ^
3797 * | |
3798 * | | sched_tick_remote()
3799 * | |
3800 * | |
3801 * +--TICK_SCHED_REMOTE_OFFLINING
3802 * | ^
3803 * | |
3804 * sched_tick_start() | | sched_tick_stop()
3805 * | |
3806 * V |
3807 * TICK_SCHED_REMOTE_RUNNING
3808 *
3809 *
3810 * Other transitions get WARN_ON_ONCE(), except that sched_tick_remote()
3811 * and sched_tick_start() are happy to leave the state in RUNNING.
3812 */
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01003813
3814static struct tick_work __percpu *tick_work_cpu;
3815
3816static void sched_tick_remote(struct work_struct *work)
3817{
3818 struct delayed_work *dwork = to_delayed_work(work);
3819 struct tick_work *twork = container_of(dwork, struct tick_work, work);
3820 int cpu = twork->cpu;
3821 struct rq *rq = cpu_rq(cpu);
Frederic Weisbeckerd9c0ffc2018-06-28 18:29:41 +02003822 struct task_struct *curr;
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01003823 struct rq_flags rf;
Frederic Weisbeckerd9c0ffc2018-06-28 18:29:41 +02003824 u64 delta;
Paul E. McKenneyb55bd582019-05-30 05:39:25 -07003825 int os;
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01003826
3827 /*
3828 * Handle the tick only if it appears the remote CPU is running in full
3829 * dynticks mode. The check is racy by nature, but missing a tick or
3830 * having one too much is no big deal because the scheduler tick updates
3831 * statistics and checks timeslices in a time-independent way, regardless
3832 * of when exactly it is running.
3833 */
Scott Wood488603b2020-01-11 04:53:38 -05003834 if (!tick_nohz_tick_stopped_cpu(cpu))
Frederic Weisbeckerd9c0ffc2018-06-28 18:29:41 +02003835 goto out_requeue;
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01003836
Frederic Weisbeckerd9c0ffc2018-06-28 18:29:41 +02003837 rq_lock_irq(rq, &rf);
3838 curr = rq->curr;
Scott Wood488603b2020-01-11 04:53:38 -05003839 if (cpu_is_offline(cpu))
Frederic Weisbeckerd9c0ffc2018-06-28 18:29:41 +02003840 goto out_unlock;
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01003841
Frederic Weisbeckerd9c0ffc2018-06-28 18:29:41 +02003842 update_rq_clock(rq);
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01003843
Scott Wood488603b2020-01-11 04:53:38 -05003844 if (!is_idle_task(curr)) {
3845 /*
3846 * Make sure the next tick runs within a reasonable
3847 * amount of time.
3848 */
3849 delta = rq_clock_task(rq) - curr->se.exec_start;
3850 WARN_ON_ONCE(delta > (u64)NSEC_PER_SEC * 3);
3851 }
Frederic Weisbeckerd9c0ffc2018-06-28 18:29:41 +02003852 curr->sched_class->task_tick(rq, curr, 0);
3853
Peter Zijlstra (Intel)ebc0f832020-01-11 04:53:39 -05003854 calc_load_nohz_remote(rq);
Frederic Weisbeckerd9c0ffc2018-06-28 18:29:41 +02003855out_unlock:
3856 rq_unlock_irq(rq, &rf);
Frederic Weisbeckerd9c0ffc2018-06-28 18:29:41 +02003857out_requeue:
Peter Zijlstra (Intel)ebc0f832020-01-11 04:53:39 -05003858
Frederic Weisbeckerd9c0ffc2018-06-28 18:29:41 +02003859 /*
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01003860 * Run the remote tick once per second (1Hz). This arbitrary
3861 * frequency is large enough to avoid overload but short enough
Paul E. McKenneyb55bd582019-05-30 05:39:25 -07003862 * to keep scheduler internal stats reasonably up to date. But
3863 * first update state to reflect hotplug activity if required.
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01003864 */
Paul E. McKenneyb55bd582019-05-30 05:39:25 -07003865 os = atomic_fetch_add_unless(&twork->state, -1, TICK_SCHED_REMOTE_RUNNING);
3866 WARN_ON_ONCE(os == TICK_SCHED_REMOTE_OFFLINE);
3867 if (os == TICK_SCHED_REMOTE_RUNNING)
3868 queue_delayed_work(system_unbound_wq, dwork, HZ);
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01003869}
3870
3871static void sched_tick_start(int cpu)
3872{
Paul E. McKenneyb55bd582019-05-30 05:39:25 -07003873 int os;
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01003874 struct tick_work *twork;
3875
3876 if (housekeeping_cpu(cpu, HK_FLAG_TICK))
3877 return;
3878
3879 WARN_ON_ONCE(!tick_work_cpu);
3880
3881 twork = per_cpu_ptr(tick_work_cpu, cpu);
Paul E. McKenneyb55bd582019-05-30 05:39:25 -07003882 os = atomic_xchg(&twork->state, TICK_SCHED_REMOTE_RUNNING);
3883 WARN_ON_ONCE(os == TICK_SCHED_REMOTE_RUNNING);
3884 if (os == TICK_SCHED_REMOTE_OFFLINE) {
3885 twork->cpu = cpu;
3886 INIT_DELAYED_WORK(&twork->work, sched_tick_remote);
3887 queue_delayed_work(system_unbound_wq, &twork->work, HZ);
3888 }
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01003889}
3890
3891#ifdef CONFIG_HOTPLUG_CPU
3892static void sched_tick_stop(int cpu)
3893{
3894 struct tick_work *twork;
Paul E. McKenneyb55bd582019-05-30 05:39:25 -07003895 int os;
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01003896
3897 if (housekeeping_cpu(cpu, HK_FLAG_TICK))
3898 return;
3899
3900 WARN_ON_ONCE(!tick_work_cpu);
3901
3902 twork = per_cpu_ptr(tick_work_cpu, cpu);
Paul E. McKenneyb55bd582019-05-30 05:39:25 -07003903 /* There cannot be competing actions, but don't rely on stop-machine. */
3904 os = atomic_xchg(&twork->state, TICK_SCHED_REMOTE_OFFLINING);
3905 WARN_ON_ONCE(os != TICK_SCHED_REMOTE_RUNNING);
3906 /* Don't cancel, as this would mess up the state machine. */
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01003907}
3908#endif /* CONFIG_HOTPLUG_CPU */
3909
3910int __init sched_tick_offload_init(void)
3911{
3912 tick_work_cpu = alloc_percpu(struct tick_work);
3913 BUG_ON(!tick_work_cpu);
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01003914 return 0;
3915}
3916
3917#else /* !CONFIG_NO_HZ_FULL */
3918static inline void sched_tick_start(int cpu) { }
3919static inline void sched_tick_stop(int cpu) { }
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02003920#endif
3921
Thomas Gleixnerc1a280b2019-07-26 23:19:37 +02003922#if defined(CONFIG_PREEMPTION) && (defined(CONFIG_DEBUG_PREEMPT) || \
Joel Fernandes (Google)c3bc8fd2018-07-30 15:24:23 -07003923 defined(CONFIG_TRACE_PREEMPT_TOGGLE))
Steven Rostedt47252cf2016-03-21 11:23:39 -04003924/*
3925 * If the value passed in is equal to the current preempt count
3926 * then we just disabled preemption. Start timing the latency.
3927 */
3928static inline void preempt_latency_start(int val)
3929{
3930 if (preempt_count() == val) {
3931 unsigned long ip = get_lock_parent_ip();
3932#ifdef CONFIG_DEBUG_PREEMPT
3933 current->preempt_disable_ip = ip;
3934#endif
3935 trace_preempt_off(CALLER_ADDR0, ip);
3936 }
3937}
Steven Rostedt7e49fcc2009-01-22 19:01:40 -05003938
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09003939void preempt_count_add(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003941#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 /*
3943 * Underflow?
3944 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003945 if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
3946 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003947#endif
Peter Zijlstrabdb43802013-09-10 12:15:23 +02003948 __preempt_count_add(val);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003949#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 /*
3951 * Spinlock count overflowing soon?
3952 */
Miguel Ojeda Sandonis33859f72006-12-10 02:20:38 -08003953 DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
3954 PREEMPT_MASK - 10);
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003955#endif
Steven Rostedt47252cf2016-03-21 11:23:39 -04003956 preempt_latency_start(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957}
Peter Zijlstrabdb43802013-09-10 12:15:23 +02003958EXPORT_SYMBOL(preempt_count_add);
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09003959NOKPROBE_SYMBOL(preempt_count_add);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960
Steven Rostedt47252cf2016-03-21 11:23:39 -04003961/*
3962 * If the value passed in equals to the current preempt count
3963 * then we just enabled preemption. Stop timing the latency.
3964 */
3965static inline void preempt_latency_stop(int val)
3966{
3967 if (preempt_count() == val)
3968 trace_preempt_on(CALLER_ADDR0, get_lock_parent_ip());
3969}
3970
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09003971void preempt_count_sub(int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972{
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003973#ifdef CONFIG_DEBUG_PREEMPT
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 /*
3975 * Underflow?
3976 */
Ingo Molnar01e3eb82009-01-12 13:00:50 +01003977 if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003978 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 /*
3980 * Is the spinlock portion underflowing?
3981 */
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003982 if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
3983 !(preempt_count() & PREEMPT_MASK)))
3984 return;
Steven Rostedt6cd8a4b2008-05-12 21:20:42 +02003985#endif
Ingo Molnar9a11b49a2006-07-03 00:24:33 -07003986
Steven Rostedt47252cf2016-03-21 11:23:39 -04003987 preempt_latency_stop(val);
Peter Zijlstrabdb43802013-09-10 12:15:23 +02003988 __preempt_count_sub(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989}
Peter Zijlstrabdb43802013-09-10 12:15:23 +02003990EXPORT_SYMBOL(preempt_count_sub);
Masami Hiramatsuedafe3a2014-04-17 17:18:42 +09003991NOKPROBE_SYMBOL(preempt_count_sub);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
Steven Rostedt47252cf2016-03-21 11:23:39 -04003993#else
3994static inline void preempt_latency_start(int val) { }
3995static inline void preempt_latency_stop(int val) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996#endif
3997
Ingo Molnar59ddbcb2017-02-03 23:37:48 +01003998static inline unsigned long get_preempt_disable_ip(struct task_struct *p)
3999{
4000#ifdef CONFIG_DEBUG_PREEMPT
4001 return p->preempt_disable_ip;
4002#else
4003 return 0;
4004#endif
4005}
4006
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007/*
Ingo Molnardd41f592007-07-09 18:51:59 +02004008 * Print scheduling while atomic bug:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 */
Ingo Molnardd41f592007-07-09 18:51:59 +02004010static noinline void __schedule_bug(struct task_struct *prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011{
Vegard Nossumd1c6d142016-07-23 09:46:39 +02004012 /* Save this before calling printk(), since that will clobber it */
4013 unsigned long preempt_disable_ip = get_preempt_disable_ip(current);
4014
Dave Jones664dfa62011-12-22 16:39:30 -05004015 if (oops_in_progress)
4016 return;
4017
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01004018 printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
4019 prev->comm, prev->pid, preempt_count());
Satyam Sharma838225b2007-10-24 18:23:50 +02004020
Ingo Molnardd41f592007-07-09 18:51:59 +02004021 debug_show_held_locks(prev);
Arjan van de Vene21f5b12008-05-23 09:05:58 -07004022 print_modules();
Ingo Molnardd41f592007-07-09 18:51:59 +02004023 if (irqs_disabled())
4024 print_irqtrace_events(prev);
Vegard Nossumd1c6d142016-07-23 09:46:39 +02004025 if (IS_ENABLED(CONFIG_DEBUG_PREEMPT)
4026 && in_atomic_preempt_off()) {
Thomas Gleixner8f47b182014-02-07 20:58:39 +01004027 pr_err("Preemption disabled at:");
Dmitry Safonov2062a4e2020-06-08 21:29:56 -07004028 print_ip_sym(KERN_ERR, preempt_disable_ip);
Thomas Gleixner8f47b182014-02-07 20:58:39 +01004029 }
Daniel Bristot de Oliveira748c7202016-06-03 17:10:18 -03004030 if (panic_on_warn)
4031 panic("scheduling while atomic\n");
4032
Stephen Boyd6135fc12012-03-28 17:10:47 -07004033 dump_stack();
Rusty Russell373d4d02013-01-21 17:17:39 +10304034 add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
Ingo Molnardd41f592007-07-09 18:51:59 +02004035}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
Ingo Molnardd41f592007-07-09 18:51:59 +02004037/*
4038 * Various schedule()-time debugging checks and statistics:
4039 */
Daniel Vetter312364f32019-08-26 22:14:23 +02004040static inline void schedule_debug(struct task_struct *prev, bool preempt)
Ingo Molnardd41f592007-07-09 18:51:59 +02004041{
Aaron Tomlin0d9e2632014-09-12 14:16:19 +01004042#ifdef CONFIG_SCHED_STACK_END_CHECK
Jann Horn29d64552016-06-01 11:55:07 +02004043 if (task_stack_end_corrupted(prev))
4044 panic("corrupted stack end detected inside scheduler\n");
Will Deacon88485be2020-05-15 14:56:05 +01004045
4046 if (task_scs_end_corrupted(prev))
4047 panic("corrupted shadow stack detected inside scheduler\n");
Aaron Tomlin0d9e2632014-09-12 14:16:19 +01004048#endif
Peter Zijlstrab99def82015-09-28 18:02:03 +02004049
Daniel Vetter312364f32019-08-26 22:14:23 +02004050#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
4051 if (!preempt && prev->state && prev->non_block_count) {
4052 printk(KERN_ERR "BUG: scheduling in a non-blocking section: %s/%d/%i\n",
4053 prev->comm, prev->pid, prev->non_block_count);
4054 dump_stack();
4055 add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
4056 }
4057#endif
4058
Peter Zijlstra1dc0fff2015-09-28 17:57:39 +02004059 if (unlikely(in_atomic_preempt_off())) {
Ingo Molnardd41f592007-07-09 18:51:59 +02004060 __schedule_bug(prev);
Peter Zijlstra1dc0fff2015-09-28 17:57:39 +02004061 preempt_count_set(PREEMPT_DISABLED);
4062 }
Paul E. McKenneyb3fbab02011-05-24 08:31:09 -07004063 rcu_sleep_check();
Ingo Molnardd41f592007-07-09 18:51:59 +02004064
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 profile_hit(SCHED_PROFILING, __builtin_return_address(0));
4066
Josh Poimboeufae928822016-06-17 12:43:24 -05004067 schedstat_inc(this_rq()->sched_count);
Ingo Molnardd41f592007-07-09 18:51:59 +02004068}
4069
Chen Yu457d1f42020-04-21 18:50:43 +08004070static void put_prev_task_balance(struct rq *rq, struct task_struct *prev,
4071 struct rq_flags *rf)
4072{
4073#ifdef CONFIG_SMP
4074 const struct sched_class *class;
4075 /*
4076 * We must do the balancing pass before put_prev_task(), such
4077 * that when we release the rq->lock the task is in the same
4078 * state as before we took rq->lock.
4079 *
4080 * We can terminate the balance pass as soon as we know there is
4081 * a runnable task of @class priority or higher.
4082 */
4083 for_class_range(class, prev->sched_class, &idle_sched_class) {
4084 if (class->balance(rq, prev, rf))
4085 break;
4086 }
4087#endif
4088
4089 put_prev_task(rq, prev);
4090}
4091
Ingo Molnardd41f592007-07-09 18:51:59 +02004092/*
4093 * Pick up the highest-prio task:
4094 */
4095static inline struct task_struct *
Matt Flemingd8ac8972016-09-21 14:38:10 +01004096pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
Ingo Molnardd41f592007-07-09 18:51:59 +02004097{
Peter Zijlstra49ee5762017-01-19 18:44:08 +01004098 const struct sched_class *class;
Ingo Molnardd41f592007-07-09 18:51:59 +02004099 struct task_struct *p;
4100
4101 /*
Peter Zijlstra0ba87bb2017-03-01 10:51:47 +01004102 * Optimization: we know that if all tasks are in the fair class we can
4103 * call that function directly, but only if the @prev task wasn't of a
4104 * higher scheduling class, because otherwise those loose the
4105 * opportunity to pull in more work from other CPUs.
Ingo Molnardd41f592007-07-09 18:51:59 +02004106 */
Kirill Tkhaiaa93cd52019-12-19 16:44:55 -05004107 if (likely(prev->sched_class <= &fair_sched_class &&
Peter Zijlstra0ba87bb2017-03-01 10:51:47 +01004108 rq->nr_running == rq->cfs.h_nr_running)) {
4109
Peter Zijlstra5d7d6052019-11-08 14:15:57 +01004110 p = pick_next_task_fair(rq, prev, rf);
Peter Zijlstra6ccdc842014-04-24 12:00:47 +02004111 if (unlikely(p == RETRY_TASK))
Peter Zijlstra67692432019-05-29 20:36:44 +00004112 goto restart;
Peter Zijlstra6ccdc842014-04-24 12:00:47 +02004113
Ingo Molnard1ccc662017-02-01 11:46:42 +01004114 /* Assumes fair_sched_class->next == idle_sched_class */
Peter Zijlstra5d7d6052019-11-08 14:15:57 +01004115 if (!p) {
Peter Zijlstraf488e102019-11-08 14:15:56 +01004116 put_prev_task(rq, prev);
Peter Zijlstra98c2f702019-11-08 14:15:58 +01004117 p = pick_next_task_idle(rq);
Peter Zijlstraf488e102019-11-08 14:15:56 +01004118 }
Peter Zijlstra6ccdc842014-04-24 12:00:47 +02004119
4120 return p;
Ingo Molnardd41f592007-07-09 18:51:59 +02004121 }
4122
Peter Zijlstra67692432019-05-29 20:36:44 +00004123restart:
Chen Yu457d1f42020-04-21 18:50:43 +08004124 put_prev_task_balance(rq, prev, rf);
Peter Zijlstra67692432019-05-29 20:36:44 +00004125
Peter Zijlstra34f971f2010-09-22 13:53:15 +02004126 for_each_class(class) {
Peter Zijlstra98c2f702019-11-08 14:15:58 +01004127 p = class->pick_next_task(rq);
Peter Zijlstra67692432019-05-29 20:36:44 +00004128 if (p)
Ingo Molnardd41f592007-07-09 18:51:59 +02004129 return p;
Ingo Molnardd41f592007-07-09 18:51:59 +02004130 }
Peter Zijlstra34f971f2010-09-22 13:53:15 +02004131
Ingo Molnard1ccc662017-02-01 11:46:42 +01004132 /* The idle class should always have a runnable task: */
4133 BUG();
Ingo Molnardd41f592007-07-09 18:51:59 +02004134}
4135
4136/*
Thomas Gleixnerc259e012011-06-22 19:47:00 +02004137 * __schedule() is the main scheduler function.
Pekka Enbergedde96e2012-08-04 11:49:47 +03004138 *
4139 * The main means of driving the scheduler and thus entering this function are:
4140 *
4141 * 1. Explicit blocking: mutex, semaphore, waitqueue, etc.
4142 *
4143 * 2. TIF_NEED_RESCHED flag is checked on interrupt and userspace return
4144 * paths. For example, see arch/x86/entry_64.S.
4145 *
4146 * To drive preemption between tasks, the scheduler sets the flag in timer
4147 * interrupt handler scheduler_tick().
4148 *
4149 * 3. Wakeups don't really cause entry into schedule(). They add a
4150 * task to the run-queue and that's it.
4151 *
4152 * Now, if the new task added to the run-queue preempts the current
4153 * task, then the wakeup sets TIF_NEED_RESCHED and schedule() gets
4154 * called on the nearest possible occasion:
4155 *
Thomas Gleixnerc1a280b2019-07-26 23:19:37 +02004156 * - If the kernel is preemptible (CONFIG_PREEMPTION=y):
Pekka Enbergedde96e2012-08-04 11:49:47 +03004157 *
4158 * - in syscall or exception context, at the next outmost
4159 * preempt_enable(). (this might be as soon as the wake_up()'s
4160 * spin_unlock()!)
4161 *
4162 * - in IRQ context, return from interrupt-handler to
4163 * preemptible context
4164 *
Thomas Gleixnerc1a280b2019-07-26 23:19:37 +02004165 * - If the kernel is not preemptible (CONFIG_PREEMPTION is not set)
Pekka Enbergedde96e2012-08-04 11:49:47 +03004166 * then at the next:
4167 *
4168 * - cond_resched() call
4169 * - explicit schedule() call
4170 * - return from syscall or exception to user-space
4171 * - return from interrupt-handler to user-space
Frederic Weisbeckerbfd9b2b2015-01-28 01:24:09 +01004172 *
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02004173 * WARNING: must be called with preemption disabled!
Ingo Molnardd41f592007-07-09 18:51:59 +02004174 */
Peter Zijlstra499d7952015-09-28 18:52:36 +02004175static void __sched notrace __schedule(bool preempt)
Ingo Molnardd41f592007-07-09 18:51:59 +02004176{
4177 struct task_struct *prev, *next;
Harvey Harrison67ca7bd2008-02-15 09:56:36 -08004178 unsigned long *switch_count;
Peter Zijlstradbfb0892020-07-03 12:40:33 +02004179 unsigned long prev_state;
Matt Flemingd8ac8972016-09-21 14:38:10 +01004180 struct rq_flags rf;
Ingo Molnardd41f592007-07-09 18:51:59 +02004181 struct rq *rq;
Peter Zijlstra31656512008-07-18 18:01:23 +02004182 int cpu;
Ingo Molnardd41f592007-07-09 18:51:59 +02004183
Ingo Molnardd41f592007-07-09 18:51:59 +02004184 cpu = smp_processor_id();
4185 rq = cpu_rq(cpu);
Ingo Molnardd41f592007-07-09 18:51:59 +02004186 prev = rq->curr;
Ingo Molnardd41f592007-07-09 18:51:59 +02004187
Daniel Vetter312364f32019-08-26 22:14:23 +02004188 schedule_debug(prev, preempt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189
Peter Zijlstra31656512008-07-18 18:01:23 +02004190 if (sched_feat(HRTICK))
Mike Galbraithf333fdc2008-05-12 21:20:55 +02004191 hrtick_clear(rq);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01004192
Paul E. McKenney46a5d162015-10-07 09:10:48 -07004193 local_irq_disable();
Paul E. McKenneybcbfdd02017-04-11 15:50:41 -07004194 rcu_note_context_switch(preempt);
Paul E. McKenney46a5d162015-10-07 09:10:48 -07004195
Peter Zijlstradbfb0892020-07-03 12:40:33 +02004196 /* See deactivate_task() below. */
4197 prev_state = prev->state;
4198
Oleg Nesterove0acd0a2013-08-12 18:14:00 +02004199 /*
4200 * Make sure that signal_pending_state()->signal_pending() below
4201 * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE)
Peter Zijlstradbfb0892020-07-03 12:40:33 +02004202 * done by the caller to avoid the race with signal_wake_up():
Mathieu Desnoyers306e0602018-01-29 15:20:12 -05004203 *
Peter Zijlstradbfb0892020-07-03 12:40:33 +02004204 * __set_current_state(@state) signal_wake_up()
4205 * schedule() set_tsk_thread_flag(p, TIF_SIGPENDING)
4206 * wake_up_state(p, state)
4207 * LOCK rq->lock LOCK p->pi_state
4208 * smp_mb__after_spinlock() smp_mb__after_spinlock()
4209 * if (signal_pending_state()) if (p->state & @state)
4210 *
4211 * Also, the membarrier system call requires a full memory barrier
Mathieu Desnoyers306e0602018-01-29 15:20:12 -05004212 * after coming from user-space, before storing to rq->curr.
Oleg Nesterove0acd0a2013-08-12 18:14:00 +02004213 */
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02004214 rq_lock(rq, &rf);
Peter Zijlstrad89e588c2016-09-05 11:37:53 +02004215 smp_mb__after_spinlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216
Ingo Molnard1ccc662017-02-01 11:46:42 +01004217 /* Promote REQ to ACT */
4218 rq->clock_update_flags <<= 1;
Peter Zijlstrabce4dc82017-02-21 14:40:35 +01004219 update_rq_clock(rq);
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +01004220
Oleg Nesterov246d86b2010-05-19 14:57:11 +02004221 switch_count = &prev->nivcsw;
Peter Zijlstradbfb0892020-07-03 12:40:33 +02004222 /*
4223 * We must re-load prev->state in case ttwu_remote() changed it
4224 * before we acquired rq->lock.
4225 */
4226 if (!preempt && prev_state && prev_state == prev->state) {
4227 if (signal_pending_state(prev_state, prev)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02004228 prev->state = TASK_RUNNING;
Tejun Heo21aa9af2010-06-08 21:40:37 +02004229 } else {
Peter Zijlstradbfb0892020-07-03 12:40:33 +02004230 prev->sched_contributes_to_load =
4231 (prev_state & TASK_UNINTERRUPTIBLE) &&
4232 !(prev_state & TASK_NOLOAD) &&
4233 !(prev->flags & PF_FROZEN);
4234
4235 if (prev->sched_contributes_to_load)
4236 rq->nr_uninterruptible++;
4237
4238 /*
4239 * __schedule() ttwu()
4240 * prev_state = prev->state; if (READ_ONCE(p->on_rq) && ...)
4241 * LOCK rq->lock goto out;
4242 * smp_mb__after_spinlock(); smp_acquire__after_ctrl_dep();
4243 * p->on_rq = 0; p->state = TASK_WAKING;
4244 *
4245 * After this, schedule() must not care about p->state any more.
4246 */
Peter Zijlstrabce4dc82017-02-21 14:40:35 +01004247 deactivate_task(rq, prev, DEQUEUE_SLEEP | DEQUEUE_NOCLOCK);
Peter Zijlstra2acca552011-04-05 17:23:50 +02004248
Tejun Heoe33a9bb2016-12-07 15:48:41 -05004249 if (prev->in_iowait) {
4250 atomic_inc(&rq->nr_iowait);
4251 delayacct_blkio_start();
4252 }
Tejun Heo21aa9af2010-06-08 21:40:37 +02004253 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004254 switch_count = &prev->nvcsw;
4255 }
4256
Matt Flemingd8ac8972016-09-21 14:38:10 +01004257 next = pick_next_task(rq, prev, &rf);
Mike Galbraithf26f9af2010-12-08 11:05:42 +01004258 clear_tsk_need_resched(prev);
Peter Zijlstraf27dde82013-08-14 14:55:31 +02004259 clear_preempt_need_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 if (likely(prev != next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 rq->nr_switches++;
Eric W. Biederman5311a982019-09-14 07:35:02 -05004263 /*
4264 * RCU users of rcu_dereference(rq->curr) may not see
4265 * changes to task_struct made by pick_next_task().
4266 */
4267 RCU_INIT_POINTER(rq->curr, next);
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -04004268 /*
4269 * The membarrier system call requires each architecture
4270 * to have a full memory barrier after updating
Mathieu Desnoyers306e0602018-01-29 15:20:12 -05004271 * rq->curr, before returning to user-space.
4272 *
4273 * Here are the schemes providing that barrier on the
4274 * various architectures:
4275 * - mm ? switch_mm() : mmdrop() for x86, s390, sparc, PowerPC.
4276 * switch_mm() rely on membarrier_arch_switch_mm() on PowerPC.
4277 * - finish_lock_switch() for weakly-ordered
4278 * architectures where spin_unlock is a full barrier,
4279 * - switch_to() for arm64 (weakly-ordered, spin_unlock
4280 * is a RELEASE barrier),
Mathieu Desnoyers22e4ebb2017-07-28 16:40:40 -04004281 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 ++*switch_count;
4283
Johannes Weinerb05e75d2020-03-16 15:13:31 -04004284 psi_sched_switch(prev, next, !task_on_rq_queued(prev));
4285
Peter Zijlstrac73464b2015-09-28 18:06:56 +02004286 trace_sched_switch(preempt, prev, next);
Ingo Molnard1ccc662017-02-01 11:46:42 +01004287
4288 /* Also unlocks the rq: */
4289 rq = context_switch(rq, prev, next, &rf);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02004290 } else {
Matt Flemingcb42c9a2016-09-21 14:38:13 +01004291 rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02004292 rq_unlock_irq(rq, &rf);
Peter Zijlstracbce1a62015-06-11 14:46:54 +02004293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02004295 balance_callback(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296}
Thomas Gleixnerc259e012011-06-22 19:47:00 +02004297
Peter Zijlstra9af65282016-09-13 18:37:29 +02004298void __noreturn do_task_dead(void)
4299{
Ingo Molnard1ccc662017-02-01 11:46:42 +01004300 /* Causes final put_task_struct in finish_task_switch(): */
Peter Zijlstrab5bf9a92018-04-30 14:51:01 +02004301 set_special_state(TASK_DEAD);
Ingo Molnard1ccc662017-02-01 11:46:42 +01004302
4303 /* Tell freezer to ignore us: */
4304 current->flags |= PF_NOFREEZE;
4305
Peter Zijlstra9af65282016-09-13 18:37:29 +02004306 __schedule(false);
4307 BUG();
Ingo Molnard1ccc662017-02-01 11:46:42 +01004308
4309 /* Avoid "noreturn function does return" - but don't continue if BUG() is a NOP: */
Peter Zijlstra9af65282016-09-13 18:37:29 +02004310 for (;;)
Ingo Molnard1ccc662017-02-01 11:46:42 +01004311 cpu_relax();
Peter Zijlstra9af65282016-09-13 18:37:29 +02004312}
4313
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02004314static inline void sched_submit_work(struct task_struct *tsk)
4315{
Sebastian Andrzej Siewiorb0fdc012019-08-16 18:06:26 +02004316 if (!tsk->state)
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02004317 return;
Thomas Gleixner6d25be52019-03-13 17:55:48 +01004318
4319 /*
4320 * If a worker went to sleep, notify and ask workqueue whether
4321 * it wants to wake up a task to maintain concurrency.
4322 * As this function is called inside the schedule() context,
4323 * we disable preemption to avoid it calling schedule() again
Sebastian Andrzej Siewior62849a92020-03-28 00:29:59 +01004324 * in the possible wakeup of a kworker and because wq_worker_sleeping()
4325 * requires it.
Thomas Gleixner6d25be52019-03-13 17:55:48 +01004326 */
Jens Axboe771b53d02019-10-22 10:25:58 -06004327 if (tsk->flags & (PF_WQ_WORKER | PF_IO_WORKER)) {
Thomas Gleixner6d25be52019-03-13 17:55:48 +01004328 preempt_disable();
Jens Axboe771b53d02019-10-22 10:25:58 -06004329 if (tsk->flags & PF_WQ_WORKER)
4330 wq_worker_sleeping(tsk);
4331 else
4332 io_wq_worker_sleeping(tsk);
Thomas Gleixner6d25be52019-03-13 17:55:48 +01004333 preempt_enable_no_resched();
4334 }
4335
Sebastian Andrzej Siewiorb0fdc012019-08-16 18:06:26 +02004336 if (tsk_is_pi_blocked(tsk))
4337 return;
4338
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02004339 /*
4340 * If we are going to sleep and we have plugged IO queued,
4341 * make sure to submit it to avoid deadlocks.
4342 */
4343 if (blk_needs_flush_plug(tsk))
4344 blk_schedule_flush_plug(tsk);
4345}
4346
Thomas Gleixner6d25be52019-03-13 17:55:48 +01004347static void sched_update_worker(struct task_struct *tsk)
4348{
Jens Axboe771b53d02019-10-22 10:25:58 -06004349 if (tsk->flags & (PF_WQ_WORKER | PF_IO_WORKER)) {
4350 if (tsk->flags & PF_WQ_WORKER)
4351 wq_worker_running(tsk);
4352 else
4353 io_wq_worker_running(tsk);
4354 }
Thomas Gleixner6d25be52019-03-13 17:55:48 +01004355}
4356
Andi Kleen722a9f92014-05-02 00:44:38 +02004357asmlinkage __visible void __sched schedule(void)
Thomas Gleixnerc259e012011-06-22 19:47:00 +02004358{
Thomas Gleixner9c40cef22011-06-22 19:47:01 +02004359 struct task_struct *tsk = current;
4360
4361 sched_submit_work(tsk);
Frederic Weisbeckerbfd9b2b2015-01-28 01:24:09 +01004362 do {
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02004363 preempt_disable();
Peter Zijlstrafc13aeb2015-09-28 18:05:34 +02004364 __schedule(false);
Frederic Weisbeckerb30f0e32015-05-12 16:41:49 +02004365 sched_preempt_enable_no_resched();
Frederic Weisbeckerbfd9b2b2015-01-28 01:24:09 +01004366 } while (need_resched());
Thomas Gleixner6d25be52019-03-13 17:55:48 +01004367 sched_update_worker(tsk);
Thomas Gleixnerc259e012011-06-22 19:47:00 +02004368}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369EXPORT_SYMBOL(schedule);
4370
Steven Rostedt (VMware)8663eff2017-04-14 08:48:09 -04004371/*
4372 * synchronize_rcu_tasks() makes sure that no task is stuck in preempted
4373 * state (have scheduled out non-voluntarily) by making sure that all
4374 * tasks have either left the run queue or have gone into user space.
4375 * As idle tasks do not do either, they must not ever be preempted
4376 * (schedule out non-voluntarily).
4377 *
4378 * schedule_idle() is similar to schedule_preempt_disable() except that it
4379 * never enables preemption because it does not call sched_submit_work().
4380 */
4381void __sched schedule_idle(void)
4382{
4383 /*
4384 * As this skips calling sched_submit_work(), which the idle task does
4385 * regardless because that function is a nop when the task is in a
4386 * TASK_RUNNING state, make sure this isn't used someplace that the
4387 * current task can be in any other state. Note, idle is always in the
4388 * TASK_RUNNING state.
4389 */
4390 WARN_ON_ONCE(current->state);
4391 do {
4392 __schedule(false);
4393 } while (need_resched());
4394}
4395
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +01004396#ifdef CONFIG_CONTEXT_TRACKING
Andi Kleen722a9f92014-05-02 00:44:38 +02004397asmlinkage __visible void __sched schedule_user(void)
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02004398{
4399 /*
4400 * If we come here after a random call to set_need_resched(),
4401 * or we have been woken up remotely but the IPI has not yet arrived,
4402 * we haven't yet exited the RCU idle mode. Do it here manually until
4403 * we find a better solution.
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08004404 *
4405 * NB: There are buggy callers of this function. Ideally we
Frederic Weisbeckerc467ea72015-03-04 18:06:33 +01004406 * should warn if prev_state != CONTEXT_USER, but that will trigger
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08004407 * too frequently to make sense yet.
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02004408 */
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08004409 enum ctx_state prev_state = exception_enter();
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02004410 schedule();
Andy Lutomirski7cc78f82014-12-03 15:37:08 -08004411 exception_exit(prev_state);
Frederic Weisbecker20ab65e32012-07-11 20:26:37 +02004412}
4413#endif
4414
Thomas Gleixnerc5491ea2011-03-21 12:09:35 +01004415/**
4416 * schedule_preempt_disabled - called with preemption disabled
4417 *
4418 * Returns with preemption disabled. Note: preempt_count must be 1
4419 */
4420void __sched schedule_preempt_disabled(void)
4421{
Thomas Gleixnerba74c142011-03-21 13:32:17 +01004422 sched_preempt_enable_no_resched();
Thomas Gleixnerc5491ea2011-03-21 12:09:35 +01004423 schedule();
4424 preempt_disable();
4425}
4426
Frederic Weisbecker06b1f802015-02-16 19:20:07 +01004427static void __sched notrace preempt_schedule_common(void)
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01004428{
4429 do {
Steven Rostedt47252cf2016-03-21 11:23:39 -04004430 /*
4431 * Because the function tracer can trace preempt_count_sub()
4432 * and it also uses preempt_enable/disable_notrace(), if
4433 * NEED_RESCHED is set, the preempt_enable_notrace() called
4434 * by the function tracer will call this function again and
4435 * cause infinite recursion.
4436 *
4437 * Preemption must be disabled here before the function
4438 * tracer can trace. Break up preempt_disable() into two
4439 * calls. One to disable preemption without fear of being
4440 * traced. The other to still record the preemption latency,
4441 * which can also be traced by the function tracer.
4442 */
Peter Zijlstra499d7952015-09-28 18:52:36 +02004443 preempt_disable_notrace();
Steven Rostedt47252cf2016-03-21 11:23:39 -04004444 preempt_latency_start(1);
Peter Zijlstrafc13aeb2015-09-28 18:05:34 +02004445 __schedule(true);
Steven Rostedt47252cf2016-03-21 11:23:39 -04004446 preempt_latency_stop(1);
Peter Zijlstra499d7952015-09-28 18:52:36 +02004447 preempt_enable_no_resched_notrace();
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01004448
4449 /*
4450 * Check again in case we missed a preemption opportunity
4451 * between schedule and now.
4452 */
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01004453 } while (need_resched());
4454}
4455
Thomas Gleixnerc1a280b2019-07-26 23:19:37 +02004456#ifdef CONFIG_PREEMPTION
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457/*
Valentin Schneidera49b4f42019-09-23 15:36:12 +01004458 * This is the entry point to schedule() from in-kernel preemption
4459 * off of preempt_enable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 */
Andi Kleen722a9f92014-05-02 00:44:38 +02004461asmlinkage __visible void __sched notrace preempt_schedule(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 /*
4464 * If there is a non-zero preempt_count or interrupts are disabled,
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01004465 * we do not want to preempt the current task. Just return..
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 */
Frederic Weisbeckerfbb00b52013-06-19 23:56:22 +02004467 if (likely(!preemptible()))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 return;
4469
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01004470 preempt_schedule_common();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471}
Masami Hiramatsu376e2422014-04-17 17:17:05 +09004472NOKPROBE_SYMBOL(preempt_schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473EXPORT_SYMBOL(preempt_schedule);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02004474
Oleg Nesterov009f60e2014-10-05 22:23:22 +02004475/**
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +02004476 * preempt_schedule_notrace - preempt_schedule called by tracing
Oleg Nesterov009f60e2014-10-05 22:23:22 +02004477 *
4478 * The tracing infrastructure uses preempt_enable_notrace to prevent
4479 * recursion and tracing preempt enabling caused by the tracing
4480 * infrastructure itself. But as tracing can happen in areas coming
4481 * from userspace or just about to enter userspace, a preempt enable
4482 * can occur before user_exit() is called. This will cause the scheduler
4483 * to be called when the system is still in usermode.
4484 *
4485 * To prevent this, the preempt_enable_notrace will use this function
4486 * instead of preempt_schedule() to exit user context if needed before
4487 * calling the scheduler.
4488 */
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +02004489asmlinkage __visible void __sched notrace preempt_schedule_notrace(void)
Oleg Nesterov009f60e2014-10-05 22:23:22 +02004490{
4491 enum ctx_state prev_ctx;
4492
4493 if (likely(!preemptible()))
4494 return;
4495
4496 do {
Steven Rostedt47252cf2016-03-21 11:23:39 -04004497 /*
4498 * Because the function tracer can trace preempt_count_sub()
4499 * and it also uses preempt_enable/disable_notrace(), if
4500 * NEED_RESCHED is set, the preempt_enable_notrace() called
4501 * by the function tracer will call this function again and
4502 * cause infinite recursion.
4503 *
4504 * Preemption must be disabled here before the function
4505 * tracer can trace. Break up preempt_disable() into two
4506 * calls. One to disable preemption without fear of being
4507 * traced. The other to still record the preemption latency,
4508 * which can also be traced by the function tracer.
4509 */
Peter Zijlstra3d8f74d2015-09-28 18:09:19 +02004510 preempt_disable_notrace();
Steven Rostedt47252cf2016-03-21 11:23:39 -04004511 preempt_latency_start(1);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02004512 /*
4513 * Needs preempt disabled in case user_exit() is traced
4514 * and the tracer calls preempt_enable_notrace() causing
4515 * an infinite recursion.
4516 */
4517 prev_ctx = exception_enter();
Peter Zijlstrafc13aeb2015-09-28 18:05:34 +02004518 __schedule(true);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02004519 exception_exit(prev_ctx);
4520
Steven Rostedt47252cf2016-03-21 11:23:39 -04004521 preempt_latency_stop(1);
Peter Zijlstra3d8f74d2015-09-28 18:09:19 +02004522 preempt_enable_no_resched_notrace();
Oleg Nesterov009f60e2014-10-05 22:23:22 +02004523 } while (need_resched());
4524}
Frederic Weisbecker4eaca0a2015-06-04 17:39:08 +02004525EXPORT_SYMBOL_GPL(preempt_schedule_notrace);
Oleg Nesterov009f60e2014-10-05 22:23:22 +02004526
Thomas Gleixnerc1a280b2019-07-26 23:19:37 +02004527#endif /* CONFIG_PREEMPTION */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528
4529/*
Valentin Schneidera49b4f42019-09-23 15:36:12 +01004530 * This is the entry point to schedule() from kernel preemption
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 * off of irq context.
4532 * Note, that this is called and return with irqs disabled. This will
4533 * protect us against recursive calling from irq.
4534 */
Andi Kleen722a9f92014-05-02 00:44:38 +02004535asmlinkage __visible void __sched preempt_schedule_irq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536{
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01004537 enum ctx_state prev_state;
Ingo Molnar6478d882008-01-25 21:08:33 +01004538
Andreas Mohr2ed6e342006-07-10 04:43:52 -07004539 /* Catch callers which need to be fixed */
Peter Zijlstraf27dde82013-08-14 14:55:31 +02004540 BUG_ON(preempt_count() || !irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01004542 prev_state = exception_enter();
4543
Andi Kleen3a5c3592007-10-15 17:00:14 +02004544 do {
Peter Zijlstra3d8f74d2015-09-28 18:09:19 +02004545 preempt_disable();
Andi Kleen3a5c3592007-10-15 17:00:14 +02004546 local_irq_enable();
Peter Zijlstrafc13aeb2015-09-28 18:05:34 +02004547 __schedule(true);
Andi Kleen3a5c3592007-10-15 17:00:14 +02004548 local_irq_disable();
Peter Zijlstra3d8f74d2015-09-28 18:09:19 +02004549 sched_preempt_enable_no_resched();
Lai Jiangshan5ed0cec2009-03-06 19:40:20 +08004550 } while (need_resched());
Frederic Weisbeckerb22366c2013-02-24 12:59:30 +01004551
4552 exception_exit(prev_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553}
4554
Ingo Molnarac6424b2017-06-20 12:06:13 +02004555int default_wake_function(wait_queue_entry_t *curr, unsigned mode, int wake_flags,
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07004556 void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557{
Peter Zijlstra63859d42009-09-15 19:14:42 +02004558 return try_to_wake_up(curr->private, mode, wake_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560EXPORT_SYMBOL(default_wake_function);
4561
Ingo Molnarb29739f2006-06-27 02:54:51 -07004562#ifdef CONFIG_RT_MUTEXES
4563
Peter Zijlstraacd58622017-03-23 15:56:11 +01004564static inline int __rt_effective_prio(struct task_struct *pi_task, int prio)
4565{
4566 if (pi_task)
4567 prio = min(prio, pi_task->prio);
4568
4569 return prio;
4570}
4571
4572static inline int rt_effective_prio(struct task_struct *p, int prio)
4573{
4574 struct task_struct *pi_task = rt_mutex_get_top_task(p);
4575
4576 return __rt_effective_prio(pi_task, prio);
4577}
4578
Ingo Molnarb29739f2006-06-27 02:54:51 -07004579/*
4580 * rt_mutex_setprio - set the current priority of a task
Peter Zijlstraacd58622017-03-23 15:56:11 +01004581 * @p: task to boost
4582 * @pi_task: donor task
Ingo Molnarb29739f2006-06-27 02:54:51 -07004583 *
4584 * This function changes the 'effective' priority of a task. It does
4585 * not touch ->normal_prio like __setscheduler().
4586 *
Thomas Gleixnerc365c292014-02-07 20:58:42 +01004587 * Used by the rt_mutex code to implement priority inheritance
4588 * logic. Call site only calls if the priority of the task changed.
Ingo Molnarb29739f2006-06-27 02:54:51 -07004589 */
Peter Zijlstraacd58622017-03-23 15:56:11 +01004590void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task)
Ingo Molnarb29739f2006-06-27 02:54:51 -07004591{
Peter Zijlstraacd58622017-03-23 15:56:11 +01004592 int prio, oldprio, queued, running, queue_flag =
Peter Zijlstra7a57f322017-02-21 14:47:02 +01004593 DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004594 const struct sched_class *prev_class;
Peter Zijlstraeb580752015-07-31 21:28:18 +02004595 struct rq_flags rf;
4596 struct rq *rq;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004597
Peter Zijlstraacd58622017-03-23 15:56:11 +01004598 /* XXX used to be waiter->prio, not waiter->task->prio */
4599 prio = __rt_effective_prio(pi_task, p->normal_prio);
4600
4601 /*
4602 * If nothing changed; bail early.
4603 */
4604 if (p->pi_top_task == pi_task && prio == p->prio && !dl_prio(prio))
4605 return;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004606
Peter Zijlstraeb580752015-07-31 21:28:18 +02004607 rq = __task_rq_lock(p, &rf);
Peter Zijlstra80f5c1b2016-10-03 16:28:37 +02004608 update_rq_clock(rq);
Peter Zijlstraacd58622017-03-23 15:56:11 +01004609 /*
4610 * Set under pi_lock && rq->lock, such that the value can be used under
4611 * either lock.
4612 *
4613 * Note that there is loads of tricky to make this pointer cache work
4614 * right. rt_mutex_slowunlock()+rt_mutex_postunlock() work together to
4615 * ensure a task is de-boosted (pi_task is set to NULL) before the
4616 * task is allowed to run again (and can exit). This ensures the pointer
4617 * points to a blocked task -- which guaratees the task is present.
4618 */
4619 p->pi_top_task = pi_task;
4620
4621 /*
4622 * For FIFO/RR we only need to set prio, if that matches we're done.
4623 */
4624 if (prio == p->prio && !dl_prio(prio))
4625 goto out_unlock;
Ingo Molnarb29739f2006-06-27 02:54:51 -07004626
Thomas Gleixner1c4dd992011-06-06 20:07:38 +02004627 /*
4628 * Idle task boosting is a nono in general. There is one
4629 * exception, when PREEMPT_RT and NOHZ is active:
4630 *
4631 * The idle task calls get_next_timer_interrupt() and holds
4632 * the timer wheel base->lock on the CPU and another CPU wants
4633 * to access the timer (probably to cancel it). We can safely
4634 * ignore the boosting request, as the idle CPU runs this code
4635 * with interrupts disabled and will complete the lock
4636 * protected section without being interrupted. So there is no
4637 * real need to boost.
4638 */
4639 if (unlikely(p == rq->idle)) {
4640 WARN_ON(p != rq->curr);
4641 WARN_ON(p->pi_blocked_on);
4642 goto out_unlock;
4643 }
4644
Peter Zijlstrab91473f2017-03-23 15:56:12 +01004645 trace_sched_pi_setprio(p, pi_task);
Andrew Mortond5f9f942007-05-08 20:27:06 -07004646 oldprio = p->prio;
Peter Zijlstraff77e462016-01-18 15:27:07 +01004647
4648 if (oldprio == prio)
4649 queue_flag &= ~DEQUEUE_MOVE;
4650
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004651 prev_class = p->sched_class;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004652 queued = task_on_rq_queued(p);
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01004653 running = task_current(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004654 if (queued)
Peter Zijlstraff77e462016-01-18 15:27:07 +01004655 dequeue_task(rq, p, queue_flag);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07004656 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04004657 put_prev_task(rq, p);
Ingo Molnardd41f592007-07-09 18:51:59 +02004658
Dario Faggioli2d3d8912013-11-07 14:43:44 +01004659 /*
4660 * Boosting condition are:
4661 * 1. -rt task is running and holds mutex A
4662 * --> -dl task blocks on mutex A
4663 *
4664 * 2. -dl task is running and holds mutex A
4665 * --> -dl task blocks on mutex A and could preempt the
4666 * running task
4667 */
4668 if (dl_prio(prio)) {
Oleg Nesterov466af292014-06-06 18:52:06 +02004669 if (!dl_prio(p->normal_prio) ||
Juri Lelli740797c2018-11-19 16:32:01 +01004670 (pi_task && dl_prio(pi_task->prio) &&
4671 dl_entity_preempt(&pi_task->dl, &p->dl))) {
Dario Faggioli2d3d8912013-11-07 14:43:44 +01004672 p->dl.dl_boosted = 1;
Peter Zijlstraff77e462016-01-18 15:27:07 +01004673 queue_flag |= ENQUEUE_REPLENISH;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01004674 } else
4675 p->dl.dl_boosted = 0;
Dario Faggioliaab03e02013-11-28 11:14:43 +01004676 p->sched_class = &dl_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01004677 } else if (rt_prio(prio)) {
4678 if (dl_prio(oldprio))
4679 p->dl.dl_boosted = 0;
4680 if (oldprio < prio)
Peter Zijlstraff77e462016-01-18 15:27:07 +01004681 queue_flag |= ENQUEUE_HEAD;
Ingo Molnardd41f592007-07-09 18:51:59 +02004682 p->sched_class = &rt_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01004683 } else {
4684 if (dl_prio(oldprio))
4685 p->dl.dl_boosted = 0;
Brian Silverman746db942015-02-18 16:23:56 -08004686 if (rt_prio(oldprio))
4687 p->rt.timeout = 0;
Ingo Molnardd41f592007-07-09 18:51:59 +02004688 p->sched_class = &fair_sched_class;
Dario Faggioli2d3d8912013-11-07 14:43:44 +01004689 }
Ingo Molnardd41f592007-07-09 18:51:59 +02004690
Ingo Molnarb29739f2006-06-27 02:54:51 -07004691 p->prio = prio;
4692
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004693 if (queued)
Peter Zijlstraff77e462016-01-18 15:27:07 +01004694 enqueue_task(rq, p, queue_flag);
Vincent Guittota399d232016-09-12 09:47:52 +02004695 if (running)
Peter Zijlstra03b7fad2019-05-29 20:36:41 +00004696 set_next_task(rq, p);
Steven Rostedtcb469842008-01-25 21:08:22 +01004697
Peter Zijlstrada7a7352011-01-17 17:03:27 +01004698 check_class_changed(rq, p, prev_class, oldprio);
Thomas Gleixner1c4dd992011-06-06 20:07:38 +02004699out_unlock:
Ingo Molnard1ccc662017-02-01 11:46:42 +01004700 /* Avoid rq from going away on us: */
4701 preempt_disable();
Peter Zijlstraeb580752015-07-31 21:28:18 +02004702 __task_rq_unlock(rq, &rf);
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02004703
4704 balance_callback(rq);
4705 preempt_enable();
Ingo Molnarb29739f2006-06-27 02:54:51 -07004706}
Peter Zijlstraacd58622017-03-23 15:56:11 +01004707#else
4708static inline int rt_effective_prio(struct task_struct *p, int prio)
4709{
4710 return prio;
4711}
Ingo Molnarb29739f2006-06-27 02:54:51 -07004712#endif
Dario Faggiolid50dde52013-11-07 14:43:36 +01004713
Ingo Molnar36c8b582006-07-03 00:25:41 -07004714void set_user_nice(struct task_struct *p, long nice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715{
Peter Zijlstra49bd21e2016-09-20 22:06:01 +02004716 bool queued, running;
Qian Cai53a23362019-12-19 09:03:14 -05004717 int old_prio;
Peter Zijlstraeb580752015-07-31 21:28:18 +02004718 struct rq_flags rf;
Ingo Molnar70b97a72006-07-03 00:25:42 -07004719 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
Dongsheng Yang75e45d52014-02-11 15:34:50 +08004721 if (task_nice(p) == nice || nice < MIN_NICE || nice > MAX_NICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 return;
4723 /*
4724 * We have to be careful, if called from sys_setpriority(),
4725 * the task might be in the middle of scheduling on another CPU.
4726 */
Peter Zijlstraeb580752015-07-31 21:28:18 +02004727 rq = task_rq_lock(p, &rf);
Peter Zijlstra2fb8d362016-10-03 16:44:25 +02004728 update_rq_clock(rq);
4729
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 /*
4731 * The RT priorities are set via sched_setscheduler(), but we still
4732 * allow the 'normal' nice value to be set - but as expected
4733 * it wont have any effect on scheduling until the task is
Dario Faggioliaab03e02013-11-28 11:14:43 +01004734 * SCHED_DEADLINE, SCHED_FIFO or SCHED_RR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 */
Dario Faggioliaab03e02013-11-28 11:14:43 +01004736 if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 p->static_prio = NICE_TO_PRIO(nice);
4738 goto out_unlock;
4739 }
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004740 queued = task_on_rq_queued(p);
Peter Zijlstra49bd21e2016-09-20 22:06:01 +02004741 running = task_current(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004742 if (queued)
Peter Zijlstra7a57f322017-02-21 14:47:02 +01004743 dequeue_task(rq, p, DEQUEUE_SAVE | DEQUEUE_NOCLOCK);
Peter Zijlstra49bd21e2016-09-20 22:06:01 +02004744 if (running)
4745 put_prev_task(rq, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 p->static_prio = NICE_TO_PRIO(nice);
Vincent Guittot90593932017-05-17 11:50:45 +02004748 set_load_weight(p, true);
Ingo Molnarb29739f2006-06-27 02:54:51 -07004749 old_prio = p->prio;
4750 p->prio = effective_prio(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751
Frederic Weisbecker5443a0b2019-12-03 17:01:06 +01004752 if (queued)
Peter Zijlstra7134b3e2017-02-21 14:23:38 +01004753 enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK);
Peter Zijlstra49bd21e2016-09-20 22:06:01 +02004754 if (running)
Peter Zijlstra03b7fad2019-05-29 20:36:41 +00004755 set_next_task(rq, p);
Frederic Weisbecker5443a0b2019-12-03 17:01:06 +01004756
4757 /*
4758 * If the task increased its priority or is running and
4759 * lowered its priority, then reschedule its CPU:
4760 */
4761 p->sched_class->prio_changed(rq, p, old_prio);
4762
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763out_unlock:
Peter Zijlstraeb580752015-07-31 21:28:18 +02004764 task_rq_unlock(rq, p, &rf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766EXPORT_SYMBOL(set_user_nice);
4767
Matt Mackalle43379f2005-05-01 08:59:00 -07004768/*
4769 * can_nice - check if a task can reduce its nice value
4770 * @p: task
4771 * @nice: nice value
4772 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004773int can_nice(const struct task_struct *p, const int nice)
Matt Mackalle43379f2005-05-01 08:59:00 -07004774{
Ingo Molnard1ccc662017-02-01 11:46:42 +01004775 /* Convert nice value [19,-20] to rlimit style value [1,40]: */
Dongsheng Yang7aa2c012014-05-08 18:33:49 +09004776 int nice_rlim = nice_to_rlimit(nice);
Ingo Molnar48f24c42006-07-03 00:25:40 -07004777
Jiri Slaby78d7d402010-03-05 13:42:54 -08004778 return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
Matt Mackalle43379f2005-05-01 08:59:00 -07004779 capable(CAP_SYS_NICE));
4780}
4781
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782#ifdef __ARCH_WANT_SYS_NICE
4783
4784/*
4785 * sys_nice - change the priority of the current process.
4786 * @increment: priority increment
4787 *
4788 * sys_setpriority is a more generic, but much slower function that
4789 * does similar things.
4790 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01004791SYSCALL_DEFINE1(nice, int, increment)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792{
Ingo Molnar48f24c42006-07-03 00:25:40 -07004793 long nice, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794
4795 /*
4796 * Setpriority might change our priority at the same moment.
4797 * We don't have to worry. Conceptually one call occurs first
4798 * and we have a single winner.
4799 */
Dongsheng Yanga9467fa2014-05-08 18:35:15 +09004800 increment = clamp(increment, -NICE_WIDTH, NICE_WIDTH);
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05004801 nice = task_nice(current) + increment;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802
Dongsheng Yanga9467fa2014-05-08 18:35:15 +09004803 nice = clamp_val(nice, MIN_NICE, MAX_NICE);
Matt Mackalle43379f2005-05-01 08:59:00 -07004804 if (increment < 0 && !can_nice(current, nice))
4805 return -EPERM;
4806
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 retval = security_task_setnice(current, nice);
4808 if (retval)
4809 return retval;
4810
4811 set_user_nice(current, nice);
4812 return 0;
4813}
4814
4815#endif
4816
4817/**
4818 * task_prio - return the priority value of a given task.
4819 * @p: the task in question.
4820 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02004821 * Return: The priority value as seen by users in /proc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 * RT tasks are offset by -200. Normal tasks are centered
4823 * around 0, value goes from -16 to +15.
4824 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004825int task_prio(const struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826{
4827 return p->prio - MAX_RT_PRIO;
4828}
4829
4830/**
Ingo Molnard1ccc662017-02-01 11:46:42 +01004831 * idle_cpu - is a given CPU idle currently?
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 * @cpu: the processor in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004833 *
4834 * Return: 1 if the CPU is currently idle. 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 */
4836int idle_cpu(int cpu)
4837{
Thomas Gleixner908a3282011-09-15 15:32:06 +02004838 struct rq *rq = cpu_rq(cpu);
4839
4840 if (rq->curr != rq->idle)
4841 return 0;
4842
4843 if (rq->nr_running)
4844 return 0;
4845
4846#ifdef CONFIG_SMP
Peter Zijlstra126c2092020-05-26 18:11:03 +02004847 if (rq->ttwu_pending)
Thomas Gleixner908a3282011-09-15 15:32:06 +02004848 return 0;
4849#endif
4850
4851 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852}
4853
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854/**
Rohit Jain943d3552018-05-09 09:39:48 -07004855 * available_idle_cpu - is a given CPU idle for enqueuing work.
4856 * @cpu: the CPU in question.
4857 *
4858 * Return: 1 if the CPU is currently idle. 0 otherwise.
4859 */
4860int available_idle_cpu(int cpu)
4861{
4862 if (!idle_cpu(cpu))
4863 return 0;
4864
Rohit Jain247f2f62018-05-02 13:52:10 -07004865 if (vcpu_is_preempted(cpu))
4866 return 0;
4867
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 return 1;
4869}
4870
4871/**
Ingo Molnard1ccc662017-02-01 11:46:42 +01004872 * idle_task - return the idle task for a given CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 * @cpu: the processor in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004874 *
Ingo Molnard1ccc662017-02-01 11:46:42 +01004875 * Return: The idle task for the CPU @cpu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07004877struct task_struct *idle_task(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878{
4879 return cpu_rq(cpu)->idle;
4880}
4881
4882/**
4883 * find_process_by_pid - find a process with a matching PID value.
4884 * @pid: the pid in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02004885 *
4886 * The task of @pid, if found. %NULL otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02004888static struct task_struct *find_process_by_pid(pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889{
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07004890 return pid ? find_task_by_vpid(pid) : current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891}
4892
Dario Faggioliaab03e02013-11-28 11:14:43 +01004893/*
Steven Rostedtc13db6b2014-07-23 11:28:26 -04004894 * sched_setparam() passes in -1 for its policy, to let the functions
4895 * it calls know not to change it.
4896 */
4897#define SETPARAM_POLICY -1
4898
Thomas Gleixnerc365c292014-02-07 20:58:42 +01004899static void __setscheduler_params(struct task_struct *p,
4900 const struct sched_attr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901{
Dario Faggiolid50dde52013-11-07 14:43:36 +01004902 int policy = attr->sched_policy;
4903
Steven Rostedtc13db6b2014-07-23 11:28:26 -04004904 if (policy == SETPARAM_POLICY)
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01004905 policy = p->policy;
4906
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 p->policy = policy;
Dario Faggiolid50dde52013-11-07 14:43:36 +01004908
Dario Faggioliaab03e02013-11-28 11:14:43 +01004909 if (dl_policy(policy))
4910 __setparam_dl(p, attr);
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01004911 else if (fair_policy(policy))
Dario Faggiolid50dde52013-11-07 14:43:36 +01004912 p->static_prio = NICE_TO_PRIO(attr->sched_nice);
4913
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01004914 /*
4915 * __sched_setscheduler() ensures attr->sched_priority == 0 when
4916 * !rt_policy. Always setting this ensures that things like
4917 * getparam()/getattr() don't report silly values for !rt tasks.
4918 */
4919 p->rt_priority = attr->sched_priority;
Steven Rostedt383afd02014-03-11 19:24:20 -04004920 p->normal_prio = normal_prio(p);
Vincent Guittot90593932017-05-17 11:50:45 +02004921 set_load_weight(p, true);
Thomas Gleixnerc365c292014-02-07 20:58:42 +01004922}
Peter Zijlstra39fd8fd2014-01-15 16:33:20 +01004923
Thomas Gleixnerc365c292014-02-07 20:58:42 +01004924/* Actually do priority change: must hold pi & rq lock. */
4925static void __setscheduler(struct rq *rq, struct task_struct *p,
Thomas Gleixner0782e632015-05-05 19:49:49 +02004926 const struct sched_attr *attr, bool keep_boost)
Thomas Gleixnerc365c292014-02-07 20:58:42 +01004927{
Patrick Bellasia509a7c2019-06-21 09:42:07 +01004928 /*
4929 * If params can't change scheduling class changes aren't allowed
4930 * either.
4931 */
4932 if (attr->sched_flags & SCHED_FLAG_KEEP_PARAMS)
4933 return;
4934
Thomas Gleixnerc365c292014-02-07 20:58:42 +01004935 __setscheduler_params(p, attr);
Dario Faggiolid50dde52013-11-07 14:43:36 +01004936
Steven Rostedt383afd02014-03-11 19:24:20 -04004937 /*
Thomas Gleixner0782e632015-05-05 19:49:49 +02004938 * Keep a potential priority boosting if called from
4939 * sched_setscheduler().
Steven Rostedt383afd02014-03-11 19:24:20 -04004940 */
Peter Zijlstraacd58622017-03-23 15:56:11 +01004941 p->prio = normal_prio(p);
Thomas Gleixner0782e632015-05-05 19:49:49 +02004942 if (keep_boost)
Peter Zijlstraacd58622017-03-23 15:56:11 +01004943 p->prio = rt_effective_prio(p, p->prio);
Steven Rostedt383afd02014-03-11 19:24:20 -04004944
Dario Faggioliaab03e02013-11-28 11:14:43 +01004945 if (dl_prio(p->prio))
4946 p->sched_class = &dl_sched_class;
4947 else if (rt_prio(p->prio))
Peter Zijlstraffd44db2009-11-10 20:12:01 +01004948 p->sched_class = &rt_sched_class;
4949 else
4950 p->sched_class = &fair_sched_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951}
Dario Faggioliaab03e02013-11-28 11:14:43 +01004952
David Howellsc69e8d92008-11-14 10:39:19 +11004953/*
Ingo Molnard1ccc662017-02-01 11:46:42 +01004954 * Check the target process has a UID that matches the current process's:
David Howellsc69e8d92008-11-14 10:39:19 +11004955 */
4956static bool check_same_owner(struct task_struct *p)
4957{
4958 const struct cred *cred = current_cred(), *pcred;
4959 bool match;
4960
4961 rcu_read_lock();
4962 pcred = __task_cred(p);
Eric W. Biederman9c806aa2012-02-02 18:54:02 -08004963 match = (uid_eq(cred->euid, pcred->euid) ||
4964 uid_eq(cred->euid, pcred->uid));
David Howellsc69e8d92008-11-14 10:39:19 +11004965 rcu_read_unlock();
4966 return match;
4967}
4968
Dario Faggiolid50dde52013-11-07 14:43:36 +01004969static int __sched_setscheduler(struct task_struct *p,
4970 const struct sched_attr *attr,
Peter Zijlstradbc7f062015-06-11 14:46:38 +02004971 bool user, bool pi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972{
Steven Rostedt383afd02014-03-11 19:24:20 -04004973 int newprio = dl_policy(attr->sched_policy) ? MAX_DL_PRIO - 1 :
4974 MAX_RT_PRIO - 1 - attr->sched_priority;
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04004975 int retval, oldprio, oldpolicy = -1, queued, running;
Thomas Gleixner0782e632015-05-05 19:49:49 +02004976 int new_effective_prio, policy = attr->sched_policy;
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01004977 const struct sched_class *prev_class;
Peter Zijlstraeb580752015-07-31 21:28:18 +02004978 struct rq_flags rf;
Lennart Poetteringca94c442009-06-15 17:17:47 +02004979 int reset_on_fork;
Peter Zijlstra7a57f322017-02-21 14:47:02 +01004980 int queue_flags = DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK;
Peter Zijlstraeb580752015-07-31 21:28:18 +02004981 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982
Steven Rostedt (VMware)896bbb22017-03-09 10:18:42 -05004983 /* The pi code expects interrupts enabled */
4984 BUG_ON(pi && in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985recheck:
Ingo Molnard1ccc662017-02-01 11:46:42 +01004986 /* Double check policy once rq lock held: */
Lennart Poetteringca94c442009-06-15 17:17:47 +02004987 if (policy < 0) {
4988 reset_on_fork = p->sched_reset_on_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 policy = oldpolicy = p->policy;
Lennart Poetteringca94c442009-06-15 17:17:47 +02004990 } else {
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004991 reset_on_fork = !!(attr->sched_flags & SCHED_FLAG_RESET_ON_FORK);
Lennart Poetteringca94c442009-06-15 17:17:47 +02004992
Henrik Austad20f9cd22015-09-09 17:00:41 +02004993 if (!valid_policy(policy))
Lennart Poetteringca94c442009-06-15 17:17:47 +02004994 return -EINVAL;
4995 }
4996
Juri Lelli794a56e2017-12-04 11:23:20 +01004997 if (attr->sched_flags & ~(SCHED_FLAG_ALL | SCHED_FLAG_SUGOV))
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01004998 return -EINVAL;
4999
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 /*
5001 * Valid priorities for SCHED_FIFO and SCHED_RR are
Ingo Molnardd41f592007-07-09 18:51:59 +02005002 * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
5003 * SCHED_BATCH and SCHED_IDLE is 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 */
Peter Zijlstra0bb040a2014-01-15 17:15:13 +01005005 if ((p->mm && attr->sched_priority > MAX_USER_RT_PRIO-1) ||
Dario Faggiolid50dde52013-11-07 14:43:36 +01005006 (!p->mm && attr->sched_priority > MAX_RT_PRIO-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 return -EINVAL;
Dario Faggioliaab03e02013-11-28 11:14:43 +01005008 if ((dl_policy(policy) && !__checkparam_dl(attr)) ||
5009 (rt_policy(policy) != (attr->sched_priority != 0)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 return -EINVAL;
5011
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005012 /*
5013 * Allow unprivileged RT tasks to decrease priority:
5014 */
Rusty Russell961ccdd2008-06-23 13:55:38 +10005015 if (user && !capable(CAP_SYS_NICE)) {
Dario Faggiolid50dde52013-11-07 14:43:36 +01005016 if (fair_policy(policy)) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05005017 if (attr->sched_nice < task_nice(p) &&
Peter Zijlstraeaad4512014-01-16 17:54:25 +01005018 !can_nice(p, attr->sched_nice))
Dario Faggiolid50dde52013-11-07 14:43:36 +01005019 return -EPERM;
5020 }
5021
Ingo Molnare05606d2007-07-09 18:51:59 +02005022 if (rt_policy(policy)) {
Oleg Nesterova44702e82010-06-11 01:09:44 +02005023 unsigned long rlim_rtprio =
5024 task_rlimit(p, RLIMIT_RTPRIO);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005025
Ingo Molnard1ccc662017-02-01 11:46:42 +01005026 /* Can't set/change the rt policy: */
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005027 if (policy != p->policy && !rlim_rtprio)
5028 return -EPERM;
5029
Ingo Molnard1ccc662017-02-01 11:46:42 +01005030 /* Can't increase priority: */
Dario Faggiolid50dde52013-11-07 14:43:36 +01005031 if (attr->sched_priority > p->rt_priority &&
5032 attr->sched_priority > rlim_rtprio)
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005033 return -EPERM;
5034 }
Darren Hartc02aa732011-02-17 15:37:07 -08005035
Juri Lellid44753b2014-03-03 12:09:21 +01005036 /*
5037 * Can't set/change SCHED_DEADLINE policy at all for now
5038 * (safest behavior); in the future we would like to allow
5039 * unprivileged DL tasks to increase their relative deadline
5040 * or reduce their runtime (both ways reducing utilization)
5041 */
5042 if (dl_policy(policy))
5043 return -EPERM;
5044
Ingo Molnardd41f592007-07-09 18:51:59 +02005045 /*
Darren Hartc02aa732011-02-17 15:37:07 -08005046 * Treat SCHED_IDLE as nice 20. Only allow a switch to
5047 * SCHED_NORMAL if the RLIMIT_NICE would normally permit it.
Ingo Molnardd41f592007-07-09 18:51:59 +02005048 */
Viresh Kumar1da18432018-11-05 16:51:55 +05305049 if (task_has_idle_policy(p) && !idle_policy(policy)) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05005050 if (!can_nice(p, task_nice(p)))
Darren Hartc02aa732011-02-17 15:37:07 -08005051 return -EPERM;
5052 }
Oleg Nesterov8dc3e902006-09-29 02:00:50 -07005053
Ingo Molnard1ccc662017-02-01 11:46:42 +01005054 /* Can't change other user's priorities: */
David Howellsc69e8d92008-11-14 10:39:19 +11005055 if (!check_same_owner(p))
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005056 return -EPERM;
Lennart Poetteringca94c442009-06-15 17:17:47 +02005057
Ingo Molnard1ccc662017-02-01 11:46:42 +01005058 /* Normal users shall not reset the sched_reset_on_fork flag: */
Lennart Poetteringca94c442009-06-15 17:17:47 +02005059 if (p->sched_reset_on_fork && !reset_on_fork)
5060 return -EPERM;
Olivier Croquette37e4ab32005-06-25 14:57:32 -07005061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005063 if (user) {
Juri Lelli794a56e2017-12-04 11:23:20 +01005064 if (attr->sched_flags & SCHED_FLAG_SUGOV)
5065 return -EINVAL;
5066
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09005067 retval = security_task_setscheduler(p);
Jeremy Fitzhardinge725aad22008-08-03 09:33:03 -07005068 if (retval)
5069 return retval;
5070 }
5071
Patrick Bellasia509a7c2019-06-21 09:42:07 +01005072 /* Update task specific "requested" clamps */
5073 if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) {
5074 retval = uclamp_validate(p, attr);
5075 if (retval)
5076 return retval;
5077 }
5078
Juri Lelli710da3c2019-07-19 16:00:00 +02005079 if (pi)
5080 cpuset_read_lock();
5081
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 /*
Ingo Molnard1ccc662017-02-01 11:46:42 +01005083 * Make sure no PI-waiters arrive (or leave) while we are
Ingo Molnarb29739f2006-06-27 02:54:51 -07005084 * changing the priority of the task:
Peter Zijlstra0122ec52011-04-05 17:23:51 +02005085 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005086 * To be able to change p->policy safely, the appropriate
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 * runqueue lock must be held.
5088 */
Peter Zijlstraeb580752015-07-31 21:28:18 +02005089 rq = task_rq_lock(p, &rf);
Peter Zijlstra80f5c1b2016-10-03 16:28:37 +02005090 update_rq_clock(rq);
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02005091
Peter Zijlstra34f971f2010-09-22 13:53:15 +02005092 /*
Ingo Molnard1ccc662017-02-01 11:46:42 +01005093 * Changing the policy of the stop threads its a very bad idea:
Peter Zijlstra34f971f2010-09-22 13:53:15 +02005094 */
5095 if (p == rq->stop) {
Mathieu Poirier4b211f22019-07-19 15:59:54 +02005096 retval = -EINVAL;
5097 goto unlock;
Peter Zijlstra34f971f2010-09-22 13:53:15 +02005098 }
5099
Dario Faggiolia51e9192011-03-24 14:00:18 +01005100 /*
Thomas Gleixnerd6b1e912014-02-07 20:58:40 +01005101 * If not changing anything there's no need to proceed further,
5102 * but store a possible modification of reset_on_fork.
Dario Faggiolia51e9192011-03-24 14:00:18 +01005103 */
Dario Faggiolid50dde52013-11-07 14:43:36 +01005104 if (unlikely(policy == p->policy)) {
Dongsheng Yangd0ea0262014-01-27 22:00:45 -05005105 if (fair_policy(policy) && attr->sched_nice != task_nice(p))
Dario Faggiolid50dde52013-11-07 14:43:36 +01005106 goto change;
5107 if (rt_policy(policy) && attr->sched_priority != p->rt_priority)
5108 goto change;
Wanpeng Li75381602014-11-26 08:44:04 +08005109 if (dl_policy(policy) && dl_param_changed(p, attr))
Dario Faggioliaab03e02013-11-28 11:14:43 +01005110 goto change;
Patrick Bellasia509a7c2019-06-21 09:42:07 +01005111 if (attr->sched_flags & SCHED_FLAG_UTIL_CLAMP)
5112 goto change;
Dario Faggiolid50dde52013-11-07 14:43:36 +01005113
Thomas Gleixnerd6b1e912014-02-07 20:58:40 +01005114 p->sched_reset_on_fork = reset_on_fork;
Mathieu Poirier4b211f22019-07-19 15:59:54 +02005115 retval = 0;
5116 goto unlock;
Dario Faggiolia51e9192011-03-24 14:00:18 +01005117 }
Dario Faggiolid50dde52013-11-07 14:43:36 +01005118change:
Dario Faggiolia51e9192011-03-24 14:00:18 +01005119
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02005120 if (user) {
Dario Faggioli332ac172013-11-07 14:43:45 +01005121#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02005122 /*
5123 * Do not allow realtime tasks into groups that have no runtime
5124 * assigned.
5125 */
5126 if (rt_bandwidth_enabled() && rt_policy(policy) &&
Mike Galbraithf4493772011-01-13 04:54:50 +01005127 task_group(p)->rt_bandwidth.rt_runtime == 0 &&
5128 !task_group_is_autogroup(task_group(p))) {
Mathieu Poirier4b211f22019-07-19 15:59:54 +02005129 retval = -EPERM;
5130 goto unlock;
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02005131 }
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02005132#endif
Dario Faggioli332ac172013-11-07 14:43:45 +01005133#ifdef CONFIG_SMP
Juri Lelli794a56e2017-12-04 11:23:20 +01005134 if (dl_bandwidth_enabled() && dl_policy(policy) &&
5135 !(attr->sched_flags & SCHED_FLAG_SUGOV)) {
Dario Faggioli332ac172013-11-07 14:43:45 +01005136 cpumask_t *span = rq->rd->span;
Dario Faggioli332ac172013-11-07 14:43:45 +01005137
5138 /*
5139 * Don't allow tasks with an affinity mask smaller than
5140 * the entire root_domain to become SCHED_DEADLINE. We
5141 * will also fail if there's no bandwidth available.
5142 */
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02005143 if (!cpumask_subset(span, p->cpus_ptr) ||
Peter Zijlstrae4099a52013-12-17 10:03:34 +01005144 rq->rd->dl_bw.bw == 0) {
Mathieu Poirier4b211f22019-07-19 15:59:54 +02005145 retval = -EPERM;
5146 goto unlock;
Dario Faggioli332ac172013-11-07 14:43:45 +01005147 }
5148 }
5149#endif
5150 }
Peter Zijlstradc61b1d2010-06-08 11:40:42 +02005151
Ingo Molnard1ccc662017-02-01 11:46:42 +01005152 /* Re-check policy now with rq lock held: */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
5154 policy = oldpolicy = -1;
Peter Zijlstraeb580752015-07-31 21:28:18 +02005155 task_rq_unlock(rq, p, &rf);
Juri Lelli710da3c2019-07-19 16:00:00 +02005156 if (pi)
5157 cpuset_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 goto recheck;
5159 }
Dario Faggioli332ac172013-11-07 14:43:45 +01005160
5161 /*
5162 * If setscheduling to SCHED_DEADLINE (or changing the parameters
5163 * of a SCHED_DEADLINE task) we need to check if enough bandwidth
5164 * is available.
5165 */
Nicolas Pitre06a76fe2017-06-21 14:22:01 -04005166 if ((dl_policy(policy) || dl_task(p)) && sched_dl_overflow(p, policy, attr)) {
Mathieu Poirier4b211f22019-07-19 15:59:54 +02005167 retval = -EBUSY;
5168 goto unlock;
Dario Faggioli332ac172013-11-07 14:43:45 +01005169 }
5170
Thomas Gleixnerc365c292014-02-07 20:58:42 +01005171 p->sched_reset_on_fork = reset_on_fork;
5172 oldprio = p->prio;
5173
Peter Zijlstradbc7f062015-06-11 14:46:38 +02005174 if (pi) {
5175 /*
5176 * Take priority boosted tasks into account. If the new
5177 * effective priority is unchanged, we just store the new
5178 * normal parameters and do not touch the scheduler class and
5179 * the runqueue. This will be done when the task deboost
5180 * itself.
5181 */
Peter Zijlstraacd58622017-03-23 15:56:11 +01005182 new_effective_prio = rt_effective_prio(p, newprio);
Peter Zijlstraff77e462016-01-18 15:27:07 +01005183 if (new_effective_prio == oldprio)
5184 queue_flags &= ~DEQUEUE_MOVE;
Thomas Gleixnerc365c292014-02-07 20:58:42 +01005185 }
5186
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005187 queued = task_on_rq_queued(p);
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01005188 running = task_current(rq, p);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005189 if (queued)
Peter Zijlstraff77e462016-01-18 15:27:07 +01005190 dequeue_task(rq, p, queue_flags);
Hiroshi Shimamoto0e1f3482008-03-10 11:01:20 -07005191 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04005192 put_prev_task(rq, p);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005193
Thomas Gleixner83ab0aa2010-02-17 09:05:48 +01005194 prev_class = p->sched_class;
Patrick Bellasia509a7c2019-06-21 09:42:07 +01005195
Peter Zijlstradbc7f062015-06-11 14:46:38 +02005196 __setscheduler(rq, p, attr, pi);
Patrick Bellasia509a7c2019-06-21 09:42:07 +01005197 __setscheduler_uclamp(p, attr);
Dmitry Adamushkof6b532052007-10-15 17:00:08 +02005198
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04005199 if (queued) {
Thomas Gleixner81a44c52014-02-07 20:58:41 +01005200 /*
5201 * We enqueue to tail when the priority of a task is
5202 * increased (user space view).
5203 */
Peter Zijlstraff77e462016-01-18 15:27:07 +01005204 if (oldprio < p->prio)
5205 queue_flags |= ENQUEUE_HEAD;
Peter Zijlstra1de64442015-09-30 17:44:13 +02005206
Peter Zijlstraff77e462016-01-18 15:27:07 +01005207 enqueue_task(rq, p, queue_flags);
Thomas Gleixner81a44c52014-02-07 20:58:41 +01005208 }
Vincent Guittota399d232016-09-12 09:47:52 +02005209 if (running)
Peter Zijlstra03b7fad2019-05-29 20:36:41 +00005210 set_next_task(rq, p);
Steven Rostedtcb469842008-01-25 21:08:22 +01005211
Peter Zijlstrada7a7352011-01-17 17:03:27 +01005212 check_class_changed(rq, p, prev_class, oldprio);
Ingo Molnard1ccc662017-02-01 11:46:42 +01005213
5214 /* Avoid rq from going away on us: */
5215 preempt_disable();
Peter Zijlstraeb580752015-07-31 21:28:18 +02005216 task_rq_unlock(rq, p, &rf);
Ingo Molnarb29739f2006-06-27 02:54:51 -07005217
Juri Lelli710da3c2019-07-19 16:00:00 +02005218 if (pi) {
5219 cpuset_read_unlock();
Peter Zijlstradbc7f062015-06-11 14:46:38 +02005220 rt_mutex_adjust_pi(p);
Juri Lelli710da3c2019-07-19 16:00:00 +02005221 }
Thomas Gleixner95e02ca2006-06-27 02:55:02 -07005222
Ingo Molnard1ccc662017-02-01 11:46:42 +01005223 /* Run balance callbacks after we've adjusted the PI chain: */
Peter Zijlstra4c9a4bc2015-06-11 14:46:39 +02005224 balance_callback(rq);
5225 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226
5227 return 0;
Mathieu Poirier4b211f22019-07-19 15:59:54 +02005228
5229unlock:
5230 task_rq_unlock(rq, p, &rf);
Juri Lelli710da3c2019-07-19 16:00:00 +02005231 if (pi)
5232 cpuset_read_unlock();
Mathieu Poirier4b211f22019-07-19 15:59:54 +02005233 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234}
Rusty Russell961ccdd2008-06-23 13:55:38 +10005235
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01005236static int _sched_setscheduler(struct task_struct *p, int policy,
5237 const struct sched_param *param, bool check)
5238{
5239 struct sched_attr attr = {
5240 .sched_policy = policy,
5241 .sched_priority = param->sched_priority,
5242 .sched_nice = PRIO_TO_NICE(p->static_prio),
5243 };
5244
Steven Rostedtc13db6b2014-07-23 11:28:26 -04005245 /* Fixup the legacy SCHED_RESET_ON_FORK hack. */
5246 if ((policy != SETPARAM_POLICY) && (policy & SCHED_RESET_ON_FORK)) {
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01005247 attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
5248 policy &= ~SCHED_RESET_ON_FORK;
5249 attr.sched_policy = policy;
5250 }
5251
Peter Zijlstradbc7f062015-06-11 14:46:38 +02005252 return __sched_setscheduler(p, &attr, check, true);
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01005253}
Rusty Russell961ccdd2008-06-23 13:55:38 +10005254/**
5255 * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
5256 * @p: the task in question.
5257 * @policy: new policy.
5258 * @param: structure containing the new RT priority.
5259 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02005260 * Return: 0 on success. An error code otherwise.
5261 *
Rusty Russell961ccdd2008-06-23 13:55:38 +10005262 * NOTE that the task may be already dead.
5263 */
5264int sched_setscheduler(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07005265 const struct sched_param *param)
Rusty Russell961ccdd2008-06-23 13:55:38 +10005266{
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01005267 return _sched_setscheduler(p, policy, param, true);
Rusty Russell961ccdd2008-06-23 13:55:38 +10005268}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269EXPORT_SYMBOL_GPL(sched_setscheduler);
5270
Dario Faggiolid50dde52013-11-07 14:43:36 +01005271int sched_setattr(struct task_struct *p, const struct sched_attr *attr)
5272{
Peter Zijlstradbc7f062015-06-11 14:46:38 +02005273 return __sched_setscheduler(p, attr, true, true);
Dario Faggiolid50dde52013-11-07 14:43:36 +01005274}
5275EXPORT_SYMBOL_GPL(sched_setattr);
5276
Juri Lelli794a56e2017-12-04 11:23:20 +01005277int sched_setattr_nocheck(struct task_struct *p, const struct sched_attr *attr)
5278{
5279 return __sched_setscheduler(p, attr, false, true);
5280}
5281
Rusty Russell961ccdd2008-06-23 13:55:38 +10005282/**
5283 * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
5284 * @p: the task in question.
5285 * @policy: new policy.
5286 * @param: structure containing the new RT priority.
5287 *
5288 * Just like sched_setscheduler, only don't bother checking if the
5289 * current context has permission. For example, this is needed in
5290 * stop_machine(): we create temporary high priority worker threads,
5291 * but our caller might not have that capability.
Yacine Belkadie69f6182013-07-12 20:45:47 +02005292 *
5293 * Return: 0 on success. An error code otherwise.
Rusty Russell961ccdd2008-06-23 13:55:38 +10005294 */
5295int sched_setscheduler_nocheck(struct task_struct *p, int policy,
KOSAKI Motohirofe7de492010-10-20 16:01:12 -07005296 const struct sched_param *param)
Rusty Russell961ccdd2008-06-23 13:55:38 +10005297{
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01005298 return _sched_setscheduler(p, policy, param, false);
Rusty Russell961ccdd2008-06-23 13:55:38 +10005299}
Davidlohr Bueso84778472015-09-02 01:28:44 -07005300EXPORT_SYMBOL_GPL(sched_setscheduler_nocheck);
Rusty Russell961ccdd2008-06-23 13:55:38 +10005301
Ingo Molnar95cdf3b2005-09-10 00:26:11 -07005302static int
5303do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 struct sched_param lparam;
5306 struct task_struct *p;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005307 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308
5309 if (!param || pid < 0)
5310 return -EINVAL;
5311 if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
5312 return -EFAULT;
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005313
5314 rcu_read_lock();
5315 retval = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 p = find_process_by_pid(pid);
Juri Lelli710da3c2019-07-19 16:00:00 +02005317 if (likely(p))
5318 get_task_struct(p);
Oleg Nesterov5fe1d752006-09-29 02:00:48 -07005319 rcu_read_unlock();
Ingo Molnar36c8b582006-07-03 00:25:41 -07005320
Juri Lelli710da3c2019-07-19 16:00:00 +02005321 if (likely(p)) {
5322 retval = sched_setscheduler(p, policy, &lparam);
5323 put_task_struct(p);
5324 }
5325
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 return retval;
5327}
5328
Dario Faggiolid50dde52013-11-07 14:43:36 +01005329/*
5330 * Mimics kernel/events/core.c perf_copy_attr().
5331 */
Ingo Molnard1ccc662017-02-01 11:46:42 +01005332static int sched_copy_attr(struct sched_attr __user *uattr, struct sched_attr *attr)
Dario Faggiolid50dde52013-11-07 14:43:36 +01005333{
5334 u32 size;
5335 int ret;
5336
Ingo Molnard1ccc662017-02-01 11:46:42 +01005337 /* Zero the full structure, so that a short copy will be nice: */
Dario Faggiolid50dde52013-11-07 14:43:36 +01005338 memset(attr, 0, sizeof(*attr));
5339
5340 ret = get_user(size, &uattr->size);
5341 if (ret)
5342 return ret;
5343
Ingo Molnard1ccc662017-02-01 11:46:42 +01005344 /* ABI compatibility quirk: */
5345 if (!size)
Dario Faggiolid50dde52013-11-07 14:43:36 +01005346 size = SCHED_ATTR_SIZE_VER0;
Aleksa Saraidff3a852019-10-01 11:10:54 +10005347 if (size < SCHED_ATTR_SIZE_VER0 || size > PAGE_SIZE)
Dario Faggiolid50dde52013-11-07 14:43:36 +01005348 goto err_size;
5349
Aleksa Saraidff3a852019-10-01 11:10:54 +10005350 ret = copy_struct_from_user(attr, sizeof(*attr), uattr, size);
5351 if (ret) {
5352 if (ret == -E2BIG)
5353 goto err_size;
5354 return ret;
Dario Faggiolid50dde52013-11-07 14:43:36 +01005355 }
5356
Patrick Bellasia509a7c2019-06-21 09:42:07 +01005357 if ((attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) &&
5358 size < SCHED_ATTR_SIZE_VER1)
5359 return -EINVAL;
5360
Dario Faggiolid50dde52013-11-07 14:43:36 +01005361 /*
Ingo Molnard1ccc662017-02-01 11:46:42 +01005362 * XXX: Do we want to be lenient like existing syscalls; or do we want
Dario Faggiolid50dde52013-11-07 14:43:36 +01005363 * to be strict and return an error on out-of-bounds values?
5364 */
Dongsheng Yang75e45d52014-02-11 15:34:50 +08005365 attr->sched_nice = clamp(attr->sched_nice, MIN_NICE, MAX_NICE);
Dario Faggiolid50dde52013-11-07 14:43:36 +01005366
Michael Kerriske78c7bc2014-05-09 16:54:28 +02005367 return 0;
Dario Faggiolid50dde52013-11-07 14:43:36 +01005368
5369err_size:
5370 put_user(sizeof(*attr), &uattr->size);
Michael Kerriske78c7bc2014-05-09 16:54:28 +02005371 return -E2BIG;
Dario Faggiolid50dde52013-11-07 14:43:36 +01005372}
5373
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374/**
5375 * sys_sched_setscheduler - set/change the scheduler policy and RT priority
5376 * @pid: the pid in question.
5377 * @policy: new policy.
5378 * @param: structure containing the new RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02005379 *
5380 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 */
Ingo Molnard1ccc662017-02-01 11:46:42 +01005382SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383{
Jason Baronc21761f2006-01-18 17:43:03 -08005384 if (policy < 0)
5385 return -EINVAL;
5386
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 return do_sched_setscheduler(pid, policy, param);
5388}
5389
5390/**
5391 * sys_sched_setparam - set/change the RT priority of a thread
5392 * @pid: the pid in question.
5393 * @param: structure containing the new RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02005394 *
5395 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005397SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398{
Steven Rostedtc13db6b2014-07-23 11:28:26 -04005399 return do_sched_setscheduler(pid, SETPARAM_POLICY, param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400}
5401
5402/**
Dario Faggiolid50dde52013-11-07 14:43:36 +01005403 * sys_sched_setattr - same as above, but with extended sched_attr
5404 * @pid: the pid in question.
Juri Lelli5778fcc2014-01-14 16:10:39 +01005405 * @uattr: structure containing the extended parameters.
Masanari Iidadb66d752014-04-18 01:59:15 +09005406 * @flags: for future extension.
Dario Faggiolid50dde52013-11-07 14:43:36 +01005407 */
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01005408SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr,
5409 unsigned int, flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01005410{
5411 struct sched_attr attr;
5412 struct task_struct *p;
5413 int retval;
5414
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01005415 if (!uattr || pid < 0 || flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01005416 return -EINVAL;
5417
Michael Kerrisk143cf232014-05-09 16:54:15 +02005418 retval = sched_copy_attr(uattr, &attr);
5419 if (retval)
5420 return retval;
Dario Faggiolid50dde52013-11-07 14:43:36 +01005421
Richard Weinbergerb14ed2c2014-06-02 22:38:34 +02005422 if ((int)attr.sched_policy < 0)
Peter Zijlstradbdb2272014-05-09 10:49:03 +02005423 return -EINVAL;
Patrick Bellasi1d6362f2019-06-21 09:42:06 +01005424 if (attr.sched_flags & SCHED_FLAG_KEEP_POLICY)
5425 attr.sched_policy = SETPARAM_POLICY;
Dario Faggiolid50dde52013-11-07 14:43:36 +01005426
5427 rcu_read_lock();
5428 retval = -ESRCH;
5429 p = find_process_by_pid(pid);
Patrick Bellasia509a7c2019-06-21 09:42:07 +01005430 if (likely(p))
5431 get_task_struct(p);
Dario Faggiolid50dde52013-11-07 14:43:36 +01005432 rcu_read_unlock();
5433
Patrick Bellasia509a7c2019-06-21 09:42:07 +01005434 if (likely(p)) {
5435 retval = sched_setattr(p, &attr);
5436 put_task_struct(p);
5437 }
5438
Dario Faggiolid50dde52013-11-07 14:43:36 +01005439 return retval;
5440}
5441
5442/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 * sys_sched_getscheduler - get the policy (scheduling class) of a thread
5444 * @pid: the pid in question.
Yacine Belkadie69f6182013-07-12 20:45:47 +02005445 *
5446 * Return: On success, the policy of the thread. Otherwise, a negative error
5447 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005449SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005451 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005452 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453
5454 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005455 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456
5457 retval = -ESRCH;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00005458 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 p = find_process_by_pid(pid);
5460 if (p) {
5461 retval = security_task_getscheduler(p);
5462 if (!retval)
Lennart Poetteringca94c442009-06-15 17:17:47 +02005463 retval = p->policy
5464 | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 }
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00005466 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 return retval;
5468}
5469
5470/**
Lennart Poetteringca94c442009-06-15 17:17:47 +02005471 * sys_sched_getparam - get the RT priority of a thread
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 * @pid: the pid in question.
5473 * @param: structure containing the RT priority.
Yacine Belkadie69f6182013-07-12 20:45:47 +02005474 *
5475 * Return: On success, 0 and the RT priority is in @param. Otherwise, an error
5476 * code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005478SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479{
Peter Zijlstrace5f7f82014-05-12 22:50:34 +02005480 struct sched_param lp = { .sched_priority = 0 };
Ingo Molnar36c8b582006-07-03 00:25:41 -07005481 struct task_struct *p;
Andi Kleen3a5c3592007-10-15 17:00:14 +02005482 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483
5484 if (!param || pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02005485 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00005487 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 p = find_process_by_pid(pid);
5489 retval = -ESRCH;
5490 if (!p)
5491 goto out_unlock;
5492
5493 retval = security_task_getscheduler(p);
5494 if (retval)
5495 goto out_unlock;
5496
Peter Zijlstrace5f7f82014-05-12 22:50:34 +02005497 if (task_has_rt_policy(p))
5498 lp.sched_priority = p->rt_priority;
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00005499 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500
5501 /*
5502 * This one might sleep, we cannot do it with a spinlock held ...
5503 */
5504 retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
5505
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 return retval;
5507
5508out_unlock:
Thomas Gleixner5fe85be2009-12-09 10:14:58 +00005509 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 return retval;
5511}
5512
Ingo Molnar12512012019-09-04 09:55:32 +02005513/*
5514 * Copy the kernel size attribute structure (which might be larger
5515 * than what user-space knows about) to user-space.
5516 *
5517 * Note that all cases are valid: user-space buffer can be larger or
5518 * smaller than the kernel-space buffer. The usual case is that both
5519 * have the same size.
5520 */
5521static int
5522sched_attr_copy_to_user(struct sched_attr __user *uattr,
5523 struct sched_attr *kattr,
5524 unsigned int usize)
Dario Faggiolid50dde52013-11-07 14:43:36 +01005525{
Ingo Molnar12512012019-09-04 09:55:32 +02005526 unsigned int ksize = sizeof(*kattr);
Dario Faggiolid50dde52013-11-07 14:43:36 +01005527
Linus Torvalds96d4f262019-01-03 18:57:57 -08005528 if (!access_ok(uattr, usize))
Dario Faggiolid50dde52013-11-07 14:43:36 +01005529 return -EFAULT;
5530
5531 /*
Ingo Molnar12512012019-09-04 09:55:32 +02005532 * sched_getattr() ABI forwards and backwards compatibility:
5533 *
5534 * If usize == ksize then we just copy everything to user-space and all is good.
5535 *
5536 * If usize < ksize then we only copy as much as user-space has space for,
5537 * this keeps ABI compatibility as well. We skip the rest.
5538 *
5539 * If usize > ksize then user-space is using a newer version of the ABI,
5540 * which part the kernel doesn't know about. Just ignore it - tooling can
5541 * detect the kernel's knowledge of attributes from the attr->size value
5542 * which is set to ksize in this case.
Dario Faggiolid50dde52013-11-07 14:43:36 +01005543 */
Ingo Molnar12512012019-09-04 09:55:32 +02005544 kattr->size = min(usize, ksize);
Dario Faggiolid50dde52013-11-07 14:43:36 +01005545
Ingo Molnar12512012019-09-04 09:55:32 +02005546 if (copy_to_user(uattr, kattr, kattr->size))
Dario Faggiolid50dde52013-11-07 14:43:36 +01005547 return -EFAULT;
5548
Michael Kerrisk22400672014-05-09 16:54:33 +02005549 return 0;
Dario Faggiolid50dde52013-11-07 14:43:36 +01005550}
5551
5552/**
Dario Faggioliaab03e02013-11-28 11:14:43 +01005553 * sys_sched_getattr - similar to sched_getparam, but with sched_attr
Dario Faggiolid50dde52013-11-07 14:43:36 +01005554 * @pid: the pid in question.
Juri Lelli5778fcc2014-01-14 16:10:39 +01005555 * @uattr: structure containing the extended parameters.
Aleksa Saraidff3a852019-10-01 11:10:54 +10005556 * @usize: sizeof(attr) for fwd/bwd comp.
Masanari Iidadb66d752014-04-18 01:59:15 +09005557 * @flags: for future extension.
Dario Faggiolid50dde52013-11-07 14:43:36 +01005558 */
Peter Zijlstra6d35ab42014-02-14 17:19:29 +01005559SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr,
Ingo Molnar12512012019-09-04 09:55:32 +02005560 unsigned int, usize, unsigned int, flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01005561{
Ingo Molnar12512012019-09-04 09:55:32 +02005562 struct sched_attr kattr = { };
Dario Faggiolid50dde52013-11-07 14:43:36 +01005563 struct task_struct *p;
5564 int retval;
5565
Ingo Molnar12512012019-09-04 09:55:32 +02005566 if (!uattr || pid < 0 || usize > PAGE_SIZE ||
5567 usize < SCHED_ATTR_SIZE_VER0 || flags)
Dario Faggiolid50dde52013-11-07 14:43:36 +01005568 return -EINVAL;
5569
5570 rcu_read_lock();
5571 p = find_process_by_pid(pid);
5572 retval = -ESRCH;
5573 if (!p)
5574 goto out_unlock;
5575
5576 retval = security_task_getscheduler(p);
5577 if (retval)
5578 goto out_unlock;
5579
Ingo Molnar12512012019-09-04 09:55:32 +02005580 kattr.sched_policy = p->policy;
Peter Zijlstra7479f3c9c2014-01-15 17:05:04 +01005581 if (p->sched_reset_on_fork)
Ingo Molnar12512012019-09-04 09:55:32 +02005582 kattr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
Dario Faggioliaab03e02013-11-28 11:14:43 +01005583 if (task_has_dl_policy(p))
Ingo Molnar12512012019-09-04 09:55:32 +02005584 __getparam_dl(p, &kattr);
Dario Faggioliaab03e02013-11-28 11:14:43 +01005585 else if (task_has_rt_policy(p))
Ingo Molnar12512012019-09-04 09:55:32 +02005586 kattr.sched_priority = p->rt_priority;
Dario Faggiolid50dde52013-11-07 14:43:36 +01005587 else
Ingo Molnar12512012019-09-04 09:55:32 +02005588 kattr.sched_nice = task_nice(p);
Dario Faggiolid50dde52013-11-07 14:43:36 +01005589
Patrick Bellasia509a7c2019-06-21 09:42:07 +01005590#ifdef CONFIG_UCLAMP_TASK
Ingo Molnar12512012019-09-04 09:55:32 +02005591 kattr.sched_util_min = p->uclamp_req[UCLAMP_MIN].value;
5592 kattr.sched_util_max = p->uclamp_req[UCLAMP_MAX].value;
Patrick Bellasia509a7c2019-06-21 09:42:07 +01005593#endif
5594
Dario Faggiolid50dde52013-11-07 14:43:36 +01005595 rcu_read_unlock();
5596
Ingo Molnar12512012019-09-04 09:55:32 +02005597 return sched_attr_copy_to_user(uattr, &kattr, usize);
Dario Faggiolid50dde52013-11-07 14:43:36 +01005598
5599out_unlock:
5600 rcu_read_unlock();
5601 return retval;
5602}
5603
Rusty Russell96f874e22008-11-25 02:35:14 +10305604long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305606 cpumask_var_t cpus_allowed, new_mask;
Ingo Molnar36c8b582006-07-03 00:25:41 -07005607 struct task_struct *p;
5608 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005610 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611
5612 p = find_process_by_pid(pid);
5613 if (!p) {
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005614 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 return -ESRCH;
5616 }
5617
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005618 /* Prevent p going away */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 get_task_struct(p);
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005620 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621
Tejun Heo14a40ff2013-03-19 13:45:20 -07005622 if (p->flags & PF_NO_SETAFFINITY) {
5623 retval = -EINVAL;
5624 goto out_put_task;
5625 }
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305626 if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
5627 retval = -ENOMEM;
5628 goto out_put_task;
5629 }
5630 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
5631 retval = -ENOMEM;
5632 goto out_free_cpus_allowed;
5633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 retval = -EPERM;
Eric W. Biederman4c44aaa2012-07-26 05:05:21 -07005635 if (!check_same_owner(p)) {
5636 rcu_read_lock();
5637 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
5638 rcu_read_unlock();
Kirill Tkhai16303ab2014-09-22 22:36:30 +04005639 goto out_free_new_mask;
Eric W. Biederman4c44aaa2012-07-26 05:05:21 -07005640 }
5641 rcu_read_unlock();
5642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09005644 retval = security_task_setscheduler(p);
David Quigleye7834f82006-06-23 02:03:59 -07005645 if (retval)
Kirill Tkhai16303ab2014-09-22 22:36:30 +04005646 goto out_free_new_mask;
David Quigleye7834f82006-06-23 02:03:59 -07005647
Peter Zijlstrae4099a52013-12-17 10:03:34 +01005648
5649 cpuset_cpus_allowed(p, cpus_allowed);
5650 cpumask_and(new_mask, in_mask, cpus_allowed);
5651
Dario Faggioli332ac172013-11-07 14:43:45 +01005652 /*
5653 * Since bandwidth control happens on root_domain basis,
5654 * if admission test is enabled, we only admit -deadline
5655 * tasks allowed to run on all the CPUs in the task's
5656 * root_domain.
5657 */
5658#ifdef CONFIG_SMP
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04005659 if (task_has_dl_policy(p) && dl_bandwidth_enabled()) {
5660 rcu_read_lock();
5661 if (!cpumask_subset(task_rq(p)->rd->span, new_mask)) {
Dario Faggioli332ac172013-11-07 14:43:45 +01005662 retval = -EBUSY;
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04005663 rcu_read_unlock();
Kirill Tkhai16303ab2014-09-22 22:36:30 +04005664 goto out_free_new_mask;
Dario Faggioli332ac172013-11-07 14:43:45 +01005665 }
Kirill Tkhaif1e3a092014-09-22 22:36:36 +04005666 rcu_read_unlock();
Dario Faggioli332ac172013-11-07 14:43:45 +01005667 }
5668#endif
Peter Zijlstra49246272010-10-17 21:46:10 +02005669again:
Peter Zijlstra25834c72015-05-15 17:43:34 +02005670 retval = __set_cpus_allowed_ptr(p, new_mask, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671
Paul Menage8707d8b2007-10-18 23:40:22 -07005672 if (!retval) {
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305673 cpuset_cpus_allowed(p, cpus_allowed);
5674 if (!cpumask_subset(new_mask, cpus_allowed)) {
Paul Menage8707d8b2007-10-18 23:40:22 -07005675 /*
5676 * We must have raced with a concurrent cpuset
5677 * update. Just reset the cpus_allowed to the
5678 * cpuset's cpus_allowed
5679 */
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305680 cpumask_copy(new_mask, cpus_allowed);
Paul Menage8707d8b2007-10-18 23:40:22 -07005681 goto again;
5682 }
5683 }
Kirill Tkhai16303ab2014-09-22 22:36:30 +04005684out_free_new_mask:
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305685 free_cpumask_var(new_mask);
5686out_free_cpus_allowed:
5687 free_cpumask_var(cpus_allowed);
5688out_put_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 put_task_struct(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 return retval;
5691}
5692
5693static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
Rusty Russell96f874e22008-11-25 02:35:14 +10305694 struct cpumask *new_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695{
Rusty Russell96f874e22008-11-25 02:35:14 +10305696 if (len < cpumask_size())
5697 cpumask_clear(new_mask);
5698 else if (len > cpumask_size())
5699 len = cpumask_size();
5700
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
5702}
5703
5704/**
Ingo Molnard1ccc662017-02-01 11:46:42 +01005705 * sys_sched_setaffinity - set the CPU affinity of a process
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 * @pid: pid of the process
5707 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
Ingo Molnard1ccc662017-02-01 11:46:42 +01005708 * @user_mask_ptr: user-space pointer to the new CPU mask
Yacine Belkadie69f6182013-07-12 20:45:47 +02005709 *
5710 * Return: 0 on success. An error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005712SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
5713 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714{
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305715 cpumask_var_t new_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 int retval;
5717
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305718 if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
5719 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720
Rusty Russell5a16f3d2008-11-25 02:35:11 +10305721 retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
5722 if (retval == 0)
5723 retval = sched_setaffinity(pid, new_mask);
5724 free_cpumask_var(new_mask);
5725 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726}
5727
Rusty Russell96f874e22008-11-25 02:35:14 +10305728long sched_getaffinity(pid_t pid, struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729{
Ingo Molnar36c8b582006-07-03 00:25:41 -07005730 struct task_struct *p;
Thomas Gleixner31605682009-12-08 20:24:16 +00005731 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005734 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735
5736 retval = -ESRCH;
5737 p = find_process_by_pid(pid);
5738 if (!p)
5739 goto out_unlock;
5740
David Quigleye7834f82006-06-23 02:03:59 -07005741 retval = security_task_getscheduler(p);
5742 if (retval)
5743 goto out_unlock;
5744
Peter Zijlstra013fdb82011-04-05 17:23:45 +02005745 raw_spin_lock_irqsave(&p->pi_lock, flags);
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02005746 cpumask_and(mask, &p->cpus_mask, cpu_active_mask);
Peter Zijlstra013fdb82011-04-05 17:23:45 +02005747 raw_spin_unlock_irqrestore(&p->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748
5749out_unlock:
Thomas Gleixner23f5d142009-12-09 10:15:01 +00005750 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751
Ulrich Drepper9531b622007-08-09 11:16:46 +02005752 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753}
5754
5755/**
Ingo Molnard1ccc662017-02-01 11:46:42 +01005756 * sys_sched_getaffinity - get the CPU affinity of a process
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 * @pid: pid of the process
5758 * @len: length in bytes of the bitmask pointed to by user_mask_ptr
Ingo Molnard1ccc662017-02-01 11:46:42 +01005759 * @user_mask_ptr: user-space pointer to hold the current CPU mask
Yacine Belkadie69f6182013-07-12 20:45:47 +02005760 *
Zev Weiss599b4842016-06-26 16:13:23 -05005761 * Return: size of CPU mask copied to user_mask_ptr on success. An
5762 * error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01005764SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
5765 unsigned long __user *, user_mask_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766{
5767 int ret;
Rusty Russellf17c8602008-11-25 02:35:11 +10305768 cpumask_var_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769
Anton Blanchard84fba5e2010-04-06 17:02:19 +10005770 if ((len * BITS_PER_BYTE) < nr_cpu_ids)
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09005771 return -EINVAL;
5772 if (len & (sizeof(unsigned long)-1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 return -EINVAL;
5774
Rusty Russellf17c8602008-11-25 02:35:11 +10305775 if (!alloc_cpumask_var(&mask, GFP_KERNEL))
5776 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777
Rusty Russellf17c8602008-11-25 02:35:11 +10305778 ret = sched_getaffinity(pid, mask);
5779 if (ret == 0) {
Alexey Dobriyan4de373a2018-02-06 15:39:37 -08005780 unsigned int retlen = min(len, cpumask_size());
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09005781
5782 if (copy_to_user(user_mask_ptr, mask, retlen))
Rusty Russellf17c8602008-11-25 02:35:11 +10305783 ret = -EFAULT;
5784 else
KOSAKI Motohirocd3d8032010-03-12 16:15:36 +09005785 ret = retlen;
Rusty Russellf17c8602008-11-25 02:35:11 +10305786 }
5787 free_cpumask_var(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788
Rusty Russellf17c8602008-11-25 02:35:11 +10305789 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790}
5791
5792/**
5793 * sys_sched_yield - yield the current processor to other threads.
5794 *
Ingo Molnardd41f592007-07-09 18:51:59 +02005795 * This function yields the current CPU to other tasks. If there are no
5796 * other threads running on this CPU then this function will return.
Yacine Belkadie69f6182013-07-12 20:45:47 +02005797 *
5798 * Return: 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 */
Dominik Brodowski7d4dd4f2018-03-14 22:40:35 +01005800static void do_sched_yield(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801{
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02005802 struct rq_flags rf;
5803 struct rq *rq;
5804
Johannes Weiner246b3b32018-10-26 15:06:23 -07005805 rq = this_rq_lock_irq(&rf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806
Josh Poimboeufae928822016-06-17 12:43:24 -05005807 schedstat_inc(rq->yld_count);
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02005808 current->sched_class->yield_task(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809
5810 /*
5811 * Since we are going to call schedule() anyway, there's
5812 * no need to preempt or enable interrupts:
5813 */
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02005814 preempt_disable();
5815 rq_unlock(rq, &rf);
Thomas Gleixnerba74c142011-03-21 13:32:17 +01005816 sched_preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817
5818 schedule();
Dominik Brodowski7d4dd4f2018-03-14 22:40:35 +01005819}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820
Dominik Brodowski7d4dd4f2018-03-14 22:40:35 +01005821SYSCALL_DEFINE0(sched_yield)
5822{
5823 do_sched_yield();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 return 0;
5825}
5826
Thomas Gleixnerc1a280b2019-07-26 23:19:37 +02005827#ifndef CONFIG_PREEMPTION
Herbert Xu02b67cc32008-01-25 21:08:28 +01005828int __sched _cond_resched(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829{
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +03005830 if (should_resched(0)) {
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01005831 preempt_schedule_common();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 return 1;
5833 }
Paul E. McKenneyf79c3ad2016-11-30 06:24:30 -08005834 rcu_all_qs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 return 0;
5836}
Herbert Xu02b67cc32008-01-25 21:08:28 +01005837EXPORT_SYMBOL(_cond_resched);
Peter Zijlstra35a773a2016-09-19 12:57:53 +02005838#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839
5840/*
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02005841 * __cond_resched_lock() - if a reschedule is pending, drop the given lock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 * call schedule, and on return reacquire the lock.
5843 *
Thomas Gleixnerc1a280b2019-07-26 23:19:37 +02005844 * This works OK both with and without CONFIG_PREEMPTION. We do strange low-level
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 * operations here to prevent schedule() from being called twice (once via
5846 * spin_unlock(), once by hand).
5847 */
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02005848int __cond_resched_lock(spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849{
Konstantin Khlebnikovfe32d3c2015-07-15 12:52:04 +03005850 int resched = should_resched(PREEMPT_LOCK_OFFSET);
Jan Kara6df3cec2005-06-13 15:52:32 -07005851 int ret = 0;
5852
Peter Zijlstraf607c662009-07-20 19:16:29 +02005853 lockdep_assert_held(lock);
5854
Paul E. McKenney4a81e832014-06-20 16:49:01 -07005855 if (spin_needbreak(lock) || resched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 spin_unlock(lock);
Peter Zijlstrad86ee482009-07-10 14:57:57 +02005857 if (resched)
Frederic Weisbeckera18b5d02015-01-22 18:08:04 +01005858 preempt_schedule_common();
Nick Piggin95c354f2008-01-30 13:31:20 +01005859 else
5860 cpu_relax();
Jan Kara6df3cec2005-06-13 15:52:32 -07005861 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 spin_lock(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 }
Jan Kara6df3cec2005-06-13 15:52:32 -07005864 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865}
Frederic Weisbecker613afbf2009-07-16 15:44:29 +02005866EXPORT_SYMBOL(__cond_resched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868/**
5869 * yield - yield the current processor to other threads.
5870 *
Peter Zijlstra8e3fabf2012-03-06 18:54:26 +01005871 * Do not ever use this function, there's a 99% chance you're doing it wrong.
5872 *
5873 * The scheduler is at all times free to pick the calling task as the most
5874 * eligible task to run, if removing the yield() call from your code breaks
5875 * it, its already broken.
5876 *
5877 * Typical broken usage is:
5878 *
5879 * while (!event)
Ingo Molnard1ccc662017-02-01 11:46:42 +01005880 * yield();
Peter Zijlstra8e3fabf2012-03-06 18:54:26 +01005881 *
5882 * where one assumes that yield() will let 'the other' process run that will
5883 * make event true. If the current task is a SCHED_FIFO task that will never
5884 * happen. Never use yield() as a progress guarantee!!
5885 *
5886 * If you want to use yield() to wait for something, use wait_event().
5887 * If you want to use yield() to be 'nice' for others, use cond_resched().
5888 * If you still want to use yield(), do not!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 */
5890void __sched yield(void)
5891{
5892 set_current_state(TASK_RUNNING);
Dominik Brodowski7d4dd4f2018-03-14 22:40:35 +01005893 do_sched_yield();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895EXPORT_SYMBOL(yield);
5896
Mike Galbraithd95f4122011-02-01 09:50:51 -05005897/**
5898 * yield_to - yield the current processor to another thread in
5899 * your thread group, or accelerate that thread toward the
5900 * processor it's on.
Randy Dunlap16addf92011-03-18 09:34:53 -07005901 * @p: target task
5902 * @preempt: whether task preemption is allowed or not
Mike Galbraithd95f4122011-02-01 09:50:51 -05005903 *
5904 * It's the caller's job to ensure that the target task struct
5905 * can't go away on us before we can do any checks.
5906 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02005907 * Return:
Peter Zijlstra7b270f62013-01-22 13:09:13 +05305908 * true (>0) if we indeed boosted the target task.
5909 * false (0) if we failed to boost the target.
5910 * -ESRCH if there's no task to yield to.
Mike Galbraithd95f4122011-02-01 09:50:51 -05005911 */
Dan Carpenterfa933842014-05-23 13:20:42 +03005912int __sched yield_to(struct task_struct *p, bool preempt)
Mike Galbraithd95f4122011-02-01 09:50:51 -05005913{
5914 struct task_struct *curr = current;
5915 struct rq *rq, *p_rq;
5916 unsigned long flags;
Dan Carpenterc3c18642013-02-05 14:37:51 +03005917 int yielded = 0;
Mike Galbraithd95f4122011-02-01 09:50:51 -05005918
5919 local_irq_save(flags);
5920 rq = this_rq();
5921
5922again:
5923 p_rq = task_rq(p);
Peter Zijlstra7b270f62013-01-22 13:09:13 +05305924 /*
5925 * If we're the only runnable task on the rq and target rq also
5926 * has only one task, there's absolutely no point in yielding.
5927 */
5928 if (rq->nr_running == 1 && p_rq->nr_running == 1) {
5929 yielded = -ESRCH;
5930 goto out_irq;
5931 }
5932
Mike Galbraithd95f4122011-02-01 09:50:51 -05005933 double_rq_lock(rq, p_rq);
Shigeru Yoshida39e24d8f2013-11-23 18:38:01 +09005934 if (task_rq(p) != p_rq) {
Mike Galbraithd95f4122011-02-01 09:50:51 -05005935 double_rq_unlock(rq, p_rq);
5936 goto again;
5937 }
5938
5939 if (!curr->sched_class->yield_to_task)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05305940 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05005941
5942 if (curr->sched_class != p->sched_class)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05305943 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05005944
5945 if (task_running(p_rq, p) || p->state)
Peter Zijlstra7b270f62013-01-22 13:09:13 +05305946 goto out_unlock;
Mike Galbraithd95f4122011-02-01 09:50:51 -05005947
Dietmar Eggemann0900acf2020-06-03 10:03:02 +02005948 yielded = curr->sched_class->yield_to_task(rq, p);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08005949 if (yielded) {
Josh Poimboeufae928822016-06-17 12:43:24 -05005950 schedstat_inc(rq->yld_count);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08005951 /*
5952 * Make p's CPU reschedule; pick_next_entity takes care of
5953 * fairness.
5954 */
5955 if (preempt && rq != p_rq)
Kirill Tkhai88751252014-06-29 00:03:57 +04005956 resched_curr(p_rq);
Venkatesh Pallipadi6d1cafd2011-03-01 16:28:21 -08005957 }
Mike Galbraithd95f4122011-02-01 09:50:51 -05005958
Peter Zijlstra7b270f62013-01-22 13:09:13 +05305959out_unlock:
Mike Galbraithd95f4122011-02-01 09:50:51 -05005960 double_rq_unlock(rq, p_rq);
Peter Zijlstra7b270f62013-01-22 13:09:13 +05305961out_irq:
Mike Galbraithd95f4122011-02-01 09:50:51 -05005962 local_irq_restore(flags);
5963
Peter Zijlstra7b270f62013-01-22 13:09:13 +05305964 if (yielded > 0)
Mike Galbraithd95f4122011-02-01 09:50:51 -05005965 schedule();
5966
5967 return yielded;
5968}
5969EXPORT_SYMBOL_GPL(yield_to);
5970
Tejun Heo10ab5642016-10-28 12:58:10 -04005971int io_schedule_prepare(void)
5972{
5973 int old_iowait = current->in_iowait;
5974
5975 current->in_iowait = 1;
5976 blk_schedule_flush_plug(current);
5977
5978 return old_iowait;
5979}
5980
5981void io_schedule_finish(int token)
5982{
5983 current->in_iowait = token;
5984}
5985
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986/*
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01005987 * This task is about to go to sleep on IO. Increment rq->nr_iowait so
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 * that process accounting knows that this is a task in IO wait state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990long __sched io_schedule_timeout(long timeout)
5991{
Tejun Heo10ab5642016-10-28 12:58:10 -04005992 int token;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 long ret;
5994
Tejun Heo10ab5642016-10-28 12:58:10 -04005995 token = io_schedule_prepare();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 ret = schedule_timeout(timeout);
Tejun Heo10ab5642016-10-28 12:58:10 -04005997 io_schedule_finish(token);
NeilBrown9cff8ad2015-02-13 15:49:17 +11005998
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 return ret;
6000}
NeilBrown9cff8ad2015-02-13 15:49:17 +11006001EXPORT_SYMBOL(io_schedule_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002
Gao Xiange3b929b2019-06-03 17:13:38 +08006003void __sched io_schedule(void)
Tejun Heo10ab5642016-10-28 12:58:10 -04006004{
6005 int token;
6006
6007 token = io_schedule_prepare();
6008 schedule();
6009 io_schedule_finish(token);
6010}
6011EXPORT_SYMBOL(io_schedule);
6012
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013/**
6014 * sys_sched_get_priority_max - return maximum RT priority.
6015 * @policy: scheduling class.
6016 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02006017 * Return: On success, this syscall returns the maximum
6018 * rt_priority that can be used by a given scheduling class.
6019 * On failure, a negative error code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01006021SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022{
6023 int ret = -EINVAL;
6024
6025 switch (policy) {
6026 case SCHED_FIFO:
6027 case SCHED_RR:
6028 ret = MAX_USER_RT_PRIO-1;
6029 break;
Dario Faggioliaab03e02013-11-28 11:14:43 +01006030 case SCHED_DEADLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08006032 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02006033 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 ret = 0;
6035 break;
6036 }
6037 return ret;
6038}
6039
6040/**
6041 * sys_sched_get_priority_min - return minimum RT priority.
6042 * @policy: scheduling class.
6043 *
Yacine Belkadie69f6182013-07-12 20:45:47 +02006044 * Return: On success, this syscall returns the minimum
6045 * rt_priority that can be used by a given scheduling class.
6046 * On failure, a negative error code is returned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 */
Heiko Carstens5add95d2009-01-14 14:14:08 +01006048SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049{
6050 int ret = -EINVAL;
6051
6052 switch (policy) {
6053 case SCHED_FIFO:
6054 case SCHED_RR:
6055 ret = 1;
6056 break;
Dario Faggioliaab03e02013-11-28 11:14:43 +01006057 case SCHED_DEADLINE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 case SCHED_NORMAL:
Ingo Molnarb0a94992006-01-14 13:20:41 -08006059 case SCHED_BATCH:
Ingo Molnardd41f592007-07-09 18:51:59 +02006060 case SCHED_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 ret = 0;
6062 }
6063 return ret;
6064}
6065
Al Viroabca5fc2017-09-19 18:17:46 -04006066static int sched_rr_get_interval(pid_t pid, struct timespec64 *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067{
Ingo Molnar36c8b582006-07-03 00:25:41 -07006068 struct task_struct *p;
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02006069 unsigned int time_slice;
Peter Zijlstraeb580752015-07-31 21:28:18 +02006070 struct rq_flags rf;
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01006071 struct rq *rq;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006072 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073
6074 if (pid < 0)
Andi Kleen3a5c3592007-10-15 17:00:14 +02006075 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076
6077 retval = -ESRCH;
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00006078 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 p = find_process_by_pid(pid);
6080 if (!p)
6081 goto out_unlock;
6082
6083 retval = security_task_getscheduler(p);
6084 if (retval)
6085 goto out_unlock;
6086
Peter Zijlstraeb580752015-07-31 21:28:18 +02006087 rq = task_rq_lock(p, &rf);
Peter Zijlstraa57beec2014-01-27 11:54:13 +01006088 time_slice = 0;
6089 if (p->sched_class->get_rr_interval)
6090 time_slice = p->sched_class->get_rr_interval(rq, p);
Peter Zijlstraeb580752015-07-31 21:28:18 +02006091 task_rq_unlock(rq, p, &rf);
Dmitry Adamushkoa4ec24b2007-10-15 17:00:13 +02006092
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00006093 rcu_read_unlock();
Al Viroabca5fc2017-09-19 18:17:46 -04006094 jiffies_to_timespec64(time_slice, t);
6095 return 0;
Andi Kleen3a5c3592007-10-15 17:00:14 +02006096
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097out_unlock:
Thomas Gleixner1a551ae2009-12-09 10:15:11 +00006098 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 return retval;
6100}
6101
Randy Dunlap2064a5a2017-12-03 13:19:00 -08006102/**
6103 * sys_sched_rr_get_interval - return the default timeslice of a process.
6104 * @pid: pid of the process.
6105 * @interval: userspace pointer to the timeslice value.
6106 *
6107 * this syscall writes the default timeslice value of a given process
6108 * into the user-space timespec buffer. A value of '0' means infinity.
6109 *
6110 * Return: On success, 0 and the timeslice is in @interval. Otherwise,
6111 * an error code.
6112 */
Al Viroabca5fc2017-09-19 18:17:46 -04006113SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
Arnd Bergmann474b9c72018-04-17 21:59:47 +02006114 struct __kernel_timespec __user *, interval)
Al Viroabca5fc2017-09-19 18:17:46 -04006115{
6116 struct timespec64 t;
6117 int retval = sched_rr_get_interval(pid, &t);
6118
6119 if (retval == 0)
6120 retval = put_timespec64(&t, interval);
6121
6122 return retval;
6123}
6124
Arnd Bergmann474b9c72018-04-17 21:59:47 +02006125#ifdef CONFIG_COMPAT_32BIT_TIME
Arnd Bergmann8dabe722019-01-07 00:33:08 +01006126SYSCALL_DEFINE2(sched_rr_get_interval_time32, pid_t, pid,
6127 struct old_timespec32 __user *, interval)
Al Viroabca5fc2017-09-19 18:17:46 -04006128{
6129 struct timespec64 t;
6130 int retval = sched_rr_get_interval(pid, &t);
6131
6132 if (retval == 0)
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +02006133 retval = put_old_timespec32(&t, interval);
Al Viroabca5fc2017-09-19 18:17:46 -04006134 return retval;
6135}
6136#endif
6137
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01006138void sched_show_task(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 unsigned long free = 0;
Paul E. McKenney4e797522012-11-07 13:35:32 -08006141 int ppid;
Ingo Molnarc930b2c2017-02-03 12:22:54 +01006142
Tetsuo Handa38200502016-11-02 19:50:29 +09006143 if (!try_get_task_stack(p))
6144 return;
Xie XiuQi20435d82017-08-07 16:44:23 +08006145
6146 printk(KERN_INFO "%-15.15s %c", p->comm, task_state_to_char(p));
6147
6148 if (p->state == TASK_RUNNING)
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006149 printk(KERN_CONT " running task ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150#ifdef CONFIG_DEBUG_STACK_USAGE
Eric Sandeen7c9f8862008-04-22 16:38:23 -05006151 free = stack_not_used(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152#endif
Oleg Nesterova90e9842014-12-10 15:45:21 -08006153 ppid = 0;
Paul E. McKenney4e797522012-11-07 13:35:32 -08006154 rcu_read_lock();
Oleg Nesterova90e9842014-12-10 15:45:21 -08006155 if (pid_alive(p))
6156 ppid = task_pid_nr(rcu_dereference(p->real_parent));
Paul E. McKenney4e797522012-11-07 13:35:32 -08006157 rcu_read_unlock();
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006158 printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free,
Paul E. McKenney4e797522012-11-07 13:35:32 -08006159 task_pid_nr(p), ppid,
David Rientjesaa47b7e2009-05-04 01:38:05 -07006160 (unsigned long)task_thread_info(p)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161
Tejun Heo3d1cb202013-04-30 15:27:22 -07006162 print_worker_info(KERN_INFO, p);
Dmitry Safonov9cb8f062020-06-08 21:32:29 -07006163 show_stack(p, NULL, KERN_INFO);
Tetsuo Handa38200502016-11-02 19:50:29 +09006164 put_task_stack(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165}
Paul E. McKenney0032f4e2017-08-30 10:40:17 -07006166EXPORT_SYMBOL_GPL(sched_show_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167
Peter Zijlstra5d68cc92017-09-22 18:32:41 +02006168static inline bool
6169state_filter_match(unsigned long state_filter, struct task_struct *p)
6170{
6171 /* no filter, everything matches */
6172 if (!state_filter)
6173 return true;
6174
6175 /* filter, but doesn't match */
6176 if (!(p->state & state_filter))
6177 return false;
6178
6179 /*
6180 * When looking for TASK_UNINTERRUPTIBLE skip TASK_IDLE (allows
6181 * TASK_KILLABLE).
6182 */
6183 if (state_filter == TASK_UNINTERRUPTIBLE && p->state == TASK_IDLE)
6184 return false;
6185
6186 return true;
6187}
6188
6189
Ingo Molnare59e2ae2006-12-06 20:35:59 -08006190void show_state_filter(unsigned long state_filter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191{
Ingo Molnar36c8b582006-07-03 00:25:41 -07006192 struct task_struct *g, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193
Ingo Molnar4bd77322007-07-11 21:21:47 +02006194#if BITS_PER_LONG == 32
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006195 printk(KERN_INFO
6196 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197#else
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006198 printk(KERN_INFO
6199 " task PC stack pid father\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200#endif
Thomas Gleixner510f5ac2011-07-17 20:47:54 +02006201 rcu_read_lock();
Oleg Nesterov5d07f422014-08-13 21:19:53 +02006202 for_each_process_thread(g, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 /*
6204 * reset the NMI-timeout, listing all files on a slow
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006205 * console might take a lot of time:
Andrey Ryabinin57675cb2016-06-09 15:20:05 +03006206 * Also, reset softlockup watchdogs on all CPUs, because
6207 * another CPU might be blocked waiting for us to process
6208 * an IPI.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209 */
6210 touch_nmi_watchdog();
Andrey Ryabinin57675cb2016-06-09 15:20:05 +03006211 touch_all_softlockup_watchdogs();
Peter Zijlstra5d68cc92017-09-22 18:32:41 +02006212 if (state_filter_match(state_filter, p))
Ingo Molnar82a1fcb2008-01-25 21:08:02 +01006213 sched_show_task(p);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02006214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215
Ingo Molnardd41f592007-07-09 18:51:59 +02006216#ifdef CONFIG_SCHED_DEBUG
Rabin Vincentfb90a6e2016-04-04 15:42:02 +02006217 if (!state_filter)
6218 sysrq_sched_debug_show();
Ingo Molnardd41f592007-07-09 18:51:59 +02006219#endif
Thomas Gleixner510f5ac2011-07-17 20:47:54 +02006220 rcu_read_unlock();
Ingo Molnare59e2ae2006-12-06 20:35:59 -08006221 /*
6222 * Only show locks if all tasks are dumped:
6223 */
Shmulik Ladkani93335a22009-11-25 15:23:41 +02006224 if (!state_filter)
Ingo Molnare59e2ae2006-12-06 20:35:59 -08006225 debug_show_all_locks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226}
6227
Ingo Molnarf340c0d2005-06-28 16:40:42 +02006228/**
6229 * init_idle - set up an idle thread for a given CPU
6230 * @idle: task in question
Ingo Molnard1ccc662017-02-01 11:46:42 +01006231 * @cpu: CPU the idle task belongs to
Ingo Molnarf340c0d2005-06-28 16:40:42 +02006232 *
6233 * NOTE: this function does not set the idle thread's NEED_RESCHED
6234 * flag, to make booting more robust.
6235 */
Paul Gortmaker0db06282013-06-19 14:53:51 -04006236void init_idle(struct task_struct *idle, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237{
Ingo Molnar70b97a72006-07-03 00:25:42 -07006238 struct rq *rq = cpu_rq(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 unsigned long flags;
6240
Peter Zijlstraff51ff82019-10-01 11:18:37 +02006241 __sched_fork(0, idle);
6242
Peter Zijlstra25834c72015-05-15 17:43:34 +02006243 raw_spin_lock_irqsave(&idle->pi_lock, flags);
6244 raw_spin_lock(&rq->lock);
Ingo Molnar5cbd54e2008-11-12 20:05:50 +01006245
Peter Zijlstra06b83b52009-12-16 18:04:35 +01006246 idle->state = TASK_RUNNING;
Ingo Molnardd41f592007-07-09 18:51:59 +02006247 idle->se.exec_start = sched_clock();
Peter Zijlstrac1de45c2016-11-28 23:03:05 -08006248 idle->flags |= PF_IDLE;
Ingo Molnardd41f592007-07-09 18:51:59 +02006249
Sami Tolvanend08b9f02020-04-27 09:00:07 -07006250 scs_task_reset(idle);
Mark Rutlande1b77c92016-03-09 14:08:18 -08006251 kasan_unpoison_task_stack(idle);
6252
Peter Zijlstrade9b8f52015-08-13 23:09:29 +02006253#ifdef CONFIG_SMP
6254 /*
6255 * Its possible that init_idle() gets called multiple times on a task,
6256 * in that case do_set_cpus_allowed() will not do the right thing.
6257 *
6258 * And since this is boot we can forgo the serialization.
6259 */
6260 set_cpus_allowed_common(idle, cpumask_of(cpu));
6261#endif
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02006262 /*
6263 * We're having a chicken and egg problem, even though we are
Ingo Molnard1ccc662017-02-01 11:46:42 +01006264 * holding rq->lock, the CPU isn't yet set to this CPU so the
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02006265 * lockdep check in task_group() will fail.
6266 *
6267 * Similar case to sched_fork(). / Alternatively we could
6268 * use task_rq_lock() here and obtain the other rq->lock.
6269 *
6270 * Silence PROVE_RCU
6271 */
6272 rcu_read_lock();
Ingo Molnardd41f592007-07-09 18:51:59 +02006273 __set_task_cpu(idle, cpu);
Peter Zijlstra6506cf6c2010-09-16 17:50:31 +02006274 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275
Eric W. Biederman5311a982019-09-14 07:35:02 -05006276 rq->idle = idle;
6277 rcu_assign_pointer(rq->curr, idle);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04006278 idle->on_rq = TASK_ON_RQ_QUEUED;
Peter Zijlstrade9b8f52015-08-13 23:09:29 +02006279#ifdef CONFIG_SMP
Peter Zijlstra3ca7a442011-04-05 17:23:40 +02006280 idle->on_cpu = 1;
Nick Piggin4866cde2005-06-25 14:57:23 -07006281#endif
Peter Zijlstra25834c72015-05-15 17:43:34 +02006282 raw_spin_unlock(&rq->lock);
6283 raw_spin_unlock_irqrestore(&idle->pi_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284
6285 /* Set the preempt count _outside_ the spinlocks! */
Peter Zijlstra01028742013-08-14 14:55:46 +02006286 init_idle_preempt_count(idle, cpu);
Jonathan Corbet625f2a32011-04-22 11:19:10 -06006287
Ingo Molnardd41f592007-07-09 18:51:59 +02006288 /*
6289 * The idle tasks have their own, simple scheduling class:
6290 */
6291 idle->sched_class = &idle_sched_class;
Steven Rostedt868baf02011-02-10 21:26:13 -05006292 ftrace_graph_init_idle_task(idle, cpu);
Frederic Weisbecker45eacc62013-05-15 22:16:32 +02006293 vtime_init_idle(idle, cpu);
Peter Zijlstrade9b8f52015-08-13 23:09:29 +02006294#ifdef CONFIG_SMP
Carsten Emdef1c6f1a2011-10-26 23:14:16 +02006295 sprintf(idle->comm, "%s/%d", INIT_TASK_COMM, cpu);
6296#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297}
6298
Nicolas Pitree1d4eee2017-06-14 13:19:23 -04006299#ifdef CONFIG_SMP
6300
Juri Lellif82f8042014-10-07 09:52:11 +01006301int cpuset_cpumask_can_shrink(const struct cpumask *cur,
6302 const struct cpumask *trial)
6303{
Nicolas Pitre06a76fe2017-06-21 14:22:01 -04006304 int ret = 1;
Juri Lellif82f8042014-10-07 09:52:11 +01006305
Mike Galbraithbb2bc552015-01-28 04:53:55 +01006306 if (!cpumask_weight(cur))
6307 return ret;
6308
Nicolas Pitre06a76fe2017-06-21 14:22:01 -04006309 ret = dl_cpuset_cpumask_can_shrink(cur, trial);
Juri Lellif82f8042014-10-07 09:52:11 +01006310
6311 return ret;
6312}
6313
Juri Lelli7f514122014-09-19 10:22:40 +01006314int task_can_attach(struct task_struct *p,
6315 const struct cpumask *cs_cpus_allowed)
6316{
6317 int ret = 0;
6318
6319 /*
6320 * Kthreads which disallow setaffinity shouldn't be moved
Ingo Molnard1ccc662017-02-01 11:46:42 +01006321 * to a new cpuset; we don't want to change their CPU
Juri Lelli7f514122014-09-19 10:22:40 +01006322 * affinity and isolating such threads by their set of
6323 * allowed nodes is unnecessary. Thus, cpusets are not
6324 * applicable for such threads. This prevents checking for
6325 * success of set_cpus_allowed_ptr() on all attached tasks
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02006326 * before cpus_mask may be changed.
Juri Lelli7f514122014-09-19 10:22:40 +01006327 */
6328 if (p->flags & PF_NO_SETAFFINITY) {
6329 ret = -EINVAL;
6330 goto out;
6331 }
6332
Juri Lelli7f514122014-09-19 10:22:40 +01006333 if (dl_task(p) && !cpumask_intersects(task_rq(p)->rd->span,
Nicolas Pitre06a76fe2017-06-21 14:22:01 -04006334 cs_cpus_allowed))
6335 ret = dl_task_can_attach(p, cs_cpus_allowed);
Juri Lelli7f514122014-09-19 10:22:40 +01006336
Juri Lelli7f514122014-09-19 10:22:40 +01006337out:
6338 return ret;
6339}
6340
Ingo Molnarf2cb1362017-02-01 13:10:18 +01006341bool sched_smp_initialized __read_mostly;
Thomas Gleixnere26fbff2016-03-10 12:54:10 +01006342
Mel Gormane6628d52013-10-07 11:29:02 +01006343#ifdef CONFIG_NUMA_BALANCING
6344/* Migrate current task p to target_cpu */
6345int migrate_task_to(struct task_struct *p, int target_cpu)
6346{
6347 struct migration_arg arg = { p, target_cpu };
6348 int curr_cpu = task_cpu(p);
6349
6350 if (curr_cpu == target_cpu)
6351 return 0;
6352
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02006353 if (!cpumask_test_cpu(target_cpu, p->cpus_ptr))
Mel Gormane6628d52013-10-07 11:29:02 +01006354 return -EINVAL;
6355
6356 /* TODO: This is not properly updating schedstats */
6357
Mel Gorman286549d2014-01-21 15:51:03 -08006358 trace_sched_move_numa(p, curr_cpu, target_cpu);
Mel Gormane6628d52013-10-07 11:29:02 +01006359 return stop_one_cpu(curr_cpu, migration_cpu_stop, &arg);
6360}
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006361
6362/*
6363 * Requeue a task on a given node and accurately track the number of NUMA
6364 * tasks on the runqueues
6365 */
6366void sched_setnuma(struct task_struct *p, int nid)
6367{
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04006368 bool queued, running;
Peter Zijlstraeb580752015-07-31 21:28:18 +02006369 struct rq_flags rf;
6370 struct rq *rq;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006371
Peter Zijlstraeb580752015-07-31 21:28:18 +02006372 rq = task_rq_lock(p, &rf);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04006373 queued = task_on_rq_queued(p);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006374 running = task_current(rq, p);
6375
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04006376 if (queued)
Peter Zijlstra1de64442015-09-30 17:44:13 +02006377 dequeue_task(rq, p, DEQUEUE_SAVE);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006378 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04006379 put_prev_task(rq, p);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006380
6381 p->numa_preferred_nid = nid;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006382
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04006383 if (queued)
Peter Zijlstra7134b3e2017-02-21 14:23:38 +01006384 enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK);
Vincent Guittota399d232016-09-12 09:47:52 +02006385 if (running)
Peter Zijlstra03b7fad2019-05-29 20:36:41 +00006386 set_next_task(rq, p);
Peter Zijlstraeb580752015-07-31 21:28:18 +02006387 task_rq_unlock(rq, p, &rf);
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01006388}
Peter Zijlstra5cc389b2015-06-11 14:46:50 +02006389#endif /* CONFIG_NUMA_BALANCING */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390
6391#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar48f24c42006-07-03 00:25:40 -07006392/*
Ingo Molnard1ccc662017-02-01 11:46:42 +01006393 * Ensure that the idle task is using init_mm right before its CPU goes
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394 * offline.
6395 */
6396void idle_task_exit(void)
6397{
6398 struct mm_struct *mm = current->active_mm;
6399
6400 BUG_ON(cpu_online(smp_processor_id()));
Peter Zijlstrabf2c59f2020-04-01 17:40:33 -04006401 BUG_ON(current != this_rq()->idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402
Martin Schwidefskya53efe52012-10-26 17:17:44 +02006403 if (mm != &init_mm) {
Andy Lutomirski252d2a42017-06-09 11:49:15 -07006404 switch_mm(mm, &init_mm, current);
Martin Schwidefskya53efe52012-10-26 17:17:44 +02006405 finish_arch_post_lock_switch();
6406 }
Peter Zijlstrabf2c59f2020-04-01 17:40:33 -04006407
6408 /* finish_cpu(), as ran on the BP, will clean up the active_mm state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409}
6410
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006411/*
Peter Zijlstra5d180232012-08-20 11:26:57 +02006412 * Since this CPU is going 'away' for a while, fold any nr_active delta
6413 * we might have. Assumes we're called after migrate_tasks() so that the
Thomas Gleixnerd60585c2016-07-12 18:33:56 +02006414 * nr_active count is stable. We need to take the teardown thread which
6415 * is calling this into account, so we hand in adjust = 1 to the load
6416 * calculation.
Peter Zijlstra5d180232012-08-20 11:26:57 +02006417 *
6418 * Also see the comment "Global load-average calculations".
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006419 */
Peter Zijlstra5d180232012-08-20 11:26:57 +02006420static void calc_load_migrate(struct rq *rq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421{
Thomas Gleixnerd60585c2016-07-12 18:33:56 +02006422 long delta = calc_load_fold_active(rq, 1);
Peter Zijlstra5d180232012-08-20 11:26:57 +02006423 if (delta)
6424 atomic_long_add(delta, &calc_load_tasks);
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02006425}
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006426
Peter Zijlstra10e70712019-08-06 15:13:17 +02006427static struct task_struct *__pick_migrate_task(struct rq *rq)
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01006428{
Peter Zijlstra10e70712019-08-06 15:13:17 +02006429 const struct sched_class *class;
6430 struct task_struct *next;
6431
6432 for_each_class(class) {
Peter Zijlstra98c2f702019-11-08 14:15:58 +01006433 next = class->pick_next_task(rq);
Peter Zijlstra10e70712019-08-06 15:13:17 +02006434 if (next) {
Peter Zijlstra6e2df052019-11-08 11:11:52 +01006435 next->sched_class->put_prev_task(rq, next);
Peter Zijlstra10e70712019-08-06 15:13:17 +02006436 return next;
6437 }
6438 }
6439
6440 /* The idle class should always have a runnable task */
6441 BUG();
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01006442}
6443
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006444/*
6445 * Migrate all tasks from the rq, sleeping tasks will be migrated by
6446 * try_to_wake_up()->select_task_rq().
6447 *
6448 * Called with rq->lock held even though we'er in stop_machine() and
6449 * there's no concurrency possible, we hold the required locks anyway
6450 * because of lock validation efforts.
6451 */
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006452static void migrate_tasks(struct rq *dead_rq, struct rq_flags *rf)
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006453{
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02006454 struct rq *rq = dead_rq;
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006455 struct task_struct *next, *stop = rq->stop;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006456 struct rq_flags orf = *rf;
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006457 int dest_cpu;
6458
6459 /*
6460 * Fudge the rq selection such that the below task selection loop
6461 * doesn't get stuck on the currently eligible stop task.
6462 *
6463 * We're currently inside stop_machine() and the rq is either stuck
6464 * in the stop_machine_cpu_stop() loop, or we're executing this code,
6465 * either way we should never end up calling schedule() until we're
6466 * done here.
6467 */
6468 rq->stop = NULL;
6469
Frederic Weisbecker77bd3972013-04-12 01:50:58 +02006470 /*
6471 * put_prev_task() and pick_next_task() sched
6472 * class method both need to have an up-to-date
6473 * value of rq->clock[_task]
6474 */
6475 update_rq_clock(rq);
6476
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02006477 for (;;) {
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006478 /*
6479 * There's this thread running, bail when that's the only
Ingo Molnard1ccc662017-02-01 11:46:42 +01006480 * remaining thread:
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006481 */
6482 if (rq->nr_running == 1)
6483 break;
6484
Peter Zijlstra10e70712019-08-06 15:13:17 +02006485 next = __pick_migrate_task(rq);
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006486
Wanpeng Li5473e0cc2015-08-28 14:55:56 +08006487 /*
Sebastian Andrzej Siewior3bd37062019-04-23 16:26:36 +02006488 * Rules for changing task_struct::cpus_mask are holding
Wanpeng Li5473e0cc2015-08-28 14:55:56 +08006489 * both pi_lock and rq->lock, such that holding either
6490 * stabilizes the mask.
6491 *
6492 * Drop rq->lock is not quite as disastrous as it usually is
6493 * because !cpu_active at this point, which means load-balance
6494 * will not interfere. Also, stop-machine.
6495 */
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006496 rq_unlock(rq, rf);
Wanpeng Li5473e0cc2015-08-28 14:55:56 +08006497 raw_spin_lock(&next->pi_lock);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006498 rq_relock(rq, rf);
Wanpeng Li5473e0cc2015-08-28 14:55:56 +08006499
6500 /*
6501 * Since we're inside stop-machine, _nothing_ should have
6502 * changed the task, WARN if weird stuff happened, because in
6503 * that case the above rq->lock drop is a fail too.
6504 */
6505 if (WARN_ON(task_rq(next) != rq || !task_on_rq_queued(next))) {
6506 raw_spin_unlock(&next->pi_lock);
6507 continue;
6508 }
6509
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006510 /* Find suitable destination for @next, with force if needed. */
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02006511 dest_cpu = select_fallback_rq(dead_rq->cpu, next);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006512 rq = __migrate_task(rq, rf, next, dest_cpu);
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02006513 if (rq != dead_rq) {
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006514 rq_unlock(rq, rf);
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02006515 rq = dead_rq;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006516 *rf = orf;
6517 rq_relock(rq, rf);
Peter Zijlstra5e16bbc2015-06-11 14:46:51 +02006518 }
Wanpeng Li5473e0cc2015-08-28 14:55:56 +08006519 raw_spin_unlock(&next->pi_lock);
Peter Zijlstra48c5ccae2010-11-13 19:32:29 +01006520 }
6521
6522 rq->stop = stop;
6523}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524#endif /* CONFIG_HOTPLUG_CPU */
6525
Ingo Molnarf2cb1362017-02-01 13:10:18 +01006526void set_rq_online(struct rq *rq)
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006527{
6528 if (!rq->online) {
6529 const struct sched_class *class;
6530
Rusty Russellc6c49272008-11-25 02:35:05 +10306531 cpumask_set_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006532 rq->online = 1;
6533
6534 for_each_class(class) {
6535 if (class->rq_online)
6536 class->rq_online(rq);
6537 }
6538 }
6539}
6540
Ingo Molnarf2cb1362017-02-01 13:10:18 +01006541void set_rq_offline(struct rq *rq)
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006542{
6543 if (rq->online) {
6544 const struct sched_class *class;
6545
6546 for_each_class(class) {
6547 if (class->rq_offline)
6548 class->rq_offline(rq);
6549 }
6550
Rusty Russellc6c49272008-11-25 02:35:05 +10306551 cpumask_clear_cpu(rq->cpu, rq->rd->online);
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006552 rq->online = 0;
6553 }
6554}
6555
Ingo Molnard1ccc662017-02-01 11:46:42 +01006556/*
6557 * used to mark begin/end of suspend/resume:
6558 */
6559static int num_cpus_frozen;
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05306560
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561/*
Tejun Heo3a101d02010-06-08 21:40:36 +02006562 * Update cpusets according to cpu_active mask. If cpusets are
6563 * disabled, cpuset_update_active_cpus() becomes a simple wrapper
6564 * around partition_sched_domains().
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05306565 *
6566 * If we come here as part of a suspend/resume, don't touch cpusets because we
6567 * want to restore it back to its original state upon resume anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 */
Thomas Gleixner40190a72016-03-10 12:54:13 +01006569static void cpuset_cpu_active(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570{
Thomas Gleixner40190a72016-03-10 12:54:13 +01006571 if (cpuhp_tasks_frozen) {
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05306572 /*
6573 * num_cpus_frozen tracks how many CPUs are involved in suspend
6574 * resume sequence. As long as this is not the last online
6575 * operation in the resume sequence, just build a single sched
6576 * domain, ignoring cpusets.
6577 */
Peter Zijlstra50e76632017-09-07 11:13:38 +02006578 partition_sched_domains(1, NULL, NULL);
6579 if (--num_cpus_frozen)
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006580 return;
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05306581 /*
6582 * This is the last CPU online operation. So fall through and
6583 * restore the original sched domains by considering the
6584 * cpuset configurations.
6585 */
Peter Zijlstra50e76632017-09-07 11:13:38 +02006586 cpuset_force_rebuild();
Max Krasnyanskye761b772008-07-15 04:43:49 -07006587 }
Rakib Mullick30e03ac2017-04-09 07:36:14 +06006588 cpuset_update_active_cpus();
Max Krasnyanskye761b772008-07-15 04:43:49 -07006589}
Tejun Heo3a101d02010-06-08 21:40:36 +02006590
Thomas Gleixner40190a72016-03-10 12:54:13 +01006591static int cpuset_cpu_inactive(unsigned int cpu)
Tejun Heo3a101d02010-06-08 21:40:36 +02006592{
Thomas Gleixner40190a72016-03-10 12:54:13 +01006593 if (!cpuhp_tasks_frozen) {
Nicolas Pitre06a76fe2017-06-21 14:22:01 -04006594 if (dl_cpu_busy(cpu))
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006595 return -EBUSY;
Rakib Mullick30e03ac2017-04-09 07:36:14 +06006596 cpuset_update_active_cpus();
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006597 } else {
Srivatsa S. Bhatd35be8b2012-05-24 19:46:26 +05306598 num_cpus_frozen++;
6599 partition_sched_domains(1, NULL, NULL);
Tejun Heo3a101d02010-06-08 21:40:36 +02006600 }
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006601 return 0;
Tejun Heo3a101d02010-06-08 21:40:36 +02006602}
Max Krasnyanskye761b772008-07-15 04:43:49 -07006603
Thomas Gleixner40190a72016-03-10 12:54:13 +01006604int sched_cpu_activate(unsigned int cpu)
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006605{
Thomas Gleixner7d976692016-03-10 12:54:17 +01006606 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006607 struct rq_flags rf;
Thomas Gleixner7d976692016-03-10 12:54:17 +01006608
Peter Zijlstraba2591a2018-05-29 16:43:46 +02006609#ifdef CONFIG_SCHED_SMT
6610 /*
Peter Zijlstra (Intel)c5511d02018-11-25 19:33:36 +01006611 * When going up, increment the number of cores with SMT present.
Peter Zijlstraba2591a2018-05-29 16:43:46 +02006612 */
Peter Zijlstra (Intel)c5511d02018-11-25 19:33:36 +01006613 if (cpumask_weight(cpu_smt_mask(cpu)) == 2)
6614 static_branch_inc_cpuslocked(&sched_smt_present);
Peter Zijlstraba2591a2018-05-29 16:43:46 +02006615#endif
Thomas Gleixner40190a72016-03-10 12:54:13 +01006616 set_cpu_active(cpu, true);
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006617
Thomas Gleixner40190a72016-03-10 12:54:13 +01006618 if (sched_smp_initialized) {
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006619 sched_domains_numa_masks_set(cpu);
Thomas Gleixner40190a72016-03-10 12:54:13 +01006620 cpuset_cpu_active();
Nick Piggin5c1e1762006-10-03 01:14:04 -07006621 }
Thomas Gleixner7d976692016-03-10 12:54:17 +01006622
6623 /*
6624 * Put the rq online, if not already. This happens:
6625 *
6626 * 1) In the early boot process, because we build the real domains
Ingo Molnard1ccc662017-02-01 11:46:42 +01006627 * after all CPUs have been brought up.
Thomas Gleixner7d976692016-03-10 12:54:17 +01006628 *
6629 * 2) At runtime, if cpuset_cpu_active() fails to rebuild the
6630 * domains.
6631 */
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006632 rq_lock_irqsave(rq, &rf);
Thomas Gleixner7d976692016-03-10 12:54:17 +01006633 if (rq->rd) {
6634 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
6635 set_rq_online(rq);
6636 }
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006637 rq_unlock_irqrestore(rq, &rf);
Thomas Gleixner7d976692016-03-10 12:54:17 +01006638
Thomas Gleixner40190a72016-03-10 12:54:13 +01006639 return 0;
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006640}
6641
Thomas Gleixner40190a72016-03-10 12:54:13 +01006642int sched_cpu_deactivate(unsigned int cpu)
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006643{
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006644 int ret;
6645
Thomas Gleixner40190a72016-03-10 12:54:13 +01006646 set_cpu_active(cpu, false);
Peter Zijlstrab2454ca2016-03-10 12:54:14 +01006647 /*
6648 * We've cleared cpu_active_mask, wait for all preempt-disabled and RCU
6649 * users of this state to go away such that all new such users will
6650 * observe it.
6651 *
Peter Zijlstrab2454ca2016-03-10 12:54:14 +01006652 * Do sync before park smpboot threads to take care the rcu boost case.
6653 */
Paul E. McKenney309ba852018-07-11 14:36:49 -07006654 synchronize_rcu();
Thomas Gleixner40190a72016-03-10 12:54:13 +01006655
Peter Zijlstra (Intel)c5511d02018-11-25 19:33:36 +01006656#ifdef CONFIG_SCHED_SMT
6657 /*
6658 * When going down, decrement the number of cores with SMT present.
6659 */
6660 if (cpumask_weight(cpu_smt_mask(cpu)) == 2)
6661 static_branch_dec_cpuslocked(&sched_smt_present);
6662#endif
6663
Thomas Gleixner40190a72016-03-10 12:54:13 +01006664 if (!sched_smp_initialized)
6665 return 0;
6666
6667 ret = cpuset_cpu_inactive(cpu);
6668 if (ret) {
6669 set_cpu_active(cpu, true);
6670 return ret;
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006671 }
Thomas Gleixner40190a72016-03-10 12:54:13 +01006672 sched_domains_numa_masks_clear(cpu);
6673 return 0;
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006674}
6675
Thomas Gleixner94baf7a2016-03-10 12:54:15 +01006676static void sched_rq_cpu_starting(unsigned int cpu)
6677{
6678 struct rq *rq = cpu_rq(cpu);
6679
6680 rq->calc_load_update = calc_load_update;
Thomas Gleixner94baf7a2016-03-10 12:54:15 +01006681 update_max_interval();
6682}
6683
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006684int sched_cpu_starting(unsigned int cpu)
6685{
Thomas Gleixner94baf7a2016-03-10 12:54:15 +01006686 sched_rq_cpu_starting(cpu);
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01006687 sched_tick_start(cpu);
Thomas Gleixner135fb3e2016-03-10 12:54:11 +01006688 return 0;
Peter Zijlstra6f505b12008-01-25 21:08:30 +01006689}
6690
Thomas Gleixnerf2785dd2016-03-10 12:54:18 +01006691#ifdef CONFIG_HOTPLUG_CPU
6692int sched_cpu_dying(unsigned int cpu)
6693{
6694 struct rq *rq = cpu_rq(cpu);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006695 struct rq_flags rf;
Thomas Gleixnerf2785dd2016-03-10 12:54:18 +01006696
6697 /* Handle pending wakeups and then migrate everything off */
Frederic Weisbeckerd84b3132018-02-21 05:17:27 +01006698 sched_tick_stop(cpu);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006699
6700 rq_lock_irqsave(rq, &rf);
Thomas Gleixnerf2785dd2016-03-10 12:54:18 +01006701 if (rq->rd) {
6702 BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
6703 set_rq_offline(rq);
6704 }
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006705 migrate_tasks(rq, &rf);
Thomas Gleixnerf2785dd2016-03-10 12:54:18 +01006706 BUG_ON(rq->nr_running != 1);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02006707 rq_unlock_irqrestore(rq, &rf);
6708
Thomas Gleixnerf2785dd2016-03-10 12:54:18 +01006709 calc_load_migrate(rq);
6710 update_max_interval();
Peter Zijlstra00357f52017-12-21 15:06:50 +01006711 nohz_balance_exit_idle(rq);
Thomas Gleixnere5ef27d2016-03-10 12:54:21 +01006712 hrtick_clear(rq);
Thomas Gleixnerf2785dd2016-03-10 12:54:18 +01006713 return 0;
6714}
6715#endif
6716
Peter Zijlstra6f505b12008-01-25 21:08:30 +01006717void __init sched_init_smp(void)
6718{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719 sched_init_numa();
Christoph Lameter476f3532007-05-06 14:48:58 -07006720
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02006721 /*
6722 * There's no userspace yet to cause hotplug operations; hence all the
Ingo Molnard1ccc662017-02-01 11:46:42 +01006723 * CPU masks are stable and all blatant races in the below code cannot
Valentin Schneiderb5a4e2b2018-12-19 18:23:16 +00006724 * happen.
Peter Zijlstra6acce3e2013-10-11 14:38:20 +02006725 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726 mutex_lock(&sched_domains_mutex);
Peter Zijlstra8d5dc512017-04-25 15:29:40 +02006727 sched_init_domains(cpu_active_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728 mutex_unlock(&sched_domains_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729
Mike Travis434d53b2008-04-04 18:11:04 -07006730 /* Move init over to a non-isolated CPU */
Frederic Weisbeckeredb93822017-10-27 04:42:37 +02006731 if (set_cpus_allowed_ptr(current, housekeeping_cpumask(HK_FLAG_DOMAIN)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732 BUG();
6733 sched_init_granularity();
Rusty Russell42128232008-11-25 02:35:12 +10306734
Rusty Russell0e3900e2008-11-25 02:35:13 +10306735 init_sched_rt_class();
Juri Lelli1baca4c2013-11-07 14:43:38 +01006736 init_sched_dl_class();
Peter Zijlstra1b568f02016-05-09 10:38:41 +02006737
Thomas Gleixnere26fbff2016-03-10 12:54:10 +01006738 sched_smp_initialized = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739}
Thomas Gleixnere26fbff2016-03-10 12:54:10 +01006740
6741static int __init migration_init(void)
6742{
Nicholas Piggin77a53522019-04-11 13:34:44 +10006743 sched_cpu_starting(smp_processor_id());
Thomas Gleixnere26fbff2016-03-10 12:54:10 +01006744 return 0;
6745}
6746early_initcall(migration_init);
6747
Ingo Molnardd41f592007-07-09 18:51:59 +02006748#else
6749void __init sched_init_smp(void)
6750{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751 sched_init_granularity();
6752}
Peter Williams2dd73a42006-06-27 02:54:34 -07006753#endif /* CONFIG_SMP */
Heiko Carstensb50f60c2006-07-30 03:03:52 -07006754
Avi Kivitye107be32007-07-26 13:40:43 +02006755int in_sched_functions(unsigned long addr)
6756{
6757 return in_lock_functions(addr) ||
6758 (addr >= (unsigned long)__sched_text_start
Christoph Lameterc9819f42006-12-10 02:20:25 -08006759 && addr < (unsigned long)__sched_text_end);
Christoph Lameter476f3532007-05-06 14:48:58 -07006760}
Christoph Lameterc9819f42006-12-10 02:20:25 -08006761
Peter Zijlstra029632f2011-10-25 10:00:11 +02006762#ifdef CONFIG_CGROUP_SCHED
Li Zefan27b4b932013-03-05 16:07:52 +08006763/*
6764 * Default task group.
6765 * Every task in system belongs to this group at bootup.
6766 */
Peter Zijlstra029632f2011-10-25 10:00:11 +02006767struct task_group root_task_group;
Mike Galbraith35cf4e52012-08-07 05:00:13 +02006768LIST_HEAD(task_groups);
Waiman Longb0367622015-12-02 13:41:49 -05006769
6770/* Cacheline aligned slab cache for task_group */
6771static struct kmem_cache *task_group_cache __read_mostly;
Heiko Carstensb50f60c2006-07-30 03:03:52 -07006772#endif
6773
Joonsoo Kime6252c32013-04-23 17:27:41 +09006774DECLARE_PER_CPU(cpumask_var_t, load_balance_mask);
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006775DECLARE_PER_CPU(cpumask_var_t, select_idle_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776
6777void __init sched_init(void)
6778{
Qian Caia1dc0442019-07-19 21:23:19 -04006779 unsigned long ptr = 0;
Dietmar Eggemann55627e32019-05-27 07:21:13 +01006780 int i;
Mike Travis434d53b2008-04-04 18:11:04 -07006781
Steven Rostedt (VMware)c3a340f2019-12-19 16:44:53 -05006782 /* Make sure the linker didn't screw up */
6783 BUG_ON(&idle_sched_class + 1 != &fair_sched_class ||
6784 &fair_sched_class + 1 != &rt_sched_class ||
6785 &rt_sched_class + 1 != &dl_sched_class);
6786#ifdef CONFIG_SMP
6787 BUG_ON(&dl_sched_class + 1 != &stop_sched_class);
6788#endif
6789
Ingo Molnar5822a452017-03-05 13:09:07 +01006790 wait_bit_init();
Linus Torvalds9dcb8b62016-10-26 10:15:30 -07006791
Mike Travis434d53b2008-04-04 18:11:04 -07006792#ifdef CONFIG_FAIR_GROUP_SCHED
Qian Caia1dc0442019-07-19 21:23:19 -04006793 ptr += 2 * nr_cpu_ids * sizeof(void **);
Mike Travis434d53b2008-04-04 18:11:04 -07006794#endif
6795#ifdef CONFIG_RT_GROUP_SCHED
Qian Caia1dc0442019-07-19 21:23:19 -04006796 ptr += 2 * nr_cpu_ids * sizeof(void **);
Mike Travis434d53b2008-04-04 18:11:04 -07006797#endif
Qian Caia1dc0442019-07-19 21:23:19 -04006798 if (ptr) {
6799 ptr = (unsigned long)kzalloc(ptr, GFP_NOWAIT);
Mike Travis434d53b2008-04-04 18:11:04 -07006800
6801#ifdef CONFIG_FAIR_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08006802 root_task_group.se = (struct sched_entity **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07006803 ptr += nr_cpu_ids * sizeof(void **);
6804
Yong Zhang07e06b02011-01-07 15:17:36 +08006805 root_task_group.cfs_rq = (struct cfs_rq **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07006806 ptr += nr_cpu_ids * sizeof(void **);
Peter Zijlstraeff766a2008-04-19 19:45:00 +02006807
Wei Yangb1d17792020-04-23 21:44:43 +00006808 root_task_group.shares = ROOT_TASK_GROUP_LOAD;
6809 init_cfs_bandwidth(&root_task_group.cfs_bandwidth);
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006810#endif /* CONFIG_FAIR_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07006811#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08006812 root_task_group.rt_se = (struct sched_rt_entity **)ptr;
Mike Travis434d53b2008-04-04 18:11:04 -07006813 ptr += nr_cpu_ids * sizeof(void **);
6814
Yong Zhang07e06b02011-01-07 15:17:36 +08006815 root_task_group.rt_rq = (struct rt_rq **)ptr;
Peter Zijlstraeff766a2008-04-19 19:45:00 +02006816 ptr += nr_cpu_ids * sizeof(void **);
6817
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006818#endif /* CONFIG_RT_GROUP_SCHED */
Mike Travis434d53b2008-04-04 18:11:04 -07006819 }
Alex Thorltonb74e6272014-12-18 12:44:30 -06006820#ifdef CONFIG_CPUMASK_OFFSTACK
6821 for_each_possible_cpu(i) {
6822 per_cpu(load_balance_mask, i) = (cpumask_var_t)kzalloc_node(
6823 cpumask_size(), GFP_KERNEL, cpu_to_node(i));
Peter Zijlstra10e2f1a2016-05-09 10:38:05 +02006824 per_cpu(select_idle_mask, i) = (cpumask_var_t)kzalloc_node(
6825 cpumask_size(), GFP_KERNEL, cpu_to_node(i));
Alex Thorltonb74e6272014-12-18 12:44:30 -06006826 }
6827#endif /* CONFIG_CPUMASK_OFFSTACK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828
Ingo Molnard1ccc662017-02-01 11:46:42 +01006829 init_rt_bandwidth(&def_rt_bandwidth, global_rt_period(), global_rt_runtime());
6830 init_dl_bandwidth(&def_dl_bandwidth, global_rt_period(), global_rt_runtime());
Dario Faggioli332ac172013-11-07 14:43:45 +01006831
Gregory Haskins57d885f2008-01-25 21:08:18 +01006832#ifdef CONFIG_SMP
6833 init_defrootdomain();
6834#endif
6835
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02006836#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08006837 init_rt_bandwidth(&root_task_group.rt_bandwidth,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02006838 global_rt_period(), global_rt_runtime());
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02006839#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02006840
Dhaval Giani7c941432010-01-20 13:26:18 +01006841#ifdef CONFIG_CGROUP_SCHED
Waiman Longb0367622015-12-02 13:41:49 -05006842 task_group_cache = KMEM_CACHE(task_group, 0);
6843
Yong Zhang07e06b02011-01-07 15:17:36 +08006844 list_add(&root_task_group.list, &task_groups);
6845 INIT_LIST_HEAD(&root_task_group.children);
Glauber Costaf4d6f6c2011-11-01 19:19:07 -02006846 INIT_LIST_HEAD(&root_task_group.siblings);
Mike Galbraith5091faa2010-11-30 14:18:03 +01006847 autogroup_init(&init_task);
Dhaval Giani7c941432010-01-20 13:26:18 +01006848#endif /* CONFIG_CGROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01006849
Ingo Molnardd41f592007-07-09 18:51:59 +02006850 for_each_possible_cpu(i) {
Ingo Molnardd41f592007-07-09 18:51:59 +02006851 struct rq *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852
6853 rq = cpu_rq(i);
Thomas Gleixner05fa7852009-11-17 14:28:38 +01006854 raw_spin_lock_init(&rq->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855 rq->nr_running = 0;
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02006856 rq->calc_load_active = 0;
6857 rq->calc_load_update = jiffies + LOAD_FREQ;
Jan H. Schönherracb5a9b2011-07-14 18:32:43 +02006858 init_cfs_rq(&rq->cfs);
Abel Vesa07c54f72015-03-03 13:50:27 +02006859 init_rt_rq(&rq->rt);
6860 init_dl_rq(&rq->dl);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01006861#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01006862 INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
Vincent Guittot9c2791f2016-11-08 10:53:43 +01006863 rq->tmp_alone_branch = &rq->leaf_cfs_rq_list;
Dhaval Giani354d60c2008-04-19 19:44:59 +02006864 /*
Ingo Molnard1ccc662017-02-01 11:46:42 +01006865 * How much CPU bandwidth does root_task_group get?
Dhaval Giani354d60c2008-04-19 19:44:59 +02006866 *
6867 * In case of task-groups formed thr' the cgroup filesystem, it
Ingo Molnard1ccc662017-02-01 11:46:42 +01006868 * gets 100% of the CPU resources in the system. This overall
6869 * system CPU resource is divided among the tasks of
Yong Zhang07e06b02011-01-07 15:17:36 +08006870 * root_task_group and its child task-groups in a fair manner,
Dhaval Giani354d60c2008-04-19 19:44:59 +02006871 * based on each entity's (task or task-group's) weight
6872 * (se->load.weight).
6873 *
Yong Zhang07e06b02011-01-07 15:17:36 +08006874 * In other words, if root_task_group has 10 tasks of weight
Dhaval Giani354d60c2008-04-19 19:44:59 +02006875 * 1024) and two child groups A0 and A1 (of weight 1024 each),
Ingo Molnard1ccc662017-02-01 11:46:42 +01006876 * then A0's share of the CPU resource is:
Dhaval Giani354d60c2008-04-19 19:44:59 +02006877 *
Ingo Molnar0d905bc2009-05-04 19:13:30 +02006878 * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
Dhaval Giani354d60c2008-04-19 19:44:59 +02006879 *
Yong Zhang07e06b02011-01-07 15:17:36 +08006880 * We achieve this by letting root_task_group's tasks sit
6881 * directly in rq->cfs (i.e root_task_group->se[] = NULL).
Dhaval Giani354d60c2008-04-19 19:44:59 +02006882 */
Yong Zhang07e06b02011-01-07 15:17:36 +08006883 init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL);
Dhaval Giani354d60c2008-04-19 19:44:59 +02006884#endif /* CONFIG_FAIR_GROUP_SCHED */
6885
6886 rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01006887#ifdef CONFIG_RT_GROUP_SCHED
Yong Zhang07e06b02011-01-07 15:17:36 +08006888 init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01006889#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890#ifdef CONFIG_SMP
Peter Zijlstraa4c410f2006-12-06 20:37:21 -08006891 rq->sd = NULL;
Gregory Haskins57d885f2008-01-25 21:08:18 +01006892 rq->rd = NULL;
Vincent Guittotca6d75e2015-02-27 16:54:09 +01006893 rq->cpu_capacity = rq->cpu_capacity_orig = SCHED_CAPACITY_SCALE;
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02006894 rq->balance_callback = NULL;
Ingo Molnar3117df02006-12-13 00:34:43 -08006895 rq->active_balance = 0;
6896 rq->next_balance = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897 rq->push_cpu = 0;
6898 rq->cpu = i;
Gregory Haskins1f11eb6a2008-06-04 15:04:05 -04006899 rq->online = 0;
Mike Galbraitheae0c9d2009-11-10 03:50:02 +01006900 rq->idle_stamp = 0;
6901 rq->avg_idle = 2*sysctl_sched_migration_cost;
Jason Low9bd721c2013-09-13 11:26:52 -07006902 rq->max_idle_balance_cost = sysctl_sched_migration_cost;
Peter Zijlstra367456c2012-02-20 21:49:09 +01006903
6904 INIT_LIST_HEAD(&rq->cfs_tasks);
6905
Gregory Haskinsdc938522008-01-25 21:08:26 +01006906 rq_attach_root(rq, &def_root_domain);
Frederic Weisbecker3451d022011-08-10 23:21:01 +02006907#ifdef CONFIG_NO_HZ_COMMON
Peter Zijlstrae022e0d2017-12-21 11:20:23 +01006908 rq->last_blocked_load_update_tick = jiffies;
Peter Zijlstraa22e47a2017-12-21 10:01:24 +01006909 atomic_set(&rq->nohz_flags, 0);
Peter Zijlstra (Intel)90b53632020-03-27 11:44:56 +01006910
6911 rq_csd_init(rq, &rq->nohz_csd, nohz_csd_func);
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -07006912#endif
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +02006913#endif /* CONFIG_SMP */
Frederic Weisbecker77a021b2018-02-21 05:17:23 +01006914 hrtick_rq_init(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915 atomic_set(&rq->nr_iowait, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 }
Ingo Molnara0f98a12007-06-17 18:37:45 +02006917
Vincent Guittot90593932017-05-17 11:50:45 +02006918 set_load_weight(&init_task, false);
Ingo Molnardd41f592007-07-09 18:51:59 +02006919
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920 /*
6921 * The boot idle thread does lazy MMU switching as well:
6922 */
Vegard Nossumf1f10072017-02-27 14:30:07 -08006923 mmgrab(&init_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924 enter_lazy_tlb(&init_mm, current);
6925
6926 /*
6927 * Make us the idle thread. Technically, schedule() should not be
6928 * called from this thread, however somewhere below it might be,
6929 * but because we are the idle thread, we just pick up running again
6930 * when this runqueue becomes "idle".
6931 */
6932 init_idle(current, smp_processor_id());
Thomas Gleixnerdce48a82009-04-11 10:43:41 +02006933
6934 calc_load_update = jiffies + LOAD_FREQ;
6935
Rusty Russellbf4d83f2008-11-25 09:57:51 +10306936#ifdef CONFIG_SMP
Thomas Gleixner29d5e042012-04-20 13:05:45 +00006937 idle_thread_set_boot_cpu();
Peter Zijlstra029632f2011-10-25 10:00:11 +02006938#endif
6939 init_sched_fair_class();
Rusty Russell6a7b3dc2008-11-25 02:35:04 +10306940
Josh Poimboeuf4698f882016-06-07 14:43:16 -05006941 init_schedstats();
6942
Johannes Weinereb414682018-10-26 15:06:27 -07006943 psi_init();
6944
Patrick Bellasi69842cb2019-06-21 09:42:02 +01006945 init_uclamp();
6946
Ingo Molnar6892b752008-02-13 14:02:36 +01006947 scheduler_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948}
6949
Frederic Weisbeckerd902db12011-06-08 19:31:56 +02006950#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02006951static inline int preempt_count_equals(int preempt_offset)
6952{
Peter Zijlstrada7142e2015-09-28 18:11:45 +02006953 int nested = preempt_count() + rcu_preempt_depth();
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02006954
Arnd Bergmann4ba82162011-01-25 22:52:22 +01006955 return (nested == preempt_offset);
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +02006956}
6957
Simon Kagstromd8948372009-12-23 11:08:18 +01006958void __might_sleep(const char *file, int line, int preempt_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959{
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02006960 /*
6961 * Blocking primitives will set (and therefore destroy) current->state,
6962 * since we will exit with TASK_RUNNING make sure we enter with it,
6963 * otherwise we will destroy state.
6964 */
Linus Torvalds00845eb2015-02-01 12:23:32 -08006965 WARN_ONCE(current->state != TASK_RUNNING && current->task_state_change,
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02006966 "do not call blocking ops when !TASK_RUNNING; "
6967 "state=%lx set at [<%p>] %pS\n",
6968 current->state,
6969 (void *)current->task_state_change,
Linus Torvalds00845eb2015-02-01 12:23:32 -08006970 (void *)current->task_state_change);
Peter Zijlstra8eb23b92014-09-24 10:18:55 +02006971
Peter Zijlstra34274452014-09-24 10:18:56 +02006972 ___might_sleep(file, line, preempt_offset);
6973}
6974EXPORT_SYMBOL(__might_sleep);
6975
6976void ___might_sleep(const char *file, int line, int preempt_offset)
6977{
Ingo Molnard1ccc662017-02-01 11:46:42 +01006978 /* Ratelimiting timestamp: */
6979 static unsigned long prev_jiffy;
6980
Vegard Nossumd1c6d142016-07-23 09:46:39 +02006981 unsigned long preempt_disable_ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982
Ingo Molnard1ccc662017-02-01 11:46:42 +01006983 /* WARN_ON_ONCE() by default, no rate limit required: */
6984 rcu_sleep_check();
6985
Thomas Gleixnerdb273be2014-02-07 20:58:38 +01006986 if ((preempt_count_equals(preempt_offset) && !irqs_disabled() &&
Daniel Vetter312364f32019-08-26 22:14:23 +02006987 !is_idle_task(current) && !current->non_block_count) ||
Thomas Gleixner1c3c5ea2017-05-16 20:42:48 +02006988 system_state == SYSTEM_BOOTING || system_state > SYSTEM_RUNNING ||
6989 oops_in_progress)
Ingo Molnaraef745f2008-08-28 11:34:43 +02006990 return;
Thomas Gleixner1c3c5ea2017-05-16 20:42:48 +02006991
Ingo Molnaraef745f2008-08-28 11:34:43 +02006992 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
6993 return;
6994 prev_jiffy = jiffies;
6995
Ingo Molnard1ccc662017-02-01 11:46:42 +01006996 /* Save this before calling printk(), since that will clobber it: */
Vegard Nossumd1c6d142016-07-23 09:46:39 +02006997 preempt_disable_ip = get_preempt_disable_ip(current);
6998
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01006999 printk(KERN_ERR
7000 "BUG: sleeping function called from invalid context at %s:%d\n",
7001 file, line);
7002 printk(KERN_ERR
Daniel Vetter312364f32019-08-26 22:14:23 +02007003 "in_atomic(): %d, irqs_disabled(): %d, non_block: %d, pid: %d, name: %s\n",
7004 in_atomic(), irqs_disabled(), current->non_block_count,
Peter Zijlstra3df0fc52009-12-20 14:23:57 +01007005 current->pid, current->comm);
Ingo Molnaraef745f2008-08-28 11:34:43 +02007006
Eric Sandeena8b686b2014-12-16 16:25:28 -06007007 if (task_stack_end_corrupted(current))
7008 printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
7009
Ingo Molnaraef745f2008-08-28 11:34:43 +02007010 debug_show_held_locks(current);
7011 if (irqs_disabled())
7012 print_irqtrace_events(current);
Vegard Nossumd1c6d142016-07-23 09:46:39 +02007013 if (IS_ENABLED(CONFIG_DEBUG_PREEMPT)
7014 && !preempt_count_equals(preempt_offset)) {
Thomas Gleixner8f47b182014-02-07 20:58:39 +01007015 pr_err("Preemption disabled at:");
Dmitry Safonov2062a4e2020-06-08 21:29:56 -07007016 print_ip_sym(KERN_ERR, preempt_disable_ip);
Thomas Gleixner8f47b182014-02-07 20:58:39 +01007017 }
Ingo Molnaraef745f2008-08-28 11:34:43 +02007018 dump_stack();
Vegard Nossumf0b22e32016-07-22 21:46:02 +02007019 add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007020}
Peter Zijlstra34274452014-09-24 10:18:56 +02007021EXPORT_SYMBOL(___might_sleep);
Peter Zijlstra568f1962019-01-28 17:21:52 -08007022
7023void __cant_sleep(const char *file, int line, int preempt_offset)
7024{
7025 static unsigned long prev_jiffy;
7026
7027 if (irqs_disabled())
7028 return;
7029
7030 if (!IS_ENABLED(CONFIG_PREEMPT_COUNT))
7031 return;
7032
7033 if (preempt_count() > preempt_offset)
7034 return;
7035
7036 if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
7037 return;
7038 prev_jiffy = jiffies;
7039
7040 printk(KERN_ERR "BUG: assuming atomic context at %s:%d\n", file, line);
7041 printk(KERN_ERR "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
7042 in_atomic(), irqs_disabled(),
7043 current->pid, current->comm);
7044
7045 debug_show_held_locks(current);
7046 dump_stack();
7047 add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
7048}
7049EXPORT_SYMBOL_GPL(__cant_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007050#endif
7051
7052#ifdef CONFIG_MAGIC_SYSRQ
7053void normalize_rt_tasks(void)
7054{
7055 struct task_struct *g, *p;
Peter Zijlstradbc7f062015-06-11 14:46:38 +02007056 struct sched_attr attr = {
7057 .sched_policy = SCHED_NORMAL,
7058 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007060 read_lock(&tasklist_lock);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007061 for_each_process_thread(g, p) {
Ingo Molnar178be792007-10-15 17:00:18 +02007062 /*
7063 * Only normalize user tasks:
7064 */
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007065 if (p->flags & PF_KTHREAD)
Ingo Molnar178be792007-10-15 17:00:18 +02007066 continue;
7067
Josh Poimboeuf4fa8d292016-06-17 12:43:26 -05007068 p->se.exec_start = 0;
7069 schedstat_set(p->se.statistics.wait_start, 0);
7070 schedstat_set(p->se.statistics.sleep_start, 0);
7071 schedstat_set(p->se.statistics.block_start, 0);
Ingo Molnardd41f592007-07-09 18:51:59 +02007072
Dario Faggioliaab03e02013-11-28 11:14:43 +01007073 if (!dl_task(p) && !rt_task(p)) {
Ingo Molnardd41f592007-07-09 18:51:59 +02007074 /*
7075 * Renice negative nice level userspace
7076 * tasks back to 0:
7077 */
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007078 if (task_nice(p) < 0)
Ingo Molnardd41f592007-07-09 18:51:59 +02007079 set_user_nice(p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080 continue;
Ingo Molnardd41f592007-07-09 18:51:59 +02007081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082
Peter Zijlstradbc7f062015-06-11 14:46:38 +02007083 __sched_setscheduler(p, &attr, false, false);
Oleg Nesterov5d07f422014-08-13 21:19:53 +02007084 }
Oleg Nesterov3472eaa2014-09-21 21:33:38 +02007085 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086}
7087
7088#endif /* CONFIG_MAGIC_SYSRQ */
Linus Torvalds1df5c102005-09-12 07:59:21 -07007089
Jason Wessel67fc4e02010-05-20 21:04:21 -05007090#if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007091/*
Jason Wessel67fc4e02010-05-20 21:04:21 -05007092 * These functions are only useful for the IA64 MCA handling, or kdb.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007093 *
7094 * They can only be called when the whole system has been
7095 * stopped - every CPU needs to be quiescent, and no scheduling
7096 * activity can take place. Using them for anything else would
7097 * be a serious bug, and as a result, they aren't even visible
7098 * under any other configuration.
7099 */
7100
7101/**
Ingo Molnard1ccc662017-02-01 11:46:42 +01007102 * curr_task - return the current task for a given CPU.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007103 * @cpu: the processor in question.
7104 *
7105 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
Yacine Belkadie69f6182013-07-12 20:45:47 +02007106 *
7107 * Return: The current task for @cpu.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007108 */
Ingo Molnar36c8b582006-07-03 00:25:41 -07007109struct task_struct *curr_task(int cpu)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007110{
7111 return cpu_curr(cpu);
7112}
7113
Jason Wessel67fc4e02010-05-20 21:04:21 -05007114#endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */
7115
7116#ifdef CONFIG_IA64
Linus Torvalds1df5c102005-09-12 07:59:21 -07007117/**
Peter Zijlstra5feeb782019-05-29 20:36:38 +00007118 * ia64_set_curr_task - set the current task for a given CPU.
Linus Torvalds1df5c102005-09-12 07:59:21 -07007119 * @cpu: the processor in question.
7120 * @p: the task pointer to set.
7121 *
7122 * Description: This function must only be used when non-maskable interrupts
Ingo Molnar41a2d6c2007-12-05 15:46:09 +01007123 * are serviced on a separate stack. It allows the architecture to switch the
Ingo Molnard1ccc662017-02-01 11:46:42 +01007124 * notion of the current task on a CPU in a non-blocking manner. This function
Linus Torvalds1df5c102005-09-12 07:59:21 -07007125 * must be called with all CPU's synchronized, and interrupts disabled, the
7126 * and caller must save the original value of the current task (see
7127 * curr_task() above) and restore that value before reenabling interrupts and
7128 * re-starting the system.
7129 *
7130 * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
7131 */
Peter Zijlstraa458ae22016-09-20 20:29:40 +02007132void ia64_set_curr_task(int cpu, struct task_struct *p)
Linus Torvalds1df5c102005-09-12 07:59:21 -07007133{
7134 cpu_curr(cpu) = p;
7135}
7136
7137#endif
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007138
Dhaval Giani7c941432010-01-20 13:26:18 +01007139#ifdef CONFIG_CGROUP_SCHED
Peter Zijlstra029632f2011-10-25 10:00:11 +02007140/* task_group_lock serializes the addition/removal of task groups */
7141static DEFINE_SPINLOCK(task_group_lock);
7142
Patrick Bellasi2480c092019-08-22 14:28:06 +01007143static inline void alloc_uclamp_sched_group(struct task_group *tg,
7144 struct task_group *parent)
7145{
7146#ifdef CONFIG_UCLAMP_TASK_GROUP
Patrick Bellasi0413d7f2019-08-22 14:28:11 +01007147 enum uclamp_id clamp_id;
Patrick Bellasi2480c092019-08-22 14:28:06 +01007148
7149 for_each_clamp_id(clamp_id) {
7150 uclamp_se_set(&tg->uclamp_req[clamp_id],
7151 uclamp_none(clamp_id), false);
Patrick Bellasi0b60ba22019-08-22 14:28:07 +01007152 tg->uclamp[clamp_id] = parent->uclamp[clamp_id];
Patrick Bellasi2480c092019-08-22 14:28:06 +01007153 }
7154#endif
7155}
7156
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007157static void sched_free_group(struct task_group *tg)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007158{
7159 free_fair_sched_group(tg);
7160 free_rt_sched_group(tg);
Mike Galbraithe9aa1dd2011-01-05 11:11:25 +01007161 autogroup_free(tg);
Waiman Longb0367622015-12-02 13:41:49 -05007162 kmem_cache_free(task_group_cache, tg);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007163}
7164
7165/* allocate runqueue etc for a new task group */
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007166struct task_group *sched_create_group(struct task_group *parent)
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007167{
7168 struct task_group *tg;
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007169
Waiman Longb0367622015-12-02 13:41:49 -05007170 tg = kmem_cache_alloc(task_group_cache, GFP_KERNEL | __GFP_ZERO);
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007171 if (!tg)
7172 return ERR_PTR(-ENOMEM);
7173
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007174 if (!alloc_fair_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007175 goto err;
7176
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007177 if (!alloc_rt_sched_group(tg, parent))
Peter Zijlstrabccbe082008-02-13 15:45:40 +01007178 goto err;
7179
Patrick Bellasi2480c092019-08-22 14:28:06 +01007180 alloc_uclamp_sched_group(tg, parent);
7181
Li Zefanace783b2013-01-24 14:30:48 +08007182 return tg;
7183
7184err:
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007185 sched_free_group(tg);
Li Zefanace783b2013-01-24 14:30:48 +08007186 return ERR_PTR(-ENOMEM);
7187}
7188
7189void sched_online_group(struct task_group *tg, struct task_group *parent)
7190{
7191 unsigned long flags;
7192
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007193 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007194 list_add_rcu(&tg->list, &task_groups);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007195
Ingo Molnard1ccc662017-02-01 11:46:42 +01007196 /* Root should already exist: */
7197 WARN_ON(!parent);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007198
7199 tg->parent = parent;
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007200 INIT_LIST_HEAD(&tg->children);
Zhang, Yanmin09f27242030-08-14 15:56:40 +08007201 list_add_rcu(&tg->siblings, &parent->children);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007202 spin_unlock_irqrestore(&task_group_lock, flags);
Peter Zijlstra8663e242016-06-22 14:58:02 +02007203
7204 online_fair_sched_group(tg);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007205}
7206
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007207/* rcu callback to free various structures associated with a task group */
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007208static void sched_free_group_rcu(struct rcu_head *rhp)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007209{
Ingo Molnard1ccc662017-02-01 11:46:42 +01007210 /* Now it should be safe to free those cfs_rqs: */
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007211 sched_free_group(container_of(rhp, struct task_group, rcu));
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007212}
7213
Ingo Molnar4cf86d72007-10-15 17:00:14 +02007214void sched_destroy_group(struct task_group *tg)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007215{
Ingo Molnard1ccc662017-02-01 11:46:42 +01007216 /* Wait for possible concurrent references to cfs_rqs complete: */
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007217 call_rcu(&tg->rcu, sched_free_group_rcu);
Li Zefanace783b2013-01-24 14:30:48 +08007218}
7219
7220void sched_offline_group(struct task_group *tg)
7221{
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007222 unsigned long flags;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007223
Ingo Molnard1ccc662017-02-01 11:46:42 +01007224 /* End participation in shares distribution: */
Peter Zijlstra6fe1f342016-01-21 22:24:16 +01007225 unregister_fair_sched_group(tg);
Peter Zijlstra3d4b47b2010-11-15 15:47:01 -08007226
7227 spin_lock_irqsave(&task_group_lock, flags);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007228 list_del_rcu(&tg->list);
Peter Zijlstraf473aa52008-04-19 19:45:00 +02007229 list_del_rcu(&tg->siblings);
Peter Zijlstra8ed36992008-02-13 15:45:39 +01007230 spin_unlock_irqrestore(&task_group_lock, flags);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007231}
7232
Vincent Guittotea86cb42016-06-17 13:38:55 +02007233static void sched_change_group(struct task_struct *tsk, int type)
7234{
7235 struct task_group *tg;
7236
7237 /*
7238 * All callers are synchronized by task_rq_lock(); we do not use RCU
7239 * which is pointless here. Thus, we pass "true" to task_css_check()
7240 * to prevent lockdep warnings.
7241 */
7242 tg = container_of(task_css_check(tsk, cpu_cgrp_id, true),
7243 struct task_group, css);
7244 tg = autogroup_task_group(tsk, tg);
7245 tsk->sched_task_group = tg;
7246
7247#ifdef CONFIG_FAIR_GROUP_SCHED
7248 if (tsk->sched_class->task_change_group)
7249 tsk->sched_class->task_change_group(tsk, type);
7250 else
7251#endif
7252 set_task_rq(tsk, task_cpu(tsk));
7253}
7254
7255/*
7256 * Change task's runqueue when it moves between groups.
7257 *
7258 * The caller of this function should have put the task in its new group by
7259 * now. This function just updates tsk->se.cfs_rq and tsk->se.parent to reflect
7260 * its new group.
Srivatsa Vaddagiri9b5b7752007-10-15 17:00:09 +02007261 */
7262void sched_move_task(struct task_struct *tsk)
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007263{
Peter Zijlstra7a57f322017-02-21 14:47:02 +01007264 int queued, running, queue_flags =
7265 DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK;
Peter Zijlstraeb580752015-07-31 21:28:18 +02007266 struct rq_flags rf;
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007267 struct rq *rq;
7268
Peter Zijlstraeb580752015-07-31 21:28:18 +02007269 rq = task_rq_lock(tsk, &rf);
Peter Zijlstra1b1d6222017-01-23 16:05:55 +01007270 update_rq_clock(rq);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007271
Dmitry Adamushko051a1d12007-12-18 15:21:13 +01007272 running = task_current(rq, tsk);
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007273 queued = task_on_rq_queued(tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007274
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007275 if (queued)
Peter Zijlstra7a57f322017-02-21 14:47:02 +01007276 dequeue_task(rq, tsk, queue_flags);
Steven Rostedt (VMware)bb3bac22017-02-06 11:04:26 -05007277 if (running)
Kirill Tkhaif3cd1c42014-09-12 17:41:40 +04007278 put_prev_task(rq, tsk);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007279
Vincent Guittotea86cb42016-06-17 13:38:55 +02007280 sched_change_group(tsk, TASK_MOVE_GROUP);
Peter Zijlstra810b3812008-02-29 15:21:01 -05007281
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04007282 if (queued)
Peter Zijlstra7a57f322017-02-21 14:47:02 +01007283 enqueue_task(rq, tsk, queue_flags);
Vincent Guittot2a4b03f2020-01-14 15:13:56 +01007284 if (running) {
Peter Zijlstra03b7fad2019-05-29 20:36:41 +00007285 set_next_task(rq, tsk);
Vincent Guittot2a4b03f2020-01-14 15:13:56 +01007286 /*
7287 * After changing group, the running task may have joined a
7288 * throttled one but it's still the running task. Trigger a
7289 * resched to make sure that task can still run.
7290 */
7291 resched_curr(rq);
7292 }
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007293
Peter Zijlstraeb580752015-07-31 21:28:18 +02007294 task_rq_unlock(rq, tsk, &rf);
Srivatsa Vaddagiri29f59db2007-10-15 17:00:07 +02007295}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007296
Tejun Heoa7c6d552013-08-08 20:11:23 -04007297static inline struct task_group *css_tg(struct cgroup_subsys_state *css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007298{
Tejun Heoa7c6d552013-08-08 20:11:23 -04007299 return css ? container_of(css, struct task_group, css) : NULL;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007300}
7301
Tejun Heoeb954192013-08-08 20:11:23 -04007302static struct cgroup_subsys_state *
7303cpu_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007304{
Tejun Heoeb954192013-08-08 20:11:23 -04007305 struct task_group *parent = css_tg(parent_css);
7306 struct task_group *tg;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007307
Tejun Heoeb954192013-08-08 20:11:23 -04007308 if (!parent) {
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007309 /* This is early initialization for the top cgroup */
Yong Zhang07e06b02011-01-07 15:17:36 +08007310 return &root_task_group.css;
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007311 }
7312
Dhaval Gianiec7dc8a2008-04-19 19:44:59 +02007313 tg = sched_create_group(parent);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007314 if (IS_ERR(tg))
7315 return ERR_PTR(-ENOMEM);
7316
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007317 return &tg->css;
7318}
7319
Konstantin Khlebnikov96b77742017-02-08 14:27:27 +03007320/* Expose task group only after completing cgroup initialization */
7321static int cpu_cgroup_css_online(struct cgroup_subsys_state *css)
7322{
7323 struct task_group *tg = css_tg(css);
7324 struct task_group *parent = css_tg(css->parent);
7325
7326 if (parent)
7327 sched_online_group(tg, parent);
Qais Yousef72260172019-12-24 11:54:04 +00007328
7329#ifdef CONFIG_UCLAMP_TASK_GROUP
7330 /* Propagate the effective uclamp value for the new group */
7331 cpu_util_update_eff(css);
7332#endif
7333
Konstantin Khlebnikov96b77742017-02-08 14:27:27 +03007334 return 0;
7335}
7336
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007337static void cpu_cgroup_css_released(struct cgroup_subsys_state *css)
Li Zefanace783b2013-01-24 14:30:48 +08007338{
Tejun Heoeb954192013-08-08 20:11:23 -04007339 struct task_group *tg = css_tg(css);
Li Zefanace783b2013-01-24 14:30:48 +08007340
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007341 sched_offline_group(tg);
Li Zefanace783b2013-01-24 14:30:48 +08007342}
7343
Tejun Heoeb954192013-08-08 20:11:23 -04007344static void cpu_cgroup_css_free(struct cgroup_subsys_state *css)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007345{
Tejun Heoeb954192013-08-08 20:11:23 -04007346 struct task_group *tg = css_tg(css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007347
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01007348 /*
7349 * Relies on the RCU grace period between css_released() and this.
7350 */
7351 sched_free_group(tg);
Li Zefanace783b2013-01-24 14:30:48 +08007352}
7353
Vincent Guittotea86cb42016-06-17 13:38:55 +02007354/*
7355 * This is called before wake_up_new_task(), therefore we really only
7356 * have to set its group bits, all the other stuff does not apply.
7357 */
Oleg Nesterovb53202e2015-12-03 10:24:08 -05007358static void cpu_cgroup_fork(struct task_struct *task)
Kirill Tkhaieeb61e52014-10-27 14:18:25 +04007359{
Vincent Guittotea86cb42016-06-17 13:38:55 +02007360 struct rq_flags rf;
7361 struct rq *rq;
7362
7363 rq = task_rq_lock(task, &rf);
7364
Peter Zijlstra80f5c1b2016-10-03 16:28:37 +02007365 update_rq_clock(rq);
Vincent Guittotea86cb42016-06-17 13:38:55 +02007366 sched_change_group(task, TASK_SET_GROUP);
7367
7368 task_rq_unlock(rq, task, &rf);
Kirill Tkhaieeb61e52014-10-27 14:18:25 +04007369}
7370
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05007371static int cpu_cgroup_can_attach(struct cgroup_taskset *tset)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007372{
Tejun Heobb9d97b2011-12-12 18:12:21 -08007373 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05007374 struct cgroup_subsys_state *css;
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02007375 int ret = 0;
Tejun Heobb9d97b2011-12-12 18:12:21 -08007376
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05007377 cgroup_taskset_for_each(task, css, tset) {
Peter Zijlstrab68aa232008-02-13 15:45:40 +01007378#ifdef CONFIG_RT_GROUP_SCHED
Tejun Heoeb954192013-08-08 20:11:23 -04007379 if (!sched_rt_can_attach(css_tg(css), task))
Tejun Heobb9d97b2011-12-12 18:12:21 -08007380 return -EINVAL;
Peter Zijlstrab68aa232008-02-13 15:45:40 +01007381#endif
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02007382 /*
7383 * Serialize against wake_up_new_task() such that if its
7384 * running, we're sure to observe its full state.
7385 */
7386 raw_spin_lock_irq(&task->pi_lock);
7387 /*
7388 * Avoid calling sched_move_task() before wake_up_new_task()
7389 * has happened. This would lead to problems with PELT, due to
7390 * move wanting to detach+attach while we're not attached yet.
7391 */
7392 if (task->state == TASK_NEW)
7393 ret = -EINVAL;
7394 raw_spin_unlock_irq(&task->pi_lock);
7395
7396 if (ret)
7397 break;
Tejun Heobb9d97b2011-12-12 18:12:21 -08007398 }
Peter Zijlstra7dc603c2016-06-16 13:29:28 +02007399 return ret;
Ben Blumbe367d02009-09-23 15:56:31 -07007400}
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007401
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05007402static void cpu_cgroup_attach(struct cgroup_taskset *tset)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007403{
Tejun Heobb9d97b2011-12-12 18:12:21 -08007404 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05007405 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -08007406
Tejun Heo1f7dd3e52015-12-03 10:18:21 -05007407 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08007408 sched_move_task(task);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007409}
7410
Patrick Bellasi2480c092019-08-22 14:28:06 +01007411#ifdef CONFIG_UCLAMP_TASK_GROUP
Patrick Bellasi0b60ba22019-08-22 14:28:07 +01007412static void cpu_util_update_eff(struct cgroup_subsys_state *css)
7413{
7414 struct cgroup_subsys_state *top_css = css;
7415 struct uclamp_se *uc_parent = NULL;
7416 struct uclamp_se *uc_se = NULL;
7417 unsigned int eff[UCLAMP_CNT];
Patrick Bellasi0413d7f2019-08-22 14:28:11 +01007418 enum uclamp_id clamp_id;
Patrick Bellasi0b60ba22019-08-22 14:28:07 +01007419 unsigned int clamps;
7420
7421 css_for_each_descendant_pre(css, top_css) {
7422 uc_parent = css_tg(css)->parent
7423 ? css_tg(css)->parent->uclamp : NULL;
7424
7425 for_each_clamp_id(clamp_id) {
7426 /* Assume effective clamps matches requested clamps */
7427 eff[clamp_id] = css_tg(css)->uclamp_req[clamp_id].value;
7428 /* Cap effective clamps with parent's effective clamps */
7429 if (uc_parent &&
7430 eff[clamp_id] > uc_parent[clamp_id].value) {
7431 eff[clamp_id] = uc_parent[clamp_id].value;
7432 }
7433 }
7434 /* Ensure protection is always capped by limit */
7435 eff[UCLAMP_MIN] = min(eff[UCLAMP_MIN], eff[UCLAMP_MAX]);
7436
7437 /* Propagate most restrictive effective clamps */
7438 clamps = 0x0;
7439 uc_se = css_tg(css)->uclamp;
7440 for_each_clamp_id(clamp_id) {
7441 if (eff[clamp_id] == uc_se[clamp_id].value)
7442 continue;
7443 uc_se[clamp_id].value = eff[clamp_id];
7444 uc_se[clamp_id].bucket_id = uclamp_bucket_id(eff[clamp_id]);
7445 clamps |= (0x1 << clamp_id);
7446 }
Patrick Bellasibabbe172019-08-22 14:28:10 +01007447 if (!clamps) {
Patrick Bellasi0b60ba22019-08-22 14:28:07 +01007448 css = css_rightmost_descendant(css);
Patrick Bellasibabbe172019-08-22 14:28:10 +01007449 continue;
7450 }
7451
7452 /* Immediately update descendants RUNNABLE tasks */
7453 uclamp_update_active_tasks(css, clamps);
Patrick Bellasi0b60ba22019-08-22 14:28:07 +01007454 }
7455}
Patrick Bellasi2480c092019-08-22 14:28:06 +01007456
7457/*
7458 * Integer 10^N with a given N exponent by casting to integer the literal "1eN"
7459 * C expression. Since there is no way to convert a macro argument (N) into a
7460 * character constant, use two levels of macros.
7461 */
7462#define _POW10(exp) ((unsigned int)1e##exp)
7463#define POW10(exp) _POW10(exp)
7464
7465struct uclamp_request {
7466#define UCLAMP_PERCENT_SHIFT 2
7467#define UCLAMP_PERCENT_SCALE (100 * POW10(UCLAMP_PERCENT_SHIFT))
7468 s64 percent;
7469 u64 util;
7470 int ret;
7471};
7472
7473static inline struct uclamp_request
7474capacity_from_percent(char *buf)
7475{
7476 struct uclamp_request req = {
7477 .percent = UCLAMP_PERCENT_SCALE,
7478 .util = SCHED_CAPACITY_SCALE,
7479 .ret = 0,
7480 };
7481
7482 buf = strim(buf);
7483 if (strcmp(buf, "max")) {
7484 req.ret = cgroup_parse_float(buf, UCLAMP_PERCENT_SHIFT,
7485 &req.percent);
7486 if (req.ret)
7487 return req;
Qais Yousefb562d142020-01-14 21:09:47 +00007488 if ((u64)req.percent > UCLAMP_PERCENT_SCALE) {
Patrick Bellasi2480c092019-08-22 14:28:06 +01007489 req.ret = -ERANGE;
7490 return req;
7491 }
7492
7493 req.util = req.percent << SCHED_CAPACITY_SHIFT;
7494 req.util = DIV_ROUND_CLOSEST_ULL(req.util, UCLAMP_PERCENT_SCALE);
7495 }
7496
7497 return req;
7498}
7499
7500static ssize_t cpu_uclamp_write(struct kernfs_open_file *of, char *buf,
7501 size_t nbytes, loff_t off,
7502 enum uclamp_id clamp_id)
7503{
7504 struct uclamp_request req;
7505 struct task_group *tg;
7506
7507 req = capacity_from_percent(buf);
7508 if (req.ret)
7509 return req.ret;
7510
Qais Yousef46609ce2020-06-30 12:21:23 +01007511 static_branch_enable(&sched_uclamp_used);
7512
Patrick Bellasi2480c092019-08-22 14:28:06 +01007513 mutex_lock(&uclamp_mutex);
7514 rcu_read_lock();
7515
7516 tg = css_tg(of_css(of));
7517 if (tg->uclamp_req[clamp_id].value != req.util)
7518 uclamp_se_set(&tg->uclamp_req[clamp_id], req.util, false);
7519
7520 /*
7521 * Because of not recoverable conversion rounding we keep track of the
7522 * exact requested value
7523 */
7524 tg->uclamp_pct[clamp_id] = req.percent;
7525
Patrick Bellasi0b60ba22019-08-22 14:28:07 +01007526 /* Update effective clamps to track the most restrictive value */
7527 cpu_util_update_eff(of_css(of));
7528
Patrick Bellasi2480c092019-08-22 14:28:06 +01007529 rcu_read_unlock();
7530 mutex_unlock(&uclamp_mutex);
7531
7532 return nbytes;
7533}
7534
7535static ssize_t cpu_uclamp_min_write(struct kernfs_open_file *of,
7536 char *buf, size_t nbytes,
7537 loff_t off)
7538{
7539 return cpu_uclamp_write(of, buf, nbytes, off, UCLAMP_MIN);
7540}
7541
7542static ssize_t cpu_uclamp_max_write(struct kernfs_open_file *of,
7543 char *buf, size_t nbytes,
7544 loff_t off)
7545{
7546 return cpu_uclamp_write(of, buf, nbytes, off, UCLAMP_MAX);
7547}
7548
7549static inline void cpu_uclamp_print(struct seq_file *sf,
7550 enum uclamp_id clamp_id)
7551{
7552 struct task_group *tg;
7553 u64 util_clamp;
7554 u64 percent;
7555 u32 rem;
7556
7557 rcu_read_lock();
7558 tg = css_tg(seq_css(sf));
7559 util_clamp = tg->uclamp_req[clamp_id].value;
7560 rcu_read_unlock();
7561
7562 if (util_clamp == SCHED_CAPACITY_SCALE) {
7563 seq_puts(sf, "max\n");
7564 return;
7565 }
7566
7567 percent = tg->uclamp_pct[clamp_id];
7568 percent = div_u64_rem(percent, POW10(UCLAMP_PERCENT_SHIFT), &rem);
7569 seq_printf(sf, "%llu.%0*u\n", percent, UCLAMP_PERCENT_SHIFT, rem);
7570}
7571
7572static int cpu_uclamp_min_show(struct seq_file *sf, void *v)
7573{
7574 cpu_uclamp_print(sf, UCLAMP_MIN);
7575 return 0;
7576}
7577
7578static int cpu_uclamp_max_show(struct seq_file *sf, void *v)
7579{
7580 cpu_uclamp_print(sf, UCLAMP_MAX);
7581 return 0;
7582}
7583#endif /* CONFIG_UCLAMP_TASK_GROUP */
7584
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007585#ifdef CONFIG_FAIR_GROUP_SCHED
Tejun Heo182446d2013-08-08 20:11:24 -04007586static int cpu_shares_write_u64(struct cgroup_subsys_state *css,
7587 struct cftype *cftype, u64 shareval)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007588{
Konstantin Khlebnikov5b61d502019-02-27 11:10:18 +03007589 if (shareval > scale_load_down(ULONG_MAX))
7590 shareval = MAX_SHARES;
Tejun Heo182446d2013-08-08 20:11:24 -04007591 return sched_group_set_shares(css_tg(css), scale_load(shareval));
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007592}
7593
Tejun Heo182446d2013-08-08 20:11:24 -04007594static u64 cpu_shares_read_u64(struct cgroup_subsys_state *css,
7595 struct cftype *cft)
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007596{
Tejun Heo182446d2013-08-08 20:11:24 -04007597 struct task_group *tg = css_tg(css);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007598
Nikhil Raoc8b28112011-05-18 14:37:48 -07007599 return (u64) scale_load_down(tg->shares);
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007600}
Paul Turnerab84d312011-07-21 09:43:28 -07007601
7602#ifdef CONFIG_CFS_BANDWIDTH
Paul Turnera790de92011-07-21 09:43:29 -07007603static DEFINE_MUTEX(cfs_constraints_mutex);
7604
Paul Turnerab84d312011-07-21 09:43:28 -07007605const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */
YueHaibingb1546ed2019-04-18 22:47:13 +08007606static const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */
Huaixin Changd505b8a2020-04-25 18:52:48 +08007607/* More than 203 days if BW_SHIFT equals 20. */
7608static const u64 max_cfs_runtime = MAX_BW * NSEC_PER_USEC;
Paul Turnerab84d312011-07-21 09:43:28 -07007609
Paul Turnera790de92011-07-21 09:43:29 -07007610static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime);
7611
Paul Turnerab84d312011-07-21 09:43:28 -07007612static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota)
7613{
Paul Turner56f570e2011-11-07 20:26:33 -08007614 int i, ret = 0, runtime_enabled, runtime_was_enabled;
Peter Zijlstra029632f2011-10-25 10:00:11 +02007615 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Paul Turnerab84d312011-07-21 09:43:28 -07007616
7617 if (tg == &root_task_group)
7618 return -EINVAL;
7619
7620 /*
7621 * Ensure we have at some amount of bandwidth every period. This is
7622 * to prevent reaching a state of large arrears when throttled via
7623 * entity_tick() resulting in prolonged exit starvation.
7624 */
7625 if (quota < min_cfs_quota_period || period < min_cfs_quota_period)
7626 return -EINVAL;
7627
7628 /*
7629 * Likewise, bound things on the otherside by preventing insane quota
7630 * periods. This also allows us to normalize in computing quota
7631 * feasibility.
7632 */
7633 if (period > max_cfs_quota_period)
7634 return -EINVAL;
7635
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04007636 /*
Huaixin Changd505b8a2020-04-25 18:52:48 +08007637 * Bound quota to defend quota against overflow during bandwidth shift.
7638 */
7639 if (quota != RUNTIME_INF && quota > max_cfs_runtime)
7640 return -EINVAL;
7641
7642 /*
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04007643 * Prevent race between setting of cfs_rq->runtime_enabled and
7644 * unthrottle_offline_cfs_rqs().
7645 */
7646 get_online_cpus();
Paul Turnera790de92011-07-21 09:43:29 -07007647 mutex_lock(&cfs_constraints_mutex);
7648 ret = __cfs_schedulable(tg, period, quota);
7649 if (ret)
7650 goto out_unlock;
7651
Paul Turner58088ad2011-07-21 09:43:31 -07007652 runtime_enabled = quota != RUNTIME_INF;
Paul Turner56f570e2011-11-07 20:26:33 -08007653 runtime_was_enabled = cfs_b->quota != RUNTIME_INF;
Ben Segall1ee14e62013-10-16 11:16:12 -07007654 /*
7655 * If we need to toggle cfs_bandwidth_used, off->on must occur
7656 * before making related changes, and on->off must occur afterwards
7657 */
7658 if (runtime_enabled && !runtime_was_enabled)
7659 cfs_bandwidth_usage_inc();
Paul Turnerab84d312011-07-21 09:43:28 -07007660 raw_spin_lock_irq(&cfs_b->lock);
7661 cfs_b->period = ns_to_ktime(period);
7662 cfs_b->quota = quota;
Paul Turner58088ad2011-07-21 09:43:31 -07007663
Paul Turnera9cf55b2011-07-21 09:43:32 -07007664 __refill_cfs_bandwidth_runtime(cfs_b);
Ingo Molnard1ccc662017-02-01 11:46:42 +01007665
7666 /* Restart the period timer (if active) to handle new period expiry: */
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +02007667 if (runtime_enabled)
7668 start_cfs_bandwidth(cfs_b);
Ingo Molnard1ccc662017-02-01 11:46:42 +01007669
Paul Turnerab84d312011-07-21 09:43:28 -07007670 raw_spin_unlock_irq(&cfs_b->lock);
7671
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04007672 for_each_online_cpu(i) {
Paul Turnerab84d312011-07-21 09:43:28 -07007673 struct cfs_rq *cfs_rq = tg->cfs_rq[i];
Peter Zijlstra029632f2011-10-25 10:00:11 +02007674 struct rq *rq = cfs_rq->rq;
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02007675 struct rq_flags rf;
Paul Turnerab84d312011-07-21 09:43:28 -07007676
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02007677 rq_lock_irq(rq, &rf);
Paul Turner58088ad2011-07-21 09:43:31 -07007678 cfs_rq->runtime_enabled = runtime_enabled;
Paul Turnerab84d312011-07-21 09:43:28 -07007679 cfs_rq->runtime_remaining = 0;
Paul Turner671fd9d2011-07-21 09:43:34 -07007680
Peter Zijlstra029632f2011-10-25 10:00:11 +02007681 if (cfs_rq->throttled)
Paul Turner671fd9d2011-07-21 09:43:34 -07007682 unthrottle_cfs_rq(cfs_rq);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02007683 rq_unlock_irq(rq, &rf);
Paul Turnerab84d312011-07-21 09:43:28 -07007684 }
Ben Segall1ee14e62013-10-16 11:16:12 -07007685 if (runtime_was_enabled && !runtime_enabled)
7686 cfs_bandwidth_usage_dec();
Paul Turnera790de92011-07-21 09:43:29 -07007687out_unlock:
7688 mutex_unlock(&cfs_constraints_mutex);
Kirill Tkhai0e59bda2014-06-25 12:19:42 +04007689 put_online_cpus();
Paul Turnerab84d312011-07-21 09:43:28 -07007690
Paul Turnera790de92011-07-21 09:43:29 -07007691 return ret;
Paul Turnerab84d312011-07-21 09:43:28 -07007692}
7693
YueHaibingb1546ed2019-04-18 22:47:13 +08007694static int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us)
Paul Turnerab84d312011-07-21 09:43:28 -07007695{
7696 u64 quota, period;
7697
Peter Zijlstra029632f2011-10-25 10:00:11 +02007698 period = ktime_to_ns(tg->cfs_bandwidth.period);
Paul Turnerab84d312011-07-21 09:43:28 -07007699 if (cfs_quota_us < 0)
7700 quota = RUNTIME_INF;
Konstantin Khlebnikov1a8b4542019-02-27 11:10:20 +03007701 else if ((u64)cfs_quota_us <= U64_MAX / NSEC_PER_USEC)
Paul Turnerab84d312011-07-21 09:43:28 -07007702 quota = (u64)cfs_quota_us * NSEC_PER_USEC;
Konstantin Khlebnikov1a8b4542019-02-27 11:10:20 +03007703 else
7704 return -EINVAL;
Paul Turnerab84d312011-07-21 09:43:28 -07007705
7706 return tg_set_cfs_bandwidth(tg, period, quota);
7707}
7708
YueHaibingb1546ed2019-04-18 22:47:13 +08007709static long tg_get_cfs_quota(struct task_group *tg)
Paul Turnerab84d312011-07-21 09:43:28 -07007710{
7711 u64 quota_us;
7712
Peter Zijlstra029632f2011-10-25 10:00:11 +02007713 if (tg->cfs_bandwidth.quota == RUNTIME_INF)
Paul Turnerab84d312011-07-21 09:43:28 -07007714 return -1;
7715
Peter Zijlstra029632f2011-10-25 10:00:11 +02007716 quota_us = tg->cfs_bandwidth.quota;
Paul Turnerab84d312011-07-21 09:43:28 -07007717 do_div(quota_us, NSEC_PER_USEC);
7718
7719 return quota_us;
7720}
7721
YueHaibingb1546ed2019-04-18 22:47:13 +08007722static int tg_set_cfs_period(struct task_group *tg, long cfs_period_us)
Paul Turnerab84d312011-07-21 09:43:28 -07007723{
7724 u64 quota, period;
7725
Konstantin Khlebnikov1a8b4542019-02-27 11:10:20 +03007726 if ((u64)cfs_period_us > U64_MAX / NSEC_PER_USEC)
7727 return -EINVAL;
7728
Paul Turnerab84d312011-07-21 09:43:28 -07007729 period = (u64)cfs_period_us * NSEC_PER_USEC;
Peter Zijlstra029632f2011-10-25 10:00:11 +02007730 quota = tg->cfs_bandwidth.quota;
Paul Turnerab84d312011-07-21 09:43:28 -07007731
Paul Turnerab84d312011-07-21 09:43:28 -07007732 return tg_set_cfs_bandwidth(tg, period, quota);
7733}
7734
YueHaibingb1546ed2019-04-18 22:47:13 +08007735static long tg_get_cfs_period(struct task_group *tg)
Paul Turnerab84d312011-07-21 09:43:28 -07007736{
7737 u64 cfs_period_us;
7738
Peter Zijlstra029632f2011-10-25 10:00:11 +02007739 cfs_period_us = ktime_to_ns(tg->cfs_bandwidth.period);
Paul Turnerab84d312011-07-21 09:43:28 -07007740 do_div(cfs_period_us, NSEC_PER_USEC);
7741
7742 return cfs_period_us;
7743}
7744
Tejun Heo182446d2013-08-08 20:11:24 -04007745static s64 cpu_cfs_quota_read_s64(struct cgroup_subsys_state *css,
7746 struct cftype *cft)
Paul Turnerab84d312011-07-21 09:43:28 -07007747{
Tejun Heo182446d2013-08-08 20:11:24 -04007748 return tg_get_cfs_quota(css_tg(css));
Paul Turnerab84d312011-07-21 09:43:28 -07007749}
7750
Tejun Heo182446d2013-08-08 20:11:24 -04007751static int cpu_cfs_quota_write_s64(struct cgroup_subsys_state *css,
7752 struct cftype *cftype, s64 cfs_quota_us)
Paul Turnerab84d312011-07-21 09:43:28 -07007753{
Tejun Heo182446d2013-08-08 20:11:24 -04007754 return tg_set_cfs_quota(css_tg(css), cfs_quota_us);
Paul Turnerab84d312011-07-21 09:43:28 -07007755}
7756
Tejun Heo182446d2013-08-08 20:11:24 -04007757static u64 cpu_cfs_period_read_u64(struct cgroup_subsys_state *css,
7758 struct cftype *cft)
Paul Turnerab84d312011-07-21 09:43:28 -07007759{
Tejun Heo182446d2013-08-08 20:11:24 -04007760 return tg_get_cfs_period(css_tg(css));
Paul Turnerab84d312011-07-21 09:43:28 -07007761}
7762
Tejun Heo182446d2013-08-08 20:11:24 -04007763static int cpu_cfs_period_write_u64(struct cgroup_subsys_state *css,
7764 struct cftype *cftype, u64 cfs_period_us)
Paul Turnerab84d312011-07-21 09:43:28 -07007765{
Tejun Heo182446d2013-08-08 20:11:24 -04007766 return tg_set_cfs_period(css_tg(css), cfs_period_us);
Paul Turnerab84d312011-07-21 09:43:28 -07007767}
7768
Paul Turnera790de92011-07-21 09:43:29 -07007769struct cfs_schedulable_data {
7770 struct task_group *tg;
7771 u64 period, quota;
7772};
7773
7774/*
7775 * normalize group quota/period to be quota/max_period
7776 * note: units are usecs
7777 */
7778static u64 normalize_cfs_quota(struct task_group *tg,
7779 struct cfs_schedulable_data *d)
7780{
7781 u64 quota, period;
7782
7783 if (tg == d->tg) {
7784 period = d->period;
7785 quota = d->quota;
7786 } else {
7787 period = tg_get_cfs_period(tg);
7788 quota = tg_get_cfs_quota(tg);
7789 }
7790
7791 /* note: these should typically be equivalent */
7792 if (quota == RUNTIME_INF || quota == -1)
7793 return RUNTIME_INF;
7794
7795 return to_ratio(period, quota);
7796}
7797
7798static int tg_cfs_schedulable_down(struct task_group *tg, void *data)
7799{
7800 struct cfs_schedulable_data *d = data;
Peter Zijlstra029632f2011-10-25 10:00:11 +02007801 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Paul Turnera790de92011-07-21 09:43:29 -07007802 s64 quota = 0, parent_quota = -1;
7803
7804 if (!tg->parent) {
7805 quota = RUNTIME_INF;
7806 } else {
Peter Zijlstra029632f2011-10-25 10:00:11 +02007807 struct cfs_bandwidth *parent_b = &tg->parent->cfs_bandwidth;
Paul Turnera790de92011-07-21 09:43:29 -07007808
7809 quota = normalize_cfs_quota(tg, d);
Zhihui Zhang9c58c792014-09-20 21:24:36 -04007810 parent_quota = parent_b->hierarchical_quota;
Paul Turnera790de92011-07-21 09:43:29 -07007811
7812 /*
Tejun Heoc53593e2018-01-22 11:26:18 -08007813 * Ensure max(child_quota) <= parent_quota. On cgroup2,
7814 * always take the min. On cgroup1, only inherit when no
Ingo Molnard1ccc662017-02-01 11:46:42 +01007815 * limit is set:
Paul Turnera790de92011-07-21 09:43:29 -07007816 */
Tejun Heoc53593e2018-01-22 11:26:18 -08007817 if (cgroup_subsys_on_dfl(cpu_cgrp_subsys)) {
7818 quota = min(quota, parent_quota);
7819 } else {
7820 if (quota == RUNTIME_INF)
7821 quota = parent_quota;
7822 else if (parent_quota != RUNTIME_INF && quota > parent_quota)
7823 return -EINVAL;
7824 }
Paul Turnera790de92011-07-21 09:43:29 -07007825 }
Zhihui Zhang9c58c792014-09-20 21:24:36 -04007826 cfs_b->hierarchical_quota = quota;
Paul Turnera790de92011-07-21 09:43:29 -07007827
7828 return 0;
7829}
7830
7831static int __cfs_schedulable(struct task_group *tg, u64 period, u64 quota)
7832{
Paul Turner82774342011-07-21 09:43:35 -07007833 int ret;
Paul Turnera790de92011-07-21 09:43:29 -07007834 struct cfs_schedulable_data data = {
7835 .tg = tg,
7836 .period = period,
7837 .quota = quota,
7838 };
7839
7840 if (quota != RUNTIME_INF) {
7841 do_div(data.period, NSEC_PER_USEC);
7842 do_div(data.quota, NSEC_PER_USEC);
7843 }
7844
Paul Turner82774342011-07-21 09:43:35 -07007845 rcu_read_lock();
7846 ret = walk_tg_tree(tg_cfs_schedulable_down, tg_nop, &data);
7847 rcu_read_unlock();
7848
7849 return ret;
Paul Turnera790de92011-07-21 09:43:29 -07007850}
Nikhil Raoe8da1b12011-07-21 09:43:40 -07007851
Tejun Heoa1f71642017-09-25 09:00:18 -07007852static int cpu_cfs_stat_show(struct seq_file *sf, void *v)
Nikhil Raoe8da1b12011-07-21 09:43:40 -07007853{
Tejun Heo2da8ca82013-12-05 12:28:04 -05007854 struct task_group *tg = css_tg(seq_css(sf));
Peter Zijlstra029632f2011-10-25 10:00:11 +02007855 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
Nikhil Raoe8da1b12011-07-21 09:43:40 -07007856
Tejun Heo44ffc752013-12-05 12:28:01 -05007857 seq_printf(sf, "nr_periods %d\n", cfs_b->nr_periods);
7858 seq_printf(sf, "nr_throttled %d\n", cfs_b->nr_throttled);
7859 seq_printf(sf, "throttled_time %llu\n", cfs_b->throttled_time);
Nikhil Raoe8da1b12011-07-21 09:43:40 -07007860
Yun Wang3d6c50c2018-07-04 11:27:27 +08007861 if (schedstat_enabled() && tg != &root_task_group) {
7862 u64 ws = 0;
7863 int i;
7864
7865 for_each_possible_cpu(i)
7866 ws += schedstat_val(tg->se[i]->statistics.wait_sum);
7867
7868 seq_printf(sf, "wait_sum %llu\n", ws);
7869 }
7870
Nikhil Raoe8da1b12011-07-21 09:43:40 -07007871 return 0;
7872}
Paul Turnerab84d312011-07-21 09:43:28 -07007873#endif /* CONFIG_CFS_BANDWIDTH */
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007874#endif /* CONFIG_FAIR_GROUP_SCHED */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007875
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007876#ifdef CONFIG_RT_GROUP_SCHED
Tejun Heo182446d2013-08-08 20:11:24 -04007877static int cpu_rt_runtime_write(struct cgroup_subsys_state *css,
7878 struct cftype *cft, s64 val)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007879{
Tejun Heo182446d2013-08-08 20:11:24 -04007880 return sched_group_set_rt_runtime(css_tg(css), val);
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007881}
7882
Tejun Heo182446d2013-08-08 20:11:24 -04007883static s64 cpu_rt_runtime_read(struct cgroup_subsys_state *css,
7884 struct cftype *cft)
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007885{
Tejun Heo182446d2013-08-08 20:11:24 -04007886 return sched_group_rt_runtime(css_tg(css));
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007887}
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007888
Tejun Heo182446d2013-08-08 20:11:24 -04007889static int cpu_rt_period_write_uint(struct cgroup_subsys_state *css,
7890 struct cftype *cftype, u64 rt_period_us)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007891{
Tejun Heo182446d2013-08-08 20:11:24 -04007892 return sched_group_set_rt_period(css_tg(css), rt_period_us);
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007893}
7894
Tejun Heo182446d2013-08-08 20:11:24 -04007895static u64 cpu_rt_period_read_uint(struct cgroup_subsys_state *css,
7896 struct cftype *cft)
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007897{
Tejun Heo182446d2013-08-08 20:11:24 -04007898 return sched_group_rt_period(css_tg(css));
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007899}
Dhaval Giani6d6bc0a2008-05-30 14:23:45 +02007900#endif /* CONFIG_RT_GROUP_SCHED */
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007901
Tejun Heoa1f71642017-09-25 09:00:18 -07007902static struct cftype cpu_legacy_files[] = {
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007903#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Menagefe5c7cc2007-10-29 21:18:11 +01007904 {
7905 .name = "shares",
Paul Menagef4c753b2008-04-29 00:59:56 -07007906 .read_u64 = cpu_shares_read_u64,
7907 .write_u64 = cpu_shares_write_u64,
Paul Menagefe5c7cc2007-10-29 21:18:11 +01007908 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007909#endif
Paul Turnerab84d312011-07-21 09:43:28 -07007910#ifdef CONFIG_CFS_BANDWIDTH
7911 {
7912 .name = "cfs_quota_us",
7913 .read_s64 = cpu_cfs_quota_read_s64,
7914 .write_s64 = cpu_cfs_quota_write_s64,
7915 },
7916 {
7917 .name = "cfs_period_us",
7918 .read_u64 = cpu_cfs_period_read_u64,
7919 .write_u64 = cpu_cfs_period_write_u64,
7920 },
Nikhil Raoe8da1b12011-07-21 09:43:40 -07007921 {
7922 .name = "stat",
Tejun Heoa1f71642017-09-25 09:00:18 -07007923 .seq_show = cpu_cfs_stat_show,
Nikhil Raoe8da1b12011-07-21 09:43:40 -07007924 },
Paul Turnerab84d312011-07-21 09:43:28 -07007925#endif
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007926#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007927 {
Peter Zijlstra9f0c1e52008-02-13 15:45:39 +01007928 .name = "rt_runtime_us",
Paul Menage06ecb272008-04-29 01:00:06 -07007929 .read_s64 = cpu_rt_runtime_read,
7930 .write_s64 = cpu_rt_runtime_write,
Peter Zijlstra6f505b12008-01-25 21:08:30 +01007931 },
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007932 {
7933 .name = "rt_period_us",
Paul Menagef4c753b2008-04-29 00:59:56 -07007934 .read_u64 = cpu_rt_period_read_uint,
7935 .write_u64 = cpu_rt_period_write_uint,
Peter Zijlstrad0b27fa2008-04-19 19:44:57 +02007936 },
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01007937#endif
Patrick Bellasi2480c092019-08-22 14:28:06 +01007938#ifdef CONFIG_UCLAMP_TASK_GROUP
7939 {
7940 .name = "uclamp.min",
7941 .flags = CFTYPE_NOT_ON_ROOT,
7942 .seq_show = cpu_uclamp_min_show,
7943 .write = cpu_uclamp_min_write,
7944 },
7945 {
7946 .name = "uclamp.max",
7947 .flags = CFTYPE_NOT_ON_ROOT,
7948 .seq_show = cpu_uclamp_max_show,
7949 .write = cpu_uclamp_max_write,
7950 },
7951#endif
Ingo Molnard1ccc662017-02-01 11:46:42 +01007952 { } /* Terminate */
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07007953};
7954
Tejun Heod41bf8c2017-10-23 16:18:27 -07007955static int cpu_extra_stat_show(struct seq_file *sf,
7956 struct cgroup_subsys_state *css)
Tejun Heo0d593632017-09-25 09:00:19 -07007957{
Tejun Heo0d593632017-09-25 09:00:19 -07007958#ifdef CONFIG_CFS_BANDWIDTH
7959 {
Tejun Heod41bf8c2017-10-23 16:18:27 -07007960 struct task_group *tg = css_tg(css);
Tejun Heo0d593632017-09-25 09:00:19 -07007961 struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
7962 u64 throttled_usec;
7963
7964 throttled_usec = cfs_b->throttled_time;
7965 do_div(throttled_usec, NSEC_PER_USEC);
7966
7967 seq_printf(sf, "nr_periods %d\n"
7968 "nr_throttled %d\n"
7969 "throttled_usec %llu\n",
7970 cfs_b->nr_periods, cfs_b->nr_throttled,
7971 throttled_usec);
7972 }
7973#endif
7974 return 0;
7975}
7976
7977#ifdef CONFIG_FAIR_GROUP_SCHED
7978static u64 cpu_weight_read_u64(struct cgroup_subsys_state *css,
7979 struct cftype *cft)
7980{
7981 struct task_group *tg = css_tg(css);
7982 u64 weight = scale_load_down(tg->shares);
7983
7984 return DIV_ROUND_CLOSEST_ULL(weight * CGROUP_WEIGHT_DFL, 1024);
7985}
7986
7987static int cpu_weight_write_u64(struct cgroup_subsys_state *css,
7988 struct cftype *cft, u64 weight)
7989{
7990 /*
7991 * cgroup weight knobs should use the common MIN, DFL and MAX
7992 * values which are 1, 100 and 10000 respectively. While it loses
7993 * a bit of range on both ends, it maps pretty well onto the shares
7994 * value used by scheduler and the round-trip conversions preserve
7995 * the original value over the entire range.
7996 */
7997 if (weight < CGROUP_WEIGHT_MIN || weight > CGROUP_WEIGHT_MAX)
7998 return -ERANGE;
7999
8000 weight = DIV_ROUND_CLOSEST_ULL(weight * 1024, CGROUP_WEIGHT_DFL);
8001
8002 return sched_group_set_shares(css_tg(css), scale_load(weight));
8003}
8004
8005static s64 cpu_weight_nice_read_s64(struct cgroup_subsys_state *css,
8006 struct cftype *cft)
8007{
8008 unsigned long weight = scale_load_down(css_tg(css)->shares);
8009 int last_delta = INT_MAX;
8010 int prio, delta;
8011
8012 /* find the closest nice value to the current weight */
8013 for (prio = 0; prio < ARRAY_SIZE(sched_prio_to_weight); prio++) {
8014 delta = abs(sched_prio_to_weight[prio] - weight);
8015 if (delta >= last_delta)
8016 break;
8017 last_delta = delta;
8018 }
8019
8020 return PRIO_TO_NICE(prio - 1 + MAX_RT_PRIO);
8021}
8022
8023static int cpu_weight_nice_write_s64(struct cgroup_subsys_state *css,
8024 struct cftype *cft, s64 nice)
8025{
8026 unsigned long weight;
Peter Zijlstra7281c8d2018-04-20 14:29:51 +02008027 int idx;
Tejun Heo0d593632017-09-25 09:00:19 -07008028
8029 if (nice < MIN_NICE || nice > MAX_NICE)
8030 return -ERANGE;
8031
Peter Zijlstra7281c8d2018-04-20 14:29:51 +02008032 idx = NICE_TO_PRIO(nice) - MAX_RT_PRIO;
8033 idx = array_index_nospec(idx, 40);
8034 weight = sched_prio_to_weight[idx];
8035
Tejun Heo0d593632017-09-25 09:00:19 -07008036 return sched_group_set_shares(css_tg(css), scale_load(weight));
8037}
8038#endif
8039
8040static void __maybe_unused cpu_period_quota_print(struct seq_file *sf,
8041 long period, long quota)
8042{
8043 if (quota < 0)
8044 seq_puts(sf, "max");
8045 else
8046 seq_printf(sf, "%ld", quota);
8047
8048 seq_printf(sf, " %ld\n", period);
8049}
8050
8051/* caller should put the current value in *@periodp before calling */
8052static int __maybe_unused cpu_period_quota_parse(char *buf,
8053 u64 *periodp, u64 *quotap)
8054{
8055 char tok[21]; /* U64_MAX */
8056
Konstantin Khlebnikov4c47acd2019-03-06 20:11:42 +03008057 if (sscanf(buf, "%20s %llu", tok, periodp) < 1)
Tejun Heo0d593632017-09-25 09:00:19 -07008058 return -EINVAL;
8059
8060 *periodp *= NSEC_PER_USEC;
8061
8062 if (sscanf(tok, "%llu", quotap))
8063 *quotap *= NSEC_PER_USEC;
8064 else if (!strcmp(tok, "max"))
8065 *quotap = RUNTIME_INF;
8066 else
8067 return -EINVAL;
8068
8069 return 0;
8070}
8071
8072#ifdef CONFIG_CFS_BANDWIDTH
8073static int cpu_max_show(struct seq_file *sf, void *v)
8074{
8075 struct task_group *tg = css_tg(seq_css(sf));
8076
8077 cpu_period_quota_print(sf, tg_get_cfs_period(tg), tg_get_cfs_quota(tg));
8078 return 0;
8079}
8080
8081static ssize_t cpu_max_write(struct kernfs_open_file *of,
8082 char *buf, size_t nbytes, loff_t off)
8083{
8084 struct task_group *tg = css_tg(of_css(of));
8085 u64 period = tg_get_cfs_period(tg);
8086 u64 quota;
8087 int ret;
8088
8089 ret = cpu_period_quota_parse(buf, &period, &quota);
8090 if (!ret)
8091 ret = tg_set_cfs_bandwidth(tg, period, quota);
8092 return ret ?: nbytes;
8093}
8094#endif
8095
8096static struct cftype cpu_files[] = {
Tejun Heo0d593632017-09-25 09:00:19 -07008097#ifdef CONFIG_FAIR_GROUP_SCHED
8098 {
8099 .name = "weight",
8100 .flags = CFTYPE_NOT_ON_ROOT,
8101 .read_u64 = cpu_weight_read_u64,
8102 .write_u64 = cpu_weight_write_u64,
8103 },
8104 {
8105 .name = "weight.nice",
8106 .flags = CFTYPE_NOT_ON_ROOT,
8107 .read_s64 = cpu_weight_nice_read_s64,
8108 .write_s64 = cpu_weight_nice_write_s64,
8109 },
8110#endif
8111#ifdef CONFIG_CFS_BANDWIDTH
8112 {
8113 .name = "max",
8114 .flags = CFTYPE_NOT_ON_ROOT,
8115 .seq_show = cpu_max_show,
8116 .write = cpu_max_write,
8117 },
8118#endif
Patrick Bellasi2480c092019-08-22 14:28:06 +01008119#ifdef CONFIG_UCLAMP_TASK_GROUP
8120 {
8121 .name = "uclamp.min",
8122 .flags = CFTYPE_NOT_ON_ROOT,
8123 .seq_show = cpu_uclamp_min_show,
8124 .write = cpu_uclamp_min_write,
8125 },
8126 {
8127 .name = "uclamp.max",
8128 .flags = CFTYPE_NOT_ON_ROOT,
8129 .seq_show = cpu_uclamp_max_show,
8130 .write = cpu_uclamp_max_write,
8131 },
8132#endif
Tejun Heo0d593632017-09-25 09:00:19 -07008133 { } /* terminate */
8134};
8135
Tejun Heo073219e2014-02-08 10:36:58 -05008136struct cgroup_subsys cpu_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08008137 .css_alloc = cpu_cgroup_css_alloc,
Konstantin Khlebnikov96b77742017-02-08 14:27:27 +03008138 .css_online = cpu_cgroup_css_online,
Peter Zijlstra2f5177f2016-03-16 16:22:45 +01008139 .css_released = cpu_cgroup_css_released,
Tejun Heo92fb9742012-11-19 08:13:38 -08008140 .css_free = cpu_cgroup_css_free,
Tejun Heod41bf8c2017-10-23 16:18:27 -07008141 .css_extra_stat_show = cpu_extra_stat_show,
Kirill Tkhaieeb61e52014-10-27 14:18:25 +04008142 .fork = cpu_cgroup_fork,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008143 .can_attach = cpu_cgroup_can_attach,
8144 .attach = cpu_cgroup_attach,
Tejun Heoa1f71642017-09-25 09:00:18 -07008145 .legacy_cftypes = cpu_legacy_files,
Tejun Heo0d593632017-09-25 09:00:19 -07008146 .dfl_cftypes = cpu_files,
Tejun Heob38e42e2016-02-23 10:00:50 -05008147 .early_init = true,
Tejun Heo0d593632017-09-25 09:00:19 -07008148 .threaded = true,
Srivatsa Vaddagiri68318b82007-10-18 23:41:03 -07008149};
8150
Peter Zijlstra052f1dc2008-02-13 15:45:40 +01008151#endif /* CONFIG_CGROUP_SCHED */
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +01008152
Paul E. McKenneyb637a322012-09-19 16:58:38 -07008153void dump_cpu_task(int cpu)
8154{
8155 pr_info("Task dump for CPU %d:\n", cpu);
8156 sched_show_task(cpu_curr(cpu));
8157}
Andi Kleened82b8a2015-11-29 20:59:43 -08008158
8159/*
8160 * Nice levels are multiplicative, with a gentle 10% change for every
8161 * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
8162 * nice 1, it will get ~10% less CPU time than another CPU-bound task
8163 * that remained on nice 0.
8164 *
8165 * The "10% effect" is relative and cumulative: from _any_ nice level,
8166 * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
8167 * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
8168 * If a task goes up by ~10% and another task goes down by ~10% then
8169 * the relative distance between them is ~25%.)
8170 */
8171const int sched_prio_to_weight[40] = {
8172 /* -20 */ 88761, 71755, 56483, 46273, 36291,
8173 /* -15 */ 29154, 23254, 18705, 14949, 11916,
8174 /* -10 */ 9548, 7620, 6100, 4904, 3906,
8175 /* -5 */ 3121, 2501, 1991, 1586, 1277,
8176 /* 0 */ 1024, 820, 655, 526, 423,
8177 /* 5 */ 335, 272, 215, 172, 137,
8178 /* 10 */ 110, 87, 70, 56, 45,
8179 /* 15 */ 36, 29, 23, 18, 15,
8180};
8181
8182/*
8183 * Inverse (2^32/x) values of the sched_prio_to_weight[] array, precalculated.
8184 *
8185 * In cases where the weight does not change often, we can use the
8186 * precalculated inverse to speed up arithmetics by turning divisions
8187 * into multiplications:
8188 */
8189const u32 sched_prio_to_wmult[40] = {
8190 /* -20 */ 48388, 59856, 76040, 92818, 118348,
8191 /* -15 */ 147320, 184698, 229616, 287308, 360437,
8192 /* -10 */ 449829, 563644, 704093, 875809, 1099582,
8193 /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
8194 /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
8195 /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
8196 /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
8197 /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
8198};
Ingo Molnar14a74052018-03-03 16:32:24 +01008199
Phil Auld9d246052020-06-29 15:23:03 -04008200void call_trace_sched_update_nr_running(struct rq *rq, int count)
8201{
8202 trace_sched_update_nr_running_tp(rq, count);
8203}