blob: 0ed63b800005c92e23dccaae2f0583663b6f3010 [file] [log] [blame]
Peter Zijlstra029632f2011-10-25 10:00:11 +02001
2#include <linux/sched.h>
Clark Williamscf4aebc22013-02-07 09:46:59 -06003#include <linux/sched/sysctl.h>
Clark Williams8bd75c72013-02-07 09:47:07 -06004#include <linux/sched/rt.h>
Ben Hutchingsc8034092019-05-10 00:46:25 +01005#include <linux/sched/smt.h>
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02006#include <linux/u64_stats_sync.h>
Dario Faggioliaab03e02013-11-28 11:14:43 +01007#include <linux/sched/deadline.h>
Frederic Weisbecker034cd8a2017-01-31 04:09:32 +01008#include <linux/kernel_stat.h>
Steven Rostedt (Red Hat)3866e842016-02-22 16:26:51 -05009#include <linux/binfmts.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020010#include <linux/mutex.h>
Johannes Weiner3df0e592018-10-26 15:06:27 -070011#include <linux/psi.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020012#include <linux/spinlock.h>
13#include <linux/stop_machine.h>
Steven Rostedtb6366f02015-03-18 14:49:46 -040014#include <linux/irq_work.h>
Frederic Weisbecker9f3660c2013-04-20 14:35:09 +020015#include <linux/tick.h>
Mel Gormanf809ca92013-10-07 11:28:57 +010016#include <linux/slab.h>
Pavankumar Kondetic5927f12017-10-11 12:36:12 +053017#include <linux/sched_energy.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +020018
Peter Zijlstra391e43d2011-11-15 17:14:39 +010019#include "cpupri.h"
Juri Lelli6bfd6d72013-11-07 14:43:47 +010020#include "cpudeadline.h"
Li Zefan60fed782013-03-29 14:36:43 +080021#include "cpuacct.h"
Peter Zijlstra029632f2011-10-25 10:00:11 +020022
Peter Zijlstra9148a3a2016-09-20 22:34:51 +020023#ifdef CONFIG_SCHED_DEBUG
24#define SCHED_WARN_ON(x) WARN_ONCE(x, #x)
25#else
26#define SCHED_WARN_ON(x) ((void)(x))
27#endif
28
Paul Gortmaker45ceebf2013-04-19 15:10:49 -040029struct rq;
Daniel Lezcano442bf3a2014-09-04 11:32:09 -040030struct cpuidle_state;
Paul Gortmaker45ceebf2013-04-19 15:10:49 -040031
Syed Rameez Mustafae14a2332017-05-19 14:42:35 -070032extern __read_mostly bool sched_predl;
Pavankumar Kondeti0d91fac2017-10-11 14:50:28 +053033extern unsigned int sched_smp_overlap_capacity;
Syed Rameez Mustafae14a2332017-05-19 14:42:35 -070034
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -080035#ifdef CONFIG_SCHED_WALT
36extern unsigned int sched_ravg_window;
Joonwoo Park858d5752017-08-21 12:09:49 -070037extern unsigned int walt_cpu_util_freq_divisor;
Channagoud Kadabi8810e5f2017-02-17 16:01:05 -080038
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +053039struct walt_sched_stats {
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -080040 int nr_big_tasks;
41 u64 cumulative_runnable_avg;
42 u64 pred_demands_sum;
43};
44
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -080045struct cpu_cycle {
46 u64 cycles;
47 u64 time;
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -080048};
49
50struct group_cpu_time {
51 u64 curr_runnable_sum;
52 u64 prev_runnable_sum;
53 u64 nt_curr_runnable_sum;
54 u64 nt_prev_runnable_sum;
55};
56
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -080057struct load_subtractions {
58 u64 window_start;
59 u64 subs;
60 u64 new_subs;
61};
62
63#define NUM_TRACKED_WINDOWS 2
64#define NUM_LOAD_INDICES 1000
65
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -080066struct sched_cluster {
67 raw_spinlock_t load_lock;
68 struct list_head list;
69 struct cpumask cpus;
70 int id;
71 int max_power_cost;
72 int min_power_cost;
73 int max_possible_capacity;
74 int capacity;
75 int efficiency; /* Differentiate cpus with different IPC capability */
76 int load_scale_factor;
77 unsigned int exec_scale_factor;
78 /*
79 * max_freq = user maximum
80 * max_mitigated_freq = thermal defined maximum
81 * max_possible_freq = maximum supported by hardware
82 */
83 unsigned int cur_freq, max_freq, max_mitigated_freq, min_freq;
84 unsigned int max_possible_freq;
85 bool freq_init_done;
86 int dstate, dstate_wakeup_latency, dstate_wakeup_energy;
87 unsigned int static_cluster_pwr_cost;
88 int notifier_sent;
89 bool wake_up_idle;
Syed Rameez Mustafa20acfe72017-01-30 09:35:46 +053090 u64 aggr_grp_load;
Satya Durga Srinivasu Prabhalac18b4032018-04-17 11:21:23 -070091 u64 coloc_boost_load;
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -080092};
93
Vikram Mulukutla4b54aae2017-03-20 13:41:37 -070094extern unsigned int sched_disable_window_stats;
Syed Rameez Mustafa20acfe72017-01-30 09:35:46 +053095
96extern struct timer_list sched_grp_timer;
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -080097#endif /* CONFIG_SCHED_WALT */
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -080098
99
Kirill Tkhaida0c1e62014-08-20 13:47:32 +0400100/* task_struct::on_rq states: */
101#define TASK_ON_RQ_QUEUED 1
Kirill Tkhaicca26e82014-08-20 13:47:42 +0400102#define TASK_ON_RQ_MIGRATING 2
Kirill Tkhaida0c1e62014-08-20 13:47:32 +0400103
Peter Zijlstra029632f2011-10-25 10:00:11 +0200104extern __read_mostly int scheduler_running;
105
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400106extern unsigned long calc_load_update;
107extern atomic_long_t calc_load_tasks;
108
Peter Zijlstra3289bdb2015-04-14 13:19:42 +0200109extern void calc_global_load_tick(struct rq *this_rq);
Thomas Gleixnerd60585c2016-07-12 18:33:56 +0200110extern long calc_load_fold_active(struct rq *this_rq, long adjust);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +0200111
112#ifdef CONFIG_SMP
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +0200113extern void cpu_load_update_active(struct rq *this_rq);
Peter Zijlstra3289bdb2015-04-14 13:19:42 +0200114#else
Frederic Weisbeckercee1afc2016-04-13 15:56:50 +0200115static inline void cpu_load_update_active(struct rq *this_rq) { }
Peter Zijlstra3289bdb2015-04-14 13:19:42 +0200116#endif
Paul Gortmaker45ceebf2013-04-19 15:10:49 -0400117
Greg Kroah-Hartmana0a93e32017-07-19 09:58:49 +0200118#ifdef CONFIG_SCHED_SMT
119extern void update_idle_core(struct rq *rq);
120#else
121static inline void update_idle_core(struct rq *rq) { }
122#endif
123
Peter Zijlstra029632f2011-10-25 10:00:11 +0200124/*
Peter Zijlstra029632f2011-10-25 10:00:11 +0200125 * Helpers for converting nanosecond timing to jiffy resolution
126 */
127#define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
128
Li Zefancc1f4b12013-03-05 16:06:09 +0800129/*
130 * Increase resolution of nice-level calculations for 64-bit architectures.
131 * The extra resolution improves shares distribution and load balancing of
132 * low-weight task groups (eg. nice +19 on an autogroup), deeper taskgroup
133 * hierarchies, especially on larger systems. This is not a user-visible change
134 * and does not change the user-interface for setting shares/weights.
135 *
136 * We increase resolution only if we have enough bits to allow this increased
Peter Zijlstra21591972016-04-28 12:49:38 +0200137 * resolution (i.e. 64bit). The costs for increasing resolution when 32bit are
138 * pretty high and the returns do not justify the increased costs.
139 *
140 * Really only required when CONFIG_FAIR_GROUP_SCHED is also set, but to
141 * increase coverage and consistency always enable it on 64bit platforms.
Li Zefancc1f4b12013-03-05 16:06:09 +0800142 */
Peter Zijlstra21591972016-04-28 12:49:38 +0200143#ifdef CONFIG_64BIT
Yuyang Du172895e2016-04-05 12:12:27 +0800144# define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT + SCHED_FIXEDPOINT_SHIFT)
Yuyang Du6ecdd742016-04-05 12:12:26 +0800145# define scale_load(w) ((w) << SCHED_FIXEDPOINT_SHIFT)
146# define scale_load_down(w) ((w) >> SCHED_FIXEDPOINT_SHIFT)
Li Zefancc1f4b12013-03-05 16:06:09 +0800147#else
Yuyang Du172895e2016-04-05 12:12:27 +0800148# define NICE_0_LOAD_SHIFT (SCHED_FIXEDPOINT_SHIFT)
Li Zefancc1f4b12013-03-05 16:06:09 +0800149# define scale_load(w) (w)
150# define scale_load_down(w) (w)
151#endif
152
Yuyang Du6ecdd742016-04-05 12:12:26 +0800153/*
Yuyang Du172895e2016-04-05 12:12:27 +0800154 * Task weight (visible to users) and its load (invisible to users) have
155 * independent resolution, but they should be well calibrated. We use
156 * scale_load() and scale_load_down(w) to convert between them. The
157 * following must be true:
158 *
159 * scale_load(sched_prio_to_weight[USER_PRIO(NICE_TO_PRIO(0))]) == NICE_0_LOAD
160 *
Yuyang Du6ecdd742016-04-05 12:12:26 +0800161 */
Yuyang Du172895e2016-04-05 12:12:27 +0800162#define NICE_0_LOAD (1L << NICE_0_LOAD_SHIFT)
Peter Zijlstra029632f2011-10-25 10:00:11 +0200163
164/*
Dario Faggioli332ac172013-11-07 14:43:45 +0100165 * Single value that decides SCHED_DEADLINE internal math precision.
166 * 10 -> just above 1us
167 * 9 -> just above 0.5us
168 */
169#define DL_SCALE (10)
170
171/*
Peter Zijlstra029632f2011-10-25 10:00:11 +0200172 * These are the 'tuning knobs' of the scheduler:
Peter Zijlstra029632f2011-10-25 10:00:11 +0200173 */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200174
175/*
176 * single value that denotes runtime == period, ie unlimited time.
177 */
178#define RUNTIME_INF ((u64)~0ULL)
179
Henrik Austad20f9cd22015-09-09 17:00:41 +0200180static inline int idle_policy(int policy)
181{
182 return policy == SCHED_IDLE;
183}
Dario Faggiolid50dde52013-11-07 14:43:36 +0100184static inline int fair_policy(int policy)
185{
186 return policy == SCHED_NORMAL || policy == SCHED_BATCH;
187}
188
Peter Zijlstra029632f2011-10-25 10:00:11 +0200189static inline int rt_policy(int policy)
190{
Dario Faggiolid50dde52013-11-07 14:43:36 +0100191 return policy == SCHED_FIFO || policy == SCHED_RR;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200192}
193
Dario Faggioliaab03e02013-11-28 11:14:43 +0100194static inline int dl_policy(int policy)
195{
196 return policy == SCHED_DEADLINE;
197}
Henrik Austad20f9cd22015-09-09 17:00:41 +0200198static inline bool valid_policy(int policy)
199{
200 return idle_policy(policy) || fair_policy(policy) ||
201 rt_policy(policy) || dl_policy(policy);
202}
Dario Faggioliaab03e02013-11-28 11:14:43 +0100203
Peter Zijlstra029632f2011-10-25 10:00:11 +0200204static inline int task_has_rt_policy(struct task_struct *p)
205{
206 return rt_policy(p->policy);
207}
208
Dario Faggioliaab03e02013-11-28 11:14:43 +0100209static inline int task_has_dl_policy(struct task_struct *p)
210{
211 return dl_policy(p->policy);
212}
213
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100214/*
215 * Tells if entity @a should preempt entity @b.
216 */
Dario Faggioli332ac172013-11-07 14:43:45 +0100217static inline bool
218dl_entity_preempt(struct sched_dl_entity *a, struct sched_dl_entity *b)
Dario Faggioli2d3d8912013-11-07 14:43:44 +0100219{
220 return dl_time_before(a->deadline, b->deadline);
221}
222
Peter Zijlstra029632f2011-10-25 10:00:11 +0200223/*
224 * This is the priority-queue data structure of the RT scheduling class:
225 */
226struct rt_prio_array {
227 DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
228 struct list_head queue[MAX_RT_PRIO];
229};
230
231struct rt_bandwidth {
232 /* nests inside the rq lock: */
233 raw_spinlock_t rt_runtime_lock;
234 ktime_t rt_period;
235 u64 rt_runtime;
236 struct hrtimer rt_period_timer;
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200237 unsigned int rt_period_active;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200238};
Juri Lellia5e7be32014-09-19 10:22:39 +0100239
240void __dl_clear_params(struct task_struct *p);
241
Dario Faggioli332ac172013-11-07 14:43:45 +0100242/*
243 * To keep the bandwidth of -deadline tasks and groups under control
244 * we need some place where:
245 * - store the maximum -deadline bandwidth of the system (the group);
246 * - cache the fraction of that bandwidth that is currently allocated.
247 *
248 * This is all done in the data structure below. It is similar to the
249 * one used for RT-throttling (rt_bandwidth), with the main difference
250 * that, since here we are only interested in admission control, we
251 * do not decrease any runtime while the group "executes", neither we
252 * need a timer to replenish it.
253 *
254 * With respect to SMP, the bandwidth is given on a per-CPU basis,
255 * meaning that:
256 * - dl_bw (< 100%) is the bandwidth of the system (group) on each CPU;
257 * - dl_total_bw array contains, in the i-eth element, the currently
258 * allocated bandwidth on the i-eth CPU.
259 * Moreover, groups consume bandwidth on each CPU, while tasks only
260 * consume bandwidth on the CPU they're running on.
261 * Finally, dl_total_bw_cpu is used to cache the index of dl_total_bw
262 * that will be shown the next time the proc or cgroup controls will
263 * be red. It on its turn can be changed by writing on its own
264 * control.
265 */
266struct dl_bandwidth {
267 raw_spinlock_t dl_runtime_lock;
268 u64 dl_runtime;
269 u64 dl_period;
270};
271
272static inline int dl_bandwidth_enabled(void)
273{
Peter Zijlstra17248132013-12-17 12:44:49 +0100274 return sysctl_sched_rt_runtime >= 0;
Dario Faggioli332ac172013-11-07 14:43:45 +0100275}
276
277extern struct dl_bw *dl_bw_of(int i);
278
279struct dl_bw {
280 raw_spinlock_t lock;
281 u64 bw, total_bw;
282};
283
Juri Lelli7f514122014-09-19 10:22:40 +0100284static inline
285void __dl_clear(struct dl_bw *dl_b, u64 tsk_bw)
286{
287 dl_b->total_bw -= tsk_bw;
288}
289
290static inline
291void __dl_add(struct dl_bw *dl_b, u64 tsk_bw)
292{
293 dl_b->total_bw += tsk_bw;
294}
295
296static inline
297bool __dl_overflow(struct dl_bw *dl_b, int cpus, u64 old_bw, u64 new_bw)
298{
299 return dl_b->bw != -1 &&
300 dl_b->bw * cpus < dl_b->total_bw - old_bw + new_bw;
301}
302
Peter Zijlstra029632f2011-10-25 10:00:11 +0200303extern struct mutex sched_domains_mutex;
304
305#ifdef CONFIG_CGROUP_SCHED
306
307#include <linux/cgroup.h>
Johannes Weiner3df0e592018-10-26 15:06:27 -0700308#include <linux/psi.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +0200309
310struct cfs_rq;
311struct rt_rq;
312
Mike Galbraith35cf4e52012-08-07 05:00:13 +0200313extern struct list_head task_groups;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200314
315struct cfs_bandwidth {
316#ifdef CONFIG_CFS_BANDWIDTH
317 raw_spinlock_t lock;
318 ktime_t period;
319 u64 quota, runtime;
Zhihui Zhang9c58c792014-09-20 21:24:36 -0400320 s64 hierarchical_quota;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200321 u64 runtime_expires;
322
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200323 int idle, period_active;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200324 struct hrtimer period_timer, slack_timer;
325 struct list_head throttled_cfs_rq;
326
327 /* statistics */
328 int nr_periods, nr_throttled;
329 u64 throttled_time;
Phil Auldbc1fccc2018-10-08 10:36:40 -0400330
331 bool distribute_running;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200332#endif
333};
334
335/* task group related information */
336struct task_group {
337 struct cgroup_subsys_state css;
338
339#ifdef CONFIG_FAIR_GROUP_SCHED
340 /* schedulable entities of this group on each cpu */
341 struct sched_entity **se;
342 /* runqueue "owned" by this group on each cpu */
343 struct cfs_rq **cfs_rq;
344 unsigned long shares;
345
Alex Shifa6bdde2013-06-20 10:18:46 +0800346#ifdef CONFIG_SMP
Waiman Longb0367622015-12-02 13:41:49 -0500347 /*
348 * load_avg can be heavily contended at clock tick time, so put
349 * it in its own cacheline separated from the fields above which
350 * will also be accessed at each tick.
351 */
352 atomic_long_t load_avg ____cacheline_aligned;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200353#endif
Alex Shifa6bdde2013-06-20 10:18:46 +0800354#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200355
356#ifdef CONFIG_RT_GROUP_SCHED
357 struct sched_rt_entity **rt_se;
358 struct rt_rq **rt_rq;
359
360 struct rt_bandwidth rt_bandwidth;
361#endif
362
363 struct rcu_head rcu;
364 struct list_head list;
365
366 struct task_group *parent;
367 struct list_head siblings;
368 struct list_head children;
369
370#ifdef CONFIG_SCHED_AUTOGROUP
371 struct autogroup *autogroup;
372#endif
373
374 struct cfs_bandwidth cfs_bandwidth;
375};
376
377#ifdef CONFIG_FAIR_GROUP_SCHED
378#define ROOT_TASK_GROUP_LOAD NICE_0_LOAD
379
380/*
381 * A weight of 0 or 1 can cause arithmetics problems.
382 * A weight of a cfs_rq is the sum of weights of which entities
383 * are queued on this cfs_rq, so a weight of a entity should not be
384 * too large, so as the shares value of a task group.
385 * (The default weight is 1024 - so there's no practical
386 * limitation from this.)
387 */
388#define MIN_SHARES (1UL << 1)
389#define MAX_SHARES (1UL << 18)
390#endif
391
Peter Zijlstra029632f2011-10-25 10:00:11 +0200392typedef int (*tg_visitor)(struct task_group *, void *);
393
394extern int walk_tg_tree_from(struct task_group *from,
395 tg_visitor down, tg_visitor up, void *data);
396
397/*
398 * Iterate the full tree, calling @down when first entering a node and @up when
399 * leaving it for the final time.
400 *
401 * Caller must hold rcu_lock or sufficient equivalent.
402 */
403static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
404{
405 return walk_tg_tree_from(&root_task_group, down, up, data);
406}
407
408extern int tg_nop(struct task_group *tg, void *data);
409
410extern void free_fair_sched_group(struct task_group *tg);
411extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
Peter Zijlstra8663e242016-06-22 14:58:02 +0200412extern void online_fair_sched_group(struct task_group *tg);
Peter Zijlstra6fe1f342016-01-21 22:24:16 +0100413extern void unregister_fair_sched_group(struct task_group *tg);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200414extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
415 struct sched_entity *se, int cpu,
416 struct sched_entity *parent);
417extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200418
419extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
Peter Zijlstra77a4d1a2015-04-15 11:41:57 +0200420extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200421extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
422
423extern void free_rt_sched_group(struct task_group *tg);
424extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
425extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
426 struct sched_rt_entity *rt_se, int cpu,
427 struct sched_rt_entity *parent);
428
Li Zefan25cc7da2013-03-05 16:07:33 +0800429extern struct task_group *sched_create_group(struct task_group *parent);
430extern void sched_online_group(struct task_group *tg,
431 struct task_group *parent);
432extern void sched_destroy_group(struct task_group *tg);
433extern void sched_offline_group(struct task_group *tg);
434
435extern void sched_move_task(struct task_struct *tsk);
436
437#ifdef CONFIG_FAIR_GROUP_SCHED
438extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
Byungchul Parkad936d82015-10-24 01:16:19 +0900439
440#ifdef CONFIG_SMP
441extern void set_task_rq_fair(struct sched_entity *se,
442 struct cfs_rq *prev, struct cfs_rq *next);
443#else /* !CONFIG_SMP */
444static inline void set_task_rq_fair(struct sched_entity *se,
445 struct cfs_rq *prev, struct cfs_rq *next) { }
446#endif /* CONFIG_SMP */
447#endif /* CONFIG_FAIR_GROUP_SCHED */
Li Zefan25cc7da2013-03-05 16:07:33 +0800448
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -0700449extern struct task_group *css_tg(struct cgroup_subsys_state *css);
450
Peter Zijlstra029632f2011-10-25 10:00:11 +0200451#else /* CONFIG_CGROUP_SCHED */
452
453struct cfs_bandwidth { };
454
455#endif /* CONFIG_CGROUP_SCHED */
456
457/* CFS-related fields in a runqueue */
458struct cfs_rq {
459 struct load_weight load;
Peter Zijlstrac82513e2012-04-26 13:12:27 +0200460 unsigned int nr_running, h_nr_running;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200461
462 u64 exec_clock;
463 u64 min_vruntime;
464#ifndef CONFIG_64BIT
465 u64 min_vruntime_copy;
466#endif
467
468 struct rb_root tasks_timeline;
469 struct rb_node *rb_leftmost;
470
Peter Zijlstra029632f2011-10-25 10:00:11 +0200471 /*
472 * 'curr' points to currently running entity on this cfs_rq.
473 * It is set to NULL otherwise (i.e when none are currently running).
474 */
475 struct sched_entity *curr, *next, *last, *skip;
476
477#ifdef CONFIG_SCHED_DEBUG
478 unsigned int nr_spread_over;
479#endif
480
Paul Turner2dac7542012-10-04 13:18:30 +0200481#ifdef CONFIG_SMP
482 /*
Yuyang Du9d89c252015-07-15 08:04:37 +0800483 * CFS load tracking
Paul Turner2dac7542012-10-04 13:18:30 +0200484 */
Yuyang Du9d89c252015-07-15 08:04:37 +0800485 struct sched_avg avg;
Yuyang Du13962232015-07-15 08:04:41 +0800486 u64 runnable_load_sum;
487 unsigned long runnable_load_avg;
Yuyang Du9d89c252015-07-15 08:04:37 +0800488#ifdef CONFIG_FAIR_GROUP_SCHED
489 unsigned long tg_load_avg_contrib;
Vincent Guittot96956e22016-11-08 10:53:44 +0100490 unsigned long propagate_avg;
Yuyang Du9d89c252015-07-15 08:04:37 +0800491#endif
492 atomic_long_t removed_load_avg, removed_util_avg;
493#ifndef CONFIG_64BIT
494 u64 load_last_update_time_copy;
495#endif
Alex Shi141965c2013-06-26 13:05:39 +0800496
Paul Turnerc566e8e2012-10-04 13:18:30 +0200497#ifdef CONFIG_FAIR_GROUP_SCHED
Paul Turner82958362012-10-04 13:18:31 +0200498 /*
499 * h_load = weight * f(tg)
500 *
501 * Where f(tg) is the recursive weight fraction assigned to
502 * this group.
503 */
504 unsigned long h_load;
Vladimir Davydov68520792013-07-15 17:49:19 +0400505 u64 last_h_load_update;
506 struct sched_entity *h_load_next;
507#endif /* CONFIG_FAIR_GROUP_SCHED */
Paul Turner82958362012-10-04 13:18:31 +0200508#endif /* CONFIG_SMP */
509
Peter Zijlstra029632f2011-10-25 10:00:11 +0200510#ifdef CONFIG_FAIR_GROUP_SCHED
511 struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
512
513 /*
514 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
515 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
516 * (like users, containers etc.)
517 *
518 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
519 * list is used during load balance.
520 */
521 int on_list;
522 struct list_head leaf_cfs_rq_list;
523 struct task_group *tg; /* group that "owns" this runqueue */
524
Peter Zijlstra029632f2011-10-25 10:00:11 +0200525#ifdef CONFIG_CFS_BANDWIDTH
Pavankumar Kondeti39c695e2017-07-20 16:05:51 +0530526
527#ifdef CONFIG_SCHED_WALT
528 struct walt_sched_stats walt_stats;
529#endif
530
Peter Zijlstra029632f2011-10-25 10:00:11 +0200531 int runtime_enabled;
532 u64 runtime_expires;
533 s64 runtime_remaining;
534
Paul Turnerf1b17282012-10-04 13:18:31 +0200535 u64 throttled_clock, throttled_clock_task;
536 u64 throttled_clock_task_time;
Peter Zijlstra55e16d32016-06-22 15:14:26 +0200537 int throttled, throttle_count;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200538 struct list_head throttled_list;
539#endif /* CONFIG_CFS_BANDWIDTH */
540#endif /* CONFIG_FAIR_GROUP_SCHED */
541};
542
543static inline int rt_bandwidth_enabled(void)
544{
545 return sysctl_sched_rt_runtime >= 0;
546}
547
Steven Rostedtb6366f02015-03-18 14:49:46 -0400548/* RT IPI pull logic requires IRQ_WORK */
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -0400549#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_SMP)
Steven Rostedtb6366f02015-03-18 14:49:46 -0400550# define HAVE_RT_PUSH_IPI
551#endif
552
Peter Zijlstra029632f2011-10-25 10:00:11 +0200553/* Real-Time classes' related field in a runqueue: */
554struct rt_rq {
555 struct rt_prio_array active;
Peter Zijlstrac82513e2012-04-26 13:12:27 +0200556 unsigned int rt_nr_running;
Frederic Weisbecker01d36d02015-11-04 18:17:10 +0100557 unsigned int rr_nr_running;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200558#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
559 struct {
560 int curr; /* highest queued rt task prio */
561#ifdef CONFIG_SMP
562 int next; /* next highest */
563#endif
564 } highest_prio;
565#endif
566#ifdef CONFIG_SMP
567 unsigned long rt_nr_migratory;
568 unsigned long rt_nr_total;
569 int overloaded;
570 struct plist_head pushable_tasks;
Steven Rostedtb6366f02015-03-18 14:49:46 -0400571#endif /* CONFIG_SMP */
Kirill Tkhaif4ebcbc2014-03-15 02:15:00 +0400572 int rt_queued;
573
Peter Zijlstra029632f2011-10-25 10:00:11 +0200574 int rt_throttled;
575 u64 rt_time;
576 u64 rt_runtime;
577 /* Nests inside the rq lock: */
578 raw_spinlock_t rt_runtime_lock;
579
580#ifdef CONFIG_RT_GROUP_SCHED
581 unsigned long rt_nr_boosted;
582
583 struct rq *rq;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200584 struct task_group *tg;
585#endif
586};
587
Dario Faggioliaab03e02013-11-28 11:14:43 +0100588/* Deadline class' related fields in a runqueue */
589struct dl_rq {
590 /* runqueue is an rbtree, ordered by deadline */
591 struct rb_root rb_root;
592 struct rb_node *rb_leftmost;
593
594 unsigned long dl_nr_running;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100595
596#ifdef CONFIG_SMP
597 /*
598 * Deadline values of the currently executing and the
599 * earliest ready task on this rq. Caching these facilitates
600 * the decision wether or not a ready but not running task
601 * should migrate somewhere else.
602 */
603 struct {
604 u64 curr;
605 u64 next;
606 } earliest_dl;
607
608 unsigned long dl_nr_migratory;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100609 int overloaded;
610
611 /*
612 * Tasks on this rq that can be pushed away. They are kept in
613 * an rb-tree, ordered by tasks' deadlines, with caching
614 * of the leftmost (earliest deadline) element.
615 */
616 struct rb_root pushable_dl_tasks_root;
617 struct rb_node *pushable_dl_tasks_leftmost;
Dario Faggioli332ac172013-11-07 14:43:45 +0100618#else
619 struct dl_bw dl_bw;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100620#endif
Dario Faggioliaab03e02013-11-28 11:14:43 +0100621};
622
Peter Zijlstra029632f2011-10-25 10:00:11 +0200623#ifdef CONFIG_SMP
624
Dietmar Eggemannbbb138b2015-09-26 18:19:54 +0100625struct max_cpu_capacity {
626 raw_spinlock_t lock;
627 unsigned long val;
628 int cpu;
629};
630
Peter Zijlstra029632f2011-10-25 10:00:11 +0200631/*
632 * We add the notion of a root-domain which will be used to define per-domain
633 * variables. Each exclusive cpuset essentially defines an island domain by
634 * fully partitioning the member cpus from any other cpuset. Whenever a new
635 * exclusive cpuset is created, we also create and attach a new root-domain
636 * object.
637 *
638 */
639struct root_domain {
640 atomic_t refcount;
641 atomic_t rto_count;
642 struct rcu_head rcu;
643 cpumask_var_t span;
644 cpumask_var_t online;
645
Tim Chen4486edd2014-06-23 12:16:49 -0700646 /* Indicate more than one runnable task for any CPU */
647 bool overload;
648
Morten Rasmussena562dfc2015-05-09 16:49:57 +0100649 /* Indicate one or more cpus over-utilized (tipping point) */
650 bool overutilized;
651
Peter Zijlstra029632f2011-10-25 10:00:11 +0200652 /*
Juri Lelli1baca4c2013-11-07 14:43:38 +0100653 * The bit corresponding to a CPU gets set here if such CPU has more
654 * than one runnable -deadline task (as it is below for RT tasks).
655 */
656 cpumask_var_t dlo_mask;
657 atomic_t dlo_count;
Dario Faggioli332ac172013-11-07 14:43:45 +0100658 struct dl_bw dl_bw;
Juri Lelli6bfd6d72013-11-07 14:43:47 +0100659 struct cpudl cpudl;
Juri Lelli1baca4c2013-11-07 14:43:38 +0100660
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -0400661#ifdef HAVE_RT_PUSH_IPI
662 /*
663 * For IPI pull requests, loop across the rto_mask.
664 */
665 struct irq_work rto_push_work;
666 raw_spinlock_t rto_lock;
667 /* These are only updated and read within rto_lock */
668 int rto_loop;
669 int rto_cpu;
670 /* These atomics are updated outside of a lock */
671 atomic_t rto_loop_next;
672 atomic_t rto_loop_start;
673#endif
Juri Lelli1baca4c2013-11-07 14:43:38 +0100674 /*
Peter Zijlstra029632f2011-10-25 10:00:11 +0200675 * The "RT overload" flag: it gets set if a CPU has more than
676 * one runnable RT task.
677 */
678 cpumask_var_t rto_mask;
679 struct cpupri cpupri;
Dietmar Eggemanncd92bfd2016-08-01 19:53:35 +0100680
Dietmar Eggemannbbb138b2015-09-26 18:19:54 +0100681 /* Maximum cpu capacity in the system. */
682 struct max_cpu_capacity max_cpu_capacity;
Dietmar Eggemann14774e72017-01-08 16:16:59 +0000683
684 /* First cpu with maximum and minimum original capacity */
685 int max_cap_orig_cpu, min_cap_orig_cpu;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200686};
687
688extern struct root_domain def_root_domain;
Steven Rostedt (VMware)a384e542018-01-23 20:45:38 -0500689extern void sched_get_rd(struct root_domain *rd);
690extern void sched_put_rd(struct root_domain *rd);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200691
Steven Rostedt (Red Hat)1c37ff72017-10-06 14:05:04 -0400692#ifdef HAVE_RT_PUSH_IPI
693extern void rto_push_irq_work_func(struct irq_work *work);
694#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200695#endif /* CONFIG_SMP */
696
697/*
698 * This is the main, per-CPU runqueue data structure.
699 *
700 * Locking rule: those places that want to lock multiple runqueues
701 * (such as the load balancing or the thread migration code), lock
702 * acquire operations must be ordered by ascending &runqueue.
703 */
704struct rq {
705 /* runqueue lock: */
706 raw_spinlock_t lock;
707
708 /*
709 * nr_running and cpu_load should be in the same cacheline because
710 * remote CPUs use both these fields when doing load calculation.
711 */
Peter Zijlstrac82513e2012-04-26 13:12:27 +0200712 unsigned int nr_running;
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +0100713#ifdef CONFIG_NUMA_BALANCING
714 unsigned int nr_numa_running;
715 unsigned int nr_preferred_running;
716#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200717 #define CPU_LOAD_IDX_MAX 5
718 unsigned long cpu_load[CPU_LOAD_IDX_MAX];
Morten Rasmussen4c6a8242016-02-25 12:47:54 +0000719 unsigned int misfit_task;
Frederic Weisbecker3451d022011-08-10 23:21:01 +0200720#ifdef CONFIG_NO_HZ_COMMON
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +0200721#ifdef CONFIG_SMP
722 unsigned long last_load_update_tick;
723#endif /* CONFIG_SMP */
Suresh Siddha1c792db2011-12-01 17:07:32 -0800724 unsigned long nohz_flags;
Frederic Weisbecker9fd81dd2016-04-19 17:36:51 +0200725#endif /* CONFIG_NO_HZ_COMMON */
Frederic Weisbecker265f22a2013-05-03 03:39:05 +0200726#ifdef CONFIG_NO_HZ_FULL
727 unsigned long last_sched_tick;
728#endif
Joseph Lo77501862013-04-22 14:39:18 +0800729
730#ifdef CONFIG_CPU_QUIET
731 /* time-based average load */
732 u64 nr_last_stamp;
733 u64 nr_running_integral;
734 seqcount_t ave_seqcnt;
735#endif
736
Peter Zijlstra029632f2011-10-25 10:00:11 +0200737 /* capture load from *all* tasks on this cpu: */
738 struct load_weight load;
739 unsigned long nr_load_updates;
740 u64 nr_switches;
741
742 struct cfs_rq cfs;
743 struct rt_rq rt;
Dario Faggioliaab03e02013-11-28 11:14:43 +0100744 struct dl_rq dl;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200745
746#ifdef CONFIG_FAIR_GROUP_SCHED
747 /* list of leaf cfs_rq on this cpu: */
748 struct list_head leaf_cfs_rq_list;
Vincent Guittot96956e22016-11-08 10:53:44 +0100749 struct list_head *tmp_alone_branch;
Peter Zijlstraa35b6462012-08-08 21:46:40 +0200750#endif /* CONFIG_FAIR_GROUP_SCHED */
751
Peter Zijlstra029632f2011-10-25 10:00:11 +0200752 /*
753 * This is part of a global counter where only the total sum
754 * over all CPUs matters. A task can increase this counter on
755 * one CPU and if it got migrated afterwards it may decrease
756 * it on another CPU. Always updated under the runqueue lock:
757 */
758 unsigned long nr_uninterruptible;
759
760 struct task_struct *curr, *idle, *stop;
761 unsigned long next_balance;
762 struct mm_struct *prev_mm;
763
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100764 unsigned int clock_skip_update;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200765 u64 clock;
766 u64 clock_task;
767
768 atomic_t nr_iowait;
769
770#ifdef CONFIG_SMP
771 struct root_domain *rd;
772 struct sched_domain *sd;
773
Nicolas Pitreced549f2014-05-26 18:19:38 -0400774 unsigned long cpu_capacity;
Vincent Guittotca6d75e2015-02-27 16:54:09 +0100775 unsigned long cpu_capacity_orig;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200776
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +0200777 struct callback_head *balance_callback;
778
Peter Zijlstra029632f2011-10-25 10:00:11 +0200779 unsigned char idle_balance;
780 /* For active balancing */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200781 int active_balance;
782 int push_cpu;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -0700783 struct task_struct *push_task;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200784 struct cpu_stop_work active_balance_work;
785 /* cpu of this runqueue: */
786 int cpu;
787 int online;
788
Peter Zijlstra367456c2012-02-20 21:49:09 +0100789 struct list_head cfs_tasks;
790
Peter Zijlstra029632f2011-10-25 10:00:11 +0200791 u64 rt_avg;
792 u64 age_stamp;
793 u64 idle_stamp;
794 u64 avg_idle;
Jason Low9bd721c2013-09-13 11:26:52 -0700795
796 /* This is used to determine avg_idle's max value */
797 u64 max_idle_balance_cost;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200798#endif
799
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -0800800#ifdef CONFIG_SCHED_WALT
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -0700801 struct sched_cluster *cluster;
802 struct cpumask freq_domain_cpumask;
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +0530803 struct walt_sched_stats walt_stats;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -0700804
805 int cstate, wakeup_latency, wakeup_energy;
806 u64 window_start;
Joonwoo Park84a80882017-02-03 11:15:31 -0800807 s64 cum_window_start;
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +0530808 unsigned long walt_flags;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -0700809
810 u64 cur_irqload;
811 u64 avg_irqload;
812 u64 irqload_ts;
813 unsigned int static_cpu_pwr_cost;
814 struct task_struct *ed_task;
815 struct cpu_cycle cc;
816 u64 old_busy_time, old_busy_time_group;
817 u64 old_estimated_time;
818 u64 curr_runnable_sum;
819 u64 prev_runnable_sum;
820 u64 nt_curr_runnable_sum;
821 u64 nt_prev_runnable_sum;
Joonwoo Park84a80882017-02-03 11:15:31 -0800822 u64 cum_window_demand;
Pavankumar Kondeti6deb2c42017-01-09 13:56:33 +0530823 struct group_cpu_time grp_time;
Syed Rameez Mustafa59b5fb72016-05-31 16:40:45 -0700824 struct load_subtractions load_subs[NUM_TRACKED_WINDOWS];
Syed Rameez Mustafa7ac74fc2016-06-07 15:18:37 -0700825 DECLARE_BITMAP_ARRAY(top_tasks_bitmap,
826 NUM_TRACKED_WINDOWS, NUM_LOAD_INDICES);
Syed Rameez Mustafa59b5fb72016-05-31 16:40:45 -0700827 u8 *top_tasks[NUM_TRACKED_WINDOWS];
828 u8 curr_table;
829 int prev_top;
830 int curr_top;
Pavankumar Kondeti4d5dd1c2018-03-19 10:47:09 +0530831 bool notif_pending;
Vikram Mulukutla77ecebb2017-05-30 14:38:55 -0700832 u64 last_cc_update;
833 u64 cycles;
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -0700834#endif
835
Peter Zijlstra029632f2011-10-25 10:00:11 +0200836#ifdef CONFIG_IRQ_TIME_ACCOUNTING
837 u64 prev_irq_time;
838#endif
839#ifdef CONFIG_PARAVIRT
840 u64 prev_steal_time;
841#endif
842#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
843 u64 prev_steal_time_rq;
844#endif
845
846 /* calc_load related fields */
847 unsigned long calc_load_update;
848 long calc_load_active;
849
850#ifdef CONFIG_SCHED_HRTICK
851#ifdef CONFIG_SMP
852 int hrtick_csd_pending;
853 struct call_single_data hrtick_csd;
854#endif
855 struct hrtimer hrtick_timer;
856#endif
857
858#ifdef CONFIG_SCHEDSTATS
859 /* latency stats */
860 struct sched_info rq_sched_info;
861 unsigned long long rq_cpu_time;
862 /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
863
864 /* sys_sched_yield() stats */
865 unsigned int yld_count;
866
867 /* schedule() stats */
Peter Zijlstra029632f2011-10-25 10:00:11 +0200868 unsigned int sched_count;
869 unsigned int sched_goidle;
870
871 /* try_to_wake_up() stats */
872 unsigned int ttwu_count;
873 unsigned int ttwu_local;
Chris Redpath385dcec2017-06-03 15:03:03 +0100874#ifdef CONFIG_SMP
Dietmar Eggemannaf88a162017-03-22 18:23:13 +0000875 struct eas_stats eas_stats;
Peter Zijlstra029632f2011-10-25 10:00:11 +0200876#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200877#endif
878
879#ifdef CONFIG_SMP
880 struct llist_head wake_list;
881#endif
Daniel Lezcano442bf3a2014-09-04 11:32:09 -0400882
883#ifdef CONFIG_CPU_IDLE
884 /* Must be inspected within a rcu lock section */
885 struct cpuidle_state *idle_state;
Morten Rasmussen06910642015-01-27 13:48:07 +0000886 int idle_state_idx;
Daniel Lezcano442bf3a2014-09-04 11:32:09 -0400887#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +0200888};
889
890static inline int cpu_of(struct rq *rq)
891{
892#ifdef CONFIG_SMP
893 return rq->cpu;
894#else
895 return 0;
896#endif
897}
898
Pranith Kumar8b06c552014-08-13 13:28:12 -0400899DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
Peter Zijlstra029632f2011-10-25 10:00:11 +0200900
Peter Zijlstra518cd622011-12-07 15:07:31 +0100901#define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500902#define this_rq() this_cpu_ptr(&runqueues)
Peter Zijlstra518cd622011-12-07 15:07:31 +0100903#define task_rq(p) cpu_rq(task_cpu(p))
904#define cpu_curr(cpu) (cpu_rq(cpu)->curr)
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500905#define raw_rq() raw_cpu_ptr(&runqueues)
Peter Zijlstra518cd622011-12-07 15:07:31 +0100906
Johannes Weiner089605b2018-10-26 15:06:19 -0700907extern void update_rq_clock(struct rq *rq);
908
Peter Zijlstracebde6d2015-01-05 11:18:10 +0100909static inline u64 __rq_clock_broken(struct rq *rq)
910{
Jason Low316c1608d2015-04-28 13:00:20 -0700911 return READ_ONCE(rq->clock);
Peter Zijlstracebde6d2015-01-05 11:18:10 +0100912}
913
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200914static inline u64 rq_clock(struct rq *rq)
915{
Peter Zijlstracebde6d2015-01-05 11:18:10 +0100916 lockdep_assert_held(&rq->lock);
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200917 return rq->clock;
918}
919
920static inline u64 rq_clock_task(struct rq *rq)
921{
Peter Zijlstracebde6d2015-01-05 11:18:10 +0100922 lockdep_assert_held(&rq->lock);
Frederic Weisbecker78becc22013-04-12 01:51:02 +0200923 return rq->clock_task;
924}
925
Peter Zijlstra9edfbfe2015-01-05 11:18:11 +0100926#define RQCF_REQ_SKIP 0x01
927#define RQCF_ACT_SKIP 0x02
928
929static inline void rq_clock_skip_update(struct rq *rq, bool skip)
930{
931 lockdep_assert_held(&rq->lock);
932 if (skip)
933 rq->clock_skip_update |= RQCF_REQ_SKIP;
934 else
935 rq->clock_skip_update &= ~RQCF_REQ_SKIP;
936}
937
Matt Fleming5a91d732016-09-21 14:38:10 +0100938struct rq_flags {
939 unsigned long flags;
940 struct pin_cookie cookie;
941};
942
943static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf)
944{
945 rf->cookie = lockdep_pin_lock(&rq->lock);
946}
947
948static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf)
949{
950 lockdep_unpin_lock(&rq->lock, rf->cookie);
951}
952
953static inline void rq_repin_lock(struct rq *rq, struct rq_flags *rf)
954{
955 lockdep_repin_lock(&rq->lock, rf->cookie);
956}
957
Johannes Weiner089605b2018-10-26 15:06:19 -0700958struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
959 __acquires(rq->lock);
960
961struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
962 __acquires(p->pi_lock)
963 __acquires(rq->lock);
964
965static inline void __task_rq_unlock(struct rq *rq, struct rq_flags *rf)
966 __releases(rq->lock)
967{
968 rq_unpin_lock(rq, rf);
969 raw_spin_unlock(&rq->lock);
970}
971
972static inline void
973task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
974 __releases(rq->lock)
975 __releases(p->pi_lock)
976{
977 rq_unpin_lock(rq, rf);
978 raw_spin_unlock(&rq->lock);
979 raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
980}
981
982static inline void
983rq_lock_irqsave(struct rq *rq, struct rq_flags *rf)
984 __acquires(rq->lock)
985{
986 raw_spin_lock_irqsave(&rq->lock, rf->flags);
987 rq_pin_lock(rq, rf);
988}
989
990static inline void
991rq_lock_irq(struct rq *rq, struct rq_flags *rf)
992 __acquires(rq->lock)
993{
994 raw_spin_lock_irq(&rq->lock);
995 rq_pin_lock(rq, rf);
996}
997
998static inline void
999rq_lock(struct rq *rq, struct rq_flags *rf)
1000 __acquires(rq->lock)
1001{
1002 raw_spin_lock(&rq->lock);
1003 rq_pin_lock(rq, rf);
1004}
1005
1006static inline void
1007rq_relock(struct rq *rq, struct rq_flags *rf)
1008 __acquires(rq->lock)
1009{
1010 raw_spin_lock(&rq->lock);
1011 rq_repin_lock(rq, rf);
1012}
1013
1014static inline void
1015rq_unlock_irqrestore(struct rq *rq, struct rq_flags *rf)
1016 __releases(rq->lock)
1017{
1018 rq_unpin_lock(rq, rf);
1019 raw_spin_unlock_irqrestore(&rq->lock, rf->flags);
1020}
1021
1022static inline void
1023rq_unlock_irq(struct rq *rq, struct rq_flags *rf)
1024 __releases(rq->lock)
1025{
1026 rq_unpin_lock(rq, rf);
1027 raw_spin_unlock_irq(&rq->lock);
1028}
1029
1030static inline void
1031rq_unlock(struct rq *rq, struct rq_flags *rf)
1032 __releases(rq->lock)
1033{
1034 rq_unpin_lock(rq, rf);
1035 raw_spin_unlock(&rq->lock);
1036}
1037
Johannes Weiner6a99eb12018-10-26 15:06:23 -07001038static inline struct rq *
1039this_rq_lock_irq(struct rq_flags *rf)
1040 __acquires(rq->lock)
1041{
1042 struct rq *rq;
1043
1044 local_irq_disable();
1045 rq = this_rq();
1046 rq_lock(rq, rf);
1047 return rq;
1048}
1049
Rik van Riel9942f792014-10-17 03:29:49 -04001050#ifdef CONFIG_NUMA
Rik van Riele3fe70b2014-10-17 03:29:50 -04001051enum numa_topology_type {
1052 NUMA_DIRECT,
1053 NUMA_GLUELESS_MESH,
1054 NUMA_BACKPLANE,
1055};
1056extern enum numa_topology_type sched_numa_topology_type;
Rik van Riel9942f792014-10-17 03:29:49 -04001057extern int sched_max_numa_distance;
1058extern bool find_numa_distance(int distance);
1059#endif
1060
Mel Gormanf809ca92013-10-07 11:28:57 +01001061#ifdef CONFIG_NUMA_BALANCING
Iulia Manda44dba3d2014-10-31 02:13:31 +02001062/* The regions in numa_faults array from task_struct */
1063enum numa_faults_stats {
1064 NUMA_MEM = 0,
1065 NUMA_CPU,
1066 NUMA_MEMBUF,
1067 NUMA_CPUBUF
1068};
Peter Zijlstra0ec8aa02013-10-07 11:29:33 +01001069extern void sched_setnuma(struct task_struct *p, int node);
Mel Gormane6628d52013-10-07 11:29:02 +01001070extern int migrate_task_to(struct task_struct *p, int cpu);
Mel Gormanf809ca92013-10-07 11:28:57 +01001071#endif /* CONFIG_NUMA_BALANCING */
Pavankumar Kondeti4e13d112018-01-25 01:12:08 +05301072extern int migrate_swap(struct task_struct *cur, struct task_struct *p);
Mel Gormanf809ca92013-10-07 11:28:57 +01001073
Peter Zijlstra518cd622011-12-07 15:07:31 +01001074#ifdef CONFIG_SMP
1075
Peter Zijlstrae3fca9e2015-06-11 14:46:37 +02001076static inline void
1077queue_balance_callback(struct rq *rq,
1078 struct callback_head *head,
1079 void (*func)(struct rq *rq))
1080{
1081 lockdep_assert_held(&rq->lock);
1082
1083 if (unlikely(head->next))
1084 return;
1085
1086 head->func = (void (*)(struct callback_head *))func;
1087 head->next = rq->balance_callback;
1088 rq->balance_callback = head;
1089}
1090
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001091extern void sched_ttwu_pending(void);
1092
Peter Zijlstra029632f2011-10-25 10:00:11 +02001093#define rcu_dereference_check_sched_domain(p) \
1094 rcu_dereference_check((p), \
1095 lockdep_is_held(&sched_domains_mutex))
1096
1097/*
1098 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
1099 * See detach_destroy_domains: synchronize_sched for details.
1100 *
1101 * The domain tree of any CPU may only be accessed from within
1102 * preempt-disabled sections.
1103 */
1104#define for_each_domain(cpu, __sd) \
Peter Zijlstra518cd622011-12-07 15:07:31 +01001105 for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
1106 __sd; __sd = __sd->parent)
Peter Zijlstra029632f2011-10-25 10:00:11 +02001107
Suresh Siddha77e81362011-11-17 11:08:23 -08001108#define for_each_lower_domain(sd) for (; sd; sd = sd->child)
1109
Peter Zijlstra518cd622011-12-07 15:07:31 +01001110/**
1111 * highest_flag_domain - Return highest sched_domain containing flag.
1112 * @cpu: The cpu whose highest level of sched domain is to
1113 * be returned.
1114 * @flag: The flag to check for the highest sched_domain
1115 * for the given cpu.
1116 *
1117 * Returns the highest sched_domain of a cpu which contains the given flag.
1118 */
1119static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
1120{
1121 struct sched_domain *sd, *hsd = NULL;
1122
1123 for_each_domain(cpu, sd) {
1124 if (!(sd->flags & flag))
1125 break;
1126 hsd = sd;
1127 }
1128
1129 return hsd;
1130}
1131
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001132static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
1133{
1134 struct sched_domain *sd;
1135
1136 for_each_domain(cpu, sd) {
1137 if (sd->flags & flag)
1138 break;
1139 }
1140
1141 return sd;
1142}
1143
Peter Zijlstra518cd622011-12-07 15:07:31 +01001144DECLARE_PER_CPU(struct sched_domain *, sd_llc);
Peter Zijlstra7d9ffa82013-07-04 12:56:46 +08001145DECLARE_PER_CPU(int, sd_llc_size);
Peter Zijlstra518cd622011-12-07 15:07:31 +01001146DECLARE_PER_CPU(int, sd_llc_id);
Peter Zijlstra0e369d72016-05-09 10:38:01 +02001147DECLARE_PER_CPU(struct sched_domain_shared *, sd_llc_shared);
Mel Gormanfb13c7e2013-10-07 11:29:17 +01001148DECLARE_PER_CPU(struct sched_domain *, sd_numa);
Preeti U Murthy37dc6b52013-10-30 08:42:52 +05301149DECLARE_PER_CPU(struct sched_domain *, sd_asym);
Morten Rasmussen30786a02015-01-02 17:08:52 +00001150DECLARE_PER_CPU(struct sched_domain *, sd_ea);
Morten Rasmussen61bf6252014-12-18 14:47:18 +00001151DECLARE_PER_CPU(struct sched_domain *, sd_scs);
Peter Zijlstra518cd622011-12-07 15:07:31 +01001152
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001153struct sched_group_capacity {
Li Zefan5e6521e2013-03-05 16:06:23 +08001154 atomic_t ref;
1155 /*
Yuyang Du172895e2016-04-05 12:12:27 +08001156 * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001157 * for a single CPU.
Li Zefan5e6521e2013-03-05 16:06:23 +08001158 */
Morten Rasmussen5cdeb5f2016-02-25 12:43:49 +00001159 unsigned long capacity;
1160 unsigned long max_capacity; /* Max per-cpu capacity in group */
Morten Rasmussen3d8cb902016-10-14 14:41:09 +01001161 unsigned long min_capacity; /* Min per-CPU capacity in group */
Li Zefan5e6521e2013-03-05 16:06:23 +08001162 unsigned long next_update;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001163 int imbalance; /* XXX unrelated to capacity but shared group state */
Li Zefan5e6521e2013-03-05 16:06:23 +08001164
1165 unsigned long cpumask[0]; /* iteration mask */
1166};
1167
1168struct sched_group {
1169 struct sched_group *next; /* Must be a circular list */
1170 atomic_t ref;
1171
1172 unsigned int group_weight;
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001173 struct sched_group_capacity *sgc;
Greg Hackmann2a3c6e62017-03-07 10:37:56 -08001174 const struct sched_group_energy *sge;
Li Zefan5e6521e2013-03-05 16:06:23 +08001175
1176 /*
1177 * The CPUs this group covers.
1178 *
1179 * NOTE: this field is variable length. (Allocated dynamically
1180 * by attaching extra space to the end of the structure,
1181 * depending on how many CPUs the kernel has booted up with)
1182 */
1183 unsigned long cpumask[0];
1184};
1185
1186static inline struct cpumask *sched_group_cpus(struct sched_group *sg)
1187{
1188 return to_cpumask(sg->cpumask);
1189}
1190
1191/*
1192 * cpumask masking which cpus in the group are allowed to iterate up the domain
1193 * tree.
1194 */
1195static inline struct cpumask *sched_group_mask(struct sched_group *sg)
1196{
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001197 return to_cpumask(sg->sgc->cpumask);
Li Zefan5e6521e2013-03-05 16:06:23 +08001198}
1199
1200/**
1201 * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
1202 * @group: The group whose first cpu is to be returned.
1203 */
1204static inline unsigned int group_first_cpu(struct sched_group *group)
1205{
1206 return cpumask_first(sched_group_cpus(group));
1207}
1208
Peter Zijlstrac1174872012-05-31 14:47:33 +02001209extern int group_balance_cpu(struct sched_group *sg);
1210
Steven Rostedt (Red Hat)3866e842016-02-22 16:26:51 -05001211#if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
1212void register_sched_domain_sysctl(void);
1213void unregister_sched_domain_sysctl(void);
1214#else
1215static inline void register_sched_domain_sysctl(void)
1216{
1217}
1218static inline void unregister_sched_domain_sysctl(void)
1219{
1220}
1221#endif
1222
Peter Zijlstrae3baac42014-06-04 10:31:18 -07001223#else
1224
1225static inline void sched_ttwu_pending(void) { }
1226
Peter Zijlstra518cd622011-12-07 15:07:31 +01001227#endif /* CONFIG_SMP */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001228
Peter Zijlstra391e43d2011-11-15 17:14:39 +01001229#include "stats.h"
1230#include "auto_group.h"
Peter Zijlstra029632f2011-10-25 10:00:11 +02001231
Pavankumar Kondeticbf7ea02017-01-11 15:11:23 +05301232enum sched_boost_policy {
1233 SCHED_BOOST_NONE,
1234 SCHED_BOOST_ON_BIG,
1235 SCHED_BOOST_ON_ALL,
1236};
1237
Pavankumar Kondeti39f82792018-04-03 14:45:06 +05301238#define NO_BOOST 0
1239#define FULL_THROTTLE_BOOST 1
1240#define CONSERVATIVE_BOOST 2
1241#define RESTRAINED_BOOST 3
1242
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07001243/*
1244 * Returns the rq capacity of any rq in a group. This does not play
1245 * well with groups where rq capacity can change independently.
1246 */
1247#define group_rq_capacity(group) cpu_capacity(group_first_cpu(group))
1248
Peter Zijlstra029632f2011-10-25 10:00:11 +02001249#ifdef CONFIG_CGROUP_SCHED
1250
1251/*
1252 * Return the group to which this tasks belongs.
1253 *
Tejun Heo8af01f52013-08-08 20:11:22 -04001254 * We cannot use task_css() and friends because the cgroup subsystem
1255 * changes that value before the cgroup_subsys::attach() method is called,
1256 * therefore we cannot pin it and might observe the wrong value.
Peter Zijlstra8323f262012-06-22 13:36:05 +02001257 *
1258 * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
1259 * core changes this before calling sched_move_task().
1260 *
1261 * Instead we use a 'copy' which is updated from sched_move_task() while
1262 * holding both task_struct::pi_lock and rq::lock.
Peter Zijlstra029632f2011-10-25 10:00:11 +02001263 */
1264static inline struct task_group *task_group(struct task_struct *p)
1265{
Peter Zijlstra8323f262012-06-22 13:36:05 +02001266 return p->sched_task_group;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001267}
1268
1269/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
1270static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
1271{
1272#if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
1273 struct task_group *tg = task_group(p);
1274#endif
1275
1276#ifdef CONFIG_FAIR_GROUP_SCHED
Byungchul Parkad936d82015-10-24 01:16:19 +09001277 set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001278 p->se.cfs_rq = tg->cfs_rq[cpu];
1279 p->se.parent = tg->se[cpu];
1280#endif
1281
1282#ifdef CONFIG_RT_GROUP_SCHED
1283 p->rt.rt_rq = tg->rt_rq[cpu];
1284 p->rt.parent = tg->rt_se[cpu];
1285#endif
1286}
1287
1288#else /* CONFIG_CGROUP_SCHED */
1289
1290static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
1291static inline struct task_group *task_group(struct task_struct *p)
1292{
1293 return NULL;
1294}
1295
1296#endif /* CONFIG_CGROUP_SCHED */
1297
1298static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
1299{
1300 set_task_rq(p, cpu);
1301#ifdef CONFIG_SMP
1302 /*
1303 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
1304 * successfuly executed on another CPU. We must ensure that updates of
1305 * per-task data have been completed by this moment.
1306 */
1307 smp_wmb();
Andy Lutomirskic65eacb2016-09-13 14:29:24 -07001308#ifdef CONFIG_THREAD_INFO_IN_TASK
1309 p->cpu = cpu;
1310#else
Peter Zijlstra029632f2011-10-25 10:00:11 +02001311 task_thread_info(p)->cpu = cpu;
Andy Lutomirskic65eacb2016-09-13 14:29:24 -07001312#endif
Peter Zijlstraac66f542013-10-07 11:29:16 +01001313 p->wake_cpu = cpu;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001314#endif
1315}
1316
1317/*
1318 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
1319 */
1320#ifdef CONFIG_SCHED_DEBUG
Ingo Molnarc5905af2012-02-24 08:31:31 +01001321# include <linux/static_key.h>
Peter Zijlstra029632f2011-10-25 10:00:11 +02001322# define const_debug __read_mostly
1323#else
1324# define const_debug const
1325#endif
1326
1327extern const_debug unsigned int sysctl_sched_features;
1328
1329#define SCHED_FEAT(name, enabled) \
1330 __SCHED_FEAT_##name ,
1331
1332enum {
Peter Zijlstra391e43d2011-11-15 17:14:39 +01001333#include "features.h"
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001334 __SCHED_FEAT_NR,
Peter Zijlstra029632f2011-10-25 10:00:11 +02001335};
1336
1337#undef SCHED_FEAT
1338
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001339#if defined(CONFIG_SCHED_DEBUG) && defined(HAVE_JUMP_LABEL)
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001340#define SCHED_FEAT(name, enabled) \
Ingo Molnarc5905af2012-02-24 08:31:31 +01001341static __always_inline bool static_branch_##name(struct static_key *key) \
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001342{ \
Jason Baron6e76ea82014-07-02 15:52:41 +00001343 return static_key_##enabled(key); \
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001344}
1345
1346#include "features.h"
1347
1348#undef SCHED_FEAT
1349
Ingo Molnarc5905af2012-02-24 08:31:31 +01001350extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001351#define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
1352#else /* !(SCHED_DEBUG && HAVE_JUMP_LABEL) */
Peter Zijlstrae3a02422018-08-29 11:45:21 +02001353#define sched_feat(x) !!(sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
Peter Zijlstraf8b6d1c2011-07-06 14:20:14 +02001354#endif /* SCHED_DEBUG && HAVE_JUMP_LABEL */
Peter Zijlstra029632f2011-10-25 10:00:11 +02001355
Srikar Dronamraju2a595722015-08-11 21:54:21 +05301356extern struct static_key_false sched_numa_balancing;
Mel Gormancb251762016-02-05 09:08:36 +00001357extern struct static_key_false sched_schedstats;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001358
Peter Zijlstra029632f2011-10-25 10:00:11 +02001359static inline u64 global_rt_period(void)
1360{
1361 return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
1362}
1363
1364static inline u64 global_rt_runtime(void)
1365{
1366 if (sysctl_sched_rt_runtime < 0)
1367 return RUNTIME_INF;
1368
1369 return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
1370}
1371
Peter Zijlstra029632f2011-10-25 10:00:11 +02001372static inline int task_current(struct rq *rq, struct task_struct *p)
1373{
1374 return rq->curr == p;
1375}
1376
1377static inline int task_running(struct rq *rq, struct task_struct *p)
1378{
1379#ifdef CONFIG_SMP
1380 return p->on_cpu;
1381#else
1382 return task_current(rq, p);
1383#endif
1384}
1385
Kirill Tkhaida0c1e62014-08-20 13:47:32 +04001386static inline int task_on_rq_queued(struct task_struct *p)
1387{
1388 return p->on_rq == TASK_ON_RQ_QUEUED;
1389}
Peter Zijlstra029632f2011-10-25 10:00:11 +02001390
Kirill Tkhaicca26e82014-08-20 13:47:42 +04001391static inline int task_on_rq_migrating(struct task_struct *p)
1392{
1393 return p->on_rq == TASK_ON_RQ_MIGRATING;
1394}
1395
Peter Zijlstra029632f2011-10-25 10:00:11 +02001396#ifndef prepare_arch_switch
1397# define prepare_arch_switch(next) do { } while (0)
1398#endif
Catalin Marinas01f23e12011-11-27 21:43:10 +00001399#ifndef finish_arch_post_lock_switch
1400# define finish_arch_post_lock_switch() do { } while (0)
1401#endif
Peter Zijlstra029632f2011-10-25 10:00:11 +02001402
Peter Zijlstra029632f2011-10-25 10:00:11 +02001403static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
1404{
1405#ifdef CONFIG_SMP
1406 /*
1407 * We can optimise this out completely for !SMP, because the
1408 * SMP rebalancing from interrupt is the only thing that cares
1409 * here.
1410 */
1411 next->on_cpu = 1;
1412#endif
1413}
1414
1415static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
1416{
1417#ifdef CONFIG_SMP
1418 /*
1419 * After ->on_cpu is cleared, the task can be moved to a different CPU.
1420 * We must ensure this doesn't happen until the switch is completely
1421 * finished.
Peter Zijlstra95913d92015-09-29 14:45:09 +02001422 *
Peter Zijlstrab75a2252015-10-06 14:36:17 +02001423 * In particular, the load of prev->state in finish_task_switch() must
1424 * happen before this.
1425 *
Peter Zijlstra1f03e8d2016-04-04 10:57:12 +02001426 * Pairs with the smp_cond_load_acquire() in try_to_wake_up().
Peter Zijlstra029632f2011-10-25 10:00:11 +02001427 */
Peter Zijlstra95913d92015-09-29 14:45:09 +02001428 smp_store_release(&prev->on_cpu, 0);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001429#endif
1430#ifdef CONFIG_DEBUG_SPINLOCK
1431 /* this is a valid case when another task releases the spinlock */
1432 rq->lock.owner = current;
1433#endif
1434 /*
1435 * If we are tracking spinlock dependencies then we have to
1436 * fix up the runqueue lock - which gets 'carried over' from
1437 * prev into current:
1438 */
1439 spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
1440
1441 raw_spin_unlock_irq(&rq->lock);
1442}
1443
Li Zefanb13095f2013-03-05 16:06:38 +08001444/*
1445 * wake flags
1446 */
1447#define WF_SYNC 0x01 /* waker goes to sleep after wakeup */
1448#define WF_FORK 0x02 /* child wakeup after fork */
1449#define WF_MIGRATED 0x4 /* internal use, task got migrated */
1450
Peter Zijlstra029632f2011-10-25 10:00:11 +02001451/*
1452 * To aid in avoiding the subversion of "niceness" due to uneven distribution
1453 * of tasks with abnormal "nice" values across CPUs the contribution that
1454 * each task makes to its run queue's load is weighted according to its
1455 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
1456 * scaled version of the new time slice allocation that they receive on time
1457 * slice expiry etc.
1458 */
1459
1460#define WEIGHT_IDLEPRIO 3
1461#define WMULT_IDLEPRIO 1431655765
1462
Andi Kleened82b8a2015-11-29 20:59:43 -08001463extern const int sched_prio_to_weight[40];
1464extern const u32 sched_prio_to_wmult[40];
Peter Zijlstra029632f2011-10-25 10:00:11 +02001465
Peter Zijlstraff77e462016-01-18 15:27:07 +01001466/*
1467 * {de,en}queue flags:
1468 *
1469 * DEQUEUE_SLEEP - task is no longer runnable
1470 * ENQUEUE_WAKEUP - task just became runnable
1471 *
1472 * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks
1473 * are in a known state which allows modification. Such pairs
1474 * should preserve as much state as possible.
1475 *
1476 * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location
1477 * in the runqueue.
1478 *
1479 * ENQUEUE_HEAD - place at front of runqueue (tail if not specified)
1480 * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline)
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02001481 * ENQUEUE_MIGRATED - the task was migrated during wakeup
Peter Zijlstraff77e462016-01-18 15:27:07 +01001482 *
1483 */
1484
1485#define DEQUEUE_SLEEP 0x01
1486#define DEQUEUE_SAVE 0x02 /* matches ENQUEUE_RESTORE */
1487#define DEQUEUE_MOVE 0x04 /* matches ENQUEUE_MOVE */
1488
Peter Zijlstra1de64442015-09-30 17:44:13 +02001489#define ENQUEUE_WAKEUP 0x01
Peter Zijlstraff77e462016-01-18 15:27:07 +01001490#define ENQUEUE_RESTORE 0x02
1491#define ENQUEUE_MOVE 0x04
1492
1493#define ENQUEUE_HEAD 0x08
1494#define ENQUEUE_REPLENISH 0x10
Li Zefanc82ba9f2013-03-05 16:06:55 +08001495#ifdef CONFIG_SMP
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02001496#define ENQUEUE_MIGRATED 0x20
Li Zefanc82ba9f2013-03-05 16:06:55 +08001497#else
Peter Zijlstra59efa0b2016-05-10 18:24:37 +02001498#define ENQUEUE_MIGRATED 0x00
Li Zefanc82ba9f2013-03-05 16:06:55 +08001499#endif
Juri Lelli43aac892015-06-26 12:14:23 +01001500#define ENQUEUE_WAKEUP_NEW 0x40
Li Zefanc82ba9f2013-03-05 16:06:55 +08001501
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001502#define RETRY_TASK ((void *)-1UL)
1503
Li Zefanc82ba9f2013-03-05 16:06:55 +08001504struct sched_class {
1505 const struct sched_class *next;
1506
1507 void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
1508 void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
1509 void (*yield_task) (struct rq *rq);
1510 bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt);
1511
1512 void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags);
1513
Peter Zijlstra606dba22012-02-11 06:05:00 +01001514 /*
1515 * It is the responsibility of the pick_next_task() method that will
1516 * return the next task to call put_prev_task() on the @prev task or
1517 * something equivalent.
Peter Zijlstra37e117c2014-02-14 12:25:08 +01001518 *
1519 * May return RETRY_TASK when it finds a higher prio class has runnable
1520 * tasks.
Peter Zijlstra606dba22012-02-11 06:05:00 +01001521 */
1522 struct task_struct * (*pick_next_task) (struct rq *rq,
Peter Zijlstrae7904a22015-08-01 19:25:08 +02001523 struct task_struct *prev,
Matt Fleming5a91d732016-09-21 14:38:10 +01001524 struct rq_flags *rf);
Li Zefanc82ba9f2013-03-05 16:06:55 +08001525 void (*put_prev_task) (struct rq *rq, struct task_struct *p);
1526
1527#ifdef CONFIG_SMP
Peter Zijlstraac66f542013-10-07 11:29:16 +01001528 int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);
xiaofeng.yan5a4fd032015-09-23 14:55:59 +08001529 void (*migrate_task_rq)(struct task_struct *p);
Li Zefanc82ba9f2013-03-05 16:06:55 +08001530
Li Zefanc82ba9f2013-03-05 16:06:55 +08001531 void (*task_woken) (struct rq *this_rq, struct task_struct *task);
1532
1533 void (*set_cpus_allowed)(struct task_struct *p,
1534 const struct cpumask *newmask);
1535
1536 void (*rq_online)(struct rq *rq);
1537 void (*rq_offline)(struct rq *rq);
1538#endif
1539
1540 void (*set_curr_task) (struct rq *rq);
1541 void (*task_tick) (struct rq *rq, struct task_struct *p, int queued);
1542 void (*task_fork) (struct task_struct *p);
Dario Faggiolie6c390f2013-11-07 14:43:35 +01001543 void (*task_dead) (struct task_struct *p);
Li Zefanc82ba9f2013-03-05 16:06:55 +08001544
Kirill Tkhai67dfa1b2014-10-27 17:40:52 +03001545 /*
1546 * The switched_from() call is allowed to drop rq->lock, therefore we
1547 * cannot assume the switched_from/switched_to pair is serliazed by
1548 * rq->lock. They are however serialized by p->pi_lock.
1549 */
Li Zefanc82ba9f2013-03-05 16:06:55 +08001550 void (*switched_from) (struct rq *this_rq, struct task_struct *task);
1551 void (*switched_to) (struct rq *this_rq, struct task_struct *task);
1552 void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
1553 int oldprio);
1554
1555 unsigned int (*get_rr_interval) (struct rq *rq,
1556 struct task_struct *task);
1557
Stanislaw Gruszka6e998912014-11-12 16:58:44 +01001558 void (*update_curr) (struct rq *rq);
1559
Vincent Guittotea86cb42016-06-17 13:38:55 +02001560#define TASK_SET_GROUP 0
1561#define TASK_MOVE_GROUP 1
1562
Li Zefanc82ba9f2013-03-05 16:06:55 +08001563#ifdef CONFIG_FAIR_GROUP_SCHED
Vincent Guittotea86cb42016-06-17 13:38:55 +02001564 void (*task_change_group) (struct task_struct *p, int type);
Li Zefanc82ba9f2013-03-05 16:06:55 +08001565#endif
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -08001566#ifdef CONFIG_SCHED_WALT
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +05301567 void (*fixup_walt_sched_stats)(struct rq *rq, struct task_struct *p,
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07001568 u32 new_task_load, u32 new_pred_demand);
Pavankumar Kondeti1c847af2019-09-04 10:08:32 +05301569 void (*fixup_cumulative_runnable_avg)(struct rq *rq,
1570 struct task_struct *task,
1571 u64 new_task_load);
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07001572#endif
Li Zefanc82ba9f2013-03-05 16:06:55 +08001573};
Peter Zijlstra029632f2011-10-25 10:00:11 +02001574
Peter Zijlstra3f1d2a32014-02-12 10:49:30 +01001575static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
1576{
1577 prev->sched_class->put_prev_task(rq, prev);
1578}
1579
Peter Zijlstrab2bf6c32016-09-20 22:00:38 +02001580static inline void set_curr_task(struct rq *rq, struct task_struct *curr)
1581{
1582 curr->sched_class->set_curr_task(rq);
1583}
1584
Peter Zijlstra029632f2011-10-25 10:00:11 +02001585#define sched_class_highest (&stop_sched_class)
1586#define for_each_class(class) \
1587 for (class = sched_class_highest; class; class = class->next)
1588
1589extern const struct sched_class stop_sched_class;
Dario Faggioliaab03e02013-11-28 11:14:43 +01001590extern const struct sched_class dl_sched_class;
Peter Zijlstra029632f2011-10-25 10:00:11 +02001591extern const struct sched_class rt_sched_class;
1592extern const struct sched_class fair_sched_class;
1593extern const struct sched_class idle_sched_class;
1594
1595
1596#ifdef CONFIG_SMP
1597
Patrick Bellasi2178e842016-07-22 11:35:59 +01001598extern void init_max_cpu_capacity(struct max_cpu_capacity *mcc);
Nicolas Pitre63b2ca32014-05-26 18:19:37 -04001599extern void update_group_capacity(struct sched_domain *sd, int cpu);
Li Zefanb7192032013-03-07 10:00:26 +08001600
Daniel Lezcano7caff662014-01-06 12:34:38 +01001601extern void trigger_load_balance(struct rq *rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001602
Peter Zijlstrac5b28032015-05-15 17:43:35 +02001603extern void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask);
1604
Pavankumar Kondeti25ba1fa2018-01-31 16:36:39 +05301605bool __cpu_overutilized(int cpu, int delta);
Joonwoo Parkdc3420d2017-01-31 11:14:43 -08001606bool cpu_overutilized(int cpu);
1607
Peter Zijlstra029632f2011-10-25 10:00:11 +02001608#endif
1609
Daniel Lezcano442bf3a2014-09-04 11:32:09 -04001610#ifdef CONFIG_CPU_IDLE
1611static inline void idle_set_state(struct rq *rq,
1612 struct cpuidle_state *idle_state)
1613{
1614 rq->idle_state = idle_state;
1615}
1616
1617static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1618{
Peter Zijlstra9148a3a2016-09-20 22:34:51 +02001619 SCHED_WARN_ON(!rcu_read_lock_held());
Daniel Lezcano442bf3a2014-09-04 11:32:09 -04001620 return rq->idle_state;
1621}
Morten Rasmussen06910642015-01-27 13:48:07 +00001622
1623static inline void idle_set_state_idx(struct rq *rq, int idle_state_idx)
1624{
1625 rq->idle_state_idx = idle_state_idx;
1626}
1627
1628static inline int idle_get_state_idx(struct rq *rq)
1629{
1630 WARN_ON(!rcu_read_lock_held());
Pavankumar Kondetic3dae852017-06-19 15:30:11 +05301631
1632 if (rq->nr_running || cpu_of(rq) == raw_smp_processor_id())
1633 return -1;
1634
Morten Rasmussen06910642015-01-27 13:48:07 +00001635 return rq->idle_state_idx;
1636}
Daniel Lezcano442bf3a2014-09-04 11:32:09 -04001637#else
1638static inline void idle_set_state(struct rq *rq,
1639 struct cpuidle_state *idle_state)
1640{
1641}
1642
1643static inline struct cpuidle_state *idle_get_state(struct rq *rq)
1644{
1645 return NULL;
1646}
Morten Rasmussen06910642015-01-27 13:48:07 +00001647
1648static inline void idle_set_state_idx(struct rq *rq, int idle_state_idx)
1649{
1650}
1651
1652static inline int idle_get_state_idx(struct rq *rq)
1653{
1654 return -1;
1655}
Daniel Lezcano442bf3a2014-09-04 11:32:09 -04001656#endif
1657
Peter Zijlstra029632f2011-10-25 10:00:11 +02001658extern void sysrq_sched_debug_show(void);
1659extern void sched_init_granularity(void);
1660extern void update_max_interval(void);
Juri Lelli1baca4c2013-11-07 14:43:38 +01001661
1662extern void init_sched_dl_class(void);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001663extern void init_sched_rt_class(void);
1664extern void init_sched_fair_class(void);
1665
Kirill Tkhai88751252014-06-29 00:03:57 +04001666extern void resched_curr(struct rq *rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001667extern void resched_cpu(int cpu);
1668
1669extern struct rt_bandwidth def_rt_bandwidth;
1670extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
1671
Dario Faggioli332ac172013-11-07 14:43:45 +01001672extern struct dl_bandwidth def_dl_bandwidth;
1673extern void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime);
Dario Faggioliaab03e02013-11-28 11:14:43 +01001674extern void init_dl_task_timer(struct sched_dl_entity *dl_se);
1675
Dario Faggioli332ac172013-11-07 14:43:45 +01001676unsigned long to_ratio(u64 period, u64 runtime);
1677
Yuyang Du540247f2015-07-15 08:04:39 +08001678extern void init_entity_runnable_average(struct sched_entity *se);
Yuyang Du2b8c41d2016-03-30 04:30:56 +08001679extern void post_init_entity_util_avg(struct sched_entity *se);
Alex Shia75cdaa2013-06-20 10:18:47 +08001680
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +02001681#ifdef CONFIG_NO_HZ_FULL
1682extern bool sched_can_stop_tick(struct rq *rq);
1683
1684/*
1685 * Tick may be needed by tasks in the runqueue depending on their policy and
1686 * requirements. If tick is needed, lets send the target an IPI to kick it out of
1687 * nohz mode if necessary.
1688 */
1689static inline void sched_update_tick_dependency(struct rq *rq)
1690{
1691 int cpu;
1692
1693 if (!tick_nohz_full_enabled())
1694 return;
1695
1696 cpu = cpu_of(rq);
1697
1698 if (!tick_nohz_full_cpu(cpu))
1699 return;
1700
1701 if (sched_can_stop_tick(rq))
1702 tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED);
1703 else
1704 tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED);
1705}
1706#else
1707static inline void sched_update_tick_dependency(struct rq *rq) { }
1708#endif
1709
Joseph Lo77501862013-04-22 14:39:18 +08001710static inline void __add_nr_running(struct rq *rq, unsigned count)
Peter Zijlstra029632f2011-10-25 10:00:11 +02001711{
Kirill Tkhai72465442014-05-09 03:00:14 +04001712 unsigned prev_nr = rq->nr_running;
1713
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07001714 sched_update_nr_prod(cpu_of(rq), count, true);
Kirill Tkhai72465442014-05-09 03:00:14 +04001715 rq->nr_running = prev_nr + count;
Frederic Weisbecker9f3660c2013-04-20 14:35:09 +02001716
Kirill Tkhai72465442014-05-09 03:00:14 +04001717 if (prev_nr < 2 && rq->nr_running >= 2) {
Tim Chen4486edd2014-06-23 12:16:49 -07001718#ifdef CONFIG_SMP
1719 if (!rq->rd->overload)
1720 rq->rd->overload = true;
1721#endif
Tim Chen4486edd2014-06-23 12:16:49 -07001722 }
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +02001723
1724 sched_update_tick_dependency(rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001725}
1726
Joseph Lo77501862013-04-22 14:39:18 +08001727static inline void __sub_nr_running(struct rq *rq, unsigned count)
Peter Zijlstra029632f2011-10-25 10:00:11 +02001728{
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07001729 sched_update_nr_prod(cpu_of(rq), count, false);
Kirill Tkhai72465442014-05-09 03:00:14 +04001730 rq->nr_running -= count;
Frederic Weisbecker76d92ac2015-07-17 22:25:49 +02001731 /* Check if we still need preemption */
1732 sched_update_tick_dependency(rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02001733}
1734
Joseph Lo77501862013-04-22 14:39:18 +08001735#ifdef CONFIG_CPU_QUIET
1736#define NR_AVE_SCALE(x) ((x) << FSHIFT)
1737static inline u64 do_nr_running_integral(struct rq *rq)
1738{
1739 s64 nr, deltax;
1740 u64 nr_running_integral = rq->nr_running_integral;
1741
1742 deltax = rq->clock_task - rq->nr_last_stamp;
1743 nr = NR_AVE_SCALE(rq->nr_running);
1744
1745 nr_running_integral += nr * deltax;
1746
1747 return nr_running_integral;
1748}
1749
1750static inline void add_nr_running(struct rq *rq, unsigned count)
1751{
1752 write_seqcount_begin(&rq->ave_seqcnt);
1753 rq->nr_running_integral = do_nr_running_integral(rq);
1754 rq->nr_last_stamp = rq->clock_task;
1755 __add_nr_running(rq, count);
1756 write_seqcount_end(&rq->ave_seqcnt);
1757}
1758
1759static inline void sub_nr_running(struct rq *rq, unsigned count)
1760{
1761 write_seqcount_begin(&rq->ave_seqcnt);
1762 rq->nr_running_integral = do_nr_running_integral(rq);
1763 rq->nr_last_stamp = rq->clock_task;
1764 __sub_nr_running(rq, count);
1765 write_seqcount_end(&rq->ave_seqcnt);
1766}
1767#else
1768#define add_nr_running __add_nr_running
1769#define sub_nr_running __sub_nr_running
1770#endif
1771
Frederic Weisbecker265f22a2013-05-03 03:39:05 +02001772static inline void rq_last_tick_reset(struct rq *rq)
1773{
1774#ifdef CONFIG_NO_HZ_FULL
1775 rq->last_sched_tick = jiffies;
1776#endif
1777}
1778
Peter Zijlstra029632f2011-10-25 10:00:11 +02001779extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
1780extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
1781
1782extern void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags);
1783
1784extern const_debug unsigned int sysctl_sched_time_avg;
1785extern const_debug unsigned int sysctl_sched_nr_migrate;
1786extern const_debug unsigned int sysctl_sched_migration_cost;
1787
1788static inline u64 sched_avg_period(void)
1789{
1790 return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
1791}
1792
Peter Zijlstra029632f2011-10-25 10:00:11 +02001793#ifdef CONFIG_SCHED_HRTICK
1794
1795/*
1796 * Use hrtick when:
1797 * - enabled by features
1798 * - hrtimer is actually high res
1799 */
1800static inline int hrtick_enabled(struct rq *rq)
1801{
1802 if (!sched_feat(HRTICK))
1803 return 0;
1804 if (!cpu_active(cpu_of(rq)))
1805 return 0;
1806 return hrtimer_is_hres_active(&rq->hrtick_timer);
1807}
1808
1809void hrtick_start(struct rq *rq, u64 delay);
1810
Mike Galbraithb39e66e2011-11-22 15:20:07 +01001811#else
1812
1813static inline int hrtick_enabled(struct rq *rq)
1814{
1815 return 0;
1816}
1817
Peter Zijlstra029632f2011-10-25 10:00:11 +02001818#endif /* CONFIG_SCHED_HRTICK */
1819
1820#ifdef CONFIG_SMP
1821extern void sched_avg_update(struct rq *rq);
Peter Zijlstradfbca412015-03-23 14:19:05 +01001822
1823#ifndef arch_scale_freq_capacity
1824static __always_inline
1825unsigned long arch_scale_freq_capacity(struct sched_domain *sd, int cpu)
1826{
1827 return SCHED_CAPACITY_SCALE;
1828}
1829#endif
Vincent Guittotb5b48602015-02-27 16:54:08 +01001830
Dietmar Eggemann29db1b42017-07-13 09:48:42 +01001831#ifndef arch_scale_max_freq_capacity
1832static __always_inline
1833unsigned long arch_scale_max_freq_capacity(struct sched_domain *sd, int cpu)
1834{
1835 return SCHED_CAPACITY_SCALE;
1836}
1837#endif
1838
Ionela Voinescuba7a2aa2017-08-30 16:43:11 +01001839#ifndef arch_scale_min_freq_capacity
1840static __always_inline
1841unsigned long arch_scale_min_freq_capacity(struct sched_domain *sd, int cpu)
1842{
1843 /*
1844 * Multiplied with any capacity value, this scale factor will return
1845 * 0, which represents an un-capped state
1846 */
1847 return 0;
1848}
1849#endif
1850
Morten Rasmussen8cd56012015-08-14 17:23:10 +01001851#ifndef arch_scale_cpu_capacity
1852static __always_inline
1853unsigned long arch_scale_cpu_capacity(struct sched_domain *sd, int cpu)
1854{
Dietmar Eggemanne3279a22015-08-15 00:04:41 +01001855 if (sd && (sd->flags & SD_SHARE_CPUCAPACITY) && (sd->span_weight > 1))
Morten Rasmussen8cd56012015-08-14 17:23:10 +01001856 return sd->smt_gain / sd->span_weight;
1857
1858 return SCHED_CAPACITY_SCALE;
1859}
1860#endif
1861
Joonwoo Park28c51212017-06-09 14:06:54 -07001862#ifndef arch_update_cpu_capacity
1863static __always_inline
1864void arch_update_cpu_capacity(int cpu)
1865{
1866}
1867#endif
1868
Steve Muckle608d4942015-06-25 14:12:33 +01001869#ifdef CONFIG_SMP
1870static inline unsigned long capacity_of(int cpu)
1871{
1872 return cpu_rq(cpu)->cpu_capacity;
1873}
1874
1875static inline unsigned long capacity_orig_of(int cpu)
1876{
1877 return cpu_rq(cpu)->cpu_capacity_orig;
1878}
1879
Srivatsa Vaddagiri26c21542016-05-31 09:08:38 -07001880extern unsigned int walt_disabled;
1881
Pavankumar Kondeti3066bc42017-08-01 15:45:31 +05301882static inline unsigned long task_util(struct task_struct *p)
1883{
1884#ifdef CONFIG_SCHED_WALT
1885 if (!walt_disabled && sysctl_sched_use_walt_task_util)
1886 return p->ravg.demand /
1887 (sched_ravg_window >> SCHED_CAPACITY_SHIFT);
1888#endif
1889 return p->se.avg.util_avg;
1890}
1891
Steve Muckle608d4942015-06-25 14:12:33 +01001892/*
1893 * cpu_util returns the amount of capacity of a CPU that is used by CFS
1894 * tasks. The unit of the return value must be the one of capacity so we can
1895 * compare the utilization with the capacity of the CPU that is available for
1896 * CFS task (ie cpu_capacity).
1897 *
1898 * cfs_rq.avg.util_avg is the sum of running time of runnable tasks plus the
1899 * recent utilization of currently non-runnable tasks on a CPU. It represents
1900 * the amount of utilization of a CPU in the range [0..capacity_orig] where
1901 * capacity_orig is the cpu_capacity available at the highest frequency
1902 * (arch_scale_freq_capacity()).
1903 * The utilization of a CPU converges towards a sum equal to or less than the
1904 * current capacity (capacity_curr <= capacity_orig) of the CPU because it is
1905 * the running time on this CPU scaled by capacity_curr.
1906 *
1907 * Nevertheless, cfs_rq.avg.util_avg can be higher than capacity_curr or even
1908 * higher than capacity_orig because of unfortunate rounding in
1909 * cfs.avg.util_avg or just after migrating tasks and new task wakeups until
1910 * the average stabilizes with the new running time. We need to check that the
1911 * utilization stays within the range of [0..capacity_orig] and cap it if
1912 * necessary. Without utilization capping, a group could be seen as overloaded
1913 * (CPU0 utilization at 121% + CPU1 utilization at 80%) whereas CPU1 has 20% of
1914 * available capacity. We allow utilization to overshoot capacity_curr (but not
1915 * capacity_orig) as it useful for predicting the capacity required after task
1916 * migrations (scheduler-driven DVFS).
1917 */
1918static inline unsigned long __cpu_util(int cpu, int delta)
1919{
Joonwoo Park93a51bf2017-01-20 11:10:15 -08001920 u64 util = cpu_rq(cpu)->cfs.avg.util_avg;
Steve Muckle608d4942015-06-25 14:12:33 +01001921 unsigned long capacity = capacity_orig_of(cpu);
1922
Srivatsa Vaddagiri26c21542016-05-31 09:08:38 -07001923#ifdef CONFIG_SCHED_WALT
Amit Pundir102f7f42016-08-24 11:52:17 +05301924 if (!walt_disabled && sysctl_sched_use_walt_cpu_util) {
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +05301925 util = cpu_rq(cpu)->walt_stats.cumulative_runnable_avg;
Joonwoo Park93a51bf2017-01-20 11:10:15 -08001926 util = div64_u64(util,
1927 sched_ravg_window >> SCHED_CAPACITY_SHIFT);
Amit Pundir102f7f42016-08-24 11:52:17 +05301928 }
Srivatsa Vaddagiri26c21542016-05-31 09:08:38 -07001929#endif
Steve Muckle608d4942015-06-25 14:12:33 +01001930 delta += util;
1931 if (delta < 0)
1932 return 0;
1933
1934 return (delta >= capacity) ? capacity : delta;
1935}
1936
1937static inline unsigned long cpu_util(int cpu)
1938{
1939 return __cpu_util(cpu, 0);
1940}
1941
Joonwoo Park19c00752017-04-27 14:37:35 -07001942struct sched_walt_cpu_load {
1943 unsigned long prev_window_util;
1944 unsigned long nl;
1945 unsigned long pl;
Vikram Mulukutlaa65aafe2017-06-05 13:37:45 -07001946 u64 ws;
Joonwoo Park19c00752017-04-27 14:37:35 -07001947};
1948
Joonwoo Parke19cd6f2017-02-03 14:32:49 -08001949static inline unsigned long cpu_util_cum(int cpu, int delta)
1950{
1951 u64 util = cpu_rq(cpu)->cfs.avg.util_avg;
1952 unsigned long capacity = capacity_orig_of(cpu);
1953
1954#ifdef CONFIG_SCHED_WALT
1955 if (!walt_disabled && sysctl_sched_use_walt_cpu_util) {
1956 util = cpu_rq(cpu)->cum_window_demand;
1957 util = div64_u64(util,
1958 sched_ravg_window >> SCHED_CAPACITY_SHIFT);
1959 }
1960#endif
1961 delta += util;
1962 if (delta < 0)
1963 return 0;
1964
1965 return (delta >= capacity) ? capacity : delta;
1966}
1967
Syed Rameez Mustafa20acfe72017-01-30 09:35:46 +05301968#ifdef CONFIG_SCHED_WALT
1969u64 freq_policy_load(struct rq *rq);
1970#endif
1971
Joonwoo Park19c00752017-04-27 14:37:35 -07001972static inline unsigned long
Joonwoo Park858d5752017-08-21 12:09:49 -07001973cpu_util_freq_pelt(int cpu)
Joonwoo Park19c00752017-04-27 14:37:35 -07001974{
Syed Rameez Mustafa20acfe72017-01-30 09:35:46 +05301975 struct rq *rq = cpu_rq(cpu);
Maria Yu4837b1a2017-09-22 16:02:01 +08001976 u64 util = rq->cfs.avg.util_avg;
Joonwoo Park4266ccd2016-12-08 16:12:12 -08001977 unsigned long capacity = capacity_orig_of(cpu);
Joonwoo Park19c00752017-04-27 14:37:35 -07001978
Joonwoo Park858d5752017-08-21 12:09:49 -07001979 util *= (100 + per_cpu(sched_load_boost, cpu));
1980 do_div(util, 100);
Syed Rameez Mustafa20acfe72017-01-30 09:35:46 +05301981
Joonwoo Park4266ccd2016-12-08 16:12:12 -08001982 return (util >= capacity) ? capacity : util;
Joonwoo Park19c00752017-04-27 14:37:35 -07001983}
Joonwoo Park858d5752017-08-21 12:09:49 -07001984
1985#ifdef CONFIG_SCHED_WALT
Lingutla Chandrasekhard60cdac2018-05-25 15:22:59 +05301986extern u64 walt_load_reported_window;
Pavankumar Kondeti7fa15402018-05-08 12:15:08 +05301987
Joonwoo Park858d5752017-08-21 12:09:49 -07001988static inline unsigned long
1989cpu_util_freq_walt(int cpu, struct sched_walt_cpu_load *walt_load)
1990{
1991 u64 util, util_unboosted;
1992 struct rq *rq = cpu_rq(cpu);
1993 unsigned long capacity = capacity_orig_of(cpu);
1994 int boost;
1995
1996 if (walt_disabled || !sysctl_sched_use_walt_cpu_util)
1997 return cpu_util_freq_pelt(cpu);
1998
1999 boost = per_cpu(sched_load_boost, cpu);
2000 util_unboosted = util = freq_policy_load(rq);
2001 util = div64_u64(util * (100 + boost),
2002 walt_cpu_util_freq_divisor);
2003
2004 if (walt_load) {
2005 u64 nl = cpu_rq(cpu)->nt_prev_runnable_sum +
2006 rq->grp_time.nt_prev_runnable_sum;
2007 u64 pl = rq->walt_stats.pred_demands_sum;
2008
2009 /* do_pl_notif() needs unboosted signals */
2010 rq->old_busy_time = div64_u64(util_unboosted,
2011 sched_ravg_window >>
2012 SCHED_CAPACITY_SHIFT);
2013 rq->old_estimated_time = div64_u64(pl, sched_ravg_window >>
2014 SCHED_CAPACITY_SHIFT);
2015
2016 nl = div64_u64(nl * (100 + boost),
2017 walt_cpu_util_freq_divisor);
2018 pl = div64_u64(pl * (100 + boost),
2019 walt_cpu_util_freq_divisor);
2020
2021 walt_load->prev_window_util = util;
2022 walt_load->nl = nl;
2023 walt_load->pl = pl;
Lingutla Chandrasekhard60cdac2018-05-25 15:22:59 +05302024 walt_load->ws = walt_load_reported_window;
Joonwoo Park858d5752017-08-21 12:09:49 -07002025 }
2026
2027 return (util >= capacity) ? capacity : util;
2028}
2029
2030static inline unsigned long
2031cpu_util_freq(int cpu, struct sched_walt_cpu_load *walt_load)
2032{
2033 return cpu_util_freq_walt(cpu, walt_load);
2034}
2035
2036#else
2037
2038static inline unsigned long
2039cpu_util_freq(int cpu, struct sched_walt_cpu_load *walt_load)
2040{
2041 return cpu_util_freq_pelt(cpu);
2042}
2043
Pavankumar Kondetib1bd5e32018-04-03 19:34:03 +05302044#define sched_ravg_window TICK_NSEC
2045#define sysctl_sched_use_walt_cpu_util 0
2046
Joonwoo Park858d5752017-08-21 12:09:49 -07002047#endif /* CONFIG_SCHED_WALT */
2048
Pavankumar Kondeti12912ba2017-11-24 10:21:46 +05302049extern unsigned long
2050boosted_cpu_util(int cpu, struct sched_walt_cpu_load *walt_load);
Steve Muckle608d4942015-06-25 14:12:33 +01002051#endif
2052
Joonwoo Park2ae888b2017-01-12 17:46:00 -08002053extern unsigned int capacity_margin_freq;
2054
Joonwoo Park858d5752017-08-21 12:09:49 -07002055static inline unsigned long
2056add_capacity_margin(unsigned long cpu_capacity, int cpu)
Joonwoo Park2ae888b2017-01-12 17:46:00 -08002057{
Joonwoo Park858d5752017-08-21 12:09:49 -07002058 cpu_capacity = cpu_capacity * capacity_margin_freq *
2059 (100 + per_cpu(sched_load_boost, cpu));
2060 cpu_capacity /= 100;
Joonwoo Park2ae888b2017-01-12 17:46:00 -08002061 cpu_capacity /= SCHED_CAPACITY_SCALE;
2062 return cpu_capacity;
2063}
2064
Peter Zijlstra029632f2011-10-25 10:00:11 +02002065static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
2066{
Vincent Guittotb5b48602015-02-27 16:54:08 +01002067 rq->rt_avg += rt_delta * arch_scale_freq_capacity(NULL, cpu_of(rq));
Peter Zijlstra029632f2011-10-25 10:00:11 +02002068}
2069#else
2070static inline void sched_rt_avg_update(struct rq *rq, u64 rt_delta) { }
2071static inline void sched_avg_update(struct rq *rq) { }
2072#endif
2073
Patrick Bellasid2489002016-07-28 18:44:40 +01002074extern struct rq *lock_rq_of(struct task_struct *p, struct rq_flags *flags);
2075extern void unlock_rq_of(struct rq *rq, struct task_struct *p, struct rq_flags *flags);
2076
Peter Zijlstra029632f2011-10-25 10:00:11 +02002077#ifdef CONFIG_SMP
2078#ifdef CONFIG_PREEMPT
2079
2080static inline void double_rq_lock(struct rq *rq1, struct rq *rq2);
2081
2082/*
2083 * fair double_lock_balance: Safely acquires both rq->locks in a fair
2084 * way at the expense of forcing extra atomic operations in all
2085 * invocations. This assures that the double_lock is acquired using the
2086 * same underlying policy as the spinlock_t on this architecture, which
2087 * reduces latency compared to the unfair variant below. However, it
2088 * also adds more overhead and therefore may reduce throughput.
2089 */
2090static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
2091 __releases(this_rq->lock)
2092 __acquires(busiest->lock)
2093 __acquires(this_rq->lock)
2094{
2095 raw_spin_unlock(&this_rq->lock);
2096 double_rq_lock(this_rq, busiest);
2097
2098 return 1;
2099}
2100
2101#else
2102/*
2103 * Unfair double_lock_balance: Optimizes throughput at the expense of
2104 * latency by eliminating extra atomic operations when the locks are
2105 * already in proper order on entry. This favors lower cpu-ids and will
2106 * grant the double lock to lower cpus over higher ids under contention,
2107 * regardless of entry order into the function.
2108 */
2109static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
2110 __releases(this_rq->lock)
2111 __acquires(busiest->lock)
2112 __acquires(this_rq->lock)
2113{
2114 int ret = 0;
2115
2116 if (unlikely(!raw_spin_trylock(&busiest->lock))) {
2117 if (busiest < this_rq) {
2118 raw_spin_unlock(&this_rq->lock);
2119 raw_spin_lock(&busiest->lock);
2120 raw_spin_lock_nested(&this_rq->lock,
2121 SINGLE_DEPTH_NESTING);
2122 ret = 1;
2123 } else
2124 raw_spin_lock_nested(&busiest->lock,
2125 SINGLE_DEPTH_NESTING);
2126 }
2127 return ret;
2128}
2129
2130#endif /* CONFIG_PREEMPT */
2131
2132/*
2133 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
2134 */
2135static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
2136{
2137 if (unlikely(!irqs_disabled())) {
2138 /* printk() doesn't work good under rq->lock */
2139 raw_spin_unlock(&this_rq->lock);
2140 BUG_ON(1);
2141 }
2142
2143 return _double_lock_balance(this_rq, busiest);
2144}
2145
2146static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
2147 __releases(busiest->lock)
2148{
Todd Kjosa31778a2016-07-04 15:04:45 +01002149 if (this_rq != busiest)
2150 raw_spin_unlock(&busiest->lock);
Peter Zijlstra029632f2011-10-25 10:00:11 +02002151 lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
2152}
2153
Peter Zijlstra74602312013-10-10 20:17:22 +02002154static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
2155{
2156 if (l1 > l2)
2157 swap(l1, l2);
2158
2159 spin_lock(l1);
2160 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
2161}
2162
Mike Galbraith60e69ee2014-04-07 10:55:15 +02002163static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2)
2164{
2165 if (l1 > l2)
2166 swap(l1, l2);
2167
2168 spin_lock_irq(l1);
2169 spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
2170}
2171
Peter Zijlstra74602312013-10-10 20:17:22 +02002172static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
2173{
2174 if (l1 > l2)
2175 swap(l1, l2);
2176
2177 raw_spin_lock(l1);
2178 raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
2179}
2180
Peter Zijlstra029632f2011-10-25 10:00:11 +02002181/*
2182 * double_rq_lock - safely lock two runqueues
2183 *
2184 * Note this does not disable interrupts like task_rq_lock,
2185 * you need to do so manually before calling.
2186 */
2187static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
2188 __acquires(rq1->lock)
2189 __acquires(rq2->lock)
2190{
2191 BUG_ON(!irqs_disabled());
2192 if (rq1 == rq2) {
2193 raw_spin_lock(&rq1->lock);
2194 __acquire(rq2->lock); /* Fake it out ;) */
2195 } else {
2196 if (rq1 < rq2) {
2197 raw_spin_lock(&rq1->lock);
2198 raw_spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
2199 } else {
2200 raw_spin_lock(&rq2->lock);
2201 raw_spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
2202 }
2203 }
2204}
2205
2206/*
2207 * double_rq_unlock - safely unlock two runqueues
2208 *
2209 * Note this does not restore interrupts like task_rq_unlock,
2210 * you need to do so manually after calling.
2211 */
2212static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
2213 __releases(rq1->lock)
2214 __releases(rq2->lock)
2215{
2216 raw_spin_unlock(&rq1->lock);
2217 if (rq1 != rq2)
2218 raw_spin_unlock(&rq2->lock);
2219 else
2220 __release(rq2->lock);
2221}
2222
John Diase2c5c982016-09-15 08:52:27 -07002223/*
2224 * task_may_not_preempt - check whether a task may not be preemptible soon
2225 */
2226extern bool task_may_not_preempt(struct task_struct *task, int cpu);
2227
Peter Zijlstra029632f2011-10-25 10:00:11 +02002228#else /* CONFIG_SMP */
2229
2230/*
2231 * double_rq_lock - safely lock two runqueues
2232 *
2233 * Note this does not disable interrupts like task_rq_lock,
2234 * you need to do so manually before calling.
2235 */
2236static inline void double_rq_lock(struct rq *rq1, struct rq *rq2)
2237 __acquires(rq1->lock)
2238 __acquires(rq2->lock)
2239{
2240 BUG_ON(!irqs_disabled());
2241 BUG_ON(rq1 != rq2);
2242 raw_spin_lock(&rq1->lock);
2243 __acquire(rq2->lock); /* Fake it out ;) */
2244}
2245
2246/*
2247 * double_rq_unlock - safely unlock two runqueues
2248 *
2249 * Note this does not restore interrupts like task_rq_unlock,
2250 * you need to do so manually after calling.
2251 */
2252static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
2253 __releases(rq1->lock)
2254 __releases(rq2->lock)
2255{
2256 BUG_ON(rq1 != rq2);
2257 raw_spin_unlock(&rq1->lock);
2258 __release(rq2->lock);
2259}
2260
2261#endif
2262
2263extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
2264extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
Srikar Dronamraju6b55c962015-06-25 22:51:41 +05302265
2266#ifdef CONFIG_SCHED_DEBUG
Peter Zijlstra029632f2011-10-25 10:00:11 +02002267extern void print_cfs_stats(struct seq_file *m, int cpu);
2268extern void print_rt_stats(struct seq_file *m, int cpu);
Wanpeng Liacb32132014-10-31 06:39:33 +08002269extern void print_dl_stats(struct seq_file *m, int cpu);
Srikar Dronamraju6b55c962015-06-25 22:51:41 +05302270extern void
2271print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq);
Srikar Dronamraju397f2372015-06-25 22:51:43 +05302272
2273#ifdef CONFIG_NUMA_BALANCING
2274extern void
2275show_numa_stats(struct task_struct *p, struct seq_file *m);
2276extern void
2277print_numa_stats(struct seq_file *m, int node, unsigned long tsf,
2278 unsigned long tpf, unsigned long gsf, unsigned long gpf);
2279#endif /* CONFIG_NUMA_BALANCING */
2280#endif /* CONFIG_SCHED_DEBUG */
Peter Zijlstra029632f2011-10-25 10:00:11 +02002281
2282extern void init_cfs_rq(struct cfs_rq *cfs_rq);
Abel Vesa07c54f72015-03-03 13:50:27 +02002283extern void init_rt_rq(struct rt_rq *rt_rq);
2284extern void init_dl_rq(struct dl_rq *dl_rq);
Peter Zijlstra029632f2011-10-25 10:00:11 +02002285
Ben Segall1ee14e62013-10-16 11:16:12 -07002286extern void cfs_bandwidth_usage_inc(void);
2287extern void cfs_bandwidth_usage_dec(void);
Suresh Siddha1c792db2011-12-01 17:07:32 -08002288
Frederic Weisbecker3451d022011-08-10 23:21:01 +02002289#ifdef CONFIG_NO_HZ_COMMON
Suresh Siddha1c792db2011-12-01 17:07:32 -08002290enum rq_nohz_flag_bits {
2291 NOHZ_TICK_STOPPED,
2292 NOHZ_BALANCE_KICK,
2293};
2294
Syed Rameez Mustafadddcab72016-09-07 16:18:27 -07002295#define NOHZ_KICK_ANY 0
2296#define NOHZ_KICK_RESTRICT 1
2297
Suresh Siddha1c792db2011-12-01 17:07:32 -08002298#define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags)
Thomas Gleixner20a5c8c2016-03-10 12:54:20 +01002299
2300extern void nohz_balance_exit_idle(unsigned int cpu);
2301#else
2302static inline void nohz_balance_exit_idle(unsigned int cpu) { }
Suresh Siddha1c792db2011-12-01 17:07:32 -08002303#endif
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002304
2305#ifdef CONFIG_IRQ_TIME_ACCOUNTING
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002306struct irqtime {
Frederic Weisbeckeraee0c9e2017-04-25 16:10:48 +02002307 u64 total;
Frederic Weisbecker034cd8a2017-01-31 04:09:32 +01002308 u64 tick_delta;
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002309 u64 irq_start_time;
2310 struct u64_stats_sync sync;
2311};
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002312
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002313DECLARE_PER_CPU(struct irqtime, cpu_irqtime);
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002314
Frederic Weisbeckeraee0c9e2017-04-25 16:10:48 +02002315/*
2316 * Returns the irqtime minus the softirq time computed by ksoftirqd.
2317 * Otherwise ksoftirqd's sum_exec_runtime is substracted its own runtime
2318 * and never move forward.
2319 */
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002320static inline u64 irq_time_read(int cpu)
2321{
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002322 struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu);
2323 unsigned int seq;
2324 u64 total;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002325
2326 do {
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002327 seq = __u64_stats_fetch_begin(&irqtime->sync);
Frederic Weisbeckeraee0c9e2017-04-25 16:10:48 +02002328 total = irqtime->total;
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002329 } while (__u64_stats_fetch_retry(&irqtime->sync, seq));
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002330
Frederic Weisbecker19d23dbf2016-09-26 02:29:20 +02002331 return total;
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002332}
Frederic Weisbecker73fbec62012-06-16 15:57:37 +02002333#endif /* CONFIG_IRQ_TIME_ACCOUNTING */
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002334
Vikram Mulukutlaa65aafe2017-06-05 13:37:45 -07002335#ifdef CONFIG_SCHED_WALT
Pavankumar Kondetifaa04442018-06-25 16:13:39 +05302336u64 sched_ktime_clock(void);
Vikram Mulukutlac7b54b82017-07-12 11:34:54 -07002337void note_task_waking(struct task_struct *p, u64 wallclock);
Vikram Mulukutlaa65aafe2017-06-05 13:37:45 -07002338#else /* CONFIG_SCHED_WALT */
Pavankumar Kondetifaa04442018-06-25 16:13:39 +05302339static inline u64 sched_ktime_clock(void)
2340{
Santosh Mardi6b12e302020-03-12 09:18:34 +05302341 return sched_clock();
Pavankumar Kondetifaa04442018-06-25 16:13:39 +05302342}
Vikram Mulukutlac7b54b82017-07-12 11:34:54 -07002343static inline void note_task_waking(struct task_struct *p, u64 wallclock) { }
Vikram Mulukutlaa65aafe2017-06-05 13:37:45 -07002344#endif /* CONFIG_SCHED_WALT */
2345
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002346#ifdef CONFIG_CPU_FREQ
2347DECLARE_PER_CPU(struct update_util_data *, cpufreq_update_util_data);
2348
2349/**
2350 * cpufreq_update_util - Take a note about CPU utilization changes.
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02002351 * @rq: Runqueue to carry out the update for.
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02002352 * @flags: Update reason flags.
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002353 *
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02002354 * This function is called by the scheduler on the CPU whose utilization is
2355 * being updated.
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002356 *
2357 * It can only be called from RCU-sched read-side critical sections.
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002358 *
2359 * The way cpufreq is currently arranged requires it to evaluate the CPU
2360 * performance state (frequency/voltage) on a regular basis to prevent it from
2361 * being stuck in a completely inadequate performance level for too long.
2362 * That is not guaranteed to happen if the updates are only triggered from CFS,
2363 * though, because they may not be coming in if RT or deadline tasks are active
2364 * all the time (or there are RT and DL tasks only).
2365 *
2366 * As a workaround for that issue, this function is called by the RT and DL
2367 * sched classes to trigger extra cpufreq updates to prevent it from stalling,
2368 * but that really is a band-aid. Going forward it should be replaced with
2369 * solutions targeted more specifically at RT and DL tasks.
2370 */
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02002371static inline void cpufreq_update_util(struct rq *rq, unsigned int flags)
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002372{
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02002373 struct update_util_data *data;
Santosh Mardi6b12e302020-03-12 09:18:34 +05302374 u64 clock;
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02002375
Vikram Mulukutlaab968a42017-05-08 19:18:22 -07002376#ifdef CONFIG_SCHED_WALT
Vikram Mulukutlaf6686692017-06-06 11:58:27 -07002377 if (!(flags & SCHED_CPUFREQ_WALT))
2378 return;
Santosh Mardi6b12e302020-03-12 09:18:34 +05302379 clock = sched_ktime_clock();
2380#else
2381 clock = rq_clock(rq);
Vikram Mulukutla4b54aae2017-03-20 13:41:37 -07002382#endif
2383
Vikram Mulukutlabab41882017-05-09 17:49:47 -07002384 data = rcu_dereference_sched(*per_cpu_ptr(&cpufreq_update_util_data,
2385 cpu_of(rq)));
Rafael J. Wysocki58919e82016-08-16 22:14:55 +02002386 if (data)
Santosh Mardi6b12e302020-03-12 09:18:34 +05302387 data->func(data, clock, flags);
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02002388}
2389
2390static inline void cpufreq_update_this_cpu(struct rq *rq, unsigned int flags)
2391{
2392 if (cpu_of(rq) == smp_processor_id())
2393 cpufreq_update_util(rq, flags);
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002394}
2395#else
Rafael J. Wysocki12bde332016-08-10 03:11:17 +02002396static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) {}
2397static inline void cpufreq_update_this_cpu(struct rq *rq, unsigned int flags) {}
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002398#endif /* CONFIG_CPU_FREQ */
Linus Torvaldsbe53f582016-03-24 09:42:50 -07002399
Rafael J. Wysocki9bdcb442016-04-02 01:09:12 +02002400#ifdef arch_scale_freq_capacity
2401#ifndef arch_scale_freq_invariant
2402#define arch_scale_freq_invariant() (true)
2403#endif
2404#else /* arch_scale_freq_capacity */
2405#define arch_scale_freq_invariant() (false)
2406#endif
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002407
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -08002408#ifdef CONFIG_SCHED_WALT
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002409
2410static inline int cluster_first_cpu(struct sched_cluster *cluster)
2411{
2412 return cpumask_first(&cluster->cpus);
2413}
2414
2415struct related_thread_group {
2416 int id;
2417 raw_spinlock_t lock;
2418 struct list_head tasks;
2419 struct list_head list;
2420 struct sched_cluster *preferred_cluster;
2421 struct rcu_head rcu;
2422 u64 last_update;
2423};
2424
2425extern struct list_head cluster_head;
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002426extern struct sched_cluster *sched_cluster[NR_CPUS];
2427
2428#define for_each_sched_cluster(cluster) \
2429 list_for_each_entry_rcu(cluster, &cluster_head, list)
2430
2431#define WINDOW_STATS_RECENT 0
2432#define WINDOW_STATS_MAX 1
2433#define WINDOW_STATS_MAX_RECENT_AVG 2
2434#define WINDOW_STATS_AVG 3
2435#define WINDOW_STATS_INVALID_POLICY 4
2436
2437#define SCHED_UPMIGRATE_MIN_NICE 15
2438#define EXITING_TASK_MARKER 0xdeaddead
2439
2440#define UP_MIGRATION 1
2441#define DOWN_MIGRATION 2
2442#define IRQLOAD_MIGRATION 3
2443
2444extern struct mutex policy_mutex;
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002445extern unsigned int sched_disable_window_stats;
2446extern unsigned int max_possible_freq;
2447extern unsigned int min_max_freq;
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002448extern unsigned int max_possible_efficiency;
2449extern unsigned int min_possible_efficiency;
2450extern unsigned int max_capacity;
2451extern unsigned int min_capacity;
2452extern unsigned int max_load_scale_factor;
2453extern unsigned int max_possible_capacity;
2454extern unsigned int min_max_possible_capacity;
2455extern unsigned int max_power_cost;
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002456extern unsigned int up_down_migrate_scale_factor;
2457extern unsigned int sysctl_sched_restrict_cluster_spill;
2458extern unsigned int sched_pred_alert_load;
2459extern struct sched_cluster init_cluster;
2460extern unsigned int __read_mostly sched_short_sleep_task_threshold;
2461extern unsigned int __read_mostly sched_long_cpu_selection_threshold;
2462extern unsigned int __read_mostly sched_big_waker_task_load;
2463extern unsigned int __read_mostly sched_small_wakee_task_load;
2464extern unsigned int __read_mostly sched_spill_load;
2465extern unsigned int __read_mostly sched_upmigrate;
2466extern unsigned int __read_mostly sched_downmigrate;
2467extern unsigned int __read_mostly sysctl_sched_spill_nr_run;
2468extern unsigned int __read_mostly sched_load_granule;
2469
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002470extern int register_cpu_cycle_counter_cb(struct cpu_cycle_counter_cb *cb);
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002471extern int update_preferred_cluster(struct related_thread_group *grp,
2472 struct task_struct *p, u32 old_load);
2473extern void set_preferred_cluster(struct related_thread_group *grp);
2474extern void add_new_task_to_grp(struct task_struct *new);
2475extern unsigned int update_freq_aggregate_threshold(unsigned int threshold);
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002476
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002477static inline int cpu_capacity(int cpu)
2478{
2479 return cpu_rq(cpu)->cluster->capacity;
2480}
2481
2482static inline int cpu_max_possible_capacity(int cpu)
2483{
2484 return cpu_rq(cpu)->cluster->max_possible_capacity;
2485}
2486
2487static inline int cpu_load_scale_factor(int cpu)
2488{
2489 return cpu_rq(cpu)->cluster->load_scale_factor;
2490}
2491
2492static inline int cpu_efficiency(int cpu)
2493{
2494 return cpu_rq(cpu)->cluster->efficiency;
2495}
2496
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002497static inline unsigned int cpu_min_freq(int cpu)
2498{
2499 return cpu_rq(cpu)->cluster->min_freq;
2500}
2501
2502static inline unsigned int cluster_max_freq(struct sched_cluster *cluster)
2503{
2504 /*
2505 * Governor and thermal driver don't know the other party's mitigation
2506 * voting. So struct cluster saves both and return min() for current
2507 * cluster fmax.
2508 */
2509 return min(cluster->max_mitigated_freq, cluster->max_freq);
2510}
2511
2512static inline unsigned int cpu_max_freq(int cpu)
2513{
2514 return cluster_max_freq(cpu_rq(cpu)->cluster);
2515}
2516
2517static inline unsigned int cpu_max_possible_freq(int cpu)
2518{
2519 return cpu_rq(cpu)->cluster->max_possible_freq;
2520}
2521
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -08002522/* Keep track of max/min capacity possible across CPUs "currently" */
2523static inline void __update_min_max_capacity(void)
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002524{
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -08002525 int i;
2526 int max_cap = 0, min_cap = INT_MAX;
2527
Pavankumar Kondetif51d5392018-11-28 11:57:29 +05302528 for_each_possible_cpu(i) {
2529
2530 if (!cpu_active(i))
2531 continue;
2532
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -08002533 max_cap = max(max_cap, cpu_capacity(i));
2534 min_cap = min(min_cap, cpu_capacity(i));
2535 }
2536
2537 max_capacity = max_cap;
2538 min_capacity = min_cap;
2539}
2540
2541/*
2542 * Return load_scale_factor of a cpu in reference to "most" efficient cpu, so
2543 * that "most" efficient cpu gets a load_scale_factor of 1
2544 */
2545static inline unsigned long
2546load_scale_cpu_efficiency(struct sched_cluster *cluster)
2547{
2548 return DIV_ROUND_UP(1024 * max_possible_efficiency,
2549 cluster->efficiency);
2550}
2551
2552/*
2553 * Return load_scale_factor of a cpu in reference to cpu with best max_freq
2554 * (max_possible_freq), so that one with best max_freq gets a load_scale_factor
2555 * of 1.
2556 */
2557static inline unsigned long load_scale_cpu_freq(struct sched_cluster *cluster)
2558{
2559 return DIV_ROUND_UP(1024 * max_possible_freq,
2560 cluster_max_freq(cluster));
2561}
2562
2563static inline int compute_load_scale_factor(struct sched_cluster *cluster)
2564{
2565 int load_scale = 1024;
2566
2567 /*
2568 * load_scale_factor accounts for the fact that task load
2569 * is in reference to "best" performing cpu. Task's load will need to be
2570 * scaled (up) by a factor to determine suitability to be placed on a
2571 * (little) cpu.
2572 */
2573 load_scale *= load_scale_cpu_efficiency(cluster);
2574 load_scale >>= 10;
2575
2576 load_scale *= load_scale_cpu_freq(cluster);
2577 load_scale >>= 10;
2578
2579 return load_scale;
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002580}
2581
2582static inline int cpu_max_power_cost(int cpu)
2583{
2584 return cpu_rq(cpu)->cluster->max_power_cost;
2585}
2586
2587static inline int cpu_min_power_cost(int cpu)
2588{
2589 return cpu_rq(cpu)->cluster->min_power_cost;
2590}
2591
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002592static inline bool hmp_capable(void)
2593{
2594 return max_possible_capacity != min_max_possible_capacity;
2595}
2596
Pavankumar Kondeti005309282017-05-10 15:43:29 +05302597static inline bool is_max_capacity_cpu(int cpu)
2598{
2599 return cpu_max_possible_capacity(cpu) == max_possible_capacity;
2600}
2601
Pavankumar Kondeti271e3142017-06-23 14:20:24 +05302602static inline bool is_min_capacity_cpu(int cpu)
2603{
2604 return cpu_max_possible_capacity(cpu) == min_max_possible_capacity;
2605}
2606
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002607/*
2608 * 'load' is in reference to "best cpu" at its best frequency.
2609 * Scale that in reference to a given cpu, accounting for how bad it is
2610 * in reference to "best cpu".
2611 */
2612static inline u64 scale_load_to_cpu(u64 task_load, int cpu)
2613{
2614 u64 lsf = cpu_load_scale_factor(cpu);
2615
2616 if (lsf != 1024) {
2617 task_load *= lsf;
2618 task_load /= 1024;
2619 }
2620
2621 return task_load;
2622}
2623
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -08002624/*
2625 * Return 'capacity' of a cpu in reference to "least" efficient cpu, such that
2626 * least efficient cpu gets capacity of 1024
2627 */
2628static unsigned long
2629capacity_scale_cpu_efficiency(struct sched_cluster *cluster)
2630{
2631 return (1024 * cluster->efficiency) / min_possible_efficiency;
2632}
2633
2634/*
2635 * Return 'capacity' of a cpu in reference to cpu with lowest max_freq
2636 * (min_max_freq), such that one with lowest max_freq gets capacity of 1024.
2637 */
2638static unsigned long capacity_scale_cpu_freq(struct sched_cluster *cluster)
2639{
2640 return (1024 * cluster_max_freq(cluster)) / min_max_freq;
2641}
2642
2643static inline int compute_capacity(struct sched_cluster *cluster)
2644{
2645 int capacity = 1024;
2646
2647 capacity *= capacity_scale_cpu_efficiency(cluster);
2648 capacity >>= 10;
2649
2650 capacity *= capacity_scale_cpu_freq(cluster);
2651 capacity >>= 10;
2652
2653 return capacity;
2654}
2655
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002656static inline unsigned int task_load(struct task_struct *p)
2657{
2658 return p->ravg.demand;
2659}
2660
Pavankumar Kondetife1a6962017-07-25 11:08:17 +05302661static inline unsigned int task_pl(struct task_struct *p)
2662{
2663 return p->ravg.pred_demand;
2664}
2665
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002666#define pct_to_real(tunable) \
2667 (div64_u64((u64)tunable * (u64)max_task_load(), 100))
2668
2669#define real_to_pct(tunable) \
2670 (div64_u64((u64)tunable * (u64)100, (u64)max_task_load()))
2671
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002672static inline bool task_in_related_thread_group(struct task_struct *p)
2673{
2674 return !!(rcu_access_pointer(p->grp) != NULL);
2675}
2676
2677static inline
2678struct related_thread_group *task_related_thread_group(struct task_struct *p)
2679{
2680 return rcu_dereference(p->grp);
2681}
2682
2683#define PRED_DEMAND_DELTA ((s64)new_pred_demand - p->ravg.pred_demand)
2684
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002685/* Is frequency of two cpus synchronized with each other? */
2686static inline int same_freq_domain(int src_cpu, int dst_cpu)
2687{
2688 struct rq *rq = cpu_rq(src_cpu);
2689
2690 if (src_cpu == dst_cpu)
2691 return 1;
2692
2693 return cpumask_test_cpu(dst_cpu, &rq->freq_domain_cpumask);
2694}
2695
2696#define BOOST_KICK 0
2697#define CPU_RESERVED 1
2698
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002699extern int sched_boost(void);
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002700extern int preferred_cluster(struct sched_cluster *cluster,
2701 struct task_struct *p);
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002702extern struct sched_cluster *rq_cluster(struct rq *rq);
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002703extern void reset_task_stats(struct task_struct *p);
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002704extern void clear_top_tasks_bitmap(unsigned long *bitmap);
2705
2706#if defined(CONFIG_SCHED_TUNE) && defined(CONFIG_CGROUP_SCHEDTUNE)
2707extern bool task_sched_boost(struct task_struct *p);
2708extern int sync_cgroup_colocation(struct task_struct *p, bool insert);
2709extern bool same_schedtune(struct task_struct *tsk1, struct task_struct *tsk2);
2710extern void update_cgroup_boost_settings(void);
2711extern void restore_cgroup_boost_settings(void);
2712
2713#else
2714static inline bool
2715same_schedtune(struct task_struct *tsk1, struct task_struct *tsk2)
2716{
2717 return true;
2718}
2719
2720static inline bool task_sched_boost(struct task_struct *p)
2721{
2722 return true;
2723}
2724
2725static inline void update_cgroup_boost_settings(void) { }
2726static inline void restore_cgroup_boost_settings(void) { }
2727#endif
2728
2729extern int alloc_related_thread_groups(void);
2730
2731extern unsigned long all_cluster_ids[];
2732
Joonwoo Parke77a2012016-12-06 18:12:43 -08002733extern void check_for_migration(struct rq *rq, struct task_struct *p);
2734
2735static inline int is_reserved(int cpu)
2736{
2737 struct rq *rq = cpu_rq(cpu);
2738
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +05302739 return test_bit(CPU_RESERVED, &rq->walt_flags);
Joonwoo Parke77a2012016-12-06 18:12:43 -08002740}
2741
2742static inline int mark_reserved(int cpu)
2743{
2744 struct rq *rq = cpu_rq(cpu);
2745
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +05302746 return test_and_set_bit(CPU_RESERVED, &rq->walt_flags);
Joonwoo Parke77a2012016-12-06 18:12:43 -08002747}
2748
2749static inline void clear_reserved(int cpu)
2750{
2751 struct rq *rq = cpu_rq(cpu);
2752
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +05302753 clear_bit(CPU_RESERVED, &rq->walt_flags);
Joonwoo Parke77a2012016-12-06 18:12:43 -08002754}
2755
Joonwoo Park84a80882017-02-03 11:15:31 -08002756static inline bool
Joonwoo Park84a80882017-02-03 11:15:31 -08002757task_in_cum_window_demand(struct rq *rq, struct task_struct *p)
2758{
Pavankumar Kondeti0cebff02017-07-21 16:28:12 +05302759 return cpu_of(rq) == task_cpu(p) && (p->on_rq || p->last_sleep_ts >=
2760 rq->window_start);
Joonwoo Park84a80882017-02-03 11:15:31 -08002761}
2762
Pavankumar Kondeti0cebff02017-07-21 16:28:12 +05302763static inline void walt_fixup_cum_window_demand(struct rq *rq, s64 delta)
Joonwoo Park84a80882017-02-03 11:15:31 -08002764{
2765 rq->cum_window_demand += delta;
Pavankumar Kondeti0cebff02017-07-21 16:28:12 +05302766 if (unlikely((s64)rq->cum_window_demand < 0))
2767 rq->cum_window_demand = 0;
Joonwoo Park84a80882017-02-03 11:15:31 -08002768}
2769
Vikram Mulukutlad0ba1882017-02-03 12:56:26 -08002770extern void update_cpu_cluster_capacity(const cpumask_t *cpus);
2771
2772extern unsigned long thermal_cap(int cpu);
2773
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +05302774extern void clear_walt_request(int cpu);
Syed Rameez Mustafa20acfe72017-01-30 09:35:46 +05302775
2776extern int got_boost_kick(void);
2777extern void clear_boost_kick(int cpu);
2778extern enum sched_boost_policy sched_boost_policy(void);
2779extern void sched_boost_parse_dt(void);
Syed Rameez Mustafa25de0112017-05-10 12:09:15 -07002780extern void clear_ed_task(struct task_struct *p, struct rq *rq);
2781extern bool early_detection_notify(struct rq *rq, u64 wallclock);
Syed Rameez Mustafa20acfe72017-01-30 09:35:46 +05302782
Pavankumar Kondetic5927f12017-10-11 12:36:12 +05302783static inline unsigned int power_cost(int cpu, bool max)
Pavankumar Kondetif9026cd2017-06-07 15:03:32 +05302784{
Pavankumar Kondetic5927f12017-10-11 12:36:12 +05302785 struct sched_group_energy *sge = sge_array[cpu][SD_LEVEL1];
2786
2787 if (!sge || !sge->nr_cap_states)
2788 return cpu_max_possible_capacity(cpu);
2789
2790 if (max)
2791 return sge->cap_states[sge->nr_cap_states - 1].power;
2792 else
2793 return sge->cap_states[0].power;
Pavankumar Kondetif9026cd2017-06-07 15:03:32 +05302794}
Pavankumar Kondetif9026cd2017-06-07 15:03:32 +05302795
Pavankumar Kondetic5927f12017-10-11 12:36:12 +05302796extern void walt_sched_energy_populated_callback(void);
Pavankumar Kondetif51d5392018-11-28 11:57:29 +05302797extern void walt_update_min_max_capacity(void);
Pavankumar Kondetic5927f12017-10-11 12:36:12 +05302798
Abhijeet Dharmapurikar53ee4232018-06-15 09:34:34 -07002799static inline enum sched_boost_policy task_boost_policy(struct task_struct *p)
2800{
2801 enum sched_boost_policy boost_on_big = task_sched_boost(p) ?
2802 sched_boost_policy() : SCHED_BOOST_NONE;
2803
2804 if (boost_on_big) {
2805 /*
2806 * Filter out tasks less than min task util threshold
2807 * under conservative boost.
2808 */
2809 if (sysctl_sched_boost == CONSERVATIVE_BOOST &&
2810 task_util(p) <=
2811 sysctl_sched_min_task_util_for_boost_colocation)
2812 boost_on_big = SCHED_BOOST_NONE;
2813 }
2814
2815 return boost_on_big;
2816}
2817
Satya Durga Srinivasu Prabhalac18b4032018-04-17 11:21:23 -07002818extern void walt_map_freq_to_load(void);
2819
2820static inline bool is_min_capacity_cluster(struct sched_cluster *cluster)
2821{
2822 return is_min_capacity_cpu(cluster_first_cpu(cluster));
2823}
2824
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -08002825#else /* CONFIG_SCHED_WALT */
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002826
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +05302827struct walt_sched_stats;
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002828struct related_thread_group;
2829struct sched_cluster;
2830
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002831static inline bool task_sched_boost(struct task_struct *p)
2832{
2833 return true;
2834}
Abhijeet Dharmapurikar53ee4232018-06-15 09:34:34 -07002835static inline enum sched_boost_policy task_boost_policy(struct task_struct *p)
2836{
2837 return SCHED_BOOST_NONE;
2838}
Joonwoo Parke77a2012016-12-06 18:12:43 -08002839static inline void check_for_migration(struct rq *rq, struct task_struct *p) { }
2840
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002841static inline int sched_boost(void)
2842{
2843 return 0;
2844}
2845
Pavankumar Kondeti271e3142017-06-23 14:20:24 +05302846static inline bool hmp_capable(void) { return false; }
Pavankumar Kondeti005309282017-05-10 15:43:29 +05302847static inline bool is_max_capacity_cpu(int cpu) { return true; }
Pavankumar Kondeti271e3142017-06-23 14:20:24 +05302848static inline bool is_min_capacity_cpu(int cpu) { return true; }
Pavankumar Kondeti005309282017-05-10 15:43:29 +05302849
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002850static inline int
2851preferred_cluster(struct sched_cluster *cluster, struct task_struct *p)
2852{
2853 return 1;
2854}
2855
2856static inline struct sched_cluster *rq_cluster(struct rq *rq)
2857{
2858 return NULL;
2859}
2860
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002861static inline u64 scale_load_to_cpu(u64 load, int cpu)
2862{
2863 return load;
2864}
2865
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002866static inline int cpu_capacity(int cpu)
2867{
2868 return SCHED_CAPACITY_SCALE;
2869}
2870
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002871static inline void set_preferred_cluster(struct related_thread_group *grp) { }
2872
2873static inline bool task_in_related_thread_group(struct task_struct *p)
2874{
2875 return false;
2876}
2877
2878static inline
2879struct related_thread_group *task_related_thread_group(struct task_struct *p)
2880{
2881 return NULL;
2882}
2883
2884static inline u32 task_load(struct task_struct *p) { return 0; }
Pavankumar Kondetife1a6962017-07-25 11:08:17 +05302885static inline u32 task_pl(struct task_struct *p) { return 0; }
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002886
2887static inline int update_preferred_cluster(struct related_thread_group *grp,
2888 struct task_struct *p, u32 old_load)
2889{
2890 return 0;
2891}
2892
2893static inline void add_new_task_to_grp(struct task_struct *new) {}
2894
2895#define PRED_DEMAND_DELTA (0)
2896
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002897static inline int same_freq_domain(int src_cpu, int dst_cpu)
2898{
2899 return 1;
2900}
2901
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002902static inline void clear_reserved(int cpu) { }
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002903static inline int alloc_related_thread_groups(void) { return 0; }
2904
2905#define trace_sched_cpu_load(...)
2906#define trace_sched_cpu_load_lb(...)
2907#define trace_sched_cpu_load_cgroup(...)
2908#define trace_sched_cpu_load_wakeup(...)
2909
Pavankumar Kondeti0cebff02017-07-21 16:28:12 +05302910static inline void walt_fixup_cum_window_demand(struct rq *rq, s64 delta) { }
Joonwoo Park84a80882017-02-03 11:15:31 -08002911
Vikram Mulukutlad0ba1882017-02-03 12:56:26 -08002912static inline void update_cpu_cluster_capacity(const cpumask_t *cpus) { }
2913
2914#ifdef CONFIG_SMP
2915static inline unsigned long thermal_cap(int cpu)
2916{
2917 return cpu_rq(cpu)->cpu_capacity_orig;
2918}
Pavankumar Kondeti79830a22017-11-20 11:34:10 +05302919
2920static inline int cpu_max_power_cost(int cpu)
2921{
2922 return capacity_orig_of(cpu);
2923}
Vikram Mulukutlad0ba1882017-02-03 12:56:26 -08002924#endif
2925
Pavankumar Kondeti84f72d72017-07-20 11:00:45 +05302926static inline void clear_walt_request(int cpu) { }
Syed Rameez Mustafa20acfe72017-01-30 09:35:46 +05302927
Pavankumar Kondeti55beff92018-04-03 14:39:46 +05302928static inline int is_reserved(int cpu)
2929{
2930 return 0;
2931}
2932
Syed Rameez Mustafa20acfe72017-01-30 09:35:46 +05302933static inline int got_boost_kick(void)
2934{
2935 return 0;
2936}
2937
2938static inline void clear_boost_kick(int cpu) { }
2939
2940static inline enum sched_boost_policy sched_boost_policy(void)
2941{
2942 return SCHED_BOOST_NONE;
2943}
2944
2945static inline void sched_boost_parse_dt(void) { }
2946
Syed Rameez Mustafa25de0112017-05-10 12:09:15 -07002947static inline void clear_ed_task(struct task_struct *p, struct rq *rq) { }
2948
2949static inline bool early_detection_notify(struct rq *rq, u64 wallclock)
2950{
2951 return 0;
2952}
2953
Pavankumar Kondetic5927f12017-10-11 12:36:12 +05302954static inline unsigned int power_cost(int cpu, bool max)
Pavankumar Kondetif9026cd2017-06-07 15:03:32 +05302955{
2956 return SCHED_CAPACITY_SCALE;
2957}
2958
Pavankumar Kondetic5927f12017-10-11 12:36:12 +05302959static inline void walt_sched_energy_populated_callback(void) { }
Pavankumar Kondetif51d5392018-11-28 11:57:29 +05302960static inline void walt_update_min_max_capacity(void) { }
Pavankumar Kondetic5927f12017-10-11 12:36:12 +05302961
Satya Durga Srinivasu Prabhalac18b4032018-04-17 11:21:23 -07002962static inline void walt_map_freq_to_load(void) { }
Joonwoo Parkf7d6cd42017-01-17 15:19:43 -08002963#endif /* CONFIG_SCHED_WALT */
Vikram Mulukutlad056dbc2017-02-07 18:58:07 -08002964
Joonwoo Parkc5ddd4a2017-01-12 17:56:46 -08002965static inline bool energy_aware(void)
2966{
2967 return sched_feat(ENERGY_AWARE);
2968}
Joonwoo Parka5e601e2017-09-20 16:13:03 -07002969
2970#ifdef CONFIG_SCHED_CORE_ROTATE
2971struct find_first_cpu_bit_env {
2972 unsigned long *avoid_prev_cpu_last;
2973 int *rotate_cpu_start;
2974 int interval;
2975 spinlock_t *rotate_lock;
2976};
2977
2978int
2979find_first_cpu_bit(struct task_struct *p, const cpumask_t *search_cpus,
2980 struct sched_group *sg_target, bool *avoid_prev_cpu,
2981 bool *do_rotate, struct find_first_cpu_bit_env *env);
Pavankumar Kondetic72d3a42017-11-14 15:35:35 +05302982#else
2983#define find_first_cpu_bit(...) -1
Joonwoo Parka5e601e2017-09-20 16:13:03 -07002984#endif
Maria Yu702cec92019-08-13 17:12:33 +08002985
2986#ifdef CONFIG_SMP
2987static inline void sched_irq_work_queue(struct irq_work *work)
2988{
2989 if (likely(cpu_online(raw_smp_processor_id())))
2990 irq_work_queue(work);
2991 else
2992 irq_work_queue_on(work, cpumask_any(cpu_online_mask));
2993}
2994#endif