blob: 5bcb86eb026b171e5dcfb2d3bb5e29d2c6a9776e [file] [log] [blame]
Peter Zijlstra029632f2011-10-25 10:00:11 +02001
2#include <linux/sched.h>
Ingo Molnardfc34012017-02-03 23:15:21 +01003#include <linux/sched/autogroup.h>
Clark Williamscf4aebc22013-02-07 09:46:59 -06004#include <linux/sched/sysctl.h>
Ingo Molnar105ab3d2017-02-01 16:36:40 +01005#include <linux/sched/topology.h>
Clark Williams8bd75c72013-02-07 09:47:07 -06006#include <linux/sched/rt.h>
Ingo Molnaref8bd772017-02-08 18:51:36 +01007#include <linux/sched/deadline.h>
Ingo Molnare6017572017-02-01 16:36:40 +01008#include <linux/sched/clock.h>
Ingo Molnar84f001e2017-02-01 16:36:40 +01009#include <linux/sched/wake_q.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010010#include <linux/sched/signal.h>
Ingo Molnar6a3827d2017-02-08 18:51:31 +010011#include <linux/sched/numa_balancing.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010012#include <linux/sched/mm.h>
Ingo Molnar55687da2017-02-08 18:51:31 +010013#include <linux/sched/cpufreq.h>
Ingo Molnar03441a32017-02-08 18:51:35 +010014#include <linux/sched/stat.h>
Ingo Molnar370c9132017-02-08 18:51:35 +010015#include <linux/sched/nohz.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010016#include <linux/sched/debug.h>
Ingo Molnaref8bd772017-02-08 18:51:36 +010017#include <linux/sched/hotplug.h>
Ingo Molnar29930022017-02-08 18:51:36 +010018#include <linux/sched/task.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010019#include <linux/sched/task_stack.h>
Ingo Molnar32ef5512017-02-05 11:48:36 +010020#include <linux/sched/cputime.h>
Ingo Molnar1777e462017-02-05 14:47:12 +010021#include <linux/sched/init.h>
Ingo Molnaref8bd772017-02-08 18:51:36 +010022
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +020023#include <linux/u64_stats_sync.h>
Frederic Weisbeckera499a5a2017-01-31 04:09:32 +010024#include <linux/kernel_stat.h>
Steven Rostedt (Red Hat)3866e842016-02-22 16:26:51 -050025#include <linux/binfmts.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020026#include <linux/mutex.h>
27#include <linux/spinlock.h>
28#include <linux/stop_machine.h>
Steven Rostedtb6366f02015-03-18 14:49:46 -040029#include <linux/irq_work.h>
Frederic Weisbecker9f3660c2013-04-20 14:35:09 +020030#include <linux/tick.h>
Mel Gormanf809ca92013-10-07 11:28:57 +010031#include <linux/slab.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020032
Ingo Molnar7fce7772017-02-02 14:47:27 +010033#ifdef CONFIG_PARAVIRT
34#include <asm/paravirt.h>
35#endif
36
Peter Zijlstra391e43d2011-11-15 17:14:39 +010037#include "cpupri.h"
Juri Lelli6bfd6d72013-11-07 14:43:47 +010038#include "cpudeadline.h"
Li Zefan60fed782013-03-29 14:36:43 +080039#include "cpuacct.h"
Peter Zijlstra029632f2011-10-25 10:00:11 +020040
Peter Zijlstra9148a3a2016-09-20 22:34:51 +020041#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar6d3aed32017-06-20 12:24:42 +020042# define SCHED_WARN_ON(x) WARN_ONCE(x, #x)
Peter Zijlstra9148a3a2016-09-20 22:34:51 +020043#else
Ingo Molnar6d3aed32017-06-20 12:24:42 +020044# define SCHED_WARN_ON(x) ({ (void)(x), 0; })
Peter Zijlstra9148a3a2016-09-20 22:34:51 +020045#endif
46
Paul Gortmaker45ceebf2013-04-19 15:10:49 -040047struct rq;
Daniel Lezcano442bf3a2014-09-04 11:32:09 -040048struct cpuidle_state;
Paul Gortmaker45ceebf2013-04-19 15:10:49 -040049
Kirill Tkhaida0c1e62014-08-20 13:47:32 +040050/* task_struct::on_rq states: */
51#define TASK_ON_RQ_QUEUED 1
Kirill Tkhaicca26e82014-08-20 13:47:42 +040052#define TASK_ON_RQ_MIGRATING 2
Kirill Tkhaida0c1e62014-08-20 13:47:32 +040053
Peter Zijlstra029632f2011-10-25 10:00:11 +020054extern __read_mostly int scheduler_running;
55
Paul Gortmaker45ceebf2013-04-19 15:10:49 -040056extern unsigned long calc_load_update;
57extern atomic_long_t calc_load_tasks;
58
Peter Zijlstra3289bdb2015-04-14 13:19:42 +020059extern void calc_global_load_tick(struct rq *this_rq);
Thomas Gleixnerd60585c2016-07-12 18:33:56 +020060extern long calc_load_fold_active(struct rq *this_rq, long adjust);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +020061
62#ifdef CONFIG_SMP
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +020063extern void cpu_load_update_active(struct rq *this_rq);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +020064#else
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +020065static inline void cpu_load_update_active(struct rq *this_rq) { }
Peter Zijlstra3289bdb2015-04-14 13:19:42 +020066#endif
Paul Gortmaker45ceebf2013-04-19 15:10:49 -040067
Peter Zijlstra029632f2011-10-25 10:00:11 +020068/*
Peter Zijlstra029632f2011-10-25 10:00:11 +020069 * Helpers for converting nanosecond timing to jiffy resolution
70 */
71#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
72
Li Zefancc1f4b12013-03-05 16:06:09 +080073/*
74 * Increase resolution of nice-level calculations for 64-bit architectures.
75 * The extra resolution improves shares distribution and load balancing of
76 * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup
77 * hierarchies, especially on larger systems. This is not a user-visible change
78 * and does not change the user-interface for setting shares/weights.
79 *
80 * We increase resolution only if we have enough bits to allow this increased
Peter Zijlstra21591972016-04-28 12:49:38 +020081 * resolution (i.e. 64bit). The costs for increasing resolution when 32bit are
82 * pretty high and the returns do not justify the increased costs.
83 *
84 * Really only required when CONFIG_FAIR_GROUP_SCHED is also set, but to
85 * increase coverage and consistency always enable it on 64bit platforms.
Li Zefancc1f4b12013-03-05 16:06:09 +080086 */
Peter Zijlstra21591972016-04-28 12:49:38 +020087#ifdef CONFIG_64BIT
Yuyang Du172895e2016-04-05 12:12:27 +080088# define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT + SCHED_FIXEDPOINT_SHIFT)
Yuyang Du6ecdd742016-04-05 12:12:26 +080089# define scale_load(w) ((w) << SCHED_FIXEDPOINT_SHIFT)
90# define scale_load_down(w) ((w) >> SCHED_FIXEDPOINT_SHIFT)
Li Zefancc1f4b12013-03-05 16:06:09 +080091#else
Yuyang Du172895e2016-04-05 12:12:27 +080092# define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT)
Li Zefancc1f4b12013-03-05 16:06:09 +080093# define scale_load(w) (w)
94# define scale_load_down(w) (w)
95#endif
96
Yuyang Du6ecdd742016-04-05 12:12:26 +080097/*
Yuyang Du172895e2016-04-05 12:12:27 +080098 * Task weight (visible to users) and its load (invisible to users) have
99 * independent resolution, but they should be well calibrated. We use
100 * scale_load() and scale_load_down(w) to convert between them. The
101 * following must be true:
102 *
103 * scale_load(sched_prio_to_weight[USER_PRIO(NICE_TO_PRIO(0))]) == NICE_0_LOAD
104 *
Yuyang Du6ecdd742016-04-05 12:12:26 +0800105 */
Yuyang Du172895e2016-04-05 12:12:27 +0800106#define NICE_0_LOAD (1L << NICE_0_LOAD_SHIFT)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200107
108/*
Dario Faggioli332ac172013-11-07 14:43:45 +0100109 * Single value that decides SCHED_DEADLINE internal math precision.
110 * 10 -> just above 1us
111 * 9 -> just above 0.5us
112 */
113#define DL_SCALE (10)
114
115/*
Peter Zijlstra029632f2011-10-25 10:00:11 +0200116 * These are the 'tuning knobs' of the scheduler:
Peter Zijlstra029632f2011-10-25 10:00:11 +0200117 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200118
119/*
120 * single value that denotes runtime == period, ie unlimited time.
121 */
122#define RUNTIME_INF ((u64)~0ULL)
123
Henrik Austad20f9cd22015-09-09 17:00:41 +0200124static inline int idle_policy(int policy)
125{
126 return policy == SCHED_IDLE;
127}
Dario Faggiolid50dde52013-11-07 14:43:36 +0100128static inline int fair_policy(int policy)
129{
130 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
131}
132
Peter Zijlstra029632f2011-10-25 10:00:11 +0200133static inline int rt_policy(int policy)
134{
Dario Faggiolid50dde52013-11-07 14:43:36 +0100135 return policy == SCHED_FIFO || policy == SCHED_RR;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200136}
137
Dario Faggioliaab03e02013-11-28 11:14:43 +0100138static inline int dl_policy(int policy)
139{
140 return policy == SCHED_DEADLINE;
141}
Henrik Austad20f9cd22015-09-09 17:00:41 +0200142static inline bool valid_policy(int policy)
143{
144 return idle_policy(policy) || fair_policy(policy) ||
145 rt_policy(policy) || dl_policy(policy);
146}
Dario Faggioliaab03e02013-11-28 11:14:43 +0100147
Peter Zijlstra029632f2011-10-25 10:00:11 +0200148static inline int task_has_rt_policy(struct task_struct *p)
149{
150 return rt_policy(p->policy);
151}
152
Dario Faggioliaab03e02013-11-28 11:14:43 +0100153static inline int task_has_dl_policy(struct task_struct *p)
154{
155 return dl_policy(p->policy);
156}
157
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100158/*
159 * Tells if entity @a should preempt entity @b.
160 */
Dario Faggioli332ac172013-11-07 14:43:45 +0100161static inline bool
162dl_entity_preempt(struct sched_dl_entity *a, struct sched_dl_entity *b)
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100163{
164 return dl_time_before(a->deadline, b->deadline);
165}
166
Peter Zijlstra029632f2011-10-25 10:00:11 +0200167/*
168 * This is the priority-queue data structure of the RT scheduling class:
169 */
170struct rt_prio_array {
171 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
172 struct list_head queue[MAX_RT_PRIO];
173};
174
175struct rt_bandwidth {
176 /* nests inside the rq lock: */
177 raw_spinlock_t rt_runtime_lock;
178 ktime_t rt_period;
179 u64 rt_runtime;
180 struct hrtimer rt_period_timer;
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200181 unsigned int rt_period_active;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200182};
Juri Lellia5e7be32014-09-19 10:22:39 +0100183
184void __dl_clear_params(struct task_struct *p);
185
Dario Faggioli332ac172013-11-07 14:43:45 +0100186/*
187 * To keep the bandwidth of -deadline tasks and groups under control
188 * we need some place where:
189 * - store the maximum -deadline bandwidth of the system (the group);
190 * - cache the fraction of that bandwidth that is currently allocated.
191 *
192 * This is all done in the data structure below. It is similar to the
193 * one used for RT-throttling (rt_bandwidth), with the main difference
194 * that, since here we are only interested in admission control, we
195 * do not decrease any runtime while the group "executes", neither we
196 * need a timer to replenish it.
197 *
198 * With respect to SMP, the bandwidth is given on a per-CPU basis,
199 * meaning that:
200 * - dl_bw (< 100%) is the bandwidth of the system (group) on each CPU;
201 * - dl_total_bw array contains, in the i-eth element, the currently
202 * allocated bandwidth on the i-eth CPU.
203 * Moreover, groups consume bandwidth on each CPU, while tasks only
204 * consume bandwidth on the CPU they're running on.
205 * Finally, dl_total_bw_cpu is used to cache the index of dl_total_bw
206 * that will be shown the next time the proc or cgroup controls will
207 * be red. It on its turn can be changed by writing on its own
208 * control.
209 */
210struct dl_bandwidth {
211 raw_spinlock_t dl_runtime_lock;
212 u64 dl_runtime;
213 u64 dl_period;
214};
215
216static inline int dl_bandwidth_enabled(void)
217{
Peter Zijlstra17248132013-12-17 12:44:49 +0100218 return sysctl_sched_rt_runtime >= 0;
Dario Faggioli332ac172013-11-07 14:43:45 +0100219}
220
Dario Faggioli332ac172013-11-07 14:43:45 +0100221struct dl_bw {
222 raw_spinlock_t lock;
223 u64 bw, total_bw;
224};
225
Luca Abenidaec5792017-05-18 22:13:36 +0200226static inline void __dl_update(struct dl_bw *dl_b, s64 bw);
227
Juri Lelli7f514122014-09-19 10:22:40 +0100228static inline
Luca Abenidaec5792017-05-18 22:13:36 +0200229void __dl_clear(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
Juri Lelli7f514122014-09-19 10:22:40 +0100230{
231 dl_b->total_bw -= tsk_bw;
Luca Abenidaec5792017-05-18 22:13:36 +0200232 __dl_update(dl_b, (s32)tsk_bw / cpus);
Juri Lelli7f514122014-09-19 10:22:40 +0100233}
234
235static inline
Luca Abenidaec5792017-05-18 22:13:36 +0200236void __dl_add(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
Juri Lelli7f514122014-09-19 10:22:40 +0100237{
238 dl_b->total_bw += tsk_bw;
Luca Abenidaec5792017-05-18 22:13:36 +0200239 __dl_update(dl_b, -((s32)tsk_bw / cpus));
Juri Lelli7f514122014-09-19 10:22:40 +0100240}
241
242static inline
243bool __dl_overflow(struct dl_bw *dl_b, int cpus, u64 old_bw, u64 new_bw)
244{
245 return dl_b->bw != -1 &&
246 dl_b->bw * cpus < dl_b->total_bw - old_bw + new_bw;
247}
248
Luca Abeni209a0cb2017-05-18 22:13:29 +0200249void dl_change_utilization(struct task_struct *p, u64 new_bw);
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100250extern void init_dl_bw(struct dl_bw *dl_b);
Nicolas Pitre06a76fe2017-06-21 14:22:01 -0400251extern int sched_dl_global_validate(void);
252extern void sched_dl_do_global(void);
253extern int sched_dl_overflow(struct task_struct *p, int policy,
254 const struct sched_attr *attr);
255extern void __setparam_dl(struct task_struct *p, const struct sched_attr *attr);
256extern void __getparam_dl(struct task_struct *p, struct sched_attr *attr);
257extern bool __checkparam_dl(const struct sched_attr *attr);
258extern void __dl_clear_params(struct task_struct *p);
259extern bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr);
260extern int dl_task_can_attach(struct task_struct *p,
261 const struct cpumask *cs_cpus_allowed);
262extern int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur,
263 const struct cpumask *trial);
264extern bool dl_cpu_busy(unsigned int cpu);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200265
266#ifdef CONFIG_CGROUP_SCHED
267
268#include <linux/cgroup.h>
269
270struct cfs_rq;
271struct rt_rq;
272
Mike Galbraith35cf4e52012-08-07 05:00:13 +0200273extern struct list_head task_groups;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200274
275struct cfs_bandwidth {
276#ifdef CONFIG_CFS_BANDWIDTH
277 raw_spinlock_t lock;
278 ktime_t period;
279 u64 quota, runtime;
Zhihui Zhang9c58c792014-09-20 21:24:36 -0400280 s64 hierarchical_quota;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200281 u64 runtime_expires;
282
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200283 int idle, period_active;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200284 struct hrtimer period_timer, slack_timer;
285 struct list_head throttled_cfs_rq;
286
287 /* statistics */
288 int nr_periods, nr_throttled;
289 u64 throttled_time;
290#endif
291};
292
293/* task group related information */
294struct task_group {
295 struct cgroup_subsys_state css;
296
297#ifdef CONFIG_FAIR_GROUP_SCHED
298 /* schedulable entities of this group on each cpu */
299 struct sched_entity **se;
300 /* runqueue "owned" by this group on each cpu */
301 struct cfs_rq **cfs_rq;
302 unsigned long shares;
303
Alex Shifa6bdde2013-06-20 10:18:46 +0800304#ifdef CONFIG_SMP
Waiman Longb0367622015-12-02 13:41:49 -0500305 /*
306 * load_avg can be heavily contended at clock tick time, so put
307 * it in its own cacheline separated from the fields above which
308 * will also be accessed at each tick.
309 */
310 atomic_long_t load_avg ____cacheline_aligned;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200311#endif
Alex Shifa6bdde2013-06-20 10:18:46 +0800312#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200313
314#ifdef CONFIG_RT_GROUP_SCHED
315 struct sched_rt_entity **rt_se;
316 struct rt_rq **rt_rq;
317
318 struct rt_bandwidth rt_bandwidth;
319#endif
320
321 struct rcu_head rcu;
322 struct list_head list;
323
324 struct task_group *parent;
325 struct list_head siblings;
326 struct list_head children;
327
328#ifdef CONFIG_SCHED_AUTOGROUP
329 struct autogroup *autogroup;
330#endif
331
332 struct cfs_bandwidth cfs_bandwidth;
333};
334
335#ifdef CONFIG_FAIR_GROUP_SCHED
336#define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
337
338/*
339 * A weight of 0 or 1 can cause arithmetics problems.
340 * A weight of a cfs_rq is the sum of weights of which entities
341 * are queued on this cfs_rq, so a weight of a entity should not be
342 * too large, so as the shares value of a task group.
343 * (The default weight is 1024 - so there's no practical
344 * limitation from this.)
345 */
346#define MIN_SHARES (1UL << 1)
347#define MAX_SHARES (1UL << 18)
348#endif
349
Peter Zijlstra029632f2011-10-25 10:00:11 +0200350typedef int (*tg_visitor)(struct task_group *, void *);
351
352extern int walk_tg_tree_from(struct task_group *from,
353 tg_visitor down, tg_visitor up, void *data);
354
355/*
356 * Iterate the full tree, calling @down when first entering a node and @up when
357 * leaving it for the final time.
358 *
359 * Caller must hold rcu_lock or sufficient equivalent.
360 */
361static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
362{
363 return walk_tg_tree_from(&root_task_group, down, up, data);
364}
365
366extern int tg_nop(struct task_group *tg, void *data);
367
368extern void free_fair_sched_group(struct task_group *tg);
369extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
Peter Zijlstra8663e242016-06-22 14:58:02 +0200370extern void online_fair_sched_group(struct task_group *tg);
Peter Zijlstra6fe1f342016-01-21 22:24:16 +0100371extern void unregister_fair_sched_group(struct task_group *tg);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200372extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
373 struct sched_entity *se, int cpu,
374 struct sched_entity *parent);
375extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200376
377extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +0200378extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200379extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
380
381extern void free_rt_sched_group(struct task_group *tg);
382extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
383extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
384 struct sched_rt_entity *rt_se, int cpu,
385 struct sched_rt_entity *parent);
Nicolas Pitre8887cd92017-06-21 14:22:02 -0400386extern int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us);
387extern int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us);
388extern long sched_group_rt_runtime(struct task_group *tg);
389extern long sched_group_rt_period(struct task_group *tg);
390extern int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200391
Li Zefan25cc7da2013-03-05 16:07:33 +0800392extern struct task_group *sched_create_group(struct task_group *parent);
393extern void sched_online_group(struct task_group *tg,
394 struct task_group *parent);
395extern void sched_destroy_group(struct task_group *tg);
396extern void sched_offline_group(struct task_group *tg);
397
398extern void sched_move_task(struct task_struct *tsk);
399
400#ifdef CONFIG_FAIR_GROUP_SCHED
401extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
Byungchul Parkad936d82015-10-24 01:16:19 +0900402
403#ifdef CONFIG_SMP
404extern void set_task_rq_fair(struct sched_entity *se,
405 struct cfs_rq *prev, struct cfs_rq *next);
406#else /* !CONFIG_SMP */
407static inline void set_task_rq_fair(struct sched_entity *se,
408 struct cfs_rq *prev, struct cfs_rq *next) { }
409#endif /* CONFIG_SMP */
410#endif /* CONFIG_FAIR_GROUP_SCHED */
Li Zefan25cc7da2013-03-05 16:07:33 +0800411
Peter Zijlstra029632f2011-10-25 10:00:11 +0200412#else /* CONFIG_CGROUP_SCHED */
413
414struct cfs_bandwidth { };
415
416#endif /* CONFIG_CGROUP_SCHED */
417
418/* CFS-related fields in a runqueue */
419struct cfs_rq {
420 struct load_weight load;
Peter Zijlstrac82513e2012-04-26 13:12:27 +0200421 unsigned int nr_running, h_nr_running;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200422
423 u64 exec_clock;
424 u64 min_vruntime;
425#ifndef CONFIG_64BIT
426 u64 min_vruntime_copy;
427#endif
428
Davidlohr Buesobfb06882017-09-08 16:14:55 -0700429 struct rb_root_cached tasks_timeline;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200430
Peter Zijlstra029632f2011-10-25 10:00:11 +0200431 /*
432 * 'curr' points to currently running entity on this cfs_rq.
433 * It is set to NULL otherwise (i.e when none are currently running).
434 */
435 struct sched_entity *curr, *next, *last, *skip;
436
437#ifdef CONFIG_SCHED_DEBUG
438 unsigned int nr_spread_over;
439#endif
440
Paul Turner2dac7542012-10-04 13:18:30 +0200441#ifdef CONFIG_SMP
442 /*
Yuyang Du9d89c252015-07-15 08:04:37 +0800443 * CFS load tracking
Paul Turner2dac7542012-10-04 13:18:30 +0200444 */
Yuyang Du9d89c252015-07-15 08:04:37 +0800445 struct sched_avg avg;
Yuyang Du13962232015-07-15 08:04:41 +0800446 u64 runnable_load_sum;
447 unsigned long runnable_load_avg;
Peter Zijlstra2a2f5d4e2017-05-08 16:51:41 +0200448#ifndef CONFIG_64BIT
449 u64 load_last_update_time_copy;
450#endif
Peter Zijlstra2a2f5d4e2017-05-08 16:51:41 +0200451 struct {
452 raw_spinlock_t lock ____cacheline_aligned;
453 int nr;
454 unsigned long load_avg;
455 unsigned long util_avg;
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +0200456 unsigned long runnable_sum;
Peter Zijlstra2a2f5d4e2017-05-08 16:51:41 +0200457 } removed;
Alex Shi141965c2013-06-26 13:05:39 +0800458
Paul Turnerc566e8e2012-10-04 13:18:30 +0200459#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +0200460 unsigned long tg_load_avg_contrib;
461 long propagate;
462 long prop_runnable_sum;
463
Paul Turner82958362012-10-04 13:18:31 +0200464 /*
465 * h_load = weight * f(tg)
466 *
467 * Where f(tg) is the recursive weight fraction assigned to
468 * this group.
469 */
470 unsigned long h_load;
Vladimir Davydov68520792013-07-15 17:49:19 +0400471 u64 last_h_load_update;
472 struct sched_entity *h_load_next;
473#endif /* CONFIG_FAIR_GROUP_SCHED */
Paul Turner82958362012-10-04 13:18:31 +0200474#endif /* CONFIG_SMP */
475
Peter Zijlstra029632f2011-10-25 10:00:11 +0200476#ifdef CONFIG_FAIR_GROUP_SCHED
477 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
478
479 /*
480 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
481 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
482 * (like users, containers etc.)
483 *
484 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
485 * list is used during load balance.
486 */
487 int on_list;
488 struct list_head leaf_cfs_rq_list;
489 struct task_group *tg; /* group that "owns" this runqueue */
490
Peter Zijlstra029632f2011-10-25 10:00:11 +0200491#ifdef CONFIG_CFS_BANDWIDTH
492 int runtime_enabled;
493 u64 runtime_expires;
494 s64 runtime_remaining;
495
Paul Turnerf1b17282012-10-04 13:18:31 +0200496 u64 throttled_clock, throttled_clock_task;
497 u64 throttled_clock_task_time;
Peter Zijlstra55e16d32016-06-22 15:14:26 +0200498 int throttled, throttle_count;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200499 struct list_head throttled_list;
500#endif /* CONFIG_CFS_BANDWIDTH */
501#endif /* CONFIG_FAIR_GROUP_SCHED */
502};
503
504static inline int rt_bandwidth_enabled(void)
505{
506 return sysctl_sched_rt_runtime >= 0;
507}
508
Steven Rostedtb6366f02015-03-18 14:49:46 -0400509/* RT IPI pull logic requires IRQ_WORK */
510#ifdef CONFIG_IRQ_WORK
511# define HAVE_RT_PUSH_IPI
512#endif
513
Peter Zijlstra029632f2011-10-25 10:00:11 +0200514/* Real-Time classes' related field in a runqueue: */
515struct rt_rq {
516 struct rt_prio_array active;
Peter Zijlstrac82513e2012-04-26 13:12:27 +0200517 unsigned int rt_nr_running;
Frederic Weisbecker01d36d02015-11-04 18:17:10 +0100518 unsigned int rr_nr_running;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200519#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
520 struct {
521 int curr; /* highest queued rt task prio */
522#ifdef CONFIG_SMP
523 int next; /* next highest */
524#endif
525 } highest_prio;
526#endif
527#ifdef CONFIG_SMP
528 unsigned long rt_nr_migratory;
529 unsigned long rt_nr_total;
530 int overloaded;
531 struct plist_head pushable_tasks;
Steven Rostedtb6366f02015-03-18 14:49:46 -0400532#ifdef HAVE_RT_PUSH_IPI
533 int push_flags;
534 int push_cpu;
535 struct irq_work push_work;
536 raw_spinlock_t push_lock;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200537#endif
Steven Rostedtb6366f02015-03-18 14:49:46 -0400538#endif /* CONFIG_SMP */
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400539 int rt_queued;
540
Peter Zijlstra029632f2011-10-25 10:00:11 +0200541 int rt_throttled;
542 u64 rt_time;
543 u64 rt_runtime;
544 /* Nests inside the rq lock: */
545 raw_spinlock_t rt_runtime_lock;
546
547#ifdef CONFIG_RT_GROUP_SCHED
548 unsigned long rt_nr_boosted;
549
550 struct rq *rq;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200551 struct task_group *tg;
552#endif
553};
554
Dario Faggioliaab03e02013-11-28 11:14:43 +0100555/* Deadline class' related fields in a runqueue */
556struct dl_rq {
557 /* runqueue is an rbtree, ordered by deadline */
Davidlohr Bueso21615732017-09-08 16:14:58 -0700558 struct rb_root_cached root;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100559
560 unsigned long dl_nr_running;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100561
562#ifdef CONFIG_SMP
563 /*
564 * Deadline values of the currently executing and the
565 * earliest ready task on this rq. Caching these facilitates
566 * the decision wether or not a ready but not running task
567 * should migrate somewhere else.
568 */
569 struct {
570 u64 curr;
571 u64 next;
572 } earliest_dl;
573
574 unsigned long dl_nr_migratory;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100575 int overloaded;
576
577 /*
578 * Tasks on this rq that can be pushed away. They are kept in
579 * an rb-tree, ordered by tasks' deadlines, with caching
580 * of the leftmost (earliest deadline) element.
581 */
Davidlohr Bueso21615732017-09-08 16:14:58 -0700582 struct rb_root_cached pushable_dl_tasks_root;
Dario Faggioli332ac172013-11-07 14:43:45 +0100583#else
584 struct dl_bw dl_bw;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100585#endif
Luca Abenie36d8672017-05-18 22:13:28 +0200586 /*
587 * "Active utilization" for this runqueue: increased when a
588 * task wakes up (becomes TASK_RUNNING) and decreased when a
589 * task blocks
590 */
591 u64 running_bw;
Luca Abeni4da3abc2017-05-18 22:13:32 +0200592
593 /*
Luca Abeni8fd27232017-05-18 22:13:34 +0200594 * Utilization of the tasks "assigned" to this runqueue (including
595 * the tasks that are in runqueue and the tasks that executed on this
596 * CPU and blocked). Increased when a task moves to this runqueue, and
597 * decreased when the task moves away (migrates, changes scheduling
598 * policy, or terminates).
599 * This is needed to compute the "inactive utilization" for the
600 * runqueue (inactive utilization = this_bw - running_bw).
601 */
602 u64 this_bw;
Luca Abenidaec5792017-05-18 22:13:36 +0200603 u64 extra_bw;
Luca Abeni8fd27232017-05-18 22:13:34 +0200604
605 /*
Luca Abeni4da3abc2017-05-18 22:13:32 +0200606 * Inverse of the fraction of CPU utilization that can be reclaimed
607 * by the GRUB algorithm.
608 */
609 u64 bw_ratio;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100610};
611
Peter Zijlstra029632f2011-10-25 10:00:11 +0200612#ifdef CONFIG_SMP
613
Tim Chenafe06ef2016-11-22 12:23:53 -0800614static inline bool sched_asym_prefer(int a, int b)
615{
616 return arch_asym_cpu_priority(a) > arch_asym_cpu_priority(b);
617}
618
Peter Zijlstra029632f2011-10-25 10:00:11 +0200619/*
620 * We add the notion of a root-domain which will be used to define per-domain
621 * variables. Each exclusive cpuset essentially defines an island domain by
622 * fully partitioning the member cpus from any other cpuset. Whenever a new
623 * exclusive cpuset is created, we also create and attach a new root-domain
624 * object.
625 *
626 */
627struct root_domain {
628 atomic_t refcount;
629 atomic_t rto_count;
630 struct rcu_head rcu;
631 cpumask_var_t span;
632 cpumask_var_t online;
633
Tim Chen4486edd2014-06-23 12:16:49 -0700634 /* Indicate more than one runnable task for any CPU */
635 bool overload;
636
Peter Zijlstra029632f2011-10-25 10:00:11 +0200637 /*
Juri Lelli1baca4c2013-11-07 14:43:38 +0100638 * The bit corresponding to a CPU gets set here if such CPU has more
639 * than one runnable -deadline task (as it is below for RT tasks).
640 */
641 cpumask_var_t dlo_mask;
642 atomic_t dlo_count;
Dario Faggioli332ac172013-11-07 14:43:45 +0100643 struct dl_bw dl_bw;
Juri Lelli6bfd6d72013-11-07 14:43:47 +0100644 struct cpudl cpudl;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100645
646 /*
Peter Zijlstra029632f2011-10-25 10:00:11 +0200647 * The "RT overload" flag: it gets set if a CPU has more than
648 * one runnable RT task.
649 */
650 cpumask_var_t rto_mask;
651 struct cpupri cpupri;
Dietmar Eggemanncd92bfd2016-08-01 19:53:35 +0100652
653 unsigned long max_cpu_capacity;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200654};
655
656extern struct root_domain def_root_domain;
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100657extern struct mutex sched_domains_mutex;
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100658
659extern void init_defrootdomain(void);
Peter Zijlstra8d5dc512017-04-25 15:29:40 +0200660extern int sched_init_domains(const struct cpumask *cpu_map);
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100661extern void rq_attach_root(struct rq *rq, struct root_domain *rd);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200662
663#endif /* CONFIG_SMP */
664
665/*
666 * This is the main, per-CPU runqueue data structure.
667 *
668 * Locking rule: those places that want to lock multiple runqueues
669 * (such as the load balancing or the thread migration code), lock
670 * acquire operations must be ordered by ascending &runqueue.
671 */
672struct rq {
673 /* runqueue lock: */
674 raw_spinlock_t lock;
675
676 /*
677 * nr_running and cpu_load should be in the same cacheline because
678 * remote CPUs use both these fields when doing load calculation.
679 */
Peter Zijlstrac82513e2012-04-26 13:12:27 +0200680 unsigned int nr_running;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +0100681#ifdef CONFIG_NUMA_BALANCING
682 unsigned int nr_numa_running;
683 unsigned int nr_preferred_running;
684#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200685 #define CPU_LOAD_IDX_MAX 5
686 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200687#ifdef CONFIG_NO_HZ_COMMON
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +0200688#ifdef CONFIG_SMP
689 unsigned long last_load_update_tick;
690#endif /* CONFIG_SMP */
Suresh Siddha1c792db2011-12-01 17:07:32 -0800691 unsigned long nohz_flags;
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +0200692#endif /* CONFIG_NO_HZ_COMMON */
Frederic Weisbecker265f22a2013-05-03 03:39:05 +0200693#ifdef CONFIG_NO_HZ_FULL
694 unsigned long last_sched_tick;
695#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200696 /* capture load from *all* tasks on this cpu: */
697 struct load_weight load;
698 unsigned long nr_load_updates;
699 u64 nr_switches;
700
701 struct cfs_rq cfs;
702 struct rt_rq rt;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100703 struct dl_rq dl;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200704
705#ifdef CONFIG_FAIR_GROUP_SCHED
706 /* list of leaf cfs_rq on this cpu: */
707 struct list_head leaf_cfs_rq_list;
Vincent Guittot9c2791f2016-11-08 10:53:43 +0100708 struct list_head *tmp_alone_branch;
Peter Zijlstraa35b6462012-08-08 21:46:40 +0200709#endif /* CONFIG_FAIR_GROUP_SCHED */
710
Peter Zijlstra029632f2011-10-25 10:00:11 +0200711 /*
712 * This is part of a global counter where only the total sum
713 * over all CPUs matters. A task can increase this counter on
714 * one CPU and if it got migrated afterwards it may decrease
715 * it on another CPU. Always updated under the runqueue lock:
716 */
717 unsigned long nr_uninterruptible;
718
719 struct task_struct *curr, *idle, *stop;
720 unsigned long next_balance;
721 struct mm_struct *prev_mm;
722
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100723 unsigned int clock_update_flags;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200724 u64 clock;
725 u64 clock_task;
726
727 atomic_t nr_iowait;
728
729#ifdef CONFIG_SMP
730 struct root_domain *rd;
731 struct sched_domain *sd;
732
Nicolas Pitreced549f2014-05-26 18:19:38 -0400733 unsigned long cpu_capacity;
Vincent Guittotca6d75e2015-02-27 16:54:09 +0100734 unsigned long cpu_capacity_orig;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200735
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200736 struct callback_head *balance_callback;
737
Peter Zijlstra029632f2011-10-25 10:00:11 +0200738 unsigned char idle_balance;
739 /* For active balancing */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200740 int active_balance;
741 int push_cpu;
742 struct cpu_stop_work active_balance_work;
743 /* cpu of this runqueue: */
744 int cpu;
745 int online;
746
Peter Zijlstra367456c2012-02-20 21:49:09 +0100747 struct list_head cfs_tasks;
748
Peter Zijlstra029632f2011-10-25 10:00:11 +0200749 u64 rt_avg;
750 u64 age_stamp;
751 u64 idle_stamp;
752 u64 avg_idle;
Jason Low9bd721c2013-09-13 11:26:52 -0700753
754 /* This is used to determine avg_idle's max value */
755 u64 max_idle_balance_cost;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200756#endif
757
758#ifdef CONFIG_IRQ_TIME_ACCOUNTING
759 u64 prev_irq_time;
760#endif
761#ifdef CONFIG_PARAVIRT
762 u64 prev_steal_time;
763#endif
764#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
765 u64 prev_steal_time_rq;
766#endif
767
768 /* calc_load related fields */
769 unsigned long calc_load_update;
770 long calc_load_active;
771
772#ifdef CONFIG_SCHED_HRTICK
773#ifdef CONFIG_SMP
774 int hrtick_csd_pending;
Ying Huang966a9672017-08-08 12:30:00 +0800775 call_single_data_t hrtick_csd;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200776#endif
777 struct hrtimer hrtick_timer;
778#endif
779
780#ifdef CONFIG_SCHEDSTATS
781 /* latency stats */
782 struct sched_info rq_sched_info;
783 unsigned long long rq_cpu_time;
784 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
785
786 /* sys_sched_yield() stats */
787 unsigned int yld_count;
788
789 /* schedule() stats */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200790 unsigned int sched_count;
791 unsigned int sched_goidle;
792
793 /* try_to_wake_up() stats */
794 unsigned int ttwu_count;
795 unsigned int ttwu_local;
796#endif
797
798#ifdef CONFIG_SMP
799 struct llist_head wake_list;
800#endif
Daniel Lezcano442bf3a2014-09-04 11:32:09 -0400801
802#ifdef CONFIG_CPU_IDLE
803 /* Must be inspected within a rcu lock section */
804 struct cpuidle_state *idle_state;
805#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200806};
807
808static inline int cpu_of(struct rq *rq)
809{
810#ifdef CONFIG_SMP
811 return rq->cpu;
812#else
813 return 0;
814#endif
815}
816
Peter Zijlstra1b568f02016-05-09 10:38:41 +0200817
818#ifdef CONFIG_SCHED_SMT
819
820extern struct static_key_false sched_smt_present;
821
822extern void __update_idle_core(struct rq *rq);
823
824static inline void update_idle_core(struct rq *rq)
825{
826 if (static_branch_unlikely(&sched_smt_present))
827 __update_idle_core(rq);
828}
829
830#else
831static inline void update_idle_core(struct rq *rq) { }
832#endif
833
Pranith Kumar8b06c552014-08-13 13:28:12 -0400834DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200835
Peter Zijlstra518cd622011-12-07 15:07:31 +0100836#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500837#define this_rq() this_cpu_ptr(&runqueues)
Peter Zijlstra518cd622011-12-07 15:07:31 +0100838#define task_rq(p) cpu_rq(task_cpu(p))
839#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500840#define raw_rq() raw_cpu_ptr(&runqueues)
Peter Zijlstra518cd622011-12-07 15:07:31 +0100841
Peter Zijlstracebde6d2015-01-05 11:18:10 +0100842static inline u64 __rq_clock_broken(struct rq *rq)
843{
Jason Low316c1608d2015-04-28 13:00:20 -0700844 return READ_ONCE(rq->clock);
Peter Zijlstracebde6d2015-01-05 11:18:10 +0100845}
846
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100847/*
848 * rq::clock_update_flags bits
849 *
850 * %RQCF_REQ_SKIP - will request skipping of clock update on the next
851 * call to __schedule(). This is an optimisation to avoid
852 * neighbouring rq clock updates.
853 *
854 * %RQCF_ACT_SKIP - is set from inside of __schedule() when skipping is
855 * in effect and calls to update_rq_clock() are being ignored.
856 *
857 * %RQCF_UPDATED - is a debug flag that indicates whether a call has been
858 * made to update_rq_clock() since the last time rq::lock was pinned.
859 *
860 * If inside of __schedule(), clock_update_flags will have been
861 * shifted left (a left shift is a cheap operation for the fast path
862 * to promote %RQCF_REQ_SKIP to %RQCF_ACT_SKIP), so you must use,
863 *
864 * if (rq-clock_update_flags >= RQCF_UPDATED)
865 *
866 * to check if %RQCF_UPADTED is set. It'll never be shifted more than
867 * one position though, because the next rq_unpin_lock() will shift it
868 * back.
869 */
870#define RQCF_REQ_SKIP 0x01
871#define RQCF_ACT_SKIP 0x02
872#define RQCF_UPDATED 0x04
873
874static inline void assert_clock_updated(struct rq *rq)
875{
876 /*
877 * The only reason for not seeing a clock update since the
878 * last rq_pin_lock() is if we're currently skipping updates.
879 */
880 SCHED_WARN_ON(rq->clock_update_flags < RQCF_ACT_SKIP);
881}
882
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200883static inline u64 rq_clock(struct rq *rq)
884{
Peter Zijlstracebde6d2015-01-05 11:18:10 +0100885 lockdep_assert_held(&rq->lock);
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100886 assert_clock_updated(rq);
887
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200888 return rq->clock;
889}
890
891static inline u64 rq_clock_task(struct rq *rq)
892{
Peter Zijlstracebde6d2015-01-05 11:18:10 +0100893 lockdep_assert_held(&rq->lock);
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100894 assert_clock_updated(rq);
895
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200896 return rq->clock_task;
897}
898
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100899static inline void rq_clock_skip_update(struct rq *rq, bool skip)
900{
901 lockdep_assert_held(&rq->lock);
902 if (skip)
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100903 rq->clock_update_flags |= RQCF_REQ_SKIP;
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100904 else
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100905 rq->clock_update_flags &= ~RQCF_REQ_SKIP;
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100906}
907
Matt Flemingd8ac8972016-09-21 14:38:10 +0100908struct rq_flags {
909 unsigned long flags;
910 struct pin_cookie cookie;
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100911#ifdef CONFIG_SCHED_DEBUG
912 /*
913 * A copy of (rq::clock_update_flags & RQCF_UPDATED) for the
914 * current pin context is stashed here in case it needs to be
915 * restored in rq_repin_lock().
916 */
917 unsigned int clock_update_flags;
918#endif
Matt Flemingd8ac8972016-09-21 14:38:10 +0100919};
920
921static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf)
922{
923 rf->cookie = lockdep_pin_lock(&rq->lock);
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100924
925#ifdef CONFIG_SCHED_DEBUG
926 rq->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
927 rf->clock_update_flags = 0;
928#endif
Matt Flemingd8ac8972016-09-21 14:38:10 +0100929}
930
931static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf)
932{
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100933#ifdef CONFIG_SCHED_DEBUG
934 if (rq->clock_update_flags > RQCF_ACT_SKIP)
935 rf->clock_update_flags = RQCF_UPDATED;
936#endif
937
Matt Flemingd8ac8972016-09-21 14:38:10 +0100938 lockdep_unpin_lock(&rq->lock, rf->cookie);
939}
940
941static inline void rq_repin_lock(struct rq *rq, struct rq_flags *rf)
942{
943 lockdep_repin_lock(&rq->lock, rf->cookie);
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100944
945#ifdef CONFIG_SCHED_DEBUG
946 /*
947 * Restore the value we stashed in @rf for this pin context.
948 */
949 rq->clock_update_flags |= rf->clock_update_flags;
950#endif
Matt Flemingd8ac8972016-09-21 14:38:10 +0100951}
952
Rik van Riel9942f792014-10-17 03:29:49 -0400953#ifdef CONFIG_NUMA
Rik van Riele3fe70b2014-10-17 03:29:50 -0400954enum numa_topology_type {
955 NUMA_DIRECT,
956 NUMA_GLUELESS_MESH,
957 NUMA_BACKPLANE,
958};
959extern enum numa_topology_type sched_numa_topology_type;
Rik van Riel9942f792014-10-17 03:29:49 -0400960extern int sched_max_numa_distance;
961extern bool find_numa_distance(int distance);
962#endif
963
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100964#ifdef CONFIG_NUMA
965extern void sched_init_numa(void);
966extern void sched_domains_numa_masks_set(unsigned int cpu);
967extern void sched_domains_numa_masks_clear(unsigned int cpu);
968#else
969static inline void sched_init_numa(void) { }
970static inline void sched_domains_numa_masks_set(unsigned int cpu) { }
971static inline void sched_domains_numa_masks_clear(unsigned int cpu) { }
972#endif
973
Mel Gormanf809ca92013-10-07 11:28:57 +0100974#ifdef CONFIG_NUMA_BALANCING
Iulia Manda44dba3d2014-10-31 02:13:31 +0200975/* The regions in numa_faults array from task_struct */
976enum numa_faults_stats {
977 NUMA_MEM = 0,
978 NUMA_CPU,
979 NUMA_MEMBUF,
980 NUMA_CPUBUF
981};
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +0100982extern void sched_setnuma(struct task_struct *p, int node);
Mel Gormane6628d52013-10-07 11:29:02 +0100983extern int migrate_task_to(struct task_struct *p, int cpu);
Peter Zijlstraac66f542013-10-07 11:29:16 +0100984extern int migrate_swap(struct task_struct *, struct task_struct *);
Mel Gormanf809ca92013-10-07 11:28:57 +0100985#endif /* CONFIG_NUMA_BALANCING */
986
Peter Zijlstra518cd622011-12-07 15:07:31 +0100987#ifdef CONFIG_SMP
988
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200989static inline void
990queue_balance_callback(struct rq *rq,
991 struct callback_head *head,
992 void (*func)(struct rq *rq))
993{
994 lockdep_assert_held(&rq->lock);
995
996 if (unlikely(head->next))
997 return;
998
999 head->func = (void (*)(struct callback_head *))func;
1000 head->next = rq->balance_callback;
1001 rq->balance_callback = head;
1002}
1003
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001004extern void sched_ttwu_pending(void);
1005
Peter Zijlstra029632f2011-10-25 10:00:11 +02001006#define rcu_dereference_check_sched_domain(p) \
1007 rcu_dereference_check((p), \
1008 lockdep_is_held(&sched_domains_mutex))
1009
1010/*
1011 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
1012 * See detach_destroy_domains: synchronize_sched for details.
1013 *
1014 * The domain tree of any CPU may only be accessed from within
1015 * preempt-disabled sections.
1016 */
1017#define for_each_domain(cpu, __sd) \
Peter Zijlstra518cd622011-12-07 15:07:31 +01001018 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
1019 __sd; __sd = __sd->parent)
Peter Zijlstra029632f2011-10-25 10:00:11 +02001020
Suresh Siddha77e81362011-11-17 11:08:23 -08001021#define for_each_lower_domain(sd) for (; sd; sd = sd->child)
1022
Peter Zijlstra518cd622011-12-07 15:07:31 +01001023/**
1024 * highest_flag_domain - Return highest sched_domain containing flag.
1025 * @cpu: The cpu whose highest level of sched domain is to
1026 * be returned.
1027 * @flag: The flag to check for the highest sched_domain
1028 * for the given cpu.
1029 *
1030 * Returns the highest sched_domain of a cpu which contains the given flag.
1031 */
1032static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
1033{
1034 struct sched_domain *sd, *hsd = NULL;
1035
1036 for_each_domain(cpu, sd) {
1037 if (!(sd->flags & flag))
1038 break;
1039 hsd = sd;
1040 }
1041
1042 return hsd;
1043}
1044
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001045static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
1046{
1047 struct sched_domain *sd;
1048
1049 for_each_domain(cpu, sd) {
1050 if (sd->flags & flag)
1051 break;
1052 }
1053
1054 return sd;
1055}
1056
Peter Zijlstra518cd622011-12-07 15:07:31 +01001057DECLARE_PER_CPU(struct sched_domain *, sd_llc);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08001058DECLARE_PER_CPU(int, sd_llc_size);
Peter Zijlstra518cd622011-12-07 15:07:31 +01001059DECLARE_PER_CPU(int, sd_llc_id);
Peter Zijlstra0e369d72016-05-09 10:38:01 +02001060DECLARE_PER_CPU(struct sched_domain_shared *, sd_llc_shared);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001061DECLARE_PER_CPU(struct sched_domain *, sd_numa);
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05301062DECLARE_PER_CPU(struct sched_domain *, sd_asym);
Peter Zijlstra518cd622011-12-07 15:07:31 +01001063
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001064struct sched_group_capacity {
Li Zefan5e6521e2013-03-05 16:06:23 +08001065 atomic_t ref;
1066 /*
Yuyang Du172895e2016-04-05 12:12:27 +08001067 * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001068 * for a single CPU.
Li Zefan5e6521e2013-03-05 16:06:23 +08001069 */
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01001070 unsigned long capacity;
1071 unsigned long min_capacity; /* Min per-CPU capacity in group */
Li Zefan5e6521e2013-03-05 16:06:23 +08001072 unsigned long next_update;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001073 int imbalance; /* XXX unrelated to capacity but shared group state */
Li Zefan5e6521e2013-03-05 16:06:23 +08001074
Peter Zijlstra005f8742017-04-26 17:35:35 +02001075#ifdef CONFIG_SCHED_DEBUG
1076 int id;
1077#endif
1078
Peter Zijlstrae5c14b12017-05-01 10:47:02 +02001079 unsigned long cpumask[0]; /* balance mask */
Li Zefan5e6521e2013-03-05 16:06:23 +08001080};
1081
1082struct sched_group {
1083 struct sched_group *next; /* Must be a circular list */
1084 atomic_t ref;
1085
1086 unsigned int group_weight;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001087 struct sched_group_capacity *sgc;
Tim Chenafe06ef2016-11-22 12:23:53 -08001088 int asym_prefer_cpu; /* cpu of highest priority in group */
Li Zefan5e6521e2013-03-05 16:06:23 +08001089
1090 /*
1091 * The CPUs this group covers.
1092 *
1093 * NOTE: this field is variable length. (Allocated dynamically
1094 * by attaching extra space to the end of the structure,
1095 * depending on how many CPUs the kernel has booted up with)
1096 */
1097 unsigned long cpumask[0];
1098};
1099
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02001100static inline struct cpumask *sched_group_span(struct sched_group *sg)
Li Zefan5e6521e2013-03-05 16:06:23 +08001101{
1102 return to_cpumask(sg->cpumask);
1103}
1104
1105/*
Peter Zijlstrae5c14b12017-05-01 10:47:02 +02001106 * See build_balance_mask().
Li Zefan5e6521e2013-03-05 16:06:23 +08001107 */
Peter Zijlstrae5c14b12017-05-01 10:47:02 +02001108static inline struct cpumask *group_balance_mask(struct sched_group *sg)
Li Zefan5e6521e2013-03-05 16:06:23 +08001109{
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001110 return to_cpumask(sg->sgc->cpumask);
Li Zefan5e6521e2013-03-05 16:06:23 +08001111}
1112
1113/**
1114 * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
1115 * @group: The group whose first cpu is to be returned.
1116 */
1117static inline unsigned int group_first_cpu(struct sched_group *group)
1118{
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02001119 return cpumask_first(sched_group_span(group));
Li Zefan5e6521e2013-03-05 16:06:23 +08001120}
1121
Peter Zijlstrac1174872012-05-31 14:47:33 +02001122extern int group_balance_cpu(struct sched_group *sg);
1123
Steven Rostedt (Red Hat)3866e842016-02-22 16:26:51 -05001124#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
1125void register_sched_domain_sysctl(void);
Peter Zijlstrabbdacdf2017-08-10 17:10:26 +02001126void dirty_sched_domain_sysctl(int cpu);
Steven Rostedt (Red Hat)3866e842016-02-22 16:26:51 -05001127void unregister_sched_domain_sysctl(void);
1128#else
1129static inline void register_sched_domain_sysctl(void)
1130{
1131}
Peter Zijlstrabbdacdf2017-08-10 17:10:26 +02001132static inline void dirty_sched_domain_sysctl(int cpu)
1133{
1134}
Steven Rostedt (Red Hat)3866e842016-02-22 16:26:51 -05001135static inline void unregister_sched_domain_sysctl(void)
1136{
1137}
1138#endif
1139
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001140#else
1141
1142static inline void sched_ttwu_pending(void) { }
1143
Peter Zijlstra518cd622011-12-07 15:07:31 +01001144#endif /* CONFIG_SMP */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001145
Peter Zijlstra391e43d2011-11-15 17:14:39 +01001146#include "stats.h"
Ingo Molnar10514082017-02-01 18:42:41 +01001147#include "autogroup.h"
Peter Zijlstra029632f2011-10-25 10:00:11 +02001148
1149#ifdef CONFIG_CGROUP_SCHED
1150
1151/*
1152 * Return the group to which this tasks belongs.
1153 *
Tejun Heo8af01f52013-08-08 20:11:22 -04001154 * We cannot use task_css() and friends because the cgroup subsystem
1155 * changes that value before the cgroup_subsys::attach() method is called,
1156 * therefore we cannot pin it and might observe the wrong value.
Peter Zijlstra8323f262012-06-22 13:36:05 +02001157 *
1158 * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
1159 * core changes this before calling sched_move_task().
1160 *
1161 * Instead we use a 'copy' which is updated from sched_move_task() while
1162 * holding both task_struct::pi_lock and rq::lock.
Peter Zijlstra029632f2011-10-25 10:00:11 +02001163 */
1164static inline struct task_group *task_group(struct task_struct *p)
1165{
Peter Zijlstra8323f262012-06-22 13:36:05 +02001166 return p->sched_task_group;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001167}
1168
1169/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
1170static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
1171{
1172#if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
1173 struct task_group *tg = task_group(p);
1174#endif
1175
1176#ifdef CONFIG_FAIR_GROUP_SCHED
Byungchul Parkad936d82015-10-24 01:16:19 +09001177 set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001178 p->se.cfs_rq = tg->cfs_rq[cpu];
1179 p->se.parent = tg->se[cpu];
1180#endif
1181
1182#ifdef CONFIG_RT_GROUP_SCHED
1183 p->rt.rt_rq = tg->rt_rq[cpu];
1184 p->rt.parent = tg->rt_se[cpu];
1185#endif
1186}
1187
1188#else /* CONFIG_CGROUP_SCHED */
1189
1190static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
1191static inline struct task_group *task_group(struct task_struct *p)
1192{
1193 return NULL;
1194}
1195
1196#endif /* CONFIG_CGROUP_SCHED */
1197
1198static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1199{
1200 set_task_rq(p, cpu);
1201#ifdef CONFIG_SMP
1202 /*
1203 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1204 * successfuly executed on another CPU. We must ensure that updates of
1205 * per-task data have been completed by this moment.
1206 */
1207 smp_wmb();
Andy Lutomirskic65eacb2016-09-13 14:29:24 -07001208#ifdef CONFIG_THREAD_INFO_IN_TASK
1209 p->cpu = cpu;
1210#else
Peter Zijlstra029632f2011-10-25 10:00:11 +02001211 task_thread_info(p)->cpu = cpu;
Andy Lutomirskic65eacb2016-09-13 14:29:24 -07001212#endif
Peter Zijlstraac66f542013-10-07 11:29:16 +01001213 p->wake_cpu = cpu;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001214#endif
1215}
1216
1217/*
1218 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
1219 */
1220#ifdef CONFIG_SCHED_DEBUG
Ingo Molnarc5905af2012-02-24 08:31:31 +01001221# include <linux/static_key.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +02001222# define const_debug __read_mostly
1223#else
1224# define const_debug const
1225#endif
1226
1227extern const_debug unsigned int sysctl_sched_features;
1228
1229#define SCHED_FEAT(name, enabled) \
1230 __SCHED_FEAT_##name ,
1231
1232enum {
Peter Zijlstra391e43d2011-11-15 17:14:39 +01001233#include "features.h"
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001234 __SCHED_FEAT_NR,
Peter Zijlstra029632f2011-10-25 10:00:11 +02001235};
1236
1237#undef SCHED_FEAT
1238
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001239#if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL)
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001240#define SCHED_FEAT(name, enabled) \
Ingo Molnarc5905af2012-02-24 08:31:31 +01001241static __always_inline bool static_branch_##name(struct static_key *key) \
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001242{ \
Jason Baron6e76ea82014-07-02 15:52:41 +00001243 return static_key_##enabled(key); \
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001244}
1245
1246#include "features.h"
1247
1248#undef SCHED_FEAT
1249
Ingo Molnarc5905af2012-02-24 08:31:31 +01001250extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001251#define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
1252#else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001253#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001254#endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001255
Srikar Dronamraju2a595722015-08-11 21:54:21 +05301256extern struct static_key_false sched_numa_balancing;
Mel Gormancb251762016-02-05 09:08:36 +00001257extern struct static_key_false sched_schedstats;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001258
Peter Zijlstra029632f2011-10-25 10:00:11 +02001259static inline u64 global_rt_period(void)
1260{
1261 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
1262}
1263
1264static inline u64 global_rt_runtime(void)
1265{
1266 if (sysctl_sched_rt_runtime < 0)
1267 return RUNTIME_INF;
1268
1269 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
1270}
1271
Peter Zijlstra029632f2011-10-25 10:00:11 +02001272static inline int task_current(struct rq *rq, struct task_struct *p)
1273{
1274 return rq->curr == p;
1275}
1276
1277static inline int task_running(struct rq *rq, struct task_struct *p)
1278{
1279#ifdef CONFIG_SMP
1280 return p->on_cpu;
1281#else
1282 return task_current(rq, p);
1283#endif
1284}
1285
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001286static inline int task_on_rq_queued(struct task_struct *p)
1287{
1288 return p->on_rq == TASK_ON_RQ_QUEUED;
1289}
Peter Zijlstra029632f2011-10-25 10:00:11 +02001290
Kirill Tkhaicca26e82014-08-20 13:47:42 +04001291static inline int task_on_rq_migrating(struct task_struct *p)
1292{
1293 return p->on_rq == TASK_ON_RQ_MIGRATING;
1294}
1295
Peter Zijlstra029632f2011-10-25 10:00:11 +02001296#ifndef prepare_arch_switch
1297# define prepare_arch_switch(next) do { } while (0)
1298#endif
Catalin Marinas01f23e12011-11-27 21:43:10 +00001299#ifndef finish_arch_post_lock_switch
1300# define finish_arch_post_lock_switch() do { } while (0)
1301#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02001302
Peter Zijlstra029632f2011-10-25 10:00:11 +02001303static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
1304{
1305#ifdef CONFIG_SMP
1306 /*
1307 * We can optimise this out completely for !SMP, because the
1308 * SMP rebalancing from interrupt is the only thing that cares
1309 * here.
1310 */
1311 next->on_cpu = 1;
1312#endif
1313}
1314
1315static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
1316{
1317#ifdef CONFIG_SMP
1318 /*
1319 * After ->on_cpu is cleared, the task can be moved to a different CPU.
1320 * We must ensure this doesn't happen until the switch is completely
1321 * finished.
Peter Zijlstra95913d92015-09-29 14:45:09 +02001322 *
Peter Zijlstrab75a2252015-10-06 14:36:17 +02001323 * In particular, the load of prev->state in finish_task_switch() must
1324 * happen before this.
1325 *
Peter Zijlstra1f03e8d2016-04-04 10:57:12 +02001326 * Pairs with the smp_cond_load_acquire() in try_to_wake_up().
Peter Zijlstra029632f2011-10-25 10:00:11 +02001327 */
Peter Zijlstra95913d92015-09-29 14:45:09 +02001328 smp_store_release(&prev->on_cpu, 0);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001329#endif
1330#ifdef CONFIG_DEBUG_SPINLOCK
1331 /* this is a valid case when another task releases the spinlock */
1332 rq->lock.owner = current;
1333#endif
1334 /*
1335 * If we are tracking spinlock dependencies then we have to
1336 * fix up the runqueue lock - which gets 'carried over' from
1337 * prev into current:
1338 */
1339 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
1340
1341 raw_spin_unlock_irq(&rq->lock);
1342}
1343
Li Zefanb13095f2013-03-05 16:06:38 +08001344/*
1345 * wake flags
1346 */
1347#define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
1348#define WF_FORK 0x02 /* child wakeup after fork */
1349#define WF_MIGRATED 0x4 /* internal use, task got migrated */
1350
Peter Zijlstra029632f2011-10-25 10:00:11 +02001351/*
1352 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1353 * of tasks with abnormal "nice" values across CPUs the contribution that
1354 * each task makes to its run queue's load is weighted according to its
1355 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
1356 * scaled version of the new time slice allocation that they receive on time
1357 * slice expiry etc.
1358 */
1359
1360#define WEIGHT_IDLEPRIO 3
1361#define WMULT_IDLEPRIO 1431655765
1362
Andi Kleened82b8a2015-11-29 20:59:43 -08001363extern const int sched_prio_to_weight[40];
1364extern const u32 sched_prio_to_wmult[40];
Peter Zijlstra029632f2011-10-25 10:00:11 +02001365
Peter Zijlstraff77e462016-01-18 15:27:07 +01001366/*
1367 * {de,en}queue flags:
1368 *
1369 * DEQUEUE_SLEEP - task is no longer runnable
1370 * ENQUEUE_WAKEUP - task just became runnable
1371 *
1372 * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks
1373 * are in a known state which allows modification. Such pairs
1374 * should preserve as much state as possible.
1375 *
1376 * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location
1377 * in the runqueue.
1378 *
1379 * ENQUEUE_HEAD - place at front of runqueue (tail if not specified)
1380 * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline)
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02001381 * ENQUEUE_MIGRATED - the task was migrated during wakeup
Peter Zijlstraff77e462016-01-18 15:27:07 +01001382 *
1383 */
1384
1385#define DEQUEUE_SLEEP 0x01
1386#define DEQUEUE_SAVE 0x02 /* matches ENQUEUE_RESTORE */
1387#define DEQUEUE_MOVE 0x04 /* matches ENQUEUE_MOVE */
Peter Zijlstra0a67d1e2016-10-04 16:29:45 +02001388#define DEQUEUE_NOCLOCK 0x08 /* matches ENQUEUE_NOCLOCK */
Peter Zijlstraff77e462016-01-18 15:27:07 +01001389
Peter Zijlstra1de64442015-09-30 17:44:13 +02001390#define ENQUEUE_WAKEUP 0x01
Peter Zijlstraff77e462016-01-18 15:27:07 +01001391#define ENQUEUE_RESTORE 0x02
1392#define ENQUEUE_MOVE 0x04
Peter Zijlstra0a67d1e2016-10-04 16:29:45 +02001393#define ENQUEUE_NOCLOCK 0x08
Peter Zijlstraff77e462016-01-18 15:27:07 +01001394
Peter Zijlstra0a67d1e2016-10-04 16:29:45 +02001395#define ENQUEUE_HEAD 0x10
1396#define ENQUEUE_REPLENISH 0x20
Li Zefanc82ba9f2013-03-05 16:06:55 +08001397#ifdef CONFIG_SMP
Peter Zijlstra0a67d1e2016-10-04 16:29:45 +02001398#define ENQUEUE_MIGRATED 0x40
Li Zefanc82ba9f2013-03-05 16:06:55 +08001399#else
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02001400#define ENQUEUE_MIGRATED 0x00
Li Zefanc82ba9f2013-03-05 16:06:55 +08001401#endif
Li Zefanc82ba9f2013-03-05 16:06:55 +08001402
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001403#define RETRY_TASK ((void *)-1UL)
1404
Li Zefanc82ba9f2013-03-05 16:06:55 +08001405struct sched_class {
1406 const struct sched_class *next;
1407
1408 void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
1409 void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
1410 void (*yield_task) (struct rq *rq);
1411 bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt);
1412
1413 void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags);
1414
Peter Zijlstra606dba22012-02-11 06:05:00 +01001415 /*
1416 * It is the responsibility of the pick_next_task() method that will
1417 * return the next task to call put_prev_task() on the @prev task or
1418 * something equivalent.
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001419 *
1420 * May return RETRY_TASK when it finds a higher prio class has runnable
1421 * tasks.
Peter Zijlstra606dba22012-02-11 06:05:00 +01001422 */
1423 struct task_struct * (*pick_next_task) (struct rq *rq,
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001424 struct task_struct *prev,
Matt Flemingd8ac8972016-09-21 14:38:10 +01001425 struct rq_flags *rf);
Li Zefanc82ba9f2013-03-05 16:06:55 +08001426 void (*put_prev_task) (struct rq *rq, struct task_struct *p);
1427
1428#ifdef CONFIG_SMP
Peter Zijlstraac66f542013-10-07 11:29:16 +01001429 int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);
xiaofeng.yan5a4fd032015-09-23 14:55:59 +08001430 void (*migrate_task_rq)(struct task_struct *p);
Li Zefanc82ba9f2013-03-05 16:06:55 +08001431
Li Zefanc82ba9f2013-03-05 16:06:55 +08001432 void (*task_woken) (struct rq *this_rq, struct task_struct *task);
1433
1434 void (*set_cpus_allowed)(struct task_struct *p,
1435 const struct cpumask *newmask);
1436
1437 void (*rq_online)(struct rq *rq);
1438 void (*rq_offline)(struct rq *rq);
1439#endif
1440
1441 void (*set_curr_task) (struct rq *rq);
1442 void (*task_tick) (struct rq *rq, struct task_struct *p, int queued);
1443 void (*task_fork) (struct task_struct *p);
Dario Faggiolie6c390f2013-11-07 14:43:35 +01001444 void (*task_dead) (struct task_struct *p);
Li Zefanc82ba9f2013-03-05 16:06:55 +08001445
Kirill Tkhai67dfa1b2014-10-27 17:40:52 +03001446 /*
1447 * The switched_from() call is allowed to drop rq->lock, therefore we
1448 * cannot assume the switched_from/switched_to pair is serliazed by
1449 * rq->lock. They are however serialized by p->pi_lock.
1450 */
Li Zefanc82ba9f2013-03-05 16:06:55 +08001451 void (*switched_from) (struct rq *this_rq, struct task_struct *task);
1452 void (*switched_to) (struct rq *this_rq, struct task_struct *task);
1453 void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
1454 int oldprio);
1455
1456 unsigned int (*get_rr_interval) (struct rq *rq,
1457 struct task_struct *task);
1458
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01001459 void (*update_curr) (struct rq *rq);
1460
Vincent Guittotea86cb42016-06-17 13:38:55 +02001461#define TASK_SET_GROUP 0
1462#define TASK_MOVE_GROUP 1
1463
Li Zefanc82ba9f2013-03-05 16:06:55 +08001464#ifdef CONFIG_FAIR_GROUP_SCHED
Vincent Guittotea86cb42016-06-17 13:38:55 +02001465 void (*task_change_group) (struct task_struct *p, int type);
Li Zefanc82ba9f2013-03-05 16:06:55 +08001466#endif
1467};
Peter Zijlstra029632f2011-10-25 10:00:11 +02001468
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01001469static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
1470{
1471 prev->sched_class->put_prev_task(rq, prev);
1472}
1473
Peter Zijlstrab2bf6c32016-09-20 22:00:38 +02001474static inline void set_curr_task(struct rq *rq, struct task_struct *curr)
1475{
1476 curr->sched_class->set_curr_task(rq);
1477}
1478
Nicolas Pitref5832c12017-05-29 17:02:57 -04001479#ifdef CONFIG_SMP
Peter Zijlstra029632f2011-10-25 10:00:11 +02001480#define sched_class_highest (&stop_sched_class)
Nicolas Pitref5832c12017-05-29 17:02:57 -04001481#else
1482#define sched_class_highest (&dl_sched_class)
1483#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02001484#define for_each_class(class) \
1485 for (class = sched_class_highest; class; class = class->next)
1486
1487extern const struct sched_class stop_sched_class;
Dario Faggioliaab03e02013-11-28 11:14:43 +01001488extern const struct sched_class dl_sched_class;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001489extern const struct sched_class rt_sched_class;
1490extern const struct sched_class fair_sched_class;
1491extern const struct sched_class idle_sched_class;
1492
1493
1494#ifdef CONFIG_SMP
1495
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001496extern void update_group_capacity(struct sched_domain *sd, int cpu);
Li Zefanb7192032013-03-07 10:00:26 +08001497
Daniel Lezcano7caff662014-01-06 12:34:38 +01001498extern void trigger_load_balance(struct rq *rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001499
Peter Zijlstrac5b28032015-05-15 17:43:35 +02001500extern void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask);
1501
Peter Zijlstra029632f2011-10-25 10:00:11 +02001502#endif
1503
Daniel Lezcano442bf3a2014-09-04 11:32:09 -04001504#ifdef CONFIG_CPU_IDLE
1505static inline void idle_set_state(struct rq *rq,
1506 struct cpuidle_state *idle_state)
1507{
1508 rq->idle_state = idle_state;
1509}
1510
1511static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1512{
Peter Zijlstra9148a3a2016-09-20 22:34:51 +02001513 SCHED_WARN_ON(!rcu_read_lock_held());
Daniel Lezcano442bf3a2014-09-04 11:32:09 -04001514 return rq->idle_state;
1515}
1516#else
1517static inline void idle_set_state(struct rq *rq,
1518 struct cpuidle_state *idle_state)
1519{
1520}
1521
1522static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1523{
1524 return NULL;
1525}
1526#endif
1527
Steven Rostedt (VMware)8663eff2017-04-14 08:48:09 -04001528extern void schedule_idle(void);
1529
Peter Zijlstra029632f2011-10-25 10:00:11 +02001530extern void sysrq_sched_debug_show(void);
1531extern void sched_init_granularity(void);
1532extern void update_max_interval(void);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001533
1534extern void init_sched_dl_class(void);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001535extern void init_sched_rt_class(void);
1536extern void init_sched_fair_class(void);
1537
Vincent Guittot90593932017-05-17 11:50:45 +02001538extern void reweight_task(struct task_struct *p, int prio);
1539
Kirill Tkhai88751252014-06-29 00:03:57 +04001540extern void resched_curr(struct rq *rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001541extern void resched_cpu(int cpu);
1542
1543extern struct rt_bandwidth def_rt_bandwidth;
1544extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
1545
Dario Faggioli332ac172013-11-07 14:43:45 +01001546extern struct dl_bandwidth def_dl_bandwidth;
1547extern void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001548extern void init_dl_task_timer(struct sched_dl_entity *dl_se);
Luca Abeni209a0cb2017-05-18 22:13:29 +02001549extern void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se);
Luca Abeni4da3abc2017-05-18 22:13:32 +02001550extern void init_dl_rq_bw_ratio(struct dl_rq *dl_rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001551
Luca Abenic52f14d2017-05-18 22:13:31 +02001552#define BW_SHIFT 20
1553#define BW_UNIT (1 << BW_SHIFT)
Luca Abeni4da3abc2017-05-18 22:13:32 +02001554#define RATIO_SHIFT 8
Dario Faggioli332ac172013-11-07 14:43:45 +01001555unsigned long to_ratio(u64 period, u64 runtime);
1556
Yuyang Du540247f2015-07-15 08:04:39 +08001557extern void init_entity_runnable_average(struct sched_entity *se);
Yuyang Du2b8c41d2016-03-30 04:30:56 +08001558extern void post_init_entity_util_avg(struct sched_entity *se);
Alex Shia75cdaa2013-06-20 10:18:47 +08001559
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +02001560#ifdef CONFIG_NO_HZ_FULL
1561extern bool sched_can_stop_tick(struct rq *rq);
1562
1563/*
1564 * Tick may be needed by tasks in the runqueue depending on their policy and
1565 * requirements. If tick is needed, lets send the target an IPI to kick it out of
1566 * nohz mode if necessary.
1567 */
1568static inline void sched_update_tick_dependency(struct rq *rq)
1569{
1570 int cpu;
1571
1572 if (!tick_nohz_full_enabled())
1573 return;
1574
1575 cpu = cpu_of(rq);
1576
1577 if (!tick_nohz_full_cpu(cpu))
1578 return;
1579
1580 if (sched_can_stop_tick(rq))
1581 tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED);
1582 else
1583 tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED);
1584}
1585#else
1586static inline void sched_update_tick_dependency(struct rq *rq) { }
1587#endif
1588
Kirill Tkhai72465442014-05-09 03:00:14 +04001589static inline void add_nr_running(struct rq *rq, unsigned count)
Peter Zijlstra029632f2011-10-25 10:00:11 +02001590{
Kirill Tkhai72465442014-05-09 03:00:14 +04001591 unsigned prev_nr = rq->nr_running;
1592
1593 rq->nr_running = prev_nr + count;
Frederic Weisbecker9f3660c2013-04-20 14:35:09 +02001594
Kirill Tkhai72465442014-05-09 03:00:14 +04001595 if (prev_nr < 2 && rq->nr_running >= 2) {
Tim Chen4486edd2014-06-23 12:16:49 -07001596#ifdef CONFIG_SMP
1597 if (!rq->rd->overload)
1598 rq->rd->overload = true;
1599#endif
Tim Chen4486edd2014-06-23 12:16:49 -07001600 }
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +02001601
1602 sched_update_tick_dependency(rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001603}
1604
Kirill Tkhai72465442014-05-09 03:00:14 +04001605static inline void sub_nr_running(struct rq *rq, unsigned count)
Peter Zijlstra029632f2011-10-25 10:00:11 +02001606{
Kirill Tkhai72465442014-05-09 03:00:14 +04001607 rq->nr_running -= count;
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +02001608 /* Check if we still need preemption */
1609 sched_update_tick_dependency(rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001610}
1611
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02001612static inline void rq_last_tick_reset(struct rq *rq)
1613{
1614#ifdef CONFIG_NO_HZ_FULL
1615 rq->last_sched_tick = jiffies;
1616#endif
1617}
1618
Peter Zijlstra029632f2011-10-25 10:00:11 +02001619extern void update_rq_clock(struct rq *rq);
1620
1621extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
1622extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
1623
1624extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
1625
1626extern const_debug unsigned int sysctl_sched_time_avg;
1627extern const_debug unsigned int sysctl_sched_nr_migrate;
1628extern const_debug unsigned int sysctl_sched_migration_cost;
1629
1630static inline u64 sched_avg_period(void)
1631{
1632 return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
1633}
1634
Peter Zijlstra029632f2011-10-25 10:00:11 +02001635#ifdef CONFIG_SCHED_HRTICK
1636
1637/*
1638 * Use hrtick when:
1639 * - enabled by features
1640 * - hrtimer is actually high res
1641 */
1642static inline int hrtick_enabled(struct rq *rq)
1643{
1644 if (!sched_feat(HRTICK))
1645 return 0;
1646 if (!cpu_active(cpu_of(rq)))
1647 return 0;
1648 return hrtimer_is_hres_active(&rq->hrtick_timer);
1649}
1650
1651void hrtick_start(struct rq *rq, u64 delay);
1652
Mike Galbraithb39e66e2011-11-22 15:20:07 +01001653#else
1654
1655static inline int hrtick_enabled(struct rq *rq)
1656{
1657 return 0;
1658}
1659
Peter Zijlstra029632f2011-10-25 10:00:11 +02001660#endif /* CONFIG_SCHED_HRTICK */
1661
1662#ifdef CONFIG_SMP
1663extern void sched_avg_update(struct rq *rq);
Peter Zijlstradfbca412015-03-23 14:19:05 +01001664
1665#ifndef arch_scale_freq_capacity
1666static __always_inline
1667unsigned long arch_scale_freq_capacity(struct sched_domain *sd, int cpu)
1668{
1669 return SCHED_CAPACITY_SCALE;
1670}
1671#endif
Vincent Guittotb5b48602015-02-27 16:54:08 +01001672
Morten Rasmussen8cd56012015-08-14 17:23:10 +01001673#ifndef arch_scale_cpu_capacity
1674static __always_inline
1675unsigned long arch_scale_cpu_capacity(struct sched_domain *sd, int cpu)
1676{
Dietmar Eggemanne3279a22015-08-15 00:04:41 +01001677 if (sd && (sd->flags & SD_SHARE_CPUCAPACITY) && (sd->span_weight > 1))
Morten Rasmussen8cd56012015-08-14 17:23:10 +01001678 return sd->smt_gain / sd->span_weight;
1679
1680 return SCHED_CAPACITY_SCALE;
1681}
1682#endif
1683
Peter Zijlstra029632f2011-10-25 10:00:11 +02001684static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1685{
Vincent Guittotb5b48602015-02-27 16:54:08 +01001686 rq->rt_avg += rt_delta * arch_scale_freq_capacity(NULL, cpu_of(rq));
Peter Zijlstra029632f2011-10-25 10:00:11 +02001687 sched_avg_update(rq);
1688}
1689#else
1690static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { }
1691static inline void sched_avg_update(struct rq *rq) { }
1692#endif
1693
Peter Zijlstraeb580752015-07-31 21:28:18 +02001694struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
Peter Zijlstra3e71a462016-04-28 16:16:33 +02001695 __acquires(rq->lock);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001696
Peter Zijlstraeb580752015-07-31 21:28:18 +02001697struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001698 __acquires(p->pi_lock)
Peter Zijlstra3e71a462016-04-28 16:16:33 +02001699 __acquires(rq->lock);
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001700
Peter Zijlstraeb580752015-07-31 21:28:18 +02001701static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf)
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001702 __releases(rq->lock)
1703{
Matt Flemingd8ac8972016-09-21 14:38:10 +01001704 rq_unpin_lock(rq, rf);
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001705 raw_spin_unlock(&rq->lock);
1706}
1707
1708static inline void
Peter Zijlstraeb580752015-07-31 21:28:18 +02001709task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001710 __releases(rq->lock)
1711 __releases(p->pi_lock)
1712{
Matt Flemingd8ac8972016-09-21 14:38:10 +01001713 rq_unpin_lock(rq, rf);
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001714 raw_spin_unlock(&rq->lock);
Peter Zijlstraeb580752015-07-31 21:28:18 +02001715 raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001716}
1717
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001718static inline void
1719rq_lock_irqsave(struct rq *rq, struct rq_flags *rf)
1720 __acquires(rq->lock)
1721{
1722 raw_spin_lock_irqsave(&rq->lock, rf->flags);
1723 rq_pin_lock(rq, rf);
1724}
1725
1726static inline void
1727rq_lock_irq(struct rq *rq, struct rq_flags *rf)
1728 __acquires(rq->lock)
1729{
1730 raw_spin_lock_irq(&rq->lock);
1731 rq_pin_lock(rq, rf);
1732}
1733
1734static inline void
1735rq_lock(struct rq *rq, struct rq_flags *rf)
1736 __acquires(rq->lock)
1737{
1738 raw_spin_lock(&rq->lock);
1739 rq_pin_lock(rq, rf);
1740}
1741
1742static inline void
1743rq_relock(struct rq *rq, struct rq_flags *rf)
1744 __acquires(rq->lock)
1745{
1746 raw_spin_lock(&rq->lock);
1747 rq_repin_lock(rq, rf);
1748}
1749
1750static inline void
1751rq_unlock_irqrestore(struct rq *rq, struct rq_flags *rf)
1752 __releases(rq->lock)
1753{
1754 rq_unpin_lock(rq, rf);
1755 raw_spin_unlock_irqrestore(&rq->lock, rf->flags);
1756}
1757
1758static inline void
1759rq_unlock_irq(struct rq *rq, struct rq_flags *rf)
1760 __releases(rq->lock)
1761{
1762 rq_unpin_lock(rq, rf);
1763 raw_spin_unlock_irq(&rq->lock);
1764}
1765
1766static inline void
1767rq_unlock(struct rq *rq, struct rq_flags *rf)
1768 __releases(rq->lock)
1769{
1770 rq_unpin_lock(rq, rf);
1771 raw_spin_unlock(&rq->lock);
1772}
1773
Peter Zijlstra029632f2011-10-25 10:00:11 +02001774#ifdef CONFIG_SMP
1775#ifdef CONFIG_PREEMPT
1776
1777static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
1778
1779/*
1780 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1781 * way at the expense of forcing extra atomic operations in all
1782 * invocations. This assures that the double_lock is acquired using the
1783 * same underlying policy as the spinlock_t on this architecture, which
1784 * reduces latency compared to the unfair variant below. However, it
1785 * also adds more overhead and therefore may reduce throughput.
1786 */
1787static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1788 __releases(this_rq->lock)
1789 __acquires(busiest->lock)
1790 __acquires(this_rq->lock)
1791{
1792 raw_spin_unlock(&this_rq->lock);
1793 double_rq_lock(this_rq, busiest);
1794
1795 return 1;
1796}
1797
1798#else
1799/*
1800 * Unfair double_lock_balance: Optimizes throughput at the expense of
1801 * latency by eliminating extra atomic operations when the locks are
1802 * already in proper order on entry. This favors lower cpu-ids and will
1803 * grant the double lock to lower cpus over higher ids under contention,
1804 * regardless of entry order into the function.
1805 */
1806static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1807 __releases(this_rq->lock)
1808 __acquires(busiest->lock)
1809 __acquires(this_rq->lock)
1810{
1811 int ret = 0;
1812
1813 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
1814 if (busiest < this_rq) {
1815 raw_spin_unlock(&this_rq->lock);
1816 raw_spin_lock(&busiest->lock);
1817 raw_spin_lock_nested(&this_rq->lock,
1818 SINGLE_DEPTH_NESTING);
1819 ret = 1;
1820 } else
1821 raw_spin_lock_nested(&busiest->lock,
1822 SINGLE_DEPTH_NESTING);
1823 }
1824 return ret;
1825}
1826
1827#endif /* CONFIG_PREEMPT */
1828
1829/*
1830 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1831 */
1832static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1833{
1834 if (unlikely(!irqs_disabled())) {
1835 /* printk() doesn't work good under rq->lock */
1836 raw_spin_unlock(&this_rq->lock);
1837 BUG_ON(1);
1838 }
1839
1840 return _double_lock_balance(this_rq, busiest);
1841}
1842
1843static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1844 __releases(busiest->lock)
1845{
1846 raw_spin_unlock(&busiest->lock);
1847 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1848}
1849
Peter Zijlstra74602312013-10-10 20:17:22 +02001850static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
1851{
1852 if (l1 > l2)
1853 swap(l1, l2);
1854
1855 spin_lock(l1);
1856 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1857}
1858
Mike Galbraith60e69ee2014-04-07 10:55:15 +02001859static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2)
1860{
1861 if (l1 > l2)
1862 swap(l1, l2);
1863
1864 spin_lock_irq(l1);
1865 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1866}
1867
Peter Zijlstra74602312013-10-10 20:17:22 +02001868static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
1869{
1870 if (l1 > l2)
1871 swap(l1, l2);
1872
1873 raw_spin_lock(l1);
1874 raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1875}
1876
Peter Zijlstra029632f2011-10-25 10:00:11 +02001877/*
1878 * double_rq_lock - safely lock two runqueues
1879 *
1880 * Note this does not disable interrupts like task_rq_lock,
1881 * you need to do so manually before calling.
1882 */
1883static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1884 __acquires(rq1->lock)
1885 __acquires(rq2->lock)
1886{
1887 BUG_ON(!irqs_disabled());
1888 if (rq1 == rq2) {
1889 raw_spin_lock(&rq1->lock);
1890 __acquire(rq2->lock); /* Fake it out ;) */
1891 } else {
1892 if (rq1 < rq2) {
1893 raw_spin_lock(&rq1->lock);
1894 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
1895 } else {
1896 raw_spin_lock(&rq2->lock);
1897 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1898 }
1899 }
1900}
1901
1902/*
1903 * double_rq_unlock - safely unlock two runqueues
1904 *
1905 * Note this does not restore interrupts like task_rq_unlock,
1906 * you need to do so manually after calling.
1907 */
1908static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1909 __releases(rq1->lock)
1910 __releases(rq2->lock)
1911{
1912 raw_spin_unlock(&rq1->lock);
1913 if (rq1 != rq2)
1914 raw_spin_unlock(&rq2->lock);
1915 else
1916 __release(rq2->lock);
1917}
1918
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001919extern void set_rq_online (struct rq *rq);
1920extern void set_rq_offline(struct rq *rq);
1921extern bool sched_smp_initialized;
1922
Peter Zijlstra029632f2011-10-25 10:00:11 +02001923#else /* CONFIG_SMP */
1924
1925/*
1926 * double_rq_lock - safely lock two runqueues
1927 *
1928 * Note this does not disable interrupts like task_rq_lock,
1929 * you need to do so manually before calling.
1930 */
1931static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1932 __acquires(rq1->lock)
1933 __acquires(rq2->lock)
1934{
1935 BUG_ON(!irqs_disabled());
1936 BUG_ON(rq1 != rq2);
1937 raw_spin_lock(&rq1->lock);
1938 __acquire(rq2->lock); /* Fake it out ;) */
1939}
1940
1941/*
1942 * double_rq_unlock - safely unlock two runqueues
1943 *
1944 * Note this does not restore interrupts like task_rq_unlock,
1945 * you need to do so manually after calling.
1946 */
1947static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1948 __releases(rq1->lock)
1949 __releases(rq2->lock)
1950{
1951 BUG_ON(rq1 != rq2);
1952 raw_spin_unlock(&rq1->lock);
1953 __release(rq2->lock);
1954}
1955
1956#endif
1957
1958extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
1959extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
Srikar Dronamraju6b55c962015-06-25 22:51:41 +05301960
1961#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra9469eb02017-09-07 17:03:53 +02001962extern bool sched_debug_enabled;
1963
Peter Zijlstra029632f2011-10-25 10:00:11 +02001964extern void print_cfs_stats(struct seq_file *m, int cpu);
1965extern void print_rt_stats(struct seq_file *m, int cpu);
Wanpeng Liacb32132014-10-31 06:39:33 +08001966extern void print_dl_stats(struct seq_file *m, int cpu);
Srikar Dronamraju6b55c962015-06-25 22:51:41 +05301967extern void
1968print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq);
Srikar Dronamraju397f2372015-06-25 22:51:43 +05301969#ifdef CONFIG_NUMA_BALANCING
1970extern void
1971show_numa_stats(struct task_struct *p, struct seq_file *m);
1972extern void
1973print_numa_stats(struct seq_file *m, int node, unsigned long tsf,
1974 unsigned long tpf, unsigned long gsf, unsigned long gpf);
1975#endif /* CONFIG_NUMA_BALANCING */
1976#endif /* CONFIG_SCHED_DEBUG */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001977
1978extern void init_cfs_rq(struct cfs_rq *cfs_rq);
Abel Vesa07c54f72015-03-03 13:50:27 +02001979extern void init_rt_rq(struct rt_rq *rt_rq);
1980extern void init_dl_rq(struct dl_rq *dl_rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001981
Ben Segall1ee14e62013-10-16 11:16:12 -07001982extern void cfs_bandwidth_usage_inc(void);
1983extern void cfs_bandwidth_usage_dec(void);
Suresh Siddha1c792db2011-12-01 17:07:32 -08001984
Frederic Weisbecker3451d022011-08-10 23:21:01 +02001985#ifdef CONFIG_NO_HZ_COMMON
Suresh Siddha1c792db2011-12-01 17:07:32 -08001986enum rq_nohz_flag_bits {
1987 NOHZ_TICK_STOPPED,
1988 NOHZ_BALANCE_KICK,
1989};
1990
1991#define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
Thomas Gleixner20a5c8c2016-03-10 12:54:20 +01001992
1993extern void nohz_balance_exit_idle(unsigned int cpu);
1994#else
1995static inline void nohz_balance_exit_idle(unsigned int cpu) { }
Suresh Siddha1c792db2011-12-01 17:07:32 -08001996#endif
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02001997
Luca Abenidaec5792017-05-18 22:13:36 +02001998
1999#ifdef CONFIG_SMP
2000static inline
2001void __dl_update(struct dl_bw *dl_b, s64 bw)
2002{
2003 struct root_domain *rd = container_of(dl_b, struct root_domain, dl_bw);
2004 int i;
2005
2006 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
2007 "sched RCU must be held");
2008 for_each_cpu_and(i, rd->span, cpu_active_mask) {
2009 struct rq *rq = cpu_rq(i);
2010
2011 rq->dl.extra_bw += bw;
2012 }
2013}
2014#else
2015static inline
2016void __dl_update(struct dl_bw *dl_b, s64 bw)
2017{
2018 struct dl_rq *dl = container_of(dl_b, struct dl_rq, dl_bw);
2019
2020 dl->extra_bw += bw;
2021}
2022#endif
2023
2024
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002025#ifdef CONFIG_IRQ_TIME_ACCOUNTING
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002026struct irqtime {
Frederic Weisbecker25e2d8c2017-04-25 16:10:48 +02002027 u64 total;
Frederic Weisbeckera499a5a2017-01-31 04:09:32 +01002028 u64 tick_delta;
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002029 u64 irq_start_time;
2030 struct u64_stats_sync sync;
2031};
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002032
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002033DECLARE_PER_CPU(struct irqtime, cpu_irqtime);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002034
Frederic Weisbecker25e2d8c2017-04-25 16:10:48 +02002035/*
2036 * Returns the irqtime minus the softirq time computed by ksoftirqd.
2037 * Otherwise ksoftirqd's sum_exec_runtime is substracted its own runtime
2038 * and never move forward.
2039 */
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002040static inline u64 irq_time_read(int cpu)
2041{
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002042 struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu);
2043 unsigned int seq;
2044 u64 total;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002045
2046 do {
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002047 seq = __u64_stats_fetch_begin(&irqtime->sync);
Frederic Weisbecker25e2d8c2017-04-25 16:10:48 +02002048 total = irqtime->total;
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002049 } while (__u64_stats_fetch_retry(&irqtime->sync, seq));
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002050
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002051 return total;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002052}
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002053#endif /* CONFIG_IRQ_TIME_ACCOUNTING */
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002054
2055#ifdef CONFIG_CPU_FREQ
2056DECLARE_PER_CPU(struct update_util_data *, cpufreq_update_util_data);
2057
2058/**
2059 * cpufreq_update_util - Take a note about CPU utilization changes.
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02002060 * @rq: Runqueue to carry out the update for.
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02002061 * @flags: Update reason flags.
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002062 *
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02002063 * This function is called by the scheduler on the CPU whose utilization is
2064 * being updated.
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002065 *
2066 * It can only be called from RCU-sched read-side critical sections.
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002067 *
2068 * The way cpufreq is currently arranged requires it to evaluate the CPU
2069 * performance state (frequency/voltage) on a regular basis to prevent it from
2070 * being stuck in a completely inadequate performance level for too long.
2071 * That is not guaranteed to happen if the updates are only triggered from CFS,
2072 * though, because they may not be coming in if RT or deadline tasks are active
2073 * all the time (or there are RT and DL tasks only).
2074 *
2075 * As a workaround for that issue, this function is called by the RT and DL
2076 * sched classes to trigger extra cpufreq updates to prevent it from stalling,
2077 * but that really is a band-aid. Going forward it should be replaced with
2078 * solutions targeted more specifically at RT and DL tasks.
2079 */
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02002080static inline void cpufreq_update_util(struct rq *rq, unsigned int flags)
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002081{
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02002082 struct update_util_data *data;
2083
Viresh Kumar674e7542017-07-28 12:16:38 +05302084 data = rcu_dereference_sched(*per_cpu_ptr(&cpufreq_update_util_data,
2085 cpu_of(rq)));
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02002086 if (data)
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02002087 data->func(data, rq_clock(rq), flags);
2088}
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002089#else
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02002090static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {}
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002091#endif /* CONFIG_CPU_FREQ */
Linus Torvaldsbe53f582016-03-24 09:42:50 -07002092
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +02002093#ifdef arch_scale_freq_capacity
2094#ifndef arch_scale_freq_invariant
2095#define arch_scale_freq_invariant() (true)
2096#endif
2097#else /* arch_scale_freq_capacity */
2098#define arch_scale_freq_invariant() (false)
2099#endif