blob: 14db76cd496ffb68641c3cc68a9be72205524e1b [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;
Yuyang Du9d89c252015-07-15 08:04:37 +0800448#ifdef CONFIG_FAIR_GROUP_SCHED
449 unsigned long tg_load_avg_contrib;
Vincent Guittot09a43ac2016-11-08 10:53:45 +0100450 unsigned long propagate_avg;
Yuyang Du9d89c252015-07-15 08:04:37 +0800451#endif
452 atomic_long_t removed_load_avg, removed_util_avg;
453#ifndef CONFIG_64BIT
454 u64 load_last_update_time_copy;
455#endif
Alex Shi141965c2013-06-26 13:05:39 +0800456
Paul Turnerc566e8e2012-10-04 13:18:30 +0200457#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Turner82958362012-10-04 13:18:31 +0200458 /*
459 * h_load = weight * f(tg)
460 *
461 * Where f(tg) is the recursive weight fraction assigned to
462 * this group.
463 */
464 unsigned long h_load;
Vladimir Davydov68520792013-07-15 17:49:19 +0400465 u64 last_h_load_update;
466 struct sched_entity *h_load_next;
467#endif /* CONFIG_FAIR_GROUP_SCHED */
Paul Turner82958362012-10-04 13:18:31 +0200468#endif /* CONFIG_SMP */
469
Peter Zijlstra029632f2011-10-25 10:00:11 +0200470#ifdef CONFIG_FAIR_GROUP_SCHED
471 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
472
473 /*
474 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
475 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
476 * (like users, containers etc.)
477 *
478 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
479 * list is used during load balance.
480 */
481 int on_list;
482 struct list_head leaf_cfs_rq_list;
483 struct task_group *tg; /* group that "owns" this runqueue */
484
Peter Zijlstra029632f2011-10-25 10:00:11 +0200485#ifdef CONFIG_CFS_BANDWIDTH
486 int runtime_enabled;
487 u64 runtime_expires;
488 s64 runtime_remaining;
489
Paul Turnerf1b17282012-10-04 13:18:31 +0200490 u64 throttled_clock, throttled_clock_task;
491 u64 throttled_clock_task_time;
Peter Zijlstra55e16d32016-06-22 15:14:26 +0200492 int throttled, throttle_count;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200493 struct list_head throttled_list;
494#endif /* CONFIG_CFS_BANDWIDTH */
495#endif /* CONFIG_FAIR_GROUP_SCHED */
496};
497
498static inline int rt_bandwidth_enabled(void)
499{
500 return sysctl_sched_rt_runtime >= 0;
501}
502
Steven Rostedtb6366f02015-03-18 14:49:46 -0400503/* RT IPI pull logic requires IRQ_WORK */
504#ifdef CONFIG_IRQ_WORK
505# define HAVE_RT_PUSH_IPI
506#endif
507
Peter Zijlstra029632f2011-10-25 10:00:11 +0200508/* Real-Time classes' related field in a runqueue: */
509struct rt_rq {
510 struct rt_prio_array active;
Peter Zijlstrac82513e2012-04-26 13:12:27 +0200511 unsigned int rt_nr_running;
Frederic Weisbecker01d36d02015-11-04 18:17:10 +0100512 unsigned int rr_nr_running;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200513#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
514 struct {
515 int curr; /* highest queued rt task prio */
516#ifdef CONFIG_SMP
517 int next; /* next highest */
518#endif
519 } highest_prio;
520#endif
521#ifdef CONFIG_SMP
522 unsigned long rt_nr_migratory;
523 unsigned long rt_nr_total;
524 int overloaded;
525 struct plist_head pushable_tasks;
Steven Rostedtb6366f02015-03-18 14:49:46 -0400526#ifdef HAVE_RT_PUSH_IPI
527 int push_flags;
528 int push_cpu;
529 struct irq_work push_work;
530 raw_spinlock_t push_lock;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200531#endif
Steven Rostedtb6366f02015-03-18 14:49:46 -0400532#endif /* CONFIG_SMP */
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400533 int rt_queued;
534
Peter Zijlstra029632f2011-10-25 10:00:11 +0200535 int rt_throttled;
536 u64 rt_time;
537 u64 rt_runtime;
538 /* Nests inside the rq lock: */
539 raw_spinlock_t rt_runtime_lock;
540
541#ifdef CONFIG_RT_GROUP_SCHED
542 unsigned long rt_nr_boosted;
543
544 struct rq *rq;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200545 struct task_group *tg;
546#endif
547};
548
Dario Faggioliaab03e02013-11-28 11:14:43 +0100549/* Deadline class' related fields in a runqueue */
550struct dl_rq {
551 /* runqueue is an rbtree, ordered by deadline */
Davidlohr Bueso21615732017-09-08 16:14:58 -0700552 struct rb_root_cached root;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100553
554 unsigned long dl_nr_running;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100555
556#ifdef CONFIG_SMP
557 /*
558 * Deadline values of the currently executing and the
559 * earliest ready task on this rq. Caching these facilitates
560 * the decision wether or not a ready but not running task
561 * should migrate somewhere else.
562 */
563 struct {
564 u64 curr;
565 u64 next;
566 } earliest_dl;
567
568 unsigned long dl_nr_migratory;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100569 int overloaded;
570
571 /*
572 * Tasks on this rq that can be pushed away. They are kept in
573 * an rb-tree, ordered by tasks' deadlines, with caching
574 * of the leftmost (earliest deadline) element.
575 */
Davidlohr Bueso21615732017-09-08 16:14:58 -0700576 struct rb_root_cached pushable_dl_tasks_root;
Dario Faggioli332ac172013-11-07 14:43:45 +0100577#else
578 struct dl_bw dl_bw;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100579#endif
Luca Abenie36d8672017-05-18 22:13:28 +0200580 /*
581 * "Active utilization" for this runqueue: increased when a
582 * task wakes up (becomes TASK_RUNNING) and decreased when a
583 * task blocks
584 */
585 u64 running_bw;
Luca Abeni4da3abc2017-05-18 22:13:32 +0200586
587 /*
Luca Abeni8fd27232017-05-18 22:13:34 +0200588 * Utilization of the tasks "assigned" to this runqueue (including
589 * the tasks that are in runqueue and the tasks that executed on this
590 * CPU and blocked). Increased when a task moves to this runqueue, and
591 * decreased when the task moves away (migrates, changes scheduling
592 * policy, or terminates).
593 * This is needed to compute the "inactive utilization" for the
594 * runqueue (inactive utilization = this_bw - running_bw).
595 */
596 u64 this_bw;
Luca Abenidaec5792017-05-18 22:13:36 +0200597 u64 extra_bw;
Luca Abeni8fd27232017-05-18 22:13:34 +0200598
599 /*
Luca Abeni4da3abc2017-05-18 22:13:32 +0200600 * Inverse of the fraction of CPU utilization that can be reclaimed
601 * by the GRUB algorithm.
602 */
603 u64 bw_ratio;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100604};
605
Peter Zijlstra029632f2011-10-25 10:00:11 +0200606#ifdef CONFIG_SMP
607
Tim Chenafe06ef2016-11-22 12:23:53 -0800608static inline bool sched_asym_prefer(int a, int b)
609{
610 return arch_asym_cpu_priority(a) > arch_asym_cpu_priority(b);
611}
612
Peter Zijlstra029632f2011-10-25 10:00:11 +0200613/*
614 * We add the notion of a root-domain which will be used to define per-domain
615 * variables. Each exclusive cpuset essentially defines an island domain by
616 * fully partitioning the member cpus from any other cpuset. Whenever a new
617 * exclusive cpuset is created, we also create and attach a new root-domain
618 * object.
619 *
620 */
621struct root_domain {
622 atomic_t refcount;
623 atomic_t rto_count;
624 struct rcu_head rcu;
625 cpumask_var_t span;
626 cpumask_var_t online;
627
Tim Chen4486edd2014-06-23 12:16:49 -0700628 /* Indicate more than one runnable task for any CPU */
629 bool overload;
630
Peter Zijlstra029632f2011-10-25 10:00:11 +0200631 /*
Juri Lelli1baca4c2013-11-07 14:43:38 +0100632 * The bit corresponding to a CPU gets set here if such CPU has more
633 * than one runnable -deadline task (as it is below for RT tasks).
634 */
635 cpumask_var_t dlo_mask;
636 atomic_t dlo_count;
Dario Faggioli332ac172013-11-07 14:43:45 +0100637 struct dl_bw dl_bw;
Juri Lelli6bfd6d72013-11-07 14:43:47 +0100638 struct cpudl cpudl;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100639
640 /*
Peter Zijlstra029632f2011-10-25 10:00:11 +0200641 * The "RT overload" flag: it gets set if a CPU has more than
642 * one runnable RT task.
643 */
644 cpumask_var_t rto_mask;
645 struct cpupri cpupri;
Dietmar Eggemanncd92bfd2016-08-01 19:53:35 +0100646
647 unsigned long max_cpu_capacity;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200648};
649
650extern struct root_domain def_root_domain;
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100651extern struct mutex sched_domains_mutex;
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100652
653extern void init_defrootdomain(void);
Peter Zijlstra8d5dc512017-04-25 15:29:40 +0200654extern int sched_init_domains(const struct cpumask *cpu_map);
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100655extern void rq_attach_root(struct rq *rq, struct root_domain *rd);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200656
657#endif /* CONFIG_SMP */
658
659/*
660 * This is the main, per-CPU runqueue data structure.
661 *
662 * Locking rule: those places that want to lock multiple runqueues
663 * (such as the load balancing or the thread migration code), lock
664 * acquire operations must be ordered by ascending &runqueue.
665 */
666struct rq {
667 /* runqueue lock: */
668 raw_spinlock_t lock;
669
670 /*
671 * nr_running and cpu_load should be in the same cacheline because
672 * remote CPUs use both these fields when doing load calculation.
673 */
Peter Zijlstrac82513e2012-04-26 13:12:27 +0200674 unsigned int nr_running;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +0100675#ifdef CONFIG_NUMA_BALANCING
676 unsigned int nr_numa_running;
677 unsigned int nr_preferred_running;
678#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200679 #define CPU_LOAD_IDX_MAX 5
680 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200681#ifdef CONFIG_NO_HZ_COMMON
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +0200682#ifdef CONFIG_SMP
683 unsigned long last_load_update_tick;
684#endif /* CONFIG_SMP */
Suresh Siddha1c792db2011-12-01 17:07:32 -0800685 unsigned long nohz_flags;
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +0200686#endif /* CONFIG_NO_HZ_COMMON */
Frederic Weisbecker265f22a2013-05-03 03:39:05 +0200687#ifdef CONFIG_NO_HZ_FULL
688 unsigned long last_sched_tick;
689#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200690 /* capture load from *all* tasks on this cpu: */
691 struct load_weight load;
692 unsigned long nr_load_updates;
693 u64 nr_switches;
694
695 struct cfs_rq cfs;
696 struct rt_rq rt;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100697 struct dl_rq dl;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200698
699#ifdef CONFIG_FAIR_GROUP_SCHED
700 /* list of leaf cfs_rq on this cpu: */
701 struct list_head leaf_cfs_rq_list;
Vincent Guittot9c2791f2016-11-08 10:53:43 +0100702 struct list_head *tmp_alone_branch;
Peter Zijlstraa35b6462012-08-08 21:46:40 +0200703#endif /* CONFIG_FAIR_GROUP_SCHED */
704
Peter Zijlstra029632f2011-10-25 10:00:11 +0200705 /*
706 * This is part of a global counter where only the total sum
707 * over all CPUs matters. A task can increase this counter on
708 * one CPU and if it got migrated afterwards it may decrease
709 * it on another CPU. Always updated under the runqueue lock:
710 */
711 unsigned long nr_uninterruptible;
712
713 struct task_struct *curr, *idle, *stop;
714 unsigned long next_balance;
715 struct mm_struct *prev_mm;
716
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100717 unsigned int clock_update_flags;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200718 u64 clock;
719 u64 clock_task;
720
721 atomic_t nr_iowait;
722
723#ifdef CONFIG_SMP
724 struct root_domain *rd;
725 struct sched_domain *sd;
726
Nicolas Pitreced549f2014-05-26 18:19:38 -0400727 unsigned long cpu_capacity;
Vincent Guittotca6d75e2015-02-27 16:54:09 +0100728 unsigned long cpu_capacity_orig;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200729
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200730 struct callback_head *balance_callback;
731
Peter Zijlstra029632f2011-10-25 10:00:11 +0200732 unsigned char idle_balance;
733 /* For active balancing */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200734 int active_balance;
735 int push_cpu;
736 struct cpu_stop_work active_balance_work;
737 /* cpu of this runqueue: */
738 int cpu;
739 int online;
740
Peter Zijlstra367456c2012-02-20 21:49:09 +0100741 struct list_head cfs_tasks;
742
Peter Zijlstra029632f2011-10-25 10:00:11 +0200743 u64 rt_avg;
744 u64 age_stamp;
745 u64 idle_stamp;
746 u64 avg_idle;
Jason Low9bd721c2013-09-13 11:26:52 -0700747
748 /* This is used to determine avg_idle's max value */
749 u64 max_idle_balance_cost;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200750#endif
751
752#ifdef CONFIG_IRQ_TIME_ACCOUNTING
753 u64 prev_irq_time;
754#endif
755#ifdef CONFIG_PARAVIRT
756 u64 prev_steal_time;
757#endif
758#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
759 u64 prev_steal_time_rq;
760#endif
761
762 /* calc_load related fields */
763 unsigned long calc_load_update;
764 long calc_load_active;
765
766#ifdef CONFIG_SCHED_HRTICK
767#ifdef CONFIG_SMP
768 int hrtick_csd_pending;
Ying Huang966a9672017-08-08 12:30:00 +0800769 call_single_data_t hrtick_csd;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200770#endif
771 struct hrtimer hrtick_timer;
772#endif
773
774#ifdef CONFIG_SCHEDSTATS
775 /* latency stats */
776 struct sched_info rq_sched_info;
777 unsigned long long rq_cpu_time;
778 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
779
780 /* sys_sched_yield() stats */
781 unsigned int yld_count;
782
783 /* schedule() stats */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200784 unsigned int sched_count;
785 unsigned int sched_goidle;
786
787 /* try_to_wake_up() stats */
788 unsigned int ttwu_count;
789 unsigned int ttwu_local;
790#endif
791
792#ifdef CONFIG_SMP
793 struct llist_head wake_list;
794#endif
Daniel Lezcano442bf3a2014-09-04 11:32:09 -0400795
796#ifdef CONFIG_CPU_IDLE
797 /* Must be inspected within a rcu lock section */
798 struct cpuidle_state *idle_state;
799#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200800};
801
802static inline int cpu_of(struct rq *rq)
803{
804#ifdef CONFIG_SMP
805 return rq->cpu;
806#else
807 return 0;
808#endif
809}
810
Peter Zijlstra1b568f02016-05-09 10:38:41 +0200811
812#ifdef CONFIG_SCHED_SMT
813
814extern struct static_key_false sched_smt_present;
815
816extern void __update_idle_core(struct rq *rq);
817
818static inline void update_idle_core(struct rq *rq)
819{
820 if (static_branch_unlikely(&sched_smt_present))
821 __update_idle_core(rq);
822}
823
824#else
825static inline void update_idle_core(struct rq *rq) { }
826#endif
827
Pranith Kumar8b06c552014-08-13 13:28:12 -0400828DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200829
Peter Zijlstra518cd622011-12-07 15:07:31 +0100830#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500831#define this_rq() this_cpu_ptr(&runqueues)
Peter Zijlstra518cd622011-12-07 15:07:31 +0100832#define task_rq(p) cpu_rq(task_cpu(p))
833#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500834#define raw_rq() raw_cpu_ptr(&runqueues)
Peter Zijlstra518cd622011-12-07 15:07:31 +0100835
Peter Zijlstracebde6d2015-01-05 11:18:10 +0100836static inline u64 __rq_clock_broken(struct rq *rq)
837{
Jason Low316c1608d2015-04-28 13:00:20 -0700838 return READ_ONCE(rq->clock);
Peter Zijlstracebde6d2015-01-05 11:18:10 +0100839}
840
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100841/*
842 * rq::clock_update_flags bits
843 *
844 * %RQCF_REQ_SKIP - will request skipping of clock update on the next
845 * call to __schedule(). This is an optimisation to avoid
846 * neighbouring rq clock updates.
847 *
848 * %RQCF_ACT_SKIP - is set from inside of __schedule() when skipping is
849 * in effect and calls to update_rq_clock() are being ignored.
850 *
851 * %RQCF_UPDATED - is a debug flag that indicates whether a call has been
852 * made to update_rq_clock() since the last time rq::lock was pinned.
853 *
854 * If inside of __schedule(), clock_update_flags will have been
855 * shifted left (a left shift is a cheap operation for the fast path
856 * to promote %RQCF_REQ_SKIP to %RQCF_ACT_SKIP), so you must use,
857 *
858 * if (rq-clock_update_flags >= RQCF_UPDATED)
859 *
860 * to check if %RQCF_UPADTED is set. It'll never be shifted more than
861 * one position though, because the next rq_unpin_lock() will shift it
862 * back.
863 */
864#define RQCF_REQ_SKIP 0x01
865#define RQCF_ACT_SKIP 0x02
866#define RQCF_UPDATED 0x04
867
868static inline void assert_clock_updated(struct rq *rq)
869{
870 /*
871 * The only reason for not seeing a clock update since the
872 * last rq_pin_lock() is if we're currently skipping updates.
873 */
874 SCHED_WARN_ON(rq->clock_update_flags < RQCF_ACT_SKIP);
875}
876
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200877static inline u64 rq_clock(struct rq *rq)
878{
Peter Zijlstracebde6d2015-01-05 11:18:10 +0100879 lockdep_assert_held(&rq->lock);
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100880 assert_clock_updated(rq);
881
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200882 return rq->clock;
883}
884
885static inline u64 rq_clock_task(struct rq *rq)
886{
Peter Zijlstracebde6d2015-01-05 11:18:10 +0100887 lockdep_assert_held(&rq->lock);
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100888 assert_clock_updated(rq);
889
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200890 return rq->clock_task;
891}
892
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100893static inline void rq_clock_skip_update(struct rq *rq, bool skip)
894{
895 lockdep_assert_held(&rq->lock);
896 if (skip)
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100897 rq->clock_update_flags |= RQCF_REQ_SKIP;
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100898 else
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100899 rq->clock_update_flags &= ~RQCF_REQ_SKIP;
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100900}
901
Matt Flemingd8ac8972016-09-21 14:38:10 +0100902struct rq_flags {
903 unsigned long flags;
904 struct pin_cookie cookie;
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100905#ifdef CONFIG_SCHED_DEBUG
906 /*
907 * A copy of (rq::clock_update_flags & RQCF_UPDATED) for the
908 * current pin context is stashed here in case it needs to be
909 * restored in rq_repin_lock().
910 */
911 unsigned int clock_update_flags;
912#endif
Matt Flemingd8ac8972016-09-21 14:38:10 +0100913};
914
915static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf)
916{
917 rf->cookie = lockdep_pin_lock(&rq->lock);
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100918
919#ifdef CONFIG_SCHED_DEBUG
920 rq->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
921 rf->clock_update_flags = 0;
922#endif
Matt Flemingd8ac8972016-09-21 14:38:10 +0100923}
924
925static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf)
926{
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100927#ifdef CONFIG_SCHED_DEBUG
928 if (rq->clock_update_flags > RQCF_ACT_SKIP)
929 rf->clock_update_flags = RQCF_UPDATED;
930#endif
931
Matt Flemingd8ac8972016-09-21 14:38:10 +0100932 lockdep_unpin_lock(&rq->lock, rf->cookie);
933}
934
935static inline void rq_repin_lock(struct rq *rq, struct rq_flags *rf)
936{
937 lockdep_repin_lock(&rq->lock, rf->cookie);
Matt Flemingcb42c9a2016-09-21 14:38:13 +0100938
939#ifdef CONFIG_SCHED_DEBUG
940 /*
941 * Restore the value we stashed in @rf for this pin context.
942 */
943 rq->clock_update_flags |= rf->clock_update_flags;
944#endif
Matt Flemingd8ac8972016-09-21 14:38:10 +0100945}
946
Rik van Riel9942f792014-10-17 03:29:49 -0400947#ifdef CONFIG_NUMA
Rik van Riele3fe70b2014-10-17 03:29:50 -0400948enum numa_topology_type {
949 NUMA_DIRECT,
950 NUMA_GLUELESS_MESH,
951 NUMA_BACKPLANE,
952};
953extern enum numa_topology_type sched_numa_topology_type;
Rik van Riel9942f792014-10-17 03:29:49 -0400954extern int sched_max_numa_distance;
955extern bool find_numa_distance(int distance);
956#endif
957
Ingo Molnarf2cb1362017-02-01 13:10:18 +0100958#ifdef CONFIG_NUMA
959extern void sched_init_numa(void);
960extern void sched_domains_numa_masks_set(unsigned int cpu);
961extern void sched_domains_numa_masks_clear(unsigned int cpu);
962#else
963static inline void sched_init_numa(void) { }
964static inline void sched_domains_numa_masks_set(unsigned int cpu) { }
965static inline void sched_domains_numa_masks_clear(unsigned int cpu) { }
966#endif
967
Mel Gormanf809ca92013-10-07 11:28:57 +0100968#ifdef CONFIG_NUMA_BALANCING
Iulia Manda44dba3d2014-10-31 02:13:31 +0200969/* The regions in numa_faults array from task_struct */
970enum numa_faults_stats {
971 NUMA_MEM = 0,
972 NUMA_CPU,
973 NUMA_MEMBUF,
974 NUMA_CPUBUF
975};
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +0100976extern void sched_setnuma(struct task_struct *p, int node);
Mel Gormane6628d52013-10-07 11:29:02 +0100977extern int migrate_task_to(struct task_struct *p, int cpu);
Peter Zijlstraac66f542013-10-07 11:29:16 +0100978extern int migrate_swap(struct task_struct *, struct task_struct *);
Mel Gormanf809ca92013-10-07 11:28:57 +0100979#endif /* CONFIG_NUMA_BALANCING */
980
Peter Zijlstra518cd622011-12-07 15:07:31 +0100981#ifdef CONFIG_SMP
982
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200983static inline void
984queue_balance_callback(struct rq *rq,
985 struct callback_head *head,
986 void (*func)(struct rq *rq))
987{
988 lockdep_assert_held(&rq->lock);
989
990 if (unlikely(head->next))
991 return;
992
993 head->func = (void (*)(struct callback_head *))func;
994 head->next = rq->balance_callback;
995 rq->balance_callback = head;
996}
997
Peter Zijlstrae3baac42014-06-04 10:31:18 -0700998extern void sched_ttwu_pending(void);
999
Peter Zijlstra029632f2011-10-25 10:00:11 +02001000#define rcu_dereference_check_sched_domain(p) \
1001 rcu_dereference_check((p), \
1002 lockdep_is_held(&sched_domains_mutex))
1003
1004/*
1005 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
1006 * See detach_destroy_domains: synchronize_sched for details.
1007 *
1008 * The domain tree of any CPU may only be accessed from within
1009 * preempt-disabled sections.
1010 */
1011#define for_each_domain(cpu, __sd) \
Peter Zijlstra518cd622011-12-07 15:07:31 +01001012 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
1013 __sd; __sd = __sd->parent)
Peter Zijlstra029632f2011-10-25 10:00:11 +02001014
Suresh Siddha77e81362011-11-17 11:08:23 -08001015#define for_each_lower_domain(sd) for (; sd; sd = sd->child)
1016
Peter Zijlstra518cd622011-12-07 15:07:31 +01001017/**
1018 * highest_flag_domain - Return highest sched_domain containing flag.
1019 * @cpu: The cpu whose highest level of sched domain is to
1020 * be returned.
1021 * @flag: The flag to check for the highest sched_domain
1022 * for the given cpu.
1023 *
1024 * Returns the highest sched_domain of a cpu which contains the given flag.
1025 */
1026static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
1027{
1028 struct sched_domain *sd, *hsd = NULL;
1029
1030 for_each_domain(cpu, sd) {
1031 if (!(sd->flags & flag))
1032 break;
1033 hsd = sd;
1034 }
1035
1036 return hsd;
1037}
1038
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001039static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
1040{
1041 struct sched_domain *sd;
1042
1043 for_each_domain(cpu, sd) {
1044 if (sd->flags & flag)
1045 break;
1046 }
1047
1048 return sd;
1049}
1050
Peter Zijlstra518cd622011-12-07 15:07:31 +01001051DECLARE_PER_CPU(struct sched_domain *, sd_llc);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08001052DECLARE_PER_CPU(int, sd_llc_size);
Peter Zijlstra518cd622011-12-07 15:07:31 +01001053DECLARE_PER_CPU(int, sd_llc_id);
Peter Zijlstra0e369d72016-05-09 10:38:01 +02001054DECLARE_PER_CPU(struct sched_domain_shared *, sd_llc_shared);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001055DECLARE_PER_CPU(struct sched_domain *, sd_numa);
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05301056DECLARE_PER_CPU(struct sched_domain *, sd_asym);
Peter Zijlstra518cd622011-12-07 15:07:31 +01001057
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001058struct sched_group_capacity {
Li Zefan5e6521e2013-03-05 16:06:23 +08001059 atomic_t ref;
1060 /*
Yuyang Du172895e2016-04-05 12:12:27 +08001061 * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001062 * for a single CPU.
Li Zefan5e6521e2013-03-05 16:06:23 +08001063 */
Morten Rasmussenbf475ce2016-10-14 14:41:09 +01001064 unsigned long capacity;
1065 unsigned long min_capacity; /* Min per-CPU capacity in group */
Li Zefan5e6521e2013-03-05 16:06:23 +08001066 unsigned long next_update;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001067 int imbalance; /* XXX unrelated to capacity but shared group state */
Li Zefan5e6521e2013-03-05 16:06:23 +08001068
Peter Zijlstra005f8742017-04-26 17:35:35 +02001069#ifdef CONFIG_SCHED_DEBUG
1070 int id;
1071#endif
1072
Peter Zijlstrae5c14b12017-05-01 10:47:02 +02001073 unsigned long cpumask[0]; /* balance mask */
Li Zefan5e6521e2013-03-05 16:06:23 +08001074};
1075
1076struct sched_group {
1077 struct sched_group *next; /* Must be a circular list */
1078 atomic_t ref;
1079
1080 unsigned int group_weight;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001081 struct sched_group_capacity *sgc;
Tim Chenafe06ef2016-11-22 12:23:53 -08001082 int asym_prefer_cpu; /* cpu of highest priority in group */
Li Zefan5e6521e2013-03-05 16:06:23 +08001083
1084 /*
1085 * The CPUs this group covers.
1086 *
1087 * NOTE: this field is variable length. (Allocated dynamically
1088 * by attaching extra space to the end of the structure,
1089 * depending on how many CPUs the kernel has booted up with)
1090 */
1091 unsigned long cpumask[0];
1092};
1093
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02001094static inline struct cpumask *sched_group_span(struct sched_group *sg)
Li Zefan5e6521e2013-03-05 16:06:23 +08001095{
1096 return to_cpumask(sg->cpumask);
1097}
1098
1099/*
Peter Zijlstrae5c14b12017-05-01 10:47:02 +02001100 * See build_balance_mask().
Li Zefan5e6521e2013-03-05 16:06:23 +08001101 */
Peter Zijlstrae5c14b12017-05-01 10:47:02 +02001102static inline struct cpumask *group_balance_mask(struct sched_group *sg)
Li Zefan5e6521e2013-03-05 16:06:23 +08001103{
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001104 return to_cpumask(sg->sgc->cpumask);
Li Zefan5e6521e2013-03-05 16:06:23 +08001105}
1106
1107/**
1108 * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
1109 * @group: The group whose first cpu is to be returned.
1110 */
1111static inline unsigned int group_first_cpu(struct sched_group *group)
1112{
Peter Zijlstraae4df9d2017-05-01 11:03:12 +02001113 return cpumask_first(sched_group_span(group));
Li Zefan5e6521e2013-03-05 16:06:23 +08001114}
1115
Peter Zijlstrac1174872012-05-31 14:47:33 +02001116extern int group_balance_cpu(struct sched_group *sg);
1117
Steven Rostedt (Red Hat)3866e842016-02-22 16:26:51 -05001118#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
1119void register_sched_domain_sysctl(void);
Peter Zijlstrabbdacdf2017-08-10 17:10:26 +02001120void dirty_sched_domain_sysctl(int cpu);
Steven Rostedt (Red Hat)3866e842016-02-22 16:26:51 -05001121void unregister_sched_domain_sysctl(void);
1122#else
1123static inline void register_sched_domain_sysctl(void)
1124{
1125}
Peter Zijlstrabbdacdf2017-08-10 17:10:26 +02001126static inline void dirty_sched_domain_sysctl(int cpu)
1127{
1128}
Steven Rostedt (Red Hat)3866e842016-02-22 16:26:51 -05001129static inline void unregister_sched_domain_sysctl(void)
1130{
1131}
1132#endif
1133
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001134#else
1135
1136static inline void sched_ttwu_pending(void) { }
1137
Peter Zijlstra518cd622011-12-07 15:07:31 +01001138#endif /* CONFIG_SMP */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001139
Peter Zijlstra391e43d2011-11-15 17:14:39 +01001140#include "stats.h"
Ingo Molnar10514082017-02-01 18:42:41 +01001141#include "autogroup.h"
Peter Zijlstra029632f2011-10-25 10:00:11 +02001142
1143#ifdef CONFIG_CGROUP_SCHED
1144
1145/*
1146 * Return the group to which this tasks belongs.
1147 *
Tejun Heo8af01f52013-08-08 20:11:22 -04001148 * We cannot use task_css() and friends because the cgroup subsystem
1149 * changes that value before the cgroup_subsys::attach() method is called,
1150 * therefore we cannot pin it and might observe the wrong value.
Peter Zijlstra8323f262012-06-22 13:36:05 +02001151 *
1152 * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
1153 * core changes this before calling sched_move_task().
1154 *
1155 * Instead we use a 'copy' which is updated from sched_move_task() while
1156 * holding both task_struct::pi_lock and rq::lock.
Peter Zijlstra029632f2011-10-25 10:00:11 +02001157 */
1158static inline struct task_group *task_group(struct task_struct *p)
1159{
Peter Zijlstra8323f262012-06-22 13:36:05 +02001160 return p->sched_task_group;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001161}
1162
1163/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
1164static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
1165{
1166#if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
1167 struct task_group *tg = task_group(p);
1168#endif
1169
1170#ifdef CONFIG_FAIR_GROUP_SCHED
Byungchul Parkad936d82015-10-24 01:16:19 +09001171 set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001172 p->se.cfs_rq = tg->cfs_rq[cpu];
1173 p->se.parent = tg->se[cpu];
1174#endif
1175
1176#ifdef CONFIG_RT_GROUP_SCHED
1177 p->rt.rt_rq = tg->rt_rq[cpu];
1178 p->rt.parent = tg->rt_se[cpu];
1179#endif
1180}
1181
1182#else /* CONFIG_CGROUP_SCHED */
1183
1184static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
1185static inline struct task_group *task_group(struct task_struct *p)
1186{
1187 return NULL;
1188}
1189
1190#endif /* CONFIG_CGROUP_SCHED */
1191
1192static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1193{
1194 set_task_rq(p, cpu);
1195#ifdef CONFIG_SMP
1196 /*
1197 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1198 * successfuly executed on another CPU. We must ensure that updates of
1199 * per-task data have been completed by this moment.
1200 */
1201 smp_wmb();
Andy Lutomirskic65eacb2016-09-13 14:29:24 -07001202#ifdef CONFIG_THREAD_INFO_IN_TASK
1203 p->cpu = cpu;
1204#else
Peter Zijlstra029632f2011-10-25 10:00:11 +02001205 task_thread_info(p)->cpu = cpu;
Andy Lutomirskic65eacb2016-09-13 14:29:24 -07001206#endif
Peter Zijlstraac66f542013-10-07 11:29:16 +01001207 p->wake_cpu = cpu;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001208#endif
1209}
1210
1211/*
1212 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
1213 */
1214#ifdef CONFIG_SCHED_DEBUG
Ingo Molnarc5905af2012-02-24 08:31:31 +01001215# include <linux/static_key.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +02001216# define const_debug __read_mostly
1217#else
1218# define const_debug const
1219#endif
1220
1221extern const_debug unsigned int sysctl_sched_features;
1222
1223#define SCHED_FEAT(name, enabled) \
1224 __SCHED_FEAT_##name ,
1225
1226enum {
Peter Zijlstra391e43d2011-11-15 17:14:39 +01001227#include "features.h"
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001228 __SCHED_FEAT_NR,
Peter Zijlstra029632f2011-10-25 10:00:11 +02001229};
1230
1231#undef SCHED_FEAT
1232
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001233#if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL)
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001234#define SCHED_FEAT(name, enabled) \
Ingo Molnarc5905af2012-02-24 08:31:31 +01001235static __always_inline bool static_branch_##name(struct static_key *key) \
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001236{ \
Jason Baron6e76ea82014-07-02 15:52:41 +00001237 return static_key_##enabled(key); \
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001238}
1239
1240#include "features.h"
1241
1242#undef SCHED_FEAT
1243
Ingo Molnarc5905af2012-02-24 08:31:31 +01001244extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001245#define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
1246#else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001247#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001248#endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001249
Srikar Dronamraju2a595722015-08-11 21:54:21 +05301250extern struct static_key_false sched_numa_balancing;
Mel Gormancb251762016-02-05 09:08:36 +00001251extern struct static_key_false sched_schedstats;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001252
Peter Zijlstra029632f2011-10-25 10:00:11 +02001253static inline u64 global_rt_period(void)
1254{
1255 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
1256}
1257
1258static inline u64 global_rt_runtime(void)
1259{
1260 if (sysctl_sched_rt_runtime < 0)
1261 return RUNTIME_INF;
1262
1263 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
1264}
1265
Peter Zijlstra029632f2011-10-25 10:00:11 +02001266static inline int task_current(struct rq *rq, struct task_struct *p)
1267{
1268 return rq->curr == p;
1269}
1270
1271static inline int task_running(struct rq *rq, struct task_struct *p)
1272{
1273#ifdef CONFIG_SMP
1274 return p->on_cpu;
1275#else
1276 return task_current(rq, p);
1277#endif
1278}
1279
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001280static inline int task_on_rq_queued(struct task_struct *p)
1281{
1282 return p->on_rq == TASK_ON_RQ_QUEUED;
1283}
Peter Zijlstra029632f2011-10-25 10:00:11 +02001284
Kirill Tkhaicca26e82014-08-20 13:47:42 +04001285static inline int task_on_rq_migrating(struct task_struct *p)
1286{
1287 return p->on_rq == TASK_ON_RQ_MIGRATING;
1288}
1289
Peter Zijlstra029632f2011-10-25 10:00:11 +02001290#ifndef prepare_arch_switch
1291# define prepare_arch_switch(next) do { } while (0)
1292#endif
Catalin Marinas01f23e12011-11-27 21:43:10 +00001293#ifndef finish_arch_post_lock_switch
1294# define finish_arch_post_lock_switch() do { } while (0)
1295#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02001296
Peter Zijlstra029632f2011-10-25 10:00:11 +02001297static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
1298{
1299#ifdef CONFIG_SMP
1300 /*
1301 * We can optimise this out completely for !SMP, because the
1302 * SMP rebalancing from interrupt is the only thing that cares
1303 * here.
1304 */
1305 next->on_cpu = 1;
1306#endif
1307}
1308
1309static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
1310{
1311#ifdef CONFIG_SMP
1312 /*
1313 * After ->on_cpu is cleared, the task can be moved to a different CPU.
1314 * We must ensure this doesn't happen until the switch is completely
1315 * finished.
Peter Zijlstra95913d92015-09-29 14:45:09 +02001316 *
Peter Zijlstrab75a2252015-10-06 14:36:17 +02001317 * In particular, the load of prev->state in finish_task_switch() must
1318 * happen before this.
1319 *
Peter Zijlstra1f03e8d2016-04-04 10:57:12 +02001320 * Pairs with the smp_cond_load_acquire() in try_to_wake_up().
Peter Zijlstra029632f2011-10-25 10:00:11 +02001321 */
Peter Zijlstra95913d92015-09-29 14:45:09 +02001322 smp_store_release(&prev->on_cpu, 0);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001323#endif
1324#ifdef CONFIG_DEBUG_SPINLOCK
1325 /* this is a valid case when another task releases the spinlock */
1326 rq->lock.owner = current;
1327#endif
1328 /*
1329 * If we are tracking spinlock dependencies then we have to
1330 * fix up the runqueue lock - which gets 'carried over' from
1331 * prev into current:
1332 */
1333 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
1334
1335 raw_spin_unlock_irq(&rq->lock);
1336}
1337
Li Zefanb13095f2013-03-05 16:06:38 +08001338/*
1339 * wake flags
1340 */
1341#define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
1342#define WF_FORK 0x02 /* child wakeup after fork */
1343#define WF_MIGRATED 0x4 /* internal use, task got migrated */
1344
Peter Zijlstra029632f2011-10-25 10:00:11 +02001345/*
1346 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1347 * of tasks with abnormal "nice" values across CPUs the contribution that
1348 * each task makes to its run queue's load is weighted according to its
1349 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
1350 * scaled version of the new time slice allocation that they receive on time
1351 * slice expiry etc.
1352 */
1353
1354#define WEIGHT_IDLEPRIO 3
1355#define WMULT_IDLEPRIO 1431655765
1356
Andi Kleened82b8a2015-11-29 20:59:43 -08001357extern const int sched_prio_to_weight[40];
1358extern const u32 sched_prio_to_wmult[40];
Peter Zijlstra029632f2011-10-25 10:00:11 +02001359
Peter Zijlstraff77e462016-01-18 15:27:07 +01001360/*
1361 * {de,en}queue flags:
1362 *
1363 * DEQUEUE_SLEEP - task is no longer runnable
1364 * ENQUEUE_WAKEUP - task just became runnable
1365 *
1366 * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks
1367 * are in a known state which allows modification. Such pairs
1368 * should preserve as much state as possible.
1369 *
1370 * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location
1371 * in the runqueue.
1372 *
1373 * ENQUEUE_HEAD - place at front of runqueue (tail if not specified)
1374 * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline)
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02001375 * ENQUEUE_MIGRATED - the task was migrated during wakeup
Peter Zijlstraff77e462016-01-18 15:27:07 +01001376 *
1377 */
1378
1379#define DEQUEUE_SLEEP 0x01
1380#define DEQUEUE_SAVE 0x02 /* matches ENQUEUE_RESTORE */
1381#define DEQUEUE_MOVE 0x04 /* matches ENQUEUE_MOVE */
Peter Zijlstra0a67d1e2016-10-04 16:29:45 +02001382#define DEQUEUE_NOCLOCK 0x08 /* matches ENQUEUE_NOCLOCK */
Peter Zijlstraff77e462016-01-18 15:27:07 +01001383
Peter Zijlstra1de64442015-09-30 17:44:13 +02001384#define ENQUEUE_WAKEUP 0x01
Peter Zijlstraff77e462016-01-18 15:27:07 +01001385#define ENQUEUE_RESTORE 0x02
1386#define ENQUEUE_MOVE 0x04
Peter Zijlstra0a67d1e2016-10-04 16:29:45 +02001387#define ENQUEUE_NOCLOCK 0x08
Peter Zijlstraff77e462016-01-18 15:27:07 +01001388
Peter Zijlstra0a67d1e2016-10-04 16:29:45 +02001389#define ENQUEUE_HEAD 0x10
1390#define ENQUEUE_REPLENISH 0x20
Li Zefanc82ba9f2013-03-05 16:06:55 +08001391#ifdef CONFIG_SMP
Peter Zijlstra0a67d1e2016-10-04 16:29:45 +02001392#define ENQUEUE_MIGRATED 0x40
Li Zefanc82ba9f2013-03-05 16:06:55 +08001393#else
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02001394#define ENQUEUE_MIGRATED 0x00
Li Zefanc82ba9f2013-03-05 16:06:55 +08001395#endif
Li Zefanc82ba9f2013-03-05 16:06:55 +08001396
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001397#define RETRY_TASK ((void *)-1UL)
1398
Li Zefanc82ba9f2013-03-05 16:06:55 +08001399struct sched_class {
1400 const struct sched_class *next;
1401
1402 void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
1403 void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
1404 void (*yield_task) (struct rq *rq);
1405 bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt);
1406
1407 void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags);
1408
Peter Zijlstra606dba22012-02-11 06:05:00 +01001409 /*
1410 * It is the responsibility of the pick_next_task() method that will
1411 * return the next task to call put_prev_task() on the @prev task or
1412 * something equivalent.
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001413 *
1414 * May return RETRY_TASK when it finds a higher prio class has runnable
1415 * tasks.
Peter Zijlstra606dba22012-02-11 06:05:00 +01001416 */
1417 struct task_struct * (*pick_next_task) (struct rq *rq,
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001418 struct task_struct *prev,
Matt Flemingd8ac8972016-09-21 14:38:10 +01001419 struct rq_flags *rf);
Li Zefanc82ba9f2013-03-05 16:06:55 +08001420 void (*put_prev_task) (struct rq *rq, struct task_struct *p);
1421
1422#ifdef CONFIG_SMP
Peter Zijlstraac66f542013-10-07 11:29:16 +01001423 int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);
xiaofeng.yan5a4fd032015-09-23 14:55:59 +08001424 void (*migrate_task_rq)(struct task_struct *p);
Li Zefanc82ba9f2013-03-05 16:06:55 +08001425
Li Zefanc82ba9f2013-03-05 16:06:55 +08001426 void (*task_woken) (struct rq *this_rq, struct task_struct *task);
1427
1428 void (*set_cpus_allowed)(struct task_struct *p,
1429 const struct cpumask *newmask);
1430
1431 void (*rq_online)(struct rq *rq);
1432 void (*rq_offline)(struct rq *rq);
1433#endif
1434
1435 void (*set_curr_task) (struct rq *rq);
1436 void (*task_tick) (struct rq *rq, struct task_struct *p, int queued);
1437 void (*task_fork) (struct task_struct *p);
Dario Faggiolie6c390f2013-11-07 14:43:35 +01001438 void (*task_dead) (struct task_struct *p);
Li Zefanc82ba9f2013-03-05 16:06:55 +08001439
Kirill Tkhai67dfa1b2014-10-27 17:40:52 +03001440 /*
1441 * The switched_from() call is allowed to drop rq->lock, therefore we
1442 * cannot assume the switched_from/switched_to pair is serliazed by
1443 * rq->lock. They are however serialized by p->pi_lock.
1444 */
Li Zefanc82ba9f2013-03-05 16:06:55 +08001445 void (*switched_from) (struct rq *this_rq, struct task_struct *task);
1446 void (*switched_to) (struct rq *this_rq, struct task_struct *task);
1447 void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
1448 int oldprio);
1449
1450 unsigned int (*get_rr_interval) (struct rq *rq,
1451 struct task_struct *task);
1452
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01001453 void (*update_curr) (struct rq *rq);
1454
Vincent Guittotea86cb42016-06-17 13:38:55 +02001455#define TASK_SET_GROUP 0
1456#define TASK_MOVE_GROUP 1
1457
Li Zefanc82ba9f2013-03-05 16:06:55 +08001458#ifdef CONFIG_FAIR_GROUP_SCHED
Vincent Guittotea86cb42016-06-17 13:38:55 +02001459 void (*task_change_group) (struct task_struct *p, int type);
Li Zefanc82ba9f2013-03-05 16:06:55 +08001460#endif
1461};
Peter Zijlstra029632f2011-10-25 10:00:11 +02001462
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01001463static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
1464{
1465 prev->sched_class->put_prev_task(rq, prev);
1466}
1467
Peter Zijlstrab2bf6c32016-09-20 22:00:38 +02001468static inline void set_curr_task(struct rq *rq, struct task_struct *curr)
1469{
1470 curr->sched_class->set_curr_task(rq);
1471}
1472
Nicolas Pitref5832c12017-05-29 17:02:57 -04001473#ifdef CONFIG_SMP
Peter Zijlstra029632f2011-10-25 10:00:11 +02001474#define sched_class_highest (&stop_sched_class)
Nicolas Pitref5832c12017-05-29 17:02:57 -04001475#else
1476#define sched_class_highest (&dl_sched_class)
1477#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02001478#define for_each_class(class) \
1479 for (class = sched_class_highest; class; class = class->next)
1480
1481extern const struct sched_class stop_sched_class;
Dario Faggioliaab03e02013-11-28 11:14:43 +01001482extern const struct sched_class dl_sched_class;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001483extern const struct sched_class rt_sched_class;
1484extern const struct sched_class fair_sched_class;
1485extern const struct sched_class idle_sched_class;
1486
1487
1488#ifdef CONFIG_SMP
1489
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001490extern void update_group_capacity(struct sched_domain *sd, int cpu);
Li Zefanb7192032013-03-07 10:00:26 +08001491
Daniel Lezcano7caff662014-01-06 12:34:38 +01001492extern void trigger_load_balance(struct rq *rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001493
Peter Zijlstrac5b28032015-05-15 17:43:35 +02001494extern void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask);
1495
Peter Zijlstra029632f2011-10-25 10:00:11 +02001496#endif
1497
Daniel Lezcano442bf3a2014-09-04 11:32:09 -04001498#ifdef CONFIG_CPU_IDLE
1499static inline void idle_set_state(struct rq *rq,
1500 struct cpuidle_state *idle_state)
1501{
1502 rq->idle_state = idle_state;
1503}
1504
1505static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1506{
Peter Zijlstra9148a3a2016-09-20 22:34:51 +02001507 SCHED_WARN_ON(!rcu_read_lock_held());
Daniel Lezcano442bf3a2014-09-04 11:32:09 -04001508 return rq->idle_state;
1509}
1510#else
1511static inline void idle_set_state(struct rq *rq,
1512 struct cpuidle_state *idle_state)
1513{
1514}
1515
1516static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1517{
1518 return NULL;
1519}
1520#endif
1521
Steven Rostedt (VMware)8663eff2017-04-14 08:48:09 -04001522extern void schedule_idle(void);
1523
Peter Zijlstra029632f2011-10-25 10:00:11 +02001524extern void sysrq_sched_debug_show(void);
1525extern void sched_init_granularity(void);
1526extern void update_max_interval(void);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001527
1528extern void init_sched_dl_class(void);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001529extern void init_sched_rt_class(void);
1530extern void init_sched_fair_class(void);
1531
Kirill Tkhai88751252014-06-29 00:03:57 +04001532extern void resched_curr(struct rq *rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001533extern void resched_cpu(int cpu);
1534
1535extern struct rt_bandwidth def_rt_bandwidth;
1536extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
1537
Dario Faggioli332ac172013-11-07 14:43:45 +01001538extern struct dl_bandwidth def_dl_bandwidth;
1539extern void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001540extern void init_dl_task_timer(struct sched_dl_entity *dl_se);
Luca Abeni209a0cb2017-05-18 22:13:29 +02001541extern void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se);
Luca Abeni4da3abc2017-05-18 22:13:32 +02001542extern void init_dl_rq_bw_ratio(struct dl_rq *dl_rq);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001543
Luca Abenic52f14d2017-05-18 22:13:31 +02001544#define BW_SHIFT 20
1545#define BW_UNIT (1 << BW_SHIFT)
Luca Abeni4da3abc2017-05-18 22:13:32 +02001546#define RATIO_SHIFT 8
Dario Faggioli332ac172013-11-07 14:43:45 +01001547unsigned long to_ratio(u64 period, u64 runtime);
1548
Yuyang Du540247f2015-07-15 08:04:39 +08001549extern void init_entity_runnable_average(struct sched_entity *se);
Yuyang Du2b8c41d2016-03-30 04:30:56 +08001550extern void post_init_entity_util_avg(struct sched_entity *se);
Alex Shia75cdaa2013-06-20 10:18:47 +08001551
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +02001552#ifdef CONFIG_NO_HZ_FULL
1553extern bool sched_can_stop_tick(struct rq *rq);
1554
1555/*
1556 * Tick may be needed by tasks in the runqueue depending on their policy and
1557 * requirements. If tick is needed, lets send the target an IPI to kick it out of
1558 * nohz mode if necessary.
1559 */
1560static inline void sched_update_tick_dependency(struct rq *rq)
1561{
1562 int cpu;
1563
1564 if (!tick_nohz_full_enabled())
1565 return;
1566
1567 cpu = cpu_of(rq);
1568
1569 if (!tick_nohz_full_cpu(cpu))
1570 return;
1571
1572 if (sched_can_stop_tick(rq))
1573 tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED);
1574 else
1575 tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED);
1576}
1577#else
1578static inline void sched_update_tick_dependency(struct rq *rq) { }
1579#endif
1580
Kirill Tkhai72465442014-05-09 03:00:14 +04001581static inline void add_nr_running(struct rq *rq, unsigned count)
Peter Zijlstra029632f2011-10-25 10:00:11 +02001582{
Kirill Tkhai72465442014-05-09 03:00:14 +04001583 unsigned prev_nr = rq->nr_running;
1584
1585 rq->nr_running = prev_nr + count;
Frederic Weisbecker9f3660c2013-04-20 14:35:09 +02001586
Kirill Tkhai72465442014-05-09 03:00:14 +04001587 if (prev_nr < 2 && rq->nr_running >= 2) {
Tim Chen4486edd2014-06-23 12:16:49 -07001588#ifdef CONFIG_SMP
1589 if (!rq->rd->overload)
1590 rq->rd->overload = true;
1591#endif
Tim Chen4486edd2014-06-23 12:16:49 -07001592 }
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +02001593
1594 sched_update_tick_dependency(rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001595}
1596
Kirill Tkhai72465442014-05-09 03:00:14 +04001597static inline void sub_nr_running(struct rq *rq, unsigned count)
Peter Zijlstra029632f2011-10-25 10:00:11 +02001598{
Kirill Tkhai72465442014-05-09 03:00:14 +04001599 rq->nr_running -= count;
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +02001600 /* Check if we still need preemption */
1601 sched_update_tick_dependency(rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001602}
1603
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02001604static inline void rq_last_tick_reset(struct rq *rq)
1605{
1606#ifdef CONFIG_NO_HZ_FULL
1607 rq->last_sched_tick = jiffies;
1608#endif
1609}
1610
Peter Zijlstra029632f2011-10-25 10:00:11 +02001611extern void update_rq_clock(struct rq *rq);
1612
1613extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
1614extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
1615
1616extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
1617
1618extern const_debug unsigned int sysctl_sched_time_avg;
1619extern const_debug unsigned int sysctl_sched_nr_migrate;
1620extern const_debug unsigned int sysctl_sched_migration_cost;
1621
1622static inline u64 sched_avg_period(void)
1623{
1624 return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
1625}
1626
Peter Zijlstra029632f2011-10-25 10:00:11 +02001627#ifdef CONFIG_SCHED_HRTICK
1628
1629/*
1630 * Use hrtick when:
1631 * - enabled by features
1632 * - hrtimer is actually high res
1633 */
1634static inline int hrtick_enabled(struct rq *rq)
1635{
1636 if (!sched_feat(HRTICK))
1637 return 0;
1638 if (!cpu_active(cpu_of(rq)))
1639 return 0;
1640 return hrtimer_is_hres_active(&rq->hrtick_timer);
1641}
1642
1643void hrtick_start(struct rq *rq, u64 delay);
1644
Mike Galbraithb39e66e2011-11-22 15:20:07 +01001645#else
1646
1647static inline int hrtick_enabled(struct rq *rq)
1648{
1649 return 0;
1650}
1651
Peter Zijlstra029632f2011-10-25 10:00:11 +02001652#endif /* CONFIG_SCHED_HRTICK */
1653
1654#ifdef CONFIG_SMP
1655extern void sched_avg_update(struct rq *rq);
Peter Zijlstradfbca412015-03-23 14:19:05 +01001656
1657#ifndef arch_scale_freq_capacity
1658static __always_inline
1659unsigned long arch_scale_freq_capacity(struct sched_domain *sd, int cpu)
1660{
1661 return SCHED_CAPACITY_SCALE;
1662}
1663#endif
Vincent Guittotb5b48602015-02-27 16:54:08 +01001664
Morten Rasmussen8cd56012015-08-14 17:23:10 +01001665#ifndef arch_scale_cpu_capacity
1666static __always_inline
1667unsigned long arch_scale_cpu_capacity(struct sched_domain *sd, int cpu)
1668{
Dietmar Eggemanne3279a22015-08-15 00:04:41 +01001669 if (sd && (sd->flags & SD_SHARE_CPUCAPACITY) && (sd->span_weight > 1))
Morten Rasmussen8cd56012015-08-14 17:23:10 +01001670 return sd->smt_gain / sd->span_weight;
1671
1672 return SCHED_CAPACITY_SCALE;
1673}
1674#endif
1675
Peter Zijlstra029632f2011-10-25 10:00:11 +02001676static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
1677{
Vincent Guittotb5b48602015-02-27 16:54:08 +01001678 rq->rt_avg += rt_delta * arch_scale_freq_capacity(NULL, cpu_of(rq));
Peter Zijlstra029632f2011-10-25 10:00:11 +02001679 sched_avg_update(rq);
1680}
1681#else
1682static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { }
1683static inline void sched_avg_update(struct rq *rq) { }
1684#endif
1685
Peter Zijlstraeb580752015-07-31 21:28:18 +02001686struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
Peter Zijlstra3e71a462016-04-28 16:16:33 +02001687 __acquires(rq->lock);
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001688
Peter Zijlstraeb580752015-07-31 21:28:18 +02001689struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001690 __acquires(p->pi_lock)
Peter Zijlstra3e71a462016-04-28 16:16:33 +02001691 __acquires(rq->lock);
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001692
Peter Zijlstraeb580752015-07-31 21:28:18 +02001693static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf)
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001694 __releases(rq->lock)
1695{
Matt Flemingd8ac8972016-09-21 14:38:10 +01001696 rq_unpin_lock(rq, rf);
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001697 raw_spin_unlock(&rq->lock);
1698}
1699
1700static inline void
Peter Zijlstraeb580752015-07-31 21:28:18 +02001701task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001702 __releases(rq->lock)
1703 __releases(p->pi_lock)
1704{
Matt Flemingd8ac8972016-09-21 14:38:10 +01001705 rq_unpin_lock(rq, rf);
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001706 raw_spin_unlock(&rq->lock);
Peter Zijlstraeb580752015-07-31 21:28:18 +02001707 raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
Peter Zijlstra3960c8c2015-02-17 13:22:25 +01001708}
1709
Peter Zijlstra8a8c69c2016-10-04 16:04:35 +02001710static inline void
1711rq_lock_irqsave(struct rq *rq, struct rq_flags *rf)
1712 __acquires(rq->lock)
1713{
1714 raw_spin_lock_irqsave(&rq->lock, rf->flags);
1715 rq_pin_lock(rq, rf);
1716}
1717
1718static inline void
1719rq_lock_irq(struct rq *rq, struct rq_flags *rf)
1720 __acquires(rq->lock)
1721{
1722 raw_spin_lock_irq(&rq->lock);
1723 rq_pin_lock(rq, rf);
1724}
1725
1726static inline void
1727rq_lock(struct rq *rq, struct rq_flags *rf)
1728 __acquires(rq->lock)
1729{
1730 raw_spin_lock(&rq->lock);
1731 rq_pin_lock(rq, rf);
1732}
1733
1734static inline void
1735rq_relock(struct rq *rq, struct rq_flags *rf)
1736 __acquires(rq->lock)
1737{
1738 raw_spin_lock(&rq->lock);
1739 rq_repin_lock(rq, rf);
1740}
1741
1742static inline void
1743rq_unlock_irqrestore(struct rq *rq, struct rq_flags *rf)
1744 __releases(rq->lock)
1745{
1746 rq_unpin_lock(rq, rf);
1747 raw_spin_unlock_irqrestore(&rq->lock, rf->flags);
1748}
1749
1750static inline void
1751rq_unlock_irq(struct rq *rq, struct rq_flags *rf)
1752 __releases(rq->lock)
1753{
1754 rq_unpin_lock(rq, rf);
1755 raw_spin_unlock_irq(&rq->lock);
1756}
1757
1758static inline void
1759rq_unlock(struct rq *rq, struct rq_flags *rf)
1760 __releases(rq->lock)
1761{
1762 rq_unpin_lock(rq, rf);
1763 raw_spin_unlock(&rq->lock);
1764}
1765
Peter Zijlstra029632f2011-10-25 10:00:11 +02001766#ifdef CONFIG_SMP
1767#ifdef CONFIG_PREEMPT
1768
1769static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
1770
1771/*
1772 * fair double_lock_balance: Safely acquires both rq->locks in a fair
1773 * way at the expense of forcing extra atomic operations in all
1774 * invocations. This assures that the double_lock is acquired using the
1775 * same underlying policy as the spinlock_t on this architecture, which
1776 * reduces latency compared to the unfair variant below. However, it
1777 * also adds more overhead and therefore may reduce throughput.
1778 */
1779static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1780 __releases(this_rq->lock)
1781 __acquires(busiest->lock)
1782 __acquires(this_rq->lock)
1783{
1784 raw_spin_unlock(&this_rq->lock);
1785 double_rq_lock(this_rq, busiest);
1786
1787 return 1;
1788}
1789
1790#else
1791/*
1792 * Unfair double_lock_balance: Optimizes throughput at the expense of
1793 * latency by eliminating extra atomic operations when the locks are
1794 * already in proper order on entry. This favors lower cpu-ids and will
1795 * grant the double lock to lower cpus over higher ids under contention,
1796 * regardless of entry order into the function.
1797 */
1798static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
1799 __releases(this_rq->lock)
1800 __acquires(busiest->lock)
1801 __acquires(this_rq->lock)
1802{
1803 int ret = 0;
1804
1805 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
1806 if (busiest < this_rq) {
1807 raw_spin_unlock(&this_rq->lock);
1808 raw_spin_lock(&busiest->lock);
1809 raw_spin_lock_nested(&this_rq->lock,
1810 SINGLE_DEPTH_NESTING);
1811 ret = 1;
1812 } else
1813 raw_spin_lock_nested(&busiest->lock,
1814 SINGLE_DEPTH_NESTING);
1815 }
1816 return ret;
1817}
1818
1819#endif /* CONFIG_PREEMPT */
1820
1821/*
1822 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
1823 */
1824static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
1825{
1826 if (unlikely(!irqs_disabled())) {
1827 /* printk() doesn't work good under rq->lock */
1828 raw_spin_unlock(&this_rq->lock);
1829 BUG_ON(1);
1830 }
1831
1832 return _double_lock_balance(this_rq, busiest);
1833}
1834
1835static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
1836 __releases(busiest->lock)
1837{
1838 raw_spin_unlock(&busiest->lock);
1839 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
1840}
1841
Peter Zijlstra74602312013-10-10 20:17:22 +02001842static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
1843{
1844 if (l1 > l2)
1845 swap(l1, l2);
1846
1847 spin_lock(l1);
1848 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1849}
1850
Mike Galbraith60e69ee2014-04-07 10:55:15 +02001851static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2)
1852{
1853 if (l1 > l2)
1854 swap(l1, l2);
1855
1856 spin_lock_irq(l1);
1857 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1858}
1859
Peter Zijlstra74602312013-10-10 20:17:22 +02001860static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
1861{
1862 if (l1 > l2)
1863 swap(l1, l2);
1864
1865 raw_spin_lock(l1);
1866 raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
1867}
1868
Peter Zijlstra029632f2011-10-25 10:00:11 +02001869/*
1870 * double_rq_lock - safely lock two runqueues
1871 *
1872 * Note this does not disable interrupts like task_rq_lock,
1873 * you need to do so manually before calling.
1874 */
1875static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1876 __acquires(rq1->lock)
1877 __acquires(rq2->lock)
1878{
1879 BUG_ON(!irqs_disabled());
1880 if (rq1 == rq2) {
1881 raw_spin_lock(&rq1->lock);
1882 __acquire(rq2->lock); /* Fake it out ;) */
1883 } else {
1884 if (rq1 < rq2) {
1885 raw_spin_lock(&rq1->lock);
1886 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
1887 } else {
1888 raw_spin_lock(&rq2->lock);
1889 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
1890 }
1891 }
1892}
1893
1894/*
1895 * double_rq_unlock - safely unlock two runqueues
1896 *
1897 * Note this does not restore interrupts like task_rq_unlock,
1898 * you need to do so manually after calling.
1899 */
1900static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1901 __releases(rq1->lock)
1902 __releases(rq2->lock)
1903{
1904 raw_spin_unlock(&rq1->lock);
1905 if (rq1 != rq2)
1906 raw_spin_unlock(&rq2->lock);
1907 else
1908 __release(rq2->lock);
1909}
1910
Ingo Molnarf2cb1362017-02-01 13:10:18 +01001911extern void set_rq_online (struct rq *rq);
1912extern void set_rq_offline(struct rq *rq);
1913extern bool sched_smp_initialized;
1914
Peter Zijlstra029632f2011-10-25 10:00:11 +02001915#else /* CONFIG_SMP */
1916
1917/*
1918 * double_rq_lock - safely lock two runqueues
1919 *
1920 * Note this does not disable interrupts like task_rq_lock,
1921 * you need to do so manually before calling.
1922 */
1923static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
1924 __acquires(rq1->lock)
1925 __acquires(rq2->lock)
1926{
1927 BUG_ON(!irqs_disabled());
1928 BUG_ON(rq1 != rq2);
1929 raw_spin_lock(&rq1->lock);
1930 __acquire(rq2->lock); /* Fake it out ;) */
1931}
1932
1933/*
1934 * double_rq_unlock - safely unlock two runqueues
1935 *
1936 * Note this does not restore interrupts like task_rq_unlock,
1937 * you need to do so manually after calling.
1938 */
1939static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
1940 __releases(rq1->lock)
1941 __releases(rq2->lock)
1942{
1943 BUG_ON(rq1 != rq2);
1944 raw_spin_unlock(&rq1->lock);
1945 __release(rq2->lock);
1946}
1947
1948#endif
1949
1950extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
1951extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
Srikar Dronamraju6b55c962015-06-25 22:51:41 +05301952
1953#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra9469eb02017-09-07 17:03:53 +02001954extern bool sched_debug_enabled;
1955
Peter Zijlstra029632f2011-10-25 10:00:11 +02001956extern void print_cfs_stats(struct seq_file *m, int cpu);
1957extern void print_rt_stats(struct seq_file *m, int cpu);
Wanpeng Liacb32132014-10-31 06:39:33 +08001958extern void print_dl_stats(struct seq_file *m, int cpu);
Srikar Dronamraju6b55c962015-06-25 22:51:41 +05301959extern void
1960print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq);
Srikar Dronamraju397f2372015-06-25 22:51:43 +05301961#ifdef CONFIG_NUMA_BALANCING
1962extern void
1963show_numa_stats(struct task_struct *p, struct seq_file *m);
1964extern void
1965print_numa_stats(struct seq_file *m, int node, unsigned long tsf,
1966 unsigned long tpf, unsigned long gsf, unsigned long gpf);
1967#endif /* CONFIG_NUMA_BALANCING */
1968#endif /* CONFIG_SCHED_DEBUG */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001969
1970extern void init_cfs_rq(struct cfs_rq *cfs_rq);
Abel Vesa07c54f72015-03-03 13:50:27 +02001971extern void init_rt_rq(struct rt_rq *rt_rq);
1972extern void init_dl_rq(struct dl_rq *dl_rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001973
Ben Segall1ee14e62013-10-16 11:16:12 -07001974extern void cfs_bandwidth_usage_inc(void);
1975extern void cfs_bandwidth_usage_dec(void);
Suresh Siddha1c792db2011-12-01 17:07:32 -08001976
Frederic Weisbecker3451d022011-08-10 23:21:01 +02001977#ifdef CONFIG_NO_HZ_COMMON
Suresh Siddha1c792db2011-12-01 17:07:32 -08001978enum rq_nohz_flag_bits {
1979 NOHZ_TICK_STOPPED,
1980 NOHZ_BALANCE_KICK,
1981};
1982
1983#define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
Thomas Gleixner20a5c8c2016-03-10 12:54:20 +01001984
1985extern void nohz_balance_exit_idle(unsigned int cpu);
1986#else
1987static inline void nohz_balance_exit_idle(unsigned int cpu) { }
Suresh Siddha1c792db2011-12-01 17:07:32 -08001988#endif
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02001989
Luca Abenidaec5792017-05-18 22:13:36 +02001990
1991#ifdef CONFIG_SMP
1992static inline
1993void __dl_update(struct dl_bw *dl_b, s64 bw)
1994{
1995 struct root_domain *rd = container_of(dl_b, struct root_domain, dl_bw);
1996 int i;
1997
1998 RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
1999 "sched RCU must be held");
2000 for_each_cpu_and(i, rd->span, cpu_active_mask) {
2001 struct rq *rq = cpu_rq(i);
2002
2003 rq->dl.extra_bw += bw;
2004 }
2005}
2006#else
2007static inline
2008void __dl_update(struct dl_bw *dl_b, s64 bw)
2009{
2010 struct dl_rq *dl = container_of(dl_b, struct dl_rq, dl_bw);
2011
2012 dl->extra_bw += bw;
2013}
2014#endif
2015
2016
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002017#ifdef CONFIG_IRQ_TIME_ACCOUNTING
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002018struct irqtime {
Frederic Weisbecker25e2d8c2017-04-25 16:10:48 +02002019 u64 total;
Frederic Weisbeckera499a5a2017-01-31 04:09:32 +01002020 u64 tick_delta;
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002021 u64 irq_start_time;
2022 struct u64_stats_sync sync;
2023};
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002024
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002025DECLARE_PER_CPU(struct irqtime, cpu_irqtime);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002026
Frederic Weisbecker25e2d8c2017-04-25 16:10:48 +02002027/*
2028 * Returns the irqtime minus the softirq time computed by ksoftirqd.
2029 * Otherwise ksoftirqd's sum_exec_runtime is substracted its own runtime
2030 * and never move forward.
2031 */
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002032static inline u64 irq_time_read(int cpu)
2033{
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002034 struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu);
2035 unsigned int seq;
2036 u64 total;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002037
2038 do {
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002039 seq = __u64_stats_fetch_begin(&irqtime->sync);
Frederic Weisbecker25e2d8c2017-04-25 16:10:48 +02002040 total = irqtime->total;
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002041 } while (__u64_stats_fetch_retry(&irqtime->sync, seq));
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002042
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002043 return total;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002044}
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002045#endif /* CONFIG_IRQ_TIME_ACCOUNTING */
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002046
2047#ifdef CONFIG_CPU_FREQ
2048DECLARE_PER_CPU(struct update_util_data *, cpufreq_update_util_data);
2049
2050/**
2051 * cpufreq_update_util - Take a note about CPU utilization changes.
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02002052 * @rq: Runqueue to carry out the update for.
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02002053 * @flags: Update reason flags.
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002054 *
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02002055 * This function is called by the scheduler on the CPU whose utilization is
2056 * being updated.
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002057 *
2058 * It can only be called from RCU-sched read-side critical sections.
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002059 *
2060 * The way cpufreq is currently arranged requires it to evaluate the CPU
2061 * performance state (frequency/voltage) on a regular basis to prevent it from
2062 * being stuck in a completely inadequate performance level for too long.
2063 * That is not guaranteed to happen if the updates are only triggered from CFS,
2064 * though, because they may not be coming in if RT or deadline tasks are active
2065 * all the time (or there are RT and DL tasks only).
2066 *
2067 * As a workaround for that issue, this function is called by the RT and DL
2068 * sched classes to trigger extra cpufreq updates to prevent it from stalling,
2069 * but that really is a band-aid. Going forward it should be replaced with
2070 * solutions targeted more specifically at RT and DL tasks.
2071 */
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02002072static inline void cpufreq_update_util(struct rq *rq, unsigned int flags)
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002073{
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02002074 struct update_util_data *data;
2075
Viresh Kumar674e7542017-07-28 12:16:38 +05302076 data = rcu_dereference_sched(*per_cpu_ptr(&cpufreq_update_util_data,
2077 cpu_of(rq)));
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02002078 if (data)
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02002079 data->func(data, rq_clock(rq), flags);
2080}
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002081#else
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02002082static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {}
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002083#endif /* CONFIG_CPU_FREQ */
Linus Torvaldsbe53f582016-03-24 09:42:50 -07002084
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +02002085#ifdef arch_scale_freq_capacity
2086#ifndef arch_scale_freq_invariant
2087#define arch_scale_freq_invariant() (true)
2088#endif
2089#else /* arch_scale_freq_capacity */
2090#define arch_scale_freq_invariant() (false)
2091#endif