blob: a81c9782e98cee27dc0db9d498d001d42d1e0ce8 [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
Peter Zijlstra8c0944ce2017-09-07 12:09:30 +0200229void __dl_sub(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);
Nicolas Pitre06a76fe2017-06-21 14:22:01 -0400258extern bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr);
259extern int dl_task_can_attach(struct task_struct *p,
260 const struct cpumask *cs_cpus_allowed);
261extern int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur,
262 const struct cpumask *trial);
263extern bool dl_cpu_busy(unsigned int cpu);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200264
265#ifdef CONFIG_CGROUP_SCHED
266
267#include <linux/cgroup.h>
268
269struct cfs_rq;
270struct rt_rq;
271
Mike Galbraith35cf4e52012-08-07 05:00:13 +0200272extern struct list_head task_groups;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200273
274struct cfs_bandwidth {
275#ifdef CONFIG_CFS_BANDWIDTH
276 raw_spinlock_t lock;
277 ktime_t period;
278 u64 quota, runtime;
Zhihui Zhang9c58c792014-09-20 21:24:36 -0400279 s64 hierarchical_quota;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200280 u64 runtime_expires;
281
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200282 int idle, period_active;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200283 struct hrtimer period_timer, slack_timer;
284 struct list_head throttled_cfs_rq;
285
286 /* statistics */
287 int nr_periods, nr_throttled;
288 u64 throttled_time;
289#endif
290};
291
292/* task group related information */
293struct task_group {
294 struct cgroup_subsys_state css;
295
296#ifdef CONFIG_FAIR_GROUP_SCHED
297 /* schedulable entities of this group on each cpu */
298 struct sched_entity **se;
299 /* runqueue "owned" by this group on each cpu */
300 struct cfs_rq **cfs_rq;
301 unsigned long shares;
302
Alex Shifa6bdde2013-06-20 10:18:46 +0800303#ifdef CONFIG_SMP
Waiman Longb0367622015-12-02 13:41:49 -0500304 /*
305 * load_avg can be heavily contended at clock tick time, so put
306 * it in its own cacheline separated from the fields above which
307 * will also be accessed at each tick.
308 */
309 atomic_long_t load_avg ____cacheline_aligned;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200310#endif
Alex Shifa6bdde2013-06-20 10:18:46 +0800311#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200312
313#ifdef CONFIG_RT_GROUP_SCHED
314 struct sched_rt_entity **rt_se;
315 struct rt_rq **rt_rq;
316
317 struct rt_bandwidth rt_bandwidth;
318#endif
319
320 struct rcu_head rcu;
321 struct list_head list;
322
323 struct task_group *parent;
324 struct list_head siblings;
325 struct list_head children;
326
327#ifdef CONFIG_SCHED_AUTOGROUP
328 struct autogroup *autogroup;
329#endif
330
331 struct cfs_bandwidth cfs_bandwidth;
332};
333
334#ifdef CONFIG_FAIR_GROUP_SCHED
335#define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
336
337/*
338 * A weight of 0 or 1 can cause arithmetics problems.
339 * A weight of a cfs_rq is the sum of weights of which entities
340 * are queued on this cfs_rq, so a weight of a entity should not be
341 * too large, so as the shares value of a task group.
342 * (The default weight is 1024 - so there's no practical
343 * limitation from this.)
344 */
345#define MIN_SHARES (1UL << 1)
346#define MAX_SHARES (1UL << 18)
347#endif
348
Peter Zijlstra029632f2011-10-25 10:00:11 +0200349typedef int (*tg_visitor)(struct task_group *, void *);
350
351extern int walk_tg_tree_from(struct task_group *from,
352 tg_visitor down, tg_visitor up, void *data);
353
354/*
355 * Iterate the full tree, calling @down when first entering a node and @up when
356 * leaving it for the final time.
357 *
358 * Caller must hold rcu_lock or sufficient equivalent.
359 */
360static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
361{
362 return walk_tg_tree_from(&root_task_group, down, up, data);
363}
364
365extern int tg_nop(struct task_group *tg, void *data);
366
367extern void free_fair_sched_group(struct task_group *tg);
368extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
Peter Zijlstra8663e242016-06-22 14:58:02 +0200369extern void online_fair_sched_group(struct task_group *tg);
Peter Zijlstra6fe1f342016-01-21 22:24:16 +0100370extern void unregister_fair_sched_group(struct task_group *tg);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200371extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
372 struct sched_entity *se, int cpu,
373 struct sched_entity *parent);
374extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200375
376extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +0200377extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200378extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
379
380extern void free_rt_sched_group(struct task_group *tg);
381extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
382extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
383 struct sched_rt_entity *rt_se, int cpu,
384 struct sched_rt_entity *parent);
Nicolas Pitre8887cd92017-06-21 14:22:02 -0400385extern int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us);
386extern int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us);
387extern long sched_group_rt_runtime(struct task_group *tg);
388extern long sched_group_rt_period(struct task_group *tg);
389extern int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200390
Li Zefan25cc7da2013-03-05 16:07:33 +0800391extern struct task_group *sched_create_group(struct task_group *parent);
392extern void sched_online_group(struct task_group *tg,
393 struct task_group *parent);
394extern void sched_destroy_group(struct task_group *tg);
395extern void sched_offline_group(struct task_group *tg);
396
397extern void sched_move_task(struct task_struct *tsk);
398
399#ifdef CONFIG_FAIR_GROUP_SCHED
400extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
Byungchul Parkad936d82015-10-24 01:16:19 +0900401
402#ifdef CONFIG_SMP
403extern void set_task_rq_fair(struct sched_entity *se,
404 struct cfs_rq *prev, struct cfs_rq *next);
405#else /* !CONFIG_SMP */
406static inline void set_task_rq_fair(struct sched_entity *se,
407 struct cfs_rq *prev, struct cfs_rq *next) { }
408#endif /* CONFIG_SMP */
409#endif /* CONFIG_FAIR_GROUP_SCHED */
Li Zefan25cc7da2013-03-05 16:07:33 +0800410
Peter Zijlstra029632f2011-10-25 10:00:11 +0200411#else /* CONFIG_CGROUP_SCHED */
412
413struct cfs_bandwidth { };
414
415#endif /* CONFIG_CGROUP_SCHED */
416
417/* CFS-related fields in a runqueue */
418struct cfs_rq {
419 struct load_weight load;
Peter Zijlstra1ea6c462017-05-06 15:59:54 +0200420 unsigned long runnable_weight;
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;
Peter Zijlstra2a2f5d4e2017-05-08 16:51:41 +0200446#ifndef CONFIG_64BIT
447 u64 load_last_update_time_copy;
448#endif
Peter Zijlstra2a2f5d4e2017-05-08 16:51:41 +0200449 struct {
450 raw_spinlock_t lock ____cacheline_aligned;
451 int nr;
452 unsigned long load_avg;
453 unsigned long util_avg;
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +0200454 unsigned long runnable_sum;
Peter Zijlstra2a2f5d4e2017-05-08 16:51:41 +0200455 } removed;
Alex Shi141965c2013-06-26 13:05:39 +0800456
Paul Turnerc566e8e2012-10-04 13:18:30 +0200457#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstra0e2d2aa2017-05-08 17:30:46 +0200458 unsigned long tg_load_avg_contrib;
459 long propagate;
460 long prop_runnable_sum;
461
Paul Turner82958362012-10-04 13:18:31 +0200462 /*
463 * h_load = weight * f(tg)
464 *
465 * Where f(tg) is the recursive weight fraction assigned to
466 * this group.
467 */
468 unsigned long h_load;
Vladimir Davydov68520792013-07-15 17:49:19 +0400469 u64 last_h_load_update;
470 struct sched_entity *h_load_next;
471#endif /* CONFIG_FAIR_GROUP_SCHED */
Paul Turner82958362012-10-04 13:18:31 +0200472#endif /* CONFIG_SMP */
473
Peter Zijlstra029632f2011-10-25 10:00:11 +0200474#ifdef CONFIG_FAIR_GROUP_SCHED
475 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
476
477 /*
478 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
479 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
480 * (like users, containers etc.)
481 *
482 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
483 * list is used during load balance.
484 */
485 int on_list;
486 struct list_head leaf_cfs_rq_list;
487 struct task_group *tg; /* group that "owns" this runqueue */
488
Peter Zijlstra029632f2011-10-25 10:00:11 +0200489#ifdef CONFIG_CFS_BANDWIDTH
490 int runtime_enabled;
491 u64 runtime_expires;
492 s64 runtime_remaining;
493
Paul Turnerf1b17282012-10-04 13:18:31 +0200494 u64 throttled_clock, throttled_clock_task;
495 u64 throttled_clock_task_time;
Peter Zijlstra55e16d32016-06-22 15:14:26 +0200496 int throttled, throttle_count;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200497 struct list_head throttled_list;
498#endif /* CONFIG_CFS_BANDWIDTH */
499#endif /* CONFIG_FAIR_GROUP_SCHED */
500};
501
502static inline int rt_bandwidth_enabled(void)
503{
504 return sysctl_sched_rt_runtime >= 0;
505}
506
Steven Rostedtb6366f02015-03-18 14:49:46 -0400507/* RT IPI pull logic requires IRQ_WORK */
508#ifdef CONFIG_IRQ_WORK
509# define HAVE_RT_PUSH_IPI
510#endif
511
Peter Zijlstra029632f2011-10-25 10:00:11 +0200512/* Real-Time classes' related field in a runqueue: */
513struct rt_rq {
514 struct rt_prio_array active;
Peter Zijlstrac82513e2012-04-26 13:12:27 +0200515 unsigned int rt_nr_running;
Frederic Weisbecker01d36d02015-11-04 18:17:10 +0100516 unsigned int rr_nr_running;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200517#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
518 struct {
519 int curr; /* highest queued rt task prio */
520#ifdef CONFIG_SMP
521 int next; /* next highest */
522#endif
523 } highest_prio;
524#endif
525#ifdef CONFIG_SMP
526 unsigned long rt_nr_migratory;
527 unsigned long rt_nr_total;
528 int overloaded;
529 struct plist_head pushable_tasks;
Steven Rostedtb6366f02015-03-18 14:49:46 -0400530#ifdef HAVE_RT_PUSH_IPI
531 int push_flags;
532 int push_cpu;
533 struct irq_work push_work;
534 raw_spinlock_t push_lock;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200535#endif
Steven Rostedtb6366f02015-03-18 14:49:46 -0400536#endif /* CONFIG_SMP */
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400537 int rt_queued;
538
Peter Zijlstra029632f2011-10-25 10:00:11 +0200539 int rt_throttled;
540 u64 rt_time;
541 u64 rt_runtime;
542 /* Nests inside the rq lock: */
543 raw_spinlock_t rt_runtime_lock;
544
545#ifdef CONFIG_RT_GROUP_SCHED
546 unsigned long rt_nr_boosted;
547
548 struct rq *rq;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200549 struct task_group *tg;
550#endif
551};
552
Dario Faggioliaab03e02013-11-28 11:14:43 +0100553/* Deadline class' related fields in a runqueue */
554struct dl_rq {
555 /* runqueue is an rbtree, ordered by deadline */
Davidlohr Bueso21615732017-09-08 16:14:58 -0700556 struct rb_root_cached root;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100557
558 unsigned long dl_nr_running;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100559
560#ifdef CONFIG_SMP
561 /*
562 * Deadline values of the currently executing and the
563 * earliest ready task on this rq. Caching these facilitates
564 * the decision wether or not a ready but not running task
565 * should migrate somewhere else.
566 */
567 struct {
568 u64 curr;
569 u64 next;
570 } earliest_dl;
571
572 unsigned long dl_nr_migratory;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100573 int overloaded;
574
575 /*
576 * Tasks on this rq that can be pushed away. They are kept in
577 * an rb-tree, ordered by tasks' deadlines, with caching
578 * of the leftmost (earliest deadline) element.
579 */
Davidlohr Bueso21615732017-09-08 16:14:58 -0700580 struct rb_root_cached pushable_dl_tasks_root;
Dario Faggioli332ac172013-11-07 14:43:45 +0100581#else
582 struct dl_bw dl_bw;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100583#endif
Luca Abenie36d8672017-05-18 22:13:28 +0200584 /*
585 * "Active utilization" for this runqueue: increased when a
586 * task wakes up (becomes TASK_RUNNING) and decreased when a
587 * task blocks
588 */
589 u64 running_bw;
Luca Abeni4da3abc2017-05-18 22:13:32 +0200590
591 /*
Luca Abeni8fd27232017-05-18 22:13:34 +0200592 * Utilization of the tasks "assigned" to this runqueue (including
593 * the tasks that are in runqueue and the tasks that executed on this
594 * CPU and blocked). Increased when a task moves to this runqueue, and
595 * decreased when the task moves away (migrates, changes scheduling
596 * policy, or terminates).
597 * This is needed to compute the "inactive utilization" for the
598 * runqueue (inactive utilization = this_bw - running_bw).
599 */
600 u64 this_bw;
Luca Abenidaec5792017-05-18 22:13:36 +0200601 u64 extra_bw;
Luca Abeni8fd27232017-05-18 22:13:34 +0200602
603 /*
Luca Abeni4da3abc2017-05-18 22:13:32 +0200604 * Inverse of the fraction of CPU utilization that can be reclaimed
605 * by the GRUB algorithm.
606 */
607 u64 bw_ratio;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100608};
609
Peter Zijlstra029632f2011-10-25 10:00:11 +0200610#ifdef CONFIG_SMP
611
Tim Chenafe06ef2016-11-22 12:23:53 -0800612static inline bool sched_asym_prefer(int a, int b)
613{
614 return arch_asym_cpu_priority(a) > arch_asym_cpu_priority(b);
615}
616
Peter Zijlstra029632f2011-10-25 10:00:11 +0200617/*
618 * We add the notion of a root-domain which will be used to define per-domain
619 * variables. Each exclusive cpuset essentially defines an island domain by
620 * fully partitioning the member cpus from any other cpuset. Whenever a new
621 * exclusive cpuset is created, we also create and attach a new root-domain
622 * object.
623 *
624 */
625struct root_domain {
626 atomic_t refcount;
627 atomic_t rto_count;
628 struct rcu_head rcu;
629 cpumask_var_t span;
630 cpumask_var_t online;
631
Tim Chen4486edd2014-06-23 12:16:49 -0700632 /* Indicate more than one runnable task for any CPU */
633 bool overload;
634
Peter Zijlstra029632f2011-10-25 10:00:11 +0200635 /*
Juri Lelli1baca4c2013-11-07 14:43:38 +0100636 * The bit corresponding to a CPU gets set here if such CPU has more
637 * than one runnable -deadline task (as it is below for RT tasks).
638 */
639 cpumask_var_t dlo_mask;
640 atomic_t dlo_count;
Dario Faggioli332ac172013-11-07 14:43:45 +0100641 struct dl_bw dl_bw;
Juri Lelli6bfd6d72013-11-07 14:43:47 +0100642 struct cpudl cpudl;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100643
644 /*
Peter Zijlstra029632f2011-10-25 10:00:11 +0200645 * The "RT overload" flag: it gets set if a CPU has more than
646 * one runnable RT task.
647 */
648 cpumask_var_t rto_mask;
649 struct cpupri cpupri;
Dietmar Eggemanncd92bfd2016-08-01 19:53:35 +0100650
651 unsigned long max_cpu_capacity;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200652};
653
654extern struct root_domain def_root_domain;
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100655extern struct mutex sched_domains_mutex;
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100656
657extern void init_defrootdomain(void);
Peter Zijlstra8d5dc512017-04-25 15:29:40 +0200658extern int sched_init_domains(const struct cpumask *cpu_map);
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100659extern void rq_attach_root(struct rq *rq, struct root_domain *rd);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200660
661#endif /* CONFIG_SMP */
662
663/*
664 * This is the main, per-CPU runqueue data structure.
665 *
666 * Locking rule: those places that want to lock multiple runqueues
667 * (such as the load balancing or the thread migration code), lock
668 * acquire operations must be ordered by ascending &runqueue.
669 */
670struct rq {
671 /* runqueue lock: */
672 raw_spinlock_t lock;
673
674 /*
675 * nr_running and cpu_load should be in the same cacheline because
676 * remote CPUs use both these fields when doing load calculation.
677 */
Peter Zijlstrac82513e2012-04-26 13:12:27 +0200678 unsigned int nr_running;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +0100679#ifdef CONFIG_NUMA_BALANCING
680 unsigned int nr_numa_running;
681 unsigned int nr_preferred_running;
682#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200683 #define CPU_LOAD_IDX_MAX 5
684 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200685#ifdef CONFIG_NO_HZ_COMMON
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +0200686#ifdef CONFIG_SMP
687 unsigned long last_load_update_tick;
688#endif /* CONFIG_SMP */
Suresh Siddha1c792db2011-12-01 17:07:32 -0800689 unsigned long nohz_flags;
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +0200690#endif /* CONFIG_NO_HZ_COMMON */
Frederic Weisbecker265f22a2013-05-03 03:39:05 +0200691#ifdef CONFIG_NO_HZ_FULL
692 unsigned long last_sched_tick;
693#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200694 /* capture load from *all* tasks on this cpu: */
695 struct load_weight load;
696 unsigned long nr_load_updates;
697 u64 nr_switches;
698
699 struct cfs_rq cfs;
700 struct rt_rq rt;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100701 struct dl_rq dl;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200702
703#ifdef CONFIG_FAIR_GROUP_SCHED
704 /* list of leaf cfs_rq on this cpu: */
705 struct list_head leaf_cfs_rq_list;
Vincent Guittot9c2791f2016-11-08 10:53:43 +0100706 struct list_head *tmp_alone_branch;
Peter Zijlstraa35b6462012-08-08 21:46:40 +0200707#endif /* CONFIG_FAIR_GROUP_SCHED */
708
Peter Zijlstra029632f2011-10-25 10:00:11 +0200709 /*
710 * This is part of a global counter where only the total sum
711 * over all CPUs matters. A task can increase this counter on
712 * one CPU and if it got migrated afterwards it may decrease
713 * it on another CPU. Always updated under the runqueue lock:
714 */
715 unsigned long nr_uninterruptible;
716
717 struct task_struct *curr, *idle, *stop;
718 unsigned long next_balance;
719 struct mm_struct *prev_mm;
720
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100721 unsigned int clock_update_flags;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200722 u64 clock;
723 u64 clock_task;
724
725 atomic_t nr_iowait;
726
727#ifdef CONFIG_SMP
728 struct root_domain *rd;
729 struct sched_domain *sd;
730
Nicolas Pitreced549f2014-05-26 18:19:38 -0400731 unsigned long cpu_capacity;
Vincent Guittotca6d75e2015-02-27 16:54:09 +0100732 unsigned long cpu_capacity_orig;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200733
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200734 struct callback_head *balance_callback;
735
Peter Zijlstra029632f2011-10-25 10:00:11 +0200736 unsigned char idle_balance;
737 /* For active balancing */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200738 int active_balance;
739 int push_cpu;
740 struct cpu_stop_work active_balance_work;
741 /* cpu of this runqueue: */
742 int cpu;
743 int online;
744
Peter Zijlstra367456c2012-02-20 21:49:09 +0100745 struct list_head cfs_tasks;
746
Peter Zijlstra029632f2011-10-25 10:00:11 +0200747 u64 rt_avg;
748 u64 age_stamp;
749 u64 idle_stamp;
750 u64 avg_idle;
Jason Low9bd721c2013-09-13 11:26:52 -0700751
752 /* This is used to determine avg_idle's max value */
753 u64 max_idle_balance_cost;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200754#endif
755
756#ifdef CONFIG_IRQ_TIME_ACCOUNTING
757 u64 prev_irq_time;
758#endif
759#ifdef CONFIG_PARAVIRT
760 u64 prev_steal_time;
761#endif
762#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
763 u64 prev_steal_time_rq;
764#endif
765
766 /* calc_load related fields */
767 unsigned long calc_load_update;
768 long calc_load_active;
769
770#ifdef CONFIG_SCHED_HRTICK
771#ifdef CONFIG_SMP
772 int hrtick_csd_pending;
Ying Huang966a9672017-08-08 12:30:00 +0800773 call_single_data_t hrtick_csd;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200774#endif
775 struct hrtimer hrtick_timer;
776#endif
777
778#ifdef CONFIG_SCHEDSTATS
779 /* latency stats */
780 struct sched_info rq_sched_info;
781 unsigned long long rq_cpu_time;
782 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
783
784 /* sys_sched_yield() stats */
785 unsigned int yld_count;
786
787 /* schedule() stats */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200788 unsigned int sched_count;
789 unsigned int sched_goidle;
790
791 /* try_to_wake_up() stats */
792 unsigned int ttwu_count;
793 unsigned int ttwu_local;
794#endif
795
796#ifdef CONFIG_SMP
797 struct llist_head wake_list;
798#endif
Daniel Lezcano442bf3a2014-09-04 11:32:09 -0400799
800#ifdef CONFIG_CPU_IDLE
801 /* Must be inspected within a rcu lock section */
802 struct cpuidle_state *idle_state;
803#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200804};
805
806static inline int cpu_of(struct rq *rq)
807{
808#ifdef CONFIG_SMP
809 return rq->cpu;
810#else
811 return 0;
812#endif
813}
814
Peter Zijlstra1b568f02016-05-09 10:38:41 +0200815
816#ifdef CONFIG_SCHED_SMT
817
818extern struct static_key_false sched_smt_present;
819
820extern void __update_idle_core(struct rq *rq);
821
822static inline void update_idle_core(struct rq *rq)
823{
824 if (static_branch_unlikely(&sched_smt_present))
825 __update_idle_core(rq);
826}
827
828#else
829static inline void update_idle_core(struct rq *rq) { }
830#endif
831
Pranith Kumar8b06c552014-08-13 13:28:12 -0400832DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200833
Peter Zijlstra518cd622011-12-07 15:07:31 +0100834#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500835#define this_rq() this_cpu_ptr(&runqueues)
Peter Zijlstra518cd622011-12-07 15:07:31 +0100836#define task_rq(p) cpu_rq(task_cpu(p))
837#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500838#define raw_rq() raw_cpu_ptr(&runqueues)
Peter Zijlstra518cd622011-12-07 15:07:31 +0100839
Peter Zijlstracebde6d2015-01-05 11:18:10 +0100840static inline u64 __rq_clock_broken(struct rq *rq)
841{
Jason Low316c1608d2015-04-28 13:00:20 -0700842 return READ_ONCE(rq->clock);
Peter Zijlstracebde6d2015-01-05 11:18:10 +0100843}
844
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100845/*
846 * rq::clock_update_flags bits
847 *
848 * %RQCF_REQ_SKIP - will request skipping of clock update on the next
849 * call to __schedule(). This is an optimisation to avoid
850 * neighbouring rq clock updates.
851 *
852 * %RQCF_ACT_SKIP - is set from inside of __schedule() when skipping is
853 * in effect and calls to update_rq_clock() are being ignored.
854 *
855 * %RQCF_UPDATED - is a debug flag that indicates whether a call has been
856 * made to update_rq_clock() since the last time rq::lock was pinned.
857 *
858 * If inside of __schedule(), clock_update_flags will have been
859 * shifted left (a left shift is a cheap operation for the fast path
860 * to promote %RQCF_REQ_SKIP to %RQCF_ACT_SKIP), so you must use,
861 *
862 * if (rq-clock_update_flags >= RQCF_UPDATED)
863 *
864 * to check if %RQCF_UPADTED is set. It'll never be shifted more than
865 * one position though, because the next rq_unpin_lock() will shift it
866 * back.
867 */
868#define RQCF_REQ_SKIP 0x01
869#define RQCF_ACT_SKIP 0x02
870#define RQCF_UPDATED 0x04
871
872static inline void assert_clock_updated(struct rq *rq)
873{
874 /*
875 * The only reason for not seeing a clock update since the
876 * last rq_pin_lock() is if we're currently skipping updates.
877 */
878 SCHED_WARN_ON(rq->clock_update_flags < RQCF_ACT_SKIP);
879}
880
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200881static inline u64 rq_clock(struct rq *rq)
882{
Peter Zijlstracebde6d2015-01-05 11:18:10 +0100883 lockdep_assert_held(&rq->lock);
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100884 assert_clock_updated(rq);
885
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200886 return rq->clock;
887}
888
889static inline u64 rq_clock_task(struct rq *rq)
890{
Peter Zijlstracebde6d2015-01-05 11:18:10 +0100891 lockdep_assert_held(&rq->lock);
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100892 assert_clock_updated(rq);
893
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200894 return rq->clock_task;
895}
896
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100897static inline void rq_clock_skip_update(struct rq *rq, bool skip)
898{
899 lockdep_assert_held(&rq->lock);
900 if (skip)
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100901 rq->clock_update_flags |= RQCF_REQ_SKIP;
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100902 else
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100903 rq->clock_update_flags &= ~RQCF_REQ_SKIP;
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100904}
905
Matt Flemingd8ac8972016-09-21 14:38:10 +0100906struct rq_flags {
907 unsigned long flags;
908 struct pin_cookie cookie;
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100909#ifdef CONFIG_SCHED_DEBUG
910 /*
911 * A copy of (rq::clock_update_flags & RQCF_UPDATED) for the
912 * current pin context is stashed here in case it needs to be
913 * restored in rq_repin_lock().
914 */
915 unsigned int clock_update_flags;
916#endif
Matt Flemingd8ac8972016-09-21 14:38:10 +0100917};
918
919static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf)
920{
921 rf->cookie = lockdep_pin_lock(&rq->lock);
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100922
923#ifdef CONFIG_SCHED_DEBUG
924 rq->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
925 rf->clock_update_flags = 0;
926#endif
Matt Flemingd8ac8972016-09-21 14:38:10 +0100927}
928
929static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf)
930{
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100931#ifdef CONFIG_SCHED_DEBUG
932 if (rq->clock_update_flags > RQCF_ACT_SKIP)
933 rf->clock_update_flags = RQCF_UPDATED;
934#endif
935
Matt Flemingd8ac8972016-09-21 14:38:10 +0100936 lockdep_unpin_lock(&rq->lock, rf->cookie);
937}
938
939static inline void rq_repin_lock(struct rq *rq, struct rq_flags *rf)
940{
941 lockdep_repin_lock(&rq->lock, rf->cookie);
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100942
943#ifdef CONFIG_SCHED_DEBUG
944 /*
945 * Restore the value we stashed in @rf for this pin context.
946 */
947 rq->clock_update_flags |= rf->clock_update_flags;
948#endif
Matt Flemingd8ac8972016-09-21 14:38:10 +0100949}
950
Rik van Riel9942f792014-10-17 03:29:49 -0400951#ifdef CONFIG_NUMA
Rik van Riele3fe70b2014-10-17 03:29:50 -0400952enum numa_topology_type {
953 NUMA_DIRECT,
954 NUMA_GLUELESS_MESH,
955 NUMA_BACKPLANE,
956};
957extern enum numa_topology_type sched_numa_topology_type;
Rik van Riel9942f792014-10-17 03:29:49 -0400958extern int sched_max_numa_distance;
959extern bool find_numa_distance(int distance);
960#endif
961
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100962#ifdef CONFIG_NUMA
963extern void sched_init_numa(void);
964extern void sched_domains_numa_masks_set(unsigned int cpu);
965extern void sched_domains_numa_masks_clear(unsigned int cpu);
966#else
967static inline void sched_init_numa(void) { }
968static inline void sched_domains_numa_masks_set(unsigned int cpu) { }
969static inline void sched_domains_numa_masks_clear(unsigned int cpu) { }
970#endif
971
Mel Gormanf809ca92013-10-07 11:28:57 +0100972#ifdef CONFIG_NUMA_BALANCING
Iulia Manda44dba3d2014-10-31 02:13:31 +0200973/* The regions in numa_faults array from task_struct */
974enum numa_faults_stats {
975 NUMA_MEM = 0,
976 NUMA_CPU,
977 NUMA_MEMBUF,
978 NUMA_CPUBUF
979};
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +0100980extern void sched_setnuma(struct task_struct *p, int node);
Mel Gormane6628d52013-10-07 11:29:02 +0100981extern int migrate_task_to(struct task_struct *p, int cpu);
Peter Zijlstraac66f542013-10-07 11:29:16 +0100982extern int migrate_swap(struct task_struct *, struct task_struct *);
Mel Gormanf809ca92013-10-07 11:28:57 +0100983#endif /* CONFIG_NUMA_BALANCING */
984
Peter Zijlstra518cd622011-12-07 15:07:31 +0100985#ifdef CONFIG_SMP
986
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200987static inline void
988queue_balance_callback(struct rq *rq,
989 struct callback_head *head,
990 void (*func)(struct rq *rq))
991{
992 lockdep_assert_held(&rq->lock);
993
994 if (unlikely(head->next))
995 return;
996
997 head->func = (void (*)(struct callback_head *))func;
998 head->next = rq->balance_callback;
999 rq->balance_callback = head;
1000}
1001
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001002extern void sched_ttwu_pending(void);
1003
Peter Zijlstra029632f2011-10-25 10:00:11 +02001004#define rcu_dereference_check_sched_domain(p) \
1005 rcu_dereference_check((p), \
1006 lockdep_is_held(&sched_domains_mutex))
1007
1008/*
1009 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
1010 * See detach_destroy_domains: synchronize_sched for details.
1011 *
1012 * The domain tree of any CPU may only be accessed from within
1013 * preempt-disabled sections.
1014 */
1015#define for_each_domain(cpu, __sd) \
Peter Zijlstra518cd622011-12-07 15:07:31 +01001016 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
1017 __sd; __sd = __sd->parent)
Peter Zijlstra029632f2011-10-25 10:00:11 +02001018
Suresh Siddha77e81362011-11-17 11:08:23 -08001019#define for_each_lower_domain(sd) for (; sd; sd = sd->child)
1020
Peter Zijlstra518cd622011-12-07 15:07:31 +01001021/**
1022 * highest_flag_domain - Return highest sched_domain containing flag.
1023 * @cpu: The cpu whose highest level of sched domain is to
1024 * be returned.
1025 * @flag: The flag to check for the highest sched_domain
1026 * for the given cpu.
1027 *
1028 * Returns the highest sched_domain of a cpu which contains the given flag.
1029 */
1030static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
1031{
1032 struct sched_domain *sd, *hsd = NULL;
1033
1034 for_each_domain(cpu, sd) {
1035 if (!(sd->flags & flag))
1036 break;
1037 hsd = sd;
1038 }
1039
1040 return hsd;
1041}
1042
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001043static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
1044{
1045 struct sched_domain *sd;
1046
1047 for_each_domain(cpu, sd) {
1048 if (sd->flags & flag)
1049 break;
1050 }
1051
1052 return sd;
1053}
1054
Peter Zijlstra518cd622011-12-07 15:07:31 +01001055DECLARE_PER_CPU(struct sched_domain *, sd_llc);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08001056DECLARE_PER_CPU(int, sd_llc_size);
Peter Zijlstra518cd622011-12-07 15:07:31 +01001057DECLARE_PER_CPU(int, sd_llc_id);
Peter Zijlstra0e369d72016-05-09 10:38:01 +02001058DECLARE_PER_CPU(struct sched_domain_shared *, sd_llc_shared);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001059DECLARE_PER_CPU(struct sched_domain *, sd_numa);
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05301060DECLARE_PER_CPU(struct sched_domain *, sd_asym);
Peter Zijlstra518cd622011-12-07 15:07:31 +01001061
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001062struct sched_group_capacity {
Li Zefan5e6521e2013-03-05 16:06:23 +08001063 atomic_t ref;
1064 /*
Yuyang Du172895e2016-04-05 12:12:27 +08001065 * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001066 * for a single CPU.
Li Zefan5e6521e2013-03-05 16:06:23 +08001067 */
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01001068 unsigned long capacity;
1069 unsigned long min_capacity; /* Min per-CPU capacity in group */
Li Zefan5e6521e2013-03-05 16:06:23 +08001070 unsigned long next_update;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001071 int imbalance; /* XXX unrelated to capacity but shared group state */
Li Zefan5e6521e2013-03-05 16:06:23 +08001072
Peter Zijlstra005f8742017-04-26 17:35:35 +02001073#ifdef CONFIG_SCHED_DEBUG
1074 int id;
1075#endif
1076
Peter Zijlstrae5c14b12017-05-01 10:47:02 +02001077 unsigned long cpumask[0]; /* balance mask */
Li Zefan5e6521e2013-03-05 16:06:23 +08001078};
1079
1080struct sched_group {
1081 struct sched_group *next; /* Must be a circular list */
1082 atomic_t ref;
1083
1084 unsigned int group_weight;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001085 struct sched_group_capacity *sgc;
Tim Chenafe06ef2016-11-22 12:23:53 -08001086 int asym_prefer_cpu; /* cpu of highest priority in group */
Li Zefan5e6521e2013-03-05 16:06:23 +08001087
1088 /*
1089 * The CPUs this group covers.
1090 *
1091 * NOTE: this field is variable length. (Allocated dynamically
1092 * by attaching extra space to the end of the structure,
1093 * depending on how many CPUs the kernel has booted up with)
1094 */
1095 unsigned long cpumask[0];
1096};
1097
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02001098static inline struct cpumask *sched_group_span(struct sched_group *sg)
Li Zefan5e6521e2013-03-05 16:06:23 +08001099{
1100 return to_cpumask(sg->cpumask);
1101}
1102
1103/*
Peter Zijlstrae5c14b12017-05-01 10:47:02 +02001104 * See build_balance_mask().
Li Zefan5e6521e2013-03-05 16:06:23 +08001105 */
Peter Zijlstrae5c14b12017-05-01 10:47:02 +02001106static inline struct cpumask *group_balance_mask(struct sched_group *sg)
Li Zefan5e6521e2013-03-05 16:06:23 +08001107{
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001108 return to_cpumask(sg->sgc->cpumask);
Li Zefan5e6521e2013-03-05 16:06:23 +08001109}
1110
1111/**
1112 * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
1113 * @group: The group whose first cpu is to be returned.
1114 */
1115static inline unsigned int group_first_cpu(struct sched_group *group)
1116{
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02001117 return cpumask_first(sched_group_span(group));
Li Zefan5e6521e2013-03-05 16:06:23 +08001118}
1119
Peter Zijlstrac1174872012-05-31 14:47:33 +02001120extern int group_balance_cpu(struct sched_group *sg);
1121
Steven Rostedt (Red Hat)3866e842016-02-22 16:26:51 -05001122#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
1123void register_sched_domain_sysctl(void);
Peter Zijlstrabbdacdf2017-08-10 17:10:26 +02001124void dirty_sched_domain_sysctl(int cpu);
Steven Rostedt (Red Hat)3866e842016-02-22 16:26:51 -05001125void unregister_sched_domain_sysctl(void);
1126#else
1127static inline void register_sched_domain_sysctl(void)
1128{
1129}
Peter Zijlstrabbdacdf2017-08-10 17:10:26 +02001130static inline void dirty_sched_domain_sysctl(int cpu)
1131{
1132}
Steven Rostedt (Red Hat)3866e842016-02-22 16:26:51 -05001133static inline void unregister_sched_domain_sysctl(void)
1134{
1135}
1136#endif
1137
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001138#else
1139
1140static inline void sched_ttwu_pending(void) { }
1141
Peter Zijlstra518cd622011-12-07 15:07:31 +01001142#endif /* CONFIG_SMP */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001143
Peter Zijlstra391e43d2011-11-15 17:14:39 +01001144#include "stats.h"
Ingo Molnar10514082017-02-01 18:42:41 +01001145#include "autogroup.h"
Peter Zijlstra029632f2011-10-25 10:00:11 +02001146
1147#ifdef CONFIG_CGROUP_SCHED
1148
1149/*
1150 * Return the group to which this tasks belongs.
1151 *
Tejun Heo8af01f52013-08-08 20:11:22 -04001152 * We cannot use task_css() and friends because the cgroup subsystem
1153 * changes that value before the cgroup_subsys::attach() method is called,
1154 * therefore we cannot pin it and might observe the wrong value.
Peter Zijlstra8323f262012-06-22 13:36:05 +02001155 *
1156 * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
1157 * core changes this before calling sched_move_task().
1158 *
1159 * Instead we use a 'copy' which is updated from sched_move_task() while
1160 * holding both task_struct::pi_lock and rq::lock.
Peter Zijlstra029632f2011-10-25 10:00:11 +02001161 */
1162static inline struct task_group *task_group(struct task_struct *p)
1163{
Peter Zijlstra8323f262012-06-22 13:36:05 +02001164 return p->sched_task_group;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001165}
1166
1167/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
1168static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
1169{
1170#if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
1171 struct task_group *tg = task_group(p);
1172#endif
1173
1174#ifdef CONFIG_FAIR_GROUP_SCHED
Byungchul Parkad936d82015-10-24 01:16:19 +09001175 set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001176 p->se.cfs_rq = tg->cfs_rq[cpu];
1177 p->se.parent = tg->se[cpu];
1178#endif
1179
1180#ifdef CONFIG_RT_GROUP_SCHED
1181 p->rt.rt_rq = tg->rt_rq[cpu];
1182 p->rt.parent = tg->rt_se[cpu];
1183#endif
1184}
1185
1186#else /* CONFIG_CGROUP_SCHED */
1187
1188static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
1189static inline struct task_group *task_group(struct task_struct *p)
1190{
1191 return NULL;
1192}
1193
1194#endif /* CONFIG_CGROUP_SCHED */
1195
1196static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1197{
1198 set_task_rq(p, cpu);
1199#ifdef CONFIG_SMP
1200 /*
1201 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1202 * successfuly executed on another CPU. We must ensure that updates of
1203 * per-task data have been completed by this moment.
1204 */
1205 smp_wmb();
Andy Lutomirskic65eacb2016-09-13 14:29:24 -07001206#ifdef CONFIG_THREAD_INFO_IN_TASK
1207 p->cpu = cpu;
1208#else
Peter Zijlstra029632f2011-10-25 10:00:11 +02001209 task_thread_info(p)->cpu = cpu;
Andy Lutomirskic65eacb2016-09-13 14:29:24 -07001210#endif
Peter Zijlstraac66f542013-10-07 11:29:16 +01001211 p->wake_cpu = cpu;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001212#endif
1213}
1214
1215/*
1216 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
1217 */
1218#ifdef CONFIG_SCHED_DEBUG
Ingo Molnarc5905af2012-02-24 08:31:31 +01001219# include <linux/static_key.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +02001220# define const_debug __read_mostly
1221#else
1222# define const_debug const
1223#endif
1224
1225extern const_debug unsigned int sysctl_sched_features;
1226
1227#define SCHED_FEAT(name, enabled) \
1228 __SCHED_FEAT_##name ,
1229
1230enum {
Peter Zijlstra391e43d2011-11-15 17:14:39 +01001231#include "features.h"
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001232 __SCHED_FEAT_NR,
Peter Zijlstra029632f2011-10-25 10:00:11 +02001233};
1234
1235#undef SCHED_FEAT
1236
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001237#if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL)
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001238#define SCHED_FEAT(name, enabled) \
Ingo Molnarc5905af2012-02-24 08:31:31 +01001239static __always_inline bool static_branch_##name(struct static_key *key) \
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001240{ \
Jason Baron6e76ea82014-07-02 15:52:41 +00001241 return static_key_##enabled(key); \
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001242}
1243
1244#include "features.h"
1245
1246#undef SCHED_FEAT
1247
Ingo Molnarc5905af2012-02-24 08:31:31 +01001248extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001249#define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
1250#else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001251#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001252#endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001253
Srikar Dronamraju2a595722015-08-11 21:54:21 +05301254extern struct static_key_false sched_numa_balancing;
Mel Gormancb251762016-02-05 09:08:36 +00001255extern struct static_key_false sched_schedstats;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001256
Peter Zijlstra029632f2011-10-25 10:00:11 +02001257static inline u64 global_rt_period(void)
1258{
1259 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
1260}
1261
1262static inline u64 global_rt_runtime(void)
1263{
1264 if (sysctl_sched_rt_runtime < 0)
1265 return RUNTIME_INF;
1266
1267 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
1268}
1269
Peter Zijlstra029632f2011-10-25 10:00:11 +02001270static inline int task_current(struct rq *rq, struct task_struct *p)
1271{
1272 return rq->curr == p;
1273}
1274
1275static inline int task_running(struct rq *rq, struct task_struct *p)
1276{
1277#ifdef CONFIG_SMP
1278 return p->on_cpu;
1279#else
1280 return task_current(rq, p);
1281#endif
1282}
1283
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001284static inline int task_on_rq_queued(struct task_struct *p)
1285{
1286 return p->on_rq == TASK_ON_RQ_QUEUED;
1287}
Peter Zijlstra029632f2011-10-25 10:00:11 +02001288
Kirill Tkhaicca26e82014-08-20 13:47:42 +04001289static inline int task_on_rq_migrating(struct task_struct *p)
1290{
1291 return p->on_rq == TASK_ON_RQ_MIGRATING;
1292}
1293
Peter Zijlstra029632f2011-10-25 10:00:11 +02001294#ifndef prepare_arch_switch
1295# define prepare_arch_switch(next) do { } while (0)
1296#endif
Catalin Marinas01f23e12011-11-27 21:43:10 +00001297#ifndef finish_arch_post_lock_switch
1298# define finish_arch_post_lock_switch() do { } while (0)
1299#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02001300
Peter Zijlstra029632f2011-10-25 10:00:11 +02001301static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
1302{
1303#ifdef CONFIG_SMP
1304 /*
1305 * We can optimise this out completely for !SMP, because the
1306 * SMP rebalancing from interrupt is the only thing that cares
1307 * here.
1308 */
1309 next->on_cpu = 1;
1310#endif
1311}
1312
1313static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
1314{
1315#ifdef CONFIG_SMP
1316 /*
1317 * After ->on_cpu is cleared, the task can be moved to a different CPU.
1318 * We must ensure this doesn't happen until the switch is completely
1319 * finished.
Peter Zijlstra95913d92015-09-29 14:45:09 +02001320 *
Peter Zijlstrab75a2252015-10-06 14:36:17 +02001321 * In particular, the load of prev->state in finish_task_switch() must
1322 * happen before this.
1323 *
Peter Zijlstra1f03e8d2016-04-04 10:57:12 +02001324 * Pairs with the smp_cond_load_acquire() in try_to_wake_up().
Peter Zijlstra029632f2011-10-25 10:00:11 +02001325 */
Peter Zijlstra95913d92015-09-29 14:45:09 +02001326 smp_store_release(&prev->on_cpu, 0);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001327#endif
1328#ifdef CONFIG_DEBUG_SPINLOCK
1329 /* this is a valid case when another task releases the spinlock */
1330 rq->lock.owner = current;
1331#endif
1332 /*
1333 * If we are tracking spinlock dependencies then we have to
1334 * fix up the runqueue lock - which gets 'carried over' from
1335 * prev into current:
1336 */
1337 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
1338
1339 raw_spin_unlock_irq(&rq->lock);
1340}
1341
Li Zefanb13095f2013-03-05 16:06:38 +08001342/*
1343 * wake flags
1344 */
1345#define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
1346#define WF_FORK 0x02 /* child wakeup after fork */
1347#define WF_MIGRATED 0x4 /* internal use, task got migrated */
1348
Peter Zijlstra029632f2011-10-25 10:00:11 +02001349/*
1350 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1351 * of tasks with abnormal "nice" values across CPUs the contribution that
1352 * each task makes to its run queue's load is weighted according to its
1353 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
1354 * scaled version of the new time slice allocation that they receive on time
1355 * slice expiry etc.
1356 */
1357
1358#define WEIGHT_IDLEPRIO 3
1359#define WMULT_IDLEPRIO 1431655765
1360
Andi Kleened82b8a2015-11-29 20:59:43 -08001361extern const int sched_prio_to_weight[40];
1362extern const u32 sched_prio_to_wmult[40];
Peter Zijlstra029632f2011-10-25 10:00:11 +02001363
Peter Zijlstraff77e462016-01-18 15:27:07 +01001364/*
1365 * {de,en}queue flags:
1366 *
1367 * DEQUEUE_SLEEP - task is no longer runnable
1368 * ENQUEUE_WAKEUP - task just became runnable
1369 *
1370 * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks
1371 * are in a known state which allows modification. Such pairs
1372 * should preserve as much state as possible.
1373 *
1374 * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location
1375 * in the runqueue.
1376 *
1377 * ENQUEUE_HEAD - place at front of runqueue (tail if not specified)
1378 * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline)
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02001379 * ENQUEUE_MIGRATED - the task was migrated during wakeup
Peter Zijlstraff77e462016-01-18 15:27:07 +01001380 *
1381 */
1382
1383#define DEQUEUE_SLEEP 0x01
1384#define DEQUEUE_SAVE 0x02 /* matches ENQUEUE_RESTORE */
1385#define DEQUEUE_MOVE 0x04 /* matches ENQUEUE_MOVE */
Peter Zijlstra0a67d1e2016-10-04 16:29:45 +02001386#define DEQUEUE_NOCLOCK 0x08 /* matches ENQUEUE_NOCLOCK */
Peter Zijlstraff77e462016-01-18 15:27:07 +01001387
Peter Zijlstra1de64442015-09-30 17:44:13 +02001388#define ENQUEUE_WAKEUP 0x01
Peter Zijlstraff77e462016-01-18 15:27:07 +01001389#define ENQUEUE_RESTORE 0x02
1390#define ENQUEUE_MOVE 0x04
Peter Zijlstra0a67d1e2016-10-04 16:29:45 +02001391#define ENQUEUE_NOCLOCK 0x08
Peter Zijlstraff77e462016-01-18 15:27:07 +01001392
Peter Zijlstra0a67d1e2016-10-04 16:29:45 +02001393#define ENQUEUE_HEAD 0x10
1394#define ENQUEUE_REPLENISH 0x20
Li Zefanc82ba9f2013-03-05 16:06:55 +08001395#ifdef CONFIG_SMP
Peter Zijlstra0a67d1e2016-10-04 16:29:45 +02001396#define ENQUEUE_MIGRATED 0x40
Li Zefanc82ba9f2013-03-05 16:06:55 +08001397#else
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02001398#define ENQUEUE_MIGRATED 0x00
Li Zefanc82ba9f2013-03-05 16:06:55 +08001399#endif
Li Zefanc82ba9f2013-03-05 16:06:55 +08001400
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001401#define RETRY_TASK ((void *)-1UL)
1402
Li Zefanc82ba9f2013-03-05 16:06:55 +08001403struct sched_class {
1404 const struct sched_class *next;
1405
1406 void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
1407 void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
1408 void (*yield_task) (struct rq *rq);
1409 bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt);
1410
1411 void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags);
1412
Peter Zijlstra606dba22012-02-11 06:05:00 +01001413 /*
1414 * It is the responsibility of the pick_next_task() method that will
1415 * return the next task to call put_prev_task() on the @prev task or
1416 * something equivalent.
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001417 *
1418 * May return RETRY_TASK when it finds a higher prio class has runnable
1419 * tasks.
Peter Zijlstra606dba22012-02-11 06:05:00 +01001420 */
1421 struct task_struct * (*pick_next_task) (struct rq *rq,
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001422 struct task_struct *prev,
Matt Flemingd8ac8972016-09-21 14:38:10 +01001423 struct rq_flags *rf);
Li Zefanc82ba9f2013-03-05 16:06:55 +08001424 void (*put_prev_task) (struct rq *rq, struct task_struct *p);
1425
1426#ifdef CONFIG_SMP
Peter Zijlstraac66f542013-10-07 11:29:16 +01001427 int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);
xiaofeng.yan5a4fd032015-09-23 14:55:59 +08001428 void (*migrate_task_rq)(struct task_struct *p);
Li Zefanc82ba9f2013-03-05 16:06:55 +08001429
Li Zefanc82ba9f2013-03-05 16:06:55 +08001430 void (*task_woken) (struct rq *this_rq, struct task_struct *task);
1431
1432 void (*set_cpus_allowed)(struct task_struct *p,
1433 const struct cpumask *newmask);
1434
1435 void (*rq_online)(struct rq *rq);
1436 void (*rq_offline)(struct rq *rq);
1437#endif
1438
1439 void (*set_curr_task) (struct rq *rq);
1440 void (*task_tick) (struct rq *rq, struct task_struct *p, int queued);
1441 void (*task_fork) (struct task_struct *p);
Dario Faggiolie6c390f2013-11-07 14:43:35 +01001442 void (*task_dead) (struct task_struct *p);
Li Zefanc82ba9f2013-03-05 16:06:55 +08001443
Kirill Tkhai67dfa1b2014-10-27 17:40:52 +03001444 /*
1445 * The switched_from() call is allowed to drop rq->lock, therefore we
1446 * cannot assume the switched_from/switched_to pair is serliazed by
1447 * rq->lock. They are however serialized by p->pi_lock.
1448 */
Li Zefanc82ba9f2013-03-05 16:06:55 +08001449 void (*switched_from) (struct rq *this_rq, struct task_struct *task);
1450 void (*switched_to) (struct rq *this_rq, struct task_struct *task);
1451 void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
1452 int oldprio);
1453
1454 unsigned int (*get_rr_interval) (struct rq *rq,
1455 struct task_struct *task);
1456
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01001457 void (*update_curr) (struct rq *rq);
1458
Vincent Guittotea86cb42016-06-17 13:38:55 +02001459#define TASK_SET_GROUP 0
1460#define TASK_MOVE_GROUP 1
1461
Li Zefanc82ba9f2013-03-05 16:06:55 +08001462#ifdef CONFIG_FAIR_GROUP_SCHED
Vincent Guittotea86cb42016-06-17 13:38:55 +02001463 void (*task_change_group) (struct task_struct *p, int type);
Li Zefanc82ba9f2013-03-05 16:06:55 +08001464#endif
1465};
Peter Zijlstra029632f2011-10-25 10:00:11 +02001466
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01001467static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
1468{
1469 prev->sched_class->put_prev_task(rq, prev);
1470}
1471
Peter Zijlstrab2bf6c32016-09-20 22:00:38 +02001472static inline void set_curr_task(struct rq *rq, struct task_struct *curr)
1473{
1474 curr->sched_class->set_curr_task(rq);
1475}
1476
Nicolas Pitref5832c12017-05-29 17:02:57 -04001477#ifdef CONFIG_SMP
Peter Zijlstra029632f2011-10-25 10:00:11 +02001478#define sched_class_highest (&stop_sched_class)
Nicolas Pitref5832c12017-05-29 17:02:57 -04001479#else
1480#define sched_class_highest (&dl_sched_class)
1481#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02001482#define for_each_class(class) \
1483 for (class = sched_class_highest; class; class = class->next)
1484
1485extern const struct sched_class stop_sched_class;
Dario Faggioliaab03e02013-11-28 11:14:43 +01001486extern const struct sched_class dl_sched_class;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001487extern const struct sched_class rt_sched_class;
1488extern const struct sched_class fair_sched_class;
1489extern const struct sched_class idle_sched_class;
1490
1491
1492#ifdef CONFIG_SMP
1493
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001494extern void update_group_capacity(struct sched_domain *sd, int cpu);
Li Zefanb7192032013-03-07 10:00:26 +08001495
Daniel Lezcano7caff662014-01-06 12:34:38 +01001496extern void trigger_load_balance(struct rq *rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001497
Peter Zijlstrac5b28032015-05-15 17:43:35 +02001498extern void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask);
1499
Peter Zijlstra029632f2011-10-25 10:00:11 +02001500#endif
1501
Daniel Lezcano442bf3a2014-09-04 11:32:09 -04001502#ifdef CONFIG_CPU_IDLE
1503static inline void idle_set_state(struct rq *rq,
1504 struct cpuidle_state *idle_state)
1505{
1506 rq->idle_state = idle_state;
1507}
1508
1509static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1510{
Peter Zijlstra9148a3a2016-09-20 22:34:51 +02001511 SCHED_WARN_ON(!rcu_read_lock_held());
Daniel Lezcano442bf3a2014-09-04 11:32:09 -04001512 return rq->idle_state;
1513}
1514#else
1515static inline void idle_set_state(struct rq *rq,
1516 struct cpuidle_state *idle_state)
1517{
1518}
1519
1520static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1521{
1522 return NULL;
1523}
1524#endif
1525
Steven Rostedt (VMware)8663eff2017-04-14 08:48:09 -04001526extern void schedule_idle(void);
1527
Peter Zijlstra029632f2011-10-25 10:00:11 +02001528extern void sysrq_sched_debug_show(void);
1529extern void sched_init_granularity(void);
1530extern void update_max_interval(void);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001531
1532extern void init_sched_dl_class(void);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001533extern void init_sched_rt_class(void);
1534extern void init_sched_fair_class(void);
1535
Vincent Guittot90593932017-05-17 11:50:45 +02001536extern void reweight_task(struct task_struct *p, int prio);
1537
Kirill Tkhai88751252014-06-29 00:03:57 +04001538extern void resched_curr(struct rq *rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001539extern void resched_cpu(int cpu);
1540
1541extern struct rt_bandwidth def_rt_bandwidth;
1542extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
1543
Dario Faggioli332ac172013-11-07 14:43:45 +01001544extern struct dl_bandwidth def_dl_bandwidth;
1545extern void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001546extern void init_dl_task_timer(struct sched_dl_entity *dl_se);
Luca Abeni209a0cb2017-05-18 22:13:29 +02001547extern void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se);
Luca Abeni4da3abc2017-05-18 22:13:32 +02001548extern void init_dl_rq_bw_ratio(struct dl_rq *dl_rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001549
Luca Abenic52f14d2017-05-18 22:13:31 +02001550#define BW_SHIFT 20
1551#define BW_UNIT (1 << BW_SHIFT)
Luca Abeni4da3abc2017-05-18 22:13:32 +02001552#define RATIO_SHIFT 8
Dario Faggioli332ac172013-11-07 14:43:45 +01001553unsigned long to_ratio(u64 period, u64 runtime);
1554
Yuyang Du540247f2015-07-15 08:04:39 +08001555extern void init_entity_runnable_average(struct sched_entity *se);
Yuyang Du2b8c41d2016-03-30 04:30:56 +08001556extern void post_init_entity_util_avg(struct sched_entity *se);
Alex Shia75cdaa2013-06-20 10:18:47 +08001557
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +02001558#ifdef CONFIG_NO_HZ_FULL
1559extern bool sched_can_stop_tick(struct rq *rq);
1560
1561/*
1562 * Tick may be needed by tasks in the runqueue depending on their policy and
1563 * requirements. If tick is needed, lets send the target an IPI to kick it out of
1564 * nohz mode if necessary.
1565 */
1566static inline void sched_update_tick_dependency(struct rq *rq)
1567{
1568 int cpu;
1569
1570 if (!tick_nohz_full_enabled())
1571 return;
1572
1573 cpu = cpu_of(rq);
1574
1575 if (!tick_nohz_full_cpu(cpu))
1576 return;
1577
1578 if (sched_can_stop_tick(rq))
1579 tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED);
1580 else
1581 tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED);
1582}
1583#else
1584static inline void sched_update_tick_dependency(struct rq *rq) { }
1585#endif
1586
Kirill Tkhai72465442014-05-09 03:00:14 +04001587static inline void add_nr_running(struct rq *rq, unsigned count)
Peter Zijlstra029632f2011-10-25 10:00:11 +02001588{
Kirill Tkhai72465442014-05-09 03:00:14 +04001589 unsigned prev_nr = rq->nr_running;
1590
1591 rq->nr_running = prev_nr + count;
Frederic Weisbecker9f3660c2013-04-20 14:35:09 +02001592
Kirill Tkhai72465442014-05-09 03:00:14 +04001593 if (prev_nr < 2 && rq->nr_running >= 2) {
Tim Chen4486edd2014-06-23 12:16:49 -07001594#ifdef CONFIG_SMP
1595 if (!rq->rd->overload)
1596 rq->rd->overload = true;
1597#endif
Tim Chen4486edd2014-06-23 12:16:49 -07001598 }
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +02001599
1600 sched_update_tick_dependency(rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001601}
1602
Kirill Tkhai72465442014-05-09 03:00:14 +04001603static inline void sub_nr_running(struct rq *rq, unsigned count)
Peter Zijlstra029632f2011-10-25 10:00:11 +02001604{
Kirill Tkhai72465442014-05-09 03:00:14 +04001605 rq->nr_running -= count;
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +02001606 /* Check if we still need preemption */
1607 sched_update_tick_dependency(rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001608}
1609
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02001610static inline void rq_last_tick_reset(struct rq *rq)
1611{
1612#ifdef CONFIG_NO_HZ_FULL
1613 rq->last_sched_tick = jiffies;
1614#endif
1615}
1616
Peter Zijlstra029632f2011-10-25 10:00:11 +02001617extern void update_rq_clock(struct rq *rq);
1618
1619extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
1620extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
1621
1622extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
1623
1624extern const_debug unsigned int sysctl_sched_time_avg;
1625extern const_debug unsigned int sysctl_sched_nr_migrate;
1626extern const_debug unsigned int sysctl_sched_migration_cost;
1627
1628static inline u64 sched_avg_period(void)
1629{
1630 return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
1631}
1632
Peter Zijlstra029632f2011-10-25 10:00:11 +02001633#ifdef CONFIG_SCHED_HRTICK
1634
1635/*
1636 * Use hrtick when:
1637 * - enabled by features
1638 * - hrtimer is actually high res
1639 */
1640static inline int hrtick_enabled(struct rq *rq)
1641{
1642 if (!sched_feat(HRTICK))
1643 return 0;
1644 if (!cpu_active(cpu_of(rq)))
1645 return 0;
1646 return hrtimer_is_hres_active(&rq->hrtick_timer);
1647}
1648
1649void hrtick_start(struct rq *rq, u64 delay);
1650
Mike Galbraithb39e66e2011-11-22 15:20:07 +01001651#else
1652
1653static inline int hrtick_enabled(struct rq *rq)
1654{
1655 return 0;
1656}
1657
Peter Zijlstra029632f2011-10-25 10:00:11 +02001658#endif /* CONFIG_SCHED_HRTICK */
1659
1660#ifdef CONFIG_SMP
1661extern void sched_avg_update(struct rq *rq);
Peter Zijlstradfbca412015-03-23 14:19:05 +01001662
1663#ifndef arch_scale_freq_capacity
1664static __always_inline
1665unsigned long arch_scale_freq_capacity(struct sched_domain *sd, int cpu)
1666{
1667 return SCHED_CAPACITY_SCALE;
1668}
1669#endif
Vincent Guittotb5b48602015-02-27 16:54:08 +01001670
Morten Rasmussen8cd56012015-08-14 17:23:10 +01001671#ifndef arch_scale_cpu_capacity
1672static __always_inline
1673unsigned long arch_scale_cpu_capacity(struct sched_domain *sd, int cpu)
1674{
Dietmar Eggemanne3279a22015-08-15 00:04:41 +01001675 if (sd && (sd->flags & SD_SHARE_CPUCAPACITY) && (sd->span_weight > 1))
Morten Rasmussen8cd56012015-08-14 17:23:10 +01001676 return sd->smt_gain / sd->span_weight;
1677
1678 return SCHED_CAPACITY_SCALE;
1679}
1680#endif
1681
Peter Zijlstra029632f2011-10-25 10:00:11 +02001682static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1683{
Vincent Guittotb5b48602015-02-27 16:54:08 +01001684 rq->rt_avg += rt_delta * arch_scale_freq_capacity(NULL, cpu_of(rq));
Peter Zijlstra029632f2011-10-25 10:00:11 +02001685 sched_avg_update(rq);
1686}
1687#else
1688static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { }
1689static inline void sched_avg_update(struct rq *rq) { }
1690#endif
1691
Peter Zijlstraeb580752015-07-31 21:28:18 +02001692struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
Peter Zijlstra3e71a462016-04-28 16:16:33 +02001693 __acquires(rq->lock);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001694
Peter Zijlstraeb580752015-07-31 21:28:18 +02001695struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001696 __acquires(p->pi_lock)
Peter Zijlstra3e71a462016-04-28 16:16:33 +02001697 __acquires(rq->lock);
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001698
Peter Zijlstraeb580752015-07-31 21:28:18 +02001699static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf)
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001700 __releases(rq->lock)
1701{
Matt Flemingd8ac8972016-09-21 14:38:10 +01001702 rq_unpin_lock(rq, rf);
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001703 raw_spin_unlock(&rq->lock);
1704}
1705
1706static inline void
Peter Zijlstraeb580752015-07-31 21:28:18 +02001707task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001708 __releases(rq->lock)
1709 __releases(p->pi_lock)
1710{
Matt Flemingd8ac8972016-09-21 14:38:10 +01001711 rq_unpin_lock(rq, rf);
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001712 raw_spin_unlock(&rq->lock);
Peter Zijlstraeb580752015-07-31 21:28:18 +02001713 raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001714}
1715
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001716static inline void
1717rq_lock_irqsave(struct rq *rq, struct rq_flags *rf)
1718 __acquires(rq->lock)
1719{
1720 raw_spin_lock_irqsave(&rq->lock, rf->flags);
1721 rq_pin_lock(rq, rf);
1722}
1723
1724static inline void
1725rq_lock_irq(struct rq *rq, struct rq_flags *rf)
1726 __acquires(rq->lock)
1727{
1728 raw_spin_lock_irq(&rq->lock);
1729 rq_pin_lock(rq, rf);
1730}
1731
1732static inline void
1733rq_lock(struct rq *rq, struct rq_flags *rf)
1734 __acquires(rq->lock)
1735{
1736 raw_spin_lock(&rq->lock);
1737 rq_pin_lock(rq, rf);
1738}
1739
1740static inline void
1741rq_relock(struct rq *rq, struct rq_flags *rf)
1742 __acquires(rq->lock)
1743{
1744 raw_spin_lock(&rq->lock);
1745 rq_repin_lock(rq, rf);
1746}
1747
1748static inline void
1749rq_unlock_irqrestore(struct rq *rq, struct rq_flags *rf)
1750 __releases(rq->lock)
1751{
1752 rq_unpin_lock(rq, rf);
1753 raw_spin_unlock_irqrestore(&rq->lock, rf->flags);
1754}
1755
1756static inline void
1757rq_unlock_irq(struct rq *rq, struct rq_flags *rf)
1758 __releases(rq->lock)
1759{
1760 rq_unpin_lock(rq, rf);
1761 raw_spin_unlock_irq(&rq->lock);
1762}
1763
1764static inline void
1765rq_unlock(struct rq *rq, struct rq_flags *rf)
1766 __releases(rq->lock)
1767{
1768 rq_unpin_lock(rq, rf);
1769 raw_spin_unlock(&rq->lock);
1770}
1771
Peter Zijlstra029632f2011-10-25 10:00:11 +02001772#ifdef CONFIG_SMP
1773#ifdef CONFIG_PREEMPT
1774
1775static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
1776
1777/*
1778 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1779 * way at the expense of forcing extra atomic operations in all
1780 * invocations. This assures that the double_lock is acquired using the
1781 * same underlying policy as the spinlock_t on this architecture, which
1782 * reduces latency compared to the unfair variant below. However, it
1783 * also adds more overhead and therefore may reduce throughput.
1784 */
1785static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1786 __releases(this_rq->lock)
1787 __acquires(busiest->lock)
1788 __acquires(this_rq->lock)
1789{
1790 raw_spin_unlock(&this_rq->lock);
1791 double_rq_lock(this_rq, busiest);
1792
1793 return 1;
1794}
1795
1796#else
1797/*
1798 * Unfair double_lock_balance: Optimizes throughput at the expense of
1799 * latency by eliminating extra atomic operations when the locks are
1800 * already in proper order on entry. This favors lower cpu-ids and will
1801 * grant the double lock to lower cpus over higher ids under contention,
1802 * regardless of entry order into the function.
1803 */
1804static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1805 __releases(this_rq->lock)
1806 __acquires(busiest->lock)
1807 __acquires(this_rq->lock)
1808{
1809 int ret = 0;
1810
1811 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
1812 if (busiest < this_rq) {
1813 raw_spin_unlock(&this_rq->lock);
1814 raw_spin_lock(&busiest->lock);
1815 raw_spin_lock_nested(&this_rq->lock,
1816 SINGLE_DEPTH_NESTING);
1817 ret = 1;
1818 } else
1819 raw_spin_lock_nested(&busiest->lock,
1820 SINGLE_DEPTH_NESTING);
1821 }
1822 return ret;
1823}
1824
1825#endif /* CONFIG_PREEMPT */
1826
1827/*
1828 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1829 */
1830static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1831{
1832 if (unlikely(!irqs_disabled())) {
1833 /* printk() doesn't work good under rq->lock */
1834 raw_spin_unlock(&this_rq->lock);
1835 BUG_ON(1);
1836 }
1837
1838 return _double_lock_balance(this_rq, busiest);
1839}
1840
1841static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1842 __releases(busiest->lock)
1843{
1844 raw_spin_unlock(&busiest->lock);
1845 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1846}
1847
Peter Zijlstra74602312013-10-10 20:17:22 +02001848static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
1849{
1850 if (l1 > l2)
1851 swap(l1, l2);
1852
1853 spin_lock(l1);
1854 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1855}
1856
Mike Galbraith60e69ee2014-04-07 10:55:15 +02001857static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2)
1858{
1859 if (l1 > l2)
1860 swap(l1, l2);
1861
1862 spin_lock_irq(l1);
1863 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1864}
1865
Peter Zijlstra74602312013-10-10 20:17:22 +02001866static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
1867{
1868 if (l1 > l2)
1869 swap(l1, l2);
1870
1871 raw_spin_lock(l1);
1872 raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1873}
1874
Peter Zijlstra029632f2011-10-25 10:00:11 +02001875/*
1876 * double_rq_lock - safely lock two runqueues
1877 *
1878 * Note this does not disable interrupts like task_rq_lock,
1879 * you need to do so manually before calling.
1880 */
1881static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1882 __acquires(rq1->lock)
1883 __acquires(rq2->lock)
1884{
1885 BUG_ON(!irqs_disabled());
1886 if (rq1 == rq2) {
1887 raw_spin_lock(&rq1->lock);
1888 __acquire(rq2->lock); /* Fake it out ;) */
1889 } else {
1890 if (rq1 < rq2) {
1891 raw_spin_lock(&rq1->lock);
1892 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
1893 } else {
1894 raw_spin_lock(&rq2->lock);
1895 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1896 }
1897 }
1898}
1899
1900/*
1901 * double_rq_unlock - safely unlock two runqueues
1902 *
1903 * Note this does not restore interrupts like task_rq_unlock,
1904 * you need to do so manually after calling.
1905 */
1906static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1907 __releases(rq1->lock)
1908 __releases(rq2->lock)
1909{
1910 raw_spin_unlock(&rq1->lock);
1911 if (rq1 != rq2)
1912 raw_spin_unlock(&rq2->lock);
1913 else
1914 __release(rq2->lock);
1915}
1916
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001917extern void set_rq_online (struct rq *rq);
1918extern void set_rq_offline(struct rq *rq);
1919extern bool sched_smp_initialized;
1920
Peter Zijlstra029632f2011-10-25 10:00:11 +02001921#else /* CONFIG_SMP */
1922
1923/*
1924 * double_rq_lock - safely lock two runqueues
1925 *
1926 * Note this does not disable interrupts like task_rq_lock,
1927 * you need to do so manually before calling.
1928 */
1929static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1930 __acquires(rq1->lock)
1931 __acquires(rq2->lock)
1932{
1933 BUG_ON(!irqs_disabled());
1934 BUG_ON(rq1 != rq2);
1935 raw_spin_lock(&rq1->lock);
1936 __acquire(rq2->lock); /* Fake it out ;) */
1937}
1938
1939/*
1940 * double_rq_unlock - safely unlock two runqueues
1941 *
1942 * Note this does not restore interrupts like task_rq_unlock,
1943 * you need to do so manually after calling.
1944 */
1945static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1946 __releases(rq1->lock)
1947 __releases(rq2->lock)
1948{
1949 BUG_ON(rq1 != rq2);
1950 raw_spin_unlock(&rq1->lock);
1951 __release(rq2->lock);
1952}
1953
1954#endif
1955
1956extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
1957extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
Srikar Dronamraju6b55c962015-06-25 22:51:41 +05301958
1959#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra9469eb02017-09-07 17:03:53 +02001960extern bool sched_debug_enabled;
1961
Peter Zijlstra029632f2011-10-25 10:00:11 +02001962extern void print_cfs_stats(struct seq_file *m, int cpu);
1963extern void print_rt_stats(struct seq_file *m, int cpu);
Wanpeng Liacb32132014-10-31 06:39:33 +08001964extern void print_dl_stats(struct seq_file *m, int cpu);
Srikar Dronamraju6b55c962015-06-25 22:51:41 +05301965extern void
1966print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq);
Srikar Dronamraju397f2372015-06-25 22:51:43 +05301967#ifdef CONFIG_NUMA_BALANCING
1968extern void
1969show_numa_stats(struct task_struct *p, struct seq_file *m);
1970extern void
1971print_numa_stats(struct seq_file *m, int node, unsigned long tsf,
1972 unsigned long tpf, unsigned long gsf, unsigned long gpf);
1973#endif /* CONFIG_NUMA_BALANCING */
1974#endif /* CONFIG_SCHED_DEBUG */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001975
1976extern void init_cfs_rq(struct cfs_rq *cfs_rq);
Abel Vesa07c54f72015-03-03 13:50:27 +02001977extern void init_rt_rq(struct rt_rq *rt_rq);
1978extern void init_dl_rq(struct dl_rq *dl_rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001979
Ben Segall1ee14e62013-10-16 11:16:12 -07001980extern void cfs_bandwidth_usage_inc(void);
1981extern void cfs_bandwidth_usage_dec(void);
Suresh Siddha1c792db2011-12-01 17:07:32 -08001982
Frederic Weisbecker3451d022011-08-10 23:21:01 +02001983#ifdef CONFIG_NO_HZ_COMMON
Suresh Siddha1c792db2011-12-01 17:07:32 -08001984enum rq_nohz_flag_bits {
1985 NOHZ_TICK_STOPPED,
1986 NOHZ_BALANCE_KICK,
1987};
1988
1989#define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
Thomas Gleixner20a5c8c2016-03-10 12:54:20 +01001990
1991extern void nohz_balance_exit_idle(unsigned int cpu);
1992#else
1993static inline void nohz_balance_exit_idle(unsigned int cpu) { }
Suresh Siddha1c792db2011-12-01 17:07:32 -08001994#endif
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02001995
Luca Abenidaec5792017-05-18 22:13:36 +02001996
1997#ifdef CONFIG_SMP
1998static inline
1999void __dl_update(struct dl_bw *dl_b, s64 bw)
2000{
2001 struct root_domain *rd = container_of(dl_b, struct root_domain, dl_bw);
2002 int i;
2003
2004 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
2005 "sched RCU must be held");
2006 for_each_cpu_and(i, rd->span, cpu_active_mask) {
2007 struct rq *rq = cpu_rq(i);
2008
2009 rq->dl.extra_bw += bw;
2010 }
2011}
2012#else
2013static inline
2014void __dl_update(struct dl_bw *dl_b, s64 bw)
2015{
2016 struct dl_rq *dl = container_of(dl_b, struct dl_rq, dl_bw);
2017
2018 dl->extra_bw += bw;
2019}
2020#endif
2021
2022
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002023#ifdef CONFIG_IRQ_TIME_ACCOUNTING
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002024struct irqtime {
Frederic Weisbecker25e2d8c2017-04-25 16:10:48 +02002025 u64 total;
Frederic Weisbeckera499a5a2017-01-31 04:09:32 +01002026 u64 tick_delta;
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002027 u64 irq_start_time;
2028 struct u64_stats_sync sync;
2029};
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002030
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002031DECLARE_PER_CPU(struct irqtime, cpu_irqtime);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002032
Frederic Weisbecker25e2d8c2017-04-25 16:10:48 +02002033/*
2034 * Returns the irqtime minus the softirq time computed by ksoftirqd.
2035 * Otherwise ksoftirqd's sum_exec_runtime is substracted its own runtime
2036 * and never move forward.
2037 */
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002038static inline u64 irq_time_read(int cpu)
2039{
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002040 struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu);
2041 unsigned int seq;
2042 u64 total;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002043
2044 do {
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002045 seq = __u64_stats_fetch_begin(&irqtime->sync);
Frederic Weisbecker25e2d8c2017-04-25 16:10:48 +02002046 total = irqtime->total;
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002047 } while (__u64_stats_fetch_retry(&irqtime->sync, seq));
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002048
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002049 return total;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002050}
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002051#endif /* CONFIG_IRQ_TIME_ACCOUNTING */
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002052
2053#ifdef CONFIG_CPU_FREQ
2054DECLARE_PER_CPU(struct update_util_data *, cpufreq_update_util_data);
2055
2056/**
2057 * cpufreq_update_util - Take a note about CPU utilization changes.
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02002058 * @rq: Runqueue to carry out the update for.
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02002059 * @flags: Update reason flags.
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002060 *
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02002061 * This function is called by the scheduler on the CPU whose utilization is
2062 * being updated.
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002063 *
2064 * It can only be called from RCU-sched read-side critical sections.
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002065 *
2066 * The way cpufreq is currently arranged requires it to evaluate the CPU
2067 * performance state (frequency/voltage) on a regular basis to prevent it from
2068 * being stuck in a completely inadequate performance level for too long.
2069 * That is not guaranteed to happen if the updates are only triggered from CFS,
2070 * though, because they may not be coming in if RT or deadline tasks are active
2071 * all the time (or there are RT and DL tasks only).
2072 *
2073 * As a workaround for that issue, this function is called by the RT and DL
2074 * sched classes to trigger extra cpufreq updates to prevent it from stalling,
2075 * but that really is a band-aid. Going forward it should be replaced with
2076 * solutions targeted more specifically at RT and DL tasks.
2077 */
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02002078static inline void cpufreq_update_util(struct rq *rq, unsigned int flags)
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002079{
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02002080 struct update_util_data *data;
2081
Viresh Kumar674e7542017-07-28 12:16:38 +05302082 data = rcu_dereference_sched(*per_cpu_ptr(&cpufreq_update_util_data,
2083 cpu_of(rq)));
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02002084 if (data)
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02002085 data->func(data, rq_clock(rq), flags);
2086}
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002087#else
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02002088static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {}
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002089#endif /* CONFIG_CPU_FREQ */
Linus Torvaldsbe53f582016-03-24 09:42:50 -07002090
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +02002091#ifdef arch_scale_freq_capacity
2092#ifndef arch_scale_freq_invariant
2093#define arch_scale_freq_invariant() (true)
2094#endif
2095#else /* arch_scale_freq_capacity */
2096#define arch_scale_freq_invariant() (false)
2097#endif